opticon.c 16 KB

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