rfkill.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Copyright (C) 2006 - 2007 Ivo van Doorn
  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. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/capability.h>
  25. #include <linux/list.h>
  26. #include <linux/mutex.h>
  27. #include <linux/rfkill.h>
  28. /* Get declaration of rfkill_switch_all() to shut up sparse. */
  29. #include "rfkill-input.h"
  30. MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
  31. MODULE_VERSION("1.0");
  32. MODULE_DESCRIPTION("RF switch support");
  33. MODULE_LICENSE("GPL");
  34. static LIST_HEAD(rfkill_list); /* list of registered rf switches */
  35. static DEFINE_MUTEX(rfkill_mutex);
  36. static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
  37. module_param_named(default_state, rfkill_default_state, uint, 0444);
  38. MODULE_PARM_DESC(default_state,
  39. "Default initial state for all radio types, 0 = radio off");
  40. static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
  41. static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
  42. /**
  43. * register_rfkill_notifier - Add notifier to rfkill notifier chain
  44. * @nb: pointer to the new entry to add to the chain
  45. *
  46. * See blocking_notifier_chain_register() for return value and further
  47. * observations.
  48. *
  49. * Adds a notifier to the rfkill notifier chain. The chain will be
  50. * called with a pointer to the relevant rfkill structure as a parameter,
  51. * refer to include/linux/rfkill.h for the possible events.
  52. *
  53. * Notifiers added to this chain are to always return NOTIFY_DONE. This
  54. * chain is a blocking notifier chain: notifiers can sleep.
  55. *
  56. * Calls to this chain may have been done through a workqueue. One must
  57. * assume unordered asynchronous behaviour, there is no way to know if
  58. * actions related to the event that generated the notification have been
  59. * carried out already.
  60. */
  61. int register_rfkill_notifier(struct notifier_block *nb)
  62. {
  63. return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
  64. }
  65. EXPORT_SYMBOL_GPL(register_rfkill_notifier);
  66. /**
  67. * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
  68. * @nb: pointer to the entry to remove from the chain
  69. *
  70. * See blocking_notifier_chain_unregister() for return value and further
  71. * observations.
  72. *
  73. * Removes a notifier from the rfkill notifier chain.
  74. */
  75. int unregister_rfkill_notifier(struct notifier_block *nb)
  76. {
  77. return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
  78. }
  79. EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
  80. static void rfkill_led_trigger(struct rfkill *rfkill,
  81. enum rfkill_state state)
  82. {
  83. #ifdef CONFIG_RFKILL_LEDS
  84. struct led_trigger *led = &rfkill->led_trigger;
  85. if (!led->name)
  86. return;
  87. if (state != RFKILL_STATE_UNBLOCKED)
  88. led_trigger_event(led, LED_OFF);
  89. else
  90. led_trigger_event(led, LED_FULL);
  91. #endif /* CONFIG_RFKILL_LEDS */
  92. }
  93. static void notify_rfkill_state_change(struct rfkill *rfkill)
  94. {
  95. blocking_notifier_call_chain(&rfkill_notifier_list,
  96. RFKILL_STATE_CHANGED,
  97. rfkill);
  98. }
  99. static void update_rfkill_state(struct rfkill *rfkill)
  100. {
  101. enum rfkill_state newstate, oldstate;
  102. if (rfkill->get_state) {
  103. mutex_lock(&rfkill->mutex);
  104. if (!rfkill->get_state(rfkill->data, &newstate)) {
  105. oldstate = rfkill->state;
  106. rfkill->state = newstate;
  107. if (oldstate != newstate)
  108. notify_rfkill_state_change(rfkill);
  109. }
  110. mutex_unlock(&rfkill->mutex);
  111. }
  112. }
  113. /**
  114. * rfkill_toggle_radio - wrapper for toggle_radio hook
  115. * @rfkill: the rfkill struct to use
  116. * @force: calls toggle_radio even if cache says it is not needed,
  117. * and also makes sure notifications of the state will be
  118. * sent even if it didn't change
  119. * @state: the new state to call toggle_radio() with
  120. *
  121. * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
  122. * calls and handling all the red tape such as issuing notifications
  123. * if the call is successful.
  124. *
  125. * Note that the @force parameter cannot override a (possibly cached)
  126. * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
  127. * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
  128. * rfkill_force_state(), so the cache either is bypassed or valid.
  129. *
  130. * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
  131. * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
  132. * give the driver a hint that it should double-BLOCK the transmitter.
  133. *
  134. * Caller must have acquired rfkill->mutex.
  135. */
  136. static int rfkill_toggle_radio(struct rfkill *rfkill,
  137. enum rfkill_state state,
  138. int force)
  139. {
  140. int retval = 0;
  141. enum rfkill_state oldstate, newstate;
  142. oldstate = rfkill->state;
  143. if (rfkill->get_state && !force &&
  144. !rfkill->get_state(rfkill->data, &newstate))
  145. rfkill->state = newstate;
  146. switch (state) {
  147. case RFKILL_STATE_HARD_BLOCKED:
  148. /* typically happens when refreshing hardware state,
  149. * such as on resume */
  150. state = RFKILL_STATE_SOFT_BLOCKED;
  151. break;
  152. case RFKILL_STATE_UNBLOCKED:
  153. /* force can't override this, only rfkill_force_state() can */
  154. if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
  155. return -EPERM;
  156. break;
  157. case RFKILL_STATE_SOFT_BLOCKED:
  158. /* nothing to do, we want to give drivers the hint to double
  159. * BLOCK even a transmitter that is already in state
  160. * RFKILL_STATE_HARD_BLOCKED */
  161. break;
  162. }
  163. if (force || state != rfkill->state) {
  164. retval = rfkill->toggle_radio(rfkill->data, state);
  165. /* never allow a HARD->SOFT downgrade! */
  166. if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
  167. rfkill->state = state;
  168. }
  169. if (force || rfkill->state != oldstate) {
  170. rfkill_led_trigger(rfkill, rfkill->state);
  171. notify_rfkill_state_change(rfkill);
  172. }
  173. return retval;
  174. }
  175. /**
  176. * rfkill_switch_all - Toggle state of all switches of given type
  177. * @type: type of interfaces to be affected
  178. * @state: the new state
  179. *
  180. * This function toggles the state of all switches of given type,
  181. * unless a specific switch is claimed by userspace (in which case,
  182. * that switch is left alone).
  183. */
  184. void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
  185. {
  186. struct rfkill *rfkill;
  187. mutex_lock(&rfkill_mutex);
  188. rfkill_states[type] = state;
  189. list_for_each_entry(rfkill, &rfkill_list, node) {
  190. if ((!rfkill->user_claim) && (rfkill->type == type)) {
  191. mutex_lock(&rfkill->mutex);
  192. rfkill_toggle_radio(rfkill, state, 0);
  193. mutex_unlock(&rfkill->mutex);
  194. }
  195. }
  196. mutex_unlock(&rfkill_mutex);
  197. }
  198. EXPORT_SYMBOL(rfkill_switch_all);
  199. /**
  200. * rfkill_epo - emergency power off all transmitters
  201. *
  202. * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
  203. * everything in its path but rfkill_mutex and rfkill->mutex.
  204. */
  205. void rfkill_epo(void)
  206. {
  207. struct rfkill *rfkill;
  208. mutex_lock(&rfkill_mutex);
  209. list_for_each_entry(rfkill, &rfkill_list, node) {
  210. mutex_lock(&rfkill->mutex);
  211. rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
  212. mutex_unlock(&rfkill->mutex);
  213. }
  214. mutex_unlock(&rfkill_mutex);
  215. }
  216. EXPORT_SYMBOL_GPL(rfkill_epo);
  217. /**
  218. * rfkill_force_state - Force the internal rfkill radio state
  219. * @rfkill: pointer to the rfkill class to modify.
  220. * @state: the current radio state the class should be forced to.
  221. *
  222. * This function updates the internal state of the radio cached
  223. * by the rfkill class. It should be used when the driver gets
  224. * a notification by the firmware/hardware of the current *real*
  225. * state of the radio rfkill switch.
  226. *
  227. * Devices which are subject to external changes on their rfkill
  228. * state (such as those caused by a hardware rfkill line) MUST
  229. * have their driver arrange to call rfkill_force_state() as soon
  230. * as possible after such a change.
  231. *
  232. * This function may not be called from an atomic context.
  233. */
  234. int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
  235. {
  236. enum rfkill_state oldstate;
  237. if (state != RFKILL_STATE_SOFT_BLOCKED &&
  238. state != RFKILL_STATE_UNBLOCKED &&
  239. state != RFKILL_STATE_HARD_BLOCKED)
  240. return -EINVAL;
  241. mutex_lock(&rfkill->mutex);
  242. oldstate = rfkill->state;
  243. rfkill->state = state;
  244. if (state != oldstate)
  245. notify_rfkill_state_change(rfkill);
  246. mutex_unlock(&rfkill->mutex);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(rfkill_force_state);
  250. static ssize_t rfkill_name_show(struct device *dev,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct rfkill *rfkill = to_rfkill(dev);
  255. return sprintf(buf, "%s\n", rfkill->name);
  256. }
  257. static const char *rfkill_get_type_str(enum rfkill_type type)
  258. {
  259. switch (type) {
  260. case RFKILL_TYPE_WLAN:
  261. return "wlan";
  262. case RFKILL_TYPE_BLUETOOTH:
  263. return "bluetooth";
  264. case RFKILL_TYPE_UWB:
  265. return "ultrawideband";
  266. case RFKILL_TYPE_WIMAX:
  267. return "wimax";
  268. case RFKILL_TYPE_WWAN:
  269. return "wwan";
  270. default:
  271. BUG();
  272. }
  273. }
  274. static ssize_t rfkill_type_show(struct device *dev,
  275. struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct rfkill *rfkill = to_rfkill(dev);
  279. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  280. }
  281. static ssize_t rfkill_state_show(struct device *dev,
  282. struct device_attribute *attr,
  283. char *buf)
  284. {
  285. struct rfkill *rfkill = to_rfkill(dev);
  286. update_rfkill_state(rfkill);
  287. return sprintf(buf, "%d\n", rfkill->state);
  288. }
  289. static ssize_t rfkill_state_store(struct device *dev,
  290. struct device_attribute *attr,
  291. const char *buf, size_t count)
  292. {
  293. struct rfkill *rfkill = to_rfkill(dev);
  294. unsigned int state = simple_strtoul(buf, NULL, 0);
  295. int error;
  296. if (!capable(CAP_NET_ADMIN))
  297. return -EPERM;
  298. /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
  299. if (state != RFKILL_STATE_UNBLOCKED &&
  300. state != RFKILL_STATE_SOFT_BLOCKED)
  301. return -EINVAL;
  302. if (mutex_lock_interruptible(&rfkill->mutex))
  303. return -ERESTARTSYS;
  304. error = rfkill_toggle_radio(rfkill, state, 0);
  305. mutex_unlock(&rfkill->mutex);
  306. return error ? error : count;
  307. }
  308. static ssize_t rfkill_claim_show(struct device *dev,
  309. struct device_attribute *attr,
  310. char *buf)
  311. {
  312. struct rfkill *rfkill = to_rfkill(dev);
  313. return sprintf(buf, "%d", rfkill->user_claim);
  314. }
  315. static ssize_t rfkill_claim_store(struct device *dev,
  316. struct device_attribute *attr,
  317. const char *buf, size_t count)
  318. {
  319. struct rfkill *rfkill = to_rfkill(dev);
  320. bool claim = !!simple_strtoul(buf, NULL, 0);
  321. int error;
  322. if (!capable(CAP_NET_ADMIN))
  323. return -EPERM;
  324. if (rfkill->user_claim_unsupported)
  325. return -EOPNOTSUPP;
  326. /*
  327. * Take the global lock to make sure the kernel is not in
  328. * the middle of rfkill_switch_all
  329. */
  330. error = mutex_lock_interruptible(&rfkill_mutex);
  331. if (error)
  332. return error;
  333. if (rfkill->user_claim != claim) {
  334. if (!claim) {
  335. mutex_lock(&rfkill->mutex);
  336. rfkill_toggle_radio(rfkill,
  337. rfkill_states[rfkill->type],
  338. 0);
  339. mutex_unlock(&rfkill->mutex);
  340. }
  341. rfkill->user_claim = claim;
  342. }
  343. mutex_unlock(&rfkill_mutex);
  344. return error ? error : count;
  345. }
  346. static struct device_attribute rfkill_dev_attrs[] = {
  347. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  348. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  349. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  350. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  351. __ATTR_NULL
  352. };
  353. static void rfkill_release(struct device *dev)
  354. {
  355. struct rfkill *rfkill = to_rfkill(dev);
  356. kfree(rfkill);
  357. module_put(THIS_MODULE);
  358. }
  359. #ifdef CONFIG_PM
  360. static int rfkill_suspend(struct device *dev, pm_message_t state)
  361. {
  362. struct rfkill *rfkill = to_rfkill(dev);
  363. if (dev->power.power_state.event != state.event) {
  364. if (state.event & PM_EVENT_SLEEP) {
  365. /* Stop transmitter, keep state, no notifies */
  366. update_rfkill_state(rfkill);
  367. mutex_lock(&rfkill->mutex);
  368. rfkill->toggle_radio(rfkill->data,
  369. RFKILL_STATE_SOFT_BLOCKED);
  370. mutex_unlock(&rfkill->mutex);
  371. }
  372. dev->power.power_state = state;
  373. }
  374. return 0;
  375. }
  376. static int rfkill_resume(struct device *dev)
  377. {
  378. struct rfkill *rfkill = to_rfkill(dev);
  379. if (dev->power.power_state.event != PM_EVENT_ON) {
  380. mutex_lock(&rfkill->mutex);
  381. /* restore radio state AND notify everybody */
  382. rfkill_toggle_radio(rfkill, rfkill->state, 1);
  383. mutex_unlock(&rfkill->mutex);
  384. }
  385. dev->power.power_state = PMSG_ON;
  386. return 0;
  387. }
  388. #else
  389. #define rfkill_suspend NULL
  390. #define rfkill_resume NULL
  391. #endif
  392. static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
  393. unsigned long eventid,
  394. void *data)
  395. {
  396. struct rfkill *rfkill = (struct rfkill *)data;
  397. switch (eventid) {
  398. case RFKILL_STATE_CHANGED:
  399. kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
  400. break;
  401. default:
  402. break;
  403. }
  404. return NOTIFY_DONE;
  405. }
  406. static struct notifier_block rfkill_blocking_uevent_nb = {
  407. .notifier_call = rfkill_blocking_uevent_notifier,
  408. .priority = 0,
  409. };
  410. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  411. {
  412. struct rfkill *rfkill = to_rfkill(dev);
  413. int error;
  414. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  415. if (error)
  416. return error;
  417. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  418. rfkill_get_type_str(rfkill->type));
  419. if (error)
  420. return error;
  421. error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
  422. return error;
  423. }
  424. static struct class rfkill_class = {
  425. .name = "rfkill",
  426. .dev_release = rfkill_release,
  427. .dev_attrs = rfkill_dev_attrs,
  428. .suspend = rfkill_suspend,
  429. .resume = rfkill_resume,
  430. .dev_uevent = rfkill_dev_uevent,
  431. };
  432. static int rfkill_add_switch(struct rfkill *rfkill)
  433. {
  434. mutex_lock(&rfkill_mutex);
  435. rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
  436. list_add_tail(&rfkill->node, &rfkill_list);
  437. mutex_unlock(&rfkill_mutex);
  438. return 0;
  439. }
  440. static void rfkill_remove_switch(struct rfkill *rfkill)
  441. {
  442. mutex_lock(&rfkill_mutex);
  443. list_del_init(&rfkill->node);
  444. mutex_unlock(&rfkill_mutex);
  445. mutex_lock(&rfkill->mutex);
  446. rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
  447. mutex_unlock(&rfkill->mutex);
  448. }
  449. /**
  450. * rfkill_allocate - allocate memory for rfkill structure.
  451. * @parent: device that has rf switch on it
  452. * @type: type of the switch (RFKILL_TYPE_*)
  453. *
  454. * This function should be called by the network driver when it needs
  455. * rfkill structure. Once the structure is allocated the driver should
  456. * finish its initialization by setting the name, private data, enable_radio
  457. * and disable_radio methods and then register it with rfkill_register().
  458. *
  459. * NOTE: If registration fails the structure shoudl be freed by calling
  460. * rfkill_free() otherwise rfkill_unregister() should be used.
  461. */
  462. struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
  463. {
  464. struct rfkill *rfkill;
  465. struct device *dev;
  466. rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
  467. if (!rfkill)
  468. return NULL;
  469. mutex_init(&rfkill->mutex);
  470. INIT_LIST_HEAD(&rfkill->node);
  471. rfkill->type = type;
  472. dev = &rfkill->dev;
  473. dev->class = &rfkill_class;
  474. dev->parent = parent;
  475. device_initialize(dev);
  476. __module_get(THIS_MODULE);
  477. return rfkill;
  478. }
  479. EXPORT_SYMBOL(rfkill_allocate);
  480. /**
  481. * rfkill_free - Mark rfkill structure for deletion
  482. * @rfkill: rfkill structure to be destroyed
  483. *
  484. * Decrements reference count of the rfkill structure so it is destroyed.
  485. * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  486. */
  487. void rfkill_free(struct rfkill *rfkill)
  488. {
  489. if (rfkill)
  490. put_device(&rfkill->dev);
  491. }
  492. EXPORT_SYMBOL(rfkill_free);
  493. static void rfkill_led_trigger_register(struct rfkill *rfkill)
  494. {
  495. #ifdef CONFIG_RFKILL_LEDS
  496. int error;
  497. if (!rfkill->led_trigger.name)
  498. rfkill->led_trigger.name = rfkill->dev.bus_id;
  499. error = led_trigger_register(&rfkill->led_trigger);
  500. if (error)
  501. rfkill->led_trigger.name = NULL;
  502. #endif /* CONFIG_RFKILL_LEDS */
  503. }
  504. static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  505. {
  506. #ifdef CONFIG_RFKILL_LEDS
  507. if (rfkill->led_trigger.name) {
  508. led_trigger_unregister(&rfkill->led_trigger);
  509. rfkill->led_trigger.name = NULL;
  510. }
  511. #endif
  512. }
  513. /**
  514. * rfkill_register - Register a rfkill structure.
  515. * @rfkill: rfkill structure to be registered
  516. *
  517. * This function should be called by the network driver when the rfkill
  518. * structure needs to be registered. Immediately from registration the
  519. * switch driver should be able to service calls to toggle_radio.
  520. */
  521. int rfkill_register(struct rfkill *rfkill)
  522. {
  523. static atomic_t rfkill_no = ATOMIC_INIT(0);
  524. struct device *dev = &rfkill->dev;
  525. int error;
  526. if (!rfkill->toggle_radio)
  527. return -EINVAL;
  528. if (rfkill->type >= RFKILL_TYPE_MAX)
  529. return -EINVAL;
  530. snprintf(dev->bus_id, sizeof(dev->bus_id),
  531. "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
  532. rfkill_led_trigger_register(rfkill);
  533. error = rfkill_add_switch(rfkill);
  534. if (error) {
  535. rfkill_led_trigger_unregister(rfkill);
  536. return error;
  537. }
  538. error = device_add(dev);
  539. if (error) {
  540. rfkill_remove_switch(rfkill);
  541. rfkill_led_trigger_unregister(rfkill);
  542. return error;
  543. }
  544. return 0;
  545. }
  546. EXPORT_SYMBOL(rfkill_register);
  547. /**
  548. * rfkill_unregister - Unregister a rfkill structure.
  549. * @rfkill: rfkill structure to be unregistered
  550. *
  551. * This function should be called by the network driver during device
  552. * teardown to destroy rfkill structure. Note that rfkill_free() should
  553. * _not_ be called after rfkill_unregister().
  554. */
  555. void rfkill_unregister(struct rfkill *rfkill)
  556. {
  557. device_del(&rfkill->dev);
  558. rfkill_remove_switch(rfkill);
  559. rfkill_led_trigger_unregister(rfkill);
  560. put_device(&rfkill->dev);
  561. }
  562. EXPORT_SYMBOL(rfkill_unregister);
  563. /*
  564. * Rfkill module initialization/deinitialization.
  565. */
  566. static int __init rfkill_init(void)
  567. {
  568. int error;
  569. int i;
  570. /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
  571. if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
  572. rfkill_default_state != RFKILL_STATE_UNBLOCKED)
  573. return -EINVAL;
  574. for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
  575. rfkill_states[i] = rfkill_default_state;
  576. error = class_register(&rfkill_class);
  577. if (error) {
  578. printk(KERN_ERR "rfkill: unable to register rfkill class\n");
  579. return error;
  580. }
  581. register_rfkill_notifier(&rfkill_blocking_uevent_nb);
  582. return 0;
  583. }
  584. static void __exit rfkill_exit(void)
  585. {
  586. unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
  587. class_unregister(&rfkill_class);
  588. }
  589. subsys_initcall(rfkill_init);
  590. module_exit(rfkill_exit);