1 // SPDX-License-Identifier: GPL-2.0-only
3 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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>
16 #include <linux/string.h>
17 #include <linux/types.h>
19 #include <drm/drm_connector.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_edid.h>
22 #include <drm/drm_file.h>
26 static void cec_fill_msg_report_features(struct cec_adapter *adap,
31 * 400 ms is the time it takes for one 16 byte message to be
32 * transferred and 5 is the maximum number of retries. Add
33 * another 100 ms as a margin. So if the transmit doesn't
34 * finish before that time something is really wrong and we
37 * This is a sign that something it really wrong and a warning
40 #define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
42 #define call_op(adap, op, arg...) \
43 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
45 #define call_void_op(adap, op, arg...) \
48 adap->ops->op(adap, ## arg); \
51 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
55 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
56 if (adap->log_addrs.log_addr[i] == log_addr)
61 static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
63 int i = cec_log_addr2idx(adap, log_addr);
65 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
68 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
71 unsigned int loc = cec_get_edid_spa_location(edid, size);
76 return CEC_PHYS_ADDR_INVALID;
77 return (edid[loc] << 8) | edid[loc + 1];
79 EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
81 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
82 const struct drm_connector *connector)
84 memset(conn_info, 0, sizeof(*conn_info));
85 conn_info->type = CEC_CONNECTOR_TYPE_DRM;
86 conn_info->drm.card_no = connector->dev->primary->index;
87 conn_info->drm.connector_id = connector->base.id;
89 EXPORT_SYMBOL_GPL(cec_fill_conn_info_from_drm);
92 * Queue a new event for this filehandle. If ts == 0, then set it
93 * to the current time.
95 * We keep a queue of at most max_event events where max_event differs
96 * per event. If the queue becomes full, then drop the oldest event and
97 * keep track of how many events we've dropped.
99 void cec_queue_event_fh(struct cec_fh *fh,
100 const struct cec_event *new_ev, u64 ts)
102 static const u16 max_events[CEC_NUM_EVENTS] = {
103 1, 1, 800, 800, 8, 8, 8, 8
105 struct cec_event_entry *entry;
106 unsigned int ev_idx = new_ev->event - 1;
108 if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
114 mutex_lock(&fh->lock);
115 if (ev_idx < CEC_NUM_CORE_EVENTS)
116 entry = &fh->core_events[ev_idx];
118 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
120 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
121 fh->queued_events[ev_idx]) {
122 entry->ev.lost_msgs.lost_msgs +=
123 new_ev->lost_msgs.lost_msgs;
129 if (fh->queued_events[ev_idx] < max_events[ev_idx]) {
130 /* Add new msg at the end of the queue */
131 list_add_tail(&entry->list, &fh->events[ev_idx]);
132 fh->queued_events[ev_idx]++;
133 fh->total_queued_events++;
137 if (ev_idx >= CEC_NUM_CORE_EVENTS) {
138 list_add_tail(&entry->list, &fh->events[ev_idx]);
139 /* drop the oldest event */
140 entry = list_first_entry(&fh->events[ev_idx],
141 struct cec_event_entry, list);
142 list_del(&entry->list);
146 /* Mark that events were lost */
147 entry = list_first_entry_or_null(&fh->events[ev_idx],
148 struct cec_event_entry, list);
150 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
153 mutex_unlock(&fh->lock);
154 wake_up_interruptible(&fh->wait);
157 /* Queue a new event for all open filehandles. */
158 static void cec_queue_event(struct cec_adapter *adap,
159 const struct cec_event *ev)
161 u64 ts = ktime_get_ns();
164 mutex_lock(&adap->devnode.lock);
165 list_for_each_entry(fh, &adap->devnode.fhs, list)
166 cec_queue_event_fh(fh, ev, ts);
167 mutex_unlock(&adap->devnode.lock);
170 /* Notify userspace that the CEC pin changed state at the given time. */
171 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
172 bool dropped_events, ktime_t ts)
174 struct cec_event ev = {
175 .event = is_high ? CEC_EVENT_PIN_CEC_HIGH :
176 CEC_EVENT_PIN_CEC_LOW,
177 .flags = dropped_events ? CEC_EVENT_FL_DROPPED_EVENTS : 0,
181 mutex_lock(&adap->devnode.lock);
182 list_for_each_entry(fh, &adap->devnode.fhs, list)
183 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
184 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
185 mutex_unlock(&adap->devnode.lock);
187 EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event);
189 /* Notify userspace that the HPD pin changed state at the given time. */
190 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
192 struct cec_event ev = {
193 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH :
194 CEC_EVENT_PIN_HPD_LOW,
198 mutex_lock(&adap->devnode.lock);
199 list_for_each_entry(fh, &adap->devnode.fhs, list)
200 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
201 mutex_unlock(&adap->devnode.lock);
203 EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event);
205 /* Notify userspace that the 5V pin changed state at the given time. */
206 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
208 struct cec_event ev = {
209 .event = is_high ? CEC_EVENT_PIN_5V_HIGH :
210 CEC_EVENT_PIN_5V_LOW,
214 mutex_lock(&adap->devnode.lock);
215 list_for_each_entry(fh, &adap->devnode.fhs, list)
216 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
217 mutex_unlock(&adap->devnode.lock);
219 EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event);
222 * Queue a new message for this filehandle.
224 * We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the
225 * queue becomes full, then drop the oldest message and keep track
226 * of how many messages we've dropped.
228 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
230 static const struct cec_event ev_lost_msgs = {
231 .event = CEC_EVENT_LOST_MSGS,
237 struct cec_msg_entry *entry;
239 mutex_lock(&fh->lock);
240 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
243 /* Add new msg at the end of the queue */
244 list_add_tail(&entry->list, &fh->msgs);
246 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
247 /* All is fine if there is enough room */
249 mutex_unlock(&fh->lock);
250 wake_up_interruptible(&fh->wait);
255 * if the message queue is full, then drop the oldest one and
256 * send a lost message event.
258 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
259 list_del(&entry->list);
262 mutex_unlock(&fh->lock);
265 * We lost a message, either because kmalloc failed or the queue
268 cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
272 * Queue the message for those filehandles that are in monitor mode.
273 * If valid_la is true (this message is for us or was sent by us),
274 * then pass it on to any monitoring filehandle. If this message
275 * isn't for us or from us, then only give it to filehandles that
276 * are in MONITOR_ALL mode.
278 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
279 * set and the CEC adapter was placed in 'monitor all' mode.
281 static void cec_queue_msg_monitor(struct cec_adapter *adap,
282 const struct cec_msg *msg,
286 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
287 CEC_MODE_MONITOR_ALL;
289 mutex_lock(&adap->devnode.lock);
290 list_for_each_entry(fh, &adap->devnode.fhs, list) {
291 if (fh->mode_follower >= monitor_mode)
292 cec_queue_msg_fh(fh, msg);
294 mutex_unlock(&adap->devnode.lock);
298 * Queue the message for follower filehandles.
300 static void cec_queue_msg_followers(struct cec_adapter *adap,
301 const struct cec_msg *msg)
305 mutex_lock(&adap->devnode.lock);
306 list_for_each_entry(fh, &adap->devnode.fhs, list) {
307 if (fh->mode_follower == CEC_MODE_FOLLOWER)
308 cec_queue_msg_fh(fh, msg);
310 mutex_unlock(&adap->devnode.lock);
313 /* Notify userspace of an adapter state change. */
314 static void cec_post_state_event(struct cec_adapter *adap)
316 struct cec_event ev = {
317 .event = CEC_EVENT_STATE_CHANGE,
320 ev.state_change.phys_addr = adap->phys_addr;
321 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
322 ev.state_change.have_conn_info =
323 adap->conn_info.type != CEC_CONNECTOR_TYPE_NO_CONNECTOR;
324 cec_queue_event(adap, &ev);
328 * A CEC transmit (and a possible wait for reply) completed.
329 * If this was in blocking mode, then complete it, otherwise
330 * queue the message for userspace to dequeue later.
332 * This function is called with adap->lock held.
334 static void cec_data_completed(struct cec_data *data)
337 * Delete this transmit from the filehandle's xfer_list since
338 * we're done with it.
340 * Note that if the filehandle is closed before this transmit
341 * finished, then the release() function will set data->fh to NULL.
342 * Without that we would be referring to a closed filehandle.
345 list_del(&data->xfer_list);
347 if (data->blocking) {
349 * Someone is blocking so mark the message as completed
352 data->completed = true;
356 * No blocking, so just queue the message if needed and
360 cec_queue_msg_fh(data->fh, &data->msg);
366 * A pending CEC transmit needs to be cancelled, either because the CEC
367 * adapter is disabled or the transmit takes an impossibly long time to
370 * This function is called with adap->lock held.
372 static void cec_data_cancel(struct cec_data *data, u8 tx_status)
375 * It's either the current transmit, or it is a pending
376 * transmit. Take the appropriate action to clear it.
378 if (data->adap->transmitting == data) {
379 data->adap->transmitting = NULL;
381 list_del_init(&data->list);
382 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
383 if (!WARN_ON(!data->adap->transmit_queue_sz))
384 data->adap->transmit_queue_sz--;
387 if (data->msg.tx_status & CEC_TX_STATUS_OK) {
388 data->msg.rx_ts = ktime_get_ns();
389 data->msg.rx_status = CEC_RX_STATUS_ABORTED;
391 data->msg.tx_ts = ktime_get_ns();
392 data->msg.tx_status |= tx_status |
393 CEC_TX_STATUS_MAX_RETRIES;
394 data->msg.tx_error_cnt++;
398 /* Queue transmitted message for monitoring purposes */
399 cec_queue_msg_monitor(data->adap, &data->msg, 1);
401 cec_data_completed(data);
405 * Flush all pending transmits and cancel any pending timeout work.
407 * This function is called with adap->lock held.
409 static void cec_flush(struct cec_adapter *adap)
411 struct cec_data *data, *n;
414 * If the adapter is disabled, or we're asked to stop,
415 * then cancel any pending transmits.
417 while (!list_empty(&adap->transmit_queue)) {
418 data = list_first_entry(&adap->transmit_queue,
419 struct cec_data, list);
420 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
422 if (adap->transmitting)
423 cec_data_cancel(adap->transmitting, CEC_TX_STATUS_ABORTED);
425 /* Cancel the pending timeout work. */
426 list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
427 if (cancel_delayed_work(&data->work))
428 cec_data_cancel(data, CEC_TX_STATUS_OK);
430 * If cancel_delayed_work returned false, then
431 * the cec_wait_timeout function is running,
432 * which will call cec_data_completed. So no
433 * need to do anything special in that case.
437 * If something went wrong and this counter isn't what it should
438 * be, then this will reset it back to 0. Warn if it is not 0,
439 * since it indicates a bug, either in this framework or in a
442 if (WARN_ON(adap->transmit_queue_sz))
443 adap->transmit_queue_sz = 0;
447 * Main CEC state machine
449 * Wait until the thread should be stopped, or we are not transmitting and
450 * a new transmit message is queued up, in which case we start transmitting
451 * that message. When the adapter finished transmitting the message it will
452 * call cec_transmit_done().
454 * If the adapter is disabled, then remove all queued messages instead.
456 * If the current transmit times out, then cancel that transmit.
458 int cec_thread_func(void *_adap)
460 struct cec_adapter *adap = _adap;
463 unsigned int signal_free_time;
464 struct cec_data *data;
465 bool timeout = false;
468 if (adap->transmitting) {
472 * We are transmitting a message, so add a timeout
473 * to prevent the state machine to get stuck waiting
474 * for this message to finalize and add a check to
475 * see if the adapter is disabled in which case the
476 * transmit should be canceled.
478 err = wait_event_interruptible_timeout(adap->kthread_waitq,
480 (!adap->is_configured && !adap->is_configuring)) ||
481 kthread_should_stop() ||
482 (!adap->transmit_in_progress &&
483 !list_empty(&adap->transmit_queue)),
484 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
487 /* Otherwise we just wait for something to happen. */
488 wait_event_interruptible(adap->kthread_waitq,
489 kthread_should_stop() ||
490 (!adap->transmit_in_progress &&
491 !list_empty(&adap->transmit_queue)));
494 mutex_lock(&adap->lock);
496 if ((adap->needs_hpd &&
497 (!adap->is_configured && !adap->is_configuring)) ||
498 kthread_should_stop()) {
503 if (adap->transmitting && timeout) {
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 CEC_XFER_TIMEOUT_MS (2.1s).
512 pr_warn("cec-%s: message %*ph timed out\n", adap->name,
513 adap->transmitting->msg.len,
514 adap->transmitting->msg.msg);
515 adap->transmit_in_progress = false;
517 /* Just give up on this. */
518 cec_data_cancel(adap->transmitting,
519 CEC_TX_STATUS_TIMEOUT);
524 * If we are still transmitting, or there is nothing new to
525 * transmit, then just continue waiting.
527 if (adap->transmit_in_progress || list_empty(&adap->transmit_queue))
530 /* Get a new message to transmit */
531 data = list_first_entry(&adap->transmit_queue,
532 struct cec_data, list);
533 list_del_init(&data->list);
534 if (!WARN_ON(!data->adap->transmit_queue_sz))
535 adap->transmit_queue_sz--;
537 /* Make this the current transmitting message */
538 adap->transmitting = data;
541 * Suggested number of attempts as per the CEC 2.0 spec:
542 * 4 attempts is the default, except for 'secondary poll
543 * messages', i.e. poll messages not sent during the adapter
544 * configuration phase when it allocates logical addresses.
546 if (data->msg.len == 1 && adap->is_configured)
551 /* Set the suggested signal free time */
552 if (data->attempts) {
553 /* should be >= 3 data bit periods for a retry */
554 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
555 } else if (adap->last_initiator !=
556 cec_msg_initiator(&data->msg)) {
557 /* should be >= 5 data bit periods for new initiator */
558 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
559 adap->last_initiator = cec_msg_initiator(&data->msg);
562 * should be >= 7 data bit periods for sending another
563 * frame immediately after another.
565 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
567 if (data->attempts == 0)
568 data->attempts = attempts;
570 /* Tell the adapter to transmit, cancel on error */
571 if (adap->ops->adap_transmit(adap, data->attempts,
572 signal_free_time, &data->msg))
573 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
575 adap->transmit_in_progress = true;
578 mutex_unlock(&adap->lock);
580 if (kthread_should_stop())
587 * Called by the CEC adapter if a transmit finished.
589 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
590 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
591 u8 error_cnt, ktime_t ts)
593 struct cec_data *data;
595 unsigned int attempts_made = arb_lost_cnt + nack_cnt +
596 low_drive_cnt + error_cnt;
598 dprintk(2, "%s: status 0x%02x\n", __func__, status);
599 if (attempts_made < 1)
602 mutex_lock(&adap->lock);
603 data = adap->transmitting;
606 * This might happen if a transmit was issued and the cable is
607 * unplugged while the transmit is ongoing. Ignore this
608 * transmit in that case.
610 if (!adap->transmit_in_progress)
611 dprintk(1, "%s was called without an ongoing transmit!\n",
613 adap->transmit_in_progress = false;
616 adap->transmit_in_progress = false;
620 /* Drivers must fill in the status! */
621 WARN_ON(status == 0);
622 msg->tx_ts = ktime_to_ns(ts);
623 msg->tx_status |= status;
624 msg->tx_arb_lost_cnt += arb_lost_cnt;
625 msg->tx_nack_cnt += nack_cnt;
626 msg->tx_low_drive_cnt += low_drive_cnt;
627 msg->tx_error_cnt += error_cnt;
629 /* Mark that we're done with this transmit */
630 adap->transmitting = NULL;
633 * If there are still retry attempts left and there was an error and
634 * the hardware didn't signal that it retried itself (by setting
635 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
637 if (data->attempts > attempts_made &&
638 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
639 /* Retry this message */
640 data->attempts -= attempts_made;
642 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
643 msg->len, msg->msg, data->attempts, msg->reply);
645 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
646 msg->len, msg->msg, data->attempts);
647 /* Add the message in front of the transmit queue */
648 list_add(&data->list, &adap->transmit_queue);
649 adap->transmit_queue_sz++;
655 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
656 if (!(status & CEC_TX_STATUS_OK))
657 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
659 /* Queue transmitted message for monitoring purposes */
660 cec_queue_msg_monitor(adap, msg, 1);
662 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
665 * Queue the message into the wait queue if we want to wait
668 list_add_tail(&data->list, &adap->wait_queue);
669 schedule_delayed_work(&data->work,
670 msecs_to_jiffies(msg->timeout));
672 /* Otherwise we're done */
673 cec_data_completed(data);
678 * Wake up the main thread to see if another message is ready
679 * for transmitting or to retry the current message.
681 wake_up_interruptible(&adap->kthread_waitq);
682 mutex_unlock(&adap->lock);
684 EXPORT_SYMBOL_GPL(cec_transmit_done_ts);
686 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
687 u8 status, ktime_t ts)
689 switch (status & ~CEC_TX_STATUS_MAX_RETRIES) {
690 case CEC_TX_STATUS_OK:
691 cec_transmit_done_ts(adap, status, 0, 0, 0, 0, ts);
693 case CEC_TX_STATUS_ARB_LOST:
694 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts);
696 case CEC_TX_STATUS_NACK:
697 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts);
699 case CEC_TX_STATUS_LOW_DRIVE:
700 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts);
702 case CEC_TX_STATUS_ERROR:
703 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts);
706 /* Should never happen */
707 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
711 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts);
714 * Called when waiting for a reply times out.
716 static void cec_wait_timeout(struct work_struct *work)
718 struct cec_data *data = container_of(work, struct cec_data, work.work);
719 struct cec_adapter *adap = data->adap;
721 mutex_lock(&adap->lock);
723 * Sanity check in case the timeout and the arrival of the message
724 * happened at the same time.
726 if (list_empty(&data->list))
729 /* Mark the message as timed out */
730 list_del_init(&data->list);
731 data->msg.rx_ts = ktime_get_ns();
732 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
733 cec_data_completed(data);
735 mutex_unlock(&adap->lock);
739 * Transmit a message. The fh argument may be NULL if the transmit is not
740 * associated with a specific filehandle.
742 * This function is called with adap->lock held.
744 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
745 struct cec_fh *fh, bool block)
747 struct cec_data *data;
748 bool is_raw = msg_is_raw(msg);
754 msg->tx_arb_lost_cnt = 0;
755 msg->tx_nack_cnt = 0;
756 msg->tx_low_drive_cnt = 0;
757 msg->tx_error_cnt = 0;
760 if (msg->reply && msg->timeout == 0) {
761 /* Make sure the timeout isn't 0. */
764 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS | CEC_MSG_FL_RAW;
767 msg->flags &= ~CEC_MSG_FL_REPLY_TO_FOLLOWERS;
770 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
771 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
775 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
778 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
779 __func__, msg->len, msg->msg, msg->reply,
780 !block ? ", nb" : "");
782 dprintk(2, "%s: %*ph%s\n",
783 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
785 if (msg->timeout && msg->len == 1) {
786 dprintk(1, "%s: can't reply to poll msg\n", __func__);
791 if (!capable(CAP_SYS_RAWIO))
794 /* A CDC-Only device can only send CDC messages */
795 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
796 (msg->len == 1 || msg->msg[1] != CEC_MSG_CDC_MESSAGE)) {
797 dprintk(1, "%s: not a CDC message\n", __func__);
801 if (msg->len >= 4 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
802 msg->msg[2] = adap->phys_addr >> 8;
803 msg->msg[3] = adap->phys_addr & 0xff;
807 if (cec_msg_destination(msg) == 0xf) {
808 dprintk(1, "%s: invalid poll message\n",
812 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
814 * If the destination is a logical address our
815 * adapter has already claimed, then just NACK
816 * this. It depends on the hardware what it will
817 * do with a POLL to itself (some OK this), so
818 * it is just as easy to handle it here so the
819 * behavior will be consistent.
821 msg->tx_ts = ktime_get_ns();
822 msg->tx_status = CEC_TX_STATUS_NACK |
823 CEC_TX_STATUS_MAX_RETRIES;
824 msg->tx_nack_cnt = 1;
825 msg->sequence = ++adap->sequence;
827 msg->sequence = ++adap->sequence;
831 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
832 cec_has_log_addr(adap, cec_msg_destination(msg))) {
833 dprintk(1, "%s: destination is the adapter itself\n",
837 if (msg->len > 1 && adap->is_configured &&
838 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
839 dprintk(1, "%s: initiator has unknown logical address %d\n",
840 __func__, cec_msg_initiator(msg));
844 * Special case: allow Ping and IMAGE/TEXT_VIEW_ON to be
845 * transmitted to a TV, even if the adapter is unconfigured.
846 * This makes it possible to detect or wake up displays that
847 * pull down the HPD when in standby.
849 if (!adap->is_configured && !adap->is_configuring &&
851 cec_msg_destination(msg) != CEC_LOG_ADDR_TV ||
852 (msg->len == 2 && msg->msg[1] != CEC_MSG_IMAGE_VIEW_ON &&
853 msg->msg[1] != CEC_MSG_TEXT_VIEW_ON))) {
854 dprintk(1, "%s: adapter is unconfigured\n", __func__);
859 if (!adap->is_configured && !adap->is_configuring) {
860 if (adap->needs_hpd) {
861 dprintk(1, "%s: adapter is unconfigured and needs HPD\n",
866 dprintk(1, "%s: invalid msg->reply\n", __func__);
871 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
872 dprintk(2, "%s: transmit queue full\n", __func__);
876 data = kzalloc(sizeof(*data), GFP_KERNEL);
880 msg->sequence = ++adap->sequence;
882 msg->sequence = ++adap->sequence;
887 data->blocking = block;
889 init_completion(&data->c);
890 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
893 list_add_tail(&data->xfer_list, &fh->xfer_list);
895 list_add_tail(&data->list, &adap->transmit_queue);
896 adap->transmit_queue_sz++;
897 if (!adap->transmitting)
898 wake_up_interruptible(&adap->kthread_waitq);
900 /* All done if we don't need to block waiting for completion */
905 * Release the lock and wait, retake the lock afterwards.
907 mutex_unlock(&adap->lock);
908 wait_for_completion_killable(&data->c);
909 if (!data->completed)
910 cancel_delayed_work_sync(&data->work);
911 mutex_lock(&adap->lock);
913 /* Cancel the transmit if it was interrupted */
914 if (!data->completed)
915 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
917 /* The transmit completed (possibly with an error) */
923 /* Helper function to be used by drivers and this framework. */
924 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
929 mutex_lock(&adap->lock);
930 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
931 mutex_unlock(&adap->lock);
934 EXPORT_SYMBOL_GPL(cec_transmit_msg);
937 * I don't like forward references but without this the low-level
938 * cec_received_msg() function would come after a bunch of high-level
939 * CEC protocol handling functions. That was very confusing.
941 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
944 #define DIRECTED 0x80
945 #define BCAST1_4 0x40
946 #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
947 #define BCAST (BCAST1_4 | BCAST2_0)
948 #define BOTH (BCAST | DIRECTED)
951 * Specify minimum length and whether the message is directed, broadcast
952 * or both. Messages that do not match the criteria are ignored as per
953 * the CEC specification.
955 static const u8 cec_msg_size[256] = {
956 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
957 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
958 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
959 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
960 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
961 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
962 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
963 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
964 [CEC_MSG_STANDBY] = 2 | BOTH,
965 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
966 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
967 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
968 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
969 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
970 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
971 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
972 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
973 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
974 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
975 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
976 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
977 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
978 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
979 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
980 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
981 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
982 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
983 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
984 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
985 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
986 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
987 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
988 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
989 [CEC_MSG_PLAY] = 3 | DIRECTED,
990 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
991 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
992 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
993 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
994 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
995 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
996 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
997 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
998 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
999 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
1000 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
1001 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
1002 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
1003 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
1004 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
1005 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
1006 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
1007 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
1008 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
1009 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
1010 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
1011 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
1012 [CEC_MSG_ABORT] = 2 | DIRECTED,
1013 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
1014 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
1015 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
1016 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
1017 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
1018 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
1019 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
1020 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
1021 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
1022 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
1023 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
1024 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
1025 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
1026 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
1027 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
1028 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
1029 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
1030 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
1033 /* Called by the CEC adapter if a message is received */
1034 void cec_received_msg_ts(struct cec_adapter *adap,
1035 struct cec_msg *msg, ktime_t ts)
1037 struct cec_data *data;
1038 u8 msg_init = cec_msg_initiator(msg);
1039 u8 msg_dest = cec_msg_destination(msg);
1040 u8 cmd = msg->msg[1];
1041 bool is_reply = false;
1042 bool valid_la = true;
1045 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
1049 * Some CEC adapters will receive the messages that they transmitted.
1050 * This test filters out those messages by checking if we are the
1051 * initiator, and just returning in that case.
1053 * Note that this won't work if this is an Unregistered device.
1055 * It is bad practice if the hardware receives the message that it
1056 * transmitted and luckily most CEC adapters behave correctly in this
1059 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
1060 cec_has_log_addr(adap, msg_init))
1063 msg->rx_ts = ktime_to_ns(ts);
1064 msg->rx_status = CEC_RX_STATUS_OK;
1065 msg->sequence = msg->reply = msg->timeout = 0;
1068 msg->tx_arb_lost_cnt = 0;
1069 msg->tx_nack_cnt = 0;
1070 msg->tx_low_drive_cnt = 0;
1071 msg->tx_error_cnt = 0;
1073 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
1075 mutex_lock(&adap->lock);
1076 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1078 adap->last_initiator = 0xff;
1080 /* Check if this message was for us (directed or broadcast). */
1081 if (!cec_msg_is_broadcast(msg))
1082 valid_la = cec_has_log_addr(adap, msg_dest);
1085 * Check if the length is not too short or if the message is a
1086 * broadcast message where a directed message was expected or
1087 * vice versa. If so, then the message has to be ignored (according
1088 * to section CEC 7.3 and CEC 12.2).
1090 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
1091 u8 dir_fl = cec_msg_size[cmd] & BOTH;
1093 min_len = cec_msg_size[cmd] & 0x1f;
1094 if (msg->len < min_len)
1096 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
1098 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST))
1100 else if (cec_msg_is_broadcast(msg) &&
1101 adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0 &&
1102 !(dir_fl & BCAST1_4))
1105 if (valid_la && min_len) {
1106 /* These messages have special length requirements */
1108 case CEC_MSG_TIMER_STATUS:
1109 if (msg->msg[2] & 0x10) {
1110 switch (msg->msg[2] & 0xf) {
1111 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
1112 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
1117 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
1122 case CEC_MSG_RECORD_ON:
1123 switch (msg->msg[2]) {
1124 case CEC_OP_RECORD_SRC_OWN:
1126 case CEC_OP_RECORD_SRC_DIGITAL:
1130 case CEC_OP_RECORD_SRC_ANALOG:
1134 case CEC_OP_RECORD_SRC_EXT_PLUG:
1138 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1147 /* It's a valid message and not a poll or CDC message */
1148 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1149 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1151 /* The aborted command is in msg[2] */
1156 * Walk over all transmitted messages that are waiting for a
1159 list_for_each_entry(data, &adap->wait_queue, list) {
1160 struct cec_msg *dst = &data->msg;
1163 * The *only* CEC message that has two possible replies
1164 * is CEC_MSG_INITIATE_ARC.
1165 * In this case allow either of the two replies.
1167 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1168 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1169 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1170 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1171 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1174 /* Does the command match? */
1175 if ((abort && cmd != dst->msg[1]) ||
1176 (!abort && cmd != dst->reply))
1179 /* Does the addressing match? */
1180 if (msg_init != cec_msg_destination(dst) &&
1181 !cec_msg_is_broadcast(dst))
1184 /* We got a reply */
1185 memcpy(dst->msg, msg->msg, msg->len);
1186 dst->len = msg->len;
1187 dst->rx_ts = msg->rx_ts;
1188 dst->rx_status = msg->rx_status;
1190 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1191 msg->flags = dst->flags;
1192 /* Remove it from the wait_queue */
1193 list_del_init(&data->list);
1195 /* Cancel the pending timeout work */
1196 if (!cancel_delayed_work(&data->work)) {
1197 mutex_unlock(&adap->lock);
1198 flush_scheduled_work();
1199 mutex_lock(&adap->lock);
1202 * Mark this as a reply, provided someone is still
1203 * waiting for the answer.
1207 cec_data_completed(data);
1211 mutex_unlock(&adap->lock);
1213 /* Pass the message on to any monitoring filehandles */
1214 cec_queue_msg_monitor(adap, msg, valid_la);
1216 /* We're done if it is not for us or a poll message */
1217 if (!valid_la || msg->len <= 1)
1220 if (adap->log_addrs.log_addr_mask == 0)
1224 * Process the message on the protocol level. If is_reply is true,
1225 * then cec_receive_notify() won't pass on the reply to the listener(s)
1226 * since that was already done by cec_data_completed() above.
1228 cec_receive_notify(adap, msg, is_reply);
1230 EXPORT_SYMBOL_GPL(cec_received_msg_ts);
1232 /* Logical Address Handling */
1235 * Attempt to claim a specific logical address.
1237 * This function is called with adap->lock held.
1239 static int cec_config_log_addr(struct cec_adapter *adap,
1241 unsigned int log_addr)
1243 struct cec_log_addrs *las = &adap->log_addrs;
1244 struct cec_msg msg = { };
1245 const unsigned int max_retries = 2;
1249 if (cec_has_log_addr(adap, log_addr))
1252 /* Send poll message */
1254 msg.msg[0] = (log_addr << 4) | log_addr;
1256 for (i = 0; i < max_retries; i++) {
1257 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1260 * While trying to poll the physical address was reset
1261 * and the adapter was unconfigured, so bail out.
1263 if (!adap->is_configuring)
1270 * The message was aborted due to a disconnect or
1271 * unconfigure, just bail out.
1273 if (msg.tx_status & CEC_TX_STATUS_ABORTED)
1275 if (msg.tx_status & CEC_TX_STATUS_OK)
1277 if (msg.tx_status & CEC_TX_STATUS_NACK)
1280 * Retry up to max_retries times if the message was neither
1281 * OKed or NACKed. This can happen due to e.g. a Lost
1282 * Arbitration condition.
1287 * If we are unable to get an OK or a NACK after max_retries attempts
1288 * (and note that each attempt already consists of four polls), then
1289 * then we assume that something is really weird and that it is not a
1290 * good idea to try and claim this logical address.
1292 if (i == max_retries)
1296 * Message not acknowledged, so this logical
1297 * address is free to use.
1299 err = adap->ops->adap_log_addr(adap, log_addr);
1303 las->log_addr[idx] = log_addr;
1304 las->log_addr_mask |= 1 << log_addr;
1305 adap->phys_addrs[log_addr] = adap->phys_addr;
1310 * Unconfigure the adapter: clear all logical addresses and send
1311 * the state changed event.
1313 * This function is called with adap->lock held.
1315 static void cec_adap_unconfigure(struct cec_adapter *adap)
1317 if (!adap->needs_hpd ||
1318 adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1319 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1320 adap->log_addrs.log_addr_mask = 0;
1321 adap->is_configuring = false;
1322 adap->is_configured = false;
1323 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1325 wake_up_interruptible(&adap->kthread_waitq);
1326 cec_post_state_event(adap);
1330 * Attempt to claim the required logical addresses.
1332 static int cec_config_thread_func(void *arg)
1334 /* The various LAs for each type of device */
1335 static const u8 tv_log_addrs[] = {
1336 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1337 CEC_LOG_ADDR_INVALID
1339 static const u8 record_log_addrs[] = {
1340 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1341 CEC_LOG_ADDR_RECORD_3,
1342 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1343 CEC_LOG_ADDR_INVALID
1345 static const u8 tuner_log_addrs[] = {
1346 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1347 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1348 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1349 CEC_LOG_ADDR_INVALID
1351 static const u8 playback_log_addrs[] = {
1352 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1353 CEC_LOG_ADDR_PLAYBACK_3,
1354 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1355 CEC_LOG_ADDR_INVALID
1357 static const u8 audiosystem_log_addrs[] = {
1358 CEC_LOG_ADDR_AUDIOSYSTEM,
1359 CEC_LOG_ADDR_INVALID
1361 static const u8 specific_use_log_addrs[] = {
1362 CEC_LOG_ADDR_SPECIFIC,
1363 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1364 CEC_LOG_ADDR_INVALID
1366 static const u8 *type2addrs[6] = {
1367 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1368 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1369 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1370 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1371 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1372 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1374 static const u16 type2mask[] = {
1375 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1376 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1377 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1378 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1379 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1380 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1382 struct cec_adapter *adap = arg;
1383 struct cec_log_addrs *las = &adap->log_addrs;
1387 mutex_lock(&adap->lock);
1388 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1389 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1390 las->log_addr_mask = 0;
1392 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1395 for (i = 0; i < las->num_log_addrs; i++) {
1396 unsigned int type = las->log_addr_type[i];
1401 * The TV functionality can only map to physical address 0.
1402 * For any other address, try the Specific functionality
1403 * instead as per the spec.
1405 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1406 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1408 la_list = type2addrs[type];
1409 last_la = las->log_addr[i];
1410 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1411 if (last_la == CEC_LOG_ADDR_INVALID ||
1412 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1413 !((1 << last_la) & type2mask[type]))
1414 last_la = la_list[0];
1416 err = cec_config_log_addr(adap, i, last_la);
1417 if (err > 0) /* Reused last LA */
1423 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1424 /* Tried this one already, skip it */
1425 if (la_list[j] == last_la)
1427 /* The backup addresses are CEC 2.0 specific */
1428 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1429 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1430 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1433 err = cec_config_log_addr(adap, i, la_list[j]);
1434 if (err == 0) /* LA is in use */
1438 /* Done, claimed an LA */
1442 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1443 dprintk(1, "could not claim LA %d\n", i);
1446 if (adap->log_addrs.log_addr_mask == 0 &&
1447 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1451 if (adap->log_addrs.log_addr_mask == 0) {
1452 /* Fall back to unregistered */
1453 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1454 las->log_addr_mask = 1 << las->log_addr[0];
1455 for (i = 1; i < las->num_log_addrs; i++)
1456 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1458 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1459 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1460 adap->is_configured = true;
1461 adap->is_configuring = false;
1462 cec_post_state_event(adap);
1465 * Now post the Report Features and Report Physical Address broadcast
1466 * messages. Note that these are non-blocking transmits, meaning that
1467 * they are just queued up and once adap->lock is unlocked the main
1468 * thread will kick in and start transmitting these.
1470 * If after this function is done (but before one or more of these
1471 * messages are actually transmitted) the CEC adapter is unconfigured,
1472 * then any remaining messages will be dropped by the main thread.
1474 for (i = 0; i < las->num_log_addrs; i++) {
1475 struct cec_msg msg = {};
1477 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1478 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1481 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1483 /* Report Features must come first according to CEC 2.0 */
1484 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1485 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1486 cec_fill_msg_report_features(adap, &msg, i);
1487 cec_transmit_msg_fh(adap, &msg, NULL, false);
1490 /* Report Physical Address */
1491 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1492 las->primary_device_type[i]);
1493 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1495 cec_phys_addr_exp(adap->phys_addr));
1496 cec_transmit_msg_fh(adap, &msg, NULL, false);
1498 /* Report Vendor ID */
1499 if (adap->log_addrs.vendor_id != CEC_VENDOR_ID_NONE) {
1500 cec_msg_device_vendor_id(&msg,
1501 adap->log_addrs.vendor_id);
1502 cec_transmit_msg_fh(adap, &msg, NULL, false);
1505 adap->kthread_config = NULL;
1506 complete(&adap->config_completion);
1507 mutex_unlock(&adap->lock);
1511 for (i = 0; i < las->num_log_addrs; i++)
1512 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1513 cec_adap_unconfigure(adap);
1514 adap->kthread_config = NULL;
1515 mutex_unlock(&adap->lock);
1516 complete(&adap->config_completion);
1521 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1522 * logical addresses.
1524 * This function is called with adap->lock held.
1526 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1528 if (WARN_ON(adap->is_configuring || adap->is_configured))
1531 init_completion(&adap->config_completion);
1533 /* Ready to kick off the thread */
1534 adap->is_configuring = true;
1535 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1536 "ceccfg-%s", adap->name);
1537 if (IS_ERR(adap->kthread_config)) {
1538 adap->kthread_config = NULL;
1540 mutex_unlock(&adap->lock);
1541 wait_for_completion(&adap->config_completion);
1542 mutex_lock(&adap->lock);
1546 /* Set a new physical address and send an event notifying userspace of this.
1548 * This function is called with adap->lock held.
1550 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1552 if (phys_addr == adap->phys_addr)
1554 if (phys_addr != CEC_PHYS_ADDR_INVALID && adap->devnode.unregistered)
1557 dprintk(1, "new physical address %x.%x.%x.%x\n",
1558 cec_phys_addr_exp(phys_addr));
1559 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1560 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1561 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1562 cec_post_state_event(adap);
1563 cec_adap_unconfigure(adap);
1564 /* Disabling monitor all mode should always succeed */
1565 if (adap->monitor_all_cnt)
1566 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1567 mutex_lock(&adap->devnode.lock);
1568 if (adap->needs_hpd || list_empty(&adap->devnode.fhs)) {
1569 WARN_ON(adap->ops->adap_enable(adap, false));
1570 adap->transmit_in_progress = false;
1571 wake_up_interruptible(&adap->kthread_waitq);
1573 mutex_unlock(&adap->devnode.lock);
1574 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1578 mutex_lock(&adap->devnode.lock);
1579 adap->last_initiator = 0xff;
1580 adap->transmit_in_progress = false;
1582 if ((adap->needs_hpd || list_empty(&adap->devnode.fhs)) &&
1583 adap->ops->adap_enable(adap, true)) {
1584 mutex_unlock(&adap->devnode.lock);
1588 if (adap->monitor_all_cnt &&
1589 call_op(adap, adap_monitor_all_enable, true)) {
1590 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1591 WARN_ON(adap->ops->adap_enable(adap, false));
1592 mutex_unlock(&adap->devnode.lock);
1595 mutex_unlock(&adap->devnode.lock);
1597 adap->phys_addr = phys_addr;
1598 cec_post_state_event(adap);
1599 if (adap->log_addrs.num_log_addrs)
1600 cec_claim_log_addrs(adap, block);
1603 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1605 if (IS_ERR_OR_NULL(adap))
1608 mutex_lock(&adap->lock);
1609 __cec_s_phys_addr(adap, phys_addr, block);
1610 mutex_unlock(&adap->lock);
1612 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1614 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1615 const struct edid *edid)
1617 u16 pa = CEC_PHYS_ADDR_INVALID;
1619 if (edid && edid->extensions)
1620 pa = cec_get_edid_phys_addr((const u8 *)edid,
1621 EDID_LENGTH * (edid->extensions + 1), NULL);
1622 cec_s_phys_addr(adap, pa, false);
1624 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1626 void cec_s_conn_info(struct cec_adapter *adap,
1627 const struct cec_connector_info *conn_info)
1629 if (IS_ERR_OR_NULL(adap))
1632 if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO))
1635 mutex_lock(&adap->lock);
1637 adap->conn_info = *conn_info;
1639 memset(&adap->conn_info, 0, sizeof(adap->conn_info));
1640 cec_post_state_event(adap);
1641 mutex_unlock(&adap->lock);
1643 EXPORT_SYMBOL_GPL(cec_s_conn_info);
1646 * Called from either the ioctl or a driver to set the logical addresses.
1648 * This function is called with adap->lock held.
1650 int __cec_s_log_addrs(struct cec_adapter *adap,
1651 struct cec_log_addrs *log_addrs, bool block)
1656 if (adap->devnode.unregistered)
1659 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1660 cec_adap_unconfigure(adap);
1661 adap->log_addrs.num_log_addrs = 0;
1662 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++)
1663 adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID;
1664 adap->log_addrs.osd_name[0] = '\0';
1665 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
1666 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
1670 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1672 * Sanitize log_addrs fields if a CDC-Only device is
1675 log_addrs->num_log_addrs = 1;
1676 log_addrs->osd_name[0] = '\0';
1677 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1678 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1680 * This is just an internal convention since a CDC-Only device
1681 * doesn't have to be a switch. But switches already use
1682 * unregistered, so it makes some kind of sense to pick this
1683 * as the primary device. Since a CDC-Only device never sends
1684 * any 'normal' CEC messages this primary device type is never
1685 * sent over the CEC bus.
1687 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1688 log_addrs->all_device_types[0] = 0;
1689 log_addrs->features[0][0] = 0;
1690 log_addrs->features[0][1] = 0;
1693 /* Ensure the osd name is 0-terminated */
1694 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1697 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1698 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1703 * Vendor ID is a 24 bit number, so check if the value is
1704 * within the correct range.
1706 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1707 (log_addrs->vendor_id & 0xff000000) != 0) {
1708 dprintk(1, "invalid vendor ID\n");
1712 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1713 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1714 dprintk(1, "invalid CEC version\n");
1718 if (log_addrs->num_log_addrs > 1)
1719 for (i = 0; i < log_addrs->num_log_addrs; i++)
1720 if (log_addrs->log_addr_type[i] ==
1721 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1722 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1726 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1727 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1728 u8 *features = log_addrs->features[i];
1729 bool op_is_dev_features = false;
1732 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1733 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1734 dprintk(1, "duplicate logical address type\n");
1737 type_mask |= 1 << log_addrs->log_addr_type[i];
1738 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1739 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1740 /* Record already contains the playback functionality */
1741 dprintk(1, "invalid record + playback combination\n");
1744 if (log_addrs->primary_device_type[i] >
1745 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1746 dprintk(1, "unknown primary device type\n");
1749 if (log_addrs->primary_device_type[i] == 2) {
1750 dprintk(1, "invalid primary device type\n");
1753 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1754 dprintk(1, "unknown logical address type\n");
1757 for (j = 0; j < feature_sz; j++) {
1758 if ((features[j] & 0x80) == 0) {
1759 if (op_is_dev_features)
1761 op_is_dev_features = true;
1764 if (!op_is_dev_features || j == feature_sz) {
1765 dprintk(1, "malformed features\n");
1768 /* Zero unused part of the feature array */
1769 memset(features + j + 1, 0, feature_sz - j - 1);
1772 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1773 if (log_addrs->num_log_addrs > 2) {
1774 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1777 if (log_addrs->num_log_addrs == 2) {
1778 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1779 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1780 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1783 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1784 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1785 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1791 /* Zero unused LAs */
1792 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1793 log_addrs->primary_device_type[i] = 0;
1794 log_addrs->log_addr_type[i] = 0;
1795 log_addrs->all_device_types[i] = 0;
1796 memset(log_addrs->features[i], 0,
1797 sizeof(log_addrs->features[i]));
1800 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1801 adap->log_addrs = *log_addrs;
1802 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1803 cec_claim_log_addrs(adap, block);
1807 int cec_s_log_addrs(struct cec_adapter *adap,
1808 struct cec_log_addrs *log_addrs, bool block)
1812 mutex_lock(&adap->lock);
1813 err = __cec_s_log_addrs(adap, log_addrs, block);
1814 mutex_unlock(&adap->lock);
1817 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1819 /* High-level core CEC message handling */
1821 /* Fill in the Report Features message */
1822 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1823 struct cec_msg *msg,
1824 unsigned int la_idx)
1826 const struct cec_log_addrs *las = &adap->log_addrs;
1827 const u8 *features = las->features[la_idx];
1828 bool op_is_dev_features = false;
1831 /* Report Features */
1832 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1834 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1835 msg->msg[2] = adap->log_addrs.cec_version;
1836 msg->msg[3] = las->all_device_types[la_idx];
1838 /* Write RC Profiles first, then Device Features */
1839 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1840 msg->msg[msg->len++] = features[idx];
1841 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1842 if (op_is_dev_features)
1844 op_is_dev_features = true;
1849 /* Transmit the Feature Abort message */
1850 static int cec_feature_abort_reason(struct cec_adapter *adap,
1851 struct cec_msg *msg, u8 reason)
1853 struct cec_msg tx_msg = { };
1856 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1859 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1861 /* Don't Feature Abort messages from 'Unregistered' */
1862 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1864 cec_msg_set_reply_to(&tx_msg, msg);
1865 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1866 return cec_transmit_msg(adap, &tx_msg, false);
1869 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1871 return cec_feature_abort_reason(adap, msg,
1872 CEC_OP_ABORT_UNRECOGNIZED_OP);
1875 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1877 return cec_feature_abort_reason(adap, msg,
1878 CEC_OP_ABORT_REFUSED);
1882 * Called when a CEC message is received. This function will do any
1883 * necessary core processing. The is_reply bool is true if this message
1884 * is a reply to an earlier transmit.
1886 * The message is either a broadcast message or a valid directed message.
1888 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1891 bool is_broadcast = cec_msg_is_broadcast(msg);
1892 u8 dest_laddr = cec_msg_destination(msg);
1893 u8 init_laddr = cec_msg_initiator(msg);
1894 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1895 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1896 bool from_unregistered = init_laddr == 0xf;
1897 struct cec_msg tx_cec_msg = { };
1899 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1901 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1902 if (cec_is_cdc_only(&adap->log_addrs) &&
1903 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1906 if (adap->ops->received) {
1907 /* Allow drivers to process the message first */
1908 if (adap->ops->received(adap, msg) != -ENOMSG)
1913 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1914 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1915 * handled by the CEC core, even if the passthrough mode is on.
1916 * The others are just ignored if passthrough mode is on.
1918 switch (msg->msg[1]) {
1919 case CEC_MSG_GET_CEC_VERSION:
1921 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1922 case CEC_MSG_GIVE_OSD_NAME:
1924 * These messages reply with a directed message, so ignore if
1925 * the initiator is Unregistered.
1927 if (!adap->passthrough && from_unregistered)
1930 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1931 case CEC_MSG_GIVE_FEATURES:
1932 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1934 * Skip processing these messages if the passthrough mode
1937 if (adap->passthrough)
1938 goto skip_processing;
1939 /* Ignore if addressing is wrong */
1944 case CEC_MSG_USER_CONTROL_PRESSED:
1945 case CEC_MSG_USER_CONTROL_RELEASED:
1946 /* Wrong addressing mode: don't process */
1947 if (is_broadcast || from_unregistered)
1948 goto skip_processing;
1951 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1953 * This message is always processed, regardless of the
1954 * passthrough setting.
1956 * Exception: don't process if wrong addressing mode.
1959 goto skip_processing;
1966 cec_msg_set_reply_to(&tx_cec_msg, msg);
1968 switch (msg->msg[1]) {
1969 /* The following messages are processed but still passed through */
1970 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1971 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1973 if (!from_unregistered)
1974 adap->phys_addrs[init_laddr] = pa;
1975 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
1976 cec_phys_addr_exp(pa), init_laddr);
1980 case CEC_MSG_USER_CONTROL_PRESSED:
1981 if (!(adap->capabilities & CEC_CAP_RC) ||
1982 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1985 #ifdef CONFIG_MEDIA_CEC_RC
1986 switch (msg->msg[2]) {
1988 * Play function, this message can have variable length
1989 * depending on the specific play function that is used.
1991 case CEC_OP_UI_CMD_PLAY_FUNCTION:
1993 rc_keydown(adap->rc, RC_PROTO_CEC,
1996 rc_keydown(adap->rc, RC_PROTO_CEC,
1997 msg->msg[2] << 8 | msg->msg[3], 0);
2000 * Other function messages that are not handled.
2001 * Currently the RC framework does not allow to supply an
2002 * additional parameter to a keypress. These "keys" contain
2003 * other information such as channel number, an input number
2005 * For the time being these messages are not processed by the
2006 * framework and are simply forwarded to the user space.
2008 case CEC_OP_UI_CMD_SELECT_BROADCAST_TYPE:
2009 case CEC_OP_UI_CMD_SELECT_SOUND_PRESENTATION:
2010 case CEC_OP_UI_CMD_TUNE_FUNCTION:
2011 case CEC_OP_UI_CMD_SELECT_MEDIA_FUNCTION:
2012 case CEC_OP_UI_CMD_SELECT_AV_INPUT_FUNCTION:
2013 case CEC_OP_UI_CMD_SELECT_AUDIO_INPUT_FUNCTION:
2016 rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0);
2022 case CEC_MSG_USER_CONTROL_RELEASED:
2023 if (!(adap->capabilities & CEC_CAP_RC) ||
2024 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
2026 #ifdef CONFIG_MEDIA_CEC_RC
2032 * The remaining messages are only processed if the passthrough mode
2035 case CEC_MSG_GET_CEC_VERSION:
2036 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
2037 return cec_transmit_msg(adap, &tx_cec_msg, false);
2039 case CEC_MSG_GIVE_PHYSICAL_ADDR:
2040 /* Do nothing for CEC switches using addr 15 */
2041 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
2043 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
2044 return cec_transmit_msg(adap, &tx_cec_msg, false);
2046 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
2047 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
2048 return cec_feature_abort(adap, msg);
2049 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
2050 return cec_transmit_msg(adap, &tx_cec_msg, false);
2053 /* Do nothing for CEC switches */
2054 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
2056 return cec_feature_refused(adap, msg);
2058 case CEC_MSG_GIVE_OSD_NAME: {
2059 if (adap->log_addrs.osd_name[0] == 0)
2060 return cec_feature_abort(adap, msg);
2061 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
2062 return cec_transmit_msg(adap, &tx_cec_msg, false);
2065 case CEC_MSG_GIVE_FEATURES:
2066 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
2067 return cec_feature_abort(adap, msg);
2068 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
2069 return cec_transmit_msg(adap, &tx_cec_msg, false);
2073 * Unprocessed messages are aborted if userspace isn't doing
2074 * any processing either.
2076 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
2077 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
2078 return cec_feature_abort(adap, msg);
2083 /* If this was a reply, then we're done, unless otherwise specified */
2084 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
2088 * Send to the exclusive follower if there is one, otherwise send
2091 if (adap->cec_follower)
2092 cec_queue_msg_fh(adap->cec_follower, msg);
2094 cec_queue_msg_followers(adap, msg);
2099 * Helper functions to keep track of the 'monitor all' use count.
2101 * These functions are called with adap->lock held.
2103 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
2107 if (adap->monitor_all_cnt == 0)
2108 ret = call_op(adap, adap_monitor_all_enable, 1);
2110 adap->monitor_all_cnt++;
2114 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
2116 adap->monitor_all_cnt--;
2117 if (adap->monitor_all_cnt == 0)
2118 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
2122 * Helper functions to keep track of the 'monitor pin' use count.
2124 * These functions are called with adap->lock held.
2126 int cec_monitor_pin_cnt_inc(struct cec_adapter *adap)
2130 if (adap->monitor_pin_cnt == 0)
2131 ret = call_op(adap, adap_monitor_pin_enable, 1);
2133 adap->monitor_pin_cnt++;
2137 void cec_monitor_pin_cnt_dec(struct cec_adapter *adap)
2139 adap->monitor_pin_cnt--;
2140 if (adap->monitor_pin_cnt == 0)
2141 WARN_ON(call_op(adap, adap_monitor_pin_enable, 0));
2144 #ifdef CONFIG_DEBUG_FS
2146 * Log the current state of the CEC adapter.
2147 * Very useful for debugging.
2149 int cec_adap_status(struct seq_file *file, void *priv)
2151 struct cec_adapter *adap = dev_get_drvdata(file->private);
2152 struct cec_data *data;
2154 mutex_lock(&adap->lock);
2155 seq_printf(file, "configured: %d\n", adap->is_configured);
2156 seq_printf(file, "configuring: %d\n", adap->is_configuring);
2157 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
2158 cec_phys_addr_exp(adap->phys_addr));
2159 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
2160 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
2161 if (adap->cec_follower)
2162 seq_printf(file, "has CEC follower%s\n",
2163 adap->passthrough ? " (in passthrough mode)" : "");
2164 if (adap->cec_initiator)
2165 seq_puts(file, "has CEC initiator\n");
2166 if (adap->monitor_all_cnt)
2167 seq_printf(file, "file handles in Monitor All mode: %u\n",
2168 adap->monitor_all_cnt);
2169 if (adap->tx_timeouts) {
2170 seq_printf(file, "transmit timeouts: %u\n",
2172 adap->tx_timeouts = 0;
2174 data = adap->transmitting;
2176 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
2177 data->msg.len, data->msg.msg, data->msg.reply,
2179 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
2180 list_for_each_entry(data, &adap->transmit_queue, list) {
2181 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
2182 data->msg.len, data->msg.msg, data->msg.reply,
2185 list_for_each_entry(data, &adap->wait_queue, list) {
2186 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
2187 data->msg.len, data->msg.msg, data->msg.reply,
2191 call_void_op(adap, adap_status, file);
2192 mutex_unlock(&adap->lock);