1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/tty.h>
19#include <linux/tty_driver.h>
20#include <linux/tty_flip.h>
21#include <linux/module.h>
22#include <linux/spinlock.h>
23#include <linux/workqueue.h>
24#include <linux/uaccess.h>
25#include <linux/usb.h>
26#include <linux/usb/serial.h>
27#include <linux/usb/ezusb.h>
28
29
30#if IS_ENABLED(CONFIG_USB_SERIAL_KEYSPAN_PDA)
31 #define KEYSPAN
32#else
33 #undef KEYSPAN
34#endif
35#if IS_ENABLED(CONFIG_USB_SERIAL_XIRCOM)
36 #define XIRCOM
37#else
38 #undef XIRCOM
39#endif
40
41#define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>, Johan Hovold <johan@kernel.org>"
42#define DRIVER_DESC "USB Keyspan PDA Converter driver"
43
44#define KEYSPAN_TX_THRESHOLD 128
45
46struct keyspan_pda_private {
47 int tx_room;
48 struct work_struct unthrottle_work;
49 struct usb_serial *serial;
50 struct usb_serial_port *port;
51};
52
53static int keyspan_pda_write_start(struct usb_serial_port *port);
54
55#define KEYSPAN_VENDOR_ID 0x06cd
56#define KEYSPAN_PDA_FAKE_ID 0x0103
57#define KEYSPAN_PDA_ID 0x0104
58
59
60#define XIRCOM_VENDOR_ID 0x085a
61#define XIRCOM_FAKE_ID 0x8027
62#define XIRCOM_FAKE_ID_2 0x8025
63#define ENTREGA_VENDOR_ID 0x1645
64#define ENTREGA_FAKE_ID 0x8093
65
66static const struct usb_device_id id_table_combined[] = {
67#ifdef KEYSPAN
68 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
69#endif
70#ifdef XIRCOM
71 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
72 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
73 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
74#endif
75 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
76 { }
77};
78
79MODULE_DEVICE_TABLE(usb, id_table_combined);
80
81static const struct usb_device_id id_table_std[] = {
82 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
83 { }
84};
85
86#ifdef KEYSPAN
87static const struct usb_device_id id_table_fake[] = {
88 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
89 { }
90};
91#endif
92
93#ifdef XIRCOM
94static const struct usb_device_id id_table_fake_xircom[] = {
95 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
96 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
97 { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
98 { }
99};
100#endif
101
102static int keyspan_pda_get_write_room(struct keyspan_pda_private *priv)
103{
104 struct usb_serial_port *port = priv->port;
105 struct usb_serial *serial = port->serial;
106 u8 *room;
107 int rc;
108
109 room = kmalloc(1, GFP_KERNEL);
110 if (!room)
111 return -ENOMEM;
112
113 rc = usb_control_msg(serial->dev,
114 usb_rcvctrlpipe(serial->dev, 0),
115 6,
116 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
117 | USB_DIR_IN,
118 0,
119 0,
120 room,
121 1,
122 2000);
123 if (rc != 1) {
124 if (rc >= 0)
125 rc = -EIO;
126 dev_dbg(&port->dev, "roomquery failed: %d\n", rc);
127 goto out_free;
128 }
129
130 dev_dbg(&port->dev, "roomquery says %d\n", *room);
131 rc = *room;
132out_free:
133 kfree(room);
134
135 return rc;
136}
137
138static void keyspan_pda_request_unthrottle(struct work_struct *work)
139{
140 struct keyspan_pda_private *priv =
141 container_of(work, struct keyspan_pda_private, unthrottle_work);
142 struct usb_serial_port *port = priv->port;
143 struct usb_serial *serial = port->serial;
144 unsigned long flags;
145 int result;
146
147 dev_dbg(&port->dev, "%s\n", __func__);
148
149
150
151 result = usb_control_msg(serial->dev,
152 usb_sndctrlpipe(serial->dev, 0),
153 7,
154 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
155 | USB_DIR_OUT,
156 KEYSPAN_TX_THRESHOLD,
157 0,
158 NULL,
159 0,
160 2000);
161 if (result < 0)
162 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
163 __func__, result);
164
165
166
167
168 result = keyspan_pda_get_write_room(priv);
169 if (result > KEYSPAN_TX_THRESHOLD) {
170 spin_lock_irqsave(&port->lock, flags);
171 priv->tx_room = max(priv->tx_room, result);
172 spin_unlock_irqrestore(&port->lock, flags);
173
174 usb_serial_port_softint(port);
175 }
176}
177
178static void keyspan_pda_rx_interrupt(struct urb *urb)
179{
180 struct usb_serial_port *port = urb->context;
181 unsigned char *data = urb->transfer_buffer;
182 unsigned int len = urb->actual_length;
183 int retval;
184 int status = urb->status;
185 struct keyspan_pda_private *priv;
186 unsigned long flags;
187
188 priv = usb_get_serial_port_data(port);
189
190 switch (status) {
191 case 0:
192
193 break;
194 case -ECONNRESET:
195 case -ENOENT:
196 case -ESHUTDOWN:
197
198 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
199 return;
200 default:
201 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
202 goto exit;
203 }
204
205 if (len < 1) {
206 dev_warn(&port->dev, "short message received\n");
207 goto exit;
208 }
209
210
211 switch (data[0]) {
212 case 0:
213
214 if (len < 2)
215 break;
216 tty_insert_flip_string(&port->port, data + 1, len - 1);
217 tty_flip_buffer_push(&port->port);
218 break;
219 case 1:
220
221 if (len < 2) {
222 dev_warn(&port->dev, "short interrupt message received\n");
223 break;
224 }
225 dev_dbg(&port->dev, "rx int, d1=%d\n", data[1]);
226 switch (data[1]) {
227 case 1:
228 break;
229 case 2:
230 spin_lock_irqsave(&port->lock, flags);
231 priv->tx_room = max(priv->tx_room, KEYSPAN_TX_THRESHOLD);
232 spin_unlock_irqrestore(&port->lock, flags);
233
234 keyspan_pda_write_start(port);
235
236
237 usb_serial_port_softint(port);
238 break;
239 default:
240 break;
241 }
242 break;
243 default:
244 break;
245 }
246
247exit:
248 retval = usb_submit_urb(urb, GFP_ATOMIC);
249 if (retval)
250 dev_err(&port->dev,
251 "%s - usb_submit_urb failed with result %d\n",
252 __func__, retval);
253}
254
255
256static void keyspan_pda_rx_throttle(struct tty_struct *tty)
257{
258
259
260
261
262
263
264 struct usb_serial_port *port = tty->driver_data;
265
266 usb_kill_urb(port->interrupt_in_urb);
267}
268
269
270static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
271{
272 struct usb_serial_port *port = tty->driver_data;
273
274
275 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
276 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
277}
278
279
280static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
281{
282 int rc;
283 int bindex;
284
285 switch (baud) {
286 case 110:
287 bindex = 0;
288 break;
289 case 300:
290 bindex = 1;
291 break;
292 case 1200:
293 bindex = 2;
294 break;
295 case 2400:
296 bindex = 3;
297 break;
298 case 4800:
299 bindex = 4;
300 break;
301 case 9600:
302 bindex = 5;
303 break;
304 case 19200:
305 bindex = 6;
306 break;
307 case 38400:
308 bindex = 7;
309 break;
310 case 57600:
311 bindex = 8;
312 break;
313 case 115200:
314 bindex = 9;
315 break;
316 default:
317 bindex = 5;
318 baud = 9600;
319 }
320
321
322
323 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
324 0,
325 USB_TYPE_VENDOR
326 | USB_RECIP_INTERFACE
327 | USB_DIR_OUT,
328 bindex,
329 0,
330 NULL,
331 0,
332 2000);
333 if (rc < 0)
334 return 0;
335 return baud;
336}
337
338
339static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
340{
341 struct usb_serial_port *port = tty->driver_data;
342 struct usb_serial *serial = port->serial;
343 int value;
344 int result;
345
346 if (break_state == -1)
347 value = 1;
348 else
349 value = 0;
350 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
351 4,
352 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
353 value, 0, NULL, 0, 2000);
354 if (result < 0)
355 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
356 __func__, result);
357
358
359
360
361}
362
363
364static void keyspan_pda_set_termios(struct tty_struct *tty,
365 struct usb_serial_port *port, struct ktermios *old_termios)
366{
367 struct usb_serial *serial = port->serial;
368 speed_t speed;
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390 speed = tty_get_baud_rate(tty);
391 speed = keyspan_pda_setbaud(serial, speed);
392
393 if (speed == 0) {
394 dev_dbg(&port->dev, "can't handle requested baud rate\n");
395
396 speed = tty_termios_baud_rate(old_termios);
397 }
398
399
400 tty_termios_copy_hw(&tty->termios, old_termios);
401 tty_encode_baud_rate(tty, speed, speed);
402}
403
404
405
406
407
408
409static int keyspan_pda_get_modem_info(struct usb_serial *serial,
410 unsigned char *value)
411{
412 int rc;
413 u8 *data;
414
415 data = kmalloc(1, GFP_KERNEL);
416 if (!data)
417 return -ENOMEM;
418
419 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
420 3,
421 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
422 0, 0, data, 1, 2000);
423 if (rc == 1)
424 *value = *data;
425 else if (rc >= 0)
426 rc = -EIO;
427
428 kfree(data);
429 return rc;
430}
431
432
433static int keyspan_pda_set_modem_info(struct usb_serial *serial,
434 unsigned char value)
435{
436 int rc;
437 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
438 3,
439 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
440 value, 0, NULL, 0, 2000);
441 return rc;
442}
443
444static int keyspan_pda_tiocmget(struct tty_struct *tty)
445{
446 struct usb_serial_port *port = tty->driver_data;
447 struct usb_serial *serial = port->serial;
448 int rc;
449 unsigned char status;
450 int value;
451
452 rc = keyspan_pda_get_modem_info(serial, &status);
453 if (rc < 0)
454 return rc;
455
456 value = ((status & BIT(7)) ? TIOCM_DTR : 0) |
457 ((status & BIT(6)) ? TIOCM_CAR : 0) |
458 ((status & BIT(5)) ? TIOCM_RNG : 0) |
459 ((status & BIT(4)) ? TIOCM_DSR : 0) |
460 ((status & BIT(3)) ? TIOCM_CTS : 0) |
461 ((status & BIT(2)) ? TIOCM_RTS : 0);
462
463 return value;
464}
465
466static int keyspan_pda_tiocmset(struct tty_struct *tty,
467 unsigned int set, unsigned int clear)
468{
469 struct usb_serial_port *port = tty->driver_data;
470 struct usb_serial *serial = port->serial;
471 int rc;
472 unsigned char status;
473
474 rc = keyspan_pda_get_modem_info(serial, &status);
475 if (rc < 0)
476 return rc;
477
478 if (set & TIOCM_RTS)
479 status |= BIT(2);
480 if (set & TIOCM_DTR)
481 status |= BIT(7);
482
483 if (clear & TIOCM_RTS)
484 status &= ~BIT(2);
485 if (clear & TIOCM_DTR)
486 status &= ~BIT(7);
487 rc = keyspan_pda_set_modem_info(serial, status);
488 return rc;
489}
490
491static int keyspan_pda_write_start(struct usb_serial_port *port)
492{
493 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
494 unsigned long flags;
495 struct urb *urb;
496 int count;
497 int room;
498 int rc;
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513 spin_lock_irqsave(&port->lock, flags);
514
515 room = priv->tx_room;
516 count = kfifo_len(&port->write_fifo);
517
518 if (!test_bit(0, &port->write_urbs_free) || count == 0 || room == 0) {
519 spin_unlock_irqrestore(&port->lock, flags);
520 return 0;
521 }
522 __clear_bit(0, &port->write_urbs_free);
523
524 if (count > room)
525 count = room;
526 if (count > port->bulk_out_size)
527 count = port->bulk_out_size;
528
529 urb = port->write_urb;
530 count = kfifo_out(&port->write_fifo, urb->transfer_buffer, count);
531 urb->transfer_buffer_length = count;
532
533 port->tx_bytes += count;
534 priv->tx_room -= count;
535
536 spin_unlock_irqrestore(&port->lock, flags);
537
538 dev_dbg(&port->dev, "%s - count = %d, txroom = %d\n", __func__, count, room);
539
540 rc = usb_submit_urb(urb, GFP_ATOMIC);
541 if (rc) {
542 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
543
544 spin_lock_irqsave(&port->lock, flags);
545 port->tx_bytes -= count;
546 priv->tx_room = max(priv->tx_room, room + count);
547 __set_bit(0, &port->write_urbs_free);
548 spin_unlock_irqrestore(&port->lock, flags);
549
550 return rc;
551 }
552
553 if (count == room)
554 schedule_work(&priv->unthrottle_work);
555
556 return count;
557}
558
559static void keyspan_pda_write_bulk_callback(struct urb *urb)
560{
561 struct usb_serial_port *port = urb->context;
562 unsigned long flags;
563
564 spin_lock_irqsave(&port->lock, flags);
565 port->tx_bytes -= urb->transfer_buffer_length;
566 __set_bit(0, &port->write_urbs_free);
567 spin_unlock_irqrestore(&port->lock, flags);
568
569 keyspan_pda_write_start(port);
570
571
572 usb_serial_port_softint(port);
573}
574
575static int keyspan_pda_write(struct tty_struct *tty, struct usb_serial_port *port,
576 const unsigned char *buf, int count)
577{
578 int rc;
579
580 dev_dbg(&port->dev, "%s - count = %d\n", __func__, count);
581
582 if (!count)
583 return 0;
584
585 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
586
587 rc = keyspan_pda_write_start(port);
588 if (rc)
589 return rc;
590
591 return count;
592}
593
594static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
595{
596 struct usb_serial *serial = port->serial;
597
598 if (on)
599 keyspan_pda_set_modem_info(serial, BIT(7) | BIT(2));
600 else
601 keyspan_pda_set_modem_info(serial, 0);
602}
603
604
605static int keyspan_pda_open(struct tty_struct *tty,
606 struct usb_serial_port *port)
607{
608 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
609 int rc;
610
611
612 rc = keyspan_pda_get_write_room(priv);
613 if (rc < 0)
614 return rc;
615
616 spin_lock_irq(&port->lock);
617 priv->tx_room = rc;
618 spin_unlock_irq(&port->lock);
619
620
621 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
622 if (rc) {
623 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
624 return rc;
625 }
626
627 return 0;
628}
629
630static void keyspan_pda_close(struct usb_serial_port *port)
631{
632 struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
633
634
635
636
637
638 usb_kill_urb(port->interrupt_in_urb);
639 usb_kill_urb(port->write_urb);
640
641 cancel_work_sync(&priv->unthrottle_work);
642
643 spin_lock_irq(&port->lock);
644 kfifo_reset(&port->write_fifo);
645 spin_unlock_irq(&port->lock);
646}
647
648
649static int keyspan_pda_fake_startup(struct usb_serial *serial)
650{
651 const char *fw_name;
652
653
654 ezusb_fx1_set_reset(serial->dev, 1);
655
656 if (0) { ; }
657#ifdef KEYSPAN
658 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
659 fw_name = "keyspan_pda/keyspan_pda.fw";
660#endif
661#ifdef XIRCOM
662 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
663 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
664 fw_name = "keyspan_pda/xircom_pgs.fw";
665#endif
666 else {
667 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
668 __func__);
669 return -ENODEV;
670 }
671
672 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
673 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
674 fw_name);
675 return -ENOENT;
676 }
677
678
679
680
681
682 return 1;
683}
684
685#ifdef KEYSPAN
686MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
687#endif
688#ifdef XIRCOM
689MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
690#endif
691
692static int keyspan_pda_port_probe(struct usb_serial_port *port)
693{
694
695 struct keyspan_pda_private *priv;
696
697 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
698 if (!priv)
699 return -ENOMEM;
700
701 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
702 priv->port = port;
703
704 usb_set_serial_port_data(port, priv);
705
706 return 0;
707}
708
709static int keyspan_pda_port_remove(struct usb_serial_port *port)
710{
711 struct keyspan_pda_private *priv;
712
713 priv = usb_get_serial_port_data(port);
714 kfree(priv);
715
716 return 0;
717}
718
719#ifdef KEYSPAN
720static struct usb_serial_driver keyspan_pda_fake_device = {
721 .driver = {
722 .owner = THIS_MODULE,
723 .name = "keyspan_pda_pre",
724 },
725 .description = "Keyspan PDA - (prerenumeration)",
726 .id_table = id_table_fake,
727 .num_ports = 1,
728 .attach = keyspan_pda_fake_startup,
729};
730#endif
731
732#ifdef XIRCOM
733static struct usb_serial_driver xircom_pgs_fake_device = {
734 .driver = {
735 .owner = THIS_MODULE,
736 .name = "xircom_no_firm",
737 },
738 .description = "Xircom / Entrega PGS - (prerenumeration)",
739 .id_table = id_table_fake_xircom,
740 .num_ports = 1,
741 .attach = keyspan_pda_fake_startup,
742};
743#endif
744
745static struct usb_serial_driver keyspan_pda_device = {
746 .driver = {
747 .owner = THIS_MODULE,
748 .name = "keyspan_pda",
749 },
750 .description = "Keyspan PDA",
751 .id_table = id_table_std,
752 .num_ports = 1,
753 .num_bulk_out = 1,
754 .num_interrupt_in = 1,
755 .dtr_rts = keyspan_pda_dtr_rts,
756 .open = keyspan_pda_open,
757 .close = keyspan_pda_close,
758 .write = keyspan_pda_write,
759 .write_bulk_callback = keyspan_pda_write_bulk_callback,
760 .read_int_callback = keyspan_pda_rx_interrupt,
761 .throttle = keyspan_pda_rx_throttle,
762 .unthrottle = keyspan_pda_rx_unthrottle,
763 .set_termios = keyspan_pda_set_termios,
764 .break_ctl = keyspan_pda_break_ctl,
765 .tiocmget = keyspan_pda_tiocmget,
766 .tiocmset = keyspan_pda_tiocmset,
767 .port_probe = keyspan_pda_port_probe,
768 .port_remove = keyspan_pda_port_remove,
769};
770
771static struct usb_serial_driver * const serial_drivers[] = {
772 &keyspan_pda_device,
773#ifdef KEYSPAN
774 &keyspan_pda_fake_device,
775#endif
776#ifdef XIRCOM
777 &xircom_pgs_fake_device,
778#endif
779 NULL
780};
781
782module_usb_serial_driver(serial_drivers, id_table_combined);
783
784MODULE_AUTHOR(DRIVER_AUTHOR);
785MODULE_DESCRIPTION(DRIVER_DESC);
786MODULE_LICENSE("GPL");
787