Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 CEC Kernel Support
0004 ==================
0005 
0006 The CEC framework provides a unified kernel interface for use with HDMI CEC
0007 hardware. It is designed to handle a multiple types of hardware (receivers,
0008 transmitters, USB dongles). The framework also gives the option to decide
0009 what to do in the kernel driver and what should be handled by userspace
0010 applications. In addition it integrates the remote control passthrough
0011 feature into the kernel's remote control framework.
0012 
0013 
0014 The CEC Protocol
0015 ----------------
0016 
0017 The CEC protocol enables consumer electronic devices to communicate with each
0018 other through the HDMI connection. The protocol uses logical addresses in the
0019 communication. The logical address is strictly connected with the functionality
0020 provided by the device. The TV acting as the communication hub is always
0021 assigned address 0. The physical address is determined by the physical
0022 connection between devices.
0023 
0024 The CEC framework described here is up to date with the CEC 2.0 specification.
0025 It is documented in the HDMI 1.4 specification with the new 2.0 bits documented
0026 in the HDMI 2.0 specification. But for most of the features the freely available
0027 HDMI 1.3a specification is sufficient:
0028 
0029 https://www.hdmi.org/spec/index
0030 
0031 
0032 CEC Adapter Interface
0033 ---------------------
0034 
0035 The struct cec_adapter represents the CEC adapter hardware. It is created by
0036 calling cec_allocate_adapter() and deleted by calling cec_delete_adapter():
0037 
0038 .. c:function::
0039    struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops, \
0040                                             void *priv, const char *name, \
0041                                             u32 caps, u8 available_las);
0042 
0043 .. c:function::
0044    void cec_delete_adapter(struct cec_adapter *adap);
0045 
0046 To create an adapter you need to pass the following information:
0047 
0048 ops:
0049         adapter operations which are called by the CEC framework and that you
0050         have to implement.
0051 
0052 priv:
0053         will be stored in adap->priv and can be used by the adapter ops.
0054         Use cec_get_drvdata(adap) to get the priv pointer.
0055 
0056 name:
0057         the name of the CEC adapter. Note: this name will be copied.
0058 
0059 caps:
0060         capabilities of the CEC adapter. These capabilities determine the
0061         capabilities of the hardware and which parts are to be handled
0062         by userspace and which parts are handled by kernelspace. The
0063         capabilities are returned by CEC_ADAP_G_CAPS.
0064 
0065 available_las:
0066         the number of simultaneous logical addresses that this
0067         adapter can handle. Must be 1 <= available_las <= CEC_MAX_LOG_ADDRS.
0068 
0069 To obtain the priv pointer use this helper function:
0070 
0071 .. c:function::
0072         void *cec_get_drvdata(const struct cec_adapter *adap);
0073 
0074 To register the /dev/cecX device node and the remote control device (if
0075 CEC_CAP_RC is set) you call:
0076 
0077 .. c:function::
0078         int cec_register_adapter(struct cec_adapter *adap, \
0079                                  struct device *parent);
0080 
0081 where parent is the parent device.
0082 
0083 To unregister the devices call:
0084 
0085 .. c:function::
0086         void cec_unregister_adapter(struct cec_adapter *adap);
0087 
0088 Note: if cec_register_adapter() fails, then call cec_delete_adapter() to
0089 clean up. But if cec_register_adapter() succeeded, then only call
0090 cec_unregister_adapter() to clean up, never cec_delete_adapter(). The
0091 unregister function will delete the adapter automatically once the last user
0092 of that /dev/cecX device has closed its file handle.
0093 
0094 
0095 Implementing the Low-Level CEC Adapter
0096 --------------------------------------
0097 
0098 The following low-level adapter operations have to be implemented in
0099 your driver:
0100 
0101 .. c:struct:: cec_adap_ops
0102 
0103 .. code-block:: none
0104 
0105         struct cec_adap_ops
0106         {
0107                 /* Low-level callbacks */
0108                 int (*adap_enable)(struct cec_adapter *adap, bool enable);
0109                 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
0110                 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
0111                 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
0112                 void (*adap_configured)(struct cec_adapter *adap, bool configured);
0113                 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
0114                                       u32 signal_free_time, struct cec_msg *msg);
0115                 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
0116                 void (*adap_free)(struct cec_adapter *adap);
0117 
0118                 /* Error injection callbacks */
0119                 ...
0120 
0121                 /* High-level callback */
0122                 ...
0123         };
0124 
0125 The seven low-level ops deal with various aspects of controlling the CEC adapter
0126 hardware:
0127 
0128 
0129 To enable/disable the hardware::
0130 
0131         int (*adap_enable)(struct cec_adapter *adap, bool enable);
0132 
0133 This callback enables or disables the CEC hardware. Enabling the CEC hardware
0134 means powering it up in a state where no logical addresses are claimed. The
0135 physical address will always be valid if CEC_CAP_NEEDS_HPD is set. If that
0136 capability is not set, then the physical address can change while the CEC
0137 hardware is enabled. CEC drivers should not set CEC_CAP_NEEDS_HPD unless
0138 the hardware design requires that as this will make it impossible to wake
0139 up displays that pull the HPD low when in standby mode.  The initial
0140 state of the CEC adapter after calling cec_allocate_adapter() is disabled.
0141 
0142 Note that adap_enable must return 0 if enable is false.
0143 
0144 
0145 To enable/disable the 'monitor all' mode::
0146 
0147         int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
0148 
0149 If enabled, then the adapter should be put in a mode to also monitor messages
0150 that are not for us. Not all hardware supports this and this function is only
0151 called if the CEC_CAP_MONITOR_ALL capability is set. This callback is optional
0152 (some hardware may always be in 'monitor all' mode).
0153 
0154 Note that adap_monitor_all_enable must return 0 if enable is false.
0155 
0156 
0157 To enable/disable the 'monitor pin' mode::
0158 
0159         int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
0160 
0161 If enabled, then the adapter should be put in a mode to also monitor CEC pin
0162 changes. Not all hardware supports this and this function is only called if
0163 the CEC_CAP_MONITOR_PIN capability is set. This callback is optional
0164 (some hardware may always be in 'monitor pin' mode).
0165 
0166 Note that adap_monitor_pin_enable must return 0 if enable is false.
0167 
0168 
0169 To program a new logical address::
0170 
0171         int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
0172 
0173 If logical_addr == CEC_LOG_ADDR_INVALID then all programmed logical addresses
0174 are to be erased. Otherwise the given logical address should be programmed.
0175 If the maximum number of available logical addresses is exceeded, then it
0176 should return -ENXIO. Once a logical address is programmed the CEC hardware
0177 can receive directed messages to that address.
0178 
0179 Note that adap_log_addr must return 0 if logical_addr is CEC_LOG_ADDR_INVALID.
0180 
0181 
0182 Called when the adapter is fully configured or unconfigured::
0183 
0184         void (*adap_configured)(struct cec_adapter *adap, bool configured);
0185 
0186 If configured == true, then the adapter is fully configured, i.e. all logical
0187 addresses have been successfully claimed. If configured == false, then the
0188 adapter is unconfigured. If the driver has to take specific actions after
0189 (un)configuration, then that can be done through this optional callback.
0190 
0191 
0192 To transmit a new message::
0193 
0194         int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
0195                              u32 signal_free_time, struct cec_msg *msg);
0196 
0197 This transmits a new message. The attempts argument is the suggested number of
0198 attempts for the transmit.
0199 
0200 The signal_free_time is the number of data bit periods that the adapter should
0201 wait when the line is free before attempting to send a message. This value
0202 depends on whether this transmit is a retry, a message from a new initiator or
0203 a new message for the same initiator. Most hardware will handle this
0204 automatically, but in some cases this information is needed.
0205 
0206 The CEC_FREE_TIME_TO_USEC macro can be used to convert signal_free_time to
0207 microseconds (one data bit period is 2.4 ms).
0208 
0209 
0210 To log the current CEC hardware status::
0211 
0212         void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
0213 
0214 This optional callback can be used to show the status of the CEC hardware.
0215 The status is available through debugfs: cat /sys/kernel/debug/cec/cecX/status
0216 
0217 To free any resources when the adapter is deleted::
0218 
0219         void (*adap_free)(struct cec_adapter *adap);
0220 
0221 This optional callback can be used to free any resources that might have been
0222 allocated by the driver. It's called from cec_delete_adapter.
0223 
0224 
0225 Your adapter driver will also have to react to events (typically interrupt
0226 driven) by calling into the framework in the following situations:
0227 
0228 When a transmit finished (successfully or otherwise)::
0229 
0230         void cec_transmit_done(struct cec_adapter *adap, u8 status,
0231                                u8 arb_lost_cnt,  u8 nack_cnt, u8 low_drive_cnt,
0232                                u8 error_cnt);
0233 
0234 or::
0235 
0236         void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status);
0237 
0238 The status can be one of:
0239 
0240 CEC_TX_STATUS_OK:
0241         the transmit was successful.
0242 
0243 CEC_TX_STATUS_ARB_LOST:
0244         arbitration was lost: another CEC initiator
0245         took control of the CEC line and you lost the arbitration.
0246 
0247 CEC_TX_STATUS_NACK:
0248         the message was nacked (for a directed message) or
0249         acked (for a broadcast message). A retransmission is needed.
0250 
0251 CEC_TX_STATUS_LOW_DRIVE:
0252         low drive was detected on the CEC bus. This indicates that
0253         a follower detected an error on the bus and requested a
0254         retransmission.
0255 
0256 CEC_TX_STATUS_ERROR:
0257         some unspecified error occurred: this can be one of ARB_LOST
0258         or LOW_DRIVE if the hardware cannot differentiate or something
0259         else entirely. Some hardware only supports OK and FAIL as the
0260         result of a transmit, i.e. there is no way to differentiate
0261         between the different possible errors. In that case map FAIL
0262         to CEC_TX_STATUS_NACK and not to CEC_TX_STATUS_ERROR.
0263 
0264 CEC_TX_STATUS_MAX_RETRIES:
0265         could not transmit the message after trying multiple times.
0266         Should only be set by the driver if it has hardware support for
0267         retrying messages. If set, then the framework assumes that it
0268         doesn't have to make another attempt to transmit the message
0269         since the hardware did that already.
0270 
0271 The hardware must be able to differentiate between OK, NACK and 'something
0272 else'.
0273 
0274 The \*_cnt arguments are the number of error conditions that were seen.
0275 This may be 0 if no information is available. Drivers that do not support
0276 hardware retry can just set the counter corresponding to the transmit error
0277 to 1, if the hardware does support retry then either set these counters to
0278 0 if the hardware provides no feedback of which errors occurred and how many
0279 times, or fill in the correct values as reported by the hardware.
0280 
0281 Be aware that calling these functions can immediately start a new transmit
0282 if there is one pending in the queue. So make sure that the hardware is in
0283 a state where new transmits can be started *before* calling these functions.
0284 
0285 The cec_transmit_attempt_done() function is a helper for cases where the
0286 hardware never retries, so the transmit is always for just a single
0287 attempt. It will call cec_transmit_done() in turn, filling in 1 for the
0288 count argument corresponding to the status. Or all 0 if the status was OK.
0289 
0290 When a CEC message was received:
0291 
0292 .. c:function::
0293         void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
0294 
0295 Speaks for itself.
0296 
0297 Implementing the interrupt handler
0298 ----------------------------------
0299 
0300 Typically the CEC hardware provides interrupts that signal when a transmit
0301 finished and whether it was successful or not, and it provides and interrupt
0302 when a CEC message was received.
0303 
0304 The CEC driver should always process the transmit interrupts first before
0305 handling the receive interrupt. The framework expects to see the cec_transmit_done
0306 call before the cec_received_msg call, otherwise it can get confused if the
0307 received message was in reply to the transmitted message.
0308 
0309 Optional: Implementing Error Injection Support
0310 ----------------------------------------------
0311 
0312 If the CEC adapter supports Error Injection functionality, then that can
0313 be exposed through the Error Injection callbacks:
0314 
0315 .. code-block:: none
0316 
0317         struct cec_adap_ops {
0318                 /* Low-level callbacks */
0319                 ...
0320 
0321                 /* Error injection callbacks */
0322                 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
0323                 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
0324 
0325                 /* High-level CEC message callback */
0326                 ...
0327         };
0328 
0329 If both callbacks are set, then an ``error-inj`` file will appear in debugfs.
0330 The basic syntax is as follows:
0331 
0332 Leading spaces/tabs are ignored. If the next character is a ``#`` or the end of the
0333 line was reached, then the whole line is ignored. Otherwise a command is expected.
0334 
0335 This basic parsing is done in the CEC Framework. It is up to the driver to decide
0336 what commands to implement. The only requirement is that the command ``clear`` without
0337 any arguments must be implemented and that it will remove all current error injection
0338 commands.
0339 
0340 This ensures that you can always do ``echo clear >error-inj`` to clear any error
0341 injections without having to know the details of the driver-specific commands.
0342 
0343 Note that the output of ``error-inj`` shall be valid as input to ``error-inj``.
0344 So this must work:
0345 
0346 .. code-block:: none
0347 
0348         $ cat error-inj >einj.txt
0349         $ cat einj.txt >error-inj
0350 
0351 The first callback is called when this file is read and it should show the
0352 current error injection state::
0353 
0354         int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
0355 
0356 It is recommended that it starts with a comment block with basic usage
0357 information. It returns 0 for success and an error otherwise.
0358 
0359 The second callback will parse commands written to the ``error-inj`` file::
0360 
0361         bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
0362 
0363 The ``line`` argument points to the start of the command. Any leading
0364 spaces or tabs have already been skipped. It is a single line only (so there
0365 are no embedded newlines) and it is 0-terminated. The callback is free to
0366 modify the contents of the buffer. It is only called for lines containing a
0367 command, so this callback is never called for empty lines or comment lines.
0368 
0369 Return true if the command was valid or false if there were syntax errors.
0370 
0371 Implementing the High-Level CEC Adapter
0372 ---------------------------------------
0373 
0374 The low-level operations drive the hardware, the high-level operations are
0375 CEC protocol driven. The following high-level callbacks are available:
0376 
0377 .. code-block:: none
0378 
0379         struct cec_adap_ops {
0380                 /* Low-level callbacks */
0381                 ...
0382 
0383                 /* Error injection callbacks */
0384                 ...
0385 
0386                 /* High-level CEC message callback */
0387                 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
0388         };
0389 
0390 The received() callback allows the driver to optionally handle a newly
0391 received CEC message::
0392 
0393         int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
0394 
0395 If the driver wants to process a CEC message, then it can implement this
0396 callback. If it doesn't want to handle this message, then it should return
0397 -ENOMSG, otherwise the CEC framework assumes it processed this message and
0398 it will not do anything with it.
0399 
0400 
0401 CEC framework functions
0402 -----------------------
0403 
0404 CEC Adapter drivers can call the following CEC framework functions:
0405 
0406 .. c:function::
0407    int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg, \
0408                         bool block);
0409 
0410 Transmit a CEC message. If block is true, then wait until the message has been
0411 transmitted, otherwise just queue it and return.
0412 
0413 .. c:function::
0414    void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block);
0415 
0416 Change the physical address. This function will set adap->phys_addr and
0417 send an event if it has changed. If cec_s_log_addrs() has been called and
0418 the physical address has become valid, then the CEC framework will start
0419 claiming the logical addresses. If block is true, then this function won't
0420 return until this process has finished.
0421 
0422 When the physical address is set to a valid value the CEC adapter will
0423 be enabled (see the adap_enable op). When it is set to CEC_PHYS_ADDR_INVALID,
0424 then the CEC adapter will be disabled. If you change a valid physical address
0425 to another valid physical address, then this function will first set the
0426 address to CEC_PHYS_ADDR_INVALID before enabling the new physical address.
0427 
0428 .. c:function::
0429    void cec_s_phys_addr_from_edid(struct cec_adapter *adap, \
0430                                   const struct edid *edid);
0431 
0432 A helper function that extracts the physical address from the edid struct
0433 and calls cec_s_phys_addr() with that address, or CEC_PHYS_ADDR_INVALID
0434 if the EDID did not contain a physical address or edid was a NULL pointer.
0435 
0436 .. c:function::
0437         int cec_s_log_addrs(struct cec_adapter *adap, \
0438                             struct cec_log_addrs *log_addrs, bool block);
0439 
0440 Claim the CEC logical addresses. Should never be called if CEC_CAP_LOG_ADDRS
0441 is set. If block is true, then wait until the logical addresses have been
0442 claimed, otherwise just queue it and return. To unconfigure all logical
0443 addresses call this function with log_addrs set to NULL or with
0444 log_addrs->num_log_addrs set to 0. The block argument is ignored when
0445 unconfiguring. This function will just return if the physical address is
0446 invalid. Once the physical address becomes valid, then the framework will
0447 attempt to claim these logical addresses.
0448 
0449 CEC Pin framework
0450 -----------------
0451 
0452 Most CEC hardware operates on full CEC messages where the software provides
0453 the message and the hardware handles the low-level CEC protocol. But some
0454 hardware only drives the CEC pin and software has to handle the low-level
0455 CEC protocol. The CEC pin framework was created to handle such devices.
0456 
0457 Note that due to the close-to-realtime requirements it can never be guaranteed
0458 to work 100%. This framework uses highres timers internally, but if a
0459 timer goes off too late by more than 300 microseconds wrong results can
0460 occur. In reality it appears to be fairly reliable.
0461 
0462 One advantage of this low-level implementation is that it can be used as
0463 a cheap CEC analyser, especially if interrupts can be used to detect
0464 CEC pin transitions from low to high or vice versa.
0465 
0466 .. kernel-doc:: include/media/cec-pin.h
0467 
0468 CEC Notifier framework
0469 ----------------------
0470 
0471 Most drm HDMI implementations have an integrated CEC implementation and no
0472 notifier support is needed. But some have independent CEC implementations
0473 that have their own driver. This could be an IP block for an SoC or a
0474 completely separate chip that deals with the CEC pin. For those cases a
0475 drm driver can install a notifier and use the notifier to inform the
0476 CEC driver about changes in the physical address.
0477 
0478 .. kernel-doc:: include/media/cec-notifier.h