rfkill.c 23 KB

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