blob: e9256408757ffdf3a7f5cc3a832bf8b503c808c9 [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 */
48static 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);
55
56static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
57
Matthias Urlichs58cfe912005-05-23 17:00:48 -070058static int option_write (struct usb_serial_port *port,
59 const unsigned char *buf, int count);
60
61static 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);
71
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 -070079
80static struct usb_device_id option_ids[] = {
81 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
Matthias Urlichsba460e42005-07-14 00:33:47 -070082 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
83 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
Matthias Urlichs58cfe912005-05-23 17:00:48 -070084 { } /* Terminating entry */
85};
86
87MODULE_DEVICE_TABLE(usb, option_ids);
88
89static struct usb_driver option_driver = {
90 .owner = THIS_MODULE,
91 .name = "option",
92 .probe = usb_serial_probe,
93 .disconnect = usb_serial_disconnect,
94 .id_table = option_ids,
95};
96
97/* The card has three separate interfaces, wich the serial driver
98 * recognizes separately, thus num_port=1.
99 */
100static struct usb_serial_device_type option_3port_device = {
Matthias Urlichsba460e42005-07-14 00:33:47 -0700101 .owner = THIS_MODULE,
102 .name = "Option 3G data card",
103 .short_name = "option",
104 .id_table = option_ids,
105 .num_interrupt_in = NUM_DONT_CARE,
106 .num_bulk_in = NUM_DONT_CARE,
107 .num_bulk_out = NUM_DONT_CARE,
108 .num_ports = 1, /* 3, but the card reports its ports separately */
109 .open = option_open,
110 .close = option_close,
111 .write = option_write,
112 .write_room = option_write_room,
113 .chars_in_buffer = option_chars_in_buffer,
114 .throttle = option_rx_throttle,
115 .unthrottle = option_rx_unthrottle,
116 .ioctl = option_ioctl,
117 .set_termios = option_set_termios,
118 .break_ctl = option_break_ctl,
119 .tiocmget = option_tiocmget,
120 .tiocmset = option_tiocmset,
121 .attach = option_startup,
122 .shutdown = option_shutdown,
123 .read_int_callback = option_instat_callback,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700124};
125
Matthias Urlichsba460e42005-07-14 00:33:47 -0700126#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700127static int debug;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700128#else
129#define debug 0
130#endif
131
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700132
133/* per port private data */
134
Matthias Urlichsba460e42005-07-14 00:33:47 -0700135#define N_IN_URB 4
136#define N_OUT_URB 1
137#define IN_BUFLEN 1024
138#define OUT_BUFLEN 128
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700139
140struct option_port_private {
141 /* Input endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700142 struct urb *in_urbs[N_IN_URB];
143 char in_buffer[N_IN_URB][IN_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700144 /* Output endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700145 struct urb *out_urbs[N_OUT_URB];
146 char out_buffer[N_OUT_URB][OUT_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700147
148 /* Settings for the port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700149 int rts_state; /* Handshaking pins (outputs) */
150 int dtr_state;
151 int cts_state; /* Handshaking pins (inputs) */
152 int dsr_state;
153 int dcd_state;
154 int ri_state;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700155
Matthias Urlichsba460e42005-07-14 00:33:47 -0700156 unsigned long tx_start_time[N_OUT_URB];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700157};
158
159
160/* Functions used by new usb-serial code. */
161static int __init
162option_init (void)
163{
164 int retval;
165 retval = usb_serial_register(&option_3port_device);
166 if (retval)
167 goto failed_3port_device_register;
168 retval = usb_register(&option_driver);
169 if (retval)
170 goto failed_driver_register;
171
172 info(DRIVER_DESC ": " DRIVER_VERSION);
173
174 return 0;
175
176failed_driver_register:
177 usb_serial_deregister (&option_3port_device);
178failed_3port_device_register:
179 return retval;
180}
181
182static void __exit
183option_exit (void)
184{
185 usb_deregister (&option_driver);
186 usb_serial_deregister (&option_3port_device);
187}
188
189module_init(option_init);
190module_exit(option_exit);
191
192static void
193option_rx_throttle (struct usb_serial_port *port)
194{
195 dbg("%s", __FUNCTION__);
196}
197
198
199static void
200option_rx_unthrottle (struct usb_serial_port *port)
201{
202 dbg("%s", __FUNCTION__);
203}
204
205
206static void
207option_break_ctl (struct usb_serial_port *port, int break_state)
208{
209 /* Unfortunately, I don't know how to send a break */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700210 dbg("%s", __FUNCTION__);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700211}
212
213
214static void
215option_set_termios (struct usb_serial_port *port,
Matthias Urlichsba460e42005-07-14 00:33:47 -0700216 struct termios *old_termios)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700217{
218 dbg("%s", __FUNCTION__);
219
220 option_send_setup(port);
221}
222
223static int
Matthias Urlichsba460e42005-07-14 00:33:47 -0700224option_tiocmget (struct usb_serial_port *port, struct file *file)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700225{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700226 unsigned int value;
227 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700228
229 portdata = usb_get_serial_port_data(port);
230
231 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
232 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
233 ((portdata->cts_state) ? TIOCM_CTS : 0) |
234 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
235 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
236 ((portdata->ri_state) ? TIOCM_RNG : 0);
237
238 return value;
239}
240
241static int
242option_tiocmset (struct usb_serial_port *port, struct file *file,
243 unsigned int set, unsigned int clear)
244{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700245 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700246
247 portdata = usb_get_serial_port_data(port);
248
249 if (set & TIOCM_RTS)
250 portdata->rts_state = 1;
251 if (set & TIOCM_DTR)
252 portdata->dtr_state = 1;
253
254 if (clear & TIOCM_RTS)
255 portdata->rts_state = 0;
256 if (clear & TIOCM_DTR)
257 portdata->dtr_state = 0;
258 return option_send_setup(port);
259}
260
261static int
262option_ioctl (struct usb_serial_port *port, struct file *file,
263 unsigned int cmd, unsigned long arg)
264{
265 return -ENOIOCTLCMD;
266}
267
268/* Write */
269static int
Matthias Urlichsba460e42005-07-14 00:33:47 -0700270option_write (struct usb_serial_port *port,
271 const unsigned char *buf, int count)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700272{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700273 struct option_port_private *portdata;
274 int i;
275 int left, todo;
276 struct urb *this_urb = NULL; /* spurious */
277 int err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700278
279 portdata = usb_get_serial_port_data(port);
280
281 dbg("%s: write (%d chars)", __FUNCTION__, count);
282
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700283 i = 0;
284 left = count;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700285 for (i=0; left > 0 && i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700286 todo = left;
287 if (todo > OUT_BUFLEN)
288 todo = OUT_BUFLEN;
289
Matthias Urlichsba460e42005-07-14 00:33:47 -0700290 this_urb = portdata->out_urbs[i];
291 if (this_urb->status == -EINPROGRESS) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700292 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
293 continue;
294 if (time_before(jiffies, portdata->tx_start_time[i] + 10 * HZ))
295 continue;
296 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
297 usb_unlink_urb(this_urb);
Matthias Urlichsba460e42005-07-14 00:33:47 -0700298 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700299 }
Matthias Urlichsba460e42005-07-14 00:33:47 -0700300 if (this_urb->status != 0)
301 dbg("usb_write %p failed (err=%d)", this_urb, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700302
303 dbg("%s: endpoint %d buf %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), i);
304
Matthias Urlichsba460e42005-07-14 00:33:47 -0700305 /* send the data */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700306 memcpy (this_urb->transfer_buffer, buf, todo);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700307 this_urb->transfer_buffer_length = todo;
308
309 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
310 this_urb->dev = port->serial->dev;
311 err = usb_submit_urb(this_urb, GFP_ATOMIC);
312 if (err) {
Matthias Urlichsba460e42005-07-14 00:33:47 -0700313 dbg("usb_submit_urb %p (write bulk) failed (%d, has %d)", this_urb, err, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700314 continue;
315 }
316 portdata->tx_start_time[i] = jiffies;
317 buf += todo;
318 left -= todo;
319 }
320
321 count -= left;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700322 dbg("%s: wrote (did %d)", __FUNCTION__, count);
323 return count;
324}
325
326static void
327option_indat_callback (struct urb *urb, struct pt_regs *regs)
328{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700329 int i, err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700330 int endpoint;
331 struct usb_serial_port *port;
332 struct tty_struct *tty;
333 unsigned char *data = urb->transfer_buffer;
334
335 dbg("%s: %p", __FUNCTION__, urb);
336
337 endpoint = usb_pipeendpoint(urb->pipe);
338 port = (struct usb_serial_port *) urb->context;
339
340 if (urb->status) {
341 dbg("%s: nonzero status: %d on endpoint %02x.",
342 __FUNCTION__, urb->status, endpoint);
343 } else {
344 tty = port->tty;
345 if (urb->actual_length) {
346 for (i = 0; i < urb->actual_length ; ++i) {
347 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
348 tty_flip_buffer_push(tty);
349 tty_insert_flip_char(tty, data[i], 0);
350 }
351 tty_flip_buffer_push(tty);
352 } else {
353 dbg("%s: empty read urb received", __FUNCTION__);
354 }
355
356 /* Resubmit urb so we continue receiving */
357 if (port->open_count && urb->status != -ESHUTDOWN) {
358 err = usb_submit_urb(urb, GFP_ATOMIC);
359 if (err)
360 printk(KERN_ERR "%s: resubmit read urb failed. (%d)", __FUNCTION__, err);
361 }
362 }
363 return;
364}
365
366static void
367option_outdat_callback (struct urb *urb, struct pt_regs *regs)
368{
369 struct usb_serial_port *port;
370
371 dbg("%s", __FUNCTION__);
372
373 port = (struct usb_serial_port *) urb->context;
374
375 if (port->open_count)
376 schedule_work(&port->work);
377}
378
379static void
380option_instat_callback (struct urb *urb, struct pt_regs *regs)
381{
382 int err;
383 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
384 struct option_port_private *portdata = usb_get_serial_port_data(port);
385 struct usb_serial *serial = port->serial;
386
387 dbg("%s", __FUNCTION__);
388 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
389
390 if (urb->status == 0) {
391 struct usb_ctrlrequest *req_pkt =
392 (struct usb_ctrlrequest *)urb->transfer_buffer;
393
394 if (!req_pkt) {
395 dbg("%s: NULL req_pkt\n", __FUNCTION__);
396 return;
397 }
398 if ((req_pkt->bRequestType == 0xA1) && (req_pkt->bRequest == 0x20)) {
399 int old_dcd_state;
400 unsigned char signals = *((unsigned char *)
401 urb->transfer_buffer + sizeof(struct usb_ctrlrequest));
402
403 dbg("%s: signal x%x", __FUNCTION__, signals);
404
405 old_dcd_state = portdata->dcd_state;
406 portdata->cts_state = 1;
407 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
408 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
409 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
410
411 if (port->tty && !C_CLOCAL(port->tty)
412 && old_dcd_state && !portdata->dcd_state) {
413 tty_hangup(port->tty);
414 }
415 } else
416 dbg("%s: type %x req %x", __FUNCTION__, req_pkt->bRequestType,req_pkt->bRequest);
417 } else
418 dbg("%s: error %d", __FUNCTION__, urb->status);
419
420 /* Resubmit urb so we continue receiving IRQ data */
421 if (urb->status != -ESHUTDOWN) {
422 urb->dev = serial->dev;
423 err = usb_submit_urb(urb, GFP_ATOMIC);
424 if (err)
425 dbg("%s: resubmit intr urb failed. (%d)", __FUNCTION__, err);
426 }
427}
428
429
430static int
431option_write_room (struct usb_serial_port *port)
432{
433 struct option_port_private *portdata;
434 int i;
435 int data_len = 0;
436 struct urb *this_urb;
437
438 portdata = usb_get_serial_port_data(port);
439
Matthias Urlichsba460e42005-07-14 00:33:47 -0700440 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700441 this_urb = portdata->out_urbs[i];
442 if (this_urb && this_urb->status != -EINPROGRESS)
443 data_len += OUT_BUFLEN;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700444 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700445
446 dbg("%s: %d", __FUNCTION__, data_len);
447 return data_len;
448}
449
450
451static int
452option_chars_in_buffer (struct usb_serial_port *port)
453{
454 struct option_port_private *portdata;
455 int i;
456 int data_len = 0;
457 struct urb *this_urb;
458
459 portdata = usb_get_serial_port_data(port);
460
Matthias Urlichsba460e42005-07-14 00:33:47 -0700461 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700462 this_urb = portdata->out_urbs[i];
463 if (this_urb && this_urb->status == -EINPROGRESS)
464 data_len += this_urb->transfer_buffer_length;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700465 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700466 dbg("%s: %d", __FUNCTION__, data_len);
467 return data_len;
468}
469
470
471static int
472option_open (struct usb_serial_port *port, struct file *filp)
473{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700474 struct option_port_private *portdata;
475 struct usb_serial *serial = port->serial;
476 int i, err;
477 struct urb *urb;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700478
479 portdata = usb_get_serial_port_data(port);
480
481 dbg("%s", __FUNCTION__);
482
483 /* Set some sane defaults */
484 portdata->rts_state = 1;
485 portdata->dtr_state = 1;
486
487 /* Reset low level data toggle and start reading from endpoints */
488 for (i = 0; i < N_IN_URB; i++) {
489 urb = portdata->in_urbs[i];
490 if (! urb)
491 continue;
492 if (urb->dev != serial->dev) {
493 dbg("%s: dev %p != %p", __FUNCTION__, urb->dev, serial->dev);
494 continue;
495 }
496
497 /* make sure endpoint data toggle is synchronized with the device */
498
499 usb_clear_halt(urb->dev, urb->pipe);
500
501 err = usb_submit_urb(urb, GFP_KERNEL);
502 if (err) {
503 dbg("%s: submit urb %d failed (%d) %d", __FUNCTION__, i, err,
504 urb->transfer_buffer_length);
505 }
506 }
507
508 /* Reset low level data toggle on out endpoints */
509 for (i = 0; i < N_OUT_URB; i++) {
510 urb = portdata->out_urbs[i];
511 if (! urb)
512 continue;
513 urb->dev = serial->dev;
514 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
515 }
516
517 port->tty->low_latency = 1;
518
519 option_send_setup(port);
520
521 return (0);
522}
523
524static inline void
Matthias Urlichsba460e42005-07-14 00:33:47 -0700525stop_urb (struct urb *urb)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700526{
527 if (urb && urb->status == -EINPROGRESS) {
528 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
529 usb_kill_urb(urb);
530 }
531}
532
533static void
Matthias Urlichsba460e42005-07-14 00:33:47 -0700534option_close (struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700535{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700536 int i;
537 struct usb_serial *serial = port->serial;
538 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700539
540 dbg("%s", __FUNCTION__);
541 portdata = usb_get_serial_port_data(port);
542
543 portdata->rts_state = 0;
544 portdata->dtr_state = 0;
545
546 if (serial->dev) {
547 option_send_setup(port);
548
549 /* Stop reading/writing urbs */
550 for (i = 0; i < N_IN_URB; i++)
551 stop_urb(portdata->in_urbs[i]);
552 for (i = 0; i < N_OUT_URB; i++)
553 stop_urb(portdata->out_urbs[i]);
554 }
555 port->tty = NULL;
556}
557
558
559/* Helper functions used by option_setup_urbs */
560static struct urb *
561option_setup_urb (struct usb_serial *serial, int endpoint,
562 int dir, void *ctx, char *buf, int len,
563 void (*callback)(struct urb *, struct pt_regs *regs))
564{
565 struct urb *urb;
566
567 if (endpoint == -1)
568 return NULL; /* endpoint not needed */
569
570 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
571 if (urb == NULL) {
572 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
573 return NULL;
574 }
575
576 /* Fill URB using supplied data. */
577 usb_fill_bulk_urb(urb, serial->dev,
578 usb_sndbulkpipe(serial->dev, endpoint) | dir,
579 buf, len, callback, ctx);
580
581 return urb;
582}
583
584/* Setup urbs */
585static void
Matthias Urlichsba460e42005-07-14 00:33:47 -0700586option_setup_urbs (struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700587{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700588 int j;
589 struct usb_serial_port *port;
590 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700591
592 dbg("%s", __FUNCTION__);
593
594 port = serial->port[0];
595 portdata = usb_get_serial_port_data(port);
596
597 /* Do indat endpoints first */
598 for (j = 0; j <= N_IN_URB; ++j) {
599 portdata->in_urbs[j] = option_setup_urb (serial,
600 port->bulk_in_endpointAddress, USB_DIR_IN, port,
601 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
602 }
603
604 /* outdat endpoints */
605 for (j = 0; j <= N_OUT_URB; ++j) {
606 portdata->out_urbs[j] = option_setup_urb (serial,
607 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
608 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
609 }
610}
611
612
613static int
Matthias Urlichsba460e42005-07-14 00:33:47 -0700614option_send_setup (struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700615{
616 struct usb_serial *serial = port->serial;
617 struct option_port_private *portdata;
618
619 dbg("%s", __FUNCTION__);
620
621 portdata = usb_get_serial_port_data(port);
622
623 if (port->tty) {
624 int val = 0;
625 if (portdata->dtr_state)
626 val |= 0x01;
627 if (portdata->rts_state)
628 val |= 0x02;
629
630 return usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
631 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
632 }
633
634 return 0;
635}
636
637
638static int
639option_startup (struct usb_serial *serial)
640{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700641 int i, err;
642 struct usb_serial_port *port;
643 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700644
645 dbg("%s", __FUNCTION__);
646
647 /* Now setup per port private data */
648 for (i = 0; i < serial->num_ports; i++) {
649 port = serial->port[i];
650 portdata = kmalloc(sizeof(struct option_port_private), GFP_KERNEL);
651 if (!portdata) {
652 dbg("%s: kmalloc for option_port_private (%d) failed!.", __FUNCTION__, i);
653 return (1);
654 }
655 memset(portdata, 0, sizeof(struct option_port_private));
656
657 usb_set_serial_port_data(port, portdata);
658
659 if (! port->interrupt_in_urb)
660 continue;
661 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
662 if (err)
663 dbg("%s: submit irq_in urb failed %d", __FUNCTION__, err);
664 }
665
666 option_setup_urbs(serial);
667
668 return (0);
669}
670
671static void
672option_shutdown (struct usb_serial *serial)
673{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700674 int i, j;
675 struct usb_serial_port *port;
676 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700677
678 dbg("%s", __FUNCTION__);
679
680 /* Stop reading/writing urbs */
681 for (i = 0; i < serial->num_ports; ++i) {
682 port = serial->port[i];
683 portdata = usb_get_serial_port_data(port);
684 for (j = 0; j < N_IN_URB; j++)
685 stop_urb(portdata->in_urbs[j]);
686 for (j = 0; j < N_OUT_URB; j++)
687 stop_urb(portdata->out_urbs[j]);
688 }
689
690 /* Now free them */
691 for (i = 0; i < serial->num_ports; ++i) {
692 port = serial->port[i];
693 portdata = usb_get_serial_port_data(port);
694
695 for (j = 0; j < N_IN_URB; j++) {
696 if (portdata->in_urbs[j]) {
697 usb_free_urb(portdata->in_urbs[j]);
698 portdata->in_urbs[j] = NULL;
699 }
700 }
701 for (j = 0; j < N_OUT_URB; j++) {
702 if (portdata->out_urbs[j]) {
703 usb_free_urb(portdata->out_urbs[j]);
704 portdata->out_urbs[j] = NULL;
705 }
706 }
707 }
708
709 /* Now free per port private data */
710 for (i = 0; i < serial->num_ports; i++) {
711 port = serial->port[i];
712 kfree(usb_get_serial_port_data(port));
713 }
714}
715
716MODULE_AUTHOR(DRIVER_AUTHOR);
717MODULE_DESCRIPTION(DRIVER_DESC);
718MODULE_VERSION(DRIVER_VERSION);
719MODULE_LICENSE("GPL");
720
Matthias Urlichsba460e42005-07-14 00:33:47 -0700721#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700722module_param(debug, bool, S_IRUGO | S_IWUSR);
723MODULE_PARM_DESC(debug, "Debug messages");
Matthias Urlichsba460e42005-07-14 00:33:47 -0700724#endif
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700725