blob: a22cf3460e0d3fd4d66c091b31db35e2558e3bf3 [file] [log] [blame]
Hans Verkuila56960e2016-06-25 09:43:58 -03001/*
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
Hans Verkuil23369d22017-08-04 06:41:51 -040034#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
35 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
36
Hans Verkuila56960e2016-06-25 09:43:58 -030037/**
38 * struct cec_devnode - cec device node
39 * @dev: cec device
40 * @cdev: cec character device
41 * @parent: parent device
42 * @minor: device node minor number
43 * @registered: the device was correctly registered
44 * @unregistered: the device was unregistered
45 * @fhs_lock: lock to control access to the filehandle list
46 * @fhs: the list of open filehandles (cec_fh)
47 *
48 * This structure represents a cec-related device node.
49 *
50 * The @parent is a physical device. It must be set by core or device drivers
51 * before registering the node.
52 */
53struct cec_devnode {
54 /* sysfs */
55 struct device dev;
56 struct cdev cdev;
57 struct device *parent;
58
59 /* device info */
60 int minor;
61 bool registered;
62 bool unregistered;
Hans Verkuila56960e2016-06-25 09:43:58 -030063 struct list_head fhs;
Hans Verkuil62148f02016-08-02 08:11:00 -030064 struct mutex lock;
Hans Verkuila56960e2016-06-25 09:43:58 -030065};
66
67struct cec_adapter;
68struct cec_data;
69
70struct cec_data {
71 struct list_head list;
72 struct list_head xfer_list;
73 struct cec_adapter *adap;
74 struct cec_msg msg;
75 struct cec_fh *fh;
76 struct delayed_work work;
77 struct completion c;
78 u8 attempts;
79 bool new_initiator;
80 bool blocking;
81 bool completed;
82};
83
84struct cec_msg_entry {
85 struct list_head list;
86 struct cec_msg msg;
87};
88
89#define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
90
91struct cec_fh {
92 struct list_head list;
93 struct list_head xfer_list;
94 struct cec_adapter *adap;
95 u8 mode_initiator;
96 u8 mode_follower;
97
98 /* Events */
99 wait_queue_head_t wait;
100 unsigned int pending_events;
101 struct cec_event events[CEC_NUM_EVENTS];
102 struct mutex lock;
103 struct list_head msgs; /* queued messages */
104 unsigned int queued_msgs;
105};
106
107#define CEC_SIGNAL_FREE_TIME_RETRY 3
108#define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
109#define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
110
111/* The nominal data bit period is 2.4 ms */
112#define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
113
114struct cec_adap_ops {
115 /* Low-level callbacks */
116 int (*adap_enable)(struct cec_adapter *adap, bool enable);
117 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
118 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
119 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
120 u32 signal_free_time, struct cec_msg *msg);
121 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
122
123 /* High-level CEC message callback */
124 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
125};
126
127/*
128 * The minimum message length you can receive (excepting poll messages) is 2.
129 * With a transfer rate of at most 36 bytes per second this makes 18 messages
130 * per second worst case.
131 *
Hans Verkuil11065f82016-07-17 11:40:05 -0300132 * We queue at most 3 seconds worth of received messages. The CEC specification
133 * requires that messages are replied to within a second, so 3 seconds should
134 * give more than enough margin. Since most messages are actually more than 2
135 * bytes, this is in practice a lot more than 3 seconds.
Hans Verkuila56960e2016-06-25 09:43:58 -0300136 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300137#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
138
139/*
140 * The transmit queue is limited to 1 second worth of messages (worst case).
141 * Messages can be transmitted by userspace and kernel space. But for both it
142 * makes no sense to have a lot of messages queued up. One second seems
143 * reasonable.
144 */
145#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
Hans Verkuila56960e2016-06-25 09:43:58 -0300146
147struct cec_adapter {
148 struct module *owner;
149 char name[32];
150 struct cec_devnode devnode;
151 struct mutex lock;
152 struct rc_dev *rc;
153
154 struct list_head transmit_queue;
Hans Verkuil11065f82016-07-17 11:40:05 -0300155 unsigned int transmit_queue_sz;
Hans Verkuila56960e2016-06-25 09:43:58 -0300156 struct list_head wait_queue;
157 struct cec_data *transmitting;
158
159 struct task_struct *kthread_config;
160 struct completion config_completion;
161
162 struct task_struct *kthread;
163 wait_queue_head_t kthread_waitq;
164 wait_queue_head_t waitq;
165
166 const struct cec_adap_ops *ops;
167 void *priv;
168 u32 capabilities;
169 u8 available_log_addrs;
170
171 u16 phys_addr;
172 bool is_configuring;
173 bool is_configured;
174 u32 monitor_all_cnt;
175 u32 follower_cnt;
176 struct cec_fh *cec_follower;
177 struct cec_fh *cec_initiator;
178 bool passthrough;
179 struct cec_log_addrs log_addrs;
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
Jose Abreu57da8012017-03-24 13:47:52 -0300192static inline void *cec_get_drvdata(const struct cec_adapter *adap)
193{
194 return adap->priv;
195}
196
Hans Verkuila56960e2016-06-25 09:43:58 -0300197static 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
202static inline bool cec_is_sink(const struct cec_adapter *adap)
203{
204 return adap->phys_addr == 0;
205}
206
Hans Verkuile5a66cf2017-06-07 11:46:08 -0300207struct edid;
208
Hans Verkuila56960e2016-06-25 09:43:58 -0300209#if IS_ENABLED(CONFIG_MEDIA_CEC)
210struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
211 void *priv, const char *name, u32 caps, u8 available_las,
212 struct device *parent);
213int cec_register_adapter(struct cec_adapter *adap);
214void cec_unregister_adapter(struct cec_adapter *adap);
215void cec_delete_adapter(struct cec_adapter *adap);
216
217int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
218 bool block);
219void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
220 bool block);
Hans Verkuile5a66cf2017-06-07 11:46:08 -0300221void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
222 const struct edid *edid);
Hans Verkuila56960e2016-06-25 09:43:58 -0300223int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
224 bool block);
225
226/* Called by the adapter */
227void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
228 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
Hans Verkuil1e726952017-06-07 11:46:10 -0300229/*
230 * Simplified version of cec_transmit_done for hardware that doesn't retry
231 * failed transmits. So this is always just one attempt in which case
232 * the status is sufficient.
233 */
234void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status);
Hans Verkuila56960e2016-06-25 09:43:58 -0300235void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
236
237#else
238
239static inline int cec_register_adapter(struct cec_adapter *adap)
240{
241 return 0;
242}
243
244static inline void cec_unregister_adapter(struct cec_adapter *adap)
245{
246}
247
248static inline void cec_delete_adapter(struct cec_adapter *adap)
249{
250}
251
252static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
253 bool block)
254{
255}
256
Hans Verkuile5a66cf2017-06-07 11:46:08 -0300257static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
258 const struct edid *edid)
259{
260}
261
Hans Verkuila56960e2016-06-25 09:43:58 -0300262#endif
263
Hans Verkuilac0c0672017-06-07 11:46:09 -0300264/**
265 * cec_phys_addr_invalidate() - set the physical address to INVALID
266 *
267 * @adap: the CEC adapter
268 *
269 * This is a simple helper function to invalidate the physical
270 * address.
271 */
272static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
273{
274 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
275}
276
Hans Verkuila56960e2016-06-25 09:43:58 -0300277#endif /* _MEDIA_CEC_H */