blob: 32ebddf7f010ea6a6c0d18b6afb3f53b0a6ae16a [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;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080032 spinlock_t lock; /* protects the following flags */
33 bool throttled;
34 bool actually_throttled;
35 bool rts;
36};
37
38static void symbol_int_callback(struct urb *urb)
39{
Johan Hovoldcced9262013-04-16 18:01:28 +020040 struct usb_serial_port *port = urb->context;
41 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080042 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080043 int status = urb->status;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080044 int result;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080045 int data_length;
46
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080047 switch (status) {
48 case 0:
49 /* success */
50 break;
51 case -ECONNRESET:
52 case -ENOENT:
53 case -ESHUTDOWN:
54 /* this urb is terminated, clean up */
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070055 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
56 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080057 return;
58 default:
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070059 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
60 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080061 goto exit;
62 }
63
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010064 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080065
66 if (urb->actual_length > 1) {
67 data_length = urb->actual_length - 1;
68
69 /*
70 * Data from the device comes with a 1 byte header:
71 *
72 * <size of data>data...
73 * This is real data to be sent to the tty layer
74 * we pretty much just ignore the size and send everything
75 * else to the tty layer.
76 */
Jiri Slaby2e124b42013-01-03 15:53:06 +010077 tty_insert_flip_string(&port->port, &data[1], data_length);
78 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080079 } else {
80 dev_dbg(&priv->udev->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +010081 "Improper amount of data received from the device, "
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080082 "%d bytes", urb->actual_length);
83 }
84
85exit:
86 spin_lock(&priv->lock);
87
88 /* Continue trying to always read if we should */
89 if (!priv->throttled) {
Johan Hovoldcced9262013-04-16 18:01:28 +020090 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080091 if (result)
92 dev_err(&port->dev,
93 "%s - failed resubmitting read urb, error %d\n",
94 __func__, result);
95 } else
96 priv->actually_throttled = true;
97 spin_unlock(&priv->lock);
98}
99
Alan Coxa509a7e2009-09-19 13:13:26 -0700100static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800101{
102 struct symbol_private *priv = usb_get_serial_data(port->serial);
103 unsigned long flags;
104 int result = 0;
105
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800106 spin_lock_irqsave(&priv->lock, flags);
107 priv->throttled = false;
108 priv->actually_throttled = false;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800109 spin_unlock_irqrestore(&priv->lock, flags);
110
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800111 /* Start reading from the device */
Johan Hovoldcced9262013-04-16 18:01:28 +0200112 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800113 if (result)
114 dev_err(&port->dev,
115 "%s - failed resubmitting read urb, error %d\n",
116 __func__, result);
117 return result;
118}
119
Alan Cox335f8512009-06-11 12:26:29 +0100120static void symbol_close(struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800121{
Johan Hovoldcced9262013-04-16 18:01:28 +0200122 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800123}
124
125static void symbol_throttle(struct tty_struct *tty)
126{
127 struct usb_serial_port *port = tty->driver_data;
128 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800129
Oliver Neukum63832512009-10-07 10:50:23 +0200130 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800131 priv->throttled = true;
Oliver Neukum63832512009-10-07 10:50:23 +0200132 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800133}
134
135static void symbol_unthrottle(struct tty_struct *tty)
136{
137 struct usb_serial_port *port = tty->driver_data;
138 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800139 int result;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200140 bool was_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800141
Oliver Neukum63832512009-10-07 10:50:23 +0200142 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800143 priv->throttled = false;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200144 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800145 priv->actually_throttled = false;
Oliver Neukum63832512009-10-07 10:50:23 +0200146 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800147
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200148 if (was_throttled) {
Johan Hovoldcced9262013-04-16 18:01:28 +0200149 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200150 if (result)
151 dev_err(&port->dev,
152 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800153 __func__, result);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200154 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800155}
156
157static int symbol_startup(struct usb_serial *serial)
158{
159 struct symbol_private *priv;
Johan Hovoldcced9262013-04-16 18:01:28 +0200160
161 if (!serial->num_interrupt_in) {
162 dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
163 return -ENODEV;
164 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800165
166 /* create our private serial structure */
167 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
168 if (priv == NULL) {
169 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
170 return -ENOMEM;
171 }
172 spin_lock_init(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800173 priv->udev = serial->dev;
174
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800175 usb_set_serial_data(serial, priv);
176 return 0;
Alan Sternf9c99bb2009-06-02 11:53:55 -0400177}
178
179static void symbol_release(struct usb_serial *serial)
180{
181 struct symbol_private *priv = usb_get_serial_data(serial);
182
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800183 kfree(priv);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800184}
185
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800186static struct usb_serial_driver symbol_device = {
187 .driver = {
188 .owner = THIS_MODULE,
189 .name = "symbol",
190 },
191 .id_table = id_table,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800192 .num_ports = 1,
193 .attach = symbol_startup,
194 .open = symbol_open,
195 .close = symbol_close,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400196 .release = symbol_release,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800197 .throttle = symbol_throttle,
198 .unthrottle = symbol_unthrottle,
Johan Hovoldcced9262013-04-16 18:01:28 +0200199 .read_int_callback = symbol_int_callback,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800200};
201
Alan Sternd8603222012-02-23 14:57:25 -0500202static struct usb_serial_driver * const serial_drivers[] = {
203 &symbol_device, NULL
204};
205
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700206module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800207
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800208MODULE_LICENSE("GPL");