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 /* Function prototypes. */
60 static void metrousb_cleanup(struct usb_serial_port
*port
);
61 static void metrousb_close(struct usb_serial_port
*port
);
62 static int metrousb_open(struct tty_struct
*tty
, struct usb_serial_port
*port
);
63 static void metrousb_read_int_callback(struct urb
*urb
);
64 static void metrousb_shutdown(struct usb_serial
*serial
);
65 static int metrousb_startup(struct usb_serial
*serial
);
66 static void metrousb_throttle(struct tty_struct
*tty
);
67 static int metrousb_tiocmget(struct tty_struct
*tty
);
68 static int metrousb_tiocmset(struct tty_struct
*tty
, unsigned int set
, unsigned int clear
);
69 static void metrousb_unthrottle(struct tty_struct
*tty
);
71 /* Driver structure. */
72 static struct usb_driver metrousb_driver
= {
74 .probe
= usb_serial_probe
,
75 .disconnect
= usb_serial_disconnect
,
79 /* Device structure. */
80 static struct usb_serial_driver metrousb_device
= {
85 .description
= "Metrologic USB to serial converter.",
88 .open
= metrousb_open
,
89 .close
= metrousb_close
,
90 .read_int_callback
= metrousb_read_int_callback
,
91 .attach
= metrousb_startup
,
92 .release
= metrousb_shutdown
,
93 .throttle
= metrousb_throttle
,
94 .unthrottle
= metrousb_unthrottle
,
95 .tiocmget
= metrousb_tiocmget
,
96 .tiocmset
= metrousb_tiocmset
,
99 static struct usb_serial_driver
* const serial_drivers
[] = {
104 /* ----------------------------------------------------------------------------------------------
106 Clean up any urbs and port information.
109 struct usb_serial_port *: pointer to a usb_serial_port structure.
112 int: Returns true (0) if successful, false otherwise.
114 static void metrousb_cleanup(struct usb_serial_port
*port
)
116 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
118 if (port
->serial
->dev
) {
119 /* Shutdown any interrupt in urbs. */
120 if (port
->interrupt_in_urb
) {
121 usb_unlink_urb(port
->interrupt_in_urb
);
122 usb_kill_urb(port
->interrupt_in_urb
);
127 /* ----------------------------------------------------------------------------------------------
129 Close the open serial port. Cleanup any open serial port information.
132 struct usb_serial_port *: pointer to a usb_serial_port structure.
133 struct file *: pointer to a file structure.
136 int: Returns true (0) if successful, false otherwise.
138 static void metrousb_close(struct usb_serial_port
*port
)
140 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
141 metrousb_cleanup(port
);
144 /* ----------------------------------------------------------------------------------------------
146 Open the drivers serial port.
149 struct usb_serial_port *: pointer to a usb_serial_port structure.
150 struct file *: pointer to a file structure.
153 int: Returns true (0) if successful, false otherwise.
155 static int metrousb_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
157 struct usb_serial
*serial
= port
->serial
;
158 struct metrousb_private
*metro_priv
= usb_get_serial_port_data(port
);
159 unsigned long flags
= 0;
162 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
164 /* Make sure the urb is initialized. */
165 if (!port
->interrupt_in_urb
) {
166 dbg("METRO-USB - %s - interrupt urb not initialized for port number=%d", __FUNCTION__
, port
->number
);
170 /* Set the private data information for the port. */
171 spin_lock_irqsave(&metro_priv
->lock
, flags
);
172 metro_priv
->control_state
= 0;
173 metro_priv
->throttled
= 0;
174 spin_unlock_irqrestore(&metro_priv
->lock
, flags
);
177 * Force low_latency on so that our tty_push actually forces the data
178 * through, otherwise it is scheduled, and with high data rates (like
179 * with OHCI) data can get lost.
182 tty
->low_latency
= 1;
184 /* Clear the urb pipe. */
185 usb_clear_halt(serial
->dev
, port
->interrupt_in_urb
->pipe
);
187 /* Start reading from the device */
188 usb_fill_int_urb(port
->interrupt_in_urb
, serial
->dev
,
189 usb_rcvintpipe(serial
->dev
, port
->interrupt_in_endpointAddress
),
190 port
->interrupt_in_urb
->transfer_buffer
,
191 port
->interrupt_in_urb
->transfer_buffer_length
,
192 metrousb_read_int_callback
, port
, 1);
193 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
196 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d"
197 , __FUNCTION__
, port
->number
, result
);
201 dbg("METRO-USB - %s - port open for port number=%d", __FUNCTION__
, port
->number
);
206 /* ----------------------------------------------------------------------------------------------
208 Read the port from the read interrupt.
211 struct urb *: urb structure to get data.
212 struct pt_regs *: pt_regs structure.
217 static void metrousb_read_int_callback(struct urb
*urb
)
219 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
220 struct metrousb_private
*metro_priv
= usb_get_serial_port_data(port
);
221 struct tty_struct
*tty
;
222 unsigned char *data
= urb
->transfer_buffer
;
225 unsigned long flags
= 0;
227 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
229 switch (urb
->status
) {
231 /* Success status, read from the port. */
236 /* urb has been terminated. */
237 dbg("METRO-USB - %s - urb shutting down, port number=%d, error code=%d",
238 __FUNCTION__
, port
->number
, result
);
241 dbg("METRO-USB - %s - non-zero urb received, port number=%d, error code=%d",
242 __FUNCTION__
, port
->number
, result
);
247 /* Set the data read from the usb port into the serial port buffer. */
248 tty
= tty_port_tty_get(&port
->port
);
250 dbg("%s - bad tty pointer - exiting", __func__
);
254 if (tty
&& urb
->actual_length
) {
255 /* Loop through the data copying each byte to the tty layer. */
256 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
258 /* Force the data to the tty layer. */
259 tty_flip_buffer_push(tty
);
263 /* Set any port variables. */
264 spin_lock_irqsave(&metro_priv
->lock
, flags
);
265 throttled
= metro_priv
->throttled
;
266 spin_unlock_irqrestore(&metro_priv
->lock
, flags
);
268 /* Continue trying to read if set. */
270 usb_fill_int_urb(port
->interrupt_in_urb
, port
->serial
->dev
,
271 usb_rcvintpipe(port
->serial
->dev
, port
->interrupt_in_endpointAddress
),
272 port
->interrupt_in_urb
->transfer_buffer
,
273 port
->interrupt_in_urb
->transfer_buffer_length
,
274 metrousb_read_int_callback
, port
, 1);
276 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
279 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
280 __FUNCTION__
, port
->number
, result
);
286 /* Try to resubmit the urb. */
287 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
289 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
290 __FUNCTION__
, port
->number
, result
);
294 /* ----------------------------------------------------------------------------------------------
296 Set the modem control state for the entered serial port.
299 struct usb_serial_port *: pointer to a usb_serial_port structure.
300 unsigned int: control state value to set.
303 int: Returns true (0) if successful, false otherwise.
305 static int metrousb_set_modem_ctrl(struct usb_serial
*serial
, unsigned int control_state
)
308 unsigned char mcr
= METROUSB_MCR_NONE
;
310 dbg("METRO-USB - %s - control state=%d", __FUNCTION__
, control_state
);
312 /* Set the modem control value. */
313 if (control_state
& TIOCM_DTR
)
314 mcr
|= METROUSB_MCR_DTR
;
315 if (control_state
& TIOCM_RTS
)
316 mcr
|= METROUSB_MCR_RTS
;
318 /* Send the command to the usb port. */
319 retval
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
320 METROUSB_SET_REQUEST_TYPE
, METROUSB_SET_MODEM_CTRL_REQUEST
,
321 control_state
, 0, NULL
, 0, WDR_TIMEOUT
);
323 dbg("METRO-USB - %s - set modem ctrl=0x%x failed, error code=%d", __FUNCTION__
, mcr
, retval
);
329 /* ----------------------------------------------------------------------------------------------
334 struct usb_serial *: pointer to a usb-serial structure.
337 int: Returns true (0) if successful, false otherwise.
339 static void metrousb_shutdown(struct usb_serial
*serial
)
343 dbg("METRO-USB - %s", __FUNCTION__
);
345 /* Stop reading and writing on all ports. */
346 for (i
= 0; i
< serial
->num_ports
; ++i
) {
347 /* Close any open urbs. */
348 metrousb_cleanup(serial
->port
[i
]);
351 kfree(usb_get_serial_port_data(serial
->port
[i
]));
352 usb_set_serial_port_data(serial
->port
[i
], NULL
);
354 dbg("METRO-USB - %s - freed port number=%d", __FUNCTION__
, serial
->port
[i
]->number
);
358 /* ----------------------------------------------------------------------------------------------
363 struct usb_serial *: pointer to a usb-serial structure.
366 int: Returns true (0) if successful, false otherwise.
368 static int metrousb_startup(struct usb_serial
*serial
)
370 struct metrousb_private
*metro_priv
;
371 struct usb_serial_port
*port
;
374 dbg("METRO-USB - %s", __FUNCTION__
);
376 /* Loop through the serial ports setting up the private structures.
377 * Currently we only use one port. */
378 for (i
= 0; i
< serial
->num_ports
; ++i
) {
379 port
= serial
->port
[i
];
381 /* Declare memory. */
382 metro_priv
= kmalloc(sizeof(struct metrousb_private
), GFP_KERNEL
);
387 memset(metro_priv
, 0x00, sizeof(struct metrousb_private
));
389 /* Initialize memory. */
390 spin_lock_init(&metro_priv
->lock
);
391 usb_set_serial_port_data(port
, metro_priv
);
393 dbg("METRO-USB - %s - port number=%d.", __FUNCTION__
, port
->number
);
399 /* ----------------------------------------------------------------------------------------------
401 Set the serial port throttle to stop reading from the port.
404 struct usb_serial_port *: pointer to a usb_serial_port structure.
409 static void metrousb_throttle(struct tty_struct
*tty
)
411 struct usb_serial_port
*port
= tty
->driver_data
;
412 struct metrousb_private
*metro_priv
= usb_get_serial_port_data(port
);
413 unsigned long flags
= 0;
415 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
417 /* Set the private information for the port to stop reading data. */
418 spin_lock_irqsave(&metro_priv
->lock
, flags
);
419 metro_priv
->throttled
= 1;
420 spin_unlock_irqrestore(&metro_priv
->lock
, flags
);
423 /* ----------------------------------------------------------------------------------------------
425 Get the serial port control line states.
428 struct usb_serial_port *: pointer to a usb_serial_port structure.
429 struct file *: pointer to a file structure.
432 int: Returns the state of the control lines.
434 static int metrousb_tiocmget(struct tty_struct
*tty
)
436 unsigned long control_state
= 0;
437 struct usb_serial_port
*port
= tty
->driver_data
;
438 struct metrousb_private
*metro_priv
= usb_get_serial_port_data(port
);
439 unsigned long flags
= 0;
441 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
443 spin_lock_irqsave(&metro_priv
->lock
, flags
);
444 control_state
= metro_priv
->control_state
;
445 spin_unlock_irqrestore(&metro_priv
->lock
, flags
);
447 return control_state
;
450 /* ----------------------------------------------------------------------------------------------
452 Set the serial port control line states.
455 struct usb_serial_port *: pointer to a usb_serial_port structure.
456 struct file *: pointer to a file structure.
457 unsigned int: line state to set.
458 unsigned int: line state to clear.
461 int: Returns the state of the control lines.
463 static int metrousb_tiocmset(struct tty_struct
*tty
,
464 unsigned int set
, unsigned int clear
)
466 struct usb_serial_port
*port
= tty
->driver_data
;
467 struct usb_serial
*serial
= port
->serial
;
468 struct metrousb_private
*metro_priv
= usb_get_serial_port_data(port
);
469 unsigned long flags
= 0;
470 unsigned long control_state
= 0;
472 dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__
, port
->number
, set
, clear
);
474 spin_lock_irqsave(&metro_priv
->lock
, flags
);
475 control_state
= metro_priv
->control_state
;
477 /* Set the RTS and DTR values. */
479 control_state
|= TIOCM_RTS
;
481 control_state
|= TIOCM_DTR
;
482 if (clear
& TIOCM_RTS
)
483 control_state
&= ~TIOCM_RTS
;
484 if (clear
& TIOCM_DTR
)
485 control_state
&= ~TIOCM_DTR
;
487 metro_priv
->control_state
= control_state
;
488 spin_unlock_irqrestore(&metro_priv
->lock
, flags
);
489 return metrousb_set_modem_ctrl(serial
, control_state
);
492 /* ----------------------------------------------------------------------------------------------
494 Set the serial port unthrottle to resume reading from the port.
497 struct usb_serial_port *: pointer to a usb_serial_port structure.
502 static void metrousb_unthrottle(struct tty_struct
*tty
)
504 struct usb_serial_port
*port
= tty
->driver_data
;
505 struct metrousb_private
*metro_priv
= usb_get_serial_port_data(port
);
506 unsigned long flags
= 0;
509 dbg("METRO-USB - %s - port number=%d", __FUNCTION__
, port
->number
);
511 /* Set the private information for the port to resume reading data. */
512 spin_lock_irqsave(&metro_priv
->lock
, flags
);
513 metro_priv
->throttled
= 0;
514 spin_unlock_irqrestore(&metro_priv
->lock
, flags
);
516 /* Submit the urb to read from the port. */
517 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
518 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
520 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
521 __FUNCTION__
, port
->number
, result
);
525 module_usb_serial_driver(metrousb_driver
, serial_drivers
);
527 MODULE_LICENSE("GPL");
528 MODULE_AUTHOR("Philip Nicastro");
529 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
530 MODULE_DESCRIPTION(DRIVER_DESC
);
532 /* Module input parameters */
533 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
534 MODULE_PARM_DESC(debug
, "Print debug info (bool 1=on, 0=off)");