blob: 2c2bfa1994f8e4cf85ee60a136bf11e5f791adc8 [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
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080029struct symbol_private {
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080030 spinlock_t lock; /* protects the following flags */
31 bool throttled;
32 bool actually_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080033};
34
35static void symbol_int_callback(struct urb *urb)
36{
Johan Hovoldcced9262013-04-16 18:01:28 +020037 struct usb_serial_port *port = urb->context;
38 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080039 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080040 int status = urb->status;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080041 int result;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080042 int data_length;
43
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080044 switch (status) {
45 case 0:
46 /* success */
47 break;
48 case -ECONNRESET:
49 case -ENOENT:
50 case -ESHUTDOWN:
51 /* this urb is terminated, clean up */
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070052 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
53 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080054 return;
55 default:
Greg Kroah-Hartmane4083ea2012-05-15 16:27:32 -070056 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
57 __func__, status);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080058 goto exit;
59 }
60
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010061 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080062
63 if (urb->actual_length > 1) {
64 data_length = urb->actual_length - 1;
65
66 /*
67 * Data from the device comes with a 1 byte header:
68 *
69 * <size of data>data...
70 * This is real data to be sent to the tty layer
71 * we pretty much just ignore the size and send everything
72 * else to the tty layer.
73 */
Jiri Slaby2e124b42013-01-03 15:53:06 +010074 tty_insert_flip_string(&port->port, &data[1], data_length);
75 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080076 } else {
Johan Hovoldef310252013-04-16 18:01:29 +020077 dev_dbg(&port->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +010078 "Improper amount of data received from the device, "
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080079 "%d bytes", urb->actual_length);
80 }
81
82exit:
83 spin_lock(&priv->lock);
84
85 /* Continue trying to always read if we should */
86 if (!priv->throttled) {
Johan Hovoldcced9262013-04-16 18:01:28 +020087 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080088 if (result)
89 dev_err(&port->dev,
90 "%s - failed resubmitting read urb, error %d\n",
91 __func__, result);
92 } else
93 priv->actually_throttled = true;
94 spin_unlock(&priv->lock);
95}
96
Alan Coxa509a7e2009-09-19 13:13:26 -070097static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080098{
99 struct symbol_private *priv = usb_get_serial_data(port->serial);
100 unsigned long flags;
101 int result = 0;
102
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800103 spin_lock_irqsave(&priv->lock, flags);
104 priv->throttled = false;
105 priv->actually_throttled = false;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800106 spin_unlock_irqrestore(&priv->lock, flags);
107
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800108 /* Start reading from the device */
Johan Hovoldcced9262013-04-16 18:01:28 +0200109 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800110 if (result)
111 dev_err(&port->dev,
112 "%s - failed resubmitting read urb, error %d\n",
113 __func__, result);
114 return result;
115}
116
Alan Cox335f8512009-06-11 12:26:29 +0100117static void symbol_close(struct usb_serial_port *port)
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800118{
Johan Hovoldcced9262013-04-16 18:01:28 +0200119 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800120}
121
122static void symbol_throttle(struct tty_struct *tty)
123{
124 struct usb_serial_port *port = tty->driver_data;
125 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800126
Oliver Neukum63832512009-10-07 10:50:23 +0200127 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800128 priv->throttled = true;
Oliver Neukum63832512009-10-07 10:50:23 +0200129 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800130}
131
132static void symbol_unthrottle(struct tty_struct *tty)
133{
134 struct usb_serial_port *port = tty->driver_data;
135 struct symbol_private *priv = usb_get_serial_data(port->serial);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800136 int result;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200137 bool was_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800138
Oliver Neukum63832512009-10-07 10:50:23 +0200139 spin_lock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800140 priv->throttled = false;
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200141 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800142 priv->actually_throttled = false;
Oliver Neukum63832512009-10-07 10:50:23 +0200143 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800144
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200145 if (was_throttled) {
Johan Hovoldcced9262013-04-16 18:01:28 +0200146 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200147 if (result)
148 dev_err(&port->dev,
149 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800150 __func__, result);
Oliver Neukumb2a5cf12009-10-07 09:30:49 +0200151 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800152}
153
154static int symbol_startup(struct usb_serial *serial)
155{
156 struct symbol_private *priv;
Johan Hovoldcced9262013-04-16 18:01:28 +0200157
158 if (!serial->num_interrupt_in) {
159 dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
160 return -ENODEV;
161 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800162
163 /* create our private serial structure */
164 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
165 if (priv == NULL) {
166 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
167 return -ENOMEM;
168 }
169 spin_lock_init(&priv->lock);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800170
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800171 usb_set_serial_data(serial, priv);
172 return 0;
Alan Sternf9c99bb2009-06-02 11:53:55 -0400173}
174
175static void symbol_release(struct usb_serial *serial)
176{
177 struct symbol_private *priv = usb_get_serial_data(serial);
178
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800179 kfree(priv);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800180}
181
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800182static struct usb_serial_driver symbol_device = {
183 .driver = {
184 .owner = THIS_MODULE,
185 .name = "symbol",
186 },
187 .id_table = id_table,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800188 .num_ports = 1,
189 .attach = symbol_startup,
190 .open = symbol_open,
191 .close = symbol_close,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400192 .release = symbol_release,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800193 .throttle = symbol_throttle,
194 .unthrottle = symbol_unthrottle,
Johan Hovoldcced9262013-04-16 18:01:28 +0200195 .read_int_callback = symbol_int_callback,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800196};
197
Alan Sternd8603222012-02-23 14:57:25 -0500198static struct usb_serial_driver * const serial_drivers[] = {
199 &symbol_device, NULL
200};
201
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700202module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800203
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800204MODULE_LICENSE("GPL");