2 * cec - HDMI Consumer Electronics Control support header
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include <linux/poll.h>
25 #include <linux/debugfs.h>
26 #include <linux/device.h>
27 #include <linux/cdev.h>
28 #include <linux/kthread.h>
29 #include <linux/timer.h>
30 #include <linux/cec-funcs.h>
31 #include <media/rc-core.h>
32 #include <media/cec-notifier.h>
34 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
35 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
38 * struct cec_devnode - cec device node
40 * @cdev: cec character device
41 * @minor: device node minor number
42 * @registered: the device was correctly registered
43 * @unregistered: the device was unregistered
44 * @fhs_lock: lock to control access to the filehandle list
45 * @fhs: the list of open filehandles (cec_fh)
47 * This structure represents a cec-related device node.
49 * The @parent is a physical device. It must be set by core or device drivers
50 * before registering the node.
70 struct list_head list;
71 struct list_head xfer_list;
72 struct cec_adapter *adap;
75 struct delayed_work work;
83 struct cec_msg_entry {
84 struct list_head list;
88 struct cec_event_entry {
89 struct list_head list;
93 #define CEC_NUM_CORE_EVENTS 2
94 #define CEC_NUM_EVENTS CEC_EVENT_PIN_CEC_HIGH
97 struct list_head list;
98 struct list_head xfer_list;
99 struct cec_adapter *adap;
104 wait_queue_head_t wait;
106 struct list_head events[CEC_NUM_EVENTS]; /* queued events */
107 u8 queued_events[CEC_NUM_EVENTS];
108 unsigned int total_queued_events;
109 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
110 struct list_head msgs; /* queued messages */
111 unsigned int queued_msgs;
114 #define CEC_SIGNAL_FREE_TIME_RETRY 3
115 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
116 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
118 /* The nominal data bit period is 2.4 ms */
119 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
121 struct cec_adap_ops {
122 /* Low-level callbacks */
123 int (*adap_enable)(struct cec_adapter *adap, bool enable);
124 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
125 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
126 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
127 u32 signal_free_time, 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 /* High-level CEC message callback */
132 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
136 * The minimum message length you can receive (excepting poll messages) is 2.
137 * With a transfer rate of at most 36 bytes per second this makes 18 messages
138 * per second worst case.
140 * We queue at most 3 seconds worth of received messages. The CEC specification
141 * requires that messages are replied to within a second, so 3 seconds should
142 * give more than enough margin. Since most messages are actually more than 2
143 * bytes, this is in practice a lot more than 3 seconds.
145 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
148 * The transmit queue is limited to 1 second worth of messages (worst case).
149 * Messages can be transmitted by userspace and kernel space. But for both it
150 * makes no sense to have a lot of messages queued up. One second seems
153 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
156 struct module *owner;
158 struct cec_devnode devnode;
162 struct list_head transmit_queue;
163 unsigned int transmit_queue_sz;
164 struct list_head wait_queue;
165 struct cec_data *transmitting;
167 struct task_struct *kthread_config;
168 struct completion config_completion;
170 struct task_struct *kthread;
171 wait_queue_head_t kthread_waitq;
172 wait_queue_head_t waitq;
174 const struct cec_adap_ops *ops;
177 u8 available_log_addrs;
183 bool cec_pin_is_high;
187 struct cec_fh *cec_follower;
188 struct cec_fh *cec_initiator;
190 struct cec_log_addrs log_addrs;
194 #ifdef CONFIG_MEDIA_CEC_RC
196 int rc_last_scancode;
197 u64 rc_last_keypress;
199 #ifdef CONFIG_CEC_NOTIFIER
200 struct cec_notifier *notifier;
202 #ifdef CONFIG_CEC_PIN
206 struct dentry *cec_dir;
207 struct dentry *status_file;
212 char device_name[32];
217 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
222 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
224 return adap->log_addrs.log_addr_mask & (1 << log_addr);
227 static inline bool cec_is_sink(const struct cec_adapter *adap)
229 return adap->phys_addr == 0;
232 #define cec_phys_addr_exp(pa) \
233 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
237 #if IS_REACHABLE(CONFIG_CEC_CORE)
238 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
239 void *priv, const char *name, u32 caps, u8 available_las);
240 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
241 void cec_unregister_adapter(struct cec_adapter *adap);
242 void cec_delete_adapter(struct cec_adapter *adap);
244 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
246 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
248 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
249 const struct edid *edid);
250 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
253 /* Called by the adapter */
254 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
255 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
256 u8 error_cnt, ktime_t ts);
258 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
259 u8 arb_lost_cnt, u8 nack_cnt,
260 u8 low_drive_cnt, u8 error_cnt)
262 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
263 low_drive_cnt, error_cnt, ktime_get());
266 * Simplified version of cec_transmit_done for hardware that doesn't retry
267 * failed transmits. So this is always just one attempt in which case
268 * the status is sufficient.
270 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
271 u8 status, ktime_t ts);
273 static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
276 cec_transmit_attempt_done_ts(adap, status, ktime_get());
279 void cec_received_msg_ts(struct cec_adapter *adap,
280 struct cec_msg *msg, ktime_t ts);
282 static inline void cec_received_msg(struct cec_adapter *adap,
285 cec_received_msg_ts(adap, msg, ktime_get());
289 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
291 * @adap: pointer to the cec adapter
292 * @is_high: when true the CEC pin is high, otherwise it is low
293 * @ts: the timestamp for this event
296 void cec_queue_pin_cec_event(struct cec_adapter *adap,
297 bool is_high, ktime_t ts);
300 * cec_get_edid_phys_addr() - find and return the physical address
302 * @edid: pointer to the EDID data
303 * @size: size in bytes of the EDID data
304 * @offset: If not %NULL then the location of the physical address
305 * bytes in the EDID will be returned here. This is set to 0
306 * if there is no physical address found.
308 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
310 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
311 unsigned int *offset);
314 * cec_set_edid_phys_addr() - find and set the physical address
316 * @edid: pointer to the EDID data
317 * @size: size in bytes of the EDID data
318 * @phys_addr: the new physical address
320 * This function finds the location of the physical address in the EDID
321 * and fills in the given physical address and updates the checksum
322 * at the end of the EDID block. It does nothing if the EDID doesn't
323 * contain a physical address.
325 void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
328 * cec_phys_addr_for_input() - calculate the PA for an input
330 * @phys_addr: the physical address of the parent
331 * @input: the number of the input port, must be between 1 and 15
333 * This function calculates a new physical address based on the input
334 * port number. For example:
336 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
338 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
340 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
342 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
344 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
346 u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
349 * cec_phys_addr_validate() - validate a physical address from an EDID
351 * @phys_addr: the physical address to validate
352 * @parent: if not %NULL, then this is filled with the parents PA.
353 * @port: if not %NULL, then this is filled with the input port.
355 * This validates a physical address as read from an EDID. If the
356 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
357 * then it will return -EINVAL.
359 * The parent PA is passed into %parent and the input port is passed into
360 * %port. For example:
362 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
364 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
366 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
368 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
370 * Return: 0 if the PA is valid, -EINVAL if not.
372 int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
376 static inline int cec_register_adapter(struct cec_adapter *adap,
377 struct device *parent)
382 static inline void cec_unregister_adapter(struct cec_adapter *adap)
386 static inline void cec_delete_adapter(struct cec_adapter *adap)
390 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
395 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
396 const struct edid *edid)
400 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
401 unsigned int *offset)
405 return CEC_PHYS_ADDR_INVALID;
408 static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
413 static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
415 return CEC_PHYS_ADDR_INVALID;
418 static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
426 * cec_phys_addr_invalidate() - set the physical address to INVALID
428 * @adap: the CEC adapter
430 * This is a simple helper function to invalidate the physical
433 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
435 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
438 #endif /* _MEDIA_CEC_H */