symbolserial.c 7.2 KB

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