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