blob: 9e0d909390fd0569a50a2e407ee33e2610ae9cae [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
Jeff Kirsherd3770502013-12-06 09:13:43 -080026 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 ********************************************************************/
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/string.h>
31#include <linux/proc_fs.h>
32#include <linux/skbuff.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080033#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/if.h>
35#include <linux/if_ether.h>
36#include <linux/if_arp.h>
37#include <linux/netdevice.h>
38#include <linux/init.h>
39#include <linux/tty.h>
40#include <linux/kmod.h>
41#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040043#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static 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
Samuel Ortiz75a69ac2007-07-18 02:16:30 -070084static void leftover_dongle(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct dongle_reg *reg = arg;
87 IRDA_WARNING("IrDA: Dongle type %x not unregistered\n",
88 reg->type);
89}
90
Samuel Ortiz75a69ac2007-07-18 02:16:30 -070091void irda_device_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Harvey Harrison0dc47872008-03-05 20:47:47 -080093 IRDA_DEBUG(4, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800110 IRDA_DEBUG(4, "%s(%s)\n", __func__, status ? "TRUE" : "FALSE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 self = (struct irlap_cb *) dev->atalk_ptr;
113
Jean Tourrilhes7e5c6bc2005-04-16 15:24:11 -0700114 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800150 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Stephen Hemminger92bcd4f2009-03-20 19:35:33 +0000152 if (!dev->netdev_ops->ndo_do_ioctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800154 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -1;
156 }
157
Stephen Hemminger92bcd4f2009-03-20 19:35:33 +0000158 ret = (dev->netdev_ops->ndo_do_ioctl)(dev, (struct ifreq *) &req,
159 SIOCGRECEIVING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (ret < 0)
161 return ret;
162
163 return req.ifr_receiving;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void __irda_task_delete(struct irda_task *task)
167{
168 del_timer(&task->timer);
169
170 kfree(task);
171}
172
Adrian Bunke9888f52008-01-19 00:00:42 -0800173static void irda_task_delete(struct irda_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 /* Unregister task */
176 hashbin_remove(tasks, (long) task, NULL);
177
178 __irda_task_delete(task);
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/*
182 * Function irda_task_kick (task)
183 *
184 * Tries to execute a task possible multiple times until the task is either
185 * finished, or askes for a timeout. When a task is finished, we do post
186 * processing, and notify the parent task, that is waiting for this task
187 * to complete.
188 */
189static int irda_task_kick(struct irda_task *task)
190{
191 int finished = TRUE;
192 int count = 0;
193 int timeout;
194
Harvey Harrison0dc47872008-03-05 20:47:47 -0800195 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 IRDA_ASSERT(task != NULL, return -1;);
198 IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
199
200 /* Execute task until it's finished, or askes for a timeout */
201 do {
202 timeout = task->function(task);
203 if (count++ > 100) {
204 IRDA_ERROR("%s: error in task handler!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800205 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 irda_task_delete(task);
207 return TRUE;
208 }
209 } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
210
211 if (timeout < 0) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800212 IRDA_ERROR("%s: Error executing task!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 irda_task_delete(task);
214 return TRUE;
215 }
216
217 /* Check if we are finished */
218 if (task->state == IRDA_TASK_DONE) {
219 del_timer(&task->timer);
220
221 /* Do post processing */
222 if (task->finished)
223 task->finished(task);
224
225 /* Notify parent */
226 if (task->parent) {
227 /* Check if parent is waiting for us to complete */
228 if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
229 task->parent->state = IRDA_TASK_CHILD_DONE;
230
231 /* Stop timer now that we are here */
232 del_timer(&task->parent->timer);
233
234 /* Kick parent task */
235 irda_task_kick(task->parent);
236 }
237 }
238 irda_task_delete(task);
239 } else if (timeout > 0) {
240 irda_start_timer(&task->timer, timeout, (void *) task,
241 irda_task_timer_expired);
242 finished = FALSE;
243 } else {
244 IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800245 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 finished = FALSE;
247 }
248
249 return finished;
250}
251
252/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * Function irda_task_timer_expired (data)
254 *
255 * Task time has expired. We now try to execute task (again), and restart
256 * the timer if the task has not finished yet
257 */
258static void irda_task_timer_expired(void *data)
259{
260 struct irda_task *task;
261
Harvey Harrison0dc47872008-03-05 20:47:47 -0800262 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Joe Perchesea110732011-06-13 16:21:26 +0000264 task = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 irda_task_kick(task);
267}
268
269/*
270 * Function irda_device_setup (dev)
271 *
272 * This function should be used by low level device drivers in a similar way
273 * as ether_setup() is used by normal network device drivers
274 */
275static void irda_device_setup(struct net_device *dev)
276{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900277 dev->hard_header_len = 0;
278 dev->addr_len = LAP_ALEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900280 dev->type = ARPHRD_IRDA;
281 dev->tx_queue_len = 8; /* Window size + 1 s-frame */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Samuel Ortizd93077f2006-02-09 16:58:46 -0800283 memset(dev->broadcast, 0xff, LAP_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 dev->mtu = 2048;
286 dev->flags = IFF_NOARP;
287}
288
289/*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900290 * Funciton alloc_irdadev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * Allocates and sets up an IRDA device in a manner similar to
292 * alloc_etherdev.
293 */
294struct net_device *alloc_irdadev(int sizeof_priv)
295{
Tom Gundersenc835a672014-07-14 16:37:24 +0200296 return alloc_netdev(sizeof_priv, "irda%d", NET_NAME_UNKNOWN,
297 irda_device_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299EXPORT_SYMBOL(alloc_irdadev);
300
Al Viro56c3b7d2005-05-04 05:39:52 +0100301#ifdef CONFIG_ISA_DMA_API
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * Function setup_dma (idev, buffer, count, mode)
304 *
Andi Kleenb6d9a5d2005-04-16 15:24:56 -0700305 * Setup the DMA channel. Commonly used by LPC FIR drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 */
308void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
309{
310 unsigned long flags;
311
312 flags = claim_dma_lock();
313
314 disable_dma(channel);
315 clear_dma_ff(channel);
316 set_dma_mode(channel, mode);
317 set_dma_addr(channel, buffer);
318 set_dma_count(channel, count);
319 enable_dma(channel);
320
321 release_dma_lock(flags);
322}
323EXPORT_SYMBOL(irda_setup_dma);
Al Viro56c3b7d2005-05-04 05:39:52 +0100324#endif