VIA_VELOCITY: Dont oops on MTU change.
[linux-2.6.22.y-op.git] / net / rfkill / rfkill.c
blobf3986d498b4088622084a121fa0bcc73b07f228e
1 /*
2 * Copyright (C) 2006 Ivo van Doorn
3 * Copyright (C) 2007 Dmitry Torokhov
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/workqueue.h>
25 #include <linux/capability.h>
26 #include <linux/list.h>
27 #include <linux/mutex.h>
28 #include <linux/rfkill.h>
30 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
31 MODULE_VERSION("1.0");
32 MODULE_DESCRIPTION("RF switch support");
33 MODULE_LICENSE("GPL");
35 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
36 static DEFINE_MUTEX(rfkill_mutex);
38 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
40 static int rfkill_toggle_radio(struct rfkill *rfkill,
41 enum rfkill_state state)
43 int retval;
45 retval = mutex_lock_interruptible(&rfkill->mutex);
46 if (retval)
47 return retval;
49 if (state != rfkill->state) {
50 retval = rfkill->toggle_radio(rfkill->data, state);
51 if (!retval)
52 rfkill->state = state;
55 mutex_unlock(&rfkill->mutex);
56 return retval;
59 /**
60 * rfkill_switch_all - Toggle state of all switches of given type
61 * @type: type of interfaces to be affeceted
62 * @state: the new state
64 * This function toggles state of all switches of given type unless
65 * a specific switch is claimed by userspace in which case it is
66 * left alone.
69 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
71 struct rfkill *rfkill;
73 mutex_lock(&rfkill_mutex);
75 rfkill_states[type] = state;
77 list_for_each_entry(rfkill, &rfkill_list, node) {
78 if (!rfkill->user_claim)
79 rfkill_toggle_radio(rfkill, state);
82 mutex_unlock(&rfkill_mutex);
84 EXPORT_SYMBOL(rfkill_switch_all);
86 static ssize_t rfkill_name_show(struct device *dev,
87 struct device_attribute *attr,
88 char *buf)
90 struct rfkill *rfkill = to_rfkill(dev);
92 return sprintf(buf, "%s\n", rfkill->name);
95 static ssize_t rfkill_type_show(struct device *dev,
96 struct device_attribute *attr,
97 char *buf)
99 struct rfkill *rfkill = to_rfkill(dev);
100 const char *type;
102 switch (rfkill->type) {
103 case RFKILL_TYPE_WLAN:
104 type = "wlan";
105 break;
106 case RFKILL_TYPE_BLUETOOTH:
107 type = "bluetooth";
108 break;
109 case RFKILL_TYPE_IRDA:
110 type = "irda";
111 break;
112 default:
113 BUG();
116 return sprintf(buf, "%s\n", type);
119 static ssize_t rfkill_state_show(struct device *dev,
120 struct device_attribute *attr,
121 char *buf)
123 struct rfkill *rfkill = to_rfkill(dev);
125 return sprintf(buf, "%d\n", rfkill->state);
128 static ssize_t rfkill_state_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf, size_t count)
132 struct rfkill *rfkill = to_rfkill(dev);
133 unsigned int state = simple_strtoul(buf, NULL, 0);
134 int error;
136 if (!capable(CAP_NET_ADMIN))
137 return -EPERM;
139 error = rfkill_toggle_radio(rfkill,
140 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
141 if (error)
142 return error;
144 return count;
147 static ssize_t rfkill_claim_show(struct device *dev,
148 struct device_attribute *attr,
149 char *buf)
151 struct rfkill *rfkill = to_rfkill(dev);
153 return sprintf(buf, "%d", rfkill->user_claim);
156 static ssize_t rfkill_claim_store(struct device *dev,
157 struct device_attribute *attr,
158 const char *buf, size_t count)
160 struct rfkill *rfkill = to_rfkill(dev);
161 bool claim = !!simple_strtoul(buf, NULL, 0);
162 int error;
164 if (!capable(CAP_NET_ADMIN))
165 return -EPERM;
168 * Take the global lock to make sure the kernel is not in
169 * the middle of rfkill_switch_all
171 error = mutex_lock_interruptible(&rfkill_mutex);
172 if (error)
173 return error;
175 if (rfkill->user_claim != claim) {
176 if (!claim)
177 rfkill_toggle_radio(rfkill,
178 rfkill_states[rfkill->type]);
179 rfkill->user_claim = claim;
182 mutex_unlock(&rfkill_mutex);
184 return count;
187 static struct device_attribute rfkill_dev_attrs[] = {
188 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
189 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
190 __ATTR(state, S_IRUGO, rfkill_state_show, rfkill_state_store),
191 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
192 __ATTR_NULL
195 static void rfkill_release(struct device *dev)
197 struct rfkill *rfkill = to_rfkill(dev);
199 kfree(rfkill);
200 module_put(THIS_MODULE);
203 #ifdef CONFIG_PM
204 static int rfkill_suspend(struct device *dev, pm_message_t state)
206 struct rfkill *rfkill = to_rfkill(dev);
208 if (dev->power.power_state.event != state.event) {
209 if (state.event == PM_EVENT_SUSPEND) {
210 mutex_lock(&rfkill->mutex);
212 if (rfkill->state == RFKILL_STATE_ON)
213 rfkill->toggle_radio(rfkill->data,
214 RFKILL_STATE_OFF);
216 mutex_unlock(&rfkill->mutex);
219 dev->power.power_state = state;
222 return 0;
225 static int rfkill_resume(struct device *dev)
227 struct rfkill *rfkill = to_rfkill(dev);
229 if (dev->power.power_state.event != PM_EVENT_ON) {
230 mutex_lock(&rfkill->mutex);
232 if (rfkill->state == RFKILL_STATE_ON)
233 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
235 mutex_unlock(&rfkill->mutex);
238 dev->power.power_state = PMSG_ON;
239 return 0;
241 #else
242 #define rfkill_suspend NULL
243 #define rfkill_resume NULL
244 #endif
246 static struct class rfkill_class = {
247 .name = "rfkill",
248 .dev_release = rfkill_release,
249 .dev_attrs = rfkill_dev_attrs,
250 .suspend = rfkill_suspend,
251 .resume = rfkill_resume,
254 static int rfkill_add_switch(struct rfkill *rfkill)
256 int retval;
258 retval = mutex_lock_interruptible(&rfkill_mutex);
259 if (retval)
260 return retval;
262 retval = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
263 if (retval)
264 goto out;
266 list_add_tail(&rfkill->node, &rfkill_list);
268 out:
269 mutex_unlock(&rfkill_mutex);
270 return retval;
273 static void rfkill_remove_switch(struct rfkill *rfkill)
275 mutex_lock(&rfkill_mutex);
276 list_del_init(&rfkill->node);
277 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
278 mutex_unlock(&rfkill_mutex);
282 * rfkill_allocate - allocate memory for rfkill structure.
283 * @parent: device that has rf switch on it
284 * @type: type of the switch (wlan, bluetooth, irda)
286 * This function should be called by the network driver when it needs
287 * rfkill structure. Once the structure is allocated the driver shoud
288 * finish its initialization by setting name, private data, enable_radio
289 * and disable_radio methods and then register it with rfkill_register().
290 * NOTE: If registration fails the structure shoudl be freed by calling
291 * rfkill_free() otherwise rfkill_unregister() should be used.
293 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
295 struct rfkill *rfkill;
296 struct device *dev;
298 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
299 if (!rfkill)
300 return NULL;
302 mutex_init(&rfkill->mutex);
303 INIT_LIST_HEAD(&rfkill->node);
304 rfkill->type = type;
306 dev = &rfkill->dev;
307 dev->class = &rfkill_class;
308 dev->parent = parent;
309 device_initialize(dev);
311 __module_get(THIS_MODULE);
313 return rfkill;
315 EXPORT_SYMBOL(rfkill_allocate);
318 * rfkill_free - Mark rfkill structure for deletion
319 * @rfkill: rfkill structure to be destroyed
321 * Decrements reference count of rfkill structure so it is destoryed.
322 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
324 void rfkill_free(struct rfkill *rfkill)
326 if (rfkill)
327 put_device(&rfkill->dev);
329 EXPORT_SYMBOL(rfkill_free);
332 * rfkill_register - Register a rfkill structure.
333 * @rfkill: rfkill structure to be registered
335 * This function should be called by the network driver when the rfkill
336 * structure needs to be registered. Immediately from registration the
337 * switch driver should be able to service calls to toggle_radio.
339 int rfkill_register(struct rfkill *rfkill)
341 static atomic_t rfkill_no = ATOMIC_INIT(0);
342 struct device *dev = &rfkill->dev;
343 int error;
345 if (!rfkill->toggle_radio)
346 return -EINVAL;
348 error = rfkill_add_switch(rfkill);
349 if (error)
350 return error;
352 snprintf(dev->bus_id, sizeof(dev->bus_id),
353 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
355 error = device_add(dev);
356 if (error) {
357 rfkill_remove_switch(rfkill);
358 return error;
361 return 0;
363 EXPORT_SYMBOL(rfkill_register);
366 * rfkill_unregister - Uegister a rfkill structure.
367 * @rfkill: rfkill structure to be unregistered
369 * This function should be called by the network driver during device
370 * teardown to destroy rfkill structure. Note that rfkill_free() should
371 * _not_ be called after rfkill_unregister().
373 void rfkill_unregister(struct rfkill *rfkill)
375 device_del(&rfkill->dev);
376 rfkill_remove_switch(rfkill);
377 put_device(&rfkill->dev);
379 EXPORT_SYMBOL(rfkill_unregister);
382 * Rfkill module initialization/deinitialization.
384 static int __init rfkill_init(void)
386 int error;
387 int i;
389 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
390 rfkill_states[i] = RFKILL_STATE_ON;
392 error = class_register(&rfkill_class);
393 if (error) {
394 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
395 return error;
398 return 0;
401 static void __exit rfkill_exit(void)
403 class_unregister(&rfkill_class);
406 module_init(rfkill_init);
407 module_exit(rfkill_exit);