1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * cec - HDMI Consumer Electronics Control support header
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
11 #include <linux/poll.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 #include <media/cec-notifier.h>
22 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
23 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
26 * struct cec_devnode - cec device node
28 * @cdev: cec character device
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)
35 * This structure represents a cec-related device node.
37 * The @parent is a physical device. It must be set by core or device drivers
38 * before registering the node.
58 struct list_head list
;
59 struct list_head xfer_list
;
60 struct cec_adapter
*adap
;
63 struct delayed_work work
;
71 struct cec_msg_entry
{
72 struct list_head list
;
76 struct cec_event_entry
{
77 struct list_head list
;
81 #define CEC_NUM_CORE_EVENTS 2
82 #define CEC_NUM_EVENTS CEC_EVENT_PIN_HPD_HIGH
85 struct list_head list
;
86 struct list_head xfer_list
;
87 struct cec_adapter
*adap
;
92 wait_queue_head_t wait
;
94 struct list_head events
[CEC_NUM_EVENTS
]; /* queued events */
95 u16 queued_events
[CEC_NUM_EVENTS
];
96 unsigned int total_queued_events
;
97 struct cec_event_entry core_events
[CEC_NUM_CORE_EVENTS
];
98 struct list_head msgs
; /* queued messages */
99 unsigned int queued_msgs
;
102 #define CEC_SIGNAL_FREE_TIME_RETRY 3
103 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
104 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
106 /* The nominal data bit period is 2.4 ms */
107 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
109 struct cec_adap_ops
{
110 /* Low-level callbacks */
111 int (*adap_enable
)(struct cec_adapter
*adap
, bool enable
);
112 int (*adap_monitor_all_enable
)(struct cec_adapter
*adap
, bool enable
);
113 int (*adap_monitor_pin_enable
)(struct cec_adapter
*adap
, bool enable
);
114 int (*adap_log_addr
)(struct cec_adapter
*adap
, u8 logical_addr
);
115 int (*adap_transmit
)(struct cec_adapter
*adap
, u8 attempts
,
116 u32 signal_free_time
, struct cec_msg
*msg
);
117 void (*adap_status
)(struct cec_adapter
*adap
, struct seq_file
*file
);
118 void (*adap_free
)(struct cec_adapter
*adap
);
120 /* Error injection callbacks */
121 int (*error_inj_show
)(struct cec_adapter
*adap
, struct seq_file
*sf
);
122 bool (*error_inj_parse_line
)(struct cec_adapter
*adap
, char *line
);
124 /* High-level CEC message callback */
125 int (*received
)(struct cec_adapter
*adap
, struct cec_msg
*msg
);
129 * The minimum message length you can receive (excepting poll messages) is 2.
130 * With a transfer rate of at most 36 bytes per second this makes 18 messages
131 * per second worst case.
133 * We queue at most 3 seconds worth of received messages. The CEC specification
134 * requires that messages are replied to within a second, so 3 seconds should
135 * give more than enough margin. Since most messages are actually more than 2
136 * bytes, this is in practice a lot more than 3 seconds.
138 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
141 * The transmit queue is limited to 1 second worth of messages (worst case).
142 * Messages can be transmitted by userspace and kernel space. But for both it
143 * makes no sense to have a lot of messages queued up. One second seems
146 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
149 struct module
*owner
;
151 struct cec_devnode devnode
;
155 struct list_head transmit_queue
;
156 unsigned int transmit_queue_sz
;
157 struct list_head wait_queue
;
158 struct cec_data
*transmitting
;
160 struct task_struct
*kthread_config
;
161 struct completion config_completion
;
163 struct task_struct
*kthread
;
164 wait_queue_head_t kthread_waitq
;
165 wait_queue_head_t waitq
;
167 const struct cec_adap_ops
*ops
;
170 u8 available_log_addrs
;
176 bool cec_pin_is_high
;
180 struct cec_fh
*cec_follower
;
181 struct cec_fh
*cec_initiator
;
183 struct cec_log_addrs log_addrs
;
187 #ifdef CONFIG_CEC_NOTIFIER
188 struct cec_notifier
*notifier
;
190 #ifdef CONFIG_CEC_PIN
194 struct dentry
*cec_dir
;
195 struct dentry
*status_file
;
196 struct dentry
*error_inj_file
;
201 char device_name
[32];
206 static inline void *cec_get_drvdata(const struct cec_adapter
*adap
)
211 static inline bool cec_has_log_addr(const struct cec_adapter
*adap
, u8 log_addr
)
213 return adap
->log_addrs
.log_addr_mask
& (1 << log_addr
);
216 static inline bool cec_is_sink(const struct cec_adapter
*adap
)
218 return adap
->phys_addr
== 0;
222 * cec_is_registered() - is the CEC adapter registered?
224 * @adap: the CEC adapter, may be NULL.
226 * Return: true if the adapter is registered, false otherwise.
228 static inline bool cec_is_registered(const struct cec_adapter
*adap
)
230 return adap
&& adap
->devnode
.registered
;
233 #define cec_phys_addr_exp(pa) \
234 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
238 #if IS_REACHABLE(CONFIG_CEC_CORE)
239 struct cec_adapter
*cec_allocate_adapter(const struct cec_adap_ops
*ops
,
240 void *priv
, const char *name
, u32 caps
, u8 available_las
);
241 int cec_register_adapter(struct cec_adapter
*adap
, struct device
*parent
);
242 void cec_unregister_adapter(struct cec_adapter
*adap
);
243 void cec_delete_adapter(struct cec_adapter
*adap
);
245 int cec_s_log_addrs(struct cec_adapter
*adap
, struct cec_log_addrs
*log_addrs
,
247 void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
249 void cec_s_phys_addr_from_edid(struct cec_adapter
*adap
,
250 const struct edid
*edid
);
251 int cec_transmit_msg(struct cec_adapter
*adap
, struct cec_msg
*msg
,
254 /* Called by the adapter */
255 void cec_transmit_done_ts(struct cec_adapter
*adap
, u8 status
,
256 u8 arb_lost_cnt
, u8 nack_cnt
, u8 low_drive_cnt
,
257 u8 error_cnt
, ktime_t ts
);
259 static inline void cec_transmit_done(struct cec_adapter
*adap
, u8 status
,
260 u8 arb_lost_cnt
, u8 nack_cnt
,
261 u8 low_drive_cnt
, u8 error_cnt
)
263 cec_transmit_done_ts(adap
, status
, arb_lost_cnt
, nack_cnt
,
264 low_drive_cnt
, error_cnt
, ktime_get());
267 * Simplified version of cec_transmit_done for hardware that doesn't retry
268 * failed transmits. So this is always just one attempt in which case
269 * the status is sufficient.
271 void cec_transmit_attempt_done_ts(struct cec_adapter
*adap
,
272 u8 status
, ktime_t ts
);
274 static inline void cec_transmit_attempt_done(struct cec_adapter
*adap
,
277 cec_transmit_attempt_done_ts(adap
, status
, ktime_get());
280 void cec_received_msg_ts(struct cec_adapter
*adap
,
281 struct cec_msg
*msg
, ktime_t ts
);
283 static inline void cec_received_msg(struct cec_adapter
*adap
,
286 cec_received_msg_ts(adap
, msg
, ktime_get());
290 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
292 * @adap: pointer to the cec adapter
293 * @is_high: when true the CEC pin is high, otherwise it is low
294 * @dropped_events: when true some events were dropped
295 * @ts: the timestamp for this event
298 void cec_queue_pin_cec_event(struct cec_adapter
*adap
, bool is_high
,
299 bool dropped_events
, ktime_t ts
);
302 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
304 * @adap: pointer to the cec adapter
305 * @is_high: when true the HPD pin is high, otherwise it is low
306 * @ts: the timestamp for this event
309 void cec_queue_pin_hpd_event(struct cec_adapter
*adap
, bool is_high
, ktime_t ts
);
312 * cec_get_edid_phys_addr() - find and return the physical address
314 * @edid: pointer to the EDID data
315 * @size: size in bytes of the EDID data
316 * @offset: If not %NULL then the location of the physical address
317 * bytes in the EDID will be returned here. This is set to 0
318 * if there is no physical address found.
320 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
322 u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
323 unsigned int *offset
);
326 * cec_set_edid_phys_addr() - find and set the physical address
328 * @edid: pointer to the EDID data
329 * @size: size in bytes of the EDID data
330 * @phys_addr: the new physical address
332 * This function finds the location of the physical address in the EDID
333 * and fills in the given physical address and updates the checksum
334 * at the end of the EDID block. It does nothing if the EDID doesn't
335 * contain a physical address.
337 void cec_set_edid_phys_addr(u8
*edid
, unsigned int size
, u16 phys_addr
);
340 * cec_phys_addr_for_input() - calculate the PA for an input
342 * @phys_addr: the physical address of the parent
343 * @input: the number of the input port, must be between 1 and 15
345 * This function calculates a new physical address based on the input
346 * port number. For example:
348 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
350 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
352 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
354 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
356 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
358 u16
cec_phys_addr_for_input(u16 phys_addr
, u8 input
);
361 * cec_phys_addr_validate() - validate a physical address from an EDID
363 * @phys_addr: the physical address to validate
364 * @parent: if not %NULL, then this is filled with the parents PA.
365 * @port: if not %NULL, then this is filled with the input port.
367 * This validates a physical address as read from an EDID. If the
368 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
369 * then it will return -EINVAL.
371 * The parent PA is passed into %parent and the input port is passed into
372 * %port. For example:
374 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
376 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
378 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
380 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
382 * Return: 0 if the PA is valid, -EINVAL if not.
384 int cec_phys_addr_validate(u16 phys_addr
, u16
*parent
, u16
*port
);
388 static inline int cec_register_adapter(struct cec_adapter
*adap
,
389 struct device
*parent
)
394 static inline void cec_unregister_adapter(struct cec_adapter
*adap
)
398 static inline void cec_delete_adapter(struct cec_adapter
*adap
)
402 static inline void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
407 static inline void cec_s_phys_addr_from_edid(struct cec_adapter
*adap
,
408 const struct edid
*edid
)
412 static inline u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
413 unsigned int *offset
)
417 return CEC_PHYS_ADDR_INVALID
;
420 static inline void cec_set_edid_phys_addr(u8
*edid
, unsigned int size
,
425 static inline u16
cec_phys_addr_for_input(u16 phys_addr
, u8 input
)
427 return CEC_PHYS_ADDR_INVALID
;
430 static inline int cec_phys_addr_validate(u16 phys_addr
, u16
*parent
, u16
*port
)
442 * cec_phys_addr_invalidate() - set the physical address to INVALID
444 * @adap: the CEC adapter
446 * This is a simple helper function to invalidate the physical
449 static inline void cec_phys_addr_invalidate(struct cec_adapter
*adap
)
451 cec_s_phys_addr(adap
, CEC_PHYS_ADDR_INVALID
, false);
454 #endif /* _MEDIA_CEC_H */