blob: 4989e5740d181141c12fa0d3a456d44f44fbe14c [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
Matthias Urlichsb6137382005-09-22 00:48:40 -070028 2005-09-10 v0.4.3 added HUAWEI E600 card and Audiovox AirCard
Matthias Urlichsb27c73d2005-09-22 00:49:33 -070029 2005-09-20 v0.4.4 increased recv buffer size: the card sometimes
30 wants to send >2000 bytes.
Matthias Urlichsba460e42005-07-14 00:33:47 -070031
32 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
33
Matthias Urlichs58cfe912005-05-23 17:00:48 -070034*/
Matthias Urlichsba460e42005-07-14 00:33:47 -070035
36#define DRIVER_VERSION "v0.4"
Matthias Urlichs58cfe912005-05-23 17:00:48 -070037#define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
38#define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver"
39
40#include <linux/config.h>
41#include <linux/kernel.h>
42#include <linux/jiffies.h>
43#include <linux/errno.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <linux/module.h>
47#include <linux/usb.h>
48#include "usb-serial.h"
49
50/* Function prototypes */
Andrew Morton7bb75ae2005-07-27 01:08:30 -070051static int option_open(struct usb_serial_port *port, struct file *filp);
52static void option_close(struct usb_serial_port *port, struct file *filp);
53static int option_startup(struct usb_serial *serial);
54static void option_shutdown(struct usb_serial *serial);
55static void option_rx_throttle(struct usb_serial_port *port);
56static void option_rx_unthrottle(struct usb_serial_port *port);
57static int option_write_room(struct usb_serial_port *port);
Matthias Urlichs58cfe912005-05-23 17:00:48 -070058
59static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
60
Andrew Morton7bb75ae2005-07-27 01:08:30 -070061static int option_write(struct usb_serial_port *port,
62 const unsigned char *buf, int count);
Matthias Urlichs58cfe912005-05-23 17:00:48 -070063
Andrew Morton7bb75ae2005-07-27 01:08:30 -070064static int option_chars_in_buffer(struct usb_serial_port *port);
65static int option_ioctl(struct usb_serial_port *port, struct file *file,
66 unsigned int cmd, unsigned long arg);
67static void option_set_termios(struct usb_serial_port *port,
68 struct termios *old);
69static void option_break_ctl(struct usb_serial_port *port, int break_state);
70static int option_tiocmget(struct usb_serial_port *port, struct file *file);
71static int option_tiocmset(struct usb_serial_port *port, struct file *file,
72 unsigned int set, unsigned int clear);
73static int option_send_setup(struct usb_serial_port *port);
Matthias Urlichs58cfe912005-05-23 17:00:48 -070074
75/* Vendor and product IDs */
Matthias Urlichsba460e42005-07-14 00:33:47 -070076#define OPTION_VENDOR_ID 0x0AF0
Matthias Urlichsb6137382005-09-22 00:48:40 -070077#define HUAWEI_VENDOR_ID 0x12D1
78#define AUDIOVOX_VENDOR_ID 0x0F3D
Matthias Urlichs58cfe912005-05-23 17:00:48 -070079
Matthias Urlichsba460e42005-07-14 00:33:47 -070080#define OPTION_PRODUCT_OLD 0x5000
81#define OPTION_PRODUCT_FUSION 0x6000
82#define OPTION_PRODUCT_FUSION2 0x6300
Matthias Urlichsb6137382005-09-22 00:48:40 -070083#define HUAWEI_PRODUCT_E600 0x1001
84#define AUDIOVOX_PRODUCT_AIRCARD 0x0112
Matthias Urlichsba460e42005-07-14 00:33:47 -070085
Matthias Urlichs58cfe912005-05-23 17:00:48 -070086static struct usb_device_id option_ids[] = {
87 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
Matthias Urlichsba460e42005-07-14 00:33:47 -070088 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
89 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
Matthias Urlichsb6137382005-09-22 00:48:40 -070090 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
91 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
Matthias Urlichs58cfe912005-05-23 17:00:48 -070092 { } /* Terminating entry */
93};
94
95MODULE_DEVICE_TABLE(usb, option_ids);
96
97static struct usb_driver option_driver = {
98 .owner = THIS_MODULE,
99 .name = "option",
100 .probe = usb_serial_probe,
101 .disconnect = usb_serial_disconnect,
102 .id_table = option_ids,
103};
104
105/* The card has three separate interfaces, wich the serial driver
106 * recognizes separately, thus num_port=1.
107 */
108static struct usb_serial_device_type option_3port_device = {
Matthias Urlichsba460e42005-07-14 00:33:47 -0700109 .owner = THIS_MODULE,
110 .name = "Option 3G data card",
111 .short_name = "option",
112 .id_table = option_ids,
113 .num_interrupt_in = NUM_DONT_CARE,
114 .num_bulk_in = NUM_DONT_CARE,
115 .num_bulk_out = NUM_DONT_CARE,
116 .num_ports = 1, /* 3, but the card reports its ports separately */
117 .open = option_open,
118 .close = option_close,
119 .write = option_write,
120 .write_room = option_write_room,
121 .chars_in_buffer = option_chars_in_buffer,
122 .throttle = option_rx_throttle,
123 .unthrottle = option_rx_unthrottle,
124 .ioctl = option_ioctl,
125 .set_termios = option_set_termios,
126 .break_ctl = option_break_ctl,
127 .tiocmget = option_tiocmget,
128 .tiocmset = option_tiocmset,
129 .attach = option_startup,
130 .shutdown = option_shutdown,
131 .read_int_callback = option_instat_callback,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700132};
133
Matthias Urlichsba460e42005-07-14 00:33:47 -0700134#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700135static int debug;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700136#else
137#define debug 0
138#endif
139
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700140/* per port private data */
141
Matthias Urlichsba460e42005-07-14 00:33:47 -0700142#define N_IN_URB 4
143#define N_OUT_URB 1
Matthias Urlichsb27c73d2005-09-22 00:49:33 -0700144#define IN_BUFLEN 4096
Matthias Urlichsba460e42005-07-14 00:33:47 -0700145#define OUT_BUFLEN 128
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700146
147struct option_port_private {
148 /* Input endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700149 struct urb *in_urbs[N_IN_URB];
150 char in_buffer[N_IN_URB][IN_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700151 /* Output endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700152 struct urb *out_urbs[N_OUT_URB];
153 char out_buffer[N_OUT_URB][OUT_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700154
155 /* Settings for the port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700156 int rts_state; /* Handshaking pins (outputs) */
157 int dtr_state;
158 int cts_state; /* Handshaking pins (inputs) */
159 int dsr_state;
160 int dcd_state;
161 int ri_state;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700162
Matthias Urlichsba460e42005-07-14 00:33:47 -0700163 unsigned long tx_start_time[N_OUT_URB];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700164};
165
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700166/* Functions used by new usb-serial code. */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700167static int __init option_init(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700168{
169 int retval;
170 retval = usb_serial_register(&option_3port_device);
171 if (retval)
172 goto failed_3port_device_register;
173 retval = usb_register(&option_driver);
174 if (retval)
175 goto failed_driver_register;
176
177 info(DRIVER_DESC ": " DRIVER_VERSION);
178
179 return 0;
180
181failed_driver_register:
182 usb_serial_deregister (&option_3port_device);
183failed_3port_device_register:
184 return retval;
185}
186
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700187static void __exit option_exit(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700188{
189 usb_deregister (&option_driver);
190 usb_serial_deregister (&option_3port_device);
191}
192
193module_init(option_init);
194module_exit(option_exit);
195
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700196static void option_rx_throttle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700197{
198 dbg("%s", __FUNCTION__);
199}
200
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700201static void option_rx_unthrottle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700202{
203 dbg("%s", __FUNCTION__);
204}
205
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700206static void option_break_ctl(struct usb_serial_port *port, int break_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700207{
208 /* Unfortunately, I don't know how to send a break */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700209 dbg("%s", __FUNCTION__);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700210}
211
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700212static void option_set_termios(struct usb_serial_port *port,
213 struct termios *old_termios)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700214{
215 dbg("%s", __FUNCTION__);
216
217 option_send_setup(port);
218}
219
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700220static int option_tiocmget(struct usb_serial_port *port, struct file *file)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700221{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700222 unsigned int value;
223 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700224
225 portdata = usb_get_serial_port_data(port);
226
227 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
228 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
229 ((portdata->cts_state) ? TIOCM_CTS : 0) |
230 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
231 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
232 ((portdata->ri_state) ? TIOCM_RNG : 0);
233
234 return value;
235}
236
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700237static int option_tiocmset(struct usb_serial_port *port, struct file *file,
238 unsigned int set, unsigned int clear)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700239{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700240 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700241
242 portdata = usb_get_serial_port_data(port);
243
244 if (set & TIOCM_RTS)
245 portdata->rts_state = 1;
246 if (set & TIOCM_DTR)
247 portdata->dtr_state = 1;
248
249 if (clear & TIOCM_RTS)
250 portdata->rts_state = 0;
251 if (clear & TIOCM_DTR)
252 portdata->dtr_state = 0;
253 return option_send_setup(port);
254}
255
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700256static int option_ioctl(struct usb_serial_port *port, struct file *file,
257 unsigned int cmd, unsigned long arg)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700258{
259 return -ENOIOCTLCMD;
260}
261
262/* Write */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700263static int option_write(struct usb_serial_port *port,
264 const unsigned char *buf, int count)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700265{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700266 struct option_port_private *portdata;
267 int i;
268 int left, todo;
269 struct urb *this_urb = NULL; /* spurious */
270 int err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700271
272 portdata = usb_get_serial_port_data(port);
273
274 dbg("%s: write (%d chars)", __FUNCTION__, count);
275
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700276 i = 0;
277 left = count;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700278 for (i=0; left > 0 && i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700279 todo = left;
280 if (todo > OUT_BUFLEN)
281 todo = OUT_BUFLEN;
282
Matthias Urlichsba460e42005-07-14 00:33:47 -0700283 this_urb = portdata->out_urbs[i];
284 if (this_urb->status == -EINPROGRESS) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700285 if (time_before(jiffies,
286 portdata->tx_start_time[i] + 10 * HZ))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700287 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700288 usb_unlink_urb(this_urb);
Matthias Urlichsba460e42005-07-14 00:33:47 -0700289 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700290 }
Matthias Urlichsba460e42005-07-14 00:33:47 -0700291 if (this_urb->status != 0)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700292 dbg("usb_write %p failed (err=%d)",
293 this_urb, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700294
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700295 dbg("%s: endpoint %d buf %d", __FUNCTION__,
296 usb_pipeendpoint(this_urb->pipe), i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700297
Matthias Urlichsba460e42005-07-14 00:33:47 -0700298 /* send the data */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700299 memcpy (this_urb->transfer_buffer, buf, todo);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700300 this_urb->transfer_buffer_length = todo;
301
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700302 this_urb->dev = port->serial->dev;
303 err = usb_submit_urb(this_urb, GFP_ATOMIC);
304 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700305 dbg("usb_submit_urb %p (write bulk) failed "
306 "(%d, has %d)", this_urb,
307 err, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700308 continue;
309 }
310 portdata->tx_start_time[i] = jiffies;
311 buf += todo;
312 left -= todo;
313 }
314
315 count -= left;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700316 dbg("%s: wrote (did %d)", __FUNCTION__, count);
317 return count;
318}
319
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700320static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700321{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700322 int i, err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700323 int endpoint;
324 struct usb_serial_port *port;
325 struct tty_struct *tty;
326 unsigned char *data = urb->transfer_buffer;
327
328 dbg("%s: %p", __FUNCTION__, urb);
329
330 endpoint = usb_pipeendpoint(urb->pipe);
331 port = (struct usb_serial_port *) urb->context;
332
333 if (urb->status) {
334 dbg("%s: nonzero status: %d on endpoint %02x.",
335 __FUNCTION__, urb->status, endpoint);
336 } else {
337 tty = port->tty;
338 if (urb->actual_length) {
339 for (i = 0; i < urb->actual_length ; ++i) {
340 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
341 tty_flip_buffer_push(tty);
342 tty_insert_flip_char(tty, data[i], 0);
343 }
344 tty_flip_buffer_push(tty);
345 } else {
346 dbg("%s: empty read urb received", __FUNCTION__);
347 }
348
349 /* Resubmit urb so we continue receiving */
350 if (port->open_count && urb->status != -ESHUTDOWN) {
351 err = usb_submit_urb(urb, GFP_ATOMIC);
352 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700353 printk(KERN_ERR "%s: resubmit read urb failed. "
354 "(%d)", __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700355 }
356 }
357 return;
358}
359
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700360static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700361{
362 struct usb_serial_port *port;
363
364 dbg("%s", __FUNCTION__);
365
366 port = (struct usb_serial_port *) urb->context;
367
368 if (port->open_count)
369 schedule_work(&port->work);
370}
371
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700372static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700373{
374 int err;
375 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
376 struct option_port_private *portdata = usb_get_serial_port_data(port);
377 struct usb_serial *serial = port->serial;
378
379 dbg("%s", __FUNCTION__);
380 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
381
382 if (urb->status == 0) {
383 struct usb_ctrlrequest *req_pkt =
384 (struct usb_ctrlrequest *)urb->transfer_buffer;
385
386 if (!req_pkt) {
387 dbg("%s: NULL req_pkt\n", __FUNCTION__);
388 return;
389 }
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700390 if ((req_pkt->bRequestType == 0xA1) &&
391 (req_pkt->bRequest == 0x20)) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700392 int old_dcd_state;
393 unsigned char signals = *((unsigned char *)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700394 urb->transfer_buffer +
395 sizeof(struct usb_ctrlrequest));
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700396
397 dbg("%s: signal x%x", __FUNCTION__, signals);
398
399 old_dcd_state = portdata->dcd_state;
400 portdata->cts_state = 1;
401 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
402 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
403 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
404
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700405 if (port->tty && !C_CLOCAL(port->tty) &&
406 old_dcd_state && !portdata->dcd_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700407 tty_hangup(port->tty);
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700408 } else {
409 dbg("%s: type %x req %x", __FUNCTION__,
410 req_pkt->bRequestType,req_pkt->bRequest);
411 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700412 } else
413 dbg("%s: error %d", __FUNCTION__, urb->status);
414
415 /* Resubmit urb so we continue receiving IRQ data */
416 if (urb->status != -ESHUTDOWN) {
417 urb->dev = serial->dev;
418 err = usb_submit_urb(urb, GFP_ATOMIC);
419 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700420 dbg("%s: resubmit intr urb failed. (%d)",
421 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700422 }
423}
424
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700425static int option_write_room(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700426{
427 struct option_port_private *portdata;
428 int i;
429 int data_len = 0;
430 struct urb *this_urb;
431
432 portdata = usb_get_serial_port_data(port);
433
Matthias Urlichsba460e42005-07-14 00:33:47 -0700434 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700435 this_urb = portdata->out_urbs[i];
436 if (this_urb && this_urb->status != -EINPROGRESS)
437 data_len += OUT_BUFLEN;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700438 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700439
440 dbg("%s: %d", __FUNCTION__, data_len);
441 return data_len;
442}
443
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700444static int option_chars_in_buffer(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700445{
446 struct option_port_private *portdata;
447 int i;
448 int data_len = 0;
449 struct urb *this_urb;
450
451 portdata = usb_get_serial_port_data(port);
452
Matthias Urlichsba460e42005-07-14 00:33:47 -0700453 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700454 this_urb = portdata->out_urbs[i];
455 if (this_urb && this_urb->status == -EINPROGRESS)
456 data_len += this_urb->transfer_buffer_length;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700457 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700458 dbg("%s: %d", __FUNCTION__, data_len);
459 return data_len;
460}
461
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700462static int option_open(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700463{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700464 struct option_port_private *portdata;
465 struct usb_serial *serial = port->serial;
466 int i, err;
467 struct urb *urb;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700468
469 portdata = usb_get_serial_port_data(port);
470
471 dbg("%s", __FUNCTION__);
472
473 /* Set some sane defaults */
474 portdata->rts_state = 1;
475 portdata->dtr_state = 1;
476
477 /* Reset low level data toggle and start reading from endpoints */
478 for (i = 0; i < N_IN_URB; i++) {
479 urb = portdata->in_urbs[i];
480 if (! urb)
481 continue;
482 if (urb->dev != serial->dev) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700483 dbg("%s: dev %p != %p", __FUNCTION__,
484 urb->dev, serial->dev);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700485 continue;
486 }
487
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700488 /*
489 * make sure endpoint data toggle is synchronized with the
490 * device
491 */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700492 usb_clear_halt(urb->dev, urb->pipe);
493
494 err = usb_submit_urb(urb, GFP_KERNEL);
495 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700496 dbg("%s: submit urb %d failed (%d) %d",
497 __FUNCTION__, i, err,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700498 urb->transfer_buffer_length);
499 }
500 }
501
502 /* Reset low level data toggle on out endpoints */
503 for (i = 0; i < N_OUT_URB; i++) {
504 urb = portdata->out_urbs[i];
505 if (! urb)
506 continue;
507 urb->dev = serial->dev;
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700508 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
509 usb_pipeout(urb->pipe), 0); */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700510 }
511
512 port->tty->low_latency = 1;
513
514 option_send_setup(port);
515
516 return (0);
517}
518
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700519static inline void stop_urb(struct urb *urb)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700520{
Greg Kroah-Hartman242cf672005-07-29 16:11:07 -0400521 if (urb && urb->status == -EINPROGRESS)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700522 usb_kill_urb(urb);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700523}
524
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700525static void option_close(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700526{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700527 int i;
528 struct usb_serial *serial = port->serial;
529 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700530
531 dbg("%s", __FUNCTION__);
532 portdata = usb_get_serial_port_data(port);
533
534 portdata->rts_state = 0;
535 portdata->dtr_state = 0;
536
537 if (serial->dev) {
538 option_send_setup(port);
539
540 /* Stop reading/writing urbs */
541 for (i = 0; i < N_IN_URB; i++)
542 stop_urb(portdata->in_urbs[i]);
543 for (i = 0; i < N_OUT_URB; i++)
544 stop_urb(portdata->out_urbs[i]);
545 }
546 port->tty = NULL;
547}
548
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700549/* Helper functions used by option_setup_urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700550static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
551 int dir, void *ctx, char *buf, int len,
552 void (*callback)(struct urb *, struct pt_regs *regs))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700553{
554 struct urb *urb;
555
556 if (endpoint == -1)
557 return NULL; /* endpoint not needed */
558
559 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
560 if (urb == NULL) {
561 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
562 return NULL;
563 }
564
565 /* Fill URB using supplied data. */
566 usb_fill_bulk_urb(urb, serial->dev,
567 usb_sndbulkpipe(serial->dev, endpoint) | dir,
568 buf, len, callback, ctx);
569
570 return urb;
571}
572
573/* Setup urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700574static void option_setup_urbs(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700575{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700576 int j;
577 struct usb_serial_port *port;
578 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700579
580 dbg("%s", __FUNCTION__);
581
582 port = serial->port[0];
583 portdata = usb_get_serial_port_data(port);
584
585 /* Do indat endpoints first */
586 for (j = 0; j <= N_IN_URB; ++j) {
587 portdata->in_urbs[j] = option_setup_urb (serial,
588 port->bulk_in_endpointAddress, USB_DIR_IN, port,
589 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
590 }
591
592 /* outdat endpoints */
593 for (j = 0; j <= N_OUT_URB; ++j) {
594 portdata->out_urbs[j] = option_setup_urb (serial,
595 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
596 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
597 }
598}
599
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700600static int option_send_setup(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700601{
602 struct usb_serial *serial = port->serial;
603 struct option_port_private *portdata;
604
605 dbg("%s", __FUNCTION__);
606
607 portdata = usb_get_serial_port_data(port);
608
609 if (port->tty) {
610 int val = 0;
611 if (portdata->dtr_state)
612 val |= 0x01;
613 if (portdata->rts_state)
614 val |= 0x02;
615
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700616 return usb_control_msg(serial->dev,
617 usb_rcvctrlpipe(serial->dev, 0),
618 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700619 }
620
621 return 0;
622}
623
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700624static int option_startup(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700625{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700626 int i, err;
627 struct usb_serial_port *port;
628 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700629
630 dbg("%s", __FUNCTION__);
631
632 /* Now setup per port private data */
633 for (i = 0; i < serial->num_ports; i++) {
634 port = serial->port[i];
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700635 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700636 if (!portdata) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700637 dbg("%s: kmalloc for option_port_private (%d) failed!.",
638 __FUNCTION__, i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700639 return (1);
640 }
641 memset(portdata, 0, sizeof(struct option_port_private));
642
643 usb_set_serial_port_data(port, portdata);
644
645 if (! port->interrupt_in_urb)
646 continue;
647 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
648 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700649 dbg("%s: submit irq_in urb failed %d",
650 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700651 }
652
653 option_setup_urbs(serial);
654
655 return (0);
656}
657
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700658static void option_shutdown(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700659{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700660 int i, j;
661 struct usb_serial_port *port;
662 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700663
664 dbg("%s", __FUNCTION__);
665
666 /* Stop reading/writing urbs */
667 for (i = 0; i < serial->num_ports; ++i) {
668 port = serial->port[i];
669 portdata = usb_get_serial_port_data(port);
670 for (j = 0; j < N_IN_URB; j++)
671 stop_urb(portdata->in_urbs[j]);
672 for (j = 0; j < N_OUT_URB; j++)
673 stop_urb(portdata->out_urbs[j]);
674 }
675
676 /* Now free them */
677 for (i = 0; i < serial->num_ports; ++i) {
678 port = serial->port[i];
679 portdata = usb_get_serial_port_data(port);
680
681 for (j = 0; j < N_IN_URB; j++) {
682 if (portdata->in_urbs[j]) {
683 usb_free_urb(portdata->in_urbs[j]);
684 portdata->in_urbs[j] = NULL;
685 }
686 }
687 for (j = 0; j < N_OUT_URB; j++) {
688 if (portdata->out_urbs[j]) {
689 usb_free_urb(portdata->out_urbs[j]);
690 portdata->out_urbs[j] = NULL;
691 }
692 }
693 }
694
695 /* Now free per port private data */
696 for (i = 0; i < serial->num_ports; i++) {
697 port = serial->port[i];
698 kfree(usb_get_serial_port_data(port));
699 }
700}
701
702MODULE_AUTHOR(DRIVER_AUTHOR);
703MODULE_DESCRIPTION(DRIVER_DESC);
704MODULE_VERSION(DRIVER_VERSION);
705MODULE_LICENSE("GPL");
706
Matthias Urlichsba460e42005-07-14 00:33:47 -0700707#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700708module_param(debug, bool, S_IRUGO | S_IWUSR);
709MODULE_PARM_DESC(debug, "Debug messages");
Matthias Urlichsba460e42005-07-14 00:33:47 -0700710#endif
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700711