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_serial_port
*port
;
44 unsigned char *bulk_in_buffer
;
45 struct urb
*bulk_read_urb
;
48 spinlock_t lock
; /* protects the following flags */
50 bool actually_throttled
;
58 static void opticon_read_bulk_callback(struct urb
*urb
)
60 struct opticon_private
*priv
= urb
->context
;
61 unsigned char *data
= urb
->transfer_buffer
;
62 struct usb_serial_port
*port
= priv
->port
;
63 int status
= urb
->status
;
64 struct tty_struct
*tty
;
76 /* this urb is terminated, clean up */
77 dev_dbg(&port
->dev
, "%s - urb shutting down with status: %d\n",
81 dev_dbg(&port
->dev
, "%s - nonzero urb status received: %d\n",
86 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
88 if (urb
->actual_length
> 2) {
89 data_length
= urb
->actual_length
- 2;
92 * Data from the device comes with a 2 byte header:
95 * This is real data to be sent to the tty layer
97 * This is a CTS level change, the third byte is the CTS
98 * value (0 for low, 1 for high).
100 if ((data
[0] == 0x00) && (data
[1] == 0x00)) {
101 /* real data, send it to the tty layer */
102 tty
= tty_port_tty_get(&port
->port
);
104 tty_insert_flip_string(tty
, data
+ 2,
106 tty_flip_buffer_push(tty
);
110 if ((data
[0] == 0x00) && (data
[1] == 0x01)) {
111 spin_lock_irqsave(&priv
->lock
, flags
);
112 /* CTS status information package */
117 spin_unlock_irqrestore(&priv
->lock
, flags
);
120 "Unknown data packet received from the device:"
127 "Improper amount of data received from the device, "
128 "%d bytes", urb
->actual_length
);
132 spin_lock(&priv
->lock
);
134 /* Continue trying to always read if we should */
135 if (!priv
->throttled
) {
136 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
139 "%s - failed resubmitting read urb, error %d\n",
142 priv
->actually_throttled
= true;
143 spin_unlock(&priv
->lock
);
146 static int send_control_msg(struct usb_serial_port
*port
, u8 requesttype
,
149 struct usb_serial
*serial
= port
->serial
;
153 buffer
= kzalloc(1, GFP_KERNEL
);
158 /* Send the message to the vendor control endpoint
159 * of the connected device */
160 retval
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
162 USB_DIR_OUT
|USB_TYPE_VENDOR
|USB_RECIP_INTERFACE
,
169 static int opticon_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
171 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
175 spin_lock_irqsave(&priv
->lock
, flags
);
176 priv
->throttled
= false;
177 priv
->actually_throttled
= false;
180 spin_unlock_irqrestore(&priv
->lock
, flags
);
183 send_control_msg(port
, CONTROL_RTS
, 0);
185 /* clear the halt status of the enpoint */
186 usb_clear_halt(port
->serial
->dev
, priv
->bulk_read_urb
->pipe
);
188 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_KERNEL
);
191 "%s - failed resubmitting read urb, error %d\n",
193 /* Request CTS line state, sometimes during opening the current
194 * CTS state can be missed. */
195 send_control_msg(port
, RESEND_CTS_STATE
, 1);
199 static void opticon_close(struct usb_serial_port
*port
)
201 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
203 /* shutdown our urbs */
204 usb_kill_urb(priv
->bulk_read_urb
);
207 static void opticon_write_control_callback(struct urb
*urb
)
209 struct opticon_private
*priv
= urb
->context
;
210 int status
= urb
->status
;
213 /* free up the transfer buffer, as usb_free_urb() does not do this */
214 kfree(urb
->transfer_buffer
);
216 /* setup packet may be set if we're using it for writing */
217 kfree(urb
->setup_packet
);
220 dev_dbg(&priv
->port
->dev
,
221 "%s - non-zero urb status received: %d\n",
224 spin_lock_irqsave(&priv
->lock
, flags
);
225 --priv
->outstanding_urbs
;
226 spin_unlock_irqrestore(&priv
->lock
, flags
);
228 usb_serial_port_softint(priv
->port
);
231 static int opticon_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
232 const unsigned char *buf
, int count
)
234 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
235 struct usb_serial
*serial
= port
->serial
;
237 unsigned char *buffer
;
240 struct usb_ctrlrequest
*dr
;
242 spin_lock_irqsave(&priv
->lock
, flags
);
243 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
) {
244 spin_unlock_irqrestore(&priv
->lock
, flags
);
245 dev_dbg(&port
->dev
, "%s - write limit hit\n", __func__
);
248 priv
->outstanding_urbs
++;
249 spin_unlock_irqrestore(&priv
->lock
, flags
);
251 buffer
= kmalloc(count
, GFP_ATOMIC
);
253 dev_err(&port
->dev
, "out of memory\n");
256 goto error_no_buffer
;
259 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
261 dev_err(&port
->dev
, "no more free urbs\n");
266 memcpy(buffer
, buf
, count
);
268 usb_serial_debug_data(&port
->dev
, __func__
, count
, buffer
);
270 /* The conncected devices do not have a bulk write endpoint,
271 * to transmit data to de barcode device the control endpoint is used */
272 dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_NOIO
);
274 dev_err(&port
->dev
, "out of memory\n");
279 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
| USB_DIR_OUT
;
283 dr
->wLength
= cpu_to_le16(count
);
285 usb_fill_control_urb(urb
, serial
->dev
,
286 usb_sndctrlpipe(serial
->dev
, 0),
287 (unsigned char *)dr
, buffer
, count
,
288 opticon_write_control_callback
, priv
);
290 /* send it down the pipe */
291 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
294 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
300 /* we are done with this urb, so let the host driver
301 * really free it when it is finished with it */
312 spin_lock_irqsave(&priv
->lock
, flags
);
313 --priv
->outstanding_urbs
;
314 spin_unlock_irqrestore(&priv
->lock
, flags
);
318 static int opticon_write_room(struct tty_struct
*tty
)
320 struct usb_serial_port
*port
= tty
->driver_data
;
321 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
325 * We really can take almost anything the user throws at us
326 * but let's pick a nice big number to tell the tty
327 * layer that we have lots of free space, unless we don't.
329 spin_lock_irqsave(&priv
->lock
, flags
);
330 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
* 2 / 3) {
331 spin_unlock_irqrestore(&priv
->lock
, flags
);
332 dev_dbg(&port
->dev
, "%s - write limit hit\n", __func__
);
335 spin_unlock_irqrestore(&priv
->lock
, flags
);
340 static void opticon_throttle(struct tty_struct
*tty
)
342 struct usb_serial_port
*port
= tty
->driver_data
;
343 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
346 spin_lock_irqsave(&priv
->lock
, flags
);
347 priv
->throttled
= true;
348 spin_unlock_irqrestore(&priv
->lock
, flags
);
352 static void opticon_unthrottle(struct tty_struct
*tty
)
354 struct usb_serial_port
*port
= tty
->driver_data
;
355 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
357 int result
, was_throttled
;
359 spin_lock_irqsave(&priv
->lock
, flags
);
360 priv
->throttled
= false;
361 was_throttled
= priv
->actually_throttled
;
362 priv
->actually_throttled
= false;
363 spin_unlock_irqrestore(&priv
->lock
, flags
);
366 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_ATOMIC
);
369 "%s - failed submitting read urb, error %d\n",
374 static int opticon_tiocmget(struct tty_struct
*tty
)
376 struct usb_serial_port
*port
= tty
->driver_data
;
377 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
381 spin_lock_irqsave(&priv
->lock
, flags
);
386 spin_unlock_irqrestore(&priv
->lock
, flags
);
388 dev_dbg(&port
->dev
, "%s - %x\n", __func__
, result
);
392 static int opticon_tiocmset(struct tty_struct
*tty
,
393 unsigned int set
, unsigned int clear
)
395 struct usb_serial_port
*port
= tty
->driver_data
;
396 struct usb_serial
*serial
= port
->serial
;
397 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
400 bool changed
= false;
403 /* We only support RTS so we only handle that */
404 spin_lock_irqsave(&priv
->lock
, flags
);
409 if (clear
& TIOCM_RTS
)
411 changed
= rts
^ priv
->rts
;
412 spin_unlock_irqrestore(&priv
->lock
, flags
);
417 /* Send the new RTS state to the connected device */
418 mutex_lock(&serial
->disc_mutex
);
419 if (!serial
->disconnected
)
420 ret
= send_control_msg(port
, CONTROL_RTS
, !rts
);
423 mutex_unlock(&serial
->disc_mutex
);
428 static int get_serial_info(struct opticon_private
*priv
,
429 struct serial_struct __user
*serial
)
431 struct serial_struct tmp
;
436 memset(&tmp
, 0x00, sizeof(tmp
));
438 /* fake emulate a 16550 uart to make userspace code happy */
439 tmp
.type
= PORT_16550A
;
440 tmp
.line
= priv
->port
->serial
->minor
;
443 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
444 tmp
.xmit_fifo_size
= 1024;
445 tmp
.baud_base
= 9600;
446 tmp
.close_delay
= 5*HZ
;
447 tmp
.closing_wait
= 30*HZ
;
449 if (copy_to_user(serial
, &tmp
, sizeof(*serial
)))
454 static int opticon_ioctl(struct tty_struct
*tty
,
455 unsigned int cmd
, unsigned long arg
)
457 struct usb_serial_port
*port
= tty
->driver_data
;
458 struct opticon_private
*priv
= usb_get_serial_data(port
->serial
);
460 dev_dbg(&port
->dev
, "%s - port %d, cmd = 0x%x\n", __func__
, port
->number
, cmd
);
464 return get_serial_info(priv
,
465 (struct serial_struct __user
*)arg
);
471 static int opticon_startup(struct usb_serial
*serial
)
473 struct opticon_private
*priv
;
474 int retval
= -ENOMEM
;
476 if (!serial
->num_bulk_in
) {
477 dev_err(&serial
->dev
->dev
, "no bulk in endpoint\n");
481 /* create our private serial structure */
482 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
484 dev_err(&serial
->dev
->dev
, "%s - Out of memory\n", __func__
);
487 spin_lock_init(&priv
->lock
);
488 priv
->port
= serial
->port
[0];
490 priv
->bulk_read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
491 if (!priv
->bulk_read_urb
) {
492 dev_err(&serial
->dev
->dev
, "out of memory\n");
496 priv
->buffer_size
= 2 * priv
->port
->bulk_in_size
;
497 priv
->bulk_in_buffer
= kmalloc(priv
->buffer_size
, GFP_KERNEL
);
498 if (!priv
->bulk_in_buffer
) {
499 dev_err(&serial
->dev
->dev
, "out of memory\n");
503 priv
->bulk_address
= priv
->port
->bulk_in_endpointAddress
;
505 usb_fill_bulk_urb(priv
->bulk_read_urb
, serial
->dev
,
506 usb_rcvbulkpipe(serial
->dev
,
508 priv
->bulk_in_buffer
, priv
->buffer_size
,
509 opticon_read_bulk_callback
, priv
);
511 usb_set_serial_data(serial
, priv
);
515 usb_free_urb(priv
->bulk_read_urb
);
516 kfree(priv
->bulk_in_buffer
);
521 static void opticon_disconnect(struct usb_serial
*serial
)
523 struct opticon_private
*priv
= usb_get_serial_data(serial
);
525 usb_kill_urb(priv
->bulk_read_urb
);
528 static void opticon_release(struct usb_serial
*serial
)
530 struct opticon_private
*priv
= usb_get_serial_data(serial
);
532 usb_free_urb(priv
->bulk_read_urb
);
533 kfree(priv
->bulk_in_buffer
);
537 static int opticon_suspend(struct usb_serial
*serial
, pm_message_t message
)
539 struct opticon_private
*priv
= usb_get_serial_data(serial
);
541 usb_kill_urb(priv
->bulk_read_urb
);
545 static int opticon_resume(struct usb_serial
*serial
)
547 struct opticon_private
*priv
= usb_get_serial_data(serial
);
548 struct usb_serial_port
*port
= serial
->port
[0];
551 mutex_lock(&port
->port
.mutex
);
552 /* This is protected by the port mutex against close/open */
553 if (test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
554 result
= usb_submit_urb(priv
->bulk_read_urb
, GFP_NOIO
);
557 mutex_unlock(&port
->port
.mutex
);
561 static struct usb_serial_driver opticon_device
= {
563 .owner
= THIS_MODULE
,
566 .id_table
= id_table
,
568 .attach
= opticon_startup
,
569 .open
= opticon_open
,
570 .close
= opticon_close
,
571 .write
= opticon_write
,
572 .write_room
= opticon_write_room
,
573 .disconnect
= opticon_disconnect
,
574 .release
= opticon_release
,
575 .throttle
= opticon_throttle
,
576 .unthrottle
= opticon_unthrottle
,
577 .ioctl
= opticon_ioctl
,
578 .tiocmget
= opticon_tiocmget
,
579 .tiocmset
= opticon_tiocmset
,
580 .suspend
= opticon_suspend
,
581 .resume
= opticon_resume
,
584 static struct usb_serial_driver
* const serial_drivers
[] = {
585 &opticon_device
, NULL
588 module_usb_serial_driver(serial_drivers
, id_table
);
590 MODULE_DESCRIPTION(DRIVER_DESC
);
591 MODULE_LICENSE("GPL");