blob: dc4b412e8fa1e2feb6676eb4eec9394241a2035c [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>
Hans Verkuile3a93ad2016-12-13 12:15:57 -020020#include <media/cec-notifier.h>
Hans Verkuila56960e2016-06-25 09:43:58 -030021
Hans Verkuilee0c5032017-08-04 06:41:51 -040022#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
23 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
24
Hans Verkuila56960e2016-06-25 09:43:58 -030025/**
26 * struct cec_devnode - cec device node
27 * @dev: cec device
28 * @cdev: cec character device
Hans Verkuila56960e2016-06-25 09:43:58 -030029 * @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;
Hans Verkuila56960e2016-06-25 09:43:58 -030044
45 /* device info */
46 int minor;
47 bool registered;
48 bool unregistered;
Hans Verkuila56960e2016-06-25 09:43:58 -030049 struct list_head fhs;
Hans Verkuil62148f02016-08-02 08:11:00 -030050 struct mutex lock;
Hans Verkuila56960e2016-06-25 09:43:58 -030051};
52
53struct cec_adapter;
54struct cec_data;
Hans Verkuilea5c8ef2017-07-11 03:30:42 -030055struct cec_pin;
Hans Verkuila56960e2016-06-25 09:43:58 -030056
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;
Hans Verkuila56960e2016-06-25 09:43:58 -030066 bool blocking;
67 bool completed;
68};
69
70struct cec_msg_entry {
71 struct list_head list;
72 struct cec_msg msg;
73};
74
Hans Verkuil6b2bbb02017-07-11 03:30:39 -030075struct cec_event_entry {
76 struct list_head list;
77 struct cec_event ev;
78};
79
80#define CEC_NUM_CORE_EVENTS 2
Hans Verkuil4786b0d2018-06-28 07:43:46 -040081#define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
Hans Verkuila56960e2016-06-25 09:43:58 -030082
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;
Hans Verkuila56960e2016-06-25 09:43:58 -030092 struct mutex lock;
Hans Verkuil6b2bbb02017-07-11 03:30:39 -030093 struct list_head events[CEC_NUM_EVENTS]; /* queued events */
Hans Verkuil6ec1cbf2018-03-06 16:20:00 -050094 u16 queued_events[CEC_NUM_EVENTS];
Hans Verkuil6b2bbb02017-07-11 03:30:39 -030095 unsigned int total_queued_events;
96 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
Hans Verkuila56960e2016-06-25 09:43:58 -030097 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);
Hans Verkuil48ea2e92017-11-05 07:36:36 -0500112 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
Hans Verkuila56960e2016-06-25 09:43:58 -0300113 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);
Hans Verkuile6259b52017-07-11 03:30:36 -0300117 void (*adap_free)(struct cec_adapter *adap);
Hans Verkuila56960e2016-06-25 09:43:58 -0300118
Hans Verkuil9ca400c2017-10-31 09:55:09 -0400119 /* 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
Hans Verkuila56960e2016-06-25 09:43:58 -0300123 /* 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 *
Hans Verkuil11065f82016-07-17 11:40:05 -0300132 * 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.
Hans Verkuila56960e2016-06-25 09:43:58 -0300136 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300137#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)
Hans Verkuila56960e2016-06-25 09:43:58 -0300146
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;
Hans Verkuil11065f82016-07-17 11:40:05 -0300155 unsigned int transmit_queue_sz;
Hans Verkuila56960e2016-06-25 09:43:58 -0300156 struct list_head wait_queue;
157 struct cec_data *transmitting;
Hans Verkuil46515302018-10-19 03:55:34 -0400158 bool transmit_in_progress;
Hans Verkuila56960e2016-06-25 09:43:58 -0300159
160 struct task_struct *kthread_config;
161 struct completion config_completion;
162
163 struct task_struct *kthread;
164 wait_queue_head_t kthread_waitq;
165 wait_queue_head_t waitq;
166
167 const struct cec_adap_ops *ops;
168 void *priv;
169 u32 capabilities;
170 u8 available_log_addrs;
171
172 u16 phys_addr;
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300173 bool needs_hpd;
Hans Verkuila56960e2016-06-25 09:43:58 -0300174 bool is_configuring;
175 bool is_configured;
Hans Verkuil28e11b12017-08-20 06:53:10 -0400176 bool cec_pin_is_high;
Hans Verkuilb68d4052018-10-05 08:00:21 -0400177 u8 last_initiator;
Hans Verkuila56960e2016-06-25 09:43:58 -0300178 u32 monitor_all_cnt;
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300179 u32 monitor_pin_cnt;
Hans Verkuila56960e2016-06-25 09:43:58 -0300180 u32 follower_cnt;
181 struct cec_fh *cec_follower;
182 struct cec_fh *cec_initiator;
183 bool passthrough;
184 struct cec_log_addrs log_addrs;
185
Hans Verkuilbb789e02017-07-11 03:30:34 -0300186 u32 tx_timeouts;
187
Hans Verkuile94c3282017-05-28 05:58:04 -0300188#ifdef CONFIG_CEC_NOTIFIER
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200189 struct cec_notifier *notifier;
190#endif
Hans Verkuilea5c8ef2017-07-11 03:30:42 -0300191#ifdef CONFIG_CEC_PIN
192 struct cec_pin *pin;
193#endif
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200194
Hans Verkuila56960e2016-06-25 09:43:58 -0300195 struct dentry *cec_dir;
196 struct dentry *status_file;
Hans Verkuil9ca400c2017-10-31 09:55:09 -0400197 struct dentry *error_inj_file;
Hans Verkuila56960e2016-06-25 09:43:58 -0300198
199 u16 phys_addrs[15];
200 u32 sequence;
201
Sean Young518f4b22017-07-01 12:13:19 -0400202 char device_name[32];
Hans Verkuila56960e2016-06-25 09:43:58 -0300203 char input_phys[32];
204 char input_drv[32];
205};
206
Jose Abreu0b0b97d2017-03-24 13:47:52 -0300207static inline void *cec_get_drvdata(const struct cec_adapter *adap)
208{
209 return adap->priv;
210}
211
Hans Verkuila56960e2016-06-25 09:43:58 -0300212static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
213{
214 return adap->log_addrs.log_addr_mask & (1 << log_addr);
215}
216
217static inline bool cec_is_sink(const struct cec_adapter *adap)
218{
219 return adap->phys_addr == 0;
220}
221
Hans Verkuilc8959a32017-11-22 04:12:39 -0500222/**
223 * cec_is_registered() - is the CEC adapter registered?
224 *
225 * @adap: the CEC adapter, may be NULL.
226 *
227 * Return: true if the adapter is registered, false otherwise.
228 */
229static inline bool cec_is_registered(const struct cec_adapter *adap)
230{
231 return adap && adap->devnode.registered;
232}
233
Hans Verkuilee7e98712017-04-17 07:54:37 -0300234#define cec_phys_addr_exp(pa) \
235 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
236
Hans Verkuil23111ec2017-06-07 11:46:08 -0300237struct edid;
238
Hans Verkuilf9f314f2017-06-08 15:37:44 -0300239#if IS_REACHABLE(CONFIG_CEC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300240struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
Hans Verkuilf51e8082016-11-25 06:23:34 -0200241 void *priv, const char *name, u32 caps, u8 available_las);
242int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
Hans Verkuila56960e2016-06-25 09:43:58 -0300243void cec_unregister_adapter(struct cec_adapter *adap);
244void cec_delete_adapter(struct cec_adapter *adap);
245
246int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
247 bool block);
248void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
249 bool block);
Hans Verkuil23111ec2017-06-07 11:46:08 -0300250void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
251 const struct edid *edid);
Hans Verkuila56960e2016-06-25 09:43:58 -0300252int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
253 bool block);
254
255/* Called by the adapter */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300256void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
257 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
258 u8 error_cnt, ktime_t ts);
259
260static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
261 u8 arb_lost_cnt, u8 nack_cnt,
262 u8 low_drive_cnt, u8 error_cnt)
263{
264 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
265 low_drive_cnt, error_cnt, ktime_get());
266}
Hans Verkuilc94cdc12017-06-07 11:46:10 -0300267/*
268 * Simplified version of cec_transmit_done for hardware that doesn't retry
269 * failed transmits. So this is always just one attempt in which case
270 * the status is sufficient.
271 */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300272void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
273 u8 status, ktime_t ts);
274
275static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
276 u8 status)
277{
278 cec_transmit_attempt_done_ts(adap, status, ktime_get());
279}
280
281void cec_received_msg_ts(struct cec_adapter *adap,
282 struct cec_msg *msg, ktime_t ts);
283
284static inline void cec_received_msg(struct cec_adapter *adap,
285 struct cec_msg *msg)
286{
287 cec_received_msg_ts(adap, msg, ktime_get());
288}
Hans Verkuila56960e2016-06-25 09:43:58 -0300289
Hans Verkuilee7e98712017-04-17 07:54:37 -0300290/**
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400291 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300292 *
293 * @adap: pointer to the cec adapter
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400294 * @is_high: when true the CEC pin is high, otherwise it is low
Hans Verkuil6ec1cbf2018-03-06 16:20:00 -0500295 * @dropped_events: when true some events were dropped
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300296 * @ts: the timestamp for this event
297 *
298 */
Hans Verkuil6ec1cbf2018-03-06 16:20:00 -0500299void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
300 bool dropped_events, ktime_t ts);
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300301
302/**
Hans Verkuil333ef6b2017-08-15 10:07:25 -0400303 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
304 *
305 * @adap: pointer to the cec adapter
306 * @is_high: when true the HPD pin is high, otherwise it is low
307 * @ts: the timestamp for this event
308 *
309 */
310void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
311
312/**
Hans Verkuil4786b0d2018-06-28 07:43:46 -0400313 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
314 *
315 * @adap: pointer to the cec adapter
316 * @is_high: when true the 5V pin is high, otherwise it is low
317 * @ts: the timestamp for this event
318 *
319 */
320void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
321
322/**
Hans Verkuilee7e98712017-04-17 07:54:37 -0300323 * cec_get_edid_phys_addr() - find and return the physical address
324 *
325 * @edid: pointer to the EDID data
326 * @size: size in bytes of the EDID data
327 * @offset: If not %NULL then the location of the physical address
328 * bytes in the EDID will be returned here. This is set to 0
329 * if there is no physical address found.
330 *
331 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
332 */
333u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
334 unsigned int *offset);
335
336/**
337 * cec_set_edid_phys_addr() - find and set the physical address
338 *
339 * @edid: pointer to the EDID data
340 * @size: size in bytes of the EDID data
341 * @phys_addr: the new physical address
342 *
343 * This function finds the location of the physical address in the EDID
344 * and fills in the given physical address and updates the checksum
345 * at the end of the EDID block. It does nothing if the EDID doesn't
346 * contain a physical address.
347 */
348void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
349
350/**
351 * cec_phys_addr_for_input() - calculate the PA for an input
352 *
353 * @phys_addr: the physical address of the parent
354 * @input: the number of the input port, must be between 1 and 15
355 *
356 * This function calculates a new physical address based on the input
357 * port number. For example:
358 *
359 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
360 *
361 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
362 *
363 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
364 *
365 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
366 *
367 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
368 */
369u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
370
371/**
372 * cec_phys_addr_validate() - validate a physical address from an EDID
373 *
374 * @phys_addr: the physical address to validate
375 * @parent: if not %NULL, then this is filled with the parents PA.
376 * @port: if not %NULL, then this is filled with the input port.
377 *
378 * This validates a physical address as read from an EDID. If the
379 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
380 * then it will return -EINVAL.
381 *
382 * The parent PA is passed into %parent and the input port is passed into
383 * %port. For example:
384 *
385 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
386 *
387 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
388 *
389 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
390 *
391 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
392 *
393 * Return: 0 if the PA is valid, -EINVAL if not.
394 */
395int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
396
Hans Verkuila56960e2016-06-25 09:43:58 -0300397#else
398
Hans Verkuilf51e8082016-11-25 06:23:34 -0200399static inline int cec_register_adapter(struct cec_adapter *adap,
400 struct device *parent)
Hans Verkuila56960e2016-06-25 09:43:58 -0300401{
402 return 0;
403}
404
405static inline void cec_unregister_adapter(struct cec_adapter *adap)
406{
407}
408
409static inline void cec_delete_adapter(struct cec_adapter *adap)
410{
411}
412
413static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
414 bool block)
415{
416}
417
Hans Verkuil23111ec2017-06-07 11:46:08 -0300418static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
419 const struct edid *edid)
420{
421}
422
Hans Verkuilee7e98712017-04-17 07:54:37 -0300423static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
424 unsigned int *offset)
425{
426 if (offset)
427 *offset = 0;
428 return CEC_PHYS_ADDR_INVALID;
429}
430
431static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
432 u16 phys_addr)
433{
434}
435
436static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
437{
438 return CEC_PHYS_ADDR_INVALID;
439}
440
441static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
442{
Hans Verkuil9267d902017-08-31 04:12:53 -0400443 if (parent)
444 *parent = phys_addr;
445 if (port)
446 *port = 0;
Hans Verkuilee7e98712017-04-17 07:54:37 -0300447 return 0;
448}
449
Hans Verkuila56960e2016-06-25 09:43:58 -0300450#endif
451
Hans Verkuil13532782017-06-07 11:46:09 -0300452/**
453 * cec_phys_addr_invalidate() - set the physical address to INVALID
454 *
455 * @adap: the CEC adapter
456 *
457 * This is a simple helper function to invalidate the physical
458 * address.
459 */
460static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
461{
462 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
463}
464
Hans Verkuilc1a41022018-09-13 03:25:59 -0400465/**
466 * cec_get_edid_spa_location() - find location of the Source Physical Address
467 *
468 * @edid: the EDID
469 * @size: the size of the EDID
470 *
471 * This EDID is expected to be a CEA-861 compliant, which means that there are
472 * at least two blocks and one or more of the extensions blocks are CEA-861
473 * blocks.
474 *
475 * The returned location is guaranteed to be <= size-2.
476 *
477 * This is an inline function since it is used by both CEC and V4L2.
478 * Ideally this would go in a module shared by both, but it is overkill to do
479 * that for just a single function.
480 */
481static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
482 unsigned int size)
483{
484 unsigned int blocks = size / 128;
485 unsigned int block;
486 u8 d;
487
488 /* Sanity check: at least 2 blocks and a multiple of the block size */
489 if (blocks < 2 || size % 128)
490 return 0;
491
492 /*
493 * If there are fewer extension blocks than the size, then update
494 * 'blocks'. It is allowed to have more extension blocks than the size,
495 * since some hardware can only read e.g. 256 bytes of the EDID, even
496 * though more blocks are present. The first CEA-861 extension block
497 * should normally be in block 1 anyway.
498 */
499 if (edid[0x7e] + 1 < blocks)
500 blocks = edid[0x7e] + 1;
501
502 for (block = 1; block < blocks; block++) {
503 unsigned int offset = block * 128;
504
505 /* Skip any non-CEA-861 extension blocks */
506 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
507 continue;
508
509 /* search Vendor Specific Data Block (tag 3) */
510 d = edid[offset + 2] & 0x7f;
511 /* Check if there are Data Blocks */
512 if (d <= 4)
513 continue;
514 if (d > 4) {
515 unsigned int i = offset + 4;
516 unsigned int end = offset + d;
517
518 /* Note: 'end' is always < 'size' */
519 do {
520 u8 tag = edid[i] >> 5;
521 u8 len = edid[i] & 0x1f;
522
523 if (tag == 3 && len >= 5 && i + len <= end &&
524 edid[i + 1] == 0x03 &&
525 edid[i + 2] == 0x0c &&
526 edid[i + 3] == 0x00)
527 return i + 4;
528 i += len + 1;
529 } while (i < end);
530 }
531 }
532 return 0;
533}
534
Hans Verkuila56960e2016-06-25 09:43:58 -0300535#endif /* _MEDIA_CEC_H */