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