rfkill.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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_global_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. struct rfkill_gsw_state {
  41. enum rfkill_state current_state;
  42. enum rfkill_state default_state;
  43. };
  44. static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
  45. static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
  46. static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
  47. /**
  48. * register_rfkill_notifier - Add notifier to rfkill notifier chain
  49. * @nb: pointer to the new entry to add to the chain
  50. *
  51. * See blocking_notifier_chain_register() for return value and further
  52. * observations.
  53. *
  54. * Adds a notifier to the rfkill notifier chain. The chain will be
  55. * called with a pointer to the relevant rfkill structure as a parameter,
  56. * refer to include/linux/rfkill.h for the possible events.
  57. *
  58. * Notifiers added to this chain are to always return NOTIFY_DONE. This
  59. * chain is a blocking notifier chain: notifiers can sleep.
  60. *
  61. * Calls to this chain may have been done through a workqueue. One must
  62. * assume unordered asynchronous behaviour, there is no way to know if
  63. * actions related to the event that generated the notification have been
  64. * carried out already.
  65. */
  66. int register_rfkill_notifier(struct notifier_block *nb)
  67. {
  68. BUG_ON(!nb);
  69. return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
  70. }
  71. EXPORT_SYMBOL_GPL(register_rfkill_notifier);
  72. /**
  73. * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
  74. * @nb: pointer to the entry to remove from the chain
  75. *
  76. * See blocking_notifier_chain_unregister() for return value and further
  77. * observations.
  78. *
  79. * Removes a notifier from the rfkill notifier chain.
  80. */
  81. int unregister_rfkill_notifier(struct notifier_block *nb)
  82. {
  83. BUG_ON(!nb);
  84. return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
  85. }
  86. EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
  87. static void rfkill_led_trigger(struct rfkill *rfkill,
  88. enum rfkill_state state)
  89. {
  90. #ifdef CONFIG_RFKILL_LEDS
  91. struct led_trigger *led = &rfkill->led_trigger;
  92. if (!led->name)
  93. return;
  94. if (state != RFKILL_STATE_UNBLOCKED)
  95. led_trigger_event(led, LED_OFF);
  96. else
  97. led_trigger_event(led, LED_FULL);
  98. #endif /* CONFIG_RFKILL_LEDS */
  99. }
  100. #ifdef CONFIG_RFKILL_LEDS
  101. static void rfkill_led_trigger_activate(struct led_classdev *led)
  102. {
  103. struct rfkill *rfkill = container_of(led->trigger,
  104. struct rfkill, led_trigger);
  105. rfkill_led_trigger(rfkill, rfkill->state);
  106. }
  107. #endif /* CONFIG_RFKILL_LEDS */
  108. static void notify_rfkill_state_change(struct rfkill *rfkill)
  109. {
  110. rfkill_led_trigger(rfkill, rfkill->state);
  111. blocking_notifier_call_chain(&rfkill_notifier_list,
  112. RFKILL_STATE_CHANGED,
  113. rfkill);
  114. }
  115. static void update_rfkill_state(struct rfkill *rfkill)
  116. {
  117. enum rfkill_state newstate, oldstate;
  118. if (rfkill->get_state) {
  119. mutex_lock(&rfkill->mutex);
  120. if (!rfkill->get_state(rfkill->data, &newstate)) {
  121. oldstate = rfkill->state;
  122. rfkill->state = newstate;
  123. if (oldstate != newstate)
  124. notify_rfkill_state_change(rfkill);
  125. }
  126. mutex_unlock(&rfkill->mutex);
  127. }
  128. }
  129. /**
  130. * rfkill_toggle_radio - wrapper for toggle_radio hook
  131. * @rfkill: the rfkill struct to use
  132. * @force: calls toggle_radio even if cache says it is not needed,
  133. * and also makes sure notifications of the state will be
  134. * sent even if it didn't change
  135. * @state: the new state to call toggle_radio() with
  136. *
  137. * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
  138. * calls and handling all the red tape such as issuing notifications
  139. * if the call is successful.
  140. *
  141. * Suspended devices are not touched at all, and -EAGAIN is returned.
  142. *
  143. * Note that the @force parameter cannot override a (possibly cached)
  144. * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
  145. * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
  146. * rfkill_force_state(), so the cache either is bypassed or valid.
  147. *
  148. * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
  149. * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
  150. * give the driver a hint that it should double-BLOCK the transmitter.
  151. *
  152. * Caller must have acquired rfkill->mutex.
  153. */
  154. static int rfkill_toggle_radio(struct rfkill *rfkill,
  155. enum rfkill_state state,
  156. int force)
  157. {
  158. int retval = 0;
  159. enum rfkill_state oldstate, newstate;
  160. if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
  161. return -EBUSY;
  162. oldstate = rfkill->state;
  163. if (rfkill->get_state && !force &&
  164. !rfkill->get_state(rfkill->data, &newstate))
  165. rfkill->state = newstate;
  166. switch (state) {
  167. case RFKILL_STATE_HARD_BLOCKED:
  168. /* typically happens when refreshing hardware state,
  169. * such as on resume */
  170. state = RFKILL_STATE_SOFT_BLOCKED;
  171. break;
  172. case RFKILL_STATE_UNBLOCKED:
  173. /* force can't override this, only rfkill_force_state() can */
  174. if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
  175. return -EPERM;
  176. break;
  177. case RFKILL_STATE_SOFT_BLOCKED:
  178. /* nothing to do, we want to give drivers the hint to double
  179. * BLOCK even a transmitter that is already in state
  180. * RFKILL_STATE_HARD_BLOCKED */
  181. break;
  182. default:
  183. WARN(1, KERN_WARNING
  184. "rfkill: illegal state %d passed as parameter "
  185. "to rfkill_toggle_radio\n", state);
  186. return -EINVAL;
  187. }
  188. if (force || state != rfkill->state) {
  189. retval = rfkill->toggle_radio(rfkill->data, state);
  190. /* never allow a HARD->SOFT downgrade! */
  191. if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
  192. rfkill->state = state;
  193. }
  194. if (force || rfkill->state != oldstate)
  195. notify_rfkill_state_change(rfkill);
  196. return retval;
  197. }
  198. /**
  199. * __rfkill_switch_all - Toggle state of all switches of given type
  200. * @type: type of interfaces to be affected
  201. * @state: the new state
  202. *
  203. * This function toggles the state of all switches of given type,
  204. * unless a specific switch is claimed by userspace (in which case,
  205. * that switch is left alone) or suspended.
  206. *
  207. * Caller must have acquired rfkill_global_mutex.
  208. */
  209. static void __rfkill_switch_all(const enum rfkill_type type,
  210. const enum rfkill_state state)
  211. {
  212. struct rfkill *rfkill;
  213. if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
  214. KERN_WARNING
  215. "rfkill: illegal state %d or type %d "
  216. "passed as parameter to __rfkill_switch_all\n",
  217. state, type))
  218. return;
  219. rfkill_global_states[type].current_state = state;
  220. list_for_each_entry(rfkill, &rfkill_list, node) {
  221. if ((!rfkill->user_claim) && (rfkill->type == type)) {
  222. mutex_lock(&rfkill->mutex);
  223. rfkill_toggle_radio(rfkill, state, 0);
  224. mutex_unlock(&rfkill->mutex);
  225. }
  226. }
  227. }
  228. /**
  229. * rfkill_switch_all - Toggle state of all switches of given type
  230. * @type: type of interfaces to be affected
  231. * @state: the new state
  232. *
  233. * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  234. * Please refer to __rfkill_switch_all() for details.
  235. */
  236. void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
  237. {
  238. mutex_lock(&rfkill_global_mutex);
  239. __rfkill_switch_all(type, state);
  240. mutex_unlock(&rfkill_global_mutex);
  241. }
  242. EXPORT_SYMBOL(rfkill_switch_all);
  243. /**
  244. * rfkill_epo - emergency power off all transmitters
  245. *
  246. * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
  247. * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
  248. *
  249. * The global state before the EPO is saved and can be restored later
  250. * using rfkill_restore_states().
  251. */
  252. void rfkill_epo(void)
  253. {
  254. struct rfkill *rfkill;
  255. int i;
  256. mutex_lock(&rfkill_global_mutex);
  257. list_for_each_entry(rfkill, &rfkill_list, node) {
  258. mutex_lock(&rfkill->mutex);
  259. rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
  260. mutex_unlock(&rfkill->mutex);
  261. }
  262. for (i = 0; i < RFKILL_TYPE_MAX; i++) {
  263. rfkill_global_states[i].default_state =
  264. rfkill_global_states[i].current_state;
  265. rfkill_global_states[i].current_state =
  266. RFKILL_STATE_SOFT_BLOCKED;
  267. }
  268. mutex_unlock(&rfkill_global_mutex);
  269. }
  270. EXPORT_SYMBOL_GPL(rfkill_epo);
  271. /**
  272. * rfkill_restore_states - restore global states
  273. *
  274. * Restore (and sync switches to) the global state from the
  275. * states in rfkill_default_states. This can undo the effects of
  276. * a call to rfkill_epo().
  277. */
  278. void rfkill_restore_states(void)
  279. {
  280. int i;
  281. mutex_lock(&rfkill_global_mutex);
  282. for (i = 0; i < RFKILL_TYPE_MAX; i++)
  283. __rfkill_switch_all(i, rfkill_global_states[i].default_state);
  284. mutex_unlock(&rfkill_global_mutex);
  285. }
  286. EXPORT_SYMBOL_GPL(rfkill_restore_states);
  287. /**
  288. * rfkill_force_state - Force the internal rfkill radio state
  289. * @rfkill: pointer to the rfkill class to modify.
  290. * @state: the current radio state the class should be forced to.
  291. *
  292. * This function updates the internal state of the radio cached
  293. * by the rfkill class. It should be used when the driver gets
  294. * a notification by the firmware/hardware of the current *real*
  295. * state of the radio rfkill switch.
  296. *
  297. * Devices which are subject to external changes on their rfkill
  298. * state (such as those caused by a hardware rfkill line) MUST
  299. * have their driver arrange to call rfkill_force_state() as soon
  300. * as possible after such a change.
  301. *
  302. * This function may not be called from an atomic context.
  303. */
  304. int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
  305. {
  306. enum rfkill_state oldstate;
  307. BUG_ON(!rfkill);
  308. if (WARN((state >= RFKILL_STATE_MAX),
  309. KERN_WARNING
  310. "rfkill: illegal state %d passed as parameter "
  311. "to rfkill_force_state\n", state))
  312. return -EINVAL;
  313. mutex_lock(&rfkill->mutex);
  314. oldstate = rfkill->state;
  315. rfkill->state = state;
  316. if (state != oldstate)
  317. notify_rfkill_state_change(rfkill);
  318. mutex_unlock(&rfkill->mutex);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL(rfkill_force_state);
  322. static ssize_t rfkill_name_show(struct device *dev,
  323. struct device_attribute *attr,
  324. char *buf)
  325. {
  326. struct rfkill *rfkill = to_rfkill(dev);
  327. return sprintf(buf, "%s\n", rfkill->name);
  328. }
  329. static const char *rfkill_get_type_str(enum rfkill_type type)
  330. {
  331. switch (type) {
  332. case RFKILL_TYPE_WLAN:
  333. return "wlan";
  334. case RFKILL_TYPE_BLUETOOTH:
  335. return "bluetooth";
  336. case RFKILL_TYPE_UWB:
  337. return "ultrawideband";
  338. case RFKILL_TYPE_WIMAX:
  339. return "wimax";
  340. case RFKILL_TYPE_WWAN:
  341. return "wwan";
  342. default:
  343. BUG();
  344. }
  345. }
  346. static ssize_t rfkill_type_show(struct device *dev,
  347. struct device_attribute *attr,
  348. char *buf)
  349. {
  350. struct rfkill *rfkill = to_rfkill(dev);
  351. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  352. }
  353. static ssize_t rfkill_state_show(struct device *dev,
  354. struct device_attribute *attr,
  355. char *buf)
  356. {
  357. struct rfkill *rfkill = to_rfkill(dev);
  358. update_rfkill_state(rfkill);
  359. return sprintf(buf, "%d\n", rfkill->state);
  360. }
  361. static ssize_t rfkill_state_store(struct device *dev,
  362. struct device_attribute *attr,
  363. const char *buf, size_t count)
  364. {
  365. struct rfkill *rfkill = to_rfkill(dev);
  366. unsigned long state;
  367. int error;
  368. if (!capable(CAP_NET_ADMIN))
  369. return -EPERM;
  370. error = strict_strtoul(buf, 0, &state);
  371. if (error)
  372. return error;
  373. /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
  374. if (state != RFKILL_STATE_UNBLOCKED &&
  375. state != RFKILL_STATE_SOFT_BLOCKED)
  376. return -EINVAL;
  377. if (mutex_lock_interruptible(&rfkill->mutex))
  378. return -ERESTARTSYS;
  379. error = rfkill_toggle_radio(rfkill, state, 0);
  380. mutex_unlock(&rfkill->mutex);
  381. return error ? error : count;
  382. }
  383. static ssize_t rfkill_claim_show(struct device *dev,
  384. struct device_attribute *attr,
  385. char *buf)
  386. {
  387. struct rfkill *rfkill = to_rfkill(dev);
  388. return sprintf(buf, "%d\n", rfkill->user_claim);
  389. }
  390. static ssize_t rfkill_claim_store(struct device *dev,
  391. struct device_attribute *attr,
  392. const char *buf, size_t count)
  393. {
  394. struct rfkill *rfkill = to_rfkill(dev);
  395. unsigned long claim_tmp;
  396. bool claim;
  397. int error;
  398. if (!capable(CAP_NET_ADMIN))
  399. return -EPERM;
  400. if (rfkill->user_claim_unsupported)
  401. return -EOPNOTSUPP;
  402. error = strict_strtoul(buf, 0, &claim_tmp);
  403. if (error)
  404. return error;
  405. claim = !!claim_tmp;
  406. /*
  407. * Take the global lock to make sure the kernel is not in
  408. * the middle of rfkill_switch_all
  409. */
  410. error = mutex_lock_interruptible(&rfkill_global_mutex);
  411. if (error)
  412. return error;
  413. if (rfkill->user_claim != claim) {
  414. if (!claim) {
  415. mutex_lock(&rfkill->mutex);
  416. rfkill_toggle_radio(rfkill,
  417. rfkill_global_states[rfkill->type].current_state,
  418. 0);
  419. mutex_unlock(&rfkill->mutex);
  420. }
  421. rfkill->user_claim = claim;
  422. }
  423. mutex_unlock(&rfkill_global_mutex);
  424. return error ? error : count;
  425. }
  426. static struct device_attribute rfkill_dev_attrs[] = {
  427. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  428. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  429. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  430. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  431. __ATTR_NULL
  432. };
  433. static void rfkill_release(struct device *dev)
  434. {
  435. struct rfkill *rfkill = to_rfkill(dev);
  436. kfree(rfkill);
  437. module_put(THIS_MODULE);
  438. }
  439. #ifdef CONFIG_PM
  440. static int rfkill_suspend(struct device *dev, pm_message_t state)
  441. {
  442. /* mark class device as suspended */
  443. if (dev->power.power_state.event != state.event)
  444. dev->power.power_state = state;
  445. return 0;
  446. }
  447. static int rfkill_resume(struct device *dev)
  448. {
  449. struct rfkill *rfkill = to_rfkill(dev);
  450. if (dev->power.power_state.event != PM_EVENT_ON) {
  451. mutex_lock(&rfkill->mutex);
  452. dev->power.power_state.event = PM_EVENT_ON;
  453. /* restore radio state AND notify everybody */
  454. rfkill_toggle_radio(rfkill, rfkill->state, 1);
  455. mutex_unlock(&rfkill->mutex);
  456. }
  457. return 0;
  458. }
  459. #else
  460. #define rfkill_suspend NULL
  461. #define rfkill_resume NULL
  462. #endif
  463. static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
  464. unsigned long eventid,
  465. void *data)
  466. {
  467. struct rfkill *rfkill = (struct rfkill *)data;
  468. switch (eventid) {
  469. case RFKILL_STATE_CHANGED:
  470. kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
  471. break;
  472. default:
  473. break;
  474. }
  475. return NOTIFY_DONE;
  476. }
  477. static struct notifier_block rfkill_blocking_uevent_nb = {
  478. .notifier_call = rfkill_blocking_uevent_notifier,
  479. .priority = 0,
  480. };
  481. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  482. {
  483. struct rfkill *rfkill = to_rfkill(dev);
  484. int error;
  485. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  486. if (error)
  487. return error;
  488. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  489. rfkill_get_type_str(rfkill->type));
  490. if (error)
  491. return error;
  492. error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
  493. return error;
  494. }
  495. static struct class rfkill_class = {
  496. .name = "rfkill",
  497. .dev_release = rfkill_release,
  498. .dev_attrs = rfkill_dev_attrs,
  499. .suspend = rfkill_suspend,
  500. .resume = rfkill_resume,
  501. .dev_uevent = rfkill_dev_uevent,
  502. };
  503. static int rfkill_check_duplicity(const struct rfkill *rfkill)
  504. {
  505. struct rfkill *p;
  506. unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
  507. memset(seen, 0, sizeof(seen));
  508. list_for_each_entry(p, &rfkill_list, node) {
  509. if (WARN((p == rfkill), KERN_WARNING
  510. "rfkill: illegal attempt to register "
  511. "an already registered rfkill struct\n"))
  512. return -EEXIST;
  513. set_bit(p->type, seen);
  514. }
  515. /* 0: first switch of its kind */
  516. return (test_bit(rfkill->type, seen)) ? 1 : 0;
  517. }
  518. static int rfkill_add_switch(struct rfkill *rfkill)
  519. {
  520. int error;
  521. mutex_lock(&rfkill_global_mutex);
  522. error = rfkill_check_duplicity(rfkill);
  523. if (error < 0)
  524. goto unlock_out;
  525. if (!error) {
  526. /* lock default after first use */
  527. set_bit(rfkill->type, rfkill_states_lockdflt);
  528. rfkill_global_states[rfkill->type].current_state =
  529. rfkill_global_states[rfkill->type].default_state;
  530. }
  531. rfkill_toggle_radio(rfkill,
  532. rfkill_global_states[rfkill->type].current_state,
  533. 0);
  534. list_add_tail(&rfkill->node, &rfkill_list);
  535. error = 0;
  536. unlock_out:
  537. mutex_unlock(&rfkill_global_mutex);
  538. return error;
  539. }
  540. static void rfkill_remove_switch(struct rfkill *rfkill)
  541. {
  542. mutex_lock(&rfkill_global_mutex);
  543. list_del_init(&rfkill->node);
  544. mutex_unlock(&rfkill_global_mutex);
  545. mutex_lock(&rfkill->mutex);
  546. rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
  547. mutex_unlock(&rfkill->mutex);
  548. }
  549. /**
  550. * rfkill_allocate - allocate memory for rfkill structure.
  551. * @parent: device that has rf switch on it
  552. * @type: type of the switch (RFKILL_TYPE_*)
  553. *
  554. * This function should be called by the network driver when it needs
  555. * rfkill structure. Once the structure is allocated the driver should
  556. * finish its initialization by setting the name, private data, enable_radio
  557. * and disable_radio methods and then register it with rfkill_register().
  558. *
  559. * NOTE: If registration fails the structure shoudl be freed by calling
  560. * rfkill_free() otherwise rfkill_unregister() should be used.
  561. */
  562. struct rfkill * __must_check rfkill_allocate(struct device *parent,
  563. enum rfkill_type type)
  564. {
  565. struct rfkill *rfkill;
  566. struct device *dev;
  567. if (WARN((type >= RFKILL_TYPE_MAX),
  568. KERN_WARNING
  569. "rfkill: illegal type %d passed as parameter "
  570. "to rfkill_allocate\n", type))
  571. return NULL;
  572. rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
  573. if (!rfkill)
  574. return NULL;
  575. mutex_init(&rfkill->mutex);
  576. INIT_LIST_HEAD(&rfkill->node);
  577. rfkill->type = type;
  578. dev = &rfkill->dev;
  579. dev->class = &rfkill_class;
  580. dev->parent = parent;
  581. device_initialize(dev);
  582. __module_get(THIS_MODULE);
  583. return rfkill;
  584. }
  585. EXPORT_SYMBOL(rfkill_allocate);
  586. /**
  587. * rfkill_free - Mark rfkill structure for deletion
  588. * @rfkill: rfkill structure to be destroyed
  589. *
  590. * Decrements reference count of the rfkill structure so it is destroyed.
  591. * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  592. */
  593. void rfkill_free(struct rfkill *rfkill)
  594. {
  595. if (rfkill)
  596. put_device(&rfkill->dev);
  597. }
  598. EXPORT_SYMBOL(rfkill_free);
  599. static void rfkill_led_trigger_register(struct rfkill *rfkill)
  600. {
  601. #ifdef CONFIG_RFKILL_LEDS
  602. int error;
  603. if (!rfkill->led_trigger.name)
  604. rfkill->led_trigger.name = rfkill->dev.bus_id;
  605. if (!rfkill->led_trigger.activate)
  606. rfkill->led_trigger.activate = rfkill_led_trigger_activate;
  607. error = led_trigger_register(&rfkill->led_trigger);
  608. if (error)
  609. rfkill->led_trigger.name = NULL;
  610. #endif /* CONFIG_RFKILL_LEDS */
  611. }
  612. static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  613. {
  614. #ifdef CONFIG_RFKILL_LEDS
  615. if (rfkill->led_trigger.name) {
  616. led_trigger_unregister(&rfkill->led_trigger);
  617. rfkill->led_trigger.name = NULL;
  618. }
  619. #endif
  620. }
  621. /**
  622. * rfkill_register - Register a rfkill structure.
  623. * @rfkill: rfkill structure to be registered
  624. *
  625. * This function should be called by the network driver when the rfkill
  626. * structure needs to be registered. Immediately from registration the
  627. * switch driver should be able to service calls to toggle_radio.
  628. */
  629. int __must_check rfkill_register(struct rfkill *rfkill)
  630. {
  631. static atomic_t rfkill_no = ATOMIC_INIT(0);
  632. struct device *dev = &rfkill->dev;
  633. int error;
  634. if (WARN((!rfkill || !rfkill->toggle_radio ||
  635. rfkill->type >= RFKILL_TYPE_MAX ||
  636. rfkill->state >= RFKILL_STATE_MAX),
  637. KERN_WARNING
  638. "rfkill: attempt to register a "
  639. "badly initialized rfkill struct\n"))
  640. return -EINVAL;
  641. snprintf(dev->bus_id, sizeof(dev->bus_id),
  642. "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
  643. rfkill_led_trigger_register(rfkill);
  644. error = rfkill_add_switch(rfkill);
  645. if (error) {
  646. rfkill_led_trigger_unregister(rfkill);
  647. return error;
  648. }
  649. error = device_add(dev);
  650. if (error) {
  651. rfkill_remove_switch(rfkill);
  652. rfkill_led_trigger_unregister(rfkill);
  653. return error;
  654. }
  655. return 0;
  656. }
  657. EXPORT_SYMBOL(rfkill_register);
  658. /**
  659. * rfkill_unregister - Unregister a rfkill structure.
  660. * @rfkill: rfkill structure to be unregistered
  661. *
  662. * This function should be called by the network driver during device
  663. * teardown to destroy rfkill structure. Note that rfkill_free() should
  664. * _not_ be called after rfkill_unregister().
  665. */
  666. void rfkill_unregister(struct rfkill *rfkill)
  667. {
  668. BUG_ON(!rfkill);
  669. device_del(&rfkill->dev);
  670. rfkill_remove_switch(rfkill);
  671. rfkill_led_trigger_unregister(rfkill);
  672. put_device(&rfkill->dev);
  673. }
  674. EXPORT_SYMBOL(rfkill_unregister);
  675. /**
  676. * rfkill_set_default - set initial value for a switch type
  677. * @type - the type of switch to set the default state of
  678. * @state - the new default state for that group of switches
  679. *
  680. * Sets the initial state rfkill should use for a given type.
  681. * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
  682. * and RFKILL_STATE_UNBLOCKED.
  683. *
  684. * This function is meant to be used by platform drivers for platforms
  685. * that can save switch state across power down/reboot.
  686. *
  687. * The default state for each switch type can be changed exactly once.
  688. * After a switch of that type is registered, the default state cannot
  689. * be changed anymore. This guards against multiple drivers it the
  690. * same platform trying to set the initial switch default state, which
  691. * is not allowed.
  692. *
  693. * Returns -EPERM if the state has already been set once or is in use,
  694. * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
  695. * if this function returns -EPERM.
  696. *
  697. * Returns 0 if the new default state was set, or an error if it
  698. * could not be set.
  699. */
  700. int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
  701. {
  702. int error;
  703. if (WARN((type >= RFKILL_TYPE_MAX ||
  704. (state != RFKILL_STATE_SOFT_BLOCKED &&
  705. state != RFKILL_STATE_UNBLOCKED)),
  706. KERN_WARNING
  707. "rfkill: illegal state %d or type %d passed as "
  708. "parameter to rfkill_set_default\n", state, type))
  709. return -EINVAL;
  710. mutex_lock(&rfkill_global_mutex);
  711. if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
  712. rfkill_global_states[type].default_state = state;
  713. error = 0;
  714. } else
  715. error = -EPERM;
  716. mutex_unlock(&rfkill_global_mutex);
  717. return error;
  718. }
  719. EXPORT_SYMBOL_GPL(rfkill_set_default);
  720. /*
  721. * Rfkill module initialization/deinitialization.
  722. */
  723. static int __init rfkill_init(void)
  724. {
  725. int error;
  726. int i;
  727. /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
  728. if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
  729. rfkill_default_state != RFKILL_STATE_UNBLOCKED)
  730. return -EINVAL;
  731. for (i = 0; i < RFKILL_TYPE_MAX; i++)
  732. rfkill_global_states[i].default_state = rfkill_default_state;
  733. error = class_register(&rfkill_class);
  734. if (error) {
  735. printk(KERN_ERR "rfkill: unable to register rfkill class\n");
  736. return error;
  737. }
  738. register_rfkill_notifier(&rfkill_blocking_uevent_nb);
  739. return 0;
  740. }
  741. static void __exit rfkill_exit(void)
  742. {
  743. unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
  744. class_unregister(&rfkill_class);
  745. }
  746. subsys_initcall(rfkill_init);
  747. module_exit(rfkill_exit);