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
;
51 struct tty_struct
*tty
;
62 /* this urb is terminated, clean up */
63 dev_dbg(&port
->dev
, "%s - urb shutting down with status: %d\n",
67 dev_dbg(&port
->dev
, "%s - nonzero urb status received: %d\n",
72 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
74 if (urb
->actual_length
> 1) {
75 data_length
= urb
->actual_length
- 1;
78 * Data from the device comes with a 1 byte header:
80 * <size of data>data...
81 * This is real data to be sent to the tty layer
82 * we pretty much just ignore the size and send everything
83 * else to the tty layer.
85 tty
= tty_port_tty_get(&port
->port
);
87 tty_insert_flip_string(tty
, &data
[1], data_length
);
88 tty_flip_buffer_push(tty
);
92 dev_dbg(&priv
->udev
->dev
,
93 "Improper amount of data received from the device, "
94 "%d bytes", urb
->actual_length
);
98 spin_lock(&priv
->lock
);
100 /* Continue trying to always read if we should */
101 if (!priv
->throttled
) {
102 usb_fill_int_urb(priv
->int_urb
, priv
->udev
,
103 usb_rcvintpipe(priv
->udev
,
105 priv
->int_buffer
, priv
->buffer_size
,
106 symbol_int_callback
, priv
, priv
->bInterval
);
107 result
= usb_submit_urb(priv
->int_urb
, GFP_ATOMIC
);
110 "%s - failed resubmitting read urb, error %d\n",
113 priv
->actually_throttled
= true;
114 spin_unlock(&priv
->lock
);
117 static int symbol_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
119 struct symbol_private
*priv
= usb_get_serial_data(port
->serial
);
123 spin_lock_irqsave(&priv
->lock
, flags
);
124 priv
->throttled
= false;
125 priv
->actually_throttled
= false;
127 spin_unlock_irqrestore(&priv
->lock
, flags
);
129 /* Start reading from the device */
130 usb_fill_int_urb(priv
->int_urb
, priv
->udev
,
131 usb_rcvintpipe(priv
->udev
, priv
->int_address
),
132 priv
->int_buffer
, priv
->buffer_size
,
133 symbol_int_callback
, priv
, priv
->bInterval
);
134 result
= usb_submit_urb(priv
->int_urb
, GFP_KERNEL
);
137 "%s - failed resubmitting read urb, error %d\n",
142 static void symbol_close(struct usb_serial_port
*port
)
144 struct symbol_private
*priv
= usb_get_serial_data(port
->serial
);
146 /* shutdown our urbs */
147 usb_kill_urb(priv
->int_urb
);
150 static void symbol_throttle(struct tty_struct
*tty
)
152 struct usb_serial_port
*port
= tty
->driver_data
;
153 struct symbol_private
*priv
= usb_get_serial_data(port
->serial
);
155 spin_lock_irq(&priv
->lock
);
156 priv
->throttled
= true;
157 spin_unlock_irq(&priv
->lock
);
160 static void symbol_unthrottle(struct tty_struct
*tty
)
162 struct usb_serial_port
*port
= tty
->driver_data
;
163 struct symbol_private
*priv
= usb_get_serial_data(port
->serial
);
167 spin_lock_irq(&priv
->lock
);
168 priv
->throttled
= false;
169 was_throttled
= priv
->actually_throttled
;
170 priv
->actually_throttled
= false;
171 spin_unlock_irq(&priv
->lock
);
174 result
= usb_submit_urb(priv
->int_urb
, GFP_KERNEL
);
177 "%s - failed submitting read urb, error %d\n",
182 static int symbol_startup(struct usb_serial
*serial
)
184 struct symbol_private
*priv
;
185 struct usb_host_interface
*intf
;
187 int retval
= -ENOMEM
;
188 bool int_in_found
= false;
190 /* create our private serial structure */
191 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
193 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
196 spin_lock_init(&priv
->lock
);
197 priv
->serial
= serial
;
198 priv
->port
= serial
->port
[0];
199 priv
->udev
= serial
->dev
;
201 /* find our interrupt endpoint */
202 intf
= serial
->interface
->altsetting
;
203 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; ++i
) {
204 struct usb_endpoint_descriptor
*endpoint
;
206 endpoint
= &intf
->endpoint
[i
].desc
;
207 if (!usb_endpoint_is_int_in(endpoint
))
210 priv
->int_urb
= usb_alloc_urb(0, GFP_KERNEL
);
211 if (!priv
->int_urb
) {
212 dev_err(&priv
->udev
->dev
, "out of memory\n");
216 priv
->buffer_size
= usb_endpoint_maxp(endpoint
) * 2;
217 priv
->int_buffer
= kmalloc(priv
->buffer_size
, GFP_KERNEL
);
218 if (!priv
->int_buffer
) {
219 dev_err(&priv
->udev
->dev
, "out of memory\n");
223 priv
->int_address
= endpoint
->bEndpointAddress
;
224 priv
->bInterval
= endpoint
->bInterval
;
226 /* set up our int urb */
227 usb_fill_int_urb(priv
->int_urb
, priv
->udev
,
228 usb_rcvintpipe(priv
->udev
,
229 endpoint
->bEndpointAddress
),
230 priv
->int_buffer
, priv
->buffer_size
,
231 symbol_int_callback
, priv
, priv
->bInterval
);
238 dev_err(&priv
->udev
->dev
,
239 "Error - the proper endpoints were not found!\n");
243 usb_set_serial_data(serial
, priv
);
247 usb_free_urb(priv
->int_urb
);
248 kfree(priv
->int_buffer
);
253 static void symbol_disconnect(struct usb_serial
*serial
)
255 struct symbol_private
*priv
= usb_get_serial_data(serial
);
257 usb_kill_urb(priv
->int_urb
);
258 usb_free_urb(priv
->int_urb
);
261 static void symbol_release(struct usb_serial
*serial
)
263 struct symbol_private
*priv
= usb_get_serial_data(serial
);
265 kfree(priv
->int_buffer
);
269 static struct usb_serial_driver symbol_device
= {
271 .owner
= THIS_MODULE
,
274 .id_table
= id_table
,
276 .attach
= symbol_startup
,
278 .close
= symbol_close
,
279 .disconnect
= symbol_disconnect
,
280 .release
= symbol_release
,
281 .throttle
= symbol_throttle
,
282 .unthrottle
= symbol_unthrottle
,
285 static struct usb_serial_driver
* const serial_drivers
[] = {
289 module_usb_serial_driver(serial_drivers
, id_table
);
291 MODULE_LICENSE("GPL");