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/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/ktime.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>
20 #include <drm/drm_connector.h>
21 #include <drm/drm_device.h>
22 #include <drm/drm_edid.h>
23 #include <drm/drm_file.h>
27 static void cec_fill_msg_report_features(struct cec_adapter *adap,
31 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
35 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
36 if (adap->log_addrs.log_addr[i] == log_addr)
41 static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
43 int i = cec_log_addr2idx(adap, log_addr);
45 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
48 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
51 unsigned int loc = cec_get_edid_spa_location(edid, size);
56 return CEC_PHYS_ADDR_INVALID;
57 return (edid[loc] << 8) | edid[loc + 1];
59 EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
61 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
62 const struct drm_connector *connector)
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;
69 EXPORT_SYMBOL_GPL(cec_fill_conn_info_from_drm);
72 * Queue a new event for this filehandle. If ts == 0, then set it
73 * to the current time.
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.
79 void cec_queue_event_fh(struct cec_fh *fh,
80 const struct cec_event *new_ev, u64 ts)
82 static const u16 max_events[CEC_NUM_EVENTS] = {
83 1, 1, 800, 800, 8, 8, 8, 8
85 struct cec_event_entry *entry;
86 unsigned int ev_idx = new_ev->event - 1;
88 if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
94 mutex_lock(&fh->lock);
95 if (ev_idx < CEC_NUM_CORE_EVENTS)
96 entry = &fh->core_events[ev_idx];
98 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
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;
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++;
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);
126 /* Mark that events were lost */
127 entry = list_first_entry_or_null(&fh->events[ev_idx],
128 struct cec_event_entry, list);
130 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
133 mutex_unlock(&fh->lock);
134 wake_up_interruptible(&fh->wait);
137 /* Queue a new event for all open filehandles. */
138 static void cec_queue_event(struct cec_adapter *adap,
139 const struct cec_event *ev)
141 u64 ts = ktime_get_ns();
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);
150 /* Notify userspace that the CEC pin changed state at the given time. */
151 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
152 bool dropped_events, ktime_t ts)
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,
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));
166 mutex_unlock(&adap->devnode.lock_fhs);
168 EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event);
170 /* Notify userspace that the HPD pin changed state at the given time. */
171 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
173 struct cec_event ev = {
174 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH :
175 CEC_EVENT_PIN_HPD_LOW,
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);
184 EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event);
186 /* Notify userspace that the 5V pin changed state at the given time. */
187 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
189 struct cec_event ev = {
190 .event = is_high ? CEC_EVENT_PIN_5V_HIGH :
191 CEC_EVENT_PIN_5V_LOW,
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);
200 EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event);
203 * Queue a new message for this filehandle.
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.
209 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
211 static const struct cec_event ev_lost_msgs = {
212 .event = CEC_EVENT_LOST_MSGS,
218 struct cec_msg_entry *entry;
220 mutex_lock(&fh->lock);
221 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
224 /* Add new msg at the end of the queue */
225 list_add_tail(&entry->list, &fh->msgs);
227 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
228 /* All is fine if there is enough room */
230 mutex_unlock(&fh->lock);
231 wake_up_interruptible(&fh->wait);
236 * if the message queue is full, then drop the oldest one and
237 * send a lost message event.
239 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
240 list_del(&entry->list);
243 mutex_unlock(&fh->lock);
246 * We lost a message, either because kmalloc failed or the queue
249 cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
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.
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.
262 static void cec_queue_msg_monitor(struct cec_adapter *adap,
263 const struct cec_msg *msg,
267 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
268 CEC_MODE_MONITOR_ALL;
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);
275 mutex_unlock(&adap->devnode.lock_fhs);
279 * Queue the message for follower filehandles.
281 static void cec_queue_msg_followers(struct cec_adapter *adap,
282 const struct cec_msg *msg)
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);
291 mutex_unlock(&adap->devnode.lock_fhs);
294 /* Notify userspace of an adapter state change. */
295 static void cec_post_state_event(struct cec_adapter *adap)
297 struct cec_event ev = {
298 .event = CEC_EVENT_STATE_CHANGE,
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);
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.
313 * This function is called with adap->lock held.
315 static void cec_data_completed(struct cec_data *data)
318 * Delete this transmit from the filehandle's xfer_list since
319 * we're done with it.
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.
326 list_del_init(&data->xfer_list);
328 if (data->blocking) {
330 * Someone is blocking so mark the message as completed
333 data->completed = true;
337 * No blocking, so just queue the message if needed and
341 cec_queue_msg_fh(data->fh, &data->msg);
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.
351 * This function is called with adap->lock held.
353 static void cec_data_cancel(struct cec_data *data, u8 tx_status, u8 rx_status)
355 struct cec_adapter *adap = data->adap;
358 * It's either the current transmit, or it is a pending
359 * transmit. Take the appropriate action to clear it.
361 if (adap->transmitting == data) {
362 adap->transmitting = NULL;
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--;
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;
374 data->msg.tx_status = 0;
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++;
382 data->msg.rx_status = 0;
385 /* Queue transmitted message for monitoring purposes */
386 cec_queue_msg_monitor(adap, &data->msg, 1);
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);
392 cec_data_completed(data);
396 * Flush all pending transmits and cancel any pending timeout work.
398 * This function is called with adap->lock held.
400 static void cec_flush(struct cec_adapter *adap)
402 struct cec_data *data, *n;
405 * If the adapter is disabled, or we're asked to stop,
406 * then cancel any pending transmits.
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);
413 if (adap->transmitting)
414 adap->transmit_in_progress_aborted = true;
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);
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.
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
433 if (WARN_ON(adap->transmit_queue_sz))
434 adap->transmit_queue_sz = 0;
438 * Main CEC state machine
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().
445 * If the adapter is disabled, then remove all queued messages instead.
447 * If the current transmit times out, then cancel that transmit.
449 int cec_thread_func(void *_adap)
451 struct cec_adapter *adap = _adap;
454 unsigned int signal_free_time;
455 struct cec_data *data;
456 bool timeout = false;
459 if (adap->transmit_in_progress) {
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.
469 err = wait_event_interruptible_timeout(adap->kthread_waitq,
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));
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)));
485 mutex_lock(&adap->lock);
487 if ((adap->needs_hpd &&
488 (!adap->is_configured && !adap->is_configuring)) ||
489 kthread_should_stop()) {
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;
503 if (adap->transmit_in_progress && 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 xfer_timeout_ms (2.1s by
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);
521 pr_warn("cec-%s: transmit timed out\n", adap->name);
523 adap->transmit_in_progress = false;
524 adap->tx_timeout_cnt++;
529 * If we are still transmitting, or there is nothing new to
530 * transmit, then just continue waiting.
532 if (adap->transmit_in_progress || list_empty(&adap->transmit_queue))
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--;
542 /* Make this the current transmitting message */
543 adap->transmitting = data;
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.
551 if (data->msg.len == 1 && adap->is_configured)
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);
567 * should be >= 7 data bit periods for sending another
568 * frame immediately after another.
570 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
572 if (data->attempts == 0)
573 data->attempts = attempts;
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);
581 adap->transmit_in_progress = true;
584 mutex_unlock(&adap->lock);
586 if (kthread_should_stop())
593 * Called by the CEC adapter if a transmit finished.
595 void 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)
599 struct cec_data *data;
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;
606 dprintk(2, "%s: status 0x%02x\n", __func__, status);
607 if (attempts_made < 1)
610 mutex_lock(&adap->lock);
611 data = adap->transmitting;
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.
618 if (!adap->transmit_in_progress)
619 dprintk(1, "%s was called without an ongoing transmit!\n",
621 adap->transmit_in_progress = false;
624 adap->transmit_in_progress = false;
625 adap->transmit_in_progress_aborted = false;
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;
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;
643 * Low Drive transmission errors should really not happen for
644 * well-behaved CEC devices and proper HDMI cables.
646 * Ditto for the 'Error' status.
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.
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,
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,
665 /* Mark that we're done with this transmit */
666 adap->transmitting = NULL;
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.
673 if (!aborted && data->attempts > attempts_made && !done) {
674 /* Retry this message */
675 data->attempts -= attempts_made;
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);
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++;
689 if (aborted && !done)
690 status |= CEC_TX_STATUS_ABORTED;
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;
697 /* Queue transmitted message for monitoring purposes */
698 cec_queue_msg_monitor(adap, msg, 1);
700 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
703 * Queue the message into the wait queue if we want to wait
706 list_add_tail(&data->list, &adap->wait_queue);
707 schedule_delayed_work(&data->work,
708 msecs_to_jiffies(msg->timeout));
710 /* Otherwise we're done */
711 cec_data_completed(data);
716 * Wake up the main thread to see if another message is ready
717 * for transmitting or to retry the current message.
719 wake_up_interruptible(&adap->kthread_waitq);
720 mutex_unlock(&adap->lock);
722 EXPORT_SYMBOL_GPL(cec_transmit_done_ts);
724 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
725 u8 status, ktime_t ts)
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);
731 case CEC_TX_STATUS_ARB_LOST:
732 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts);
734 case CEC_TX_STATUS_NACK:
735 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts);
737 case CEC_TX_STATUS_LOW_DRIVE:
738 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts);
740 case CEC_TX_STATUS_ERROR:
741 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts);
744 /* Should never happen */
745 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
749 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts);
752 * Called when waiting for a reply times out.
754 static void cec_wait_timeout(struct work_struct *work)
756 struct cec_data *data = container_of(work, struct cec_data, work.work);
757 struct cec_adapter *adap = data->adap;
759 mutex_lock(&adap->lock);
761 * Sanity check in case the timeout and the arrival of the message
762 * happened at the same time.
764 if (list_empty(&data->list))
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);
771 mutex_unlock(&adap->lock);
775 * Transmit a message. The fh argument may be NULL if the transmit is not
776 * associated with a specific filehandle.
778 * This function is called with adap->lock held.
780 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
781 struct cec_fh *fh, bool block)
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;
789 if (adap->devnode.unregistered)
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;
801 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS | CEC_MSG_FL_RAW |
802 (reply_vendor_id ? CEC_MSG_FL_REPLY_VENDOR_ID : 0);
804 if ((reply_vendor_id || msg->reply) && msg->timeout == 0) {
805 /* Make sure the timeout isn't 0. */
810 msg->flags &= ~CEC_MSG_FL_REPLY_TO_FOLLOWERS;
813 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
814 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
817 if (reply_vendor_id && msg->len < 6) {
818 dprintk(1, "%s: <Vendor Command With ID> message too short\n",
823 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
826 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
827 __func__, msg->len, msg->msg, msg->reply,
828 !block ? ", nb" : "");
830 dprintk(2, "%s: %*ph%s\n",
831 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
833 if (msg->timeout && msg->len == 1) {
834 dprintk(1, "%s: can't reply to poll msg\n", __func__);
839 if (!capable(CAP_SYS_RAWIO))
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__);
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;
855 if (cec_msg_destination(msg) == 0xf) {
856 dprintk(1, "%s: invalid poll message\n",
860 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
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.
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;
875 msg->sequence = ++adap->sequence;
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",
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));
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.
897 if (!adap->is_configured && !adap->is_configuring &&
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__);
907 if (!adap->is_configured && !adap->is_configuring) {
908 if (adap->needs_hpd) {
909 dprintk(1, "%s: adapter is unconfigured and needs HPD\n",
913 if (reply_vendor_id || msg->reply) {
914 dprintk(1, "%s: adapter is unconfigured so reply is not supported\n",
920 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
921 dprintk(2, "%s: transmit queue full\n", __func__);
925 data = kzalloc(sizeof(*data), GFP_KERNEL);
929 msg->sequence = ++adap->sequence;
931 msg->sequence = ++adap->sequence;
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;
941 } else if (msg->timeout) {
942 data->match_reply[0] = msg->reply;
946 init_completion(&data->c);
947 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
950 list_add_tail(&data->xfer_list, &fh->xfer_list);
952 INIT_LIST_HEAD(&data->xfer_list);
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);
959 /* All done if we don't need to block waiting for completion */
964 * Release the lock and wait, retake the lock afterwards.
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);
972 adap->transmit_in_progress_aborted = true;
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);
979 cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0);
982 /* The transmit completed (possibly with an error) */
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);
992 /* Helper function to be used by drivers and this framework. */
993 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
998 mutex_lock(&adap->lock);
999 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
1000 mutex_unlock(&adap->lock);
1003 EXPORT_SYMBOL_GPL(cec_transmit_msg);
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.
1010 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
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)
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.
1024 static 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,
1103 /* Called by the CEC adapter if a message is received */
1104 void cec_received_msg_ts(struct cec_adapter *adap,
1105 struct cec_msg *msg, ktime_t ts)
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;
1116 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
1119 if (adap->devnode.unregistered)
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.
1127 * Note that this won't work if this is an Unregistered device.
1129 * It is bad practice if the hardware receives the message that it
1130 * transmitted and luckily most CEC adapters behave correctly in this
1133 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
1134 cec_has_log_addr(adap, msg_init))
1137 msg->rx_ts = ktime_to_ns(ts);
1138 msg->rx_status = CEC_RX_STATUS_OK;
1139 msg->sequence = msg->reply = msg->timeout = 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;
1147 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
1149 mutex_lock(&adap->lock);
1150 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1152 if (!adap->transmit_in_progress)
1153 adap->last_initiator = 0xff;
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;
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).
1167 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
1168 u8 dir_fl = cec_msg_size[cmd] & BOTH;
1170 min_len = cec_msg_size[cmd] & 0x1f;
1171 if (msg->len < min_len)
1173 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
1175 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST))
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))
1182 if (valid_la && min_len) {
1183 /* These messages have special length requirements */
1185 case CEC_MSG_RECORD_ON:
1186 switch (msg->msg[2]) {
1187 case CEC_OP_RECORD_SRC_OWN:
1189 case CEC_OP_RECORD_SRC_DIGITAL:
1193 case CEC_OP_RECORD_SRC_ANALOG:
1197 case CEC_OP_RECORD_SRC_EXT_PLUG:
1201 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
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;
1214 /* The aborted command is in msg[2] */
1219 * Walk over all transmitted messages that are waiting for a
1222 list_for_each_entry(data, &adap->wait_queue, list) {
1223 struct cec_msg *dst = &data->msg;
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.
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)) {
1236 data->match_reply[0] = cmd;
1239 /* Does the command match? */
1240 if ((abort && cmd != dst->msg[1]) ||
1241 (!abort && memcmp(data->match_reply, msg->msg + 1, data->match_len)))
1244 /* Does the addressing match? */
1245 if (msg_init != cec_msg_destination(dst) &&
1246 !cec_msg_is_broadcast(dst))
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;
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);
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);
1268 * Mark this as a reply, provided someone is still
1269 * waiting for the answer.
1273 cec_data_completed(data);
1277 mutex_unlock(&adap->lock);
1279 /* Pass the message on to any monitoring filehandles */
1280 cec_queue_msg_monitor(adap, msg, monitor_valid_la);
1282 /* We're done if it is not for us or a poll message */
1283 if (!valid_la || msg->len <= 1)
1286 if (adap->log_addrs.log_addr_mask == 0)
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.
1294 cec_receive_notify(adap, msg, is_reply);
1296 EXPORT_SYMBOL_GPL(cec_received_msg_ts);
1298 /* Logical Address Handling */
1301 * Attempt to claim a specific logical address.
1303 * This function is called with adap->lock held.
1305 static int cec_config_log_addr(struct cec_adapter *adap,
1307 unsigned int log_addr)
1309 struct cec_log_addrs *las = &adap->log_addrs;
1310 struct cec_msg msg = { };
1311 const unsigned int max_retries = 2;
1315 if (cec_has_log_addr(adap, log_addr))
1318 /* Send poll message */
1320 msg.msg[0] = (log_addr << 4) | log_addr;
1322 for (i = 0; i < max_retries; i++) {
1323 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1326 * While trying to poll the physical address was reset
1327 * and the adapter was unconfigured, so bail out.
1329 if (adap->phys_addr == CEC_PHYS_ADDR_INVALID)
1332 /* Also bail out if the PA changed while configuring. */
1333 if (adap->must_reconfigure)
1340 * The message was aborted or timed out due to a disconnect or
1341 * unconfigure, just bail out.
1344 (CEC_TX_STATUS_ABORTED | CEC_TX_STATUS_TIMEOUT))
1346 if (msg.tx_status & CEC_TX_STATUS_OK)
1348 if (msg.tx_status & CEC_TX_STATUS_NACK)
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.
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.
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);
1370 * Message not acknowledged, so this logical
1371 * address is free to use.
1373 err = call_op(adap, adap_log_addr, log_addr);
1377 las->log_addr[idx] = log_addr;
1378 las->log_addr_mask |= 1 << log_addr;
1383 * Unconfigure the adapter: clear all logical addresses and send
1384 * the state changed event.
1386 * This function is called with adap->lock held.
1388 static void cec_adap_unconfigure(struct cec_adapter *adap)
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;
1395 wake_up_interruptible(&adap->kthread_waitq);
1396 cec_post_state_event(adap);
1397 call_void_op(adap, adap_unconfigured);
1401 * Attempt to claim the required logical addresses.
1403 static int cec_config_thread_func(void *arg)
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
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
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
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
1428 static const u8 audiosystem_log_addrs[] = {
1429 CEC_LOG_ADDR_AUDIOSYSTEM,
1430 CEC_LOG_ADDR_INVALID
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
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,
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,
1453 struct cec_adapter *adap = arg;
1454 struct cec_log_addrs *las = &adap->log_addrs;
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;
1463 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1467 for (i = 0; i < las->num_log_addrs; i++) {
1468 unsigned int type = las->log_addr_type[i];
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.
1477 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1478 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
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];
1488 err = cec_config_log_addr(adap, i, last_la);
1490 if (adap->must_reconfigure) {
1491 adap->must_reconfigure = false;
1492 las->log_addr_mask = 0;
1496 if (err > 0) /* Reused last LA */
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)
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)
1512 err = cec_config_log_addr(adap, i, la_list[j]);
1513 if (err == 0) /* LA is in use */
1517 /* Done, claimed an LA */
1521 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1522 dprintk(1, "could not claim LA %d\n", i);
1525 if (adap->log_addrs.log_addr_mask == 0 &&
1526 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
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;
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);
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.
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.
1554 for (i = 0; i < las->num_log_addrs; i++) {
1555 struct cec_msg msg = {};
1557 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1558 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1561 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
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);
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",
1575 cec_phys_addr_exp(adap->phys_addr));
1576 cec_transmit_msg_fh(adap, &msg, NULL, false);
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);
1585 adap->kthread_config = NULL;
1586 complete(&adap->config_completion);
1587 mutex_unlock(&adap->lock);
1588 call_void_op(adap, configured);
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);
1604 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1605 * logical addresses.
1607 * This function is called with adap->lock held.
1609 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1611 if (WARN_ON(adap->is_claiming_log_addrs ||
1612 adap->is_configuring || adap->is_configured))
1615 adap->is_claiming_log_addrs = true;
1617 init_completion(&adap->config_completion);
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;
1627 mutex_unlock(&adap->lock);
1628 wait_for_completion(&adap->config_completion);
1629 mutex_lock(&adap->lock);
1631 adap->is_claiming_log_addrs = false;
1635 * Helper function to enable/disable the CEC adapter.
1637 * This function is called with adap->lock held.
1639 int cec_adap_enable(struct cec_adapter *adap)
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;
1649 if (adap->devnode.unregistered)
1652 if (enable == adap->is_enabled)
1655 /* serialize adap_enable */
1656 mutex_lock(&adap->devnode.lock);
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);
1665 * Enable monitor-all/pin modes if needed. We warn, but
1666 * continue if this fails as this is not a critical error.
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));
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);
1687 adap->is_enabled = enable;
1688 wake_up_interruptible(&adap->kthread_waitq);
1689 mutex_unlock(&adap->devnode.lock);
1693 /* Set a new physical address and send an event notifying userspace of this.
1695 * This function is called with adap->lock held.
1697 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1699 bool becomes_invalid = phys_addr == CEC_PHYS_ADDR_INVALID;
1700 bool is_invalid = adap->phys_addr == CEC_PHYS_ADDR_INVALID;
1702 if (phys_addr == adap->phys_addr)
1704 if (!becomes_invalid && adap->devnode.unregistered)
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);
1719 adap->phys_addr = phys_addr;
1721 cec_adap_enable(adap);
1723 cec_post_state_event(adap);
1724 if (!adap->log_addrs.num_log_addrs)
1726 if (adap->is_configuring)
1727 adap->must_reconfigure = true;
1729 cec_claim_log_addrs(adap, block);
1732 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1734 if (IS_ERR_OR_NULL(adap))
1737 mutex_lock(&adap->lock);
1738 __cec_s_phys_addr(adap, phys_addr, block);
1739 mutex_unlock(&adap->lock);
1741 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1744 * Note: In the drm subsystem, prefer calling (if possible):
1746 * cec_s_phys_addr(adap, connector->display_info.source_physical_address, false);
1748 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1749 const struct edid *edid)
1751 u16 pa = CEC_PHYS_ADDR_INVALID;
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);
1758 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1760 void cec_s_conn_info(struct cec_adapter *adap,
1761 const struct cec_connector_info *conn_info)
1763 if (IS_ERR_OR_NULL(adap))
1766 if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO))
1769 mutex_lock(&adap->lock);
1771 adap->conn_info = *conn_info;
1773 memset(&adap->conn_info, 0, sizeof(adap->conn_info));
1774 cec_post_state_event(adap);
1775 mutex_unlock(&adap->lock);
1777 EXPORT_SYMBOL_GPL(cec_s_conn_info);
1780 * Called from either the ioctl or a driver to set the logical addresses.
1782 * This function is called with adap->lock held.
1784 int __cec_s_log_addrs(struct cec_adapter *adap,
1785 struct cec_log_addrs *log_addrs, bool block)
1791 if (adap->devnode.unregistered)
1794 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1795 if (!adap->log_addrs.num_log_addrs)
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);
1809 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1811 * Sanitize log_addrs fields if a CDC-Only device is
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;
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.
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;
1832 /* Ensure the osd name is 0-terminated */
1833 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1836 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1837 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1842 * Vendor ID is a 24 bit number, so check if the value is
1843 * within the correct range.
1845 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1846 (log_addrs->vendor_id & 0xff000000) != 0) {
1847 dprintk(1, "invalid vendor ID\n");
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");
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");
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;
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");
1876 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1877 dprintk(1, "duplicate logical address type\n");
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");
1887 if (log_addrs->primary_device_type[i] >
1888 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1889 dprintk(1, "unknown primary device type\n");
1892 if (log_addrs->primary_device_type[i] == 2) {
1893 dprintk(1, "invalid primary device type\n");
1896 for (j = 0; j < feature_sz; j++) {
1897 if ((features[j] & 0x80) == 0) {
1898 if (op_is_dev_features)
1900 op_is_dev_features = true;
1903 if (!op_is_dev_features || j == feature_sz) {
1904 dprintk(1, "malformed features\n");
1907 /* Zero unused part of the feature array */
1908 memset(features + j + 1, 0, feature_sz - j - 1);
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");
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");
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");
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]));
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);
1947 int cec_s_log_addrs(struct cec_adapter *adap,
1948 struct cec_log_addrs *log_addrs, bool block)
1952 mutex_lock(&adap->lock);
1953 err = __cec_s_log_addrs(adap, log_addrs, block);
1954 mutex_unlock(&adap->lock);
1957 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1959 /* High-level core CEC message handling */
1961 /* Fill in the Report Features message */
1962 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1963 struct cec_msg *msg,
1964 unsigned int la_idx)
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;
1971 /* Report Features */
1972 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
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];
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)
1984 op_is_dev_features = true;
1989 /* Transmit the Feature Abort message */
1990 static int cec_feature_abort_reason(struct cec_adapter *adap,
1991 struct cec_msg *msg, u8 reason)
1993 struct cec_msg tx_msg = { };
1996 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1999 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
2001 /* Don't Feature Abort messages from 'Unregistered' */
2002 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
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);
2009 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
2011 return cec_feature_abort_reason(adap, msg,
2012 CEC_OP_ABORT_UNRECOGNIZED_OP);
2015 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
2017 return cec_feature_abort_reason(adap, msg,
2018 CEC_OP_ABORT_REFUSED);
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.
2026 * The message is either a broadcast message or a valid directed message.
2028 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
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 = { };
2039 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
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)
2046 /* Allow drivers to process the message first */
2047 if (adap->ops->received && !adap->devnode.unregistered &&
2048 adap->ops->received(adap, msg) != -ENOMSG)
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.
2057 switch (msg->msg[1]) {
2058 case CEC_MSG_GET_CEC_VERSION:
2060 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
2061 case CEC_MSG_GIVE_OSD_NAME:
2063 * These messages reply with a directed message, so ignore if
2064 * the initiator is Unregistered.
2066 if (!adap->passthrough && from_unregistered)
2069 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
2070 case CEC_MSG_GIVE_FEATURES:
2071 case CEC_MSG_GIVE_PHYSICAL_ADDR:
2073 * Skip processing these messages if the passthrough mode
2076 if (adap->passthrough)
2077 goto skip_processing;
2078 /* Ignore if addressing is wrong */
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;
2090 case CEC_MSG_REPORT_PHYSICAL_ADDR:
2092 * This message is always processed, regardless of the
2093 * passthrough setting.
2095 * Exception: don't process if wrong addressing mode.
2098 goto skip_processing;
2105 cec_msg_set_reply_to(&tx_cec_msg, msg);
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];
2112 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
2113 cec_phys_addr_exp(pa), init_laddr);
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))
2122 #ifdef CONFIG_MEDIA_CEC_RC
2123 switch (msg->msg[2]) {
2125 * Play function, this message can have variable length
2126 * depending on the specific play function that is used.
2128 case CEC_OP_UI_CMD_PLAY_FUNCTION:
2130 rc_keydown(adap->rc, RC_PROTO_CEC,
2133 rc_keydown(adap->rc, RC_PROTO_CEC,
2134 msg->msg[2] << 8 | msg->msg[3], 0);
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
2142 * For the time being these messages are not processed by the
2143 * framework and are simply forwarded to the user space.
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:
2153 rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0);
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))
2163 #ifdef CONFIG_MEDIA_CEC_RC
2169 * The remaining messages are only processed if the passthrough mode
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);
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)
2180 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
2181 return cec_transmit_msg(adap, &tx_cec_msg, false);
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);
2190 /* Do nothing for CEC switches */
2191 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
2193 return cec_feature_refused(adap, msg);
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);
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);
2210 * Unprocessed messages are aborted if userspace isn't doing
2211 * any processing either.
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);
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))
2225 * Send to the exclusive follower if there is one, otherwise send
2228 if (adap->cec_follower)
2229 cec_queue_msg_fh(adap->cec_follower, msg);
2231 cec_queue_msg_followers(adap, msg);
2236 * Helper functions to keep track of the 'monitor all' use count.
2238 * These functions are called with adap->lock held.
2240 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
2244 if (adap->monitor_all_cnt++)
2247 ret = cec_adap_enable(adap);
2249 adap->monitor_all_cnt--;
2253 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
2255 if (WARN_ON(!adap->monitor_all_cnt))
2257 if (--adap->monitor_all_cnt)
2259 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
2260 cec_adap_enable(adap);
2264 * Helper functions to keep track of the 'monitor pin' use count.
2266 * These functions are called with adap->lock held.
2268 int cec_monitor_pin_cnt_inc(struct cec_adapter *adap)
2272 if (adap->monitor_pin_cnt++)
2275 ret = cec_adap_enable(adap);
2277 adap->monitor_pin_cnt--;
2281 void cec_monitor_pin_cnt_dec(struct cec_adapter *adap)
2283 if (WARN_ON(!adap->monitor_pin_cnt))
2285 if (--adap->monitor_pin_cnt)
2287 WARN_ON(call_op(adap, adap_monitor_pin_enable, false));
2288 cec_adap_enable(adap);
2291 #ifdef CONFIG_DEBUG_FS
2293 * Log the current state of the CEC adapter.
2294 * Very useful for debugging.
2296 int cec_adap_status(struct seq_file *file, void *priv)
2298 struct cec_adapter *adap = dev_get_drvdata(file->private);
2299 struct cec_data *data;
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;
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;
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;
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;
2340 data = adap->transmitting;
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,
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,
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,
2360 call_void_op(adap, adap_status, file);
2361 mutex_unlock(&adap->lock);