rfkill.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. rfkill - RF switch subsystem support
  2. ====================================
  3. 1 Introduction
  4. 2 Implementation details
  5. 3 Kernel driver guidelines
  6. 4 Kernel API
  7. 5 Userspace support
  8. 1. Introduction:
  9. The rfkill switch subsystem exists to add a generic interface to circuitry that
  10. can enable or disable the signal output of a wireless *transmitter* of any
  11. type. By far, the most common use is to disable radio-frequency transmitters.
  12. The rfkill switch subsystem offers support for keys and switches often found on
  13. laptops to enable wireless devices like WiFi and Bluetooth to actually perform
  14. an action.
  15. The buttons to enable and disable the wireless transmitters are important in
  16. situations where the user is for example using his laptop on a location where
  17. radio-frequency transmitters _must_ be disabled (e.g. airplanes).
  18. Because of this requirement, userspace support for the keys should not be made
  19. mandatory. Because userspace might want to perform some additional smarter
  20. tasks when the key is pressed, rfkill provides userspace the possibility to
  21. take over the task to handle the key events.
  22. ===============================================================================
  23. 2: Implementation details
  24. The rfkill class provides kernel drivers with an interface that allows them to
  25. know when they should enable or disable a wireless network device transmitter.
  26. The rfkill-input module provides the kernel with the ability to implement a
  27. basic response when the user presses a key or button (or toggles a switch)
  28. related to rfkill functionality. It is an in-kernel implementation of default
  29. policy of reacting to rfkill-related input events and neither mandatory nor
  30. required for wireless drivers to operate.
  31. The rfkill-input module also provides EPO (emergency power-off) functionality
  32. for all wireless transmitters. This function cannot be overriden, and it is
  33. always active. rfkill EPO is related to *_RFKILL_ALL input events.
  34. All state changes on rfkill devices are propagated by the rfkill class to a
  35. notification chain and also to userspace through uevents.
  36. The system inside the kernel has been split into 2 separate sections:
  37. 1 - RFKILL
  38. 2 - RFKILL_INPUT
  39. The first option enables rfkill support and will make sure userspace will be
  40. notified of any events through uevents. It provides a notification chain for
  41. interested parties in the kernel to also get notified of rfkill state changes
  42. in other drivers. It creates several sysfs entries which can be used by
  43. userspace. See section "Userspace support".
  44. The second option provides an rfkill input handler. This handler will listen to
  45. all rfkill key events and will toggle the radio accordingly. With this option
  46. enabled userspace could either do nothing or simply perform monitoring tasks.
  47. When a rfkill switch is in the RFKILL_STATE_ON, the wireless transmitter (radio
  48. TX circuit for example) is *enabled*. When the rfkill switch is in the
  49. RFKILL_STATE_OFF, the wireless transmitter is to be *blocked* from operating.
  50. Full rfkill functionality requires two different subsystems to cooperate: the
  51. input layer and the rfkill class. The input layer issues *commands* to the
  52. entire system requesting that devices registered to the rfkill class change
  53. state. The way this interaction happens is not complex, but it is not obvious
  54. either:
  55. Kernel Input layer:
  56. * Generates KEY_WWAN, KEY_WLAN, KEY_BLUETOOTH, SW_RFKILL_ALL, and
  57. other such events when the user presses certain keys, buttons, or
  58. toggles certain physical switches.
  59. THE INPUT LAYER IS NEVER USED TO PROPAGATE STATUS, NOTIFICATIONS OR THE
  60. KIND OF STUFF AN ON-SCREEN-DISPLAY APPLICATION WOULD REPORT. It is
  61. used to issue *commands* for the system to change behaviour, and these
  62. commands may or may not be carried out by some kernel driver or
  63. userspace application. It follows that doing user feedback based only
  64. on input events is broken, there is no guarantee that an input event
  65. will be acted upon.
  66. Most wireless communication device drivers implementing rfkill
  67. functionality MUST NOT generate these events, and have no reason to
  68. register themselves with the input layer. This is a common
  69. misconception. There is an API to propagate rfkill status change
  70. information, and it is NOT the input layer.
  71. rfkill class:
  72. * Calls a hook in a driver to effectively change the wireless
  73. transmitter state;
  74. * Keeps track of the wireless transmitter state (with help from
  75. the driver);
  76. * Generates userspace notifications (uevents) and a call to a
  77. notification chain (kernel) when there is a wireless transmitter
  78. state change;
  79. * Connects a wireless communications driver with the common rfkill
  80. control system, which, for example, allows actions such as
  81. "switch all bluetooth devices offline" to be carried out by
  82. userspace or by rfkill-input.
  83. THE RFKILL CLASS NEVER ISSUES INPUT EVENTS. THE RFKILL CLASS DOES
  84. NOT LISTEN TO INPUT EVENTS. NO DRIVER USING THE RFKILL CLASS SHALL
  85. EVER LISTEN TO, OR ACT ON RFKILL INPUT EVENTS.
  86. Most wireless data communication drivers in the kernel have just to
  87. implement the rfkill class API to work properly. Interfacing to the
  88. input layer is not often required (and is very often a *bug*).
  89. Userspace input handlers (uevents) or kernel input handlers (rfkill-input):
  90. * Implements the policy of what should happen when one of the input
  91. layer events related to rfkill operation is received.
  92. * Uses the sysfs interface (userspace) or private rfkill API calls
  93. to tell the devices registered with the rfkill class to change
  94. their state (i.e. translates the input layer event into real
  95. action).
  96. * rfkill-input implements EPO by handling EV_SW SW_RFKILL_ALL 0
  97. (power off all transmitters) in a special way: it ignores any
  98. overrides and local state cache and forces all transmitters to
  99. the OFF state (including those which are already supposed to be
  100. OFF). Note that the opposite event (power on all transmitters)
  101. is handled normally.
  102. Userspace uevent handler or kernel platform-specific drivers hooked to the
  103. rfkill notifier chain:
  104. * Taps into the rfkill notifier chain or to KOBJ_CHANGE uevents,
  105. in order to know when a device that is registered with the rfkill
  106. class changes state;
  107. * Issues feedback notifications to the user;
  108. * In the rare platforms where this is required, synthesizes an input
  109. event to command all *OTHER* rfkill devices to also change their
  110. statues when a specific rfkill device changes state.
  111. ===============================================================================
  112. 3: Kernel driver guidelines
  113. The first thing one needs to know is whether his driver should be talking to
  114. the rfkill class or to the input layer.
  115. Do not mistake input devices for rfkill devices. The only type of "rfkill
  116. switch" device that is to be registered with the rfkill class are those
  117. directly controlling the circuits that cause a wireless transmitter to stop
  118. working (or the software equivalent of them). Every other kind of "rfkill
  119. switch" is just an input device and MUST NOT be registered with the rfkill
  120. class.
  121. A driver should register a device with the rfkill class when ALL of the
  122. following conditions are met:
  123. 1. The device is/controls a data communications wireless transmitter;
  124. 2. The kernel can interact with the hardware/firmware to CHANGE the wireless
  125. transmitter state (block/unblock TX operation);
  126. A driver should register a device with the input subsystem to issue
  127. rfkill-related events (KEY_WLAN, KEY_BLUETOOTH, KEY_WWAN, KEY_WIMAX,
  128. SW_RFKILL_ALL, etc) when ALL of the folowing conditions are met:
  129. 1. It is directly related to some physical device the user interacts with, to
  130. command the O.S./firmware/hardware to enable/disable a data communications
  131. wireless transmitter.
  132. Examples of the physical device are: buttons, keys and switches the user
  133. will press/touch/slide/switch to enable or disable the wireless
  134. communication device.
  135. 2. It is NOT slaved to another device, i.e. there is no other device that
  136. issues rfkill-related input events in preference to this one.
  137. Typically, the ACPI "radio kill" switch of a laptop is the master input
  138. device to issue rfkill events, and, e.g., the WLAN card is just a slave
  139. device that gets disabled by its hardware radio-kill input pin.
  140. When in doubt, do not issue input events. For drivers that should generate
  141. input events in some platforms, but not in others (e.g. b43), the best solution
  142. is to NEVER generate input events in the first place. That work should be
  143. deferred to a platform-specific kernel module (which will know when to generate
  144. events through the rfkill notifier chain) or to userspace. This avoids the
  145. usual maintenance problems with DMI whitelisting.
  146. Corner cases and examples:
  147. ====================================
  148. 1. If the device is an input device that, because of hardware or firmware,
  149. causes wireless transmitters to be blocked regardless of the kernel's will, it
  150. is still just an input device, and NOT to be registered with the rfkill class.
  151. 2. If the wireless transmitter switch control is read-only, it is an input
  152. device and not to be registered with the rfkill class (and maybe not to be made
  153. an input layer event source either, see below).
  154. 3. If there is some other device driver *closer* to the actual hardware the
  155. user interacted with (the button/switch/key) to issue an input event, THAT is
  156. the device driver that should be issuing input events.
  157. E.g:
  158. [RFKILL slider switch] -- [GPIO hardware] -- [WLAN card rf-kill input]
  159. (platform driver) (wireless card driver)
  160. The user is closer to the RFKILL slide switch plaform driver, so the driver
  161. which must issue input events is the platform driver looking at the GPIO
  162. hardware, and NEVER the wireless card driver (which is just a slave). It is
  163. very likely that there are other leaves than just the WLAN card rf-kill input
  164. (e.g. a bluetooth card, etc)...
  165. On the other hand, some embedded devices do this:
  166. [RFKILL slider switch] -- [WLAN card rf-kill input]
  167. (wireless card driver)
  168. In this situation, the wireless card driver *could* register itself as an input
  169. device and issue rf-kill related input events... but in order to AVOID the need
  170. for DMI whitelisting, the wireless card driver does NOT do it. Userspace (HAL)
  171. or a platform driver (that exists only on these embedded devices) will do the
  172. dirty job of issuing the input events.
  173. COMMON MISTAKES in kernel drivers, related to rfkill:
  174. ====================================
  175. 1. NEVER confuse input device keys and buttons with input device switches.
  176. 1a. Switches are always set or reset. They report the current state
  177. (on position or off position).
  178. 1b. Keys and buttons are either in the pressed or not-pressed state, and
  179. that's it. A "button" that latches down when you press it, and
  180. unlatches when you press it again is in fact a switch as far as input
  181. devices go.
  182. Add the SW_* events you need for switches, do NOT try to emulate a button using
  183. KEY_* events just because there is no such SW_* event yet. Do NOT try to use,
  184. for example, KEY_BLUETOOTH when you should be using SW_BLUETOOTH instead.
  185. 2. Input device switches (sources of EV_SW events) DO store their current
  186. state, and that state CAN be queried from userspace through IOCTLs. There is
  187. no sysfs interface for this, but that doesn't mean you should break things
  188. trying to hook it to the rfkill class to get a sysfs interface :-)
  189. 3. Do not issue *_RFKILL_ALL events, unless you are sure it is the correct
  190. event for your switch/button. These events are emergency power-off events when
  191. they are trying to turn the transmitters off. An example of an input device
  192. which SHOULD generate *_RFKILL_ALL events is the wireless-kill switch in a
  193. laptop which is NOT a hotkey, but a real switch that kills radios in hardware,
  194. even if the O.S. has gone to lunch. An example of an input device which SHOULD
  195. NOT generate *_RFKILL_ALL events is any sort of hot key that does nothing by
  196. itself, as well as any hot key that is type-specific (e.g. the one for WLAN).
  197. ===============================================================================
  198. 4: Kernel API
  199. To build a driver with rfkill subsystem support, the driver should depend on
  200. the Kconfig symbol RFKILL; it should _not_ depend on RKFILL_INPUT.
  201. The hardware the driver talks to may be write-only (where the current state
  202. of the hardware is unknown), or read-write (where the hardware can be queried
  203. about its current state).
  204. The rfkill class will call the get_state hook of a device every time it needs
  205. to know the *real* current state of the hardware. This can happen often.
  206. Some hardware provides events when its status changes. In these cases, it is
  207. best for the driver to not provide a get_state hook, and instead register the
  208. rfkill class *already* with the correct status, and keep it updated using
  209. rfkill_force_state() when it gets an event from the hardware.
  210. There is no provision for a statically-allocated rfkill struct. You must
  211. use rfkill_allocate() to allocate one.
  212. You should:
  213. - rfkill_allocate()
  214. - modify rfkill fields (flags, name)
  215. - modify state to the current hardware state (THIS IS THE ONLY TIME
  216. YOU CAN ACCESS state DIRECTLY)
  217. - rfkill_register()
  218. Please refer to the source for more documentation.
  219. ===============================================================================
  220. 5: Userspace support
  221. rfkill devices issue uevents (with an action of "change"), with the following
  222. environment variables set:
  223. RFKILL_NAME
  224. RFKILL_STATE
  225. RFKILL_TYPE
  226. The ABI for these variables is defined by the sysfs attributes. It is best
  227. to take a quick look at the source to make sure of the possible values.
  228. It is expected that HAL will trap those, and bridge them to DBUS, etc. These
  229. events CAN and SHOULD be used to give feedback to the user about the rfkill
  230. status of the system.
  231. Input devices may issue events that are related to rfkill. These are the
  232. various KEY_* events and SW_* events supported by rfkill-input.c.
  233. ******IMPORTANT******
  234. When rfkill-input is ACTIVE, userspace is NOT TO CHANGE THE STATE OF AN RFKILL
  235. SWITCH IN RESPONSE TO AN INPUT EVENT also handled by rfkill-input, unless it
  236. has set to true the user_claim attribute for that particular switch. This rule
  237. is *absolute*; do NOT violate it.
  238. ******IMPORTANT******
  239. Userspace must not assume it is the only source of control for rfkill switches.
  240. Their state CAN and WILL change on its own, due to firmware actions, direct
  241. user actions, and the rfkill-input EPO override for *_RFKILL_ALL.
  242. When rfkill-input is not active, userspace must initiate an rfkill status
  243. change by writing to the "state" attribute in order for anything to happen.
  244. Take particular care to implement EV_SW SW_RFKILL_ALL properly. When that
  245. switch is set to OFF, *every* rfkill device *MUST* be immediately put into the
  246. OFF state, no questions asked.
  247. The following sysfs entries will be created:
  248. name: Name assigned by driver to this key (interface or driver name).
  249. type: Name of the key type ("wlan", "bluetooth", etc).
  250. state: Current state of the key. 1: On, 0: Off.
  251. claim: 1: Userspace handles events, 0: Kernel handles events
  252. Both the "state" and "claim" entries are also writable. For the "state" entry
  253. this means that when 1 or 0 is written, the device rfkill state (if not yet in
  254. the requested state), will be will be toggled accordingly.
  255. For the "claim" entry writing 1 to it means that the kernel no longer handles
  256. key events even though RFKILL_INPUT input was enabled. When "claim" has been
  257. set to 0, userspace should make sure that it listens for the input events or
  258. check the sysfs "state" entry regularly to correctly perform the required tasks
  259. when the rkfill key is pressed.
  260. A note about input devices and EV_SW events:
  261. In order to know the current state of an input device switch (like
  262. SW_RFKILL_ALL), you will need to use an IOCTL. That information is not
  263. available through sysfs in a generic way at this time, and it is not available
  264. through the rfkill class AT ALL.