blob: 7bd4d7d8713c9dc99e98840e822587a455706f16 [file] [log] [blame]
Hans Verkuil9881fe02016-06-25 09:44:16 -03001/*
2 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/kmod.h>
25#include <linux/ktime.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/string.h>
29#include <linux/types.h>
30
Hans Verkuil23111ec2017-06-07 11:46:08 -030031#include <drm/drm_edid.h>
32
Hans Verkuil9881fe02016-06-25 09:44:16 -030033#include "cec-priv.h"
34
Hans Verkuil52bc30f2016-12-09 11:36:03 -020035static void cec_fill_msg_report_features(struct cec_adapter *adap,
36 struct cec_msg *msg,
37 unsigned int la_idx);
Hans Verkuil9881fe02016-06-25 09:44:16 -030038
39/*
40 * 400 ms is the time it takes for one 16 byte message to be
41 * transferred and 5 is the maximum number of retries. Add
42 * another 100 ms as a margin. So if the transmit doesn't
43 * finish before that time something is really wrong and we
44 * have to time out.
45 *
46 * This is a sign that something it really wrong and a warning
47 * will be issued.
48 */
49#define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
50
51#define call_op(adap, op, arg...) \
52 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
53
54#define call_void_op(adap, op, arg...) \
55 do { \
56 if (adap->ops->op) \
57 adap->ops->op(adap, ## arg); \
58 } while (0)
59
60static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
61{
62 int i;
63
64 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
65 if (adap->log_addrs.log_addr[i] == log_addr)
66 return i;
67 return -1;
68}
69
70static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
71{
72 int i = cec_log_addr2idx(adap, log_addr);
73
74 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
75}
76
77/*
78 * Queue a new event for this filehandle. If ts == 0, then set it
79 * to the current time.
80 *
81 * The two events that are currently defined do not need to keep track
82 * of intermediate events, so no actual queue of events is needed,
83 * instead just store the latest state and the total number of lost
84 * messages.
85 *
86 * Should new events be added in the future that require intermediate
87 * results to be queued as well, then a proper queue data structure is
88 * required. But until then, just keep it simple.
89 */
90void cec_queue_event_fh(struct cec_fh *fh,
91 const struct cec_event *new_ev, u64 ts)
92{
93 struct cec_event *ev = &fh->events[new_ev->event - 1];
94
95 if (ts == 0)
96 ts = ktime_get_ns();
97
98 mutex_lock(&fh->lock);
99 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
100 fh->pending_events & (1 << new_ev->event)) {
101 /*
102 * If there is already a lost_msgs event, then just
103 * update the lost_msgs count. This effectively
104 * merges the old and new events into one.
105 */
106 ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
107 goto unlock;
108 }
109
110 /*
111 * Intermediate states are not interesting, so just
112 * overwrite any older event.
113 */
114 *ev = *new_ev;
115 ev->ts = ts;
116 fh->pending_events |= 1 << new_ev->event;
117
118unlock:
119 mutex_unlock(&fh->lock);
120 wake_up_interruptible(&fh->wait);
121}
122
123/* Queue a new event for all open filehandles. */
124static void cec_queue_event(struct cec_adapter *adap,
125 const struct cec_event *ev)
126{
127 u64 ts = ktime_get_ns();
128 struct cec_fh *fh;
129
Hans Verkuil62148f02016-08-02 08:11:00 -0300130 mutex_lock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300131 list_for_each_entry(fh, &adap->devnode.fhs, list)
132 cec_queue_event_fh(fh, ev, ts);
Hans Verkuil62148f02016-08-02 08:11:00 -0300133 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300134}
135
136/*
137 * Queue a new message for this filehandle. If there is no more room
138 * in the queue, then send the LOST_MSGS event instead.
139 */
140static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
141{
142 static const struct cec_event ev_lost_msg = {
Andrew Morton6a91d602016-07-12 15:30:55 -0700143 .ts = 0,
Hans Verkuil9881fe02016-06-25 09:44:16 -0300144 .event = CEC_EVENT_LOST_MSGS,
Andrew Morton6a91d602016-07-12 15:30:55 -0700145 .flags = 0,
146 {
147 .lost_msgs.lost_msgs = 1,
148 },
Hans Verkuil9881fe02016-06-25 09:44:16 -0300149 };
150 struct cec_msg_entry *entry;
151
152 mutex_lock(&fh->lock);
153 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
154 if (!entry)
155 goto lost_msgs;
156
157 entry->msg = *msg;
158 /* Add new msg at the end of the queue */
159 list_add_tail(&entry->list, &fh->msgs);
160
161 /*
Hans Verkuil11065f82016-07-17 11:40:05 -0300162 * if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
Hans Verkuil9881fe02016-06-25 09:44:16 -0300163 * messages, drop the oldest one and send a lost message event.
164 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300165 if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300166 list_del(&entry->list);
167 goto lost_msgs;
168 }
169 fh->queued_msgs++;
170 mutex_unlock(&fh->lock);
171 wake_up_interruptible(&fh->wait);
172 return;
173
174lost_msgs:
175 mutex_unlock(&fh->lock);
176 cec_queue_event_fh(fh, &ev_lost_msg, 0);
177}
178
179/*
180 * Queue the message for those filehandles that are in monitor mode.
181 * If valid_la is true (this message is for us or was sent by us),
182 * then pass it on to any monitoring filehandle. If this message
183 * isn't for us or from us, then only give it to filehandles that
184 * are in MONITOR_ALL mode.
185 *
186 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
187 * set and the CEC adapter was placed in 'monitor all' mode.
188 */
189static void cec_queue_msg_monitor(struct cec_adapter *adap,
190 const struct cec_msg *msg,
191 bool valid_la)
192{
193 struct cec_fh *fh;
194 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
195 CEC_MODE_MONITOR_ALL;
196
Hans Verkuil62148f02016-08-02 08:11:00 -0300197 mutex_lock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300198 list_for_each_entry(fh, &adap->devnode.fhs, list) {
199 if (fh->mode_follower >= monitor_mode)
200 cec_queue_msg_fh(fh, msg);
201 }
Hans Verkuil62148f02016-08-02 08:11:00 -0300202 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300203}
204
205/*
206 * Queue the message for follower filehandles.
207 */
208static void cec_queue_msg_followers(struct cec_adapter *adap,
209 const struct cec_msg *msg)
210{
211 struct cec_fh *fh;
212
Hans Verkuil62148f02016-08-02 08:11:00 -0300213 mutex_lock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300214 list_for_each_entry(fh, &adap->devnode.fhs, list) {
215 if (fh->mode_follower == CEC_MODE_FOLLOWER)
216 cec_queue_msg_fh(fh, msg);
217 }
Hans Verkuil62148f02016-08-02 08:11:00 -0300218 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300219}
220
221/* Notify userspace of an adapter state change. */
222static void cec_post_state_event(struct cec_adapter *adap)
223{
224 struct cec_event ev = {
225 .event = CEC_EVENT_STATE_CHANGE,
226 };
227
228 ev.state_change.phys_addr = adap->phys_addr;
229 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
230 cec_queue_event(adap, &ev);
231}
232
233/*
234 * A CEC transmit (and a possible wait for reply) completed.
235 * If this was in blocking mode, then complete it, otherwise
236 * queue the message for userspace to dequeue later.
237 *
238 * This function is called with adap->lock held.
239 */
240static void cec_data_completed(struct cec_data *data)
241{
242 /*
243 * Delete this transmit from the filehandle's xfer_list since
244 * we're done with it.
245 *
246 * Note that if the filehandle is closed before this transmit
247 * finished, then the release() function will set data->fh to NULL.
248 * Without that we would be referring to a closed filehandle.
249 */
250 if (data->fh)
251 list_del(&data->xfer_list);
252
253 if (data->blocking) {
254 /*
255 * Someone is blocking so mark the message as completed
256 * and call complete.
257 */
258 data->completed = true;
259 complete(&data->c);
260 } else {
261 /*
262 * No blocking, so just queue the message if needed and
263 * free the memory.
264 */
265 if (data->fh)
266 cec_queue_msg_fh(data->fh, &data->msg);
267 kfree(data);
268 }
269}
270
271/*
272 * A pending CEC transmit needs to be cancelled, either because the CEC
273 * adapter is disabled or the transmit takes an impossibly long time to
274 * finish.
275 *
276 * This function is called with adap->lock held.
277 */
278static void cec_data_cancel(struct cec_data *data)
279{
280 /*
281 * It's either the current transmit, or it is a pending
282 * transmit. Take the appropriate action to clear it.
283 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300284 if (data->adap->transmitting == data) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300285 data->adap->transmitting = NULL;
Hans Verkuil11065f82016-07-17 11:40:05 -0300286 } else {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300287 list_del_init(&data->list);
Hans Verkuil11065f82016-07-17 11:40:05 -0300288 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
289 data->adap->transmit_queue_sz--;
290 }
Hans Verkuil9881fe02016-06-25 09:44:16 -0300291
292 /* Mark it as an error */
Hans Verkuil980e0b362016-07-12 11:10:42 -0300293 data->msg.tx_ts = ktime_get_ns();
Hans Verkuil12047612016-12-09 11:14:32 -0200294 data->msg.tx_status |= CEC_TX_STATUS_ERROR |
295 CEC_TX_STATUS_MAX_RETRIES;
296 data->msg.tx_error_cnt++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300297 data->attempts = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300298 /* Queue transmitted message for monitoring purposes */
299 cec_queue_msg_monitor(data->adap, &data->msg, 1);
300
301 cec_data_completed(data);
302}
303
304/*
Hans Verkuilb39f93e2017-02-10 13:02:31 -0200305 * Flush all pending transmits and cancel any pending timeout work.
306 *
307 * This function is called with adap->lock held.
308 */
309static void cec_flush(struct cec_adapter *adap)
310{
311 struct cec_data *data, *n;
312
313 /*
314 * If the adapter is disabled, or we're asked to stop,
315 * then cancel any pending transmits.
316 */
317 while (!list_empty(&adap->transmit_queue)) {
318 data = list_first_entry(&adap->transmit_queue,
319 struct cec_data, list);
320 cec_data_cancel(data);
321 }
322 if (adap->transmitting)
323 cec_data_cancel(adap->transmitting);
324
325 /* Cancel the pending timeout work. */
326 list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
327 if (cancel_delayed_work(&data->work))
328 cec_data_cancel(data);
329 /*
330 * If cancel_delayed_work returned false, then
331 * the cec_wait_timeout function is running,
332 * which will call cec_data_completed. So no
333 * need to do anything special in that case.
334 */
335 }
336}
337
338/*
Hans Verkuil9881fe02016-06-25 09:44:16 -0300339 * Main CEC state machine
340 *
341 * Wait until the thread should be stopped, or we are not transmitting and
342 * a new transmit message is queued up, in which case we start transmitting
343 * that message. When the adapter finished transmitting the message it will
344 * call cec_transmit_done().
345 *
346 * If the adapter is disabled, then remove all queued messages instead.
347 *
348 * If the current transmit times out, then cancel that transmit.
349 */
350int cec_thread_func(void *_adap)
351{
352 struct cec_adapter *adap = _adap;
353
354 for (;;) {
355 unsigned int signal_free_time;
356 struct cec_data *data;
357 bool timeout = false;
358 u8 attempts;
359
360 if (adap->transmitting) {
361 int err;
362
363 /*
364 * We are transmitting a message, so add a timeout
365 * to prevent the state machine to get stuck waiting
366 * for this message to finalize and add a check to
367 * see if the adapter is disabled in which case the
368 * transmit should be canceled.
369 */
370 err = wait_event_interruptible_timeout(adap->kthread_waitq,
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300371 (adap->needs_hpd &&
372 (!adap->is_configured && !adap->is_configuring)) ||
Hans Verkuil9881fe02016-06-25 09:44:16 -0300373 kthread_should_stop() ||
Hans Verkuil9881fe02016-06-25 09:44:16 -0300374 (!adap->transmitting &&
375 !list_empty(&adap->transmit_queue)),
376 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
377 timeout = err == 0;
378 } else {
379 /* Otherwise we just wait for something to happen. */
380 wait_event_interruptible(adap->kthread_waitq,
381 kthread_should_stop() ||
382 (!adap->transmitting &&
383 !list_empty(&adap->transmit_queue)));
384 }
385
386 mutex_lock(&adap->lock);
387
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300388 if ((adap->needs_hpd &&
389 (!adap->is_configured && !adap->is_configuring)) ||
390 kthread_should_stop()) {
Hans Verkuilb39f93e2017-02-10 13:02:31 -0200391 cec_flush(adap);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300392 goto unlock;
393 }
394
395 if (adap->transmitting && timeout) {
396 /*
Hans Verkuilbb789e02017-07-11 03:30:34 -0300397 * If we timeout, then log that. Normally this does
398 * not happen and it is an indication of a faulty CEC
399 * adapter driver, or the CEC bus is in some weird
400 * state. On rare occasions it can happen if there is
401 * so much traffic on the bus that the adapter was
402 * unable to transmit for CEC_XFER_TIMEOUT_MS (2.1s).
Hans Verkuil9881fe02016-06-25 09:44:16 -0300403 */
Hans Verkuilbb789e02017-07-11 03:30:34 -0300404 dprintk(1, "%s: message %*ph timed out\n", __func__,
Hans Verkuil9881fe02016-06-25 09:44:16 -0300405 adap->transmitting->msg.len,
406 adap->transmitting->msg.msg);
Hans Verkuilbb789e02017-07-11 03:30:34 -0300407 adap->tx_timeouts++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300408 /* Just give up on this. */
409 cec_data_cancel(adap->transmitting);
410 goto unlock;
411 }
412
413 /*
414 * If we are still transmitting, or there is nothing new to
415 * transmit, then just continue waiting.
416 */
417 if (adap->transmitting || list_empty(&adap->transmit_queue))
418 goto unlock;
419
420 /* Get a new message to transmit */
421 data = list_first_entry(&adap->transmit_queue,
422 struct cec_data, list);
423 list_del_init(&data->list);
Hans Verkuil11065f82016-07-17 11:40:05 -0300424 adap->transmit_queue_sz--;
Hans Verkuil533a3f72017-02-10 13:02:31 -0200425
Hans Verkuil9881fe02016-06-25 09:44:16 -0300426 /* Make this the current transmitting message */
427 adap->transmitting = data;
428
429 /*
430 * Suggested number of attempts as per the CEC 2.0 spec:
431 * 4 attempts is the default, except for 'secondary poll
432 * messages', i.e. poll messages not sent during the adapter
433 * configuration phase when it allocates logical addresses.
434 */
435 if (data->msg.len == 1 && adap->is_configured)
436 attempts = 2;
437 else
438 attempts = 4;
439
440 /* Set the suggested signal free time */
441 if (data->attempts) {
442 /* should be >= 3 data bit periods for a retry */
443 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
444 } else if (data->new_initiator) {
445 /* should be >= 5 data bit periods for new initiator */
446 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
447 } else {
448 /*
449 * should be >= 7 data bit periods for sending another
450 * frame immediately after another.
451 */
452 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
453 }
454 if (data->attempts == 0)
455 data->attempts = attempts;
456
457 /* Tell the adapter to transmit, cancel on error */
458 if (adap->ops->adap_transmit(adap, data->attempts,
459 signal_free_time, &data->msg))
460 cec_data_cancel(data);
461
462unlock:
463 mutex_unlock(&adap->lock);
464
465 if (kthread_should_stop())
466 break;
467 }
468 return 0;
469}
470
471/*
472 * Called by the CEC adapter if a transmit finished.
473 */
474void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
475 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
476{
477 struct cec_data *data;
478 struct cec_msg *msg;
Hans Verkuil980e0b362016-07-12 11:10:42 -0300479 u64 ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300480
Hans Verkuila7a04b52017-05-31 06:50:26 -0300481 dprintk(2, "%s: status %02x\n", __func__, status);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300482 mutex_lock(&adap->lock);
483 data = adap->transmitting;
484 if (!data) {
485 /*
486 * This can happen if a transmit was issued and the cable is
487 * unplugged while the transmit is ongoing. Ignore this
488 * transmit in that case.
489 */
Hans Verkuila7a04b52017-05-31 06:50:26 -0300490 dprintk(1, "%s was called without an ongoing transmit!\n",
491 __func__);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300492 goto unlock;
493 }
494
495 msg = &data->msg;
496
497 /* Drivers must fill in the status! */
498 WARN_ON(status == 0);
Hans Verkuil980e0b362016-07-12 11:10:42 -0300499 msg->tx_ts = ts;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300500 msg->tx_status |= status;
501 msg->tx_arb_lost_cnt += arb_lost_cnt;
502 msg->tx_nack_cnt += nack_cnt;
503 msg->tx_low_drive_cnt += low_drive_cnt;
504 msg->tx_error_cnt += error_cnt;
505
506 /* Mark that we're done with this transmit */
507 adap->transmitting = NULL;
508
509 /*
510 * If there are still retry attempts left and there was an error and
511 * the hardware didn't signal that it retried itself (by setting
512 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
513 */
514 if (data->attempts > 1 &&
515 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
516 /* Retry this message */
517 data->attempts--;
Hans Verkuila7a04b52017-05-31 06:50:26 -0300518 if (msg->timeout)
519 dprintk(2, "retransmit: %*ph (attempts: %d, wait for 0x%02x)\n",
520 msg->len, msg->msg, data->attempts, msg->reply);
521 else
522 dprintk(2, "retransmit: %*ph (attempts: %d)\n",
523 msg->len, msg->msg, data->attempts);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300524 /* Add the message in front of the transmit queue */
525 list_add(&data->list, &adap->transmit_queue);
Hans Verkuil11065f82016-07-17 11:40:05 -0300526 adap->transmit_queue_sz++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300527 goto wake_thread;
528 }
529
530 data->attempts = 0;
531
532 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
533 if (!(status & CEC_TX_STATUS_OK))
534 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
535
536 /* Queue transmitted message for monitoring purposes */
537 cec_queue_msg_monitor(adap, msg, 1);
538
Hans Verkuil86e35772016-07-13 04:33:58 -0300539 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
540 msg->timeout) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300541 /*
542 * Queue the message into the wait queue if we want to wait
543 * for a reply.
544 */
545 list_add_tail(&data->list, &adap->wait_queue);
546 schedule_delayed_work(&data->work,
547 msecs_to_jiffies(msg->timeout));
548 } else {
549 /* Otherwise we're done */
550 cec_data_completed(data);
551 }
552
553wake_thread:
554 /*
555 * Wake up the main thread to see if another message is ready
556 * for transmitting or to retry the current message.
557 */
558 wake_up_interruptible(&adap->kthread_waitq);
559unlock:
560 mutex_unlock(&adap->lock);
561}
562EXPORT_SYMBOL_GPL(cec_transmit_done);
563
Hans Verkuilc94cdc12017-06-07 11:46:10 -0300564void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status)
565{
566 switch (status) {
567 case CEC_TX_STATUS_OK:
568 cec_transmit_done(adap, status, 0, 0, 0, 0);
569 return;
570 case CEC_TX_STATUS_ARB_LOST:
571 cec_transmit_done(adap, status, 1, 0, 0, 0);
572 return;
573 case CEC_TX_STATUS_NACK:
574 cec_transmit_done(adap, status, 0, 1, 0, 0);
575 return;
576 case CEC_TX_STATUS_LOW_DRIVE:
577 cec_transmit_done(adap, status, 0, 0, 1, 0);
578 return;
579 case CEC_TX_STATUS_ERROR:
580 cec_transmit_done(adap, status, 0, 0, 0, 1);
581 return;
582 default:
583 /* Should never happen */
584 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
585 return;
586 }
587}
588EXPORT_SYMBOL_GPL(cec_transmit_attempt_done);
589
Hans Verkuil9881fe02016-06-25 09:44:16 -0300590/*
591 * Called when waiting for a reply times out.
592 */
593static void cec_wait_timeout(struct work_struct *work)
594{
595 struct cec_data *data = container_of(work, struct cec_data, work.work);
596 struct cec_adapter *adap = data->adap;
597
598 mutex_lock(&adap->lock);
599 /*
600 * Sanity check in case the timeout and the arrival of the message
601 * happened at the same time.
602 */
603 if (list_empty(&data->list))
604 goto unlock;
605
606 /* Mark the message as timed out */
607 list_del_init(&data->list);
Hans Verkuil980e0b362016-07-12 11:10:42 -0300608 data->msg.rx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300609 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
610 cec_data_completed(data);
611unlock:
612 mutex_unlock(&adap->lock);
613}
614
615/*
616 * Transmit a message. The fh argument may be NULL if the transmit is not
617 * associated with a specific filehandle.
618 *
619 * This function is called with adap->lock held.
620 */
621int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
622 struct cec_fh *fh, bool block)
623{
624 struct cec_data *data;
625 u8 last_initiator = 0xff;
626 unsigned int timeout;
627 int res = 0;
628
Hans Verkuil40df3a72016-07-16 09:44:13 -0300629 msg->rx_ts = 0;
630 msg->tx_ts = 0;
631 msg->rx_status = 0;
632 msg->tx_status = 0;
633 msg->tx_arb_lost_cnt = 0;
634 msg->tx_nack_cnt = 0;
635 msg->tx_low_drive_cnt = 0;
636 msg->tx_error_cnt = 0;
Hans Verkuil15e809e2017-07-06 11:09:52 -0300637 msg->sequence = 0;
Hans Verkuil40df3a72016-07-16 09:44:13 -0300638
Hans Verkuil9881fe02016-06-25 09:44:16 -0300639 if (msg->reply && msg->timeout == 0) {
640 /* Make sure the timeout isn't 0. */
641 msg->timeout = 1000;
642 }
Hans Verkuil7ae2a882016-11-04 07:52:11 -0200643 if (msg->timeout)
644 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
645 else
646 msg->flags = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300647
648 /* Sanity checks */
649 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
Hans Verkuil5a137df2017-02-27 10:54:09 -0300650 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300651 return -EINVAL;
652 }
653 if (msg->timeout && msg->len == 1) {
Hans Verkuil5a137df2017-02-27 10:54:09 -0300654 dprintk(1, "%s: can't reply for poll msg\n", __func__);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300655 return -EINVAL;
656 }
Hans Verkuil045344c2016-07-17 05:16:40 -0300657 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300658 if (msg->len == 1) {
Hans Verkuil42980da2017-02-11 09:24:46 -0200659 if (cec_msg_destination(msg) == 0xf) {
Hans Verkuil5a137df2017-02-27 10:54:09 -0300660 dprintk(1, "%s: invalid poll message\n", __func__);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300661 return -EINVAL;
662 }
663 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
664 /*
665 * If the destination is a logical address our adapter
666 * has already claimed, then just NACK this.
667 * It depends on the hardware what it will do with a
668 * POLL to itself (some OK this), so it is just as
669 * easy to handle it here so the behavior will be
670 * consistent.
671 */
Hans Verkuil9a3f14e2016-07-15 11:13:49 -0300672 msg->tx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300673 msg->tx_status = CEC_TX_STATUS_NACK |
674 CEC_TX_STATUS_MAX_RETRIES;
675 msg->tx_nack_cnt = 1;
Hans Verkuil15e809e2017-07-06 11:09:52 -0300676 msg->sequence = ++adap->sequence;
677 if (!msg->sequence)
678 msg->sequence = ++adap->sequence;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300679 return 0;
680 }
681 }
682 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
683 cec_has_log_addr(adap, cec_msg_destination(msg))) {
Hans Verkuil5a137df2017-02-27 10:54:09 -0300684 dprintk(1, "%s: destination is the adapter itself\n", __func__);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300685 return -EINVAL;
686 }
Hans Verkuil42980da2017-02-11 09:24:46 -0200687 if (msg->len > 1 && adap->is_configured &&
Hans Verkuil9881fe02016-06-25 09:44:16 -0300688 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
Hans Verkuil5a137df2017-02-27 10:54:09 -0300689 dprintk(1, "%s: initiator has unknown logical address %d\n",
690 __func__, cec_msg_initiator(msg));
Hans Verkuil9881fe02016-06-25 09:44:16 -0300691 return -EINVAL;
692 }
Hans Verkuil25c21072017-02-27 10:54:09 -0300693 if (!adap->is_configured && !adap->is_configuring) {
Hans Verkuilf902c1e2017-06-07 11:46:12 -0300694 if (adap->needs_hpd || msg->msg[0] != 0xf0) {
Hans Verkuil25c21072017-02-27 10:54:09 -0300695 dprintk(1, "%s: adapter is unconfigured\n", __func__);
696 return -ENONET;
697 }
698 if (msg->reply) {
699 dprintk(1, "%s: invalid msg->reply\n", __func__);
700 return -EINVAL;
701 }
702 }
Hans Verkuil9881fe02016-06-25 09:44:16 -0300703
Hans Verkuil25c21072017-02-27 10:54:09 -0300704 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
705 dprintk(1, "%s: transmit queue full\n", __func__);
Hans Verkuil11065f82016-07-17 11:40:05 -0300706 return -EBUSY;
Hans Verkuil25c21072017-02-27 10:54:09 -0300707 }
Hans Verkuil11065f82016-07-17 11:40:05 -0300708
Hans Verkuil9881fe02016-06-25 09:44:16 -0300709 data = kzalloc(sizeof(*data), GFP_KERNEL);
710 if (!data)
711 return -ENOMEM;
712
Hans Verkuil15e809e2017-07-06 11:09:52 -0300713 msg->sequence = ++adap->sequence;
714 if (!msg->sequence)
715 msg->sequence = ++adap->sequence;
716
Hans Verkuil9881fe02016-06-25 09:44:16 -0300717 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
718 msg->msg[2] = adap->phys_addr >> 8;
719 msg->msg[3] = adap->phys_addr & 0xff;
720 }
721
722 if (msg->timeout)
Hans Verkuil5a137df2017-02-27 10:54:09 -0300723 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
724 __func__, msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
Hans Verkuil9881fe02016-06-25 09:44:16 -0300725 else
Hans Verkuil5a137df2017-02-27 10:54:09 -0300726 dprintk(2, "%s: %*ph%s\n",
727 __func__, msg->len, msg->msg, !block ? " (nb)" : "");
Hans Verkuil9881fe02016-06-25 09:44:16 -0300728
Hans Verkuil9881fe02016-06-25 09:44:16 -0300729 data->msg = *msg;
730 data->fh = fh;
731 data->adap = adap;
732 data->blocking = block;
733
734 /*
735 * Determine if this message follows a message from the same
736 * initiator. Needed to determine the free signal time later on.
737 */
738 if (msg->len > 1) {
739 if (!(list_empty(&adap->transmit_queue))) {
740 const struct cec_data *last;
741
742 last = list_last_entry(&adap->transmit_queue,
743 const struct cec_data, list);
744 last_initiator = cec_msg_initiator(&last->msg);
745 } else if (adap->transmitting) {
746 last_initiator =
747 cec_msg_initiator(&adap->transmitting->msg);
748 }
749 }
750 data->new_initiator = last_initiator != cec_msg_initiator(msg);
751 init_completion(&data->c);
752 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
753
Hans Verkuil9881fe02016-06-25 09:44:16 -0300754 if (fh)
755 list_add_tail(&data->xfer_list, &fh->xfer_list);
Hans Verkuil533a3f72017-02-10 13:02:31 -0200756
Hans Verkuil9881fe02016-06-25 09:44:16 -0300757 list_add_tail(&data->list, &adap->transmit_queue);
Hans Verkuil11065f82016-07-17 11:40:05 -0300758 adap->transmit_queue_sz++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300759 if (!adap->transmitting)
760 wake_up_interruptible(&adap->kthread_waitq);
761
762 /* All done if we don't need to block waiting for completion */
763 if (!block)
764 return 0;
765
766 /*
767 * If we don't get a completion before this time something is really
768 * wrong and we time out.
769 */
770 timeout = CEC_XFER_TIMEOUT_MS;
771 /* Add the requested timeout if we have to wait for a reply as well */
772 if (msg->timeout)
773 timeout += msg->timeout;
774
775 /*
776 * Release the lock and wait, retake the lock afterwards.
777 */
778 mutex_unlock(&adap->lock);
779 res = wait_for_completion_killable_timeout(&data->c,
780 msecs_to_jiffies(timeout));
781 mutex_lock(&adap->lock);
782
783 if (data->completed) {
784 /* The transmit completed (possibly with an error) */
785 *msg = data->msg;
786 kfree(data);
787 return 0;
788 }
789 /*
790 * The wait for completion timed out or was interrupted, so mark this
791 * as non-blocking and disconnect from the filehandle since it is
792 * still 'in flight'. When it finally completes it will just drop the
793 * result silently.
794 */
795 data->blocking = false;
796 if (data->fh)
797 list_del(&data->xfer_list);
798 data->fh = NULL;
799
800 if (res == 0) { /* timed out */
801 /* Check if the reply or the transmit failed */
802 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
803 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
804 else
805 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
806 }
807 return res > 0 ? 0 : res;
808}
809
810/* Helper function to be used by drivers and this framework. */
811int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
812 bool block)
813{
814 int ret;
815
816 mutex_lock(&adap->lock);
817 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
818 mutex_unlock(&adap->lock);
819 return ret;
820}
821EXPORT_SYMBOL_GPL(cec_transmit_msg);
822
823/*
824 * I don't like forward references but without this the low-level
825 * cec_received_msg() function would come after a bunch of high-level
826 * CEC protocol handling functions. That was very confusing.
827 */
828static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
829 bool is_reply);
830
Hans Verkuil3074fe42016-11-01 10:48:22 -0200831#define DIRECTED 0x80
832#define BCAST1_4 0x40
833#define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
834#define BCAST (BCAST1_4 | BCAST2_0)
835#define BOTH (BCAST | DIRECTED)
836
837/*
838 * Specify minimum length and whether the message is directed, broadcast
839 * or both. Messages that do not match the criteria are ignored as per
840 * the CEC specification.
841 */
842static const u8 cec_msg_size[256] = {
843 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
844 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
845 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
846 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
847 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
848 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
849 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
850 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
851 [CEC_MSG_STANDBY] = 2 | BOTH,
852 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
853 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
854 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
855 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
856 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
857 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
858 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
859 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
860 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
861 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
862 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
863 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
864 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
865 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
866 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
867 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
868 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
869 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
870 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
871 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
872 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
873 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
874 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
875 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
876 [CEC_MSG_PLAY] = 3 | DIRECTED,
877 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
878 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
879 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
880 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
881 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
882 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
883 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
884 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
885 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
886 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
887 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
888 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
889 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
890 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
891 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
892 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
893 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
894 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
895 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
896 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
897 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
898 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
899 [CEC_MSG_ABORT] = 2 | DIRECTED,
900 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
901 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
902 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
903 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
904 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
905 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
906 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
907 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
908 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
909 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
910 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
911 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
912 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
913 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
914 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
915 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
Hans Verkuilf3854972016-12-06 11:17:12 -0200916 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
Hans Verkuil3074fe42016-11-01 10:48:22 -0200917 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
918};
919
Hans Verkuil9881fe02016-06-25 09:44:16 -0300920/* Called by the CEC adapter if a message is received */
921void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
922{
923 struct cec_data *data;
924 u8 msg_init = cec_msg_initiator(msg);
925 u8 msg_dest = cec_msg_destination(msg);
Hans Verkuil3074fe42016-11-01 10:48:22 -0200926 u8 cmd = msg->msg[1];
Hans Verkuil9881fe02016-06-25 09:44:16 -0300927 bool is_reply = false;
928 bool valid_la = true;
Hans Verkuil3074fe42016-11-01 10:48:22 -0200929 u8 min_len = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300930
Hans Verkuil52d802d2016-07-12 11:10:41 -0300931 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
932 return;
933
Hans Verkuil3f98da92016-11-21 13:15:45 -0200934 /*
935 * Some CEC adapters will receive the messages that they transmitted.
936 * This test filters out those messages by checking if we are the
937 * initiator, and just returning in that case.
938 *
939 * Note that this won't work if this is an Unregistered device.
940 *
941 * It is bad practice if the hardware receives the message that it
942 * transmitted and luckily most CEC adapters behave correctly in this
943 * respect.
944 */
945 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
946 cec_has_log_addr(adap, msg_init))
947 return;
948
Hans Verkuil980e0b362016-07-12 11:10:42 -0300949 msg->rx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300950 msg->rx_status = CEC_RX_STATUS_OK;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300951 msg->sequence = msg->reply = msg->timeout = 0;
Hans Verkuil980e0b362016-07-12 11:10:42 -0300952 msg->tx_status = 0;
953 msg->tx_ts = 0;
Hans Verkuil8991a63d2016-11-09 12:10:59 -0200954 msg->tx_arb_lost_cnt = 0;
955 msg->tx_nack_cnt = 0;
956 msg->tx_low_drive_cnt = 0;
957 msg->tx_error_cnt = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300958 msg->flags = 0;
Hans Verkuil045344c2016-07-17 05:16:40 -0300959 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300960
Hans Verkuil980e0b362016-07-12 11:10:42 -0300961 mutex_lock(&adap->lock);
Hans Verkuila7a04b52017-05-31 06:50:26 -0300962 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300963
964 /* Check if this message was for us (directed or broadcast). */
965 if (!cec_msg_is_broadcast(msg))
966 valid_la = cec_has_log_addr(adap, msg_dest);
967
Hans Verkuil3074fe42016-11-01 10:48:22 -0200968 /*
969 * Check if the length is not too short or if the message is a
970 * broadcast message where a directed message was expected or
971 * vice versa. If so, then the message has to be ignored (according
972 * to section CEC 7.3 and CEC 12.2).
973 */
974 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
975 u8 dir_fl = cec_msg_size[cmd] & BOTH;
976
977 min_len = cec_msg_size[cmd] & 0x1f;
978 if (msg->len < min_len)
979 valid_la = false;
980 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
981 valid_la = false;
982 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
983 valid_la = false;
984 else if (cec_msg_is_broadcast(msg) &&
985 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
986 !(dir_fl & BCAST2_0))
987 valid_la = false;
988 }
989 if (valid_la && min_len) {
990 /* These messages have special length requirements */
991 switch (cmd) {
992 case CEC_MSG_TIMER_STATUS:
993 if (msg->msg[2] & 0x10) {
994 switch (msg->msg[2] & 0xf) {
995 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
996 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
997 if (msg->len < 5)
998 valid_la = false;
999 break;
1000 }
1001 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
1002 if (msg->len < 5)
1003 valid_la = false;
1004 }
1005 break;
1006 case CEC_MSG_RECORD_ON:
1007 switch (msg->msg[2]) {
1008 case CEC_OP_RECORD_SRC_OWN:
1009 break;
1010 case CEC_OP_RECORD_SRC_DIGITAL:
1011 if (msg->len < 10)
1012 valid_la = false;
1013 break;
1014 case CEC_OP_RECORD_SRC_ANALOG:
1015 if (msg->len < 7)
1016 valid_la = false;
1017 break;
1018 case CEC_OP_RECORD_SRC_EXT_PLUG:
1019 if (msg->len < 4)
1020 valid_la = false;
1021 break;
1022 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1023 if (msg->len < 5)
1024 valid_la = false;
1025 break;
1026 }
1027 break;
1028 }
1029 }
1030
Hans Verkuil9881fe02016-06-25 09:44:16 -03001031 /* It's a valid message and not a poll or CDC message */
Hans Verkuil3074fe42016-11-01 10:48:22 -02001032 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001033 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1034
1035 /* The aborted command is in msg[2] */
1036 if (abort)
1037 cmd = msg->msg[2];
1038
1039 /*
1040 * Walk over all transmitted messages that are waiting for a
1041 * reply.
1042 */
1043 list_for_each_entry(data, &adap->wait_queue, list) {
1044 struct cec_msg *dst = &data->msg;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001045
Hans Verkuilf5580d82016-11-01 11:07:17 -02001046 /*
1047 * The *only* CEC message that has two possible replies
1048 * is CEC_MSG_INITIATE_ARC.
1049 * In this case allow either of the two replies.
1050 */
1051 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1052 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1053 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1054 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
1055 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
1056 dst->reply = cmd;
1057
Hans Verkuil9881fe02016-06-25 09:44:16 -03001058 /* Does the command match? */
1059 if ((abort && cmd != dst->msg[1]) ||
1060 (!abort && cmd != dst->reply))
1061 continue;
1062
1063 /* Does the addressing match? */
1064 if (msg_init != cec_msg_destination(dst) &&
1065 !cec_msg_is_broadcast(dst))
1066 continue;
1067
1068 /* We got a reply */
Hans Verkuil980e0b362016-07-12 11:10:42 -03001069 memcpy(dst->msg, msg->msg, msg->len);
1070 dst->len = msg->len;
1071 dst->rx_ts = msg->rx_ts;
1072 dst->rx_status = msg->rx_status;
Hans Verkuil86e35772016-07-13 04:33:58 -03001073 if (abort)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001074 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
Hans Verkuiladc0c622016-11-01 08:55:05 -02001075 msg->flags = dst->flags;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001076 /* Remove it from the wait_queue */
1077 list_del_init(&data->list);
1078
1079 /* Cancel the pending timeout work */
1080 if (!cancel_delayed_work(&data->work)) {
1081 mutex_unlock(&adap->lock);
1082 flush_scheduled_work();
1083 mutex_lock(&adap->lock);
1084 }
1085 /*
1086 * Mark this as a reply, provided someone is still
1087 * waiting for the answer.
1088 */
1089 if (data->fh)
1090 is_reply = true;
1091 cec_data_completed(data);
1092 break;
1093 }
1094 }
1095 mutex_unlock(&adap->lock);
1096
1097 /* Pass the message on to any monitoring filehandles */
1098 cec_queue_msg_monitor(adap, msg, valid_la);
1099
1100 /* We're done if it is not for us or a poll message */
1101 if (!valid_la || msg->len <= 1)
1102 return;
1103
Hans Verkuil3e92d8b2016-08-12 13:32:07 -03001104 if (adap->log_addrs.log_addr_mask == 0)
1105 return;
1106
Hans Verkuil9881fe02016-06-25 09:44:16 -03001107 /*
1108 * Process the message on the protocol level. If is_reply is true,
1109 * then cec_receive_notify() won't pass on the reply to the listener(s)
1110 * since that was already done by cec_data_completed() above.
1111 */
1112 cec_receive_notify(adap, msg, is_reply);
1113}
1114EXPORT_SYMBOL_GPL(cec_received_msg);
1115
1116/* Logical Address Handling */
1117
1118/*
1119 * Attempt to claim a specific logical address.
1120 *
1121 * This function is called with adap->lock held.
1122 */
1123static int cec_config_log_addr(struct cec_adapter *adap,
1124 unsigned int idx,
1125 unsigned int log_addr)
1126{
1127 struct cec_log_addrs *las = &adap->log_addrs;
1128 struct cec_msg msg = { };
1129 int err;
1130
1131 if (cec_has_log_addr(adap, log_addr))
1132 return 0;
1133
1134 /* Send poll message */
1135 msg.len = 1;
Hans Verkuil42980da2017-02-11 09:24:46 -02001136 msg.msg[0] = (log_addr << 4) | log_addr;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001137 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1138
1139 /*
1140 * While trying to poll the physical address was reset
1141 * and the adapter was unconfigured, so bail out.
1142 */
1143 if (!adap->is_configuring)
1144 return -EINTR;
1145
1146 if (err)
1147 return err;
1148
1149 if (msg.tx_status & CEC_TX_STATUS_OK)
1150 return 0;
1151
1152 /*
1153 * Message not acknowledged, so this logical
1154 * address is free to use.
1155 */
1156 err = adap->ops->adap_log_addr(adap, log_addr);
1157 if (err)
1158 return err;
1159
1160 las->log_addr[idx] = log_addr;
1161 las->log_addr_mask |= 1 << log_addr;
1162 adap->phys_addrs[log_addr] = adap->phys_addr;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001163 return 1;
1164}
1165
1166/*
1167 * Unconfigure the adapter: clear all logical addresses and send
1168 * the state changed event.
1169 *
1170 * This function is called with adap->lock held.
1171 */
1172static void cec_adap_unconfigure(struct cec_adapter *adap)
1173{
Hans Verkuilf902c1e2017-06-07 11:46:12 -03001174 if (!adap->needs_hpd ||
1175 adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1176 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
Hans Verkuil9881fe02016-06-25 09:44:16 -03001177 adap->log_addrs.log_addr_mask = 0;
1178 adap->is_configuring = false;
1179 adap->is_configured = false;
1180 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
Hans Verkuil533a3f72017-02-10 13:02:31 -02001181 cec_flush(adap);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001182 wake_up_interruptible(&adap->kthread_waitq);
1183 cec_post_state_event(adap);
1184}
1185
1186/*
1187 * Attempt to claim the required logical addresses.
1188 */
1189static int cec_config_thread_func(void *arg)
1190{
1191 /* The various LAs for each type of device */
1192 static const u8 tv_log_addrs[] = {
1193 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1194 CEC_LOG_ADDR_INVALID
1195 };
1196 static const u8 record_log_addrs[] = {
1197 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1198 CEC_LOG_ADDR_RECORD_3,
1199 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1200 CEC_LOG_ADDR_INVALID
1201 };
1202 static const u8 tuner_log_addrs[] = {
1203 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1204 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1205 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1206 CEC_LOG_ADDR_INVALID
1207 };
1208 static const u8 playback_log_addrs[] = {
1209 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1210 CEC_LOG_ADDR_PLAYBACK_3,
1211 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1212 CEC_LOG_ADDR_INVALID
1213 };
1214 static const u8 audiosystem_log_addrs[] = {
1215 CEC_LOG_ADDR_AUDIOSYSTEM,
1216 CEC_LOG_ADDR_INVALID
1217 };
1218 static const u8 specific_use_log_addrs[] = {
1219 CEC_LOG_ADDR_SPECIFIC,
1220 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1221 CEC_LOG_ADDR_INVALID
1222 };
1223 static const u8 *type2addrs[6] = {
1224 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1225 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1226 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1227 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1228 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1229 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1230 };
1231 static const u16 type2mask[] = {
1232 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1233 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1234 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1235 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1236 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1237 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1238 };
1239 struct cec_adapter *adap = arg;
1240 struct cec_log_addrs *las = &adap->log_addrs;
1241 int err;
1242 int i, j;
1243
1244 mutex_lock(&adap->lock);
1245 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1246 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1247 las->log_addr_mask = 0;
1248
1249 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1250 goto configured;
1251
1252 for (i = 0; i < las->num_log_addrs; i++) {
1253 unsigned int type = las->log_addr_type[i];
1254 const u8 *la_list;
1255 u8 last_la;
1256
1257 /*
1258 * The TV functionality can only map to physical address 0.
1259 * For any other address, try the Specific functionality
1260 * instead as per the spec.
1261 */
1262 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1263 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1264
1265 la_list = type2addrs[type];
1266 last_la = las->log_addr[i];
1267 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1268 if (last_la == CEC_LOG_ADDR_INVALID ||
1269 last_la == CEC_LOG_ADDR_UNREGISTERED ||
Hans Verkuilf9f96fc2017-01-10 09:44:54 -02001270 !((1 << last_la) & type2mask[type]))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001271 last_la = la_list[0];
1272
1273 err = cec_config_log_addr(adap, i, last_la);
1274 if (err > 0) /* Reused last LA */
1275 continue;
1276
1277 if (err < 0)
1278 goto unconfigure;
1279
1280 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1281 /* Tried this one already, skip it */
1282 if (la_list[j] == last_la)
1283 continue;
1284 /* The backup addresses are CEC 2.0 specific */
1285 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1286 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1287 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1288 continue;
1289
1290 err = cec_config_log_addr(adap, i, la_list[j]);
1291 if (err == 0) /* LA is in use */
1292 continue;
1293 if (err < 0)
1294 goto unconfigure;
1295 /* Done, claimed an LA */
1296 break;
1297 }
1298
1299 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1300 dprintk(1, "could not claim LA %d\n", i);
1301 }
1302
Hans Verkuildcceb1e2016-08-10 09:24:45 -03001303 if (adap->log_addrs.log_addr_mask == 0 &&
1304 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1305 goto unconfigure;
1306
Hans Verkuil9881fe02016-06-25 09:44:16 -03001307configured:
1308 if (adap->log_addrs.log_addr_mask == 0) {
1309 /* Fall back to unregistered */
1310 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1311 las->log_addr_mask = 1 << las->log_addr[0];
Hans Verkuil0c1d61b2016-08-14 08:27:09 -03001312 for (i = 1; i < las->num_log_addrs; i++)
1313 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001314 }
Hans Verkuil7af26f82016-12-09 11:54:06 -02001315 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1316 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001317 adap->is_configured = true;
1318 adap->is_configuring = false;
1319 cec_post_state_event(adap);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001320
Hans Verkuilf60f3562016-12-09 12:00:49 -02001321 /*
1322 * Now post the Report Features and Report Physical Address broadcast
1323 * messages. Note that these are non-blocking transmits, meaning that
1324 * they are just queued up and once adap->lock is unlocked the main
1325 * thread will kick in and start transmitting these.
1326 *
1327 * If after this function is done (but before one or more of these
1328 * messages are actually transmitted) the CEC adapter is unconfigured,
1329 * then any remaining messages will be dropped by the main thread.
1330 */
Hans Verkuil9881fe02016-06-25 09:44:16 -03001331 for (i = 0; i < las->num_log_addrs; i++) {
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001332 struct cec_msg msg = {};
1333
Hans Verkuila69a1682016-11-02 07:41:41 -02001334 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1335 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001336 continue;
1337
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001338 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1339
1340 /* Report Features must come first according to CEC 2.0 */
1341 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1342 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1343 cec_fill_msg_report_features(adap, &msg, i);
Hans Verkuilf60f3562016-12-09 12:00:49 -02001344 cec_transmit_msg_fh(adap, &msg, NULL, false);
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001345 }
1346
Hans Verkuild3d64bc2016-12-09 11:48:34 -02001347 /* Report Physical Address */
1348 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1349 las->primary_device_type[i]);
Hans Verkuila7a04b52017-05-31 06:50:26 -03001350 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
Hans Verkuild3d64bc2016-12-09 11:48:34 -02001351 las->log_addr[i],
1352 cec_phys_addr_exp(adap->phys_addr));
Hans Verkuilf60f3562016-12-09 12:00:49 -02001353 cec_transmit_msg_fh(adap, &msg, NULL, false);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001354 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001355 adap->kthread_config = NULL;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001356 complete(&adap->config_completion);
Hans Verkuilf60f3562016-12-09 12:00:49 -02001357 mutex_unlock(&adap->lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001358 return 0;
1359
1360unconfigure:
1361 for (i = 0; i < las->num_log_addrs; i++)
1362 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1363 cec_adap_unconfigure(adap);
1364 adap->kthread_config = NULL;
1365 mutex_unlock(&adap->lock);
1366 complete(&adap->config_completion);
1367 return 0;
1368}
1369
1370/*
1371 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1372 * logical addresses.
1373 *
1374 * This function is called with adap->lock held.
1375 */
1376static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1377{
1378 if (WARN_ON(adap->is_configuring || adap->is_configured))
1379 return;
1380
1381 init_completion(&adap->config_completion);
1382
1383 /* Ready to kick off the thread */
1384 adap->is_configuring = true;
1385 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1386 "ceccfg-%s", adap->name);
1387 if (IS_ERR(adap->kthread_config)) {
1388 adap->kthread_config = NULL;
1389 } else if (block) {
1390 mutex_unlock(&adap->lock);
1391 wait_for_completion(&adap->config_completion);
1392 mutex_lock(&adap->lock);
1393 }
1394}
1395
1396/* Set a new physical address and send an event notifying userspace of this.
1397 *
1398 * This function is called with adap->lock held.
1399 */
1400void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1401{
Hans Verkuilc000e5d2016-07-10 10:11:17 -03001402 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001403 return;
1404
Hans Verkuilf902c1e2017-06-07 11:46:12 -03001405 dprintk(1, "new physical address %x.%x.%x.%x\n",
1406 cec_phys_addr_exp(phys_addr));
Hans Verkuil9881fe02016-06-25 09:44:16 -03001407 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1408 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1409 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1410 cec_post_state_event(adap);
1411 cec_adap_unconfigure(adap);
1412 /* Disabling monitor all mode should always succeed */
1413 if (adap->monitor_all_cnt)
1414 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
Hans Verkuil533a3f72017-02-10 13:02:31 -02001415 mutex_lock(&adap->devnode.lock);
Hans Verkuilf902c1e2017-06-07 11:46:12 -03001416 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
Hans Verkuil533a3f72017-02-10 13:02:31 -02001417 WARN_ON(adap->ops->adap_enable(adap, false));
1418 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001419 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1420 return;
1421 }
1422
Hans Verkuil533a3f72017-02-10 13:02:31 -02001423 mutex_lock(&adap->devnode.lock);
Hans Verkuilf902c1e2017-06-07 11:46:12 -03001424 if ((adap->needs_hpd || list_empty(&adap->devnode.fhs)) &&
Hans Verkuil533a3f72017-02-10 13:02:31 -02001425 adap->ops->adap_enable(adap, true)) {
1426 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001427 return;
Hans Verkuil533a3f72017-02-10 13:02:31 -02001428 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001429
1430 if (adap->monitor_all_cnt &&
1431 call_op(adap, adap_monitor_all_enable, true)) {
Hans Verkuilf902c1e2017-06-07 11:46:12 -03001432 if (adap->needs_hpd || list_empty(&adap->devnode.fhs))
Hans Verkuil533a3f72017-02-10 13:02:31 -02001433 WARN_ON(adap->ops->adap_enable(adap, false));
1434 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001435 return;
1436 }
Hans Verkuil533a3f72017-02-10 13:02:31 -02001437 mutex_unlock(&adap->devnode.lock);
1438
Hans Verkuil9881fe02016-06-25 09:44:16 -03001439 adap->phys_addr = phys_addr;
1440 cec_post_state_event(adap);
1441 if (adap->log_addrs.num_log_addrs)
1442 cec_claim_log_addrs(adap, block);
1443}
1444
1445void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1446{
1447 if (IS_ERR_OR_NULL(adap))
1448 return;
1449
Hans Verkuil9881fe02016-06-25 09:44:16 -03001450 mutex_lock(&adap->lock);
1451 __cec_s_phys_addr(adap, phys_addr, block);
1452 mutex_unlock(&adap->lock);
1453}
1454EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1455
Hans Verkuil23111ec2017-06-07 11:46:08 -03001456void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1457 const struct edid *edid)
1458{
1459 u16 pa = CEC_PHYS_ADDR_INVALID;
1460
1461 if (edid && edid->extensions)
1462 pa = cec_get_edid_phys_addr((const u8 *)edid,
1463 EDID_LENGTH * (edid->extensions + 1), NULL);
1464 cec_s_phys_addr(adap, pa, false);
1465}
1466EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1467
Hans Verkuil9881fe02016-06-25 09:44:16 -03001468/*
1469 * Called from either the ioctl or a driver to set the logical addresses.
1470 *
1471 * This function is called with adap->lock held.
1472 */
1473int __cec_s_log_addrs(struct cec_adapter *adap,
1474 struct cec_log_addrs *log_addrs, bool block)
1475{
1476 u16 type_mask = 0;
1477 int i;
1478
Hans Verkuilc000e5d2016-07-10 10:11:17 -03001479 if (adap->devnode.unregistered)
1480 return -ENODEV;
1481
Hans Verkuil9881fe02016-06-25 09:44:16 -03001482 if (!log_addrs || log_addrs->num_log_addrs == 0) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001483 cec_adap_unconfigure(adap);
Hans Verkuil299708e2017-07-04 11:21:14 -03001484 adap->log_addrs.num_log_addrs = 0;
1485 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++)
1486 adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID;
1487 adap->log_addrs.osd_name[0] = '\0';
1488 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
1489 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001490 return 0;
1491 }
1492
Hans Verkuila69a1682016-11-02 07:41:41 -02001493 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1494 /*
1495 * Sanitize log_addrs fields if a CDC-Only device is
1496 * requested.
1497 */
1498 log_addrs->num_log_addrs = 1;
1499 log_addrs->osd_name[0] = '\0';
1500 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1501 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1502 /*
1503 * This is just an internal convention since a CDC-Only device
1504 * doesn't have to be a switch. But switches already use
1505 * unregistered, so it makes some kind of sense to pick this
1506 * as the primary device. Since a CDC-Only device never sends
1507 * any 'normal' CEC messages this primary device type is never
1508 * sent over the CEC bus.
1509 */
1510 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1511 log_addrs->all_device_types[0] = 0;
1512 log_addrs->features[0][0] = 0;
1513 log_addrs->features[0][1] = 0;
1514 }
1515
Hans Verkuil9881fe02016-06-25 09:44:16 -03001516 /* Ensure the osd name is 0-terminated */
1517 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1518
1519 /* Sanity checks */
1520 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1521 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1522 return -EINVAL;
1523 }
1524
1525 /*
1526 * Vendor ID is a 24 bit number, so check if the value is
1527 * within the correct range.
1528 */
1529 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
Hans Verkuil79cabaa2017-02-20 17:19:13 -03001530 (log_addrs->vendor_id & 0xff000000) != 0) {
1531 dprintk(1, "invalid vendor ID\n");
Hans Verkuil9881fe02016-06-25 09:44:16 -03001532 return -EINVAL;
Hans Verkuil79cabaa2017-02-20 17:19:13 -03001533 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001534
1535 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
Hans Verkuil79cabaa2017-02-20 17:19:13 -03001536 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1537 dprintk(1, "invalid CEC version\n");
Hans Verkuil9881fe02016-06-25 09:44:16 -03001538 return -EINVAL;
Hans Verkuil79cabaa2017-02-20 17:19:13 -03001539 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001540
1541 if (log_addrs->num_log_addrs > 1)
1542 for (i = 0; i < log_addrs->num_log_addrs; i++)
1543 if (log_addrs->log_addr_type[i] ==
1544 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1545 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1546 return -EINVAL;
1547 }
1548
Hans Verkuil9881fe02016-06-25 09:44:16 -03001549 for (i = 0; i < log_addrs->num_log_addrs; i++) {
Hans Verkuil009a6202016-07-18 03:44:10 -03001550 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001551 u8 *features = log_addrs->features[i];
1552 bool op_is_dev_features = false;
Hans Verkuila161bef2016-11-04 10:52:10 -02001553 unsigned j;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001554
1555 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1556 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1557 dprintk(1, "duplicate logical address type\n");
1558 return -EINVAL;
1559 }
1560 type_mask |= 1 << log_addrs->log_addr_type[i];
1561 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1562 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1563 /* Record already contains the playback functionality */
1564 dprintk(1, "invalid record + playback combination\n");
1565 return -EINVAL;
1566 }
1567 if (log_addrs->primary_device_type[i] >
1568 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1569 dprintk(1, "unknown primary device type\n");
1570 return -EINVAL;
1571 }
1572 if (log_addrs->primary_device_type[i] == 2) {
1573 dprintk(1, "invalid primary device type\n");
1574 return -EINVAL;
1575 }
1576 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1577 dprintk(1, "unknown logical address type\n");
1578 return -EINVAL;
1579 }
Hans Verkuila161bef2016-11-04 10:52:10 -02001580 for (j = 0; j < feature_sz; j++) {
1581 if ((features[j] & 0x80) == 0) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001582 if (op_is_dev_features)
1583 break;
1584 op_is_dev_features = true;
1585 }
1586 }
Hans Verkuila161bef2016-11-04 10:52:10 -02001587 if (!op_is_dev_features || j == feature_sz) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001588 dprintk(1, "malformed features\n");
1589 return -EINVAL;
1590 }
Hans Verkuil009a6202016-07-18 03:44:10 -03001591 /* Zero unused part of the feature array */
Hans Verkuila161bef2016-11-04 10:52:10 -02001592 memset(features + j + 1, 0, feature_sz - j - 1);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001593 }
1594
1595 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1596 if (log_addrs->num_log_addrs > 2) {
1597 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1598 return -EINVAL;
1599 }
1600 if (log_addrs->num_log_addrs == 2) {
1601 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1602 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
Hans Verkuila7a04b52017-05-31 06:50:26 -03001603 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
Hans Verkuil9881fe02016-06-25 09:44:16 -03001604 return -EINVAL;
1605 }
1606 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1607 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
Hans Verkuila7a04b52017-05-31 06:50:26 -03001608 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
Hans Verkuil9881fe02016-06-25 09:44:16 -03001609 return -EINVAL;
1610 }
1611 }
1612 }
1613
Hans Verkuil009a6202016-07-18 03:44:10 -03001614 /* Zero unused LAs */
1615 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1616 log_addrs->primary_device_type[i] = 0;
1617 log_addrs->log_addr_type[i] = 0;
1618 log_addrs->all_device_types[i] = 0;
1619 memset(log_addrs->features[i], 0,
1620 sizeof(log_addrs->features[i]));
1621 }
1622
Hans Verkuil9881fe02016-06-25 09:44:16 -03001623 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1624 adap->log_addrs = *log_addrs;
1625 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1626 cec_claim_log_addrs(adap, block);
1627 return 0;
1628}
1629
1630int cec_s_log_addrs(struct cec_adapter *adap,
1631 struct cec_log_addrs *log_addrs, bool block)
1632{
1633 int err;
1634
Hans Verkuil9881fe02016-06-25 09:44:16 -03001635 mutex_lock(&adap->lock);
1636 err = __cec_s_log_addrs(adap, log_addrs, block);
1637 mutex_unlock(&adap->lock);
1638 return err;
1639}
1640EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1641
1642/* High-level core CEC message handling */
1643
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001644/* Fill in the Report Features message */
1645static void cec_fill_msg_report_features(struct cec_adapter *adap,
1646 struct cec_msg *msg,
1647 unsigned int la_idx)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001648{
Hans Verkuil9881fe02016-06-25 09:44:16 -03001649 const struct cec_log_addrs *las = &adap->log_addrs;
1650 const u8 *features = las->features[la_idx];
1651 bool op_is_dev_features = false;
1652 unsigned int idx;
1653
Hans Verkuil9881fe02016-06-25 09:44:16 -03001654 /* Report Features */
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001655 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1656 msg->len = 4;
1657 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1658 msg->msg[2] = adap->log_addrs.cec_version;
1659 msg->msg[3] = las->all_device_types[la_idx];
Hans Verkuil9881fe02016-06-25 09:44:16 -03001660
1661 /* Write RC Profiles first, then Device Features */
1662 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001663 msg->msg[msg->len++] = features[idx];
Hans Verkuil9881fe02016-06-25 09:44:16 -03001664 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1665 if (op_is_dev_features)
1666 break;
1667 op_is_dev_features = true;
1668 }
1669 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001670}
1671
Hans Verkuil9881fe02016-06-25 09:44:16 -03001672/* Transmit the Feature Abort message */
1673static int cec_feature_abort_reason(struct cec_adapter *adap,
1674 struct cec_msg *msg, u8 reason)
1675{
1676 struct cec_msg tx_msg = { };
1677
1678 /*
1679 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1680 * message!
1681 */
1682 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1683 return 0;
Hans Verkuila8e97e52017-02-28 10:20:50 -03001684 /* Don't Feature Abort messages from 'Unregistered' */
1685 if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
1686 return 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001687 cec_msg_set_reply_to(&tx_msg, msg);
1688 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1689 return cec_transmit_msg(adap, &tx_msg, false);
1690}
1691
1692static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1693{
1694 return cec_feature_abort_reason(adap, msg,
1695 CEC_OP_ABORT_UNRECOGNIZED_OP);
1696}
1697
1698static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1699{
1700 return cec_feature_abort_reason(adap, msg,
1701 CEC_OP_ABORT_REFUSED);
1702}
1703
1704/*
1705 * Called when a CEC message is received. This function will do any
1706 * necessary core processing. The is_reply bool is true if this message
1707 * is a reply to an earlier transmit.
1708 *
1709 * The message is either a broadcast message or a valid directed message.
1710 */
1711static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1712 bool is_reply)
1713{
1714 bool is_broadcast = cec_msg_is_broadcast(msg);
1715 u8 dest_laddr = cec_msg_destination(msg);
1716 u8 init_laddr = cec_msg_initiator(msg);
1717 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1718 int la_idx = cec_log_addr2idx(adap, dest_laddr);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001719 bool from_unregistered = init_laddr == 0xf;
1720 struct cec_msg tx_cec_msg = { };
1721
Hans Verkuila7a04b52017-05-31 06:50:26 -03001722 dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001723
Hans Verkuila69a1682016-11-02 07:41:41 -02001724 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1725 if (cec_is_cdc_only(&adap->log_addrs) &&
1726 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1727 return 0;
1728
Hans Verkuil9881fe02016-06-25 09:44:16 -03001729 if (adap->ops->received) {
1730 /* Allow drivers to process the message first */
1731 if (adap->ops->received(adap, msg) != -ENOMSG)
1732 return 0;
1733 }
1734
1735 /*
1736 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1737 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1738 * handled by the CEC core, even if the passthrough mode is on.
1739 * The others are just ignored if passthrough mode is on.
1740 */
1741 switch (msg->msg[1]) {
1742 case CEC_MSG_GET_CEC_VERSION:
1743 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1744 case CEC_MSG_ABORT:
1745 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1746 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1747 case CEC_MSG_GIVE_OSD_NAME:
1748 case CEC_MSG_GIVE_FEATURES:
1749 /*
1750 * Skip processing these messages if the passthrough mode
1751 * is on.
1752 */
1753 if (adap->passthrough)
1754 goto skip_processing;
1755 /* Ignore if addressing is wrong */
1756 if (is_broadcast || from_unregistered)
1757 return 0;
1758 break;
1759
1760 case CEC_MSG_USER_CONTROL_PRESSED:
1761 case CEC_MSG_USER_CONTROL_RELEASED:
1762 /* Wrong addressing mode: don't process */
1763 if (is_broadcast || from_unregistered)
1764 goto skip_processing;
1765 break;
1766
1767 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1768 /*
1769 * This message is always processed, regardless of the
1770 * passthrough setting.
1771 *
1772 * Exception: don't process if wrong addressing mode.
1773 */
1774 if (!is_broadcast)
1775 goto skip_processing;
1776 break;
1777
1778 default:
1779 break;
1780 }
1781
1782 cec_msg_set_reply_to(&tx_cec_msg, msg);
1783
1784 switch (msg->msg[1]) {
1785 /* The following messages are processed but still passed through */
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001786 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1787 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1788
1789 if (!from_unregistered)
1790 adap->phys_addrs[init_laddr] = pa;
Hans Verkuila7a04b52017-05-31 06:50:26 -03001791 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001792 cec_phys_addr_exp(pa), init_laddr);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001793 break;
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001794 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001795
1796 case CEC_MSG_USER_CONTROL_PRESSED:
Hans Verkuilf4062622016-11-01 07:59:34 -02001797 if (!(adap->capabilities & CEC_CAP_RC) ||
1798 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001799 break;
1800
Hans Verkuil5f2c4672017-04-17 08:05:10 -03001801#ifdef CONFIG_MEDIA_CEC_RC
Hans Verkuil9881fe02016-06-25 09:44:16 -03001802 switch (msg->msg[2]) {
1803 /*
1804 * Play function, this message can have variable length
1805 * depending on the specific play function that is used.
1806 */
1807 case 0x60:
1808 if (msg->len == 2)
1809 rc_keydown(adap->rc, RC_TYPE_CEC,
1810 msg->msg[2], 0);
1811 else
1812 rc_keydown(adap->rc, RC_TYPE_CEC,
1813 msg->msg[2] << 8 | msg->msg[3], 0);
1814 break;
1815 /*
1816 * Other function messages that are not handled.
1817 * Currently the RC framework does not allow to supply an
1818 * additional parameter to a keypress. These "keys" contain
1819 * other information such as channel number, an input number
1820 * etc.
1821 * For the time being these messages are not processed by the
1822 * framework and are simply forwarded to the user space.
1823 */
1824 case 0x56: case 0x57:
1825 case 0x67: case 0x68: case 0x69: case 0x6a:
1826 break;
1827 default:
1828 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1829 break;
1830 }
1831#endif
1832 break;
1833
1834 case CEC_MSG_USER_CONTROL_RELEASED:
Hans Verkuilf4062622016-11-01 07:59:34 -02001835 if (!(adap->capabilities & CEC_CAP_RC) ||
1836 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001837 break;
Hans Verkuil5f2c4672017-04-17 08:05:10 -03001838#ifdef CONFIG_MEDIA_CEC_RC
Hans Verkuil9881fe02016-06-25 09:44:16 -03001839 rc_keyup(adap->rc);
1840#endif
1841 break;
1842
1843 /*
1844 * The remaining messages are only processed if the passthrough mode
1845 * is off.
1846 */
1847 case CEC_MSG_GET_CEC_VERSION:
1848 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1849 return cec_transmit_msg(adap, &tx_cec_msg, false);
1850
1851 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1852 /* Do nothing for CEC switches using addr 15 */
1853 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1854 return 0;
1855 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1856 return cec_transmit_msg(adap, &tx_cec_msg, false);
1857
1858 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1859 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1860 return cec_feature_abort(adap, msg);
1861 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1862 return cec_transmit_msg(adap, &tx_cec_msg, false);
1863
1864 case CEC_MSG_ABORT:
1865 /* Do nothing for CEC switches */
1866 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1867 return 0;
1868 return cec_feature_refused(adap, msg);
1869
1870 case CEC_MSG_GIVE_OSD_NAME: {
1871 if (adap->log_addrs.osd_name[0] == 0)
1872 return cec_feature_abort(adap, msg);
1873 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1874 return cec_transmit_msg(adap, &tx_cec_msg, false);
1875 }
1876
1877 case CEC_MSG_GIVE_FEATURES:
Hans Verkuila24f56d2016-12-09 11:28:19 -02001878 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1879 return cec_feature_abort(adap, msg);
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001880 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1881 return cec_transmit_msg(adap, &tx_cec_msg, false);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001882
1883 default:
1884 /*
1885 * Unprocessed messages are aborted if userspace isn't doing
1886 * any processing either.
1887 */
Hans Verkuila179b692016-08-24 05:36:53 -03001888 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
Hans Verkuil9881fe02016-06-25 09:44:16 -03001889 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1890 return cec_feature_abort(adap, msg);
1891 break;
1892 }
1893
1894skip_processing:
Hans Verkuiladc0c622016-11-01 08:55:05 -02001895 /* If this was a reply, then we're done, unless otherwise specified */
1896 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001897 return 0;
1898
1899 /*
1900 * Send to the exclusive follower if there is one, otherwise send
1901 * to all followers.
1902 */
1903 if (adap->cec_follower)
1904 cec_queue_msg_fh(adap->cec_follower, msg);
1905 else
1906 cec_queue_msg_followers(adap, msg);
1907 return 0;
1908}
1909
1910/*
1911 * Helper functions to keep track of the 'monitor all' use count.
1912 *
1913 * These functions are called with adap->lock held.
1914 */
1915int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1916{
1917 int ret = 0;
1918
1919 if (adap->monitor_all_cnt == 0)
1920 ret = call_op(adap, adap_monitor_all_enable, 1);
1921 if (ret == 0)
1922 adap->monitor_all_cnt++;
1923 return ret;
1924}
1925
1926void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1927{
1928 adap->monitor_all_cnt--;
1929 if (adap->monitor_all_cnt == 0)
1930 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1931}
1932
Hans Verkuil20249f842017-05-28 05:52:16 -03001933#ifdef CONFIG_DEBUG_FS
Hans Verkuil9881fe02016-06-25 09:44:16 -03001934/*
1935 * Log the current state of the CEC adapter.
1936 * Very useful for debugging.
1937 */
1938int cec_adap_status(struct seq_file *file, void *priv)
1939{
1940 struct cec_adapter *adap = dev_get_drvdata(file->private);
1941 struct cec_data *data;
1942
1943 mutex_lock(&adap->lock);
1944 seq_printf(file, "configured: %d\n", adap->is_configured);
1945 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1946 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1947 cec_phys_addr_exp(adap->phys_addr));
1948 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1949 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1950 if (adap->cec_follower)
1951 seq_printf(file, "has CEC follower%s\n",
1952 adap->passthrough ? " (in passthrough mode)" : "");
1953 if (adap->cec_initiator)
1954 seq_puts(file, "has CEC initiator\n");
1955 if (adap->monitor_all_cnt)
1956 seq_printf(file, "file handles in Monitor All mode: %u\n",
1957 adap->monitor_all_cnt);
Hans Verkuilbb789e02017-07-11 03:30:34 -03001958 if (adap->tx_timeouts) {
1959 seq_printf(file, "transmit timeouts: %u\n",
1960 adap->tx_timeouts);
1961 adap->tx_timeouts = 0;
1962 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001963 data = adap->transmitting;
1964 if (data)
Hans Verkuil11065f82016-07-17 11:40:05 -03001965 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1966 data->msg.len, data->msg.msg, data->msg.reply,
1967 data->msg.timeout);
1968 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001969 list_for_each_entry(data, &adap->transmit_queue, list) {
Hans Verkuil11065f82016-07-17 11:40:05 -03001970 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1971 data->msg.len, data->msg.msg, data->msg.reply,
1972 data->msg.timeout);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001973 }
1974 list_for_each_entry(data, &adap->wait_queue, list) {
Hans Verkuil11065f82016-07-17 11:40:05 -03001975 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1976 data->msg.len, data->msg.msg, data->msg.reply,
1977 data->msg.timeout);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001978 }
1979
1980 call_void_op(adap, adap_status, file);
1981 mutex_unlock(&adap->lock);
1982 return 0;
1983}
1984#endif