perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / cec / cec-adap.c
blob030b2602faf0c322ad0483cd5f0f29e565b06497
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/kmod.h>
13 #include <linux/ktime.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
19 #include <drm/drm_edid.h>
21 #include "cec-priv.h"
23 static void cec_fill_msg_report_features(struct cec_adapter *adap,
24 struct cec_msg *msg,
25 unsigned int la_idx);
28 * 400 ms is the time it takes for one 16 byte message to be
29 * transferred and 5 is the maximum number of retries. Add
30 * another 100 ms as a margin. So if the transmit doesn't
31 * finish before that time something is really wrong and we
32 * have to time out.
34 * This is a sign that something it really wrong and a warning
35 * will be issued.
37 #define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
39 #define call_op(adap, op, arg...) \
40 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
42 #define call_void_op(adap, op, arg...) \
43 do { \
44 if (adap->ops->op) \
45 adap->ops->op(adap, ## arg); \
46 } while (0)
48 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
50 int i;
52 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
53 if (adap->log_addrs.log_addr[i] == log_addr)
54 return i;
55 return -1;
58 static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
60 int i = cec_log_addr2idx(adap, log_addr);
62 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
66 * Queue a new event for this filehandle. If ts == 0, then set it
67 * to the current time.
69 * We keep a queue of at most max_event events where max_event differs
70 * per event. If the queue becomes full, then drop the oldest event and
71 * keep track of how many events we've dropped.
73 void cec_queue_event_fh(struct cec_fh *fh,
74 const struct cec_event *new_ev, u64 ts)
76 static const u16 max_events[CEC_NUM_EVENTS] = {
77 1, 1, 800, 800, 8, 8, 8, 8
79 struct cec_event_entry *entry;
80 unsigned int ev_idx = new_ev->event - 1;
82 if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
83 return;
85 if (ts == 0)
86 ts = ktime_get_ns();
88 mutex_lock(&fh->lock);
89 if (ev_idx < CEC_NUM_CORE_EVENTS)
90 entry = &fh->core_events[ev_idx];
91 else
92 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
93 if (entry) {
94 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
95 fh->queued_events[ev_idx]) {
96 entry->ev.lost_msgs.lost_msgs +=
97 new_ev->lost_msgs.lost_msgs;
98 goto unlock;
100 entry->ev = *new_ev;
101 entry->ev.ts = ts;
103 if (fh->queued_events[ev_idx] < max_events[ev_idx]) {
104 /* Add new msg at the end of the queue */
105 list_add_tail(&entry->list, &fh->events[ev_idx]);
106 fh->queued_events[ev_idx]++;
107 fh->total_queued_events++;
108 goto unlock;
111 if (ev_idx >= CEC_NUM_CORE_EVENTS) {
112 list_add_tail(&entry->list, &fh->events[ev_idx]);
113 /* drop the oldest event */
114 entry = list_first_entry(&fh->events[ev_idx],
115 struct cec_event_entry, list);
116 list_del(&entry->list);
117 kfree(entry);
120 /* Mark that events were lost */
121 entry = list_first_entry_or_null(&fh->events[ev_idx],
122 struct cec_event_entry, list);
123 if (entry)
124 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
126 unlock:
127 mutex_unlock(&fh->lock);
128 wake_up_interruptible(&fh->wait);
131 /* Queue a new event for all open filehandles. */
132 static void cec_queue_event(struct cec_adapter *adap,
133 const struct cec_event *ev)
135 u64 ts = ktime_get_ns();
136 struct cec_fh *fh;
138 mutex_lock(&adap->devnode.lock);
139 list_for_each_entry(fh, &adap->devnode.fhs, list)
140 cec_queue_event_fh(fh, ev, ts);
141 mutex_unlock(&adap->devnode.lock);
144 /* Notify userspace that the CEC pin changed state at the given time. */
145 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
146 bool dropped_events, ktime_t ts)
148 struct cec_event ev = {
149 .event = is_high ? CEC_EVENT_PIN_CEC_HIGH :
150 CEC_EVENT_PIN_CEC_LOW,
151 .flags = dropped_events ? CEC_EVENT_FL_DROPPED_EVENTS : 0,
153 struct cec_fh *fh;
155 mutex_lock(&adap->devnode.lock);
156 list_for_each_entry(fh, &adap->devnode.fhs, list)
157 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
158 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
159 mutex_unlock(&adap->devnode.lock);
161 EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event);
163 /* Notify userspace that the HPD pin changed state at the given time. */
164 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
166 struct cec_event ev = {
167 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH :
168 CEC_EVENT_PIN_HPD_LOW,
170 struct cec_fh *fh;
172 mutex_lock(&adap->devnode.lock);
173 list_for_each_entry(fh, &adap->devnode.fhs, list)
174 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
175 mutex_unlock(&adap->devnode.lock);
177 EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event);
179 /* Notify userspace that the 5V pin changed state at the given time. */
180 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
182 struct cec_event ev = {
183 .event = is_high ? CEC_EVENT_PIN_5V_HIGH :
184 CEC_EVENT_PIN_5V_LOW,
186 struct cec_fh *fh;
188 mutex_lock(&adap->devnode.lock);
189 list_for_each_entry(fh, &adap->devnode.fhs, list)
190 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
191 mutex_unlock(&adap->devnode.lock);
193 EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event);
196 * Queue a new message for this filehandle.
198 * We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the
199 * queue becomes full, then drop the oldest message and keep track
200 * of how many messages we've dropped.
202 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
204 static const struct cec_event ev_lost_msgs = {
205 .event = CEC_EVENT_LOST_MSGS,
206 .flags = 0,
208 .lost_msgs = { 1 },
211 struct cec_msg_entry *entry;
213 mutex_lock(&fh->lock);
214 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
215 if (entry) {
216 entry->msg = *msg;
217 /* Add new msg at the end of the queue */
218 list_add_tail(&entry->list, &fh->msgs);
220 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
221 /* All is fine if there is enough room */
222 fh->queued_msgs++;
223 mutex_unlock(&fh->lock);
224 wake_up_interruptible(&fh->wait);
225 return;
229 * if the message queue is full, then drop the oldest one and
230 * send a lost message event.
232 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
233 list_del(&entry->list);
234 kfree(entry);
236 mutex_unlock(&fh->lock);
239 * We lost a message, either because kmalloc failed or the queue
240 * was full.
242 cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
246 * Queue the message for those filehandles that are in monitor mode.
247 * If valid_la is true (this message is for us or was sent by us),
248 * then pass it on to any monitoring filehandle. If this message
249 * isn't for us or from us, then only give it to filehandles that
250 * are in MONITOR_ALL mode.
252 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
253 * set and the CEC adapter was placed in 'monitor all' mode.
255 static void cec_queue_msg_monitor(struct cec_adapter *adap,
256 const struct cec_msg *msg,
257 bool valid_la)
259 struct cec_fh *fh;
260 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
261 CEC_MODE_MONITOR_ALL;
263 mutex_lock(&adap->devnode.lock);
264 list_for_each_entry(fh, &adap->devnode.fhs, list) {
265 if (fh->mode_follower >= monitor_mode)
266 cec_queue_msg_fh(fh, msg);
268 mutex_unlock(&adap->devnode.lock);
272 * Queue the message for follower filehandles.
274 static void cec_queue_msg_followers(struct cec_adapter *adap,
275 const struct cec_msg *msg)
277 struct cec_fh *fh;
279 mutex_lock(&adap->devnode.lock);
280 list_for_each_entry(fh, &adap->devnode.fhs, list) {
281 if (fh->mode_follower == CEC_MODE_FOLLOWER)
282 cec_queue_msg_fh(fh, msg);
284 mutex_unlock(&adap->devnode.lock);
287 /* Notify userspace of an adapter state change. */
288 static void cec_post_state_event(struct cec_adapter *adap)
290 struct cec_event ev = {
291 .event = CEC_EVENT_STATE_CHANGE,
294 ev.state_change.phys_addr = adap->phys_addr;
295 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
296 cec_queue_event(adap, &ev);
300 * A CEC transmit (and a possible wait for reply) completed.
301 * If this was in blocking mode, then complete it, otherwise
302 * queue the message for userspace to dequeue later.
304 * This function is called with adap->lock held.
306 static void cec_data_completed(struct cec_data *data)
309 * Delete this transmit from the filehandle's xfer_list since
310 * we're done with it.
312 * Note that if the filehandle is closed before this transmit
313 * finished, then the release() function will set data->fh to NULL.
314 * Without that we would be referring to a closed filehandle.
316 if (data->fh)
317 list_del(&data->xfer_list);
319 if (data->blocking) {
321 * Someone is blocking so mark the message as completed
322 * and call complete.
324 data->completed = true;
325 complete(&data->c);
326 } else {
328 * No blocking, so just queue the message if needed and
329 * free the memory.
331 if (data->fh)
332 cec_queue_msg_fh(data->fh, &data->msg);
333 kfree(data);
338 * A pending CEC transmit needs to be cancelled, either because the CEC
339 * adapter is disabled or the transmit takes an impossibly long time to
340 * finish.
342 * This function is called with adap->lock held.
344 static void cec_data_cancel(struct cec_data *data)
347 * It's either the current transmit, or it is a pending
348 * transmit. Take the appropriate action to clear it.
350 if (data->adap->transmitting == data) {
351 data->adap->transmitting = NULL;
352 } else {
353 list_del_init(&data->list);
354 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
355 data->adap->transmit_queue_sz--;
358 if (data->msg.tx_status & CEC_TX_STATUS_OK) {
359 /* Mark the canceled RX as a timeout */
360 data->msg.rx_ts = ktime_get_ns();
361 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
362 } else {
363 /* Mark the canceled TX as an error */
364 data->msg.tx_ts = ktime_get_ns();
365 data->msg.tx_status |= CEC_TX_STATUS_ERROR |
366 CEC_TX_STATUS_MAX_RETRIES;
367 data->msg.tx_error_cnt++;
368 data->attempts = 0;
371 /* Queue transmitted message for monitoring purposes */
372 cec_queue_msg_monitor(data->adap, &data->msg, 1);
374 cec_data_completed(data);
378 * Flush all pending transmits and cancel any pending timeout work.
380 * This function is called with adap->lock held.
382 static void cec_flush(struct cec_adapter *adap)
384 struct cec_data *data, *n;
387 * If the adapter is disabled, or we're asked to stop,
388 * then cancel any pending transmits.
390 while (!list_empty(&adap->transmit_queue)) {
391 data = list_first_entry(&adap->transmit_queue,
392 struct cec_data, list);
393 cec_data_cancel(data);
395 if (adap->transmitting)
396 cec_data_cancel(adap->transmitting);
398 /* Cancel the pending timeout work. */
399 list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
400 if (cancel_delayed_work(&data->work))
401 cec_data_cancel(data);
403 * If cancel_delayed_work returned false, then
404 * the cec_wait_timeout function is running,
405 * which will call cec_data_completed. So no
406 * need to do anything special in that case.
412 * Main CEC state machine
414 * Wait until the thread should be stopped, or we are not transmitting and
415 * a new transmit message is queued up, in which case we start transmitting
416 * that message. When the adapter finished transmitting the message it will
417 * call cec_transmit_done().
419 * If the adapter is disabled, then remove all queued messages instead.
421 * If the current transmit times out, then cancel that transmit.
423 int cec_thread_func(void *_adap)
425 struct cec_adapter *adap = _adap;
427 for (;;) {
428 unsigned int signal_free_time;
429 struct cec_data *data;
430 bool timeout = false;
431 u8 attempts;
433 if (adap->transmitting) {
434 int err;
437 * We are transmitting a message, so add a timeout
438 * to prevent the state machine to get stuck waiting
439 * for this message to finalize and add a check to
440 * see if the adapter is disabled in which case the
441 * transmit should be canceled.
443 err = wait_event_interruptible_timeout(adap->kthread_waitq,
444 (adap->needs_hpd &&
445 (!adap->is_configured && !adap->is_configuring)) ||
446 kthread_should_stop() ||
447 (!adap->transmitting &&
448 !list_empty(&adap->transmit_queue)),
449 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
450 timeout = err == 0;
451 } else {
452 /* Otherwise we just wait for something to happen. */
453 wait_event_interruptible(adap->kthread_waitq,
454 kthread_should_stop() ||
455 (!adap->transmitting &&
456 !list_empty(&adap->transmit_queue)));
459 mutex_lock(&adap->lock);
461 if ((adap->needs_hpd &&
462 (!adap->is_configured && !adap->is_configuring)) ||
463 kthread_should_stop()) {
464 cec_flush(adap);
465 goto unlock;
468 if (adap->transmitting && timeout) {
470 * If we timeout, then log that. Normally this does
471 * not happen and it is an indication of a faulty CEC
472 * adapter driver, or the CEC bus is in some weird
473 * state. On rare occasions it can happen if there is
474 * so much traffic on the bus that the adapter was
475 * unable to transmit for CEC_XFER_TIMEOUT_MS (2.1s).
477 dprintk(1, "%s: message %*ph timed out\n", __func__,
478 adap->transmitting->msg.len,
479 adap->transmitting->msg.msg);
480 adap->tx_timeouts++;
481 /* Just give up on this. */
482 cec_data_cancel(adap->transmitting);
483 goto unlock;
487 * If we are still transmitting, or there is nothing new to
488 * transmit, then just continue waiting.
490 if (adap->transmitting || list_empty(&adap->transmit_queue))
491 goto unlock;
493 /* Get a new message to transmit */
494 data = list_first_entry(&adap->transmit_queue,
495 struct cec_data, list);
496 list_del_init(&data->list);
497 adap->transmit_queue_sz--;
499 /* Make this the current transmitting message */
500 adap->transmitting = data;
503 * Suggested number of attempts as per the CEC 2.0 spec:
504 * 4 attempts is the default, except for 'secondary poll
505 * messages', i.e. poll messages not sent during the adapter
506 * configuration phase when it allocates logical addresses.
508 if (data->msg.len == 1 && adap->is_configured)
509 attempts = 2;
510 else
511 attempts = 4;
513 /* Set the suggested signal free time */
514 if (data->attempts) {
515 /* should be >= 3 data bit periods for a retry */
516 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
517 } else if (data->new_initiator) {
518 /* should be >= 5 data bit periods for new initiator */
519 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
520 } else {
522 * should be >= 7 data bit periods for sending another
523 * frame immediately after another.
525 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
527 if (data->attempts == 0)
528 data->attempts = attempts;
530 /* Tell the adapter to transmit, cancel on error */
531 if (adap->ops->adap_transmit(adap, data->attempts,
532 signal_free_time, &data->msg))
533 cec_data_cancel(data);
535 unlock:
536 mutex_unlock(&adap->lock);
538 if (kthread_should_stop())
539 break;
541 return 0;
545 * Called by the CEC adapter if a transmit finished.
547 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
548 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
549 u8 error_cnt, ktime_t ts)
551 struct cec_data *data;
552 struct cec_msg *msg;
553 unsigned int attempts_made = arb_lost_cnt + nack_cnt +
554 low_drive_cnt + error_cnt;
556 dprintk(2, "%s: status 0x%02x\n", __func__, status);
557 if (attempts_made < 1)
558 attempts_made = 1;
560 mutex_lock(&adap->lock);
561 data = adap->transmitting;
562 if (!data) {
564 * This can happen if a transmit was issued and the cable is
565 * unplugged while the transmit is ongoing. Ignore this
566 * transmit in that case.
568 dprintk(1, "%s was called without an ongoing transmit!\n",
569 __func__);
570 goto unlock;
573 msg = &data->msg;
575 /* Drivers must fill in the status! */
576 WARN_ON(status == 0);
577 msg->tx_ts = ktime_to_ns(ts);
578 msg->tx_status |= status;
579 msg->tx_arb_lost_cnt += arb_lost_cnt;
580 msg->tx_nack_cnt += nack_cnt;
581 msg->tx_low_drive_cnt += low_drive_cnt;
582 msg->tx_error_cnt += error_cnt;
584 /* Mark that we're done with this transmit */
585 adap->transmitting = NULL;
588 * If there are still retry attempts left and there was an error and
589 * the hardware didn't signal that it retried itself (by setting
590 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
592 if (data->attempts > attempts_made &&
593 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
594 /* Retry this message */
595 data->attempts -= attempts_made;
596 if (msg->timeout)
597 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
598 msg->len, msg->msg, data->attempts, msg->reply);
599 else
600 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
601 msg->len, msg->msg, data->attempts);
602 /* Add the message in front of the transmit queue */
603 list_add(&data->list, &adap->transmit_queue);
604 adap->transmit_queue_sz++;
605 goto wake_thread;
608 data->attempts = 0;
610 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
611 if (!(status & CEC_TX_STATUS_OK))
612 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
614 /* Queue transmitted message for monitoring purposes */
615 cec_queue_msg_monitor(adap, msg, 1);
617 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
618 msg->timeout) {
620 * Queue the message into the wait queue if we want to wait
621 * for a reply.
623 list_add_tail(&data->list, &adap->wait_queue);
624 schedule_delayed_work(&data->work,
625 msecs_to_jiffies(msg->timeout));
626 } else {
627 /* Otherwise we're done */
628 cec_data_completed(data);
631 wake_thread:
633 * Wake up the main thread to see if another message is ready
634 * for transmitting or to retry the current message.
636 wake_up_interruptible(&adap->kthread_waitq);
637 unlock:
638 mutex_unlock(&adap->lock);
640 EXPORT_SYMBOL_GPL(cec_transmit_done_ts);
642 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
643 u8 status, ktime_t ts)
645 switch (status & ~CEC_TX_STATUS_MAX_RETRIES) {
646 case CEC_TX_STATUS_OK:
647 cec_transmit_done_ts(adap, status, 0, 0, 0, 0, ts);
648 return;
649 case CEC_TX_STATUS_ARB_LOST:
650 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts);
651 return;
652 case CEC_TX_STATUS_NACK:
653 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts);
654 return;
655 case CEC_TX_STATUS_LOW_DRIVE:
656 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts);
657 return;
658 case CEC_TX_STATUS_ERROR:
659 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts);
660 return;
661 default:
662 /* Should never happen */
663 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
664 return;
667 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts);
670 * Called when waiting for a reply times out.
672 static void cec_wait_timeout(struct work_struct *work)
674 struct cec_data *data = container_of(work, struct cec_data, work.work);
675 struct cec_adapter *adap = data->adap;
677 mutex_lock(&adap->lock);
679 * Sanity check in case the timeout and the arrival of the message
680 * happened at the same time.
682 if (list_empty(&data->list))
683 goto unlock;
685 /* Mark the message as timed out */
686 list_del_init(&data->list);
687 data->msg.rx_ts = ktime_get_ns();
688 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
689 cec_data_completed(data);
690 unlock:
691 mutex_unlock(&adap->lock);
695 * Transmit a message. The fh argument may be NULL if the transmit is not
696 * associated with a specific filehandle.
698 * This function is called with adap->lock held.
700 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
701 struct cec_fh *fh, bool block)
703 struct cec_data *data;
704 u8 last_initiator = 0xff;
705 unsigned int timeout;
706 int res = 0;
708 msg->rx_ts = 0;
709 msg->tx_ts = 0;
710 msg->rx_status = 0;
711 msg->tx_status = 0;
712 msg->tx_arb_lost_cnt = 0;
713 msg->tx_nack_cnt = 0;
714 msg->tx_low_drive_cnt = 0;
715 msg->tx_error_cnt = 0;
716 msg->sequence = 0;
718 if (msg->reply && msg->timeout == 0) {
719 /* Make sure the timeout isn't 0. */
720 msg->timeout = 1000;
722 if (msg->timeout)
723 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
724 else
725 msg->flags = 0;
727 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
728 msg->msg[2] = adap->phys_addr >> 8;
729 msg->msg[3] = adap->phys_addr & 0xff;
732 /* Sanity checks */
733 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
734 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
735 return -EINVAL;
738 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
740 if (msg->timeout)
741 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
742 __func__, msg->len, msg->msg, msg->reply,
743 !block ? ", nb" : "");
744 else
745 dprintk(2, "%s: %*ph%s\n",
746 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
748 if (msg->timeout && msg->len == 1) {
749 dprintk(1, "%s: can't reply to poll msg\n", __func__);
750 return -EINVAL;
752 if (msg->len == 1) {
753 if (cec_msg_destination(msg) == 0xf) {
754 dprintk(1, "%s: invalid poll message\n", __func__);
755 return -EINVAL;
757 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
759 * If the destination is a logical address our adapter
760 * has already claimed, then just NACK this.
761 * It depends on the hardware what it will do with a
762 * POLL to itself (some OK this), so it is just as
763 * easy to handle it here so the behavior will be
764 * consistent.
766 msg->tx_ts = ktime_get_ns();
767 msg->tx_status = CEC_TX_STATUS_NACK |
768 CEC_TX_STATUS_MAX_RETRIES;
769 msg->tx_nack_cnt = 1;
770 msg->sequence = ++adap->sequence;
771 if (!msg->sequence)
772 msg->sequence = ++adap->sequence;
773 return 0;
776 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
777 cec_has_log_addr(adap, cec_msg_destination(msg))) {
778 dprintk(1, "%s: destination is the adapter itself\n", __func__);
779 return -EINVAL;
781 if (msg->len > 1 && adap->is_configured &&
782 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
783 dprintk(1, "%s: initiator has unknown logical address %d\n",
784 __func__, cec_msg_initiator(msg));
785 return -EINVAL;
787 if (!adap->is_configured && !adap->is_configuring) {
788 if (adap->needs_hpd || msg->msg[0] != 0xf0) {
789 dprintk(1, "%s: adapter is unconfigured\n", __func__);
790 return -ENONET;
792 if (msg->reply) {
793 dprintk(1, "%s: invalid msg->reply\n", __func__);
794 return -EINVAL;
798 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
799 dprintk(1, "%s: transmit queue full\n", __func__);
800 return -EBUSY;
803 data = kzalloc(sizeof(*data), GFP_KERNEL);
804 if (!data)
805 return -ENOMEM;
807 msg->sequence = ++adap->sequence;
808 if (!msg->sequence)
809 msg->sequence = ++adap->sequence;
811 data->msg = *msg;
812 data->fh = fh;
813 data->adap = adap;
814 data->blocking = block;
817 * Determine if this message follows a message from the same
818 * initiator. Needed to determine the free signal time later on.
820 if (msg->len > 1) {
821 if (!(list_empty(&adap->transmit_queue))) {
822 const struct cec_data *last;
824 last = list_last_entry(&adap->transmit_queue,
825 const struct cec_data, list);
826 last_initiator = cec_msg_initiator(&last->msg);
827 } else if (adap->transmitting) {
828 last_initiator =
829 cec_msg_initiator(&adap->transmitting->msg);
832 data->new_initiator = last_initiator != cec_msg_initiator(msg);
833 init_completion(&data->c);
834 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
836 if (fh)
837 list_add_tail(&data->xfer_list, &fh->xfer_list);
839 list_add_tail(&data->list, &adap->transmit_queue);
840 adap->transmit_queue_sz++;
841 if (!adap->transmitting)
842 wake_up_interruptible(&adap->kthread_waitq);
844 /* All done if we don't need to block waiting for completion */
845 if (!block)
846 return 0;
849 * If we don't get a completion before this time something is really
850 * wrong and we time out.
852 timeout = CEC_XFER_TIMEOUT_MS;
853 /* Add the requested timeout if we have to wait for a reply as well */
854 if (msg->timeout)
855 timeout += msg->timeout;
858 * Release the lock and wait, retake the lock afterwards.
860 mutex_unlock(&adap->lock);
861 res = wait_for_completion_killable_timeout(&data->c,
862 msecs_to_jiffies(timeout));
863 mutex_lock(&adap->lock);
865 if (data->completed) {
866 /* The transmit completed (possibly with an error) */
867 *msg = data->msg;
868 kfree(data);
869 return 0;
872 * The wait for completion timed out or was interrupted, so mark this
873 * as non-blocking and disconnect from the filehandle since it is
874 * still 'in flight'. When it finally completes it will just drop the
875 * result silently.
877 data->blocking = false;
878 if (data->fh)
879 list_del(&data->xfer_list);
880 data->fh = NULL;
882 if (res == 0) { /* timed out */
883 /* Check if the reply or the transmit failed */
884 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
885 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
886 else
887 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
889 return res > 0 ? 0 : res;
892 /* Helper function to be used by drivers and this framework. */
893 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
894 bool block)
896 int ret;
898 mutex_lock(&adap->lock);
899 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
900 mutex_unlock(&adap->lock);
901 return ret;
903 EXPORT_SYMBOL_GPL(cec_transmit_msg);
906 * I don't like forward references but without this the low-level
907 * cec_received_msg() function would come after a bunch of high-level
908 * CEC protocol handling functions. That was very confusing.
910 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
911 bool is_reply);
913 #define DIRECTED 0x80
914 #define BCAST1_4 0x40
915 #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
916 #define BCAST (BCAST1_4 | BCAST2_0)
917 #define BOTH (BCAST | DIRECTED)
920 * Specify minimum length and whether the message is directed, broadcast
921 * or both. Messages that do not match the criteria are ignored as per
922 * the CEC specification.
924 static const u8 cec_msg_size[256] = {
925 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
926 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
927 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
928 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
929 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
930 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
931 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
932 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
933 [CEC_MSG_STANDBY] = 2 | BOTH,
934 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
935 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
936 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
937 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
938 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
939 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
940 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
941 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
942 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
943 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
944 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
945 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
946 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
947 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
948 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
949 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
950 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
951 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
952 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
953 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
954 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
955 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
956 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
957 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
958 [CEC_MSG_PLAY] = 3 | DIRECTED,
959 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
960 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
961 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
962 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
963 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
964 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
965 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
966 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
967 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
968 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
969 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
970 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
971 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
972 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
973 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
974 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
975 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
976 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
977 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
978 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
979 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
980 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
981 [CEC_MSG_ABORT] = 2 | DIRECTED,
982 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
983 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
984 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
985 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
986 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
987 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
988 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
989 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
990 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
991 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
992 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
993 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
994 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
995 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
996 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
997 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
998 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
999 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
1002 /* Called by the CEC adapter if a message is received */
1003 void cec_received_msg_ts(struct cec_adapter *adap,
1004 struct cec_msg *msg, ktime_t ts)
1006 struct cec_data *data;
1007 u8 msg_init = cec_msg_initiator(msg);
1008 u8 msg_dest = cec_msg_destination(msg);
1009 u8 cmd = msg->msg[1];
1010 bool is_reply = false;
1011 bool valid_la = true;
1012 u8 min_len = 0;
1014 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
1015 return;
1018 * Some CEC adapters will receive the messages that they transmitted.
1019 * This test filters out those messages by checking if we are the
1020 * initiator, and just returning in that case.
1022 * Note that this won't work if this is an Unregistered device.
1024 * It is bad practice if the hardware receives the message that it
1025 * transmitted and luckily most CEC adapters behave correctly in this
1026 * respect.
1028 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
1029 cec_has_log_addr(adap, msg_init))
1030 return;
1032 msg->rx_ts = ktime_to_ns(ts);
1033 msg->rx_status = CEC_RX_STATUS_OK;
1034 msg->sequence = msg->reply = msg->timeout = 0;
1035 msg->tx_status = 0;
1036 msg->tx_ts = 0;
1037 msg->tx_arb_lost_cnt = 0;
1038 msg->tx_nack_cnt = 0;
1039 msg->tx_low_drive_cnt = 0;
1040 msg->tx_error_cnt = 0;
1041 msg->flags = 0;
1042 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
1044 mutex_lock(&adap->lock);
1045 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1047 /* Check if this message was for us (directed or broadcast). */
1048 if (!cec_msg_is_broadcast(msg))
1049 valid_la = cec_has_log_addr(adap, msg_dest);
1052 * Check if the length is not too short or if the message is a
1053 * broadcast message where a directed message was expected or
1054 * vice versa. If so, then the message has to be ignored (according
1055 * to section CEC 7.3 and CEC 12.2).
1057 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
1058 u8 dir_fl = cec_msg_size[cmd] & BOTH;
1060 min_len = cec_msg_size[cmd] & 0x1f;
1061 if (msg->len < min_len)
1062 valid_la = false;
1063 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
1064 valid_la = false;
1065 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
1066 valid_la = false;
1067 else if (cec_msg_is_broadcast(msg) &&
1068 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
1069 !(dir_fl & BCAST2_0))
1070 valid_la = false;
1072 if (valid_la && min_len) {
1073 /* These messages have special length requirements */
1074 switch (cmd) {
1075 case CEC_MSG_TIMER_STATUS:
1076 if (msg->msg[2] & 0x10) {
1077 switch (msg->msg[2] & 0xf) {
1078 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
1079 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
1080 if (msg->len < 5)
1081 valid_la = false;
1082 break;
1084 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
1085 if (msg->len < 5)
1086 valid_la = false;
1088 break;
1089 case CEC_MSG_RECORD_ON:
1090 switch (msg->msg[2]) {
1091 case CEC_OP_RECORD_SRC_OWN:
1092 break;
1093 case CEC_OP_RECORD_SRC_DIGITAL:
1094 if (msg->len < 10)
1095 valid_la = false;
1096 break;
1097 case CEC_OP_RECORD_SRC_ANALOG:
1098 if (msg->len < 7)
1099 valid_la = false;
1100 break;
1101 case CEC_OP_RECORD_SRC_EXT_PLUG:
1102 if (msg->len < 4)
1103 valid_la = false;
1104 break;
1105 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1106 if (msg->len < 5)
1107 valid_la = false;
1108 break;
1110 break;
1114 /* It's a valid message and not a poll or CDC message */
1115 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1116 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1118 /* The aborted command is in msg[2] */
1119 if (abort)
1120 cmd = msg->msg[2];
1123 * Walk over all transmitted messages that are waiting for a
1124 * reply.
1126 list_for_each_entry(data, &adap->wait_queue, list) {
1127 struct cec_msg *dst = &data->msg;
1130 * The *only* CEC message that has two possible replies
1131 * is CEC_MSG_INITIATE_ARC.
1132 * In this case allow either of the two replies.
1134 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1135 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1136 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1137 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1138 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1139 dst->reply = cmd;
1141 /* Does the command match? */
1142 if ((abort && cmd != dst->msg[1]) ||
1143 (!abort && cmd != dst->reply))
1144 continue;
1146 /* Does the addressing match? */
1147 if (msg_init != cec_msg_destination(dst) &&
1148 !cec_msg_is_broadcast(dst))
1149 continue;
1151 /* We got a reply */
1152 memcpy(dst->msg, msg->msg, msg->len);
1153 dst->len = msg->len;
1154 dst->rx_ts = msg->rx_ts;
1155 dst->rx_status = msg->rx_status;
1156 if (abort)
1157 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1158 msg->flags = dst->flags;
1159 /* Remove it from the wait_queue */
1160 list_del_init(&data->list);
1162 /* Cancel the pending timeout work */
1163 if (!cancel_delayed_work(&data->work)) {
1164 mutex_unlock(&adap->lock);
1165 flush_scheduled_work();
1166 mutex_lock(&adap->lock);
1169 * Mark this as a reply, provided someone is still
1170 * waiting for the answer.
1172 if (data->fh)
1173 is_reply = true;
1174 cec_data_completed(data);
1175 break;
1178 mutex_unlock(&adap->lock);
1180 /* Pass the message on to any monitoring filehandles */
1181 cec_queue_msg_monitor(adap, msg, valid_la);
1183 /* We're done if it is not for us or a poll message */
1184 if (!valid_la || msg->len <= 1)
1185 return;
1187 if (adap->log_addrs.log_addr_mask == 0)
1188 return;
1191 * Process the message on the protocol level. If is_reply is true,
1192 * then cec_receive_notify() won't pass on the reply to the listener(s)
1193 * since that was already done by cec_data_completed() above.
1195 cec_receive_notify(adap, msg, is_reply);
1197 EXPORT_SYMBOL_GPL(cec_received_msg_ts);
1199 /* Logical Address Handling */
1202 * Attempt to claim a specific logical address.
1204 * This function is called with adap->lock held.
1206 static int cec_config_log_addr(struct cec_adapter *adap,
1207 unsigned int idx,
1208 unsigned int log_addr)
1210 struct cec_log_addrs *las = &adap->log_addrs;
1211 struct cec_msg msg = { };
1212 int err;
1214 if (cec_has_log_addr(adap, log_addr))
1215 return 0;
1217 /* Send poll message */
1218 msg.len = 1;
1219 msg.msg[0] = (log_addr << 4) | log_addr;
1220 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1223 * While trying to poll the physical address was reset
1224 * and the adapter was unconfigured, so bail out.
1226 if (!adap->is_configuring)
1227 return -EINTR;
1229 if (err)
1230 return err;
1232 if (msg.tx_status & CEC_TX_STATUS_OK)
1233 return 0;
1236 * Message not acknowledged, so this logical
1237 * address is free to use.
1239 err = adap->ops->adap_log_addr(adap, log_addr);
1240 if (err)
1241 return err;
1243 las->log_addr[idx] = log_addr;
1244 las->log_addr_mask |= 1 << log_addr;
1245 adap->phys_addrs[log_addr] = adap->phys_addr;
1246 return 1;
1250 * Unconfigure the adapter: clear all logical addresses and send
1251 * the state changed event.
1253 * This function is called with adap->lock held.
1255 static void cec_adap_unconfigure(struct cec_adapter *adap)
1257 if (!adap->needs_hpd ||
1258 adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1259 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1260 adap->log_addrs.log_addr_mask = 0;
1261 adap->is_configuring = false;
1262 adap->is_configured = false;
1263 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1264 cec_flush(adap);
1265 wake_up_interruptible(&adap->kthread_waitq);
1266 cec_post_state_event(adap);
1270 * Attempt to claim the required logical addresses.
1272 static int cec_config_thread_func(void *arg)
1274 /* The various LAs for each type of device */
1275 static const u8 tv_log_addrs[] = {
1276 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1277 CEC_LOG_ADDR_INVALID
1279 static const u8 record_log_addrs[] = {
1280 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1281 CEC_LOG_ADDR_RECORD_3,
1282 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1283 CEC_LOG_ADDR_INVALID
1285 static const u8 tuner_log_addrs[] = {
1286 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1287 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1288 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1289 CEC_LOG_ADDR_INVALID
1291 static const u8 playback_log_addrs[] = {
1292 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1293 CEC_LOG_ADDR_PLAYBACK_3,
1294 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1295 CEC_LOG_ADDR_INVALID
1297 static const u8 audiosystem_log_addrs[] = {
1298 CEC_LOG_ADDR_AUDIOSYSTEM,
1299 CEC_LOG_ADDR_INVALID
1301 static const u8 specific_use_log_addrs[] = {
1302 CEC_LOG_ADDR_SPECIFIC,
1303 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1304 CEC_LOG_ADDR_INVALID
1306 static const u8 *type2addrs[6] = {
1307 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1308 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1309 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1310 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1311 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1312 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1314 static const u16 type2mask[] = {
1315 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1316 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1317 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1318 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1319 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1320 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1322 struct cec_adapter *adap = arg;
1323 struct cec_log_addrs *las = &adap->log_addrs;
1324 int err;
1325 int i, j;
1327 mutex_lock(&adap->lock);
1328 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1329 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1330 las->log_addr_mask = 0;
1332 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1333 goto configured;
1335 for (i = 0; i < las->num_log_addrs; i++) {
1336 unsigned int type = las->log_addr_type[i];
1337 const u8 *la_list;
1338 u8 last_la;
1341 * The TV functionality can only map to physical address 0.
1342 * For any other address, try the Specific functionality
1343 * instead as per the spec.
1345 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1346 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1348 la_list = type2addrs[type];
1349 last_la = las->log_addr[i];
1350 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1351 if (last_la == CEC_LOG_ADDR_INVALID ||
1352 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1353 !((1 << last_la) & type2mask[type]))
1354 last_la = la_list[0];
1356 err = cec_config_log_addr(adap, i, last_la);
1357 if (err > 0) /* Reused last LA */
1358 continue;
1360 if (err < 0)
1361 goto unconfigure;
1363 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1364 /* Tried this one already, skip it */
1365 if (la_list[j] == last_la)
1366 continue;
1367 /* The backup addresses are CEC 2.0 specific */
1368 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1369 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1370 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1371 continue;
1373 err = cec_config_log_addr(adap, i, la_list[j]);
1374 if (err == 0) /* LA is in use */
1375 continue;
1376 if (err < 0)
1377 goto unconfigure;
1378 /* Done, claimed an LA */
1379 break;
1382 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1383 dprintk(1, "could not claim LA %d\n", i);
1386 if (adap->log_addrs.log_addr_mask == 0 &&
1387 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1388 goto unconfigure;
1390 configured:
1391 if (adap->log_addrs.log_addr_mask == 0) {
1392 /* Fall back to unregistered */
1393 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1394 las->log_addr_mask = 1 << las->log_addr[0];
1395 for (i = 1; i < las->num_log_addrs; i++)
1396 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1398 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1399 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1400 adap->is_configured = true;
1401 adap->is_configuring = false;
1402 cec_post_state_event(adap);
1405 * Now post the Report Features and Report Physical Address broadcast
1406 * messages. Note that these are non-blocking transmits, meaning that
1407 * they are just queued up and once adap->lock is unlocked the main
1408 * thread will kick in and start transmitting these.
1410 * If after this function is done (but before one or more of these
1411 * messages are actually transmitted) the CEC adapter is unconfigured,
1412 * then any remaining messages will be dropped by the main thread.
1414 for (i = 0; i < las->num_log_addrs; i++) {
1415 struct cec_msg msg = {};
1417 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1418 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1419 continue;
1421 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1423 /* Report Features must come first according to CEC 2.0 */
1424 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1425 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1426 cec_fill_msg_report_features(adap, &msg, i);
1427 cec_transmit_msg_fh(adap, &msg, NULL, false);
1430 /* Report Physical Address */
1431 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1432 las->primary_device_type[i]);
1433 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1434 las->log_addr[i],
1435 cec_phys_addr_exp(adap->phys_addr));
1436 cec_transmit_msg_fh(adap, &msg, NULL, false);
1438 adap->kthread_config = NULL;
1439 complete(&adap->config_completion);
1440 mutex_unlock(&adap->lock);
1441 return 0;
1443 unconfigure:
1444 for (i = 0; i < las->num_log_addrs; i++)
1445 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1446 cec_adap_unconfigure(adap);
1447 adap->kthread_config = NULL;
1448 mutex_unlock(&adap->lock);
1449 complete(&adap->config_completion);
1450 return 0;
1454 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1455 * logical addresses.
1457 * This function is called with adap->lock held.
1459 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1461 if (WARN_ON(adap->is_configuring || adap->is_configured))
1462 return;
1464 init_completion(&adap->config_completion);
1466 /* Ready to kick off the thread */
1467 adap->is_configuring = true;
1468 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1469 "ceccfg-%s", adap->name);
1470 if (IS_ERR(adap->kthread_config)) {
1471 adap->kthread_config = NULL;
1472 } else if (block) {
1473 mutex_unlock(&adap->lock);
1474 wait_for_completion(&adap->config_completion);
1475 mutex_lock(&adap->lock);
1479 /* Set a new physical address and send an event notifying userspace of this.
1481 * This function is called with adap->lock held.
1483 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1485 if (phys_addr == adap->phys_addr)
1486 return;
1487 if (phys_addr != CEC_PHYS_ADDR_INVALID && adap->devnode.unregistered)
1488 return;
1490 dprintk(1, "new physical address %x.%x.%x.%x\n",
1491 cec_phys_addr_exp(phys_addr));
1492 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1493 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1494 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1495 cec_post_state_event(adap);
1496 cec_adap_unconfigure(adap);
1497 /* Disabling monitor all mode should always succeed */
1498 if (adap->monitor_all_cnt)
1499 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1500 mutex_lock(&adap->devnode.lock);
1501 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1502 WARN_ON(adap->ops->adap_enable(adap, false));
1503 mutex_unlock(&adap->devnode.lock);
1504 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1505 return;
1508 mutex_lock(&adap->devnode.lock);
1509 if ((adap->needs_hpd || list_empty(&adap->devnode.fhs)) &&
1510 adap->ops->adap_enable(adap, true)) {
1511 mutex_unlock(&adap->devnode.lock);
1512 return;
1515 if (adap->monitor_all_cnt &&
1516 call_op(adap, adap_monitor_all_enable, true)) {
1517 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1518 WARN_ON(adap->ops->adap_enable(adap, false));
1519 mutex_unlock(&adap->devnode.lock);
1520 return;
1522 mutex_unlock(&adap->devnode.lock);
1524 adap->phys_addr = phys_addr;
1525 cec_post_state_event(adap);
1526 if (adap->log_addrs.num_log_addrs)
1527 cec_claim_log_addrs(adap, block);
1530 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1532 if (IS_ERR_OR_NULL(adap))
1533 return;
1535 mutex_lock(&adap->lock);
1536 __cec_s_phys_addr(adap, phys_addr, block);
1537 mutex_unlock(&adap->lock);
1539 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1541 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1542 const struct edid *edid)
1544 u16 pa = CEC_PHYS_ADDR_INVALID;
1546 if (edid && edid->extensions)
1547 pa = cec_get_edid_phys_addr((const u8 *)edid,
1548 EDID_LENGTH * (edid->extensions + 1), NULL);
1549 cec_s_phys_addr(adap, pa, false);
1551 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1554 * Called from either the ioctl or a driver to set the logical addresses.
1556 * This function is called with adap->lock held.
1558 int __cec_s_log_addrs(struct cec_adapter *adap,
1559 struct cec_log_addrs *log_addrs, bool block)
1561 u16 type_mask = 0;
1562 int i;
1564 if (adap->devnode.unregistered)
1565 return -ENODEV;
1567 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1568 cec_adap_unconfigure(adap);
1569 adap->log_addrs.num_log_addrs = 0;
1570 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++)
1571 adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID;
1572 adap->log_addrs.osd_name[0] = '\0';
1573 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
1574 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
1575 return 0;
1578 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1580 * Sanitize log_addrs fields if a CDC-Only device is
1581 * requested.
1583 log_addrs->num_log_addrs = 1;
1584 log_addrs->osd_name[0] = '\0';
1585 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1586 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1588 * This is just an internal convention since a CDC-Only device
1589 * doesn't have to be a switch. But switches already use
1590 * unregistered, so it makes some kind of sense to pick this
1591 * as the primary device. Since a CDC-Only device never sends
1592 * any 'normal' CEC messages this primary device type is never
1593 * sent over the CEC bus.
1595 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1596 log_addrs->all_device_types[0] = 0;
1597 log_addrs->features[0][0] = 0;
1598 log_addrs->features[0][1] = 0;
1601 /* Ensure the osd name is 0-terminated */
1602 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1604 /* Sanity checks */
1605 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1606 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1607 return -EINVAL;
1611 * Vendor ID is a 24 bit number, so check if the value is
1612 * within the correct range.
1614 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1615 (log_addrs->vendor_id & 0xff000000) != 0) {
1616 dprintk(1, "invalid vendor ID\n");
1617 return -EINVAL;
1620 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1621 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1622 dprintk(1, "invalid CEC version\n");
1623 return -EINVAL;
1626 if (log_addrs->num_log_addrs > 1)
1627 for (i = 0; i < log_addrs->num_log_addrs; i++)
1628 if (log_addrs->log_addr_type[i] ==
1629 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1630 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1631 return -EINVAL;
1634 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1635 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1636 u8 *features = log_addrs->features[i];
1637 bool op_is_dev_features = false;
1638 unsigned j;
1640 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1641 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1642 dprintk(1, "duplicate logical address type\n");
1643 return -EINVAL;
1645 type_mask |= 1 << log_addrs->log_addr_type[i];
1646 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1647 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1648 /* Record already contains the playback functionality */
1649 dprintk(1, "invalid record + playback combination\n");
1650 return -EINVAL;
1652 if (log_addrs->primary_device_type[i] >
1653 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1654 dprintk(1, "unknown primary device type\n");
1655 return -EINVAL;
1657 if (log_addrs->primary_device_type[i] == 2) {
1658 dprintk(1, "invalid primary device type\n");
1659 return -EINVAL;
1661 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1662 dprintk(1, "unknown logical address type\n");
1663 return -EINVAL;
1665 for (j = 0; j < feature_sz; j++) {
1666 if ((features[j] & 0x80) == 0) {
1667 if (op_is_dev_features)
1668 break;
1669 op_is_dev_features = true;
1672 if (!op_is_dev_features || j == feature_sz) {
1673 dprintk(1, "malformed features\n");
1674 return -EINVAL;
1676 /* Zero unused part of the feature array */
1677 memset(features + j + 1, 0, feature_sz - j - 1);
1680 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1681 if (log_addrs->num_log_addrs > 2) {
1682 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1683 return -EINVAL;
1685 if (log_addrs->num_log_addrs == 2) {
1686 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1687 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1688 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1689 return -EINVAL;
1691 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1692 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1693 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1694 return -EINVAL;
1699 /* Zero unused LAs */
1700 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1701 log_addrs->primary_device_type[i] = 0;
1702 log_addrs->log_addr_type[i] = 0;
1703 log_addrs->all_device_types[i] = 0;
1704 memset(log_addrs->features[i], 0,
1705 sizeof(log_addrs->features[i]));
1708 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1709 adap->log_addrs = *log_addrs;
1710 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1711 cec_claim_log_addrs(adap, block);
1712 return 0;
1715 int cec_s_log_addrs(struct cec_adapter *adap,
1716 struct cec_log_addrs *log_addrs, bool block)
1718 int err;
1720 mutex_lock(&adap->lock);
1721 err = __cec_s_log_addrs(adap, log_addrs, block);
1722 mutex_unlock(&adap->lock);
1723 return err;
1725 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1727 /* High-level core CEC message handling */
1729 /* Fill in the Report Features message */
1730 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1731 struct cec_msg *msg,
1732 unsigned int la_idx)
1734 const struct cec_log_addrs *las = &adap->log_addrs;
1735 const u8 *features = las->features[la_idx];
1736 bool op_is_dev_features = false;
1737 unsigned int idx;
1739 /* Report Features */
1740 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1741 msg->len = 4;
1742 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1743 msg->msg[2] = adap->log_addrs.cec_version;
1744 msg->msg[3] = las->all_device_types[la_idx];
1746 /* Write RC Profiles first, then Device Features */
1747 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1748 msg->msg[msg->len++] = features[idx];
1749 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1750 if (op_is_dev_features)
1751 break;
1752 op_is_dev_features = true;
1757 /* Transmit the Feature Abort message */
1758 static int cec_feature_abort_reason(struct cec_adapter *adap,
1759 struct cec_msg *msg, u8 reason)
1761 struct cec_msg tx_msg = { };
1764 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1765 * message!
1767 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1768 return 0;
1769 /* Don't Feature Abort messages from 'Unregistered' */
1770 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1771 return 0;
1772 cec_msg_set_reply_to(&tx_msg, msg);
1773 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1774 return cec_transmit_msg(adap, &tx_msg, false);
1777 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1779 return cec_feature_abort_reason(adap, msg,
1780 CEC_OP_ABORT_UNRECOGNIZED_OP);
1783 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1785 return cec_feature_abort_reason(adap, msg,
1786 CEC_OP_ABORT_REFUSED);
1790 * Called when a CEC message is received. This function will do any
1791 * necessary core processing. The is_reply bool is true if this message
1792 * is a reply to an earlier transmit.
1794 * The message is either a broadcast message or a valid directed message.
1796 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1797 bool is_reply)
1799 bool is_broadcast = cec_msg_is_broadcast(msg);
1800 u8 dest_laddr = cec_msg_destination(msg);
1801 u8 init_laddr = cec_msg_initiator(msg);
1802 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1803 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1804 bool from_unregistered = init_laddr == 0xf;
1805 struct cec_msg tx_cec_msg = { };
1807 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1809 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1810 if (cec_is_cdc_only(&adap->log_addrs) &&
1811 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1812 return 0;
1814 if (adap->ops->received) {
1815 /* Allow drivers to process the message first */
1816 if (adap->ops->received(adap, msg) != -ENOMSG)
1817 return 0;
1821 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1822 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1823 * handled by the CEC core, even if the passthrough mode is on.
1824 * The others are just ignored if passthrough mode is on.
1826 switch (msg->msg[1]) {
1827 case CEC_MSG_GET_CEC_VERSION:
1828 case CEC_MSG_ABORT:
1829 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1830 case CEC_MSG_GIVE_OSD_NAME:
1832 * These messages reply with a directed message, so ignore if
1833 * the initiator is Unregistered.
1835 if (!adap->passthrough && from_unregistered)
1836 return 0;
1837 /* Fall through */
1838 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1839 case CEC_MSG_GIVE_FEATURES:
1840 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1842 * Skip processing these messages if the passthrough mode
1843 * is on.
1845 if (adap->passthrough)
1846 goto skip_processing;
1847 /* Ignore if addressing is wrong */
1848 if (is_broadcast)
1849 return 0;
1850 break;
1852 case CEC_MSG_USER_CONTROL_PRESSED:
1853 case CEC_MSG_USER_CONTROL_RELEASED:
1854 /* Wrong addressing mode: don't process */
1855 if (is_broadcast || from_unregistered)
1856 goto skip_processing;
1857 break;
1859 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1861 * This message is always processed, regardless of the
1862 * passthrough setting.
1864 * Exception: don't process if wrong addressing mode.
1866 if (!is_broadcast)
1867 goto skip_processing;
1868 break;
1870 default:
1871 break;
1874 cec_msg_set_reply_to(&tx_cec_msg, msg);
1876 switch (msg->msg[1]) {
1877 /* The following messages are processed but still passed through */
1878 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1879 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1881 if (!from_unregistered)
1882 adap->phys_addrs[init_laddr] = pa;
1883 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
1884 cec_phys_addr_exp(pa), init_laddr);
1885 break;
1888 case CEC_MSG_USER_CONTROL_PRESSED:
1889 if (!(adap->capabilities & CEC_CAP_RC) ||
1890 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1891 break;
1893 #ifdef CONFIG_MEDIA_CEC_RC
1894 switch (msg->msg[2]) {
1896 * Play function, this message can have variable length
1897 * depending on the specific play function that is used.
1899 case 0x60:
1900 if (msg->len == 2)
1901 rc_keydown(adap->rc, RC_PROTO_CEC,
1902 msg->msg[2], 0);
1903 else
1904 rc_keydown(adap->rc, RC_PROTO_CEC,
1905 msg->msg[2] << 8 | msg->msg[3], 0);
1906 break;
1908 * Other function messages that are not handled.
1909 * Currently the RC framework does not allow to supply an
1910 * additional parameter to a keypress. These "keys" contain
1911 * other information such as channel number, an input number
1912 * etc.
1913 * For the time being these messages are not processed by the
1914 * framework and are simply forwarded to the user space.
1916 case 0x56: case 0x57:
1917 case 0x67: case 0x68: case 0x69: case 0x6a:
1918 break;
1919 default:
1920 rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0);
1921 break;
1923 #endif
1924 break;
1926 case CEC_MSG_USER_CONTROL_RELEASED:
1927 if (!(adap->capabilities & CEC_CAP_RC) ||
1928 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1929 break;
1930 #ifdef CONFIG_MEDIA_CEC_RC
1931 rc_keyup(adap->rc);
1932 #endif
1933 break;
1936 * The remaining messages are only processed if the passthrough mode
1937 * is off.
1939 case CEC_MSG_GET_CEC_VERSION:
1940 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1941 return cec_transmit_msg(adap, &tx_cec_msg, false);
1943 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1944 /* Do nothing for CEC switches using addr 15 */
1945 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1946 return 0;
1947 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1948 return cec_transmit_msg(adap, &tx_cec_msg, false);
1950 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1951 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1952 return cec_feature_abort(adap, msg);
1953 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1954 return cec_transmit_msg(adap, &tx_cec_msg, false);
1956 case CEC_MSG_ABORT:
1957 /* Do nothing for CEC switches */
1958 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1959 return 0;
1960 return cec_feature_refused(adap, msg);
1962 case CEC_MSG_GIVE_OSD_NAME: {
1963 if (adap->log_addrs.osd_name[0] == 0)
1964 return cec_feature_abort(adap, msg);
1965 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1966 return cec_transmit_msg(adap, &tx_cec_msg, false);
1969 case CEC_MSG_GIVE_FEATURES:
1970 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1971 return cec_feature_abort(adap, msg);
1972 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1973 return cec_transmit_msg(adap, &tx_cec_msg, false);
1975 default:
1977 * Unprocessed messages are aborted if userspace isn't doing
1978 * any processing either.
1980 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
1981 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1982 return cec_feature_abort(adap, msg);
1983 break;
1986 skip_processing:
1987 /* If this was a reply, then we're done, unless otherwise specified */
1988 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
1989 return 0;
1992 * Send to the exclusive follower if there is one, otherwise send
1993 * to all followers.
1995 if (adap->cec_follower)
1996 cec_queue_msg_fh(adap->cec_follower, msg);
1997 else
1998 cec_queue_msg_followers(adap, msg);
1999 return 0;
2003 * Helper functions to keep track of the 'monitor all' use count.
2005 * These functions are called with adap->lock held.
2007 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
2009 int ret = 0;
2011 if (adap->monitor_all_cnt == 0)
2012 ret = call_op(adap, adap_monitor_all_enable, 1);
2013 if (ret == 0)
2014 adap->monitor_all_cnt++;
2015 return ret;
2018 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
2020 adap->monitor_all_cnt--;
2021 if (adap->monitor_all_cnt == 0)
2022 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
2026 * Helper functions to keep track of the 'monitor pin' use count.
2028 * These functions are called with adap->lock held.
2030 int cec_monitor_pin_cnt_inc(struct cec_adapter *adap)
2032 int ret = 0;
2034 if (adap->monitor_pin_cnt == 0)
2035 ret = call_op(adap, adap_monitor_pin_enable, 1);
2036 if (ret == 0)
2037 adap->monitor_pin_cnt++;
2038 return ret;
2041 void cec_monitor_pin_cnt_dec(struct cec_adapter *adap)
2043 adap->monitor_pin_cnt--;
2044 if (adap->monitor_pin_cnt == 0)
2045 WARN_ON(call_op(adap, adap_monitor_pin_enable, 0));
2048 #ifdef CONFIG_DEBUG_FS
2050 * Log the current state of the CEC adapter.
2051 * Very useful for debugging.
2053 int cec_adap_status(struct seq_file *file, void *priv)
2055 struct cec_adapter *adap = dev_get_drvdata(file->private);
2056 struct cec_data *data;
2058 mutex_lock(&adap->lock);
2059 seq_printf(file, "configured: %d\n", adap->is_configured);
2060 seq_printf(file, "configuring: %d\n", adap->is_configuring);
2061 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
2062 cec_phys_addr_exp(adap->phys_addr));
2063 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
2064 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
2065 if (adap->cec_follower)
2066 seq_printf(file, "has CEC follower%s\n",
2067 adap->passthrough ? " (in passthrough mode)" : "");
2068 if (adap->cec_initiator)
2069 seq_puts(file, "has CEC initiator\n");
2070 if (adap->monitor_all_cnt)
2071 seq_printf(file, "file handles in Monitor All mode: %u\n",
2072 adap->monitor_all_cnt);
2073 if (adap->tx_timeouts) {
2074 seq_printf(file, "transmit timeouts: %u\n",
2075 adap->tx_timeouts);
2076 adap->tx_timeouts = 0;
2078 data = adap->transmitting;
2079 if (data)
2080 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
2081 data->msg.len, data->msg.msg, data->msg.reply,
2082 data->msg.timeout);
2083 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
2084 list_for_each_entry(data, &adap->transmit_queue, list) {
2085 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
2086 data->msg.len, data->msg.msg, data->msg.reply,
2087 data->msg.timeout);
2089 list_for_each_entry(data, &adap->wait_queue, list) {
2090 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
2091 data->msg.len, data->msg.msg, data->msg.reply,
2092 data->msg.timeout);
2095 call_void_op(adap, adap_status, file);
2096 mutex_unlock(&adap->lock);
2097 return 0;
2099 #endif