blob: ce0e23148cdde2bf6cb714da88f528255d9f6b08 [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001/*
Ivo van Doornfe242cf2007-09-27 14:57:05 -07002 * Copyright (C) 2006 - 2007 Ivo van Doorn
Ivo van Doorncf4328c2007-05-07 00:34:20 -07003 * Copyright (C) 2007 Dmitry Torokhov
4 *
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.
9 *
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.
14 *
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.
19 */
20
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>
29
Michael Buesch27366222007-11-02 20:18:11 +010030/* Get declaration of rfkill_switch_all() to shut up sparse. */
31#include "rfkill-input.h"
32
33
Ivo van Doorncf4328c2007-05-07 00:34:20 -070034MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35MODULE_VERSION("1.0");
36MODULE_DESCRIPTION("RF switch support");
37MODULE_LICENSE("GPL");
38
39static LIST_HEAD(rfkill_list); /* list of registered rf switches */
40static DEFINE_MUTEX(rfkill_mutex);
41
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -030042static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -030043module_param_named(default_state, rfkill_default_state, uint, 0444);
44MODULE_PARM_DESC(default_state,
45 "Default initial state for all radio types, 0 = radio off");
46
Ivo van Doorncf4328c2007-05-07 00:34:20 -070047static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
48
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -030049static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
50
51
52/**
53 * register_rfkill_notifier - Add notifier to rfkill notifier chain
54 * @nb: pointer to the new entry to add to the chain
55 *
56 * See blocking_notifier_chain_register() for return value and further
57 * observations.
58 *
59 * Adds a notifier to the rfkill notifier chain. The chain will be
60 * called with a pointer to the relevant rfkill structure as a parameter,
61 * refer to include/linux/rfkill.h for the possible events.
62 *
63 * Notifiers added to this chain are to always return NOTIFY_DONE. This
64 * chain is a blocking notifier chain: notifiers can sleep.
65 *
66 * Calls to this chain may have been done through a workqueue. One must
67 * assume unordered asynchronous behaviour, there is no way to know if
68 * actions related to the event that generated the notification have been
69 * carried out already.
70 */
71int register_rfkill_notifier(struct notifier_block *nb)
72{
73 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
74}
75EXPORT_SYMBOL_GPL(register_rfkill_notifier);
76
77/**
78 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
79 * @nb: pointer to the entry to remove from the chain
80 *
81 * See blocking_notifier_chain_unregister() for return value and further
82 * observations.
83 *
84 * Removes a notifier from the rfkill notifier chain.
85 */
86int unregister_rfkill_notifier(struct notifier_block *nb)
87{
88 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
89}
90EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
91
Michael Buesch135900c2007-09-27 21:33:12 +020092
93static void rfkill_led_trigger(struct rfkill *rfkill,
94 enum rfkill_state state)
95{
96#ifdef CONFIG_RFKILL_LEDS
97 struct led_trigger *led = &rfkill->led_trigger;
98
99 if (!led->name)
100 return;
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300101 if (state != RFKILL_STATE_UNBLOCKED)
Michael Buesch135900c2007-09-27 21:33:12 +0200102 led_trigger_event(led, LED_OFF);
103 else
104 led_trigger_event(led, LED_FULL);
105#endif /* CONFIG_RFKILL_LEDS */
106}
107
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300108static void notify_rfkill_state_change(struct rfkill *rfkill)
109{
110 blocking_notifier_call_chain(&rfkill_notifier_list,
111 RFKILL_STATE_CHANGED,
112 rfkill);
113}
114
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300115static void update_rfkill_state(struct rfkill *rfkill)
116{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300117 enum rfkill_state newstate, oldstate;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300118
119 if (rfkill->get_state) {
120 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300121 if (!rfkill->get_state(rfkill->data, &newstate)) {
122 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300123 rfkill->state = newstate;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300124 if (oldstate != newstate)
125 notify_rfkill_state_change(rfkill);
126 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300127 mutex_unlock(&rfkill->mutex);
128 }
129}
130
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300131/**
132 * rfkill_toggle_radio - wrapper for toggle_radio hook
133 * calls toggle_radio taking into account a lot of "small"
134 * details.
135 * @rfkill: the rfkill struct to use
136 * @force: calls toggle_radio even if cache says it is not needed,
137 * and also makes sure notifications of the state will be
138 * sent even if it didn't change
139 * @state: the new state to call toggle_radio() with
140 *
141 * This wrappen protects and enforces the API for toggle_radio
142 * calls. Note that @force cannot override a (possibly cached)
143 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
144 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
145 * rfkill_force_state(), so the cache either is bypassed or valid.
146 *
147 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
148 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
149 * give the driver a hint that it should double-BLOCK the transmitter.
150 *
151 * Caller must have aquired rfkill_mutex.
152 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700153static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300154 enum rfkill_state state,
155 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700156{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100157 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300158 enum rfkill_state oldstate, newstate;
159
160 oldstate = rfkill->state;
161
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300162 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300163 !rfkill->get_state(rfkill->data, &newstate))
164 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700165
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300166 switch (state) {
167 case RFKILL_STATE_HARD_BLOCKED:
168 /* typically happens when refreshing hardware state,
169 * such as on resume */
170 state = RFKILL_STATE_SOFT_BLOCKED;
171 break;
172 case RFKILL_STATE_UNBLOCKED:
173 /* force can't override this, only rfkill_force_state() can */
174 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
175 return -EPERM;
176 break;
177 case RFKILL_STATE_SOFT_BLOCKED:
178 /* nothing to do, we want to give drivers the hint to double
179 * BLOCK even a transmitter that is already in state
180 * RFKILL_STATE_HARD_BLOCKED */
181 break;
182 }
183
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300184 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700185 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300186 /* never allow a HARD->SOFT downgrade! */
187 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700188 rfkill->state = state;
189 }
190
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300191 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300192 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300193 notify_rfkill_state_change(rfkill);
194 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300195
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700196 return retval;
197}
198
199/**
200 * rfkill_switch_all - Toggle state of all switches of given type
201 * @type: type of interfaces to be affeceted
202 * @state: the new state
203 *
204 * This function toggles state of all switches of given type unless
205 * a specific switch is claimed by userspace in which case it is
206 * left alone.
207 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700208void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
209{
210 struct rfkill *rfkill;
211
212 mutex_lock(&rfkill_mutex);
213
214 rfkill_states[type] = state;
215
216 list_for_each_entry(rfkill, &rfkill_list, node) {
Carlos Corbacho89796f62008-04-12 16:39:47 +0100217 if ((!rfkill->user_claim) && (rfkill->type == type))
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300218 rfkill_toggle_radio(rfkill, state, 0);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700219 }
220
221 mutex_unlock(&rfkill_mutex);
222}
223EXPORT_SYMBOL(rfkill_switch_all);
224
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300225/**
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300226 * rfkill_epo - emergency power off all transmitters
227 *
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300228 * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300229 * everything in its path but rfkill_mutex.
230 */
231void rfkill_epo(void)
232{
233 struct rfkill *rfkill;
234
235 mutex_lock(&rfkill_mutex);
236 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300237 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300238 }
239 mutex_unlock(&rfkill_mutex);
240}
241EXPORT_SYMBOL_GPL(rfkill_epo);
242
243/**
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300244 * rfkill_force_state - Force the internal rfkill radio state
245 * @rfkill: pointer to the rfkill class to modify.
246 * @state: the current radio state the class should be forced to.
247 *
248 * This function updates the internal state of the radio cached
249 * by the rfkill class. It should be used when the driver gets
250 * a notification by the firmware/hardware of the current *real*
251 * state of the radio rfkill switch.
252 *
253 * It may not be called from an atomic context.
254 */
255int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
256{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300257 enum rfkill_state oldstate;
258
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300259 if (state != RFKILL_STATE_SOFT_BLOCKED &&
260 state != RFKILL_STATE_UNBLOCKED &&
261 state != RFKILL_STATE_HARD_BLOCKED)
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300262 return -EINVAL;
263
264 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300265
266 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300267 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300268
269 if (state != oldstate)
270 notify_rfkill_state_change(rfkill);
271
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300272 mutex_unlock(&rfkill->mutex);
273
274 return 0;
275}
276EXPORT_SYMBOL(rfkill_force_state);
277
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700278static ssize_t rfkill_name_show(struct device *dev,
279 struct device_attribute *attr,
280 char *buf)
281{
282 struct rfkill *rfkill = to_rfkill(dev);
283
284 return sprintf(buf, "%s\n", rfkill->name);
285}
286
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300287static const char *rfkill_get_type_str(enum rfkill_type type)
288{
289 switch (type) {
290 case RFKILL_TYPE_WLAN:
291 return "wlan";
292 case RFKILL_TYPE_BLUETOOTH:
293 return "bluetooth";
294 case RFKILL_TYPE_UWB:
295 return "ultrawideband";
296 case RFKILL_TYPE_WIMAX:
297 return "wimax";
298 case RFKILL_TYPE_WWAN:
299 return "wwan";
300 default:
301 BUG();
302 }
303}
304
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700305static ssize_t rfkill_type_show(struct device *dev,
306 struct device_attribute *attr,
307 char *buf)
308{
309 struct rfkill *rfkill = to_rfkill(dev);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700310
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300311 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700312}
313
314static ssize_t rfkill_state_show(struct device *dev,
315 struct device_attribute *attr,
316 char *buf)
317{
318 struct rfkill *rfkill = to_rfkill(dev);
319
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300320 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700321 return sprintf(buf, "%d\n", rfkill->state);
322}
323
324static ssize_t rfkill_state_store(struct device *dev,
325 struct device_attribute *attr,
326 const char *buf, size_t count)
327{
328 struct rfkill *rfkill = to_rfkill(dev);
329 unsigned int state = simple_strtoul(buf, NULL, 0);
330 int error;
331
332 if (!capable(CAP_NET_ADMIN))
333 return -EPERM;
334
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300335 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
336 if (state != RFKILL_STATE_UNBLOCKED &&
337 state != RFKILL_STATE_SOFT_BLOCKED)
338 return -EINVAL;
339
Michael Buesch7f4c5342007-11-28 17:49:34 +0100340 if (mutex_lock_interruptible(&rfkill->mutex))
341 return -ERESTARTSYS;
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300342 error = rfkill_toggle_radio(rfkill, state, 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100343 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700344
Michael Buesch7f4c5342007-11-28 17:49:34 +0100345 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700346}
347
348static ssize_t rfkill_claim_show(struct device *dev,
349 struct device_attribute *attr,
350 char *buf)
351{
352 struct rfkill *rfkill = to_rfkill(dev);
353
354 return sprintf(buf, "%d", rfkill->user_claim);
355}
356
357static ssize_t rfkill_claim_store(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
360{
361 struct rfkill *rfkill = to_rfkill(dev);
362 bool claim = !!simple_strtoul(buf, NULL, 0);
363 int error;
364
365 if (!capable(CAP_NET_ADMIN))
366 return -EPERM;
367
368 /*
369 * Take the global lock to make sure the kernel is not in
370 * the middle of rfkill_switch_all
371 */
372 error = mutex_lock_interruptible(&rfkill_mutex);
373 if (error)
374 return error;
375
Michael Buesch20405c02007-09-27 21:34:23 +0200376 if (rfkill->user_claim_unsupported) {
377 error = -EOPNOTSUPP;
378 goto out_unlock;
379 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700380 if (rfkill->user_claim != claim) {
381 if (!claim)
382 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300383 rfkill_states[rfkill->type],
384 0);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700385 rfkill->user_claim = claim;
386 }
387
Michael Buesch20405c02007-09-27 21:34:23 +0200388out_unlock:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700389 mutex_unlock(&rfkill_mutex);
390
Michael Buesch20405c02007-09-27 21:34:23 +0200391 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700392}
393
394static struct device_attribute rfkill_dev_attrs[] = {
395 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
396 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700397 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700398 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
399 __ATTR_NULL
400};
401
402static void rfkill_release(struct device *dev)
403{
404 struct rfkill *rfkill = to_rfkill(dev);
405
406 kfree(rfkill);
407 module_put(THIS_MODULE);
408}
409
410#ifdef CONFIG_PM
411static int rfkill_suspend(struct device *dev, pm_message_t state)
412{
413 struct rfkill *rfkill = to_rfkill(dev);
414
415 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100416 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300417 /* Stop transmitter, keep state, no notifies */
418 update_rfkill_state(rfkill);
419
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700420 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300421 rfkill->toggle_radio(rfkill->data,
422 RFKILL_STATE_SOFT_BLOCKED);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700423 mutex_unlock(&rfkill->mutex);
424 }
425
426 dev->power.power_state = state;
427 }
428
429 return 0;
430}
431
432static int rfkill_resume(struct device *dev)
433{
434 struct rfkill *rfkill = to_rfkill(dev);
435
436 if (dev->power.power_state.event != PM_EVENT_ON) {
437 mutex_lock(&rfkill->mutex);
438
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300439 /* restore radio state AND notify everybody */
440 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700441
442 mutex_unlock(&rfkill->mutex);
443 }
444
445 dev->power.power_state = PMSG_ON;
446 return 0;
447}
448#else
449#define rfkill_suspend NULL
450#define rfkill_resume NULL
451#endif
452
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300453static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
454 unsigned long eventid,
455 void *data)
456{
457 struct rfkill *rfkill = (struct rfkill *)data;
458
459 switch (eventid) {
460 case RFKILL_STATE_CHANGED:
461 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
462 break;
463 default:
464 break;
465 }
466
467 return NOTIFY_DONE;
468}
469
470static struct notifier_block rfkill_blocking_uevent_nb = {
471 .notifier_call = rfkill_blocking_uevent_notifier,
472 .priority = 0,
473};
474
475static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
476{
477 struct rfkill *rfkill = to_rfkill(dev);
478 int error;
479
480 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
481 if (error)
482 return error;
483 error = add_uevent_var(env, "RFKILL_TYPE=%s",
484 rfkill_get_type_str(rfkill->type));
485 if (error)
486 return error;
487 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
488 return error;
489}
490
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700491static struct class rfkill_class = {
492 .name = "rfkill",
493 .dev_release = rfkill_release,
494 .dev_attrs = rfkill_dev_attrs,
495 .suspend = rfkill_suspend,
496 .resume = rfkill_resume,
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300497 .dev_uevent = rfkill_dev_uevent,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700498};
499
500static int rfkill_add_switch(struct rfkill *rfkill)
501{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100502 int error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700503
Michael Buesch7319f1e2007-10-28 15:16:50 +0100504 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700505
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300506 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100507 if (!error)
508 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700509
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700510 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100511
512 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700513}
514
515static void rfkill_remove_switch(struct rfkill *rfkill)
516{
517 mutex_lock(&rfkill_mutex);
518 list_del_init(&rfkill->node);
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300519 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700520 mutex_unlock(&rfkill_mutex);
521}
522
523/**
524 * rfkill_allocate - allocate memory for rfkill structure.
525 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200526 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700527 *
528 * This function should be called by the network driver when it needs
529 * rfkill structure. Once the structure is allocated the driver shoud
530 * finish its initialization by setting name, private data, enable_radio
531 * and disable_radio methods and then register it with rfkill_register().
532 * NOTE: If registration fails the structure shoudl be freed by calling
533 * rfkill_free() otherwise rfkill_unregister() should be used.
534 */
535struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
536{
537 struct rfkill *rfkill;
538 struct device *dev;
539
540 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700541 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700542 return NULL;
543
544 mutex_init(&rfkill->mutex);
545 INIT_LIST_HEAD(&rfkill->node);
546 rfkill->type = type;
547
548 dev = &rfkill->dev;
549 dev->class = &rfkill_class;
550 dev->parent = parent;
551 device_initialize(dev);
552
553 __module_get(THIS_MODULE);
554
555 return rfkill;
556}
557EXPORT_SYMBOL(rfkill_allocate);
558
559/**
560 * rfkill_free - Mark rfkill structure for deletion
561 * @rfkill: rfkill structure to be destroyed
562 *
Oliver Pinter1dbede82008-02-03 17:55:45 +0200563 * Decrements reference count of rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700564 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
565 */
566void rfkill_free(struct rfkill *rfkill)
567{
568 if (rfkill)
569 put_device(&rfkill->dev);
570}
571EXPORT_SYMBOL(rfkill_free);
572
Michael Buesch135900c2007-09-27 21:33:12 +0200573static void rfkill_led_trigger_register(struct rfkill *rfkill)
574{
575#ifdef CONFIG_RFKILL_LEDS
576 int error;
577
578 rfkill->led_trigger.name = rfkill->dev.bus_id;
579 error = led_trigger_register(&rfkill->led_trigger);
580 if (error)
581 rfkill->led_trigger.name = NULL;
582#endif /* CONFIG_RFKILL_LEDS */
583}
584
585static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
586{
587#ifdef CONFIG_RFKILL_LEDS
588 if (rfkill->led_trigger.name)
589 led_trigger_unregister(&rfkill->led_trigger);
590#endif
591}
592
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700593/**
594 * rfkill_register - Register a rfkill structure.
595 * @rfkill: rfkill structure to be registered
596 *
597 * This function should be called by the network driver when the rfkill
598 * structure needs to be registered. Immediately from registration the
599 * switch driver should be able to service calls to toggle_radio.
600 */
601int rfkill_register(struct rfkill *rfkill)
602{
603 static atomic_t rfkill_no = ATOMIC_INIT(0);
604 struct device *dev = &rfkill->dev;
605 int error;
606
607 if (!rfkill->toggle_radio)
608 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100609 if (rfkill->type >= RFKILL_TYPE_MAX)
610 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700611
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100612 snprintf(dev->bus_id, sizeof(dev->bus_id),
613 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
614
615 rfkill_led_trigger_register(rfkill);
616
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700617 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500618 if (error) {
619 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700620 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500621 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700622
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700623 error = device_add(dev);
624 if (error) {
Eric Paris632041f2008-01-13 16:20:56 -0500625 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700626 rfkill_remove_switch(rfkill);
627 return error;
628 }
629
630 return 0;
631}
632EXPORT_SYMBOL(rfkill_register);
633
634/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300635 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700636 * @rfkill: rfkill structure to be unregistered
637 *
638 * This function should be called by the network driver during device
639 * teardown to destroy rfkill structure. Note that rfkill_free() should
640 * _not_ be called after rfkill_unregister().
641 */
642void rfkill_unregister(struct rfkill *rfkill)
643{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700644 device_del(&rfkill->dev);
645 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100646 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700647 put_device(&rfkill->dev);
648}
649EXPORT_SYMBOL(rfkill_unregister);
650
651/*
652 * Rfkill module initialization/deinitialization.
653 */
654static int __init rfkill_init(void)
655{
656 int error;
657 int i;
658
Henrique de Moraes Holschuh5005657c2008-06-23 17:46:42 -0300659 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
660 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
661 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300662 return -EINVAL;
663
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700664 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300665 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700666
667 error = class_register(&rfkill_class);
668 if (error) {
669 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
670 return error;
671 }
672
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300673 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
674
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700675 return 0;
676}
677
678static void __exit rfkill_exit(void)
679{
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300680 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700681 class_unregister(&rfkill_class);
682}
683
Michael Buesch2bf236d2007-10-28 14:39:02 +0100684subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700685module_exit(rfkill_exit);