opticon.c 14 KB

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