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