xref: /linux/drivers/media/cec/core/cec-api.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * cec-api.c - HDMI Consumer Electronics Control framework - API
4  *
5  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/kmod.h>
13 #include <linux/ktime.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/version.h>
20 
21 #include <media/cec-pin.h>
22 #include "cec-priv.h"
23 #include "cec-pin-priv.h"
24 
25 static inline struct cec_devnode *cec_devnode_data(struct file *filp)
26 {
27 	struct cec_fh *fh = filp->private_data;
28 
29 	return &fh->adap->devnode;
30 }
31 
32 /* CEC file operations */
33 
34 static __poll_t cec_poll(struct file *filp,
35 			     struct poll_table_struct *poll)
36 {
37 	struct cec_fh *fh = filp->private_data;
38 	struct cec_adapter *adap = fh->adap;
39 	__poll_t res = 0;
40 
41 	poll_wait(filp, &fh->wait, poll);
42 	if (!cec_is_registered(adap))
43 		return EPOLLERR | EPOLLHUP | EPOLLPRI;
44 	mutex_lock(&adap->lock);
45 	if (adap->is_configured &&
46 	    adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
47 		res |= EPOLLOUT | EPOLLWRNORM;
48 	if (fh->queued_msgs)
49 		res |= EPOLLIN | EPOLLRDNORM;
50 	if (fh->total_queued_events)
51 		res |= EPOLLPRI;
52 	mutex_unlock(&adap->lock);
53 	return res;
54 }
55 
56 static bool cec_is_busy(const struct cec_adapter *adap,
57 			const struct cec_fh *fh)
58 {
59 	bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
60 	bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
61 
62 	/*
63 	 * Exclusive initiators and followers can always access the CEC adapter
64 	 */
65 	if (valid_initiator || valid_follower)
66 		return false;
67 	/*
68 	 * All others can only access the CEC adapter if there is no
69 	 * exclusive initiator and they are in INITIATOR mode.
70 	 */
71 	return adap->cec_initiator ||
72 	       fh->mode_initiator == CEC_MODE_NO_INITIATOR;
73 }
74 
75 static long cec_adap_g_caps(struct cec_adapter *adap,
76 			    struct cec_caps __user *parg)
77 {
78 	struct cec_caps caps = {};
79 
80 	strscpy(caps.driver, adap->devnode.dev.parent->driver->name,
81 		sizeof(caps.driver));
82 	strscpy(caps.name, adap->name, sizeof(caps.name));
83 	caps.available_log_addrs = adap->available_log_addrs;
84 	caps.capabilities = adap->capabilities;
85 	caps.version = LINUX_VERSION_CODE;
86 	if (copy_to_user(parg, &caps, sizeof(caps)))
87 		return -EFAULT;
88 	return 0;
89 }
90 
91 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
92 				 __u16 __user *parg)
93 {
94 	u16 phys_addr;
95 
96 	mutex_lock(&adap->lock);
97 	phys_addr = adap->phys_addr;
98 	mutex_unlock(&adap->lock);
99 	if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
100 		return -EFAULT;
101 	return 0;
102 }
103 
104 static int cec_validate_phys_addr(u16 phys_addr)
105 {
106 	int i;
107 
108 	if (phys_addr == CEC_PHYS_ADDR_INVALID)
109 		return 0;
110 	for (i = 0; i < 16; i += 4)
111 		if (phys_addr & (0xf << i))
112 			break;
113 	if (i == 16)
114 		return 0;
115 	for (i += 4; i < 16; i += 4)
116 		if ((phys_addr & (0xf << i)) == 0)
117 			return -EINVAL;
118 	return 0;
119 }
120 
121 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
122 				 bool block, __u16 __user *parg)
123 {
124 	u16 phys_addr;
125 	long err;
126 
127 	if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
128 		return -ENOTTY;
129 	if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
130 		return -EFAULT;
131 
132 	err = cec_validate_phys_addr(phys_addr);
133 	if (err)
134 		return err;
135 	mutex_lock(&adap->lock);
136 	if (cec_is_busy(adap, fh))
137 		err = -EBUSY;
138 	else
139 		__cec_s_phys_addr(adap, phys_addr, block);
140 	mutex_unlock(&adap->lock);
141 	return err;
142 }
143 
144 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
145 				 struct cec_log_addrs __user *parg)
146 {
147 	struct cec_log_addrs log_addrs;
148 
149 	mutex_lock(&adap->lock);
150 	/*
151 	 * We use memcpy here instead of assignment since there is a
152 	 * hole at the end of struct cec_log_addrs that an assignment
153 	 * might ignore. So when we do copy_to_user() we could leak
154 	 * one byte of memory.
155 	 */
156 	memcpy(&log_addrs, &adap->log_addrs, sizeof(log_addrs));
157 	if (!adap->is_configured)
158 		memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
159 		       sizeof(log_addrs.log_addr));
160 	mutex_unlock(&adap->lock);
161 
162 	if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
163 		return -EFAULT;
164 	return 0;
165 }
166 
167 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
168 				 bool block, struct cec_log_addrs __user *parg)
169 {
170 	struct cec_log_addrs log_addrs;
171 	long err = -EBUSY;
172 
173 	if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
174 		return -ENOTTY;
175 	if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
176 		return -EFAULT;
177 	log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
178 			   CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
179 			   CEC_LOG_ADDRS_FL_CDC_ONLY;
180 	mutex_lock(&adap->lock);
181 	if (!adap->is_claiming_log_addrs && !adap->is_configuring &&
182 	    (!log_addrs.num_log_addrs || !adap->is_configured) &&
183 	    !cec_is_busy(adap, fh)) {
184 		err = __cec_s_log_addrs(adap, &log_addrs, block);
185 		if (!err)
186 			log_addrs = adap->log_addrs;
187 	}
188 	mutex_unlock(&adap->lock);
189 	if (err)
190 		return err;
191 	if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
192 		return -EFAULT;
193 	return 0;
194 }
195 
196 static long cec_adap_g_connector_info(struct cec_adapter *adap,
197 				      struct cec_log_addrs __user *parg)
198 {
199 	int ret = 0;
200 
201 	if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO))
202 		return -ENOTTY;
203 
204 	mutex_lock(&adap->lock);
205 	if (copy_to_user(parg, &adap->conn_info, sizeof(adap->conn_info)))
206 		ret = -EFAULT;
207 	mutex_unlock(&adap->lock);
208 	return ret;
209 }
210 
211 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
212 			 bool block, struct cec_msg __user *parg)
213 {
214 	struct cec_msg msg = {};
215 	long err = 0;
216 
217 	if (!(adap->capabilities & CEC_CAP_TRANSMIT))
218 		return -ENOTTY;
219 	if (copy_from_user(&msg, parg, sizeof(msg)))
220 		return -EFAULT;
221 
222 	mutex_lock(&adap->lock);
223 	if (adap->log_addrs.num_log_addrs == 0)
224 		err = -EPERM;
225 	else if (adap->is_configuring && !msg_is_raw(&msg))
226 		err = -ENONET;
227 	else if (cec_is_busy(adap, fh))
228 		err = -EBUSY;
229 	else
230 		err = cec_transmit_msg_fh(adap, &msg, fh, block);
231 	mutex_unlock(&adap->lock);
232 	if (err)
233 		return err;
234 	if (copy_to_user(parg, &msg, sizeof(msg)))
235 		return -EFAULT;
236 	return 0;
237 }
238 
239 /* Called by CEC_RECEIVE: wait for a message to arrive */
240 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
241 {
242 	u32 timeout = msg->timeout;
243 	int res;
244 
245 	do {
246 		mutex_lock(&fh->lock);
247 		/* Are there received messages queued up? */
248 		if (fh->queued_msgs) {
249 			/* Yes, return the first one */
250 			struct cec_msg_entry *entry =
251 				list_first_entry(&fh->msgs,
252 						 struct cec_msg_entry, list);
253 
254 			list_del(&entry->list);
255 			*msg = entry->msg;
256 			kfree(entry);
257 			fh->queued_msgs--;
258 			mutex_unlock(&fh->lock);
259 			/* restore original timeout value */
260 			msg->timeout = timeout;
261 			return 0;
262 		}
263 
264 		/* No, return EAGAIN in non-blocking mode or wait */
265 		mutex_unlock(&fh->lock);
266 
267 		/* Return when in non-blocking mode */
268 		if (!block)
269 			return -EAGAIN;
270 
271 		if (msg->timeout) {
272 			/* The user specified a timeout */
273 			res = wait_event_interruptible_timeout(fh->wait,
274 							       fh->queued_msgs,
275 				msecs_to_jiffies(msg->timeout));
276 			if (res == 0)
277 				res = -ETIMEDOUT;
278 			else if (res > 0)
279 				res = 0;
280 		} else {
281 			/* Wait indefinitely */
282 			res = wait_event_interruptible(fh->wait,
283 						       fh->queued_msgs);
284 		}
285 		/* Exit on error, otherwise loop to get the new message */
286 	} while (!res);
287 	return res;
288 }
289 
290 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
291 			bool block, struct cec_msg __user *parg)
292 {
293 	struct cec_msg msg = {};
294 	long err;
295 
296 	if (copy_from_user(&msg, parg, sizeof(msg)))
297 		return -EFAULT;
298 
299 	err = cec_receive_msg(fh, &msg, block);
300 	if (err)
301 		return err;
302 	msg.flags = 0;
303 	if (copy_to_user(parg, &msg, sizeof(msg)))
304 		return -EFAULT;
305 	return 0;
306 }
307 
308 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
309 			bool block, struct cec_event __user *parg)
310 {
311 	struct cec_event_entry *ev = NULL;
312 	u64 ts = ~0ULL;
313 	unsigned int i;
314 	unsigned int ev_idx;
315 	long err = 0;
316 
317 	mutex_lock(&fh->lock);
318 	while (!fh->total_queued_events && block) {
319 		mutex_unlock(&fh->lock);
320 		err = wait_event_interruptible(fh->wait,
321 					       fh->total_queued_events);
322 		if (err)
323 			return err;
324 		mutex_lock(&fh->lock);
325 	}
326 
327 	/* Find the oldest event */
328 	for (i = 0; i < CEC_NUM_EVENTS; i++) {
329 		struct cec_event_entry *entry =
330 			list_first_entry_or_null(&fh->events[i],
331 						 struct cec_event_entry, list);
332 
333 		if (entry && entry->ev.ts <= ts) {
334 			ev = entry;
335 			ev_idx = i;
336 			ts = ev->ev.ts;
337 		}
338 	}
339 
340 	if (!ev) {
341 		err = -EAGAIN;
342 		goto unlock;
343 	}
344 	list_del(&ev->list);
345 
346 	if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
347 		err = -EFAULT;
348 	kfree(ev);
349 	fh->queued_events[ev_idx]--;
350 	fh->total_queued_events--;
351 
352 unlock:
353 	mutex_unlock(&fh->lock);
354 	return err;
355 }
356 
357 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
358 		       u32 __user *parg)
359 {
360 	u32 mode = fh->mode_initiator | fh->mode_follower;
361 
362 	if (copy_to_user(parg, &mode, sizeof(mode)))
363 		return -EFAULT;
364 	return 0;
365 }
366 
367 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
368 		       u32 __user *parg)
369 {
370 	u32 mode;
371 	u8 mode_initiator;
372 	u8 mode_follower;
373 	bool send_pin_event = false;
374 	long err = 0;
375 
376 	if (copy_from_user(&mode, parg, sizeof(mode)))
377 		return -EFAULT;
378 	if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
379 		dprintk(1, "%s: invalid mode bits set\n", __func__);
380 		return -EINVAL;
381 	}
382 
383 	mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
384 	mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
385 
386 	if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
387 	    mode_follower > CEC_MODE_MONITOR_ALL) {
388 		dprintk(1, "%s: unknown mode\n", __func__);
389 		return -EINVAL;
390 	}
391 
392 	if (mode_follower == CEC_MODE_MONITOR_ALL &&
393 	    !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
394 		dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
395 		return -EINVAL;
396 	}
397 
398 	if (mode_follower == CEC_MODE_MONITOR_PIN &&
399 	    !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
400 		dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
401 		return -EINVAL;
402 	}
403 
404 	/* Follower modes should always be able to send CEC messages */
405 	if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
406 	     !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
407 	    mode_follower >= CEC_MODE_FOLLOWER &&
408 	    mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
409 		dprintk(1, "%s: cannot transmit\n", __func__);
410 		return -EINVAL;
411 	}
412 
413 	/* Monitor modes require CEC_MODE_NO_INITIATOR */
414 	if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
415 		dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
416 			__func__);
417 		return -EINVAL;
418 	}
419 
420 	/* Monitor modes require CAP_NET_ADMIN */
421 	if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
422 		return -EPERM;
423 
424 	mutex_lock(&adap->lock);
425 	/*
426 	 * You can't become exclusive follower if someone else already
427 	 * has that job.
428 	 */
429 	if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
430 	     mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
431 	    adap->cec_follower && adap->cec_follower != fh)
432 		err = -EBUSY;
433 	/*
434 	 * You can't become exclusive initiator if someone else already
435 	 * has that job.
436 	 */
437 	if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
438 	    adap->cec_initiator && adap->cec_initiator != fh)
439 		err = -EBUSY;
440 
441 	if (!err) {
442 		bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
443 		bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
444 
445 		if (old_mon_all != new_mon_all) {
446 			if (new_mon_all)
447 				err = cec_monitor_all_cnt_inc(adap);
448 			else
449 				cec_monitor_all_cnt_dec(adap);
450 		}
451 	}
452 
453 	if (!err) {
454 		bool old_mon_pin = fh->mode_follower == CEC_MODE_MONITOR_PIN;
455 		bool new_mon_pin = mode_follower == CEC_MODE_MONITOR_PIN;
456 
457 		if (old_mon_pin != new_mon_pin) {
458 			send_pin_event = new_mon_pin;
459 			if (new_mon_pin)
460 				err = cec_monitor_pin_cnt_inc(adap);
461 			else
462 				cec_monitor_pin_cnt_dec(adap);
463 		}
464 	}
465 
466 	if (err) {
467 		mutex_unlock(&adap->lock);
468 		return err;
469 	}
470 
471 	if (fh->mode_follower == CEC_MODE_FOLLOWER)
472 		adap->follower_cnt--;
473 	if (mode_follower == CEC_MODE_FOLLOWER)
474 		adap->follower_cnt++;
475 	if (send_pin_event) {
476 		struct cec_event ev = {
477 			.flags = CEC_EVENT_FL_INITIAL_STATE,
478 		};
479 
480 		ev.event = adap->cec_pin_is_high ? CEC_EVENT_PIN_CEC_HIGH :
481 						   CEC_EVENT_PIN_CEC_LOW;
482 		cec_queue_event_fh(fh, &ev, 0);
483 	}
484 	if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
485 	    mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
486 		adap->passthrough =
487 			mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
488 		adap->cec_follower = fh;
489 	} else if (adap->cec_follower == fh) {
490 		adap->passthrough = false;
491 		adap->cec_follower = NULL;
492 	}
493 	if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
494 		adap->cec_initiator = fh;
495 	else if (adap->cec_initiator == fh)
496 		adap->cec_initiator = NULL;
497 	fh->mode_initiator = mode_initiator;
498 	fh->mode_follower = mode_follower;
499 	mutex_unlock(&adap->lock);
500 	return 0;
501 }
502 
503 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
504 {
505 	struct cec_fh *fh = filp->private_data;
506 	struct cec_adapter *adap = fh->adap;
507 	bool block = !(filp->f_flags & O_NONBLOCK);
508 	void __user *parg = (void __user *)arg;
509 
510 	if (!cec_is_registered(adap))
511 		return -ENODEV;
512 
513 	switch (cmd) {
514 	case CEC_ADAP_G_CAPS:
515 		return cec_adap_g_caps(adap, parg);
516 
517 	case CEC_ADAP_G_PHYS_ADDR:
518 		return cec_adap_g_phys_addr(adap, parg);
519 
520 	case CEC_ADAP_S_PHYS_ADDR:
521 		return cec_adap_s_phys_addr(adap, fh, block, parg);
522 
523 	case CEC_ADAP_G_LOG_ADDRS:
524 		return cec_adap_g_log_addrs(adap, parg);
525 
526 	case CEC_ADAP_S_LOG_ADDRS:
527 		return cec_adap_s_log_addrs(adap, fh, block, parg);
528 
529 	case CEC_ADAP_G_CONNECTOR_INFO:
530 		return cec_adap_g_connector_info(adap, parg);
531 
532 	case CEC_TRANSMIT:
533 		return cec_transmit(adap, fh, block, parg);
534 
535 	case CEC_RECEIVE:
536 		return cec_receive(adap, fh, block, parg);
537 
538 	case CEC_DQEVENT:
539 		return cec_dqevent(adap, fh, block, parg);
540 
541 	case CEC_G_MODE:
542 		return cec_g_mode(adap, fh, parg);
543 
544 	case CEC_S_MODE:
545 		return cec_s_mode(adap, fh, parg);
546 
547 	default:
548 		return -ENOTTY;
549 	}
550 }
551 
552 static int cec_open(struct inode *inode, struct file *filp)
553 {
554 	struct cec_devnode *devnode =
555 		container_of(inode->i_cdev, struct cec_devnode, cdev);
556 	struct cec_adapter *adap = to_cec_adapter(devnode);
557 	struct cec_fh *fh = kzalloc_obj(*fh);
558 	/*
559 	 * Initial events that are automatically sent when the cec device is
560 	 * opened.
561 	 */
562 	struct cec_event ev = {
563 		.event = CEC_EVENT_STATE_CHANGE,
564 		.flags = CEC_EVENT_FL_INITIAL_STATE,
565 	};
566 	unsigned int i;
567 	int err;
568 
569 	if (!fh)
570 		return -ENOMEM;
571 
572 	INIT_LIST_HEAD(&fh->msgs);
573 	INIT_LIST_HEAD(&fh->xfer_list);
574 	for (i = 0; i < CEC_NUM_EVENTS; i++)
575 		INIT_LIST_HEAD(&fh->events[i]);
576 	mutex_init(&fh->lock);
577 	init_waitqueue_head(&fh->wait);
578 
579 	fh->mode_initiator = CEC_MODE_INITIATOR;
580 	fh->adap = adap;
581 
582 	err = cec_get_device(adap);
583 	if (err) {
584 		kfree(fh);
585 		return err;
586 	}
587 
588 	filp->private_data = fh;
589 
590 	/* Queue up initial state events */
591 	ev.state_change.phys_addr = adap->phys_addr;
592 	ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
593 	ev.state_change.have_conn_info =
594 		adap->conn_info.type != CEC_CONNECTOR_TYPE_NO_CONNECTOR;
595 	cec_queue_event_fh(fh, &ev, 0);
596 #ifdef CONFIG_CEC_PIN
597 	if (adap->pin && adap->pin->ops->read_hpd &&
598 	    !adap->devnode.unregistered) {
599 		err = adap->pin->ops->read_hpd(adap);
600 		if (err >= 0) {
601 			ev.event = err ? CEC_EVENT_PIN_HPD_HIGH :
602 					 CEC_EVENT_PIN_HPD_LOW;
603 			cec_queue_event_fh(fh, &ev, 0);
604 		}
605 	}
606 	if (adap->pin && adap->pin->ops->read_5v &&
607 	    !adap->devnode.unregistered) {
608 		err = adap->pin->ops->read_5v(adap);
609 		if (err >= 0) {
610 			ev.event = err ? CEC_EVENT_PIN_5V_HIGH :
611 					 CEC_EVENT_PIN_5V_LOW;
612 			cec_queue_event_fh(fh, &ev, 0);
613 		}
614 	}
615 #endif
616 
617 	mutex_lock(&devnode->lock);
618 	mutex_lock(&devnode->lock_fhs);
619 	list_add(&fh->list, &devnode->fhs);
620 	mutex_unlock(&devnode->lock_fhs);
621 	mutex_unlock(&devnode->lock);
622 
623 	return 0;
624 }
625 
626 /* Override for the release function */
627 static int cec_release(struct inode *inode, struct file *filp)
628 {
629 	struct cec_devnode *devnode = cec_devnode_data(filp);
630 	struct cec_adapter *adap = to_cec_adapter(devnode);
631 	struct cec_fh *fh = filp->private_data;
632 	unsigned int i;
633 
634 	mutex_lock(&adap->lock);
635 	if (adap->cec_initiator == fh)
636 		adap->cec_initiator = NULL;
637 	if (adap->cec_follower == fh) {
638 		adap->cec_follower = NULL;
639 		adap->passthrough = false;
640 	}
641 	if (fh->mode_follower == CEC_MODE_FOLLOWER)
642 		adap->follower_cnt--;
643 	if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
644 		cec_monitor_pin_cnt_dec(adap);
645 	if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
646 		cec_monitor_all_cnt_dec(adap);
647 	mutex_unlock(&adap->lock);
648 
649 	mutex_lock(&devnode->lock);
650 	mutex_lock(&devnode->lock_fhs);
651 	list_del(&fh->list);
652 	mutex_unlock(&devnode->lock_fhs);
653 	mutex_unlock(&devnode->lock);
654 
655 	/* Unhook pending transmits from this filehandle. */
656 	mutex_lock(&adap->lock);
657 	while (!list_empty(&fh->xfer_list)) {
658 		struct cec_data *data =
659 			list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
660 
661 		data->blocking = false;
662 		data->fh = NULL;
663 		list_del_init(&data->xfer_list);
664 	}
665 	mutex_unlock(&adap->lock);
666 
667 	mutex_lock(&fh->lock);
668 	while (!list_empty(&fh->msgs)) {
669 		struct cec_msg_entry *entry =
670 			list_first_entry(&fh->msgs, struct cec_msg_entry, list);
671 
672 		list_del(&entry->list);
673 		kfree(entry);
674 	}
675 	for (i = 0; i < CEC_NUM_EVENTS; i++) {
676 		while (!list_empty(&fh->events[i])) {
677 			struct cec_event_entry *entry =
678 				list_first_entry(&fh->events[i],
679 						 struct cec_event_entry, list);
680 
681 			list_del(&entry->list);
682 			kfree(entry);
683 		}
684 	}
685 	mutex_unlock(&fh->lock);
686 	kfree(fh);
687 
688 	cec_put_device(adap);
689 	filp->private_data = NULL;
690 	return 0;
691 }
692 
693 const struct file_operations cec_devnode_fops = {
694 	.owner = THIS_MODULE,
695 	.open = cec_open,
696 	.unlocked_ioctl = cec_ioctl,
697 	.compat_ioctl = cec_ioctl,
698 	.release = cec_release,
699 	.poll = cec_poll,
700 };
701