2 * Opticon USB barcode to serial driver
4 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
5 * Copyright (C) 2008 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/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>
24 static struct usb_device_id id_table
[] = {
25 { USB_DEVICE(0x065a, 0x0009) },
28 MODULE_DEVICE_TABLE(usb
, id_table
);
30 /* This structure holds all of the individual device information */
31 struct opticon_private
{
32 struct usb_device
*udev
;
33 struct usb_serial
*serial
;
34 struct usb_serial_port
*port
;
35 unsigned char *bulk_in_buffer
;
36 struct urb
*bulk_read_urb
;
39 spinlock_t lock
; /* protects the following flags */
41 bool actually_throttled
;
45 static void opticon_bulk_callback(struct urb
*urb
)
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
;
53 int available_room
= 0;
56 dbg("%s - port %d", __func__
, port
->number
);
65 /* this urb is terminated, clean up */
66 dbg("%s - urb shutting down with status: %d",
70 dbg("%s - nonzero urb status received: %d",
75 usb_serial_debug_data(debug
, &port
->dev
, __func__
, urb
->actual_length
,
78 if (urb
->actual_length
> 2) {
79 data_length
= urb
->actual_length
- 2;
82 * Data from the device comes with a 2 byte header:
85 * This is real data to be sent to the tty layer
87 * This is a RTS level change, the third byte is the RTS
88 * value (0 for low, 1 for high).
90 if ((data
[0] == 0x00) && (data
[1] == 0x00)) {
91 /* real data, send it to the tty layer */
92 tty
= tty_port_tty_get(&port
->port
);
94 available_room
= tty_buffer_request_room(tty
,
97 tty_insert_flip_string(tty
, data
,
99 tty_flip_buffer_push(tty
);
104 if ((data
[0] == 0x00) && (data
[1] == 0x01)) {
109 /* FIXME change the RTS level */
111 dev_dbg(&priv
->udev
->dev
,
112 "Unknown data packet received from the device:"
118 dev_dbg(&priv
->udev
->dev
,
119 "Improper ammount of data received from the device, "
120 "%d bytes", urb
->actual_length
);
124 spin_lock(&priv
->lock
);
126 /* Continue trying to always read if we should */
127 if (!priv
->throttled
) {
128 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
129 usb_rcvbulkpipe(priv
->udev
,
131 priv
->bulk_in_buffer
, priv
->buffer_size
,
132 opticon_bulk_callback
, priv
);
133 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
136 "%s - failed resubmitting read urb, error %d\n",
139 priv
->actually_throttled
= true;
140 spin_unlock(&priv
->lock
);
143 static int opticon_open(struct tty_struct
*tty
, struct usb_serial_port
*port
,
146 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
150 dbg("%s - port %d", __func__
, port
->number
);
152 spin_lock_irqsave(&priv
->lock
, flags
);
153 priv
->throttled
= false;
154 priv
->actually_throttled
= false;
156 spin_unlock_irqrestore(&priv
->lock
, flags
);
159 * Force low_latency on so that our tty_push actually forces the data
160 * through, otherwise it is scheduled, and with high data rates (like
161 * with OHCI) data can get lost.
164 tty
->low_latency
= 1;
166 /* Start reading from the device */
167 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
168 usb_rcvbulkpipe(priv
->udev
,
170 priv
->bulk_in_buffer
, priv
->buffer_size
,
171 opticon_bulk_callback
, priv
);
172 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_KERNEL
);
175 "%s - failed resubmitting read urb, error %d\n",
180 static void opticon_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
183 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
185 dbg("%s - port %d", __func__
, port
->number
);
187 /* shutdown our urbs */
188 usb_kill_urb(priv
->bulk_read_urb
);
191 static void opticon_throttle(struct tty_struct
*tty
)
193 struct usb_serial_port
*port
= tty
->driver_data
;
194 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
197 dbg("%s - port %d", __func__
, port
->number
);
198 spin_lock_irqsave(&priv
->lock
, flags
);
199 priv
->throttled
= true;
200 spin_unlock_irqrestore(&priv
->lock
, flags
);
204 static void opticon_unthrottle(struct tty_struct
*tty
)
206 struct usb_serial_port
*port
= tty
->driver_data
;
207 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
211 dbg("%s - port %d", __func__
, port
->number
);
213 spin_lock_irqsave(&priv
->lock
, flags
);
214 priv
->throttled
= false;
215 priv
->actually_throttled
= false;
216 spin_unlock_irqrestore(&priv
->lock
, flags
);
218 priv
->bulk_read_urb
->dev
= port
->serial
->dev
;
219 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
222 "%s - failed submitting read urb, error %d\n",
226 static int opticon_startup(struct usb_serial
*serial
)
228 struct opticon_private
*priv
;
229 struct usb_host_interface
*intf
;
231 int retval
= -ENOMEM
;
232 bool bulk_in_found
= false;
234 /* create our private serial structure */
235 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
237 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
240 spin_lock_init(&priv
->lock
);
241 priv
->serial
= serial
;
242 priv
->port
= serial
->port
[0];
243 priv
->udev
= serial
->dev
;
245 /* find our bulk endpoint */
246 intf
= serial
->interface
->altsetting
;
247 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; ++i
) {
248 struct usb_endpoint_descriptor
*endpoint
;
250 endpoint
= &intf
->endpoint
[i
].desc
;
251 if (!usb_endpoint_is_bulk_in(endpoint
))
254 priv
->bulk_read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
255 if (!priv
->bulk_read_urb
) {
256 dev_err(&priv
->udev
->dev
, "out of memory\n");
260 priv
->buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
) * 2;
261 priv
->bulk_in_buffer
= kmalloc(priv
->buffer_size
, GFP_KERNEL
);
262 if (!priv
->bulk_in_buffer
) {
263 dev_err(&priv
->udev
->dev
, "out of memory\n");
267 priv
->bulk_address
= endpoint
->bEndpointAddress
;
269 /* set up our bulk urb */
270 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
271 usb_rcvbulkpipe(priv
->udev
,
272 endpoint
->bEndpointAddress
),
273 priv
->bulk_in_buffer
, priv
->buffer_size
,
274 opticon_bulk_callback
, priv
);
276 bulk_in_found
= true;
280 if (!bulk_in_found
) {
281 dev_err(&priv
->udev
->dev
,
282 "Error - the proper endpoints were not found!\n");
286 usb_set_serial_data(serial
, priv
);
290 usb_free_urb(priv
->bulk_read_urb
);
291 kfree(priv
->bulk_in_buffer
);
296 static void opticon_shutdown(struct usb_serial
*serial
)
298 struct opticon_private
*priv
= usb_get_serial_data(serial
);
302 usb_kill_urb(priv
->bulk_read_urb
);
303 usb_free_urb(priv
->bulk_read_urb
);
304 kfree(priv
->bulk_in_buffer
);
306 usb_set_serial_data(serial
, NULL
);
310 static struct usb_driver opticon_driver
= {
312 .probe
= usb_serial_probe
,
313 .disconnect
= usb_serial_disconnect
,
314 .id_table
= id_table
,
318 static struct usb_serial_driver opticon_device
= {
320 .owner
= THIS_MODULE
,
323 .id_table
= id_table
,
324 .usb_driver
= &opticon_driver
,
326 .attach
= opticon_startup
,
327 .open
= opticon_open
,
328 .close
= opticon_close
,
329 .shutdown
= opticon_shutdown
,
330 .throttle
= opticon_throttle
,
331 .unthrottle
= opticon_unthrottle
,
334 static int __init
opticon_init(void)
338 retval
= usb_serial_register(&opticon_device
);
341 retval
= usb_register(&opticon_driver
);
343 usb_serial_deregister(&opticon_device
);
347 static void __exit
opticon_exit(void)
349 usb_deregister(&opticon_driver
);
350 usb_serial_deregister(&opticon_device
);
353 module_init(opticon_init
);
354 module_exit(opticon_exit
);
355 MODULE_LICENSE("GPL");
357 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
358 MODULE_PARM_DESC(debug
, "Debug enabled or not");