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 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;
58 int available_room = 0;
61 dbg("%s - port %d", __func__, port->number);
70 /* this urb is terminated, clean up */
71 dbg("%s - urb shutting down with status: %d",
75 dbg("%s - nonzero urb status received: %d",
80 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
83 if (urb->actual_length > 2) {
84 data_length = urb->actual_length - 2;
87 * Data from the device comes with a 2 byte header:
90 * This is real data to be sent to the tty layer
92 * This is a RTS level change, the third byte is the RTS
93 * value (0 for low, 1 for high).
95 if ((data[0] == 0x00) && (data[1] == 0x00)) {
96 /* real data, send it to the tty layer */
97 tty = tty_port_tty_get(&port->port);
99 available_room = tty_buffer_request_room(tty,
101 if (available_room) {
102 tty_insert_flip_string(tty, data,
104 tty_flip_buffer_push(tty);
109 if ((data[0] == 0x00) && (data[1] == 0x01)) {
115 dev_dbg(&priv->udev->dev,
116 "Unknown data packet received from the device:"
122 dev_dbg(&priv->udev->dev,
123 "Improper ammount of data received from the device, "
124 "%d bytes", urb->actual_length);
128 spin_lock(&priv->lock);
130 /* Continue trying to always read if we should */
131 if (!priv->throttled) {
132 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
133 usb_rcvbulkpipe(priv->udev,
135 priv->bulk_in_buffer, priv->buffer_size,
136 opticon_bulk_callback, priv);
137 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
140 "%s - failed resubmitting read urb, error %d\n",
143 priv->actually_throttled = true;
144 spin_unlock(&priv->lock);
147 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
149 struct opticon_private *priv = usb_get_serial_data(port->serial);
153 dbg("%s - port %d", __func__, port->number);
155 spin_lock_irqsave(&priv->lock, flags);
156 priv->throttled = false;
157 priv->actually_throttled = false;
159 spin_unlock_irqrestore(&priv->lock, flags);
161 /* Start reading from the device */
162 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
163 usb_rcvbulkpipe(priv->udev,
165 priv->bulk_in_buffer, priv->buffer_size,
166 opticon_bulk_callback, priv);
167 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
170 "%s - failed resubmitting read urb, error %d\n",
175 static void opticon_close(struct usb_serial_port *port)
177 struct opticon_private *priv = usb_get_serial_data(port->serial);
179 dbg("%s - port %d", __func__, port->number);
181 /* shutdown our urbs */
182 usb_kill_urb(priv->bulk_read_urb);
185 static void opticon_write_bulk_callback(struct urb *urb)
187 struct opticon_private *priv = urb->context;
188 int status = urb->status;
191 /* free up the transfer buffer, as usb_free_urb() does not do this */
192 kfree(urb->transfer_buffer);
195 dbg("%s - nonzero write bulk status received: %d",
198 spin_lock_irqsave(&priv->lock, flags);
199 --priv->outstanding_urbs;
200 spin_unlock_irqrestore(&priv->lock, flags);
202 usb_serial_port_softint(priv->port);
205 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
206 const unsigned char *buf, int count)
208 struct opticon_private *priv = usb_get_serial_data(port->serial);
209 struct usb_serial *serial = port->serial;
211 unsigned char *buffer;
215 dbg("%s - port %d", __func__, port->number);
217 spin_lock_irqsave(&priv->lock, flags);
218 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
219 spin_unlock_irqrestore(&priv->lock, flags);
220 dbg("%s - write limit hit\n", __func__);
223 priv->outstanding_urbs++;
224 spin_unlock_irqrestore(&priv->lock, flags);
226 buffer = kmalloc(count, GFP_ATOMIC);
228 dev_err(&port->dev, "out of memory\n");
230 goto error_no_buffer;
233 urb = usb_alloc_urb(0, GFP_ATOMIC);
235 dev_err(&port->dev, "no more free urbs\n");
240 memcpy(buffer, buf, count);
242 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
244 usb_fill_bulk_urb(urb, serial->dev,
245 usb_sndbulkpipe(serial->dev,
246 port->bulk_out_endpointAddress),
247 buffer, count, opticon_write_bulk_callback, priv);
249 /* send it down the pipe */
250 status = usb_submit_urb(urb, GFP_ATOMIC);
253 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
259 /* we are done with this urb, so let the host driver
260 * really free it when it is finished with it */
269 spin_lock_irqsave(&priv->lock, flags);
270 --priv->outstanding_urbs;
271 spin_unlock_irqrestore(&priv->lock, flags);
275 static int opticon_write_room(struct tty_struct *tty)
277 struct usb_serial_port *port = tty->driver_data;
278 struct opticon_private *priv = usb_get_serial_data(port->serial);
281 dbg("%s - port %d", __func__, port->number);
284 * We really can take almost anything the user throws at us
285 * but let's pick a nice big number to tell the tty
286 * layer that we have lots of free space, unless we don't.
288 spin_lock_irqsave(&priv->lock, flags);
289 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
290 spin_unlock_irqrestore(&priv->lock, flags);
291 dbg("%s - write limit hit\n", __func__);
294 spin_unlock_irqrestore(&priv->lock, flags);
299 static void opticon_throttle(struct tty_struct *tty)
301 struct usb_serial_port *port = tty->driver_data;
302 struct opticon_private *priv = usb_get_serial_data(port->serial);
305 dbg("%s - port %d", __func__, port->number);
306 spin_lock_irqsave(&priv->lock, flags);
307 priv->throttled = true;
308 spin_unlock_irqrestore(&priv->lock, flags);
312 static void opticon_unthrottle(struct tty_struct *tty)
314 struct usb_serial_port *port = tty->driver_data;
315 struct opticon_private *priv = usb_get_serial_data(port->serial);
317 int result, was_throttled;
319 dbg("%s - port %d", __func__, port->number);
321 spin_lock_irqsave(&priv->lock, flags);
322 priv->throttled = false;
323 was_throttled = priv->actually_throttled;
324 priv->actually_throttled = false;
325 spin_unlock_irqrestore(&priv->lock, flags);
327 priv->bulk_read_urb->dev = port->serial->dev;
329 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
332 "%s - failed submitting read urb, error %d\n",
337 static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
339 struct usb_serial_port *port = tty->driver_data;
340 struct opticon_private *priv = usb_get_serial_data(port->serial);
344 dbg("%s - port %d", __func__, port->number);
346 spin_lock_irqsave(&priv->lock, flags);
349 spin_unlock_irqrestore(&priv->lock, flags);
351 dbg("%s - %x", __func__, result);
355 static int get_serial_info(struct opticon_private *priv,
356 struct serial_struct __user *serial)
358 struct serial_struct tmp;
363 memset(&tmp, 0x00, sizeof(tmp));
365 /* fake emulate a 16550 uart to make userspace code happy */
366 tmp.type = PORT_16550A;
367 tmp.line = priv->serial->minor;
370 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
371 tmp.xmit_fifo_size = 1024;
372 tmp.baud_base = 9600;
373 tmp.close_delay = 5*HZ;
374 tmp.closing_wait = 30*HZ;
376 if (copy_to_user(serial, &tmp, sizeof(*serial)))
381 static int opticon_ioctl(struct tty_struct *tty, struct file *file,
382 unsigned int cmd, unsigned long arg)
384 struct usb_serial_port *port = tty->driver_data;
385 struct opticon_private *priv = usb_get_serial_data(port->serial);
387 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
391 return get_serial_info(priv,
392 (struct serial_struct __user *)arg);
398 static int opticon_startup(struct usb_serial *serial)
400 struct opticon_private *priv;
401 struct usb_host_interface *intf;
403 int retval = -ENOMEM;
404 bool bulk_in_found = false;
406 /* create our private serial structure */
407 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
409 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
412 spin_lock_init(&priv->lock);
413 priv->serial = serial;
414 priv->port = serial->port[0];
415 priv->udev = serial->dev;
417 /* find our bulk endpoint */
418 intf = serial->interface->altsetting;
419 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
420 struct usb_endpoint_descriptor *endpoint;
422 endpoint = &intf->endpoint[i].desc;
423 if (!usb_endpoint_is_bulk_in(endpoint))
426 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
427 if (!priv->bulk_read_urb) {
428 dev_err(&priv->udev->dev, "out of memory\n");
432 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
433 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
434 if (!priv->bulk_in_buffer) {
435 dev_err(&priv->udev->dev, "out of memory\n");
439 priv->bulk_address = endpoint->bEndpointAddress;
441 /* set up our bulk urb */
442 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
443 usb_rcvbulkpipe(priv->udev,
444 endpoint->bEndpointAddress),
445 priv->bulk_in_buffer, priv->buffer_size,
446 opticon_bulk_callback, priv);
448 bulk_in_found = true;
452 if (!bulk_in_found) {
453 dev_err(&priv->udev->dev,
454 "Error - the proper endpoints were not found!\n");
458 usb_set_serial_data(serial, priv);
462 usb_free_urb(priv->bulk_read_urb);
463 kfree(priv->bulk_in_buffer);
468 static void opticon_disconnect(struct usb_serial *serial)
470 struct opticon_private *priv = usb_get_serial_data(serial);
474 usb_kill_urb(priv->bulk_read_urb);
475 usb_free_urb(priv->bulk_read_urb);
478 static void opticon_release(struct usb_serial *serial)
480 struct opticon_private *priv = usb_get_serial_data(serial);
484 kfree(priv->bulk_in_buffer);
488 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
490 struct usb_serial *serial = usb_get_intfdata(intf);
491 struct opticon_private *priv = usb_get_serial_data(serial);
493 usb_kill_urb(priv->bulk_read_urb);
497 static int opticon_resume(struct usb_interface *intf)
499 struct usb_serial *serial = usb_get_intfdata(intf);
500 struct opticon_private *priv = usb_get_serial_data(serial);
501 struct usb_serial_port *port = serial->port[0];
504 mutex_lock(&port->port.mutex);
505 /* This is protected by the port mutex against close/open */
506 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
507 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
510 mutex_unlock(&port->port.mutex);
514 static struct usb_driver opticon_driver = {
516 .probe = usb_serial_probe,
517 .disconnect = usb_serial_disconnect,
518 .suspend = opticon_suspend,
519 .resume = opticon_resume,
520 .id_table = id_table,
524 static struct usb_serial_driver opticon_device = {
526 .owner = THIS_MODULE,
529 .id_table = id_table,
530 .usb_driver = &opticon_driver,
532 .attach = opticon_startup,
533 .open = opticon_open,
534 .close = opticon_close,
535 .write = opticon_write,
536 .write_room = opticon_write_room,
537 .disconnect = opticon_disconnect,
538 .release = opticon_release,
539 .throttle = opticon_throttle,
540 .unthrottle = opticon_unthrottle,
541 .ioctl = opticon_ioctl,
542 .tiocmget = opticon_tiocmget,
545 static int __init opticon_init(void)
549 retval = usb_serial_register(&opticon_device);
552 retval = usb_register(&opticon_driver);
554 usb_serial_deregister(&opticon_device);
558 static void __exit opticon_exit(void)
560 usb_deregister(&opticon_driver);
561 usb_serial_deregister(&opticon_device);
564 module_init(opticon_init);
565 module_exit(opticon_exit);
566 MODULE_LICENSE("GPL");
568 module_param(debug, bool, S_IRUGO | S_IWUSR);
569 MODULE_PARM_DESC(debug, "Debug enabled or not");