1 // SPDX-License-Identifier: GPL-2.0-only
3 * cec-core.c - HDMI Consumer Electronics Control framework - Core
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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>
15 #include <linux/string.h>
16 #include <linux/types.h>
20 #define CEC_NUM_DEVICES 256
21 #define CEC_NAME "cec"
24 module_param_named(debug
, cec_debug
, int, 0644);
25 MODULE_PARM_DESC(debug
, "debug level (0-2)");
27 static bool debug_phys_addr
;
28 module_param(debug_phys_addr
, bool, 0644);
29 MODULE_PARM_DESC(debug_phys_addr
, "add CEC_CAP_PHYS_ADDR if set");
31 static dev_t cec_dev_t
;
34 static DEFINE_MUTEX(cec_devnode_lock
);
35 static DECLARE_BITMAP(cec_devnode_nums
, CEC_NUM_DEVICES
);
37 static struct dentry
*top_cec_dir
;
39 /* dev to cec_devnode */
40 #define to_cec_devnode(cd) container_of(cd, struct cec_devnode, dev)
42 int cec_get_device(struct cec_devnode
*devnode
)
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
51 mutex_lock(&devnode
->lock
);
53 * return ENXIO if the cec device has been removed
54 * already or if it is not registered anymore.
56 if (!devnode
->registered
) {
57 mutex_unlock(&devnode
->lock
);
60 /* and increase the device refcount */
61 get_device(&devnode
->dev
);
62 mutex_unlock(&devnode
->lock
);
66 void cec_put_device(struct cec_devnode
*devnode
)
68 put_device(&devnode
->dev
);
71 /* Called when the last user of the cec device exits. */
72 static void cec_devnode_release(struct device
*cd
)
74 struct cec_devnode
*devnode
= to_cec_devnode(cd
);
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
);
81 cec_delete_adapter(to_cec_adapter(devnode
));
84 static struct bus_type cec_bus_type
= {
89 * Register a cec device node
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.
95 * Zero is returned on success.
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
101 static int __must_check
cec_devnode_register(struct cec_devnode
*devnode
,
102 struct module
*owner
)
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");
116 set_bit(minor
, cec_devnode_nums
);
117 mutex_unlock(&cec_devnode_lock
);
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
);
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
);
131 devnode
->registered
= true;
132 ret
= cdev_device_add(&devnode
->cdev
, &devnode
->dev
);
134 devnode
->registered
= false;
135 pr_err("%s: cdev_device_add failed\n", __func__
);
142 mutex_lock(&cec_devnode_lock
);
143 clear_bit(devnode
->minor
, cec_devnode_nums
);
144 mutex_unlock(&cec_devnode_lock
);
149 * Unregister a cec device node
151 * This unregisters the passed device. Future open calls will be met with
154 * This function can safely be called if the device node has never been
155 * registered or has already been unregistered.
157 static void cec_devnode_unregister(struct cec_adapter
*adap
)
159 struct cec_devnode
*devnode
= &adap
->devnode
;
162 mutex_lock(&devnode
->lock
);
164 /* Check if devnode was never registered or already unregistered */
165 if (!devnode
->registered
|| devnode
->unregistered
) {
166 mutex_unlock(&devnode
->lock
);
170 list_for_each_entry(fh
, &devnode
->fhs
, list
)
171 wake_up_interruptible(&fh
->wait
);
173 devnode
->registered
= false;
174 devnode
->unregistered
= true;
175 mutex_unlock(&devnode
->lock
);
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
);
182 cdev_device_del(&devnode
->cdev
, &devnode
->dev
);
183 put_device(&devnode
->dev
);
186 #ifdef CONFIG_DEBUG_FS
187 static ssize_t
cec_error_inj_write(struct file
*file
,
188 const char __user
*ubuf
, size_t count
, loff_t
*ppos
)
190 struct seq_file
*sf
= file
->private_data
;
191 struct cec_adapter
*adap
= sf
->private;
196 buf
= memdup_user_nul(ubuf
, min_t(size_t, PAGE_SIZE
, count
));
202 line
= strsep(&p
, "\n");
203 if (!*line
|| *line
== '#')
205 if (!adap
->ops
->error_inj_parse_line(adap
, line
)) {
214 static int cec_error_inj_show(struct seq_file
*sf
, void *unused
)
216 struct cec_adapter
*adap
= sf
->private;
218 return adap
->ops
->error_inj_show(adap
, sf
);
221 static int cec_error_inj_open(struct inode
*inode
, struct file
*file
)
223 return single_open(file
, cec_error_inj_show
, inode
->i_private
);
226 static const struct file_operations cec_error_inj_fops
= {
227 .open
= cec_error_inj_open
,
228 .write
= cec_error_inj_write
,
231 .release
= single_release
,
235 struct cec_adapter
*cec_allocate_adapter(const struct cec_adap_ops
*ops
,
236 void *priv
, const char *name
, u32 caps
,
239 struct cec_adapter
*adap
;
242 #ifndef CONFIG_MEDIA_CEC_RC
247 return ERR_PTR(-EINVAL
);
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
);
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
;
262 adap
->capabilities
|= CEC_CAP_PHYS_ADDR
;
263 adap
->needs_hpd
= caps
& CEC_CAP_NEEDS_HPD
;
264 adap
->available_log_addrs
= available_las
;
268 memset(adap
->phys_addrs
, 0xff, sizeof(adap
->phys_addrs
));
269 mutex_init(&adap
->lock
);
270 INIT_LIST_HEAD(&adap
->transmit_queue
);
271 INIT_LIST_HEAD(&adap
->wait_queue
);
272 init_waitqueue_head(&adap
->kthread_waitq
);
274 /* adap->devnode initialization */
275 INIT_LIST_HEAD(&adap
->devnode
.fhs
);
276 mutex_init(&adap
->devnode
.lock
);
278 adap
->kthread
= kthread_run(cec_thread_func
, adap
, "cec-%s", name
);
279 if (IS_ERR(adap
->kthread
)) {
280 pr_err("cec-%s: kernel_thread() failed\n", name
);
281 res
= PTR_ERR(adap
->kthread
);
286 #ifdef CONFIG_MEDIA_CEC_RC
287 if (!(caps
& CEC_CAP_RC
))
290 /* Prepare the RC input device */
291 adap
->rc
= rc_allocate_device(RC_DRIVER_SCANCODE
);
293 pr_err("cec-%s: failed to allocate memory for rc_dev\n",
295 kthread_stop(adap
->kthread
);
297 return ERR_PTR(-ENOMEM
);
300 snprintf(adap
->input_phys
, sizeof(adap
->input_phys
),
301 "%s/input0", adap
->name
);
303 adap
->rc
->device_name
= adap
->name
;
304 adap
->rc
->input_phys
= adap
->input_phys
;
305 adap
->rc
->input_id
.bustype
= BUS_CEC
;
306 adap
->rc
->input_id
.vendor
= 0;
307 adap
->rc
->input_id
.product
= 0;
308 adap
->rc
->input_id
.version
= 1;
309 adap
->rc
->driver_name
= CEC_NAME
;
310 adap
->rc
->allowed_protocols
= RC_PROTO_BIT_CEC
;
311 adap
->rc
->priv
= adap
;
312 adap
->rc
->map_name
= RC_MAP_CEC
;
313 adap
->rc
->timeout
= MS_TO_NS(550);
317 EXPORT_SYMBOL_GPL(cec_allocate_adapter
);
319 int cec_register_adapter(struct cec_adapter
*adap
,
320 struct device
*parent
)
324 if (IS_ERR_OR_NULL(adap
))
327 if (WARN_ON(!parent
))
330 adap
->owner
= parent
->driver
->owner
;
331 adap
->devnode
.dev
.parent
= parent
;
333 #ifdef CONFIG_MEDIA_CEC_RC
334 if (adap
->capabilities
& CEC_CAP_RC
) {
335 adap
->rc
->dev
.parent
= parent
;
336 res
= rc_register_device(adap
->rc
);
339 pr_err("cec-%s: failed to prepare input device\n",
341 rc_free_device(adap
->rc
);
348 res
= cec_devnode_register(&adap
->devnode
, adap
->owner
);
350 #ifdef CONFIG_MEDIA_CEC_RC
351 /* Note: rc_unregister also calls rc_free */
352 rc_unregister_device(adap
->rc
);
358 dev_set_drvdata(&adap
->devnode
.dev
, adap
);
359 #ifdef CONFIG_DEBUG_FS
363 adap
->cec_dir
= debugfs_create_dir(dev_name(&adap
->devnode
.dev
), top_cec_dir
);
364 if (IS_ERR_OR_NULL(adap
->cec_dir
)) {
365 pr_warn("cec-%s: Failed to create debugfs dir\n", adap
->name
);
368 adap
->status_file
= debugfs_create_devm_seqfile(&adap
->devnode
.dev
,
369 "status", adap
->cec_dir
, cec_adap_status
);
370 if (IS_ERR_OR_NULL(adap
->status_file
)) {
371 pr_warn("cec-%s: Failed to create status file\n", adap
->name
);
372 debugfs_remove_recursive(adap
->cec_dir
);
373 adap
->cec_dir
= NULL
;
376 if (!adap
->ops
->error_inj_show
|| !adap
->ops
->error_inj_parse_line
)
378 adap
->error_inj_file
= debugfs_create_file("error-inj", 0644,
380 &cec_error_inj_fops
);
381 if (IS_ERR_OR_NULL(adap
->error_inj_file
))
382 pr_warn("cec-%s: Failed to create error-inj file\n",
387 EXPORT_SYMBOL_GPL(cec_register_adapter
);
389 void cec_unregister_adapter(struct cec_adapter
*adap
)
391 if (IS_ERR_OR_NULL(adap
))
394 #ifdef CONFIG_MEDIA_CEC_RC
395 /* Note: rc_unregister also calls rc_free */
396 rc_unregister_device(adap
->rc
);
399 debugfs_remove_recursive(adap
->cec_dir
);
400 #ifdef CONFIG_CEC_NOTIFIER
401 cec_notifier_cec_adap_unregister(adap
->notifier
, adap
);
403 cec_devnode_unregister(adap
);
405 EXPORT_SYMBOL_GPL(cec_unregister_adapter
);
407 void cec_delete_adapter(struct cec_adapter
*adap
)
409 if (IS_ERR_OR_NULL(adap
))
411 kthread_stop(adap
->kthread
);
412 if (adap
->kthread_config
)
413 kthread_stop(adap
->kthread_config
);
414 if (adap
->ops
->adap_free
)
415 adap
->ops
->adap_free(adap
);
416 #ifdef CONFIG_MEDIA_CEC_RC
417 rc_free_device(adap
->rc
);
421 EXPORT_SYMBOL_GPL(cec_delete_adapter
);
424 * Initialise cec for linux
426 static int __init
cec_devnode_init(void)
428 int ret
= alloc_chrdev_region(&cec_dev_t
, 0, CEC_NUM_DEVICES
, CEC_NAME
);
431 pr_warn("cec: unable to allocate major\n");
435 #ifdef CONFIG_DEBUG_FS
436 top_cec_dir
= debugfs_create_dir("cec", NULL
);
437 if (IS_ERR_OR_NULL(top_cec_dir
)) {
438 pr_warn("cec: Failed to create debugfs cec dir\n");
443 ret
= bus_register(&cec_bus_type
);
445 unregister_chrdev_region(cec_dev_t
, CEC_NUM_DEVICES
);
446 pr_warn("cec: bus_register failed\n");
453 static void __exit
cec_devnode_exit(void)
455 debugfs_remove_recursive(top_cec_dir
);
456 bus_unregister(&cec_bus_type
);
457 unregister_chrdev_region(cec_dev_t
, CEC_NUM_DEVICES
);
460 subsys_initcall(cec_devnode_init
);
461 module_exit(cec_devnode_exit
)
463 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
464 MODULE_DESCRIPTION("Device node registration for cec drivers");
465 MODULE_LICENSE("GPL");