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 struct tty_struct *tty;
56 tty = tty_port_tty_get(&port->port);
60 tty_insert_flip_string(tty, buf, len);
61 tty_flip_buffer_push(tty);
65 static void opticon_process_status_packet(struct usb_serial_port *port,
66 const unsigned char *buf, size_t len)
68 struct opticon_private *priv = usb_get_serial_port_data(port);
71 spin_lock_irqsave(&priv->lock, flags);
76 spin_unlock_irqrestore(&priv->lock, flags);
79 static void opticon_process_read_urb(struct urb *urb)
81 struct usb_serial_port *port = urb->context;
82 const unsigned char *hdr = urb->transfer_buffer;
83 const unsigned char *data = hdr + 2;
84 size_t data_len = urb->actual_length - 2;
86 if (urb->actual_length <= 2) {
87 dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
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 ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
101 opticon_process_data_packet(port, data, data_len);
102 } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
103 opticon_process_status_packet(port, data, data_len);
105 dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
110 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
113 struct usb_serial *serial = port->serial;
117 buffer = kzalloc(1, GFP_KERNEL);
122 /* Send the message to the vendor control endpoint
123 * of the connected device */
124 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
126 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
133 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
135 struct opticon_private *priv = usb_get_serial_port_data(port);
139 spin_lock_irqsave(&priv->lock, flags);
141 spin_unlock_irqrestore(&priv->lock, flags);
144 send_control_msg(port, CONTROL_RTS, 0);
146 /* clear the halt status of the enpoint */
147 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
149 res = usb_serial_generic_open(tty, port);
153 /* Request CTS line state, sometimes during opening the current
154 * CTS state can be missed. */
155 send_control_msg(port, RESEND_CTS_STATE, 1);
160 static void opticon_write_control_callback(struct urb *urb)
162 struct usb_serial_port *port = urb->context;
163 struct opticon_private *priv = usb_get_serial_port_data(port);
164 int status = urb->status;
167 /* free up the transfer buffer, as usb_free_urb() does not do this */
168 kfree(urb->transfer_buffer);
170 /* setup packet may be set if we're using it for writing */
171 kfree(urb->setup_packet);
175 "%s - non-zero urb status received: %d\n",
178 spin_lock_irqsave(&priv->lock, flags);
179 --priv->outstanding_urbs;
180 spin_unlock_irqrestore(&priv->lock, flags);
182 usb_serial_port_softint(port);
185 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
186 const unsigned char *buf, int count)
188 struct opticon_private *priv = usb_get_serial_port_data(port);
189 struct usb_serial *serial = port->serial;
191 unsigned char *buffer;
194 struct usb_ctrlrequest *dr;
196 spin_lock_irqsave(&priv->lock, flags);
197 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
198 spin_unlock_irqrestore(&priv->lock, flags);
199 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
202 priv->outstanding_urbs++;
203 spin_unlock_irqrestore(&priv->lock, flags);
205 buffer = kmalloc(count, GFP_ATOMIC);
207 dev_err(&port->dev, "out of memory\n");
210 goto error_no_buffer;
213 urb = usb_alloc_urb(0, GFP_ATOMIC);
215 dev_err(&port->dev, "no more free urbs\n");
220 memcpy(buffer, buf, count);
222 usb_serial_debug_data(&port->dev, __func__, count, buffer);
224 /* The conncected devices do not have a bulk write endpoint,
225 * to transmit data to de barcode device the control endpoint is used */
226 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
228 dev_err(&port->dev, "out of memory\n");
233 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
237 dr->wLength = cpu_to_le16(count);
239 usb_fill_control_urb(urb, serial->dev,
240 usb_sndctrlpipe(serial->dev, 0),
241 (unsigned char *)dr, buffer, count,
242 opticon_write_control_callback, port);
244 /* send it down the pipe */
245 status = usb_submit_urb(urb, GFP_ATOMIC);
248 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
254 /* we are done with this urb, so let the host driver
255 * really free it when it is finished with it */
266 spin_lock_irqsave(&priv->lock, flags);
267 --priv->outstanding_urbs;
268 spin_unlock_irqrestore(&priv->lock, flags);
272 static int opticon_write_room(struct tty_struct *tty)
274 struct usb_serial_port *port = tty->driver_data;
275 struct opticon_private *priv = usb_get_serial_port_data(port);
279 * We really can take almost anything the user throws at us
280 * but let's pick a nice big number to tell the tty
281 * layer that we have lots of free space, unless we don't.
283 spin_lock_irqsave(&priv->lock, flags);
284 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
285 spin_unlock_irqrestore(&priv->lock, flags);
286 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
289 spin_unlock_irqrestore(&priv->lock, flags);
294 static int opticon_tiocmget(struct tty_struct *tty)
296 struct usb_serial_port *port = tty->driver_data;
297 struct opticon_private *priv = usb_get_serial_port_data(port);
301 spin_lock_irqsave(&priv->lock, flags);
306 spin_unlock_irqrestore(&priv->lock, flags);
308 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
312 static int opticon_tiocmset(struct tty_struct *tty,
313 unsigned int set, unsigned int clear)
315 struct usb_serial_port *port = tty->driver_data;
316 struct usb_serial *serial = port->serial;
317 struct opticon_private *priv = usb_get_serial_port_data(port);
320 bool changed = false;
323 /* We only support RTS so we only handle that */
324 spin_lock_irqsave(&priv->lock, flags);
329 if (clear & TIOCM_RTS)
331 changed = rts ^ priv->rts;
332 spin_unlock_irqrestore(&priv->lock, flags);
337 /* Send the new RTS state to the connected device */
338 mutex_lock(&serial->disc_mutex);
339 if (!serial->disconnected)
340 ret = send_control_msg(port, CONTROL_RTS, !rts);
343 mutex_unlock(&serial->disc_mutex);
348 static int get_serial_info(struct usb_serial_port *port,
349 struct serial_struct __user *serial)
351 struct serial_struct tmp;
356 memset(&tmp, 0x00, sizeof(tmp));
358 /* fake emulate a 16550 uart to make userspace code happy */
359 tmp.type = PORT_16550A;
360 tmp.line = port->serial->minor;
363 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
364 tmp.xmit_fifo_size = 1024;
365 tmp.baud_base = 9600;
366 tmp.close_delay = 5*HZ;
367 tmp.closing_wait = 30*HZ;
369 if (copy_to_user(serial, &tmp, sizeof(*serial)))
374 static int opticon_ioctl(struct tty_struct *tty,
375 unsigned int cmd, unsigned long arg)
377 struct usb_serial_port *port = tty->driver_data;
379 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
383 return get_serial_info(port,
384 (struct serial_struct __user *)arg);
390 static int opticon_startup(struct usb_serial *serial)
392 if (!serial->num_bulk_in) {
393 dev_err(&serial->dev->dev, "no bulk in endpoint\n");
400 static int opticon_port_probe(struct usb_serial_port *port)
402 struct opticon_private *priv;
404 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
408 spin_lock_init(&priv->lock);
410 usb_set_serial_port_data(port, priv);
415 static int opticon_port_remove(struct usb_serial_port *port)
417 struct opticon_private *priv = usb_get_serial_port_data(port);
424 static struct usb_serial_driver opticon_device = {
426 .owner = THIS_MODULE,
429 .id_table = id_table,
432 .attach = opticon_startup,
433 .port_probe = opticon_port_probe,
434 .port_remove = opticon_port_remove,
435 .open = opticon_open,
436 .write = opticon_write,
437 .write_room = opticon_write_room,
438 .throttle = usb_serial_generic_throttle,
439 .unthrottle = usb_serial_generic_unthrottle,
440 .ioctl = opticon_ioctl,
441 .tiocmget = opticon_tiocmget,
442 .tiocmset = opticon_tiocmset,
443 .process_read_urb = opticon_process_read_urb,
446 static struct usb_serial_driver * const serial_drivers[] = {
447 &opticon_device, NULL
450 module_usb_serial_driver(serial_drivers, id_table);
452 MODULE_DESCRIPTION(DRIVER_DESC);
453 MODULE_LICENSE("GPL");