opticon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Opticon USB barcode to serial driver
  3. *
  4. * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
  5. * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  6. * Copyright (C) 2008 - 2009 Novell Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.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. struct urb *bulk_read_urb;
  38. spinlock_t lock; /* protects the following flags */
  39. bool throttled;
  40. bool actually_throttled;
  41. bool rts;
  42. bool cts;
  43. int outstanding_urbs;
  44. };
  45. static void opticon_process_data_packet(struct usb_serial_port *port,
  46. const unsigned char *buf, size_t len)
  47. {
  48. struct tty_struct *tty;
  49. tty = tty_port_tty_get(&port->port);
  50. if (!tty)
  51. return;
  52. tty_insert_flip_string(tty, buf, len);
  53. tty_flip_buffer_push(tty);
  54. tty_kref_put(tty);
  55. }
  56. static void opticon_process_status_packet(struct usb_serial_port *port,
  57. const unsigned char *buf, size_t len)
  58. {
  59. struct opticon_private *priv = usb_get_serial_port_data(port);
  60. unsigned long flags;
  61. spin_lock_irqsave(&priv->lock, flags);
  62. if (buf[0] == 0x00)
  63. priv->cts = false;
  64. else
  65. priv->cts = true;
  66. spin_unlock_irqrestore(&priv->lock, flags);
  67. }
  68. static void opticon_process_read_urb(struct urb *urb)
  69. {
  70. struct usb_serial_port *port = urb->context;
  71. const unsigned char *hdr = urb->transfer_buffer;
  72. const unsigned char *data = hdr + 2;
  73. size_t data_len = urb->actual_length - 2;
  74. if (urb->actual_length <= 2) {
  75. dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
  76. urb->actual_length);
  77. return;
  78. }
  79. /*
  80. * Data from the device comes with a 2 byte header:
  81. *
  82. * <0x00><0x00>data...
  83. * This is real data to be sent to the tty layer
  84. * <0x00><0x01>level
  85. * This is a CTS level change, the third byte is the CTS
  86. * value (0 for low, 1 for high).
  87. */
  88. if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
  89. opticon_process_data_packet(port, data, data_len);
  90. } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
  91. opticon_process_status_packet(port, data, data_len);
  92. } else {
  93. dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
  94. hdr[0], hdr[1]);
  95. }
  96. }
  97. static void opticon_read_bulk_callback(struct urb *urb)
  98. {
  99. struct usb_serial_port *port = urb->context;
  100. struct opticon_private *priv = usb_get_serial_port_data(port);
  101. unsigned char *data = urb->transfer_buffer;
  102. int status = urb->status;
  103. int result;
  104. switch (status) {
  105. case 0:
  106. /* success */
  107. break;
  108. case -ECONNRESET:
  109. case -ENOENT:
  110. case -ESHUTDOWN:
  111. /* this urb is terminated, clean up */
  112. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  113. __func__, status);
  114. return;
  115. default:
  116. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  117. __func__, status);
  118. goto exit;
  119. }
  120. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  121. opticon_process_read_urb(urb);
  122. exit:
  123. spin_lock(&priv->lock);
  124. /* Continue trying to always read if we should */
  125. if (!priv->throttled) {
  126. result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
  127. if (result)
  128. dev_err(&port->dev,
  129. "%s - failed resubmitting read urb, error %d\n",
  130. __func__, result);
  131. } else
  132. priv->actually_throttled = true;
  133. spin_unlock(&priv->lock);
  134. }
  135. static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
  136. u8 val)
  137. {
  138. struct usb_serial *serial = port->serial;
  139. int retval;
  140. u8 *buffer;
  141. buffer = kzalloc(1, GFP_KERNEL);
  142. if (!buffer)
  143. return -ENOMEM;
  144. buffer[0] = val;
  145. /* Send the message to the vendor control endpoint
  146. * of the connected device */
  147. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  148. requesttype,
  149. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
  150. 0, 0, buffer, 1, 0);
  151. kfree(buffer);
  152. return retval;
  153. }
  154. static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
  155. {
  156. struct opticon_private *priv = usb_get_serial_port_data(port);
  157. unsigned long flags;
  158. int result = 0;
  159. spin_lock_irqsave(&priv->lock, flags);
  160. priv->throttled = false;
  161. priv->actually_throttled = false;
  162. priv->rts = false;
  163. spin_unlock_irqrestore(&priv->lock, flags);
  164. /* Clear RTS line */
  165. send_control_msg(port, CONTROL_RTS, 0);
  166. /* clear the halt status of the enpoint */
  167. usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe);
  168. result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
  169. if (result)
  170. dev_err(&port->dev,
  171. "%s - failed resubmitting read urb, error %d\n",
  172. __func__, result);
  173. /* Request CTS line state, sometimes during opening the current
  174. * CTS state can be missed. */
  175. send_control_msg(port, RESEND_CTS_STATE, 1);
  176. return result;
  177. }
  178. static void opticon_close(struct usb_serial_port *port)
  179. {
  180. struct opticon_private *priv = usb_get_serial_port_data(port);
  181. /* shutdown our urbs */
  182. usb_kill_urb(priv->bulk_read_urb);
  183. }
  184. static void opticon_write_control_callback(struct urb *urb)
  185. {
  186. struct usb_serial_port *port = urb->context;
  187. struct opticon_private *priv = usb_get_serial_port_data(port);
  188. int status = urb->status;
  189. unsigned long flags;
  190. /* free up the transfer buffer, as usb_free_urb() does not do this */
  191. kfree(urb->transfer_buffer);
  192. /* setup packet may be set if we're using it for writing */
  193. kfree(urb->setup_packet);
  194. if (status)
  195. dev_dbg(&port->dev,
  196. "%s - non-zero urb status received: %d\n",
  197. __func__, status);
  198. spin_lock_irqsave(&priv->lock, flags);
  199. --priv->outstanding_urbs;
  200. spin_unlock_irqrestore(&priv->lock, flags);
  201. usb_serial_port_softint(port);
  202. }
  203. static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
  204. const unsigned char *buf, int count)
  205. {
  206. struct opticon_private *priv = usb_get_serial_port_data(port);
  207. struct usb_serial *serial = port->serial;
  208. struct urb *urb;
  209. unsigned char *buffer;
  210. unsigned long flags;
  211. int status;
  212. struct usb_ctrlrequest *dr;
  213. spin_lock_irqsave(&priv->lock, flags);
  214. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  215. spin_unlock_irqrestore(&priv->lock, flags);
  216. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  217. return 0;
  218. }
  219. priv->outstanding_urbs++;
  220. spin_unlock_irqrestore(&priv->lock, flags);
  221. buffer = kmalloc(count, GFP_ATOMIC);
  222. if (!buffer) {
  223. dev_err(&port->dev, "out of memory\n");
  224. count = -ENOMEM;
  225. goto error_no_buffer;
  226. }
  227. urb = usb_alloc_urb(0, GFP_ATOMIC);
  228. if (!urb) {
  229. dev_err(&port->dev, "no more free urbs\n");
  230. count = -ENOMEM;
  231. goto error_no_urb;
  232. }
  233. memcpy(buffer, buf, count);
  234. usb_serial_debug_data(&port->dev, __func__, count, buffer);
  235. /* The conncected devices do not have a bulk write endpoint,
  236. * to transmit data to de barcode device the control endpoint is used */
  237. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
  238. if (!dr) {
  239. dev_err(&port->dev, "out of memory\n");
  240. count = -ENOMEM;
  241. goto error_no_dr;
  242. }
  243. dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
  244. dr->bRequest = 0x01;
  245. dr->wValue = 0;
  246. dr->wIndex = 0;
  247. dr->wLength = cpu_to_le16(count);
  248. usb_fill_control_urb(urb, serial->dev,
  249. usb_sndctrlpipe(serial->dev, 0),
  250. (unsigned char *)dr, buffer, count,
  251. opticon_write_control_callback, port);
  252. /* send it down the pipe */
  253. status = usb_submit_urb(urb, GFP_ATOMIC);
  254. if (status) {
  255. dev_err(&port->dev,
  256. "%s - usb_submit_urb(write endpoint) failed status = %d\n",
  257. __func__, status);
  258. count = status;
  259. goto error;
  260. }
  261. /* we are done with this urb, so let the host driver
  262. * really free it when it is finished with it */
  263. usb_free_urb(urb);
  264. return count;
  265. error:
  266. kfree(dr);
  267. error_no_dr:
  268. usb_free_urb(urb);
  269. error_no_urb:
  270. kfree(buffer);
  271. error_no_buffer:
  272. spin_lock_irqsave(&priv->lock, flags);
  273. --priv->outstanding_urbs;
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. return count;
  276. }
  277. static int opticon_write_room(struct tty_struct *tty)
  278. {
  279. struct usb_serial_port *port = tty->driver_data;
  280. struct opticon_private *priv = usb_get_serial_port_data(port);
  281. unsigned long flags;
  282. /*
  283. * We really can take almost anything the user throws at us
  284. * but let's pick a nice big number to tell the tty
  285. * layer that we have lots of free space, unless we don't.
  286. */
  287. spin_lock_irqsave(&priv->lock, flags);
  288. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  289. spin_unlock_irqrestore(&priv->lock, flags);
  290. dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
  291. return 0;
  292. }
  293. spin_unlock_irqrestore(&priv->lock, flags);
  294. return 2048;
  295. }
  296. static void opticon_throttle(struct tty_struct *tty)
  297. {
  298. struct usb_serial_port *port = tty->driver_data;
  299. struct opticon_private *priv = usb_get_serial_port_data(port);
  300. unsigned long flags;
  301. spin_lock_irqsave(&priv->lock, flags);
  302. priv->throttled = true;
  303. spin_unlock_irqrestore(&priv->lock, flags);
  304. }
  305. static void opticon_unthrottle(struct tty_struct *tty)
  306. {
  307. struct usb_serial_port *port = tty->driver_data;
  308. struct opticon_private *priv = usb_get_serial_port_data(port);
  309. unsigned long flags;
  310. int result, was_throttled;
  311. spin_lock_irqsave(&priv->lock, flags);
  312. priv->throttled = false;
  313. was_throttled = priv->actually_throttled;
  314. priv->actually_throttled = false;
  315. spin_unlock_irqrestore(&priv->lock, flags);
  316. if (was_throttled) {
  317. result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
  318. if (result)
  319. dev_err(&port->dev,
  320. "%s - failed submitting read urb, error %d\n",
  321. __func__, result);
  322. }
  323. }
  324. static int opticon_tiocmget(struct tty_struct *tty)
  325. {
  326. struct usb_serial_port *port = tty->driver_data;
  327. struct opticon_private *priv = usb_get_serial_port_data(port);
  328. unsigned long flags;
  329. int result = 0;
  330. spin_lock_irqsave(&priv->lock, flags);
  331. if (priv->rts)
  332. result |= TIOCM_RTS;
  333. if (priv->cts)
  334. result |= TIOCM_CTS;
  335. spin_unlock_irqrestore(&priv->lock, flags);
  336. dev_dbg(&port->dev, "%s - %x\n", __func__, result);
  337. return result;
  338. }
  339. static int opticon_tiocmset(struct tty_struct *tty,
  340. unsigned int set, unsigned int clear)
  341. {
  342. struct usb_serial_port *port = tty->driver_data;
  343. struct usb_serial *serial = port->serial;
  344. struct opticon_private *priv = usb_get_serial_port_data(port);
  345. unsigned long flags;
  346. bool rts;
  347. bool changed = false;
  348. int ret;
  349. /* We only support RTS so we only handle that */
  350. spin_lock_irqsave(&priv->lock, flags);
  351. rts = priv->rts;
  352. if (set & TIOCM_RTS)
  353. priv->rts = true;
  354. if (clear & TIOCM_RTS)
  355. priv->rts = false;
  356. changed = rts ^ priv->rts;
  357. spin_unlock_irqrestore(&priv->lock, flags);
  358. if (!changed)
  359. return 0;
  360. /* Send the new RTS state to the connected device */
  361. mutex_lock(&serial->disc_mutex);
  362. if (!serial->disconnected)
  363. ret = send_control_msg(port, CONTROL_RTS, !rts);
  364. else
  365. ret = -ENODEV;
  366. mutex_unlock(&serial->disc_mutex);
  367. return ret;
  368. }
  369. static int get_serial_info(struct usb_serial_port *port,
  370. struct serial_struct __user *serial)
  371. {
  372. struct serial_struct tmp;
  373. if (!serial)
  374. return -EFAULT;
  375. memset(&tmp, 0x00, sizeof(tmp));
  376. /* fake emulate a 16550 uart to make userspace code happy */
  377. tmp.type = PORT_16550A;
  378. tmp.line = port->serial->minor;
  379. tmp.port = 0;
  380. tmp.irq = 0;
  381. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  382. tmp.xmit_fifo_size = 1024;
  383. tmp.baud_base = 9600;
  384. tmp.close_delay = 5*HZ;
  385. tmp.closing_wait = 30*HZ;
  386. if (copy_to_user(serial, &tmp, sizeof(*serial)))
  387. return -EFAULT;
  388. return 0;
  389. }
  390. static int opticon_ioctl(struct tty_struct *tty,
  391. unsigned int cmd, unsigned long arg)
  392. {
  393. struct usb_serial_port *port = tty->driver_data;
  394. dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
  395. switch (cmd) {
  396. case TIOCGSERIAL:
  397. return get_serial_info(port,
  398. (struct serial_struct __user *)arg);
  399. }
  400. return -ENOIOCTLCMD;
  401. }
  402. static int opticon_startup(struct usb_serial *serial)
  403. {
  404. if (!serial->num_bulk_in) {
  405. dev_err(&serial->dev->dev, "no bulk in endpoint\n");
  406. return -ENODEV;
  407. }
  408. return 0;
  409. }
  410. static int opticon_port_probe(struct usb_serial_port *port)
  411. {
  412. struct opticon_private *priv;
  413. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  414. if (!priv)
  415. return -ENOMEM;
  416. spin_lock_init(&priv->lock);
  417. priv->bulk_read_urb = port->read_urbs[0];
  418. usb_set_serial_port_data(port, priv);
  419. return 0;
  420. }
  421. static int opticon_port_remove(struct usb_serial_port *port)
  422. {
  423. struct opticon_private *priv = usb_get_serial_port_data(port);
  424. kfree(priv);
  425. return 0;
  426. }
  427. static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
  428. {
  429. struct opticon_private *priv;
  430. priv = usb_get_serial_port_data(serial->port[0]);
  431. usb_kill_urb(priv->bulk_read_urb);
  432. return 0;
  433. }
  434. static int opticon_resume(struct usb_serial *serial)
  435. {
  436. struct usb_serial_port *port = serial->port[0];
  437. struct opticon_private *priv = usb_get_serial_port_data(port);
  438. int result;
  439. mutex_lock(&port->port.mutex);
  440. /* This is protected by the port mutex against close/open */
  441. if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  442. result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
  443. else
  444. result = 0;
  445. mutex_unlock(&port->port.mutex);
  446. return result;
  447. }
  448. static struct usb_serial_driver opticon_device = {
  449. .driver = {
  450. .owner = THIS_MODULE,
  451. .name = "opticon",
  452. },
  453. .id_table = id_table,
  454. .num_ports = 1,
  455. .bulk_in_size = 256,
  456. .attach = opticon_startup,
  457. .port_probe = opticon_port_probe,
  458. .port_remove = opticon_port_remove,
  459. .open = opticon_open,
  460. .close = opticon_close,
  461. .write = opticon_write,
  462. .write_room = opticon_write_room,
  463. .throttle = opticon_throttle,
  464. .unthrottle = opticon_unthrottle,
  465. .ioctl = opticon_ioctl,
  466. .tiocmget = opticon_tiocmget,
  467. .tiocmset = opticon_tiocmset,
  468. .suspend = opticon_suspend,
  469. .resume = opticon_resume,
  470. .read_bulk_callback = opticon_read_bulk_callback,
  471. };
  472. static struct usb_serial_driver * const serial_drivers[] = {
  473. &opticon_device, NULL
  474. };
  475. module_usb_serial_driver(serial_drivers, id_table);
  476. MODULE_DESCRIPTION(DRIVER_DESC);
  477. MODULE_LICENSE("GPL");