• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * cec-core.c - HDMI Consumer Electronics Control framework - Core
3  *
4  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you may redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17  * SOFTWARE.
18  */
19 
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/kmod.h>
25 #include <linux/slab.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 
30 #include "cec-priv.h"
31 
32 #define CEC_NUM_DEVICES	256
33 #define CEC_NAME	"cec"
34 
35 int cec_debug;
36 module_param_named(debug, cec_debug, int, 0644);
37 MODULE_PARM_DESC(debug, "debug level (0-2)");
38 
39 static dev_t cec_dev_t;
40 
41 /* Active devices */
42 static DEFINE_MUTEX(cec_devnode_lock);
43 static DECLARE_BITMAP(cec_devnode_nums, CEC_NUM_DEVICES);
44 
45 static struct dentry *top_cec_dir;
46 
47 /* dev to cec_devnode */
48 #define to_cec_devnode(cd) container_of(cd, struct cec_devnode, dev)
49 
cec_get_device(struct cec_devnode * devnode)50 int cec_get_device(struct cec_devnode *devnode)
51 {
52 	/*
53 	 * Check if the cec device is available. This needs to be done with
54 	 * the devnode->lock held to prevent an open/unregister race:
55 	 * without the lock, the device could be unregistered and freed between
56 	 * the devnode->registered check and get_device() calls, leading to
57 	 * a crash.
58 	 */
59 	mutex_lock(&devnode->lock);
60 	/*
61 	 * return ENXIO if the cec device has been removed
62 	 * already or if it is not registered anymore.
63 	 */
64 	if (!devnode->registered) {
65 		mutex_unlock(&devnode->lock);
66 		return -ENXIO;
67 	}
68 	/* and increase the device refcount */
69 	get_device(&devnode->dev);
70 	mutex_unlock(&devnode->lock);
71 	return 0;
72 }
73 
cec_put_device(struct cec_devnode * devnode)74 void cec_put_device(struct cec_devnode *devnode)
75 {
76 	put_device(&devnode->dev);
77 }
78 
79 /* Called when the last user of the cec device exits. */
cec_devnode_release(struct device * cd)80 static void cec_devnode_release(struct device *cd)
81 {
82 	struct cec_devnode *devnode = to_cec_devnode(cd);
83 
84 	mutex_lock(&cec_devnode_lock);
85 	/* Mark device node number as free */
86 	clear_bit(devnode->minor, cec_devnode_nums);
87 	mutex_unlock(&cec_devnode_lock);
88 
89 	cec_delete_adapter(to_cec_adapter(devnode));
90 }
91 
92 static struct bus_type cec_bus_type = {
93 	.name = CEC_NAME,
94 };
95 
96 /*
97  * Register a cec device node
98  *
99  * The registration code assigns minor numbers and registers the new device node
100  * with the kernel. An error is returned if no free minor number can be found,
101  * or if the registration of the device node fails.
102  *
103  * Zero is returned on success.
104  *
105  * Note that if the cec_devnode_register call fails, the release() callback of
106  * the cec_devnode structure is *not* called, so the caller is responsible for
107  * freeing any data.
108  */
cec_devnode_register(struct cec_devnode * devnode,struct module * owner)109 static int __must_check cec_devnode_register(struct cec_devnode *devnode,
110 					     struct module *owner)
111 {
112 	int minor;
113 	int ret;
114 
115 	/* Initialization */
116 	INIT_LIST_HEAD(&devnode->fhs);
117 	mutex_init(&devnode->lock);
118 
119 	/* Part 1: Find a free minor number */
120 	mutex_lock(&cec_devnode_lock);
121 	minor = find_next_zero_bit(cec_devnode_nums, CEC_NUM_DEVICES, 0);
122 	if (minor == CEC_NUM_DEVICES) {
123 		mutex_unlock(&cec_devnode_lock);
124 		pr_err("could not get a free minor\n");
125 		return -ENFILE;
126 	}
127 
128 	set_bit(minor, cec_devnode_nums);
129 	mutex_unlock(&cec_devnode_lock);
130 
131 	devnode->minor = minor;
132 	devnode->dev.bus = &cec_bus_type;
133 	devnode->dev.devt = MKDEV(MAJOR(cec_dev_t), minor);
134 	devnode->dev.release = cec_devnode_release;
135 	devnode->dev.parent = devnode->parent;
136 	dev_set_name(&devnode->dev, "cec%d", devnode->minor);
137 	device_initialize(&devnode->dev);
138 
139 	/* Part 2: Initialize and register the character device */
140 	cdev_init(&devnode->cdev, &cec_devnode_fops);
141 	devnode->cdev.kobj.parent = &devnode->dev.kobj;
142 	devnode->cdev.owner = owner;
143 
144 	ret = cdev_add(&devnode->cdev, devnode->dev.devt, 1);
145 	if (ret < 0) {
146 		pr_err("%s: cdev_add failed\n", __func__);
147 		goto clr_bit;
148 	}
149 
150 	ret = device_add(&devnode->dev);
151 	if (ret)
152 		goto cdev_del;
153 
154 	devnode->registered = true;
155 	return 0;
156 
157 cdev_del:
158 	cdev_del(&devnode->cdev);
159 clr_bit:
160 	mutex_lock(&cec_devnode_lock);
161 	clear_bit(devnode->minor, cec_devnode_nums);
162 	mutex_unlock(&cec_devnode_lock);
163 	return ret;
164 }
165 
166 /*
167  * Unregister a cec device node
168  *
169  * This unregisters the passed device. Future open calls will be met with
170  * errors.
171  *
172  * This function can safely be called if the device node has never been
173  * registered or has already been unregistered.
174  */
cec_devnode_unregister(struct cec_devnode * devnode)175 static void cec_devnode_unregister(struct cec_devnode *devnode)
176 {
177 	struct cec_fh *fh;
178 
179 	mutex_lock(&devnode->lock);
180 
181 	/* Check if devnode was never registered or already unregistered */
182 	if (!devnode->registered || devnode->unregistered) {
183 		mutex_unlock(&devnode->lock);
184 		return;
185 	}
186 
187 	list_for_each_entry(fh, &devnode->fhs, list)
188 		wake_up_interruptible(&fh->wait);
189 
190 	devnode->registered = false;
191 	devnode->unregistered = true;
192 	mutex_unlock(&devnode->lock);
193 
194 	device_del(&devnode->dev);
195 	cdev_del(&devnode->cdev);
196 	put_device(&devnode->dev);
197 }
198 
cec_allocate_adapter(const struct cec_adap_ops * ops,void * priv,const char * name,u32 caps,u8 available_las,struct device * parent)199 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
200 					 void *priv, const char *name, u32 caps,
201 					 u8 available_las, struct device *parent)
202 {
203 	struct cec_adapter *adap;
204 	int res;
205 
206 	if (WARN_ON(!parent))
207 		return ERR_PTR(-EINVAL);
208 	if (WARN_ON(!caps))
209 		return ERR_PTR(-EINVAL);
210 	if (WARN_ON(!ops))
211 		return ERR_PTR(-EINVAL);
212 	if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS))
213 		return ERR_PTR(-EINVAL);
214 	adap = kzalloc(sizeof(*adap), GFP_KERNEL);
215 	if (!adap)
216 		return ERR_PTR(-ENOMEM);
217 	adap->owner = parent->driver->owner;
218 	adap->devnode.parent = parent;
219 	strlcpy(adap->name, name, sizeof(adap->name));
220 	adap->phys_addr = CEC_PHYS_ADDR_INVALID;
221 	adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
222 	adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
223 	adap->capabilities = caps;
224 	adap->available_log_addrs = available_las;
225 	adap->sequence = 0;
226 	adap->ops = ops;
227 	adap->priv = priv;
228 	memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
229 	mutex_init(&adap->lock);
230 	INIT_LIST_HEAD(&adap->transmit_queue);
231 	INIT_LIST_HEAD(&adap->wait_queue);
232 	init_waitqueue_head(&adap->kthread_waitq);
233 
234 	adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
235 	if (IS_ERR(adap->kthread)) {
236 		pr_err("cec-%s: kernel_thread() failed\n", name);
237 		res = PTR_ERR(adap->kthread);
238 		kfree(adap);
239 		return ERR_PTR(res);
240 	}
241 
242 	if (!(caps & CEC_CAP_RC))
243 		return adap;
244 
245 #if IS_REACHABLE(CONFIG_RC_CORE)
246 	/* Prepare the RC input device */
247 	adap->rc = rc_allocate_device();
248 	if (!adap->rc) {
249 		pr_err("cec-%s: failed to allocate memory for rc_dev\n",
250 		       name);
251 		kthread_stop(adap->kthread);
252 		kfree(adap);
253 		return ERR_PTR(-ENOMEM);
254 	}
255 
256 	snprintf(adap->input_name, sizeof(adap->input_name),
257 		 "RC for %s", name);
258 	snprintf(adap->input_phys, sizeof(adap->input_phys),
259 		 "%s/input0", name);
260 
261 	adap->rc->input_name = adap->input_name;
262 	adap->rc->input_phys = adap->input_phys;
263 	adap->rc->input_id.bustype = BUS_CEC;
264 	adap->rc->input_id.vendor = 0;
265 	adap->rc->input_id.product = 0;
266 	adap->rc->input_id.version = 1;
267 	adap->rc->dev.parent = parent;
268 	adap->rc->driver_type = RC_DRIVER_SCANCODE;
269 	adap->rc->driver_name = CEC_NAME;
270 	adap->rc->allowed_protocols = RC_BIT_CEC;
271 	adap->rc->priv = adap;
272 	adap->rc->map_name = RC_MAP_CEC;
273 	adap->rc->timeout = MS_TO_NS(100);
274 #else
275 	adap->capabilities &= ~CEC_CAP_RC;
276 #endif
277 	return adap;
278 }
279 EXPORT_SYMBOL_GPL(cec_allocate_adapter);
280 
cec_register_adapter(struct cec_adapter * adap)281 int cec_register_adapter(struct cec_adapter *adap)
282 {
283 	int res;
284 
285 	if (IS_ERR_OR_NULL(adap))
286 		return 0;
287 
288 #if IS_REACHABLE(CONFIG_RC_CORE)
289 	if (adap->capabilities & CEC_CAP_RC) {
290 		res = rc_register_device(adap->rc);
291 
292 		if (res) {
293 			pr_err("cec-%s: failed to prepare input device\n",
294 			       adap->name);
295 			rc_free_device(adap->rc);
296 			adap->rc = NULL;
297 			return res;
298 		}
299 	}
300 #endif
301 
302 	res = cec_devnode_register(&adap->devnode, adap->owner);
303 	if (res) {
304 #if IS_REACHABLE(CONFIG_RC_CORE)
305 		/* Note: rc_unregister also calls rc_free */
306 		rc_unregister_device(adap->rc);
307 		adap->rc = NULL;
308 #endif
309 		return res;
310 	}
311 
312 	dev_set_drvdata(&adap->devnode.dev, adap);
313 #ifdef CONFIG_MEDIA_CEC_DEBUG
314 	if (!top_cec_dir)
315 		return 0;
316 
317 	adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev), top_cec_dir);
318 	if (IS_ERR_OR_NULL(adap->cec_dir)) {
319 		pr_warn("cec-%s: Failed to create debugfs dir\n", adap->name);
320 		return 0;
321 	}
322 	adap->status_file = debugfs_create_devm_seqfile(&adap->devnode.dev,
323 		"status", adap->cec_dir, cec_adap_status);
324 	if (IS_ERR_OR_NULL(adap->status_file)) {
325 		pr_warn("cec-%s: Failed to create status file\n", adap->name);
326 		debugfs_remove_recursive(adap->cec_dir);
327 		adap->cec_dir = NULL;
328 	}
329 #endif
330 	return 0;
331 }
332 EXPORT_SYMBOL_GPL(cec_register_adapter);
333 
cec_unregister_adapter(struct cec_adapter * adap)334 void cec_unregister_adapter(struct cec_adapter *adap)
335 {
336 	if (IS_ERR_OR_NULL(adap))
337 		return;
338 
339 #if IS_REACHABLE(CONFIG_RC_CORE)
340 	/* Note: rc_unregister also calls rc_free */
341 	rc_unregister_device(adap->rc);
342 	adap->rc = NULL;
343 #endif
344 	debugfs_remove_recursive(adap->cec_dir);
345 	cec_devnode_unregister(&adap->devnode);
346 }
347 EXPORT_SYMBOL_GPL(cec_unregister_adapter);
348 
cec_delete_adapter(struct cec_adapter * adap)349 void cec_delete_adapter(struct cec_adapter *adap)
350 {
351 	if (IS_ERR_OR_NULL(adap))
352 		return;
353 	mutex_lock(&adap->lock);
354 	__cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
355 	mutex_unlock(&adap->lock);
356 	kthread_stop(adap->kthread);
357 	if (adap->kthread_config)
358 		kthread_stop(adap->kthread_config);
359 #if IS_REACHABLE(CONFIG_RC_CORE)
360 	rc_free_device(adap->rc);
361 #endif
362 	kfree(adap);
363 }
364 EXPORT_SYMBOL_GPL(cec_delete_adapter);
365 
366 /*
367  *	Initialise cec for linux
368  */
cec_devnode_init(void)369 static int __init cec_devnode_init(void)
370 {
371 	int ret;
372 
373 	pr_info("Linux cec interface: v0.10\n");
374 	ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES,
375 				  CEC_NAME);
376 	if (ret < 0) {
377 		pr_warn("cec: unable to allocate major\n");
378 		return ret;
379 	}
380 
381 #ifdef CONFIG_MEDIA_CEC_DEBUG
382 	top_cec_dir = debugfs_create_dir("cec", NULL);
383 	if (IS_ERR_OR_NULL(top_cec_dir)) {
384 		pr_warn("cec: Failed to create debugfs cec dir\n");
385 		top_cec_dir = NULL;
386 	}
387 #endif
388 
389 	ret = bus_register(&cec_bus_type);
390 	if (ret < 0) {
391 		unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
392 		pr_warn("cec: bus_register failed\n");
393 		return -EIO;
394 	}
395 
396 	return 0;
397 }
398 
cec_devnode_exit(void)399 static void __exit cec_devnode_exit(void)
400 {
401 	debugfs_remove_recursive(top_cec_dir);
402 	bus_unregister(&cec_bus_type);
403 	unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
404 }
405 
406 subsys_initcall(cec_devnode_init);
407 module_exit(cec_devnode_exit)
408 
409 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
410 MODULE_DESCRIPTION("Device node registration for cec drivers");
411 MODULE_LICENSE("GPL");
412