opticon.c 15 KB

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