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-edid.h>
33 #include <media/cec-notifier.h>
36 * struct cec_devnode - cec device node
38 * @cdev: cec character device
39 * @minor: device node minor number
40 * @registered: the device was correctly registered
41 * @unregistered: the device was unregistered
42 * @fhs_lock: lock to control access to the filehandle list
43 * @fhs: the list of open filehandles (cec_fh)
45 * This structure represents a cec-related device node.
47 * The @parent is a physical device. It must be set by core or device drivers
48 * before registering the node.
67 struct list_head list;
68 struct list_head xfer_list;
69 struct cec_adapter *adap;
72 struct delayed_work work;
80 struct cec_msg_entry {
81 struct list_head list;
85 #define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
88 struct list_head list;
89 struct list_head xfer_list;
90 struct cec_adapter *adap;
95 wait_queue_head_t wait;
96 unsigned int pending_events;
97 struct cec_event events[CEC_NUM_EVENTS];
99 struct list_head msgs; /* queued messages */
100 unsigned int queued_msgs;
103 #define CEC_SIGNAL_FREE_TIME_RETRY 3
104 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
105 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
107 /* The nominal data bit period is 2.4 ms */
108 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
110 struct cec_adap_ops {
111 /* Low-level callbacks */
112 int (*adap_enable)(struct cec_adapter *adap, bool enable);
113 int (*adap_monitor_all_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);
119 /* High-level CEC message callback */
120 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
124 * The minimum message length you can receive (excepting poll messages) is 2.
125 * With a transfer rate of at most 36 bytes per second this makes 18 messages
126 * per second worst case.
128 * We queue at most 3 seconds worth of received messages. The CEC specification
129 * requires that messages are replied to within a second, so 3 seconds should
130 * give more than enough margin. Since most messages are actually more than 2
131 * bytes, this is in practice a lot more than 3 seconds.
133 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
136 * The transmit queue is limited to 1 second worth of messages (worst case).
137 * Messages can be transmitted by userspace and kernel space. But for both it
138 * makes no sense to have a lot of messages queued up. One second seems
141 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
144 struct module *owner;
146 struct cec_devnode devnode;
150 struct list_head transmit_queue;
151 unsigned int transmit_queue_sz;
152 struct list_head wait_queue;
153 struct cec_data *transmitting;
155 struct task_struct *kthread_config;
156 struct completion config_completion;
158 struct task_struct *kthread;
159 wait_queue_head_t kthread_waitq;
160 wait_queue_head_t waitq;
162 const struct cec_adap_ops *ops;
165 u8 available_log_addrs;
172 struct cec_fh *cec_follower;
173 struct cec_fh *cec_initiator;
175 struct cec_log_addrs log_addrs;
177 #ifdef CONFIG_MEDIA_CEC_NOTIFIER
178 struct cec_notifier *notifier;
181 struct dentry *cec_dir;
182 struct dentry *status_file;
192 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
197 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
199 return adap->log_addrs.log_addr_mask & (1 << log_addr);
202 static inline bool cec_is_sink(const struct cec_adapter *adap)
204 return adap->phys_addr == 0;
207 #if IS_ENABLED(CONFIG_CEC_CORE)
208 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
209 void *priv, const char *name, u32 caps, u8 available_las);
210 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
211 void cec_unregister_adapter(struct cec_adapter *adap);
212 void cec_delete_adapter(struct cec_adapter *adap);
214 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
216 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
218 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
221 /* Called by the adapter */
222 void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
223 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
224 void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
226 #ifdef CONFIG_MEDIA_CEC_NOTIFIER
227 void cec_register_cec_notifier(struct cec_adapter *adap,
228 struct cec_notifier *notifier);
233 static inline int cec_register_adapter(struct cec_adapter *adap,
234 struct device *parent)
239 static inline void cec_unregister_adapter(struct cec_adapter *adap)
243 static inline void cec_delete_adapter(struct cec_adapter *adap)
247 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
254 #endif /* _MEDIA_CEC_H */