cec-core.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. CEC Kernel Support
  2. ==================
  3. The CEC framework provides a unified kernel interface for use with HDMI CEC
  4. hardware. It is designed to handle a multiple types of hardware (receivers,
  5. transmitters, USB dongles). The framework also gives the option to decide
  6. what to do in the kernel driver and what should be handled by userspace
  7. applications. In addition it integrates the remote control passthrough
  8. feature into the kernel's remote control framework.
  9. The CEC Protocol
  10. ----------------
  11. The CEC protocol enables consumer electronic devices to communicate with each
  12. other through the HDMI connection. The protocol uses logical addresses in the
  13. communication. The logical address is strictly connected with the functionality
  14. provided by the device. The TV acting as the communication hub is always
  15. assigned address 0. The physical address is determined by the physical
  16. connection between devices.
  17. The CEC framework described here is up to date with the CEC 2.0 specification.
  18. It is documented in the HDMI 1.4 specification with the new 2.0 bits documented
  19. in the HDMI 2.0 specification. But for most of the features the freely available
  20. HDMI 1.3a specification is sufficient:
  21. http://www.microprocessor.org/HDMISpecification13a.pdf
  22. The Kernel Interface
  23. ====================
  24. CEC Adapter
  25. -----------
  26. The struct cec_adapter represents the CEC adapter hardware. It is created by
  27. calling cec_allocate_adapter() and deleted by calling cec_delete_adapter():
  28. .. c:function::
  29. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  30. void *priv, const char *name, u32 caps, u8 available_las,
  31. struct device *parent);
  32. .. c:function::
  33. void cec_delete_adapter(struct cec_adapter *adap);
  34. To create an adapter you need to pass the following information:
  35. ops:
  36. adapter operations which are called by the CEC framework and that you
  37. have to implement.
  38. priv:
  39. will be stored in adap->priv and can be used by the adapter ops.
  40. name:
  41. the name of the CEC adapter. Note: this name will be copied.
  42. caps:
  43. capabilities of the CEC adapter. These capabilities determine the
  44. capabilities of the hardware and which parts are to be handled
  45. by userspace and which parts are handled by kernelspace. The
  46. capabilities are returned by CEC_ADAP_G_CAPS.
  47. available_las:
  48. the number of simultaneous logical addresses that this
  49. adapter can handle. Must be 1 <= available_las <= CEC_MAX_LOG_ADDRS.
  50. parent:
  51. the parent device.
  52. To register the /dev/cecX device node and the remote control device (if
  53. CEC_CAP_RC is set) you call:
  54. .. c:function::
  55. int cec_register_adapter(struct cec_adapter \*adap);
  56. To unregister the devices call:
  57. .. c:function::
  58. void cec_unregister_adapter(struct cec_adapter \*adap);
  59. Note: if cec_register_adapter() fails, then call cec_delete_adapter() to
  60. clean up. But if cec_register_adapter() succeeded, then only call
  61. cec_unregister_adapter() to clean up, never cec_delete_adapter(). The
  62. unregister function will delete the adapter automatically once the last user
  63. of that /dev/cecX device has closed its file handle.
  64. Implementing the Low-Level CEC Adapter
  65. --------------------------------------
  66. The following low-level adapter operations have to be implemented in
  67. your driver:
  68. .. c:type:: struct cec_adap_ops
  69. .. code-block:: none
  70. struct cec_adap_ops
  71. {
  72. /* Low-level callbacks */
  73. int (*adap_enable)(struct cec_adapter *adap, bool enable);
  74. int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  75. int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  76. int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  77. u32 signal_free_time, struct cec_msg *msg);
  78. void (\*adap_log_status)(struct cec_adapter *adap);
  79. /* High-level callbacks */
  80. ...
  81. };
  82. The three low-level ops deal with various aspects of controlling the CEC adapter
  83. hardware:
  84. To enable/disable the hardware:
  85. .. c:function::
  86. int (*adap_enable)(struct cec_adapter *adap, bool enable);
  87. This callback enables or disables the CEC hardware. Enabling the CEC hardware
  88. means powering it up in a state where no logical addresses are claimed. This
  89. op assumes that the physical address (adap->phys_addr) is valid when enable is
  90. true and will not change while the CEC adapter remains enabled. The initial
  91. state of the CEC adapter after calling cec_allocate_adapter() is disabled.
  92. Note that adap_enable must return 0 if enable is false.
  93. To enable/disable the 'monitor all' mode:
  94. .. c:function::
  95. int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  96. If enabled, then the adapter should be put in a mode to also monitor messages
  97. that not for us. Not all hardware supports this and this function is only
  98. called if the CEC_CAP_MONITOR_ALL capability is set. This callback is optional
  99. (some hardware may always be in 'monitor all' mode).
  100. Note that adap_monitor_all_enable must return 0 if enable is false.
  101. To program a new logical address:
  102. .. c:function::
  103. int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  104. If logical_addr == CEC_LOG_ADDR_INVALID then all programmed logical addresses
  105. are to be erased. Otherwise the given logical address should be programmed.
  106. If the maximum number of available logical addresses is exceeded, then it
  107. should return -ENXIO. Once a logical address is programmed the CEC hardware
  108. can receive directed messages to that address.
  109. Note that adap_log_addr must return 0 if logical_addr is CEC_LOG_ADDR_INVALID.
  110. To transmit a new message:
  111. .. c:function::
  112. int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  113. u32 signal_free_time, struct cec_msg *msg);
  114. This transmits a new message. The attempts argument is the suggested number of
  115. attempts for the transmit.
  116. The signal_free_time is the number of data bit periods that the adapter should
  117. wait when the line is free before attempting to send a message. This value
  118. depends on whether this transmit is a retry, a message from a new initiator or
  119. a new message for the same initiator. Most hardware will handle this
  120. automatically, but in some cases this information is needed.
  121. The CEC_FREE_TIME_TO_USEC macro can be used to convert signal_free_time to
  122. microseconds (one data bit period is 2.4 ms).
  123. To log the current CEC hardware status:
  124. .. c:function::
  125. void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
  126. This optional callback can be used to show the status of the CEC hardware.
  127. The status is available through debugfs: cat /sys/kernel/debug/cec/cecX/status
  128. Your adapter driver will also have to react to events (typically interrupt
  129. driven) by calling into the framework in the following situations:
  130. When a transmit finished (successfully or otherwise):
  131. .. c:function::
  132. void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
  133. u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
  134. The status can be one of:
  135. CEC_TX_STATUS_OK:
  136. the transmit was successful.
  137. CEC_TX_STATUS_ARB_LOST:
  138. arbitration was lost: another CEC initiator
  139. took control of the CEC line and you lost the arbitration.
  140. CEC_TX_STATUS_NACK:
  141. the message was nacked (for a directed message) or
  142. acked (for a broadcast message). A retransmission is needed.
  143. CEC_TX_STATUS_LOW_DRIVE:
  144. low drive was detected on the CEC bus. This indicates that
  145. a follower detected an error on the bus and requested a
  146. retransmission.
  147. CEC_TX_STATUS_ERROR:
  148. some unspecified error occurred: this can be one of
  149. the previous two if the hardware cannot differentiate or something
  150. else entirely.
  151. CEC_TX_STATUS_MAX_RETRIES:
  152. could not transmit the message after trying multiple times.
  153. Should only be set by the driver if it has hardware support for
  154. retrying messages. If set, then the framework assumes that it
  155. doesn't have to make another attempt to transmit the message
  156. since the hardware did that already.
  157. The \*_cnt arguments are the number of error conditions that were seen.
  158. This may be 0 if no information is available. Drivers that do not support
  159. hardware retry can just set the counter corresponding to the transmit error
  160. to 1, if the hardware does support retry then either set these counters to
  161. 0 if the hardware provides no feedback of which errors occurred and how many
  162. times, or fill in the correct values as reported by the hardware.
  163. When a CEC message was received:
  164. .. c:function::
  165. void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
  166. Speaks for itself.
  167. Implementing the High-Level CEC Adapter
  168. ---------------------------------------
  169. The low-level operations drive the hardware, the high-level operations are
  170. CEC protocol driven. The following high-level callbacks are available:
  171. .. code-block:: none
  172. struct cec_adap_ops {
  173. /\* Low-level callbacks \*/
  174. ...
  175. /\* High-level CEC message callback \*/
  176. int (\*received)(struct cec_adapter \*adap, struct cec_msg \*msg);
  177. };
  178. The received() callback allows the driver to optionally handle a newly
  179. received CEC message
  180. .. c:function::
  181. int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
  182. If the driver wants to process a CEC message, then it can implement this
  183. callback. If it doesn't want to handle this message, then it should return
  184. -ENOMSG, otherwise the CEC framework assumes it processed this message and
  185. it will not no anything with it.
  186. CEC framework functions
  187. -----------------------
  188. CEC Adapter drivers can call the following CEC framework functions:
  189. .. c:function::
  190. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  191. bool block);
  192. Transmit a CEC message. If block is true, then wait until the message has been
  193. transmitted, otherwise just queue it and return.
  194. .. c:function::
  195. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  196. bool block);
  197. Change the physical address. This function will set adap->phys_addr and
  198. send an event if it has changed. If cec_s_log_addrs() has been called and
  199. the physical address has become valid, then the CEC framework will start
  200. claiming the logical addresses. If block is true, then this function won't
  201. return until this process has finished.
  202. When the physical address is set to a valid value the CEC adapter will
  203. be enabled (see the adap_enable op). When it is set to CEC_PHYS_ADDR_INVALID,
  204. then the CEC adapter will be disabled. If you change a valid physical address
  205. to another valid physical address, then this function will first set the
  206. address to CEC_PHYS_ADDR_INVALID before enabling the new physical address.
  207. .. c:function::
  208. int cec_s_log_addrs(struct cec_adapter *adap,
  209. struct cec_log_addrs *log_addrs, bool block);
  210. Claim the CEC logical addresses. Should never be called if CEC_CAP_LOG_ADDRS
  211. is set. If block is true, then wait until the logical addresses have been
  212. claimed, otherwise just queue it and return. To unconfigure all logical
  213. addresses call this function with log_addrs set to NULL or with
  214. log_addrs->num_log_addrs set to 0. The block argument is ignored when
  215. unconfiguring. This function will just return if the physical address is
  216. invalid. Once the physical address becomes valid, then the framework will
  217. attempt to claim these logical addresses.