blob: e968d3396813cd09d4744b6154685d894c2f28ee [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{
61 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
62 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-Hartmand4cbd6e2012-03-08 13:50:54 -0800256 metro_priv = kmalloc(sizeof(struct metrousb_private), GFP_KERNEL);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800257 if (!metro_priv)
258 return -ENOMEM;
259
260 /* Clear memory. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800261 memset(metro_priv, 0x00, sizeof(struct metrousb_private));
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800262
263 /* Initialize memory. */
264 spin_lock_init(&metro_priv->lock);
265 usb_set_serial_port_data(port, metro_priv);
266
267 dbg("METRO-USB - %s - port number=%d.", __FUNCTION__, port->number);
268 }
269
270 return 0;
271}
272
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800273static void metrousb_throttle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800274{
275 struct usb_serial_port *port = tty->driver_data;
276 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
277 unsigned long flags = 0;
278
279 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
280
281 /* Set the private information for the port to stop reading data. */
282 spin_lock_irqsave(&metro_priv->lock, flags);
283 metro_priv->throttled = 1;
284 spin_unlock_irqrestore(&metro_priv->lock, flags);
285}
286
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800287static int metrousb_tiocmget(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800288{
289 unsigned long control_state = 0;
290 struct usb_serial_port *port = tty->driver_data;
291 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
292 unsigned long flags = 0;
293
294 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
295
296 spin_lock_irqsave(&metro_priv->lock, flags);
297 control_state = metro_priv->control_state;
298 spin_unlock_irqrestore(&metro_priv->lock, flags);
299
300 return control_state;
301}
302
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800303static int metrousb_tiocmset(struct tty_struct *tty,
304 unsigned int set, unsigned int clear)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800305{
306 struct usb_serial_port *port = tty->driver_data;
307 struct usb_serial *serial = port->serial;
308 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
309 unsigned long flags = 0;
310 unsigned long control_state = 0;
311
312 dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__, port->number, set, clear);
313
314 spin_lock_irqsave(&metro_priv->lock, flags);
315 control_state = metro_priv->control_state;
316
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800317 /* Set the RTS and DTR values. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800318 if (set & TIOCM_RTS)
319 control_state |= TIOCM_RTS;
320 if (set & TIOCM_DTR)
321 control_state |= TIOCM_DTR;
322 if (clear & TIOCM_RTS)
323 control_state &= ~TIOCM_RTS;
324 if (clear & TIOCM_DTR)
325 control_state &= ~TIOCM_DTR;
326
327 metro_priv->control_state = control_state;
328 spin_unlock_irqrestore(&metro_priv->lock, flags);
329 return metrousb_set_modem_ctrl(serial, control_state);
330}
331
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800332static void metrousb_unthrottle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800333{
334 struct usb_serial_port *port = tty->driver_data;
335 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
336 unsigned long flags = 0;
337 int result = 0;
338
339 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
340
341 /* Set the private information for the port to resume reading data. */
342 spin_lock_irqsave(&metro_priv->lock, flags);
343 metro_priv->throttled = 0;
344 spin_unlock_irqrestore(&metro_priv->lock, flags);
345
346 /* Submit the urb to read from the port. */
347 port->interrupt_in_urb->dev = port->serial->dev;
348 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
349 if (result) {
350 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
351 __FUNCTION__, port->number, result);
352 }
353}
354
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800355static struct usb_driver metrousb_driver = {
356 .name = "metro-usb",
357 .probe = usb_serial_probe,
358 .disconnect = usb_serial_disconnect,
359 .id_table = id_table
360};
361
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800362static struct usb_serial_driver metrousb_device = {
363 .driver = {
364 .owner = THIS_MODULE,
365 .name = "metro-usb",
366 },
367 .description = "Metrologic USB to serial converter.",
368 .id_table = id_table,
369 .num_ports = 1,
370 .open = metrousb_open,
371 .close = metrousb_cleanup,
372 .read_int_callback = metrousb_read_int_callback,
373 .attach = metrousb_startup,
374 .release = metrousb_shutdown,
375 .throttle = metrousb_throttle,
376 .unthrottle = metrousb_unthrottle,
377 .tiocmget = metrousb_tiocmget,
378 .tiocmset = metrousb_tiocmset,
379};
380
381static struct usb_serial_driver * const serial_drivers[] = {
382 &metrousb_device,
383 NULL,
384};
385
Greg Kroah-Hartman1935e352012-03-08 13:39:53 -0800386module_usb_serial_driver(metrousb_driver, serial_drivers);
387
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800388MODULE_LICENSE("GPL");
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800389MODULE_AUTHOR("Philip Nicastro");
390MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
391MODULE_DESCRIPTION(DRIVER_DESC);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800392
393/* Module input parameters */
394module_param(debug, bool, S_IRUGO | S_IWUSR);
395MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");