symbolserial.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Symbol USB barcode to serial driver
  4. *
  5. * Copyright (C) 2013 Johan Hovold <jhovold@gmail.com>
  6. * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
  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. int result;
  35. int data_length;
  36. switch (status) {
  37. case 0:
  38. /* success */
  39. break;
  40. case -ECONNRESET:
  41. case -ENOENT:
  42. case -ESHUTDOWN:
  43. /* this urb is terminated, clean up */
  44. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  45. __func__, status);
  46. return;
  47. default:
  48. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  49. __func__, status);
  50. goto exit;
  51. }
  52. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  53. /*
  54. * Data from the device comes with a 1 byte header:
  55. *
  56. * <size of data> <data>...
  57. */
  58. if (urb->actual_length > 1) {
  59. data_length = data[0];
  60. if (data_length > (urb->actual_length - 1))
  61. data_length = urb->actual_length - 1;
  62. tty_insert_flip_string(&port->port, &data[1], data_length);
  63. tty_flip_buffer_push(&port->port);
  64. } else {
  65. dev_dbg(&port->dev, "%s - short packet\n", __func__);
  66. }
  67. exit:
  68. spin_lock(&priv->lock);
  69. /* Continue trying to always read if we should */
  70. if (!priv->throttled) {
  71. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  72. if (result)
  73. dev_err(&port->dev,
  74. "%s - failed resubmitting read urb, error %d\n",
  75. __func__, result);
  76. } else
  77. priv->actually_throttled = true;
  78. spin_unlock(&priv->lock);
  79. }
  80. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  81. {
  82. struct symbol_private *priv = usb_get_serial_port_data(port);
  83. unsigned long flags;
  84. int result = 0;
  85. spin_lock_irqsave(&priv->lock, flags);
  86. priv->throttled = false;
  87. priv->actually_throttled = false;
  88. spin_unlock_irqrestore(&priv->lock, flags);
  89. /* Start reading from the device */
  90. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  91. if (result)
  92. dev_err(&port->dev,
  93. "%s - failed resubmitting read urb, error %d\n",
  94. __func__, result);
  95. return result;
  96. }
  97. static void symbol_close(struct usb_serial_port *port)
  98. {
  99. usb_kill_urb(port->interrupt_in_urb);
  100. }
  101. static void symbol_throttle(struct tty_struct *tty)
  102. {
  103. struct usb_serial_port *port = tty->driver_data;
  104. struct symbol_private *priv = usb_get_serial_port_data(port);
  105. spin_lock_irq(&priv->lock);
  106. priv->throttled = true;
  107. spin_unlock_irq(&priv->lock);
  108. }
  109. static void symbol_unthrottle(struct tty_struct *tty)
  110. {
  111. struct usb_serial_port *port = tty->driver_data;
  112. struct symbol_private *priv = usb_get_serial_port_data(port);
  113. int result;
  114. bool was_throttled;
  115. spin_lock_irq(&priv->lock);
  116. priv->throttled = false;
  117. was_throttled = priv->actually_throttled;
  118. priv->actually_throttled = false;
  119. spin_unlock_irq(&priv->lock);
  120. if (was_throttled) {
  121. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  122. if (result)
  123. dev_err(&port->dev,
  124. "%s - failed submitting read urb, error %d\n",
  125. __func__, result);
  126. }
  127. }
  128. static int symbol_port_probe(struct usb_serial_port *port)
  129. {
  130. struct symbol_private *priv;
  131. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  132. if (!priv)
  133. return -ENOMEM;
  134. spin_lock_init(&priv->lock);
  135. usb_set_serial_port_data(port, priv);
  136. return 0;
  137. }
  138. static int symbol_port_remove(struct usb_serial_port *port)
  139. {
  140. struct symbol_private *priv = usb_get_serial_port_data(port);
  141. kfree(priv);
  142. return 0;
  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");