blob: 14653b8d664dba5d83129b98230ddfe010674111 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/string.h>
33#include <linux/proc_fs.h>
34#include <linux/skbuff.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040045#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/ioctls.h>
48#include <asm/uaccess.h>
49#include <asm/dma.h>
50#include <asm/io.h>
51
52#include <net/irda/irda_device.h>
53#include <net/irda/irlap.h>
54#include <net/irda/timer.h>
55#include <net/irda/wrapper.h>
56
57static void __irda_task_delete(struct irda_task *task);
58
59static hashbin_t *dongles = NULL;
60static hashbin_t *tasks = NULL;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void irda_task_timer_expired(void *data);
63
64int __init irda_device_init( void)
65{
66 dongles = hashbin_new(HB_NOLOCK);
67 if (dongles == NULL) {
68 IRDA_WARNING("IrDA: Can't allocate dongles hashbin!\n");
69 return -ENOMEM;
70 }
71 spin_lock_init(&dongles->hb_spinlock);
72
73 tasks = hashbin_new(HB_LOCK);
74 if (tasks == NULL) {
75 IRDA_WARNING("IrDA: Can't allocate tasks hashbin!\n");
76 hashbin_delete(dongles, NULL);
77 return -ENOMEM;
78 }
79
80 /* We no longer initialise the driver ourselves here, we let
81 * the system do it for us... - Jean II */
82
83 return 0;
84}
85
Samuel Ortiz75a69ac2007-07-18 02:16:30 -070086static void leftover_dongle(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct dongle_reg *reg = arg;
89 IRDA_WARNING("IrDA: Dongle type %x not unregistered\n",
90 reg->type);
91}
92
Samuel Ortiz75a69ac2007-07-18 02:16:30 -070093void irda_device_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Harvey Harrison0dc47872008-03-05 20:47:47 -080095 IRDA_DEBUG(4, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
98
99 hashbin_delete(dongles, leftover_dongle);
100}
101
102/*
103 * Function irda_device_set_media_busy (self, status)
104 *
105 * Called when we have detected that another station is transmitting
106 * in contention mode.
107 */
108void irda_device_set_media_busy(struct net_device *dev, int status)
109{
110 struct irlap_cb *self;
111
Harvey Harrison0dc47872008-03-05 20:47:47 -0800112 IRDA_DEBUG(4, "%s(%s)\n", __func__, status ? "TRUE" : "FALSE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 self = (struct irlap_cb *) dev->atalk_ptr;
115
Jean Tourrilhes7e5c6bc2005-04-16 15:24:11 -0700116 /* Some drivers may enable the receive interrupt before calling
117 * irlap_open(), or they may disable the receive interrupt
118 * after calling irlap_close().
119 * The IrDA stack is protected from this in irlap_driver_rcv().
120 * However, the driver calls directly the wrapper, that calls
121 * us directly. Make sure we protect ourselves.
122 * Jean II */
123 if (!self || self->magic != LAP_MAGIC)
124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 if (status) {
127 self->media_busy = TRUE;
128 if (status == SMALL)
129 irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
130 else
131 irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
132 IRDA_DEBUG( 4, "Media busy!\n");
133 } else {
134 self->media_busy = FALSE;
135 irlap_stop_mbusy_timer(self);
136 }
137}
138EXPORT_SYMBOL(irda_device_set_media_busy);
139
140
141/*
142 * Function irda_device_is_receiving (dev)
143 *
144 * Check if the device driver is currently receiving data
145 *
146 */
147int irda_device_is_receiving(struct net_device *dev)
148{
149 struct if_irda_req req;
150 int ret;
151
Harvey Harrison0dc47872008-03-05 20:47:47 -0800152 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Stephen Hemminger92bcd4f2009-03-20 19:35:33 +0000154 if (!dev->netdev_ops->ndo_do_ioctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800156 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -1;
158 }
159
Stephen Hemminger92bcd4f2009-03-20 19:35:33 +0000160 ret = (dev->netdev_ops->ndo_do_ioctl)(dev, (struct ifreq *) &req,
161 SIOCGRECEIVING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (ret < 0)
163 return ret;
164
165 return req.ifr_receiving;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static void __irda_task_delete(struct irda_task *task)
169{
170 del_timer(&task->timer);
171
172 kfree(task);
173}
174
Adrian Bunke9888f52008-01-19 00:00:42 -0800175static void irda_task_delete(struct irda_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 /* Unregister task */
178 hashbin_remove(tasks, (long) task, NULL);
179
180 __irda_task_delete(task);
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/*
184 * Function irda_task_kick (task)
185 *
186 * Tries to execute a task possible multiple times until the task is either
187 * finished, or askes for a timeout. When a task is finished, we do post
188 * processing, and notify the parent task, that is waiting for this task
189 * to complete.
190 */
191static int irda_task_kick(struct irda_task *task)
192{
193 int finished = TRUE;
194 int count = 0;
195 int timeout;
196
Harvey Harrison0dc47872008-03-05 20:47:47 -0800197 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 IRDA_ASSERT(task != NULL, return -1;);
200 IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
201
202 /* Execute task until it's finished, or askes for a timeout */
203 do {
204 timeout = task->function(task);
205 if (count++ > 100) {
206 IRDA_ERROR("%s: error in task handler!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800207 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 irda_task_delete(task);
209 return TRUE;
210 }
211 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
212
213 if (timeout < 0) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800214 IRDA_ERROR("%s: Error executing task!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 irda_task_delete(task);
216 return TRUE;
217 }
218
219 /* Check if we are finished */
220 if (task->state == IRDA_TASK_DONE) {
221 del_timer(&task->timer);
222
223 /* Do post processing */
224 if (task->finished)
225 task->finished(task);
226
227 /* Notify parent */
228 if (task->parent) {
229 /* Check if parent is waiting for us to complete */
230 if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
231 task->parent->state = IRDA_TASK_CHILD_DONE;
232
233 /* Stop timer now that we are here */
234 del_timer(&task->parent->timer);
235
236 /* Kick parent task */
237 irda_task_kick(task->parent);
238 }
239 }
240 irda_task_delete(task);
241 } else if (timeout > 0) {
242 irda_start_timer(&task->timer, timeout, (void *) task,
243 irda_task_timer_expired);
244 finished = FALSE;
245 } else {
246 IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800247 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 finished = FALSE;
249 }
250
251 return finished;
252}
253
254/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * Function irda_task_timer_expired (data)
256 *
257 * Task time has expired. We now try to execute task (again), and restart
258 * the timer if the task has not finished yet
259 */
260static void irda_task_timer_expired(void *data)
261{
262 struct irda_task *task;
263
Harvey Harrison0dc47872008-03-05 20:47:47 -0800264 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Joe Perchesea110732011-06-13 16:21:26 +0000266 task = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 irda_task_kick(task);
269}
270
271/*
272 * Function irda_device_setup (dev)
273 *
274 * This function should be used by low level device drivers in a similar way
275 * as ether_setup() is used by normal network device drivers
276 */
277static void irda_device_setup(struct net_device *dev)
278{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900279 dev->hard_header_len = 0;
280 dev->addr_len = LAP_ALEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900282 dev->type = ARPHRD_IRDA;
283 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Samuel Ortizd93077f2006-02-09 16:58:46 -0800285 memset(dev->broadcast, 0xff, LAP_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 dev->mtu = 2048;
288 dev->flags = IFF_NOARP;
289}
290
291/*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900292 * Funciton alloc_irdadev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * Allocates and sets up an IRDA device in a manner similar to
294 * alloc_etherdev.
295 */
296struct net_device *alloc_irdadev(int sizeof_priv)
297{
298 return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
299}
300EXPORT_SYMBOL(alloc_irdadev);
301
Al Viro56c3b7d2005-05-04 05:39:52 +0100302#ifdef CONFIG_ISA_DMA_API
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * Function setup_dma (idev, buffer, count, mode)
305 *
Andi Kleenb6d9a5d2005-04-16 15:24:56 -0700306 * Setup the DMA channel. Commonly used by LPC FIR drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 *
308 */
309void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
310{
311 unsigned long flags;
312
313 flags = claim_dma_lock();
314
315 disable_dma(channel);
316 clear_dma_ff(channel);
317 set_dma_mode(channel, mode);
318 set_dma_addr(channel, buffer);
319 set_dma_count(channel, count);
320 enable_dma(channel);
321
322 release_dma_lock(flags);
323}
324EXPORT_SYMBOL(irda_setup_dma);
Al Viro56c3b7d2005-05-04 05:39:52 +0100325#endif