2 * Opticon USB barcode to serial driver
4 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
5 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (C) 2008 - 2009 Novell Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/slab.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
25 #define CONTROL_RTS 0x02
26 #define RESEND_CTS_STATE 0x03
28 /* max number of write urbs in flight */
29 #define URB_UPPER_LIMIT 8
31 /* This driver works for the Opticon 1D barcode reader
32 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
33 #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)"
37 static const struct usb_device_id id_table
[] = {
38 { USB_DEVICE(0x065a, 0x0009) },
41 MODULE_DEVICE_TABLE(usb
, id_table
);
43 /* This structure holds all of the individual device information */
44 struct opticon_private
{
45 struct usb_device
*udev
;
46 struct usb_serial
*serial
;
47 struct usb_serial_port
*port
;
48 unsigned char *bulk_in_buffer
;
49 struct urb
*bulk_read_urb
;
52 spinlock_t lock
; /* protects the following flags */
54 bool actually_throttled
;
62 static void opticon_read_bulk_callback(struct urb
*urb
)
64 struct opticon_private
*priv
= urb
->context
;
65 unsigned char *data
= urb
->transfer_buffer
;
66 struct usb_serial_port
*port
= priv
->port
;
67 int status
= urb
->status
;
68 struct tty_struct
*tty
;
73 dbg("%s - port %d", __func__
, port
->number
);
82 /* this urb is terminated, clean up */
83 dbg("%s - urb shutting down with status: %d",
87 dbg("%s - nonzero urb status received: %d",
92 usb_serial_debug_data(debug
, &port
->dev
, __func__
, urb
->actual_length
,
95 if (urb
->actual_length
> 2) {
96 data_length
= urb
->actual_length
- 2;
99 * Data from the device comes with a 2 byte header:
101 * <0x00><0x00>data...
102 * This is real data to be sent to the tty layer
104 * This is a CTS level change, the third byte is the CTS
105 * value (0 for low, 1 for high).
107 if ((data
[0] == 0x00) && (data
[1] == 0x00)) {
108 /* real data, send it to the tty layer */
109 tty
= tty_port_tty_get(&port
->port
);
111 tty_insert_flip_string(tty
, data
+ 2,
113 tty_flip_buffer_push(tty
);
117 if ((data
[0] == 0x00) && (data
[1] == 0x01)) {
118 spin_lock_irqsave(&priv
->lock
, flags
);
119 /* CTS status information package */
124 spin_unlock_irqrestore(&priv
->lock
, flags
);
126 dev_dbg(&priv
->udev
->dev
,
127 "Unknown data packet received from the device:"
133 dev_dbg(&priv
->udev
->dev
,
134 "Improper amount of data received from the device, "
135 "%d bytes", urb
->actual_length
);
139 spin_lock(&priv
->lock
);
141 /* Continue trying to always read if we should */
142 if (!priv
->throttled
) {
143 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
144 usb_rcvbulkpipe(priv
->udev
,
146 priv
->bulk_in_buffer
, priv
->buffer_size
,
147 opticon_read_bulk_callback
, priv
);
148 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
151 "%s - failed resubmitting read urb, error %d\n",
154 priv
->actually_throttled
= true;
155 spin_unlock(&priv
->lock
);
158 static int send_control_msg(struct usb_serial_port
*port
, u8 requesttype
,
161 struct usb_serial
*serial
= port
->serial
;
166 /* Send the message to the vendor control endpoint
167 * of the connected device */
168 retval
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
170 USB_DIR_OUT
|USB_TYPE_VENDOR
|USB_RECIP_INTERFACE
,
176 static int opticon_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
178 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
182 dbg("%s - port %d", __func__
, port
->number
);
184 spin_lock_irqsave(&priv
->lock
, flags
);
185 priv
->throttled
= false;
186 priv
->actually_throttled
= false;
189 spin_unlock_irqrestore(&priv
->lock
, flags
);
192 send_control_msg(port
, CONTROL_RTS
, 0);
194 /* Setup the read URB and start reading from the device */
195 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
196 usb_rcvbulkpipe(priv
->udev
,
198 priv
->bulk_in_buffer
, priv
->buffer_size
,
199 opticon_read_bulk_callback
, priv
);
201 /* clear the halt status of the enpoint */
202 usb_clear_halt(priv
->udev
, priv
->bulk_read_urb
->pipe
);
204 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_KERNEL
);
207 "%s - failed resubmitting read urb, error %d\n",
209 /* Request CTS line state, sometimes during opening the current
210 * CTS state can be missed. */
211 send_control_msg(port
, RESEND_CTS_STATE
, 1);
215 static void opticon_close(struct usb_serial_port
*port
)
217 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
219 dbg("%s - port %d", __func__
, port
->number
);
221 /* shutdown our urbs */
222 usb_kill_urb(priv
->bulk_read_urb
);
225 static void opticon_write_control_callback(struct urb
*urb
)
227 struct opticon_private
*priv
= urb
->context
;
228 int status
= urb
->status
;
231 /* free up the transfer buffer, as usb_free_urb() does not do this */
232 kfree(urb
->transfer_buffer
);
234 /* setup packet may be set if we're using it for writing */
235 kfree(urb
->setup_packet
);
238 dbg("%s - nonzero write bulk status received: %d",
241 spin_lock_irqsave(&priv
->lock
, flags
);
242 --priv
->outstanding_urbs
;
243 spin_unlock_irqrestore(&priv
->lock
, flags
);
245 usb_serial_port_softint(priv
->port
);
248 static int opticon_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
249 const unsigned char *buf
, int count
)
251 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
252 struct usb_serial
*serial
= port
->serial
;
254 unsigned char *buffer
;
257 struct usb_ctrlrequest
*dr
;
259 dbg("%s - port %d", __func__
, port
->number
);
261 spin_lock_irqsave(&priv
->lock
, flags
);
262 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
) {
263 spin_unlock_irqrestore(&priv
->lock
, flags
);
264 dbg("%s - write limit hit", __func__
);
267 priv
->outstanding_urbs
++;
268 spin_unlock_irqrestore(&priv
->lock
, flags
);
270 buffer
= kmalloc(count
, GFP_ATOMIC
);
272 dev_err(&port
->dev
, "out of memory\n");
275 goto error_no_buffer
;
278 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
280 dev_err(&port
->dev
, "no more free urbs\n");
285 memcpy(buffer
, buf
, count
);
287 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, buffer
);
289 /* The conncected devices do not have a bulk write endpoint,
290 * to transmit data to de barcode device the control endpoint is used */
291 dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_NOIO
);
293 dev_err(&port
->dev
, "out of memory\n");
298 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
| USB_DIR_OUT
;
302 dr
->wLength
= cpu_to_le16(count
);
304 usb_fill_control_urb(urb
, serial
->dev
,
305 usb_sndctrlpipe(serial
->dev
, 0),
306 (unsigned char *)dr
, buffer
, count
,
307 opticon_write_control_callback
, priv
);
309 /* send it down the pipe */
310 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
313 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
319 /* we are done with this urb, so let the host driver
320 * really free it when it is finished with it */
329 spin_lock_irqsave(&priv
->lock
, flags
);
330 --priv
->outstanding_urbs
;
331 spin_unlock_irqrestore(&priv
->lock
, flags
);
335 static int opticon_write_room(struct tty_struct
*tty
)
337 struct usb_serial_port
*port
= tty
->driver_data
;
338 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
341 dbg("%s - port %d", __func__
, port
->number
);
344 * We really can take almost anything the user throws at us
345 * but let's pick a nice big number to tell the tty
346 * layer that we have lots of free space, unless we don't.
348 spin_lock_irqsave(&priv
->lock
, flags
);
349 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
* 2 / 3) {
350 spin_unlock_irqrestore(&priv
->lock
, flags
);
351 dbg("%s - write limit hit", __func__
);
354 spin_unlock_irqrestore(&priv
->lock
, flags
);
359 static void opticon_throttle(struct tty_struct
*tty
)
361 struct usb_serial_port
*port
= tty
->driver_data
;
362 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
365 dbg("%s - port %d", __func__
, port
->number
);
366 spin_lock_irqsave(&priv
->lock
, flags
);
367 priv
->throttled
= true;
368 spin_unlock_irqrestore(&priv
->lock
, flags
);
372 static void opticon_unthrottle(struct tty_struct
*tty
)
374 struct usb_serial_port
*port
= tty
->driver_data
;
375 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
377 int result
, was_throttled
;
379 dbg("%s - port %d", __func__
, port
->number
);
381 spin_lock_irqsave(&priv
->lock
, flags
);
382 priv
->throttled
= false;
383 was_throttled
= priv
->actually_throttled
;
384 priv
->actually_throttled
= false;
385 spin_unlock_irqrestore(&priv
->lock
, flags
);
387 priv
->bulk_read_urb
->dev
= port
->serial
->dev
;
389 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
392 "%s - failed submitting read urb, error %d\n",
397 static int opticon_tiocmget(struct tty_struct
*tty
)
399 struct usb_serial_port
*port
= tty
->driver_data
;
400 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
404 dbg("%s - port %d", __func__
, port
->number
);
405 if (!usb_get_intfdata(port
->serial
->interface
))
408 spin_lock_irqsave(&priv
->lock
, flags
);
413 spin_unlock_irqrestore(&priv
->lock
, flags
);
415 dbg("%s - %x", __func__
, result
);
419 static int opticon_tiocmset(struct tty_struct
*tty
,
420 unsigned int set
, unsigned int clear
)
422 struct usb_serial_port
*port
= tty
->driver_data
;
423 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
426 bool changed
= false;
428 if (!usb_get_intfdata(port
->serial
->interface
))
430 /* We only support RTS so we only handle that */
431 spin_lock_irqsave(&priv
->lock
, flags
);
436 if (clear
& TIOCM_RTS
)
438 changed
= rts
^ priv
->rts
;
439 spin_unlock_irqrestore(&priv
->lock
, flags
);
444 /* Send the new RTS state to the connected device */
445 return send_control_msg(port
, CONTROL_RTS
, !rts
);
448 static int get_serial_info(struct opticon_private
*priv
,
449 struct serial_struct __user
*serial
)
451 struct serial_struct tmp
;
456 memset(&tmp
, 0x00, sizeof(tmp
));
458 /* fake emulate a 16550 uart to make userspace code happy */
459 tmp
.type
= PORT_16550A
;
460 tmp
.line
= priv
->serial
->minor
;
463 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
464 tmp
.xmit_fifo_size
= 1024;
465 tmp
.baud_base
= 9600;
466 tmp
.close_delay
= 5*HZ
;
467 tmp
.closing_wait
= 30*HZ
;
469 if (copy_to_user(serial
, &tmp
, sizeof(*serial
)))
474 static int opticon_ioctl(struct tty_struct
*tty
,
475 unsigned int cmd
, unsigned long arg
)
477 struct usb_serial_port
*port
= tty
->driver_data
;
478 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
480 dbg("%s - port %d, cmd = 0x%x", __func__
, port
->number
, cmd
);
484 return get_serial_info(priv
,
485 (struct serial_struct __user
*)arg
);
491 static int opticon_startup(struct usb_serial
*serial
)
493 struct opticon_private
*priv
;
494 struct usb_host_interface
*intf
;
496 int retval
= -ENOMEM
;
497 bool bulk_in_found
= false;
499 /* create our private serial structure */
500 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
502 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
505 spin_lock_init(&priv
->lock
);
506 priv
->serial
= serial
;
507 priv
->port
= serial
->port
[0];
508 priv
->udev
= serial
->dev
;
509 priv
->outstanding_urbs
= 0; /* Init the outstanding urbs */
511 /* find our bulk endpoint */
512 intf
= serial
->interface
->altsetting
;
513 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; ++i
) {
514 struct usb_endpoint_descriptor
*endpoint
;
516 endpoint
= &intf
->endpoint
[i
].desc
;
517 if (!usb_endpoint_is_bulk_in(endpoint
))
520 priv
->bulk_read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
521 if (!priv
->bulk_read_urb
) {
522 dev_err(&priv
->udev
->dev
, "out of memory\n");
526 priv
->buffer_size
= usb_endpoint_maxp(endpoint
) * 2;
527 priv
->bulk_in_buffer
= kmalloc(priv
->buffer_size
, GFP_KERNEL
);
528 if (!priv
->bulk_in_buffer
) {
529 dev_err(&priv
->udev
->dev
, "out of memory\n");
533 priv
->bulk_address
= endpoint
->bEndpointAddress
;
535 bulk_in_found
= true;
539 if (!bulk_in_found
) {
540 dev_err(&priv
->udev
->dev
,
541 "Error - the proper endpoints were not found!\n");
545 usb_set_serial_data(serial
, priv
);
549 usb_free_urb(priv
->bulk_read_urb
);
550 kfree(priv
->bulk_in_buffer
);
555 static void opticon_disconnect(struct usb_serial
*serial
)
557 struct opticon_private
*priv
= usb_get_serial_data(serial
);
561 usb_kill_urb(priv
->bulk_read_urb
);
562 usb_free_urb(priv
->bulk_read_urb
);
565 static void opticon_release(struct usb_serial
*serial
)
567 struct opticon_private
*priv
= usb_get_serial_data(serial
);
571 kfree(priv
->bulk_in_buffer
);
575 static int opticon_suspend(struct usb_interface
*intf
, pm_message_t message
)
577 struct usb_serial
*serial
= usb_get_intfdata(intf
);
578 struct opticon_private
*priv
= usb_get_serial_data(serial
);
580 usb_kill_urb(priv
->bulk_read_urb
);
584 static int opticon_resume(struct usb_interface
*intf
)
586 struct usb_serial
*serial
= usb_get_intfdata(intf
);
587 struct opticon_private
*priv
= usb_get_serial_data(serial
);
588 struct usb_serial_port
*port
= serial
->port
[0];
591 mutex_lock(&port
->port
.mutex
);
592 /* This is protected by the port mutex against close/open */
593 if (test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
594 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_NOIO
);
597 mutex_unlock(&port
->port
.mutex
);
601 static struct usb_driver opticon_driver
= {
603 .probe
= usb_serial_probe
,
604 .disconnect
= usb_serial_disconnect
,
605 .suspend
= opticon_suspend
,
606 .resume
= opticon_resume
,
607 .id_table
= id_table
,
611 static struct usb_serial_driver opticon_device
= {
613 .owner
= THIS_MODULE
,
616 .id_table
= id_table
,
617 .usb_driver
= &opticon_driver
,
619 .attach
= opticon_startup
,
620 .open
= opticon_open
,
621 .close
= opticon_close
,
622 .write
= opticon_write
,
623 .write_room
= opticon_write_room
,
624 .disconnect
= opticon_disconnect
,
625 .release
= opticon_release
,
626 .throttle
= opticon_throttle
,
627 .unthrottle
= opticon_unthrottle
,
628 .ioctl
= opticon_ioctl
,
629 .tiocmget
= opticon_tiocmget
,
630 .tiocmset
= opticon_tiocmset
,
633 static int __init
opticon_init(void)
637 retval
= usb_serial_register(&opticon_device
);
640 retval
= usb_register(&opticon_driver
);
642 usb_serial_deregister(&opticon_device
);
646 static void __exit
opticon_exit(void)
648 usb_deregister(&opticon_driver
);
649 usb_serial_deregister(&opticon_device
);
652 module_init(opticon_init
);
653 module_exit(opticon_exit
);
654 MODULE_DESCRIPTION(DRIVER_DESC
);
655 MODULE_LICENSE("GPL");
657 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
658 MODULE_PARM_DESC(debug
, "Debug enabled or not");