blob: 421472b492ee87c28a1c78deeb371252ceeb2b8a [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
31#include "cec-priv.h"
32
Hans Verkuil52bc30f2016-12-09 11:36:03 -020033static void cec_fill_msg_report_features(struct cec_adapter *adap,
34 struct cec_msg *msg,
35 unsigned int la_idx);
Hans Verkuil9881fe02016-06-25 09:44:16 -030036
37/*
38 * 400 ms is the time it takes for one 16 byte message to be
39 * transferred and 5 is the maximum number of retries. Add
40 * another 100 ms as a margin. So if the transmit doesn't
41 * finish before that time something is really wrong and we
42 * have to time out.
43 *
44 * This is a sign that something it really wrong and a warning
45 * will be issued.
46 */
47#define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
48
49#define call_op(adap, op, arg...) \
50 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
51
52#define call_void_op(adap, op, arg...) \
53 do { \
54 if (adap->ops->op) \
55 adap->ops->op(adap, ## arg); \
56 } while (0)
57
58static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
59{
60 int i;
61
62 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
63 if (adap->log_addrs.log_addr[i] == log_addr)
64 return i;
65 return -1;
66}
67
68static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
69{
70 int i = cec_log_addr2idx(adap, log_addr);
71
72 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
73}
74
75/*
76 * Queue a new event for this filehandle. If ts == 0, then set it
77 * to the current time.
78 *
79 * The two events that are currently defined do not need to keep track
80 * of intermediate events, so no actual queue of events is needed,
81 * instead just store the latest state and the total number of lost
82 * messages.
83 *
84 * Should new events be added in the future that require intermediate
85 * results to be queued as well, then a proper queue data structure is
86 * required. But until then, just keep it simple.
87 */
88void cec_queue_event_fh(struct cec_fh *fh,
89 const struct cec_event *new_ev, u64 ts)
90{
91 struct cec_event *ev = &fh->events[new_ev->event - 1];
92
93 if (ts == 0)
94 ts = ktime_get_ns();
95
96 mutex_lock(&fh->lock);
97 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
98 fh->pending_events & (1 << new_ev->event)) {
99 /*
100 * If there is already a lost_msgs event, then just
101 * update the lost_msgs count. This effectively
102 * merges the old and new events into one.
103 */
104 ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
105 goto unlock;
106 }
107
108 /*
109 * Intermediate states are not interesting, so just
110 * overwrite any older event.
111 */
112 *ev = *new_ev;
113 ev->ts = ts;
114 fh->pending_events |= 1 << new_ev->event;
115
116unlock:
117 mutex_unlock(&fh->lock);
118 wake_up_interruptible(&fh->wait);
119}
120
121/* Queue a new event for all open filehandles. */
122static void cec_queue_event(struct cec_adapter *adap,
123 const struct cec_event *ev)
124{
125 u64 ts = ktime_get_ns();
126 struct cec_fh *fh;
127
Hans Verkuil62148f02016-08-02 08:11:00 -0300128 mutex_lock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300129 list_for_each_entry(fh, &adap->devnode.fhs, list)
130 cec_queue_event_fh(fh, ev, ts);
Hans Verkuil62148f02016-08-02 08:11:00 -0300131 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300132}
133
134/*
135 * Queue a new message for this filehandle. If there is no more room
136 * in the queue, then send the LOST_MSGS event instead.
137 */
138static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
139{
140 static const struct cec_event ev_lost_msg = {
Andrew Morton6a91d602016-07-12 15:30:55 -0700141 .ts = 0,
Hans Verkuil9881fe02016-06-25 09:44:16 -0300142 .event = CEC_EVENT_LOST_MSGS,
Andrew Morton6a91d602016-07-12 15:30:55 -0700143 .flags = 0,
144 {
145 .lost_msgs.lost_msgs = 1,
146 },
Hans Verkuil9881fe02016-06-25 09:44:16 -0300147 };
148 struct cec_msg_entry *entry;
149
150 mutex_lock(&fh->lock);
151 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
152 if (!entry)
153 goto lost_msgs;
154
155 entry->msg = *msg;
156 /* Add new msg at the end of the queue */
157 list_add_tail(&entry->list, &fh->msgs);
158
159 /*
Hans Verkuil11065f82016-07-17 11:40:05 -0300160 * if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
Hans Verkuil9881fe02016-06-25 09:44:16 -0300161 * messages, drop the oldest one and send a lost message event.
162 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300163 if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300164 list_del(&entry->list);
165 goto lost_msgs;
166 }
167 fh->queued_msgs++;
168 mutex_unlock(&fh->lock);
169 wake_up_interruptible(&fh->wait);
170 return;
171
172lost_msgs:
173 mutex_unlock(&fh->lock);
174 cec_queue_event_fh(fh, &ev_lost_msg, 0);
175}
176
177/*
178 * Queue the message for those filehandles that are in monitor mode.
179 * If valid_la is true (this message is for us or was sent by us),
180 * then pass it on to any monitoring filehandle. If this message
181 * isn't for us or from us, then only give it to filehandles that
182 * are in MONITOR_ALL mode.
183 *
184 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
185 * set and the CEC adapter was placed in 'monitor all' mode.
186 */
187static void cec_queue_msg_monitor(struct cec_adapter *adap,
188 const struct cec_msg *msg,
189 bool valid_la)
190{
191 struct cec_fh *fh;
192 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
193 CEC_MODE_MONITOR_ALL;
194
Hans Verkuil62148f02016-08-02 08:11:00 -0300195 mutex_lock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300196 list_for_each_entry(fh, &adap->devnode.fhs, list) {
197 if (fh->mode_follower >= monitor_mode)
198 cec_queue_msg_fh(fh, msg);
199 }
Hans Verkuil62148f02016-08-02 08:11:00 -0300200 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300201}
202
203/*
204 * Queue the message for follower filehandles.
205 */
206static void cec_queue_msg_followers(struct cec_adapter *adap,
207 const struct cec_msg *msg)
208{
209 struct cec_fh *fh;
210
Hans Verkuil62148f02016-08-02 08:11:00 -0300211 mutex_lock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300212 list_for_each_entry(fh, &adap->devnode.fhs, list) {
213 if (fh->mode_follower == CEC_MODE_FOLLOWER)
214 cec_queue_msg_fh(fh, msg);
215 }
Hans Verkuil62148f02016-08-02 08:11:00 -0300216 mutex_unlock(&adap->devnode.lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300217}
218
219/* Notify userspace of an adapter state change. */
220static void cec_post_state_event(struct cec_adapter *adap)
221{
222 struct cec_event ev = {
223 .event = CEC_EVENT_STATE_CHANGE,
224 };
225
226 ev.state_change.phys_addr = adap->phys_addr;
227 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
228 cec_queue_event(adap, &ev);
229}
230
231/*
232 * A CEC transmit (and a possible wait for reply) completed.
233 * If this was in blocking mode, then complete it, otherwise
234 * queue the message for userspace to dequeue later.
235 *
236 * This function is called with adap->lock held.
237 */
238static void cec_data_completed(struct cec_data *data)
239{
240 /*
241 * Delete this transmit from the filehandle's xfer_list since
242 * we're done with it.
243 *
244 * Note that if the filehandle is closed before this transmit
245 * finished, then the release() function will set data->fh to NULL.
246 * Without that we would be referring to a closed filehandle.
247 */
248 if (data->fh)
249 list_del(&data->xfer_list);
250
251 if (data->blocking) {
252 /*
253 * Someone is blocking so mark the message as completed
254 * and call complete.
255 */
256 data->completed = true;
257 complete(&data->c);
258 } else {
259 /*
260 * No blocking, so just queue the message if needed and
261 * free the memory.
262 */
263 if (data->fh)
264 cec_queue_msg_fh(data->fh, &data->msg);
265 kfree(data);
266 }
267}
268
269/*
270 * A pending CEC transmit needs to be cancelled, either because the CEC
271 * adapter is disabled or the transmit takes an impossibly long time to
272 * finish.
273 *
274 * This function is called with adap->lock held.
275 */
276static void cec_data_cancel(struct cec_data *data)
277{
278 /*
279 * It's either the current transmit, or it is a pending
280 * transmit. Take the appropriate action to clear it.
281 */
Hans Verkuil11065f82016-07-17 11:40:05 -0300282 if (data->adap->transmitting == data) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300283 data->adap->transmitting = NULL;
Hans Verkuil11065f82016-07-17 11:40:05 -0300284 } else {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300285 list_del_init(&data->list);
Hans Verkuil11065f82016-07-17 11:40:05 -0300286 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
287 data->adap->transmit_queue_sz--;
288 }
Hans Verkuil9881fe02016-06-25 09:44:16 -0300289
290 /* Mark it as an error */
Hans Verkuil980e0b362016-07-12 11:10:42 -0300291 data->msg.tx_ts = ktime_get_ns();
Hans Verkuil12047612016-12-09 11:14:32 -0200292 data->msg.tx_status |= CEC_TX_STATUS_ERROR |
293 CEC_TX_STATUS_MAX_RETRIES;
294 data->msg.tx_error_cnt++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300295 data->attempts = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300296 /* Queue transmitted message for monitoring purposes */
297 cec_queue_msg_monitor(data->adap, &data->msg, 1);
298
299 cec_data_completed(data);
300}
301
302/*
Hans Verkuilb39f93e2017-02-10 13:02:31 -0200303 * Flush all pending transmits and cancel any pending timeout work.
304 *
305 * This function is called with adap->lock held.
306 */
307static void cec_flush(struct cec_adapter *adap)
308{
309 struct cec_data *data, *n;
310
311 /*
312 * If the adapter is disabled, or we're asked to stop,
313 * then cancel any pending transmits.
314 */
315 while (!list_empty(&adap->transmit_queue)) {
316 data = list_first_entry(&adap->transmit_queue,
317 struct cec_data, list);
318 cec_data_cancel(data);
319 }
320 if (adap->transmitting)
321 cec_data_cancel(adap->transmitting);
322
323 /* Cancel the pending timeout work. */
324 list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
325 if (cancel_delayed_work(&data->work))
326 cec_data_cancel(data);
327 /*
328 * If cancel_delayed_work returned false, then
329 * the cec_wait_timeout function is running,
330 * which will call cec_data_completed. So no
331 * need to do anything special in that case.
332 */
333 }
334}
335
336/*
Hans Verkuil9881fe02016-06-25 09:44:16 -0300337 * Main CEC state machine
338 *
339 * Wait until the thread should be stopped, or we are not transmitting and
340 * a new transmit message is queued up, in which case we start transmitting
341 * that message. When the adapter finished transmitting the message it will
342 * call cec_transmit_done().
343 *
344 * If the adapter is disabled, then remove all queued messages instead.
345 *
346 * If the current transmit times out, then cancel that transmit.
347 */
348int cec_thread_func(void *_adap)
349{
350 struct cec_adapter *adap = _adap;
351
352 for (;;) {
353 unsigned int signal_free_time;
354 struct cec_data *data;
355 bool timeout = false;
356 u8 attempts;
357
358 if (adap->transmitting) {
359 int err;
360
361 /*
362 * We are transmitting a message, so add a timeout
363 * to prevent the state machine to get stuck waiting
364 * for this message to finalize and add a check to
365 * see if the adapter is disabled in which case the
366 * transmit should be canceled.
367 */
368 err = wait_event_interruptible_timeout(adap->kthread_waitq,
369 kthread_should_stop() ||
Hans Verkuil58ecc292016-07-17 11:42:06 -0300370 (!adap->is_configured && !adap->is_configuring) ||
Hans Verkuil9881fe02016-06-25 09:44:16 -0300371 (!adap->transmitting &&
372 !list_empty(&adap->transmit_queue)),
373 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
374 timeout = err == 0;
375 } else {
376 /* Otherwise we just wait for something to happen. */
377 wait_event_interruptible(adap->kthread_waitq,
378 kthread_should_stop() ||
379 (!adap->transmitting &&
380 !list_empty(&adap->transmit_queue)));
381 }
382
383 mutex_lock(&adap->lock);
384
Hans Verkuil58ecc292016-07-17 11:42:06 -0300385 if ((!adap->is_configured && !adap->is_configuring) ||
Hans Verkuil9881fe02016-06-25 09:44:16 -0300386 kthread_should_stop()) {
Hans Verkuilb39f93e2017-02-10 13:02:31 -0200387 cec_flush(adap);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300388 goto unlock;
389 }
390
391 if (adap->transmitting && timeout) {
392 /*
393 * If we timeout, then log that. This really shouldn't
394 * happen and is an indication of a faulty CEC adapter
395 * driver, or the CEC bus is in some weird state.
396 */
397 dprintk(0, "message %*ph timed out!\n",
398 adap->transmitting->msg.len,
399 adap->transmitting->msg.msg);
400 /* Just give up on this. */
401 cec_data_cancel(adap->transmitting);
402 goto unlock;
403 }
404
405 /*
406 * If we are still transmitting, or there is nothing new to
407 * transmit, then just continue waiting.
408 */
409 if (adap->transmitting || list_empty(&adap->transmit_queue))
410 goto unlock;
411
412 /* Get a new message to transmit */
413 data = list_first_entry(&adap->transmit_queue,
414 struct cec_data, list);
415 list_del_init(&data->list);
Hans Verkuil11065f82016-07-17 11:40:05 -0300416 adap->transmit_queue_sz--;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300417 /* Make this the current transmitting message */
418 adap->transmitting = data;
419
420 /*
421 * Suggested number of attempts as per the CEC 2.0 spec:
422 * 4 attempts is the default, except for 'secondary poll
423 * messages', i.e. poll messages not sent during the adapter
424 * configuration phase when it allocates logical addresses.
425 */
426 if (data->msg.len == 1 && adap->is_configured)
427 attempts = 2;
428 else
429 attempts = 4;
430
431 /* Set the suggested signal free time */
432 if (data->attempts) {
433 /* should be >= 3 data bit periods for a retry */
434 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
435 } else if (data->new_initiator) {
436 /* should be >= 5 data bit periods for new initiator */
437 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
438 } else {
439 /*
440 * should be >= 7 data bit periods for sending another
441 * frame immediately after another.
442 */
443 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
444 }
445 if (data->attempts == 0)
446 data->attempts = attempts;
447
448 /* Tell the adapter to transmit, cancel on error */
449 if (adap->ops->adap_transmit(adap, data->attempts,
450 signal_free_time, &data->msg))
451 cec_data_cancel(data);
452
453unlock:
454 mutex_unlock(&adap->lock);
455
456 if (kthread_should_stop())
457 break;
458 }
459 return 0;
460}
461
462/*
463 * Called by the CEC adapter if a transmit finished.
464 */
465void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
466 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
467{
468 struct cec_data *data;
469 struct cec_msg *msg;
Hans Verkuil980e0b362016-07-12 11:10:42 -0300470 u64 ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300471
472 dprintk(2, "cec_transmit_done %02x\n", status);
473 mutex_lock(&adap->lock);
474 data = adap->transmitting;
475 if (!data) {
476 /*
477 * This can happen if a transmit was issued and the cable is
478 * unplugged while the transmit is ongoing. Ignore this
479 * transmit in that case.
480 */
481 dprintk(1, "cec_transmit_done without an ongoing transmit!\n");
482 goto unlock;
483 }
484
485 msg = &data->msg;
486
487 /* Drivers must fill in the status! */
488 WARN_ON(status == 0);
Hans Verkuil980e0b362016-07-12 11:10:42 -0300489 msg->tx_ts = ts;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300490 msg->tx_status |= status;
491 msg->tx_arb_lost_cnt += arb_lost_cnt;
492 msg->tx_nack_cnt += nack_cnt;
493 msg->tx_low_drive_cnt += low_drive_cnt;
494 msg->tx_error_cnt += error_cnt;
495
496 /* Mark that we're done with this transmit */
497 adap->transmitting = NULL;
498
499 /*
500 * If there are still retry attempts left and there was an error and
501 * the hardware didn't signal that it retried itself (by setting
502 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
503 */
504 if (data->attempts > 1 &&
505 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
506 /* Retry this message */
507 data->attempts--;
508 /* Add the message in front of the transmit queue */
509 list_add(&data->list, &adap->transmit_queue);
Hans Verkuil11065f82016-07-17 11:40:05 -0300510 adap->transmit_queue_sz++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300511 goto wake_thread;
512 }
513
514 data->attempts = 0;
515
516 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
517 if (!(status & CEC_TX_STATUS_OK))
518 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
519
520 /* Queue transmitted message for monitoring purposes */
521 cec_queue_msg_monitor(adap, msg, 1);
522
Hans Verkuil86e35772016-07-13 04:33:58 -0300523 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
524 msg->timeout) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300525 /*
526 * Queue the message into the wait queue if we want to wait
527 * for a reply.
528 */
529 list_add_tail(&data->list, &adap->wait_queue);
530 schedule_delayed_work(&data->work,
531 msecs_to_jiffies(msg->timeout));
532 } else {
533 /* Otherwise we're done */
534 cec_data_completed(data);
535 }
536
537wake_thread:
538 /*
539 * Wake up the main thread to see if another message is ready
540 * for transmitting or to retry the current message.
541 */
542 wake_up_interruptible(&adap->kthread_waitq);
543unlock:
544 mutex_unlock(&adap->lock);
545}
546EXPORT_SYMBOL_GPL(cec_transmit_done);
547
548/*
549 * Called when waiting for a reply times out.
550 */
551static void cec_wait_timeout(struct work_struct *work)
552{
553 struct cec_data *data = container_of(work, struct cec_data, work.work);
554 struct cec_adapter *adap = data->adap;
555
556 mutex_lock(&adap->lock);
557 /*
558 * Sanity check in case the timeout and the arrival of the message
559 * happened at the same time.
560 */
561 if (list_empty(&data->list))
562 goto unlock;
563
564 /* Mark the message as timed out */
565 list_del_init(&data->list);
Hans Verkuil980e0b362016-07-12 11:10:42 -0300566 data->msg.rx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300567 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
568 cec_data_completed(data);
569unlock:
570 mutex_unlock(&adap->lock);
571}
572
573/*
574 * Transmit a message. The fh argument may be NULL if the transmit is not
575 * associated with a specific filehandle.
576 *
577 * This function is called with adap->lock held.
578 */
579int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
580 struct cec_fh *fh, bool block)
581{
582 struct cec_data *data;
583 u8 last_initiator = 0xff;
584 unsigned int timeout;
585 int res = 0;
586
Hans Verkuil40df3a72016-07-16 09:44:13 -0300587 msg->rx_ts = 0;
588 msg->tx_ts = 0;
589 msg->rx_status = 0;
590 msg->tx_status = 0;
591 msg->tx_arb_lost_cnt = 0;
592 msg->tx_nack_cnt = 0;
593 msg->tx_low_drive_cnt = 0;
594 msg->tx_error_cnt = 0;
Hans Verkuil40df3a72016-07-16 09:44:13 -0300595 msg->sequence = ++adap->sequence;
596 if (!msg->sequence)
597 msg->sequence = ++adap->sequence;
598
Hans Verkuil9881fe02016-06-25 09:44:16 -0300599 if (msg->reply && msg->timeout == 0) {
600 /* Make sure the timeout isn't 0. */
601 msg->timeout = 1000;
602 }
Hans Verkuil7ae2a882016-11-04 07:52:11 -0200603 if (msg->timeout)
604 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
605 else
606 msg->flags = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300607
608 /* Sanity checks */
609 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
610 dprintk(1, "cec_transmit_msg: invalid length %d\n", msg->len);
611 return -EINVAL;
612 }
613 if (msg->timeout && msg->len == 1) {
614 dprintk(1, "cec_transmit_msg: can't reply for poll msg\n");
615 return -EINVAL;
616 }
Hans Verkuil045344c2016-07-17 05:16:40 -0300617 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300618 if (msg->len == 1) {
Hans Verkuil42980da2017-02-11 09:24:46 -0200619 if (cec_msg_destination(msg) == 0xf) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300620 dprintk(1, "cec_transmit_msg: invalid poll message\n");
621 return -EINVAL;
622 }
623 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
624 /*
625 * If the destination is a logical address our adapter
626 * has already claimed, then just NACK this.
627 * It depends on the hardware what it will do with a
628 * POLL to itself (some OK this), so it is just as
629 * easy to handle it here so the behavior will be
630 * consistent.
631 */
Hans Verkuil9a3f14e2016-07-15 11:13:49 -0300632 msg->tx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300633 msg->tx_status = CEC_TX_STATUS_NACK |
634 CEC_TX_STATUS_MAX_RETRIES;
635 msg->tx_nack_cnt = 1;
636 return 0;
637 }
638 }
639 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
640 cec_has_log_addr(adap, cec_msg_destination(msg))) {
641 dprintk(1, "cec_transmit_msg: destination is the adapter itself\n");
642 return -EINVAL;
643 }
Hans Verkuil42980da2017-02-11 09:24:46 -0200644 if (msg->len > 1 && adap->is_configured &&
Hans Verkuil9881fe02016-06-25 09:44:16 -0300645 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
646 dprintk(1, "cec_transmit_msg: initiator has unknown logical address %d\n",
647 cec_msg_initiator(msg));
648 return -EINVAL;
649 }
650 if (!adap->is_configured && !adap->is_configuring)
651 return -ENONET;
652
Hans Verkuil11065f82016-07-17 11:40:05 -0300653 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ)
654 return -EBUSY;
655
Hans Verkuil9881fe02016-06-25 09:44:16 -0300656 data = kzalloc(sizeof(*data), GFP_KERNEL);
657 if (!data)
658 return -ENOMEM;
659
660 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
661 msg->msg[2] = adap->phys_addr >> 8;
662 msg->msg[3] = adap->phys_addr & 0xff;
663 }
664
665 if (msg->timeout)
666 dprintk(2, "cec_transmit_msg: %*ph (wait for 0x%02x%s)\n",
667 msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
668 else
669 dprintk(2, "cec_transmit_msg: %*ph%s\n",
670 msg->len, msg->msg, !block ? " (nb)" : "");
671
Hans Verkuil9881fe02016-06-25 09:44:16 -0300672 data->msg = *msg;
673 data->fh = fh;
674 data->adap = adap;
675 data->blocking = block;
676
677 /*
678 * Determine if this message follows a message from the same
679 * initiator. Needed to determine the free signal time later on.
680 */
681 if (msg->len > 1) {
682 if (!(list_empty(&adap->transmit_queue))) {
683 const struct cec_data *last;
684
685 last = list_last_entry(&adap->transmit_queue,
686 const struct cec_data, list);
687 last_initiator = cec_msg_initiator(&last->msg);
688 } else if (adap->transmitting) {
689 last_initiator =
690 cec_msg_initiator(&adap->transmitting->msg);
691 }
692 }
693 data->new_initiator = last_initiator != cec_msg_initiator(msg);
694 init_completion(&data->c);
695 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
696
Hans Verkuil9881fe02016-06-25 09:44:16 -0300697 if (fh)
698 list_add_tail(&data->xfer_list, &fh->xfer_list);
699 list_add_tail(&data->list, &adap->transmit_queue);
Hans Verkuil11065f82016-07-17 11:40:05 -0300700 adap->transmit_queue_sz++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300701 if (!adap->transmitting)
702 wake_up_interruptible(&adap->kthread_waitq);
703
704 /* All done if we don't need to block waiting for completion */
705 if (!block)
706 return 0;
707
708 /*
709 * If we don't get a completion before this time something is really
710 * wrong and we time out.
711 */
712 timeout = CEC_XFER_TIMEOUT_MS;
713 /* Add the requested timeout if we have to wait for a reply as well */
714 if (msg->timeout)
715 timeout += msg->timeout;
716
717 /*
718 * Release the lock and wait, retake the lock afterwards.
719 */
720 mutex_unlock(&adap->lock);
721 res = wait_for_completion_killable_timeout(&data->c,
722 msecs_to_jiffies(timeout));
723 mutex_lock(&adap->lock);
724
725 if (data->completed) {
726 /* The transmit completed (possibly with an error) */
727 *msg = data->msg;
728 kfree(data);
729 return 0;
730 }
731 /*
732 * The wait for completion timed out or was interrupted, so mark this
733 * as non-blocking and disconnect from the filehandle since it is
734 * still 'in flight'. When it finally completes it will just drop the
735 * result silently.
736 */
737 data->blocking = false;
738 if (data->fh)
739 list_del(&data->xfer_list);
740 data->fh = NULL;
741
742 if (res == 0) { /* timed out */
743 /* Check if the reply or the transmit failed */
744 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
745 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
746 else
747 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
748 }
749 return res > 0 ? 0 : res;
750}
751
752/* Helper function to be used by drivers and this framework. */
753int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
754 bool block)
755{
756 int ret;
757
758 mutex_lock(&adap->lock);
759 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
760 mutex_unlock(&adap->lock);
761 return ret;
762}
763EXPORT_SYMBOL_GPL(cec_transmit_msg);
764
765/*
766 * I don't like forward references but without this the low-level
767 * cec_received_msg() function would come after a bunch of high-level
768 * CEC protocol handling functions. That was very confusing.
769 */
770static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
771 bool is_reply);
772
Hans Verkuil3074fe42016-11-01 10:48:22 -0200773#define DIRECTED 0x80
774#define BCAST1_4 0x40
775#define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
776#define BCAST (BCAST1_4 | BCAST2_0)
777#define BOTH (BCAST | DIRECTED)
778
779/*
780 * Specify minimum length and whether the message is directed, broadcast
781 * or both. Messages that do not match the criteria are ignored as per
782 * the CEC specification.
783 */
784static const u8 cec_msg_size[256] = {
785 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
786 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
787 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
788 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
789 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
790 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
791 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
792 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
793 [CEC_MSG_STANDBY] = 2 | BOTH,
794 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
795 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
796 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
797 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
798 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
799 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
800 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
801 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
802 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
803 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
804 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
805 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
806 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
807 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
808 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
809 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
810 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
811 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
812 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
813 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
814 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
815 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
816 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
817 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
818 [CEC_MSG_PLAY] = 3 | DIRECTED,
819 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
820 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
821 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
822 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
823 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
824 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
825 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
826 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
827 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
828 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
829 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
830 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
831 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
832 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
833 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
834 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
835 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
836 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
837 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
838 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
839 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
840 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
841 [CEC_MSG_ABORT] = 2 | DIRECTED,
842 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
843 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
844 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
845 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
846 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
847 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
848 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
849 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
850 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
851 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
852 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
853 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
854 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
855 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
856 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
857 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
Hans Verkuilf3854972016-12-06 11:17:12 -0200858 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
Hans Verkuil3074fe42016-11-01 10:48:22 -0200859 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
860};
861
Hans Verkuil9881fe02016-06-25 09:44:16 -0300862/* Called by the CEC adapter if a message is received */
863void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
864{
865 struct cec_data *data;
866 u8 msg_init = cec_msg_initiator(msg);
867 u8 msg_dest = cec_msg_destination(msg);
Hans Verkuil3074fe42016-11-01 10:48:22 -0200868 u8 cmd = msg->msg[1];
Hans Verkuil9881fe02016-06-25 09:44:16 -0300869 bool is_reply = false;
870 bool valid_la = true;
Hans Verkuil3074fe42016-11-01 10:48:22 -0200871 u8 min_len = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300872
Hans Verkuil52d802d2016-07-12 11:10:41 -0300873 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
874 return;
875
Hans Verkuil3f98da92016-11-21 13:15:45 -0200876 /*
877 * Some CEC adapters will receive the messages that they transmitted.
878 * This test filters out those messages by checking if we are the
879 * initiator, and just returning in that case.
880 *
881 * Note that this won't work if this is an Unregistered device.
882 *
883 * It is bad practice if the hardware receives the message that it
884 * transmitted and luckily most CEC adapters behave correctly in this
885 * respect.
886 */
887 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
888 cec_has_log_addr(adap, msg_init))
889 return;
890
Hans Verkuil980e0b362016-07-12 11:10:42 -0300891 msg->rx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300892 msg->rx_status = CEC_RX_STATUS_OK;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300893 msg->sequence = msg->reply = msg->timeout = 0;
Hans Verkuil980e0b362016-07-12 11:10:42 -0300894 msg->tx_status = 0;
895 msg->tx_ts = 0;
Hans Verkuil8991a63d2016-11-09 12:10:59 -0200896 msg->tx_arb_lost_cnt = 0;
897 msg->tx_nack_cnt = 0;
898 msg->tx_low_drive_cnt = 0;
899 msg->tx_error_cnt = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300900 msg->flags = 0;
Hans Verkuil045344c2016-07-17 05:16:40 -0300901 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300902
Hans Verkuil980e0b362016-07-12 11:10:42 -0300903 mutex_lock(&adap->lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300904 dprintk(2, "cec_received_msg: %*ph\n", msg->len, msg->msg);
905
906 /* Check if this message was for us (directed or broadcast). */
907 if (!cec_msg_is_broadcast(msg))
908 valid_la = cec_has_log_addr(adap, msg_dest);
909
Hans Verkuil3074fe42016-11-01 10:48:22 -0200910 /*
911 * Check if the length is not too short or if the message is a
912 * broadcast message where a directed message was expected or
913 * vice versa. If so, then the message has to be ignored (according
914 * to section CEC 7.3 and CEC 12.2).
915 */
916 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
917 u8 dir_fl = cec_msg_size[cmd] & BOTH;
918
919 min_len = cec_msg_size[cmd] & 0x1f;
920 if (msg->len < min_len)
921 valid_la = false;
922 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
923 valid_la = false;
924 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
925 valid_la = false;
926 else if (cec_msg_is_broadcast(msg) &&
927 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
928 !(dir_fl & BCAST2_0))
929 valid_la = false;
930 }
931 if (valid_la && min_len) {
932 /* These messages have special length requirements */
933 switch (cmd) {
934 case CEC_MSG_TIMER_STATUS:
935 if (msg->msg[2] & 0x10) {
936 switch (msg->msg[2] & 0xf) {
937 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
938 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
939 if (msg->len < 5)
940 valid_la = false;
941 break;
942 }
943 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
944 if (msg->len < 5)
945 valid_la = false;
946 }
947 break;
948 case CEC_MSG_RECORD_ON:
949 switch (msg->msg[2]) {
950 case CEC_OP_RECORD_SRC_OWN:
951 break;
952 case CEC_OP_RECORD_SRC_DIGITAL:
953 if (msg->len < 10)
954 valid_la = false;
955 break;
956 case CEC_OP_RECORD_SRC_ANALOG:
957 if (msg->len < 7)
958 valid_la = false;
959 break;
960 case CEC_OP_RECORD_SRC_EXT_PLUG:
961 if (msg->len < 4)
962 valid_la = false;
963 break;
964 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
965 if (msg->len < 5)
966 valid_la = false;
967 break;
968 }
969 break;
970 }
971 }
972
Hans Verkuil9881fe02016-06-25 09:44:16 -0300973 /* It's a valid message and not a poll or CDC message */
Hans Verkuil3074fe42016-11-01 10:48:22 -0200974 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300975 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
976
977 /* The aborted command is in msg[2] */
978 if (abort)
979 cmd = msg->msg[2];
980
981 /*
982 * Walk over all transmitted messages that are waiting for a
983 * reply.
984 */
985 list_for_each_entry(data, &adap->wait_queue, list) {
986 struct cec_msg *dst = &data->msg;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300987
Hans Verkuilf5580d82016-11-01 11:07:17 -0200988 /*
989 * The *only* CEC message that has two possible replies
990 * is CEC_MSG_INITIATE_ARC.
991 * In this case allow either of the two replies.
992 */
993 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
994 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
995 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
996 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
997 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
998 dst->reply = cmd;
999
Hans Verkuil9881fe02016-06-25 09:44:16 -03001000 /* Does the command match? */
1001 if ((abort && cmd != dst->msg[1]) ||
1002 (!abort && cmd != dst->reply))
1003 continue;
1004
1005 /* Does the addressing match? */
1006 if (msg_init != cec_msg_destination(dst) &&
1007 !cec_msg_is_broadcast(dst))
1008 continue;
1009
1010 /* We got a reply */
Hans Verkuil980e0b362016-07-12 11:10:42 -03001011 memcpy(dst->msg, msg->msg, msg->len);
1012 dst->len = msg->len;
1013 dst->rx_ts = msg->rx_ts;
1014 dst->rx_status = msg->rx_status;
Hans Verkuil86e35772016-07-13 04:33:58 -03001015 if (abort)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001016 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
Hans Verkuiladc0c622016-11-01 08:55:05 -02001017 msg->flags = dst->flags;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001018 /* Remove it from the wait_queue */
1019 list_del_init(&data->list);
1020
1021 /* Cancel the pending timeout work */
1022 if (!cancel_delayed_work(&data->work)) {
1023 mutex_unlock(&adap->lock);
1024 flush_scheduled_work();
1025 mutex_lock(&adap->lock);
1026 }
1027 /*
1028 * Mark this as a reply, provided someone is still
1029 * waiting for the answer.
1030 */
1031 if (data->fh)
1032 is_reply = true;
1033 cec_data_completed(data);
1034 break;
1035 }
1036 }
1037 mutex_unlock(&adap->lock);
1038
1039 /* Pass the message on to any monitoring filehandles */
1040 cec_queue_msg_monitor(adap, msg, valid_la);
1041
1042 /* We're done if it is not for us or a poll message */
1043 if (!valid_la || msg->len <= 1)
1044 return;
1045
Hans Verkuil3e92d8b2016-08-12 13:32:07 -03001046 if (adap->log_addrs.log_addr_mask == 0)
1047 return;
1048
Hans Verkuil9881fe02016-06-25 09:44:16 -03001049 /*
1050 * Process the message on the protocol level. If is_reply is true,
1051 * then cec_receive_notify() won't pass on the reply to the listener(s)
1052 * since that was already done by cec_data_completed() above.
1053 */
1054 cec_receive_notify(adap, msg, is_reply);
1055}
1056EXPORT_SYMBOL_GPL(cec_received_msg);
1057
1058/* Logical Address Handling */
1059
1060/*
1061 * Attempt to claim a specific logical address.
1062 *
1063 * This function is called with adap->lock held.
1064 */
1065static int cec_config_log_addr(struct cec_adapter *adap,
1066 unsigned int idx,
1067 unsigned int log_addr)
1068{
1069 struct cec_log_addrs *las = &adap->log_addrs;
1070 struct cec_msg msg = { };
1071 int err;
1072
1073 if (cec_has_log_addr(adap, log_addr))
1074 return 0;
1075
1076 /* Send poll message */
1077 msg.len = 1;
Hans Verkuil42980da2017-02-11 09:24:46 -02001078 msg.msg[0] = (log_addr << 4) | log_addr;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001079 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1080
1081 /*
1082 * While trying to poll the physical address was reset
1083 * and the adapter was unconfigured, so bail out.
1084 */
1085 if (!adap->is_configuring)
1086 return -EINTR;
1087
1088 if (err)
1089 return err;
1090
1091 if (msg.tx_status & CEC_TX_STATUS_OK)
1092 return 0;
1093
1094 /*
1095 * Message not acknowledged, so this logical
1096 * address is free to use.
1097 */
1098 err = adap->ops->adap_log_addr(adap, log_addr);
1099 if (err)
1100 return err;
1101
1102 las->log_addr[idx] = log_addr;
1103 las->log_addr_mask |= 1 << log_addr;
1104 adap->phys_addrs[log_addr] = adap->phys_addr;
1105
1106 dprintk(2, "claimed addr %d (%d)\n", log_addr,
1107 las->primary_device_type[idx]);
1108 return 1;
1109}
1110
1111/*
1112 * Unconfigure the adapter: clear all logical addresses and send
1113 * the state changed event.
1114 *
1115 * This function is called with adap->lock held.
1116 */
1117static void cec_adap_unconfigure(struct cec_adapter *adap)
1118{
1119 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1120 adap->log_addrs.log_addr_mask = 0;
1121 adap->is_configuring = false;
1122 adap->is_configured = false;
1123 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1124 wake_up_interruptible(&adap->kthread_waitq);
1125 cec_post_state_event(adap);
1126}
1127
1128/*
1129 * Attempt to claim the required logical addresses.
1130 */
1131static int cec_config_thread_func(void *arg)
1132{
1133 /* The various LAs for each type of device */
1134 static const u8 tv_log_addrs[] = {
1135 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1136 CEC_LOG_ADDR_INVALID
1137 };
1138 static const u8 record_log_addrs[] = {
1139 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1140 CEC_LOG_ADDR_RECORD_3,
1141 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1142 CEC_LOG_ADDR_INVALID
1143 };
1144 static const u8 tuner_log_addrs[] = {
1145 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1146 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1147 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1148 CEC_LOG_ADDR_INVALID
1149 };
1150 static const u8 playback_log_addrs[] = {
1151 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1152 CEC_LOG_ADDR_PLAYBACK_3,
1153 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1154 CEC_LOG_ADDR_INVALID
1155 };
1156 static const u8 audiosystem_log_addrs[] = {
1157 CEC_LOG_ADDR_AUDIOSYSTEM,
1158 CEC_LOG_ADDR_INVALID
1159 };
1160 static const u8 specific_use_log_addrs[] = {
1161 CEC_LOG_ADDR_SPECIFIC,
1162 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1163 CEC_LOG_ADDR_INVALID
1164 };
1165 static const u8 *type2addrs[6] = {
1166 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1167 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1168 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1169 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1170 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1171 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1172 };
1173 static const u16 type2mask[] = {
1174 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1175 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1176 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1177 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1178 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1179 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1180 };
1181 struct cec_adapter *adap = arg;
1182 struct cec_log_addrs *las = &adap->log_addrs;
1183 int err;
1184 int i, j;
1185
1186 mutex_lock(&adap->lock);
1187 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1188 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1189 las->log_addr_mask = 0;
1190
1191 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1192 goto configured;
1193
1194 for (i = 0; i < las->num_log_addrs; i++) {
1195 unsigned int type = las->log_addr_type[i];
1196 const u8 *la_list;
1197 u8 last_la;
1198
1199 /*
1200 * The TV functionality can only map to physical address 0.
1201 * For any other address, try the Specific functionality
1202 * instead as per the spec.
1203 */
1204 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1205 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1206
1207 la_list = type2addrs[type];
1208 last_la = las->log_addr[i];
1209 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1210 if (last_la == CEC_LOG_ADDR_INVALID ||
1211 last_la == CEC_LOG_ADDR_UNREGISTERED ||
Hans Verkuilf9f96fc2017-01-10 09:44:54 -02001212 !((1 << last_la) & type2mask[type]))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001213 last_la = la_list[0];
1214
1215 err = cec_config_log_addr(adap, i, last_la);
1216 if (err > 0) /* Reused last LA */
1217 continue;
1218
1219 if (err < 0)
1220 goto unconfigure;
1221
1222 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1223 /* Tried this one already, skip it */
1224 if (la_list[j] == last_la)
1225 continue;
1226 /* The backup addresses are CEC 2.0 specific */
1227 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1228 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1229 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1230 continue;
1231
1232 err = cec_config_log_addr(adap, i, la_list[j]);
1233 if (err == 0) /* LA is in use */
1234 continue;
1235 if (err < 0)
1236 goto unconfigure;
1237 /* Done, claimed an LA */
1238 break;
1239 }
1240
1241 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1242 dprintk(1, "could not claim LA %d\n", i);
1243 }
1244
Hans Verkuildcceb1e2016-08-10 09:24:45 -03001245 if (adap->log_addrs.log_addr_mask == 0 &&
1246 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1247 goto unconfigure;
1248
Hans Verkuil9881fe02016-06-25 09:44:16 -03001249configured:
1250 if (adap->log_addrs.log_addr_mask == 0) {
1251 /* Fall back to unregistered */
1252 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1253 las->log_addr_mask = 1 << las->log_addr[0];
Hans Verkuil0c1d61b2016-08-14 08:27:09 -03001254 for (i = 1; i < las->num_log_addrs; i++)
1255 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001256 }
Hans Verkuil7af26f82016-12-09 11:54:06 -02001257 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1258 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001259 adap->is_configured = true;
1260 adap->is_configuring = false;
1261 cec_post_state_event(adap);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001262
Hans Verkuilf60f3562016-12-09 12:00:49 -02001263 /*
1264 * Now post the Report Features and Report Physical Address broadcast
1265 * messages. Note that these are non-blocking transmits, meaning that
1266 * they are just queued up and once adap->lock is unlocked the main
1267 * thread will kick in and start transmitting these.
1268 *
1269 * If after this function is done (but before one or more of these
1270 * messages are actually transmitted) the CEC adapter is unconfigured,
1271 * then any remaining messages will be dropped by the main thread.
1272 */
Hans Verkuil9881fe02016-06-25 09:44:16 -03001273 for (i = 0; i < las->num_log_addrs; i++) {
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001274 struct cec_msg msg = {};
1275
Hans Verkuila69a1682016-11-02 07:41:41 -02001276 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1277 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001278 continue;
1279
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001280 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1281
1282 /* Report Features must come first according to CEC 2.0 */
1283 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1284 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1285 cec_fill_msg_report_features(adap, &msg, i);
Hans Verkuilf60f3562016-12-09 12:00:49 -02001286 cec_transmit_msg_fh(adap, &msg, NULL, false);
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001287 }
1288
Hans Verkuild3d64bc2016-12-09 11:48:34 -02001289 /* Report Physical Address */
1290 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1291 las->primary_device_type[i]);
1292 dprintk(2, "config: la %d pa %x.%x.%x.%x\n",
1293 las->log_addr[i],
1294 cec_phys_addr_exp(adap->phys_addr));
Hans Verkuilf60f3562016-12-09 12:00:49 -02001295 cec_transmit_msg_fh(adap, &msg, NULL, false);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001296 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001297 adap->kthread_config = NULL;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001298 complete(&adap->config_completion);
Hans Verkuilf60f3562016-12-09 12:00:49 -02001299 mutex_unlock(&adap->lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001300 return 0;
1301
1302unconfigure:
1303 for (i = 0; i < las->num_log_addrs; i++)
1304 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1305 cec_adap_unconfigure(adap);
1306 adap->kthread_config = NULL;
1307 mutex_unlock(&adap->lock);
1308 complete(&adap->config_completion);
1309 return 0;
1310}
1311
1312/*
1313 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1314 * logical addresses.
1315 *
1316 * This function is called with adap->lock held.
1317 */
1318static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1319{
1320 if (WARN_ON(adap->is_configuring || adap->is_configured))
1321 return;
1322
1323 init_completion(&adap->config_completion);
1324
1325 /* Ready to kick off the thread */
1326 adap->is_configuring = true;
1327 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1328 "ceccfg-%s", adap->name);
1329 if (IS_ERR(adap->kthread_config)) {
1330 adap->kthread_config = NULL;
1331 } else if (block) {
1332 mutex_unlock(&adap->lock);
1333 wait_for_completion(&adap->config_completion);
1334 mutex_lock(&adap->lock);
1335 }
1336}
1337
1338/* Set a new physical address and send an event notifying userspace of this.
1339 *
1340 * This function is called with adap->lock held.
1341 */
1342void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1343{
Hans Verkuilc000e5d2016-07-10 10:11:17 -03001344 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001345 return;
1346
1347 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1348 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1349 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1350 cec_post_state_event(adap);
1351 cec_adap_unconfigure(adap);
1352 /* Disabling monitor all mode should always succeed */
1353 if (adap->monitor_all_cnt)
1354 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1355 WARN_ON(adap->ops->adap_enable(adap, false));
1356 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1357 return;
1358 }
1359
1360 if (adap->ops->adap_enable(adap, true))
1361 return;
1362
1363 if (adap->monitor_all_cnt &&
1364 call_op(adap, adap_monitor_all_enable, true)) {
1365 WARN_ON(adap->ops->adap_enable(adap, false));
1366 return;
1367 }
1368 adap->phys_addr = phys_addr;
1369 cec_post_state_event(adap);
1370 if (adap->log_addrs.num_log_addrs)
1371 cec_claim_log_addrs(adap, block);
1372}
1373
1374void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1375{
1376 if (IS_ERR_OR_NULL(adap))
1377 return;
1378
Hans Verkuil9881fe02016-06-25 09:44:16 -03001379 mutex_lock(&adap->lock);
1380 __cec_s_phys_addr(adap, phys_addr, block);
1381 mutex_unlock(&adap->lock);
1382}
1383EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1384
1385/*
1386 * Called from either the ioctl or a driver to set the logical addresses.
1387 *
1388 * This function is called with adap->lock held.
1389 */
1390int __cec_s_log_addrs(struct cec_adapter *adap,
1391 struct cec_log_addrs *log_addrs, bool block)
1392{
1393 u16 type_mask = 0;
1394 int i;
1395
Hans Verkuilc000e5d2016-07-10 10:11:17 -03001396 if (adap->devnode.unregistered)
1397 return -ENODEV;
1398
Hans Verkuil9881fe02016-06-25 09:44:16 -03001399 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1400 adap->log_addrs.num_log_addrs = 0;
1401 cec_adap_unconfigure(adap);
1402 return 0;
1403 }
1404
Hans Verkuila69a1682016-11-02 07:41:41 -02001405 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1406 /*
1407 * Sanitize log_addrs fields if a CDC-Only device is
1408 * requested.
1409 */
1410 log_addrs->num_log_addrs = 1;
1411 log_addrs->osd_name[0] = '\0';
1412 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1413 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1414 /*
1415 * This is just an internal convention since a CDC-Only device
1416 * doesn't have to be a switch. But switches already use
1417 * unregistered, so it makes some kind of sense to pick this
1418 * as the primary device. Since a CDC-Only device never sends
1419 * any 'normal' CEC messages this primary device type is never
1420 * sent over the CEC bus.
1421 */
1422 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1423 log_addrs->all_device_types[0] = 0;
1424 log_addrs->features[0][0] = 0;
1425 log_addrs->features[0][1] = 0;
1426 }
1427
Hans Verkuil9881fe02016-06-25 09:44:16 -03001428 /* Ensure the osd name is 0-terminated */
1429 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1430
1431 /* Sanity checks */
1432 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1433 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1434 return -EINVAL;
1435 }
1436
1437 /*
1438 * Vendor ID is a 24 bit number, so check if the value is
1439 * within the correct range.
1440 */
1441 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1442 (log_addrs->vendor_id & 0xff000000) != 0)
1443 return -EINVAL;
1444
1445 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1446 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0)
1447 return -EINVAL;
1448
1449 if (log_addrs->num_log_addrs > 1)
1450 for (i = 0; i < log_addrs->num_log_addrs; i++)
1451 if (log_addrs->log_addr_type[i] ==
1452 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1453 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1454 return -EINVAL;
1455 }
1456
Hans Verkuil9881fe02016-06-25 09:44:16 -03001457 for (i = 0; i < log_addrs->num_log_addrs; i++) {
Hans Verkuil009a6202016-07-18 03:44:10 -03001458 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001459 u8 *features = log_addrs->features[i];
1460 bool op_is_dev_features = false;
Hans Verkuila161bef2016-11-04 10:52:10 -02001461 unsigned j;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001462
1463 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1464 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1465 dprintk(1, "duplicate logical address type\n");
1466 return -EINVAL;
1467 }
1468 type_mask |= 1 << log_addrs->log_addr_type[i];
1469 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1470 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1471 /* Record already contains the playback functionality */
1472 dprintk(1, "invalid record + playback combination\n");
1473 return -EINVAL;
1474 }
1475 if (log_addrs->primary_device_type[i] >
1476 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1477 dprintk(1, "unknown primary device type\n");
1478 return -EINVAL;
1479 }
1480 if (log_addrs->primary_device_type[i] == 2) {
1481 dprintk(1, "invalid primary device type\n");
1482 return -EINVAL;
1483 }
1484 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1485 dprintk(1, "unknown logical address type\n");
1486 return -EINVAL;
1487 }
Hans Verkuila161bef2016-11-04 10:52:10 -02001488 for (j = 0; j < feature_sz; j++) {
1489 if ((features[j] & 0x80) == 0) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001490 if (op_is_dev_features)
1491 break;
1492 op_is_dev_features = true;
1493 }
1494 }
Hans Verkuila161bef2016-11-04 10:52:10 -02001495 if (!op_is_dev_features || j == feature_sz) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001496 dprintk(1, "malformed features\n");
1497 return -EINVAL;
1498 }
Hans Verkuil009a6202016-07-18 03:44:10 -03001499 /* Zero unused part of the feature array */
Hans Verkuila161bef2016-11-04 10:52:10 -02001500 memset(features + j + 1, 0, feature_sz - j - 1);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001501 }
1502
1503 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1504 if (log_addrs->num_log_addrs > 2) {
1505 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1506 return -EINVAL;
1507 }
1508 if (log_addrs->num_log_addrs == 2) {
1509 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1510 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1511 dprintk(1, "Two LAs is only allowed for audiosystem and TV\n");
1512 return -EINVAL;
1513 }
1514 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1515 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1516 dprintk(1, "An audiosystem/TV can only be combined with record or playback\n");
1517 return -EINVAL;
1518 }
1519 }
1520 }
1521
Hans Verkuil009a6202016-07-18 03:44:10 -03001522 /* Zero unused LAs */
1523 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1524 log_addrs->primary_device_type[i] = 0;
1525 log_addrs->log_addr_type[i] = 0;
1526 log_addrs->all_device_types[i] = 0;
1527 memset(log_addrs->features[i], 0,
1528 sizeof(log_addrs->features[i]));
1529 }
1530
Hans Verkuil9881fe02016-06-25 09:44:16 -03001531 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1532 adap->log_addrs = *log_addrs;
1533 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1534 cec_claim_log_addrs(adap, block);
1535 return 0;
1536}
1537
1538int cec_s_log_addrs(struct cec_adapter *adap,
1539 struct cec_log_addrs *log_addrs, bool block)
1540{
1541 int err;
1542
Hans Verkuil9881fe02016-06-25 09:44:16 -03001543 mutex_lock(&adap->lock);
1544 err = __cec_s_log_addrs(adap, log_addrs, block);
1545 mutex_unlock(&adap->lock);
1546 return err;
1547}
1548EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1549
1550/* High-level core CEC message handling */
1551
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001552/* Fill in the Report Features message */
1553static void cec_fill_msg_report_features(struct cec_adapter *adap,
1554 struct cec_msg *msg,
1555 unsigned int la_idx)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001556{
Hans Verkuil9881fe02016-06-25 09:44:16 -03001557 const struct cec_log_addrs *las = &adap->log_addrs;
1558 const u8 *features = las->features[la_idx];
1559 bool op_is_dev_features = false;
1560 unsigned int idx;
1561
Hans Verkuil9881fe02016-06-25 09:44:16 -03001562 /* Report Features */
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001563 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1564 msg->len = 4;
1565 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1566 msg->msg[2] = adap->log_addrs.cec_version;
1567 msg->msg[3] = las->all_device_types[la_idx];
Hans Verkuil9881fe02016-06-25 09:44:16 -03001568
1569 /* Write RC Profiles first, then Device Features */
1570 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001571 msg->msg[msg->len++] = features[idx];
Hans Verkuil9881fe02016-06-25 09:44:16 -03001572 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1573 if (op_is_dev_features)
1574 break;
1575 op_is_dev_features = true;
1576 }
1577 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001578}
1579
Hans Verkuil9881fe02016-06-25 09:44:16 -03001580/* Transmit the Feature Abort message */
1581static int cec_feature_abort_reason(struct cec_adapter *adap,
1582 struct cec_msg *msg, u8 reason)
1583{
1584 struct cec_msg tx_msg = { };
1585
1586 /*
1587 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1588 * message!
1589 */
1590 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1591 return 0;
1592 cec_msg_set_reply_to(&tx_msg, msg);
1593 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1594 return cec_transmit_msg(adap, &tx_msg, false);
1595}
1596
1597static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1598{
1599 return cec_feature_abort_reason(adap, msg,
1600 CEC_OP_ABORT_UNRECOGNIZED_OP);
1601}
1602
1603static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1604{
1605 return cec_feature_abort_reason(adap, msg,
1606 CEC_OP_ABORT_REFUSED);
1607}
1608
1609/*
1610 * Called when a CEC message is received. This function will do any
1611 * necessary core processing. The is_reply bool is true if this message
1612 * is a reply to an earlier transmit.
1613 *
1614 * The message is either a broadcast message or a valid directed message.
1615 */
1616static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1617 bool is_reply)
1618{
1619 bool is_broadcast = cec_msg_is_broadcast(msg);
1620 u8 dest_laddr = cec_msg_destination(msg);
1621 u8 init_laddr = cec_msg_initiator(msg);
1622 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1623 int la_idx = cec_log_addr2idx(adap, dest_laddr);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001624 bool from_unregistered = init_laddr == 0xf;
1625 struct cec_msg tx_cec_msg = { };
1626
1627 dprintk(1, "cec_receive_notify: %*ph\n", msg->len, msg->msg);
1628
Hans Verkuila69a1682016-11-02 07:41:41 -02001629 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1630 if (cec_is_cdc_only(&adap->log_addrs) &&
1631 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1632 return 0;
1633
Hans Verkuil9881fe02016-06-25 09:44:16 -03001634 if (adap->ops->received) {
1635 /* Allow drivers to process the message first */
1636 if (adap->ops->received(adap, msg) != -ENOMSG)
1637 return 0;
1638 }
1639
1640 /*
1641 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1642 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1643 * handled by the CEC core, even if the passthrough mode is on.
1644 * The others are just ignored if passthrough mode is on.
1645 */
1646 switch (msg->msg[1]) {
1647 case CEC_MSG_GET_CEC_VERSION:
1648 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1649 case CEC_MSG_ABORT:
1650 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1651 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1652 case CEC_MSG_GIVE_OSD_NAME:
1653 case CEC_MSG_GIVE_FEATURES:
1654 /*
1655 * Skip processing these messages if the passthrough mode
1656 * is on.
1657 */
1658 if (adap->passthrough)
1659 goto skip_processing;
1660 /* Ignore if addressing is wrong */
1661 if (is_broadcast || from_unregistered)
1662 return 0;
1663 break;
1664
1665 case CEC_MSG_USER_CONTROL_PRESSED:
1666 case CEC_MSG_USER_CONTROL_RELEASED:
1667 /* Wrong addressing mode: don't process */
1668 if (is_broadcast || from_unregistered)
1669 goto skip_processing;
1670 break;
1671
1672 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1673 /*
1674 * This message is always processed, regardless of the
1675 * passthrough setting.
1676 *
1677 * Exception: don't process if wrong addressing mode.
1678 */
1679 if (!is_broadcast)
1680 goto skip_processing;
1681 break;
1682
1683 default:
1684 break;
1685 }
1686
1687 cec_msg_set_reply_to(&tx_cec_msg, msg);
1688
1689 switch (msg->msg[1]) {
1690 /* The following messages are processed but still passed through */
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001691 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1692 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1693
1694 if (!from_unregistered)
1695 adap->phys_addrs[init_laddr] = pa;
1696 dprintk(1, "Reported physical address %x.%x.%x.%x for logical address %d\n",
1697 cec_phys_addr_exp(pa), init_laddr);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001698 break;
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001699 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001700
1701 case CEC_MSG_USER_CONTROL_PRESSED:
Hans Verkuilf4062622016-11-01 07:59:34 -02001702 if (!(adap->capabilities & CEC_CAP_RC) ||
1703 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001704 break;
1705
Hans Verkuil5bb23992016-07-01 07:33:10 -03001706#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001707 switch (msg->msg[2]) {
1708 /*
1709 * Play function, this message can have variable length
1710 * depending on the specific play function that is used.
1711 */
1712 case 0x60:
1713 if (msg->len == 2)
1714 rc_keydown(adap->rc, RC_TYPE_CEC,
1715 msg->msg[2], 0);
1716 else
1717 rc_keydown(adap->rc, RC_TYPE_CEC,
1718 msg->msg[2] << 8 | msg->msg[3], 0);
1719 break;
1720 /*
1721 * Other function messages that are not handled.
1722 * Currently the RC framework does not allow to supply an
1723 * additional parameter to a keypress. These "keys" contain
1724 * other information such as channel number, an input number
1725 * etc.
1726 * For the time being these messages are not processed by the
1727 * framework and are simply forwarded to the user space.
1728 */
1729 case 0x56: case 0x57:
1730 case 0x67: case 0x68: case 0x69: case 0x6a:
1731 break;
1732 default:
1733 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1734 break;
1735 }
1736#endif
1737 break;
1738
1739 case CEC_MSG_USER_CONTROL_RELEASED:
Hans Verkuilf4062622016-11-01 07:59:34 -02001740 if (!(adap->capabilities & CEC_CAP_RC) ||
1741 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001742 break;
Hans Verkuil5bb23992016-07-01 07:33:10 -03001743#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001744 rc_keyup(adap->rc);
1745#endif
1746 break;
1747
1748 /*
1749 * The remaining messages are only processed if the passthrough mode
1750 * is off.
1751 */
1752 case CEC_MSG_GET_CEC_VERSION:
1753 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1754 return cec_transmit_msg(adap, &tx_cec_msg, false);
1755
1756 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1757 /* Do nothing for CEC switches using addr 15 */
1758 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1759 return 0;
1760 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1761 return cec_transmit_msg(adap, &tx_cec_msg, false);
1762
1763 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1764 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1765 return cec_feature_abort(adap, msg);
1766 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1767 return cec_transmit_msg(adap, &tx_cec_msg, false);
1768
1769 case CEC_MSG_ABORT:
1770 /* Do nothing for CEC switches */
1771 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1772 return 0;
1773 return cec_feature_refused(adap, msg);
1774
1775 case CEC_MSG_GIVE_OSD_NAME: {
1776 if (adap->log_addrs.osd_name[0] == 0)
1777 return cec_feature_abort(adap, msg);
1778 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1779 return cec_transmit_msg(adap, &tx_cec_msg, false);
1780 }
1781
1782 case CEC_MSG_GIVE_FEATURES:
Hans Verkuila24f56d2016-12-09 11:28:19 -02001783 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1784 return cec_feature_abort(adap, msg);
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001785 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1786 return cec_transmit_msg(adap, &tx_cec_msg, false);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001787
1788 default:
1789 /*
1790 * Unprocessed messages are aborted if userspace isn't doing
1791 * any processing either.
1792 */
Hans Verkuila179b692016-08-24 05:36:53 -03001793 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
Hans Verkuil9881fe02016-06-25 09:44:16 -03001794 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1795 return cec_feature_abort(adap, msg);
1796 break;
1797 }
1798
1799skip_processing:
Hans Verkuiladc0c622016-11-01 08:55:05 -02001800 /* If this was a reply, then we're done, unless otherwise specified */
1801 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001802 return 0;
1803
1804 /*
1805 * Send to the exclusive follower if there is one, otherwise send
1806 * to all followers.
1807 */
1808 if (adap->cec_follower)
1809 cec_queue_msg_fh(adap->cec_follower, msg);
1810 else
1811 cec_queue_msg_followers(adap, msg);
1812 return 0;
1813}
1814
1815/*
1816 * Helper functions to keep track of the 'monitor all' use count.
1817 *
1818 * These functions are called with adap->lock held.
1819 */
1820int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1821{
1822 int ret = 0;
1823
1824 if (adap->monitor_all_cnt == 0)
1825 ret = call_op(adap, adap_monitor_all_enable, 1);
1826 if (ret == 0)
1827 adap->monitor_all_cnt++;
1828 return ret;
1829}
1830
1831void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1832{
1833 adap->monitor_all_cnt--;
1834 if (adap->monitor_all_cnt == 0)
1835 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1836}
1837
1838#ifdef CONFIG_MEDIA_CEC_DEBUG
1839/*
1840 * Log the current state of the CEC adapter.
1841 * Very useful for debugging.
1842 */
1843int cec_adap_status(struct seq_file *file, void *priv)
1844{
1845 struct cec_adapter *adap = dev_get_drvdata(file->private);
1846 struct cec_data *data;
1847
1848 mutex_lock(&adap->lock);
1849 seq_printf(file, "configured: %d\n", adap->is_configured);
1850 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1851 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1852 cec_phys_addr_exp(adap->phys_addr));
1853 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1854 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1855 if (adap->cec_follower)
1856 seq_printf(file, "has CEC follower%s\n",
1857 adap->passthrough ? " (in passthrough mode)" : "");
1858 if (adap->cec_initiator)
1859 seq_puts(file, "has CEC initiator\n");
1860 if (adap->monitor_all_cnt)
1861 seq_printf(file, "file handles in Monitor All mode: %u\n",
1862 adap->monitor_all_cnt);
1863 data = adap->transmitting;
1864 if (data)
Hans Verkuil11065f82016-07-17 11:40:05 -03001865 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1866 data->msg.len, data->msg.msg, data->msg.reply,
1867 data->msg.timeout);
1868 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001869 list_for_each_entry(data, &adap->transmit_queue, list) {
Hans Verkuil11065f82016-07-17 11:40:05 -03001870 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1871 data->msg.len, data->msg.msg, data->msg.reply,
1872 data->msg.timeout);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001873 }
1874 list_for_each_entry(data, &adap->wait_queue, list) {
Hans Verkuil11065f82016-07-17 11:40:05 -03001875 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1876 data->msg.len, data->msg.msg, data->msg.reply,
1877 data->msg.timeout);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001878 }
1879
1880 call_void_op(adap, adap_status, file);
1881 mutex_unlock(&adap->lock);
1882 return 0;
1883}
1884#endif