GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / cec / cec-api.c
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         if (!cec_is_registered(adap))
42                 return EPOLLERR | EPOLLHUP;
43         mutex_lock(&adap->lock);
44         if (adap->is_configured &&
45             adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
46                 res |= EPOLLOUT | EPOLLWRNORM;
47         if (fh->queued_msgs)
48                 res |= EPOLLIN | EPOLLRDNORM;
49         if (fh->total_queued_events)
50                 res |= EPOLLPRI;
51         poll_wait(filp, &fh->wait, poll);
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         strlcpy(caps.driver, adap->devnode.dev.parent->driver->name,
81                 sizeof(caps.driver));
82         strlcpy(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_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_transmit(struct cec_adapter *adap, struct cec_fh *fh,
197                          bool block, struct cec_msg __user *parg)
198 {
199         struct cec_msg msg = {};
200         long err = 0;
201
202         if (!(adap->capabilities & CEC_CAP_TRANSMIT))
203                 return -ENOTTY;
204         if (copy_from_user(&msg, parg, sizeof(msg)))
205                 return -EFAULT;
206
207         /* A CDC-Only device can only send CDC messages */
208         if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
209             (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE))
210                 return -EINVAL;
211
212         mutex_lock(&adap->lock);
213         if (adap->log_addrs.num_log_addrs == 0)
214                 err = -EPERM;
215         else if (adap->is_configuring)
216                 err = -ENONET;
217         else if (!adap->is_configured &&
218                  (adap->needs_hpd || msg.msg[0] != 0xf0))
219                 err = -ENONET;
220         else if (cec_is_busy(adap, fh))
221                 err = -EBUSY;
222         else
223                 err = cec_transmit_msg_fh(adap, &msg, fh, block);
224         mutex_unlock(&adap->lock);
225         if (err)
226                 return err;
227         if (copy_to_user(parg, &msg, sizeof(msg)))
228                 return -EFAULT;
229         return 0;
230 }
231
232 /* Called by CEC_RECEIVE: wait for a message to arrive */
233 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
234 {
235         u32 timeout = msg->timeout;
236         int res;
237
238         do {
239                 mutex_lock(&fh->lock);
240                 /* Are there received messages queued up? */
241                 if (fh->queued_msgs) {
242                         /* Yes, return the first one */
243                         struct cec_msg_entry *entry =
244                                 list_first_entry(&fh->msgs,
245                                                  struct cec_msg_entry, list);
246
247                         list_del(&entry->list);
248                         *msg = entry->msg;
249                         kfree(entry);
250                         fh->queued_msgs--;
251                         mutex_unlock(&fh->lock);
252                         /* restore original timeout value */
253                         msg->timeout = timeout;
254                         return 0;
255                 }
256
257                 /* No, return EAGAIN in non-blocking mode or wait */
258                 mutex_unlock(&fh->lock);
259
260                 /* Return when in non-blocking mode */
261                 if (!block)
262                         return -EAGAIN;
263
264                 if (msg->timeout) {
265                         /* The user specified a timeout */
266                         res = wait_event_interruptible_timeout(fh->wait,
267                                                                fh->queued_msgs,
268                                 msecs_to_jiffies(msg->timeout));
269                         if (res == 0)
270                                 res = -ETIMEDOUT;
271                         else if (res > 0)
272                                 res = 0;
273                 } else {
274                         /* Wait indefinitely */
275                         res = wait_event_interruptible(fh->wait,
276                                                        fh->queued_msgs);
277                 }
278                 /* Exit on error, otherwise loop to get the new message */
279         } while (!res);
280         return res;
281 }
282
283 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
284                         bool block, struct cec_msg __user *parg)
285 {
286         struct cec_msg msg = {};
287         long err;
288
289         if (copy_from_user(&msg, parg, sizeof(msg)))
290                 return -EFAULT;
291
292         err = cec_receive_msg(fh, &msg, block);
293         if (err)
294                 return err;
295         msg.flags = 0;
296         if (copy_to_user(parg, &msg, sizeof(msg)))
297                 return -EFAULT;
298         return 0;
299 }
300
301 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
302                         bool block, struct cec_event __user *parg)
303 {
304         struct cec_event_entry *ev = NULL;
305         u64 ts = ~0ULL;
306         unsigned int i;
307         unsigned int ev_idx;
308         long err = 0;
309
310         mutex_lock(&fh->lock);
311         while (!fh->total_queued_events && block) {
312                 mutex_unlock(&fh->lock);
313                 err = wait_event_interruptible(fh->wait,
314                                                fh->total_queued_events);
315                 if (err)
316                         return err;
317                 mutex_lock(&fh->lock);
318         }
319
320         /* Find the oldest event */
321         for (i = 0; i < CEC_NUM_EVENTS; i++) {
322                 struct cec_event_entry *entry =
323                         list_first_entry_or_null(&fh->events[i],
324                                                  struct cec_event_entry, list);
325
326                 if (entry && entry->ev.ts <= ts) {
327                         ev = entry;
328                         ev_idx = i;
329                         ts = ev->ev.ts;
330                 }
331         }
332
333         if (!ev) {
334                 err = -EAGAIN;
335                 goto unlock;
336         }
337         list_del(&ev->list);
338
339         if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
340                 err = -EFAULT;
341         if (ev_idx >= CEC_NUM_CORE_EVENTS)
342                 kfree(ev);
343         fh->queued_events[ev_idx]--;
344         fh->total_queued_events--;
345
346 unlock:
347         mutex_unlock(&fh->lock);
348         return err;
349 }
350
351 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
352                        u32 __user *parg)
353 {
354         u32 mode = fh->mode_initiator | fh->mode_follower;
355
356         if (copy_to_user(parg, &mode, sizeof(mode)))
357                 return -EFAULT;
358         return 0;
359 }
360
361 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
362                        u32 __user *parg)
363 {
364         u32 mode;
365         u8 mode_initiator;
366         u8 mode_follower;
367         bool send_pin_event = false;
368         long err = 0;
369
370         if (copy_from_user(&mode, parg, sizeof(mode)))
371                 return -EFAULT;
372         if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
373                 dprintk(1, "%s: invalid mode bits set\n", __func__);
374                 return -EINVAL;
375         }
376
377         mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
378         mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
379
380         if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
381             mode_follower > CEC_MODE_MONITOR_ALL) {
382                 dprintk(1, "%s: unknown mode\n", __func__);
383                 return -EINVAL;
384         }
385
386         if (mode_follower == CEC_MODE_MONITOR_ALL &&
387             !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
388                 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
389                 return -EINVAL;
390         }
391
392         if (mode_follower == CEC_MODE_MONITOR_PIN &&
393             !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
394                 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
395                 return -EINVAL;
396         }
397
398         /* Follower modes should always be able to send CEC messages */
399         if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
400              !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
401             mode_follower >= CEC_MODE_FOLLOWER &&
402             mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
403                 dprintk(1, "%s: cannot transmit\n", __func__);
404                 return -EINVAL;
405         }
406
407         /* Monitor modes require CEC_MODE_NO_INITIATOR */
408         if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
409                 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
410                         __func__);
411                 return -EINVAL;
412         }
413
414         /* Monitor modes require CAP_NET_ADMIN */
415         if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
416                 return -EPERM;
417
418         mutex_lock(&adap->lock);
419         /*
420          * You can't become exclusive follower if someone else already
421          * has that job.
422          */
423         if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
424              mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
425             adap->cec_follower && adap->cec_follower != fh)
426                 err = -EBUSY;
427         /*
428          * You can't become exclusive initiator if someone else already
429          * has that job.
430          */
431         if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
432             adap->cec_initiator && adap->cec_initiator != fh)
433                 err = -EBUSY;
434
435         if (!err) {
436                 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
437                 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
438
439                 if (old_mon_all != new_mon_all) {
440                         if (new_mon_all)
441                                 err = cec_monitor_all_cnt_inc(adap);
442                         else
443                                 cec_monitor_all_cnt_dec(adap);
444                 }
445         }
446
447         if (!err) {
448                 bool old_mon_pin = fh->mode_follower == CEC_MODE_MONITOR_PIN;
449                 bool new_mon_pin = mode_follower == CEC_MODE_MONITOR_PIN;
450
451                 if (old_mon_pin != new_mon_pin) {
452                         send_pin_event = new_mon_pin;
453                         if (new_mon_pin)
454                                 err = cec_monitor_pin_cnt_inc(adap);
455                         else
456                                 cec_monitor_pin_cnt_dec(adap);
457                 }
458         }
459
460         if (err) {
461                 mutex_unlock(&adap->lock);
462                 return err;
463         }
464
465         if (fh->mode_follower == CEC_MODE_FOLLOWER)
466                 adap->follower_cnt--;
467         if (mode_follower == CEC_MODE_FOLLOWER)
468                 adap->follower_cnt++;
469         if (send_pin_event) {
470                 struct cec_event ev = {
471                         .flags = CEC_EVENT_FL_INITIAL_STATE,
472                 };
473
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         }
478         if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
479             mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
480                 adap->passthrough =
481                         mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
482                 adap->cec_follower = fh;
483         } else if (adap->cec_follower == fh) {
484                 adap->passthrough = false;
485                 adap->cec_follower = NULL;
486         }
487         if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
488                 adap->cec_initiator = fh;
489         else if (adap->cec_initiator == fh)
490                 adap->cec_initiator = NULL;
491         fh->mode_initiator = mode_initiator;
492         fh->mode_follower = mode_follower;
493         mutex_unlock(&adap->lock);
494         return 0;
495 }
496
497 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
498 {
499         struct cec_fh *fh = filp->private_data;
500         struct cec_adapter *adap = fh->adap;
501         bool block = !(filp->f_flags & O_NONBLOCK);
502         void __user *parg = (void __user *)arg;
503
504         if (!cec_is_registered(adap))
505                 return -ENODEV;
506
507         switch (cmd) {
508         case CEC_ADAP_G_CAPS:
509                 return cec_adap_g_caps(adap, parg);
510
511         case CEC_ADAP_G_PHYS_ADDR:
512                 return cec_adap_g_phys_addr(adap, parg);
513
514         case CEC_ADAP_S_PHYS_ADDR:
515                 return cec_adap_s_phys_addr(adap, fh, block, parg);
516
517         case CEC_ADAP_G_LOG_ADDRS:
518                 return cec_adap_g_log_addrs(adap, parg);
519
520         case CEC_ADAP_S_LOG_ADDRS:
521                 return cec_adap_s_log_addrs(adap, fh, block, parg);
522
523         case CEC_TRANSMIT:
524                 return cec_transmit(adap, fh, block, parg);
525
526         case CEC_RECEIVE:
527                 return cec_receive(adap, fh, block, parg);
528
529         case CEC_DQEVENT:
530                 return cec_dqevent(adap, fh, block, parg);
531
532         case CEC_G_MODE:
533                 return cec_g_mode(adap, fh, parg);
534
535         case CEC_S_MODE:
536                 return cec_s_mode(adap, fh, parg);
537
538         default:
539                 return -ENOTTY;
540         }
541 }
542
543 static int cec_open(struct inode *inode, struct file *filp)
544 {
545         struct cec_devnode *devnode =
546                 container_of(inode->i_cdev, struct cec_devnode, cdev);
547         struct cec_adapter *adap = to_cec_adapter(devnode);
548         struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
549         /*
550          * Initial events that are automatically sent when the cec device is
551          * opened.
552          */
553         struct cec_event ev = {
554                 .event = CEC_EVENT_STATE_CHANGE,
555                 .flags = CEC_EVENT_FL_INITIAL_STATE,
556         };
557         unsigned int i;
558         int err;
559
560         if (!fh)
561                 return -ENOMEM;
562
563         INIT_LIST_HEAD(&fh->msgs);
564         INIT_LIST_HEAD(&fh->xfer_list);
565         for (i = 0; i < CEC_NUM_EVENTS; i++)
566                 INIT_LIST_HEAD(&fh->events[i]);
567         mutex_init(&fh->lock);
568         init_waitqueue_head(&fh->wait);
569
570         fh->mode_initiator = CEC_MODE_INITIATOR;
571         fh->adap = adap;
572
573         err = cec_get_device(devnode);
574         if (err) {
575                 kfree(fh);
576                 return err;
577         }
578
579         mutex_lock(&devnode->lock);
580         if (list_empty(&devnode->fhs) &&
581             !adap->needs_hpd &&
582             adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
583                 err = adap->ops->adap_enable(adap, true);
584                 if (err) {
585                         mutex_unlock(&devnode->lock);
586                         kfree(fh);
587                         return err;
588                 }
589         }
590         filp->private_data = fh;
591
592         /* Queue up initial state events */
593         ev.state_change.phys_addr = adap->phys_addr;
594         ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
595         cec_queue_event_fh(fh, &ev, 0);
596 #ifdef CONFIG_CEC_PIN
597         if (adap->pin && adap->pin->ops->read_hpd) {
598                 err = adap->pin->ops->read_hpd(adap);
599                 if (err >= 0) {
600                         ev.event = err ? CEC_EVENT_PIN_HPD_HIGH :
601                                          CEC_EVENT_PIN_HPD_LOW;
602                         cec_queue_event_fh(fh, &ev, 0);
603                 }
604         }
605         if (adap->pin && adap->pin->ops->read_5v) {
606                 err = adap->pin->ops->read_5v(adap);
607                 if (err >= 0) {
608                         ev.event = err ? CEC_EVENT_PIN_5V_HIGH :
609                                          CEC_EVENT_PIN_5V_LOW;
610                         cec_queue_event_fh(fh, &ev, 0);
611                 }
612         }
613 #endif
614
615         list_add(&fh->list, &devnode->fhs);
616         mutex_unlock(&devnode->lock);
617
618         return 0;
619 }
620
621 /* Override for the release function */
622 static int cec_release(struct inode *inode, struct file *filp)
623 {
624         struct cec_devnode *devnode = cec_devnode_data(filp);
625         struct cec_adapter *adap = to_cec_adapter(devnode);
626         struct cec_fh *fh = filp->private_data;
627         unsigned int i;
628
629         mutex_lock(&adap->lock);
630         if (adap->cec_initiator == fh)
631                 adap->cec_initiator = NULL;
632         if (adap->cec_follower == fh) {
633                 adap->cec_follower = NULL;
634                 adap->passthrough = false;
635         }
636         if (fh->mode_follower == CEC_MODE_FOLLOWER)
637                 adap->follower_cnt--;
638         if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
639                 cec_monitor_pin_cnt_dec(adap);
640         if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
641                 cec_monitor_all_cnt_dec(adap);
642         mutex_unlock(&adap->lock);
643
644         mutex_lock(&devnode->lock);
645         list_del(&fh->list);
646         if (cec_is_registered(adap) && list_empty(&devnode->fhs) &&
647             !adap->needs_hpd && adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
648                 WARN_ON(adap->ops->adap_enable(adap, false));
649         }
650         mutex_unlock(&devnode->lock);
651
652         /* Unhook pending transmits from this filehandle. */
653         mutex_lock(&adap->lock);
654         while (!list_empty(&fh->xfer_list)) {
655                 struct cec_data *data =
656                         list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
657
658                 data->blocking = false;
659                 data->fh = NULL;
660                 list_del(&data->xfer_list);
661         }
662         mutex_unlock(&adap->lock);
663         while (!list_empty(&fh->msgs)) {
664                 struct cec_msg_entry *entry =
665                         list_first_entry(&fh->msgs, struct cec_msg_entry, list);
666
667                 list_del(&entry->list);
668                 kfree(entry);
669         }
670         for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
671                 while (!list_empty(&fh->events[i])) {
672                         struct cec_event_entry *entry =
673                                 list_first_entry(&fh->events[i],
674                                                  struct cec_event_entry, list);
675
676                         list_del(&entry->list);
677                         kfree(entry);
678                 }
679         }
680         kfree(fh);
681
682         cec_put_device(devnode);
683         filp->private_data = NULL;
684         return 0;
685 }
686
687 const struct file_operations cec_devnode_fops = {
688         .owner = THIS_MODULE,
689         .open = cec_open,
690         .unlocked_ioctl = cec_ioctl,
691         .release = cec_release,
692         .poll = cec_poll,
693         .llseek = no_llseek,
694 };