blob: 7c14671d6bd933d2413d83009b6897206fe40ada [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
Aleksey Babahin3a450852012-03-20 00:46:31 +040030#define FOCUS_PRODUCT_ID_BI 0x0720
31#define FOCUS_PRODUCT_ID_UNI 0x0700
Greg Kroah-Hartman159d4d82012-03-08 13:42:41 -080032
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 Babahin3a450852012-03-20 00:46:31 +040050 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
Aleksey Babahin43d186f2012-03-08 13:18:43 -080051 { 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
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -080069 dev_dbg(&port->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -080070
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. */
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -080079 dev_dbg(&port->dev,
80 "%s - urb shutting down, error code=%d\n",
81 __func__, result);
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080082 return;
83 default:
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -080084 dev_dbg(&port->dev,
85 "%s - non-zero urb received, error code=%d\n",
86 __func__, result);
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080087 goto exit;
Aleksey Babahin43d186f2012-03-08 13:18:43 -080088 }
89
90
91 /* Set the data read from the usb port into the serial port buffer. */
92 tty = tty_port_tty_get(&port->port);
93 if (!tty) {
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -080094 dev_dbg(&port->dev, "%s - bad tty pointer - exiting\n",
95 __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -080096 return;
97 }
98
99 if (tty && urb->actual_length) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800100 /* Loop through the data copying each byte to the tty layer. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800101 tty_insert_flip_string(tty, data, urb->actual_length);
102
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800103 /* Force the data to the tty layer. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800104 tty_flip_buffer_push(tty);
105 }
106 tty_kref_put(tty);
107
108 /* Set any port variables. */
109 spin_lock_irqsave(&metro_priv->lock, flags);
110 throttled = metro_priv->throttled;
111 spin_unlock_irqrestore(&metro_priv->lock, flags);
112
113 /* Continue trying to read if set. */
114 if (!throttled) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800115 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
116 usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
117 port->interrupt_in_urb->transfer_buffer,
118 port->interrupt_in_urb->transfer_buffer_length,
119 metrousb_read_int_callback, port, 1);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800120
121 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
122
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800123 if (result)
124 dev_dbg(&port->dev,
125 "%s - failed submitting interrupt in urb, error code=%d\n",
126 __func__, result);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800127 }
128 return;
129
130exit:
131 /* Try to resubmit the urb. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800132 result = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800133 if (result)
134 dev_dbg(&port->dev,
135 "%s - failed submitting interrupt in urb, error code=%d\n",
136 __func__, result);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800137}
138
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800139static void metrousb_cleanup(struct usb_serial_port *port)
140{
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800141 dev_dbg(&port->dev, "%s\n", __func__);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800142
143 if (port->serial->dev) {
144 /* Shutdown any interrupt in urbs. */
145 if (port->interrupt_in_urb) {
146 usb_unlink_urb(port->interrupt_in_urb);
147 usb_kill_urb(port->interrupt_in_urb);
148 }
149 }
150}
151
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800152static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
153{
154 struct usb_serial *serial = port->serial;
155 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
156 unsigned long flags = 0;
157 int result = 0;
158
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800159 dev_dbg(&port->dev, "%s\n", __func__);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800160
161 /* Make sure the urb is initialized. */
162 if (!port->interrupt_in_urb) {
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800163 dev_dbg(&port->dev, "%s - interrupt urb not initialized\n",
164 __func__);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800165 return -ENODEV;
166 }
167
168 /* Set the private data information for the port. */
169 spin_lock_irqsave(&metro_priv->lock, flags);
170 metro_priv->control_state = 0;
171 metro_priv->throttled = 0;
172 spin_unlock_irqrestore(&metro_priv->lock, flags);
173
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800174 /* Clear the urb pipe. */
175 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
176
177 /* Start reading from the device */
178 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
179 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
180 port->interrupt_in_urb->transfer_buffer,
181 port->interrupt_in_urb->transfer_buffer_length,
182 metrousb_read_int_callback, port, 1);
183 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
184
185 if (result) {
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800186 dev_dbg(&port->dev,
187 "%s - failed submitting interrupt in urb, error code=%d\n",
188 __func__, result);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800189 goto exit;
190 }
191
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800192 dev_dbg(&port->dev, "%s - port open\n", __func__);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800193exit:
194 return result;
195}
196
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800197static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
198{
199 int retval = 0;
200 unsigned char mcr = METROUSB_MCR_NONE;
201
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800202 dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
203 __func__, control_state);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800204
205 /* Set the modem control value. */
206 if (control_state & TIOCM_DTR)
207 mcr |= METROUSB_MCR_DTR;
208 if (control_state & TIOCM_RTS)
209 mcr |= METROUSB_MCR_RTS;
210
211 /* Send the command to the usb port. */
212 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
213 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
214 control_state, 0, NULL, 0, WDR_TIMEOUT);
215 if (retval < 0)
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800216 dev_dbg(&serial->dev->dev,
217 "%s - set modem ctrl=0x%x failed, error code=%d\n",
218 __func__, mcr, retval);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800219
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
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800227 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800228
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
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800238 dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
239 __func__, serial->port[i]->number);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800240 }
241}
242
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800243static int metrousb_startup(struct usb_serial *serial)
244{
245 struct metrousb_private *metro_priv;
246 struct usb_serial_port *port;
247 int i = 0;
248
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800249 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800250
251 /* Loop through the serial ports setting up the private structures.
252 * Currently we only use one port. */
253 for (i = 0; i < serial->num_ports; ++i) {
254 port = serial->port[i];
255
256 /* Declare memory. */
Greg Kroah-Hartman8111e4e2012-03-08 14:00:11 -0800257 metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800258 if (!metro_priv)
259 return -ENOMEM;
260
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800261 /* Initialize memory. */
262 spin_lock_init(&metro_priv->lock);
263 usb_set_serial_port_data(port, metro_priv);
264
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800265 dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
266 __func__, port->number);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800267 }
268
269 return 0;
270}
271
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800272static void metrousb_throttle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800273{
274 struct usb_serial_port *port = tty->driver_data;
275 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
276 unsigned long flags = 0;
277
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800278 dev_dbg(tty->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800279
280 /* Set the private information for the port to stop reading data. */
281 spin_lock_irqsave(&metro_priv->lock, flags);
282 metro_priv->throttled = 1;
283 spin_unlock_irqrestore(&metro_priv->lock, flags);
284}
285
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800286static int metrousb_tiocmget(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800287{
288 unsigned long control_state = 0;
289 struct usb_serial_port *port = tty->driver_data;
290 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
291 unsigned long flags = 0;
292
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800293 dev_dbg(tty->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800294
295 spin_lock_irqsave(&metro_priv->lock, flags);
296 control_state = metro_priv->control_state;
297 spin_unlock_irqrestore(&metro_priv->lock, flags);
298
299 return control_state;
300}
301
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800302static int metrousb_tiocmset(struct tty_struct *tty,
303 unsigned int set, unsigned int clear)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800304{
305 struct usb_serial_port *port = tty->driver_data;
306 struct usb_serial *serial = port->serial;
307 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
308 unsigned long flags = 0;
309 unsigned long control_state = 0;
310
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800311 dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800312
313 spin_lock_irqsave(&metro_priv->lock, flags);
314 control_state = metro_priv->control_state;
315
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800316 /* Set the RTS and DTR values. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800317 if (set & TIOCM_RTS)
318 control_state |= TIOCM_RTS;
319 if (set & TIOCM_DTR)
320 control_state |= TIOCM_DTR;
321 if (clear & TIOCM_RTS)
322 control_state &= ~TIOCM_RTS;
323 if (clear & TIOCM_DTR)
324 control_state &= ~TIOCM_DTR;
325
326 metro_priv->control_state = control_state;
327 spin_unlock_irqrestore(&metro_priv->lock, flags);
328 return metrousb_set_modem_ctrl(serial, control_state);
329}
330
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800331static void metrousb_unthrottle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800332{
333 struct usb_serial_port *port = tty->driver_data;
334 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
335 unsigned long flags = 0;
336 int result = 0;
337
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800338 dev_dbg(tty->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800339
340 /* Set the private information for the port to resume reading data. */
341 spin_lock_irqsave(&metro_priv->lock, flags);
342 metro_priv->throttled = 0;
343 spin_unlock_irqrestore(&metro_priv->lock, flags);
344
345 /* Submit the urb to read from the port. */
346 port->interrupt_in_urb->dev = port->serial->dev;
347 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800348 if (result)
349 dev_dbg(tty->dev,
350 "failed submitting interrupt in urb error code=%d\n",
351 result);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800352}
353
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800354static struct usb_driver metrousb_driver = {
355 .name = "metro-usb",
356 .probe = usb_serial_probe,
357 .disconnect = usb_serial_disconnect,
358 .id_table = id_table
359};
360
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800361static struct usb_serial_driver metrousb_device = {
362 .driver = {
363 .owner = THIS_MODULE,
364 .name = "metro-usb",
365 },
366 .description = "Metrologic USB to serial converter.",
367 .id_table = id_table,
368 .num_ports = 1,
369 .open = metrousb_open,
370 .close = metrousb_cleanup,
371 .read_int_callback = metrousb_read_int_callback,
372 .attach = metrousb_startup,
373 .release = metrousb_shutdown,
374 .throttle = metrousb_throttle,
375 .unthrottle = metrousb_unthrottle,
376 .tiocmget = metrousb_tiocmget,
377 .tiocmset = metrousb_tiocmset,
378};
379
380static struct usb_serial_driver * const serial_drivers[] = {
381 &metrousb_device,
382 NULL,
383};
384
Greg Kroah-Hartman1935e352012-03-08 13:39:53 -0800385module_usb_serial_driver(metrousb_driver, serial_drivers);
386
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800387MODULE_LICENSE("GPL");
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800388MODULE_AUTHOR("Philip Nicastro");
389MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
390MODULE_DESCRIPTION(DRIVER_DESC);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800391
392/* Module input parameters */
393module_param(debug, bool, S_IRUGO | S_IWUSR);
394MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");