2 * Opticon USB barcode to serial driver
4 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
5 * Copyright (C) 2008 - 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/tty_driver.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
25 static struct usb_device_id id_table
[] = {
26 { USB_DEVICE(0x065a, 0x0009) },
29 MODULE_DEVICE_TABLE(usb
, id_table
);
31 /* This structure holds all of the individual device information */
32 struct opticon_private
{
33 struct usb_device
*udev
;
34 struct usb_serial
*serial
;
35 struct usb_serial_port
*port
;
36 unsigned char *bulk_in_buffer
;
37 struct urb
*bulk_read_urb
;
40 spinlock_t lock
; /* protects the following flags */
42 bool actually_throttled
;
47 /* max number of write urbs in flight */
48 #define URB_UPPER_LIMIT 4
50 static void opticon_bulk_callback(struct urb
*urb
)
52 struct opticon_private
*priv
= urb
->context
;
53 unsigned char *data
= urb
->transfer_buffer
;
54 struct usb_serial_port
*port
= priv
->port
;
55 int status
= urb
->status
;
56 struct tty_struct
*tty
;
58 int available_room
= 0;
61 dbg("%s - port %d", __func__
, port
->number
);
70 /* this urb is terminated, clean up */
71 dbg("%s - urb shutting down with status: %d",
75 dbg("%s - nonzero urb status received: %d",
80 usb_serial_debug_data(debug
, &port
->dev
, __func__
, urb
->actual_length
,
83 if (urb
->actual_length
> 2) {
84 data_length
= urb
->actual_length
- 2;
87 * Data from the device comes with a 2 byte header:
90 * This is real data to be sent to the tty layer
92 * This is a RTS level change, the third byte is the RTS
93 * value (0 for low, 1 for high).
95 if ((data
[0] == 0x00) && (data
[1] == 0x00)) {
96 /* real data, send it to the tty layer */
97 tty
= tty_port_tty_get(&port
->port
);
99 available_room
= tty_buffer_request_room(tty
,
101 if (available_room
) {
102 tty_insert_flip_string(tty
, data
,
104 tty_flip_buffer_push(tty
);
109 if ((data
[0] == 0x00) && (data
[1] == 0x01)) {
115 dev_dbg(&priv
->udev
->dev
,
116 "Unknown data packet received from the device:"
122 dev_dbg(&priv
->udev
->dev
,
123 "Improper ammount of data received from the device, "
124 "%d bytes", urb
->actual_length
);
128 spin_lock(&priv
->lock
);
130 /* Continue trying to always read if we should */
131 if (!priv
->throttled
) {
132 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
133 usb_rcvbulkpipe(priv
->udev
,
135 priv
->bulk_in_buffer
, priv
->buffer_size
,
136 opticon_bulk_callback
, priv
);
137 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
140 "%s - failed resubmitting read urb, error %d\n",
143 priv
->actually_throttled
= true;
144 spin_unlock(&priv
->lock
);
147 static int opticon_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
149 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
153 dbg("%s - port %d", __func__
, port
->number
);
155 spin_lock_irqsave(&priv
->lock
, flags
);
156 priv
->throttled
= false;
157 priv
->actually_throttled
= false;
159 spin_unlock_irqrestore(&priv
->lock
, flags
);
161 /* Start reading from the device */
162 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
163 usb_rcvbulkpipe(priv
->udev
,
165 priv
->bulk_in_buffer
, priv
->buffer_size
,
166 opticon_bulk_callback
, priv
);
167 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_KERNEL
);
170 "%s - failed resubmitting read urb, error %d\n",
175 static void opticon_close(struct usb_serial_port
*port
)
177 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
179 dbg("%s - port %d", __func__
, port
->number
);
181 /* shutdown our urbs */
182 usb_kill_urb(priv
->bulk_read_urb
);
185 static void opticon_write_bulk_callback(struct urb
*urb
)
187 struct opticon_private
*priv
= urb
->context
;
188 int status
= urb
->status
;
191 /* free up the transfer buffer, as usb_free_urb() does not do this */
192 kfree(urb
->transfer_buffer
);
195 dbg("%s - nonzero write bulk status received: %d",
198 spin_lock_irqsave(&priv
->lock
, flags
);
199 --priv
->outstanding_urbs
;
200 spin_unlock_irqrestore(&priv
->lock
, flags
);
202 usb_serial_port_softint(priv
->port
);
205 static int opticon_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
206 const unsigned char *buf
, int count
)
208 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
209 struct usb_serial
*serial
= port
->serial
;
211 unsigned char *buffer
;
215 dbg("%s - port %d", __func__
, port
->number
);
217 spin_lock_irqsave(&priv
->lock
, flags
);
218 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
) {
219 spin_unlock_irqrestore(&priv
->lock
, flags
);
220 dbg("%s - write limit hit\n", __func__
);
223 priv
->outstanding_urbs
++;
224 spin_unlock_irqrestore(&priv
->lock
, flags
);
226 buffer
= kmalloc(count
, GFP_ATOMIC
);
228 dev_err(&port
->dev
, "out of memory\n");
230 goto error_no_buffer
;
233 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
235 dev_err(&port
->dev
, "no more free urbs\n");
240 memcpy(buffer
, buf
, count
);
242 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, buffer
);
244 usb_fill_bulk_urb(urb
, serial
->dev
,
245 usb_sndbulkpipe(serial
->dev
,
246 port
->bulk_out_endpointAddress
),
247 buffer
, count
, opticon_write_bulk_callback
, priv
);
249 /* send it down the pipe */
250 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
253 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
259 /* we are done with this urb, so let the host driver
260 * really free it when it is finished with it */
269 spin_lock_irqsave(&priv
->lock
, flags
);
270 --priv
->outstanding_urbs
;
271 spin_unlock_irqrestore(&priv
->lock
, flags
);
275 static int opticon_write_room(struct tty_struct
*tty
)
277 struct usb_serial_port
*port
= tty
->driver_data
;
278 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
281 dbg("%s - port %d", __func__
, port
->number
);
284 * We really can take almost anything the user throws at us
285 * but let's pick a nice big number to tell the tty
286 * layer that we have lots of free space, unless we don't.
288 spin_lock_irqsave(&priv
->lock
, flags
);
289 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
* 2 / 3) {
290 spin_unlock_irqrestore(&priv
->lock
, flags
);
291 dbg("%s - write limit hit\n", __func__
);
294 spin_unlock_irqrestore(&priv
->lock
, flags
);
299 static void opticon_throttle(struct tty_struct
*tty
)
301 struct usb_serial_port
*port
= tty
->driver_data
;
302 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
305 dbg("%s - port %d", __func__
, port
->number
);
306 spin_lock_irqsave(&priv
->lock
, flags
);
307 priv
->throttled
= true;
308 spin_unlock_irqrestore(&priv
->lock
, flags
);
312 static void opticon_unthrottle(struct tty_struct
*tty
)
314 struct usb_serial_port
*port
= tty
->driver_data
;
315 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
319 dbg("%s - port %d", __func__
, port
->number
);
321 spin_lock_irqsave(&priv
->lock
, flags
);
322 priv
->throttled
= false;
323 priv
->actually_throttled
= false;
324 spin_unlock_irqrestore(&priv
->lock
, flags
);
326 priv
->bulk_read_urb
->dev
= port
->serial
->dev
;
327 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
330 "%s - failed submitting read urb, error %d\n",
334 static int opticon_tiocmget(struct tty_struct
*tty
, struct file
*file
)
336 struct usb_serial_port
*port
= tty
->driver_data
;
337 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
341 dbg("%s - port %d", __func__
, port
->number
);
343 spin_lock_irqsave(&priv
->lock
, flags
);
346 spin_unlock_irqrestore(&priv
->lock
, flags
);
348 dbg("%s - %x", __func__
, result
);
352 static int get_serial_info(struct opticon_private
*priv
,
353 struct serial_struct __user
*serial
)
355 struct serial_struct tmp
;
360 memset(&tmp
, 0x00, sizeof(tmp
));
362 /* fake emulate a 16550 uart to make userspace code happy */
363 tmp
.type
= PORT_16550A
;
364 tmp
.line
= priv
->serial
->minor
;
367 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
368 tmp
.xmit_fifo_size
= 1024;
369 tmp
.baud_base
= 9600;
370 tmp
.close_delay
= 5*HZ
;
371 tmp
.closing_wait
= 30*HZ
;
373 if (copy_to_user(serial
, &tmp
, sizeof(*serial
)))
378 static int opticon_ioctl(struct tty_struct
*tty
, struct file
*file
,
379 unsigned int cmd
, unsigned long arg
)
381 struct usb_serial_port
*port
= tty
->driver_data
;
382 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
384 dbg("%s - port %d, cmd = 0x%x", __func__
, port
->number
, cmd
);
388 return get_serial_info(priv
,
389 (struct serial_struct __user
*)arg
);
395 static int opticon_startup(struct usb_serial
*serial
)
397 struct opticon_private
*priv
;
398 struct usb_host_interface
*intf
;
400 int retval
= -ENOMEM
;
401 bool bulk_in_found
= false;
403 /* create our private serial structure */
404 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
406 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
409 spin_lock_init(&priv
->lock
);
410 priv
->serial
= serial
;
411 priv
->port
= serial
->port
[0];
412 priv
->udev
= serial
->dev
;
414 /* find our bulk endpoint */
415 intf
= serial
->interface
->altsetting
;
416 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; ++i
) {
417 struct usb_endpoint_descriptor
*endpoint
;
419 endpoint
= &intf
->endpoint
[i
].desc
;
420 if (!usb_endpoint_is_bulk_in(endpoint
))
423 priv
->bulk_read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
424 if (!priv
->bulk_read_urb
) {
425 dev_err(&priv
->udev
->dev
, "out of memory\n");
429 priv
->buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
) * 2;
430 priv
->bulk_in_buffer
= kmalloc(priv
->buffer_size
, GFP_KERNEL
);
431 if (!priv
->bulk_in_buffer
) {
432 dev_err(&priv
->udev
->dev
, "out of memory\n");
436 priv
->bulk_address
= endpoint
->bEndpointAddress
;
438 /* set up our bulk urb */
439 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
440 usb_rcvbulkpipe(priv
->udev
,
441 endpoint
->bEndpointAddress
),
442 priv
->bulk_in_buffer
, priv
->buffer_size
,
443 opticon_bulk_callback
, priv
);
445 bulk_in_found
= true;
449 if (!bulk_in_found
) {
450 dev_err(&priv
->udev
->dev
,
451 "Error - the proper endpoints were not found!\n");
455 usb_set_serial_data(serial
, priv
);
459 usb_free_urb(priv
->bulk_read_urb
);
460 kfree(priv
->bulk_in_buffer
);
465 static void opticon_disconnect(struct usb_serial
*serial
)
467 struct opticon_private
*priv
= usb_get_serial_data(serial
);
471 usb_kill_urb(priv
->bulk_read_urb
);
472 usb_free_urb(priv
->bulk_read_urb
);
475 static void opticon_release(struct usb_serial
*serial
)
477 struct opticon_private
*priv
= usb_get_serial_data(serial
);
481 kfree(priv
->bulk_in_buffer
);
485 static int opticon_suspend(struct usb_interface
*intf
, pm_message_t message
)
487 struct usb_serial
*serial
= usb_get_intfdata(intf
);
488 struct opticon_private
*priv
= usb_get_serial_data(serial
);
490 usb_kill_urb(priv
->bulk_read_urb
);
494 static int opticon_resume(struct usb_interface
*intf
)
496 struct usb_serial
*serial
= usb_get_intfdata(intf
);
497 struct opticon_private
*priv
= usb_get_serial_data(serial
);
498 struct usb_serial_port
*port
= serial
->port
[0];
501 mutex_lock(&port
->mutex
);
502 if (port
->port
.count
)
503 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_NOIO
);
506 mutex_unlock(&port
->mutex
);
510 static struct usb_driver opticon_driver
= {
512 .probe
= usb_serial_probe
,
513 .disconnect
= usb_serial_disconnect
,
514 .suspend
= opticon_suspend
,
515 .resume
= opticon_resume
,
516 .id_table
= id_table
,
520 static struct usb_serial_driver opticon_device
= {
522 .owner
= THIS_MODULE
,
525 .id_table
= id_table
,
526 .usb_driver
= &opticon_driver
,
528 .attach
= opticon_startup
,
529 .open
= opticon_open
,
530 .close
= opticon_close
,
531 .write
= opticon_write
,
532 .write_room
= opticon_write_room
,
533 .disconnect
= opticon_disconnect
,
534 .release
= opticon_release
,
535 .throttle
= opticon_throttle
,
536 .unthrottle
= opticon_unthrottle
,
537 .ioctl
= opticon_ioctl
,
538 .tiocmget
= opticon_tiocmget
,
541 static int __init
opticon_init(void)
545 retval
= usb_serial_register(&opticon_device
);
548 retval
= usb_register(&opticon_driver
);
550 usb_serial_deregister(&opticon_device
);
554 static void __exit
opticon_exit(void)
556 usb_deregister(&opticon_driver
);
557 usb_serial_deregister(&opticon_device
);
560 module_init(opticon_init
);
561 module_exit(opticon_exit
);
562 MODULE_LICENSE("GPL");
564 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
565 MODULE_PARM_DESC(debug
, "Debug enabled or not");