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