blob: 35a9994e23397e70b7f099b1bd573ed28c361512 [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 Holschuhe10e0df2008-08-02 14:56:25 -0300153 * Suspended devices are not touched at all, and -EAGAIN is returned.
154 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300155 * Note that the @force parameter cannot override a (possibly cached)
156 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300157 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
158 * rfkill_force_state(), so the cache either is bypassed or valid.
159 *
160 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
161 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
162 * give the driver a hint that it should double-BLOCK the transmitter.
163 *
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300164 * Caller must have acquired rfkill->mutex.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300165 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700166static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300167 enum rfkill_state state,
168 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700169{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100170 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300171 enum rfkill_state oldstate, newstate;
172
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300173 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
174 return -EBUSY;
175
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300176 oldstate = rfkill->state;
177
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300178 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300179 !rfkill->get_state(rfkill->data, &newstate))
180 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700181
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300182 switch (state) {
183 case RFKILL_STATE_HARD_BLOCKED:
184 /* typically happens when refreshing hardware state,
185 * such as on resume */
186 state = RFKILL_STATE_SOFT_BLOCKED;
187 break;
188 case RFKILL_STATE_UNBLOCKED:
189 /* force can't override this, only rfkill_force_state() can */
190 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
191 return -EPERM;
192 break;
193 case RFKILL_STATE_SOFT_BLOCKED:
194 /* nothing to do, we want to give drivers the hint to double
195 * BLOCK even a transmitter that is already in state
196 * RFKILL_STATE_HARD_BLOCKED */
197 break;
198 }
199
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300200 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700201 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300202 /* never allow a HARD->SOFT downgrade! */
203 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700204 rfkill->state = state;
205 }
206
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300207 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300208 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300209 notify_rfkill_state_change(rfkill);
210 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300211
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700212 return retval;
213}
214
215/**
216 * rfkill_switch_all - Toggle state of all switches of given type
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300217 * @type: type of interfaces to be affected
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700218 * @state: the new state
219 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300220 * This function toggles the state of all switches of given type,
221 * unless a specific switch is claimed by userspace (in which case,
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300222 * that switch is left alone) or suspended.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700223 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700224void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
225{
226 struct rfkill *rfkill;
227
228 mutex_lock(&rfkill_mutex);
229
230 rfkill_states[type] = state;
231
232 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300233 if ((!rfkill->user_claim) && (rfkill->type == type)) {
234 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300235 rfkill_toggle_radio(rfkill, state, 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300236 mutex_unlock(&rfkill->mutex);
237 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700238 }
239
240 mutex_unlock(&rfkill_mutex);
241}
242EXPORT_SYMBOL(rfkill_switch_all);
243
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300244/**
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300245 * rfkill_epo - emergency power off all transmitters
246 *
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300247 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
248 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300249 */
250void rfkill_epo(void)
251{
252 struct rfkill *rfkill;
253
254 mutex_lock(&rfkill_mutex);
255 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300256 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300257 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300258 mutex_unlock(&rfkill->mutex);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300259 }
260 mutex_unlock(&rfkill_mutex);
261}
262EXPORT_SYMBOL_GPL(rfkill_epo);
263
264/**
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300265 * rfkill_force_state - Force the internal rfkill radio state
266 * @rfkill: pointer to the rfkill class to modify.
267 * @state: the current radio state the class should be forced to.
268 *
269 * This function updates the internal state of the radio cached
270 * by the rfkill class. It should be used when the driver gets
271 * a notification by the firmware/hardware of the current *real*
272 * state of the radio rfkill switch.
273 *
Henrique de Moraes Holschuh2fd9b222008-07-21 21:18:17 -0300274 * Devices which are subject to external changes on their rfkill
275 * state (such as those caused by a hardware rfkill line) MUST
276 * have their driver arrange to call rfkill_force_state() as soon
277 * as possible after such a change.
278 *
279 * This function may not be called from an atomic context.
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300280 */
281int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
282{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300283 enum rfkill_state oldstate;
284
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300285 if (state != RFKILL_STATE_SOFT_BLOCKED &&
286 state != RFKILL_STATE_UNBLOCKED &&
287 state != RFKILL_STATE_HARD_BLOCKED)
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300288 return -EINVAL;
289
290 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300291
292 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300293 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300294
295 if (state != oldstate)
296 notify_rfkill_state_change(rfkill);
297
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300298 mutex_unlock(&rfkill->mutex);
299
300 return 0;
301}
302EXPORT_SYMBOL(rfkill_force_state);
303
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700304static ssize_t rfkill_name_show(struct device *dev,
305 struct device_attribute *attr,
306 char *buf)
307{
308 struct rfkill *rfkill = to_rfkill(dev);
309
310 return sprintf(buf, "%s\n", rfkill->name);
311}
312
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300313static const char *rfkill_get_type_str(enum rfkill_type type)
314{
315 switch (type) {
316 case RFKILL_TYPE_WLAN:
317 return "wlan";
318 case RFKILL_TYPE_BLUETOOTH:
319 return "bluetooth";
320 case RFKILL_TYPE_UWB:
321 return "ultrawideband";
322 case RFKILL_TYPE_WIMAX:
323 return "wimax";
324 case RFKILL_TYPE_WWAN:
325 return "wwan";
326 default:
327 BUG();
328 }
329}
330
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700331static ssize_t rfkill_type_show(struct device *dev,
332 struct device_attribute *attr,
333 char *buf)
334{
335 struct rfkill *rfkill = to_rfkill(dev);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700336
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300337 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700338}
339
340static ssize_t rfkill_state_show(struct device *dev,
341 struct device_attribute *attr,
342 char *buf)
343{
344 struct rfkill *rfkill = to_rfkill(dev);
345
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300346 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700347 return sprintf(buf, "%d\n", rfkill->state);
348}
349
350static ssize_t rfkill_state_store(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
353{
354 struct rfkill *rfkill = to_rfkill(dev);
355 unsigned int state = simple_strtoul(buf, NULL, 0);
356 int error;
357
358 if (!capable(CAP_NET_ADMIN))
359 return -EPERM;
360
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300361 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
362 if (state != RFKILL_STATE_UNBLOCKED &&
363 state != RFKILL_STATE_SOFT_BLOCKED)
364 return -EINVAL;
365
Michael Buesch7f4c5342007-11-28 17:49:34 +0100366 if (mutex_lock_interruptible(&rfkill->mutex))
367 return -ERESTARTSYS;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300368 error = rfkill_toggle_radio(rfkill, state, 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100369 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700370
Michael Buesch7f4c5342007-11-28 17:49:34 +0100371 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700372}
373
374static ssize_t rfkill_claim_show(struct device *dev,
375 struct device_attribute *attr,
376 char *buf)
377{
378 struct rfkill *rfkill = to_rfkill(dev);
379
380 return sprintf(buf, "%d", rfkill->user_claim);
381}
382
383static ssize_t rfkill_claim_store(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 struct rfkill *rfkill = to_rfkill(dev);
388 bool claim = !!simple_strtoul(buf, NULL, 0);
389 int error;
390
391 if (!capable(CAP_NET_ADMIN))
392 return -EPERM;
393
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300394 if (rfkill->user_claim_unsupported)
395 return -EOPNOTSUPP;
396
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700397 /*
398 * Take the global lock to make sure the kernel is not in
399 * the middle of rfkill_switch_all
400 */
401 error = mutex_lock_interruptible(&rfkill_mutex);
402 if (error)
403 return error;
404
405 if (rfkill->user_claim != claim) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300406 if (!claim) {
407 mutex_lock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700408 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300409 rfkill_states[rfkill->type],
410 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300411 mutex_unlock(&rfkill->mutex);
412 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700413 rfkill->user_claim = claim;
414 }
415
416 mutex_unlock(&rfkill_mutex);
417
Michael Buesch20405c02007-09-27 21:34:23 +0200418 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700419}
420
421static struct device_attribute rfkill_dev_attrs[] = {
422 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
423 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700424 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700425 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
426 __ATTR_NULL
427};
428
429static void rfkill_release(struct device *dev)
430{
431 struct rfkill *rfkill = to_rfkill(dev);
432
433 kfree(rfkill);
434 module_put(THIS_MODULE);
435}
436
437#ifdef CONFIG_PM
438static int rfkill_suspend(struct device *dev, pm_message_t state)
439{
440 struct rfkill *rfkill = to_rfkill(dev);
441
442 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100443 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300444 /* Stop transmitter, keep state, no notifies */
445 update_rfkill_state(rfkill);
446
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700447 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300448 rfkill->toggle_radio(rfkill->data,
449 RFKILL_STATE_SOFT_BLOCKED);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700450 mutex_unlock(&rfkill->mutex);
451 }
452
453 dev->power.power_state = state;
454 }
455
456 return 0;
457}
458
459static int rfkill_resume(struct device *dev)
460{
461 struct rfkill *rfkill = to_rfkill(dev);
462
463 if (dev->power.power_state.event != PM_EVENT_ON) {
464 mutex_lock(&rfkill->mutex);
465
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300466 dev->power.power_state.event = PM_EVENT_ON;
467
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300468 /* restore radio state AND notify everybody */
469 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700470
471 mutex_unlock(&rfkill->mutex);
472 }
473
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700474 return 0;
475}
476#else
477#define rfkill_suspend NULL
478#define rfkill_resume NULL
479#endif
480
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300481static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
482 unsigned long eventid,
483 void *data)
484{
485 struct rfkill *rfkill = (struct rfkill *)data;
486
487 switch (eventid) {
488 case RFKILL_STATE_CHANGED:
489 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
490 break;
491 default:
492 break;
493 }
494
495 return NOTIFY_DONE;
496}
497
498static struct notifier_block rfkill_blocking_uevent_nb = {
499 .notifier_call = rfkill_blocking_uevent_notifier,
500 .priority = 0,
501};
502
503static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
504{
505 struct rfkill *rfkill = to_rfkill(dev);
506 int error;
507
508 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
509 if (error)
510 return error;
511 error = add_uevent_var(env, "RFKILL_TYPE=%s",
512 rfkill_get_type_str(rfkill->type));
513 if (error)
514 return error;
515 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
516 return error;
517}
518
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700519static struct class rfkill_class = {
520 .name = "rfkill",
521 .dev_release = rfkill_release,
522 .dev_attrs = rfkill_dev_attrs,
523 .suspend = rfkill_suspend,
524 .resume = rfkill_resume,
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300525 .dev_uevent = rfkill_dev_uevent,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700526};
527
528static int rfkill_add_switch(struct rfkill *rfkill)
529{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100530 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700531
Henrique de Moraes Holschuhfd4484a2008-07-03 13:14:57 -0300532 rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
533
534 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700535
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700536 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100537
Henrique de Moraes Holschuhfd4484a2008-07-03 13:14:57 -0300538 return 0;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700539}
540
541static void rfkill_remove_switch(struct rfkill *rfkill)
542{
543 mutex_lock(&rfkill_mutex);
544 list_del_init(&rfkill->node);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700545 mutex_unlock(&rfkill_mutex);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300546
547 mutex_lock(&rfkill->mutex);
548 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
549 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700550}
551
552/**
553 * rfkill_allocate - allocate memory for rfkill structure.
554 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200555 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700556 *
557 * This function should be called by the network driver when it needs
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300558 * rfkill structure. Once the structure is allocated the driver should
559 * finish its initialization by setting the name, private data, enable_radio
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700560 * and disable_radio methods and then register it with rfkill_register().
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300561 *
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700562 * NOTE: If registration fails the structure shoudl be freed by calling
563 * rfkill_free() otherwise rfkill_unregister() should be used.
564 */
565struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
566{
567 struct rfkill *rfkill;
568 struct device *dev;
569
570 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700571 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700572 return NULL;
573
574 mutex_init(&rfkill->mutex);
575 INIT_LIST_HEAD(&rfkill->node);
576 rfkill->type = type;
577
578 dev = &rfkill->dev;
579 dev->class = &rfkill_class;
580 dev->parent = parent;
581 device_initialize(dev);
582
583 __module_get(THIS_MODULE);
584
585 return rfkill;
586}
587EXPORT_SYMBOL(rfkill_allocate);
588
589/**
590 * rfkill_free - Mark rfkill structure for deletion
591 * @rfkill: rfkill structure to be destroyed
592 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300593 * Decrements reference count of the rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700594 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
595 */
596void rfkill_free(struct rfkill *rfkill)
597{
598 if (rfkill)
599 put_device(&rfkill->dev);
600}
601EXPORT_SYMBOL(rfkill_free);
602
Michael Buesch135900c2007-09-27 21:33:12 +0200603static void rfkill_led_trigger_register(struct rfkill *rfkill)
604{
605#ifdef CONFIG_RFKILL_LEDS
606 int error;
607
Dmitry Baryshkov7c4f4572008-07-22 14:17:37 +0400608 if (!rfkill->led_trigger.name)
609 rfkill->led_trigger.name = rfkill->dev.bus_id;
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400610 if (!rfkill->led_trigger.activate)
611 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
Michael Buesch135900c2007-09-27 21:33:12 +0200612 error = led_trigger_register(&rfkill->led_trigger);
613 if (error)
614 rfkill->led_trigger.name = NULL;
615#endif /* CONFIG_RFKILL_LEDS */
616}
617
618static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
619{
620#ifdef CONFIG_RFKILL_LEDS
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300621 if (rfkill->led_trigger.name) {
Michael Buesch135900c2007-09-27 21:33:12 +0200622 led_trigger_unregister(&rfkill->led_trigger);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300623 rfkill->led_trigger.name = NULL;
624 }
Michael Buesch135900c2007-09-27 21:33:12 +0200625#endif
626}
627
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700628/**
629 * rfkill_register - Register a rfkill structure.
630 * @rfkill: rfkill structure to be registered
631 *
632 * This function should be called by the network driver when the rfkill
633 * structure needs to be registered. Immediately from registration the
634 * switch driver should be able to service calls to toggle_radio.
635 */
636int rfkill_register(struct rfkill *rfkill)
637{
638 static atomic_t rfkill_no = ATOMIC_INIT(0);
639 struct device *dev = &rfkill->dev;
640 int error;
641
642 if (!rfkill->toggle_radio)
643 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100644 if (rfkill->type >= RFKILL_TYPE_MAX)
645 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700646
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100647 snprintf(dev->bus_id, sizeof(dev->bus_id),
648 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
649
650 rfkill_led_trigger_register(rfkill);
651
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700652 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500653 if (error) {
654 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700655 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500656 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700657
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700658 error = device_add(dev);
659 if (error) {
660 rfkill_remove_switch(rfkill);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300661 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700662 return error;
663 }
664
665 return 0;
666}
667EXPORT_SYMBOL(rfkill_register);
668
669/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300670 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700671 * @rfkill: rfkill structure to be unregistered
672 *
673 * This function should be called by the network driver during device
674 * teardown to destroy rfkill structure. Note that rfkill_free() should
675 * _not_ be called after rfkill_unregister().
676 */
677void rfkill_unregister(struct rfkill *rfkill)
678{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700679 device_del(&rfkill->dev);
680 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100681 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700682 put_device(&rfkill->dev);
683}
684EXPORT_SYMBOL(rfkill_unregister);
685
686/*
687 * Rfkill module initialization/deinitialization.
688 */
689static int __init rfkill_init(void)
690{
691 int error;
692 int i;
693
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300694 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
695 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
696 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300697 return -EINVAL;
698
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700699 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300700 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700701
702 error = class_register(&rfkill_class);
703 if (error) {
704 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
705 return error;
706 }
707
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300708 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
709
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700710 return 0;
711}
712
713static void __exit rfkill_exit(void)
714{
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300715 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700716 class_unregister(&rfkill_class);
717}
718
Michael Buesch2bf236d2007-10-28 14:39:02 +0100719subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700720module_exit(rfkill_exit);