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