Linux Audio

Check our new training course

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