2019-05-19 16:51:43 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2009-06-02 15:01:37 +04:00
/*
* Copyright ( C ) 2006 - 2007 Ivo van Doorn
* Copyright ( C ) 2007 Dmitry Torokhov
* Copyright 2009 Johannes Berg < johannes @ sipsolutions . net >
*/
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/init.h>
# include <linux/workqueue.h>
# include <linux/capability.h>
# include <linux/list.h>
# include <linux/mutex.h>
# include <linux/rfkill.h>
2009-10-04 16:11:37 +04:00
# include <linux/sched.h>
2009-06-02 15:01:37 +04:00
# include <linux/spinlock.h>
2012-01-22 20:23:42 +04:00
# include <linux/device.h>
2009-06-02 15:01:38 +04:00
# include <linux/miscdevice.h>
# include <linux/wait.h>
# include <linux/poll.h>
# include <linux/fs.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2009-06-02 15:01:37 +04:00
# include "rfkill.h"
# define POLL_INTERVAL (5 * HZ)
# define RFKILL_BLOCK_HW BIT(0)
# define RFKILL_BLOCK_SW BIT(1)
# define RFKILL_BLOCK_SW_PREV BIT(2)
# define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
RFKILL_BLOCK_SW | \
RFKILL_BLOCK_SW_PREV )
# define RFKILL_BLOCK_SW_SETCALL BIT(31)
struct rfkill {
spinlock_t lock ;
enum rfkill_type type ;
unsigned long state ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
unsigned long hard_block_reasons ;
2009-06-02 15:01:37 +04:00
2009-06-02 15:01:38 +04:00
u32 idx ;
2009-06-02 15:01:37 +04:00
bool registered ;
2009-06-08 16:27:27 +04:00
bool persistent ;
2016-01-20 12:39:23 +03:00
bool polling_paused ;
bool suspended ;
2009-06-02 15:01:37 +04:00
const struct rfkill_ops * ops ;
void * data ;
# ifdef CONFIG_RFKILL_LEDS
struct led_trigger led_trigger ;
const char * ledtrigname ;
# endif
struct device dev ;
struct list_head node ;
struct delayed_work poll_work ;
struct work_struct uevent_work ;
struct work_struct sync_work ;
2015-12-10 12:37:51 +03:00
char name [ ] ;
2009-06-02 15:01:37 +04:00
} ;
# define to_rfkill(d) container_of(d, struct rfkill, dev)
2009-06-02 15:01:38 +04:00
struct rfkill_int_event {
struct list_head list ;
rfkill: revert back to old userspace API by default
Recompiling with the new extended version of struct rfkill_event
broke systemd in *two* ways:
- It used "sizeof(struct rfkill_event)" to read the event, but
then complained if it actually got something != 8, this broke
it on new kernels (that include the updated API);
- It used sizeof(struct rfkill_event) to write a command, but
didn't implement the intended expansion protocol where the
kernel returns only how many bytes it accepted, and errored
out due to the unexpected smaller size on kernels that didn't
include the updated API.
Even though systemd has now been fixed, that fix may not be always
deployed, and other applications could potentially have similar
issues.
As such, in the interest of avoiding regressions, revert the
default API "struct rfkill_event" back to the original size.
Instead, add a new "struct rfkill_event_ext" that extends it by
the new field, and even more clearly document that applications
should be prepared for extensions in two ways:
* write might only accept fewer bytes on older kernels, and
will return how many to let userspace know which data may
have been ignored;
* read might return anything between 8 (the original size) and
whatever size the application sized its buffer at, indicating
how much event data was supported by the kernel.
Perhaps that will help avoid such issues in the future and we
won't have to come up with another version of the struct if we
ever need to extend it again.
Applications that want to take advantage of the new field will
have to be modified to use struct rfkill_event_ext instead now,
which comes with the danger of them having already been updated
to use it from 'struct rfkill_event', but I found no evidence
of that, and it's still relatively new.
Cc: stable@vger.kernel.org # 5.11
Reported-by: Takashi Iwai <tiwai@suse.de>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM/Clang v12.0.0-r4 (x86-64)
Link: https://lore.kernel.org/r/20210319232510.f1a139cfdd9c.Ic5c7c9d1d28972059e132ea653a21a427c326678@changeid
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2021-03-20 01:25:11 +03:00
struct rfkill_event_ext ev ;
2009-06-02 15:01:38 +04:00
} ;
struct rfkill_data {
struct list_head list ;
struct list_head events ;
struct mutex mtx ;
wait_queue_head_t read_wait ;
bool input_handler ;
2022-03-16 23:27:51 +03:00
u8 max_size ;
2009-06-02 15:01:38 +04:00
} ;
2009-06-02 15:01:37 +04:00
MODULE_AUTHOR ( " Ivo van Doorn <IvDoorn@gmail.com> " ) ;
MODULE_AUTHOR ( " Johannes Berg <johannes@sipsolutions.net> " ) ;
MODULE_DESCRIPTION ( " RF switch support " ) ;
MODULE_LICENSE ( " GPL " ) ;
/*
* The locking here should be made much smarter , we currently have
* a bit of a stupid situation because drivers might want to register
* the rfkill struct under their own lock , and take this lock during
* rfkill method calls - - which will cause an AB - BA deadlock situation .
*
* To fix that , we need to rework this code here to be mostly lock - free
* and only use the mutex for list manipulations , not to protect the
* various other global variables . Then we can avoid holding the mutex
* around driver operations , and all is happy .
*/
static LIST_HEAD ( rfkill_list ) ; /* list of registered rf switches */
static DEFINE_MUTEX ( rfkill_global_mutex ) ;
2009-06-02 15:01:38 +04:00
static LIST_HEAD ( rfkill_fds ) ; /* list of open fds of /dev/rfkill */
2009-06-02 15:01:37 +04:00
static unsigned int rfkill_default_state = 1 ;
module_param_named ( default_state , rfkill_default_state , uint , 0444 ) ;
MODULE_PARM_DESC ( default_state ,
" Default initial state for all radio types, 0 = radio off " ) ;
static struct {
2009-06-08 16:27:27 +04:00
bool cur , sav ;
2009-06-02 15:01:37 +04:00
} rfkill_global_states [ NUM_RFKILL_TYPES ] ;
static bool rfkill_epo_lock_active ;
# ifdef CONFIG_RFKILL_LEDS
static void rfkill_led_trigger_event ( struct rfkill * rfkill )
{
struct led_trigger * trigger ;
if ( ! rfkill - > registered )
return ;
trigger = & rfkill - > led_trigger ;
if ( rfkill - > state & RFKILL_BLOCK_ANY )
led_trigger_event ( trigger , LED_OFF ) ;
else
led_trigger_event ( trigger , LED_FULL ) ;
}
2018-07-02 23:05:21 +03:00
static int rfkill_led_trigger_activate ( struct led_classdev * led )
2009-06-02 15:01:37 +04:00
{
struct rfkill * rfkill ;
rfkill = container_of ( led - > trigger , struct rfkill , led_trigger ) ;
rfkill_led_trigger_event ( rfkill ) ;
2018-07-02 23:05:21 +03:00
return 0 ;
2009-06-02 15:01:37 +04:00
}
2012-07-26 05:51:08 +04:00
const char * rfkill_get_led_trigger_name ( struct rfkill * rfkill )
{
return rfkill - > led_trigger . name ;
}
EXPORT_SYMBOL ( rfkill_get_led_trigger_name ) ;
void rfkill_set_led_trigger_name ( struct rfkill * rfkill , const char * name )
{
BUG_ON ( ! rfkill ) ;
rfkill - > ledtrigname = name ;
}
EXPORT_SYMBOL ( rfkill_set_led_trigger_name ) ;
2009-06-02 15:01:37 +04:00
static int rfkill_led_trigger_register ( struct rfkill * rfkill )
{
rfkill - > led_trigger . name = rfkill - > ledtrigname
? : dev_name ( & rfkill - > dev ) ;
rfkill - > led_trigger . activate = rfkill_led_trigger_activate ;
return led_trigger_register ( & rfkill - > led_trigger ) ;
}
static void rfkill_led_trigger_unregister ( struct rfkill * rfkill )
{
led_trigger_unregister ( & rfkill - > led_trigger ) ;
}
2017-01-06 09:07:47 +03:00
static struct led_trigger rfkill_any_led_trigger ;
2018-05-23 00:29:32 +03:00
static struct led_trigger rfkill_none_led_trigger ;
2018-05-23 00:29:31 +03:00
static struct work_struct rfkill_global_led_trigger_work ;
2017-01-06 09:07:47 +03:00
2018-05-23 00:29:31 +03:00
static void rfkill_global_led_trigger_worker ( struct work_struct * work )
2017-01-06 09:07:47 +03:00
{
enum led_brightness brightness = LED_OFF ;
struct rfkill * rfkill ;
mutex_lock ( & rfkill_global_mutex ) ;
list_for_each_entry ( rfkill , & rfkill_list , node ) {
if ( ! ( rfkill - > state & RFKILL_BLOCK_ANY ) ) {
brightness = LED_FULL ;
break ;
}
}
mutex_unlock ( & rfkill_global_mutex ) ;
led_trigger_event ( & rfkill_any_led_trigger , brightness ) ;
2018-05-23 00:29:32 +03:00
led_trigger_event ( & rfkill_none_led_trigger ,
brightness = = LED_OFF ? LED_FULL : LED_OFF ) ;
2017-01-06 09:07:47 +03:00
}
2018-05-23 00:29:31 +03:00
static void rfkill_global_led_trigger_event ( void )
2017-01-06 09:07:47 +03:00
{
2018-05-23 00:29:31 +03:00
schedule_work ( & rfkill_global_led_trigger_work ) ;
2017-01-06 09:07:47 +03:00
}
2018-05-23 00:29:31 +03:00
static int rfkill_global_led_trigger_register ( void )
2017-01-06 09:07:47 +03:00
{
2018-05-23 00:29:32 +03:00
int ret ;
2018-05-23 00:29:31 +03:00
INIT_WORK ( & rfkill_global_led_trigger_work ,
rfkill_global_led_trigger_worker ) ;
2018-05-23 00:29:32 +03:00
2017-01-06 09:07:47 +03:00
rfkill_any_led_trigger . name = " rfkill-any " ;
2018-05-23 00:29:32 +03:00
ret = led_trigger_register ( & rfkill_any_led_trigger ) ;
if ( ret )
return ret ;
rfkill_none_led_trigger . name = " rfkill-none " ;
ret = led_trigger_register ( & rfkill_none_led_trigger ) ;
if ( ret )
led_trigger_unregister ( & rfkill_any_led_trigger ) ;
else
/* Delay activation until all global triggers are registered */
rfkill_global_led_trigger_event ( ) ;
return ret ;
2017-01-06 09:07:47 +03:00
}
2018-05-23 00:29:31 +03:00
static void rfkill_global_led_trigger_unregister ( void )
2017-01-06 09:07:47 +03:00
{
2018-05-23 00:29:32 +03:00
led_trigger_unregister ( & rfkill_none_led_trigger ) ;
2017-01-06 09:07:47 +03:00
led_trigger_unregister ( & rfkill_any_led_trigger ) ;
2018-05-23 00:29:31 +03:00
cancel_work_sync ( & rfkill_global_led_trigger_work ) ;
2017-01-06 09:07:47 +03:00
}
2009-06-02 15:01:37 +04:00
# else
static void rfkill_led_trigger_event ( struct rfkill * rfkill )
{
}
static inline int rfkill_led_trigger_register ( struct rfkill * rfkill )
{
return 0 ;
}
static inline void rfkill_led_trigger_unregister ( struct rfkill * rfkill )
{
}
2017-01-06 09:07:47 +03:00
2018-05-23 00:29:31 +03:00
static void rfkill_global_led_trigger_event ( void )
2017-01-06 09:07:47 +03:00
{
}
2018-05-23 00:29:31 +03:00
static int rfkill_global_led_trigger_register ( void )
2017-01-06 09:07:47 +03:00
{
return 0 ;
}
2018-05-23 00:29:31 +03:00
static void rfkill_global_led_trigger_unregister ( void )
2017-01-06 09:07:47 +03:00
{
}
2009-06-02 15:01:37 +04:00
# endif /* CONFIG_RFKILL_LEDS */
rfkill: revert back to old userspace API by default
Recompiling with the new extended version of struct rfkill_event
broke systemd in *two* ways:
- It used "sizeof(struct rfkill_event)" to read the event, but
then complained if it actually got something != 8, this broke
it on new kernels (that include the updated API);
- It used sizeof(struct rfkill_event) to write a command, but
didn't implement the intended expansion protocol where the
kernel returns only how many bytes it accepted, and errored
out due to the unexpected smaller size on kernels that didn't
include the updated API.
Even though systemd has now been fixed, that fix may not be always
deployed, and other applications could potentially have similar
issues.
As such, in the interest of avoiding regressions, revert the
default API "struct rfkill_event" back to the original size.
Instead, add a new "struct rfkill_event_ext" that extends it by
the new field, and even more clearly document that applications
should be prepared for extensions in two ways:
* write might only accept fewer bytes on older kernels, and
will return how many to let userspace know which data may
have been ignored;
* read might return anything between 8 (the original size) and
whatever size the application sized its buffer at, indicating
how much event data was supported by the kernel.
Perhaps that will help avoid such issues in the future and we
won't have to come up with another version of the struct if we
ever need to extend it again.
Applications that want to take advantage of the new field will
have to be modified to use struct rfkill_event_ext instead now,
which comes with the danger of them having already been updated
to use it from 'struct rfkill_event', but I found no evidence
of that, and it's still relatively new.
Cc: stable@vger.kernel.org # 5.11
Reported-by: Takashi Iwai <tiwai@suse.de>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM/Clang v12.0.0-r4 (x86-64)
Link: https://lore.kernel.org/r/20210319232510.f1a139cfdd9c.Ic5c7c9d1d28972059e132ea653a21a427c326678@changeid
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2021-03-20 01:25:11 +03:00
static void rfkill_fill_event ( struct rfkill_event_ext * ev ,
struct rfkill * rfkill ,
2009-06-02 15:01:38 +04:00
enum rfkill_operation op )
{
unsigned long flags ;
ev - > idx = rfkill - > idx ;
ev - > type = rfkill - > type ;
ev - > op = op ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
ev - > hard = ! ! ( rfkill - > state & RFKILL_BLOCK_HW ) ;
ev - > soft = ! ! ( rfkill - > state & ( RFKILL_BLOCK_SW |
RFKILL_BLOCK_SW_PREV ) ) ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
ev - > hard_block_reasons = rfkill - > hard_block_reasons ;
2009-06-02 15:01:38 +04:00
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
}
static void rfkill_send_events ( struct rfkill * rfkill , enum rfkill_operation op )
{
struct rfkill_data * data ;
struct rfkill_int_event * ev ;
list_for_each_entry ( data , & rfkill_fds , list ) {
ev = kzalloc ( sizeof ( * ev ) , GFP_KERNEL ) ;
if ( ! ev )
continue ;
rfkill_fill_event ( & ev - > ev , rfkill , op ) ;
mutex_lock ( & data - > mtx ) ;
list_add_tail ( & ev - > list , & data - > events ) ;
mutex_unlock ( & data - > mtx ) ;
wake_up_interruptible ( & data - > read_wait ) ;
}
}
static void rfkill_event ( struct rfkill * rfkill )
2009-06-02 15:01:37 +04:00
{
2009-06-16 18:39:51 +04:00
if ( ! rfkill - > registered )
2009-06-02 15:01:37 +04:00
return ;
kobject_uevent ( & rfkill - > dev . kobj , KOBJ_CHANGE ) ;
2009-06-02 15:01:38 +04:00
/* also send event to /dev/rfkill */
rfkill_send_events ( rfkill , RFKILL_OP_CHANGE ) ;
2009-06-02 15:01:37 +04:00
}
/**
* rfkill_set_block - wrapper for set_block method
*
* @ rfkill : the rfkill struct to use
* @ blocked : the new software state
*
* Calls the set_block method ( when applicable ) and handles notifications
* etc . as well .
*/
static void rfkill_set_block ( struct rfkill * rfkill , bool blocked )
{
unsigned long flags ;
2012-09-06 18:06:52 +04:00
bool prev , curr ;
2009-06-02 15:01:37 +04:00
int err ;
2009-06-16 17:53:24 +04:00
if ( unlikely ( rfkill - > dev . power . power_state . event & PM_EVENT_SLEEP ) )
return ;
2009-06-02 15:01:37 +04:00
/*
* Some platforms ( . . . ! ) generate input events which affect the
* _hard_ kill state - - whenever something tries to change the
* current software state query the hardware state too .
*/
if ( rfkill - > ops - > query )
rfkill - > ops - > query ( rfkill , rfkill - > data ) ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
2012-09-06 18:06:52 +04:00
prev = rfkill - > state & RFKILL_BLOCK_SW ;
2016-01-19 18:42:37 +03:00
if ( prev )
2009-06-02 15:01:37 +04:00
rfkill - > state | = RFKILL_BLOCK_SW_PREV ;
else
rfkill - > state & = ~ RFKILL_BLOCK_SW_PREV ;
if ( blocked )
rfkill - > state | = RFKILL_BLOCK_SW ;
else
rfkill - > state & = ~ RFKILL_BLOCK_SW ;
rfkill - > state | = RFKILL_BLOCK_SW_SETCALL ;
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
err = rfkill - > ops - > set_block ( rfkill - > data , blocked ) ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
if ( err ) {
/*
2016-02-22 19:36:32 +03:00
* Failed - - reset status to _PREV , which may be different
* from what we have set _PREV to earlier in this function
2009-06-02 15:01:37 +04:00
* if rfkill_set_sw_state was invoked .
*/
if ( rfkill - > state & RFKILL_BLOCK_SW_PREV )
rfkill - > state | = RFKILL_BLOCK_SW ;
else
rfkill - > state & = ~ RFKILL_BLOCK_SW ;
}
rfkill - > state & = ~ RFKILL_BLOCK_SW_SETCALL ;
rfkill - > state & = ~ RFKILL_BLOCK_SW_PREV ;
2012-09-06 18:06:52 +04:00
curr = rfkill - > state & RFKILL_BLOCK_SW ;
2009-06-02 15:01:37 +04:00
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
rfkill_led_trigger_event ( rfkill ) ;
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_event ( ) ;
2012-09-06 18:06:52 +04:00
if ( prev ! = curr )
rfkill_event ( rfkill ) ;
2009-06-02 15:01:37 +04:00
}
2016-02-22 19:36:36 +03:00
static void rfkill_update_global_state ( enum rfkill_type type , bool blocked )
{
int i ;
if ( type ! = RFKILL_TYPE_ALL ) {
rfkill_global_states [ type ] . cur = blocked ;
return ;
}
for ( i = 0 ; i < NUM_RFKILL_TYPES ; i + + )
rfkill_global_states [ i ] . cur = blocked ;
}
2009-06-02 15:01:38 +04:00
# ifdef CONFIG_RFKILL_INPUT
static atomic_t rfkill_input_disabled = ATOMIC_INIT ( 0 ) ;
2009-06-02 15:01:37 +04:00
/**
* __rfkill_switch_all - Toggle state of all switches of given type
* @ type : type of interfaces to be affected
2014-10-08 00:20:23 +04:00
* @ blocked : the new state
2009-06-02 15:01:37 +04:00
*
* This function sets the state of all switches of given type ,
2016-01-19 18:42:39 +03:00
* unless a specific switch is suspended .
2009-06-02 15:01:37 +04:00
*
* Caller must have acquired rfkill_global_mutex .
*/
static void __rfkill_switch_all ( const enum rfkill_type type , bool blocked )
{
struct rfkill * rfkill ;
2016-02-22 19:36:36 +03:00
rfkill_update_global_state ( type , blocked ) ;
2009-06-02 15:01:37 +04:00
list_for_each_entry ( rfkill , & rfkill_list , node ) {
2012-05-23 10:11:52 +04:00
if ( rfkill - > type ! = type & & type ! = RFKILL_TYPE_ALL )
2009-06-02 15:01:37 +04:00
continue ;
rfkill_set_block ( rfkill , blocked ) ;
}
}
/**
* rfkill_switch_all - Toggle state of all switches of given type
* @ type : type of interfaces to be affected
2014-10-08 00:20:23 +04:00
* @ blocked : the new state
2009-06-02 15:01:37 +04:00
*
* Acquires rfkill_global_mutex and calls __rfkill_switch_all ( @ type , @ state ) .
* Please refer to __rfkill_switch_all ( ) for details .
*
* Does nothing if the EPO lock is active .
*/
void rfkill_switch_all ( enum rfkill_type type , bool blocked )
{
2009-06-02 15:01:38 +04:00
if ( atomic_read ( & rfkill_input_disabled ) )
return ;
2009-06-02 15:01:37 +04:00
mutex_lock ( & rfkill_global_mutex ) ;
if ( ! rfkill_epo_lock_active )
__rfkill_switch_all ( type , blocked ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
}
/**
* rfkill_epo - emergency power off all transmitters
*
* This kicks all non - suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED ,
* ignoring everything in its path but rfkill_global_mutex and rfkill - > mutex .
*
* The global state before the EPO is saved and can be restored later
* using rfkill_restore_states ( ) .
*/
void rfkill_epo ( void )
{
struct rfkill * rfkill ;
int i ;
2009-06-02 15:01:38 +04:00
if ( atomic_read ( & rfkill_input_disabled ) )
return ;
2009-06-02 15:01:37 +04:00
mutex_lock ( & rfkill_global_mutex ) ;
rfkill_epo_lock_active = true ;
list_for_each_entry ( rfkill , & rfkill_list , node )
rfkill_set_block ( rfkill , true ) ;
for ( i = 0 ; i < NUM_RFKILL_TYPES ; i + + ) {
2009-06-08 16:27:27 +04:00
rfkill_global_states [ i ] . sav = rfkill_global_states [ i ] . cur ;
2009-06-02 15:01:37 +04:00
rfkill_global_states [ i ] . cur = true ;
}
2009-06-02 15:01:38 +04:00
2009-06-02 15:01:37 +04:00
mutex_unlock ( & rfkill_global_mutex ) ;
}
/**
* rfkill_restore_states - restore global states
*
* Restore ( and sync switches to ) the global state from the
* states in rfkill_default_states . This can undo the effects of
* a call to rfkill_epo ( ) .
*/
void rfkill_restore_states ( void )
{
int i ;
2009-06-02 15:01:38 +04:00
if ( atomic_read ( & rfkill_input_disabled ) )
return ;
2009-06-02 15:01:37 +04:00
mutex_lock ( & rfkill_global_mutex ) ;
rfkill_epo_lock_active = false ;
for ( i = 0 ; i < NUM_RFKILL_TYPES ; i + + )
2009-06-08 16:27:27 +04:00
__rfkill_switch_all ( i , rfkill_global_states [ i ] . sav ) ;
2009-06-02 15:01:37 +04:00
mutex_unlock ( & rfkill_global_mutex ) ;
}
/**
* rfkill_remove_epo_lock - unlock state changes
*
* Used by rfkill - input manually unlock state changes , when
* the EPO switch is deactivated .
*/
void rfkill_remove_epo_lock ( void )
{
2009-06-02 15:01:38 +04:00
if ( atomic_read ( & rfkill_input_disabled ) )
return ;
2009-06-02 15:01:37 +04:00
mutex_lock ( & rfkill_global_mutex ) ;
rfkill_epo_lock_active = false ;
mutex_unlock ( & rfkill_global_mutex ) ;
}
/**
* rfkill_is_epo_lock_active - returns true EPO is active
*
2018-07-23 22:41:38 +03:00
* Returns 0 ( false ) if there is NOT an active EPO condition ,
* and 1 ( true ) if there is an active EPO condition , which
2009-06-02 15:01:37 +04:00
* locks all radios in one of the BLOCKED states .
*
* Can be called in atomic context .
*/
bool rfkill_is_epo_lock_active ( void )
{
return rfkill_epo_lock_active ;
}
/**
* rfkill_get_global_sw_state - returns global state for a type
* @ type : the type to get the global state of
*
* Returns the current global state for a given wireless
* device type .
*/
bool rfkill_get_global_sw_state ( const enum rfkill_type type )
{
return rfkill_global_states [ type ] . cur ;
}
2009-06-02 15:01:38 +04:00
# endif
2009-06-02 15:01:37 +04:00
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
bool rfkill_set_hw_state_reason ( struct rfkill * rfkill ,
bool blocked , unsigned long reason )
2009-06-02 15:01:37 +04:00
{
2016-01-19 18:42:38 +03:00
unsigned long flags ;
bool ret , prev ;
BUG_ON ( ! rfkill ) ;
2009-06-02 15:01:37 +04:00
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
if ( WARN ( reason &
~ ( RFKILL_HARD_BLOCK_SIGNAL | RFKILL_HARD_BLOCK_NOT_OWNER ) ,
" hw_state reason not supported: 0x%lx " , reason ) )
return blocked ;
2016-01-19 18:42:38 +03:00
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
prev = ! ! ( rfkill - > hard_block_reasons & reason ) ;
if ( blocked ) {
2016-01-19 18:42:38 +03:00
rfkill - > state | = RFKILL_BLOCK_HW ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
rfkill - > hard_block_reasons | = reason ;
} else {
rfkill - > hard_block_reasons & = ~ reason ;
if ( ! rfkill - > hard_block_reasons )
rfkill - > state & = ~ RFKILL_BLOCK_HW ;
}
2016-01-19 18:42:38 +03:00
ret = ! ! ( rfkill - > state & RFKILL_BLOCK_ANY ) ;
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
2009-06-02 15:01:37 +04:00
2016-01-19 18:42:38 +03:00
rfkill_led_trigger_event ( rfkill ) ;
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_event ( ) ;
2009-06-02 15:01:37 +04:00
2016-12-13 11:38:25 +03:00
if ( rfkill - > registered & & prev ! = blocked )
2009-06-02 15:01:37 +04:00
schedule_work ( & rfkill - > uevent_work ) ;
return ret ;
}
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
EXPORT_SYMBOL ( rfkill_set_hw_state_reason ) ;
2009-06-02 15:01:37 +04:00
static void __rfkill_set_sw_state ( struct rfkill * rfkill , bool blocked )
{
u32 bit = RFKILL_BLOCK_SW ;
/* if in a ops->set_block right now, use other bit */
if ( rfkill - > state & RFKILL_BLOCK_SW_SETCALL )
bit = RFKILL_BLOCK_SW_PREV ;
if ( blocked )
rfkill - > state | = bit ;
else
rfkill - > state & = ~ bit ;
}
bool rfkill_set_sw_state ( struct rfkill * rfkill , bool blocked )
{
unsigned long flags ;
bool prev , hwblock ;
BUG_ON ( ! rfkill ) ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
prev = ! ! ( rfkill - > state & RFKILL_BLOCK_SW ) ;
__rfkill_set_sw_state ( rfkill , blocked ) ;
hwblock = ! ! ( rfkill - > state & RFKILL_BLOCK_HW ) ;
blocked = blocked | | hwblock ;
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
2009-06-16 18:39:51 +04:00
if ( ! rfkill - > registered )
return blocked ;
2009-06-02 15:01:37 +04:00
2009-06-16 18:39:51 +04:00
if ( prev ! = blocked & & ! hwblock )
schedule_work ( & rfkill - > uevent_work ) ;
rfkill_led_trigger_event ( rfkill ) ;
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_event ( ) ;
2009-06-02 15:01:37 +04:00
return blocked ;
}
EXPORT_SYMBOL ( rfkill_set_sw_state ) ;
2009-06-16 18:39:51 +04:00
void rfkill_init_sw_state ( struct rfkill * rfkill , bool blocked )
{
unsigned long flags ;
BUG_ON ( ! rfkill ) ;
BUG_ON ( rfkill - > registered ) ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
__rfkill_set_sw_state ( rfkill , blocked ) ;
rfkill - > persistent = true ;
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
}
EXPORT_SYMBOL ( rfkill_init_sw_state ) ;
2009-06-02 15:01:37 +04:00
void rfkill_set_states ( struct rfkill * rfkill , bool sw , bool hw )
{
unsigned long flags ;
bool swprev , hwprev ;
BUG_ON ( ! rfkill ) ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
/*
* No need to care about prev / setblock . . . this is for uevent only
* and that will get triggered by rfkill_set_block anyway .
*/
swprev = ! ! ( rfkill - > state & RFKILL_BLOCK_SW ) ;
hwprev = ! ! ( rfkill - > state & RFKILL_BLOCK_HW ) ;
__rfkill_set_sw_state ( rfkill , sw ) ;
2009-07-12 20:03:13 +04:00
if ( hw )
rfkill - > state | = RFKILL_BLOCK_HW ;
else
rfkill - > state & = ~ RFKILL_BLOCK_HW ;
2009-06-02 15:01:37 +04:00
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
2009-06-08 16:27:27 +04:00
if ( ! rfkill - > registered ) {
rfkill - > persistent = true ;
} else {
if ( swprev ! = sw | | hwprev ! = hw )
schedule_work ( & rfkill - > uevent_work ) ;
2009-06-02 15:01:37 +04:00
2009-06-08 16:27:27 +04:00
rfkill_led_trigger_event ( rfkill ) ;
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_event ( ) ;
2009-06-08 16:27:27 +04:00
}
2009-06-02 15:01:37 +04:00
}
EXPORT_SYMBOL ( rfkill_set_states ) ;
2016-01-25 12:03:46 +03:00
static const char * const rfkill_types [ ] = {
NULL , /* RFKILL_TYPE_ALL */
" wlan " ,
" bluetooth " ,
" ultrawideband " ,
" wimax " ,
" wwan " ,
" gps " ,
" fm " ,
" nfc " ,
} ;
enum rfkill_type rfkill_find_type ( const char * name )
{
int i ;
BUILD_BUG_ON ( ARRAY_SIZE ( rfkill_types ) ! = NUM_RFKILL_TYPES ) ;
if ( ! name )
return RFKILL_TYPE_ALL ;
for ( i = 1 ; i < NUM_RFKILL_TYPES ; i + + )
if ( ! strcmp ( name , rfkill_types [ i ] ) )
return i ;
return RFKILL_TYPE_ALL ;
}
EXPORT_SYMBOL ( rfkill_find_type ) ;
2013-07-25 02:05:36 +04:00
static ssize_t name_show ( struct device * dev , struct device_attribute * attr ,
char * buf )
2009-06-02 15:01:37 +04:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
return sprintf ( buf , " %s \n " , rfkill - > name ) ;
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RO ( name ) ;
2009-06-02 15:01:37 +04:00
2013-07-25 02:05:36 +04:00
static ssize_t type_show ( struct device * dev , struct device_attribute * attr ,
char * buf )
2009-06-02 15:01:37 +04:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
2016-01-25 12:03:46 +03:00
return sprintf ( buf , " %s \n " , rfkill_types [ rfkill - > type ] ) ;
2009-06-02 15:01:37 +04:00
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RO ( type ) ;
2009-06-02 15:01:37 +04:00
2013-07-25 02:05:36 +04:00
static ssize_t index_show ( struct device * dev , struct device_attribute * attr ,
char * buf )
2009-06-02 15:01:38 +04:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
return sprintf ( buf , " %d \n " , rfkill - > idx ) ;
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RO ( index ) ;
2009-06-02 15:01:38 +04:00
2013-07-25 02:05:36 +04:00
static ssize_t persistent_show ( struct device * dev ,
struct device_attribute * attr , char * buf )
2009-06-16 17:54:04 +04:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
return sprintf ( buf , " %d \n " , rfkill - > persistent ) ;
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RO ( persistent ) ;
2009-06-16 17:54:04 +04:00
2013-07-25 02:05:36 +04:00
static ssize_t hard_show ( struct device * dev , struct device_attribute * attr ,
char * buf )
2010-02-26 14:01:34 +03:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
2010-03-13 15:31:05 +03:00
return sprintf ( buf , " %d \n " , ( rfkill - > state & RFKILL_BLOCK_HW ) ? 1 : 0 ) ;
2010-02-26 14:01:34 +03:00
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RO ( hard ) ;
2010-02-26 14:01:34 +03:00
2013-07-25 02:05:36 +04:00
static ssize_t soft_show ( struct device * dev , struct device_attribute * attr ,
char * buf )
2010-02-26 14:01:34 +03:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
2010-03-13 15:31:05 +03:00
return sprintf ( buf , " %d \n " , ( rfkill - > state & RFKILL_BLOCK_SW ) ? 1 : 0 ) ;
2010-02-26 14:01:34 +03:00
}
2013-07-25 02:05:36 +04:00
static ssize_t soft_store ( struct device * dev , struct device_attribute * attr ,
const char * buf , size_t count )
2010-02-26 14:01:34 +03:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
unsigned long state ;
int err ;
if ( ! capable ( CAP_NET_ADMIN ) )
return - EPERM ;
2011-11-06 17:26:49 +04:00
err = kstrtoul ( buf , 0 , & state ) ;
2010-02-26 14:01:34 +03:00
if ( err )
return err ;
if ( state > 1 )
return - EINVAL ;
mutex_lock ( & rfkill_global_mutex ) ;
rfkill_set_block ( rfkill , state ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
2012-10-25 18:18:34 +04:00
return count ;
2010-02-26 14:01:34 +03:00
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RW ( soft ) ;
2010-02-26 14:01:34 +03:00
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
static ssize_t hard_block_reasons_show ( struct device * dev ,
struct device_attribute * attr ,
char * buf )
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
return sprintf ( buf , " 0x%lx \n " , rfkill - > hard_block_reasons ) ;
}
static DEVICE_ATTR_RO ( hard_block_reasons ) ;
2009-06-02 15:01:37 +04:00
static u8 user_state_from_blocked ( unsigned long state )
{
if ( state & RFKILL_BLOCK_HW )
return RFKILL_USER_STATE_HARD_BLOCKED ;
if ( state & RFKILL_BLOCK_SW )
return RFKILL_USER_STATE_SOFT_BLOCKED ;
return RFKILL_USER_STATE_UNBLOCKED ;
}
2013-07-25 02:05:36 +04:00
static ssize_t state_show ( struct device * dev , struct device_attribute * attr ,
char * buf )
2009-06-02 15:01:37 +04:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
2010-03-13 15:31:05 +03:00
return sprintf ( buf , " %d \n " , user_state_from_blocked ( rfkill - > state ) ) ;
2009-06-02 15:01:37 +04:00
}
2013-07-25 02:05:36 +04:00
static ssize_t state_store ( struct device * dev , struct device_attribute * attr ,
const char * buf , size_t count )
2009-06-02 15:01:37 +04:00
{
2009-07-10 23:41:39 +04:00
struct rfkill * rfkill = to_rfkill ( dev ) ;
unsigned long state ;
int err ;
if ( ! capable ( CAP_NET_ADMIN ) )
return - EPERM ;
2011-11-06 17:26:49 +04:00
err = kstrtoul ( buf , 0 , & state ) ;
2009-07-10 23:41:39 +04:00
if ( err )
return err ;
if ( state ! = RFKILL_USER_STATE_SOFT_BLOCKED & &
state ! = RFKILL_USER_STATE_UNBLOCKED )
return - EINVAL ;
mutex_lock ( & rfkill_global_mutex ) ;
rfkill_set_block ( rfkill , state = = RFKILL_USER_STATE_SOFT_BLOCKED ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
2009-06-02 15:01:37 +04:00
2012-10-25 18:18:34 +04:00
return count ;
2009-06-02 15:01:37 +04:00
}
2013-07-25 02:05:36 +04:00
static DEVICE_ATTR_RW ( state ) ;
2009-06-02 15:01:37 +04:00
2013-07-25 02:05:36 +04:00
static struct attribute * rfkill_dev_attrs [ ] = {
& dev_attr_name . attr ,
& dev_attr_type . attr ,
& dev_attr_index . attr ,
& dev_attr_persistent . attr ,
& dev_attr_state . attr ,
& dev_attr_soft . attr ,
& dev_attr_hard . attr ,
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
& dev_attr_hard_block_reasons . attr ,
2013-07-25 02:05:36 +04:00
NULL ,
2009-06-02 15:01:37 +04:00
} ;
2013-07-25 02:05:36 +04:00
ATTRIBUTE_GROUPS ( rfkill_dev ) ;
2009-06-02 15:01:37 +04:00
static void rfkill_release ( struct device * dev )
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
kfree ( rfkill ) ;
}
static int rfkill_dev_uevent ( struct device * dev , struct kobj_uevent_env * env )
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
unsigned long flags ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
unsigned long reasons ;
2009-06-02 15:01:37 +04:00
u32 state ;
int error ;
error = add_uevent_var ( env , " RFKILL_NAME=%s " , rfkill - > name ) ;
if ( error )
return error ;
error = add_uevent_var ( env , " RFKILL_TYPE=%s " ,
2016-01-25 12:03:46 +03:00
rfkill_types [ rfkill - > type ] ) ;
2009-06-02 15:01:37 +04:00
if ( error )
return error ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
state = rfkill - > state ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
reasons = rfkill - > hard_block_reasons ;
2009-06-02 15:01:37 +04:00
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
error = add_uevent_var ( env , " RFKILL_STATE=%d " ,
user_state_from_blocked ( state ) ) ;
rfkill: add a reason to the HW rfkill state
The WLAN device may exist yet not be usable. This can happen
when the WLAN device is controllable by both the host and
some platform internal component.
We need some arbritration that is vendor specific, but when
the device is not available for the host, we need to reflect
this state towards the user space.
Add a reason field to the rfkill object (and event) so that
userspace can know why the device is in rfkill: because some
other platform component currently owns the device, or
because the actual hw rfkill signal is asserted.
Capable userspace can now determine the reason for the rfkill
and possibly do some negotiation on a side band channel using
a proprietary protocol to gain ownership on the device in case
the device is owned by some other component. When the host
gains ownership on the device, the kernel can remove the
RFKILL_HARD_BLOCK_NOT_OWNER reason and the hw rfkill state
will be off. Then, the userspace can bring the device up and
start normal operation.
The rfkill_event structure is enlarged to include the additional
byte, it is now 9 bytes long. Old user space will ask to read
only 8 bytes so that the kernel can know not to feed them with
more data. When the user space writes 8 bytes, new kernels will
just read what is present in the file descriptor. This new byte
is read only from the userspace standpoint anyway.
If a new user space uses an old kernel, it'll ask to read 9 bytes
but will get only 8, and it'll know that it didn't get the new
state. When it'll write 9 bytes, the kernel will again ignore
this new byte which is read only from the userspace standpoint.
Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Link: https://lore.kernel.org/r/20201104134641.28816-1-emmanuel.grumbach@intel.com
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-04 16:46:41 +03:00
if ( error )
return error ;
return add_uevent_var ( env , " RFKILL_HW_BLOCK_REASON=0x%lx " , reasons ) ;
2009-06-02 15:01:37 +04:00
}
void rfkill_pause_polling ( struct rfkill * rfkill )
{
BUG_ON ( ! rfkill ) ;
if ( ! rfkill - > ops - > poll )
return ;
2016-01-20 12:39:23 +03:00
rfkill - > polling_paused = true ;
2009-06-02 15:01:37 +04:00
cancel_delayed_work_sync ( & rfkill - > poll_work ) ;
}
EXPORT_SYMBOL ( rfkill_pause_polling ) ;
void rfkill_resume_polling ( struct rfkill * rfkill )
{
BUG_ON ( ! rfkill ) ;
if ( ! rfkill - > ops - > poll )
return ;
2016-01-20 12:39:23 +03:00
rfkill - > polling_paused = false ;
if ( rfkill - > suspended )
return ;
2014-01-31 02:43:34 +04:00
queue_delayed_work ( system_power_efficient_wq ,
& rfkill - > poll_work , 0 ) ;
2009-06-02 15:01:37 +04:00
}
EXPORT_SYMBOL ( rfkill_resume_polling ) ;
2015-05-16 15:23:55 +03:00
# ifdef CONFIG_PM_SLEEP
static int rfkill_suspend ( struct device * dev )
2009-06-02 15:01:37 +04:00
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
2016-01-20 12:39:23 +03:00
rfkill - > suspended = true ;
cancel_delayed_work_sync ( & rfkill - > poll_work ) ;
2009-06-02 15:01:37 +04:00
return 0 ;
}
static int rfkill_resume ( struct device * dev )
{
struct rfkill * rfkill = to_rfkill ( dev ) ;
bool cur ;
2016-01-20 12:39:23 +03:00
rfkill - > suspended = false ;
rfkill: Fix use-after-free in rfkill_resume()
If a device is getting removed or reprobed during resume, use-after-free
might happen. For example, h5_btrtl_resume() schedules a work queue for
device reprobing, which of course requires removal first.
If the removal happens in parallel with the device_resume() and wins the
race to acquire device_lock(), removal may remove the device from the PM
lists and all, but device_resume() is already running and will continue
when the lock can be acquired, thus calling rfkill_resume().
During this, if rfkill_set_block() is then called after the corresponding
*_unregister() and kfree() are called, there will be an use-after-free
in hci_rfkill_set_block():
BUG: KASAN: use-after-free in hci_rfkill_set_block+0x58/0xc0 [bluetooth]
...
Call trace:
dump_backtrace+0x0/0x154
show_stack+0x20/0x2c
dump_stack+0xbc/0x12c
print_address_description+0x88/0x4b0
__kasan_report+0x144/0x168
kasan_report+0x10/0x18
check_memory_region+0x19c/0x1ac
__kasan_check_write+0x18/0x24
hci_rfkill_set_block+0x58/0xc0 [bluetooth]
rfkill_set_block+0x9c/0x120
rfkill_resume+0x34/0x70
dpm_run_callback+0xf0/0x1f4
device_resume+0x210/0x22c
Fix this by checking rfkill->registered in rfkill_resume(). device_del()
in rfkill_unregister() requires device_lock() and the whole rfkill_resume()
is also protected by the same lock via device_resume(), we can make sure
either the rfkill->registered is false before rfkill_resume() starts or the
rfkill device won't be unregistered before rfkill_resume() returns.
As async_resume() holds a reference to the device, at this level there can
be no use-after-free; only in the user that doesn't expect this scenario.
Fixes: 8589086f4efd ("Bluetooth: hci_h5: Turn off RTL8723BS on suspend, reprobe on resume")
Signed-off-by: Claire Chang <tientzu@chromium.org>
Link: https://lore.kernel.org/r/20201110084908.219088-1-tientzu@chromium.org
[edit commit message for clarity and add more info provided later]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2020-11-10 11:49:08 +03:00
if ( ! rfkill - > registered )
return 0 ;
2009-06-16 18:39:51 +04:00
if ( ! rfkill - > persistent ) {
cur = ! ! ( rfkill - > state & RFKILL_BLOCK_SW ) ;
rfkill_set_block ( rfkill , cur ) ;
}
2009-06-02 15:01:37 +04:00
2016-01-20 12:39:23 +03:00
if ( rfkill - > ops - > poll & & ! rfkill - > polling_paused )
queue_delayed_work ( system_power_efficient_wq ,
& rfkill - > poll_work , 0 ) ;
2009-06-02 15:01:37 +04:00
return 0 ;
}
2015-05-16 15:23:55 +03:00
static SIMPLE_DEV_PM_OPS ( rfkill_pm_ops , rfkill_suspend , rfkill_resume ) ;
# define RFKILL_PM_OPS (&rfkill_pm_ops)
# else
# define RFKILL_PM_OPS NULL
# endif
2009-06-02 15:01:37 +04:00
static struct class rfkill_class = {
. name = " rfkill " ,
. dev_release = rfkill_release ,
2013-07-25 02:05:36 +04:00
. dev_groups = rfkill_dev_groups ,
2009-06-02 15:01:37 +04:00
. dev_uevent = rfkill_dev_uevent ,
2015-05-16 15:23:55 +03:00
. pm = RFKILL_PM_OPS ,
2009-06-02 15:01:37 +04:00
} ;
2009-06-02 15:01:40 +04:00
bool rfkill_blocked ( struct rfkill * rfkill )
{
unsigned long flags ;
u32 state ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
state = rfkill - > state ;
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
return ! ! ( state & RFKILL_BLOCK_ANY ) ;
}
EXPORT_SYMBOL ( rfkill_blocked ) ;
2021-12-19 22:51:24 +03:00
bool rfkill_soft_blocked ( struct rfkill * rfkill )
{
unsigned long flags ;
u32 state ;
spin_lock_irqsave ( & rfkill - > lock , flags ) ;
state = rfkill - > state ;
spin_unlock_irqrestore ( & rfkill - > lock , flags ) ;
return ! ! ( state & RFKILL_BLOCK_SW ) ;
}
EXPORT_SYMBOL ( rfkill_soft_blocked ) ;
2009-06-02 15:01:37 +04:00
struct rfkill * __must_check rfkill_alloc ( const char * name ,
struct device * parent ,
const enum rfkill_type type ,
const struct rfkill_ops * ops ,
void * ops_data )
{
struct rfkill * rfkill ;
struct device * dev ;
if ( WARN_ON ( ! ops ) )
return NULL ;
if ( WARN_ON ( ! ops - > set_block ) )
return NULL ;
if ( WARN_ON ( ! name ) )
return NULL ;
2009-06-02 15:01:38 +04:00
if ( WARN_ON ( type = = RFKILL_TYPE_ALL | | type > = NUM_RFKILL_TYPES ) )
2009-06-02 15:01:37 +04:00
return NULL ;
2015-12-10 12:37:51 +03:00
rfkill = kzalloc ( sizeof ( * rfkill ) + strlen ( name ) + 1 , GFP_KERNEL ) ;
2009-06-02 15:01:37 +04:00
if ( ! rfkill )
return NULL ;
spin_lock_init ( & rfkill - > lock ) ;
INIT_LIST_HEAD ( & rfkill - > node ) ;
rfkill - > type = type ;
2015-12-10 12:37:51 +03:00
strcpy ( rfkill - > name , name ) ;
2009-06-02 15:01:37 +04:00
rfkill - > ops = ops ;
rfkill - > data = ops_data ;
dev = & rfkill - > dev ;
dev - > class = & rfkill_class ;
dev - > parent = parent ;
device_initialize ( dev ) ;
return rfkill ;
}
EXPORT_SYMBOL ( rfkill_alloc ) ;
static void rfkill_poll ( struct work_struct * work )
{
struct rfkill * rfkill ;
rfkill = container_of ( work , struct rfkill , poll_work . work ) ;
/*
* Poll hardware state - - driver will use one of the
* rfkill_set { , _hw , _sw } _state functions and use its
* return value to update the current status .
*/
rfkill - > ops - > poll ( rfkill , rfkill - > data ) ;
2014-01-31 02:43:34 +04:00
queue_delayed_work ( system_power_efficient_wq ,
& rfkill - > poll_work ,
2009-06-02 15:01:37 +04:00
round_jiffies_relative ( POLL_INTERVAL ) ) ;
}
static void rfkill_uevent_work ( struct work_struct * work )
{
struct rfkill * rfkill ;
rfkill = container_of ( work , struct rfkill , uevent_work ) ;
2009-06-02 15:01:38 +04:00
mutex_lock ( & rfkill_global_mutex ) ;
rfkill_event ( rfkill ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
2009-06-02 15:01:37 +04:00
}
static void rfkill_sync_work ( struct work_struct * work )
{
struct rfkill * rfkill ;
bool cur ;
rfkill = container_of ( work , struct rfkill , sync_work ) ;
mutex_lock ( & rfkill_global_mutex ) ;
cur = rfkill_global_states [ rfkill - > type ] . cur ;
rfkill_set_block ( rfkill , cur ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
}
int __must_check rfkill_register ( struct rfkill * rfkill )
{
static unsigned long rfkill_no ;
2019-12-15 18:34:08 +03:00
struct device * dev ;
2009-06-02 15:01:37 +04:00
int error ;
2019-12-15 18:34:08 +03:00
if ( ! rfkill )
return - EINVAL ;
dev = & rfkill - > dev ;
2009-06-02 15:01:37 +04:00
mutex_lock ( & rfkill_global_mutex ) ;
if ( rfkill - > registered ) {
error = - EALREADY ;
goto unlock ;
}
2009-06-02 15:01:38 +04:00
rfkill - > idx = rfkill_no ;
2009-06-02 15:01:37 +04:00
dev_set_name ( dev , " rfkill%lu " , rfkill_no ) ;
rfkill_no + + ;
list_add_tail ( & rfkill - > node , & rfkill_list ) ;
error = device_add ( dev ) ;
if ( error )
goto remove ;
error = rfkill_led_trigger_register ( rfkill ) ;
if ( error )
goto devdel ;
rfkill - > registered = true ;
2009-06-03 11:55:29 +04:00
INIT_DELAYED_WORK ( & rfkill - > poll_work , rfkill_poll ) ;
2009-06-02 15:01:37 +04:00
INIT_WORK ( & rfkill - > uevent_work , rfkill_uevent_work ) ;
INIT_WORK ( & rfkill - > sync_work , rfkill_sync_work ) ;
2009-06-03 11:55:29 +04:00
if ( rfkill - > ops - > poll )
2014-01-31 02:43:34 +04:00
queue_delayed_work ( system_power_efficient_wq ,
& rfkill - > poll_work ,
2009-06-03 11:55:29 +04:00
round_jiffies_relative ( POLL_INTERVAL ) ) ;
2009-06-08 16:27:27 +04:00
if ( ! rfkill - > persistent | | rfkill_epo_lock_active ) {
schedule_work ( & rfkill - > sync_work ) ;
} else {
# ifdef CONFIG_RFKILL_INPUT
bool soft_blocked = ! ! ( rfkill - > state & RFKILL_BLOCK_SW ) ;
if ( ! atomic_read ( & rfkill_input_disabled ) )
__rfkill_switch_all ( rfkill - > type , soft_blocked ) ;
# endif
}
2009-06-03 11:55:29 +04:00
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_event ( ) ;
2009-06-02 15:01:38 +04:00
rfkill_send_events ( rfkill , RFKILL_OP_ADD ) ;
2009-06-02 15:01:37 +04:00
mutex_unlock ( & rfkill_global_mutex ) ;
return 0 ;
devdel :
device_del ( & rfkill - > dev ) ;
remove :
list_del_init ( & rfkill - > node ) ;
unlock :
mutex_unlock ( & rfkill_global_mutex ) ;
return error ;
}
EXPORT_SYMBOL ( rfkill_register ) ;
void rfkill_unregister ( struct rfkill * rfkill )
{
BUG_ON ( ! rfkill ) ;
if ( rfkill - > ops - > poll )
cancel_delayed_work_sync ( & rfkill - > poll_work ) ;
cancel_work_sync ( & rfkill - > uevent_work ) ;
cancel_work_sync ( & rfkill - > sync_work ) ;
rfkill - > registered = false ;
device_del ( & rfkill - > dev ) ;
mutex_lock ( & rfkill_global_mutex ) ;
2009-06-02 15:01:38 +04:00
rfkill_send_events ( rfkill , RFKILL_OP_DEL ) ;
2009-06-02 15:01:37 +04:00
list_del_init ( & rfkill - > node ) ;
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_event ( ) ;
2009-06-02 15:01:37 +04:00
mutex_unlock ( & rfkill_global_mutex ) ;
rfkill_led_trigger_unregister ( rfkill ) ;
}
EXPORT_SYMBOL ( rfkill_unregister ) ;
void rfkill_destroy ( struct rfkill * rfkill )
{
if ( rfkill )
put_device ( & rfkill - > dev ) ;
}
EXPORT_SYMBOL ( rfkill_destroy ) ;
2009-06-02 15:01:38 +04:00
static int rfkill_fop_open ( struct inode * inode , struct file * file )
{
struct rfkill_data * data ;
struct rfkill * rfkill ;
struct rfkill_int_event * ev , * tmp ;
data = kzalloc ( sizeof ( * data ) , GFP_KERNEL ) ;
if ( ! data )
return - ENOMEM ;
2022-03-16 23:27:51 +03:00
data - > max_size = RFKILL_EVENT_SIZE_V1 ;
2009-06-02 15:01:38 +04:00
INIT_LIST_HEAD ( & data - > events ) ;
mutex_init ( & data - > mtx ) ;
init_waitqueue_head ( & data - > read_wait ) ;
mutex_lock ( & rfkill_global_mutex ) ;
mutex_lock ( & data - > mtx ) ;
/*
* start getting events from elsewhere but hold mtx to get
* startup events added first
*/
list_for_each_entry ( rfkill , & rfkill_list , node ) {
ev = kzalloc ( sizeof ( * ev ) , GFP_KERNEL ) ;
if ( ! ev )
goto free ;
rfkill_fill_event ( & ev - > ev , rfkill , RFKILL_OP_ADD ) ;
list_add_tail ( & ev - > list , & data - > events ) ;
}
2011-05-13 17:52:10 +04:00
list_add ( & data - > list , & rfkill_fds ) ;
2009-06-02 15:01:38 +04:00
mutex_unlock ( & data - > mtx ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
file - > private_data = data ;
2019-03-26 23:51:19 +03:00
return stream_open ( inode , file ) ;
2009-06-02 15:01:38 +04:00
free :
mutex_unlock ( & data - > mtx ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
mutex_destroy ( & data - > mtx ) ;
list_for_each_entry_safe ( ev , tmp , & data - > events , list )
kfree ( ev ) ;
kfree ( data ) ;
return - ENOMEM ;
}
2017-07-03 07:01:49 +03:00
static __poll_t rfkill_fop_poll ( struct file * file , poll_table * wait )
2009-06-02 15:01:38 +04:00
{
struct rfkill_data * data = file - > private_data ;
2018-02-12 01:34:03 +03:00
__poll_t res = EPOLLOUT | EPOLLWRNORM ;
2009-06-02 15:01:38 +04:00
poll_wait ( file , & data - > read_wait , wait ) ;
mutex_lock ( & data - > mtx ) ;
if ( ! list_empty ( & data - > events ) )
2018-02-12 01:34:03 +03:00
res = EPOLLIN | EPOLLRDNORM ;
2009-06-02 15:01:38 +04:00
mutex_unlock ( & data - > mtx ) ;
return res ;
}
static ssize_t rfkill_fop_read ( struct file * file , char __user * buf ,
size_t count , loff_t * pos )
{
struct rfkill_data * data = file - > private_data ;
struct rfkill_int_event * ev ;
unsigned long sz ;
int ret ;
mutex_lock ( & data - > mtx ) ;
while ( list_empty ( & data - > events ) ) {
if ( file - > f_flags & O_NONBLOCK ) {
ret = - EAGAIN ;
goto out ;
}
mutex_unlock ( & data - > mtx ) ;
2016-01-26 13:29:03 +03:00
/* since we re-check and it just compares pointers,
* using ! list_empty ( ) without locking isn ' t a problem
*/
2009-06-02 15:01:38 +04:00
ret = wait_event_interruptible ( data - > read_wait ,
2016-01-26 13:29:03 +03:00
! list_empty ( & data - > events ) ) ;
2009-06-02 15:01:38 +04:00
mutex_lock ( & data - > mtx ) ;
if ( ret )
goto out ;
}
ev = list_first_entry ( & data - > events , struct rfkill_int_event ,
list ) ;
sz = min_t ( unsigned long , sizeof ( ev - > ev ) , count ) ;
2022-03-16 23:27:51 +03:00
sz = min_t ( unsigned long , sz , data - > max_size ) ;
2009-06-02 15:01:38 +04:00
ret = sz ;
if ( copy_to_user ( buf , & ev - > ev , sz ) )
ret = - EFAULT ;
list_del ( & ev - > list ) ;
kfree ( ev ) ;
out :
mutex_unlock ( & data - > mtx ) ;
return ret ;
}
static ssize_t rfkill_fop_write ( struct file * file , const char __user * buf ,
size_t count , loff_t * pos )
{
2022-03-16 23:27:51 +03:00
struct rfkill_data * data = file - > private_data ;
2009-06-02 15:01:38 +04:00
struct rfkill * rfkill ;
rfkill: revert back to old userspace API by default
Recompiling with the new extended version of struct rfkill_event
broke systemd in *two* ways:
- It used "sizeof(struct rfkill_event)" to read the event, but
then complained if it actually got something != 8, this broke
it on new kernels (that include the updated API);
- It used sizeof(struct rfkill_event) to write a command, but
didn't implement the intended expansion protocol where the
kernel returns only how many bytes it accepted, and errored
out due to the unexpected smaller size on kernels that didn't
include the updated API.
Even though systemd has now been fixed, that fix may not be always
deployed, and other applications could potentially have similar
issues.
As such, in the interest of avoiding regressions, revert the
default API "struct rfkill_event" back to the original size.
Instead, add a new "struct rfkill_event_ext" that extends it by
the new field, and even more clearly document that applications
should be prepared for extensions in two ways:
* write might only accept fewer bytes on older kernels, and
will return how many to let userspace know which data may
have been ignored;
* read might return anything between 8 (the original size) and
whatever size the application sized its buffer at, indicating
how much event data was supported by the kernel.
Perhaps that will help avoid such issues in the future and we
won't have to come up with another version of the struct if we
ever need to extend it again.
Applications that want to take advantage of the new field will
have to be modified to use struct rfkill_event_ext instead now,
which comes with the danger of them having already been updated
to use it from 'struct rfkill_event', but I found no evidence
of that, and it's still relatively new.
Cc: stable@vger.kernel.org # 5.11
Reported-by: Takashi Iwai <tiwai@suse.de>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM/Clang v12.0.0-r4 (x86-64)
Link: https://lore.kernel.org/r/20210319232510.f1a139cfdd9c.Ic5c7c9d1d28972059e132ea653a21a427c326678@changeid
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
2021-03-20 01:25:11 +03:00
struct rfkill_event_ext ev ;
2016-02-22 19:36:39 +03:00
int ret ;
2009-06-02 15:01:38 +04:00
/* we don't need the 'hard' variable but accept it */
2009-07-05 16:51:06 +04:00
if ( count < RFKILL_EVENT_SIZE_V1 - 1 )
2009-06-02 15:01:38 +04:00
return - EINVAL ;
2009-07-05 16:51:06 +04:00
/*
* Copy as much data as we can accept into our ' ev ' buffer ,
* but tell userspace how much we ' ve copied so it can determine
* our API version even in a write ( ) call , if it cares .
*/
count = min ( count , sizeof ( ev ) ) ;
2022-03-16 23:27:51 +03:00
count = min_t ( size_t , count , data - > max_size ) ;
2009-07-05 16:51:06 +04:00
if ( copy_from_user ( & ev , buf , count ) )
2009-06-02 15:01:38 +04:00
return - EFAULT ;
if ( ev . type > = NUM_RFKILL_TYPES )
return - EINVAL ;
mutex_lock ( & rfkill_global_mutex ) ;
2016-02-22 19:36:39 +03:00
switch ( ev . op ) {
case RFKILL_OP_CHANGE_ALL :
2016-02-22 19:36:36 +03:00
rfkill_update_global_state ( ev . type , ev . soft ) ;
2016-02-22 19:36:39 +03:00
list_for_each_entry ( rfkill , & rfkill_list , node )
if ( rfkill - > type = = ev . type | |
ev . type = = RFKILL_TYPE_ALL )
rfkill_set_block ( rfkill , ev . soft ) ;
ret = 0 ;
break ;
case RFKILL_OP_CHANGE :
list_for_each_entry ( rfkill , & rfkill_list , node )
if ( rfkill - > idx = = ev . idx & &
( rfkill - > type = = ev . type | |
ev . type = = RFKILL_TYPE_ALL ) )
rfkill_set_block ( rfkill , ev . soft ) ;
ret = 0 ;
break ;
default :
ret = - EINVAL ;
break ;
2009-06-02 15:01:38 +04:00
}
2016-02-22 19:36:39 +03:00
2009-06-02 15:01:38 +04:00
mutex_unlock ( & rfkill_global_mutex ) ;
2016-02-22 19:36:39 +03:00
return ret ? : count ;
2009-06-02 15:01:38 +04:00
}
static int rfkill_fop_release ( struct inode * inode , struct file * file )
{
struct rfkill_data * data = file - > private_data ;
struct rfkill_int_event * ev , * tmp ;
mutex_lock ( & rfkill_global_mutex ) ;
list_del ( & data - > list ) ;
mutex_unlock ( & rfkill_global_mutex ) ;
mutex_destroy ( & data - > mtx ) ;
list_for_each_entry_safe ( ev , tmp , & data - > events , list )
kfree ( ev ) ;
# ifdef CONFIG_RFKILL_INPUT
if ( data - > input_handler )
2009-06-07 14:26:52 +04:00
if ( atomic_dec_return ( & rfkill_input_disabled ) = = 0 )
printk ( KERN_DEBUG " rfkill: input handler enabled \n " ) ;
2009-06-02 15:01:38 +04:00
# endif
kfree ( data ) ;
return 0 ;
}
static long rfkill_fop_ioctl ( struct file * file , unsigned int cmd ,
unsigned long arg )
{
struct rfkill_data * data = file - > private_data ;
2022-03-16 23:27:51 +03:00
int ret = - ENOSYS ;
u32 size ;
2009-06-02 15:01:38 +04:00
if ( _IOC_TYPE ( cmd ) ! = RFKILL_IOC_MAGIC )
return - ENOSYS ;
mutex_lock ( & data - > mtx ) ;
2022-03-16 23:27:51 +03:00
switch ( _IOC_NR ( cmd ) ) {
# ifdef CONFIG_RFKILL_INPUT
case RFKILL_IOC_NOINPUT :
if ( ! data - > input_handler ) {
if ( atomic_inc_return ( & rfkill_input_disabled ) = = 1 )
printk ( KERN_DEBUG " rfkill: input handler disabled \n " ) ;
data - > input_handler = true ;
}
ret = 0 ;
break ;
# endif
case RFKILL_IOC_MAX_SIZE :
if ( get_user ( size , ( __u32 __user * ) arg ) ) {
ret = - EFAULT ;
break ;
}
if ( size < RFKILL_EVENT_SIZE_V1 | | size > U8_MAX ) {
ret = - EINVAL ;
break ;
}
data - > max_size = size ;
ret = 0 ;
break ;
default :
break ;
2009-06-02 15:01:38 +04:00
}
mutex_unlock ( & data - > mtx ) ;
2022-03-16 23:27:51 +03:00
return ret ;
2009-06-02 15:01:38 +04:00
}
static const struct file_operations rfkill_fops = {
2009-11-23 13:27:30 +03:00
. owner = THIS_MODULE ,
2009-06-02 15:01:38 +04:00
. open = rfkill_fop_open ,
. read = rfkill_fop_read ,
. write = rfkill_fop_write ,
. poll = rfkill_fop_poll ,
. release = rfkill_fop_release ,
. unlocked_ioctl = rfkill_fop_ioctl ,
2018-09-11 22:59:08 +03:00
. compat_ioctl = compat_ptr_ioctl ,
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 20:52:59 +04:00
. llseek = no_llseek ,
2009-06-02 15:01:38 +04:00
} ;
2019-10-24 20:40:42 +03:00
# define RFKILL_NAME "rfkill"
2009-06-02 15:01:38 +04:00
static struct miscdevice rfkill_miscdev = {
. fops = & rfkill_fops ,
2019-10-24 20:40:42 +03:00
. name = RFKILL_NAME ,
. minor = RFKILL_MINOR ,
2009-06-02 15:01:38 +04:00
} ;
2009-06-02 15:01:37 +04:00
static int __init rfkill_init ( void )
{
int error ;
2016-02-22 19:36:36 +03:00
rfkill_update_global_state ( RFKILL_TYPE_ALL , ! rfkill_default_state ) ;
2009-06-02 15:01:37 +04:00
error = class_register ( & rfkill_class ) ;
if ( error )
2016-12-08 10:30:51 +03:00
goto error_class ;
2009-06-02 15:01:37 +04:00
2009-06-02 15:01:38 +04:00
error = misc_register ( & rfkill_miscdev ) ;
2016-12-08 10:30:51 +03:00
if ( error )
goto error_misc ;
2009-06-02 15:01:38 +04:00
2018-05-23 00:29:31 +03:00
error = rfkill_global_led_trigger_register ( ) ;
2017-01-06 09:07:47 +03:00
if ( error )
goto error_led_trigger ;
2009-06-02 15:01:37 +04:00
# ifdef CONFIG_RFKILL_INPUT
error = rfkill_handler_init ( ) ;
2016-12-08 10:30:51 +03:00
if ( error )
goto error_input ;
2009-06-02 15:01:37 +04:00
# endif
2016-12-08 10:30:51 +03:00
return 0 ;
2016-12-16 11:44:08 +03:00
# ifdef CONFIG_RFKILL_INPUT
2016-12-08 10:30:51 +03:00
error_input :
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_unregister ( ) ;
2016-12-19 11:51:11 +03:00
# endif
2017-01-06 09:07:47 +03:00
error_led_trigger :
misc_deregister ( & rfkill_miscdev ) ;
2016-12-08 10:30:51 +03:00
error_misc :
class_unregister ( & rfkill_class ) ;
error_class :
2009-06-02 15:01:37 +04:00
return error ;
}
subsys_initcall ( rfkill_init ) ;
static void __exit rfkill_exit ( void )
{
# ifdef CONFIG_RFKILL_INPUT
rfkill_handler_exit ( ) ;
# endif
2018-05-23 00:29:31 +03:00
rfkill_global_led_trigger_unregister ( ) ;
2009-06-02 15:01:38 +04:00
misc_deregister ( & rfkill_miscdev ) ;
2009-06-02 15:01:37 +04:00
class_unregister ( & rfkill_class ) ;
}
module_exit ( rfkill_exit ) ;
2019-10-24 20:40:42 +03:00
MODULE_ALIAS_MISCDEV ( RFKILL_MINOR ) ;
MODULE_ALIAS ( " devname: " RFKILL_NAME ) ;