symbolserial.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. 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. tty_insert_flip_string(tty, &data[1], data_length);
  83. tty_flip_buffer_push(tty);
  84. tty_kref_put(tty);
  85. }
  86. } else {
  87. dev_dbg(&priv->udev->dev,
  88. "Improper amount of data received from the device, "
  89. "%d bytes", urb->actual_length);
  90. }
  91. exit:
  92. spin_lock(&priv->lock);
  93. /* Continue trying to always read if we should */
  94. if (!priv->throttled) {
  95. usb_fill_int_urb(priv->int_urb, priv->udev,
  96. usb_rcvintpipe(priv->udev,
  97. priv->int_address),
  98. priv->int_buffer, priv->buffer_size,
  99. symbol_int_callback, priv, priv->bInterval);
  100. result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
  101. if (result)
  102. dev_err(&port->dev,
  103. "%s - failed resubmitting read urb, error %d\n",
  104. __func__, result);
  105. } else
  106. priv->actually_throttled = true;
  107. spin_unlock(&priv->lock);
  108. }
  109. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  110. {
  111. struct symbol_private *priv = usb_get_serial_data(port->serial);
  112. unsigned long flags;
  113. int result = 0;
  114. dbg("%s - port %d", __func__, port->number);
  115. spin_lock_irqsave(&priv->lock, flags);
  116. priv->throttled = false;
  117. priv->actually_throttled = false;
  118. priv->port = port;
  119. spin_unlock_irqrestore(&priv->lock, flags);
  120. /* Start reading from the device */
  121. usb_fill_int_urb(priv->int_urb, priv->udev,
  122. usb_rcvintpipe(priv->udev, priv->int_address),
  123. priv->int_buffer, priv->buffer_size,
  124. symbol_int_callback, priv, priv->bInterval);
  125. result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
  126. if (result)
  127. dev_err(&port->dev,
  128. "%s - failed resubmitting read urb, error %d\n",
  129. __func__, result);
  130. return result;
  131. }
  132. static void symbol_close(struct usb_serial_port *port)
  133. {
  134. struct symbol_private *priv = usb_get_serial_data(port->serial);
  135. dbg("%s - port %d", __func__, port->number);
  136. /* shutdown our urbs */
  137. usb_kill_urb(priv->int_urb);
  138. }
  139. static void symbol_throttle(struct tty_struct *tty)
  140. {
  141. struct usb_serial_port *port = tty->driver_data;
  142. struct symbol_private *priv = usb_get_serial_data(port->serial);
  143. dbg("%s - port %d", __func__, port->number);
  144. spin_lock_irq(&priv->lock);
  145. priv->throttled = true;
  146. spin_unlock_irq(&priv->lock);
  147. }
  148. static void symbol_unthrottle(struct tty_struct *tty)
  149. {
  150. struct usb_serial_port *port = tty->driver_data;
  151. struct symbol_private *priv = usb_get_serial_data(port->serial);
  152. int result;
  153. bool was_throttled;
  154. dbg("%s - port %d", __func__, port->number);
  155. spin_lock_irq(&priv->lock);
  156. priv->throttled = false;
  157. was_throttled = priv->actually_throttled;
  158. priv->actually_throttled = false;
  159. spin_unlock_irq(&priv->lock);
  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 = usb_endpoint_maxp(endpoint) * 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. };
  247. static struct usb_serial_driver symbol_device = {
  248. .driver = {
  249. .owner = THIS_MODULE,
  250. .name = "symbol",
  251. },
  252. .id_table = id_table,
  253. .num_ports = 1,
  254. .attach = symbol_startup,
  255. .open = symbol_open,
  256. .close = symbol_close,
  257. .disconnect = symbol_disconnect,
  258. .release = symbol_release,
  259. .throttle = symbol_throttle,
  260. .unthrottle = symbol_unthrottle,
  261. };
  262. static struct usb_serial_driver * const serial_drivers[] = {
  263. &symbol_device, NULL
  264. };
  265. module_usb_serial_driver(symbol_driver, serial_drivers);
  266. MODULE_LICENSE("GPL");
  267. module_param(debug, bool, S_IRUGO | S_IWUSR);
  268. MODULE_PARM_DESC(debug, "Debug enabled or not");