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