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