rfkill.c 12 KB

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