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>
35 static inline struct cec_devnode *cec_devnode_data(struct file *filp)
37 struct cec_fh *fh = filp->private_data;
39 return &fh->adap->devnode;
42 /* CEC file operations */
44 static unsigned int cec_poll(struct file *filp,
45 struct poll_table_struct *poll)
47 struct cec_devnode *devnode = cec_devnode_data(filp);
48 struct cec_fh *fh = filp->private_data;
49 struct cec_adapter *adap = fh->adap;
52 if (!devnode->registered)
53 return POLLERR | POLLHUP;
54 mutex_lock(&adap->lock);
55 if (adap->is_configured &&
56 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
57 res |= POLLOUT | POLLWRNORM;
59 res |= POLLIN | POLLRDNORM;
60 if (fh->pending_events)
62 poll_wait(filp, &fh->wait, poll);
63 mutex_unlock(&adap->lock);
67 static bool cec_is_busy(const struct cec_adapter *adap,
68 const struct cec_fh *fh)
70 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
71 bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
74 * Exclusive initiators and followers can always access the CEC adapter
76 if (valid_initiator || valid_follower)
79 * All others can only access the CEC adapter if there is no
80 * exclusive initiator and they are in INITIATOR mode.
82 return adap->cec_initiator ||
83 fh->mode_initiator == CEC_MODE_NO_INITIATOR;
86 static long cec_adap_g_caps(struct cec_adapter *adap,
87 struct cec_caps __user *parg)
89 struct cec_caps caps = {};
91 strlcpy(caps.driver, adap->devnode.dev.parent->driver->name,
93 strlcpy(caps.name, adap->name, sizeof(caps.name));
94 caps.available_log_addrs = adap->available_log_addrs;
95 caps.capabilities = adap->capabilities;
96 caps.version = LINUX_VERSION_CODE;
97 if (copy_to_user(parg, &caps, sizeof(caps)))
102 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
107 mutex_lock(&adap->lock);
108 phys_addr = adap->phys_addr;
109 mutex_unlock(&adap->lock);
110 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
115 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
116 bool block, __u16 __user *parg)
121 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
123 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
126 err = cec_phys_addr_validate(phys_addr, NULL, NULL);
129 mutex_lock(&adap->lock);
130 if (cec_is_busy(adap, fh))
133 __cec_s_phys_addr(adap, phys_addr, block);
134 mutex_unlock(&adap->lock);
138 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
139 struct cec_log_addrs __user *parg)
141 struct cec_log_addrs log_addrs;
143 mutex_lock(&adap->lock);
144 log_addrs = adap->log_addrs;
145 if (!adap->is_configured)
146 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
147 sizeof(log_addrs.log_addr));
148 mutex_unlock(&adap->lock);
150 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
155 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
156 bool block, struct cec_log_addrs __user *parg)
158 struct cec_log_addrs log_addrs;
161 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
163 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
165 log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
166 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
167 CEC_LOG_ADDRS_FL_CDC_ONLY;
168 mutex_lock(&adap->lock);
169 if (!adap->is_configuring &&
170 (!log_addrs.num_log_addrs || !adap->is_configured) &&
171 !cec_is_busy(adap, fh)) {
172 err = __cec_s_log_addrs(adap, &log_addrs, block);
174 log_addrs = adap->log_addrs;
176 mutex_unlock(&adap->lock);
179 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
184 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
185 bool block, struct cec_msg __user *parg)
187 struct cec_msg msg = {};
190 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
192 if (copy_from_user(&msg, parg, sizeof(msg)))
195 /* A CDC-Only device can only send CDC messages */
196 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
197 (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE))
200 mutex_lock(&adap->lock);
201 if (adap->log_addrs.num_log_addrs == 0)
203 else if (adap->is_configuring)
205 else if (!adap->is_configured && msg.msg[0] != 0xf0)
207 else if (cec_is_busy(adap, fh))
210 err = cec_transmit_msg_fh(adap, &msg, fh, block);
211 mutex_unlock(&adap->lock);
214 if (copy_to_user(parg, &msg, sizeof(msg)))
219 /* Called by CEC_RECEIVE: wait for a message to arrive */
220 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
222 u32 timeout = msg->timeout;
226 mutex_lock(&fh->lock);
227 /* Are there received messages queued up? */
228 if (fh->queued_msgs) {
229 /* Yes, return the first one */
230 struct cec_msg_entry *entry =
231 list_first_entry(&fh->msgs,
232 struct cec_msg_entry, list);
234 list_del(&entry->list);
238 mutex_unlock(&fh->lock);
239 /* restore original timeout value */
240 msg->timeout = timeout;
244 /* No, return EAGAIN in non-blocking mode or wait */
245 mutex_unlock(&fh->lock);
247 /* Return when in non-blocking mode */
252 /* The user specified a timeout */
253 res = wait_event_interruptible_timeout(fh->wait,
255 msecs_to_jiffies(msg->timeout));
261 /* Wait indefinitely */
262 res = wait_event_interruptible(fh->wait,
265 /* Exit on error, otherwise loop to get the new message */
270 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
271 bool block, struct cec_msg __user *parg)
273 struct cec_msg msg = {};
276 if (copy_from_user(&msg, parg, sizeof(msg)))
278 mutex_lock(&adap->lock);
279 if (!adap->is_configured && fh->mode_follower < CEC_MODE_MONITOR)
281 mutex_unlock(&adap->lock);
285 err = cec_receive_msg(fh, &msg, block);
289 if (copy_to_user(parg, &msg, sizeof(msg)))
294 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
295 bool block, struct cec_event __user *parg)
297 struct cec_event *ev = NULL;
302 mutex_lock(&fh->lock);
303 while (!fh->pending_events && block) {
304 mutex_unlock(&fh->lock);
305 err = wait_event_interruptible(fh->wait, fh->pending_events);
308 mutex_lock(&fh->lock);
311 /* Find the oldest event */
312 for (i = 0; i < CEC_NUM_EVENTS; i++) {
313 if (fh->pending_events & (1 << (i + 1)) &&
314 fh->events[i].ts <= ts) {
324 if (copy_to_user(parg, ev, sizeof(*ev))) {
329 fh->pending_events &= ~(1 << ev->event);
332 mutex_unlock(&fh->lock);
336 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
339 u32 mode = fh->mode_initiator | fh->mode_follower;
341 if (copy_to_user(parg, &mode, sizeof(mode)))
346 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
354 if (copy_from_user(&mode, parg, sizeof(mode)))
356 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK))
359 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
360 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
362 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
363 mode_follower > CEC_MODE_MONITOR_ALL)
366 if (mode_follower == CEC_MODE_MONITOR_ALL &&
367 !(adap->capabilities & CEC_CAP_MONITOR_ALL))
370 /* Follower modes should always be able to send CEC messages */
371 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
372 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
373 mode_follower >= CEC_MODE_FOLLOWER &&
374 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU)
377 /* Monitor modes require CEC_MODE_NO_INITIATOR */
378 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR)
381 /* Monitor modes require CAP_NET_ADMIN */
382 if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN))
385 mutex_lock(&adap->lock);
387 * You can't become exclusive follower if someone else already
390 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
391 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
392 adap->cec_follower && adap->cec_follower != fh)
395 * You can't become exclusive initiator if someone else already
398 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
399 adap->cec_initiator && adap->cec_initiator != fh)
403 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
404 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
406 if (old_mon_all != new_mon_all) {
408 err = cec_monitor_all_cnt_inc(adap);
410 cec_monitor_all_cnt_dec(adap);
415 mutex_unlock(&adap->lock);
419 if (fh->mode_follower == CEC_MODE_FOLLOWER)
420 adap->follower_cnt--;
421 if (mode_follower == CEC_MODE_FOLLOWER)
422 adap->follower_cnt++;
423 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
424 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
426 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
427 adap->cec_follower = fh;
428 } else if (adap->cec_follower == fh) {
429 adap->passthrough = false;
430 adap->cec_follower = NULL;
432 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
433 adap->cec_initiator = fh;
434 else if (adap->cec_initiator == fh)
435 adap->cec_initiator = NULL;
436 fh->mode_initiator = mode_initiator;
437 fh->mode_follower = mode_follower;
438 mutex_unlock(&adap->lock);
442 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
444 struct cec_devnode *devnode = cec_devnode_data(filp);
445 struct cec_fh *fh = filp->private_data;
446 struct cec_adapter *adap = fh->adap;
447 bool block = !(filp->f_flags & O_NONBLOCK);
448 void __user *parg = (void __user *)arg;
450 if (!devnode->registered)
454 case CEC_ADAP_G_CAPS:
455 return cec_adap_g_caps(adap, parg);
457 case CEC_ADAP_G_PHYS_ADDR:
458 return cec_adap_g_phys_addr(adap, parg);
460 case CEC_ADAP_S_PHYS_ADDR:
461 return cec_adap_s_phys_addr(adap, fh, block, parg);
463 case CEC_ADAP_G_LOG_ADDRS:
464 return cec_adap_g_log_addrs(adap, parg);
466 case CEC_ADAP_S_LOG_ADDRS:
467 return cec_adap_s_log_addrs(adap, fh, block, parg);
470 return cec_transmit(adap, fh, block, parg);
473 return cec_receive(adap, fh, block, parg);
476 return cec_dqevent(adap, fh, block, parg);
479 return cec_g_mode(adap, fh, parg);
482 return cec_s_mode(adap, fh, parg);
489 static int cec_open(struct inode *inode, struct file *filp)
491 struct cec_devnode *devnode =
492 container_of(inode->i_cdev, struct cec_devnode, cdev);
493 struct cec_adapter *adap = to_cec_adapter(devnode);
494 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
496 * Initial events that are automatically sent when the cec device is
499 struct cec_event ev_state = {
500 .event = CEC_EVENT_STATE_CHANGE,
501 .flags = CEC_EVENT_FL_INITIAL_STATE,
508 INIT_LIST_HEAD(&fh->msgs);
509 INIT_LIST_HEAD(&fh->xfer_list);
510 mutex_init(&fh->lock);
511 init_waitqueue_head(&fh->wait);
513 fh->mode_initiator = CEC_MODE_INITIATOR;
516 err = cec_get_device(devnode);
522 mutex_lock(&devnode->lock);
523 if (list_empty(&devnode->fhs) &&
524 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
525 err = adap->ops->adap_enable(adap, true);
527 mutex_unlock(&devnode->lock);
532 filp->private_data = fh;
534 /* Queue up initial state events */
535 ev_state.state_change.phys_addr = adap->phys_addr;
536 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
537 cec_queue_event_fh(fh, &ev_state, 0);
539 list_add(&fh->list, &devnode->fhs);
540 mutex_unlock(&devnode->lock);
545 /* Override for the release function */
546 static int cec_release(struct inode *inode, struct file *filp)
548 struct cec_devnode *devnode = cec_devnode_data(filp);
549 struct cec_adapter *adap = to_cec_adapter(devnode);
550 struct cec_fh *fh = filp->private_data;
552 mutex_lock(&adap->lock);
553 if (adap->cec_initiator == fh)
554 adap->cec_initiator = NULL;
555 if (adap->cec_follower == fh) {
556 adap->cec_follower = NULL;
557 adap->passthrough = false;
559 if (fh->mode_follower == CEC_MODE_FOLLOWER)
560 adap->follower_cnt--;
561 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
562 cec_monitor_all_cnt_dec(adap);
563 mutex_unlock(&adap->lock);
565 mutex_lock(&devnode->lock);
567 if (list_empty(&devnode->fhs) &&
568 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
569 WARN_ON(adap->ops->adap_enable(adap, false));
571 mutex_unlock(&devnode->lock);
573 /* Unhook pending transmits from this filehandle. */
574 mutex_lock(&adap->lock);
575 while (!list_empty(&fh->xfer_list)) {
576 struct cec_data *data =
577 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
579 data->blocking = false;
581 list_del(&data->xfer_list);
583 mutex_unlock(&adap->lock);
584 while (!list_empty(&fh->msgs)) {
585 struct cec_msg_entry *entry =
586 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
588 list_del(&entry->list);
593 cec_put_device(devnode);
594 filp->private_data = NULL;
598 const struct file_operations cec_devnode_fops = {
599 .owner = THIS_MODULE,
601 .unlocked_ioctl = cec_ioctl,
602 .release = cec_release,