rfkill.c 19 KB

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