xref: /openbmc/linux/drivers/media/cec/core/cec-core.c (revision 2f0754f2)
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_first_zero_bit(cec_devnode_nums, CEC_NUM_DEVICES);
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 	devnode->registered = false;
170 	devnode->unregistered = true;
171 
172 	mutex_lock(&devnode->lock_fhs);
173 	list_for_each_entry(fh, &devnode->fhs, list)
174 		wake_up_interruptible(&fh->wait);
175 	mutex_unlock(&devnode->lock_fhs);
176 
177 	mutex_unlock(&devnode->lock);
178 
179 	mutex_lock(&adap->lock);
180 	__cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
181 	__cec_s_log_addrs(adap, NULL, false);
182 	mutex_unlock(&adap->lock);
183 
184 	cdev_device_del(&devnode->cdev, &devnode->dev);
185 	put_device(&devnode->dev);
186 }
187 
188 #ifdef CONFIG_DEBUG_FS
189 static ssize_t cec_error_inj_write(struct file *file,
190 	const char __user *ubuf, size_t count, loff_t *ppos)
191 {
192 	struct seq_file *sf = file->private_data;
193 	struct cec_adapter *adap = sf->private;
194 	char *buf;
195 	char *line;
196 	char *p;
197 
198 	buf = memdup_user_nul(ubuf, min_t(size_t, PAGE_SIZE, count));
199 	if (IS_ERR(buf))
200 		return PTR_ERR(buf);
201 	p = buf;
202 	while (p && *p) {
203 		p = skip_spaces(p);
204 		line = strsep(&p, "\n");
205 		if (!*line || *line == '#')
206 			continue;
207 		if (!adap->ops->error_inj_parse_line(adap, line)) {
208 			kfree(buf);
209 			return -EINVAL;
210 		}
211 	}
212 	kfree(buf);
213 	return count;
214 }
215 
216 static int cec_error_inj_show(struct seq_file *sf, void *unused)
217 {
218 	struct cec_adapter *adap = sf->private;
219 
220 	return adap->ops->error_inj_show(adap, sf);
221 }
222 
223 static int cec_error_inj_open(struct inode *inode, struct file *file)
224 {
225 	return single_open(file, cec_error_inj_show, inode->i_private);
226 }
227 
228 static const struct file_operations cec_error_inj_fops = {
229 	.open = cec_error_inj_open,
230 	.write = cec_error_inj_write,
231 	.read = seq_read,
232 	.llseek = seq_lseek,
233 	.release = single_release,
234 };
235 #endif
236 
237 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
238 					 void *priv, const char *name, u32 caps,
239 					 u8 available_las)
240 {
241 	struct cec_adapter *adap;
242 	int res;
243 
244 #ifndef CONFIG_MEDIA_CEC_RC
245 	caps &= ~CEC_CAP_RC;
246 #endif
247 
248 	if (WARN_ON(!caps))
249 		return ERR_PTR(-EINVAL);
250 	if (WARN_ON(!ops))
251 		return ERR_PTR(-EINVAL);
252 	if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS))
253 		return ERR_PTR(-EINVAL);
254 	adap = kzalloc(sizeof(*adap), GFP_KERNEL);
255 	if (!adap)
256 		return ERR_PTR(-ENOMEM);
257 	strscpy(adap->name, name, sizeof(adap->name));
258 	adap->phys_addr = CEC_PHYS_ADDR_INVALID;
259 	adap->cec_pin_is_high = true;
260 	adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
261 	adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
262 	adap->capabilities = caps;
263 	if (debug_phys_addr)
264 		adap->capabilities |= CEC_CAP_PHYS_ADDR;
265 	adap->needs_hpd = caps & CEC_CAP_NEEDS_HPD;
266 	adap->available_log_addrs = available_las;
267 	adap->sequence = 0;
268 	adap->ops = ops;
269 	adap->priv = priv;
270 	mutex_init(&adap->lock);
271 	INIT_LIST_HEAD(&adap->transmit_queue);
272 	INIT_LIST_HEAD(&adap->wait_queue);
273 	init_waitqueue_head(&adap->kthread_waitq);
274 
275 	/* adap->devnode initialization */
276 	INIT_LIST_HEAD(&adap->devnode.fhs);
277 	mutex_init(&adap->devnode.lock_fhs);
278 	mutex_init(&adap->devnode.lock);
279 
280 	adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
281 	if (IS_ERR(adap->kthread)) {
282 		pr_err("cec-%s: kernel_thread() failed\n", name);
283 		res = PTR_ERR(adap->kthread);
284 		kfree(adap);
285 		return ERR_PTR(res);
286 	}
287 
288 #ifdef CONFIG_MEDIA_CEC_RC
289 	if (!(caps & CEC_CAP_RC))
290 		return adap;
291 
292 	/* Prepare the RC input device */
293 	adap->rc = rc_allocate_device(RC_DRIVER_SCANCODE);
294 	if (!adap->rc) {
295 		pr_err("cec-%s: failed to allocate memory for rc_dev\n",
296 		       name);
297 		kthread_stop(adap->kthread);
298 		kfree(adap);
299 		return ERR_PTR(-ENOMEM);
300 	}
301 
302 	snprintf(adap->input_phys, sizeof(adap->input_phys),
303 		 "%s/input0", adap->name);
304 
305 	adap->rc->device_name = adap->name;
306 	adap->rc->input_phys = adap->input_phys;
307 	adap->rc->input_id.bustype = BUS_CEC;
308 	adap->rc->input_id.vendor = 0;
309 	adap->rc->input_id.product = 0;
310 	adap->rc->input_id.version = 1;
311 	adap->rc->driver_name = CEC_NAME;
312 	adap->rc->allowed_protocols = RC_PROTO_BIT_CEC;
313 	adap->rc->priv = adap;
314 	adap->rc->map_name = RC_MAP_CEC;
315 	adap->rc->timeout = MS_TO_US(550);
316 #endif
317 	return adap;
318 }
319 EXPORT_SYMBOL_GPL(cec_allocate_adapter);
320 
321 int cec_register_adapter(struct cec_adapter *adap,
322 			 struct device *parent)
323 {
324 	int res;
325 
326 	if (IS_ERR_OR_NULL(adap))
327 		return 0;
328 
329 	if (WARN_ON(!parent))
330 		return -EINVAL;
331 
332 	adap->owner = parent->driver->owner;
333 	adap->devnode.dev.parent = parent;
334 
335 #ifdef CONFIG_MEDIA_CEC_RC
336 	if (adap->capabilities & CEC_CAP_RC) {
337 		adap->rc->dev.parent = parent;
338 		res = rc_register_device(adap->rc);
339 
340 		if (res) {
341 			pr_err("cec-%s: failed to prepare input device\n",
342 			       adap->name);
343 			rc_free_device(adap->rc);
344 			adap->rc = NULL;
345 			return res;
346 		}
347 	}
348 #endif
349 
350 	res = cec_devnode_register(&adap->devnode, adap->owner);
351 	if (res) {
352 #ifdef CONFIG_MEDIA_CEC_RC
353 		/* Note: rc_unregister also calls rc_free */
354 		rc_unregister_device(adap->rc);
355 		adap->rc = NULL;
356 #endif
357 		return res;
358 	}
359 
360 	dev_set_drvdata(&adap->devnode.dev, adap);
361 #ifdef CONFIG_DEBUG_FS
362 	if (!top_cec_dir)
363 		return 0;
364 
365 	adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev),
366 					   top_cec_dir);
367 
368 	debugfs_create_devm_seqfile(&adap->devnode.dev, "status", adap->cec_dir,
369 				    cec_adap_status);
370 
371 	if (!adap->ops->error_inj_show || !adap->ops->error_inj_parse_line)
372 		return 0;
373 	debugfs_create_file("error-inj", 0644, adap->cec_dir, adap,
374 			    &cec_error_inj_fops);
375 #endif
376 	return 0;
377 }
378 EXPORT_SYMBOL_GPL(cec_register_adapter);
379 
380 void cec_unregister_adapter(struct cec_adapter *adap)
381 {
382 	if (IS_ERR_OR_NULL(adap))
383 		return;
384 
385 #ifdef CONFIG_MEDIA_CEC_RC
386 	/* Note: rc_unregister also calls rc_free */
387 	rc_unregister_device(adap->rc);
388 	adap->rc = NULL;
389 #endif
390 	debugfs_remove_recursive(adap->cec_dir);
391 #ifdef CONFIG_CEC_NOTIFIER
392 	cec_notifier_cec_adap_unregister(adap->notifier, adap);
393 #endif
394 	cec_devnode_unregister(adap);
395 }
396 EXPORT_SYMBOL_GPL(cec_unregister_adapter);
397 
398 void cec_delete_adapter(struct cec_adapter *adap)
399 {
400 	if (IS_ERR_OR_NULL(adap))
401 		return;
402 	if (adap->kthread_config)
403 		kthread_stop(adap->kthread_config);
404 	kthread_stop(adap->kthread);
405 	if (adap->ops->adap_free)
406 		adap->ops->adap_free(adap);
407 #ifdef CONFIG_MEDIA_CEC_RC
408 	rc_free_device(adap->rc);
409 #endif
410 	kfree(adap);
411 }
412 EXPORT_SYMBOL_GPL(cec_delete_adapter);
413 
414 /*
415  *	Initialise cec for linux
416  */
417 static int __init cec_devnode_init(void)
418 {
419 	int ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES, CEC_NAME);
420 
421 	if (ret < 0) {
422 		pr_warn("cec: unable to allocate major\n");
423 		return ret;
424 	}
425 
426 #ifdef CONFIG_DEBUG_FS
427 	top_cec_dir = debugfs_create_dir("cec", NULL);
428 	if (IS_ERR_OR_NULL(top_cec_dir)) {
429 		pr_warn("cec: Failed to create debugfs cec dir\n");
430 		top_cec_dir = NULL;
431 	}
432 #endif
433 
434 	ret = bus_register(&cec_bus_type);
435 	if (ret < 0) {
436 		unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
437 		pr_warn("cec: bus_register failed\n");
438 		return -EIO;
439 	}
440 
441 	return 0;
442 }
443 
444 static void __exit cec_devnode_exit(void)
445 {
446 	debugfs_remove_recursive(top_cec_dir);
447 	bus_unregister(&cec_bus_type);
448 	unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
449 }
450 
451 subsys_initcall(cec_devnode_init);
452 module_exit(cec_devnode_exit)
453 
454 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
455 MODULE_DESCRIPTION("Device node registration for cec drivers");
456 MODULE_LICENSE("GPL");
457