blob: 4f985f43e79e859cbe8de3a989744c3d8b95955a [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) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700276 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
277 continue;
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700278 if (time_before(jiffies,
279 portdata->tx_start_time[i] + 10 * HZ))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700280 continue;
281 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
282 usb_unlink_urb(this_urb);
Matthias Urlichsba460e42005-07-14 00:33:47 -0700283 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700284 }
Matthias Urlichsba460e42005-07-14 00:33:47 -0700285 if (this_urb->status != 0)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700286 dbg("usb_write %p failed (err=%d)",
287 this_urb, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700288
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700289 dbg("%s: endpoint %d buf %d", __FUNCTION__,
290 usb_pipeendpoint(this_urb->pipe), i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700291
Matthias Urlichsba460e42005-07-14 00:33:47 -0700292 /* send the data */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700293 memcpy (this_urb->transfer_buffer, buf, todo);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700294 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) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700300 dbg("usb_submit_urb %p (write bulk) failed "
301 "(%d, has %d)", this_urb,
302 err, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700303 continue;
304 }
305 portdata->tx_start_time[i] = jiffies;
306 buf += todo;
307 left -= todo;
308 }
309
310 count -= left;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700311 dbg("%s: wrote (did %d)", __FUNCTION__, count);
312 return count;
313}
314
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700315static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700316{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700317 int i, err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700318 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)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700348 printk(KERN_ERR "%s: resubmit read urb failed. "
349 "(%d)", __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700350 }
351 }
352 return;
353}
354
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700355static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700356{
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
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700367static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700368{
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 }
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700385 if ((req_pkt->bRequestType == 0xA1) &&
386 (req_pkt->bRequest == 0x20)) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700387 int old_dcd_state;
388 unsigned char signals = *((unsigned char *)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700389 urb->transfer_buffer +
390 sizeof(struct usb_ctrlrequest));
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700391
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
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700400 if (port->tty && !C_CLOCAL(port->tty) &&
401 old_dcd_state && !portdata->dcd_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700402 tty_hangup(port->tty);
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700403 } else {
404 dbg("%s: type %x req %x", __FUNCTION__,
405 req_pkt->bRequestType,req_pkt->bRequest);
406 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700407 } 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)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700415 dbg("%s: resubmit intr urb failed. (%d)",
416 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700417 }
418}
419
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700420static int option_write_room(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700421{
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
Matthias Urlichsba460e42005-07-14 00:33:47 -0700429 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700430 this_urb = portdata->out_urbs[i];
431 if (this_urb && this_urb->status != -EINPROGRESS)
432 data_len += OUT_BUFLEN;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700433 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700434
435 dbg("%s: %d", __FUNCTION__, data_len);
436 return data_len;
437}
438
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700439static int option_chars_in_buffer(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700440{
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
Matthias Urlichsba460e42005-07-14 00:33:47 -0700448 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700449 this_urb = portdata->out_urbs[i];
450 if (this_urb && this_urb->status == -EINPROGRESS)
451 data_len += this_urb->transfer_buffer_length;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700452 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700453 dbg("%s: %d", __FUNCTION__, data_len);
454 return data_len;
455}
456
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700457static int option_open(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700458{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700459 struct option_port_private *portdata;
460 struct usb_serial *serial = port->serial;
461 int i, err;
462 struct urb *urb;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700463
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) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700478 dbg("%s: dev %p != %p", __FUNCTION__,
479 urb->dev, serial->dev);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700480 continue;
481 }
482
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700483 /*
484 * make sure endpoint data toggle is synchronized with the
485 * device
486 */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700487 usb_clear_halt(urb->dev, urb->pipe);
488
489 err = usb_submit_urb(urb, GFP_KERNEL);
490 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700491 dbg("%s: submit urb %d failed (%d) %d",
492 __FUNCTION__, i, err,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700493 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;
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700503 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
504 usb_pipeout(urb->pipe), 0); */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700505 }
506
507 port->tty->low_latency = 1;
508
509 option_send_setup(port);
510
511 return (0);
512}
513
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700514static inline void stop_urb(struct urb *urb)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700515{
516 if (urb && urb->status == -EINPROGRESS) {
517 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
518 usb_kill_urb(urb);
519 }
520}
521
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700522static void option_close(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700523{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700524 int i;
525 struct usb_serial *serial = port->serial;
526 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700527
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
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700546/* Helper functions used by option_setup_urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700547static 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))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700550{
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 */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700571static void option_setup_urbs(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700572{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700573 int j;
574 struct usb_serial_port *port;
575 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700576
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
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700597static int option_send_setup(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700598{
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
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700613 return usb_control_msg(serial->dev,
614 usb_rcvctrlpipe(serial->dev, 0),
615 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700616 }
617
618 return 0;
619}
620
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700621static int option_startup(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700622{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700623 int i, err;
624 struct usb_serial_port *port;
625 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700626
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];
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700632 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700633 if (!portdata) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700634 dbg("%s: kmalloc for option_port_private (%d) failed!.",
635 __FUNCTION__, i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700636 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)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700646 dbg("%s: submit irq_in urb failed %d",
647 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700648 }
649
650 option_setup_urbs(serial);
651
652 return (0);
653}
654
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700655static void option_shutdown(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700656{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700657 int i, j;
658 struct usb_serial_port *port;
659 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700660
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
699MODULE_AUTHOR(DRIVER_AUTHOR);
700MODULE_DESCRIPTION(DRIVER_DESC);
701MODULE_VERSION(DRIVER_VERSION);
702MODULE_LICENSE("GPL");
703
Matthias Urlichsba460e42005-07-14 00:33:47 -0700704#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700705module_param(debug, bool, S_IRUGO | S_IWUSR);
706MODULE_PARM_DESC(debug, "Debug messages");
Matthias Urlichsba460e42005-07-14 00:33:47 -0700707#endif
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700708