symbolserial.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 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 available_room = 0;
  50. int data_length;
  51. dbg("%s - port %d", __func__, port->number);
  52. switch (status) {
  53. case 0:
  54. /* success */
  55. break;
  56. case -ECONNRESET:
  57. case -ENOENT:
  58. case -ESHUTDOWN:
  59. /* this urb is terminated, clean up */
  60. dbg("%s - urb shutting down with status: %d",
  61. __func__, status);
  62. return;
  63. default:
  64. dbg("%s - nonzero urb status received: %d",
  65. __func__, status);
  66. goto exit;
  67. }
  68. usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
  69. data);
  70. if (urb->actual_length > 1) {
  71. data_length = urb->actual_length - 1;
  72. /*
  73. * Data from the device comes with a 1 byte header:
  74. *
  75. * <size of data>data...
  76. * This is real data to be sent to the tty layer
  77. * we pretty much just ignore the size and send everything
  78. * else to the tty layer.
  79. */
  80. tty = tty_port_tty_get(&port->port);
  81. if (tty) {
  82. available_room = tty_buffer_request_room(tty,
  83. data_length);
  84. if (available_room) {
  85. tty_insert_flip_string(tty, &data[1],
  86. available_room);
  87. tty_flip_buffer_push(tty);
  88. }
  89. tty_kref_put(tty);
  90. }
  91. } else {
  92. dev_dbg(&priv->udev->dev,
  93. "Improper ammount of data received from the device, "
  94. "%d bytes", urb->actual_length);
  95. }
  96. exit:
  97. spin_lock(&priv->lock);
  98. /* Continue trying to always read if we should */
  99. if (!priv->throttled) {
  100. usb_fill_int_urb(priv->int_urb, priv->udev,
  101. usb_rcvintpipe(priv->udev,
  102. priv->int_address),
  103. priv->int_buffer, priv->buffer_size,
  104. symbol_int_callback, priv, priv->bInterval);
  105. result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
  106. if (result)
  107. dev_err(&port->dev,
  108. "%s - failed resubmitting read urb, error %d\n",
  109. __func__, result);
  110. } else
  111. priv->actually_throttled = true;
  112. spin_unlock(&priv->lock);
  113. }
  114. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  115. {
  116. struct symbol_private *priv = usb_get_serial_data(port->serial);
  117. unsigned long flags;
  118. int result = 0;
  119. dbg("%s - port %d", __func__, port->number);
  120. spin_lock_irqsave(&priv->lock, flags);
  121. priv->throttled = false;
  122. priv->actually_throttled = false;
  123. priv->port = port;
  124. spin_unlock_irqrestore(&priv->lock, flags);
  125. /* Start reading from the device */
  126. usb_fill_int_urb(priv->int_urb, priv->udev,
  127. usb_rcvintpipe(priv->udev, priv->int_address),
  128. priv->int_buffer, priv->buffer_size,
  129. symbol_int_callback, priv, priv->bInterval);
  130. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  131. if (result)
  132. dev_err(&port->dev,
  133. "%s - failed resubmitting read urb, error %d\n",
  134. __func__, result);
  135. return result;
  136. }
  137. static void symbol_close(struct usb_serial_port *port)
  138. {
  139. struct symbol_private *priv = usb_get_serial_data(port->serial);
  140. dbg("%s - port %d", __func__, port->number);
  141. /* shutdown our urbs */
  142. usb_kill_urb(priv->int_urb);
  143. }
  144. static void symbol_throttle(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. dbg("%s - port %d", __func__, port->number);
  149. spin_lock_irq(&priv->lock);
  150. priv->throttled = true;
  151. spin_unlock_irq(&priv->lock);
  152. }
  153. static void symbol_unthrottle(struct tty_struct *tty)
  154. {
  155. struct usb_serial_port *port = tty->driver_data;
  156. struct symbol_private *priv = usb_get_serial_data(port->serial);
  157. int result;
  158. bool was_throttled;
  159. dbg("%s - port %d", __func__, port->number);
  160. spin_lock_irq(&priv->lock);
  161. priv->throttled = false;
  162. was_throttled = priv->actually_throttled;
  163. priv->actually_throttled = false;
  164. spin_unlock_irq(&priv->lock);
  165. priv->int_urb->dev = port->serial->dev;
  166. if (was_throttled) {
  167. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  168. if (result)
  169. dev_err(&port->dev,
  170. "%s - failed submitting read urb, error %d\n",
  171. __func__, result);
  172. }
  173. }
  174. static int symbol_startup(struct usb_serial *serial)
  175. {
  176. struct symbol_private *priv;
  177. struct usb_host_interface *intf;
  178. int i;
  179. int retval = -ENOMEM;
  180. bool int_in_found = false;
  181. /* create our private serial structure */
  182. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  183. if (priv == NULL) {
  184. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  185. return -ENOMEM;
  186. }
  187. spin_lock_init(&priv->lock);
  188. priv->serial = serial;
  189. priv->port = serial->port[0];
  190. priv->udev = serial->dev;
  191. /* find our interrupt endpoint */
  192. intf = serial->interface->altsetting;
  193. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  194. struct usb_endpoint_descriptor *endpoint;
  195. endpoint = &intf->endpoint[i].desc;
  196. if (!usb_endpoint_is_int_in(endpoint))
  197. continue;
  198. priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  199. if (!priv->int_urb) {
  200. dev_err(&priv->udev->dev, "out of memory\n");
  201. goto error;
  202. }
  203. priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  204. priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  205. if (!priv->int_buffer) {
  206. dev_err(&priv->udev->dev, "out of memory\n");
  207. goto error;
  208. }
  209. priv->int_address = endpoint->bEndpointAddress;
  210. priv->bInterval = endpoint->bInterval;
  211. /* set up our int urb */
  212. usb_fill_int_urb(priv->int_urb, priv->udev,
  213. usb_rcvintpipe(priv->udev,
  214. endpoint->bEndpointAddress),
  215. priv->int_buffer, priv->buffer_size,
  216. symbol_int_callback, priv, priv->bInterval);
  217. int_in_found = true;
  218. break;
  219. }
  220. if (!int_in_found) {
  221. dev_err(&priv->udev->dev,
  222. "Error - the proper endpoints were not found!\n");
  223. goto error;
  224. }
  225. usb_set_serial_data(serial, priv);
  226. return 0;
  227. error:
  228. usb_free_urb(priv->int_urb);
  229. kfree(priv->int_buffer);
  230. kfree(priv);
  231. return retval;
  232. }
  233. static void symbol_disconnect(struct usb_serial *serial)
  234. {
  235. struct symbol_private *priv = usb_get_serial_data(serial);
  236. dbg("%s", __func__);
  237. usb_kill_urb(priv->int_urb);
  238. usb_free_urb(priv->int_urb);
  239. }
  240. static void symbol_release(struct usb_serial *serial)
  241. {
  242. struct symbol_private *priv = usb_get_serial_data(serial);
  243. dbg("%s", __func__);
  244. kfree(priv->int_buffer);
  245. kfree(priv);
  246. }
  247. static struct usb_driver symbol_driver = {
  248. .name = "symbol",
  249. .probe = usb_serial_probe,
  250. .disconnect = usb_serial_disconnect,
  251. .id_table = id_table,
  252. .no_dynamic_id = 1,
  253. };
  254. static struct usb_serial_driver symbol_device = {
  255. .driver = {
  256. .owner = THIS_MODULE,
  257. .name = "symbol",
  258. },
  259. .id_table = id_table,
  260. .usb_driver = &symbol_driver,
  261. .num_ports = 1,
  262. .attach = symbol_startup,
  263. .open = symbol_open,
  264. .close = symbol_close,
  265. .disconnect = symbol_disconnect,
  266. .release = symbol_release,
  267. .throttle = symbol_throttle,
  268. .unthrottle = symbol_unthrottle,
  269. };
  270. static int __init symbol_init(void)
  271. {
  272. int retval;
  273. retval = usb_serial_register(&symbol_device);
  274. if (retval)
  275. return retval;
  276. retval = usb_register(&symbol_driver);
  277. if (retval)
  278. usb_serial_deregister(&symbol_device);
  279. return retval;
  280. }
  281. static void __exit symbol_exit(void)
  282. {
  283. usb_deregister(&symbol_driver);
  284. usb_serial_deregister(&symbol_device);
  285. }
  286. module_init(symbol_init);
  287. module_exit(symbol_exit);
  288. MODULE_LICENSE("GPL");
  289. module_param(debug, bool, S_IRUGO | S_IWUSR);
  290. MODULE_PARM_DESC(debug, "Debug enabled or not");