]> Git Repo - J-linux.git/blob - drivers/media/cec/core/cec-adap.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / drivers / media / cec / core / cec-adap.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
4  *
5  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/kmod.h>
13 #include <linux/ktime.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18
19 #include <drm/drm_connector.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_edid.h>
22 #include <drm/drm_file.h>
23
24 #include "cec-priv.h"
25
26 static void cec_fill_msg_report_features(struct cec_adapter *adap,
27                                          struct cec_msg *msg,
28                                          unsigned int la_idx);
29
30 static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
31 {
32         int i;
33
34         for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
35                 if (adap->log_addrs.log_addr[i] == log_addr)
36                         return i;
37         return -1;
38 }
39
40 static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
41 {
42         int i = cec_log_addr2idx(adap, log_addr);
43
44         return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
45 }
46
47 u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
48                            unsigned int *offset)
49 {
50         unsigned int loc = cec_get_edid_spa_location(edid, size);
51
52         if (offset)
53                 *offset = loc;
54         if (loc == 0)
55                 return CEC_PHYS_ADDR_INVALID;
56         return (edid[loc] << 8) | edid[loc + 1];
57 }
58 EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
59
60 void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
61                                  const struct drm_connector *connector)
62 {
63         memset(conn_info, 0, sizeof(*conn_info));
64         conn_info->type = CEC_CONNECTOR_TYPE_DRM;
65         conn_info->drm.card_no = connector->dev->primary->index;
66         conn_info->drm.connector_id = connector->base.id;
67 }
68 EXPORT_SYMBOL_GPL(cec_fill_conn_info_from_drm);
69
70 /*
71  * Queue a new event for this filehandle. If ts == 0, then set it
72  * to the current time.
73  *
74  * We keep a queue of at most max_event events where max_event differs
75  * per event. If the queue becomes full, then drop the oldest event and
76  * keep track of how many events we've dropped.
77  */
78 void cec_queue_event_fh(struct cec_fh *fh,
79                         const struct cec_event *new_ev, u64 ts)
80 {
81         static const u16 max_events[CEC_NUM_EVENTS] = {
82                 1, 1, 800, 800, 8, 8, 8, 8
83         };
84         struct cec_event_entry *entry;
85         unsigned int ev_idx = new_ev->event - 1;
86
87         if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
88                 return;
89
90         if (ts == 0)
91                 ts = ktime_get_ns();
92
93         mutex_lock(&fh->lock);
94         if (ev_idx < CEC_NUM_CORE_EVENTS)
95                 entry = &fh->core_events[ev_idx];
96         else
97                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
98         if (entry) {
99                 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
100                     fh->queued_events[ev_idx]) {
101                         entry->ev.lost_msgs.lost_msgs +=
102                                 new_ev->lost_msgs.lost_msgs;
103                         goto unlock;
104                 }
105                 entry->ev = *new_ev;
106                 entry->ev.ts = ts;
107
108                 if (fh->queued_events[ev_idx] < max_events[ev_idx]) {
109                         /* Add new msg at the end of the queue */
110                         list_add_tail(&entry->list, &fh->events[ev_idx]);
111                         fh->queued_events[ev_idx]++;
112                         fh->total_queued_events++;
113                         goto unlock;
114                 }
115
116                 if (ev_idx >= CEC_NUM_CORE_EVENTS) {
117                         list_add_tail(&entry->list, &fh->events[ev_idx]);
118                         /* drop the oldest event */
119                         entry = list_first_entry(&fh->events[ev_idx],
120                                                  struct cec_event_entry, list);
121                         list_del(&entry->list);
122                         kfree(entry);
123                 }
124         }
125         /* Mark that events were lost */
126         entry = list_first_entry_or_null(&fh->events[ev_idx],
127                                          struct cec_event_entry, list);
128         if (entry)
129                 entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
130
131 unlock:
132         mutex_unlock(&fh->lock);
133         wake_up_interruptible(&fh->wait);
134 }
135
136 /* Queue a new event for all open filehandles. */
137 static void cec_queue_event(struct cec_adapter *adap,
138                             const struct cec_event *ev)
139 {
140         u64 ts = ktime_get_ns();
141         struct cec_fh *fh;
142
143         mutex_lock(&adap->devnode.lock_fhs);
144         list_for_each_entry(fh, &adap->devnode.fhs, list)
145                 cec_queue_event_fh(fh, ev, ts);
146         mutex_unlock(&adap->devnode.lock_fhs);
147 }
148
149 /* Notify userspace that the CEC pin changed state at the given time. */
150 void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
151                              bool dropped_events, ktime_t ts)
152 {
153         struct cec_event ev = {
154                 .event = is_high ? CEC_EVENT_PIN_CEC_HIGH :
155                                    CEC_EVENT_PIN_CEC_LOW,
156                 .flags = dropped_events ? CEC_EVENT_FL_DROPPED_EVENTS : 0,
157         };
158         struct cec_fh *fh;
159
160         mutex_lock(&adap->devnode.lock_fhs);
161         list_for_each_entry(fh, &adap->devnode.fhs, list) {
162                 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
163                         cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
164         }
165         mutex_unlock(&adap->devnode.lock_fhs);
166 }
167 EXPORT_SYMBOL_GPL(cec_queue_pin_cec_event);
168
169 /* Notify userspace that the HPD pin changed state at the given time. */
170 void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
171 {
172         struct cec_event ev = {
173                 .event = is_high ? CEC_EVENT_PIN_HPD_HIGH :
174                                    CEC_EVENT_PIN_HPD_LOW,
175         };
176         struct cec_fh *fh;
177
178         mutex_lock(&adap->devnode.lock_fhs);
179         list_for_each_entry(fh, &adap->devnode.fhs, list)
180                 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
181         mutex_unlock(&adap->devnode.lock_fhs);
182 }
183 EXPORT_SYMBOL_GPL(cec_queue_pin_hpd_event);
184
185 /* Notify userspace that the 5V pin changed state at the given time. */
186 void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts)
187 {
188         struct cec_event ev = {
189                 .event = is_high ? CEC_EVENT_PIN_5V_HIGH :
190                                    CEC_EVENT_PIN_5V_LOW,
191         };
192         struct cec_fh *fh;
193
194         mutex_lock(&adap->devnode.lock_fhs);
195         list_for_each_entry(fh, &adap->devnode.fhs, list)
196                 cec_queue_event_fh(fh, &ev, ktime_to_ns(ts));
197         mutex_unlock(&adap->devnode.lock_fhs);
198 }
199 EXPORT_SYMBOL_GPL(cec_queue_pin_5v_event);
200
201 /*
202  * Queue a new message for this filehandle.
203  *
204  * We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the
205  * queue becomes full, then drop the oldest message and keep track
206  * of how many messages we've dropped.
207  */
208 static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
209 {
210         static const struct cec_event ev_lost_msgs = {
211                 .event = CEC_EVENT_LOST_MSGS,
212                 .flags = 0,
213                 {
214                         .lost_msgs = { 1 },
215                 },
216         };
217         struct cec_msg_entry *entry;
218
219         mutex_lock(&fh->lock);
220         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
221         if (entry) {
222                 entry->msg = *msg;
223                 /* Add new msg at the end of the queue */
224                 list_add_tail(&entry->list, &fh->msgs);
225
226                 if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
227                         /* All is fine if there is enough room */
228                         fh->queued_msgs++;
229                         mutex_unlock(&fh->lock);
230                         wake_up_interruptible(&fh->wait);
231                         return;
232                 }
233
234                 /*
235                  * if the message queue is full, then drop the oldest one and
236                  * send a lost message event.
237                  */
238                 entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
239                 list_del(&entry->list);
240                 kfree(entry);
241         }
242         mutex_unlock(&fh->lock);
243
244         /*
245          * We lost a message, either because kmalloc failed or the queue
246          * was full.
247          */
248         cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
249 }
250
251 /*
252  * Queue the message for those filehandles that are in monitor mode.
253  * If valid_la is true (this message is for us or was sent by us),
254  * then pass it on to any monitoring filehandle. If this message
255  * isn't for us or from us, then only give it to filehandles that
256  * are in MONITOR_ALL mode.
257  *
258  * This can only happen if the CEC_CAP_MONITOR_ALL capability is
259  * set and the CEC adapter was placed in 'monitor all' mode.
260  */
261 static void cec_queue_msg_monitor(struct cec_adapter *adap,
262                                   const struct cec_msg *msg,
263                                   bool valid_la)
264 {
265         struct cec_fh *fh;
266         u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
267                                       CEC_MODE_MONITOR_ALL;
268
269         mutex_lock(&adap->devnode.lock_fhs);
270         list_for_each_entry(fh, &adap->devnode.fhs, list) {
271                 if (fh->mode_follower >= monitor_mode)
272                         cec_queue_msg_fh(fh, msg);
273         }
274         mutex_unlock(&adap->devnode.lock_fhs);
275 }
276
277 /*
278  * Queue the message for follower filehandles.
279  */
280 static void cec_queue_msg_followers(struct cec_adapter *adap,
281                                     const struct cec_msg *msg)
282 {
283         struct cec_fh *fh;
284
285         mutex_lock(&adap->devnode.lock_fhs);
286         list_for_each_entry(fh, &adap->devnode.fhs, list) {
287                 if (fh->mode_follower == CEC_MODE_FOLLOWER)
288                         cec_queue_msg_fh(fh, msg);
289         }
290         mutex_unlock(&adap->devnode.lock_fhs);
291 }
292
293 /* Notify userspace of an adapter state change. */
294 static void cec_post_state_event(struct cec_adapter *adap)
295 {
296         struct cec_event ev = {
297                 .event = CEC_EVENT_STATE_CHANGE,
298         };
299
300         ev.state_change.phys_addr = adap->phys_addr;
301         ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
302         ev.state_change.have_conn_info =
303                 adap->conn_info.type != CEC_CONNECTOR_TYPE_NO_CONNECTOR;
304         cec_queue_event(adap, &ev);
305 }
306
307 /*
308  * A CEC transmit (and a possible wait for reply) completed.
309  * If this was in blocking mode, then complete it, otherwise
310  * queue the message for userspace to dequeue later.
311  *
312  * This function is called with adap->lock held.
313  */
314 static void cec_data_completed(struct cec_data *data)
315 {
316         /*
317          * Delete this transmit from the filehandle's xfer_list since
318          * we're done with it.
319          *
320          * Note that if the filehandle is closed before this transmit
321          * finished, then the release() function will set data->fh to NULL.
322          * Without that we would be referring to a closed filehandle.
323          */
324         if (data->fh)
325                 list_del_init(&data->xfer_list);
326
327         if (data->blocking) {
328                 /*
329                  * Someone is blocking so mark the message as completed
330                  * and call complete.
331                  */
332                 data->completed = true;
333                 complete(&data->c);
334         } else {
335                 /*
336                  * No blocking, so just queue the message if needed and
337                  * free the memory.
338                  */
339                 if (data->fh)
340                         cec_queue_msg_fh(data->fh, &data->msg);
341                 kfree(data);
342         }
343 }
344
345 /*
346  * A pending CEC transmit needs to be cancelled, either because the CEC
347  * adapter is disabled or the transmit takes an impossibly long time to
348  * finish, or the reply timed out.
349  *
350  * This function is called with adap->lock held.
351  */
352 static void cec_data_cancel(struct cec_data *data, u8 tx_status, u8 rx_status)
353 {
354         struct cec_adapter *adap = data->adap;
355
356         /*
357          * It's either the current transmit, or it is a pending
358          * transmit. Take the appropriate action to clear it.
359          */
360         if (adap->transmitting == data) {
361                 adap->transmitting = NULL;
362         } else {
363                 list_del_init(&data->list);
364                 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
365                         if (!WARN_ON(!adap->transmit_queue_sz))
366                                 adap->transmit_queue_sz--;
367         }
368
369         if (data->msg.tx_status & CEC_TX_STATUS_OK) {
370                 data->msg.rx_ts = ktime_get_ns();
371                 data->msg.rx_status = rx_status;
372                 if (!data->blocking)
373                         data->msg.tx_status = 0;
374         } else {
375                 data->msg.tx_ts = ktime_get_ns();
376                 data->msg.tx_status |= tx_status |
377                                        CEC_TX_STATUS_MAX_RETRIES;
378                 data->msg.tx_error_cnt++;
379                 data->attempts = 0;
380                 if (!data->blocking)
381                         data->msg.rx_status = 0;
382         }
383
384         /* Queue transmitted message for monitoring purposes */
385         cec_queue_msg_monitor(adap, &data->msg, 1);
386
387         if (!data->blocking && data->msg.sequence)
388                 /* Allow drivers to react to a canceled transmit */
389                 call_void_op(adap, adap_nb_transmit_canceled, &data->msg);
390
391         cec_data_completed(data);
392 }
393
394 /*
395  * Flush all pending transmits and cancel any pending timeout work.
396  *
397  * This function is called with adap->lock held.
398  */
399 static void cec_flush(struct cec_adapter *adap)
400 {
401         struct cec_data *data, *n;
402
403         /*
404          * If the adapter is disabled, or we're asked to stop,
405          * then cancel any pending transmits.
406          */
407         while (!list_empty(&adap->transmit_queue)) {
408                 data = list_first_entry(&adap->transmit_queue,
409                                         struct cec_data, list);
410                 cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0);
411         }
412         if (adap->transmitting)
413                 adap->transmit_in_progress_aborted = true;
414
415         /* Cancel the pending timeout work. */
416         list_for_each_entry_safe(data, n, &adap->wait_queue, list) {
417                 if (cancel_delayed_work(&data->work))
418                         cec_data_cancel(data, CEC_TX_STATUS_OK, CEC_RX_STATUS_ABORTED);
419                 /*
420                  * If cancel_delayed_work returned false, then
421                  * the cec_wait_timeout function is running,
422                  * which will call cec_data_completed. So no
423                  * need to do anything special in that case.
424                  */
425         }
426         /*
427          * If something went wrong and this counter isn't what it should
428          * be, then this will reset it back to 0. Warn if it is not 0,
429          * since it indicates a bug, either in this framework or in a
430          * CEC driver.
431          */
432         if (WARN_ON(adap->transmit_queue_sz))
433                 adap->transmit_queue_sz = 0;
434 }
435
436 /*
437  * Main CEC state machine
438  *
439  * Wait until the thread should be stopped, or we are not transmitting and
440  * a new transmit message is queued up, in which case we start transmitting
441  * that message. When the adapter finished transmitting the message it will
442  * call cec_transmit_done().
443  *
444  * If the adapter is disabled, then remove all queued messages instead.
445  *
446  * If the current transmit times out, then cancel that transmit.
447  */
448 int cec_thread_func(void *_adap)
449 {
450         struct cec_adapter *adap = _adap;
451
452         for (;;) {
453                 unsigned int signal_free_time;
454                 struct cec_data *data;
455                 bool timeout = false;
456                 u8 attempts;
457
458                 if (adap->transmit_in_progress) {
459                         int err;
460
461                         /*
462                          * We are transmitting a message, so add a timeout
463                          * to prevent the state machine to get stuck waiting
464                          * for this message to finalize and add a check to
465                          * see if the adapter is disabled in which case the
466                          * transmit should be canceled.
467                          */
468                         err = wait_event_interruptible_timeout(adap->kthread_waitq,
469                                 (adap->needs_hpd &&
470                                  (!adap->is_configured && !adap->is_configuring)) ||
471                                 kthread_should_stop() ||
472                                 (!adap->transmit_in_progress &&
473                                  !list_empty(&adap->transmit_queue)),
474                                 msecs_to_jiffies(adap->xfer_timeout_ms));
475                         timeout = err == 0;
476                 } else {
477                         /* Otherwise we just wait for something to happen. */
478                         wait_event_interruptible(adap->kthread_waitq,
479                                 kthread_should_stop() ||
480                                 (!adap->transmit_in_progress &&
481                                  !list_empty(&adap->transmit_queue)));
482                 }
483
484                 mutex_lock(&adap->lock);
485
486                 if ((adap->needs_hpd &&
487                      (!adap->is_configured && !adap->is_configuring)) ||
488                     kthread_should_stop()) {
489                         cec_flush(adap);
490                         goto unlock;
491                 }
492
493                 if (adap->transmit_in_progress &&
494                     adap->transmit_in_progress_aborted) {
495                         if (adap->transmitting)
496                                 cec_data_cancel(adap->transmitting,
497                                                 CEC_TX_STATUS_ABORTED, 0);
498                         adap->transmit_in_progress = false;
499                         adap->transmit_in_progress_aborted = false;
500                         goto unlock;
501                 }
502                 if (adap->transmit_in_progress && timeout) {
503                         /*
504                          * If we timeout, then log that. Normally this does
505                          * not happen and it is an indication of a faulty CEC
506                          * adapter driver, or the CEC bus is in some weird
507                          * state. On rare occasions it can happen if there is
508                          * so much traffic on the bus that the adapter was
509                          * unable to transmit for xfer_timeout_ms (2.1s by
510                          * default).
511                          */
512                         if (adap->transmitting) {
513                                 pr_warn("cec-%s: message %*ph timed out\n", adap->name,
514                                         adap->transmitting->msg.len,
515                                         adap->transmitting->msg.msg);
516                                 /* Just give up on this. */
517                                 cec_data_cancel(adap->transmitting,
518                                                 CEC_TX_STATUS_TIMEOUT, 0);
519                         } else {
520                                 pr_warn("cec-%s: transmit timed out\n", adap->name);
521                         }
522                         adap->transmit_in_progress = false;
523                         adap->tx_timeout_cnt++;
524                         goto unlock;
525                 }
526
527                 /*
528                  * If we are still transmitting, or there is nothing new to
529                  * transmit, then just continue waiting.
530                  */
531                 if (adap->transmit_in_progress || list_empty(&adap->transmit_queue))
532                         goto unlock;
533
534                 /* Get a new message to transmit */
535                 data = list_first_entry(&adap->transmit_queue,
536                                         struct cec_data, list);
537                 list_del_init(&data->list);
538                 if (!WARN_ON(!data->adap->transmit_queue_sz))
539                         adap->transmit_queue_sz--;
540
541                 /* Make this the current transmitting message */
542                 adap->transmitting = data;
543
544                 /*
545                  * Suggested number of attempts as per the CEC 2.0 spec:
546                  * 4 attempts is the default, except for 'secondary poll
547                  * messages', i.e. poll messages not sent during the adapter
548                  * configuration phase when it allocates logical addresses.
549                  */
550                 if (data->msg.len == 1 && adap->is_configured)
551                         attempts = 2;
552                 else
553                         attempts = 4;
554
555                 /* Set the suggested signal free time */
556                 if (data->attempts) {
557                         /* should be >= 3 data bit periods for a retry */
558                         signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
559                 } else if (adap->last_initiator !=
560                            cec_msg_initiator(&data->msg)) {
561                         /* should be >= 5 data bit periods for new initiator */
562                         signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
563                         adap->last_initiator = cec_msg_initiator(&data->msg);
564                 } else {
565                         /*
566                          * should be >= 7 data bit periods for sending another
567                          * frame immediately after another.
568                          */
569                         signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
570                 }
571                 if (data->attempts == 0)
572                         data->attempts = attempts;
573
574                 adap->transmit_in_progress_aborted = false;
575                 /* Tell the adapter to transmit, cancel on error */
576                 if (call_op(adap, adap_transmit, data->attempts,
577                             signal_free_time, &data->msg))
578                         cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0);
579                 else
580                         adap->transmit_in_progress = true;
581
582 unlock:
583                 mutex_unlock(&adap->lock);
584
585                 if (kthread_should_stop())
586                         break;
587         }
588         return 0;
589 }
590
591 /*
592  * Called by the CEC adapter if a transmit finished.
593  */
594 void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
595                           u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
596                           u8 error_cnt, ktime_t ts)
597 {
598         struct cec_data *data;
599         struct cec_msg *msg;
600         unsigned int attempts_made = arb_lost_cnt + nack_cnt +
601                                      low_drive_cnt + error_cnt;
602         bool done = status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK);
603         bool aborted = adap->transmit_in_progress_aborted;
604
605         dprintk(2, "%s: status 0x%02x\n", __func__, status);
606         if (attempts_made < 1)
607                 attempts_made = 1;
608
609         mutex_lock(&adap->lock);
610         data = adap->transmitting;
611         if (!data) {
612                 /*
613                  * This might happen if a transmit was issued and the cable is
614                  * unplugged while the transmit is ongoing. Ignore this
615                  * transmit in that case.
616                  */
617                 if (!adap->transmit_in_progress)
618                         dprintk(1, "%s was called without an ongoing transmit!\n",
619                                 __func__);
620                 adap->transmit_in_progress = false;
621                 goto wake_thread;
622         }
623         adap->transmit_in_progress = false;
624         adap->transmit_in_progress_aborted = false;
625
626         msg = &data->msg;
627
628         /* Drivers must fill in the status! */
629         WARN_ON(status == 0);
630         msg->tx_ts = ktime_to_ns(ts);
631         msg->tx_status |= status;
632         msg->tx_arb_lost_cnt += arb_lost_cnt;
633         msg->tx_nack_cnt += nack_cnt;
634         msg->tx_low_drive_cnt += low_drive_cnt;
635         msg->tx_error_cnt += error_cnt;
636
637         adap->tx_arb_lost_cnt += arb_lost_cnt;
638         adap->tx_low_drive_cnt += low_drive_cnt;
639         adap->tx_error_cnt += error_cnt;
640
641         /*
642          * Low Drive transmission errors should really not happen for
643          * well-behaved CEC devices and proper HDMI cables.
644          *
645          * Ditto for the 'Error' status.
646          *
647          * For the first few times that this happens, log this.
648          * Stop logging after that, since that will not add any more
649          * useful information and instead it will just flood the kernel log.
650          */
651         if (done && adap->tx_low_drive_log_cnt < 8 && msg->tx_low_drive_cnt) {
652                 adap->tx_low_drive_log_cnt++;
653                 dprintk(0, "low drive counter: %u (seq %u: %*ph)\n",
654                         msg->tx_low_drive_cnt, msg->sequence,
655                         msg->len, msg->msg);
656         }
657         if (done && adap->tx_error_log_cnt < 8 && msg->tx_error_cnt) {
658                 adap->tx_error_log_cnt++;
659                 dprintk(0, "error counter: %u (seq %u: %*ph)\n",
660                         msg->tx_error_cnt, msg->sequence,
661                         msg->len, msg->msg);
662         }
663
664         /* Mark that we're done with this transmit */
665         adap->transmitting = NULL;
666
667         /*
668          * If there are still retry attempts left and there was an error and
669          * the hardware didn't signal that it retried itself (by setting
670          * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
671          */
672         if (!aborted && data->attempts > attempts_made && !done) {
673                 /* Retry this message */
674                 data->attempts -= attempts_made;
675                 if (msg->timeout)
676                         dprintk(2, "retransmit: %*ph (attempts: %d, wait for %*ph)\n",
677                                 msg->len, msg->msg, data->attempts,
678                                 data->match_len, data->match_reply);
679                 else
680                         dprintk(2, "retransmit: %*ph (attempts: %d)\n",
681                                 msg->len, msg->msg, data->attempts);
682                 /* Add the message in front of the transmit queue */
683                 list_add(&data->list, &adap->transmit_queue);
684                 adap->transmit_queue_sz++;
685                 goto wake_thread;
686         }
687
688         if (aborted && !done)
689                 status |= CEC_TX_STATUS_ABORTED;
690         data->attempts = 0;
691
692         /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
693         if (!(status & CEC_TX_STATUS_OK))
694                 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
695
696         /* Queue transmitted message for monitoring purposes */
697         cec_queue_msg_monitor(adap, msg, 1);
698
699         if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
700             msg->timeout) {
701                 /*
702                  * Queue the message into the wait queue if we want to wait
703                  * for a reply.
704                  */
705                 list_add_tail(&data->list, &adap->wait_queue);
706                 schedule_delayed_work(&data->work,
707                                       msecs_to_jiffies(msg->timeout));
708         } else {
709                 /* Otherwise we're done */
710                 cec_data_completed(data);
711         }
712
713 wake_thread:
714         /*
715          * Wake up the main thread to see if another message is ready
716          * for transmitting or to retry the current message.
717          */
718         wake_up_interruptible(&adap->kthread_waitq);
719         mutex_unlock(&adap->lock);
720 }
721 EXPORT_SYMBOL_GPL(cec_transmit_done_ts);
722
723 void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
724                                   u8 status, ktime_t ts)
725 {
726         switch (status & ~CEC_TX_STATUS_MAX_RETRIES) {
727         case CEC_TX_STATUS_OK:
728                 cec_transmit_done_ts(adap, status, 0, 0, 0, 0, ts);
729                 return;
730         case CEC_TX_STATUS_ARB_LOST:
731                 cec_transmit_done_ts(adap, status, 1, 0, 0, 0, ts);
732                 return;
733         case CEC_TX_STATUS_NACK:
734                 cec_transmit_done_ts(adap, status, 0, 1, 0, 0, ts);
735                 return;
736         case CEC_TX_STATUS_LOW_DRIVE:
737                 cec_transmit_done_ts(adap, status, 0, 0, 1, 0, ts);
738                 return;
739         case CEC_TX_STATUS_ERROR:
740                 cec_transmit_done_ts(adap, status, 0, 0, 0, 1, ts);
741                 return;
742         default:
743                 /* Should never happen */
744                 WARN(1, "cec-%s: invalid status 0x%02x\n", adap->name, status);
745                 return;
746         }
747 }
748 EXPORT_SYMBOL_GPL(cec_transmit_attempt_done_ts);
749
750 /*
751  * Called when waiting for a reply times out.
752  */
753 static void cec_wait_timeout(struct work_struct *work)
754 {
755         struct cec_data *data = container_of(work, struct cec_data, work.work);
756         struct cec_adapter *adap = data->adap;
757
758         mutex_lock(&adap->lock);
759         /*
760          * Sanity check in case the timeout and the arrival of the message
761          * happened at the same time.
762          */
763         if (list_empty(&data->list))
764                 goto unlock;
765
766         /* Mark the message as timed out */
767         list_del_init(&data->list);
768         cec_data_cancel(data, CEC_TX_STATUS_OK, CEC_RX_STATUS_TIMEOUT);
769 unlock:
770         mutex_unlock(&adap->lock);
771 }
772
773 /*
774  * Transmit a message. The fh argument may be NULL if the transmit is not
775  * associated with a specific filehandle.
776  *
777  * This function is called with adap->lock held.
778  */
779 int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
780                         struct cec_fh *fh, bool block)
781 {
782         struct cec_data *data;
783         bool is_raw = msg_is_raw(msg);
784         bool reply_vendor_id = (msg->flags & CEC_MSG_FL_REPLY_VENDOR_ID) &&
785                 msg->len > 1 && msg->msg[1] == CEC_MSG_VENDOR_COMMAND_WITH_ID;
786         int err;
787
788         if (adap->devnode.unregistered)
789                 return -ENODEV;
790
791         msg->rx_ts = 0;
792         msg->tx_ts = 0;
793         msg->rx_status = 0;
794         msg->tx_status = 0;
795         msg->tx_arb_lost_cnt = 0;
796         msg->tx_nack_cnt = 0;
797         msg->tx_low_drive_cnt = 0;
798         msg->tx_error_cnt = 0;
799         msg->sequence = 0;
800         msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS | CEC_MSG_FL_RAW |
801                       (reply_vendor_id ? CEC_MSG_FL_REPLY_VENDOR_ID : 0);
802
803         if ((reply_vendor_id || msg->reply) && msg->timeout == 0) {
804                 /* Make sure the timeout isn't 0. */
805                 msg->timeout = 1000;
806         }
807
808         if (!msg->timeout)
809                 msg->flags &= ~CEC_MSG_FL_REPLY_TO_FOLLOWERS;
810
811         /* Sanity checks */
812         if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
813                 dprintk(1, "%s: invalid length %d\n", __func__, msg->len);
814                 return -EINVAL;
815         }
816         if (reply_vendor_id && msg->len < 6) {
817                 dprintk(1, "%s: <Vendor Command With ID> message too short\n",
818                         __func__);
819                 return -EINVAL;
820         }
821
822         memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
823
824         if (msg->timeout)
825                 dprintk(2, "%s: %*ph (wait for 0x%02x%s)\n",
826                         __func__, msg->len, msg->msg, msg->reply,
827                         !block ? ", nb" : "");
828         else
829                 dprintk(2, "%s: %*ph%s\n",
830                         __func__, msg->len, msg->msg, !block ? " (nb)" : "");
831
832         if (msg->timeout && msg->len == 1) {
833                 dprintk(1, "%s: can't reply to poll msg\n", __func__);
834                 return -EINVAL;
835         }
836
837         if (is_raw) {
838                 if (!capable(CAP_SYS_RAWIO))
839                         return -EPERM;
840         } else {
841                 /* A CDC-Only device can only send CDC messages */
842                 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
843                     (msg->len == 1 || msg->msg[1] != CEC_MSG_CDC_MESSAGE)) {
844                         dprintk(1, "%s: not a CDC message\n", __func__);
845                         return -EINVAL;
846                 }
847
848                 if (msg->len >= 4 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
849                         msg->msg[2] = adap->phys_addr >> 8;
850                         msg->msg[3] = adap->phys_addr & 0xff;
851                 }
852
853                 if (msg->len == 1) {
854                         if (cec_msg_destination(msg) == 0xf) {
855                                 dprintk(1, "%s: invalid poll message\n",
856                                         __func__);
857                                 return -EINVAL;
858                         }
859                         if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
860                                 /*
861                                  * If the destination is a logical address our
862                                  * adapter has already claimed, then just NACK
863                                  * this. It depends on the hardware what it will
864                                  * do with a POLL to itself (some OK this), so
865                                  * it is just as easy to handle it here so the
866                                  * behavior will be consistent.
867                                  */
868                                 msg->tx_ts = ktime_get_ns();
869                                 msg->tx_status = CEC_TX_STATUS_NACK |
870                                         CEC_TX_STATUS_MAX_RETRIES;
871                                 msg->tx_nack_cnt = 1;
872                                 msg->sequence = ++adap->sequence;
873                                 if (!msg->sequence)
874                                         msg->sequence = ++adap->sequence;
875                                 return 0;
876                         }
877                 }
878                 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
879                     cec_has_log_addr(adap, cec_msg_destination(msg))) {
880                         dprintk(1, "%s: destination is the adapter itself\n",
881                                 __func__);
882                         return -EINVAL;
883                 }
884                 if (msg->len > 1 && adap->is_configured &&
885                     !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
886                         dprintk(1, "%s: initiator has unknown logical address %d\n",
887                                 __func__, cec_msg_initiator(msg));
888                         return -EINVAL;
889                 }
890                 /*
891                  * Special case: allow Ping and IMAGE/TEXT_VIEW_ON to be
892                  * transmitted to a TV, even if the adapter is unconfigured.
893                  * This makes it possible to detect or wake up displays that
894                  * pull down the HPD when in standby.
895                  */
896                 if (!adap->is_configured && !adap->is_configuring &&
897                     (msg->len > 2 ||
898                      cec_msg_destination(msg) != CEC_LOG_ADDR_TV ||
899                      (msg->len == 2 && msg->msg[1] != CEC_MSG_IMAGE_VIEW_ON &&
900                       msg->msg[1] != CEC_MSG_TEXT_VIEW_ON))) {
901                         dprintk(1, "%s: adapter is unconfigured\n", __func__);
902                         return -ENONET;
903                 }
904         }
905
906         if (!adap->is_configured && !adap->is_configuring) {
907                 if (adap->needs_hpd) {
908                         dprintk(1, "%s: adapter is unconfigured and needs HPD\n",
909                                 __func__);
910                         return -ENONET;
911                 }
912                 if (reply_vendor_id || msg->reply) {
913                         dprintk(1, "%s: adapter is unconfigured so reply is not supported\n",
914                                 __func__);
915                         return -EINVAL;
916                 }
917         }
918
919         if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ) {
920                 dprintk(2, "%s: transmit queue full\n", __func__);
921                 return -EBUSY;
922         }
923
924         data = kzalloc(sizeof(*data), GFP_KERNEL);
925         if (!data)
926                 return -ENOMEM;
927
928         msg->sequence = ++adap->sequence;
929         if (!msg->sequence)
930                 msg->sequence = ++adap->sequence;
931
932         data->msg = *msg;
933         data->fh = fh;
934         data->adap = adap;
935         data->blocking = block;
936         if (reply_vendor_id) {
937                 memcpy(data->match_reply, msg->msg + 1, 4);
938                 data->match_reply[4] = msg->reply;
939                 data->match_len = 5;
940         } else if (msg->timeout) {
941                 data->match_reply[0] = msg->reply;
942                 data->match_len = 1;
943         }
944
945         init_completion(&data->c);
946         INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
947
948         if (fh)
949                 list_add_tail(&data->xfer_list, &fh->xfer_list);
950         else
951                 INIT_LIST_HEAD(&data->xfer_list);
952
953         list_add_tail(&data->list, &adap->transmit_queue);
954         adap->transmit_queue_sz++;
955         if (!adap->transmitting)
956                 wake_up_interruptible(&adap->kthread_waitq);
957
958         /* All done if we don't need to block waiting for completion */
959         if (!block)
960                 return 0;
961
962         /*
963          * Release the lock and wait, retake the lock afterwards.
964          */
965         mutex_unlock(&adap->lock);
966         err = wait_for_completion_killable(&data->c);
967         cancel_delayed_work_sync(&data->work);
968         mutex_lock(&adap->lock);
969
970         if (err)
971                 adap->transmit_in_progress_aborted = true;
972
973         /* Cancel the transmit if it was interrupted */
974         if (!data->completed) {
975                 if (data->msg.tx_status & CEC_TX_STATUS_OK)
976                         cec_data_cancel(data, CEC_TX_STATUS_OK, CEC_RX_STATUS_ABORTED);
977                 else
978                         cec_data_cancel(data, CEC_TX_STATUS_ABORTED, 0);
979         }
980
981         /* The transmit completed (possibly with an error) */
982         *msg = data->msg;
983         if (WARN_ON(!list_empty(&data->list)))
984                 list_del(&data->list);
985         if (WARN_ON(!list_empty(&data->xfer_list)))
986                 list_del(&data->xfer_list);
987         kfree(data);
988         return 0;
989 }
990
991 /* Helper function to be used by drivers and this framework. */
992 int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
993                      bool block)
994 {
995         int ret;
996
997         mutex_lock(&adap->lock);
998         ret = cec_transmit_msg_fh(adap, msg, NULL, block);
999         mutex_unlock(&adap->lock);
1000         return ret;
1001 }
1002 EXPORT_SYMBOL_GPL(cec_transmit_msg);
1003
1004 /*
1005  * I don't like forward references but without this the low-level
1006  * cec_received_msg() function would come after a bunch of high-level
1007  * CEC protocol handling functions. That was very confusing.
1008  */
1009 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1010                               bool is_reply);
1011
1012 #define DIRECTED        0x80
1013 #define BCAST1_4        0x40
1014 #define BCAST2_0        0x20    /* broadcast only allowed for >= 2.0 */
1015 #define BCAST           (BCAST1_4 | BCAST2_0)
1016 #define BOTH            (BCAST | DIRECTED)
1017
1018 /*
1019  * Specify minimum length and whether the message is directed, broadcast
1020  * or both. Messages that do not match the criteria are ignored as per
1021  * the CEC specification.
1022  */
1023 static const u8 cec_msg_size[256] = {
1024         [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
1025         [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
1026         [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
1027         [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
1028         [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
1029         [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
1030         [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
1031         [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
1032         [CEC_MSG_STANDBY] = 2 | BOTH,
1033         [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
1034         [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
1035         [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
1036         [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
1037         [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
1038         [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
1039         [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
1040         [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
1041         [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
1042         [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
1043         [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
1044         [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
1045         [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
1046         [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
1047         [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
1048         [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
1049         [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
1050         [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
1051         [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
1052         [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
1053         [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
1054         [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
1055         [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
1056         [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
1057         [CEC_MSG_PLAY] = 3 | DIRECTED,
1058         [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
1059         [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
1060         [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
1061         [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
1062         [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
1063         [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
1064         [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
1065         [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
1066         [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
1067         [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
1068         [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
1069         [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
1070         [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
1071         [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
1072         [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
1073         [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
1074         [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
1075         [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
1076         [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
1077         [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
1078         [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
1079         [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
1080         [CEC_MSG_ABORT] = 2 | DIRECTED,
1081         [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
1082         [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
1083         [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
1084         [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
1085         [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
1086         [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
1087         [CEC_MSG_SET_AUDIO_VOLUME_LEVEL] = 3 | DIRECTED,
1088         [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
1089         [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
1090         [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
1091         [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
1092         [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
1093         [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
1094         [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
1095         [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
1096         [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
1097         [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
1098         [CEC_MSG_REPORT_CURRENT_LATENCY] = 6 | BCAST,
1099         [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
1100 };
1101
1102 /* Called by the CEC adapter if a message is received */
1103 void cec_received_msg_ts(struct cec_adapter *adap,
1104                          struct cec_msg *msg, ktime_t ts)
1105 {
1106         struct cec_data *data;
1107         u8 msg_init = cec_msg_initiator(msg);
1108         u8 msg_dest = cec_msg_destination(msg);
1109         u8 cmd = msg->msg[1];
1110         bool is_reply = false;
1111         bool valid_la = true;
1112         bool monitor_valid_la = true;
1113         u8 min_len = 0;
1114
1115         if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
1116                 return;
1117
1118         if (adap->devnode.unregistered)
1119                 return;
1120
1121         /*
1122          * Some CEC adapters will receive the messages that they transmitted.
1123          * This test filters out those messages by checking if we are the
1124          * initiator, and just returning in that case.
1125          *
1126          * Note that this won't work if this is an Unregistered device.
1127          *
1128          * It is bad practice if the hardware receives the message that it
1129          * transmitted and luckily most CEC adapters behave correctly in this
1130          * respect.
1131          */
1132         if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
1133             cec_has_log_addr(adap, msg_init))
1134                 return;
1135
1136         msg->rx_ts = ktime_to_ns(ts);
1137         msg->rx_status = CEC_RX_STATUS_OK;
1138         msg->sequence = msg->reply = msg->timeout = 0;
1139         msg->tx_status = 0;
1140         msg->tx_ts = 0;
1141         msg->tx_arb_lost_cnt = 0;
1142         msg->tx_nack_cnt = 0;
1143         msg->tx_low_drive_cnt = 0;
1144         msg->tx_error_cnt = 0;
1145         msg->flags = 0;
1146         memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
1147
1148         mutex_lock(&adap->lock);
1149         dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
1150
1151         if (!adap->transmit_in_progress)
1152                 adap->last_initiator = 0xff;
1153
1154         /* Check if this message was for us (directed or broadcast). */
1155         if (!cec_msg_is_broadcast(msg)) {
1156                 valid_la = cec_has_log_addr(adap, msg_dest);
1157                 monitor_valid_la = valid_la;
1158         }
1159
1160         /*
1161          * Check if the length is not too short or if the message is a
1162          * broadcast message where a directed message was expected or
1163          * vice versa. If so, then the message has to be ignored (according
1164          * to section CEC 7.3 and CEC 12.2).
1165          */
1166         if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
1167                 u8 dir_fl = cec_msg_size[cmd] & BOTH;
1168
1169                 min_len = cec_msg_size[cmd] & 0x1f;
1170                 if (msg->len < min_len)
1171                         valid_la = false;
1172                 else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
1173                         valid_la = false;
1174                 else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST))
1175                         valid_la = false;
1176                 else if (cec_msg_is_broadcast(msg) &&
1177                          adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0 &&
1178                          !(dir_fl & BCAST1_4))
1179                         valid_la = false;
1180         }
1181         if (valid_la && min_len) {
1182                 /* These messages have special length requirements */
1183                 switch (cmd) {
1184                 case CEC_MSG_RECORD_ON:
1185                         switch (msg->msg[2]) {
1186                         case CEC_OP_RECORD_SRC_OWN:
1187                                 break;
1188                         case CEC_OP_RECORD_SRC_DIGITAL:
1189                                 if (msg->len < 10)
1190                                         valid_la = false;
1191                                 break;
1192                         case CEC_OP_RECORD_SRC_ANALOG:
1193                                 if (msg->len < 7)
1194                                         valid_la = false;
1195                                 break;
1196                         case CEC_OP_RECORD_SRC_EXT_PLUG:
1197                                 if (msg->len < 4)
1198                                         valid_la = false;
1199                                 break;
1200                         case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
1201                                 if (msg->len < 5)
1202                                         valid_la = false;
1203                                 break;
1204                         }
1205                         break;
1206                 }
1207         }
1208
1209         /* It's a valid message and not a poll or CDC message */
1210         if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
1211                 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
1212
1213                 /* The aborted command is in msg[2] */
1214                 if (abort)
1215                         cmd = msg->msg[2];
1216
1217                 /*
1218                  * Walk over all transmitted messages that are waiting for a
1219                  * reply.
1220                  */
1221                 list_for_each_entry(data, &adap->wait_queue, list) {
1222                         struct cec_msg *dst = &data->msg;
1223
1224                         /*
1225                          * The *only* CEC message that has two possible replies
1226                          * is CEC_MSG_INITIATE_ARC.
1227                          * In this case allow either of the two replies.
1228                          */
1229                         if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
1230                             (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
1231                              cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
1232                             (data->match_reply[0] == CEC_MSG_REPORT_ARC_INITIATED ||
1233                              data->match_reply[0] == CEC_MSG_REPORT_ARC_TERMINATED)) {
1234                                 dst->reply = cmd;
1235                                 data->match_reply[0] = cmd;
1236                         }
1237
1238                         /* Does the command match? */
1239                         if ((abort && cmd != dst->msg[1]) ||
1240                             (!abort && memcmp(data->match_reply, msg->msg + 1, data->match_len)))
1241                                 continue;
1242
1243                         /* Does the addressing match? */
1244                         if (msg_init != cec_msg_destination(dst) &&
1245                             !cec_msg_is_broadcast(dst))
1246                                 continue;
1247
1248                         /* We got a reply */
1249                         memcpy(dst->msg, msg->msg, msg->len);
1250                         dst->len = msg->len;
1251                         dst->rx_ts = msg->rx_ts;
1252                         dst->rx_status = msg->rx_status;
1253                         if (abort)
1254                                 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
1255                         msg->flags = dst->flags;
1256                         msg->sequence = dst->sequence;
1257                         /* Remove it from the wait_queue */
1258                         list_del_init(&data->list);
1259
1260                         /* Cancel the pending timeout work */
1261                         if (!cancel_delayed_work(&data->work)) {
1262                                 mutex_unlock(&adap->lock);
1263                                 cancel_delayed_work_sync(&data->work);
1264                                 mutex_lock(&adap->lock);
1265                         }
1266                         /*
1267                          * Mark this as a reply, provided someone is still
1268                          * waiting for the answer.
1269                          */
1270                         if (data->fh)
1271                                 is_reply = true;
1272                         cec_data_completed(data);
1273                         break;
1274                 }
1275         }
1276         mutex_unlock(&adap->lock);
1277
1278         /* Pass the message on to any monitoring filehandles */
1279         cec_queue_msg_monitor(adap, msg, monitor_valid_la);
1280
1281         /* We're done if it is not for us or a poll message */
1282         if (!valid_la || msg->len <= 1)
1283                 return;
1284
1285         if (adap->log_addrs.log_addr_mask == 0)
1286                 return;
1287
1288         /*
1289          * Process the message on the protocol level. If is_reply is true,
1290          * then cec_receive_notify() won't pass on the reply to the listener(s)
1291          * since that was already done by cec_data_completed() above.
1292          */
1293         cec_receive_notify(adap, msg, is_reply);
1294 }
1295 EXPORT_SYMBOL_GPL(cec_received_msg_ts);
1296
1297 /* Logical Address Handling */
1298
1299 /*
1300  * Attempt to claim a specific logical address.
1301  *
1302  * This function is called with adap->lock held.
1303  */
1304 static int cec_config_log_addr(struct cec_adapter *adap,
1305                                unsigned int idx,
1306                                unsigned int log_addr)
1307 {
1308         struct cec_log_addrs *las = &adap->log_addrs;
1309         struct cec_msg msg = { };
1310         const unsigned int max_retries = 2;
1311         unsigned int i;
1312         int err;
1313
1314         if (cec_has_log_addr(adap, log_addr))
1315                 return 0;
1316
1317         /* Send poll message */
1318         msg.len = 1;
1319         msg.msg[0] = (log_addr << 4) | log_addr;
1320
1321         for (i = 0; i < max_retries; i++) {
1322                 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
1323
1324                 /*
1325                  * While trying to poll the physical address was reset
1326                  * and the adapter was unconfigured, so bail out.
1327                  */
1328                 if (adap->phys_addr == CEC_PHYS_ADDR_INVALID)
1329                         return -EINTR;
1330
1331                 /* Also bail out if the PA changed while configuring. */
1332                 if (adap->must_reconfigure)
1333                         return -EINTR;
1334
1335                 if (err)
1336                         return err;
1337
1338                 /*
1339                  * The message was aborted or timed out due to a disconnect or
1340                  * unconfigure, just bail out.
1341                  */
1342                 if (msg.tx_status &
1343                     (CEC_TX_STATUS_ABORTED | CEC_TX_STATUS_TIMEOUT))
1344                         return -EINTR;
1345                 if (msg.tx_status & CEC_TX_STATUS_OK)
1346                         return 0;
1347                 if (msg.tx_status & CEC_TX_STATUS_NACK)
1348                         break;
1349                 /*
1350                  * Retry up to max_retries times if the message was neither
1351                  * OKed or NACKed. This can happen due to e.g. a Lost
1352                  * Arbitration condition.
1353                  */
1354         }
1355
1356         /*
1357          * If we are unable to get an OK or a NACK after max_retries attempts
1358          * (and note that each attempt already consists of four polls), then
1359          * we assume that something is really weird and that it is not a
1360          * good idea to try and claim this logical address.
1361          */
1362         if (i == max_retries) {
1363                 dprintk(0, "polling for LA %u failed with tx_status=0x%04x\n",
1364                         log_addr, msg.tx_status);
1365                 return 0;
1366         }
1367
1368         /*
1369          * Message not acknowledged, so this logical
1370          * address is free to use.
1371          */
1372         err = call_op(adap, adap_log_addr, log_addr);
1373         if (err)
1374                 return err;
1375
1376         las->log_addr[idx] = log_addr;
1377         las->log_addr_mask |= 1 << log_addr;
1378         return 1;
1379 }
1380
1381 /*
1382  * Unconfigure the adapter: clear all logical addresses and send
1383  * the state changed event.
1384  *
1385  * This function is called with adap->lock held.
1386  */
1387 static void cec_adap_unconfigure(struct cec_adapter *adap)
1388 {
1389         if (!adap->needs_hpd || adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1390                 WARN_ON(call_op(adap, adap_log_addr, CEC_LOG_ADDR_INVALID));
1391         adap->log_addrs.log_addr_mask = 0;
1392         adap->is_configured = false;
1393         cec_flush(adap);
1394         wake_up_interruptible(&adap->kthread_waitq);
1395         cec_post_state_event(adap);
1396         call_void_op(adap, adap_unconfigured);
1397 }
1398
1399 /*
1400  * Attempt to claim the required logical addresses.
1401  */
1402 static int cec_config_thread_func(void *arg)
1403 {
1404         /* The various LAs for each type of device */
1405         static const u8 tv_log_addrs[] = {
1406                 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
1407                 CEC_LOG_ADDR_INVALID
1408         };
1409         static const u8 record_log_addrs[] = {
1410                 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
1411                 CEC_LOG_ADDR_RECORD_3,
1412                 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1413                 CEC_LOG_ADDR_INVALID
1414         };
1415         static const u8 tuner_log_addrs[] = {
1416                 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
1417                 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
1418                 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1419                 CEC_LOG_ADDR_INVALID
1420         };
1421         static const u8 playback_log_addrs[] = {
1422                 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
1423                 CEC_LOG_ADDR_PLAYBACK_3,
1424                 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1425                 CEC_LOG_ADDR_INVALID
1426         };
1427         static const u8 audiosystem_log_addrs[] = {
1428                 CEC_LOG_ADDR_AUDIOSYSTEM,
1429                 CEC_LOG_ADDR_INVALID
1430         };
1431         static const u8 specific_use_log_addrs[] = {
1432                 CEC_LOG_ADDR_SPECIFIC,
1433                 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
1434                 CEC_LOG_ADDR_INVALID
1435         };
1436         static const u8 *type2addrs[6] = {
1437                 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
1438                 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
1439                 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
1440                 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
1441                 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
1442                 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
1443         };
1444         static const u16 type2mask[] = {
1445                 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
1446                 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
1447                 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
1448                 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
1449                 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
1450                 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
1451         };
1452         struct cec_adapter *adap = arg;
1453         struct cec_log_addrs *las = &adap->log_addrs;
1454         int err;
1455         int i, j;
1456
1457         mutex_lock(&adap->lock);
1458         dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
1459                 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
1460         las->log_addr_mask = 0;
1461
1462         if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
1463                 goto configured;
1464
1465 reconfigure:
1466         for (i = 0; i < las->num_log_addrs; i++) {
1467                 unsigned int type = las->log_addr_type[i];
1468                 const u8 *la_list;
1469                 u8 last_la;
1470
1471                 /*
1472                  * The TV functionality can only map to physical address 0.
1473                  * For any other address, try the Specific functionality
1474                  * instead as per the spec.
1475                  */
1476                 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1477                         type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1478
1479                 la_list = type2addrs[type];
1480                 last_la = las->log_addr[i];
1481                 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1482                 if (last_la == CEC_LOG_ADDR_INVALID ||
1483                     last_la == CEC_LOG_ADDR_UNREGISTERED ||
1484                     !((1 << last_la) & type2mask[type]))
1485                         last_la = la_list[0];
1486
1487                 err = cec_config_log_addr(adap, i, last_la);
1488
1489                 if (adap->must_reconfigure) {
1490                         adap->must_reconfigure = false;
1491                         las->log_addr_mask = 0;
1492                         goto reconfigure;
1493                 }
1494
1495                 if (err > 0) /* Reused last LA */
1496                         continue;
1497
1498                 if (err < 0)
1499                         goto unconfigure;
1500
1501                 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1502                         /* Tried this one already, skip it */
1503                         if (la_list[j] == last_la)
1504                                 continue;
1505                         /* The backup addresses are CEC 2.0 specific */
1506                         if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1507                              la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1508                             las->cec_version < CEC_OP_CEC_VERSION_2_0)
1509                                 continue;
1510
1511                         err = cec_config_log_addr(adap, i, la_list[j]);
1512                         if (err == 0) /* LA is in use */
1513                                 continue;
1514                         if (err < 0)
1515                                 goto unconfigure;
1516                         /* Done, claimed an LA */
1517                         break;
1518                 }
1519
1520                 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1521                         dprintk(1, "could not claim LA %d\n", i);
1522         }
1523
1524         if (adap->log_addrs.log_addr_mask == 0 &&
1525             !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
1526                 goto unconfigure;
1527
1528 configured:
1529         if (adap->log_addrs.log_addr_mask == 0) {
1530                 /* Fall back to unregistered */
1531                 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1532                 las->log_addr_mask = 1 << las->log_addr[0];
1533                 for (i = 1; i < las->num_log_addrs; i++)
1534                         las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1535         }
1536         for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
1537                 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1538         adap->is_configured = true;
1539         adap->is_configuring = false;
1540         adap->must_reconfigure = false;
1541         cec_post_state_event(adap);
1542
1543         /*
1544          * Now post the Report Features and Report Physical Address broadcast
1545          * messages. Note that these are non-blocking transmits, meaning that
1546          * they are just queued up and once adap->lock is unlocked the main
1547          * thread will kick in and start transmitting these.
1548          *
1549          * If after this function is done (but before one or more of these
1550          * messages are actually transmitted) the CEC adapter is unconfigured,
1551          * then any remaining messages will be dropped by the main thread.
1552          */
1553         for (i = 0; i < las->num_log_addrs; i++) {
1554                 struct cec_msg msg = {};
1555
1556                 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
1557                     (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
1558                         continue;
1559
1560                 msg.msg[0] = (las->log_addr[i] << 4) | 0x0f;
1561
1562                 /* Report Features must come first according to CEC 2.0 */
1563                 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED &&
1564                     adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0) {
1565                         cec_fill_msg_report_features(adap, &msg, i);
1566                         cec_transmit_msg_fh(adap, &msg, NULL, false);
1567                 }
1568
1569                 /* Report Physical Address */
1570                 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1571                                              las->primary_device_type[i]);
1572                 dprintk(1, "config: la %d pa %x.%x.%x.%x\n",
1573                         las->log_addr[i],
1574                         cec_phys_addr_exp(adap->phys_addr));
1575                 cec_transmit_msg_fh(adap, &msg, NULL, false);
1576
1577                 /* Report Vendor ID */
1578                 if (adap->log_addrs.vendor_id != CEC_VENDOR_ID_NONE) {
1579                         cec_msg_device_vendor_id(&msg,
1580                                                  adap->log_addrs.vendor_id);
1581                         cec_transmit_msg_fh(adap, &msg, NULL, false);
1582                 }
1583         }
1584         adap->kthread_config = NULL;
1585         complete(&adap->config_completion);
1586         mutex_unlock(&adap->lock);
1587         call_void_op(adap, configured);
1588         return 0;
1589
1590 unconfigure:
1591         for (i = 0; i < las->num_log_addrs; i++)
1592                 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1593         cec_adap_unconfigure(adap);
1594         adap->is_configuring = false;
1595         adap->must_reconfigure = false;
1596         adap->kthread_config = NULL;
1597         complete(&adap->config_completion);
1598         mutex_unlock(&adap->lock);
1599         return 0;
1600 }
1601
1602 /*
1603  * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1604  * logical addresses.
1605  *
1606  * This function is called with adap->lock held.
1607  */
1608 static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1609 {
1610         if (WARN_ON(adap->is_claiming_log_addrs ||
1611                     adap->is_configuring || adap->is_configured))
1612                 return;
1613
1614         adap->is_claiming_log_addrs = true;
1615
1616         init_completion(&adap->config_completion);
1617
1618         /* Ready to kick off the thread */
1619         adap->is_configuring = true;
1620         adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1621                                            "ceccfg-%s", adap->name);
1622         if (IS_ERR(adap->kthread_config)) {
1623                 adap->kthread_config = NULL;
1624                 adap->is_configuring = false;
1625         } else if (block) {
1626                 mutex_unlock(&adap->lock);
1627                 wait_for_completion(&adap->config_completion);
1628                 mutex_lock(&adap->lock);
1629         }
1630         adap->is_claiming_log_addrs = false;
1631 }
1632
1633 /*
1634  * Helper function to enable/disable the CEC adapter.
1635  *
1636  * This function is called with adap->lock held.
1637  */
1638 int cec_adap_enable(struct cec_adapter *adap)
1639 {
1640         bool enable;
1641         int ret = 0;
1642
1643         enable = adap->monitor_all_cnt || adap->monitor_pin_cnt ||
1644                  adap->log_addrs.num_log_addrs;
1645         if (adap->needs_hpd)
1646                 enable = enable && adap->phys_addr != CEC_PHYS_ADDR_INVALID;
1647
1648         if (adap->devnode.unregistered)
1649                 enable = false;
1650
1651         if (enable == adap->is_enabled)
1652                 return 0;
1653
1654         /* serialize adap_enable */
1655         mutex_lock(&adap->devnode.lock);
1656         if (enable) {
1657                 adap->last_initiator = 0xff;
1658                 adap->transmit_in_progress = false;
1659                 adap->tx_low_drive_log_cnt = 0;
1660                 adap->tx_error_log_cnt = 0;
1661                 ret = adap->ops->adap_enable(adap, true);
1662                 if (!ret) {
1663                         /*
1664                          * Enable monitor-all/pin modes if needed. We warn, but
1665                          * continue if this fails as this is not a critical error.
1666                          */
1667                         if (adap->monitor_all_cnt)
1668                                 WARN_ON(call_op(adap, adap_monitor_all_enable, true));
1669                         if (adap->monitor_pin_cnt)
1670                                 WARN_ON(call_op(adap, adap_monitor_pin_enable, true));
1671                 }
1672         } else {
1673                 /* Disable monitor-all/pin modes if needed (needs_hpd == 1) */
1674                 if (adap->monitor_all_cnt)
1675                         WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1676                 if (adap->monitor_pin_cnt)
1677                         WARN_ON(call_op(adap, adap_monitor_pin_enable, false));
1678                 WARN_ON(adap->ops->adap_enable(adap, false));
1679                 adap->last_initiator = 0xff;
1680                 adap->transmit_in_progress = false;
1681                 adap->transmit_in_progress_aborted = false;
1682                 if (adap->transmitting)
1683                         cec_data_cancel(adap->transmitting, CEC_TX_STATUS_ABORTED, 0);
1684         }
1685         if (!ret)
1686                 adap->is_enabled = enable;
1687         wake_up_interruptible(&adap->kthread_waitq);
1688         mutex_unlock(&adap->devnode.lock);
1689         return ret;
1690 }
1691
1692 /* Set a new physical address and send an event notifying userspace of this.
1693  *
1694  * This function is called with adap->lock held.
1695  */
1696 void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1697 {
1698         bool becomes_invalid = phys_addr == CEC_PHYS_ADDR_INVALID;
1699         bool is_invalid = adap->phys_addr == CEC_PHYS_ADDR_INVALID;
1700
1701         if (phys_addr == adap->phys_addr)
1702                 return;
1703         if (!becomes_invalid && adap->devnode.unregistered)
1704                 return;
1705
1706         dprintk(1, "new physical address %x.%x.%x.%x\n",
1707                 cec_phys_addr_exp(phys_addr));
1708         if (becomes_invalid || !is_invalid) {
1709                 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1710                 cec_post_state_event(adap);
1711                 cec_adap_unconfigure(adap);
1712                 if (becomes_invalid) {
1713                         cec_adap_enable(adap);
1714                         return;
1715                 }
1716         }
1717
1718         adap->phys_addr = phys_addr;
1719         if (is_invalid)
1720                 cec_adap_enable(adap);
1721
1722         cec_post_state_event(adap);
1723         if (!adap->log_addrs.num_log_addrs)
1724                 return;
1725         if (adap->is_configuring)
1726                 adap->must_reconfigure = true;
1727         else
1728                 cec_claim_log_addrs(adap, block);
1729 }
1730
1731 void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1732 {
1733         if (IS_ERR_OR_NULL(adap))
1734                 return;
1735
1736         mutex_lock(&adap->lock);
1737         __cec_s_phys_addr(adap, phys_addr, block);
1738         mutex_unlock(&adap->lock);
1739 }
1740 EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1741
1742 /*
1743  * Note: In the drm subsystem, prefer calling (if possible):
1744  *
1745  * cec_s_phys_addr(adap, connector->display_info.source_physical_address, false);
1746  */
1747 void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
1748                                const struct edid *edid)
1749 {
1750         u16 pa = CEC_PHYS_ADDR_INVALID;
1751
1752         if (edid && edid->extensions)
1753                 pa = cec_get_edid_phys_addr((const u8 *)edid,
1754                                 EDID_LENGTH * (edid->extensions + 1), NULL);
1755         cec_s_phys_addr(adap, pa, false);
1756 }
1757 EXPORT_SYMBOL_GPL(cec_s_phys_addr_from_edid);
1758
1759 void cec_s_conn_info(struct cec_adapter *adap,
1760                      const struct cec_connector_info *conn_info)
1761 {
1762         if (IS_ERR_OR_NULL(adap))
1763                 return;
1764
1765         if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO))
1766                 return;
1767
1768         mutex_lock(&adap->lock);
1769         if (conn_info)
1770                 adap->conn_info = *conn_info;
1771         else
1772                 memset(&adap->conn_info, 0, sizeof(adap->conn_info));
1773         cec_post_state_event(adap);
1774         mutex_unlock(&adap->lock);
1775 }
1776 EXPORT_SYMBOL_GPL(cec_s_conn_info);
1777
1778 /*
1779  * Called from either the ioctl or a driver to set the logical addresses.
1780  *
1781  * This function is called with adap->lock held.
1782  */
1783 int __cec_s_log_addrs(struct cec_adapter *adap,
1784                       struct cec_log_addrs *log_addrs, bool block)
1785 {
1786         u16 type_mask = 0;
1787         int err;
1788         int i;
1789
1790         if (adap->devnode.unregistered)
1791                 return -ENODEV;
1792
1793         if (!log_addrs || log_addrs->num_log_addrs == 0) {
1794                 if (!adap->log_addrs.num_log_addrs)
1795                         return 0;
1796                 if (adap->is_configuring || adap->is_configured)
1797                         cec_adap_unconfigure(adap);
1798                 adap->log_addrs.num_log_addrs = 0;
1799                 for (i = 0; i < CEC_MAX_LOG_ADDRS; i++)
1800                         adap->log_addrs.log_addr[i] = CEC_LOG_ADDR_INVALID;
1801                 adap->log_addrs.osd_name[0] = '\0';
1802                 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
1803                 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
1804                 cec_adap_enable(adap);
1805                 return 0;
1806         }
1807
1808         if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
1809                 /*
1810                  * Sanitize log_addrs fields if a CDC-Only device is
1811                  * requested.
1812                  */
1813                 log_addrs->num_log_addrs = 1;
1814                 log_addrs->osd_name[0] = '\0';
1815                 log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
1816                 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
1817                 /*
1818                  * This is just an internal convention since a CDC-Only device
1819                  * doesn't have to be a switch. But switches already use
1820                  * unregistered, so it makes some kind of sense to pick this
1821                  * as the primary device. Since a CDC-Only device never sends
1822                  * any 'normal' CEC messages this primary device type is never
1823                  * sent over the CEC bus.
1824                  */
1825                 log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
1826                 log_addrs->all_device_types[0] = 0;
1827                 log_addrs->features[0][0] = 0;
1828                 log_addrs->features[0][1] = 0;
1829         }
1830
1831         /* Ensure the osd name is 0-terminated */
1832         log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1833
1834         /* Sanity checks */
1835         if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1836                 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1837                 return -EINVAL;
1838         }
1839
1840         /*
1841          * Vendor ID is a 24 bit number, so check if the value is
1842          * within the correct range.
1843          */
1844         if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1845             (log_addrs->vendor_id & 0xff000000) != 0) {
1846                 dprintk(1, "invalid vendor ID\n");
1847                 return -EINVAL;
1848         }
1849
1850         if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1851             log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0) {
1852                 dprintk(1, "invalid CEC version\n");
1853                 return -EINVAL;
1854         }
1855
1856         if (log_addrs->num_log_addrs > 1)
1857                 for (i = 0; i < log_addrs->num_log_addrs; i++)
1858                         if (log_addrs->log_addr_type[i] ==
1859                                         CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1860                                 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1861                                 return -EINVAL;
1862                         }
1863
1864         for (i = 0; i < log_addrs->num_log_addrs; i++) {
1865                 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1866                 u8 *features = log_addrs->features[i];
1867                 bool op_is_dev_features = false;
1868                 unsigned int j;
1869
1870                 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1871                 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1872                         dprintk(1, "unknown logical address type\n");
1873                         return -EINVAL;
1874                 }
1875                 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1876                         dprintk(1, "duplicate logical address type\n");
1877                         return -EINVAL;
1878                 }
1879                 type_mask |= 1 << log_addrs->log_addr_type[i];
1880                 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1881                     (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1882                         /* Record already contains the playback functionality */
1883                         dprintk(1, "invalid record + playback combination\n");
1884                         return -EINVAL;
1885                 }
1886                 if (log_addrs->primary_device_type[i] >
1887                                         CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1888                         dprintk(1, "unknown primary device type\n");
1889                         return -EINVAL;
1890                 }
1891                 if (log_addrs->primary_device_type[i] == 2) {
1892                         dprintk(1, "invalid primary device type\n");
1893                         return -EINVAL;
1894                 }
1895                 for (j = 0; j < feature_sz; j++) {
1896                         if ((features[j] & 0x80) == 0) {
1897                                 if (op_is_dev_features)
1898                                         break;
1899                                 op_is_dev_features = true;
1900                         }
1901                 }
1902                 if (!op_is_dev_features || j == feature_sz) {
1903                         dprintk(1, "malformed features\n");
1904                         return -EINVAL;
1905                 }
1906                 /* Zero unused part of the feature array */
1907                 memset(features + j + 1, 0, feature_sz - j - 1);
1908         }
1909
1910         if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1911                 if (log_addrs->num_log_addrs > 2) {
1912                         dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1913                         return -EINVAL;
1914                 }
1915                 if (log_addrs->num_log_addrs == 2) {
1916                         if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1917                                            (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1918                                 dprintk(1, "two LAs is only allowed for audiosystem and TV\n");
1919                                 return -EINVAL;
1920                         }
1921                         if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1922                                            (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1923                                 dprintk(1, "an audiosystem/TV can only be combined with record or playback\n");
1924                                 return -EINVAL;
1925                         }
1926                 }
1927         }
1928
1929         /* Zero unused LAs */
1930         for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1931                 log_addrs->primary_device_type[i] = 0;
1932                 log_addrs->log_addr_type[i] = 0;
1933                 log_addrs->all_device_types[i] = 0;
1934                 memset(log_addrs->features[i], 0,
1935                        sizeof(log_addrs->features[i]));
1936         }
1937
1938         log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1939         adap->log_addrs = *log_addrs;
1940         err = cec_adap_enable(adap);
1941         if (!err && adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1942                 cec_claim_log_addrs(adap, block);
1943         return err;
1944 }
1945
1946 int cec_s_log_addrs(struct cec_adapter *adap,
1947                     struct cec_log_addrs *log_addrs, bool block)
1948 {
1949         int err;
1950
1951         mutex_lock(&adap->lock);
1952         err = __cec_s_log_addrs(adap, log_addrs, block);
1953         mutex_unlock(&adap->lock);
1954         return err;
1955 }
1956 EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1957
1958 /* High-level core CEC message handling */
1959
1960 /* Fill in the Report Features message */
1961 static void cec_fill_msg_report_features(struct cec_adapter *adap,
1962                                          struct cec_msg *msg,
1963                                          unsigned int la_idx)
1964 {
1965         const struct cec_log_addrs *las = &adap->log_addrs;
1966         const u8 *features = las->features[la_idx];
1967         bool op_is_dev_features = false;
1968         unsigned int idx;
1969
1970         /* Report Features */
1971         msg->msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1972         msg->len = 4;
1973         msg->msg[1] = CEC_MSG_REPORT_FEATURES;
1974         msg->msg[2] = adap->log_addrs.cec_version;
1975         msg->msg[3] = las->all_device_types[la_idx];
1976
1977         /* Write RC Profiles first, then Device Features */
1978         for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1979                 msg->msg[msg->len++] = features[idx];
1980                 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1981                         if (op_is_dev_features)
1982                                 break;
1983                         op_is_dev_features = true;
1984                 }
1985         }
1986 }
1987
1988 /* Transmit the Feature Abort message */
1989 static int cec_feature_abort_reason(struct cec_adapter *adap,
1990                                     struct cec_msg *msg, u8 reason)
1991 {
1992         struct cec_msg tx_msg = { };
1993
1994         /*
1995          * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1996          * message!
1997          */
1998         if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1999                 return 0;
2000         /* Don't Feature Abort messages from 'Unregistered' */
2001         if (cec_msg_initiator(msg) == CEC_LOG_ADDR_UNREGISTERED)
2002                 return 0;
2003         cec_msg_set_reply_to(&tx_msg, msg);
2004         cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
2005         return cec_transmit_msg(adap, &tx_msg, false);
2006 }
2007
2008 static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
2009 {
2010         return cec_feature_abort_reason(adap, msg,
2011                                         CEC_OP_ABORT_UNRECOGNIZED_OP);
2012 }
2013
2014 static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
2015 {
2016         return cec_feature_abort_reason(adap, msg,
2017                                         CEC_OP_ABORT_REFUSED);
2018 }
2019
2020 /*
2021  * Called when a CEC message is received. This function will do any
2022  * necessary core processing. The is_reply bool is true if this message
2023  * is a reply to an earlier transmit.
2024  *
2025  * The message is either a broadcast message or a valid directed message.
2026  */
2027 static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
2028                               bool is_reply)
2029 {
2030         bool is_broadcast = cec_msg_is_broadcast(msg);
2031         u8 dest_laddr = cec_msg_destination(msg);
2032         u8 init_laddr = cec_msg_initiator(msg);
2033         u8 devtype = cec_log_addr2dev(adap, dest_laddr);
2034         int la_idx = cec_log_addr2idx(adap, dest_laddr);
2035         bool from_unregistered = init_laddr == 0xf;
2036         struct cec_msg tx_cec_msg = { };
2037
2038         dprintk(2, "%s: %*ph\n", __func__, msg->len, msg->msg);
2039
2040         /* If this is a CDC-Only device, then ignore any non-CDC messages */
2041         if (cec_is_cdc_only(&adap->log_addrs) &&
2042             msg->msg[1] != CEC_MSG_CDC_MESSAGE)
2043                 return 0;
2044
2045         /* Allow drivers to process the message first */
2046         if (adap->ops->received && !adap->devnode.unregistered &&
2047             adap->ops->received(adap, msg) != -ENOMSG)
2048                 return 0;
2049
2050         /*
2051          * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
2052          * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
2053          * handled by the CEC core, even if the passthrough mode is on.
2054          * The others are just ignored if passthrough mode is on.
2055          */
2056         switch (msg->msg[1]) {
2057         case CEC_MSG_GET_CEC_VERSION:
2058         case CEC_MSG_ABORT:
2059         case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
2060         case CEC_MSG_GIVE_OSD_NAME:
2061                 /*
2062                  * These messages reply with a directed message, so ignore if
2063                  * the initiator is Unregistered.
2064                  */
2065                 if (!adap->passthrough && from_unregistered)
2066                         return 0;
2067                 fallthrough;
2068         case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
2069         case CEC_MSG_GIVE_FEATURES:
2070         case CEC_MSG_GIVE_PHYSICAL_ADDR:
2071                 /*
2072                  * Skip processing these messages if the passthrough mode
2073                  * is on.
2074                  */
2075                 if (adap->passthrough)
2076                         goto skip_processing;
2077                 /* Ignore if addressing is wrong */
2078                 if (is_broadcast)
2079                         return 0;
2080                 break;
2081
2082         case CEC_MSG_USER_CONTROL_PRESSED:
2083         case CEC_MSG_USER_CONTROL_RELEASED:
2084                 /* Wrong addressing mode: don't process */
2085                 if (is_broadcast || from_unregistered)
2086                         goto skip_processing;
2087                 break;
2088
2089         case CEC_MSG_REPORT_PHYSICAL_ADDR:
2090                 /*
2091                  * This message is always processed, regardless of the
2092                  * passthrough setting.
2093                  *
2094                  * Exception: don't process if wrong addressing mode.
2095                  */
2096                 if (!is_broadcast)
2097                         goto skip_processing;
2098                 break;
2099
2100         default:
2101                 break;
2102         }
2103
2104         cec_msg_set_reply_to(&tx_cec_msg, msg);
2105
2106         switch (msg->msg[1]) {
2107         /* The following messages are processed but still passed through */
2108         case CEC_MSG_REPORT_PHYSICAL_ADDR: {
2109                 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
2110
2111                 dprintk(1, "reported physical address %x.%x.%x.%x for logical address %d\n",
2112                         cec_phys_addr_exp(pa), init_laddr);
2113                 break;
2114         }
2115
2116         case CEC_MSG_USER_CONTROL_PRESSED:
2117                 if (!(adap->capabilities & CEC_CAP_RC) ||
2118                     !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
2119                         break;
2120
2121 #ifdef CONFIG_MEDIA_CEC_RC
2122                 switch (msg->msg[2]) {
2123                 /*
2124                  * Play function, this message can have variable length
2125                  * depending on the specific play function that is used.
2126                  */
2127                 case CEC_OP_UI_CMD_PLAY_FUNCTION:
2128                         if (msg->len == 2)
2129                                 rc_keydown(adap->rc, RC_PROTO_CEC,
2130                                            msg->msg[2], 0);
2131                         else
2132                                 rc_keydown(adap->rc, RC_PROTO_CEC,
2133                                            msg->msg[2] << 8 | msg->msg[3], 0);
2134                         break;
2135                 /*
2136                  * Other function messages that are not handled.
2137                  * Currently the RC framework does not allow to supply an
2138                  * additional parameter to a keypress. These "keys" contain
2139                  * other information such as channel number, an input number
2140                  * etc.
2141                  * For the time being these messages are not processed by the
2142                  * framework and are simply forwarded to the user space.
2143                  */
2144                 case CEC_OP_UI_CMD_SELECT_BROADCAST_TYPE:
2145                 case CEC_OP_UI_CMD_SELECT_SOUND_PRESENTATION:
2146                 case CEC_OP_UI_CMD_TUNE_FUNCTION:
2147                 case CEC_OP_UI_CMD_SELECT_MEDIA_FUNCTION:
2148                 case CEC_OP_UI_CMD_SELECT_AV_INPUT_FUNCTION:
2149                 case CEC_OP_UI_CMD_SELECT_AUDIO_INPUT_FUNCTION:
2150                         break;
2151                 default:
2152                         rc_keydown(adap->rc, RC_PROTO_CEC, msg->msg[2], 0);
2153                         break;
2154                 }
2155 #endif
2156                 break;
2157
2158         case CEC_MSG_USER_CONTROL_RELEASED:
2159                 if (!(adap->capabilities & CEC_CAP_RC) ||
2160                     !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
2161                         break;
2162 #ifdef CONFIG_MEDIA_CEC_RC
2163                 rc_keyup(adap->rc);
2164 #endif
2165                 break;
2166
2167         /*
2168          * The remaining messages are only processed if the passthrough mode
2169          * is off.
2170          */
2171         case CEC_MSG_GET_CEC_VERSION:
2172                 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
2173                 return cec_transmit_msg(adap, &tx_cec_msg, false);
2174
2175         case CEC_MSG_GIVE_PHYSICAL_ADDR:
2176                 /* Do nothing for CEC switches using addr 15 */
2177                 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
2178                         return 0;
2179                 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
2180                 return cec_transmit_msg(adap, &tx_cec_msg, false);
2181
2182         case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
2183                 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
2184                         return cec_feature_abort(adap, msg);
2185                 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
2186                 return cec_transmit_msg(adap, &tx_cec_msg, false);
2187
2188         case CEC_MSG_ABORT:
2189                 /* Do nothing for CEC switches */
2190                 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
2191                         return 0;
2192                 return cec_feature_refused(adap, msg);
2193
2194         case CEC_MSG_GIVE_OSD_NAME: {
2195                 if (adap->log_addrs.osd_name[0] == 0)
2196                         return cec_feature_abort(adap, msg);
2197                 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
2198                 return cec_transmit_msg(adap, &tx_cec_msg, false);
2199         }
2200
2201         case CEC_MSG_GIVE_FEATURES:
2202                 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
2203                         return cec_feature_abort(adap, msg);
2204                 cec_fill_msg_report_features(adap, &tx_cec_msg, la_idx);
2205                 return cec_transmit_msg(adap, &tx_cec_msg, false);
2206
2207         default:
2208                 /*
2209                  * Unprocessed messages are aborted if userspace isn't doing
2210                  * any processing either.
2211                  */
2212                 if (!is_broadcast && !is_reply && !adap->follower_cnt &&
2213                     !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
2214                         return cec_feature_abort(adap, msg);
2215                 break;
2216         }
2217
2218 skip_processing:
2219         /* If this was a reply, then we're done, unless otherwise specified */
2220         if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
2221                 return 0;
2222
2223         /*
2224          * Send to the exclusive follower if there is one, otherwise send
2225          * to all followers.
2226          */
2227         if (adap->cec_follower)
2228                 cec_queue_msg_fh(adap->cec_follower, msg);
2229         else
2230                 cec_queue_msg_followers(adap, msg);
2231         return 0;
2232 }
2233
2234 /*
2235  * Helper functions to keep track of the 'monitor all' use count.
2236  *
2237  * These functions are called with adap->lock held.
2238  */
2239 int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
2240 {
2241         int ret;
2242
2243         if (adap->monitor_all_cnt++)
2244                 return 0;
2245
2246         ret = cec_adap_enable(adap);
2247         if (ret)
2248                 adap->monitor_all_cnt--;
2249         return ret;
2250 }
2251
2252 void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
2253 {
2254         if (WARN_ON(!adap->monitor_all_cnt))
2255                 return;
2256         if (--adap->monitor_all_cnt)
2257                 return;
2258         WARN_ON(call_op(adap, adap_monitor_all_enable, false));
2259         cec_adap_enable(adap);
2260 }
2261
2262 /*
2263  * Helper functions to keep track of the 'monitor pin' use count.
2264  *
2265  * These functions are called with adap->lock held.
2266  */
2267 int cec_monitor_pin_cnt_inc(struct cec_adapter *adap)
2268 {
2269         int ret;
2270
2271         if (adap->monitor_pin_cnt++)
2272                 return 0;
2273
2274         ret = cec_adap_enable(adap);
2275         if (ret)
2276                 adap->monitor_pin_cnt--;
2277         return ret;
2278 }
2279
2280 void cec_monitor_pin_cnt_dec(struct cec_adapter *adap)
2281 {
2282         if (WARN_ON(!adap->monitor_pin_cnt))
2283                 return;
2284         if (--adap->monitor_pin_cnt)
2285                 return;
2286         WARN_ON(call_op(adap, adap_monitor_pin_enable, false));
2287         cec_adap_enable(adap);
2288 }
2289
2290 #ifdef CONFIG_DEBUG_FS
2291 /*
2292  * Log the current state of the CEC adapter.
2293  * Very useful for debugging.
2294  */
2295 int cec_adap_status(struct seq_file *file, void *priv)
2296 {
2297         struct cec_adapter *adap = dev_get_drvdata(file->private);
2298         struct cec_data *data;
2299
2300         mutex_lock(&adap->lock);
2301         seq_printf(file, "enabled: %d\n", adap->is_enabled);
2302         seq_printf(file, "configured: %d\n", adap->is_configured);
2303         seq_printf(file, "configuring: %d\n", adap->is_configuring);
2304         seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
2305                    cec_phys_addr_exp(adap->phys_addr));
2306         seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
2307         seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
2308         if (adap->cec_follower)
2309                 seq_printf(file, "has CEC follower%s\n",
2310                            adap->passthrough ? " (in passthrough mode)" : "");
2311         if (adap->cec_initiator)
2312                 seq_puts(file, "has CEC initiator\n");
2313         if (adap->monitor_all_cnt)
2314                 seq_printf(file, "file handles in Monitor All mode: %u\n",
2315                            adap->monitor_all_cnt);
2316         if (adap->monitor_pin_cnt)
2317                 seq_printf(file, "file handles in Monitor Pin mode: %u\n",
2318                            adap->monitor_pin_cnt);
2319         if (adap->tx_timeout_cnt) {
2320                 seq_printf(file, "transmit timeout count: %u\n",
2321                            adap->tx_timeout_cnt);
2322                 adap->tx_timeout_cnt = 0;
2323         }
2324         if (adap->tx_low_drive_cnt) {
2325                 seq_printf(file, "transmit low drive count: %u\n",
2326                            adap->tx_low_drive_cnt);
2327                 adap->tx_low_drive_cnt = 0;
2328         }
2329         if (adap->tx_arb_lost_cnt) {
2330                 seq_printf(file, "transmit arbitration lost count: %u\n",
2331                            adap->tx_arb_lost_cnt);
2332                 adap->tx_arb_lost_cnt = 0;
2333         }
2334         if (adap->tx_error_cnt) {
2335                 seq_printf(file, "transmit error count: %u\n",
2336                            adap->tx_error_cnt);
2337                 adap->tx_error_cnt = 0;
2338         }
2339         data = adap->transmitting;
2340         if (data)
2341                 seq_printf(file, "transmitting message: %*ph (reply: %*ph, timeout: %ums)\n",
2342                            data->msg.len, data->msg.msg,
2343                            data->match_len, data->match_reply,
2344                            data->msg.timeout);
2345         seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
2346         list_for_each_entry(data, &adap->transmit_queue, list) {
2347                 seq_printf(file, "queued tx message: %*ph (reply: %*ph, timeout: %ums)\n",
2348                            data->msg.len, data->msg.msg,
2349                            data->match_len, data->match_reply,
2350                            data->msg.timeout);
2351         }
2352         list_for_each_entry(data, &adap->wait_queue, list) {
2353                 seq_printf(file, "message waiting for reply: %*ph (reply: %*ph, timeout: %ums)\n",
2354                            data->msg.len, data->msg.msg,
2355                            data->match_len, data->match_reply,
2356                            data->msg.timeout);
2357         }
2358
2359         call_void_op(adap, adap_status, file);
2360         mutex_unlock(&adap->lock);
2361         return 0;
2362 }
2363 #endif
This page took 0.163161 seconds and 4 git commands to generate.