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