2 Some of this code is credited to Linux USB open source files that are
3 distributed with Linux.
5 Copyright: 2007 Metrologic Instruments. All rights reserved.
6 Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
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>
20 #include <linux/errno.h>
21 #include <linux/uaccess.h>
22 #include <linux/usb/serial.h>
24 /* Version Information */
25 #define DRIVER_VERSION "v1.2.0.0"
26 #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
28 /* Product information. */
29 #define FOCUS_VENDOR_ID 0x0C2E
30 #define FOCUS_PRODUCT_ID 0x0720
31 #define FOCUS_PRODUCT_ID_UNI 0x0710
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. */
39 #define WDR_TIMEOUT 5000 /* default urb timeout. */
41 /* Private data structure. */
42 struct metrousb_private {
45 unsigned long control_state;
48 /* Device table list. */
49 static struct usb_device_id id_table[] = {
50 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID) },
51 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
52 { }, /* Terminating entry. */
54 MODULE_DEVICE_TABLE(usb, id_table);
56 /* Input parameter constants. */
59 /* ----------------------------------------------------------------------------------------------
61 Read the port from the read interrupt.
64 struct urb *: urb structure to get data.
65 struct pt_regs *: pt_regs structure.
70 static void metrousb_read_int_callback(struct urb *urb)
72 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
73 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
74 struct tty_struct *tty;
75 unsigned char *data = urb->transfer_buffer;
78 unsigned long flags = 0;
80 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
82 switch (urb->status) {
84 /* Success status, read from the port. */
89 /* urb has been terminated. */
90 dbg("METRO-USB - %s - urb shutting down, port number=%d, error code=%d",
91 __FUNCTION__, port->number, result);
94 dbg("METRO-USB - %s - non-zero urb received, port number=%d, error code=%d",
95 __FUNCTION__, port->number, result);
100 /* Set the data read from the usb port into the serial port buffer. */
101 tty = tty_port_tty_get(&port->port);
103 dbg("%s - bad tty pointer - exiting", __func__);
107 if (tty && urb->actual_length) {
108 /* Loop through the data copying each byte to the tty layer. */
109 tty_insert_flip_string(tty, data, urb->actual_length);
111 /* Force the data to the tty layer. */
112 tty_flip_buffer_push(tty);
116 /* Set any port variables. */
117 spin_lock_irqsave(&metro_priv->lock, flags);
118 throttled = metro_priv->throttled;
119 spin_unlock_irqrestore(&metro_priv->lock, flags);
121 /* Continue trying to read if set. */
123 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
124 usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
125 port->interrupt_in_urb->transfer_buffer,
126 port->interrupt_in_urb->transfer_buffer_length,
127 metrousb_read_int_callback, port, 1);
129 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
132 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
133 __FUNCTION__, port->number, result);
139 /* Try to resubmit the urb. */
140 result = usb_submit_urb(urb, GFP_ATOMIC);
142 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
143 __FUNCTION__, port->number, result);
147 /* ----------------------------------------------------------------------------------------------
149 Clean up any urbs and port information.
152 struct usb_serial_port *: pointer to a usb_serial_port structure.
155 int: Returns true (0) if successful, false otherwise.
157 static void metrousb_cleanup(struct usb_serial_port *port)
159 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
161 if (port->serial->dev) {
162 /* Shutdown any interrupt in urbs. */
163 if (port->interrupt_in_urb) {
164 usb_unlink_urb(port->interrupt_in_urb);
165 usb_kill_urb(port->interrupt_in_urb);
170 /* ----------------------------------------------------------------------------------------------
172 Open the drivers serial port.
175 struct usb_serial_port *: pointer to a usb_serial_port structure.
176 struct file *: pointer to a file structure.
179 int: Returns true (0) if successful, false otherwise.
181 static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
183 struct usb_serial *serial = port->serial;
184 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
185 unsigned long flags = 0;
188 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
190 /* Make sure the urb is initialized. */
191 if (!port->interrupt_in_urb) {
192 dbg("METRO-USB - %s - interrupt urb not initialized for port number=%d", __FUNCTION__, port->number);
196 /* Set the private data information for the port. */
197 spin_lock_irqsave(&metro_priv->lock, flags);
198 metro_priv->control_state = 0;
199 metro_priv->throttled = 0;
200 spin_unlock_irqrestore(&metro_priv->lock, flags);
203 * Force low_latency on so that our tty_push actually forces the data
204 * through, otherwise it is scheduled, and with high data rates (like
205 * with OHCI) data can get lost.
208 tty->low_latency = 1;
210 /* Clear the urb pipe. */
211 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
213 /* Start reading from the device */
214 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
215 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
216 port->interrupt_in_urb->transfer_buffer,
217 port->interrupt_in_urb->transfer_buffer_length,
218 metrousb_read_int_callback, port, 1);
219 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
222 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d"
223 , __FUNCTION__, port->number, result);
227 dbg("METRO-USB - %s - port open for port number=%d", __FUNCTION__, port->number);
232 /* ----------------------------------------------------------------------------------------------
234 Set the modem control state for the entered serial port.
237 struct usb_serial_port *: pointer to a usb_serial_port structure.
238 unsigned int: control state value to set.
241 int: Returns true (0) if successful, false otherwise.
243 static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
246 unsigned char mcr = METROUSB_MCR_NONE;
248 dbg("METRO-USB - %s - control state=%d", __FUNCTION__, control_state);
250 /* Set the modem control value. */
251 if (control_state & TIOCM_DTR)
252 mcr |= METROUSB_MCR_DTR;
253 if (control_state & TIOCM_RTS)
254 mcr |= METROUSB_MCR_RTS;
256 /* Send the command to the usb port. */
257 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
258 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
259 control_state, 0, NULL, 0, WDR_TIMEOUT);
261 dbg("METRO-USB - %s - set modem ctrl=0x%x failed, error code=%d", __FUNCTION__, mcr, retval);
267 /* ----------------------------------------------------------------------------------------------
272 struct usb_serial *: pointer to a usb-serial structure.
275 int: Returns true (0) if successful, false otherwise.
277 static void metrousb_shutdown(struct usb_serial *serial)
281 dbg("METRO-USB - %s", __FUNCTION__);
283 /* Stop reading and writing on all ports. */
284 for (i = 0; i < serial->num_ports; ++i) {
285 /* Close any open urbs. */
286 metrousb_cleanup(serial->port[i]);
289 kfree(usb_get_serial_port_data(serial->port[i]));
290 usb_set_serial_port_data(serial->port[i], NULL);
292 dbg("METRO-USB - %s - freed port number=%d", __FUNCTION__, serial->port[i]->number);
296 /* ----------------------------------------------------------------------------------------------
301 struct usb_serial *: pointer to a usb-serial structure.
304 int: Returns true (0) if successful, false otherwise.
306 static int metrousb_startup(struct usb_serial *serial)
308 struct metrousb_private *metro_priv;
309 struct usb_serial_port *port;
312 dbg("METRO-USB - %s", __FUNCTION__);
314 /* Loop through the serial ports setting up the private structures.
315 * Currently we only use one port. */
316 for (i = 0; i < serial->num_ports; ++i) {
317 port = serial->port[i];
319 /* Declare memory. */
320 metro_priv = kmalloc(sizeof(struct metrousb_private), GFP_KERNEL);
325 memset(metro_priv, 0x00, sizeof(struct metrousb_private));
327 /* Initialize memory. */
328 spin_lock_init(&metro_priv->lock);
329 usb_set_serial_port_data(port, metro_priv);
331 dbg("METRO-USB - %s - port number=%d.", __FUNCTION__, port->number);
337 /* ----------------------------------------------------------------------------------------------
339 Set the serial port throttle to stop reading from the port.
342 struct usb_serial_port *: pointer to a usb_serial_port structure.
347 static void metrousb_throttle(struct tty_struct *tty)
349 struct usb_serial_port *port = tty->driver_data;
350 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
351 unsigned long flags = 0;
353 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
355 /* Set the private information for the port to stop reading data. */
356 spin_lock_irqsave(&metro_priv->lock, flags);
357 metro_priv->throttled = 1;
358 spin_unlock_irqrestore(&metro_priv->lock, flags);
361 /* ----------------------------------------------------------------------------------------------
363 Get the serial port control line states.
366 struct usb_serial_port *: pointer to a usb_serial_port structure.
367 struct file *: pointer to a file structure.
370 int: Returns the state of the control lines.
372 static int metrousb_tiocmget(struct tty_struct *tty)
374 unsigned long control_state = 0;
375 struct usb_serial_port *port = tty->driver_data;
376 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
377 unsigned long flags = 0;
379 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
381 spin_lock_irqsave(&metro_priv->lock, flags);
382 control_state = metro_priv->control_state;
383 spin_unlock_irqrestore(&metro_priv->lock, flags);
385 return control_state;
388 /* ----------------------------------------------------------------------------------------------
390 Set the serial port control line states.
393 struct usb_serial_port *: pointer to a usb_serial_port structure.
394 struct file *: pointer to a file structure.
395 unsigned int: line state to set.
396 unsigned int: line state to clear.
399 int: Returns the state of the control lines.
401 static int metrousb_tiocmset(struct tty_struct *tty,
402 unsigned int set, unsigned int clear)
404 struct usb_serial_port *port = tty->driver_data;
405 struct usb_serial *serial = port->serial;
406 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
407 unsigned long flags = 0;
408 unsigned long control_state = 0;
410 dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__, port->number, set, clear);
412 spin_lock_irqsave(&metro_priv->lock, flags);
413 control_state = metro_priv->control_state;
415 /* Set the RTS and DTR values. */
417 control_state |= TIOCM_RTS;
419 control_state |= TIOCM_DTR;
420 if (clear & TIOCM_RTS)
421 control_state &= ~TIOCM_RTS;
422 if (clear & TIOCM_DTR)
423 control_state &= ~TIOCM_DTR;
425 metro_priv->control_state = control_state;
426 spin_unlock_irqrestore(&metro_priv->lock, flags);
427 return metrousb_set_modem_ctrl(serial, control_state);
430 /* ----------------------------------------------------------------------------------------------
432 Set the serial port unthrottle to resume reading from the port.
435 struct usb_serial_port *: pointer to a usb_serial_port structure.
440 static void metrousb_unthrottle(struct tty_struct *tty)
442 struct usb_serial_port *port = tty->driver_data;
443 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
444 unsigned long flags = 0;
447 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
449 /* Set the private information for the port to resume reading data. */
450 spin_lock_irqsave(&metro_priv->lock, flags);
451 metro_priv->throttled = 0;
452 spin_unlock_irqrestore(&metro_priv->lock, flags);
454 /* Submit the urb to read from the port. */
455 port->interrupt_in_urb->dev = port->serial->dev;
456 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
458 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
459 __FUNCTION__, port->number, result);
463 /* Driver structure. */
464 static struct usb_driver metrousb_driver = {
466 .probe = usb_serial_probe,
467 .disconnect = usb_serial_disconnect,
471 /* Device structure. */
472 static struct usb_serial_driver metrousb_device = {
474 .owner = THIS_MODULE,
477 .description = "Metrologic USB to serial converter.",
478 .id_table = id_table,
480 .open = metrousb_open,
481 .close = metrousb_cleanup,
482 .read_int_callback = metrousb_read_int_callback,
483 .attach = metrousb_startup,
484 .release = metrousb_shutdown,
485 .throttle = metrousb_throttle,
486 .unthrottle = metrousb_unthrottle,
487 .tiocmget = metrousb_tiocmget,
488 .tiocmset = metrousb_tiocmset,
491 static struct usb_serial_driver * const serial_drivers[] = {
496 module_usb_serial_driver(metrousb_driver, serial_drivers);
498 MODULE_LICENSE("GPL");
499 MODULE_AUTHOR("Philip Nicastro");
500 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
501 MODULE_DESCRIPTION(DRIVER_DESC);
503 /* Module input parameters */
504 module_param(debug, bool, S_IRUGO | S_IWUSR);
505 MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");