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