symbolserial.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. struct file *filp)
  116. {
  117. struct symbol_private *priv = usb_get_serial_data(port->serial);
  118. unsigned long flags;
  119. int result = 0;
  120. dbg("%s - port %d", __func__, port->number);
  121. spin_lock_irqsave(&priv->lock, flags);
  122. priv->throttled = false;
  123. priv->actually_throttled = false;
  124. priv->port = port;
  125. spin_unlock_irqrestore(&priv->lock, flags);
  126. /* Start reading from the device */
  127. usb_fill_int_urb(priv->int_urb, priv->udev,
  128. usb_rcvintpipe(priv->udev, priv->int_address),
  129. priv->int_buffer, priv->buffer_size,
  130. symbol_int_callback, priv, priv->bInterval);
  131. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  132. if (result)
  133. dev_err(&port->dev,
  134. "%s - failed resubmitting read urb, error %d\n",
  135. __func__, result);
  136. return result;
  137. }
  138. static void symbol_close(struct usb_serial_port *port)
  139. {
  140. struct symbol_private *priv = usb_get_serial_data(port->serial);
  141. dbg("%s - port %d", __func__, port->number);
  142. /* shutdown our urbs */
  143. usb_kill_urb(priv->int_urb);
  144. }
  145. static void symbol_throttle(struct tty_struct *tty)
  146. {
  147. struct usb_serial_port *port = tty->driver_data;
  148. struct symbol_private *priv = usb_get_serial_data(port->serial);
  149. unsigned long flags;
  150. dbg("%s - port %d", __func__, port->number);
  151. spin_lock_irqsave(&priv->lock, flags);
  152. priv->throttled = true;
  153. spin_unlock_irqrestore(&priv->lock, flags);
  154. }
  155. static void symbol_unthrottle(struct tty_struct *tty)
  156. {
  157. struct usb_serial_port *port = tty->driver_data;
  158. struct symbol_private *priv = usb_get_serial_data(port->serial);
  159. unsigned long flags;
  160. int result;
  161. dbg("%s - port %d", __func__, port->number);
  162. spin_lock_irqsave(&priv->lock, flags);
  163. priv->throttled = false;
  164. priv->actually_throttled = false;
  165. spin_unlock_irqrestore(&priv->lock, flags);
  166. priv->int_urb->dev = port->serial->dev;
  167. result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
  168. if (result)
  169. dev_err(&port->dev,
  170. "%s - failed submitting read urb, error %d\n",
  171. __func__, result);
  172. }
  173. static int symbol_startup(struct usb_serial *serial)
  174. {
  175. struct symbol_private *priv;
  176. struct usb_host_interface *intf;
  177. int i;
  178. int retval = -ENOMEM;
  179. bool int_in_found = false;
  180. /* create our private serial structure */
  181. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  182. if (priv == NULL) {
  183. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  184. return -ENOMEM;
  185. }
  186. spin_lock_init(&priv->lock);
  187. priv->serial = serial;
  188. priv->port = serial->port[0];
  189. priv->udev = serial->dev;
  190. /* find our interrupt endpoint */
  191. intf = serial->interface->altsetting;
  192. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  193. struct usb_endpoint_descriptor *endpoint;
  194. endpoint = &intf->endpoint[i].desc;
  195. if (!usb_endpoint_is_int_in(endpoint))
  196. continue;
  197. priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  198. if (!priv->int_urb) {
  199. dev_err(&priv->udev->dev, "out of memory\n");
  200. goto error;
  201. }
  202. priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  203. priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  204. if (!priv->int_buffer) {
  205. dev_err(&priv->udev->dev, "out of memory\n");
  206. goto error;
  207. }
  208. priv->int_address = endpoint->bEndpointAddress;
  209. priv->bInterval = endpoint->bInterval;
  210. /* set up our int urb */
  211. usb_fill_int_urb(priv->int_urb, priv->udev,
  212. usb_rcvintpipe(priv->udev,
  213. endpoint->bEndpointAddress),
  214. priv->int_buffer, priv->buffer_size,
  215. symbol_int_callback, priv, priv->bInterval);
  216. int_in_found = true;
  217. break;
  218. }
  219. if (!int_in_found) {
  220. dev_err(&priv->udev->dev,
  221. "Error - the proper endpoints were not found!\n");
  222. goto error;
  223. }
  224. usb_set_serial_data(serial, priv);
  225. return 0;
  226. error:
  227. usb_free_urb(priv->int_urb);
  228. kfree(priv->int_buffer);
  229. kfree(priv);
  230. return retval;
  231. }
  232. static void symbol_disconnect(struct usb_serial *serial)
  233. {
  234. struct symbol_private *priv = usb_get_serial_data(serial);
  235. dbg("%s", __func__);
  236. usb_kill_urb(priv->int_urb);
  237. usb_free_urb(priv->int_urb);
  238. }
  239. static void symbol_release(struct usb_serial *serial)
  240. {
  241. struct symbol_private *priv = usb_get_serial_data(serial);
  242. dbg("%s", __func__);
  243. kfree(priv->int_buffer);
  244. kfree(priv);
  245. }
  246. static struct usb_driver symbol_driver = {
  247. .name = "symbol",
  248. .probe = usb_serial_probe,
  249. .disconnect = usb_serial_disconnect,
  250. .id_table = id_table,
  251. .no_dynamic_id = 1,
  252. };
  253. static struct usb_serial_driver symbol_device = {
  254. .driver = {
  255. .owner = THIS_MODULE,
  256. .name = "symbol",
  257. },
  258. .id_table = id_table,
  259. .usb_driver = &symbol_driver,
  260. .num_ports = 1,
  261. .attach = symbol_startup,
  262. .open = symbol_open,
  263. .close = symbol_close,
  264. .disconnect = symbol_disconnect,
  265. .release = symbol_release,
  266. .throttle = symbol_throttle,
  267. .unthrottle = symbol_unthrottle,
  268. };
  269. static int __init symbol_init(void)
  270. {
  271. int retval;
  272. retval = usb_serial_register(&symbol_device);
  273. if (retval)
  274. return retval;
  275. retval = usb_register(&symbol_driver);
  276. if (retval)
  277. usb_serial_deregister(&symbol_device);
  278. return retval;
  279. }
  280. static void __exit symbol_exit(void)
  281. {
  282. usb_deregister(&symbol_driver);
  283. usb_serial_deregister(&symbol_device);
  284. }
  285. module_init(symbol_init);
  286. module_exit(symbol_exit);
  287. MODULE_LICENSE("GPL");
  288. module_param(debug, bool, S_IRUGO | S_IWUSR);
  289. MODULE_PARM_DESC(debug, "Debug enabled or not");