blob: 701fffa8431f5f39e8643c66d06af8c244427ef3 [file] [log] [blame]
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080016#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
Németh Márton7d40d7e2010-01-10 15:34:24 +010023static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080024 { USB_DEVICE(0x05e0, 0x0600) },
25 { },
26};
27MODULE_DEVICE_TABLE(usb, id_table);
28
29/* This structure holds all of the individual device information */
30struct 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
45static 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;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080053 int data_length;
54
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080055 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 */
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070063 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
64 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080065 return;
66 default:
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070067 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
68 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080069 goto exit;
70 }
71
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010072 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080073
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) {
Alan Coxa108bfc2010-02-18 16:44:01 +000087 tty_insert_flip_string(tty, &data[1], data_length);
88 tty_flip_buffer_push(tty);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080089 tty_kref_put(tty);
90 }
91 } else {
92 dev_dbg(&priv->udev->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +010093 "Improper amount of data received from the device, "
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080094 "%d bytes", urb->actual_length);
95 }
96
97exit:
98 spin_lock(&priv->lock);
99
100 /* Continue trying to always read if we should */
101 if (!priv->throttled) {
102 usb_fill_int_urb(priv->int_urb, priv->udev,
103 usb_rcvintpipe(priv->udev,
104 priv->int_address),
105 priv->int_buffer, priv->buffer_size,
106 symbol_int_callback, priv, priv->bInterval);
107 result = usb_submit_urb(priv->int_urb, GFP_ATOMIC);
108 if (result)
109 dev_err(&port->dev,
110 "%s - failed resubmitting read urb, error %d\n",
111 __func__, result);
112 } else
113 priv->actually_throttled = true;
114 spin_unlock(&priv->lock);
115}
116
Alan Coxa509a7e2009-09-19 13:13:26 -0700117static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800118{
119 struct symbol_private *priv = usb_get_serial_data(port->serial);
120 unsigned long flags;
121 int result = 0;
122
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800123 spin_lock_irqsave(&priv->lock, flags);
124 priv->throttled = false;
125 priv->actually_throttled = false;
126 priv->port = port;
127 spin_unlock_irqrestore(&priv->lock, flags);
128
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800129 /* Start reading from the device */
130 usb_fill_int_urb(priv->int_urb, priv->udev,
131 usb_rcvintpipe(priv->udev, priv->int_address),
132 priv->int_buffer, priv->buffer_size,
133 symbol_int_callback, priv, priv->bInterval);
134 result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
135 if (result)
136 dev_err(&port->dev,
137 "%s - failed resubmitting read urb, error %d\n",
138 __func__, result);
139 return result;
140}
141
Alan Cox335f8512009-06-11 12:26:29 +0100142static void symbol_close(struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800143{
144 struct symbol_private *priv = usb_get_serial_data(port->serial);
145
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800146 /* shutdown our urbs */
147 usb_kill_urb(priv->int_urb);
148}
149
150static void symbol_throttle(struct tty_struct *tty)
151{
152 struct usb_serial_port *port = tty->driver_data;
153 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800154
Oliver Neukum63832512009-10-07 10:50:23 +0200155 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800156 priv->throttled = true;
Oliver Neukum63832512009-10-07 10:50:23 +0200157 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800158}
159
160static void symbol_unthrottle(struct tty_struct *tty)
161{
162 struct usb_serial_port *port = tty->driver_data;
163 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800164 int result;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200165 bool was_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800166
Oliver Neukum63832512009-10-07 10:50:23 +0200167 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800168 priv->throttled = false;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200169 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800170 priv->actually_throttled = false;
Oliver Neukum63832512009-10-07 10:50:23 +0200171 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800172
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200173 if (was_throttled) {
Oliver Neukum63832512009-10-07 10:50:23 +0200174 result = usb_submit_urb(priv->int_urb, GFP_KERNEL);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200175 if (result)
176 dev_err(&port->dev,
177 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800178 __func__, result);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200179 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800180}
181
182static int symbol_startup(struct usb_serial *serial)
183{
184 struct symbol_private *priv;
185 struct usb_host_interface *intf;
186 int i;
187 int retval = -ENOMEM;
188 bool int_in_found = false;
189
190 /* create our private serial structure */
191 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
192 if (priv == NULL) {
193 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
194 return -ENOMEM;
195 }
196 spin_lock_init(&priv->lock);
197 priv->serial = serial;
198 priv->port = serial->port[0];
199 priv->udev = serial->dev;
200
201 /* find our interrupt endpoint */
202 intf = serial->interface->altsetting;
203 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
204 struct usb_endpoint_descriptor *endpoint;
205
206 endpoint = &intf->endpoint[i].desc;
207 if (!usb_endpoint_is_int_in(endpoint))
208 continue;
209
210 priv->int_urb = usb_alloc_urb(0, GFP_KERNEL);
211 if (!priv->int_urb) {
212 dev_err(&priv->udev->dev, "out of memory\n");
213 goto error;
214 }
215
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700216 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800217 priv->int_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
218 if (!priv->int_buffer) {
219 dev_err(&priv->udev->dev, "out of memory\n");
220 goto error;
221 }
222
223 priv->int_address = endpoint->bEndpointAddress;
224 priv->bInterval = endpoint->bInterval;
225
226 /* set up our int urb */
227 usb_fill_int_urb(priv->int_urb, priv->udev,
228 usb_rcvintpipe(priv->udev,
229 endpoint->bEndpointAddress),
230 priv->int_buffer, priv->buffer_size,
231 symbol_int_callback, priv, priv->bInterval);
232
233 int_in_found = true;
234 break;
235 }
236
237 if (!int_in_found) {
238 dev_err(&priv->udev->dev,
239 "Error - the proper endpoints were not found!\n");
240 goto error;
241 }
242
243 usb_set_serial_data(serial, priv);
244 return 0;
245
246error:
247 usb_free_urb(priv->int_urb);
248 kfree(priv->int_buffer);
249 kfree(priv);
250 return retval;
251}
252
Alan Sternf9c99bb2009-06-02 11:53:55 -0400253static void symbol_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800254{
255 struct symbol_private *priv = usb_get_serial_data(serial);
256
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800257 usb_kill_urb(priv->int_urb);
258 usb_free_urb(priv->int_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400259}
260
261static void symbol_release(struct usb_serial *serial)
262{
263 struct symbol_private *priv = usb_get_serial_data(serial);
264
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800265 kfree(priv->int_buffer);
266 kfree(priv);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800267}
268
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800269static struct usb_serial_driver symbol_device = {
270 .driver = {
271 .owner = THIS_MODULE,
272 .name = "symbol",
273 },
274 .id_table = id_table,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800275 .num_ports = 1,
276 .attach = symbol_startup,
277 .open = symbol_open,
278 .close = symbol_close,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400279 .disconnect = symbol_disconnect,
280 .release = symbol_release,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800281 .throttle = symbol_throttle,
282 .unthrottle = symbol_unthrottle,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800283};
284
Alan Sternd8603222012-02-23 14:57:25 -0500285static struct usb_serial_driver * const serial_drivers[] = {
286 &symbol_device, NULL
287};
288
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700289module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800290
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800291MODULE_LICENSE("GPL");