symbolserial.c 7.3 KB

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