Commit | Line | Data |
---|---|---|
cf4328cd | 1 | /* |
fe242cfd | 2 | * Copyright (C) 2006 - 2007 Ivo van Doorn |
cf4328cd ID |
3 | * 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 | ||
27366223 MB |
30 | /* Get declaration of rfkill_switch_all() to shut up sparse. */ |
31 | #include "rfkill-input.h" | |
32 | ||
33 | ||
cf4328cd ID |
34 | MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>"); |
35 | MODULE_VERSION("1.0"); | |
36 | MODULE_DESCRIPTION("RF switch support"); | |
37 | MODULE_LICENSE("GPL"); | |
38 | ||
39 | static LIST_HEAD(rfkill_list); /* list of registered rf switches */ | |
15635744 | 40 | static DEFINE_MUTEX(rfkill_global_mutex); |
cf4328cd | 41 | |
5005657c | 42 | static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED; |
e954b0b8 HMH |
43 | module_param_named(default_state, rfkill_default_state, uint, 0444); |
44 | MODULE_PARM_DESC(default_state, | |
45 | "Default initial state for all radio types, 0 = radio off"); | |
46 | ||
99619201 HMH |
47 | struct rfkill_gsw_state { |
48 | enum rfkill_state current_state; | |
49 | enum rfkill_state default_state; | |
50 | }; | |
51 | ||
52 | static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX]; | |
53 | static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; | |
cf4328cd | 54 | |
79399a8d HMH |
55 | static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list); |
56 | ||
57 | ||
58 | /** | |
59 | * register_rfkill_notifier - Add notifier to rfkill notifier chain | |
60 | * @nb: pointer to the new entry to add to the chain | |
61 | * | |
62 | * See blocking_notifier_chain_register() for return value and further | |
63 | * observations. | |
64 | * | |
65 | * Adds a notifier to the rfkill notifier chain. The chain will be | |
66 | * called with a pointer to the relevant rfkill structure as a parameter, | |
67 | * refer to include/linux/rfkill.h for the possible events. | |
68 | * | |
69 | * Notifiers added to this chain are to always return NOTIFY_DONE. This | |
70 | * chain is a blocking notifier chain: notifiers can sleep. | |
71 | * | |
72 | * Calls to this chain may have been done through a workqueue. One must | |
73 | * assume unordered asynchronous behaviour, there is no way to know if | |
74 | * actions related to the event that generated the notification have been | |
75 | * carried out already. | |
76 | */ | |
77 | int register_rfkill_notifier(struct notifier_block *nb) | |
78 | { | |
f745ba03 | 79 | BUG_ON(!nb); |
79399a8d HMH |
80 | return blocking_notifier_chain_register(&rfkill_notifier_list, nb); |
81 | } | |
82 | EXPORT_SYMBOL_GPL(register_rfkill_notifier); | |
83 | ||
84 | /** | |
85 | * unregister_rfkill_notifier - remove notifier from rfkill notifier chain | |
86 | * @nb: pointer to the entry to remove from the chain | |
87 | * | |
88 | * See blocking_notifier_chain_unregister() for return value and further | |
89 | * observations. | |
90 | * | |
91 | * Removes a notifier from the rfkill notifier chain. | |
92 | */ | |
93 | int unregister_rfkill_notifier(struct notifier_block *nb) | |
94 | { | |
f745ba03 | 95 | BUG_ON(!nb); |
79399a8d HMH |
96 | return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb); |
97 | } | |
98 | EXPORT_SYMBOL_GPL(unregister_rfkill_notifier); | |
99 | ||
135900c1 MB |
100 | |
101 | static void rfkill_led_trigger(struct rfkill *rfkill, | |
102 | enum rfkill_state state) | |
103 | { | |
104 | #ifdef CONFIG_RFKILL_LEDS | |
105 | struct led_trigger *led = &rfkill->led_trigger; | |
106 | ||
107 | if (!led->name) | |
108 | return; | |
5005657c | 109 | if (state != RFKILL_STATE_UNBLOCKED) |
135900c1 MB |
110 | led_trigger_event(led, LED_OFF); |
111 | else | |
112 | led_trigger_event(led, LED_FULL); | |
113 | #endif /* CONFIG_RFKILL_LEDS */ | |
114 | } | |
115 | ||
96185664 DB |
116 | #ifdef CONFIG_RFKILL_LEDS |
117 | static void rfkill_led_trigger_activate(struct led_classdev *led) | |
118 | { | |
119 | struct rfkill *rfkill = container_of(led->trigger, | |
120 | struct rfkill, led_trigger); | |
121 | ||
122 | rfkill_led_trigger(rfkill, rfkill->state); | |
123 | } | |
124 | #endif /* CONFIG_RFKILL_LEDS */ | |
125 | ||
79399a8d HMH |
126 | static void notify_rfkill_state_change(struct rfkill *rfkill) |
127 | { | |
128 | blocking_notifier_call_chain(&rfkill_notifier_list, | |
129 | RFKILL_STATE_CHANGED, | |
130 | rfkill); | |
131 | } | |
132 | ||
801e49af HMH |
133 | static void update_rfkill_state(struct rfkill *rfkill) |
134 | { | |
79399a8d | 135 | enum rfkill_state newstate, oldstate; |
801e49af HMH |
136 | |
137 | if (rfkill->get_state) { | |
138 | mutex_lock(&rfkill->mutex); | |
79399a8d HMH |
139 | if (!rfkill->get_state(rfkill->data, &newstate)) { |
140 | oldstate = rfkill->state; | |
801e49af | 141 | rfkill->state = newstate; |
79399a8d HMH |
142 | if (oldstate != newstate) |
143 | notify_rfkill_state_change(rfkill); | |
144 | } | |
801e49af HMH |
145 | mutex_unlock(&rfkill->mutex); |
146 | } | |
147 | } | |
148 | ||
5005657c HMH |
149 | /** |
150 | * rfkill_toggle_radio - wrapper for toggle_radio hook | |
5005657c HMH |
151 | * @rfkill: the rfkill struct to use |
152 | * @force: calls toggle_radio even if cache says it is not needed, | |
153 | * and also makes sure notifications of the state will be | |
154 | * sent even if it didn't change | |
155 | * @state: the new state to call toggle_radio() with | |
156 | * | |
0f687e9a HMH |
157 | * Calls rfkill->toggle_radio, enforcing the API for toggle_radio |
158 | * calls and handling all the red tape such as issuing notifications | |
159 | * if the call is successful. | |
160 | * | |
e10e0dfe HMH |
161 | * Suspended devices are not touched at all, and -EAGAIN is returned. |
162 | * | |
435307a3 HMH |
163 | * Note that the @force parameter cannot override a (possibly cached) |
164 | * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of | |
5005657c HMH |
165 | * RFKILL_STATE_HARD_BLOCKED implements either get_state() or |
166 | * rfkill_force_state(), so the cache either is bypassed or valid. | |
167 | * | |
168 | * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED | |
169 | * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to | |
170 | * give the driver a hint that it should double-BLOCK the transmitter. | |
171 | * | |
064af111 | 172 | * Caller must have acquired rfkill->mutex. |
5005657c | 173 | */ |
cf4328cd | 174 | static int rfkill_toggle_radio(struct rfkill *rfkill, |
526324b6 HMH |
175 | enum rfkill_state state, |
176 | int force) | |
cf4328cd | 177 | { |
7f4c5341 | 178 | int retval = 0; |
801e49af HMH |
179 | enum rfkill_state oldstate, newstate; |
180 | ||
e10e0dfe HMH |
181 | if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP)) |
182 | return -EBUSY; | |
183 | ||
801e49af HMH |
184 | oldstate = rfkill->state; |
185 | ||
526324b6 | 186 | if (rfkill->get_state && !force && |
801e49af HMH |
187 | !rfkill->get_state(rfkill->data, &newstate)) |
188 | rfkill->state = newstate; | |
cf4328cd | 189 | |
5005657c HMH |
190 | switch (state) { |
191 | case RFKILL_STATE_HARD_BLOCKED: | |
192 | /* typically happens when refreshing hardware state, | |
193 | * such as on resume */ | |
194 | state = RFKILL_STATE_SOFT_BLOCKED; | |
195 | break; | |
196 | case RFKILL_STATE_UNBLOCKED: | |
197 | /* force can't override this, only rfkill_force_state() can */ | |
198 | if (rfkill->state == RFKILL_STATE_HARD_BLOCKED) | |
199 | return -EPERM; | |
200 | break; | |
201 | case RFKILL_STATE_SOFT_BLOCKED: | |
202 | /* nothing to do, we want to give drivers the hint to double | |
203 | * BLOCK even a transmitter that is already in state | |
204 | * RFKILL_STATE_HARD_BLOCKED */ | |
205 | break; | |
96c87607 | 206 | default: |
f745ba03 HMH |
207 | WARN(1, KERN_WARNING |
208 | "rfkill: illegal state %d passed as parameter " | |
209 | "to rfkill_toggle_radio\n", state); | |
96c87607 | 210 | return -EINVAL; |
5005657c HMH |
211 | } |
212 | ||
526324b6 | 213 | if (force || state != rfkill->state) { |
cf4328cd | 214 | retval = rfkill->toggle_radio(rfkill->data, state); |
5005657c HMH |
215 | /* never allow a HARD->SOFT downgrade! */ |
216 | if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED) | |
cf4328cd ID |
217 | rfkill->state = state; |
218 | } | |
219 | ||
79399a8d | 220 | if (force || rfkill->state != oldstate) { |
801e49af | 221 | rfkill_led_trigger(rfkill, rfkill->state); |
79399a8d HMH |
222 | notify_rfkill_state_change(rfkill); |
223 | } | |
801e49af | 224 | |
cf4328cd ID |
225 | return retval; |
226 | } | |
227 | ||
228 | /** | |
99619201 | 229 | * __rfkill_switch_all - Toggle state of all switches of given type |
435307a3 | 230 | * @type: type of interfaces to be affected |
cf4328cd ID |
231 | * @state: the new state |
232 | * | |
435307a3 HMH |
233 | * This function toggles the state of all switches of given type, |
234 | * unless a specific switch is claimed by userspace (in which case, | |
e10e0dfe | 235 | * that switch is left alone) or suspended. |
99619201 | 236 | * |
15635744 | 237 | * Caller must have acquired rfkill_global_mutex. |
cf4328cd | 238 | */ |
99619201 HMH |
239 | static void __rfkill_switch_all(const enum rfkill_type type, |
240 | const enum rfkill_state state) | |
cf4328cd ID |
241 | { |
242 | struct rfkill *rfkill; | |
243 | ||
f745ba03 HMH |
244 | if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX), |
245 | KERN_WARNING | |
246 | "rfkill: illegal state %d or type %d " | |
247 | "passed as parameter to __rfkill_switch_all\n", | |
248 | state, type)) | |
96c87607 HMH |
249 | return; |
250 | ||
99619201 | 251 | rfkill_global_states[type].current_state = state; |
cf4328cd | 252 | list_for_each_entry(rfkill, &rfkill_list, node) { |
064af111 HMH |
253 | if ((!rfkill->user_claim) && (rfkill->type == type)) { |
254 | mutex_lock(&rfkill->mutex); | |
526324b6 | 255 | rfkill_toggle_radio(rfkill, state, 0); |
064af111 HMH |
256 | mutex_unlock(&rfkill->mutex); |
257 | } | |
cf4328cd | 258 | } |
99619201 | 259 | } |
cf4328cd | 260 | |
99619201 HMH |
261 | /** |
262 | * rfkill_switch_all - Toggle state of all switches of given type | |
263 | * @type: type of interfaces to be affected | |
264 | * @state: the new state | |
265 | * | |
15635744 | 266 | * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state). |
99619201 HMH |
267 | * Please refer to __rfkill_switch_all() for details. |
268 | */ | |
269 | void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state) | |
270 | { | |
15635744 | 271 | mutex_lock(&rfkill_global_mutex); |
99619201 | 272 | __rfkill_switch_all(type, state); |
15635744 | 273 | mutex_unlock(&rfkill_global_mutex); |
cf4328cd ID |
274 | } |
275 | EXPORT_SYMBOL(rfkill_switch_all); | |
276 | ||
4081f00d HMH |
277 | /** |
278 | * rfkill_epo - emergency power off all transmitters | |
279 | * | |
e10e0dfe | 280 | * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED, |
15635744 | 281 | * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex. |
99619201 HMH |
282 | * |
283 | * The global state before the EPO is saved and can be restored later | |
284 | * using rfkill_restore_states(). | |
4081f00d HMH |
285 | */ |
286 | void rfkill_epo(void) | |
287 | { | |
288 | struct rfkill *rfkill; | |
99619201 | 289 | int i; |
4081f00d | 290 | |
15635744 HMH |
291 | mutex_lock(&rfkill_global_mutex); |
292 | ||
4081f00d | 293 | list_for_each_entry(rfkill, &rfkill_list, node) { |
064af111 | 294 | mutex_lock(&rfkill->mutex); |
5005657c | 295 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
064af111 | 296 | mutex_unlock(&rfkill->mutex); |
4081f00d | 297 | } |
99619201 HMH |
298 | for (i = 0; i < RFKILL_TYPE_MAX; i++) { |
299 | rfkill_global_states[i].default_state = | |
300 | rfkill_global_states[i].current_state; | |
301 | rfkill_global_states[i].current_state = | |
302 | RFKILL_STATE_SOFT_BLOCKED; | |
303 | } | |
15635744 | 304 | mutex_unlock(&rfkill_global_mutex); |
4081f00d HMH |
305 | } |
306 | EXPORT_SYMBOL_GPL(rfkill_epo); | |
307 | ||
99619201 HMH |
308 | /** |
309 | * rfkill_restore_states - restore global states | |
310 | * | |
311 | * Restore (and sync switches to) the global state from the | |
312 | * states in rfkill_default_states. This can undo the effects of | |
313 | * a call to rfkill_epo(). | |
314 | */ | |
315 | void rfkill_restore_states(void) | |
316 | { | |
317 | int i; | |
318 | ||
15635744 HMH |
319 | mutex_lock(&rfkill_global_mutex); |
320 | ||
99619201 HMH |
321 | for (i = 0; i < RFKILL_TYPE_MAX; i++) |
322 | __rfkill_switch_all(i, rfkill_global_states[i].default_state); | |
15635744 | 323 | mutex_unlock(&rfkill_global_mutex); |
99619201 HMH |
324 | } |
325 | EXPORT_SYMBOL_GPL(rfkill_restore_states); | |
326 | ||
801e49af HMH |
327 | /** |
328 | * rfkill_force_state - Force the internal rfkill radio state | |
329 | * @rfkill: pointer to the rfkill class to modify. | |
330 | * @state: the current radio state the class should be forced to. | |
331 | * | |
332 | * This function updates the internal state of the radio cached | |
333 | * by the rfkill class. It should be used when the driver gets | |
334 | * a notification by the firmware/hardware of the current *real* | |
335 | * state of the radio rfkill switch. | |
336 | * | |
2fd9b221 HMH |
337 | * Devices which are subject to external changes on their rfkill |
338 | * state (such as those caused by a hardware rfkill line) MUST | |
339 | * have their driver arrange to call rfkill_force_state() as soon | |
340 | * as possible after such a change. | |
341 | * | |
342 | * This function may not be called from an atomic context. | |
801e49af HMH |
343 | */ |
344 | int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state) | |
345 | { | |
79399a8d HMH |
346 | enum rfkill_state oldstate; |
347 | ||
f745ba03 HMH |
348 | BUG_ON(!rfkill); |
349 | if (WARN((state >= RFKILL_STATE_MAX), | |
350 | KERN_WARNING | |
351 | "rfkill: illegal state %d passed as parameter " | |
352 | "to rfkill_force_state\n", state)) | |
801e49af HMH |
353 | return -EINVAL; |
354 | ||
355 | mutex_lock(&rfkill->mutex); | |
79399a8d HMH |
356 | |
357 | oldstate = rfkill->state; | |
801e49af | 358 | rfkill->state = state; |
79399a8d HMH |
359 | |
360 | if (state != oldstate) | |
361 | notify_rfkill_state_change(rfkill); | |
362 | ||
801e49af HMH |
363 | mutex_unlock(&rfkill->mutex); |
364 | ||
365 | return 0; | |
366 | } | |
367 | EXPORT_SYMBOL(rfkill_force_state); | |
368 | ||
cf4328cd ID |
369 | static ssize_t rfkill_name_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, "%s\n", rfkill->name); | |
376 | } | |
377 | ||
99c632e5 | 378 | static const char *rfkill_get_type_str(enum rfkill_type type) |
cf4328cd | 379 | { |
99c632e5 | 380 | switch (type) { |
cf4328cd | 381 | case RFKILL_TYPE_WLAN: |
99c632e5 | 382 | return "wlan"; |
cf4328cd | 383 | case RFKILL_TYPE_BLUETOOTH: |
99c632e5 | 384 | return "bluetooth"; |
e0665486 | 385 | case RFKILL_TYPE_UWB: |
99c632e5 | 386 | return "ultrawideband"; |
303d9bf6 | 387 | case RFKILL_TYPE_WIMAX: |
99c632e5 | 388 | return "wimax"; |
477576a0 | 389 | case RFKILL_TYPE_WWAN: |
99c632e5 | 390 | return "wwan"; |
cf4328cd ID |
391 | default: |
392 | BUG(); | |
393 | } | |
99c632e5 HMH |
394 | } |
395 | ||
396 | static ssize_t rfkill_type_show(struct device *dev, | |
397 | struct device_attribute *attr, | |
398 | char *buf) | |
399 | { | |
400 | struct rfkill *rfkill = to_rfkill(dev); | |
cf4328cd | 401 | |
99c632e5 | 402 | return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type)); |
cf4328cd ID |
403 | } |
404 | ||
405 | static ssize_t rfkill_state_show(struct device *dev, | |
406 | struct device_attribute *attr, | |
407 | char *buf) | |
408 | { | |
409 | struct rfkill *rfkill = to_rfkill(dev); | |
410 | ||
801e49af | 411 | update_rfkill_state(rfkill); |
cf4328cd ID |
412 | return sprintf(buf, "%d\n", rfkill->state); |
413 | } | |
414 | ||
415 | static ssize_t rfkill_state_store(struct device *dev, | |
416 | struct device_attribute *attr, | |
417 | const char *buf, size_t count) | |
418 | { | |
419 | struct rfkill *rfkill = to_rfkill(dev); | |
849e0576 | 420 | unsigned long state; |
cf4328cd ID |
421 | int error; |
422 | ||
423 | if (!capable(CAP_NET_ADMIN)) | |
424 | return -EPERM; | |
425 | ||
849e0576 HMH |
426 | error = strict_strtoul(buf, 0, &state); |
427 | if (error) | |
428 | return error; | |
429 | ||
5005657c HMH |
430 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
431 | if (state != RFKILL_STATE_UNBLOCKED && | |
432 | state != RFKILL_STATE_SOFT_BLOCKED) | |
433 | return -EINVAL; | |
434 | ||
7f4c5341 MB |
435 | if (mutex_lock_interruptible(&rfkill->mutex)) |
436 | return -ERESTARTSYS; | |
5005657c | 437 | error = rfkill_toggle_radio(rfkill, state, 0); |
7f4c5341 | 438 | mutex_unlock(&rfkill->mutex); |
cf4328cd | 439 | |
7f4c5341 | 440 | return error ? error : count; |
cf4328cd ID |
441 | } |
442 | ||
443 | static ssize_t rfkill_claim_show(struct device *dev, | |
444 | struct device_attribute *attr, | |
445 | char *buf) | |
446 | { | |
447 | struct rfkill *rfkill = to_rfkill(dev); | |
448 | ||
01b510b9 | 449 | return sprintf(buf, "%d\n", rfkill->user_claim); |
cf4328cd ID |
450 | } |
451 | ||
452 | static ssize_t rfkill_claim_store(struct device *dev, | |
453 | struct device_attribute *attr, | |
454 | const char *buf, size_t count) | |
455 | { | |
456 | struct rfkill *rfkill = to_rfkill(dev); | |
849e0576 HMH |
457 | unsigned long claim_tmp; |
458 | bool claim; | |
cf4328cd ID |
459 | int error; |
460 | ||
461 | if (!capable(CAP_NET_ADMIN)) | |
462 | return -EPERM; | |
463 | ||
064af111 HMH |
464 | if (rfkill->user_claim_unsupported) |
465 | return -EOPNOTSUPP; | |
466 | ||
849e0576 HMH |
467 | error = strict_strtoul(buf, 0, &claim_tmp); |
468 | if (error) | |
469 | return error; | |
470 | claim = !!claim_tmp; | |
471 | ||
cf4328cd ID |
472 | /* |
473 | * Take the global lock to make sure the kernel is not in | |
474 | * the middle of rfkill_switch_all | |
475 | */ | |
15635744 | 476 | error = mutex_lock_interruptible(&rfkill_global_mutex); |
cf4328cd ID |
477 | if (error) |
478 | return error; | |
479 | ||
480 | if (rfkill->user_claim != claim) { | |
064af111 HMH |
481 | if (!claim) { |
482 | mutex_lock(&rfkill->mutex); | |
cf4328cd | 483 | rfkill_toggle_radio(rfkill, |
99619201 HMH |
484 | rfkill_global_states[rfkill->type].current_state, |
485 | 0); | |
064af111 HMH |
486 | mutex_unlock(&rfkill->mutex); |
487 | } | |
cf4328cd ID |
488 | rfkill->user_claim = claim; |
489 | } | |
490 | ||
15635744 | 491 | mutex_unlock(&rfkill_global_mutex); |
cf4328cd | 492 | |
20405c08 | 493 | return error ? error : count; |
cf4328cd ID |
494 | } |
495 | ||
496 | static struct device_attribute rfkill_dev_attrs[] = { | |
497 | __ATTR(name, S_IRUGO, rfkill_name_show, NULL), | |
498 | __ATTR(type, S_IRUGO, rfkill_type_show, NULL), | |
c81de6ad | 499 | __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store), |
cf4328cd ID |
500 | __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store), |
501 | __ATTR_NULL | |
502 | }; | |
503 | ||
504 | static void rfkill_release(struct device *dev) | |
505 | { | |
506 | struct rfkill *rfkill = to_rfkill(dev); | |
507 | ||
508 | kfree(rfkill); | |
509 | module_put(THIS_MODULE); | |
510 | } | |
511 | ||
512 | #ifdef CONFIG_PM | |
513 | static int rfkill_suspend(struct device *dev, pm_message_t state) | |
514 | { | |
515 | struct rfkill *rfkill = to_rfkill(dev); | |
516 | ||
517 | if (dev->power.power_state.event != state.event) { | |
3a2d5b70 | 518 | if (state.event & PM_EVENT_SLEEP) { |
526324b6 HMH |
519 | /* Stop transmitter, keep state, no notifies */ |
520 | update_rfkill_state(rfkill); | |
cf4328cd | 521 | |
526324b6 | 522 | mutex_lock(&rfkill->mutex); |
5005657c HMH |
523 | rfkill->toggle_radio(rfkill->data, |
524 | RFKILL_STATE_SOFT_BLOCKED); | |
cf4328cd ID |
525 | mutex_unlock(&rfkill->mutex); |
526 | } | |
527 | ||
528 | dev->power.power_state = state; | |
529 | } | |
530 | ||
531 | return 0; | |
532 | } | |
533 | ||
534 | static int rfkill_resume(struct device *dev) | |
535 | { | |
536 | struct rfkill *rfkill = to_rfkill(dev); | |
537 | ||
538 | if (dev->power.power_state.event != PM_EVENT_ON) { | |
539 | mutex_lock(&rfkill->mutex); | |
540 | ||
e10e0dfe HMH |
541 | dev->power.power_state.event = PM_EVENT_ON; |
542 | ||
526324b6 HMH |
543 | /* restore radio state AND notify everybody */ |
544 | rfkill_toggle_radio(rfkill, rfkill->state, 1); | |
cf4328cd ID |
545 | |
546 | mutex_unlock(&rfkill->mutex); | |
547 | } | |
548 | ||
cf4328cd ID |
549 | return 0; |
550 | } | |
551 | #else | |
552 | #define rfkill_suspend NULL | |
553 | #define rfkill_resume NULL | |
554 | #endif | |
555 | ||
ffb67c34 HMH |
556 | static int rfkill_blocking_uevent_notifier(struct notifier_block *nb, |
557 | unsigned long eventid, | |
558 | void *data) | |
559 | { | |
560 | struct rfkill *rfkill = (struct rfkill *)data; | |
561 | ||
562 | switch (eventid) { | |
563 | case RFKILL_STATE_CHANGED: | |
564 | kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE); | |
565 | break; | |
566 | default: | |
567 | break; | |
568 | } | |
569 | ||
570 | return NOTIFY_DONE; | |
571 | } | |
572 | ||
573 | static struct notifier_block rfkill_blocking_uevent_nb = { | |
574 | .notifier_call = rfkill_blocking_uevent_notifier, | |
575 | .priority = 0, | |
576 | }; | |
577 | ||
578 | static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env) | |
579 | { | |
580 | struct rfkill *rfkill = to_rfkill(dev); | |
581 | int error; | |
582 | ||
583 | error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name); | |
584 | if (error) | |
585 | return error; | |
586 | error = add_uevent_var(env, "RFKILL_TYPE=%s", | |
587 | rfkill_get_type_str(rfkill->type)); | |
588 | if (error) | |
589 | return error; | |
590 | error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state); | |
591 | return error; | |
592 | } | |
593 | ||
cf4328cd ID |
594 | static struct class rfkill_class = { |
595 | .name = "rfkill", | |
596 | .dev_release = rfkill_release, | |
597 | .dev_attrs = rfkill_dev_attrs, | |
598 | .suspend = rfkill_suspend, | |
599 | .resume = rfkill_resume, | |
ffb67c34 | 600 | .dev_uevent = rfkill_dev_uevent, |
cf4328cd ID |
601 | }; |
602 | ||
02589f60 HMH |
603 | static int rfkill_check_duplicity(const struct rfkill *rfkill) |
604 | { | |
605 | struct rfkill *p; | |
606 | unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)]; | |
607 | ||
608 | memset(seen, 0, sizeof(seen)); | |
609 | ||
610 | list_for_each_entry(p, &rfkill_list, node) { | |
f745ba03 HMH |
611 | if (WARN((p == rfkill), KERN_WARNING |
612 | "rfkill: illegal attempt to register " | |
613 | "an already registered rfkill struct\n")) | |
02589f60 | 614 | return -EEXIST; |
02589f60 HMH |
615 | set_bit(p->type, seen); |
616 | } | |
617 | ||
618 | /* 0: first switch of its kind */ | |
619 | return test_bit(rfkill->type, seen); | |
620 | } | |
621 | ||
cf4328cd ID |
622 | static int rfkill_add_switch(struct rfkill *rfkill) |
623 | { | |
02589f60 HMH |
624 | int error; |
625 | ||
15635744 | 626 | mutex_lock(&rfkill_global_mutex); |
cf4328cd | 627 | |
02589f60 HMH |
628 | error = rfkill_check_duplicity(rfkill); |
629 | if (error < 0) | |
630 | goto unlock_out; | |
631 | ||
99619201 HMH |
632 | if (!error) { |
633 | /* lock default after first use */ | |
634 | set_bit(rfkill->type, rfkill_states_lockdflt); | |
635 | rfkill_global_states[rfkill->type].current_state = | |
636 | rfkill_global_states[rfkill->type].default_state; | |
637 | } | |
638 | ||
639 | rfkill_toggle_radio(rfkill, | |
640 | rfkill_global_states[rfkill->type].current_state, | |
641 | 0); | |
fd4484af HMH |
642 | |
643 | list_add_tail(&rfkill->node, &rfkill_list); | |
cf4328cd | 644 | |
02589f60 HMH |
645 | error = 0; |
646 | unlock_out: | |
15635744 | 647 | mutex_unlock(&rfkill_global_mutex); |
7319f1e6 | 648 | |
02589f60 | 649 | return error; |
cf4328cd ID |
650 | } |
651 | ||
652 | static void rfkill_remove_switch(struct rfkill *rfkill) | |
653 | { | |
15635744 | 654 | mutex_lock(&rfkill_global_mutex); |
cf4328cd | 655 | list_del_init(&rfkill->node); |
15635744 | 656 | mutex_unlock(&rfkill_global_mutex); |
064af111 HMH |
657 | |
658 | mutex_lock(&rfkill->mutex); | |
659 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); | |
660 | mutex_unlock(&rfkill->mutex); | |
cf4328cd ID |
661 | } |
662 | ||
663 | /** | |
664 | * rfkill_allocate - allocate memory for rfkill structure. | |
665 | * @parent: device that has rf switch on it | |
234a0ca6 | 666 | * @type: type of the switch (RFKILL_TYPE_*) |
cf4328cd ID |
667 | * |
668 | * This function should be called by the network driver when it needs | |
435307a3 HMH |
669 | * rfkill structure. Once the structure is allocated the driver should |
670 | * finish its initialization by setting the name, private data, enable_radio | |
cf4328cd | 671 | * and disable_radio methods and then register it with rfkill_register(). |
435307a3 | 672 | * |
cf4328cd ID |
673 | * NOTE: If registration fails the structure shoudl be freed by calling |
674 | * rfkill_free() otherwise rfkill_unregister() should be used. | |
675 | */ | |
77fba13c HMH |
676 | struct rfkill * __must_check rfkill_allocate(struct device *parent, |
677 | enum rfkill_type type) | |
cf4328cd ID |
678 | { |
679 | struct rfkill *rfkill; | |
680 | struct device *dev; | |
681 | ||
f745ba03 HMH |
682 | if (WARN((type >= RFKILL_TYPE_MAX), |
683 | KERN_WARNING | |
684 | "rfkill: illegal type %d passed as parameter " | |
685 | "to rfkill_allocate\n", type)) | |
686 | return NULL; | |
687 | ||
cf4328cd | 688 | rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL); |
d007da1f | 689 | if (!rfkill) |
cf4328cd ID |
690 | return NULL; |
691 | ||
692 | mutex_init(&rfkill->mutex); | |
693 | INIT_LIST_HEAD(&rfkill->node); | |
694 | rfkill->type = type; | |
695 | ||
696 | dev = &rfkill->dev; | |
697 | dev->class = &rfkill_class; | |
698 | dev->parent = parent; | |
699 | device_initialize(dev); | |
700 | ||
701 | __module_get(THIS_MODULE); | |
702 | ||
703 | return rfkill; | |
704 | } | |
705 | EXPORT_SYMBOL(rfkill_allocate); | |
706 | ||
707 | /** | |
708 | * rfkill_free - Mark rfkill structure for deletion | |
709 | * @rfkill: rfkill structure to be destroyed | |
710 | * | |
435307a3 | 711 | * Decrements reference count of the rfkill structure so it is destroyed. |
cf4328cd ID |
712 | * Note that rfkill_free() should _not_ be called after rfkill_unregister(). |
713 | */ | |
714 | void rfkill_free(struct rfkill *rfkill) | |
715 | { | |
716 | if (rfkill) | |
717 | put_device(&rfkill->dev); | |
718 | } | |
719 | EXPORT_SYMBOL(rfkill_free); | |
720 | ||
135900c1 MB |
721 | static void rfkill_led_trigger_register(struct rfkill *rfkill) |
722 | { | |
723 | #ifdef CONFIG_RFKILL_LEDS | |
724 | int error; | |
725 | ||
7c4f4578 DB |
726 | if (!rfkill->led_trigger.name) |
727 | rfkill->led_trigger.name = rfkill->dev.bus_id; | |
96185664 DB |
728 | if (!rfkill->led_trigger.activate) |
729 | rfkill->led_trigger.activate = rfkill_led_trigger_activate; | |
135900c1 MB |
730 | error = led_trigger_register(&rfkill->led_trigger); |
731 | if (error) | |
732 | rfkill->led_trigger.name = NULL; | |
733 | #endif /* CONFIG_RFKILL_LEDS */ | |
734 | } | |
735 | ||
736 | static void rfkill_led_trigger_unregister(struct rfkill *rfkill) | |
737 | { | |
738 | #ifdef CONFIG_RFKILL_LEDS | |
37f55e9d | 739 | if (rfkill->led_trigger.name) { |
135900c1 | 740 | led_trigger_unregister(&rfkill->led_trigger); |
37f55e9d HMH |
741 | rfkill->led_trigger.name = NULL; |
742 | } | |
135900c1 MB |
743 | #endif |
744 | } | |
745 | ||
cf4328cd ID |
746 | /** |
747 | * rfkill_register - Register a rfkill structure. | |
748 | * @rfkill: rfkill structure to be registered | |
749 | * | |
750 | * This function should be called by the network driver when the rfkill | |
751 | * structure needs to be registered. Immediately from registration the | |
752 | * switch driver should be able to service calls to toggle_radio. | |
753 | */ | |
77fba13c | 754 | int __must_check rfkill_register(struct rfkill *rfkill) |
cf4328cd ID |
755 | { |
756 | static atomic_t rfkill_no = ATOMIC_INIT(0); | |
757 | struct device *dev = &rfkill->dev; | |
758 | int error; | |
759 | ||
f745ba03 HMH |
760 | if (WARN((!rfkill || !rfkill->toggle_radio || |
761 | rfkill->type >= RFKILL_TYPE_MAX || | |
762 | rfkill->state >= RFKILL_STATE_MAX), | |
763 | KERN_WARNING | |
764 | "rfkill: attempt to register a " | |
765 | "badly initialized rfkill struct\n")) | |
96c87607 | 766 | return -EINVAL; |
cf4328cd | 767 | |
8a8f1c04 MB |
768 | snprintf(dev->bus_id, sizeof(dev->bus_id), |
769 | "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1); | |
770 | ||
771 | rfkill_led_trigger_register(rfkill); | |
772 | ||
cf4328cd | 773 | error = rfkill_add_switch(rfkill); |
632041f3 EP |
774 | if (error) { |
775 | rfkill_led_trigger_unregister(rfkill); | |
cf4328cd | 776 | return error; |
632041f3 | 777 | } |
cf4328cd | 778 | |
cf4328cd ID |
779 | error = device_add(dev); |
780 | if (error) { | |
781 | rfkill_remove_switch(rfkill); | |
37f55e9d | 782 | rfkill_led_trigger_unregister(rfkill); |
cf4328cd ID |
783 | return error; |
784 | } | |
785 | ||
786 | return 0; | |
787 | } | |
788 | EXPORT_SYMBOL(rfkill_register); | |
789 | ||
790 | /** | |
c8fcd905 | 791 | * rfkill_unregister - Unregister a rfkill structure. |
cf4328cd ID |
792 | * @rfkill: rfkill structure to be unregistered |
793 | * | |
794 | * This function should be called by the network driver during device | |
795 | * teardown to destroy rfkill structure. Note that rfkill_free() should | |
796 | * _not_ be called after rfkill_unregister(). | |
797 | */ | |
798 | void rfkill_unregister(struct rfkill *rfkill) | |
799 | { | |
f745ba03 | 800 | BUG_ON(!rfkill); |
cf4328cd ID |
801 | device_del(&rfkill->dev); |
802 | rfkill_remove_switch(rfkill); | |
8a8f1c04 | 803 | rfkill_led_trigger_unregister(rfkill); |
cf4328cd ID |
804 | put_device(&rfkill->dev); |
805 | } | |
806 | EXPORT_SYMBOL(rfkill_unregister); | |
807 | ||
99619201 HMH |
808 | /** |
809 | * rfkill_set_default - set initial value for a switch type | |
810 | * @type - the type of switch to set the default state of | |
811 | * @state - the new default state for that group of switches | |
812 | * | |
813 | * Sets the initial state rfkill should use for a given type. | |
814 | * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED | |
815 | * and RFKILL_STATE_UNBLOCKED. | |
816 | * | |
817 | * This function is meant to be used by platform drivers for platforms | |
818 | * that can save switch state across power down/reboot. | |
819 | * | |
820 | * The default state for each switch type can be changed exactly once. | |
821 | * After a switch of that type is registered, the default state cannot | |
822 | * be changed anymore. This guards against multiple drivers it the | |
823 | * same platform trying to set the initial switch default state, which | |
824 | * is not allowed. | |
825 | * | |
826 | * Returns -EPERM if the state has already been set once or is in use, | |
827 | * so drivers likely want to either ignore or at most printk(KERN_NOTICE) | |
828 | * if this function returns -EPERM. | |
829 | * | |
830 | * Returns 0 if the new default state was set, or an error if it | |
831 | * could not be set. | |
832 | */ | |
833 | int rfkill_set_default(enum rfkill_type type, enum rfkill_state state) | |
834 | { | |
835 | int error; | |
836 | ||
f745ba03 HMH |
837 | if (WARN((type >= RFKILL_TYPE_MAX || |
838 | (state != RFKILL_STATE_SOFT_BLOCKED && | |
839 | state != RFKILL_STATE_UNBLOCKED)), | |
840 | KERN_WARNING | |
841 | "rfkill: illegal state %d or type %d passed as " | |
842 | "parameter to rfkill_set_default\n", state, type)) | |
99619201 HMH |
843 | return -EINVAL; |
844 | ||
15635744 | 845 | mutex_lock(&rfkill_global_mutex); |
99619201 HMH |
846 | |
847 | if (!test_and_set_bit(type, rfkill_states_lockdflt)) { | |
848 | rfkill_global_states[type].default_state = state; | |
849 | error = 0; | |
850 | } else | |
851 | error = -EPERM; | |
852 | ||
15635744 | 853 | mutex_unlock(&rfkill_global_mutex); |
99619201 HMH |
854 | return error; |
855 | } | |
856 | EXPORT_SYMBOL_GPL(rfkill_set_default); | |
857 | ||
cf4328cd ID |
858 | /* |
859 | * Rfkill module initialization/deinitialization. | |
860 | */ | |
861 | static int __init rfkill_init(void) | |
862 | { | |
863 | int error; | |
864 | int i; | |
865 | ||
5005657c HMH |
866 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
867 | if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED && | |
868 | rfkill_default_state != RFKILL_STATE_UNBLOCKED) | |
e954b0b8 HMH |
869 | return -EINVAL; |
870 | ||
99619201 HMH |
871 | for (i = 0; i < RFKILL_TYPE_MAX; i++) |
872 | rfkill_global_states[i].default_state = rfkill_default_state; | |
cf4328cd ID |
873 | |
874 | error = class_register(&rfkill_class); | |
875 | if (error) { | |
876 | printk(KERN_ERR "rfkill: unable to register rfkill class\n"); | |
877 | return error; | |
878 | } | |
879 | ||
ffb67c34 HMH |
880 | register_rfkill_notifier(&rfkill_blocking_uevent_nb); |
881 | ||
cf4328cd ID |
882 | return 0; |
883 | } | |
884 | ||
885 | static void __exit rfkill_exit(void) | |
886 | { | |
ffb67c34 | 887 | unregister_rfkill_notifier(&rfkill_blocking_uevent_nb); |
cf4328cd ID |
888 | class_unregister(&rfkill_class); |
889 | } | |
890 | ||
2bf236d5 | 891 | subsys_initcall(rfkill_init); |
cf4328cd | 892 | module_exit(rfkill_exit); |