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