]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/media/cec.h
regulator: Fix return value of _set_load() stub
[mirror_ubuntu-bionic-kernel.git] / include / media / cec.h
CommitLineData
a56960e8
HV
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>
e3a93adc 32#include <media/cec-notifier.h>
a56960e8 33
ee0c503e
HV
34#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
35 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
36
a56960e8
HV
37/**
38 * struct cec_devnode - cec device node
39 * @dev: cec device
40 * @cdev: cec character device
a56960e8
HV
41 * @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;
a56960e8
HV
56
57 /* device info */
58 int minor;
59 bool registered;
60 bool unregistered;
a56960e8 61 struct list_head fhs;
62148f09 62 struct mutex lock;
a56960e8
HV
63};
64
65struct cec_adapter;
66struct cec_data;
ea5c8ef2 67struct cec_pin;
a56960e8
HV
68
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
6b2bbb08
HV
88struct cec_event_entry {
89 struct list_head list;
90 struct cec_event ev;
91};
92
93#define CEC_NUM_CORE_EVENTS 2
333ef6bd 94#define CEC_NUM_EVENTS CEC_EVENT_PIN_HPD_HIGH
a56960e8
HV
95
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;
a56960e8 105 struct mutex lock;
6b2bbb08
HV
106 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];
a56960e8
HV
110 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);
125 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
126 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
127 u32 signal_free_time, struct cec_msg *msg);
128 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
e6259b5f 129 void (*adap_free)(struct cec_adapter *adap);
a56960e8
HV
130
131 /* High-level CEC message callback */
132 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
133};
134
135/*
136 * The minimum message length you can receive (excepting poll messages) is 2.
137 * With a transfer rate of at most 36 bytes per second this makes 18 messages
138 * per second worst case.
139 *
11065f85
HV
140 * We queue at most 3 seconds worth of received messages. The CEC specification
141 * requires that messages are replied to within a second, so 3 seconds should
142 * give more than enough margin. Since most messages are actually more than 2
143 * bytes, this is in practice a lot more than 3 seconds.
a56960e8 144 */
11065f85
HV
145#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
146
147/*
148 * The transmit queue is limited to 1 second worth of messages (worst case).
149 * Messages can be transmitted by userspace and kernel space. But for both it
150 * makes no sense to have a lot of messages queued up. One second seems
151 * reasonable.
152 */
153#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
a56960e8
HV
154
155struct cec_adapter {
156 struct module *owner;
157 char name[32];
158 struct cec_devnode devnode;
159 struct mutex lock;
160 struct rc_dev *rc;
161
162 struct list_head transmit_queue;
11065f85 163 unsigned int transmit_queue_sz;
a56960e8
HV
164 struct list_head wait_queue;
165 struct cec_data *transmitting;
835bef74 166 bool transmit_in_progress;
a56960e8
HV
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;
f902c1e9 181 bool needs_hpd;
a56960e8
HV
182 bool is_configuring;
183 bool is_configured;
28e11b15 184 bool cec_pin_is_high;
a56960e8 185 u32 monitor_all_cnt;
b8d62f50 186 u32 monitor_pin_cnt;
a56960e8
HV
187 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
bb789e03
HV
193 u32 tx_timeouts;
194
a9a249a2
HV
195#ifdef CONFIG_MEDIA_CEC_RC
196 bool rc_repeating;
197 int rc_last_scancode;
198 u64 rc_last_keypress;
199#endif
e94c3281 200#ifdef CONFIG_CEC_NOTIFIER
e3a93adc
HV
201 struct cec_notifier *notifier;
202#endif
ea5c8ef2
HV
203#ifdef CONFIG_CEC_PIN
204 struct cec_pin *pin;
205#endif
e3a93adc 206
a56960e8
HV
207 struct dentry *cec_dir;
208 struct dentry *status_file;
209
210 u16 phys_addrs[15];
211 u32 sequence;
212
518f4b26 213 char device_name[32];
a56960e8
HV
214 char input_phys[32];
215 char input_drv[32];
216};
217
0b0b97dc
JA
218static inline void *cec_get_drvdata(const struct cec_adapter *adap)
219{
220 return adap->priv;
221}
222
a56960e8
HV
223static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
224{
225 return adap->log_addrs.log_addr_mask & (1 << log_addr);
226}
227
228static inline bool cec_is_sink(const struct cec_adapter *adap)
229{
230 return adap->phys_addr == 0;
231}
232
ee7e9871
HV
233#define cec_phys_addr_exp(pa) \
234 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
235
23111ec3
HV
236struct edid;
237
f9f314f3 238#if IS_REACHABLE(CONFIG_CEC_CORE)
a56960e8 239struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
f51e8080
HV
240 void *priv, const char *name, u32 caps, u8 available_las);
241int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
a56960e8
HV
242void cec_unregister_adapter(struct cec_adapter *adap);
243void cec_delete_adapter(struct cec_adapter *adap);
244
245int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
246 bool block);
247void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
248 bool block);
23111ec3
HV
249void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
250 const struct edid *edid);
a56960e8
HV
251int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
252 bool block);
253
254/* Called by the adapter */
0861ad14
HV
255void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
256 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
257 u8 error_cnt, ktime_t ts);
258
259static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
260 u8 arb_lost_cnt, u8 nack_cnt,
261 u8 low_drive_cnt, u8 error_cnt)
262{
263 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
264 low_drive_cnt, error_cnt, ktime_get());
265}
c94cdc1e
HV
266/*
267 * Simplified version of cec_transmit_done for hardware that doesn't retry
268 * failed transmits. So this is always just one attempt in which case
269 * the status is sufficient.
270 */
0861ad14
HV
271void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
272 u8 status, ktime_t ts);
273
274static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
275 u8 status)
276{
277 cec_transmit_attempt_done_ts(adap, status, ktime_get());
278}
279
280void cec_received_msg_ts(struct cec_adapter *adap,
281 struct cec_msg *msg, ktime_t ts);
282
283static inline void cec_received_msg(struct cec_adapter *adap,
284 struct cec_msg *msg)
285{
286 cec_received_msg_ts(adap, msg, ktime_get());
287}
a56960e8 288
b8d62f50 289/**
9a6b2a87 290 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
b8d62f50
HV
291 *
292 * @adap: pointer to the cec adapter
9a6b2a87 293 * @is_high: when true the CEC pin is high, otherwise it is low
b8d62f50
HV
294 * @ts: the timestamp for this event
295 *
296 */
9a6b2a87
HV
297void cec_queue_pin_cec_event(struct cec_adapter *adap,
298 bool is_high, ktime_t ts);
b8d62f50 299
333ef6bd
HV
300/**
301 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
302 *
303 * @adap: pointer to the cec adapter
304 * @is_high: when true the HPD pin is high, otherwise it is low
305 * @ts: the timestamp for this event
306 *
307 */
308void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
309
ee7e9871
HV
310/**
311 * cec_get_edid_phys_addr() - find and return the physical address
312 *
313 * @edid: pointer to the EDID data
314 * @size: size in bytes of the EDID data
315 * @offset: If not %NULL then the location of the physical address
316 * bytes in the EDID will be returned here. This is set to 0
317 * if there is no physical address found.
318 *
319 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
320 */
321u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
322 unsigned int *offset);
323
324/**
325 * cec_set_edid_phys_addr() - find and set the physical address
326 *
327 * @edid: pointer to the EDID data
328 * @size: size in bytes of the EDID data
329 * @phys_addr: the new physical address
330 *
331 * This function finds the location of the physical address in the EDID
332 * and fills in the given physical address and updates the checksum
333 * at the end of the EDID block. It does nothing if the EDID doesn't
334 * contain a physical address.
335 */
336void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
337
338/**
339 * cec_phys_addr_for_input() - calculate the PA for an input
340 *
341 * @phys_addr: the physical address of the parent
342 * @input: the number of the input port, must be between 1 and 15
343 *
344 * This function calculates a new physical address based on the input
345 * port number. For example:
346 *
347 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
348 *
349 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
350 *
351 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
352 *
353 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
354 *
355 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
356 */
357u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
358
359/**
360 * cec_phys_addr_validate() - validate a physical address from an EDID
361 *
362 * @phys_addr: the physical address to validate
363 * @parent: if not %NULL, then this is filled with the parents PA.
364 * @port: if not %NULL, then this is filled with the input port.
365 *
366 * This validates a physical address as read from an EDID. If the
367 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
368 * then it will return -EINVAL.
369 *
370 * The parent PA is passed into %parent and the input port is passed into
371 * %port. For example:
372 *
373 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
374 *
375 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
376 *
377 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
378 *
379 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
380 *
381 * Return: 0 if the PA is valid, -EINVAL if not.
382 */
383int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
384
a56960e8
HV
385#else
386
f51e8080
HV
387static inline int cec_register_adapter(struct cec_adapter *adap,
388 struct device *parent)
a56960e8
HV
389{
390 return 0;
391}
392
393static inline void cec_unregister_adapter(struct cec_adapter *adap)
394{
395}
396
397static inline void cec_delete_adapter(struct cec_adapter *adap)
398{
399}
400
401static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
402 bool block)
403{
404}
405
23111ec3
HV
406static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
407 const struct edid *edid)
408{
409}
410
ee7e9871
HV
411static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
412 unsigned int *offset)
413{
414 if (offset)
415 *offset = 0;
416 return CEC_PHYS_ADDR_INVALID;
417}
418
419static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
420 u16 phys_addr)
421{
422}
423
424static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
425{
426 return CEC_PHYS_ADDR_INVALID;
427}
428
429static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
430{
9267d90c
HV
431 if (parent)
432 *parent = phys_addr;
433 if (port)
434 *port = 0;
ee7e9871
HV
435 return 0;
436}
437
a56960e8
HV
438#endif
439
13532789
HV
440/**
441 * cec_phys_addr_invalidate() - set the physical address to INVALID
442 *
443 * @adap: the CEC adapter
444 *
445 * This is a simple helper function to invalidate the physical
446 * address.
447 */
448static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
449{
450 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
451}
452
ce0ace31
HV
453/**
454 * cec_get_edid_spa_location() - find location of the Source Physical Address
455 *
456 * @edid: the EDID
457 * @size: the size of the EDID
458 *
459 * This EDID is expected to be a CEA-861 compliant, which means that there are
460 * at least two blocks and one or more of the extensions blocks are CEA-861
461 * blocks.
462 *
463 * The returned location is guaranteed to be <= size-2.
464 *
465 * This is an inline function since it is used by both CEC and V4L2.
466 * Ideally this would go in a module shared by both, but it is overkill to do
467 * that for just a single function.
468 */
469static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
470 unsigned int size)
471{
472 unsigned int blocks = size / 128;
473 unsigned int block;
474 u8 d;
475
476 /* Sanity check: at least 2 blocks and a multiple of the block size */
477 if (blocks < 2 || size % 128)
478 return 0;
479
480 /*
481 * If there are fewer extension blocks than the size, then update
482 * 'blocks'. It is allowed to have more extension blocks than the size,
483 * since some hardware can only read e.g. 256 bytes of the EDID, even
484 * though more blocks are present. The first CEA-861 extension block
485 * should normally be in block 1 anyway.
486 */
487 if (edid[0x7e] + 1 < blocks)
488 blocks = edid[0x7e] + 1;
489
490 for (block = 1; block < blocks; block++) {
491 unsigned int offset = block * 128;
492
493 /* Skip any non-CEA-861 extension blocks */
494 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
495 continue;
496
497 /* search Vendor Specific Data Block (tag 3) */
498 d = edid[offset + 2] & 0x7f;
499 /* Check if there are Data Blocks */
500 if (d <= 4)
501 continue;
502 if (d > 4) {
503 unsigned int i = offset + 4;
504 unsigned int end = offset + d;
505
506 /* Note: 'end' is always < 'size' */
507 do {
508 u8 tag = edid[i] >> 5;
509 u8 len = edid[i] & 0x1f;
510
511 if (tag == 3 && len >= 5 && i + len <= end &&
512 edid[i + 1] == 0x03 &&
513 edid[i + 2] == 0x0c &&
514 edid[i + 3] == 0x00)
515 return i + 4;
516 i += len + 1;
517 } while (i < end);
518 }
519 }
520 return 0;
521}
522
a56960e8 523#endif /* _MEDIA_CEC_H */