Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v4.12 599 lines 15 kB view raw
1/* 2 * cec-api.c - HDMI Consumer Electronics Control framework - API 3 * 4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 5 * 6 * This program is free software; you may redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; version 2 of the License. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 17 * SOFTWARE. 18 */ 19 20#include <linux/errno.h> 21#include <linux/init.h> 22#include <linux/module.h> 23#include <linux/kernel.h> 24#include <linux/kmod.h> 25#include <linux/ktime.h> 26#include <linux/slab.h> 27#include <linux/mm.h> 28#include <linux/string.h> 29#include <linux/types.h> 30#include <linux/uaccess.h> 31#include <linux/version.h> 32 33#include "cec-priv.h" 34 35static inline struct cec_devnode *cec_devnode_data(struct file *filp) 36{ 37 struct cec_fh *fh = filp->private_data; 38 39 return &fh->adap->devnode; 40} 41 42/* CEC file operations */ 43 44static unsigned int cec_poll(struct file *filp, 45 struct poll_table_struct *poll) 46{ 47 struct cec_devnode *devnode = cec_devnode_data(filp); 48 struct cec_fh *fh = filp->private_data; 49 struct cec_adapter *adap = fh->adap; 50 unsigned int res = 0; 51 52 if (!devnode->registered) 53 return POLLERR | POLLHUP; 54 mutex_lock(&adap->lock); 55 if (adap->is_configured && 56 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ) 57 res |= POLLOUT | POLLWRNORM; 58 if (fh->queued_msgs) 59 res |= POLLIN | POLLRDNORM; 60 if (fh->pending_events) 61 res |= POLLPRI; 62 poll_wait(filp, &fh->wait, poll); 63 mutex_unlock(&adap->lock); 64 return res; 65} 66 67static bool cec_is_busy(const struct cec_adapter *adap, 68 const struct cec_fh *fh) 69{ 70 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh; 71 bool valid_follower = adap->cec_follower && adap->cec_follower == fh; 72 73 /* 74 * Exclusive initiators and followers can always access the CEC adapter 75 */ 76 if (valid_initiator || valid_follower) 77 return false; 78 /* 79 * All others can only access the CEC adapter if there is no 80 * exclusive initiator and they are in INITIATOR mode. 81 */ 82 return adap->cec_initiator || 83 fh->mode_initiator == CEC_MODE_NO_INITIATOR; 84} 85 86static long cec_adap_g_caps(struct cec_adapter *adap, 87 struct cec_caps __user *parg) 88{ 89 struct cec_caps caps = {}; 90 91 strlcpy(caps.driver, adap->devnode.dev.parent->driver->name, 92 sizeof(caps.driver)); 93 strlcpy(caps.name, adap->name, sizeof(caps.name)); 94 caps.available_log_addrs = adap->available_log_addrs; 95 caps.capabilities = adap->capabilities; 96 caps.version = LINUX_VERSION_CODE; 97 if (copy_to_user(parg, &caps, sizeof(caps))) 98 return -EFAULT; 99 return 0; 100} 101 102static long cec_adap_g_phys_addr(struct cec_adapter *adap, 103 __u16 __user *parg) 104{ 105 u16 phys_addr; 106 107 mutex_lock(&adap->lock); 108 phys_addr = adap->phys_addr; 109 mutex_unlock(&adap->lock); 110 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr))) 111 return -EFAULT; 112 return 0; 113} 114 115static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh, 116 bool block, __u16 __user *parg) 117{ 118 u16 phys_addr; 119 long err; 120 121 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR)) 122 return -ENOTTY; 123 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr))) 124 return -EFAULT; 125 126 err = cec_phys_addr_validate(phys_addr, NULL, NULL); 127 if (err) 128 return err; 129 mutex_lock(&adap->lock); 130 if (cec_is_busy(adap, fh)) 131 err = -EBUSY; 132 else 133 __cec_s_phys_addr(adap, phys_addr, block); 134 mutex_unlock(&adap->lock); 135 return err; 136} 137 138static long cec_adap_g_log_addrs(struct cec_adapter *adap, 139 struct cec_log_addrs __user *parg) 140{ 141 struct cec_log_addrs log_addrs; 142 143 mutex_lock(&adap->lock); 144 log_addrs = adap->log_addrs; 145 if (!adap->is_configured) 146 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID, 147 sizeof(log_addrs.log_addr)); 148 mutex_unlock(&adap->lock); 149 150 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs))) 151 return -EFAULT; 152 return 0; 153} 154 155static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh, 156 bool block, struct cec_log_addrs __user *parg) 157{ 158 struct cec_log_addrs log_addrs; 159 long err = -EBUSY; 160 161 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS)) 162 return -ENOTTY; 163 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs))) 164 return -EFAULT; 165 log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK | 166 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU | 167 CEC_LOG_ADDRS_FL_CDC_ONLY; 168 mutex_lock(&adap->lock); 169 if (!adap->is_configuring && 170 (!log_addrs.num_log_addrs || !adap->is_configured) && 171 !cec_is_busy(adap, fh)) { 172 err = __cec_s_log_addrs(adap, &log_addrs, block); 173 if (!err) 174 log_addrs = adap->log_addrs; 175 } 176 mutex_unlock(&adap->lock); 177 if (err) 178 return err; 179 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs))) 180 return -EFAULT; 181 return 0; 182} 183 184static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh, 185 bool block, struct cec_msg __user *parg) 186{ 187 struct cec_msg msg = {}; 188 long err = 0; 189 190 if (!(adap->capabilities & CEC_CAP_TRANSMIT)) 191 return -ENOTTY; 192 if (copy_from_user(&msg, parg, sizeof(msg))) 193 return -EFAULT; 194 195 /* A CDC-Only device can only send CDC messages */ 196 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) && 197 (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE)) 198 return -EINVAL; 199 200 mutex_lock(&adap->lock); 201 if (adap->log_addrs.num_log_addrs == 0) 202 err = -EPERM; 203 else if (adap->is_configuring) 204 err = -ENONET; 205 else if (!adap->is_configured && msg.msg[0] != 0xf0) 206 err = -ENONET; 207 else if (cec_is_busy(adap, fh)) 208 err = -EBUSY; 209 else 210 err = cec_transmit_msg_fh(adap, &msg, fh, block); 211 mutex_unlock(&adap->lock); 212 if (err) 213 return err; 214 if (copy_to_user(parg, &msg, sizeof(msg))) 215 return -EFAULT; 216 return 0; 217} 218 219/* Called by CEC_RECEIVE: wait for a message to arrive */ 220static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block) 221{ 222 u32 timeout = msg->timeout; 223 int res; 224 225 do { 226 mutex_lock(&fh->lock); 227 /* Are there received messages queued up? */ 228 if (fh->queued_msgs) { 229 /* Yes, return the first one */ 230 struct cec_msg_entry *entry = 231 list_first_entry(&fh->msgs, 232 struct cec_msg_entry, list); 233 234 list_del(&entry->list); 235 *msg = entry->msg; 236 kfree(entry); 237 fh->queued_msgs--; 238 mutex_unlock(&fh->lock); 239 /* restore original timeout value */ 240 msg->timeout = timeout; 241 return 0; 242 } 243 244 /* No, return EAGAIN in non-blocking mode or wait */ 245 mutex_unlock(&fh->lock); 246 247 /* Return when in non-blocking mode */ 248 if (!block) 249 return -EAGAIN; 250 251 if (msg->timeout) { 252 /* The user specified a timeout */ 253 res = wait_event_interruptible_timeout(fh->wait, 254 fh->queued_msgs, 255 msecs_to_jiffies(msg->timeout)); 256 if (res == 0) 257 res = -ETIMEDOUT; 258 else if (res > 0) 259 res = 0; 260 } else { 261 /* Wait indefinitely */ 262 res = wait_event_interruptible(fh->wait, 263 fh->queued_msgs); 264 } 265 /* Exit on error, otherwise loop to get the new message */ 266 } while (!res); 267 return res; 268} 269 270static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh, 271 bool block, struct cec_msg __user *parg) 272{ 273 struct cec_msg msg = {}; 274 long err; 275 276 if (copy_from_user(&msg, parg, sizeof(msg))) 277 return -EFAULT; 278 279 err = cec_receive_msg(fh, &msg, block); 280 if (err) 281 return err; 282 msg.flags = 0; 283 if (copy_to_user(parg, &msg, sizeof(msg))) 284 return -EFAULT; 285 return 0; 286} 287 288static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh, 289 bool block, struct cec_event __user *parg) 290{ 291 struct cec_event *ev = NULL; 292 u64 ts = ~0ULL; 293 unsigned int i; 294 long err = 0; 295 296 mutex_lock(&fh->lock); 297 while (!fh->pending_events && block) { 298 mutex_unlock(&fh->lock); 299 err = wait_event_interruptible(fh->wait, fh->pending_events); 300 if (err) 301 return err; 302 mutex_lock(&fh->lock); 303 } 304 305 /* Find the oldest event */ 306 for (i = 0; i < CEC_NUM_EVENTS; i++) { 307 if (fh->pending_events & (1 << (i + 1)) && 308 fh->events[i].ts <= ts) { 309 ev = &fh->events[i]; 310 ts = ev->ts; 311 } 312 } 313 if (!ev) { 314 err = -EAGAIN; 315 goto unlock; 316 } 317 318 if (copy_to_user(parg, ev, sizeof(*ev))) { 319 err = -EFAULT; 320 goto unlock; 321 } 322 323 fh->pending_events &= ~(1 << ev->event); 324 325unlock: 326 mutex_unlock(&fh->lock); 327 return err; 328} 329 330static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh, 331 u32 __user *parg) 332{ 333 u32 mode = fh->mode_initiator | fh->mode_follower; 334 335 if (copy_to_user(parg, &mode, sizeof(mode))) 336 return -EFAULT; 337 return 0; 338} 339 340static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh, 341 u32 __user *parg) 342{ 343 u32 mode; 344 u8 mode_initiator; 345 u8 mode_follower; 346 long err = 0; 347 348 if (copy_from_user(&mode, parg, sizeof(mode))) 349 return -EFAULT; 350 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) 351 return -EINVAL; 352 353 mode_initiator = mode & CEC_MODE_INITIATOR_MSK; 354 mode_follower = mode & CEC_MODE_FOLLOWER_MSK; 355 356 if (mode_initiator > CEC_MODE_EXCL_INITIATOR || 357 mode_follower > CEC_MODE_MONITOR_ALL) 358 return -EINVAL; 359 360 if (mode_follower == CEC_MODE_MONITOR_ALL && 361 !(adap->capabilities & CEC_CAP_MONITOR_ALL)) 362 return -EINVAL; 363 364 /* Follower modes should always be able to send CEC messages */ 365 if ((mode_initiator == CEC_MODE_NO_INITIATOR || 366 !(adap->capabilities & CEC_CAP_TRANSMIT)) && 367 mode_follower >= CEC_MODE_FOLLOWER && 368 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) 369 return -EINVAL; 370 371 /* Monitor modes require CEC_MODE_NO_INITIATOR */ 372 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR) 373 return -EINVAL; 374 375 /* Monitor modes require CAP_NET_ADMIN */ 376 if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN)) 377 return -EPERM; 378 379 mutex_lock(&adap->lock); 380 /* 381 * You can't become exclusive follower if someone else already 382 * has that job. 383 */ 384 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER || 385 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) && 386 adap->cec_follower && adap->cec_follower != fh) 387 err = -EBUSY; 388 /* 389 * You can't become exclusive initiator if someone else already 390 * has that job. 391 */ 392 if (mode_initiator == CEC_MODE_EXCL_INITIATOR && 393 adap->cec_initiator && adap->cec_initiator != fh) 394 err = -EBUSY; 395 396 if (!err) { 397 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL; 398 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL; 399 400 if (old_mon_all != new_mon_all) { 401 if (new_mon_all) 402 err = cec_monitor_all_cnt_inc(adap); 403 else 404 cec_monitor_all_cnt_dec(adap); 405 } 406 } 407 408 if (err) { 409 mutex_unlock(&adap->lock); 410 return err; 411 } 412 413 if (fh->mode_follower == CEC_MODE_FOLLOWER) 414 adap->follower_cnt--; 415 if (mode_follower == CEC_MODE_FOLLOWER) 416 adap->follower_cnt++; 417 if (mode_follower == CEC_MODE_EXCL_FOLLOWER || 418 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) { 419 adap->passthrough = 420 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU; 421 adap->cec_follower = fh; 422 } else if (adap->cec_follower == fh) { 423 adap->passthrough = false; 424 adap->cec_follower = NULL; 425 } 426 if (mode_initiator == CEC_MODE_EXCL_INITIATOR) 427 adap->cec_initiator = fh; 428 else if (adap->cec_initiator == fh) 429 adap->cec_initiator = NULL; 430 fh->mode_initiator = mode_initiator; 431 fh->mode_follower = mode_follower; 432 mutex_unlock(&adap->lock); 433 return 0; 434} 435 436static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 437{ 438 struct cec_devnode *devnode = cec_devnode_data(filp); 439 struct cec_fh *fh = filp->private_data; 440 struct cec_adapter *adap = fh->adap; 441 bool block = !(filp->f_flags & O_NONBLOCK); 442 void __user *parg = (void __user *)arg; 443 444 if (!devnode->registered) 445 return -ENODEV; 446 447 switch (cmd) { 448 case CEC_ADAP_G_CAPS: 449 return cec_adap_g_caps(adap, parg); 450 451 case CEC_ADAP_G_PHYS_ADDR: 452 return cec_adap_g_phys_addr(adap, parg); 453 454 case CEC_ADAP_S_PHYS_ADDR: 455 return cec_adap_s_phys_addr(adap, fh, block, parg); 456 457 case CEC_ADAP_G_LOG_ADDRS: 458 return cec_adap_g_log_addrs(adap, parg); 459 460 case CEC_ADAP_S_LOG_ADDRS: 461 return cec_adap_s_log_addrs(adap, fh, block, parg); 462 463 case CEC_TRANSMIT: 464 return cec_transmit(adap, fh, block, parg); 465 466 case CEC_RECEIVE: 467 return cec_receive(adap, fh, block, parg); 468 469 case CEC_DQEVENT: 470 return cec_dqevent(adap, fh, block, parg); 471 472 case CEC_G_MODE: 473 return cec_g_mode(adap, fh, parg); 474 475 case CEC_S_MODE: 476 return cec_s_mode(adap, fh, parg); 477 478 default: 479 return -ENOTTY; 480 } 481} 482 483static int cec_open(struct inode *inode, struct file *filp) 484{ 485 struct cec_devnode *devnode = 486 container_of(inode->i_cdev, struct cec_devnode, cdev); 487 struct cec_adapter *adap = to_cec_adapter(devnode); 488 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL); 489 /* 490 * Initial events that are automatically sent when the cec device is 491 * opened. 492 */ 493 struct cec_event ev_state = { 494 .event = CEC_EVENT_STATE_CHANGE, 495 .flags = CEC_EVENT_FL_INITIAL_STATE, 496 }; 497 int err; 498 499 if (!fh) 500 return -ENOMEM; 501 502 INIT_LIST_HEAD(&fh->msgs); 503 INIT_LIST_HEAD(&fh->xfer_list); 504 mutex_init(&fh->lock); 505 init_waitqueue_head(&fh->wait); 506 507 fh->mode_initiator = CEC_MODE_INITIATOR; 508 fh->adap = adap; 509 510 err = cec_get_device(devnode); 511 if (err) { 512 kfree(fh); 513 return err; 514 } 515 516 mutex_lock(&devnode->lock); 517 if (list_empty(&devnode->fhs) && 518 adap->phys_addr == CEC_PHYS_ADDR_INVALID) { 519 err = adap->ops->adap_enable(adap, true); 520 if (err) { 521 mutex_unlock(&devnode->lock); 522 kfree(fh); 523 return err; 524 } 525 } 526 filp->private_data = fh; 527 528 /* Queue up initial state events */ 529 ev_state.state_change.phys_addr = adap->phys_addr; 530 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask; 531 cec_queue_event_fh(fh, &ev_state, 0); 532 533 list_add(&fh->list, &devnode->fhs); 534 mutex_unlock(&devnode->lock); 535 536 return 0; 537} 538 539/* Override for the release function */ 540static int cec_release(struct inode *inode, struct file *filp) 541{ 542 struct cec_devnode *devnode = cec_devnode_data(filp); 543 struct cec_adapter *adap = to_cec_adapter(devnode); 544 struct cec_fh *fh = filp->private_data; 545 546 mutex_lock(&adap->lock); 547 if (adap->cec_initiator == fh) 548 adap->cec_initiator = NULL; 549 if (adap->cec_follower == fh) { 550 adap->cec_follower = NULL; 551 adap->passthrough = false; 552 } 553 if (fh->mode_follower == CEC_MODE_FOLLOWER) 554 adap->follower_cnt--; 555 if (fh->mode_follower == CEC_MODE_MONITOR_ALL) 556 cec_monitor_all_cnt_dec(adap); 557 mutex_unlock(&adap->lock); 558 559 mutex_lock(&devnode->lock); 560 list_del(&fh->list); 561 if (list_empty(&devnode->fhs) && 562 adap->phys_addr == CEC_PHYS_ADDR_INVALID) { 563 WARN_ON(adap->ops->adap_enable(adap, false)); 564 } 565 mutex_unlock(&devnode->lock); 566 567 /* Unhook pending transmits from this filehandle. */ 568 mutex_lock(&adap->lock); 569 while (!list_empty(&fh->xfer_list)) { 570 struct cec_data *data = 571 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list); 572 573 data->blocking = false; 574 data->fh = NULL; 575 list_del(&data->xfer_list); 576 } 577 mutex_unlock(&adap->lock); 578 while (!list_empty(&fh->msgs)) { 579 struct cec_msg_entry *entry = 580 list_first_entry(&fh->msgs, struct cec_msg_entry, list); 581 582 list_del(&entry->list); 583 kfree(entry); 584 } 585 kfree(fh); 586 587 cec_put_device(devnode); 588 filp->private_data = NULL; 589 return 0; 590} 591 592const struct file_operations cec_devnode_fops = { 593 .owner = THIS_MODULE, 594 .open = cec_open, 595 .unlocked_ioctl = cec_ioctl, 596 .release = cec_release, 597 .poll = cec_poll, 598 .llseek = no_llseek, 599};