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
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>
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>
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;
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;
60 res |= POLLIN | POLLRDNORM;
61 if (fh->total_queued_events)
63 poll_wait(filp, &fh->wait, poll);
64 mutex_unlock(&adap->lock);
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)
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,
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)))
103 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
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)))
116 static int cec_validate_phys_addr(u16 phys_addr)
120 if (phys_addr == CEC_PHYS_ADDR_INVALID)
122 for (i = 0; i < 16; i += 4)
123 if (phys_addr & (0xf << i))
127 for (i += 4; i < 16; i += 4)
128 if ((phys_addr & (0xf << i)) == 0)
133 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
134 bool block, __u16 __user *parg)
139 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
141 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
144 err = cec_validate_phys_addr(phys_addr);
147 mutex_lock(&adap->lock);
148 if (cec_is_busy(adap, fh))
151 __cec_s_phys_addr(adap, phys_addr, block);
152 mutex_unlock(&adap->lock);
156 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
157 struct cec_log_addrs __user *parg)
159 struct cec_log_addrs log_addrs;
161 mutex_lock(&adap->lock);
163 * We use memcpy here instead of assignment since there is a
164 * hole at the end of struct cec_log_addrs that an assignment
165 * might ignore. So when we do copy_to_user() we could leak
166 * one byte of memory.
168 memcpy(&log_addrs, &adap->log_addrs, sizeof(log_addrs));
169 if (!adap->is_configured)
170 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
171 sizeof(log_addrs.log_addr));
172 mutex_unlock(&adap->lock);
174 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
179 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
180 bool block, struct cec_log_addrs __user *parg)
182 struct cec_log_addrs log_addrs;
185 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
187 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
189 log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
190 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
191 CEC_LOG_ADDRS_FL_CDC_ONLY;
192 mutex_lock(&adap->lock);
193 if (!adap->is_configuring &&
194 (!log_addrs.num_log_addrs || !adap->is_configured) &&
195 !cec_is_busy(adap, fh)) {
196 err = __cec_s_log_addrs(adap, &log_addrs, block);
198 log_addrs = adap->log_addrs;
200 mutex_unlock(&adap->lock);
203 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
208 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
209 bool block, struct cec_msg __user *parg)
211 struct cec_msg msg = {};
214 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
216 if (copy_from_user(&msg, parg, sizeof(msg)))
219 /* A CDC-Only device can only send CDC messages */
220 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
221 (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE))
224 mutex_lock(&adap->lock);
225 if (adap->log_addrs.num_log_addrs == 0)
227 else if (adap->is_configuring)
229 else if (!adap->is_configured &&
230 (adap->needs_hpd || msg.msg[0] != 0xf0))
232 else if (cec_is_busy(adap, fh))
235 err = cec_transmit_msg_fh(adap, &msg, fh, block);
236 mutex_unlock(&adap->lock);
239 if (copy_to_user(parg, &msg, sizeof(msg)))
244 /* Called by CEC_RECEIVE: wait for a message to arrive */
245 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
247 u32 timeout = msg->timeout;
251 mutex_lock(&fh->lock);
252 /* Are there received messages queued up? */
253 if (fh->queued_msgs) {
254 /* Yes, return the first one */
255 struct cec_msg_entry *entry =
256 list_first_entry(&fh->msgs,
257 struct cec_msg_entry, list);
259 list_del(&entry->list);
263 mutex_unlock(&fh->lock);
264 /* restore original timeout value */
265 msg->timeout = timeout;
269 /* No, return EAGAIN in non-blocking mode or wait */
270 mutex_unlock(&fh->lock);
272 /* Return when in non-blocking mode */
277 /* The user specified a timeout */
278 res = wait_event_interruptible_timeout(fh->wait,
280 msecs_to_jiffies(msg->timeout));
286 /* Wait indefinitely */
287 res = wait_event_interruptible(fh->wait,
290 /* Exit on error, otherwise loop to get the new message */
295 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
296 bool block, struct cec_msg __user *parg)
298 struct cec_msg msg = {};
301 if (copy_from_user(&msg, parg, sizeof(msg)))
304 err = cec_receive_msg(fh, &msg, block);
308 if (copy_to_user(parg, &msg, sizeof(msg)))
313 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
314 bool block, struct cec_event __user *parg)
316 struct cec_event_entry *ev = NULL;
322 mutex_lock(&fh->lock);
323 while (!fh->total_queued_events && block) {
324 mutex_unlock(&fh->lock);
325 err = wait_event_interruptible(fh->wait,
326 fh->total_queued_events);
329 mutex_lock(&fh->lock);
332 /* Find the oldest event */
333 for (i = 0; i < CEC_NUM_EVENTS; i++) {
334 struct cec_event_entry *entry =
335 list_first_entry_or_null(&fh->events[i],
336 struct cec_event_entry, list);
338 if (entry && entry->ev.ts <= ts) {
351 if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
353 if (ev_idx >= CEC_NUM_CORE_EVENTS)
355 fh->queued_events[ev_idx]--;
356 fh->total_queued_events--;
359 mutex_unlock(&fh->lock);
363 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
366 u32 mode = fh->mode_initiator | fh->mode_follower;
368 if (copy_to_user(parg, &mode, sizeof(mode)))
373 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
381 if (copy_from_user(&mode, parg, sizeof(mode)))
383 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
384 dprintk(1, "%s: invalid mode bits set\n", __func__);
388 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
389 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
391 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
392 mode_follower > CEC_MODE_MONITOR_ALL) {
393 dprintk(1, "%s: unknown mode\n", __func__);
397 if (mode_follower == CEC_MODE_MONITOR_ALL &&
398 !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
399 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
403 if (mode_follower == CEC_MODE_MONITOR_PIN &&
404 !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
405 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
409 /* Follower modes should always be able to send CEC messages */
410 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
411 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
412 mode_follower >= CEC_MODE_FOLLOWER &&
413 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
414 dprintk(1, "%s: cannot transmit\n", __func__);
418 /* Monitor modes require CEC_MODE_NO_INITIATOR */
419 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
420 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
425 /* Monitor modes require CAP_NET_ADMIN */
426 if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
429 mutex_lock(&adap->lock);
431 * You can't become exclusive follower if someone else already
434 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
435 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
436 adap->cec_follower && adap->cec_follower != fh)
439 * You can't become exclusive initiator if someone else already
442 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
443 adap->cec_initiator && adap->cec_initiator != fh)
447 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
448 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
450 if (old_mon_all != new_mon_all) {
452 err = cec_monitor_all_cnt_inc(adap);
454 cec_monitor_all_cnt_dec(adap);
459 mutex_unlock(&adap->lock);
463 if (fh->mode_follower == CEC_MODE_FOLLOWER)
464 adap->follower_cnt--;
465 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
466 adap->monitor_pin_cnt--;
467 if (mode_follower == CEC_MODE_FOLLOWER)
468 adap->follower_cnt++;
469 if (mode_follower == CEC_MODE_MONITOR_PIN) {
470 struct cec_event ev = {
471 .flags = CEC_EVENT_FL_INITIAL_STATE,
474 ev.event = adap->cec_pin_is_high ? CEC_EVENT_PIN_CEC_HIGH :
475 CEC_EVENT_PIN_CEC_LOW;
476 cec_queue_event_fh(fh, &ev, 0);
477 adap->monitor_pin_cnt++;
479 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
480 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
482 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
483 adap->cec_follower = fh;
484 } else if (adap->cec_follower == fh) {
485 adap->passthrough = false;
486 adap->cec_follower = NULL;
488 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
489 adap->cec_initiator = fh;
490 else if (adap->cec_initiator == fh)
491 adap->cec_initiator = NULL;
492 fh->mode_initiator = mode_initiator;
493 fh->mode_follower = mode_follower;
494 mutex_unlock(&adap->lock);
498 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
500 struct cec_devnode *devnode = cec_devnode_data(filp);
501 struct cec_fh *fh = filp->private_data;
502 struct cec_adapter *adap = fh->adap;
503 bool block = !(filp->f_flags & O_NONBLOCK);
504 void __user *parg = (void __user *)arg;
506 if (!devnode->registered)
510 case CEC_ADAP_G_CAPS:
511 return cec_adap_g_caps(adap, parg);
513 case CEC_ADAP_G_PHYS_ADDR:
514 return cec_adap_g_phys_addr(adap, parg);
516 case CEC_ADAP_S_PHYS_ADDR:
517 return cec_adap_s_phys_addr(adap, fh, block, parg);
519 case CEC_ADAP_G_LOG_ADDRS:
520 return cec_adap_g_log_addrs(adap, parg);
522 case CEC_ADAP_S_LOG_ADDRS:
523 return cec_adap_s_log_addrs(adap, fh, block, parg);
526 return cec_transmit(adap, fh, block, parg);
529 return cec_receive(adap, fh, block, parg);
532 return cec_dqevent(adap, fh, block, parg);
535 return cec_g_mode(adap, fh, parg);
538 return cec_s_mode(adap, fh, parg);
545 static int cec_open(struct inode *inode, struct file *filp)
547 struct cec_devnode *devnode =
548 container_of(inode->i_cdev, struct cec_devnode, cdev);
549 struct cec_adapter *adap = to_cec_adapter(devnode);
550 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
552 * Initial events that are automatically sent when the cec device is
555 struct cec_event ev_state = {
556 .event = CEC_EVENT_STATE_CHANGE,
557 .flags = CEC_EVENT_FL_INITIAL_STATE,
565 INIT_LIST_HEAD(&fh->msgs);
566 INIT_LIST_HEAD(&fh->xfer_list);
567 for (i = 0; i < CEC_NUM_EVENTS; i++)
568 INIT_LIST_HEAD(&fh->events[i]);
569 mutex_init(&fh->lock);
570 init_waitqueue_head(&fh->wait);
572 fh->mode_initiator = CEC_MODE_INITIATOR;
575 err = cec_get_device(devnode);
581 mutex_lock(&devnode->lock);
582 if (list_empty(&devnode->fhs) &&
584 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
585 err = adap->ops->adap_enable(adap, true);
587 mutex_unlock(&devnode->lock);
592 filp->private_data = fh;
594 /* Queue up initial state events */
595 ev_state.state_change.phys_addr = adap->phys_addr;
596 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
597 cec_queue_event_fh(fh, &ev_state, 0);
599 list_add(&fh->list, &devnode->fhs);
600 mutex_unlock(&devnode->lock);
605 /* Override for the release function */
606 static int cec_release(struct inode *inode, struct file *filp)
608 struct cec_devnode *devnode = cec_devnode_data(filp);
609 struct cec_adapter *adap = to_cec_adapter(devnode);
610 struct cec_fh *fh = filp->private_data;
613 mutex_lock(&adap->lock);
614 if (adap->cec_initiator == fh)
615 adap->cec_initiator = NULL;
616 if (adap->cec_follower == fh) {
617 adap->cec_follower = NULL;
618 adap->passthrough = false;
620 if (fh->mode_follower == CEC_MODE_FOLLOWER)
621 adap->follower_cnt--;
622 if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
623 adap->monitor_pin_cnt--;
624 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
625 cec_monitor_all_cnt_dec(adap);
626 mutex_unlock(&adap->lock);
628 mutex_lock(&devnode->lock);
630 if (list_empty(&devnode->fhs) &&
632 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
633 WARN_ON(adap->ops->adap_enable(adap, false));
635 mutex_unlock(&devnode->lock);
637 /* Unhook pending transmits from this filehandle. */
638 mutex_lock(&adap->lock);
639 while (!list_empty(&fh->xfer_list)) {
640 struct cec_data *data =
641 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
643 data->blocking = false;
645 list_del(&data->xfer_list);
647 mutex_unlock(&adap->lock);
648 while (!list_empty(&fh->msgs)) {
649 struct cec_msg_entry *entry =
650 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
652 list_del(&entry->list);
655 for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
656 while (!list_empty(&fh->events[i])) {
657 struct cec_event_entry *entry =
658 list_first_entry(&fh->events[i],
659 struct cec_event_entry, list);
661 list_del(&entry->list);
667 cec_put_device(devnode);
668 filp->private_data = NULL;
672 const struct file_operations cec_devnode_fops = {
673 .owner = THIS_MODULE,
675 .unlocked_ioctl = cec_ioctl,
676 .release = cec_release,