symbolserial.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Symbol USB barcode to serial driver
  3. *
  4. * Copyright (C) 2013 Johan Hovold <jhovold@gmail.com>
  5. * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
  6. * Copyright (C) 2009 Novell Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/tty.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/serial.h>
  20. #include <linux/uaccess.h>
  21. static const struct usb_device_id id_table[] = {
  22. { USB_DEVICE(0x05e0, 0x0600) },
  23. { },
  24. };
  25. MODULE_DEVICE_TABLE(usb, id_table);
  26. struct symbol_private {
  27. spinlock_t lock; /* protects the following flags */
  28. bool throttled;
  29. bool actually_throttled;
  30. };
  31. static void symbol_int_callback(struct urb *urb)
  32. {
  33. struct usb_serial_port *port = urb->context;
  34. struct symbol_private *priv = usb_get_serial_port_data(port);
  35. unsigned char *data = urb->transfer_buffer;
  36. int status = urb->status;
  37. int result;
  38. int data_length;
  39. switch (status) {
  40. case 0:
  41. /* success */
  42. break;
  43. case -ECONNRESET:
  44. case -ENOENT:
  45. case -ESHUTDOWN:
  46. /* this urb is terminated, clean up */
  47. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  48. __func__, status);
  49. return;
  50. default:
  51. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  52. __func__, status);
  53. goto exit;
  54. }
  55. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  56. /*
  57. * Data from the device comes with a 1 byte header:
  58. *
  59. * <size of data> <data>...
  60. */
  61. if (urb->actual_length > 1) {
  62. data_length = data[0];
  63. if (data_length > (urb->actual_length - 1))
  64. data_length = urb->actual_length - 1;
  65. tty_insert_flip_string(&port->port, &data[1], data_length);
  66. tty_flip_buffer_push(&port->port);
  67. } else {
  68. dev_dbg(&port->dev, "%s - short packet\n", __func__);
  69. }
  70. exit:
  71. spin_lock(&priv->lock);
  72. /* Continue trying to always read if we should */
  73. if (!priv->throttled) {
  74. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  75. if (result)
  76. dev_err(&port->dev,
  77. "%s - failed resubmitting read urb, error %d\n",
  78. __func__, result);
  79. } else
  80. priv->actually_throttled = true;
  81. spin_unlock(&priv->lock);
  82. }
  83. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  84. {
  85. struct symbol_private *priv = usb_get_serial_port_data(port);
  86. unsigned long flags;
  87. int result = 0;
  88. spin_lock_irqsave(&priv->lock, flags);
  89. priv->throttled = false;
  90. priv->actually_throttled = false;
  91. spin_unlock_irqrestore(&priv->lock, flags);
  92. /* Start reading from the device */
  93. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  94. if (result)
  95. dev_err(&port->dev,
  96. "%s - failed resubmitting read urb, error %d\n",
  97. __func__, result);
  98. return result;
  99. }
  100. static void symbol_close(struct usb_serial_port *port)
  101. {
  102. usb_kill_urb(port->interrupt_in_urb);
  103. }
  104. static void symbol_throttle(struct tty_struct *tty)
  105. {
  106. struct usb_serial_port *port = tty->driver_data;
  107. struct symbol_private *priv = usb_get_serial_port_data(port);
  108. spin_lock_irq(&priv->lock);
  109. priv->throttled = true;
  110. spin_unlock_irq(&priv->lock);
  111. }
  112. static void symbol_unthrottle(struct tty_struct *tty)
  113. {
  114. struct usb_serial_port *port = tty->driver_data;
  115. struct symbol_private *priv = usb_get_serial_port_data(port);
  116. int result;
  117. bool was_throttled;
  118. spin_lock_irq(&priv->lock);
  119. priv->throttled = false;
  120. was_throttled = priv->actually_throttled;
  121. priv->actually_throttled = false;
  122. spin_unlock_irq(&priv->lock);
  123. if (was_throttled) {
  124. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  125. if (result)
  126. dev_err(&port->dev,
  127. "%s - failed submitting read urb, error %d\n",
  128. __func__, result);
  129. }
  130. }
  131. static int symbol_startup(struct usb_serial *serial)
  132. {
  133. if (!serial->num_interrupt_in) {
  134. dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
  135. return -ENODEV;
  136. }
  137. return 0;
  138. }
  139. static int symbol_port_probe(struct usb_serial_port *port)
  140. {
  141. struct symbol_private *priv;
  142. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  143. if (!priv)
  144. return -ENOMEM;
  145. spin_lock_init(&priv->lock);
  146. usb_set_serial_port_data(port, priv);
  147. return 0;
  148. }
  149. static int symbol_port_remove(struct usb_serial_port *port)
  150. {
  151. struct symbol_private *priv = usb_get_serial_port_data(port);
  152. kfree(priv);
  153. return 0;
  154. }
  155. static struct usb_serial_driver symbol_device = {
  156. .driver = {
  157. .owner = THIS_MODULE,
  158. .name = "symbol",
  159. },
  160. .id_table = id_table,
  161. .num_ports = 1,
  162. .attach = symbol_startup,
  163. .port_probe = symbol_port_probe,
  164. .port_remove = symbol_port_remove,
  165. .open = symbol_open,
  166. .close = symbol_close,
  167. .throttle = symbol_throttle,
  168. .unthrottle = symbol_unthrottle,
  169. .read_int_callback = symbol_int_callback,
  170. };
  171. static struct usb_serial_driver * const serial_drivers[] = {
  172. &symbol_device, NULL
  173. };
  174. module_usb_serial_driver(serial_drivers, id_table);
  175. MODULE_LICENSE("GPL");