]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/media/cec.h
[media] cec: Kconfig cleanup
[karo-tx-linux.git] / include / media / cec.h
1 /*
2  * cec - HDMI Consumer Electronics Control support header
3  *
4  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *
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.
9  *
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
17  * SOFTWARE.
18  */
19
20 #ifndef _MEDIA_CEC_H
21 #define _MEDIA_CEC_H
22
23 #include <linux/poll.h>
24 #include <linux/fs.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>
34
35 /**
36  * struct cec_devnode - cec device node
37  * @dev:        cec device
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)
44  *
45  * This structure represents a cec-related device node.
46  *
47  * The @parent is a physical device. It must be set by core or device drivers
48  * before registering the node.
49  */
50 struct cec_devnode {
51         /* sysfs */
52         struct device dev;
53         struct cdev cdev;
54
55         /* device info */
56         int minor;
57         bool registered;
58         bool unregistered;
59         struct list_head fhs;
60         struct mutex lock;
61 };
62
63 struct cec_adapter;
64 struct cec_data;
65
66 struct cec_data {
67         struct list_head list;
68         struct list_head xfer_list;
69         struct cec_adapter *adap;
70         struct cec_msg msg;
71         struct cec_fh *fh;
72         struct delayed_work work;
73         struct completion c;
74         u8 attempts;
75         bool new_initiator;
76         bool blocking;
77         bool completed;
78 };
79
80 struct cec_msg_entry {
81         struct list_head        list;
82         struct cec_msg          msg;
83 };
84
85 #define CEC_NUM_EVENTS          CEC_EVENT_LOST_MSGS
86
87 struct cec_fh {
88         struct list_head        list;
89         struct list_head        xfer_list;
90         struct cec_adapter      *adap;
91         u8                      mode_initiator;
92         u8                      mode_follower;
93
94         /* Events */
95         wait_queue_head_t       wait;
96         unsigned int            pending_events;
97         struct cec_event        events[CEC_NUM_EVENTS];
98         struct mutex            lock;
99         struct list_head        msgs; /* queued messages */
100         unsigned int            queued_msgs;
101 };
102
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
106
107 /* The nominal data bit period is 2.4 ms */
108 #define CEC_FREE_TIME_TO_USEC(ft)               ((ft) * 2400)
109
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);
118
119         /* High-level CEC message callback */
120         int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
121 };
122
123 /*
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.
127  *
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.
132  */
133 #define CEC_MAX_MSG_RX_QUEUE_SZ         (18 * 3)
134
135 /*
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
139  * reasonable.
140  */
141 #define CEC_MAX_MSG_TX_QUEUE_SZ         (18 * 1)
142
143 struct cec_adapter {
144         struct module *owner;
145         char name[32];
146         struct cec_devnode devnode;
147         struct mutex lock;
148         struct rc_dev *rc;
149
150         struct list_head transmit_queue;
151         unsigned int transmit_queue_sz;
152         struct list_head wait_queue;
153         struct cec_data *transmitting;
154
155         struct task_struct *kthread_config;
156         struct completion config_completion;
157
158         struct task_struct *kthread;
159         wait_queue_head_t kthread_waitq;
160         wait_queue_head_t waitq;
161
162         const struct cec_adap_ops *ops;
163         void *priv;
164         u32 capabilities;
165         u8 available_log_addrs;
166
167         u16 phys_addr;
168         bool is_configuring;
169         bool is_configured;
170         u32 monitor_all_cnt;
171         u32 follower_cnt;
172         struct cec_fh *cec_follower;
173         struct cec_fh *cec_initiator;
174         bool passthrough;
175         struct cec_log_addrs log_addrs;
176
177 #ifdef CONFIG_MEDIA_CEC_NOTIFIER
178         struct cec_notifier *notifier;
179 #endif
180
181         struct dentry *cec_dir;
182         struct dentry *status_file;
183
184         u16 phys_addrs[15];
185         u32 sequence;
186
187         char input_name[32];
188         char input_phys[32];
189         char input_drv[32];
190 };
191
192 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
193 {
194         return adap->priv;
195 }
196
197 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
198 {
199         return adap->log_addrs.log_addr_mask & (1 << log_addr);
200 }
201
202 static inline bool cec_is_sink(const struct cec_adapter *adap)
203 {
204         return adap->phys_addr == 0;
205 }
206
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);
213
214 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
215                     bool block);
216 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
217                      bool block);
218 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
219                      bool block);
220
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);
225
226 #ifdef CONFIG_MEDIA_CEC_NOTIFIER
227 void cec_register_cec_notifier(struct cec_adapter *adap,
228                                struct cec_notifier *notifier);
229 #endif
230
231 #else
232
233 static inline int cec_register_adapter(struct cec_adapter *adap,
234                                        struct device *parent)
235 {
236         return 0;
237 }
238
239 static inline void cec_unregister_adapter(struct cec_adapter *adap)
240 {
241 }
242
243 static inline void cec_delete_adapter(struct cec_adapter *adap)
244 {
245 }
246
247 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
248                                    bool block)
249 {
250 }
251
252 #endif
253
254 #endif /* _MEDIA_CEC_H */