perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / cec / cec-core.c
blobb278ab90b38715023ea286c79f35cdfa47c94532
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * cec-core.c - HDMI Consumer Electronics Control framework - Core
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
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>
18 #include "cec-priv.h"
20 #define CEC_NUM_DEVICES 256
21 #define CEC_NAME "cec"
23 int cec_debug;
24 module_param_named(debug, cec_debug, int, 0644);
25 MODULE_PARM_DESC(debug, "debug level (0-2)");
27 static dev_t cec_dev_t;
29 /* Active devices */
30 static DEFINE_MUTEX(cec_devnode_lock);
31 static DECLARE_BITMAP(cec_devnode_nums, CEC_NUM_DEVICES);
33 static struct dentry *top_cec_dir;
35 /* dev to cec_devnode */
36 #define to_cec_devnode(cd) container_of(cd, struct cec_devnode, dev)
38 int cec_get_device(struct cec_devnode *devnode)
41 * Check if the cec device is available. This needs to be done with
42 * the devnode->lock held to prevent an open/unregister race:
43 * without the lock, the device could be unregistered and freed between
44 * the devnode->registered check and get_device() calls, leading to
45 * a crash.
47 mutex_lock(&devnode->lock);
49 * return ENXIO if the cec device has been removed
50 * already or if it is not registered anymore.
52 if (!devnode->registered) {
53 mutex_unlock(&devnode->lock);
54 return -ENXIO;
56 /* and increase the device refcount */
57 get_device(&devnode->dev);
58 mutex_unlock(&devnode->lock);
59 return 0;
62 void cec_put_device(struct cec_devnode *devnode)
64 put_device(&devnode->dev);
67 /* Called when the last user of the cec device exits. */
68 static void cec_devnode_release(struct device *cd)
70 struct cec_devnode *devnode = to_cec_devnode(cd);
72 mutex_lock(&cec_devnode_lock);
73 /* Mark device node number as free */
74 clear_bit(devnode->minor, cec_devnode_nums);
75 mutex_unlock(&cec_devnode_lock);
77 cec_delete_adapter(to_cec_adapter(devnode));
80 static struct bus_type cec_bus_type = {
81 .name = CEC_NAME,
85 * Register a cec device node
87 * The registration code assigns minor numbers and registers the new device node
88 * with the kernel. An error is returned if no free minor number can be found,
89 * or if the registration of the device node fails.
91 * Zero is returned on success.
93 * Note that if the cec_devnode_register call fails, the release() callback of
94 * the cec_devnode structure is *not* called, so the caller is responsible for
95 * freeing any data.
97 static int __must_check cec_devnode_register(struct cec_devnode *devnode,
98 struct module *owner)
100 int minor;
101 int ret;
103 /* Part 1: Find a free minor number */
104 mutex_lock(&cec_devnode_lock);
105 minor = find_next_zero_bit(cec_devnode_nums, CEC_NUM_DEVICES, 0);
106 if (minor == CEC_NUM_DEVICES) {
107 mutex_unlock(&cec_devnode_lock);
108 pr_err("could not get a free minor\n");
109 return -ENFILE;
112 set_bit(minor, cec_devnode_nums);
113 mutex_unlock(&cec_devnode_lock);
115 devnode->minor = minor;
116 devnode->dev.bus = &cec_bus_type;
117 devnode->dev.devt = MKDEV(MAJOR(cec_dev_t), minor);
118 devnode->dev.release = cec_devnode_release;
119 dev_set_name(&devnode->dev, "cec%d", devnode->minor);
120 device_initialize(&devnode->dev);
122 /* Part 2: Initialize and register the character device */
123 cdev_init(&devnode->cdev, &cec_devnode_fops);
124 devnode->cdev.owner = owner;
126 ret = cdev_device_add(&devnode->cdev, &devnode->dev);
127 if (ret) {
128 pr_err("%s: cdev_device_add failed\n", __func__);
129 goto clr_bit;
132 devnode->registered = true;
133 return 0;
135 clr_bit:
136 mutex_lock(&cec_devnode_lock);
137 clear_bit(devnode->minor, cec_devnode_nums);
138 mutex_unlock(&cec_devnode_lock);
139 return ret;
143 * Unregister a cec device node
145 * This unregisters the passed device. Future open calls will be met with
146 * errors.
148 * This function can safely be called if the device node has never been
149 * registered or has already been unregistered.
151 static void cec_devnode_unregister(struct cec_adapter *adap)
153 struct cec_devnode *devnode = &adap->devnode;
154 struct cec_fh *fh;
156 mutex_lock(&devnode->lock);
158 /* Check if devnode was never registered or already unregistered */
159 if (!devnode->registered || devnode->unregistered) {
160 mutex_unlock(&devnode->lock);
161 return;
164 list_for_each_entry(fh, &devnode->fhs, list)
165 wake_up_interruptible(&fh->wait);
167 devnode->registered = false;
168 devnode->unregistered = true;
169 mutex_unlock(&devnode->lock);
171 mutex_lock(&adap->lock);
172 __cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
173 __cec_s_log_addrs(adap, NULL, false);
174 mutex_unlock(&adap->lock);
176 cdev_device_del(&devnode->cdev, &devnode->dev);
177 put_device(&devnode->dev);
180 #ifdef CONFIG_CEC_NOTIFIER
181 static void cec_cec_notify(struct cec_adapter *adap, u16 pa)
183 cec_s_phys_addr(adap, pa, false);
186 void cec_register_cec_notifier(struct cec_adapter *adap,
187 struct cec_notifier *notifier)
189 if (WARN_ON(!cec_is_registered(adap)))
190 return;
192 adap->notifier = notifier;
193 cec_notifier_register(adap->notifier, adap, cec_cec_notify);
195 EXPORT_SYMBOL_GPL(cec_register_cec_notifier);
196 #endif
198 #ifdef CONFIG_DEBUG_FS
199 static ssize_t cec_error_inj_write(struct file *file,
200 const char __user *ubuf, size_t count, loff_t *ppos)
202 struct seq_file *sf = file->private_data;
203 struct cec_adapter *adap = sf->private;
204 char *buf;
205 char *line;
206 char *p;
208 buf = memdup_user_nul(ubuf, min_t(size_t, PAGE_SIZE, count));
209 if (IS_ERR(buf))
210 return PTR_ERR(buf);
211 p = buf;
212 while (p && *p) {
213 p = skip_spaces(p);
214 line = strsep(&p, "\n");
215 if (!*line || *line == '#')
216 continue;
217 if (!adap->ops->error_inj_parse_line(adap, line)) {
218 kfree(buf);
219 return -EINVAL;
222 kfree(buf);
223 return count;
226 static int cec_error_inj_show(struct seq_file *sf, void *unused)
228 struct cec_adapter *adap = sf->private;
230 return adap->ops->error_inj_show(adap, sf);
233 static int cec_error_inj_open(struct inode *inode, struct file *file)
235 return single_open(file, cec_error_inj_show, inode->i_private);
238 static const struct file_operations cec_error_inj_fops = {
239 .open = cec_error_inj_open,
240 .write = cec_error_inj_write,
241 .read = seq_read,
242 .llseek = seq_lseek,
243 .release = single_release,
245 #endif
247 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
248 void *priv, const char *name, u32 caps,
249 u8 available_las)
251 struct cec_adapter *adap;
252 int res;
254 #ifndef CONFIG_MEDIA_CEC_RC
255 caps &= ~CEC_CAP_RC;
256 #endif
258 if (WARN_ON(!caps))
259 return ERR_PTR(-EINVAL);
260 if (WARN_ON(!ops))
261 return ERR_PTR(-EINVAL);
262 if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS))
263 return ERR_PTR(-EINVAL);
264 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
265 if (!adap)
266 return ERR_PTR(-ENOMEM);
267 strlcpy(adap->name, name, sizeof(adap->name));
268 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
269 adap->cec_pin_is_high = true;
270 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
271 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
272 adap->capabilities = caps;
273 adap->needs_hpd = caps & CEC_CAP_NEEDS_HPD;
274 adap->available_log_addrs = available_las;
275 adap->sequence = 0;
276 adap->ops = ops;
277 adap->priv = priv;
278 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
279 mutex_init(&adap->lock);
280 INIT_LIST_HEAD(&adap->transmit_queue);
281 INIT_LIST_HEAD(&adap->wait_queue);
282 init_waitqueue_head(&adap->kthread_waitq);
284 /* adap->devnode initialization */
285 INIT_LIST_HEAD(&adap->devnode.fhs);
286 mutex_init(&adap->devnode.lock);
288 adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
289 if (IS_ERR(adap->kthread)) {
290 pr_err("cec-%s: kernel_thread() failed\n", name);
291 res = PTR_ERR(adap->kthread);
292 kfree(adap);
293 return ERR_PTR(res);
296 #ifdef CONFIG_MEDIA_CEC_RC
297 if (!(caps & CEC_CAP_RC))
298 return adap;
300 /* Prepare the RC input device */
301 adap->rc = rc_allocate_device(RC_DRIVER_SCANCODE);
302 if (!adap->rc) {
303 pr_err("cec-%s: failed to allocate memory for rc_dev\n",
304 name);
305 kthread_stop(adap->kthread);
306 kfree(adap);
307 return ERR_PTR(-ENOMEM);
310 snprintf(adap->device_name, sizeof(adap->device_name),
311 "RC for %s", name);
312 snprintf(adap->input_phys, sizeof(adap->input_phys),
313 "%s/input0", name);
315 adap->rc->device_name = adap->device_name;
316 adap->rc->input_phys = adap->input_phys;
317 adap->rc->input_id.bustype = BUS_CEC;
318 adap->rc->input_id.vendor = 0;
319 adap->rc->input_id.product = 0;
320 adap->rc->input_id.version = 1;
321 adap->rc->driver_name = CEC_NAME;
322 adap->rc->allowed_protocols = RC_PROTO_BIT_CEC;
323 adap->rc->priv = adap;
324 adap->rc->map_name = RC_MAP_CEC;
325 adap->rc->timeout = MS_TO_NS(550);
326 #endif
327 return adap;
329 EXPORT_SYMBOL_GPL(cec_allocate_adapter);
331 int cec_register_adapter(struct cec_adapter *adap,
332 struct device *parent)
334 int res;
336 if (IS_ERR_OR_NULL(adap))
337 return 0;
339 if (WARN_ON(!parent))
340 return -EINVAL;
342 adap->owner = parent->driver->owner;
343 adap->devnode.dev.parent = parent;
345 #ifdef CONFIG_MEDIA_CEC_RC
346 if (adap->capabilities & CEC_CAP_RC) {
347 adap->rc->dev.parent = parent;
348 res = rc_register_device(adap->rc);
350 if (res) {
351 pr_err("cec-%s: failed to prepare input device\n",
352 adap->name);
353 rc_free_device(adap->rc);
354 adap->rc = NULL;
355 return res;
358 #endif
360 res = cec_devnode_register(&adap->devnode, adap->owner);
361 if (res) {
362 #ifdef CONFIG_MEDIA_CEC_RC
363 /* Note: rc_unregister also calls rc_free */
364 rc_unregister_device(adap->rc);
365 adap->rc = NULL;
366 #endif
367 return res;
370 dev_set_drvdata(&adap->devnode.dev, adap);
371 #ifdef CONFIG_DEBUG_FS
372 if (!top_cec_dir)
373 return 0;
375 adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev), top_cec_dir);
376 if (IS_ERR_OR_NULL(adap->cec_dir)) {
377 pr_warn("cec-%s: Failed to create debugfs dir\n", adap->name);
378 return 0;
380 adap->status_file = debugfs_create_devm_seqfile(&adap->devnode.dev,
381 "status", adap->cec_dir, cec_adap_status);
382 if (IS_ERR_OR_NULL(adap->status_file)) {
383 pr_warn("cec-%s: Failed to create status file\n", adap->name);
384 debugfs_remove_recursive(adap->cec_dir);
385 adap->cec_dir = NULL;
386 return 0;
388 if (!adap->ops->error_inj_show || !adap->ops->error_inj_parse_line)
389 return 0;
390 adap->error_inj_file = debugfs_create_file("error-inj", 0644,
391 adap->cec_dir, adap,
392 &cec_error_inj_fops);
393 if (IS_ERR_OR_NULL(adap->error_inj_file))
394 pr_warn("cec-%s: Failed to create error-inj file\n",
395 adap->name);
396 #endif
397 return 0;
399 EXPORT_SYMBOL_GPL(cec_register_adapter);
401 void cec_unregister_adapter(struct cec_adapter *adap)
403 if (IS_ERR_OR_NULL(adap))
404 return;
406 #ifdef CONFIG_MEDIA_CEC_RC
407 /* Note: rc_unregister also calls rc_free */
408 rc_unregister_device(adap->rc);
409 adap->rc = NULL;
410 #endif
411 debugfs_remove_recursive(adap->cec_dir);
412 #ifdef CONFIG_CEC_NOTIFIER
413 if (adap->notifier)
414 cec_notifier_unregister(adap->notifier);
415 #endif
416 cec_devnode_unregister(adap);
418 EXPORT_SYMBOL_GPL(cec_unregister_adapter);
420 void cec_delete_adapter(struct cec_adapter *adap)
422 if (IS_ERR_OR_NULL(adap))
423 return;
424 kthread_stop(adap->kthread);
425 if (adap->kthread_config)
426 kthread_stop(adap->kthread_config);
427 if (adap->ops->adap_free)
428 adap->ops->adap_free(adap);
429 #ifdef CONFIG_MEDIA_CEC_RC
430 rc_free_device(adap->rc);
431 #endif
432 kfree(adap);
434 EXPORT_SYMBOL_GPL(cec_delete_adapter);
437 * Initialise cec for linux
439 static int __init cec_devnode_init(void)
441 int ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES, CEC_NAME);
443 if (ret < 0) {
444 pr_warn("cec: unable to allocate major\n");
445 return ret;
448 #ifdef CONFIG_DEBUG_FS
449 top_cec_dir = debugfs_create_dir("cec", NULL);
450 if (IS_ERR_OR_NULL(top_cec_dir)) {
451 pr_warn("cec: Failed to create debugfs cec dir\n");
452 top_cec_dir = NULL;
454 #endif
456 ret = bus_register(&cec_bus_type);
457 if (ret < 0) {
458 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
459 pr_warn("cec: bus_register failed\n");
460 return -EIO;
463 return 0;
466 static void __exit cec_devnode_exit(void)
468 debugfs_remove_recursive(top_cec_dir);
469 bus_unregister(&cec_bus_type);
470 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
473 subsys_initcall(cec_devnode_init);
474 module_exit(cec_devnode_exit)
476 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
477 MODULE_DESCRIPTION("Device node registration for cec drivers");
478 MODULE_LICENSE("GPL");