opticon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Opticon USB barcode to serial driver
  4. *
  5. * Copyright (C) 2011 - 2012 Johan Hovold <[email protected]>
  6. * Copyright (C) 2011 Martin Jansen <[email protected]>
  7. * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <[email protected]>
  8. * Copyright (C) 2008 - 2009 Novell Inc.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_driver.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/serial.h>
  16. #include <linux/module.h>
  17. #include <linux/usb.h>
  18. #include <linux/usb/serial.h>
  19. #include <linux/uaccess.h>
  20. #define CONTROL_RTS 0x02
  21. #define RESEND_CTS_STATE 0x03
  22. /* max number of write urbs in flight */
  23. #define URB_UPPER_LIMIT 8
  24. /* This driver works for the Opticon 1D barcode reader
  25. * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
  26. #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)"
  27. static const struct usb_device_id id_table[] = {
  28. { USB_DEVICE(0x065a, 0x0009) },
  29. { },
  30. };
  31. MODULE_DEVICE_TABLE(usb, id_table);
  32. /* This structure holds all of the individual device information */
  33. struct opticon_private {
  34. spinlock_t lock; /* protects the following flags */
  35. bool rts;
  36. bool cts;
  37. int outstanding_urbs;
  38. int outstanding_bytes;
  39. struct usb_anchor anchor;
  40. };
  41. static void opticon_process_data_packet(struct usb_serial_port *port,
  42. const unsigned char *buf, size_t len)
  43. {
  44. tty_insert_flip_string(&port->port, buf, len);
  45. tty_flip_buffer_push(&port->port);
  46. }
  47. static void opticon_process_status_packet(struct usb_serial_port *port,
  48. const unsigned char *buf, size_t len)
  49. {
  50. struct opticon_private *priv = usb_get_serial_port_data(port);
  51. unsigned long flags;
  52. spin_lock_irqsave(&priv->lock, flags);
  53. if (buf[0] == 0x00)
  54. priv->cts = false;
  55. else
  56. priv->cts = true;
  57. spin_unlock_irqrestore(&priv->lock, flags);
  58. }
  59. static void opticon_process_read_urb(struct urb *urb)
  60. {
  61. struct usb_serial_port *port = urb->context;
  62. const unsigned char *hdr = urb->transfer_buffer;
  63. const unsigned char *data = hdr + 2;
  64. size_t data_len = urb->actual_length - 2;
  65. if (urb->actual_length <= 2) {
  66. dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
  67. urb->actual_length);
  68. return;
  69. }
  70. /*
  71. * Data from the device comes with a 2 byte header:
  72. *
  73. * <0x00><0x00>data...
  74. * This is real data to be sent to the tty layer
  75. * <0x00><0x01>level
  76. * This is a CTS level change, the third byte is the CTS
  77. * value (0 for low, 1 for high).
  78. */
  79. if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
  80. opticon_process_data_packet(port, data, data_len);
  81. } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
  82. opticon_process_status_packet(port, data, data_len);
  83. } else {
  84. dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
  85. hdr[0], hdr[1]);
  86. }
  87. }
  88. static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
  89. u8 val)
  90. {
  91. struct usb_serial *serial = port->serial;
  92. int retval;
  93. u8 *buffer;
  94. buffer = kzalloc(1, GFP_KERNEL);
  95. if (!buffer)
  96. return -ENOMEM;
  97. buffer[0] = val;
  98. /* Send the message to the vendor control endpoint
  99. * of the connected device */
  100. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  101. requesttype,
  102. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
  103. 0, 0, buffer, 1, USB_CTRL_SET_TIMEOUT);
  104. kfree(buffer);
  105. if (retval < 0)
  106. return retval;
  107. return 0;
  108. }
  109. static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
  110. {
  111. struct opticon_private *priv = usb_get_serial_port_data(port);
  112. unsigned long flags;
  113. int res;
  114. spin_lock_irqsave(&priv->lock, flags);
  115. priv->rts = false;
  116. spin_unlock_irqrestore(&priv->lock, flags);
  117. /* Clear RTS line */
  118. send_control_msg(port, CONTROL_RTS, 0);
  119. /* clear the halt status of the endpoint */
  120. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  121. res = usb_serial_generic_open(tty, port);
  122. if (res)
  123. return res;
  124. /* Request CTS line state, sometimes during opening the current
  125. * CTS state can be missed. */
  126. send_control_msg(port, RESEND_CTS_STATE, 1);
  127. return res;
  128. }
  129. static void opticon_close(struct usb_serial_port *port)
  130. {
  131. struct opticon_private *priv = usb_get_serial_port_data(port);
  132. usb_kill_anchored_urbs(&priv->anchor);
  133. usb_serial_generic_close(port);
  134. }
  135. static void opticon_write_control_callback(struct urb *urb)
  136. {
  137. struct usb_serial_port *port = urb->context;
  138. struct opticon_private *priv = usb_get_serial_port_data(port);
  139. int status = urb->status;
  140. unsigned long flags;
  141. /* free up the transfer buffer, as usb_free_urb() does not do this */
  142. kfree(urb->transfer_buffer);
  143. /* setup packet may be set if we're using it for writing */
  144. kfree(urb->setup_packet);
  145. if (status)
  146. dev_dbg(&port->dev,
  147. "%s - non-zero urb status received: %d\n",
  148. __func__, status);
  149. spin_lock_irqsave(&priv->lock, flags);
  150. --priv->outstanding_urbs;
  151. priv->outstanding_bytes -= urb->transfer_buffer_length;
  152. spin_unlock_irqrestore(&priv->lock, flags);
  153. usb_serial_port_softint(port);
  154. }
  155. static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
  156. const unsigned char *buf, int count)
  157. {
  158. struct opticon_private *priv = usb_get_serial_port_data(port);
  159. struct usb_serial *serial = port->serial;
  160. struct urb *urb;
  161. unsigned char *buffer;
  162. unsigned long flags;
  163. struct usb_ctrlrequest *dr;
  164. int ret = -ENOMEM;
  165. spin_lock_irqsave(&priv->lock, flags);
  166. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  167. spin_unlock_irqrestore(&priv->lock, flags);
  168. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  169. return 0;
  170. }
  171. priv->outstanding_urbs++;
  172. priv->outstanding_bytes += count;
  173. spin_unlock_irqrestore(&priv->lock, flags);
  174. buffer = kmemdup(buf, count, GFP_ATOMIC);
  175. if (!buffer)
  176. goto error_no_buffer;
  177. urb = usb_alloc_urb(0, GFP_ATOMIC);
  178. if (!urb)
  179. goto error_no_urb;
  180. usb_serial_debug_data(&port->dev, __func__, count, buffer);
  181. /* The connected devices do not have a bulk write endpoint,
  182. * to transmit data to de barcode device the control endpoint is used */
  183. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  184. if (!dr)
  185. goto error_no_dr;
  186. dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
  187. dr->bRequest = 0x01;
  188. dr->wValue = 0;
  189. dr->wIndex = 0;
  190. dr->wLength = cpu_to_le16(count);
  191. usb_fill_control_urb(urb, serial->dev,
  192. usb_sndctrlpipe(serial->dev, 0),
  193. (unsigned char *)dr, buffer, count,
  194. opticon_write_control_callback, port);
  195. usb_anchor_urb(urb, &priv->anchor);
  196. /* send it down the pipe */
  197. ret = usb_submit_urb(urb, GFP_ATOMIC);
  198. if (ret) {
  199. dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
  200. usb_unanchor_urb(urb);
  201. goto error;
  202. }
  203. /* we are done with this urb, so let the host driver
  204. * really free it when it is finished with it */
  205. usb_free_urb(urb);
  206. return count;
  207. error:
  208. kfree(dr);
  209. error_no_dr:
  210. usb_free_urb(urb);
  211. error_no_urb:
  212. kfree(buffer);
  213. error_no_buffer:
  214. spin_lock_irqsave(&priv->lock, flags);
  215. --priv->outstanding_urbs;
  216. priv->outstanding_bytes -= count;
  217. spin_unlock_irqrestore(&priv->lock, flags);
  218. return ret;
  219. }
  220. static unsigned int opticon_write_room(struct tty_struct *tty)
  221. {
  222. struct usb_serial_port *port = tty->driver_data;
  223. struct opticon_private *priv = usb_get_serial_port_data(port);
  224. unsigned long flags;
  225. /*
  226. * We really can take almost anything the user throws at us
  227. * but let's pick a nice big number to tell the tty
  228. * layer that we have lots of free space, unless we don't.
  229. */
  230. spin_lock_irqsave(&priv->lock, flags);
  231. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  232. spin_unlock_irqrestore(&priv->lock, flags);
  233. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  234. return 0;
  235. }
  236. spin_unlock_irqrestore(&priv->lock, flags);
  237. return 2048;
  238. }
  239. static unsigned int opticon_chars_in_buffer(struct tty_struct *tty)
  240. {
  241. struct usb_serial_port *port = tty->driver_data;
  242. struct opticon_private *priv = usb_get_serial_port_data(port);
  243. unsigned long flags;
  244. unsigned int count;
  245. spin_lock_irqsave(&priv->lock, flags);
  246. count = priv->outstanding_bytes;
  247. spin_unlock_irqrestore(&priv->lock, flags);
  248. return count;
  249. }
  250. static int opticon_tiocmget(struct tty_struct *tty)
  251. {
  252. struct usb_serial_port *port = tty->driver_data;
  253. struct opticon_private *priv = usb_get_serial_port_data(port);
  254. unsigned long flags;
  255. int result = 0;
  256. spin_lock_irqsave(&priv->lock, flags);
  257. if (priv->rts)
  258. result |= TIOCM_RTS;
  259. if (priv->cts)
  260. result |= TIOCM_CTS;
  261. spin_unlock_irqrestore(&priv->lock, flags);
  262. dev_dbg(&port->dev, "%s - %x\n", __func__, result);
  263. return result;
  264. }
  265. static int opticon_tiocmset(struct tty_struct *tty,
  266. unsigned int set, unsigned int clear)
  267. {
  268. struct usb_serial_port *port = tty->driver_data;
  269. struct opticon_private *priv = usb_get_serial_port_data(port);
  270. unsigned long flags;
  271. bool rts;
  272. bool changed = false;
  273. int ret;
  274. /* We only support RTS so we only handle that */
  275. spin_lock_irqsave(&priv->lock, flags);
  276. rts = priv->rts;
  277. if (set & TIOCM_RTS)
  278. priv->rts = true;
  279. if (clear & TIOCM_RTS)
  280. priv->rts = false;
  281. changed = rts ^ priv->rts;
  282. spin_unlock_irqrestore(&priv->lock, flags);
  283. if (!changed)
  284. return 0;
  285. ret = send_control_msg(port, CONTROL_RTS, !rts);
  286. if (ret)
  287. return usb_translate_errors(ret);
  288. return 0;
  289. }
  290. static int opticon_port_probe(struct usb_serial_port *port)
  291. {
  292. struct opticon_private *priv;
  293. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  294. if (!priv)
  295. return -ENOMEM;
  296. spin_lock_init(&priv->lock);
  297. init_usb_anchor(&priv->anchor);
  298. usb_set_serial_port_data(port, priv);
  299. return 0;
  300. }
  301. static void opticon_port_remove(struct usb_serial_port *port)
  302. {
  303. struct opticon_private *priv = usb_get_serial_port_data(port);
  304. kfree(priv);
  305. }
  306. static struct usb_serial_driver opticon_device = {
  307. .driver = {
  308. .owner = THIS_MODULE,
  309. .name = "opticon",
  310. },
  311. .id_table = id_table,
  312. .num_ports = 1,
  313. .num_bulk_in = 1,
  314. .bulk_in_size = 256,
  315. .port_probe = opticon_port_probe,
  316. .port_remove = opticon_port_remove,
  317. .open = opticon_open,
  318. .close = opticon_close,
  319. .write = opticon_write,
  320. .write_room = opticon_write_room,
  321. .chars_in_buffer = opticon_chars_in_buffer,
  322. .throttle = usb_serial_generic_throttle,
  323. .unthrottle = usb_serial_generic_unthrottle,
  324. .tiocmget = opticon_tiocmget,
  325. .tiocmset = opticon_tiocmset,
  326. .process_read_urb = opticon_process_read_urb,
  327. };
  328. static struct usb_serial_driver * const serial_drivers[] = {
  329. &opticon_device, NULL
  330. };
  331. module_usb_serial_driver(serial_drivers, id_table);
  332. MODULE_DESCRIPTION(DRIVER_DESC);
  333. MODULE_LICENSE("GPL v2");