symbolserial.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Symbol USB barcode to serial driver
  3. *
  4. * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
  5. * Copyright (C) 2009 Novell Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.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 bool debug;
  22. static const struct usb_device_id id_table[] = {
  23. { USB_DEVICE(0x05e0, 0x0600) },
  24. { },
  25. };
  26. MODULE_DEVICE_TABLE(usb, id_table);
  27. /* This structure holds all of the individual device information */
  28. struct symbol_private {
  29. struct usb_device *udev;
  30. struct usb_serial *serial;
  31. struct usb_serial_port *port;
  32. unsigned char *int_buffer;
  33. struct urb *int_urb;
  34. int buffer_size;
  35. u8 bInterval;
  36. u8 int_address;
  37. spinlock_t lock; /* protects the following flags */
  38. bool throttled;
  39. bool actually_throttled;
  40. bool rts;
  41. };
  42. static void symbol_int_callback(struct urb *urb)
  43. {
  44. struct symbol_private *priv = urb->context;
  45. unsigned char *data = urb->transfer_buffer;
  46. struct usb_serial_port *port = priv->port;
  47. int status = urb->status;
  48. struct tty_struct *tty;
  49. int result;
  50. int data_length;
  51. switch (status) {
  52. case 0:
  53. /* success */
  54. break;
  55. case -ECONNRESET:
  56. case -ENOENT:
  57. case -ESHUTDOWN:
  58. /* this urb is terminated, clean up */
  59. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  60. __func__, status);
  61. return;
  62. default:
  63. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  64. __func__, status);
  65. goto exit;
  66. }
  67. usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
  68. data);
  69. if (urb->actual_length > 1) {
  70. data_length = urb->actual_length - 1;
  71. /*
  72. * Data from the device comes with a 1 byte header:
  73. *
  74. * <size of data>data...
  75. * This is real data to be sent to the tty layer
  76. * we pretty much just ignore the size and send everything
  77. * else to the tty layer.
  78. */
  79. tty = tty_port_tty_get(&port->port);
  80. if (tty) {
  81. tty_insert_flip_string(tty, &data[1], data_length);
  82. tty_flip_buffer_push(tty);
  83. tty_kref_put(tty);
  84. }
  85. } else {
  86. dev_dbg(&priv->udev->dev,
  87. "Improper amount of data received from the device, "
  88. "%d bytes", urb->actual_length);
  89. }
  90. exit:
  91. spin_lock(&priv->lock);
  92. /* Continue trying to always read if we should */
  93. if (!priv->throttled) {
  94. usb_fill_int_urb(priv->int_urb, priv->udev,
  95. usb_rcvintpipe(priv->udev,
  96. priv->int_address),
  97. priv->int_buffer, priv->buffer_size,
  98. symbol_int_callback, priv, priv->bInterval);
  99. result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
  100. if (result)
  101. dev_err(&port->dev,
  102. "%s - failed resubmitting read urb, error %d\n",
  103. __func__, result);
  104. } else
  105. priv->actually_throttled = true;
  106. spin_unlock(&priv->lock);
  107. }
  108. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  109. {
  110. struct symbol_private *priv = usb_get_serial_data(port->serial);
  111. unsigned long flags;
  112. int result = 0;
  113. spin_lock_irqsave(&priv->lock, flags);
  114. priv->throttled = false;
  115. priv->actually_throttled = false;
  116. priv->port = port;
  117. spin_unlock_irqrestore(&priv->lock, flags);
  118. /* Start reading from the device */
  119. usb_fill_int_urb(priv->int_urb, priv->udev,
  120. usb_rcvintpipe(priv->udev, priv->int_address),
  121. priv->int_buffer, priv->buffer_size,
  122. symbol_int_callback, priv, priv->bInterval);
  123. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  124. if (result)
  125. dev_err(&port->dev,
  126. "%s - failed resubmitting read urb, error %d\n",
  127. __func__, result);
  128. return result;
  129. }
  130. static void symbol_close(struct usb_serial_port *port)
  131. {
  132. struct symbol_private *priv = usb_get_serial_data(port->serial);
  133. /* shutdown our urbs */
  134. usb_kill_urb(priv->int_urb);
  135. }
  136. static void symbol_throttle(struct tty_struct *tty)
  137. {
  138. struct usb_serial_port *port = tty->driver_data;
  139. struct symbol_private *priv = usb_get_serial_data(port->serial);
  140. spin_lock_irq(&priv->lock);
  141. priv->throttled = true;
  142. spin_unlock_irq(&priv->lock);
  143. }
  144. static void symbol_unthrottle(struct tty_struct *tty)
  145. {
  146. struct usb_serial_port *port = tty->driver_data;
  147. struct symbol_private *priv = usb_get_serial_data(port->serial);
  148. int result;
  149. bool was_throttled;
  150. spin_lock_irq(&priv->lock);
  151. priv->throttled = false;
  152. was_throttled = priv->actually_throttled;
  153. priv->actually_throttled = false;
  154. spin_unlock_irq(&priv->lock);
  155. if (was_throttled) {
  156. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  157. if (result)
  158. dev_err(&port->dev,
  159. "%s - failed submitting read urb, error %d\n",
  160. __func__, result);
  161. }
  162. }
  163. static int symbol_startup(struct usb_serial *serial)
  164. {
  165. struct symbol_private *priv;
  166. struct usb_host_interface *intf;
  167. int i;
  168. int retval = -ENOMEM;
  169. bool int_in_found = false;
  170. /* create our private serial structure */
  171. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  172. if (priv == NULL) {
  173. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  174. return -ENOMEM;
  175. }
  176. spin_lock_init(&priv->lock);
  177. priv->serial = serial;
  178. priv->port = serial->port[0];
  179. priv->udev = serial->dev;
  180. /* find our interrupt endpoint */
  181. intf = serial->interface->altsetting;
  182. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  183. struct usb_endpoint_descriptor *endpoint;
  184. endpoint = &intf->endpoint[i].desc;
  185. if (!usb_endpoint_is_int_in(endpoint))
  186. continue;
  187. priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  188. if (!priv->int_urb) {
  189. dev_err(&priv->udev->dev, "out of memory\n");
  190. goto error;
  191. }
  192. priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
  193. priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  194. if (!priv->int_buffer) {
  195. dev_err(&priv->udev->dev, "out of memory\n");
  196. goto error;
  197. }
  198. priv->int_address = endpoint->bEndpointAddress;
  199. priv->bInterval = endpoint->bInterval;
  200. /* set up our int urb */
  201. usb_fill_int_urb(priv->int_urb, priv->udev,
  202. usb_rcvintpipe(priv->udev,
  203. endpoint->bEndpointAddress),
  204. priv->int_buffer, priv->buffer_size,
  205. symbol_int_callback, priv, priv->bInterval);
  206. int_in_found = true;
  207. break;
  208. }
  209. if (!int_in_found) {
  210. dev_err(&priv->udev->dev,
  211. "Error - the proper endpoints were not found!\n");
  212. goto error;
  213. }
  214. usb_set_serial_data(serial, priv);
  215. return 0;
  216. error:
  217. usb_free_urb(priv->int_urb);
  218. kfree(priv->int_buffer);
  219. kfree(priv);
  220. return retval;
  221. }
  222. static void symbol_disconnect(struct usb_serial *serial)
  223. {
  224. struct symbol_private *priv = usb_get_serial_data(serial);
  225. usb_kill_urb(priv->int_urb);
  226. usb_free_urb(priv->int_urb);
  227. }
  228. static void symbol_release(struct usb_serial *serial)
  229. {
  230. struct symbol_private *priv = usb_get_serial_data(serial);
  231. kfree(priv->int_buffer);
  232. kfree(priv);
  233. }
  234. static struct usb_serial_driver symbol_device = {
  235. .driver = {
  236. .owner = THIS_MODULE,
  237. .name = "symbol",
  238. },
  239. .id_table = id_table,
  240. .num_ports = 1,
  241. .attach = symbol_startup,
  242. .open = symbol_open,
  243. .close = symbol_close,
  244. .disconnect = symbol_disconnect,
  245. .release = symbol_release,
  246. .throttle = symbol_throttle,
  247. .unthrottle = symbol_unthrottle,
  248. };
  249. static struct usb_serial_driver * const serial_drivers[] = {
  250. &symbol_device, NULL
  251. };
  252. module_usb_serial_driver(serial_drivers, id_table);
  253. MODULE_LICENSE("GPL");
  254. module_param(debug, bool, S_IRUGO | S_IWUSR);
  255. MODULE_PARM_DESC(debug, "Debug enabled or not");