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_BI 0x0720
31 #define FOCUS_PRODUCT_ID_UNI 0x0700
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_BI) },
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 static void metrousb_read_int_callback(struct urb *urb)
61 struct usb_serial_port *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;
67 unsigned long flags = 0;
69 dev_dbg(&port->dev, "%s\n", __func__);
71 switch (urb->status) {
73 /* Success status, read from the port. */
78 /* urb has been terminated. */
80 "%s - urb shutting down, error code=%d\n",
85 "%s - non-zero urb received, error code=%d\n",
91 /* Set the data read from the usb port into the serial port buffer. */
92 tty = tty_port_tty_get(&port->port);
94 dev_dbg(&port->dev, "%s - bad tty pointer - exiting\n",
99 if (tty && urb->actual_length) {
100 /* Loop through the data copying each byte to the tty layer. */
101 tty_insert_flip_string(tty, data, urb->actual_length);
103 /* Force the data to the tty layer. */
104 tty_flip_buffer_push(tty);
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);
113 /* Continue trying to read if set. */
115 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);
121 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
125 "%s - failed submitting interrupt in urb, error code=%d\n",
131 /* Try to resubmit the urb. */
132 result = usb_submit_urb(urb, GFP_ATOMIC);
135 "%s - failed submitting interrupt in urb, error code=%d\n",
139 static void metrousb_cleanup(struct usb_serial_port *port)
141 dev_dbg(&port->dev, "%s\n", __func__);
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);
152 static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
154 struct usb_serial *serial = port->serial;
155 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
156 unsigned long flags = 0;
159 dev_dbg(&port->dev, "%s\n", __func__);
161 /* Make sure the urb is initialized. */
162 if (!port->interrupt_in_urb) {
163 dev_dbg(&port->dev, "%s - interrupt urb not initialized\n",
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);
175 * Force low_latency on so that our tty_push actually forces the data
176 * through, otherwise it is scheduled, and with high data rates (like
177 * with OHCI) data can get lost.
180 tty->low_latency = 1;
182 /* Clear the urb pipe. */
183 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
185 /* Start reading from the device */
186 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
187 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
188 port->interrupt_in_urb->transfer_buffer,
189 port->interrupt_in_urb->transfer_buffer_length,
190 metrousb_read_int_callback, port, 1);
191 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
195 "%s - failed submitting interrupt in urb, error code=%d\n",
200 dev_dbg(&port->dev, "%s - port open\n", __func__);
205 static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
208 unsigned char mcr = METROUSB_MCR_NONE;
210 dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
211 __func__, control_state);
213 /* Set the modem control value. */
214 if (control_state & TIOCM_DTR)
215 mcr |= METROUSB_MCR_DTR;
216 if (control_state & TIOCM_RTS)
217 mcr |= METROUSB_MCR_RTS;
219 /* Send the command to the usb port. */
220 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
221 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
222 control_state, 0, NULL, 0, WDR_TIMEOUT);
224 dev_dbg(&serial->dev->dev,
225 "%s - set modem ctrl=0x%x failed, error code=%d\n",
226 __func__, mcr, retval);
231 static void metrousb_shutdown(struct usb_serial *serial)
235 dev_dbg(&serial->dev->dev, "%s\n", __func__);
237 /* Stop reading and writing on all ports. */
238 for (i = 0; i < serial->num_ports; ++i) {
239 /* Close any open urbs. */
240 metrousb_cleanup(serial->port[i]);
243 kfree(usb_get_serial_port_data(serial->port[i]));
244 usb_set_serial_port_data(serial->port[i], NULL);
246 dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
247 __func__, serial->port[i]->number);
251 static int metrousb_startup(struct usb_serial *serial)
253 struct metrousb_private *metro_priv;
254 struct usb_serial_port *port;
257 dev_dbg(&serial->dev->dev, "%s\n", __func__);
259 /* Loop through the serial ports setting up the private structures.
260 * Currently we only use one port. */
261 for (i = 0; i < serial->num_ports; ++i) {
262 port = serial->port[i];
264 /* Declare memory. */
265 metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
269 /* Initialize memory. */
270 spin_lock_init(&metro_priv->lock);
271 usb_set_serial_port_data(port, metro_priv);
273 dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
274 __func__, port->number);
280 static void metrousb_throttle(struct tty_struct *tty)
282 struct usb_serial_port *port = tty->driver_data;
283 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
284 unsigned long flags = 0;
286 dev_dbg(tty->dev, "%s\n", __func__);
288 /* Set the private information for the port to stop reading data. */
289 spin_lock_irqsave(&metro_priv->lock, flags);
290 metro_priv->throttled = 1;
291 spin_unlock_irqrestore(&metro_priv->lock, flags);
294 static int metrousb_tiocmget(struct tty_struct *tty)
296 unsigned long control_state = 0;
297 struct usb_serial_port *port = tty->driver_data;
298 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
299 unsigned long flags = 0;
301 dev_dbg(tty->dev, "%s\n", __func__);
303 spin_lock_irqsave(&metro_priv->lock, flags);
304 control_state = metro_priv->control_state;
305 spin_unlock_irqrestore(&metro_priv->lock, flags);
307 return control_state;
310 static int metrousb_tiocmset(struct tty_struct *tty,
311 unsigned int set, unsigned int clear)
313 struct usb_serial_port *port = tty->driver_data;
314 struct usb_serial *serial = port->serial;
315 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
316 unsigned long flags = 0;
317 unsigned long control_state = 0;
319 dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
321 spin_lock_irqsave(&metro_priv->lock, flags);
322 control_state = metro_priv->control_state;
324 /* Set the RTS and DTR values. */
326 control_state |= TIOCM_RTS;
328 control_state |= TIOCM_DTR;
329 if (clear & TIOCM_RTS)
330 control_state &= ~TIOCM_RTS;
331 if (clear & TIOCM_DTR)
332 control_state &= ~TIOCM_DTR;
334 metro_priv->control_state = control_state;
335 spin_unlock_irqrestore(&metro_priv->lock, flags);
336 return metrousb_set_modem_ctrl(serial, control_state);
339 static void metrousb_unthrottle(struct tty_struct *tty)
341 struct usb_serial_port *port = tty->driver_data;
342 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
343 unsigned long flags = 0;
346 dev_dbg(tty->dev, "%s\n", __func__);
348 /* Set the private information for the port to resume reading data. */
349 spin_lock_irqsave(&metro_priv->lock, flags);
350 metro_priv->throttled = 0;
351 spin_unlock_irqrestore(&metro_priv->lock, flags);
353 /* Submit the urb to read from the port. */
354 port->interrupt_in_urb->dev = port->serial->dev;
355 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
358 "failed submitting interrupt in urb error code=%d\n",
362 static struct usb_driver metrousb_driver = {
364 .probe = usb_serial_probe,
365 .disconnect = usb_serial_disconnect,
369 static struct usb_serial_driver metrousb_device = {
371 .owner = THIS_MODULE,
374 .description = "Metrologic USB to serial converter.",
375 .id_table = id_table,
377 .open = metrousb_open,
378 .close = metrousb_cleanup,
379 .read_int_callback = metrousb_read_int_callback,
380 .attach = metrousb_startup,
381 .release = metrousb_shutdown,
382 .throttle = metrousb_throttle,
383 .unthrottle = metrousb_unthrottle,
384 .tiocmget = metrousb_tiocmget,
385 .tiocmset = metrousb_tiocmset,
388 static struct usb_serial_driver * const serial_drivers[] = {
393 module_usb_serial_driver(metrousb_driver, serial_drivers);
395 MODULE_LICENSE("GPL");
396 MODULE_AUTHOR("Philip Nicastro");
397 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
398 MODULE_DESCRIPTION(DRIVER_DESC);
400 /* Module input parameters */
401 module_param(debug, bool, S_IRUGO | S_IWUSR);
402 MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");