initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / irda / irda_device.c
blob80a6648a40438b9ec6ba8bc3ee369661ab3bc3dc
1 /*********************************************************************
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>
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,
28 * MA 02111-1307 USA
30 ********************************************************************/
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/spinlock.h>
45 #include <asm/ioctls.h>
46 #include <asm/uaccess.h>
47 #include <asm/dma.h>
48 #include <asm/io.h>
50 #include <net/irda/irda_device.h>
51 #include <net/irda/irlap.h>
52 #include <net/irda/timer.h>
53 #include <net/irda/wrapper.h>
55 static void __irda_task_delete(struct irda_task *task);
57 static hashbin_t *dongles = NULL;
58 static hashbin_t *tasks = NULL;
60 #ifdef CONFIG_IRDA_DEBUG
61 static const char *task_state[] = {
62 "IRDA_TASK_INIT",
63 "IRDA_TASK_DONE",
64 "IRDA_TASK_WAIT",
65 "IRDA_TASK_WAIT1",
66 "IRDA_TASK_WAIT2",
67 "IRDA_TASK_WAIT3",
68 "IRDA_TASK_CHILD_INIT",
69 "IRDA_TASK_CHILD_WAIT",
70 "IRDA_TASK_CHILD_DONE",
72 #endif /* CONFIG_IRDA_DEBUG */
74 static void irda_task_timer_expired(void *data);
76 int __init irda_device_init( void)
78 dongles = hashbin_new(HB_NOLOCK);
79 if (dongles == NULL) {
80 printk(KERN_WARNING "IrDA: Can't allocate dongles hashbin!\n");
81 return -ENOMEM;
83 spin_lock_init(&dongles->hb_spinlock);
85 tasks = hashbin_new(HB_LOCK);
86 if (tasks == NULL) {
87 printk(KERN_WARNING "IrDA: Can't allocate tasks hashbin!\n");
88 hashbin_delete(dongles, NULL);
89 return -ENOMEM;
92 /* We no longer initialise the driver ourselves here, we let
93 * the system do it for us... - Jean II */
95 return 0;
98 static void __exit leftover_dongle(void *arg)
100 struct dongle_reg *reg = arg;
101 printk(KERN_WARNING "IrDA: Dongle type %x not unregistered\n",
102 reg->type);
105 void __exit irda_device_cleanup(void)
107 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
109 hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
111 hashbin_delete(dongles, leftover_dongle);
115 * Function irda_device_set_media_busy (self, status)
117 * Called when we have detected that another station is transmitting
118 * in contention mode.
120 void irda_device_set_media_busy(struct net_device *dev, int status)
122 struct irlap_cb *self;
124 IRDA_DEBUG(4, "%s(%s)\n", __FUNCTION__, status ? "TRUE" : "FALSE");
126 self = (struct irlap_cb *) dev->atalk_ptr;
128 ASSERT(self != NULL, return;);
129 ASSERT(self->magic == LAP_MAGIC, return;);
131 if (status) {
132 self->media_busy = TRUE;
133 if (status == SMALL)
134 irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
135 else
136 irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
137 IRDA_DEBUG( 4, "Media busy!\n");
138 } else {
139 self->media_busy = FALSE;
140 irlap_stop_mbusy_timer(self);
143 EXPORT_SYMBOL(irda_device_set_media_busy);
146 int irda_device_set_dtr_rts(struct net_device *dev, int dtr, int rts)
148 struct if_irda_req req;
149 int ret;
151 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
153 if (!dev->do_ioctl) {
154 ERROR("%s: do_ioctl not impl. by device driver\n",
155 __FUNCTION__);
156 return -1;
159 req.ifr_dtr = dtr;
160 req.ifr_rts = rts;
162 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSDTRRTS);
164 return ret;
167 int irda_device_change_speed(struct net_device *dev, __u32 speed)
169 struct if_irda_req req;
170 int ret;
172 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
174 if (!dev->do_ioctl) {
175 ERROR("%s: do_ioctl not impl. by device driver\n",
176 __FUNCTION__);
177 return -1;
180 req.ifr_baudrate = speed;
182 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSBANDWIDTH);
184 return ret;
188 * Function irda_device_is_receiving (dev)
190 * Check if the device driver is currently receiving data
193 int irda_device_is_receiving(struct net_device *dev)
195 struct if_irda_req req;
196 int ret;
198 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
200 if (!dev->do_ioctl) {
201 ERROR("%s: do_ioctl not impl. by device driver\n",
202 __FUNCTION__);
203 return -1;
206 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
207 if (ret < 0)
208 return ret;
210 return req.ifr_receiving;
213 void irda_task_next_state(struct irda_task *task, IRDA_TASK_STATE state)
215 IRDA_DEBUG(2, "%s(), state = %s\n", __FUNCTION__, task_state[state]);
217 task->state = state;
219 EXPORT_SYMBOL(irda_task_next_state);
221 static void __irda_task_delete(struct irda_task *task)
223 del_timer(&task->timer);
225 kfree(task);
228 void irda_task_delete(struct irda_task *task)
230 /* Unregister task */
231 hashbin_remove(tasks, (long) task, NULL);
233 __irda_task_delete(task);
235 EXPORT_SYMBOL(irda_task_delete);
238 * Function irda_task_kick (task)
240 * Tries to execute a task possible multiple times until the task is either
241 * finished, or askes for a timeout. When a task is finished, we do post
242 * processing, and notify the parent task, that is waiting for this task
243 * to complete.
245 static int irda_task_kick(struct irda_task *task)
247 int finished = TRUE;
248 int count = 0;
249 int timeout;
251 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
253 ASSERT(task != NULL, return -1;);
254 ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
256 /* Execute task until it's finished, or askes for a timeout */
257 do {
258 timeout = task->function(task);
259 if (count++ > 100) {
260 ERROR("%s: error in task handler!\n", __FUNCTION__);
261 irda_task_delete(task);
262 return TRUE;
264 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
266 if (timeout < 0) {
267 ERROR("%s: Error executing task!\n", __FUNCTION__);
268 irda_task_delete(task);
269 return TRUE;
272 /* Check if we are finished */
273 if (task->state == IRDA_TASK_DONE) {
274 del_timer(&task->timer);
276 /* Do post processing */
277 if (task->finished)
278 task->finished(task);
280 /* Notify parent */
281 if (task->parent) {
282 /* Check if parent is waiting for us to complete */
283 if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
284 task->parent->state = IRDA_TASK_CHILD_DONE;
286 /* Stop timer now that we are here */
287 del_timer(&task->parent->timer);
289 /* Kick parent task */
290 irda_task_kick(task->parent);
293 irda_task_delete(task);
294 } else if (timeout > 0) {
295 irda_start_timer(&task->timer, timeout, (void *) task,
296 irda_task_timer_expired);
297 finished = FALSE;
298 } else {
299 IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
300 __FUNCTION__);
301 finished = FALSE;
304 return finished;
308 * Function irda_task_execute (instance, function, finished)
310 * This function registers and tries to execute tasks that may take some
311 * time to complete. We do it this hairy way since we may have been
312 * called from interrupt context, so it's not possible to use
313 * schedule_timeout()
314 * Two important notes :
315 * o Make sure you irda_task_delete(task); in case you delete the
316 * calling instance.
317 * o No real need to lock when calling this function, but you may
318 * want to lock within the task handler.
319 * Jean II
321 struct irda_task *irda_task_execute(void *instance,
322 IRDA_TASK_CALLBACK function,
323 IRDA_TASK_CALLBACK finished,
324 struct irda_task *parent, void *param)
326 struct irda_task *task;
328 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
330 task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
331 if (!task)
332 return NULL;
334 task->state = IRDA_TASK_INIT;
335 task->instance = instance;
336 task->function = function;
337 task->finished = finished;
338 task->parent = parent;
339 task->param = param;
340 task->magic = IRDA_TASK_MAGIC;
342 init_timer(&task->timer);
344 /* Register task */
345 hashbin_insert(tasks, (irda_queue_t *) task, (long) task, NULL);
347 /* No time to waste, so lets get going! */
348 return irda_task_kick(task) ? NULL : task;
350 EXPORT_SYMBOL(irda_task_execute);
353 * Function irda_task_timer_expired (data)
355 * Task time has expired. We now try to execute task (again), and restart
356 * the timer if the task has not finished yet
358 static void irda_task_timer_expired(void *data)
360 struct irda_task *task;
362 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
364 task = (struct irda_task *) data;
366 irda_task_kick(task);
370 * Function irda_device_setup (dev)
372 * This function should be used by low level device drivers in a similar way
373 * as ether_setup() is used by normal network device drivers
375 void irda_device_setup(struct net_device *dev)
377 dev->hard_header_len = 0;
378 dev->addr_len = 0;
380 dev->type = ARPHRD_IRDA;
381 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
383 memset(dev->broadcast, 0xff, 4);
385 dev->mtu = 2048;
386 dev->flags = IFF_NOARP;
390 * Funciton alloc_irdadev
391 * Allocates and sets up an IRDA device in a manner similar to
392 * alloc_etherdev.
394 struct net_device *alloc_irdadev(int sizeof_priv)
396 return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
398 EXPORT_SYMBOL(alloc_irdadev);
401 * Function irda_device_init_dongle (self, type, qos)
403 * Initialize attached dongle.
405 * Important : request_module require us to call this function with
406 * a process context and irq enabled. - Jean II
408 dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
410 struct dongle_reg *reg;
411 dongle_t *dongle = NULL;
413 might_sleep();
415 spin_lock(&dongles->hb_spinlock);
416 reg = hashbin_find(dongles, type, NULL);
418 #ifdef CONFIG_KMOD
419 /* Try to load the module needed */
420 if (!reg && capable(CAP_SYS_MODULE)) {
421 spin_unlock(&dongles->hb_spinlock);
423 request_module("irda-dongle-%d", type);
425 spin_lock(&dongles->hb_spinlock);
426 reg = hashbin_find(dongles, type, NULL);
428 #endif
430 if (!reg || !try_module_get(reg->owner) ) {
431 ERROR("IrDA: Unable to find requested dongle type %x\n", type);
432 goto out;
435 /* Allocate dongle info for this instance */
436 dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
437 if (!dongle)
438 goto out;
440 memset(dongle, 0, sizeof(dongle_t));
442 /* Bind the registration info to this particular instance */
443 dongle->issue = reg;
444 dongle->dev = dev;
446 out:
447 spin_unlock(&dongles->hb_spinlock);
448 return dongle;
450 EXPORT_SYMBOL(irda_device_dongle_init);
453 * Function irda_device_dongle_cleanup (dongle)
455 int irda_device_dongle_cleanup(dongle_t *dongle)
457 ASSERT(dongle != NULL, return -1;);
459 dongle->issue->close(dongle);
460 module_put(dongle->issue->owner);
461 kfree(dongle);
463 return 0;
465 EXPORT_SYMBOL(irda_device_dongle_cleanup);
468 * Function irda_device_register_dongle (dongle)
470 int irda_device_register_dongle(struct dongle_reg *new)
472 spin_lock(&dongles->hb_spinlock);
473 /* Check if this dongle has been registered before */
474 if (hashbin_find(dongles, new->type, NULL)) {
475 MESSAGE("%s: Dongle type %x already registered\n",
476 __FUNCTION__, new->type);
477 } else {
478 /* Insert IrDA dongle into hashbin */
479 hashbin_insert(dongles, (irda_queue_t *) new, new->type, NULL);
481 spin_unlock(&dongles->hb_spinlock);
483 return 0;
485 EXPORT_SYMBOL(irda_device_register_dongle);
488 * Function irda_device_unregister_dongle (dongle)
490 * Unregister dongle, and remove dongle from list of registered dongles
493 void irda_device_unregister_dongle(struct dongle_reg *dongle)
495 struct dongle *node;
497 spin_lock(&dongles->hb_spinlock);
498 node = hashbin_remove(dongles, dongle->type, NULL);
499 if (!node)
500 ERROR("%s: dongle not found!\n", __FUNCTION__);
501 spin_unlock(&dongles->hb_spinlock);
503 EXPORT_SYMBOL(irda_device_unregister_dongle);
506 * Function irda_device_set_mode (self, mode)
508 * Set the Infrared device driver into mode where it sends and receives
509 * data without using IrLAP framing. Check out the particular device
510 * driver to find out which modes it support.
512 int irda_device_set_mode(struct net_device* dev, int mode)
514 struct if_irda_req req;
515 int ret;
517 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
519 if (!dev->do_ioctl) {
520 ERROR("%s: set_raw_mode not impl. by device driver\n",
521 __FUNCTION__);
522 return -1;
525 req.ifr_mode = mode;
527 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCSMODE);
529 return ret;
532 #ifdef CONFIG_ISA
534 * Function setup_dma (idev, buffer, count, mode)
536 * Setup the DMA channel. Commonly used by ISA FIR drivers
539 void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
541 unsigned long flags;
543 flags = claim_dma_lock();
545 disable_dma(channel);
546 clear_dma_ff(channel);
547 set_dma_mode(channel, mode);
548 set_dma_addr(channel, buffer);
549 set_dma_count(channel, count);
550 enable_dma(channel);
552 release_dma_lock(flags);
554 EXPORT_SYMBOL(irda_setup_dma);
555 #endif