rfkill.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 const char *rfkill_get_type_str(enum rfkill_type type)
  190. {
  191. switch (type) {
  192. case RFKILL_TYPE_WLAN:
  193. return "wlan";
  194. case RFKILL_TYPE_BLUETOOTH:
  195. return "bluetooth";
  196. case RFKILL_TYPE_UWB:
  197. return "ultrawideband";
  198. case RFKILL_TYPE_WIMAX:
  199. return "wimax";
  200. case RFKILL_TYPE_WWAN:
  201. return "wwan";
  202. default:
  203. BUG();
  204. }
  205. }
  206. static ssize_t rfkill_type_show(struct device *dev,
  207. struct device_attribute *attr,
  208. char *buf)
  209. {
  210. struct rfkill *rfkill = to_rfkill(dev);
  211. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  212. }
  213. static ssize_t rfkill_state_show(struct device *dev,
  214. struct device_attribute *attr,
  215. char *buf)
  216. {
  217. struct rfkill *rfkill = to_rfkill(dev);
  218. update_rfkill_state(rfkill);
  219. return sprintf(buf, "%d\n", rfkill->state);
  220. }
  221. static ssize_t rfkill_state_store(struct device *dev,
  222. struct device_attribute *attr,
  223. const char *buf, size_t count)
  224. {
  225. struct rfkill *rfkill = to_rfkill(dev);
  226. unsigned int state = simple_strtoul(buf, NULL, 0);
  227. int error;
  228. if (!capable(CAP_NET_ADMIN))
  229. return -EPERM;
  230. if (mutex_lock_interruptible(&rfkill->mutex))
  231. return -ERESTARTSYS;
  232. error = rfkill_toggle_radio(rfkill,
  233. state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
  234. 0);
  235. mutex_unlock(&rfkill->mutex);
  236. return error ? error : count;
  237. }
  238. static ssize_t rfkill_claim_show(struct device *dev,
  239. struct device_attribute *attr,
  240. char *buf)
  241. {
  242. struct rfkill *rfkill = to_rfkill(dev);
  243. return sprintf(buf, "%d", rfkill->user_claim);
  244. }
  245. static ssize_t rfkill_claim_store(struct device *dev,
  246. struct device_attribute *attr,
  247. const char *buf, size_t count)
  248. {
  249. struct rfkill *rfkill = to_rfkill(dev);
  250. bool claim = !!simple_strtoul(buf, NULL, 0);
  251. int error;
  252. if (!capable(CAP_NET_ADMIN))
  253. return -EPERM;
  254. /*
  255. * Take the global lock to make sure the kernel is not in
  256. * the middle of rfkill_switch_all
  257. */
  258. error = mutex_lock_interruptible(&rfkill_mutex);
  259. if (error)
  260. return error;
  261. if (rfkill->user_claim_unsupported) {
  262. error = -EOPNOTSUPP;
  263. goto out_unlock;
  264. }
  265. if (rfkill->user_claim != claim) {
  266. if (!claim)
  267. rfkill_toggle_radio(rfkill,
  268. rfkill_states[rfkill->type],
  269. 0);
  270. rfkill->user_claim = claim;
  271. }
  272. out_unlock:
  273. mutex_unlock(&rfkill_mutex);
  274. return error ? error : count;
  275. }
  276. static struct device_attribute rfkill_dev_attrs[] = {
  277. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  278. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  279. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  280. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  281. __ATTR_NULL
  282. };
  283. static void rfkill_release(struct device *dev)
  284. {
  285. struct rfkill *rfkill = to_rfkill(dev);
  286. kfree(rfkill);
  287. module_put(THIS_MODULE);
  288. }
  289. #ifdef CONFIG_PM
  290. static int rfkill_suspend(struct device *dev, pm_message_t state)
  291. {
  292. struct rfkill *rfkill = to_rfkill(dev);
  293. if (dev->power.power_state.event != state.event) {
  294. if (state.event & PM_EVENT_SLEEP) {
  295. /* Stop transmitter, keep state, no notifies */
  296. update_rfkill_state(rfkill);
  297. mutex_lock(&rfkill->mutex);
  298. rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
  299. mutex_unlock(&rfkill->mutex);
  300. }
  301. dev->power.power_state = state;
  302. }
  303. return 0;
  304. }
  305. static int rfkill_resume(struct device *dev)
  306. {
  307. struct rfkill *rfkill = to_rfkill(dev);
  308. if (dev->power.power_state.event != PM_EVENT_ON) {
  309. mutex_lock(&rfkill->mutex);
  310. /* restore radio state AND notify everybody */
  311. rfkill_toggle_radio(rfkill, rfkill->state, 1);
  312. mutex_unlock(&rfkill->mutex);
  313. }
  314. dev->power.power_state = PMSG_ON;
  315. return 0;
  316. }
  317. #else
  318. #define rfkill_suspend NULL
  319. #define rfkill_resume NULL
  320. #endif
  321. static struct class rfkill_class = {
  322. .name = "rfkill",
  323. .dev_release = rfkill_release,
  324. .dev_attrs = rfkill_dev_attrs,
  325. .suspend = rfkill_suspend,
  326. .resume = rfkill_resume,
  327. };
  328. static int rfkill_add_switch(struct rfkill *rfkill)
  329. {
  330. int error;
  331. mutex_lock(&rfkill_mutex);
  332. error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
  333. if (!error)
  334. list_add_tail(&rfkill->node, &rfkill_list);
  335. mutex_unlock(&rfkill_mutex);
  336. return error;
  337. }
  338. static void rfkill_remove_switch(struct rfkill *rfkill)
  339. {
  340. mutex_lock(&rfkill_mutex);
  341. list_del_init(&rfkill->node);
  342. rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
  343. mutex_unlock(&rfkill_mutex);
  344. }
  345. /**
  346. * rfkill_allocate - allocate memory for rfkill structure.
  347. * @parent: device that has rf switch on it
  348. * @type: type of the switch (RFKILL_TYPE_*)
  349. *
  350. * This function should be called by the network driver when it needs
  351. * rfkill structure. Once the structure is allocated the driver shoud
  352. * finish its initialization by setting name, private data, enable_radio
  353. * and disable_radio methods and then register it with rfkill_register().
  354. * NOTE: If registration fails the structure shoudl be freed by calling
  355. * rfkill_free() otherwise rfkill_unregister() should be used.
  356. */
  357. struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
  358. {
  359. struct rfkill *rfkill;
  360. struct device *dev;
  361. rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
  362. if (!rfkill)
  363. return NULL;
  364. mutex_init(&rfkill->mutex);
  365. INIT_LIST_HEAD(&rfkill->node);
  366. rfkill->type = type;
  367. dev = &rfkill->dev;
  368. dev->class = &rfkill_class;
  369. dev->parent = parent;
  370. device_initialize(dev);
  371. __module_get(THIS_MODULE);
  372. return rfkill;
  373. }
  374. EXPORT_SYMBOL(rfkill_allocate);
  375. /**
  376. * rfkill_free - Mark rfkill structure for deletion
  377. * @rfkill: rfkill structure to be destroyed
  378. *
  379. * Decrements reference count of rfkill structure so it is destroyed.
  380. * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  381. */
  382. void rfkill_free(struct rfkill *rfkill)
  383. {
  384. if (rfkill)
  385. put_device(&rfkill->dev);
  386. }
  387. EXPORT_SYMBOL(rfkill_free);
  388. static void rfkill_led_trigger_register(struct rfkill *rfkill)
  389. {
  390. #ifdef CONFIG_RFKILL_LEDS
  391. int error;
  392. rfkill->led_trigger.name = rfkill->dev.bus_id;
  393. error = led_trigger_register(&rfkill->led_trigger);
  394. if (error)
  395. rfkill->led_trigger.name = NULL;
  396. #endif /* CONFIG_RFKILL_LEDS */
  397. }
  398. static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  399. {
  400. #ifdef CONFIG_RFKILL_LEDS
  401. if (rfkill->led_trigger.name)
  402. led_trigger_unregister(&rfkill->led_trigger);
  403. #endif
  404. }
  405. /**
  406. * rfkill_register - Register a rfkill structure.
  407. * @rfkill: rfkill structure to be registered
  408. *
  409. * This function should be called by the network driver when the rfkill
  410. * structure needs to be registered. Immediately from registration the
  411. * switch driver should be able to service calls to toggle_radio.
  412. */
  413. int rfkill_register(struct rfkill *rfkill)
  414. {
  415. static atomic_t rfkill_no = ATOMIC_INIT(0);
  416. struct device *dev = &rfkill->dev;
  417. int error;
  418. if (!rfkill->toggle_radio)
  419. return -EINVAL;
  420. if (rfkill->type >= RFKILL_TYPE_MAX)
  421. return -EINVAL;
  422. snprintf(dev->bus_id, sizeof(dev->bus_id),
  423. "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
  424. rfkill_led_trigger_register(rfkill);
  425. error = rfkill_add_switch(rfkill);
  426. if (error) {
  427. rfkill_led_trigger_unregister(rfkill);
  428. return error;
  429. }
  430. error = device_add(dev);
  431. if (error) {
  432. rfkill_led_trigger_unregister(rfkill);
  433. rfkill_remove_switch(rfkill);
  434. return error;
  435. }
  436. return 0;
  437. }
  438. EXPORT_SYMBOL(rfkill_register);
  439. /**
  440. * rfkill_unregister - Unregister a rfkill structure.
  441. * @rfkill: rfkill structure to be unregistered
  442. *
  443. * This function should be called by the network driver during device
  444. * teardown to destroy rfkill structure. Note that rfkill_free() should
  445. * _not_ be called after rfkill_unregister().
  446. */
  447. void rfkill_unregister(struct rfkill *rfkill)
  448. {
  449. device_del(&rfkill->dev);
  450. rfkill_remove_switch(rfkill);
  451. rfkill_led_trigger_unregister(rfkill);
  452. put_device(&rfkill->dev);
  453. }
  454. EXPORT_SYMBOL(rfkill_unregister);
  455. /*
  456. * Rfkill module initialization/deinitialization.
  457. */
  458. static int __init rfkill_init(void)
  459. {
  460. int error;
  461. int i;
  462. if (rfkill_default_state != RFKILL_STATE_OFF &&
  463. rfkill_default_state != RFKILL_STATE_ON)
  464. return -EINVAL;
  465. for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
  466. rfkill_states[i] = rfkill_default_state;
  467. error = class_register(&rfkill_class);
  468. if (error) {
  469. printk(KERN_ERR "rfkill: unable to register rfkill class\n");
  470. return error;
  471. }
  472. return 0;
  473. }
  474. static void __exit rfkill_exit(void)
  475. {
  476. class_unregister(&rfkill_class);
  477. }
  478. subsys_initcall(rfkill_init);
  479. module_exit(rfkill_exit);