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,
150 struct opticon_private *priv = usb_get_serial_data(port->serial);
154 dbg("%s - port %d", __func__, port->number);
156 spin_lock_irqsave(&priv->lock, flags);
157 priv->throttled = false;
158 priv->actually_throttled = false;
160 spin_unlock_irqrestore(&priv->lock, flags);
162 /* Start reading from the device */
163 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
164 usb_rcvbulkpipe(priv->udev,
166 priv->bulk_in_buffer, priv->buffer_size,
167 opticon_bulk_callback, priv);
168 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
171 "%s - failed resubmitting read urb, error %d\n",
176 static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port,
179 struct opticon_private *priv = usb_get_serial_data(port->serial);
181 dbg("%s - port %d", __func__, port->number);
183 /* shutdown our urbs */
184 usb_kill_urb(priv->bulk_read_urb);
187 static void opticon_write_bulk_callback(struct urb *urb)
189 struct opticon_private *priv = urb->context;
190 int status = urb->status;
193 /* free up the transfer buffer, as usb_free_urb() does not do this */
194 kfree(urb->transfer_buffer);
197 dbg("%s - nonzero write bulk status received: %d",
200 spin_lock_irqsave(&priv->lock, flags);
201 --priv->outstanding_urbs;
202 spin_unlock_irqrestore(&priv->lock, flags);
204 usb_serial_port_softint(priv->port);
207 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
208 const unsigned char *buf, int count)
210 struct opticon_private *priv = usb_get_serial_data(port->serial);
211 struct usb_serial *serial = port->serial;
213 unsigned char *buffer;
217 dbg("%s - port %d", __func__, port->number);
219 spin_lock_irqsave(&priv->lock, flags);
220 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
221 spin_unlock_irqrestore(&priv->lock, flags);
222 dbg("%s - write limit hit\n", __func__);
225 priv->outstanding_urbs++;
226 spin_unlock_irqrestore(&priv->lock, flags);
228 buffer = kmalloc(count, GFP_ATOMIC);
230 dev_err(&port->dev, "out of memory\n");
232 goto error_no_buffer;
235 urb = usb_alloc_urb(0, GFP_ATOMIC);
237 dev_err(&port->dev, "no more free urbs\n");
242 memcpy(buffer, buf, count);
244 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
246 usb_fill_bulk_urb(urb, serial->dev,
247 usb_sndbulkpipe(serial->dev,
248 port->bulk_out_endpointAddress),
249 buffer, count, opticon_write_bulk_callback, priv);
251 /* send it down the pipe */
252 status = usb_submit_urb(urb, GFP_ATOMIC);
255 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
261 /* we are done with this urb, so let the host driver
262 * really free it when it is finished with it */
271 spin_lock_irqsave(&priv->lock, flags);
272 --priv->outstanding_urbs;
273 spin_unlock_irqrestore(&priv->lock, flags);
277 static int opticon_write_room(struct tty_struct *tty)
279 struct usb_serial_port *port = tty->driver_data;
280 struct opticon_private *priv = usb_get_serial_data(port->serial);
283 dbg("%s - port %d", __func__, port->number);
286 * We really can take almost anything the user throws at us
287 * but let's pick a nice big number to tell the tty
288 * layer that we have lots of free space, unless we don't.
290 spin_lock_irqsave(&priv->lock, flags);
291 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
292 spin_unlock_irqrestore(&priv->lock, flags);
293 dbg("%s - write limit hit\n", __func__);
296 spin_unlock_irqrestore(&priv->lock, flags);
301 static void opticon_throttle(struct tty_struct *tty)
303 struct usb_serial_port *port = tty->driver_data;
304 struct opticon_private *priv = usb_get_serial_data(port->serial);
307 dbg("%s - port %d", __func__, port->number);
308 spin_lock_irqsave(&priv->lock, flags);
309 priv->throttled = true;
310 spin_unlock_irqrestore(&priv->lock, flags);
314 static void opticon_unthrottle(struct tty_struct *tty)
316 struct usb_serial_port *port = tty->driver_data;
317 struct opticon_private *priv = usb_get_serial_data(port->serial);
321 dbg("%s - port %d", __func__, port->number);
323 spin_lock_irqsave(&priv->lock, flags);
324 priv->throttled = false;
325 priv->actually_throttled = false;
326 spin_unlock_irqrestore(&priv->lock, flags);
328 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",
336 static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
338 struct usb_serial_port *port = tty->driver_data;
339 struct opticon_private *priv = usb_get_serial_data(port->serial);
343 dbg("%s - port %d", __func__, port->number);
345 spin_lock_irqsave(&priv->lock, flags);
348 spin_unlock_irqrestore(&priv->lock, flags);
350 dbg("%s - %x", __func__, result);
354 static int get_serial_info(struct opticon_private *priv,
355 struct serial_struct __user *serial)
357 struct serial_struct tmp;
362 memset(&tmp, 0x00, sizeof(tmp));
364 /* fake emulate a 16550 uart to make userspace code happy */
365 tmp.type = PORT_16550A;
366 tmp.line = priv->serial->minor;
369 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
370 tmp.xmit_fifo_size = 1024;
371 tmp.baud_base = 9600;
372 tmp.close_delay = 5*HZ;
373 tmp.closing_wait = 30*HZ;
375 if (copy_to_user(serial, &tmp, sizeof(*serial)))
380 static int opticon_ioctl(struct tty_struct *tty, struct file *file,
381 unsigned int cmd, unsigned long arg)
383 struct usb_serial_port *port = tty->driver_data;
384 struct opticon_private *priv = usb_get_serial_data(port->serial);
386 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
390 return get_serial_info(priv,
391 (struct serial_struct __user *)arg);
397 static int opticon_startup(struct usb_serial *serial)
399 struct opticon_private *priv;
400 struct usb_host_interface *intf;
402 int retval = -ENOMEM;
403 bool bulk_in_found = false;
405 /* create our private serial structure */
406 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
408 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
411 spin_lock_init(&priv->lock);
412 priv->serial = serial;
413 priv->port = serial->port[0];
414 priv->udev = serial->dev;
416 /* find our bulk endpoint */
417 intf = serial->interface->altsetting;
418 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
419 struct usb_endpoint_descriptor *endpoint;
421 endpoint = &intf->endpoint[i].desc;
422 if (!usb_endpoint_is_bulk_in(endpoint))
425 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
426 if (!priv->bulk_read_urb) {
427 dev_err(&priv->udev->dev, "out of memory\n");
431 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
432 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
433 if (!priv->bulk_in_buffer) {
434 dev_err(&priv->udev->dev, "out of memory\n");
438 priv->bulk_address = endpoint->bEndpointAddress;
440 /* set up our bulk urb */
441 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
442 usb_rcvbulkpipe(priv->udev,
443 endpoint->bEndpointAddress),
444 priv->bulk_in_buffer, priv->buffer_size,
445 opticon_bulk_callback, priv);
447 bulk_in_found = true;
451 if (!bulk_in_found) {
452 dev_err(&priv->udev->dev,
453 "Error - the proper endpoints were not found!\n");
457 usb_set_serial_data(serial, priv);
461 usb_free_urb(priv->bulk_read_urb);
462 kfree(priv->bulk_in_buffer);
467 static void opticon_shutdown(struct usb_serial *serial)
469 struct opticon_private *priv = usb_get_serial_data(serial);
473 usb_kill_urb(priv->bulk_read_urb);
474 usb_free_urb(priv->bulk_read_urb);
475 kfree(priv->bulk_in_buffer);
477 usb_set_serial_data(serial, NULL);
480 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
482 struct usb_serial *serial = usb_get_intfdata(intf);
483 struct opticon_private *priv = usb_get_serial_data(serial);
485 usb_kill_urb(priv->bulk_read_urb);
489 static int opticon_resume(struct usb_interface *intf)
491 struct usb_serial *serial = usb_get_intfdata(intf);
492 struct opticon_private *priv = usb_get_serial_data(serial);
493 struct usb_serial_port *port = serial->port[0];
496 mutex_lock(&port->mutex);
497 if (port->port.count)
498 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
501 mutex_unlock(&port->mutex);
505 static struct usb_driver opticon_driver = {
507 .probe = usb_serial_probe,
508 .disconnect = usb_serial_disconnect,
509 .suspend = opticon_suspend,
510 .resume = opticon_resume,
511 .id_table = id_table,
515 static struct usb_serial_driver opticon_device = {
517 .owner = THIS_MODULE,
520 .id_table = id_table,
521 .usb_driver = &opticon_driver,
523 .attach = opticon_startup,
524 .open = opticon_open,
525 .close = opticon_close,
526 .write = opticon_write,
527 .write_room = opticon_write_room,
528 .shutdown = opticon_shutdown,
529 .throttle = opticon_throttle,
530 .unthrottle = opticon_unthrottle,
531 .ioctl = opticon_ioctl,
532 .tiocmget = opticon_tiocmget,
535 static int __init opticon_init(void)
539 retval = usb_serial_register(&opticon_device);
542 retval = usb_register(&opticon_driver);
544 usb_serial_deregister(&opticon_device);
548 static void __exit opticon_exit(void)
550 usb_deregister(&opticon_driver);
551 usb_serial_deregister(&opticon_device);
554 module_init(opticon_init);
555 module_exit(opticon_exit);
556 MODULE_LICENSE("GPL");
558 module_param(debug, bool, S_IRUGO | S_IWUSR);
559 MODULE_PARM_DESC(debug, "Debug enabled or not");