0c8e86115b6f5c913a24e0ab6798d3ca90d819ca
[linux-2.6-block.git] / include / media / cec.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * cec - HDMI Consumer Electronics Control support header
4  *
5  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7
8 #ifndef _MEDIA_CEC_H
9 #define _MEDIA_CEC_H
10
11 #include <linux/poll.h>
12 #include <linux/fs.h>
13 #include <linux/device.h>
14 #include <linux/cdev.h>
15 #include <linux/kthread.h>
16 #include <linux/timer.h>
17 #include <linux/cec-funcs.h>
18 #include <media/rc-core.h>
19
20 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
21                           CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
22
23 /**
24  * struct cec_devnode - cec device node
25  * @dev:        cec device
26  * @cdev:       cec character device
27  * @minor:      device node minor number
28  * @lock:       lock to serialize open/release and registration
29  * @registered: the device was correctly registered
30  * @unregistered: the device was unregistered
31  * @lock_fhs:   lock to control access to @fhs
32  * @fhs:        the list of open filehandles (cec_fh)
33  *
34  * This structure represents a cec-related device node.
35  *
36  * To add or remove filehandles from @fhs the @lock must be taken first,
37  * followed by @lock_fhs. It is safe to access @fhs if either lock is held.
38  *
39  * The @parent is a physical device. It must be set by core or device drivers
40  * before registering the node.
41  */
42 struct cec_devnode {
43         /* sysfs */
44         struct device dev;
45         struct cdev cdev;
46
47         /* device info */
48         int minor;
49         /* serialize open/release and registration */
50         struct mutex lock;
51         bool registered;
52         bool unregistered;
53         /* protect access to fhs */
54         struct mutex lock_fhs;
55         struct list_head fhs;
56 };
57
58 struct cec_adapter;
59 struct cec_data;
60 struct cec_pin;
61 struct cec_notifier;
62
63 struct cec_data {
64         struct list_head list;
65         struct list_head xfer_list;
66         struct cec_adapter *adap;
67         struct cec_msg msg;
68         u8 match_len;
69         u8 match_reply[5];
70         struct cec_fh *fh;
71         struct delayed_work work;
72         struct completion c;
73         u8 attempts;
74         bool blocking;
75         bool completed;
76 };
77
78 struct cec_msg_entry {
79         struct list_head        list;
80         struct cec_msg          msg;
81 };
82
83 struct cec_event_entry {
84         struct list_head        list;
85         struct cec_event        ev;
86 };
87
88 #define CEC_NUM_CORE_EVENTS 2
89 #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
90
91 struct 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         struct mutex            lock;
101         struct list_head        events[CEC_NUM_EVENTS]; /* queued events */
102         u16                     queued_events[CEC_NUM_EVENTS];
103         unsigned int            total_queued_events;
104         struct cec_event_entry  core_events[CEC_NUM_CORE_EVENTS];
105         struct list_head        msgs; /* queued messages */
106         unsigned int            queued_msgs;
107 };
108
109 #define CEC_SIGNAL_FREE_TIME_RETRY              3
110 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR      5
111 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER          7
112
113 /* The nominal data bit period is 2.4 ms */
114 #define CEC_FREE_TIME_TO_USEC(ft)               ((ft) * 2400)
115
116 struct cec_adap_ops {
117         /* Low-level callbacks, called with adap->lock held */
118         int (*adap_enable)(struct cec_adapter *adap, bool enable);
119         int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
120         int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
121         int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
122         void (*adap_unconfigured)(struct cec_adapter *adap);
123         int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
124                              u32 signal_free_time, struct cec_msg *msg);
125         void (*adap_nb_transmit_canceled)(struct cec_adapter *adap,
126                                           const struct cec_msg *msg);
127         void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
128         void (*adap_free)(struct cec_adapter *adap);
129
130         /* Error injection callbacks, called without adap->lock held */
131         int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
132         bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
133
134         /* High-level CEC message callback, called without adap->lock held */
135         void (*configured)(struct cec_adapter *adap);
136         int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
137 };
138
139 /*
140  * The minimum message length you can receive (excepting poll messages) is 2.
141  * With a transfer rate of at most 36 bytes per second this makes 18 messages
142  * per second worst case.
143  *
144  * We queue at most 3 seconds worth of received messages. The CEC specification
145  * requires that messages are replied to within a second, so 3 seconds should
146  * give more than enough margin. Since most messages are actually more than 2
147  * bytes, this is in practice a lot more than 3 seconds.
148  */
149 #define CEC_MAX_MSG_RX_QUEUE_SZ         (18 * 3)
150
151 /*
152  * The transmit queue is limited to 1 second worth of messages (worst case).
153  * Messages can be transmitted by userspace and kernel space. But for both it
154  * makes no sense to have a lot of messages queued up. One second seems
155  * reasonable.
156  */
157 #define CEC_MAX_MSG_TX_QUEUE_SZ         (18 * 1)
158
159 /**
160  * struct cec_adapter - cec adapter structure
161  * @owner:              module owner
162  * @name:               name of the CEC adapter
163  * @devnode:            device node for the /dev/cecX device
164  * @lock:               mutex controlling access to this structure
165  * @rc:                 remote control device
166  * @transmit_queue:     queue of pending transmits
167  * @transmit_queue_sz:  number of pending transmits
168  * @wait_queue:         queue of transmits waiting for a reply
169  * @transmitting:       CEC messages currently being transmitted
170  * @transmit_in_progress: true if a transmit is in progress
171  * @transmit_in_progress_aborted: true if a transmit is in progress is to be
172  *                      aborted. This happens if the logical address is
173  *                      invalidated while the transmit is ongoing. In that
174  *                      case the transmit will finish, but will not retransmit
175  *                      and be marked as ABORTED.
176  * @xfer_timeout_ms:    the transfer timeout in ms.
177  *                      If 0, then timeout after 2100 ms.
178  * @kthread_config:     kthread used to configure a CEC adapter
179  * @config_completion:  used to signal completion of the config kthread
180  * @kthread:            main CEC processing thread
181  * @kthread_waitq:      main CEC processing wait_queue
182  * @ops:                cec adapter ops
183  * @priv:               cec driver's private data
184  * @capabilities:       cec adapter capabilities
185  * @available_log_addrs: maximum number of available logical addresses
186  * @phys_addr:          the current physical address
187  * @needs_hpd:          if true, then the HDMI HotPlug Detect pin must be high
188  *      in order to transmit or receive CEC messages. This is usually a HW
189  *      limitation.
190  * @is_enabled:         the CEC adapter is enabled
191  * @is_claiming_log_addrs:  true if cec_claim_log_addrs() is running
192  * @is_configuring:     the CEC adapter is configuring (i.e. claiming LAs)
193  * @must_reconfigure:   while configuring, the PA changed, so reclaim LAs
194  * @is_configured:      the CEC adapter is configured (i.e. has claimed LAs)
195  * @cec_pin_is_high:    if true then the CEC pin is high. Only used with the
196  *      CEC pin framework.
197  * @adap_controls_phys_addr: if true, then the CEC adapter controls the
198  *      physical address, i.e. the CEC hardware can detect HPD changes and
199  *      read the EDID and is not dependent on an external HDMI driver.
200  *      Drivers that need this can set this field to true after the
201  *      cec_allocate_adapter() call.
202  * @last_initiator:     the initiator of the last transmitted message.
203  * @monitor_all_cnt:    number of filehandles monitoring all msgs
204  * @monitor_pin_cnt:    number of filehandles monitoring pin changes
205  * @follower_cnt:       number of filehandles in follower mode
206  * @cec_follower:       filehandle of the exclusive follower
207  * @cec_initiator:      filehandle of the exclusive initiator
208  * @passthrough:        if true, then the exclusive follower is in
209  *      passthrough mode.
210  * @log_addrs:          current logical addresses
211  * @conn_info:          current connector info
212  * @tx_timeout_cnt:     count the number of Timed Out transmits.
213  *                      Reset to 0 when this is reported in cec_adap_status().
214  * @tx_low_drive_cnt:   count the number of Low Drive transmits.
215  *                      Reset to 0 when this is reported in cec_adap_status().
216  * @tx_error_cnt:       count the number of Error transmits.
217  *                      Reset to 0 when this is reported in cec_adap_status().
218  * @tx_arb_lost_cnt:    count the number of Arb Lost transmits.
219  *                      Reset to 0 when this is reported in cec_adap_status().
220  * @tx_low_drive_log_cnt: number of logged Low Drive transmits since the
221  *                      adapter was enabled. Used to avoid flooding the kernel
222  *                      log if this happens a lot.
223  * @tx_error_log_cnt:   number of logged Error transmits since the adapter was
224  *                      enabled. Used to avoid flooding the kernel log if this
225  *                      happens a lot.
226  * @notifier:           CEC notifier
227  * @pin:                CEC pin status struct
228  * @cec_dir:            debugfs cec directory
229  * @sequence:           transmit sequence counter
230  * @input_phys:         remote control input_phys name
231  *
232  * This structure represents a cec adapter.
233  */
234 struct cec_adapter {
235         struct module *owner;
236         char name[32];
237         struct cec_devnode devnode;
238         struct mutex lock;
239         struct rc_dev *rc;
240
241         struct list_head transmit_queue;
242         unsigned int transmit_queue_sz;
243         struct list_head wait_queue;
244         struct cec_data *transmitting;
245         bool transmit_in_progress;
246         bool transmit_in_progress_aborted;
247         unsigned int xfer_timeout_ms;
248
249         struct task_struct *kthread_config;
250         struct completion config_completion;
251
252         struct task_struct *kthread;
253         wait_queue_head_t kthread_waitq;
254
255         const struct cec_adap_ops *ops;
256         void *priv;
257         u32 capabilities;
258         u8 available_log_addrs;
259
260         u16 phys_addr;
261         bool needs_hpd;
262         bool is_enabled;
263         bool is_claiming_log_addrs;
264         bool is_configuring;
265         bool must_reconfigure;
266         bool is_configured;
267         bool cec_pin_is_high;
268         bool adap_controls_phys_addr;
269         u8 last_initiator;
270         u32 monitor_all_cnt;
271         u32 monitor_pin_cnt;
272         u32 follower_cnt;
273         struct cec_fh *cec_follower;
274         struct cec_fh *cec_initiator;
275         bool passthrough;
276         struct cec_log_addrs log_addrs;
277         struct cec_connector_info conn_info;
278
279         u32 tx_timeout_cnt;
280         u32 tx_low_drive_cnt;
281         u32 tx_error_cnt;
282         u32 tx_arb_lost_cnt;
283         u32 tx_low_drive_log_cnt;
284         u32 tx_error_log_cnt;
285
286 #ifdef CONFIG_CEC_NOTIFIER
287         struct cec_notifier *notifier;
288 #endif
289 #ifdef CONFIG_CEC_PIN
290         struct cec_pin *pin;
291 #endif
292
293         struct dentry *cec_dir;
294
295         u32 sequence;
296
297         char input_phys[40];
298 };
299
300 static inline int cec_get_device(struct cec_adapter *adap)
301 {
302         struct cec_devnode *devnode = &adap->devnode;
303
304         /*
305          * Check if the cec device is available. This needs to be done with
306          * the devnode->lock held to prevent an open/unregister race:
307          * without the lock, the device could be unregistered and freed between
308          * the devnode->registered check and get_device() calls, leading to
309          * a crash.
310          */
311         mutex_lock(&devnode->lock);
312         /*
313          * return ENODEV if the cec device has been removed
314          * already or if it is not registered anymore.
315          */
316         if (!devnode->registered) {
317                 mutex_unlock(&devnode->lock);
318                 return -ENODEV;
319         }
320         /* and increase the device refcount */
321         get_device(&devnode->dev);
322         mutex_unlock(&devnode->lock);
323         return 0;
324 }
325
326 static inline void cec_put_device(struct cec_adapter *adap)
327 {
328         put_device(&adap->devnode.dev);
329 }
330
331 static inline void *cec_get_drvdata(const struct cec_adapter *adap)
332 {
333         return adap->priv;
334 }
335
336 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
337 {
338         return adap->log_addrs.log_addr_mask & (1 << log_addr);
339 }
340
341 static inline bool cec_is_sink(const struct cec_adapter *adap)
342 {
343         return adap->phys_addr == 0;
344 }
345
346 /**
347  * cec_is_registered() - is the CEC adapter registered?
348  *
349  * @adap:       the CEC adapter, may be NULL.
350  *
351  * Return: true if the adapter is registered, false otherwise.
352  */
353 static inline bool cec_is_registered(const struct cec_adapter *adap)
354 {
355         return adap && adap->devnode.registered;
356 }
357
358 #define cec_phys_addr_exp(pa) \
359         ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
360
361 struct edid;
362 struct drm_connector;
363
364 #if IS_REACHABLE(CONFIG_CEC_CORE)
365 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
366                 void *priv, const char *name, u32 caps, u8 available_las);
367 int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
368 void cec_unregister_adapter(struct cec_adapter *adap);
369 void cec_delete_adapter(struct cec_adapter *adap);
370
371 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
372                     bool block);
373 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
374                      bool block);
375 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
376                                const struct edid *edid);
377 void cec_s_conn_info(struct cec_adapter *adap,
378                      const struct cec_connector_info *conn_info);
379 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
380                      bool block);
381
382 /* Called by the adapter */
383 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
384                           u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
385                           u8 error_cnt, ktime_t ts);
386
387 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
388                                      u8 arb_lost_cnt, u8 nack_cnt,
389                                      u8 low_drive_cnt, u8 error_cnt)
390 {
391         cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
392                              low_drive_cnt, error_cnt, ktime_get());
393 }
394 /*
395  * Simplified version of cec_transmit_done for hardware that doesn't retry
396  * failed transmits. So this is always just one attempt in which case
397  * the status is sufficient.
398  */
399 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
400                                   u8 status, ktime_t ts);
401
402 static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
403                                              u8 status)
404 {
405         cec_transmit_attempt_done_ts(adap, status, ktime_get());
406 }
407
408 void cec_received_msg_ts(struct cec_adapter *adap,
409                          struct cec_msg *msg, ktime_t ts);
410
411 static inline void cec_received_msg(struct cec_adapter *adap,
412                                     struct cec_msg *msg)
413 {
414         cec_received_msg_ts(adap, msg, ktime_get());
415 }
416
417 /**
418  * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
419  *
420  * @adap:       pointer to the cec adapter
421  * @is_high:    when true the CEC pin is high, otherwise it is low
422  * @dropped_events: when true some events were dropped
423  * @ts:         the timestamp for this event
424  *
425  */
426 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
427                              bool dropped_events, ktime_t ts);
428
429 /**
430  * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
431  *
432  * @adap:       pointer to the cec adapter
433  * @is_high:    when true the HPD pin is high, otherwise it is low
434  * @ts:         the timestamp for this event
435  *
436  */
437 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
438
439 /**
440  * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
441  *
442  * @adap:       pointer to the cec adapter
443  * @is_high:    when true the 5V pin is high, otherwise it is low
444  * @ts:         the timestamp for this event
445  *
446  */
447 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
448
449 /**
450  * cec_get_edid_phys_addr() - find and return the physical address
451  *
452  * @edid:       pointer to the EDID data
453  * @size:       size in bytes of the EDID data
454  * @offset:     If not %NULL then the location of the physical address
455  *              bytes in the EDID will be returned here. This is set to 0
456  *              if there is no physical address found.
457  *
458  * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
459  */
460 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
461                            unsigned int *offset);
462
463 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
464                                  const struct drm_connector *connector);
465
466 #else
467
468 static inline int cec_register_adapter(struct cec_adapter *adap,
469                                        struct device *parent)
470 {
471         return 0;
472 }
473
474 static inline void cec_unregister_adapter(struct cec_adapter *adap)
475 {
476 }
477
478 static inline void cec_delete_adapter(struct cec_adapter *adap)
479 {
480 }
481
482 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
483                                    bool block)
484 {
485 }
486
487 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
488                                              const struct edid *edid)
489 {
490 }
491
492 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
493                                          unsigned int *offset)
494 {
495         if (offset)
496                 *offset = 0;
497         return CEC_PHYS_ADDR_INVALID;
498 }
499
500 static inline void cec_s_conn_info(struct cec_adapter *adap,
501                                    const struct cec_connector_info *conn_info)
502 {
503 }
504
505 static inline void
506 cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
507                             const struct drm_connector *connector)
508 {
509         memset(conn_info, 0, sizeof(*conn_info));
510 }
511
512 #endif
513
514 /**
515  * cec_phys_addr_invalidate() - set the physical address to INVALID
516  *
517  * @adap:       the CEC adapter
518  *
519  * This is a simple helper function to invalidate the physical
520  * address.
521  */
522 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
523 {
524         cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
525 }
526
527 /**
528  * cec_get_edid_spa_location() - find location of the Source Physical Address
529  *
530  * @edid: the EDID
531  * @size: the size of the EDID
532  *
533  * This EDID is expected to be a CEA-861 compliant, which means that there are
534  * at least two blocks and one or more of the extensions blocks are CEA-861
535  * blocks.
536  *
537  * The returned location is guaranteed to be <= size-2.
538  *
539  * This is an inline function since it is used by both CEC and V4L2.
540  * Ideally this would go in a module shared by both, but it is overkill to do
541  * that for just a single function.
542  */
543 static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
544                                                      unsigned int size)
545 {
546         unsigned int blocks = size / 128;
547         unsigned int block;
548         u8 d;
549
550         /* Sanity check: at least 2 blocks and a multiple of the block size */
551         if (blocks < 2 || size % 128)
552                 return 0;
553
554         /*
555          * If there are fewer extension blocks than the size, then update
556          * 'blocks'. It is allowed to have more extension blocks than the size,
557          * since some hardware can only read e.g. 256 bytes of the EDID, even
558          * though more blocks are present. The first CEA-861 extension block
559          * should normally be in block 1 anyway.
560          */
561         if (edid[0x7e] + 1 < blocks)
562                 blocks = edid[0x7e] + 1;
563
564         for (block = 1; block < blocks; block++) {
565                 unsigned int offset = block * 128;
566
567                 /* Skip any non-CEA-861 extension blocks */
568                 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
569                         continue;
570
571                 /* search Vendor Specific Data Block (tag 3) */
572                 d = edid[offset + 2] & 0x7f;
573                 /* Check if there are Data Blocks */
574                 if (d <= 4)
575                         continue;
576                 if (d > 4) {
577                         unsigned int i = offset + 4;
578                         unsigned int end = offset + d;
579
580                         /* Note: 'end' is always < 'size' */
581                         do {
582                                 u8 tag = edid[i] >> 5;
583                                 u8 len = edid[i] & 0x1f;
584
585                                 if (tag == 3 && len >= 5 && i + len <= end &&
586                                     edid[i + 1] == 0x03 &&
587                                     edid[i + 2] == 0x0c &&
588                                     edid[i + 3] == 0x00)
589                                         return i + 4;
590                                 i += len + 1;
591                         } while (i < end);
592                 }
593         }
594         return 0;
595 }
596
597 #endif /* _MEDIA_CEC_H */