blob: 919dd47ab46f1007ce764e50ecaf503a26b8edb1 [file] [log] [blame]
Aleksey Babahin43d186f2012-03-08 13:18:43 -08001/*
2 Date Created: 9/15/2006
3 File Name: metro-usb.c
4 Description: metro-usb.c is the drivers main source file. The driver is a USB to Serial converter.
5 The driver takes USB data and sends it to a virtual ttyUSB# serial port.
6 The driver interfaces with the usbserial.ko driver supplied by Linux.
7
8 NOTES:
9 To install the driver:
10 1. Install the usbserial.ko module supplied by Linux with: # insmod usbserial.ko
Greg Kroah-Hartmanfdac0f62012-03-08 13:37:32 -080011 2. Install the metro-usb.ko module with: # insmod metro-usb.ko
Aleksey Babahin43d186f2012-03-08 13:18:43 -080012
13 Some of this code is credited to Linux USB open source files that are distributed with Linux.
14
15 Copyright: 2007 Metrologic Instruments. All rights reserved.
16 Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
17 Requirements: gedit.exe, notepad.exe
18
19 Revision History:
20
21 Date: Developer: Revisions:
22 ------------------------------------------------------------------------------
23 1/30/2007 Philip Nicastro Initial release. (v1.0.0.0)
24 2/27/2007 Philip Nicastro Changed the metrousb_read_int_callback function to use a loop with the tty_insert_flip_char function to copy each byte to the tty layer. Removed the tty_buffer_request_room and the tty_insert_flip_string function calls. These calls were not supported on Fedora.
25 2/27/2007 Philip Nicastro Released. (v1.1.0.0)
26 10/07/2011 Aleksey Babahin Update for new kernel (tested on 2.6.38)
27 Add unidirection mode support
28
29
30*/
31
32#include <linux/kernel.h>
33#include <linux/init.h>
34#include <linux/tty.h>
35#include <linux/module.h>
36#include <linux/usb.h>
37#include <linux/errno.h>
38#include <linux/slab.h>
39#include <linux/tty_driver.h>
40#include <linux/tty_flip.h>
41#include <linux/moduleparam.h>
42#include <linux/spinlock.h>
43#include <asm/uaccess.h>
44#include <linux/errno.h>
45#include "metro-usb.h"
46#include <linux/usb/serial.h>
47
48/* Version Information */
49#define DRIVER_VERSION "v1.2.0.0"
50#define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
51
52/* Device table list. */
53static struct usb_device_id id_table [] = {
54 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID) },
55 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
Aleksey Babahin43d186f2012-03-08 13:18:43 -080056 { }, /* Terminating entry. */
57};
58MODULE_DEVICE_TABLE(usb, id_table);
59
60/* Input parameter constants. */
Greg Kroah-Hartmanfdac0f62012-03-08 13:37:32 -080061static bool debug;
Aleksey Babahin43d186f2012-03-08 13:18:43 -080062
63/* Function prototypes. */
64static void metrousb_cleanup (struct usb_serial_port *port);
65static void metrousb_close (struct usb_serial_port *port);
66static int metrousb_open (struct tty_struct *tty, struct usb_serial_port *port);
67static void metrousb_read_int_callback (struct urb *urb);
68static void metrousb_shutdown (struct usb_serial *serial);
69static int metrousb_startup (struct usb_serial *serial);
70static void metrousb_throttle(struct tty_struct *tty);
71static int metrousb_tiocmget(struct tty_struct *tty);
72static int metrousb_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
73static void metrousb_unthrottle(struct tty_struct *tty);
74
75/* Driver structure. */
76static struct usb_driver metrousb_driver = {
77 .name = "metro-usb",
78 .probe = usb_serial_probe,
79 .disconnect = usb_serial_disconnect,
80 .id_table = id_table
81};
82
83/* Device structure. */
84static struct usb_serial_driver metrousb_device = {
85 .driver = {
86 .owner = THIS_MODULE,
87 .name = "metro-usb",
88 },
89 .description = "Metrologic USB to serial converter.",
90 .id_table = id_table,
Aleksey Babahin43d186f2012-03-08 13:18:43 -080091 .num_ports = 1,
92 .open = metrousb_open,
93 .close = metrousb_close,
94 .read_int_callback = metrousb_read_int_callback,
95 .attach = metrousb_startup,
96 .release = metrousb_shutdown,
97 .throttle = metrousb_throttle,
98 .unthrottle = metrousb_unthrottle,
99 .tiocmget = metrousb_tiocmget,
100 .tiocmset = metrousb_tiocmset,
101};
102
Greg Kroah-Hartman11a4f402012-03-08 13:33:04 -0800103static struct usb_serial_driver * const serial_drivers[] = {
104 &metrousb_device,
105 NULL,
106};
107
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800108/* ----------------------------------------------------------------------------------------------
109 Description:
110 Clean up any urbs and port information.
111
112 Input:
113 struct usb_serial_port *: pointer to a usb_serial_port structure.
114
115 Output:
116 int: Returns true (0) if successful, false otherwise.
117*/
118static void metrousb_cleanup (struct usb_serial_port *port)
119{
120 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
121
122 if (port->serial->dev) {
123 /* Shutdown any interrupt in urbs. */
124 if (port->interrupt_in_urb) {
125 usb_unlink_urb(port->interrupt_in_urb);
126 usb_kill_urb(port->interrupt_in_urb);
127 }
128
129 // temp
130 // this will be needed for the write urb
131 /* Shutdown any interrupt_out_urbs. */
132 //if (serial->num_bulk_in)
133 // usb_kill_urb(port->read_urb);
134 }
135}
136
137/* ----------------------------------------------------------------------------------------------
138 Description:
139 Close the open serial port. Cleanup any open serial port information.
140
141 Input:
142 struct usb_serial_port *: pointer to a usb_serial_port structure.
143 struct file *: pointer to a file structure.
144
145 Output:
146 int: Returns true (0) if successful, false otherwise.
147*/
148static void metrousb_close (struct usb_serial_port *port)
149{
150 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
151 metrousb_cleanup(port);
152}
153
154/* ----------------------------------------------------------------------------------------------
155 Description:
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800156 Open the drivers serial port.
157
158 Input:
159 struct usb_serial_port *: pointer to a usb_serial_port structure.
160 struct file *: pointer to a file structure.
161
162 Output:
163 int: Returns true (0) if successful, false otherwise.
164*/
165static int metrousb_open (struct tty_struct *tty, struct usb_serial_port *port)
166{
167 struct usb_serial *serial = port->serial;
168 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
169 unsigned long flags = 0;
170 int result = 0;
171
172 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
173
174 /* Make sure the urb is initialized. */
175 if (!port->interrupt_in_urb) {
176 dbg("METRO-USB - %s - interrupt urb not initialized for port number=%d", __FUNCTION__, port->number);
177 return -ENODEV;
178 }
179
180 /* Set the private data information for the port. */
181 spin_lock_irqsave(&metro_priv->lock, flags);
182 metro_priv->control_state = 0;
183 metro_priv->throttled = 0;
184 spin_unlock_irqrestore(&metro_priv->lock, flags);
185
186 /*
187 * Force low_latency on so that our tty_push actually forces the data
188 * through, otherwise it is scheduled, and with high data rates (like
189 * with OHCI) data can get lost.
190 */
191 if (tty) {
192 tty->low_latency = 1;
193 }
194
195 /* Clear the urb pipe. */
196 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
197
198 /* Start reading from the device */
199 usb_fill_int_urb (port->interrupt_in_urb, serial->dev,
200 usb_rcvintpipe (serial->dev, port->interrupt_in_endpointAddress),
201 port->interrupt_in_urb->transfer_buffer,
202 port->interrupt_in_urb->transfer_buffer_length,
203 metrousb_read_int_callback, port, 1);
204 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
205
206 if (result) {
207 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d"
208 , __FUNCTION__, port->number, result);
209 goto exit;
210 }
211
212 dbg("METRO-USB - %s - port open for port number=%d", __FUNCTION__, port->number);
213exit:
214 return result;
215}
216
217/* ----------------------------------------------------------------------------------------------
218 Description:
219 Read the port from the read interrupt.
220
221 Input:
222 struct urb *: urb structure to get data.
223 struct pt_regs *: pt_regs structure.
224
225 Output:
226 None:
227*/
228static void metrousb_read_int_callback (struct urb *urb)
229{
230 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
231 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
232 struct tty_struct *tty;
233 unsigned char *data = urb->transfer_buffer;
234 int throttled = 0;
235 int result = 0;
236 unsigned long flags = 0;
237
238 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
239
240 switch (urb->status) {
241 case 0:
242 /* Success status, read from the port. */
243 break;
244 case -ECONNRESET:
245 case -ENOENT:
246 case -ESHUTDOWN:
247 /* urb has been terminated. */
248 dbg("METRO-USB - %s - urb shutting down, port number=%d, error code=%d",
249 __FUNCTION__, port->number, result);
250 return;
251 default:
252 dbg("METRO-USB - %s - non-zero urb received, port number=%d, error code=%d",
253 __FUNCTION__, port->number, result);
254 goto exit;
255 }
256
257
258 /* Set the data read from the usb port into the serial port buffer. */
259 tty = tty_port_tty_get(&port->port);
260 if (!tty) {
261 dbg("%s - bad tty pointer - exiting", __func__);
262 return;
263 }
264
265 if (tty && urb->actual_length) {
266 // Loop through the data copying each byte to the tty layer.
267 tty_insert_flip_string(tty, data, urb->actual_length);
268
269 // Force the data to the tty layer.
270 tty_flip_buffer_push(tty);
271 }
272 tty_kref_put(tty);
273
274 /* Set any port variables. */
275 spin_lock_irqsave(&metro_priv->lock, flags);
276 throttled = metro_priv->throttled;
277 spin_unlock_irqrestore(&metro_priv->lock, flags);
278
279 /* Continue trying to read if set. */
280 if (!throttled) {
281 usb_fill_int_urb (port->interrupt_in_urb, port->serial->dev,
282 usb_rcvintpipe (port->serial->dev, port->interrupt_in_endpointAddress),
283 port->interrupt_in_urb->transfer_buffer,
284 port->interrupt_in_urb->transfer_buffer_length,
285 metrousb_read_int_callback, port, 1);
286
287 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
288
289 if (result) {
290 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
291 __FUNCTION__, port->number, result);
292 }
293 }
294 return;
295
296exit:
297 /* Try to resubmit the urb. */
298 result = usb_submit_urb (urb, GFP_ATOMIC);
299 if (result) {
300 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
301 __FUNCTION__, port->number, result);
302 }
303}
304
305/* ----------------------------------------------------------------------------------------------
306 Description:
307 Set the modem control state for the entered serial port.
308
309 Input:
310 struct usb_serial_port *: pointer to a usb_serial_port structure.
311 unsigned int: control state value to set.
312
313 Output:
314 int: Returns true (0) if successful, false otherwise.
315*/
316static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
317{
318 int retval = 0;
319 unsigned char mcr = METROUSB_MCR_NONE;
320
321 dbg("METRO-USB - %s - control state=%d", __FUNCTION__, control_state);
322
323 /* Set the modem control value. */
324 if (control_state & TIOCM_DTR)
325 mcr |= METROUSB_MCR_DTR;
326 if (control_state & TIOCM_RTS)
327 mcr |= METROUSB_MCR_RTS;
328
329 /* Send the command to the usb port. */
330 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
331 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
332 control_state, 0, NULL, 0, WDR_TIMEOUT);
333 if (retval < 0)
334 dbg("METRO-USB - %s - set modem ctrl=0x%x failed, error code=%d", __FUNCTION__, mcr, retval);
335
336 return retval;
337}
338
339
340/* ----------------------------------------------------------------------------------------------
341 Description:
342 Shutdown the driver.
343
344 Input:
345 struct usb_serial *: pointer to a usb-serial structure.
346
347 Output:
348 int: Returns true (0) if successful, false otherwise.
349*/
350static void metrousb_shutdown (struct usb_serial *serial)
351{
352 int i = 0;
353
354 dbg("METRO-USB - %s", __FUNCTION__);
355
356 /* Stop reading and writing on all ports. */
357 for (i=0; i < serial->num_ports; ++i) {
358 /* Close any open urbs. */
359 metrousb_cleanup(serial->port[i]);
360
361 /* Free memory. */
362 kfree(usb_get_serial_port_data(serial->port[i]));
363 usb_set_serial_port_data(serial->port[i], NULL);
364
365 dbg("METRO-USB - %s - freed port number=%d", __FUNCTION__, serial->port[i]->number);
366 }
367}
368
369/* ----------------------------------------------------------------------------------------------
370 Description:
371 Startup the driver.
372
373 Input:
374 struct usb_serial *: pointer to a usb-serial structure.
375
376 Output:
377 int: Returns true (0) if successful, false otherwise.
378*/
379static int metrousb_startup(struct usb_serial *serial)
380{
381 struct metrousb_private *metro_priv;
382 struct usb_serial_port *port;
383 int i = 0;
384
385 dbg("METRO-USB - %s", __FUNCTION__);
386
387 /* Loop through the serial ports setting up the private structures.
388 * Currently we only use one port. */
389 for (i = 0; i < serial->num_ports; ++i) {
390 port = serial->port[i];
391
392 /* Declare memory. */
393 metro_priv = (struct metrousb_private *) kmalloc (sizeof(struct metrousb_private), GFP_KERNEL);
394 if (!metro_priv)
395 return -ENOMEM;
396
397 /* Clear memory. */
398 memset (metro_priv, 0x00, sizeof(struct metrousb_private));
399
400 /* Initialize memory. */
401 spin_lock_init(&metro_priv->lock);
402 usb_set_serial_port_data(port, metro_priv);
403
404 dbg("METRO-USB - %s - port number=%d.", __FUNCTION__, port->number);
405 }
406
407 return 0;
408}
409
410/* ----------------------------------------------------------------------------------------------
411 Description:
412 Set the serial port throttle to stop reading from the port.
413
414 Input:
415 struct usb_serial_port *: pointer to a usb_serial_port structure.
416
417 Output:
418 None:
419*/
420static void metrousb_throttle (struct tty_struct *tty)
421{
422 struct usb_serial_port *port = tty->driver_data;
423 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
424 unsigned long flags = 0;
425
426 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
427
428 /* Set the private information for the port to stop reading data. */
429 spin_lock_irqsave(&metro_priv->lock, flags);
430 metro_priv->throttled = 1;
431 spin_unlock_irqrestore(&metro_priv->lock, flags);
432}
433
434/* ----------------------------------------------------------------------------------------------
435 Description:
436 Get the serial port control line states.
437
438 Input:
439 struct usb_serial_port *: pointer to a usb_serial_port structure.
440 struct file *: pointer to a file structure.
441
442 Output:
443 int: Returns the state of the control lines.
444*/
445static int metrousb_tiocmget (struct tty_struct *tty)
446{
447 unsigned long control_state = 0;
448 struct usb_serial_port *port = tty->driver_data;
449 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
450 unsigned long flags = 0;
451
452 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
453
454 spin_lock_irqsave(&metro_priv->lock, flags);
455 control_state = metro_priv->control_state;
456 spin_unlock_irqrestore(&metro_priv->lock, flags);
457
458 return control_state;
459}
460
461/* ----------------------------------------------------------------------------------------------
462 Description:
463 Set the serial port control line states.
464
465 Input:
466 struct usb_serial_port *: pointer to a usb_serial_port structure.
467 struct file *: pointer to a file structure.
468 unsigned int: line state to set.
469 unsigned int: line state to clear.
470
471 Output:
472 int: Returns the state of the control lines.
473*/
474static int metrousb_tiocmset (struct tty_struct *tty,
475 unsigned int set, unsigned int clear)
476{
477 struct usb_serial_port *port = tty->driver_data;
478 struct usb_serial *serial = port->serial;
479 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
480 unsigned long flags = 0;
481 unsigned long control_state = 0;
482
483 dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__, port->number, set, clear);
484
485 spin_lock_irqsave(&metro_priv->lock, flags);
486 control_state = metro_priv->control_state;
487
488 // Set the RTS and DTR values.
489 if (set & TIOCM_RTS)
490 control_state |= TIOCM_RTS;
491 if (set & TIOCM_DTR)
492 control_state |= TIOCM_DTR;
493 if (clear & TIOCM_RTS)
494 control_state &= ~TIOCM_RTS;
495 if (clear & TIOCM_DTR)
496 control_state &= ~TIOCM_DTR;
497
498 metro_priv->control_state = control_state;
499 spin_unlock_irqrestore(&metro_priv->lock, flags);
500 return metrousb_set_modem_ctrl(serial, control_state);
501}
502
503/* ----------------------------------------------------------------------------------------------
504 Description:
505 Set the serial port unthrottle to resume reading from the port.
506
507 Input:
508 struct usb_serial_port *: pointer to a usb_serial_port structure.
509
510 Output:
511 None:
512*/
513static void metrousb_unthrottle (struct tty_struct *tty)
514{
515 struct usb_serial_port *port = tty->driver_data;
516 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
517 unsigned long flags = 0;
518 int result = 0;
519
520 dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
521
522 /* Set the private information for the port to resume reading data. */
523 spin_lock_irqsave(&metro_priv->lock, flags);
524 metro_priv->throttled = 0;
525 spin_unlock_irqrestore(&metro_priv->lock, flags);
526
527 /* Submit the urb to read from the port. */
528 port->interrupt_in_urb->dev = port->serial->dev;
529 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
530 if (result) {
531 dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
532 __FUNCTION__, port->number, result);
533 }
534}
535
Greg Kroah-Hartman1935e352012-03-08 13:39:53 -0800536module_usb_serial_driver(metrousb_driver, serial_drivers);
537
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800538MODULE_LICENSE("GPL");
539MODULE_AUTHOR( "Philip Nicastro" );
540MODULE_AUTHOR( "Aleksey Babahin <tamerlan311@gmail.com>" );
541MODULE_DESCRIPTION( DRIVER_DESC );
542
543/* Module input parameters */
544module_param(debug, bool, S_IRUGO | S_IWUSR);
545MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");