Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * cec-api.c - HDMI Consumer Electronics Control framework - API
0004  *
0005  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
0006  */
0007 
0008 #include <linux/errno.h>
0009 #include <linux/init.h>
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/kmod.h>
0013 #include <linux/ktime.h>
0014 #include <linux/slab.h>
0015 #include <linux/mm.h>
0016 #include <linux/string.h>
0017 #include <linux/types.h>
0018 #include <linux/uaccess.h>
0019 #include <linux/version.h>
0020 
0021 #include <media/cec-pin.h>
0022 #include "cec-priv.h"
0023 #include "cec-pin-priv.h"
0024 
0025 static inline struct cec_devnode *cec_devnode_data(struct file *filp)
0026 {
0027     struct cec_fh *fh = filp->private_data;
0028 
0029     return &fh->adap->devnode;
0030 }
0031 
0032 /* CEC file operations */
0033 
0034 static __poll_t cec_poll(struct file *filp,
0035                  struct poll_table_struct *poll)
0036 {
0037     struct cec_fh *fh = filp->private_data;
0038     struct cec_adapter *adap = fh->adap;
0039     __poll_t res = 0;
0040 
0041     poll_wait(filp, &fh->wait, poll);
0042     if (!cec_is_registered(adap))
0043         return EPOLLERR | EPOLLHUP | EPOLLPRI;
0044     mutex_lock(&adap->lock);
0045     if (adap->is_configured &&
0046         adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
0047         res |= EPOLLOUT | EPOLLWRNORM;
0048     if (fh->queued_msgs)
0049         res |= EPOLLIN | EPOLLRDNORM;
0050     if (fh->total_queued_events)
0051         res |= EPOLLPRI;
0052     mutex_unlock(&adap->lock);
0053     return res;
0054 }
0055 
0056 static bool cec_is_busy(const struct cec_adapter *adap,
0057             const struct cec_fh *fh)
0058 {
0059     bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
0060     bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
0061 
0062     /*
0063      * Exclusive initiators and followers can always access the CEC adapter
0064      */
0065     if (valid_initiator || valid_follower)
0066         return false;
0067     /*
0068      * All others can only access the CEC adapter if there is no
0069      * exclusive initiator and they are in INITIATOR mode.
0070      */
0071     return adap->cec_initiator ||
0072            fh->mode_initiator == CEC_MODE_NO_INITIATOR;
0073 }
0074 
0075 static long cec_adap_g_caps(struct cec_adapter *adap,
0076                 struct cec_caps __user *parg)
0077 {
0078     struct cec_caps caps = {};
0079 
0080     strscpy(caps.driver, adap->devnode.dev.parent->driver->name,
0081         sizeof(caps.driver));
0082     strscpy(caps.name, adap->name, sizeof(caps.name));
0083     caps.available_log_addrs = adap->available_log_addrs;
0084     caps.capabilities = adap->capabilities;
0085     caps.version = LINUX_VERSION_CODE;
0086     if (copy_to_user(parg, &caps, sizeof(caps)))
0087         return -EFAULT;
0088     return 0;
0089 }
0090 
0091 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
0092                  __u16 __user *parg)
0093 {
0094     u16 phys_addr;
0095 
0096     mutex_lock(&adap->lock);
0097     phys_addr = adap->phys_addr;
0098     mutex_unlock(&adap->lock);
0099     if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
0100         return -EFAULT;
0101     return 0;
0102 }
0103 
0104 static int cec_validate_phys_addr(u16 phys_addr)
0105 {
0106     int i;
0107 
0108     if (phys_addr == CEC_PHYS_ADDR_INVALID)
0109         return 0;
0110     for (i = 0; i < 16; i += 4)
0111         if (phys_addr & (0xf << i))
0112             break;
0113     if (i == 16)
0114         return 0;
0115     for (i += 4; i < 16; i += 4)
0116         if ((phys_addr & (0xf << i)) == 0)
0117             return -EINVAL;
0118     return 0;
0119 }
0120 
0121 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
0122                  bool block, __u16 __user *parg)
0123 {
0124     u16 phys_addr;
0125     long err;
0126 
0127     if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
0128         return -ENOTTY;
0129     if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
0130         return -EFAULT;
0131 
0132     err = cec_validate_phys_addr(phys_addr);
0133     if (err)
0134         return err;
0135     mutex_lock(&adap->lock);
0136     if (cec_is_busy(adap, fh))
0137         err = -EBUSY;
0138     else
0139         __cec_s_phys_addr(adap, phys_addr, block);
0140     mutex_unlock(&adap->lock);
0141     return err;
0142 }
0143 
0144 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
0145                  struct cec_log_addrs __user *parg)
0146 {
0147     struct cec_log_addrs log_addrs;
0148 
0149     mutex_lock(&adap->lock);
0150     /*
0151      * We use memcpy here instead of assignment since there is a
0152      * hole at the end of struct cec_log_addrs that an assignment
0153      * might ignore. So when we do copy_to_user() we could leak
0154      * one byte of memory.
0155      */
0156     memcpy(&log_addrs, &adap->log_addrs, sizeof(log_addrs));
0157     if (!adap->is_configured)
0158         memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
0159                sizeof(log_addrs.log_addr));
0160     mutex_unlock(&adap->lock);
0161 
0162     if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
0163         return -EFAULT;
0164     return 0;
0165 }
0166 
0167 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
0168                  bool block, struct cec_log_addrs __user *parg)
0169 {
0170     struct cec_log_addrs log_addrs;
0171     long err = -EBUSY;
0172 
0173     if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
0174         return -ENOTTY;
0175     if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
0176         return -EFAULT;
0177     log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
0178                CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
0179                CEC_LOG_ADDRS_FL_CDC_ONLY;
0180     mutex_lock(&adap->lock);
0181     if (!adap->is_configuring &&
0182         (!log_addrs.num_log_addrs || !adap->is_configured) &&
0183         !cec_is_busy(adap, fh)) {
0184         err = __cec_s_log_addrs(adap, &log_addrs, block);
0185         if (!err)
0186             log_addrs = adap->log_addrs;
0187     }
0188     mutex_unlock(&adap->lock);
0189     if (err)
0190         return err;
0191     if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
0192         return -EFAULT;
0193     return 0;
0194 }
0195 
0196 static long cec_adap_g_connector_info(struct cec_adapter *adap,
0197                       struct cec_log_addrs __user *parg)
0198 {
0199     int ret = 0;
0200 
0201     if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO))
0202         return -ENOTTY;
0203 
0204     mutex_lock(&adap->lock);
0205     if (copy_to_user(parg, &adap->conn_info, sizeof(adap->conn_info)))
0206         ret = -EFAULT;
0207     mutex_unlock(&adap->lock);
0208     return ret;
0209 }
0210 
0211 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
0212              bool block, struct cec_msg __user *parg)
0213 {
0214     struct cec_msg msg = {};
0215     long err = 0;
0216 
0217     if (!(adap->capabilities & CEC_CAP_TRANSMIT))
0218         return -ENOTTY;
0219     if (copy_from_user(&msg, parg, sizeof(msg)))
0220         return -EFAULT;
0221 
0222     mutex_lock(&adap->lock);
0223     if (adap->log_addrs.num_log_addrs == 0)
0224         err = -EPERM;
0225     else if (adap->is_configuring)
0226         err = -ENONET;
0227     else if (cec_is_busy(adap, fh))
0228         err = -EBUSY;
0229     else
0230         err = cec_transmit_msg_fh(adap, &msg, fh, block);
0231     mutex_unlock(&adap->lock);
0232     if (err)
0233         return err;
0234     if (copy_to_user(parg, &msg, sizeof(msg)))
0235         return -EFAULT;
0236     return 0;
0237 }
0238 
0239 /* Called by CEC_RECEIVE: wait for a message to arrive */
0240 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
0241 {
0242     u32 timeout = msg->timeout;
0243     int res;
0244 
0245     do {
0246         mutex_lock(&fh->lock);
0247         /* Are there received messages queued up? */
0248         if (fh->queued_msgs) {
0249             /* Yes, return the first one */
0250             struct cec_msg_entry *entry =
0251                 list_first_entry(&fh->msgs,
0252                          struct cec_msg_entry, list);
0253 
0254             list_del(&entry->list);
0255             *msg = entry->msg;
0256             kfree(entry);
0257             fh->queued_msgs--;
0258             mutex_unlock(&fh->lock);
0259             /* restore original timeout value */
0260             msg->timeout = timeout;
0261             return 0;
0262         }
0263 
0264         /* No, return EAGAIN in non-blocking mode or wait */
0265         mutex_unlock(&fh->lock);
0266 
0267         /* Return when in non-blocking mode */
0268         if (!block)
0269             return -EAGAIN;
0270 
0271         if (msg->timeout) {
0272             /* The user specified a timeout */
0273             res = wait_event_interruptible_timeout(fh->wait,
0274                                    fh->queued_msgs,
0275                 msecs_to_jiffies(msg->timeout));
0276             if (res == 0)
0277                 res = -ETIMEDOUT;
0278             else if (res > 0)
0279                 res = 0;
0280         } else {
0281             /* Wait indefinitely */
0282             res = wait_event_interruptible(fh->wait,
0283                                fh->queued_msgs);
0284         }
0285         /* Exit on error, otherwise loop to get the new message */
0286     } while (!res);
0287     return res;
0288 }
0289 
0290 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
0291             bool block, struct cec_msg __user *parg)
0292 {
0293     struct cec_msg msg = {};
0294     long err;
0295 
0296     if (copy_from_user(&msg, parg, sizeof(msg)))
0297         return -EFAULT;
0298 
0299     err = cec_receive_msg(fh, &msg, block);
0300     if (err)
0301         return err;
0302     msg.flags = 0;
0303     if (copy_to_user(parg, &msg, sizeof(msg)))
0304         return -EFAULT;
0305     return 0;
0306 }
0307 
0308 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
0309             bool block, struct cec_event __user *parg)
0310 {
0311     struct cec_event_entry *ev = NULL;
0312     u64 ts = ~0ULL;
0313     unsigned int i;
0314     unsigned int ev_idx;
0315     long err = 0;
0316 
0317     mutex_lock(&fh->lock);
0318     while (!fh->total_queued_events && block) {
0319         mutex_unlock(&fh->lock);
0320         err = wait_event_interruptible(fh->wait,
0321                            fh->total_queued_events);
0322         if (err)
0323             return err;
0324         mutex_lock(&fh->lock);
0325     }
0326 
0327     /* Find the oldest event */
0328     for (i = 0; i < CEC_NUM_EVENTS; i++) {
0329         struct cec_event_entry *entry =
0330             list_first_entry_or_null(&fh->events[i],
0331                          struct cec_event_entry, list);
0332 
0333         if (entry && entry->ev.ts <= ts) {
0334             ev = entry;
0335             ev_idx = i;
0336             ts = ev->ev.ts;
0337         }
0338     }
0339 
0340     if (!ev) {
0341         err = -EAGAIN;
0342         goto unlock;
0343     }
0344     list_del(&ev->list);
0345 
0346     if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
0347         err = -EFAULT;
0348     if (ev_idx >= CEC_NUM_CORE_EVENTS)
0349         kfree(ev);
0350     fh->queued_events[ev_idx]--;
0351     fh->total_queued_events--;
0352 
0353 unlock:
0354     mutex_unlock(&fh->lock);
0355     return err;
0356 }
0357 
0358 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
0359                u32 __user *parg)
0360 {
0361     u32 mode = fh->mode_initiator | fh->mode_follower;
0362 
0363     if (copy_to_user(parg, &mode, sizeof(mode)))
0364         return -EFAULT;
0365     return 0;
0366 }
0367 
0368 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
0369                u32 __user *parg)
0370 {
0371     u32 mode;
0372     u8 mode_initiator;
0373     u8 mode_follower;
0374     bool send_pin_event = false;
0375     long err = 0;
0376 
0377     if (copy_from_user(&mode, parg, sizeof(mode)))
0378         return -EFAULT;
0379     if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
0380         dprintk(1, "%s: invalid mode bits set\n", __func__);
0381         return -EINVAL;
0382     }
0383 
0384     mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
0385     mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
0386 
0387     if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
0388         mode_follower > CEC_MODE_MONITOR_ALL) {
0389         dprintk(1, "%s: unknown mode\n", __func__);
0390         return -EINVAL;
0391     }
0392 
0393     if (mode_follower == CEC_MODE_MONITOR_ALL &&
0394         !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
0395         dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
0396         return -EINVAL;
0397     }
0398 
0399     if (mode_follower == CEC_MODE_MONITOR_PIN &&
0400         !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
0401         dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
0402         return -EINVAL;
0403     }
0404 
0405     /* Follower modes should always be able to send CEC messages */
0406     if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
0407          !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
0408         mode_follower >= CEC_MODE_FOLLOWER &&
0409         mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
0410         dprintk(1, "%s: cannot transmit\n", __func__);
0411         return -EINVAL;
0412     }
0413 
0414     /* Monitor modes require CEC_MODE_NO_INITIATOR */
0415     if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
0416         dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
0417             __func__);
0418         return -EINVAL;
0419     }
0420 
0421     /* Monitor modes require CAP_NET_ADMIN */
0422     if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
0423         return -EPERM;
0424 
0425     mutex_lock(&adap->lock);
0426     /*
0427      * You can't become exclusive follower if someone else already
0428      * has that job.
0429      */
0430     if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
0431          mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
0432         adap->cec_follower && adap->cec_follower != fh)
0433         err = -EBUSY;
0434     /*
0435      * You can't become exclusive initiator if someone else already
0436      * has that job.
0437      */
0438     if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
0439         adap->cec_initiator && adap->cec_initiator != fh)
0440         err = -EBUSY;
0441 
0442     if (!err) {
0443         bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
0444         bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
0445 
0446         if (old_mon_all != new_mon_all) {
0447             if (new_mon_all)
0448                 err = cec_monitor_all_cnt_inc(adap);
0449             else
0450                 cec_monitor_all_cnt_dec(adap);
0451         }
0452     }
0453 
0454     if (!err) {
0455         bool old_mon_pin = fh->mode_follower == CEC_MODE_MONITOR_PIN;
0456         bool new_mon_pin = mode_follower == CEC_MODE_MONITOR_PIN;
0457 
0458         if (old_mon_pin != new_mon_pin) {
0459             send_pin_event = new_mon_pin;
0460             if (new_mon_pin)
0461                 err = cec_monitor_pin_cnt_inc(adap);
0462             else
0463                 cec_monitor_pin_cnt_dec(adap);
0464         }
0465     }
0466 
0467     if (err) {
0468         mutex_unlock(&adap->lock);
0469         return err;
0470     }
0471 
0472     if (fh->mode_follower == CEC_MODE_FOLLOWER)
0473         adap->follower_cnt--;
0474     if (mode_follower == CEC_MODE_FOLLOWER)
0475         adap->follower_cnt++;
0476     if (send_pin_event) {
0477         struct cec_event ev = {
0478             .flags = CEC_EVENT_FL_INITIAL_STATE,
0479         };
0480 
0481         ev.event = adap->cec_pin_is_high ? CEC_EVENT_PIN_CEC_HIGH :
0482                            CEC_EVENT_PIN_CEC_LOW;
0483         cec_queue_event_fh(fh, &ev, 0);
0484     }
0485     if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
0486         mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
0487         adap->passthrough =
0488             mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
0489         adap->cec_follower = fh;
0490     } else if (adap->cec_follower == fh) {
0491         adap->passthrough = false;
0492         adap->cec_follower = NULL;
0493     }
0494     if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
0495         adap->cec_initiator = fh;
0496     else if (adap->cec_initiator == fh)
0497         adap->cec_initiator = NULL;
0498     fh->mode_initiator = mode_initiator;
0499     fh->mode_follower = mode_follower;
0500     mutex_unlock(&adap->lock);
0501     return 0;
0502 }
0503 
0504 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0505 {
0506     struct cec_fh *fh = filp->private_data;
0507     struct cec_adapter *adap = fh->adap;
0508     bool block = !(filp->f_flags & O_NONBLOCK);
0509     void __user *parg = (void __user *)arg;
0510 
0511     if (!cec_is_registered(adap))
0512         return -ENODEV;
0513 
0514     switch (cmd) {
0515     case CEC_ADAP_G_CAPS:
0516         return cec_adap_g_caps(adap, parg);
0517 
0518     case CEC_ADAP_G_PHYS_ADDR:
0519         return cec_adap_g_phys_addr(adap, parg);
0520 
0521     case CEC_ADAP_S_PHYS_ADDR:
0522         return cec_adap_s_phys_addr(adap, fh, block, parg);
0523 
0524     case CEC_ADAP_G_LOG_ADDRS:
0525         return cec_adap_g_log_addrs(adap, parg);
0526 
0527     case CEC_ADAP_S_LOG_ADDRS:
0528         return cec_adap_s_log_addrs(adap, fh, block, parg);
0529 
0530     case CEC_ADAP_G_CONNECTOR_INFO:
0531         return cec_adap_g_connector_info(adap, parg);
0532 
0533     case CEC_TRANSMIT:
0534         return cec_transmit(adap, fh, block, parg);
0535 
0536     case CEC_RECEIVE:
0537         return cec_receive(adap, fh, block, parg);
0538 
0539     case CEC_DQEVENT:
0540         return cec_dqevent(adap, fh, block, parg);
0541 
0542     case CEC_G_MODE:
0543         return cec_g_mode(adap, fh, parg);
0544 
0545     case CEC_S_MODE:
0546         return cec_s_mode(adap, fh, parg);
0547 
0548     default:
0549         return -ENOTTY;
0550     }
0551 }
0552 
0553 static int cec_open(struct inode *inode, struct file *filp)
0554 {
0555     struct cec_devnode *devnode =
0556         container_of(inode->i_cdev, struct cec_devnode, cdev);
0557     struct cec_adapter *adap = to_cec_adapter(devnode);
0558     struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
0559     /*
0560      * Initial events that are automatically sent when the cec device is
0561      * opened.
0562      */
0563     struct cec_event ev = {
0564         .event = CEC_EVENT_STATE_CHANGE,
0565         .flags = CEC_EVENT_FL_INITIAL_STATE,
0566     };
0567     unsigned int i;
0568     int err;
0569 
0570     if (!fh)
0571         return -ENOMEM;
0572 
0573     INIT_LIST_HEAD(&fh->msgs);
0574     INIT_LIST_HEAD(&fh->xfer_list);
0575     for (i = 0; i < CEC_NUM_EVENTS; i++)
0576         INIT_LIST_HEAD(&fh->events[i]);
0577     mutex_init(&fh->lock);
0578     init_waitqueue_head(&fh->wait);
0579 
0580     fh->mode_initiator = CEC_MODE_INITIATOR;
0581     fh->adap = adap;
0582 
0583     err = cec_get_device(devnode);
0584     if (err) {
0585         kfree(fh);
0586         return err;
0587     }
0588 
0589     filp->private_data = fh;
0590 
0591     /* Queue up initial state events */
0592     ev.state_change.phys_addr = adap->phys_addr;
0593     ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
0594     ev.state_change.have_conn_info =
0595         adap->conn_info.type != CEC_CONNECTOR_TYPE_NO_CONNECTOR;
0596     cec_queue_event_fh(fh, &ev, 0);
0597 #ifdef CONFIG_CEC_PIN
0598     if (adap->pin && adap->pin->ops->read_hpd &&
0599         !adap->devnode.unregistered) {
0600         err = adap->pin->ops->read_hpd(adap);
0601         if (err >= 0) {
0602             ev.event = err ? CEC_EVENT_PIN_HPD_HIGH :
0603                      CEC_EVENT_PIN_HPD_LOW;
0604             cec_queue_event_fh(fh, &ev, 0);
0605         }
0606     }
0607     if (adap->pin && adap->pin->ops->read_5v &&
0608         !adap->devnode.unregistered) {
0609         err = adap->pin->ops->read_5v(adap);
0610         if (err >= 0) {
0611             ev.event = err ? CEC_EVENT_PIN_5V_HIGH :
0612                      CEC_EVENT_PIN_5V_LOW;
0613             cec_queue_event_fh(fh, &ev, 0);
0614         }
0615     }
0616 #endif
0617 
0618     mutex_lock(&devnode->lock);
0619     mutex_lock(&devnode->lock_fhs);
0620     list_add(&fh->list, &devnode->fhs);
0621     mutex_unlock(&devnode->lock_fhs);
0622     mutex_unlock(&devnode->lock);
0623 
0624     return 0;
0625 }
0626 
0627 /* Override for the release function */
0628 static int cec_release(struct inode *inode, struct file *filp)
0629 {
0630     struct cec_devnode *devnode = cec_devnode_data(filp);
0631     struct cec_adapter *adap = to_cec_adapter(devnode);
0632     struct cec_fh *fh = filp->private_data;
0633     unsigned int i;
0634 
0635     mutex_lock(&adap->lock);
0636     if (adap->cec_initiator == fh)
0637         adap->cec_initiator = NULL;
0638     if (adap->cec_follower == fh) {
0639         adap->cec_follower = NULL;
0640         adap->passthrough = false;
0641     }
0642     if (fh->mode_follower == CEC_MODE_FOLLOWER)
0643         adap->follower_cnt--;
0644     if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
0645         cec_monitor_pin_cnt_dec(adap);
0646     if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
0647         cec_monitor_all_cnt_dec(adap);
0648     mutex_unlock(&adap->lock);
0649 
0650     mutex_lock(&devnode->lock);
0651     mutex_lock(&devnode->lock_fhs);
0652     list_del(&fh->list);
0653     mutex_unlock(&devnode->lock_fhs);
0654     mutex_unlock(&devnode->lock);
0655 
0656     /* Unhook pending transmits from this filehandle. */
0657     mutex_lock(&adap->lock);
0658     while (!list_empty(&fh->xfer_list)) {
0659         struct cec_data *data =
0660             list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
0661 
0662         data->blocking = false;
0663         data->fh = NULL;
0664         list_del_init(&data->xfer_list);
0665     }
0666     mutex_unlock(&adap->lock);
0667     while (!list_empty(&fh->msgs)) {
0668         struct cec_msg_entry *entry =
0669             list_first_entry(&fh->msgs, struct cec_msg_entry, list);
0670 
0671         list_del(&entry->list);
0672         kfree(entry);
0673     }
0674     for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
0675         while (!list_empty(&fh->events[i])) {
0676             struct cec_event_entry *entry =
0677                 list_first_entry(&fh->events[i],
0678                          struct cec_event_entry, list);
0679 
0680             list_del(&entry->list);
0681             kfree(entry);
0682         }
0683     }
0684     kfree(fh);
0685 
0686     cec_put_device(devnode);
0687     filp->private_data = NULL;
0688     return 0;
0689 }
0690 
0691 const struct file_operations cec_devnode_fops = {
0692     .owner = THIS_MODULE,
0693     .open = cec_open,
0694     .unlocked_ioctl = cec_ioctl,
0695     .compat_ioctl = cec_ioctl,
0696     .release = cec_release,
0697     .poll = cec_poll,
0698     .llseek = no_llseek,
0699 };