rfkill.c 12 KB

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