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