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,
371 kthread_should_stop() ||
372 (!adap->transmitting &&
373 !list_empty(&adap->transmit_queue)),
374 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
377 /* Otherwise we just wait for something to happen. */
378 wait_event_interruptible(adap->kthread_waitq,
379 kthread_should_stop() ||
380 (!adap->transmitting &&
381 !list_empty(&adap->transmit_queue)));
384 mutex_lock(&adap->lock);
386 if (kthread_should_stop()) {
391 if (adap->transmitting && timeout) {
393 * If we timeout, then log that. This really shouldn't
394 * happen and is an indication of a faulty CEC adapter
395 * driver, or the CEC bus is in some weird state.
397 dprintk(0, "%s: message %*ph timed out!\n", __func__,
398 adap->transmitting->msg.len,
399 adap->transmitting->msg.msg);
400 /* Just give up on this. */
401 cec_data_cancel(adap->transmitting);
406 * If we are still transmitting, or there is nothing new to
407 * transmit, then just continue waiting.
409 if (adap->transmitting || list_empty(&adap->transmit_queue))
412 /* Get a new message to transmit */
413 data = list_first_entry(&adap->transmit_queue,
414 struct cec_data, list);
415 list_del_init(&data->list);
416 adap->transmit_queue_sz--;
418 /* Make this the current transmitting message */
419 adap->transmitting = data;
422 * Suggested number of attempts as per the CEC 2.0 spec:
423 * 4 attempts is the default, except for 'secondary poll
424 * messages', i.e. poll messages not sent during the adapter
425 * configuration phase when it allocates logical addresses.
427 if (data->msg.len == 1 && adap->is_configured)
432 /* Set the suggested signal free time */
433 if (data->attempts) {
434 /* should be >= 3 data bit periods for a retry */
435 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
436 } else if (data->new_initiator) {
437 /* should be >= 5 data bit periods for new initiator */
438 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
441 * should be >= 7 data bit periods for sending another
442 * frame immediately after another.
444 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
446 if (data->attempts == 0)
447 data->attempts = attempts;
449 /* Tell the adapter to transmit, cancel on error */
450 if (adap->ops->adap_transmit(adap, data->attempts,
451 signal_free_time, &data->msg))
452 cec_data_cancel(data);
455 mutex_unlock(&adap->lock);
457 if (kthread_should_stop())
464 * Called by the CEC adapter if a transmit finished.
466 void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
467 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
469 struct cec_data *data;
471 u64 ts = ktime_get_ns();
473 dprintk(2, "%s: status %02x\n", __func__, status);
474 mutex_lock(&adap->lock);
475 data = adap->transmitting;
478 * This can happen if a transmit was issued and the cable is
479 * unplugged while the transmit is ongoing. Ignore this
480 * transmit in that case.
482 dprintk(1, "%s was called without an ongoing transmit!\n",
489 /* Drivers must fill in the status! */
490 WARN_ON(status == 0);
492 msg->tx_status |= status;
493 msg->tx_arb_lost_cnt += arb_lost_cnt;
494 msg->tx_nack_cnt += nack_cnt;
495 msg->tx_low_drive_cnt += low_drive_cnt;
496 msg->tx_error_cnt += error_cnt;
498 /* Mark that we're done with this transmit */
499 adap->transmitting = NULL;
502 * If there are still retry attempts left and there was an error and
503 * the hardware didn't signal that it retried itself (by setting
504 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
506 if (data->attempts > 1 &&
507 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
508 /* Retry this message */
511 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
512 msg->len, msg->msg, data->attempts, msg->reply);
514 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
515 msg->len, msg->msg, data->attempts);
516 /* Add the message in front of the transmit queue */
517 list_add(&data->list, &adap->transmit_queue);
518 adap->transmit_queue_sz++;
524 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
525 if (!(status & CEC_TX_STATUS_OK))
526 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
528 /* Queue transmitted message for monitoring purposes */
529 cec_queue_msg_monitor(adap, msg, 1);
531 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
534 * Queue the message into the wait queue if we want to wait
537 list_add_tail(&data->list, &adap->wait_queue);
538 schedule_delayed_work(&data->work,
539 msecs_to_jiffies(msg->timeout));
541 /* Otherwise we're done */
542 cec_data_completed(data);
547 * Wake up the main thread to see if another message is ready
548 * for transmitting or to retry the current message.
550 wake_up_interruptible(&adap->kthread_waitq);
552 mutex_unlock(&adap->lock);
554 EXPORT_SYMBOL_GPL(cec_transmit_done);
556 void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status)
559 case CEC_TX_STATUS_OK:
560 cec_transmit_done(adap, status, 0, 0, 0, 0);
562 case CEC_TX_STATUS_ARB_LOST:
563 cec_transmit_done(adap, status, 1, 0, 0, 0);
565 case CEC_TX_STATUS_NACK:
566 cec_transmit_done(adap, status, 0, 1, 0, 0);
568 case CEC_TX_STATUS_LOW_DRIVE:
569 cec_transmit_done(adap, status, 0, 0, 1, 0);
571 case CEC_TX_STATUS_ERROR:
572 cec_transmit_done(adap, status, 0, 0, 0, 1);
575 /* Should never happen */
576 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
580 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done);
583 * Called when waiting for a reply times out.
585 static void cec_wait_timeout(struct work_struct *work)
587 struct cec_data *data = container_of(work, struct cec_data, work.work);
588 struct cec_adapter *adap = data->adap;
590 mutex_lock(&adap->lock);
592 * Sanity check in case the timeout and the arrival of the message
593 * happened at the same time.
595 if (list_empty(&data->list))
598 /* Mark the message as timed out */
599 list_del_init(&data->list);
600 data->msg.rx_ts = ktime_get_ns();
601 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
602 cec_data_completed(data);
604 mutex_unlock(&adap->lock);
608 * Transmit a message. The fh argument may be NULL if the transmit is not
609 * associated with a specific filehandle.
611 * This function is called with adap->lock held.
613 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
614 struct cec_fh *fh, bool block)
616 struct cec_data *data;
617 u8 last_initiator = 0xff;
618 unsigned int timeout;
625 msg->tx_arb_lost_cnt = 0;
626 msg->tx_nack_cnt = 0;
627 msg->tx_low_drive_cnt = 0;
628 msg->tx_error_cnt = 0;
629 msg->sequence = ++adap->sequence;
631 msg->sequence = ++adap->sequence;
633 if (msg->reply && msg->timeout == 0) {
634 /* Make sure the timeout isn't 0. */
638 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
643 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
644 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
647 if (msg->timeout && msg->len == 1) {
648 dprintk(1, "%s: can't reply for poll msg\n", __func__);
651 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
653 if (cec_msg_destination(msg) == 0xf) {
654 dprintk(1, "%s: invalid poll message\n", __func__);
657 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
659 * If the destination is a logical address our adapter
660 * has already claimed, then just NACK this.
661 * It depends on the hardware what it will do with a
662 * POLL to itself (some OK this), so it is just as
663 * easy to handle it here so the behavior will be
666 msg->tx_ts = ktime_get_ns();
667 msg->tx_status = CEC_TX_STATUS_NACK |
668 CEC_TX_STATUS_MAX_RETRIES;
669 msg->tx_nack_cnt = 1;
673 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
674 cec_has_log_addr(adap, cec_msg_destination(msg))) {
675 dprintk(1, "%s: destination is the adapter itself\n", __func__);
678 if (msg->len > 1 && adap->is_configured &&
679 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
680 dprintk(1, "%s: initiator has unknown logical address %d\n",
681 __func__, cec_msg_initiator(msg));
684 if (!adap->is_configured && !adap->is_configuring) {
685 if (msg->msg[0] != 0xf0) {
686 dprintk(1, "%s: adapter is unconfigured\n", __func__);
690 dprintk(1, "%s: invalid msg->reply\n", __func__);
695 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
696 dprintk(1, "%s: transmit queue full\n", __func__);
700 data = kzalloc(sizeof(*data), GFP_KERNEL);
704 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
705 msg->msg[2] = adap->phys_addr >> 8;
706 msg->msg[3] = adap->phys_addr & 0xff;
710 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
711 __func__, msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
713 dprintk(2, "%s: %*ph%s\n",
714 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
719 data->blocking = block;
722 * Determine if this message follows a message from the same
723 * initiator. Needed to determine the free signal time later on.
726 if (!(list_empty(&adap->transmit_queue))) {
727 const struct cec_data *last;
729 last = list_last_entry(&adap->transmit_queue,
730 const struct cec_data, list);
731 last_initiator = cec_msg_initiator(&last->msg);
732 } else if (adap->transmitting) {
734 cec_msg_initiator(&adap->transmitting->msg);
737 data->new_initiator = last_initiator != cec_msg_initiator(msg);
738 init_completion(&data->c);
739 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
742 list_add_tail(&data->xfer_list, &fh->xfer_list);
744 list_add_tail(&data->list, &adap->transmit_queue);
745 adap->transmit_queue_sz++;
746 if (!adap->transmitting)
747 wake_up_interruptible(&adap->kthread_waitq);
749 /* All done if we don't need to block waiting for completion */
754 * If we don't get a completion before this time something is really
755 * wrong and we time out.
757 timeout = CEC_XFER_TIMEOUT_MS;
758 /* Add the requested timeout if we have to wait for a reply as well */
760 timeout += msg->timeout;
763 * Release the lock and wait, retake the lock afterwards.
765 mutex_unlock(&adap->lock);
766 res = wait_for_completion_killable_timeout(&data->c,
767 msecs_to_jiffies(timeout));
768 mutex_lock(&adap->lock);
770 if (data->completed) {
771 /* The transmit completed (possibly with an error) */
777 * The wait for completion timed out or was interrupted, so mark this
778 * as non-blocking and disconnect from the filehandle since it is
779 * still 'in flight'. When it finally completes it will just drop the
782 data->blocking = false;
784 list_del(&data->xfer_list);
787 if (res == 0) { /* timed out */
788 /* Check if the reply or the transmit failed */
789 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
790 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
792 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
794 return res > 0 ? 0 : res;
797 /* Helper function to be used by drivers and this framework. */
798 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
803 mutex_lock(&adap->lock);
804 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
805 mutex_unlock(&adap->lock);
808 EXPORT_SYMBOL_GPL(cec_transmit_msg);
811 * I don't like forward references but without this the low-level
812 * cec_received_msg() function would come after a bunch of high-level
813 * CEC protocol handling functions. That was very confusing.
815 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
818 #define DIRECTED 0x80
819 #define BCAST1_4 0x40
820 #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
821 #define BCAST (BCAST1_4 | BCAST2_0)
822 #define BOTH (BCAST | DIRECTED)
825 * Specify minimum length and whether the message is directed, broadcast
826 * or both. Messages that do not match the criteria are ignored as per
827 * the CEC specification.
829 static const u8 cec_msg_size[256] = {
830 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
831 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
832 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
833 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
834 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
835 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
836 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
837 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
838 [CEC_MSG_STANDBY] = 2 | BOTH,
839 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
840 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
841 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
842 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
843 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
844 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
845 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
846 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
847 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
848 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
849 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
850 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
851 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
852 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
853 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
854 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
855 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
856 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
857 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
858 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
859 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
860 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
861 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
862 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
863 [CEC_MSG_PLAY] = 3 | DIRECTED,
864 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
865 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
866 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
867 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
868 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
869 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
870 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
871 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
872 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
873 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
874 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
875 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
876 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
877 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
878 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
879 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
880 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
881 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
882 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
883 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
884 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
885 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
886 [CEC_MSG_ABORT] = 2 | DIRECTED,
887 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
888 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
889 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
890 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
891 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
892 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
893 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
894 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
895 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
896 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
897 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
898 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
899 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
900 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
901 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
902 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
903 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
904 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
907 /* Called by the CEC adapter if a message is received */
908 void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
910 struct cec_data *data;
911 u8 msg_init = cec_msg_initiator(msg);
912 u8 msg_dest = cec_msg_destination(msg);
913 u8 cmd = msg->msg[1];
914 bool is_reply = false;
915 bool valid_la = true;
918 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
922 * Some CEC adapters will receive the messages that they transmitted.
923 * This test filters out those messages by checking if we are the
924 * initiator, and just returning in that case.
926 * Note that this won't work if this is an Unregistered device.
928 * It is bad practice if the hardware receives the message that it
929 * transmitted and luckily most CEC adapters behave correctly in this
932 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
933 cec_has_log_addr(adap, msg_init))
936 msg->rx_ts = ktime_get_ns();
937 msg->rx_status = CEC_RX_STATUS_OK;
938 msg->sequence = msg->reply = msg->timeout = 0;
941 msg->tx_arb_lost_cnt = 0;
942 msg->tx_nack_cnt = 0;
943 msg->tx_low_drive_cnt = 0;
944 msg->tx_error_cnt = 0;
946 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
948 mutex_lock(&adap->lock);
949 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
951 /* Check if this message was for us (directed or broadcast). */
952 if (!cec_msg_is_broadcast(msg))
953 valid_la = cec_has_log_addr(adap, msg_dest);
956 * Check if the length is not too short or if the message is a
957 * broadcast message where a directed message was expected or
958 * vice versa. If so, then the message has to be ignored (according
959 * to section CEC 7.3 and CEC 12.2).
961 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
962 u8 dir_fl = cec_msg_size[cmd] & BOTH;
964 min_len = cec_msg_size[cmd] & 0x1f;
965 if (msg->len < min_len)
967 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
969 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
971 else if (cec_msg_is_broadcast(msg) &&
972 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
973 !(dir_fl & BCAST2_0))
976 if (valid_la && min_len) {
977 /* These messages have special length requirements */
979 case CEC_MSG_TIMER_STATUS:
980 if (msg->msg[2] & 0x10) {
981 switch (msg->msg[2] & 0xf) {
982 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
983 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
988 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
993 case CEC_MSG_RECORD_ON:
994 switch (msg->msg[2]) {
995 case CEC_OP_RECORD_SRC_OWN:
997 case CEC_OP_RECORD_SRC_DIGITAL:
1001 case CEC_OP_RECORD_SRC_ANALOG:
1005 case CEC_OP_RECORD_SRC_EXT_PLUG:
1009 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1018 /* It's a valid message and not a poll or CDC message */
1019 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1020 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1022 /* The aborted command is in msg[2] */
1027 * Walk over all transmitted messages that are waiting for a
1030 list_for_each_entry(data, &adap->wait_queue, list) {
1031 struct cec_msg *dst = &data->msg;
1034 * The *only* CEC message that has two possible replies
1035 * is CEC_MSG_INITIATE_ARC.
1036 * In this case allow either of the two replies.
1038 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1039 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1040 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1041 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1042 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1045 /* Does the command match? */
1046 if ((abort && cmd != dst->msg[1]) ||
1047 (!abort && cmd != dst->reply))
1050 /* Does the addressing match? */
1051 if (msg_init != cec_msg_destination(dst) &&
1052 !cec_msg_is_broadcast(dst))
1055 /* We got a reply */
1056 memcpy(dst->msg, msg->msg, msg->len);
1057 dst->len = msg->len;
1058 dst->rx_ts = msg->rx_ts;
1059 dst->rx_status = msg->rx_status;
1061 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1062 msg->flags = dst->flags;
1063 /* Remove it from the wait_queue */
1064 list_del_init(&data->list);
1066 /* Cancel the pending timeout work */
1067 if (!cancel_delayed_work(&data->work)) {
1068 mutex_unlock(&adap->lock);
1069 flush_scheduled_work();
1070 mutex_lock(&adap->lock);
1073 * Mark this as a reply, provided someone is still
1074 * waiting for the answer.
1078 cec_data_completed(data);
1082 mutex_unlock(&adap->lock);
1084 /* Pass the message on to any monitoring filehandles */
1085 cec_queue_msg_monitor(adap, msg, valid_la);
1087 /* We're done if it is not for us or a poll message */
1088 if (!valid_la || msg->len <= 1)
1091 if (adap->log_addrs.log_addr_mask == 0)
1095 * Process the message on the protocol level. If is_reply is true,
1096 * then cec_receive_notify() won't pass on the reply to the listener(s)
1097 * since that was already done by cec_data_completed() above.
1099 cec_receive_notify(adap, msg, is_reply);
1101 EXPORT_SYMBOL_GPL(cec_received_msg);
1103 /* Logical Address Handling */
1106 * Attempt to claim a specific logical address.
1108 * This function is called with adap->lock held.
1110 static int cec_config_log_addr(struct cec_adapter *adap,
1112 unsigned int log_addr)
1114 struct cec_log_addrs *las = &adap->log_addrs;
1115 struct cec_msg msg = { };
1118 if (cec_has_log_addr(adap, log_addr))
1121 /* Send poll message */
1123 msg.msg[0] = (log_addr << 4) | log_addr;
1124 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1127 * While trying to poll the physical address was reset
1128 * and the adapter was unconfigured, so bail out.
1130 if (!adap->is_configuring)
1136 if (msg.tx_status & CEC_TX_STATUS_OK)
1140 * Message not acknowledged, so this logical
1141 * address is free to use.
1143 err = adap->ops->adap_log_addr(adap, log_addr);
1147 las->log_addr[idx] = log_addr;
1148 las->log_addr_mask |= 1 << log_addr;
1149 adap->phys_addrs[log_addr] = adap->phys_addr;
1154 * Unconfigure the adapter: clear all logical addresses and send
1155 * the state changed event.
1157 * This function is called with adap->lock held.
1159 static void cec_adap_unconfigure(struct cec_adapter *adap)
1161 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1162 adap->log_addrs.log_addr_mask = 0;
1163 adap->is_configuring = false;
1164 adap->is_configured = false;
1165 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1167 wake_up_interruptible(&adap->kthread_waitq);
1168 cec_post_state_event(adap);
1172 * Attempt to claim the required logical addresses.
1174 static int cec_config_thread_func(void *arg)
1176 /* The various LAs for each type of device */
1177 static const u8 tv_log_addrs[] = {
1178 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1179 CEC_LOG_ADDR_INVALID
1181 static const u8 record_log_addrs[] = {
1182 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1183 CEC_LOG_ADDR_RECORD_3,
1184 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1185 CEC_LOG_ADDR_INVALID
1187 static const u8 tuner_log_addrs[] = {
1188 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1189 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1190 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1191 CEC_LOG_ADDR_INVALID
1193 static const u8 playback_log_addrs[] = {
1194 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1195 CEC_LOG_ADDR_PLAYBACK_3,
1196 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1197 CEC_LOG_ADDR_INVALID
1199 static const u8 audiosystem_log_addrs[] = {
1200 CEC_LOG_ADDR_AUDIOSYSTEM,
1201 CEC_LOG_ADDR_INVALID
1203 static const u8 specific_use_log_addrs[] = {
1204 CEC_LOG_ADDR_SPECIFIC,
1205 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1206 CEC_LOG_ADDR_INVALID
1208 static const u8 *type2addrs[6] = {
1209 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1210 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1211 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1212 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1213 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1214 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1216 static const u16 type2mask[] = {
1217 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1218 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1219 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1220 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1221 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1222 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1224 struct cec_adapter *adap = arg;
1225 struct cec_log_addrs *las = &adap->log_addrs;
1229 mutex_lock(&adap->lock);
1230 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1231 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1232 las->log_addr_mask = 0;
1234 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1237 for (i = 0; i < las->num_log_addrs; i++) {
1238 unsigned int type = las->log_addr_type[i];
1243 * The TV functionality can only map to physical address 0.
1244 * For any other address, try the Specific functionality
1245 * instead as per the spec.
1247 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1248 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1250 la_list = type2addrs[type];
1251 last_la = las->log_addr[i];
1252 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1253 if (last_la == CEC_LOG_ADDR_INVALID ||
1254 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1255 !((1 << last_la) & type2mask[type]))
1256 last_la = la_list[0];
1258 err = cec_config_log_addr(adap, i, last_la);
1259 if (err > 0) /* Reused last LA */
1265 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1266 /* Tried this one already, skip it */
1267 if (la_list[j] == last_la)
1269 /* The backup addresses are CEC 2.0 specific */
1270 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1271 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1272 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1275 err = cec_config_log_addr(adap, i, la_list[j]);
1276 if (err == 0) /* LA is in use */
1280 /* Done, claimed an LA */
1284 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1285 dprintk(1, "could not claim LA %d\n", i);
1288 if (adap->log_addrs.log_addr_mask == 0 &&
1289 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1293 if (adap->log_addrs.log_addr_mask == 0) {
1294 /* Fall back to unregistered */
1295 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1296 las->log_addr_mask = 1 << las->log_addr[0];
1297 for (i = 1; i < las->num_log_addrs; i++)
1298 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1300 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1301 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1302 adap->is_configured = true;
1303 adap->is_configuring = false;
1304 cec_post_state_event(adap);
1307 * Now post the Report Features and Report Physical Address broadcast
1308 * messages. Note that these are non-blocking transmits, meaning that
1309 * they are just queued up and once adap->lock is unlocked the main
1310 * thread will kick in and start transmitting these.
1312 * If after this function is done (but before one or more of these
1313 * messages are actually transmitted) the CEC adapter is unconfigured,
1314 * then any remaining messages will be dropped by the main thread.
1316 for (i = 0; i < las->num_log_addrs; i++) {
1317 struct cec_msg msg = {};
1319 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1320 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1323 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1325 /* Report Features must come first according to CEC 2.0 */
1326 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1327 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1328 cec_fill_msg_report_features(adap, &msg, i);
1329 cec_transmit_msg_fh(adap, &msg, NULL, false);
1332 /* Report Physical Address */
1333 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1334 las->primary_device_type[i]);
1335 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1337 cec_phys_addr_exp(adap->phys_addr));
1338 cec_transmit_msg_fh(adap, &msg, NULL, false);
1340 adap->kthread_config = NULL;
1341 complete(&adap->config_completion);
1342 mutex_unlock(&adap->lock);
1346 for (i = 0; i < las->num_log_addrs; i++)
1347 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1348 cec_adap_unconfigure(adap);
1349 adap->kthread_config = NULL;
1350 mutex_unlock(&adap->lock);
1351 complete(&adap->config_completion);
1356 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1357 * logical addresses.
1359 * This function is called with adap->lock held.
1361 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1363 if (WARN_ON(adap->is_configuring || adap->is_configured))
1366 init_completion(&adap->config_completion);
1368 /* Ready to kick off the thread */
1369 adap->is_configuring = true;
1370 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1371 "ceccfg-%s", adap->name);
1372 if (IS_ERR(adap->kthread_config)) {
1373 adap->kthread_config = NULL;
1375 mutex_unlock(&adap->lock);
1376 wait_for_completion(&adap->config_completion);
1377 mutex_lock(&adap->lock);
1381 /* Set a new physical address and send an event notifying userspace of this.
1383 * This function is called with adap->lock held.
1385 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1387 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
1390 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1391 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1392 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1393 cec_post_state_event(adap);
1394 cec_adap_unconfigure(adap);
1395 /* Disabling monitor all mode should always succeed */
1396 if (adap->monitor_all_cnt)
1397 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1398 mutex_lock(&adap->devnode.lock);
1399 if (list_empty(&adap->devnode.fhs))
1400 WARN_ON(adap->ops->adap_enable(adap, false));
1401 mutex_unlock(&adap->devnode.lock);
1402 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1406 mutex_lock(&adap->devnode.lock);
1407 if (list_empty(&adap->devnode.fhs) &&
1408 adap->ops->adap_enable(adap, true)) {
1409 mutex_unlock(&adap->devnode.lock);
1413 if (adap->monitor_all_cnt &&
1414 call_op(adap, adap_monitor_all_enable, true)) {
1415 if (list_empty(&adap->devnode.fhs))
1416 WARN_ON(adap->ops->adap_enable(adap, false));
1417 mutex_unlock(&adap->devnode.lock);
1420 mutex_unlock(&adap->devnode.lock);
1422 adap->phys_addr = phys_addr;
1423 cec_post_state_event(adap);
1424 if (adap->log_addrs.num_log_addrs)
1425 cec_claim_log_addrs(adap, block);
1428 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1430 if (IS_ERR_OR_NULL(adap))
1433 mutex_lock(&adap->lock);
1434 __cec_s_phys_addr(adap, phys_addr, block);
1435 mutex_unlock(&adap->lock);
1437 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1439 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1440 const struct edid *edid)
1442 u16 pa = CEC_PHYS_ADDR_INVALID;
1444 if (edid && edid->extensions)
1445 pa = cec_get_edid_phys_addr((const u8 *)edid,
1446 EDID_LENGTH * (edid->extensions + 1), NULL);
1447 cec_s_phys_addr(adap, pa, false);
1449 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1452 * Called from either the ioctl or a driver to set the logical addresses.
1454 * This function is called with adap->lock held.
1456 int __cec_s_log_addrs(struct cec_adapter *adap,
1457 struct cec_log_addrs *log_addrs, bool block)
1462 if (adap->devnode.unregistered)
1465 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1466 adap->log_addrs.num_log_addrs = 0;
1467 cec_adap_unconfigure(adap);
1471 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1473 * Sanitize log_addrs fields if a CDC-Only device is
1476 log_addrs->num_log_addrs = 1;
1477 log_addrs->osd_name[0] = '\0';
1478 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1479 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1481 * This is just an internal convention since a CDC-Only device
1482 * doesn't have to be a switch. But switches already use
1483 * unregistered, so it makes some kind of sense to pick this
1484 * as the primary device. Since a CDC-Only device never sends
1485 * any 'normal' CEC messages this primary device type is never
1486 * sent over the CEC bus.
1488 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1489 log_addrs->all_device_types[0] = 0;
1490 log_addrs->features[0][0] = 0;
1491 log_addrs->features[0][1] = 0;
1494 /* Ensure the osd name is 0-terminated */
1495 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1498 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1499 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1504 * Vendor ID is a 24 bit number, so check if the value is
1505 * within the correct range.
1507 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1508 (log_addrs->vendor_id & 0xff000000) != 0) {
1509 dprintk(1, "invalid vendor ID\n");
1513 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1514 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1515 dprintk(1, "invalid CEC version\n");
1519 if (log_addrs->num_log_addrs > 1)
1520 for (i = 0; i < log_addrs->num_log_addrs; i++)
1521 if (log_addrs->log_addr_type[i] ==
1522 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1523 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1527 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1528 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1529 u8 *features = log_addrs->features[i];
1530 bool op_is_dev_features = false;
1533 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1534 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1535 dprintk(1, "duplicate logical address type\n");
1538 type_mask |= 1 << log_addrs->log_addr_type[i];
1539 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1540 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1541 /* Record already contains the playback functionality */
1542 dprintk(1, "invalid record + playback combination\n");
1545 if (log_addrs->primary_device_type[i] >
1546 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1547 dprintk(1, "unknown primary device type\n");
1550 if (log_addrs->primary_device_type[i] == 2) {
1551 dprintk(1, "invalid primary device type\n");
1554 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1555 dprintk(1, "unknown logical address type\n");
1558 for (j = 0; j < feature_sz; j++) {
1559 if ((features[j] & 0x80) == 0) {
1560 if (op_is_dev_features)
1562 op_is_dev_features = true;
1565 if (!op_is_dev_features || j == feature_sz) {
1566 dprintk(1, "malformed features\n");
1569 /* Zero unused part of the feature array */
1570 memset(features + j + 1, 0, feature_sz - j - 1);
1573 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1574 if (log_addrs->num_log_addrs > 2) {
1575 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1578 if (log_addrs->num_log_addrs == 2) {
1579 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1580 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1581 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1584 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1585 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1586 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1592 /* Zero unused LAs */
1593 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1594 log_addrs->primary_device_type[i] = 0;
1595 log_addrs->log_addr_type[i] = 0;
1596 log_addrs->all_device_types[i] = 0;
1597 memset(log_addrs->features[i], 0,
1598 sizeof(log_addrs->features[i]));
1601 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1602 adap->log_addrs = *log_addrs;
1603 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1604 cec_claim_log_addrs(adap, block);
1608 int cec_s_log_addrs(struct cec_adapter *adap,
1609 struct cec_log_addrs *log_addrs, bool block)
1613 mutex_lock(&adap->lock);
1614 err = __cec_s_log_addrs(adap, log_addrs, block);
1615 mutex_unlock(&adap->lock);
1618 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1620 /* High-level core CEC message handling */
1622 /* Fill in the Report Features message */
1623 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1624 struct cec_msg *msg,
1625 unsigned int la_idx)
1627 const struct cec_log_addrs *las = &adap->log_addrs;
1628 const u8 *features = las->features[la_idx];
1629 bool op_is_dev_features = false;
1632 /* Report Features */
1633 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1635 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1636 msg->msg[2] = adap->log_addrs.cec_version;
1637 msg->msg[3] = las->all_device_types[la_idx];
1639 /* Write RC Profiles first, then Device Features */
1640 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1641 msg->msg[msg->len++] = features[idx];
1642 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1643 if (op_is_dev_features)
1645 op_is_dev_features = true;
1650 /* Transmit the Feature Abort message */
1651 static int cec_feature_abort_reason(struct cec_adapter *adap,
1652 struct cec_msg *msg, u8 reason)
1654 struct cec_msg tx_msg = { };
1657 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1660 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1662 /* Don't Feature Abort messages from 'Unregistered' */
1663 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1665 cec_msg_set_reply_to(&tx_msg, msg);
1666 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1667 return cec_transmit_msg(adap, &tx_msg, false);
1670 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1672 return cec_feature_abort_reason(adap, msg,
1673 CEC_OP_ABORT_UNRECOGNIZED_OP);
1676 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1678 return cec_feature_abort_reason(adap, msg,
1679 CEC_OP_ABORT_REFUSED);
1683 * Called when a CEC message is received. This function will do any
1684 * necessary core processing. The is_reply bool is true if this message
1685 * is a reply to an earlier transmit.
1687 * The message is either a broadcast message or a valid directed message.
1689 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1692 bool is_broadcast = cec_msg_is_broadcast(msg);
1693 u8 dest_laddr = cec_msg_destination(msg);
1694 u8 init_laddr = cec_msg_initiator(msg);
1695 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1696 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1697 bool from_unregistered = init_laddr == 0xf;
1698 struct cec_msg tx_cec_msg = { };
1700 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1702 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1703 if (cec_is_cdc_only(&adap->log_addrs) &&
1704 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1707 if (adap->ops->received) {
1708 /* Allow drivers to process the message first */
1709 if (adap->ops->received(adap, msg) != -ENOMSG)
1714 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1715 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1716 * handled by the CEC core, even if the passthrough mode is on.
1717 * The others are just ignored if passthrough mode is on.
1719 switch (msg->msg[1]) {
1720 case CEC_MSG_GET_CEC_VERSION:
1721 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1723 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1724 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1725 case CEC_MSG_GIVE_OSD_NAME:
1726 case CEC_MSG_GIVE_FEATURES:
1728 * Skip processing these messages if the passthrough mode
1731 if (adap->passthrough)
1732 goto skip_processing;
1733 /* Ignore if addressing is wrong */
1734 if (is_broadcast || from_unregistered)
1738 case CEC_MSG_USER_CONTROL_PRESSED:
1739 case CEC_MSG_USER_CONTROL_RELEASED:
1740 /* Wrong addressing mode: don't process */
1741 if (is_broadcast || from_unregistered)
1742 goto skip_processing;
1745 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1747 * This message is always processed, regardless of the
1748 * passthrough setting.
1750 * Exception: don't process if wrong addressing mode.
1753 goto skip_processing;
1760 cec_msg_set_reply_to(&tx_cec_msg, msg);
1762 switch (msg->msg[1]) {
1763 /* The following messages are processed but still passed through */
1764 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1765 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1767 if (!from_unregistered)
1768 adap->phys_addrs[init_laddr] = pa;
1769 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
1770 cec_phys_addr_exp(pa), init_laddr);
1774 case CEC_MSG_USER_CONTROL_PRESSED:
1775 if (!(adap->capabilities & CEC_CAP_RC) ||
1776 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1779 #ifdef CONFIG_MEDIA_CEC_RC
1780 switch (msg->msg[2]) {
1782 * Play function, this message can have variable length
1783 * depending on the specific play function that is used.
1787 rc_keydown(adap->rc, RC_TYPE_CEC,
1790 rc_keydown(adap->rc, RC_TYPE_CEC,
1791 msg->msg[2] << 8 | msg->msg[3], 0);
1794 * Other function messages that are not handled.
1795 * Currently the RC framework does not allow to supply an
1796 * additional parameter to a keypress. These "keys" contain
1797 * other information such as channel number, an input number
1799 * For the time being these messages are not processed by the
1800 * framework and are simply forwarded to the user space.
1802 case 0x56: case 0x57:
1803 case 0x67: case 0x68: case 0x69: case 0x6a:
1806 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1812 case CEC_MSG_USER_CONTROL_RELEASED:
1813 if (!(adap->capabilities & CEC_CAP_RC) ||
1814 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1816 #ifdef CONFIG_MEDIA_CEC_RC
1822 * The remaining messages are only processed if the passthrough mode
1825 case CEC_MSG_GET_CEC_VERSION:
1826 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1827 return cec_transmit_msg(adap, &tx_cec_msg, false);
1829 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1830 /* Do nothing for CEC switches using addr 15 */
1831 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1833 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1834 return cec_transmit_msg(adap, &tx_cec_msg, false);
1836 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1837 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1838 return cec_feature_abort(adap, msg);
1839 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1840 return cec_transmit_msg(adap, &tx_cec_msg, false);
1843 /* Do nothing for CEC switches */
1844 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1846 return cec_feature_refused(adap, msg);
1848 case CEC_MSG_GIVE_OSD_NAME: {
1849 if (adap->log_addrs.osd_name[0] == 0)
1850 return cec_feature_abort(adap, msg);
1851 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1852 return cec_transmit_msg(adap, &tx_cec_msg, false);
1855 case CEC_MSG_GIVE_FEATURES:
1856 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1857 return cec_feature_abort(adap, msg);
1858 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1859 return cec_transmit_msg(adap, &tx_cec_msg, false);
1863 * Unprocessed messages are aborted if userspace isn't doing
1864 * any processing either.
1866 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
1867 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1868 return cec_feature_abort(adap, msg);
1873 /* If this was a reply, then we're done, unless otherwise specified */
1874 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
1878 * Send to the exclusive follower if there is one, otherwise send
1881 if (adap->cec_follower)
1882 cec_queue_msg_fh(adap->cec_follower, msg);
1884 cec_queue_msg_followers(adap, msg);
1889 * Helper functions to keep track of the 'monitor all' use count.
1891 * These functions are called with adap->lock held.
1893 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1897 if (adap->monitor_all_cnt == 0)
1898 ret = call_op(adap, adap_monitor_all_enable, 1);
1900 adap->monitor_all_cnt++;
1904 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1906 adap->monitor_all_cnt--;
1907 if (adap->monitor_all_cnt == 0)
1908 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1911 #ifdef CONFIG_DEBUG_FS
1913 * Log the current state of the CEC adapter.
1914 * Very useful for debugging.
1916 int cec_adap_status(struct seq_file *file, void *priv)
1918 struct cec_adapter *adap = dev_get_drvdata(file->private);
1919 struct cec_data *data;
1921 mutex_lock(&adap->lock);
1922 seq_printf(file, "configured: %d\n", adap->is_configured);
1923 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1924 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1925 cec_phys_addr_exp(adap->phys_addr));
1926 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1927 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1928 if (adap->cec_follower)
1929 seq_printf(file, "has CEC follower%s\n",
1930 adap->passthrough ? " (in passthrough mode)" : "");
1931 if (adap->cec_initiator)
1932 seq_puts(file, "has CEC initiator\n");
1933 if (adap->monitor_all_cnt)
1934 seq_printf(file, "file handles in Monitor All mode: %u\n",
1935 adap->monitor_all_cnt);
1936 data = adap->transmitting;
1938 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1939 data->msg.len, data->msg.msg, data->msg.reply,
1941 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
1942 list_for_each_entry(data, &adap->transmit_queue, list) {
1943 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1944 data->msg.len, data->msg.msg, data->msg.reply,
1947 list_for_each_entry(data, &adap->wait_queue, list) {
1948 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1949 data->msg.len, data->msg.msg, data->msg.reply,
1953 call_void_op(adap, adap_status, file);
1954 mutex_unlock(&adap->lock);