symbolserial.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Symbol USB barcode to serial driver
  3. *
  4. * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
  5. * Copyright (C) 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/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. /* This structure holds all of the individual device information */
  27. struct symbol_private {
  28. struct usb_device *udev;
  29. spinlock_t lock; /* protects the following flags */
  30. bool throttled;
  31. bool actually_throttled;
  32. bool rts;
  33. };
  34. static void symbol_int_callback(struct urb *urb)
  35. {
  36. struct usb_serial_port *port = urb->context;
  37. struct symbol_private *priv = usb_get_serial_data(port->serial);
  38. unsigned char *data = urb->transfer_buffer;
  39. int status = urb->status;
  40. int result;
  41. int data_length;
  42. switch (status) {
  43. case 0:
  44. /* success */
  45. break;
  46. case -ECONNRESET:
  47. case -ENOENT:
  48. case -ESHUTDOWN:
  49. /* this urb is terminated, clean up */
  50. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  51. __func__, status);
  52. return;
  53. default:
  54. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  55. __func__, status);
  56. goto exit;
  57. }
  58. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  59. if (urb->actual_length > 1) {
  60. data_length = urb->actual_length - 1;
  61. /*
  62. * Data from the device comes with a 1 byte header:
  63. *
  64. * <size of data>data...
  65. * This is real data to be sent to the tty layer
  66. * we pretty much just ignore the size and send everything
  67. * else to the tty layer.
  68. */
  69. tty_insert_flip_string(&port->port, &data[1], data_length);
  70. tty_flip_buffer_push(&port->port);
  71. } else {
  72. dev_dbg(&priv->udev->dev,
  73. "Improper amount of data received from the device, "
  74. "%d bytes", urb->actual_length);
  75. }
  76. exit:
  77. spin_lock(&priv->lock);
  78. /* Continue trying to always read if we should */
  79. if (!priv->throttled) {
  80. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  81. if (result)
  82. dev_err(&port->dev,
  83. "%s - failed resubmitting read urb, error %d\n",
  84. __func__, result);
  85. } else
  86. priv->actually_throttled = true;
  87. spin_unlock(&priv->lock);
  88. }
  89. static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
  90. {
  91. struct symbol_private *priv = usb_get_serial_data(port->serial);
  92. unsigned long flags;
  93. int result = 0;
  94. spin_lock_irqsave(&priv->lock, flags);
  95. priv->throttled = false;
  96. priv->actually_throttled = false;
  97. spin_unlock_irqrestore(&priv->lock, flags);
  98. /* Start reading from the device */
  99. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  100. if (result)
  101. dev_err(&port->dev,
  102. "%s - failed resubmitting read urb, error %d\n",
  103. __func__, result);
  104. return result;
  105. }
  106. static void symbol_close(struct usb_serial_port *port)
  107. {
  108. usb_kill_urb(port->interrupt_in_urb);
  109. }
  110. static void symbol_throttle(struct tty_struct *tty)
  111. {
  112. struct usb_serial_port *port = tty->driver_data;
  113. struct symbol_private *priv = usb_get_serial_data(port->serial);
  114. spin_lock_irq(&priv->lock);
  115. priv->throttled = true;
  116. spin_unlock_irq(&priv->lock);
  117. }
  118. static void symbol_unthrottle(struct tty_struct *tty)
  119. {
  120. struct usb_serial_port *port = tty->driver_data;
  121. struct symbol_private *priv = usb_get_serial_data(port->serial);
  122. int result;
  123. bool was_throttled;
  124. spin_lock_irq(&priv->lock);
  125. priv->throttled = false;
  126. was_throttled = priv->actually_throttled;
  127. priv->actually_throttled = false;
  128. spin_unlock_irq(&priv->lock);
  129. if (was_throttled) {
  130. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  131. if (result)
  132. dev_err(&port->dev,
  133. "%s - failed submitting read urb, error %d\n",
  134. __func__, result);
  135. }
  136. }
  137. static int symbol_startup(struct usb_serial *serial)
  138. {
  139. struct symbol_private *priv;
  140. if (!serial->num_interrupt_in) {
  141. dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
  142. return -ENODEV;
  143. }
  144. /* create our private serial structure */
  145. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  146. if (priv == NULL) {
  147. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  148. return -ENOMEM;
  149. }
  150. spin_lock_init(&priv->lock);
  151. priv->udev = serial->dev;
  152. usb_set_serial_data(serial, priv);
  153. return 0;
  154. }
  155. static void symbol_release(struct usb_serial *serial)
  156. {
  157. struct symbol_private *priv = usb_get_serial_data(serial);
  158. kfree(priv);
  159. }
  160. static struct usb_serial_driver symbol_device = {
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = "symbol",
  164. },
  165. .id_table = id_table,
  166. .num_ports = 1,
  167. .attach = symbol_startup,
  168. .open = symbol_open,
  169. .close = symbol_close,
  170. .release = symbol_release,
  171. .throttle = symbol_throttle,
  172. .unthrottle = symbol_unthrottle,
  173. .read_int_callback = symbol_int_callback,
  174. };
  175. static struct usb_serial_driver * const serial_drivers[] = {
  176. &symbol_device, NULL
  177. };
  178. module_usb_serial_driver(serial_drivers, id_table);
  179. MODULE_LICENSE("GPL");