Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Jeff Kirsher | d377050 | 2013-12-06 09:13:43 -0800 | [diff] [blame] | 26 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * |
| 28 | ********************************************************************/ |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/string.h> |
| 31 | #include <linux/proc_fs.h> |
| 32 | #include <linux/skbuff.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 33 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 43 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <asm/ioctls.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 46 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 55 | static void __irda_task_delete(struct irda_task *task); |
| 56 | |
| 57 | static hashbin_t *dongles = NULL; |
| 58 | static hashbin_t *tasks = NULL; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static void irda_task_timer_expired(void *data); |
| 61 | |
| 62 | int __init irda_device_init( void) |
| 63 | { |
| 64 | dongles = hashbin_new(HB_NOLOCK); |
| 65 | if (dongles == NULL) { |
Joe Perches | 6c91023 | 2014-11-11 13:37:30 -0800 | [diff] [blame] | 66 | net_warn_ratelimited("IrDA: Can't allocate dongles hashbin!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return -ENOMEM; |
| 68 | } |
| 69 | spin_lock_init(&dongles->hb_spinlock); |
| 70 | |
| 71 | tasks = hashbin_new(HB_LOCK); |
| 72 | if (tasks == NULL) { |
Joe Perches | 6c91023 | 2014-11-11 13:37:30 -0800 | [diff] [blame] | 73 | net_warn_ratelimited("IrDA: Can't allocate tasks hashbin!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Ortiz | 75a69ac | 2007-07-18 02:16:30 -0700 | [diff] [blame] | 84 | static void leftover_dongle(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | struct dongle_reg *reg = arg; |
Joe Perches | 6c91023 | 2014-11-11 13:37:30 -0800 | [diff] [blame] | 87 | net_warn_ratelimited("IrDA: Dongle type %x not unregistered\n", |
| 88 | reg->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Samuel Ortiz | 75a69ac | 2007-07-18 02:16:30 -0700 | [diff] [blame] | 91 | void irda_device_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete); |
| 94 | |
| 95 | hashbin_delete(dongles, leftover_dongle); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Function irda_device_set_media_busy (self, status) |
| 100 | * |
| 101 | * Called when we have detected that another station is transmitting |
| 102 | * in contention mode. |
| 103 | */ |
| 104 | void irda_device_set_media_busy(struct net_device *dev, int status) |
| 105 | { |
| 106 | struct irlap_cb *self; |
| 107 | |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame] | 108 | pr_debug("%s(%s)\n", __func__, status ? "TRUE" : "FALSE"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | self = (struct irlap_cb *) dev->atalk_ptr; |
| 111 | |
Jean Tourrilhes | 7e5c6bc | 2005-04-16 15:24:11 -0700 | [diff] [blame] | 112 | /* Some drivers may enable the receive interrupt before calling |
| 113 | * irlap_open(), or they may disable the receive interrupt |
| 114 | * after calling irlap_close(). |
| 115 | * The IrDA stack is protected from this in irlap_driver_rcv(). |
| 116 | * However, the driver calls directly the wrapper, that calls |
| 117 | * us directly. Make sure we protect ourselves. |
| 118 | * Jean II */ |
| 119 | if (!self || self->magic != LAP_MAGIC) |
| 120 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | if (status) { |
| 123 | self->media_busy = TRUE; |
| 124 | if (status == SMALL) |
| 125 | irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT); |
| 126 | else |
| 127 | irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT); |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame] | 128 | pr_debug("Media busy!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } else { |
| 130 | self->media_busy = FALSE; |
| 131 | irlap_stop_mbusy_timer(self); |
| 132 | } |
| 133 | } |
| 134 | EXPORT_SYMBOL(irda_device_set_media_busy); |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * Function irda_device_is_receiving (dev) |
| 139 | * |
| 140 | * Check if the device driver is currently receiving data |
| 141 | * |
| 142 | */ |
| 143 | int irda_device_is_receiving(struct net_device *dev) |
| 144 | { |
| 145 | struct if_irda_req req; |
| 146 | int ret; |
| 147 | |
Stephen Hemminger | 92bcd4f | 2009-03-20 19:35:33 +0000 | [diff] [blame] | 148 | if (!dev->netdev_ops->ndo_do_ioctl) { |
Joe Perches | 6c91023 | 2014-11-11 13:37:30 -0800 | [diff] [blame] | 149 | net_err_ratelimited("%s: do_ioctl not impl. by device driver\n", |
| 150 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
Stephen Hemminger | 92bcd4f | 2009-03-20 19:35:33 +0000 | [diff] [blame] | 154 | ret = (dev->netdev_ops->ndo_do_ioctl)(dev, (struct ifreq *) &req, |
| 155 | SIOCGRECEIVING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (ret < 0) |
| 157 | return ret; |
| 158 | |
| 159 | return req.ifr_receiving; |
| 160 | } |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | static void __irda_task_delete(struct irda_task *task) |
| 163 | { |
| 164 | del_timer(&task->timer); |
| 165 | |
| 166 | kfree(task); |
| 167 | } |
| 168 | |
Adrian Bunk | e9888f5 | 2008-01-19 00:00:42 -0800 | [diff] [blame] | 169 | static void irda_task_delete(struct irda_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | /* Unregister task */ |
| 172 | hashbin_remove(tasks, (long) task, NULL); |
| 173 | |
| 174 | __irda_task_delete(task); |
| 175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * Function irda_task_kick (task) |
| 179 | * |
| 180 | * Tries to execute a task possible multiple times until the task is either |
| 181 | * finished, or askes for a timeout. When a task is finished, we do post |
| 182 | * processing, and notify the parent task, that is waiting for this task |
| 183 | * to complete. |
| 184 | */ |
| 185 | static int irda_task_kick(struct irda_task *task) |
| 186 | { |
| 187 | int finished = TRUE; |
| 188 | int count = 0; |
| 189 | int timeout; |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | IRDA_ASSERT(task != NULL, return -1;); |
| 192 | IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;); |
| 193 | |
| 194 | /* Execute task until it's finished, or askes for a timeout */ |
| 195 | do { |
| 196 | timeout = task->function(task); |
| 197 | if (count++ > 100) { |
Joe Perches | 6c91023 | 2014-11-11 13:37:30 -0800 | [diff] [blame] | 198 | net_err_ratelimited("%s: error in task handler!\n", |
| 199 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | irda_task_delete(task); |
| 201 | return TRUE; |
| 202 | } |
| 203 | } while ((timeout == 0) && (task->state != IRDA_TASK_DONE)); |
| 204 | |
| 205 | if (timeout < 0) { |
Joe Perches | 6c91023 | 2014-11-11 13:37:30 -0800 | [diff] [blame] | 206 | net_err_ratelimited("%s: Error executing task!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | irda_task_delete(task); |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
| 211 | /* Check if we are finished */ |
| 212 | if (task->state == IRDA_TASK_DONE) { |
| 213 | del_timer(&task->timer); |
| 214 | |
| 215 | /* Do post processing */ |
| 216 | if (task->finished) |
| 217 | task->finished(task); |
| 218 | |
| 219 | /* Notify parent */ |
| 220 | if (task->parent) { |
| 221 | /* Check if parent is waiting for us to complete */ |
| 222 | if (task->parent->state == IRDA_TASK_CHILD_WAIT) { |
| 223 | task->parent->state = IRDA_TASK_CHILD_DONE; |
| 224 | |
| 225 | /* Stop timer now that we are here */ |
| 226 | del_timer(&task->parent->timer); |
| 227 | |
| 228 | /* Kick parent task */ |
| 229 | irda_task_kick(task->parent); |
| 230 | } |
| 231 | } |
| 232 | irda_task_delete(task); |
| 233 | } else if (timeout > 0) { |
| 234 | irda_start_timer(&task->timer, timeout, (void *) task, |
| 235 | irda_task_timer_expired); |
| 236 | finished = FALSE; |
| 237 | } else { |
Joe Perches | 955a9d20 | 2014-11-11 14:44:57 -0800 | [diff] [blame] | 238 | pr_debug("%s(), not finished, and no timeout!\n", |
| 239 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | finished = FALSE; |
| 241 | } |
| 242 | |
| 243 | return finished; |
| 244 | } |
| 245 | |
| 246 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | * Function irda_task_timer_expired (data) |
| 248 | * |
| 249 | * Task time has expired. We now try to execute task (again), and restart |
| 250 | * the timer if the task has not finished yet |
| 251 | */ |
| 252 | static void irda_task_timer_expired(void *data) |
| 253 | { |
| 254 | struct irda_task *task; |
| 255 | |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 256 | task = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | irda_task_kick(task); |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Function irda_device_setup (dev) |
| 263 | * |
| 264 | * This function should be used by low level device drivers in a similar way |
| 265 | * as ether_setup() is used by normal network device drivers |
| 266 | */ |
| 267 | static void irda_device_setup(struct net_device *dev) |
| 268 | { |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 269 | dev->hard_header_len = 0; |
| 270 | dev->addr_len = LAP_ALEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 272 | dev->type = ARPHRD_IRDA; |
| 273 | dev->tx_queue_len = 8; /* Window size + 1 s-frame */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Samuel Ortiz | d93077f | 2006-02-09 16:58:46 -0800 | [diff] [blame] | 275 | memset(dev->broadcast, 0xff, LAP_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | dev->mtu = 2048; |
| 278 | dev->flags = IFF_NOARP; |
| 279 | } |
| 280 | |
| 281 | /* |
YOSHIFUJI Hideaki | 6819bc2 | 2007-02-09 23:24:53 +0900 | [diff] [blame] | 282 | * Funciton alloc_irdadev |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * Allocates and sets up an IRDA device in a manner similar to |
| 284 | * alloc_etherdev. |
| 285 | */ |
| 286 | struct net_device *alloc_irdadev(int sizeof_priv) |
| 287 | { |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 288 | return alloc_netdev(sizeof_priv, "irda%d", NET_NAME_UNKNOWN, |
| 289 | irda_device_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | EXPORT_SYMBOL(alloc_irdadev); |
| 292 | |
Al Viro | 56c3b7d | 2005-05-04 05:39:52 +0100 | [diff] [blame] | 293 | #ifdef CONFIG_ISA_DMA_API |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | /* |
| 295 | * Function setup_dma (idev, buffer, count, mode) |
| 296 | * |
Andi Kleen | b6d9a5d | 2005-04-16 15:24:56 -0700 | [diff] [blame] | 297 | * Setup the DMA channel. Commonly used by LPC FIR drivers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | * |
| 299 | */ |
| 300 | void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode) |
| 301 | { |
| 302 | unsigned long flags; |
| 303 | |
| 304 | flags = claim_dma_lock(); |
| 305 | |
| 306 | disable_dma(channel); |
| 307 | clear_dma_ff(channel); |
| 308 | set_dma_mode(channel, mode); |
| 309 | set_dma_addr(channel, buffer); |
| 310 | set_dma_count(channel, count); |
| 311 | enable_dma(channel); |
| 312 | |
| 313 | release_dma_lock(flags); |
| 314 | } |
| 315 | EXPORT_SYMBOL(irda_setup_dma); |
Al Viro | 56c3b7d | 2005-05-04 05:39:52 +0100 | [diff] [blame] | 316 | #endif |