rfkill.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2006 - 2007 Ivo van Doorn
  3. * Copyright (C) 2007 Dmitry Torokhov
  4. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef __RFKILL_H
  19. #define __RFKILL_H
  20. #include <uapi/linux/rfkill.h>
  21. /* don't allow anyone to use these in the kernel */
  22. enum rfkill_user_states {
  23. RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
  24. RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
  25. RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
  26. };
  27. #undef RFKILL_STATE_SOFT_BLOCKED
  28. #undef RFKILL_STATE_UNBLOCKED
  29. #undef RFKILL_STATE_HARD_BLOCKED
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/mutex.h>
  33. #include <linux/leds.h>
  34. #include <linux/err.h>
  35. struct device;
  36. /* this is opaque */
  37. struct rfkill;
  38. /**
  39. * struct rfkill_ops - rfkill driver methods
  40. *
  41. * @poll: poll the rfkill block state(s) -- only assign this method
  42. * when you need polling. When called, simply call one of the
  43. * rfkill_set{,_hw,_sw}_state family of functions. If the hw
  44. * is getting unblocked you need to take into account the return
  45. * value of those functions to make sure the software block is
  46. * properly used.
  47. * @query: query the rfkill block state(s) and call exactly one of the
  48. * rfkill_set{,_hw,_sw}_state family of functions. Assign this
  49. * method if input events can cause hardware state changes to make
  50. * the rfkill core query your driver before setting a requested
  51. * block.
  52. * @set_block: turn the transmitter on (blocked == false) or off
  53. * (blocked == true) -- ignore and return 0 when hard blocked.
  54. * This callback must be assigned.
  55. */
  56. struct rfkill_ops {
  57. void (*poll)(struct rfkill *rfkill, void *data);
  58. void (*query)(struct rfkill *rfkill, void *data);
  59. int (*set_block)(void *data, bool blocked);
  60. };
  61. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  62. /**
  63. * rfkill_alloc - allocate rfkill structure
  64. * @name: name of the struct -- the string is not copied internally
  65. * @parent: device that has rf switch on it
  66. * @type: type of the switch (RFKILL_TYPE_*)
  67. * @ops: rfkill methods
  68. * @ops_data: data passed to each method
  69. *
  70. * This function should be called by the transmitter driver to allocate an
  71. * rfkill structure. Returns %NULL on failure.
  72. */
  73. struct rfkill * __must_check rfkill_alloc(const char *name,
  74. struct device *parent,
  75. const enum rfkill_type type,
  76. const struct rfkill_ops *ops,
  77. void *ops_data);
  78. /**
  79. * rfkill_register - Register a rfkill structure.
  80. * @rfkill: rfkill structure to be registered
  81. *
  82. * This function should be called by the transmitter driver to register
  83. * the rfkill structure. Before calling this function the driver needs
  84. * to be ready to service method calls from rfkill.
  85. *
  86. * If rfkill_init_sw_state() is not called before registration,
  87. * set_block() will be called to initialize the software blocked state
  88. * to a default value.
  89. *
  90. * If the hardware blocked state is not set before registration,
  91. * it is assumed to be unblocked.
  92. */
  93. int __must_check rfkill_register(struct rfkill *rfkill);
  94. /**
  95. * rfkill_pause_polling(struct rfkill *rfkill)
  96. *
  97. * Pause polling -- say transmitter is off for other reasons.
  98. * NOTE: not necessary for suspend/resume -- in that case the
  99. * core stops polling anyway
  100. */
  101. void rfkill_pause_polling(struct rfkill *rfkill);
  102. /**
  103. * rfkill_resume_polling(struct rfkill *rfkill)
  104. *
  105. * Pause polling -- say transmitter is off for other reasons.
  106. * NOTE: not necessary for suspend/resume -- in that case the
  107. * core stops polling anyway
  108. */
  109. void rfkill_resume_polling(struct rfkill *rfkill);
  110. /**
  111. * rfkill_unregister - Unregister a rfkill structure.
  112. * @rfkill: rfkill structure to be unregistered
  113. *
  114. * This function should be called by the network driver during device
  115. * teardown to destroy rfkill structure. Until it returns, the driver
  116. * needs to be able to service method calls.
  117. */
  118. void rfkill_unregister(struct rfkill *rfkill);
  119. /**
  120. * rfkill_destroy - free rfkill structure
  121. * @rfkill: rfkill structure to be destroyed
  122. *
  123. * Destroys the rfkill structure.
  124. */
  125. void rfkill_destroy(struct rfkill *rfkill);
  126. /**
  127. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  128. * @rfkill: pointer to the rfkill class to modify.
  129. * @state: the current hardware block state to set
  130. *
  131. * rfkill drivers that get events when the hard-blocked state changes
  132. * use this function to notify the rfkill core (and through that also
  133. * userspace) of the current state. They should also use this after
  134. * resume if the state could have changed.
  135. *
  136. * You need not (but may) call this function if poll_state is assigned.
  137. *
  138. * This function can be called in any context, even from within rfkill
  139. * callbacks.
  140. *
  141. * The function returns the combined block state (true if transmitter
  142. * should be blocked) so that drivers need not keep track of the soft
  143. * block state -- which they might not be able to.
  144. */
  145. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
  146. /**
  147. * rfkill_set_sw_state - Set the internal rfkill software block state
  148. * @rfkill: pointer to the rfkill class to modify.
  149. * @state: the current software block state to set
  150. *
  151. * rfkill drivers that get events when the soft-blocked state changes
  152. * (yes, some platforms directly act on input but allow changing again)
  153. * use this function to notify the rfkill core (and through that also
  154. * userspace) of the current state.
  155. *
  156. * Drivers should also call this function after resume if the state has
  157. * been changed by the user. This only makes sense for "persistent"
  158. * devices (see rfkill_init_sw_state()).
  159. *
  160. * This function can be called in any context, even from within rfkill
  161. * callbacks.
  162. *
  163. * The function returns the combined block state (true if transmitter
  164. * should be blocked).
  165. */
  166. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  167. /**
  168. * rfkill_init_sw_state - Initialize persistent software block state
  169. * @rfkill: pointer to the rfkill class to modify.
  170. * @state: the current software block state to set
  171. *
  172. * rfkill drivers that preserve their software block state over power off
  173. * use this function to notify the rfkill core (and through that also
  174. * userspace) of their initial state. It should only be used before
  175. * registration.
  176. *
  177. * In addition, it marks the device as "persistent", an attribute which
  178. * can be read by userspace. Persistent devices are expected to preserve
  179. * their own state when suspended.
  180. */
  181. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
  182. /**
  183. * rfkill_set_states - Set the internal rfkill block states
  184. * @rfkill: pointer to the rfkill class to modify.
  185. * @sw: the current software block state to set
  186. * @hw: the current hardware block state to set
  187. *
  188. * This function can be called in any context, even from within rfkill
  189. * callbacks.
  190. */
  191. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  192. /**
  193. * rfkill_blocked - query rfkill block
  194. *
  195. * @rfkill: rfkill struct to query
  196. */
  197. bool rfkill_blocked(struct rfkill *rfkill);
  198. #else /* !RFKILL */
  199. static inline struct rfkill * __must_check
  200. rfkill_alloc(const char *name,
  201. struct device *parent,
  202. const enum rfkill_type type,
  203. const struct rfkill_ops *ops,
  204. void *ops_data)
  205. {
  206. return ERR_PTR(-ENODEV);
  207. }
  208. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  209. {
  210. if (rfkill == ERR_PTR(-ENODEV))
  211. return 0;
  212. return -EINVAL;
  213. }
  214. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  215. {
  216. }
  217. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  218. {
  219. }
  220. static inline void rfkill_unregister(struct rfkill *rfkill)
  221. {
  222. }
  223. static inline void rfkill_destroy(struct rfkill *rfkill)
  224. {
  225. }
  226. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  227. {
  228. return blocked;
  229. }
  230. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  231. {
  232. return blocked;
  233. }
  234. static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  235. {
  236. }
  237. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  238. {
  239. }
  240. static inline bool rfkill_blocked(struct rfkill *rfkill)
  241. {
  242. return false;
  243. }
  244. #endif /* RFKILL || RFKILL_MODULE */
  245. #ifdef CONFIG_RFKILL_LEDS
  246. /**
  247. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  248. * This function might return a NULL pointer if registering of the
  249. * LED trigger failed. Use this as "default_trigger" for the LED.
  250. */
  251. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  252. /**
  253. * rfkill_set_led_trigger_name -- set the LED trigger name
  254. * @rfkill: rfkill struct
  255. * @name: LED trigger name
  256. *
  257. * This function sets the LED trigger name of the radio LED
  258. * trigger that rfkill creates. It is optional, but if called
  259. * must be called before rfkill_register() to be effective.
  260. */
  261. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  262. #else
  263. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  264. {
  265. return NULL;
  266. }
  267. static inline void
  268. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  269. {
  270. }
  271. #endif
  272. #endif /* RFKILL_H */