rfkill.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 (but will also correctly handle
  100. * the case of polling having been paused before suspend.)
  101. */
  102. void rfkill_pause_polling(struct rfkill *rfkill);
  103. /**
  104. * rfkill_resume_polling(struct rfkill *rfkill)
  105. *
  106. * Resume polling
  107. * NOTE: not necessary for suspend/resume -- in that case the
  108. * core stops polling anyway
  109. */
  110. void rfkill_resume_polling(struct rfkill *rfkill);
  111. /**
  112. * rfkill_unregister - Unregister a rfkill structure.
  113. * @rfkill: rfkill structure to be unregistered
  114. *
  115. * This function should be called by the network driver during device
  116. * teardown to destroy rfkill structure. Until it returns, the driver
  117. * needs to be able to service method calls.
  118. */
  119. void rfkill_unregister(struct rfkill *rfkill);
  120. /**
  121. * rfkill_destroy - Free rfkill structure
  122. * @rfkill: rfkill structure to be destroyed
  123. *
  124. * Destroys the rfkill structure.
  125. */
  126. void rfkill_destroy(struct rfkill *rfkill);
  127. /**
  128. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  129. * @rfkill: pointer to the rfkill class to modify.
  130. * @blocked: the current hardware block state to set
  131. *
  132. * rfkill drivers that get events when the hard-blocked state changes
  133. * use this function to notify the rfkill core (and through that also
  134. * userspace) of the current state. They should also use this after
  135. * resume if the state could have changed.
  136. *
  137. * You need not (but may) call this function if poll_state is assigned.
  138. *
  139. * This function can be called in any context, even from within rfkill
  140. * callbacks.
  141. *
  142. * The function returns the combined block state (true if transmitter
  143. * should be blocked) so that drivers need not keep track of the soft
  144. * block state -- which they might not be able to.
  145. */
  146. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
  147. /**
  148. * rfkill_set_sw_state - Set the internal rfkill software block state
  149. * @rfkill: pointer to the rfkill class to modify.
  150. * @blocked: the current software block state to set
  151. *
  152. * rfkill drivers that get events when the soft-blocked state changes
  153. * (yes, some platforms directly act on input but allow changing again)
  154. * use this function to notify the rfkill core (and through that also
  155. * userspace) of the current state.
  156. *
  157. * Drivers should also call this function after resume if the state has
  158. * been changed by the user. This only makes sense for "persistent"
  159. * devices (see rfkill_init_sw_state()).
  160. *
  161. * This function can be called in any context, even from within rfkill
  162. * callbacks.
  163. *
  164. * The function returns the combined block state (true if transmitter
  165. * should be blocked).
  166. */
  167. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  168. /**
  169. * rfkill_init_sw_state - Initialize persistent software block state
  170. * @rfkill: pointer to the rfkill class to modify.
  171. * @blocked: the current software block state to set
  172. *
  173. * rfkill drivers that preserve their software block state over power off
  174. * use this function to notify the rfkill core (and through that also
  175. * userspace) of their initial state. It should only be used before
  176. * registration.
  177. *
  178. * In addition, it marks the device as "persistent", an attribute which
  179. * can be read by userspace. Persistent devices are expected to preserve
  180. * their own state when suspended.
  181. */
  182. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
  183. /**
  184. * rfkill_set_states - Set the internal rfkill block states
  185. * @rfkill: pointer to the rfkill class to modify.
  186. * @sw: the current software block state to set
  187. * @hw: the current hardware block state to set
  188. *
  189. * This function can be called in any context, even from within rfkill
  190. * callbacks.
  191. */
  192. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  193. /**
  194. * rfkill_blocked - Query rfkill block state
  195. *
  196. * @rfkill: rfkill struct to query
  197. */
  198. bool rfkill_blocked(struct rfkill *rfkill);
  199. /**
  200. * rfkill_find_type - Helper for finding rfkill type by name
  201. * @name: the name of the type
  202. *
  203. * Returns enum rfkill_type that corresponds to the name.
  204. */
  205. enum rfkill_type rfkill_find_type(const char *name);
  206. #else /* !RFKILL */
  207. static inline struct rfkill * __must_check
  208. rfkill_alloc(const char *name,
  209. struct device *parent,
  210. const enum rfkill_type type,
  211. const struct rfkill_ops *ops,
  212. void *ops_data)
  213. {
  214. return ERR_PTR(-ENODEV);
  215. }
  216. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  217. {
  218. if (rfkill == ERR_PTR(-ENODEV))
  219. return 0;
  220. return -EINVAL;
  221. }
  222. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  223. {
  224. }
  225. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  226. {
  227. }
  228. static inline void rfkill_unregister(struct rfkill *rfkill)
  229. {
  230. }
  231. static inline void rfkill_destroy(struct rfkill *rfkill)
  232. {
  233. }
  234. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  235. {
  236. return blocked;
  237. }
  238. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  239. {
  240. return blocked;
  241. }
  242. static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  243. {
  244. }
  245. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  246. {
  247. }
  248. static inline bool rfkill_blocked(struct rfkill *rfkill)
  249. {
  250. return false;
  251. }
  252. static inline enum rfkill_type rfkill_find_type(const char *name)
  253. {
  254. return RFKILL_TYPE_ALL;
  255. }
  256. #endif /* RFKILL || RFKILL_MODULE */
  257. #ifdef CONFIG_RFKILL_LEDS
  258. /**
  259. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  260. * This function might return a NULL pointer if registering of the
  261. * LED trigger failed. Use this as "default_trigger" for the LED.
  262. */
  263. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  264. /**
  265. * rfkill_set_led_trigger_name - Set the LED trigger name
  266. * @rfkill: rfkill struct
  267. * @name: LED trigger name
  268. *
  269. * This function sets the LED trigger name of the radio LED
  270. * trigger that rfkill creates. It is optional, but if called
  271. * must be called before rfkill_register() to be effective.
  272. */
  273. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  274. #else
  275. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  276. {
  277. return NULL;
  278. }
  279. static inline void
  280. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  281. {
  282. }
  283. #endif
  284. #endif /* RFKILL_H */