blob: 495db5755df9e3a1ea1ff7aae075d8853c8b1485 [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 = {
Matthias Urlichs58cfe912005-05-23 17:00:48 -070098 .name = "option",
99 .probe = usb_serial_probe,
100 .disconnect = usb_serial_disconnect,
101 .id_table = option_ids,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800102 .no_dynamic_id = 1,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700103};
104
Uwe Zeisbergerc30fe7f2006-03-24 18:23:14 +0100105/* The card has three separate interfaces, which the serial driver
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700106 * recognizes separately, thus num_port=1.
107 */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700108static struct usb_serial_driver option_3port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700109 .driver = {
110 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700111 .name = "option",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700112 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700113 .description = "Option 3G data card",
Matthias Urlichsba460e42005-07-14 00:33:47 -0700114 .id_table = option_ids,
115 .num_interrupt_in = NUM_DONT_CARE,
116 .num_bulk_in = NUM_DONT_CARE,
117 .num_bulk_out = NUM_DONT_CARE,
118 .num_ports = 1, /* 3, but the card reports its ports separately */
119 .open = option_open,
120 .close = option_close,
121 .write = option_write,
122 .write_room = option_write_room,
123 .chars_in_buffer = option_chars_in_buffer,
124 .throttle = option_rx_throttle,
125 .unthrottle = option_rx_unthrottle,
126 .ioctl = option_ioctl,
127 .set_termios = option_set_termios,
128 .break_ctl = option_break_ctl,
129 .tiocmget = option_tiocmget,
130 .tiocmset = option_tiocmset,
131 .attach = option_startup,
132 .shutdown = option_shutdown,
133 .read_int_callback = option_instat_callback,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700134};
135
Matthias Urlichsba460e42005-07-14 00:33:47 -0700136#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700137static int debug;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700138#else
139#define debug 0
140#endif
141
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700142/* per port private data */
143
Matthias Urlichsba460e42005-07-14 00:33:47 -0700144#define N_IN_URB 4
145#define N_OUT_URB 1
Matthias Urlichsb27c73d2005-09-22 00:49:33 -0700146#define IN_BUFLEN 4096
Matthias Urlichsba460e42005-07-14 00:33:47 -0700147#define OUT_BUFLEN 128
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700148
149struct option_port_private {
150 /* Input endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700151 struct urb *in_urbs[N_IN_URB];
152 char in_buffer[N_IN_URB][IN_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700153 /* Output endpoints and buffer for this port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700154 struct urb *out_urbs[N_OUT_URB];
155 char out_buffer[N_OUT_URB][OUT_BUFLEN];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700156
157 /* Settings for the port */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700158 int rts_state; /* Handshaking pins (outputs) */
159 int dtr_state;
160 int cts_state; /* Handshaking pins (inputs) */
161 int dsr_state;
162 int dcd_state;
163 int ri_state;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700164
Matthias Urlichsba460e42005-07-14 00:33:47 -0700165 unsigned long tx_start_time[N_OUT_URB];
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700166};
167
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700168/* Functions used by new usb-serial code. */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700169static int __init option_init(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700170{
171 int retval;
172 retval = usb_serial_register(&option_3port_device);
173 if (retval)
174 goto failed_3port_device_register;
175 retval = usb_register(&option_driver);
176 if (retval)
177 goto failed_driver_register;
178
179 info(DRIVER_DESC ": " DRIVER_VERSION);
180
181 return 0;
182
183failed_driver_register:
184 usb_serial_deregister (&option_3port_device);
185failed_3port_device_register:
186 return retval;
187}
188
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700189static void __exit option_exit(void)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700190{
191 usb_deregister (&option_driver);
192 usb_serial_deregister (&option_3port_device);
193}
194
195module_init(option_init);
196module_exit(option_exit);
197
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700198static void option_rx_throttle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700199{
200 dbg("%s", __FUNCTION__);
201}
202
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700203static void option_rx_unthrottle(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700204{
205 dbg("%s", __FUNCTION__);
206}
207
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700208static void option_break_ctl(struct usb_serial_port *port, int break_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700209{
210 /* Unfortunately, I don't know how to send a break */
Matthias Urlichsba460e42005-07-14 00:33:47 -0700211 dbg("%s", __FUNCTION__);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700212}
213
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700214static void option_set_termios(struct usb_serial_port *port,
215 struct termios *old_termios)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700216{
217 dbg("%s", __FUNCTION__);
218
219 option_send_setup(port);
220}
221
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700222static int option_tiocmget(struct usb_serial_port *port, struct file *file)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700223{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700224 unsigned int value;
225 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700226
227 portdata = usb_get_serial_port_data(port);
228
229 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
230 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
231 ((portdata->cts_state) ? TIOCM_CTS : 0) |
232 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
233 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
234 ((portdata->ri_state) ? TIOCM_RNG : 0);
235
236 return value;
237}
238
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700239static int option_tiocmset(struct usb_serial_port *port, struct file *file,
240 unsigned int set, unsigned int clear)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700241{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700242 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700243
244 portdata = usb_get_serial_port_data(port);
245
246 if (set & TIOCM_RTS)
247 portdata->rts_state = 1;
248 if (set & TIOCM_DTR)
249 portdata->dtr_state = 1;
250
251 if (clear & TIOCM_RTS)
252 portdata->rts_state = 0;
253 if (clear & TIOCM_DTR)
254 portdata->dtr_state = 0;
255 return option_send_setup(port);
256}
257
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700258static int option_ioctl(struct usb_serial_port *port, struct file *file,
259 unsigned int cmd, unsigned long arg)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700260{
261 return -ENOIOCTLCMD;
262}
263
264/* Write */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700265static int option_write(struct usb_serial_port *port,
266 const unsigned char *buf, int count)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700267{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700268 struct option_port_private *portdata;
269 int i;
270 int left, todo;
271 struct urb *this_urb = NULL; /* spurious */
272 int err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700273
274 portdata = usb_get_serial_port_data(port);
275
276 dbg("%s: write (%d chars)", __FUNCTION__, count);
277
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700278 i = 0;
279 left = count;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700280 for (i=0; left > 0 && i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700281 todo = left;
282 if (todo > OUT_BUFLEN)
283 todo = OUT_BUFLEN;
284
Matthias Urlichsba460e42005-07-14 00:33:47 -0700285 this_urb = portdata->out_urbs[i];
286 if (this_urb->status == -EINPROGRESS) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700287 if (time_before(jiffies,
288 portdata->tx_start_time[i] + 10 * HZ))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700289 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700290 usb_unlink_urb(this_urb);
Matthias Urlichsba460e42005-07-14 00:33:47 -0700291 continue;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700292 }
Matthias Urlichsba460e42005-07-14 00:33:47 -0700293 if (this_urb->status != 0)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700294 dbg("usb_write %p failed (err=%d)",
295 this_urb, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700296
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700297 dbg("%s: endpoint %d buf %d", __FUNCTION__,
298 usb_pipeendpoint(this_urb->pipe), i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700299
Matthias Urlichsba460e42005-07-14 00:33:47 -0700300 /* send the data */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700301 memcpy (this_urb->transfer_buffer, buf, todo);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700302 this_urb->transfer_buffer_length = todo;
303
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700304 this_urb->dev = port->serial->dev;
305 err = usb_submit_urb(this_urb, GFP_ATOMIC);
306 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700307 dbg("usb_submit_urb %p (write bulk) failed "
308 "(%d, has %d)", this_urb,
309 err, this_urb->status);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700310 continue;
311 }
312 portdata->tx_start_time[i] = jiffies;
313 buf += todo;
314 left -= todo;
315 }
316
317 count -= left;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700318 dbg("%s: wrote (did %d)", __FUNCTION__, count);
319 return count;
320}
321
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700322static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700323{
Alan Cox33f0f882006-01-09 20:54:13 -0800324 int err;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700325 int endpoint;
326 struct usb_serial_port *port;
327 struct tty_struct *tty;
328 unsigned char *data = urb->transfer_buffer;
329
330 dbg("%s: %p", __FUNCTION__, urb);
331
332 endpoint = usb_pipeendpoint(urb->pipe);
333 port = (struct usb_serial_port *) urb->context;
334
335 if (urb->status) {
336 dbg("%s: nonzero status: %d on endpoint %02x.",
337 __FUNCTION__, urb->status, endpoint);
338 } else {
339 tty = port->tty;
340 if (urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800341 tty_buffer_request_room(tty, urb->actual_length);
342 tty_insert_flip_string(tty, data, urb->actual_length);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700343 tty_flip_buffer_push(tty);
344 } else {
345 dbg("%s: empty read urb received", __FUNCTION__);
346 }
347
348 /* Resubmit urb so we continue receiving */
349 if (port->open_count && urb->status != -ESHUTDOWN) {
350 err = usb_submit_urb(urb, GFP_ATOMIC);
351 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700352 printk(KERN_ERR "%s: resubmit read urb failed. "
353 "(%d)", __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700354 }
355 }
356 return;
357}
358
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700359static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700360{
361 struct usb_serial_port *port;
362
363 dbg("%s", __FUNCTION__);
364
365 port = (struct usb_serial_port *) urb->context;
366
367 if (port->open_count)
368 schedule_work(&port->work);
369}
370
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700371static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700372{
373 int err;
374 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
375 struct option_port_private *portdata = usb_get_serial_port_data(port);
376 struct usb_serial *serial = port->serial;
377
378 dbg("%s", __FUNCTION__);
379 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
380
381 if (urb->status == 0) {
382 struct usb_ctrlrequest *req_pkt =
383 (struct usb_ctrlrequest *)urb->transfer_buffer;
384
385 if (!req_pkt) {
386 dbg("%s: NULL req_pkt\n", __FUNCTION__);
387 return;
388 }
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700389 if ((req_pkt->bRequestType == 0xA1) &&
390 (req_pkt->bRequest == 0x20)) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700391 int old_dcd_state;
392 unsigned char signals = *((unsigned char *)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700393 urb->transfer_buffer +
394 sizeof(struct usb_ctrlrequest));
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700395
396 dbg("%s: signal x%x", __FUNCTION__, signals);
397
398 old_dcd_state = portdata->dcd_state;
399 portdata->cts_state = 1;
400 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
401 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
402 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
403
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700404 if (port->tty && !C_CLOCAL(port->tty) &&
405 old_dcd_state && !portdata->dcd_state)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700406 tty_hangup(port->tty);
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700407 } else {
408 dbg("%s: type %x req %x", __FUNCTION__,
409 req_pkt->bRequestType,req_pkt->bRequest);
410 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700411 } else
412 dbg("%s: error %d", __FUNCTION__, urb->status);
413
414 /* Resubmit urb so we continue receiving IRQ data */
415 if (urb->status != -ESHUTDOWN) {
416 urb->dev = serial->dev;
417 err = usb_submit_urb(urb, GFP_ATOMIC);
418 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700419 dbg("%s: resubmit intr urb failed. (%d)",
420 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700421 }
422}
423
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700424static int option_write_room(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700425{
426 struct option_port_private *portdata;
427 int i;
428 int data_len = 0;
429 struct urb *this_urb;
430
431 portdata = usb_get_serial_port_data(port);
432
Matthias Urlichsba460e42005-07-14 00:33:47 -0700433 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700434 this_urb = portdata->out_urbs[i];
435 if (this_urb && this_urb->status != -EINPROGRESS)
436 data_len += OUT_BUFLEN;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700437 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700438
439 dbg("%s: %d", __FUNCTION__, data_len);
440 return data_len;
441}
442
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700443static int option_chars_in_buffer(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700444{
445 struct option_port_private *portdata;
446 int i;
447 int data_len = 0;
448 struct urb *this_urb;
449
450 portdata = usb_get_serial_port_data(port);
451
Matthias Urlichsba460e42005-07-14 00:33:47 -0700452 for (i=0; i < N_OUT_URB; i++) {
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700453 this_urb = portdata->out_urbs[i];
454 if (this_urb && this_urb->status == -EINPROGRESS)
455 data_len += this_urb->transfer_buffer_length;
Matthias Urlichsba460e42005-07-14 00:33:47 -0700456 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700457 dbg("%s: %d", __FUNCTION__, data_len);
458 return data_len;
459}
460
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700461static int option_open(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700462{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700463 struct option_port_private *portdata;
464 struct usb_serial *serial = port->serial;
465 int i, err;
466 struct urb *urb;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700467
468 portdata = usb_get_serial_port_data(port);
469
470 dbg("%s", __FUNCTION__);
471
472 /* Set some sane defaults */
473 portdata->rts_state = 1;
474 portdata->dtr_state = 1;
475
476 /* Reset low level data toggle and start reading from endpoints */
477 for (i = 0; i < N_IN_URB; i++) {
478 urb = portdata->in_urbs[i];
479 if (! urb)
480 continue;
481 if (urb->dev != serial->dev) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700482 dbg("%s: dev %p != %p", __FUNCTION__,
483 urb->dev, serial->dev);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700484 continue;
485 }
486
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700487 /*
488 * make sure endpoint data toggle is synchronized with the
489 * device
490 */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700491 usb_clear_halt(urb->dev, urb->pipe);
492
493 err = usb_submit_urb(urb, GFP_KERNEL);
494 if (err) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700495 dbg("%s: submit urb %d failed (%d) %d",
496 __FUNCTION__, i, err,
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700497 urb->transfer_buffer_length);
498 }
499 }
500
501 /* Reset low level data toggle on out endpoints */
502 for (i = 0; i < N_OUT_URB; i++) {
503 urb = portdata->out_urbs[i];
504 if (! urb)
505 continue;
506 urb->dev = serial->dev;
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700507 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
508 usb_pipeout(urb->pipe), 0); */
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700509 }
510
511 port->tty->low_latency = 1;
512
513 option_send_setup(port);
514
515 return (0);
516}
517
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700518static inline void stop_urb(struct urb *urb)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700519{
Greg Kroah-Hartman242cf672005-07-29 16:11:07 -0400520 if (urb && urb->status == -EINPROGRESS)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700521 usb_kill_urb(urb);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700522}
523
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700524static void option_close(struct usb_serial_port *port, struct file *filp)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700525{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700526 int i;
527 struct usb_serial *serial = port->serial;
528 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700529
530 dbg("%s", __FUNCTION__);
531 portdata = usb_get_serial_port_data(port);
532
533 portdata->rts_state = 0;
534 portdata->dtr_state = 0;
535
536 if (serial->dev) {
537 option_send_setup(port);
538
539 /* Stop reading/writing urbs */
540 for (i = 0; i < N_IN_URB; i++)
541 stop_urb(portdata->in_urbs[i]);
542 for (i = 0; i < N_OUT_URB; i++)
543 stop_urb(portdata->out_urbs[i]);
544 }
545 port->tty = NULL;
546}
547
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700548/* Helper functions used by option_setup_urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700549static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
550 int dir, void *ctx, char *buf, int len,
551 void (*callback)(struct urb *, struct pt_regs *regs))
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700552{
553 struct urb *urb;
554
555 if (endpoint == -1)
556 return NULL; /* endpoint not needed */
557
558 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
559 if (urb == NULL) {
560 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
561 return NULL;
562 }
563
564 /* Fill URB using supplied data. */
565 usb_fill_bulk_urb(urb, serial->dev,
566 usb_sndbulkpipe(serial->dev, endpoint) | dir,
567 buf, len, callback, ctx);
568
569 return urb;
570}
571
572/* Setup urbs */
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700573static void option_setup_urbs(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700574{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700575 int j;
576 struct usb_serial_port *port;
577 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700578
579 dbg("%s", __FUNCTION__);
580
581 port = serial->port[0];
582 portdata = usb_get_serial_port_data(port);
583
584 /* Do indat endpoints first */
585 for (j = 0; j <= N_IN_URB; ++j) {
586 portdata->in_urbs[j] = option_setup_urb (serial,
587 port->bulk_in_endpointAddress, USB_DIR_IN, port,
588 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
589 }
590
591 /* outdat endpoints */
592 for (j = 0; j <= N_OUT_URB; ++j) {
593 portdata->out_urbs[j] = option_setup_urb (serial,
594 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
595 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
596 }
597}
598
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700599static int option_send_setup(struct usb_serial_port *port)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700600{
601 struct usb_serial *serial = port->serial;
602 struct option_port_private *portdata;
603
604 dbg("%s", __FUNCTION__);
605
606 portdata = usb_get_serial_port_data(port);
607
608 if (port->tty) {
609 int val = 0;
610 if (portdata->dtr_state)
611 val |= 0x01;
612 if (portdata->rts_state)
613 val |= 0x02;
614
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700615 return usb_control_msg(serial->dev,
616 usb_rcvctrlpipe(serial->dev, 0),
617 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700618 }
619
620 return 0;
621}
622
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700623static int option_startup(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700624{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700625 int i, err;
626 struct usb_serial_port *port;
627 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700628
629 dbg("%s", __FUNCTION__);
630
631 /* Now setup per port private data */
632 for (i = 0; i < serial->num_ports; i++) {
633 port = serial->port[i];
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100634 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700635 if (!portdata) {
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700636 dbg("%s: kmalloc for option_port_private (%d) failed!.",
637 __FUNCTION__, i);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700638 return (1);
639 }
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700640
641 usb_set_serial_port_data(port, portdata);
642
643 if (! port->interrupt_in_urb)
644 continue;
645 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
646 if (err)
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700647 dbg("%s: submit irq_in urb failed %d",
648 __FUNCTION__, err);
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700649 }
650
651 option_setup_urbs(serial);
652
653 return (0);
654}
655
Andrew Morton7bb75ae2005-07-27 01:08:30 -0700656static void option_shutdown(struct usb_serial *serial)
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700657{
Matthias Urlichsba460e42005-07-14 00:33:47 -0700658 int i, j;
659 struct usb_serial_port *port;
660 struct option_port_private *portdata;
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700661
662 dbg("%s", __FUNCTION__);
663
664 /* Stop reading/writing urbs */
665 for (i = 0; i < serial->num_ports; ++i) {
666 port = serial->port[i];
667 portdata = usb_get_serial_port_data(port);
668 for (j = 0; j < N_IN_URB; j++)
669 stop_urb(portdata->in_urbs[j]);
670 for (j = 0; j < N_OUT_URB; j++)
671 stop_urb(portdata->out_urbs[j]);
672 }
673
674 /* Now free them */
675 for (i = 0; i < serial->num_ports; ++i) {
676 port = serial->port[i];
677 portdata = usb_get_serial_port_data(port);
678
679 for (j = 0; j < N_IN_URB; j++) {
680 if (portdata->in_urbs[j]) {
681 usb_free_urb(portdata->in_urbs[j]);
682 portdata->in_urbs[j] = NULL;
683 }
684 }
685 for (j = 0; j < N_OUT_URB; j++) {
686 if (portdata->out_urbs[j]) {
687 usb_free_urb(portdata->out_urbs[j]);
688 portdata->out_urbs[j] = NULL;
689 }
690 }
691 }
692
693 /* Now free per port private data */
694 for (i = 0; i < serial->num_ports; i++) {
695 port = serial->port[i];
696 kfree(usb_get_serial_port_data(port));
697 }
698}
699
700MODULE_AUTHOR(DRIVER_AUTHOR);
701MODULE_DESCRIPTION(DRIVER_DESC);
702MODULE_VERSION(DRIVER_VERSION);
703MODULE_LICENSE("GPL");
704
Matthias Urlichsba460e42005-07-14 00:33:47 -0700705#ifdef CONFIG_USB_DEBUG
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700706module_param(debug, bool, S_IRUGO | S_IWUSR);
707MODULE_PARM_DESC(debug, "Debug messages");
Matthias Urlichsba460e42005-07-14 00:33:47 -0700708#endif
Matthias Urlichs58cfe912005-05-23 17:00:48 -0700709