symbolserial.c 7.1 KB

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