opticon.c 16 KB

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