rfkill.c 18 KB

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