2 * USB Keyspan PDA / Xircom / Entregra Converter driver
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>
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.
13 * See Documentation/usb/usb-serial.txt for more information on using this
18 #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>
28 #include <linux/firmware.h>
29 #include <linux/ihex.h>
30 #include <linux/uaccess.h>
31 #include <linux/usb.h>
32 #include <linux/usb/serial.h>
34 /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
35 #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
40 #if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
49 #define DRIVER_VERSION "v1.1"
50 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
51 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
53 struct keyspan_pda_private {
56 struct work_struct wakeup_work;
57 struct work_struct unthrottle_work;
58 struct usb_serial *serial;
59 struct usb_serial_port *port;
63 #define KEYSPAN_VENDOR_ID 0x06cd
64 #define KEYSPAN_PDA_FAKE_ID 0x0103
65 #define KEYSPAN_PDA_ID 0x0104 /* no clue */
67 /* For Xircom PGSDB9 and older Entregra version of the same device */
68 #define XIRCOM_VENDOR_ID 0x085a
69 #define XIRCOM_FAKE_ID 0x8027
70 #define ENTREGRA_VENDOR_ID 0x1645
71 #define ENTREGRA_FAKE_ID 0x8093
73 static const struct usb_device_id id_table_combined[] = {
75 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
78 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
79 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
81 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
82 { } /* Terminating entry */
85 MODULE_DEVICE_TABLE(usb, id_table_combined);
87 static const struct usb_device_id id_table_std[] = {
88 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
89 { } /* Terminating entry */
93 static const struct usb_device_id id_table_fake[] = {
94 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
95 { } /* Terminating entry */
100 static const struct usb_device_id id_table_fake_xircom[] = {
101 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
102 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
107 static void keyspan_pda_wakeup_write(struct work_struct *work)
109 struct keyspan_pda_private *priv =
110 container_of(work, struct keyspan_pda_private, wakeup_work);
111 struct usb_serial_port *port = priv->port;
112 struct tty_struct *tty = tty_port_tty_get(&port->port);
118 static void keyspan_pda_request_unthrottle(struct work_struct *work)
120 struct keyspan_pda_private *priv =
121 container_of(work, struct keyspan_pda_private, unthrottle_work);
122 struct usb_serial *serial = priv->serial;
125 /* ask the device to tell us when the tx buffer becomes
126 sufficiently empty */
127 result = usb_control_msg(serial->dev,
128 usb_sndctrlpipe(serial->dev, 0),
129 7, /* request_unthrottle */
130 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
132 16, /* value: threshold */
138 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
143 static void keyspan_pda_rx_interrupt(struct urb *urb)
145 struct usb_serial_port *port = urb->context;
146 struct tty_struct *tty;
147 unsigned char *data = urb->transfer_buffer;
149 int status = urb->status;
150 struct keyspan_pda_private *priv;
151 priv = usb_get_serial_port_data(port);
160 /* this urb is terminated, clean up */
161 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
164 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
168 /* see if the message is data or a status interrupt */
171 tty = tty_port_tty_get(&port->port);
172 /* rest of message is rx data */
173 if (tty && urb->actual_length) {
174 tty_insert_flip_string(tty, data + 1,
175 urb->actual_length - 1);
176 tty_flip_buffer_push(tty);
181 /* status interrupt */
182 dev_dbg(&port->dev, "rx int, d1=%d, d2=%d\n", data[1], data[2]);
184 case 1: /* modemline change */
186 case 2: /* tx unthrottle interrupt */
187 priv->tx_throttled = 0;
188 /* queue up a wakeup at scheduler time */
189 schedule_work(&priv->wakeup_work);
200 retval = usb_submit_urb(urb, GFP_ATOMIC);
203 "%s - usb_submit_urb failed with result %d",
208 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
210 /* stop receiving characters. We just turn off the URB request, and
211 let chars pile up in the device. If we're doing hardware
212 flowcontrol, the device will signal the other end when its buffer
213 fills up. If we're doing XON/XOFF, this would be a good time to
214 send an XOFF, although it might make sense to foist that off
215 upon the device too. */
216 struct usb_serial_port *port = tty->driver_data;
218 usb_kill_urb(port->interrupt_in_urb);
222 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
224 struct usb_serial_port *port = tty->driver_data;
225 /* just restart the receive interrupt URB */
227 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
228 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
232 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
269 bindex = 5; /* Default to 9600 */
273 /* rather than figure out how to sleep while waiting for this
274 to complete, I just use the "legacy" API. */
275 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
278 | USB_RECIP_INTERFACE
279 | USB_DIR_OUT, /* type */
291 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
293 struct usb_serial_port *port = tty->driver_data;
294 struct usb_serial *serial = port->serial;
298 if (break_state == -1)
299 value = 1; /* start break */
301 value = 0; /* clear break */
302 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
304 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
305 value, 0, NULL, 0, 2000);
307 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
309 /* there is something funky about this.. the TCSBRK that 'cu' performs
310 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
311 seconds apart, but it feels like the break sent isn't as long as it
316 static void keyspan_pda_set_termios(struct tty_struct *tty,
317 struct usb_serial_port *port, struct ktermios *old_termios)
319 struct usb_serial *serial = port->serial;
322 /* cflag specifies lots of stuff: number of stop bits, parity, number
323 of data bits, baud. What can the device actually handle?:
324 CSTOPB (1 stop bit or 2)
327 There is minimal hw support for parity (a PSW bit seems to hold the
328 parity of whatever is in the accumulator). The UART either deals
329 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
330 1 special, stop). So, with firmware changes, we could do:
332 8N2: 11 bit, extra bit always (mark?)
333 8[EOMS]1: 11 bit, extra bit is parity
334 7[EOMS]1: 10 bit, b0/b7 is parity
335 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
337 HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS
340 For now, just do baud. */
342 speed = tty_get_baud_rate(tty);
343 speed = keyspan_pda_setbaud(serial, speed);
346 dev_dbg(&port->dev, "can't handle requested baud rate\n");
347 /* It hasn't changed so.. */
348 speed = tty_termios_baud_rate(old_termios);
350 /* Only speed can change so copy the old h/w parameters
351 then encode the new speed */
352 tty_termios_copy_hw(tty->termios, old_termios);
353 tty_encode_baud_rate(tty, speed, speed);
357 /* modem control pins: DTR and RTS are outputs and can be controlled.
358 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
359 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
361 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
362 unsigned char *value)
367 data = kmalloc(1, GFP_KERNEL);
371 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
373 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
374 0, 0, data, 1, 2000);
383 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
387 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
389 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
390 value, 0, NULL, 0, 2000);
394 static int keyspan_pda_tiocmget(struct tty_struct *tty)
396 struct usb_serial_port *port = tty->driver_data;
397 struct usb_serial *serial = port->serial;
399 unsigned char status;
402 rc = keyspan_pda_get_modem_info(serial, &status);
406 ((status & (1<<7)) ? TIOCM_DTR : 0) |
407 ((status & (1<<6)) ? TIOCM_CAR : 0) |
408 ((status & (1<<5)) ? TIOCM_RNG : 0) |
409 ((status & (1<<4)) ? TIOCM_DSR : 0) |
410 ((status & (1<<3)) ? TIOCM_CTS : 0) |
411 ((status & (1<<2)) ? TIOCM_RTS : 0);
415 static int keyspan_pda_tiocmset(struct tty_struct *tty,
416 unsigned int set, unsigned int clear)
418 struct usb_serial_port *port = tty->driver_data;
419 struct usb_serial *serial = port->serial;
421 unsigned char status;
423 rc = keyspan_pda_get_modem_info(serial, &status);
432 if (clear & TIOCM_RTS)
434 if (clear & TIOCM_DTR)
436 rc = keyspan_pda_set_modem_info(serial, status);
440 static int keyspan_pda_write(struct tty_struct *tty,
441 struct usb_serial_port *port, const unsigned char *buf, int count)
443 struct usb_serial *serial = port->serial;
444 int request_unthrottle = 0;
446 struct keyspan_pda_private *priv;
448 priv = usb_get_serial_port_data(port);
449 /* guess how much room is left in the device's ring buffer, and if we
450 want to send more than that, check first, updating our notion of
451 what is left. If our write will result in no room left, ask the
452 device to give us an interrupt when the room available rises above
453 a threshold, and hold off all writers (eventually, those using
454 select() or poll() too) until we receive that unthrottle interrupt.
455 Block if we can't write anything at all, otherwise write as much as
458 dev_dbg(&port->dev, "write request of 0 bytes\n");
462 /* we might block because of:
463 the TX urb is in-flight (wait until it completes)
464 the device is full (wait until it says there is room)
466 spin_lock_bh(&port->lock);
467 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
468 spin_unlock_bh(&port->lock);
471 clear_bit(0, &port->write_urbs_free);
472 spin_unlock_bh(&port->lock);
474 /* At this point the URB is in our control, nobody else can submit it
475 again (the only sudden transition was the one from EINPROGRESS to
476 finished). Also, the tx process is not throttled. So we are
479 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
481 /* Check if we might overrun the Tx buffer. If so, ask the
482 device how much room it really has. This is done only on
483 scheduler time, since usb_control_msg() sleeps. */
484 if (count > priv->tx_room && !in_interrupt()) {
487 room = kmalloc(1, GFP_KERNEL);
493 rc = usb_control_msg(serial->dev,
494 usb_rcvctrlpipe(serial->dev, 0),
496 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
498 0, /* value: 0 means "remaining room" */
504 dev_dbg(&port->dev, "roomquery says %d\n", *room);
505 priv->tx_room = *room;
509 dev_dbg(&port->dev, "roomquery failed\n");
513 dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
514 rc = -EIO; /* device didn't return any data */
518 if (count > priv->tx_room) {
519 /* we're about to completely fill the Tx buffer, so
520 we'll be throttled afterwards. */
521 count = priv->tx_room;
522 request_unthrottle = 1;
526 /* now transfer data */
527 memcpy(port->write_urb->transfer_buffer, buf, count);
528 /* send the data out the bulk port */
529 port->write_urb->transfer_buffer_length = count;
531 priv->tx_room -= count;
533 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
535 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
539 /* There wasn't any room left, so we are throttled until
540 the buffer empties a bit */
541 request_unthrottle = 1;
544 if (request_unthrottle) {
545 priv->tx_throttled = 1; /* block writers */
546 schedule_work(&priv->unthrottle_work);
552 set_bit(0, &port->write_urbs_free);
557 static void keyspan_pda_write_bulk_callback(struct urb *urb)
559 struct usb_serial_port *port = urb->context;
560 struct keyspan_pda_private *priv;
562 set_bit(0, &port->write_urbs_free);
563 priv = usb_get_serial_port_data(port);
565 /* queue up a wakeup at scheduler time */
566 schedule_work(&priv->wakeup_work);
570 static int keyspan_pda_write_room(struct tty_struct *tty)
572 struct usb_serial_port *port = tty->driver_data;
573 struct keyspan_pda_private *priv;
574 priv = usb_get_serial_port_data(port);
575 /* used by n_tty.c for processing of tabs and such. Giving it our
576 conservative guess is probably good enough, but needs testing by
577 running a console through the device. */
578 return priv->tx_room;
582 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
584 struct usb_serial_port *port = tty->driver_data;
585 struct keyspan_pda_private *priv;
589 priv = usb_get_serial_port_data(port);
591 /* when throttled, return at least WAKEUP_CHARS to tell select() (via
592 n_tty.c:normal_poll() ) that we're not writeable. */
594 spin_lock_irqsave(&port->lock, flags);
595 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
597 spin_unlock_irqrestore(&port->lock, flags);
602 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
604 struct usb_serial *serial = port->serial;
608 keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2));
610 keyspan_pda_set_modem_info(serial, 0);
615 static int keyspan_pda_open(struct tty_struct *tty,
616 struct usb_serial_port *port)
618 struct usb_serial *serial = port->serial;
621 struct keyspan_pda_private *priv;
623 /* find out how much room is in the Tx ring */
624 room = kmalloc(1, GFP_KERNEL);
628 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
630 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
638 dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
642 dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
646 priv = usb_get_serial_port_data(port);
647 priv->tx_room = *room;
648 priv->tx_throttled = *room ? 0 : 1;
650 /*Start reading from the device*/
651 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
653 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
660 static void keyspan_pda_close(struct usb_serial_port *port)
662 struct usb_serial *serial = port->serial;
665 /* shutdown our bulk reads and writes */
666 usb_kill_urb(port->write_urb);
667 usb_kill_urb(port->interrupt_in_urb);
672 /* download the firmware to a "fake" device (pre-renumeration) */
673 static int keyspan_pda_fake_startup(struct usb_serial *serial)
677 const struct ihex_binrec *record;
678 const struct firmware *fw;
680 /* download the firmware here ... */
681 response = ezusb_set_reset(serial->dev, 1);
685 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
686 fw_name = "keyspan_pda/keyspan_pda.fw";
689 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
690 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID))
691 fw_name = "keyspan_pda/xircom_pgs.fw";
694 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
698 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
699 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
703 record = (const struct ihex_binrec *)fw->data;
706 response = ezusb_writememory(serial->dev, be32_to_cpu(record->addr),
707 (unsigned char *)record->data,
708 be16_to_cpu(record->len), 0xa0);
710 dev_err(&serial->dev->dev, "ezusb_writememory failed "
711 "for Keyspan PDA firmware (%d %04X %p %d)\n",
712 response, be32_to_cpu(record->addr),
713 record->data, be16_to_cpu(record->len));
716 record = ihex_next_binrec(record);
718 release_firmware(fw);
719 /* bring device out of reset. Renumeration will occur in a moment
720 and the new device will bind to the real driver */
721 response = ezusb_set_reset(serial->dev, 0);
723 /* we want this device to fail to have a driver assigned to it. */
728 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
731 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
734 static int keyspan_pda_startup(struct usb_serial *serial)
737 struct keyspan_pda_private *priv;
739 /* allocate the private data structures for all ports. Well, for all
742 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
744 return 1; /* error */
745 usb_set_serial_port_data(serial->port[0], priv);
746 init_waitqueue_head(&serial->port[0]->write_wait);
747 INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
748 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
749 priv->serial = serial;
750 priv->port = serial->port[0];
754 static void keyspan_pda_release(struct usb_serial *serial)
756 kfree(usb_get_serial_port_data(serial->port[0]));
760 static struct usb_serial_driver keyspan_pda_fake_device = {
762 .owner = THIS_MODULE,
763 .name = "keyspan_pda_pre",
765 .description = "Keyspan PDA - (prerenumeration)",
766 .id_table = id_table_fake,
768 .attach = keyspan_pda_fake_startup,
773 static struct usb_serial_driver xircom_pgs_fake_device = {
775 .owner = THIS_MODULE,
776 .name = "xircom_no_firm",
778 .description = "Xircom / Entregra PGS - (prerenumeration)",
779 .id_table = id_table_fake_xircom,
781 .attach = keyspan_pda_fake_startup,
785 static struct usb_serial_driver keyspan_pda_device = {
787 .owner = THIS_MODULE,
788 .name = "keyspan_pda",
790 .description = "Keyspan PDA",
791 .id_table = id_table_std,
793 .dtr_rts = keyspan_pda_dtr_rts,
794 .open = keyspan_pda_open,
795 .close = keyspan_pda_close,
796 .write = keyspan_pda_write,
797 .write_room = keyspan_pda_write_room,
798 .write_bulk_callback = keyspan_pda_write_bulk_callback,
799 .read_int_callback = keyspan_pda_rx_interrupt,
800 .chars_in_buffer = keyspan_pda_chars_in_buffer,
801 .throttle = keyspan_pda_rx_throttle,
802 .unthrottle = keyspan_pda_rx_unthrottle,
803 .set_termios = keyspan_pda_set_termios,
804 .break_ctl = keyspan_pda_break_ctl,
805 .tiocmget = keyspan_pda_tiocmget,
806 .tiocmset = keyspan_pda_tiocmset,
807 .attach = keyspan_pda_startup,
808 .release = keyspan_pda_release,
811 static struct usb_serial_driver * const serial_drivers[] = {
814 &keyspan_pda_fake_device,
817 &xircom_pgs_fake_device,
822 module_usb_serial_driver(serial_drivers, id_table_combined);
824 MODULE_AUTHOR(DRIVER_AUTHOR);
825 MODULE_DESCRIPTION(DRIVER_DESC);
826 MODULE_LICENSE("GPL");