cec-core.c (12024B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * cec-core.c - HDMI Consumer Electronics Control framework - Core 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/slab.h> 14#include <linux/mm.h> 15#include <linux/string.h> 16#include <linux/types.h> 17 18#include "cec-priv.h" 19 20#define CEC_NUM_DEVICES 256 21#define CEC_NAME "cec" 22 23/* 24 * 400 ms is the time it takes for one 16 byte message to be 25 * transferred and 5 is the maximum number of retries. Add 26 * another 100 ms as a margin. So if the transmit doesn't 27 * finish before that time something is really wrong and we 28 * have to time out. 29 * 30 * This is a sign that something it really wrong and a warning 31 * will be issued. 32 */ 33#define CEC_XFER_TIMEOUT_MS (5 * 400 + 100) 34 35int cec_debug; 36module_param_named(debug, cec_debug, int, 0644); 37MODULE_PARM_DESC(debug, "debug level (0-2)"); 38 39static bool debug_phys_addr; 40module_param(debug_phys_addr, bool, 0644); 41MODULE_PARM_DESC(debug_phys_addr, "add CEC_CAP_PHYS_ADDR if set"); 42 43static dev_t cec_dev_t; 44 45/* Active devices */ 46static DEFINE_MUTEX(cec_devnode_lock); 47static DECLARE_BITMAP(cec_devnode_nums, CEC_NUM_DEVICES); 48 49static struct dentry *top_cec_dir; 50 51/* dev to cec_devnode */ 52#define to_cec_devnode(cd) container_of(cd, struct cec_devnode, dev) 53 54int cec_get_device(struct cec_devnode *devnode) 55{ 56 /* 57 * Check if the cec device is available. This needs to be done with 58 * the devnode->lock held to prevent an open/unregister race: 59 * without the lock, the device could be unregistered and freed between 60 * the devnode->registered check and get_device() calls, leading to 61 * a crash. 62 */ 63 mutex_lock(&devnode->lock); 64 /* 65 * return ENXIO if the cec device has been removed 66 * already or if it is not registered anymore. 67 */ 68 if (!devnode->registered) { 69 mutex_unlock(&devnode->lock); 70 return -ENXIO; 71 } 72 /* and increase the device refcount */ 73 get_device(&devnode->dev); 74 mutex_unlock(&devnode->lock); 75 return 0; 76} 77 78void cec_put_device(struct cec_devnode *devnode) 79{ 80 put_device(&devnode->dev); 81} 82 83/* Called when the last user of the cec device exits. */ 84static void cec_devnode_release(struct device *cd) 85{ 86 struct cec_devnode *devnode = to_cec_devnode(cd); 87 88 mutex_lock(&cec_devnode_lock); 89 /* Mark device node number as free */ 90 clear_bit(devnode->minor, cec_devnode_nums); 91 mutex_unlock(&cec_devnode_lock); 92 93 cec_delete_adapter(to_cec_adapter(devnode)); 94} 95 96static struct bus_type cec_bus_type = { 97 .name = CEC_NAME, 98}; 99 100/* 101 * Register a cec device node 102 * 103 * The registration code assigns minor numbers and registers the new device node 104 * with the kernel. An error is returned if no free minor number can be found, 105 * or if the registration of the device node fails. 106 * 107 * Zero is returned on success. 108 * 109 * Note that if the cec_devnode_register call fails, the release() callback of 110 * the cec_devnode structure is *not* called, so the caller is responsible for 111 * freeing any data. 112 */ 113static int __must_check cec_devnode_register(struct cec_devnode *devnode, 114 struct module *owner) 115{ 116 int minor; 117 int ret; 118 119 /* Part 1: Find a free minor number */ 120 mutex_lock(&cec_devnode_lock); 121 minor = find_first_zero_bit(cec_devnode_nums, CEC_NUM_DEVICES); 122 if (minor == CEC_NUM_DEVICES) { 123 mutex_unlock(&cec_devnode_lock); 124 pr_err("could not get a free minor\n"); 125 return -ENFILE; 126 } 127 128 set_bit(minor, cec_devnode_nums); 129 mutex_unlock(&cec_devnode_lock); 130 131 devnode->minor = minor; 132 devnode->dev.bus = &cec_bus_type; 133 devnode->dev.devt = MKDEV(MAJOR(cec_dev_t), minor); 134 devnode->dev.release = cec_devnode_release; 135 dev_set_name(&devnode->dev, "cec%d", devnode->minor); 136 device_initialize(&devnode->dev); 137 138 /* Part 2: Initialize and register the character device */ 139 cdev_init(&devnode->cdev, &cec_devnode_fops); 140 devnode->cdev.owner = owner; 141 kobject_set_name(&devnode->cdev.kobj, "cec%d", devnode->minor); 142 143 devnode->registered = true; 144 ret = cdev_device_add(&devnode->cdev, &devnode->dev); 145 if (ret) { 146 devnode->registered = false; 147 pr_err("%s: cdev_device_add failed\n", __func__); 148 goto clr_bit; 149 } 150 151 return 0; 152 153clr_bit: 154 mutex_lock(&cec_devnode_lock); 155 clear_bit(devnode->minor, cec_devnode_nums); 156 mutex_unlock(&cec_devnode_lock); 157 return ret; 158} 159 160/* 161 * Unregister a cec device node 162 * 163 * This unregisters the passed device. Future open calls will be met with 164 * errors. 165 * 166 * This function can safely be called if the device node has never been 167 * registered or has already been unregistered. 168 */ 169static void cec_devnode_unregister(struct cec_adapter *adap) 170{ 171 struct cec_devnode *devnode = &adap->devnode; 172 struct cec_fh *fh; 173 174 mutex_lock(&devnode->lock); 175 176 /* Check if devnode was never registered or already unregistered */ 177 if (!devnode->registered || devnode->unregistered) { 178 mutex_unlock(&devnode->lock); 179 return; 180 } 181 devnode->registered = false; 182 devnode->unregistered = true; 183 184 mutex_lock(&devnode->lock_fhs); 185 list_for_each_entry(fh, &devnode->fhs, list) 186 wake_up_interruptible(&fh->wait); 187 mutex_unlock(&devnode->lock_fhs); 188 189 mutex_unlock(&devnode->lock); 190 191 mutex_lock(&adap->lock); 192 __cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false); 193 __cec_s_log_addrs(adap, NULL, false); 194 mutex_unlock(&adap->lock); 195 196 cdev_device_del(&devnode->cdev, &devnode->dev); 197 put_device(&devnode->dev); 198} 199 200#ifdef CONFIG_DEBUG_FS 201static ssize_t cec_error_inj_write(struct file *file, 202 const char __user *ubuf, size_t count, loff_t *ppos) 203{ 204 struct seq_file *sf = file->private_data; 205 struct cec_adapter *adap = sf->private; 206 char *buf; 207 char *line; 208 char *p; 209 210 buf = memdup_user_nul(ubuf, min_t(size_t, PAGE_SIZE, count)); 211 if (IS_ERR(buf)) 212 return PTR_ERR(buf); 213 p = buf; 214 while (p && *p) { 215 p = skip_spaces(p); 216 line = strsep(&p, "\n"); 217 if (!*line || *line == '#') 218 continue; 219 if (!call_op(adap, error_inj_parse_line, line)) { 220 kfree(buf); 221 return -EINVAL; 222 } 223 } 224 kfree(buf); 225 return count; 226} 227 228static int cec_error_inj_show(struct seq_file *sf, void *unused) 229{ 230 struct cec_adapter *adap = sf->private; 231 232 return call_op(adap, error_inj_show, sf); 233} 234 235static int cec_error_inj_open(struct inode *inode, struct file *file) 236{ 237 return single_open(file, cec_error_inj_show, inode->i_private); 238} 239 240static const struct file_operations cec_error_inj_fops = { 241 .open = cec_error_inj_open, 242 .write = cec_error_inj_write, 243 .read = seq_read, 244 .llseek = seq_lseek, 245 .release = single_release, 246}; 247#endif 248 249struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops, 250 void *priv, const char *name, u32 caps, 251 u8 available_las) 252{ 253 struct cec_adapter *adap; 254 int res; 255 256#ifndef CONFIG_MEDIA_CEC_RC 257 caps &= ~CEC_CAP_RC; 258#endif 259 260 if (WARN_ON(!caps)) 261 return ERR_PTR(-EINVAL); 262 if (WARN_ON(!ops)) 263 return ERR_PTR(-EINVAL); 264 if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS)) 265 return ERR_PTR(-EINVAL); 266 adap = kzalloc(sizeof(*adap), GFP_KERNEL); 267 if (!adap) 268 return ERR_PTR(-ENOMEM); 269 strscpy(adap->name, name, sizeof(adap->name)); 270 adap->phys_addr = CEC_PHYS_ADDR_INVALID; 271 adap->cec_pin_is_high = true; 272 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0; 273 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE; 274 adap->capabilities = caps; 275 if (debug_phys_addr) 276 adap->capabilities |= CEC_CAP_PHYS_ADDR; 277 adap->needs_hpd = caps & CEC_CAP_NEEDS_HPD; 278 adap->available_log_addrs = available_las; 279 adap->sequence = 0; 280 adap->ops = ops; 281 adap->priv = priv; 282 mutex_init(&adap->lock); 283 INIT_LIST_HEAD(&adap->transmit_queue); 284 INIT_LIST_HEAD(&adap->wait_queue); 285 init_waitqueue_head(&adap->kthread_waitq); 286 287 /* adap->devnode initialization */ 288 INIT_LIST_HEAD(&adap->devnode.fhs); 289 mutex_init(&adap->devnode.lock_fhs); 290 mutex_init(&adap->devnode.lock); 291 292 adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name); 293 if (IS_ERR(adap->kthread)) { 294 pr_err("cec-%s: kernel_thread() failed\n", name); 295 res = PTR_ERR(adap->kthread); 296 kfree(adap); 297 return ERR_PTR(res); 298 } 299 300#ifdef CONFIG_MEDIA_CEC_RC 301 if (!(caps & CEC_CAP_RC)) 302 return adap; 303 304 /* Prepare the RC input device */ 305 adap->rc = rc_allocate_device(RC_DRIVER_SCANCODE); 306 if (!adap->rc) { 307 pr_err("cec-%s: failed to allocate memory for rc_dev\n", 308 name); 309 kthread_stop(adap->kthread); 310 kfree(adap); 311 return ERR_PTR(-ENOMEM); 312 } 313 314 snprintf(adap->input_phys, sizeof(adap->input_phys), 315 "%s/input0", adap->name); 316 317 adap->rc->device_name = adap->name; 318 adap->rc->input_phys = adap->input_phys; 319 adap->rc->input_id.bustype = BUS_CEC; 320 adap->rc->input_id.vendor = 0; 321 adap->rc->input_id.product = 0; 322 adap->rc->input_id.version = 1; 323 adap->rc->driver_name = CEC_NAME; 324 adap->rc->allowed_protocols = RC_PROTO_BIT_CEC; 325 adap->rc->priv = adap; 326 adap->rc->map_name = RC_MAP_CEC; 327 adap->rc->timeout = MS_TO_US(550); 328#endif 329 return adap; 330} 331EXPORT_SYMBOL_GPL(cec_allocate_adapter); 332 333int cec_register_adapter(struct cec_adapter *adap, 334 struct device *parent) 335{ 336 int res; 337 338 if (IS_ERR_OR_NULL(adap)) 339 return 0; 340 341 if (WARN_ON(!parent)) 342 return -EINVAL; 343 344 adap->owner = parent->driver->owner; 345 adap->devnode.dev.parent = parent; 346 if (!adap->xfer_timeout_ms) 347 adap->xfer_timeout_ms = CEC_XFER_TIMEOUT_MS; 348 349#ifdef CONFIG_MEDIA_CEC_RC 350 if (adap->capabilities & CEC_CAP_RC) { 351 adap->rc->dev.parent = parent; 352 res = rc_register_device(adap->rc); 353 354 if (res) { 355 pr_err("cec-%s: failed to prepare input device\n", 356 adap->name); 357 rc_free_device(adap->rc); 358 adap->rc = NULL; 359 return res; 360 } 361 } 362#endif 363 364 res = cec_devnode_register(&adap->devnode, adap->owner); 365 if (res) { 366#ifdef CONFIG_MEDIA_CEC_RC 367 /* Note: rc_unregister also calls rc_free */ 368 rc_unregister_device(adap->rc); 369 adap->rc = NULL; 370#endif 371 return res; 372 } 373 374 dev_set_drvdata(&adap->devnode.dev, adap); 375#ifdef CONFIG_DEBUG_FS 376 if (!top_cec_dir) 377 return 0; 378 379 adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev), 380 top_cec_dir); 381 382 debugfs_create_devm_seqfile(&adap->devnode.dev, "status", adap->cec_dir, 383 cec_adap_status); 384 385 if (!adap->ops->error_inj_show || !adap->ops->error_inj_parse_line) 386 return 0; 387 debugfs_create_file("error-inj", 0644, adap->cec_dir, adap, 388 &cec_error_inj_fops); 389#endif 390 return 0; 391} 392EXPORT_SYMBOL_GPL(cec_register_adapter); 393 394void cec_unregister_adapter(struct cec_adapter *adap) 395{ 396 if (IS_ERR_OR_NULL(adap)) 397 return; 398 399#ifdef CONFIG_MEDIA_CEC_RC 400 /* Note: rc_unregister also calls rc_free */ 401 rc_unregister_device(adap->rc); 402 adap->rc = NULL; 403#endif 404 debugfs_remove_recursive(adap->cec_dir); 405#ifdef CONFIG_CEC_NOTIFIER 406 cec_notifier_cec_adap_unregister(adap->notifier, adap); 407#endif 408 cec_devnode_unregister(adap); 409} 410EXPORT_SYMBOL_GPL(cec_unregister_adapter); 411 412void cec_delete_adapter(struct cec_adapter *adap) 413{ 414 if (IS_ERR_OR_NULL(adap)) 415 return; 416 if (adap->kthread_config) 417 kthread_stop(adap->kthread_config); 418 kthread_stop(adap->kthread); 419 if (adap->ops->adap_free) 420 adap->ops->adap_free(adap); 421#ifdef CONFIG_MEDIA_CEC_RC 422 rc_free_device(adap->rc); 423#endif 424 kfree(adap); 425} 426EXPORT_SYMBOL_GPL(cec_delete_adapter); 427 428/* 429 * Initialise cec for linux 430 */ 431static int __init cec_devnode_init(void) 432{ 433 int ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES, CEC_NAME); 434 435 if (ret < 0) { 436 pr_warn("cec: unable to allocate major\n"); 437 return ret; 438 } 439 440#ifdef CONFIG_DEBUG_FS 441 top_cec_dir = debugfs_create_dir("cec", NULL); 442 if (IS_ERR_OR_NULL(top_cec_dir)) { 443 pr_warn("cec: Failed to create debugfs cec dir\n"); 444 top_cec_dir = NULL; 445 } 446#endif 447 448 ret = bus_register(&cec_bus_type); 449 if (ret < 0) { 450 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES); 451 pr_warn("cec: bus_register failed\n"); 452 return -EIO; 453 } 454 455 return 0; 456} 457 458static void __exit cec_devnode_exit(void) 459{ 460 debugfs_remove_recursive(top_cec_dir); 461 bus_unregister(&cec_bus_type); 462 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES); 463} 464 465subsys_initcall(cec_devnode_init); 466module_exit(cec_devnode_exit) 467 468MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>"); 469MODULE_DESCRIPTION("Device node registration for cec drivers"); 470MODULE_LICENSE("GPL");