blob: 341ae504915a0b6ae892b6a5bd129a3c67237f57 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Keyspan PDA / Xircom / Entregra Converter driver
3 *
4 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 1999, 2000 Brian Warner <warner@lothar.com>
6 * Copyright (C) 2000 Al Borchers <borchers@steinerpoint.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
Alan Cox9e70f312008-07-22 11:13:42 +010013 * See Documentation/usb/usb-serial.txt for more information on using this
14 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/tty.h>
23#include <linux/tty_driver.h>
24#include <linux/tty_flip.h>
25#include <linux/module.h>
26#include <linux/spinlock.h>
27#include <linux/workqueue.h>
David Woodhouse3edbf982008-05-30 15:15:13 +030028#include <linux/firmware.h>
29#include <linux/ihex.h>
Alan Cox9e70f312008-07-22 11:13:42 +010030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070032#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Rusty Russell90ab5ee2012-01-13 09:32:20 +103034static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* make a simple define to handle if we are compiling keyspan_pda or xircom support */
37#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
38 #define KEYSPAN
39#else
40 #undef KEYSPAN
41#endif
42#if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
43 #define XIRCOM
44#else
45 #undef XIRCOM
46#endif
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Version Information
50 */
51#define DRIVER_VERSION "v1.1"
52#define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
53#define DRIVER_DESC "USB Keyspan PDA Converter driver"
54
55struct keyspan_pda_private {
56 int tx_room;
57 int tx_throttled;
58 struct work_struct wakeup_work;
59 struct work_struct unthrottle_work;
David Howellsc4028952006-11-22 14:57:56 +000060 struct usb_serial *serial;
61 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64
65#define KEYSPAN_VENDOR_ID 0x06cd
66#define KEYSPAN_PDA_FAKE_ID 0x0103
67#define KEYSPAN_PDA_ID 0x0104 /* no clue */
68
69/* For Xircom PGSDB9 and older Entregra version of the same device */
70#define XIRCOM_VENDOR_ID 0x085a
71#define XIRCOM_FAKE_ID 0x8027
72#define ENTREGRA_VENDOR_ID 0x1645
73#define ENTREGRA_FAKE_ID 0x8093
74
Németh Márton7d40d7e2010-01-10 15:34:24 +010075static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#ifdef KEYSPAN
77 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
78#endif
79#ifdef XIRCOM
80 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
81 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
82#endif
83 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
84 { } /* Terminating entry */
85};
86
Alan Cox9e70f312008-07-22 11:13:42 +010087MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static struct usb_driver keyspan_pda_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .name = "keyspan_pda",
91 .probe = usb_serial_probe,
92 .disconnect = usb_serial_disconnect,
93 .id_table = id_table_combined,
94};
95
Németh Márton7d40d7e2010-01-10 15:34:24 +010096static const struct usb_device_id id_table_std[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
98 { } /* Terminating entry */
99};
100
101#ifdef KEYSPAN
Németh Márton7d40d7e2010-01-10 15:34:24 +0100102static const struct usb_device_id id_table_fake[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
104 { } /* Terminating entry */
105};
106#endif
107
108#ifdef XIRCOM
Németh Márton7d40d7e2010-01-10 15:34:24 +0100109static const struct usb_device_id id_table_fake_xircom[] = {
Alan Cox9e70f312008-07-22 11:13:42 +0100110 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
111 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
112 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114#endif
115
David Howellsc4028952006-11-22 14:57:56 +0000116static void keyspan_pda_wakeup_write(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
David Howellsc4028952006-11-22 14:57:56 +0000118 struct keyspan_pda_private *priv =
119 container_of(work, struct keyspan_pda_private, wakeup_work);
120 struct usb_serial_port *port = priv->port;
Alan Cox4a90f092008-10-13 10:39:46 +0100121 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100122 if (tty)
123 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100124 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
David Howellsc4028952006-11-22 14:57:56 +0000127static void keyspan_pda_request_unthrottle(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
David Howellsc4028952006-11-22 14:57:56 +0000129 struct keyspan_pda_private *priv =
130 container_of(work, struct keyspan_pda_private, unthrottle_work);
131 struct usb_serial *serial = priv->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int result;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* ask the device to tell us when the tx buffer becomes
135 sufficiently empty */
Alan Cox9e70f312008-07-22 11:13:42 +0100136 result = usb_control_msg(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 usb_sndctrlpipe(serial->dev, 0),
138 7, /* request_unthrottle */
139 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
140 | USB_DIR_OUT,
141 16, /* value: threshold */
142 0, /* index */
143 NULL,
144 0,
145 2000);
146 if (result < 0)
Alan Cox9e70f312008-07-22 11:13:42 +0100147 dbg("%s - error %d from usb_control_msg",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800148 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151
Alan Cox9e70f312008-07-22 11:13:42 +0100152static void keyspan_pda_rx_interrupt(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Ming Leicdc97792008-02-24 18:41:47 +0800154 struct usb_serial_port *port = urb->context;
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100155 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700157 int retval;
158 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct keyspan_pda_private *priv;
160 priv = usb_get_serial_port_data(port);
161
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700162 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case 0:
164 /* success */
165 break;
166 case -ECONNRESET:
167 case -ENOENT:
168 case -ESHUTDOWN:
169 /* this urb is terminated, clean up */
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700170 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800171 __func__, status);
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 default:
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700174 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800175 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto exit;
177 }
178
Alan Cox9e70f312008-07-22 11:13:42 +0100179 /* see if the message is data or a status interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 switch (data[0]) {
181 case 0:
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100182 tty = tty_port_tty_get(&port->port);
183 /* rest of message is rx data */
184 if (tty && urb->actual_length) {
Alan Cox8dd03a52008-07-22 11:13:51 +0100185 tty_insert_flip_string(tty, data + 1,
186 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 tty_flip_buffer_push(tty);
188 }
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100189 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
191 case 1:
192 /* status interrupt */
193 dbg(" rx int, d1=%d, d2=%d", data[1], data[2]);
194 switch (data[1]) {
195 case 1: /* modemline change */
196 break;
197 case 2: /* tx unthrottle interrupt */
198 priv->tx_throttled = 0;
199 /* queue up a wakeup at scheduler time */
200 schedule_work(&priv->wakeup_work);
201 break;
202 default:
203 break;
204 }
205 break;
206 default:
207 break;
208 }
209
210exit:
Alan Cox9e70f312008-07-22 11:13:42 +0100211 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700212 if (retval)
Alan Cox4a90f092008-10-13 10:39:46 +0100213 dev_err(&port->dev,
214 "%s - usb_submit_urb failed with result %d",
215 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218
Alan Cox95da3102008-07-22 11:09:07 +0100219static void keyspan_pda_rx_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 /* stop receiving characters. We just turn off the URB request, and
222 let chars pile up in the device. If we're doing hardware
223 flowcontrol, the device will signal the other end when its buffer
224 fills up. If we're doing XON/XOFF, this would be a good time to
225 send an XOFF, although it might make sense to foist that off
226 upon the device too. */
Alan Cox95da3102008-07-22 11:09:07 +0100227 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman5542cf72012-05-03 16:44:23 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 usb_kill_urb(port->interrupt_in_urb);
230}
231
232
Alan Cox95da3102008-07-22 11:09:07 +0100233static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Alan Cox95da3102008-07-22 11:09:07 +0100235 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* just restart the receive interrupt URB */
Greg Kroah-Hartman5542cf72012-05-03 16:44:23 -0700237
Oliver Neukum63832512009-10-07 10:50:23 +0200238 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dbg(" usb_submit_urb(read urb) failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
Alan Cox9e70f312008-07-22 11:13:42 +0100243static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int rc;
246 int bindex;
247
Alan Cox9e70f312008-07-22 11:13:42 +0100248 switch (baud) {
249 case 110:
250 bindex = 0;
251 break;
252 case 300:
253 bindex = 1;
254 break;
255 case 1200:
256 bindex = 2;
257 break;
258 case 2400:
259 bindex = 3;
260 break;
261 case 4800:
262 bindex = 4;
263 break;
264 case 9600:
265 bindex = 5;
266 break;
267 case 19200:
268 bindex = 6;
269 break;
270 case 38400:
271 bindex = 7;
272 break;
273 case 57600:
274 bindex = 8;
275 break;
276 case 115200:
277 bindex = 9;
278 break;
279 default:
280 bindex = 5; /* Default to 9600 */
281 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 /* rather than figure out how to sleep while waiting for this
285 to complete, I just use the "legacy" API. */
286 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
287 0, /* set baud */
Alan Cox9e70f312008-07-22 11:13:42 +0100288 USB_TYPE_VENDOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 | USB_RECIP_INTERFACE
290 | USB_DIR_OUT, /* type */
291 bindex, /* value */
292 0, /* index */
293 NULL, /* &data */
294 0, /* size */
295 2000); /* timeout */
Alan Coxe7806e32007-12-13 16:15:28 -0800296 if (rc < 0)
297 return 0;
298 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301
Alan Cox95da3102008-07-22 11:09:07 +0100302static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Alan Cox95da3102008-07-22 11:09:07 +0100304 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct usb_serial *serial = port->serial;
306 int value;
307 int result;
308
309 if (break_state == -1)
310 value = 1; /* start break */
311 else
312 value = 0; /* clear break */
313 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox9e70f312008-07-22 11:13:42 +0100314 4, /* set break */
315 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
316 value, 0, NULL, 0, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (result < 0)
Alan Cox9e70f312008-07-22 11:13:42 +0100318 dbg("%s - error %d from usb_control_msg",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800319 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* there is something funky about this.. the TCSBRK that 'cu' performs
321 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
322 seconds apart, but it feels like the break sent isn't as long as it
323 is on /dev/ttyS0 */
324}
325
326
Alan Cox95da3102008-07-22 11:09:07 +0100327static void keyspan_pda_set_termios(struct tty_struct *tty,
328 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct usb_serial *serial = port->serial;
Alan Coxe7806e32007-12-13 16:15:28 -0800331 speed_t speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* cflag specifies lots of stuff: number of stop bits, parity, number
334 of data bits, baud. What can the device actually handle?:
335 CSTOPB (1 stop bit or 2)
336 PARENB (parity)
337 CSIZE (5bit .. 8bit)
338 There is minimal hw support for parity (a PSW bit seems to hold the
339 parity of whatever is in the accumulator). The UART either deals
340 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
341 1 special, stop). So, with firmware changes, we could do:
342 8N1: 10 bit
343 8N2: 11 bit, extra bit always (mark?)
344 8[EOMS]1: 11 bit, extra bit is parity
345 7[EOMS]1: 10 bit, b0/b7 is parity
346 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
347
348 HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS
349 bit.
350
351 For now, just do baud. */
352
Alan Cox95da3102008-07-22 11:09:07 +0100353 speed = tty_get_baud_rate(tty);
Alan Coxe7806e32007-12-13 16:15:28 -0800354 speed = keyspan_pda_setbaud(serial, speed);
355
356 if (speed == 0) {
357 dbg("can't handle requested baud rate");
358 /* It hasn't changed so.. */
359 speed = tty_termios_baud_rate(old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Alan Coxe7806e32007-12-13 16:15:28 -0800361 /* Only speed can change so copy the old h/w parameters
362 then encode the new speed */
Alan Cox95da3102008-07-22 11:09:07 +0100363 tty_termios_copy_hw(tty->termios, old_termios);
364 tty_encode_baud_rate(tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367
368/* modem control pins: DTR and RTS are outputs and can be controlled.
369 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
370 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
371
372static int keyspan_pda_get_modem_info(struct usb_serial *serial,
373 unsigned char *value)
374{
375 int rc;
Johan Hovoldca65d252009-12-28 23:01:51 +0100376 u8 *data;
377
378 data = kmalloc(1, GFP_KERNEL);
379 if (!data)
380 return -ENOMEM;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
383 3, /* get pins */
384 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
Johan Hovoldca65d252009-12-28 23:01:51 +0100385 0, 0, data, 1, 2000);
Benny Halevy3b36a8f2008-06-27 12:22:32 +0300386 if (rc >= 0)
Johan Hovoldca65d252009-12-28 23:01:51 +0100387 *value = *data;
388
389 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return rc;
391}
392
393
394static int keyspan_pda_set_modem_info(struct usb_serial *serial,
395 unsigned char value)
396{
397 int rc;
398 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
399 3, /* set pins */
400 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
401 value, 0, NULL, 0, 2000);
402 return rc;
403}
404
Alan Cox60b33c12011-02-14 16:26:14 +0000405static int keyspan_pda_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Alan Cox95da3102008-07-22 11:09:07 +0100407 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct usb_serial *serial = port->serial;
409 int rc;
410 unsigned char status;
411 int value;
412
413 rc = keyspan_pda_get_modem_info(serial, &status);
414 if (rc < 0)
415 return rc;
416 value =
417 ((status & (1<<7)) ? TIOCM_DTR : 0) |
418 ((status & (1<<6)) ? TIOCM_CAR : 0) |
419 ((status & (1<<5)) ? TIOCM_RNG : 0) |
420 ((status & (1<<4)) ? TIOCM_DSR : 0) |
421 ((status & (1<<3)) ? TIOCM_CTS : 0) |
422 ((status & (1<<2)) ? TIOCM_RTS : 0);
423 return value;
424}
425
Alan Cox20b9d172011-02-14 16:26:50 +0000426static int keyspan_pda_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned int set, unsigned int clear)
428{
Alan Cox95da3102008-07-22 11:09:07 +0100429 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct usb_serial *serial = port->serial;
431 int rc;
432 unsigned char status;
433
434 rc = keyspan_pda_get_modem_info(serial, &status);
435 if (rc < 0)
436 return rc;
437
438 if (set & TIOCM_RTS)
439 status |= (1<<2);
440 if (set & TIOCM_DTR)
441 status |= (1<<7);
442
443 if (clear & TIOCM_RTS)
444 status &= ~(1<<2);
445 if (clear & TIOCM_DTR)
446 status &= ~(1<<7);
447 rc = keyspan_pda_set_modem_info(serial, status);
448 return rc;
449}
450
Alan Cox95da3102008-07-22 11:09:07 +0100451static int keyspan_pda_write(struct tty_struct *tty,
452 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct usb_serial *serial = port->serial;
455 int request_unthrottle = 0;
456 int rc = 0;
457 struct keyspan_pda_private *priv;
458
459 priv = usb_get_serial_port_data(port);
460 /* guess how much room is left in the device's ring buffer, and if we
461 want to send more than that, check first, updating our notion of
462 what is left. If our write will result in no room left, ask the
463 device to give us an interrupt when the room available rises above
464 a threshold, and hold off all writers (eventually, those using
465 select() or poll() too) until we receive that unthrottle interrupt.
466 Block if we can't write anything at all, otherwise write as much as
467 we can. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (count == 0) {
469 dbg(" write request of 0 bytes");
Alan Cox9e70f312008-07-22 11:13:42 +0100470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 /* we might block because of:
474 the TX urb is in-flight (wait until it completes)
475 the device is full (wait until it says there is room)
476 */
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200477 spin_lock_bh(&port->lock);
Johan Hovoldda280e32011-11-06 19:06:24 +0100478 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200479 spin_unlock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Johan Hovoldda280e32011-11-06 19:06:24 +0100482 clear_bit(0, &port->write_urbs_free);
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200483 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* At this point the URB is in our control, nobody else can submit it
486 again (the only sudden transition was the one from EINPROGRESS to
487 finished). Also, the tx process is not throttled. So we are
488 ready to write. */
489
490 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
491
492 /* Check if we might overrun the Tx buffer. If so, ask the
493 device how much room it really has. This is done only on
494 scheduler time, since usb_control_msg() sleeps. */
495 if (count > priv->tx_room && !in_interrupt()) {
Johan Hovoldca65d252009-12-28 23:01:51 +0100496 u8 *room;
497
498 room = kmalloc(1, GFP_KERNEL);
499 if (!room) {
500 rc = -ENOMEM;
501 goto exit;
502 }
503
Alan Cox9e70f312008-07-22 11:13:42 +0100504 rc = usb_control_msg(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 usb_rcvctrlpipe(serial->dev, 0),
506 6, /* write_room */
507 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
508 | USB_DIR_IN,
509 0, /* value: 0 means "remaining room" */
510 0, /* index */
Johan Hovoldca65d252009-12-28 23:01:51 +0100511 room,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 1,
513 2000);
Johan Hovoldca65d252009-12-28 23:01:51 +0100514 if (rc > 0) {
515 dbg(" roomquery says %d", *room);
516 priv->tx_room = *room;
517 }
518 kfree(room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (rc < 0) {
520 dbg(" roomquery failed");
521 goto exit;
522 }
523 if (rc == 0) {
524 dbg(" roomquery returned 0 bytes");
525 rc = -EIO; /* device didn't return any data */
526 goto exit;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 if (count > priv->tx_room) {
530 /* we're about to completely fill the Tx buffer, so
531 we'll be throttled afterwards. */
532 count = priv->tx_room;
533 request_unthrottle = 1;
534 }
535
536 if (count) {
537 /* now transfer data */
Alan Cox9e70f312008-07-22 11:13:42 +0100538 memcpy(port->write_urb->transfer_buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* send the data out the bulk port */
540 port->write_urb->transfer_buffer_length = count;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 priv->tx_room -= count;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
545 if (rc) {
546 dbg(" usb_submit_urb(write bulk) failed");
547 goto exit;
548 }
Alan Cox9e70f312008-07-22 11:13:42 +0100549 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* There wasn't any room left, so we are throttled until
551 the buffer empties a bit */
552 request_unthrottle = 1;
553 }
554
555 if (request_unthrottle) {
556 priv->tx_throttled = 1; /* block writers */
557 schedule_work(&priv->unthrottle_work);
558 }
559
560 rc = count;
561exit:
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700562 if (rc < 0)
Johan Hovoldda280e32011-11-06 19:06:24 +0100563 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return rc;
565}
566
567
Alan Cox9e70f312008-07-22 11:13:42 +0100568static void keyspan_pda_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Ming Leicdc97792008-02-24 18:41:47 +0800570 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct keyspan_pda_private *priv;
572
Johan Hovoldda280e32011-11-06 19:06:24 +0100573 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 priv = usb_get_serial_port_data(port);
575
576 /* queue up a wakeup at scheduler time */
577 schedule_work(&priv->wakeup_work);
578}
579
580
Alan Cox95da3102008-07-22 11:09:07 +0100581static int keyspan_pda_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Alan Cox95da3102008-07-22 11:09:07 +0100583 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct keyspan_pda_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* used by n_tty.c for processing of tabs and such. Giving it our
587 conservative guess is probably good enough, but needs testing by
588 running a console through the device. */
Alan Cox9e70f312008-07-22 11:13:42 +0100589 return priv->tx_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592
Alan Cox95da3102008-07-22 11:09:07 +0100593static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Alan Cox95da3102008-07-22 11:09:07 +0100595 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct keyspan_pda_private *priv;
Alan Coxa5b6f602008-04-08 17:16:06 +0100597 unsigned long flags;
598 int ret = 0;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* when throttled, return at least WAKEUP_CHARS to tell select() (via
603 n_tty.c:normal_poll() ) that we're not writeable. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100604
605 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldda280e32011-11-06 19:06:24 +0100606 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
Alan Coxa5b6f602008-04-08 17:16:06 +0100607 ret = 256;
608 spin_unlock_irqrestore(&port->lock, flags);
609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612
Alan Cox335f8512009-06-11 12:26:29 +0100613static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
614{
615 struct usb_serial *serial = port->serial;
616
617 if (serial->dev) {
618 if (on)
619 keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2));
620 else
621 keyspan_pda_set_modem_info(serial, 0);
622 }
623}
624
Alan Cox335f8512009-06-11 12:26:29 +0100625
Alan Cox95da3102008-07-22 11:09:07 +0100626static int keyspan_pda_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700627 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct usb_serial *serial = port->serial;
Johan Hovoldca65d252009-12-28 23:01:51 +0100630 u8 *room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int rc = 0;
632 struct keyspan_pda_private *priv;
633
634 /* find out how much room is in the Tx ring */
Johan Hovoldca65d252009-12-28 23:01:51 +0100635 room = kmalloc(1, GFP_KERNEL);
636 if (!room)
637 return -ENOMEM;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
640 6, /* write_room */
641 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
642 | USB_DIR_IN,
643 0, /* value */
644 0, /* index */
Johan Hovoldca65d252009-12-28 23:01:51 +0100645 room,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 1,
647 2000);
648 if (rc < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800649 dbg("%s - roomquery failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto error;
651 }
652 if (rc == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dbg("%s - roomquery returned 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 rc = -EIO;
655 goto error;
656 }
657 priv = usb_get_serial_port_data(port);
Johan Hovoldca65d252009-12-28 23:01:51 +0100658 priv->tx_room = *room;
659 priv->tx_throttled = *room ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*Start reading from the device*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
663 if (rc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800664 dbg("%s - usb_submit_urb(read int) failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto error;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667error:
Johan Hovoldca65d252009-12-28 23:01:51 +0100668 kfree(room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return rc;
670}
Alan Cox335f8512009-06-11 12:26:29 +0100671static void keyspan_pda_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct usb_serial *serial = port->serial;
674
675 if (serial->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* shutdown our bulk reads and writes */
677 usb_kill_urb(port->write_urb);
678 usb_kill_urb(port->interrupt_in_urb);
679 }
680}
681
682
683/* download the firmware to a "fake" device (pre-renumeration) */
Alan Cox9e70f312008-07-22 11:13:42 +0100684static int keyspan_pda_fake_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int response;
David Woodhouse3edbf982008-05-30 15:15:13 +0300687 const char *fw_name;
688 const struct ihex_binrec *record;
689 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /* download the firmware here ... */
692 response = ezusb_set_reset(serial, 1);
693
David Woodhouse3edbf982008-05-30 15:15:13 +0300694 if (0) { ; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#ifdef KEYSPAN
David Woodhouse3edbf982008-05-30 15:15:13 +0300696 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
697 fw_name = "keyspan_pda/keyspan_pda.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif
699#ifdef XIRCOM
David Woodhouse3edbf982008-05-30 15:15:13 +0300700 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
701 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID))
702 fw_name = "keyspan_pda/xircom_pgs.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#endif
David Woodhouse3edbf982008-05-30 15:15:13 +0300704 else {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700705 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
706 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -ENODEV;
708 }
David Woodhouse3edbf982008-05-30 15:15:13 +0300709 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700710 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
711 fw_name);
David Woodhouse3edbf982008-05-30 15:15:13 +0300712 return -ENOENT;
713 }
714 record = (const struct ihex_binrec *)fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
David Woodhouse3edbf982008-05-30 15:15:13 +0300716 while (record) {
717 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 (unsigned char *)record->data,
David Woodhouse3edbf982008-05-30 15:15:13 +0300719 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700721 dev_err(&serial->dev->dev, "ezusb_writememory failed "
722 "for Keyspan PDA firmware (%d %04X %p %d)\n",
723 response, be32_to_cpu(record->addr),
724 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
726 }
David Woodhouse3edbf982008-05-30 15:15:13 +0300727 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
David Woodhouse3edbf982008-05-30 15:15:13 +0300729 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* bring device out of reset. Renumeration will occur in a moment
731 and the new device will bind to the real driver */
732 response = ezusb_set_reset(serial, 0);
733
734 /* we want this device to fail to have a driver assigned to it. */
Alan Cox9e70f312008-07-22 11:13:42 +0100735 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Ben Hutchingse6c4ef92010-01-13 23:34:18 +0000738#ifdef KEYSPAN
739MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
740#endif
741#ifdef XIRCOM
742MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
743#endif
744
Alan Cox9e70f312008-07-22 11:13:42 +0100745static int keyspan_pda_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747
748 struct keyspan_pda_private *priv;
749
750 /* allocate the private data structures for all ports. Well, for all
751 one ports. */
752
753 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
754 if (!priv)
Alan Cox9e70f312008-07-22 11:13:42 +0100755 return 1; /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 usb_set_serial_port_data(serial->port[0], priv);
757 init_waitqueue_head(&serial->port[0]->write_wait);
David Howellsc4028952006-11-22 14:57:56 +0000758 INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
759 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
760 priv->serial = serial;
761 priv->port = serial->port[0];
Alan Cox9e70f312008-07-22 11:13:42 +0100762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Alan Sternf9c99bb2009-06-02 11:53:55 -0400765static void keyspan_pda_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 kfree(usb_get_serial_port_data(serial->port[0]));
768}
769
770#ifdef KEYSPAN
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700771static struct usb_serial_driver keyspan_pda_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700772 .driver = {
773 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700774 .name = "keyspan_pda_pre",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700775 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700776 .description = "Keyspan PDA - (prerenumeration)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .id_table = id_table_fake,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .num_ports = 1,
779 .attach = keyspan_pda_fake_startup,
780};
781#endif
782
783#ifdef XIRCOM
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700784static struct usb_serial_driver xircom_pgs_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700785 .driver = {
786 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700787 .name = "xircom_no_firm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700788 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700789 .description = "Xircom / Entregra PGS - (prerenumeration)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 .id_table = id_table_fake_xircom,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .num_ports = 1,
792 .attach = keyspan_pda_fake_startup,
793};
794#endif
795
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700796static struct usb_serial_driver keyspan_pda_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700797 .driver = {
798 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700799 .name = "keyspan_pda",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700800 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700801 .description = "Keyspan PDA",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 .num_ports = 1,
Alan Cox335f8512009-06-11 12:26:29 +0100804 .dtr_rts = keyspan_pda_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .open = keyspan_pda_open,
806 .close = keyspan_pda_close,
807 .write = keyspan_pda_write,
808 .write_room = keyspan_pda_write_room,
809 .write_bulk_callback = keyspan_pda_write_bulk_callback,
810 .read_int_callback = keyspan_pda_rx_interrupt,
811 .chars_in_buffer = keyspan_pda_chars_in_buffer,
812 .throttle = keyspan_pda_rx_throttle,
813 .unthrottle = keyspan_pda_rx_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 .set_termios = keyspan_pda_set_termios,
815 .break_ctl = keyspan_pda_break_ctl,
816 .tiocmget = keyspan_pda_tiocmget,
817 .tiocmset = keyspan_pda_tiocmset,
818 .attach = keyspan_pda_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400819 .release = keyspan_pda_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
Greg Kroah-Hartman8c49fc92012-02-24 12:49:34 -0800822static struct usb_serial_driver * const serial_drivers[] = {
823 &keyspan_pda_device,
824#ifdef KEYSPAN
825 &keyspan_pda_fake_device,
826#endif
827#ifdef XIRCOM
828 &xircom_pgs_fake_device,
829#endif
830 NULL
831};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Greg Kroah-Hartmand40f5b02012-02-28 13:11:59 -0800833module_usb_serial_driver(keyspan_pda_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Alan Cox9e70f312008-07-22 11:13:42 +0100835MODULE_AUTHOR(DRIVER_AUTHOR);
836MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837MODULE_LICENSE("GPL");
838
839module_param(debug, bool, S_IRUGO | S_IWUSR);
840MODULE_PARM_DESC(debug, "Debug enabled or not");