opticon.c 11 KB

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