infiniband: convert ipoib to net_device_ops
[deliverable/linux.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
1da177e4
LT
32#include <linux/string.h>
33#include <linux/proc_fs.h>
34#include <linux/skbuff.h>
4fc268d2 35#include <linux/capability.h>
1da177e4
LT
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>
44
45#include <asm/ioctls.h>
46#include <asm/uaccess.h>
47#include <asm/dma.h>
48#include <asm/io.h>
49
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>
54
55static void __irda_task_delete(struct irda_task *task);
56
57static hashbin_t *dongles = NULL;
58static hashbin_t *tasks = NULL;
59
1da177e4
LT
60static void irda_task_timer_expired(void *data);
61
62int __init irda_device_init( void)
63{
64 dongles = hashbin_new(HB_NOLOCK);
65 if (dongles == NULL) {
66 IRDA_WARNING("IrDA: Can't allocate dongles hashbin!\n");
67 return -ENOMEM;
68 }
69 spin_lock_init(&dongles->hb_spinlock);
70
71 tasks = hashbin_new(HB_LOCK);
72 if (tasks == NULL) {
73 IRDA_WARNING("IrDA: Can't allocate tasks hashbin!\n");
74 hashbin_delete(dongles, NULL);
75 return -ENOMEM;
76 }
77
78 /* We no longer initialise the driver ourselves here, we let
79 * the system do it for us... - Jean II */
80
81 return 0;
82}
83
75a69ac6 84static void leftover_dongle(void *arg)
1da177e4
LT
85{
86 struct dongle_reg *reg = arg;
87 IRDA_WARNING("IrDA: Dongle type %x not unregistered\n",
88 reg->type);
89}
90
75a69ac6 91void irda_device_cleanup(void)
1da177e4 92{
0dc47877 93 IRDA_DEBUG(4, "%s()\n", __func__);
1da177e4
LT
94
95 hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
96
97 hashbin_delete(dongles, leftover_dongle);
98}
99
100/*
101 * Function irda_device_set_media_busy (self, status)
102 *
103 * Called when we have detected that another station is transmitting
104 * in contention mode.
105 */
106void irda_device_set_media_busy(struct net_device *dev, int status)
107{
108 struct irlap_cb *self;
109
0dc47877 110 IRDA_DEBUG(4, "%s(%s)\n", __func__, status ? "TRUE" : "FALSE");
1da177e4
LT
111
112 self = (struct irlap_cb *) dev->atalk_ptr;
113
7e5c6bc0
JT
114 /* Some drivers may enable the receive interrupt before calling
115 * irlap_open(), or they may disable the receive interrupt
116 * after calling irlap_close().
117 * The IrDA stack is protected from this in irlap_driver_rcv().
118 * However, the driver calls directly the wrapper, that calls
119 * us directly. Make sure we protect ourselves.
120 * Jean II */
121 if (!self || self->magic != LAP_MAGIC)
122 return;
1da177e4
LT
123
124 if (status) {
125 self->media_busy = TRUE;
126 if (status == SMALL)
127 irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
128 else
129 irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
130 IRDA_DEBUG( 4, "Media busy!\n");
131 } else {
132 self->media_busy = FALSE;
133 irlap_stop_mbusy_timer(self);
134 }
135}
136EXPORT_SYMBOL(irda_device_set_media_busy);
137
138
139/*
140 * Function irda_device_is_receiving (dev)
141 *
142 * Check if the device driver is currently receiving data
143 *
144 */
145int irda_device_is_receiving(struct net_device *dev)
146{
147 struct if_irda_req req;
148 int ret;
149
0dc47877 150 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4
LT
151
152 if (!dev->do_ioctl) {
153 IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
0dc47877 154 __func__);
1da177e4
LT
155 return -1;
156 }
157
158 ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
159 if (ret < 0)
160 return ret;
161
162 return req.ifr_receiving;
163}
164
1da177e4
LT
165static void __irda_task_delete(struct irda_task *task)
166{
167 del_timer(&task->timer);
168
169 kfree(task);
170}
171
e9888f54 172static void irda_task_delete(struct irda_task *task)
1da177e4
LT
173{
174 /* Unregister task */
175 hashbin_remove(tasks, (long) task, NULL);
176
177 __irda_task_delete(task);
178}
1da177e4
LT
179
180/*
181 * Function irda_task_kick (task)
182 *
183 * Tries to execute a task possible multiple times until the task is either
184 * finished, or askes for a timeout. When a task is finished, we do post
185 * processing, and notify the parent task, that is waiting for this task
186 * to complete.
187 */
188static int irda_task_kick(struct irda_task *task)
189{
190 int finished = TRUE;
191 int count = 0;
192 int timeout;
193
0dc47877 194 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4
LT
195
196 IRDA_ASSERT(task != NULL, return -1;);
197 IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
198
199 /* Execute task until it's finished, or askes for a timeout */
200 do {
201 timeout = task->function(task);
202 if (count++ > 100) {
203 IRDA_ERROR("%s: error in task handler!\n",
0dc47877 204 __func__);
1da177e4
LT
205 irda_task_delete(task);
206 return TRUE;
207 }
208 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
209
210 if (timeout < 0) {
0dc47877 211 IRDA_ERROR("%s: Error executing task!\n", __func__);
1da177e4
LT
212 irda_task_delete(task);
213 return TRUE;
214 }
215
216 /* Check if we are finished */
217 if (task->state == IRDA_TASK_DONE) {
218 del_timer(&task->timer);
219
220 /* Do post processing */
221 if (task->finished)
222 task->finished(task);
223
224 /* Notify parent */
225 if (task->parent) {
226 /* Check if parent is waiting for us to complete */
227 if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
228 task->parent->state = IRDA_TASK_CHILD_DONE;
229
230 /* Stop timer now that we are here */
231 del_timer(&task->parent->timer);
232
233 /* Kick parent task */
234 irda_task_kick(task->parent);
235 }
236 }
237 irda_task_delete(task);
238 } else if (timeout > 0) {
239 irda_start_timer(&task->timer, timeout, (void *) task,
240 irda_task_timer_expired);
241 finished = FALSE;
242 } else {
243 IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
0dc47877 244 __func__);
1da177e4
LT
245 finished = FALSE;
246 }
247
248 return finished;
249}
250
1da177e4
LT
251/*
252 * Function irda_task_timer_expired (data)
253 *
254 * Task time has expired. We now try to execute task (again), and restart
255 * the timer if the task has not finished yet
256 */
257static void irda_task_timer_expired(void *data)
258{
259 struct irda_task *task;
260
0dc47877 261 IRDA_DEBUG(2, "%s()\n", __func__);
1da177e4
LT
262
263 task = (struct irda_task *) data;
264
265 irda_task_kick(task);
266}
267
268/*
269 * Function irda_device_setup (dev)
270 *
271 * This function should be used by low level device drivers in a similar way
272 * as ether_setup() is used by normal network device drivers
273 */
274static void irda_device_setup(struct net_device *dev)
275{
6819bc2e
YH
276 dev->hard_header_len = 0;
277 dev->addr_len = LAP_ALEN;
1da177e4 278
6819bc2e
YH
279 dev->type = ARPHRD_IRDA;
280 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
1da177e4 281
d93077fb 282 memset(dev->broadcast, 0xff, LAP_ALEN);
1da177e4
LT
283
284 dev->mtu = 2048;
285 dev->flags = IFF_NOARP;
286}
287
288/*
6819bc2e 289 * Funciton alloc_irdadev
1da177e4
LT
290 * Allocates and sets up an IRDA device in a manner similar to
291 * alloc_etherdev.
292 */
293struct net_device *alloc_irdadev(int sizeof_priv)
294{
295 return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
296}
297EXPORT_SYMBOL(alloc_irdadev);
298
56c3b7d7 299#ifdef CONFIG_ISA_DMA_API
1da177e4
LT
300/*
301 * Function setup_dma (idev, buffer, count, mode)
302 *
b6d9a5d8 303 * Setup the DMA channel. Commonly used by LPC FIR drivers
1da177e4
LT
304 *
305 */
306void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
307{
308 unsigned long flags;
309
310 flags = claim_dma_lock();
311
312 disable_dma(channel);
313 clear_dma_ff(channel);
314 set_dma_mode(channel, mode);
315 set_dma_addr(channel, buffer);
316 set_dma_count(channel, count);
317 enable_dma(channel);
318
319 release_dma_lock(flags);
320}
321EXPORT_SYMBOL(irda_setup_dma);
56c3b7d7 322#endif
This page took 0.419363 seconds and 5 git commands to generate.