rfkill.c 11 KB

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