blob: f3d495654a53fbe85f6773f7aacdd6a7354ab120 [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/*
303 * Main CEC state machine
304 *
305 * Wait until the thread should be stopped, or we are not transmitting and
306 * a new transmit message is queued up, in which case we start transmitting
307 * that message. When the adapter finished transmitting the message it will
308 * call cec_transmit_done().
309 *
310 * If the adapter is disabled, then remove all queued messages instead.
311 *
312 * If the current transmit times out, then cancel that transmit.
313 */
314int cec_thread_func(void *_adap)
315{
316 struct cec_adapter *adap = _adap;
317
318 for (;;) {
319 unsigned int signal_free_time;
320 struct cec_data *data;
321 bool timeout = false;
322 u8 attempts;
323
324 if (adap->transmitting) {
325 int err;
326
327 /*
328 * We are transmitting a message, so add a timeout
329 * to prevent the state machine to get stuck waiting
330 * for this message to finalize and add a check to
331 * see if the adapter is disabled in which case the
332 * transmit should be canceled.
333 */
334 err = wait_event_interruptible_timeout(adap->kthread_waitq,
335 kthread_should_stop() ||
Hans Verkuil58ecc292016-07-17 11:42:06 -0300336 (!adap->is_configured && !adap->is_configuring) ||
Hans Verkuil9881fe02016-06-25 09:44:16 -0300337 (!adap->transmitting &&
338 !list_empty(&adap->transmit_queue)),
339 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
340 timeout = err == 0;
341 } else {
342 /* Otherwise we just wait for something to happen. */
343 wait_event_interruptible(adap->kthread_waitq,
344 kthread_should_stop() ||
345 (!adap->transmitting &&
346 !list_empty(&adap->transmit_queue)));
347 }
348
349 mutex_lock(&adap->lock);
350
Hans Verkuil58ecc292016-07-17 11:42:06 -0300351 if ((!adap->is_configured && !adap->is_configuring) ||
Hans Verkuil9881fe02016-06-25 09:44:16 -0300352 kthread_should_stop()) {
353 /*
354 * If the adapter is disabled, or we're asked to stop,
355 * then cancel any pending transmits.
356 */
357 while (!list_empty(&adap->transmit_queue)) {
358 data = list_first_entry(&adap->transmit_queue,
359 struct cec_data, list);
360 cec_data_cancel(data);
361 }
362 if (adap->transmitting)
363 cec_data_cancel(adap->transmitting);
364
365 /*
366 * Cancel the pending timeout work. We have to unlock
367 * the mutex when flushing the work since
368 * cec_wait_timeout() will take it. This is OK since
369 * no new entries can be added to wait_queue as long
370 * as adap->transmitting is NULL, which it is due to
371 * the cec_data_cancel() above.
372 */
373 while (!list_empty(&adap->wait_queue)) {
374 data = list_first_entry(&adap->wait_queue,
375 struct cec_data, list);
376
377 if (!cancel_delayed_work(&data->work)) {
378 mutex_unlock(&adap->lock);
379 flush_scheduled_work();
380 mutex_lock(&adap->lock);
381 }
382 cec_data_cancel(data);
383 }
384 goto unlock;
385 }
386
387 if (adap->transmitting && timeout) {
388 /*
389 * If we timeout, then log that. This really shouldn't
390 * happen and is an indication of a faulty CEC adapter
391 * driver, or the CEC bus is in some weird state.
392 */
393 dprintk(0, "message %*ph timed out!\n",
394 adap->transmitting->msg.len,
395 adap->transmitting->msg.msg);
396 /* Just give up on this. */
397 cec_data_cancel(adap->transmitting);
398 goto unlock;
399 }
400
401 /*
402 * If we are still transmitting, or there is nothing new to
403 * transmit, then just continue waiting.
404 */
405 if (adap->transmitting || list_empty(&adap->transmit_queue))
406 goto unlock;
407
408 /* Get a new message to transmit */
409 data = list_first_entry(&adap->transmit_queue,
410 struct cec_data, list);
411 list_del_init(&data->list);
Hans Verkuil11065f82016-07-17 11:40:05 -0300412 adap->transmit_queue_sz--;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300413 /* Make this the current transmitting message */
414 adap->transmitting = data;
415
416 /*
417 * Suggested number of attempts as per the CEC 2.0 spec:
418 * 4 attempts is the default, except for 'secondary poll
419 * messages', i.e. poll messages not sent during the adapter
420 * configuration phase when it allocates logical addresses.
421 */
422 if (data->msg.len == 1 && adap->is_configured)
423 attempts = 2;
424 else
425 attempts = 4;
426
427 /* Set the suggested signal free time */
428 if (data->attempts) {
429 /* should be >= 3 data bit periods for a retry */
430 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
431 } else if (data->new_initiator) {
432 /* should be >= 5 data bit periods for new initiator */
433 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
434 } else {
435 /*
436 * should be >= 7 data bit periods for sending another
437 * frame immediately after another.
438 */
439 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
440 }
441 if (data->attempts == 0)
442 data->attempts = attempts;
443
444 /* Tell the adapter to transmit, cancel on error */
445 if (adap->ops->adap_transmit(adap, data->attempts,
446 signal_free_time, &data->msg))
447 cec_data_cancel(data);
448
449unlock:
450 mutex_unlock(&adap->lock);
451
452 if (kthread_should_stop())
453 break;
454 }
455 return 0;
456}
457
458/*
459 * Called by the CEC adapter if a transmit finished.
460 */
461void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
462 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
463{
464 struct cec_data *data;
465 struct cec_msg *msg;
Hans Verkuil980e0b362016-07-12 11:10:42 -0300466 u64 ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300467
468 dprintk(2, "cec_transmit_done %02x\n", status);
469 mutex_lock(&adap->lock);
470 data = adap->transmitting;
471 if (!data) {
472 /*
473 * This can happen if a transmit was issued and the cable is
474 * unplugged while the transmit is ongoing. Ignore this
475 * transmit in that case.
476 */
477 dprintk(1, "cec_transmit_done without an ongoing transmit!\n");
478 goto unlock;
479 }
480
481 msg = &data->msg;
482
483 /* Drivers must fill in the status! */
484 WARN_ON(status == 0);
Hans Verkuil980e0b362016-07-12 11:10:42 -0300485 msg->tx_ts = ts;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300486 msg->tx_status |= status;
487 msg->tx_arb_lost_cnt += arb_lost_cnt;
488 msg->tx_nack_cnt += nack_cnt;
489 msg->tx_low_drive_cnt += low_drive_cnt;
490 msg->tx_error_cnt += error_cnt;
491
492 /* Mark that we're done with this transmit */
493 adap->transmitting = NULL;
494
495 /*
496 * If there are still retry attempts left and there was an error and
497 * the hardware didn't signal that it retried itself (by setting
498 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
499 */
500 if (data->attempts > 1 &&
501 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
502 /* Retry this message */
503 data->attempts--;
504 /* Add the message in front of the transmit queue */
505 list_add(&data->list, &adap->transmit_queue);
Hans Verkuil11065f82016-07-17 11:40:05 -0300506 adap->transmit_queue_sz++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300507 goto wake_thread;
508 }
509
510 data->attempts = 0;
511
512 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
513 if (!(status & CEC_TX_STATUS_OK))
514 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
515
516 /* Queue transmitted message for monitoring purposes */
517 cec_queue_msg_monitor(adap, msg, 1);
518
Hans Verkuil86e35772016-07-13 04:33:58 -0300519 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
520 msg->timeout) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300521 /*
522 * Queue the message into the wait queue if we want to wait
523 * for a reply.
524 */
525 list_add_tail(&data->list, &adap->wait_queue);
526 schedule_delayed_work(&data->work,
527 msecs_to_jiffies(msg->timeout));
528 } else {
529 /* Otherwise we're done */
530 cec_data_completed(data);
531 }
532
533wake_thread:
534 /*
535 * Wake up the main thread to see if another message is ready
536 * for transmitting or to retry the current message.
537 */
538 wake_up_interruptible(&adap->kthread_waitq);
539unlock:
540 mutex_unlock(&adap->lock);
541}
542EXPORT_SYMBOL_GPL(cec_transmit_done);
543
544/*
545 * Called when waiting for a reply times out.
546 */
547static void cec_wait_timeout(struct work_struct *work)
548{
549 struct cec_data *data = container_of(work, struct cec_data, work.work);
550 struct cec_adapter *adap = data->adap;
551
552 mutex_lock(&adap->lock);
553 /*
554 * Sanity check in case the timeout and the arrival of the message
555 * happened at the same time.
556 */
557 if (list_empty(&data->list))
558 goto unlock;
559
560 /* Mark the message as timed out */
561 list_del_init(&data->list);
Hans Verkuil980e0b362016-07-12 11:10:42 -0300562 data->msg.rx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300563 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
564 cec_data_completed(data);
565unlock:
566 mutex_unlock(&adap->lock);
567}
568
569/*
570 * Transmit a message. The fh argument may be NULL if the transmit is not
571 * associated with a specific filehandle.
572 *
573 * This function is called with adap->lock held.
574 */
575int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
576 struct cec_fh *fh, bool block)
577{
578 struct cec_data *data;
579 u8 last_initiator = 0xff;
580 unsigned int timeout;
581 int res = 0;
582
Hans Verkuil40df3a72016-07-16 09:44:13 -0300583 msg->rx_ts = 0;
584 msg->tx_ts = 0;
585 msg->rx_status = 0;
586 msg->tx_status = 0;
587 msg->tx_arb_lost_cnt = 0;
588 msg->tx_nack_cnt = 0;
589 msg->tx_low_drive_cnt = 0;
590 msg->tx_error_cnt = 0;
Hans Verkuil40df3a72016-07-16 09:44:13 -0300591 msg->sequence = ++adap->sequence;
592 if (!msg->sequence)
593 msg->sequence = ++adap->sequence;
594
Hans Verkuil9881fe02016-06-25 09:44:16 -0300595 if (msg->reply && msg->timeout == 0) {
596 /* Make sure the timeout isn't 0. */
597 msg->timeout = 1000;
598 }
Hans Verkuil7ae2a882016-11-04 07:52:11 -0200599 if (msg->timeout)
600 msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
601 else
602 msg->flags = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300603
604 /* Sanity checks */
605 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
606 dprintk(1, "cec_transmit_msg: invalid length %d\n", msg->len);
607 return -EINVAL;
608 }
609 if (msg->timeout && msg->len == 1) {
610 dprintk(1, "cec_transmit_msg: can't reply for poll msg\n");
611 return -EINVAL;
612 }
Hans Verkuil045344c2016-07-17 05:16:40 -0300613 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300614 if (msg->len == 1) {
615 if (cec_msg_initiator(msg) != 0xf ||
616 cec_msg_destination(msg) == 0xf) {
617 dprintk(1, "cec_transmit_msg: invalid poll message\n");
618 return -EINVAL;
619 }
620 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
621 /*
622 * If the destination is a logical address our adapter
623 * has already claimed, then just NACK this.
624 * It depends on the hardware what it will do with a
625 * POLL to itself (some OK this), so it is just as
626 * easy to handle it here so the behavior will be
627 * consistent.
628 */
Hans Verkuil9a3f14e2016-07-15 11:13:49 -0300629 msg->tx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300630 msg->tx_status = CEC_TX_STATUS_NACK |
631 CEC_TX_STATUS_MAX_RETRIES;
632 msg->tx_nack_cnt = 1;
633 return 0;
634 }
635 }
636 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
637 cec_has_log_addr(adap, cec_msg_destination(msg))) {
638 dprintk(1, "cec_transmit_msg: destination is the adapter itself\n");
639 return -EINVAL;
640 }
641 if (cec_msg_initiator(msg) != 0xf &&
642 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
643 dprintk(1, "cec_transmit_msg: initiator has unknown logical address %d\n",
644 cec_msg_initiator(msg));
645 return -EINVAL;
646 }
647 if (!adap->is_configured && !adap->is_configuring)
648 return -ENONET;
649
Hans Verkuil11065f82016-07-17 11:40:05 -0300650 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ)
651 return -EBUSY;
652
Hans Verkuil9881fe02016-06-25 09:44:16 -0300653 data = kzalloc(sizeof(*data), GFP_KERNEL);
654 if (!data)
655 return -ENOMEM;
656
657 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
658 msg->msg[2] = adap->phys_addr >> 8;
659 msg->msg[3] = adap->phys_addr & 0xff;
660 }
661
662 if (msg->timeout)
663 dprintk(2, "cec_transmit_msg: %*ph (wait for 0x%02x%s)\n",
664 msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
665 else
666 dprintk(2, "cec_transmit_msg: %*ph%s\n",
667 msg->len, msg->msg, !block ? " (nb)" : "");
668
Hans Verkuil9881fe02016-06-25 09:44:16 -0300669 data->msg = *msg;
670 data->fh = fh;
671 data->adap = adap;
672 data->blocking = block;
673
674 /*
675 * Determine if this message follows a message from the same
676 * initiator. Needed to determine the free signal time later on.
677 */
678 if (msg->len > 1) {
679 if (!(list_empty(&adap->transmit_queue))) {
680 const struct cec_data *last;
681
682 last = list_last_entry(&adap->transmit_queue,
683 const struct cec_data, list);
684 last_initiator = cec_msg_initiator(&last->msg);
685 } else if (adap->transmitting) {
686 last_initiator =
687 cec_msg_initiator(&adap->transmitting->msg);
688 }
689 }
690 data->new_initiator = last_initiator != cec_msg_initiator(msg);
691 init_completion(&data->c);
692 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
693
Hans Verkuil9881fe02016-06-25 09:44:16 -0300694 if (fh)
695 list_add_tail(&data->xfer_list, &fh->xfer_list);
696 list_add_tail(&data->list, &adap->transmit_queue);
Hans Verkuil11065f82016-07-17 11:40:05 -0300697 adap->transmit_queue_sz++;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300698 if (!adap->transmitting)
699 wake_up_interruptible(&adap->kthread_waitq);
700
701 /* All done if we don't need to block waiting for completion */
702 if (!block)
703 return 0;
704
705 /*
706 * If we don't get a completion before this time something is really
707 * wrong and we time out.
708 */
709 timeout = CEC_XFER_TIMEOUT_MS;
710 /* Add the requested timeout if we have to wait for a reply as well */
711 if (msg->timeout)
712 timeout += msg->timeout;
713
714 /*
715 * Release the lock and wait, retake the lock afterwards.
716 */
717 mutex_unlock(&adap->lock);
718 res = wait_for_completion_killable_timeout(&data->c,
719 msecs_to_jiffies(timeout));
720 mutex_lock(&adap->lock);
721
722 if (data->completed) {
723 /* The transmit completed (possibly with an error) */
724 *msg = data->msg;
725 kfree(data);
726 return 0;
727 }
728 /*
729 * The wait for completion timed out or was interrupted, so mark this
730 * as non-blocking and disconnect from the filehandle since it is
731 * still 'in flight'. When it finally completes it will just drop the
732 * result silently.
733 */
734 data->blocking = false;
735 if (data->fh)
736 list_del(&data->xfer_list);
737 data->fh = NULL;
738
739 if (res == 0) { /* timed out */
740 /* Check if the reply or the transmit failed */
741 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
742 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
743 else
744 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
745 }
746 return res > 0 ? 0 : res;
747}
748
749/* Helper function to be used by drivers and this framework. */
750int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
751 bool block)
752{
753 int ret;
754
755 mutex_lock(&adap->lock);
756 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
757 mutex_unlock(&adap->lock);
758 return ret;
759}
760EXPORT_SYMBOL_GPL(cec_transmit_msg);
761
762/*
763 * I don't like forward references but without this the low-level
764 * cec_received_msg() function would come after a bunch of high-level
765 * CEC protocol handling functions. That was very confusing.
766 */
767static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
768 bool is_reply);
769
Hans Verkuil3074fe42016-11-01 10:48:22 -0200770#define DIRECTED 0x80
771#define BCAST1_4 0x40
772#define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
773#define BCAST (BCAST1_4 | BCAST2_0)
774#define BOTH (BCAST | DIRECTED)
775
776/*
777 * Specify minimum length and whether the message is directed, broadcast
778 * or both. Messages that do not match the criteria are ignored as per
779 * the CEC specification.
780 */
781static const u8 cec_msg_size[256] = {
782 [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
783 [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
784 [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
785 [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
786 [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
787 [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
788 [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
789 [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
790 [CEC_MSG_STANDBY] = 2 | BOTH,
791 [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
792 [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
793 [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
794 [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
795 [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
796 [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
797 [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
798 [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
799 [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
800 [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
801 [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
802 [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
803 [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
804 [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
805 [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
806 [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
807 [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
808 [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
809 [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
810 [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
811 [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
812 [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
813 [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
814 [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
815 [CEC_MSG_PLAY] = 3 | DIRECTED,
816 [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
817 [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
818 [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
819 [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
820 [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
821 [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
822 [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
823 [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
824 [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
825 [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
826 [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
827 [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
828 [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
829 [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
830 [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
831 [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
832 [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
833 [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
834 [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
835 [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
836 [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
837 [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
838 [CEC_MSG_ABORT] = 2 | DIRECTED,
839 [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
840 [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
841 [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
842 [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
843 [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
844 [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
845 [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
846 [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
847 [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
848 [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
849 [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
850 [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
851 [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
852 [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
853 [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
854 [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
Hans Verkuilf3854972016-12-06 11:17:12 -0200855 [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
Hans Verkuil3074fe42016-11-01 10:48:22 -0200856 [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
857};
858
Hans Verkuil9881fe02016-06-25 09:44:16 -0300859/* Called by the CEC adapter if a message is received */
860void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
861{
862 struct cec_data *data;
863 u8 msg_init = cec_msg_initiator(msg);
864 u8 msg_dest = cec_msg_destination(msg);
Hans Verkuil3074fe42016-11-01 10:48:22 -0200865 u8 cmd = msg->msg[1];
Hans Verkuil9881fe02016-06-25 09:44:16 -0300866 bool is_reply = false;
867 bool valid_la = true;
Hans Verkuil3074fe42016-11-01 10:48:22 -0200868 u8 min_len = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300869
Hans Verkuil52d802d2016-07-12 11:10:41 -0300870 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
871 return;
872
Hans Verkuil3f98da92016-11-21 13:15:45 -0200873 /*
874 * Some CEC adapters will receive the messages that they transmitted.
875 * This test filters out those messages by checking if we are the
876 * initiator, and just returning in that case.
877 *
878 * Note that this won't work if this is an Unregistered device.
879 *
880 * It is bad practice if the hardware receives the message that it
881 * transmitted and luckily most CEC adapters behave correctly in this
882 * respect.
883 */
884 if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
885 cec_has_log_addr(adap, msg_init))
886 return;
887
Hans Verkuil980e0b362016-07-12 11:10:42 -0300888 msg->rx_ts = ktime_get_ns();
Hans Verkuil9881fe02016-06-25 09:44:16 -0300889 msg->rx_status = CEC_RX_STATUS_OK;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300890 msg->sequence = msg->reply = msg->timeout = 0;
Hans Verkuil980e0b362016-07-12 11:10:42 -0300891 msg->tx_status = 0;
892 msg->tx_ts = 0;
Hans Verkuil8991a63d2016-11-09 12:10:59 -0200893 msg->tx_arb_lost_cnt = 0;
894 msg->tx_nack_cnt = 0;
895 msg->tx_low_drive_cnt = 0;
896 msg->tx_error_cnt = 0;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300897 msg->flags = 0;
Hans Verkuil045344c2016-07-17 05:16:40 -0300898 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300899
Hans Verkuil980e0b362016-07-12 11:10:42 -0300900 mutex_lock(&adap->lock);
Hans Verkuil9881fe02016-06-25 09:44:16 -0300901 dprintk(2, "cec_received_msg: %*ph\n", msg->len, msg->msg);
902
903 /* Check if this message was for us (directed or broadcast). */
904 if (!cec_msg_is_broadcast(msg))
905 valid_la = cec_has_log_addr(adap, msg_dest);
906
Hans Verkuil3074fe42016-11-01 10:48:22 -0200907 /*
908 * Check if the length is not too short or if the message is a
909 * broadcast message where a directed message was expected or
910 * vice versa. If so, then the message has to be ignored (according
911 * to section CEC 7.3 and CEC 12.2).
912 */
913 if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
914 u8 dir_fl = cec_msg_size[cmd] & BOTH;
915
916 min_len = cec_msg_size[cmd] & 0x1f;
917 if (msg->len < min_len)
918 valid_la = false;
919 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
920 valid_la = false;
921 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
922 valid_la = false;
923 else if (cec_msg_is_broadcast(msg) &&
924 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
925 !(dir_fl & BCAST2_0))
926 valid_la = false;
927 }
928 if (valid_la && min_len) {
929 /* These messages have special length requirements */
930 switch (cmd) {
931 case CEC_MSG_TIMER_STATUS:
932 if (msg->msg[2] & 0x10) {
933 switch (msg->msg[2] & 0xf) {
934 case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
935 case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
936 if (msg->len < 5)
937 valid_la = false;
938 break;
939 }
940 } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
941 if (msg->len < 5)
942 valid_la = false;
943 }
944 break;
945 case CEC_MSG_RECORD_ON:
946 switch (msg->msg[2]) {
947 case CEC_OP_RECORD_SRC_OWN:
948 break;
949 case CEC_OP_RECORD_SRC_DIGITAL:
950 if (msg->len < 10)
951 valid_la = false;
952 break;
953 case CEC_OP_RECORD_SRC_ANALOG:
954 if (msg->len < 7)
955 valid_la = false;
956 break;
957 case CEC_OP_RECORD_SRC_EXT_PLUG:
958 if (msg->len < 4)
959 valid_la = false;
960 break;
961 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
962 if (msg->len < 5)
963 valid_la = false;
964 break;
965 }
966 break;
967 }
968 }
969
Hans Verkuil9881fe02016-06-25 09:44:16 -0300970 /* It's a valid message and not a poll or CDC message */
Hans Verkuil3074fe42016-11-01 10:48:22 -0200971 if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
Hans Verkuil9881fe02016-06-25 09:44:16 -0300972 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
973
974 /* The aborted command is in msg[2] */
975 if (abort)
976 cmd = msg->msg[2];
977
978 /*
979 * Walk over all transmitted messages that are waiting for a
980 * reply.
981 */
982 list_for_each_entry(data, &adap->wait_queue, list) {
983 struct cec_msg *dst = &data->msg;
Hans Verkuil9881fe02016-06-25 09:44:16 -0300984
Hans Verkuilf5580d82016-11-01 11:07:17 -0200985 /*
986 * The *only* CEC message that has two possible replies
987 * is CEC_MSG_INITIATE_ARC.
988 * In this case allow either of the two replies.
989 */
990 if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
991 (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
992 cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
993 (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
994 dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
995 dst->reply = cmd;
996
Hans Verkuil9881fe02016-06-25 09:44:16 -0300997 /* Does the command match? */
998 if ((abort && cmd != dst->msg[1]) ||
999 (!abort && cmd != dst->reply))
1000 continue;
1001
1002 /* Does the addressing match? */
1003 if (msg_init != cec_msg_destination(dst) &&
1004 !cec_msg_is_broadcast(dst))
1005 continue;
1006
1007 /* We got a reply */
Hans Verkuil980e0b362016-07-12 11:10:42 -03001008 memcpy(dst->msg, msg->msg, msg->len);
1009 dst->len = msg->len;
1010 dst->rx_ts = msg->rx_ts;
1011 dst->rx_status = msg->rx_status;
Hans Verkuil86e35772016-07-13 04:33:58 -03001012 if (abort)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001013 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
Hans Verkuiladc0c622016-11-01 08:55:05 -02001014 msg->flags = dst->flags;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001015 /* Remove it from the wait_queue */
1016 list_del_init(&data->list);
1017
1018 /* Cancel the pending timeout work */
1019 if (!cancel_delayed_work(&data->work)) {
1020 mutex_unlock(&adap->lock);
1021 flush_scheduled_work();
1022 mutex_lock(&adap->lock);
1023 }
1024 /*
1025 * Mark this as a reply, provided someone is still
1026 * waiting for the answer.
1027 */
1028 if (data->fh)
1029 is_reply = true;
1030 cec_data_completed(data);
1031 break;
1032 }
1033 }
1034 mutex_unlock(&adap->lock);
1035
1036 /* Pass the message on to any monitoring filehandles */
1037 cec_queue_msg_monitor(adap, msg, valid_la);
1038
1039 /* We're done if it is not for us or a poll message */
1040 if (!valid_la || msg->len <= 1)
1041 return;
1042
Hans Verkuil3e92d8b2016-08-12 13:32:07 -03001043 if (adap->log_addrs.log_addr_mask == 0)
1044 return;
1045
Hans Verkuil9881fe02016-06-25 09:44:16 -03001046 /*
1047 * Process the message on the protocol level. If is_reply is true,
1048 * then cec_receive_notify() won't pass on the reply to the listener(s)
1049 * since that was already done by cec_data_completed() above.
1050 */
1051 cec_receive_notify(adap, msg, is_reply);
1052}
1053EXPORT_SYMBOL_GPL(cec_received_msg);
1054
1055/* Logical Address Handling */
1056
1057/*
1058 * Attempt to claim a specific logical address.
1059 *
1060 * This function is called with adap->lock held.
1061 */
1062static int cec_config_log_addr(struct cec_adapter *adap,
1063 unsigned int idx,
1064 unsigned int log_addr)
1065{
1066 struct cec_log_addrs *las = &adap->log_addrs;
1067 struct cec_msg msg = { };
1068 int err;
1069
1070 if (cec_has_log_addr(adap, log_addr))
1071 return 0;
1072
1073 /* Send poll message */
1074 msg.len = 1;
1075 msg.msg[0] = 0xf0 | log_addr;
1076 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1077
1078 /*
1079 * While trying to poll the physical address was reset
1080 * and the adapter was unconfigured, so bail out.
1081 */
1082 if (!adap->is_configuring)
1083 return -EINTR;
1084
1085 if (err)
1086 return err;
1087
1088 if (msg.tx_status & CEC_TX_STATUS_OK)
1089 return 0;
1090
1091 /*
1092 * Message not acknowledged, so this logical
1093 * address is free to use.
1094 */
1095 err = adap->ops->adap_log_addr(adap, log_addr);
1096 if (err)
1097 return err;
1098
1099 las->log_addr[idx] = log_addr;
1100 las->log_addr_mask |= 1 << log_addr;
1101 adap->phys_addrs[log_addr] = adap->phys_addr;
1102
1103 dprintk(2, "claimed addr %d (%d)\n", log_addr,
1104 las->primary_device_type[idx]);
1105 return 1;
1106}
1107
1108/*
1109 * Unconfigure the adapter: clear all logical addresses and send
1110 * the state changed event.
1111 *
1112 * This function is called with adap->lock held.
1113 */
1114static void cec_adap_unconfigure(struct cec_adapter *adap)
1115{
1116 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
1117 adap->log_addrs.log_addr_mask = 0;
1118 adap->is_configuring = false;
1119 adap->is_configured = false;
1120 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
1121 wake_up_interruptible(&adap->kthread_waitq);
1122 cec_post_state_event(adap);
1123}
1124
1125/*
1126 * Attempt to claim the required logical addresses.
1127 */
1128static int cec_config_thread_func(void *arg)
1129{
1130 /* The various LAs for each type of device */
1131 static const u8 tv_log_addrs[] = {
1132 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1133 CEC_LOG_ADDR_INVALID
1134 };
1135 static const u8 record_log_addrs[] = {
1136 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1137 CEC_LOG_ADDR_RECORD_3,
1138 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1139 CEC_LOG_ADDR_INVALID
1140 };
1141 static const u8 tuner_log_addrs[] = {
1142 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1143 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1144 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1145 CEC_LOG_ADDR_INVALID
1146 };
1147 static const u8 playback_log_addrs[] = {
1148 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1149 CEC_LOG_ADDR_PLAYBACK_3,
1150 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1151 CEC_LOG_ADDR_INVALID
1152 };
1153 static const u8 audiosystem_log_addrs[] = {
1154 CEC_LOG_ADDR_AUDIOSYSTEM,
1155 CEC_LOG_ADDR_INVALID
1156 };
1157 static const u8 specific_use_log_addrs[] = {
1158 CEC_LOG_ADDR_SPECIFIC,
1159 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1160 CEC_LOG_ADDR_INVALID
1161 };
1162 static const u8 *type2addrs[6] = {
1163 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1164 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1165 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1166 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1167 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1168 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1169 };
1170 static const u16 type2mask[] = {
1171 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1172 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1173 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1174 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1175 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1176 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1177 };
1178 struct cec_adapter *adap = arg;
1179 struct cec_log_addrs *las = &adap->log_addrs;
1180 int err;
1181 int i, j;
1182
1183 mutex_lock(&adap->lock);
1184 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1185 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1186 las->log_addr_mask = 0;
1187
1188 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1189 goto configured;
1190
1191 for (i = 0; i < las->num_log_addrs; i++) {
1192 unsigned int type = las->log_addr_type[i];
1193 const u8 *la_list;
1194 u8 last_la;
1195
1196 /*
1197 * The TV functionality can only map to physical address 0.
1198 * For any other address, try the Specific functionality
1199 * instead as per the spec.
1200 */
1201 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1202 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1203
1204 la_list = type2addrs[type];
1205 last_la = las->log_addr[i];
1206 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1207 if (last_la == CEC_LOG_ADDR_INVALID ||
1208 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1209 !(last_la & type2mask[type]))
1210 last_la = la_list[0];
1211
1212 err = cec_config_log_addr(adap, i, last_la);
1213 if (err > 0) /* Reused last LA */
1214 continue;
1215
1216 if (err < 0)
1217 goto unconfigure;
1218
1219 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1220 /* Tried this one already, skip it */
1221 if (la_list[j] == last_la)
1222 continue;
1223 /* The backup addresses are CEC 2.0 specific */
1224 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1225 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1226 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1227 continue;
1228
1229 err = cec_config_log_addr(adap, i, la_list[j]);
1230 if (err == 0) /* LA is in use */
1231 continue;
1232 if (err < 0)
1233 goto unconfigure;
1234 /* Done, claimed an LA */
1235 break;
1236 }
1237
1238 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1239 dprintk(1, "could not claim LA %d\n", i);
1240 }
1241
Hans Verkuildcceb1e2016-08-10 09:24:45 -03001242 if (adap->log_addrs.log_addr_mask == 0 &&
1243 !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1244 goto unconfigure;
1245
Hans Verkuil9881fe02016-06-25 09:44:16 -03001246configured:
1247 if (adap->log_addrs.log_addr_mask == 0) {
1248 /* Fall back to unregistered */
1249 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1250 las->log_addr_mask = 1 << las->log_addr[0];
Hans Verkuil0c1d61b2016-08-14 08:27:09 -03001251 for (i = 1; i < las->num_log_addrs; i++)
1252 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001253 }
Hans Verkuil7af26f82016-12-09 11:54:06 -02001254 for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1255 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001256 adap->is_configured = true;
1257 adap->is_configuring = false;
1258 cec_post_state_event(adap);
1259 mutex_unlock(&adap->lock);
1260
1261 for (i = 0; i < las->num_log_addrs; i++) {
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001262 struct cec_msg msg = {};
1263
Hans Verkuila69a1682016-11-02 07:41:41 -02001264 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1265 (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001266 continue;
1267
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001268 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1269
1270 /* Report Features must come first according to CEC 2.0 */
1271 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1272 adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1273 cec_fill_msg_report_features(adap, &msg, i);
1274 cec_transmit_msg(adap, &msg, false);
1275 }
1276
Hans Verkuild3d64bc2016-12-09 11:48:34 -02001277 /* Report Physical Address */
1278 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1279 las->primary_device_type[i]);
1280 dprintk(2, "config: la %d pa %x.%x.%x.%x\n",
1281 las->log_addr[i],
1282 cec_phys_addr_exp(adap->phys_addr));
1283 cec_transmit_msg(adap, &msg, false);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001284 }
1285 mutex_lock(&adap->lock);
1286 adap->kthread_config = NULL;
1287 mutex_unlock(&adap->lock);
1288 complete(&adap->config_completion);
1289 return 0;
1290
1291unconfigure:
1292 for (i = 0; i < las->num_log_addrs; i++)
1293 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1294 cec_adap_unconfigure(adap);
1295 adap->kthread_config = NULL;
1296 mutex_unlock(&adap->lock);
1297 complete(&adap->config_completion);
1298 return 0;
1299}
1300
1301/*
1302 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1303 * logical addresses.
1304 *
1305 * This function is called with adap->lock held.
1306 */
1307static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1308{
1309 if (WARN_ON(adap->is_configuring || adap->is_configured))
1310 return;
1311
1312 init_completion(&adap->config_completion);
1313
1314 /* Ready to kick off the thread */
1315 adap->is_configuring = true;
1316 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1317 "ceccfg-%s", adap->name);
1318 if (IS_ERR(adap->kthread_config)) {
1319 adap->kthread_config = NULL;
1320 } else if (block) {
1321 mutex_unlock(&adap->lock);
1322 wait_for_completion(&adap->config_completion);
1323 mutex_lock(&adap->lock);
1324 }
1325}
1326
1327/* Set a new physical address and send an event notifying userspace of this.
1328 *
1329 * This function is called with adap->lock held.
1330 */
1331void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1332{
Hans Verkuilc000e5d2016-07-10 10:11:17 -03001333 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001334 return;
1335
1336 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1337 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1338 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1339 cec_post_state_event(adap);
1340 cec_adap_unconfigure(adap);
1341 /* Disabling monitor all mode should always succeed */
1342 if (adap->monitor_all_cnt)
1343 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1344 WARN_ON(adap->ops->adap_enable(adap, false));
1345 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1346 return;
1347 }
1348
1349 if (adap->ops->adap_enable(adap, true))
1350 return;
1351
1352 if (adap->monitor_all_cnt &&
1353 call_op(adap, adap_monitor_all_enable, true)) {
1354 WARN_ON(adap->ops->adap_enable(adap, false));
1355 return;
1356 }
1357 adap->phys_addr = phys_addr;
1358 cec_post_state_event(adap);
1359 if (adap->log_addrs.num_log_addrs)
1360 cec_claim_log_addrs(adap, block);
1361}
1362
1363void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1364{
1365 if (IS_ERR_OR_NULL(adap))
1366 return;
1367
Hans Verkuil9881fe02016-06-25 09:44:16 -03001368 mutex_lock(&adap->lock);
1369 __cec_s_phys_addr(adap, phys_addr, block);
1370 mutex_unlock(&adap->lock);
1371}
1372EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1373
1374/*
1375 * Called from either the ioctl or a driver to set the logical addresses.
1376 *
1377 * This function is called with adap->lock held.
1378 */
1379int __cec_s_log_addrs(struct cec_adapter *adap,
1380 struct cec_log_addrs *log_addrs, bool block)
1381{
1382 u16 type_mask = 0;
1383 int i;
1384
Hans Verkuilc000e5d2016-07-10 10:11:17 -03001385 if (adap->devnode.unregistered)
1386 return -ENODEV;
1387
Hans Verkuil9881fe02016-06-25 09:44:16 -03001388 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1389 adap->log_addrs.num_log_addrs = 0;
1390 cec_adap_unconfigure(adap);
1391 return 0;
1392 }
1393
Hans Verkuila69a1682016-11-02 07:41:41 -02001394 if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1395 /*
1396 * Sanitize log_addrs fields if a CDC-Only device is
1397 * requested.
1398 */
1399 log_addrs->num_log_addrs = 1;
1400 log_addrs->osd_name[0] = '\0';
1401 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1402 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1403 /*
1404 * This is just an internal convention since a CDC-Only device
1405 * doesn't have to be a switch. But switches already use
1406 * unregistered, so it makes some kind of sense to pick this
1407 * as the primary device. Since a CDC-Only device never sends
1408 * any 'normal' CEC messages this primary device type is never
1409 * sent over the CEC bus.
1410 */
1411 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1412 log_addrs->all_device_types[0] = 0;
1413 log_addrs->features[0][0] = 0;
1414 log_addrs->features[0][1] = 0;
1415 }
1416
Hans Verkuil9881fe02016-06-25 09:44:16 -03001417 /* Ensure the osd name is 0-terminated */
1418 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1419
1420 /* Sanity checks */
1421 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1422 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1423 return -EINVAL;
1424 }
1425
1426 /*
1427 * Vendor ID is a 24 bit number, so check if the value is
1428 * within the correct range.
1429 */
1430 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1431 (log_addrs->vendor_id & 0xff000000) != 0)
1432 return -EINVAL;
1433
1434 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1435 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0)
1436 return -EINVAL;
1437
1438 if (log_addrs->num_log_addrs > 1)
1439 for (i = 0; i < log_addrs->num_log_addrs; i++)
1440 if (log_addrs->log_addr_type[i] ==
1441 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1442 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1443 return -EINVAL;
1444 }
1445
Hans Verkuil9881fe02016-06-25 09:44:16 -03001446 for (i = 0; i < log_addrs->num_log_addrs; i++) {
Hans Verkuil009a6202016-07-18 03:44:10 -03001447 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001448 u8 *features = log_addrs->features[i];
1449 bool op_is_dev_features = false;
Hans Verkuila161bef2016-11-04 10:52:10 -02001450 unsigned j;
Hans Verkuil9881fe02016-06-25 09:44:16 -03001451
1452 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1453 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1454 dprintk(1, "duplicate logical address type\n");
1455 return -EINVAL;
1456 }
1457 type_mask |= 1 << log_addrs->log_addr_type[i];
1458 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1459 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1460 /* Record already contains the playback functionality */
1461 dprintk(1, "invalid record + playback combination\n");
1462 return -EINVAL;
1463 }
1464 if (log_addrs->primary_device_type[i] >
1465 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1466 dprintk(1, "unknown primary device type\n");
1467 return -EINVAL;
1468 }
1469 if (log_addrs->primary_device_type[i] == 2) {
1470 dprintk(1, "invalid primary device type\n");
1471 return -EINVAL;
1472 }
1473 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1474 dprintk(1, "unknown logical address type\n");
1475 return -EINVAL;
1476 }
Hans Verkuila161bef2016-11-04 10:52:10 -02001477 for (j = 0; j < feature_sz; j++) {
1478 if ((features[j] & 0x80) == 0) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001479 if (op_is_dev_features)
1480 break;
1481 op_is_dev_features = true;
1482 }
1483 }
Hans Verkuila161bef2016-11-04 10:52:10 -02001484 if (!op_is_dev_features || j == feature_sz) {
Hans Verkuil9881fe02016-06-25 09:44:16 -03001485 dprintk(1, "malformed features\n");
1486 return -EINVAL;
1487 }
Hans Verkuil009a6202016-07-18 03:44:10 -03001488 /* Zero unused part of the feature array */
Hans Verkuila161bef2016-11-04 10:52:10 -02001489 memset(features + j + 1, 0, feature_sz - j - 1);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001490 }
1491
1492 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1493 if (log_addrs->num_log_addrs > 2) {
1494 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1495 return -EINVAL;
1496 }
1497 if (log_addrs->num_log_addrs == 2) {
1498 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1499 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1500 dprintk(1, "Two LAs is only allowed for audiosystem and TV\n");
1501 return -EINVAL;
1502 }
1503 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1504 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1505 dprintk(1, "An audiosystem/TV can only be combined with record or playback\n");
1506 return -EINVAL;
1507 }
1508 }
1509 }
1510
Hans Verkuil009a6202016-07-18 03:44:10 -03001511 /* Zero unused LAs */
1512 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1513 log_addrs->primary_device_type[i] = 0;
1514 log_addrs->log_addr_type[i] = 0;
1515 log_addrs->all_device_types[i] = 0;
1516 memset(log_addrs->features[i], 0,
1517 sizeof(log_addrs->features[i]));
1518 }
1519
Hans Verkuil9881fe02016-06-25 09:44:16 -03001520 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1521 adap->log_addrs = *log_addrs;
1522 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1523 cec_claim_log_addrs(adap, block);
1524 return 0;
1525}
1526
1527int cec_s_log_addrs(struct cec_adapter *adap,
1528 struct cec_log_addrs *log_addrs, bool block)
1529{
1530 int err;
1531
Hans Verkuil9881fe02016-06-25 09:44:16 -03001532 mutex_lock(&adap->lock);
1533 err = __cec_s_log_addrs(adap, log_addrs, block);
1534 mutex_unlock(&adap->lock);
1535 return err;
1536}
1537EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1538
1539/* High-level core CEC message handling */
1540
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001541/* Fill in the Report Features message */
1542static void cec_fill_msg_report_features(struct cec_adapter *adap,
1543 struct cec_msg *msg,
1544 unsigned int la_idx)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001545{
Hans Verkuil9881fe02016-06-25 09:44:16 -03001546 const struct cec_log_addrs *las = &adap->log_addrs;
1547 const u8 *features = las->features[la_idx];
1548 bool op_is_dev_features = false;
1549 unsigned int idx;
1550
Hans Verkuil9881fe02016-06-25 09:44:16 -03001551 /* Report Features */
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001552 msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1553 msg->len = 4;
1554 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1555 msg->msg[2] = adap->log_addrs.cec_version;
1556 msg->msg[3] = las->all_device_types[la_idx];
Hans Verkuil9881fe02016-06-25 09:44:16 -03001557
1558 /* Write RC Profiles first, then Device Features */
1559 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001560 msg->msg[msg->len++] = features[idx];
Hans Verkuil9881fe02016-06-25 09:44:16 -03001561 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1562 if (op_is_dev_features)
1563 break;
1564 op_is_dev_features = true;
1565 }
1566 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001567}
1568
Hans Verkuil9881fe02016-06-25 09:44:16 -03001569/* Transmit the Feature Abort message */
1570static int cec_feature_abort_reason(struct cec_adapter *adap,
1571 struct cec_msg *msg, u8 reason)
1572{
1573 struct cec_msg tx_msg = { };
1574
1575 /*
1576 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1577 * message!
1578 */
1579 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1580 return 0;
1581 cec_msg_set_reply_to(&tx_msg, msg);
1582 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1583 return cec_transmit_msg(adap, &tx_msg, false);
1584}
1585
1586static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1587{
1588 return cec_feature_abort_reason(adap, msg,
1589 CEC_OP_ABORT_UNRECOGNIZED_OP);
1590}
1591
1592static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1593{
1594 return cec_feature_abort_reason(adap, msg,
1595 CEC_OP_ABORT_REFUSED);
1596}
1597
1598/*
1599 * Called when a CEC message is received. This function will do any
1600 * necessary core processing. The is_reply bool is true if this message
1601 * is a reply to an earlier transmit.
1602 *
1603 * The message is either a broadcast message or a valid directed message.
1604 */
1605static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1606 bool is_reply)
1607{
1608 bool is_broadcast = cec_msg_is_broadcast(msg);
1609 u8 dest_laddr = cec_msg_destination(msg);
1610 u8 init_laddr = cec_msg_initiator(msg);
1611 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1612 int la_idx = cec_log_addr2idx(adap, dest_laddr);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001613 bool from_unregistered = init_laddr == 0xf;
1614 struct cec_msg tx_cec_msg = { };
1615
1616 dprintk(1, "cec_receive_notify: %*ph\n", msg->len, msg->msg);
1617
Hans Verkuila69a1682016-11-02 07:41:41 -02001618 /* If this is a CDC-Only device, then ignore any non-CDC messages */
1619 if (cec_is_cdc_only(&adap->log_addrs) &&
1620 msg->msg[1] != CEC_MSG_CDC_MESSAGE)
1621 return 0;
1622
Hans Verkuil9881fe02016-06-25 09:44:16 -03001623 if (adap->ops->received) {
1624 /* Allow drivers to process the message first */
1625 if (adap->ops->received(adap, msg) != -ENOMSG)
1626 return 0;
1627 }
1628
1629 /*
1630 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1631 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1632 * handled by the CEC core, even if the passthrough mode is on.
1633 * The others are just ignored if passthrough mode is on.
1634 */
1635 switch (msg->msg[1]) {
1636 case CEC_MSG_GET_CEC_VERSION:
1637 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1638 case CEC_MSG_ABORT:
1639 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1640 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1641 case CEC_MSG_GIVE_OSD_NAME:
1642 case CEC_MSG_GIVE_FEATURES:
1643 /*
1644 * Skip processing these messages if the passthrough mode
1645 * is on.
1646 */
1647 if (adap->passthrough)
1648 goto skip_processing;
1649 /* Ignore if addressing is wrong */
1650 if (is_broadcast || from_unregistered)
1651 return 0;
1652 break;
1653
1654 case CEC_MSG_USER_CONTROL_PRESSED:
1655 case CEC_MSG_USER_CONTROL_RELEASED:
1656 /* Wrong addressing mode: don't process */
1657 if (is_broadcast || from_unregistered)
1658 goto skip_processing;
1659 break;
1660
1661 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1662 /*
1663 * This message is always processed, regardless of the
1664 * passthrough setting.
1665 *
1666 * Exception: don't process if wrong addressing mode.
1667 */
1668 if (!is_broadcast)
1669 goto skip_processing;
1670 break;
1671
1672 default:
1673 break;
1674 }
1675
1676 cec_msg_set_reply_to(&tx_cec_msg, msg);
1677
1678 switch (msg->msg[1]) {
1679 /* The following messages are processed but still passed through */
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001680 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1681 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1682
1683 if (!from_unregistered)
1684 adap->phys_addrs[init_laddr] = pa;
1685 dprintk(1, "Reported physical address %x.%x.%x.%x for logical address %d\n",
1686 cec_phys_addr_exp(pa), init_laddr);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001687 break;
Hans Verkuilf8db65f2016-06-30 07:08:53 -03001688 }
Hans Verkuil9881fe02016-06-25 09:44:16 -03001689
1690 case CEC_MSG_USER_CONTROL_PRESSED:
Hans Verkuilf4062622016-11-01 07:59:34 -02001691 if (!(adap->capabilities & CEC_CAP_RC) ||
1692 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001693 break;
1694
Hans Verkuil5bb23992016-07-01 07:33:10 -03001695#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001696 switch (msg->msg[2]) {
1697 /*
1698 * Play function, this message can have variable length
1699 * depending on the specific play function that is used.
1700 */
1701 case 0x60:
1702 if (msg->len == 2)
1703 rc_keydown(adap->rc, RC_TYPE_CEC,
1704 msg->msg[2], 0);
1705 else
1706 rc_keydown(adap->rc, RC_TYPE_CEC,
1707 msg->msg[2] << 8 | msg->msg[3], 0);
1708 break;
1709 /*
1710 * Other function messages that are not handled.
1711 * Currently the RC framework does not allow to supply an
1712 * additional parameter to a keypress. These "keys" contain
1713 * other information such as channel number, an input number
1714 * etc.
1715 * For the time being these messages are not processed by the
1716 * framework and are simply forwarded to the user space.
1717 */
1718 case 0x56: case 0x57:
1719 case 0x67: case 0x68: case 0x69: case 0x6a:
1720 break;
1721 default:
1722 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1723 break;
1724 }
1725#endif
1726 break;
1727
1728 case CEC_MSG_USER_CONTROL_RELEASED:
Hans Verkuilf4062622016-11-01 07:59:34 -02001729 if (!(adap->capabilities & CEC_CAP_RC) ||
1730 !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001731 break;
Hans Verkuil5bb23992016-07-01 07:33:10 -03001732#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuil9881fe02016-06-25 09:44:16 -03001733 rc_keyup(adap->rc);
1734#endif
1735 break;
1736
1737 /*
1738 * The remaining messages are only processed if the passthrough mode
1739 * is off.
1740 */
1741 case CEC_MSG_GET_CEC_VERSION:
1742 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1743 return cec_transmit_msg(adap, &tx_cec_msg, false);
1744
1745 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1746 /* Do nothing for CEC switches using addr 15 */
1747 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1748 return 0;
1749 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1750 return cec_transmit_msg(adap, &tx_cec_msg, false);
1751
1752 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1753 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1754 return cec_feature_abort(adap, msg);
1755 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1756 return cec_transmit_msg(adap, &tx_cec_msg, false);
1757
1758 case CEC_MSG_ABORT:
1759 /* Do nothing for CEC switches */
1760 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1761 return 0;
1762 return cec_feature_refused(adap, msg);
1763
1764 case CEC_MSG_GIVE_OSD_NAME: {
1765 if (adap->log_addrs.osd_name[0] == 0)
1766 return cec_feature_abort(adap, msg);
1767 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1768 return cec_transmit_msg(adap, &tx_cec_msg, false);
1769 }
1770
1771 case CEC_MSG_GIVE_FEATURES:
Hans Verkuila24f56d2016-12-09 11:28:19 -02001772 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1773 return cec_feature_abort(adap, msg);
Hans Verkuil52bc30f2016-12-09 11:36:03 -02001774 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
1775 return cec_transmit_msg(adap, &tx_cec_msg, false);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001776
1777 default:
1778 /*
1779 * Unprocessed messages are aborted if userspace isn't doing
1780 * any processing either.
1781 */
Hans Verkuila179b692016-08-24 05:36:53 -03001782 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
Hans Verkuil9881fe02016-06-25 09:44:16 -03001783 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1784 return cec_feature_abort(adap, msg);
1785 break;
1786 }
1787
1788skip_processing:
Hans Verkuiladc0c622016-11-01 08:55:05 -02001789 /* If this was a reply, then we're done, unless otherwise specified */
1790 if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
Hans Verkuil9881fe02016-06-25 09:44:16 -03001791 return 0;
1792
1793 /*
1794 * Send to the exclusive follower if there is one, otherwise send
1795 * to all followers.
1796 */
1797 if (adap->cec_follower)
1798 cec_queue_msg_fh(adap->cec_follower, msg);
1799 else
1800 cec_queue_msg_followers(adap, msg);
1801 return 0;
1802}
1803
1804/*
1805 * Helper functions to keep track of the 'monitor all' use count.
1806 *
1807 * These functions are called with adap->lock held.
1808 */
1809int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1810{
1811 int ret = 0;
1812
1813 if (adap->monitor_all_cnt == 0)
1814 ret = call_op(adap, adap_monitor_all_enable, 1);
1815 if (ret == 0)
1816 adap->monitor_all_cnt++;
1817 return ret;
1818}
1819
1820void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1821{
1822 adap->monitor_all_cnt--;
1823 if (adap->monitor_all_cnt == 0)
1824 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1825}
1826
1827#ifdef CONFIG_MEDIA_CEC_DEBUG
1828/*
1829 * Log the current state of the CEC adapter.
1830 * Very useful for debugging.
1831 */
1832int cec_adap_status(struct seq_file *file, void *priv)
1833{
1834 struct cec_adapter *adap = dev_get_drvdata(file->private);
1835 struct cec_data *data;
1836
1837 mutex_lock(&adap->lock);
1838 seq_printf(file, "configured: %d\n", adap->is_configured);
1839 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1840 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1841 cec_phys_addr_exp(adap->phys_addr));
1842 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1843 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1844 if (adap->cec_follower)
1845 seq_printf(file, "has CEC follower%s\n",
1846 adap->passthrough ? " (in passthrough mode)" : "");
1847 if (adap->cec_initiator)
1848 seq_puts(file, "has CEC initiator\n");
1849 if (adap->monitor_all_cnt)
1850 seq_printf(file, "file handles in Monitor All mode: %u\n",
1851 adap->monitor_all_cnt);
1852 data = adap->transmitting;
1853 if (data)
Hans Verkuil11065f82016-07-17 11:40:05 -03001854 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1855 data->msg.len, data->msg.msg, data->msg.reply,
1856 data->msg.timeout);
1857 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001858 list_for_each_entry(data, &adap->transmit_queue, list) {
Hans Verkuil11065f82016-07-17 11:40:05 -03001859 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1860 data->msg.len, data->msg.msg, data->msg.reply,
1861 data->msg.timeout);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001862 }
1863 list_for_each_entry(data, &adap->wait_queue, list) {
Hans Verkuil11065f82016-07-17 11:40:05 -03001864 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1865 data->msg.len, data->msg.msg, data->msg.reply,
1866 data->msg.timeout);
Hans Verkuil9881fe02016-06-25 09:44:16 -03001867 }
1868
1869 call_void_op(adap, adap_status, file);
1870 mutex_unlock(&adap->lock);
1871 return 0;
1872}
1873#endif