rfkill.c 22 KB

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