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;
165 buffer = kzalloc(1, GFP_KERNEL);
170 /* Send the message to the vendor control endpoint
171 * of the connected device */
172 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
174 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
181 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
183 struct opticon_private *priv = usb_get_serial_data(port->serial);
187 dbg("%s - port %d", __func__, port->number);
189 spin_lock_irqsave(&priv->lock, flags);
190 priv->throttled = false;
191 priv->actually_throttled = false;
194 spin_unlock_irqrestore(&priv->lock, flags);
197 send_control_msg(port, CONTROL_RTS, 0);
199 /* Setup the read URB and start reading from the device */
200 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
201 usb_rcvbulkpipe(priv->udev,
203 priv->bulk_in_buffer, priv->buffer_size,
204 opticon_read_bulk_callback, priv);
206 /* clear the halt status of the enpoint */
207 usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
209 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
212 "%s - failed resubmitting read urb, error %d\n",
214 /* Request CTS line state, sometimes during opening the current
215 * CTS state can be missed. */
216 send_control_msg(port, RESEND_CTS_STATE, 1);
220 static void opticon_close(struct usb_serial_port *port)
222 struct opticon_private *priv = usb_get_serial_data(port->serial);
224 dbg("%s - port %d", __func__, port->number);
226 /* shutdown our urbs */
227 usb_kill_urb(priv->bulk_read_urb);
230 static void opticon_write_control_callback(struct urb *urb)
232 struct opticon_private *priv = urb->context;
233 int status = urb->status;
236 /* free up the transfer buffer, as usb_free_urb() does not do this */
237 kfree(urb->transfer_buffer);
239 /* setup packet may be set if we're using it for writing */
240 kfree(urb->setup_packet);
243 dbg("%s - nonzero write bulk status received: %d",
246 spin_lock_irqsave(&priv->lock, flags);
247 --priv->outstanding_urbs;
248 spin_unlock_irqrestore(&priv->lock, flags);
250 usb_serial_port_softint(priv->port);
253 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
254 const unsigned char *buf, int count)
256 struct opticon_private *priv = usb_get_serial_data(port->serial);
257 struct usb_serial *serial = port->serial;
259 unsigned char *buffer;
262 struct usb_ctrlrequest *dr;
264 dbg("%s - port %d", __func__, port->number);
266 spin_lock_irqsave(&priv->lock, flags);
267 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
268 spin_unlock_irqrestore(&priv->lock, flags);
269 dbg("%s - write limit hit", __func__);
272 priv->outstanding_urbs++;
273 spin_unlock_irqrestore(&priv->lock, flags);
275 buffer = kmalloc(count, GFP_ATOMIC);
277 dev_err(&port->dev, "out of memory\n");
280 goto error_no_buffer;
283 urb = usb_alloc_urb(0, GFP_ATOMIC);
285 dev_err(&port->dev, "no more free urbs\n");
290 memcpy(buffer, buf, count);
292 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
294 /* The conncected devices do not have a bulk write endpoint,
295 * to transmit data to de barcode device the control endpoint is used */
296 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
298 dev_err(&port->dev, "out of memory\n");
303 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
307 dr->wLength = cpu_to_le16(count);
309 usb_fill_control_urb(urb, serial->dev,
310 usb_sndctrlpipe(serial->dev, 0),
311 (unsigned char *)dr, buffer, count,
312 opticon_write_control_callback, priv);
314 /* send it down the pipe */
315 status = usb_submit_urb(urb, GFP_ATOMIC);
318 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
324 /* we are done with this urb, so let the host driver
325 * really free it when it is finished with it */
336 spin_lock_irqsave(&priv->lock, flags);
337 --priv->outstanding_urbs;
338 spin_unlock_irqrestore(&priv->lock, flags);
342 static int opticon_write_room(struct tty_struct *tty)
344 struct usb_serial_port *port = tty->driver_data;
345 struct opticon_private *priv = usb_get_serial_data(port->serial);
348 dbg("%s - port %d", __func__, port->number);
351 * We really can take almost anything the user throws at us
352 * but let's pick a nice big number to tell the tty
353 * layer that we have lots of free space, unless we don't.
355 spin_lock_irqsave(&priv->lock, flags);
356 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
357 spin_unlock_irqrestore(&priv->lock, flags);
358 dbg("%s - write limit hit", __func__);
361 spin_unlock_irqrestore(&priv->lock, flags);
366 static void opticon_throttle(struct tty_struct *tty)
368 struct usb_serial_port *port = tty->driver_data;
369 struct opticon_private *priv = usb_get_serial_data(port->serial);
372 dbg("%s - port %d", __func__, port->number);
373 spin_lock_irqsave(&priv->lock, flags);
374 priv->throttled = true;
375 spin_unlock_irqrestore(&priv->lock, flags);
379 static void opticon_unthrottle(struct tty_struct *tty)
381 struct usb_serial_port *port = tty->driver_data;
382 struct opticon_private *priv = usb_get_serial_data(port->serial);
384 int result, was_throttled;
386 dbg("%s - port %d", __func__, port->number);
388 spin_lock_irqsave(&priv->lock, flags);
389 priv->throttled = false;
390 was_throttled = priv->actually_throttled;
391 priv->actually_throttled = false;
392 spin_unlock_irqrestore(&priv->lock, flags);
394 priv->bulk_read_urb->dev = port->serial->dev;
396 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
399 "%s - failed submitting read urb, error %d\n",
404 static int opticon_tiocmget(struct tty_struct *tty)
406 struct usb_serial_port *port = tty->driver_data;
407 struct opticon_private *priv = usb_get_serial_data(port->serial);
411 dbg("%s - port %d", __func__, port->number);
412 if (!usb_get_intfdata(port->serial->interface))
415 spin_lock_irqsave(&priv->lock, flags);
420 spin_unlock_irqrestore(&priv->lock, flags);
422 dbg("%s - %x", __func__, result);
426 static int opticon_tiocmset(struct tty_struct *tty,
427 unsigned int set, unsigned int clear)
429 struct usb_serial_port *port = tty->driver_data;
430 struct opticon_private *priv = usb_get_serial_data(port->serial);
433 bool changed = false;
435 if (!usb_get_intfdata(port->serial->interface))
437 /* We only support RTS so we only handle that */
438 spin_lock_irqsave(&priv->lock, flags);
443 if (clear & TIOCM_RTS)
445 changed = rts ^ priv->rts;
446 spin_unlock_irqrestore(&priv->lock, flags);
451 /* Send the new RTS state to the connected device */
452 return send_control_msg(port, CONTROL_RTS, !rts);
455 static int get_serial_info(struct opticon_private *priv,
456 struct serial_struct __user *serial)
458 struct serial_struct tmp;
463 memset(&tmp, 0x00, sizeof(tmp));
465 /* fake emulate a 16550 uart to make userspace code happy */
466 tmp.type = PORT_16550A;
467 tmp.line = priv->serial->minor;
470 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
471 tmp.xmit_fifo_size = 1024;
472 tmp.baud_base = 9600;
473 tmp.close_delay = 5*HZ;
474 tmp.closing_wait = 30*HZ;
476 if (copy_to_user(serial, &tmp, sizeof(*serial)))
481 static int opticon_ioctl(struct tty_struct *tty,
482 unsigned int cmd, unsigned long arg)
484 struct usb_serial_port *port = tty->driver_data;
485 struct opticon_private *priv = usb_get_serial_data(port->serial);
487 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
491 return get_serial_info(priv,
492 (struct serial_struct __user *)arg);
498 static int opticon_startup(struct usb_serial *serial)
500 struct opticon_private *priv;
501 struct usb_host_interface *intf;
503 int retval = -ENOMEM;
504 bool bulk_in_found = false;
506 /* create our private serial structure */
507 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
509 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
512 spin_lock_init(&priv->lock);
513 priv->serial = serial;
514 priv->port = serial->port[0];
515 priv->udev = serial->dev;
516 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
518 /* find our bulk endpoint */
519 intf = serial->interface->altsetting;
520 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
521 struct usb_endpoint_descriptor *endpoint;
523 endpoint = &intf->endpoint[i].desc;
524 if (!usb_endpoint_is_bulk_in(endpoint))
527 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
528 if (!priv->bulk_read_urb) {
529 dev_err(&priv->udev->dev, "out of memory\n");
533 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
534 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
535 if (!priv->bulk_in_buffer) {
536 dev_err(&priv->udev->dev, "out of memory\n");
540 priv->bulk_address = endpoint->bEndpointAddress;
542 bulk_in_found = true;
546 if (!bulk_in_found) {
547 dev_err(&priv->udev->dev,
548 "Error - the proper endpoints were not found!\n");
552 usb_set_serial_data(serial, priv);
556 usb_free_urb(priv->bulk_read_urb);
557 kfree(priv->bulk_in_buffer);
562 static void opticon_disconnect(struct usb_serial *serial)
564 struct opticon_private *priv = usb_get_serial_data(serial);
568 usb_kill_urb(priv->bulk_read_urb);
569 usb_free_urb(priv->bulk_read_urb);
572 static void opticon_release(struct usb_serial *serial)
574 struct opticon_private *priv = usb_get_serial_data(serial);
578 kfree(priv->bulk_in_buffer);
582 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
584 struct usb_serial *serial = usb_get_intfdata(intf);
585 struct opticon_private *priv = usb_get_serial_data(serial);
587 usb_kill_urb(priv->bulk_read_urb);
591 static int opticon_resume(struct usb_interface *intf)
593 struct usb_serial *serial = usb_get_intfdata(intf);
594 struct opticon_private *priv = usb_get_serial_data(serial);
595 struct usb_serial_port *port = serial->port[0];
598 mutex_lock(&port->port.mutex);
599 /* This is protected by the port mutex against close/open */
600 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
601 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
604 mutex_unlock(&port->port.mutex);
608 static struct usb_driver opticon_driver = {
610 .probe = usb_serial_probe,
611 .disconnect = usb_serial_disconnect,
612 .suspend = opticon_suspend,
613 .resume = opticon_resume,
614 .id_table = id_table,
618 static struct usb_serial_driver opticon_device = {
620 .owner = THIS_MODULE,
623 .id_table = id_table,
624 .usb_driver = &opticon_driver,
626 .attach = opticon_startup,
627 .open = opticon_open,
628 .close = opticon_close,
629 .write = opticon_write,
630 .write_room = opticon_write_room,
631 .disconnect = opticon_disconnect,
632 .release = opticon_release,
633 .throttle = opticon_throttle,
634 .unthrottle = opticon_unthrottle,
635 .ioctl = opticon_ioctl,
636 .tiocmget = opticon_tiocmget,
637 .tiocmset = opticon_tiocmset,
640 static int __init opticon_init(void)
644 retval = usb_serial_register(&opticon_device);
647 retval = usb_register(&opticon_driver);
649 usb_serial_deregister(&opticon_device);
653 static void __exit opticon_exit(void)
655 usb_deregister(&opticon_driver);
656 usb_serial_deregister(&opticon_device);
659 module_init(opticon_init);
660 module_exit(opticon_exit);
661 MODULE_DESCRIPTION(DRIVER_DESC);
662 MODULE_LICENSE("GPL");
664 module_param(debug, bool, S_IRUGO | S_IWUSR);
665 MODULE_PARM_DESC(debug, "Debug enabled or not");