Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
4 *
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
7
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>
18
19#include <drm/drm_edid.h>
20
21#include "cec-priv.h"
22
23static void cec_fill_msg_report_features(struct cec_adapter *adap,
24 struct cec_msg *msg,
25 unsigned int la_idx);
26
27/*
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.
33 *
34 * This is a sign that something it really wrong and a warning
35 * will be issued.
36 */
37#define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
38
39#define call_op(adap, op, arg...) \
40 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
41
42#define call_void_op(adap, op, arg...) \
43 do { \
44 if (adap->ops->op) \
45 adap->ops->op(adap, ## arg); \
46 } while (0)
47
48static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
49{
50 int i;
51
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;
56}
57
58static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
59{
60 int i = cec_log_addr2idx(adap, log_addr);
61
62 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
63}
64
65u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
66 unsigned int *offset)
67{
68 unsigned int loc = cec_get_edid_spa_location(edid, size);
69
70 if (offset)
71 *offset = loc;
72 if (loc == 0)
73 return CEC_PHYS_ADDR_INVALID;
74 return (edid[loc] << 8) | edid[loc + 1];
75}
76EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
77
78/*
79 * Queue a new event for this filehandle. If ts == 0, then set it
80 * to the current time.
81 *
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.
85 */
86void cec_queue_event_fh(struct cec_fh *fh,
87 const struct cec_event *new_ev, u64 ts)
88{
89 static const u16 max_events[CEC_NUM_EVENTS] = {
90 1, 1, 800, 800, 8, 8, 8, 8
91 };
92 struct cec_event_entry *entry;
93 unsigned int ev_idx = new_ev->event - 1;
94
95 if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
96 return;
97
98 if (ts == 0)
99 ts = ktime_get_ns();
100
101 mutex_lock(&fh->lock);
102 if (ev_idx < CEC_NUM_CORE_EVENTS)
103 entry = &fh->core_events[ev_idx];
104 else
105 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
106 if (entry) {
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;
111 goto unlock;
112 }
113 entry->ev = *new_ev;
114 entry->ev.ts = ts;
115
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++;
121 goto unlock;
122 }
123
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);
130 kfree(entry);
131 }
132 }
133 /* Mark that events were lost */
134 entry = list_first_entry_or_null(&fh->events[ev_idx],
135 struct cec_event_entry, list);
136 if (entry)
137 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
138
139unlock:
140 mutex_unlock(&fh->lock);
141 wake_up_interruptible(&fh->wait);
142}
143
144/* Queue a new event for all open filehandles. */
145static void cec_queue_event(struct cec_adapter *adap,
146 const struct cec_event *ev)
147{
148 u64 ts = ktime_get_ns();
149 struct cec_fh *fh;
150
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);
155}
156
157/* Notify userspace that the CEC pin changed state at the given time. */
158void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
159 bool dropped_events, ktime_t ts)
160{
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,
165 };
166 struct cec_fh *fh;
167
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);
173}
174EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event);
175
176/* Notify userspace that the HPD pin changed state at the given time. */
177void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
178{
179 struct cec_event ev = {
180 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH :
181 CEC_EVENT_PIN_HPD_LOW,
182 };
183 struct cec_fh *fh;
184
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);
189}
190EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event);
191
192/* Notify userspace that the 5V pin changed state at the given time. */
193void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
194{
195 struct cec_event ev = {
196 .event = is_high ? CEC_EVENT_PIN_5V_HIGH :
197 CEC_EVENT_PIN_5V_LOW,
198 };
199 struct cec_fh *fh;
200
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);
205}
206EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event);
207
208/*
209 * Queue a new message for this filehandle.
210 *
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.
214 */
215static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
216{
217 static const struct cec_event ev_lost_msgs = {
218 .event = CEC_EVENT_LOST_MSGS,
219 .flags = 0,
220 {
221 .lost_msgs = { 1 },
222 },
223 };
224 struct cec_msg_entry *entry;
225
226 mutex_lock(&fh->lock);
227 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
228 if (entry) {
229 entry->msg = *msg;
230 /* Add new msg at the end of the queue */
231 list_add_tail(&entry->list, &fh->msgs);
232
233 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
234 /* All is fine if there is enough room */
235 fh->queued_msgs++;
236 mutex_unlock(&fh->lock);
237 wake_up_interruptible(&fh->wait);
238 return;
239 }
240
241 /*
242 * if the message queue is full, then drop the oldest one and
243 * send a lost message event.
244 */
245 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
246 list_del(&entry->list);
247 kfree(entry);
248 }
249 mutex_unlock(&fh->lock);
250
251 /*
252 * We lost a message, either because kmalloc failed or the queue
253 * was full.
254 */
255 cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
256}
257
258/*
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.
264 *
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.
267 */
268static void cec_queue_msg_monitor(struct cec_adapter *adap,
269 const struct cec_msg *msg,
270 bool valid_la)
271{
272 struct cec_fh *fh;
273 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
274 CEC_MODE_MONITOR_ALL;
275
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);
280 }
281 mutex_unlock(&adap->devnode.lock);
282}
283
284/*
285 * Queue the message for follower filehandles.
286 */
287static void cec_queue_msg_followers(struct cec_adapter *adap,
288 const struct cec_msg *msg)
289{
290 struct cec_fh *fh;
291
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);
296 }
297 mutex_unlock(&adap->devnode.lock);
298}
299
300/* Notify userspace of an adapter state change. */
301static void cec_post_state_event(struct cec_adapter *adap)
302{
303 struct cec_event ev = {
304 .event = CEC_EVENT_STATE_CHANGE,
305 };
306
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);
310}
311
312/*
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.
316 *
317 * This function is called with adap->lock held.
318 */
319static void cec_data_completed(struct cec_data *data)
320{
321 /*
322 * Delete this transmit from the filehandle's xfer_list since
323 * we're done with it.
324 *
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.
328 */
329 if (data->fh)
330 list_del(&data->xfer_list);
331
332 if (data->blocking) {
333 /*
334 * Someone is blocking so mark the message as completed
335 * and call complete.
336 */
337 data->completed = true;
338 complete(&data->c);
339 } else {
340 /*
341 * No blocking, so just queue the message if needed and
342 * free the memory.
343 */
344 if (data->fh)
345 cec_queue_msg_fh(data->fh, &data->msg);
346 kfree(data);
347 }
348}
349
350/*
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
353 * finish.
354 *
355 * This function is called with adap->lock held.
356 */
357static void cec_data_cancel(struct cec_data *data, u8 tx_status)
358{
359 /*
360 * It's either the current transmit, or it is a pending
361 * transmit. Take the appropriate action to clear it.
362 */
363 if (data->adap->transmitting == data) {
364 data->adap->transmitting = NULL;
365 } else {
366 list_del_init(&data->list);
367 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
368 data->adap->transmit_queue_sz--;
369 }
370
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;
374 } else {
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++;
379 data->attempts = 0;
380 }
381
382 /* Queue transmitted message for monitoring purposes */
383 cec_queue_msg_monitor(data->adap, &data->msg, 1);
384
385 cec_data_completed(data);
386}
387
388/*
389 * Flush all pending transmits and cancel any pending timeout work.
390 *
391 * This function is called with adap->lock held.
392 */
393static void cec_flush(struct cec_adapter *adap)
394{
395 struct cec_data *data, *n;
396
397 /*
398 * If the adapter is disabled, or we're asked to stop,
399 * then cancel any pending transmits.
400 */
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);
405 }
406 if (adap->transmitting)
407 cec_data_cancel(adap->transmitting, CEC_TX_STATUS_ABORTED);
408
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);
413 /*
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.
418 */
419 }
420}
421
422/*
423 * Main CEC state machine
424 *
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().
429 *
430 * If the adapter is disabled, then remove all queued messages instead.
431 *
432 * If the current transmit times out, then cancel that transmit.
433 */
434int cec_thread_func(void *_adap)
435{
436 struct cec_adapter *adap = _adap;
437
438 for (;;) {
439 unsigned int signal_free_time;
440 struct cec_data *data;
441 bool timeout = false;
442 u8 attempts;
443
444 if (adap->transmitting) {
445 int err;
446
447 /*
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.
453 */
454 err = wait_event_interruptible_timeout(adap->kthread_waitq,
455 (adap->needs_hpd &&
456 (!adap->is_configured && !adap->is_configuring)) ||
457 kthread_should_stop() ||
458 (!adap->transmit_in_progress &&
459 !list_empty(&adap->transmit_queue)),
460 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
461 timeout = err == 0;
462 } else {
463 /* Otherwise we just wait for something to happen. */
464 wait_event_interruptible(adap->kthread_waitq,
465 kthread_should_stop() ||
466 (!adap->transmit_in_progress &&
467 !list_empty(&adap->transmit_queue)));
468 }
469
470 mutex_lock(&adap->lock);
471
472 if ((adap->needs_hpd &&
473 (!adap->is_configured && !adap->is_configuring)) ||
474 kthread_should_stop()) {
475 cec_flush(adap);
476 goto unlock;
477 }
478
479 if (adap->transmitting && timeout) {
480 /*
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).
487 */
488 pr_warn("cec-%s: message %*ph timed out\n", adap->name,
489 adap->transmitting->msg.len,
490 adap->transmitting->msg.msg);
491 adap->transmit_in_progress = false;
492 adap->tx_timeouts++;
493 /* Just give up on this. */
494 cec_data_cancel(adap->transmitting,
495 CEC_TX_STATUS_TIMEOUT);
496 goto unlock;
497 }
498
499 /*
500 * If we are still transmitting, or there is nothing new to
501 * transmit, then just continue waiting.
502 */
503 if (adap->transmit_in_progress || list_empty(&adap->transmit_queue))
504 goto unlock;
505
506 /* Get a new message to transmit */
507 data = list_first_entry(&adap->transmit_queue,
508 struct cec_data, list);
509 list_del_init(&data->list);
510 adap->transmit_queue_sz--;
511
512 /* Make this the current transmitting message */
513 adap->transmitting = data;
514
515 /*
516 * Suggested number of attempts as per the CEC 2.0 spec:
517 * 4 attempts is the default, except for 'secondary poll
518 * messages', i.e. poll messages not sent during the adapter
519 * configuration phase when it allocates logical addresses.
520 */
521 if (data->msg.len == 1 && adap->is_configured)
522 attempts = 2;
523 else
524 attempts = 4;
525
526 /* Set the suggested signal free time */
527 if (data->attempts) {
528 /* should be >= 3 data bit periods for a retry */
529 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
530 } else if (adap->last_initiator !=
531 cec_msg_initiator(&data->msg)) {
532 /* should be >= 5 data bit periods for new initiator */
533 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
534 adap->last_initiator = cec_msg_initiator(&data->msg);
535 } else {
536 /*
537 * should be >= 7 data bit periods for sending another
538 * frame immediately after another.
539 */
540 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
541 }
542 if (data->attempts == 0)
543 data->attempts = attempts;
544
545 /* Tell the adapter to transmit, cancel on error */
546 if (adap->ops->adap_transmit(adap, data->attempts,
547 signal_free_time, &data->msg))
548 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
549 else
550 adap->transmit_in_progress = true;
551
552unlock:
553 mutex_unlock(&adap->lock);
554
555 if (kthread_should_stop())
556 break;
557 }
558 return 0;
559}
560
561/*
562 * Called by the CEC adapter if a transmit finished.
563 */
564void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
565 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
566 u8 error_cnt, ktime_t ts)
567{
568 struct cec_data *data;
569 struct cec_msg *msg;
570 unsigned int attempts_made = arb_lost_cnt + nack_cnt +
571 low_drive_cnt + error_cnt;
572
573 dprintk(2, "%s: status 0x%02x\n", __func__, status);
574 if (attempts_made < 1)
575 attempts_made = 1;
576
577 mutex_lock(&adap->lock);
578 data = adap->transmitting;
579 if (!data) {
580 /*
581 * This might happen if a transmit was issued and the cable is
582 * unplugged while the transmit is ongoing. Ignore this
583 * transmit in that case.
584 */
585 if (!adap->transmit_in_progress)
586 dprintk(1, "%s was called without an ongoing transmit!\n",
587 __func__);
588 adap->transmit_in_progress = false;
589 goto wake_thread;
590 }
591 adap->transmit_in_progress = false;
592
593 msg = &data->msg;
594
595 /* Drivers must fill in the status! */
596 WARN_ON(status == 0);
597 msg->tx_ts = ktime_to_ns(ts);
598 msg->tx_status |= status;
599 msg->tx_arb_lost_cnt += arb_lost_cnt;
600 msg->tx_nack_cnt += nack_cnt;
601 msg->tx_low_drive_cnt += low_drive_cnt;
602 msg->tx_error_cnt += error_cnt;
603
604 /* Mark that we're done with this transmit */
605 adap->transmitting = NULL;
606
607 /*
608 * If there are still retry attempts left and there was an error and
609 * the hardware didn't signal that it retried itself (by setting
610 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
611 */
612 if (data->attempts > attempts_made &&
613 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
614 /* Retry this message */
615 data->attempts -= attempts_made;
616 if (msg->timeout)
617 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
618 msg->len, msg->msg, data->attempts, msg->reply);
619 else
620 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
621 msg->len, msg->msg, data->attempts);
622 /* Add the message in front of the transmit queue */
623 list_add(&data->list, &adap->transmit_queue);
624 adap->transmit_queue_sz++;
625 goto wake_thread;
626 }
627
628 data->attempts = 0;
629
630 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
631 if (!(status & CEC_TX_STATUS_OK))
632 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
633
634 /* Queue transmitted message for monitoring purposes */
635 cec_queue_msg_monitor(adap, msg, 1);
636
637 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
638 msg->timeout) {
639 /*
640 * Queue the message into the wait queue if we want to wait
641 * for a reply.
642 */
643 list_add_tail(&data->list, &adap->wait_queue);
644 schedule_delayed_work(&data->work,
645 msecs_to_jiffies(msg->timeout));
646 } else {
647 /* Otherwise we're done */
648 cec_data_completed(data);
649 }
650
651wake_thread:
652 /*
653 * Wake up the main thread to see if another message is ready
654 * for transmitting or to retry the current message.
655 */
656 wake_up_interruptible(&adap->kthread_waitq);
657 mutex_unlock(&adap->lock);
658}
659EXPORT_SYMBOL_GPL(cec_transmit_done_ts);
660
661void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
662 u8 status, ktime_t ts)
663{
664 switch (status & ~CEC_TX_STATUS_MAX_RETRIES) {
665 case CEC_TX_STATUS_OK:
666 cec_transmit_done_ts(adap, status, 0, 0, 0, 0, ts);
667 return;
668 case CEC_TX_STATUS_ARB_LOST:
669 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts);
670 return;
671 case CEC_TX_STATUS_NACK:
672 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts);
673 return;
674 case CEC_TX_STATUS_LOW_DRIVE:
675 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts);
676 return;
677 case CEC_TX_STATUS_ERROR:
678 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts);
679 return;
680 default:
681 /* Should never happen */
682 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
683 return;
684 }
685}
686EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts);
687
688/*
689 * Called when waiting for a reply times out.
690 */
691static void cec_wait_timeout(struct work_struct *work)
692{
693 struct cec_data *data = container_of(work, struct cec_data, work.work);
694 struct cec_adapter *adap = data->adap;
695
696 mutex_lock(&adap->lock);
697 /*
698 * Sanity check in case the timeout and the arrival of the message
699 * happened at the same time.
700 */
701 if (list_empty(&data->list))
702 goto unlock;
703
704 /* Mark the message as timed out */
705 list_del_init(&data->list);
706 data->msg.rx_ts = ktime_get_ns();
707 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
708 cec_data_completed(data);
709unlock:
710 mutex_unlock(&adap->lock);
711}
712
713/*
714 * Transmit a message. The fh argument may be NULL if the transmit is not
715 * associated with a specific filehandle.
716 *
717 * This function is called with adap->lock held.
718 */
719int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
720 struct cec_fh *fh, bool block)
721{
722 struct cec_data *data;
723
724 msg->rx_ts = 0;
725 msg->tx_ts = 0;
726 msg->rx_status = 0;
727 msg->tx_status = 0;
728 msg->tx_arb_lost_cnt = 0;
729 msg->tx_nack_cnt = 0;
730 msg->tx_low_drive_cnt = 0;
731 msg->tx_error_cnt = 0;
732 msg->sequence = 0;
733
734 if (msg->reply && msg->timeout == 0) {
735 /* Make sure the timeout isn't 0. */
736 msg->timeout = 1000;
737 }
738 if (msg->timeout)
739 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
740 else
741 msg->flags = 0;
742
743 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
744 msg->msg[2] = adap->phys_addr >> 8;
745 msg->msg[3] = adap->phys_addr & 0xff;
746 }
747
748 /* Sanity checks */
749 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
750 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
751 return -EINVAL;
752 }
753
754 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
755
756 if (msg->timeout)
757 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
758 __func__, msg->len, msg->msg, msg->reply,
759 !block ? ", nb" : "");
760 else
761 dprintk(2, "%s: %*ph%s\n",
762 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
763
764 if (msg->timeout && msg->len == 1) {
765 dprintk(1, "%s: can't reply to poll msg\n", __func__);
766 return -EINVAL;
767 }
768 if (msg->len == 1) {
769 if (cec_msg_destination(msg) == 0xf) {
770 dprintk(1, "%s: invalid poll message\n", __func__);
771 return -EINVAL;
772 }
773 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
774 /*
775 * If the destination is a logical address our adapter
776 * has already claimed, then just NACK this.
777 * It depends on the hardware what it will do with a
778 * POLL to itself (some OK this), so it is just as
779 * easy to handle it here so the behavior will be
780 * consistent.
781 */
782 msg->tx_ts = ktime_get_ns();
783 msg->tx_status = CEC_TX_STATUS_NACK |
784 CEC_TX_STATUS_MAX_RETRIES;
785 msg->tx_nack_cnt = 1;
786 msg->sequence = ++adap->sequence;
787 if (!msg->sequence)
788 msg->sequence = ++adap->sequence;
789 return 0;
790 }
791 }
792 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
793 cec_has_log_addr(adap, cec_msg_destination(msg))) {
794 dprintk(1, "%s: destination is the adapter itself\n", __func__);
795 return -EINVAL;
796 }
797 if (msg->len > 1 && adap->is_configured &&
798 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
799 dprintk(1, "%s: initiator has unknown logical address %d\n",
800 __func__, cec_msg_initiator(msg));
801 return -EINVAL;
802 }
803 if (!adap->is_configured && !adap->is_configuring) {
804 if (adap->needs_hpd || msg->msg[0] != 0xf0) {
805 dprintk(1, "%s: adapter is unconfigured\n", __func__);
806 return -ENONET;
807 }
808 if (msg->reply) {
809 dprintk(1, "%s: invalid msg->reply\n", __func__);
810 return -EINVAL;
811 }
812 }
813
814 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
815 dprintk(2, "%s: transmit queue full\n", __func__);
816 return -EBUSY;
817 }
818
819 data = kzalloc(sizeof(*data), GFP_KERNEL);
820 if (!data)
821 return -ENOMEM;
822
823 msg->sequence = ++adap->sequence;
824 if (!msg->sequence)
825 msg->sequence = ++adap->sequence;
826
827 data->msg = *msg;
828 data->fh = fh;
829 data->adap = adap;
830 data->blocking = block;
831
832 init_completion(&data->c);
833 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
834
835 if (fh)
836 list_add_tail(&data->xfer_list, &fh->xfer_list);
837
838 list_add_tail(&data->list, &adap->transmit_queue);
839 adap->transmit_queue_sz++;
840 if (!adap->transmitting)
841 wake_up_interruptible(&adap->kthread_waitq);
842
843 /* All done if we don't need to block waiting for completion */
844 if (!block)
845 return 0;
846
847 /*
848 * Release the lock and wait, retake the lock afterwards.
849 */
850 mutex_unlock(&adap->lock);
851 wait_for_completion_killable(&data->c);
852 if (!data->completed)
853 cancel_delayed_work_sync(&data->work);
854 mutex_lock(&adap->lock);
855
856 /* Cancel the transmit if it was interrupted */
857 if (!data->completed)
858 cec_data_cancel(data, CEC_TX_STATUS_ABORTED);
859
860 /* The transmit completed (possibly with an error) */
861 *msg = data->msg;
862 kfree(data);
863 return 0;
864}
865
866/* Helper function to be used by drivers and this framework. */
867int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
868 bool block)
869{
870 int ret;
871
872 mutex_lock(&adap->lock);
873 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
874 mutex_unlock(&adap->lock);
875 return ret;
876}
877EXPORT_SYMBOL_GPL(cec_transmit_msg);
878
879/*
880 * I don't like forward references but without this the low-level
881 * cec_received_msg() function would come after a bunch of high-level
882 * CEC protocol handling functions. That was very confusing.
883 */
884static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
885 bool is_reply);
886
887#define DIRECTED 0x80
888#define BCAST1_4 0x40
889#define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
890#define BCAST (BCAST1_4 | BCAST2_0)
891#define BOTH (BCAST | DIRECTED)
892
893/*
894 * Specify minimum length and whether the message is directed, broadcast
895 * or both. Messages that do not match the criteria are ignored as per
896 * the CEC specification.
897 */
898static const u8 cec_msg_size[256] = {
899 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
900 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
901 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
902 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
903 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
904 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
905 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
906 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
907 [CEC_MSG_STANDBY] = 2 | BOTH,
908 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
909 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
910 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
911 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
912 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
913 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
914 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
915 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
916 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
917 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
918 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
919 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
920 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
921 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
922 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
923 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
924 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
925 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
926 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
927 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
928 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
929 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
930 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
931 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
932 [CEC_MSG_PLAY] = 3 | DIRECTED,
933 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
934 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
935 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
936 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
937 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
938 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
939 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
940 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
941 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
942 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
943 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
944 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
945 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
946 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
947 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
948 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
949 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
950 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
951 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
952 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
953 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
954 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
955 [CEC_MSG_ABORT] = 2 | DIRECTED,
956 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
957 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
958 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
959 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
960 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
961 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
962 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
963 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
964 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
965 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
966 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
967 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
968 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
969 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
970 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
971 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
972 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
973 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
974};
975
976/* Called by the CEC adapter if a message is received */
977void cec_received_msg_ts(struct cec_adapter *adap,
978 struct cec_msg *msg, ktime_t ts)
979{
980 struct cec_data *data;
981 u8 msg_init = cec_msg_initiator(msg);
982 u8 msg_dest = cec_msg_destination(msg);
983 u8 cmd = msg->msg[1];
984 bool is_reply = false;
985 bool valid_la = true;
986 u8 min_len = 0;
987
988 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
989 return;
990
991 /*
992 * Some CEC adapters will receive the messages that they transmitted.
993 * This test filters out those messages by checking if we are the
994 * initiator, and just returning in that case.
995 *
996 * Note that this won't work if this is an Unregistered device.
997 *
998 * It is bad practice if the hardware receives the message that it
999 * transmitted and luckily most CEC adapters behave correctly in this
1000 * respect.
1001 */
1002 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
1003 cec_has_log_addr(adap, msg_init))
1004 return;
1005
1006 msg->rx_ts = ktime_to_ns(ts);
1007 msg->rx_status = CEC_RX_STATUS_OK;
1008 msg->sequence = msg->reply = msg->timeout = 0;
1009 msg->tx_status = 0;
1010 msg->tx_ts = 0;
1011 msg->tx_arb_lost_cnt = 0;
1012 msg->tx_nack_cnt = 0;
1013 msg->tx_low_drive_cnt = 0;
1014 msg->tx_error_cnt = 0;
1015 msg->flags = 0;
1016 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
1017
1018 mutex_lock(&adap->lock);
1019 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1020
1021 adap->last_initiator = 0xff;
1022
1023 /* Check if this message was for us (directed or broadcast). */
1024 if (!cec_msg_is_broadcast(msg))
1025 valid_la = cec_has_log_addr(adap, msg_dest);
1026
1027 /*
1028 * Check if the length is not too short or if the message is a
1029 * broadcast message where a directed message was expected or
1030 * vice versa. If so, then the message has to be ignored (according
1031 * to section CEC 7.3 and CEC 12.2).
1032 */
1033 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
1034 u8 dir_fl = cec_msg_size[cmd] & BOTH;
1035
1036 min_len = cec_msg_size[cmd] & 0x1f;
1037 if (msg->len < min_len)
1038 valid_la = false;
1039 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
1040 valid_la = false;
1041 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
1042 valid_la = false;
1043 else if (cec_msg_is_broadcast(msg) &&
1044 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
1045 !(dir_fl & BCAST2_0))
1046 valid_la = false;
1047 }
1048 if (valid_la && min_len) {
1049 /* These messages have special length requirements */
1050 switch (cmd) {
1051 case CEC_MSG_TIMER_STATUS:
1052 if (msg->msg[2] & 0x10) {
1053 switch (msg->msg[2] & 0xf) {
1054 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
1055 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
1056 if (msg->len < 5)
1057 valid_la = false;
1058 break;
1059 }
1060 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
1061 if (msg->len < 5)
1062 valid_la = false;
1063 }
1064 break;
1065 case CEC_MSG_RECORD_ON:
1066 switch (msg->msg[2]) {
1067 case CEC_OP_RECORD_SRC_OWN:
1068 break;
1069 case CEC_OP_RECORD_SRC_DIGITAL:
1070 if (msg->len < 10)
1071 valid_la = false;
1072 break;
1073 case CEC_OP_RECORD_SRC_ANALOG:
1074 if (msg->len < 7)
1075 valid_la = false;
1076 break;
1077 case CEC_OP_RECORD_SRC_EXT_PLUG:
1078 if (msg->len < 4)
1079 valid_la = false;
1080 break;
1081 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1082 if (msg->len < 5)
1083 valid_la = false;
1084 break;
1085 }
1086 break;
1087 }
1088 }
1089
1090 /* It's a valid message and not a poll or CDC message */
1091 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1092 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1093
1094 /* The aborted command is in msg[2] */
1095 if (abort)
1096 cmd = msg->msg[2];
1097
1098 /*
1099 * Walk over all transmitted messages that are waiting for a
1100 * reply.
1101 */
1102 list_for_each_entry(data, &adap->wait_queue, list) {
1103 struct cec_msg *dst = &data->msg;
1104
1105 /*
1106 * The *only* CEC message that has two possible replies
1107 * is CEC_MSG_INITIATE_ARC.
1108 * In this case allow either of the two replies.
1109 */
1110 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1111 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1112 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1113 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1114 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1115 dst->reply = cmd;
1116
1117 /* Does the command match? */
1118 if ((abort && cmd != dst->msg[1]) ||
1119 (!abort && cmd != dst->reply))
1120 continue;
1121
1122 /* Does the addressing match? */
1123 if (msg_init != cec_msg_destination(dst) &&
1124 !cec_msg_is_broadcast(dst))
1125 continue;
1126
1127 /* We got a reply */
1128 memcpy(dst->msg, msg->msg, msg->len);
1129 dst->len = msg->len;
1130 dst->rx_ts = msg->rx_ts;
1131 dst->rx_status = msg->rx_status;
1132 if (abort)
1133 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1134 msg->flags = dst->flags;
1135 /* Remove it from the wait_queue */
1136 list_del_init(&data->list);
1137
1138 /* Cancel the pending timeout work */
1139 if (!cancel_delayed_work(&data->work)) {
1140 mutex_unlock(&adap->lock);
1141 flush_scheduled_work();
1142 mutex_lock(&adap->lock);
1143 }
1144 /*
1145 * Mark this as a reply, provided someone is still
1146 * waiting for the answer.
1147 */
1148 if (data->fh)
1149 is_reply = true;
1150 cec_data_completed(data);
1151 break;
1152 }
1153 }
1154 mutex_unlock(&adap->lock);
1155
1156 /* Pass the message on to any monitoring filehandles */
1157 cec_queue_msg_monitor(adap, msg, valid_la);
1158
1159 /* We're done if it is not for us or a poll message */
1160 if (!valid_la || msg->len <= 1)
1161 return;
1162
1163 if (adap->log_addrs.log_addr_mask == 0)
1164 return;
1165
1166 /*
1167 * Process the message on the protocol level. If is_reply is true,
1168 * then cec_receive_notify() won't pass on the reply to the listener(s)
1169 * since that was already done by cec_data_completed() above.
1170 */
1171 cec_receive_notify(adap, msg, is_reply);
1172}
1173EXPORT_SYMBOL_GPL(cec_received_msg_ts);
1174
1175/* Logical Address Handling */
1176
1177/*
1178 * Attempt to claim a specific logical address.
1179 *
1180 * This function is called with adap->lock held.
1181 */
1182static int cec_config_log_addr(struct cec_adapter *adap,
1183 unsigned int idx,
1184 unsigned int log_addr)
1185{
1186 struct cec_log_addrs *las = &adap->log_addrs;
1187 struct cec_msg msg = { };
1188 const unsigned int max_retries = 2;
1189 unsigned int i;
1190 int err;
1191
1192 if (cec_has_log_addr(adap, log_addr))
1193 return 0;
1194
1195 /* Send poll message */
1196 msg.len = 1;
1197 msg.msg[0] = (log_addr << 4) | log_addr;
1198
1199 for (i = 0; i < max_retries; i++) {
1200 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1201
1202 /*
1203 * While trying to poll the physical address was reset
1204 * and the adapter was unconfigured, so bail out.
1205 */
1206 if (!adap->is_configuring)
1207 return -EINTR;
1208
1209 if (err)
1210 return err;
1211
1212 /*
1213 * The message was aborted due to a disconnect or
1214 * unconfigure, just bail out.
1215 */
1216 if (msg.tx_status & CEC_TX_STATUS_ABORTED)
1217 return -EINTR;
1218 if (msg.tx_status & CEC_TX_STATUS_OK)
1219 return 0;
1220 if (msg.tx_status & CEC_TX_STATUS_NACK)
1221 break;
1222 /*
1223 * Retry up to max_retries times if the message was neither
1224 * OKed or NACKed. This can happen due to e.g. a Lost
1225 * Arbitration condition.
1226 */
1227 }
1228
1229 /*
1230 * If we are unable to get an OK or a NACK after max_retries attempts
1231 * (and note that each attempt already consists of four polls), then
1232 * then we assume that something is really weird and that it is not a
1233 * good idea to try and claim this logical address.
1234 */
1235 if (i == max_retries)
1236 return 0;
1237
1238 /*
1239 * Message not acknowledged, so this logical
1240 * address is free to use.
1241 */
1242 err = adap->ops->adap_log_addr(adap, log_addr);
1243 if (err)
1244 return err;
1245
1246 las->log_addr[idx] = log_addr;
1247 las->log_addr_mask |= 1 << log_addr;
1248 adap->phys_addrs[log_addr] = adap->phys_addr;
1249 return 1;
1250}
1251
1252/*
1253 * Unconfigure the adapter: clear all logical addresses and send
1254 * the state changed event.
1255 *
1256 * This function is called with adap->lock held.
1257 */
1258static void cec_adap_unconfigure(struct cec_adapter *adap)
1259{
1260 if (!adap->needs_hpd ||
1261 adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1262 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1263 adap->log_addrs.log_addr_mask = 0;
1264 adap->is_configuring = false;
1265 adap->is_configured = false;
1266 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1267 cec_flush(adap);
1268 wake_up_interruptible(&adap->kthread_waitq);
1269 cec_post_state_event(adap);
1270}
1271
1272/*
1273 * Attempt to claim the required logical addresses.
1274 */
1275static int cec_config_thread_func(void *arg)
1276{
1277 /* The various LAs for each type of device */
1278 static const u8 tv_log_addrs[] = {
1279 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1280 CEC_LOG_ADDR_INVALID
1281 };
1282 static const u8 record_log_addrs[] = {
1283 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1284 CEC_LOG_ADDR_RECORD_3,
1285 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1286 CEC_LOG_ADDR_INVALID
1287 };
1288 static const u8 tuner_log_addrs[] = {
1289 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1290 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1291 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1292 CEC_LOG_ADDR_INVALID
1293 };
1294 static const u8 playback_log_addrs[] = {
1295 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1296 CEC_LOG_ADDR_PLAYBACK_3,
1297 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1298 CEC_LOG_ADDR_INVALID
1299 };
1300 static const u8 audiosystem_log_addrs[] = {
1301 CEC_LOG_ADDR_AUDIOSYSTEM,
1302 CEC_LOG_ADDR_INVALID
1303 };
1304 static const u8 specific_use_log_addrs[] = {
1305 CEC_LOG_ADDR_SPECIFIC,
1306 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1307 CEC_LOG_ADDR_INVALID
1308 };
1309 static const u8 *type2addrs[6] = {
1310 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1311 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1312 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1313 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1314 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1315 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1316 };
1317 static const u16 type2mask[] = {
1318 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1319 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1320 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1321 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1322 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1323 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1324 };
1325 struct cec_adapter *adap = arg;
1326 struct cec_log_addrs *las = &adap->log_addrs;
1327 int err;
1328 int i, j;
1329
1330 mutex_lock(&adap->lock);
1331 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1332 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1333 las->log_addr_mask = 0;
1334
1335 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1336 goto configured;
1337
1338 for (i = 0; i < las->num_log_addrs; i++) {
1339 unsigned int type = las->log_addr_type[i];
1340 const u8 *la_list;
1341 u8 last_la;
1342
1343 /*
1344 * The TV functionality can only map to physical address 0.
1345 * For any other address, try the Specific functionality
1346 * instead as per the spec.
1347 */
1348 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1349 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1350
1351 la_list = type2addrs[type];
1352 last_la = las->log_addr[i];
1353 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1354 if (last_la == CEC_LOG_ADDR_INVALID ||
1355 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1356 !((1 << last_la) & type2mask[type]))
1357 last_la = la_list[0];
1358
1359 err = cec_config_log_addr(adap, i, last_la);
1360 if (err > 0) /* Reused last LA */
1361 continue;
1362
1363 if (err < 0)
1364 goto unconfigure;
1365
1366 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1367 /* Tried this one already, skip it */
1368 if (la_list[j] == last_la)
1369 continue;
1370 /* The backup addresses are CEC 2.0 specific */
1371 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1372 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1373 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1374 continue;
1375
1376 err = cec_config_log_addr(adap, i, la_list[j]);
1377 if (err == 0) /* LA is in use */
1378 continue;
1379 if (err < 0)
1380 goto unconfigure;
1381 /* Done, claimed an LA */
1382 break;
1383 }
1384
1385 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1386 dprintk(1, "could not claim LA %d\n", i);
1387 }
1388
1389 if (adap->log_addrs.log_addr_mask == 0 &&
1390 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1391 goto unconfigure;
1392
1393configured:
1394 if (adap->log_addrs.log_addr_mask == 0) {
1395 /* Fall back to unregistered */
1396 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1397 las->log_addr_mask = 1 << las->log_addr[0];
1398 for (i = 1; i < las->num_log_addrs; i++)
1399 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1400 }
1401 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1402 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1403 adap->is_configured = true;
1404 adap->is_configuring = false;
1405 cec_post_state_event(adap);
1406
1407 /*
1408 * Now post the Report Features and Report Physical Address broadcast
1409 * messages. Note that these are non-blocking transmits, meaning that
1410 * they are just queued up and once adap->lock is unlocked the main
1411 * thread will kick in and start transmitting these.
1412 *
1413 * If after this function is done (but before one or more of these
1414 * messages are actually transmitted) the CEC adapter is unconfigured,
1415 * then any remaining messages will be dropped by the main thread.
1416 */
1417 for (i = 0; i < las->num_log_addrs; i++) {
1418 struct cec_msg msg = {};
1419
1420 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1421 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1422 continue;
1423
1424 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1425
1426 /* Report Features must come first according to CEC 2.0 */
1427 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1428 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1429 cec_fill_msg_report_features(adap, &msg, i);
1430 cec_transmit_msg_fh(adap, &msg, NULL, false);
1431 }
1432
1433 /* Report Physical Address */
1434 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1435 las->primary_device_type[i]);
1436 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1437 las->log_addr[i],
1438 cec_phys_addr_exp(adap->phys_addr));
1439 cec_transmit_msg_fh(adap, &msg, NULL, false);
1440
1441 /* Report Vendor ID */
1442 if (adap->log_addrs.vendor_id != CEC_VENDOR_ID_NONE) {
1443 cec_msg_device_vendor_id(&msg,
1444 adap->log_addrs.vendor_id);
1445 cec_transmit_msg_fh(adap, &msg, NULL, false);
1446 }
1447 }
1448 adap->kthread_config = NULL;
1449 complete(&adap->config_completion);
1450 mutex_unlock(&adap->lock);
1451 return 0;
1452
1453unconfigure:
1454 for (i = 0; i < las->num_log_addrs; i++)
1455 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1456 cec_adap_unconfigure(adap);
1457 adap->kthread_config = NULL;
1458 mutex_unlock(&adap->lock);
1459 complete(&adap->config_completion);
1460 return 0;
1461}
1462
1463/*
1464 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1465 * logical addresses.
1466 *
1467 * This function is called with adap->lock held.
1468 */
1469static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1470{
1471 if (WARN_ON(adap->is_configuring || adap->is_configured))
1472 return;
1473
1474 init_completion(&adap->config_completion);
1475
1476 /* Ready to kick off the thread */
1477 adap->is_configuring = true;
1478 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1479 "ceccfg-%s", adap->name);
1480 if (IS_ERR(adap->kthread_config)) {
1481 adap->kthread_config = NULL;
1482 } else if (block) {
1483 mutex_unlock(&adap->lock);
1484 wait_for_completion(&adap->config_completion);
1485 mutex_lock(&adap->lock);
1486 }
1487}
1488
1489/* Set a new physical address and send an event notifying userspace of this.
1490 *
1491 * This function is called with adap->lock held.
1492 */
1493void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1494{
1495 if (phys_addr == adap->phys_addr)
1496 return;
1497 if (phys_addr != CEC_PHYS_ADDR_INVALID && adap->devnode.unregistered)
1498 return;
1499
1500 dprintk(1, "new physical address %x.%x.%x.%x\n",
1501 cec_phys_addr_exp(phys_addr));
1502 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1503 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1504 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1505 cec_post_state_event(adap);
1506 cec_adap_unconfigure(adap);
1507 /* Disabling monitor all mode should always succeed */
1508 if (adap->monitor_all_cnt)
1509 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1510 mutex_lock(&adap->devnode.lock);
1511 if (adap->needs_hpd || list_empty(&adap->devnode.fhs)) {
1512 WARN_ON(adap->ops->adap_enable(adap, false));
1513 adap->transmit_in_progress = false;
1514 wake_up_interruptible(&adap->kthread_waitq);
1515 }
1516 mutex_unlock(&adap->devnode.lock);
1517 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1518 return;
1519 }
1520
1521 mutex_lock(&adap->devnode.lock);
1522 adap->last_initiator = 0xff;
1523 adap->transmit_in_progress = false;
1524
1525 if ((adap->needs_hpd || list_empty(&adap->devnode.fhs)) &&
1526 adap->ops->adap_enable(adap, true)) {
1527 mutex_unlock(&adap->devnode.lock);
1528 return;
1529 }
1530
1531 if (adap->monitor_all_cnt &&
1532 call_op(adap, adap_monitor_all_enable, true)) {
1533 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
1534 WARN_ON(adap->ops->adap_enable(adap, false));
1535 mutex_unlock(&adap->devnode.lock);
1536 return;
1537 }
1538 mutex_unlock(&adap->devnode.lock);
1539
1540 adap->phys_addr = phys_addr;
1541 cec_post_state_event(adap);
1542 if (adap->log_addrs.num_log_addrs)
1543 cec_claim_log_addrs(adap, block);
1544}
1545
1546void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1547{
1548 if (IS_ERR_OR_NULL(adap))
1549 return;
1550
1551 mutex_lock(&adap->lock);
1552 __cec_s_phys_addr(adap, phys_addr, block);
1553 mutex_unlock(&adap->lock);
1554}
1555EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1556
1557void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1558 const struct edid *edid)
1559{
1560 u16 pa = CEC_PHYS_ADDR_INVALID;
1561
1562 if (edid && edid->extensions)
1563 pa = cec_get_edid_phys_addr((const u8 *)edid,
1564 EDID_LENGTH * (edid->extensions + 1), NULL);
1565 cec_s_phys_addr(adap, pa, false);
1566}
1567EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1568
1569/*
1570 * Called from either the ioctl or a driver to set the logical addresses.
1571 *
1572 * This function is called with adap->lock held.
1573 */
1574int __cec_s_log_addrs(struct cec_adapter *adap,
1575 struct cec_log_addrs *log_addrs, bool block)
1576{
1577 u16 type_mask = 0;
1578 int i;
1579
1580 if (adap->devnode.unregistered)
1581 return -ENODEV;
1582
1583 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1584 cec_adap_unconfigure(adap);
1585 adap->log_addrs.num_log_addrs = 0;
1586 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++)
1587 adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID;
1588 adap->log_addrs.osd_name[0] = '\0';
1589 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
1590 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
1591 return 0;
1592 }
1593
1594 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1595 /*
1596 * Sanitize log_addrs fields if a CDC-Only device is
1597 * requested.
1598 */
1599 log_addrs->num_log_addrs = 1;
1600 log_addrs->osd_name[0] = '\0';
1601 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1602 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1603 /*
1604 * This is just an internal convention since a CDC-Only device
1605 * doesn't have to be a switch. But switches already use
1606 * unregistered, so it makes some kind of sense to pick this
1607 * as the primary device. Since a CDC-Only device never sends
1608 * any 'normal' CEC messages this primary device type is never
1609 * sent over the CEC bus.
1610 */
1611 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1612 log_addrs->all_device_types[0] = 0;
1613 log_addrs->features[0][0] = 0;
1614 log_addrs->features[0][1] = 0;
1615 }
1616
1617 /* Ensure the osd name is 0-terminated */
1618 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1619
1620 /* Sanity checks */
1621 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1622 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1623 return -EINVAL;
1624 }
1625
1626 /*
1627 * Vendor ID is a 24 bit number, so check if the value is
1628 * within the correct range.
1629 */
1630 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1631 (log_addrs->vendor_id & 0xff000000) != 0) {
1632 dprintk(1, "invalid vendor ID\n");
1633 return -EINVAL;
1634 }
1635
1636 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1637 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1638 dprintk(1, "invalid CEC version\n");
1639 return -EINVAL;
1640 }
1641
1642 if (log_addrs->num_log_addrs > 1)
1643 for (i = 0; i < log_addrs->num_log_addrs; i++)
1644 if (log_addrs->log_addr_type[i] ==
1645 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1646 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1647 return -EINVAL;
1648 }
1649
1650 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1651 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1652 u8 *features = log_addrs->features[i];
1653 bool op_is_dev_features = false;
1654 unsigned j;
1655
1656 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1657 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1658 dprintk(1, "duplicate logical address type\n");
1659 return -EINVAL;
1660 }
1661 type_mask |= 1 << log_addrs->log_addr_type[i];
1662 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1663 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1664 /* Record already contains the playback functionality */
1665 dprintk(1, "invalid record + playback combination\n");
1666 return -EINVAL;
1667 }
1668 if (log_addrs->primary_device_type[i] >
1669 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1670 dprintk(1, "unknown primary device type\n");
1671 return -EINVAL;
1672 }
1673 if (log_addrs->primary_device_type[i] == 2) {
1674 dprintk(1, "invalid primary device type\n");
1675 return -EINVAL;
1676 }
1677 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1678 dprintk(1, "unknown logical address type\n");
1679 return -EINVAL;
1680 }
1681 for (j = 0; j < feature_sz; j++) {
1682 if ((features[j] & 0x80) == 0) {
1683 if (op_is_dev_features)
1684 break;
1685 op_is_dev_features = true;
1686 }
1687 }
1688 if (!op_is_dev_features || j == feature_sz) {
1689 dprintk(1, "malformed features\n");
1690 return -EINVAL;
1691 }
1692 /* Zero unused part of the feature array */
1693 memset(features + j + 1, 0, feature_sz - j - 1);
1694 }
1695
1696 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1697 if (log_addrs->num_log_addrs > 2) {
1698 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1699 return -EINVAL;
1700 }
1701 if (log_addrs->num_log_addrs == 2) {
1702 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1703 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1704 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1705 return -EINVAL;
1706 }
1707 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1708 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1709 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1710 return -EINVAL;
1711 }
1712 }
1713 }
1714
1715 /* Zero unused LAs */
1716 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1717 log_addrs->primary_device_type[i] = 0;
1718 log_addrs->log_addr_type[i] = 0;
1719 log_addrs->all_device_types[i] = 0;
1720 memset(log_addrs->features[i], 0,
1721 sizeof(log_addrs->features[i]));
1722 }
1723
1724 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1725 adap->log_addrs = *log_addrs;
1726 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1727 cec_claim_log_addrs(adap, block);
1728 return 0;
1729}
1730
1731int cec_s_log_addrs(struct cec_adapter *adap,
1732 struct cec_log_addrs *log_addrs, bool block)
1733{
1734 int err;
1735
1736 mutex_lock(&adap->lock);
1737 err = __cec_s_log_addrs(adap, log_addrs, block);
1738 mutex_unlock(&adap->lock);
1739 return err;
1740}
1741EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1742
1743/* High-level core CEC message handling */
1744
1745/* Fill in the Report Features message */
1746static void cec_fill_msg_report_features(struct cec_adapter *adap,
1747 struct cec_msg *msg,
1748 unsigned int la_idx)
1749{
1750 const struct cec_log_addrs *las = &adap->log_addrs;
1751 const u8 *features = las->features[la_idx];
1752 bool op_is_dev_features = false;
1753 unsigned int idx;
1754
1755 /* Report Features */
1756 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1757 msg->len = 4;
1758 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1759 msg->msg[2] = adap->log_addrs.cec_version;
1760 msg->msg[3] = las->all_device_types[la_idx];
1761
1762 /* Write RC Profiles first, then Device Features */
1763 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1764 msg->msg[msg->len++] = features[idx];
1765 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1766 if (op_is_dev_features)
1767 break;
1768 op_is_dev_features = true;
1769 }
1770 }
1771}
1772
1773/* Transmit the Feature Abort message */
1774static int cec_feature_abort_reason(struct cec_adapter *adap,
1775 struct cec_msg *msg, u8 reason)
1776{
1777 struct cec_msg tx_msg = { };
1778
1779 /*
1780 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1781 * message!
1782 */
1783 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1784 return 0;
1785 /* Don't Feature Abort messages from 'Unregistered' */
1786 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1787 return 0;
1788 cec_msg_set_reply_to(&tx_msg, msg);
1789 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1790 return cec_transmit_msg(adap, &tx_msg, false);
1791}
1792
1793static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1794{
1795 return cec_feature_abort_reason(adap, msg,
1796 CEC_OP_ABORT_UNRECOGNIZED_OP);
1797}
1798
1799static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1800{
1801 return cec_feature_abort_reason(adap, msg,
1802 CEC_OP_ABORT_REFUSED);
1803}
1804
1805/*
1806 * Called when a CEC message is received. This function will do any
1807 * necessary core processing. The is_reply bool is true if this message
1808 * is a reply to an earlier transmit.
1809 *
1810 * The message is either a broadcast message or a valid directed message.
1811 */
1812static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1813 bool is_reply)
1814{
1815 bool is_broadcast = cec_msg_is_broadcast(msg);
1816 u8 dest_laddr = cec_msg_destination(msg);
1817 u8 init_laddr = cec_msg_initiator(msg);
1818 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1819 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1820 bool from_unregistered = init_laddr == 0xf;
1821 struct cec_msg tx_cec_msg = { };
1822
1823 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1824
1825 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1826 if (cec_is_cdc_only(&adap->log_addrs) &&
1827 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1828 return 0;
1829
1830 if (adap->ops->received) {
1831 /* Allow drivers to process the message first */
1832 if (adap->ops->received(adap, msg) != -ENOMSG)
1833 return 0;
1834 }
1835
1836 /*
1837 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1838 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1839 * handled by the CEC core, even if the passthrough mode is on.
1840 * The others are just ignored if passthrough mode is on.
1841 */
1842 switch (msg->msg[1]) {
1843 case CEC_MSG_GET_CEC_VERSION:
1844 case CEC_MSG_ABORT:
1845 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1846 case CEC_MSG_GIVE_OSD_NAME:
1847 /*
1848 * These messages reply with a directed message, so ignore if
1849 * the initiator is Unregistered.
1850 */
1851 if (!adap->passthrough && from_unregistered)
1852 return 0;
1853 /* Fall through */
1854 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1855 case CEC_MSG_GIVE_FEATURES:
1856 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1857 /*
1858 * Skip processing these messages if the passthrough mode
1859 * is on.
1860 */
1861 if (adap->passthrough)
1862 goto skip_processing;
1863 /* Ignore if addressing is wrong */
1864 if (is_broadcast)
1865 return 0;
1866 break;
1867
1868 case CEC_MSG_USER_CONTROL_PRESSED:
1869 case CEC_MSG_USER_CONTROL_RELEASED:
1870 /* Wrong addressing mode: don't process */
1871 if (is_broadcast || from_unregistered)
1872 goto skip_processing;
1873 break;
1874
1875 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1876 /*
1877 * This message is always processed, regardless of the
1878 * passthrough setting.
1879 *
1880 * Exception: don't process if wrong addressing mode.
1881 */
1882 if (!is_broadcast)
1883 goto skip_processing;
1884 break;
1885
1886 default:
1887 break;
1888 }
1889
1890 cec_msg_set_reply_to(&tx_cec_msg, msg);
1891
1892 switch (msg->msg[1]) {
1893 /* The following messages are processed but still passed through */
1894 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1895 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1896
1897 if (!from_unregistered)
1898 adap->phys_addrs[init_laddr] = pa;
1899 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
1900 cec_phys_addr_exp(pa), init_laddr);
1901 break;
1902 }
1903
1904 case CEC_MSG_USER_CONTROL_PRESSED:
1905 if (!(adap->capabilities & CEC_CAP_RC) ||
1906 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1907 break;
1908
1909#ifdef CONFIG_MEDIA_CEC_RC
1910 switch (msg->msg[2]) {
1911 /*
1912 * Play function, this message can have variable length
1913 * depending on the specific play function that is used.
1914 */
1915 case 0x60:
1916 if (msg->len == 2)
1917 rc_keydown(adap->rc, RC_PROTO_CEC,
1918 msg->msg[2], 0);
1919 else
1920 rc_keydown(adap->rc, RC_PROTO_CEC,
1921 msg->msg[2] << 8 | msg->msg[3], 0);
1922 break;
1923 /*
1924 * Other function messages that are not handled.
1925 * Currently the RC framework does not allow to supply an
1926 * additional parameter to a keypress. These "keys" contain
1927 * other information such as channel number, an input number
1928 * etc.
1929 * For the time being these messages are not processed by the
1930 * framework and are simply forwarded to the user space.
1931 */
1932 case 0x56: case 0x57:
1933 case 0x67: case 0x68: case 0x69: case 0x6a:
1934 break;
1935 default:
1936 rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0);
1937 break;
1938 }
1939#endif
1940 break;
1941
1942 case CEC_MSG_USER_CONTROL_RELEASED:
1943 if (!(adap->capabilities & CEC_CAP_RC) ||
1944 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1945 break;
1946#ifdef CONFIG_MEDIA_CEC_RC
1947 rc_keyup(adap->rc);
1948#endif
1949 break;
1950
1951 /*
1952 * The remaining messages are only processed if the passthrough mode
1953 * is off.
1954 */
1955 case CEC_MSG_GET_CEC_VERSION:
1956 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1957 return cec_transmit_msg(adap, &tx_cec_msg, false);
1958
1959 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1960 /* Do nothing for CEC switches using addr 15 */
1961 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1962 return 0;
1963 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1964 return cec_transmit_msg(adap, &tx_cec_msg, false);
1965
1966 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1967 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1968 return cec_feature_abort(adap, msg);
1969 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1970 return cec_transmit_msg(adap, &tx_cec_msg, false);
1971
1972 case CEC_MSG_ABORT:
1973 /* Do nothing for CEC switches */
1974 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1975 return 0;
1976 return cec_feature_refused(adap, msg);
1977
1978 case CEC_MSG_GIVE_OSD_NAME: {
1979 if (adap->log_addrs.osd_name[0] == 0)
1980 return cec_feature_abort(adap, msg);
1981 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1982 return cec_transmit_msg(adap, &tx_cec_msg, false);
1983 }
1984
1985 case CEC_MSG_GIVE_FEATURES:
1986 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1987 return cec_feature_abort(adap, msg);
1988 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1989 return cec_transmit_msg(adap, &tx_cec_msg, false);
1990
1991 default:
1992 /*
1993 * Unprocessed messages are aborted if userspace isn't doing
1994 * any processing either.
1995 */
1996 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
1997 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1998 return cec_feature_abort(adap, msg);
1999 break;
2000 }
2001
2002skip_processing:
2003 /* If this was a reply, then we're done, unless otherwise specified */
2004 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
2005 return 0;
2006
2007 /*
2008 * Send to the exclusive follower if there is one, otherwise send
2009 * to all followers.
2010 */
2011 if (adap->cec_follower)
2012 cec_queue_msg_fh(adap->cec_follower, msg);
2013 else
2014 cec_queue_msg_followers(adap, msg);
2015 return 0;
2016}
2017
2018/*
2019 * Helper functions to keep track of the 'monitor all' use count.
2020 *
2021 * These functions are called with adap->lock held.
2022 */
2023int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
2024{
2025 int ret = 0;
2026
2027 if (adap->monitor_all_cnt == 0)
2028 ret = call_op(adap, adap_monitor_all_enable, 1);
2029 if (ret == 0)
2030 adap->monitor_all_cnt++;
2031 return ret;
2032}
2033
2034void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
2035{
2036 adap->monitor_all_cnt--;
2037 if (adap->monitor_all_cnt == 0)
2038 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
2039}
2040
2041/*
2042 * Helper functions to keep track of the 'monitor pin' use count.
2043 *
2044 * These functions are called with adap->lock held.
2045 */
2046int cec_monitor_pin_cnt_inc(struct cec_adapter *adap)
2047{
2048 int ret = 0;
2049
2050 if (adap->monitor_pin_cnt == 0)
2051 ret = call_op(adap, adap_monitor_pin_enable, 1);
2052 if (ret == 0)
2053 adap->monitor_pin_cnt++;
2054 return ret;
2055}
2056
2057void cec_monitor_pin_cnt_dec(struct cec_adapter *adap)
2058{
2059 adap->monitor_pin_cnt--;
2060 if (adap->monitor_pin_cnt == 0)
2061 WARN_ON(call_op(adap, adap_monitor_pin_enable, 0));
2062}
2063
2064#ifdef CONFIG_DEBUG_FS
2065/*
2066 * Log the current state of the CEC adapter.
2067 * Very useful for debugging.
2068 */
2069int cec_adap_status(struct seq_file *file, void *priv)
2070{
2071 struct cec_adapter *adap = dev_get_drvdata(file->private);
2072 struct cec_data *data;
2073
2074 mutex_lock(&adap->lock);
2075 seq_printf(file, "configured: %d\n", adap->is_configured);
2076 seq_printf(file, "configuring: %d\n", adap->is_configuring);
2077 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
2078 cec_phys_addr_exp(adap->phys_addr));
2079 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
2080 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
2081 if (adap->cec_follower)
2082 seq_printf(file, "has CEC follower%s\n",
2083 adap->passthrough ? " (in passthrough mode)" : "");
2084 if (adap->cec_initiator)
2085 seq_puts(file, "has CEC initiator\n");
2086 if (adap->monitor_all_cnt)
2087 seq_printf(file, "file handles in Monitor All mode: %u\n",
2088 adap->monitor_all_cnt);
2089 if (adap->tx_timeouts) {
2090 seq_printf(file, "transmit timeouts: %u\n",
2091 adap->tx_timeouts);
2092 adap->tx_timeouts = 0;
2093 }
2094 data = adap->transmitting;
2095 if (data)
2096 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
2097 data->msg.len, data->msg.msg, data->msg.reply,
2098 data->msg.timeout);
2099 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
2100 list_for_each_entry(data, &adap->transmit_queue, list) {
2101 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
2102 data->msg.len, data->msg.msg, data->msg.reply,
2103 data->msg.timeout);
2104 }
2105 list_for_each_entry(data, &adap->wait_queue, list) {
2106 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
2107 data->msg.len, data->msg.msg, data->msg.reply,
2108 data->msg.timeout);
2109 }
2110
2111 call_void_op(adap, adap_status, file);
2112 mutex_unlock(&adap->lock);
2113 return 0;
2114}
2115#endif