symbolserial.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (urb->actual_length > 1) {
  57. data_length = urb->actual_length - 1;
  58. /*
  59. * Data from the device comes with a 1 byte header:
  60. *
  61. * <size of data>data...
  62. * This is real data to be sent to the tty layer
  63. * we pretty much just ignore the size and send everything
  64. * else to the tty layer.
  65. */
  66. tty_insert_flip_string(&port->port, &data[1], data_length);
  67. tty_flip_buffer_push(&port->port);
  68. } else {
  69. dev_dbg(&port->dev,
  70. "Improper amount of data received from the device, "
  71. "%d bytes", urb->actual_length);
  72. }
  73. exit:
  74. spin_lock(&priv->lock);
  75. /* Continue trying to always read if we should */
  76. if (!priv->throttled) {
  77. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  78. if (result)
  79. dev_err(&port->dev,
  80. "%s - failed resubmitting read urb, error %d\n",
  81. __func__, result);
  82. } else
  83. priv->actually_throttled = true;
  84. spin_unlock(&priv->lock);
  85. }
  86. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  87. {
  88. struct symbol_private *priv = usb_get_serial_data(port->serial);
  89. unsigned long flags;
  90. int result = 0;
  91. spin_lock_irqsave(&priv->lock, flags);
  92. priv->throttled = false;
  93. priv->actually_throttled = false;
  94. spin_unlock_irqrestore(&priv->lock, flags);
  95. /* Start reading from the device */
  96. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  97. if (result)
  98. dev_err(&port->dev,
  99. "%s - failed resubmitting read urb, error %d\n",
  100. __func__, result);
  101. return result;
  102. }
  103. static void symbol_close(struct usb_serial_port *port)
  104. {
  105. usb_kill_urb(port->interrupt_in_urb);
  106. }
  107. static void symbol_throttle(struct tty_struct *tty)
  108. {
  109. struct usb_serial_port *port = tty->driver_data;
  110. struct symbol_private *priv = usb_get_serial_data(port->serial);
  111. spin_lock_irq(&priv->lock);
  112. priv->throttled = true;
  113. spin_unlock_irq(&priv->lock);
  114. }
  115. static void symbol_unthrottle(struct tty_struct *tty)
  116. {
  117. struct usb_serial_port *port = tty->driver_data;
  118. struct symbol_private *priv = usb_get_serial_data(port->serial);
  119. int result;
  120. bool was_throttled;
  121. spin_lock_irq(&priv->lock);
  122. priv->throttled = false;
  123. was_throttled = priv->actually_throttled;
  124. priv->actually_throttled = false;
  125. spin_unlock_irq(&priv->lock);
  126. if (was_throttled) {
  127. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  128. if (result)
  129. dev_err(&port->dev,
  130. "%s - failed submitting read urb, error %d\n",
  131. __func__, result);
  132. }
  133. }
  134. static int symbol_startup(struct usb_serial *serial)
  135. {
  136. if (!serial->num_interrupt_in) {
  137. dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
  138. return -ENODEV;
  139. }
  140. return 0;
  141. }
  142. static int symbol_port_probe(struct usb_serial_port *port)
  143. {
  144. struct symbol_private *priv;
  145. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  146. if (!priv)
  147. return -ENOMEM;
  148. spin_lock_init(&priv->lock);
  149. usb_set_serial_port_data(port, priv);
  150. return 0;
  151. }
  152. static int symbol_port_remove(struct usb_serial_port *port)
  153. {
  154. struct symbol_private *priv = usb_get_serial_port_data(port);
  155. kfree(priv);
  156. return 0;
  157. }
  158. static struct usb_serial_driver symbol_device = {
  159. .driver = {
  160. .owner = THIS_MODULE,
  161. .name = "symbol",
  162. },
  163. .id_table = id_table,
  164. .num_ports = 1,
  165. .attach = symbol_startup,
  166. .port_probe = symbol_port_probe,
  167. .port_remove = symbol_port_remove,
  168. .open = symbol_open,
  169. .close = symbol_close,
  170. .throttle = symbol_throttle,
  171. .unthrottle = symbol_unthrottle,
  172. .read_int_callback = symbol_int_callback,
  173. };
  174. static struct usb_serial_driver * const serial_drivers[] = {
  175. &symbol_device, NULL
  176. };
  177. module_usb_serial_driver(serial_drivers, id_table);
  178. MODULE_LICENSE("GPL");