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