opticon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. return retval;
  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 enpoint */
  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_write_control_callback(struct urb *urb)
  130. {
  131. struct usb_serial_port *port = urb->context;
  132. struct opticon_private *priv = usb_get_serial_port_data(port);
  133. int status = urb->status;
  134. unsigned long flags;
  135. /* free up the transfer buffer, as usb_free_urb() does not do this */
  136. kfree(urb->transfer_buffer);
  137. /* setup packet may be set if we're using it for writing */
  138. kfree(urb->setup_packet);
  139. if (status)
  140. dev_dbg(&port->dev,
  141. "%s - non-zero urb status received: %d\n",
  142. __func__, status);
  143. spin_lock_irqsave(&priv->lock, flags);
  144. --priv->outstanding_urbs;
  145. spin_unlock_irqrestore(&priv->lock, flags);
  146. usb_serial_port_softint(port);
  147. }
  148. static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
  149. const unsigned char *buf, int count)
  150. {
  151. struct opticon_private *priv = usb_get_serial_port_data(port);
  152. struct usb_serial *serial = port->serial;
  153. struct urb *urb;
  154. unsigned char *buffer;
  155. unsigned long flags;
  156. int status;
  157. struct usb_ctrlrequest *dr;
  158. spin_lock_irqsave(&priv->lock, flags);
  159. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  160. spin_unlock_irqrestore(&priv->lock, flags);
  161. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  162. return 0;
  163. }
  164. priv->outstanding_urbs++;
  165. spin_unlock_irqrestore(&priv->lock, flags);
  166. buffer = kmalloc(count, GFP_ATOMIC);
  167. if (!buffer) {
  168. dev_err(&port->dev, "out of memory\n");
  169. count = -ENOMEM;
  170. goto error_no_buffer;
  171. }
  172. urb = usb_alloc_urb(0, GFP_ATOMIC);
  173. if (!urb) {
  174. dev_err(&port->dev, "no more free urbs\n");
  175. count = -ENOMEM;
  176. goto error_no_urb;
  177. }
  178. memcpy(buffer, buf, count);
  179. usb_serial_debug_data(&port->dev, __func__, count, buffer);
  180. /* The conncected devices do not have a bulk write endpoint,
  181. * to transmit data to de barcode device the control endpoint is used */
  182. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
  183. if (!dr) {
  184. dev_err(&port->dev, "out of memory\n");
  185. count = -ENOMEM;
  186. goto error_no_dr;
  187. }
  188. dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
  189. dr->bRequest = 0x01;
  190. dr->wValue = 0;
  191. dr->wIndex = 0;
  192. dr->wLength = cpu_to_le16(count);
  193. usb_fill_control_urb(urb, serial->dev,
  194. usb_sndctrlpipe(serial->dev, 0),
  195. (unsigned char *)dr, buffer, count,
  196. opticon_write_control_callback, port);
  197. /* send it down the pipe */
  198. status = usb_submit_urb(urb, GFP_ATOMIC);
  199. if (status) {
  200. dev_err(&port->dev,
  201. "%s - usb_submit_urb(write endpoint) failed status = %d\n",
  202. __func__, status);
  203. count = status;
  204. goto error;
  205. }
  206. /* we are done with this urb, so let the host driver
  207. * really free it when it is finished with it */
  208. usb_free_urb(urb);
  209. return count;
  210. error:
  211. kfree(dr);
  212. error_no_dr:
  213. usb_free_urb(urb);
  214. error_no_urb:
  215. kfree(buffer);
  216. error_no_buffer:
  217. spin_lock_irqsave(&priv->lock, flags);
  218. --priv->outstanding_urbs;
  219. spin_unlock_irqrestore(&priv->lock, flags);
  220. return count;
  221. }
  222. static int opticon_write_room(struct tty_struct *tty)
  223. {
  224. struct usb_serial_port *port = tty->driver_data;
  225. struct opticon_private *priv = usb_get_serial_port_data(port);
  226. unsigned long flags;
  227. /*
  228. * We really can take almost anything the user throws at us
  229. * but let's pick a nice big number to tell the tty
  230. * layer that we have lots of free space, unless we don't.
  231. */
  232. spin_lock_irqsave(&priv->lock, flags);
  233. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  234. spin_unlock_irqrestore(&priv->lock, flags);
  235. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  236. return 0;
  237. }
  238. spin_unlock_irqrestore(&priv->lock, flags);
  239. return 2048;
  240. }
  241. static int opticon_tiocmget(struct tty_struct *tty)
  242. {
  243. struct usb_serial_port *port = tty->driver_data;
  244. struct opticon_private *priv = usb_get_serial_port_data(port);
  245. unsigned long flags;
  246. int result = 0;
  247. spin_lock_irqsave(&priv->lock, flags);
  248. if (priv->rts)
  249. result |= TIOCM_RTS;
  250. if (priv->cts)
  251. result |= TIOCM_CTS;
  252. spin_unlock_irqrestore(&priv->lock, flags);
  253. dev_dbg(&port->dev, "%s - %x\n", __func__, result);
  254. return result;
  255. }
  256. static int opticon_tiocmset(struct tty_struct *tty,
  257. unsigned int set, unsigned int clear)
  258. {
  259. struct usb_serial_port *port = tty->driver_data;
  260. struct usb_serial *serial = port->serial;
  261. struct opticon_private *priv = usb_get_serial_port_data(port);
  262. unsigned long flags;
  263. bool rts;
  264. bool changed = false;
  265. int ret;
  266. /* We only support RTS so we only handle that */
  267. spin_lock_irqsave(&priv->lock, flags);
  268. rts = priv->rts;
  269. if (set & TIOCM_RTS)
  270. priv->rts = true;
  271. if (clear & TIOCM_RTS)
  272. priv->rts = false;
  273. changed = rts ^ priv->rts;
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. if (!changed)
  276. return 0;
  277. /* Send the new RTS state to the connected device */
  278. mutex_lock(&serial->disc_mutex);
  279. if (!serial->disconnected)
  280. ret = send_control_msg(port, CONTROL_RTS, !rts);
  281. else
  282. ret = -ENODEV;
  283. mutex_unlock(&serial->disc_mutex);
  284. return ret;
  285. }
  286. static int get_serial_info(struct usb_serial_port *port,
  287. struct serial_struct __user *serial)
  288. {
  289. struct serial_struct tmp;
  290. if (!serial)
  291. return -EFAULT;
  292. memset(&tmp, 0x00, sizeof(tmp));
  293. /* fake emulate a 16550 uart to make userspace code happy */
  294. tmp.type = PORT_16550A;
  295. tmp.line = port->serial->minor;
  296. tmp.port = 0;
  297. tmp.irq = 0;
  298. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  299. tmp.xmit_fifo_size = 1024;
  300. tmp.baud_base = 9600;
  301. tmp.close_delay = 5*HZ;
  302. tmp.closing_wait = 30*HZ;
  303. if (copy_to_user(serial, &tmp, sizeof(*serial)))
  304. return -EFAULT;
  305. return 0;
  306. }
  307. static int opticon_ioctl(struct tty_struct *tty,
  308. unsigned int cmd, unsigned long arg)
  309. {
  310. struct usb_serial_port *port = tty->driver_data;
  311. dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
  312. switch (cmd) {
  313. case TIOCGSERIAL:
  314. return get_serial_info(port,
  315. (struct serial_struct __user *)arg);
  316. }
  317. return -ENOIOCTLCMD;
  318. }
  319. static int opticon_startup(struct usb_serial *serial)
  320. {
  321. if (!serial->num_bulk_in) {
  322. dev_err(&serial->dev->dev, "no bulk in endpoint\n");
  323. return -ENODEV;
  324. }
  325. return 0;
  326. }
  327. static int opticon_port_probe(struct usb_serial_port *port)
  328. {
  329. struct opticon_private *priv;
  330. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  331. if (!priv)
  332. return -ENOMEM;
  333. spin_lock_init(&priv->lock);
  334. usb_set_serial_port_data(port, priv);
  335. return 0;
  336. }
  337. static int opticon_port_remove(struct usb_serial_port *port)
  338. {
  339. struct opticon_private *priv = usb_get_serial_port_data(port);
  340. kfree(priv);
  341. return 0;
  342. }
  343. static struct usb_serial_driver opticon_device = {
  344. .driver = {
  345. .owner = THIS_MODULE,
  346. .name = "opticon",
  347. },
  348. .id_table = id_table,
  349. .num_ports = 1,
  350. .bulk_in_size = 256,
  351. .attach = opticon_startup,
  352. .port_probe = opticon_port_probe,
  353. .port_remove = opticon_port_remove,
  354. .open = opticon_open,
  355. .write = opticon_write,
  356. .write_room = opticon_write_room,
  357. .throttle = usb_serial_generic_throttle,
  358. .unthrottle = usb_serial_generic_unthrottle,
  359. .ioctl = opticon_ioctl,
  360. .tiocmget = opticon_tiocmget,
  361. .tiocmset = opticon_tiocmset,
  362. .process_read_urb = opticon_process_read_urb,
  363. };
  364. static struct usb_serial_driver * const serial_drivers[] = {
  365. &opticon_device, NULL
  366. };
  367. module_usb_serial_driver(serial_drivers, id_table);
  368. MODULE_DESCRIPTION(DRIVER_DESC);
  369. MODULE_LICENSE("GPL");