blob: 6b32a288714f5eadd8652f04f58c87bf07439f6e [file] [log] [blame]
Hans Verkuila56960e2016-06-25 09:43:58 -03001/*
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
35int cec_debug;
36module_param_named(debug, cec_debug, int, 0644);
37MODULE_PARM_DESC(debug, "debug level (0-2)");
38
39static dev_t cec_dev_t;
40
41/* Active devices */
42static DEFINE_MUTEX(cec_devnode_lock);
43static DECLARE_BITMAP(cec_devnode_nums, CEC_NUM_DEVICES);
44
45static 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
50int cec_get_device(struct cec_devnode *devnode)
51{
52 /*
53 * Check if the cec device is available. This needs to be done with
Hans Verkuil2ab25d32016-08-02 08:13:57 -030054 * the devnode->lock held to prevent an open/unregister race:
Hans Verkuila56960e2016-06-25 09:43:58 -030055 * 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 */
Hans Verkuil2ab25d32016-08-02 08:13:57 -030059 mutex_lock(&devnode->lock);
Hans Verkuila56960e2016-06-25 09:43:58 -030060 /*
61 * return ENXIO if the cec device has been removed
62 * already or if it is not registered anymore.
63 */
64 if (!devnode->registered) {
Hans Verkuil2ab25d32016-08-02 08:13:57 -030065 mutex_unlock(&devnode->lock);
Hans Verkuila56960e2016-06-25 09:43:58 -030066 return -ENXIO;
67 }
68 /* and increase the device refcount */
69 get_device(&devnode->dev);
Hans Verkuil2ab25d32016-08-02 08:13:57 -030070 mutex_unlock(&devnode->lock);
Hans Verkuila56960e2016-06-25 09:43:58 -030071 return 0;
72}
73
74void cec_put_device(struct cec_devnode *devnode)
75{
Hans Verkuila56960e2016-06-25 09:43:58 -030076 put_device(&devnode->dev);
Hans Verkuila56960e2016-06-25 09:43:58 -030077}
78
79/* Called when the last user of the cec device exits. */
80static void cec_devnode_release(struct device *cd)
81{
82 struct cec_devnode *devnode = to_cec_devnode(cd);
83
84 mutex_lock(&cec_devnode_lock);
Hans Verkuila56960e2016-06-25 09:43:58 -030085 /* Mark device node number as free */
86 clear_bit(devnode->minor, cec_devnode_nums);
Hans Verkuila56960e2016-06-25 09:43:58 -030087 mutex_unlock(&cec_devnode_lock);
Hans Verkuil2ab25d32016-08-02 08:13:57 -030088
Hans Verkuila56960e2016-06-25 09:43:58 -030089 cec_delete_adapter(to_cec_adapter(devnode));
90}
91
92static 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 */
109static 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);
Hans Verkuil62148f02016-08-02 08:11:00 -0300117 mutex_init(&devnode->lock);
Hans Verkuila56960e2016-06-25 09:43:58 -0300118
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;
Hans Verkuila56960e2016-06-25 09:43:58 -0300135 dev_set_name(&devnode->dev, "cec%d", devnode->minor);
136 device_initialize(&devnode->dev);
137
138 /* Part 2: Initialize and register the character device */
139 cdev_init(&devnode->cdev, &cec_devnode_fops);
140 devnode->cdev.kobj.parent = &devnode->dev.kobj;
141 devnode->cdev.owner = owner;
142
143 ret = cdev_add(&devnode->cdev, devnode->dev.devt, 1);
144 if (ret < 0) {
145 pr_err("%s: cdev_add failed\n", __func__);
146 goto clr_bit;
147 }
148
149 ret = device_add(&devnode->dev);
150 if (ret)
151 goto cdev_del;
152
153 devnode->registered = true;
154 return 0;
155
156cdev_del:
157 cdev_del(&devnode->cdev);
158clr_bit:
Hans Verkuil2ab25d32016-08-02 08:13:57 -0300159 mutex_lock(&cec_devnode_lock);
Hans Verkuila56960e2016-06-25 09:43:58 -0300160 clear_bit(devnode->minor, cec_devnode_nums);
Hans Verkuil2ab25d32016-08-02 08:13:57 -0300161 mutex_unlock(&cec_devnode_lock);
Hans Verkuila56960e2016-06-25 09:43:58 -0300162 return ret;
163}
164
165/*
166 * Unregister a cec device node
167 *
168 * This unregisters the passed device. Future open calls will be met with
169 * errors.
170 *
171 * This function can safely be called if the device node has never been
172 * registered or has already been unregistered.
173 */
174static void cec_devnode_unregister(struct cec_devnode *devnode)
175{
176 struct cec_fh *fh;
177
Hans Verkuil62148f02016-08-02 08:11:00 -0300178 mutex_lock(&devnode->lock);
Hans Verkuila56960e2016-06-25 09:43:58 -0300179
Hans Verkuil2ab25d32016-08-02 08:13:57 -0300180 /* Check if devnode was never registered or already unregistered */
181 if (!devnode->registered || devnode->unregistered) {
182 mutex_unlock(&devnode->lock);
183 return;
184 }
185
Hans Verkuila56960e2016-06-25 09:43:58 -0300186 list_for_each_entry(fh, &devnode->fhs, list)
187 wake_up_interruptible(&fh->wait);
Hans Verkuila56960e2016-06-25 09:43:58 -0300188
189 devnode->registered = false;
190 devnode->unregistered = true;
Hans Verkuil2ab25d32016-08-02 08:13:57 -0300191 mutex_unlock(&devnode->lock);
192
Hans Verkuila56960e2016-06-25 09:43:58 -0300193 device_del(&devnode->dev);
194 cdev_del(&devnode->cdev);
195 put_device(&devnode->dev);
196}
197
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200198#ifdef CONFIG_MEDIA_CEC_NOTIFIER
199static void cec_cec_notify(struct cec_adapter *adap, u16 pa)
200{
201 cec_s_phys_addr(adap, pa, false);
202}
203
204void cec_register_cec_notifier(struct cec_adapter *adap,
205 struct cec_notifier *notifier)
206{
207 if (WARN_ON(!adap->devnode.registered))
208 return;
209
210 adap->notifier = notifier;
211 cec_notifier_register(adap->notifier, adap, cec_cec_notify);
212}
213EXPORT_SYMBOL_GPL(cec_register_cec_notifier);
214#endif
215
Hans Verkuila56960e2016-06-25 09:43:58 -0300216struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
217 void *priv, const char *name, u32 caps,
Hans Verkuilf51e8082016-11-25 06:23:34 -0200218 u8 available_las)
Hans Verkuila56960e2016-06-25 09:43:58 -0300219{
220 struct cec_adapter *adap;
221 int res;
222
Hans Verkuila56960e2016-06-25 09:43:58 -0300223 if (WARN_ON(!caps))
224 return ERR_PTR(-EINVAL);
225 if (WARN_ON(!ops))
226 return ERR_PTR(-EINVAL);
227 if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS))
228 return ERR_PTR(-EINVAL);
229 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
230 if (!adap)
231 return ERR_PTR(-ENOMEM);
Hans Verkuila56960e2016-06-25 09:43:58 -0300232 strlcpy(adap->name, name, sizeof(adap->name));
233 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
234 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
235 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
236 adap->capabilities = caps;
237 adap->available_log_addrs = available_las;
238 adap->sequence = 0;
239 adap->ops = ops;
240 adap->priv = priv;
241 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
242 mutex_init(&adap->lock);
243 INIT_LIST_HEAD(&adap->transmit_queue);
244 INIT_LIST_HEAD(&adap->wait_queue);
245 init_waitqueue_head(&adap->kthread_waitq);
246
247 adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
248 if (IS_ERR(adap->kthread)) {
249 pr_err("cec-%s: kernel_thread() failed\n", name);
250 res = PTR_ERR(adap->kthread);
251 kfree(adap);
252 return ERR_PTR(res);
253 }
254
255 if (!(caps & CEC_CAP_RC))
256 return adap;
257
Hans Verkuil5bb23992016-07-01 07:33:10 -0300258#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300259 /* Prepare the RC input device */
Andi Shyti0f7499f2016-12-16 06:50:58 -0200260 adap->rc = rc_allocate_device(RC_DRIVER_SCANCODE);
Hans Verkuila56960e2016-06-25 09:43:58 -0300261 if (!adap->rc) {
262 pr_err("cec-%s: failed to allocate memory for rc_dev\n",
263 name);
264 kthread_stop(adap->kthread);
265 kfree(adap);
266 return ERR_PTR(-ENOMEM);
267 }
268
269 snprintf(adap->input_name, sizeof(adap->input_name),
270 "RC for %s", name);
271 snprintf(adap->input_phys, sizeof(adap->input_phys),
272 "%s/input0", name);
273
274 adap->rc->input_name = adap->input_name;
275 adap->rc->input_phys = adap->input_phys;
276 adap->rc->input_id.bustype = BUS_CEC;
277 adap->rc->input_id.vendor = 0;
278 adap->rc->input_id.product = 0;
279 adap->rc->input_id.version = 1;
Hans Verkuila56960e2016-06-25 09:43:58 -0300280 adap->rc->driver_name = CEC_NAME;
281 adap->rc->allowed_protocols = RC_BIT_CEC;
282 adap->rc->priv = adap;
283 adap->rc->map_name = RC_MAP_CEC;
284 adap->rc->timeout = MS_TO_NS(100);
285#else
286 adap->capabilities &= ~CEC_CAP_RC;
287#endif
288 return adap;
289}
290EXPORT_SYMBOL_GPL(cec_allocate_adapter);
291
Hans Verkuilf51e8082016-11-25 06:23:34 -0200292int cec_register_adapter(struct cec_adapter *adap,
293 struct device *parent)
Hans Verkuila56960e2016-06-25 09:43:58 -0300294{
295 int res;
296
297 if (IS_ERR_OR_NULL(adap))
298 return 0;
299
Hans Verkuilf51e8082016-11-25 06:23:34 -0200300 if (WARN_ON(!parent))
301 return -EINVAL;
302
303 adap->owner = parent->driver->owner;
304 adap->devnode.dev.parent = parent;
305
Hans Verkuil5bb23992016-07-01 07:33:10 -0300306#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300307 if (adap->capabilities & CEC_CAP_RC) {
Lee Jones43c0c032017-04-04 09:32:19 -0300308 adap->rc->dev.parent = parent;
Hans Verkuila56960e2016-06-25 09:43:58 -0300309 res = rc_register_device(adap->rc);
310
311 if (res) {
312 pr_err("cec-%s: failed to prepare input device\n",
313 adap->name);
314 rc_free_device(adap->rc);
315 adap->rc = NULL;
316 return res;
317 }
318 }
319#endif
320
321 res = cec_devnode_register(&adap->devnode, adap->owner);
322 if (res) {
Hans Verkuil5bb23992016-07-01 07:33:10 -0300323#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300324 /* Note: rc_unregister also calls rc_free */
325 rc_unregister_device(adap->rc);
326 adap->rc = NULL;
327#endif
328 return res;
329 }
330
331 dev_set_drvdata(&adap->devnode.dev, adap);
332#ifdef CONFIG_MEDIA_CEC_DEBUG
333 if (!top_cec_dir)
334 return 0;
335
336 adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev), top_cec_dir);
337 if (IS_ERR_OR_NULL(adap->cec_dir)) {
338 pr_warn("cec-%s: Failed to create debugfs dir\n", adap->name);
339 return 0;
340 }
341 adap->status_file = debugfs_create_devm_seqfile(&adap->devnode.dev,
342 "status", adap->cec_dir, cec_adap_status);
343 if (IS_ERR_OR_NULL(adap->status_file)) {
344 pr_warn("cec-%s: Failed to create status file\n", adap->name);
345 debugfs_remove_recursive(adap->cec_dir);
346 adap->cec_dir = NULL;
347 }
348#endif
349 return 0;
350}
351EXPORT_SYMBOL_GPL(cec_register_adapter);
352
353void cec_unregister_adapter(struct cec_adapter *adap)
354{
355 if (IS_ERR_OR_NULL(adap))
356 return;
357
Hans Verkuil5bb23992016-07-01 07:33:10 -0300358#if IS_REACHABLE(CONFIG_RC_CORE)
Hans Verkuila56960e2016-06-25 09:43:58 -0300359 /* Note: rc_unregister also calls rc_free */
360 rc_unregister_device(adap->rc);
361 adap->rc = NULL;
362#endif
363 debugfs_remove_recursive(adap->cec_dir);
Hans Verkuile3a93ad2016-12-13 12:15:57 -0200364#ifdef CONFIG_MEDIA_CEC_NOTIFIER
365 if (adap->notifier)
366 cec_notifier_unregister(adap->notifier);
367#endif
Hans Verkuila56960e2016-06-25 09:43:58 -0300368 cec_devnode_unregister(&adap->devnode);
369}
370EXPORT_SYMBOL_GPL(cec_unregister_adapter);
371
372void cec_delete_adapter(struct cec_adapter *adap)
373{
374 if (IS_ERR_OR_NULL(adap))
375 return;
376 mutex_lock(&adap->lock);
377 __cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
378 mutex_unlock(&adap->lock);
379 kthread_stop(adap->kthread);
380 if (adap->kthread_config)
381 kthread_stop(adap->kthread_config);
Hans Verkuil5bb23992016-07-01 07:33:10 -0300382#if IS_REACHABLE(CONFIG_RC_CORE)
Markus Elfringd8eddb12016-07-17 17:52:49 -0300383 rc_free_device(adap->rc);
Hans Verkuila56960e2016-06-25 09:43:58 -0300384#endif
385 kfree(adap);
386}
387EXPORT_SYMBOL_GPL(cec_delete_adapter);
388
389/*
390 * Initialise cec for linux
391 */
392static int __init cec_devnode_init(void)
393{
394 int ret;
395
396 pr_info("Linux cec interface: v0.10\n");
397 ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES,
398 CEC_NAME);
399 if (ret < 0) {
400 pr_warn("cec: unable to allocate major\n");
401 return ret;
402 }
403
404#ifdef CONFIG_MEDIA_CEC_DEBUG
405 top_cec_dir = debugfs_create_dir("cec", NULL);
406 if (IS_ERR_OR_NULL(top_cec_dir)) {
407 pr_warn("cec: Failed to create debugfs cec dir\n");
408 top_cec_dir = NULL;
409 }
410#endif
411
412 ret = bus_register(&cec_bus_type);
413 if (ret < 0) {
414 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
415 pr_warn("cec: bus_register failed\n");
416 return -EIO;
417 }
418
419 return 0;
420}
421
422static void __exit cec_devnode_exit(void)
423{
424 debugfs_remove_recursive(top_cec_dir);
425 bus_unregister(&cec_bus_type);
426 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
427}
428
429subsys_initcall(cec_devnode_init);
430module_exit(cec_devnode_exit)
431
432MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
433MODULE_DESCRIPTION("Device node registration for cec drivers");
434MODULE_LICENSE("GPL");