opticon.c 15 KB

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