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 * @registered: the device was correctly registered
30 * @unregistered: the device was unregistered
31 * @fhs_lock: lock to control access to the filehandle list
32 * @fhs: the list of open filehandles (cec_fh)
34 * This structure represents a cec-related device node.
36 * The @parent is a physical device. It must be set by core or device drivers
37 * before registering the node.
58 struct list_head list;
59 struct list_head xfer_list;
60 struct cec_adapter *adap;
63 struct delayed_work work;
70 struct cec_msg_entry {
71 struct list_head list;
75 struct cec_event_entry {
76 struct list_head list;
80 #define CEC_NUM_CORE_EVENTS 2
81 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
84 struct list_head list;
85 struct list_head xfer_list;
86 struct cec_adapter *adap;
91 wait_queue_head_t wait;
93 struct list_head events[CEC_NUM_EVENTS]; /* queued events */
94 u16 queued_events[CEC_NUM_EVENTS];
95 unsigned int total_queued_events;
96 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
97 struct list_head msgs; /* queued messages */
98 unsigned int queued_msgs;
101 #define CEC_SIGNAL_FREE_TIME_RETRY 3
102 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
103 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
105 /* The nominal data bit period is 2.4 ms */
106 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
108 struct cec_adap_ops {
109 /* Low-level callbacks */
110 int (*adap_enable)(struct cec_adapter *adap, bool enable);
111 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
112 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
113 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
114 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
115 u32 signal_free_time, struct cec_msg *msg);
116 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
117 void (*adap_free)(struct cec_adapter *adap);
119 /* Error injection callbacks */
120 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
121 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
123 /* High-level CEC message callback */
124 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
128 * The minimum message length you can receive (excepting poll messages) is 2.
129 * With a transfer rate of at most 36 bytes per second this makes 18 messages
130 * per second worst case.
132 * We queue at most 3 seconds worth of received messages. The CEC specification
133 * requires that messages are replied to within a second, so 3 seconds should
134 * give more than enough margin. Since most messages are actually more than 2
135 * bytes, this is in practice a lot more than 3 seconds.
137 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
140 * The transmit queue is limited to 1 second worth of messages (worst case).
141 * Messages can be transmitted by userspace and kernel space. But for both it
142 * makes no sense to have a lot of messages queued up. One second seems
145 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
148 * struct cec_adapter - cec adapter structure
149 * @owner: module owner
150 * @name: name of the CEC adapter
151 * @devnode: device node for the /dev/cecX device
152 * @lock: mutex controlling access to this structure
153 * @rc: remote control device
154 * @transmit_queue: queue of pending transmits
155 * @transmit_queue_sz: number of pending transmits
156 * @wait_queue: queue of transmits waiting for a reply
157 * @transmitting: CEC messages currently being transmitted
158 * @transmit_in_progress: true if a transmit is in progress
159 * @kthread_config: kthread used to configure a CEC adapter
160 * @config_completion: used to signal completion of the config kthread
161 * @kthread: main CEC processing thread
162 * @kthread_waitq: main CEC processing wait_queue
163 * @ops: cec adapter ops
164 * @priv: cec driver's private data
165 * @capabilities: cec adapter capabilities
166 * @available_log_addrs: maximum number of available logical addresses
167 * @phys_addr: the current physical address
168 * @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high
169 * in order to transmit or receive CEC messages. This is usually a HW
171 * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
172 * @is_configured: the CEC adapter is configured (i.e. has claimed LAs)
173 * @cec_pin_is_high: if true then the CEC pin is high. Only used with the
175 * @adap_controls_phys_addr: if true, then the CEC adapter controls the
176 * physical address, i.e. the CEC hardware can detect HPD changes and
177 * read the EDID and is not dependent on an external HDMI driver.
178 * Drivers that need this can set this field to true after the
179 * cec_allocate_adapter() call.
180 * @last_initiator: the initiator of the last transmitted message.
181 * @monitor_all_cnt: number of filehandles monitoring all msgs
182 * @monitor_pin_cnt: number of filehandles monitoring pin changes
183 * @follower_cnt: number of filehandles in follower mode
184 * @cec_follower: filehandle of the exclusive follower
185 * @cec_initiator: filehandle of the exclusive initiator
186 * @passthrough: if true, then the exclusive follower is in
188 * @log_addrs: current logical addresses
189 * @conn_info: current connector info
190 * @tx_timeouts: number of transmit timeouts
191 * @notifier: CEC notifier
192 * @pin: CEC pin status struct
193 * @cec_dir: debugfs cec directory
194 * @status_file: debugfs cec status file
195 * @error_inj_file: debugfs cec error injection file
196 * @sequence: transmit sequence counter
197 * @input_phys: remote control input_phys name
199 * This structure represents a cec adapter.
202 struct module *owner;
204 struct cec_devnode devnode;
208 struct list_head transmit_queue;
209 unsigned int transmit_queue_sz;
210 struct list_head wait_queue;
211 struct cec_data *transmitting;
212 bool transmit_in_progress;
214 struct task_struct *kthread_config;
215 struct completion config_completion;
217 struct task_struct *kthread;
218 wait_queue_head_t kthread_waitq;
220 const struct cec_adap_ops *ops;
223 u8 available_log_addrs;
229 bool cec_pin_is_high;
230 bool adap_controls_phys_addr;
235 struct cec_fh *cec_follower;
236 struct cec_fh *cec_initiator;
238 struct cec_log_addrs log_addrs;
239 struct cec_connector_info conn_info;
243 #ifdef CONFIG_CEC_NOTIFIER
244 struct cec_notifier *notifier;
246 #ifdef CONFIG_CEC_PIN
250 struct dentry *cec_dir;
251 struct dentry *status_file;
252 struct dentry *error_inj_file;
259 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
264 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
266 return adap->log_addrs.log_addr_mask & (1 << log_addr);
269 static inline bool cec_is_sink(const struct cec_adapter *adap)
271 return adap->phys_addr == 0;
275 * cec_is_registered() - is the CEC adapter registered?
277 * @adap: the CEC adapter, may be NULL.
279 * Return: true if the adapter is registered, false otherwise.
281 static inline bool cec_is_registered(const struct cec_adapter *adap)
283 return adap && adap->devnode.registered;
286 #define cec_phys_addr_exp(pa) \
287 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
290 struct drm_connector;
292 #if IS_REACHABLE(CONFIG_CEC_CORE)
293 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
294 void *priv, const char *name, u32 caps, u8 available_las);
295 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
296 void cec_unregister_adapter(struct cec_adapter *adap);
297 void cec_delete_adapter(struct cec_adapter *adap);
299 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
301 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
303 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
304 const struct edid *edid);
305 void cec_s_conn_info(struct cec_adapter *adap,
306 const struct cec_connector_info *conn_info);
307 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
310 /* Called by the adapter */
311 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
312 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
313 u8 error_cnt, ktime_t ts);
315 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
316 u8 arb_lost_cnt, u8 nack_cnt,
317 u8 low_drive_cnt, u8 error_cnt)
319 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
320 low_drive_cnt, error_cnt, ktime_get());
323 * Simplified version of cec_transmit_done for hardware that doesn't retry
324 * failed transmits. So this is always just one attempt in which case
325 * the status is sufficient.
327 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
328 u8 status, ktime_t ts);
330 static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
333 cec_transmit_attempt_done_ts(adap, status, ktime_get());
336 void cec_received_msg_ts(struct cec_adapter *adap,
337 struct cec_msg *msg, ktime_t ts);
339 static inline void cec_received_msg(struct cec_adapter *adap,
342 cec_received_msg_ts(adap, msg, ktime_get());
346 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
348 * @adap: pointer to the cec adapter
349 * @is_high: when true the CEC pin is high, otherwise it is low
350 * @dropped_events: when true some events were dropped
351 * @ts: the timestamp for this event
354 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
355 bool dropped_events, ktime_t ts);
358 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
360 * @adap: pointer to the cec adapter
361 * @is_high: when true the HPD pin is high, otherwise it is low
362 * @ts: the timestamp for this event
365 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
368 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
370 * @adap: pointer to the cec adapter
371 * @is_high: when true the 5V pin is high, otherwise it is low
372 * @ts: the timestamp for this event
375 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
378 * cec_get_edid_phys_addr() - find and return the physical address
380 * @edid: pointer to the EDID data
381 * @size: size in bytes of the EDID data
382 * @offset: If not %NULL then the location of the physical address
383 * bytes in the EDID will be returned here. This is set to 0
384 * if there is no physical address found.
386 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
388 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
389 unsigned int *offset);
391 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
392 const struct drm_connector *connector);
396 static inline int cec_register_adapter(struct cec_adapter *adap,
397 struct device *parent)
402 static inline void cec_unregister_adapter(struct cec_adapter *adap)
406 static inline void cec_delete_adapter(struct cec_adapter *adap)
410 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
415 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
416 const struct edid *edid)
420 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
421 unsigned int *offset)
425 return CEC_PHYS_ADDR_INVALID;
428 static inline void cec_s_conn_info(struct cec_adapter *adap,
429 const struct cec_connector_info *conn_info)
434 cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
435 const struct drm_connector *connector)
437 memset(conn_info, 0, sizeof(*conn_info));
443 * cec_phys_addr_invalidate() - set the physical address to INVALID
445 * @adap: the CEC adapter
447 * This is a simple helper function to invalidate the physical
450 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
452 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
456 * cec_get_edid_spa_location() - find location of the Source Physical Address
459 * @size: the size of the EDID
461 * This EDID is expected to be a CEA-861 compliant, which means that there are
462 * at least two blocks and one or more of the extensions blocks are CEA-861
465 * The returned location is guaranteed to be <= size-2.
467 * This is an inline function since it is used by both CEC and V4L2.
468 * Ideally this would go in a module shared by both, but it is overkill to do
469 * that for just a single function.
471 static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
474 unsigned int blocks = size / 128;
478 /* Sanity check: at least 2 blocks and a multiple of the block size */
479 if (blocks < 2 || size % 128)
483 * If there are fewer extension blocks than the size, then update
484 * 'blocks'. It is allowed to have more extension blocks than the size,
485 * since some hardware can only read e.g. 256 bytes of the EDID, even
486 * though more blocks are present. The first CEA-861 extension block
487 * should normally be in block 1 anyway.
489 if (edid[0x7e] + 1 < blocks)
490 blocks = edid[0x7e] + 1;
492 for (block = 1; block < blocks; block++) {
493 unsigned int offset = block * 128;
495 /* Skip any non-CEA-861 extension blocks */
496 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
499 /* search Vendor Specific Data Block (tag 3) */
500 d = edid[offset + 2] & 0x7f;
501 /* Check if there are Data Blocks */
505 unsigned int i = offset + 4;
506 unsigned int end = offset + d;
508 /* Note: 'end' is always < 'size' */
510 u8 tag = edid[i] >> 5;
511 u8 len = edid[i] & 0x1f;
513 if (tag == 3 && len >= 5 && i + len <= end &&
514 edid[i + 1] == 0x03 &&
515 edid[i + 2] == 0x0c &&
525 #endif /* _MEDIA_CEC_H */