opticon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Opticon USB barcode to serial driver
  3. *
  4. * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  5. * Copyright (C) 2008 - 2009 Novell Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_driver.h>
  15. #include <linux/slab.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/serial.h>
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. #include <linux/uaccess.h>
  22. static int debug;
  23. static const struct usb_device_id id_table[] = {
  24. { USB_DEVICE(0x065a, 0x0009) },
  25. { },
  26. };
  27. MODULE_DEVICE_TABLE(usb, id_table);
  28. /* This structure holds all of the individual device information */
  29. struct opticon_private {
  30. struct usb_device *udev;
  31. struct usb_serial *serial;
  32. struct usb_serial_port *port;
  33. unsigned char *bulk_in_buffer;
  34. struct urb *bulk_read_urb;
  35. int buffer_size;
  36. u8 bulk_address;
  37. spinlock_t lock; /* protects the following flags */
  38. bool throttled;
  39. bool actually_throttled;
  40. bool rts;
  41. int outstanding_urbs;
  42. };
  43. /* max number of write urbs in flight */
  44. #define URB_UPPER_LIMIT 4
  45. static void opticon_bulk_callback(struct urb *urb)
  46. {
  47. struct opticon_private *priv = urb->context;
  48. unsigned char *data = urb->transfer_buffer;
  49. struct usb_serial_port *port = priv->port;
  50. int status = urb->status;
  51. struct tty_struct *tty;
  52. int result;
  53. int data_length;
  54. dbg("%s - port %d", __func__, port->number);
  55. switch (status) {
  56. case 0:
  57. /* success */
  58. break;
  59. case -ECONNRESET:
  60. case -ENOENT:
  61. case -ESHUTDOWN:
  62. /* this urb is terminated, clean up */
  63. dbg("%s - urb shutting down with status: %d",
  64. __func__, status);
  65. return;
  66. default:
  67. dbg("%s - nonzero urb status received: %d",
  68. __func__, status);
  69. goto exit;
  70. }
  71. usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
  72. data);
  73. if (urb->actual_length > 2) {
  74. data_length = urb->actual_length - 2;
  75. /*
  76. * Data from the device comes with a 2 byte header:
  77. *
  78. * <0x00><0x00>data...
  79. * This is real data to be sent to the tty layer
  80. * <0x00><0x01)level
  81. * This is a RTS level change, the third byte is the RTS
  82. * value (0 for low, 1 for high).
  83. */
  84. if ((data[0] == 0x00) && (data[1] == 0x00)) {
  85. /* real data, send it to the tty layer */
  86. tty = tty_port_tty_get(&port->port);
  87. if (tty) {
  88. tty_insert_flip_string(tty, data,
  89. data_length);
  90. tty_flip_buffer_push(tty);
  91. tty_kref_put(tty);
  92. }
  93. } else {
  94. if ((data[0] == 0x00) && (data[1] == 0x01)) {
  95. if (data[2] == 0x00)
  96. priv->rts = false;
  97. else
  98. priv->rts = true;
  99. } else {
  100. dev_dbg(&priv->udev->dev,
  101. "Unknown data packet received from the device:"
  102. " %2x %2x\n",
  103. data[0], data[1]);
  104. }
  105. }
  106. } else {
  107. dev_dbg(&priv->udev->dev,
  108. "Improper amount of data received from the device, "
  109. "%d bytes", urb->actual_length);
  110. }
  111. exit:
  112. spin_lock(&priv->lock);
  113. /* Continue trying to always read if we should */
  114. if (!priv->throttled) {
  115. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  116. usb_rcvbulkpipe(priv->udev,
  117. priv->bulk_address),
  118. priv->bulk_in_buffer, priv->buffer_size,
  119. opticon_bulk_callback, priv);
  120. result = usb_submit_urb(port->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 opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
  130. {
  131. struct opticon_private *priv = usb_get_serial_data(port->serial);
  132. unsigned long flags;
  133. int result = 0;
  134. dbg("%s - port %d", __func__, port->number);
  135. spin_lock_irqsave(&priv->lock, flags);
  136. priv->throttled = false;
  137. priv->actually_throttled = false;
  138. priv->port = port;
  139. spin_unlock_irqrestore(&priv->lock, flags);
  140. /* Start reading from the device */
  141. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  142. usb_rcvbulkpipe(priv->udev,
  143. priv->bulk_address),
  144. priv->bulk_in_buffer, priv->buffer_size,
  145. opticon_bulk_callback, priv);
  146. result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
  147. if (result)
  148. dev_err(&port->dev,
  149. "%s - failed resubmitting read urb, error %d\n",
  150. __func__, result);
  151. return result;
  152. }
  153. static void opticon_close(struct usb_serial_port *port)
  154. {
  155. struct opticon_private *priv = usb_get_serial_data(port->serial);
  156. dbg("%s - port %d", __func__, port->number);
  157. /* shutdown our urbs */
  158. usb_kill_urb(priv->bulk_read_urb);
  159. }
  160. static void opticon_write_bulk_callback(struct urb *urb)
  161. {
  162. struct opticon_private *priv = urb->context;
  163. int status = urb->status;
  164. unsigned long flags;
  165. /* free up the transfer buffer, as usb_free_urb() does not do this */
  166. kfree(urb->transfer_buffer);
  167. if (status)
  168. dbg("%s - nonzero write bulk status received: %d",
  169. __func__, status);
  170. spin_lock_irqsave(&priv->lock, flags);
  171. --priv->outstanding_urbs;
  172. spin_unlock_irqrestore(&priv->lock, flags);
  173. usb_serial_port_softint(priv->port);
  174. }
  175. static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
  176. const unsigned char *buf, int count)
  177. {
  178. struct opticon_private *priv = usb_get_serial_data(port->serial);
  179. struct usb_serial *serial = port->serial;
  180. struct urb *urb;
  181. unsigned char *buffer;
  182. unsigned long flags;
  183. int status;
  184. dbg("%s - port %d", __func__, port->number);
  185. spin_lock_irqsave(&priv->lock, flags);
  186. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  187. spin_unlock_irqrestore(&priv->lock, flags);
  188. dbg("%s - write limit hit", __func__);
  189. return 0;
  190. }
  191. priv->outstanding_urbs++;
  192. spin_unlock_irqrestore(&priv->lock, flags);
  193. buffer = kmalloc(count, GFP_ATOMIC);
  194. if (!buffer) {
  195. dev_err(&port->dev, "out of memory\n");
  196. count = -ENOMEM;
  197. goto error_no_buffer;
  198. }
  199. urb = usb_alloc_urb(0, GFP_ATOMIC);
  200. if (!urb) {
  201. dev_err(&port->dev, "no more free urbs\n");
  202. count = -ENOMEM;
  203. goto error_no_urb;
  204. }
  205. memcpy(buffer, buf, count);
  206. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  207. usb_fill_bulk_urb(urb, serial->dev,
  208. usb_sndbulkpipe(serial->dev,
  209. port->bulk_out_endpointAddress),
  210. buffer, count, opticon_write_bulk_callback, priv);
  211. /* send it down the pipe */
  212. status = usb_submit_urb(urb, GFP_ATOMIC);
  213. if (status) {
  214. dev_err(&port->dev,
  215. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  216. __func__, status);
  217. count = status;
  218. goto error;
  219. }
  220. /* we are done with this urb, so let the host driver
  221. * really free it when it is finished with it */
  222. usb_free_urb(urb);
  223. return count;
  224. error:
  225. usb_free_urb(urb);
  226. error_no_urb:
  227. kfree(buffer);
  228. error_no_buffer:
  229. spin_lock_irqsave(&priv->lock, flags);
  230. --priv->outstanding_urbs;
  231. spin_unlock_irqrestore(&priv->lock, flags);
  232. return count;
  233. }
  234. static int opticon_write_room(struct tty_struct *tty)
  235. {
  236. struct usb_serial_port *port = tty->driver_data;
  237. struct opticon_private *priv = usb_get_serial_data(port->serial);
  238. unsigned long flags;
  239. dbg("%s - port %d", __func__, port->number);
  240. /*
  241. * We really can take almost anything the user throws at us
  242. * but let's pick a nice big number to tell the tty
  243. * layer that we have lots of free space, unless we don't.
  244. */
  245. spin_lock_irqsave(&priv->lock, flags);
  246. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  247. spin_unlock_irqrestore(&priv->lock, flags);
  248. dbg("%s - write limit hit", __func__);
  249. return 0;
  250. }
  251. spin_unlock_irqrestore(&priv->lock, flags);
  252. return 2048;
  253. }
  254. static void opticon_throttle(struct tty_struct *tty)
  255. {
  256. struct usb_serial_port *port = tty->driver_data;
  257. struct opticon_private *priv = usb_get_serial_data(port->serial);
  258. unsigned long flags;
  259. dbg("%s - port %d", __func__, port->number);
  260. spin_lock_irqsave(&priv->lock, flags);
  261. priv->throttled = true;
  262. spin_unlock_irqrestore(&priv->lock, flags);
  263. }
  264. static void opticon_unthrottle(struct tty_struct *tty)
  265. {
  266. struct usb_serial_port *port = tty->driver_data;
  267. struct opticon_private *priv = usb_get_serial_data(port->serial);
  268. unsigned long flags;
  269. int result, was_throttled;
  270. dbg("%s - port %d", __func__, port->number);
  271. spin_lock_irqsave(&priv->lock, flags);
  272. priv->throttled = false;
  273. was_throttled = priv->actually_throttled;
  274. priv->actually_throttled = false;
  275. spin_unlock_irqrestore(&priv->lock, flags);
  276. priv->bulk_read_urb->dev = port->serial->dev;
  277. if (was_throttled) {
  278. result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
  279. if (result)
  280. dev_err(&port->dev,
  281. "%s - failed submitting read urb, error %d\n",
  282. __func__, result);
  283. }
  284. }
  285. static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
  286. {
  287. struct usb_serial_port *port = tty->driver_data;
  288. struct opticon_private *priv = usb_get_serial_data(port->serial);
  289. unsigned long flags;
  290. int result = 0;
  291. dbg("%s - port %d", __func__, port->number);
  292. spin_lock_irqsave(&priv->lock, flags);
  293. if (priv->rts)
  294. result = TIOCM_RTS;
  295. spin_unlock_irqrestore(&priv->lock, flags);
  296. dbg("%s - %x", __func__, result);
  297. return result;
  298. }
  299. static int get_serial_info(struct opticon_private *priv,
  300. struct serial_struct __user *serial)
  301. {
  302. struct serial_struct tmp;
  303. if (!serial)
  304. return -EFAULT;
  305. memset(&tmp, 0x00, sizeof(tmp));
  306. /* fake emulate a 16550 uart to make userspace code happy */
  307. tmp.type = PORT_16550A;
  308. tmp.line = priv->serial->minor;
  309. tmp.port = 0;
  310. tmp.irq = 0;
  311. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  312. tmp.xmit_fifo_size = 1024;
  313. tmp.baud_base = 9600;
  314. tmp.close_delay = 5*HZ;
  315. tmp.closing_wait = 30*HZ;
  316. if (copy_to_user(serial, &tmp, sizeof(*serial)))
  317. return -EFAULT;
  318. return 0;
  319. }
  320. static int opticon_ioctl(struct tty_struct *tty, struct file *file,
  321. unsigned int cmd, unsigned long arg)
  322. {
  323. struct usb_serial_port *port = tty->driver_data;
  324. struct opticon_private *priv = usb_get_serial_data(port->serial);
  325. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  326. switch (cmd) {
  327. case TIOCGSERIAL:
  328. return get_serial_info(priv,
  329. (struct serial_struct __user *)arg);
  330. }
  331. return -ENOIOCTLCMD;
  332. }
  333. static int opticon_startup(struct usb_serial *serial)
  334. {
  335. struct opticon_private *priv;
  336. struct usb_host_interface *intf;
  337. int i;
  338. int retval = -ENOMEM;
  339. bool bulk_in_found = false;
  340. /* create our private serial structure */
  341. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  342. if (priv == NULL) {
  343. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  344. return -ENOMEM;
  345. }
  346. spin_lock_init(&priv->lock);
  347. priv->serial = serial;
  348. priv->port = serial->port[0];
  349. priv->udev = serial->dev;
  350. /* find our bulk endpoint */
  351. intf = serial->interface->altsetting;
  352. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  353. struct usb_endpoint_descriptor *endpoint;
  354. endpoint = &intf->endpoint[i].desc;
  355. if (!usb_endpoint_is_bulk_in(endpoint))
  356. continue;
  357. priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
  358. if (!priv->bulk_read_urb) {
  359. dev_err(&priv->udev->dev, "out of memory\n");
  360. goto error;
  361. }
  362. priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  363. priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  364. if (!priv->bulk_in_buffer) {
  365. dev_err(&priv->udev->dev, "out of memory\n");
  366. goto error;
  367. }
  368. priv->bulk_address = endpoint->bEndpointAddress;
  369. /* set up our bulk urb */
  370. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  371. usb_rcvbulkpipe(priv->udev,
  372. endpoint->bEndpointAddress),
  373. priv->bulk_in_buffer, priv->buffer_size,
  374. opticon_bulk_callback, priv);
  375. bulk_in_found = true;
  376. break;
  377. }
  378. if (!bulk_in_found) {
  379. dev_err(&priv->udev->dev,
  380. "Error - the proper endpoints were not found!\n");
  381. goto error;
  382. }
  383. usb_set_serial_data(serial, priv);
  384. return 0;
  385. error:
  386. usb_free_urb(priv->bulk_read_urb);
  387. kfree(priv->bulk_in_buffer);
  388. kfree(priv);
  389. return retval;
  390. }
  391. static void opticon_disconnect(struct usb_serial *serial)
  392. {
  393. struct opticon_private *priv = usb_get_serial_data(serial);
  394. dbg("%s", __func__);
  395. usb_kill_urb(priv->bulk_read_urb);
  396. usb_free_urb(priv->bulk_read_urb);
  397. }
  398. static void opticon_release(struct usb_serial *serial)
  399. {
  400. struct opticon_private *priv = usb_get_serial_data(serial);
  401. dbg("%s", __func__);
  402. kfree(priv->bulk_in_buffer);
  403. kfree(priv);
  404. }
  405. static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
  406. {
  407. struct usb_serial *serial = usb_get_intfdata(intf);
  408. struct opticon_private *priv = usb_get_serial_data(serial);
  409. usb_kill_urb(priv->bulk_read_urb);
  410. return 0;
  411. }
  412. static int opticon_resume(struct usb_interface *intf)
  413. {
  414. struct usb_serial *serial = usb_get_intfdata(intf);
  415. struct opticon_private *priv = usb_get_serial_data(serial);
  416. struct usb_serial_port *port = serial->port[0];
  417. int result;
  418. mutex_lock(&port->port.mutex);
  419. /* This is protected by the port mutex against close/open */
  420. if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  421. result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
  422. else
  423. result = 0;
  424. mutex_unlock(&port->port.mutex);
  425. return result;
  426. }
  427. static struct usb_driver opticon_driver = {
  428. .name = "opticon",
  429. .probe = usb_serial_probe,
  430. .disconnect = usb_serial_disconnect,
  431. .suspend = opticon_suspend,
  432. .resume = opticon_resume,
  433. .id_table = id_table,
  434. .no_dynamic_id = 1,
  435. };
  436. static struct usb_serial_driver opticon_device = {
  437. .driver = {
  438. .owner = THIS_MODULE,
  439. .name = "opticon",
  440. },
  441. .id_table = id_table,
  442. .usb_driver = &opticon_driver,
  443. .num_ports = 1,
  444. .attach = opticon_startup,
  445. .open = opticon_open,
  446. .close = opticon_close,
  447. .write = opticon_write,
  448. .write_room = opticon_write_room,
  449. .disconnect = opticon_disconnect,
  450. .release = opticon_release,
  451. .throttle = opticon_throttle,
  452. .unthrottle = opticon_unthrottle,
  453. .ioctl = opticon_ioctl,
  454. .tiocmget = opticon_tiocmget,
  455. };
  456. static int __init opticon_init(void)
  457. {
  458. int retval;
  459. retval = usb_serial_register(&opticon_device);
  460. if (retval)
  461. return retval;
  462. retval = usb_register(&opticon_driver);
  463. if (retval)
  464. usb_serial_deregister(&opticon_device);
  465. return retval;
  466. }
  467. static void __exit opticon_exit(void)
  468. {
  469. usb_deregister(&opticon_driver);
  470. usb_serial_deregister(&opticon_device);
  471. }
  472. module_init(opticon_init);
  473. module_exit(opticon_exit);
  474. MODULE_LICENSE("GPL");
  475. module_param(debug, bool, S_IRUGO | S_IWUSR);
  476. MODULE_PARM_DESC(debug, "Debug enabled or not");