blob: a561e350a70a8d2f3a1efc17a3735f33837ac4a7 [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 Holschuhe954b0b2008-06-23 17:22:59 -030042static unsigned int rfkill_default_state = RFKILL_STATE_ON;
43module_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;
101 if (state == RFKILL_STATE_OFF)
102 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
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700131static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300132 enum rfkill_state state,
133 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700134{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100135 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300136 enum rfkill_state oldstate, newstate;
137
138 oldstate = rfkill->state;
139
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300140 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300141 !rfkill->get_state(rfkill->data, &newstate))
142 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700143
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300144 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700145 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300146 if (!retval)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700147 rfkill->state = state;
148 }
149
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300150 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300151 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300152 notify_rfkill_state_change(rfkill);
153 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300154
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700155 return retval;
156}
157
158/**
159 * rfkill_switch_all - Toggle state of all switches of given type
160 * @type: type of interfaces to be affeceted
161 * @state: the new state
162 *
163 * This function toggles state of all switches of given type unless
164 * a specific switch is claimed by userspace in which case it is
165 * left alone.
166 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700167void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
168{
169 struct rfkill *rfkill;
170
171 mutex_lock(&rfkill_mutex);
172
173 rfkill_states[type] = state;
174
175 list_for_each_entry(rfkill, &rfkill_list, node) {
Carlos Corbacho89796f62008-04-12 16:39:47 +0100176 if ((!rfkill->user_claim) && (rfkill->type == type))
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300177 rfkill_toggle_radio(rfkill, state, 0);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700178 }
179
180 mutex_unlock(&rfkill_mutex);
181}
182EXPORT_SYMBOL(rfkill_switch_all);
183
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300184/**
185 * rfkill_force_state - Force the internal rfkill radio state
186 * @rfkill: pointer to the rfkill class to modify.
187 * @state: the current radio state the class should be forced to.
188 *
189 * This function updates the internal state of the radio cached
190 * by the rfkill class. It should be used when the driver gets
191 * a notification by the firmware/hardware of the current *real*
192 * state of the radio rfkill switch.
193 *
194 * It may not be called from an atomic context.
195 */
196int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
197{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300198 enum rfkill_state oldstate;
199
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300200 if (state != RFKILL_STATE_OFF &&
201 state != RFKILL_STATE_ON)
202 return -EINVAL;
203
204 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300205
206 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300207 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300208
209 if (state != oldstate)
210 notify_rfkill_state_change(rfkill);
211
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300212 mutex_unlock(&rfkill->mutex);
213
214 return 0;
215}
216EXPORT_SYMBOL(rfkill_force_state);
217
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700218static ssize_t rfkill_name_show(struct device *dev,
219 struct device_attribute *attr,
220 char *buf)
221{
222 struct rfkill *rfkill = to_rfkill(dev);
223
224 return sprintf(buf, "%s\n", rfkill->name);
225}
226
227static ssize_t rfkill_type_show(struct device *dev,
228 struct device_attribute *attr,
229 char *buf)
230{
231 struct rfkill *rfkill = to_rfkill(dev);
232 const char *type;
233
234 switch (rfkill->type) {
235 case RFKILL_TYPE_WLAN:
236 type = "wlan";
237 break;
238 case RFKILL_TYPE_BLUETOOTH:
239 type = "bluetooth";
240 break;
Ivo van Doorne0665482007-09-13 09:21:31 +0200241 case RFKILL_TYPE_UWB:
242 type = "ultrawideband";
243 break;
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800244 case RFKILL_TYPE_WIMAX:
245 type = "wimax";
246 break;
Henrique de Moraes Holschuh477576a2008-06-23 17:23:01 -0300247 case RFKILL_TYPE_WWAN:
248 type = "wwan";
249 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700250 default:
251 BUG();
252 }
253
254 return sprintf(buf, "%s\n", type);
255}
256
257static ssize_t rfkill_state_show(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
260{
261 struct rfkill *rfkill = to_rfkill(dev);
262
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300263 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700264 return sprintf(buf, "%d\n", rfkill->state);
265}
266
267static ssize_t rfkill_state_store(struct device *dev,
268 struct device_attribute *attr,
269 const char *buf, size_t count)
270{
271 struct rfkill *rfkill = to_rfkill(dev);
272 unsigned int state = simple_strtoul(buf, NULL, 0);
273 int error;
274
275 if (!capable(CAP_NET_ADMIN))
276 return -EPERM;
277
Michael Buesch7f4c5342007-11-28 17:49:34 +0100278 if (mutex_lock_interruptible(&rfkill->mutex))
279 return -ERESTARTSYS;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700280 error = rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300281 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
282 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100283 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700284
Michael Buesch7f4c5342007-11-28 17:49:34 +0100285 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700286}
287
288static ssize_t rfkill_claim_show(struct device *dev,
289 struct device_attribute *attr,
290 char *buf)
291{
292 struct rfkill *rfkill = to_rfkill(dev);
293
294 return sprintf(buf, "%d", rfkill->user_claim);
295}
296
297static ssize_t rfkill_claim_store(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf, size_t count)
300{
301 struct rfkill *rfkill = to_rfkill(dev);
302 bool claim = !!simple_strtoul(buf, NULL, 0);
303 int error;
304
305 if (!capable(CAP_NET_ADMIN))
306 return -EPERM;
307
308 /*
309 * Take the global lock to make sure the kernel is not in
310 * the middle of rfkill_switch_all
311 */
312 error = mutex_lock_interruptible(&rfkill_mutex);
313 if (error)
314 return error;
315
Michael Buesch20405c02007-09-27 21:34:23 +0200316 if (rfkill->user_claim_unsupported) {
317 error = -EOPNOTSUPP;
318 goto out_unlock;
319 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700320 if (rfkill->user_claim != claim) {
321 if (!claim)
322 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300323 rfkill_states[rfkill->type],
324 0);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700325 rfkill->user_claim = claim;
326 }
327
Michael Buesch20405c02007-09-27 21:34:23 +0200328out_unlock:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700329 mutex_unlock(&rfkill_mutex);
330
Michael Buesch20405c02007-09-27 21:34:23 +0200331 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700332}
333
334static struct device_attribute rfkill_dev_attrs[] = {
335 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
336 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700337 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700338 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
339 __ATTR_NULL
340};
341
342static void rfkill_release(struct device *dev)
343{
344 struct rfkill *rfkill = to_rfkill(dev);
345
346 kfree(rfkill);
347 module_put(THIS_MODULE);
348}
349
350#ifdef CONFIG_PM
351static int rfkill_suspend(struct device *dev, pm_message_t state)
352{
353 struct rfkill *rfkill = to_rfkill(dev);
354
355 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100356 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300357 /* Stop transmitter, keep state, no notifies */
358 update_rfkill_state(rfkill);
359
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700360 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300361 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700362 mutex_unlock(&rfkill->mutex);
363 }
364
365 dev->power.power_state = state;
366 }
367
368 return 0;
369}
370
371static int rfkill_resume(struct device *dev)
372{
373 struct rfkill *rfkill = to_rfkill(dev);
374
375 if (dev->power.power_state.event != PM_EVENT_ON) {
376 mutex_lock(&rfkill->mutex);
377
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300378 /* restore radio state AND notify everybody */
379 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700380
381 mutex_unlock(&rfkill->mutex);
382 }
383
384 dev->power.power_state = PMSG_ON;
385 return 0;
386}
387#else
388#define rfkill_suspend NULL
389#define rfkill_resume NULL
390#endif
391
392static struct class rfkill_class = {
393 .name = "rfkill",
394 .dev_release = rfkill_release,
395 .dev_attrs = rfkill_dev_attrs,
396 .suspend = rfkill_suspend,
397 .resume = rfkill_resume,
398};
399
400static int rfkill_add_switch(struct rfkill *rfkill)
401{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100402 int error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700403
Michael Buesch7319f1e2007-10-28 15:16:50 +0100404 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700405
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300406 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100407 if (!error)
408 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700409
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700410 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100411
412 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700413}
414
415static void rfkill_remove_switch(struct rfkill *rfkill)
416{
417 mutex_lock(&rfkill_mutex);
418 list_del_init(&rfkill->node);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300419 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700420 mutex_unlock(&rfkill_mutex);
421}
422
423/**
424 * rfkill_allocate - allocate memory for rfkill structure.
425 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200426 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700427 *
428 * This function should be called by the network driver when it needs
429 * rfkill structure. Once the structure is allocated the driver shoud
430 * finish its initialization by setting name, private data, enable_radio
431 * and disable_radio methods and then register it with rfkill_register().
432 * NOTE: If registration fails the structure shoudl be freed by calling
433 * rfkill_free() otherwise rfkill_unregister() should be used.
434 */
435struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
436{
437 struct rfkill *rfkill;
438 struct device *dev;
439
440 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700441 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700442 return NULL;
443
444 mutex_init(&rfkill->mutex);
445 INIT_LIST_HEAD(&rfkill->node);
446 rfkill->type = type;
447
448 dev = &rfkill->dev;
449 dev->class = &rfkill_class;
450 dev->parent = parent;
451 device_initialize(dev);
452
453 __module_get(THIS_MODULE);
454
455 return rfkill;
456}
457EXPORT_SYMBOL(rfkill_allocate);
458
459/**
460 * rfkill_free - Mark rfkill structure for deletion
461 * @rfkill: rfkill structure to be destroyed
462 *
Oliver Pinter1dbede82008-02-03 17:55:45 +0200463 * Decrements reference count of rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700464 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
465 */
466void rfkill_free(struct rfkill *rfkill)
467{
468 if (rfkill)
469 put_device(&rfkill->dev);
470}
471EXPORT_SYMBOL(rfkill_free);
472
Michael Buesch135900c2007-09-27 21:33:12 +0200473static void rfkill_led_trigger_register(struct rfkill *rfkill)
474{
475#ifdef CONFIG_RFKILL_LEDS
476 int error;
477
478 rfkill->led_trigger.name = rfkill->dev.bus_id;
479 error = led_trigger_register(&rfkill->led_trigger);
480 if (error)
481 rfkill->led_trigger.name = NULL;
482#endif /* CONFIG_RFKILL_LEDS */
483}
484
485static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
486{
487#ifdef CONFIG_RFKILL_LEDS
488 if (rfkill->led_trigger.name)
489 led_trigger_unregister(&rfkill->led_trigger);
490#endif
491}
492
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700493/**
494 * rfkill_register - Register a rfkill structure.
495 * @rfkill: rfkill structure to be registered
496 *
497 * This function should be called by the network driver when the rfkill
498 * structure needs to be registered. Immediately from registration the
499 * switch driver should be able to service calls to toggle_radio.
500 */
501int rfkill_register(struct rfkill *rfkill)
502{
503 static atomic_t rfkill_no = ATOMIC_INIT(0);
504 struct device *dev = &rfkill->dev;
505 int error;
506
507 if (!rfkill->toggle_radio)
508 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100509 if (rfkill->type >= RFKILL_TYPE_MAX)
510 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700511
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100512 snprintf(dev->bus_id, sizeof(dev->bus_id),
513 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
514
515 rfkill_led_trigger_register(rfkill);
516
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700517 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500518 if (error) {
519 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700520 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500521 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700522
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700523 error = device_add(dev);
524 if (error) {
Eric Paris632041f2008-01-13 16:20:56 -0500525 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700526 rfkill_remove_switch(rfkill);
527 return error;
528 }
529
530 return 0;
531}
532EXPORT_SYMBOL(rfkill_register);
533
534/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300535 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700536 * @rfkill: rfkill structure to be unregistered
537 *
538 * This function should be called by the network driver during device
539 * teardown to destroy rfkill structure. Note that rfkill_free() should
540 * _not_ be called after rfkill_unregister().
541 */
542void rfkill_unregister(struct rfkill *rfkill)
543{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700544 device_del(&rfkill->dev);
545 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100546 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700547 put_device(&rfkill->dev);
548}
549EXPORT_SYMBOL(rfkill_unregister);
550
551/*
552 * Rfkill module initialization/deinitialization.
553 */
554static int __init rfkill_init(void)
555{
556 int error;
557 int i;
558
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300559 if (rfkill_default_state != RFKILL_STATE_OFF &&
560 rfkill_default_state != RFKILL_STATE_ON)
561 return -EINVAL;
562
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700563 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300564 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700565
566 error = class_register(&rfkill_class);
567 if (error) {
568 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
569 return error;
570 }
571
572 return 0;
573}
574
575static void __exit rfkill_exit(void)
576{
577 class_unregister(&rfkill_class);
578}
579
Michael Buesch2bf236d2007-10-28 14:39:02 +0100580subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700581module_exit(rfkill_exit);