5 * Copyright (C) 2006 - 2007 Ivo van Doorn
6 * Copyright (C) 2007 Dmitry Torokhov
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
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.
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.
22 #include <linux/types.h>
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
30 * enum rfkill_type - type of rfkill switch.
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 * @RFKILL_TYPE_GPS: switch is on a GPS device.
39 * @NUM_RFKILL_TYPES: number of defined rfkill types
44 RFKILL_TYPE_BLUETOOTH,
53 * enum rfkill_operation - operation types
54 * @RFKILL_OP_ADD: a device was added
55 * @RFKILL_OP_DEL: a device was removed
56 * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
57 * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
59 enum rfkill_operation {
67 * struct rfkill_event - events for userspace on /dev/rfkill
68 * @idx: index of dev rfkill
69 * @type: type of the rfkill struct
71 * @hard: hard state (0/1)
72 * @soft: soft state (0/1)
74 * Structure used for userspace communication on /dev/rfkill,
75 * used for events from the kernel and control to the kernel.
85 * We are planning to be backward and forward compatible with changes
86 * to the event struct, by adding new, optional, members at the end.
87 * When reading an event (whether the kernel from userspace or vice
88 * versa) we need to accept anything that's at least as large as the
89 * version 1 event size, but might be able to accept other sizes in
92 * One exception is the kernel -- we already have two event sizes in
93 * that we've made the 'hard' member optional since our only option
94 * is to ignore it anyway.
96 #define RFKILL_EVENT_SIZE_V1 8
98 /* ioctl for turning off rfkill-input (if present) */
99 #define RFKILL_IOC_MAGIC 'R'
100 #define RFKILL_IOC_NOINPUT 1
101 #define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
103 /* and that's all userspace gets */
105 /* don't allow anyone to use these in the kernel */
106 enum rfkill_user_states {
107 RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
108 RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
109 RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
111 #undef RFKILL_STATE_SOFT_BLOCKED
112 #undef RFKILL_STATE_UNBLOCKED
113 #undef RFKILL_STATE_HARD_BLOCKED
115 #include <linux/kernel.h>
116 #include <linux/list.h>
117 #include <linux/mutex.h>
118 #include <linux/device.h>
119 #include <linux/leds.h>
120 #include <linux/err.h>
126 * struct rfkill_ops - rfkill driver methods
128 * @poll: poll the rfkill block state(s) -- only assign this method
129 * when you need polling. When called, simply call one of the
130 * rfkill_set{,_hw,_sw}_state family of functions. If the hw
131 * is getting unblocked you need to take into account the return
132 * value of those functions to make sure the software block is
134 * @query: query the rfkill block state(s) and call exactly one of the
135 * rfkill_set{,_hw,_sw}_state family of functions. Assign this
136 * method if input events can cause hardware state changes to make
137 * the rfkill core query your driver before setting a requested
139 * @set_block: turn the transmitter on (blocked == false) or off
140 * (blocked == true) -- ignore and return 0 when hard blocked.
141 * This callback must be assigned.
144 void (*poll)(struct rfkill *rfkill, void *data);
145 void (*query)(struct rfkill *rfkill, void *data);
146 int (*set_block)(void *data, bool blocked);
149 #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
151 * rfkill_alloc - allocate rfkill structure
152 * @name: name of the struct -- the string is not copied internally
153 * @parent: device that has rf switch on it
154 * @type: type of the switch (RFKILL_TYPE_*)
155 * @ops: rfkill methods
156 * @ops_data: data passed to each method
158 * This function should be called by the transmitter driver to allocate an
159 * rfkill structure. Returns %NULL on failure.
161 struct rfkill * __must_check rfkill_alloc(const char *name,
162 struct device *parent,
163 const enum rfkill_type type,
164 const struct rfkill_ops *ops,
168 * rfkill_register - Register a rfkill structure.
169 * @rfkill: rfkill structure to be registered
171 * This function should be called by the transmitter driver to register
172 * the rfkill structure. Before calling this function the driver needs
173 * to be ready to service method calls from rfkill.
175 * If rfkill_init_sw_state() is not called before registration,
176 * set_block() will be called to initialize the software blocked state
177 * to a default value.
179 * If the hardware blocked state is not set before registration,
180 * it is assumed to be unblocked.
182 int __must_check rfkill_register(struct rfkill *rfkill);
185 * rfkill_pause_polling(struct rfkill *rfkill)
187 * Pause polling -- say transmitter is off for other reasons.
188 * NOTE: not necessary for suspend/resume -- in that case the
189 * core stops polling anyway
191 void rfkill_pause_polling(struct rfkill *rfkill);
194 * rfkill_resume_polling(struct rfkill *rfkill)
196 * Pause polling -- say transmitter is off for other reasons.
197 * NOTE: not necessary for suspend/resume -- in that case the
198 * core stops polling anyway
200 void rfkill_resume_polling(struct rfkill *rfkill);
204 * rfkill_unregister - Unregister a rfkill structure.
205 * @rfkill: rfkill structure to be unregistered
207 * This function should be called by the network driver during device
208 * teardown to destroy rfkill structure. Until it returns, the driver
209 * needs to be able to service method calls.
211 void rfkill_unregister(struct rfkill *rfkill);
214 * rfkill_destroy - free rfkill structure
215 * @rfkill: rfkill structure to be destroyed
217 * Destroys the rfkill structure.
219 void rfkill_destroy(struct rfkill *rfkill);
222 * rfkill_set_hw_state - Set the internal rfkill hardware block state
223 * @rfkill: pointer to the rfkill class to modify.
224 * @state: the current hardware block state to set
226 * rfkill drivers that get events when the hard-blocked state changes
227 * use this function to notify the rfkill core (and through that also
228 * userspace) of the current state. They should also use this after
229 * resume if the state could have changed.
231 * You need not (but may) call this function if poll_state is assigned.
233 * This function can be called in any context, even from within rfkill
236 * The function returns the combined block state (true if transmitter
237 * should be blocked) so that drivers need not keep track of the soft
238 * block state -- which they might not be able to.
240 bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
243 * rfkill_set_sw_state - Set the internal rfkill software block state
244 * @rfkill: pointer to the rfkill class to modify.
245 * @state: the current software block state to set
247 * rfkill drivers that get events when the soft-blocked state changes
248 * (yes, some platforms directly act on input but allow changing again)
249 * use this function to notify the rfkill core (and through that also
250 * userspace) of the current state.
252 * Drivers should also call this function after resume if the state has
253 * been changed by the user. This only makes sense for "persistent"
254 * devices (see rfkill_init_sw_state()).
256 * This function can be called in any context, even from within rfkill
259 * The function returns the combined block state (true if transmitter
260 * should be blocked).
262 bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
265 * rfkill_init_sw_state - Initialize persistent software block state
266 * @rfkill: pointer to the rfkill class to modify.
267 * @state: the current software block state to set
269 * rfkill drivers that preserve their software block state over power off
270 * use this function to notify the rfkill core (and through that also
271 * userspace) of their initial state. It should only be used before
274 * In addition, it marks the device as "persistent", an attribute which
275 * can be read by userspace. Persistent devices are expected to preserve
276 * their own state when suspended.
278 void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
281 * rfkill_set_states - Set the internal rfkill block states
282 * @rfkill: pointer to the rfkill class to modify.
283 * @sw: the current software block state to set
284 * @hw: the current hardware block state to set
286 * This function can be called in any context, even from within rfkill
289 void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
292 * rfkill_blocked - query rfkill block
294 * @rfkill: rfkill struct to query
296 bool rfkill_blocked(struct rfkill *rfkill);
298 static inline struct rfkill * __must_check
299 rfkill_alloc(const char *name,
300 struct device *parent,
301 const enum rfkill_type type,
302 const struct rfkill_ops *ops,
305 return ERR_PTR(-ENODEV);
308 static inline int __must_check rfkill_register(struct rfkill *rfkill)
310 if (rfkill == ERR_PTR(-ENODEV))
315 static inline void rfkill_pause_polling(struct rfkill *rfkill)
319 static inline void rfkill_resume_polling(struct rfkill *rfkill)
323 static inline void rfkill_unregister(struct rfkill *rfkill)
327 static inline void rfkill_destroy(struct rfkill *rfkill)
331 static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
336 static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
341 static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
345 static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
349 static inline bool rfkill_blocked(struct rfkill *rfkill)
353 #endif /* RFKILL || RFKILL_MODULE */
356 #ifdef CONFIG_RFKILL_LEDS
358 * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
359 * This function might return a NULL pointer if registering of the
360 * LED trigger failed. Use this as "default_trigger" for the LED.
362 const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
365 * rfkill_set_led_trigger_name -- set the LED trigger name
366 * @rfkill: rfkill struct
367 * @name: LED trigger name
369 * This function sets the LED trigger name of the radio LED
370 * trigger that rfkill creates. It is optional, but if called
371 * must be called before rfkill_register() to be effective.
373 void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
375 static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
381 rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
386 #endif /* __KERNEL__ */
388 #endif /* RFKILL_H */