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