opticon.c 14 KB

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