blob: d2d45655cd1a69582a8fdc2948c69c652ed0fd19 [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 Holschuh50056572008-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 Holschuh50056572008-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
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400108#ifdef CONFIG_RFKILL_LEDS
109static void rfkill_led_trigger_activate(struct led_classdev *led)
110{
111 struct rfkill *rfkill = container_of(led->trigger,
112 struct rfkill, led_trigger);
113
114 rfkill_led_trigger(rfkill, rfkill->state);
115}
116#endif /* CONFIG_RFKILL_LEDS */
117
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300118static void notify_rfkill_state_change(struct rfkill *rfkill)
119{
120 blocking_notifier_call_chain(&rfkill_notifier_list,
121 RFKILL_STATE_CHANGED,
122 rfkill);
123}
124
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300125static void update_rfkill_state(struct rfkill *rfkill)
126{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300127 enum rfkill_state newstate, oldstate;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300128
129 if (rfkill->get_state) {
130 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300131 if (!rfkill->get_state(rfkill->data, &newstate)) {
132 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300133 rfkill->state = newstate;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300134 if (oldstate != newstate)
135 notify_rfkill_state_change(rfkill);
136 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300137 mutex_unlock(&rfkill->mutex);
138 }
139}
140
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300141/**
142 * rfkill_toggle_radio - wrapper for toggle_radio hook
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300143 * @rfkill: the rfkill struct to use
144 * @force: calls toggle_radio even if cache says it is not needed,
145 * and also makes sure notifications of the state will be
146 * sent even if it didn't change
147 * @state: the new state to call toggle_radio() with
148 *
Henrique de Moraes Holschuh0f687e92008-07-03 13:14:56 -0300149 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
150 * calls and handling all the red tape such as issuing notifications
151 * if the call is successful.
152 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300153 * Note that the @force parameter cannot override a (possibly cached)
154 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300155 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
156 * rfkill_force_state(), so the cache either is bypassed or valid.
157 *
158 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
159 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
160 * give the driver a hint that it should double-BLOCK the transmitter.
161 *
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300162 * Caller must have acquired rfkill->mutex.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300163 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700164static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300165 enum rfkill_state state,
166 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700167{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100168 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300169 enum rfkill_state oldstate, newstate;
170
171 oldstate = rfkill->state;
172
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300173 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300174 !rfkill->get_state(rfkill->data, &newstate))
175 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700176
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300177 switch (state) {
178 case RFKILL_STATE_HARD_BLOCKED:
179 /* typically happens when refreshing hardware state,
180 * such as on resume */
181 state = RFKILL_STATE_SOFT_BLOCKED;
182 break;
183 case RFKILL_STATE_UNBLOCKED:
184 /* force can't override this, only rfkill_force_state() can */
185 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
186 return -EPERM;
187 break;
188 case RFKILL_STATE_SOFT_BLOCKED:
189 /* nothing to do, we want to give drivers the hint to double
190 * BLOCK even a transmitter that is already in state
191 * RFKILL_STATE_HARD_BLOCKED */
192 break;
193 }
194
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300195 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700196 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300197 /* never allow a HARD->SOFT downgrade! */
198 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700199 rfkill->state = state;
200 }
201
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300202 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300203 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300204 notify_rfkill_state_change(rfkill);
205 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300206
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700207 return retval;
208}
209
210/**
211 * rfkill_switch_all - Toggle state of all switches of given type
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300212 * @type: type of interfaces to be affected
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700213 * @state: the new state
214 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300215 * This function toggles the state of all switches of given type,
216 * unless a specific switch is claimed by userspace (in which case,
217 * that switch is left alone).
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700218 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700219void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
220{
221 struct rfkill *rfkill;
222
223 mutex_lock(&rfkill_mutex);
224
225 rfkill_states[type] = state;
226
227 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300228 if ((!rfkill->user_claim) && (rfkill->type == type)) {
229 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300230 rfkill_toggle_radio(rfkill, state, 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300231 mutex_unlock(&rfkill->mutex);
232 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700233 }
234
235 mutex_unlock(&rfkill_mutex);
236}
237EXPORT_SYMBOL(rfkill_switch_all);
238
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300239/**
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300240 * rfkill_epo - emergency power off all transmitters
241 *
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300242 * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300243 * everything in its path but rfkill_mutex and rfkill->mutex.
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300244 */
245void rfkill_epo(void)
246{
247 struct rfkill *rfkill;
248
249 mutex_lock(&rfkill_mutex);
250 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300251 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300252 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300253 mutex_unlock(&rfkill->mutex);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300254 }
255 mutex_unlock(&rfkill_mutex);
256}
257EXPORT_SYMBOL_GPL(rfkill_epo);
258
259/**
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300260 * rfkill_force_state - Force the internal rfkill radio state
261 * @rfkill: pointer to the rfkill class to modify.
262 * @state: the current radio state the class should be forced to.
263 *
264 * This function updates the internal state of the radio cached
265 * by the rfkill class. It should be used when the driver gets
266 * a notification by the firmware/hardware of the current *real*
267 * state of the radio rfkill switch.
268 *
Henrique de Moraes Holschuh2fd9b222008-07-21 21:18:17 -0300269 * Devices which are subject to external changes on their rfkill
270 * state (such as those caused by a hardware rfkill line) MUST
271 * have their driver arrange to call rfkill_force_state() as soon
272 * as possible after such a change.
273 *
274 * This function may not be called from an atomic context.
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300275 */
276int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
277{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300278 enum rfkill_state oldstate;
279
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300280 if (state != RFKILL_STATE_SOFT_BLOCKED &&
281 state != RFKILL_STATE_UNBLOCKED &&
282 state != RFKILL_STATE_HARD_BLOCKED)
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300283 return -EINVAL;
284
285 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300286
287 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300288 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300289
290 if (state != oldstate)
291 notify_rfkill_state_change(rfkill);
292
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300293 mutex_unlock(&rfkill->mutex);
294
295 return 0;
296}
297EXPORT_SYMBOL(rfkill_force_state);
298
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700299static ssize_t rfkill_name_show(struct device *dev,
300 struct device_attribute *attr,
301 char *buf)
302{
303 struct rfkill *rfkill = to_rfkill(dev);
304
305 return sprintf(buf, "%s\n", rfkill->name);
306}
307
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300308static const char *rfkill_get_type_str(enum rfkill_type type)
309{
310 switch (type) {
311 case RFKILL_TYPE_WLAN:
312 return "wlan";
313 case RFKILL_TYPE_BLUETOOTH:
314 return "bluetooth";
315 case RFKILL_TYPE_UWB:
316 return "ultrawideband";
317 case RFKILL_TYPE_WIMAX:
318 return "wimax";
319 case RFKILL_TYPE_WWAN:
320 return "wwan";
321 default:
322 BUG();
323 }
324}
325
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700326static ssize_t rfkill_type_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
330 struct rfkill *rfkill = to_rfkill(dev);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700331
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300332 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700333}
334
335static ssize_t rfkill_state_show(struct device *dev,
336 struct device_attribute *attr,
337 char *buf)
338{
339 struct rfkill *rfkill = to_rfkill(dev);
340
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300341 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700342 return sprintf(buf, "%d\n", rfkill->state);
343}
344
345static ssize_t rfkill_state_store(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
348{
349 struct rfkill *rfkill = to_rfkill(dev);
350 unsigned int state = simple_strtoul(buf, NULL, 0);
351 int error;
352
353 if (!capable(CAP_NET_ADMIN))
354 return -EPERM;
355
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300356 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
357 if (state != RFKILL_STATE_UNBLOCKED &&
358 state != RFKILL_STATE_SOFT_BLOCKED)
359 return -EINVAL;
360
Michael Buesch7f4c5342007-11-28 17:49:34 +0100361 if (mutex_lock_interruptible(&rfkill->mutex))
362 return -ERESTARTSYS;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300363 error = rfkill_toggle_radio(rfkill, state, 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100364 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700365
Michael Buesch7f4c5342007-11-28 17:49:34 +0100366 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700367}
368
369static ssize_t rfkill_claim_show(struct device *dev,
370 struct device_attribute *attr,
371 char *buf)
372{
373 struct rfkill *rfkill = to_rfkill(dev);
374
375 return sprintf(buf, "%d", rfkill->user_claim);
376}
377
378static ssize_t rfkill_claim_store(struct device *dev,
379 struct device_attribute *attr,
380 const char *buf, size_t count)
381{
382 struct rfkill *rfkill = to_rfkill(dev);
383 bool claim = !!simple_strtoul(buf, NULL, 0);
384 int error;
385
386 if (!capable(CAP_NET_ADMIN))
387 return -EPERM;
388
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300389 if (rfkill->user_claim_unsupported)
390 return -EOPNOTSUPP;
391
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700392 /*
393 * Take the global lock to make sure the kernel is not in
394 * the middle of rfkill_switch_all
395 */
396 error = mutex_lock_interruptible(&rfkill_mutex);
397 if (error)
398 return error;
399
400 if (rfkill->user_claim != claim) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300401 if (!claim) {
402 mutex_lock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700403 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300404 rfkill_states[rfkill->type],
405 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300406 mutex_unlock(&rfkill->mutex);
407 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700408 rfkill->user_claim = claim;
409 }
410
411 mutex_unlock(&rfkill_mutex);
412
Michael Buesch20405c02007-09-27 21:34:23 +0200413 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700414}
415
416static struct device_attribute rfkill_dev_attrs[] = {
417 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
418 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700419 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700420 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
421 __ATTR_NULL
422};
423
424static void rfkill_release(struct device *dev)
425{
426 struct rfkill *rfkill = to_rfkill(dev);
427
428 kfree(rfkill);
429 module_put(THIS_MODULE);
430}
431
432#ifdef CONFIG_PM
433static int rfkill_suspend(struct device *dev, pm_message_t state)
434{
435 struct rfkill *rfkill = to_rfkill(dev);
436
437 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100438 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300439 /* Stop transmitter, keep state, no notifies */
440 update_rfkill_state(rfkill);
441
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700442 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300443 rfkill->toggle_radio(rfkill->data,
444 RFKILL_STATE_SOFT_BLOCKED);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700445 mutex_unlock(&rfkill->mutex);
446 }
447
448 dev->power.power_state = state;
449 }
450
451 return 0;
452}
453
454static int rfkill_resume(struct device *dev)
455{
456 struct rfkill *rfkill = to_rfkill(dev);
457
458 if (dev->power.power_state.event != PM_EVENT_ON) {
459 mutex_lock(&rfkill->mutex);
460
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300461 /* restore radio state AND notify everybody */
462 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700463
464 mutex_unlock(&rfkill->mutex);
465 }
466
467 dev->power.power_state = PMSG_ON;
468 return 0;
469}
470#else
471#define rfkill_suspend NULL
472#define rfkill_resume NULL
473#endif
474
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300475static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
476 unsigned long eventid,
477 void *data)
478{
479 struct rfkill *rfkill = (struct rfkill *)data;
480
481 switch (eventid) {
482 case RFKILL_STATE_CHANGED:
483 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
484 break;
485 default:
486 break;
487 }
488
489 return NOTIFY_DONE;
490}
491
492static struct notifier_block rfkill_blocking_uevent_nb = {
493 .notifier_call = rfkill_blocking_uevent_notifier,
494 .priority = 0,
495};
496
497static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
498{
499 struct rfkill *rfkill = to_rfkill(dev);
500 int error;
501
502 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
503 if (error)
504 return error;
505 error = add_uevent_var(env, "RFKILL_TYPE=%s",
506 rfkill_get_type_str(rfkill->type));
507 if (error)
508 return error;
509 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
510 return error;
511}
512
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700513static struct class rfkill_class = {
514 .name = "rfkill",
515 .dev_release = rfkill_release,
516 .dev_attrs = rfkill_dev_attrs,
517 .suspend = rfkill_suspend,
518 .resume = rfkill_resume,
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300519 .dev_uevent = rfkill_dev_uevent,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700520};
521
522static int rfkill_add_switch(struct rfkill *rfkill)
523{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100524 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700525
Henrique de Moraes Holschuhfd4484af2008-07-03 13:14:57 -0300526 rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
527
528 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700529
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700530 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100531
Henrique de Moraes Holschuhfd4484af2008-07-03 13:14:57 -0300532 return 0;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700533}
534
535static void rfkill_remove_switch(struct rfkill *rfkill)
536{
537 mutex_lock(&rfkill_mutex);
538 list_del_init(&rfkill->node);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700539 mutex_unlock(&rfkill_mutex);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300540
541 mutex_lock(&rfkill->mutex);
542 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
543 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700544}
545
546/**
547 * rfkill_allocate - allocate memory for rfkill structure.
548 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200549 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700550 *
551 * This function should be called by the network driver when it needs
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300552 * rfkill structure. Once the structure is allocated the driver should
553 * finish its initialization by setting the name, private data, enable_radio
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700554 * and disable_radio methods and then register it with rfkill_register().
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300555 *
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700556 * NOTE: If registration fails the structure shoudl be freed by calling
557 * rfkill_free() otherwise rfkill_unregister() should be used.
558 */
559struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
560{
561 struct rfkill *rfkill;
562 struct device *dev;
563
564 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700565 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700566 return NULL;
567
568 mutex_init(&rfkill->mutex);
569 INIT_LIST_HEAD(&rfkill->node);
570 rfkill->type = type;
571
572 dev = &rfkill->dev;
573 dev->class = &rfkill_class;
574 dev->parent = parent;
575 device_initialize(dev);
576
577 __module_get(THIS_MODULE);
578
579 return rfkill;
580}
581EXPORT_SYMBOL(rfkill_allocate);
582
583/**
584 * rfkill_free - Mark rfkill structure for deletion
585 * @rfkill: rfkill structure to be destroyed
586 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300587 * Decrements reference count of the rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700588 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
589 */
590void rfkill_free(struct rfkill *rfkill)
591{
592 if (rfkill)
593 put_device(&rfkill->dev);
594}
595EXPORT_SYMBOL(rfkill_free);
596
Michael Buesch135900c2007-09-27 21:33:12 +0200597static void rfkill_led_trigger_register(struct rfkill *rfkill)
598{
599#ifdef CONFIG_RFKILL_LEDS
600 int error;
601
Dmitry Baryshkov7c4f4572008-07-22 14:17:37 +0400602 if (!rfkill->led_trigger.name)
603 rfkill->led_trigger.name = rfkill->dev.bus_id;
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400604 if (!rfkill->led_trigger.activate)
605 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
Michael Buesch135900c2007-09-27 21:33:12 +0200606 error = led_trigger_register(&rfkill->led_trigger);
607 if (error)
608 rfkill->led_trigger.name = NULL;
609#endif /* CONFIG_RFKILL_LEDS */
610}
611
612static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
613{
614#ifdef CONFIG_RFKILL_LEDS
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300615 if (rfkill->led_trigger.name) {
Michael Buesch135900c2007-09-27 21:33:12 +0200616 led_trigger_unregister(&rfkill->led_trigger);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300617 rfkill->led_trigger.name = NULL;
618 }
Michael Buesch135900c2007-09-27 21:33:12 +0200619#endif
620}
621
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700622/**
623 * rfkill_register - Register a rfkill structure.
624 * @rfkill: rfkill structure to be registered
625 *
626 * This function should be called by the network driver when the rfkill
627 * structure needs to be registered. Immediately from registration the
628 * switch driver should be able to service calls to toggle_radio.
629 */
630int rfkill_register(struct rfkill *rfkill)
631{
632 static atomic_t rfkill_no = ATOMIC_INIT(0);
633 struct device *dev = &rfkill->dev;
634 int error;
635
636 if (!rfkill->toggle_radio)
637 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100638 if (rfkill->type >= RFKILL_TYPE_MAX)
639 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700640
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100641 snprintf(dev->bus_id, sizeof(dev->bus_id),
642 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
643
644 rfkill_led_trigger_register(rfkill);
645
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700646 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500647 if (error) {
648 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700649 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500650 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700651
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700652 error = device_add(dev);
653 if (error) {
654 rfkill_remove_switch(rfkill);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300655 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700656 return error;
657 }
658
659 return 0;
660}
661EXPORT_SYMBOL(rfkill_register);
662
663/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300664 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700665 * @rfkill: rfkill structure to be unregistered
666 *
667 * This function should be called by the network driver during device
668 * teardown to destroy rfkill structure. Note that rfkill_free() should
669 * _not_ be called after rfkill_unregister().
670 */
671void rfkill_unregister(struct rfkill *rfkill)
672{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700673 device_del(&rfkill->dev);
674 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100675 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700676 put_device(&rfkill->dev);
677}
678EXPORT_SYMBOL(rfkill_unregister);
679
680/*
681 * Rfkill module initialization/deinitialization.
682 */
683static int __init rfkill_init(void)
684{
685 int error;
686 int i;
687
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300688 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
689 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
690 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300691 return -EINVAL;
692
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700693 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300694 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700695
696 error = class_register(&rfkill_class);
697 if (error) {
698 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
699 return error;
700 }
701
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300702 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
703
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700704 return 0;
705}
706
707static void __exit rfkill_exit(void)
708{
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300709 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700710 class_unregister(&rfkill_class);
711}
712
Michael Buesch2bf236d2007-10-28 14:39:02 +0100713subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700714module_exit(rfkill_exit);