1 /*********************************************************************
3 * Filename: irda_device.c
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>
12 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
13 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
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.
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.
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,
30 ********************************************************************/
32 #include <linux/config.h>
33 #include <linux/string.h>
34 #include <linux/proc_fs.h>
35 #include <linux/skbuff.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>
46 #include <asm/ioctls.h>
47 #include <asm/segment.h>
48 #include <asm/uaccess.h>
52 #include <net/pkt_sched.h>
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>
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);
72 static void __irda_task_delete(struct irda_task *task);
74 static hashbin_t *dongles = NULL;
75 static hashbin_t *tasks = NULL;
77 const char *infrared_mode[] = {
84 #ifdef CONFIG_IRDA_DEBUG
85 static const char *task_state[] = {
92 "IRDA_TASK_CHILD_INIT",
93 "IRDA_TASK_CHILD_WAIT",
94 "IRDA_TASK_CHILD_DONE",
96 #endif /* CONFIG_IRDA_DEBUG */
98 static void irda_task_timer_expired(void *data);
100 #ifdef CONFIG_PROC_FS
101 int irda_device_proc_read(char *buf, char **start, off_t offset, int len,
104 #endif /* CONFIG_PROC_FS */
106 int __init irda_device_init( void)
108 dongles = hashbin_new(HB_GLOBAL);
109 if (dongles == NULL) {
111 "IrDA: Can't allocate dongles hashbin!\n");
115 tasks = hashbin_new(HB_GLOBAL);
118 "IrDA: Can't allocate tasks hashbin!\n");
123 * Call the init function of the device drivers that has not been
124 * compiled as a module
126 #ifdef CONFIG_IRTTY_SIR
129 #ifdef CONFIG_WINBOND_FIR
132 #ifdef CONFIG_SA1100_FIR
135 #ifdef CONFIG_NSC_FIR
138 #ifdef CONFIG_TOSHIBA_FIR
141 #ifdef CONFIG_SMC_IRCC_FIR
144 #ifdef CONFIG_ESI_DONGLE
147 #ifdef CONFIG_TEKRAM_DONGLE
150 #ifdef CONFIG_ACTISYS_DONGLE
153 #ifdef CONFIG_GIRBIL_DONGLE
156 #ifdef CONFIG_LITELINK_DONGLE
159 #ifdef CONFIG_OLD_BELKIN
162 #ifdef CONFIG_EP7211_IR
168 void irda_device_cleanup(void)
170 IRDA_DEBUG(4, __FUNCTION__ "()\n");
172 hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
173 hashbin_delete(dongles, NULL);
177 * Function irda_device_set_media_busy (self, status)
179 * Called when we have detected that another station is transmiting
180 * in contention mode.
182 void irda_device_set_media_busy(struct net_device *dev, int status)
184 struct irlap_cb *self;
186 IRDA_DEBUG(4, __FUNCTION__ "(%s)\n", status ? "TRUE" : "FALSE");
188 self = (struct irlap_cb *) dev->atalk_ptr;
190 ASSERT(self != NULL, return;);
191 ASSERT(self->magic == LAP_MAGIC, return;);
194 self->media_busy = TRUE;
196 irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
198 irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
199 IRDA_DEBUG( 4, "Media busy!\n");
201 self->media_busy = FALSE;
202 irlap_stop_mbusy_timer(self);
206 int irda_device_set_dtr_rts(struct net_device *dev, int dtr, int rts)
208 struct if_irda_req req;
211 IRDA_DEBUG(2, __FUNCTION__ "()\n");
213 if (!dev->do_ioctl) {
214 ERROR(__FUNCTION__ "(), do_ioctl not impl. by "
222 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSDTRRTS);
227 int irda_device_change_speed(struct net_device *dev, __u32 speed)
229 struct if_irda_req req;
232 IRDA_DEBUG(2, __FUNCTION__ "()\n");
234 if (!dev->do_ioctl) {
235 ERROR(__FUNCTION__ "(), do_ioctl not impl. by "
240 req.ifr_baudrate = speed;
242 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSBANDWIDTH);
248 * Function irda_device_is_receiving (dev)
250 * Check if the device driver is currently receiving data
253 int irda_device_is_receiving(struct net_device *dev)
255 struct if_irda_req req;
258 IRDA_DEBUG(2, __FUNCTION__ "()\n");
260 if (!dev->do_ioctl) {
261 ERROR(__FUNCTION__ "(), do_ioctl not impl. by "
266 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
270 return req.ifr_receiving;
273 void irda_task_next_state(struct irda_task *task, IRDA_TASK_STATE state)
275 IRDA_DEBUG(2, __FUNCTION__ "(), state = %s\n", task_state[state]);
280 static void __irda_task_delete(struct irda_task *task)
282 del_timer(&task->timer);
287 void irda_task_delete(struct irda_task *task)
289 /* Unregister task */
290 hashbin_remove(tasks, (int) task, NULL);
292 __irda_task_delete(task);
296 * Function irda_task_kick (task)
298 * Tries to execute a task possible multiple times until the task is either
299 * finished, or askes for a timeout. When a task is finished, we do post
300 * processing, and notify the parent task, that is waiting for this task
303 int irda_task_kick(struct irda_task *task)
309 IRDA_DEBUG(2, __FUNCTION__ "()\n");
311 ASSERT(task != NULL, return -1;);
312 ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
314 /* Execute task until it's finished, or askes for a timeout */
316 timeout = task->function(task);
318 ERROR(__FUNCTION__ "(), error in task handler!\n");
319 irda_task_delete(task);
322 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
325 ERROR(__FUNCTION__ "(), Error executing task!\n");
326 irda_task_delete(task);
330 /* Check if we are finished */
331 if (task->state == IRDA_TASK_DONE) {
332 del_timer(&task->timer);
334 /* Do post processing */
336 task->finished(task);
340 /* Check if parent is waiting for us to complete */
341 if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
342 task->parent->state = IRDA_TASK_CHILD_DONE;
344 /* Stop timer now that we are here */
345 del_timer(&task->parent->timer);
347 /* Kick parent task */
348 irda_task_kick(task->parent);
351 irda_task_delete(task);
352 } else if (timeout > 0) {
353 irda_start_timer(&task->timer, timeout, (void *) task,
354 irda_task_timer_expired);
357 IRDA_DEBUG(0, __FUNCTION__
358 "(), not finished, and no timeout!\n");
366 * Function irda_task_execute (instance, function, finished)
368 * This function registers and tries to execute tasks that may take some
369 * time to complete. We do it this hairy way since we may have been
370 * called from interrupt context, so it's not possible to use
372 * Two important notes :
373 * o Make sure you irda_task_delete(task); in case you delete the
375 * o No real need to lock when calling this function, but you may
376 * want to lock within the task handler.
379 struct irda_task *irda_task_execute(void *instance,
380 IRDA_TASK_CALLBACK function,
381 IRDA_TASK_CALLBACK finished,
382 struct irda_task *parent, void *param)
384 struct irda_task *task;
387 IRDA_DEBUG(2, __FUNCTION__ "()\n");
389 task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
393 task->state = IRDA_TASK_INIT;
394 task->instance = instance;
395 task->function = function;
396 task->finished = finished;
397 task->parent = parent;
399 task->magic = IRDA_TASK_MAGIC;
401 init_timer(&task->timer);
404 hashbin_insert(tasks, (irda_queue_t *) task, (int) task, NULL);
406 /* No time to waste, so lets get going! */
407 ret = irda_task_kick(task);
415 * Function irda_task_timer_expired (data)
417 * Task time has expired. We now try to execute task (again), and restart
418 * the timer if the task has not finished yet
420 static void irda_task_timer_expired(void *data)
422 struct irda_task *task;
424 IRDA_DEBUG(2, __FUNCTION__ "()\n");
426 task = (struct irda_task *) data;
428 irda_task_kick(task);
432 * Function irda_device_setup (dev)
434 * This function should be used by low level device drivers in a similar way
435 * as ether_setup() is used by normal network device drivers
437 int irda_device_setup(struct net_device *dev)
439 ASSERT(dev != NULL, return -1;);
441 dev->hard_header_len = 0;
444 dev->features |= NETIF_F_DYNALLOC;
445 /* dev->destructor = irda_device_destructor; */
447 dev->type = ARPHRD_IRDA;
448 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
450 memset(dev->broadcast, 0xff, 4);
453 dev->flags = IFF_NOARP;
458 * Function irda_device_txqueue_empty (dev)
460 * Check if there is still some frames in the transmit queue for this
461 * device. Maybe we should use: q->q.qlen == 0.
464 int irda_device_txqueue_empty(struct net_device *dev)
466 if (skb_queue_len(&dev->qdisc->q))
473 * Function irda_device_init_dongle (self, type, qos)
475 * Initialize attached dongle.
477 * Important : request_module require us to call this function with
478 * a process context and irq enabled. - Jean II
480 dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
482 struct dongle_reg *reg;
485 ASSERT(dev != NULL, return NULL;);
490 ASSERT(!in_interrupt(), return NULL;);
491 /* Try to load the module needed */
492 sprintf(modname, "irda-dongle-%d", type);
493 request_module(modname);
495 #endif /* CONFIG_KMOD */
497 if (!(reg = hashbin_find(dongles, type, NULL))) {
498 ERROR("IrDA: Unable to find requested dongle\n");
502 /* Allocate dongle info for this instance */
503 dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
507 memset(dongle, 0, sizeof(dongle_t));
509 /* Bind the registration info to this particular instance */
517 * Function irda_device_dongle_cleanup (dongle)
522 int irda_device_dongle_cleanup(dongle_t *dongle)
524 ASSERT(dongle != NULL, return -1;);
526 dongle->issue->close(dongle);
534 * Function irda_device_register_dongle (dongle)
539 int irda_device_register_dongle(struct dongle_reg *new)
541 /* Check if this dongle has been registred before */
542 if (hashbin_find(dongles, new->type, NULL)) {
543 MESSAGE(__FUNCTION__ "(), Dongle already registered\n");
547 /* Insert IrDA dongle into hashbin */
548 hashbin_insert(dongles, (irda_queue_t *) new, new->type, NULL);
554 * Function irda_device_unregister_dongle (dongle)
556 * Unregister dongle, and remove dongle from list of registred dongles
559 void irda_device_unregister_dongle(struct dongle_reg *dongle)
563 node = hashbin_remove(dongles, dongle->type, NULL);
565 ERROR(__FUNCTION__ "(), dongle not found!\n");
571 * Function irda_device_set_mode (self, mode)
573 * Set the Infrared device driver into mode where it sends and receives
574 * data without using IrLAP framing. Check out the particular device
575 * driver to find out which modes it support.
577 int irda_device_set_mode(struct net_device* dev, int mode)
579 struct if_irda_req req;
582 IRDA_DEBUG(0, __FUNCTION__ "()\n");
584 if (!dev->do_ioctl) {
585 ERROR(__FUNCTION__ "(), set_raw_mode not impl. by "
592 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSMODE);
598 * Function setup_dma (idev, buffer, count, mode)
600 * Setup the DMA channel. Commonly used by ISA FIR drivers
603 void setup_dma(int channel, char *buffer, int count, int mode)
607 flags = claim_dma_lock();
609 disable_dma(channel);
610 clear_dma_ff(channel);
611 set_dma_mode(channel, mode);
612 set_dma_addr(channel, virt_to_bus(buffer));
613 set_dma_count(channel, count);
616 release_dma_lock(flags);