rfkill.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. * Permission to use, copy, modify, and/or distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. #include <linux/types.h>
  21. /* define userspace visible states */
  22. #define RFKILL_STATE_SOFT_BLOCKED 0
  23. #define RFKILL_STATE_UNBLOCKED 1
  24. #define RFKILL_STATE_HARD_BLOCKED 2
  25. /**
  26. * enum rfkill_type - type of rfkill switch.
  27. *
  28. * @RFKILL_TYPE_ALL: toggles all switches (userspace only)
  29. * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device.
  30. * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device.
  31. * @RFKILL_TYPE_UWB: switch is on a ultra wideband device.
  32. * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device.
  33. * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device.
  34. * @NUM_RFKILL_TYPES: number of defined rfkill types
  35. */
  36. enum rfkill_type {
  37. RFKILL_TYPE_ALL = 0,
  38. RFKILL_TYPE_WLAN,
  39. RFKILL_TYPE_BLUETOOTH,
  40. RFKILL_TYPE_UWB,
  41. RFKILL_TYPE_WIMAX,
  42. RFKILL_TYPE_WWAN,
  43. RFKILL_TYPE_GPS,
  44. NUM_RFKILL_TYPES,
  45. };
  46. /**
  47. * enum rfkill_operation - operation types
  48. * @RFKILL_OP_ADD: a device was added
  49. * @RFKILL_OP_DEL: a device was removed
  50. * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
  51. * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
  52. */
  53. enum rfkill_operation {
  54. RFKILL_OP_ADD = 0,
  55. RFKILL_OP_DEL,
  56. RFKILL_OP_CHANGE,
  57. RFKILL_OP_CHANGE_ALL,
  58. };
  59. /**
  60. * struct rfkill_event - events for userspace on /dev/rfkill
  61. * @idx: index of dev rfkill
  62. * @type: type of the rfkill struct
  63. * @op: operation code
  64. * @hard: hard state (0/1)
  65. * @soft: soft state (0/1)
  66. *
  67. * Structure used for userspace communication on /dev/rfkill,
  68. * used for events from the kernel and control to the kernel.
  69. */
  70. struct rfkill_event {
  71. __u32 idx;
  72. __u8 type;
  73. __u8 op;
  74. __u8 soft, hard;
  75. } __packed;
  76. /*
  77. * We are planning to be backward and forward compatible with changes
  78. * to the event struct, by adding new, optional, members at the end.
  79. * When reading an event (whether the kernel from userspace or vice
  80. * versa) we need to accept anything that's at least as large as the
  81. * version 1 event size, but might be able to accept other sizes in
  82. * the future.
  83. *
  84. * One exception is the kernel -- we already have two event sizes in
  85. * that we've made the 'hard' member optional since our only option
  86. * is to ignore it anyway.
  87. */
  88. #define RFKILL_EVENT_SIZE_V1 8
  89. /* ioctl for turning off rfkill-input (if present) */
  90. #define RFKILL_IOC_MAGIC 'R'
  91. #define RFKILL_IOC_NOINPUT 1
  92. #define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
  93. /* and that's all userspace gets */
  94. #ifdef __KERNEL__
  95. /* don't allow anyone to use these in the kernel */
  96. enum rfkill_user_states {
  97. RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
  98. RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
  99. RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
  100. };
  101. #undef RFKILL_STATE_SOFT_BLOCKED
  102. #undef RFKILL_STATE_UNBLOCKED
  103. #undef RFKILL_STATE_HARD_BLOCKED
  104. #include <linux/kernel.h>
  105. #include <linux/list.h>
  106. #include <linux/mutex.h>
  107. #include <linux/device.h>
  108. #include <linux/leds.h>
  109. #include <linux/err.h>
  110. /* this is opaque */
  111. struct rfkill;
  112. /**
  113. * struct rfkill_ops - rfkill driver methods
  114. *
  115. * @poll: poll the rfkill block state(s) -- only assign this method
  116. * when you need polling. When called, simply call one of the
  117. * rfkill_set{,_hw,_sw}_state family of functions. If the hw
  118. * is getting unblocked you need to take into account the return
  119. * value of those functions to make sure the software block is
  120. * properly used.
  121. * @query: query the rfkill block state(s) and call exactly one of the
  122. * rfkill_set{,_hw,_sw}_state family of functions. Assign this
  123. * method if input events can cause hardware state changes to make
  124. * the rfkill core query your driver before setting a requested
  125. * block.
  126. * @set_block: turn the transmitter on (blocked == false) or off
  127. * (blocked == true) -- ignore and return 0 when hard blocked.
  128. * This callback must be assigned.
  129. */
  130. struct rfkill_ops {
  131. void (*poll)(struct rfkill *rfkill, void *data);
  132. void (*query)(struct rfkill *rfkill, void *data);
  133. int (*set_block)(void *data, bool blocked);
  134. };
  135. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  136. /**
  137. * rfkill_alloc - allocate rfkill structure
  138. * @name: name of the struct -- the string is not copied internally
  139. * @parent: device that has rf switch on it
  140. * @type: type of the switch (RFKILL_TYPE_*)
  141. * @ops: rfkill methods
  142. * @ops_data: data passed to each method
  143. *
  144. * This function should be called by the transmitter driver to allocate an
  145. * rfkill structure. Returns %NULL on failure.
  146. */
  147. struct rfkill * __must_check rfkill_alloc(const char *name,
  148. struct device *parent,
  149. const enum rfkill_type type,
  150. const struct rfkill_ops *ops,
  151. void *ops_data);
  152. /**
  153. * rfkill_register - Register a rfkill structure.
  154. * @rfkill: rfkill structure to be registered
  155. *
  156. * This function should be called by the transmitter driver to register
  157. * the rfkill structure. Before calling this function the driver needs
  158. * to be ready to service method calls from rfkill.
  159. *
  160. * If rfkill_init_sw_state() is not called before registration,
  161. * set_block() will be called to initialize the software blocked state
  162. * to a default value.
  163. *
  164. * If the hardware blocked state is not set before registration,
  165. * it is assumed to be unblocked.
  166. */
  167. int __must_check rfkill_register(struct rfkill *rfkill);
  168. /**
  169. * rfkill_pause_polling(struct rfkill *rfkill)
  170. *
  171. * Pause polling -- say transmitter is off for other reasons.
  172. * NOTE: not necessary for suspend/resume -- in that case the
  173. * core stops polling anyway
  174. */
  175. void rfkill_pause_polling(struct rfkill *rfkill);
  176. /**
  177. * rfkill_resume_polling(struct rfkill *rfkill)
  178. *
  179. * Pause polling -- say transmitter is off for other reasons.
  180. * NOTE: not necessary for suspend/resume -- in that case the
  181. * core stops polling anyway
  182. */
  183. void rfkill_resume_polling(struct rfkill *rfkill);
  184. /**
  185. * rfkill_unregister - Unregister a rfkill structure.
  186. * @rfkill: rfkill structure to be unregistered
  187. *
  188. * This function should be called by the network driver during device
  189. * teardown to destroy rfkill structure. Until it returns, the driver
  190. * needs to be able to service method calls.
  191. */
  192. void rfkill_unregister(struct rfkill *rfkill);
  193. /**
  194. * rfkill_destroy - free rfkill structure
  195. * @rfkill: rfkill structure to be destroyed
  196. *
  197. * Destroys the rfkill structure.
  198. */
  199. void rfkill_destroy(struct rfkill *rfkill);
  200. /**
  201. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  202. * @rfkill: pointer to the rfkill class to modify.
  203. * @state: the current hardware block state to set
  204. *
  205. * rfkill drivers that get events when the hard-blocked state changes
  206. * use this function to notify the rfkill core (and through that also
  207. * userspace) of the current state. They should also use this after
  208. * resume if the state could have changed.
  209. *
  210. * You need not (but may) call this function if poll_state is assigned.
  211. *
  212. * This function can be called in any context, even from within rfkill
  213. * callbacks.
  214. *
  215. * The function returns the combined block state (true if transmitter
  216. * should be blocked) so that drivers need not keep track of the soft
  217. * block state -- which they might not be able to.
  218. */
  219. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
  220. /**
  221. * rfkill_set_sw_state - Set the internal rfkill software block state
  222. * @rfkill: pointer to the rfkill class to modify.
  223. * @state: the current software block state to set
  224. *
  225. * rfkill drivers that get events when the soft-blocked state changes
  226. * (yes, some platforms directly act on input but allow changing again)
  227. * use this function to notify the rfkill core (and through that also
  228. * userspace) of the current state.
  229. *
  230. * Drivers should also call this function after resume if the state has
  231. * been changed by the user. This only makes sense for "persistent"
  232. * devices (see rfkill_init_sw_state()).
  233. *
  234. * This function can be called in any context, even from within rfkill
  235. * callbacks.
  236. *
  237. * The function returns the combined block state (true if transmitter
  238. * should be blocked).
  239. */
  240. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  241. /**
  242. * rfkill_init_sw_state - Initialize persistent software block state
  243. * @rfkill: pointer to the rfkill class to modify.
  244. * @state: the current software block state to set
  245. *
  246. * rfkill drivers that preserve their software block state over power off
  247. * use this function to notify the rfkill core (and through that also
  248. * userspace) of their initial state. It should only be used before
  249. * registration.
  250. *
  251. * In addition, it marks the device as "persistent", an attribute which
  252. * can be read by userspace. Persistent devices are expected to preserve
  253. * their own state when suspended.
  254. */
  255. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
  256. /**
  257. * rfkill_set_states - Set the internal rfkill block states
  258. * @rfkill: pointer to the rfkill class to modify.
  259. * @sw: the current software block state to set
  260. * @hw: the current hardware block state to set
  261. *
  262. * This function can be called in any context, even from within rfkill
  263. * callbacks.
  264. */
  265. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  266. /**
  267. * rfkill_blocked - query rfkill block
  268. *
  269. * @rfkill: rfkill struct to query
  270. */
  271. bool rfkill_blocked(struct rfkill *rfkill);
  272. #else /* !RFKILL */
  273. static inline struct rfkill * __must_check
  274. rfkill_alloc(const char *name,
  275. struct device *parent,
  276. const enum rfkill_type type,
  277. const struct rfkill_ops *ops,
  278. void *ops_data)
  279. {
  280. return ERR_PTR(-ENODEV);
  281. }
  282. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  283. {
  284. if (rfkill == ERR_PTR(-ENODEV))
  285. return 0;
  286. return -EINVAL;
  287. }
  288. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  289. {
  290. }
  291. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  292. {
  293. }
  294. static inline void rfkill_unregister(struct rfkill *rfkill)
  295. {
  296. }
  297. static inline void rfkill_destroy(struct rfkill *rfkill)
  298. {
  299. }
  300. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  301. {
  302. return blocked;
  303. }
  304. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  305. {
  306. return blocked;
  307. }
  308. static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  309. {
  310. }
  311. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  312. {
  313. }
  314. static inline bool rfkill_blocked(struct rfkill *rfkill)
  315. {
  316. return false;
  317. }
  318. #endif /* RFKILL || RFKILL_MODULE */
  319. #ifdef CONFIG_RFKILL_LEDS
  320. /**
  321. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  322. * This function might return a NULL pointer if registering of the
  323. * LED trigger failed. Use this as "default_trigger" for the LED.
  324. */
  325. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  326. /**
  327. * rfkill_set_led_trigger_name -- set the LED trigger name
  328. * @rfkill: rfkill struct
  329. * @name: LED trigger name
  330. *
  331. * This function sets the LED trigger name of the radio LED
  332. * trigger that rfkill creates. It is optional, but if called
  333. * must be called before rfkill_register() to be effective.
  334. */
  335. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  336. #else
  337. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  338. {
  339. return NULL;
  340. }
  341. static inline void
  342. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  343. {
  344. }
  345. #endif
  346. #endif /* __KERNEL__ */
  347. #endif /* RFKILL_H */