opticon.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Opticon USB barcode to serial driver
  3. *
  4. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  5. * Copyright (C) 2008 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(0x065a, 0x0009) },
  23. { },
  24. };
  25. MODULE_DEVICE_TABLE(usb, id_table);
  26. /* This structure holds all of the individual device information */
  27. struct opticon_private {
  28. struct usb_device *udev;
  29. struct usb_serial *serial;
  30. struct usb_serial_port *port;
  31. unsigned char *bulk_in_buffer;
  32. struct urb *bulk_read_urb;
  33. int buffer_size;
  34. u8 bulk_address;
  35. spinlock_t lock; /* protects the following flags */
  36. bool throttled;
  37. bool actually_throttled;
  38. bool rts;
  39. };
  40. static void opticon_bulk_callback(struct urb *urb)
  41. {
  42. struct opticon_private *priv = urb->context;
  43. unsigned char *data = urb->transfer_buffer;
  44. struct usb_serial_port *port = priv->port;
  45. int status = urb->status;
  46. struct tty_struct *tty;
  47. int result;
  48. int available_room = 0;
  49. int data_length;
  50. dbg("%s - port %d", __func__, port->number);
  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. dbg("%s - urb shutting down with status: %d",
  60. __func__, status);
  61. return;
  62. default:
  63. dbg("%s - nonzero urb status received: %d",
  64. __func__, status);
  65. goto exit;
  66. }
  67. usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
  68. data);
  69. if (urb->actual_length > 2) {
  70. data_length = urb->actual_length - 2;
  71. /*
  72. * Data from the device comes with a 2 byte header:
  73. *
  74. * <0x00><0x00>data...
  75. * This is real data to be sent to the tty layer
  76. * <0x00><0x01)level
  77. * This is a RTS level change, the third byte is the RTS
  78. * value (0 for low, 1 for high).
  79. */
  80. if ((data[0] == 0x00) && (data[1] == 0x00)) {
  81. /* real data, send it to the tty layer */
  82. tty = tty_port_tty_get(&port->port);
  83. if (tty) {
  84. available_room = tty_buffer_request_room(tty,
  85. data_length);
  86. if (available_room) {
  87. tty_insert_flip_string(tty, data,
  88. available_room);
  89. tty_flip_buffer_push(tty);
  90. }
  91. tty_kref_put(tty);
  92. }
  93. } else {
  94. if ((data[0] == 0x00) && (data[1] == 0x01)) {
  95. if (data[2] == 0x00)
  96. priv->rts = false;
  97. else
  98. priv->rts = true;
  99. /* FIXME change the RTS level */
  100. } else {
  101. dev_dbg(&priv->udev->dev,
  102. "Unknown data packet received from the device:"
  103. " %2x %2x\n",
  104. data[0], data[1]);
  105. }
  106. }
  107. } else {
  108. dev_dbg(&priv->udev->dev,
  109. "Improper ammount of data received from the device, "
  110. "%d bytes", urb->actual_length);
  111. }
  112. exit:
  113. spin_lock(&priv->lock);
  114. /* Continue trying to always read if we should */
  115. if (!priv->throttled) {
  116. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  117. usb_rcvbulkpipe(priv->udev,
  118. priv->bulk_address),
  119. priv->bulk_in_buffer, priv->buffer_size,
  120. opticon_bulk_callback, priv);
  121. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  122. if (result)
  123. dev_err(&port->dev,
  124. "%s - failed resubmitting read urb, error %d\n",
  125. __func__, result);
  126. } else
  127. priv->actually_throttled = true;
  128. spin_unlock(&priv->lock);
  129. }
  130. static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port,
  131. struct file *filp)
  132. {
  133. struct opticon_private *priv = usb_get_serial_data(port->serial);
  134. unsigned long flags;
  135. int result = 0;
  136. dbg("%s - port %d", __func__, port->number);
  137. spin_lock_irqsave(&priv->lock, flags);
  138. priv->throttled = false;
  139. priv->actually_throttled = false;
  140. priv->port = port;
  141. spin_unlock_irqrestore(&priv->lock, flags);
  142. /*
  143. * Force low_latency on so that our tty_push actually forces the data
  144. * through, otherwise it is scheduled, and with high data rates (like
  145. * with OHCI) data can get lost.
  146. */
  147. if (tty)
  148. tty->low_latency = 1;
  149. /* Start reading from the device */
  150. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  151. usb_rcvbulkpipe(priv->udev,
  152. priv->bulk_address),
  153. priv->bulk_in_buffer, priv->buffer_size,
  154. opticon_bulk_callback, priv);
  155. result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
  156. if (result)
  157. dev_err(&port->dev,
  158. "%s - failed resubmitting read urb, error %d\n",
  159. __func__, result);
  160. return result;
  161. }
  162. static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port,
  163. struct file *filp)
  164. {
  165. struct opticon_private *priv = usb_get_serial_data(port->serial);
  166. dbg("%s - port %d", __func__, port->number);
  167. /* shutdown our urbs */
  168. usb_kill_urb(priv->bulk_read_urb);
  169. }
  170. static void opticon_throttle(struct tty_struct *tty)
  171. {
  172. struct usb_serial_port *port = tty->driver_data;
  173. struct opticon_private *priv = usb_get_serial_data(port->serial);
  174. unsigned long flags;
  175. dbg("%s - port %d", __func__, port->number);
  176. spin_lock_irqsave(&priv->lock, flags);
  177. priv->throttled = true;
  178. spin_unlock_irqrestore(&priv->lock, flags);
  179. }
  180. static void opticon_unthrottle(struct tty_struct *tty)
  181. {
  182. struct usb_serial_port *port = tty->driver_data;
  183. struct opticon_private *priv = usb_get_serial_data(port->serial);
  184. unsigned long flags;
  185. int result;
  186. dbg("%s - port %d", __func__, port->number);
  187. spin_lock_irqsave(&priv->lock, flags);
  188. priv->throttled = false;
  189. priv->actually_throttled = false;
  190. spin_unlock_irqrestore(&priv->lock, flags);
  191. priv->bulk_read_urb->dev = port->serial->dev;
  192. result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
  193. if (result)
  194. dev_err(&port->dev,
  195. "%s - failed submitting read urb, error %d\n",
  196. __func__, result);
  197. }
  198. static int opticon_startup(struct usb_serial *serial)
  199. {
  200. struct opticon_private *priv;
  201. struct usb_host_interface *intf;
  202. int i;
  203. int retval = -ENOMEM;
  204. bool bulk_in_found = false;
  205. /* create our private serial structure */
  206. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  207. if (priv == NULL) {
  208. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  209. return -ENOMEM;
  210. }
  211. spin_lock_init(&priv->lock);
  212. priv->serial = serial;
  213. priv->port = serial->port[0];
  214. priv->udev = serial->dev;
  215. /* find our bulk endpoint */
  216. intf = serial->interface->altsetting;
  217. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  218. struct usb_endpoint_descriptor *endpoint;
  219. endpoint = &intf->endpoint[i].desc;
  220. if (!usb_endpoint_is_bulk_in(endpoint))
  221. continue;
  222. priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
  223. if (!priv->bulk_read_urb) {
  224. dev_err(&priv->udev->dev, "out of memory\n");
  225. goto error;
  226. }
  227. priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  228. priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  229. if (!priv->bulk_in_buffer) {
  230. dev_err(&priv->udev->dev, "out of memory\n");
  231. goto error;
  232. }
  233. priv->bulk_address = endpoint->bEndpointAddress;
  234. /* set up our bulk urb */
  235. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  236. usb_rcvbulkpipe(priv->udev,
  237. endpoint->bEndpointAddress),
  238. priv->bulk_in_buffer, priv->buffer_size,
  239. opticon_bulk_callback, priv);
  240. bulk_in_found = true;
  241. break;
  242. }
  243. if (!bulk_in_found) {
  244. dev_err(&priv->udev->dev,
  245. "Error - the proper endpoints were not found!\n");
  246. goto error;
  247. }
  248. usb_set_serial_data(serial, priv);
  249. return 0;
  250. error:
  251. usb_free_urb(priv->bulk_read_urb);
  252. kfree(priv->bulk_in_buffer);
  253. kfree(priv);
  254. return retval;
  255. }
  256. static void opticon_shutdown(struct usb_serial *serial)
  257. {
  258. struct opticon_private *priv = usb_get_serial_data(serial);
  259. dbg("%s", __func__);
  260. usb_kill_urb(priv->bulk_read_urb);
  261. usb_free_urb(priv->bulk_read_urb);
  262. kfree(priv->bulk_in_buffer);
  263. kfree(priv);
  264. usb_set_serial_data(serial, NULL);
  265. }
  266. static struct usb_driver opticon_driver = {
  267. .name = "opticon",
  268. .probe = usb_serial_probe,
  269. .disconnect = usb_serial_disconnect,
  270. .id_table = id_table,
  271. .no_dynamic_id = 1,
  272. };
  273. static struct usb_serial_driver opticon_device = {
  274. .driver = {
  275. .owner = THIS_MODULE,
  276. .name = "opticon",
  277. },
  278. .id_table = id_table,
  279. .usb_driver = &opticon_driver,
  280. .num_ports = 1,
  281. .attach = opticon_startup,
  282. .open = opticon_open,
  283. .close = opticon_close,
  284. .shutdown = opticon_shutdown,
  285. .throttle = opticon_throttle,
  286. .unthrottle = opticon_unthrottle,
  287. };
  288. static int __init opticon_init(void)
  289. {
  290. int retval;
  291. retval = usb_serial_register(&opticon_device);
  292. if (retval)
  293. return retval;
  294. retval = usb_register(&opticon_driver);
  295. if (retval)
  296. usb_serial_deregister(&opticon_device);
  297. return retval;
  298. }
  299. static void __exit opticon_exit(void)
  300. {
  301. usb_deregister(&opticon_driver);
  302. usb_serial_deregister(&opticon_device);
  303. }
  304. module_init(opticon_init);
  305. module_exit(opticon_exit);
  306. MODULE_LICENSE("GPL");
  307. module_param(debug, bool, S_IRUGO | S_IWUSR);
  308. MODULE_PARM_DESC(debug, "Debug enabled or not");