opticon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Opticon USB barcode to serial driver
  3. *
  4. * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
  5. * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
  6. * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (C) 2008 - 2009 Novell Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/slab.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/serial.h>
  19. #include <linux/module.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/serial.h>
  22. #include <linux/uaccess.h>
  23. #define CONTROL_RTS 0x02
  24. #define RESEND_CTS_STATE 0x03
  25. /* max number of write urbs in flight */
  26. #define URB_UPPER_LIMIT 8
  27. /* This driver works for the Opticon 1D barcode reader
  28. * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
  29. #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)"
  30. static const struct usb_device_id id_table[] = {
  31. { USB_DEVICE(0x065a, 0x0009) },
  32. { },
  33. };
  34. MODULE_DEVICE_TABLE(usb, id_table);
  35. /* This structure holds all of the individual device information */
  36. struct opticon_private {
  37. spinlock_t lock; /* protects the following flags */
  38. bool rts;
  39. bool cts;
  40. int outstanding_urbs;
  41. };
  42. static void opticon_process_data_packet(struct usb_serial_port *port,
  43. const unsigned char *buf, size_t len)
  44. {
  45. tty_insert_flip_string(&port->port, buf, len);
  46. tty_flip_buffer_push(&port->port);
  47. }
  48. static void opticon_process_status_packet(struct usb_serial_port *port,
  49. const unsigned char *buf, size_t len)
  50. {
  51. struct opticon_private *priv = usb_get_serial_port_data(port);
  52. unsigned long flags;
  53. spin_lock_irqsave(&priv->lock, flags);
  54. if (buf[0] == 0x00)
  55. priv->cts = false;
  56. else
  57. priv->cts = true;
  58. spin_unlock_irqrestore(&priv->lock, flags);
  59. }
  60. static void opticon_process_read_urb(struct urb *urb)
  61. {
  62. struct usb_serial_port *port = urb->context;
  63. const unsigned char *hdr = urb->transfer_buffer;
  64. const unsigned char *data = hdr + 2;
  65. size_t data_len = urb->actual_length - 2;
  66. if (urb->actual_length <= 2) {
  67. dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
  68. urb->actual_length);
  69. return;
  70. }
  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 CTS level change, the third byte is the CTS
  78. * value (0 for low, 1 for high).
  79. */
  80. if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
  81. opticon_process_data_packet(port, data, data_len);
  82. } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
  83. opticon_process_status_packet(port, data, data_len);
  84. } else {
  85. dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
  86. hdr[0], hdr[1]);
  87. }
  88. }
  89. static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
  90. u8 val)
  91. {
  92. struct usb_serial *serial = port->serial;
  93. int retval;
  94. u8 *buffer;
  95. buffer = kzalloc(1, GFP_KERNEL);
  96. if (!buffer)
  97. return -ENOMEM;
  98. buffer[0] = val;
  99. /* Send the message to the vendor control endpoint
  100. * of the connected device */
  101. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  102. requesttype,
  103. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
  104. 0, 0, buffer, 1, 0);
  105. kfree(buffer);
  106. if (retval < 0)
  107. return retval;
  108. return 0;
  109. }
  110. static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
  111. {
  112. struct opticon_private *priv = usb_get_serial_port_data(port);
  113. unsigned long flags;
  114. int res;
  115. spin_lock_irqsave(&priv->lock, flags);
  116. priv->rts = false;
  117. spin_unlock_irqrestore(&priv->lock, flags);
  118. /* Clear RTS line */
  119. send_control_msg(port, CONTROL_RTS, 0);
  120. /* clear the halt status of the endpoint */
  121. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  122. res = usb_serial_generic_open(tty, port);
  123. if (res)
  124. return res;
  125. /* Request CTS line state, sometimes during opening the current
  126. * CTS state can be missed. */
  127. send_control_msg(port, RESEND_CTS_STATE, 1);
  128. return res;
  129. }
  130. static void opticon_write_control_callback(struct urb *urb)
  131. {
  132. struct usb_serial_port *port = urb->context;
  133. struct opticon_private *priv = usb_get_serial_port_data(port);
  134. int status = urb->status;
  135. unsigned long flags;
  136. /* free up the transfer buffer, as usb_free_urb() does not do this */
  137. kfree(urb->transfer_buffer);
  138. /* setup packet may be set if we're using it for writing */
  139. kfree(urb->setup_packet);
  140. if (status)
  141. dev_dbg(&port->dev,
  142. "%s - non-zero urb status received: %d\n",
  143. __func__, status);
  144. spin_lock_irqsave(&priv->lock, flags);
  145. --priv->outstanding_urbs;
  146. spin_unlock_irqrestore(&priv->lock, flags);
  147. usb_serial_port_softint(port);
  148. }
  149. static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
  150. const unsigned char *buf, int count)
  151. {
  152. struct opticon_private *priv = usb_get_serial_port_data(port);
  153. struct usb_serial *serial = port->serial;
  154. struct urb *urb;
  155. unsigned char *buffer;
  156. unsigned long flags;
  157. int status;
  158. struct usb_ctrlrequest *dr;
  159. spin_lock_irqsave(&priv->lock, flags);
  160. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  161. spin_unlock_irqrestore(&priv->lock, flags);
  162. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  163. return 0;
  164. }
  165. priv->outstanding_urbs++;
  166. spin_unlock_irqrestore(&priv->lock, flags);
  167. buffer = kmalloc(count, GFP_ATOMIC);
  168. if (!buffer) {
  169. count = -ENOMEM;
  170. goto error_no_buffer;
  171. }
  172. urb = usb_alloc_urb(0, GFP_ATOMIC);
  173. if (!urb) {
  174. count = -ENOMEM;
  175. goto error_no_urb;
  176. }
  177. memcpy(buffer, buf, count);
  178. usb_serial_debug_data(&port->dev, __func__, count, buffer);
  179. /* The connected devices do not have a bulk write endpoint,
  180. * to transmit data to de barcode device the control endpoint is used */
  181. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  182. if (!dr) {
  183. count = -ENOMEM;
  184. goto error_no_dr;
  185. }
  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. /* send it down the pipe */
  196. status = usb_submit_urb(urb, GFP_ATOMIC);
  197. if (status) {
  198. dev_err(&port->dev,
  199. "%s - usb_submit_urb(write endpoint) failed status = %d\n",
  200. __func__, status);
  201. count = status;
  202. goto error;
  203. }
  204. /* we are done with this urb, so let the host driver
  205. * really free it when it is finished with it */
  206. usb_free_urb(urb);
  207. return count;
  208. error:
  209. kfree(dr);
  210. error_no_dr:
  211. usb_free_urb(urb);
  212. error_no_urb:
  213. kfree(buffer);
  214. error_no_buffer:
  215. spin_lock_irqsave(&priv->lock, flags);
  216. --priv->outstanding_urbs;
  217. spin_unlock_irqrestore(&priv->lock, flags);
  218. return count;
  219. }
  220. static 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 int opticon_tiocmget(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. int result = 0;
  245. spin_lock_irqsave(&priv->lock, flags);
  246. if (priv->rts)
  247. result |= TIOCM_RTS;
  248. if (priv->cts)
  249. result |= TIOCM_CTS;
  250. spin_unlock_irqrestore(&priv->lock, flags);
  251. dev_dbg(&port->dev, "%s - %x\n", __func__, result);
  252. return result;
  253. }
  254. static int opticon_tiocmset(struct tty_struct *tty,
  255. unsigned int set, unsigned int clear)
  256. {
  257. struct usb_serial_port *port = tty->driver_data;
  258. struct opticon_private *priv = usb_get_serial_port_data(port);
  259. unsigned long flags;
  260. bool rts;
  261. bool changed = false;
  262. int ret;
  263. /* We only support RTS so we only handle that */
  264. spin_lock_irqsave(&priv->lock, flags);
  265. rts = priv->rts;
  266. if (set & TIOCM_RTS)
  267. priv->rts = true;
  268. if (clear & TIOCM_RTS)
  269. priv->rts = false;
  270. changed = rts ^ priv->rts;
  271. spin_unlock_irqrestore(&priv->lock, flags);
  272. if (!changed)
  273. return 0;
  274. ret = send_control_msg(port, CONTROL_RTS, !rts);
  275. if (ret)
  276. return usb_translate_errors(ret);
  277. return 0;
  278. }
  279. static int get_serial_info(struct usb_serial_port *port,
  280. struct serial_struct __user *serial)
  281. {
  282. struct serial_struct tmp;
  283. if (!serial)
  284. return -EFAULT;
  285. memset(&tmp, 0x00, sizeof(tmp));
  286. /* fake emulate a 16550 uart to make userspace code happy */
  287. tmp.type = PORT_16550A;
  288. tmp.line = port->minor;
  289. tmp.port = 0;
  290. tmp.irq = 0;
  291. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  292. tmp.xmit_fifo_size = 1024;
  293. tmp.baud_base = 9600;
  294. tmp.close_delay = 5*HZ;
  295. tmp.closing_wait = 30*HZ;
  296. if (copy_to_user(serial, &tmp, sizeof(*serial)))
  297. return -EFAULT;
  298. return 0;
  299. }
  300. static int opticon_ioctl(struct tty_struct *tty,
  301. unsigned int cmd, unsigned long arg)
  302. {
  303. struct usb_serial_port *port = tty->driver_data;
  304. switch (cmd) {
  305. case TIOCGSERIAL:
  306. return get_serial_info(port,
  307. (struct serial_struct __user *)arg);
  308. }
  309. return -ENOIOCTLCMD;
  310. }
  311. static int opticon_startup(struct usb_serial *serial)
  312. {
  313. if (!serial->num_bulk_in) {
  314. dev_err(&serial->dev->dev, "no bulk in endpoint\n");
  315. return -ENODEV;
  316. }
  317. return 0;
  318. }
  319. static int opticon_port_probe(struct usb_serial_port *port)
  320. {
  321. struct opticon_private *priv;
  322. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  323. if (!priv)
  324. return -ENOMEM;
  325. spin_lock_init(&priv->lock);
  326. usb_set_serial_port_data(port, priv);
  327. return 0;
  328. }
  329. static int opticon_port_remove(struct usb_serial_port *port)
  330. {
  331. struct opticon_private *priv = usb_get_serial_port_data(port);
  332. kfree(priv);
  333. return 0;
  334. }
  335. static struct usb_serial_driver opticon_device = {
  336. .driver = {
  337. .owner = THIS_MODULE,
  338. .name = "opticon",
  339. },
  340. .id_table = id_table,
  341. .num_ports = 1,
  342. .bulk_in_size = 256,
  343. .attach = opticon_startup,
  344. .port_probe = opticon_port_probe,
  345. .port_remove = opticon_port_remove,
  346. .open = opticon_open,
  347. .write = opticon_write,
  348. .write_room = opticon_write_room,
  349. .throttle = usb_serial_generic_throttle,
  350. .unthrottle = usb_serial_generic_unthrottle,
  351. .ioctl = opticon_ioctl,
  352. .tiocmget = opticon_tiocmget,
  353. .tiocmset = opticon_tiocmset,
  354. .process_read_urb = opticon_process_read_urb,
  355. };
  356. static struct usb_serial_driver * const serial_drivers[] = {
  357. &opticon_device, NULL
  358. };
  359. module_usb_serial_driver(serial_drivers, id_table);
  360. MODULE_DESCRIPTION(DRIVER_DESC);
  361. MODULE_LICENSE("GPL");