blob: 77346f757036dc4283b6107bc5a0b6076fe339cc [file] [log] [blame]
Hans Verkuilab15d242018-02-07 09:05:46 -05001/* SPDX-License-Identifier: GPL-2.0-only */
Hans Verkuila56960e2016-06-25 09:43:58 -03002/*
3 * cec - HDMI Consumer Electronics Control support header
4 *
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
Hans Verkuila56960e2016-06-25 09:43:58 -03006 */
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>
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -040020
Hans Verkuilee0c5032017-08-04 06:41:51 -040021#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
22 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
23
Hans Verkuila56960e2016-06-25 09:43:58 -030024/**
25 * struct cec_devnode - cec device node
26 * @dev: cec device
27 * @cdev: cec character device
Hans Verkuila56960e2016-06-25 09:43:58 -030028 * @minor: device node minor number
Hans Verkuila9e61072021-12-01 13:41:26 +010029 * @lock: lock to serialize open/release and registration
Hans Verkuila56960e2016-06-25 09:43:58 -030030 * @registered: the device was correctly registered
31 * @unregistered: the device was unregistered
Hans Verkuila9e61072021-12-01 13:41:26 +010032 * @lock_fhs: lock to control access to @fhs
Hans Verkuila56960e2016-06-25 09:43:58 -030033 * @fhs: the list of open filehandles (cec_fh)
34 *
35 * This structure represents a cec-related device node.
36 *
Hans Verkuila9e61072021-12-01 13:41:26 +010037 * 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 *
Hans Verkuila56960e2016-06-25 09:43:58 -030040 * 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;
Hans Verkuila56960e2016-06-25 09:43:58 -030047
48 /* device info */
49 int minor;
Hans Verkuila9e61072021-12-01 13:41:26 +010050 /* serialize open/release and registration */
51 struct mutex lock;
Hans Verkuila56960e2016-06-25 09:43:58 -030052 bool registered;
53 bool unregistered;
Hans Verkuila9e61072021-12-01 13:41:26 +010054 /* protect access to fhs */
55 struct mutex lock_fhs;
Hans Verkuila56960e2016-06-25 09:43:58 -030056 struct list_head fhs;
57};
58
59struct cec_adapter;
60struct cec_data;
Hans Verkuilea5c8ef2017-07-11 03:30:42 -030061struct cec_pin;
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -040062struct cec_notifier;
Hans Verkuila56960e2016-06-25 09:43:58 -030063
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 struct cec_fh *fh;
70 struct delayed_work work;
71 struct completion c;
72 u8 attempts;
Hans Verkuila56960e2016-06-25 09:43:58 -030073 bool blocking;
74 bool completed;
75};
76
77struct cec_msg_entry {
78 struct list_head list;
79 struct cec_msg msg;
80};
81
Hans Verkuil6b2bbb02017-07-11 03:30:39 -030082struct cec_event_entry {
83 struct list_head list;
84 struct cec_event ev;
85};
86
87#define CEC_NUM_CORE_EVENTS 2
Hans Verkuil4786b0d2018-06-28 07:43:46 -040088#define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
Hans Verkuila56960e2016-06-25 09:43:58 -030089
90struct 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;
Hans Verkuila56960e2016-06-25 09:43:58 -030099 struct mutex lock;
Hans Verkuil6b2bbb02017-07-11 03:30:39 -0300100 struct list_head events[CEC_NUM_EVENTS]; /* queued events */
Hans Verkuil6ec1cbf2018-03-06 16:20:00 -0500101 u16 queued_events[CEC_NUM_EVENTS];
Hans Verkuil6b2bbb02017-07-11 03:30:39 -0300102 unsigned int total_queued_events;
103 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
Hans Verkuila56960e2016-06-25 09:43:58 -0300104 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
115struct 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);
Hans Verkuil48ea2e92017-11-05 07:36:36 -0500119 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
Hans Verkuila56960e2016-06-25 09:43:58 -0300120 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
121 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
122 u32 signal_free_time, struct cec_msg *msg);
123 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
Hans Verkuile6259b52017-07-11 03:30:36 -0300124 void (*adap_free)(struct cec_adapter *adap);
Hans Verkuila56960e2016-06-25 09:43:58 -0300125
Hans Verkuil9ca400c2017-10-31 09:55:09 -0400126 /* Error injection callbacks */
127 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
128 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
129
Hans Verkuila56960e2016-06-25 09:43:58 -0300130 /* High-level CEC message callback */
131 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
132};
133
134/*
135 * The minimum message length you can receive (excepting poll messages) is 2.
136 * With a transfer rate of at most 36 bytes per second this makes 18 messages
137 * per second worst case.
138 *
Hans Verkuil11065f82016-07-17 11:40:05 -0300139 * We queue at most 3 seconds worth of received messages. The CEC specification
140 * requires that messages are replied to within a second, so 3 seconds should
141 * give more than enough margin. Since most messages are actually more than 2
142 * bytes, this is in practice a lot more than 3 seconds.
Hans Verkuila56960e2016-06-25 09:43:58 -0300143 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300144#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
145
146/*
147 * The transmit queue is limited to 1 second worth of messages (worst case).
148 * Messages can be transmitted by userspace and kernel space. But for both it
149 * makes no sense to have a lot of messages queued up. One second seems
150 * reasonable.
151 */
152#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
Hans Verkuila56960e2016-06-25 09:43:58 -0300153
Hans Verkuile233f812020-06-20 12:16:41 +0200154/**
155 * struct cec_adapter - cec adapter structure
156 * @owner: module owner
157 * @name: name of the CEC adapter
158 * @devnode: device node for the /dev/cecX device
159 * @lock: mutex controlling access to this structure
160 * @rc: remote control device
161 * @transmit_queue: queue of pending transmits
162 * @transmit_queue_sz: number of pending transmits
163 * @wait_queue: queue of transmits waiting for a reply
164 * @transmitting: CEC messages currently being transmitted
165 * @transmit_in_progress: true if a transmit is in progress
166 * @kthread_config: kthread used to configure a CEC adapter
167 * @config_completion: used to signal completion of the config kthread
168 * @kthread: main CEC processing thread
169 * @kthread_waitq: main CEC processing wait_queue
170 * @ops: cec adapter ops
171 * @priv: cec driver's private data
172 * @capabilities: cec adapter capabilities
173 * @available_log_addrs: maximum number of available logical addresses
174 * @phys_addr: the current physical address
175 * @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high
176 * in order to transmit or receive CEC messages. This is usually a HW
177 * limitation.
178 * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
179 * @is_configured: the CEC adapter is configured (i.e. has claimed LAs)
180 * @cec_pin_is_high: if true then the CEC pin is high. Only used with the
181 * CEC pin framework.
Jeff Chase98f803c2020-06-23 01:59:49 +0200182 * @adap_controls_phys_addr: if true, then the CEC adapter controls the
183 * physical address, i.e. the CEC hardware can detect HPD changes and
184 * read the EDID and is not dependent on an external HDMI driver.
185 * Drivers that need this can set this field to true after the
186 * cec_allocate_adapter() call.
Hans Verkuile233f812020-06-20 12:16:41 +0200187 * @last_initiator: the initiator of the last transmitted message.
188 * @monitor_all_cnt: number of filehandles monitoring all msgs
189 * @monitor_pin_cnt: number of filehandles monitoring pin changes
190 * @follower_cnt: number of filehandles in follower mode
191 * @cec_follower: filehandle of the exclusive follower
192 * @cec_initiator: filehandle of the exclusive initiator
193 * @passthrough: if true, then the exclusive follower is in
194 * passthrough mode.
195 * @log_addrs: current logical addresses
196 * @conn_info: current connector info
197 * @tx_timeouts: number of transmit timeouts
198 * @notifier: CEC notifier
199 * @pin: CEC pin status struct
200 * @cec_dir: debugfs cec directory
201 * @status_file: debugfs cec status file
202 * @error_inj_file: debugfs cec error injection file
203 * @sequence: transmit sequence counter
204 * @input_phys: remote control input_phys name
205 *
206 * This structure represents a cec adapter.
207 */
Hans Verkuila56960e2016-06-25 09:43:58 -0300208struct cec_adapter {
209 struct module *owner;
210 char name[32];
211 struct cec_devnode devnode;
212 struct mutex lock;
213 struct rc_dev *rc;
214
215 struct list_head transmit_queue;
Hans Verkuil11065f82016-07-17 11:40:05 -0300216 unsigned int transmit_queue_sz;
Hans Verkuila56960e2016-06-25 09:43:58 -0300217 struct list_head wait_queue;
218 struct cec_data *transmitting;
Hans Verkuil32804fc2018-10-19 03:55:34 -0400219 bool transmit_in_progress;
Hans Verkuila56960e2016-06-25 09:43:58 -0300220
221 struct task_struct *kthread_config;
222 struct completion config_completion;
223
224 struct task_struct *kthread;
225 wait_queue_head_t kthread_waitq;
Hans Verkuila56960e2016-06-25 09:43:58 -0300226
227 const struct cec_adap_ops *ops;
228 void *priv;
229 u32 capabilities;
230 u8 available_log_addrs;
231
232 u16 phys_addr;
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300233 bool needs_hpd;
Hans Verkuila56960e2016-06-25 09:43:58 -0300234 bool is_configuring;
235 bool is_configured;
Hans Verkuil28e11b12017-08-20 06:53:10 -0400236 bool cec_pin_is_high;
Jeff Chase98f803c2020-06-23 01:59:49 +0200237 bool adap_controls_phys_addr;
Hans Verkuil7d867a12018-10-05 08:00:21 -0400238 u8 last_initiator;
Hans Verkuila56960e2016-06-25 09:43:58 -0300239 u32 monitor_all_cnt;
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300240 u32 monitor_pin_cnt;
Hans Verkuila56960e2016-06-25 09:43:58 -0300241 u32 follower_cnt;
242 struct cec_fh *cec_follower;
243 struct cec_fh *cec_initiator;
244 bool passthrough;
245 struct cec_log_addrs log_addrs;
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -0400246 struct cec_connector_info conn_info;
Hans Verkuila56960e2016-06-25 09:43:58 -0300247
Hans Verkuilbb789e02017-07-11 03:30:34 -0300248 u32 tx_timeouts;
249
Hans Verkuile94c3282017-05-28 05:58:04 -0300250#ifdef CONFIG_CEC_NOTIFIER
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200251 struct cec_notifier *notifier;
252#endif
Hans Verkuilea5c8ef2017-07-11 03:30:42 -0300253#ifdef CONFIG_CEC_PIN
254 struct cec_pin *pin;
255#endif
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200256
Hans Verkuila56960e2016-06-25 09:43:58 -0300257 struct dentry *cec_dir;
Hans Verkuila56960e2016-06-25 09:43:58 -0300258
Hans Verkuila56960e2016-06-25 09:43:58 -0300259 u32 sequence;
260
Hans Verkuila56960e2016-06-25 09:43:58 -0300261 char input_phys[32];
Hans Verkuila56960e2016-06-25 09:43:58 -0300262};
263
Jose Abreu0b0b97d2017-03-24 13:47:52 -0300264static inline void *cec_get_drvdata(const struct cec_adapter *adap)
265{
266 return adap->priv;
267}
268
Hans Verkuila56960e2016-06-25 09:43:58 -0300269static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
270{
271 return adap->log_addrs.log_addr_mask & (1 << log_addr);
272}
273
274static inline bool cec_is_sink(const struct cec_adapter *adap)
275{
276 return adap->phys_addr == 0;
277}
278
Hans Verkuilc8959a32017-11-22 04:12:39 -0500279/**
280 * cec_is_registered() - is the CEC adapter registered?
281 *
282 * @adap: the CEC adapter, may be NULL.
283 *
284 * Return: true if the adapter is registered, false otherwise.
285 */
286static inline bool cec_is_registered(const struct cec_adapter *adap)
287{
288 return adap && adap->devnode.registered;
289}
290
Hans Verkuilee7e98712017-04-17 07:54:37 -0300291#define cec_phys_addr_exp(pa) \
292 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
293
Hans Verkuil23111ec2017-06-07 11:46:08 -0300294struct edid;
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -0400295struct drm_connector;
Hans Verkuil23111ec2017-06-07 11:46:08 -0300296
Hans Verkuilf9f314f2017-06-08 15:37:44 -0300297#if IS_REACHABLE(CONFIG_CEC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300298struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
Hans Verkuilf51e8082016-11-25 06:23:34 -0200299 void *priv, const char *name, u32 caps, u8 available_las);
300int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
Hans Verkuila56960e2016-06-25 09:43:58 -0300301void cec_unregister_adapter(struct cec_adapter *adap);
302void cec_delete_adapter(struct cec_adapter *adap);
303
304int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
305 bool block);
306void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
307 bool block);
Hans Verkuil23111ec2017-06-07 11:46:08 -0300308void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
309 const struct edid *edid);
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -0400310void cec_s_conn_info(struct cec_adapter *adap,
311 const struct cec_connector_info *conn_info);
Hans Verkuila56960e2016-06-25 09:43:58 -0300312int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
313 bool block);
314
315/* Called by the adapter */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300316void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
317 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
318 u8 error_cnt, ktime_t ts);
319
320static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
321 u8 arb_lost_cnt, u8 nack_cnt,
322 u8 low_drive_cnt, u8 error_cnt)
323{
324 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
325 low_drive_cnt, error_cnt, ktime_get());
326}
Hans Verkuilc94cdc12017-06-07 11:46:10 -0300327/*
328 * Simplified version of cec_transmit_done for hardware that doesn't retry
329 * failed transmits. So this is always just one attempt in which case
330 * the status is sufficient.
331 */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300332void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
333 u8 status, ktime_t ts);
334
335static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
336 u8 status)
337{
338 cec_transmit_attempt_done_ts(adap, status, ktime_get());
339}
340
341void cec_received_msg_ts(struct cec_adapter *adap,
342 struct cec_msg *msg, ktime_t ts);
343
344static inline void cec_received_msg(struct cec_adapter *adap,
345 struct cec_msg *msg)
346{
347 cec_received_msg_ts(adap, msg, ktime_get());
348}
Hans Verkuila56960e2016-06-25 09:43:58 -0300349
Hans Verkuilee7e98712017-04-17 07:54:37 -0300350/**
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400351 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300352 *
353 * @adap: pointer to the cec adapter
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400354 * @is_high: when true the CEC pin is high, otherwise it is low
Hans Verkuil6ec1cbf2018-03-06 16:20:00 -0500355 * @dropped_events: when true some events were dropped
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300356 * @ts: the timestamp for this event
357 *
358 */
Hans Verkuil6ec1cbf2018-03-06 16:20:00 -0500359void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
360 bool dropped_events, ktime_t ts);
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300361
362/**
Hans Verkuil333ef6b2017-08-15 10:07:25 -0400363 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
364 *
365 * @adap: pointer to the cec adapter
366 * @is_high: when true the HPD pin is high, otherwise it is low
367 * @ts: the timestamp for this event
368 *
369 */
370void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
371
372/**
Hans Verkuil4786b0d2018-06-28 07:43:46 -0400373 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
374 *
375 * @adap: pointer to the cec adapter
376 * @is_high: when true the 5V pin is high, otherwise it is low
377 * @ts: the timestamp for this event
378 *
379 */
380void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
381
382/**
Hans Verkuilee7e98712017-04-17 07:54:37 -0300383 * cec_get_edid_phys_addr() - find and return the physical address
384 *
385 * @edid: pointer to the EDID data
386 * @size: size in bytes of the EDID data
387 * @offset: If not %NULL then the location of the physical address
388 * bytes in the EDID will be returned here. This is set to 0
389 * if there is no physical address found.
390 *
391 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
392 */
393u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
394 unsigned int *offset);
395
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -0400396void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
397 const struct drm_connector *connector);
398
Hans Verkuila56960e2016-06-25 09:43:58 -0300399#else
400
Hans Verkuilf51e8082016-11-25 06:23:34 -0200401static inline int cec_register_adapter(struct cec_adapter *adap,
402 struct device *parent)
Hans Verkuila56960e2016-06-25 09:43:58 -0300403{
404 return 0;
405}
406
407static inline void cec_unregister_adapter(struct cec_adapter *adap)
408{
409}
410
411static inline void cec_delete_adapter(struct cec_adapter *adap)
412{
413}
414
415static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
416 bool block)
417{
418}
419
Hans Verkuil23111ec2017-06-07 11:46:08 -0300420static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
421 const struct edid *edid)
422{
423}
424
Hans Verkuilee7e98712017-04-17 07:54:37 -0300425static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
426 unsigned int *offset)
427{
428 if (offset)
429 *offset = 0;
430 return CEC_PHYS_ADDR_INVALID;
431}
432
Dariusz Marcinkiewicz32a847f2019-06-20 05:17:18 -0400433static inline void cec_s_conn_info(struct cec_adapter *adap,
434 const struct cec_connector_info *conn_info)
435{
436}
437
438static inline void
439cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
440 const struct drm_connector *connector)
441{
442 memset(conn_info, 0, sizeof(*conn_info));
443}
444
445#endif
446
Hans Verkuil13532782017-06-07 11:46:09 -0300447/**
448 * cec_phys_addr_invalidate() - set the physical address to INVALID
449 *
450 * @adap: the CEC adapter
451 *
452 * This is a simple helper function to invalidate the physical
453 * address.
454 */
455static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
456{
457 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
458}
459
Hans Verkuilb915bf572018-09-13 03:25:59 -0400460/**
461 * cec_get_edid_spa_location() - find location of the Source Physical Address
462 *
463 * @edid: the EDID
464 * @size: the size of the EDID
465 *
466 * This EDID is expected to be a CEA-861 compliant, which means that there are
467 * at least two blocks and one or more of the extensions blocks are CEA-861
468 * blocks.
469 *
470 * The returned location is guaranteed to be <= size-2.
471 *
472 * This is an inline function since it is used by both CEC and V4L2.
473 * Ideally this would go in a module shared by both, but it is overkill to do
474 * that for just a single function.
475 */
476static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
477 unsigned int size)
478{
479 unsigned int blocks = size / 128;
480 unsigned int block;
481 u8 d;
482
483 /* Sanity check: at least 2 blocks and a multiple of the block size */
484 if (blocks < 2 || size % 128)
485 return 0;
486
487 /*
488 * If there are fewer extension blocks than the size, then update
489 * 'blocks'. It is allowed to have more extension blocks than the size,
490 * since some hardware can only read e.g. 256 bytes of the EDID, even
491 * though more blocks are present. The first CEA-861 extension block
492 * should normally be in block 1 anyway.
493 */
494 if (edid[0x7e] + 1 < blocks)
495 blocks = edid[0x7e] + 1;
496
497 for (block = 1; block < blocks; block++) {
498 unsigned int offset = block * 128;
499
500 /* Skip any non-CEA-861 extension blocks */
501 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
502 continue;
503
504 /* search Vendor Specific Data Block (tag 3) */
505 d = edid[offset + 2] & 0x7f;
506 /* Check if there are Data Blocks */
507 if (d <= 4)
508 continue;
509 if (d > 4) {
510 unsigned int i = offset + 4;
511 unsigned int end = offset + d;
512
513 /* Note: 'end' is always < 'size' */
514 do {
515 u8 tag = edid[i] >> 5;
516 u8 len = edid[i] & 0x1f;
517
518 if (tag == 3 && len >= 5 && i + len <= end &&
519 edid[i + 1] == 0x03 &&
520 edid[i + 2] == 0x0c &&
521 edid[i + 3] == 0x00)
522 return i + 4;
523 i += len + 1;
524 } while (i < end);
525 }
526 }
527 return 0;
528}
529
Hans Verkuila56960e2016-06-25 09:43:58 -0300530#endif /* _MEDIA_CEC_H */