symbolserial.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 tty_struct *tty, struct usb_serial_port *port,
  139. struct file *filp)
  140. {
  141. struct symbol_private *priv = usb_get_serial_data(port->serial);
  142. dbg("%s - port %d", __func__, port->number);
  143. /* shutdown our urbs */
  144. usb_kill_urb(priv->int_urb);
  145. }
  146. static void symbol_throttle(struct tty_struct *tty)
  147. {
  148. struct usb_serial_port *port = tty->driver_data;
  149. struct symbol_private *priv = usb_get_serial_data(port->serial);
  150. unsigned long flags;
  151. dbg("%s - port %d", __func__, port->number);
  152. spin_lock_irqsave(&priv->lock, flags);
  153. priv->throttled = true;
  154. spin_unlock_irqrestore(&priv->lock, flags);
  155. }
  156. static void symbol_unthrottle(struct tty_struct *tty)
  157. {
  158. struct usb_serial_port *port = tty->driver_data;
  159. struct symbol_private *priv = usb_get_serial_data(port->serial);
  160. unsigned long flags;
  161. int result;
  162. dbg("%s - port %d", __func__, port->number);
  163. spin_lock_irqsave(&priv->lock, flags);
  164. priv->throttled = false;
  165. priv->actually_throttled = false;
  166. spin_unlock_irqrestore(&priv->lock, flags);
  167. priv->int_urb->dev = port->serial->dev;
  168. result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
  169. if (result)
  170. dev_err(&port->dev,
  171. "%s - failed submitting read urb, error %d\n",
  172. __func__, result);
  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_shutdown(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. kfree(priv->int_buffer);
  240. kfree(priv);
  241. usb_set_serial_data(serial, NULL);
  242. }
  243. static struct usb_driver symbol_driver = {
  244. .name = "symbol",
  245. .probe = usb_serial_probe,
  246. .disconnect = usb_serial_disconnect,
  247. .id_table = id_table,
  248. .no_dynamic_id = 1,
  249. };
  250. static struct usb_serial_driver symbol_device = {
  251. .driver = {
  252. .owner = THIS_MODULE,
  253. .name = "symbol",
  254. },
  255. .id_table = id_table,
  256. .usb_driver = &symbol_driver,
  257. .num_ports = 1,
  258. .attach = symbol_startup,
  259. .open = symbol_open,
  260. .close = symbol_close,
  261. .shutdown = symbol_shutdown,
  262. .throttle = symbol_throttle,
  263. .unthrottle = symbol_unthrottle,
  264. };
  265. static int __init symbol_init(void)
  266. {
  267. int retval;
  268. retval = usb_serial_register(&symbol_device);
  269. if (retval)
  270. return retval;
  271. retval = usb_register(&symbol_driver);
  272. if (retval)
  273. usb_serial_deregister(&symbol_device);
  274. return retval;
  275. }
  276. static void __exit symbol_exit(void)
  277. {
  278. usb_deregister(&symbol_driver);
  279. usb_serial_deregister(&symbol_device);
  280. }
  281. module_init(symbol_init);
  282. module_exit(symbol_exit);
  283. MODULE_LICENSE("GPL");
  284. module_param(debug, bool, S_IRUGO | S_IWUSR);
  285. MODULE_PARM_DESC(debug, "Debug enabled or not");