blob: dd97d8b572c336e03c7c4c282fbe7e38c44646a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mark Knibbs5f9f9752014-10-04 12:19:59 +01002 * USB Keyspan PDA / Xircom / Entrega Converter driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
23#include <linux/tty_flip.h>
24#include <linux/module.h>
25#include <linux/spinlock.h>
26#include <linux/workqueue.h>
Alan Cox9e70f312008-07-22 11:13:42 +010027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070029#include <linux/usb/serial.h>
Rene Buergelcc183e22012-09-18 09:00:41 +020030#include <linux/usb/ezusb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* make a simple define to handle if we are compiling keyspan_pda or xircom support */
33#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
34 #define KEYSPAN
35#else
36 #undef KEYSPAN
37#endif
38#if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
39 #define XIRCOM
40#else
41 #undef XIRCOM
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
45#define DRIVER_DESC "USB Keyspan PDA Converter driver"
46
47struct keyspan_pda_private {
48 int tx_room;
49 int tx_throttled;
50 struct work_struct wakeup_work;
51 struct work_struct unthrottle_work;
David Howellsc4028952006-11-22 14:57:56 +000052 struct usb_serial *serial;
53 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56
57#define KEYSPAN_VENDOR_ID 0x06cd
58#define KEYSPAN_PDA_FAKE_ID 0x0103
59#define KEYSPAN_PDA_ID 0x0104 /* no clue */
60
Mark Knibbs5f9f9752014-10-04 12:19:59 +010061/* For Xircom PGSDB9 and older Entrega version of the same device */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define XIRCOM_VENDOR_ID 0x085a
63#define XIRCOM_FAKE_ID 0x8027
Mark Knibbs5f9f9752014-10-04 12:19:59 +010064#define ENTREGA_VENDOR_ID 0x1645
65#define ENTREGA_FAKE_ID 0x8093
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Németh Márton7d40d7e2010-01-10 15:34:24 +010067static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifdef KEYSPAN
69 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
70#endif
71#ifdef XIRCOM
72 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
Mark Knibbs5f9f9752014-10-04 12:19:59 +010073 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
76 { } /* Terminating entry */
77};
78
Alan Cox9e70f312008-07-22 11:13:42 +010079MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Németh Márton7d40d7e2010-01-10 15:34:24 +010081static const struct usb_device_id id_table_std[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
83 { } /* Terminating entry */
84};
85
86#ifdef KEYSPAN
Németh Márton7d40d7e2010-01-10 15:34:24 +010087static const struct usb_device_id id_table_fake[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
89 { } /* Terminating entry */
90};
91#endif
92
93#ifdef XIRCOM
Németh Márton7d40d7e2010-01-10 15:34:24 +010094static const struct usb_device_id id_table_fake_xircom[] = {
Alan Cox9e70f312008-07-22 11:13:42 +010095 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
Mark Knibbs5f9f9752014-10-04 12:19:59 +010096 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
Alan Cox9e70f312008-07-22 11:13:42 +010097 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99#endif
100
David Howellsc4028952006-11-22 14:57:56 +0000101static void keyspan_pda_wakeup_write(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
David Howellsc4028952006-11-22 14:57:56 +0000103 struct keyspan_pda_private *priv =
104 container_of(work, struct keyspan_pda_private, wakeup_work);
105 struct usb_serial_port *port = priv->port;
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100106
107 tty_port_tty_wakeup(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
David Howellsc4028952006-11-22 14:57:56 +0000110static void keyspan_pda_request_unthrottle(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
David Howellsc4028952006-11-22 14:57:56 +0000112 struct keyspan_pda_private *priv =
113 container_of(work, struct keyspan_pda_private, unthrottle_work);
114 struct usb_serial *serial = priv->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int result;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* ask the device to tell us when the tx buffer becomes
118 sufficiently empty */
Alan Cox9e70f312008-07-22 11:13:42 +0100119 result = usb_control_msg(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 usb_sndctrlpipe(serial->dev, 0),
121 7, /* request_unthrottle */
122 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
123 | USB_DIR_OUT,
124 16, /* value: threshold */
125 0, /* index */
126 NULL,
127 0,
128 2000);
129 if (result < 0)
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700130 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
131 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134
Alan Cox9e70f312008-07-22 11:13:42 +0100135static void keyspan_pda_rx_interrupt(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Ming Leicdc97792008-02-24 18:41:47 +0800137 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700139 int retval;
140 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct keyspan_pda_private *priv;
142 priv = usb_get_serial_port_data(port);
143
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700144 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case 0:
146 /* success */
147 break;
148 case -ECONNRESET:
149 case -ENOENT:
150 case -ESHUTDOWN:
151 /* this urb is terminated, clean up */
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700152 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 default:
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700155 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto exit;
157 }
158
Alan Cox9e70f312008-07-22 11:13:42 +0100159 /* see if the message is data or a status interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 switch (data[0]) {
161 case 0:
Jiri Slabyf7d7aed2011-02-28 10:34:05 +0100162 /* rest of message is rx data */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100163 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100164 tty_insert_flip_string(&port->port, data + 1,
Alan Cox8dd03a52008-07-22 11:13:51 +0100165 urb->actual_length - 1);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100166 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 break;
169 case 1:
170 /* status interrupt */
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700171 dev_dbg(&port->dev, "rx int, d1=%d, d2=%d\n", data[1], data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 switch (data[1]) {
173 case 1: /* modemline change */
174 break;
175 case 2: /* tx unthrottle interrupt */
176 priv->tx_throttled = 0;
177 /* queue up a wakeup at scheduler time */
178 schedule_work(&priv->wakeup_work);
179 break;
180 default:
181 break;
182 }
183 break;
184 default:
185 break;
186 }
187
188exit:
Alan Cox9e70f312008-07-22 11:13:42 +0100189 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman23189ae2007-06-15 15:44:13 -0700190 if (retval)
Alan Cox4a90f092008-10-13 10:39:46 +0100191 dev_err(&port->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100192 "%s - usb_submit_urb failed with result %d\n",
Alan Cox4a90f092008-10-13 10:39:46 +0100193 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196
Alan Cox95da3102008-07-22 11:09:07 +0100197static void keyspan_pda_rx_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 /* stop receiving characters. We just turn off the URB request, and
200 let chars pile up in the device. If we're doing hardware
201 flowcontrol, the device will signal the other end when its buffer
202 fills up. If we're doing XON/XOFF, this would be a good time to
203 send an XOFF, although it might make sense to foist that off
204 upon the device too. */
Alan Cox95da3102008-07-22 11:09:07 +0100205 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman5542cf72012-05-03 16:44:23 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 usb_kill_urb(port->interrupt_in_urb);
208}
209
210
Alan Cox95da3102008-07-22 11:09:07 +0100211static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Alan Cox95da3102008-07-22 11:09:07 +0100213 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* just restart the receive interrupt URB */
Greg Kroah-Hartman5542cf72012-05-03 16:44:23 -0700215
Oliver Neukum63832512009-10-07 10:50:23 +0200216 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700217 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220
Alan Cox9e70f312008-07-22 11:13:42 +0100221static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int rc;
224 int bindex;
225
Alan Cox9e70f312008-07-22 11:13:42 +0100226 switch (baud) {
227 case 110:
228 bindex = 0;
229 break;
230 case 300:
231 bindex = 1;
232 break;
233 case 1200:
234 bindex = 2;
235 break;
236 case 2400:
237 bindex = 3;
238 break;
239 case 4800:
240 bindex = 4;
241 break;
242 case 9600:
243 bindex = 5;
244 break;
245 case 19200:
246 bindex = 6;
247 break;
248 case 38400:
249 bindex = 7;
250 break;
251 case 57600:
252 bindex = 8;
253 break;
254 case 115200:
255 bindex = 9;
256 break;
257 default:
258 bindex = 5; /* Default to 9600 */
259 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 /* rather than figure out how to sleep while waiting for this
263 to complete, I just use the "legacy" API. */
264 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
265 0, /* set baud */
Alan Cox9e70f312008-07-22 11:13:42 +0100266 USB_TYPE_VENDOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 | USB_RECIP_INTERFACE
268 | USB_DIR_OUT, /* type */
269 bindex, /* value */
270 0, /* index */
271 NULL, /* &data */
272 0, /* size */
273 2000); /* timeout */
Alan Coxe7806e32007-12-13 16:15:28 -0800274 if (rc < 0)
275 return 0;
276 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279
Alan Cox95da3102008-07-22 11:09:07 +0100280static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Alan Cox95da3102008-07-22 11:09:07 +0100282 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct usb_serial *serial = port->serial;
284 int value;
285 int result;
286
287 if (break_state == -1)
288 value = 1; /* start break */
289 else
290 value = 0; /* clear break */
291 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox9e70f312008-07-22 11:13:42 +0100292 4, /* set break */
293 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
294 value, 0, NULL, 0, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (result < 0)
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700296 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
297 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* there is something funky about this.. the TCSBRK that 'cu' performs
299 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
300 seconds apart, but it feels like the break sent isn't as long as it
301 is on /dev/ttyS0 */
302}
303
304
Alan Cox95da3102008-07-22 11:09:07 +0100305static void keyspan_pda_set_termios(struct tty_struct *tty,
306 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct usb_serial *serial = port->serial;
Alan Coxe7806e32007-12-13 16:15:28 -0800309 speed_t speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* cflag specifies lots of stuff: number of stop bits, parity, number
312 of data bits, baud. What can the device actually handle?:
313 CSTOPB (1 stop bit or 2)
314 PARENB (parity)
315 CSIZE (5bit .. 8bit)
316 There is minimal hw support for parity (a PSW bit seems to hold the
317 parity of whatever is in the accumulator). The UART either deals
318 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
319 1 special, stop). So, with firmware changes, we could do:
320 8N1: 10 bit
321 8N2: 11 bit, extra bit always (mark?)
322 8[EOMS]1: 11 bit, extra bit is parity
323 7[EOMS]1: 10 bit, b0/b7 is parity
324 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
325
Alan Coxadc8d742012-07-14 15:31:47 +0100326 HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 bit.
328
329 For now, just do baud. */
330
Alan Cox95da3102008-07-22 11:09:07 +0100331 speed = tty_get_baud_rate(tty);
Alan Coxe7806e32007-12-13 16:15:28 -0800332 speed = keyspan_pda_setbaud(serial, speed);
333
334 if (speed == 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700335 dev_dbg(&port->dev, "can't handle requested baud rate\n");
Alan Coxe7806e32007-12-13 16:15:28 -0800336 /* It hasn't changed so.. */
337 speed = tty_termios_baud_rate(old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Alan Coxe7806e32007-12-13 16:15:28 -0800339 /* Only speed can change so copy the old h/w parameters
340 then encode the new speed */
Alan Coxadc8d742012-07-14 15:31:47 +0100341 tty_termios_copy_hw(&tty->termios, old_termios);
Alan Cox95da3102008-07-22 11:09:07 +0100342 tty_encode_baud_rate(tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345
346/* modem control pins: DTR and RTS are outputs and can be controlled.
347 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
348 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
349
350static int keyspan_pda_get_modem_info(struct usb_serial *serial,
351 unsigned char *value)
352{
353 int rc;
Johan Hovoldca65d252009-12-28 23:01:51 +0100354 u8 *data;
355
356 data = kmalloc(1, GFP_KERNEL);
357 if (!data)
358 return -ENOMEM;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
361 3, /* get pins */
362 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
Johan Hovoldca65d252009-12-28 23:01:51 +0100363 0, 0, data, 1, 2000);
Benny Halevy3b36a8f2008-06-27 12:22:32 +0300364 if (rc >= 0)
Johan Hovoldca65d252009-12-28 23:01:51 +0100365 *value = *data;
366
367 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return rc;
369}
370
371
372static int keyspan_pda_set_modem_info(struct usb_serial *serial,
373 unsigned char value)
374{
375 int rc;
376 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
377 3, /* set pins */
378 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
379 value, 0, NULL, 0, 2000);
380 return rc;
381}
382
Alan Cox60b33c12011-02-14 16:26:14 +0000383static int keyspan_pda_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Alan Cox95da3102008-07-22 11:09:07 +0100385 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct usb_serial *serial = port->serial;
387 int rc;
388 unsigned char status;
389 int value;
390
391 rc = keyspan_pda_get_modem_info(serial, &status);
392 if (rc < 0)
393 return rc;
394 value =
395 ((status & (1<<7)) ? TIOCM_DTR : 0) |
396 ((status & (1<<6)) ? TIOCM_CAR : 0) |
397 ((status & (1<<5)) ? TIOCM_RNG : 0) |
398 ((status & (1<<4)) ? TIOCM_DSR : 0) |
399 ((status & (1<<3)) ? TIOCM_CTS : 0) |
400 ((status & (1<<2)) ? TIOCM_RTS : 0);
401 return value;
402}
403
Alan Cox20b9d172011-02-14 16:26:50 +0000404static int keyspan_pda_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned int set, unsigned int clear)
406{
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
412 rc = keyspan_pda_get_modem_info(serial, &status);
413 if (rc < 0)
414 return rc;
415
416 if (set & TIOCM_RTS)
417 status |= (1<<2);
418 if (set & TIOCM_DTR)
419 status |= (1<<7);
420
421 if (clear & TIOCM_RTS)
422 status &= ~(1<<2);
423 if (clear & TIOCM_DTR)
424 status &= ~(1<<7);
425 rc = keyspan_pda_set_modem_info(serial, status);
426 return rc;
427}
428
Alan Cox95da3102008-07-22 11:09:07 +0100429static int keyspan_pda_write(struct tty_struct *tty,
430 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct usb_serial *serial = port->serial;
433 int request_unthrottle = 0;
434 int rc = 0;
435 struct keyspan_pda_private *priv;
436
437 priv = usb_get_serial_port_data(port);
438 /* guess how much room is left in the device's ring buffer, and if we
439 want to send more than that, check first, updating our notion of
440 what is left. If our write will result in no room left, ask the
441 device to give us an interrupt when the room available rises above
442 a threshold, and hold off all writers (eventually, those using
443 select() or poll() too) until we receive that unthrottle interrupt.
444 Block if we can't write anything at all, otherwise write as much as
445 we can. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (count == 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700447 dev_dbg(&port->dev, "write request of 0 bytes\n");
Alan Cox9e70f312008-07-22 11:13:42 +0100448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 /* we might block because of:
452 the TX urb is in-flight (wait until it completes)
453 the device is full (wait until it says there is room)
454 */
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200455 spin_lock_bh(&port->lock);
Johan Hovoldda280e32011-11-06 19:06:24 +0100456 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200457 spin_unlock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Johan Hovoldda280e32011-11-06 19:06:24 +0100460 clear_bit(0, &port->write_urbs_free);
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200461 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* At this point the URB is in our control, nobody else can submit it
464 again (the only sudden transition was the one from EINPROGRESS to
465 finished). Also, the tx process is not throttled. So we are
466 ready to write. */
467
468 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
469
470 /* Check if we might overrun the Tx buffer. If so, ask the
471 device how much room it really has. This is done only on
472 scheduler time, since usb_control_msg() sleeps. */
473 if (count > priv->tx_room && !in_interrupt()) {
Johan Hovoldca65d252009-12-28 23:01:51 +0100474 u8 *room;
475
476 room = kmalloc(1, GFP_KERNEL);
477 if (!room) {
478 rc = -ENOMEM;
479 goto exit;
480 }
481
Alan Cox9e70f312008-07-22 11:13:42 +0100482 rc = usb_control_msg(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 usb_rcvctrlpipe(serial->dev, 0),
484 6, /* write_room */
485 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
486 | USB_DIR_IN,
487 0, /* value: 0 means "remaining room" */
488 0, /* index */
Johan Hovoldca65d252009-12-28 23:01:51 +0100489 room,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 1,
491 2000);
Johan Hovoldca65d252009-12-28 23:01:51 +0100492 if (rc > 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700493 dev_dbg(&port->dev, "roomquery says %d\n", *room);
Johan Hovoldca65d252009-12-28 23:01:51 +0100494 priv->tx_room = *room;
495 }
496 kfree(room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (rc < 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700498 dev_dbg(&port->dev, "roomquery failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto exit;
500 }
501 if (rc == 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700502 dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rc = -EIO; /* device didn't return any data */
504 goto exit;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 if (count > priv->tx_room) {
508 /* we're about to completely fill the Tx buffer, so
509 we'll be throttled afterwards. */
510 count = priv->tx_room;
511 request_unthrottle = 1;
512 }
513
514 if (count) {
515 /* now transfer data */
Alan Cox9e70f312008-07-22 11:13:42 +0100516 memcpy(port->write_urb->transfer_buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* send the data out the bulk port */
518 port->write_urb->transfer_buffer_length = count;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 priv->tx_room -= count;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
523 if (rc) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700524 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto exit;
526 }
Alan Cox9e70f312008-07-22 11:13:42 +0100527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* There wasn't any room left, so we are throttled until
529 the buffer empties a bit */
530 request_unthrottle = 1;
531 }
532
533 if (request_unthrottle) {
534 priv->tx_throttled = 1; /* block writers */
535 schedule_work(&priv->unthrottle_work);
536 }
537
538 rc = count;
539exit:
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700540 if (rc < 0)
Johan Hovoldda280e32011-11-06 19:06:24 +0100541 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return rc;
543}
544
545
Alan Cox9e70f312008-07-22 11:13:42 +0100546static void keyspan_pda_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Ming Leicdc97792008-02-24 18:41:47 +0800548 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct keyspan_pda_private *priv;
550
Johan Hovoldda280e32011-11-06 19:06:24 +0100551 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 priv = usb_get_serial_port_data(port);
553
554 /* queue up a wakeup at scheduler time */
555 schedule_work(&priv->wakeup_work);
556}
557
558
Alan Cox95da3102008-07-22 11:09:07 +0100559static int keyspan_pda_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Alan Cox95da3102008-07-22 11:09:07 +0100561 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct keyspan_pda_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* used by n_tty.c for processing of tabs and such. Giving it our
565 conservative guess is probably good enough, but needs testing by
566 running a console through the device. */
Alan Cox9e70f312008-07-22 11:13:42 +0100567 return priv->tx_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570
Alan Cox95da3102008-07-22 11:09:07 +0100571static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Alan Cox95da3102008-07-22 11:09:07 +0100573 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct keyspan_pda_private *priv;
Alan Coxa5b6f602008-04-08 17:16:06 +0100575 unsigned long flags;
576 int ret = 0;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* when throttled, return at least WAKEUP_CHARS to tell select() (via
581 n_tty.c:normal_poll() ) that we're not writeable. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100582
583 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldda280e32011-11-06 19:06:24 +0100584 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
Alan Coxa5b6f602008-04-08 17:16:06 +0100585 ret = 256;
586 spin_unlock_irqrestore(&port->lock, flags);
587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590
Alan Cox335f8512009-06-11 12:26:29 +0100591static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
592{
593 struct usb_serial *serial = port->serial;
594
Johan Hovoldd7f08452013-03-21 12:36:33 +0100595 if (on)
596 keyspan_pda_set_modem_info(serial, (1 << 7) | (1 << 2));
597 else
598 keyspan_pda_set_modem_info(serial, 0);
Alan Cox335f8512009-06-11 12:26:29 +0100599}
600
Alan Cox335f8512009-06-11 12:26:29 +0100601
Alan Cox95da3102008-07-22 11:09:07 +0100602static int keyspan_pda_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700603 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct usb_serial *serial = port->serial;
Johan Hovoldca65d252009-12-28 23:01:51 +0100606 u8 *room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int rc = 0;
608 struct keyspan_pda_private *priv;
609
610 /* find out how much room is in the Tx ring */
Johan Hovoldca65d252009-12-28 23:01:51 +0100611 room = kmalloc(1, GFP_KERNEL);
612 if (!room)
613 return -ENOMEM;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
616 6, /* write_room */
617 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
618 | USB_DIR_IN,
619 0, /* value */
620 0, /* index */
Johan Hovoldca65d252009-12-28 23:01:51 +0100621 room,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 1,
623 2000);
624 if (rc < 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700625 dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto error;
627 }
628 if (rc == 0) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700629 dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 rc = -EIO;
631 goto error;
632 }
633 priv = usb_get_serial_port_data(port);
Johan Hovoldca65d252009-12-28 23:01:51 +0100634 priv->tx_room = *room;
635 priv->tx_throttled = *room ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*Start reading from the device*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
639 if (rc) {
Greg Kroah-Hartman826575f2012-09-14 12:06:52 -0700640 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto error;
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643error:
Johan Hovoldca65d252009-12-28 23:01:51 +0100644 kfree(room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return rc;
646}
Alan Cox335f8512009-06-11 12:26:29 +0100647static void keyspan_pda_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Johan Hovold75d22b32013-03-21 12:36:34 +0100649 usb_kill_urb(port->write_urb);
650 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653
654/* download the firmware to a "fake" device (pre-renumeration) */
Alan Cox9e70f312008-07-22 11:13:42 +0100655static int keyspan_pda_fake_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int response;
David Woodhouse3edbf982008-05-30 15:15:13 +0300658 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* download the firmware here ... */
Rene Buergelcc183e22012-09-18 09:00:41 +0200661 response = ezusb_fx1_set_reset(serial->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David Woodhouse3edbf982008-05-30 15:15:13 +0300663 if (0) { ; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#ifdef KEYSPAN
David Woodhouse3edbf982008-05-30 15:15:13 +0300665 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
666 fw_name = "keyspan_pda/keyspan_pda.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
668#ifdef XIRCOM
David Woodhouse3edbf982008-05-30 15:15:13 +0300669 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
Mark Knibbs5f9f9752014-10-04 12:19:59 +0100670 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
David Woodhouse3edbf982008-05-30 15:15:13 +0300671 fw_name = "keyspan_pda/xircom_pgs.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#endif
David Woodhouse3edbf982008-05-30 15:15:13 +0300673 else {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700674 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
675 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENODEV;
677 }
Rene Buergel8d733e22012-09-18 09:02:01 +0200678
679 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700680 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
681 fw_name);
David Woodhouse3edbf982008-05-30 15:15:13 +0300682 return -ENOENT;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Rene Buergel8d733e22012-09-18 09:02:01 +0200685 /* after downloading firmware Renumeration will occur in a
686 moment and the new device will bind to the real driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* we want this device to fail to have a driver assigned to it. */
Alan Cox9e70f312008-07-22 11:13:42 +0100689 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Ben Hutchingse6c4ef92010-01-13 23:34:18 +0000692#ifdef KEYSPAN
693MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
694#endif
695#ifdef XIRCOM
696MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
697#endif
698
Johan Hovold94ab71c2012-10-17 13:35:00 +0200699static int keyspan_pda_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701
702 struct keyspan_pda_private *priv;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
705 if (!priv)
Johan Hovold94ab71c2012-10-17 13:35:00 +0200706 return -ENOMEM;
707
David Howellsc4028952006-11-22 14:57:56 +0000708 INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
709 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
Johan Hovold94ab71c2012-10-17 13:35:00 +0200710 priv->serial = port->serial;
711 priv->port = port;
712
713 usb_set_serial_port_data(port, priv);
714
Alan Cox9e70f312008-07-22 11:13:42 +0100715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Johan Hovold94ab71c2012-10-17 13:35:00 +0200718static int keyspan_pda_port_remove(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Johan Hovold94ab71c2012-10-17 13:35:00 +0200720 struct keyspan_pda_private *priv;
721
722 priv = usb_get_serial_port_data(port);
723 kfree(priv);
724
725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728#ifdef KEYSPAN
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700729static struct usb_serial_driver keyspan_pda_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700730 .driver = {
731 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700732 .name = "keyspan_pda_pre",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700733 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700734 .description = "Keyspan PDA - (prerenumeration)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .id_table = id_table_fake,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 .num_ports = 1,
737 .attach = keyspan_pda_fake_startup,
738};
739#endif
740
741#ifdef XIRCOM
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700742static struct usb_serial_driver xircom_pgs_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700743 .driver = {
744 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700745 .name = "xircom_no_firm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700746 },
Mark Knibbs5f9f9752014-10-04 12:19:59 +0100747 .description = "Xircom / Entrega PGS - (prerenumeration)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .id_table = id_table_fake_xircom,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .num_ports = 1,
750 .attach = keyspan_pda_fake_startup,
751};
752#endif
753
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700754static struct usb_serial_driver keyspan_pda_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700755 .driver = {
756 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700757 .name = "keyspan_pda",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700758 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700759 .description = "Keyspan PDA",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .num_ports = 1,
Alan Cox335f8512009-06-11 12:26:29 +0100762 .dtr_rts = keyspan_pda_dtr_rts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 .open = keyspan_pda_open,
764 .close = keyspan_pda_close,
765 .write = keyspan_pda_write,
766 .write_room = keyspan_pda_write_room,
767 .write_bulk_callback = keyspan_pda_write_bulk_callback,
768 .read_int_callback = keyspan_pda_rx_interrupt,
769 .chars_in_buffer = keyspan_pda_chars_in_buffer,
770 .throttle = keyspan_pda_rx_throttle,
771 .unthrottle = keyspan_pda_rx_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 .set_termios = keyspan_pda_set_termios,
773 .break_ctl = keyspan_pda_break_ctl,
774 .tiocmget = keyspan_pda_tiocmget,
775 .tiocmset = keyspan_pda_tiocmset,
Johan Hovold94ab71c2012-10-17 13:35:00 +0200776 .port_probe = keyspan_pda_port_probe,
777 .port_remove = keyspan_pda_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
Greg Kroah-Hartman8c49fc92012-02-24 12:49:34 -0800780static struct usb_serial_driver * const serial_drivers[] = {
781 &keyspan_pda_device,
782#ifdef KEYSPAN
783 &keyspan_pda_fake_device,
784#endif
785#ifdef XIRCOM
786 &xircom_pgs_fake_device,
787#endif
788 NULL
789};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700791module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Alan Cox9e70f312008-07-22 11:13:42 +0100793MODULE_AUTHOR(DRIVER_AUTHOR);
794MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795MODULE_LICENSE("GPL");