rfkill.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) 2006 - 2007 Ivo van Doorn
  3. * Copyright (C) 2007 Dmitry Torokhov
  4. * Copyright 2009 Johannes Berg <[email protected]>
  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_reason - Set the internal rfkill hardware block state
  129. * with a reason
  130. * @rfkill: pointer to the rfkill class to modify.
  131. * @blocked: the current hardware block state to set
  132. * @reason: one of &enum rfkill_hard_block_reasons
  133. *
  134. * Prefer to use rfkill_set_hw_state if you don't need any special reason.
  135. */
  136. bool rfkill_set_hw_state_reason(struct rfkill *rfkill,
  137. bool blocked, unsigned long reason);
  138. /**
  139. * rfkill_set_hw_state - Set the internal rfkill hardware block state
  140. * @rfkill: pointer to the rfkill class to modify.
  141. * @blocked: the current hardware block state to set
  142. *
  143. * rfkill drivers that get events when the hard-blocked state changes
  144. * use this function to notify the rfkill core (and through that also
  145. * userspace) of the current state. They should also use this after
  146. * resume if the state could have changed.
  147. *
  148. * You need not (but may) call this function if poll_state is assigned.
  149. *
  150. * This function can be called in any context, even from within rfkill
  151. * callbacks.
  152. *
  153. * The function returns the combined block state (true if transmitter
  154. * should be blocked) so that drivers need not keep track of the soft
  155. * block state -- which they might not be able to.
  156. */
  157. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  158. {
  159. return rfkill_set_hw_state_reason(rfkill, blocked,
  160. RFKILL_HARD_BLOCK_SIGNAL);
  161. }
  162. /**
  163. * rfkill_set_sw_state - Set the internal rfkill software block state
  164. * @rfkill: pointer to the rfkill class to modify.
  165. * @blocked: the current software block state to set
  166. *
  167. * rfkill drivers that get events when the soft-blocked state changes
  168. * (yes, some platforms directly act on input but allow changing again)
  169. * use this function to notify the rfkill core (and through that also
  170. * userspace) of the current state.
  171. *
  172. * Drivers should also call this function after resume if the state has
  173. * been changed by the user. This only makes sense for "persistent"
  174. * devices (see rfkill_init_sw_state()).
  175. *
  176. * This function can be called in any context, even from within rfkill
  177. * callbacks.
  178. *
  179. * The function returns the combined block state (true if transmitter
  180. * should be blocked).
  181. */
  182. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
  183. /**
  184. * rfkill_init_sw_state - Initialize persistent software block state
  185. * @rfkill: pointer to the rfkill class to modify.
  186. * @blocked: the current software block state to set
  187. *
  188. * rfkill drivers that preserve their software block state over power off
  189. * use this function to notify the rfkill core (and through that also
  190. * userspace) of their initial state. It should only be used before
  191. * registration.
  192. *
  193. * In addition, it marks the device as "persistent", an attribute which
  194. * can be read by userspace. Persistent devices are expected to preserve
  195. * their own state when suspended.
  196. */
  197. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
  198. /**
  199. * rfkill_set_states - Set the internal rfkill block states
  200. * @rfkill: pointer to the rfkill class to modify.
  201. * @sw: the current software block state to set
  202. * @hw: the current hardware block state to set
  203. *
  204. * This function can be called in any context, even from within rfkill
  205. * callbacks.
  206. */
  207. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
  208. /**
  209. * rfkill_blocked - Query rfkill block state
  210. *
  211. * @rfkill: rfkill struct to query
  212. */
  213. bool rfkill_blocked(struct rfkill *rfkill);
  214. /**
  215. * rfkill_soft_blocked - Query soft rfkill block state
  216. *
  217. * @rfkill: rfkill struct to query
  218. */
  219. bool rfkill_soft_blocked(struct rfkill *rfkill);
  220. /**
  221. * rfkill_find_type - Helper for finding rfkill type by name
  222. * @name: the name of the type
  223. *
  224. * Returns enum rfkill_type that corresponds to the name.
  225. */
  226. enum rfkill_type rfkill_find_type(const char *name);
  227. #else /* !RFKILL */
  228. static inline struct rfkill * __must_check
  229. rfkill_alloc(const char *name,
  230. struct device *parent,
  231. const enum rfkill_type type,
  232. const struct rfkill_ops *ops,
  233. void *ops_data)
  234. {
  235. return ERR_PTR(-ENODEV);
  236. }
  237. static inline int __must_check rfkill_register(struct rfkill *rfkill)
  238. {
  239. if (rfkill == ERR_PTR(-ENODEV))
  240. return 0;
  241. return -EINVAL;
  242. }
  243. static inline void rfkill_pause_polling(struct rfkill *rfkill)
  244. {
  245. }
  246. static inline void rfkill_resume_polling(struct rfkill *rfkill)
  247. {
  248. }
  249. static inline void rfkill_unregister(struct rfkill *rfkill)
  250. {
  251. }
  252. static inline void rfkill_destroy(struct rfkill *rfkill)
  253. {
  254. }
  255. static inline bool rfkill_set_hw_state_reason(struct rfkill *rfkill,
  256. bool blocked,
  257. unsigned long reason)
  258. {
  259. return blocked;
  260. }
  261. static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  262. {
  263. return blocked;
  264. }
  265. static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  266. {
  267. return blocked;
  268. }
  269. static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  270. {
  271. }
  272. static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  273. {
  274. }
  275. static inline bool rfkill_blocked(struct rfkill *rfkill)
  276. {
  277. return false;
  278. }
  279. static inline bool rfkill_soft_blocked(struct rfkill *rfkill)
  280. {
  281. return false;
  282. }
  283. static inline enum rfkill_type rfkill_find_type(const char *name)
  284. {
  285. return RFKILL_TYPE_ALL;
  286. }
  287. #endif /* RFKILL || RFKILL_MODULE */
  288. #ifdef CONFIG_RFKILL_LEDS
  289. /**
  290. * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
  291. * This function might return a NULL pointer if registering of the
  292. * LED trigger failed. Use this as "default_trigger" for the LED.
  293. */
  294. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
  295. /**
  296. * rfkill_set_led_trigger_name - Set the LED trigger name
  297. * @rfkill: rfkill struct
  298. * @name: LED trigger name
  299. *
  300. * This function sets the LED trigger name of the radio LED
  301. * trigger that rfkill creates. It is optional, but if called
  302. * must be called before rfkill_register() to be effective.
  303. */
  304. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
  305. #else
  306. static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  307. {
  308. return NULL;
  309. }
  310. static inline void
  311. rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  312. {
  313. }
  314. #endif
  315. #endif /* RFKILL_H */