sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / cec / cec-core.c
blobaca3ab83a8a142af696443857dcc1a8882689ac6
1 /*
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
17 * SOFTWARE.
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>
30 #include "cec-priv.h"
32 #define CEC_NUM_DEVICES 256
33 #define CEC_NAME "cec"
35 int cec_debug;
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;
41 /* Active devices */
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
57 * a crash.
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);
66 return -ENXIO;
68 /* and increase the device refcount */
69 get_device(&devnode->dev);
70 mutex_unlock(&devnode->lock);
71 return 0;
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 = {
93 .name = CEC_NAME,
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
107 * freeing any data.
109 static int __must_check cec_devnode_register(struct cec_devnode *devnode,
110 struct module *owner)
112 int minor;
113 int ret;
115 /* Initialization */
116 INIT_LIST_HEAD(&devnode->fhs);
117 mutex_init(&devnode->lock);
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;
128 set_bit(minor, cec_devnode_nums);
129 mutex_unlock(&cec_devnode_lock);
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 dev_set_name(&devnode->dev, "cec%d", devnode->minor);
136 device_initialize(&devnode->dev);
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;
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;
149 ret = device_add(&devnode->dev);
150 if (ret)
151 goto cdev_del;
153 devnode->registered = true;
154 return 0;
156 cdev_del:
157 cdev_del(&devnode->cdev);
158 clr_bit:
159 mutex_lock(&cec_devnode_lock);
160 clear_bit(devnode->minor, cec_devnode_nums);
161 mutex_unlock(&cec_devnode_lock);
162 return ret;
166 * Unregister a cec device node
168 * This unregisters the passed device. Future open calls will be met with
169 * errors.
171 * This function can safely be called if the device node has never been
172 * registered or has already been unregistered.
174 static void cec_devnode_unregister(struct cec_devnode *devnode)
176 struct cec_fh *fh;
178 mutex_lock(&devnode->lock);
180 /* Check if devnode was never registered or already unregistered */
181 if (!devnode->registered || devnode->unregistered) {
182 mutex_unlock(&devnode->lock);
183 return;
186 list_for_each_entry(fh, &devnode->fhs, list)
187 wake_up_interruptible(&fh->wait);
189 devnode->registered = false;
190 devnode->unregistered = true;
191 mutex_unlock(&devnode->lock);
193 device_del(&devnode->dev);
194 cdev_del(&devnode->cdev);
195 put_device(&devnode->dev);
198 struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
199 void *priv, const char *name, u32 caps,
200 u8 available_las)
202 struct cec_adapter *adap;
203 int res;
205 if (WARN_ON(!caps))
206 return ERR_PTR(-EINVAL);
207 if (WARN_ON(!ops))
208 return ERR_PTR(-EINVAL);
209 if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS))
210 return ERR_PTR(-EINVAL);
211 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
212 if (!adap)
213 return ERR_PTR(-ENOMEM);
214 strlcpy(adap->name, name, sizeof(adap->name));
215 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
216 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
217 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
218 adap->capabilities = caps;
219 adap->available_log_addrs = available_las;
220 adap->sequence = 0;
221 adap->ops = ops;
222 adap->priv = priv;
223 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
224 mutex_init(&adap->lock);
225 INIT_LIST_HEAD(&adap->transmit_queue);
226 INIT_LIST_HEAD(&adap->wait_queue);
227 init_waitqueue_head(&adap->kthread_waitq);
229 adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
230 if (IS_ERR(adap->kthread)) {
231 pr_err("cec-%s: kernel_thread() failed\n", name);
232 res = PTR_ERR(adap->kthread);
233 kfree(adap);
234 return ERR_PTR(res);
237 if (!(caps & CEC_CAP_RC))
238 return adap;
240 #if IS_REACHABLE(CONFIG_RC_CORE)
241 /* Prepare the RC input device */
242 adap->rc = rc_allocate_device();
243 if (!adap->rc) {
244 pr_err("cec-%s: failed to allocate memory for rc_dev\n",
245 name);
246 kthread_stop(adap->kthread);
247 kfree(adap);
248 return ERR_PTR(-ENOMEM);
251 snprintf(adap->input_name, sizeof(adap->input_name),
252 "RC for %s", name);
253 snprintf(adap->input_phys, sizeof(adap->input_phys),
254 "%s/input0", name);
256 adap->rc->input_name = adap->input_name;
257 adap->rc->input_phys = adap->input_phys;
258 adap->rc->input_id.bustype = BUS_CEC;
259 adap->rc->input_id.vendor = 0;
260 adap->rc->input_id.product = 0;
261 adap->rc->input_id.version = 1;
262 adap->rc->driver_type = RC_DRIVER_SCANCODE;
263 adap->rc->driver_name = CEC_NAME;
264 adap->rc->allowed_protocols = RC_BIT_CEC;
265 adap->rc->priv = adap;
266 adap->rc->map_name = RC_MAP_CEC;
267 adap->rc->timeout = MS_TO_NS(100);
268 #else
269 adap->capabilities &= ~CEC_CAP_RC;
270 #endif
271 return adap;
273 EXPORT_SYMBOL_GPL(cec_allocate_adapter);
275 int cec_register_adapter(struct cec_adapter *adap,
276 struct device *parent)
278 int res;
280 if (IS_ERR_OR_NULL(adap))
281 return 0;
283 if (WARN_ON(!parent))
284 return -EINVAL;
286 adap->owner = parent->driver->owner;
287 adap->devnode.dev.parent = parent;
289 #if IS_REACHABLE(CONFIG_RC_CORE)
290 adap->rc->dev.parent = parent;
291 if (adap->capabilities & CEC_CAP_RC) {
292 res = rc_register_device(adap->rc);
294 if (res) {
295 pr_err("cec-%s: failed to prepare input device\n",
296 adap->name);
297 rc_free_device(adap->rc);
298 adap->rc = NULL;
299 return res;
302 #endif
304 res = cec_devnode_register(&adap->devnode, adap->owner);
305 if (res) {
306 #if IS_REACHABLE(CONFIG_RC_CORE)
307 /* Note: rc_unregister also calls rc_free */
308 rc_unregister_device(adap->rc);
309 adap->rc = NULL;
310 #endif
311 return res;
314 dev_set_drvdata(&adap->devnode.dev, adap);
315 #ifdef CONFIG_MEDIA_CEC_DEBUG
316 if (!top_cec_dir)
317 return 0;
319 adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev), top_cec_dir);
320 if (IS_ERR_OR_NULL(adap->cec_dir)) {
321 pr_warn("cec-%s: Failed to create debugfs dir\n", adap->name);
322 return 0;
324 adap->status_file = debugfs_create_devm_seqfile(&adap->devnode.dev,
325 "status", adap->cec_dir, cec_adap_status);
326 if (IS_ERR_OR_NULL(adap->status_file)) {
327 pr_warn("cec-%s: Failed to create status file\n", adap->name);
328 debugfs_remove_recursive(adap->cec_dir);
329 adap->cec_dir = NULL;
331 #endif
332 return 0;
334 EXPORT_SYMBOL_GPL(cec_register_adapter);
336 void cec_unregister_adapter(struct cec_adapter *adap)
338 if (IS_ERR_OR_NULL(adap))
339 return;
341 #if IS_REACHABLE(CONFIG_RC_CORE)
342 /* Note: rc_unregister also calls rc_free */
343 rc_unregister_device(adap->rc);
344 adap->rc = NULL;
345 #endif
346 debugfs_remove_recursive(adap->cec_dir);
347 cec_devnode_unregister(&adap->devnode);
349 EXPORT_SYMBOL_GPL(cec_unregister_adapter);
351 void cec_delete_adapter(struct cec_adapter *adap)
353 if (IS_ERR_OR_NULL(adap))
354 return;
355 mutex_lock(&adap->lock);
356 __cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
357 mutex_unlock(&adap->lock);
358 kthread_stop(adap->kthread);
359 if (adap->kthread_config)
360 kthread_stop(adap->kthread_config);
361 #if IS_REACHABLE(CONFIG_RC_CORE)
362 rc_free_device(adap->rc);
363 #endif
364 kfree(adap);
366 EXPORT_SYMBOL_GPL(cec_delete_adapter);
369 * Initialise cec for linux
371 static int __init cec_devnode_init(void)
373 int ret;
375 pr_info("Linux cec interface: v0.10\n");
376 ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES,
377 CEC_NAME);
378 if (ret < 0) {
379 pr_warn("cec: unable to allocate major\n");
380 return ret;
383 #ifdef CONFIG_MEDIA_CEC_DEBUG
384 top_cec_dir = debugfs_create_dir("cec", NULL);
385 if (IS_ERR_OR_NULL(top_cec_dir)) {
386 pr_warn("cec: Failed to create debugfs cec dir\n");
387 top_cec_dir = NULL;
389 #endif
391 ret = bus_register(&cec_bus_type);
392 if (ret < 0) {
393 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
394 pr_warn("cec: bus_register failed\n");
395 return -EIO;
398 return 0;
401 static void __exit cec_devnode_exit(void)
403 debugfs_remove_recursive(top_cec_dir);
404 bus_unregister(&cec_bus_type);
405 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
408 subsys_initcall(cec_devnode_init);
409 module_exit(cec_devnode_exit)
411 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
412 MODULE_DESCRIPTION("Device node registration for cec drivers");
413 MODULE_LICENSE("GPL");