symbolserial.c 5.0 KB

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