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>
21 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
22 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
25 * struct cec_devnode - cec device node
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)
35 * This structure represents a cec-related device node.
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.
40 * The @parent is a physical device. It must be set by core or device drivers
41 * before registering the node.
50 /* serialize open/release and registration */
54 /* protect access to fhs */
55 struct mutex lock_fhs
;
65 struct list_head list
;
66 struct list_head xfer_list
;
67 struct cec_adapter
*adap
;
72 struct delayed_work work
;
79 struct cec_msg_entry
{
80 struct list_head list
;
84 struct cec_event_entry
{
85 struct list_head list
;
89 #define CEC_NUM_CORE_EVENTS 2
90 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
93 struct list_head list
;
94 struct list_head xfer_list
;
95 struct cec_adapter
*adap
;
100 wait_queue_head_t wait
;
102 struct list_head events
[CEC_NUM_EVENTS
]; /* queued events */
103 u16 queued_events
[CEC_NUM_EVENTS
];
104 unsigned int total_queued_events
;
105 struct cec_event_entry core_events
[CEC_NUM_CORE_EVENTS
];
106 struct list_head msgs
; /* queued messages */
107 unsigned int queued_msgs
;
110 #define CEC_SIGNAL_FREE_TIME_RETRY 3
111 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
112 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
114 /* The nominal data bit period is 2.4 ms */
115 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
117 struct cec_adap_ops
{
118 /* Low-level callbacks, called with adap->lock held */
119 int (*adap_enable
)(struct cec_adapter
*adap
, bool enable
);
120 int (*adap_monitor_all_enable
)(struct cec_adapter
*adap
, bool enable
);
121 int (*adap_monitor_pin_enable
)(struct cec_adapter
*adap
, bool enable
);
122 int (*adap_log_addr
)(struct cec_adapter
*adap
, u8 logical_addr
);
123 void (*adap_unconfigured
)(struct cec_adapter
*adap
);
124 int (*adap_transmit
)(struct cec_adapter
*adap
, u8 attempts
,
125 u32 signal_free_time
, struct cec_msg
*msg
);
126 void (*adap_nb_transmit_canceled
)(struct cec_adapter
*adap
,
127 const struct cec_msg
*msg
);
128 void (*adap_status
)(struct cec_adapter
*adap
, struct seq_file
*file
);
129 void (*adap_free
)(struct cec_adapter
*adap
);
131 /* Error injection callbacks, called without adap->lock held */
132 int (*error_inj_show
)(struct cec_adapter
*adap
, struct seq_file
*sf
);
133 bool (*error_inj_parse_line
)(struct cec_adapter
*adap
, char *line
);
135 /* High-level CEC message callback, called without adap->lock held */
136 void (*configured
)(struct cec_adapter
*adap
);
137 int (*received
)(struct cec_adapter
*adap
, struct cec_msg
*msg
);
141 * The minimum message length you can receive (excepting poll messages) is 2.
142 * With a transfer rate of at most 36 bytes per second this makes 18 messages
143 * per second worst case.
145 * We queue at most 3 seconds worth of received messages. The CEC specification
146 * requires that messages are replied to within a second, so 3 seconds should
147 * give more than enough margin. Since most messages are actually more than 2
148 * bytes, this is in practice a lot more than 3 seconds.
150 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
153 * The transmit queue is limited to 1 second worth of messages (worst case).
154 * Messages can be transmitted by userspace and kernel space. But for both it
155 * makes no sense to have a lot of messages queued up. One second seems
158 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
161 * struct cec_adapter - cec adapter structure
162 * @owner: module owner
163 * @name: name of the CEC adapter
164 * @devnode: device node for the /dev/cecX device
165 * @lock: mutex controlling access to this structure
166 * @rc: remote control device
167 * @transmit_queue: queue of pending transmits
168 * @transmit_queue_sz: number of pending transmits
169 * @wait_queue: queue of transmits waiting for a reply
170 * @transmitting: CEC messages currently being transmitted
171 * @transmit_in_progress: true if a transmit is in progress
172 * @transmit_in_progress_aborted: true if a transmit is in progress is to be
173 * aborted. This happens if the logical address is
174 * invalidated while the transmit is ongoing. In that
175 * case the transmit will finish, but will not retransmit
176 * and be marked as ABORTED.
177 * @xfer_timeout_ms: the transfer timeout in ms.
178 * If 0, then timeout after 2100 ms.
179 * @kthread_config: kthread used to configure a CEC adapter
180 * @config_completion: used to signal completion of the config kthread
181 * @kthread: main CEC processing thread
182 * @kthread_waitq: main CEC processing wait_queue
183 * @ops: cec adapter ops
184 * @priv: cec driver's private data
185 * @capabilities: cec adapter capabilities
186 * @available_log_addrs: maximum number of available logical addresses
187 * @phys_addr: the current physical address
188 * @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high
189 * in order to transmit or receive CEC messages. This is usually a HW
191 * @is_enabled: the CEC adapter is enabled
192 * @is_claiming_log_addrs: true if cec_claim_log_addrs() is running
193 * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
194 * @must_reconfigure: while configuring, the PA changed, so reclaim LAs
195 * @is_configured: the CEC adapter is configured (i.e. has claimed LAs)
196 * @cec_pin_is_high: if true then the CEC pin is high. Only used with the
198 * @adap_controls_phys_addr: if true, then the CEC adapter controls the
199 * physical address, i.e. the CEC hardware can detect HPD changes and
200 * read the EDID and is not dependent on an external HDMI driver.
201 * Drivers that need this can set this field to true after the
202 * cec_allocate_adapter() call.
203 * @last_initiator: the initiator of the last transmitted message.
204 * @monitor_all_cnt: number of filehandles monitoring all msgs
205 * @monitor_pin_cnt: number of filehandles monitoring pin changes
206 * @follower_cnt: number of filehandles in follower mode
207 * @cec_follower: filehandle of the exclusive follower
208 * @cec_initiator: filehandle of the exclusive initiator
209 * @passthrough: if true, then the exclusive follower is in
211 * @log_addrs: current logical addresses
212 * @conn_info: current connector info
213 * @tx_timeout_cnt: count the number of Timed Out transmits.
214 * Reset to 0 when this is reported in cec_adap_status().
215 * @tx_low_drive_cnt: count the number of Low Drive transmits.
216 * Reset to 0 when this is reported in cec_adap_status().
217 * @tx_error_cnt: count the number of Error transmits.
218 * Reset to 0 when this is reported in cec_adap_status().
219 * @tx_arb_lost_cnt: count the number of Arb Lost transmits.
220 * Reset to 0 when this is reported in cec_adap_status().
221 * @tx_low_drive_log_cnt: number of logged Low Drive transmits since the
222 * adapter was enabled. Used to avoid flooding the kernel
223 * log if this happens a lot.
224 * @tx_error_log_cnt: number of logged Error transmits since the adapter was
225 * enabled. Used to avoid flooding the kernel log if this
227 * @notifier: CEC notifier
228 * @pin: CEC pin status struct
229 * @cec_dir: debugfs cec directory
230 * @sequence: transmit sequence counter
231 * @input_phys: remote control input_phys name
233 * This structure represents a cec adapter.
236 struct module
*owner
;
238 struct cec_devnode devnode
;
242 struct list_head transmit_queue
;
243 unsigned int transmit_queue_sz
;
244 struct list_head wait_queue
;
245 struct cec_data
*transmitting
;
246 bool transmit_in_progress
;
247 bool transmit_in_progress_aborted
;
248 unsigned int xfer_timeout_ms
;
250 struct task_struct
*kthread_config
;
251 struct completion config_completion
;
253 struct task_struct
*kthread
;
254 wait_queue_head_t kthread_waitq
;
256 const struct cec_adap_ops
*ops
;
259 u8 available_log_addrs
;
264 bool is_claiming_log_addrs
;
266 bool must_reconfigure
;
268 bool cec_pin_is_high
;
269 bool adap_controls_phys_addr
;
274 struct cec_fh
*cec_follower
;
275 struct cec_fh
*cec_initiator
;
277 struct cec_log_addrs log_addrs
;
278 struct cec_connector_info conn_info
;
281 u32 tx_low_drive_cnt
;
284 u32 tx_low_drive_log_cnt
;
285 u32 tx_error_log_cnt
;
287 #ifdef CONFIG_CEC_NOTIFIER
288 struct cec_notifier
*notifier
;
290 #ifdef CONFIG_CEC_PIN
294 struct dentry
*cec_dir
;
301 static inline int cec_get_device(struct cec_adapter
*adap
)
303 struct cec_devnode
*devnode
= &adap
->devnode
;
306 * Check if the cec device is available. This needs to be done with
307 * the devnode->lock held to prevent an open/unregister race:
308 * without the lock, the device could be unregistered and freed between
309 * the devnode->registered check and get_device() calls, leading to
312 mutex_lock(&devnode
->lock
);
314 * return ENODEV if the cec device has been removed
315 * already or if it is not registered anymore.
317 if (!devnode
->registered
) {
318 mutex_unlock(&devnode
->lock
);
321 /* and increase the device refcount */
322 get_device(&devnode
->dev
);
323 mutex_unlock(&devnode
->lock
);
327 static inline void cec_put_device(struct cec_adapter
*adap
)
329 put_device(&adap
->devnode
.dev
);
332 static inline void *cec_get_drvdata(const struct cec_adapter
*adap
)
337 static inline bool cec_has_log_addr(const struct cec_adapter
*adap
, u8 log_addr
)
339 return adap
->log_addrs
.log_addr_mask
& (1 << log_addr
);
342 static inline bool cec_is_sink(const struct cec_adapter
*adap
)
344 return adap
->phys_addr
== 0;
348 * cec_is_registered() - is the CEC adapter registered?
350 * @adap: the CEC adapter, may be NULL.
352 * Return: true if the adapter is registered, false otherwise.
354 static inline bool cec_is_registered(const struct cec_adapter
*adap
)
356 return adap
&& adap
->devnode
.registered
;
359 #define cec_phys_addr_exp(pa) \
360 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
363 struct drm_connector
;
365 #if IS_REACHABLE(CONFIG_CEC_CORE)
366 struct cec_adapter
*cec_allocate_adapter(const struct cec_adap_ops
*ops
,
367 void *priv
, const char *name
, u32 caps
, u8 available_las
);
368 int cec_register_adapter(struct cec_adapter
*adap
, struct device
*parent
);
369 void cec_unregister_adapter(struct cec_adapter
*adap
);
370 void cec_delete_adapter(struct cec_adapter
*adap
);
372 int cec_s_log_addrs(struct cec_adapter
*adap
, struct cec_log_addrs
*log_addrs
,
374 void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
376 void cec_s_phys_addr_from_edid(struct cec_adapter
*adap
,
377 const struct edid
*edid
);
378 void cec_s_conn_info(struct cec_adapter
*adap
,
379 const struct cec_connector_info
*conn_info
);
380 int cec_transmit_msg(struct cec_adapter
*adap
, struct cec_msg
*msg
,
383 /* Called by the adapter */
384 void cec_transmit_done_ts(struct cec_adapter
*adap
, u8 status
,
385 u8 arb_lost_cnt
, u8 nack_cnt
, u8 low_drive_cnt
,
386 u8 error_cnt
, ktime_t ts
);
388 static inline void cec_transmit_done(struct cec_adapter
*adap
, u8 status
,
389 u8 arb_lost_cnt
, u8 nack_cnt
,
390 u8 low_drive_cnt
, u8 error_cnt
)
392 cec_transmit_done_ts(adap
, status
, arb_lost_cnt
, nack_cnt
,
393 low_drive_cnt
, error_cnt
, ktime_get());
396 * Simplified version of cec_transmit_done for hardware that doesn't retry
397 * failed transmits. So this is always just one attempt in which case
398 * the status is sufficient.
400 void cec_transmit_attempt_done_ts(struct cec_adapter
*adap
,
401 u8 status
, ktime_t ts
);
403 static inline void cec_transmit_attempt_done(struct cec_adapter
*adap
,
406 cec_transmit_attempt_done_ts(adap
, status
, ktime_get());
409 void cec_received_msg_ts(struct cec_adapter
*adap
,
410 struct cec_msg
*msg
, ktime_t ts
);
412 static inline void cec_received_msg(struct cec_adapter
*adap
,
415 cec_received_msg_ts(adap
, msg
, ktime_get());
419 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
421 * @adap: pointer to the cec adapter
422 * @is_high: when true the CEC pin is high, otherwise it is low
423 * @dropped_events: when true some events were dropped
424 * @ts: the timestamp for this event
427 void cec_queue_pin_cec_event(struct cec_adapter
*adap
, bool is_high
,
428 bool dropped_events
, ktime_t ts
);
431 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
433 * @adap: pointer to the cec adapter
434 * @is_high: when true the HPD pin is high, otherwise it is low
435 * @ts: the timestamp for this event
438 void cec_queue_pin_hpd_event(struct cec_adapter
*adap
, bool is_high
, ktime_t ts
);
441 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
443 * @adap: pointer to the cec adapter
444 * @is_high: when true the 5V pin is high, otherwise it is low
445 * @ts: the timestamp for this event
448 void cec_queue_pin_5v_event(struct cec_adapter
*adap
, bool is_high
, ktime_t ts
);
451 * cec_get_edid_phys_addr() - find and return the physical address
453 * @edid: pointer to the EDID data
454 * @size: size in bytes of the EDID data
455 * @offset: If not %NULL then the location of the physical address
456 * bytes in the EDID will be returned here. This is set to 0
457 * if there is no physical address found.
459 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
461 u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
462 unsigned int *offset
);
464 void cec_fill_conn_info_from_drm(struct cec_connector_info
*conn_info
,
465 const struct drm_connector
*connector
);
469 static inline int cec_register_adapter(struct cec_adapter
*adap
,
470 struct device
*parent
)
475 static inline void cec_unregister_adapter(struct cec_adapter
*adap
)
479 static inline void cec_delete_adapter(struct cec_adapter
*adap
)
483 static inline void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
488 static inline void cec_s_phys_addr_from_edid(struct cec_adapter
*adap
,
489 const struct edid
*edid
)
493 static inline u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
494 unsigned int *offset
)
498 return CEC_PHYS_ADDR_INVALID
;
501 static inline void cec_s_conn_info(struct cec_adapter
*adap
,
502 const struct cec_connector_info
*conn_info
)
507 cec_fill_conn_info_from_drm(struct cec_connector_info
*conn_info
,
508 const struct drm_connector
*connector
)
510 memset(conn_info
, 0, sizeof(*conn_info
));
516 * cec_phys_addr_invalidate() - set the physical address to INVALID
518 * @adap: the CEC adapter
520 * This is a simple helper function to invalidate the physical
523 static inline void cec_phys_addr_invalidate(struct cec_adapter
*adap
)
525 cec_s_phys_addr(adap
, CEC_PHYS_ADDR_INVALID
, false);
529 * cec_get_edid_spa_location() - find location of the Source Physical Address
532 * @size: the size of the EDID
534 * This EDID is expected to be a CEA-861 compliant, which means that there are
535 * at least two blocks and one or more of the extensions blocks are CEA-861
538 * The returned location is guaranteed to be <= size-2.
540 * This is an inline function since it is used by both CEC and V4L2.
541 * Ideally this would go in a module shared by both, but it is overkill to do
542 * that for just a single function.
544 static inline unsigned int cec_get_edid_spa_location(const u8
*edid
,
547 unsigned int blocks
= size
/ 128;
551 /* Sanity check: at least 2 blocks and a multiple of the block size */
552 if (blocks
< 2 || size
% 128)
556 * If there are fewer extension blocks than the size, then update
557 * 'blocks'. It is allowed to have more extension blocks than the size,
558 * since some hardware can only read e.g. 256 bytes of the EDID, even
559 * though more blocks are present. The first CEA-861 extension block
560 * should normally be in block 1 anyway.
562 if (edid
[0x7e] + 1 < blocks
)
563 blocks
= edid
[0x7e] + 1;
565 for (block
= 1; block
< blocks
; block
++) {
566 unsigned int offset
= block
* 128;
568 /* Skip any non-CEA-861 extension blocks */
569 if (edid
[offset
] != 0x02 || edid
[offset
+ 1] != 0x03)
572 /* search Vendor Specific Data Block (tag 3) */
573 d
= edid
[offset
+ 2] & 0x7f;
574 /* Check if there are Data Blocks */
578 unsigned int i
= offset
+ 4;
579 unsigned int end
= offset
+ d
;
581 /* Note: 'end' is always < 'size' */
583 u8 tag
= edid
[i
] >> 5;
584 u8 len
= edid
[i
] & 0x1f;
586 if (tag
== 3 && len
>= 5 && i
+ len
<= end
&&
587 edid
[i
+ 1] == 0x03 &&
588 edid
[i
+ 2] == 0x0c &&
598 #endif /* _MEDIA_CEC_H */