blob: ac205eceaf42b9167c71d257165a0eb1fddbd184 [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
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300108static void notify_rfkill_state_change(struct rfkill *rfkill)
109{
110 blocking_notifier_call_chain(&rfkill_notifier_list,
111 RFKILL_STATE_CHANGED,
112 rfkill);
113}
114
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300115static void update_rfkill_state(struct rfkill *rfkill)
116{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300117 enum rfkill_state newstate, oldstate;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300118
119 if (rfkill->get_state) {
120 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300121 if (!rfkill->get_state(rfkill->data, &newstate)) {
122 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300123 rfkill->state = newstate;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300124 if (oldstate != newstate)
125 notify_rfkill_state_change(rfkill);
126 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300127 mutex_unlock(&rfkill->mutex);
128 }
129}
130
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300131/**
132 * rfkill_toggle_radio - wrapper for toggle_radio hook
Henrique de Moraes Holschuh0f687e92008-07-03 13:14:56 -0300133 *
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300134 * @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 *
Henrique de Moraes Holschuh0f687e92008-07-03 13:14:56 -0300140 * 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
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300146 * 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 *
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300153 * Caller must have acquired rfkill->mutex.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300154 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700155static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300156 enum rfkill_state state,
157 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700158{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100159 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300160 enum rfkill_state oldstate, newstate;
161
162 oldstate = rfkill->state;
163
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300164 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300165 !rfkill->get_state(rfkill->data, &newstate))
166 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700167
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300168 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
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300186 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700187 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300188 /* never allow a HARD->SOFT downgrade! */
189 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700190 rfkill->state = state;
191 }
192
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300193 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300194 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300195 notify_rfkill_state_change(rfkill);
196 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300197
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700198 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 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700210void 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) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300219 if ((!rfkill->user_claim) && (rfkill->type == type)) {
220 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300221 rfkill_toggle_radio(rfkill, state, 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300222 mutex_unlock(&rfkill->mutex);
223 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700224 }
225
226 mutex_unlock(&rfkill_mutex);
227}
228EXPORT_SYMBOL(rfkill_switch_all);
229
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300230/**
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300231 * rfkill_epo - emergency power off all transmitters
232 *
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300233 * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300234 * everything in its path but rfkill_mutex and rfkill->mutex.
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300235 */
236void rfkill_epo(void)
237{
238 struct rfkill *rfkill;
239
240 mutex_lock(&rfkill_mutex);
241 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300242 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300243 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300244 mutex_unlock(&rfkill->mutex);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300245 }
246 mutex_unlock(&rfkill_mutex);
247}
248EXPORT_SYMBOL_GPL(rfkill_epo);
249
250/**
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300251 * rfkill_force_state - Force the internal rfkill radio state
252 * @rfkill: pointer to the rfkill class to modify.
253 * @state: the current radio state the class should be forced to.
254 *
255 * This function updates the internal state of the radio cached
256 * by the rfkill class. It should be used when the driver gets
257 * a notification by the firmware/hardware of the current *real*
258 * state of the radio rfkill switch.
259 *
Henrique de Moraes Holschuh2fd9b222008-07-21 21:18:17 -0300260 * Devices which are subject to external changes on their rfkill
261 * state (such as those caused by a hardware rfkill line) MUST
262 * have their driver arrange to call rfkill_force_state() as soon
263 * as possible after such a change.
264 *
265 * This function may not be called from an atomic context.
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300266 */
267int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
268{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300269 enum rfkill_state oldstate;
270
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300271 if (state != RFKILL_STATE_SOFT_BLOCKED &&
272 state != RFKILL_STATE_UNBLOCKED &&
273 state != RFKILL_STATE_HARD_BLOCKED)
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300274 return -EINVAL;
275
276 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300277
278 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300279 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300280
281 if (state != oldstate)
282 notify_rfkill_state_change(rfkill);
283
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300284 mutex_unlock(&rfkill->mutex);
285
286 return 0;
287}
288EXPORT_SYMBOL(rfkill_force_state);
289
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700290static ssize_t rfkill_name_show(struct device *dev,
291 struct device_attribute *attr,
292 char *buf)
293{
294 struct rfkill *rfkill = to_rfkill(dev);
295
296 return sprintf(buf, "%s\n", rfkill->name);
297}
298
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300299static const char *rfkill_get_type_str(enum rfkill_type type)
300{
301 switch (type) {
302 case RFKILL_TYPE_WLAN:
303 return "wlan";
304 case RFKILL_TYPE_BLUETOOTH:
305 return "bluetooth";
306 case RFKILL_TYPE_UWB:
307 return "ultrawideband";
308 case RFKILL_TYPE_WIMAX:
309 return "wimax";
310 case RFKILL_TYPE_WWAN:
311 return "wwan";
312 default:
313 BUG();
314 }
315}
316
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700317static ssize_t rfkill_type_show(struct device *dev,
318 struct device_attribute *attr,
319 char *buf)
320{
321 struct rfkill *rfkill = to_rfkill(dev);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700322
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300323 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700324}
325
326static ssize_t rfkill_state_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
330 struct rfkill *rfkill = to_rfkill(dev);
331
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300332 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700333 return sprintf(buf, "%d\n", rfkill->state);
334}
335
336static ssize_t rfkill_state_store(struct device *dev,
337 struct device_attribute *attr,
338 const char *buf, size_t count)
339{
340 struct rfkill *rfkill = to_rfkill(dev);
341 unsigned int state = simple_strtoul(buf, NULL, 0);
342 int error;
343
344 if (!capable(CAP_NET_ADMIN))
345 return -EPERM;
346
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300347 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
348 if (state != RFKILL_STATE_UNBLOCKED &&
349 state != RFKILL_STATE_SOFT_BLOCKED)
350 return -EINVAL;
351
Michael Buesch7f4c5342007-11-28 17:49:34 +0100352 if (mutex_lock_interruptible(&rfkill->mutex))
353 return -ERESTARTSYS;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300354 error = rfkill_toggle_radio(rfkill, state, 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100355 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700356
Michael Buesch7f4c5342007-11-28 17:49:34 +0100357 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700358}
359
360static ssize_t rfkill_claim_show(struct device *dev,
361 struct device_attribute *attr,
362 char *buf)
363{
364 struct rfkill *rfkill = to_rfkill(dev);
365
366 return sprintf(buf, "%d", rfkill->user_claim);
367}
368
369static ssize_t rfkill_claim_store(struct device *dev,
370 struct device_attribute *attr,
371 const char *buf, size_t count)
372{
373 struct rfkill *rfkill = to_rfkill(dev);
374 bool claim = !!simple_strtoul(buf, NULL, 0);
375 int error;
376
377 if (!capable(CAP_NET_ADMIN))
378 return -EPERM;
379
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300380 if (rfkill->user_claim_unsupported)
381 return -EOPNOTSUPP;
382
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700383 /*
384 * Take the global lock to make sure the kernel is not in
385 * the middle of rfkill_switch_all
386 */
387 error = mutex_lock_interruptible(&rfkill_mutex);
388 if (error)
389 return error;
390
391 if (rfkill->user_claim != claim) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300392 if (!claim) {
393 mutex_lock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700394 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300395 rfkill_states[rfkill->type],
396 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300397 mutex_unlock(&rfkill->mutex);
398 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700399 rfkill->user_claim = claim;
400 }
401
402 mutex_unlock(&rfkill_mutex);
403
Michael Buesch20405c02007-09-27 21:34:23 +0200404 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700405}
406
407static struct device_attribute rfkill_dev_attrs[] = {
408 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
409 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700410 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700411 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
412 __ATTR_NULL
413};
414
415static void rfkill_release(struct device *dev)
416{
417 struct rfkill *rfkill = to_rfkill(dev);
418
419 kfree(rfkill);
420 module_put(THIS_MODULE);
421}
422
423#ifdef CONFIG_PM
424static int rfkill_suspend(struct device *dev, pm_message_t state)
425{
426 struct rfkill *rfkill = to_rfkill(dev);
427
428 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100429 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300430 /* Stop transmitter, keep state, no notifies */
431 update_rfkill_state(rfkill);
432
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700433 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300434 rfkill->toggle_radio(rfkill->data,
435 RFKILL_STATE_SOFT_BLOCKED);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700436 mutex_unlock(&rfkill->mutex);
437 }
438
439 dev->power.power_state = state;
440 }
441
442 return 0;
443}
444
445static int rfkill_resume(struct device *dev)
446{
447 struct rfkill *rfkill = to_rfkill(dev);
448
449 if (dev->power.power_state.event != PM_EVENT_ON) {
450 mutex_lock(&rfkill->mutex);
451
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300452 /* restore radio state AND notify everybody */
453 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700454
455 mutex_unlock(&rfkill->mutex);
456 }
457
458 dev->power.power_state = PMSG_ON;
459 return 0;
460}
461#else
462#define rfkill_suspend NULL
463#define rfkill_resume NULL
464#endif
465
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300466static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
467 unsigned long eventid,
468 void *data)
469{
470 struct rfkill *rfkill = (struct rfkill *)data;
471
472 switch (eventid) {
473 case RFKILL_STATE_CHANGED:
474 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
475 break;
476 default:
477 break;
478 }
479
480 return NOTIFY_DONE;
481}
482
483static struct notifier_block rfkill_blocking_uevent_nb = {
484 .notifier_call = rfkill_blocking_uevent_notifier,
485 .priority = 0,
486};
487
488static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
489{
490 struct rfkill *rfkill = to_rfkill(dev);
491 int error;
492
493 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
494 if (error)
495 return error;
496 error = add_uevent_var(env, "RFKILL_TYPE=%s",
497 rfkill_get_type_str(rfkill->type));
498 if (error)
499 return error;
500 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
501 return error;
502}
503
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700504static struct class rfkill_class = {
505 .name = "rfkill",
506 .dev_release = rfkill_release,
507 .dev_attrs = rfkill_dev_attrs,
508 .suspend = rfkill_suspend,
509 .resume = rfkill_resume,
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300510 .dev_uevent = rfkill_dev_uevent,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700511};
512
513static int rfkill_add_switch(struct rfkill *rfkill)
514{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100515 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700516
Henrique de Moraes Holschuhfd4484a2008-07-03 13:14:57 -0300517 rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
518
519 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700520
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700521 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100522
Henrique de Moraes Holschuhfd4484a2008-07-03 13:14:57 -0300523 return 0;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700524}
525
526static void rfkill_remove_switch(struct rfkill *rfkill)
527{
528 mutex_lock(&rfkill_mutex);
529 list_del_init(&rfkill->node);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700530 mutex_unlock(&rfkill_mutex);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300531
532 mutex_lock(&rfkill->mutex);
533 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
534 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700535}
536
537/**
538 * rfkill_allocate - allocate memory for rfkill structure.
539 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200540 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700541 *
542 * This function should be called by the network driver when it needs
543 * rfkill structure. Once the structure is allocated the driver shoud
544 * finish its initialization by setting name, private data, enable_radio
545 * and disable_radio methods and then register it with rfkill_register().
546 * NOTE: If registration fails the structure shoudl be freed by calling
547 * rfkill_free() otherwise rfkill_unregister() should be used.
548 */
549struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
550{
551 struct rfkill *rfkill;
552 struct device *dev;
553
554 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700555 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700556 return NULL;
557
558 mutex_init(&rfkill->mutex);
559 INIT_LIST_HEAD(&rfkill->node);
560 rfkill->type = type;
561
562 dev = &rfkill->dev;
563 dev->class = &rfkill_class;
564 dev->parent = parent;
565 device_initialize(dev);
566
567 __module_get(THIS_MODULE);
568
569 return rfkill;
570}
571EXPORT_SYMBOL(rfkill_allocate);
572
573/**
574 * rfkill_free - Mark rfkill structure for deletion
575 * @rfkill: rfkill structure to be destroyed
576 *
Oliver Pinter1dbede82008-02-03 17:55:45 +0200577 * Decrements reference count of rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700578 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
579 */
580void rfkill_free(struct rfkill *rfkill)
581{
582 if (rfkill)
583 put_device(&rfkill->dev);
584}
585EXPORT_SYMBOL(rfkill_free);
586
Michael Buesch135900c2007-09-27 21:33:12 +0200587static void rfkill_led_trigger_register(struct rfkill *rfkill)
588{
589#ifdef CONFIG_RFKILL_LEDS
590 int error;
591
592 rfkill->led_trigger.name = rfkill->dev.bus_id;
593 error = led_trigger_register(&rfkill->led_trigger);
594 if (error)
595 rfkill->led_trigger.name = NULL;
596#endif /* CONFIG_RFKILL_LEDS */
597}
598
599static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
600{
601#ifdef CONFIG_RFKILL_LEDS
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300602 if (rfkill->led_trigger.name) {
Michael Buesch135900c2007-09-27 21:33:12 +0200603 led_trigger_unregister(&rfkill->led_trigger);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300604 rfkill->led_trigger.name = NULL;
605 }
Michael Buesch135900c2007-09-27 21:33:12 +0200606#endif
607}
608
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700609/**
610 * rfkill_register - Register a rfkill structure.
611 * @rfkill: rfkill structure to be registered
612 *
613 * This function should be called by the network driver when the rfkill
614 * structure needs to be registered. Immediately from registration the
615 * switch driver should be able to service calls to toggle_radio.
616 */
617int rfkill_register(struct rfkill *rfkill)
618{
619 static atomic_t rfkill_no = ATOMIC_INIT(0);
620 struct device *dev = &rfkill->dev;
621 int error;
622
623 if (!rfkill->toggle_radio)
624 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100625 if (rfkill->type >= RFKILL_TYPE_MAX)
626 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700627
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100628 snprintf(dev->bus_id, sizeof(dev->bus_id),
629 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
630
631 rfkill_led_trigger_register(rfkill);
632
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700633 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500634 if (error) {
635 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700636 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500637 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700638
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700639 error = device_add(dev);
640 if (error) {
641 rfkill_remove_switch(rfkill);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300642 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700643 return error;
644 }
645
646 return 0;
647}
648EXPORT_SYMBOL(rfkill_register);
649
650/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300651 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700652 * @rfkill: rfkill structure to be unregistered
653 *
654 * This function should be called by the network driver during device
655 * teardown to destroy rfkill structure. Note that rfkill_free() should
656 * _not_ be called after rfkill_unregister().
657 */
658void rfkill_unregister(struct rfkill *rfkill)
659{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700660 device_del(&rfkill->dev);
661 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100662 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700663 put_device(&rfkill->dev);
664}
665EXPORT_SYMBOL(rfkill_unregister);
666
667/*
668 * Rfkill module initialization/deinitialization.
669 */
670static int __init rfkill_init(void)
671{
672 int error;
673 int i;
674
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300675 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
676 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
677 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300678 return -EINVAL;
679
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700680 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300681 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700682
683 error = class_register(&rfkill_class);
684 if (error) {
685 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
686 return error;
687 }
688
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300689 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
690
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700691 return 0;
692}
693
694static void __exit rfkill_exit(void)
695{
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300696 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700697 class_unregister(&rfkill_class);
698}
699
Michael Buesch2bf236d2007-10-28 14:39:02 +0100700subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700701module_exit(rfkill_exit);