opticon.c 11 KB

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