xref: /openbmc/linux/include/media/cec.h (revision 9a6b55ac)
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  * @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)
33  *
34  * This structure represents a cec-related device node.
35  *
36  * The @parent is a physical device. It must be set by core or device drivers
37  * before registering the node.
38  */
39 struct cec_devnode {
40 	/* sysfs */
41 	struct device dev;
42 	struct cdev cdev;
43 
44 	/* device info */
45 	int minor;
46 	bool registered;
47 	bool unregistered;
48 	struct list_head fhs;
49 	struct mutex lock;
50 };
51 
52 struct cec_adapter;
53 struct cec_data;
54 struct cec_pin;
55 struct cec_notifier;
56 
57 struct cec_data {
58 	struct list_head list;
59 	struct list_head xfer_list;
60 	struct cec_adapter *adap;
61 	struct cec_msg msg;
62 	struct cec_fh *fh;
63 	struct delayed_work work;
64 	struct completion c;
65 	u8 attempts;
66 	bool blocking;
67 	bool completed;
68 };
69 
70 struct cec_msg_entry {
71 	struct list_head	list;
72 	struct cec_msg		msg;
73 };
74 
75 struct cec_event_entry {
76 	struct list_head	list;
77 	struct cec_event	ev;
78 };
79 
80 #define CEC_NUM_CORE_EVENTS 2
81 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
82 
83 struct cec_fh {
84 	struct list_head	list;
85 	struct list_head	xfer_list;
86 	struct cec_adapter	*adap;
87 	u8			mode_initiator;
88 	u8			mode_follower;
89 
90 	/* Events */
91 	wait_queue_head_t	wait;
92 	struct mutex		lock;
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;
99 };
100 
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
104 
105 /* The nominal data bit period is 2.4 ms */
106 #define CEC_FREE_TIME_TO_USEC(ft)		((ft) * 2400)
107 
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);
118 
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);
122 
123 	/* High-level CEC message callback */
124 	int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
125 };
126 
127 /*
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.
131  *
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.
136  */
137 #define CEC_MAX_MSG_RX_QUEUE_SZ		(18 * 3)
138 
139 /*
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
143  * reasonable.
144  */
145 #define CEC_MAX_MSG_TX_QUEUE_SZ		(18 * 1)
146 
147 struct cec_adapter {
148 	struct module *owner;
149 	char name[32];
150 	struct cec_devnode devnode;
151 	struct mutex lock;
152 	struct rc_dev *rc;
153 
154 	struct list_head transmit_queue;
155 	unsigned int transmit_queue_sz;
156 	struct list_head wait_queue;
157 	struct cec_data *transmitting;
158 	bool transmit_in_progress;
159 
160 	struct task_struct *kthread_config;
161 	struct completion config_completion;
162 
163 	struct task_struct *kthread;
164 	wait_queue_head_t kthread_waitq;
165 	wait_queue_head_t waitq;
166 
167 	const struct cec_adap_ops *ops;
168 	void *priv;
169 	u32 capabilities;
170 	u8 available_log_addrs;
171 
172 	u16 phys_addr;
173 	bool needs_hpd;
174 	bool is_configuring;
175 	bool is_configured;
176 	bool cec_pin_is_high;
177 	u8 last_initiator;
178 	u32 monitor_all_cnt;
179 	u32 monitor_pin_cnt;
180 	u32 follower_cnt;
181 	struct cec_fh *cec_follower;
182 	struct cec_fh *cec_initiator;
183 	bool passthrough;
184 	struct cec_log_addrs log_addrs;
185 	struct cec_connector_info conn_info;
186 
187 	u32 tx_timeouts;
188 
189 #ifdef CONFIG_CEC_NOTIFIER
190 	struct cec_notifier *notifier;
191 #endif
192 #ifdef CONFIG_CEC_PIN
193 	struct cec_pin *pin;
194 #endif
195 
196 	struct dentry *cec_dir;
197 	struct dentry *status_file;
198 	struct dentry *error_inj_file;
199 
200 	u16 phys_addrs[15];
201 	u32 sequence;
202 
203 	char input_phys[32];
204 };
205 
206 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
207 {
208 	return adap->priv;
209 }
210 
211 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
212 {
213 	return adap->log_addrs.log_addr_mask & (1 << log_addr);
214 }
215 
216 static inline bool cec_is_sink(const struct cec_adapter *adap)
217 {
218 	return adap->phys_addr == 0;
219 }
220 
221 /**
222  * cec_is_registered() - is the CEC adapter registered?
223  *
224  * @adap:	the CEC adapter, may be NULL.
225  *
226  * Return: true if the adapter is registered, false otherwise.
227  */
228 static inline bool cec_is_registered(const struct cec_adapter *adap)
229 {
230 	return adap && adap->devnode.registered;
231 }
232 
233 #define cec_phys_addr_exp(pa) \
234 	((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
235 
236 struct edid;
237 struct drm_connector;
238 
239 #if IS_REACHABLE(CONFIG_CEC_CORE)
240 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
241 		void *priv, const char *name, u32 caps, u8 available_las);
242 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
243 void cec_unregister_adapter(struct cec_adapter *adap);
244 void cec_delete_adapter(struct cec_adapter *adap);
245 
246 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
247 		    bool block);
248 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
249 		     bool block);
250 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
251 			       const struct edid *edid);
252 void cec_s_conn_info(struct cec_adapter *adap,
253 		     const struct cec_connector_info *conn_info);
254 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
255 		     bool block);
256 
257 /* Called by the adapter */
258 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
259 			  u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
260 			  u8 error_cnt, ktime_t ts);
261 
262 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
263 				     u8 arb_lost_cnt, u8 nack_cnt,
264 				     u8 low_drive_cnt, u8 error_cnt)
265 {
266 	cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
267 			     low_drive_cnt, error_cnt, ktime_get());
268 }
269 /*
270  * Simplified version of cec_transmit_done for hardware that doesn't retry
271  * failed transmits. So this is always just one attempt in which case
272  * the status is sufficient.
273  */
274 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
275 				  u8 status, ktime_t ts);
276 
277 static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
278 					     u8 status)
279 {
280 	cec_transmit_attempt_done_ts(adap, status, ktime_get());
281 }
282 
283 void cec_received_msg_ts(struct cec_adapter *adap,
284 			 struct cec_msg *msg, ktime_t ts);
285 
286 static inline void cec_received_msg(struct cec_adapter *adap,
287 				    struct cec_msg *msg)
288 {
289 	cec_received_msg_ts(adap, msg, ktime_get());
290 }
291 
292 /**
293  * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
294  *
295  * @adap:	pointer to the cec adapter
296  * @is_high:	when true the CEC pin is high, otherwise it is low
297  * @dropped_events: when true some events were dropped
298  * @ts:		the timestamp for this event
299  *
300  */
301 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
302 			     bool dropped_events, ktime_t ts);
303 
304 /**
305  * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
306  *
307  * @adap:	pointer to the cec adapter
308  * @is_high:	when true the HPD pin is high, otherwise it is low
309  * @ts:		the timestamp for this event
310  *
311  */
312 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
313 
314 /**
315  * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
316  *
317  * @adap:	pointer to the cec adapter
318  * @is_high:	when true the 5V pin is high, otherwise it is low
319  * @ts:		the timestamp for this event
320  *
321  */
322 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
323 
324 /**
325  * cec_get_edid_phys_addr() - find and return the physical address
326  *
327  * @edid:	pointer to the EDID data
328  * @size:	size in bytes of the EDID data
329  * @offset:	If not %NULL then the location of the physical address
330  *		bytes in the EDID will be returned here. This is set to 0
331  *		if there is no physical address found.
332  *
333  * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
334  */
335 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
336 			   unsigned int *offset);
337 
338 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
339 				 const struct drm_connector *connector);
340 
341 #else
342 
343 static inline int cec_register_adapter(struct cec_adapter *adap,
344 				       struct device *parent)
345 {
346 	return 0;
347 }
348 
349 static inline void cec_unregister_adapter(struct cec_adapter *adap)
350 {
351 }
352 
353 static inline void cec_delete_adapter(struct cec_adapter *adap)
354 {
355 }
356 
357 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
358 				   bool block)
359 {
360 }
361 
362 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
363 					     const struct edid *edid)
364 {
365 }
366 
367 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
368 					 unsigned int *offset)
369 {
370 	if (offset)
371 		*offset = 0;
372 	return CEC_PHYS_ADDR_INVALID;
373 }
374 
375 static inline void cec_s_conn_info(struct cec_adapter *adap,
376 				   const struct cec_connector_info *conn_info)
377 {
378 }
379 
380 static inline void
381 cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
382 			    const struct drm_connector *connector)
383 {
384 	memset(conn_info, 0, sizeof(*conn_info));
385 }
386 
387 #endif
388 
389 #if IS_REACHABLE(CONFIG_CEC_CORE) && IS_ENABLED(CONFIG_CEC_NOTIFIER)
390 
391 /**
392  * cec_notifier_register - register a callback with the notifier
393  * @n: the CEC notifier
394  * @adap: the CEC adapter, passed as argument to the callback function
395  * @callback: the callback function
396  */
397 void cec_notifier_register(struct cec_notifier *n,
398 			   struct cec_adapter *adap,
399 			   void (*callback)(struct cec_adapter *adap, u16 pa));
400 
401 /**
402  * cec_notifier_unregister - unregister the callback from the notifier.
403  * @n: the CEC notifier
404  */
405 void cec_notifier_unregister(struct cec_notifier *n);
406 
407 /**
408  * cec_register_cec_notifier - register the notifier with the cec adapter.
409  * @adap: the CEC adapter
410  * @notifier: the CEC notifier
411  */
412 void cec_register_cec_notifier(struct cec_adapter *adap,
413 			       struct cec_notifier *notifier);
414 
415 #else
416 
417 static inline void
418 cec_notifier_register(struct cec_notifier *n,
419 		      struct cec_adapter *adap,
420 		      void (*callback)(struct cec_adapter *adap, u16 pa))
421 {
422 }
423 
424 static inline void cec_notifier_unregister(struct cec_notifier *n)
425 {
426 }
427 
428 static inline void cec_register_cec_notifier(struct cec_adapter *adap,
429 					     struct cec_notifier *notifier)
430 {
431 }
432 
433 #endif
434 
435 /**
436  * cec_phys_addr_invalidate() - set the physical address to INVALID
437  *
438  * @adap:	the CEC adapter
439  *
440  * This is a simple helper function to invalidate the physical
441  * address.
442  */
443 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
444 {
445 	cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
446 }
447 
448 /**
449  * cec_get_edid_spa_location() - find location of the Source Physical Address
450  *
451  * @edid: the EDID
452  * @size: the size of the EDID
453  *
454  * This EDID is expected to be a CEA-861 compliant, which means that there are
455  * at least two blocks and one or more of the extensions blocks are CEA-861
456  * blocks.
457  *
458  * The returned location is guaranteed to be <= size-2.
459  *
460  * This is an inline function since it is used by both CEC and V4L2.
461  * Ideally this would go in a module shared by both, but it is overkill to do
462  * that for just a single function.
463  */
464 static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
465 						     unsigned int size)
466 {
467 	unsigned int blocks = size / 128;
468 	unsigned int block;
469 	u8 d;
470 
471 	/* Sanity check: at least 2 blocks and a multiple of the block size */
472 	if (blocks < 2 || size % 128)
473 		return 0;
474 
475 	/*
476 	 * If there are fewer extension blocks than the size, then update
477 	 * 'blocks'. It is allowed to have more extension blocks than the size,
478 	 * since some hardware can only read e.g. 256 bytes of the EDID, even
479 	 * though more blocks are present. The first CEA-861 extension block
480 	 * should normally be in block 1 anyway.
481 	 */
482 	if (edid[0x7e] + 1 < blocks)
483 		blocks = edid[0x7e] + 1;
484 
485 	for (block = 1; block < blocks; block++) {
486 		unsigned int offset = block * 128;
487 
488 		/* Skip any non-CEA-861 extension blocks */
489 		if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
490 			continue;
491 
492 		/* search Vendor Specific Data Block (tag 3) */
493 		d = edid[offset + 2] & 0x7f;
494 		/* Check if there are Data Blocks */
495 		if (d <= 4)
496 			continue;
497 		if (d > 4) {
498 			unsigned int i = offset + 4;
499 			unsigned int end = offset + d;
500 
501 			/* Note: 'end' is always < 'size' */
502 			do {
503 				u8 tag = edid[i] >> 5;
504 				u8 len = edid[i] & 0x1f;
505 
506 				if (tag == 3 && len >= 5 && i + len <= end &&
507 				    edid[i + 1] == 0x03 &&
508 				    edid[i + 2] == 0x0c &&
509 				    edid[i + 3] == 0x00)
510 					return i + 4;
511 				i += len + 1;
512 			} while (i < end);
513 		}
514 	}
515 	return 0;
516 }
517 
518 #endif /* _MEDIA_CEC_H */
519