]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/irda/irda_device.c
[PATCH] capable/capability.h (net/)
[net-next-2.6.git] / net / irda / irda_device.c
CommitLineData
1da177e4
LT
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>
4fc268d2 36#include <linux/capability.h>
1da177e4
LT
37#include <linux/if.h>
38#include <linux/if_ether.h>
39#include <linux/if_arp.h>
40#include <linux/netdevice.h>
41#include <linux/init.h>
42#include <linux/tty.h>
43#include <linux/kmod.h>
44#include <linux/spinlock.h>
45
46#include <asm/ioctls.h>
47#include <asm/uaccess.h>
48#include <asm/dma.h>
49#include <asm/io.h>
50
51#include <net/irda/irda_device.h>
52#include <net/irda/irlap.h>
53#include <net/irda/timer.h>
54#include <net/irda/wrapper.h>
55
56static void __irda_task_delete(struct irda_task *task);
57
58static hashbin_t *dongles = NULL;
59static hashbin_t *tasks = NULL;
60
61#ifdef CONFIG_IRDA_DEBUG
62static const char *task_state[] = {
63 "IRDA_TASK_INIT",
64 "IRDA_TASK_DONE",
65 "IRDA_TASK_WAIT",
66 "IRDA_TASK_WAIT1",
67 "IRDA_TASK_WAIT2",
68 "IRDA_TASK_WAIT3",
69 "IRDA_TASK_CHILD_INIT",
70 "IRDA_TASK_CHILD_WAIT",
71 "IRDA_TASK_CHILD_DONE",
72};
73#endif /* CONFIG_IRDA_DEBUG */
74
75static void irda_task_timer_expired(void *data);
76
77int __init irda_device_init( void)
78{
79 dongles = hashbin_new(HB_NOLOCK);
80 if (dongles == NULL) {
81 IRDA_WARNING("IrDA: Can't allocate dongles hashbin!\n");
82 return -ENOMEM;
83 }
84 spin_lock_init(&dongles->hb_spinlock);
85
86 tasks = hashbin_new(HB_LOCK);
87 if (tasks == NULL) {
88 IRDA_WARNING("IrDA: Can't allocate tasks hashbin!\n");
89 hashbin_delete(dongles, NULL);
90 return -ENOMEM;
91 }
92
93 /* We no longer initialise the driver ourselves here, we let
94 * the system do it for us... - Jean II */
95
96 return 0;
97}
98
99static void __exit leftover_dongle(void *arg)
100{
101 struct dongle_reg *reg = arg;
102 IRDA_WARNING("IrDA: Dongle type %x not unregistered\n",
103 reg->type);
104}
105
106void __exit irda_device_cleanup(void)
107{
108 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
109
110 hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
111
112 hashbin_delete(dongles, leftover_dongle);
113}
114
115/*
116 * Function irda_device_set_media_busy (self, status)
117 *
118 * Called when we have detected that another station is transmitting
119 * in contention mode.
120 */
121void irda_device_set_media_busy(struct net_device *dev, int status)
122{
123 struct irlap_cb *self;
124
125 IRDA_DEBUG(4, "%s(%s)\n", __FUNCTION__, status ? "TRUE" : "FALSE");
126
127 self = (struct irlap_cb *) dev->atalk_ptr;
128
7e5c6bc0
JT
129 /* Some drivers may enable the receive interrupt before calling
130 * irlap_open(), or they may disable the receive interrupt
131 * after calling irlap_close().
132 * The IrDA stack is protected from this in irlap_driver_rcv().
133 * However, the driver calls directly the wrapper, that calls
134 * us directly. Make sure we protect ourselves.
135 * Jean II */
136 if (!self || self->magic != LAP_MAGIC)
137 return;
1da177e4
LT
138
139 if (status) {
140 self->media_busy = TRUE;
141 if (status == SMALL)
142 irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
143 else
144 irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
145 IRDA_DEBUG( 4, "Media busy!\n");
146 } else {
147 self->media_busy = FALSE;
148 irlap_stop_mbusy_timer(self);
149 }
150}
151EXPORT_SYMBOL(irda_device_set_media_busy);
152
153
154/*
155 * Function irda_device_is_receiving (dev)
156 *
157 * Check if the device driver is currently receiving data
158 *
159 */
160int irda_device_is_receiving(struct net_device *dev)
161{
162 struct if_irda_req req;
163 int ret;
164
165 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
166
167 if (!dev->do_ioctl) {
168 IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
169 __FUNCTION__);
170 return -1;
171 }
172
173 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
174 if (ret < 0)
175 return ret;
176
177 return req.ifr_receiving;
178}
179
180void irda_task_next_state(struct irda_task *task, IRDA_TASK_STATE state)
181{
182 IRDA_DEBUG(2, "%s(), state = %s\n", __FUNCTION__, task_state[state]);
183
184 task->state = state;
185}
186EXPORT_SYMBOL(irda_task_next_state);
187
188static void __irda_task_delete(struct irda_task *task)
189{
190 del_timer(&task->timer);
191
192 kfree(task);
193}
194
195void irda_task_delete(struct irda_task *task)
196{
197 /* Unregister task */
198 hashbin_remove(tasks, (long) task, NULL);
199
200 __irda_task_delete(task);
201}
202EXPORT_SYMBOL(irda_task_delete);
203
204/*
205 * Function irda_task_kick (task)
206 *
207 * Tries to execute a task possible multiple times until the task is either
208 * finished, or askes for a timeout. When a task is finished, we do post
209 * processing, and notify the parent task, that is waiting for this task
210 * to complete.
211 */
212static int irda_task_kick(struct irda_task *task)
213{
214 int finished = TRUE;
215 int count = 0;
216 int timeout;
217
218 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
219
220 IRDA_ASSERT(task != NULL, return -1;);
221 IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
222
223 /* Execute task until it's finished, or askes for a timeout */
224 do {
225 timeout = task->function(task);
226 if (count++ > 100) {
227 IRDA_ERROR("%s: error in task handler!\n",
228 __FUNCTION__);
229 irda_task_delete(task);
230 return TRUE;
231 }
232 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
233
234 if (timeout < 0) {
235 IRDA_ERROR("%s: Error executing task!\n", __FUNCTION__);
236 irda_task_delete(task);
237 return TRUE;
238 }
239
240 /* Check if we are finished */
241 if (task->state == IRDA_TASK_DONE) {
242 del_timer(&task->timer);
243
244 /* Do post processing */
245 if (task->finished)
246 task->finished(task);
247
248 /* Notify parent */
249 if (task->parent) {
250 /* Check if parent is waiting for us to complete */
251 if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
252 task->parent->state = IRDA_TASK_CHILD_DONE;
253
254 /* Stop timer now that we are here */
255 del_timer(&task->parent->timer);
256
257 /* Kick parent task */
258 irda_task_kick(task->parent);
259 }
260 }
261 irda_task_delete(task);
262 } else if (timeout > 0) {
263 irda_start_timer(&task->timer, timeout, (void *) task,
264 irda_task_timer_expired);
265 finished = FALSE;
266 } else {
267 IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
268 __FUNCTION__);
269 finished = FALSE;
270 }
271
272 return finished;
273}
274
275/*
276 * Function irda_task_execute (instance, function, finished)
277 *
278 * This function registers and tries to execute tasks that may take some
279 * time to complete. We do it this hairy way since we may have been
280 * called from interrupt context, so it's not possible to use
281 * schedule_timeout()
282 * Two important notes :
283 * o Make sure you irda_task_delete(task); in case you delete the
284 * calling instance.
285 * o No real need to lock when calling this function, but you may
286 * want to lock within the task handler.
287 * Jean II
288 */
289struct irda_task *irda_task_execute(void *instance,
290 IRDA_TASK_CALLBACK function,
291 IRDA_TASK_CALLBACK finished,
292 struct irda_task *parent, void *param)
293{
294 struct irda_task *task;
295
296 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
297
298 task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
299 if (!task)
300 return NULL;
301
302 task->state = IRDA_TASK_INIT;
303 task->instance = instance;
304 task->function = function;
305 task->finished = finished;
306 task->parent = parent;
307 task->param = param;
308 task->magic = IRDA_TASK_MAGIC;
309
310 init_timer(&task->timer);
311
312 /* Register task */
313 hashbin_insert(tasks, (irda_queue_t *) task, (long) task, NULL);
314
315 /* No time to waste, so lets get going! */
316 return irda_task_kick(task) ? NULL : task;
317}
318EXPORT_SYMBOL(irda_task_execute);
319
320/*
321 * Function irda_task_timer_expired (data)
322 *
323 * Task time has expired. We now try to execute task (again), and restart
324 * the timer if the task has not finished yet
325 */
326static void irda_task_timer_expired(void *data)
327{
328 struct irda_task *task;
329
330 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
331
332 task = (struct irda_task *) data;
333
334 irda_task_kick(task);
335}
336
337/*
338 * Function irda_device_setup (dev)
339 *
340 * This function should be used by low level device drivers in a similar way
341 * as ether_setup() is used by normal network device drivers
342 */
343static void irda_device_setup(struct net_device *dev)
344{
345 dev->hard_header_len = 0;
346 dev->addr_len = 0;
347
348 dev->type = ARPHRD_IRDA;
349 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
350
351 memset(dev->broadcast, 0xff, 4);
352
353 dev->mtu = 2048;
354 dev->flags = IFF_NOARP;
355}
356
357/*
358 * Funciton alloc_irdadev
359 * Allocates and sets up an IRDA device in a manner similar to
360 * alloc_etherdev.
361 */
362struct net_device *alloc_irdadev(int sizeof_priv)
363{
364 return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
365}
366EXPORT_SYMBOL(alloc_irdadev);
367
368/*
369 * Function irda_device_init_dongle (self, type, qos)
370 *
371 * Initialize attached dongle.
372 *
373 * Important : request_module require us to call this function with
374 * a process context and irq enabled. - Jean II
375 */
376dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
377{
378 struct dongle_reg *reg;
379 dongle_t *dongle = NULL;
380
381 might_sleep();
382
383 spin_lock(&dongles->hb_spinlock);
384 reg = hashbin_find(dongles, type, NULL);
385
386#ifdef CONFIG_KMOD
387 /* Try to load the module needed */
388 if (!reg && capable(CAP_SYS_MODULE)) {
389 spin_unlock(&dongles->hb_spinlock);
390
391 request_module("irda-dongle-%d", type);
392
393 spin_lock(&dongles->hb_spinlock);
394 reg = hashbin_find(dongles, type, NULL);
395 }
396#endif
397
398 if (!reg || !try_module_get(reg->owner) ) {
399 IRDA_ERROR("IrDA: Unable to find requested dongle type %x\n",
400 type);
401 goto out;
402 }
403
404 /* Allocate dongle info for this instance */
405 dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
406 if (!dongle)
407 goto out;
408
409 memset(dongle, 0, sizeof(dongle_t));
410
411 /* Bind the registration info to this particular instance */
412 dongle->issue = reg;
413 dongle->dev = dev;
414
415 out:
416 spin_unlock(&dongles->hb_spinlock);
417 return dongle;
418}
419EXPORT_SYMBOL(irda_device_dongle_init);
420
421/*
422 * Function irda_device_dongle_cleanup (dongle)
423 */
424int irda_device_dongle_cleanup(dongle_t *dongle)
425{
426 IRDA_ASSERT(dongle != NULL, return -1;);
427
428 dongle->issue->close(dongle);
429 module_put(dongle->issue->owner);
430 kfree(dongle);
431
432 return 0;
433}
434EXPORT_SYMBOL(irda_device_dongle_cleanup);
435
436/*
437 * Function irda_device_register_dongle (dongle)
438 */
439int irda_device_register_dongle(struct dongle_reg *new)
440{
441 spin_lock(&dongles->hb_spinlock);
442 /* Check if this dongle has been registered before */
443 if (hashbin_find(dongles, new->type, NULL)) {
444 IRDA_MESSAGE("%s: Dongle type %x already registered\n",
445 __FUNCTION__, new->type);
446 } else {
447 /* Insert IrDA dongle into hashbin */
448 hashbin_insert(dongles, (irda_queue_t *) new, new->type, NULL);
449 }
450 spin_unlock(&dongles->hb_spinlock);
451
452 return 0;
453}
454EXPORT_SYMBOL(irda_device_register_dongle);
455
456/*
457 * Function irda_device_unregister_dongle (dongle)
458 *
459 * Unregister dongle, and remove dongle from list of registered dongles
460 *
461 */
462void irda_device_unregister_dongle(struct dongle_reg *dongle)
463{
464 struct dongle *node;
465
466 spin_lock(&dongles->hb_spinlock);
467 node = hashbin_remove(dongles, dongle->type, NULL);
468 if (!node)
469 IRDA_ERROR("%s: dongle not found!\n", __FUNCTION__);
470 spin_unlock(&dongles->hb_spinlock);
471}
472EXPORT_SYMBOL(irda_device_unregister_dongle);
473
56c3b7d7 474#ifdef CONFIG_ISA_DMA_API
1da177e4
LT
475/*
476 * Function setup_dma (idev, buffer, count, mode)
477 *
b6d9a5d8 478 * Setup the DMA channel. Commonly used by LPC FIR drivers
1da177e4
LT
479 *
480 */
481void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
482{
483 unsigned long flags;
484
485 flags = claim_dma_lock();
486
487 disable_dma(channel);
488 clear_dma_ff(channel);
489 set_dma_mode(channel, mode);
490 set_dma_addr(channel, buffer);
491 set_dma_count(channel, count);
492 enable_dma(channel);
493
494 release_dma_lock(flags);
495}
496EXPORT_SYMBOL(irda_setup_dma);
56c3b7d7 497#endif