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