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.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 ********************************************************************/
31 #include <linux/config.h>
32 #include <linux/string.h>
33 #include <linux/proc_fs.h>
34 #include <linux/skbuff.h>
36 #include <linux/if_ether.h>
37 #include <linux/if_arp.h>
38 #include <linux/netdevice.h>
39 #include <linux/init.h>
40 #include <linux/tty.h>
41 #include <linux/kmod.h>
42 #include <linux/wireless.h>
43 #include <linux/spinlock.h>
45 #include <asm/ioctls.h>
46 #include <asm/segment.h>
47 #include <asm/uaccess.h>
51 #include <net/pkt_sched.h>
53 #include <net/irda/irda_device.h>
54 #include <net/irda/irlap.h>
55 #include <net/irda/timer.h>
56 #include <net/irda/wrapper.h>
58 extern int irtty_init(void);
59 extern int nsc_ircc_init(void);
60 extern int ircc_init(void);
61 extern int toshoboe_init(void);
62 extern int litelink_init(void);
63 extern int w83977af_init(void);
64 extern int esi_init(void);
65 extern int tekram_init(void);
66 extern int actisys_init(void);
67 extern int girbil_init(void);
69 static void __irda_task_delete(struct irda_task
*task
);
71 static hashbin_t
*dongles
= NULL
;
72 static hashbin_t
*tasks
= NULL
;
74 const char *infrared_mode
[] = {
81 static const char *task_state
[] = {
88 "IRDA_TASK_CHILD_INIT",
89 "IRDA_TASK_CHILD_WAIT",
90 "IRDA_TASK_CHILD_DONE",
93 static void irda_task_timer_expired(void *data
);
96 int irda_device_proc_read(char *buf
, char **start
, off_t offset
, int len
,
99 #endif /* CONFIG_PROC_FS */
101 int __init
irda_device_init( void)
103 dongles
= hashbin_new(HB_GLOBAL
);
104 if (dongles
== NULL
) {
106 "IrDA: Can't allocate dongles hashbin!\n");
110 tasks
= hashbin_new(HB_GLOBAL
);
113 "IrDA: Can't allocate tasks hashbin!\n");
118 * Call the init function of the device drivers that has not been
119 * compiled as a module
121 #ifdef CONFIG_IRTTY_SIR
124 #ifdef CONFIG_WINBOND_FIR
127 #ifdef CONFIG_NSC_FIR
130 #ifdef CONFIG_TOSHIBA_FIR
133 #ifdef CONFIG_SMC_IRCC_FIR
136 #ifdef CONFIG_ESI_DONGLE
139 #ifdef CONFIG_TEKRAM_DONGLE
142 #ifdef CONFIG_ACTISYS_DONGLE
145 #ifdef CONFIG_GIRBIL_DONGLE
148 #ifdef CONFIG_LITELINK_DONGLE
151 #ifdef CONFIG_OLD_BELKIN
157 void irda_device_cleanup(void)
159 IRDA_DEBUG(4, __FUNCTION__
"()\n");
161 hashbin_delete(tasks
, (FREE_FUNC
) __irda_task_delete
);
162 hashbin_delete(dongles
, NULL
);
166 * Function irda_device_set_media_busy (self, status)
168 * Called when we have detected that another station is transmiting
169 * in contention mode.
171 void irda_device_set_media_busy(struct net_device
*dev
, int status
)
173 struct irlap_cb
*self
;
175 IRDA_DEBUG(4, __FUNCTION__
"(%s)\n", status
? "TRUE" : "FALSE");
177 self
= (struct irlap_cb
*) dev
->atalk_ptr
;
179 ASSERT(self
!= NULL
, return;);
180 ASSERT(self
->magic
== LAP_MAGIC
, return;);
183 self
->media_busy
= TRUE
;
184 irlap_start_mbusy_timer(self
);
185 IRDA_DEBUG( 4, "Media busy!\n");
187 self
->media_busy
= FALSE
;
188 irlap_stop_mbusy_timer(self
);
192 int irda_device_set_dtr_rts(struct net_device
*dev
, int dtr
, int rts
)
194 struct if_irda_req req
;
197 IRDA_DEBUG(2, __FUNCTION__
"()\n");
199 if (!dev
->do_ioctl
) {
200 ERROR(__FUNCTION__
"(), do_ioctl not impl. by "
208 ret
= dev
->do_ioctl(dev
, (struct ifreq
*) &req
, SIOCSDTRRTS
);
213 int irda_device_change_speed(struct net_device
*dev
, __u32 speed
)
215 struct if_irda_req req
;
218 IRDA_DEBUG(2, __FUNCTION__
"()\n");
220 if (!dev
->do_ioctl
) {
221 ERROR(__FUNCTION__
"(), do_ioctl not impl. by "
226 req
.ifr_baudrate
= speed
;
228 ret
= dev
->do_ioctl(dev
, (struct ifreq
*) &req
, SIOCSBANDWIDTH
);
234 * Function irda_device_is_receiving (dev)
236 * Check if the device driver is currently receiving data
239 int irda_device_is_receiving(struct net_device
*dev
)
241 struct if_irda_req req
;
244 IRDA_DEBUG(2, __FUNCTION__
"()\n");
246 if (!dev
->do_ioctl
) {
247 ERROR(__FUNCTION__
"(), do_ioctl not impl. by "
252 ret
= dev
->do_ioctl(dev
, (struct ifreq
*) &req
, SIOCGRECEIVING
);
256 return req
.ifr_receiving
;
259 void irda_task_next_state(struct irda_task
*task
, IRDA_TASK_STATE state
)
261 IRDA_DEBUG(2, __FUNCTION__
"(), state = %s\n", task_state
[state
]);
266 static void __irda_task_delete(struct irda_task
*task
)
268 del_timer(&task
->timer
);
273 void irda_task_delete(struct irda_task
*task
)
275 /* Unregister task */
276 hashbin_remove(tasks
, (int) task
, NULL
);
278 __irda_task_delete(task
);
282 * Function irda_task_kick (task)
284 * Tries to execute a task possible multiple times until the task is either
285 * finished, or askes for a timeout. When a task is finished, we do post
286 * processing, and notify the parent task, that is waiting for this task
289 int irda_task_kick(struct irda_task
*task
)
295 IRDA_DEBUG(2, __FUNCTION__
"()\n");
297 ASSERT(task
!= NULL
, return -1;);
298 ASSERT(task
->magic
== IRDA_TASK_MAGIC
, return -1;);
300 /* Execute task until it's finished, or askes for a timeout */
302 timeout
= task
->function(task
);
304 ERROR(__FUNCTION__
"(), error in task handler!\n");
305 irda_task_delete(task
);
308 } while ((timeout
== 0) && (task
->state
!= IRDA_TASK_DONE
));
311 ERROR(__FUNCTION__
"(), Error executing task!\n");
312 irda_task_delete(task
);
316 /* Check if we are finished */
317 if (task
->state
== IRDA_TASK_DONE
) {
318 del_timer(&task
->timer
);
320 /* Do post processing */
322 task
->finished(task
);
326 /* Check if parent is waiting for us to complete */
327 if (task
->parent
->state
== IRDA_TASK_CHILD_WAIT
) {
328 task
->parent
->state
= IRDA_TASK_CHILD_DONE
;
330 /* Stop timer now that we are here */
331 del_timer(&task
->parent
->timer
);
333 /* Kick parent task */
334 irda_task_kick(task
->parent
);
337 irda_task_delete(task
);
338 } else if (timeout
> 0) {
339 irda_start_timer(&task
->timer
, timeout
, (void *) task
,
340 irda_task_timer_expired
);
343 IRDA_DEBUG(0, __FUNCTION__
344 "(), not finished, and no timeout!\n");
352 * Function irda_task_execute (instance, function, finished)
354 * This function registers and tries to execute tasks that may take some
355 * time to complete. We do it this hairy way since we may have been
356 * called from interrupt context, so it's not possible to use
359 struct irda_task
*irda_task_execute(void *instance
,
360 IRDA_TASK_CALLBACK function
,
361 IRDA_TASK_CALLBACK finished
,
362 struct irda_task
*parent
, void *param
)
364 struct irda_task
*task
;
367 IRDA_DEBUG(2, __FUNCTION__
"()\n");
369 task
= kmalloc(sizeof(struct irda_task
), GFP_ATOMIC
);
373 task
->state
= IRDA_TASK_INIT
;
374 task
->instance
= instance
;
375 task
->function
= function
;
376 task
->finished
= finished
;
377 task
->parent
= parent
;
379 task
->magic
= IRDA_TASK_MAGIC
;
381 init_timer(&task
->timer
);
384 hashbin_insert(tasks
, (irda_queue_t
*) task
, (int) task
, NULL
);
386 /* No time to waste, so lets get going! */
387 ret
= irda_task_kick(task
);
395 * Function irda_task_timer_expired (data)
397 * Task time has expired. We now try to execute task (again), and restart
398 * the timer if the task has not finished yet
400 static void irda_task_timer_expired(void *data
)
402 struct irda_task
*task
;
404 IRDA_DEBUG(2, __FUNCTION__
"()\n");
406 task
= (struct irda_task
*) data
;
408 irda_task_kick(task
);
412 * Function irda_device_setup (dev)
414 * This function should be used by low level device drivers in a similar way
415 * as ether_setup() is used by normal network device drivers
417 int irda_device_setup(struct net_device
*dev
)
419 ASSERT(dev
!= NULL
, return -1;);
421 dev
->hard_header_len
= 0;
424 dev
->features
|= NETIF_F_DYNALLOC
;
425 /* dev->destructor = irda_device_destructor; */
427 dev
->type
= ARPHRD_IRDA
;
428 dev
->tx_queue_len
= 8; /* Window size + 1 s-frame */
430 memset(dev
->broadcast
, 0xff, 4);
433 dev_init_buffers(dev
);
434 dev
->flags
= IFF_NOARP
;
439 * Function irda_device_txqueue_empty (dev)
441 * Check if there is still some frames in the transmit queue for this
442 * device. Maybe we should use: q->q.qlen == 0.
445 int irda_device_txqueue_empty(struct net_device
*dev
)
447 if (skb_queue_len(&dev
->qdisc
->q
))
454 * Function irda_device_init_dongle (self, type, qos)
456 * Initialize attached dongle.
458 dongle_t
*irda_device_dongle_init(struct net_device
*dev
, int type
)
460 struct dongle_reg
*reg
;
464 ASSERT(dev
!= NULL
, return NULL
;);
467 /* Try to load the module needed */
468 sprintf(modname
, "irda-dongle-%d", type
);
469 request_module(modname
);
470 #endif /* CONFIG_KMOD */
472 if (!(reg
= hashbin_find(dongles
, type
, NULL
))) {
473 ERROR("IrDA: Unable to find requested dongle\n");
477 /* Allocate dongle info for this instance */
478 dongle
= kmalloc(sizeof(dongle_t
), GFP_KERNEL
);
482 memset(dongle
, 0, sizeof(dongle_t
));
484 /* Bind the registration info to this particular instance */
492 * Function irda_device_dongle_cleanup (dongle)
497 int irda_device_dongle_cleanup(dongle_t
*dongle
)
499 ASSERT(dongle
!= NULL
, return -1;);
501 dongle
->issue
->close(dongle
);
509 * Function irda_device_register_dongle (dongle)
514 int irda_device_register_dongle(struct dongle_reg
*new)
516 /* Check if this dongle has been registred before */
517 if (hashbin_find(dongles
, new->type
, NULL
)) {
518 MESSAGE(__FUNCTION__
"(), Dongle already registered\n");
522 /* Insert IrDA dongle into hashbin */
523 hashbin_insert(dongles
, (irda_queue_t
*) new, new->type
, NULL
);
529 * Function irda_device_unregister_dongle (dongle)
531 * Unregister dongle, and remove dongle from list of registred dongles
534 void irda_device_unregister_dongle(struct dongle_reg
*dongle
)
538 node
= hashbin_remove(dongles
, dongle
->type
, NULL
);
540 ERROR(__FUNCTION__
"(), dongle not found!\n");
546 * Function irda_device_set_mode (self, mode)
548 * Set the Infrared device driver into mode where it sends and receives
549 * data without using IrLAP framing. Check out the particular device
550 * driver to find out which modes it support.
552 int irda_device_set_mode(struct net_device
* dev
, int mode
)
554 struct if_irda_req req
;
557 IRDA_DEBUG(0, __FUNCTION__
"()\n");
559 if (!dev
->do_ioctl
) {
560 ERROR(__FUNCTION__
"(), set_raw_mode not impl. by "
567 ret
= dev
->do_ioctl(dev
, (struct ifreq
*) &req
, SIOCSMODE
);
573 * Function setup_dma (idev, buffer, count, mode)
575 * Setup the DMA channel. Commonly used by ISA FIR drivers
578 void setup_dma(int channel
, char *buffer
, int count
, int mode
)
582 flags
= claim_dma_lock();
584 disable_dma(channel
);
585 clear_dma_ff(channel
);
586 set_dma_mode(channel
, mode
);
587 set_dma_addr(channel
, virt_to_bus(buffer
));
588 set_dma_count(channel
, count
);
591 release_dma_lock(flags
);