blob: 9fa7dd413e8361ddef978763dee1f979916063d0 [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>
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -080014#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;
Johan Hovolda85796e2013-04-16 18:01:30 +020038 struct symbol_private *priv = usb_get_serial_port_data(port);
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{
Johan Hovoldcced9262013-04-16 18:01:28 +0200156 if (!serial->num_interrupt_in) {
157 dev_err(&serial->dev->dev, "no interrupt-in endpoint\n");
158 return -ENODEV;
159 }
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800160
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800161 return 0;
Alan Sternf9c99bb2009-06-02 11:53:55 -0400162}
163
Johan Hovolda85796e2013-04-16 18:01:30 +0200164static int symbol_port_probe(struct usb_serial_port *port)
Alan Sternf9c99bb2009-06-02 11:53:55 -0400165{
Johan Hovolda85796e2013-04-16 18:01:30 +0200166 struct symbol_private *priv;
167
168 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
169 if (!priv)
170 return -ENOMEM;
171
172 spin_lock_init(&priv->lock);
173
174 usb_set_serial_port_data(port, priv);
175
176 return 0;
177}
178
179static int symbol_port_remove(struct usb_serial_port *port)
180{
181 struct symbol_private *priv = usb_get_serial_port_data(port);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400182
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800183 kfree(priv);
Johan Hovolda85796e2013-04-16 18:01:30 +0200184
185 return 0;
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800186}
187
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800188static struct usb_serial_driver symbol_device = {
189 .driver = {
190 .owner = THIS_MODULE,
191 .name = "symbol",
192 },
193 .id_table = id_table,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800194 .num_ports = 1,
195 .attach = symbol_startup,
Johan Hovolda85796e2013-04-16 18:01:30 +0200196 .port_probe = symbol_port_probe,
197 .port_remove = symbol_port_remove,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800198 .open = symbol_open,
199 .close = symbol_close,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800200 .throttle = symbol_throttle,
201 .unthrottle = symbol_unthrottle,
Johan Hovoldcced9262013-04-16 18:01:28 +0200202 .read_int_callback = symbol_int_callback,
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800203};
204
Alan Sternd8603222012-02-23 14:57:25 -0500205static struct usb_serial_driver * const serial_drivers[] = {
206 &symbol_device, NULL
207};
208
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700209module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800210
Greg Kroah-Hartman68b44eae2009-02-13 17:25:46 -0800211MODULE_LICENSE("GPL");