USB: ftdi_sio: new VID/PIDs for various Papouch devices
[pandora-kernel.git] / drivers / usb / serial / opticon.c
1 /*
2  * Opticon USB barcode to serial driver
3  *
4  * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
5  * Copyright (C) 2008 - 2009 Novell Inc.
6  *
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.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/tty.h>
15 #include <linux/tty_driver.h>
16 #include <linux/slab.h>
17 #include <linux/tty_flip.h>
18 #include <linux/serial.h>
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/uaccess.h>
23
24 static int debug;
25
26 static const struct usb_device_id id_table[] = {
27         { USB_DEVICE(0x065a, 0x0009) },
28         { },
29 };
30 MODULE_DEVICE_TABLE(usb, id_table);
31
32 /* This structure holds all of the individual device information */
33 struct opticon_private {
34         struct usb_device *udev;
35         struct usb_serial *serial;
36         struct usb_serial_port *port;
37         unsigned char *bulk_in_buffer;
38         struct urb *bulk_read_urb;
39         int buffer_size;
40         u8 bulk_address;
41         spinlock_t lock;        /* protects the following flags */
42         bool throttled;
43         bool actually_throttled;
44         bool rts;
45         int outstanding_urbs;
46 };
47
48 /* max number of write urbs in flight */
49 #define URB_UPPER_LIMIT 4
50
51 static void opticon_bulk_callback(struct urb *urb)
52 {
53         struct opticon_private *priv = urb->context;
54         unsigned char *data = urb->transfer_buffer;
55         struct usb_serial_port *port = priv->port;
56         int status = urb->status;
57         struct tty_struct *tty;
58         int result;
59         int data_length;
60
61         dbg("%s - port %d", __func__, port->number);
62
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;
78         }
79
80         usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
81                               data);
82
83         if (urb->actual_length > 2) {
84                 data_length = urb->actual_length - 2;
85
86                 /*
87                  * Data from the device comes with a 2 byte header:
88                  *
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).
94                  */
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                                 tty_insert_flip_string(tty, data,
100                                                                data_length);
101                                 tty_flip_buffer_push(tty);
102                                 tty_kref_put(tty);
103                         }
104                 } else {
105                         if ((data[0] == 0x00) && (data[1] == 0x01)) {
106                                 if (data[2] == 0x00)
107                                         priv->rts = false;
108                                 else
109                                         priv->rts = true;
110                         } else {
111                         dev_dbg(&priv->udev->dev,
112                                 "Unknown data packet received from the device:"
113                                 " %2x %2x\n",
114                                 data[0], data[1]);
115                         }
116                 }
117         } else {
118                 dev_dbg(&priv->udev->dev,
119                         "Improper amount of data received from the device, "
120                         "%d bytes", urb->actual_length);
121         }
122
123 exit:
124         spin_lock(&priv->lock);
125
126         /* Continue trying to always read if we should */
127         if (!priv->throttled) {
128                 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
129                                   usb_rcvbulkpipe(priv->udev,
130                                                   priv->bulk_address),
131                                   priv->bulk_in_buffer, priv->buffer_size,
132                                   opticon_bulk_callback, priv);
133                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
134                 if (result)
135                         dev_err(&port->dev,
136                             "%s - failed resubmitting read urb, error %d\n",
137                                                         __func__, result);
138         } else
139                 priv->actually_throttled = true;
140         spin_unlock(&priv->lock);
141 }
142
143 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
144 {
145         struct opticon_private *priv = usb_get_serial_data(port->serial);
146         unsigned long flags;
147         int result = 0;
148
149         dbg("%s - port %d", __func__, port->number);
150
151         spin_lock_irqsave(&priv->lock, flags);
152         priv->throttled = false;
153         priv->actually_throttled = false;
154         priv->port = port;
155         spin_unlock_irqrestore(&priv->lock, flags);
156
157         /* Start reading from the device */
158         usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
159                           usb_rcvbulkpipe(priv->udev,
160                                           priv->bulk_address),
161                           priv->bulk_in_buffer, priv->buffer_size,
162                           opticon_bulk_callback, priv);
163         result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
164         if (result)
165                 dev_err(&port->dev,
166                         "%s - failed resubmitting read urb, error %d\n",
167                         __func__, result);
168         return result;
169 }
170
171 static void opticon_close(struct usb_serial_port *port)
172 {
173         struct opticon_private *priv = usb_get_serial_data(port->serial);
174
175         dbg("%s - port %d", __func__, port->number);
176
177         /* shutdown our urbs */
178         usb_kill_urb(priv->bulk_read_urb);
179 }
180
181 static void opticon_write_bulk_callback(struct urb *urb)
182 {
183         struct opticon_private *priv = urb->context;
184         int status = urb->status;
185         unsigned long flags;
186
187         /* free up the transfer buffer, as usb_free_urb() does not do this */
188         kfree(urb->transfer_buffer);
189
190         if (status)
191                 dbg("%s - nonzero write bulk status received: %d",
192                     __func__, status);
193
194         spin_lock_irqsave(&priv->lock, flags);
195         --priv->outstanding_urbs;
196         spin_unlock_irqrestore(&priv->lock, flags);
197
198         usb_serial_port_softint(priv->port);
199 }
200
201 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
202                          const unsigned char *buf, int count)
203 {
204         struct opticon_private *priv = usb_get_serial_data(port->serial);
205         struct usb_serial *serial = port->serial;
206         struct urb *urb;
207         unsigned char *buffer;
208         unsigned long flags;
209         int status;
210
211         dbg("%s - port %d", __func__, port->number);
212
213         spin_lock_irqsave(&priv->lock, flags);
214         if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
215                 spin_unlock_irqrestore(&priv->lock, flags);
216                 dbg("%s - write limit hit", __func__);
217                 return 0;
218         }
219         priv->outstanding_urbs++;
220         spin_unlock_irqrestore(&priv->lock, flags);
221
222         buffer = kmalloc(count, GFP_ATOMIC);
223         if (!buffer) {
224                 dev_err(&port->dev, "out of memory\n");
225                 count = -ENOMEM;
226                 goto error_no_buffer;
227         }
228
229         urb = usb_alloc_urb(0, GFP_ATOMIC);
230         if (!urb) {
231                 dev_err(&port->dev, "no more free urbs\n");
232                 count = -ENOMEM;
233                 goto error_no_urb;
234         }
235
236         memcpy(buffer, buf, count);
237
238         usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
239
240         usb_fill_bulk_urb(urb, serial->dev,
241                           usb_sndbulkpipe(serial->dev,
242                                           port->bulk_out_endpointAddress),
243                           buffer, count, opticon_write_bulk_callback, priv);
244
245         /* send it down the pipe */
246         status = usb_submit_urb(urb, GFP_ATOMIC);
247         if (status) {
248                 dev_err(&port->dev,
249                    "%s - usb_submit_urb(write bulk) failed with status = %d\n",
250                                                         __func__, status);
251                 count = status;
252                 goto error;
253         }
254
255         /* we are done with this urb, so let the host driver
256          * really free it when it is finished with it */
257         usb_free_urb(urb);
258
259         return count;
260 error:
261         usb_free_urb(urb);
262 error_no_urb:
263         kfree(buffer);
264 error_no_buffer:
265         spin_lock_irqsave(&priv->lock, flags);
266         --priv->outstanding_urbs;
267         spin_unlock_irqrestore(&priv->lock, flags);
268         return count;
269 }
270
271 static int opticon_write_room(struct tty_struct *tty)
272 {
273         struct usb_serial_port *port = tty->driver_data;
274         struct opticon_private *priv = usb_get_serial_data(port->serial);
275         unsigned long flags;
276
277         dbg("%s - port %d", __func__, port->number);
278
279         /*
280          * We really can take almost anything the user throws at us
281          * but let's pick a nice big number to tell the tty
282          * layer that we have lots of free space, unless we don't.
283          */
284         spin_lock_irqsave(&priv->lock, flags);
285         if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
286                 spin_unlock_irqrestore(&priv->lock, flags);
287                 dbg("%s - write limit hit", __func__);
288                 return 0;
289         }
290         spin_unlock_irqrestore(&priv->lock, flags);
291
292         return 2048;
293 }
294
295 static void opticon_throttle(struct tty_struct *tty)
296 {
297         struct usb_serial_port *port = tty->driver_data;
298         struct opticon_private *priv = usb_get_serial_data(port->serial);
299         unsigned long flags;
300
301         dbg("%s - port %d", __func__, port->number);
302         spin_lock_irqsave(&priv->lock, flags);
303         priv->throttled = true;
304         spin_unlock_irqrestore(&priv->lock, flags);
305 }
306
307
308 static void opticon_unthrottle(struct tty_struct *tty)
309 {
310         struct usb_serial_port *port = tty->driver_data;
311         struct opticon_private *priv = usb_get_serial_data(port->serial);
312         unsigned long flags;
313         int result, was_throttled;
314
315         dbg("%s - port %d", __func__, port->number);
316
317         spin_lock_irqsave(&priv->lock, flags);
318         priv->throttled = false;
319         was_throttled = priv->actually_throttled;
320         priv->actually_throttled = false;
321         spin_unlock_irqrestore(&priv->lock, flags);
322
323         priv->bulk_read_urb->dev = port->serial->dev;
324         if (was_throttled) {
325                 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
326                 if (result)
327                         dev_err(&port->dev,
328                                 "%s - failed submitting read urb, error %d\n",
329                                                         __func__, result);
330         }
331 }
332
333 static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
334 {
335         struct usb_serial_port *port = tty->driver_data;
336         struct opticon_private *priv = usb_get_serial_data(port->serial);
337         unsigned long flags;
338         int result = 0;
339
340         dbg("%s - port %d", __func__, port->number);
341
342         spin_lock_irqsave(&priv->lock, flags);
343         if (priv->rts)
344                 result = TIOCM_RTS;
345         spin_unlock_irqrestore(&priv->lock, flags);
346
347         dbg("%s - %x", __func__, result);
348         return result;
349 }
350
351 static int get_serial_info(struct opticon_private *priv,
352                            struct serial_struct __user *serial)
353 {
354         struct serial_struct tmp;
355
356         if (!serial)
357                 return -EFAULT;
358
359         memset(&tmp, 0x00, sizeof(tmp));
360
361         /* fake emulate a 16550 uart to make userspace code happy */
362         tmp.type                = PORT_16550A;
363         tmp.line                = priv->serial->minor;
364         tmp.port                = 0;
365         tmp.irq                 = 0;
366         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
367         tmp.xmit_fifo_size      = 1024;
368         tmp.baud_base           = 9600;
369         tmp.close_delay         = 5*HZ;
370         tmp.closing_wait        = 30*HZ;
371
372         if (copy_to_user(serial, &tmp, sizeof(*serial)))
373                 return -EFAULT;
374         return 0;
375 }
376
377 static int opticon_ioctl(struct tty_struct *tty, struct file *file,
378                          unsigned int cmd, unsigned long arg)
379 {
380         struct usb_serial_port *port = tty->driver_data;
381         struct opticon_private *priv = usb_get_serial_data(port->serial);
382
383         dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
384
385         switch (cmd) {
386         case TIOCGSERIAL:
387                 return get_serial_info(priv,
388                                        (struct serial_struct __user *)arg);
389         }
390
391         return -ENOIOCTLCMD;
392 }
393
394 static int opticon_startup(struct usb_serial *serial)
395 {
396         struct opticon_private *priv;
397         struct usb_host_interface *intf;
398         int i;
399         int retval = -ENOMEM;
400         bool bulk_in_found = false;
401
402         /* create our private serial structure */
403         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
404         if (priv == NULL) {
405                 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
406                 return -ENOMEM;
407         }
408         spin_lock_init(&priv->lock);
409         priv->serial = serial;
410         priv->port = serial->port[0];
411         priv->udev = serial->dev;
412
413         /* find our bulk endpoint */
414         intf = serial->interface->altsetting;
415         for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
416                 struct usb_endpoint_descriptor *endpoint;
417
418                 endpoint = &intf->endpoint[i].desc;
419                 if (!usb_endpoint_is_bulk_in(endpoint))
420                         continue;
421
422                 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
423                 if (!priv->bulk_read_urb) {
424                         dev_err(&priv->udev->dev, "out of memory\n");
425                         goto error;
426                 }
427
428                 priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
429                 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
430                 if (!priv->bulk_in_buffer) {
431                         dev_err(&priv->udev->dev, "out of memory\n");
432                         goto error;
433                 }
434
435                 priv->bulk_address = endpoint->bEndpointAddress;
436
437                 /* set up our bulk urb */
438                 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
439                                   usb_rcvbulkpipe(priv->udev,
440                                                   endpoint->bEndpointAddress),
441                                   priv->bulk_in_buffer, priv->buffer_size,
442                                   opticon_bulk_callback, priv);
443
444                 bulk_in_found = true;
445                 break;
446                 }
447
448         if (!bulk_in_found) {
449                 dev_err(&priv->udev->dev,
450                         "Error - the proper endpoints were not found!\n");
451                 goto error;
452         }
453
454         usb_set_serial_data(serial, priv);
455         return 0;
456
457 error:
458         usb_free_urb(priv->bulk_read_urb);
459         kfree(priv->bulk_in_buffer);
460         kfree(priv);
461         return retval;
462 }
463
464 static void opticon_disconnect(struct usb_serial *serial)
465 {
466         struct opticon_private *priv = usb_get_serial_data(serial);
467
468         dbg("%s", __func__);
469
470         usb_kill_urb(priv->bulk_read_urb);
471         usb_free_urb(priv->bulk_read_urb);
472 }
473
474 static void opticon_release(struct usb_serial *serial)
475 {
476         struct opticon_private *priv = usb_get_serial_data(serial);
477
478         dbg("%s", __func__);
479
480         kfree(priv->bulk_in_buffer);
481         kfree(priv);
482 }
483
484 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
485 {
486         struct usb_serial *serial = usb_get_intfdata(intf);
487         struct opticon_private *priv = usb_get_serial_data(serial);
488
489         usb_kill_urb(priv->bulk_read_urb);
490         return 0;
491 }
492
493 static int opticon_resume(struct usb_interface *intf)
494 {
495         struct usb_serial *serial = usb_get_intfdata(intf);
496         struct opticon_private *priv = usb_get_serial_data(serial);
497         struct usb_serial_port *port = serial->port[0];
498         int result;
499
500         mutex_lock(&port->port.mutex);
501         /* This is protected by the port mutex against close/open */
502         if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
503                 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
504         else
505                 result = 0;
506         mutex_unlock(&port->port.mutex);
507         return result;
508 }
509
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,
518 };
519
520 static struct usb_serial_driver opticon_device = {
521         .driver = {
522                 .owner =        THIS_MODULE,
523                 .name =         "opticon",
524         },
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,
539 };
540
541 static int __init opticon_init(void)
542 {
543         int retval;
544
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;
552 }
553
554 static void __exit opticon_exit(void)
555 {
556         usb_deregister(&opticon_driver);
557         usb_serial_deregister(&opticon_device);
558 }
559
560 module_init(opticon_init);
561 module_exit(opticon_exit);
562 MODULE_LICENSE("GPL");
563
564 module_param(debug, bool, S_IRUGO | S_IWUSR);
565 MODULE_PARM_DESC(debug, "Debug enabled or not");