rfkill.h 12 KB

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