xref: /openbmc/linux/include/media/cec.h (revision f2d8b6917f3bcfb3190eb80567fea71a9b59dbd3)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * cec - HDMI Consumer Electronics Control support header
4  *
5  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #ifndef _MEDIA_CEC_H
9 #define _MEDIA_CEC_H
10 
11 #include <linux/poll.h>
12 #include <linux/fs.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>
20 
21 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
22 			  CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
23 
24 /**
25  * struct cec_devnode - cec device node
26  * @dev:	cec device
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)
34  *
35  * This structure represents a cec-related device node.
36  *
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.
39  *
40  * The @parent is a physical device. It must be set by core or device drivers
41  * before registering the node.
42  */
43 struct cec_devnode {
44 	/* sysfs */
45 	struct device dev;
46 	struct cdev cdev;
47 
48 	/* device info */
49 	int minor;
50 	/* serialize open/release and registration */
51 	struct mutex lock;
52 	bool registered;
53 	bool unregistered;
54 	/* protect access to fhs */
55 	struct mutex lock_fhs;
56 	struct list_head fhs;
57 };
58 
59 struct cec_adapter;
60 struct cec_data;
61 struct cec_pin;
62 struct cec_notifier;
63 
64 struct cec_data {
65 	struct list_head list;
66 	struct list_head xfer_list;
67 	struct cec_adapter *adap;
68 	struct cec_msg msg;
69 	struct cec_fh *fh;
70 	struct delayed_work work;
71 	struct completion c;
72 	u8 attempts;
73 	bool blocking;
74 	bool completed;
75 };
76 
77 struct cec_msg_entry {
78 	struct list_head	list;
79 	struct cec_msg		msg;
80 };
81 
82 struct cec_event_entry {
83 	struct list_head	list;
84 	struct cec_event	ev;
85 };
86 
87 #define CEC_NUM_CORE_EVENTS 2
88 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
89 
90 struct cec_fh {
91 	struct list_head	list;
92 	struct list_head	xfer_list;
93 	struct cec_adapter	*adap;
94 	u8			mode_initiator;
95 	u8			mode_follower;
96 
97 	/* Events */
98 	wait_queue_head_t	wait;
99 	struct mutex		lock;
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;
106 };
107 
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
111 
112 /* The nominal data bit period is 2.4 ms */
113 #define CEC_FREE_TIME_TO_USEC(ft)		((ft) * 2400)
114 
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_status)(struct cec_adapter *adap, struct seq_file *file);
125 	void (*adap_free)(struct cec_adapter *adap);
126 
127 	/* Error injection callbacks */
128 	int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
129 	bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
130 
131 	/* High-level CEC message callback */
132 	int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
133 };
134 
135 /*
136  * The minimum message length you can receive (excepting poll messages) is 2.
137  * With a transfer rate of at most 36 bytes per second this makes 18 messages
138  * per second worst case.
139  *
140  * We queue at most 3 seconds worth of received messages. The CEC specification
141  * requires that messages are replied to within a second, so 3 seconds should
142  * give more than enough margin. Since most messages are actually more than 2
143  * bytes, this is in practice a lot more than 3 seconds.
144  */
145 #define CEC_MAX_MSG_RX_QUEUE_SZ		(18 * 3)
146 
147 /*
148  * The transmit queue is limited to 1 second worth of messages (worst case).
149  * Messages can be transmitted by userspace and kernel space. But for both it
150  * makes no sense to have a lot of messages queued up. One second seems
151  * reasonable.
152  */
153 #define CEC_MAX_MSG_TX_QUEUE_SZ		(18 * 1)
154 
155 /**
156  * struct cec_adapter - cec adapter structure
157  * @owner:		module owner
158  * @name:		name of the CEC adapter
159  * @devnode:		device node for the /dev/cecX device
160  * @lock:		mutex controlling access to this structure
161  * @rc:			remote control device
162  * @transmit_queue:	queue of pending transmits
163  * @transmit_queue_sz:	number of pending transmits
164  * @wait_queue:		queue of transmits waiting for a reply
165  * @transmitting:	CEC messages currently being transmitted
166  * @transmit_in_progress: true if a transmit is in progress
167  * @transmit_in_progress_aborted: true if a transmit is in progress is to be
168  *			aborted. This happens if the logical address is
169  *			invalidated while the transmit is ongoing. In that
170  *			case the transmit will finish, but will not retransmit
171  *			and be marked as ABORTED.
172  * @xfer_timeout_ms:	the transfer timeout in ms.
173  *			If 0, then timeout after 2.1 ms.
174  * @kthread_config:	kthread used to configure a CEC adapter
175  * @config_completion:	used to signal completion of the config kthread
176  * @kthread:		main CEC processing thread
177  * @kthread_waitq:	main CEC processing wait_queue
178  * @ops:		cec adapter ops
179  * @priv:		cec driver's private data
180  * @capabilities:	cec adapter capabilities
181  * @available_log_addrs: maximum number of available logical addresses
182  * @phys_addr:		the current physical address
183  * @needs_hpd:		if true, then the HDMI HotPlug Detect pin must be high
184  *	in order to transmit or receive CEC messages. This is usually a HW
185  *	limitation.
186  * @is_configuring:	the CEC adapter is configuring (i.e. claiming LAs)
187  * @is_configured:	the CEC adapter is configured (i.e. has claimed LAs)
188  * @cec_pin_is_high:	if true then the CEC pin is high. Only used with the
189  *	CEC pin framework.
190  * @adap_controls_phys_addr: if true, then the CEC adapter controls the
191  *	physical address, i.e. the CEC hardware can detect HPD changes and
192  *	read the EDID and is not dependent on an external HDMI driver.
193  *	Drivers that need this can set this field to true after the
194  *	cec_allocate_adapter() call.
195  * @last_initiator:	the initiator of the last transmitted message.
196  * @activate_cnt:	number of times that CEC is activated
197  * @monitor_all_cnt:	number of filehandles monitoring all msgs
198  * @monitor_pin_cnt:	number of filehandles monitoring pin changes
199  * @follower_cnt:	number of filehandles in follower mode
200  * @cec_follower:	filehandle of the exclusive follower
201  * @cec_initiator:	filehandle of the exclusive initiator
202  * @passthrough:	if true, then the exclusive follower is in
203  *	passthrough mode.
204  * @log_addrs:		current logical addresses
205  * @conn_info:		current connector info
206  * @tx_timeouts:	number of transmit timeouts
207  * @notifier:		CEC notifier
208  * @pin:		CEC pin status struct
209  * @cec_dir:		debugfs cec directory
210  * @status_file:	debugfs cec status file
211  * @error_inj_file:	debugfs cec error injection file
212  * @sequence:		transmit sequence counter
213  * @input_phys:		remote control input_phys name
214  *
215  * This structure represents a cec adapter.
216  */
217 struct cec_adapter {
218 	struct module *owner;
219 	char name[32];
220 	struct cec_devnode devnode;
221 	struct mutex lock;
222 	struct rc_dev *rc;
223 
224 	struct list_head transmit_queue;
225 	unsigned int transmit_queue_sz;
226 	struct list_head wait_queue;
227 	struct cec_data *transmitting;
228 	bool transmit_in_progress;
229 	bool transmit_in_progress_aborted;
230 	unsigned int xfer_timeout_ms;
231 
232 	struct task_struct *kthread_config;
233 	struct completion config_completion;
234 
235 	struct task_struct *kthread;
236 	wait_queue_head_t kthread_waitq;
237 
238 	const struct cec_adap_ops *ops;
239 	void *priv;
240 	u32 capabilities;
241 	u8 available_log_addrs;
242 
243 	u16 phys_addr;
244 	bool needs_hpd;
245 	bool is_configuring;
246 	bool is_configured;
247 	bool cec_pin_is_high;
248 	bool adap_controls_phys_addr;
249 	u8 last_initiator;
250 	u32 activate_cnt;
251 	u32 monitor_all_cnt;
252 	u32 monitor_pin_cnt;
253 	u32 follower_cnt;
254 	struct cec_fh *cec_follower;
255 	struct cec_fh *cec_initiator;
256 	bool passthrough;
257 	struct cec_log_addrs log_addrs;
258 	struct cec_connector_info conn_info;
259 
260 	u32 tx_timeouts;
261 
262 #ifdef CONFIG_CEC_NOTIFIER
263 	struct cec_notifier *notifier;
264 #endif
265 #ifdef CONFIG_CEC_PIN
266 	struct cec_pin *pin;
267 #endif
268 
269 	struct dentry *cec_dir;
270 
271 	u32 sequence;
272 
273 	char input_phys[32];
274 };
275 
276 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
277 {
278 	return adap->priv;
279 }
280 
281 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
282 {
283 	return adap->log_addrs.log_addr_mask & (1 << log_addr);
284 }
285 
286 static inline bool cec_is_sink(const struct cec_adapter *adap)
287 {
288 	return adap->phys_addr == 0;
289 }
290 
291 /**
292  * cec_is_registered() - is the CEC adapter registered?
293  *
294  * @adap:	the CEC adapter, may be NULL.
295  *
296  * Return: true if the adapter is registered, false otherwise.
297  */
298 static inline bool cec_is_registered(const struct cec_adapter *adap)
299 {
300 	return adap && adap->devnode.registered;
301 }
302 
303 #define cec_phys_addr_exp(pa) \
304 	((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
305 
306 struct edid;
307 struct drm_connector;
308 
309 #if IS_REACHABLE(CONFIG_CEC_CORE)
310 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
311 		void *priv, const char *name, u32 caps, u8 available_las);
312 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
313 void cec_unregister_adapter(struct cec_adapter *adap);
314 void cec_delete_adapter(struct cec_adapter *adap);
315 
316 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
317 		    bool block);
318 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
319 		     bool block);
320 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
321 			       const struct edid *edid);
322 void cec_s_conn_info(struct cec_adapter *adap,
323 		     const struct cec_connector_info *conn_info);
324 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
325 		     bool block);
326 
327 /* Called by the adapter */
328 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
329 			  u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
330 			  u8 error_cnt, ktime_t ts);
331 
332 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
333 				     u8 arb_lost_cnt, u8 nack_cnt,
334 				     u8 low_drive_cnt, u8 error_cnt)
335 {
336 	cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
337 			     low_drive_cnt, error_cnt, ktime_get());
338 }
339 /*
340  * Simplified version of cec_transmit_done for hardware that doesn't retry
341  * failed transmits. So this is always just one attempt in which case
342  * the status is sufficient.
343  */
344 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
345 				  u8 status, ktime_t ts);
346 
347 static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
348 					     u8 status)
349 {
350 	cec_transmit_attempt_done_ts(adap, status, ktime_get());
351 }
352 
353 void cec_received_msg_ts(struct cec_adapter *adap,
354 			 struct cec_msg *msg, ktime_t ts);
355 
356 static inline void cec_received_msg(struct cec_adapter *adap,
357 				    struct cec_msg *msg)
358 {
359 	cec_received_msg_ts(adap, msg, ktime_get());
360 }
361 
362 /**
363  * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
364  *
365  * @adap:	pointer to the cec adapter
366  * @is_high:	when true the CEC pin is high, otherwise it is low
367  * @dropped_events: when true some events were dropped
368  * @ts:		the timestamp for this event
369  *
370  */
371 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
372 			     bool dropped_events, ktime_t ts);
373 
374 /**
375  * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
376  *
377  * @adap:	pointer to the cec adapter
378  * @is_high:	when true the HPD pin is high, otherwise it is low
379  * @ts:		the timestamp for this event
380  *
381  */
382 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
383 
384 /**
385  * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
386  *
387  * @adap:	pointer to the cec adapter
388  * @is_high:	when true the 5V pin is high, otherwise it is low
389  * @ts:		the timestamp for this event
390  *
391  */
392 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
393 
394 /**
395  * cec_get_edid_phys_addr() - find and return the physical address
396  *
397  * @edid:	pointer to the EDID data
398  * @size:	size in bytes of the EDID data
399  * @offset:	If not %NULL then the location of the physical address
400  *		bytes in the EDID will be returned here. This is set to 0
401  *		if there is no physical address found.
402  *
403  * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
404  */
405 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
406 			   unsigned int *offset);
407 
408 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
409 				 const struct drm_connector *connector);
410 
411 #else
412 
413 static inline int cec_register_adapter(struct cec_adapter *adap,
414 				       struct device *parent)
415 {
416 	return 0;
417 }
418 
419 static inline void cec_unregister_adapter(struct cec_adapter *adap)
420 {
421 }
422 
423 static inline void cec_delete_adapter(struct cec_adapter *adap)
424 {
425 }
426 
427 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
428 				   bool block)
429 {
430 }
431 
432 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
433 					     const struct edid *edid)
434 {
435 }
436 
437 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
438 					 unsigned int *offset)
439 {
440 	if (offset)
441 		*offset = 0;
442 	return CEC_PHYS_ADDR_INVALID;
443 }
444 
445 static inline void cec_s_conn_info(struct cec_adapter *adap,
446 				   const struct cec_connector_info *conn_info)
447 {
448 }
449 
450 static inline void
451 cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
452 			    const struct drm_connector *connector)
453 {
454 	memset(conn_info, 0, sizeof(*conn_info));
455 }
456 
457 #endif
458 
459 /**
460  * cec_phys_addr_invalidate() - set the physical address to INVALID
461  *
462  * @adap:	the CEC adapter
463  *
464  * This is a simple helper function to invalidate the physical
465  * address.
466  */
467 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
468 {
469 	cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
470 }
471 
472 /**
473  * cec_get_edid_spa_location() - find location of the Source Physical Address
474  *
475  * @edid: the EDID
476  * @size: the size of the EDID
477  *
478  * This EDID is expected to be a CEA-861 compliant, which means that there are
479  * at least two blocks and one or more of the extensions blocks are CEA-861
480  * blocks.
481  *
482  * The returned location is guaranteed to be <= size-2.
483  *
484  * This is an inline function since it is used by both CEC and V4L2.
485  * Ideally this would go in a module shared by both, but it is overkill to do
486  * that for just a single function.
487  */
488 static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
489 						     unsigned int size)
490 {
491 	unsigned int blocks = size / 128;
492 	unsigned int block;
493 	u8 d;
494 
495 	/* Sanity check: at least 2 blocks and a multiple of the block size */
496 	if (blocks < 2 || size % 128)
497 		return 0;
498 
499 	/*
500 	 * If there are fewer extension blocks than the size, then update
501 	 * 'blocks'. It is allowed to have more extension blocks than the size,
502 	 * since some hardware can only read e.g. 256 bytes of the EDID, even
503 	 * though more blocks are present. The first CEA-861 extension block
504 	 * should normally be in block 1 anyway.
505 	 */
506 	if (edid[0x7e] + 1 < blocks)
507 		blocks = edid[0x7e] + 1;
508 
509 	for (block = 1; block < blocks; block++) {
510 		unsigned int offset = block * 128;
511 
512 		/* Skip any non-CEA-861 extension blocks */
513 		if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
514 			continue;
515 
516 		/* search Vendor Specific Data Block (tag 3) */
517 		d = edid[offset + 2] & 0x7f;
518 		/* Check if there are Data Blocks */
519 		if (d <= 4)
520 			continue;
521 		if (d > 4) {
522 			unsigned int i = offset + 4;
523 			unsigned int end = offset + d;
524 
525 			/* Note: 'end' is always < 'size' */
526 			do {
527 				u8 tag = edid[i] >> 5;
528 				u8 len = edid[i] & 0x1f;
529 
530 				if (tag == 3 && len >= 5 && i + len <= end &&
531 				    edid[i + 1] == 0x03 &&
532 				    edid[i + 2] == 0x0c &&
533 				    edid[i + 3] == 0x00)
534 					return i + 4;
535 				i += len + 1;
536 			} while (i < end);
537 		}
538 	}
539 	return 0;
540 }
541 
542 #endif /* _MEDIA_CEC_H */
543