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);
71 extern int mcp2120_init(void);
73 static void __irda_task_delete(struct irda_task *task);
75 static hashbin_t *dongles = NULL;
76 static hashbin_t *tasks = NULL;
78 const char *infrared_mode[] = {
85 #ifdef CONFIG_IRDA_DEBUG
86 static const char *task_state[] = {
93 "IRDA_TASK_CHILD_INIT",
94 "IRDA_TASK_CHILD_WAIT",
95 "IRDA_TASK_CHILD_DONE",
97 #endif /* CONFIG_IRDA_DEBUG */
99 static void irda_task_timer_expired(void *data);
101 #ifdef CONFIG_PROC_FS
102 int irda_device_proc_read(char *buf, char **start, off_t offset, int len,
105 #endif /* CONFIG_PROC_FS */
107 int __init irda_device_init( void)
109 dongles = hashbin_new(HB_GLOBAL);
110 if (dongles == NULL) {
112 "IrDA: Can't allocate dongles hashbin!\n");
116 tasks = hashbin_new(HB_GLOBAL);
119 "IrDA: Can't allocate tasks hashbin!\n");
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
130 #ifdef CONFIG_IRTTY_SIR
133 #ifdef CONFIG_WINBOND_FIR
136 #ifdef CONFIG_SA1100_FIR
139 #ifdef CONFIG_NSC_FIR
142 #ifdef CONFIG_TOSHIBA_OLD
145 #ifdef CONFIG_SMC_IRCC_FIR
148 #ifdef CONFIG_ESI_DONGLE
151 #ifdef CONFIG_TEKRAM_DONGLE
154 #ifdef CONFIG_ACTISYS_DONGLE
157 #ifdef CONFIG_GIRBIL_DONGLE
160 #ifdef CONFIG_LITELINK_DONGLE
163 #ifdef CONFIG_OLD_BELKIN
166 #ifdef CONFIG_EP7211_IR
169 #ifdef CONFIG_MCP2120_DONGLE
175 void irda_device_cleanup(void)
177 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
179 hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
180 hashbin_delete(dongles, NULL);
184 * Function irda_device_set_media_busy (self, status)
186 * Called when we have detected that another station is transmiting
187 * in contention mode.
189 void irda_device_set_media_busy(struct net_device *dev, int status)
191 struct irlap_cb *self;
193 IRDA_DEBUG(4, "%s(%s)\n", __FUNCTION__, status ? "TRUE" : "FALSE");
195 self = (struct irlap_cb *) dev->atalk_ptr;
197 ASSERT(self != NULL, return;);
198 ASSERT(self->magic == LAP_MAGIC, return;);
201 self->media_busy = TRUE;
203 irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
205 irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
206 IRDA_DEBUG( 4, "Media busy!\n");
208 self->media_busy = FALSE;
209 irlap_stop_mbusy_timer(self);
213 int irda_device_set_dtr_rts(struct net_device *dev, int dtr, int rts)
215 struct if_irda_req req;
218 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
220 if (!dev->do_ioctl) {
221 ERROR("%s(), do_ioctl not impl. by "
222 "device driver\n", __FUNCTION__);
229 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSDTRRTS);
234 int irda_device_change_speed(struct net_device *dev, __u32 speed)
236 struct if_irda_req req;
239 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
241 if (!dev->do_ioctl) {
242 ERROR("%s(), do_ioctl not impl. by "
243 "device driver\n", __FUNCTION__);
247 req.ifr_baudrate = speed;
249 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSBANDWIDTH);
255 * Function irda_device_is_receiving (dev)
257 * Check if the device driver is currently receiving data
260 int irda_device_is_receiving(struct net_device *dev)
262 struct if_irda_req req;
265 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
267 if (!dev->do_ioctl) {
268 ERROR("%s(), do_ioctl not impl. by "
269 "device driver\n", __FUNCTION__);
273 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
277 return req.ifr_receiving;
280 void irda_task_next_state(struct irda_task *task, IRDA_TASK_STATE state)
282 IRDA_DEBUG(2, "%s(), state = %s\n", __FUNCTION__, task_state[state]);
287 static void __irda_task_delete(struct irda_task *task)
289 del_timer(&task->timer);
294 void irda_task_delete(struct irda_task *task)
296 /* Unregister task */
297 hashbin_remove(tasks, (int) task, NULL);
299 __irda_task_delete(task);
303 * Function irda_task_kick (task)
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
310 int irda_task_kick(struct irda_task *task)
316 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
318 ASSERT(task != NULL, return -1;);
319 ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
321 /* Execute task until it's finished, or askes for a timeout */
323 timeout = task->function(task);
325 ERROR("%s(), error in task handler!\n", __FUNCTION__);
326 irda_task_delete(task);
329 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
332 ERROR("%s(), Error executing task!\n", __FUNCTION__);
333 irda_task_delete(task);
337 /* Check if we are finished */
338 if (task->state == IRDA_TASK_DONE) {
339 del_timer(&task->timer);
341 /* Do post processing */
343 task->finished(task);
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;
351 /* Stop timer now that we are here */
352 del_timer(&task->parent->timer);
354 /* Kick parent task */
355 irda_task_kick(task->parent);
358 irda_task_delete(task);
359 } else if (timeout > 0) {
360 irda_start_timer(&task->timer, timeout, (void *) task,
361 irda_task_timer_expired);
364 IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n", __FUNCTION__);
372 * Function irda_task_execute (instance, function, finished)
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
378 * Two important notes :
379 * o Make sure you irda_task_delete(task); in case you delete the
381 * o No real need to lock when calling this function, but you may
382 * want to lock within the task handler.
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)
390 struct irda_task *task;
393 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
395 task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
399 task->state = IRDA_TASK_INIT;
400 task->instance = instance;
401 task->function = function;
402 task->finished = finished;
403 task->parent = parent;
405 task->magic = IRDA_TASK_MAGIC;
407 init_timer(&task->timer);
410 hashbin_insert(tasks, (irda_queue_t *) task, (int) task, NULL);
412 /* No time to waste, so lets get going! */
413 ret = irda_task_kick(task);
421 * Function irda_task_timer_expired (data)
423 * Task time has expired. We now try to execute task (again), and restart
424 * the timer if the task has not finished yet
426 static void irda_task_timer_expired(void *data)
428 struct irda_task *task;
430 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
432 task = (struct irda_task *) data;
434 irda_task_kick(task);
438 * Function irda_device_setup (dev)
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
443 int irda_device_setup(struct net_device *dev)
445 ASSERT(dev != NULL, return -1;);
447 dev->hard_header_len = 0;
450 dev->features |= NETIF_F_DYNALLOC;
451 /* dev->destructor = irda_device_destructor; */
453 dev->type = ARPHRD_IRDA;
454 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
456 memset(dev->broadcast, 0xff, 4);
459 dev->flags = IFF_NOARP;
464 * Function irda_device_txqueue_empty (dev)
466 * Check if there is still some frames in the transmit queue for this
467 * device. Maybe we should use: q->q.qlen == 0.
470 int irda_device_txqueue_empty(struct net_device *dev)
472 if (skb_queue_len(&dev->qdisc->q))
479 * Function irda_device_init_dongle (self, type, qos)
481 * Initialize attached dongle.
483 * Important : request_module require us to call this function with
484 * a process context and irq enabled. - Jean II
486 dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
488 struct dongle_reg *reg;
491 ASSERT(dev != NULL, return NULL;);
496 ASSERT(!in_interrupt(), return NULL;);
497 /* Try to load the module needed */
498 sprintf(modname, "irda-dongle-%d", type);
499 request_module(modname);
501 #endif /* CONFIG_KMOD */
503 if (!(reg = hashbin_find(dongles, type, NULL))) {
504 ERROR("IrDA: Unable to find requested dongle\n");
508 /* Allocate dongle info for this instance */
509 dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
513 memset(dongle, 0, sizeof(dongle_t));
515 /* Bind the registration info to this particular instance */
523 * Function irda_device_dongle_cleanup (dongle)
528 int irda_device_dongle_cleanup(dongle_t *dongle)
530 ASSERT(dongle != NULL, return -1;);
532 dongle->issue->close(dongle);
540 * Function irda_device_register_dongle (dongle)
545 int irda_device_register_dongle(struct dongle_reg *new)
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__);
553 /* Insert IrDA dongle into hashbin */
554 hashbin_insert(dongles, (irda_queue_t *) new, new->type, NULL);
560 * Function irda_device_unregister_dongle (dongle)
562 * Unregister dongle, and remove dongle from list of registred dongles
565 void irda_device_unregister_dongle(struct dongle_reg *dongle)
569 node = hashbin_remove(dongles, dongle->type, NULL);
571 ERROR("%s(), dongle not found!\n", __FUNCTION__);
577 * Function irda_device_set_mode (self, mode)
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.
583 int irda_device_set_mode(struct net_device* dev, int mode)
585 struct if_irda_req req;
588 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
590 if (!dev->do_ioctl) {
591 ERROR("%s(), set_raw_mode not impl. by "
592 "device driver\n", __FUNCTION__);
598 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSMODE);
604 * Function setup_dma (idev, buffer, count, mode)
606 * Setup the DMA channel. Commonly used by ISA FIR drivers
609 void setup_dma(int channel, char *buffer, int count, int mode)
613 flags = claim_dma_lock();
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);
622 release_dma_lock(flags);