File Index
Files and directories
Sign in
FILE

cec-adap.c

File preview with syntax highlighting.

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