xref: /openbmc/linux/drivers/media/cec/core/cec-core.c (revision 11c416e3)
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 int cec_debug;
24 module_param_named(debug, cec_debug, int, 0644);
25 MODULE_PARM_DESC(debug, "debug level (0-2)");
26 
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");
30 
31 static dev_t cec_dev_t;
32 
33 /* Active devices */
34 static DEFINE_MUTEX(cec_devnode_lock);
35 static DECLARE_BITMAP(cec_devnode_nums, CEC_NUM_DEVICES);
36 
37 static 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 
42 int 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 
66 void 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. */
72 static 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 
84 static 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  */
101 static 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 
141 clr_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  */
157 static 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 
170 	list_for_each_entry(fh, &devnode->fhs, list)
171 		wake_up_interruptible(&fh->wait);
172 
173 	devnode->registered = false;
174 	devnode->unregistered = true;
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
187 static 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 
214 static 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 
221 static 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 
226 static 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 
235 struct 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 	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);
273 
274 	/* adap->devnode initialization */
275 	INIT_LIST_HEAD(&adap->devnode.fhs);
276 	mutex_init(&adap->devnode.lock);
277 
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);
282 		kfree(adap);
283 		return ERR_PTR(res);
284 	}
285 
286 #ifdef CONFIG_MEDIA_CEC_RC
287 	if (!(caps & CEC_CAP_RC))
288 		return adap;
289 
290 	/* Prepare the RC input device */
291 	adap->rc = rc_allocate_device(RC_DRIVER_SCANCODE);
292 	if (!adap->rc) {
293 		pr_err("cec-%s: failed to allocate memory for rc_dev\n",
294 		       name);
295 		kthread_stop(adap->kthread);
296 		kfree(adap);
297 		return ERR_PTR(-ENOMEM);
298 	}
299 
300 	snprintf(adap->input_phys, sizeof(adap->input_phys),
301 		 "%s/input0", adap->name);
302 
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);
314 #endif
315 	return adap;
316 }
317 EXPORT_SYMBOL_GPL(cec_allocate_adapter);
318 
319 int cec_register_adapter(struct cec_adapter *adap,
320 			 struct device *parent)
321 {
322 	int res;
323 
324 	if (IS_ERR_OR_NULL(adap))
325 		return 0;
326 
327 	if (WARN_ON(!parent))
328 		return -EINVAL;
329 
330 	adap->owner = parent->driver->owner;
331 	adap->devnode.dev.parent = parent;
332 
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);
337 
338 		if (res) {
339 			pr_err("cec-%s: failed to prepare input device\n",
340 			       adap->name);
341 			rc_free_device(adap->rc);
342 			adap->rc = NULL;
343 			return res;
344 		}
345 	}
346 #endif
347 
348 	res = cec_devnode_register(&adap->devnode, adap->owner);
349 	if (res) {
350 #ifdef CONFIG_MEDIA_CEC_RC
351 		/* Note: rc_unregister also calls rc_free */
352 		rc_unregister_device(adap->rc);
353 		adap->rc = NULL;
354 #endif
355 		return res;
356 	}
357 
358 	dev_set_drvdata(&adap->devnode.dev, adap);
359 #ifdef CONFIG_DEBUG_FS
360 	if (!top_cec_dir)
361 		return 0;
362 
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);
366 		return 0;
367 	}
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;
374 		return 0;
375 	}
376 	if (!adap->ops->error_inj_show || !adap->ops->error_inj_parse_line)
377 		return 0;
378 	adap->error_inj_file = debugfs_create_file("error-inj", 0644,
379 						   adap->cec_dir, adap,
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",
383 			adap->name);
384 #endif
385 	return 0;
386 }
387 EXPORT_SYMBOL_GPL(cec_register_adapter);
388 
389 void cec_unregister_adapter(struct cec_adapter *adap)
390 {
391 	if (IS_ERR_OR_NULL(adap))
392 		return;
393 
394 #ifdef CONFIG_MEDIA_CEC_RC
395 	/* Note: rc_unregister also calls rc_free */
396 	rc_unregister_device(adap->rc);
397 	adap->rc = NULL;
398 #endif
399 	debugfs_remove_recursive(adap->cec_dir);
400 #ifdef CONFIG_CEC_NOTIFIER
401 	cec_notifier_cec_adap_unregister(adap->notifier, adap);
402 #endif
403 	cec_devnode_unregister(adap);
404 }
405 EXPORT_SYMBOL_GPL(cec_unregister_adapter);
406 
407 void cec_delete_adapter(struct cec_adapter *adap)
408 {
409 	if (IS_ERR_OR_NULL(adap))
410 		return;
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);
418 #endif
419 	kfree(adap);
420 }
421 EXPORT_SYMBOL_GPL(cec_delete_adapter);
422 
423 /*
424  *	Initialise cec for linux
425  */
426 static int __init cec_devnode_init(void)
427 {
428 	int ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES, CEC_NAME);
429 
430 	if (ret < 0) {
431 		pr_warn("cec: unable to allocate major\n");
432 		return ret;
433 	}
434 
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");
439 		top_cec_dir = NULL;
440 	}
441 #endif
442 
443 	ret = bus_register(&cec_bus_type);
444 	if (ret < 0) {
445 		unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
446 		pr_warn("cec: bus_register failed\n");
447 		return -EIO;
448 	}
449 
450 	return 0;
451 }
452 
453 static void __exit cec_devnode_exit(void)
454 {
455 	debugfs_remove_recursive(top_cec_dir);
456 	bus_unregister(&cec_bus_type);
457 	unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
458 }
459 
460 subsys_initcall(cec_devnode_init);
461 module_exit(cec_devnode_exit)
462 
463 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
464 MODULE_DESCRIPTION("Device node registration for cec drivers");
465 MODULE_LICENSE("GPL");
466