rfkill.c 11 KB

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