162306a36Sopenharmony_ci===============================
262306a36Sopenharmony_cirfkill - RF kill switch support
362306a36Sopenharmony_ci===============================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci.. contents::
762306a36Sopenharmony_ci   :depth: 2
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciIntroduction
1062306a36Sopenharmony_ci============
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciThe rfkill subsystem provides a generic interface for disabling any radio
1362306a36Sopenharmony_citransmitter in the system. When a transmitter is blocked, it shall not
1462306a36Sopenharmony_ciradiate any power.
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciThe subsystem also provides the ability to react on button presses and
1762306a36Sopenharmony_cidisable all transmitters of a certain type (or all). This is intended for
1862306a36Sopenharmony_cisituations where transmitters need to be turned off, for example on
1962306a36Sopenharmony_ciaircraft.
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciThe rfkill subsystem has a concept of "hard" and "soft" block, which
2262306a36Sopenharmony_cidiffer little in their meaning (block == transmitters off) but rather in
2362306a36Sopenharmony_ciwhether they can be changed or not:
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci - hard block
2662306a36Sopenharmony_ci	read-only radio block that cannot be overridden by software
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci - soft block
2962306a36Sopenharmony_ci	writable radio block (need not be readable) that is set by
3062306a36Sopenharmony_ci        the system software.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciThe rfkill subsystem has two parameters, rfkill.default_state and
3362306a36Sopenharmony_cirfkill.master_switch_mode, which are documented in
3462306a36Sopenharmony_ciadmin-guide/kernel-parameters.rst.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ciImplementation details
3862306a36Sopenharmony_ci======================
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciThe rfkill subsystem is composed of three main components:
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci * the rfkill core,
4362306a36Sopenharmony_ci * the deprecated rfkill-input module (an input layer handler, being
4462306a36Sopenharmony_ci   replaced by userspace policy code) and
4562306a36Sopenharmony_ci * the rfkill drivers.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciThe rfkill core provides API for kernel drivers to register their radio
4862306a36Sopenharmony_citransmitter with the kernel, methods for turning it on and off, and letting
4962306a36Sopenharmony_cithe system know about hardware-disabled states that may be implemented on
5062306a36Sopenharmony_cithe device.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciThe rfkill core code also notifies userspace of state changes, and provides
5362306a36Sopenharmony_ciways for userspace to query the current states. See the "Userspace support"
5462306a36Sopenharmony_cisection below.
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciWhen the device is hard-blocked (either by a call to rfkill_set_hw_state()
5762306a36Sopenharmony_cior from query_hw_block), set_block() will be invoked for additional software
5862306a36Sopenharmony_ciblock, but drivers can ignore the method call since they can use the return
5962306a36Sopenharmony_civalue of the function rfkill_set_hw_state() to sync the software state
6062306a36Sopenharmony_ciinstead of keeping track of calls to set_block(). In fact, drivers should
6162306a36Sopenharmony_ciuse the return value of rfkill_set_hw_state() unless the hardware actually
6262306a36Sopenharmony_cikeeps track of soft and hard block separately.
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciKernel API
6662306a36Sopenharmony_ci==========
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciDrivers for radio transmitters normally implement an rfkill driver.
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciPlatform drivers might implement input devices if the rfkill button is just
7162306a36Sopenharmony_cithat, a button. If that button influences the hardware then you need to
7262306a36Sopenharmony_ciimplement an rfkill driver instead. This also applies if the platform provides
7362306a36Sopenharmony_cia way to turn on/off the transmitter(s).
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciFor some platforms, it is possible that the hardware state changes during
7662306a36Sopenharmony_cisuspend/hibernation, in which case it will be necessary to update the rfkill
7762306a36Sopenharmony_cicore with the current state at resume time.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciTo create an rfkill driver, driver's Kconfig needs to have::
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	depends on RFKILL || !RFKILL
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cito ensure the driver cannot be built-in when rfkill is modular. The !RFKILL
8462306a36Sopenharmony_cicase allows the driver to be built when rfkill is not configured, in which
8562306a36Sopenharmony_cicase all rfkill API can still be used but will be provided by static inlines
8662306a36Sopenharmony_ciwhich compile to almost nothing.
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciCalling rfkill_set_hw_state() when a state change happens is required from
8962306a36Sopenharmony_cirfkill drivers that control devices that can be hard-blocked unless they also
9062306a36Sopenharmony_ciassign the poll_hw_block() callback (then the rfkill core will poll the
9162306a36Sopenharmony_cidevice). Don't do this unless you cannot get the event in any other way.
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cirfkill provides per-switch LED triggers, which can be used to drive LEDs
9462306a36Sopenharmony_ciaccording to the switch state (LED_FULL when blocked, LED_OFF otherwise).
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciUserspace support
9862306a36Sopenharmony_ci=================
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciThe recommended userspace interface to use is /dev/rfkill, which is a misc
10162306a36Sopenharmony_cicharacter device that allows userspace to obtain and set the state of rfkill
10262306a36Sopenharmony_cidevices and sets of devices. It also notifies userspace about device addition
10362306a36Sopenharmony_ciand removal. The API is a simple read/write API that is defined in
10462306a36Sopenharmony_cilinux/rfkill.h, with one ioctl that allows turning off the deprecated input
10562306a36Sopenharmony_cihandler in the kernel for the transition period.
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciExcept for the one ioctl, communication with the kernel is done via read()
10862306a36Sopenharmony_ciand write() of instances of 'struct rfkill_event'. In this structure, the
10962306a36Sopenharmony_cisoft and hard block are properly separated (unlike sysfs, see below) and
11062306a36Sopenharmony_ciuserspace is able to get a consistent snapshot of all rfkill devices in the
11162306a36Sopenharmony_cisystem. Also, it is possible to switch all rfkill drivers (or all drivers of
11262306a36Sopenharmony_cia specified type) into a state which also updates the default state for
11362306a36Sopenharmony_cihotplugged devices.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciAfter an application opens /dev/rfkill, it can read the current state of all
11662306a36Sopenharmony_cidevices. Changes can be obtained by either polling the descriptor for
11762306a36Sopenharmony_cihotplug or state change events or by listening for uevents emitted by the
11862306a36Sopenharmony_cirfkill core framework.
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciAdditionally, each rfkill device is registered in sysfs and emits uevents.
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cirfkill devices issue uevents (with an action of "change"), with the following
12362306a36Sopenharmony_cienvironment variables set::
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	RFKILL_NAME
12662306a36Sopenharmony_ci	RFKILL_STATE
12762306a36Sopenharmony_ci	RFKILL_TYPE
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ciThe content of these variables corresponds to the "name", "state" and
13062306a36Sopenharmony_ci"type" sysfs files explained above.
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciFor further details consult Documentation/ABI/stable/sysfs-class-rfkill.
133