rfkill.c 22 KB

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