opticon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. {
  135. struct opticon_private *priv = usb_get_serial_data(port->serial);
  136. unsigned long flags;
  137. int result = 0;
  138. dbg("%s - port %d", __func__, port->number);
  139. spin_lock_irqsave(&priv->lock, flags);
  140. priv->throttled = false;
  141. priv->actually_throttled = false;
  142. priv->port = port;
  143. spin_unlock_irqrestore(&priv->lock, flags);
  144. /* Start reading from the device */
  145. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  146. usb_rcvbulkpipe(priv->udev,
  147. priv->bulk_address),
  148. priv->bulk_in_buffer, priv->buffer_size,
  149. opticon_bulk_callback, priv);
  150. result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
  151. if (result)
  152. dev_err(&port->dev,
  153. "%s - failed resubmitting read urb, error %d\n",
  154. __func__, result);
  155. return result;
  156. }
  157. static void opticon_close(struct usb_serial_port *port)
  158. {
  159. struct opticon_private *priv = usb_get_serial_data(port->serial);
  160. dbg("%s - port %d", __func__, port->number);
  161. /* shutdown our urbs */
  162. usb_kill_urb(priv->bulk_read_urb);
  163. }
  164. static void opticon_write_bulk_callback(struct urb *urb)
  165. {
  166. struct opticon_private *priv = urb->context;
  167. int status = urb->status;
  168. unsigned long flags;
  169. /* free up the transfer buffer, as usb_free_urb() does not do this */
  170. kfree(urb->transfer_buffer);
  171. if (status)
  172. dbg("%s - nonzero write bulk status received: %d",
  173. __func__, status);
  174. spin_lock_irqsave(&priv->lock, flags);
  175. --priv->outstanding_urbs;
  176. spin_unlock_irqrestore(&priv->lock, flags);
  177. usb_serial_port_softint(priv->port);
  178. }
  179. static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
  180. const unsigned char *buf, int count)
  181. {
  182. struct opticon_private *priv = usb_get_serial_data(port->serial);
  183. struct usb_serial *serial = port->serial;
  184. struct urb *urb;
  185. unsigned char *buffer;
  186. unsigned long flags;
  187. int status;
  188. dbg("%s - port %d", __func__, port->number);
  189. spin_lock_irqsave(&priv->lock, flags);
  190. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  191. spin_unlock_irqrestore(&priv->lock, flags);
  192. dbg("%s - write limit hit\n", __func__);
  193. return 0;
  194. }
  195. priv->outstanding_urbs++;
  196. spin_unlock_irqrestore(&priv->lock, flags);
  197. buffer = kmalloc(count, GFP_ATOMIC);
  198. if (!buffer) {
  199. dev_err(&port->dev, "out of memory\n");
  200. count = -ENOMEM;
  201. goto error_no_buffer;
  202. }
  203. urb = usb_alloc_urb(0, GFP_ATOMIC);
  204. if (!urb) {
  205. dev_err(&port->dev, "no more free urbs\n");
  206. count = -ENOMEM;
  207. goto error_no_urb;
  208. }
  209. memcpy(buffer, buf, count);
  210. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  211. usb_fill_bulk_urb(urb, serial->dev,
  212. usb_sndbulkpipe(serial->dev,
  213. port->bulk_out_endpointAddress),
  214. buffer, count, opticon_write_bulk_callback, priv);
  215. /* send it down the pipe */
  216. status = usb_submit_urb(urb, GFP_ATOMIC);
  217. if (status) {
  218. dev_err(&port->dev,
  219. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  220. __func__, status);
  221. count = status;
  222. goto error;
  223. }
  224. /* we are done with this urb, so let the host driver
  225. * really free it when it is finished with it */
  226. usb_free_urb(urb);
  227. return count;
  228. error:
  229. usb_free_urb(urb);
  230. error_no_urb:
  231. kfree(buffer);
  232. error_no_buffer:
  233. spin_lock_irqsave(&priv->lock, flags);
  234. --priv->outstanding_urbs;
  235. spin_unlock_irqrestore(&priv->lock, flags);
  236. return count;
  237. }
  238. static int opticon_write_room(struct tty_struct *tty)
  239. {
  240. struct usb_serial_port *port = tty->driver_data;
  241. struct opticon_private *priv = usb_get_serial_data(port->serial);
  242. unsigned long flags;
  243. dbg("%s - port %d", __func__, port->number);
  244. /*
  245. * We really can take almost anything the user throws at us
  246. * but let's pick a nice big number to tell the tty
  247. * layer that we have lots of free space, unless we don't.
  248. */
  249. spin_lock_irqsave(&priv->lock, flags);
  250. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  251. spin_unlock_irqrestore(&priv->lock, flags);
  252. dbg("%s - write limit hit\n", __func__);
  253. return 0;
  254. }
  255. spin_unlock_irqrestore(&priv->lock, flags);
  256. return 2048;
  257. }
  258. static void opticon_throttle(struct tty_struct *tty)
  259. {
  260. struct usb_serial_port *port = tty->driver_data;
  261. struct opticon_private *priv = usb_get_serial_data(port->serial);
  262. unsigned long flags;
  263. dbg("%s - port %d", __func__, port->number);
  264. spin_lock_irqsave(&priv->lock, flags);
  265. priv->throttled = true;
  266. spin_unlock_irqrestore(&priv->lock, flags);
  267. }
  268. static void opticon_unthrottle(struct tty_struct *tty)
  269. {
  270. struct usb_serial_port *port = tty->driver_data;
  271. struct opticon_private *priv = usb_get_serial_data(port->serial);
  272. unsigned long flags;
  273. int result, was_throttled;
  274. dbg("%s - port %d", __func__, port->number);
  275. spin_lock_irqsave(&priv->lock, flags);
  276. priv->throttled = false;
  277. was_throttled = priv->actually_throttled;
  278. priv->actually_throttled = false;
  279. spin_unlock_irqrestore(&priv->lock, flags);
  280. priv->bulk_read_urb->dev = port->serial->dev;
  281. if (was_throttled) {
  282. result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
  283. if (result)
  284. dev_err(&port->dev,
  285. "%s - failed submitting read urb, error %d\n",
  286. __func__, result);
  287. }
  288. }
  289. static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
  290. {
  291. struct usb_serial_port *port = tty->driver_data;
  292. struct opticon_private *priv = usb_get_serial_data(port->serial);
  293. unsigned long flags;
  294. int result = 0;
  295. dbg("%s - port %d", __func__, port->number);
  296. spin_lock_irqsave(&priv->lock, flags);
  297. if (priv->rts)
  298. result = TIOCM_RTS;
  299. spin_unlock_irqrestore(&priv->lock, flags);
  300. dbg("%s - %x", __func__, result);
  301. return result;
  302. }
  303. static int get_serial_info(struct opticon_private *priv,
  304. struct serial_struct __user *serial)
  305. {
  306. struct serial_struct tmp;
  307. if (!serial)
  308. return -EFAULT;
  309. memset(&tmp, 0x00, sizeof(tmp));
  310. /* fake emulate a 16550 uart to make userspace code happy */
  311. tmp.type = PORT_16550A;
  312. tmp.line = priv->serial->minor;
  313. tmp.port = 0;
  314. tmp.irq = 0;
  315. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  316. tmp.xmit_fifo_size = 1024;
  317. tmp.baud_base = 9600;
  318. tmp.close_delay = 5*HZ;
  319. tmp.closing_wait = 30*HZ;
  320. if (copy_to_user(serial, &tmp, sizeof(*serial)))
  321. return -EFAULT;
  322. return 0;
  323. }
  324. static int opticon_ioctl(struct tty_struct *tty, struct file *file,
  325. unsigned int cmd, unsigned long arg)
  326. {
  327. struct usb_serial_port *port = tty->driver_data;
  328. struct opticon_private *priv = usb_get_serial_data(port->serial);
  329. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  330. switch (cmd) {
  331. case TIOCGSERIAL:
  332. return get_serial_info(priv,
  333. (struct serial_struct __user *)arg);
  334. }
  335. return -ENOIOCTLCMD;
  336. }
  337. static int opticon_startup(struct usb_serial *serial)
  338. {
  339. struct opticon_private *priv;
  340. struct usb_host_interface *intf;
  341. int i;
  342. int retval = -ENOMEM;
  343. bool bulk_in_found = false;
  344. /* create our private serial structure */
  345. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  346. if (priv == NULL) {
  347. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  348. return -ENOMEM;
  349. }
  350. spin_lock_init(&priv->lock);
  351. priv->serial = serial;
  352. priv->port = serial->port[0];
  353. priv->udev = serial->dev;
  354. /* find our bulk endpoint */
  355. intf = serial->interface->altsetting;
  356. for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
  357. struct usb_endpoint_descriptor *endpoint;
  358. endpoint = &intf->endpoint[i].desc;
  359. if (!usb_endpoint_is_bulk_in(endpoint))
  360. continue;
  361. priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
  362. if (!priv->bulk_read_urb) {
  363. dev_err(&priv->udev->dev, "out of memory\n");
  364. goto error;
  365. }
  366. priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  367. priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
  368. if (!priv->bulk_in_buffer) {
  369. dev_err(&priv->udev->dev, "out of memory\n");
  370. goto error;
  371. }
  372. priv->bulk_address = endpoint->bEndpointAddress;
  373. /* set up our bulk urb */
  374. usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
  375. usb_rcvbulkpipe(priv->udev,
  376. endpoint->bEndpointAddress),
  377. priv->bulk_in_buffer, priv->buffer_size,
  378. opticon_bulk_callback, priv);
  379. bulk_in_found = true;
  380. break;
  381. }
  382. if (!bulk_in_found) {
  383. dev_err(&priv->udev->dev,
  384. "Error - the proper endpoints were not found!\n");
  385. goto error;
  386. }
  387. usb_set_serial_data(serial, priv);
  388. return 0;
  389. error:
  390. usb_free_urb(priv->bulk_read_urb);
  391. kfree(priv->bulk_in_buffer);
  392. kfree(priv);
  393. return retval;
  394. }
  395. static void opticon_disconnect(struct usb_serial *serial)
  396. {
  397. struct opticon_private *priv = usb_get_serial_data(serial);
  398. dbg("%s", __func__);
  399. usb_kill_urb(priv->bulk_read_urb);
  400. usb_free_urb(priv->bulk_read_urb);
  401. }
  402. static void opticon_release(struct usb_serial *serial)
  403. {
  404. struct opticon_private *priv = usb_get_serial_data(serial);
  405. dbg("%s", __func__);
  406. kfree(priv->bulk_in_buffer);
  407. kfree(priv);
  408. }
  409. static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
  410. {
  411. struct usb_serial *serial = usb_get_intfdata(intf);
  412. struct opticon_private *priv = usb_get_serial_data(serial);
  413. usb_kill_urb(priv->bulk_read_urb);
  414. return 0;
  415. }
  416. static int opticon_resume(struct usb_interface *intf)
  417. {
  418. struct usb_serial *serial = usb_get_intfdata(intf);
  419. struct opticon_private *priv = usb_get_serial_data(serial);
  420. struct usb_serial_port *port = serial->port[0];
  421. int result;
  422. mutex_lock(&port->port.mutex);
  423. /* This is protected by the port mutex against close/open */
  424. if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  425. result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
  426. else
  427. result = 0;
  428. mutex_unlock(&port->port.mutex);
  429. return result;
  430. }
  431. static struct usb_driver opticon_driver = {
  432. .name = "opticon",
  433. .probe = usb_serial_probe,
  434. .disconnect = usb_serial_disconnect,
  435. .suspend = opticon_suspend,
  436. .resume = opticon_resume,
  437. .id_table = id_table,
  438. .no_dynamic_id = 1,
  439. };
  440. static struct usb_serial_driver opticon_device = {
  441. .driver = {
  442. .owner = THIS_MODULE,
  443. .name = "opticon",
  444. },
  445. .id_table = id_table,
  446. .usb_driver = &opticon_driver,
  447. .num_ports = 1,
  448. .attach = opticon_startup,
  449. .open = opticon_open,
  450. .close = opticon_close,
  451. .write = opticon_write,
  452. .write_room = opticon_write_room,
  453. .disconnect = opticon_disconnect,
  454. .release = opticon_release,
  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");