summaryrefslogtreecommitdiffstats
path: root/net/irda/irda_device.c
blob: 94345259d7ce4a88ae9f34de89b40a402b4d9d0f (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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*********************************************************************
 *                
 * Filename:      irda_device.c
 * Version:       0.9
 * Description:   Utility functions used by the device drivers
 * Status:        Experimental.
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Sat Oct  9 09:22:27 1999
 * Modified at:   Sun Jan 23 17:41:24 2000
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * 
 *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
 *     
 *     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.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License 
 *     along with this program; if not, write to the Free Software 
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *     MA 02111-1307 USA
 *     
 ********************************************************************/

#include <linux/config.h>
#include <linux/string.h>
#include <linux/proc_fs.h>
#include <linux/skbuff.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>
#include <linux/init.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/wireless.h>
#include <linux/spinlock.h>

#include <asm/ioctls.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <asm/dma.h>

#include <net/pkt_sched.h>

#include <net/irda/irda_device.h>
#include <net/irda/irlap.h>
#include <net/irda/timer.h>
#include <net/irda/wrapper.h>

extern int irtty_init(void);
extern int nsc_ircc_init(void);
extern int ircc_init(void);
extern int w83977af_init(void);
extern int esi_init(void);
extern int tekram_init(void);
extern int actisys_init(void);
extern int girbil_init(void);

static void __irda_task_delete(struct irda_task *task);

static hashbin_t *dongles = NULL;
static hashbin_t *tasks = NULL;

const char *infrared_mode[] = {
	"IRDA_IRLAP",
	"IRDA_RAW",
	"SHARP_ASK",
	"TV_REMOTE",
};

static const char *task_state[] = {
	"IRDA_TASK_INIT",
	"IRDA_TASK_DONE", 
	"IRDA_TASK_WAIT",
	"IRDA_TASK_WAIT1",
	"IRDA_TASK_WAIT2",
	"IRDA_TASK_WAIT3",
	"IRDA_TASK_CHILD_INIT",
	"IRDA_TASK_CHILD_WAIT",
	"IRDA_TASK_CHILD_DONE",
};

static void irda_task_timer_expired(void *data);

#ifdef CONFIG_PROC_FS
int irda_device_proc_read(char *buf, char **start, off_t offset, int len, 
			  int unused);

#endif /* CONFIG_PROC_FS */

int __init irda_device_init( void)
{
	dongles = hashbin_new(HB_GLOBAL);
	if (dongles == NULL) {
		printk(KERN_WARNING 
		       "IrDA: Can't allocate dongles hashbin!\n");
		return -ENOMEM;
	}

	tasks = hashbin_new(HB_GLOBAL);
	if (tasks == NULL) {
		printk(KERN_WARNING 
		       "IrDA: Can't allocate tasks hashbin!\n");
		return -ENOMEM;
	}

	/* 
	 * Call the init function of the device drivers that has not been
	 * compiled as a module 
	 */
#ifdef CONFIG_IRTTY_SIR
	irtty_init();
#endif
#ifdef CONFIG_WINBOND_FIR
	w83977af_init();
#endif
#ifdef CONFIG_NSC_FIR
	nsc_ircc_init();
#endif
#ifdef CONFIG_TOSHIBA_FIR
	toshoboe_init();
#endif
#ifdef CONFIG_SMC_IRCC_FIR
	ircc_init();
#endif
#ifdef CONFIG_ESI_DONGLE
	esi_init();
#endif
#ifdef CONFIG_TEKRAM_DONGLE
	tekram_init();
#endif
#ifdef CONFIG_ACTISYS_DONGLE
	actisys_init();
#endif
#ifdef CONFIG_GIRBIL_DONGLE
	girbil_init();
#endif
#ifdef CONFIG_LITELINK_DONGLE
	litelink_init();
#endif
#ifdef CONFIG_OLD_BELKIN
 	old_belkin_init();
#endif
	return 0;
}

void irda_device_cleanup(void)
{
	IRDA_DEBUG(4, __FUNCTION__ "()\n");

	hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
	hashbin_delete(dongles, NULL);
}

/*
 * Function irda_device_set_media_busy (self, status)
 *
 *    Called when we have detected that another station is transmiting
 *    in contention mode.
 */
void irda_device_set_media_busy(struct net_device *dev, int status) 
{
	struct irlap_cb *self;

	IRDA_DEBUG(4, __FUNCTION__ "(%s)\n", status ? "TRUE" : "FALSE");

	self = (struct irlap_cb *) dev->atalk_ptr;

	ASSERT(self != NULL, return;);
	ASSERT(self->magic == LAP_MAGIC, return;);

	if (status) {
		self->media_busy = TRUE;
		irlap_start_mbusy_timer(self);
		IRDA_DEBUG( 4, "Media busy!\n");
	} else {
		self->media_busy = FALSE;
		del_timer(&self->media_busy_timer);
	}
}

int irda_device_set_dtr_rts(struct net_device *dev, int dtr, int rts)
{	
	struct if_irda_req req;
	int ret;

	IRDA_DEBUG(2, __FUNCTION__ "()\n");

	if (!dev->do_ioctl) {
		ERROR(__FUNCTION__ "(), do_ioctl not impl. by "
		      "device driver\n");
		return -1;
	}

	req.ifr_dtr = dtr;
	req.ifr_rts = rts;

	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSDTRRTS);

	return ret;
}

int irda_device_change_speed(struct net_device *dev, __u32 speed)
{	
	struct if_irda_req req;
	int ret;

	IRDA_DEBUG(2, __FUNCTION__ "()\n");

	if (!dev->do_ioctl) {
		ERROR(__FUNCTION__ "(), do_ioctl not impl. by "
		      "device driver\n");
		return -1;
	}

	req.ifr_baudrate = speed;

	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSBANDWIDTH);

	return ret;
}

/*
 * Function irda_device_is_receiving (dev)
 *
 *    Check if the device driver is currently receiving data
 *
 */
int irda_device_is_receiving(struct net_device *dev)
{
	struct if_irda_req req;
	int ret;

	IRDA_DEBUG(2, __FUNCTION__ "()\n");

	if (!dev->do_ioctl) {
		ERROR(__FUNCTION__ "(), do_ioctl not impl. by "
		      "device driver\n");
		return -1;
	}

	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
	if (ret < 0)
		return ret;

	return req.ifr_receiving;
}

void irda_task_next_state(struct irda_task *task, TASK_STATE state)
{
	IRDA_DEBUG(2, __FUNCTION__ "(), state = %s\n", task_state[state]);

	task->state = state;
}

static void __irda_task_delete(struct irda_task *task)
{
	del_timer(&task->timer);
	
	kfree(task);
}

void irda_task_delete(struct irda_task *task)
{
	/* Unregister task */
	hashbin_remove(tasks, (int) task, NULL);

	__irda_task_delete(task);
}

/*
 * Function irda_task_kick (task)
 *
 *    Tries to execute a task possible multiple times until the task is either
 *    finished, or askes for a timeout. When a task is finished, we do post
 *    processing, and notify the parent task, that is waiting for this task
 *    to complete.
 */
int irda_task_kick(struct irda_task *task)
{
	int finished = TRUE;
	int count = 0;
	int timeout;

	IRDA_DEBUG(2, __FUNCTION__ "()\n");

	ASSERT(task != NULL, return -1;);
	ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);

	/* Execute task until it's finished, or askes for a timeout */
	do {
		timeout = task->function(task);
		if (count++ > 100) {
			ERROR(__FUNCTION__ "(), error in task handler!\n");
			irda_task_delete(task);
			return TRUE;
		}			
	} while ((timeout == 0) && (task->state != IRDA_TASK_DONE));

	if (timeout < 0) {
		ERROR(__FUNCTION__ "(), Error executing task!\n");
		irda_task_delete(task);
		return TRUE;
	}

	/* Check if we are finished */
	if (task->state == IRDA_TASK_DONE) {
		del_timer(&task->timer);

		/* Do post processing */
		if (task->finished)
			task->finished(task);

		/* Notify parent */
		if (task->parent) {
			/* Check if parent is waiting for us to complete */
			if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
				task->parent->state = IRDA_TASK_CHILD_DONE;

				/* Stop timer now that we are here */
				del_timer(&task->parent->timer);

				/* Kick parent task */
				irda_task_kick(task->parent);
			}
		}		
		irda_task_delete(task);
	} else if (timeout > 0) {
		irda_start_timer(&task->timer, timeout, (void *) task, 
				 irda_task_timer_expired);
		finished = FALSE;
	} else {
		IRDA_DEBUG(0, __FUNCTION__ 
			   "(), not finished, and no timeout!\n");
		finished = FALSE;
	}

	return finished;
}

/*
 * Function irda_task_execute (instance, function, finished)
 *
 *    This function registers and tries to execute tasks that may take some
 *    time to complete. We do it this hairy way since we may have been
 *    called from interrupt context, so it's not possible to use
 *    schedule_timeout() 
 */
struct irda_task *irda_task_execute(void *instance, TASK_CALLBACK function, 
				    TASK_CALLBACK finished, 
				    struct irda_task *parent, void *param)
{
	struct irda_task *task;
	int ret;

	IRDA_DEBUG(2, __FUNCTION__ "()\n");

	task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
	if (!task)
		return NULL;

	task->state    = IRDA_TASK_INIT;
	task->instance = instance;
	task->function = function;
	task->finished = finished;
	task->parent   = parent;
	task->param    = param;	
	task->magic    = IRDA_TASK_MAGIC;

	init_timer(&task->timer);

	/* Register task */
	hashbin_insert(tasks, (queue_t *) task, (int) task, NULL);

	/* No time to waste, so lets get going! */
	ret = irda_task_kick(task);
	if (ret)
		return NULL;
	else
		return task;
}

/*
 * Function irda_task_timer_expired (data)
 *
 *    Task time has expired. We now try to execute task (again), and restart
 *    the timer if the task has not finished yet
 */
static void irda_task_timer_expired(void *data)
{
	struct irda_task *task;

	IRDA_DEBUG(2, __FUNCTION__ "()\n");

	task = (struct irda_task *) data;

	irda_task_kick(task);
}

/*
 * Function irda_device_setup (dev)
 *
 *    This function should be used by low level device drivers in a similar way
 *    as ether_setup() is used by normal network device drivers
 */
int irda_device_setup(struct net_device *dev) 
{
	ASSERT(dev != NULL, return -1;);

        dev->hard_header_len = 0;
        dev->addr_len        = 0;

	dev->new_style       = 1;
	/* dev->destructor      = irda_device_destructor; */

        dev->type            = ARPHRD_IRDA;
        dev->tx_queue_len    = 8; /* Window size + 1 s-frame */
 
	memset(dev->broadcast, 0xff, 4);

	dev->mtu = 2048;
	dev_init_buffers(dev);
	dev->flags = IFF_NOARP;
	return 0;
}

/*
 * Function irda_device_txqueue_empty (dev)
 *
 *    Check if there is still some frames in the transmit queue for this
 *    device. Maybe we should use: q->q.qlen == 0.
 *
 */
int irda_device_txqueue_empty(struct net_device *dev)
{
	if (skb_queue_len(&dev->qdisc->q))
		return FALSE;

	return TRUE;
}

/*
 * Function irda_device_init_dongle (self, type, qos)
 *
 *    Initialize attached dongle.
 */
dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
{
	struct dongle_reg *reg;
	char modname[32];
	dongle_t *dongle;

	ASSERT(dev != NULL, return NULL;);

#ifdef CONFIG_KMOD
	/* Try to load the module needed */
	sprintf(modname, "irda-dongle-%d", type);
	request_module(modname);
#endif /* CONFIG_KMOD */

	if (!(reg = hashbin_find(dongles, type, NULL))) {
		ERROR("IrDA: Unable to find requested dongle\n");
		return NULL;
	}

	/* Allocate dongle info for this instance */
	dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
	if (!dongle)
		return NULL;

	memset(dongle, 0, sizeof(dongle_t));

	/* Bind the registration info to this particular instance */
	dongle->issue = reg;
	dongle->dev = dev;

	return dongle;
}

/*
 * Function irda_device_dongle_cleanup (dongle)
 *
 *    
 *
 */
int irda_device_dongle_cleanup(dongle_t *dongle)
{
	ASSERT(dongle != NULL, return -1;);

	dongle->issue->close(dongle);

	kfree(dongle);

	return 0;
}

/*
 * Function irda_device_register_dongle (dongle)
 *
 *    
 *
 */
int irda_device_register_dongle(struct dongle_reg *new)
{
	/* Check if this dongle has been registred before */
	if (hashbin_find(dongles, new->type, NULL)) {
		MESSAGE(__FUNCTION__ "(), Dongle already registered\n");
                return 0;
        }
	
	/* Insert IrDA dongle into hashbin */
	hashbin_insert(dongles, (queue_t *) new, new->type, NULL);
	
        return 0;
}

/*
 * Function irda_device_unregister_dongle (dongle)
 *
 *    Unregister dongle, and remove dongle from list of registred dongles
 *
 */
void irda_device_unregister_dongle(struct dongle_reg *dongle)
{
	struct dongle *node;

	node = hashbin_remove(dongles, dongle->type, NULL);
	if (!node) {
		ERROR(__FUNCTION__ "(), dongle not found!\n");
		return;
	}
}

/*
 * Function irda_device_set_mode (self, mode)
 *
 *    Set the Infrared device driver into mode where it sends and receives
 *    data without using IrLAP framing. Check out the particular device
 *    driver to find out which modes it support.
 */
int irda_device_set_mode(struct net_device* dev, int mode)
{	
	struct if_irda_req req;
	int ret;

	IRDA_DEBUG(0, __FUNCTION__ "()\n");

	if (!dev->do_ioctl) {
		ERROR(__FUNCTION__ "(), set_raw_mode not impl. by "
		      "device driver\n");
		return -1;
	}
	
	req.ifr_mode = mode;

	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSMODE);
	
	return ret;
}

/*
 * Function setup_dma (idev, buffer, count, mode)
 *
 *    Setup the DMA channel. Commonly used by ISA FIR drivers
 *
 */
void setup_dma(int channel, char *buffer, int count, int mode)
{
	unsigned long flags;
	
	flags = claim_dma_lock();
	
	disable_dma(channel);
	clear_dma_ff(channel);
	set_dma_mode(channel, mode);
	set_dma_addr(channel, virt_to_bus(buffer));
	set_dma_count(channel, count);
	enable_dma(channel);

	release_dma_lock(flags);
}