1CEC Kernel Support 2================== 3 4The CEC framework provides a unified kernel interface for use with HDMI CEC 5hardware. It is designed to handle a multiple types of hardware (receivers, 6transmitters, USB dongles). The framework also gives the option to decide 7what to do in the kernel driver and what should be handled by userspace 8applications. In addition it integrates the remote control passthrough 9feature into the kernel's remote control framework. 10 11 12The CEC Protocol 13---------------- 14 15The CEC protocol enables consumer electronic devices to communicate with each 16other through the HDMI connection. The protocol uses logical addresses in the 17communication. The logical address is strictly connected with the functionality 18provided by the device. The TV acting as the communication hub is always 19assigned address 0. The physical address is determined by the physical 20connection between devices. 21 22The CEC framework described here is up to date with the CEC 2.0 specification. 23It is documented in the HDMI 1.4 specification with the new 2.0 bits documented 24in the HDMI 2.0 specification. But for most of the features the freely available 25HDMI 1.3a specification is sufficient: 26 27http://www.microprocessor.org/HDMISpecification13a.pdf 28 29 30CEC Adapter Interface 31--------------------- 32 33The struct cec_adapter represents the CEC adapter hardware. It is created by 34calling cec_allocate_adapter() and deleted by calling cec_delete_adapter(): 35 36.. c:function:: 37 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops, void *priv, 38 const char *name, u32 caps, u8 available_las); 39 40.. c:function:: 41 void cec_delete_adapter(struct cec_adapter *adap); 42 43To create an adapter you need to pass the following information: 44 45ops: 46 adapter operations which are called by the CEC framework and that you 47 have to implement. 48 49priv: 50 will be stored in adap->priv and can be used by the adapter ops. 51 Use cec_get_drvdata(adap) to get the priv pointer. 52 53name: 54 the name of the CEC adapter. Note: this name will be copied. 55 56caps: 57 capabilities of the CEC adapter. These capabilities determine the 58 capabilities of the hardware and which parts are to be handled 59 by userspace and which parts are handled by kernelspace. The 60 capabilities are returned by CEC_ADAP_G_CAPS. 61 62available_las: 63 the number of simultaneous logical addresses that this 64 adapter can handle. Must be 1 <= available_las <= CEC_MAX_LOG_ADDRS. 65 66To obtain the priv pointer use this helper function: 67 68.. c:function:: 69 void *cec_get_drvdata(const struct cec_adapter *adap); 70 71To register the /dev/cecX device node and the remote control device (if 72CEC_CAP_RC is set) you call: 73 74.. c:function:: 75 int cec_register_adapter(struct cec_adapter *adap, struct device *parent); 76 77where parent is the parent device. 78 79To unregister the devices call: 80 81.. c:function:: 82 void cec_unregister_adapter(struct cec_adapter *adap); 83 84Note: if cec_register_adapter() fails, then call cec_delete_adapter() to 85clean up. But if cec_register_adapter() succeeded, then only call 86cec_unregister_adapter() to clean up, never cec_delete_adapter(). The 87unregister function will delete the adapter automatically once the last user 88of that /dev/cecX device has closed its file handle. 89 90 91Implementing the Low-Level CEC Adapter 92-------------------------------------- 93 94The following low-level adapter operations have to be implemented in 95your driver: 96 97.. c:type:: struct cec_adap_ops 98 99.. code-block:: none 100 101 struct cec_adap_ops 102 { 103 /* Low-level callbacks */ 104 int (*adap_enable)(struct cec_adapter *adap, bool enable); 105 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable); 106 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable); 107 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr); 108 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts, 109 u32 signal_free_time, struct cec_msg *msg); 110 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file); 111 void (*adap_free)(struct cec_adapter *adap); 112 113 /* Error injection callbacks */ 114 ... 115 116 /* High-level callbacks */ 117 ... 118 }; 119 120The seven low-level ops deal with various aspects of controlling the CEC adapter 121hardware: 122 123 124To enable/disable the hardware: 125 126.. c:function:: 127 int (*adap_enable)(struct cec_adapter *adap, bool enable); 128 129This callback enables or disables the CEC hardware. Enabling the CEC hardware 130means powering it up in a state where no logical addresses are claimed. This 131op assumes that the physical address (adap->phys_addr) is valid when enable is 132true and will not change while the CEC adapter remains enabled. The initial 133state of the CEC adapter after calling cec_allocate_adapter() is disabled. 134 135Note that adap_enable must return 0 if enable is false. 136 137 138To enable/disable the 'monitor all' mode: 139 140.. c:function:: 141 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable); 142 143If enabled, then the adapter should be put in a mode to also monitor messages 144that not for us. Not all hardware supports this and this function is only 145called if the CEC_CAP_MONITOR_ALL capability is set. This callback is optional 146(some hardware may always be in 'monitor all' mode). 147 148Note that adap_monitor_all_enable must return 0 if enable is false. 149 150 151To enable/disable the 'monitor pin' mode: 152 153.. c:function:: 154 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable); 155 156If enabled, then the adapter should be put in a mode to also monitor CEC pin 157changes. Not all hardware supports this and this function is only called if 158the CEC_CAP_MONITOR_PIN capability is set. This callback is optional 159(some hardware may always be in 'monitor pin' mode). 160 161Note that adap_monitor_pin_enable must return 0 if enable is false. 162 163 164To program a new logical address: 165 166.. c:function:: 167 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr); 168 169If logical_addr == CEC_LOG_ADDR_INVALID then all programmed logical addresses 170are to be erased. Otherwise the given logical address should be programmed. 171If the maximum number of available logical addresses is exceeded, then it 172should return -ENXIO. Once a logical address is programmed the CEC hardware 173can receive directed messages to that address. 174 175Note that adap_log_addr must return 0 if logical_addr is CEC_LOG_ADDR_INVALID. 176 177 178To transmit a new message: 179 180.. c:function:: 181 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts, 182 u32 signal_free_time, struct cec_msg *msg); 183 184This transmits a new message. The attempts argument is the suggested number of 185attempts for the transmit. 186 187The signal_free_time is the number of data bit periods that the adapter should 188wait when the line is free before attempting to send a message. This value 189depends on whether this transmit is a retry, a message from a new initiator or 190a new message for the same initiator. Most hardware will handle this 191automatically, but in some cases this information is needed. 192 193The CEC_FREE_TIME_TO_USEC macro can be used to convert signal_free_time to 194microseconds (one data bit period is 2.4 ms). 195 196 197To log the current CEC hardware status: 198 199.. c:function:: 200 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file); 201 202This optional callback can be used to show the status of the CEC hardware. 203The status is available through debugfs: cat /sys/kernel/debug/cec/cecX/status 204 205To free any resources when the adapter is deleted: 206 207.. c:function:: 208 void (*adap_free)(struct cec_adapter *adap); 209 210This optional callback can be used to free any resources that might have been 211allocated by the driver. It's called from cec_delete_adapter. 212 213 214Your adapter driver will also have to react to events (typically interrupt 215driven) by calling into the framework in the following situations: 216 217When a transmit finished (successfully or otherwise): 218 219.. c:function:: 220 void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt, 221 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt); 222 223or: 224 225.. c:function:: 226 void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status); 227 228The status can be one of: 229 230CEC_TX_STATUS_OK: 231 the transmit was successful. 232 233CEC_TX_STATUS_ARB_LOST: 234 arbitration was lost: another CEC initiator 235 took control of the CEC line and you lost the arbitration. 236 237CEC_TX_STATUS_NACK: 238 the message was nacked (for a directed message) or 239 acked (for a broadcast message). A retransmission is needed. 240 241CEC_TX_STATUS_LOW_DRIVE: 242 low drive was detected on the CEC bus. This indicates that 243 a follower detected an error on the bus and requested a 244 retransmission. 245 246CEC_TX_STATUS_ERROR: 247 some unspecified error occurred: this can be one of ARB_LOST 248 or LOW_DRIVE if the hardware cannot differentiate or something 249 else entirely. Some hardware only supports OK and FAIL as the 250 result of a transmit, i.e. there is no way to differentiate 251 between the different possible errors. In that case map FAIL 252 to CEC_TX_STATUS_NACK and not to CEC_TX_STATUS_ERROR. 253 254CEC_TX_STATUS_MAX_RETRIES: 255 could not transmit the message after trying multiple times. 256 Should only be set by the driver if it has hardware support for 257 retrying messages. If set, then the framework assumes that it 258 doesn't have to make another attempt to transmit the message 259 since the hardware did that already. 260 261The hardware must be able to differentiate between OK, NACK and 'something 262else'. 263 264The \*_cnt arguments are the number of error conditions that were seen. 265This may be 0 if no information is available. Drivers that do not support 266hardware retry can just set the counter corresponding to the transmit error 267to 1, if the hardware does support retry then either set these counters to 2680 if the hardware provides no feedback of which errors occurred and how many 269times, or fill in the correct values as reported by the hardware. 270 271The cec_transmit_attempt_done() function is a helper for cases where the 272hardware never retries, so the transmit is always for just a single 273attempt. It will call cec_transmit_done() in turn, filling in 1 for the 274count argument corresponding to the status. Or all 0 if the status was OK. 275 276When a CEC message was received: 277 278.. c:function:: 279 void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg); 280 281Speaks for itself. 282 283Implementing the interrupt handler 284---------------------------------- 285 286Typically the CEC hardware provides interrupts that signal when a transmit 287finished and whether it was successful or not, and it provides and interrupt 288when a CEC message was received. 289 290The CEC driver should always process the transmit interrupts first before 291handling the receive interrupt. The framework expects to see the cec_transmit_done 292call before the cec_received_msg call, otherwise it can get confused if the 293received message was in reply to the transmitted message. 294 295Optional: Implementing Error Injection Support 296---------------------------------------------- 297 298If the CEC adapter supports Error Injection functionality, then that can 299be exposed through the Error Injection callbacks: 300 301.. code-block:: none 302 303 struct cec_adap_ops { 304 /* Low-level callbacks */ 305 ... 306 307 /* Error injection callbacks */ 308 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf); 309 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line); 310 311 /* High-level CEC message callback */ 312 ... 313 }; 314 315If both callbacks are set, then an ``error-inj`` file will appear in debugfs. 316The basic syntax is as follows: 317 318Leading spaces/tabs are ignored. If the next character is a ``#`` or the end of the 319line was reached, then the whole line is ignored. Otherwise a command is expected. 320 321This basic parsing is done in the CEC Framework. It is up to the driver to decide 322what commands to implement. The only requirement is that the command ``clear`` without 323any arguments must be implemented and that it will remove all current error injection 324commands. 325 326This ensures that you can always do ``echo clear >error-inj`` to clear any error 327injections without having to know the details of the driver-specific commands. 328 329Note that the output of ``error-inj`` shall be valid as input to ``error-inj``. 330So this must work: 331 332.. code-block:: none 333 334 $ cat error-inj >einj.txt 335 $ cat einj.txt >error-inj 336 337The first callback is called when this file is read and it should show the 338the current error injection state: 339 340.. c:function:: 341 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf); 342 343It is recommended that it starts with a comment block with basic usage 344information. It returns 0 for success and an error otherwise. 345 346The second callback will parse commands written to the ``error-inj`` file: 347 348.. c:function:: 349 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line); 350 351The ``line`` argument points to the start of the command. Any leading 352spaces or tabs have already been skipped. It is a single line only (so there 353are no embedded newlines) and it is 0-terminated. The callback is free to 354modify the contents of the buffer. It is only called for lines containing a 355command, so this callback is never called for empty lines or comment lines. 356 357Return true if the command was valid or false if there were syntax errors. 358 359Implementing the High-Level CEC Adapter 360--------------------------------------- 361 362The low-level operations drive the hardware, the high-level operations are 363CEC protocol driven. The following high-level callbacks are available: 364 365.. code-block:: none 366 367 struct cec_adap_ops { 368 /* Low-level callbacks */ 369 ... 370 371 /* Error injection callbacks */ 372 ... 373 374 /* High-level CEC message callback */ 375 int (*received)(struct cec_adapter *adap, struct cec_msg *msg); 376 }; 377 378The received() callback allows the driver to optionally handle a newly 379received CEC message 380 381.. c:function:: 382 int (*received)(struct cec_adapter *adap, struct cec_msg *msg); 383 384If the driver wants to process a CEC message, then it can implement this 385callback. If it doesn't want to handle this message, then it should return 386-ENOMSG, otherwise the CEC framework assumes it processed this message and 387it will not do anything with it. 388 389 390CEC framework functions 391----------------------- 392 393CEC Adapter drivers can call the following CEC framework functions: 394 395.. c:function:: 396 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg, 397 bool block); 398 399Transmit a CEC message. If block is true, then wait until the message has been 400transmitted, otherwise just queue it and return. 401 402.. c:function:: 403 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, 404 bool block); 405 406Change the physical address. This function will set adap->phys_addr and 407send an event if it has changed. If cec_s_log_addrs() has been called and 408the physical address has become valid, then the CEC framework will start 409claiming the logical addresses. If block is true, then this function won't 410return until this process has finished. 411 412When the physical address is set to a valid value the CEC adapter will 413be enabled (see the adap_enable op). When it is set to CEC_PHYS_ADDR_INVALID, 414then the CEC adapter will be disabled. If you change a valid physical address 415to another valid physical address, then this function will first set the 416address to CEC_PHYS_ADDR_INVALID before enabling the new physical address. 417 418.. c:function:: 419 void cec_s_phys_addr_from_edid(struct cec_adapter *adap, 420 const struct edid *edid); 421 422A helper function that extracts the physical address from the edid struct 423and calls cec_s_phys_addr() with that address, or CEC_PHYS_ADDR_INVALID 424if the EDID did not contain a physical address or edid was a NULL pointer. 425 426.. c:function:: 427 int cec_s_log_addrs(struct cec_adapter *adap, 428 struct cec_log_addrs *log_addrs, bool block); 429 430Claim the CEC logical addresses. Should never be called if CEC_CAP_LOG_ADDRS 431is set. If block is true, then wait until the logical addresses have been 432claimed, otherwise just queue it and return. To unconfigure all logical 433addresses call this function with log_addrs set to NULL or with 434log_addrs->num_log_addrs set to 0. The block argument is ignored when 435unconfiguring. This function will just return if the physical address is 436invalid. Once the physical address becomes valid, then the framework will 437attempt to claim these logical addresses. 438 439CEC Pin framework 440----------------- 441 442Most CEC hardware operates on full CEC messages where the software provides 443the message and the hardware handles the low-level CEC protocol. But some 444hardware only drives the CEC pin and software has to handle the low-level 445CEC protocol. The CEC pin framework was created to handle such devices. 446 447Note that due to the close-to-realtime requirements it can never be guaranteed 448to work 100%. This framework uses highres timers internally, but if a 449timer goes off too late by more than 300 microseconds wrong results can 450occur. In reality it appears to be fairly reliable. 451 452One advantage of this low-level implementation is that it can be used as 453a cheap CEC analyser, especially if interrupts can be used to detect 454CEC pin transitions from low to high or vice versa. 455 456.. kernel-doc:: include/media/cec-pin.h 457 458CEC Notifier framework 459---------------------- 460 461Most drm HDMI implementations have an integrated CEC implementation and no 462notifier support is needed. But some have independent CEC implementations 463that have their own driver. This could be an IP block for an SoC or a 464completely separate chip that deals with the CEC pin. For those cases a 465drm driver can install a notifier and use the notifier to inform the 466CEC driver about changes in the physical address. 467 468.. kernel-doc:: include/media/cec-notifier.h 469