blob: 7dec42cfa41956c4bf79c79f29c5fa7a8e83a9b8 [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 Berg8bc11b42009-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 Berg8bc11b42009-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 *
Matthew Garrett3082a2b2010-02-16 16:36:25 -050032 * @RFKILL_TYPE_ALL: toggles all switches (requests only - not a switch type)
Johannes Bergc64fb012009-06-02 13:01:38 +020033 * @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.
Marcel Holtmann0878c352009-11-18 16:48:00 +010038 * @RFKILL_TYPE_GPS: switch is on a GPS device.
Marcel Holtmann875405a2009-11-18 16:48:01 +010039 * @RFKILL_TYPE_FM: switch is on a FM radio device.
Johannes Bergc64fb012009-06-02 13:01:38 +020040 * @NUM_RFKILL_TYPES: number of defined rfkill types
41 */
42enum rfkill_type {
43 RFKILL_TYPE_ALL = 0,
44 RFKILL_TYPE_WLAN,
45 RFKILL_TYPE_BLUETOOTH,
46 RFKILL_TYPE_UWB,
47 RFKILL_TYPE_WIMAX,
48 RFKILL_TYPE_WWAN,
Tomas Winkler3ad20142009-08-02 02:36:49 +030049 RFKILL_TYPE_GPS,
Marcel Holtmann875405a2009-11-18 16:48:01 +010050 RFKILL_TYPE_FM,
Johannes Bergc64fb012009-06-02 13:01:38 +020051 NUM_RFKILL_TYPES,
52};
53
54/**
55 * enum rfkill_operation - operation types
56 * @RFKILL_OP_ADD: a device was added
57 * @RFKILL_OP_DEL: a device was removed
58 * @RFKILL_OP_CHANGE: a device's state changed -- userspace changes one device
59 * @RFKILL_OP_CHANGE_ALL: userspace changes all devices (of a type, or all)
60 */
61enum rfkill_operation {
62 RFKILL_OP_ADD = 0,
63 RFKILL_OP_DEL,
64 RFKILL_OP_CHANGE,
65 RFKILL_OP_CHANGE_ALL,
66};
67
68/**
69 * struct rfkill_event - events for userspace on /dev/rfkill
70 * @idx: index of dev rfkill
71 * @type: type of the rfkill struct
72 * @op: operation code
73 * @hard: hard state (0/1)
74 * @soft: soft state (0/1)
75 *
76 * Structure used for userspace communication on /dev/rfkill,
77 * used for events from the kernel and control to the kernel.
78 */
79struct rfkill_event {
80 __u32 idx;
81 __u8 type;
82 __u8 op;
83 __u8 soft, hard;
Changli Gao09cd2b92010-08-22 17:25:05 +000084} __attribute__((packed));
Johannes Bergc64fb012009-06-02 13:01:38 +020085
Johannes Berg1be491f2009-07-05 14:51:06 +020086/*
87 * We are planning to be backward and forward compatible with changes
88 * to the event struct, by adding new, optional, members at the end.
89 * When reading an event (whether the kernel from userspace or vice
90 * versa) we need to accept anything that's at least as large as the
91 * version 1 event size, but might be able to accept other sizes in
92 * the future.
93 *
94 * One exception is the kernel -- we already have two event sizes in
95 * that we've made the 'hard' member optional since our only option
96 * is to ignore it anyway.
97 */
98#define RFKILL_EVENT_SIZE_V1 8
99
Johannes Bergc64fb012009-06-02 13:01:38 +0200100/* ioctl for turning off rfkill-input (if present) */
101#define RFKILL_IOC_MAGIC 'R'
102#define RFKILL_IOC_NOINPUT 1
103#define RFKILL_IOCTL_NOINPUT _IO(RFKILL_IOC_MAGIC, RFKILL_IOC_NOINPUT)
104
Johannes Berg19d337d2009-06-02 13:01:37 +0200105/* and that's all userspace gets */
106#ifdef __KERNEL__
107/* don't allow anyone to use these in the kernel */
108enum rfkill_user_states {
109 RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
110 RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
111 RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
112};
113#undef RFKILL_STATE_SOFT_BLOCKED
114#undef RFKILL_STATE_UNBLOCKED
115#undef RFKILL_STATE_HARD_BLOCKED
116
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700117#include <linux/kernel.h>
118#include <linux/list.h>
119#include <linux/mutex.h>
Michael Buesch135900c2007-09-27 21:33:12 +0200120#include <linux/leds.h>
Johannes Berg1506e302009-06-09 17:49:06 -0700121#include <linux/err.h>
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700122
Paul Gortmaker313162d2012-01-30 11:46:54 -0500123struct device;
Johannes Berg19d337d2009-06-02 13:01:37 +0200124/* this is opaque */
125struct rfkill;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700126
127/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200128 * struct rfkill_ops - rfkill driver methods
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700129 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200130 * @poll: poll the rfkill block state(s) -- only assign this method
131 * when you need polling. When called, simply call one of the
132 * rfkill_set{,_hw,_sw}_state family of functions. If the hw
133 * is getting unblocked you need to take into account the return
134 * value of those functions to make sure the software block is
135 * properly used.
136 * @query: query the rfkill block state(s) and call exactly one of the
137 * rfkill_set{,_hw,_sw}_state family of functions. Assign this
138 * method if input events can cause hardware state changes to make
139 * the rfkill core query your driver before setting a requested
140 * block.
141 * @set_block: turn the transmitter on (blocked == false) or off
Johannes Bergc64fb012009-06-02 13:01:38 +0200142 * (blocked == true) -- ignore and return 0 when hard blocked.
Johannes Berg19d337d2009-06-02 13:01:37 +0200143 * This callback must be assigned.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700144 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200145struct rfkill_ops {
146 void (*poll)(struct rfkill *rfkill, void *data);
147 void (*query)(struct rfkill *rfkill, void *data);
148 int (*set_block)(void *data, bool blocked);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700149};
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700150
Johannes Berg19d337d2009-06-02 13:01:37 +0200151#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
152/**
153 * rfkill_alloc - allocate rfkill structure
154 * @name: name of the struct -- the string is not copied internally
155 * @parent: device that has rf switch on it
156 * @type: type of the switch (RFKILL_TYPE_*)
157 * @ops: rfkill methods
158 * @ops_data: data passed to each method
159 *
160 * This function should be called by the transmitter driver to allocate an
161 * rfkill structure. Returns %NULL on failure.
162 */
163struct rfkill * __must_check rfkill_alloc(const char *name,
164 struct device *parent,
165 const enum rfkill_type type,
166 const struct rfkill_ops *ops,
167 void *ops_data);
168
169/**
170 * rfkill_register - Register a rfkill structure.
171 * @rfkill: rfkill structure to be registered
172 *
173 * This function should be called by the transmitter driver to register
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100174 * the rfkill structure. Before calling this function the driver needs
175 * to be ready to service method calls from rfkill.
176 *
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100177 * If rfkill_init_sw_state() is not called before registration,
178 * set_block() will be called to initialize the software blocked state
179 * to a default value.
Alan Jenkinsb3fa1322009-06-08 13:27:27 +0100180 *
181 * If the hardware blocked state is not set before registration,
182 * it is assumed to be unblocked.
Johannes Berg19d337d2009-06-02 13:01:37 +0200183 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300184int __must_check rfkill_register(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200185
186/**
187 * rfkill_pause_polling(struct rfkill *rfkill)
188 *
189 * Pause polling -- say transmitter is off for other reasons.
190 * NOTE: not necessary for suspend/resume -- in that case the
191 * core stops polling anyway
192 */
193void rfkill_pause_polling(struct rfkill *rfkill);
194
195/**
196 * rfkill_resume_polling(struct rfkill *rfkill)
197 *
198 * Pause polling -- say transmitter is off for other reasons.
199 * NOTE: not necessary for suspend/resume -- in that case the
200 * core stops polling anyway
201 */
Chintan Pandyacba43d22011-11-29 13:11:40 +0530202#ifdef CONFIG_RFKILL_PM
Johannes Berg19d337d2009-06-02 13:01:37 +0200203void rfkill_resume_polling(struct rfkill *rfkill);
Chintan Pandyacba43d22011-11-29 13:11:40 +0530204#else
205static inline void rfkill_resume_polling(struct rfkill *rfkill) { }
206#endif
Johannes Berg19d337d2009-06-02 13:01:37 +0200207
208/**
209 * rfkill_unregister - Unregister a rfkill structure.
210 * @rfkill: rfkill structure to be unregistered
211 *
212 * This function should be called by the network driver during device
213 * teardown to destroy rfkill structure. Until it returns, the driver
214 * needs to be able to service method calls.
215 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700216void rfkill_unregister(struct rfkill *rfkill);
217
Michael Buesch135900c2007-09-27 21:33:12 +0200218/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200219 * rfkill_destroy - free rfkill structure
220 * @rfkill: rfkill structure to be destroyed
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300221 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200222 * Destroys the rfkill structure.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300223 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200224void rfkill_destroy(struct rfkill *rfkill);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300225
226/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200227 * rfkill_set_hw_state - Set the internal rfkill hardware block state
228 * @rfkill: pointer to the rfkill class to modify.
229 * @state: the current hardware block state to set
230 *
231 * rfkill drivers that get events when the hard-blocked state changes
232 * use this function to notify the rfkill core (and through that also
Alan Jenkins908209c2009-06-08 13:12:23 +0100233 * userspace) of the current state. They should also use this after
Johannes Berg19d337d2009-06-02 13:01:37 +0200234 * resume if the state could have changed.
235 *
236 * You need not (but may) call this function if poll_state is assigned.
237 *
238 * This function can be called in any context, even from within rfkill
239 * callbacks.
240 *
241 * The function returns the combined block state (true if transmitter
242 * should be blocked) so that drivers need not keep track of the soft
243 * block state -- which they might not be able to.
Michael Buesch135900c2007-09-27 21:33:12 +0200244 */
Alan Jenkinse56f0972009-07-18 19:20:20 +0100245bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
Johannes Berg19d337d2009-06-02 13:01:37 +0200246
247/**
248 * rfkill_set_sw_state - Set the internal rfkill software block state
249 * @rfkill: pointer to the rfkill class to modify.
250 * @state: the current software block state to set
251 *
252 * rfkill drivers that get events when the soft-blocked state changes
253 * (yes, some platforms directly act on input but allow changing again)
254 * use this function to notify the rfkill core (and through that also
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100255 * userspace) of the current state.
256 *
257 * Drivers should also call this function after resume if the state has
258 * been changed by the user. This only makes sense for "persistent"
259 * devices (see rfkill_init_sw_state()).
Johannes Berg19d337d2009-06-02 13:01:37 +0200260 *
261 * This function can be called in any context, even from within rfkill
262 * callbacks.
263 *
264 * The function returns the combined block state (true if transmitter
265 * should be blocked).
266 */
267bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
268
269/**
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100270 * rfkill_init_sw_state - Initialize persistent software block state
271 * @rfkill: pointer to the rfkill class to modify.
272 * @state: the current software block state to set
273 *
274 * rfkill drivers that preserve their software block state over power off
275 * use this function to notify the rfkill core (and through that also
276 * userspace) of their initial state. It should only be used before
277 * registration.
278 *
Alan Jenkins464902e2009-06-16 14:54:04 +0100279 * In addition, it marks the device as "persistent", an attribute which
280 * can be read by userspace. Persistent devices are expected to preserve
281 * their own state when suspended.
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100282 */
283void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
284
285/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200286 * rfkill_set_states - Set the internal rfkill block states
287 * @rfkill: pointer to the rfkill class to modify.
288 * @sw: the current software block state to set
289 * @hw: the current hardware block state to set
290 *
291 * This function can be called in any context, even from within rfkill
292 * callbacks.
293 */
294void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
295
296/**
Johannes Berg60811622009-06-02 13:01:40 +0200297 * rfkill_blocked - query rfkill block
298 *
299 * @rfkill: rfkill struct to query
300 */
301bool rfkill_blocked(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200302#else /* !RFKILL */
303static inline struct rfkill * __must_check
304rfkill_alloc(const char *name,
305 struct device *parent,
306 const enum rfkill_type type,
307 const struct rfkill_ops *ops,
308 void *ops_data)
Michael Buesch135900c2007-09-27 21:33:12 +0200309{
Johannes Berg19d337d2009-06-02 13:01:37 +0200310 return ERR_PTR(-ENODEV);
Michael Buesch135900c2007-09-27 21:33:12 +0200311}
312
Johannes Berg19d337d2009-06-02 13:01:37 +0200313static inline int __must_check rfkill_register(struct rfkill *rfkill)
314{
315 if (rfkill == ERR_PTR(-ENODEV))
316 return 0;
317 return -EINVAL;
318}
319
320static inline void rfkill_pause_polling(struct rfkill *rfkill)
321{
322}
323
324static inline void rfkill_resume_polling(struct rfkill *rfkill)
325{
326}
327
328static inline void rfkill_unregister(struct rfkill *rfkill)
329{
330}
331
332static inline void rfkill_destroy(struct rfkill *rfkill)
333{
334}
335
336static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
337{
338 return blocked;
339}
340
341static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
342{
343 return blocked;
344}
345
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100346static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
347{
348}
349
Johannes Berg19d337d2009-06-02 13:01:37 +0200350static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
351{
352}
353
Johannes Berg60811622009-06-02 13:01:40 +0200354static inline bool rfkill_blocked(struct rfkill *rfkill)
355{
356 return false;
357}
Johannes Berg19d337d2009-06-02 13:01:37 +0200358#endif /* RFKILL || RFKILL_MODULE */
359
Johannes Berg19d337d2009-06-02 13:01:37 +0200360#endif /* __KERNEL__ */
361
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700362#endif /* RFKILL_H */