summaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/metro-usb.c
blob: d22a603597e899b1b16aca85280b5f768cd6683a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
  Date Created:	9/15/2006
  File Name:		metro-usb.c
  Description:	metro-usb.c is the drivers main source file. The driver is a USB to Serial converter.
		The driver takes USB data and sends it to a virtual ttyUSB# serial port.
		The driver interfaces with the usbserial.ko driver supplied by Linux.

		NOTES:
		To install the driver:
		1. Install the usbserial.ko module supplied by Linux with: # insmod usbserial.ko
		2. Install the metro-usb.ko module with: # insmod metro-usb.ko

		Some of this code is credited to Linux USB open source files that are distributed with Linux.

  Copyright:	2007 Metrologic Instruments. All rights reserved.
  Copyright:	2011 Azimut Ltd. <http://azimutrzn.ru/>
  Requirements: gedit.exe, notepad.exe
 
  Revision History:

  Date:			Developer:			Revisions:
  ------------------------------------------------------------------------------
  1/30/2007		Philip Nicastro		Initial release. (v1.0.0.0)
  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.
  2/27/2007		Philip Nicastro		Released. (v1.1.0.0)
  10/07/2011		Aleksey Babahin		Update for new kernel (tested on 2.6.38)
						Add unidirection mode support
 
 
*/

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/moduleparam.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/usb/serial.h>
#include <asm/uaccess.h>

/* Version Information */
#define DRIVER_VERSION "v1.2.0.0"
#define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"

/* Product information. */
#define FOCUS_VENDOR_ID			0x0C2E
#define FOCUS_PRODUCT_ID		0x0720
#define FOCUS_PRODUCT_ID_UNI		0x0710

#define METROUSB_SET_REQUEST_TYPE	0x40
#define METROUSB_SET_MODEM_CTRL_REQUEST	10
#define METROUSB_SET_BREAK_REQUEST	0x40
#define METROUSB_MCR_NONE		0x08	/* Deactivate DTR and RTS. */
#define METROUSB_MCR_RTS		0x0a	/* Activate RTS. */
#define METROUSB_MCR_DTR		0x09	/* Activate DTR. */
#define WDR_TIMEOUT			5000 	/* default urb timeout. */

/* Private data structure. */
struct metrousb_private {
	spinlock_t lock;
	int throttled;
	unsigned long control_state;
};

/* Device table list. */
static struct usb_device_id id_table [] = {
	{ USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID) },
	{ USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
	{ }, /* Terminating entry. */
};
MODULE_DEVICE_TABLE(usb, id_table);

/* Input parameter constants. */
static bool debug;

/* Function prototypes. */
static void metrousb_cleanup (struct usb_serial_port *port);
static void metrousb_close (struct usb_serial_port *port);
static int  metrousb_open (struct tty_struct *tty, struct usb_serial_port *port);
static void metrousb_read_int_callback (struct urb *urb);
static void metrousb_shutdown (struct usb_serial *serial);
static int metrousb_startup (struct usb_serial *serial);
static void metrousb_throttle(struct tty_struct *tty);
static int metrousb_tiocmget(struct tty_struct *tty);
static int metrousb_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
static void metrousb_unthrottle(struct tty_struct *tty);

/* Driver structure. */
static struct usb_driver metrousb_driver = {
	.name =		"metro-usb",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table
};

/* Device structure. */
static struct usb_serial_driver metrousb_device = {
	.driver = {
		.owner =	THIS_MODULE,
		.name =		"metro-usb",
	},
	.description 		= "Metrologic USB to serial converter.",
	.id_table 		= id_table,
	.num_ports 		= 1,
	.open 			= metrousb_open,
	.close 			= metrousb_close,
	.read_int_callback 	= metrousb_read_int_callback,
	.attach 		= metrousb_startup,
	.release 		= metrousb_shutdown,
	.throttle          	= metrousb_throttle,
	.unthrottle        	= metrousb_unthrottle,
	.tiocmget          	= metrousb_tiocmget,
	.tiocmset          	= metrousb_tiocmset,
};

static struct usb_serial_driver * const serial_drivers[] = {
	&metrousb_device,
	NULL,
};

/* ----------------------------------------------------------------------------------------------
  Description:
	Clean up any urbs and port information.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.

  Output:
	int: Returns true (0) if successful, false otherwise.
*/
static void metrousb_cleanup (struct usb_serial_port *port)
{
	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);

	if (port->serial->dev) {
		/* Shutdown any interrupt in urbs. */
		if (port->interrupt_in_urb) {
			usb_unlink_urb(port->interrupt_in_urb);
			usb_kill_urb(port->interrupt_in_urb);
		}

		// temp
		// this will be needed for the write urb
		/* Shutdown any interrupt_out_urbs. */
		//if (serial->num_bulk_in)
		//	usb_kill_urb(port->read_urb);
	}
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Close the open serial port. Cleanup any open serial port information.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.
	struct file *: pointer to a file structure.

  Output:
	int: Returns true (0) if successful, false otherwise.
*/
static void metrousb_close (struct usb_serial_port *port)
{
	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
	metrousb_cleanup(port);
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Open the drivers serial port.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.
	struct file *: pointer to a file structure.

  Output:
	int: Returns true (0) if successful, false otherwise.
*/
static int metrousb_open (struct tty_struct *tty, struct usb_serial_port *port)
{
	struct usb_serial *serial = port->serial;
	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;
	int result = 0;

	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);

	/* Make sure the urb is initialized. */
	if (!port->interrupt_in_urb) {
		dbg("METRO-USB - %s - interrupt urb not initialized for port number=%d", __FUNCTION__, port->number);
		return -ENODEV;
	}

	/* Set the private data information for the port. */
	spin_lock_irqsave(&metro_priv->lock, flags);
	metro_priv->control_state = 0;
	metro_priv->throttled = 0;
	spin_unlock_irqrestore(&metro_priv->lock, flags);

	/*
	 * Force low_latency on so that our tty_push actually forces the data
	 * through, otherwise it is scheduled, and with high data rates (like
	 * with OHCI) data can get lost.
	 */
	if (tty) {
		tty->low_latency = 1;
	}

	/* Clear the urb pipe. */
	usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);

	/* Start reading from the device */
	usb_fill_int_urb (port->interrupt_in_urb, serial->dev,
			   usb_rcvintpipe (serial->dev, port->interrupt_in_endpointAddress),
			   port->interrupt_in_urb->transfer_buffer,
			   port->interrupt_in_urb->transfer_buffer_length,
			   metrousb_read_int_callback, port, 1);
	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);

	if (result) {
		dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d"
			, __FUNCTION__, port->number, result);
		goto exit;
	}

	dbg("METRO-USB - %s - port open for port number=%d", __FUNCTION__, port->number);
exit:
	return result;
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Read the port from the read interrupt.

  Input:
	struct urb *: urb structure to get data.
	struct pt_regs *: pt_regs structure.

  Output:
	None:
*/
static void metrousb_read_int_callback (struct urb *urb)
{
	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	unsigned char *data = urb->transfer_buffer;
	int throttled = 0;
	int result = 0;
	unsigned long flags = 0;

	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);

	switch (urb->status) {
		case 0:
			/* Success status, read from the port. */
			break;
		case -ECONNRESET:
		case -ENOENT:
		case -ESHUTDOWN:
			/* urb has been terminated. */
			dbg("METRO-USB - %s - urb shutting down, port number=%d, error code=%d",
				__FUNCTION__, port->number, result);
			return;
		default:
			dbg("METRO-USB - %s - non-zero urb received, port number=%d, error code=%d",
				__FUNCTION__, port->number, result);
			goto exit;
	}


	/* Set the data read from the usb port into the serial port buffer. */
	tty = tty_port_tty_get(&port->port);
	if (!tty) {
		dbg("%s - bad tty pointer - exiting", __func__);
		return;
	}

	if (tty && urb->actual_length) {
		// Loop through the data copying each byte to the tty layer.
		tty_insert_flip_string(tty, data, urb->actual_length);

		// Force the data to the tty layer.
		tty_flip_buffer_push(tty);
	}
	tty_kref_put(tty);

	/* Set any port variables. */
	spin_lock_irqsave(&metro_priv->lock, flags);
	throttled = metro_priv->throttled;
	spin_unlock_irqrestore(&metro_priv->lock, flags);

	/* Continue trying to read if set. */
	if (!throttled) {
		usb_fill_int_urb (port->interrupt_in_urb, port->serial->dev,
				   usb_rcvintpipe (port->serial->dev, port->interrupt_in_endpointAddress),
				   port->interrupt_in_urb->transfer_buffer,
				   port->interrupt_in_urb->transfer_buffer_length,
				   metrousb_read_int_callback, port, 1);

		result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);

		if (result) {
			dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
				__FUNCTION__, port->number, result);
		}
	}
	return;

exit:
	/* Try to resubmit the urb. */
	result = usb_submit_urb (urb, GFP_ATOMIC);
	if (result) {
		dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
			__FUNCTION__, port->number, result);
	}
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Set the modem control state for the entered serial port.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.
	unsigned int: control state value to set.

  Output:
	int: Returns true (0) if successful, false otherwise.
*/
static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
{
	int retval = 0;
	unsigned char mcr = METROUSB_MCR_NONE;

	dbg("METRO-USB - %s - control state=%d", __FUNCTION__, control_state);

	/* Set the modem control value. */
	if (control_state & TIOCM_DTR)
		mcr |= METROUSB_MCR_DTR;
	if (control_state & TIOCM_RTS)
		mcr |= METROUSB_MCR_RTS;

	/* Send the command to the usb port. */
	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
				control_state, 0, NULL, 0, WDR_TIMEOUT);
	if (retval < 0)
		dbg("METRO-USB - %s - set modem ctrl=0x%x failed, error code=%d", __FUNCTION__, mcr, retval);

	return retval;
}


/* ----------------------------------------------------------------------------------------------
  Description:
	Shutdown the driver.

  Input:
	struct usb_serial *: pointer to a usb-serial structure.

  Output:
	int: Returns true (0) if successful, false otherwise.
*/
static void metrousb_shutdown (struct usb_serial *serial)
{
	int i = 0;

	dbg("METRO-USB - %s", __FUNCTION__);

	/* Stop reading and writing on all ports. */
	for (i=0; i < serial->num_ports; ++i) {
		/* Close any open urbs. */
		metrousb_cleanup(serial->port[i]);

		/* Free memory. */
		kfree(usb_get_serial_port_data(serial->port[i]));
		usb_set_serial_port_data(serial->port[i], NULL);

		dbg("METRO-USB - %s - freed port number=%d", __FUNCTION__, serial->port[i]->number);
	}
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Startup the driver.

  Input:
	struct usb_serial *: pointer to a usb-serial structure.

  Output:
	int: Returns true (0) if successful, false otherwise.
*/
static int metrousb_startup(struct usb_serial *serial)
{
	struct metrousb_private *metro_priv;
	struct usb_serial_port *port;
	int i = 0;

	dbg("METRO-USB - %s", __FUNCTION__);

	/* Loop through the serial ports setting up the private structures.
	 * Currently we only use one port. */
	for (i = 0; i < serial->num_ports; ++i) {
		port = serial->port[i];

		/* Declare memory. */
		metro_priv = (struct metrousb_private *) kmalloc (sizeof(struct metrousb_private), GFP_KERNEL);
		if (!metro_priv)
			return -ENOMEM;

		/* Clear memory. */
		memset (metro_priv, 0x00, sizeof(struct metrousb_private));

		/* Initialize memory. */
		spin_lock_init(&metro_priv->lock);
		usb_set_serial_port_data(port, metro_priv);

		dbg("METRO-USB - %s - port number=%d.", __FUNCTION__, port->number);
	}

	return 0;
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Set the serial port throttle to stop reading from the port.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.

  Output:
	None:
*/
static void metrousb_throttle (struct tty_struct *tty)
{
	struct usb_serial_port *port = tty->driver_data;
	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;

	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);

	/* Set the private information for the port to stop reading data. */
	spin_lock_irqsave(&metro_priv->lock, flags);
	metro_priv->throttled = 1;
	spin_unlock_irqrestore(&metro_priv->lock, flags);
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Get the serial port control line states.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.
	struct file *: pointer to a file structure.

  Output:
	int: Returns the state of the control lines.
*/
static int metrousb_tiocmget (struct tty_struct *tty)
{
	unsigned long control_state = 0;
	struct usb_serial_port *port = tty->driver_data;
	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;

	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);

	spin_lock_irqsave(&metro_priv->lock, flags);
	control_state = metro_priv->control_state;
	spin_unlock_irqrestore(&metro_priv->lock, flags);

	return control_state;
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Set the serial port control line states.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.
	struct file *: pointer to a file structure.
	unsigned int: line state to set.
	unsigned int: line state to clear.

  Output:
	int: Returns the state of the control lines.
*/
static int metrousb_tiocmset (struct tty_struct *tty,
			       unsigned int set, unsigned int clear)
{
	struct usb_serial_port *port = tty->driver_data;
	struct usb_serial *serial = port->serial;
	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;
	unsigned long control_state = 0;

	dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__, port->number, set, clear);

	spin_lock_irqsave(&metro_priv->lock, flags);
	control_state = metro_priv->control_state;

	// Set the RTS and DTR values.
	if (set & TIOCM_RTS)
		control_state |= TIOCM_RTS;
	if (set & TIOCM_DTR)
		control_state |= TIOCM_DTR;
	if (clear & TIOCM_RTS)
		control_state &= ~TIOCM_RTS;
	if (clear & TIOCM_DTR)
		control_state &= ~TIOCM_DTR;

	metro_priv->control_state = control_state;
	spin_unlock_irqrestore(&metro_priv->lock, flags);
	return metrousb_set_modem_ctrl(serial, control_state);
}

/* ----------------------------------------------------------------------------------------------
  Description:
	Set the serial port unthrottle to resume reading from the port.

  Input:
	struct usb_serial_port *: pointer to a usb_serial_port structure.

  Output:
	None:
*/
static void metrousb_unthrottle (struct tty_struct *tty)
{
	struct usb_serial_port *port = tty->driver_data;
	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;
	int result = 0;

	dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);

	/* Set the private information for the port to resume reading data. */
	spin_lock_irqsave(&metro_priv->lock, flags);
	metro_priv->throttled = 0;
	spin_unlock_irqrestore(&metro_priv->lock, flags);

	/* Submit the urb to read from the port. */
	port->interrupt_in_urb->dev = port->serial->dev;
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result) {
		dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
			__FUNCTION__, port->number, result);
	}
}

module_usb_serial_driver(metrousb_driver, serial_drivers);

MODULE_LICENSE("GPL");
MODULE_AUTHOR( "Philip Nicastro" );
MODULE_AUTHOR( "Aleksey Babahin <tamerlan311@gmail.com>" );
MODULE_DESCRIPTION( DRIVER_DESC );

/* Module input parameters */
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");