1 // SPDX-License-Identifier: GPL-2.0+
3 * USB Keyspan PDA / Xircom / Entrega Converter driver
5 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 1999, 2000 Brian Warner <warner@lothar.com>
7 * Copyright (C) 2000 Al Borchers <borchers@steinerpoint.com>
9 * See Documentation/usb/usb-serial.txt for more information on using this
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/tty_flip.h>
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/workqueue.h>
23 #include <linux/uaccess.h>
24 #include <linux/usb.h>
25 #include <linux/usb/serial.h>
26 #include <linux/usb/ezusb.h>
28 /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
29 #if IS_ENABLED(CONFIG_USB_SERIAL_KEYSPAN_PDA)
34 #if IS_ENABLED(CONFIG_USB_SERIAL_XIRCOM)
40 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
41 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
43 #define KEYSPAN_TX_THRESHOLD 16
45 struct keyspan_pda_private {
48 struct work_struct unthrottle_work;
49 struct usb_serial *serial;
50 struct usb_serial_port *port;
54 #define KEYSPAN_VENDOR_ID 0x06cd
55 #define KEYSPAN_PDA_FAKE_ID 0x0103
56 #define KEYSPAN_PDA_ID 0x0104 /* no clue */
58 /* For Xircom PGSDB9 and older Entrega version of the same device */
59 #define XIRCOM_VENDOR_ID 0x085a
60 #define XIRCOM_FAKE_ID 0x8027
61 #define XIRCOM_FAKE_ID_2 0x8025 /* "PGMFHUB" serial */
62 #define ENTREGA_VENDOR_ID 0x1645
63 #define ENTREGA_FAKE_ID 0x8093
65 static const struct usb_device_id id_table_combined[] = {
67 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
70 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
71 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
72 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
74 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
75 { } /* Terminating entry */
78 MODULE_DEVICE_TABLE(usb, id_table_combined);
80 static const struct usb_device_id id_table_std[] = {
81 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
82 { } /* Terminating entry */
86 static const struct usb_device_id id_table_fake[] = {
87 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
88 { } /* Terminating entry */
93 static const struct usb_device_id id_table_fake_xircom[] = {
94 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
95 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
96 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
101 static void keyspan_pda_request_unthrottle(struct work_struct *work)
103 struct keyspan_pda_private *priv =
104 container_of(work, struct keyspan_pda_private, unthrottle_work);
105 struct usb_serial *serial = priv->serial;
108 /* ask the device to tell us when the tx buffer becomes
109 sufficiently empty */
110 result = usb_control_msg(serial->dev,
111 usb_sndctrlpipe(serial->dev, 0),
112 7, /* request_unthrottle */
113 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
115 KEYSPAN_TX_THRESHOLD,
121 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
126 static void keyspan_pda_rx_interrupt(struct urb *urb)
128 struct usb_serial_port *port = urb->context;
129 unsigned char *data = urb->transfer_buffer;
130 unsigned int len = urb->actual_length;
132 int status = urb->status;
133 struct keyspan_pda_private *priv;
136 priv = usb_get_serial_port_data(port);
145 /* this urb is terminated, clean up */
146 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
149 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
154 dev_warn(&port->dev, "short message received\n");
158 /* see if the message is data or a status interrupt */
161 /* rest of message is rx data */
164 tty_insert_flip_string(&port->port, data + 1, len - 1);
165 tty_flip_buffer_push(&port->port);
168 /* status interrupt */
170 dev_warn(&port->dev, "short interrupt message received\n");
173 dev_dbg(&port->dev, "rx int, d1=%d\n", data[1]);
175 case 1: /* modemline change */
177 case 2: /* tx unthrottle interrupt */
178 spin_lock_irqsave(&port->lock, flags);
179 priv->tx_throttled = 0;
180 priv->tx_room = max(priv->tx_room, KEYSPAN_TX_THRESHOLD);
181 spin_unlock_irqrestore(&port->lock, flags);
182 /* queue up a wakeup at scheduler time */
183 usb_serial_port_softint(port);
194 retval = usb_submit_urb(urb, GFP_ATOMIC);
197 "%s - usb_submit_urb failed with result %d\n",
202 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
204 /* stop receiving characters. We just turn off the URB request, and
205 let chars pile up in the device. If we're doing hardware
206 flowcontrol, the device will signal the other end when its buffer
207 fills up. If we're doing XON/XOFF, this would be a good time to
208 send an XOFF, although it might make sense to foist that off
209 upon the device too. */
210 struct usb_serial_port *port = tty->driver_data;
212 usb_kill_urb(port->interrupt_in_urb);
216 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
218 struct usb_serial_port *port = tty->driver_data;
219 /* just restart the receive interrupt URB */
221 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
222 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
226 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
263 bindex = 5; /* Default to 9600 */
267 /* rather than figure out how to sleep while waiting for this
268 to complete, I just use the "legacy" API. */
269 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
272 | USB_RECIP_INTERFACE
273 | USB_DIR_OUT, /* type */
285 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
287 struct usb_serial_port *port = tty->driver_data;
288 struct usb_serial *serial = port->serial;
292 if (break_state == -1)
293 value = 1; /* start break */
295 value = 0; /* clear break */
296 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
298 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
299 value, 0, NULL, 0, 2000);
301 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
303 /* there is something funky about this.. the TCSBRK that 'cu' performs
304 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
305 seconds apart, but it feels like the break sent isn't as long as it
310 static void keyspan_pda_set_termios(struct tty_struct *tty,
311 struct usb_serial_port *port, struct ktermios *old_termios)
313 struct usb_serial *serial = port->serial;
316 /* cflag specifies lots of stuff: number of stop bits, parity, number
317 of data bits, baud. What can the device actually handle?:
318 CSTOPB (1 stop bit or 2)
321 There is minimal hw support for parity (a PSW bit seems to hold the
322 parity of whatever is in the accumulator). The UART either deals
323 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
324 1 special, stop). So, with firmware changes, we could do:
326 8N2: 11 bit, extra bit always (mark?)
327 8[EOMS]1: 11 bit, extra bit is parity
328 7[EOMS]1: 10 bit, b0/b7 is parity
329 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
331 HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
334 For now, just do baud. */
336 speed = tty_get_baud_rate(tty);
337 speed = keyspan_pda_setbaud(serial, speed);
340 dev_dbg(&port->dev, "can't handle requested baud rate\n");
341 /* It hasn't changed so.. */
342 speed = tty_termios_baud_rate(old_termios);
344 /* Only speed can change so copy the old h/w parameters
345 then encode the new speed */
346 tty_termios_copy_hw(&tty->termios, old_termios);
347 tty_encode_baud_rate(tty, speed, speed);
351 /* modem control pins: DTR and RTS are outputs and can be controlled.
352 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
353 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
355 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
356 unsigned char *value)
361 data = kmalloc(1, GFP_KERNEL);
365 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
367 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
368 0, 0, data, 1, 2000);
379 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
383 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
385 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
386 value, 0, NULL, 0, 2000);
390 static int keyspan_pda_tiocmget(struct tty_struct *tty)
392 struct usb_serial_port *port = tty->driver_data;
393 struct usb_serial *serial = port->serial;
395 unsigned char status;
398 rc = keyspan_pda_get_modem_info(serial, &status);
402 ((status & (1<<7)) ? TIOCM_DTR : 0) |
403 ((status & (1<<6)) ? TIOCM_CAR : 0) |
404 ((status & (1<<5)) ? TIOCM_RNG : 0) |
405 ((status & (1<<4)) ? TIOCM_DSR : 0) |
406 ((status & (1<<3)) ? TIOCM_CTS : 0) |
407 ((status & (1<<2)) ? TIOCM_RTS : 0);
411 static int keyspan_pda_tiocmset(struct tty_struct *tty,
412 unsigned int set, unsigned int clear)
414 struct usb_serial_port *port = tty->driver_data;
415 struct usb_serial *serial = port->serial;
417 unsigned char status;
419 rc = keyspan_pda_get_modem_info(serial, &status);
428 if (clear & TIOCM_RTS)
430 if (clear & TIOCM_DTR)
432 rc = keyspan_pda_set_modem_info(serial, status);
436 static int keyspan_pda_write(struct tty_struct *tty,
437 struct usb_serial_port *port, const unsigned char *buf, int count)
439 struct usb_serial *serial = port->serial;
440 int request_unthrottle = 0;
442 struct keyspan_pda_private *priv;
445 priv = usb_get_serial_port_data(port);
446 /* guess how much room is left in the device's ring buffer, and if we
447 want to send more than that, check first, updating our notion of
448 what is left. If our write will result in no room left, ask the
449 device to give us an interrupt when the room available rises above
450 a threshold, and hold off all writers (eventually, those using
451 select() or poll() too) until we receive that unthrottle interrupt.
452 Block if we can't write anything at all, otherwise write as much as
455 dev_dbg(&port->dev, "write request of 0 bytes\n");
459 /* we might block because of:
460 the TX urb is in-flight (wait until it completes)
461 the device is full (wait until it says there is room)
463 spin_lock_irqsave(&port->lock, flags);
464 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
465 spin_unlock_irqrestore(&port->lock, flags);
468 clear_bit(0, &port->write_urbs_free);
469 spin_unlock_irqrestore(&port->lock, flags);
471 /* At this point the URB is in our control, nobody else can submit it
472 again (the only sudden transition was the one from EINPROGRESS to
473 finished). Also, the tx process is not throttled. So we are
476 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
478 /* Check if we might overrun the Tx buffer. If so, ask the
479 device how much room it really has. This is done only on
480 scheduler time, since usb_control_msg() sleeps. */
481 if (count > priv->tx_room && !in_interrupt()) {
484 room = kmalloc(1, GFP_KERNEL);
490 rc = usb_control_msg(serial->dev,
491 usb_rcvctrlpipe(serial->dev, 0),
493 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
495 0, /* value: 0 means "remaining room" */
501 dev_dbg(&port->dev, "roomquery says %d\n", *room);
502 priv->tx_room = *room;
506 dev_dbg(&port->dev, "roomquery failed\n");
510 dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
511 rc = -EIO; /* device didn't return any data */
516 if (count >= priv->tx_room) {
517 /* we're about to completely fill the Tx buffer, so
518 we'll be throttled afterwards. */
519 count = priv->tx_room;
520 request_unthrottle = 1;
524 /* now transfer data */
525 memcpy(port->write_urb->transfer_buffer, buf, count);
526 /* send the data out the bulk port */
527 port->write_urb->transfer_buffer_length = count;
529 priv->tx_room -= count;
531 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
533 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
537 /* There wasn't any room left, so we are throttled until
538 the buffer empties a bit */
539 request_unthrottle = 1;
542 if (request_unthrottle) {
543 priv->tx_throttled = 1; /* block writers */
544 schedule_work(&priv->unthrottle_work);
550 set_bit(0, &port->write_urbs_free);
555 static void keyspan_pda_write_bulk_callback(struct urb *urb)
557 struct usb_serial_port *port = urb->context;
559 set_bit(0, &port->write_urbs_free);
561 /* queue up a wakeup at scheduler time */
562 usb_serial_port_softint(port);
566 static int keyspan_pda_write_room(struct tty_struct *tty)
568 struct usb_serial_port *port = tty->driver_data;
569 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
573 spin_lock_irqsave(&port->lock, flags);
574 if (test_bit(0, &port->write_urbs_free) && !priv->tx_throttled)
575 room = priv->tx_room;
576 spin_unlock_irqrestore(&port->lock, flags);
581 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
583 struct usb_serial_port *port = tty->driver_data;
584 struct keyspan_pda_private *priv;
588 priv = usb_get_serial_port_data(port);
590 /* when throttled, return at least WAKEUP_CHARS to tell select() (via
591 n_tty.c:normal_poll() ) that we're not writeable. */
593 spin_lock_irqsave(&port->lock, flags);
594 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
596 spin_unlock_irqrestore(&port->lock, flags);
601 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
603 struct usb_serial *serial = port->serial;
606 keyspan_pda_set_modem_info(serial, (1 << 7) | (1 << 2));
608 keyspan_pda_set_modem_info(serial, 0);
612 static int keyspan_pda_open(struct tty_struct *tty,
613 struct usb_serial_port *port)
615 struct usb_serial *serial = port->serial;
618 struct keyspan_pda_private *priv;
620 /* find out how much room is in the Tx ring */
621 room = kmalloc(1, GFP_KERNEL);
625 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
627 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
635 dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
639 dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
643 priv = usb_get_serial_port_data(port);
644 priv->tx_room = *room;
645 priv->tx_throttled = *room ? 0 : 1;
647 /*Start reading from the device*/
648 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
650 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
657 static void keyspan_pda_close(struct usb_serial_port *port)
659 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
661 usb_kill_urb(port->write_urb);
662 usb_kill_urb(port->interrupt_in_urb);
664 cancel_work_sync(&priv->unthrottle_work);
668 /* download the firmware to a "fake" device (pre-renumeration) */
669 static int keyspan_pda_fake_startup(struct usb_serial *serial)
674 /* download the firmware here ... */
675 response = ezusb_fx1_set_reset(serial->dev, 1);
679 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
680 fw_name = "keyspan_pda/keyspan_pda.fw";
683 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
684 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
685 fw_name = "keyspan_pda/xircom_pgs.fw";
688 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
693 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
694 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
699 /* after downloading firmware Renumeration will occur in a
700 moment and the new device will bind to the real driver */
702 /* we want this device to fail to have a driver assigned to it. */
707 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
710 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
713 static int keyspan_pda_port_probe(struct usb_serial_port *port)
716 struct keyspan_pda_private *priv;
718 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
722 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
723 priv->serial = port->serial;
726 usb_set_serial_port_data(port, priv);
731 static int keyspan_pda_port_remove(struct usb_serial_port *port)
733 struct keyspan_pda_private *priv;
735 priv = usb_get_serial_port_data(port);
742 static struct usb_serial_driver keyspan_pda_fake_device = {
744 .owner = THIS_MODULE,
745 .name = "keyspan_pda_pre",
747 .description = "Keyspan PDA - (prerenumeration)",
748 .id_table = id_table_fake,
750 .attach = keyspan_pda_fake_startup,
755 static struct usb_serial_driver xircom_pgs_fake_device = {
757 .owner = THIS_MODULE,
758 .name = "xircom_no_firm",
760 .description = "Xircom / Entrega PGS - (prerenumeration)",
761 .id_table = id_table_fake_xircom,
763 .attach = keyspan_pda_fake_startup,
767 static struct usb_serial_driver keyspan_pda_device = {
769 .owner = THIS_MODULE,
770 .name = "keyspan_pda",
772 .description = "Keyspan PDA",
773 .id_table = id_table_std,
776 .num_interrupt_in = 1,
777 .dtr_rts = keyspan_pda_dtr_rts,
778 .open = keyspan_pda_open,
779 .close = keyspan_pda_close,
780 .write = keyspan_pda_write,
781 .write_room = keyspan_pda_write_room,
782 .write_bulk_callback = keyspan_pda_write_bulk_callback,
783 .read_int_callback = keyspan_pda_rx_interrupt,
784 .chars_in_buffer = keyspan_pda_chars_in_buffer,
785 .throttle = keyspan_pda_rx_throttle,
786 .unthrottle = keyspan_pda_rx_unthrottle,
787 .set_termios = keyspan_pda_set_termios,
788 .break_ctl = keyspan_pda_break_ctl,
789 .tiocmget = keyspan_pda_tiocmget,
790 .tiocmset = keyspan_pda_tiocmset,
791 .port_probe = keyspan_pda_port_probe,
792 .port_remove = keyspan_pda_port_remove,
795 static struct usb_serial_driver * const serial_drivers[] = {
798 &keyspan_pda_fake_device,
801 &xircom_pgs_fake_device,
806 module_usb_serial_driver(serial_drivers, id_table_combined);
808 MODULE_AUTHOR(DRIVER_AUTHOR);
809 MODULE_DESCRIPTION(DRIVER_DESC);
810 MODULE_LICENSE("GPL");