Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / cec / cec-api.c
blob492db12b8c4dc8a0040ffdc922f5786f66dd6c47
1 /*
2 * cec-api.c - HDMI Consumer Electronics Control framework - API
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/ktime.h>
26 #include <linux/slab.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <linux/version.h>
33 #include <media/cec-pin.h>
34 #include "cec-priv.h"
35 #include "cec-pin-priv.h"
37 static inline struct cec_devnode *cec_devnode_data(struct file *filp)
39 struct cec_fh *fh = filp->private_data;
41 return &fh->adap->devnode;
44 /* CEC file operations */
46 static __poll_t cec_poll(struct file *filp,
47 struct poll_table_struct *poll)
49 struct cec_fh *fh = filp->private_data;
50 struct cec_adapter *adap = fh->adap;
51 __poll_t res = 0;
53 if (!cec_is_registered(adap))
54 return EPOLLERR | EPOLLHUP;
55 mutex_lock(&adap->lock);
56 if (adap->is_configured &&
57 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
58 res |= EPOLLOUT | EPOLLWRNORM;
59 if (fh->queued_msgs)
60 res |= EPOLLIN | EPOLLRDNORM;
61 if (fh->total_queued_events)
62 res |= EPOLLPRI;
63 poll_wait(filp, &fh->wait, poll);
64 mutex_unlock(&adap->lock);
65 return res;
68 static bool cec_is_busy(const struct cec_adapter *adap,
69 const struct cec_fh *fh)
71 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
72 bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
75 * Exclusive initiators and followers can always access the CEC adapter
77 if (valid_initiator || valid_follower)
78 return false;
80 * All others can only access the CEC adapter if there is no
81 * exclusive initiator and they are in INITIATOR mode.
83 return adap->cec_initiator ||
84 fh->mode_initiator == CEC_MODE_NO_INITIATOR;
87 static long cec_adap_g_caps(struct cec_adapter *adap,
88 struct cec_caps __user *parg)
90 struct cec_caps caps = {};
92 strlcpy(caps.driver, adap->devnode.dev.parent->driver->name,
93 sizeof(caps.driver));
94 strlcpy(caps.name, adap->name, sizeof(caps.name));
95 caps.available_log_addrs = adap->available_log_addrs;
96 caps.capabilities = adap->capabilities;
97 caps.version = LINUX_VERSION_CODE;
98 if (copy_to_user(parg, &caps, sizeof(caps)))
99 return -EFAULT;
100 return 0;
103 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
104 __u16 __user *parg)
106 u16 phys_addr;
108 mutex_lock(&adap->lock);
109 phys_addr = adap->phys_addr;
110 mutex_unlock(&adap->lock);
111 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
112 return -EFAULT;
113 return 0;
116 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
117 bool block, __u16 __user *parg)
119 u16 phys_addr;
120 long err;
122 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
123 return -ENOTTY;
124 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
125 return -EFAULT;
127 err = cec_phys_addr_validate(phys_addr, NULL, NULL);
128 if (err)
129 return err;
130 mutex_lock(&adap->lock);
131 if (cec_is_busy(adap, fh))
132 err = -EBUSY;
133 else
134 __cec_s_phys_addr(adap, phys_addr, block);
135 mutex_unlock(&adap->lock);
136 return err;
139 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
140 struct cec_log_addrs __user *parg)
142 struct cec_log_addrs log_addrs;
144 mutex_lock(&adap->lock);
145 log_addrs = adap->log_addrs;
146 if (!adap->is_configured)
147 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
148 sizeof(log_addrs.log_addr));
149 mutex_unlock(&adap->lock);
151 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
152 return -EFAULT;
153 return 0;
156 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
157 bool block, struct cec_log_addrs __user *parg)
159 struct cec_log_addrs log_addrs;
160 long err = -EBUSY;
162 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
163 return -ENOTTY;
164 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
165 return -EFAULT;
166 log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
167 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
168 CEC_LOG_ADDRS_FL_CDC_ONLY;
169 mutex_lock(&adap->lock);
170 if (!adap->is_configuring &&
171 (!log_addrs.num_log_addrs || !adap->is_configured) &&
172 !cec_is_busy(adap, fh)) {
173 err = __cec_s_log_addrs(adap, &log_addrs, block);
174 if (!err)
175 log_addrs = adap->log_addrs;
177 mutex_unlock(&adap->lock);
178 if (err)
179 return err;
180 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
181 return -EFAULT;
182 return 0;
185 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
186 bool block, struct cec_msg __user *parg)
188 struct cec_msg msg = {};
189 long err = 0;
191 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
192 return -ENOTTY;
193 if (copy_from_user(&msg, parg, sizeof(msg)))
194 return -EFAULT;
196 /* A CDC-Only device can only send CDC messages */
197 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
198 (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE))
199 return -EINVAL;
201 mutex_lock(&adap->lock);
202 if (adap->log_addrs.num_log_addrs == 0)
203 err = -EPERM;
204 else if (adap->is_configuring)
205 err = -ENONET;
206 else if (!adap->is_configured &&
207 (adap->needs_hpd || msg.msg[0] != 0xf0))
208 err = -ENONET;
209 else if (cec_is_busy(adap, fh))
210 err = -EBUSY;
211 else
212 err = cec_transmit_msg_fh(adap, &msg, fh, block);
213 mutex_unlock(&adap->lock);
214 if (err)
215 return err;
216 if (copy_to_user(parg, &msg, sizeof(msg)))
217 return -EFAULT;
218 return 0;
221 /* Called by CEC_RECEIVE: wait for a message to arrive */
222 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
224 u32 timeout = msg->timeout;
225 int res;
227 do {
228 mutex_lock(&fh->lock);
229 /* Are there received messages queued up? */
230 if (fh->queued_msgs) {
231 /* Yes, return the first one */
232 struct cec_msg_entry *entry =
233 list_first_entry(&fh->msgs,
234 struct cec_msg_entry, list);
236 list_del(&entry->list);
237 *msg = entry->msg;
238 kfree(entry);
239 fh->queued_msgs--;
240 mutex_unlock(&fh->lock);
241 /* restore original timeout value */
242 msg->timeout = timeout;
243 return 0;
246 /* No, return EAGAIN in non-blocking mode or wait */
247 mutex_unlock(&fh->lock);
249 /* Return when in non-blocking mode */
250 if (!block)
251 return -EAGAIN;
253 if (msg->timeout) {
254 /* The user specified a timeout */
255 res = wait_event_interruptible_timeout(fh->wait,
256 fh->queued_msgs,
257 msecs_to_jiffies(msg->timeout));
258 if (res == 0)
259 res = -ETIMEDOUT;
260 else if (res > 0)
261 res = 0;
262 } else {
263 /* Wait indefinitely */
264 res = wait_event_interruptible(fh->wait,
265 fh->queued_msgs);
267 /* Exit on error, otherwise loop to get the new message */
268 } while (!res);
269 return res;
272 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
273 bool block, struct cec_msg __user *parg)
275 struct cec_msg msg = {};
276 long err;
278 if (copy_from_user(&msg, parg, sizeof(msg)))
279 return -EFAULT;
281 err = cec_receive_msg(fh, &msg, block);
282 if (err)
283 return err;
284 msg.flags = 0;
285 if (copy_to_user(parg, &msg, sizeof(msg)))
286 return -EFAULT;
287 return 0;
290 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
291 bool block, struct cec_event __user *parg)
293 struct cec_event_entry *ev = NULL;
294 u64 ts = ~0ULL;
295 unsigned int i;
296 unsigned int ev_idx;
297 long err = 0;
299 mutex_lock(&fh->lock);
300 while (!fh->total_queued_events && block) {
301 mutex_unlock(&fh->lock);
302 err = wait_event_interruptible(fh->wait,
303 fh->total_queued_events);
304 if (err)
305 return err;
306 mutex_lock(&fh->lock);
309 /* Find the oldest event */
310 for (i = 0; i < CEC_NUM_EVENTS; i++) {
311 struct cec_event_entry *entry =
312 list_first_entry_or_null(&fh->events[i],
313 struct cec_event_entry, list);
315 if (entry && entry->ev.ts <= ts) {
316 ev = entry;
317 ev_idx = i;
318 ts = ev->ev.ts;
322 if (!ev) {
323 err = -EAGAIN;
324 goto unlock;
326 list_del(&ev->list);
328 if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
329 err = -EFAULT;
330 if (ev_idx >= CEC_NUM_CORE_EVENTS)
331 kfree(ev);
332 fh->queued_events[ev_idx]--;
333 fh->total_queued_events--;
335 unlock:
336 mutex_unlock(&fh->lock);
337 return err;
340 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
341 u32 __user *parg)
343 u32 mode = fh->mode_initiator | fh->mode_follower;
345 if (copy_to_user(parg, &mode, sizeof(mode)))
346 return -EFAULT;
347 return 0;
350 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
351 u32 __user *parg)
353 u32 mode;
354 u8 mode_initiator;
355 u8 mode_follower;
356 bool send_pin_event = false;
357 long err = 0;
359 if (copy_from_user(&mode, parg, sizeof(mode)))
360 return -EFAULT;
361 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
362 dprintk(1, "%s: invalid mode bits set\n", __func__);
363 return -EINVAL;
366 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
367 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
369 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
370 mode_follower > CEC_MODE_MONITOR_ALL) {
371 dprintk(1, "%s: unknown mode\n", __func__);
372 return -EINVAL;
375 if (mode_follower == CEC_MODE_MONITOR_ALL &&
376 !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
377 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
378 return -EINVAL;
381 if (mode_follower == CEC_MODE_MONITOR_PIN &&
382 !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
383 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
384 return -EINVAL;
387 /* Follower modes should always be able to send CEC messages */
388 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
389 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
390 mode_follower >= CEC_MODE_FOLLOWER &&
391 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
392 dprintk(1, "%s: cannot transmit\n", __func__);
393 return -EINVAL;
396 /* Monitor modes require CEC_MODE_NO_INITIATOR */
397 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
398 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
399 __func__);
400 return -EINVAL;
403 /* Monitor modes require CAP_NET_ADMIN */
404 if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
405 return -EPERM;
407 mutex_lock(&adap->lock);
409 * You can't become exclusive follower if someone else already
410 * has that job.
412 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
413 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
414 adap->cec_follower && adap->cec_follower != fh)
415 err = -EBUSY;
417 * You can't become exclusive initiator if someone else already
418 * has that job.
420 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
421 adap->cec_initiator && adap->cec_initiator != fh)
422 err = -EBUSY;
424 if (!err) {
425 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
426 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
428 if (old_mon_all != new_mon_all) {
429 if (new_mon_all)
430 err = cec_monitor_all_cnt_inc(adap);
431 else
432 cec_monitor_all_cnt_dec(adap);
436 if (!err) {
437 bool old_mon_pin = fh->mode_follower == CEC_MODE_MONITOR_PIN;
438 bool new_mon_pin = mode_follower == CEC_MODE_MONITOR_PIN;
440 if (old_mon_pin != new_mon_pin) {
441 send_pin_event = new_mon_pin;
442 if (new_mon_pin)
443 err = cec_monitor_pin_cnt_inc(adap);
444 else
445 cec_monitor_pin_cnt_dec(adap);
449 if (err) {
450 mutex_unlock(&adap->lock);
451 return err;
454 if (fh->mode_follower == CEC_MODE_FOLLOWER)
455 adap->follower_cnt--;
456 if (mode_follower == CEC_MODE_FOLLOWER)
457 adap->follower_cnt++;
458 if (send_pin_event) {
459 struct cec_event ev = {
460 .flags = CEC_EVENT_FL_INITIAL_STATE,
463 ev.event = adap->cec_pin_is_high ? CEC_EVENT_PIN_CEC_HIGH :
464 CEC_EVENT_PIN_CEC_LOW;
465 cec_queue_event_fh(fh, &ev, 0);
467 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
468 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
469 adap->passthrough =
470 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
471 adap->cec_follower = fh;
472 } else if (adap->cec_follower == fh) {
473 adap->passthrough = false;
474 adap->cec_follower = NULL;
476 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
477 adap->cec_initiator = fh;
478 else if (adap->cec_initiator == fh)
479 adap->cec_initiator = NULL;
480 fh->mode_initiator = mode_initiator;
481 fh->mode_follower = mode_follower;
482 mutex_unlock(&adap->lock);
483 return 0;
486 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
488 struct cec_fh *fh = filp->private_data;
489 struct cec_adapter *adap = fh->adap;
490 bool block = !(filp->f_flags & O_NONBLOCK);
491 void __user *parg = (void __user *)arg;
493 if (!cec_is_registered(adap))
494 return -ENODEV;
496 switch (cmd) {
497 case CEC_ADAP_G_CAPS:
498 return cec_adap_g_caps(adap, parg);
500 case CEC_ADAP_G_PHYS_ADDR:
501 return cec_adap_g_phys_addr(adap, parg);
503 case CEC_ADAP_S_PHYS_ADDR:
504 return cec_adap_s_phys_addr(adap, fh, block, parg);
506 case CEC_ADAP_G_LOG_ADDRS:
507 return cec_adap_g_log_addrs(adap, parg);
509 case CEC_ADAP_S_LOG_ADDRS:
510 return cec_adap_s_log_addrs(adap, fh, block, parg);
512 case CEC_TRANSMIT:
513 return cec_transmit(adap, fh, block, parg);
515 case CEC_RECEIVE:
516 return cec_receive(adap, fh, block, parg);
518 case CEC_DQEVENT:
519 return cec_dqevent(adap, fh, block, parg);
521 case CEC_G_MODE:
522 return cec_g_mode(adap, fh, parg);
524 case CEC_S_MODE:
525 return cec_s_mode(adap, fh, parg);
527 default:
528 return -ENOTTY;
532 static int cec_open(struct inode *inode, struct file *filp)
534 struct cec_devnode *devnode =
535 container_of(inode->i_cdev, struct cec_devnode, cdev);
536 struct cec_adapter *adap = to_cec_adapter(devnode);
537 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
539 * Initial events that are automatically sent when the cec device is
540 * opened.
542 struct cec_event ev = {
543 .event = CEC_EVENT_STATE_CHANGE,
544 .flags = CEC_EVENT_FL_INITIAL_STATE,
546 unsigned int i;
547 int err;
549 if (!fh)
550 return -ENOMEM;
552 INIT_LIST_HEAD(&fh->msgs);
553 INIT_LIST_HEAD(&fh->xfer_list);
554 for (i = 0; i < CEC_NUM_EVENTS; i++)
555 INIT_LIST_HEAD(&fh->events[i]);
556 mutex_init(&fh->lock);
557 init_waitqueue_head(&fh->wait);
559 fh->mode_initiator = CEC_MODE_INITIATOR;
560 fh->adap = adap;
562 err = cec_get_device(devnode);
563 if (err) {
564 kfree(fh);
565 return err;
568 mutex_lock(&devnode->lock);
569 if (list_empty(&devnode->fhs) &&
570 !adap->needs_hpd &&
571 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
572 err = adap->ops->adap_enable(adap, true);
573 if (err) {
574 mutex_unlock(&devnode->lock);
575 kfree(fh);
576 return err;
579 filp->private_data = fh;
581 /* Queue up initial state events */
582 ev.state_change.phys_addr = adap->phys_addr;
583 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
584 cec_queue_event_fh(fh, &ev, 0);
585 #ifdef CONFIG_CEC_PIN
586 if (adap->pin && adap->pin->ops->read_hpd) {
587 err = adap->pin->ops->read_hpd(adap);
588 if (err >= 0) {
589 ev.event = err ? CEC_EVENT_PIN_HPD_HIGH :
590 CEC_EVENT_PIN_HPD_LOW;
591 cec_queue_event_fh(fh, &ev, 0);
594 #endif
596 list_add(&fh->list, &devnode->fhs);
597 mutex_unlock(&devnode->lock);
599 return 0;
602 /* Override for the release function */
603 static int cec_release(struct inode *inode, struct file *filp)
605 struct cec_devnode *devnode = cec_devnode_data(filp);
606 struct cec_adapter *adap = to_cec_adapter(devnode);
607 struct cec_fh *fh = filp->private_data;
608 unsigned int i;
610 mutex_lock(&adap->lock);
611 if (adap->cec_initiator == fh)
612 adap->cec_initiator = NULL;
613 if (adap->cec_follower == fh) {
614 adap->cec_follower = NULL;
615 adap->passthrough = false;
617 if (fh->mode_follower == CEC_MODE_FOLLOWER)
618 adap->follower_cnt--;
619 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
620 cec_monitor_pin_cnt_dec(adap);
621 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
622 cec_monitor_all_cnt_dec(adap);
623 mutex_unlock(&adap->lock);
625 mutex_lock(&devnode->lock);
626 list_del(&fh->list);
627 if (cec_is_registered(adap) && list_empty(&devnode->fhs) &&
628 !adap->needs_hpd && adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
629 WARN_ON(adap->ops->adap_enable(adap, false));
631 mutex_unlock(&devnode->lock);
633 /* Unhook pending transmits from this filehandle. */
634 mutex_lock(&adap->lock);
635 while (!list_empty(&fh->xfer_list)) {
636 struct cec_data *data =
637 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
639 data->blocking = false;
640 data->fh = NULL;
641 list_del(&data->xfer_list);
643 mutex_unlock(&adap->lock);
644 while (!list_empty(&fh->msgs)) {
645 struct cec_msg_entry *entry =
646 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
648 list_del(&entry->list);
649 kfree(entry);
651 for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
652 while (!list_empty(&fh->events[i])) {
653 struct cec_event_entry *entry =
654 list_first_entry(&fh->events[i],
655 struct cec_event_entry, list);
657 list_del(&entry->list);
658 kfree(entry);
661 kfree(fh);
663 cec_put_device(devnode);
664 filp->private_data = NULL;
665 return 0;
668 const struct file_operations cec_devnode_fops = {
669 .owner = THIS_MODULE,
670 .open = cec_open,
671 .unlocked_ioctl = cec_ioctl,
672 .release = cec_release,
673 .poll = cec_poll,
674 .llseek = no_llseek,