symbolserial.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/tty_driver.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/module.h>
  17. #include <linux/usb.h>
  18. #include <linux/usb/serial.h>
  19. #include <linux/uaccess.h>
  20. static int debug;
  21. static const struct usb_device_id id_table[] = {
  22. { USB_DEVICE(0x05e0, 0x0600) },
  23. { },
  24. };
  25. MODULE_DEVICE_TABLE(usb, id_table);
  26. /* This structure holds all of the individual device information */
  27. struct symbol_private {
  28. struct usb_device *udev;
  29. struct usb_serial *serial;
  30. struct usb_serial_port *port;
  31. unsigned char *int_buffer;
  32. struct urb *int_urb;
  33. int buffer_size;
  34. u8 bInterval;
  35. u8 int_address;
  36. spinlock_t lock; /* protects the following flags */
  37. bool throttled;
  38. bool actually_throttled;
  39. bool rts;
  40. };
  41. static void symbol_int_callback(struct urb *urb)
  42. {
  43. struct symbol_private *priv = urb->context;
  44. unsigned char *data = urb->transfer_buffer;
  45. struct usb_serial_port *port = priv->port;
  46. int status = urb->status;
  47. struct tty_struct *tty;
  48. int result;
  49. int data_length;
  50. dbg("%s - port %d", __func__, port->number);
  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. dbg("%s - urb shutting down with status: %d",
  60. __func__, status);
  61. return;
  62. default:
  63. dbg("%s - nonzero urb status received: %d",
  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 ammount 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. dbg("%s - port %d", __func__, port->number);
  114. spin_lock_irqsave(&priv->lock, flags);
  115. priv->throttled = false;
  116. priv->actually_throttled = false;
  117. priv->port = port;
  118. spin_unlock_irqrestore(&priv->lock, flags);
  119. /* Start reading from the device */
  120. usb_fill_int_urb(priv->int_urb, priv->udev,
  121. usb_rcvintpipe(priv->udev, priv->int_address),
  122. priv->int_buffer, priv->buffer_size,
  123. symbol_int_callback, priv, priv->bInterval);
  124. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  125. if (result)
  126. dev_err(&port->dev,
  127. "%s - failed resubmitting read urb, error %d\n",
  128. __func__, result);
  129. return result;
  130. }
  131. static void symbol_close(struct usb_serial_port *port)
  132. {
  133. struct symbol_private *priv = usb_get_serial_data(port->serial);
  134. dbg("%s - port %d", __func__, port->number);
  135. /* shutdown our urbs */
  136. usb_kill_urb(priv->int_urb);
  137. }
  138. static void symbol_throttle(struct tty_struct *tty)
  139. {
  140. struct usb_serial_port *port = tty->driver_data;
  141. struct symbol_private *priv = usb_get_serial_data(port->serial);
  142. dbg("%s - port %d", __func__, port->number);
  143. spin_lock_irq(&priv->lock);
  144. priv->throttled = true;
  145. spin_unlock_irq(&priv->lock);
  146. }
  147. static void symbol_unthrottle(struct tty_struct *tty)
  148. {
  149. struct usb_serial_port *port = tty->driver_data;
  150. struct symbol_private *priv = usb_get_serial_data(port->serial);
  151. int result;
  152. bool was_throttled;
  153. dbg("%s - port %d", __func__, port->number);
  154. spin_lock_irq(&priv->lock);
  155. priv->throttled = false;
  156. was_throttled = priv->actually_throttled;
  157. priv->actually_throttled = false;
  158. spin_unlock_irq(&priv->lock);
  159. priv->int_urb->dev = port->serial->dev;
  160. if (was_throttled) {
  161. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  162. if (result)
  163. dev_err(&port->dev,
  164. "%s - failed submitting read urb, error %d\n",
  165. __func__, result);
  166. }
  167. }
  168. static int symbol_startup(struct usb_serial *serial)
  169. {
  170. struct symbol_private *priv;
  171. struct usb_host_interface *intf;
  172. int i;
  173. int retval = -ENOMEM;
  174. bool int_in_found = false;
  175. /* create our private serial structure */
  176. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  177. if (priv == NULL) {
  178. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  179. return -ENOMEM;
  180. }
  181. spin_lock_init(&priv->lock);
  182. priv->serial = serial;
  183. priv->port = serial->port[0];
  184. priv->udev = serial->dev;
  185. /* find our interrupt endpoint */
  186. intf = serial->interface->altsetting;
  187. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  188. struct usb_endpoint_descriptor *endpoint;
  189. endpoint = &intf->endpoint[i].desc;
  190. if (!usb_endpoint_is_int_in(endpoint))
  191. continue;
  192. priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  193. if (!priv->int_urb) {
  194. dev_err(&priv->udev->dev, "out of memory\n");
  195. goto error;
  196. }
  197. priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  198. priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  199. if (!priv->int_buffer) {
  200. dev_err(&priv->udev->dev, "out of memory\n");
  201. goto error;
  202. }
  203. priv->int_address = endpoint->bEndpointAddress;
  204. priv->bInterval = endpoint->bInterval;
  205. /* set up our int urb */
  206. usb_fill_int_urb(priv->int_urb, priv->udev,
  207. usb_rcvintpipe(priv->udev,
  208. endpoint->bEndpointAddress),
  209. priv->int_buffer, priv->buffer_size,
  210. symbol_int_callback, priv, priv->bInterval);
  211. int_in_found = true;
  212. break;
  213. }
  214. if (!int_in_found) {
  215. dev_err(&priv->udev->dev,
  216. "Error - the proper endpoints were not found!\n");
  217. goto error;
  218. }
  219. usb_set_serial_data(serial, priv);
  220. return 0;
  221. error:
  222. usb_free_urb(priv->int_urb);
  223. kfree(priv->int_buffer);
  224. kfree(priv);
  225. return retval;
  226. }
  227. static void symbol_disconnect(struct usb_serial *serial)
  228. {
  229. struct symbol_private *priv = usb_get_serial_data(serial);
  230. dbg("%s", __func__);
  231. usb_kill_urb(priv->int_urb);
  232. usb_free_urb(priv->int_urb);
  233. }
  234. static void symbol_release(struct usb_serial *serial)
  235. {
  236. struct symbol_private *priv = usb_get_serial_data(serial);
  237. dbg("%s", __func__);
  238. kfree(priv->int_buffer);
  239. kfree(priv);
  240. }
  241. static struct usb_driver symbol_driver = {
  242. .name = "symbol",
  243. .probe = usb_serial_probe,
  244. .disconnect = usb_serial_disconnect,
  245. .id_table = id_table,
  246. .no_dynamic_id = 1,
  247. };
  248. static struct usb_serial_driver symbol_device = {
  249. .driver = {
  250. .owner = THIS_MODULE,
  251. .name = "symbol",
  252. },
  253. .id_table = id_table,
  254. .usb_driver = &symbol_driver,
  255. .num_ports = 1,
  256. .attach = symbol_startup,
  257. .open = symbol_open,
  258. .close = symbol_close,
  259. .disconnect = symbol_disconnect,
  260. .release = symbol_release,
  261. .throttle = symbol_throttle,
  262. .unthrottle = symbol_unthrottle,
  263. };
  264. static int __init symbol_init(void)
  265. {
  266. int retval;
  267. retval = usb_serial_register(&symbol_device);
  268. if (retval)
  269. return retval;
  270. retval = usb_register(&symbol_driver);
  271. if (retval)
  272. usb_serial_deregister(&symbol_device);
  273. return retval;
  274. }
  275. static void __exit symbol_exit(void)
  276. {
  277. usb_deregister(&symbol_driver);
  278. usb_serial_deregister(&symbol_device);
  279. }
  280. module_init(symbol_init);
  281. module_exit(symbol_exit);
  282. MODULE_LICENSE("GPL");
  283. module_param(debug, bool, S_IRUGO | S_IWUSR);
  284. MODULE_PARM_DESC(debug, "Debug enabled or not");