2 * Opticon USB barcode to serial driver
4 * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
5 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
6 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (C) 2008 - 2009 Novell Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/tty.h>
17 #include <linux/tty_driver.h>
18 #include <linux/slab.h>
19 #include <linux/tty_flip.h>
20 #include <linux/serial.h>
21 #include <linux/module.h>
22 #include <linux/usb.h>
23 #include <linux/usb/serial.h>
24 #include <linux/uaccess.h>
26 #define CONTROL_RTS 0x02
27 #define RESEND_CTS_STATE 0x03
29 /* max number of write urbs in flight */
30 #define URB_UPPER_LIMIT 8
32 /* This driver works for the Opticon 1D barcode reader
33 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
34 #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)"
36 static const struct usb_device_id id_table
[] = {
37 { USB_DEVICE(0x065a, 0x0009) },
40 MODULE_DEVICE_TABLE(usb
, id_table
);
42 /* This structure holds all of the individual device information */
43 struct opticon_private
{
44 spinlock_t lock
; /* protects the following flags */
51 static void opticon_process_data_packet(struct usb_serial_port
*port
,
52 const unsigned char *buf
, size_t len
)
54 tty_insert_flip_string(&port
->port
, buf
, len
);
55 tty_flip_buffer_push(&port
->port
);
58 static void opticon_process_status_packet(struct usb_serial_port
*port
,
59 const unsigned char *buf
, size_t len
)
61 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
64 spin_lock_irqsave(&priv
->lock
, flags
);
69 spin_unlock_irqrestore(&priv
->lock
, flags
);
72 static void opticon_process_read_urb(struct urb
*urb
)
74 struct usb_serial_port
*port
= urb
->context
;
75 const unsigned char *hdr
= urb
->transfer_buffer
;
76 const unsigned char *data
= hdr
+ 2;
77 size_t data_len
= urb
->actual_length
- 2;
79 if (urb
->actual_length
<= 2) {
80 dev_dbg(&port
->dev
, "malformed packet received: %d bytes\n",
85 * Data from the device comes with a 2 byte header:
88 * This is real data to be sent to the tty layer
90 * This is a CTS level change, the third byte is the CTS
91 * value (0 for low, 1 for high).
93 if ((hdr
[0] == 0x00) && (hdr
[1] == 0x00)) {
94 opticon_process_data_packet(port
, data
, data_len
);
95 } else if ((hdr
[0] == 0x00) && (hdr
[1] == 0x01)) {
96 opticon_process_status_packet(port
, data
, data_len
);
98 dev_dbg(&port
->dev
, "unknown packet received: %02x %02x\n",
103 static int send_control_msg(struct usb_serial_port
*port
, u8 requesttype
,
106 struct usb_serial
*serial
= port
->serial
;
110 buffer
= kzalloc(1, GFP_KERNEL
);
115 /* Send the message to the vendor control endpoint
116 * of the connected device */
117 retval
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
119 USB_DIR_OUT
|USB_TYPE_VENDOR
|USB_RECIP_INTERFACE
,
129 static int opticon_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
131 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
135 spin_lock_irqsave(&priv
->lock
, flags
);
137 spin_unlock_irqrestore(&priv
->lock
, flags
);
140 send_control_msg(port
, CONTROL_RTS
, 0);
142 /* clear the halt status of the enpoint */
143 usb_clear_halt(port
->serial
->dev
, port
->read_urb
->pipe
);
145 res
= usb_serial_generic_open(tty
, port
);
149 /* Request CTS line state, sometimes during opening the current
150 * CTS state can be missed. */
151 send_control_msg(port
, RESEND_CTS_STATE
, 1);
156 static void opticon_write_control_callback(struct urb
*urb
)
158 struct usb_serial_port
*port
= urb
->context
;
159 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
160 int status
= urb
->status
;
163 /* free up the transfer buffer, as usb_free_urb() does not do this */
164 kfree(urb
->transfer_buffer
);
166 /* setup packet may be set if we're using it for writing */
167 kfree(urb
->setup_packet
);
171 "%s - non-zero urb status received: %d\n",
174 spin_lock_irqsave(&priv
->lock
, flags
);
175 --priv
->outstanding_urbs
;
176 spin_unlock_irqrestore(&priv
->lock
, flags
);
178 usb_serial_port_softint(port
);
181 static int opticon_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
182 const unsigned char *buf
, int count
)
184 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
185 struct usb_serial
*serial
= port
->serial
;
187 unsigned char *buffer
;
190 struct usb_ctrlrequest
*dr
;
192 spin_lock_irqsave(&priv
->lock
, flags
);
193 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
) {
194 spin_unlock_irqrestore(&priv
->lock
, flags
);
195 dev_dbg(&port
->dev
, "%s - write limit hit\n", __func__
);
198 priv
->outstanding_urbs
++;
199 spin_unlock_irqrestore(&priv
->lock
, flags
);
201 buffer
= kmalloc(count
, GFP_ATOMIC
);
203 dev_err(&port
->dev
, "out of memory\n");
206 goto error_no_buffer
;
209 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
211 dev_err(&port
->dev
, "no more free urbs\n");
216 memcpy(buffer
, buf
, count
);
218 usb_serial_debug_data(&port
->dev
, __func__
, count
, buffer
);
220 /* The conncected devices do not have a bulk write endpoint,
221 * to transmit data to de barcode device the control endpoint is used */
222 dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_NOIO
);
224 dev_err(&port
->dev
, "out of memory\n");
229 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
| USB_DIR_OUT
;
233 dr
->wLength
= cpu_to_le16(count
);
235 usb_fill_control_urb(urb
, serial
->dev
,
236 usb_sndctrlpipe(serial
->dev
, 0),
237 (unsigned char *)dr
, buffer
, count
,
238 opticon_write_control_callback
, port
);
240 /* send it down the pipe */
241 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
244 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
250 /* we are done with this urb, so let the host driver
251 * really free it when it is finished with it */
262 spin_lock_irqsave(&priv
->lock
, flags
);
263 --priv
->outstanding_urbs
;
264 spin_unlock_irqrestore(&priv
->lock
, flags
);
268 static int opticon_write_room(struct tty_struct
*tty
)
270 struct usb_serial_port
*port
= tty
->driver_data
;
271 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
275 * We really can take almost anything the user throws at us
276 * but let's pick a nice big number to tell the tty
277 * layer that we have lots of free space, unless we don't.
279 spin_lock_irqsave(&priv
->lock
, flags
);
280 if (priv
->outstanding_urbs
> URB_UPPER_LIMIT
* 2 / 3) {
281 spin_unlock_irqrestore(&priv
->lock
, flags
);
282 dev_dbg(&port
->dev
, "%s - write limit hit\n", __func__
);
285 spin_unlock_irqrestore(&priv
->lock
, flags
);
290 static int opticon_tiocmget(struct tty_struct
*tty
)
292 struct usb_serial_port
*port
= tty
->driver_data
;
293 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
297 spin_lock_irqsave(&priv
->lock
, flags
);
302 spin_unlock_irqrestore(&priv
->lock
, flags
);
304 dev_dbg(&port
->dev
, "%s - %x\n", __func__
, result
);
308 static int opticon_tiocmset(struct tty_struct
*tty
,
309 unsigned int set
, unsigned int clear
)
311 struct usb_serial_port
*port
= tty
->driver_data
;
312 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
315 bool changed
= false;
318 /* We only support RTS so we only handle that */
319 spin_lock_irqsave(&priv
->lock
, flags
);
324 if (clear
& TIOCM_RTS
)
326 changed
= rts
^ priv
->rts
;
327 spin_unlock_irqrestore(&priv
->lock
, flags
);
332 ret
= send_control_msg(port
, CONTROL_RTS
, !rts
);
334 return usb_translate_errors(ret
);
339 static int get_serial_info(struct usb_serial_port
*port
,
340 struct serial_struct __user
*serial
)
342 struct serial_struct tmp
;
347 memset(&tmp
, 0x00, sizeof(tmp
));
349 /* fake emulate a 16550 uart to make userspace code happy */
350 tmp
.type
= PORT_16550A
;
351 tmp
.line
= port
->minor
;
354 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
355 tmp
.xmit_fifo_size
= 1024;
356 tmp
.baud_base
= 9600;
357 tmp
.close_delay
= 5*HZ
;
358 tmp
.closing_wait
= 30*HZ
;
360 if (copy_to_user(serial
, &tmp
, sizeof(*serial
)))
365 static int opticon_ioctl(struct tty_struct
*tty
,
366 unsigned int cmd
, unsigned long arg
)
368 struct usb_serial_port
*port
= tty
->driver_data
;
370 dev_dbg(&port
->dev
, "%s - cmd = 0x%x\n", __func__
, cmd
);
374 return get_serial_info(port
,
375 (struct serial_struct __user
*)arg
);
381 static int opticon_startup(struct usb_serial
*serial
)
383 if (!serial
->num_bulk_in
) {
384 dev_err(&serial
->dev
->dev
, "no bulk in endpoint\n");
391 static int opticon_port_probe(struct usb_serial_port
*port
)
393 struct opticon_private
*priv
;
395 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
399 spin_lock_init(&priv
->lock
);
401 usb_set_serial_port_data(port
, priv
);
406 static int opticon_port_remove(struct usb_serial_port
*port
)
408 struct opticon_private
*priv
= usb_get_serial_port_data(port
);
415 static struct usb_serial_driver opticon_device
= {
417 .owner
= THIS_MODULE
,
420 .id_table
= id_table
,
423 .attach
= opticon_startup
,
424 .port_probe
= opticon_port_probe
,
425 .port_remove
= opticon_port_remove
,
426 .open
= opticon_open
,
427 .write
= opticon_write
,
428 .write_room
= opticon_write_room
,
429 .throttle
= usb_serial_generic_throttle
,
430 .unthrottle
= usb_serial_generic_unthrottle
,
431 .ioctl
= opticon_ioctl
,
432 .tiocmget
= opticon_tiocmget
,
433 .tiocmset
= opticon_tiocmset
,
434 .process_read_urb
= opticon_process_read_urb
,
437 static struct usb_serial_driver
* const serial_drivers
[] = {
438 &opticon_device
, NULL
441 module_usb_serial_driver(serial_drivers
, id_table
);
443 MODULE_DESCRIPTION(DRIVER_DESC
);
444 MODULE_LICENSE("GPL");