rfkill.c 10 KB

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