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/module.h>
18 #include <linux/usb.h>
19 #include <linux/usb/serial.h>
20 #include <linux/uaccess.h>
24 static struct usb_device_id id_table[] = {
25 { USB_DEVICE(0x065a, 0x0009) },
28 MODULE_DEVICE_TABLE(usb, id_table);
30 /* This structure holds all of the individual device information */
31 struct opticon_private {
32 struct usb_device *udev;
33 struct usb_serial *serial;
34 struct usb_serial_port *port;
35 unsigned char *bulk_in_buffer;
36 struct urb *bulk_read_urb;
39 spinlock_t lock; /* protects the following flags */
41 bool actually_throttled;
46 /* max number of write urbs in flight */
47 #define URB_UPPER_LIMIT 4
49 static void opticon_bulk_callback(struct urb *urb)
51 struct opticon_private *priv = urb->context;
52 unsigned char *data = urb->transfer_buffer;
53 struct usb_serial_port *port = priv->port;
54 int status = urb->status;
55 struct tty_struct *tty;
57 int available_room = 0;
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 available_room = tty_buffer_request_room(tty,
100 if (available_room) {
101 tty_insert_flip_string(tty, data,
103 tty_flip_buffer_push(tty);
108 if ((data[0] == 0x00) && (data[1] == 0x01)) {
113 /* FIXME change the RTS level */
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);
163 * Force low_latency on so that our tty_push actually forces the data
164 * through, otherwise it is scheduled, and with high data rates (like
165 * with OHCI) data can get lost.
168 tty->low_latency = 1;
170 /* Start reading from the device */
171 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
172 usb_rcvbulkpipe(priv->udev,
174 priv->bulk_in_buffer, priv->buffer_size,
175 opticon_bulk_callback, priv);
176 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
179 "%s - failed resubmitting read urb, error %d\n",
184 static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port,
187 struct opticon_private *priv = usb_get_serial_data(port->serial);
189 dbg("%s - port %d", __func__, port->number);
191 /* shutdown our urbs */
192 usb_kill_urb(priv->bulk_read_urb);
195 static void opticon_write_bulk_callback(struct urb *urb)
197 struct opticon_private *priv = urb->context;
198 int status = urb->status;
201 /* free up the transfer buffer, as usb_free_urb() does not do this */
202 kfree(urb->transfer_buffer);
205 dbg("%s - nonzero write bulk status received: %d",
208 spin_lock_irqsave(&priv->lock, flags);
209 --priv->outstanding_urbs;
210 spin_unlock_irqrestore(&priv->lock, flags);
212 usb_serial_port_softint(priv->port);
215 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
216 const unsigned char *buf, int count)
218 struct opticon_private *priv = usb_get_serial_data(port->serial);
219 struct usb_serial *serial = port->serial;
221 unsigned char *buffer;
225 dbg("%s - port %d", __func__, port->number);
227 spin_lock_irqsave(&priv->lock, flags);
228 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
229 spin_unlock_irqrestore(&priv->lock, flags);
230 dbg("%s - write limit hit\n", __func__);
233 priv->outstanding_urbs++;
234 spin_unlock_irqrestore(&priv->lock, flags);
236 buffer = kmalloc(count, GFP_ATOMIC);
238 dev_err(&port->dev, "out of memory\n");
240 goto error_no_buffer;
243 urb = usb_alloc_urb(0, GFP_ATOMIC);
245 dev_err(&port->dev, "no more free urbs\n");
250 memcpy(buffer, buf, count);
252 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
254 usb_fill_bulk_urb(urb, serial->dev,
255 usb_sndbulkpipe(serial->dev,
256 port->bulk_out_endpointAddress),
257 buffer, count, opticon_write_bulk_callback, priv);
259 /* send it down the pipe */
260 status = usb_submit_urb(urb, GFP_ATOMIC);
263 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
269 /* we are done with this urb, so let the host driver
270 * really free it when it is finished with it */
279 spin_lock_irqsave(&priv->lock, flags);
280 --priv->outstanding_urbs;
281 spin_unlock_irqrestore(&priv->lock, flags);
285 static int opticon_write_room(struct tty_struct *tty)
287 struct usb_serial_port *port = tty->driver_data;
288 struct opticon_private *priv = usb_get_serial_data(port->serial);
291 dbg("%s - port %d", __func__, port->number);
294 * We really can take almost anything the user throws at us
295 * but let's pick a nice big number to tell the tty
296 * layer that we have lots of free space, unless we don't.
298 spin_lock_irqsave(&priv->lock, flags);
299 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
300 spin_unlock_irqrestore(&priv->lock, flags);
301 dbg("%s - write limit hit\n", __func__);
304 spin_unlock_irqrestore(&priv->lock, flags);
309 static void opticon_throttle(struct tty_struct *tty)
311 struct usb_serial_port *port = tty->driver_data;
312 struct opticon_private *priv = usb_get_serial_data(port->serial);
315 dbg("%s - port %d", __func__, port->number);
316 spin_lock_irqsave(&priv->lock, flags);
317 priv->throttled = true;
318 spin_unlock_irqrestore(&priv->lock, flags);
322 static void opticon_unthrottle(struct tty_struct *tty)
324 struct usb_serial_port *port = tty->driver_data;
325 struct opticon_private *priv = usb_get_serial_data(port->serial);
329 dbg("%s - port %d", __func__, port->number);
331 spin_lock_irqsave(&priv->lock, flags);
332 priv->throttled = false;
333 priv->actually_throttled = false;
334 spin_unlock_irqrestore(&priv->lock, flags);
336 priv->bulk_read_urb->dev = port->serial->dev;
337 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
340 "%s - failed submitting read urb, error %d\n",
344 static int opticon_startup(struct usb_serial *serial)
346 struct opticon_private *priv;
347 struct usb_host_interface *intf;
349 int retval = -ENOMEM;
350 bool bulk_in_found = false;
352 /* create our private serial structure */
353 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
355 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
358 spin_lock_init(&priv->lock);
359 priv->serial = serial;
360 priv->port = serial->port[0];
361 priv->udev = serial->dev;
363 /* find our bulk endpoint */
364 intf = serial->interface->altsetting;
365 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
366 struct usb_endpoint_descriptor *endpoint;
368 endpoint = &intf->endpoint[i].desc;
369 if (!usb_endpoint_is_bulk_in(endpoint))
372 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
373 if (!priv->bulk_read_urb) {
374 dev_err(&priv->udev->dev, "out of memory\n");
378 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
379 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
380 if (!priv->bulk_in_buffer) {
381 dev_err(&priv->udev->dev, "out of memory\n");
385 priv->bulk_address = endpoint->bEndpointAddress;
387 /* set up our bulk urb */
388 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
389 usb_rcvbulkpipe(priv->udev,
390 endpoint->bEndpointAddress),
391 priv->bulk_in_buffer, priv->buffer_size,
392 opticon_bulk_callback, priv);
394 bulk_in_found = true;
398 if (!bulk_in_found) {
399 dev_err(&priv->udev->dev,
400 "Error - the proper endpoints were not found!\n");
404 usb_set_serial_data(serial, priv);
408 usb_free_urb(priv->bulk_read_urb);
409 kfree(priv->bulk_in_buffer);
414 static void opticon_shutdown(struct usb_serial *serial)
416 struct opticon_private *priv = usb_get_serial_data(serial);
420 usb_kill_urb(priv->bulk_read_urb);
421 usb_free_urb(priv->bulk_read_urb);
422 kfree(priv->bulk_in_buffer);
424 usb_set_serial_data(serial, NULL);
427 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
429 struct usb_serial *serial = usb_get_intfdata(intf);
430 struct opticon_private *priv = usb_get_serial_data(serial);
432 usb_kill_urb(priv->bulk_read_urb);
436 static int opticon_resume(struct usb_interface *intf)
438 struct usb_serial *serial = usb_get_intfdata(intf);
439 struct opticon_private *priv = usb_get_serial_data(serial);
440 struct usb_serial_port *port = serial->port[0];
443 mutex_lock(&port->mutex);
444 if (port->port.count)
445 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
448 mutex_unlock(&port->mutex);
452 static struct usb_driver opticon_driver = {
454 .probe = usb_serial_probe,
455 .disconnect = usb_serial_disconnect,
456 .suspend = opticon_suspend,
457 .resume = opticon_resume,
458 .id_table = id_table,
462 static struct usb_serial_driver opticon_device = {
464 .owner = THIS_MODULE,
467 .id_table = id_table,
468 .usb_driver = &opticon_driver,
470 .attach = opticon_startup,
471 .open = opticon_open,
472 .close = opticon_close,
473 .write = opticon_write,
474 .write_room = opticon_write_room,
475 .shutdown = opticon_shutdown,
476 .throttle = opticon_throttle,
477 .unthrottle = opticon_unthrottle,
480 static int __init opticon_init(void)
484 retval = usb_serial_register(&opticon_device);
487 retval = usb_register(&opticon_driver);
489 usb_serial_deregister(&opticon_device);
493 static void __exit opticon_exit(void)
495 usb_deregister(&opticon_driver);
496 usb_serial_deregister(&opticon_device);
499 module_init(opticon_init);
500 module_exit(opticon_exit);
501 MODULE_LICENSE("GPL");
503 module_param(debug, bool, S_IRUGO | S_IWUSR);
504 MODULE_PARM_DESC(debug, "Debug enabled or not");