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/debugfs.h> 14 #include <linux/device.h> 15 #include <linux/cdev.h> 16 #include <linux/kthread.h> 17 #include <linux/timer.h> 18 #include <linux/cec-funcs.h> 19 #include <media/rc-core.h> 20 #include <media/cec-notifier.h> 21 22 #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \ 23 CEC_CAP_PASSTHROUGH | CEC_CAP_RC) 24 25 /** 26 * struct cec_devnode - cec device node 27 * @dev: cec device 28 * @cdev: cec character device 29 * @minor: device node minor number 30 * @registered: the device was correctly registered 31 * @unregistered: the device was unregistered 32 * @fhs_lock: lock to control access to the filehandle list 33 * @fhs: the list of open filehandles (cec_fh) 34 * 35 * This structure represents a cec-related device node. 36 * 37 * The @parent is a physical device. It must be set by core or device drivers 38 * before registering the node. 39 */ 40 struct cec_devnode { 41 /* sysfs */ 42 struct device dev; 43 struct cdev cdev; 44 45 /* device info */ 46 int minor; 47 bool registered; 48 bool unregistered; 49 struct list_head fhs; 50 struct mutex lock; 51 }; 52 53 struct cec_adapter; 54 struct cec_data; 55 struct cec_pin; 56 57 struct cec_data { 58 struct list_head list; 59 struct list_head xfer_list; 60 struct cec_adapter *adap; 61 struct cec_msg msg; 62 struct cec_fh *fh; 63 struct delayed_work work; 64 struct completion c; 65 u8 attempts; 66 bool new_initiator; 67 bool blocking; 68 bool completed; 69 }; 70 71 struct cec_msg_entry { 72 struct list_head list; 73 struct cec_msg msg; 74 }; 75 76 struct cec_event_entry { 77 struct list_head list; 78 struct cec_event ev; 79 }; 80 81 #define CEC_NUM_CORE_EVENTS 2 82 #define CEC_NUM_EVENTS CEC_EVENT_PIN_HPD_HIGH 83 84 struct cec_fh { 85 struct list_head list; 86 struct list_head xfer_list; 87 struct cec_adapter *adap; 88 u8 mode_initiator; 89 u8 mode_follower; 90 91 /* Events */ 92 wait_queue_head_t wait; 93 struct mutex lock; 94 struct list_head events[CEC_NUM_EVENTS]; /* queued events */ 95 u8 queued_events[CEC_NUM_EVENTS]; 96 unsigned int total_queued_events; 97 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS]; 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 109 struct 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_monitor_pin_enable)(struct cec_adapter *adap, bool enable); 114 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr); 115 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts, 116 u32 signal_free_time, struct cec_msg *msg); 117 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file); 118 void (*adap_free)(struct cec_adapter *adap); 119 120 /* High-level CEC message callback */ 121 int (*received)(struct cec_adapter *adap, struct cec_msg *msg); 122 }; 123 124 /* 125 * The minimum message length you can receive (excepting poll messages) is 2. 126 * With a transfer rate of at most 36 bytes per second this makes 18 messages 127 * per second worst case. 128 * 129 * We queue at most 3 seconds worth of received messages. The CEC specification 130 * requires that messages are replied to within a second, so 3 seconds should 131 * give more than enough margin. Since most messages are actually more than 2 132 * bytes, this is in practice a lot more than 3 seconds. 133 */ 134 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3) 135 136 /* 137 * The transmit queue is limited to 1 second worth of messages (worst case). 138 * Messages can be transmitted by userspace and kernel space. But for both it 139 * makes no sense to have a lot of messages queued up. One second seems 140 * reasonable. 141 */ 142 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1) 143 144 struct cec_adapter { 145 struct module *owner; 146 char name[32]; 147 struct cec_devnode devnode; 148 struct mutex lock; 149 struct rc_dev *rc; 150 151 struct list_head transmit_queue; 152 unsigned int transmit_queue_sz; 153 struct list_head wait_queue; 154 struct cec_data *transmitting; 155 156 struct task_struct *kthread_config; 157 struct completion config_completion; 158 159 struct task_struct *kthread; 160 wait_queue_head_t kthread_waitq; 161 wait_queue_head_t waitq; 162 163 const struct cec_adap_ops *ops; 164 void *priv; 165 u32 capabilities; 166 u8 available_log_addrs; 167 168 u16 phys_addr; 169 bool needs_hpd; 170 bool is_configuring; 171 bool is_configured; 172 bool cec_pin_is_high; 173 u32 monitor_all_cnt; 174 u32 monitor_pin_cnt; 175 u32 follower_cnt; 176 struct cec_fh *cec_follower; 177 struct cec_fh *cec_initiator; 178 bool passthrough; 179 struct cec_log_addrs log_addrs; 180 181 u32 tx_timeouts; 182 183 #ifdef CONFIG_CEC_NOTIFIER 184 struct cec_notifier *notifier; 185 #endif 186 #ifdef CONFIG_CEC_PIN 187 struct cec_pin *pin; 188 #endif 189 190 struct dentry *cec_dir; 191 struct dentry *status_file; 192 193 u16 phys_addrs[15]; 194 u32 sequence; 195 196 char device_name[32]; 197 char input_phys[32]; 198 char input_drv[32]; 199 }; 200 201 static inline void *cec_get_drvdata(const struct cec_adapter *adap) 202 { 203 return adap->priv; 204 } 205 206 static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr) 207 { 208 return adap->log_addrs.log_addr_mask & (1 << log_addr); 209 } 210 211 static inline bool cec_is_sink(const struct cec_adapter *adap) 212 { 213 return adap->phys_addr == 0; 214 } 215 216 /** 217 * cec_is_registered() - is the CEC adapter registered? 218 * 219 * @adap: the CEC adapter, may be NULL. 220 * 221 * Return: true if the adapter is registered, false otherwise. 222 */ 223 static inline bool cec_is_registered(const struct cec_adapter *adap) 224 { 225 return adap && adap->devnode.registered; 226 } 227 228 #define cec_phys_addr_exp(pa) \ 229 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf 230 231 struct edid; 232 233 #if IS_REACHABLE(CONFIG_CEC_CORE) 234 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops, 235 void *priv, const char *name, u32 caps, u8 available_las); 236 int cec_register_adapter(struct cec_adapter *adap, struct device *parent); 237 void cec_unregister_adapter(struct cec_adapter *adap); 238 void cec_delete_adapter(struct cec_adapter *adap); 239 240 int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs, 241 bool block); 242 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, 243 bool block); 244 void cec_s_phys_addr_from_edid(struct cec_adapter *adap, 245 const struct edid *edid); 246 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg, 247 bool block); 248 249 /* Called by the adapter */ 250 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status, 251 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt, 252 u8 error_cnt, ktime_t ts); 253 254 static inline void cec_transmit_done(struct cec_adapter *adap, u8 status, 255 u8 arb_lost_cnt, u8 nack_cnt, 256 u8 low_drive_cnt, u8 error_cnt) 257 { 258 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt, 259 low_drive_cnt, error_cnt, ktime_get()); 260 } 261 /* 262 * Simplified version of cec_transmit_done for hardware that doesn't retry 263 * failed transmits. So this is always just one attempt in which case 264 * the status is sufficient. 265 */ 266 void cec_transmit_attempt_done_ts(struct cec_adapter *adap, 267 u8 status, ktime_t ts); 268 269 static inline void cec_transmit_attempt_done(struct cec_adapter *adap, 270 u8 status) 271 { 272 cec_transmit_attempt_done_ts(adap, status, ktime_get()); 273 } 274 275 void cec_received_msg_ts(struct cec_adapter *adap, 276 struct cec_msg *msg, ktime_t ts); 277 278 static inline void cec_received_msg(struct cec_adapter *adap, 279 struct cec_msg *msg) 280 { 281 cec_received_msg_ts(adap, msg, ktime_get()); 282 } 283 284 /** 285 * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp. 286 * 287 * @adap: pointer to the cec adapter 288 * @is_high: when true the CEC pin is high, otherwise it is low 289 * @ts: the timestamp for this event 290 * 291 */ 292 void cec_queue_pin_cec_event(struct cec_adapter *adap, 293 bool is_high, ktime_t ts); 294 295 /** 296 * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp. 297 * 298 * @adap: pointer to the cec adapter 299 * @is_high: when true the HPD pin is high, otherwise it is low 300 * @ts: the timestamp for this event 301 * 302 */ 303 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts); 304 305 /** 306 * cec_get_edid_phys_addr() - find and return the physical address 307 * 308 * @edid: pointer to the EDID data 309 * @size: size in bytes of the EDID data 310 * @offset: If not %NULL then the location of the physical address 311 * bytes in the EDID will be returned here. This is set to 0 312 * if there is no physical address found. 313 * 314 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none. 315 */ 316 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size, 317 unsigned int *offset); 318 319 /** 320 * cec_set_edid_phys_addr() - find and set the physical address 321 * 322 * @edid: pointer to the EDID data 323 * @size: size in bytes of the EDID data 324 * @phys_addr: the new physical address 325 * 326 * This function finds the location of the physical address in the EDID 327 * and fills in the given physical address and updates the checksum 328 * at the end of the EDID block. It does nothing if the EDID doesn't 329 * contain a physical address. 330 */ 331 void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr); 332 333 /** 334 * cec_phys_addr_for_input() - calculate the PA for an input 335 * 336 * @phys_addr: the physical address of the parent 337 * @input: the number of the input port, must be between 1 and 15 338 * 339 * This function calculates a new physical address based on the input 340 * port number. For example: 341 * 342 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0 343 * 344 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0 345 * 346 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5 347 * 348 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth. 349 * 350 * Return: the new physical address or CEC_PHYS_ADDR_INVALID. 351 */ 352 u16 cec_phys_addr_for_input(u16 phys_addr, u8 input); 353 354 /** 355 * cec_phys_addr_validate() - validate a physical address from an EDID 356 * 357 * @phys_addr: the physical address to validate 358 * @parent: if not %NULL, then this is filled with the parents PA. 359 * @port: if not %NULL, then this is filled with the input port. 360 * 361 * This validates a physical address as read from an EDID. If the 362 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end), 363 * then it will return -EINVAL. 364 * 365 * The parent PA is passed into %parent and the input port is passed into 366 * %port. For example: 367 * 368 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0. 369 * 370 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1. 371 * 372 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2. 373 * 374 * PA = f.f.f.f: has parent f.f.f.f and input port 0. 375 * 376 * Return: 0 if the PA is valid, -EINVAL if not. 377 */ 378 int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port); 379 380 #else 381 382 static inline int cec_register_adapter(struct cec_adapter *adap, 383 struct device *parent) 384 { 385 return 0; 386 } 387 388 static inline void cec_unregister_adapter(struct cec_adapter *adap) 389 { 390 } 391 392 static inline void cec_delete_adapter(struct cec_adapter *adap) 393 { 394 } 395 396 static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, 397 bool block) 398 { 399 } 400 401 static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap, 402 const struct edid *edid) 403 { 404 } 405 406 static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size, 407 unsigned int *offset) 408 { 409 if (offset) 410 *offset = 0; 411 return CEC_PHYS_ADDR_INVALID; 412 } 413 414 static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size, 415 u16 phys_addr) 416 { 417 } 418 419 static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input) 420 { 421 return CEC_PHYS_ADDR_INVALID; 422 } 423 424 static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port) 425 { 426 if (parent) 427 *parent = phys_addr; 428 if (port) 429 *port = 0; 430 return 0; 431 } 432 433 #endif 434 435 /** 436 * cec_phys_addr_invalidate() - set the physical address to INVALID 437 * 438 * @adap: the CEC adapter 439 * 440 * This is a simple helper function to invalidate the physical 441 * address. 442 */ 443 static inline void cec_phys_addr_invalidate(struct cec_adapter *adap) 444 { 445 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false); 446 } 447 448 #endif /* _MEDIA_CEC_H */ 449