opticon.c 14 KB

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