Commit | Line | Data |
---|---|---|
58cfe911 MU |
1 | /* |
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 | |
ba460e48 | 15 | and analysis of misbehavior with the standard driver |
58cfe911 MU |
16 | 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 | |
ba460e48 MU |
21 | 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 | ||
58cfe911 | 31 | */ |
ba460e48 MU |
32 | |
33 | #define DRIVER_VERSION "v0.4" | |
58cfe911 MU |
34 | #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 */ | |
7bb75aee AM |
48 | static int option_open(struct usb_serial_port *port, struct file *filp); |
49 | static void option_close(struct usb_serial_port *port, struct file *filp); | |
50 | static int option_startup(struct usb_serial *serial); | |
51 | static void option_shutdown(struct usb_serial *serial); | |
52 | static void option_rx_throttle(struct usb_serial_port *port); | |
53 | static void option_rx_unthrottle(struct usb_serial_port *port); | |
54 | static int option_write_room(struct usb_serial_port *port); | |
58cfe911 MU |
55 | |
56 | static void option_instat_callback(struct urb *urb, struct pt_regs *regs); | |
57 | ||
7bb75aee AM |
58 | static int option_write(struct usb_serial_port *port, |
59 | const unsigned char *buf, int count); | |
58cfe911 | 60 | |
7bb75aee AM |
61 | static int option_chars_in_buffer(struct usb_serial_port *port); |
62 | static int option_ioctl(struct usb_serial_port *port, struct file *file, | |
63 | unsigned int cmd, unsigned long arg); | |
64 | static void option_set_termios(struct usb_serial_port *port, | |
65 | struct termios *old); | |
66 | static void option_break_ctl(struct usb_serial_port *port, int break_state); | |
67 | static int option_tiocmget(struct usb_serial_port *port, struct file *file); | |
68 | static int option_tiocmset(struct usb_serial_port *port, struct file *file, | |
69 | unsigned int set, unsigned int clear); | |
70 | static int option_send_setup(struct usb_serial_port *port); | |
58cfe911 MU |
71 | |
72 | /* Vendor and product IDs */ | |
ba460e48 MU |
73 | #define OPTION_VENDOR_ID 0x0AF0 |
74 | ||
75 | #define OPTION_PRODUCT_OLD 0x5000 | |
76 | #define OPTION_PRODUCT_FUSION 0x6000 | |
77 | #define OPTION_PRODUCT_FUSION2 0x6300 | |
58cfe911 | 78 | |
58cfe911 MU |
79 | static struct usb_device_id option_ids[] = { |
80 | { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) }, | |
ba460e48 MU |
81 | { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) }, |
82 | { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) }, | |
58cfe911 MU |
83 | { } /* Terminating entry */ |
84 | }; | |
85 | ||
86 | MODULE_DEVICE_TABLE(usb, option_ids); | |
87 | ||
88 | static 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 | */ | |
99 | static struct usb_serial_device_type option_3port_device = { | |
ba460e48 MU |
100 | .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, | |
58cfe911 MU |
123 | }; |
124 | ||
ba460e48 | 125 | #ifdef CONFIG_USB_DEBUG |
58cfe911 | 126 | static int debug; |
ba460e48 MU |
127 | #else |
128 | #define debug 0 | |
129 | #endif | |
130 | ||
58cfe911 MU |
131 | /* per port private data */ |
132 | ||
ba460e48 MU |
133 | #define N_IN_URB 4 |
134 | #define N_OUT_URB 1 | |
135 | #define IN_BUFLEN 1024 | |
136 | #define OUT_BUFLEN 128 | |
58cfe911 MU |
137 | |
138 | struct option_port_private { | |
139 | /* Input endpoints and buffer for this port */ | |
ba460e48 MU |
140 | struct urb *in_urbs[N_IN_URB]; |
141 | char in_buffer[N_IN_URB][IN_BUFLEN]; | |
58cfe911 | 142 | /* Output endpoints and buffer for this port */ |
ba460e48 MU |
143 | struct urb *out_urbs[N_OUT_URB]; |
144 | char out_buffer[N_OUT_URB][OUT_BUFLEN]; | |
58cfe911 MU |
145 | |
146 | /* Settings for the port */ | |
ba460e48 MU |
147 | 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; | |
153 | ||
154 | unsigned long tx_start_time[N_OUT_URB]; | |
58cfe911 MU |
155 | }; |
156 | ||
58cfe911 | 157 | /* Functions used by new usb-serial code. */ |
7bb75aee | 158 | static int __init option_init(void) |
58cfe911 MU |
159 | { |
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 | ||
172 | failed_driver_register: | |
173 | usb_serial_deregister (&option_3port_device); | |
174 | failed_3port_device_register: | |
175 | return retval; | |
176 | } | |
177 | ||
7bb75aee | 178 | static void __exit option_exit(void) |
58cfe911 MU |
179 | { |
180 | usb_deregister (&option_driver); | |
181 | usb_serial_deregister (&option_3port_device); | |
182 | } | |
183 | ||
184 | module_init(option_init); | |
185 | module_exit(option_exit); | |
186 | ||
7bb75aee | 187 | static void option_rx_throttle(struct usb_serial_port *port) |
58cfe911 MU |
188 | { |
189 | dbg("%s", __FUNCTION__); | |
190 | } | |
191 | ||
7bb75aee | 192 | static void option_rx_unthrottle(struct usb_serial_port *port) |
58cfe911 MU |
193 | { |
194 | dbg("%s", __FUNCTION__); | |
195 | } | |
196 | ||
7bb75aee | 197 | static void option_break_ctl(struct usb_serial_port *port, int break_state) |
58cfe911 MU |
198 | { |
199 | /* Unfortunately, I don't know how to send a break */ | |
ba460e48 | 200 | dbg("%s", __FUNCTION__); |
58cfe911 MU |
201 | } |
202 | ||
7bb75aee AM |
203 | static void option_set_termios(struct usb_serial_port *port, |
204 | struct termios *old_termios) | |
58cfe911 MU |
205 | { |
206 | dbg("%s", __FUNCTION__); | |
207 | ||
208 | option_send_setup(port); | |
209 | } | |
210 | ||
7bb75aee | 211 | static int option_tiocmget(struct usb_serial_port *port, struct file *file) |
58cfe911 | 212 | { |
ba460e48 MU |
213 | unsigned int value; |
214 | struct option_port_private *portdata; | |
58cfe911 MU |
215 | |
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 | ||
7bb75aee AM |
228 | static int option_tiocmset(struct usb_serial_port *port, struct file *file, |
229 | unsigned int set, unsigned int clear) | |
58cfe911 | 230 | { |
ba460e48 | 231 | struct option_port_private *portdata; |
58cfe911 MU |
232 | |
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 | ||
7bb75aee AM |
247 | static int option_ioctl(struct usb_serial_port *port, struct file *file, |
248 | unsigned int cmd, unsigned long arg) | |
58cfe911 MU |
249 | { |
250 | return -ENOIOCTLCMD; | |
251 | } | |
252 | ||
253 | /* Write */ | |
7bb75aee AM |
254 | static int option_write(struct usb_serial_port *port, |
255 | const unsigned char *buf, int count) | |
58cfe911 | 256 | { |
ba460e48 MU |
257 | struct option_port_private *portdata; |
258 | int i; | |
259 | int left, todo; | |
260 | struct urb *this_urb = NULL; /* spurious */ | |
261 | int err; | |
58cfe911 MU |
262 | |
263 | portdata = usb_get_serial_port_data(port); | |
264 | ||
265 | dbg("%s: write (%d chars)", __FUNCTION__, count); | |
266 | ||
58cfe911 MU |
267 | i = 0; |
268 | left = count; | |
ba460e48 | 269 | for (i=0; left > 0 && i < N_OUT_URB; i++) { |
58cfe911 MU |
270 | todo = left; |
271 | if (todo > OUT_BUFLEN) | |
272 | todo = OUT_BUFLEN; | |
273 | ||
ba460e48 MU |
274 | this_urb = portdata->out_urbs[i]; |
275 | if (this_urb->status == -EINPROGRESS) { | |
58cfe911 MU |
276 | if (this_urb->transfer_flags & URB_ASYNC_UNLINK) |
277 | continue; | |
7bb75aee AM |
278 | if (time_before(jiffies, |
279 | portdata->tx_start_time[i] + 10 * HZ)) | |
58cfe911 MU |
280 | continue; |
281 | this_urb->transfer_flags |= URB_ASYNC_UNLINK; | |
282 | usb_unlink_urb(this_urb); | |
ba460e48 | 283 | continue; |
58cfe911 | 284 | } |
ba460e48 | 285 | if (this_urb->status != 0) |
7bb75aee AM |
286 | dbg("usb_write %p failed (err=%d)", |
287 | this_urb, this_urb->status); | |
58cfe911 | 288 | |
7bb75aee AM |
289 | dbg("%s: endpoint %d buf %d", __FUNCTION__, |
290 | usb_pipeendpoint(this_urb->pipe), i); | |
58cfe911 | 291 | |
ba460e48 | 292 | /* send the data */ |
58cfe911 | 293 | memcpy (this_urb->transfer_buffer, buf, todo); |
58cfe911 MU |
294 | this_urb->transfer_buffer_length = todo; |
295 | ||
296 | this_urb->transfer_flags &= ~URB_ASYNC_UNLINK; | |
297 | this_urb->dev = port->serial->dev; | |
298 | err = usb_submit_urb(this_urb, GFP_ATOMIC); | |
299 | if (err) { | |
7bb75aee AM |
300 | dbg("usb_submit_urb %p (write bulk) failed " |
301 | "(%d, has %d)", this_urb, | |
302 | err, this_urb->status); | |
58cfe911 MU |
303 | continue; |
304 | } | |
305 | portdata->tx_start_time[i] = jiffies; | |
306 | buf += todo; | |
307 | left -= todo; | |
308 | } | |
309 | ||
310 | count -= left; | |
58cfe911 MU |
311 | dbg("%s: wrote (did %d)", __FUNCTION__, count); |
312 | return count; | |
313 | } | |
314 | ||
7bb75aee | 315 | static void option_indat_callback(struct urb *urb, struct pt_regs *regs) |
58cfe911 | 316 | { |
ba460e48 | 317 | int i, err; |
58cfe911 MU |
318 | int endpoint; |
319 | struct usb_serial_port *port; | |
320 | struct tty_struct *tty; | |
321 | unsigned char *data = urb->transfer_buffer; | |
322 | ||
323 | dbg("%s: %p", __FUNCTION__, urb); | |
324 | ||
325 | endpoint = usb_pipeendpoint(urb->pipe); | |
326 | port = (struct usb_serial_port *) urb->context; | |
327 | ||
328 | if (urb->status) { | |
329 | dbg("%s: nonzero status: %d on endpoint %02x.", | |
330 | __FUNCTION__, urb->status, endpoint); | |
331 | } else { | |
332 | tty = port->tty; | |
333 | if (urb->actual_length) { | |
334 | for (i = 0; i < urb->actual_length ; ++i) { | |
335 | if (tty->flip.count >= TTY_FLIPBUF_SIZE) | |
336 | tty_flip_buffer_push(tty); | |
337 | tty_insert_flip_char(tty, data[i], 0); | |
338 | } | |
339 | tty_flip_buffer_push(tty); | |
340 | } else { | |
341 | dbg("%s: empty read urb received", __FUNCTION__); | |
342 | } | |
343 | ||
344 | /* Resubmit urb so we continue receiving */ | |
345 | if (port->open_count && urb->status != -ESHUTDOWN) { | |
346 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
347 | if (err) | |
7bb75aee AM |
348 | printk(KERN_ERR "%s: resubmit read urb failed. " |
349 | "(%d)", __FUNCTION__, err); | |
58cfe911 MU |
350 | } |
351 | } | |
352 | return; | |
353 | } | |
354 | ||
7bb75aee | 355 | static void option_outdat_callback(struct urb *urb, struct pt_regs *regs) |
58cfe911 MU |
356 | { |
357 | struct usb_serial_port *port; | |
358 | ||
359 | dbg("%s", __FUNCTION__); | |
360 | ||
361 | port = (struct usb_serial_port *) urb->context; | |
362 | ||
363 | if (port->open_count) | |
364 | schedule_work(&port->work); | |
365 | } | |
366 | ||
7bb75aee | 367 | static void option_instat_callback(struct urb *urb, struct pt_regs *regs) |
58cfe911 MU |
368 | { |
369 | int err; | |
370 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; | |
371 | struct option_port_private *portdata = usb_get_serial_port_data(port); | |
372 | struct usb_serial *serial = port->serial; | |
373 | ||
374 | dbg("%s", __FUNCTION__); | |
375 | dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata); | |
376 | ||
377 | if (urb->status == 0) { | |
378 | struct usb_ctrlrequest *req_pkt = | |
379 | (struct usb_ctrlrequest *)urb->transfer_buffer; | |
380 | ||
381 | if (!req_pkt) { | |
382 | dbg("%s: NULL req_pkt\n", __FUNCTION__); | |
383 | return; | |
384 | } | |
7bb75aee AM |
385 | if ((req_pkt->bRequestType == 0xA1) && |
386 | (req_pkt->bRequest == 0x20)) { | |
58cfe911 MU |
387 | int old_dcd_state; |
388 | unsigned char signals = *((unsigned char *) | |
7bb75aee AM |
389 | urb->transfer_buffer + |
390 | sizeof(struct usb_ctrlrequest)); | |
58cfe911 MU |
391 | |
392 | dbg("%s: signal x%x", __FUNCTION__, signals); | |
393 | ||
394 | old_dcd_state = portdata->dcd_state; | |
395 | portdata->cts_state = 1; | |
396 | portdata->dcd_state = ((signals & 0x01) ? 1 : 0); | |
397 | portdata->dsr_state = ((signals & 0x02) ? 1 : 0); | |
398 | portdata->ri_state = ((signals & 0x08) ? 1 : 0); | |
399 | ||
7bb75aee AM |
400 | if (port->tty && !C_CLOCAL(port->tty) && |
401 | old_dcd_state && !portdata->dcd_state) | |
58cfe911 | 402 | tty_hangup(port->tty); |
7bb75aee AM |
403 | } else { |
404 | dbg("%s: type %x req %x", __FUNCTION__, | |
405 | req_pkt->bRequestType,req_pkt->bRequest); | |
406 | } | |
58cfe911 MU |
407 | } else |
408 | dbg("%s: error %d", __FUNCTION__, urb->status); | |
409 | ||
410 | /* Resubmit urb so we continue receiving IRQ data */ | |
411 | if (urb->status != -ESHUTDOWN) { | |
412 | urb->dev = serial->dev; | |
413 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
414 | if (err) | |
7bb75aee AM |
415 | dbg("%s: resubmit intr urb failed. (%d)", |
416 | __FUNCTION__, err); | |
58cfe911 MU |
417 | } |
418 | } | |
419 | ||
7bb75aee | 420 | static int option_write_room(struct usb_serial_port *port) |
58cfe911 MU |
421 | { |
422 | struct option_port_private *portdata; | |
423 | int i; | |
424 | int data_len = 0; | |
425 | struct urb *this_urb; | |
426 | ||
427 | portdata = usb_get_serial_port_data(port); | |
428 | ||
ba460e48 | 429 | for (i=0; i < N_OUT_URB; i++) { |
58cfe911 MU |
430 | this_urb = portdata->out_urbs[i]; |
431 | if (this_urb && this_urb->status != -EINPROGRESS) | |
432 | data_len += OUT_BUFLEN; | |
ba460e48 | 433 | } |
58cfe911 MU |
434 | |
435 | dbg("%s: %d", __FUNCTION__, data_len); | |
436 | return data_len; | |
437 | } | |
438 | ||
7bb75aee | 439 | static int option_chars_in_buffer(struct usb_serial_port *port) |
58cfe911 MU |
440 | { |
441 | struct option_port_private *portdata; | |
442 | int i; | |
443 | int data_len = 0; | |
444 | struct urb *this_urb; | |
445 | ||
446 | portdata = usb_get_serial_port_data(port); | |
447 | ||
ba460e48 | 448 | for (i=0; i < N_OUT_URB; i++) { |
58cfe911 MU |
449 | this_urb = portdata->out_urbs[i]; |
450 | if (this_urb && this_urb->status == -EINPROGRESS) | |
451 | data_len += this_urb->transfer_buffer_length; | |
ba460e48 | 452 | } |
58cfe911 MU |
453 | dbg("%s: %d", __FUNCTION__, data_len); |
454 | return data_len; | |
455 | } | |
456 | ||
7bb75aee | 457 | static int option_open(struct usb_serial_port *port, struct file *filp) |
58cfe911 | 458 | { |
ba460e48 MU |
459 | struct option_port_private *portdata; |
460 | struct usb_serial *serial = port->serial; | |
461 | int i, err; | |
462 | struct urb *urb; | |
58cfe911 MU |
463 | |
464 | portdata = usb_get_serial_port_data(port); | |
465 | ||
466 | dbg("%s", __FUNCTION__); | |
467 | ||
468 | /* Set some sane defaults */ | |
469 | portdata->rts_state = 1; | |
470 | portdata->dtr_state = 1; | |
471 | ||
472 | /* Reset low level data toggle and start reading from endpoints */ | |
473 | for (i = 0; i < N_IN_URB; i++) { | |
474 | urb = portdata->in_urbs[i]; | |
475 | if (! urb) | |
476 | continue; | |
477 | if (urb->dev != serial->dev) { | |
7bb75aee AM |
478 | dbg("%s: dev %p != %p", __FUNCTION__, |
479 | urb->dev, serial->dev); | |
58cfe911 MU |
480 | continue; |
481 | } | |
482 | ||
7bb75aee AM |
483 | /* |
484 | * make sure endpoint data toggle is synchronized with the | |
485 | * device | |
486 | */ | |
58cfe911 MU |
487 | usb_clear_halt(urb->dev, urb->pipe); |
488 | ||
489 | err = usb_submit_urb(urb, GFP_KERNEL); | |
490 | if (err) { | |
7bb75aee AM |
491 | dbg("%s: submit urb %d failed (%d) %d", |
492 | __FUNCTION__, i, err, | |
58cfe911 MU |
493 | urb->transfer_buffer_length); |
494 | } | |
495 | } | |
496 | ||
497 | /* Reset low level data toggle on out endpoints */ | |
498 | for (i = 0; i < N_OUT_URB; i++) { | |
499 | urb = portdata->out_urbs[i]; | |
500 | if (! urb) | |
501 | continue; | |
502 | urb->dev = serial->dev; | |
7bb75aee AM |
503 | /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), |
504 | usb_pipeout(urb->pipe), 0); */ | |
58cfe911 MU |
505 | } |
506 | ||
507 | port->tty->low_latency = 1; | |
508 | ||
509 | option_send_setup(port); | |
510 | ||
511 | return (0); | |
512 | } | |
513 | ||
7bb75aee | 514 | static inline void stop_urb(struct urb *urb) |
58cfe911 MU |
515 | { |
516 | if (urb && urb->status == -EINPROGRESS) { | |
517 | urb->transfer_flags &= ~URB_ASYNC_UNLINK; | |
518 | usb_kill_urb(urb); | |
519 | } | |
520 | } | |
521 | ||
7bb75aee | 522 | static void option_close(struct usb_serial_port *port, struct file *filp) |
58cfe911 | 523 | { |
ba460e48 MU |
524 | int i; |
525 | struct usb_serial *serial = port->serial; | |
526 | struct option_port_private *portdata; | |
58cfe911 MU |
527 | |
528 | dbg("%s", __FUNCTION__); | |
529 | portdata = usb_get_serial_port_data(port); | |
530 | ||
531 | portdata->rts_state = 0; | |
532 | portdata->dtr_state = 0; | |
533 | ||
534 | if (serial->dev) { | |
535 | option_send_setup(port); | |
536 | ||
537 | /* Stop reading/writing urbs */ | |
538 | for (i = 0; i < N_IN_URB; i++) | |
539 | stop_urb(portdata->in_urbs[i]); | |
540 | for (i = 0; i < N_OUT_URB; i++) | |
541 | stop_urb(portdata->out_urbs[i]); | |
542 | } | |
543 | port->tty = NULL; | |
544 | } | |
545 | ||
58cfe911 | 546 | /* Helper functions used by option_setup_urbs */ |
7bb75aee AM |
547 | static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint, |
548 | int dir, void *ctx, char *buf, int len, | |
549 | void (*callback)(struct urb *, struct pt_regs *regs)) | |
58cfe911 MU |
550 | { |
551 | struct urb *urb; | |
552 | ||
553 | if (endpoint == -1) | |
554 | return NULL; /* endpoint not needed */ | |
555 | ||
556 | urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ | |
557 | if (urb == NULL) { | |
558 | dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint); | |
559 | return NULL; | |
560 | } | |
561 | ||
562 | /* Fill URB using supplied data. */ | |
563 | usb_fill_bulk_urb(urb, serial->dev, | |
564 | usb_sndbulkpipe(serial->dev, endpoint) | dir, | |
565 | buf, len, callback, ctx); | |
566 | ||
567 | return urb; | |
568 | } | |
569 | ||
570 | /* Setup urbs */ | |
7bb75aee | 571 | static void option_setup_urbs(struct usb_serial *serial) |
58cfe911 | 572 | { |
ba460e48 MU |
573 | int j; |
574 | struct usb_serial_port *port; | |
575 | struct option_port_private *portdata; | |
58cfe911 MU |
576 | |
577 | dbg("%s", __FUNCTION__); | |
578 | ||
579 | port = serial->port[0]; | |
580 | portdata = usb_get_serial_port_data(port); | |
581 | ||
582 | /* Do indat endpoints first */ | |
583 | for (j = 0; j <= N_IN_URB; ++j) { | |
584 | portdata->in_urbs[j] = option_setup_urb (serial, | |
585 | port->bulk_in_endpointAddress, USB_DIR_IN, port, | |
586 | portdata->in_buffer[j], IN_BUFLEN, option_indat_callback); | |
587 | } | |
588 | ||
589 | /* outdat endpoints */ | |
590 | for (j = 0; j <= N_OUT_URB; ++j) { | |
591 | portdata->out_urbs[j] = option_setup_urb (serial, | |
592 | port->bulk_out_endpointAddress, USB_DIR_OUT, port, | |
593 | portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback); | |
594 | } | |
595 | } | |
596 | ||
7bb75aee | 597 | static int option_send_setup(struct usb_serial_port *port) |
58cfe911 MU |
598 | { |
599 | struct usb_serial *serial = port->serial; | |
600 | struct option_port_private *portdata; | |
601 | ||
602 | dbg("%s", __FUNCTION__); | |
603 | ||
604 | portdata = usb_get_serial_port_data(port); | |
605 | ||
606 | if (port->tty) { | |
607 | int val = 0; | |
608 | if (portdata->dtr_state) | |
609 | val |= 0x01; | |
610 | if (portdata->rts_state) | |
611 | val |= 0x02; | |
612 | ||
7bb75aee AM |
613 | return usb_control_msg(serial->dev, |
614 | usb_rcvctrlpipe(serial->dev, 0), | |
615 | 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT); | |
58cfe911 MU |
616 | } |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
7bb75aee | 621 | static int option_startup(struct usb_serial *serial) |
58cfe911 | 622 | { |
ba460e48 MU |
623 | int i, err; |
624 | struct usb_serial_port *port; | |
625 | struct option_port_private *portdata; | |
58cfe911 MU |
626 | |
627 | dbg("%s", __FUNCTION__); | |
628 | ||
629 | /* Now setup per port private data */ | |
630 | for (i = 0; i < serial->num_ports; i++) { | |
631 | port = serial->port[i]; | |
7bb75aee | 632 | portdata = kmalloc(sizeof(*portdata), GFP_KERNEL); |
58cfe911 | 633 | if (!portdata) { |
7bb75aee AM |
634 | dbg("%s: kmalloc for option_port_private (%d) failed!.", |
635 | __FUNCTION__, i); | |
58cfe911 MU |
636 | return (1); |
637 | } | |
638 | memset(portdata, 0, sizeof(struct option_port_private)); | |
639 | ||
640 | usb_set_serial_port_data(port, portdata); | |
641 | ||
642 | if (! port->interrupt_in_urb) | |
643 | continue; | |
644 | err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); | |
645 | if (err) | |
7bb75aee AM |
646 | dbg("%s: submit irq_in urb failed %d", |
647 | __FUNCTION__, err); | |
58cfe911 MU |
648 | } |
649 | ||
650 | option_setup_urbs(serial); | |
651 | ||
652 | return (0); | |
653 | } | |
654 | ||
7bb75aee | 655 | static void option_shutdown(struct usb_serial *serial) |
58cfe911 | 656 | { |
ba460e48 MU |
657 | int i, j; |
658 | struct usb_serial_port *port; | |
659 | struct option_port_private *portdata; | |
58cfe911 MU |
660 | |
661 | dbg("%s", __FUNCTION__); | |
662 | ||
663 | /* Stop reading/writing urbs */ | |
664 | for (i = 0; i < serial->num_ports; ++i) { | |
665 | port = serial->port[i]; | |
666 | portdata = usb_get_serial_port_data(port); | |
667 | for (j = 0; j < N_IN_URB; j++) | |
668 | stop_urb(portdata->in_urbs[j]); | |
669 | for (j = 0; j < N_OUT_URB; j++) | |
670 | stop_urb(portdata->out_urbs[j]); | |
671 | } | |
672 | ||
673 | /* Now free them */ | |
674 | for (i = 0; i < serial->num_ports; ++i) { | |
675 | port = serial->port[i]; | |
676 | portdata = usb_get_serial_port_data(port); | |
677 | ||
678 | for (j = 0; j < N_IN_URB; j++) { | |
679 | if (portdata->in_urbs[j]) { | |
680 | usb_free_urb(portdata->in_urbs[j]); | |
681 | portdata->in_urbs[j] = NULL; | |
682 | } | |
683 | } | |
684 | for (j = 0; j < N_OUT_URB; j++) { | |
685 | if (portdata->out_urbs[j]) { | |
686 | usb_free_urb(portdata->out_urbs[j]); | |
687 | portdata->out_urbs[j] = NULL; | |
688 | } | |
689 | } | |
690 | } | |
691 | ||
692 | /* Now free per port private data */ | |
693 | for (i = 0; i < serial->num_ports; i++) { | |
694 | port = serial->port[i]; | |
695 | kfree(usb_get_serial_port_data(port)); | |
696 | } | |
697 | } | |
698 | ||
699 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
700 | MODULE_DESCRIPTION(DRIVER_DESC); | |
701 | MODULE_VERSION(DRIVER_VERSION); | |
702 | MODULE_LICENSE("GPL"); | |
703 | ||
ba460e48 | 704 | #ifdef CONFIG_USB_DEBUG |
58cfe911 MU |
705 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
706 | MODULE_PARM_DESC(debug, "Debug messages"); | |
ba460e48 | 707 | #endif |
58cfe911 | 708 |