USB: ftdi_sio: re-implement read processing
[linux-2.6/mini2440.git] / drivers / usb / serial / opticon.c
blob1085a577c5c1c9b209fee089c1c36ac156ac5bc1
1 /*
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>
23 static int debug;
25 static struct usb_device_id id_table[] = {
26 { USB_DEVICE(0x065a, 0x0009) },
27 { },
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;
38 int buffer_size;
39 u8 bulk_address;
40 spinlock_t lock; /* protects the following flags */
41 bool throttled;
42 bool actually_throttled;
43 bool rts;
44 int outstanding_urbs;
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;
57 int result;
58 int available_room = 0;
59 int data_length;
61 dbg("%s - port %d", __func__, port->number);
63 switch (status) {
64 case 0:
65 /* success */
66 break;
67 case -ECONNRESET:
68 case -ENOENT:
69 case -ESHUTDOWN:
70 /* this urb is terminated, clean up */
71 dbg("%s - urb shutting down with status: %d",
72 __func__, status);
73 return;
74 default:
75 dbg("%s - nonzero urb status received: %d",
76 __func__, status);
77 goto exit;
80 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
81 data);
83 if (urb->actual_length > 2) {
84 data_length = urb->actual_length - 2;
87 * Data from the device comes with a 2 byte header:
89 * <0x00><0x00>data...
90 * This is real data to be sent to the tty layer
91 * <0x00><0x01)level
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);
98 if (tty) {
99 available_room = tty_buffer_request_room(tty,
100 data_length);
101 if (available_room) {
102 tty_insert_flip_string(tty, data,
103 available_room);
104 tty_flip_buffer_push(tty);
106 tty_kref_put(tty);
108 } else {
109 if ((data[0] == 0x00) && (data[1] == 0x01)) {
110 if (data[2] == 0x00)
111 priv->rts = false;
112 else
113 priv->rts = true;
114 } else {
115 dev_dbg(&priv->udev->dev,
116 "Unknown data packet received from the device:"
117 " %2x %2x\n",
118 data[0], data[1]);
121 } else {
122 dev_dbg(&priv->udev->dev,
123 "Improper ammount of data received from the device, "
124 "%d bytes", urb->actual_length);
127 exit:
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,
134 priv->bulk_address),
135 priv->bulk_in_buffer, priv->buffer_size,
136 opticon_bulk_callback, priv);
137 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
138 if (result)
139 dev_err(&port->dev,
140 "%s - failed resubmitting read urb, error %d\n",
141 __func__, result);
142 } else
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);
150 unsigned long flags;
151 int result = 0;
153 dbg("%s - port %d", __func__, port->number);
155 spin_lock_irqsave(&priv->lock, flags);
156 priv->throttled = false;
157 priv->actually_throttled = false;
158 priv->port = port;
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,
164 priv->bulk_address),
165 priv->bulk_in_buffer, priv->buffer_size,
166 opticon_bulk_callback, priv);
167 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
168 if (result)
169 dev_err(&port->dev,
170 "%s - failed resubmitting read urb, error %d\n",
171 __func__, result);
172 return result;
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;
189 unsigned long flags;
191 /* free up the transfer buffer, as usb_free_urb() does not do this */
192 kfree(urb->transfer_buffer);
194 if (status)
195 dbg("%s - nonzero write bulk status received: %d",
196 __func__, status);
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;
210 struct urb *urb;
211 unsigned char *buffer;
212 unsigned long flags;
213 int status;
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__);
221 return 0;
223 priv->outstanding_urbs++;
224 spin_unlock_irqrestore(&priv->lock, flags);
226 buffer = kmalloc(count, GFP_ATOMIC);
227 if (!buffer) {
228 dev_err(&port->dev, "out of memory\n");
229 count = -ENOMEM;
230 goto error_no_buffer;
233 urb = usb_alloc_urb(0, GFP_ATOMIC);
234 if (!urb) {
235 dev_err(&port->dev, "no more free urbs\n");
236 count = -ENOMEM;
237 goto error_no_urb;
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);
251 if (status) {
252 dev_err(&port->dev,
253 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
254 __func__, status);
255 count = status;
256 goto error;
259 /* we are done with this urb, so let the host driver
260 * really free it when it is finished with it */
261 usb_free_urb(urb);
263 return count;
264 error:
265 usb_free_urb(urb);
266 error_no_urb:
267 kfree(buffer);
268 error_no_buffer:
269 spin_lock_irqsave(&priv->lock, flags);
270 --priv->outstanding_urbs;
271 spin_unlock_irqrestore(&priv->lock, flags);
272 return count;
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);
279 unsigned long flags;
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__);
292 return 0;
294 spin_unlock_irqrestore(&priv->lock, flags);
296 return 2048;
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);
303 unsigned long flags;
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);
316 unsigned long flags;
317 int result;
319 dbg("%s - port %d", __func__, port->number);
321 spin_lock_irqsave(&priv->lock, flags);
322 priv->throttled = false;
323 priv->actually_throttled = false;
324 spin_unlock_irqrestore(&priv->lock, flags);
326 priv->bulk_read_urb->dev = port->serial->dev;
327 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
328 if (result)
329 dev_err(&port->dev,
330 "%s - failed submitting read urb, error %d\n",
331 __func__, result);
334 static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
336 struct usb_serial_port *port = tty->driver_data;
337 struct opticon_private *priv = usb_get_serial_data(port->serial);
338 unsigned long flags;
339 int result = 0;
341 dbg("%s - port %d", __func__, port->number);
343 spin_lock_irqsave(&priv->lock, flags);
344 if (priv->rts)
345 result = TIOCM_RTS;
346 spin_unlock_irqrestore(&priv->lock, flags);
348 dbg("%s - %x", __func__, result);
349 return result;
352 static int get_serial_info(struct opticon_private *priv,
353 struct serial_struct __user *serial)
355 struct serial_struct tmp;
357 if (!serial)
358 return -EFAULT;
360 memset(&tmp, 0x00, sizeof(tmp));
362 /* fake emulate a 16550 uart to make userspace code happy */
363 tmp.type = PORT_16550A;
364 tmp.line = priv->serial->minor;
365 tmp.port = 0;
366 tmp.irq = 0;
367 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
368 tmp.xmit_fifo_size = 1024;
369 tmp.baud_base = 9600;
370 tmp.close_delay = 5*HZ;
371 tmp.closing_wait = 30*HZ;
373 if (copy_to_user(serial, &tmp, sizeof(*serial)))
374 return -EFAULT;
375 return 0;
378 static int opticon_ioctl(struct tty_struct *tty, struct file *file,
379 unsigned int cmd, unsigned long arg)
381 struct usb_serial_port *port = tty->driver_data;
382 struct opticon_private *priv = usb_get_serial_data(port->serial);
384 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
386 switch (cmd) {
387 case TIOCGSERIAL:
388 return get_serial_info(priv,
389 (struct serial_struct __user *)arg);
392 return -ENOIOCTLCMD;
395 static int opticon_startup(struct usb_serial *serial)
397 struct opticon_private *priv;
398 struct usb_host_interface *intf;
399 int i;
400 int retval = -ENOMEM;
401 bool bulk_in_found = false;
403 /* create our private serial structure */
404 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
405 if (priv == NULL) {
406 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
407 return -ENOMEM;
409 spin_lock_init(&priv->lock);
410 priv->serial = serial;
411 priv->port = serial->port[0];
412 priv->udev = serial->dev;
414 /* find our bulk endpoint */
415 intf = serial->interface->altsetting;
416 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
417 struct usb_endpoint_descriptor *endpoint;
419 endpoint = &intf->endpoint[i].desc;
420 if (!usb_endpoint_is_bulk_in(endpoint))
421 continue;
423 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
424 if (!priv->bulk_read_urb) {
425 dev_err(&priv->udev->dev, "out of memory\n");
426 goto error;
429 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
430 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
431 if (!priv->bulk_in_buffer) {
432 dev_err(&priv->udev->dev, "out of memory\n");
433 goto error;
436 priv->bulk_address = endpoint->bEndpointAddress;
438 /* set up our bulk urb */
439 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
440 usb_rcvbulkpipe(priv->udev,
441 endpoint->bEndpointAddress),
442 priv->bulk_in_buffer, priv->buffer_size,
443 opticon_bulk_callback, priv);
445 bulk_in_found = true;
446 break;
449 if (!bulk_in_found) {
450 dev_err(&priv->udev->dev,
451 "Error - the proper endpoints were not found!\n");
452 goto error;
455 usb_set_serial_data(serial, priv);
456 return 0;
458 error:
459 usb_free_urb(priv->bulk_read_urb);
460 kfree(priv->bulk_in_buffer);
461 kfree(priv);
462 return retval;
465 static void opticon_disconnect(struct usb_serial *serial)
467 struct opticon_private *priv = usb_get_serial_data(serial);
469 dbg("%s", __func__);
471 usb_kill_urb(priv->bulk_read_urb);
472 usb_free_urb(priv->bulk_read_urb);
475 static void opticon_release(struct usb_serial *serial)
477 struct opticon_private *priv = usb_get_serial_data(serial);
479 dbg("%s", __func__);
481 kfree(priv->bulk_in_buffer);
482 kfree(priv);
485 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
487 struct usb_serial *serial = usb_get_intfdata(intf);
488 struct opticon_private *priv = usb_get_serial_data(serial);
490 usb_kill_urb(priv->bulk_read_urb);
491 return 0;
494 static int opticon_resume(struct usb_interface *intf)
496 struct usb_serial *serial = usb_get_intfdata(intf);
497 struct opticon_private *priv = usb_get_serial_data(serial);
498 struct usb_serial_port *port = serial->port[0];
499 int result;
501 mutex_lock(&port->mutex);
502 if (port->port.count)
503 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
504 else
505 result = 0;
506 mutex_unlock(&port->mutex);
507 return result;
510 static struct usb_driver opticon_driver = {
511 .name = "opticon",
512 .probe = usb_serial_probe,
513 .disconnect = usb_serial_disconnect,
514 .suspend = opticon_suspend,
515 .resume = opticon_resume,
516 .id_table = id_table,
517 .no_dynamic_id = 1,
520 static struct usb_serial_driver opticon_device = {
521 .driver = {
522 .owner = THIS_MODULE,
523 .name = "opticon",
525 .id_table = id_table,
526 .usb_driver = &opticon_driver,
527 .num_ports = 1,
528 .attach = opticon_startup,
529 .open = opticon_open,
530 .close = opticon_close,
531 .write = opticon_write,
532 .write_room = opticon_write_room,
533 .disconnect = opticon_disconnect,
534 .release = opticon_release,
535 .throttle = opticon_throttle,
536 .unthrottle = opticon_unthrottle,
537 .ioctl = opticon_ioctl,
538 .tiocmget = opticon_tiocmget,
541 static int __init opticon_init(void)
543 int retval;
545 retval = usb_serial_register(&opticon_device);
546 if (retval)
547 return retval;
548 retval = usb_register(&opticon_driver);
549 if (retval)
550 usb_serial_deregister(&opticon_device);
551 return retval;
554 static void __exit opticon_exit(void)
556 usb_deregister(&opticon_driver);
557 usb_serial_deregister(&opticon_device);
560 module_init(opticon_init);
561 module_exit(opticon_exit);
562 MODULE_LICENSE("GPL");
564 module_param(debug, bool, S_IRUGO | S_IWUSR);
565 MODULE_PARM_DESC(debug, "Debug enabled or not");