rfkill.c 23 KB

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