rfkill.txt 18 KB

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