opticon.c 10 KB

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