blob: 627cdf7b12d161c2cb8d7debbee61669b6b1b455 [file] [log] [blame]
Hans Verkuilca684382016-06-25 09:44:43 -03001/*
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 "cec-priv.h"
34
35static inline struct cec_devnode *cec_devnode_data(struct file *filp)
36{
37 struct cec_fh *fh = filp->private_data;
38
39 return &fh->adap->devnode;
40}
41
42/* CEC file operations */
43
44static unsigned int cec_poll(struct file *filp,
45 struct poll_table_struct *poll)
46{
47 struct cec_devnode *devnode = cec_devnode_data(filp);
48 struct cec_fh *fh = filp->private_data;
49 struct cec_adapter *adap = fh->adap;
50 unsigned int res = 0;
51
52 if (!devnode->registered)
53 return POLLERR | POLLHUP;
54 mutex_lock(&adap->lock);
Hans Verkuilb7cbc892016-07-17 13:02:44 -030055 if (adap->is_configured &&
56 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
Hans Verkuilca684382016-06-25 09:44:43 -030057 res |= POLLOUT | POLLWRNORM;
58 if (fh->queued_msgs)
59 res |= POLLIN | POLLRDNORM;
60 if (fh->pending_events)
61 res |= POLLPRI;
62 poll_wait(filp, &fh->wait, poll);
63 mutex_unlock(&adap->lock);
64 return res;
65}
66
67static bool cec_is_busy(const struct cec_adapter *adap,
68 const struct cec_fh *fh)
69{
70 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
71 bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
72
73 /*
74 * Exclusive initiators and followers can always access the CEC adapter
75 */
76 if (valid_initiator || valid_follower)
77 return false;
78 /*
79 * All others can only access the CEC adapter if there is no
80 * exclusive initiator and they are in INITIATOR mode.
81 */
82 return adap->cec_initiator ||
83 fh->mode_initiator == CEC_MODE_NO_INITIATOR;
84}
85
86static long cec_adap_g_caps(struct cec_adapter *adap,
87 struct cec_caps __user *parg)
88{
89 struct cec_caps caps = {};
90
Hans Verkuilf51e8082016-11-25 06:23:34 -020091 strlcpy(caps.driver, adap->devnode.dev.parent->driver->name,
Hans Verkuilca684382016-06-25 09:44:43 -030092 sizeof(caps.driver));
93 strlcpy(caps.name, adap->name, sizeof(caps.name));
94 caps.available_log_addrs = adap->available_log_addrs;
95 caps.capabilities = adap->capabilities;
96 caps.version = LINUX_VERSION_CODE;
97 if (copy_to_user(parg, &caps, sizeof(caps)))
98 return -EFAULT;
99 return 0;
100}
101
102static long cec_adap_g_phys_addr(struct cec_adapter *adap,
103 __u16 __user *parg)
104{
105 u16 phys_addr;
106
107 mutex_lock(&adap->lock);
108 phys_addr = adap->phys_addr;
109 mutex_unlock(&adap->lock);
110 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
111 return -EFAULT;
112 return 0;
113}
114
115static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
116 bool block, __u16 __user *parg)
117{
118 u16 phys_addr;
119 long err;
120
121 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
122 return -ENOTTY;
123 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
124 return -EFAULT;
125
126 err = cec_phys_addr_validate(phys_addr, NULL, NULL);
127 if (err)
128 return err;
129 mutex_lock(&adap->lock);
130 if (cec_is_busy(adap, fh))
131 err = -EBUSY;
132 else
133 __cec_s_phys_addr(adap, phys_addr, block);
134 mutex_unlock(&adap->lock);
135 return err;
136}
137
138static long cec_adap_g_log_addrs(struct cec_adapter *adap,
139 struct cec_log_addrs __user *parg)
140{
141 struct cec_log_addrs log_addrs;
142
143 mutex_lock(&adap->lock);
144 log_addrs = adap->log_addrs;
145 if (!adap->is_configured)
146 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
147 sizeof(log_addrs.log_addr));
148 mutex_unlock(&adap->lock);
149
150 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
151 return -EFAULT;
152 return 0;
153}
154
155static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
156 bool block, struct cec_log_addrs __user *parg)
157{
158 struct cec_log_addrs log_addrs;
159 long err = -EBUSY;
160
161 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
162 return -ENOTTY;
163 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
164 return -EFAULT;
Hans Verkuilf4062622016-11-01 07:59:34 -0200165 log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
Hans Verkuila69a1682016-11-02 07:41:41 -0200166 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
167 CEC_LOG_ADDRS_FL_CDC_ONLY;
Hans Verkuilca684382016-06-25 09:44:43 -0300168 mutex_lock(&adap->lock);
169 if (!adap->is_configuring &&
170 (!log_addrs.num_log_addrs || !adap->is_configured) &&
171 !cec_is_busy(adap, fh)) {
172 err = __cec_s_log_addrs(adap, &log_addrs, block);
173 if (!err)
174 log_addrs = adap->log_addrs;
175 }
176 mutex_unlock(&adap->lock);
177 if (err)
178 return err;
179 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
180 return -EFAULT;
181 return 0;
182}
183
184static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
185 bool block, struct cec_msg __user *parg)
186{
187 struct cec_msg msg = {};
188 long err = 0;
189
190 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
191 return -ENOTTY;
192 if (copy_from_user(&msg, parg, sizeof(msg)))
193 return -EFAULT;
Hans Verkuila69a1682016-11-02 07:41:41 -0200194
195 /* A CDC-Only device can only send CDC messages */
196 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
197 (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE))
198 return -EINVAL;
199
Hans Verkuilca684382016-06-25 09:44:43 -0300200 mutex_lock(&adap->lock);
Hans Verkuil533a3f72017-02-10 13:02:31 -0200201 if (adap->is_configuring)
202 err = -ENONET;
203 else if (!adap->is_configured && (msg.msg[0] != 0xf0 || msg.reply))
Hans Verkuilca684382016-06-25 09:44:43 -0300204 err = -ENONET;
Hans Verkuil4eef4042016-07-16 09:59:18 -0300205 else if (cec_is_busy(adap, fh))
Hans Verkuilca684382016-06-25 09:44:43 -0300206 err = -EBUSY;
Hans Verkuil4eef4042016-07-16 09:59:18 -0300207 else
Hans Verkuilca684382016-06-25 09:44:43 -0300208 err = cec_transmit_msg_fh(adap, &msg, fh, block);
Hans Verkuilca684382016-06-25 09:44:43 -0300209 mutex_unlock(&adap->lock);
210 if (err)
211 return err;
212 if (copy_to_user(parg, &msg, sizeof(msg)))
213 return -EFAULT;
214 return 0;
215}
216
217/* Called by CEC_RECEIVE: wait for a message to arrive */
218static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
219{
Hans Verkuile883b4d2016-07-16 07:58:31 -0300220 u32 timeout = msg->timeout;
Hans Verkuilca684382016-06-25 09:44:43 -0300221 int res;
222
223 do {
224 mutex_lock(&fh->lock);
225 /* Are there received messages queued up? */
226 if (fh->queued_msgs) {
227 /* Yes, return the first one */
228 struct cec_msg_entry *entry =
229 list_first_entry(&fh->msgs,
230 struct cec_msg_entry, list);
231
232 list_del(&entry->list);
233 *msg = entry->msg;
234 kfree(entry);
235 fh->queued_msgs--;
236 mutex_unlock(&fh->lock);
Hans Verkuile883b4d2016-07-16 07:58:31 -0300237 /* restore original timeout value */
238 msg->timeout = timeout;
Hans Verkuilca684382016-06-25 09:44:43 -0300239 return 0;
240 }
241
242 /* No, return EAGAIN in non-blocking mode or wait */
243 mutex_unlock(&fh->lock);
244
245 /* Return when in non-blocking mode */
246 if (!block)
247 return -EAGAIN;
248
249 if (msg->timeout) {
250 /* The user specified a timeout */
251 res = wait_event_interruptible_timeout(fh->wait,
252 fh->queued_msgs,
253 msecs_to_jiffies(msg->timeout));
254 if (res == 0)
255 res = -ETIMEDOUT;
256 else if (res > 0)
257 res = 0;
258 } else {
259 /* Wait indefinitely */
260 res = wait_event_interruptible(fh->wait,
261 fh->queued_msgs);
262 }
263 /* Exit on error, otherwise loop to get the new message */
264 } while (!res);
265 return res;
266}
267
268static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
269 bool block, struct cec_msg __user *parg)
270{
271 struct cec_msg msg = {};
272 long err = 0;
273
274 if (copy_from_user(&msg, parg, sizeof(msg)))
275 return -EFAULT;
276 mutex_lock(&adap->lock);
Hans Verkuilca000332016-07-11 05:48:10 -0300277 if (!adap->is_configured && fh->mode_follower < CEC_MODE_MONITOR)
Hans Verkuilca684382016-06-25 09:44:43 -0300278 err = -ENONET;
279 mutex_unlock(&adap->lock);
280 if (err)
281 return err;
282
283 err = cec_receive_msg(fh, &msg, block);
284 if (err)
285 return err;
Hans Verkuil7ae2a882016-11-04 07:52:11 -0200286 msg.flags = 0;
Hans Verkuilca684382016-06-25 09:44:43 -0300287 if (copy_to_user(parg, &msg, sizeof(msg)))
288 return -EFAULT;
289 return 0;
290}
291
292static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
293 bool block, struct cec_event __user *parg)
294{
295 struct cec_event *ev = NULL;
296 u64 ts = ~0ULL;
297 unsigned int i;
298 long err = 0;
299
300 mutex_lock(&fh->lock);
301 while (!fh->pending_events && block) {
302 mutex_unlock(&fh->lock);
303 err = wait_event_interruptible(fh->wait, fh->pending_events);
304 if (err)
305 return err;
306 mutex_lock(&fh->lock);
307 }
308
309 /* Find the oldest event */
310 for (i = 0; i < CEC_NUM_EVENTS; i++) {
311 if (fh->pending_events & (1 << (i + 1)) &&
312 fh->events[i].ts <= ts) {
313 ev = &fh->events[i];
314 ts = ev->ts;
315 }
316 }
317 if (!ev) {
318 err = -EAGAIN;
319 goto unlock;
320 }
321
322 if (copy_to_user(parg, ev, sizeof(*ev))) {
323 err = -EFAULT;
324 goto unlock;
325 }
326
327 fh->pending_events &= ~(1 << ev->event);
328
329unlock:
330 mutex_unlock(&fh->lock);
331 return err;
332}
333
334static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
335 u32 __user *parg)
336{
337 u32 mode = fh->mode_initiator | fh->mode_follower;
338
339 if (copy_to_user(parg, &mode, sizeof(mode)))
340 return -EFAULT;
341 return 0;
342}
343
344static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
345 u32 __user *parg)
346{
347 u32 mode;
348 u8 mode_initiator;
349 u8 mode_follower;
350 long err = 0;
351
352 if (copy_from_user(&mode, parg, sizeof(mode)))
353 return -EFAULT;
354 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK))
355 return -EINVAL;
356
357 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
358 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
359
360 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
361 mode_follower > CEC_MODE_MONITOR_ALL)
362 return -EINVAL;
363
364 if (mode_follower == CEC_MODE_MONITOR_ALL &&
365 !(adap->capabilities & CEC_CAP_MONITOR_ALL))
366 return -EINVAL;
367
368 /* Follower modes should always be able to send CEC messages */
369 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
370 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
371 mode_follower >= CEC_MODE_FOLLOWER &&
372 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU)
373 return -EINVAL;
374
375 /* Monitor modes require CEC_MODE_NO_INITIATOR */
376 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR)
377 return -EINVAL;
378
379 /* Monitor modes require CAP_NET_ADMIN */
380 if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN))
381 return -EPERM;
382
383 mutex_lock(&adap->lock);
384 /*
385 * You can't become exclusive follower if someone else already
386 * has that job.
387 */
388 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
389 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
390 adap->cec_follower && adap->cec_follower != fh)
391 err = -EBUSY;
392 /*
393 * You can't become exclusive initiator if someone else already
394 * has that job.
395 */
396 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
397 adap->cec_initiator && adap->cec_initiator != fh)
398 err = -EBUSY;
399
400 if (!err) {
401 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
402 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
403
404 if (old_mon_all != new_mon_all) {
405 if (new_mon_all)
406 err = cec_monitor_all_cnt_inc(adap);
407 else
408 cec_monitor_all_cnt_dec(adap);
409 }
410 }
411
412 if (err) {
413 mutex_unlock(&adap->lock);
414 return err;
415 }
416
417 if (fh->mode_follower == CEC_MODE_FOLLOWER)
418 adap->follower_cnt--;
419 if (mode_follower == CEC_MODE_FOLLOWER)
420 adap->follower_cnt++;
421 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
422 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
423 adap->passthrough =
424 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
425 adap->cec_follower = fh;
426 } else if (adap->cec_follower == fh) {
427 adap->passthrough = false;
428 adap->cec_follower = NULL;
429 }
430 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
431 adap->cec_initiator = fh;
432 else if (adap->cec_initiator == fh)
433 adap->cec_initiator = NULL;
434 fh->mode_initiator = mode_initiator;
435 fh->mode_follower = mode_follower;
436 mutex_unlock(&adap->lock);
437 return 0;
438}
439
440static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
441{
442 struct cec_devnode *devnode = cec_devnode_data(filp);
443 struct cec_fh *fh = filp->private_data;
444 struct cec_adapter *adap = fh->adap;
445 bool block = !(filp->f_flags & O_NONBLOCK);
446 void __user *parg = (void __user *)arg;
447
448 if (!devnode->registered)
Hans Verkuil60815d42016-08-24 07:17:22 -0300449 return -ENODEV;
Hans Verkuilca684382016-06-25 09:44:43 -0300450
451 switch (cmd) {
452 case CEC_ADAP_G_CAPS:
453 return cec_adap_g_caps(adap, parg);
454
455 case CEC_ADAP_G_PHYS_ADDR:
456 return cec_adap_g_phys_addr(adap, parg);
457
458 case CEC_ADAP_S_PHYS_ADDR:
459 return cec_adap_s_phys_addr(adap, fh, block, parg);
460
461 case CEC_ADAP_G_LOG_ADDRS:
462 return cec_adap_g_log_addrs(adap, parg);
463
464 case CEC_ADAP_S_LOG_ADDRS:
465 return cec_adap_s_log_addrs(adap, fh, block, parg);
466
467 case CEC_TRANSMIT:
468 return cec_transmit(adap, fh, block, parg);
469
470 case CEC_RECEIVE:
471 return cec_receive(adap, fh, block, parg);
472
473 case CEC_DQEVENT:
474 return cec_dqevent(adap, fh, block, parg);
475
476 case CEC_G_MODE:
477 return cec_g_mode(adap, fh, parg);
478
479 case CEC_S_MODE:
480 return cec_s_mode(adap, fh, parg);
481
482 default:
483 return -ENOTTY;
484 }
485}
486
487static int cec_open(struct inode *inode, struct file *filp)
488{
489 struct cec_devnode *devnode =
490 container_of(inode->i_cdev, struct cec_devnode, cdev);
491 struct cec_adapter *adap = to_cec_adapter(devnode);
492 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
493 /*
494 * Initial events that are automatically sent when the cec device is
495 * opened.
496 */
497 struct cec_event ev_state = {
498 .event = CEC_EVENT_STATE_CHANGE,
499 .flags = CEC_EVENT_FL_INITIAL_STATE,
500 };
501 int err;
502
503 if (!fh)
504 return -ENOMEM;
505
506 INIT_LIST_HEAD(&fh->msgs);
507 INIT_LIST_HEAD(&fh->xfer_list);
508 mutex_init(&fh->lock);
509 init_waitqueue_head(&fh->wait);
510
511 fh->mode_initiator = CEC_MODE_INITIATOR;
512 fh->adap = adap;
513
514 err = cec_get_device(devnode);
515 if (err) {
516 kfree(fh);
517 return err;
518 }
519
Hans Verkuil533a3f72017-02-10 13:02:31 -0200520 mutex_lock(&devnode->lock);
521 if (list_empty(&devnode->fhs) &&
522 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
523 err = adap->ops->adap_enable(adap, true);
524 if (err) {
525 mutex_unlock(&devnode->lock);
526 kfree(fh);
527 return err;
528 }
529 }
Hans Verkuilca684382016-06-25 09:44:43 -0300530 filp->private_data = fh;
531
Hans Verkuilca684382016-06-25 09:44:43 -0300532 /* Queue up initial state events */
533 ev_state.state_change.phys_addr = adap->phys_addr;
534 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
535 cec_queue_event_fh(fh, &ev_state, 0);
536
537 list_add(&fh->list, &devnode->fhs);
Hans Verkuil62148f02016-08-02 08:11:00 -0300538 mutex_unlock(&devnode->lock);
Hans Verkuilca684382016-06-25 09:44:43 -0300539
540 return 0;
541}
542
543/* Override for the release function */
544static int cec_release(struct inode *inode, struct file *filp)
545{
546 struct cec_devnode *devnode = cec_devnode_data(filp);
547 struct cec_adapter *adap = to_cec_adapter(devnode);
548 struct cec_fh *fh = filp->private_data;
549
550 mutex_lock(&adap->lock);
551 if (adap->cec_initiator == fh)
552 adap->cec_initiator = NULL;
553 if (adap->cec_follower == fh) {
554 adap->cec_follower = NULL;
555 adap->passthrough = false;
556 }
557 if (fh->mode_follower == CEC_MODE_FOLLOWER)
558 adap->follower_cnt--;
559 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
560 cec_monitor_all_cnt_dec(adap);
561 mutex_unlock(&adap->lock);
562
Hans Verkuil62148f02016-08-02 08:11:00 -0300563 mutex_lock(&devnode->lock);
Hans Verkuilca684382016-06-25 09:44:43 -0300564 list_del(&fh->list);
Hans Verkuil533a3f72017-02-10 13:02:31 -0200565 if (list_empty(&devnode->fhs) &&
566 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
567 WARN_ON(adap->ops->adap_enable(adap, false));
568 }
Hans Verkuil62148f02016-08-02 08:11:00 -0300569 mutex_unlock(&devnode->lock);
Hans Verkuilca684382016-06-25 09:44:43 -0300570
571 /* Unhook pending transmits from this filehandle. */
572 mutex_lock(&adap->lock);
573 while (!list_empty(&fh->xfer_list)) {
574 struct cec_data *data =
575 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
576
577 data->blocking = false;
578 data->fh = NULL;
579 list_del(&data->xfer_list);
580 }
581 mutex_unlock(&adap->lock);
582 while (!list_empty(&fh->msgs)) {
583 struct cec_msg_entry *entry =
584 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
585
586 list_del(&entry->list);
587 kfree(entry);
588 }
589 kfree(fh);
590
591 cec_put_device(devnode);
592 filp->private_data = NULL;
593 return 0;
594}
595
596const struct file_operations cec_devnode_fops = {
597 .owner = THIS_MODULE,
598 .open = cec_open,
599 .unlocked_ioctl = cec_ioctl,
600 .release = cec_release,
601 .poll = cec_poll,
602 .llseek = no_llseek,
603};