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)"
35 static const struct usb_device_id id_table
[] = {
36 { USB_DEVICE(0x065a, 0x0009) },
39 MODULE_DEVICE_TABLE(usb
, id_table
);
41 /* This structure holds all of the individual device information */
42 struct opticon_private
{
43 struct usb_device
*udev
;
44 struct usb_serial
*serial
;
45 struct usb_serial_port
*port
;
46 unsigned char *bulk_in_buffer
;
47 struct urb
*bulk_read_urb
;
50 spinlock_t lock
; /* protects the following flags */
52 bool actually_throttled
;
60 static void opticon_read_bulk_callback(struct urb
*urb
)
62 struct opticon_private
*priv
= urb
->context
;
63 unsigned char *data
= urb
->transfer_buffer
;
64 struct usb_serial_port
*port
= priv
->port
;
65 int status
= urb
->status
;
66 struct tty_struct
*tty
;
78 /* this urb is terminated, clean up */
79 dev_dbg(&priv
->udev
->dev
, "%s - urb shutting down with status: %d\n",
83 dev_dbg(&priv
->udev
->dev
, "%s - nonzero urb status received: %d\n",
88 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
90 if (urb
->actual_length
> 2) {
91 data_length
= urb
->actual_length
- 2;
94 * Data from the device comes with a 2 byte header:
97 * This is real data to be sent to the tty layer
99 * This is a CTS level change, the third byte is the CTS
100 * value (0 for low, 1 for high).
102 if ((data
[0] == 0x00) && (data
[1] == 0x00)) {
103 /* real data, send it to the tty layer */
104 tty
= tty_port_tty_get(&port
->port
);
106 tty_insert_flip_string(tty
, data
+ 2,
108 tty_flip_buffer_push(tty
);
112 if ((data
[0] == 0x00) && (data
[1] == 0x01)) {
113 spin_lock_irqsave(&priv
->lock
, flags
);
114 /* CTS status information package */
119 spin_unlock_irqrestore(&priv
->lock
, flags
);
121 dev_dbg(&priv
->udev
->dev
,
122 "Unknown data packet received from the device:"
128 dev_dbg(&priv
->udev
->dev
,
129 "Improper amount of data received from the device, "
130 "%d bytes", urb
->actual_length
);
134 spin_lock(&priv
->lock
);
136 /* Continue trying to always read if we should */
137 if (!priv
->throttled
) {
138 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
139 usb_rcvbulkpipe(priv
->udev
,
141 priv
->bulk_in_buffer
, priv
->buffer_size
,
142 opticon_read_bulk_callback
, priv
);
143 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
146 "%s - failed resubmitting read urb, error %d\n",
149 priv
->actually_throttled
= true;
150 spin_unlock(&priv
->lock
);
153 static int send_control_msg(struct usb_serial_port
*port
, u8 requesttype
,
156 struct usb_serial
*serial
= port
->serial
;
160 buffer
= kzalloc(1, GFP_KERNEL
);
165 /* Send the message to the vendor control endpoint
166 * of the connected device */
167 retval
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
169 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 spin_lock_irqsave(&priv
->lock
, flags
);
183 priv
->throttled
= false;
184 priv
->actually_throttled
= false;
187 spin_unlock_irqrestore(&priv
->lock
, flags
);
190 send_control_msg(port
, CONTROL_RTS
, 0);
192 /* Setup the read URB and start reading from the device */
193 usb_fill_bulk_urb(priv
->bulk_read_urb
, priv
->udev
,
194 usb_rcvbulkpipe(priv
->udev
,
196 priv
->bulk_in_buffer
, priv
->buffer_size
,
197 opticon_read_bulk_callback
, priv
);
199 /* clear the halt status of the enpoint */
200 usb_clear_halt(priv
->udev
, priv
->bulk_read_urb
->pipe
);
202 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_KERNEL
);
205 "%s - failed resubmitting read urb, error %d\n",
207 /* Request CTS line state, sometimes during opening the current
208 * CTS state can be missed. */
209 send_control_msg(port
, RESEND_CTS_STATE
, 1);
213 static void opticon_close(struct usb_serial_port
*port
)
215 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
217 /* shutdown our urbs */
218 usb_kill_urb(priv
->bulk_read_urb
);
221 static void opticon_write_control_callback(struct urb
*urb
)
223 struct opticon_private
*priv
= urb
->context
;
224 int status
= urb
->status
;
227 /* free up the transfer buffer, as usb_free_urb() does not do this */
228 kfree(urb
->transfer_buffer
);
230 /* setup packet may be set if we're using it for writing */
231 kfree(urb
->setup_packet
);
234 dev_dbg(&priv
->udev
->dev
, "%s - nonzero write bulk status received: %d\n",
237 spin_lock_irqsave(&priv
->lock
, flags
);
238 --priv
->outstanding_urbs
;
239 spin_unlock_irqrestore(&priv
->lock
, flags
);
241 usb_serial_port_softint(priv
->port
);
244 static int opticon_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
245 const unsigned char *buf
, int count
)
247 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
248 struct usb_serial
*serial
= port
->serial
;
250 unsigned char *buffer
;
253 struct usb_ctrlrequest
*dr
;
255 spin_lock_irqsave(&priv
->lock
, flags
);
256 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
) {
257 spin_unlock_irqrestore(&priv
->lock
, flags
);
258 dev_dbg(&port
->dev
, "%s - write limit hit\n", __func__
);
261 priv
->outstanding_urbs
++;
262 spin_unlock_irqrestore(&priv
->lock
, flags
);
264 buffer
= kmalloc(count
, GFP_ATOMIC
);
266 dev_err(&port
->dev
, "out of memory\n");
269 goto error_no_buffer
;
272 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
274 dev_err(&port
->dev
, "no more free urbs\n");
279 memcpy(buffer
, buf
, count
);
281 usb_serial_debug_data(&port
->dev
, __func__
, count
, buffer
);
283 /* The conncected devices do not have a bulk write endpoint,
284 * to transmit data to de barcode device the control endpoint is used */
285 dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_NOIO
);
287 dev_err(&port
->dev
, "out of memory\n");
292 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
| USB_DIR_OUT
;
296 dr
->wLength
= cpu_to_le16(count
);
298 usb_fill_control_urb(urb
, serial
->dev
,
299 usb_sndctrlpipe(serial
->dev
, 0),
300 (unsigned char *)dr
, buffer
, count
,
301 opticon_write_control_callback
, priv
);
303 /* send it down the pipe */
304 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
307 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
313 /* we are done with this urb, so let the host driver
314 * really free it when it is finished with it */
325 spin_lock_irqsave(&priv
->lock
, flags
);
326 --priv
->outstanding_urbs
;
327 spin_unlock_irqrestore(&priv
->lock
, flags
);
331 static int opticon_write_room(struct tty_struct
*tty
)
333 struct usb_serial_port
*port
= tty
->driver_data
;
334 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
338 * We really can take almost anything the user throws at us
339 * but let's pick a nice big number to tell the tty
340 * layer that we have lots of free space, unless we don't.
342 spin_lock_irqsave(&priv
->lock
, flags
);
343 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
* 2 / 3) {
344 spin_unlock_irqrestore(&priv
->lock
, flags
);
345 dev_dbg(&port
->dev
, "%s - write limit hit\n", __func__
);
348 spin_unlock_irqrestore(&priv
->lock
, flags
);
353 static void opticon_throttle(struct tty_struct
*tty
)
355 struct usb_serial_port
*port
= tty
->driver_data
;
356 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
359 spin_lock_irqsave(&priv
->lock
, flags
);
360 priv
->throttled
= true;
361 spin_unlock_irqrestore(&priv
->lock
, flags
);
365 static void opticon_unthrottle(struct tty_struct
*tty
)
367 struct usb_serial_port
*port
= tty
->driver_data
;
368 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
370 int result
, was_throttled
;
372 spin_lock_irqsave(&priv
->lock
, flags
);
373 priv
->throttled
= false;
374 was_throttled
= priv
->actually_throttled
;
375 priv
->actually_throttled
= false;
376 spin_unlock_irqrestore(&priv
->lock
, flags
);
379 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
382 "%s - failed submitting read urb, error %d\n",
387 static int opticon_tiocmget(struct tty_struct
*tty
)
389 struct usb_serial_port
*port
= tty
->driver_data
;
390 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
394 spin_lock_irqsave(&priv
->lock
, flags
);
399 spin_unlock_irqrestore(&priv
->lock
, flags
);
401 dev_dbg(&port
->dev
, "%s - %x\n", __func__
, result
);
405 static int opticon_tiocmset(struct tty_struct
*tty
,
406 unsigned int set
, unsigned int clear
)
408 struct usb_serial_port
*port
= tty
->driver_data
;
409 struct usb_serial
*serial
= port
->serial
;
410 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
413 bool changed
= false;
416 /* We only support RTS so we only handle that */
417 spin_lock_irqsave(&priv
->lock
, flags
);
422 if (clear
& TIOCM_RTS
)
424 changed
= rts
^ priv
->rts
;
425 spin_unlock_irqrestore(&priv
->lock
, flags
);
430 /* Send the new RTS state to the connected device */
431 mutex_lock(&serial
->disc_mutex
);
432 if (!serial
->disconnected
)
433 ret
= send_control_msg(port
, CONTROL_RTS
, !rts
);
436 mutex_unlock(&serial
->disc_mutex
);
441 static int get_serial_info(struct opticon_private
*priv
,
442 struct serial_struct __user
*serial
)
444 struct serial_struct tmp
;
449 memset(&tmp
, 0x00, sizeof(tmp
));
451 /* fake emulate a 16550 uart to make userspace code happy */
452 tmp
.type
= PORT_16550A
;
453 tmp
.line
= priv
->serial
->minor
;
456 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
457 tmp
.xmit_fifo_size
= 1024;
458 tmp
.baud_base
= 9600;
459 tmp
.close_delay
= 5*HZ
;
460 tmp
.closing_wait
= 30*HZ
;
462 if (copy_to_user(serial
, &tmp
, sizeof(*serial
)))
467 static int opticon_ioctl(struct tty_struct
*tty
,
468 unsigned int cmd
, unsigned long arg
)
470 struct usb_serial_port
*port
= tty
->driver_data
;
471 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
473 dev_dbg(&port
->dev
, "%s - port %d, cmd = 0x%x\n", __func__
, port
->number
, cmd
);
477 return get_serial_info(priv
,
478 (struct serial_struct __user
*)arg
);
484 static int opticon_startup(struct usb_serial
*serial
)
486 struct opticon_private
*priv
;
487 struct usb_host_interface
*intf
;
489 int retval
= -ENOMEM
;
490 bool bulk_in_found
= false;
492 /* create our private serial structure */
493 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
495 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
498 spin_lock_init(&priv
->lock
);
499 priv
->serial
= serial
;
500 priv
->port
= serial
->port
[0];
501 priv
->udev
= serial
->dev
;
502 priv
->outstanding_urbs
= 0; /* Init the outstanding urbs */
504 /* find our bulk endpoint */
505 intf
= serial
->interface
->altsetting
;
506 for (i
= 0; i
< intf
->desc
.bNumEndpoints
; ++i
) {
507 struct usb_endpoint_descriptor
*endpoint
;
509 endpoint
= &intf
->endpoint
[i
].desc
;
510 if (!usb_endpoint_is_bulk_in(endpoint
))
513 priv
->bulk_read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
514 if (!priv
->bulk_read_urb
) {
515 dev_err(&priv
->udev
->dev
, "out of memory\n");
519 priv
->buffer_size
= usb_endpoint_maxp(endpoint
) * 2;
520 priv
->bulk_in_buffer
= kmalloc(priv
->buffer_size
, GFP_KERNEL
);
521 if (!priv
->bulk_in_buffer
) {
522 dev_err(&priv
->udev
->dev
, "out of memory\n");
526 priv
->bulk_address
= endpoint
->bEndpointAddress
;
528 bulk_in_found
= true;
532 if (!bulk_in_found
) {
533 dev_err(&priv
->udev
->dev
,
534 "Error - the proper endpoints were not found!\n");
538 usb_set_serial_data(serial
, priv
);
542 usb_free_urb(priv
->bulk_read_urb
);
543 kfree(priv
->bulk_in_buffer
);
548 static void opticon_disconnect(struct usb_serial
*serial
)
550 struct opticon_private
*priv
= usb_get_serial_data(serial
);
552 usb_kill_urb(priv
->bulk_read_urb
);
553 usb_free_urb(priv
->bulk_read_urb
);
556 static void opticon_release(struct usb_serial
*serial
)
558 struct opticon_private
*priv
= usb_get_serial_data(serial
);
560 kfree(priv
->bulk_in_buffer
);
564 static int opticon_suspend(struct usb_serial
*serial
, pm_message_t message
)
566 struct opticon_private
*priv
= usb_get_serial_data(serial
);
568 usb_kill_urb(priv
->bulk_read_urb
);
572 static int opticon_resume(struct usb_serial
*serial
)
574 struct opticon_private
*priv
= usb_get_serial_data(serial
);
575 struct usb_serial_port
*port
= serial
->port
[0];
578 mutex_lock(&port
->port
.mutex
);
579 /* This is protected by the port mutex against close/open */
580 if (test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
581 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_NOIO
);
584 mutex_unlock(&port
->port
.mutex
);
588 static struct usb_serial_driver opticon_device
= {
590 .owner
= THIS_MODULE
,
593 .id_table
= id_table
,
595 .attach
= opticon_startup
,
596 .open
= opticon_open
,
597 .close
= opticon_close
,
598 .write
= opticon_write
,
599 .write_room
= opticon_write_room
,
600 .disconnect
= opticon_disconnect
,
601 .release
= opticon_release
,
602 .throttle
= opticon_throttle
,
603 .unthrottle
= opticon_unthrottle
,
604 .ioctl
= opticon_ioctl
,
605 .tiocmget
= opticon_tiocmget
,
606 .tiocmset
= opticon_tiocmset
,
607 .suspend
= opticon_suspend
,
608 .resume
= opticon_resume
,
611 static struct usb_serial_driver
* const serial_drivers
[] = {
612 &opticon_device
, NULL
615 module_usb_serial_driver(serial_drivers
, id_table
);
617 MODULE_DESCRIPTION(DRIVER_DESC
);
618 MODULE_LICENSE("GPL");