blob: 6b7d2779148c9dfb6311b98f3e13e88c2bc71ad3 [file] [log] [blame]
Aleksey Babahin43d186f2012-03-08 13:18:43 -08001/*
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -08002 Some of this code is credited to Linux USB open source files that are
3 distributed with Linux.
Aleksey Babahin43d186f2012-03-08 13:18:43 -08004
5 Copyright: 2007 Metrologic Instruments. All rights reserved.
6 Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
Aleksey Babahin43d186f2012-03-08 13:18:43 -08007*/
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/tty.h>
12#include <linux/module.h>
13#include <linux/usb.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/tty_driver.h>
17#include <linux/tty_flip.h>
18#include <linux/moduleparam.h>
19#include <linux/spinlock.h>
Aleksey Babahin43d186f2012-03-08 13:18:43 -080020#include <linux/errno.h>
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080021#include <linux/uaccess.h>
Aleksey Babahin43d186f2012-03-08 13:18:43 -080022#include <linux/usb/serial.h>
23
24/* Version Information */
25#define DRIVER_VERSION "v1.2.0.0"
26#define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
27
Greg Kroah-Hartman159d4d82012-03-08 13:42:41 -080028/* Product information. */
29#define FOCUS_VENDOR_ID 0x0C2E
30#define FOCUS_PRODUCT_ID 0x0720
31#define FOCUS_PRODUCT_ID_UNI 0x0710
32
33#define METROUSB_SET_REQUEST_TYPE 0x40
34#define METROUSB_SET_MODEM_CTRL_REQUEST 10
35#define METROUSB_SET_BREAK_REQUEST 0x40
36#define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */
37#define METROUSB_MCR_RTS 0x0a /* Activate RTS. */
38#define METROUSB_MCR_DTR 0x09 /* Activate DTR. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080039#define WDR_TIMEOUT 5000 /* default urb timeout. */
Greg Kroah-Hartman159d4d82012-03-08 13:42:41 -080040
41/* Private data structure. */
42struct metrousb_private {
43 spinlock_t lock;
44 int throttled;
45 unsigned long control_state;
46};
47
Aleksey Babahin43d186f2012-03-08 13:18:43 -080048/* Device table list. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080049static struct usb_device_id id_table[] = {
Aleksey Babahin43d186f2012-03-08 13:18:43 -080050 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID) },
51 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
Aleksey Babahin43d186f2012-03-08 13:18:43 -080052 { }, /* Terminating entry. */
53};
54MODULE_DEVICE_TABLE(usb, id_table);
55
56/* Input parameter constants. */
Greg Kroah-Hartmanfdac0f62012-03-08 13:37:32 -080057static bool debug;
Aleksey Babahin43d186f2012-03-08 13:18:43 -080058
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080059static void metrousb_read_int_callback(struct urb *urb)
Aleksey Babahin43d186f2012-03-08 13:18:43 -080060{
Greg Kroah-Hartman8111e4e2012-03-08 14:00:11 -080061 struct usb_serial_port *port = urb->context;
Aleksey Babahin43d186f2012-03-08 13:18:43 -080062 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
63 struct tty_struct *tty;
64 unsigned char *data = urb->transfer_buffer;
65 int throttled = 0;
66 int result = 0;
67 unsigned long flags = 0;
68
69 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
70
71 switch (urb->status) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080072 case 0:
73 /* Success status, read from the port. */
74 break;
75 case -ECONNRESET:
76 case -ENOENT:
77 case -ESHUTDOWN:
78 /* urb has been terminated. */
79 dbg("METRO-USB - %s - urb shutting down, port number=%d, error code=%d",
80 __FUNCTION__, port->number, result);
81 return;
82 default:
83 dbg("METRO-USB - %s - non-zero urb received, port number=%d, error code=%d",
84 __FUNCTION__, port->number, result);
85 goto exit;
Aleksey Babahin43d186f2012-03-08 13:18:43 -080086 }
87
88
89 /* Set the data read from the usb port into the serial port buffer. */
90 tty = tty_port_tty_get(&port->port);
91 if (!tty) {
92 dbg("%s - bad tty pointer - exiting", __func__);
93 return;
94 }
95
96 if (tty && urb->actual_length) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080097 /* Loop through the data copying each byte to the tty layer. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -080098 tty_insert_flip_string(tty, data, urb->actual_length);
99
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800100 /* Force the data to the tty layer. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800101 tty_flip_buffer_push(tty);
102 }
103 tty_kref_put(tty);
104
105 /* Set any port variables. */
106 spin_lock_irqsave(&metro_priv->lock, flags);
107 throttled = metro_priv->throttled;
108 spin_unlock_irqrestore(&metro_priv->lock, flags);
109
110 /* Continue trying to read if set. */
111 if (!throttled) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800112 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
113 usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
114 port->interrupt_in_urb->transfer_buffer,
115 port->interrupt_in_urb->transfer_buffer_length,
116 metrousb_read_int_callback, port, 1);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800117
118 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
119
120 if (result) {
121 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
122 __FUNCTION__, port->number, result);
123 }
124 }
125 return;
126
127exit:
128 /* Try to resubmit the urb. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800129 result = usb_submit_urb(urb, GFP_ATOMIC);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800130 if (result) {
131 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
132 __FUNCTION__, port->number, result);
133 }
134}
135
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800136static void metrousb_cleanup(struct usb_serial_port *port)
137{
138 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
139
140 if (port->serial->dev) {
141 /* Shutdown any interrupt in urbs. */
142 if (port->interrupt_in_urb) {
143 usb_unlink_urb(port->interrupt_in_urb);
144 usb_kill_urb(port->interrupt_in_urb);
145 }
146 }
147}
148
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800149static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
150{
151 struct usb_serial *serial = port->serial;
152 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
153 unsigned long flags = 0;
154 int result = 0;
155
156 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
157
158 /* Make sure the urb is initialized. */
159 if (!port->interrupt_in_urb) {
160 dbg("METRO-USB - %s - interrupt urb not initialized for port number=%d", __FUNCTION__, port->number);
161 return -ENODEV;
162 }
163
164 /* Set the private data information for the port. */
165 spin_lock_irqsave(&metro_priv->lock, flags);
166 metro_priv->control_state = 0;
167 metro_priv->throttled = 0;
168 spin_unlock_irqrestore(&metro_priv->lock, flags);
169
170 /*
171 * Force low_latency on so that our tty_push actually forces the data
172 * through, otherwise it is scheduled, and with high data rates (like
173 * with OHCI) data can get lost.
174 */
175 if (tty)
176 tty->low_latency = 1;
177
178 /* Clear the urb pipe. */
179 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
180
181 /* Start reading from the device */
182 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
183 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
184 port->interrupt_in_urb->transfer_buffer,
185 port->interrupt_in_urb->transfer_buffer_length,
186 metrousb_read_int_callback, port, 1);
187 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
188
189 if (result) {
190 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d"
191 , __FUNCTION__, port->number, result);
192 goto exit;
193 }
194
195 dbg("METRO-USB - %s - port open for port number=%d", __FUNCTION__, port->number);
196exit:
197 return result;
198}
199
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800200static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
201{
202 int retval = 0;
203 unsigned char mcr = METROUSB_MCR_NONE;
204
205 dbg("METRO-USB - %s - control state=%d", __FUNCTION__, control_state);
206
207 /* Set the modem control value. */
208 if (control_state & TIOCM_DTR)
209 mcr |= METROUSB_MCR_DTR;
210 if (control_state & TIOCM_RTS)
211 mcr |= METROUSB_MCR_RTS;
212
213 /* Send the command to the usb port. */
214 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
215 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
216 control_state, 0, NULL, 0, WDR_TIMEOUT);
217 if (retval < 0)
218 dbg("METRO-USB - %s - set modem ctrl=0x%x failed, error code=%d", __FUNCTION__, mcr, retval);
219
220 return retval;
221}
222
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800223static void metrousb_shutdown(struct usb_serial *serial)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800224{
225 int i = 0;
226
227 dbg("METRO-USB - %s", __FUNCTION__);
228
229 /* Stop reading and writing on all ports. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800230 for (i = 0; i < serial->num_ports; ++i) {
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800231 /* Close any open urbs. */
232 metrousb_cleanup(serial->port[i]);
233
234 /* Free memory. */
235 kfree(usb_get_serial_port_data(serial->port[i]));
236 usb_set_serial_port_data(serial->port[i], NULL);
237
238 dbg("METRO-USB - %s - freed port number=%d", __FUNCTION__, serial->port[i]->number);
239 }
240}
241
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800242static int metrousb_startup(struct usb_serial *serial)
243{
244 struct metrousb_private *metro_priv;
245 struct usb_serial_port *port;
246 int i = 0;
247
248 dbg("METRO-USB - %s", __FUNCTION__);
249
250 /* Loop through the serial ports setting up the private structures.
251 * Currently we only use one port. */
252 for (i = 0; i < serial->num_ports; ++i) {
253 port = serial->port[i];
254
255 /* Declare memory. */
Greg Kroah-Hartman8111e4e2012-03-08 14:00:11 -0800256 metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800257 if (!metro_priv)
258 return -ENOMEM;
259
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800260 /* Initialize memory. */
261 spin_lock_init(&metro_priv->lock);
262 usb_set_serial_port_data(port, metro_priv);
263
264 dbg("METRO-USB - %s - port number=%d.", __FUNCTION__, port->number);
265 }
266
267 return 0;
268}
269
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800270static void metrousb_throttle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800271{
272 struct usb_serial_port *port = tty->driver_data;
273 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
274 unsigned long flags = 0;
275
276 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
277
278 /* Set the private information for the port to stop reading data. */
279 spin_lock_irqsave(&metro_priv->lock, flags);
280 metro_priv->throttled = 1;
281 spin_unlock_irqrestore(&metro_priv->lock, flags);
282}
283
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800284static int metrousb_tiocmget(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800285{
286 unsigned long control_state = 0;
287 struct usb_serial_port *port = tty->driver_data;
288 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
289 unsigned long flags = 0;
290
291 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
292
293 spin_lock_irqsave(&metro_priv->lock, flags);
294 control_state = metro_priv->control_state;
295 spin_unlock_irqrestore(&metro_priv->lock, flags);
296
297 return control_state;
298}
299
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800300static int metrousb_tiocmset(struct tty_struct *tty,
301 unsigned int set, unsigned int clear)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800302{
303 struct usb_serial_port *port = tty->driver_data;
304 struct usb_serial *serial = port->serial;
305 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
306 unsigned long flags = 0;
307 unsigned long control_state = 0;
308
309 dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__, port->number, set, clear);
310
311 spin_lock_irqsave(&metro_priv->lock, flags);
312 control_state = metro_priv->control_state;
313
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800314 /* Set the RTS and DTR values. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800315 if (set & TIOCM_RTS)
316 control_state |= TIOCM_RTS;
317 if (set & TIOCM_DTR)
318 control_state |= TIOCM_DTR;
319 if (clear & TIOCM_RTS)
320 control_state &= ~TIOCM_RTS;
321 if (clear & TIOCM_DTR)
322 control_state &= ~TIOCM_DTR;
323
324 metro_priv->control_state = control_state;
325 spin_unlock_irqrestore(&metro_priv->lock, flags);
326 return metrousb_set_modem_ctrl(serial, control_state);
327}
328
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800329static void metrousb_unthrottle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800330{
331 struct usb_serial_port *port = tty->driver_data;
332 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
333 unsigned long flags = 0;
334 int result = 0;
335
336 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
337
338 /* Set the private information for the port to resume reading data. */
339 spin_lock_irqsave(&metro_priv->lock, flags);
340 metro_priv->throttled = 0;
341 spin_unlock_irqrestore(&metro_priv->lock, flags);
342
343 /* Submit the urb to read from the port. */
344 port->interrupt_in_urb->dev = port->serial->dev;
345 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
346 if (result) {
347 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
348 __FUNCTION__, port->number, result);
349 }
350}
351
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800352static struct usb_driver metrousb_driver = {
353 .name = "metro-usb",
354 .probe = usb_serial_probe,
355 .disconnect = usb_serial_disconnect,
356 .id_table = id_table
357};
358
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800359static struct usb_serial_driver metrousb_device = {
360 .driver = {
361 .owner = THIS_MODULE,
362 .name = "metro-usb",
363 },
364 .description = "Metrologic USB to serial converter.",
365 .id_table = id_table,
366 .num_ports = 1,
367 .open = metrousb_open,
368 .close = metrousb_cleanup,
369 .read_int_callback = metrousb_read_int_callback,
370 .attach = metrousb_startup,
371 .release = metrousb_shutdown,
372 .throttle = metrousb_throttle,
373 .unthrottle = metrousb_unthrottle,
374 .tiocmget = metrousb_tiocmget,
375 .tiocmset = metrousb_tiocmset,
376};
377
378static struct usb_serial_driver * const serial_drivers[] = {
379 &metrousb_device,
380 NULL,
381};
382
Greg Kroah-Hartman1935e352012-03-08 13:39:53 -0800383module_usb_serial_driver(metrousb_driver, serial_drivers);
384
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800385MODULE_LICENSE("GPL");
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800386MODULE_AUTHOR("Philip Nicastro");
387MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
388MODULE_DESCRIPTION(DRIVER_DESC);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800389
390/* Module input parameters */
391module_param(debug, bool, S_IRUGO | S_IWUSR);
392MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");