/* SPDX-License-Identifier: GPL-2.0-only */1/*2* cec - HDMI Consumer Electronics Control support header3*4* Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.5*/67#ifndef _MEDIA_CEC_H8#define _MEDIA_CEC_H910#include <linux/poll.h>11#include <linux/fs.h>12#include <linux/device.h>13#include <linux/cdev.h>14#include <linux/kthread.h>15#include <linux/timer.h>16#include <linux/cec-funcs.h>17#include <media/rc-core.h>1819#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \20CEC_CAP_PASSTHROUGH | CEC_CAP_RC)2122/**23* struct cec_devnode - cec device node24* @dev: cec device25* @cdev: cec character device26* @minor: device node minor number27* @lock: lock to serialize open/release and registration28* @registered: the device was correctly registered29* @unregistered: the device was unregistered30* @lock_fhs: lock to control access to @fhs31* @fhs: the list of open filehandles (cec_fh)32*33* This structure represents a cec-related device node.34*35* To add or remove filehandles from @fhs the @lock must be taken first,36* followed by @lock_fhs. It is safe to access @fhs if either lock is held.37*38* The @parent is a physical device. It must be set by core or device drivers39* before registering the node.40*/41struct cec_devnode {42/* sysfs */43struct device dev;44struct cdev cdev;4546/* device info */47int minor;48/* serialize open/release and registration */49struct mutex lock;50bool registered;51bool unregistered;52/* protect access to fhs */53struct mutex lock_fhs;54struct list_head fhs;55};5657struct cec_adapter;58struct cec_data;59struct cec_pin;60struct cec_notifier;6162struct cec_data {63struct list_head list;64struct list_head xfer_list;65struct cec_adapter *adap;66struct cec_msg msg;67u8 match_len;68u8 match_reply[5];69struct cec_fh *fh;70struct delayed_work work;71struct completion c;72u8 attempts;73bool blocking;74bool completed;75};7677struct cec_msg_entry {78struct list_head list;79struct cec_msg msg;80};8182struct cec_event_entry {83struct list_head list;84struct cec_event ev;85};8687#define CEC_NUM_CORE_EVENTS 288#define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH8990struct cec_fh {91struct list_head list;92struct list_head xfer_list;93struct cec_adapter *adap;94u8 mode_initiator;95u8 mode_follower;9697/* Events */98wait_queue_head_t wait;99struct mutex lock;100struct list_head events[CEC_NUM_EVENTS]; /* queued events */101u16 queued_events[CEC_NUM_EVENTS];102unsigned int total_queued_events;103struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];104struct list_head msgs; /* queued messages */105unsigned int queued_msgs;106};107108#define CEC_SIGNAL_FREE_TIME_RETRY 3109#define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5110#define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7111112/* The nominal data bit period is 2.4 ms */113#define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)114115struct cec_adap_ops {116/* Low-level callbacks, called with adap->lock held */117int (*adap_enable)(struct cec_adapter *adap, bool enable);118int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);119int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);120int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);121void (*adap_unconfigured)(struct cec_adapter *adap);122int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,123u32 signal_free_time, struct cec_msg *msg);124void (*adap_nb_transmit_canceled)(struct cec_adapter *adap,125const struct cec_msg *msg);126void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);127void (*adap_free)(struct cec_adapter *adap);128129/* Error injection callbacks, called without adap->lock held */130int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);131bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);132133/* High-level CEC message callback, called without adap->lock held */134void (*configured)(struct cec_adapter *adap);135int (*received)(struct cec_adapter *adap, struct cec_msg *msg);136};137138/*139* The minimum message length you can receive (excepting poll messages) is 2.140* With a transfer rate of at most 36 bytes per second this makes 18 messages141* per second worst case.142*143* We queue at most 3 seconds worth of received messages. The CEC specification144* requires that messages are replied to within a second, so 3 seconds should145* give more than enough margin. Since most messages are actually more than 2146* bytes, this is in practice a lot more than 3 seconds.147*/148#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)149150/*151* The transmit queue is limited to 1 second worth of messages (worst case).152* Messages can be transmitted by userspace and kernel space. But for both it153* makes no sense to have a lot of messages queued up. One second seems154* reasonable.155*/156#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)157158/**159* struct cec_adapter - cec adapter structure160* @owner: module owner161* @name: name of the CEC adapter162* @devnode: device node for the /dev/cecX device163* @lock: mutex controlling access to this structure164* @rc: remote control device165* @transmit_queue: queue of pending transmits166* @transmit_queue_sz: number of pending transmits167* @wait_queue: queue of transmits waiting for a reply168* @transmitting: CEC messages currently being transmitted169* @transmit_in_progress: true if a transmit is in progress170* @transmit_in_progress_aborted: true if a transmit is in progress is to be171* aborted. This happens if the logical address is172* invalidated while the transmit is ongoing. In that173* case the transmit will finish, but will not retransmit174* and be marked as ABORTED.175* @xfer_timeout_ms: the transfer timeout in ms.176* If 0, then timeout after 2100 ms.177* @kthread_config: kthread used to configure a CEC adapter178* @config_completion: used to signal completion of the config kthread179* @kthread: main CEC processing thread180* @kthread_waitq: main CEC processing wait_queue181* @ops: cec adapter ops182* @priv: cec driver's private data183* @capabilities: cec adapter capabilities184* @available_log_addrs: maximum number of available logical addresses185* @phys_addr: the current physical address186* @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high187* in order to transmit or receive CEC messages. This is usually a HW188* limitation.189* @is_enabled: the CEC adapter is enabled190* @is_claiming_log_addrs: true if cec_claim_log_addrs() is running191* @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)192* @must_reconfigure: while configuring, the PA changed, so reclaim LAs193* @is_configured: the CEC adapter is configured (i.e. has claimed LAs)194* @cec_pin_is_high: if true then the CEC pin is high. Only used with the195* CEC pin framework.196* @adap_controls_phys_addr: if true, then the CEC adapter controls the197* physical address, i.e. the CEC hardware can detect HPD changes and198* read the EDID and is not dependent on an external HDMI driver.199* Drivers that need this can set this field to true after the200* cec_allocate_adapter() call.201* @last_initiator: the initiator of the last transmitted message.202* @monitor_all_cnt: number of filehandles monitoring all msgs203* @monitor_pin_cnt: number of filehandles monitoring pin changes204* @follower_cnt: number of filehandles in follower mode205* @cec_follower: filehandle of the exclusive follower206* @cec_initiator: filehandle of the exclusive initiator207* @passthrough: if true, then the exclusive follower is in208* passthrough mode.209* @log_addrs: current logical addresses210* @conn_info: current connector info211* @tx_timeout_cnt: count the number of Timed Out transmits.212* Reset to 0 when this is reported in cec_adap_status().213* @tx_low_drive_cnt: count the number of Low Drive transmits.214* Reset to 0 when this is reported in cec_adap_status().215* @tx_error_cnt: count the number of Error transmits.216* Reset to 0 when this is reported in cec_adap_status().217* @tx_arb_lost_cnt: count the number of Arb Lost transmits.218* Reset to 0 when this is reported in cec_adap_status().219* @tx_low_drive_log_cnt: number of logged Low Drive transmits since the220* adapter was enabled. Used to avoid flooding the kernel221* log if this happens a lot.222* @tx_error_log_cnt: number of logged Error transmits since the adapter was223* enabled. Used to avoid flooding the kernel log if this224* happens a lot.225* @notifier: CEC notifier226* @pin: CEC pin status struct227* @cec_dir: debugfs cec directory228* @sequence: transmit sequence counter229* @input_phys: remote control input_phys name230*231* This structure represents a cec adapter.232*/233struct cec_adapter {234struct module *owner;235char name[32];236struct cec_devnode devnode;237struct mutex lock;238struct rc_dev *rc;239240struct list_head transmit_queue;241unsigned int transmit_queue_sz;242struct list_head wait_queue;243struct cec_data *transmitting;244bool transmit_in_progress;245bool transmit_in_progress_aborted;246unsigned int xfer_timeout_ms;247248struct task_struct *kthread_config;249struct completion config_completion;250251struct task_struct *kthread;252wait_queue_head_t kthread_waitq;253254const struct cec_adap_ops *ops;255void *priv;256u32 capabilities;257u8 available_log_addrs;258259u16 phys_addr;260bool needs_hpd;261bool is_enabled;262bool is_claiming_log_addrs;263bool is_configuring;264bool must_reconfigure;265bool is_configured;266bool cec_pin_is_high;267bool adap_controls_phys_addr;268u8 last_initiator;269u32 monitor_all_cnt;270u32 monitor_pin_cnt;271u32 follower_cnt;272struct cec_fh *cec_follower;273struct cec_fh *cec_initiator;274bool passthrough;275struct cec_log_addrs log_addrs;276struct cec_connector_info conn_info;277278u32 tx_timeout_cnt;279u32 tx_low_drive_cnt;280u32 tx_error_cnt;281u32 tx_arb_lost_cnt;282u32 tx_low_drive_log_cnt;283u32 tx_error_log_cnt;284285#ifdef CONFIG_CEC_NOTIFIER286struct cec_notifier *notifier;287#endif288#ifdef CONFIG_CEC_PIN289struct cec_pin *pin;290#endif291292struct dentry *cec_dir;293294u32 sequence;295296char input_phys[40];297};298299static inline int cec_get_device(struct cec_adapter *adap)300{301struct cec_devnode *devnode = &adap->devnode;302303/*304* Check if the cec device is available. This needs to be done with305* the devnode->lock held to prevent an open/unregister race:306* without the lock, the device could be unregistered and freed between307* the devnode->registered check and get_device() calls, leading to308* a crash.309*/310mutex_lock(&devnode->lock);311/*312* return ENODEV if the cec device has been removed313* already or if it is not registered anymore.314*/315if (!devnode->registered) {316mutex_unlock(&devnode->lock);317return -ENODEV;318}319/* and increase the device refcount */320get_device(&devnode->dev);321mutex_unlock(&devnode->lock);322return 0;323}324325static inline void cec_put_device(struct cec_adapter *adap)326{327put_device(&adap->devnode.dev);328}329330static inline void *cec_get_drvdata(const struct cec_adapter *adap)331{332return adap->priv;333}334335static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)336{337return adap->log_addrs.log_addr_mask & (1 << log_addr);338}339340static inline bool cec_is_sink(const struct cec_adapter *adap)341{342return adap->phys_addr == 0;343}344345/**346* cec_is_registered() - is the CEC adapter registered?347*348* @adap: the CEC adapter, may be NULL.349*350* Return: true if the adapter is registered, false otherwise.351*/352static inline bool cec_is_registered(const struct cec_adapter *adap)353{354return adap && adap->devnode.registered;355}356357#define cec_phys_addr_exp(pa) \358((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf359360struct edid;361struct drm_connector;362363#if IS_REACHABLE(CONFIG_CEC_CORE)364struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,365void *priv, const char *name, u32 caps, u8 available_las);366int cec_register_adapter(struct cec_adapter *adap, struct device *parent);367void cec_unregister_adapter(struct cec_adapter *adap);368void cec_delete_adapter(struct cec_adapter *adap);369370int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,371bool block);372void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,373bool block);374void cec_s_phys_addr_from_edid(struct cec_adapter *adap,375const struct edid *edid);376void cec_s_conn_info(struct cec_adapter *adap,377const struct cec_connector_info *conn_info);378int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,379bool block);380381/* Called by the adapter */382void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,383u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,384u8 error_cnt, ktime_t ts);385386static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,387u8 arb_lost_cnt, u8 nack_cnt,388u8 low_drive_cnt, u8 error_cnt)389{390cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,391low_drive_cnt, error_cnt, ktime_get());392}393/*394* Simplified version of cec_transmit_done for hardware that doesn't retry395* failed transmits. So this is always just one attempt in which case396* the status is sufficient.397*/398void cec_transmit_attempt_done_ts(struct cec_adapter *adap,399u8 status, ktime_t ts);400401static inline void cec_transmit_attempt_done(struct cec_adapter *adap,402u8 status)403{404cec_transmit_attempt_done_ts(adap, status, ktime_get());405}406407void cec_received_msg_ts(struct cec_adapter *adap,408struct cec_msg *msg, ktime_t ts);409410static inline void cec_received_msg(struct cec_adapter *adap,411struct cec_msg *msg)412{413cec_received_msg_ts(adap, msg, ktime_get());414}415416/**417* cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.418*419* @adap: pointer to the cec adapter420* @is_high: when true the CEC pin is high, otherwise it is low421* @dropped_events: when true some events were dropped422* @ts: the timestamp for this event423*424*/425void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,426bool dropped_events, ktime_t ts);427428/**429* cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.430*431* @adap: pointer to the cec adapter432* @is_high: when true the HPD pin is high, otherwise it is low433* @ts: the timestamp for this event434*435*/436void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);437438/**439* cec_queue_pin_5v_event() - queue a pin event with a given timestamp.440*441* @adap: pointer to the cec adapter442* @is_high: when true the 5V pin is high, otherwise it is low443* @ts: the timestamp for this event444*445*/446void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);447448/**449* cec_get_edid_phys_addr() - find and return the physical address450*451* @edid: pointer to the EDID data452* @size: size in bytes of the EDID data453* @offset: If not %NULL then the location of the physical address454* bytes in the EDID will be returned here. This is set to 0455* if there is no physical address found.456*457* Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.458*/459u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,460unsigned int *offset);461462void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,463const struct drm_connector *connector);464465#else466467static inline int cec_register_adapter(struct cec_adapter *adap,468struct device *parent)469{470return 0;471}472473static inline void cec_unregister_adapter(struct cec_adapter *adap)474{475}476477static inline void cec_delete_adapter(struct cec_adapter *adap)478{479}480481static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,482bool block)483{484}485486static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,487const struct edid *edid)488{489}490491static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,492unsigned int *offset)493{494if (offset)495*offset = 0;496return CEC_PHYS_ADDR_INVALID;497}498499static inline void cec_s_conn_info(struct cec_adapter *adap,500const struct cec_connector_info *conn_info)501{502}503504static inline void505cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,506const struct drm_connector *connector)507{508memset(conn_info, 0, sizeof(*conn_info));509}510511#endif512513/**514* cec_phys_addr_invalidate() - set the physical address to INVALID515*516* @adap: the CEC adapter517*518* This is a simple helper function to invalidate the physical519* address.520*/521static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)522{523cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);524}525526/**527* cec_get_edid_spa_location() - find location of the Source Physical Address528*529* @edid: the EDID530* @size: the size of the EDID531*532* This EDID is expected to be a CEA-861 compliant, which means that there are533* at least two blocks and one or more of the extensions blocks are CEA-861534* blocks.535*536* The returned location is guaranteed to be <= size-2.537*538* This is an inline function since it is used by both CEC and V4L2.539* Ideally this would go in a module shared by both, but it is overkill to do540* that for just a single function.541*/542static inline unsigned int cec_get_edid_spa_location(const u8 *edid,543unsigned int size)544{545unsigned int blocks = size / 128;546unsigned int block;547u8 d;548549/* Sanity check: at least 2 blocks and a multiple of the block size */550if (blocks < 2 || size % 128)551return 0;552553/*554* If there are fewer extension blocks than the size, then update555* 'blocks'. It is allowed to have more extension blocks than the size,556* since some hardware can only read e.g. 256 bytes of the EDID, even557* though more blocks are present. The first CEA-861 extension block558* should normally be in block 1 anyway.559*/560if (edid[0x7e] + 1 < blocks)561blocks = edid[0x7e] + 1;562563for (block = 1; block < blocks; block++) {564unsigned int offset = block * 128;565566/* Skip any non-CEA-861 extension blocks */567if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)568continue;569570/* search Vendor Specific Data Block (tag 3) */571d = edid[offset + 2] & 0x7f;572/* Check if there are Data Blocks */573if (d <= 4)574continue;575if (d > 4) {576unsigned int i = offset + 4;577unsigned int end = offset + d;578579/* Note: 'end' is always < 'size' */580do {581u8 tag = edid[i] >> 5;582u8 len = edid[i] & 0x1f;583584if (tag == 3 && len >= 5 && i + len <= end &&585edid[i + 1] == 0x03 &&586edid[i + 2] == 0x0c &&587edid[i + 3] == 0x00)588return i + 4;589i += len + 1;590} while (i < end);591}592}593return 0;594}595596#endif /* _MEDIA_CEC_H */597598599