rfkill.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #ifndef __RFKILL_H
  2. #define __RFKILL_H
  3. /*
  4. * Copyright (C) 2006 - 2007 Ivo van Doorn
  5. * Copyright (C) 2007 Dmitry Torokhov
  6. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/types.h>
  24. /* define userspace visible states */
  25. #define RFKILL_STATE_SOFT_BLOCKED 0
  26. #define RFKILL_STATE_UNBLOCKED 1
  27. #define RFKILL_STATE_HARD_BLOCKED 2
  28. /**
  29. * enum rfkill_type - type of rfkill switch.
  30. *
  31. * @RFKILL_TYPE_ALL: toggles all switches (userspace only)
  32. * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device.
  33. * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device.
  34. * @RFKILL_TYPE_UWB: switch is on a ultra wideband device.
  35. * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device.
  36. * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device.
  37. * @NUM_RFKILL_TYPES: number of defined rfkill types
  38. */
  39. enum rfkill_type {
  40. RFKILL_TYPE_ALL = 0,
  41. RFKILL_TYPE_WLAN,
  42. RFKILL_TYPE_BLUETOOTH,
  43. RFKILL_TYPE_UWB,
  44. RFKILL_TYPE_WIMAX,
  45. RFKILL_TYPE_WWAN,
  46. NUM_RFKILL_TYPES,
  47. };
  48. /**
  49. * enum rfkill_operation - operation types
  50. * @RFKILL_OP_ADD: a device was added
  51. * @RFKILL_OP_DEL: a device was removed
  52. * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
  53. * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
  54. */
  55. enum rfkill_operation {
  56. RFKILL_OP_ADD = 0,
  57. RFKILL_OP_DEL,
  58. RFKILL_OP_CHANGE,
  59. RFKILL_OP_CHANGE_ALL,
  60. };
  61. /**
  62. * struct rfkill_event - events for userspace on /dev/rfkill
  63. * @idx: index of dev rfkill
  64. * @type: type of the rfkill struct
  65. * @op: operation code
  66. * @hard: hard state (0/1)
  67. * @soft: soft state (0/1)
  68. *
  69. * Structure used for userspace communication on /dev/rfkill,
  70. * used for events from the kernel and control to the kernel.
  71. */
  72. struct rfkill_event {
  73. __u32 idx;
  74. __u8 type;
  75. __u8 op;
  76. __u8 soft, hard;
  77. } __packed;
  78. /* ioctl for turning off rfkill-input (if present) */
  79. #define RFKILL_IOC_MAGIC 'R'
  80. #define RFKILL_IOC_NOINPUT 1
  81. #define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
  82. /* and that's all userspace gets */
  83. #ifdef __KERNEL__
  84. /* don't allow anyone to use these in the kernel */
  85. enum rfkill_user_states {
  86. RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
  87. RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
  88. RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
  89. };
  90. #undef RFKILL_STATE_SOFT_BLOCKED
  91. #undef RFKILL_STATE_UNBLOCKED
  92. #undef RFKILL_STATE_HARD_BLOCKED
  93. #include <linux/types.h>
  94. #include <linux/kernel.h>
  95. #include <linux/list.h>
  96. #include <linux/mutex.h>
  97. #include <linux/device.h>
  98. #include <linux/leds.h>
  99. /* this is opaque */
  100. struct rfkill;
  101. /**
  102. * struct rfkill_ops - rfkill driver methods
  103. *
  104. * @poll: poll the rfkill block state(s) -- only assign this method
  105. * when you need polling. When called, simply call one of the
  106. * rfkill_set{,_hw,_sw}_state family of functions. If the hw
  107. * is getting unblocked you need to take into account the return
  108. * value of those functions to make sure the software block is
  109. * properly used.
  110. * @query: query the rfkill block state(s) and call exactly one of the
  111. * rfkill_set{,_hw,_sw}_state family of functions. Assign this
  112. * method if input events can cause hardware state changes to make
  113. * the rfkill core query your driver before setting a requested
  114. * block.
  115. * @set_block: turn the transmitter on (blocked == false) or off
  116. * (blocked == true) -- ignore and return 0 when hard blocked.
  117. * This callback must be assigned.
  118. */
  119. struct rfkill_ops {
  120. void (*poll)(struct rfkill *rfkill, void *data);
  121. void (*query)(struct rfkill *rfkill, void *data);
  122. int (*set_block)(void *data, bool blocked);
  123. };
  124. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  125. /**
  126. * rfkill_alloc - allocate rfkill structure
  127. * @name: name of the struct -- the string is not copied internally
  128. * @parent: device that has rf switch on it
  129. * @type: type of the switch (RFKILL_TYPE_*)
  130. * @ops: rfkill methods
  131. * @ops_data: data passed to each method
  132. *
  133. * This function should be called by the transmitter driver to allocate an
  134. * rfkill structure. Returns %NULL on failure.
  135. */
  136. struct rfkill * __must_check rfkill_alloc(const char *name,
  137. struct device *parent,
  138. const enum rfkill_type type,
  139. const struct rfkill_ops *ops,
  140. void *ops_data);
  141. /**
  142. * rfkill_register - Register a rfkill structure.
  143. * @rfkill: rfkill structure to be registered
  144. *
  145. * This function should be called by the transmitter driver to register
  146. * the rfkill structure needs to be registered. Before calling this function
  147. * the driver needs to be ready to service method calls from rfkill.
  148. */
  149. int __must_check rfkill_register(struct rfkill *rfkill);
  150. /**
  151. * rfkill_pause_polling(struct rfkill *rfkill)
  152. *
  153. * Pause polling -- say transmitter is off for other reasons.
  154. * NOTE: not necessary for suspend/resume -- in that case the
  155. * core stops polling anyway
  156. */
  157. void rfkill_pause_polling(struct rfkill *rfkill);
  158. /**
  159. * rfkill_resume_polling(struct rfkill *rfkill)
  160. *
  161. * Pause polling -- say transmitter is off for other reasons.
  162. * NOTE: not necessary for suspend/resume -- in that case the
  163. * core stops polling anyway
  164. */
  165. void rfkill_resume_polling(struct rfkill *rfkill);
  166. /**
  167. * rfkill_unregister - Unregister a rfkill structure.
  168. * @rfkill: rfkill structure to be unregistered
  169. *
  170. * This function should be called by the network driver during device
  171. * teardown to destroy rfkill structure. Until it returns, the driver
  172. * needs to be able to service method calls.
  173. */
  174. void rfkill_unregister(struct rfkill *rfkill);
  175. /**
  176. * rfkill_destroy - free rfkill structure
  177. * @rfkill: rfkill structure to be destroyed
  178. *
  179. * Destroys the rfkill structure.
  180. */
  181. void rfkill_destroy(struct rfkill *rfkill);
  182. /**
  183. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  184. * @rfkill: pointer to the rfkill class to modify.
  185. * @state: the current hardware block state to set
  186. *
  187. * rfkill drivers that get events when the hard-blocked state changes
  188. * use this function to notify the rfkill core (and through that also
  189. * userspace) of the current state -- they should also use this after
  190. * resume if the state could have changed.
  191. *
  192. * You need not (but may) call this function if poll_state is assigned.
  193. *
  194. * This function can be called in any context, even from within rfkill
  195. * callbacks.
  196. *
  197. * The function returns the combined block state (true if transmitter
  198. * should be blocked) so that drivers need not keep track of the soft
  199. * block state -- which they might not be able to.
  200. */
  201. bool __must_check rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
  202. /**
  203. * rfkill_set_sw_state - Set the internal rfkill software block state
  204. * @rfkill: pointer to the rfkill class to modify.
  205. * @state: the current software block state to set
  206. *
  207. * rfkill drivers that get events when the soft-blocked state changes
  208. * (yes, some platforms directly act on input but allow changing again)
  209. * use this function to notify the rfkill core (and through that also
  210. * userspace) of the current state -- they should also use this after
  211. * resume if the state could have changed.
  212. *
  213. * This function can be called in any context, even from within rfkill
  214. * callbacks.
  215. *
  216. * The function returns the combined block state (true if transmitter
  217. * should be blocked).
  218. */
  219. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  220. /**
  221. * rfkill_set_states - Set the internal rfkill block states
  222. * @rfkill: pointer to the rfkill class to modify.
  223. * @sw: the current software block state to set
  224. * @hw: the current hardware block state to set
  225. *
  226. * This function can be called in any context, even from within rfkill
  227. * callbacks.
  228. */
  229. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  230. /**
  231. * rfkill_set_global_sw_state - set global sw block default
  232. * @type: rfkill type to set default for
  233. * @blocked: default to set
  234. *
  235. * This function sets the global default -- use at boot if your platform has
  236. * an rfkill switch. If not early enough this call may be ignored.
  237. *
  238. * XXX: instead of ignoring -- how about just updating all currently
  239. * registered drivers?
  240. */
  241. void rfkill_set_global_sw_state(const enum rfkill_type type, bool blocked);
  242. /**
  243. * rfkill_blocked - query rfkill block
  244. *
  245. * @rfkill: rfkill struct to query
  246. */
  247. bool rfkill_blocked(struct rfkill *rfkill);
  248. #else /* !RFKILL */
  249. static inline struct rfkill * __must_check
  250. rfkill_alloc(const char *name,
  251. struct device *parent,
  252. const enum rfkill_type type,
  253. const struct rfkill_ops *ops,
  254. void *ops_data)
  255. {
  256. return ERR_PTR(-ENODEV);
  257. }
  258. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  259. {
  260. if (rfkill == ERR_PTR(-ENODEV))
  261. return 0;
  262. return -EINVAL;
  263. }
  264. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  265. {
  266. }
  267. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  268. {
  269. }
  270. static inline void rfkill_unregister(struct rfkill *rfkill)
  271. {
  272. }
  273. static inline void rfkill_destroy(struct rfkill *rfkill)
  274. {
  275. }
  276. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  277. {
  278. return blocked;
  279. }
  280. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  281. {
  282. return blocked;
  283. }
  284. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  285. {
  286. }
  287. static inline void rfkill_set_global_sw_state(const enum rfkill_type type,
  288. bool blocked)
  289. {
  290. }
  291. static inline bool rfkill_blocked(struct rfkill *rfkill)
  292. {
  293. return false;
  294. }
  295. #endif /* RFKILL || RFKILL_MODULE */
  296. #ifdef CONFIG_RFKILL_LEDS
  297. /**
  298. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  299. * This function might return a NULL pointer if registering of the
  300. * LED trigger failed. Use this as "default_trigger" for the LED.
  301. */
  302. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  303. /**
  304. * rfkill_set_led_trigger_name -- set the LED trigger name
  305. * @rfkill: rfkill struct
  306. * @name: LED trigger name
  307. *
  308. * This function sets the LED trigger name of the radio LED
  309. * trigger that rfkill creates. It is optional, but if called
  310. * must be called before rfkill_register() to be effective.
  311. */
  312. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  313. #else
  314. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  315. {
  316. return NULL;
  317. }
  318. static inline void
  319. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  320. {
  321. }
  322. #endif
  323. #endif /* __KERNEL__ */
  324. #endif /* RFKILL_H */