opticon.c 15 KB

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