blob: 3392c59d270681b0336d6edf6deac6e942e02693 [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 *
Johannes Berg8bc11b492009-08-26 18:13:17 +02009 * 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.
Ivo van Doorncf4328c2007-05-07 00:34:20 -070012 *
Johannes Berg8bc11b492009-08-26 18:13:17 +020013 * 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.
Ivo van Doorncf4328c2007-05-07 00:34:20 -070020 */
21
Johannes Bergc64fb012009-06-02 13:01:38 +020022#include <linux/types.h>
Johannes Berg19d337d2009-06-02 13:01:37 +020023
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
Johannes Bergc64fb012009-06-02 13:01:38 +020029/**
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 */
40enum 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,
Tomas Winkler3ad20142009-08-02 02:36:49 +030047 RFKILL_TYPE_GPS,
Johannes Bergc64fb012009-06-02 13:01:38 +020048 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 */
58enum 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 */
76struct rfkill_event {
77 __u32 idx;
78 __u8 type;
79 __u8 op;
80 __u8 soft, hard;
81} __packed;
82
Johannes Berg1be491f2009-07-05 14:51:06 +020083/*
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
Johannes Bergc64fb012009-06-02 13:01:38 +020097/* 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
Johannes Berg19d337d2009-06-02 13:01:37 +0200102/* and that's all userspace gets */
103#ifdef __KERNEL__
104/* don't allow anyone to use these in the kernel */
105enum 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
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700114#include <linux/kernel.h>
115#include <linux/list.h>
116#include <linux/mutex.h>
117#include <linux/device.h>
Michael Buesch135900c2007-09-27 21:33:12 +0200118#include <linux/leds.h>
Johannes Berg1506e302009-06-09 17:49:06 -0700119#include <linux/err.h>
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700120
Johannes Berg19d337d2009-06-02 13:01:37 +0200121/* this is opaque */
122struct rfkill;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700123
124/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200125 * struct rfkill_ops - rfkill driver methods
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700126 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200127 * @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
Johannes Bergc64fb012009-06-02 13:01:38 +0200139 * (blocked == true) -- ignore and return 0 when hard blocked.
Johannes Berg19d337d2009-06-02 13:01:37 +0200140 * This callback must be assigned.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700141 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200142struct 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);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700146};
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700147
Johannes Berg19d337d2009-06-02 13:01:37 +0200148#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 */
160struct 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
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100171 * the rfkill structure. Before calling this function the driver needs
172 * to be ready to service method calls from rfkill.
173 *
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100174 * 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.
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100177 *
178 * If the hardware blocked state is not set before registration,
179 * it is assumed to be unblocked.
Johannes Berg19d337d2009-06-02 13:01:37 +0200180 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300181int __must_check rfkill_register(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200182
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 */
190void 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 */
199void 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 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700210void rfkill_unregister(struct rfkill *rfkill);
211
Michael Buesch135900c2007-09-27 21:33:12 +0200212/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200213 * rfkill_destroy - free rfkill structure
214 * @rfkill: rfkill structure to be destroyed
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300215 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200216 * Destroys the rfkill structure.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300217 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200218void rfkill_destroy(struct rfkill *rfkill);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300219
220/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200221 * 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
Alan Jenkins908209c2009-06-08 13:12:23 +0100227 * userspace) of the current state. They should also use this after
Johannes Berg19d337d2009-06-02 13:01:37 +0200228 * 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.
Michael Buesch135900c2007-09-27 21:33:12 +0200238 */
Alan Jenkinse56f0972009-07-18 19:20:20 +0100239bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
Johannes Berg19d337d2009-06-02 13:01:37 +0200240
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
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100249 * 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()).
Johannes Berg19d337d2009-06-02 13:01:37 +0200254 *
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 */
261bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
262
263/**
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100264 * 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 *
Alan Jenkins464902e2009-06-16 14:54:04 +0100273 * 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.
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100276 */
277void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
278
279/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200280 * 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 */
288void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
289
290/**
Johannes Berg60811622009-06-02 13:01:40 +0200291 * rfkill_blocked - query rfkill block
292 *
293 * @rfkill: rfkill struct to query
294 */
295bool rfkill_blocked(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200296#else /* !RFKILL */
297static inline struct rfkill * __must_check
298rfkill_alloc(const char *name,
299 struct device *parent,
300 const enum rfkill_type type,
301 const struct rfkill_ops *ops,
302 void *ops_data)
Michael Buesch135900c2007-09-27 21:33:12 +0200303{
Johannes Berg19d337d2009-06-02 13:01:37 +0200304 return ERR_PTR(-ENODEV);
Michael Buesch135900c2007-09-27 21:33:12 +0200305}
306
Johannes Berg19d337d2009-06-02 13:01:37 +0200307static inline int __must_check rfkill_register(struct rfkill *rfkill)
308{
309 if (rfkill == ERR_PTR(-ENODEV))
310 return 0;
311 return -EINVAL;
312}
313
314static inline void rfkill_pause_polling(struct rfkill *rfkill)
315{
316}
317
318static inline void rfkill_resume_polling(struct rfkill *rfkill)
319{
320}
321
322static inline void rfkill_unregister(struct rfkill *rfkill)
323{
324}
325
326static inline void rfkill_destroy(struct rfkill *rfkill)
327{
328}
329
330static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
331{
332 return blocked;
333}
334
335static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
336{
337 return blocked;
338}
339
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100340static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
341{
342}
343
Johannes Berg19d337d2009-06-02 13:01:37 +0200344static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
345{
346}
347
Johannes Berg60811622009-06-02 13:01:40 +0200348static inline bool rfkill_blocked(struct rfkill *rfkill)
349{
350 return false;
351}
Johannes Berg19d337d2009-06-02 13:01:37 +0200352#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 */
361const 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 */
372void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
373#else
374static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
375{
376 return NULL;
377}
378
379static inline void
380rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
381{
382}
383#endif
384
385#endif /* __KERNEL__ */
386
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700387#endif /* RFKILL_H */