drivers/net/wireless: Use DIV_ROUND_CLOSEST
[deliverable/linux.git] / include / linux / rfkill.h
CommitLineData
cf4328cd
ID
1#ifndef __RFKILL_H
2#define __RFKILL_H
3
4/*
fe242cfd 5 * Copyright (C) 2006 - 2007 Ivo van Doorn
cf4328cd 6 * Copyright (C) 2007 Dmitry Torokhov
19d337df 7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
cf4328cd
ID
8 *
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
c64fb016 25#include <linux/types.h>
19d337df
JB
26
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
cf4328cd
ID
32/**
33 * enum rfkill_type - type of rfkill switch.
19d337df 34 *
c64fb016 35 * @RFKILL_TYPE_ALL: toggles all switches (userspace only)
19d337df
JB
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
cf4328cd
ID
42 */
43enum rfkill_type {
c64fb016 44 RFKILL_TYPE_ALL = 0,
19d337df 45 RFKILL_TYPE_WLAN,
234a0ca6 46 RFKILL_TYPE_BLUETOOTH,
e0665486 47 RFKILL_TYPE_UWB,
303d9bf6 48 RFKILL_TYPE_WIMAX,
477576a0 49 RFKILL_TYPE_WWAN,
19d337df 50 NUM_RFKILL_TYPES,
cf4328cd
ID
51};
52
c64fb016
JB
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
1be491fc
JB
85/*
86 * We are planning to be backward and forward compatible with changes
87 * to the event struct, by adding new, optional, members at the end.
88 * When reading an event (whether the kernel from userspace or vice
89 * versa) we need to accept anything that's at least as large as the
90 * version 1 event size, but might be able to accept other sizes in
91 * the future.
92 *
93 * One exception is the kernel -- we already have two event sizes in
94 * that we've made the 'hard' member optional since our only option
95 * is to ignore it anyway.
96 */
97#define RFKILL_EVENT_SIZE_V1 8
98
c64fb016
JB
99/* ioctl for turning off rfkill-input (if present) */
100#define RFKILL_IOC_MAGIC 'R'
101#define RFKILL_IOC_NOINPUT 1
102#define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
103
104/* and that's all userspace gets */
105#ifdef __KERNEL__
106/* don't allow anyone to use these in the kernel */
107enum rfkill_user_states {
108 RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
109 RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
110 RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
111};
112#undef RFKILL_STATE_SOFT_BLOCKED
113#undef RFKILL_STATE_UNBLOCKED
114#undef RFKILL_STATE_HARD_BLOCKED
115
c64fb016
JB
116#include <linux/kernel.h>
117#include <linux/list.h>
118#include <linux/mutex.h>
119#include <linux/device.h>
120#include <linux/leds.h>
1506e30b 121#include <linux/err.h>
c64fb016 122
19d337df
JB
123/* this is opaque */
124struct rfkill;
125
126/**
127 * struct rfkill_ops - rfkill driver methods
128 *
129 * @poll: poll the rfkill block state(s) -- only assign this method
130 * when you need polling. When called, simply call one of the
131 * rfkill_set{,_hw,_sw}_state family of functions. If the hw
132 * is getting unblocked you need to take into account the return
133 * value of those functions to make sure the software block is
134 * properly used.
135 * @query: query the rfkill block state(s) and call exactly one of the
136 * rfkill_set{,_hw,_sw}_state family of functions. Assign this
137 * method if input events can cause hardware state changes to make
138 * the rfkill core query your driver before setting a requested
139 * block.
140 * @set_block: turn the transmitter on (blocked == false) or off
c64fb016 141 * (blocked == true) -- ignore and return 0 when hard blocked.
19d337df
JB
142 * This callback must be assigned.
143 */
144struct rfkill_ops {
145 void (*poll)(struct rfkill *rfkill, void *data);
146 void (*query)(struct rfkill *rfkill, void *data);
147 int (*set_block)(void *data, bool blocked);
cf4328cd
ID
148};
149
19d337df 150#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
cf4328cd 151/**
19d337df
JB
152 * rfkill_alloc - allocate rfkill structure
153 * @name: name of the struct -- the string is not copied internally
154 * @parent: device that has rf switch on it
155 * @type: type of the switch (RFKILL_TYPE_*)
156 * @ops: rfkill methods
157 * @ops_data: data passed to each method
158 *
159 * This function should be called by the transmitter driver to allocate an
160 * rfkill structure. Returns %NULL on failure.
cf4328cd 161 */
19d337df
JB
162struct rfkill * __must_check rfkill_alloc(const char *name,
163 struct device *parent,
164 const enum rfkill_type type,
165 const struct rfkill_ops *ops,
166 void *ops_data);
cf4328cd 167
19d337df
JB
168/**
169 * rfkill_register - Register a rfkill structure.
170 * @rfkill: rfkill structure to be registered
171 *
172 * This function should be called by the transmitter driver to register
b3fa1329
AJ
173 * the rfkill structure. Before calling this function the driver needs
174 * to be ready to service method calls from rfkill.
175 *
06d5caf4
AJ
176 * If rfkill_init_sw_state() is not called before registration,
177 * set_block() will be called to initialize the software blocked state
178 * to a default value.
b3fa1329
AJ
179 *
180 * If the hardware blocked state is not set before registration,
181 * it is assumed to be unblocked.
19d337df
JB
182 */
183int __must_check rfkill_register(struct rfkill *rfkill);
135900c1 184
19d337df
JB
185/**
186 * rfkill_pause_polling(struct rfkill *rfkill)
187 *
188 * Pause polling -- say transmitter is off for other reasons.
189 * NOTE: not necessary for suspend/resume -- in that case the
190 * core stops polling anyway
191 */
192void rfkill_pause_polling(struct rfkill *rfkill);
cf4328cd 193
19d337df
JB
194/**
195 * rfkill_resume_polling(struct rfkill *rfkill)
196 *
197 * Pause polling -- say transmitter is off for other reasons.
198 * NOTE: not necessary for suspend/resume -- in that case the
199 * core stops polling anyway
200 */
201void rfkill_resume_polling(struct rfkill *rfkill);
202
203
204/**
205 * rfkill_unregister - Unregister a rfkill structure.
206 * @rfkill: rfkill structure to be unregistered
207 *
208 * This function should be called by the network driver during device
209 * teardown to destroy rfkill structure. Until it returns, the driver
210 * needs to be able to service method calls.
211 */
cf4328cd
ID
212void rfkill_unregister(struct rfkill *rfkill);
213
19d337df
JB
214/**
215 * rfkill_destroy - free rfkill structure
216 * @rfkill: rfkill structure to be destroyed
217 *
218 * Destroys the rfkill structure.
219 */
220void rfkill_destroy(struct rfkill *rfkill);
221
222/**
223 * rfkill_set_hw_state - Set the internal rfkill hardware block state
224 * @rfkill: pointer to the rfkill class to modify.
225 * @state: the current hardware block state to set
226 *
227 * rfkill drivers that get events when the hard-blocked state changes
228 * use this function to notify the rfkill core (and through that also
908209c1 229 * userspace) of the current state. They should also use this after
19d337df
JB
230 * resume if the state could have changed.
231 *
232 * You need not (but may) call this function if poll_state is assigned.
233 *
234 * This function can be called in any context, even from within rfkill
235 * callbacks.
236 *
237 * The function returns the combined block state (true if transmitter
238 * should be blocked) so that drivers need not keep track of the soft
239 * block state -- which they might not be able to.
240 */
e56f0975 241bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
19d337df
JB
242
243/**
244 * rfkill_set_sw_state - Set the internal rfkill software block state
245 * @rfkill: pointer to the rfkill class to modify.
246 * @state: the current software block state to set
247 *
248 * rfkill drivers that get events when the soft-blocked state changes
249 * (yes, some platforms directly act on input but allow changing again)
250 * use this function to notify the rfkill core (and through that also
06d5caf4
AJ
251 * userspace) of the current state.
252 *
253 * Drivers should also call this function after resume if the state has
254 * been changed by the user. This only makes sense for "persistent"
255 * devices (see rfkill_init_sw_state()).
19d337df
JB
256 *
257 * This function can be called in any context, even from within rfkill
258 * callbacks.
259 *
260 * The function returns the combined block state (true if transmitter
261 * should be blocked).
262 */
263bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
264
06d5caf4
AJ
265/**
266 * rfkill_init_sw_state - Initialize persistent software block state
267 * @rfkill: pointer to the rfkill class to modify.
268 * @state: the current software block state to set
269 *
270 * rfkill drivers that preserve their software block state over power off
271 * use this function to notify the rfkill core (and through that also
272 * userspace) of their initial state. It should only be used before
273 * registration.
274 *
464902e8
AJ
275 * In addition, it marks the device as "persistent", an attribute which
276 * can be read by userspace. Persistent devices are expected to preserve
277 * their own state when suspended.
06d5caf4
AJ
278 */
279void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
280
19d337df
JB
281/**
282 * rfkill_set_states - Set the internal rfkill block states
283 * @rfkill: pointer to the rfkill class to modify.
284 * @sw: the current software block state to set
285 * @hw: the current hardware block state to set
286 *
287 * This function can be called in any context, even from within rfkill
288 * callbacks.
289 */
290void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
801e49af 291
6081162e
JB
292/**
293 * rfkill_blocked - query rfkill block
294 *
295 * @rfkill: rfkill struct to query
296 */
297bool rfkill_blocked(struct rfkill *rfkill);
19d337df
JB
298#else /* !RFKILL */
299static inline struct rfkill * __must_check
300rfkill_alloc(const char *name,
301 struct device *parent,
302 const enum rfkill_type type,
303 const struct rfkill_ops *ops,
304 void *ops_data)
305{
306 return ERR_PTR(-ENODEV);
307}
308
309static inline int __must_check rfkill_register(struct rfkill *rfkill)
310{
311 if (rfkill == ERR_PTR(-ENODEV))
312 return 0;
313 return -EINVAL;
314}
315
316static inline void rfkill_pause_polling(struct rfkill *rfkill)
317{
318}
319
320static inline void rfkill_resume_polling(struct rfkill *rfkill)
321{
322}
323
324static inline void rfkill_unregister(struct rfkill *rfkill)
325{
326}
327
328static inline void rfkill_destroy(struct rfkill *rfkill)
329{
330}
331
332static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
333{
334 return blocked;
335}
336
337static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
338{
339 return blocked;
340}
341
06d5caf4
AJ
342static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
343{
344}
345
19d337df
JB
346static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
347{
348}
349
6081162e
JB
350static inline bool rfkill_blocked(struct rfkill *rfkill)
351{
352 return false;
353}
19d337df
JB
354#endif /* RFKILL || RFKILL_MODULE */
355
5005657c 356
19d337df 357#ifdef CONFIG_RFKILL_LEDS
135900c1 358/**
19d337df 359 * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
135900c1 360 * This function might return a NULL pointer if registering of the
19d337df 361 * LED trigger failed. Use this as "default_trigger" for the LED.
135900c1 362 */
19d337df
JB
363const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
364
365/**
366 * rfkill_set_led_trigger_name -- set the LED trigger name
367 * @rfkill: rfkill struct
368 * @name: LED trigger name
369 *
370 * This function sets the LED trigger name of the radio LED
371 * trigger that rfkill creates. It is optional, but if called
372 * must be called before rfkill_register() to be effective.
373 */
374void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
135900c1 375#else
19d337df
JB
376static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
377{
135900c1 378 return NULL;
135900c1
MB
379}
380
19d337df
JB
381static inline void
382rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
383{
384}
385#endif
386
387#endif /* __KERNEL__ */
388
cf4328cd 389#endif /* RFKILL_H */
This page took 0.435828 seconds and 5 git commands to generate.