Merge branch 'fixes' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / serial / symbolserial.c
CommitLineData
68b44eae
GKH
1/*
2 * Symbol USB barcode to serial driver
3 *
a85796ee 4 * Copyright (C) 2013 Johan Hovold <jhovold@gmail.com>
68b44eae
GKH
5 * Copyright (C) 2009 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (C) 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>
5a0e3ad6 16#include <linux/slab.h>
68b44eae
GKH
17#include <linux/tty_driver.h>
18#include <linux/tty_flip.h>
19#include <linux/module.h>
20#include <linux/usb.h>
21#include <linux/usb/serial.h>
22#include <linux/uaccess.h>
23
7d40d7e8 24static const struct usb_device_id id_table[] = {
68b44eae
GKH
25 { USB_DEVICE(0x05e0, 0x0600) },
26 { },
27};
28MODULE_DEVICE_TABLE(usb, id_table);
29
68b44eae 30struct symbol_private {
68b44eae
GKH
31 spinlock_t lock; /* protects the following flags */
32 bool throttled;
33 bool actually_throttled;
68b44eae
GKH
34};
35
36static void symbol_int_callback(struct urb *urb)
37{
cced926f 38 struct usb_serial_port *port = urb->context;
a85796ee 39 struct symbol_private *priv = usb_get_serial_port_data(port);
68b44eae 40 unsigned char *data = urb->transfer_buffer;
68b44eae 41 int status = urb->status;
68b44eae 42 int result;
68b44eae
GKH
43 int data_length;
44
68b44eae
GKH
45 switch (status) {
46 case 0:
47 /* success */
48 break;
49 case -ECONNRESET:
50 case -ENOENT:
51 case -ESHUTDOWN:
52 /* this urb is terminated, clean up */
e4083ea5
GKH
53 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
54 __func__, status);
68b44eae
GKH
55 return;
56 default:
e4083ea5
GKH
57 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
58 __func__, status);
68b44eae
GKH
59 goto exit;
60 }
61
59d33f2f 62 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
68b44eae
GKH
63
64 if (urb->actual_length > 1) {
65 data_length = urb->actual_length - 1;
66
67 /*
68 * Data from the device comes with a 1 byte header:
69 *
70 * <size of data>data...
71 * This is real data to be sent to the tty layer
72 * we pretty much just ignore the size and send everything
73 * else to the tty layer.
74 */
2e124b4a
JS
75 tty_insert_flip_string(&port->port, &data[1], data_length);
76 tty_flip_buffer_push(&port->port);
68b44eae 77 } else {
ef31025d 78 dev_dbg(&port->dev,
9ddc5b6f 79 "Improper amount of data received from the device, "
68b44eae
GKH
80 "%d bytes", urb->actual_length);
81 }
82
83exit:
84 spin_lock(&priv->lock);
85
86 /* Continue trying to always read if we should */
87 if (!priv->throttled) {
cced926f 88 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
68b44eae
GKH
89 if (result)
90 dev_err(&port->dev,
91 "%s - failed resubmitting read urb, error %d\n",
92 __func__, result);
93 } else
94 priv->actually_throttled = true;
95 spin_unlock(&priv->lock);
96}
97
a509a7e4 98static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
68b44eae
GKH
99{
100 struct symbol_private *priv = usb_get_serial_data(port->serial);
101 unsigned long flags;
102 int result = 0;
103
68b44eae
GKH
104 spin_lock_irqsave(&priv->lock, flags);
105 priv->throttled = false;
106 priv->actually_throttled = false;
68b44eae
GKH
107 spin_unlock_irqrestore(&priv->lock, flags);
108
68b44eae 109 /* Start reading from the device */
cced926f 110 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
68b44eae
GKH
111 if (result)
112 dev_err(&port->dev,
113 "%s - failed resubmitting read urb, error %d\n",
114 __func__, result);
115 return result;
116}
117
335f8514 118static void symbol_close(struct usb_serial_port *port)
68b44eae 119{
cced926f 120 usb_kill_urb(port->interrupt_in_urb);
68b44eae
GKH
121}
122
123static void symbol_throttle(struct tty_struct *tty)
124{
125 struct usb_serial_port *port = tty->driver_data;
126 struct symbol_private *priv = usb_get_serial_data(port->serial);
68b44eae 127
63832515 128 spin_lock_irq(&priv->lock);
68b44eae 129 priv->throttled = true;
63832515 130 spin_unlock_irq(&priv->lock);
68b44eae
GKH
131}
132
133static void symbol_unthrottle(struct tty_struct *tty)
134{
135 struct usb_serial_port *port = tty->driver_data;
136 struct symbol_private *priv = usb_get_serial_data(port->serial);
68b44eae 137 int result;
b2a5cf1b 138 bool was_throttled;
68b44eae 139
63832515 140 spin_lock_irq(&priv->lock);
68b44eae 141 priv->throttled = false;
b2a5cf1b 142 was_throttled = priv->actually_throttled;
68b44eae 143 priv->actually_throttled = false;
63832515 144 spin_unlock_irq(&priv->lock);
68b44eae 145
b2a5cf1b 146 if (was_throttled) {
cced926f 147 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
b2a5cf1b
ON
148 if (result)
149 dev_err(&port->dev,
150 "%s - failed submitting read urb, error %d\n",
68b44eae 151 __func__, result);
b2a5cf1b 152 }
68b44eae
GKH
153}
154
155static int symbol_startup(struct usb_serial *serial)
156{
cced926f
JH
157 if (!serial->num_interrupt_in) {
158 dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
159 return -ENODEV;
160 }
68b44eae 161
a85796ee
JH
162 return 0;
163}
164
165static int symbol_port_probe(struct usb_serial_port *port)
166{
167 struct symbol_private *priv;
168
68b44eae 169 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
a85796ee 170 if (!priv)
68b44eae 171 return -ENOMEM;
a85796ee 172
68b44eae 173 spin_lock_init(&priv->lock);
68b44eae 174
a85796ee
JH
175 usb_set_serial_port_data(port, priv);
176
68b44eae 177 return 0;
f9c99bb8
AS
178}
179
a85796ee 180static int symbol_port_remove(struct usb_serial_port *port)
f9c99bb8 181{
a85796ee 182 struct symbol_private *priv = usb_get_serial_port_data(port);
f9c99bb8 183
68b44eae 184 kfree(priv);
a85796ee
JH
185
186 return 0;
68b44eae
GKH
187}
188
68b44eae
GKH
189static struct usb_serial_driver symbol_device = {
190 .driver = {
191 .owner = THIS_MODULE,
192 .name = "symbol",
193 },
194 .id_table = id_table,
68b44eae
GKH
195 .num_ports = 1,
196 .attach = symbol_startup,
a85796ee
JH
197 .port_probe = symbol_port_probe,
198 .port_remove = symbol_port_remove,
68b44eae
GKH
199 .open = symbol_open,
200 .close = symbol_close,
68b44eae
GKH
201 .throttle = symbol_throttle,
202 .unthrottle = symbol_unthrottle,
cced926f 203 .read_int_callback = symbol_int_callback,
68b44eae
GKH
204};
205
d860322f
AS
206static struct usb_serial_driver * const serial_drivers[] = {
207 &symbol_device, NULL
208};
209
68e24113 210module_usb_serial_driver(serial_drivers, id_table);
68b44eae 211
68b44eae 212MODULE_LICENSE("GPL");