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/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>
20 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
21 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
24 * struct cec_devnode - cec device node
26 * @cdev: cec character device
27 * @minor: device node minor number
28 * @lock: lock to serialize open/release and registration
29 * @registered: the device was correctly registered
30 * @unregistered: the device was unregistered
31 * @lock_fhs: lock to control access to @fhs
32 * @fhs: the list of open filehandles (cec_fh)
34 * This structure represents a cec-related device node.
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.
39 * The @parent is a physical device. It must be set by core or device drivers
40 * before registering the node.
49 /* serialize open/release and registration */
53 /* protect access to fhs */
54 struct mutex lock_fhs
;
64 struct list_head list
;
65 struct list_head xfer_list
;
66 struct cec_adapter
*adap
;
71 struct delayed_work work
;
78 struct cec_msg_entry
{
79 struct list_head list
;
83 struct cec_event_entry
{
84 struct list_head list
;
88 #define CEC_NUM_CORE_EVENTS 2
89 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
92 struct list_head list
;
93 struct list_head xfer_list
;
94 struct cec_adapter
*adap
;
99 wait_queue_head_t wait
;
101 struct list_head events
[CEC_NUM_EVENTS
]; /* queued events */
102 u16 queued_events
[CEC_NUM_EVENTS
];
103 unsigned int total_queued_events
;
104 struct cec_event_entry core_events
[CEC_NUM_CORE_EVENTS
];
105 struct list_head msgs
; /* queued messages */
106 unsigned int queued_msgs
;
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
113 /* The nominal data bit period is 2.4 ms */
114 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
116 struct cec_adap_ops
{
117 /* Low-level callbacks, called with adap->lock held */
118 int (*adap_enable
)(struct cec_adapter
*adap
, bool enable
);
119 int (*adap_monitor_all_enable
)(struct cec_adapter
*adap
, bool enable
);
120 int (*adap_monitor_pin_enable
)(struct cec_adapter
*adap
, bool enable
);
121 int (*adap_log_addr
)(struct cec_adapter
*adap
, u8 logical_addr
);
122 void (*adap_unconfigured
)(struct cec_adapter
*adap
);
123 int (*adap_transmit
)(struct cec_adapter
*adap
, u8 attempts
,
124 u32 signal_free_time
, struct cec_msg
*msg
);
125 void (*adap_nb_transmit_canceled
)(struct cec_adapter
*adap
,
126 const struct cec_msg
*msg
);
127 void (*adap_status
)(struct cec_adapter
*adap
, struct seq_file
*file
);
128 void (*adap_free
)(struct cec_adapter
*adap
);
130 /* Error injection callbacks, called without adap->lock held */
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
);
134 /* High-level CEC message callback, called without adap->lock held */
135 void (*configured
)(struct cec_adapter
*adap
);
136 int (*received
)(struct cec_adapter
*adap
, struct cec_msg
*msg
);
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.
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.
149 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
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
157 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
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
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.
176 * @xfer_timeout_ms: the transfer timeout in ms.
177 * If 0, then timeout after 2100 ms.
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
190 * @is_enabled: the CEC adapter is enabled
191 * @is_claiming_log_addrs: true if cec_claim_log_addrs() is running
192 * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
193 * @must_reconfigure: while configuring, the PA changed, so reclaim LAs
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
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.
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
210 * @log_addrs: current logical addresses
211 * @conn_info: current connector info
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
226 * @notifier: CEC notifier
227 * @pin: CEC pin status struct
228 * @cec_dir: debugfs cec directory
229 * @sequence: transmit sequence counter
230 * @input_phys: remote control input_phys name
232 * This structure represents a cec adapter.
235 struct module
*owner
;
237 struct cec_devnode devnode
;
241 struct list_head transmit_queue
;
242 unsigned int transmit_queue_sz
;
243 struct list_head wait_queue
;
244 struct cec_data
*transmitting
;
245 bool transmit_in_progress
;
246 bool transmit_in_progress_aborted
;
247 unsigned int xfer_timeout_ms
;
249 struct task_struct
*kthread_config
;
250 struct completion config_completion
;
252 struct task_struct
*kthread
;
253 wait_queue_head_t kthread_waitq
;
255 const struct cec_adap_ops
*ops
;
258 u8 available_log_addrs
;
263 bool is_claiming_log_addrs
;
265 bool must_reconfigure
;
267 bool cec_pin_is_high
;
268 bool adap_controls_phys_addr
;
273 struct cec_fh
*cec_follower
;
274 struct cec_fh
*cec_initiator
;
276 struct cec_log_addrs log_addrs
;
277 struct cec_connector_info conn_info
;
280 u32 tx_low_drive_cnt
;
283 u32 tx_low_drive_log_cnt
;
284 u32 tx_error_log_cnt
;
286 #ifdef CONFIG_CEC_NOTIFIER
287 struct cec_notifier
*notifier
;
289 #ifdef CONFIG_CEC_PIN
293 struct dentry
*cec_dir
;
300 static inline int cec_get_device(struct cec_adapter
*adap
)
302 struct cec_devnode
*devnode
= &adap
->devnode
;
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
311 mutex_lock(&devnode
->lock
);
313 * return ENODEV if the cec device has been removed
314 * already or if it is not registered anymore.
316 if (!devnode
->registered
) {
317 mutex_unlock(&devnode
->lock
);
320 /* and increase the device refcount */
321 get_device(&devnode
->dev
);
322 mutex_unlock(&devnode
->lock
);
326 static inline void cec_put_device(struct cec_adapter
*adap
)
328 put_device(&adap
->devnode
.dev
);
331 static inline void *cec_get_drvdata(const struct cec_adapter
*adap
)
336 static inline bool cec_has_log_addr(const struct cec_adapter
*adap
, u8 log_addr
)
338 return adap
->log_addrs
.log_addr_mask
& (1 << log_addr
);
341 static inline bool cec_is_sink(const struct cec_adapter
*adap
)
343 return adap
->phys_addr
== 0;
347 * cec_is_registered() - is the CEC adapter registered?
349 * @adap: the CEC adapter, may be NULL.
351 * Return: true if the adapter is registered, false otherwise.
353 static inline bool cec_is_registered(const struct cec_adapter
*adap
)
355 return adap
&& adap
->devnode
.registered
;
358 #define cec_phys_addr_exp(pa) \
359 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
362 struct drm_connector
;
364 #if IS_REACHABLE(CONFIG_CEC_CORE)
365 struct cec_adapter
*cec_allocate_adapter(const struct cec_adap_ops
*ops
,
366 void *priv
, const char *name
, u32 caps
, u8 available_las
);
367 int cec_register_adapter(struct cec_adapter
*adap
, struct device
*parent
);
368 void cec_unregister_adapter(struct cec_adapter
*adap
);
369 void cec_delete_adapter(struct cec_adapter
*adap
);
371 int cec_s_log_addrs(struct cec_adapter
*adap
, struct cec_log_addrs
*log_addrs
,
373 void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
375 void cec_s_phys_addr_from_edid(struct cec_adapter
*adap
,
376 const struct edid
*edid
);
377 void cec_s_conn_info(struct cec_adapter
*adap
,
378 const struct cec_connector_info
*conn_info
);
379 int cec_transmit_msg(struct cec_adapter
*adap
, struct cec_msg
*msg
,
382 /* Called by the adapter */
383 void 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
);
387 static 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
)
391 cec_transmit_done_ts(adap
, status
, arb_lost_cnt
, nack_cnt
,
392 low_drive_cnt
, error_cnt
, ktime_get());
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.
399 void cec_transmit_attempt_done_ts(struct cec_adapter
*adap
,
400 u8 status
, ktime_t ts
);
402 static inline void cec_transmit_attempt_done(struct cec_adapter
*adap
,
405 cec_transmit_attempt_done_ts(adap
, status
, ktime_get());
408 void cec_received_msg_ts(struct cec_adapter
*adap
,
409 struct cec_msg
*msg
, ktime_t ts
);
411 static inline void cec_received_msg(struct cec_adapter
*adap
,
414 cec_received_msg_ts(adap
, msg
, ktime_get());
418 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
420 * @adap: pointer to the cec adapter
421 * @is_high: when true the CEC pin is high, otherwise it is low
422 * @dropped_events: when true some events were dropped
423 * @ts: the timestamp for this event
426 void cec_queue_pin_cec_event(struct cec_adapter
*adap
, bool is_high
,
427 bool dropped_events
, ktime_t ts
);
430 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
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
437 void cec_queue_pin_hpd_event(struct cec_adapter
*adap
, bool is_high
, ktime_t ts
);
440 * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
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
447 void cec_queue_pin_5v_event(struct cec_adapter
*adap
, bool is_high
, ktime_t ts
);
450 * cec_get_edid_phys_addr() - find and return the physical address
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.
458 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
460 u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
461 unsigned int *offset
);
463 void cec_fill_conn_info_from_drm(struct cec_connector_info
*conn_info
,
464 const struct drm_connector
*connector
);
468 static inline int cec_register_adapter(struct cec_adapter
*adap
,
469 struct device
*parent
)
474 static inline void cec_unregister_adapter(struct cec_adapter
*adap
)
478 static inline void cec_delete_adapter(struct cec_adapter
*adap
)
482 static inline void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
487 static inline void cec_s_phys_addr_from_edid(struct cec_adapter
*adap
,
488 const struct edid
*edid
)
492 static inline u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
493 unsigned int *offset
)
497 return CEC_PHYS_ADDR_INVALID
;
500 static inline void cec_s_conn_info(struct cec_adapter
*adap
,
501 const struct cec_connector_info
*conn_info
)
506 cec_fill_conn_info_from_drm(struct cec_connector_info
*conn_info
,
507 const struct drm_connector
*connector
)
509 memset(conn_info
, 0, sizeof(*conn_info
));
515 * cec_phys_addr_invalidate() - set the physical address to INVALID
517 * @adap: the CEC adapter
519 * This is a simple helper function to invalidate the physical
522 static inline void cec_phys_addr_invalidate(struct cec_adapter
*adap
)
524 cec_s_phys_addr(adap
, CEC_PHYS_ADDR_INVALID
, false);
528 * cec_get_edid_spa_location() - find location of the Source Physical Address
531 * @size: the size of the EDID
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
537 * The returned location is guaranteed to be <= size-2.
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.
543 static inline unsigned int cec_get_edid_spa_location(const u8
*edid
,
546 unsigned int blocks
= size
/ 128;
550 /* Sanity check: at least 2 blocks and a multiple of the block size */
551 if (blocks
< 2 || size
% 128)
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.
561 if (edid
[0x7e] + 1 < blocks
)
562 blocks
= edid
[0x7e] + 1;
564 for (block
= 1; block
< blocks
; block
++) {
565 unsigned int offset
= block
* 128;
567 /* Skip any non-CEA-861 extension blocks */
568 if (edid
[offset
] != 0x02 || edid
[offset
+ 1] != 0x03)
571 /* search Vendor Specific Data Block (tag 3) */
572 d
= edid
[offset
+ 2] & 0x7f;
573 /* Check if there are Data Blocks */
577 unsigned int i
= offset
+ 4;
578 unsigned int end
= offset
+ d
;
580 /* Note: 'end' is always < 'size' */
582 u8 tag
= edid
[i
] >> 5;
583 u8 len
= edid
[i
] & 0x1f;
585 if (tag
== 3 && len
>= 5 && i
+ len
<= end
&&
586 edid
[i
+ 1] == 0x03 &&
587 edid
[i
+ 2] == 0x0c &&
597 #endif /* _MEDIA_CEC_H */