symbolserial.c 4.6 KB

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