opticon.c 14 KB

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