1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * cec - HDMI Consumer Electronics Control support header
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
11 #include <linux/poll.h>
13 #include <linux/debugfs.h>
14 #include <linux/device.h>
15 #include <linux/cdev.h>
16 #include <linux/kthread.h>
17 #include <linux/timer.h>
18 #include <linux/cec-funcs.h>
19 #include <media/rc-core.h>
21 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
22 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
25 * struct cec_devnode - cec device node
27 * @cdev: cec character device
28 * @minor: device node minor number
29 * @lock: lock to serialize open/release and registration
30 * @registered: the device was correctly registered
31 * @unregistered: the device was unregistered
32 * @lock_fhs: lock to control access to @fhs
33 * @fhs: the list of open filehandles (cec_fh)
35 * This structure represents a cec-related device node.
37 * To add or remove filehandles from @fhs the @lock must be taken first,
38 * followed by @lock_fhs. It is safe to access @fhs if either lock is held.
40 * The @parent is a physical device. It must be set by core or device drivers
41 * before registering the node.
50 /* serialize open/release and registration */
54 /* protect access to fhs */
55 struct mutex lock_fhs;
65 struct list_head list;
66 struct list_head xfer_list;
67 struct cec_adapter *adap;
70 struct delayed_work work;
77 struct cec_msg_entry {
78 struct list_head list;
82 struct cec_event_entry {
83 struct list_head list;
87 #define CEC_NUM_CORE_EVENTS 2
88 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
91 struct list_head list;
92 struct list_head xfer_list;
93 struct cec_adapter *adap;
98 wait_queue_head_t wait;
100 struct list_head events[CEC_NUM_EVENTS]; /* queued events */
101 u16 queued_events[CEC_NUM_EVENTS];
102 unsigned int total_queued_events;
103 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
104 struct list_head msgs; /* queued messages */
105 unsigned int queued_msgs;
108 #define CEC_SIGNAL_FREE_TIME_RETRY 3
109 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
110 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
112 /* The nominal data bit period is 2.4 ms */
113 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
115 struct cec_adap_ops {
116 /* Low-level callbacks */
117 int (*adap_enable)(struct cec_adapter *adap, bool enable);
118 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
119 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
120 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
121 void (*adap_configured)(struct cec_adapter *adap, bool configured);
122 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
123 u32 signal_free_time, struct cec_msg *msg);
124 void (*adap_nb_transmit_canceled)(struct cec_adapter *adap,
125 const struct cec_msg *msg);
126 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
127 void (*adap_free)(struct cec_adapter *adap);
129 /* Error injection callbacks, called without adap->lock held */
130 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
131 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
133 /* High-level CEC message callback, called without adap->lock held */
134 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
138 * The minimum message length you can receive (excepting poll messages) is 2.
139 * With a transfer rate of at most 36 bytes per second this makes 18 messages
140 * per second worst case.
142 * We queue at most 3 seconds worth of received messages. The CEC specification
143 * requires that messages are replied to within a second, so 3 seconds should
144 * give more than enough margin. Since most messages are actually more than 2
145 * bytes, this is in practice a lot more than 3 seconds.
147 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
150 * The transmit queue is limited to 1 second worth of messages (worst case).
151 * Messages can be transmitted by userspace and kernel space. But for both it
152 * makes no sense to have a lot of messages queued up. One second seems
155 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
158 * struct cec_adapter - cec adapter structure
159 * @owner: module owner
160 * @name: name of the CEC adapter
161 * @devnode: device node for the /dev/cecX device
162 * @lock: mutex controlling access to this structure
163 * @rc: remote control device
164 * @transmit_queue: queue of pending transmits
165 * @transmit_queue_sz: number of pending transmits
166 * @wait_queue: queue of transmits waiting for a reply
167 * @transmitting: CEC messages currently being transmitted
168 * @transmit_in_progress: true if a transmit is in progress
169 * @transmit_in_progress_aborted: true if a transmit is in progress is to be
170 * aborted. This happens if the logical address is
171 * invalidated while the transmit is ongoing. In that
172 * case the transmit will finish, but will not retransmit
173 * and be marked as ABORTED.
174 * @xfer_timeout_ms: the transfer timeout in ms.
175 * If 0, then timeout after 2.1 ms.
176 * @kthread_config: kthread used to configure a CEC adapter
177 * @config_completion: used to signal completion of the config kthread
178 * @kthread: main CEC processing thread
179 * @kthread_waitq: main CEC processing wait_queue
180 * @ops: cec adapter ops
181 * @priv: cec driver's private data
182 * @capabilities: cec adapter capabilities
183 * @available_log_addrs: maximum number of available logical addresses
184 * @phys_addr: the current physical address
185 * @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high
186 * in order to transmit or receive CEC messages. This is usually a HW
188 * @is_enabled: the CEC adapter is enabled
189 * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
190 * @must_reconfigure: while configuring, the PA changed, so reclaim LAs
191 * @is_configured: the CEC adapter is configured (i.e. has claimed LAs)
192 * @cec_pin_is_high: if true then the CEC pin is high. Only used with the
194 * @adap_controls_phys_addr: if true, then the CEC adapter controls the
195 * physical address, i.e. the CEC hardware can detect HPD changes and
196 * read the EDID and is not dependent on an external HDMI driver.
197 * Drivers that need this can set this field to true after the
198 * cec_allocate_adapter() call.
199 * @last_initiator: the initiator of the last transmitted message.
200 * @monitor_all_cnt: number of filehandles monitoring all msgs
201 * @monitor_pin_cnt: number of filehandles monitoring pin changes
202 * @follower_cnt: number of filehandles in follower mode
203 * @cec_follower: filehandle of the exclusive follower
204 * @cec_initiator: filehandle of the exclusive initiator
205 * @passthrough: if true, then the exclusive follower is in
207 * @log_addrs: current logical addresses
208 * @conn_info: current connector info
209 * @tx_timeouts: number of transmit timeouts
210 * @notifier: CEC notifier
211 * @pin: CEC pin status struct
212 * @cec_dir: debugfs cec directory
213 * @status_file: debugfs cec status file
214 * @error_inj_file: debugfs cec error injection file
215 * @sequence: transmit sequence counter
216 * @input_phys: remote control input_phys name
218 * This structure represents a cec adapter.
221 struct module *owner;
223 struct cec_devnode devnode;
227 struct list_head transmit_queue;
228 unsigned int transmit_queue_sz;
229 struct list_head wait_queue;
230 struct cec_data *transmitting;
231 bool transmit_in_progress;
232 bool transmit_in_progress_aborted;
233 unsigned int xfer_timeout_ms;
235 struct task_struct *kthread_config;
236 struct completion config_completion;
238 struct task_struct *kthread;
239 wait_queue_head_t kthread_waitq;
241 const struct cec_adap_ops *ops;
244 u8 available_log_addrs;
250 bool must_reconfigure;
252 bool cec_pin_is_high;
253 bool adap_controls_phys_addr;
258 struct cec_fh *cec_follower;
259 struct cec_fh *cec_initiator;
261 struct cec_log_addrs log_addrs;
262 struct cec_connector_info conn_info;
266 #ifdef CONFIG_CEC_NOTIFIER
267 struct cec_notifier *notifier;
269 #ifdef CONFIG_CEC_PIN
273 struct dentry *cec_dir;
280 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
285 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
287 return adap->log_addrs.log_addr_mask & (1 << log_addr);
290 static inline bool cec_is_sink(const struct cec_adapter *adap)
292 return adap->phys_addr == 0;
296 * cec_is_registered() - is the CEC adapter registered?
298 * @adap: the CEC adapter, may be NULL.
300 * Return: true if the adapter is registered, false otherwise.
302 static inline bool cec_is_registered(const struct cec_adapter *adap)
304 return adap && adap->devnode.registered;
307 #define cec_phys_addr_exp(pa) \
308 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
311 struct drm_connector;
313 #if IS_REACHABLE(CONFIG_CEC_CORE)
314 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
315 void *priv, const char *name, u32 caps, u8 available_las);
316 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
317 void cec_unregister_adapter(struct cec_adapter *adap);
318 void cec_delete_adapter(struct cec_adapter *adap);
320 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
322 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
324 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
325 const struct edid *edid);
326 void cec_s_conn_info(struct cec_adapter *adap,
327 const struct cec_connector_info *conn_info);
328 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
331 /* Called by the adapter */
332 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
333 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
334 u8 error_cnt, ktime_t ts);
336 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
337 u8 arb_lost_cnt, u8 nack_cnt,
338 u8 low_drive_cnt, u8 error_cnt)
340 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
341 low_drive_cnt, error_cnt, ktime_get());
344 * Simplified version of cec_transmit_done for hardware that doesn't retry
345 * failed transmits. So this is always just one attempt in which case
346 * the status is sufficient.
348 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
349 u8 status, ktime_t ts);
351 static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
354 cec_transmit_attempt_done_ts(adap, status, ktime_get());
357 void cec_received_msg_ts(struct cec_adapter *adap,
358 struct cec_msg *msg, ktime_t ts);
360 static inline void cec_received_msg(struct cec_adapter *adap,
363 cec_received_msg_ts(adap, msg, ktime_get());
367 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
369 * @adap: pointer to the cec adapter
370 * @is_high: when true the CEC pin is high, otherwise it is low
371 * @dropped_events: when true some events were dropped
372 * @ts: the timestamp for this event
375 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
376 bool dropped_events, ktime_t ts);
379 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
381 * @adap: pointer to the cec adapter
382 * @is_high: when true the HPD pin is high, otherwise it is low
383 * @ts: the timestamp for this event
386 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
389 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
391 * @adap: pointer to the cec adapter
392 * @is_high: when true the 5V pin is high, otherwise it is low
393 * @ts: the timestamp for this event
396 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
399 * cec_get_edid_phys_addr() - find and return the physical address
401 * @edid: pointer to the EDID data
402 * @size: size in bytes of the EDID data
403 * @offset: If not %NULL then the location of the physical address
404 * bytes in the EDID will be returned here. This is set to 0
405 * if there is no physical address found.
407 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
409 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
410 unsigned int *offset);
412 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
413 const struct drm_connector *connector);
417 static inline int cec_register_adapter(struct cec_adapter *adap,
418 struct device *parent)
423 static inline void cec_unregister_adapter(struct cec_adapter *adap)
427 static inline void cec_delete_adapter(struct cec_adapter *adap)
431 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
436 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
437 const struct edid *edid)
441 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
442 unsigned int *offset)
446 return CEC_PHYS_ADDR_INVALID;
449 static inline void cec_s_conn_info(struct cec_adapter *adap,
450 const struct cec_connector_info *conn_info)
455 cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
456 const struct drm_connector *connector)
458 memset(conn_info, 0, sizeof(*conn_info));
464 * cec_phys_addr_invalidate() - set the physical address to INVALID
466 * @adap: the CEC adapter
468 * This is a simple helper function to invalidate the physical
471 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
473 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
477 * cec_get_edid_spa_location() - find location of the Source Physical Address
480 * @size: the size of the EDID
482 * This EDID is expected to be a CEA-861 compliant, which means that there are
483 * at least two blocks and one or more of the extensions blocks are CEA-861
486 * The returned location is guaranteed to be <= size-2.
488 * This is an inline function since it is used by both CEC and V4L2.
489 * Ideally this would go in a module shared by both, but it is overkill to do
490 * that for just a single function.
492 static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
495 unsigned int blocks = size / 128;
499 /* Sanity check: at least 2 blocks and a multiple of the block size */
500 if (blocks < 2 || size % 128)
504 * If there are fewer extension blocks than the size, then update
505 * 'blocks'. It is allowed to have more extension blocks than the size,
506 * since some hardware can only read e.g. 256 bytes of the EDID, even
507 * though more blocks are present. The first CEA-861 extension block
508 * should normally be in block 1 anyway.
510 if (edid[0x7e] + 1 < blocks)
511 blocks = edid[0x7e] + 1;
513 for (block = 1; block < blocks; block++) {
514 unsigned int offset = block * 128;
516 /* Skip any non-CEA-861 extension blocks */
517 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
520 /* search Vendor Specific Data Block (tag 3) */
521 d = edid[offset + 2] & 0x7f;
522 /* Check if there are Data Blocks */
526 unsigned int i = offset + 4;
527 unsigned int end = offset + d;
529 /* Note: 'end' is always < 'size' */
531 u8 tag = edid[i] >> 5;
532 u8 len = edid[i] & 0x1f;
534 if (tag == 3 && len >= 5 && i + len <= end &&
535 edid[i + 1] == 0x03 &&
536 edid[i + 2] == 0x0c &&
546 #endif /* _MEDIA_CEC_H */