rfkill.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 rfkill_init_sw_state() is not called before registration,
  151. * set_block() will be called to initialize the software blocked state
  152. * to a default value.
  153. *
  154. * If the hardware blocked state is not set before registration,
  155. * it is assumed to be unblocked.
  156. */
  157. int __must_check rfkill_register(struct rfkill *rfkill);
  158. /**
  159. * rfkill_pause_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_pause_polling(struct rfkill *rfkill);
  166. /**
  167. * rfkill_resume_polling(struct rfkill *rfkill)
  168. *
  169. * Pause polling -- say transmitter is off for other reasons.
  170. * NOTE: not necessary for suspend/resume -- in that case the
  171. * core stops polling anyway
  172. */
  173. void rfkill_resume_polling(struct rfkill *rfkill);
  174. /**
  175. * rfkill_unregister - Unregister a rfkill structure.
  176. * @rfkill: rfkill structure to be unregistered
  177. *
  178. * This function should be called by the network driver during device
  179. * teardown to destroy rfkill structure. Until it returns, the driver
  180. * needs to be able to service method calls.
  181. */
  182. void rfkill_unregister(struct rfkill *rfkill);
  183. /**
  184. * rfkill_destroy - free rfkill structure
  185. * @rfkill: rfkill structure to be destroyed
  186. *
  187. * Destroys the rfkill structure.
  188. */
  189. void rfkill_destroy(struct rfkill *rfkill);
  190. /**
  191. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  192. * @rfkill: pointer to the rfkill class to modify.
  193. * @state: the current hardware block state to set
  194. *
  195. * rfkill drivers that get events when the hard-blocked state changes
  196. * use this function to notify the rfkill core (and through that also
  197. * userspace) of the current state. They should also use this after
  198. * resume if the state could have changed.
  199. *
  200. * You need not (but may) call this function if poll_state is assigned.
  201. *
  202. * This function can be called in any context, even from within rfkill
  203. * callbacks.
  204. *
  205. * The function returns the combined block state (true if transmitter
  206. * should be blocked) so that drivers need not keep track of the soft
  207. * block state -- which they might not be able to.
  208. */
  209. bool __must_check rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
  210. /**
  211. * rfkill_set_sw_state - Set the internal rfkill software block state
  212. * @rfkill: pointer to the rfkill class to modify.
  213. * @state: the current software block state to set
  214. *
  215. * rfkill drivers that get events when the soft-blocked state changes
  216. * (yes, some platforms directly act on input but allow changing again)
  217. * use this function to notify the rfkill core (and through that also
  218. * userspace) of the current state.
  219. *
  220. * Drivers should also call this function after resume if the state has
  221. * been changed by the user. This only makes sense for "persistent"
  222. * devices (see rfkill_init_sw_state()).
  223. *
  224. * This function can be called in any context, even from within rfkill
  225. * callbacks.
  226. *
  227. * The function returns the combined block state (true if transmitter
  228. * should be blocked).
  229. */
  230. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  231. /**
  232. * rfkill_init_sw_state - Initialize persistent software block state
  233. * @rfkill: pointer to the rfkill class to modify.
  234. * @state: the current software block state to set
  235. *
  236. * rfkill drivers that preserve their software block state over power off
  237. * use this function to notify the rfkill core (and through that also
  238. * userspace) of their initial state. It should only be used before
  239. * registration.
  240. *
  241. * In addition, it marks the device as "persistent", an attribute which
  242. * can be read by userspace. Persistent devices are expected to preserve
  243. * their own state when suspended.
  244. */
  245. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
  246. /**
  247. * rfkill_set_states - Set the internal rfkill block states
  248. * @rfkill: pointer to the rfkill class to modify.
  249. * @sw: the current software block state to set
  250. * @hw: the current hardware block state to set
  251. *
  252. * This function can be called in any context, even from within rfkill
  253. * callbacks.
  254. */
  255. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  256. /**
  257. * rfkill_blocked - query rfkill block
  258. *
  259. * @rfkill: rfkill struct to query
  260. */
  261. bool rfkill_blocked(struct rfkill *rfkill);
  262. #else /* !RFKILL */
  263. static inline struct rfkill * __must_check
  264. rfkill_alloc(const char *name,
  265. struct device *parent,
  266. const enum rfkill_type type,
  267. const struct rfkill_ops *ops,
  268. void *ops_data)
  269. {
  270. return ERR_PTR(-ENODEV);
  271. }
  272. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  273. {
  274. if (rfkill == ERR_PTR(-ENODEV))
  275. return 0;
  276. return -EINVAL;
  277. }
  278. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  279. {
  280. }
  281. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  282. {
  283. }
  284. static inline void rfkill_unregister(struct rfkill *rfkill)
  285. {
  286. }
  287. static inline void rfkill_destroy(struct rfkill *rfkill)
  288. {
  289. }
  290. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  291. {
  292. return blocked;
  293. }
  294. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  295. {
  296. return blocked;
  297. }
  298. static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  299. {
  300. }
  301. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  302. {
  303. }
  304. static inline bool rfkill_blocked(struct rfkill *rfkill)
  305. {
  306. return false;
  307. }
  308. #endif /* RFKILL || RFKILL_MODULE */
  309. #ifdef CONFIG_RFKILL_LEDS
  310. /**
  311. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  312. * This function might return a NULL pointer if registering of the
  313. * LED trigger failed. Use this as "default_trigger" for the LED.
  314. */
  315. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  316. /**
  317. * rfkill_set_led_trigger_name -- set the LED trigger name
  318. * @rfkill: rfkill struct
  319. * @name: LED trigger name
  320. *
  321. * This function sets the LED trigger name of the radio LED
  322. * trigger that rfkill creates. It is optional, but if called
  323. * must be called before rfkill_register() to be effective.
  324. */
  325. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  326. #else
  327. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  328. {
  329. return NULL;
  330. }
  331. static inline void
  332. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  333. {
  334. }
  335. #endif
  336. #endif /* __KERNEL__ */
  337. #endif /* RFKILL_H */