1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 "cec-priv.h"
32
33static void cec_fill_msg_report_features(struct cec_adapter *adap,
34 struct cec_msg *msg,
35 unsigned int la_idx);
36
37
38
39
40
41
42
43
44
45
46
47#define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
48
49#define call_op(adap, op, arg...) \
50 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
51
52#define call_void_op(adap, op, arg...) \
53 do { \
54 if (adap->ops->op) \
55 adap->ops->op(adap, ## arg); \
56 } while (0)
57
58static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
59{
60 int i;
61
62 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
63 if (adap->log_addrs.log_addr[i] == log_addr)
64 return i;
65 return -1;
66}
67
68static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
69{
70 int i = cec_log_addr2idx(adap, log_addr);
71
72 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
73}
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88void cec_queue_event_fh(struct cec_fh *fh,
89 const struct cec_event *new_ev, u64 ts)
90{
91 struct cec_event *ev = &fh->events[new_ev->event - 1];
92
93 if (ts == 0)
94 ts = ktime_get_ns();
95
96 mutex_lock(&fh->lock);
97 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
98 fh->pending_events & (1 << new_ev->event)) {
99
100
101
102
103
104 ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
105 goto unlock;
106 }
107
108
109
110
111
112 *ev = *new_ev;
113 ev->ts = ts;
114 fh->pending_events |= 1 << new_ev->event;
115
116unlock:
117 mutex_unlock(&fh->lock);
118 wake_up_interruptible(&fh->wait);
119}
120
121
122static void cec_queue_event(struct cec_adapter *adap,
123 const struct cec_event *ev)
124{
125 u64 ts = ktime_get_ns();
126 struct cec_fh *fh;
127
128 mutex_lock(&adap->devnode.lock);
129 list_for_each_entry(fh, &adap->devnode.fhs, list)
130 cec_queue_event_fh(fh, ev, ts);
131 mutex_unlock(&adap->devnode.lock);
132}
133
134
135
136
137
138static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
139{
140 static const struct cec_event ev_lost_msg = {
141 .ts = 0,
142 .event = CEC_EVENT_LOST_MSGS,
143 .flags = 0,
144 {
145 .lost_msgs.lost_msgs = 1,
146 },
147 };
148 struct cec_msg_entry *entry;
149
150 mutex_lock(&fh->lock);
151 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
152 if (!entry)
153 goto lost_msgs;
154
155 entry->msg = *msg;
156
157 list_add_tail(&entry->list, &fh->msgs);
158
159
160
161
162
163 if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
164 list_del(&entry->list);
165 goto lost_msgs;
166 }
167 fh->queued_msgs++;
168 mutex_unlock(&fh->lock);
169 wake_up_interruptible(&fh->wait);
170 return;
171
172lost_msgs:
173 mutex_unlock(&fh->lock);
174 cec_queue_event_fh(fh, &ev_lost_msg, 0);
175}
176
177
178
179
180
181
182
183
184
185
186
187static void cec_queue_msg_monitor(struct cec_adapter *adap,
188 const struct cec_msg *msg,
189 bool valid_la)
190{
191 struct cec_fh *fh;
192 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
193 CEC_MODE_MONITOR_ALL;
194
195 mutex_lock(&adap->devnode.lock);
196 list_for_each_entry(fh, &adap->devnode.fhs, list) {
197 if (fh->mode_follower >= monitor_mode)
198 cec_queue_msg_fh(fh, msg);
199 }
200 mutex_unlock(&adap->devnode.lock);
201}
202
203
204
205
206static void cec_queue_msg_followers(struct cec_adapter *adap,
207 const struct cec_msg *msg)
208{
209 struct cec_fh *fh;
210
211 mutex_lock(&adap->devnode.lock);
212 list_for_each_entry(fh, &adap->devnode.fhs, list) {
213 if (fh->mode_follower == CEC_MODE_FOLLOWER)
214 cec_queue_msg_fh(fh, msg);
215 }
216 mutex_unlock(&adap->devnode.lock);
217}
218
219
220static void cec_post_state_event(struct cec_adapter *adap)
221{
222 struct cec_event ev = {
223 .event = CEC_EVENT_STATE_CHANGE,
224 };
225
226 ev.state_change.phys_addr = adap->phys_addr;
227 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
228 cec_queue_event(adap, &ev);
229}
230
231
232
233
234
235
236
237
238static void cec_data_completed(struct cec_data *data)
239{
240
241
242
243
244
245
246
247
248 if (data->fh)
249 list_del(&data->xfer_list);
250
251 if (data->blocking) {
252
253
254
255
256 data->completed = true;
257 complete(&data->c);
258 } else {
259
260
261
262
263 if (data->fh)
264 cec_queue_msg_fh(data->fh, &data->msg);
265 kfree(data);
266 }
267}
268
269
270
271
272
273
274
275
276static void cec_data_cancel(struct cec_data *data)
277{
278
279
280
281
282 if (data->adap->transmitting == data) {
283 data->adap->transmitting = NULL;
284 } else {
285 list_del_init(&data->list);
286 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
287 data->adap->transmit_queue_sz--;
288 }
289
290
291 data->msg.tx_ts = ktime_get_ns();
292 data->msg.tx_status |= CEC_TX_STATUS_ERROR |
293 CEC_TX_STATUS_MAX_RETRIES;
294 data->msg.tx_error_cnt++;
295 data->attempts = 0;
296
297 cec_queue_msg_monitor(data->adap, &data->msg, 1);
298
299 cec_data_completed(data);
300}
301
302
303
304
305
306
307
308
309
310
311
312
313
314int cec_thread_func(void *_adap)
315{
316 struct cec_adapter *adap = _adap;
317
318 for (;;) {
319 unsigned int signal_free_time;
320 struct cec_data *data;
321 bool timeout = false;
322 u8 attempts;
323
324 if (adap->transmitting) {
325 int err;
326
327
328
329
330
331
332
333
334 err = wait_event_interruptible_timeout(adap->kthread_waitq,
335 kthread_should_stop() ||
336 (!adap->is_configured && !adap->is_configuring) ||
337 (!adap->transmitting &&
338 !list_empty(&adap->transmit_queue)),
339 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
340 timeout = err == 0;
341 } else {
342
343 wait_event_interruptible(adap->kthread_waitq,
344 kthread_should_stop() ||
345 (!adap->transmitting &&
346 !list_empty(&adap->transmit_queue)));
347 }
348
349 mutex_lock(&adap->lock);
350
351 if ((!adap->is_configured && !adap->is_configuring) ||
352 kthread_should_stop()) {
353
354
355
356
357 while (!list_empty(&adap->transmit_queue)) {
358 data = list_first_entry(&adap->transmit_queue,
359 struct cec_data, list);
360 cec_data_cancel(data);
361 }
362 if (adap->transmitting)
363 cec_data_cancel(adap->transmitting);
364
365
366
367
368
369
370
371
372
373 while (!list_empty(&adap->wait_queue)) {
374 data = list_first_entry(&adap->wait_queue,
375 struct cec_data, list);
376
377 if (!cancel_delayed_work(&data->work)) {
378 mutex_unlock(&adap->lock);
379 flush_scheduled_work();
380 mutex_lock(&adap->lock);
381 }
382 cec_data_cancel(data);
383 }
384 goto unlock;
385 }
386
387 if (adap->transmitting && timeout) {
388
389
390
391
392
393 dprintk(0, "message %*ph timed out!\n",
394 adap->transmitting->msg.len,
395 adap->transmitting->msg.msg);
396
397 cec_data_cancel(adap->transmitting);
398 goto unlock;
399 }
400
401
402
403
404
405 if (adap->transmitting || list_empty(&adap->transmit_queue))
406 goto unlock;
407
408
409 data = list_first_entry(&adap->transmit_queue,
410 struct cec_data, list);
411 list_del_init(&data->list);
412 adap->transmit_queue_sz--;
413
414 adap->transmitting = data;
415
416
417
418
419
420
421
422 if (data->msg.len == 1 && adap->is_configured)
423 attempts = 2;
424 else
425 attempts = 4;
426
427
428 if (data->attempts) {
429
430 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
431 } else if (data->new_initiator) {
432
433 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
434 } else {
435
436
437
438
439 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
440 }
441 if (data->attempts == 0)
442 data->attempts = attempts;
443
444
445 if (adap->ops->adap_transmit(adap, data->attempts,
446 signal_free_time, &data->msg))
447 cec_data_cancel(data);
448
449unlock:
450 mutex_unlock(&adap->lock);
451
452 if (kthread_should_stop())
453 break;
454 }
455 return 0;
456}
457
458
459
460
461void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
462 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
463{
464 struct cec_data *data;
465 struct cec_msg *msg;
466 u64 ts = ktime_get_ns();
467
468 dprintk(2, "cec_transmit_done %02x\n", status);
469 mutex_lock(&adap->lock);
470 data = adap->transmitting;
471 if (!data) {
472
473
474
475
476
477 dprintk(1, "cec_transmit_done without an ongoing transmit!\n");
478 goto unlock;
479 }
480
481 msg = &data->msg;
482
483
484 WARN_ON(status == 0);
485 msg->tx_ts = ts;
486 msg->tx_status |= status;
487 msg->tx_arb_lost_cnt += arb_lost_cnt;
488 msg->tx_nack_cnt += nack_cnt;
489 msg->tx_low_drive_cnt += low_drive_cnt;
490 msg->tx_error_cnt += error_cnt;
491
492
493 adap->transmitting = NULL;
494
495
496
497
498
499
500 if (data->attempts > 1 &&
501 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
502
503 data->attempts--;
504
505 list_add(&data->list, &adap->transmit_queue);
506 adap->transmit_queue_sz++;
507 goto wake_thread;
508 }
509
510 data->attempts = 0;
511
512
513 if (!(status & CEC_TX_STATUS_OK))
514 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
515
516
517 cec_queue_msg_monitor(adap, msg, 1);
518
519 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
520 msg->timeout) {
521
522
523
524
525 list_add_tail(&data->list, &adap->wait_queue);
526 schedule_delayed_work(&data->work,
527 msecs_to_jiffies(msg->timeout));
528 } else {
529
530 cec_data_completed(data);
531 }
532
533wake_thread:
534
535
536
537
538 wake_up_interruptible(&adap->kthread_waitq);
539unlock:
540 mutex_unlock(&adap->lock);
541}
542EXPORT_SYMBOL_GPL(cec_transmit_done);
543
544
545
546
547static void cec_wait_timeout(struct work_struct *work)
548{
549 struct cec_data *data = container_of(work, struct cec_data, work.work);
550 struct cec_adapter *adap = data->adap;
551
552 mutex_lock(&adap->lock);
553
554
555
556
557 if (list_empty(&data->list))
558 goto unlock;
559
560
561 list_del_init(&data->list);
562 data->msg.rx_ts = ktime_get_ns();
563 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
564 cec_data_completed(data);
565unlock:
566 mutex_unlock(&adap->lock);
567}
568
569
570
571
572
573
574
575int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
576 struct cec_fh *fh, bool block)
577{
578 struct cec_data *data;
579 u8 last_initiator = 0xff;
580 unsigned int timeout;
581 int res = 0;
582
583 msg->rx_ts = 0;
584 msg->tx_ts = 0;
585 msg->rx_status = 0;
586 msg->tx_status = 0;
587 msg->tx_arb_lost_cnt = 0;
588 msg->tx_nack_cnt = 0;
589 msg->tx_low_drive_cnt = 0;
590 msg->tx_error_cnt = 0;
591 msg->sequence = ++adap->sequence;
592 if (!msg->sequence)
593 msg->sequence = ++adap->sequence;
594
595 if (msg->reply && msg->timeout == 0) {
596
597 msg->timeout = 1000;
598 }
599 if (msg->timeout)
600 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
601 else
602 msg->flags = 0;
603
604
605 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
606 dprintk(1, "cec_transmit_msg: invalid length %d\n", msg->len);
607 return -EINVAL;
608 }
609 if (msg->timeout && msg->len == 1) {
610 dprintk(1, "cec_transmit_msg: can't reply for poll msg\n");
611 return -EINVAL;
612 }
613 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
614 if (msg->len == 1) {
615 if (cec_msg_destination(msg) == 0xf) {
616 dprintk(1, "cec_transmit_msg: invalid poll message\n");
617 return -EINVAL;
618 }
619 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
620
621
622
623
624
625
626
627
628 msg->tx_ts = ktime_get_ns();
629 msg->tx_status = CEC_TX_STATUS_NACK |
630 CEC_TX_STATUS_MAX_RETRIES;
631 msg->tx_nack_cnt = 1;
632 return 0;
633 }
634 }
635 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
636 cec_has_log_addr(adap, cec_msg_destination(msg))) {
637 dprintk(1, "cec_transmit_msg: destination is the adapter itself\n");
638 return -EINVAL;
639 }
640 if (msg->len > 1 && adap->is_configured &&
641 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
642 dprintk(1, "cec_transmit_msg: initiator has unknown logical address %d\n",
643 cec_msg_initiator(msg));
644 return -EINVAL;
645 }
646 if (!adap->is_configured && !adap->is_configuring)
647 return -ENONET;
648
649 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ)
650 return -EBUSY;
651
652 data = kzalloc(sizeof(*data), GFP_KERNEL);
653 if (!data)
654 return -ENOMEM;
655
656 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
657 msg->msg[2] = adap->phys_addr >> 8;
658 msg->msg[3] = adap->phys_addr & 0xff;
659 }
660
661 if (msg->timeout)
662 dprintk(2, "cec_transmit_msg: %*ph (wait for 0x%02x%s)\n",
663 msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
664 else
665 dprintk(2, "cec_transmit_msg: %*ph%s\n",
666 msg->len, msg->msg, !block ? " (nb)" : "");
667
668 data->msg = *msg;
669 data->fh = fh;
670 data->adap = adap;
671 data->blocking = block;
672
673
674
675
676
677 if (msg->len > 1) {
678 if (!(list_empty(&adap->transmit_queue))) {
679 const struct cec_data *last;
680
681 last = list_last_entry(&adap->transmit_queue,
682 const struct cec_data, list);
683 last_initiator = cec_msg_initiator(&last->msg);
684 } else if (adap->transmitting) {
685 last_initiator =
686 cec_msg_initiator(&adap->transmitting->msg);
687 }
688 }
689 data->new_initiator = last_initiator != cec_msg_initiator(msg);
690 init_completion(&data->c);
691 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
692
693 if (fh)
694 list_add_tail(&data->xfer_list, &fh->xfer_list);
695 list_add_tail(&data->list, &adap->transmit_queue);
696 adap->transmit_queue_sz++;
697 if (!adap->transmitting)
698 wake_up_interruptible(&adap->kthread_waitq);
699
700
701 if (!block)
702 return 0;
703
704
705
706
707
708 timeout = CEC_XFER_TIMEOUT_MS;
709
710 if (msg->timeout)
711 timeout += msg->timeout;
712
713
714
715
716 mutex_unlock(&adap->lock);
717 res = wait_for_completion_killable_timeout(&data->c,
718 msecs_to_jiffies(timeout));
719 mutex_lock(&adap->lock);
720
721 if (data->completed) {
722
723 *msg = data->msg;
724 kfree(data);
725 return 0;
726 }
727
728
729
730
731
732
733 data->blocking = false;
734 if (data->fh)
735 list_del(&data->xfer_list);
736 data->fh = NULL;
737
738 if (res == 0) {
739
740 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
741 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
742 else
743 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
744 }
745 return res > 0 ? 0 : res;
746}
747
748
749int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
750 bool block)
751{
752 int ret;
753
754 mutex_lock(&adap->lock);
755 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
756 mutex_unlock(&adap->lock);
757 return ret;
758}
759EXPORT_SYMBOL_GPL(cec_transmit_msg);
760
761
762
763
764
765
766static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
767 bool is_reply);
768
769#define DIRECTED 0x80
770#define BCAST1_4 0x40
771#define BCAST2_0 0x20
772#define BCAST (BCAST1_4 | BCAST2_0)
773#define BOTH (BCAST | DIRECTED)
774
775
776
777
778
779
780static const u8 cec_msg_size[256] = {
781 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
782 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
783 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
784 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
785 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
786 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
787 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
788 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
789 [CEC_MSG_STANDBY] = 2 | BOTH,
790 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
791 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
792 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
793 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
794 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
795 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
796 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
797 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
798 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
799 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
800 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
801 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
802 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
803 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
804 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
805 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
806 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
807 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
808 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
809 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
810 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
811 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
812 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
813 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
814 [CEC_MSG_PLAY] = 3 | DIRECTED,
815 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
816 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
817 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
818 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
819 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
820 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
821 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
822 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
823 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
824 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
825 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
826 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
827 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
828 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
829 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
830 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
831 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
832 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
833 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
834 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
835 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
836 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
837 [CEC_MSG_ABORT] = 2 | DIRECTED,
838 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
839 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
840 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
841 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
842 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
843 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
844 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
845 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
846 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
847 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
848 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
849 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
850 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
851 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
852 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
853 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
854 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
855 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
856};
857
858
859void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
860{
861 struct cec_data *data;
862 u8 msg_init = cec_msg_initiator(msg);
863 u8 msg_dest = cec_msg_destination(msg);
864 u8 cmd = msg->msg[1];
865 bool is_reply = false;
866 bool valid_la = true;
867 u8 min_len = 0;
868
869 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
870 return;
871
872
873
874
875
876
877
878
879
880
881
882
883 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
884 cec_has_log_addr(adap, msg_init))
885 return;
886
887 msg->rx_ts = ktime_get_ns();
888 msg->rx_status = CEC_RX_STATUS_OK;
889 msg->sequence = msg->reply = msg->timeout = 0;
890 msg->tx_status = 0;
891 msg->tx_ts = 0;
892 msg->tx_arb_lost_cnt = 0;
893 msg->tx_nack_cnt = 0;
894 msg->tx_low_drive_cnt = 0;
895 msg->tx_error_cnt = 0;
896 msg->flags = 0;
897 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
898
899 mutex_lock(&adap->lock);
900 dprintk(2, "cec_received_msg: %*ph\n", msg->len, msg->msg);
901
902
903 if (!cec_msg_is_broadcast(msg))
904 valid_la = cec_has_log_addr(adap, msg_dest);
905
906
907
908
909
910
911
912 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
913 u8 dir_fl = cec_msg_size[cmd] & BOTH;
914
915 min_len = cec_msg_size[cmd] & 0x1f;
916 if (msg->len < min_len)
917 valid_la = false;
918 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
919 valid_la = false;
920 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
921 valid_la = false;
922 else if (cec_msg_is_broadcast(msg) &&
923 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
924 !(dir_fl & BCAST2_0))
925 valid_la = false;
926 }
927 if (valid_la && min_len) {
928
929 switch (cmd) {
930 case CEC_MSG_TIMER_STATUS:
931 if (msg->msg[2] & 0x10) {
932 switch (msg->msg[2] & 0xf) {
933 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
934 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
935 if (msg->len < 5)
936 valid_la = false;
937 break;
938 }
939 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
940 if (msg->len < 5)
941 valid_la = false;
942 }
943 break;
944 case CEC_MSG_RECORD_ON:
945 switch (msg->msg[2]) {
946 case CEC_OP_RECORD_SRC_OWN:
947 break;
948 case CEC_OP_RECORD_SRC_DIGITAL:
949 if (msg->len < 10)
950 valid_la = false;
951 break;
952 case CEC_OP_RECORD_SRC_ANALOG:
953 if (msg->len < 7)
954 valid_la = false;
955 break;
956 case CEC_OP_RECORD_SRC_EXT_PLUG:
957 if (msg->len < 4)
958 valid_la = false;
959 break;
960 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
961 if (msg->len < 5)
962 valid_la = false;
963 break;
964 }
965 break;
966 }
967 }
968
969
970 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
971 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
972
973
974 if (abort)
975 cmd = msg->msg[2];
976
977
978
979
980
981 list_for_each_entry(data, &adap->wait_queue, list) {
982 struct cec_msg *dst = &data->msg;
983
984
985
986
987
988
989 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
990 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
991 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
992 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
993 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
994 dst->reply = cmd;
995
996
997 if ((abort && cmd != dst->msg[1]) ||
998 (!abort && cmd != dst->reply))
999 continue;
1000
1001
1002 if (msg_init != cec_msg_destination(dst) &&
1003 !cec_msg_is_broadcast(dst))
1004 continue;
1005
1006
1007 memcpy(dst->msg, msg->msg, msg->len);
1008 dst->len = msg->len;
1009 dst->rx_ts = msg->rx_ts;
1010 dst->rx_status = msg->rx_status;
1011 if (abort)
1012 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1013 msg->flags = dst->flags;
1014
1015 list_del_init(&data->list);
1016
1017
1018 if (!cancel_delayed_work(&data->work)) {
1019 mutex_unlock(&adap->lock);
1020 flush_scheduled_work();
1021 mutex_lock(&adap->lock);
1022 }
1023
1024
1025
1026
1027 if (data->fh)
1028 is_reply = true;
1029 cec_data_completed(data);
1030 break;
1031 }
1032 }
1033 mutex_unlock(&adap->lock);
1034
1035
1036 cec_queue_msg_monitor(adap, msg, valid_la);
1037
1038
1039 if (!valid_la || msg->len <= 1)
1040 return;
1041
1042 if (adap->log_addrs.log_addr_mask == 0)
1043 return;
1044
1045
1046
1047
1048
1049
1050 cec_receive_notify(adap, msg, is_reply);
1051}
1052EXPORT_SYMBOL_GPL(cec_received_msg);
1053
1054
1055
1056
1057
1058
1059
1060
1061static int cec_config_log_addr(struct cec_adapter *adap,
1062 unsigned int idx,
1063 unsigned int log_addr)
1064{
1065 struct cec_log_addrs *las = &adap->log_addrs;
1066 struct cec_msg msg = { };
1067 int err;
1068
1069 if (cec_has_log_addr(adap, log_addr))
1070 return 0;
1071
1072
1073 msg.len = 1;
1074 msg.msg[0] = (log_addr << 4) | log_addr;
1075 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1076
1077
1078
1079
1080
1081 if (!adap->is_configuring)
1082 return -EINTR;
1083
1084 if (err)
1085 return err;
1086
1087 if (msg.tx_status & CEC_TX_STATUS_OK)
1088 return 0;
1089
1090
1091
1092
1093
1094 err = adap->ops->adap_log_addr(adap, log_addr);
1095 if (err)
1096 return err;
1097
1098 las->log_addr[idx] = log_addr;
1099 las->log_addr_mask |= 1 << log_addr;
1100 adap->phys_addrs[log_addr] = adap->phys_addr;
1101
1102 dprintk(2, "claimed addr %d (%d)\n", log_addr,
1103 las->primary_device_type[idx]);
1104 return 1;
1105}
1106
1107
1108
1109
1110
1111
1112
1113static void cec_adap_unconfigure(struct cec_adapter *adap)
1114{
1115 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1116 adap->log_addrs.log_addr_mask = 0;
1117 adap->is_configuring = false;
1118 adap->is_configured = false;
1119 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1120 wake_up_interruptible(&adap->kthread_waitq);
1121 cec_post_state_event(adap);
1122}
1123
1124
1125
1126
1127static int cec_config_thread_func(void *arg)
1128{
1129
1130 static const u8 tv_log_addrs[] = {
1131 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1132 CEC_LOG_ADDR_INVALID
1133 };
1134 static const u8 record_log_addrs[] = {
1135 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1136 CEC_LOG_ADDR_RECORD_3,
1137 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1138 CEC_LOG_ADDR_INVALID
1139 };
1140 static const u8 tuner_log_addrs[] = {
1141 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1142 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1143 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1144 CEC_LOG_ADDR_INVALID
1145 };
1146 static const u8 playback_log_addrs[] = {
1147 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1148 CEC_LOG_ADDR_PLAYBACK_3,
1149 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1150 CEC_LOG_ADDR_INVALID
1151 };
1152 static const u8 audiosystem_log_addrs[] = {
1153 CEC_LOG_ADDR_AUDIOSYSTEM,
1154 CEC_LOG_ADDR_INVALID
1155 };
1156 static const u8 specific_use_log_addrs[] = {
1157 CEC_LOG_ADDR_SPECIFIC,
1158 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1159 CEC_LOG_ADDR_INVALID
1160 };
1161 static const u8 *type2addrs[6] = {
1162 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1163 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1164 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1165 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1166 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1167 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1168 };
1169 static const u16 type2mask[] = {
1170 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1171 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1172 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1173 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1174 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1175 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1176 };
1177 struct cec_adapter *adap = arg;
1178 struct cec_log_addrs *las = &adap->log_addrs;
1179 int err;
1180 int i, j;
1181
1182 mutex_lock(&adap->lock);
1183 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1184 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1185 las->log_addr_mask = 0;
1186
1187 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1188 goto configured;
1189
1190 for (i = 0; i < las->num_log_addrs; i++) {
1191 unsigned int type = las->log_addr_type[i];
1192 const u8 *la_list;
1193 u8 last_la;
1194
1195
1196
1197
1198
1199
1200 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1201 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1202
1203 la_list = type2addrs[type];
1204 last_la = las->log_addr[i];
1205 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1206 if (last_la == CEC_LOG_ADDR_INVALID ||
1207 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1208 !((1 << last_la) & type2mask[type]))
1209 last_la = la_list[0];
1210
1211 err = cec_config_log_addr(adap, i, last_la);
1212 if (err > 0)
1213 continue;
1214
1215 if (err < 0)
1216 goto unconfigure;
1217
1218 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1219
1220 if (la_list[j] == last_la)
1221 continue;
1222
1223 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1224 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1225 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1226 continue;
1227
1228 err = cec_config_log_addr(adap, i, la_list[j]);
1229 if (err == 0)
1230 continue;
1231 if (err < 0)
1232 goto unconfigure;
1233
1234 break;
1235 }
1236
1237 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1238 dprintk(1, "could not claim LA %d\n", i);
1239 }
1240
1241 if (adap->log_addrs.log_addr_mask == 0 &&
1242 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1243 goto unconfigure;
1244
1245configured:
1246 if (adap->log_addrs.log_addr_mask == 0) {
1247
1248 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1249 las->log_addr_mask = 1 << las->log_addr[0];
1250 for (i = 1; i < las->num_log_addrs; i++)
1251 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1252 }
1253 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1254 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1255 adap->is_configured = true;
1256 adap->is_configuring = false;
1257 cec_post_state_event(adap);
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269 for (i = 0; i < las->num_log_addrs; i++) {
1270 struct cec_msg msg = {};
1271
1272 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1273 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1274 continue;
1275
1276 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1277
1278
1279 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1280 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1281 cec_fill_msg_report_features(adap, &msg, i);
1282 cec_transmit_msg_fh(adap, &msg, NULL, false);
1283 }
1284
1285
1286 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1287 las->primary_device_type[i]);
1288 dprintk(2, "config: la %d pa %x.%x.%x.%x\n",
1289 las->log_addr[i],
1290 cec_phys_addr_exp(adap->phys_addr));
1291 cec_transmit_msg_fh(adap, &msg, NULL, false);
1292 }
1293 adap->kthread_config = NULL;
1294 complete(&adap->config_completion);
1295 mutex_unlock(&adap->lock);
1296 return 0;
1297
1298unconfigure:
1299 for (i = 0; i < las->num_log_addrs; i++)
1300 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1301 cec_adap_unconfigure(adap);
1302 adap->kthread_config = NULL;
1303 mutex_unlock(&adap->lock);
1304 complete(&adap->config_completion);
1305 return 0;
1306}
1307
1308
1309
1310
1311
1312
1313
1314static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1315{
1316 if (WARN_ON(adap->is_configuring || adap->is_configured))
1317 return;
1318
1319 init_completion(&adap->config_completion);
1320
1321
1322 adap->is_configuring = true;
1323 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1324 "ceccfg-%s", adap->name);
1325 if (IS_ERR(adap->kthread_config)) {
1326 adap->kthread_config = NULL;
1327 } else if (block) {
1328 mutex_unlock(&adap->lock);
1329 wait_for_completion(&adap->config_completion);
1330 mutex_lock(&adap->lock);
1331 }
1332}
1333
1334
1335
1336
1337
1338void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1339{
1340 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
1341 return;
1342
1343 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1344 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1345 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1346 cec_post_state_event(adap);
1347 cec_adap_unconfigure(adap);
1348
1349 if (adap->monitor_all_cnt)
1350 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1351 WARN_ON(adap->ops->adap_enable(adap, false));
1352 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1353 return;
1354 }
1355
1356 if (adap->ops->adap_enable(adap, true))
1357 return;
1358
1359 if (adap->monitor_all_cnt &&
1360 call_op(adap, adap_monitor_all_enable, true)) {
1361 WARN_ON(adap->ops->adap_enable(adap, false));
1362 return;
1363 }
1364 adap->phys_addr = phys_addr;
1365 cec_post_state_event(adap);
1366 if (adap->log_addrs.num_log_addrs)
1367 cec_claim_log_addrs(adap, block);
1368}
1369
1370void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1371{
1372 if (IS_ERR_OR_NULL(adap))
1373 return;
1374
1375 mutex_lock(&adap->lock);
1376 __cec_s_phys_addr(adap, phys_addr, block);
1377 mutex_unlock(&adap->lock);
1378}
1379EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1380
1381
1382
1383
1384
1385
1386int __cec_s_log_addrs(struct cec_adapter *adap,
1387 struct cec_log_addrs *log_addrs, bool block)
1388{
1389 u16 type_mask = 0;
1390 int i;
1391
1392 if (adap->devnode.unregistered)
1393 return -ENODEV;
1394
1395 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1396 adap->log_addrs.num_log_addrs = 0;
1397 cec_adap_unconfigure(adap);
1398 return 0;
1399 }
1400
1401 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1402
1403
1404
1405
1406 log_addrs->num_log_addrs = 1;
1407 log_addrs->osd_name[0] = '\0';
1408 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1409 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1410
1411
1412
1413
1414
1415
1416
1417
1418 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1419 log_addrs->all_device_types[0] = 0;
1420 log_addrs->features[0][0] = 0;
1421 log_addrs->features[0][1] = 0;
1422 }
1423
1424
1425 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1426
1427
1428 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1429 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1430 return -EINVAL;
1431 }
1432
1433
1434
1435
1436
1437 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1438 (log_addrs->vendor_id & 0xff000000) != 0)
1439 return -EINVAL;
1440
1441 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1442 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0)
1443 return -EINVAL;
1444
1445 if (log_addrs->num_log_addrs > 1)
1446 for (i = 0; i < log_addrs->num_log_addrs; i++)
1447 if (log_addrs->log_addr_type[i] ==
1448 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1449 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1450 return -EINVAL;
1451 }
1452
1453 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1454 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1455 u8 *features = log_addrs->features[i];
1456 bool op_is_dev_features = false;
1457 unsigned j;
1458
1459 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1460 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1461 dprintk(1, "duplicate logical address type\n");
1462 return -EINVAL;
1463 }
1464 type_mask |= 1 << log_addrs->log_addr_type[i];
1465 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1466 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1467
1468 dprintk(1, "invalid record + playback combination\n");
1469 return -EINVAL;
1470 }
1471 if (log_addrs->primary_device_type[i] >
1472 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1473 dprintk(1, "unknown primary device type\n");
1474 return -EINVAL;
1475 }
1476 if (log_addrs->primary_device_type[i] == 2) {
1477 dprintk(1, "invalid primary device type\n");
1478 return -EINVAL;
1479 }
1480 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1481 dprintk(1, "unknown logical address type\n");
1482 return -EINVAL;
1483 }
1484 for (j = 0; j < feature_sz; j++) {
1485 if ((features[j] & 0x80) == 0) {
1486 if (op_is_dev_features)
1487 break;
1488 op_is_dev_features = true;
1489 }
1490 }
1491 if (!op_is_dev_features || j == feature_sz) {
1492 dprintk(1, "malformed features\n");
1493 return -EINVAL;
1494 }
1495
1496 memset(features + j + 1, 0, feature_sz - j - 1);
1497 }
1498
1499 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1500 if (log_addrs->num_log_addrs > 2) {
1501 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1502 return -EINVAL;
1503 }
1504 if (log_addrs->num_log_addrs == 2) {
1505 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1506 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1507 dprintk(1, "Two LAs is only allowed for audiosystem and TV\n");
1508 return -EINVAL;
1509 }
1510 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1511 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1512 dprintk(1, "An audiosystem/TV can only be combined with record or playback\n");
1513 return -EINVAL;
1514 }
1515 }
1516 }
1517
1518
1519 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1520 log_addrs->primary_device_type[i] = 0;
1521 log_addrs->log_addr_type[i] = 0;
1522 log_addrs->all_device_types[i] = 0;
1523 memset(log_addrs->features[i], 0,
1524 sizeof(log_addrs->features[i]));
1525 }
1526
1527 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1528 adap->log_addrs = *log_addrs;
1529 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1530 cec_claim_log_addrs(adap, block);
1531 return 0;
1532}
1533
1534int cec_s_log_addrs(struct cec_adapter *adap,
1535 struct cec_log_addrs *log_addrs, bool block)
1536{
1537 int err;
1538
1539 mutex_lock(&adap->lock);
1540 err = __cec_s_log_addrs(adap, log_addrs, block);
1541 mutex_unlock(&adap->lock);
1542 return err;
1543}
1544EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1545
1546
1547
1548
1549static void cec_fill_msg_report_features(struct cec_adapter *adap,
1550 struct cec_msg *msg,
1551 unsigned int la_idx)
1552{
1553 const struct cec_log_addrs *las = &adap->log_addrs;
1554 const u8 *features = las->features[la_idx];
1555 bool op_is_dev_features = false;
1556 unsigned int idx;
1557
1558
1559 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1560 msg->len = 4;
1561 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1562 msg->msg[2] = adap->log_addrs.cec_version;
1563 msg->msg[3] = las->all_device_types[la_idx];
1564
1565
1566 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1567 msg->msg[msg->len++] = features[idx];
1568 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1569 if (op_is_dev_features)
1570 break;
1571 op_is_dev_features = true;
1572 }
1573 }
1574}
1575
1576
1577static int cec_feature_abort_reason(struct cec_adapter *adap,
1578 struct cec_msg *msg, u8 reason)
1579{
1580 struct cec_msg tx_msg = { };
1581
1582
1583
1584
1585
1586 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1587 return 0;
1588 cec_msg_set_reply_to(&tx_msg, msg);
1589 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1590 return cec_transmit_msg(adap, &tx_msg, false);
1591}
1592
1593static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1594{
1595 return cec_feature_abort_reason(adap, msg,
1596 CEC_OP_ABORT_UNRECOGNIZED_OP);
1597}
1598
1599static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1600{
1601 return cec_feature_abort_reason(adap, msg,
1602 CEC_OP_ABORT_REFUSED);
1603}
1604
1605
1606
1607
1608
1609
1610
1611
1612static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1613 bool is_reply)
1614{
1615 bool is_broadcast = cec_msg_is_broadcast(msg);
1616 u8 dest_laddr = cec_msg_destination(msg);
1617 u8 init_laddr = cec_msg_initiator(msg);
1618 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1619 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1620 bool from_unregistered = init_laddr == 0xf;
1621 struct cec_msg tx_cec_msg = { };
1622
1623 dprintk(1, "cec_receive_notify: %*ph\n", msg->len, msg->msg);
1624
1625
1626 if (cec_is_cdc_only(&adap->log_addrs) &&
1627 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1628 return 0;
1629
1630 if (adap->ops->received) {
1631
1632 if (adap->ops->received(adap, msg) != -ENOMSG)
1633 return 0;
1634 }
1635
1636
1637
1638
1639
1640
1641
1642 switch (msg->msg[1]) {
1643 case CEC_MSG_GET_CEC_VERSION:
1644 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1645 case CEC_MSG_ABORT:
1646 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1647 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1648 case CEC_MSG_GIVE_OSD_NAME:
1649 case CEC_MSG_GIVE_FEATURES:
1650
1651
1652
1653
1654 if (adap->passthrough)
1655 goto skip_processing;
1656
1657 if (is_broadcast || from_unregistered)
1658 return 0;
1659 break;
1660
1661 case CEC_MSG_USER_CONTROL_PRESSED:
1662 case CEC_MSG_USER_CONTROL_RELEASED:
1663
1664 if (is_broadcast || from_unregistered)
1665 goto skip_processing;
1666 break;
1667
1668 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1669
1670
1671
1672
1673
1674
1675 if (!is_broadcast)
1676 goto skip_processing;
1677 break;
1678
1679 default:
1680 break;
1681 }
1682
1683 cec_msg_set_reply_to(&tx_cec_msg, msg);
1684
1685 switch (msg->msg[1]) {
1686
1687 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1688 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1689
1690 if (!from_unregistered)
1691 adap->phys_addrs[init_laddr] = pa;
1692 dprintk(1, "Reported physical address %x.%x.%x.%x for logical address %d\n",
1693 cec_phys_addr_exp(pa), init_laddr);
1694 break;
1695 }
1696
1697 case CEC_MSG_USER_CONTROL_PRESSED:
1698 if (!(adap->capabilities & CEC_CAP_RC) ||
1699 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1700 break;
1701
1702#if IS_REACHABLE(CONFIG_RC_CORE)
1703 switch (msg->msg[2]) {
1704
1705
1706
1707
1708 case 0x60:
1709 if (msg->len == 2)
1710 rc_keydown(adap->rc, RC_TYPE_CEC,
1711 msg->msg[2], 0);
1712 else
1713 rc_keydown(adap->rc, RC_TYPE_CEC,
1714 msg->msg[2] << 8 | msg->msg[3], 0);
1715 break;
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725 case 0x56: case 0x57:
1726 case 0x67: case 0x68: case 0x69: case 0x6a:
1727 break;
1728 default:
1729 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1730 break;
1731 }
1732#endif
1733 break;
1734
1735 case CEC_MSG_USER_CONTROL_RELEASED:
1736 if (!(adap->capabilities & CEC_CAP_RC) ||
1737 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
1738 break;
1739#if IS_REACHABLE(CONFIG_RC_CORE)
1740 rc_keyup(adap->rc);
1741#endif
1742 break;
1743
1744
1745
1746
1747
1748 case CEC_MSG_GET_CEC_VERSION:
1749 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1750 return cec_transmit_msg(adap, &tx_cec_msg, false);
1751
1752 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1753
1754 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1755 return 0;
1756 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1757 return cec_transmit_msg(adap, &tx_cec_msg, false);
1758
1759 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1760 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1761 return cec_feature_abort(adap, msg);
1762 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1763 return cec_transmit_msg(adap, &tx_cec_msg, false);
1764
1765 case CEC_MSG_ABORT:
1766
1767 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1768 return 0;
1769 return cec_feature_refused(adap, msg);
1770
1771 case CEC_MSG_GIVE_OSD_NAME: {
1772 if (adap->log_addrs.osd_name[0] == 0)
1773 return cec_feature_abort(adap, msg);
1774 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1775 return cec_transmit_msg(adap, &tx_cec_msg, false);
1776 }
1777
1778 case CEC_MSG_GIVE_FEATURES:
1779 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1780 return cec_feature_abort(adap, msg);
1781 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1782 return cec_transmit_msg(adap, &tx_cec_msg, false);
1783
1784 default:
1785
1786
1787
1788
1789 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
1790 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1791 return cec_feature_abort(adap, msg);
1792 break;
1793 }
1794
1795skip_processing:
1796
1797 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
1798 return 0;
1799
1800
1801
1802
1803
1804 if (adap->cec_follower)
1805 cec_queue_msg_fh(adap->cec_follower, msg);
1806 else
1807 cec_queue_msg_followers(adap, msg);
1808 return 0;
1809}
1810
1811
1812
1813
1814
1815
1816int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1817{
1818 int ret = 0;
1819
1820 if (adap->monitor_all_cnt == 0)
1821 ret = call_op(adap, adap_monitor_all_enable, 1);
1822 if (ret == 0)
1823 adap->monitor_all_cnt++;
1824 return ret;
1825}
1826
1827void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1828{
1829 adap->monitor_all_cnt--;
1830 if (adap->monitor_all_cnt == 0)
1831 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1832}
1833
1834#ifdef CONFIG_MEDIA_CEC_DEBUG
1835
1836
1837
1838
1839int cec_adap_status(struct seq_file *file, void *priv)
1840{
1841 struct cec_adapter *adap = dev_get_drvdata(file->private);
1842 struct cec_data *data;
1843
1844 mutex_lock(&adap->lock);
1845 seq_printf(file, "configured: %d\n", adap->is_configured);
1846 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1847 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1848 cec_phys_addr_exp(adap->phys_addr));
1849 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1850 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1851 if (adap->cec_follower)
1852 seq_printf(file, "has CEC follower%s\n",
1853 adap->passthrough ? " (in passthrough mode)" : "");
1854 if (adap->cec_initiator)
1855 seq_puts(file, "has CEC initiator\n");
1856 if (adap->monitor_all_cnt)
1857 seq_printf(file, "file handles in Monitor All mode: %u\n",
1858 adap->monitor_all_cnt);
1859 data = adap->transmitting;
1860 if (data)
1861 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1862 data->msg.len, data->msg.msg, data->msg.reply,
1863 data->msg.timeout);
1864 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
1865 list_for_each_entry(data, &adap->transmit_queue, list) {
1866 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1867 data->msg.len, data->msg.msg, data->msg.reply,
1868 data->msg.timeout);
1869 }
1870 list_for_each_entry(data, &adap->wait_queue, list) {
1871 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1872 data->msg.len, data->msg.msg, data->msg.reply,
1873 data->msg.timeout);
1874 }
1875
1876 call_void_op(adap, adap_status, file);
1877 mutex_unlock(&adap->lock);
1878 return 0;
1879}
1880#endif
1881