opticon.c 14 KB

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