Linux 4.14.51
[linux/fpc-iii.git] / drivers / media / cec / cec-api.c
bloba079f7fe018c4ab2529f30e6b412ee51aef4bae1
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"
36 static inline struct cec_devnode *cec_devnode_data(struct file *filp)
38 struct cec_fh *fh = filp->private_data;
40 return &fh->adap->devnode;
43 /* CEC file operations */
45 static unsigned int cec_poll(struct file *filp,
46 struct poll_table_struct *poll)
48 struct cec_devnode *devnode = cec_devnode_data(filp);
49 struct cec_fh *fh = filp->private_data;
50 struct cec_adapter *adap = fh->adap;
51 unsigned int res = 0;
53 if (!devnode->registered)
54 return POLLERR | POLLHUP;
55 mutex_lock(&adap->lock);
56 if (adap->is_configured &&
57 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
58 res |= POLLOUT | POLLWRNORM;
59 if (fh->queued_msgs)
60 res |= POLLIN | POLLRDNORM;
61 if (fh->total_queued_events)
62 res |= POLLPRI;
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 long err = 0;
358 if (copy_from_user(&mode, parg, sizeof(mode)))
359 return -EFAULT;
360 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
361 dprintk(1, "%s: invalid mode bits set\n", __func__);
362 return -EINVAL;
365 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
366 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
368 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
369 mode_follower > CEC_MODE_MONITOR_ALL) {
370 dprintk(1, "%s: unknown mode\n", __func__);
371 return -EINVAL;
374 if (mode_follower == CEC_MODE_MONITOR_ALL &&
375 !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
376 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
377 return -EINVAL;
380 if (mode_follower == CEC_MODE_MONITOR_PIN &&
381 !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
382 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
383 return -EINVAL;
386 /* Follower modes should always be able to send CEC messages */
387 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
388 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
389 mode_follower >= CEC_MODE_FOLLOWER &&
390 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
391 dprintk(1, "%s: cannot transmit\n", __func__);
392 return -EINVAL;
395 /* Monitor modes require CEC_MODE_NO_INITIATOR */
396 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
397 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
398 __func__);
399 return -EINVAL;
402 /* Monitor modes require CAP_NET_ADMIN */
403 if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
404 return -EPERM;
406 mutex_lock(&adap->lock);
408 * You can't become exclusive follower if someone else already
409 * has that job.
411 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
412 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
413 adap->cec_follower && adap->cec_follower != fh)
414 err = -EBUSY;
416 * You can't become exclusive initiator if someone else already
417 * has that job.
419 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
420 adap->cec_initiator && adap->cec_initiator != fh)
421 err = -EBUSY;
423 if (!err) {
424 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
425 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
427 if (old_mon_all != new_mon_all) {
428 if (new_mon_all)
429 err = cec_monitor_all_cnt_inc(adap);
430 else
431 cec_monitor_all_cnt_dec(adap);
435 if (err) {
436 mutex_unlock(&adap->lock);
437 return err;
440 if (fh->mode_follower == CEC_MODE_FOLLOWER)
441 adap->follower_cnt--;
442 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
443 adap->monitor_pin_cnt--;
444 if (mode_follower == CEC_MODE_FOLLOWER)
445 adap->follower_cnt++;
446 if (mode_follower == CEC_MODE_MONITOR_PIN) {
447 struct cec_event ev = {
448 .flags = CEC_EVENT_FL_INITIAL_STATE,
451 ev.event = adap->cec_pin_is_high ? CEC_EVENT_PIN_CEC_HIGH :
452 CEC_EVENT_PIN_CEC_LOW;
453 cec_queue_event_fh(fh, &ev, 0);
454 adap->monitor_pin_cnt++;
456 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
457 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
458 adap->passthrough =
459 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
460 adap->cec_follower = fh;
461 } else if (adap->cec_follower == fh) {
462 adap->passthrough = false;
463 adap->cec_follower = NULL;
465 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
466 adap->cec_initiator = fh;
467 else if (adap->cec_initiator == fh)
468 adap->cec_initiator = NULL;
469 fh->mode_initiator = mode_initiator;
470 fh->mode_follower = mode_follower;
471 mutex_unlock(&adap->lock);
472 return 0;
475 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
477 struct cec_devnode *devnode = cec_devnode_data(filp);
478 struct cec_fh *fh = filp->private_data;
479 struct cec_adapter *adap = fh->adap;
480 bool block = !(filp->f_flags & O_NONBLOCK);
481 void __user *parg = (void __user *)arg;
483 if (!devnode->registered)
484 return -ENODEV;
486 switch (cmd) {
487 case CEC_ADAP_G_CAPS:
488 return cec_adap_g_caps(adap, parg);
490 case CEC_ADAP_G_PHYS_ADDR:
491 return cec_adap_g_phys_addr(adap, parg);
493 case CEC_ADAP_S_PHYS_ADDR:
494 return cec_adap_s_phys_addr(adap, fh, block, parg);
496 case CEC_ADAP_G_LOG_ADDRS:
497 return cec_adap_g_log_addrs(adap, parg);
499 case CEC_ADAP_S_LOG_ADDRS:
500 return cec_adap_s_log_addrs(adap, fh, block, parg);
502 case CEC_TRANSMIT:
503 return cec_transmit(adap, fh, block, parg);
505 case CEC_RECEIVE:
506 return cec_receive(adap, fh, block, parg);
508 case CEC_DQEVENT:
509 return cec_dqevent(adap, fh, block, parg);
511 case CEC_G_MODE:
512 return cec_g_mode(adap, fh, parg);
514 case CEC_S_MODE:
515 return cec_s_mode(adap, fh, parg);
517 default:
518 return -ENOTTY;
522 static int cec_open(struct inode *inode, struct file *filp)
524 struct cec_devnode *devnode =
525 container_of(inode->i_cdev, struct cec_devnode, cdev);
526 struct cec_adapter *adap = to_cec_adapter(devnode);
527 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
529 * Initial events that are automatically sent when the cec device is
530 * opened.
532 struct cec_event ev_state = {
533 .event = CEC_EVENT_STATE_CHANGE,
534 .flags = CEC_EVENT_FL_INITIAL_STATE,
536 unsigned int i;
537 int err;
539 if (!fh)
540 return -ENOMEM;
542 INIT_LIST_HEAD(&fh->msgs);
543 INIT_LIST_HEAD(&fh->xfer_list);
544 for (i = 0; i < CEC_NUM_EVENTS; i++)
545 INIT_LIST_HEAD(&fh->events[i]);
546 mutex_init(&fh->lock);
547 init_waitqueue_head(&fh->wait);
549 fh->mode_initiator = CEC_MODE_INITIATOR;
550 fh->adap = adap;
552 err = cec_get_device(devnode);
553 if (err) {
554 kfree(fh);
555 return err;
558 mutex_lock(&devnode->lock);
559 if (list_empty(&devnode->fhs) &&
560 !adap->needs_hpd &&
561 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
562 err = adap->ops->adap_enable(adap, true);
563 if (err) {
564 mutex_unlock(&devnode->lock);
565 kfree(fh);
566 return err;
569 filp->private_data = fh;
571 /* Queue up initial state events */
572 ev_state.state_change.phys_addr = adap->phys_addr;
573 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
574 cec_queue_event_fh(fh, &ev_state, 0);
576 list_add(&fh->list, &devnode->fhs);
577 mutex_unlock(&devnode->lock);
579 return 0;
582 /* Override for the release function */
583 static int cec_release(struct inode *inode, struct file *filp)
585 struct cec_devnode *devnode = cec_devnode_data(filp);
586 struct cec_adapter *adap = to_cec_adapter(devnode);
587 struct cec_fh *fh = filp->private_data;
588 unsigned int i;
590 mutex_lock(&adap->lock);
591 if (adap->cec_initiator == fh)
592 adap->cec_initiator = NULL;
593 if (adap->cec_follower == fh) {
594 adap->cec_follower = NULL;
595 adap->passthrough = false;
597 if (fh->mode_follower == CEC_MODE_FOLLOWER)
598 adap->follower_cnt--;
599 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
600 adap->monitor_pin_cnt--;
601 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
602 cec_monitor_all_cnt_dec(adap);
603 mutex_unlock(&adap->lock);
605 mutex_lock(&devnode->lock);
606 list_del(&fh->list);
607 if (list_empty(&devnode->fhs) &&
608 !adap->needs_hpd &&
609 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
610 WARN_ON(adap->ops->adap_enable(adap, false));
612 mutex_unlock(&devnode->lock);
614 /* Unhook pending transmits from this filehandle. */
615 mutex_lock(&adap->lock);
616 while (!list_empty(&fh->xfer_list)) {
617 struct cec_data *data =
618 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
620 data->blocking = false;
621 data->fh = NULL;
622 list_del(&data->xfer_list);
624 mutex_unlock(&adap->lock);
625 while (!list_empty(&fh->msgs)) {
626 struct cec_msg_entry *entry =
627 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
629 list_del(&entry->list);
630 kfree(entry);
632 for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
633 while (!list_empty(&fh->events[i])) {
634 struct cec_event_entry *entry =
635 list_first_entry(&fh->events[i],
636 struct cec_event_entry, list);
638 list_del(&entry->list);
639 kfree(entry);
642 kfree(fh);
644 cec_put_device(devnode);
645 filp->private_data = NULL;
646 return 0;
649 const struct file_operations cec_devnode_fops = {
650 .owner = THIS_MODULE,
651 .open = cec_open,
652 .unlocked_ioctl = cec_ioctl,
653 .release = cec_release,
654 .poll = cec_poll,
655 .llseek = no_llseek,