blob: 92d0f925d053c64326878afc14704b9d2877cdd7 [file] [log] [blame]
Matthias Urlichs58cfe912005-05-23 17:00:48 -07001/*
2 Option Card (PCMCIA to) USB to Serial Driver
3
4 Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
5
6 This driver is free software; you can redistribute it and/or modify
7 it under the terms of Version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
9
10 Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
11
12 History:
13
14 2005-05-19 v0.1 Initial version, based on incomplete docs
Matthias Urlichsba460e42005-07-14 00:33:47 -070015 and analysis of misbehavior with the standard driver
Matthias Urlichs58cfe912005-05-23 17:00:48 -070016 2005-05-20 v0.2 Extended the input buffer to avoid losing
17 random 64-byte chunks of data
18 2005-05-21 v0.3 implemented chars_in_buffer()
19 turned on low_latency
20 simplified the code somewhat
Matthias Urlichsba460e42005-07-14 00:33:47 -070021 2005-05-24 v0.4 option_write() sometimes deadlocked under heavy load
22 removed some dead code
23 added sponsor notice
24 coding style clean-up
25 2005-06-20 v0.4.1 add missing braces :-/
26 killed end-of-line whitespace
27 2005-07-15 v0.4.2 rename WLAN product to FUSION, add FUSION2
28
29 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
30
Matthias Urlichs58cfe912005-05-23 17:00:48 -070031*/
Matthias Urlichsba460e42005-07-14 00:33:47 -070032
33#define DRIVER_VERSION "v0.4"
Matthias Urlichs58cfe912005-05-23 17:00:48 -070034#define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
35#define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver"
36
37#include <linux/config.h>
38#include <linux/kernel.h>
39#include <linux/jiffies.h>
40#include <linux/errno.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/module.h>
44#include <linux/usb.h>
45#include "usb-serial.h"
46
47/* Function prototypes */
Andrew Morton7bb75ae2005-07-27 01:08:30 -070048static int option_open(struct usb_serial_port *port, struct file *filp);
49static void option_close(struct usb_serial_port *port, struct file *filp);
50static int option_startup(struct usb_serial *serial);
51static void option_shutdown(struct usb_serial *serial);
52static void option_rx_throttle(struct usb_serial_port *port);
53static void option_rx_unthrottle(struct usb_serial_port *port);
54static int option_write_room(struct usb_serial_port *port);
Matthias Urlichs58cfe912005-05-23 17:00:48 -070055
56static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
57
Andrew Morton7bb75ae2005-07-27 01:08:30 -070058static int option_write(struct usb_serial_port *port,
59 const unsigned char *buf, int count);
Matthias Urlichs58cfe912005-05-23 17:00:48 -070060
Andrew Morton7bb75ae2005-07-27 01:08:30 -070061static int option_chars_in_buffer(struct usb_serial_port *port);
62static int option_ioctl(struct usb_serial_port *port, struct file *file,
63 unsigned int cmd, unsigned long arg);
64static void option_set_termios(struct usb_serial_port *port,
65 struct termios *old);
66static void option_break_ctl(struct usb_serial_port *port, int break_state);
67static int option_tiocmget(struct usb_serial_port *port, struct file *file);
68static int option_tiocmset(struct usb_serial_port *port, struct file *file,
69 unsigned int set, unsigned int clear);
70static int option_send_setup(struct usb_serial_port *port);
Matthias Urlichs58cfe912005-05-23 17:00:48 -070071
72/* Vendor and product IDs */
Matthias Urlichsba460e42005-07-14 00:33:47 -070073#define OPTION_VENDOR_ID 0x0AF0
Matthias Urlichs58cfe912005-05-23 17:00:48 -070074
Matthias Urlichsba460e42005-07-14 00:33:47 -070075#define OPTION_PRODUCT_OLD 0x5000
76#define OPTION_PRODUCT_FUSION 0x6000
77#define OPTION_PRODUCT_FUSION2 0x6300
78
Matthias Urlichs58cfe912005-05-23 17:00:48 -070079static struct usb_device_id option_ids[] = {
80 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
Matthias Urlichsba460e42005-07-14 00:33:47 -070081 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
82 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
Matthias Urlichs58cfe912005-05-23 17:00:48 -070083 { } /* Terminating entry */
84};
85
86MODULE_DEVICE_TABLE(usb, option_ids);
87
88static struct usb_driver option_driver = {
89 .owner = THIS_MODULE,
90 .name = "option",
91 .probe = usb_serial_probe,
92 .disconnect = usb_serial_disconnect,
93 .id_table = option_ids,
94};
95
96/* The card has three separate interfaces, wich the serial driver
97 * recognizes separately, thus num_port=1.
98 */
99static struct usb_serial_device_type option_3port_device = {
Matthias Urlichsba460e42005-07-14 00:33:47 -0700100 .owner = THIS_MODULE,
101 .name = "Option 3G data card",
102 .short_name = "option",
103 .id_table = option_ids,
104 .num_interrupt_in = NUM_DONT_CARE,
105 .num_bulk_in = NUM_DONT_CARE,
106 .num_bulk_out = NUM_DONT_CARE,
107 .num_ports = 1, /* 3, but the card reports its ports separately */
108 .open = option_open,
109 .close = option_close,
110 .write = option_write,
111 .write_room = option_write_room,
112 .chars_in_buffer = option_chars_in_buffer,
113 .throttle = option_rx_throttle,
114 .unthrottle = option_rx_unthrottle,
115 .ioctl = option_ioctl,
116 .set_termios = option_set_termios,
117 .break_ctl = option_break_ctl,
118 .tiocmget = option_tiocmget,
119 .tiocmset = option_tiocmset,
120 .attach = option_startup,
121 .shutdown = option_shutdown,
122 .read_int_callback = option_instat_callback,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700123};
124
Matthias Urlichsba460e42005-07-14 00:33:47 -0700125#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700126static int debug;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700127#else
128#define debug 0
129#endif
130
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700131/* per port private data */
132
Matthias Urlichsba460e42005-07-14 00:33:47 -0700133#define N_IN_URB 4
134#define N_OUT_URB 1
135#define IN_BUFLEN 1024
136#define OUT_BUFLEN 128
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700137
138struct option_port_private {
139 /* Input endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700140 struct urb *in_urbs[N_IN_URB];
141 char in_buffer[N_IN_URB][IN_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700142 /* Output endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700143 struct urb *out_urbs[N_OUT_URB];
144 char out_buffer[N_OUT_URB][OUT_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700145
146 /* Settings for the port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700147 int rts_state; /* Handshaking pins (outputs) */
148 int dtr_state;
149 int cts_state; /* Handshaking pins (inputs) */
150 int dsr_state;
151 int dcd_state;
152 int ri_state;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700153
Matthias Urlichsba460e42005-07-14 00:33:47 -0700154 unsigned long tx_start_time[N_OUT_URB];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700155};
156
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700157/* Functions used by new usb-serial code. */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700158static int __init option_init(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700159{
160 int retval;
161 retval = usb_serial_register(&option_3port_device);
162 if (retval)
163 goto failed_3port_device_register;
164 retval = usb_register(&option_driver);
165 if (retval)
166 goto failed_driver_register;
167
168 info(DRIVER_DESC ": " DRIVER_VERSION);
169
170 return 0;
171
172failed_driver_register:
173 usb_serial_deregister (&option_3port_device);
174failed_3port_device_register:
175 return retval;
176}
177
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700178static void __exit option_exit(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700179{
180 usb_deregister (&option_driver);
181 usb_serial_deregister (&option_3port_device);
182}
183
184module_init(option_init);
185module_exit(option_exit);
186
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700187static void option_rx_throttle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700188{
189 dbg("%s", __FUNCTION__);
190}
191
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700192static void option_rx_unthrottle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700193{
194 dbg("%s", __FUNCTION__);
195}
196
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700197static void option_break_ctl(struct usb_serial_port *port, int break_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700198{
199 /* Unfortunately, I don't know how to send a break */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700200 dbg("%s", __FUNCTION__);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700201}
202
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700203static void option_set_termios(struct usb_serial_port *port,
204 struct termios *old_termios)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700205{
206 dbg("%s", __FUNCTION__);
207
208 option_send_setup(port);
209}
210
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700211static int option_tiocmget(struct usb_serial_port *port, struct file *file)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700212{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700213 unsigned int value;
214 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700215
216 portdata = usb_get_serial_port_data(port);
217
218 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
219 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
220 ((portdata->cts_state) ? TIOCM_CTS : 0) |
221 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
222 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
223 ((portdata->ri_state) ? TIOCM_RNG : 0);
224
225 return value;
226}
227
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700228static int option_tiocmset(struct usb_serial_port *port, struct file *file,
229 unsigned int set, unsigned int clear)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700230{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700231 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700232
233 portdata = usb_get_serial_port_data(port);
234
235 if (set & TIOCM_RTS)
236 portdata->rts_state = 1;
237 if (set & TIOCM_DTR)
238 portdata->dtr_state = 1;
239
240 if (clear & TIOCM_RTS)
241 portdata->rts_state = 0;
242 if (clear & TIOCM_DTR)
243 portdata->dtr_state = 0;
244 return option_send_setup(port);
245}
246
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700247static int option_ioctl(struct usb_serial_port *port, struct file *file,
248 unsigned int cmd, unsigned long arg)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700249{
250 return -ENOIOCTLCMD;
251}
252
253/* Write */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700254static int option_write(struct usb_serial_port *port,
255 const unsigned char *buf, int count)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700256{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700257 struct option_port_private *portdata;
258 int i;
259 int left, todo;
260 struct urb *this_urb = NULL; /* spurious */
261 int err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700262
263 portdata = usb_get_serial_port_data(port);
264
265 dbg("%s: write (%d chars)", __FUNCTION__, count);
266
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700267 i = 0;
268 left = count;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700269 for (i=0; left > 0 && i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700270 todo = left;
271 if (todo > OUT_BUFLEN)
272 todo = OUT_BUFLEN;
273
Matthias Urlichsba460e42005-07-14 00:33:47 -0700274 this_urb = portdata->out_urbs[i];
275 if (this_urb->status == -EINPROGRESS) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700276 if (time_before(jiffies,
277 portdata->tx_start_time[i] + 10 * HZ))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700278 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700279 usb_unlink_urb(this_urb);
Matthias Urlichsba460e42005-07-14 00:33:47 -0700280 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700281 }
Matthias Urlichsba460e42005-07-14 00:33:47 -0700282 if (this_urb->status != 0)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700283 dbg("usb_write %p failed (err=%d)",
284 this_urb, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700285
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700286 dbg("%s: endpoint %d buf %d", __FUNCTION__,
287 usb_pipeendpoint(this_urb->pipe), i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700288
Matthias Urlichsba460e42005-07-14 00:33:47 -0700289 /* send the data */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700290 memcpy (this_urb->transfer_buffer, buf, todo);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700291 this_urb->transfer_buffer_length = todo;
292
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700293 this_urb->dev = port->serial->dev;
294 err = usb_submit_urb(this_urb, GFP_ATOMIC);
295 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700296 dbg("usb_submit_urb %p (write bulk) failed "
297 "(%d, has %d)", this_urb,
298 err, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700299 continue;
300 }
301 portdata->tx_start_time[i] = jiffies;
302 buf += todo;
303 left -= todo;
304 }
305
306 count -= left;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700307 dbg("%s: wrote (did %d)", __FUNCTION__, count);
308 return count;
309}
310
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700311static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700312{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700313 int i, err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700314 int endpoint;
315 struct usb_serial_port *port;
316 struct tty_struct *tty;
317 unsigned char *data = urb->transfer_buffer;
318
319 dbg("%s: %p", __FUNCTION__, urb);
320
321 endpoint = usb_pipeendpoint(urb->pipe);
322 port = (struct usb_serial_port *) urb->context;
323
324 if (urb->status) {
325 dbg("%s: nonzero status: %d on endpoint %02x.",
326 __FUNCTION__, urb->status, endpoint);
327 } else {
328 tty = port->tty;
329 if (urb->actual_length) {
330 for (i = 0; i < urb->actual_length ; ++i) {
331 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
332 tty_flip_buffer_push(tty);
333 tty_insert_flip_char(tty, data[i], 0);
334 }
335 tty_flip_buffer_push(tty);
336 } else {
337 dbg("%s: empty read urb received", __FUNCTION__);
338 }
339
340 /* Resubmit urb so we continue receiving */
341 if (port->open_count && urb->status != -ESHUTDOWN) {
342 err = usb_submit_urb(urb, GFP_ATOMIC);
343 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700344 printk(KERN_ERR "%s: resubmit read urb failed. "
345 "(%d)", __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700346 }
347 }
348 return;
349}
350
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700351static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700352{
353 struct usb_serial_port *port;
354
355 dbg("%s", __FUNCTION__);
356
357 port = (struct usb_serial_port *) urb->context;
358
359 if (port->open_count)
360 schedule_work(&port->work);
361}
362
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700363static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700364{
365 int err;
366 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
367 struct option_port_private *portdata = usb_get_serial_port_data(port);
368 struct usb_serial *serial = port->serial;
369
370 dbg("%s", __FUNCTION__);
371 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
372
373 if (urb->status == 0) {
374 struct usb_ctrlrequest *req_pkt =
375 (struct usb_ctrlrequest *)urb->transfer_buffer;
376
377 if (!req_pkt) {
378 dbg("%s: NULL req_pkt\n", __FUNCTION__);
379 return;
380 }
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700381 if ((req_pkt->bRequestType == 0xA1) &&
382 (req_pkt->bRequest == 0x20)) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700383 int old_dcd_state;
384 unsigned char signals = *((unsigned char *)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700385 urb->transfer_buffer +
386 sizeof(struct usb_ctrlrequest));
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700387
388 dbg("%s: signal x%x", __FUNCTION__, signals);
389
390 old_dcd_state = portdata->dcd_state;
391 portdata->cts_state = 1;
392 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
393 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
394 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
395
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700396 if (port->tty && !C_CLOCAL(port->tty) &&
397 old_dcd_state && !portdata->dcd_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700398 tty_hangup(port->tty);
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700399 } else {
400 dbg("%s: type %x req %x", __FUNCTION__,
401 req_pkt->bRequestType,req_pkt->bRequest);
402 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700403 } else
404 dbg("%s: error %d", __FUNCTION__, urb->status);
405
406 /* Resubmit urb so we continue receiving IRQ data */
407 if (urb->status != -ESHUTDOWN) {
408 urb->dev = serial->dev;
409 err = usb_submit_urb(urb, GFP_ATOMIC);
410 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700411 dbg("%s: resubmit intr urb failed. (%d)",
412 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700413 }
414}
415
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700416static int option_write_room(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700417{
418 struct option_port_private *portdata;
419 int i;
420 int data_len = 0;
421 struct urb *this_urb;
422
423 portdata = usb_get_serial_port_data(port);
424
Matthias Urlichsba460e42005-07-14 00:33:47 -0700425 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700426 this_urb = portdata->out_urbs[i];
427 if (this_urb && this_urb->status != -EINPROGRESS)
428 data_len += OUT_BUFLEN;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700429 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700430
431 dbg("%s: %d", __FUNCTION__, data_len);
432 return data_len;
433}
434
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700435static int option_chars_in_buffer(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700436{
437 struct option_port_private *portdata;
438 int i;
439 int data_len = 0;
440 struct urb *this_urb;
441
442 portdata = usb_get_serial_port_data(port);
443
Matthias Urlichsba460e42005-07-14 00:33:47 -0700444 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700445 this_urb = portdata->out_urbs[i];
446 if (this_urb && this_urb->status == -EINPROGRESS)
447 data_len += this_urb->transfer_buffer_length;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700448 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700449 dbg("%s: %d", __FUNCTION__, data_len);
450 return data_len;
451}
452
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700453static int option_open(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700454{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700455 struct option_port_private *portdata;
456 struct usb_serial *serial = port->serial;
457 int i, err;
458 struct urb *urb;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700459
460 portdata = usb_get_serial_port_data(port);
461
462 dbg("%s", __FUNCTION__);
463
464 /* Set some sane defaults */
465 portdata->rts_state = 1;
466 portdata->dtr_state = 1;
467
468 /* Reset low level data toggle and start reading from endpoints */
469 for (i = 0; i < N_IN_URB; i++) {
470 urb = portdata->in_urbs[i];
471 if (! urb)
472 continue;
473 if (urb->dev != serial->dev) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700474 dbg("%s: dev %p != %p", __FUNCTION__,
475 urb->dev, serial->dev);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700476 continue;
477 }
478
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700479 /*
480 * make sure endpoint data toggle is synchronized with the
481 * device
482 */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700483 usb_clear_halt(urb->dev, urb->pipe);
484
485 err = usb_submit_urb(urb, GFP_KERNEL);
486 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700487 dbg("%s: submit urb %d failed (%d) %d",
488 __FUNCTION__, i, err,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700489 urb->transfer_buffer_length);
490 }
491 }
492
493 /* Reset low level data toggle on out endpoints */
494 for (i = 0; i < N_OUT_URB; i++) {
495 urb = portdata->out_urbs[i];
496 if (! urb)
497 continue;
498 urb->dev = serial->dev;
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700499 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
500 usb_pipeout(urb->pipe), 0); */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700501 }
502
503 port->tty->low_latency = 1;
504
505 option_send_setup(port);
506
507 return (0);
508}
509
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700510static inline void stop_urb(struct urb *urb)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700511{
Greg Kroah-Hartman242cf672005-07-29 16:11:07 -0400512 if (urb && urb->status == -EINPROGRESS)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700513 usb_kill_urb(urb);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700514}
515
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700516static void option_close(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700517{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700518 int i;
519 struct usb_serial *serial = port->serial;
520 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700521
522 dbg("%s", __FUNCTION__);
523 portdata = usb_get_serial_port_data(port);
524
525 portdata->rts_state = 0;
526 portdata->dtr_state = 0;
527
528 if (serial->dev) {
529 option_send_setup(port);
530
531 /* Stop reading/writing urbs */
532 for (i = 0; i < N_IN_URB; i++)
533 stop_urb(portdata->in_urbs[i]);
534 for (i = 0; i < N_OUT_URB; i++)
535 stop_urb(portdata->out_urbs[i]);
536 }
537 port->tty = NULL;
538}
539
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700540/* Helper functions used by option_setup_urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700541static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
542 int dir, void *ctx, char *buf, int len,
543 void (*callback)(struct urb *, struct pt_regs *regs))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700544{
545 struct urb *urb;
546
547 if (endpoint == -1)
548 return NULL; /* endpoint not needed */
549
550 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
551 if (urb == NULL) {
552 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
553 return NULL;
554 }
555
556 /* Fill URB using supplied data. */
557 usb_fill_bulk_urb(urb, serial->dev,
558 usb_sndbulkpipe(serial->dev, endpoint) | dir,
559 buf, len, callback, ctx);
560
561 return urb;
562}
563
564/* Setup urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700565static void option_setup_urbs(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700566{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700567 int j;
568 struct usb_serial_port *port;
569 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700570
571 dbg("%s", __FUNCTION__);
572
573 port = serial->port[0];
574 portdata = usb_get_serial_port_data(port);
575
576 /* Do indat endpoints first */
577 for (j = 0; j <= N_IN_URB; ++j) {
578 portdata->in_urbs[j] = option_setup_urb (serial,
579 port->bulk_in_endpointAddress, USB_DIR_IN, port,
580 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
581 }
582
583 /* outdat endpoints */
584 for (j = 0; j <= N_OUT_URB; ++j) {
585 portdata->out_urbs[j] = option_setup_urb (serial,
586 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
587 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
588 }
589}
590
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700591static int option_send_setup(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700592{
593 struct usb_serial *serial = port->serial;
594 struct option_port_private *portdata;
595
596 dbg("%s", __FUNCTION__);
597
598 portdata = usb_get_serial_port_data(port);
599
600 if (port->tty) {
601 int val = 0;
602 if (portdata->dtr_state)
603 val |= 0x01;
604 if (portdata->rts_state)
605 val |= 0x02;
606
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700607 return usb_control_msg(serial->dev,
608 usb_rcvctrlpipe(serial->dev, 0),
609 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700610 }
611
612 return 0;
613}
614
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700615static int option_startup(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700616{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700617 int i, err;
618 struct usb_serial_port *port;
619 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700620
621 dbg("%s", __FUNCTION__);
622
623 /* Now setup per port private data */
624 for (i = 0; i < serial->num_ports; i++) {
625 port = serial->port[i];
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700626 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700627 if (!portdata) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700628 dbg("%s: kmalloc for option_port_private (%d) failed!.",
629 __FUNCTION__, i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700630 return (1);
631 }
632 memset(portdata, 0, sizeof(struct option_port_private));
633
634 usb_set_serial_port_data(port, portdata);
635
636 if (! port->interrupt_in_urb)
637 continue;
638 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
639 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700640 dbg("%s: submit irq_in urb failed %d",
641 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700642 }
643
644 option_setup_urbs(serial);
645
646 return (0);
647}
648
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700649static void option_shutdown(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700650{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700651 int i, j;
652 struct usb_serial_port *port;
653 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700654
655 dbg("%s", __FUNCTION__);
656
657 /* Stop reading/writing urbs */
658 for (i = 0; i < serial->num_ports; ++i) {
659 port = serial->port[i];
660 portdata = usb_get_serial_port_data(port);
661 for (j = 0; j < N_IN_URB; j++)
662 stop_urb(portdata->in_urbs[j]);
663 for (j = 0; j < N_OUT_URB; j++)
664 stop_urb(portdata->out_urbs[j]);
665 }
666
667 /* Now free them */
668 for (i = 0; i < serial->num_ports; ++i) {
669 port = serial->port[i];
670 portdata = usb_get_serial_port_data(port);
671
672 for (j = 0; j < N_IN_URB; j++) {
673 if (portdata->in_urbs[j]) {
674 usb_free_urb(portdata->in_urbs[j]);
675 portdata->in_urbs[j] = NULL;
676 }
677 }
678 for (j = 0; j < N_OUT_URB; j++) {
679 if (portdata->out_urbs[j]) {
680 usb_free_urb(portdata->out_urbs[j]);
681 portdata->out_urbs[j] = NULL;
682 }
683 }
684 }
685
686 /* Now free per port private data */
687 for (i = 0; i < serial->num_ports; i++) {
688 port = serial->port[i];
689 kfree(usb_get_serial_port_data(port));
690 }
691}
692
693MODULE_AUTHOR(DRIVER_AUTHOR);
694MODULE_DESCRIPTION(DRIVER_DESC);
695MODULE_VERSION(DRIVER_VERSION);
696MODULE_LICENSE("GPL");
697
Matthias Urlichsba460e42005-07-14 00:33:47 -0700698#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700699module_param(debug, bool, S_IRUGO | S_IWUSR);
700MODULE_PARM_DESC(debug, "Debug messages");
Matthias Urlichsba460e42005-07-14 00:33:47 -0700701#endif
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700702