Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / usb / serial / keyspan_pda.c
blobe98b479593d37167574cd640dee06f18557d2ea1
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
8 * Copyright (C) 2020 Johan Hovold <johan@kernel.org>
10 * See Documentation/usb/usb-serial.rst for more information on using this
11 * driver
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 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>, Johan Hovold <johan@kernel.org>"
29 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
31 #define KEYSPAN_TX_THRESHOLD 128
33 struct keyspan_pda_private {
34 int tx_room;
35 struct work_struct unthrottle_work;
36 struct usb_serial *serial;
37 struct usb_serial_port *port;
40 static int keyspan_pda_write_start(struct usb_serial_port *port);
42 #define KEYSPAN_VENDOR_ID 0x06cd
43 #define KEYSPAN_PDA_FAKE_ID 0x0103
44 #define KEYSPAN_PDA_ID 0x0104 /* no clue */
46 /* For Xircom PGSDB9 and older Entrega version of the same device */
47 #define XIRCOM_VENDOR_ID 0x085a
48 #define XIRCOM_FAKE_ID 0x8027
49 #define XIRCOM_FAKE_ID_2 0x8025 /* "PGMFHUB" serial */
50 #define ENTREGA_VENDOR_ID 0x1645
51 #define ENTREGA_FAKE_ID 0x8093
53 static const struct usb_device_id id_table_combined[] = {
54 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
55 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
56 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
57 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
58 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
59 { } /* Terminating entry */
61 MODULE_DEVICE_TABLE(usb, id_table_combined);
63 static const struct usb_device_id id_table_std[] = {
64 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
65 { } /* Terminating entry */
68 static const struct usb_device_id id_table_fake[] = {
69 { 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) },
73 { } /* Terminating entry */
76 static int keyspan_pda_get_write_room(struct keyspan_pda_private *priv)
78 struct usb_serial_port *port = priv->port;
79 struct usb_serial *serial = port->serial;
80 u8 room;
81 int rc;
83 rc = usb_control_msg_recv(serial->dev,
85 6, /* write_room */
86 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_IN,
87 0, /* value: 0 means "remaining room" */
88 0, /* index */
89 &room,
91 2000,
92 GFP_KERNEL);
93 if (rc) {
94 dev_dbg(&port->dev, "roomquery failed: %d\n", rc);
95 return rc;
98 dev_dbg(&port->dev, "roomquery says %d\n", room);
100 return room;
103 static void keyspan_pda_request_unthrottle(struct work_struct *work)
105 struct keyspan_pda_private *priv =
106 container_of(work, struct keyspan_pda_private, unthrottle_work);
107 struct usb_serial_port *port = priv->port;
108 struct usb_serial *serial = port->serial;
109 unsigned long flags;
110 int result;
112 dev_dbg(&port->dev, "%s\n", __func__);
115 * Ask the device to tell us when the tx buffer becomes
116 * sufficiently empty.
118 result = usb_control_msg(serial->dev,
119 usb_sndctrlpipe(serial->dev, 0),
120 7, /* request_unthrottle */
121 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
122 | USB_DIR_OUT,
123 KEYSPAN_TX_THRESHOLD,
124 0, /* index */
125 NULL,
127 2000);
128 if (result < 0)
129 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
130 __func__, result);
132 * Need to check available space after requesting notification in case
133 * buffer is already empty so that no notification is sent.
135 result = keyspan_pda_get_write_room(priv);
136 if (result > KEYSPAN_TX_THRESHOLD) {
137 spin_lock_irqsave(&port->lock, flags);
138 priv->tx_room = max(priv->tx_room, result);
139 spin_unlock_irqrestore(&port->lock, flags);
141 usb_serial_port_softint(port);
145 static void keyspan_pda_rx_interrupt(struct urb *urb)
147 struct usb_serial_port *port = urb->context;
148 unsigned char *data = urb->transfer_buffer;
149 unsigned int len = urb->actual_length;
150 int retval;
151 int status = urb->status;
152 struct keyspan_pda_private *priv;
153 unsigned long flags;
155 priv = usb_get_serial_port_data(port);
157 switch (status) {
158 case 0:
159 /* success */
160 break;
161 case -ECONNRESET:
162 case -ENOENT:
163 case -ESHUTDOWN:
164 /* this urb is terminated, clean up */
165 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
166 return;
167 default:
168 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
169 goto exit;
172 if (len < 1) {
173 dev_warn(&port->dev, "short message received\n");
174 goto exit;
177 /* see if the message is data or a status interrupt */
178 switch (data[0]) {
179 case 0:
180 /* rest of message is rx data */
181 if (len < 2)
182 break;
183 tty_insert_flip_string(&port->port, data + 1, len - 1);
184 tty_flip_buffer_push(&port->port);
185 break;
186 case 1:
187 /* status interrupt */
188 if (len < 2) {
189 dev_warn(&port->dev, "short interrupt message received\n");
190 break;
192 dev_dbg(&port->dev, "rx int, d1=%d\n", data[1]);
193 switch (data[1]) {
194 case 1: /* modemline change */
195 break;
196 case 2: /* tx unthrottle interrupt */
197 spin_lock_irqsave(&port->lock, flags);
198 priv->tx_room = max(priv->tx_room, KEYSPAN_TX_THRESHOLD);
199 spin_unlock_irqrestore(&port->lock, flags);
201 keyspan_pda_write_start(port);
203 usb_serial_port_softint(port);
204 break;
205 default:
206 break;
208 break;
209 default:
210 break;
213 exit:
214 retval = usb_submit_urb(urb, GFP_ATOMIC);
215 if (retval)
216 dev_err(&port->dev,
217 "%s - usb_submit_urb failed with result %d\n",
218 __func__, retval);
221 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
223 struct usb_serial_port *port = tty->driver_data;
226 * Stop receiving characters. We just turn off the URB request, and
227 * let chars pile up in the device. If we're doing hardware
228 * flowcontrol, the device will signal the other end when its buffer
229 * fills up. If we're doing XON/XOFF, this would be a good time to
230 * send an XOFF, although it might make sense to foist that off upon
231 * the device too.
233 usb_kill_urb(port->interrupt_in_urb);
236 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
238 struct usb_serial_port *port = tty->driver_data;
240 /* just restart the receive interrupt URB */
241 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
242 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
245 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
247 int rc;
248 int bindex;
250 switch (baud) {
251 case 110:
252 bindex = 0;
253 break;
254 case 300:
255 bindex = 1;
256 break;
257 case 1200:
258 bindex = 2;
259 break;
260 case 2400:
261 bindex = 3;
262 break;
263 case 4800:
264 bindex = 4;
265 break;
266 case 9600:
267 bindex = 5;
268 break;
269 case 19200:
270 bindex = 6;
271 break;
272 case 38400:
273 bindex = 7;
274 break;
275 case 57600:
276 bindex = 8;
277 break;
278 case 115200:
279 bindex = 9;
280 break;
281 default:
282 bindex = 5; /* Default to 9600 */
283 baud = 9600;
286 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
287 0, /* set baud */
288 USB_TYPE_VENDOR
289 | USB_RECIP_INTERFACE
290 | USB_DIR_OUT, /* type */
291 bindex, /* value */
292 0, /* index */
293 NULL, /* &data */
294 0, /* size */
295 2000); /* timeout */
296 if (rc < 0)
297 return 0;
299 return baud;
302 static int keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
304 struct usb_serial_port *port = tty->driver_data;
305 struct usb_serial *serial = port->serial;
306 int value;
307 int result;
309 if (break_state == -1)
310 value = 1; /* start break */
311 else
312 value = 0; /* clear break */
314 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
315 4, /* set break */
316 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
317 value, 0, NULL, 0, 2000);
318 if (result < 0) {
319 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
320 __func__, result);
321 return result;
324 return 0;
327 static void keyspan_pda_set_termios(struct tty_struct *tty,
328 struct usb_serial_port *port,
329 const struct ktermios *old_termios)
331 struct usb_serial *serial = port->serial;
332 speed_t speed;
335 * cflag specifies lots of stuff: number of stop bits, parity, number
336 * of data bits, baud. What can the device actually handle?:
337 * CSTOPB (1 stop bit or 2)
338 * PARENB (parity)
339 * CSIZE (5bit .. 8bit)
340 * There is minimal hw support for parity (a PSW bit seems to hold the
341 * parity of whatever is in the accumulator). The UART either deals
342 * with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
343 * 1 special, stop). So, with firmware changes, we could do:
344 * 8N1: 10 bit
345 * 8N2: 11 bit, extra bit always (mark?)
346 * 8[EOMS]1: 11 bit, extra bit is parity
347 * 7[EOMS]1: 10 bit, b0/b7 is parity
348 * 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
350 * HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
351 * bit.
353 * For now, just do baud.
355 speed = tty_get_baud_rate(tty);
356 speed = keyspan_pda_setbaud(serial, speed);
358 if (speed == 0) {
359 dev_dbg(&port->dev, "can't handle requested baud rate\n");
360 /* It hasn't changed so.. */
361 speed = tty_termios_baud_rate(old_termios);
364 * Only speed can change so copy the old h/w parameters then encode
365 * the new speed.
367 tty_termios_copy_hw(&tty->termios, old_termios);
368 tty_encode_baud_rate(tty, speed, speed);
372 * Modem control pins: DTR and RTS are outputs and can be controlled.
373 * DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
374 * read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused.
376 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
377 unsigned char *value)
379 int rc;
380 u8 data;
382 rc = usb_control_msg_recv(serial->dev, 0,
383 3, /* get pins */
384 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_IN,
387 &data,
389 2000,
390 GFP_KERNEL);
391 if (rc == 0)
392 *value = data;
394 return rc;
397 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
398 unsigned char value)
400 int rc;
401 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
402 3, /* set pins */
403 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
404 value, 0, NULL, 0, 2000);
405 return rc;
408 static int keyspan_pda_tiocmget(struct tty_struct *tty)
410 struct usb_serial_port *port = tty->driver_data;
411 struct usb_serial *serial = port->serial;
412 int rc;
413 unsigned char status;
414 int value;
416 rc = keyspan_pda_get_modem_info(serial, &status);
417 if (rc < 0)
418 return rc;
420 value = ((status & BIT(7)) ? TIOCM_DTR : 0) |
421 ((status & BIT(6)) ? TIOCM_CAR : 0) |
422 ((status & BIT(5)) ? TIOCM_RNG : 0) |
423 ((status & BIT(4)) ? TIOCM_DSR : 0) |
424 ((status & BIT(3)) ? TIOCM_CTS : 0) |
425 ((status & BIT(2)) ? TIOCM_RTS : 0);
427 return value;
430 static int keyspan_pda_tiocmset(struct tty_struct *tty,
431 unsigned int set, unsigned int clear)
433 struct usb_serial_port *port = tty->driver_data;
434 struct usb_serial *serial = port->serial;
435 int rc;
436 unsigned char status;
438 rc = keyspan_pda_get_modem_info(serial, &status);
439 if (rc < 0)
440 return rc;
442 if (set & TIOCM_RTS)
443 status |= BIT(2);
444 if (set & TIOCM_DTR)
445 status |= BIT(7);
447 if (clear & TIOCM_RTS)
448 status &= ~BIT(2);
449 if (clear & TIOCM_DTR)
450 status &= ~BIT(7);
451 rc = keyspan_pda_set_modem_info(serial, status);
452 return rc;
455 static int keyspan_pda_write_start(struct usb_serial_port *port)
457 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
458 unsigned long flags;
459 struct urb *urb;
460 int count;
461 int room;
462 int rc;
465 * Guess how much room is left in the device's ring buffer. If our
466 * write will result in no room left, ask the device to give us an
467 * interrupt when the room available rises above a threshold but also
468 * query how much room is currently available (in case our guess was
469 * too conservative and the buffer is already empty when the
470 * unthrottle work is scheduled).
474 * We might block because of:
475 * the TX urb is in-flight (wait until it completes)
476 * the device is full (wait until it says there is room)
478 spin_lock_irqsave(&port->lock, flags);
480 room = priv->tx_room;
481 count = kfifo_len(&port->write_fifo);
483 if (!test_bit(0, &port->write_urbs_free) || count == 0 || room == 0) {
484 spin_unlock_irqrestore(&port->lock, flags);
485 return 0;
487 __clear_bit(0, &port->write_urbs_free);
489 if (count > room)
490 count = room;
491 if (count > port->bulk_out_size)
492 count = port->bulk_out_size;
494 urb = port->write_urb;
495 count = kfifo_out(&port->write_fifo, urb->transfer_buffer, count);
496 urb->transfer_buffer_length = count;
498 port->tx_bytes += count;
499 priv->tx_room -= count;
501 spin_unlock_irqrestore(&port->lock, flags);
503 dev_dbg(&port->dev, "%s - count = %d, txroom = %d\n", __func__, count, room);
505 rc = usb_submit_urb(urb, GFP_ATOMIC);
506 if (rc) {
507 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
509 spin_lock_irqsave(&port->lock, flags);
510 port->tx_bytes -= count;
511 priv->tx_room = max(priv->tx_room, room + count);
512 __set_bit(0, &port->write_urbs_free);
513 spin_unlock_irqrestore(&port->lock, flags);
515 return rc;
518 if (count == room)
519 schedule_work(&priv->unthrottle_work);
521 return count;
524 static void keyspan_pda_write_bulk_callback(struct urb *urb)
526 struct usb_serial_port *port = urb->context;
527 unsigned long flags;
529 spin_lock_irqsave(&port->lock, flags);
530 port->tx_bytes -= urb->transfer_buffer_length;
531 __set_bit(0, &port->write_urbs_free);
532 spin_unlock_irqrestore(&port->lock, flags);
534 keyspan_pda_write_start(port);
536 usb_serial_port_softint(port);
539 static int keyspan_pda_write(struct tty_struct *tty, struct usb_serial_port *port,
540 const unsigned char *buf, int count)
542 int rc;
544 dev_dbg(&port->dev, "%s - count = %d\n", __func__, count);
546 if (!count)
547 return 0;
549 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
551 rc = keyspan_pda_write_start(port);
552 if (rc)
553 return rc;
555 return count;
558 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
560 struct usb_serial *serial = port->serial;
562 if (on)
563 keyspan_pda_set_modem_info(serial, BIT(7) | BIT(2));
564 else
565 keyspan_pda_set_modem_info(serial, 0);
569 static int keyspan_pda_open(struct tty_struct *tty,
570 struct usb_serial_port *port)
572 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
573 int rc;
575 /* find out how much room is in the Tx ring */
576 rc = keyspan_pda_get_write_room(priv);
577 if (rc < 0)
578 return rc;
580 spin_lock_irq(&port->lock);
581 priv->tx_room = rc;
582 spin_unlock_irq(&port->lock);
584 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
585 if (rc) {
586 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
587 return rc;
590 return 0;
593 static void keyspan_pda_close(struct usb_serial_port *port)
595 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
598 * Stop the interrupt URB first as its completion handler may submit
599 * the write URB.
601 usb_kill_urb(port->interrupt_in_urb);
602 usb_kill_urb(port->write_urb);
604 cancel_work_sync(&priv->unthrottle_work);
606 spin_lock_irq(&port->lock);
607 kfifo_reset(&port->write_fifo);
608 spin_unlock_irq(&port->lock);
611 /* download the firmware to a "fake" device (pre-renumeration) */
612 static int keyspan_pda_fake_startup(struct usb_serial *serial)
614 unsigned int vid = le16_to_cpu(serial->dev->descriptor.idVendor);
615 const char *fw_name;
617 /* download the firmware here ... */
618 ezusb_fx1_set_reset(serial->dev, 1);
620 switch (vid) {
621 case KEYSPAN_VENDOR_ID:
622 fw_name = "keyspan_pda/keyspan_pda.fw";
623 break;
624 case XIRCOM_VENDOR_ID:
625 case ENTREGA_VENDOR_ID:
626 fw_name = "keyspan_pda/xircom_pgs.fw";
627 break;
628 default:
629 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
630 __func__);
631 return -ENODEV;
634 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
635 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
636 fw_name);
637 return -ENOENT;
641 * After downloading firmware renumeration will occur in a moment and
642 * the new device will bind to the real driver.
645 /* We want this device to fail to have a driver assigned to it. */
646 return 1;
649 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
650 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
652 static int keyspan_pda_port_probe(struct usb_serial_port *port)
655 struct keyspan_pda_private *priv;
657 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
658 if (!priv)
659 return -ENOMEM;
661 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
662 priv->port = port;
664 usb_set_serial_port_data(port, priv);
666 return 0;
669 static void keyspan_pda_port_remove(struct usb_serial_port *port)
671 struct keyspan_pda_private *priv;
673 priv = usb_get_serial_port_data(port);
674 kfree(priv);
677 static struct usb_serial_driver keyspan_pda_fake_device = {
678 .driver = {
679 .name = "keyspan_pda_pre",
681 .description = "Keyspan PDA - (prerenumeration)",
682 .id_table = id_table_fake,
683 .num_ports = 1,
684 .attach = keyspan_pda_fake_startup,
687 static struct usb_serial_driver keyspan_pda_device = {
688 .driver = {
689 .name = "keyspan_pda",
691 .description = "Keyspan PDA",
692 .id_table = id_table_std,
693 .num_ports = 1,
694 .num_bulk_out = 1,
695 .num_interrupt_in = 1,
696 .dtr_rts = keyspan_pda_dtr_rts,
697 .open = keyspan_pda_open,
698 .close = keyspan_pda_close,
699 .write = keyspan_pda_write,
700 .write_bulk_callback = keyspan_pda_write_bulk_callback,
701 .read_int_callback = keyspan_pda_rx_interrupt,
702 .throttle = keyspan_pda_rx_throttle,
703 .unthrottle = keyspan_pda_rx_unthrottle,
704 .set_termios = keyspan_pda_set_termios,
705 .break_ctl = keyspan_pda_break_ctl,
706 .tiocmget = keyspan_pda_tiocmget,
707 .tiocmset = keyspan_pda_tiocmset,
708 .port_probe = keyspan_pda_port_probe,
709 .port_remove = keyspan_pda_port_remove,
712 static struct usb_serial_driver * const serial_drivers[] = {
713 &keyspan_pda_device,
714 &keyspan_pda_fake_device,
715 NULL
718 module_usb_serial_driver(serial_drivers, id_table_combined);
720 MODULE_AUTHOR(DRIVER_AUTHOR);
721 MODULE_DESCRIPTION(DRIVER_DESC);
722 MODULE_LICENSE("GPL");