2 * Opticon USB barcode to serial driver
4 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
5 * Copyright (C) 2008 - 2009 Novell Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/tty.h>
15 #include <linux/tty_driver.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
25 static const struct usb_device_id id_table[] = {
26 { USB_DEVICE(0x065a, 0x0009) },
29 MODULE_DEVICE_TABLE(usb, id_table);
31 /* This structure holds all of the individual device information */
32 struct opticon_private {
33 struct usb_device *udev;
34 struct usb_serial *serial;
35 struct usb_serial_port *port;
36 unsigned char *bulk_in_buffer;
37 struct urb *bulk_read_urb;
40 spinlock_t lock; /* protects the following flags */
42 bool actually_throttled;
47 /* max number of write urbs in flight */
48 #define URB_UPPER_LIMIT 4
50 static void opticon_bulk_callback(struct urb *urb)
52 struct opticon_private *priv = urb->context;
53 unsigned char *data = urb->transfer_buffer;
54 struct usb_serial_port *port = priv->port;
55 int status = urb->status;
56 struct tty_struct *tty;
60 dbg("%s - port %d", __func__, port->number);
69 /* this urb is terminated, clean up */
70 dbg("%s - urb shutting down with status: %d",
74 dbg("%s - nonzero urb status received: %d",
79 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
82 if (urb->actual_length > 2) {
83 data_length = urb->actual_length - 2;
86 * Data from the device comes with a 2 byte header:
89 * This is real data to be sent to the tty layer
91 * This is a RTS level change, the third byte is the RTS
92 * value (0 for low, 1 for high).
94 if ((data[0] == 0x00) && (data[1] == 0x00)) {
95 /* real data, send it to the tty layer */
96 tty = tty_port_tty_get(&port->port);
98 tty_insert_flip_string(tty, data,
100 tty_flip_buffer_push(tty);
104 if ((data[0] == 0x00) && (data[1] == 0x01)) {
110 dev_dbg(&priv->udev->dev,
111 "Unknown data packet received from the device:"
117 dev_dbg(&priv->udev->dev,
118 "Improper amount of data received from the device, "
119 "%d bytes", urb->actual_length);
123 spin_lock(&priv->lock);
125 /* Continue trying to always read if we should */
126 if (!priv->throttled) {
127 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
128 usb_rcvbulkpipe(priv->udev,
130 priv->bulk_in_buffer, priv->buffer_size,
131 opticon_bulk_callback, priv);
132 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
135 "%s - failed resubmitting read urb, error %d\n",
138 priv->actually_throttled = true;
139 spin_unlock(&priv->lock);
142 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
144 struct opticon_private *priv = usb_get_serial_data(port->serial);
148 dbg("%s - port %d", __func__, port->number);
150 spin_lock_irqsave(&priv->lock, flags);
151 priv->throttled = false;
152 priv->actually_throttled = false;
154 spin_unlock_irqrestore(&priv->lock, flags);
156 /* Start reading from the device */
157 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
158 usb_rcvbulkpipe(priv->udev,
160 priv->bulk_in_buffer, priv->buffer_size,
161 opticon_bulk_callback, priv);
162 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
165 "%s - failed resubmitting read urb, error %d\n",
170 static void opticon_close(struct usb_serial_port *port)
172 struct opticon_private *priv = usb_get_serial_data(port->serial);
174 dbg("%s - port %d", __func__, port->number);
176 /* shutdown our urbs */
177 usb_kill_urb(priv->bulk_read_urb);
180 static void opticon_write_bulk_callback(struct urb *urb)
182 struct opticon_private *priv = urb->context;
183 int status = urb->status;
186 /* free up the transfer buffer, as usb_free_urb() does not do this */
187 kfree(urb->transfer_buffer);
190 dbg("%s - nonzero write bulk status received: %d",
193 spin_lock_irqsave(&priv->lock, flags);
194 --priv->outstanding_urbs;
195 spin_unlock_irqrestore(&priv->lock, flags);
197 usb_serial_port_softint(priv->port);
200 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
201 const unsigned char *buf, int count)
203 struct opticon_private *priv = usb_get_serial_data(port->serial);
204 struct usb_serial *serial = port->serial;
206 unsigned char *buffer;
210 dbg("%s - port %d", __func__, port->number);
212 spin_lock_irqsave(&priv->lock, flags);
213 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
214 spin_unlock_irqrestore(&priv->lock, flags);
215 dbg("%s - write limit hit", __func__);
218 priv->outstanding_urbs++;
219 spin_unlock_irqrestore(&priv->lock, flags);
221 buffer = kmalloc(count, GFP_ATOMIC);
223 dev_err(&port->dev, "out of memory\n");
225 goto error_no_buffer;
228 urb = usb_alloc_urb(0, GFP_ATOMIC);
230 dev_err(&port->dev, "no more free urbs\n");
235 memcpy(buffer, buf, count);
237 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
239 usb_fill_bulk_urb(urb, serial->dev,
240 usb_sndbulkpipe(serial->dev,
241 port->bulk_out_endpointAddress),
242 buffer, count, opticon_write_bulk_callback, priv);
244 /* send it down the pipe */
245 status = usb_submit_urb(urb, GFP_ATOMIC);
248 "%s - usb_submit_urb(write bulk) failed with 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 */
264 spin_lock_irqsave(&priv->lock, flags);
265 --priv->outstanding_urbs;
266 spin_unlock_irqrestore(&priv->lock, flags);
270 static int opticon_write_room(struct tty_struct *tty)
272 struct usb_serial_port *port = tty->driver_data;
273 struct opticon_private *priv = usb_get_serial_data(port->serial);
276 dbg("%s - port %d", __func__, port->number);
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 dbg("%s - write limit hit", __func__);
289 spin_unlock_irqrestore(&priv->lock, flags);
294 static void opticon_throttle(struct tty_struct *tty)
296 struct usb_serial_port *port = tty->driver_data;
297 struct opticon_private *priv = usb_get_serial_data(port->serial);
300 dbg("%s - port %d", __func__, port->number);
301 spin_lock_irqsave(&priv->lock, flags);
302 priv->throttled = true;
303 spin_unlock_irqrestore(&priv->lock, flags);
307 static void opticon_unthrottle(struct tty_struct *tty)
309 struct usb_serial_port *port = tty->driver_data;
310 struct opticon_private *priv = usb_get_serial_data(port->serial);
312 int result, was_throttled;
314 dbg("%s - port %d", __func__, port->number);
316 spin_lock_irqsave(&priv->lock, flags);
317 priv->throttled = false;
318 was_throttled = priv->actually_throttled;
319 priv->actually_throttled = false;
320 spin_unlock_irqrestore(&priv->lock, flags);
322 priv->bulk_read_urb->dev = port->serial->dev;
324 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
327 "%s - failed submitting read urb, error %d\n",
332 static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
334 struct usb_serial_port *port = tty->driver_data;
335 struct opticon_private *priv = usb_get_serial_data(port->serial);
339 dbg("%s - port %d", __func__, port->number);
341 spin_lock_irqsave(&priv->lock, flags);
344 spin_unlock_irqrestore(&priv->lock, flags);
346 dbg("%s - %x", __func__, result);
350 static int get_serial_info(struct opticon_private *priv,
351 struct serial_struct __user *serial)
353 struct serial_struct tmp;
358 memset(&tmp, 0x00, sizeof(tmp));
360 /* fake emulate a 16550 uart to make userspace code happy */
361 tmp.type = PORT_16550A;
362 tmp.line = priv->serial->minor;
365 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
366 tmp.xmit_fifo_size = 1024;
367 tmp.baud_base = 9600;
368 tmp.close_delay = 5*HZ;
369 tmp.closing_wait = 30*HZ;
371 if (copy_to_user(serial, &tmp, sizeof(*serial)))
376 static int opticon_ioctl(struct tty_struct *tty, struct file *file,
377 unsigned int cmd, unsigned long arg)
379 struct usb_serial_port *port = tty->driver_data;
380 struct opticon_private *priv = usb_get_serial_data(port->serial);
382 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
386 return get_serial_info(priv,
387 (struct serial_struct __user *)arg);
393 static int opticon_startup(struct usb_serial *serial)
395 struct opticon_private *priv;
396 struct usb_host_interface *intf;
398 int retval = -ENOMEM;
399 bool bulk_in_found = false;
401 /* create our private serial structure */
402 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
404 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
407 spin_lock_init(&priv->lock);
408 priv->serial = serial;
409 priv->port = serial->port[0];
410 priv->udev = serial->dev;
412 /* find our bulk endpoint */
413 intf = serial->interface->altsetting;
414 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
415 struct usb_endpoint_descriptor *endpoint;
417 endpoint = &intf->endpoint[i].desc;
418 if (!usb_endpoint_is_bulk_in(endpoint))
421 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
422 if (!priv->bulk_read_urb) {
423 dev_err(&priv->udev->dev, "out of memory\n");
427 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
428 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
429 if (!priv->bulk_in_buffer) {
430 dev_err(&priv->udev->dev, "out of memory\n");
434 priv->bulk_address = endpoint->bEndpointAddress;
436 /* set up our bulk urb */
437 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
438 usb_rcvbulkpipe(priv->udev,
439 endpoint->bEndpointAddress),
440 priv->bulk_in_buffer, priv->buffer_size,
441 opticon_bulk_callback, priv);
443 bulk_in_found = true;
447 if (!bulk_in_found) {
448 dev_err(&priv->udev->dev,
449 "Error - the proper endpoints were not found!\n");
453 usb_set_serial_data(serial, priv);
457 usb_free_urb(priv->bulk_read_urb);
458 kfree(priv->bulk_in_buffer);
463 static void opticon_disconnect(struct usb_serial *serial)
465 struct opticon_private *priv = usb_get_serial_data(serial);
469 usb_kill_urb(priv->bulk_read_urb);
470 usb_free_urb(priv->bulk_read_urb);
473 static void opticon_release(struct usb_serial *serial)
475 struct opticon_private *priv = usb_get_serial_data(serial);
479 kfree(priv->bulk_in_buffer);
483 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
485 struct usb_serial *serial = usb_get_intfdata(intf);
486 struct opticon_private *priv = usb_get_serial_data(serial);
488 usb_kill_urb(priv->bulk_read_urb);
492 static int opticon_resume(struct usb_interface *intf)
494 struct usb_serial *serial = usb_get_intfdata(intf);
495 struct opticon_private *priv = usb_get_serial_data(serial);
496 struct usb_serial_port *port = serial->port[0];
499 mutex_lock(&port->port.mutex);
500 /* This is protected by the port mutex against close/open */
501 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
502 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
505 mutex_unlock(&port->port.mutex);
509 static struct usb_driver opticon_driver = {
511 .probe = usb_serial_probe,
512 .disconnect = usb_serial_disconnect,
513 .suspend = opticon_suspend,
514 .resume = opticon_resume,
515 .id_table = id_table,
519 static struct usb_serial_driver opticon_device = {
521 .owner = THIS_MODULE,
524 .id_table = id_table,
525 .usb_driver = &opticon_driver,
527 .attach = opticon_startup,
528 .open = opticon_open,
529 .close = opticon_close,
530 .write = opticon_write,
531 .write_room = opticon_write_room,
532 .disconnect = opticon_disconnect,
533 .release = opticon_release,
534 .throttle = opticon_throttle,
535 .unthrottle = opticon_unthrottle,
536 .ioctl = opticon_ioctl,
537 .tiocmget = opticon_tiocmget,
540 static int __init opticon_init(void)
544 retval = usb_serial_register(&opticon_device);
547 retval = usb_register(&opticon_driver);
549 usb_serial_deregister(&opticon_device);
553 static void __exit opticon_exit(void)
555 usb_deregister(&opticon_driver);
556 usb_serial_deregister(&opticon_device);
559 module_init(opticon_init);
560 module_exit(opticon_exit);
561 MODULE_LICENSE("GPL");
563 module_param(debug, bool, S_IRUGO | S_IWUSR);
564 MODULE_PARM_DESC(debug, "Debug enabled or not");