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 */ | |
40 | static DEFINE_MUTEX(rfkill_mutex); | |
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 | ||
cf4328cd ID |
47 | static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX]; |
48 | ||
79399a8d HMH |
49 | static 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 | */ | |
71 | int register_rfkill_notifier(struct notifier_block *nb) | |
72 | { | |
73 | return blocking_notifier_chain_register(&rfkill_notifier_list, nb); | |
74 | } | |
75 | EXPORT_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 | */ | |
86 | int unregister_rfkill_notifier(struct notifier_block *nb) | |
87 | { | |
88 | return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb); | |
89 | } | |
90 | EXPORT_SYMBOL_GPL(unregister_rfkill_notifier); | |
91 | ||
135900c1 MB |
92 | |
93 | static 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; | |
5005657c | 101 | if (state != RFKILL_STATE_UNBLOCKED) |
135900c1 MB |
102 | led_trigger_event(led, LED_OFF); |
103 | else | |
104 | led_trigger_event(led, LED_FULL); | |
105 | #endif /* CONFIG_RFKILL_LEDS */ | |
106 | } | |
107 | ||
79399a8d HMH |
108 | static 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 | ||
801e49af HMH |
115 | static void update_rfkill_state(struct rfkill *rfkill) |
116 | { | |
79399a8d | 117 | enum rfkill_state newstate, oldstate; |
801e49af HMH |
118 | |
119 | if (rfkill->get_state) { | |
120 | mutex_lock(&rfkill->mutex); | |
79399a8d HMH |
121 | if (!rfkill->get_state(rfkill->data, &newstate)) { |
122 | oldstate = rfkill->state; | |
801e49af | 123 | rfkill->state = newstate; |
79399a8d HMH |
124 | if (oldstate != newstate) |
125 | notify_rfkill_state_change(rfkill); | |
126 | } | |
801e49af HMH |
127 | mutex_unlock(&rfkill->mutex); |
128 | } | |
129 | } | |
130 | ||
5005657c HMH |
131 | /** |
132 | * rfkill_toggle_radio - wrapper for toggle_radio hook | |
0f687e9a | 133 | * |
5005657c HMH |
134 | * @rfkill: the rfkill struct to use |
135 | * @force: calls toggle_radio even if cache says it is not needed, | |
136 | * and also makes sure notifications of the state will be | |
137 | * sent even if it didn't change | |
138 | * @state: the new state to call toggle_radio() with | |
139 | * | |
0f687e9a HMH |
140 | * Calls rfkill->toggle_radio, enforcing the API for toggle_radio |
141 | * calls and handling all the red tape such as issuing notifications | |
142 | * if the call is successful. | |
143 | * | |
144 | * Note that @force cannot override a (possibly cached) state of | |
145 | * RFKILL_STATE_HARD_BLOCKED. Any device making use of | |
5005657c HMH |
146 | * RFKILL_STATE_HARD_BLOCKED implements either get_state() or |
147 | * rfkill_force_state(), so the cache either is bypassed or valid. | |
148 | * | |
149 | * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED | |
150 | * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to | |
151 | * give the driver a hint that it should double-BLOCK the transmitter. | |
152 | * | |
153 | * Caller must have aquired rfkill_mutex. | |
154 | */ | |
cf4328cd | 155 | static int rfkill_toggle_radio(struct rfkill *rfkill, |
526324b6 HMH |
156 | enum rfkill_state state, |
157 | int force) | |
cf4328cd | 158 | { |
7f4c5341 | 159 | int retval = 0; |
801e49af HMH |
160 | enum rfkill_state oldstate, newstate; |
161 | ||
162 | oldstate = rfkill->state; | |
163 | ||
526324b6 | 164 | if (rfkill->get_state && !force && |
801e49af HMH |
165 | !rfkill->get_state(rfkill->data, &newstate)) |
166 | rfkill->state = newstate; | |
cf4328cd | 167 | |
5005657c HMH |
168 | switch (state) { |
169 | case RFKILL_STATE_HARD_BLOCKED: | |
170 | /* typically happens when refreshing hardware state, | |
171 | * such as on resume */ | |
172 | state = RFKILL_STATE_SOFT_BLOCKED; | |
173 | break; | |
174 | case RFKILL_STATE_UNBLOCKED: | |
175 | /* force can't override this, only rfkill_force_state() can */ | |
176 | if (rfkill->state == RFKILL_STATE_HARD_BLOCKED) | |
177 | return -EPERM; | |
178 | break; | |
179 | case RFKILL_STATE_SOFT_BLOCKED: | |
180 | /* nothing to do, we want to give drivers the hint to double | |
181 | * BLOCK even a transmitter that is already in state | |
182 | * RFKILL_STATE_HARD_BLOCKED */ | |
183 | break; | |
184 | } | |
185 | ||
526324b6 | 186 | if (force || state != rfkill->state) { |
cf4328cd | 187 | retval = rfkill->toggle_radio(rfkill->data, state); |
5005657c HMH |
188 | /* never allow a HARD->SOFT downgrade! */ |
189 | if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED) | |
cf4328cd ID |
190 | rfkill->state = state; |
191 | } | |
192 | ||
79399a8d | 193 | if (force || rfkill->state != oldstate) { |
801e49af | 194 | rfkill_led_trigger(rfkill, rfkill->state); |
79399a8d HMH |
195 | notify_rfkill_state_change(rfkill); |
196 | } | |
801e49af | 197 | |
cf4328cd ID |
198 | return retval; |
199 | } | |
200 | ||
201 | /** | |
202 | * rfkill_switch_all - Toggle state of all switches of given type | |
203 | * @type: type of interfaces to be affeceted | |
204 | * @state: the new state | |
205 | * | |
206 | * This function toggles state of all switches of given type unless | |
207 | * a specific switch is claimed by userspace in which case it is | |
208 | * left alone. | |
209 | */ | |
cf4328cd ID |
210 | void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state) |
211 | { | |
212 | struct rfkill *rfkill; | |
213 | ||
214 | mutex_lock(&rfkill_mutex); | |
215 | ||
216 | rfkill_states[type] = state; | |
217 | ||
218 | list_for_each_entry(rfkill, &rfkill_list, node) { | |
89796f64 | 219 | if ((!rfkill->user_claim) && (rfkill->type == type)) |
526324b6 | 220 | rfkill_toggle_radio(rfkill, state, 0); |
cf4328cd ID |
221 | } |
222 | ||
223 | mutex_unlock(&rfkill_mutex); | |
224 | } | |
225 | EXPORT_SYMBOL(rfkill_switch_all); | |
226 | ||
4081f00d HMH |
227 | /** |
228 | * rfkill_epo - emergency power off all transmitters | |
229 | * | |
5005657c | 230 | * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring |
4081f00d HMH |
231 | * everything in its path but rfkill_mutex. |
232 | */ | |
233 | void rfkill_epo(void) | |
234 | { | |
235 | struct rfkill *rfkill; | |
236 | ||
237 | mutex_lock(&rfkill_mutex); | |
238 | list_for_each_entry(rfkill, &rfkill_list, node) { | |
5005657c | 239 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
4081f00d HMH |
240 | } |
241 | mutex_unlock(&rfkill_mutex); | |
242 | } | |
243 | EXPORT_SYMBOL_GPL(rfkill_epo); | |
244 | ||
801e49af HMH |
245 | /** |
246 | * rfkill_force_state - Force the internal rfkill radio state | |
247 | * @rfkill: pointer to the rfkill class to modify. | |
248 | * @state: the current radio state the class should be forced to. | |
249 | * | |
250 | * This function updates the internal state of the radio cached | |
251 | * by the rfkill class. It should be used when the driver gets | |
252 | * a notification by the firmware/hardware of the current *real* | |
253 | * state of the radio rfkill switch. | |
254 | * | |
255 | * It may not be called from an atomic context. | |
256 | */ | |
257 | int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state) | |
258 | { | |
79399a8d HMH |
259 | enum rfkill_state oldstate; |
260 | ||
5005657c HMH |
261 | if (state != RFKILL_STATE_SOFT_BLOCKED && |
262 | state != RFKILL_STATE_UNBLOCKED && | |
263 | state != RFKILL_STATE_HARD_BLOCKED) | |
801e49af HMH |
264 | return -EINVAL; |
265 | ||
266 | mutex_lock(&rfkill->mutex); | |
79399a8d HMH |
267 | |
268 | oldstate = rfkill->state; | |
801e49af | 269 | rfkill->state = state; |
79399a8d HMH |
270 | |
271 | if (state != oldstate) | |
272 | notify_rfkill_state_change(rfkill); | |
273 | ||
801e49af HMH |
274 | mutex_unlock(&rfkill->mutex); |
275 | ||
276 | return 0; | |
277 | } | |
278 | EXPORT_SYMBOL(rfkill_force_state); | |
279 | ||
cf4328cd ID |
280 | static ssize_t rfkill_name_show(struct device *dev, |
281 | struct device_attribute *attr, | |
282 | char *buf) | |
283 | { | |
284 | struct rfkill *rfkill = to_rfkill(dev); | |
285 | ||
286 | return sprintf(buf, "%s\n", rfkill->name); | |
287 | } | |
288 | ||
99c632e5 | 289 | static const char *rfkill_get_type_str(enum rfkill_type type) |
cf4328cd | 290 | { |
99c632e5 | 291 | switch (type) { |
cf4328cd | 292 | case RFKILL_TYPE_WLAN: |
99c632e5 | 293 | return "wlan"; |
cf4328cd | 294 | case RFKILL_TYPE_BLUETOOTH: |
99c632e5 | 295 | return "bluetooth"; |
e0665486 | 296 | case RFKILL_TYPE_UWB: |
99c632e5 | 297 | return "ultrawideband"; |
303d9bf6 | 298 | case RFKILL_TYPE_WIMAX: |
99c632e5 | 299 | return "wimax"; |
477576a0 | 300 | case RFKILL_TYPE_WWAN: |
99c632e5 | 301 | return "wwan"; |
cf4328cd ID |
302 | default: |
303 | BUG(); | |
304 | } | |
99c632e5 HMH |
305 | } |
306 | ||
307 | static ssize_t rfkill_type_show(struct device *dev, | |
308 | struct device_attribute *attr, | |
309 | char *buf) | |
310 | { | |
311 | struct rfkill *rfkill = to_rfkill(dev); | |
cf4328cd | 312 | |
99c632e5 | 313 | return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type)); |
cf4328cd ID |
314 | } |
315 | ||
316 | static ssize_t rfkill_state_show(struct device *dev, | |
317 | struct device_attribute *attr, | |
318 | char *buf) | |
319 | { | |
320 | struct rfkill *rfkill = to_rfkill(dev); | |
321 | ||
801e49af | 322 | update_rfkill_state(rfkill); |
cf4328cd ID |
323 | return sprintf(buf, "%d\n", rfkill->state); |
324 | } | |
325 | ||
326 | static ssize_t rfkill_state_store(struct device *dev, | |
327 | struct device_attribute *attr, | |
328 | const char *buf, size_t count) | |
329 | { | |
330 | struct rfkill *rfkill = to_rfkill(dev); | |
331 | unsigned int state = simple_strtoul(buf, NULL, 0); | |
332 | int error; | |
333 | ||
334 | if (!capable(CAP_NET_ADMIN)) | |
335 | return -EPERM; | |
336 | ||
5005657c HMH |
337 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
338 | if (state != RFKILL_STATE_UNBLOCKED && | |
339 | state != RFKILL_STATE_SOFT_BLOCKED) | |
340 | return -EINVAL; | |
341 | ||
7f4c5341 MB |
342 | if (mutex_lock_interruptible(&rfkill->mutex)) |
343 | return -ERESTARTSYS; | |
5005657c | 344 | error = rfkill_toggle_radio(rfkill, state, 0); |
7f4c5341 | 345 | mutex_unlock(&rfkill->mutex); |
cf4328cd | 346 | |
7f4c5341 | 347 | return error ? error : count; |
cf4328cd ID |
348 | } |
349 | ||
350 | static ssize_t rfkill_claim_show(struct device *dev, | |
351 | struct device_attribute *attr, | |
352 | char *buf) | |
353 | { | |
354 | struct rfkill *rfkill = to_rfkill(dev); | |
355 | ||
356 | return sprintf(buf, "%d", rfkill->user_claim); | |
357 | } | |
358 | ||
359 | static ssize_t rfkill_claim_store(struct device *dev, | |
360 | struct device_attribute *attr, | |
361 | const char *buf, size_t count) | |
362 | { | |
363 | struct rfkill *rfkill = to_rfkill(dev); | |
364 | bool claim = !!simple_strtoul(buf, NULL, 0); | |
365 | int error; | |
366 | ||
367 | if (!capable(CAP_NET_ADMIN)) | |
368 | return -EPERM; | |
369 | ||
370 | /* | |
371 | * Take the global lock to make sure the kernel is not in | |
372 | * the middle of rfkill_switch_all | |
373 | */ | |
374 | error = mutex_lock_interruptible(&rfkill_mutex); | |
375 | if (error) | |
376 | return error; | |
377 | ||
20405c08 MB |
378 | if (rfkill->user_claim_unsupported) { |
379 | error = -EOPNOTSUPP; | |
380 | goto out_unlock; | |
381 | } | |
cf4328cd ID |
382 | if (rfkill->user_claim != claim) { |
383 | if (!claim) | |
384 | rfkill_toggle_radio(rfkill, | |
526324b6 HMH |
385 | rfkill_states[rfkill->type], |
386 | 0); | |
cf4328cd ID |
387 | rfkill->user_claim = claim; |
388 | } | |
389 | ||
20405c08 | 390 | out_unlock: |
cf4328cd ID |
391 | mutex_unlock(&rfkill_mutex); |
392 | ||
20405c08 | 393 | return error ? error : count; |
cf4328cd ID |
394 | } |
395 | ||
396 | static struct device_attribute rfkill_dev_attrs[] = { | |
397 | __ATTR(name, S_IRUGO, rfkill_name_show, NULL), | |
398 | __ATTR(type, S_IRUGO, rfkill_type_show, NULL), | |
c81de6ad | 399 | __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store), |
cf4328cd ID |
400 | __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store), |
401 | __ATTR_NULL | |
402 | }; | |
403 | ||
404 | static void rfkill_release(struct device *dev) | |
405 | { | |
406 | struct rfkill *rfkill = to_rfkill(dev); | |
407 | ||
408 | kfree(rfkill); | |
409 | module_put(THIS_MODULE); | |
410 | } | |
411 | ||
412 | #ifdef CONFIG_PM | |
413 | static int rfkill_suspend(struct device *dev, pm_message_t state) | |
414 | { | |
415 | struct rfkill *rfkill = to_rfkill(dev); | |
416 | ||
417 | if (dev->power.power_state.event != state.event) { | |
3a2d5b70 | 418 | if (state.event & PM_EVENT_SLEEP) { |
526324b6 HMH |
419 | /* Stop transmitter, keep state, no notifies */ |
420 | update_rfkill_state(rfkill); | |
cf4328cd | 421 | |
526324b6 | 422 | mutex_lock(&rfkill->mutex); |
5005657c HMH |
423 | rfkill->toggle_radio(rfkill->data, |
424 | RFKILL_STATE_SOFT_BLOCKED); | |
cf4328cd ID |
425 | mutex_unlock(&rfkill->mutex); |
426 | } | |
427 | ||
428 | dev->power.power_state = state; | |
429 | } | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int rfkill_resume(struct device *dev) | |
435 | { | |
436 | struct rfkill *rfkill = to_rfkill(dev); | |
437 | ||
438 | if (dev->power.power_state.event != PM_EVENT_ON) { | |
439 | mutex_lock(&rfkill->mutex); | |
440 | ||
526324b6 HMH |
441 | /* restore radio state AND notify everybody */ |
442 | rfkill_toggle_radio(rfkill, rfkill->state, 1); | |
cf4328cd ID |
443 | |
444 | mutex_unlock(&rfkill->mutex); | |
445 | } | |
446 | ||
447 | dev->power.power_state = PMSG_ON; | |
448 | return 0; | |
449 | } | |
450 | #else | |
451 | #define rfkill_suspend NULL | |
452 | #define rfkill_resume NULL | |
453 | #endif | |
454 | ||
ffb67c34 HMH |
455 | static int rfkill_blocking_uevent_notifier(struct notifier_block *nb, |
456 | unsigned long eventid, | |
457 | void *data) | |
458 | { | |
459 | struct rfkill *rfkill = (struct rfkill *)data; | |
460 | ||
461 | switch (eventid) { | |
462 | case RFKILL_STATE_CHANGED: | |
463 | kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE); | |
464 | break; | |
465 | default: | |
466 | break; | |
467 | } | |
468 | ||
469 | return NOTIFY_DONE; | |
470 | } | |
471 | ||
472 | static struct notifier_block rfkill_blocking_uevent_nb = { | |
473 | .notifier_call = rfkill_blocking_uevent_notifier, | |
474 | .priority = 0, | |
475 | }; | |
476 | ||
477 | static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env) | |
478 | { | |
479 | struct rfkill *rfkill = to_rfkill(dev); | |
480 | int error; | |
481 | ||
482 | error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name); | |
483 | if (error) | |
484 | return error; | |
485 | error = add_uevent_var(env, "RFKILL_TYPE=%s", | |
486 | rfkill_get_type_str(rfkill->type)); | |
487 | if (error) | |
488 | return error; | |
489 | error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state); | |
490 | return error; | |
491 | } | |
492 | ||
cf4328cd ID |
493 | static struct class rfkill_class = { |
494 | .name = "rfkill", | |
495 | .dev_release = rfkill_release, | |
496 | .dev_attrs = rfkill_dev_attrs, | |
497 | .suspend = rfkill_suspend, | |
498 | .resume = rfkill_resume, | |
ffb67c34 | 499 | .dev_uevent = rfkill_dev_uevent, |
cf4328cd ID |
500 | }; |
501 | ||
502 | static int rfkill_add_switch(struct rfkill *rfkill) | |
503 | { | |
7319f1e6 | 504 | mutex_lock(&rfkill_mutex); |
cf4328cd | 505 | |
fd4484af HMH |
506 | rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0); |
507 | ||
508 | list_add_tail(&rfkill->node, &rfkill_list); | |
cf4328cd | 509 | |
cf4328cd | 510 | mutex_unlock(&rfkill_mutex); |
7319f1e6 | 511 | |
fd4484af | 512 | return 0; |
cf4328cd ID |
513 | } |
514 | ||
515 | static void rfkill_remove_switch(struct rfkill *rfkill) | |
516 | { | |
517 | mutex_lock(&rfkill_mutex); | |
518 | list_del_init(&rfkill->node); | |
5005657c | 519 | rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1); |
cf4328cd ID |
520 | mutex_unlock(&rfkill_mutex); |
521 | } | |
522 | ||
523 | /** | |
524 | * rfkill_allocate - allocate memory for rfkill structure. | |
525 | * @parent: device that has rf switch on it | |
234a0ca6 | 526 | * @type: type of the switch (RFKILL_TYPE_*) |
cf4328cd ID |
527 | * |
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 | */ | |
535 | struct 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); | |
d007da1f | 541 | if (!rfkill) |
cf4328cd ID |
542 | 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 | } | |
557 | EXPORT_SYMBOL(rfkill_allocate); | |
558 | ||
559 | /** | |
560 | * rfkill_free - Mark rfkill structure for deletion | |
561 | * @rfkill: rfkill structure to be destroyed | |
562 | * | |
1dbede87 | 563 | * Decrements reference count of rfkill structure so it is destroyed. |
cf4328cd ID |
564 | * Note that rfkill_free() should _not_ be called after rfkill_unregister(). |
565 | */ | |
566 | void rfkill_free(struct rfkill *rfkill) | |
567 | { | |
568 | if (rfkill) | |
569 | put_device(&rfkill->dev); | |
570 | } | |
571 | EXPORT_SYMBOL(rfkill_free); | |
572 | ||
135900c1 MB |
573 | static 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 | ||
585 | static 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 | ||
cf4328cd ID |
593 | /** |
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 | */ | |
601 | int 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; | |
7319f1e6 MB |
609 | if (rfkill->type >= RFKILL_TYPE_MAX) |
610 | return -EINVAL; | |
cf4328cd | 611 | |
8a8f1c04 MB |
612 | 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 | ||
cf4328cd | 617 | error = rfkill_add_switch(rfkill); |
632041f3 EP |
618 | if (error) { |
619 | rfkill_led_trigger_unregister(rfkill); | |
cf4328cd | 620 | return error; |
632041f3 | 621 | } |
cf4328cd | 622 | |
cf4328cd ID |
623 | error = device_add(dev); |
624 | if (error) { | |
632041f3 | 625 | rfkill_led_trigger_unregister(rfkill); |
cf4328cd ID |
626 | rfkill_remove_switch(rfkill); |
627 | return error; | |
628 | } | |
629 | ||
630 | return 0; | |
631 | } | |
632 | EXPORT_SYMBOL(rfkill_register); | |
633 | ||
634 | /** | |
c8fcd905 | 635 | * rfkill_unregister - Unregister a rfkill structure. |
cf4328cd ID |
636 | * @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 | */ | |
642 | void rfkill_unregister(struct rfkill *rfkill) | |
643 | { | |
644 | device_del(&rfkill->dev); | |
645 | rfkill_remove_switch(rfkill); | |
8a8f1c04 | 646 | rfkill_led_trigger_unregister(rfkill); |
cf4328cd ID |
647 | put_device(&rfkill->dev); |
648 | } | |
649 | EXPORT_SYMBOL(rfkill_unregister); | |
650 | ||
651 | /* | |
652 | * Rfkill module initialization/deinitialization. | |
653 | */ | |
654 | static int __init rfkill_init(void) | |
655 | { | |
656 | int error; | |
657 | int i; | |
658 | ||
5005657c HMH |
659 | /* RFKILL_STATE_HARD_BLOCKED is illegal here... */ |
660 | if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED && | |
661 | rfkill_default_state != RFKILL_STATE_UNBLOCKED) | |
e954b0b8 HMH |
662 | return -EINVAL; |
663 | ||
cf4328cd | 664 | for (i = 0; i < ARRAY_SIZE(rfkill_states); i++) |
e954b0b8 | 665 | rfkill_states[i] = rfkill_default_state; |
cf4328cd ID |
666 | |
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 | ||
ffb67c34 HMH |
673 | register_rfkill_notifier(&rfkill_blocking_uevent_nb); |
674 | ||
cf4328cd ID |
675 | return 0; |
676 | } | |
677 | ||
678 | static void __exit rfkill_exit(void) | |
679 | { | |
ffb67c34 | 680 | unregister_rfkill_notifier(&rfkill_blocking_uevent_nb); |
cf4328cd ID |
681 | class_unregister(&rfkill_class); |
682 | } | |
683 | ||
2bf236d5 | 684 | subsys_initcall(rfkill_init); |
cf4328cd | 685 | module_exit(rfkill_exit); |