rfkill.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <linux/err.h>
  100. /* this is opaque */
  101. struct rfkill;
  102. /**
  103. * struct rfkill_ops - rfkill driver methods
  104. *
  105. * @poll: poll the rfkill block state(s) -- only assign this method
  106. * when you need polling. When called, simply call one of the
  107. * rfkill_set{,_hw,_sw}_state family of functions. If the hw
  108. * is getting unblocked you need to take into account the return
  109. * value of those functions to make sure the software block is
  110. * properly used.
  111. * @query: query the rfkill block state(s) and call exactly one of the
  112. * rfkill_set{,_hw,_sw}_state family of functions. Assign this
  113. * method if input events can cause hardware state changes to make
  114. * the rfkill core query your driver before setting a requested
  115. * block.
  116. * @set_block: turn the transmitter on (blocked == false) or off
  117. * (blocked == true) -- ignore and return 0 when hard blocked.
  118. * This callback must be assigned.
  119. */
  120. struct rfkill_ops {
  121. void (*poll)(struct rfkill *rfkill, void *data);
  122. void (*query)(struct rfkill *rfkill, void *data);
  123. int (*set_block)(void *data, bool blocked);
  124. };
  125. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  126. /**
  127. * rfkill_alloc - allocate rfkill structure
  128. * @name: name of the struct -- the string is not copied internally
  129. * @parent: device that has rf switch on it
  130. * @type: type of the switch (RFKILL_TYPE_*)
  131. * @ops: rfkill methods
  132. * @ops_data: data passed to each method
  133. *
  134. * This function should be called by the transmitter driver to allocate an
  135. * rfkill structure. Returns %NULL on failure.
  136. */
  137. struct rfkill * __must_check rfkill_alloc(const char *name,
  138. struct device *parent,
  139. const enum rfkill_type type,
  140. const struct rfkill_ops *ops,
  141. void *ops_data);
  142. /**
  143. * rfkill_register - Register a rfkill structure.
  144. * @rfkill: rfkill structure to be registered
  145. *
  146. * This function should be called by the transmitter driver to register
  147. * the rfkill structure. Before calling this function the driver needs
  148. * to be ready to service method calls from rfkill.
  149. *
  150. * If the software blocked state is not set before registration,
  151. * set_block will be called to initialize it to a default value.
  152. *
  153. * If the hardware blocked state is not set before registration,
  154. * it is assumed to be unblocked.
  155. */
  156. int __must_check rfkill_register(struct rfkill *rfkill);
  157. /**
  158. * rfkill_pause_polling(struct rfkill *rfkill)
  159. *
  160. * Pause polling -- say transmitter is off for other reasons.
  161. * NOTE: not necessary for suspend/resume -- in that case the
  162. * core stops polling anyway
  163. */
  164. void rfkill_pause_polling(struct rfkill *rfkill);
  165. /**
  166. * rfkill_resume_polling(struct rfkill *rfkill)
  167. *
  168. * Pause polling -- say transmitter is off for other reasons.
  169. * NOTE: not necessary for suspend/resume -- in that case the
  170. * core stops polling anyway
  171. */
  172. void rfkill_resume_polling(struct rfkill *rfkill);
  173. /**
  174. * rfkill_unregister - Unregister a rfkill structure.
  175. * @rfkill: rfkill structure to be unregistered
  176. *
  177. * This function should be called by the network driver during device
  178. * teardown to destroy rfkill structure. Until it returns, the driver
  179. * needs to be able to service method calls.
  180. */
  181. void rfkill_unregister(struct rfkill *rfkill);
  182. /**
  183. * rfkill_destroy - free rfkill structure
  184. * @rfkill: rfkill structure to be destroyed
  185. *
  186. * Destroys the rfkill structure.
  187. */
  188. void rfkill_destroy(struct rfkill *rfkill);
  189. /**
  190. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  191. * @rfkill: pointer to the rfkill class to modify.
  192. * @state: the current hardware block state to set
  193. *
  194. * rfkill drivers that get events when the hard-blocked state changes
  195. * use this function to notify the rfkill core (and through that also
  196. * userspace) of the current state. They should also use this after
  197. * resume if the state could have changed.
  198. *
  199. * You need not (but may) call this function if poll_state is assigned.
  200. *
  201. * This function can be called in any context, even from within rfkill
  202. * callbacks.
  203. *
  204. * The function returns the combined block state (true if transmitter
  205. * should be blocked) so that drivers need not keep track of the soft
  206. * block state -- which they might not be able to.
  207. */
  208. bool __must_check rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
  209. /**
  210. * rfkill_set_sw_state - Set the internal rfkill software block state
  211. * @rfkill: pointer to the rfkill class to modify.
  212. * @state: the current software block state to set
  213. *
  214. * rfkill drivers that get events when the soft-blocked state changes
  215. * (yes, some platforms directly act on input but allow changing again)
  216. * use this function to notify the rfkill core (and through that also
  217. * userspace) of the current state. It is not necessary to notify on
  218. * resume; since hibernation can always change the soft-blocked state,
  219. * the rfkill core will unconditionally restore the previous state.
  220. *
  221. * This function can be called in any context, even from within rfkill
  222. * callbacks.
  223. *
  224. * The function returns the combined block state (true if transmitter
  225. * should be blocked).
  226. */
  227. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  228. /**
  229. * rfkill_set_states - Set the internal rfkill block states
  230. * @rfkill: pointer to the rfkill class to modify.
  231. * @sw: the current software block state to set
  232. * @hw: the current hardware block state to set
  233. *
  234. * This function can be called in any context, even from within rfkill
  235. * callbacks.
  236. */
  237. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  238. /**
  239. * rfkill_blocked - query rfkill block
  240. *
  241. * @rfkill: rfkill struct to query
  242. */
  243. bool rfkill_blocked(struct rfkill *rfkill);
  244. #else /* !RFKILL */
  245. static inline struct rfkill * __must_check
  246. rfkill_alloc(const char *name,
  247. struct device *parent,
  248. const enum rfkill_type type,
  249. const struct rfkill_ops *ops,
  250. void *ops_data)
  251. {
  252. return ERR_PTR(-ENODEV);
  253. }
  254. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  255. {
  256. if (rfkill == ERR_PTR(-ENODEV))
  257. return 0;
  258. return -EINVAL;
  259. }
  260. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  261. {
  262. }
  263. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  264. {
  265. }
  266. static inline void rfkill_unregister(struct rfkill *rfkill)
  267. {
  268. }
  269. static inline void rfkill_destroy(struct rfkill *rfkill)
  270. {
  271. }
  272. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  273. {
  274. return blocked;
  275. }
  276. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  277. {
  278. return blocked;
  279. }
  280. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  281. {
  282. }
  283. static inline bool rfkill_blocked(struct rfkill *rfkill)
  284. {
  285. return false;
  286. }
  287. #endif /* RFKILL || RFKILL_MODULE */
  288. #ifdef CONFIG_RFKILL_LEDS
  289. /**
  290. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  291. * This function might return a NULL pointer if registering of the
  292. * LED trigger failed. Use this as "default_trigger" for the LED.
  293. */
  294. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  295. /**
  296. * rfkill_set_led_trigger_name -- set the LED trigger name
  297. * @rfkill: rfkill struct
  298. * @name: LED trigger name
  299. *
  300. * This function sets the LED trigger name of the radio LED
  301. * trigger that rfkill creates. It is optional, but if called
  302. * must be called before rfkill_register() to be effective.
  303. */
  304. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  305. #else
  306. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  307. {
  308. return NULL;
  309. }
  310. static inline void
  311. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  312. {
  313. }
  314. #endif
  315. #endif /* __KERNEL__ */
  316. #endif /* RFKILL_H */