blob: 9b1648945e7a854151d260dd79815a8db90f4b12 [file] [log] [blame]
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -08001/*
2 * Symbol USB barcode to serial driver
3 *
Johan Hovolda85796e2013-04-16 18:01:30 +02004 * Copyright (C) 2013 Johan Hovold <jhovold@gmail.com>
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -08005 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080017#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
Németh Márton7d40d7e2010-01-10 15:34:24 +010024static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080025 { USB_DEVICE(0x05e0, 0x0600) },
26 { },
27};
28MODULE_DEVICE_TABLE(usb, id_table);
29
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080030struct symbol_private {
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080031 spinlock_t lock; /* protects the following flags */
32 bool throttled;
33 bool actually_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080034};
35
36static void symbol_int_callback(struct urb *urb)
37{
Johan Hovoldcced9262013-04-16 18:01:28 +020038 struct usb_serial_port *port = urb->context;
Johan Hovolda85796e2013-04-16 18:01:30 +020039 struct symbol_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080040 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080041 int status = urb->status;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080042 int result;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080043 int data_length;
44
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080045 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 */
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070053 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
54 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080055 return;
56 default:
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070057 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
58 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080059 goto exit;
60 }
61
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010062 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080063
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 */
Jiri Slaby2e124b42013-01-03 15:53:06 +010075 tty_insert_flip_string(&port->port, &data[1], data_length);
76 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080077 } else {
Johan Hovoldef310252013-04-16 18:01:29 +020078 dev_dbg(&port->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +010079 "Improper amount of data received from the device, "
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080080 "%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) {
Johan Hovoldcced9262013-04-16 18:01:28 +020088 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080089 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
Alan Coxa509a7e2009-09-19 13:13:26 -070098static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080099{
100 struct symbol_private *priv = usb_get_serial_data(port->serial);
101 unsigned long flags;
102 int result = 0;
103
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800104 spin_lock_irqsave(&priv->lock, flags);
105 priv->throttled = false;
106 priv->actually_throttled = false;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800107 spin_unlock_irqrestore(&priv->lock, flags);
108
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800109 /* Start reading from the device */
Johan Hovoldcced9262013-04-16 18:01:28 +0200110 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800111 if (result)
112 dev_err(&port->dev,
113 "%s - failed resubmitting read urb, error %d\n",
114 __func__, result);
115 return result;
116}
117
Alan Cox335f8512009-06-11 12:26:29 +0100118static void symbol_close(struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800119{
Johan Hovoldcced9262013-04-16 18:01:28 +0200120 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800121}
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);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800127
Oliver Neukum63832512009-10-07 10:50:23 +0200128 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800129 priv->throttled = true;
Oliver Neukum63832512009-10-07 10:50:23 +0200130 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800131}
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);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800137 int result;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200138 bool was_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800139
Oliver Neukum63832512009-10-07 10:50:23 +0200140 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800141 priv->throttled = false;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200142 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800143 priv->actually_throttled = false;
Oliver Neukum63832512009-10-07 10:50:23 +0200144 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800145
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200146 if (was_throttled) {
Johan Hovoldcced9262013-04-16 18:01:28 +0200147 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200148 if (result)
149 dev_err(&port->dev,
150 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800151 __func__, result);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200152 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800153}
154
155static int symbol_startup(struct usb_serial *serial)
156{
Johan Hovoldcced9262013-04-16 18:01:28 +0200157 if (!serial->num_interrupt_in) {
158 dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
159 return -ENODEV;
160 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800161
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800162 return 0;
Alan Sternf9c99bb2009-06-02 11:53:55 -0400163}
164
Johan Hovolda85796e2013-04-16 18:01:30 +0200165static int symbol_port_probe(struct usb_serial_port *port)
Alan Sternf9c99bb2009-06-02 11:53:55 -0400166{
Johan Hovolda85796e2013-04-16 18:01:30 +0200167 struct symbol_private *priv;
168
169 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
170 if (!priv)
171 return -ENOMEM;
172
173 spin_lock_init(&priv->lock);
174
175 usb_set_serial_port_data(port, priv);
176
177 return 0;
178}
179
180static int symbol_port_remove(struct usb_serial_port *port)
181{
182 struct symbol_private *priv = usb_get_serial_port_data(port);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400183
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800184 kfree(priv);
Johan Hovolda85796e2013-04-16 18:01:30 +0200185
186 return 0;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800187}
188
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800189static struct usb_serial_driver symbol_device = {
190 .driver = {
191 .owner = THIS_MODULE,
192 .name = "symbol",
193 },
194 .id_table = id_table,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800195 .num_ports = 1,
196 .attach = symbol_startup,
Johan Hovolda85796e2013-04-16 18:01:30 +0200197 .port_probe = symbol_port_probe,
198 .port_remove = symbol_port_remove,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800199 .open = symbol_open,
200 .close = symbol_close,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800201 .throttle = symbol_throttle,
202 .unthrottle = symbol_unthrottle,
Johan Hovoldcced9262013-04-16 18:01:28 +0200203 .read_int_callback = symbol_int_callback,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800204};
205
Alan Sternd8603222012-02-23 14:57:25 -0500206static struct usb_serial_driver * const serial_drivers[] = {
207 &symbol_device, NULL
208};
209
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700210module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800211
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800212MODULE_LICENSE("GPL");