blob: 4ee657eaaa0ba8289b01a199167112d4cafa9353 [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,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800103 .no_dynamic_id = 1,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700104};
105
106/* The card has three separate interfaces, wich the serial driver
107 * recognizes separately, thus num_port=1.
108 */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700109static struct usb_serial_driver option_3port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700110 .driver = {
111 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700112 .name = "option",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700113 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700114 .description = "Option 3G data card",
Matthias Urlichsba460e42005-07-14 00:33:47 -0700115 .id_table = option_ids,
116 .num_interrupt_in = NUM_DONT_CARE,
117 .num_bulk_in = NUM_DONT_CARE,
118 .num_bulk_out = NUM_DONT_CARE,
119 .num_ports = 1, /* 3, but the card reports its ports separately */
120 .open = option_open,
121 .close = option_close,
122 .write = option_write,
123 .write_room = option_write_room,
124 .chars_in_buffer = option_chars_in_buffer,
125 .throttle = option_rx_throttle,
126 .unthrottle = option_rx_unthrottle,
127 .ioctl = option_ioctl,
128 .set_termios = option_set_termios,
129 .break_ctl = option_break_ctl,
130 .tiocmget = option_tiocmget,
131 .tiocmset = option_tiocmset,
132 .attach = option_startup,
133 .shutdown = option_shutdown,
134 .read_int_callback = option_instat_callback,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700135};
136
Matthias Urlichsba460e42005-07-14 00:33:47 -0700137#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700138static int debug;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700139#else
140#define debug 0
141#endif
142
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700143/* per port private data */
144
Matthias Urlichsba460e42005-07-14 00:33:47 -0700145#define N_IN_URB 4
146#define N_OUT_URB 1
Matthias Urlichsb27c73d2005-09-22 00:49:33 -0700147#define IN_BUFLEN 4096
Matthias Urlichsba460e42005-07-14 00:33:47 -0700148#define OUT_BUFLEN 128
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700149
150struct option_port_private {
151 /* Input endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700152 struct urb *in_urbs[N_IN_URB];
153 char in_buffer[N_IN_URB][IN_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700154 /* Output endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700155 struct urb *out_urbs[N_OUT_URB];
156 char out_buffer[N_OUT_URB][OUT_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700157
158 /* Settings for the port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700159 int rts_state; /* Handshaking pins (outputs) */
160 int dtr_state;
161 int cts_state; /* Handshaking pins (inputs) */
162 int dsr_state;
163 int dcd_state;
164 int ri_state;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700165
Matthias Urlichsba460e42005-07-14 00:33:47 -0700166 unsigned long tx_start_time[N_OUT_URB];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700167};
168
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700169/* Functions used by new usb-serial code. */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700170static int __init option_init(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700171{
172 int retval;
173 retval = usb_serial_register(&option_3port_device);
174 if (retval)
175 goto failed_3port_device_register;
176 retval = usb_register(&option_driver);
177 if (retval)
178 goto failed_driver_register;
179
180 info(DRIVER_DESC ": " DRIVER_VERSION);
181
182 return 0;
183
184failed_driver_register:
185 usb_serial_deregister (&option_3port_device);
186failed_3port_device_register:
187 return retval;
188}
189
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700190static void __exit option_exit(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700191{
192 usb_deregister (&option_driver);
193 usb_serial_deregister (&option_3port_device);
194}
195
196module_init(option_init);
197module_exit(option_exit);
198
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700199static void option_rx_throttle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700200{
201 dbg("%s", __FUNCTION__);
202}
203
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700204static void option_rx_unthrottle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700205{
206 dbg("%s", __FUNCTION__);
207}
208
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700209static void option_break_ctl(struct usb_serial_port *port, int break_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700210{
211 /* Unfortunately, I don't know how to send a break */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700212 dbg("%s", __FUNCTION__);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700213}
214
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700215static void option_set_termios(struct usb_serial_port *port,
216 struct termios *old_termios)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700217{
218 dbg("%s", __FUNCTION__);
219
220 option_send_setup(port);
221}
222
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700223static int option_tiocmget(struct usb_serial_port *port, struct file *file)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700224{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700225 unsigned int value;
226 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700227
228 portdata = usb_get_serial_port_data(port);
229
230 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
231 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
232 ((portdata->cts_state) ? TIOCM_CTS : 0) |
233 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
234 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
235 ((portdata->ri_state) ? TIOCM_RNG : 0);
236
237 return value;
238}
239
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700240static int option_tiocmset(struct usb_serial_port *port, struct file *file,
241 unsigned int set, unsigned int clear)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700242{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700243 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700244
245 portdata = usb_get_serial_port_data(port);
246
247 if (set & TIOCM_RTS)
248 portdata->rts_state = 1;
249 if (set & TIOCM_DTR)
250 portdata->dtr_state = 1;
251
252 if (clear & TIOCM_RTS)
253 portdata->rts_state = 0;
254 if (clear & TIOCM_DTR)
255 portdata->dtr_state = 0;
256 return option_send_setup(port);
257}
258
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700259static int option_ioctl(struct usb_serial_port *port, struct file *file,
260 unsigned int cmd, unsigned long arg)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700261{
262 return -ENOIOCTLCMD;
263}
264
265/* Write */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700266static int option_write(struct usb_serial_port *port,
267 const unsigned char *buf, int count)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700268{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700269 struct option_port_private *portdata;
270 int i;
271 int left, todo;
272 struct urb *this_urb = NULL; /* spurious */
273 int err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700274
275 portdata = usb_get_serial_port_data(port);
276
277 dbg("%s: write (%d chars)", __FUNCTION__, count);
278
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700279 i = 0;
280 left = count;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700281 for (i=0; left > 0 && i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700282 todo = left;
283 if (todo > OUT_BUFLEN)
284 todo = OUT_BUFLEN;
285
Matthias Urlichsba460e42005-07-14 00:33:47 -0700286 this_urb = portdata->out_urbs[i];
287 if (this_urb->status == -EINPROGRESS) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700288 if (time_before(jiffies,
289 portdata->tx_start_time[i] + 10 * HZ))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700290 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700291 usb_unlink_urb(this_urb);
Matthias Urlichsba460e42005-07-14 00:33:47 -0700292 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700293 }
Matthias Urlichsba460e42005-07-14 00:33:47 -0700294 if (this_urb->status != 0)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700295 dbg("usb_write %p failed (err=%d)",
296 this_urb, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700297
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700298 dbg("%s: endpoint %d buf %d", __FUNCTION__,
299 usb_pipeendpoint(this_urb->pipe), i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700300
Matthias Urlichsba460e42005-07-14 00:33:47 -0700301 /* send the data */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700302 memcpy (this_urb->transfer_buffer, buf, todo);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700303 this_urb->transfer_buffer_length = todo;
304
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700305 this_urb->dev = port->serial->dev;
306 err = usb_submit_urb(this_urb, GFP_ATOMIC);
307 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700308 dbg("usb_submit_urb %p (write bulk) failed "
309 "(%d, has %d)", this_urb,
310 err, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700311 continue;
312 }
313 portdata->tx_start_time[i] = jiffies;
314 buf += todo;
315 left -= todo;
316 }
317
318 count -= left;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700319 dbg("%s: wrote (did %d)", __FUNCTION__, count);
320 return count;
321}
322
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700323static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700324{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700325 int i, err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700326 int endpoint;
327 struct usb_serial_port *port;
328 struct tty_struct *tty;
329 unsigned char *data = urb->transfer_buffer;
330
331 dbg("%s: %p", __FUNCTION__, urb);
332
333 endpoint = usb_pipeendpoint(urb->pipe);
334 port = (struct usb_serial_port *) urb->context;
335
336 if (urb->status) {
337 dbg("%s: nonzero status: %d on endpoint %02x.",
338 __FUNCTION__, urb->status, endpoint);
339 } else {
340 tty = port->tty;
341 if (urb->actual_length) {
342 for (i = 0; i < urb->actual_length ; ++i) {
343 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
344 tty_flip_buffer_push(tty);
345 tty_insert_flip_char(tty, data[i], 0);
346 }
347 tty_flip_buffer_push(tty);
348 } else {
349 dbg("%s: empty read urb received", __FUNCTION__);
350 }
351
352 /* Resubmit urb so we continue receiving */
353 if (port->open_count && urb->status != -ESHUTDOWN) {
354 err = usb_submit_urb(urb, GFP_ATOMIC);
355 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700356 printk(KERN_ERR "%s: resubmit read urb failed. "
357 "(%d)", __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700358 }
359 }
360 return;
361}
362
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700363static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700364{
365 struct usb_serial_port *port;
366
367 dbg("%s", __FUNCTION__);
368
369 port = (struct usb_serial_port *) urb->context;
370
371 if (port->open_count)
372 schedule_work(&port->work);
373}
374
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700375static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700376{
377 int err;
378 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
379 struct option_port_private *portdata = usb_get_serial_port_data(port);
380 struct usb_serial *serial = port->serial;
381
382 dbg("%s", __FUNCTION__);
383 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
384
385 if (urb->status == 0) {
386 struct usb_ctrlrequest *req_pkt =
387 (struct usb_ctrlrequest *)urb->transfer_buffer;
388
389 if (!req_pkt) {
390 dbg("%s: NULL req_pkt\n", __FUNCTION__);
391 return;
392 }
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700393 if ((req_pkt->bRequestType == 0xA1) &&
394 (req_pkt->bRequest == 0x20)) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700395 int old_dcd_state;
396 unsigned char signals = *((unsigned char *)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700397 urb->transfer_buffer +
398 sizeof(struct usb_ctrlrequest));
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700399
400 dbg("%s: signal x%x", __FUNCTION__, signals);
401
402 old_dcd_state = portdata->dcd_state;
403 portdata->cts_state = 1;
404 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
405 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
406 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
407
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700408 if (port->tty && !C_CLOCAL(port->tty) &&
409 old_dcd_state && !portdata->dcd_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700410 tty_hangup(port->tty);
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700411 } else {
412 dbg("%s: type %x req %x", __FUNCTION__,
413 req_pkt->bRequestType,req_pkt->bRequest);
414 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700415 } else
416 dbg("%s: error %d", __FUNCTION__, urb->status);
417
418 /* Resubmit urb so we continue receiving IRQ data */
419 if (urb->status != -ESHUTDOWN) {
420 urb->dev = serial->dev;
421 err = usb_submit_urb(urb, GFP_ATOMIC);
422 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700423 dbg("%s: resubmit intr urb failed. (%d)",
424 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700425 }
426}
427
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700428static int option_write_room(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700429{
430 struct option_port_private *portdata;
431 int i;
432 int data_len = 0;
433 struct urb *this_urb;
434
435 portdata = usb_get_serial_port_data(port);
436
Matthias Urlichsba460e42005-07-14 00:33:47 -0700437 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700438 this_urb = portdata->out_urbs[i];
439 if (this_urb && this_urb->status != -EINPROGRESS)
440 data_len += OUT_BUFLEN;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700441 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700442
443 dbg("%s: %d", __FUNCTION__, data_len);
444 return data_len;
445}
446
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700447static int option_chars_in_buffer(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700448{
449 struct option_port_private *portdata;
450 int i;
451 int data_len = 0;
452 struct urb *this_urb;
453
454 portdata = usb_get_serial_port_data(port);
455
Matthias Urlichsba460e42005-07-14 00:33:47 -0700456 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700457 this_urb = portdata->out_urbs[i];
458 if (this_urb && this_urb->status == -EINPROGRESS)
459 data_len += this_urb->transfer_buffer_length;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700460 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700461 dbg("%s: %d", __FUNCTION__, data_len);
462 return data_len;
463}
464
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700465static int option_open(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700466{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700467 struct option_port_private *portdata;
468 struct usb_serial *serial = port->serial;
469 int i, err;
470 struct urb *urb;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700471
472 portdata = usb_get_serial_port_data(port);
473
474 dbg("%s", __FUNCTION__);
475
476 /* Set some sane defaults */
477 portdata->rts_state = 1;
478 portdata->dtr_state = 1;
479
480 /* Reset low level data toggle and start reading from endpoints */
481 for (i = 0; i < N_IN_URB; i++) {
482 urb = portdata->in_urbs[i];
483 if (! urb)
484 continue;
485 if (urb->dev != serial->dev) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700486 dbg("%s: dev %p != %p", __FUNCTION__,
487 urb->dev, serial->dev);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700488 continue;
489 }
490
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700491 /*
492 * make sure endpoint data toggle is synchronized with the
493 * device
494 */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700495 usb_clear_halt(urb->dev, urb->pipe);
496
497 err = usb_submit_urb(urb, GFP_KERNEL);
498 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700499 dbg("%s: submit urb %d failed (%d) %d",
500 __FUNCTION__, i, err,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700501 urb->transfer_buffer_length);
502 }
503 }
504
505 /* Reset low level data toggle on out endpoints */
506 for (i = 0; i < N_OUT_URB; i++) {
507 urb = portdata->out_urbs[i];
508 if (! urb)
509 continue;
510 urb->dev = serial->dev;
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700511 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
512 usb_pipeout(urb->pipe), 0); */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700513 }
514
515 port->tty->low_latency = 1;
516
517 option_send_setup(port);
518
519 return (0);
520}
521
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700522static inline void stop_urb(struct urb *urb)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700523{
Greg Kroah-Hartman242cf672005-07-29 16:11:07 -0400524 if (urb && urb->status == -EINPROGRESS)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700525 usb_kill_urb(urb);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700526}
527
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700528static void option_close(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700529{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700530 int i;
531 struct usb_serial *serial = port->serial;
532 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700533
534 dbg("%s", __FUNCTION__);
535 portdata = usb_get_serial_port_data(port);
536
537 portdata->rts_state = 0;
538 portdata->dtr_state = 0;
539
540 if (serial->dev) {
541 option_send_setup(port);
542
543 /* Stop reading/writing urbs */
544 for (i = 0; i < N_IN_URB; i++)
545 stop_urb(portdata->in_urbs[i]);
546 for (i = 0; i < N_OUT_URB; i++)
547 stop_urb(portdata->out_urbs[i]);
548 }
549 port->tty = NULL;
550}
551
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700552/* Helper functions used by option_setup_urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700553static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
554 int dir, void *ctx, char *buf, int len,
555 void (*callback)(struct urb *, struct pt_regs *regs))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700556{
557 struct urb *urb;
558
559 if (endpoint == -1)
560 return NULL; /* endpoint not needed */
561
562 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
563 if (urb == NULL) {
564 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
565 return NULL;
566 }
567
568 /* Fill URB using supplied data. */
569 usb_fill_bulk_urb(urb, serial->dev,
570 usb_sndbulkpipe(serial->dev, endpoint) | dir,
571 buf, len, callback, ctx);
572
573 return urb;
574}
575
576/* Setup urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700577static void option_setup_urbs(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700578{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700579 int j;
580 struct usb_serial_port *port;
581 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700582
583 dbg("%s", __FUNCTION__);
584
585 port = serial->port[0];
586 portdata = usb_get_serial_port_data(port);
587
588 /* Do indat endpoints first */
589 for (j = 0; j <= N_IN_URB; ++j) {
590 portdata->in_urbs[j] = option_setup_urb (serial,
591 port->bulk_in_endpointAddress, USB_DIR_IN, port,
592 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
593 }
594
595 /* outdat endpoints */
596 for (j = 0; j <= N_OUT_URB; ++j) {
597 portdata->out_urbs[j] = option_setup_urb (serial,
598 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
599 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
600 }
601}
602
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700603static int option_send_setup(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700604{
605 struct usb_serial *serial = port->serial;
606 struct option_port_private *portdata;
607
608 dbg("%s", __FUNCTION__);
609
610 portdata = usb_get_serial_port_data(port);
611
612 if (port->tty) {
613 int val = 0;
614 if (portdata->dtr_state)
615 val |= 0x01;
616 if (portdata->rts_state)
617 val |= 0x02;
618
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700619 return usb_control_msg(serial->dev,
620 usb_rcvctrlpipe(serial->dev, 0),
621 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700622 }
623
624 return 0;
625}
626
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700627static int option_startup(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700628{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700629 int i, err;
630 struct usb_serial_port *port;
631 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700632
633 dbg("%s", __FUNCTION__);
634
635 /* Now setup per port private data */
636 for (i = 0; i < serial->num_ports; i++) {
637 port = serial->port[i];
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700638 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700639 if (!portdata) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700640 dbg("%s: kmalloc for option_port_private (%d) failed!.",
641 __FUNCTION__, i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700642 return (1);
643 }
644 memset(portdata, 0, sizeof(struct option_port_private));
645
646 usb_set_serial_port_data(port, portdata);
647
648 if (! port->interrupt_in_urb)
649 continue;
650 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
651 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700652 dbg("%s: submit irq_in urb failed %d",
653 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700654 }
655
656 option_setup_urbs(serial);
657
658 return (0);
659}
660
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700661static void option_shutdown(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700662{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700663 int i, j;
664 struct usb_serial_port *port;
665 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700666
667 dbg("%s", __FUNCTION__);
668
669 /* Stop reading/writing urbs */
670 for (i = 0; i < serial->num_ports; ++i) {
671 port = serial->port[i];
672 portdata = usb_get_serial_port_data(port);
673 for (j = 0; j < N_IN_URB; j++)
674 stop_urb(portdata->in_urbs[j]);
675 for (j = 0; j < N_OUT_URB; j++)
676 stop_urb(portdata->out_urbs[j]);
677 }
678
679 /* Now free them */
680 for (i = 0; i < serial->num_ports; ++i) {
681 port = serial->port[i];
682 portdata = usb_get_serial_port_data(port);
683
684 for (j = 0; j < N_IN_URB; j++) {
685 if (portdata->in_urbs[j]) {
686 usb_free_urb(portdata->in_urbs[j]);
687 portdata->in_urbs[j] = NULL;
688 }
689 }
690 for (j = 0; j < N_OUT_URB; j++) {
691 if (portdata->out_urbs[j]) {
692 usb_free_urb(portdata->out_urbs[j]);
693 portdata->out_urbs[j] = NULL;
694 }
695 }
696 }
697
698 /* Now free per port private data */
699 for (i = 0; i < serial->num_ports; i++) {
700 port = serial->port[i];
701 kfree(usb_get_serial_port_data(port));
702 }
703}
704
705MODULE_AUTHOR(DRIVER_AUTHOR);
706MODULE_DESCRIPTION(DRIVER_DESC);
707MODULE_VERSION(DRIVER_VERSION);
708MODULE_LICENSE("GPL");
709
Matthias Urlichsba460e42005-07-14 00:33:47 -0700710#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700711module_param(debug, bool, S_IRUGO | S_IWUSR);
712MODULE_PARM_DESC(debug, "Debug messages");
Matthias Urlichsba460e42005-07-14 00:33:47 -0700713#endif
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700714