blob: 37768203572d4275ebc2378be543b8956d49cce3 [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>
Hans Verkuile3a93ad2016-12-13 12:15:57 -020032#include <media/cec-notifier.h>
Hans Verkuila56960e2016-06-25 09:43:58 -030033
34/**
35 * struct cec_devnode - cec device node
36 * @dev: cec device
37 * @cdev: cec character device
Hans Verkuila56960e2016-06-25 09:43:58 -030038 * @minor: device node minor number
39 * @registered: the device was correctly registered
40 * @unregistered: the device was unregistered
41 * @fhs_lock: lock to control access to the filehandle list
42 * @fhs: the list of open filehandles (cec_fh)
43 *
44 * This structure represents a cec-related device node.
45 *
46 * The @parent is a physical device. It must be set by core or device drivers
47 * before registering the node.
48 */
49struct cec_devnode {
50 /* sysfs */
51 struct device dev;
52 struct cdev cdev;
Hans Verkuila56960e2016-06-25 09:43:58 -030053
54 /* device info */
55 int minor;
56 bool registered;
57 bool unregistered;
Hans Verkuila56960e2016-06-25 09:43:58 -030058 struct list_head fhs;
Hans Verkuil62148f092016-08-02 08:11:00 -030059 struct mutex lock;
Hans Verkuila56960e2016-06-25 09:43:58 -030060};
61
62struct cec_adapter;
63struct cec_data;
64
65struct cec_data {
66 struct list_head list;
67 struct list_head xfer_list;
68 struct cec_adapter *adap;
69 struct cec_msg msg;
70 struct cec_fh *fh;
71 struct delayed_work work;
72 struct completion c;
73 u8 attempts;
74 bool new_initiator;
75 bool blocking;
76 bool completed;
77};
78
79struct cec_msg_entry {
80 struct list_head list;
81 struct cec_msg msg;
82};
83
84#define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
85
86struct cec_fh {
87 struct list_head list;
88 struct list_head xfer_list;
89 struct cec_adapter *adap;
90 u8 mode_initiator;
91 u8 mode_follower;
92
93 /* Events */
94 wait_queue_head_t wait;
95 unsigned int pending_events;
96 struct cec_event events[CEC_NUM_EVENTS];
97 struct mutex lock;
98 struct list_head msgs; /* queued messages */
99 unsigned int queued_msgs;
100};
101
102#define CEC_SIGNAL_FREE_TIME_RETRY 3
103#define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
104#define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
105
106/* The nominal data bit period is 2.4 ms */
107#define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
108
109struct cec_adap_ops {
110 /* Low-level callbacks */
111 int (*adap_enable)(struct cec_adapter *adap, bool enable);
112 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
113 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
114 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
115 u32 signal_free_time, struct cec_msg *msg);
116 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
Hans Verkuile6259b52017-07-11 03:30:36 -0300117 void (*adap_free)(struct cec_adapter *adap);
Hans Verkuila56960e2016-06-25 09:43:58 -0300118
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 *
Hans Verkuil11065f82016-07-17 11:40:05 -0300128 * 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.
Hans Verkuila56960e2016-06-25 09:43:58 -0300132 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300133#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)
Hans Verkuila56960e2016-06-25 09:43:58 -0300142
143struct 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;
Hans Verkuil11065f82016-07-17 11:40:05 -0300151 unsigned int transmit_queue_sz;
Hans Verkuila56960e2016-06-25 09:43:58 -0300152 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;
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300168 bool needs_hpd;
Hans Verkuila56960e2016-06-25 09:43:58 -0300169 bool is_configuring;
170 bool is_configured;
171 u32 monitor_all_cnt;
172 u32 follower_cnt;
173 struct cec_fh *cec_follower;
174 struct cec_fh *cec_initiator;
175 bool passthrough;
176 struct cec_log_addrs log_addrs;
177
Hans Verkuilbb789e02017-07-11 03:30:34 -0300178 u32 tx_timeouts;
179
Hans Verkuile94c3282017-05-28 05:58:04 -0300180#ifdef CONFIG_CEC_NOTIFIER
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200181 struct cec_notifier *notifier;
182#endif
183
Hans Verkuila56960e2016-06-25 09:43:58 -0300184 struct dentry *cec_dir;
185 struct dentry *status_file;
186
187 u16 phys_addrs[15];
188 u32 sequence;
189
190 char input_name[32];
191 char input_phys[32];
192 char input_drv[32];
193};
194
Jose Abreu0b0b97d2017-03-24 13:47:52 -0300195static inline void *cec_get_drvdata(const struct cec_adapter *adap)
196{
197 return adap->priv;
198}
199
Hans Verkuila56960e2016-06-25 09:43:58 -0300200static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
201{
202 return adap->log_addrs.log_addr_mask & (1 << log_addr);
203}
204
205static inline bool cec_is_sink(const struct cec_adapter *adap)
206{
207 return adap->phys_addr == 0;
208}
209
Hans Verkuilee7e98712017-04-17 07:54:37 -0300210#define cec_phys_addr_exp(pa) \
211 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
212
Hans Verkuil23111ec2017-06-07 11:46:08 -0300213struct edid;
214
Hans Verkuilf9f314f2017-06-08 15:37:44 -0300215#if IS_REACHABLE(CONFIG_CEC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300216struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
Hans Verkuilf51e8082016-11-25 06:23:34 -0200217 void *priv, const char *name, u32 caps, u8 available_las);
218int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
Hans Verkuila56960e2016-06-25 09:43:58 -0300219void cec_unregister_adapter(struct cec_adapter *adap);
220void cec_delete_adapter(struct cec_adapter *adap);
221
222int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
223 bool block);
224void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
225 bool block);
Hans Verkuil23111ec2017-06-07 11:46:08 -0300226void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
227 const struct edid *edid);
Hans Verkuila56960e2016-06-25 09:43:58 -0300228int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
229 bool block);
230
231/* Called by the adapter */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300232void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
233 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
234 u8 error_cnt, ktime_t ts);
235
236static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
237 u8 arb_lost_cnt, u8 nack_cnt,
238 u8 low_drive_cnt, u8 error_cnt)
239{
240 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
241 low_drive_cnt, error_cnt, ktime_get());
242}
Hans Verkuilc94cdc12017-06-07 11:46:10 -0300243/*
244 * Simplified version of cec_transmit_done for hardware that doesn't retry
245 * failed transmits. So this is always just one attempt in which case
246 * the status is sufficient.
247 */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300248void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
249 u8 status, ktime_t ts);
250
251static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
252 u8 status)
253{
254 cec_transmit_attempt_done_ts(adap, status, ktime_get());
255}
256
257void cec_received_msg_ts(struct cec_adapter *adap,
258 struct cec_msg *msg, ktime_t ts);
259
260static inline void cec_received_msg(struct cec_adapter *adap,
261 struct cec_msg *msg)
262{
263 cec_received_msg_ts(adap, msg, ktime_get());
264}
Hans Verkuila56960e2016-06-25 09:43:58 -0300265
Hans Verkuilee7e98712017-04-17 07:54:37 -0300266/**
267 * cec_get_edid_phys_addr() - find and return the physical address
268 *
269 * @edid: pointer to the EDID data
270 * @size: size in bytes of the EDID data
271 * @offset: If not %NULL then the location of the physical address
272 * bytes in the EDID will be returned here. This is set to 0
273 * if there is no physical address found.
274 *
275 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
276 */
277u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
278 unsigned int *offset);
279
280/**
281 * cec_set_edid_phys_addr() - find and set the physical address
282 *
283 * @edid: pointer to the EDID data
284 * @size: size in bytes of the EDID data
285 * @phys_addr: the new physical address
286 *
287 * This function finds the location of the physical address in the EDID
288 * and fills in the given physical address and updates the checksum
289 * at the end of the EDID block. It does nothing if the EDID doesn't
290 * contain a physical address.
291 */
292void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
293
294/**
295 * cec_phys_addr_for_input() - calculate the PA for an input
296 *
297 * @phys_addr: the physical address of the parent
298 * @input: the number of the input port, must be between 1 and 15
299 *
300 * This function calculates a new physical address based on the input
301 * port number. For example:
302 *
303 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
304 *
305 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
306 *
307 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
308 *
309 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
310 *
311 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
312 */
313u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
314
315/**
316 * cec_phys_addr_validate() - validate a physical address from an EDID
317 *
318 * @phys_addr: the physical address to validate
319 * @parent: if not %NULL, then this is filled with the parents PA.
320 * @port: if not %NULL, then this is filled with the input port.
321 *
322 * This validates a physical address as read from an EDID. If the
323 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
324 * then it will return -EINVAL.
325 *
326 * The parent PA is passed into %parent and the input port is passed into
327 * %port. For example:
328 *
329 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
330 *
331 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
332 *
333 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
334 *
335 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
336 *
337 * Return: 0 if the PA is valid, -EINVAL if not.
338 */
339int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
340
Hans Verkuile94c3282017-05-28 05:58:04 -0300341#ifdef CONFIG_CEC_NOTIFIER
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200342void cec_register_cec_notifier(struct cec_adapter *adap,
343 struct cec_notifier *notifier);
344#endif
345
Hans Verkuila56960e2016-06-25 09:43:58 -0300346#else
347
Hans Verkuilf51e8082016-11-25 06:23:34 -0200348static inline int cec_register_adapter(struct cec_adapter *adap,
349 struct device *parent)
Hans Verkuila56960e2016-06-25 09:43:58 -0300350{
351 return 0;
352}
353
354static inline void cec_unregister_adapter(struct cec_adapter *adap)
355{
356}
357
358static inline void cec_delete_adapter(struct cec_adapter *adap)
359{
360}
361
362static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
363 bool block)
364{
365}
366
Hans Verkuil23111ec2017-06-07 11:46:08 -0300367static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
368 const struct edid *edid)
369{
370}
371
Hans Verkuilee7e98712017-04-17 07:54:37 -0300372static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
373 unsigned int *offset)
374{
375 if (offset)
376 *offset = 0;
377 return CEC_PHYS_ADDR_INVALID;
378}
379
380static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
381 u16 phys_addr)
382{
383}
384
385static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
386{
387 return CEC_PHYS_ADDR_INVALID;
388}
389
390static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
391{
392 return 0;
393}
394
Hans Verkuila56960e2016-06-25 09:43:58 -0300395#endif
396
Hans Verkuil13532782017-06-07 11:46:09 -0300397/**
398 * cec_phys_addr_invalidate() - set the physical address to INVALID
399 *
400 * @adap: the CEC adapter
401 *
402 * This is a simple helper function to invalidate the physical
403 * address.
404 */
405static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
406{
407 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
408}
409
Hans Verkuila56960e2016-06-25 09:43:58 -0300410#endif /* _MEDIA_CEC_H */