]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/serial/symbolserial.c
2ffa6ae3b5edc87909b4177bf398b320b3d96dad
[karo-tx-linux.git] / drivers / usb / serial / symbolserial.c
1 /*
2  * Symbol USB barcode to serial driver
3  *
4  * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
5  * Copyright (C) 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/slab.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
22
23 static const struct usb_device_id id_table[] = {
24         { USB_DEVICE(0x05e0, 0x0600) },
25         { },
26 };
27 MODULE_DEVICE_TABLE(usb, id_table);
28
29 /* This structure holds all of the individual device information */
30 struct symbol_private {
31         struct usb_device *udev;
32         struct usb_serial *serial;
33         struct usb_serial_port *port;
34         unsigned char *int_buffer;
35         struct urb *int_urb;
36         int buffer_size;
37         u8 bInterval;
38         u8 int_address;
39         spinlock_t lock;        /* protects the following flags */
40         bool throttled;
41         bool actually_throttled;
42         bool rts;
43 };
44
45 static void symbol_int_callback(struct urb *urb)
46 {
47         struct symbol_private *priv = urb->context;
48         unsigned char *data = urb->transfer_buffer;
49         struct usb_serial_port *port = priv->port;
50         int status = urb->status;
51         struct tty_struct *tty;
52         int result;
53         int data_length;
54
55         switch (status) {
56         case 0:
57                 /* success */
58                 break;
59         case -ECONNRESET:
60         case -ENOENT:
61         case -ESHUTDOWN:
62                 /* this urb is terminated, clean up */
63                 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
64                         __func__, status);
65                 return;
66         default:
67                 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
68                         __func__, status);
69                 goto exit;
70         }
71
72         usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
73
74         if (urb->actual_length > 1) {
75                 data_length = urb->actual_length - 1;
76
77                 /*
78                  * Data from the device comes with a 1 byte header:
79                  *
80                  * <size of data>data...
81                  *      This is real data to be sent to the tty layer
82                  * we pretty much just ignore the size and send everything
83                  * else to the tty layer.
84                  */
85                 tty = tty_port_tty_get(&port->port);
86                 if (tty) {
87                         tty_insert_flip_string(&port->port, &data[1],
88                                         data_length);
89                         tty_flip_buffer_push(tty);
90                         tty_kref_put(tty);
91                 }
92         } else {
93                 dev_dbg(&priv->udev->dev,
94                         "Improper amount of data received from the device, "
95                         "%d bytes", urb->actual_length);
96         }
97
98 exit:
99         spin_lock(&priv->lock);
100
101         /* Continue trying to always read if we should */
102         if (!priv->throttled) {
103                 usb_fill_int_urb(priv->int_urb, priv->udev,
104                                  usb_rcvintpipe(priv->udev,
105                                                 priv->int_address),
106                                  priv->int_buffer, priv->buffer_size,
107                                  symbol_int_callback, priv, priv->bInterval);
108                 result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
109                 if (result)
110                         dev_err(&port->dev,
111                             "%s - failed resubmitting read urb, error %d\n",
112                                                         __func__, result);
113         } else
114                 priv->actually_throttled = true;
115         spin_unlock(&priv->lock);
116 }
117
118 static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
119 {
120         struct symbol_private *priv = usb_get_serial_data(port->serial);
121         unsigned long flags;
122         int result = 0;
123
124         spin_lock_irqsave(&priv->lock, flags);
125         priv->throttled = false;
126         priv->actually_throttled = false;
127         priv->port = port;
128         spin_unlock_irqrestore(&priv->lock, flags);
129
130         /* Start reading from the device */
131         usb_fill_int_urb(priv->int_urb, priv->udev,
132                          usb_rcvintpipe(priv->udev, priv->int_address),
133                          priv->int_buffer, priv->buffer_size,
134                          symbol_int_callback, priv, priv->bInterval);
135         result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
136         if (result)
137                 dev_err(&port->dev,
138                         "%s - failed resubmitting read urb, error %d\n",
139                         __func__, result);
140         return result;
141 }
142
143 static void symbol_close(struct usb_serial_port *port)
144 {
145         struct symbol_private *priv = usb_get_serial_data(port->serial);
146
147         /* shutdown our urbs */
148         usb_kill_urb(priv->int_urb);
149 }
150
151 static void symbol_throttle(struct tty_struct *tty)
152 {
153         struct usb_serial_port *port = tty->driver_data;
154         struct symbol_private *priv = usb_get_serial_data(port->serial);
155
156         spin_lock_irq(&priv->lock);
157         priv->throttled = true;
158         spin_unlock_irq(&priv->lock);
159 }
160
161 static void symbol_unthrottle(struct tty_struct *tty)
162 {
163         struct usb_serial_port *port = tty->driver_data;
164         struct symbol_private *priv = usb_get_serial_data(port->serial);
165         int result;
166         bool was_throttled;
167
168         spin_lock_irq(&priv->lock);
169         priv->throttled = false;
170         was_throttled = priv->actually_throttled;
171         priv->actually_throttled = false;
172         spin_unlock_irq(&priv->lock);
173
174         if (was_throttled) {
175                 result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
176                 if (result)
177                         dev_err(&port->dev,
178                                 "%s - failed submitting read urb, error %d\n",
179                                                         __func__, result);
180         }
181 }
182
183 static int symbol_startup(struct usb_serial *serial)
184 {
185         struct symbol_private *priv;
186         struct usb_host_interface *intf;
187         int i;
188         int retval = -ENOMEM;
189         bool int_in_found = false;
190
191         /* create our private serial structure */
192         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
193         if (priv == NULL) {
194                 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
195                 return -ENOMEM;
196         }
197         spin_lock_init(&priv->lock);
198         priv->serial = serial;
199         priv->port = serial->port[0];
200         priv->udev = serial->dev;
201
202         /* find our interrupt endpoint */
203         intf = serial->interface->altsetting;
204         for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
205                 struct usb_endpoint_descriptor *endpoint;
206
207                 endpoint = &intf->endpoint[i].desc;
208                 if (!usb_endpoint_is_int_in(endpoint))
209                         continue;
210
211                 priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
212                 if (!priv->int_urb) {
213                         dev_err(&priv->udev->dev, "out of memory\n");
214                         goto error;
215                 }
216
217                 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
218                 priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
219                 if (!priv->int_buffer) {
220                         dev_err(&priv->udev->dev, "out of memory\n");
221                         goto error;
222                 }
223
224                 priv->int_address = endpoint->bEndpointAddress;
225                 priv->bInterval = endpoint->bInterval;
226
227                 /* set up our int urb */
228                 usb_fill_int_urb(priv->int_urb, priv->udev,
229                                  usb_rcvintpipe(priv->udev,
230                                                 endpoint->bEndpointAddress),
231                                  priv->int_buffer, priv->buffer_size,
232                                  symbol_int_callback, priv, priv->bInterval);
233
234                 int_in_found = true;
235                 break;
236                 }
237
238         if (!int_in_found) {
239                 dev_err(&priv->udev->dev,
240                         "Error - the proper endpoints were not found!\n");
241                 goto error;
242         }
243
244         usb_set_serial_data(serial, priv);
245         return 0;
246
247 error:
248         usb_free_urb(priv->int_urb);
249         kfree(priv->int_buffer);
250         kfree(priv);
251         return retval;
252 }
253
254 static void symbol_disconnect(struct usb_serial *serial)
255 {
256         struct symbol_private *priv = usb_get_serial_data(serial);
257
258         usb_kill_urb(priv->int_urb);
259         usb_free_urb(priv->int_urb);
260 }
261
262 static void symbol_release(struct usb_serial *serial)
263 {
264         struct symbol_private *priv = usb_get_serial_data(serial);
265
266         kfree(priv->int_buffer);
267         kfree(priv);
268 }
269
270 static struct usb_serial_driver symbol_device = {
271         .driver = {
272                 .owner =        THIS_MODULE,
273                 .name =         "symbol",
274         },
275         .id_table =             id_table,
276         .num_ports =            1,
277         .attach =               symbol_startup,
278         .open =                 symbol_open,
279         .close =                symbol_close,
280         .disconnect =           symbol_disconnect,
281         .release =              symbol_release,
282         .throttle =             symbol_throttle,
283         .unthrottle =           symbol_unthrottle,
284 };
285
286 static struct usb_serial_driver * const serial_drivers[] = {
287         &symbol_device, NULL
288 };
289
290 module_usb_serial_driver(serial_drivers, id_table);
291
292 MODULE_LICENSE("GPL");