Blame view

net/irda/irda_device.c 8.01 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  /*********************************************************************
   *
   * Filename:      irda_device.c
   * Version:       0.9
   * Description:   Utility functions used by the device drivers
   * Status:        Experimental.
   * Author:        Dag Brattli <dagb@cs.uit.no>
   * Created at:    Sat Oct  9 09:22:27 1999
   * Modified at:   Sun Jan 23 17:41:24 2000
   * Modified by:   Dag Brattli <dagb@cs.uit.no>
   *
   *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
   *     Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
   *
   *     This program is free software; you can redistribute it and/or
   *     modify it under the terms of the GNU General Public License as
   *     published by the Free Software Foundation; either version 2 of
   *     the License, or (at your option) any later version.
   *
   *     This program is distributed in the hope that it will be useful,
   *     but WITHOUT ANY WARRANTY; without even the implied warranty of
   *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   *     GNU General Public License for more details.
   *
   *     You should have received a copy of the GNU General Public License
   *     along with this program; if not, write to the Free Software
   *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   *     MA 02111-1307 USA
   *
   ********************************************************************/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
  #include <linux/string.h>
  #include <linux/proc_fs.h>
  #include <linux/skbuff.h>
4fc268d24   Randy Dunlap   [PATCH] capable/c...
34
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
39
40
41
42
  #include <linux/if.h>
  #include <linux/if_ether.h>
  #include <linux/if_arp.h>
  #include <linux/netdevice.h>
  #include <linux/init.h>
  #include <linux/tty.h>
  #include <linux/kmod.h>
  #include <linux/spinlock.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
43
  #include <linux/slab.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
44
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  
  #include <asm/ioctls.h>
  #include <asm/uaccess.h>
  #include <asm/dma.h>
  #include <asm/io.h>
  
  #include <net/irda/irda_device.h>
  #include <net/irda/irlap.h>
  #include <net/irda/timer.h>
  #include <net/irda/wrapper.h>
  
  static void __irda_task_delete(struct irda_task *task);
  
  static hashbin_t *dongles = NULL;
  static hashbin_t *tasks = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  static void irda_task_timer_expired(void *data);
  
  int __init irda_device_init( void)
  {
  	dongles = hashbin_new(HB_NOLOCK);
  	if (dongles == NULL) {
  		IRDA_WARNING("IrDA: Can't allocate dongles hashbin!
  ");
  		return -ENOMEM;
  	}
  	spin_lock_init(&dongles->hb_spinlock);
  
  	tasks = hashbin_new(HB_LOCK);
  	if (tasks == NULL) {
  		IRDA_WARNING("IrDA: Can't allocate tasks hashbin!
  ");
  		hashbin_delete(dongles, NULL);
  		return -ENOMEM;
  	}
  
  	/* We no longer initialise the driver ourselves here, we let
  	 * the system do it for us... - Jean II */
  
  	return 0;
  }
75a69ac6d   Samuel Ortiz   [IrDA]: Fix IrDA ...
85
  static void leftover_dongle(void *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
91
  {
  	struct dongle_reg *reg = arg;
  	IRDA_WARNING("IrDA: Dongle type %x not unregistered
  ",
  		     reg->type);
  }
75a69ac6d   Samuel Ortiz   [IrDA]: Fix IrDA ...
92
  void irda_device_cleanup(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  {
0dc47877a   Harvey Harrison   net: replace rema...
94
95
  	IRDA_DEBUG(4, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  
  	hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
  
  	hashbin_delete(dongles, leftover_dongle);
  }
  
  /*
   * Function irda_device_set_media_busy (self, status)
   *
   *    Called when we have detected that another station is transmitting
   *    in contention mode.
   */
  void irda_device_set_media_busy(struct net_device *dev, int status)
  {
  	struct irlap_cb *self;
0dc47877a   Harvey Harrison   net: replace rema...
111
112
  	IRDA_DEBUG(4, "%s(%s)
  ", __func__, status ? "TRUE" : "FALSE");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
  
  	self = (struct irlap_cb *) dev->atalk_ptr;
7e5c6bc0a   Jean Tourrilhes   [PATCH] irda_devi...
115
116
117
118
119
120
121
122
123
  	/* Some drivers may enable the receive interrupt before calling
  	 * irlap_open(), or they may disable the receive interrupt
  	 * after calling irlap_close().
  	 * The IrDA stack is protected from this in irlap_driver_rcv().
  	 * However, the driver calls directly the wrapper, that calls
  	 * us directly. Make sure we protect ourselves.
  	 * Jean II */
  	if (!self || self->magic != LAP_MAGIC)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  
  	if (status) {
  		self->media_busy = TRUE;
  		if (status == SMALL)
  			irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
  		else
  			irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
  		IRDA_DEBUG( 4, "Media busy!
  ");
  	} else {
  		self->media_busy = FALSE;
  		irlap_stop_mbusy_timer(self);
  	}
  }
  EXPORT_SYMBOL(irda_device_set_media_busy);
  
  
  /*
   * Function irda_device_is_receiving (dev)
   *
   *    Check if the device driver is currently receiving data
   *
   */
  int irda_device_is_receiving(struct net_device *dev)
  {
  	struct if_irda_req req;
  	int ret;
0dc47877a   Harvey Harrison   net: replace rema...
151
152
  	IRDA_DEBUG(2, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153

92bcd4fe9   Stephen Hemminger   irda: net_device_...
154
  	if (!dev->netdev_ops->ndo_do_ioctl) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  		IRDA_ERROR("%s: do_ioctl not impl. by device driver
  ",
0dc47877a   Harvey Harrison   net: replace rema...
157
  			   __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
  		return -1;
  	}
92bcd4fe9   Stephen Hemminger   irda: net_device_...
160
161
  	ret = (dev->netdev_ops->ndo_do_ioctl)(dev, (struct ifreq *) &req,
  					      SIOCGRECEIVING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
163
164
165
166
  	if (ret < 0)
  		return ret;
  
  	return req.ifr_receiving;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
171
172
  static void __irda_task_delete(struct irda_task *task)
  {
  	del_timer(&task->timer);
  
  	kfree(task);
  }
e9888f549   Adrian Bunk   [IrDA]: Irport re...
173
  static void irda_task_delete(struct irda_task *task)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
178
179
  {
  	/* Unregister task */
  	hashbin_remove(tasks, (long) task, NULL);
  
  	__irda_task_delete(task);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  
  /*
   * Function irda_task_kick (task)
   *
   *    Tries to execute a task possible multiple times until the task is either
   *    finished, or askes for a timeout. When a task is finished, we do post
   *    processing, and notify the parent task, that is waiting for this task
   *    to complete.
   */
  static int irda_task_kick(struct irda_task *task)
  {
  	int finished = TRUE;
  	int count = 0;
  	int timeout;
0dc47877a   Harvey Harrison   net: replace rema...
194
195
  	IRDA_DEBUG(2, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
200
201
202
203
204
205
  
  	IRDA_ASSERT(task != NULL, return -1;);
  	IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
  
  	/* Execute task until it's finished, or askes for a timeout */
  	do {
  		timeout = task->function(task);
  		if (count++ > 100) {
  			IRDA_ERROR("%s: error in task handler!
  ",
0dc47877a   Harvey Harrison   net: replace rema...
206
  				   __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
210
211
212
  			irda_task_delete(task);
  			return TRUE;
  		}
  	} while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
  
  	if (timeout < 0) {
0dc47877a   Harvey Harrison   net: replace rema...
213
214
  		IRDA_ERROR("%s: Error executing task!
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  		irda_task_delete(task);
  		return TRUE;
  	}
  
  	/* Check if we are finished */
  	if (task->state == IRDA_TASK_DONE) {
  		del_timer(&task->timer);
  
  		/* Do post processing */
  		if (task->finished)
  			task->finished(task);
  
  		/* Notify parent */
  		if (task->parent) {
  			/* Check if parent is waiting for us to complete */
  			if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
  				task->parent->state = IRDA_TASK_CHILD_DONE;
  
  				/* Stop timer now that we are here */
  				del_timer(&task->parent->timer);
  
  				/* Kick parent task */
  				irda_task_kick(task->parent);
  			}
  		}
  		irda_task_delete(task);
  	} else if (timeout > 0) {
  		irda_start_timer(&task->timer, timeout, (void *) task,
  				 irda_task_timer_expired);
  		finished = FALSE;
  	} else {
  		IRDA_DEBUG(0, "%s(), not finished, and no timeout!
  ",
0dc47877a   Harvey Harrison   net: replace rema...
248
  			   __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
251
252
253
254
255
  		finished = FALSE;
  	}
  
  	return finished;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
258
259
260
261
262
263
   * Function irda_task_timer_expired (data)
   *
   *    Task time has expired. We now try to execute task (again), and restart
   *    the timer if the task has not finished yet
   */
  static void irda_task_timer_expired(void *data)
  {
  	struct irda_task *task;
0dc47877a   Harvey Harrison   net: replace rema...
264
265
  	IRDA_DEBUG(2, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266

ea1107338   Joe Perches   net: Remove casts...
267
  	task = data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
269
270
271
272
273
274
275
276
277
278
279
  
  	irda_task_kick(task);
  }
  
  /*
   * Function irda_device_setup (dev)
   *
   *    This function should be used by low level device drivers in a similar way
   *    as ether_setup() is used by normal network device drivers
   */
  static void irda_device_setup(struct net_device *dev)
  {
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
280
281
  	dev->hard_header_len = 0;
  	dev->addr_len        = LAP_ALEN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282

6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
283
284
  	dev->type            = ARPHRD_IRDA;
  	dev->tx_queue_len    = 8; /* Window size + 1 s-frame */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285

d93077fb0   Samuel Ortiz   [IRDA]: Set prope...
286
  	memset(dev->broadcast, 0xff, LAP_ALEN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
  
  	dev->mtu = 2048;
  	dev->flags = IFF_NOARP;
  }
  
  /*
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
293
   * Funciton  alloc_irdadev
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
296
297
298
299
300
301
   * 	Allocates and sets up an IRDA device in a manner similar to
   * 	alloc_etherdev.
   */
  struct net_device *alloc_irdadev(int sizeof_priv)
  {
  	return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
  }
  EXPORT_SYMBOL(alloc_irdadev);
56c3b7d78   Al Viro   [PATCH] ISA DMA K...
302
  #ifdef CONFIG_ISA_DMA_API
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
  /*
   * Function setup_dma (idev, buffer, count, mode)
   *
b6d9a5d81   Andi Kleen   [PATCH] x86_64: M...
306
   *    Setup the DMA channel. Commonly used by LPC FIR drivers
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
   *
   */
  void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
  {
  	unsigned long flags;
  
  	flags = claim_dma_lock();
  
  	disable_dma(channel);
  	clear_dma_ff(channel);
  	set_dma_mode(channel, mode);
  	set_dma_addr(channel, buffer);
  	set_dma_count(channel, count);
  	enable_dma(channel);
  
  	release_dma_lock(flags);
  }
  EXPORT_SYMBOL(irda_setup_dma);
56c3b7d78   Al Viro   [PATCH] ISA DMA K...
325
  #endif