blob: c1dca0b8138b6d33230d907b9f9cb1896c9fb03f [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001#ifndef __RFKILL_H
2#define __RFKILL_H
3
4/*
Ivo van Doornfe242cf2007-09-27 14:57:05 -07005 * Copyright (C) 2006 - 2007 Ivo van Doorn
Ivo van Doorncf4328c2007-05-07 00:34:20 -07006 * Copyright (C) 2007 Dmitry Torokhov
Johannes Berg19d337d2009-06-02 13:01:37 +02007 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
Ivo van Doorncf4328c2007-05-07 00:34:20 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the
21 * Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
Johannes Bergc64fb012009-06-02 13:01:38 +020025#include <linux/types.h>
Johannes Berg19d337d2009-06-02 13:01:37 +020026
27/* define userspace visible states */
28#define RFKILL_STATE_SOFT_BLOCKED 0
29#define RFKILL_STATE_UNBLOCKED 1
30#define RFKILL_STATE_HARD_BLOCKED 2
31
Johannes Bergc64fb012009-06-02 13:01:38 +020032/**
33 * enum rfkill_type - type of rfkill switch.
34 *
35 * @RFKILL_TYPE_ALL: toggles all switches (userspace only)
36 * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device.
37 * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device.
38 * @RFKILL_TYPE_UWB: switch is on a ultra wideband device.
39 * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device.
40 * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device.
41 * @NUM_RFKILL_TYPES: number of defined rfkill types
42 */
43enum rfkill_type {
44 RFKILL_TYPE_ALL = 0,
45 RFKILL_TYPE_WLAN,
46 RFKILL_TYPE_BLUETOOTH,
47 RFKILL_TYPE_UWB,
48 RFKILL_TYPE_WIMAX,
49 RFKILL_TYPE_WWAN,
50 NUM_RFKILL_TYPES,
51};
52
53/**
54 * enum rfkill_operation - operation types
55 * @RFKILL_OP_ADD: a device was added
56 * @RFKILL_OP_DEL: a device was removed
57 * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
58 * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
59 */
60enum rfkill_operation {
61 RFKILL_OP_ADD = 0,
62 RFKILL_OP_DEL,
63 RFKILL_OP_CHANGE,
64 RFKILL_OP_CHANGE_ALL,
65};
66
67/**
68 * struct rfkill_event - events for userspace on /dev/rfkill
69 * @idx: index of dev rfkill
70 * @type: type of the rfkill struct
71 * @op: operation code
72 * @hard: hard state (0/1)
73 * @soft: soft state (0/1)
74 *
75 * Structure used for userspace communication on /dev/rfkill,
76 * used for events from the kernel and control to the kernel.
77 */
78struct rfkill_event {
79 __u32 idx;
80 __u8 type;
81 __u8 op;
82 __u8 soft, hard;
83} __packed;
84
85/* ioctl for turning off rfkill-input (if present) */
86#define RFKILL_IOC_MAGIC 'R'
87#define RFKILL_IOC_NOINPUT 1
88#define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
89
Johannes Berg19d337d2009-06-02 13:01:37 +020090/* and that's all userspace gets */
91#ifdef __KERNEL__
92/* don't allow anyone to use these in the kernel */
93enum rfkill_user_states {
94 RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
95 RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
96 RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
97};
98#undef RFKILL_STATE_SOFT_BLOCKED
99#undef RFKILL_STATE_UNBLOCKED
100#undef RFKILL_STATE_HARD_BLOCKED
101
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700102#include <linux/types.h>
103#include <linux/kernel.h>
104#include <linux/list.h>
105#include <linux/mutex.h>
106#include <linux/device.h>
Michael Buesch135900c2007-09-27 21:33:12 +0200107#include <linux/leds.h>
Johannes Berg1506e302009-06-09 17:49:06 -0700108#include <linux/err.h>
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700109
Johannes Berg19d337d2009-06-02 13:01:37 +0200110/* this is opaque */
111struct rfkill;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700112
113/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200114 * struct rfkill_ops - rfkill driver methods
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700115 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200116 * @poll: poll the rfkill block state(s) -- only assign this method
117 * when you need polling. When called, simply call one of the
118 * rfkill_set{,_hw,_sw}_state family of functions. If the hw
119 * is getting unblocked you need to take into account the return
120 * value of those functions to make sure the software block is
121 * properly used.
122 * @query: query the rfkill block state(s) and call exactly one of the
123 * rfkill_set{,_hw,_sw}_state family of functions. Assign this
124 * method if input events can cause hardware state changes to make
125 * the rfkill core query your driver before setting a requested
126 * block.
127 * @set_block: turn the transmitter on (blocked == false) or off
Johannes Bergc64fb012009-06-02 13:01:38 +0200128 * (blocked == true) -- ignore and return 0 when hard blocked.
Johannes Berg19d337d2009-06-02 13:01:37 +0200129 * This callback must be assigned.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700130 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200131struct rfkill_ops {
132 void (*poll)(struct rfkill *rfkill, void *data);
133 void (*query)(struct rfkill *rfkill, void *data);
134 int (*set_block)(void *data, bool blocked);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700135};
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700136
Johannes Berg19d337d2009-06-02 13:01:37 +0200137#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
138/**
139 * rfkill_alloc - allocate rfkill structure
140 * @name: name of the struct -- the string is not copied internally
141 * @parent: device that has rf switch on it
142 * @type: type of the switch (RFKILL_TYPE_*)
143 * @ops: rfkill methods
144 * @ops_data: data passed to each method
145 *
146 * This function should be called by the transmitter driver to allocate an
147 * rfkill structure. Returns %NULL on failure.
148 */
149struct rfkill * __must_check rfkill_alloc(const char *name,
150 struct device *parent,
151 const enum rfkill_type type,
152 const struct rfkill_ops *ops,
153 void *ops_data);
154
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
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100160 * the rfkill structure. Before calling this function the driver needs
161 * to be ready to service method calls from rfkill.
162 *
163 * If the software blocked state is not set before registration,
164 * set_block will be called to initialize it to a default value.
165 *
166 * If the hardware blocked state is not set before registration,
167 * it is assumed to be unblocked.
Johannes Berg19d337d2009-06-02 13:01:37 +0200168 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300169int __must_check rfkill_register(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200170
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 */
178void rfkill_pause_polling(struct rfkill *rfkill);
179
180/**
181 * rfkill_resume_polling(struct rfkill *rfkill)
182 *
183 * Pause polling -- say transmitter is off for other reasons.
184 * NOTE: not necessary for suspend/resume -- in that case the
185 * core stops polling anyway
186 */
187void rfkill_resume_polling(struct rfkill *rfkill);
188
189
190/**
191 * rfkill_unregister - Unregister a rfkill structure.
192 * @rfkill: rfkill structure to be unregistered
193 *
194 * This function should be called by the network driver during device
195 * teardown to destroy rfkill structure. Until it returns, the driver
196 * needs to be able to service method calls.
197 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700198void rfkill_unregister(struct rfkill *rfkill);
199
Michael Buesch135900c2007-09-27 21:33:12 +0200200/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200201 * rfkill_destroy - free rfkill structure
202 * @rfkill: rfkill structure to be destroyed
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300203 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200204 * Destroys the rfkill structure.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300205 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200206void rfkill_destroy(struct rfkill *rfkill);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300207
208/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200209 * rfkill_set_hw_state - Set the internal rfkill hardware block state
210 * @rfkill: pointer to the rfkill class to modify.
211 * @state: the current hardware block state to set
212 *
213 * rfkill drivers that get events when the hard-blocked state changes
214 * use this function to notify the rfkill core (and through that also
215 * userspace) of the current state -- they should also use this after
216 * resume if the state could have changed.
217 *
218 * You need not (but may) call this function if poll_state is assigned.
219 *
220 * This function can be called in any context, even from within rfkill
221 * callbacks.
222 *
223 * The function returns the combined block state (true if transmitter
224 * should be blocked) so that drivers need not keep track of the soft
225 * block state -- which they might not be able to.
Michael Buesch135900c2007-09-27 21:33:12 +0200226 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200227bool __must_check rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
228
229/**
230 * rfkill_set_sw_state - Set the internal rfkill software block state
231 * @rfkill: pointer to the rfkill class to modify.
232 * @state: the current software block state to set
233 *
234 * rfkill drivers that get events when the soft-blocked state changes
235 * (yes, some platforms directly act on input but allow changing again)
236 * use this function to notify the rfkill core (and through that also
237 * userspace) of the current state -- they should also use this after
238 * resume if the state could have changed.
239 *
240 * This function can be called in any context, even from within rfkill
241 * callbacks.
242 *
243 * The function returns the combined block state (true if transmitter
244 * should be blocked).
245 */
246bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
247
248/**
249 * rfkill_set_states - Set the internal rfkill block states
250 * @rfkill: pointer to the rfkill class to modify.
251 * @sw: the current software block state to set
252 * @hw: the current hardware block state to set
253 *
254 * This function can be called in any context, even from within rfkill
255 * callbacks.
256 */
257void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
258
259/**
Johannes Berg60811622009-06-02 13:01:40 +0200260 * rfkill_blocked - query rfkill block
261 *
262 * @rfkill: rfkill struct to query
263 */
264bool rfkill_blocked(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200265#else /* !RFKILL */
266static inline struct rfkill * __must_check
267rfkill_alloc(const char *name,
268 struct device *parent,
269 const enum rfkill_type type,
270 const struct rfkill_ops *ops,
271 void *ops_data)
Michael Buesch135900c2007-09-27 21:33:12 +0200272{
Johannes Berg19d337d2009-06-02 13:01:37 +0200273 return ERR_PTR(-ENODEV);
Michael Buesch135900c2007-09-27 21:33:12 +0200274}
275
Johannes Berg19d337d2009-06-02 13:01:37 +0200276static inline int __must_check rfkill_register(struct rfkill *rfkill)
277{
278 if (rfkill == ERR_PTR(-ENODEV))
279 return 0;
280 return -EINVAL;
281}
282
283static inline void rfkill_pause_polling(struct rfkill *rfkill)
284{
285}
286
287static inline void rfkill_resume_polling(struct rfkill *rfkill)
288{
289}
290
291static inline void rfkill_unregister(struct rfkill *rfkill)
292{
293}
294
295static inline void rfkill_destroy(struct rfkill *rfkill)
296{
297}
298
299static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
300{
301 return blocked;
302}
303
304static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
305{
306 return blocked;
307}
308
309static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
310{
311}
312
Johannes Berg60811622009-06-02 13:01:40 +0200313static inline bool rfkill_blocked(struct rfkill *rfkill)
314{
315 return false;
316}
Johannes Berg19d337d2009-06-02 13:01:37 +0200317#endif /* RFKILL || RFKILL_MODULE */
318
319
320#ifdef CONFIG_RFKILL_LEDS
321/**
322 * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
323 * This function might return a NULL pointer if registering of the
324 * LED trigger failed. Use this as "default_trigger" for the LED.
325 */
326const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
327
328/**
329 * rfkill_set_led_trigger_name -- set the LED trigger name
330 * @rfkill: rfkill struct
331 * @name: LED trigger name
332 *
333 * This function sets the LED trigger name of the radio LED
334 * trigger that rfkill creates. It is optional, but if called
335 * must be called before rfkill_register() to be effective.
336 */
337void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
338#else
339static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
340{
341 return NULL;
342}
343
344static inline void
345rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
346{
347}
348#endif
349
350#endif /* __KERNEL__ */
351
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700352#endif /* RFKILL_H */