]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/media/cec.h
Merge tag 'staging-4.20-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[mirror_ubuntu-eoan-kernel.git] / include / media / cec.h
CommitLineData
ab15d248 1/* SPDX-License-Identifier: GPL-2.0-only */
a56960e8
HV
2/*
3 * cec - HDMI Consumer Electronics Control support header
4 *
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
a56960e8
HV
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>
e3a93adc 20#include <media/cec-notifier.h>
a56960e8 21
ee0c503e
HV
22#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
23 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
24
a56960e8
HV
25/**
26 * struct cec_devnode - cec device node
27 * @dev: cec device
28 * @cdev: cec character device
a56960e8
HV
29 * @minor: device node minor number
30 * @registered: the device was correctly registered
31 * @unregistered: the device was unregistered
32 * @fhs_lock: lock to control access to the filehandle list
33 * @fhs: the list of open filehandles (cec_fh)
34 *
35 * This structure represents a cec-related device node.
36 *
37 * The @parent is a physical device. It must be set by core or device drivers
38 * before registering the node.
39 */
40struct cec_devnode {
41 /* sysfs */
42 struct device dev;
43 struct cdev cdev;
a56960e8
HV
44
45 /* device info */
46 int minor;
47 bool registered;
48 bool unregistered;
a56960e8 49 struct list_head fhs;
62148f09 50 struct mutex lock;
a56960e8
HV
51};
52
53struct cec_adapter;
54struct cec_data;
ea5c8ef2 55struct cec_pin;
a56960e8
HV
56
57struct 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;
a56960e8
HV
66 bool blocking;
67 bool completed;
68};
69
70struct cec_msg_entry {
71 struct list_head list;
72 struct cec_msg msg;
73};
74
6b2bbb08
HV
75struct cec_event_entry {
76 struct list_head list;
77 struct cec_event ev;
78};
79
80#define CEC_NUM_CORE_EVENTS 2
4786b0d6 81#define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
a56960e8
HV
82
83struct 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;
a56960e8 92 struct mutex lock;
6b2bbb08 93 struct list_head events[CEC_NUM_EVENTS]; /* queued events */
6ec1cbf6 94 u16 queued_events[CEC_NUM_EVENTS];
6b2bbb08
HV
95 unsigned int total_queued_events;
96 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
a56960e8
HV
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
108struct 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);
48ea2e92 112 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
a56960e8
HV
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);
e6259b5f 117 void (*adap_free)(struct cec_adapter *adap);
a56960e8 118
9ca400c1
HV
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
a56960e8
HV
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 *
11065f85
HV
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.
a56960e8 136 */
11065f85
HV
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)
a56960e8
HV
146
147struct 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;
11065f85 155 unsigned int transmit_queue_sz;
a56960e8
HV
156 struct list_head wait_queue;
157 struct cec_data *transmitting;
158
159 struct task_struct *kthread_config;
160 struct completion config_completion;
161
162 struct task_struct *kthread;
163 wait_queue_head_t kthread_waitq;
164 wait_queue_head_t waitq;
165
166 const struct cec_adap_ops *ops;
167 void *priv;
168 u32 capabilities;
169 u8 available_log_addrs;
170
171 u16 phys_addr;
f902c1e9 172 bool needs_hpd;
a56960e8
HV
173 bool is_configuring;
174 bool is_configured;
28e11b15 175 bool cec_pin_is_high;
7d867a1b 176 u8 last_initiator;
a56960e8 177 u32 monitor_all_cnt;
b8d62f50 178 u32 monitor_pin_cnt;
a56960e8
HV
179 u32 follower_cnt;
180 struct cec_fh *cec_follower;
181 struct cec_fh *cec_initiator;
182 bool passthrough;
183 struct cec_log_addrs log_addrs;
184
bb789e03
HV
185 u32 tx_timeouts;
186
e94c3281 187#ifdef CONFIG_CEC_NOTIFIER
e3a93adc
HV
188 struct cec_notifier *notifier;
189#endif
ea5c8ef2
HV
190#ifdef CONFIG_CEC_PIN
191 struct cec_pin *pin;
192#endif
e3a93adc 193
a56960e8
HV
194 struct dentry *cec_dir;
195 struct dentry *status_file;
9ca400c1 196 struct dentry *error_inj_file;
a56960e8
HV
197
198 u16 phys_addrs[15];
199 u32 sequence;
200
a56960e8 201 char input_phys[32];
a56960e8
HV
202};
203
0b0b97dc
JA
204static inline void *cec_get_drvdata(const struct cec_adapter *adap)
205{
206 return adap->priv;
207}
208
a56960e8
HV
209static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
210{
211 return adap->log_addrs.log_addr_mask & (1 << log_addr);
212}
213
214static inline bool cec_is_sink(const struct cec_adapter *adap)
215{
216 return adap->phys_addr == 0;
217}
218
c8959a39
HV
219/**
220 * cec_is_registered() - is the CEC adapter registered?
221 *
222 * @adap: the CEC adapter, may be NULL.
223 *
224 * Return: true if the adapter is registered, false otherwise.
225 */
226static inline bool cec_is_registered(const struct cec_adapter *adap)
227{
228 return adap && adap->devnode.registered;
229}
230
ee7e9871
HV
231#define cec_phys_addr_exp(pa) \
232 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
233
23111ec3
HV
234struct edid;
235
f9f314f3 236#if IS_REACHABLE(CONFIG_CEC_CORE)
a56960e8 237struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
f51e8080
HV
238 void *priv, const char *name, u32 caps, u8 available_las);
239int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
a56960e8
HV
240void cec_unregister_adapter(struct cec_adapter *adap);
241void cec_delete_adapter(struct cec_adapter *adap);
242
243int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
244 bool block);
245void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
246 bool block);
23111ec3
HV
247void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
248 const struct edid *edid);
a56960e8
HV
249int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
250 bool block);
251
252/* Called by the adapter */
0861ad14
HV
253void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
254 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
255 u8 error_cnt, ktime_t ts);
256
257static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
258 u8 arb_lost_cnt, u8 nack_cnt,
259 u8 low_drive_cnt, u8 error_cnt)
260{
261 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
262 low_drive_cnt, error_cnt, ktime_get());
263}
c94cdc1e
HV
264/*
265 * Simplified version of cec_transmit_done for hardware that doesn't retry
266 * failed transmits. So this is always just one attempt in which case
267 * the status is sufficient.
268 */
0861ad14
HV
269void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
270 u8 status, ktime_t ts);
271
272static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
273 u8 status)
274{
275 cec_transmit_attempt_done_ts(adap, status, ktime_get());
276}
277
278void cec_received_msg_ts(struct cec_adapter *adap,
279 struct cec_msg *msg, ktime_t ts);
280
281static inline void cec_received_msg(struct cec_adapter *adap,
282 struct cec_msg *msg)
283{
284 cec_received_msg_ts(adap, msg, ktime_get());
285}
a56960e8 286
b8d62f50 287/**
9a6b2a87 288 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
b8d62f50
HV
289 *
290 * @adap: pointer to the cec adapter
9a6b2a87 291 * @is_high: when true the CEC pin is high, otherwise it is low
6ec1cbf6 292 * @dropped_events: when true some events were dropped
b8d62f50
HV
293 * @ts: the timestamp for this event
294 *
295 */
6ec1cbf6
HV
296void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
297 bool dropped_events, ktime_t ts);
b8d62f50 298
333ef6bd
HV
299/**
300 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
301 *
302 * @adap: pointer to the cec adapter
303 * @is_high: when true the HPD pin is high, otherwise it is low
304 * @ts: the timestamp for this event
305 *
306 */
307void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
308
4786b0d6
HV
309/**
310 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
311 *
312 * @adap: pointer to the cec adapter
313 * @is_high: when true the 5V pin is high, otherwise it is low
314 * @ts: the timestamp for this event
315 *
316 */
317void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
318
ee7e9871
HV
319/**
320 * cec_get_edid_phys_addr() - find and return the physical address
321 *
322 * @edid: pointer to the EDID data
323 * @size: size in bytes of the EDID data
324 * @offset: If not %NULL then the location of the physical address
325 * bytes in the EDID will be returned here. This is set to 0
326 * if there is no physical address found.
327 *
328 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
329 */
330u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
331 unsigned int *offset);
332
a56960e8
HV
333#else
334
f51e8080
HV
335static inline int cec_register_adapter(struct cec_adapter *adap,
336 struct device *parent)
a56960e8
HV
337{
338 return 0;
339}
340
341static inline void cec_unregister_adapter(struct cec_adapter *adap)
342{
343}
344
345static inline void cec_delete_adapter(struct cec_adapter *adap)
346{
347}
348
349static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
350 bool block)
351{
352}
353
23111ec3
HV
354static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
355 const struct edid *edid)
356{
357}
358
ee7e9871
HV
359static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
360 unsigned int *offset)
361{
362 if (offset)
363 *offset = 0;
364 return CEC_PHYS_ADDR_INVALID;
365}
366
a56960e8
HV
367#endif
368
13532789
HV
369/**
370 * cec_phys_addr_invalidate() - set the physical address to INVALID
371 *
372 * @adap: the CEC adapter
373 *
374 * This is a simple helper function to invalidate the physical
375 * address.
376 */
377static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
378{
379 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
380}
381
b915bf57
HV
382/**
383 * cec_get_edid_spa_location() - find location of the Source Physical Address
384 *
385 * @edid: the EDID
386 * @size: the size of the EDID
387 *
388 * This EDID is expected to be a CEA-861 compliant, which means that there are
389 * at least two blocks and one or more of the extensions blocks are CEA-861
390 * blocks.
391 *
392 * The returned location is guaranteed to be <= size-2.
393 *
394 * This is an inline function since it is used by both CEC and V4L2.
395 * Ideally this would go in a module shared by both, but it is overkill to do
396 * that for just a single function.
397 */
398static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
399 unsigned int size)
400{
401 unsigned int blocks = size / 128;
402 unsigned int block;
403 u8 d;
404
405 /* Sanity check: at least 2 blocks and a multiple of the block size */
406 if (blocks < 2 || size % 128)
407 return 0;
408
409 /*
410 * If there are fewer extension blocks than the size, then update
411 * 'blocks'. It is allowed to have more extension blocks than the size,
412 * since some hardware can only read e.g. 256 bytes of the EDID, even
413 * though more blocks are present. The first CEA-861 extension block
414 * should normally be in block 1 anyway.
415 */
416 if (edid[0x7e] + 1 < blocks)
417 blocks = edid[0x7e] + 1;
418
419 for (block = 1; block < blocks; block++) {
420 unsigned int offset = block * 128;
421
422 /* Skip any non-CEA-861 extension blocks */
423 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
424 continue;
425
426 /* search Vendor Specific Data Block (tag 3) */
427 d = edid[offset + 2] & 0x7f;
428 /* Check if there are Data Blocks */
429 if (d <= 4)
430 continue;
431 if (d > 4) {
432 unsigned int i = offset + 4;
433 unsigned int end = offset + d;
434
435 /* Note: 'end' is always < 'size' */
436 do {
437 u8 tag = edid[i] >> 5;
438 u8 len = edid[i] & 0x1f;
439
440 if (tag == 3 && len >= 5 && i + len <= end &&
441 edid[i + 1] == 0x03 &&
442 edid[i + 2] == 0x0c &&
443 edid[i + 3] == 0x00)
444 return i + 4;
445 i += len + 1;
446 } while (i < end);
447 }
448 }
449 return 0;
450}
451
a56960e8 452#endif /* _MEDIA_CEC_H */