rfkill.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/kernel.h>
  94. #include <linux/list.h>
  95. #include <linux/mutex.h>
  96. #include <linux/device.h>
  97. #include <linux/leds.h>
  98. #include <linux/err.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. Before calling this function the driver needs
  147. * to be ready to service method calls from rfkill.
  148. *
  149. * If rfkill_init_sw_state() is not called before registration,
  150. * set_block() will be called to initialize the software blocked state
  151. * 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.
  218. *
  219. * Drivers should also call this function after resume if the state has
  220. * been changed by the user. This only makes sense for "persistent"
  221. * devices (see rfkill_init_sw_state()).
  222. *
  223. * This function can be called in any context, even from within rfkill
  224. * callbacks.
  225. *
  226. * The function returns the combined block state (true if transmitter
  227. * should be blocked).
  228. */
  229. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  230. /**
  231. * rfkill_init_sw_state - Initialize persistent software block state
  232. * @rfkill: pointer to the rfkill class to modify.
  233. * @state: the current software block state to set
  234. *
  235. * rfkill drivers that preserve their software block state over power off
  236. * use this function to notify the rfkill core (and through that also
  237. * userspace) of their initial state. It should only be used before
  238. * registration.
  239. *
  240. * In addition, it marks the device as "persistent", an attribute which
  241. * can be read by userspace. Persistent devices are expected to preserve
  242. * their own state when suspended.
  243. */
  244. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
  245. /**
  246. * rfkill_set_states - Set the internal rfkill block states
  247. * @rfkill: pointer to the rfkill class to modify.
  248. * @sw: the current software block state to set
  249. * @hw: the current hardware block state to set
  250. *
  251. * This function can be called in any context, even from within rfkill
  252. * callbacks.
  253. */
  254. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  255. /**
  256. * rfkill_blocked - query rfkill block
  257. *
  258. * @rfkill: rfkill struct to query
  259. */
  260. bool rfkill_blocked(struct rfkill *rfkill);
  261. #else /* !RFKILL */
  262. static inline struct rfkill * __must_check
  263. rfkill_alloc(const char *name,
  264. struct device *parent,
  265. const enum rfkill_type type,
  266. const struct rfkill_ops *ops,
  267. void *ops_data)
  268. {
  269. return ERR_PTR(-ENODEV);
  270. }
  271. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  272. {
  273. if (rfkill == ERR_PTR(-ENODEV))
  274. return 0;
  275. return -EINVAL;
  276. }
  277. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  278. {
  279. }
  280. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  281. {
  282. }
  283. static inline void rfkill_unregister(struct rfkill *rfkill)
  284. {
  285. }
  286. static inline void rfkill_destroy(struct rfkill *rfkill)
  287. {
  288. }
  289. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  290. {
  291. return blocked;
  292. }
  293. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  294. {
  295. return blocked;
  296. }
  297. static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  298. {
  299. }
  300. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  301. {
  302. }
  303. static inline bool rfkill_blocked(struct rfkill *rfkill)
  304. {
  305. return false;
  306. }
  307. #endif /* RFKILL || RFKILL_MODULE */
  308. #ifdef CONFIG_RFKILL_LEDS
  309. /**
  310. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  311. * This function might return a NULL pointer if registering of the
  312. * LED trigger failed. Use this as "default_trigger" for the LED.
  313. */
  314. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  315. /**
  316. * rfkill_set_led_trigger_name -- set the LED trigger name
  317. * @rfkill: rfkill struct
  318. * @name: LED trigger name
  319. *
  320. * This function sets the LED trigger name of the radio LED
  321. * trigger that rfkill creates. It is optional, but if called
  322. * must be called before rfkill_register() to be effective.
  323. */
  324. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  325. #else
  326. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  327. {
  328. return NULL;
  329. }
  330. static inline void
  331. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  332. {
  333. }
  334. #endif
  335. #endif /* __KERNEL__ */
  336. #endif /* RFKILL_H */