blob: 7cdf71d7125a3e6bd1bd627b2aca8ba4dbd91c7f [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
Hans Verkuilee0c5032017-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
Hans Verkuila56960e2016-06-25 09:43:58 -030041 * @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)
46 *
47 * This structure represents a cec-related device node.
48 *
49 * The @parent is a physical device. It must be set by core or device drivers
50 * before registering the node.
51 */
52struct cec_devnode {
53 /* sysfs */
54 struct device dev;
55 struct cdev cdev;
Hans Verkuila56960e2016-06-25 09:43:58 -030056
57 /* device info */
58 int minor;
59 bool registered;
60 bool unregistered;
Hans Verkuila56960e2016-06-25 09:43:58 -030061 struct list_head fhs;
Hans Verkuil62148f02016-08-02 08:11:00 -030062 struct mutex lock;
Hans Verkuila56960e2016-06-25 09:43:58 -030063};
64
65struct cec_adapter;
66struct cec_data;
Hans Verkuilea5c8ef2017-07-11 03:30:42 -030067struct cec_pin;
Hans Verkuila56960e2016-06-25 09:43:58 -030068
69struct cec_data {
70 struct list_head list;
71 struct list_head xfer_list;
72 struct cec_adapter *adap;
73 struct cec_msg msg;
74 struct cec_fh *fh;
75 struct delayed_work work;
76 struct completion c;
77 u8 attempts;
78 bool new_initiator;
79 bool blocking;
80 bool completed;
81};
82
83struct cec_msg_entry {
84 struct list_head list;
85 struct cec_msg msg;
86};
87
Hans Verkuil6b2bbb02017-07-11 03:30:39 -030088struct cec_event_entry {
89 struct list_head list;
90 struct cec_event ev;
91};
92
93#define CEC_NUM_CORE_EVENTS 2
Hans Verkuil333ef6b2017-08-15 10:07:25 -040094#define CEC_NUM_EVENTS CEC_EVENT_PIN_HPD_HIGH
Hans Verkuila56960e2016-06-25 09:43:58 -030095
96struct cec_fh {
97 struct list_head list;
98 struct list_head xfer_list;
99 struct cec_adapter *adap;
100 u8 mode_initiator;
101 u8 mode_follower;
102
103 /* Events */
104 wait_queue_head_t wait;
Hans Verkuila56960e2016-06-25 09:43:58 -0300105 struct mutex lock;
Hans Verkuil6b2bbb02017-07-11 03:30:39 -0300106 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];
Hans Verkuila56960e2016-06-25 09:43:58 -0300110 struct list_head msgs; /* queued messages */
111 unsigned int queued_msgs;
112};
113
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
117
118/* The nominal data bit period is 2.4 ms */
119#define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
120
121struct 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);
Hans Verkuil48ea2e92017-11-05 07:36:36 -0500125 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
Hans Verkuila56960e2016-06-25 09:43:58 -0300126 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
127 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
128 u32 signal_free_time, struct cec_msg *msg);
129 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
Hans Verkuile6259b52017-07-11 03:30:36 -0300130 void (*adap_free)(struct cec_adapter *adap);
Hans Verkuila56960e2016-06-25 09:43:58 -0300131
132 /* High-level CEC message callback */
133 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
134};
135
136/*
137 * The minimum message length you can receive (excepting poll messages) is 2.
138 * With a transfer rate of at most 36 bytes per second this makes 18 messages
139 * per second worst case.
140 *
Hans Verkuil11065f82016-07-17 11:40:05 -0300141 * We queue at most 3 seconds worth of received messages. The CEC specification
142 * requires that messages are replied to within a second, so 3 seconds should
143 * give more than enough margin. Since most messages are actually more than 2
144 * bytes, this is in practice a lot more than 3 seconds.
Hans Verkuila56960e2016-06-25 09:43:58 -0300145 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300146#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
147
148/*
149 * The transmit queue is limited to 1 second worth of messages (worst case).
150 * Messages can be transmitted by userspace and kernel space. But for both it
151 * makes no sense to have a lot of messages queued up. One second seems
152 * reasonable.
153 */
154#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
Hans Verkuila56960e2016-06-25 09:43:58 -0300155
156struct cec_adapter {
157 struct module *owner;
158 char name[32];
159 struct cec_devnode devnode;
160 struct mutex lock;
161 struct rc_dev *rc;
162
163 struct list_head transmit_queue;
Hans Verkuil11065f82016-07-17 11:40:05 -0300164 unsigned int transmit_queue_sz;
Hans Verkuila56960e2016-06-25 09:43:58 -0300165 struct list_head wait_queue;
166 struct cec_data *transmitting;
167
168 struct task_struct *kthread_config;
169 struct completion config_completion;
170
171 struct task_struct *kthread;
172 wait_queue_head_t kthread_waitq;
173 wait_queue_head_t waitq;
174
175 const struct cec_adap_ops *ops;
176 void *priv;
177 u32 capabilities;
178 u8 available_log_addrs;
179
180 u16 phys_addr;
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300181 bool needs_hpd;
Hans Verkuila56960e2016-06-25 09:43:58 -0300182 bool is_configuring;
183 bool is_configured;
Hans Verkuil28e11b12017-08-20 06:53:10 -0400184 bool cec_pin_is_high;
Hans Verkuila56960e2016-06-25 09:43:58 -0300185 u32 monitor_all_cnt;
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300186 u32 monitor_pin_cnt;
Hans Verkuila56960e2016-06-25 09:43:58 -0300187 u32 follower_cnt;
188 struct cec_fh *cec_follower;
189 struct cec_fh *cec_initiator;
190 bool passthrough;
191 struct cec_log_addrs log_addrs;
192
Hans Verkuilbb789e02017-07-11 03:30:34 -0300193 u32 tx_timeouts;
194
Hans Verkuile94c3282017-05-28 05:58:04 -0300195#ifdef CONFIG_CEC_NOTIFIER
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200196 struct cec_notifier *notifier;
197#endif
Hans Verkuilea5c8ef2017-07-11 03:30:42 -0300198#ifdef CONFIG_CEC_PIN
199 struct cec_pin *pin;
200#endif
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200201
Hans Verkuila56960e2016-06-25 09:43:58 -0300202 struct dentry *cec_dir;
203 struct dentry *status_file;
204
205 u16 phys_addrs[15];
206 u32 sequence;
207
Sean Young518f4b22017-07-01 12:13:19 -0400208 char device_name[32];
Hans Verkuila56960e2016-06-25 09:43:58 -0300209 char input_phys[32];
210 char input_drv[32];
211};
212
Jose Abreu0b0b97d2017-03-24 13:47:52 -0300213static inline void *cec_get_drvdata(const struct cec_adapter *adap)
214{
215 return adap->priv;
216}
217
Hans Verkuila56960e2016-06-25 09:43:58 -0300218static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
219{
220 return adap->log_addrs.log_addr_mask & (1 << log_addr);
221}
222
223static inline bool cec_is_sink(const struct cec_adapter *adap)
224{
225 return adap->phys_addr == 0;
226}
227
Hans Verkuilc8959a32017-11-22 04:12:39 -0500228/**
229 * cec_is_registered() - is the CEC adapter registered?
230 *
231 * @adap: the CEC adapter, may be NULL.
232 *
233 * Return: true if the adapter is registered, false otherwise.
234 */
235static inline bool cec_is_registered(const struct cec_adapter *adap)
236{
237 return adap && adap->devnode.registered;
238}
239
Hans Verkuilee7e98712017-04-17 07:54:37 -0300240#define cec_phys_addr_exp(pa) \
241 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
242
Hans Verkuil23111ec2017-06-07 11:46:08 -0300243struct edid;
244
Hans Verkuilf9f314f2017-06-08 15:37:44 -0300245#if IS_REACHABLE(CONFIG_CEC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300246struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
Hans Verkuilf51e8082016-11-25 06:23:34 -0200247 void *priv, const char *name, u32 caps, u8 available_las);
248int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
Hans Verkuila56960e2016-06-25 09:43:58 -0300249void cec_unregister_adapter(struct cec_adapter *adap);
250void cec_delete_adapter(struct cec_adapter *adap);
251
252int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
253 bool block);
254void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
255 bool block);
Hans Verkuil23111ec2017-06-07 11:46:08 -0300256void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
257 const struct edid *edid);
Hans Verkuila56960e2016-06-25 09:43:58 -0300258int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
259 bool block);
260
261/* Called by the adapter */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300262void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
263 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
264 u8 error_cnt, ktime_t ts);
265
266static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
267 u8 arb_lost_cnt, u8 nack_cnt,
268 u8 low_drive_cnt, u8 error_cnt)
269{
270 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
271 low_drive_cnt, error_cnt, ktime_get());
272}
Hans Verkuilc94cdc12017-06-07 11:46:10 -0300273/*
274 * Simplified version of cec_transmit_done for hardware that doesn't retry
275 * failed transmits. So this is always just one attempt in which case
276 * the status is sufficient.
277 */
Hans Verkuil0861ad12017-07-11 03:30:35 -0300278void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
279 u8 status, ktime_t ts);
280
281static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
282 u8 status)
283{
284 cec_transmit_attempt_done_ts(adap, status, ktime_get());
285}
286
287void cec_received_msg_ts(struct cec_adapter *adap,
288 struct cec_msg *msg, ktime_t ts);
289
290static inline void cec_received_msg(struct cec_adapter *adap,
291 struct cec_msg *msg)
292{
293 cec_received_msg_ts(adap, msg, ktime_get());
294}
Hans Verkuila56960e2016-06-25 09:43:58 -0300295
Hans Verkuilee7e98712017-04-17 07:54:37 -0300296/**
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400297 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300298 *
299 * @adap: pointer to the cec adapter
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400300 * @is_high: when true the CEC pin is high, otherwise it is low
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300301 * @ts: the timestamp for this event
302 *
303 */
Hans Verkuil9a6b2a82017-08-15 15:26:25 -0400304void cec_queue_pin_cec_event(struct cec_adapter *adap,
305 bool is_high, ktime_t ts);
Hans Verkuilb8d62f52017-07-11 03:30:41 -0300306
307/**
Hans Verkuil333ef6b2017-08-15 10:07:25 -0400308 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
309 *
310 * @adap: pointer to the cec adapter
311 * @is_high: when true the HPD pin is high, otherwise it is low
312 * @ts: the timestamp for this event
313 *
314 */
315void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
316
317/**
Hans Verkuilee7e98712017-04-17 07:54:37 -0300318 * cec_get_edid_phys_addr() - find and return the physical address
319 *
320 * @edid: pointer to the EDID data
321 * @size: size in bytes of the EDID data
322 * @offset: If not %NULL then the location of the physical address
323 * bytes in the EDID will be returned here. This is set to 0
324 * if there is no physical address found.
325 *
326 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
327 */
328u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
329 unsigned int *offset);
330
331/**
332 * cec_set_edid_phys_addr() - find and set the physical address
333 *
334 * @edid: pointer to the EDID data
335 * @size: size in bytes of the EDID data
336 * @phys_addr: the new physical address
337 *
338 * This function finds the location of the physical address in the EDID
339 * and fills in the given physical address and updates the checksum
340 * at the end of the EDID block. It does nothing if the EDID doesn't
341 * contain a physical address.
342 */
343void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
344
345/**
346 * cec_phys_addr_for_input() - calculate the PA for an input
347 *
348 * @phys_addr: the physical address of the parent
349 * @input: the number of the input port, must be between 1 and 15
350 *
351 * This function calculates a new physical address based on the input
352 * port number. For example:
353 *
354 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
355 *
356 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
357 *
358 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
359 *
360 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
361 *
362 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
363 */
364u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
365
366/**
367 * cec_phys_addr_validate() - validate a physical address from an EDID
368 *
369 * @phys_addr: the physical address to validate
370 * @parent: if not %NULL, then this is filled with the parents PA.
371 * @port: if not %NULL, then this is filled with the input port.
372 *
373 * This validates a physical address as read from an EDID. If the
374 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
375 * then it will return -EINVAL.
376 *
377 * The parent PA is passed into %parent and the input port is passed into
378 * %port. For example:
379 *
380 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
381 *
382 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
383 *
384 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
385 *
386 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
387 *
388 * Return: 0 if the PA is valid, -EINVAL if not.
389 */
390int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
391
Hans Verkuila56960e2016-06-25 09:43:58 -0300392#else
393
Hans Verkuilf51e8082016-11-25 06:23:34 -0200394static inline int cec_register_adapter(struct cec_adapter *adap,
395 struct device *parent)
Hans Verkuila56960e2016-06-25 09:43:58 -0300396{
397 return 0;
398}
399
400static inline void cec_unregister_adapter(struct cec_adapter *adap)
401{
402}
403
404static inline void cec_delete_adapter(struct cec_adapter *adap)
405{
406}
407
408static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
409 bool block)
410{
411}
412
Hans Verkuil23111ec2017-06-07 11:46:08 -0300413static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
414 const struct edid *edid)
415{
416}
417
Hans Verkuilee7e98712017-04-17 07:54:37 -0300418static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
419 unsigned int *offset)
420{
421 if (offset)
422 *offset = 0;
423 return CEC_PHYS_ADDR_INVALID;
424}
425
426static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
427 u16 phys_addr)
428{
429}
430
431static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
432{
433 return CEC_PHYS_ADDR_INVALID;
434}
435
436static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
437{
Hans Verkuil9267d902017-08-31 04:12:53 -0400438 if (parent)
439 *parent = phys_addr;
440 if (port)
441 *port = 0;
Hans Verkuilee7e98712017-04-17 07:54:37 -0300442 return 0;
443}
444
Hans Verkuila56960e2016-06-25 09:43:58 -0300445#endif
446
Hans Verkuil13532782017-06-07 11:46:09 -0300447/**
448 * cec_phys_addr_invalidate() - set the physical address to INVALID
449 *
450 * @adap: the CEC adapter
451 *
452 * This is a simple helper function to invalidate the physical
453 * address.
454 */
455static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
456{
457 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
458}
459
Hans Verkuila56960e2016-06-25 09:43:58 -0300460#endif /* _MEDIA_CEC_H */