File: ipaq.c

package info (click to toggle)
kernel-source-2.4.18 2.4.18-14.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 144,648 kB
  • ctags: 443,980
  • sloc: ansic: 2,548,117; asm: 142,436; makefile: 8,411; sh: 3,097; perl: 2,561; yacc: 1,177; cpp: 755; tcl: 577; lex: 352; awk: 251; lisp: 218; sed: 72
file content (532 lines) | stat: -rw-r--r-- 13,820 bytes parent folder | download | duplicates (2)
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
/*
 * USB Compaq iPAQ driver
 *
 *	Copyright (C) 2001
 *	    Ganesh Varadarajan <ganesh@veritas.com>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/usb.h>

#ifdef CONFIG_USB_SERIAL_DEBUG
	static int debug = 1;
#else
	static int debug = 0;
#endif

#include "usb-serial.h"
#include "ipaq.h"

/*
 * Version Information
 */
#define DRIVER_VERSION "v0.1"
#define DRIVER_AUTHOR "Ganesh Varadarajan <ganesh@veritas.com>"
#define DRIVER_DESC "USB Compaq iPAQ driver"

/* Function prototypes for an ipaq */
static int  ipaq_open (struct usb_serial_port *port, struct file *filp);
static void ipaq_close (struct usb_serial_port *port, struct file *filp);
static int  ipaq_startup (struct usb_serial *serial);
static void ipaq_shutdown (struct usb_serial *serial);
static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
		       int count);
static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
			   int count);
static int ipaq_write_flush(struct usb_serial_port *port);
static void ipaq_read_bulk_callback (struct urb *urb);
static void ipaq_write_bulk_callback(struct urb *urb);
static int ipaq_write_room(struct usb_serial_port *port);
static int ipaq_chars_in_buffer(struct usb_serial_port *port);
static void ipaq_destroy_lists(struct usb_serial_port *port);


static __devinitdata struct usb_device_id ipaq_id_table [] = {
	{ USB_DEVICE(IPAQ_VENDOR_ID, IPAQ_PRODUCT_ID) },
	{ }					/* Terminating entry */
};

MODULE_DEVICE_TABLE (usb, ipaq_id_table);

/* All of the device info needed for the Compaq iPAQ */
struct usb_serial_device_type ipaq_device = {
	name:			"Compaq iPAQ",
	id_table:		ipaq_id_table,
	needs_interrupt_in:	MUST_HAVE_NOT,
	needs_bulk_in:		MUST_HAVE,
	needs_bulk_out:		MUST_HAVE,
	num_interrupt_in:	0,
	num_bulk_in:		1,
	num_bulk_out:		1,
	num_ports:		1,
	open:			ipaq_open,
	close:			ipaq_close,
	startup:		ipaq_startup,
	shutdown:		ipaq_shutdown,
	write:			ipaq_write,
	write_room:		ipaq_write_room,
	chars_in_buffer:	ipaq_chars_in_buffer,
	read_bulk_callback:	ipaq_read_bulk_callback,
	write_bulk_callback:	ipaq_write_bulk_callback,
};

static spinlock_t	write_list_lock;
static int		bytes_in;
static int		bytes_out;

static int ipaq_open(struct usb_serial_port *port, struct file *filp)
{
	struct usb_serial	*serial = port->serial;
	struct ipaq_private	*priv;
	struct ipaq_packet	*pkt;
	int			i, result = 0;

	if (port_paranoia_check(port, __FUNCTION__)) {
		return -ENODEV;
	}
	
	dbg(__FUNCTION__ " - port %d", port->number);

	down(&port->sem);
	
	++port->open_count;
	MOD_INC_USE_COUNT;
	
	if (!port->active) {
		port->active = 1;
		bytes_in = 0;
		bytes_out = 0;
		priv = (struct ipaq_private *)kmalloc(sizeof(struct ipaq_private), GFP_KERNEL);
		if (priv == NULL) {
			err(__FUNCTION__ " - Out of memory");
			return -ENOMEM;
		}
		port->private = (void *)priv;
		priv->active = 0;
		priv->queue_len = 0;
		INIT_LIST_HEAD(&priv->queue);
		INIT_LIST_HEAD(&priv->freelist);

		for (i = 0; i < URBDATA_QUEUE_MAX / PACKET_SIZE; i++) {
			pkt = kmalloc(sizeof(struct ipaq_packet), GFP_KERNEL);
			if (pkt == NULL) {
				goto enomem;
			}
			pkt->data = kmalloc(PACKET_SIZE, GFP_KERNEL);
			if (pkt->data == NULL) {
				kfree(pkt);
				goto enomem;
			}
			pkt->len = 0;
			pkt->written = 0;
			INIT_LIST_HEAD(&pkt->list);
			list_add(&pkt->list, &priv->freelist);
			priv->free_len += PACKET_SIZE;
		}

		/*
		 * Force low latency on. This will immediately push data to the line
		 * discipline instead of queueing.
		 */

		port->tty->low_latency = 1;

		/*
		 * Lose the small buffers usbserial provides. Make larger ones.
		 */

		kfree(port->bulk_in_buffer);
		kfree(port->bulk_out_buffer);
		port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
		if (port->bulk_in_buffer == NULL) {
			goto enomem;
		}
		port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL);
		if (port->bulk_out_buffer == NULL) {
			kfree(port->bulk_in_buffer);
			goto enomem;
		}
		port->read_urb->transfer_buffer = port->bulk_in_buffer;
		port->write_urb->transfer_buffer = port->bulk_out_buffer;
		port->read_urb->transfer_buffer_length = URBDATA_SIZE;
		port->bulk_out_size = port->write_urb->transfer_buffer_length = URBDATA_SIZE;
		
		/* Start reading from the device */
		FILL_BULK_URB(port->read_urb, serial->dev, 
			      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
			      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
			      ipaq_read_bulk_callback, port);
		result = usb_submit_urb(port->read_urb);
		if (result) {
			err(__FUNCTION__ " - failed submitting read urb, error %d", result);
		}

		/*
		 * Send out two control messages observed in win98 sniffs. Not sure what
		 * they do.
		 */

		result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
				0x1, 0, NULL, 0, 5 * HZ);
		if (result < 0) {
			err(__FUNCTION__ " - failed doing control urb, error %d", result);
		}
		result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 0x22, 0x21,
				0x1, 0, NULL, 0, 5 * HZ);
		if (result < 0) {
			err(__FUNCTION__ " - failed doing control urb, error %d", result);
		}
	}
	
	up(&port->sem);
	
	return result;

enomem:
	ipaq_destroy_lists(port);
	kfree(priv);
	err(__FUNCTION__ " - Out of memory");
	return -ENOMEM;
}


static void ipaq_close(struct usb_serial_port *port, struct file *filp)
{
	struct usb_serial	*serial;
	struct ipaq_private	*priv = port->private;

	if (port_paranoia_check(port, __FUNCTION__)) {
		return; 
	}
	
	dbg(__FUNCTION__ " - port %d", port->number);
			 
	serial = get_usb_serial(port, __FUNCTION__);
	if (!serial)
		return;
	
	down (&port->sem);

	--port->open_count;

	if (port->open_count <= 0) {

		/*
		 * shut down bulk read and write
		 */

		usb_unlink_urb(port->write_urb);
		usb_unlink_urb(port->read_urb);
		ipaq_destroy_lists(port);
		kfree(priv);
		port->private = NULL;
		port->active = 0;
		port->open_count = 0;

	}
	up (&port->sem);

	/* Uncomment the following line if you want to see some statistics in your syslog */
	/* info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out); */

	MOD_DEC_USE_COUNT;
}

static void ipaq_read_bulk_callback(struct urb *urb)
{
	struct usb_serial_port	*port = (struct usb_serial_port *)urb->context;
	struct usb_serial	*serial = get_usb_serial (port, __FUNCTION__);
	struct tty_struct	*tty;
	unsigned char		*data = urb->transfer_buffer;
	int			i, result;

	if (port_paranoia_check(port, __FUNCTION__))
		return;

	dbg(__FUNCTION__ " - port %d", port->number);

	if (!serial) {
		dbg(__FUNCTION__ " - bad serial pointer, exiting");
		return;
	}

	if (urb->status) {
		dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
		return;
	}

	usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);

	tty = port->tty;
	if (urb->actual_length) {
		for (i = 0; i < urb->actual_length ; ++i) {
			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
				tty_flip_buffer_push(tty);
			}
			/* this doesn't actually push the data through unless tty->low_latency is set */
			tty_insert_flip_char(tty, data[i], 0);
		}
		tty_flip_buffer_push(tty);
		bytes_in += urb->actual_length;
	}

	/* Continue trying to always read  */
	FILL_BULK_URB(port->read_urb, serial->dev, 
		      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
		      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
		      ipaq_read_bulk_callback, port);
	result = usb_submit_urb(port->read_urb);
	if (result)
		err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
	return;
}

static int ipaq_write(struct usb_serial_port *port, int from_user, const unsigned char *buf,
		       int count)
{
	const unsigned char	*current_position = buf;
	int			bytes_sent = 0;
	int			transfer_size;

	dbg(__FUNCTION__ " - port %d", port->number);

	usb_serial_debug_data(__FILE__, __FUNCTION__, count, buf);
	
	while (count > 0) {
		transfer_size = min(count, PACKET_SIZE);
		if (ipaq_write_bulk(port, from_user, current_position, transfer_size)) {
			break;
		}
		current_position += transfer_size;
		bytes_sent += transfer_size;
		count -= transfer_size;
		bytes_out += transfer_size;
	}

	return bytes_sent;
} 

static int ipaq_write_bulk(struct usb_serial_port *port, int from_user, const unsigned char *buf,
			   int count)
{
	struct ipaq_private	*priv = port->private;
	struct ipaq_packet	*pkt = NULL;
	int			result = 0;
	unsigned long		flags;

	if (priv->free_len <= 0) {
		dbg(__FUNCTION__ " - we're stuffed");
		return -EAGAIN;
	}

	spin_lock_irqsave(&write_list_lock, flags);
	if (!list_empty(&priv->freelist)) {
		pkt = list_entry(priv->freelist.next, struct ipaq_packet, list);
		list_del(&pkt->list);
		priv->free_len -= PACKET_SIZE;
	}
	spin_unlock_irqrestore(&write_list_lock, flags);
	if (pkt == NULL) {
		dbg(__FUNCTION__ " - we're stuffed");
		return -EAGAIN;
	}

	if (from_user) {
		copy_from_user(pkt->data, buf, count);
	} else {
		memcpy(pkt->data, buf, count);
	}
	usb_serial_debug_data(__FILE__, __FUNCTION__, count, pkt->data);

	pkt->len = count;
	pkt->written = 0;
	spin_lock_irqsave(&write_list_lock, flags);
	list_add_tail(&pkt->list, &priv->queue);
	priv->queue_len += count;
	if (priv->active == 0) {
		priv->active = 1;
		result = ipaq_write_flush(port);
	}
	spin_unlock_irqrestore(&write_list_lock, flags);
	return result;
}

static int ipaq_write_flush(struct usb_serial_port *port)
{
	struct ipaq_private	*priv = (struct ipaq_private *)port->private;
	struct usb_serial	*serial = port->serial;
	int			count, room, result;
	struct ipaq_packet	*pkt;
	struct urb		*urb = port->write_urb;
	struct list_head	*tmp;

	if (urb->status == -EINPROGRESS) {
		/* Should never happen */
		err(__FUNCTION__ " - flushing while urb is active !");
		return -EAGAIN;
	}
	room = URBDATA_SIZE;
	for (tmp = priv->queue.next; tmp != &priv->queue;) {
		pkt = list_entry(tmp, struct ipaq_packet, list);
		tmp = tmp->next;
		count = min(room, (int)(pkt->len - pkt->written));
		memcpy(urb->transfer_buffer + (URBDATA_SIZE - room),
		       pkt->data + pkt->written, count);
		room -= count;
		pkt->written += count;
		priv->queue_len -= count;
		if (pkt->written == pkt->len) {
			list_del(&pkt->list);
			list_add(&pkt->list, &priv->freelist);
			priv->free_len += PACKET_SIZE;
		}
		if (room == 0) {
			break;
		}
	}

	count = URBDATA_SIZE - room;
	FILL_BULK_URB(port->write_urb, serial->dev, 
		      usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
		      port->write_urb->transfer_buffer, count, ipaq_write_bulk_callback,
		      port);
	result = usb_submit_urb(urb);
	if (result) {
		err(__FUNCTION__ " - failed submitting write urb, error %d", result);
	}
	return result;
}

static void ipaq_write_bulk_callback(struct urb *urb)
{
	struct usb_serial_port	*port = (struct usb_serial_port *)urb->context;
	struct ipaq_private	*priv = (struct ipaq_private *)port->private;
	unsigned long		flags;

	if (port_paranoia_check (port, __FUNCTION__)) {
		return;
	}
	
	dbg(__FUNCTION__ " - port %d", port->number);
	
	if (urb->status) {
		dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
	}

	spin_lock_irqsave(&write_list_lock, flags);
	if (!list_empty(&priv->queue)) {
		ipaq_write_flush(port);
	} else {
		priv->active = 0;
	}
	spin_unlock_irqrestore(&write_list_lock, flags);
	queue_task(&port->tqueue, &tq_immediate);
	mark_bh(IMMEDIATE_BH);
	
	return;
}

static int ipaq_write_room(struct usb_serial_port *port)
{
	struct ipaq_private	*priv = (struct ipaq_private *)port->private;

	dbg(__FUNCTION__ " - freelen %d", priv->free_len);
	return priv->free_len;
}

static int ipaq_chars_in_buffer(struct usb_serial_port *port)
{
	struct ipaq_private	*priv = (struct ipaq_private *)port->private;

	dbg(__FUNCTION__ " - queuelen %d", priv->queue_len);
	return priv->queue_len;
}

static void ipaq_destroy_lists(struct usb_serial_port *port)
{
	struct ipaq_private	*priv = (struct ipaq_private *)port->private;
	struct list_head	*tmp;
	struct ipaq_packet	*pkt;

	for (tmp = priv->queue.next; tmp != &priv->queue;) {
		pkt = list_entry(tmp, struct ipaq_packet, list);
		tmp = tmp->next;
		kfree(pkt->data);
		kfree(pkt);
	}
	for (tmp = priv->freelist.next; tmp != &priv->freelist;) {
		pkt = list_entry(tmp, struct ipaq_packet, list);
		tmp = tmp->next;
		kfree(pkt->data);
		kfree(pkt);
	}
	return;
}


static int ipaq_startup(struct usb_serial *serial)
{
	dbg(__FUNCTION__);
	usb_set_configuration(serial->dev, 1);
	return 0;
}

static void ipaq_shutdown(struct usb_serial *serial)
{
	int i;

	dbg (__FUNCTION__);

	/* stop reads and writes on all ports */
	for (i=0; i < serial->num_ports; ++i) {
		while (serial->port[i].open_count > 0) {
			ipaq_close(&serial->port[i], NULL);
		}
	}
}

static int __init ipaq_init(void)
{
	usb_serial_register(&ipaq_device);
	info(DRIVER_DESC " " DRIVER_VERSION);

	return 0;
}


static void __exit ipaq_exit(void)
{
	usb_serial_deregister(&ipaq_device);
}


module_init(ipaq_init);
module_exit(ipaq_exit);

MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
MODULE_LICENSE("GPL");

MODULE_PARM(debug, "i");
MODULE_PARM_DESC(debug, "Debug enabled or not");