rfkill.c 9.4 KB

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