blob: e6a0031d1b1fdc1793200d1fc93f48bf1973ecfc [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001/*
Ivo van Doornfe242cf2007-09-27 14:57:05 -07002 * Copyright (C) 2006 - 2007 Ivo van Doorn
Ivo van Doorncf4328c2007-05-07 00:34:20 -07003 * Copyright (C) 2007 Dmitry Torokhov
Johannes Berg19d337d2009-06-02 13:01:37 +02004 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
Ivo van Doorncf4328c2007-05-07 00:34:20 -07005 *
Johannes Berg8bc11b492009-08-26 18:13:17 +02006 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
Ivo van Doorncf4328c2007-05-07 00:34:20 -07009 *
Johannes Berg8bc11b492009-08-26 18:13:17 +020010 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Ivo van Doorncf4328c2007-05-07 00:34:20 -070017 */
David Howells607ca462012-10-13 10:46:48 +010018#ifndef __RFKILL_H
19#define __RFKILL_H
Ivo van Doorncf4328c2007-05-07 00:34:20 -070020
David Howells607ca462012-10-13 10:46:48 +010021#include <uapi/linux/rfkill.h>
Johannes Berg19d337d2009-06-02 13:01:37 +020022
Johannes Berg19d337d2009-06-02 13:01:37 +020023/* don't allow anyone to use these in the kernel */
24enum rfkill_user_states {
25 RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED,
26 RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED,
27 RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED,
28};
29#undef RFKILL_STATE_SOFT_BLOCKED
30#undef RFKILL_STATE_UNBLOCKED
31#undef RFKILL_STATE_HARD_BLOCKED
32
Ivo van Doorncf4328c2007-05-07 00:34:20 -070033#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/mutex.h>
Michael Buesch135900c2007-09-27 21:33:12 +020036#include <linux/leds.h>
Johannes Berg1506e302009-06-09 17:49:06 -070037#include <linux/err.h>
Ivo van Doorncf4328c2007-05-07 00:34:20 -070038
Paul Gortmaker313162d2012-01-30 11:46:54 -050039struct device;
Johannes Berg19d337d2009-06-02 13:01:37 +020040/* this is opaque */
41struct rfkill;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070042
43/**
Johannes Berg19d337d2009-06-02 13:01:37 +020044 * struct rfkill_ops - rfkill driver methods
Ivo van Doorncf4328c2007-05-07 00:34:20 -070045 *
Johannes Berg19d337d2009-06-02 13:01:37 +020046 * @poll: poll the rfkill block state(s) -- only assign this method
47 * when you need polling. When called, simply call one of the
48 * rfkill_set{,_hw,_sw}_state family of functions. If the hw
49 * is getting unblocked you need to take into account the return
50 * value of those functions to make sure the software block is
51 * properly used.
52 * @query: query the rfkill block state(s) and call exactly one of the
53 * rfkill_set{,_hw,_sw}_state family of functions. Assign this
54 * method if input events can cause hardware state changes to make
55 * the rfkill core query your driver before setting a requested
56 * block.
57 * @set_block: turn the transmitter on (blocked == false) or off
Johannes Bergc64fb012009-06-02 13:01:38 +020058 * (blocked == true) -- ignore and return 0 when hard blocked.
Johannes Berg19d337d2009-06-02 13:01:37 +020059 * This callback must be assigned.
Ivo van Doorncf4328c2007-05-07 00:34:20 -070060 */
Johannes Berg19d337d2009-06-02 13:01:37 +020061struct rfkill_ops {
62 void (*poll)(struct rfkill *rfkill, void *data);
63 void (*query)(struct rfkill *rfkill, void *data);
64 int (*set_block)(void *data, bool blocked);
Ivo van Doorncf4328c2007-05-07 00:34:20 -070065};
Ivo van Doorncf4328c2007-05-07 00:34:20 -070066
Johannes Berg19d337d2009-06-02 13:01:37 +020067#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
68/**
69 * rfkill_alloc - allocate rfkill structure
70 * @name: name of the struct -- the string is not copied internally
71 * @parent: device that has rf switch on it
72 * @type: type of the switch (RFKILL_TYPE_*)
73 * @ops: rfkill methods
74 * @ops_data: data passed to each method
75 *
76 * This function should be called by the transmitter driver to allocate an
77 * rfkill structure. Returns %NULL on failure.
78 */
79struct rfkill * __must_check rfkill_alloc(const char *name,
80 struct device *parent,
81 const enum rfkill_type type,
82 const struct rfkill_ops *ops,
83 void *ops_data);
84
85/**
86 * rfkill_register - Register a rfkill structure.
87 * @rfkill: rfkill structure to be registered
88 *
89 * This function should be called by the transmitter driver to register
Alan Jenkinsb3fa1322009-06-08 13:27:27 +010090 * the rfkill structure. Before calling this function the driver needs
91 * to be ready to service method calls from rfkill.
92 *
Alan Jenkins06d5caf2009-06-16 15:39:51 +010093 * If rfkill_init_sw_state() is not called before registration,
94 * set_block() will be called to initialize the software blocked state
95 * to a default value.
Alan Jenkinsb3fa1322009-06-08 13:27:27 +010096 *
97 * If the hardware blocked state is not set before registration,
98 * it is assumed to be unblocked.
Johannes Berg19d337d2009-06-02 13:01:37 +020099 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300100int __must_check rfkill_register(struct rfkill *rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200101
102/**
103 * rfkill_pause_polling(struct rfkill *rfkill)
104 *
105 * Pause polling -- say transmitter is off for other reasons.
106 * NOTE: not necessary for suspend/resume -- in that case the
Johannes Bergdd21dfc2016-01-20 10:39:23 +0100107 * core stops polling anyway (but will also correctly handle
108 * the case of polling having been paused before suspend.)
Johannes Berg19d337d2009-06-02 13:01:37 +0200109 */
110void rfkill_pause_polling(struct rfkill *rfkill);
111
112/**
113 * rfkill_resume_polling(struct rfkill *rfkill)
114 *
115 * Pause polling -- say transmitter is off for other reasons.
116 * NOTE: not necessary for suspend/resume -- in that case the
117 * core stops polling anyway
118 */
119void rfkill_resume_polling(struct rfkill *rfkill);
120
121
122/**
123 * rfkill_unregister - Unregister a rfkill structure.
124 * @rfkill: rfkill structure to be unregistered
125 *
126 * This function should be called by the network driver during device
127 * teardown to destroy rfkill structure. Until it returns, the driver
128 * needs to be able to service method calls.
129 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700130void rfkill_unregister(struct rfkill *rfkill);
131
Michael Buesch135900c2007-09-27 21:33:12 +0200132/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200133 * rfkill_destroy - free rfkill structure
134 * @rfkill: rfkill structure to be destroyed
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300135 *
Johannes Berg19d337d2009-06-02 13:01:37 +0200136 * Destroys the rfkill structure.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300137 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200138void rfkill_destroy(struct rfkill *rfkill);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300139
140/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200141 * rfkill_set_hw_state - Set the internal rfkill hardware block state
142 * @rfkill: pointer to the rfkill class to modify.
143 * @state: the current hardware block state to set
144 *
145 * rfkill drivers that get events when the hard-blocked state changes
146 * use this function to notify the rfkill core (and through that also
Alan Jenkins908209c2009-06-08 13:12:23 +0100147 * userspace) of the current state. They should also use this after
Johannes Berg19d337d2009-06-02 13:01:37 +0200148 * resume if the state could have changed.
149 *
150 * You need not (but may) call this function if poll_state is assigned.
151 *
152 * This function can be called in any context, even from within rfkill
153 * callbacks.
154 *
155 * The function returns the combined block state (true if transmitter
156 * should be blocked) so that drivers need not keep track of the soft
157 * block state -- which they might not be able to.
Michael Buesch135900c2007-09-27 21:33:12 +0200158 */
Alan Jenkinse56f0972009-07-18 19:20:20 +0100159bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked);
Johannes Berg19d337d2009-06-02 13:01:37 +0200160
161/**
162 * rfkill_set_sw_state - Set the internal rfkill software block state
163 * @rfkill: pointer to the rfkill class to modify.
164 * @state: the current software block state to set
165 *
166 * rfkill drivers that get events when the soft-blocked state changes
167 * (yes, some platforms directly act on input but allow changing again)
168 * use this function to notify the rfkill core (and through that also
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100169 * userspace) of the current state.
170 *
171 * Drivers should also call this function after resume if the state has
172 * been changed by the user. This only makes sense for "persistent"
173 * devices (see rfkill_init_sw_state()).
Johannes Berg19d337d2009-06-02 13:01:37 +0200174 *
175 * This function can be called in any context, even from within rfkill
176 * callbacks.
177 *
178 * The function returns the combined block state (true if transmitter
179 * should be blocked).
180 */
181bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked);
182
183/**
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100184 * rfkill_init_sw_state - Initialize persistent software block state
185 * @rfkill: pointer to the rfkill class to modify.
186 * @state: the current software block state to set
187 *
188 * rfkill drivers that preserve their software block state over power off
189 * use this function to notify the rfkill core (and through that also
190 * userspace) of their initial state. It should only be used before
191 * registration.
192 *
Alan Jenkins464902e2009-06-16 14:54:04 +0100193 * In addition, it marks the device as "persistent", an attribute which
194 * can be read by userspace. Persistent devices are expected to preserve
195 * their own state when suspended.
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100196 */
197void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked);
198
199/**
Johannes Berg19d337d2009-06-02 13:01:37 +0200200 * rfkill_set_states - Set the internal rfkill block states
201 * @rfkill: pointer to the rfkill class to modify.
202 * @sw: the current software block state to set
203 * @hw: the current hardware block state to set
204 *
205 * This function can be called in any context, even from within rfkill
206 * callbacks.
207 */
208void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
209
210/**
Johannes Berg60811622009-06-02 13:01:40 +0200211 * rfkill_blocked - query rfkill block
212 *
213 * @rfkill: rfkill struct to query
214 */
215bool rfkill_blocked(struct rfkill *rfkill);
Heikki Krogerus648b50d2016-01-25 12:03:46 +0300216
217/**
218 * rfkill_find_type - Helpper for finding rfkill type by name
219 * @name: the name of the type
220 *
221 * Returns enum rfkill_type that conrresponds the name.
222 */
223enum rfkill_type rfkill_find_type(const char *name);
224
Johannes Berg19d337d2009-06-02 13:01:37 +0200225#else /* !RFKILL */
226static inline struct rfkill * __must_check
227rfkill_alloc(const char *name,
228 struct device *parent,
229 const enum rfkill_type type,
230 const struct rfkill_ops *ops,
231 void *ops_data)
Michael Buesch135900c2007-09-27 21:33:12 +0200232{
Johannes Berg19d337d2009-06-02 13:01:37 +0200233 return ERR_PTR(-ENODEV);
Michael Buesch135900c2007-09-27 21:33:12 +0200234}
235
Johannes Berg19d337d2009-06-02 13:01:37 +0200236static inline int __must_check rfkill_register(struct rfkill *rfkill)
237{
238 if (rfkill == ERR_PTR(-ENODEV))
239 return 0;
240 return -EINVAL;
241}
242
243static inline void rfkill_pause_polling(struct rfkill *rfkill)
244{
245}
246
247static inline void rfkill_resume_polling(struct rfkill *rfkill)
248{
249}
250
251static inline void rfkill_unregister(struct rfkill *rfkill)
252{
253}
254
255static inline void rfkill_destroy(struct rfkill *rfkill)
256{
257}
258
259static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
260{
261 return blocked;
262}
263
264static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
265{
266 return blocked;
267}
268
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100269static inline void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
270{
271}
272
Johannes Berg19d337d2009-06-02 13:01:37 +0200273static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
274{
275}
276
Johannes Berg60811622009-06-02 13:01:40 +0200277static inline bool rfkill_blocked(struct rfkill *rfkill)
278{
279 return false;
280}
Heikki Krogerus648b50d2016-01-25 12:03:46 +0300281
282static inline enum rfkill_type rfkill_find_type(const char *name)
283{
284 return RFKILL_TYPE_ALL;
285}
286
Johannes Berg19d337d2009-06-02 13:01:37 +0200287#endif /* RFKILL || RFKILL_MODULE */
288
AceLan Kao06d7de82012-07-26 09:51:08 +0800289
290#ifdef CONFIG_RFKILL_LEDS
291/**
292 * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED.
293 * This function might return a NULL pointer if registering of the
294 * LED trigger failed. Use this as "default_trigger" for the LED.
295 */
296const char *rfkill_get_led_trigger_name(struct rfkill *rfkill);
297
298/**
299 * rfkill_set_led_trigger_name -- set the LED trigger name
300 * @rfkill: rfkill struct
301 * @name: LED trigger name
302 *
303 * This function sets the LED trigger name of the radio LED
304 * trigger that rfkill creates. It is optional, but if called
305 * must be called before rfkill_register() to be effective.
306 */
307void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name);
308#else
309static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
310{
311 return NULL;
312}
313
314static inline void
315rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
316{
317}
318#endif
319
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700320#endif /* RFKILL_H */