2 * Symbol USB barcode to serial driver
4 * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
5 * Copyright (C) 2009 Novell Inc.
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.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/tty.h>
15 #include <linux/slab.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
23 static const struct usb_device_id id_table[] = {
24 { USB_DEVICE(0x05e0, 0x0600) },
27 MODULE_DEVICE_TABLE(usb, id_table);
29 /* This structure holds all of the individual device information */
30 struct symbol_private {
31 struct usb_device *udev;
32 struct usb_serial *serial;
33 struct usb_serial_port *port;
34 unsigned char *int_buffer;
39 spinlock_t lock; /* protects the following flags */
41 bool actually_throttled;
45 static void symbol_int_callback(struct urb *urb)
47 struct symbol_private *priv = urb->context;
48 unsigned char *data = urb->transfer_buffer;
49 struct usb_serial_port *port = priv->port;
50 int status = urb->status;
61 /* this urb is terminated, clean up */
62 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
66 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
71 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
73 if (urb->actual_length > 1) {
74 data_length = urb->actual_length - 1;
77 * Data from the device comes with a 1 byte header:
79 * <size of data>data...
80 * This is real data to be sent to the tty layer
81 * we pretty much just ignore the size and send everything
82 * else to the tty layer.
84 tty_insert_flip_string(&port->port, &data[1], data_length);
85 tty_flip_buffer_push(&port->port);
87 dev_dbg(&priv->udev->dev,
88 "Improper amount of data received from the device, "
89 "%d bytes", urb->actual_length);
93 spin_lock(&priv->lock);
95 /* Continue trying to always read if we should */
96 if (!priv->throttled) {
97 usb_fill_int_urb(priv->int_urb, priv->udev,
98 usb_rcvintpipe(priv->udev,
100 priv->int_buffer, priv->buffer_size,
101 symbol_int_callback, priv, priv->bInterval);
102 result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
105 "%s - failed resubmitting read urb, error %d\n",
108 priv->actually_throttled = true;
109 spin_unlock(&priv->lock);
112 static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
114 struct symbol_private *priv = usb_get_serial_data(port->serial);
118 spin_lock_irqsave(&priv->lock, flags);
119 priv->throttled = false;
120 priv->actually_throttled = false;
122 spin_unlock_irqrestore(&priv->lock, flags);
124 /* Start reading from the device */
125 usb_fill_int_urb(priv->int_urb, priv->udev,
126 usb_rcvintpipe(priv->udev, priv->int_address),
127 priv->int_buffer, priv->buffer_size,
128 symbol_int_callback, priv, priv->bInterval);
129 result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
132 "%s - failed resubmitting read urb, error %d\n",
137 static void symbol_close(struct usb_serial_port *port)
139 struct symbol_private *priv = usb_get_serial_data(port->serial);
141 /* shutdown our urbs */
142 usb_kill_urb(priv->int_urb);
145 static void symbol_throttle(struct tty_struct *tty)
147 struct usb_serial_port *port = tty->driver_data;
148 struct symbol_private *priv = usb_get_serial_data(port->serial);
150 spin_lock_irq(&priv->lock);
151 priv->throttled = true;
152 spin_unlock_irq(&priv->lock);
155 static void symbol_unthrottle(struct tty_struct *tty)
157 struct usb_serial_port *port = tty->driver_data;
158 struct symbol_private *priv = usb_get_serial_data(port->serial);
162 spin_lock_irq(&priv->lock);
163 priv->throttled = false;
164 was_throttled = priv->actually_throttled;
165 priv->actually_throttled = false;
166 spin_unlock_irq(&priv->lock);
169 result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
172 "%s - failed submitting read urb, error %d\n",
177 static int symbol_startup(struct usb_serial *serial)
179 struct symbol_private *priv;
180 struct usb_host_interface *intf;
182 int retval = -ENOMEM;
183 bool int_in_found = false;
185 /* create our private serial structure */
186 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
188 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
191 spin_lock_init(&priv->lock);
192 priv->serial = serial;
193 priv->port = serial->port[0];
194 priv->udev = serial->dev;
196 /* find our interrupt endpoint */
197 intf = serial->interface->altsetting;
198 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
199 struct usb_endpoint_descriptor *endpoint;
201 endpoint = &intf->endpoint[i].desc;
202 if (!usb_endpoint_is_int_in(endpoint))
205 priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
206 if (!priv->int_urb) {
207 dev_err(&priv->udev->dev, "out of memory\n");
211 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
212 priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
213 if (!priv->int_buffer) {
214 dev_err(&priv->udev->dev, "out of memory\n");
218 priv->int_address = endpoint->bEndpointAddress;
219 priv->bInterval = endpoint->bInterval;
221 /* set up our int urb */
222 usb_fill_int_urb(priv->int_urb, priv->udev,
223 usb_rcvintpipe(priv->udev,
224 endpoint->bEndpointAddress),
225 priv->int_buffer, priv->buffer_size,
226 symbol_int_callback, priv, priv->bInterval);
233 dev_err(&priv->udev->dev,
234 "Error - the proper endpoints were not found!\n");
238 usb_set_serial_data(serial, priv);
242 usb_free_urb(priv->int_urb);
243 kfree(priv->int_buffer);
248 static void symbol_disconnect(struct usb_serial *serial)
250 struct symbol_private *priv = usb_get_serial_data(serial);
252 usb_kill_urb(priv->int_urb);
253 usb_free_urb(priv->int_urb);
256 static void symbol_release(struct usb_serial *serial)
258 struct symbol_private *priv = usb_get_serial_data(serial);
260 kfree(priv->int_buffer);
264 static struct usb_serial_driver symbol_device = {
266 .owner = THIS_MODULE,
269 .id_table = id_table,
271 .attach = symbol_startup,
273 .close = symbol_close,
274 .disconnect = symbol_disconnect,
275 .release = symbol_release,
276 .throttle = symbol_throttle,
277 .unthrottle = symbol_unthrottle,
280 static struct usb_serial_driver * const serial_drivers[] = {
284 module_usb_serial_driver(serial_drivers, id_table);
286 MODULE_LICENSE("GPL");