2 * cec-core.c - HDMI Consumer Electronics Control framework - Core
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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
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>
27 #include <linux/string.h>
28 #include <linux/types.h>
32 #define CEC_NUM_DEVICES 256
33 #define CEC_NAME "cec"
36 module_param_named(debug
, cec_debug
, int, 0644);
37 MODULE_PARM_DESC(debug
, "debug level (0-2)");
39 static dev_t cec_dev_t
;
42 static DEFINE_MUTEX(cec_devnode_lock
);
43 static DECLARE_BITMAP(cec_devnode_nums
, CEC_NUM_DEVICES
);
45 static struct dentry
*top_cec_dir
;
47 /* dev to cec_devnode */
48 #define to_cec_devnode(cd) container_of(cd, struct cec_devnode, dev)
50 int cec_get_device(struct cec_devnode
*devnode
)
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
59 mutex_lock(&devnode
->lock
);
61 * return ENXIO if the cec device has been removed
62 * already or if it is not registered anymore.
64 if (!devnode
->registered
) {
65 mutex_unlock(&devnode
->lock
);
68 /* and increase the device refcount */
69 get_device(&devnode
->dev
);
70 mutex_unlock(&devnode
->lock
);
74 void cec_put_device(struct cec_devnode
*devnode
)
76 put_device(&devnode
->dev
);
79 /* Called when the last user of the cec device exits. */
80 static void cec_devnode_release(struct device
*cd
)
82 struct cec_devnode
*devnode
= to_cec_devnode(cd
);
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
);
89 cec_delete_adapter(to_cec_adapter(devnode
));
92 static struct bus_type cec_bus_type
= {
97 * Register a cec device node
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.
103 * Zero is returned on success.
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
109 static int __must_check
cec_devnode_register(struct cec_devnode
*devnode
,
110 struct module
*owner
)
115 /* Part 1: Find a free minor number */
116 mutex_lock(&cec_devnode_lock
);
117 minor
= find_next_zero_bit(cec_devnode_nums
, CEC_NUM_DEVICES
, 0);
118 if (minor
== CEC_NUM_DEVICES
) {
119 mutex_unlock(&cec_devnode_lock
);
120 pr_err("could not get a free minor\n");
124 set_bit(minor
, cec_devnode_nums
);
125 mutex_unlock(&cec_devnode_lock
);
127 devnode
->minor
= minor
;
128 devnode
->dev
.bus
= &cec_bus_type
;
129 devnode
->dev
.devt
= MKDEV(MAJOR(cec_dev_t
), minor
);
130 devnode
->dev
.release
= cec_devnode_release
;
131 dev_set_name(&devnode
->dev
, "cec%d", devnode
->minor
);
132 device_initialize(&devnode
->dev
);
134 /* Part 2: Initialize and register the character device */
135 cdev_init(&devnode
->cdev
, &cec_devnode_fops
);
136 devnode
->cdev
.owner
= owner
;
138 ret
= cdev_device_add(&devnode
->cdev
, &devnode
->dev
);
140 pr_err("%s: cdev_device_add failed\n", __func__
);
144 devnode
->registered
= true;
148 mutex_lock(&cec_devnode_lock
);
149 clear_bit(devnode
->minor
, cec_devnode_nums
);
150 mutex_unlock(&cec_devnode_lock
);
155 * Unregister a cec device node
157 * This unregisters the passed device. Future open calls will be met with
160 * This function can safely be called if the device node has never been
161 * registered or has already been unregistered.
163 static void cec_devnode_unregister(struct cec_adapter
*adap
)
165 struct cec_devnode
*devnode
= &adap
->devnode
;
168 mutex_lock(&devnode
->lock
);
170 /* Check if devnode was never registered or already unregistered */
171 if (!devnode
->registered
|| devnode
->unregistered
) {
172 mutex_unlock(&devnode
->lock
);
176 list_for_each_entry(fh
, &devnode
->fhs
, list
)
177 wake_up_interruptible(&fh
->wait
);
179 devnode
->registered
= false;
180 devnode
->unregistered
= true;
181 mutex_unlock(&devnode
->lock
);
183 mutex_lock(&adap
->lock
);
184 __cec_s_phys_addr(adap
, CEC_PHYS_ADDR_INVALID
, false);
185 __cec_s_log_addrs(adap
, NULL
, false);
186 mutex_unlock(&adap
->lock
);
188 cdev_device_del(&devnode
->cdev
, &devnode
->dev
);
189 put_device(&devnode
->dev
);
192 #ifdef CONFIG_CEC_NOTIFIER
193 static void cec_cec_notify(struct cec_adapter
*adap
, u16 pa
)
195 cec_s_phys_addr(adap
, pa
, false);
198 void cec_register_cec_notifier(struct cec_adapter
*adap
,
199 struct cec_notifier
*notifier
)
201 if (WARN_ON(!cec_is_registered(adap
)))
204 adap
->notifier
= notifier
;
205 cec_notifier_register(adap
->notifier
, adap
, cec_cec_notify
);
207 EXPORT_SYMBOL_GPL(cec_register_cec_notifier
);
210 struct cec_adapter
*cec_allocate_adapter(const struct cec_adap_ops
*ops
,
211 void *priv
, const char *name
, u32 caps
,
214 struct cec_adapter
*adap
;
217 #ifndef CONFIG_MEDIA_CEC_RC
222 return ERR_PTR(-EINVAL
);
224 return ERR_PTR(-EINVAL
);
225 if (WARN_ON(!available_las
|| available_las
> CEC_MAX_LOG_ADDRS
))
226 return ERR_PTR(-EINVAL
);
227 adap
= kzalloc(sizeof(*adap
), GFP_KERNEL
);
229 return ERR_PTR(-ENOMEM
);
230 strlcpy(adap
->name
, name
, sizeof(adap
->name
));
231 adap
->phys_addr
= CEC_PHYS_ADDR_INVALID
;
232 adap
->cec_pin_is_high
= true;
233 adap
->log_addrs
.cec_version
= CEC_OP_CEC_VERSION_2_0
;
234 adap
->log_addrs
.vendor_id
= CEC_VENDOR_ID_NONE
;
235 adap
->capabilities
= caps
;
236 adap
->needs_hpd
= caps
& CEC_CAP_NEEDS_HPD
;
237 adap
->available_log_addrs
= available_las
;
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
);
247 /* adap->devnode initialization */
248 INIT_LIST_HEAD(&adap
->devnode
.fhs
);
249 mutex_init(&adap
->devnode
.lock
);
251 adap
->kthread
= kthread_run(cec_thread_func
, adap
, "cec-%s", name
);
252 if (IS_ERR(adap
->kthread
)) {
253 pr_err("cec-%s: kernel_thread() failed\n", name
);
254 res
= PTR_ERR(adap
->kthread
);
259 #ifdef CONFIG_MEDIA_CEC_RC
260 if (!(caps
& CEC_CAP_RC
))
263 /* Prepare the RC input device */
264 adap
->rc
= rc_allocate_device(RC_DRIVER_SCANCODE
);
266 pr_err("cec-%s: failed to allocate memory for rc_dev\n",
268 kthread_stop(adap
->kthread
);
270 return ERR_PTR(-ENOMEM
);
273 snprintf(adap
->device_name
, sizeof(adap
->device_name
),
275 snprintf(adap
->input_phys
, sizeof(adap
->input_phys
),
278 adap
->rc
->device_name
= adap
->device_name
;
279 adap
->rc
->input_phys
= adap
->input_phys
;
280 adap
->rc
->input_id
.bustype
= BUS_CEC
;
281 adap
->rc
->input_id
.vendor
= 0;
282 adap
->rc
->input_id
.product
= 0;
283 adap
->rc
->input_id
.version
= 1;
284 adap
->rc
->driver_name
= CEC_NAME
;
285 adap
->rc
->allowed_protocols
= RC_PROTO_BIT_CEC
;
286 adap
->rc
->priv
= adap
;
287 adap
->rc
->map_name
= RC_MAP_CEC
;
288 adap
->rc
->timeout
= MS_TO_NS(100);
292 EXPORT_SYMBOL_GPL(cec_allocate_adapter
);
294 int cec_register_adapter(struct cec_adapter
*adap
,
295 struct device
*parent
)
299 if (IS_ERR_OR_NULL(adap
))
302 if (WARN_ON(!parent
))
305 adap
->owner
= parent
->driver
->owner
;
306 adap
->devnode
.dev
.parent
= parent
;
308 #ifdef CONFIG_MEDIA_CEC_RC
309 if (adap
->capabilities
& CEC_CAP_RC
) {
310 adap
->rc
->dev
.parent
= parent
;
311 res
= rc_register_device(adap
->rc
);
314 pr_err("cec-%s: failed to prepare input device\n",
316 rc_free_device(adap
->rc
);
323 res
= cec_devnode_register(&adap
->devnode
, adap
->owner
);
325 #ifdef CONFIG_MEDIA_CEC_RC
326 /* Note: rc_unregister also calls rc_free */
327 rc_unregister_device(adap
->rc
);
333 dev_set_drvdata(&adap
->devnode
.dev
, adap
);
334 #ifdef CONFIG_DEBUG_FS
338 adap
->cec_dir
= debugfs_create_dir(dev_name(&adap
->devnode
.dev
), top_cec_dir
);
339 if (IS_ERR_OR_NULL(adap
->cec_dir
)) {
340 pr_warn("cec-%s: Failed to create debugfs dir\n", adap
->name
);
343 adap
->status_file
= debugfs_create_devm_seqfile(&adap
->devnode
.dev
,
344 "status", adap
->cec_dir
, cec_adap_status
);
345 if (IS_ERR_OR_NULL(adap
->status_file
)) {
346 pr_warn("cec-%s: Failed to create status file\n", adap
->name
);
347 debugfs_remove_recursive(adap
->cec_dir
);
348 adap
->cec_dir
= NULL
;
353 EXPORT_SYMBOL_GPL(cec_register_adapter
);
355 void cec_unregister_adapter(struct cec_adapter
*adap
)
357 if (IS_ERR_OR_NULL(adap
))
360 #ifdef CONFIG_MEDIA_CEC_RC
361 /* Note: rc_unregister also calls rc_free */
362 rc_unregister_device(adap
->rc
);
365 debugfs_remove_recursive(adap
->cec_dir
);
366 #ifdef CONFIG_CEC_NOTIFIER
368 cec_notifier_unregister(adap
->notifier
);
370 cec_devnode_unregister(adap
);
372 EXPORT_SYMBOL_GPL(cec_unregister_adapter
);
374 void cec_delete_adapter(struct cec_adapter
*adap
)
376 if (IS_ERR_OR_NULL(adap
))
378 kthread_stop(adap
->kthread
);
379 if (adap
->kthread_config
)
380 kthread_stop(adap
->kthread_config
);
381 if (adap
->ops
->adap_free
)
382 adap
->ops
->adap_free(adap
);
383 #ifdef CONFIG_MEDIA_CEC_RC
384 rc_free_device(adap
->rc
);
388 EXPORT_SYMBOL_GPL(cec_delete_adapter
);
391 * Initialise cec for linux
393 static int __init
cec_devnode_init(void)
395 int ret
= alloc_chrdev_region(&cec_dev_t
, 0, CEC_NUM_DEVICES
, CEC_NAME
);
398 pr_warn("cec: unable to allocate major\n");
402 #ifdef CONFIG_DEBUG_FS
403 top_cec_dir
= debugfs_create_dir("cec", NULL
);
404 if (IS_ERR_OR_NULL(top_cec_dir
)) {
405 pr_warn("cec: Failed to create debugfs cec dir\n");
410 ret
= bus_register(&cec_bus_type
);
412 unregister_chrdev_region(cec_dev_t
, CEC_NUM_DEVICES
);
413 pr_warn("cec: bus_register failed\n");
420 static void __exit
cec_devnode_exit(void)
422 debugfs_remove_recursive(top_cec_dir
);
423 bus_unregister(&cec_bus_type
);
424 unregister_chrdev_region(cec_dev_t
, CEC_NUM_DEVICES
);
427 subsys_initcall(cec_devnode_init
);
428 module_exit(cec_devnode_exit
)
430 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
431 MODULE_DESCRIPTION("Device node registration for cec drivers");
432 MODULE_LICENSE("GPL");