2 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/kmod.h>
25 #include <linux/ktime.h>
26 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
31 #include <drm/drm_edid.h>
35 static void cec_fill_msg_report_features(struct cec_adapter *adap,
40 * 400 ms is the time it takes for one 16 byte message to be
41 * transferred and 5 is the maximum number of retries. Add
42 * another 100 ms as a margin. So if the transmit doesn't
43 * finish before that time something is really wrong and we
46 * This is a sign that something it really wrong and a warning
49 #define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
51 #define call_op(adap, op, arg...) \
52 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
54 #define call_void_op(adap, op, arg...) \
57 adap->ops->op(adap, ## arg); \
60 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
64 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
65 if (adap->log_addrs.log_addr[i] == log_addr)
70 static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
72 int i = cec_log_addr2idx(adap, log_addr);
74 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
78 * Queue a new event for this filehandle. If ts == 0, then set it
79 * to the current time.
81 * The two events that are currently defined do not need to keep track
82 * of intermediate events, so no actual queue of events is needed,
83 * instead just store the latest state and the total number of lost
86 * Should new events be added in the future that require intermediate
87 * results to be queued as well, then a proper queue data structure is
88 * required. But until then, just keep it simple.
90 void cec_queue_event_fh(struct cec_fh *fh,
91 const struct cec_event *new_ev, u64 ts)
93 struct cec_event *ev = &fh->events[new_ev->event - 1];
98 mutex_lock(&fh->lock);
99 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
100 fh->pending_events & (1 << new_ev->event)) {
102 * If there is already a lost_msgs event, then just
103 * update the lost_msgs count. This effectively
104 * merges the old and new events into one.
106 ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
111 * Intermediate states are not interesting, so just
112 * overwrite any older event.
116 fh->pending_events |= 1 << new_ev->event;
119 mutex_unlock(&fh->lock);
120 wake_up_interruptible(&fh->wait);
123 /* Queue a new event for all open filehandles. */
124 static void cec_queue_event(struct cec_adapter *adap,
125 const struct cec_event *ev)
127 u64 ts = ktime_get_ns();
130 mutex_lock(&adap->devnode.lock);
131 list_for_each_entry(fh, &adap->devnode.fhs, list)
132 cec_queue_event_fh(fh, ev, ts);
133 mutex_unlock(&adap->devnode.lock);
137 * Queue a new message for this filehandle. If there is no more room
138 * in the queue, then send the LOST_MSGS event instead.
140 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
142 static const struct cec_event ev_lost_msg = {
144 .event = CEC_EVENT_LOST_MSGS,
147 .lost_msgs.lost_msgs = 1,
150 struct cec_msg_entry *entry;
152 mutex_lock(&fh->lock);
153 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
158 /* Add new msg at the end of the queue */
159 list_add_tail(&entry->list, &fh->msgs);
162 * if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
163 * messages, drop the oldest one and send a lost message event.
165 if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
166 list_del(&entry->list);
170 mutex_unlock(&fh->lock);
171 wake_up_interruptible(&fh->wait);
175 mutex_unlock(&fh->lock);
176 cec_queue_event_fh(fh, &ev_lost_msg, 0);
180 * Queue the message for those filehandles that are in monitor mode.
181 * If valid_la is true (this message is for us or was sent by us),
182 * then pass it on to any monitoring filehandle. If this message
183 * isn't for us or from us, then only give it to filehandles that
184 * are in MONITOR_ALL mode.
186 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
187 * set and the CEC adapter was placed in 'monitor all' mode.
189 static void cec_queue_msg_monitor(struct cec_adapter *adap,
190 const struct cec_msg *msg,
194 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
195 CEC_MODE_MONITOR_ALL;
197 mutex_lock(&adap->devnode.lock);
198 list_for_each_entry(fh, &adap->devnode.fhs, list) {
199 if (fh->mode_follower >= monitor_mode)
200 cec_queue_msg_fh(fh, msg);
202 mutex_unlock(&adap->devnode.lock);
206 * Queue the message for follower filehandles.
208 static void cec_queue_msg_followers(struct cec_adapter *adap,
209 const struct cec_msg *msg)
213 mutex_lock(&adap->devnode.lock);
214 list_for_each_entry(fh, &adap->devnode.fhs, list) {
215 if (fh->mode_follower == CEC_MODE_FOLLOWER)
216 cec_queue_msg_fh(fh, msg);
218 mutex_unlock(&adap->devnode.lock);
221 /* Notify userspace of an adapter state change. */
222 static void cec_post_state_event(struct cec_adapter *adap)
224 struct cec_event ev = {
225 .event = CEC_EVENT_STATE_CHANGE,
228 ev.state_change.phys_addr = adap->phys_addr;
229 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
230 cec_queue_event(adap, &ev);
234 * A CEC transmit (and a possible wait for reply) completed.
235 * If this was in blocking mode, then complete it, otherwise
236 * queue the message for userspace to dequeue later.
238 * This function is called with adap->lock held.
240 static void cec_data_completed(struct cec_data *data)
243 * Delete this transmit from the filehandle's xfer_list since
244 * we're done with it.
246 * Note that if the filehandle is closed before this transmit
247 * finished, then the release() function will set data->fh to NULL.
248 * Without that we would be referring to a closed filehandle.
251 list_del(&data->xfer_list);
253 if (data->blocking) {
255 * Someone is blocking so mark the message as completed
258 data->completed = true;
262 * No blocking, so just queue the message if needed and
266 cec_queue_msg_fh(data->fh, &data->msg);
272 * A pending CEC transmit needs to be cancelled, either because the CEC
273 * adapter is disabled or the transmit takes an impossibly long time to
276 * This function is called with adap->lock held.
278 static void cec_data_cancel(struct cec_data *data)
281 * It's either the current transmit, or it is a pending
282 * transmit. Take the appropriate action to clear it.
284 if (data->adap->transmitting == data) {
285 data->adap->transmitting = NULL;
287 list_del_init(&data->list);
288 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
289 data->adap->transmit_queue_sz--;
292 /* Mark it as an error */
293 data->msg.tx_ts = ktime_get_ns();
294 data->msg.tx_status |= CEC_TX_STATUS_ERROR |
295 CEC_TX_STATUS_MAX_RETRIES;
296 data->msg.tx_error_cnt++;
298 /* Queue transmitted message for monitoring purposes */
299 cec_queue_msg_monitor(data->adap, &data->msg, 1);
301 cec_data_completed(data);
305 * Flush all pending transmits and cancel any pending timeout work.
307 * This function is called with adap->lock held.
309 static void cec_flush(struct cec_adapter *adap)
311 struct cec_data *data, *n;
314 * If the adapter is disabled, or we're asked to stop,
315 * then cancel any pending transmits.
317 while (!list_empty(&adap->transmit_queue)) {
318 data = list_first_entry(&adap->transmit_queue,
319 struct cec_data, list);
320 cec_data_cancel(data);
322 if (adap->transmitting)
323 cec_data_cancel(adap->transmitting);
325 /* Cancel the pending timeout work. */
326 list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
327 if (cancel_delayed_work(&data->work))
328 cec_data_cancel(data);
330 * If cancel_delayed_work returned false, then
331 * the cec_wait_timeout function is running,
332 * which will call cec_data_completed. So no
333 * need to do anything special in that case.
339 * Main CEC state machine
341 * Wait until the thread should be stopped, or we are not transmitting and
342 * a new transmit message is queued up, in which case we start transmitting
343 * that message. When the adapter finished transmitting the message it will
344 * call cec_transmit_done().
346 * If the adapter is disabled, then remove all queued messages instead.
348 * If the current transmit times out, then cancel that transmit.
350 int cec_thread_func(void *_adap)
352 struct cec_adapter *adap = _adap;
355 unsigned int signal_free_time;
356 struct cec_data *data;
357 bool timeout = false;
360 if (adap->transmitting) {
364 * We are transmitting a message, so add a timeout
365 * to prevent the state machine to get stuck waiting
366 * for this message to finalize and add a check to
367 * see if the adapter is disabled in which case the
368 * transmit should be canceled.
370 err = wait_event_interruptible_timeout(adap->kthread_waitq,
372 (!adap->is_configured && !adap->is_configuring)) ||
373 kthread_should_stop() ||
374 (!adap->transmitting &&
375 !list_empty(&adap->transmit_queue)),
376 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
379 /* Otherwise we just wait for something to happen. */
380 wait_event_interruptible(adap->kthread_waitq,
381 kthread_should_stop() ||
382 (!adap->transmitting &&
383 !list_empty(&adap->transmit_queue)));
386 mutex_lock(&adap->lock);
388 if ((adap->needs_hpd &&
389 (!adap->is_configured && !adap->is_configuring)) ||
390 kthread_should_stop()) {
395 if (adap->transmitting && timeout) {
397 * If we timeout, then log that. This really shouldn't
398 * happen and is an indication of a faulty CEC adapter
399 * driver, or the CEC bus is in some weird state.
401 dprintk(0, "%s: message %*ph timed out!\n", __func__,
402 adap->transmitting->msg.len,
403 adap->transmitting->msg.msg);
404 /* Just give up on this. */
405 cec_data_cancel(adap->transmitting);
410 * If we are still transmitting, or there is nothing new to
411 * transmit, then just continue waiting.
413 if (adap->transmitting || list_empty(&adap->transmit_queue))
416 /* Get a new message to transmit */
417 data = list_first_entry(&adap->transmit_queue,
418 struct cec_data, list);
419 list_del_init(&data->list);
420 adap->transmit_queue_sz--;
422 /* Make this the current transmitting message */
423 adap->transmitting = data;
426 * Suggested number of attempts as per the CEC 2.0 spec:
427 * 4 attempts is the default, except for 'secondary poll
428 * messages', i.e. poll messages not sent during the adapter
429 * configuration phase when it allocates logical addresses.
431 if (data->msg.len == 1 && adap->is_configured)
436 /* Set the suggested signal free time */
437 if (data->attempts) {
438 /* should be >= 3 data bit periods for a retry */
439 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
440 } else if (data->new_initiator) {
441 /* should be >= 5 data bit periods for new initiator */
442 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
445 * should be >= 7 data bit periods for sending another
446 * frame immediately after another.
448 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
450 if (data->attempts == 0)
451 data->attempts = attempts;
453 /* Tell the adapter to transmit, cancel on error */
454 if (adap->ops->adap_transmit(adap, data->attempts,
455 signal_free_time, &data->msg))
456 cec_data_cancel(data);
459 mutex_unlock(&adap->lock);
461 if (kthread_should_stop())
468 * Called by the CEC adapter if a transmit finished.
470 void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
471 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
473 struct cec_data *data;
475 u64 ts = ktime_get_ns();
477 dprintk(2, "%s: status %02x\n", __func__, status);
478 mutex_lock(&adap->lock);
479 data = adap->transmitting;
482 * This can happen if a transmit was issued and the cable is
483 * unplugged while the transmit is ongoing. Ignore this
484 * transmit in that case.
486 dprintk(1, "%s was called without an ongoing transmit!\n",
493 /* Drivers must fill in the status! */
494 WARN_ON(status == 0);
496 msg->tx_status |= status;
497 msg->tx_arb_lost_cnt += arb_lost_cnt;
498 msg->tx_nack_cnt += nack_cnt;
499 msg->tx_low_drive_cnt += low_drive_cnt;
500 msg->tx_error_cnt += error_cnt;
502 /* Mark that we're done with this transmit */
503 adap->transmitting = NULL;
506 * If there are still retry attempts left and there was an error and
507 * the hardware didn't signal that it retried itself (by setting
508 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
510 if (data->attempts > 1 &&
511 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
512 /* Retry this message */
515 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
516 msg->len, msg->msg, data->attempts, msg->reply);
518 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
519 msg->len, msg->msg, data->attempts);
520 /* Add the message in front of the transmit queue */
521 list_add(&data->list, &adap->transmit_queue);
522 adap->transmit_queue_sz++;
528 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
529 if (!(status & CEC_TX_STATUS_OK))
530 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
532 /* Queue transmitted message for monitoring purposes */
533 cec_queue_msg_monitor(adap, msg, 1);
535 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
538 * Queue the message into the wait queue if we want to wait
541 list_add_tail(&data->list, &adap->wait_queue);
542 schedule_delayed_work(&data->work,
543 msecs_to_jiffies(msg->timeout));
545 /* Otherwise we're done */
546 cec_data_completed(data);
551 * Wake up the main thread to see if another message is ready
552 * for transmitting or to retry the current message.
554 wake_up_interruptible(&adap->kthread_waitq);
556 mutex_unlock(&adap->lock);
558 EXPORT_SYMBOL_GPL(cec_transmit_done);
560 void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status)
563 case CEC_TX_STATUS_OK:
564 cec_transmit_done(adap, status, 0, 0, 0, 0);
566 case CEC_TX_STATUS_ARB_LOST:
567 cec_transmit_done(adap, status, 1, 0, 0, 0);
569 case CEC_TX_STATUS_NACK:
570 cec_transmit_done(adap, status, 0, 1, 0, 0);
572 case CEC_TX_STATUS_LOW_DRIVE:
573 cec_transmit_done(adap, status, 0, 0, 1, 0);
575 case CEC_TX_STATUS_ERROR:
576 cec_transmit_done(adap, status, 0, 0, 0, 1);
579 /* Should never happen */
580 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
584 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done);
587 * Called when waiting for a reply times out.
589 static void cec_wait_timeout(struct work_struct *work)
591 struct cec_data *data = container_of(work, struct cec_data, work.work);
592 struct cec_adapter *adap = data->adap;
594 mutex_lock(&adap->lock);
596 * Sanity check in case the timeout and the arrival of the message
597 * happened at the same time.
599 if (list_empty(&data->list))
602 /* Mark the message as timed out */
603 list_del_init(&data->list);
604 data->msg.rx_ts = ktime_get_ns();
605 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
606 cec_data_completed(data);
608 mutex_unlock(&adap->lock);
612 * Transmit a message. The fh argument may be NULL if the transmit is not
613 * associated with a specific filehandle.
615 * This function is called with adap->lock held.
617 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
618 struct cec_fh *fh, bool block)
620 struct cec_data *data;
621 u8 last_initiator = 0xff;
622 unsigned int timeout;
629 msg->tx_arb_lost_cnt = 0;
630 msg->tx_nack_cnt = 0;
631 msg->tx_low_drive_cnt = 0;
632 msg->tx_error_cnt = 0;
633 msg->sequence = ++adap->sequence;
635 msg->sequence = ++adap->sequence;
637 if (msg->reply && msg->timeout == 0) {
638 /* Make sure the timeout isn't 0. */
642 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
647 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
648 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
651 if (msg->timeout && msg->len == 1) {
652 dprintk(1, "%s: can't reply for poll msg\n", __func__);
655 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
657 if (cec_msg_destination(msg) == 0xf) {
658 dprintk(1, "%s: invalid poll message\n", __func__);
661 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
663 * If the destination is a logical address our adapter
664 * has already claimed, then just NACK this.
665 * It depends on the hardware what it will do with a
666 * POLL to itself (some OK this), so it is just as
667 * easy to handle it here so the behavior will be
670 msg->tx_ts = ktime_get_ns();
671 msg->tx_status = CEC_TX_STATUS_NACK |
672 CEC_TX_STATUS_MAX_RETRIES;
673 msg->tx_nack_cnt = 1;
677 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
678 cec_has_log_addr(adap, cec_msg_destination(msg))) {
679 dprintk(1, "%s: destination is the adapter itself\n", __func__);
682 if (msg->len > 1 && adap->is_configured &&
683 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
684 dprintk(1, "%s: initiator has unknown logical address %d\n",
685 __func__, cec_msg_initiator(msg));
688 if (!adap->is_configured && !adap->is_configuring) {
689 if (adap->needs_hpd || msg->msg[0] != 0xf0) {
690 dprintk(1, "%s: adapter is unconfigured\n", __func__);
694 dprintk(1, "%s: invalid msg->reply\n", __func__);
699 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
700 dprintk(1, "%s: transmit queue full\n", __func__);
704 data = kzalloc(sizeof(*data), GFP_KERNEL);
708 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
709 msg->msg[2] = adap->phys_addr >> 8;
710 msg->msg[3] = adap->phys_addr & 0xff;
714 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
715 __func__, msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
717 dprintk(2, "%s: %*ph%s\n",
718 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
723 data->blocking = block;
726 * Determine if this message follows a message from the same
727 * initiator. Needed to determine the free signal time later on.
730 if (!(list_empty(&adap->transmit_queue))) {
731 const struct cec_data *last;
733 last = list_last_entry(&adap->transmit_queue,
734 const struct cec_data, list);
735 last_initiator = cec_msg_initiator(&last->msg);
736 } else if (adap->transmitting) {
738 cec_msg_initiator(&adap->transmitting->msg);
741 data->new_initiator = last_initiator != cec_msg_initiator(msg);
742 init_completion(&data->c);
743 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
746 list_add_tail(&data->xfer_list, &fh->xfer_list);
748 list_add_tail(&data->list, &adap->transmit_queue);
749 adap->transmit_queue_sz++;
750 if (!adap->transmitting)
751 wake_up_interruptible(&adap->kthread_waitq);
753 /* All done if we don't need to block waiting for completion */
758 * If we don't get a completion before this time something is really
759 * wrong and we time out.
761 timeout = CEC_XFER_TIMEOUT_MS;
762 /* Add the requested timeout if we have to wait for a reply as well */
764 timeout += msg->timeout;
767 * Release the lock and wait, retake the lock afterwards.
769 mutex_unlock(&adap->lock);
770 res = wait_for_completion_killable_timeout(&data->c,
771 msecs_to_jiffies(timeout));
772 mutex_lock(&adap->lock);
774 if (data->completed) {
775 /* The transmit completed (possibly with an error) */
781 * The wait for completion timed out or was interrupted, so mark this
782 * as non-blocking and disconnect from the filehandle since it is
783 * still 'in flight'. When it finally completes it will just drop the
786 data->blocking = false;
788 list_del(&data->xfer_list);
791 if (res == 0) { /* timed out */
792 /* Check if the reply or the transmit failed */
793 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
794 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
796 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
798 return res > 0 ? 0 : res;
801 /* Helper function to be used by drivers and this framework. */
802 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
807 mutex_lock(&adap->lock);
808 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
809 mutex_unlock(&adap->lock);
812 EXPORT_SYMBOL_GPL(cec_transmit_msg);
815 * I don't like forward references but without this the low-level
816 * cec_received_msg() function would come after a bunch of high-level
817 * CEC protocol handling functions. That was very confusing.
819 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
822 #define DIRECTED 0x80
823 #define BCAST1_4 0x40
824 #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
825 #define BCAST (BCAST1_4 | BCAST2_0)
826 #define BOTH (BCAST | DIRECTED)
829 * Specify minimum length and whether the message is directed, broadcast
830 * or both. Messages that do not match the criteria are ignored as per
831 * the CEC specification.
833 static const u8 cec_msg_size[256] = {
834 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
835 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
836 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
837 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
838 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
839 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
840 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
841 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
842 [CEC_MSG_STANDBY] = 2 | BOTH,
843 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
844 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
845 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
846 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
847 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
848 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
849 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
850 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
851 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
852 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
853 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
854 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
855 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
856 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
857 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
858 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
859 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
860 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
861 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
862 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
863 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
864 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
865 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
866 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
867 [CEC_MSG_PLAY] = 3 | DIRECTED,
868 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
869 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
870 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
871 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
872 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
873 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
874 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
875 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
876 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
877 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
878 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
879 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
880 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
881 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
882 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
883 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
884 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
885 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
886 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
887 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
888 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
889 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
890 [CEC_MSG_ABORT] = 2 | DIRECTED,
891 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
892 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
893 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
894 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
895 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
896 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
897 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
898 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
899 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
900 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
901 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
902 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
903 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
904 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
905 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
906 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
907 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
908 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
911 /* Called by the CEC adapter if a message is received */
912 void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
914 struct cec_data *data;
915 u8 msg_init = cec_msg_initiator(msg);
916 u8 msg_dest = cec_msg_destination(msg);
917 u8 cmd = msg->msg[1];
918 bool is_reply = false;
919 bool valid_la = true;
922 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
926 * Some CEC adapters will receive the messages that they transmitted.
927 * This test filters out those messages by checking if we are the
928 * initiator, and just returning in that case.
930 * Note that this won't work if this is an Unregistered device.
932 * It is bad practice if the hardware receives the message that it
933 * transmitted and luckily most CEC adapters behave correctly in this
936 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
937 cec_has_log_addr(adap, msg_init))
940 msg->rx_ts = ktime_get_ns();
941 msg->rx_status = CEC_RX_STATUS_OK;
942 msg->sequence = msg->reply = msg->timeout = 0;
945 msg->tx_arb_lost_cnt = 0;
946 msg->tx_nack_cnt = 0;
947 msg->tx_low_drive_cnt = 0;
948 msg->tx_error_cnt = 0;
950 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
952 mutex_lock(&adap->lock);
953 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
955 /* Check if this message was for us (directed or broadcast). */
956 if (!cec_msg_is_broadcast(msg))
957 valid_la = cec_has_log_addr(adap, msg_dest);
960 * Check if the length is not too short or if the message is a
961 * broadcast message where a directed message was expected or
962 * vice versa. If so, then the message has to be ignored (according
963 * to section CEC 7.3 and CEC 12.2).
965 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
966 u8 dir_fl = cec_msg_size[cmd] & BOTH;
968 min_len = cec_msg_size[cmd] & 0x1f;
969 if (msg->len < min_len)
971 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
973 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
975 else if (cec_msg_is_broadcast(msg) &&
976 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
977 !(dir_fl & BCAST2_0))
980 if (valid_la && min_len) {
981 /* These messages have special length requirements */
983 case CEC_MSG_TIMER_STATUS:
984 if (msg->msg[2] & 0x10) {
985 switch (msg->msg[2] & 0xf) {
986 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
987 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
992 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
997 case CEC_MSG_RECORD_ON:
998 switch (msg->msg[2]) {
999 case CEC_OP_RECORD_SRC_OWN:
1001 case CEC_OP_RECORD_SRC_DIGITAL:
1005 case CEC_OP_RECORD_SRC_ANALOG:
1009 case CEC_OP_RECORD_SRC_EXT_PLUG:
1013 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1022 /* It's a valid message and not a poll or CDC message */
1023 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1024 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1026 /* The aborted command is in msg[2] */
1031 * Walk over all transmitted messages that are waiting for a
1034 list_for_each_entry(data, &adap->wait_queue, list) {
1035 struct cec_msg *dst = &data->msg;
1038 * The *only* CEC message that has two possible replies
1039 * is CEC_MSG_INITIATE_ARC.
1040 * In this case allow either of the two replies.
1042 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1043 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1044 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1045 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1046 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1049 /* Does the command match? */
1050 if ((abort && cmd != dst->msg[1]) ||
1051 (!abort && cmd != dst->reply))
1054 /* Does the addressing match? */
1055 if (msg_init != cec_msg_destination(dst) &&
1056 !cec_msg_is_broadcast(dst))
1059 /* We got a reply */
1060 memcpy(dst->msg, msg->msg, msg->len);
1061 dst->len = msg->len;
1062 dst->rx_ts = msg->rx_ts;
1063 dst->rx_status = msg->rx_status;
1065 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1066 msg->flags = dst->flags;
1067 /* Remove it from the wait_queue */
1068 list_del_init(&data->list);
1070 /* Cancel the pending timeout work */
1071 if (!cancel_delayed_work(&data->work)) {
1072 mutex_unlock(&adap->lock);
1073 flush_scheduled_work();
1074 mutex_lock(&adap->lock);
1077 * Mark this as a reply, provided someone is still
1078 * waiting for the answer.
1082 cec_data_completed(data);
1086 mutex_unlock(&adap->lock);
1088 /* Pass the message on to any monitoring filehandles */
1089 cec_queue_msg_monitor(adap, msg, valid_la);
1091 /* We're done if it is not for us or a poll message */
1092 if (!valid_la || msg->len <= 1)
1095 if (adap->log_addrs.log_addr_mask == 0)
1099 * Process the message on the protocol level. If is_reply is true,
1100 * then cec_receive_notify() won't pass on the reply to the listener(s)
1101 * since that was already done by cec_data_completed() above.
1103 cec_receive_notify(adap, msg, is_reply);
1105 EXPORT_SYMBOL_GPL(cec_received_msg);
1107 /* Logical Address Handling */
1110 * Attempt to claim a specific logical address.
1112 * This function is called with adap->lock held.
1114 static int cec_config_log_addr(struct cec_adapter *adap,
1116 unsigned int log_addr)
1118 struct cec_log_addrs *las = &adap->log_addrs;
1119 struct cec_msg msg = { };
1122 if (cec_has_log_addr(adap, log_addr))
1125 /* Send poll message */
1127 msg.msg[0] = (log_addr << 4) | log_addr;
1128 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1131 * While trying to poll the physical address was reset
1132 * and the adapter was unconfigured, so bail out.
1134 if (!adap->is_configuring)
1140 if (msg.tx_status & CEC_TX_STATUS_OK)
1144 * Message not acknowledged, so this logical
1145 * address is free to use.
1147 err = adap->ops->adap_log_addr(adap, log_addr);
1151 las->log_addr[idx] = log_addr;
1152 las->log_addr_mask |= 1 << log_addr;
1153 adap->phys_addrs[log_addr] = adap->phys_addr;
1158 * Unconfigure the adapter: clear all logical addresses and send
1159 * the state changed event.
1161 * This function is called with adap->lock held.
1163 static void cec_adap_unconfigure(struct cec_adapter *adap)
1165 if (!adap->needs_hpd ||
1166 adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1167 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1168 adap->log_addrs.log_addr_mask = 0;
1169 adap->is_configuring = false;
1170 adap->is_configured = false;
1171 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1173 wake_up_interruptible(&adap->kthread_waitq);
1174 cec_post_state_event(adap);
1178 * Attempt to claim the required logical addresses.
1180 static int cec_config_thread_func(void *arg)
1182 /* The various LAs for each type of device */
1183 static const u8 tv_log_addrs[] = {
1184 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1185 CEC_LOG_ADDR_INVALID
1187 static const u8 record_log_addrs[] = {
1188 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1189 CEC_LOG_ADDR_RECORD_3,
1190 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1191 CEC_LOG_ADDR_INVALID
1193 static const u8 tuner_log_addrs[] = {
1194 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1195 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1196 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1197 CEC_LOG_ADDR_INVALID
1199 static const u8 playback_log_addrs[] = {
1200 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1201 CEC_LOG_ADDR_PLAYBACK_3,
1202 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1203 CEC_LOG_ADDR_INVALID
1205 static const u8 audiosystem_log_addrs[] = {
1206 CEC_LOG_ADDR_AUDIOSYSTEM,
1207 CEC_LOG_ADDR_INVALID
1209 static const u8 specific_use_log_addrs[] = {
1210 CEC_LOG_ADDR_SPECIFIC,
1211 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1212 CEC_LOG_ADDR_INVALID
1214 static const u8 *type2addrs[6] = {
1215 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1216 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1217 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1218 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1219 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1220 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1222 static const u16 type2mask[] = {
1223 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1224 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1225 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1226 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1227 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1228 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1230 struct cec_adapter *adap = arg;
1231 struct cec_log_addrs *las = &adap->log_addrs;
1235 mutex_lock(&adap->lock);
1236 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1237 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1238 las->log_addr_mask = 0;
1240 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1243 for (i = 0; i < las->num_log_addrs; i++) {
1244 unsigned int type = las->log_addr_type[i];
1249 * The TV functionality can only map to physical address 0.
1250 * For any other address, try the Specific functionality
1251 * instead as per the spec.
1253 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1254 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1256 la_list = type2addrs[type];
1257 last_la = las->log_addr[i];
1258 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1259 if (last_la == CEC_LOG_ADDR_INVALID ||
1260 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1261 !((1 << last_la) & type2mask[type]))
1262 last_la = la_list[0];
1264 err = cec_config_log_addr(adap, i, last_la);
1265 if (err > 0) /* Reused last LA */
1271 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1272 /* Tried this one already, skip it */
1273 if (la_list[j] == last_la)
1275 /* The backup addresses are CEC 2.0 specific */
1276 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1277 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1278 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1281 err = cec_config_log_addr(adap, i, la_list[j]);
1282 if (err == 0) /* LA is in use */
1286 /* Done, claimed an LA */
1290 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1291 dprintk(1, "could not claim LA %d\n", i);
1294 if (adap->log_addrs.log_addr_mask == 0 &&
1295 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1299 if (adap->log_addrs.log_addr_mask == 0) {
1300 /* Fall back to unregistered */
1301 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1302 las->log_addr_mask = 1 << las->log_addr[0];
1303 for (i = 1; i < las->num_log_addrs; i++)
1304 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1306 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1307 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1308 adap->is_configured = true;
1309 adap->is_configuring = false;
1310 cec_post_state_event(adap);
1313 * Now post the Report Features and Report Physical Address broadcast
1314 * messages. Note that these are non-blocking transmits, meaning that
1315 * they are just queued up and once adap->lock is unlocked the main
1316 * thread will kick in and start transmitting these.
1318 * If after this function is done (but before one or more of these
1319 * messages are actually transmitted) the CEC adapter is unconfigured,
1320 * then any remaining messages will be dropped by the main thread.
1322 for (i = 0; i < las->num_log_addrs; i++) {
1323 struct cec_msg msg = {};
1325 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1326 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1329 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1331 /* Report Features must come first according to CEC 2.0 */
1332 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1333 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1334 cec_fill_msg_report_features(adap, &msg, i);
1335 cec_transmit_msg_fh(adap, &msg, NULL, false);
1338 /* Report Physical Address */
1339 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1340 las->primary_device_type[i]);
1341 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1343 cec_phys_addr_exp(adap->phys_addr));
1344 cec_transmit_msg_fh(adap, &msg, NULL, false);
1346 adap->kthread_config = NULL;
1347 complete(&adap->config_completion);
1348 mutex_unlock(&adap->lock);
1352 for (i = 0; i < las->num_log_addrs; i++)
1353 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1354 cec_adap_unconfigure(adap);
1355 adap->kthread_config = NULL;
1356 mutex_unlock(&adap->lock);
1357 complete(&adap->config_completion);
1362 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1363 * logical addresses.
1365 * This function is called with adap->lock held.
1367 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1369 if (WARN_ON(adap->is_configuring || adap->is_configured))
1372 init_completion(&adap->config_completion);
1374 /* Ready to kick off the thread */
1375 adap->is_configuring = true;
1376 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1377 "ceccfg-%s", adap->name);
1378 if (IS_ERR(adap->kthread_config)) {
1379 adap->kthread_config = NULL;
1381 mutex_unlock(&adap->lock);
1382 wait_for_completion(&adap->config_completion);
1383 mutex_lock(&adap->lock);
1387 /* Set a new physical address and send an event notifying userspace of this.
1389 * This function is called with adap->lock held.
1391 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1393 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
1396 dprintk(1, "new physical address %x.%x.%x.%x\n",
1397 cec_phys_addr_exp(phys_addr));
1398 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1399 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1400 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1401 cec_post_state_event(adap);
1402 cec_adap_unconfigure(adap);
1403 /* Disabling monitor all mode should always succeed */
1404 if (adap->monitor_all_cnt)
1405 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1406 mutex_lock(&adap->devnode.lock);
1407 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1408 WARN_ON(adap->ops->adap_enable(adap, false));
1409 mutex_unlock(&adap->devnode.lock);
1410 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1414 mutex_lock(&adap->devnode.lock);
1415 if ((adap->needs_hpd || list_empty(&adap->devnode.fhs)) &&
1416 adap->ops->adap_enable(adap, true)) {
1417 mutex_unlock(&adap->devnode.lock);
1421 if (adap->monitor_all_cnt &&
1422 call_op(adap, adap_monitor_all_enable, true)) {
1423 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1424 WARN_ON(adap->ops->adap_enable(adap, false));
1425 mutex_unlock(&adap->devnode.lock);
1428 mutex_unlock(&adap->devnode.lock);
1430 adap->phys_addr = phys_addr;
1431 cec_post_state_event(adap);
1432 if (adap->log_addrs.num_log_addrs)
1433 cec_claim_log_addrs(adap, block);
1436 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1438 if (IS_ERR_OR_NULL(adap))
1441 mutex_lock(&adap->lock);
1442 __cec_s_phys_addr(adap, phys_addr, block);
1443 mutex_unlock(&adap->lock);
1445 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1447 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1448 const struct edid *edid)
1450 u16 pa = CEC_PHYS_ADDR_INVALID;
1452 if (edid && edid->extensions)
1453 pa = cec_get_edid_phys_addr((const u8 *)edid,
1454 EDID_LENGTH * (edid->extensions + 1), NULL);
1455 cec_s_phys_addr(adap, pa, false);
1457 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1460 * Called from either the ioctl or a driver to set the logical addresses.
1462 * This function is called with adap->lock held.
1464 int __cec_s_log_addrs(struct cec_adapter *adap,
1465 struct cec_log_addrs *log_addrs, bool block)
1470 if (adap->devnode.unregistered)
1473 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1474 adap->log_addrs.num_log_addrs = 0;
1475 cec_adap_unconfigure(adap);
1479 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1481 * Sanitize log_addrs fields if a CDC-Only device is
1484 log_addrs->num_log_addrs = 1;
1485 log_addrs->osd_name[0] = '\0';
1486 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1487 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1489 * This is just an internal convention since a CDC-Only device
1490 * doesn't have to be a switch. But switches already use
1491 * unregistered, so it makes some kind of sense to pick this
1492 * as the primary device. Since a CDC-Only device never sends
1493 * any 'normal' CEC messages this primary device type is never
1494 * sent over the CEC bus.
1496 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1497 log_addrs->all_device_types[0] = 0;
1498 log_addrs->features[0][0] = 0;
1499 log_addrs->features[0][1] = 0;
1502 /* Ensure the osd name is 0-terminated */
1503 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1506 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1507 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1512 * Vendor ID is a 24 bit number, so check if the value is
1513 * within the correct range.
1515 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1516 (log_addrs->vendor_id & 0xff000000) != 0) {
1517 dprintk(1, "invalid vendor ID\n");
1521 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1522 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1523 dprintk(1, "invalid CEC version\n");
1527 if (log_addrs->num_log_addrs > 1)
1528 for (i = 0; i < log_addrs->num_log_addrs; i++)
1529 if (log_addrs->log_addr_type[i] ==
1530 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1531 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1535 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1536 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1537 u8 *features = log_addrs->features[i];
1538 bool op_is_dev_features = false;
1541 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1542 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1543 dprintk(1, "duplicate logical address type\n");
1546 type_mask |= 1 << log_addrs->log_addr_type[i];
1547 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1548 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1549 /* Record already contains the playback functionality */
1550 dprintk(1, "invalid record + playback combination\n");
1553 if (log_addrs->primary_device_type[i] >
1554 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1555 dprintk(1, "unknown primary device type\n");
1558 if (log_addrs->primary_device_type[i] == 2) {
1559 dprintk(1, "invalid primary device type\n");
1562 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1563 dprintk(1, "unknown logical address type\n");
1566 for (j = 0; j < feature_sz; j++) {
1567 if ((features[j] & 0x80) == 0) {
1568 if (op_is_dev_features)
1570 op_is_dev_features = true;
1573 if (!op_is_dev_features || j == feature_sz) {
1574 dprintk(1, "malformed features\n");
1577 /* Zero unused part of the feature array */
1578 memset(features + j + 1, 0, feature_sz - j - 1);
1581 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1582 if (log_addrs->num_log_addrs > 2) {
1583 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1586 if (log_addrs->num_log_addrs == 2) {
1587 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1588 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1589 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1592 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1593 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1594 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1600 /* Zero unused LAs */
1601 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1602 log_addrs->primary_device_type[i] = 0;
1603 log_addrs->log_addr_type[i] = 0;
1604 log_addrs->all_device_types[i] = 0;
1605 memset(log_addrs->features[i], 0,
1606 sizeof(log_addrs->features[i]));
1609 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1610 adap->log_addrs = *log_addrs;
1611 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1612 cec_claim_log_addrs(adap, block);
1616 int cec_s_log_addrs(struct cec_adapter *adap,
1617 struct cec_log_addrs *log_addrs, bool block)
1621 mutex_lock(&adap->lock);
1622 err = __cec_s_log_addrs(adap, log_addrs, block);
1623 mutex_unlock(&adap->lock);
1626 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1628 /* High-level core CEC message handling */
1630 /* Fill in the Report Features message */
1631 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1632 struct cec_msg *msg,
1633 unsigned int la_idx)
1635 const struct cec_log_addrs *las = &adap->log_addrs;
1636 const u8 *features = las->features[la_idx];
1637 bool op_is_dev_features = false;
1640 /* Report Features */
1641 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1643 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1644 msg->msg[2] = adap->log_addrs.cec_version;
1645 msg->msg[3] = las->all_device_types[la_idx];
1647 /* Write RC Profiles first, then Device Features */
1648 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1649 msg->msg[msg->len++] = features[idx];
1650 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1651 if (op_is_dev_features)
1653 op_is_dev_features = true;
1658 /* Transmit the Feature Abort message */
1659 static int cec_feature_abort_reason(struct cec_adapter *adap,
1660 struct cec_msg *msg, u8 reason)
1662 struct cec_msg tx_msg = { };
1665 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1668 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1670 /* Don't Feature Abort messages from 'Unregistered' */
1671 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1673 cec_msg_set_reply_to(&tx_msg, msg);
1674 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1675 return cec_transmit_msg(adap, &tx_msg, false);
1678 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1680 return cec_feature_abort_reason(adap, msg,
1681 CEC_OP_ABORT_UNRECOGNIZED_OP);
1684 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1686 return cec_feature_abort_reason(adap, msg,
1687 CEC_OP_ABORT_REFUSED);
1691 * Called when a CEC message is received. This function will do any
1692 * necessary core processing. The is_reply bool is true if this message
1693 * is a reply to an earlier transmit.
1695 * The message is either a broadcast message or a valid directed message.
1697 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1700 bool is_broadcast = cec_msg_is_broadcast(msg);
1701 u8 dest_laddr = cec_msg_destination(msg);
1702 u8 init_laddr = cec_msg_initiator(msg);
1703 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1704 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1705 bool from_unregistered = init_laddr == 0xf;
1706 struct cec_msg tx_cec_msg = { };
1708 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1710 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1711 if (cec_is_cdc_only(&adap->log_addrs) &&
1712 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1715 if (adap->ops->received) {
1716 /* Allow drivers to process the message first */
1717 if (adap->ops->received(adap, msg) != -ENOMSG)
1722 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1723 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1724 * handled by the CEC core, even if the passthrough mode is on.
1725 * The others are just ignored if passthrough mode is on.
1727 switch (msg->msg[1]) {
1728 case CEC_MSG_GET_CEC_VERSION:
1729 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1731 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1732 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1733 case CEC_MSG_GIVE_OSD_NAME:
1734 case CEC_MSG_GIVE_FEATURES:
1736 * Skip processing these messages if the passthrough mode
1739 if (adap->passthrough)
1740 goto skip_processing;
1741 /* Ignore if addressing is wrong */
1742 if (is_broadcast || from_unregistered)
1746 case CEC_MSG_USER_CONTROL_PRESSED:
1747 case CEC_MSG_USER_CONTROL_RELEASED:
1748 /* Wrong addressing mode: don't process */
1749 if (is_broadcast || from_unregistered)
1750 goto skip_processing;
1753 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1755 * This message is always processed, regardless of the
1756 * passthrough setting.
1758 * Exception: don't process if wrong addressing mode.
1761 goto skip_processing;
1768 cec_msg_set_reply_to(&tx_cec_msg, msg);
1770 switch (msg->msg[1]) {
1771 /* The following messages are processed but still passed through */
1772 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1773 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1775 if (!from_unregistered)
1776 adap->phys_addrs[init_laddr] = pa;
1777 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
1778 cec_phys_addr_exp(pa), init_laddr);
1782 case CEC_MSG_USER_CONTROL_PRESSED:
1783 if (!(adap->capabilities & CEC_CAP_RC) ||
1784 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1787 #ifdef CONFIG_MEDIA_CEC_RC
1788 switch (msg->msg[2]) {
1790 * Play function, this message can have variable length
1791 * depending on the specific play function that is used.
1795 rc_keydown(adap->rc, RC_TYPE_CEC,
1798 rc_keydown(adap->rc, RC_TYPE_CEC,
1799 msg->msg[2] << 8 | msg->msg[3], 0);
1802 * Other function messages that are not handled.
1803 * Currently the RC framework does not allow to supply an
1804 * additional parameter to a keypress. These "keys" contain
1805 * other information such as channel number, an input number
1807 * For the time being these messages are not processed by the
1808 * framework and are simply forwarded to the user space.
1810 case 0x56: case 0x57:
1811 case 0x67: case 0x68: case 0x69: case 0x6a:
1814 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1820 case CEC_MSG_USER_CONTROL_RELEASED:
1821 if (!(adap->capabilities & CEC_CAP_RC) ||
1822 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1824 #ifdef CONFIG_MEDIA_CEC_RC
1830 * The remaining messages are only processed if the passthrough mode
1833 case CEC_MSG_GET_CEC_VERSION:
1834 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1835 return cec_transmit_msg(adap, &tx_cec_msg, false);
1837 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1838 /* Do nothing for CEC switches using addr 15 */
1839 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1841 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1842 return cec_transmit_msg(adap, &tx_cec_msg, false);
1844 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1845 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1846 return cec_feature_abort(adap, msg);
1847 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1848 return cec_transmit_msg(adap, &tx_cec_msg, false);
1851 /* Do nothing for CEC switches */
1852 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1854 return cec_feature_refused(adap, msg);
1856 case CEC_MSG_GIVE_OSD_NAME: {
1857 if (adap->log_addrs.osd_name[0] == 0)
1858 return cec_feature_abort(adap, msg);
1859 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1860 return cec_transmit_msg(adap, &tx_cec_msg, false);
1863 case CEC_MSG_GIVE_FEATURES:
1864 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1865 return cec_feature_abort(adap, msg);
1866 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1867 return cec_transmit_msg(adap, &tx_cec_msg, false);
1871 * Unprocessed messages are aborted if userspace isn't doing
1872 * any processing either.
1874 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
1875 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1876 return cec_feature_abort(adap, msg);
1881 /* If this was a reply, then we're done, unless otherwise specified */
1882 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
1886 * Send to the exclusive follower if there is one, otherwise send
1889 if (adap->cec_follower)
1890 cec_queue_msg_fh(adap->cec_follower, msg);
1892 cec_queue_msg_followers(adap, msg);
1897 * Helper functions to keep track of the 'monitor all' use count.
1899 * These functions are called with adap->lock held.
1901 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1905 if (adap->monitor_all_cnt == 0)
1906 ret = call_op(adap, adap_monitor_all_enable, 1);
1908 adap->monitor_all_cnt++;
1912 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1914 adap->monitor_all_cnt--;
1915 if (adap->monitor_all_cnt == 0)
1916 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1919 #ifdef CONFIG_DEBUG_FS
1921 * Log the current state of the CEC adapter.
1922 * Very useful for debugging.
1924 int cec_adap_status(struct seq_file *file, void *priv)
1926 struct cec_adapter *adap = dev_get_drvdata(file->private);
1927 struct cec_data *data;
1929 mutex_lock(&adap->lock);
1930 seq_printf(file, "configured: %d\n", adap->is_configured);
1931 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1932 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1933 cec_phys_addr_exp(adap->phys_addr));
1934 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1935 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1936 if (adap->cec_follower)
1937 seq_printf(file, "has CEC follower%s\n",
1938 adap->passthrough ? " (in passthrough mode)" : "");
1939 if (adap->cec_initiator)
1940 seq_puts(file, "has CEC initiator\n");
1941 if (adap->monitor_all_cnt)
1942 seq_printf(file, "file handles in Monitor All mode: %u\n",
1943 adap->monitor_all_cnt);
1944 data = adap->transmitting;
1946 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1947 data->msg.len, data->msg.msg, data->msg.reply,
1949 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
1950 list_for_each_entry(data, &adap->transmit_queue, list) {
1951 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1952 data->msg.len, data->msg.msg, data->msg.reply,
1955 list_for_each_entry(data, &adap->wait_queue, list) {
1956 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1957 data->msg.len, data->msg.msg, data->msg.reply,
1961 call_void_op(adap, adap_status, file);
1962 mutex_unlock(&adap->lock);