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