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;
80 /* this urb is terminated, clean up */
81 dbg("%s - urb shutting down with status: %d",
85 dbg("%s - nonzero urb status received: %d",
90 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
93 if (urb->actual_length > 2) {
94 data_length = urb->actual_length - 2;
97 * Data from the device comes with a 2 byte header:
100 * This is real data to be sent to the tty layer
102 * This is a CTS level change, the third byte is the CTS
103 * value (0 for low, 1 for high).
105 if ((data[0] == 0x00) && (data[1] == 0x00)) {
106 /* real data, send it to the tty layer */
107 tty = tty_port_tty_get(&port->port);
109 tty_insert_flip_string(tty, data + 2,
111 tty_flip_buffer_push(tty);
115 if ((data[0] == 0x00) && (data[1] == 0x01)) {
116 spin_lock_irqsave(&priv->lock, flags);
117 /* CTS status information package */
122 spin_unlock_irqrestore(&priv->lock, flags);
124 dev_dbg(&priv->udev->dev,
125 "Unknown data packet received from the device:"
131 dev_dbg(&priv->udev->dev,
132 "Improper amount of data received from the device, "
133 "%d bytes", urb->actual_length);
137 spin_lock(&priv->lock);
139 /* Continue trying to always read if we should */
140 if (!priv->throttled) {
141 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
142 usb_rcvbulkpipe(priv->udev,
144 priv->bulk_in_buffer, priv->buffer_size,
145 opticon_read_bulk_callback, priv);
146 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
149 "%s - failed resubmitting read urb, error %d\n",
152 priv->actually_throttled = true;
153 spin_unlock(&priv->lock);
156 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
159 struct usb_serial *serial = port->serial;
164 /* Send the message to the vendor control endpoint
165 * of the connected device */
166 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
168 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
174 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
176 struct opticon_private *priv = usb_get_serial_data(port->serial);
180 spin_lock_irqsave(&priv->lock, flags);
181 priv->throttled = false;
182 priv->actually_throttled = false;
185 spin_unlock_irqrestore(&priv->lock, flags);
188 send_control_msg(port, CONTROL_RTS, 0);
190 /* Setup the read URB and start reading from the device */
191 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
192 usb_rcvbulkpipe(priv->udev,
194 priv->bulk_in_buffer, priv->buffer_size,
195 opticon_read_bulk_callback, priv);
197 /* clear the halt status of the enpoint */
198 usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
200 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
203 "%s - failed resubmitting read urb, error %d\n",
205 /* Request CTS line state, sometimes during opening the current
206 * CTS state can be missed. */
207 send_control_msg(port, RESEND_CTS_STATE, 1);
211 static void opticon_close(struct usb_serial_port *port)
213 struct opticon_private *priv = usb_get_serial_data(port->serial);
215 /* shutdown our urbs */
216 usb_kill_urb(priv->bulk_read_urb);
219 static void opticon_write_control_callback(struct urb *urb)
221 struct opticon_private *priv = urb->context;
222 int status = urb->status;
225 /* free up the transfer buffer, as usb_free_urb() does not do this */
226 kfree(urb->transfer_buffer);
228 /* setup packet may be set if we're using it for writing */
229 kfree(urb->setup_packet);
232 dbg("%s - nonzero write bulk status received: %d",
235 spin_lock_irqsave(&priv->lock, flags);
236 --priv->outstanding_urbs;
237 spin_unlock_irqrestore(&priv->lock, flags);
239 usb_serial_port_softint(priv->port);
242 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
243 const unsigned char *buf, int count)
245 struct opticon_private *priv = usb_get_serial_data(port->serial);
246 struct usb_serial *serial = port->serial;
248 unsigned char *buffer;
251 struct usb_ctrlrequest *dr;
253 spin_lock_irqsave(&priv->lock, flags);
254 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
255 spin_unlock_irqrestore(&priv->lock, flags);
256 dbg("%s - write limit hit", __func__);
259 priv->outstanding_urbs++;
260 spin_unlock_irqrestore(&priv->lock, flags);
262 buffer = kmalloc(count, GFP_ATOMIC);
264 dev_err(&port->dev, "out of memory\n");
267 goto error_no_buffer;
270 urb = usb_alloc_urb(0, GFP_ATOMIC);
272 dev_err(&port->dev, "no more free urbs\n");
277 memcpy(buffer, buf, count);
279 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
281 /* The conncected devices do not have a bulk write endpoint,
282 * to transmit data to de barcode device the control endpoint is used */
283 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
285 dev_err(&port->dev, "out of memory\n");
290 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
294 dr->wLength = cpu_to_le16(count);
296 usb_fill_control_urb(urb, serial->dev,
297 usb_sndctrlpipe(serial->dev, 0),
298 (unsigned char *)dr, buffer, count,
299 opticon_write_control_callback, priv);
301 /* send it down the pipe */
302 status = usb_submit_urb(urb, GFP_ATOMIC);
305 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
311 /* we are done with this urb, so let the host driver
312 * really free it when it is finished with it */
321 spin_lock_irqsave(&priv->lock, flags);
322 --priv->outstanding_urbs;
323 spin_unlock_irqrestore(&priv->lock, flags);
327 static int opticon_write_room(struct tty_struct *tty)
329 struct usb_serial_port *port = tty->driver_data;
330 struct opticon_private *priv = usb_get_serial_data(port->serial);
334 * We really can take almost anything the user throws at us
335 * but let's pick a nice big number to tell the tty
336 * layer that we have lots of free space, unless we don't.
338 spin_lock_irqsave(&priv->lock, flags);
339 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
340 spin_unlock_irqrestore(&priv->lock, flags);
341 dbg("%s - write limit hit", __func__);
344 spin_unlock_irqrestore(&priv->lock, flags);
349 static void opticon_throttle(struct tty_struct *tty)
351 struct usb_serial_port *port = tty->driver_data;
352 struct opticon_private *priv = usb_get_serial_data(port->serial);
355 spin_lock_irqsave(&priv->lock, flags);
356 priv->throttled = true;
357 spin_unlock_irqrestore(&priv->lock, flags);
361 static void opticon_unthrottle(struct tty_struct *tty)
363 struct usb_serial_port *port = tty->driver_data;
364 struct opticon_private *priv = usb_get_serial_data(port->serial);
366 int result, was_throttled;
368 spin_lock_irqsave(&priv->lock, flags);
369 priv->throttled = false;
370 was_throttled = priv->actually_throttled;
371 priv->actually_throttled = false;
372 spin_unlock_irqrestore(&priv->lock, flags);
375 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
378 "%s - failed submitting read urb, error %d\n",
383 static int opticon_tiocmget(struct tty_struct *tty)
385 struct usb_serial_port *port = tty->driver_data;
386 struct opticon_private *priv = usb_get_serial_data(port->serial);
390 spin_lock_irqsave(&priv->lock, flags);
395 spin_unlock_irqrestore(&priv->lock, flags);
397 dbg("%s - %x", __func__, result);
401 static int opticon_tiocmset(struct tty_struct *tty,
402 unsigned int set, unsigned int clear)
404 struct usb_serial_port *port = tty->driver_data;
405 struct usb_serial *serial = port->serial;
406 struct opticon_private *priv = usb_get_serial_data(port->serial);
409 bool changed = false;
412 /* We only support RTS so we only handle that */
413 spin_lock_irqsave(&priv->lock, flags);
418 if (clear & TIOCM_RTS)
420 changed = rts ^ priv->rts;
421 spin_unlock_irqrestore(&priv->lock, flags);
426 /* Send the new RTS state to the connected device */
427 mutex_lock(&serial->disc_mutex);
428 if (!serial->disconnected)
429 ret = send_control_msg(port, CONTROL_RTS, !rts);
432 mutex_unlock(&serial->disc_mutex);
437 static int get_serial_info(struct opticon_private *priv,
438 struct serial_struct __user *serial)
440 struct serial_struct tmp;
445 memset(&tmp, 0x00, sizeof(tmp));
447 /* fake emulate a 16550 uart to make userspace code happy */
448 tmp.type = PORT_16550A;
449 tmp.line = priv->serial->minor;
452 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
453 tmp.xmit_fifo_size = 1024;
454 tmp.baud_base = 9600;
455 tmp.close_delay = 5*HZ;
456 tmp.closing_wait = 30*HZ;
458 if (copy_to_user(serial, &tmp, sizeof(*serial)))
463 static int opticon_ioctl(struct tty_struct *tty,
464 unsigned int cmd, unsigned long arg)
466 struct usb_serial_port *port = tty->driver_data;
467 struct opticon_private *priv = usb_get_serial_data(port->serial);
469 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
473 return get_serial_info(priv,
474 (struct serial_struct __user *)arg);
480 static int opticon_startup(struct usb_serial *serial)
482 struct opticon_private *priv;
483 struct usb_host_interface *intf;
485 int retval = -ENOMEM;
486 bool bulk_in_found = false;
488 /* create our private serial structure */
489 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
491 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
494 spin_lock_init(&priv->lock);
495 priv->serial = serial;
496 priv->port = serial->port[0];
497 priv->udev = serial->dev;
498 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
500 /* find our bulk endpoint */
501 intf = serial->interface->altsetting;
502 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
503 struct usb_endpoint_descriptor *endpoint;
505 endpoint = &intf->endpoint[i].desc;
506 if (!usb_endpoint_is_bulk_in(endpoint))
509 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
510 if (!priv->bulk_read_urb) {
511 dev_err(&priv->udev->dev, "out of memory\n");
515 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
516 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
517 if (!priv->bulk_in_buffer) {
518 dev_err(&priv->udev->dev, "out of memory\n");
522 priv->bulk_address = endpoint->bEndpointAddress;
524 bulk_in_found = true;
528 if (!bulk_in_found) {
529 dev_err(&priv->udev->dev,
530 "Error - the proper endpoints were not found!\n");
534 usb_set_serial_data(serial, priv);
538 usb_free_urb(priv->bulk_read_urb);
539 kfree(priv->bulk_in_buffer);
544 static void opticon_disconnect(struct usb_serial *serial)
546 struct opticon_private *priv = usb_get_serial_data(serial);
548 usb_kill_urb(priv->bulk_read_urb);
549 usb_free_urb(priv->bulk_read_urb);
552 static void opticon_release(struct usb_serial *serial)
554 struct opticon_private *priv = usb_get_serial_data(serial);
556 kfree(priv->bulk_in_buffer);
560 static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
562 struct opticon_private *priv = usb_get_serial_data(serial);
564 usb_kill_urb(priv->bulk_read_urb);
568 static int opticon_resume(struct usb_serial *serial)
570 struct opticon_private *priv = usb_get_serial_data(serial);
571 struct usb_serial_port *port = serial->port[0];
574 mutex_lock(&port->port.mutex);
575 /* This is protected by the port mutex against close/open */
576 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
577 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
580 mutex_unlock(&port->port.mutex);
584 static struct usb_serial_driver opticon_device = {
586 .owner = THIS_MODULE,
589 .id_table = id_table,
591 .attach = opticon_startup,
592 .open = opticon_open,
593 .close = opticon_close,
594 .write = opticon_write,
595 .write_room = opticon_write_room,
596 .disconnect = opticon_disconnect,
597 .release = opticon_release,
598 .throttle = opticon_throttle,
599 .unthrottle = opticon_unthrottle,
600 .ioctl = opticon_ioctl,
601 .tiocmget = opticon_tiocmget,
602 .tiocmset = opticon_tiocmset,
603 .suspend = opticon_suspend,
604 .resume = opticon_resume,
607 static struct usb_serial_driver * const serial_drivers[] = {
608 &opticon_device, NULL
611 module_usb_serial_driver(serial_drivers, id_table);
613 MODULE_DESCRIPTION(DRIVER_DESC);
614 MODULE_LICENSE("GPL");
616 module_param(debug, bool, S_IRUGO | S_IWUSR);
617 MODULE_PARM_DESC(debug, "Debug enabled or not");