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