rfkill.c 11 KB

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