1 /*********************************************************************
2  *
3  * Filename:      irda_device.c
4  * Version:       0.9
5  * Description:   Utility functions used by the device drivers
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sat Oct  9 09:22:27 1999
9  * Modified at:   Sun Jan 23 17:41:24 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  *
12  *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
13  *     Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public License
26  *     along with this program; if not, write to the Free Software
27  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  *     MA 02111-1307 USA
29  *
30  ********************************************************************/
31 
32 #include <linux/config.h>
33 #include <linux/string.h>
34 #include <linux/proc_fs.h>
35 #include <linux/skbuff.h>
36 #include <linux/if.h>
37 #include <linux/if_ether.h>
38 #include <linux/if_arp.h>
39 #include <linux/netdevice.h>
40 #include <linux/init.h>
41 #include <linux/tty.h>
42 #include <linux/kmod.h>
43 #include <linux/wireless.h>
44 #include <linux/spinlock.h>
45 
46 #include <asm/ioctls.h>
47 #include <asm/segment.h>
48 #include <asm/uaccess.h>
49 #include <asm/dma.h>
50 #include <asm/io.h>
51 
52 #include <net/pkt_sched.h>
53 
54 #include <net/irda/irda_device.h>
55 #include <net/irda/irlap.h>
56 #include <net/irda/timer.h>
57 #include <net/irda/wrapper.h>
58 
59 extern int irtty_init(void);
60 extern int nsc_ircc_init(void);
61 extern int ircc_init(void);
62 extern int toshoboe_init(void);
63 extern int litelink_init(void);
64 extern int w83977af_init(void);
65 extern int esi_init(void);
66 extern int tekram_init(void);
67 extern int actisys_init(void);
68 extern int girbil_init(void);
69 extern int sa1100_irda_init(void);
70 extern int ep7211_ir_init(void);
71 extern int mcp2120_init(void);
72 
73 static void __irda_task_delete(struct irda_task *task);
74 
75 static hashbin_t *dongles = NULL;
76 static hashbin_t *tasks = NULL;
77 
78 const char *infrared_mode[] = {
79 	"IRDA_IRLAP",
80 	"IRDA_RAW",
81 	"SHARP_ASK",
82 	"TV_REMOTE",
83 };
84 
85 #ifdef CONFIG_IRDA_DEBUG
86 static const char *task_state[] = {
87 	"IRDA_TASK_INIT",
88 	"IRDA_TASK_DONE",
89 	"IRDA_TASK_WAIT",
90 	"IRDA_TASK_WAIT1",
91 	"IRDA_TASK_WAIT2",
92 	"IRDA_TASK_WAIT3",
93 	"IRDA_TASK_CHILD_INIT",
94 	"IRDA_TASK_CHILD_WAIT",
95 	"IRDA_TASK_CHILD_DONE",
96 };
97 #endif	/* CONFIG_IRDA_DEBUG */
98 
99 static void irda_task_timer_expired(void *data);
100 
101 #ifdef CONFIG_PROC_FS
102 int irda_device_proc_read(char *buf, char **start, off_t offset, int len,
103 			  int unused);
104 
105 #endif /* CONFIG_PROC_FS */
106 
irda_device_init(void)107 int __init irda_device_init( void)
108 {
109 	dongles = hashbin_new(HB_GLOBAL);
110 	if (dongles == NULL) {
111 		printk(KERN_WARNING
112 		       "IrDA: Can't allocate dongles hashbin!\n");
113 		return -ENOMEM;
114 	}
115 
116 	tasks = hashbin_new(HB_GLOBAL);
117 	if (tasks == NULL) {
118 		printk(KERN_WARNING
119 		       "IrDA: Can't allocate tasks hashbin!\n");
120 		return -ENOMEM;
121 	}
122 
123 	/*
124 	 * Call the init function of the device drivers that has not been
125 	 * compiled as a module
126 	 * Note : non-modular IrDA is not supported in 2.4.X, so don't
127 	 * waste too much time fixing this code. If you require it, please
128 	 * upgrade to the IrDA stack in 2.5.X. Jean II
129 	 */
130 #ifdef CONFIG_IRTTY_SIR
131 	irtty_init();
132 #endif
133 #ifdef CONFIG_WINBOND_FIR
134 	w83977af_init();
135 #endif
136 #ifdef CONFIG_SA1100_FIR
137 	sa1100_irda_init();
138 #endif
139 #ifdef CONFIG_NSC_FIR
140 	nsc_ircc_init();
141 #endif
142 #ifdef CONFIG_TOSHIBA_OLD
143 	toshoboe_init();
144 #endif
145 #ifdef CONFIG_SMC_IRCC_FIR
146 	ircc_init();
147 #endif
148 #ifdef CONFIG_ESI_DONGLE
149 	esi_init();
150 #endif
151 #ifdef CONFIG_TEKRAM_DONGLE
152 	tekram_init();
153 #endif
154 #ifdef CONFIG_ACTISYS_DONGLE
155 	actisys_init();
156 #endif
157 #ifdef CONFIG_GIRBIL_DONGLE
158 	girbil_init();
159 #endif
160 #ifdef CONFIG_LITELINK_DONGLE
161 	litelink_init();
162 #endif
163 #ifdef CONFIG_OLD_BELKIN
164  	old_belkin_init();
165 #endif
166 #ifdef CONFIG_EP7211_IR
167  	ep7211_ir_init();
168 #endif
169 #ifdef CONFIG_MCP2120_DONGLE
170 	mcp2120_init();
171 #endif
172 	return 0;
173 }
174 
irda_device_cleanup(void)175 void irda_device_cleanup(void)
176 {
177 	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
178 
179 	hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
180 	hashbin_delete(dongles, NULL);
181 }
182 
183 /*
184  * Function irda_device_set_media_busy (self, status)
185  *
186  *    Called when we have detected that another station is transmiting
187  *    in contention mode.
188  */
irda_device_set_media_busy(struct net_device * dev,int status)189 void irda_device_set_media_busy(struct net_device *dev, int status)
190 {
191 	struct irlap_cb *self;
192 
193 	IRDA_DEBUG(4, "%s(%s)\n", __FUNCTION__, status ? "TRUE" : "FALSE");
194 
195 	self = (struct irlap_cb *) dev->atalk_ptr;
196 
197 	ASSERT(self != NULL, return;);
198 	ASSERT(self->magic == LAP_MAGIC, return;);
199 
200 	if (status) {
201 		self->media_busy = TRUE;
202 		if (status == SMALL)
203 			irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
204 		else
205 			irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
206 		IRDA_DEBUG( 4, "Media busy!\n");
207 	} else {
208 		self->media_busy = FALSE;
209 		irlap_stop_mbusy_timer(self);
210 	}
211 }
212 
irda_device_set_dtr_rts(struct net_device * dev,int dtr,int rts)213 int irda_device_set_dtr_rts(struct net_device *dev, int dtr, int rts)
214 {
215 	struct if_irda_req req;
216 	int ret;
217 
218 	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
219 
220 	if (!dev->do_ioctl) {
221 		ERROR("%s(), do_ioctl not impl. by "
222 		      "device driver\n", __FUNCTION__);
223 		return -1;
224 	}
225 
226 	req.ifr_dtr = dtr;
227 	req.ifr_rts = rts;
228 
229 	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSDTRRTS);
230 
231 	return ret;
232 }
233 
irda_device_change_speed(struct net_device * dev,__u32 speed)234 int irda_device_change_speed(struct net_device *dev, __u32 speed)
235 {
236 	struct if_irda_req req;
237 	int ret;
238 
239 	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
240 
241 	if (!dev->do_ioctl) {
242 		ERROR("%s(), do_ioctl not impl. by "
243 		      "device driver\n", __FUNCTION__);
244 		return -1;
245 	}
246 
247 	req.ifr_baudrate = speed;
248 
249 	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSBANDWIDTH);
250 
251 	return ret;
252 }
253 
254 /*
255  * Function irda_device_is_receiving (dev)
256  *
257  *    Check if the device driver is currently receiving data
258  *
259  */
irda_device_is_receiving(struct net_device * dev)260 int irda_device_is_receiving(struct net_device *dev)
261 {
262 	struct if_irda_req req;
263 	int ret;
264 
265 	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
266 
267 	if (!dev->do_ioctl) {
268 		ERROR("%s(), do_ioctl not impl. by "
269 		      "device driver\n", __FUNCTION__);
270 		return -1;
271 	}
272 
273 	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
274 	if (ret < 0)
275 		return ret;
276 
277 	return req.ifr_receiving;
278 }
279 
irda_task_next_state(struct irda_task * task,IRDA_TASK_STATE state)280 void irda_task_next_state(struct irda_task *task, IRDA_TASK_STATE state)
281 {
282 	IRDA_DEBUG(2, "%s(), state = %s\n", __FUNCTION__, task_state[state]);
283 
284 	task->state = state;
285 }
286 
__irda_task_delete(struct irda_task * task)287 static void __irda_task_delete(struct irda_task *task)
288 {
289 	del_timer(&task->timer);
290 
291 	kfree(task);
292 }
293 
irda_task_delete(struct irda_task * task)294 void irda_task_delete(struct irda_task *task)
295 {
296 	/* Unregister task */
297 	hashbin_remove(tasks, (int) task, NULL);
298 
299 	__irda_task_delete(task);
300 }
301 
302 /*
303  * Function irda_task_kick (task)
304  *
305  *    Tries to execute a task possible multiple times until the task is either
306  *    finished, or askes for a timeout. When a task is finished, we do post
307  *    processing, and notify the parent task, that is waiting for this task
308  *    to complete.
309  */
irda_task_kick(struct irda_task * task)310 int irda_task_kick(struct irda_task *task)
311 {
312 	int finished = TRUE;
313 	int count = 0;
314 	int timeout;
315 
316 	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
317 
318 	ASSERT(task != NULL, return -1;);
319 	ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
320 
321 	/* Execute task until it's finished, or askes for a timeout */
322 	do {
323 		timeout = task->function(task);
324 		if (count++ > 100) {
325 			ERROR("%s(), error in task handler!\n", __FUNCTION__);
326 			irda_task_delete(task);
327 			return TRUE;
328 		}
329 	} while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
330 
331 	if (timeout < 0) {
332 		ERROR("%s(), Error executing task!\n", __FUNCTION__);
333 		irda_task_delete(task);
334 		return TRUE;
335 	}
336 
337 	/* Check if we are finished */
338 	if (task->state == IRDA_TASK_DONE) {
339 		del_timer(&task->timer);
340 
341 		/* Do post processing */
342 		if (task->finished)
343 			task->finished(task);
344 
345 		/* Notify parent */
346 		if (task->parent) {
347 			/* Check if parent is waiting for us to complete */
348 			if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
349 				task->parent->state = IRDA_TASK_CHILD_DONE;
350 
351 				/* Stop timer now that we are here */
352 				del_timer(&task->parent->timer);
353 
354 				/* Kick parent task */
355 				irda_task_kick(task->parent);
356 			}
357 		}
358 		irda_task_delete(task);
359 	} else if (timeout > 0) {
360 		irda_start_timer(&task->timer, timeout, (void *) task,
361 				 irda_task_timer_expired);
362 		finished = FALSE;
363 	} else {
364 		IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n", __FUNCTION__);
365 		finished = FALSE;
366 	}
367 
368 	return finished;
369 }
370 
371 /*
372  * Function irda_task_execute (instance, function, finished)
373  *
374  *    This function registers and tries to execute tasks that may take some
375  *    time to complete. We do it this hairy way since we may have been
376  *    called from interrupt context, so it's not possible to use
377  *    schedule_timeout()
378  * Two important notes :
379  *	o Make sure you irda_task_delete(task); in case you delete the
380  *	  calling instance.
381  *	o No real need to lock when calling this function, but you may
382  *	  want to lock within the task handler.
383  * Jean II
384  */
irda_task_execute(void * instance,IRDA_TASK_CALLBACK function,IRDA_TASK_CALLBACK finished,struct irda_task * parent,void * param)385 struct irda_task *irda_task_execute(void *instance,
386 				    IRDA_TASK_CALLBACK function,
387 				    IRDA_TASK_CALLBACK finished,
388 				    struct irda_task *parent, void *param)
389 {
390 	struct irda_task *task;
391 	int ret;
392 
393 	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
394 
395 	task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
396 	if (!task)
397 		return NULL;
398 
399 	task->state    = IRDA_TASK_INIT;
400 	task->instance = instance;
401 	task->function = function;
402 	task->finished = finished;
403 	task->parent   = parent;
404 	task->param    = param;
405 	task->magic    = IRDA_TASK_MAGIC;
406 
407 	init_timer(&task->timer);
408 
409 	/* Register task */
410 	hashbin_insert(tasks, (irda_queue_t *) task, (int) task, NULL);
411 
412 	/* No time to waste, so lets get going! */
413 	ret = irda_task_kick(task);
414 	if (ret)
415 		return NULL;
416 	else
417 		return task;
418 }
419 
420 /*
421  * Function irda_task_timer_expired (data)
422  *
423  *    Task time has expired. We now try to execute task (again), and restart
424  *    the timer if the task has not finished yet
425  */
irda_task_timer_expired(void * data)426 static void irda_task_timer_expired(void *data)
427 {
428 	struct irda_task *task;
429 
430 	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
431 
432 	task = (struct irda_task *) data;
433 
434 	irda_task_kick(task);
435 }
436 
437 /*
438  * Function irda_device_setup (dev)
439  *
440  *    This function should be used by low level device drivers in a similar way
441  *    as ether_setup() is used by normal network device drivers
442  */
irda_device_setup(struct net_device * dev)443 int irda_device_setup(struct net_device *dev)
444 {
445 	ASSERT(dev != NULL, return -1;);
446 
447         dev->hard_header_len = 0;
448         dev->addr_len        = 0;
449 
450 	dev->features        |= NETIF_F_DYNALLOC;
451 	/* dev->destructor      = irda_device_destructor; */
452 
453         dev->type            = ARPHRD_IRDA;
454         dev->tx_queue_len    = 8; /* Window size + 1 s-frame */
455 
456 	memset(dev->broadcast, 0xff, 4);
457 
458 	dev->mtu = 2048;
459 	dev->flags = IFF_NOARP;
460 	return 0;
461 }
462 
463 /*
464  * Function irda_device_txqueue_empty (dev)
465  *
466  *    Check if there is still some frames in the transmit queue for this
467  *    device. Maybe we should use: q->q.qlen == 0.
468  *
469  */
irda_device_txqueue_empty(struct net_device * dev)470 int irda_device_txqueue_empty(struct net_device *dev)
471 {
472 	if (skb_queue_len(&dev->qdisc->q))
473 		return FALSE;
474 
475 	return TRUE;
476 }
477 
478 /*
479  * Function irda_device_init_dongle (self, type, qos)
480  *
481  *    Initialize attached dongle.
482  *
483  * Important : request_module require us to call this function with
484  * a process context and irq enabled. - Jean II
485  */
irda_device_dongle_init(struct net_device * dev,int type)486 dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
487 {
488 	struct dongle_reg *reg;
489 	dongle_t *dongle;
490 
491 	ASSERT(dev != NULL, return NULL;);
492 
493 #ifdef CONFIG_KMOD
494 	{
495 	char modname[32];
496 	ASSERT(!in_interrupt(), return NULL;);
497 	/* Try to load the module needed */
498 	sprintf(modname, "irda-dongle-%d", type);
499 	request_module(modname);
500 	}
501 #endif /* CONFIG_KMOD */
502 
503 	if (!(reg = hashbin_find(dongles, type, NULL))) {
504 		ERROR("IrDA: Unable to find requested dongle\n");
505 		return NULL;
506 	}
507 
508 	/* Allocate dongle info for this instance */
509 	dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
510 	if (!dongle)
511 		return NULL;
512 
513 	memset(dongle, 0, sizeof(dongle_t));
514 
515 	/* Bind the registration info to this particular instance */
516 	dongle->issue = reg;
517 	dongle->dev = dev;
518 
519 	return dongle;
520 }
521 
522 /*
523  * Function irda_device_dongle_cleanup (dongle)
524  *
525  *
526  *
527  */
irda_device_dongle_cleanup(dongle_t * dongle)528 int irda_device_dongle_cleanup(dongle_t *dongle)
529 {
530 	ASSERT(dongle != NULL, return -1;);
531 
532 	dongle->issue->close(dongle);
533 
534 	kfree(dongle);
535 
536 	return 0;
537 }
538 
539 /*
540  * Function irda_device_register_dongle (dongle)
541  *
542  *
543  *
544  */
irda_device_register_dongle(struct dongle_reg * new)545 int irda_device_register_dongle(struct dongle_reg *new)
546 {
547 	/* Check if this dongle has been registred before */
548 	if (hashbin_find(dongles, new->type, NULL)) {
549 		MESSAGE("%s(), Dongle already registered\n", __FUNCTION__);
550                 return 0;
551         }
552 
553 	/* Insert IrDA dongle into hashbin */
554 	hashbin_insert(dongles, (irda_queue_t *) new, new->type, NULL);
555 
556         return 0;
557 }
558 
559 /*
560  * Function irda_device_unregister_dongle (dongle)
561  *
562  *    Unregister dongle, and remove dongle from list of registred dongles
563  *
564  */
irda_device_unregister_dongle(struct dongle_reg * dongle)565 void irda_device_unregister_dongle(struct dongle_reg *dongle)
566 {
567 	struct dongle *node;
568 
569 	node = hashbin_remove(dongles, dongle->type, NULL);
570 	if (!node) {
571 		ERROR("%s(), dongle not found!\n", __FUNCTION__);
572 		return;
573 	}
574 }
575 
576 /*
577  * Function irda_device_set_mode (self, mode)
578  *
579  *    Set the Infrared device driver into mode where it sends and receives
580  *    data without using IrLAP framing. Check out the particular device
581  *    driver to find out which modes it support.
582  */
irda_device_set_mode(struct net_device * dev,int mode)583 int irda_device_set_mode(struct net_device* dev, int mode)
584 {
585 	struct if_irda_req req;
586 	int ret;
587 
588 	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
589 
590 	if (!dev->do_ioctl) {
591 		ERROR("%s(), set_raw_mode not impl. by "
592 		      "device driver\n", __FUNCTION__);
593 		return -1;
594 	}
595 
596 	req.ifr_mode = mode;
597 
598 	ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSMODE);
599 
600 	return ret;
601 }
602 
603 /*
604  * Function setup_dma (idev, buffer, count, mode)
605  *
606  *    Setup the DMA channel. Commonly used by ISA FIR drivers
607  *
608  */
setup_dma(int channel,char * buffer,int count,int mode)609 void setup_dma(int channel, char *buffer, int count, int mode)
610 {
611 	unsigned long flags;
612 
613 	flags = claim_dma_lock();
614 
615 	disable_dma(channel);
616 	clear_dma_ff(channel);
617 	set_dma_mode(channel, mode);
618 	set_dma_addr(channel, virt_to_bus(buffer));
619 	set_dma_count(channel, count);
620 	enable_dma(channel);
621 
622 	release_dma_lock(flags);
623 }
624