rfkill.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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_ON;
  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. static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
  41. static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
  42. /**
  43. * register_rfkill_notifier - Add notifier to rfkill notifier chain
  44. * @nb: pointer to the new entry to add to the chain
  45. *
  46. * See blocking_notifier_chain_register() for return value and further
  47. * observations.
  48. *
  49. * Adds a notifier to the rfkill notifier chain. The chain will be
  50. * called with a pointer to the relevant rfkill structure as a parameter,
  51. * refer to include/linux/rfkill.h for the possible events.
  52. *
  53. * Notifiers added to this chain are to always return NOTIFY_DONE. This
  54. * chain is a blocking notifier chain: notifiers can sleep.
  55. *
  56. * Calls to this chain may have been done through a workqueue. One must
  57. * assume unordered asynchronous behaviour, there is no way to know if
  58. * actions related to the event that generated the notification have been
  59. * carried out already.
  60. */
  61. int register_rfkill_notifier(struct notifier_block *nb)
  62. {
  63. return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
  64. }
  65. EXPORT_SYMBOL_GPL(register_rfkill_notifier);
  66. /**
  67. * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
  68. * @nb: pointer to the entry to remove from the chain
  69. *
  70. * See blocking_notifier_chain_unregister() for return value and further
  71. * observations.
  72. *
  73. * Removes a notifier from the rfkill notifier chain.
  74. */
  75. int unregister_rfkill_notifier(struct notifier_block *nb)
  76. {
  77. return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
  78. }
  79. EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
  80. static void rfkill_led_trigger(struct rfkill *rfkill,
  81. enum rfkill_state state)
  82. {
  83. #ifdef CONFIG_RFKILL_LEDS
  84. struct led_trigger *led = &rfkill->led_trigger;
  85. if (!led->name)
  86. return;
  87. if (state == RFKILL_STATE_OFF)
  88. led_trigger_event(led, LED_OFF);
  89. else
  90. led_trigger_event(led, LED_FULL);
  91. #endif /* CONFIG_RFKILL_LEDS */
  92. }
  93. static void notify_rfkill_state_change(struct rfkill *rfkill)
  94. {
  95. blocking_notifier_call_chain(&rfkill_notifier_list,
  96. RFKILL_STATE_CHANGED,
  97. rfkill);
  98. }
  99. static void update_rfkill_state(struct rfkill *rfkill)
  100. {
  101. enum rfkill_state newstate, oldstate;
  102. if (rfkill->get_state) {
  103. mutex_lock(&rfkill->mutex);
  104. if (!rfkill->get_state(rfkill->data, &newstate)) {
  105. oldstate = rfkill->state;
  106. rfkill->state = newstate;
  107. if (oldstate != newstate)
  108. notify_rfkill_state_change(rfkill);
  109. }
  110. mutex_unlock(&rfkill->mutex);
  111. }
  112. }
  113. static int rfkill_toggle_radio(struct rfkill *rfkill,
  114. enum rfkill_state state,
  115. int force)
  116. {
  117. int retval = 0;
  118. enum rfkill_state oldstate, newstate;
  119. oldstate = rfkill->state;
  120. if (rfkill->get_state && !force &&
  121. !rfkill->get_state(rfkill->data, &newstate))
  122. rfkill->state = newstate;
  123. if (force || state != rfkill->state) {
  124. retval = rfkill->toggle_radio(rfkill->data, state);
  125. if (!retval)
  126. rfkill->state = state;
  127. }
  128. if (force || rfkill->state != oldstate) {
  129. rfkill_led_trigger(rfkill, rfkill->state);
  130. notify_rfkill_state_change(rfkill);
  131. }
  132. return retval;
  133. }
  134. /**
  135. * rfkill_switch_all - Toggle state of all switches of given type
  136. * @type: type of interfaces to be affeceted
  137. * @state: the new state
  138. *
  139. * This function toggles state of all switches of given type unless
  140. * a specific switch is claimed by userspace in which case it is
  141. * left alone.
  142. */
  143. void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
  144. {
  145. struct rfkill *rfkill;
  146. mutex_lock(&rfkill_mutex);
  147. rfkill_states[type] = state;
  148. list_for_each_entry(rfkill, &rfkill_list, node) {
  149. if ((!rfkill->user_claim) && (rfkill->type == type))
  150. rfkill_toggle_radio(rfkill, state, 0);
  151. }
  152. mutex_unlock(&rfkill_mutex);
  153. }
  154. EXPORT_SYMBOL(rfkill_switch_all);
  155. /**
  156. * rfkill_force_state - Force the internal rfkill radio state
  157. * @rfkill: pointer to the rfkill class to modify.
  158. * @state: the current radio state the class should be forced to.
  159. *
  160. * This function updates the internal state of the radio cached
  161. * by the rfkill class. It should be used when the driver gets
  162. * a notification by the firmware/hardware of the current *real*
  163. * state of the radio rfkill switch.
  164. *
  165. * It may not be called from an atomic context.
  166. */
  167. int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
  168. {
  169. enum rfkill_state oldstate;
  170. if (state != RFKILL_STATE_OFF &&
  171. state != RFKILL_STATE_ON)
  172. return -EINVAL;
  173. mutex_lock(&rfkill->mutex);
  174. oldstate = rfkill->state;
  175. rfkill->state = state;
  176. if (state != oldstate)
  177. notify_rfkill_state_change(rfkill);
  178. mutex_unlock(&rfkill->mutex);
  179. return 0;
  180. }
  181. EXPORT_SYMBOL(rfkill_force_state);
  182. static ssize_t rfkill_name_show(struct device *dev,
  183. struct device_attribute *attr,
  184. char *buf)
  185. {
  186. struct rfkill *rfkill = to_rfkill(dev);
  187. return sprintf(buf, "%s\n", rfkill->name);
  188. }
  189. static ssize_t rfkill_type_show(struct device *dev,
  190. struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct rfkill *rfkill = to_rfkill(dev);
  194. const char *type;
  195. switch (rfkill->type) {
  196. case RFKILL_TYPE_WLAN:
  197. type = "wlan";
  198. break;
  199. case RFKILL_TYPE_BLUETOOTH:
  200. type = "bluetooth";
  201. break;
  202. case RFKILL_TYPE_UWB:
  203. type = "ultrawideband";
  204. break;
  205. case RFKILL_TYPE_WIMAX:
  206. type = "wimax";
  207. break;
  208. case RFKILL_TYPE_WWAN:
  209. type = "wwan";
  210. break;
  211. default:
  212. BUG();
  213. }
  214. return sprintf(buf, "%s\n", type);
  215. }
  216. static ssize_t rfkill_state_show(struct device *dev,
  217. struct device_attribute *attr,
  218. char *buf)
  219. {
  220. struct rfkill *rfkill = to_rfkill(dev);
  221. update_rfkill_state(rfkill);
  222. return sprintf(buf, "%d\n", rfkill->state);
  223. }
  224. static ssize_t rfkill_state_store(struct device *dev,
  225. struct device_attribute *attr,
  226. const char *buf, size_t count)
  227. {
  228. struct rfkill *rfkill = to_rfkill(dev);
  229. unsigned int state = simple_strtoul(buf, NULL, 0);
  230. int error;
  231. if (!capable(CAP_NET_ADMIN))
  232. return -EPERM;
  233. if (mutex_lock_interruptible(&rfkill->mutex))
  234. return -ERESTARTSYS;
  235. error = rfkill_toggle_radio(rfkill,
  236. state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
  237. 0);
  238. mutex_unlock(&rfkill->mutex);
  239. return error ? error : count;
  240. }
  241. static ssize_t rfkill_claim_show(struct device *dev,
  242. struct device_attribute *attr,
  243. char *buf)
  244. {
  245. struct rfkill *rfkill = to_rfkill(dev);
  246. return sprintf(buf, "%d", rfkill->user_claim);
  247. }
  248. static ssize_t rfkill_claim_store(struct device *dev,
  249. struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct rfkill *rfkill = to_rfkill(dev);
  253. bool claim = !!simple_strtoul(buf, NULL, 0);
  254. int error;
  255. if (!capable(CAP_NET_ADMIN))
  256. return -EPERM;
  257. /*
  258. * Take the global lock to make sure the kernel is not in
  259. * the middle of rfkill_switch_all
  260. */
  261. error = mutex_lock_interruptible(&rfkill_mutex);
  262. if (error)
  263. return error;
  264. if (rfkill->user_claim_unsupported) {
  265. error = -EOPNOTSUPP;
  266. goto out_unlock;
  267. }
  268. if (rfkill->user_claim != claim) {
  269. if (!claim)
  270. rfkill_toggle_radio(rfkill,
  271. rfkill_states[rfkill->type],
  272. 0);
  273. rfkill->user_claim = claim;
  274. }
  275. out_unlock:
  276. mutex_unlock(&rfkill_mutex);
  277. return error ? error : count;
  278. }
  279. static struct device_attribute rfkill_dev_attrs[] = {
  280. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  281. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  282. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  283. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  284. __ATTR_NULL
  285. };
  286. static void rfkill_release(struct device *dev)
  287. {
  288. struct rfkill *rfkill = to_rfkill(dev);
  289. kfree(rfkill);
  290. module_put(THIS_MODULE);
  291. }
  292. #ifdef CONFIG_PM
  293. static int rfkill_suspend(struct device *dev, pm_message_t state)
  294. {
  295. struct rfkill *rfkill = to_rfkill(dev);
  296. if (dev->power.power_state.event != state.event) {
  297. if (state.event & PM_EVENT_SLEEP) {
  298. /* Stop transmitter, keep state, no notifies */
  299. update_rfkill_state(rfkill);
  300. mutex_lock(&rfkill->mutex);
  301. rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
  302. mutex_unlock(&rfkill->mutex);
  303. }
  304. dev->power.power_state = state;
  305. }
  306. return 0;
  307. }
  308. static int rfkill_resume(struct device *dev)
  309. {
  310. struct rfkill *rfkill = to_rfkill(dev);
  311. if (dev->power.power_state.event != PM_EVENT_ON) {
  312. mutex_lock(&rfkill->mutex);
  313. /* restore radio state AND notify everybody */
  314. rfkill_toggle_radio(rfkill, rfkill->state, 1);
  315. mutex_unlock(&rfkill->mutex);
  316. }
  317. dev->power.power_state = PMSG_ON;
  318. return 0;
  319. }
  320. #else
  321. #define rfkill_suspend NULL
  322. #define rfkill_resume NULL
  323. #endif
  324. static struct class rfkill_class = {
  325. .name = "rfkill",
  326. .dev_release = rfkill_release,
  327. .dev_attrs = rfkill_dev_attrs,
  328. .suspend = rfkill_suspend,
  329. .resume = rfkill_resume,
  330. };
  331. static int rfkill_add_switch(struct rfkill *rfkill)
  332. {
  333. int error;
  334. mutex_lock(&rfkill_mutex);
  335. error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
  336. if (!error)
  337. list_add_tail(&rfkill->node, &rfkill_list);
  338. mutex_unlock(&rfkill_mutex);
  339. return error;
  340. }
  341. static void rfkill_remove_switch(struct rfkill *rfkill)
  342. {
  343. mutex_lock(&rfkill_mutex);
  344. list_del_init(&rfkill->node);
  345. rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
  346. mutex_unlock(&rfkill_mutex);
  347. }
  348. /**
  349. * rfkill_allocate - allocate memory for rfkill structure.
  350. * @parent: device that has rf switch on it
  351. * @type: type of the switch (RFKILL_TYPE_*)
  352. *
  353. * This function should be called by the network driver when it needs
  354. * rfkill structure. Once the structure is allocated the driver shoud
  355. * finish its initialization by setting name, private data, enable_radio
  356. * and disable_radio methods and then register it with rfkill_register().
  357. * NOTE: If registration fails the structure shoudl be freed by calling
  358. * rfkill_free() otherwise rfkill_unregister() should be used.
  359. */
  360. struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
  361. {
  362. struct rfkill *rfkill;
  363. struct device *dev;
  364. rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
  365. if (!rfkill)
  366. return NULL;
  367. mutex_init(&rfkill->mutex);
  368. INIT_LIST_HEAD(&rfkill->node);
  369. rfkill->type = type;
  370. dev = &rfkill->dev;
  371. dev->class = &rfkill_class;
  372. dev->parent = parent;
  373. device_initialize(dev);
  374. __module_get(THIS_MODULE);
  375. return rfkill;
  376. }
  377. EXPORT_SYMBOL(rfkill_allocate);
  378. /**
  379. * rfkill_free - Mark rfkill structure for deletion
  380. * @rfkill: rfkill structure to be destroyed
  381. *
  382. * Decrements reference count of rfkill structure so it is destroyed.
  383. * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  384. */
  385. void rfkill_free(struct rfkill *rfkill)
  386. {
  387. if (rfkill)
  388. put_device(&rfkill->dev);
  389. }
  390. EXPORT_SYMBOL(rfkill_free);
  391. static void rfkill_led_trigger_register(struct rfkill *rfkill)
  392. {
  393. #ifdef CONFIG_RFKILL_LEDS
  394. int error;
  395. rfkill->led_trigger.name = rfkill->dev.bus_id;
  396. error = led_trigger_register(&rfkill->led_trigger);
  397. if (error)
  398. rfkill->led_trigger.name = NULL;
  399. #endif /* CONFIG_RFKILL_LEDS */
  400. }
  401. static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  402. {
  403. #ifdef CONFIG_RFKILL_LEDS
  404. if (rfkill->led_trigger.name)
  405. led_trigger_unregister(&rfkill->led_trigger);
  406. #endif
  407. }
  408. /**
  409. * rfkill_register - Register a rfkill structure.
  410. * @rfkill: rfkill structure to be registered
  411. *
  412. * This function should be called by the network driver when the rfkill
  413. * structure needs to be registered. Immediately from registration the
  414. * switch driver should be able to service calls to toggle_radio.
  415. */
  416. int rfkill_register(struct rfkill *rfkill)
  417. {
  418. static atomic_t rfkill_no = ATOMIC_INIT(0);
  419. struct device *dev = &rfkill->dev;
  420. int error;
  421. if (!rfkill->toggle_radio)
  422. return -EINVAL;
  423. if (rfkill->type >= RFKILL_TYPE_MAX)
  424. return -EINVAL;
  425. snprintf(dev->bus_id, sizeof(dev->bus_id),
  426. "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
  427. rfkill_led_trigger_register(rfkill);
  428. error = rfkill_add_switch(rfkill);
  429. if (error) {
  430. rfkill_led_trigger_unregister(rfkill);
  431. return error;
  432. }
  433. error = device_add(dev);
  434. if (error) {
  435. rfkill_led_trigger_unregister(rfkill);
  436. rfkill_remove_switch(rfkill);
  437. return error;
  438. }
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(rfkill_register);
  442. /**
  443. * rfkill_unregister - Unregister a rfkill structure.
  444. * @rfkill: rfkill structure to be unregistered
  445. *
  446. * This function should be called by the network driver during device
  447. * teardown to destroy rfkill structure. Note that rfkill_free() should
  448. * _not_ be called after rfkill_unregister().
  449. */
  450. void rfkill_unregister(struct rfkill *rfkill)
  451. {
  452. device_del(&rfkill->dev);
  453. rfkill_remove_switch(rfkill);
  454. rfkill_led_trigger_unregister(rfkill);
  455. put_device(&rfkill->dev);
  456. }
  457. EXPORT_SYMBOL(rfkill_unregister);
  458. /*
  459. * Rfkill module initialization/deinitialization.
  460. */
  461. static int __init rfkill_init(void)
  462. {
  463. int error;
  464. int i;
  465. if (rfkill_default_state != RFKILL_STATE_OFF &&
  466. rfkill_default_state != RFKILL_STATE_ON)
  467. return -EINVAL;
  468. for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
  469. rfkill_states[i] = rfkill_default_state;
  470. error = class_register(&rfkill_class);
  471. if (error) {
  472. printk(KERN_ERR "rfkill: unable to register rfkill class\n");
  473. return error;
  474. }
  475. return 0;
  476. }
  477. static void __exit rfkill_exit(void)
  478. {
  479. class_unregister(&rfkill_class);
  480. }
  481. subsys_initcall(rfkill_init);
  482. module_exit(rfkill_exit);