]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/serial/opticon.c
5b83e10b6b6313c8697ea1fb3ffe08b994a801a3
[karo-tx-linux.git] / drivers / usb / serial / opticon.c
1 /*
2  * Opticon USB barcode to serial driver
3  *
4  * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
5  * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
6  * Copyright (C) 2008 - 2009 Novell Inc.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License version
10  *      2 as published by the Free Software Foundation.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/slab.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
24
25 #define CONTROL_RTS                     0x02
26 #define RESEND_CTS_STATE        0x03
27
28 /* max number of write urbs in flight */
29 #define URB_UPPER_LIMIT 8
30
31 /* This driver works for the Opticon 1D barcode reader
32  * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
33 #define DRIVER_DESC     "Opticon USB barcode to serial driver (1D)"
34
35 static int debug;
36
37 static const struct usb_device_id id_table[] = {
38         { USB_DEVICE(0x065a, 0x0009) },
39         { },
40 };
41 MODULE_DEVICE_TABLE(usb, id_table);
42
43 /* This structure holds all of the individual device information */
44 struct opticon_private {
45         struct usb_device *udev;
46         struct usb_serial *serial;
47         struct usb_serial_port *port;
48         unsigned char *bulk_in_buffer;
49         struct urb *bulk_read_urb;
50         int buffer_size;
51         u8 bulk_address;
52         spinlock_t lock;        /* protects the following flags */
53         bool throttled;
54         bool actually_throttled;
55         bool rts;
56         bool cts;
57         int outstanding_urbs;
58 };
59
60
61
62 static void opticon_read_bulk_callback(struct urb *urb)
63 {
64         struct opticon_private *priv = urb->context;
65         unsigned char *data = urb->transfer_buffer;
66         struct usb_serial_port *port = priv->port;
67         int status = urb->status;
68         struct tty_struct *tty;
69         int result;
70         int data_length;
71         unsigned long flags;
72
73         dbg("%s - port %d", __func__, port->number);
74
75         switch (status) {
76         case 0:
77                 /* success */
78                 break;
79         case -ECONNRESET:
80         case -ENOENT:
81         case -ESHUTDOWN:
82                 /* this urb is terminated, clean up */
83                 dbg("%s - urb shutting down with status: %d",
84                     __func__, status);
85                 return;
86         default:
87                 dbg("%s - nonzero urb status received: %d",
88                     __func__, status);
89                 goto exit;
90         }
91
92         usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
93                               data);
94
95         if (urb->actual_length > 2) {
96                 data_length = urb->actual_length - 2;
97
98                 /*
99                  * Data from the device comes with a 2 byte header:
100                  *
101                  * <0x00><0x00>data...
102                  *      This is real data to be sent to the tty layer
103                  * <0x00><0x01)level
104                  *      This is a CTS level change, the third byte is the CTS
105                  *      value (0 for low, 1 for high).
106                  */
107                 if ((data[0] == 0x00) && (data[1] == 0x00)) {
108                         /* real data, send it to the tty layer */
109                         tty = tty_port_tty_get(&port->port);
110                         if (tty) {
111                                 tty_insert_flip_string(tty, data + 2,
112                                                        data_length);
113                                 tty_flip_buffer_push(tty);
114                                 tty_kref_put(tty);
115                         }
116                 } else {
117                         if ((data[0] == 0x00) && (data[1] == 0x01)) {
118                                 spin_lock_irqsave(&priv->lock, flags);
119                                 /* CTS status information package */
120                                 if (data[2] == 0x00)
121                                         priv->cts = false;
122                                 else
123                                         priv->cts = true;
124                                 spin_unlock_irqrestore(&priv->lock, flags);
125                         } else {
126                                 dev_dbg(&priv->udev->dev,
127                                         "Unknown data packet received from the device:"
128                                         " %2x %2x\n",
129                                         data[0], data[1]);
130                         }
131                 }
132         } else {
133                 dev_dbg(&priv->udev->dev,
134                         "Improper amount of data received from the device, "
135                         "%d bytes", urb->actual_length);
136         }
137
138 exit:
139         spin_lock(&priv->lock);
140
141         /* Continue trying to always read if we should */
142         if (!priv->throttled) {
143                 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
144                                   usb_rcvbulkpipe(priv->udev,
145                                                   priv->bulk_address),
146                                   priv->bulk_in_buffer, priv->buffer_size,
147                                   opticon_read_bulk_callback, priv);
148                 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
149                 if (result)
150                         dev_err(&port->dev,
151                             "%s - failed resubmitting read urb, error %d\n",
152                                                         __func__, result);
153         } else
154                 priv->actually_throttled = true;
155         spin_unlock(&priv->lock);
156 }
157
158 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
159                                 u8 val)
160 {
161         struct usb_serial *serial = port->serial;
162         int retval;
163         u8 *buffer;
164
165         buffer = kzalloc(1, GFP_KERNEL);
166         if (!buffer)
167                 return -ENOMEM;
168
169         buffer[0] = val;
170         /* Send the message to the vendor control endpoint
171          * of the connected device */
172         retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
173                                 requesttype,
174                                 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
175                                 0, 0, buffer, 1, 0);
176         kfree(buffer);
177
178         return retval;
179 }
180
181 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
182 {
183         struct opticon_private *priv = usb_get_serial_data(port->serial);
184         unsigned long flags;
185         int result = 0;
186
187         dbg("%s - port %d", __func__, port->number);
188
189         spin_lock_irqsave(&priv->lock, flags);
190         priv->throttled = false;
191         priv->actually_throttled = false;
192         priv->port = port;
193         priv->rts = false;
194         spin_unlock_irqrestore(&priv->lock, flags);
195
196         /* Clear RTS line */
197         send_control_msg(port, CONTROL_RTS, 0);
198
199         /* Setup the read URB and start reading from the device */
200         usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
201                           usb_rcvbulkpipe(priv->udev,
202                                           priv->bulk_address),
203                           priv->bulk_in_buffer, priv->buffer_size,
204                           opticon_read_bulk_callback, priv);
205
206         /* clear the halt status of the enpoint */
207         usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
208
209         result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
210         if (result)
211                 dev_err(&port->dev,
212                         "%s - failed resubmitting read urb, error %d\n",
213                         __func__, result);
214         /* Request CTS line state, sometimes during opening the current
215          * CTS state can be missed. */
216         send_control_msg(port, RESEND_CTS_STATE, 1);
217         return result;
218 }
219
220 static void opticon_close(struct usb_serial_port *port)
221 {
222         struct opticon_private *priv = usb_get_serial_data(port->serial);
223
224         dbg("%s - port %d", __func__, port->number);
225
226         /* shutdown our urbs */
227         usb_kill_urb(priv->bulk_read_urb);
228 }
229
230 static void opticon_write_control_callback(struct urb *urb)
231 {
232         struct opticon_private *priv = urb->context;
233         int status = urb->status;
234         unsigned long flags;
235
236         /* free up the transfer buffer, as usb_free_urb() does not do this */
237         kfree(urb->transfer_buffer);
238
239         /* setup packet may be set if we're using it for writing */
240         kfree(urb->setup_packet);
241
242         if (status)
243                 dbg("%s - nonzero write bulk status received: %d",
244                     __func__, status);
245
246         spin_lock_irqsave(&priv->lock, flags);
247         --priv->outstanding_urbs;
248         spin_unlock_irqrestore(&priv->lock, flags);
249
250         usb_serial_port_softint(priv->port);
251 }
252
253 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
254                          const unsigned char *buf, int count)
255 {
256         struct opticon_private *priv = usb_get_serial_data(port->serial);
257         struct usb_serial *serial = port->serial;
258         struct urb *urb;
259         unsigned char *buffer;
260         unsigned long flags;
261         int status;
262         struct usb_ctrlrequest *dr;
263
264         dbg("%s - port %d", __func__, port->number);
265
266         spin_lock_irqsave(&priv->lock, flags);
267         if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
268                 spin_unlock_irqrestore(&priv->lock, flags);
269                 dbg("%s - write limit hit", __func__);
270                 return 0;
271         }
272         priv->outstanding_urbs++;
273         spin_unlock_irqrestore(&priv->lock, flags);
274
275         buffer = kmalloc(count, GFP_ATOMIC);
276         if (!buffer) {
277                 dev_err(&port->dev, "out of memory\n");
278                 count = -ENOMEM;
279
280                 goto error_no_buffer;
281         }
282
283         urb = usb_alloc_urb(0, GFP_ATOMIC);
284         if (!urb) {
285                 dev_err(&port->dev, "no more free urbs\n");
286                 count = -ENOMEM;
287                 goto error_no_urb;
288         }
289
290         memcpy(buffer, buf, count);
291
292         usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
293
294         /* The conncected devices do not have a bulk write endpoint,
295          * to transmit data to de barcode device the control endpoint is used */
296         dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
297         if (!dr) {
298                 dev_err(&port->dev, "out of memory\n");
299                 count = -ENOMEM;
300                 goto error;
301         }
302
303         dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
304         dr->bRequest = 0x01;
305         dr->wValue = 0;
306         dr->wIndex = 0;
307         dr->wLength = cpu_to_le16(count);
308
309         usb_fill_control_urb(urb, serial->dev,
310                 usb_sndctrlpipe(serial->dev, 0),
311                 (unsigned char *)dr, buffer, count,
312                 opticon_write_control_callback, priv);
313
314         /* send it down the pipe */
315         status = usb_submit_urb(urb, GFP_ATOMIC);
316         if (status) {
317                 dev_err(&port->dev,
318                 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
319                                                         __func__, status);
320                 count = status;
321                 goto error;
322         }
323
324         /* we are done with this urb, so let the host driver
325          * really free it when it is finished with it */
326         usb_free_urb(urb);
327
328         return count;
329 error:
330         usb_free_urb(urb);
331 error_no_urb:
332         kfree(buffer);
333 error_no_buffer:
334         spin_lock_irqsave(&priv->lock, flags);
335         --priv->outstanding_urbs;
336         spin_unlock_irqrestore(&priv->lock, flags);
337         return count;
338 }
339
340 static int opticon_write_room(struct tty_struct *tty)
341 {
342         struct usb_serial_port *port = tty->driver_data;
343         struct opticon_private *priv = usb_get_serial_data(port->serial);
344         unsigned long flags;
345
346         dbg("%s - port %d", __func__, port->number);
347
348         /*
349          * We really can take almost anything the user throws at us
350          * but let's pick a nice big number to tell the tty
351          * layer that we have lots of free space, unless we don't.
352          */
353         spin_lock_irqsave(&priv->lock, flags);
354         if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
355                 spin_unlock_irqrestore(&priv->lock, flags);
356                 dbg("%s - write limit hit", __func__);
357                 return 0;
358         }
359         spin_unlock_irqrestore(&priv->lock, flags);
360
361         return 2048;
362 }
363
364 static void opticon_throttle(struct tty_struct *tty)
365 {
366         struct usb_serial_port *port = tty->driver_data;
367         struct opticon_private *priv = usb_get_serial_data(port->serial);
368         unsigned long flags;
369
370         dbg("%s - port %d", __func__, port->number);
371         spin_lock_irqsave(&priv->lock, flags);
372         priv->throttled = true;
373         spin_unlock_irqrestore(&priv->lock, flags);
374 }
375
376
377 static void opticon_unthrottle(struct tty_struct *tty)
378 {
379         struct usb_serial_port *port = tty->driver_data;
380         struct opticon_private *priv = usb_get_serial_data(port->serial);
381         unsigned long flags;
382         int result, was_throttled;
383
384         dbg("%s - port %d", __func__, port->number);
385
386         spin_lock_irqsave(&priv->lock, flags);
387         priv->throttled = false;
388         was_throttled = priv->actually_throttled;
389         priv->actually_throttled = false;
390         spin_unlock_irqrestore(&priv->lock, flags);
391
392         priv->bulk_read_urb->dev = port->serial->dev;
393         if (was_throttled) {
394                 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
395                 if (result)
396                         dev_err(&port->dev,
397                                 "%s - failed submitting read urb, error %d\n",
398                                                         __func__, result);
399         }
400 }
401
402 static int opticon_tiocmget(struct tty_struct *tty)
403 {
404         struct usb_serial_port *port = tty->driver_data;
405         struct opticon_private *priv = usb_get_serial_data(port->serial);
406         unsigned long flags;
407         int result = 0;
408
409         dbg("%s - port %d", __func__, port->number);
410         if (!usb_get_intfdata(port->serial->interface))
411                 return -ENODEV;
412
413         spin_lock_irqsave(&priv->lock, flags);
414         if (priv->rts)
415                 result |= TIOCM_RTS;
416         if (priv->cts)
417                 result |= TIOCM_CTS;
418         spin_unlock_irqrestore(&priv->lock, flags);
419
420         dbg("%s - %x", __func__, result);
421         return result;
422 }
423
424 static int opticon_tiocmset(struct tty_struct *tty,
425                            unsigned int set, unsigned int clear)
426 {
427         struct usb_serial_port *port = tty->driver_data;
428         struct opticon_private *priv = usb_get_serial_data(port->serial);
429         unsigned long flags;
430         bool rts;
431         bool changed = false;
432
433         if (!usb_get_intfdata(port->serial->interface))
434                 return -ENODEV;
435         /* We only support RTS so we only handle that */
436         spin_lock_irqsave(&priv->lock, flags);
437
438         rts = priv->rts;
439         if (set & TIOCM_RTS)
440                 priv->rts = true;
441         if (clear & TIOCM_RTS)
442                 priv->rts = false;
443         changed = rts ^ priv->rts;
444         spin_unlock_irqrestore(&priv->lock, flags);
445
446         if (!changed)
447                 return 0;
448
449         /* Send the new RTS state to the connected device */
450         return send_control_msg(port, CONTROL_RTS, !rts);
451 }
452
453 static int get_serial_info(struct opticon_private *priv,
454                            struct serial_struct __user *serial)
455 {
456         struct serial_struct tmp;
457
458         if (!serial)
459                 return -EFAULT;
460
461         memset(&tmp, 0x00, sizeof(tmp));
462
463         /* fake emulate a 16550 uart to make userspace code happy */
464         tmp.type                = PORT_16550A;
465         tmp.line                = priv->serial->minor;
466         tmp.port                = 0;
467         tmp.irq                 = 0;
468         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
469         tmp.xmit_fifo_size      = 1024;
470         tmp.baud_base           = 9600;
471         tmp.close_delay         = 5*HZ;
472         tmp.closing_wait        = 30*HZ;
473
474         if (copy_to_user(serial, &tmp, sizeof(*serial)))
475                 return -EFAULT;
476         return 0;
477 }
478
479 static int opticon_ioctl(struct tty_struct *tty,
480                          unsigned int cmd, unsigned long arg)
481 {
482         struct usb_serial_port *port = tty->driver_data;
483         struct opticon_private *priv = usb_get_serial_data(port->serial);
484
485         dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
486
487         switch (cmd) {
488         case TIOCGSERIAL:
489                 return get_serial_info(priv,
490                                        (struct serial_struct __user *)arg);
491         }
492
493         return -ENOIOCTLCMD;
494 }
495
496 static int opticon_startup(struct usb_serial *serial)
497 {
498         struct opticon_private *priv;
499         struct usb_host_interface *intf;
500         int i;
501         int retval = -ENOMEM;
502         bool bulk_in_found = false;
503
504         /* create our private serial structure */
505         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
506         if (priv == NULL) {
507                 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
508                 return -ENOMEM;
509         }
510         spin_lock_init(&priv->lock);
511         priv->serial = serial;
512         priv->port = serial->port[0];
513         priv->udev = serial->dev;
514         priv->outstanding_urbs = 0;     /* Init the outstanding urbs */
515
516         /* find our bulk endpoint */
517         intf = serial->interface->altsetting;
518         for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
519                 struct usb_endpoint_descriptor *endpoint;
520
521                 endpoint = &intf->endpoint[i].desc;
522                 if (!usb_endpoint_is_bulk_in(endpoint))
523                         continue;
524
525                 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
526                 if (!priv->bulk_read_urb) {
527                         dev_err(&priv->udev->dev, "out of memory\n");
528                         goto error;
529                 }
530
531                 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
532                 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
533                 if (!priv->bulk_in_buffer) {
534                         dev_err(&priv->udev->dev, "out of memory\n");
535                         goto error;
536                 }
537
538                 priv->bulk_address = endpoint->bEndpointAddress;
539
540                 bulk_in_found = true;
541                 break;
542                 }
543
544         if (!bulk_in_found) {
545                 dev_err(&priv->udev->dev,
546                         "Error - the proper endpoints were not found!\n");
547                 goto error;
548         }
549
550         usb_set_serial_data(serial, priv);
551         return 0;
552
553 error:
554         usb_free_urb(priv->bulk_read_urb);
555         kfree(priv->bulk_in_buffer);
556         kfree(priv);
557         return retval;
558 }
559
560 static void opticon_disconnect(struct usb_serial *serial)
561 {
562         struct opticon_private *priv = usb_get_serial_data(serial);
563
564         dbg("%s", __func__);
565
566         usb_kill_urb(priv->bulk_read_urb);
567         usb_free_urb(priv->bulk_read_urb);
568 }
569
570 static void opticon_release(struct usb_serial *serial)
571 {
572         struct opticon_private *priv = usb_get_serial_data(serial);
573
574         dbg("%s", __func__);
575
576         kfree(priv->bulk_in_buffer);
577         kfree(priv);
578 }
579
580 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
581 {
582         struct usb_serial *serial = usb_get_intfdata(intf);
583         struct opticon_private *priv = usb_get_serial_data(serial);
584
585         usb_kill_urb(priv->bulk_read_urb);
586         return 0;
587 }
588
589 static int opticon_resume(struct usb_interface *intf)
590 {
591         struct usb_serial *serial = usb_get_intfdata(intf);
592         struct opticon_private *priv = usb_get_serial_data(serial);
593         struct usb_serial_port *port = serial->port[0];
594         int result;
595
596         mutex_lock(&port->port.mutex);
597         /* This is protected by the port mutex against close/open */
598         if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
599                 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
600         else
601                 result = 0;
602         mutex_unlock(&port->port.mutex);
603         return result;
604 }
605
606 static struct usb_driver opticon_driver = {
607         .name =         "opticon",
608         .probe =        usb_serial_probe,
609         .disconnect =   usb_serial_disconnect,
610         .suspend =      opticon_suspend,
611         .resume =       opticon_resume,
612         .id_table =     id_table,
613         .no_dynamic_id =        1,
614 };
615
616 static struct usb_serial_driver opticon_device = {
617         .driver = {
618                 .owner =        THIS_MODULE,
619                 .name =         "opticon",
620         },
621         .id_table =             id_table,
622         .usb_driver =           &opticon_driver,
623         .num_ports =            1,
624         .attach =               opticon_startup,
625         .open =                 opticon_open,
626         .close =                opticon_close,
627         .write =                opticon_write,
628         .write_room =           opticon_write_room,
629         .disconnect =           opticon_disconnect,
630         .release =              opticon_release,
631         .throttle =             opticon_throttle,
632         .unthrottle =           opticon_unthrottle,
633         .ioctl =                opticon_ioctl,
634         .tiocmget =             opticon_tiocmget,
635         .tiocmset =             opticon_tiocmset,
636 };
637
638 static int __init opticon_init(void)
639 {
640         int retval;
641
642         retval = usb_serial_register(&opticon_device);
643         if (retval)
644                 return retval;
645         retval = usb_register(&opticon_driver);
646         if (retval)
647                 usb_serial_deregister(&opticon_device);
648         return retval;
649 }
650
651 static void __exit opticon_exit(void)
652 {
653         usb_deregister(&opticon_driver);
654         usb_serial_deregister(&opticon_device);
655 }
656
657 module_init(opticon_init);
658 module_exit(opticon_exit);
659 MODULE_DESCRIPTION(DRIVER_DESC);
660 MODULE_LICENSE("GPL");
661
662 module_param(debug, bool, S_IRUGO | S_IWUSR);
663 MODULE_PARM_DESC(debug, "Debug enabled or not");