1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter 4 * 5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 6 */ 7 8 #include <linux/errno.h> 9 #include <linux/init.h> 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/kmod.h> 13 #include <linux/ktime.h> 14 #include <linux/slab.h> 15 #include <linux/mm.h> 16 #include <linux/string.h> 17 #include <linux/types.h> 18 19 #include <drm/drm_connector.h> 20 #include <drm/drm_device.h> 21 #include <drm/drm_edid.h> 22 #include <drm/drm_file.h> 23 24 #include "cec-priv.h" 25 26 static void cec_fill_msg_report_features(struct cec_adapter *adap, 27 struct cec_msg *msg, 28 unsigned int la_idx); 29 30 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr) 31 { 32 int i; 33 34 for (i = 0; i < adap->log_addrs.num_log_addrs; i++) 35 if (adap->log_addrs.log_addr[i] == log_addr) 36 return i; 37 return -1; 38 } 39 40 static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr) 41 { 42 int i = cec_log_addr2idx(adap, log_addr); 43 44 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i]; 45 } 46 47 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size, 48 unsigned int *offset) 49 { 50 unsigned int loc = cec_get_edid_spa_location(edid, size); 51 52 if (offset) 53 *offset = loc; 54 if (loc == 0) 55 return CEC_PHYS_ADDR_INVALID; 56 return (edid[loc] << 8) | edid[loc + 1]; 57 } 58 EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr); 59 60 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info, 61 const struct drm_connector *connector) 62 { 63 memset(conn_info, 0, sizeof(*conn_info)); 64 conn_info->type = CEC_CONNECTOR_TYPE_DRM; 65 conn_info->drm.card_no = connector->dev->primary->index; 66 conn_info->drm.connector_id = connector->base.id; 67 } 68 EXPORT_SYMBOL_GPL(cec_fill_conn_info_from_drm); 69 70 /* 71 * Queue a new event for this filehandle. If ts == 0, then set it 72 * to the current time. 73 * 74 * We keep a queue of at most max_event events where max_event differs 75 * per event. If the queue becomes full, then drop the oldest event and 76 * keep track of how many events we've dropped. 77 */ 78 void cec_queue_event_fh(struct cec_fh *fh, 79 const struct cec_event *new_ev, u64 ts) 80 { 81 static const u16 max_events[CEC_NUM_EVENTS] = { 82 1, 1, 800, 800, 8, 8, 8, 8 83 }; 84 struct cec_event_entry *entry; 85 unsigned int ev_idx = new_ev->event - 1; 86 87 if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events))) 88 return; 89 90 if (ts == 0) 91 ts = ktime_get_ns(); 92 93 mutex_lock(&fh->lock); 94 if (ev_idx < CEC_NUM_CORE_EVENTS) 95 entry = &fh->core_events[ev_idx]; 96 else 97 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 98 if (entry) { 99 if (new_ev->event == CEC_EVENT_LOST_MSGS && 100 fh->queued_events[ev_idx]) { 101 entry->ev.lost_msgs.lost_msgs += 102 new_ev->lost_msgs.lost_msgs; 103 goto unlock; 104 } 105 entry->ev = *new_ev; 106 entry->ev.ts = ts; 107 108 if (fh->queued_events[ev_idx] < max_events[ev_idx]) { 109 /* Add new msg at the end of the queue */ 110 list_add_tail(&entry->list, &fh->events[ev_idx]); 111 fh->queued_events[ev_idx]++; 112 fh->total_queued_events++; 113 goto unlock; 114 } 115 116 if (ev_idx >= CEC_NUM_CORE_EVENTS) { 117 list_add_tail(&entry->list, &fh->events[ev_idx]); 118 /* drop the oldest event */ 119 entry = list_first_entry(&fh->events[ev_idx], 120 struct cec_event_entry, list); 121 list_del(&entry->list); 122 kfree(entry); 123 } 124 } 125 /* Mark that events were lost */ 126 entry = list_first_entry_or_null(&fh->events[ev_idx], 127 struct cec_event_entry, list); 128 if (entry) 129 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS; 130 131 unlock: 132 mutex_unlock(&fh->lock); 133 wake_up_interruptible(&fh->wait); 134 } 135 136 /* Queue a new event for all open filehandles. */ 137 static void cec_queue_event(struct cec_adapter *adap, 138 const struct cec_event *ev) 139 { 140 u64 ts = ktime_get_ns(); 141 struct cec_fh *fh; 142 143 mutex_lock(&adap->devnode.lock_fhs); 144 list_for_each_entry(fh, &adap->devnode.fhs, list) 145 cec_queue_event_fh(fh, ev, ts); 146 mutex_unlock(&adap->devnode.lock_fhs); 147 } 148 149 /* Notify userspace that the CEC pin changed state at the given time. */ 150 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high, 151 bool dropped_events, ktime_t ts) 152 { 153 struct cec_event ev = { 154 .event = is_high ? CEC_EVENT_PIN_CEC_HIGH : 155 CEC_EVENT_PIN_CEC_LOW, 156 .flags = dropped_events ? CEC_EVENT_FL_DROPPED_EVENTS : 0, 157 }; 158 struct cec_fh *fh; 159 160 mutex_lock(&adap->devnode.lock_fhs); 161 list_for_each_entry(fh, &adap->devnode.fhs, list) { 162 if (fh->mode_follower == CEC_MODE_MONITOR_PIN) 163 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts)); 164 } 165 mutex_unlock(&adap->devnode.lock_fhs); 166 } 167 EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event); 168 169 /* Notify userspace that the HPD pin changed state at the given time. */ 170 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts) 171 { 172 struct cec_event ev = { 173 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH : 174 CEC_EVENT_PIN_HPD_LOW, 175 }; 176 struct cec_fh *fh; 177 178 mutex_lock(&adap->devnode.lock_fhs); 179 list_for_each_entry(fh, &adap->devnode.fhs, list) 180 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts)); 181 mutex_unlock(&adap->devnode.lock_fhs); 182 } 183 EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event); 184 185 /* Notify userspace that the 5V pin changed state at the given time. */ 186 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts) 187 { 188 struct cec_event ev = { 189 .event = is_high ? CEC_EVENT_PIN_5V_HIGH : 190 CEC_EVENT_PIN_5V_LOW, 191 }; 192 struct cec_fh *fh; 193 194 mutex_lock(&adap->devnode.lock_fhs); 195 list_for_each_entry(fh, &adap->devnode.fhs, list) 196 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts)); 197 mutex_unlock(&adap->devnode.lock_fhs); 198 } 199 EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event); 200 201 /* 202 * Queue a new message for this filehandle. 203 * 204 * We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the 205 * queue becomes full, then drop the oldest message and keep track 206 * of how many messages we've dropped. 207 */ 208 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg) 209 { 210 static const struct cec_event ev_lost_msgs = { 211 .event = CEC_EVENT_LOST_MSGS, 212 .flags = 0, 213 { 214 .lost_msgs = { 1 }, 215 }, 216 }; 217 struct cec_msg_entry *entry; 218 219 mutex_lock(&fh->lock); 220 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 221 if (entry) { 222 entry->msg = *msg; 223 /* Add new msg at the end of the queue */ 224 list_add_tail(&entry->list, &fh->msgs); 225 226 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) { 227 /* All is fine if there is enough room */ 228 fh->queued_msgs++; 229 mutex_unlock(&fh->lock); 230 wake_up_interruptible(&fh->wait); 231 return; 232 } 233 234 /* 235 * if the message queue is full, then drop the oldest one and 236 * send a lost message event. 237 */ 238 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list); 239 list_del(&entry->list); 240 kfree(entry); 241 } 242 mutex_unlock(&fh->lock); 243 244 /* 245 * We lost a message, either because kmalloc failed or the queue 246 * was full. 247 */ 248 cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns()); 249 } 250 251 /* 252 * Queue the message for those filehandles that are in monitor mode. 253 * If valid_la is true (this message is for us or was sent by us), 254 * then pass it on to any monitoring filehandle. If this message 255 * isn't for us or from us, then only give it to filehandles that 256 * are in MONITOR_ALL mode. 257 * 258 * This can only happen if the CEC_CAP_MONITOR_ALL capability is 259 * set and the CEC adapter was placed in 'monitor all' mode. 260 */ 261 static void cec_queue_msg_monitor(struct cec_adapter *adap, 262 const struct cec_msg *msg, 263 bool valid_la) 264 { 265 struct cec_fh *fh; 266 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR : 267 CEC_MODE_MONITOR_ALL; 268 269 mutex_lock(&adap->devnode.lock_fhs); 270 list_for_each_entry(fh, &adap->devnode.fhs, list) { 271 if (fh->mode_follower >= monitor_mode) 272 cec_queue_msg_fh(fh, msg); 273 } 274 mutex_unlock(&adap->devnode.lock_fhs); 275 } 276 277 /* 278 * Queue the message for follower filehandles. 279 */ 280 static void cec_queue_msg_followers(struct cec_adapter *adap, 281 const struct cec_msg *msg) 282 { 283 struct cec_fh *fh; 284 285 mutex_lock(&adap->devnode.lock_fhs); 286 list_for_each_entry(fh, &adap->devnode.fhs, list) { 287 if (fh->mode_follower == CEC_MODE_FOLLOWER) 288 cec_queue_msg_fh(fh, msg); 289 } 290 mutex_unlock(&adap->devnode.lock_fhs); 291 } 292 293 /* Notify userspace of an adapter state change. */ 294 static void cec_post_state_event(struct cec_adapter *adap) 295 { 296 struct cec_event ev = { 297 .event = CEC_EVENT_STATE_CHANGE, 298 }; 299 300 ev.state_change.phys_addr = adap->phys_addr; 301 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask; 302 ev.state_change.have_conn_info = 303 adap->conn_info.type != CEC_CONNECTOR_TYPE_NO_CONNECTOR; 304 cec_queue_event(adap, &ev); 305 } 306 307 /* 308 * A CEC transmit (and a possible wait for reply) completed. 309 * If this was in blocking mode, then complete it, otherwise 310 * queue the message for userspace to dequeue later. 311 * 312 * This function is called with adap->lock held. 313 */ 314 static void cec_data_completed(struct cec_data *data) 315 { 316 /* 317 * Delete this transmit from the filehandle's xfer_list since 318 * we're done with it. 319 * 320 * Note that if the filehandle is closed before this transmit 321 * finished, then the release() function will set data->fh to NULL. 322 * Without that we would be referring to a closed filehandle. 323 */ 324 if (data->fh) 325 list_del_init(&data->xfer_list); 326 327 if (data->blocking) { 328 /* 329 * Someone is blocking so mark the message as completed 330 * and call complete. 331 */ 332 data->completed = true; 333 complete(&data->c); 334 } else { 335 /* 336 * No blocking, so just queue the message if needed and 337 * free the memory. 338 */ 339 if (data->fh) 340 cec_queue_msg_fh(data->fh, &data->msg); 341 kfree(data); 342 } 343 } 344 345 /* 346 * A pending CEC transmit needs to be cancelled, either because the CEC 347 * adapter is disabled or the transmit takes an impossibly long time to 348 * finish, or the reply timed out. 349 * 350 * This function is called with adap->lock held. 351 */ 352 static void cec_data_cancel(struct cec_data *data, u8 tx_status, u8 rx_status) 353 { 354 struct cec_adapter *adap = data->adap; 355 356 /* 357 * It's either the current transmit, or it is a pending 358 * transmit. Take the appropriate action to clear it. 359 */ 360 if (adap->transmitting == data) { 361 adap->transmitting = NULL; 362 } else { 363 list_del_init(&data->list); 364 if (!(data->msg.tx_status & CEC_TX_STATUS_OK)) 365 if (!WARN_ON(!adap->transmit_queue_sz)) 366 adap->transmit_queue_sz--; 367 } 368 369 if (data->msg.tx_status & CEC_TX_STATUS_OK) { 370 data->msg.rx_ts = ktime_get_ns(); 371 data->msg.rx_status = rx_status; 372 if (!data->blocking) 373 data->msg.tx_status = 0; 374 } else { 375 data->msg.tx_ts = ktime_get_ns(); 376 data->msg.tx_status |= tx_status | 377 CEC_TX_STATUS_MAX_RETRIES; 378 data->msg.tx_error_cnt++; 379 data->attempts = 0; 380 if (!data->blocking) 381 data->msg.rx_status = 0; 382 } 383 384 /* Queue transmitted message for monitoring purposes */ 385 cec_queue_msg_monitor(adap, &data->msg, 1); 386 387 if (!data->blocking && data->msg.sequence) 388 /* Allow drivers to react to a canceled transmit */ 389 call_void_op(adap, adap_nb_transmit_canceled, &data->msg); 390 391 cec_data_completed(data); 392 } 393 394 /* 395 * Flush all pending transmits and cancel any pending timeout work. 396 * 397 * This function is called with adap->lock held. 398 */ 399 static void cec_flush(struct cec_adapter *adap) 400 { 401 struct cec_data *data, *n; 402 403 /* 404 * If the adapter is disabled, or we're asked to stop, 405 * then cancel any pending transmits. 406 */ 407 while (!list_empty(&adap->transmit_queue)) { 408 data = list_first_entry(&adap->transmit_queue, 409 struct cec_data, list); 410 cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0); 411 } 412 if (adap->transmitting) 413 adap->transmit_in_progress_aborted = true; 414 415 /* Cancel the pending timeout work. */ 416 list_for_each_entry_safe(data, n, &adap->wait_queue, list) { 417 if (cancel_delayed_work(&data->work)) 418 cec_data_cancel(data, CEC_TX_STATUS_OK, CEC_RX_STATUS_ABORTED); 419 /* 420 * If cancel_delayed_work returned false, then 421 * the cec_wait_timeout function is running, 422 * which will call cec_data_completed. So no 423 * need to do anything special in that case. 424 */ 425 } 426 /* 427 * If something went wrong and this counter isn't what it should 428 * be, then this will reset it back to 0. Warn if it is not 0, 429 * since it indicates a bug, either in this framework or in a 430 * CEC driver. 431 */ 432 if (WARN_ON(adap->transmit_queue_sz)) 433 adap->transmit_queue_sz = 0; 434 } 435 436 /* 437 * Main CEC state machine 438 * 439 * Wait until the thread should be stopped, or we are not transmitting and 440 * a new transmit message is queued up, in which case we start transmitting 441 * that message. When the adapter finished transmitting the message it will 442 * call cec_transmit_done(). 443 * 444 * If the adapter is disabled, then remove all queued messages instead. 445 * 446 * If the current transmit times out, then cancel that transmit. 447 */ 448 int cec_thread_func(void *_adap) 449 { 450 struct cec_adapter *adap = _adap; 451 452 for (;;) { 453 unsigned int signal_free_time; 454 struct cec_data *data; 455 bool timeout = false; 456 u8 attempts; 457 458 if (adap->transmit_in_progress) { 459 int err; 460 461 /* 462 * We are transmitting a message, so add a timeout 463 * to prevent the state machine to get stuck waiting 464 * for this message to finalize and add a check to 465 * see if the adapter is disabled in which case the 466 * transmit should be canceled. 467 */ 468 err = wait_event_interruptible_timeout(adap->kthread_waitq, 469 (adap->needs_hpd && 470 (!adap->is_configured && !adap->is_configuring)) || 471 kthread_should_stop() || 472 (!adap->transmit_in_progress && 473 !list_empty(&adap->transmit_queue)), 474 msecs_to_jiffies(adap->xfer_timeout_ms)); 475 timeout = err == 0; 476 } else { 477 /* Otherwise we just wait for something to happen. */ 478 wait_event_interruptible(adap->kthread_waitq, 479 kthread_should_stop() || 480 (!adap->transmit_in_progress && 481 !list_empty(&adap->transmit_queue))); 482 } 483 484 mutex_lock(&adap->lock); 485 486 if ((adap->needs_hpd && 487 (!adap->is_configured && !adap->is_configuring)) || 488 kthread_should_stop()) { 489 cec_flush(adap); 490 goto unlock; 491 } 492 493 if (adap->transmit_in_progress && 494 adap->transmit_in_progress_aborted) { 495 if (adap->transmitting) 496 cec_data_cancel(adap->transmitting, 497 CEC_TX_STATUS_ABORTED, 0); 498 adap->transmit_in_progress = false; 499 adap->transmit_in_progress_aborted = false; 500 goto unlock; 501 } 502 if (adap->transmit_in_progress && timeout) { 503 /* 504 * If we timeout, then log that. Normally this does 505 * not happen and it is an indication of a faulty CEC 506 * adapter driver, or the CEC bus is in some weird 507 * state. On rare occasions it can happen if there is 508 * so much traffic on the bus that the adapter was 509 * unable to transmit for xfer_timeout_ms (2.1s by 510 * default). 511 */ 512 if (adap->transmitting) { 513 pr_warn("cec-%s: message %*ph timed out\n", adap->name, 514 adap->transmitting->msg.len, 515 adap->transmitting->msg.msg); 516 /* Just give up on this. */ 517 cec_data_cancel(adap->transmitting, 518 CEC_TX_STATUS_TIMEOUT, 0); 519 } else { 520 pr_warn("cec-%s: transmit timed out\n", adap->name); 521 } 522 adap->transmit_in_progress = false; 523 adap->tx_timeout_cnt++; 524 goto unlock; 525 } 526 527 /* 528 * If we are still transmitting, or there is nothing new to 529 * transmit, then just continue waiting. 530 */ 531 if (adap->transmit_in_progress || list_empty(&adap->transmit_queue)) 532 goto unlock; 533 534 /* Get a new message to transmit */ 535 data = list_first_entry(&adap->transmit_queue, 536 struct cec_data, list); 537 list_del_init(&data->list); 538 if (!WARN_ON(!data->adap->transmit_queue_sz)) 539 adap->transmit_queue_sz--; 540 541 /* Make this the current transmitting message */ 542 adap->transmitting = data; 543 544 /* 545 * Suggested number of attempts as per the CEC 2.0 spec: 546 * 4 attempts is the default, except for 'secondary poll 547 * messages', i.e. poll messages not sent during the adapter 548 * configuration phase when it allocates logical addresses. 549 */ 550 if (data->msg.len == 1 && adap->is_configured) 551 attempts = 2; 552 else 553 attempts = 4; 554 555 /* Set the suggested signal free time */ 556 if (data->attempts) { 557 /* should be >= 3 data bit periods for a retry */ 558 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY; 559 } else if (adap->last_initiator != 560 cec_msg_initiator(&data->msg)) { 561 /* should be >= 5 data bit periods for new initiator */ 562 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR; 563 adap->last_initiator = cec_msg_initiator(&data->msg); 564 } else { 565 /* 566 * should be >= 7 data bit periods for sending another 567 * frame immediately after another. 568 */ 569 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER; 570 } 571 if (data->attempts == 0) 572 data->attempts = attempts; 573 574 adap->transmit_in_progress_aborted = false; 575 /* Tell the adapter to transmit, cancel on error */ 576 if (call_op(adap, adap_transmit, data->attempts, 577 signal_free_time, &data->msg)) 578 cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0); 579 else 580 adap->transmit_in_progress = true; 581 582 unlock: 583 mutex_unlock(&adap->lock); 584 585 if (kthread_should_stop()) 586 break; 587 } 588 return 0; 589 } 590 591 /* 592 * Called by the CEC adapter if a transmit finished. 593 */ 594 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status, 595 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt, 596 u8 error_cnt, ktime_t ts) 597 { 598 struct cec_data *data; 599 struct cec_msg *msg; 600 unsigned int attempts_made = arb_lost_cnt + nack_cnt + 601 low_drive_cnt + error_cnt; 602 bool done = status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK); 603 bool aborted = adap->transmit_in_progress_aborted; 604 605 dprintk(2, "%s: status 0x%02x\n", __func__, status); 606 if (attempts_made < 1) 607 attempts_made = 1; 608 609 mutex_lock(&adap->lock); 610 data = adap->transmitting; 611 if (!data) { 612 /* 613 * This might happen if a transmit was issued and the cable is 614 * unplugged while the transmit is ongoing. Ignore this 615 * transmit in that case. 616 */ 617 if (!adap->transmit_in_progress) 618 dprintk(1, "%s was called without an ongoing transmit!\n", 619 __func__); 620 adap->transmit_in_progress = false; 621 goto wake_thread; 622 } 623 adap->transmit_in_progress = false; 624 adap->transmit_in_progress_aborted = false; 625 626 msg = &data->msg; 627 628 /* Drivers must fill in the status! */ 629 WARN_ON(status == 0); 630 msg->tx_ts = ktime_to_ns(ts); 631 msg->tx_status |= status; 632 msg->tx_arb_lost_cnt += arb_lost_cnt; 633 msg->tx_nack_cnt += nack_cnt; 634 msg->tx_low_drive_cnt += low_drive_cnt; 635 msg->tx_error_cnt += error_cnt; 636 637 adap->tx_arb_lost_cnt += arb_lost_cnt; 638 adap->tx_low_drive_cnt += low_drive_cnt; 639 adap->tx_error_cnt += error_cnt; 640 641 /* 642 * Low Drive transmission errors should really not happen for 643 * well-behaved CEC devices and proper HDMI cables. 644 * 645 * Ditto for the 'Error' status. 646 * 647 * For the first few times that this happens, log this. 648 * Stop logging after that, since that will not add any more 649 * useful information and instead it will just flood the kernel log. 650 */ 651 if (done && adap->tx_low_drive_log_cnt < 8 && msg->tx_low_drive_cnt) { 652 adap->tx_low_drive_log_cnt++; 653 dprintk(0, "low drive counter: %u (seq %u: %*ph)\n", 654 msg->tx_low_drive_cnt, msg->sequence, 655 msg->len, msg->msg); 656 } 657 if (done && adap->tx_error_log_cnt < 8 && msg->tx_error_cnt) { 658 adap->tx_error_log_cnt++; 659 dprintk(0, "error counter: %u (seq %u: %*ph)\n", 660 msg->tx_error_cnt, msg->sequence, 661 msg->len, msg->msg); 662 } 663 664 /* Mark that we're done with this transmit */ 665 adap->transmitting = NULL; 666 667 /* 668 * If there are still retry attempts left and there was an error and 669 * the hardware didn't signal that it retried itself (by setting 670 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves. 671 */ 672 if (!aborted && data->attempts > attempts_made && !done) { 673 /* Retry this message */ 674 data->attempts -= attempts_made; 675 if (msg->timeout) 676 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n", 677 msg->len, msg->msg, data->attempts, msg->reply); 678 else 679 dprintk(2, "retransmit: %*ph (attempts: %d)\n", 680 msg->len, msg->msg, data->attempts); 681 /* Add the message in front of the transmit queue */ 682 list_add(&data->list, &adap->transmit_queue); 683 adap->transmit_queue_sz++; 684 goto wake_thread; 685 } 686 687 if (aborted && !done) 688 status |= CEC_TX_STATUS_ABORTED; 689 data->attempts = 0; 690 691 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */ 692 if (!(status & CEC_TX_STATUS_OK)) 693 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES; 694 695 /* Queue transmitted message for monitoring purposes */ 696 cec_queue_msg_monitor(adap, msg, 1); 697 698 if ((status & CEC_TX_STATUS_OK) && adap->is_configured && 699 msg->timeout) { 700 /* 701 * Queue the message into the wait queue if we want to wait 702 * for a reply. 703 */ 704 list_add_tail(&data->list, &adap->wait_queue); 705 schedule_delayed_work(&data->work, 706 msecs_to_jiffies(msg->timeout)); 707 } else { 708 /* Otherwise we're done */ 709 cec_data_completed(data); 710 } 711 712 wake_thread: 713 /* 714 * Wake up the main thread to see if another message is ready 715 * for transmitting or to retry the current message. 716 */ 717 wake_up_interruptible(&adap->kthread_waitq); 718 mutex_unlock(&adap->lock); 719 } 720 EXPORT_SYMBOL_GPL(cec_transmit_done_ts); 721 722 void cec_transmit_attempt_done_ts(struct cec_adapter *adap, 723 u8 status, ktime_t ts) 724 { 725 switch (status & ~CEC_TX_STATUS_MAX_RETRIES) { 726 case CEC_TX_STATUS_OK: 727 cec_transmit_done_ts(adap, status, 0, 0, 0, 0, ts); 728 return; 729 case CEC_TX_STATUS_ARB_LOST: 730 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts); 731 return; 732 case CEC_TX_STATUS_NACK: 733 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts); 734 return; 735 case CEC_TX_STATUS_LOW_DRIVE: 736 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts); 737 return; 738 case CEC_TX_STATUS_ERROR: 739 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts); 740 return; 741 default: 742 /* Should never happen */ 743 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status); 744 return; 745 } 746 } 747 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts); 748 749 /* 750 * Called when waiting for a reply times out. 751 */ 752 static void cec_wait_timeout(struct work_struct *work) 753 { 754 struct cec_data *data = container_of(work, struct cec_data, work.work); 755 struct cec_adapter *adap = data->adap; 756 757 mutex_lock(&adap->lock); 758 /* 759 * Sanity check in case the timeout and the arrival of the message 760 * happened at the same time. 761 */ 762 if (list_empty(&data->list)) 763 goto unlock; 764 765 /* Mark the message as timed out */ 766 list_del_init(&data->list); 767 cec_data_cancel(data, CEC_TX_STATUS_OK, CEC_RX_STATUS_TIMEOUT); 768 unlock: 769 mutex_unlock(&adap->lock); 770 } 771 772 /* 773 * Transmit a message. The fh argument may be NULL if the transmit is not 774 * associated with a specific filehandle. 775 * 776 * This function is called with adap->lock held. 777 */ 778 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg, 779 struct cec_fh *fh, bool block) 780 { 781 struct cec_data *data; 782 bool is_raw = msg_is_raw(msg); 783 int err; 784 785 if (adap->devnode.unregistered) 786 return -ENODEV; 787 788 msg->rx_ts = 0; 789 msg->tx_ts = 0; 790 msg->rx_status = 0; 791 msg->tx_status = 0; 792 msg->tx_arb_lost_cnt = 0; 793 msg->tx_nack_cnt = 0; 794 msg->tx_low_drive_cnt = 0; 795 msg->tx_error_cnt = 0; 796 msg->sequence = 0; 797 798 if (msg->reply && msg->timeout == 0) { 799 /* Make sure the timeout isn't 0. */ 800 msg->timeout = 1000; 801 } 802 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS | CEC_MSG_FL_RAW; 803 804 if (!msg->timeout) 805 msg->flags &= ~CEC_MSG_FL_REPLY_TO_FOLLOWERS; 806 807 /* Sanity checks */ 808 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) { 809 dprintk(1, "%s: invalid length %d\n", __func__, msg->len); 810 return -EINVAL; 811 } 812 813 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len); 814 815 if (msg->timeout) 816 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n", 817 __func__, msg->len, msg->msg, msg->reply, 818 !block ? ", nb" : ""); 819 else 820 dprintk(2, "%s: %*ph%s\n", 821 __func__, msg->len, msg->msg, !block ? " (nb)" : ""); 822 823 if (msg->timeout && msg->len == 1) { 824 dprintk(1, "%s: can't reply to poll msg\n", __func__); 825 return -EINVAL; 826 } 827 828 if (is_raw) { 829 if (!capable(CAP_SYS_RAWIO)) 830 return -EPERM; 831 } else { 832 /* A CDC-Only device can only send CDC messages */ 833 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) && 834 (msg->len == 1 || msg->msg[1] != CEC_MSG_CDC_MESSAGE)) { 835 dprintk(1, "%s: not a CDC message\n", __func__); 836 return -EINVAL; 837 } 838 839 if (msg->len >= 4 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) { 840 msg->msg[2] = adap->phys_addr >> 8; 841 msg->msg[3] = adap->phys_addr & 0xff; 842 } 843 844 if (msg->len == 1) { 845 if (cec_msg_destination(msg) == 0xf) { 846 dprintk(1, "%s: invalid poll message\n", 847 __func__); 848 return -EINVAL; 849 } 850 if (cec_has_log_addr(adap, cec_msg_destination(msg))) { 851 /* 852 * If the destination is a logical address our 853 * adapter has already claimed, then just NACK 854 * this. It depends on the hardware what it will 855 * do with a POLL to itself (some OK this), so 856 * it is just as easy to handle it here so the 857 * behavior will be consistent. 858 */ 859 msg->tx_ts = ktime_get_ns(); 860 msg->tx_status = CEC_TX_STATUS_NACK | 861 CEC_TX_STATUS_MAX_RETRIES; 862 msg->tx_nack_cnt = 1; 863 msg->sequence = ++adap->sequence; 864 if (!msg->sequence) 865 msg->sequence = ++adap->sequence; 866 return 0; 867 } 868 } 869 if (msg->len > 1 && !cec_msg_is_broadcast(msg) && 870 cec_has_log_addr(adap, cec_msg_destination(msg))) { 871 dprintk(1, "%s: destination is the adapter itself\n", 872 __func__); 873 return -EINVAL; 874 } 875 if (msg->len > 1 && adap->is_configured && 876 !cec_has_log_addr(adap, cec_msg_initiator(msg))) { 877 dprintk(1, "%s: initiator has unknown logical address %d\n", 878 __func__, cec_msg_initiator(msg)); 879 return -EINVAL; 880 } 881 /* 882 * Special case: allow Ping and IMAGE/TEXT_VIEW_ON to be 883 * transmitted to a TV, even if the adapter is unconfigured. 884 * This makes it possible to detect or wake up displays that 885 * pull down the HPD when in standby. 886 */ 887 if (!adap->is_configured && !adap->is_configuring && 888 (msg->len > 2 || 889 cec_msg_destination(msg) != CEC_LOG_ADDR_TV || 890 (msg->len == 2 && msg->msg[1] != CEC_MSG_IMAGE_VIEW_ON && 891 msg->msg[1] != CEC_MSG_TEXT_VIEW_ON))) { 892 dprintk(1, "%s: adapter is unconfigured\n", __func__); 893 return -ENONET; 894 } 895 } 896 897 if (!adap->is_configured && !adap->is_configuring) { 898 if (adap->needs_hpd) { 899 dprintk(1, "%s: adapter is unconfigured and needs HPD\n", 900 __func__); 901 return -ENONET; 902 } 903 if (msg->reply) { 904 dprintk(1, "%s: invalid msg->reply\n", __func__); 905 return -EINVAL; 906 } 907 } 908 909 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) { 910 dprintk(2, "%s: transmit queue full\n", __func__); 911 return -EBUSY; 912 } 913 914 data = kzalloc(sizeof(*data), GFP_KERNEL); 915 if (!data) 916 return -ENOMEM; 917 918 msg->sequence = ++adap->sequence; 919 if (!msg->sequence) 920 msg->sequence = ++adap->sequence; 921 922 data->msg = *msg; 923 data->fh = fh; 924 data->adap = adap; 925 data->blocking = block; 926 927 init_completion(&data->c); 928 INIT_DELAYED_WORK(&data->work, cec_wait_timeout); 929 930 if (fh) 931 list_add_tail(&data->xfer_list, &fh->xfer_list); 932 else 933 INIT_LIST_HEAD(&data->xfer_list); 934 935 list_add_tail(&data->list, &adap->transmit_queue); 936 adap->transmit_queue_sz++; 937 if (!adap->transmitting) 938 wake_up_interruptible(&adap->kthread_waitq); 939 940 /* All done if we don't need to block waiting for completion */ 941 if (!block) 942 return 0; 943 944 /* 945 * Release the lock and wait, retake the lock afterwards. 946 */ 947 mutex_unlock(&adap->lock); 948 err = wait_for_completion_killable(&data->c); 949 cancel_delayed_work_sync(&data->work); 950 mutex_lock(&adap->lock); 951 952 if (err) 953 adap->transmit_in_progress_aborted = true; 954 955 /* Cancel the transmit if it was interrupted */ 956 if (!data->completed) { 957 if (data->msg.tx_status & CEC_TX_STATUS_OK) 958 cec_data_cancel(data, CEC_TX_STATUS_OK, CEC_RX_STATUS_ABORTED); 959 else 960 cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0); 961 } 962 963 /* The transmit completed (possibly with an error) */ 964 *msg = data->msg; 965 if (WARN_ON(!list_empty(&data->list))) 966 list_del(&data->list); 967 if (WARN_ON(!list_empty(&data->xfer_list))) 968 list_del(&data->xfer_list); 969 kfree(data); 970 return 0; 971 } 972 973 /* Helper function to be used by drivers and this framework. */ 974 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg, 975 bool block) 976 { 977 int ret; 978 979 mutex_lock(&adap->lock); 980 ret = cec_transmit_msg_fh(adap, msg, NULL, block); 981 mutex_unlock(&adap->lock); 982 return ret; 983 } 984 EXPORT_SYMBOL_GPL(cec_transmit_msg); 985 986 /* 987 * I don't like forward references but without this the low-level 988 * cec_received_msg() function would come after a bunch of high-level 989 * CEC protocol handling functions. That was very confusing. 990 */ 991 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg, 992 bool is_reply); 993 994 #define DIRECTED 0x80 995 #define BCAST1_4 0x40 996 #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */ 997 #define BCAST (BCAST1_4 | BCAST2_0) 998 #define BOTH (BCAST | DIRECTED) 999 1000 /* 1001 * Specify minimum length and whether the message is directed, broadcast 1002 * or both. Messages that do not match the criteria are ignored as per 1003 * the CEC specification. 1004 */ 1005 static const u8 cec_msg_size[256] = { 1006 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST, 1007 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED, 1008 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED, 1009 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED, 1010 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST, 1011 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST, 1012 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST, 1013 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST, 1014 [CEC_MSG_STANDBY] = 2 | BOTH, 1015 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED, 1016 [CEC_MSG_RECORD_ON] = 3 | DIRECTED, 1017 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED, 1018 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED, 1019 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED, 1020 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED, 1021 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED, 1022 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED, 1023 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED, 1024 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED, 1025 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED, 1026 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED, 1027 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED, 1028 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED, 1029 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED, 1030 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED, 1031 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED, 1032 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST, 1033 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST, 1034 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST, 1035 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED, 1036 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED, 1037 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED, 1038 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED, 1039 [CEC_MSG_PLAY] = 3 | DIRECTED, 1040 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED, 1041 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED, 1042 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED, 1043 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED, 1044 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED, 1045 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED, 1046 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST, 1047 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED, 1048 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED, 1049 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH, 1050 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH, 1051 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH, 1052 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED, 1053 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED, 1054 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED, 1055 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED, 1056 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED, 1057 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED, 1058 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED, 1059 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED, 1060 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0, 1061 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED, 1062 [CEC_MSG_ABORT] = 2 | DIRECTED, 1063 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED, 1064 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED, 1065 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED, 1066 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED, 1067 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED, 1068 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH, 1069 [CEC_MSG_SET_AUDIO_VOLUME_LEVEL] = 3 | DIRECTED, 1070 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED, 1071 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED, 1072 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED, 1073 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED, 1074 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED, 1075 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED, 1076 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED, 1077 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED, 1078 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED, 1079 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST, 1080 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST, 1081 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST, 1082 }; 1083 1084 /* Called by the CEC adapter if a message is received */ 1085 void cec_received_msg_ts(struct cec_adapter *adap, 1086 struct cec_msg *msg, ktime_t ts) 1087 { 1088 struct cec_data *data; 1089 u8 msg_init = cec_msg_initiator(msg); 1090 u8 msg_dest = cec_msg_destination(msg); 1091 u8 cmd = msg->msg[1]; 1092 bool is_reply = false; 1093 bool valid_la = true; 1094 bool monitor_valid_la = true; 1095 u8 min_len = 0; 1096 1097 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE)) 1098 return; 1099 1100 if (adap->devnode.unregistered) 1101 return; 1102 1103 /* 1104 * Some CEC adapters will receive the messages that they transmitted. 1105 * This test filters out those messages by checking if we are the 1106 * initiator, and just returning in that case. 1107 * 1108 * Note that this won't work if this is an Unregistered device. 1109 * 1110 * It is bad practice if the hardware receives the message that it 1111 * transmitted and luckily most CEC adapters behave correctly in this 1112 * respect. 1113 */ 1114 if (msg_init != CEC_LOG_ADDR_UNREGISTERED && 1115 cec_has_log_addr(adap, msg_init)) 1116 return; 1117 1118 msg->rx_ts = ktime_to_ns(ts); 1119 msg->rx_status = CEC_RX_STATUS_OK; 1120 msg->sequence = msg->reply = msg->timeout = 0; 1121 msg->tx_status = 0; 1122 msg->tx_ts = 0; 1123 msg->tx_arb_lost_cnt = 0; 1124 msg->tx_nack_cnt = 0; 1125 msg->tx_low_drive_cnt = 0; 1126 msg->tx_error_cnt = 0; 1127 msg->flags = 0; 1128 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len); 1129 1130 mutex_lock(&adap->lock); 1131 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg); 1132 1133 if (!adap->transmit_in_progress) 1134 adap->last_initiator = 0xff; 1135 1136 /* Check if this message was for us (directed or broadcast). */ 1137 if (!cec_msg_is_broadcast(msg)) { 1138 valid_la = cec_has_log_addr(adap, msg_dest); 1139 monitor_valid_la = valid_la; 1140 } 1141 1142 /* 1143 * Check if the length is not too short or if the message is a 1144 * broadcast message where a directed message was expected or 1145 * vice versa. If so, then the message has to be ignored (according 1146 * to section CEC 7.3 and CEC 12.2). 1147 */ 1148 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) { 1149 u8 dir_fl = cec_msg_size[cmd] & BOTH; 1150 1151 min_len = cec_msg_size[cmd] & 0x1f; 1152 if (msg->len < min_len) 1153 valid_la = false; 1154 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED)) 1155 valid_la = false; 1156 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST)) 1157 valid_la = false; 1158 else if (cec_msg_is_broadcast(msg) && 1159 adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0 && 1160 !(dir_fl & BCAST1_4)) 1161 valid_la = false; 1162 } 1163 if (valid_la && min_len) { 1164 /* These messages have special length requirements */ 1165 switch (cmd) { 1166 case CEC_MSG_RECORD_ON: 1167 switch (msg->msg[2]) { 1168 case CEC_OP_RECORD_SRC_OWN: 1169 break; 1170 case CEC_OP_RECORD_SRC_DIGITAL: 1171 if (msg->len < 10) 1172 valid_la = false; 1173 break; 1174 case CEC_OP_RECORD_SRC_ANALOG: 1175 if (msg->len < 7) 1176 valid_la = false; 1177 break; 1178 case CEC_OP_RECORD_SRC_EXT_PLUG: 1179 if (msg->len < 4) 1180 valid_la = false; 1181 break; 1182 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR: 1183 if (msg->len < 5) 1184 valid_la = false; 1185 break; 1186 } 1187 break; 1188 } 1189 } 1190 1191 /* It's a valid message and not a poll or CDC message */ 1192 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) { 1193 bool abort = cmd == CEC_MSG_FEATURE_ABORT; 1194 1195 /* The aborted command is in msg[2] */ 1196 if (abort) 1197 cmd = msg->msg[2]; 1198 1199 /* 1200 * Walk over all transmitted messages that are waiting for a 1201 * reply. 1202 */ 1203 list_for_each_entry(data, &adap->wait_queue, list) { 1204 struct cec_msg *dst = &data->msg; 1205 1206 /* 1207 * The *only* CEC message that has two possible replies 1208 * is CEC_MSG_INITIATE_ARC. 1209 * In this case allow either of the two replies. 1210 */ 1211 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC && 1212 (cmd == CEC_MSG_REPORT_ARC_INITIATED || 1213 cmd == CEC_MSG_REPORT_ARC_TERMINATED) && 1214 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED || 1215 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED)) 1216 dst->reply = cmd; 1217 1218 /* Does the command match? */ 1219 if ((abort && cmd != dst->msg[1]) || 1220 (!abort && cmd != dst->reply)) 1221 continue; 1222 1223 /* Does the addressing match? */ 1224 if (msg_init != cec_msg_destination(dst) && 1225 !cec_msg_is_broadcast(dst)) 1226 continue; 1227 1228 /* We got a reply */ 1229 memcpy(dst->msg, msg->msg, msg->len); 1230 dst->len = msg->len; 1231 dst->rx_ts = msg->rx_ts; 1232 dst->rx_status = msg->rx_status; 1233 if (abort) 1234 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT; 1235 msg->flags = dst->flags; 1236 msg->sequence = dst->sequence; 1237 /* Remove it from the wait_queue */ 1238 list_del_init(&data->list); 1239 1240 /* Cancel the pending timeout work */ 1241 if (!cancel_delayed_work(&data->work)) { 1242 mutex_unlock(&adap->lock); 1243 cancel_delayed_work_sync(&data->work); 1244 mutex_lock(&adap->lock); 1245 } 1246 /* 1247 * Mark this as a reply, provided someone is still 1248 * waiting for the answer. 1249 */ 1250 if (data->fh) 1251 is_reply = true; 1252 cec_data_completed(data); 1253 break; 1254 } 1255 } 1256 mutex_unlock(&adap->lock); 1257 1258 /* Pass the message on to any monitoring filehandles */ 1259 cec_queue_msg_monitor(adap, msg, monitor_valid_la); 1260 1261 /* We're done if it is not for us or a poll message */ 1262 if (!valid_la || msg->len <= 1) 1263 return; 1264 1265 if (adap->log_addrs.log_addr_mask == 0) 1266 return; 1267 1268 /* 1269 * Process the message on the protocol level. If is_reply is true, 1270 * then cec_receive_notify() won't pass on the reply to the listener(s) 1271 * since that was already done by cec_data_completed() above. 1272 */ 1273 cec_receive_notify(adap, msg, is_reply); 1274 } 1275 EXPORT_SYMBOL_GPL(cec_received_msg_ts); 1276 1277 /* Logical Address Handling */ 1278 1279 /* 1280 * Attempt to claim a specific logical address. 1281 * 1282 * This function is called with adap->lock held. 1283 */ 1284 static int cec_config_log_addr(struct cec_adapter *adap, 1285 unsigned int idx, 1286 unsigned int log_addr) 1287 { 1288 struct cec_log_addrs *las = &adap->log_addrs; 1289 struct cec_msg msg = { }; 1290 const unsigned int max_retries = 2; 1291 unsigned int i; 1292 int err; 1293 1294 if (cec_has_log_addr(adap, log_addr)) 1295 return 0; 1296 1297 /* Send poll message */ 1298 msg.len = 1; 1299 msg.msg[0] = (log_addr << 4) | log_addr; 1300 1301 for (i = 0; i < max_retries; i++) { 1302 err = cec_transmit_msg_fh(adap, &msg, NULL, true); 1303 1304 /* 1305 * While trying to poll the physical address was reset 1306 * and the adapter was unconfigured, so bail out. 1307 */ 1308 if (adap->phys_addr == CEC_PHYS_ADDR_INVALID) 1309 return -EINTR; 1310 1311 /* Also bail out if the PA changed while configuring. */ 1312 if (adap->must_reconfigure) 1313 return -EINTR; 1314 1315 if (err) 1316 return err; 1317 1318 /* 1319 * The message was aborted or timed out due to a disconnect or 1320 * unconfigure, just bail out. 1321 */ 1322 if (msg.tx_status & 1323 (CEC_TX_STATUS_ABORTED | CEC_TX_STATUS_TIMEOUT)) 1324 return -EINTR; 1325 if (msg.tx_status & CEC_TX_STATUS_OK) 1326 return 0; 1327 if (msg.tx_status & CEC_TX_STATUS_NACK) 1328 break; 1329 /* 1330 * Retry up to max_retries times if the message was neither 1331 * OKed or NACKed. This can happen due to e.g. a Lost 1332 * Arbitration condition. 1333 */ 1334 } 1335 1336 /* 1337 * If we are unable to get an OK or a NACK after max_retries attempts 1338 * (and note that each attempt already consists of four polls), then 1339 * we assume that something is really weird and that it is not a 1340 * good idea to try and claim this logical address. 1341 */ 1342 if (i == max_retries) { 1343 dprintk(0, "polling for LA %u failed with tx_status=0x%04x\n", 1344 log_addr, msg.tx_status); 1345 return 0; 1346 } 1347 1348 /* 1349 * Message not acknowledged, so this logical 1350 * address is free to use. 1351 */ 1352 err = call_op(adap, adap_log_addr, log_addr); 1353 if (err) 1354 return err; 1355 1356 las->log_addr[idx] = log_addr; 1357 las->log_addr_mask |= 1 << log_addr; 1358 return 1; 1359 } 1360 1361 /* 1362 * Unconfigure the adapter: clear all logical addresses and send 1363 * the state changed event. 1364 * 1365 * This function is called with adap->lock held. 1366 */ 1367 static void cec_adap_unconfigure(struct cec_adapter *adap) 1368 { 1369 if (!adap->needs_hpd || adap->phys_addr != CEC_PHYS_ADDR_INVALID) 1370 WARN_ON(call_op(adap, adap_log_addr, CEC_LOG_ADDR_INVALID)); 1371 adap->log_addrs.log_addr_mask = 0; 1372 adap->is_configured = false; 1373 cec_flush(adap); 1374 wake_up_interruptible(&adap->kthread_waitq); 1375 cec_post_state_event(adap); 1376 call_void_op(adap, adap_unconfigured); 1377 } 1378 1379 /* 1380 * Attempt to claim the required logical addresses. 1381 */ 1382 static int cec_config_thread_func(void *arg) 1383 { 1384 /* The various LAs for each type of device */ 1385 static const u8 tv_log_addrs[] = { 1386 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC, 1387 CEC_LOG_ADDR_INVALID 1388 }; 1389 static const u8 record_log_addrs[] = { 1390 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2, 1391 CEC_LOG_ADDR_RECORD_3, 1392 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2, 1393 CEC_LOG_ADDR_INVALID 1394 }; 1395 static const u8 tuner_log_addrs[] = { 1396 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2, 1397 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4, 1398 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2, 1399 CEC_LOG_ADDR_INVALID 1400 }; 1401 static const u8 playback_log_addrs[] = { 1402 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2, 1403 CEC_LOG_ADDR_PLAYBACK_3, 1404 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2, 1405 CEC_LOG_ADDR_INVALID 1406 }; 1407 static const u8 audiosystem_log_addrs[] = { 1408 CEC_LOG_ADDR_AUDIOSYSTEM, 1409 CEC_LOG_ADDR_INVALID 1410 }; 1411 static const u8 specific_use_log_addrs[] = { 1412 CEC_LOG_ADDR_SPECIFIC, 1413 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2, 1414 CEC_LOG_ADDR_INVALID 1415 }; 1416 static const u8 *type2addrs[6] = { 1417 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs, 1418 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs, 1419 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs, 1420 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs, 1421 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs, 1422 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs, 1423 }; 1424 static const u16 type2mask[] = { 1425 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV, 1426 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD, 1427 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER, 1428 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK, 1429 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM, 1430 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC, 1431 }; 1432 struct cec_adapter *adap = arg; 1433 struct cec_log_addrs *las = &adap->log_addrs; 1434 int err; 1435 int i, j; 1436 1437 mutex_lock(&adap->lock); 1438 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n", 1439 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs); 1440 las->log_addr_mask = 0; 1441 1442 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED) 1443 goto configured; 1444 1445 reconfigure: 1446 for (i = 0; i < las->num_log_addrs; i++) { 1447 unsigned int type = las->log_addr_type[i]; 1448 const u8 *la_list; 1449 u8 last_la; 1450 1451 /* 1452 * The TV functionality can only map to physical address 0. 1453 * For any other address, try the Specific functionality 1454 * instead as per the spec. 1455 */ 1456 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV) 1457 type = CEC_LOG_ADDR_TYPE_SPECIFIC; 1458 1459 la_list = type2addrs[type]; 1460 last_la = las->log_addr[i]; 1461 las->log_addr[i] = CEC_LOG_ADDR_INVALID; 1462 if (last_la == CEC_LOG_ADDR_INVALID || 1463 last_la == CEC_LOG_ADDR_UNREGISTERED || 1464 !((1 << last_la) & type2mask[type])) 1465 last_la = la_list[0]; 1466 1467 err = cec_config_log_addr(adap, i, last_la); 1468 1469 if (adap->must_reconfigure) { 1470 adap->must_reconfigure = false; 1471 las->log_addr_mask = 0; 1472 goto reconfigure; 1473 } 1474 1475 if (err > 0) /* Reused last LA */ 1476 continue; 1477 1478 if (err < 0) 1479 goto unconfigure; 1480 1481 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) { 1482 /* Tried this one already, skip it */ 1483 if (la_list[j] == last_la) 1484 continue; 1485 /* The backup addresses are CEC 2.0 specific */ 1486 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 || 1487 la_list[j] == CEC_LOG_ADDR_BACKUP_2) && 1488 las->cec_version < CEC_OP_CEC_VERSION_2_0) 1489 continue; 1490 1491 err = cec_config_log_addr(adap, i, la_list[j]); 1492 if (err == 0) /* LA is in use */ 1493 continue; 1494 if (err < 0) 1495 goto unconfigure; 1496 /* Done, claimed an LA */ 1497 break; 1498 } 1499 1500 if (la_list[j] == CEC_LOG_ADDR_INVALID) 1501 dprintk(1, "could not claim LA %d\n", i); 1502 } 1503 1504 if (adap->log_addrs.log_addr_mask == 0 && 1505 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK)) 1506 goto unconfigure; 1507 1508 configured: 1509 if (adap->log_addrs.log_addr_mask == 0) { 1510 /* Fall back to unregistered */ 1511 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED; 1512 las->log_addr_mask = 1 << las->log_addr[0]; 1513 for (i = 1; i < las->num_log_addrs; i++) 1514 las->log_addr[i] = CEC_LOG_ADDR_INVALID; 1515 } 1516 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) 1517 las->log_addr[i] = CEC_LOG_ADDR_INVALID; 1518 adap->is_configured = true; 1519 adap->is_configuring = false; 1520 adap->must_reconfigure = false; 1521 cec_post_state_event(adap); 1522 1523 /* 1524 * Now post the Report Features and Report Physical Address broadcast 1525 * messages. Note that these are non-blocking transmits, meaning that 1526 * they are just queued up and once adap->lock is unlocked the main 1527 * thread will kick in and start transmitting these. 1528 * 1529 * If after this function is done (but before one or more of these 1530 * messages are actually transmitted) the CEC adapter is unconfigured, 1531 * then any remaining messages will be dropped by the main thread. 1532 */ 1533 for (i = 0; i < las->num_log_addrs; i++) { 1534 struct cec_msg msg = {}; 1535 1536 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID || 1537 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY)) 1538 continue; 1539 1540 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f; 1541 1542 /* Report Features must come first according to CEC 2.0 */ 1543 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED && 1544 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) { 1545 cec_fill_msg_report_features(adap, &msg, i); 1546 cec_transmit_msg_fh(adap, &msg, NULL, false); 1547 } 1548 1549 /* Report Physical Address */ 1550 cec_msg_report_physical_addr(&msg, adap->phys_addr, 1551 las->primary_device_type[i]); 1552 dprintk(1, "config: la %d pa %x.%x.%x.%x\n", 1553 las->log_addr[i], 1554 cec_phys_addr_exp(adap->phys_addr)); 1555 cec_transmit_msg_fh(adap, &msg, NULL, false); 1556 1557 /* Report Vendor ID */ 1558 if (adap->log_addrs.vendor_id != CEC_VENDOR_ID_NONE) { 1559 cec_msg_device_vendor_id(&msg, 1560 adap->log_addrs.vendor_id); 1561 cec_transmit_msg_fh(adap, &msg, NULL, false); 1562 } 1563 } 1564 adap->kthread_config = NULL; 1565 complete(&adap->config_completion); 1566 mutex_unlock(&adap->lock); 1567 call_void_op(adap, configured); 1568 return 0; 1569 1570 unconfigure: 1571 for (i = 0; i < las->num_log_addrs; i++) 1572 las->log_addr[i] = CEC_LOG_ADDR_INVALID; 1573 cec_adap_unconfigure(adap); 1574 adap->is_configuring = false; 1575 adap->must_reconfigure = false; 1576 adap->kthread_config = NULL; 1577 complete(&adap->config_completion); 1578 mutex_unlock(&adap->lock); 1579 return 0; 1580 } 1581 1582 /* 1583 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the 1584 * logical addresses. 1585 * 1586 * This function is called with adap->lock held. 1587 */ 1588 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block) 1589 { 1590 if (WARN_ON(adap->is_claiming_log_addrs || 1591 adap->is_configuring || adap->is_configured)) 1592 return; 1593 1594 adap->is_claiming_log_addrs = true; 1595 1596 init_completion(&adap->config_completion); 1597 1598 /* Ready to kick off the thread */ 1599 adap->is_configuring = true; 1600 adap->kthread_config = kthread_run(cec_config_thread_func, adap, 1601 "ceccfg-%s", adap->name); 1602 if (IS_ERR(adap->kthread_config)) { 1603 adap->kthread_config = NULL; 1604 adap->is_configuring = false; 1605 } else if (block) { 1606 mutex_unlock(&adap->lock); 1607 wait_for_completion(&adap->config_completion); 1608 mutex_lock(&adap->lock); 1609 } 1610 adap->is_claiming_log_addrs = false; 1611 } 1612 1613 /* 1614 * Helper function to enable/disable the CEC adapter. 1615 * 1616 * This function is called with adap->lock held. 1617 */ 1618 int cec_adap_enable(struct cec_adapter *adap) 1619 { 1620 bool enable; 1621 int ret = 0; 1622 1623 enable = adap->monitor_all_cnt || adap->monitor_pin_cnt || 1624 adap->log_addrs.num_log_addrs; 1625 if (adap->needs_hpd) 1626 enable = enable && adap->phys_addr != CEC_PHYS_ADDR_INVALID; 1627 1628 if (adap->devnode.unregistered) 1629 enable = false; 1630 1631 if (enable == adap->is_enabled) 1632 return 0; 1633 1634 /* serialize adap_enable */ 1635 mutex_lock(&adap->devnode.lock); 1636 if (enable) { 1637 adap->last_initiator = 0xff; 1638 adap->transmit_in_progress = false; 1639 adap->tx_low_drive_log_cnt = 0; 1640 adap->tx_error_log_cnt = 0; 1641 ret = adap->ops->adap_enable(adap, true); 1642 if (!ret) { 1643 /* 1644 * Enable monitor-all/pin modes if needed. We warn, but 1645 * continue if this fails as this is not a critical error. 1646 */ 1647 if (adap->monitor_all_cnt) 1648 WARN_ON(call_op(adap, adap_monitor_all_enable, true)); 1649 if (adap->monitor_pin_cnt) 1650 WARN_ON(call_op(adap, adap_monitor_pin_enable, true)); 1651 } 1652 } else { 1653 /* Disable monitor-all/pin modes if needed (needs_hpd == 1) */ 1654 if (adap->monitor_all_cnt) 1655 WARN_ON(call_op(adap, adap_monitor_all_enable, false)); 1656 if (adap->monitor_pin_cnt) 1657 WARN_ON(call_op(adap, adap_monitor_pin_enable, false)); 1658 WARN_ON(adap->ops->adap_enable(adap, false)); 1659 adap->last_initiator = 0xff; 1660 adap->transmit_in_progress = false; 1661 adap->transmit_in_progress_aborted = false; 1662 if (adap->transmitting) 1663 cec_data_cancel(adap->transmitting, CEC_TX_STATUS_ABORTED, 0); 1664 } 1665 if (!ret) 1666 adap->is_enabled = enable; 1667 wake_up_interruptible(&adap->kthread_waitq); 1668 mutex_unlock(&adap->devnode.lock); 1669 return ret; 1670 } 1671 1672 /* Set a new physical address and send an event notifying userspace of this. 1673 * 1674 * This function is called with adap->lock held. 1675 */ 1676 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block) 1677 { 1678 bool becomes_invalid = phys_addr == CEC_PHYS_ADDR_INVALID; 1679 bool is_invalid = adap->phys_addr == CEC_PHYS_ADDR_INVALID; 1680 1681 if (phys_addr == adap->phys_addr) 1682 return; 1683 if (!becomes_invalid && adap->devnode.unregistered) 1684 return; 1685 1686 dprintk(1, "new physical address %x.%x.%x.%x\n", 1687 cec_phys_addr_exp(phys_addr)); 1688 if (becomes_invalid || !is_invalid) { 1689 adap->phys_addr = CEC_PHYS_ADDR_INVALID; 1690 cec_post_state_event(adap); 1691 cec_adap_unconfigure(adap); 1692 if (becomes_invalid) { 1693 cec_adap_enable(adap); 1694 return; 1695 } 1696 } 1697 1698 adap->phys_addr = phys_addr; 1699 if (is_invalid) 1700 cec_adap_enable(adap); 1701 1702 cec_post_state_event(adap); 1703 if (!adap->log_addrs.num_log_addrs) 1704 return; 1705 if (adap->is_configuring) 1706 adap->must_reconfigure = true; 1707 else 1708 cec_claim_log_addrs(adap, block); 1709 } 1710 1711 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block) 1712 { 1713 if (IS_ERR_OR_NULL(adap)) 1714 return; 1715 1716 mutex_lock(&adap->lock); 1717 __cec_s_phys_addr(adap, phys_addr, block); 1718 mutex_unlock(&adap->lock); 1719 } 1720 EXPORT_SYMBOL_GPL(cec_s_phys_addr); 1721 1722 /* 1723 * Note: In the drm subsystem, prefer calling (if possible): 1724 * 1725 * cec_s_phys_addr(adap, connector->display_info.source_physical_address, false); 1726 */ 1727 void cec_s_phys_addr_from_edid(struct cec_adapter *adap, 1728 const struct edid *edid) 1729 { 1730 u16 pa = CEC_PHYS_ADDR_INVALID; 1731 1732 if (edid && edid->extensions) 1733 pa = cec_get_edid_phys_addr((const u8 *)edid, 1734 EDID_LENGTH * (edid->extensions + 1), NULL); 1735 cec_s_phys_addr(adap, pa, false); 1736 } 1737 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid); 1738 1739 void cec_s_conn_info(struct cec_adapter *adap, 1740 const struct cec_connector_info *conn_info) 1741 { 1742 if (IS_ERR_OR_NULL(adap)) 1743 return; 1744 1745 if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO)) 1746 return; 1747 1748 mutex_lock(&adap->lock); 1749 if (conn_info) 1750 adap->conn_info = *conn_info; 1751 else 1752 memset(&adap->conn_info, 0, sizeof(adap->conn_info)); 1753 cec_post_state_event(adap); 1754 mutex_unlock(&adap->lock); 1755 } 1756 EXPORT_SYMBOL_GPL(cec_s_conn_info); 1757 1758 /* 1759 * Called from either the ioctl or a driver to set the logical addresses. 1760 * 1761 * This function is called with adap->lock held. 1762 */ 1763 int __cec_s_log_addrs(struct cec_adapter *adap, 1764 struct cec_log_addrs *log_addrs, bool block) 1765 { 1766 u16 type_mask = 0; 1767 int err; 1768 int i; 1769 1770 if (adap->devnode.unregistered) 1771 return -ENODEV; 1772 1773 if (!log_addrs || log_addrs->num_log_addrs == 0) { 1774 if (!adap->log_addrs.num_log_addrs) 1775 return 0; 1776 if (adap->is_configuring || adap->is_configured) 1777 cec_adap_unconfigure(adap); 1778 adap->log_addrs.num_log_addrs = 0; 1779 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++) 1780 adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID; 1781 adap->log_addrs.osd_name[0] = '\0'; 1782 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE; 1783 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0; 1784 cec_adap_enable(adap); 1785 return 0; 1786 } 1787 1788 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) { 1789 /* 1790 * Sanitize log_addrs fields if a CDC-Only device is 1791 * requested. 1792 */ 1793 log_addrs->num_log_addrs = 1; 1794 log_addrs->osd_name[0] = '\0'; 1795 log_addrs->vendor_id = CEC_VENDOR_ID_NONE; 1796 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED; 1797 /* 1798 * This is just an internal convention since a CDC-Only device 1799 * doesn't have to be a switch. But switches already use 1800 * unregistered, so it makes some kind of sense to pick this 1801 * as the primary device. Since a CDC-Only device never sends 1802 * any 'normal' CEC messages this primary device type is never 1803 * sent over the CEC bus. 1804 */ 1805 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH; 1806 log_addrs->all_device_types[0] = 0; 1807 log_addrs->features[0][0] = 0; 1808 log_addrs->features[0][1] = 0; 1809 } 1810 1811 /* Ensure the osd name is 0-terminated */ 1812 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0'; 1813 1814 /* Sanity checks */ 1815 if (log_addrs->num_log_addrs > adap->available_log_addrs) { 1816 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs); 1817 return -EINVAL; 1818 } 1819 1820 /* 1821 * Vendor ID is a 24 bit number, so check if the value is 1822 * within the correct range. 1823 */ 1824 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE && 1825 (log_addrs->vendor_id & 0xff000000) != 0) { 1826 dprintk(1, "invalid vendor ID\n"); 1827 return -EINVAL; 1828 } 1829 1830 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 && 1831 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) { 1832 dprintk(1, "invalid CEC version\n"); 1833 return -EINVAL; 1834 } 1835 1836 if (log_addrs->num_log_addrs > 1) 1837 for (i = 0; i < log_addrs->num_log_addrs; i++) 1838 if (log_addrs->log_addr_type[i] == 1839 CEC_LOG_ADDR_TYPE_UNREGISTERED) { 1840 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n"); 1841 return -EINVAL; 1842 } 1843 1844 for (i = 0; i < log_addrs->num_log_addrs; i++) { 1845 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]); 1846 u8 *features = log_addrs->features[i]; 1847 bool op_is_dev_features = false; 1848 unsigned int j; 1849 1850 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID; 1851 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) { 1852 dprintk(1, "unknown logical address type\n"); 1853 return -EINVAL; 1854 } 1855 if (type_mask & (1 << log_addrs->log_addr_type[i])) { 1856 dprintk(1, "duplicate logical address type\n"); 1857 return -EINVAL; 1858 } 1859 type_mask |= 1 << log_addrs->log_addr_type[i]; 1860 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) && 1861 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) { 1862 /* Record already contains the playback functionality */ 1863 dprintk(1, "invalid record + playback combination\n"); 1864 return -EINVAL; 1865 } 1866 if (log_addrs->primary_device_type[i] > 1867 CEC_OP_PRIM_DEVTYPE_PROCESSOR) { 1868 dprintk(1, "unknown primary device type\n"); 1869 return -EINVAL; 1870 } 1871 if (log_addrs->primary_device_type[i] == 2) { 1872 dprintk(1, "invalid primary device type\n"); 1873 return -EINVAL; 1874 } 1875 for (j = 0; j < feature_sz; j++) { 1876 if ((features[j] & 0x80) == 0) { 1877 if (op_is_dev_features) 1878 break; 1879 op_is_dev_features = true; 1880 } 1881 } 1882 if (!op_is_dev_features || j == feature_sz) { 1883 dprintk(1, "malformed features\n"); 1884 return -EINVAL; 1885 } 1886 /* Zero unused part of the feature array */ 1887 memset(features + j + 1, 0, feature_sz - j - 1); 1888 } 1889 1890 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) { 1891 if (log_addrs->num_log_addrs > 2) { 1892 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n"); 1893 return -EINVAL; 1894 } 1895 if (log_addrs->num_log_addrs == 2) { 1896 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) | 1897 (1 << CEC_LOG_ADDR_TYPE_TV)))) { 1898 dprintk(1, "two LAs is only allowed for audiosystem and TV\n"); 1899 return -EINVAL; 1900 } 1901 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) | 1902 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) { 1903 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n"); 1904 return -EINVAL; 1905 } 1906 } 1907 } 1908 1909 /* Zero unused LAs */ 1910 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) { 1911 log_addrs->primary_device_type[i] = 0; 1912 log_addrs->log_addr_type[i] = 0; 1913 log_addrs->all_device_types[i] = 0; 1914 memset(log_addrs->features[i], 0, 1915 sizeof(log_addrs->features[i])); 1916 } 1917 1918 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask; 1919 adap->log_addrs = *log_addrs; 1920 err = cec_adap_enable(adap); 1921 if (!err && adap->phys_addr != CEC_PHYS_ADDR_INVALID) 1922 cec_claim_log_addrs(adap, block); 1923 return err; 1924 } 1925 1926 int cec_s_log_addrs(struct cec_adapter *adap, 1927 struct cec_log_addrs *log_addrs, bool block) 1928 { 1929 int err; 1930 1931 mutex_lock(&adap->lock); 1932 err = __cec_s_log_addrs(adap, log_addrs, block); 1933 mutex_unlock(&adap->lock); 1934 return err; 1935 } 1936 EXPORT_SYMBOL_GPL(cec_s_log_addrs); 1937 1938 /* High-level core CEC message handling */ 1939 1940 /* Fill in the Report Features message */ 1941 static void cec_fill_msg_report_features(struct cec_adapter *adap, 1942 struct cec_msg *msg, 1943 unsigned int la_idx) 1944 { 1945 const struct cec_log_addrs *las = &adap->log_addrs; 1946 const u8 *features = las->features[la_idx]; 1947 bool op_is_dev_features = false; 1948 unsigned int idx; 1949 1950 /* Report Features */ 1951 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f; 1952 msg->len = 4; 1953 msg->msg[1] = CEC_MSG_REPORT_FEATURES; 1954 msg->msg[2] = adap->log_addrs.cec_version; 1955 msg->msg[3] = las->all_device_types[la_idx]; 1956 1957 /* Write RC Profiles first, then Device Features */ 1958 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) { 1959 msg->msg[msg->len++] = features[idx]; 1960 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) { 1961 if (op_is_dev_features) 1962 break; 1963 op_is_dev_features = true; 1964 } 1965 } 1966 } 1967 1968 /* Transmit the Feature Abort message */ 1969 static int cec_feature_abort_reason(struct cec_adapter *adap, 1970 struct cec_msg *msg, u8 reason) 1971 { 1972 struct cec_msg tx_msg = { }; 1973 1974 /* 1975 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT 1976 * message! 1977 */ 1978 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT) 1979 return 0; 1980 /* Don't Feature Abort messages from 'Unregistered' */ 1981 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED) 1982 return 0; 1983 cec_msg_set_reply_to(&tx_msg, msg); 1984 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason); 1985 return cec_transmit_msg(adap, &tx_msg, false); 1986 } 1987 1988 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg) 1989 { 1990 return cec_feature_abort_reason(adap, msg, 1991 CEC_OP_ABORT_UNRECOGNIZED_OP); 1992 } 1993 1994 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg) 1995 { 1996 return cec_feature_abort_reason(adap, msg, 1997 CEC_OP_ABORT_REFUSED); 1998 } 1999 2000 /* 2001 * Called when a CEC message is received. This function will do any 2002 * necessary core processing. The is_reply bool is true if this message 2003 * is a reply to an earlier transmit. 2004 * 2005 * The message is either a broadcast message or a valid directed message. 2006 */ 2007 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg, 2008 bool is_reply) 2009 { 2010 bool is_broadcast = cec_msg_is_broadcast(msg); 2011 u8 dest_laddr = cec_msg_destination(msg); 2012 u8 init_laddr = cec_msg_initiator(msg); 2013 u8 devtype = cec_log_addr2dev(adap, dest_laddr); 2014 int la_idx = cec_log_addr2idx(adap, dest_laddr); 2015 bool from_unregistered = init_laddr == 0xf; 2016 struct cec_msg tx_cec_msg = { }; 2017 2018 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg); 2019 2020 /* If this is a CDC-Only device, then ignore any non-CDC messages */ 2021 if (cec_is_cdc_only(&adap->log_addrs) && 2022 msg->msg[1] != CEC_MSG_CDC_MESSAGE) 2023 return 0; 2024 2025 /* Allow drivers to process the message first */ 2026 if (adap->ops->received && !adap->devnode.unregistered && 2027 adap->ops->received(adap, msg) != -ENOMSG) 2028 return 0; 2029 2030 /* 2031 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and 2032 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be 2033 * handled by the CEC core, even if the passthrough mode is on. 2034 * The others are just ignored if passthrough mode is on. 2035 */ 2036 switch (msg->msg[1]) { 2037 case CEC_MSG_GET_CEC_VERSION: 2038 case CEC_MSG_ABORT: 2039 case CEC_MSG_GIVE_DEVICE_POWER_STATUS: 2040 case CEC_MSG_GIVE_OSD_NAME: 2041 /* 2042 * These messages reply with a directed message, so ignore if 2043 * the initiator is Unregistered. 2044 */ 2045 if (!adap->passthrough && from_unregistered) 2046 return 0; 2047 fallthrough; 2048 case CEC_MSG_GIVE_DEVICE_VENDOR_ID: 2049 case CEC_MSG_GIVE_FEATURES: 2050 case CEC_MSG_GIVE_PHYSICAL_ADDR: 2051 /* 2052 * Skip processing these messages if the passthrough mode 2053 * is on. 2054 */ 2055 if (adap->passthrough) 2056 goto skip_processing; 2057 /* Ignore if addressing is wrong */ 2058 if (is_broadcast) 2059 return 0; 2060 break; 2061 2062 case CEC_MSG_USER_CONTROL_PRESSED: 2063 case CEC_MSG_USER_CONTROL_RELEASED: 2064 /* Wrong addressing mode: don't process */ 2065 if (is_broadcast || from_unregistered) 2066 goto skip_processing; 2067 break; 2068 2069 case CEC_MSG_REPORT_PHYSICAL_ADDR: 2070 /* 2071 * This message is always processed, regardless of the 2072 * passthrough setting. 2073 * 2074 * Exception: don't process if wrong addressing mode. 2075 */ 2076 if (!is_broadcast) 2077 goto skip_processing; 2078 break; 2079 2080 default: 2081 break; 2082 } 2083 2084 cec_msg_set_reply_to(&tx_cec_msg, msg); 2085 2086 switch (msg->msg[1]) { 2087 /* The following messages are processed but still passed through */ 2088 case CEC_MSG_REPORT_PHYSICAL_ADDR: { 2089 u16 pa = (msg->msg[2] << 8) | msg->msg[3]; 2090 2091 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n", 2092 cec_phys_addr_exp(pa), init_laddr); 2093 break; 2094 } 2095 2096 case CEC_MSG_USER_CONTROL_PRESSED: 2097 if (!(adap->capabilities & CEC_CAP_RC) || 2098 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU)) 2099 break; 2100 2101 #ifdef CONFIG_MEDIA_CEC_RC 2102 switch (msg->msg[2]) { 2103 /* 2104 * Play function, this message can have variable length 2105 * depending on the specific play function that is used. 2106 */ 2107 case CEC_OP_UI_CMD_PLAY_FUNCTION: 2108 if (msg->len == 2) 2109 rc_keydown(adap->rc, RC_PROTO_CEC, 2110 msg->msg[2], 0); 2111 else 2112 rc_keydown(adap->rc, RC_PROTO_CEC, 2113 msg->msg[2] << 8 | msg->msg[3], 0); 2114 break; 2115 /* 2116 * Other function messages that are not handled. 2117 * Currently the RC framework does not allow to supply an 2118 * additional parameter to a keypress. These "keys" contain 2119 * other information such as channel number, an input number 2120 * etc. 2121 * For the time being these messages are not processed by the 2122 * framework and are simply forwarded to the user space. 2123 */ 2124 case CEC_OP_UI_CMD_SELECT_BROADCAST_TYPE: 2125 case CEC_OP_UI_CMD_SELECT_SOUND_PRESENTATION: 2126 case CEC_OP_UI_CMD_TUNE_FUNCTION: 2127 case CEC_OP_UI_CMD_SELECT_MEDIA_FUNCTION: 2128 case CEC_OP_UI_CMD_SELECT_AV_INPUT_FUNCTION: 2129 case CEC_OP_UI_CMD_SELECT_AUDIO_INPUT_FUNCTION: 2130 break; 2131 default: 2132 rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0); 2133 break; 2134 } 2135 #endif 2136 break; 2137 2138 case CEC_MSG_USER_CONTROL_RELEASED: 2139 if (!(adap->capabilities & CEC_CAP_RC) || 2140 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU)) 2141 break; 2142 #ifdef CONFIG_MEDIA_CEC_RC 2143 rc_keyup(adap->rc); 2144 #endif 2145 break; 2146 2147 /* 2148 * The remaining messages are only processed if the passthrough mode 2149 * is off. 2150 */ 2151 case CEC_MSG_GET_CEC_VERSION: 2152 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version); 2153 return cec_transmit_msg(adap, &tx_cec_msg, false); 2154 2155 case CEC_MSG_GIVE_PHYSICAL_ADDR: 2156 /* Do nothing for CEC switches using addr 15 */ 2157 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15) 2158 return 0; 2159 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype); 2160 return cec_transmit_msg(adap, &tx_cec_msg, false); 2161 2162 case CEC_MSG_GIVE_DEVICE_VENDOR_ID: 2163 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE) 2164 return cec_feature_abort(adap, msg); 2165 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id); 2166 return cec_transmit_msg(adap, &tx_cec_msg, false); 2167 2168 case CEC_MSG_ABORT: 2169 /* Do nothing for CEC switches */ 2170 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH) 2171 return 0; 2172 return cec_feature_refused(adap, msg); 2173 2174 case CEC_MSG_GIVE_OSD_NAME: { 2175 if (adap->log_addrs.osd_name[0] == 0) 2176 return cec_feature_abort(adap, msg); 2177 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name); 2178 return cec_transmit_msg(adap, &tx_cec_msg, false); 2179 } 2180 2181 case CEC_MSG_GIVE_FEATURES: 2182 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0) 2183 return cec_feature_abort(adap, msg); 2184 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx); 2185 return cec_transmit_msg(adap, &tx_cec_msg, false); 2186 2187 default: 2188 /* 2189 * Unprocessed messages are aborted if userspace isn't doing 2190 * any processing either. 2191 */ 2192 if (!is_broadcast && !is_reply && !adap->follower_cnt && 2193 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT) 2194 return cec_feature_abort(adap, msg); 2195 break; 2196 } 2197 2198 skip_processing: 2199 /* If this was a reply, then we're done, unless otherwise specified */ 2200 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS)) 2201 return 0; 2202 2203 /* 2204 * Send to the exclusive follower if there is one, otherwise send 2205 * to all followers. 2206 */ 2207 if (adap->cec_follower) 2208 cec_queue_msg_fh(adap->cec_follower, msg); 2209 else 2210 cec_queue_msg_followers(adap, msg); 2211 return 0; 2212 } 2213 2214 /* 2215 * Helper functions to keep track of the 'monitor all' use count. 2216 * 2217 * These functions are called with adap->lock held. 2218 */ 2219 int cec_monitor_all_cnt_inc(struct cec_adapter *adap) 2220 { 2221 int ret; 2222 2223 if (adap->monitor_all_cnt++) 2224 return 0; 2225 2226 ret = cec_adap_enable(adap); 2227 if (ret) 2228 adap->monitor_all_cnt--; 2229 return ret; 2230 } 2231 2232 void cec_monitor_all_cnt_dec(struct cec_adapter *adap) 2233 { 2234 if (WARN_ON(!adap->monitor_all_cnt)) 2235 return; 2236 if (--adap->monitor_all_cnt) 2237 return; 2238 WARN_ON(call_op(adap, adap_monitor_all_enable, false)); 2239 cec_adap_enable(adap); 2240 } 2241 2242 /* 2243 * Helper functions to keep track of the 'monitor pin' use count. 2244 * 2245 * These functions are called with adap->lock held. 2246 */ 2247 int cec_monitor_pin_cnt_inc(struct cec_adapter *adap) 2248 { 2249 int ret; 2250 2251 if (adap->monitor_pin_cnt++) 2252 return 0; 2253 2254 ret = cec_adap_enable(adap); 2255 if (ret) 2256 adap->monitor_pin_cnt--; 2257 return ret; 2258 } 2259 2260 void cec_monitor_pin_cnt_dec(struct cec_adapter *adap) 2261 { 2262 if (WARN_ON(!adap->monitor_pin_cnt)) 2263 return; 2264 if (--adap->monitor_pin_cnt) 2265 return; 2266 WARN_ON(call_op(adap, adap_monitor_pin_enable, false)); 2267 cec_adap_enable(adap); 2268 } 2269 2270 #ifdef CONFIG_DEBUG_FS 2271 /* 2272 * Log the current state of the CEC adapter. 2273 * Very useful for debugging. 2274 */ 2275 int cec_adap_status(struct seq_file *file, void *priv) 2276 { 2277 struct cec_adapter *adap = dev_get_drvdata(file->private); 2278 struct cec_data *data; 2279 2280 mutex_lock(&adap->lock); 2281 seq_printf(file, "enabled: %d\n", adap->is_enabled); 2282 seq_printf(file, "configured: %d\n", adap->is_configured); 2283 seq_printf(file, "configuring: %d\n", adap->is_configuring); 2284 seq_printf(file, "phys_addr: %x.%x.%x.%x\n", 2285 cec_phys_addr_exp(adap->phys_addr)); 2286 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs); 2287 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask); 2288 if (adap->cec_follower) 2289 seq_printf(file, "has CEC follower%s\n", 2290 adap->passthrough ? " (in passthrough mode)" : ""); 2291 if (adap->cec_initiator) 2292 seq_puts(file, "has CEC initiator\n"); 2293 if (adap->monitor_all_cnt) 2294 seq_printf(file, "file handles in Monitor All mode: %u\n", 2295 adap->monitor_all_cnt); 2296 if (adap->monitor_pin_cnt) 2297 seq_printf(file, "file handles in Monitor Pin mode: %u\n", 2298 adap->monitor_pin_cnt); 2299 if (adap->tx_timeout_cnt) { 2300 seq_printf(file, "transmit timeout count: %u\n", 2301 adap->tx_timeout_cnt); 2302 adap->tx_timeout_cnt = 0; 2303 } 2304 if (adap->tx_low_drive_cnt) { 2305 seq_printf(file, "transmit low drive count: %u\n", 2306 adap->tx_low_drive_cnt); 2307 adap->tx_low_drive_cnt = 0; 2308 } 2309 if (adap->tx_arb_lost_cnt) { 2310 seq_printf(file, "transmit arbitration lost count: %u\n", 2311 adap->tx_arb_lost_cnt); 2312 adap->tx_arb_lost_cnt = 0; 2313 } 2314 if (adap->tx_error_cnt) { 2315 seq_printf(file, "transmit error count: %u\n", 2316 adap->tx_error_cnt); 2317 adap->tx_error_cnt = 0; 2318 } 2319 data = adap->transmitting; 2320 if (data) 2321 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n", 2322 data->msg.len, data->msg.msg, data->msg.reply, 2323 data->msg.timeout); 2324 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz); 2325 list_for_each_entry(data, &adap->transmit_queue, list) { 2326 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n", 2327 data->msg.len, data->msg.msg, data->msg.reply, 2328 data->msg.timeout); 2329 } 2330 list_for_each_entry(data, &adap->wait_queue, list) { 2331 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n", 2332 data->msg.len, data->msg.msg, data->msg.reply, 2333 data->msg.timeout); 2334 } 2335 2336 call_void_op(adap, adap_status, file); 2337 mutex_unlock(&adap->lock); 2338 return 0; 2339 } 2340 #endif 2341