symbolserial.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Symbol USB barcode to serial driver
  3. *
  4. * Copyright (C) 2013 Johan Hovold <jhovold@gmail.com>
  5. * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
  6. * Copyright (C) 2009 Novell Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/tty.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty_driver.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/serial.h>
  20. #include <linux/uaccess.h>
  21. static const struct usb_device_id id_table[] = {
  22. { USB_DEVICE(0x05e0, 0x0600) },
  23. { },
  24. };
  25. MODULE_DEVICE_TABLE(usb, id_table);
  26. struct symbol_private {
  27. spinlock_t lock; /* protects the following flags */
  28. bool throttled;
  29. bool actually_throttled;
  30. };
  31. static void symbol_int_callback(struct urb *urb)
  32. {
  33. struct usb_serial_port *port = urb->context;
  34. struct symbol_private *priv = usb_get_serial_port_data(port);
  35. unsigned char *data = urb->transfer_buffer;
  36. int status = urb->status;
  37. int result;
  38. int data_length;
  39. switch (status) {
  40. case 0:
  41. /* success */
  42. break;
  43. case -ECONNRESET:
  44. case -ENOENT:
  45. case -ESHUTDOWN:
  46. /* this urb is terminated, clean up */
  47. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  48. __func__, status);
  49. return;
  50. default:
  51. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  52. __func__, status);
  53. goto exit;
  54. }
  55. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  56. if (urb->actual_length > 1) {
  57. data_length = urb->actual_length - 1;
  58. /*
  59. * Data from the device comes with a 1 byte header:
  60. *
  61. * <size of data>data...
  62. * This is real data to be sent to the tty layer
  63. * we pretty much just ignore the size and send everything
  64. * else to the tty layer.
  65. */
  66. tty_insert_flip_string(&port->port, &data[1], data_length);
  67. tty_flip_buffer_push(&port->port);
  68. } else {
  69. dev_dbg(&port->dev, "%s - short packet\n", __func__);
  70. }
  71. exit:
  72. spin_lock(&priv->lock);
  73. /* Continue trying to always read if we should */
  74. if (!priv->throttled) {
  75. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  76. if (result)
  77. dev_err(&port->dev,
  78. "%s - failed resubmitting read urb, error %d\n",
  79. __func__, result);
  80. } else
  81. priv->actually_throttled = true;
  82. spin_unlock(&priv->lock);
  83. }
  84. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  85. {
  86. struct symbol_private *priv = usb_get_serial_data(port->serial);
  87. unsigned long flags;
  88. int result = 0;
  89. spin_lock_irqsave(&priv->lock, flags);
  90. priv->throttled = false;
  91. priv->actually_throttled = false;
  92. spin_unlock_irqrestore(&priv->lock, flags);
  93. /* Start reading from the device */
  94. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  95. if (result)
  96. dev_err(&port->dev,
  97. "%s - failed resubmitting read urb, error %d\n",
  98. __func__, result);
  99. return result;
  100. }
  101. static void symbol_close(struct usb_serial_port *port)
  102. {
  103. usb_kill_urb(port->interrupt_in_urb);
  104. }
  105. static void symbol_throttle(struct tty_struct *tty)
  106. {
  107. struct usb_serial_port *port = tty->driver_data;
  108. struct symbol_private *priv = usb_get_serial_data(port->serial);
  109. spin_lock_irq(&priv->lock);
  110. priv->throttled = true;
  111. spin_unlock_irq(&priv->lock);
  112. }
  113. static void symbol_unthrottle(struct tty_struct *tty)
  114. {
  115. struct usb_serial_port *port = tty->driver_data;
  116. struct symbol_private *priv = usb_get_serial_data(port->serial);
  117. int result;
  118. bool was_throttled;
  119. spin_lock_irq(&priv->lock);
  120. priv->throttled = false;
  121. was_throttled = priv->actually_throttled;
  122. priv->actually_throttled = false;
  123. spin_unlock_irq(&priv->lock);
  124. if (was_throttled) {
  125. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  126. if (result)
  127. dev_err(&port->dev,
  128. "%s - failed submitting read urb, error %d\n",
  129. __func__, result);
  130. }
  131. }
  132. static int symbol_startup(struct usb_serial *serial)
  133. {
  134. if (!serial->num_interrupt_in) {
  135. dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
  136. return -ENODEV;
  137. }
  138. return 0;
  139. }
  140. static int symbol_port_probe(struct usb_serial_port *port)
  141. {
  142. struct symbol_private *priv;
  143. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  144. if (!priv)
  145. return -ENOMEM;
  146. spin_lock_init(&priv->lock);
  147. usb_set_serial_port_data(port, priv);
  148. return 0;
  149. }
  150. static int symbol_port_remove(struct usb_serial_port *port)
  151. {
  152. struct symbol_private *priv = usb_get_serial_port_data(port);
  153. kfree(priv);
  154. return 0;
  155. }
  156. static struct usb_serial_driver symbol_device = {
  157. .driver = {
  158. .owner = THIS_MODULE,
  159. .name = "symbol",
  160. },
  161. .id_table = id_table,
  162. .num_ports = 1,
  163. .attach = symbol_startup,
  164. .port_probe = symbol_port_probe,
  165. .port_remove = symbol_port_remove,
  166. .open = symbol_open,
  167. .close = symbol_close,
  168. .throttle = symbol_throttle,
  169. .unthrottle = symbol_unthrottle,
  170. .read_int_callback = symbol_int_callback,
  171. };
  172. static struct usb_serial_driver * const serial_drivers[] = {
  173. &symbol_device, NULL
  174. };
  175. module_usb_serial_driver(serial_drivers, id_table);
  176. MODULE_LICENSE("GPL");