xref: /linux-6.15/include/linux/rfkill.h (revision 27258e44)
1 #ifndef __RFKILL_H
2 #define __RFKILL_H
3 
4 /*
5  * Copyright (C) 2006 - 2007 Ivo van Doorn
6  * Copyright (C) 2007 Dmitry Torokhov
7  * Copyright 2009 Johannes Berg <[email protected]>
8  *
9  * Permission to use, copy, modify, and/or distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <linux/types.h>
23 
24 /* define userspace visible states */
25 #define RFKILL_STATE_SOFT_BLOCKED	0
26 #define RFKILL_STATE_UNBLOCKED		1
27 #define RFKILL_STATE_HARD_BLOCKED	2
28 
29 /**
30  * enum rfkill_type - type of rfkill switch.
31  *
32  * @RFKILL_TYPE_ALL: toggles all switches (userspace only)
33  * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device.
34  * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device.
35  * @RFKILL_TYPE_UWB: switch is on a ultra wideband device.
36  * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device.
37  * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device.
38  * @NUM_RFKILL_TYPES: number of defined rfkill types
39  */
40 enum rfkill_type {
41 	RFKILL_TYPE_ALL = 0,
42 	RFKILL_TYPE_WLAN,
43 	RFKILL_TYPE_BLUETOOTH,
44 	RFKILL_TYPE_UWB,
45 	RFKILL_TYPE_WIMAX,
46 	RFKILL_TYPE_WWAN,
47 	RFKILL_TYPE_GPS,
48 	NUM_RFKILL_TYPES,
49 };
50 
51 /**
52  * enum rfkill_operation - operation types
53  * @RFKILL_OP_ADD: a device was added
54  * @RFKILL_OP_DEL: a device was removed
55  * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
56  * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
57  */
58 enum rfkill_operation {
59 	RFKILL_OP_ADD = 0,
60 	RFKILL_OP_DEL,
61 	RFKILL_OP_CHANGE,
62 	RFKILL_OP_CHANGE_ALL,
63 };
64 
65 /**
66  * struct rfkill_event - events for userspace on /dev/rfkill
67  * @idx: index of dev rfkill
68  * @type: type of the rfkill struct
69  * @op: operation code
70  * @hard: hard state (0/1)
71  * @soft: soft state (0/1)
72  *
73  * Structure used for userspace communication on /dev/rfkill,
74  * used for events from the kernel and control to the kernel.
75  */
76 struct rfkill_event {
77 	__u32 idx;
78 	__u8  type;
79 	__u8  op;
80 	__u8  soft, hard;
81 } __packed;
82 
83 /*
84  * We are planning to be backward and forward compatible with changes
85  * to the event struct, by adding new, optional, members at the end.
86  * When reading an event (whether the kernel from userspace or vice
87  * versa) we need to accept anything that's at least as large as the
88  * version 1 event size, but might be able to accept other sizes in
89  * the future.
90  *
91  * One exception is the kernel -- we already have two event sizes in
92  * that we've made the 'hard' member optional since our only option
93  * is to ignore it anyway.
94  */
95 #define RFKILL_EVENT_SIZE_V1	8
96 
97 /* ioctl for turning off rfkill-input (if present) */
98 #define RFKILL_IOC_MAGIC	'R'
99 #define RFKILL_IOC_NOINPUT	1
100 #define RFKILL_IOCTL_NOINPUT	_IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
101 
102 /* and that's all userspace gets */
103 #ifdef __KERNEL__
104 /* don't allow anyone to use these in the kernel */
105 enum rfkill_user_states {
106 	RFKILL_USER_STATE_SOFT_BLOCKED	= RFKILL_STATE_SOFT_BLOCKED,
107 	RFKILL_USER_STATE_UNBLOCKED	= RFKILL_STATE_UNBLOCKED,
108 	RFKILL_USER_STATE_HARD_BLOCKED	= RFKILL_STATE_HARD_BLOCKED,
109 };
110 #undef RFKILL_STATE_SOFT_BLOCKED
111 #undef RFKILL_STATE_UNBLOCKED
112 #undef RFKILL_STATE_HARD_BLOCKED
113 
114 #include <linux/kernel.h>
115 #include <linux/list.h>
116 #include <linux/mutex.h>
117 #include <linux/device.h>
118 #include <linux/leds.h>
119 #include <linux/err.h>
120 
121 /* this is opaque */
122 struct rfkill;
123 
124 /**
125  * struct rfkill_ops - rfkill driver methods
126  *
127  * @poll: poll the rfkill block state(s) -- only assign this method
128  *	when you need polling. When called, simply call one of the
129  *	rfkill_set{,_hw,_sw}_state family of functions. If the hw
130  *	is getting unblocked you need to take into account the return
131  *	value of those functions to make sure the software block is
132  *	properly used.
133  * @query: query the rfkill block state(s) and call exactly one of the
134  *	rfkill_set{,_hw,_sw}_state family of functions. Assign this
135  *	method if input events can cause hardware state changes to make
136  *	the rfkill core query your driver before setting a requested
137  *	block.
138  * @set_block: turn the transmitter on (blocked == false) or off
139  *	(blocked == true) -- ignore and return 0 when hard blocked.
140  *	This callback must be assigned.
141  */
142 struct rfkill_ops {
143 	void	(*poll)(struct rfkill *rfkill, void *data);
144 	void	(*query)(struct rfkill *rfkill, void *data);
145 	int	(*set_block)(void *data, bool blocked);
146 };
147 
148 #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
149 /**
150  * rfkill_alloc - allocate rfkill structure
151  * @name: name of the struct -- the string is not copied internally
152  * @parent: device that has rf switch on it
153  * @type: type of the switch (RFKILL_TYPE_*)
154  * @ops: rfkill methods
155  * @ops_data: data passed to each method
156  *
157  * This function should be called by the transmitter driver to allocate an
158  * rfkill structure. Returns %NULL on failure.
159  */
160 struct rfkill * __must_check rfkill_alloc(const char *name,
161 					  struct device *parent,
162 					  const enum rfkill_type type,
163 					  const struct rfkill_ops *ops,
164 					  void *ops_data);
165 
166 /**
167  * rfkill_register - Register a rfkill structure.
168  * @rfkill: rfkill structure to be registered
169  *
170  * This function should be called by the transmitter driver to register
171  * the rfkill structure. Before calling this function the driver needs
172  * to be ready to service method calls from rfkill.
173  *
174  * If rfkill_init_sw_state() is not called before registration,
175  * set_block() will be called to initialize the software blocked state
176  * to a default value.
177  *
178  * If the hardware blocked state is not set before registration,
179  * it is assumed to be unblocked.
180  */
181 int __must_check rfkill_register(struct rfkill *rfkill);
182 
183 /**
184  * rfkill_pause_polling(struct rfkill *rfkill)
185  *
186  * Pause polling -- say transmitter is off for other reasons.
187  * NOTE: not necessary for suspend/resume -- in that case the
188  * core stops polling anyway
189  */
190 void rfkill_pause_polling(struct rfkill *rfkill);
191 
192 /**
193  * rfkill_resume_polling(struct rfkill *rfkill)
194  *
195  * Pause polling -- say transmitter is off for other reasons.
196  * NOTE: not necessary for suspend/resume -- in that case the
197  * core stops polling anyway
198  */
199 void rfkill_resume_polling(struct rfkill *rfkill);
200 
201 
202 /**
203  * rfkill_unregister - Unregister a rfkill structure.
204  * @rfkill: rfkill structure to be unregistered
205  *
206  * This function should be called by the network driver during device
207  * teardown to destroy rfkill structure. Until it returns, the driver
208  * needs to be able to service method calls.
209  */
210 void rfkill_unregister(struct rfkill *rfkill);
211 
212 /**
213  * rfkill_destroy - free rfkill structure
214  * @rfkill: rfkill structure to be destroyed
215  *
216  * Destroys the rfkill structure.
217  */
218 void rfkill_destroy(struct rfkill *rfkill);
219 
220 /**
221  * rfkill_set_hw_state - Set the internal rfkill hardware block state
222  * @rfkill: pointer to the rfkill class to modify.
223  * @state: the current hardware block state to set
224  *
225  * rfkill drivers that get events when the hard-blocked state changes
226  * use this function to notify the rfkill core (and through that also
227  * userspace) of the current state.  They should also use this after
228  * resume if the state could have changed.
229  *
230  * You need not (but may) call this function if poll_state is assigned.
231  *
232  * This function can be called in any context, even from within rfkill
233  * callbacks.
234  *
235  * The function returns the combined block state (true if transmitter
236  * should be blocked) so that drivers need not keep track of the soft
237  * block state -- which they might not be able to.
238  */
239 bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
240 
241 /**
242  * rfkill_set_sw_state - Set the internal rfkill software block state
243  * @rfkill: pointer to the rfkill class to modify.
244  * @state: the current software block state to set
245  *
246  * rfkill drivers that get events when the soft-blocked state changes
247  * (yes, some platforms directly act on input but allow changing again)
248  * use this function to notify the rfkill core (and through that also
249  * userspace) of the current state.
250  *
251  * Drivers should also call this function after resume if the state has
252  * been changed by the user.  This only makes sense for "persistent"
253  * devices (see rfkill_init_sw_state()).
254  *
255  * This function can be called in any context, even from within rfkill
256  * callbacks.
257  *
258  * The function returns the combined block state (true if transmitter
259  * should be blocked).
260  */
261 bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
262 
263 /**
264  * rfkill_init_sw_state - Initialize persistent software block state
265  * @rfkill: pointer to the rfkill class to modify.
266  * @state: the current software block state to set
267  *
268  * rfkill drivers that preserve their software block state over power off
269  * use this function to notify the rfkill core (and through that also
270  * userspace) of their initial state.  It should only be used before
271  * registration.
272  *
273  * In addition, it marks the device as "persistent", an attribute which
274  * can be read by userspace.  Persistent devices are expected to preserve
275  * their own state when suspended.
276  */
277 void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
278 
279 /**
280  * rfkill_set_states - Set the internal rfkill block states
281  * @rfkill: pointer to the rfkill class to modify.
282  * @sw: the current software block state to set
283  * @hw: the current hardware block state to set
284  *
285  * This function can be called in any context, even from within rfkill
286  * callbacks.
287  */
288 void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
289 
290 /**
291  * rfkill_blocked - query rfkill block
292  *
293  * @rfkill: rfkill struct to query
294  */
295 bool rfkill_blocked(struct rfkill *rfkill);
296 #else /* !RFKILL */
297 static inline struct rfkill * __must_check
298 rfkill_alloc(const char *name,
299 	     struct device *parent,
300 	     const enum rfkill_type type,
301 	     const struct rfkill_ops *ops,
302 	     void *ops_data)
303 {
304 	return ERR_PTR(-ENODEV);
305 }
306 
307 static inline int __must_check rfkill_register(struct rfkill *rfkill)
308 {
309 	if (rfkill == ERR_PTR(-ENODEV))
310 		return 0;
311 	return -EINVAL;
312 }
313 
314 static inline void rfkill_pause_polling(struct rfkill *rfkill)
315 {
316 }
317 
318 static inline void rfkill_resume_polling(struct rfkill *rfkill)
319 {
320 }
321 
322 static inline void rfkill_unregister(struct rfkill *rfkill)
323 {
324 }
325 
326 static inline void rfkill_destroy(struct rfkill *rfkill)
327 {
328 }
329 
330 static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
331 {
332 	return blocked;
333 }
334 
335 static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
336 {
337 	return blocked;
338 }
339 
340 static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
341 {
342 }
343 
344 static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
345 {
346 }
347 
348 static inline bool rfkill_blocked(struct rfkill *rfkill)
349 {
350 	return false;
351 }
352 #endif /* RFKILL || RFKILL_MODULE */
353 
354 
355 #ifdef CONFIG_RFKILL_LEDS
356 /**
357  * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
358  * This function might return a NULL pointer if registering of the
359  * LED trigger failed. Use this as "default_trigger" for the LED.
360  */
361 const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
362 
363 /**
364  * rfkill_set_led_trigger_name -- set the LED trigger name
365  * @rfkill: rfkill struct
366  * @name: LED trigger name
367  *
368  * This function sets the LED trigger name of the radio LED
369  * trigger that rfkill creates. It is optional, but if called
370  * must be called before rfkill_register() to be effective.
371  */
372 void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
373 #else
374 static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
375 {
376 	return NULL;
377 }
378 
379 static inline void
380 rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
381 {
382 }
383 #endif
384 
385 #endif /* __KERNEL__ */
386 
387 #endif /* RFKILL_H */
388