1 // SPDX-License-Identifier: GPL-2.0-only
3 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/kmod.h>
13 #include <linux/ktime.h>
14 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
19 #include <drm/drm_edid.h>
23 static void cec_fill_msg_report_features(struct cec_adapter *adap,
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
34 * This is a sign that something it really wrong and a warning
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...) \
45 adap->ops->op(adap, ## arg); \
48 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
52 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
53 if (adap->log_addrs.log_addr[i] == log_addr)
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];
65 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
68 unsigned int loc = cec_get_edid_spa_location(edid, size);
73 return CEC_PHYS_ADDR_INVALID;
74 return (edid[loc] << 8) | edid[loc + 1];
76 EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
79 * Queue a new event for this filehandle. If ts == 0, then set it
80 * to the current time.
82 * We keep a queue of at most max_event events where max_event differs
83 * per event. If the queue becomes full, then drop the oldest event and
84 * keep track of how many events we've dropped.
86 void cec_queue_event_fh(struct cec_fh *fh,
87 const struct cec_event *new_ev, u64 ts)
89 static const u16 max_events[CEC_NUM_EVENTS] = {
90 1, 1, 800, 800, 8, 8, 8, 8
92 struct cec_event_entry *entry;
93 unsigned int ev_idx = new_ev->event - 1;
95 if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
101 mutex_lock(&fh->lock);
102 if (ev_idx < CEC_NUM_CORE_EVENTS)
103 entry = &fh->core_events[ev_idx];
105 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
107 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
108 fh->queued_events[ev_idx]) {
109 entry->ev.lost_msgs.lost_msgs +=
110 new_ev->lost_msgs.lost_msgs;
116 if (fh->queued_events[ev_idx] < max_events[ev_idx]) {
117 /* Add new msg at the end of the queue */
118 list_add_tail(&entry->list, &fh->events[ev_idx]);
119 fh->queued_events[ev_idx]++;
120 fh->total_queued_events++;
124 if (ev_idx >= CEC_NUM_CORE_EVENTS) {
125 list_add_tail(&entry->list, &fh->events[ev_idx]);
126 /* drop the oldest event */
127 entry = list_first_entry(&fh->events[ev_idx],
128 struct cec_event_entry, list);
129 list_del(&entry->list);
133 /* Mark that events were lost */
134 entry = list_first_entry_or_null(&fh->events[ev_idx],
135 struct cec_event_entry, list);
137 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
140 mutex_unlock(&fh->lock);
141 wake_up_interruptible(&fh->wait);
144 /* Queue a new event for all open filehandles. */
145 static void cec_queue_event(struct cec_adapter *adap,
146 const struct cec_event *ev)
148 u64 ts = ktime_get_ns();
151 mutex_lock(&adap->devnode.lock);
152 list_for_each_entry(fh, &adap->devnode.fhs, list)
153 cec_queue_event_fh(fh, ev, ts);
154 mutex_unlock(&adap->devnode.lock);
157 /* Notify userspace that the CEC pin changed state at the given time. */
158 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
159 bool dropped_events, ktime_t ts)
161 struct cec_event ev = {
162 .event = is_high ? CEC_EVENT_PIN_CEC_HIGH :
163 CEC_EVENT_PIN_CEC_LOW,
164 .flags = dropped_events ? CEC_EVENT_FL_DROPPED_EVENTS : 0,
168 mutex_lock(&adap->devnode.lock);
169 list_for_each_entry(fh, &adap->devnode.fhs, list)
170 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
171 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
172 mutex_unlock(&adap->devnode.lock);
174 EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event);
176 /* Notify userspace that the HPD pin changed state at the given time. */
177 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
179 struct cec_event ev = {
180 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH :
181 CEC_EVENT_PIN_HPD_LOW,
185 mutex_lock(&adap->devnode.lock);
186 list_for_each_entry(fh, &adap->devnode.fhs, list)
187 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
188 mutex_unlock(&adap->devnode.lock);
190 EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event);
192 /* Notify userspace that the 5V pin changed state at the given time. */
193 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
195 struct cec_event ev = {
196 .event = is_high ? CEC_EVENT_PIN_5V_HIGH :
197 CEC_EVENT_PIN_5V_LOW,
201 mutex_lock(&adap->devnode.lock);
202 list_for_each_entry(fh, &adap->devnode.fhs, list)
203 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
204 mutex_unlock(&adap->devnode.lock);
206 EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event);
209 * Queue a new message for this filehandle.
211 * We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the
212 * queue becomes full, then drop the oldest message and keep track
213 * of how many messages we've dropped.
215 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
217 static const struct cec_event ev_lost_msgs = {
218 .event = CEC_EVENT_LOST_MSGS,
224 struct cec_msg_entry *entry;
226 mutex_lock(&fh->lock);
227 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
230 /* Add new msg at the end of the queue */
231 list_add_tail(&entry->list, &fh->msgs);
233 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
234 /* All is fine if there is enough room */
236 mutex_unlock(&fh->lock);
237 wake_up_interruptible(&fh->wait);
242 * if the message queue is full, then drop the oldest one and
243 * send a lost message event.
245 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
246 list_del(&entry->list);
249 mutex_unlock(&fh->lock);
252 * We lost a message, either because kmalloc failed or the queue
255 cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
259 * Queue the message for those filehandles that are in monitor mode.
260 * If valid_la is true (this message is for us or was sent by us),
261 * then pass it on to any monitoring filehandle. If this message
262 * isn't for us or from us, then only give it to filehandles that
263 * are in MONITOR_ALL mode.
265 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
266 * set and the CEC adapter was placed in 'monitor all' mode.
268 static void cec_queue_msg_monitor(struct cec_adapter *adap,
269 const struct cec_msg *msg,
273 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
274 CEC_MODE_MONITOR_ALL;
276 mutex_lock(&adap->devnode.lock);
277 list_for_each_entry(fh, &adap->devnode.fhs, list) {
278 if (fh->mode_follower >= monitor_mode)
279 cec_queue_msg_fh(fh, msg);
281 mutex_unlock(&adap->devnode.lock);
285 * Queue the message for follower filehandles.
287 static void cec_queue_msg_followers(struct cec_adapter *adap,
288 const struct cec_msg *msg)
292 mutex_lock(&adap->devnode.lock);
293 list_for_each_entry(fh, &adap->devnode.fhs, list) {
294 if (fh->mode_follower == CEC_MODE_FOLLOWER)
295 cec_queue_msg_fh(fh, msg);
297 mutex_unlock(&adap->devnode.lock);
300 /* Notify userspace of an adapter state change. */
301 static void cec_post_state_event(struct cec_adapter *adap)
303 struct cec_event ev = {
304 .event = CEC_EVENT_STATE_CHANGE,
307 ev.state_change.phys_addr = adap->phys_addr;
308 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
309 cec_queue_event(adap, &ev);
313 * A CEC transmit (and a possible wait for reply) completed.
314 * If this was in blocking mode, then complete it, otherwise
315 * queue the message for userspace to dequeue later.
317 * This function is called with adap->lock held.
319 static void cec_data_completed(struct cec_data *data)
322 * Delete this transmit from the filehandle's xfer_list since
323 * we're done with it.
325 * Note that if the filehandle is closed before this transmit
326 * finished, then the release() function will set data->fh to NULL.
327 * Without that we would be referring to a closed filehandle.
330 list_del(&data->xfer_list);
332 if (data->blocking) {
334 * Someone is blocking so mark the message as completed
337 data->completed = true;
341 * No blocking, so just queue the message if needed and
345 cec_queue_msg_fh(data->fh, &data->msg);
351 * A pending CEC transmit needs to be cancelled, either because the CEC
352 * adapter is disabled or the transmit takes an impossibly long time to
355 * This function is called with adap->lock held.
357 static void cec_data_cancel(struct cec_data *data, u8 tx_status)
360 * It's either the current transmit, or it is a pending
361 * transmit. Take the appropriate action to clear it.
363 if (data->adap->transmitting == data) {
364 data->adap->transmitting = NULL;
366 list_del_init(&data->list);
367 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
368 data->adap->transmit_queue_sz--;
371 if (data->msg.tx_status & CEC_TX_STATUS_OK) {
372 data->msg.rx_ts = ktime_get_ns();
373 data->msg.rx_status = CEC_RX_STATUS_ABORTED;
375 data->msg.tx_ts = ktime_get_ns();
376 data->msg.tx_status |= tx_status |
377 CEC_TX_STATUS_MAX_RETRIES;
378 data->msg.tx_error_cnt++;
382 /* Queue transmitted message for monitoring purposes */
383 cec_queue_msg_monitor(data->adap, &data->msg, 1);
385 cec_data_completed(data);
389 * Flush all pending transmits and cancel any pending timeout work.
391 * This function is called with adap->lock held.
393 static void cec_flush(struct cec_adapter *adap)
395 struct cec_data *data, *n;
398 * If the adapter is disabled, or we're asked to stop,
399 * then cancel any pending transmits.
401 while (!list_empty(&adap->transmit_queue)) {
402 data = list_first_entry(&adap->transmit_queue,
403 struct cec_data, list);
404 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
406 if (adap->transmitting)
407 cec_data_cancel(adap->transmitting, CEC_TX_STATUS_ABORTED);
409 /* Cancel the pending timeout work. */
410 list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
411 if (cancel_delayed_work(&data->work))
412 cec_data_cancel(data, CEC_TX_STATUS_OK);
414 * If cancel_delayed_work returned false, then
415 * the cec_wait_timeout function is running,
416 * which will call cec_data_completed. So no
417 * need to do anything special in that case.
423 * Main CEC state machine
425 * Wait until the thread should be stopped, or we are not transmitting and
426 * a new transmit message is queued up, in which case we start transmitting
427 * that message. When the adapter finished transmitting the message it will
428 * call cec_transmit_done().
430 * If the adapter is disabled, then remove all queued messages instead.
432 * If the current transmit times out, then cancel that transmit.
434 int cec_thread_func(void *_adap)
436 struct cec_adapter *adap = _adap;
439 unsigned int signal_free_time;
440 struct cec_data *data;
441 bool timeout = false;
444 if (adap->transmitting) {
448 * We are transmitting a message, so add a timeout
449 * to prevent the state machine to get stuck waiting
450 * for this message to finalize and add a check to
451 * see if the adapter is disabled in which case the
452 * transmit should be canceled.
454 err = wait_event_interruptible_timeout(adap->kthread_waitq,
456 (!adap->is_configured && !adap->is_configuring)) ||
457 kthread_should_stop() ||
458 (!adap->transmitting &&
459 !list_empty(&adap->transmit_queue)),
460 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
463 /* Otherwise we just wait for something to happen. */
464 wait_event_interruptible(adap->kthread_waitq,
465 kthread_should_stop() ||
466 (!adap->transmitting &&
467 !list_empty(&adap->transmit_queue)));
470 mutex_lock(&adap->lock);
472 if ((adap->needs_hpd &&
473 (!adap->is_configured && !adap->is_configuring)) ||
474 kthread_should_stop()) {
479 if (adap->transmitting && timeout) {
481 * If we timeout, then log that. Normally this does
482 * not happen and it is an indication of a faulty CEC
483 * adapter driver, or the CEC bus is in some weird
484 * state. On rare occasions it can happen if there is
485 * so much traffic on the bus that the adapter was
486 * unable to transmit for CEC_XFER_TIMEOUT_MS (2.1s).
488 pr_warn("cec-%s: message %*ph timed out\n", adap->name,
489 adap->transmitting->msg.len,
490 adap->transmitting->msg.msg);
492 /* Just give up on this. */
493 cec_data_cancel(adap->transmitting,
494 CEC_TX_STATUS_TIMEOUT);
499 * If we are still transmitting, or there is nothing new to
500 * transmit, then just continue waiting.
502 if (adap->transmitting || list_empty(&adap->transmit_queue))
505 /* Get a new message to transmit */
506 data = list_first_entry(&adap->transmit_queue,
507 struct cec_data, list);
508 list_del_init(&data->list);
509 adap->transmit_queue_sz--;
511 /* Make this the current transmitting message */
512 adap->transmitting = data;
515 * Suggested number of attempts as per the CEC 2.0 spec:
516 * 4 attempts is the default, except for 'secondary poll
517 * messages', i.e. poll messages not sent during the adapter
518 * configuration phase when it allocates logical addresses.
520 if (data->msg.len == 1 && adap->is_configured)
525 /* Set the suggested signal free time */
526 if (data->attempts) {
527 /* should be >= 3 data bit periods for a retry */
528 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
529 } else if (adap->last_initiator !=
530 cec_msg_initiator(&data->msg)) {
531 /* should be >= 5 data bit periods for new initiator */
532 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
533 adap->last_initiator = cec_msg_initiator(&data->msg);
536 * should be >= 7 data bit periods for sending another
537 * frame immediately after another.
539 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
541 if (data->attempts == 0)
542 data->attempts = attempts;
544 /* Tell the adapter to transmit, cancel on error */
545 if (adap->ops->adap_transmit(adap, data->attempts,
546 signal_free_time, &data->msg))
547 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
550 mutex_unlock(&adap->lock);
552 if (kthread_should_stop())
559 * Called by the CEC adapter if a transmit finished.
561 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
562 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
563 u8 error_cnt, ktime_t ts)
565 struct cec_data *data;
567 unsigned int attempts_made = arb_lost_cnt + nack_cnt +
568 low_drive_cnt + error_cnt;
570 dprintk(2, "%s: status 0x%02x\n", __func__, status);
571 if (attempts_made < 1)
574 mutex_lock(&adap->lock);
575 data = adap->transmitting;
578 * This can happen if a transmit was issued and the cable is
579 * unplugged while the transmit is ongoing. Ignore this
580 * transmit in that case.
582 dprintk(1, "%s was called without an ongoing transmit!\n",
589 /* Drivers must fill in the status! */
590 WARN_ON(status == 0);
591 msg->tx_ts = ktime_to_ns(ts);
592 msg->tx_status |= status;
593 msg->tx_arb_lost_cnt += arb_lost_cnt;
594 msg->tx_nack_cnt += nack_cnt;
595 msg->tx_low_drive_cnt += low_drive_cnt;
596 msg->tx_error_cnt += error_cnt;
598 /* Mark that we're done with this transmit */
599 adap->transmitting = NULL;
602 * If there are still retry attempts left and there was an error and
603 * the hardware didn't signal that it retried itself (by setting
604 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
606 if (data->attempts > attempts_made &&
607 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
608 /* Retry this message */
609 data->attempts -= attempts_made;
611 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
612 msg->len, msg->msg, data->attempts, msg->reply);
614 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
615 msg->len, msg->msg, data->attempts);
616 /* Add the message in front of the transmit queue */
617 list_add(&data->list, &adap->transmit_queue);
618 adap->transmit_queue_sz++;
624 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
625 if (!(status & CEC_TX_STATUS_OK))
626 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
628 /* Queue transmitted message for monitoring purposes */
629 cec_queue_msg_monitor(adap, msg, 1);
631 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
634 * Queue the message into the wait queue if we want to wait
637 list_add_tail(&data->list, &adap->wait_queue);
638 schedule_delayed_work(&data->work,
639 msecs_to_jiffies(msg->timeout));
641 /* Otherwise we're done */
642 cec_data_completed(data);
647 * Wake up the main thread to see if another message is ready
648 * for transmitting or to retry the current message.
650 wake_up_interruptible(&adap->kthread_waitq);
652 mutex_unlock(&adap->lock);
654 EXPORT_SYMBOL_GPL(cec_transmit_done_ts);
656 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
657 u8 status, ktime_t ts)
659 switch (status & ~CEC_TX_STATUS_MAX_RETRIES) {
660 case CEC_TX_STATUS_OK:
661 cec_transmit_done_ts(adap, status, 0, 0, 0, 0, ts);
663 case CEC_TX_STATUS_ARB_LOST:
664 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts);
666 case CEC_TX_STATUS_NACK:
667 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts);
669 case CEC_TX_STATUS_LOW_DRIVE:
670 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts);
672 case CEC_TX_STATUS_ERROR:
673 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts);
676 /* Should never happen */
677 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
681 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts);
684 * Called when waiting for a reply times out.
686 static void cec_wait_timeout(struct work_struct *work)
688 struct cec_data *data = container_of(work, struct cec_data, work.work);
689 struct cec_adapter *adap = data->adap;
691 mutex_lock(&adap->lock);
693 * Sanity check in case the timeout and the arrival of the message
694 * happened at the same time.
696 if (list_empty(&data->list))
699 /* Mark the message as timed out */
700 list_del_init(&data->list);
701 data->msg.rx_ts = ktime_get_ns();
702 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
703 cec_data_completed(data);
705 mutex_unlock(&adap->lock);
709 * Transmit a message. The fh argument may be NULL if the transmit is not
710 * associated with a specific filehandle.
712 * This function is called with adap->lock held.
714 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
715 struct cec_fh *fh, bool block)
717 struct cec_data *data;
723 msg->tx_arb_lost_cnt = 0;
724 msg->tx_nack_cnt = 0;
725 msg->tx_low_drive_cnt = 0;
726 msg->tx_error_cnt = 0;
729 if (msg->reply && msg->timeout == 0) {
730 /* Make sure the timeout isn't 0. */
734 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
738 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
739 msg->msg[2] = adap->phys_addr >> 8;
740 msg->msg[3] = adap->phys_addr & 0xff;
744 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
745 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
749 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
752 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
753 __func__, msg->len, msg->msg, msg->reply,
754 !block ? ", nb" : "");
756 dprintk(2, "%s: %*ph%s\n",
757 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
759 if (msg->timeout && msg->len == 1) {
760 dprintk(1, "%s: can't reply to poll msg\n", __func__);
764 if (cec_msg_destination(msg) == 0xf) {
765 dprintk(1, "%s: invalid poll message\n", __func__);
768 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
770 * If the destination is a logical address our adapter
771 * has already claimed, then just NACK this.
772 * It depends on the hardware what it will do with a
773 * POLL to itself (some OK this), so it is just as
774 * easy to handle it here so the behavior will be
777 msg->tx_ts = ktime_get_ns();
778 msg->tx_status = CEC_TX_STATUS_NACK |
779 CEC_TX_STATUS_MAX_RETRIES;
780 msg->tx_nack_cnt = 1;
781 msg->sequence = ++adap->sequence;
783 msg->sequence = ++adap->sequence;
787 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
788 cec_has_log_addr(adap, cec_msg_destination(msg))) {
789 dprintk(1, "%s: destination is the adapter itself\n", __func__);
792 if (msg->len > 1 && adap->is_configured &&
793 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
794 dprintk(1, "%s: initiator has unknown logical address %d\n",
795 __func__, cec_msg_initiator(msg));
798 if (!adap->is_configured && !adap->is_configuring) {
799 if (adap->needs_hpd || msg->msg[0] != 0xf0) {
800 dprintk(1, "%s: adapter is unconfigured\n", __func__);
804 dprintk(1, "%s: invalid msg->reply\n", __func__);
809 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
810 dprintk(1, "%s: transmit queue full\n", __func__);
814 data = kzalloc(sizeof(*data), GFP_KERNEL);
818 msg->sequence = ++adap->sequence;
820 msg->sequence = ++adap->sequence;
825 data->blocking = block;
827 init_completion(&data->c);
828 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
831 list_add_tail(&data->xfer_list, &fh->xfer_list);
833 list_add_tail(&data->list, &adap->transmit_queue);
834 adap->transmit_queue_sz++;
835 if (!adap->transmitting)
836 wake_up_interruptible(&adap->kthread_waitq);
838 /* All done if we don't need to block waiting for completion */
843 * Release the lock and wait, retake the lock afterwards.
845 mutex_unlock(&adap->lock);
846 wait_for_completion_killable(&data->c);
847 mutex_lock(&adap->lock);
849 /* Cancel the transmit if it was interrupted */
850 if (!data->completed)
851 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
853 /* The transmit completed (possibly with an error) */
859 /* Helper function to be used by drivers and this framework. */
860 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
865 mutex_lock(&adap->lock);
866 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
867 mutex_unlock(&adap->lock);
870 EXPORT_SYMBOL_GPL(cec_transmit_msg);
873 * I don't like forward references but without this the low-level
874 * cec_received_msg() function would come after a bunch of high-level
875 * CEC protocol handling functions. That was very confusing.
877 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
880 #define DIRECTED 0x80
881 #define BCAST1_4 0x40
882 #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
883 #define BCAST (BCAST1_4 | BCAST2_0)
884 #define BOTH (BCAST | DIRECTED)
887 * Specify minimum length and whether the message is directed, broadcast
888 * or both. Messages that do not match the criteria are ignored as per
889 * the CEC specification.
891 static const u8 cec_msg_size[256] = {
892 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
893 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
894 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
895 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
896 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
897 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
898 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
899 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
900 [CEC_MSG_STANDBY] = 2 | BOTH,
901 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
902 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
903 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
904 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
905 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
906 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
907 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
908 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
909 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
910 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
911 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
912 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
913 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
914 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
915 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
916 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
917 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
918 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
919 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
920 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
921 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
922 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
923 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
924 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
925 [CEC_MSG_PLAY] = 3 | DIRECTED,
926 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
927 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
928 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
929 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
930 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
931 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
932 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
933 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
934 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
935 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
936 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
937 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
938 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
939 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
940 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
941 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
942 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
943 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
944 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
945 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
946 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
947 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
948 [CEC_MSG_ABORT] = 2 | DIRECTED,
949 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
950 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
951 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
952 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
953 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
954 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
955 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
956 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
957 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
958 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
959 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
960 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
961 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
962 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
963 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
964 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
965 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
966 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
969 /* Called by the CEC adapter if a message is received */
970 void cec_received_msg_ts(struct cec_adapter *adap,
971 struct cec_msg *msg, ktime_t ts)
973 struct cec_data *data;
974 u8 msg_init = cec_msg_initiator(msg);
975 u8 msg_dest = cec_msg_destination(msg);
976 u8 cmd = msg->msg[1];
977 bool is_reply = false;
978 bool valid_la = true;
981 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
985 * Some CEC adapters will receive the messages that they transmitted.
986 * This test filters out those messages by checking if we are the
987 * initiator, and just returning in that case.
989 * Note that this won't work if this is an Unregistered device.
991 * It is bad practice if the hardware receives the message that it
992 * transmitted and luckily most CEC adapters behave correctly in this
995 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
996 cec_has_log_addr(adap, msg_init))
999 msg->rx_ts = ktime_to_ns(ts);
1000 msg->rx_status = CEC_RX_STATUS_OK;
1001 msg->sequence = msg->reply = msg->timeout = 0;
1004 msg->tx_arb_lost_cnt = 0;
1005 msg->tx_nack_cnt = 0;
1006 msg->tx_low_drive_cnt = 0;
1007 msg->tx_error_cnt = 0;
1009 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
1011 mutex_lock(&adap->lock);
1012 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1014 adap->last_initiator = 0xff;
1016 /* Check if this message was for us (directed or broadcast). */
1017 if (!cec_msg_is_broadcast(msg))
1018 valid_la = cec_has_log_addr(adap, msg_dest);
1021 * Check if the length is not too short or if the message is a
1022 * broadcast message where a directed message was expected or
1023 * vice versa. If so, then the message has to be ignored (according
1024 * to section CEC 7.3 and CEC 12.2).
1026 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
1027 u8 dir_fl = cec_msg_size[cmd] & BOTH;
1029 min_len = cec_msg_size[cmd] & 0x1f;
1030 if (msg->len < min_len)
1032 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
1034 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
1036 else if (cec_msg_is_broadcast(msg) &&
1037 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
1038 !(dir_fl & BCAST2_0))
1041 if (valid_la && min_len) {
1042 /* These messages have special length requirements */
1044 case CEC_MSG_TIMER_STATUS:
1045 if (msg->msg[2] & 0x10) {
1046 switch (msg->msg[2] & 0xf) {
1047 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
1048 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
1053 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
1058 case CEC_MSG_RECORD_ON:
1059 switch (msg->msg[2]) {
1060 case CEC_OP_RECORD_SRC_OWN:
1062 case CEC_OP_RECORD_SRC_DIGITAL:
1066 case CEC_OP_RECORD_SRC_ANALOG:
1070 case CEC_OP_RECORD_SRC_EXT_PLUG:
1074 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1083 /* It's a valid message and not a poll or CDC message */
1084 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1085 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1087 /* The aborted command is in msg[2] */
1092 * Walk over all transmitted messages that are waiting for a
1095 list_for_each_entry(data, &adap->wait_queue, list) {
1096 struct cec_msg *dst = &data->msg;
1099 * The *only* CEC message that has two possible replies
1100 * is CEC_MSG_INITIATE_ARC.
1101 * In this case allow either of the two replies.
1103 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1104 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1105 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1106 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1107 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1110 /* Does the command match? */
1111 if ((abort && cmd != dst->msg[1]) ||
1112 (!abort && cmd != dst->reply))
1115 /* Does the addressing match? */
1116 if (msg_init != cec_msg_destination(dst) &&
1117 !cec_msg_is_broadcast(dst))
1120 /* We got a reply */
1121 memcpy(dst->msg, msg->msg, msg->len);
1122 dst->len = msg->len;
1123 dst->rx_ts = msg->rx_ts;
1124 dst->rx_status = msg->rx_status;
1126 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1127 msg->flags = dst->flags;
1128 /* Remove it from the wait_queue */
1129 list_del_init(&data->list);
1131 /* Cancel the pending timeout work */
1132 if (!cancel_delayed_work(&data->work)) {
1133 mutex_unlock(&adap->lock);
1134 flush_scheduled_work();
1135 mutex_lock(&adap->lock);
1138 * Mark this as a reply, provided someone is still
1139 * waiting for the answer.
1143 cec_data_completed(data);
1147 mutex_unlock(&adap->lock);
1149 /* Pass the message on to any monitoring filehandles */
1150 cec_queue_msg_monitor(adap, msg, valid_la);
1152 /* We're done if it is not for us or a poll message */
1153 if (!valid_la || msg->len <= 1)
1156 if (adap->log_addrs.log_addr_mask == 0)
1160 * Process the message on the protocol level. If is_reply is true,
1161 * then cec_receive_notify() won't pass on the reply to the listener(s)
1162 * since that was already done by cec_data_completed() above.
1164 cec_receive_notify(adap, msg, is_reply);
1166 EXPORT_SYMBOL_GPL(cec_received_msg_ts);
1168 /* Logical Address Handling */
1171 * Attempt to claim a specific logical address.
1173 * This function is called with adap->lock held.
1175 static int cec_config_log_addr(struct cec_adapter *adap,
1177 unsigned int log_addr)
1179 struct cec_log_addrs *las = &adap->log_addrs;
1180 struct cec_msg msg = { };
1183 if (cec_has_log_addr(adap, log_addr))
1186 /* Send poll message */
1188 msg.msg[0] = (log_addr << 4) | log_addr;
1189 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1192 * While trying to poll the physical address was reset
1193 * and the adapter was unconfigured, so bail out.
1195 if (!adap->is_configuring)
1201 if (msg.tx_status & CEC_TX_STATUS_OK)
1205 * Message not acknowledged, so this logical
1206 * address is free to use.
1208 err = adap->ops->adap_log_addr(adap, log_addr);
1212 las->log_addr[idx] = log_addr;
1213 las->log_addr_mask |= 1 << log_addr;
1214 adap->phys_addrs[log_addr] = adap->phys_addr;
1219 * Unconfigure the adapter: clear all logical addresses and send
1220 * the state changed event.
1222 * This function is called with adap->lock held.
1224 static void cec_adap_unconfigure(struct cec_adapter *adap)
1226 if (!adap->needs_hpd ||
1227 adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1228 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1229 adap->log_addrs.log_addr_mask = 0;
1230 adap->is_configuring = false;
1231 adap->is_configured = false;
1232 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1234 wake_up_interruptible(&adap->kthread_waitq);
1235 cec_post_state_event(adap);
1239 * Attempt to claim the required logical addresses.
1241 static int cec_config_thread_func(void *arg)
1243 /* The various LAs for each type of device */
1244 static const u8 tv_log_addrs[] = {
1245 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1246 CEC_LOG_ADDR_INVALID
1248 static const u8 record_log_addrs[] = {
1249 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1250 CEC_LOG_ADDR_RECORD_3,
1251 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1252 CEC_LOG_ADDR_INVALID
1254 static const u8 tuner_log_addrs[] = {
1255 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1256 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1257 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1258 CEC_LOG_ADDR_INVALID
1260 static const u8 playback_log_addrs[] = {
1261 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1262 CEC_LOG_ADDR_PLAYBACK_3,
1263 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1264 CEC_LOG_ADDR_INVALID
1266 static const u8 audiosystem_log_addrs[] = {
1267 CEC_LOG_ADDR_AUDIOSYSTEM,
1268 CEC_LOG_ADDR_INVALID
1270 static const u8 specific_use_log_addrs[] = {
1271 CEC_LOG_ADDR_SPECIFIC,
1272 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1273 CEC_LOG_ADDR_INVALID
1275 static const u8 *type2addrs[6] = {
1276 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1277 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1278 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1279 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1280 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1281 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1283 static const u16 type2mask[] = {
1284 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1285 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1286 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1287 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1288 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1289 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1291 struct cec_adapter *adap = arg;
1292 struct cec_log_addrs *las = &adap->log_addrs;
1296 mutex_lock(&adap->lock);
1297 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1298 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1299 las->log_addr_mask = 0;
1301 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1304 for (i = 0; i < las->num_log_addrs; i++) {
1305 unsigned int type = las->log_addr_type[i];
1310 * The TV functionality can only map to physical address 0.
1311 * For any other address, try the Specific functionality
1312 * instead as per the spec.
1314 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1315 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1317 la_list = type2addrs[type];
1318 last_la = las->log_addr[i];
1319 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1320 if (last_la == CEC_LOG_ADDR_INVALID ||
1321 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1322 !((1 << last_la) & type2mask[type]))
1323 last_la = la_list[0];
1325 err = cec_config_log_addr(adap, i, last_la);
1326 if (err > 0) /* Reused last LA */
1332 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1333 /* Tried this one already, skip it */
1334 if (la_list[j] == last_la)
1336 /* The backup addresses are CEC 2.0 specific */
1337 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1338 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1339 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1342 err = cec_config_log_addr(adap, i, la_list[j]);
1343 if (err == 0) /* LA is in use */
1347 /* Done, claimed an LA */
1351 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1352 dprintk(1, "could not claim LA %d\n", i);
1355 if (adap->log_addrs.log_addr_mask == 0 &&
1356 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1360 if (adap->log_addrs.log_addr_mask == 0) {
1361 /* Fall back to unregistered */
1362 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1363 las->log_addr_mask = 1 << las->log_addr[0];
1364 for (i = 1; i < las->num_log_addrs; i++)
1365 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1367 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1368 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1369 adap->is_configured = true;
1370 adap->is_configuring = false;
1371 cec_post_state_event(adap);
1374 * Now post the Report Features and Report Physical Address broadcast
1375 * messages. Note that these are non-blocking transmits, meaning that
1376 * they are just queued up and once adap->lock is unlocked the main
1377 * thread will kick in and start transmitting these.
1379 * If after this function is done (but before one or more of these
1380 * messages are actually transmitted) the CEC adapter is unconfigured,
1381 * then any remaining messages will be dropped by the main thread.
1383 for (i = 0; i < las->num_log_addrs; i++) {
1384 struct cec_msg msg = {};
1386 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1387 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1390 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1392 /* Report Features must come first according to CEC 2.0 */
1393 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1394 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1395 cec_fill_msg_report_features(adap, &msg, i);
1396 cec_transmit_msg_fh(adap, &msg, NULL, false);
1399 /* Report Physical Address */
1400 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1401 las->primary_device_type[i]);
1402 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1404 cec_phys_addr_exp(adap->phys_addr));
1405 cec_transmit_msg_fh(adap, &msg, NULL, false);
1407 adap->kthread_config = NULL;
1408 complete(&adap->config_completion);
1409 mutex_unlock(&adap->lock);
1413 for (i = 0; i < las->num_log_addrs; i++)
1414 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1415 cec_adap_unconfigure(adap);
1416 adap->kthread_config = NULL;
1417 mutex_unlock(&adap->lock);
1418 complete(&adap->config_completion);
1423 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1424 * logical addresses.
1426 * This function is called with adap->lock held.
1428 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1430 if (WARN_ON(adap->is_configuring || adap->is_configured))
1433 init_completion(&adap->config_completion);
1435 /* Ready to kick off the thread */
1436 adap->is_configuring = true;
1437 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1438 "ceccfg-%s", adap->name);
1439 if (IS_ERR(adap->kthread_config)) {
1440 adap->kthread_config = NULL;
1442 mutex_unlock(&adap->lock);
1443 wait_for_completion(&adap->config_completion);
1444 mutex_lock(&adap->lock);
1448 /* Set a new physical address and send an event notifying userspace of this.
1450 * This function is called with adap->lock held.
1452 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1454 if (phys_addr == adap->phys_addr)
1456 if (phys_addr != CEC_PHYS_ADDR_INVALID && adap->devnode.unregistered)
1459 dprintk(1, "new physical address %x.%x.%x.%x\n",
1460 cec_phys_addr_exp(phys_addr));
1461 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1462 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1463 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1464 cec_post_state_event(adap);
1465 cec_adap_unconfigure(adap);
1466 /* Disabling monitor all mode should always succeed */
1467 if (adap->monitor_all_cnt)
1468 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1469 mutex_lock(&adap->devnode.lock);
1470 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1471 WARN_ON(adap->ops->adap_enable(adap, false));
1472 mutex_unlock(&adap->devnode.lock);
1473 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1477 mutex_lock(&adap->devnode.lock);
1478 adap->last_initiator = 0xff;
1480 if ((adap->needs_hpd || list_empty(&adap->devnode.fhs)) &&
1481 adap->ops->adap_enable(adap, true)) {
1482 mutex_unlock(&adap->devnode.lock);
1486 if (adap->monitor_all_cnt &&
1487 call_op(adap, adap_monitor_all_enable, true)) {
1488 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1489 WARN_ON(adap->ops->adap_enable(adap, false));
1490 mutex_unlock(&adap->devnode.lock);
1493 mutex_unlock(&adap->devnode.lock);
1495 adap->phys_addr = phys_addr;
1496 cec_post_state_event(adap);
1497 if (adap->log_addrs.num_log_addrs)
1498 cec_claim_log_addrs(adap, block);
1501 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1503 if (IS_ERR_OR_NULL(adap))
1506 mutex_lock(&adap->lock);
1507 __cec_s_phys_addr(adap, phys_addr, block);
1508 mutex_unlock(&adap->lock);
1510 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1512 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1513 const struct edid *edid)
1515 u16 pa = CEC_PHYS_ADDR_INVALID;
1517 if (edid && edid->extensions)
1518 pa = cec_get_edid_phys_addr((const u8 *)edid,
1519 EDID_LENGTH * (edid->extensions + 1), NULL);
1520 cec_s_phys_addr(adap, pa, false);
1522 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1525 * Called from either the ioctl or a driver to set the logical addresses.
1527 * This function is called with adap->lock held.
1529 int __cec_s_log_addrs(struct cec_adapter *adap,
1530 struct cec_log_addrs *log_addrs, bool block)
1535 if (adap->devnode.unregistered)
1538 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1539 cec_adap_unconfigure(adap);
1540 adap->log_addrs.num_log_addrs = 0;
1541 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++)
1542 adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID;
1543 adap->log_addrs.osd_name[0] = '\0';
1544 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
1545 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
1549 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1551 * Sanitize log_addrs fields if a CDC-Only device is
1554 log_addrs->num_log_addrs = 1;
1555 log_addrs->osd_name[0] = '\0';
1556 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1557 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1559 * This is just an internal convention since a CDC-Only device
1560 * doesn't have to be a switch. But switches already use
1561 * unregistered, so it makes some kind of sense to pick this
1562 * as the primary device. Since a CDC-Only device never sends
1563 * any 'normal' CEC messages this primary device type is never
1564 * sent over the CEC bus.
1566 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1567 log_addrs->all_device_types[0] = 0;
1568 log_addrs->features[0][0] = 0;
1569 log_addrs->features[0][1] = 0;
1572 /* Ensure the osd name is 0-terminated */
1573 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1576 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1577 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1582 * Vendor ID is a 24 bit number, so check if the value is
1583 * within the correct range.
1585 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1586 (log_addrs->vendor_id & 0xff000000) != 0) {
1587 dprintk(1, "invalid vendor ID\n");
1591 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1592 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1593 dprintk(1, "invalid CEC version\n");
1597 if (log_addrs->num_log_addrs > 1)
1598 for (i = 0; i < log_addrs->num_log_addrs; i++)
1599 if (log_addrs->log_addr_type[i] ==
1600 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1601 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1605 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1606 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1607 u8 *features = log_addrs->features[i];
1608 bool op_is_dev_features = false;
1611 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1612 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1613 dprintk(1, "duplicate logical address type\n");
1616 type_mask |= 1 << log_addrs->log_addr_type[i];
1617 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1618 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1619 /* Record already contains the playback functionality */
1620 dprintk(1, "invalid record + playback combination\n");
1623 if (log_addrs->primary_device_type[i] >
1624 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1625 dprintk(1, "unknown primary device type\n");
1628 if (log_addrs->primary_device_type[i] == 2) {
1629 dprintk(1, "invalid primary device type\n");
1632 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1633 dprintk(1, "unknown logical address type\n");
1636 for (j = 0; j < feature_sz; j++) {
1637 if ((features[j] & 0x80) == 0) {
1638 if (op_is_dev_features)
1640 op_is_dev_features = true;
1643 if (!op_is_dev_features || j == feature_sz) {
1644 dprintk(1, "malformed features\n");
1647 /* Zero unused part of the feature array */
1648 memset(features + j + 1, 0, feature_sz - j - 1);
1651 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1652 if (log_addrs->num_log_addrs > 2) {
1653 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1656 if (log_addrs->num_log_addrs == 2) {
1657 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1658 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1659 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1662 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1663 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1664 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1670 /* Zero unused LAs */
1671 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1672 log_addrs->primary_device_type[i] = 0;
1673 log_addrs->log_addr_type[i] = 0;
1674 log_addrs->all_device_types[i] = 0;
1675 memset(log_addrs->features[i], 0,
1676 sizeof(log_addrs->features[i]));
1679 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1680 adap->log_addrs = *log_addrs;
1681 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1682 cec_claim_log_addrs(adap, block);
1686 int cec_s_log_addrs(struct cec_adapter *adap,
1687 struct cec_log_addrs *log_addrs, bool block)
1691 mutex_lock(&adap->lock);
1692 err = __cec_s_log_addrs(adap, log_addrs, block);
1693 mutex_unlock(&adap->lock);
1696 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1698 /* High-level core CEC message handling */
1700 /* Fill in the Report Features message */
1701 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1702 struct cec_msg *msg,
1703 unsigned int la_idx)
1705 const struct cec_log_addrs *las = &adap->log_addrs;
1706 const u8 *features = las->features[la_idx];
1707 bool op_is_dev_features = false;
1710 /* Report Features */
1711 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1713 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1714 msg->msg[2] = adap->log_addrs.cec_version;
1715 msg->msg[3] = las->all_device_types[la_idx];
1717 /* Write RC Profiles first, then Device Features */
1718 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1719 msg->msg[msg->len++] = features[idx];
1720 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1721 if (op_is_dev_features)
1723 op_is_dev_features = true;
1728 /* Transmit the Feature Abort message */
1729 static int cec_feature_abort_reason(struct cec_adapter *adap,
1730 struct cec_msg *msg, u8 reason)
1732 struct cec_msg tx_msg = { };
1735 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1738 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1740 /* Don't Feature Abort messages from 'Unregistered' */
1741 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1743 cec_msg_set_reply_to(&tx_msg, msg);
1744 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1745 return cec_transmit_msg(adap, &tx_msg, false);
1748 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1750 return cec_feature_abort_reason(adap, msg,
1751 CEC_OP_ABORT_UNRECOGNIZED_OP);
1754 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1756 return cec_feature_abort_reason(adap, msg,
1757 CEC_OP_ABORT_REFUSED);
1761 * Called when a CEC message is received. This function will do any
1762 * necessary core processing. The is_reply bool is true if this message
1763 * is a reply to an earlier transmit.
1765 * The message is either a broadcast message or a valid directed message.
1767 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1770 bool is_broadcast = cec_msg_is_broadcast(msg);
1771 u8 dest_laddr = cec_msg_destination(msg);
1772 u8 init_laddr = cec_msg_initiator(msg);
1773 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1774 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1775 bool from_unregistered = init_laddr == 0xf;
1776 struct cec_msg tx_cec_msg = { };
1778 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1780 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1781 if (cec_is_cdc_only(&adap->log_addrs) &&
1782 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1785 if (adap->ops->received) {
1786 /* Allow drivers to process the message first */
1787 if (adap->ops->received(adap, msg) != -ENOMSG)
1792 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1793 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1794 * handled by the CEC core, even if the passthrough mode is on.
1795 * The others are just ignored if passthrough mode is on.
1797 switch (msg->msg[1]) {
1798 case CEC_MSG_GET_CEC_VERSION:
1800 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1801 case CEC_MSG_GIVE_OSD_NAME:
1803 * These messages reply with a directed message, so ignore if
1804 * the initiator is Unregistered.
1806 if (!adap->passthrough && from_unregistered)
1809 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1810 case CEC_MSG_GIVE_FEATURES:
1811 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1813 * Skip processing these messages if the passthrough mode
1816 if (adap->passthrough)
1817 goto skip_processing;
1818 /* Ignore if addressing is wrong */
1823 case CEC_MSG_USER_CONTROL_PRESSED:
1824 case CEC_MSG_USER_CONTROL_RELEASED:
1825 /* Wrong addressing mode: don't process */
1826 if (is_broadcast || from_unregistered)
1827 goto skip_processing;
1830 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1832 * This message is always processed, regardless of the
1833 * passthrough setting.
1835 * Exception: don't process if wrong addressing mode.
1838 goto skip_processing;
1845 cec_msg_set_reply_to(&tx_cec_msg, msg);
1847 switch (msg->msg[1]) {
1848 /* The following messages are processed but still passed through */
1849 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1850 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1852 if (!from_unregistered)
1853 adap->phys_addrs[init_laddr] = pa;
1854 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
1855 cec_phys_addr_exp(pa), init_laddr);
1859 case CEC_MSG_USER_CONTROL_PRESSED:
1860 if (!(adap->capabilities & CEC_CAP_RC) ||
1861 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1864 #ifdef CONFIG_MEDIA_CEC_RC
1865 switch (msg->msg[2]) {
1867 * Play function, this message can have variable length
1868 * depending on the specific play function that is used.
1872 rc_keydown(adap->rc, RC_PROTO_CEC,
1875 rc_keydown(adap->rc, RC_PROTO_CEC,
1876 msg->msg[2] << 8 | msg->msg[3], 0);
1879 * Other function messages that are not handled.
1880 * Currently the RC framework does not allow to supply an
1881 * additional parameter to a keypress. These "keys" contain
1882 * other information such as channel number, an input number
1884 * For the time being these messages are not processed by the
1885 * framework and are simply forwarded to the user space.
1887 case 0x56: case 0x57:
1888 case 0x67: case 0x68: case 0x69: case 0x6a:
1891 rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0);
1897 case CEC_MSG_USER_CONTROL_RELEASED:
1898 if (!(adap->capabilities & CEC_CAP_RC) ||
1899 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1901 #ifdef CONFIG_MEDIA_CEC_RC
1907 * The remaining messages are only processed if the passthrough mode
1910 case CEC_MSG_GET_CEC_VERSION:
1911 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1912 return cec_transmit_msg(adap, &tx_cec_msg, false);
1914 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1915 /* Do nothing for CEC switches using addr 15 */
1916 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1918 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1919 return cec_transmit_msg(adap, &tx_cec_msg, false);
1921 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1922 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1923 return cec_feature_abort(adap, msg);
1924 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1925 return cec_transmit_msg(adap, &tx_cec_msg, false);
1928 /* Do nothing for CEC switches */
1929 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1931 return cec_feature_refused(adap, msg);
1933 case CEC_MSG_GIVE_OSD_NAME: {
1934 if (adap->log_addrs.osd_name[0] == 0)
1935 return cec_feature_abort(adap, msg);
1936 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1937 return cec_transmit_msg(adap, &tx_cec_msg, false);
1940 case CEC_MSG_GIVE_FEATURES:
1941 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1942 return cec_feature_abort(adap, msg);
1943 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1944 return cec_transmit_msg(adap, &tx_cec_msg, false);
1948 * Unprocessed messages are aborted if userspace isn't doing
1949 * any processing either.
1951 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
1952 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1953 return cec_feature_abort(adap, msg);
1958 /* If this was a reply, then we're done, unless otherwise specified */
1959 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
1963 * Send to the exclusive follower if there is one, otherwise send
1966 if (adap->cec_follower)
1967 cec_queue_msg_fh(adap->cec_follower, msg);
1969 cec_queue_msg_followers(adap, msg);
1974 * Helper functions to keep track of the 'monitor all' use count.
1976 * These functions are called with adap->lock held.
1978 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1982 if (adap->monitor_all_cnt == 0)
1983 ret = call_op(adap, adap_monitor_all_enable, 1);
1985 adap->monitor_all_cnt++;
1989 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1991 adap->monitor_all_cnt--;
1992 if (adap->monitor_all_cnt == 0)
1993 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1997 * Helper functions to keep track of the 'monitor pin' use count.
1999 * These functions are called with adap->lock held.
2001 int cec_monitor_pin_cnt_inc(struct cec_adapter *adap)
2005 if (adap->monitor_pin_cnt == 0)
2006 ret = call_op(adap, adap_monitor_pin_enable, 1);
2008 adap->monitor_pin_cnt++;
2012 void cec_monitor_pin_cnt_dec(struct cec_adapter *adap)
2014 adap->monitor_pin_cnt--;
2015 if (adap->monitor_pin_cnt == 0)
2016 WARN_ON(call_op(adap, adap_monitor_pin_enable, 0));
2019 #ifdef CONFIG_DEBUG_FS
2021 * Log the current state of the CEC adapter.
2022 * Very useful for debugging.
2024 int cec_adap_status(struct seq_file *file, void *priv)
2026 struct cec_adapter *adap = dev_get_drvdata(file->private);
2027 struct cec_data *data;
2029 mutex_lock(&adap->lock);
2030 seq_printf(file, "configured: %d\n", adap->is_configured);
2031 seq_printf(file, "configuring: %d\n", adap->is_configuring);
2032 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
2033 cec_phys_addr_exp(adap->phys_addr));
2034 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
2035 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
2036 if (adap->cec_follower)
2037 seq_printf(file, "has CEC follower%s\n",
2038 adap->passthrough ? " (in passthrough mode)" : "");
2039 if (adap->cec_initiator)
2040 seq_puts(file, "has CEC initiator\n");
2041 if (adap->monitor_all_cnt)
2042 seq_printf(file, "file handles in Monitor All mode: %u\n",
2043 adap->monitor_all_cnt);
2044 if (adap->tx_timeouts) {
2045 seq_printf(file, "transmit timeouts: %u\n",
2047 adap->tx_timeouts = 0;
2049 data = adap->transmitting;
2051 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
2052 data->msg.len, data->msg.msg, data->msg.reply,
2054 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
2055 list_for_each_entry(data, &adap->transmit_queue, list) {
2056 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
2057 data->msg.len, data->msg.msg, data->msg.reply,
2060 list_for_each_entry(data, &adap->wait_queue, list) {
2061 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
2062 data->msg.len, data->msg.msg, data->msg.reply,
2066 call_void_op(adap, adap_status, file);
2067 mutex_unlock(&adap->lock);