2 * cec-api.c - HDMI Consumer Electronics Control framework - API
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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
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>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <linux/version.h>
33 #include <media/cec-pin.h>
35 #include "cec-pin-priv.h"
37 static inline struct cec_devnode
*cec_devnode_data(struct file
*filp
)
39 struct cec_fh
*fh
= filp
->private_data
;
41 return &fh
->adap
->devnode
;
44 /* CEC file operations */
46 static unsigned int cec_poll(struct file
*filp
,
47 struct poll_table_struct
*poll
)
49 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
50 struct cec_fh
*fh
= filp
->private_data
;
51 struct cec_adapter
*adap
= fh
->adap
;
54 if (!devnode
->registered
)
55 return POLLERR
| POLLHUP
;
56 mutex_lock(&adap
->lock
);
57 if (adap
->is_configured
&&
58 adap
->transmit_queue_sz
< CEC_MAX_MSG_TX_QUEUE_SZ
)
59 res
|= POLLOUT
| POLLWRNORM
;
61 res
|= POLLIN
| POLLRDNORM
;
62 if (fh
->total_queued_events
)
64 poll_wait(filp
, &fh
->wait
, poll
);
65 mutex_unlock(&adap
->lock
);
69 static bool cec_is_busy(const struct cec_adapter
*adap
,
70 const struct cec_fh
*fh
)
72 bool valid_initiator
= adap
->cec_initiator
&& adap
->cec_initiator
== fh
;
73 bool valid_follower
= adap
->cec_follower
&& adap
->cec_follower
== fh
;
76 * Exclusive initiators and followers can always access the CEC adapter
78 if (valid_initiator
|| valid_follower
)
81 * All others can only access the CEC adapter if there is no
82 * exclusive initiator and they are in INITIATOR mode.
84 return adap
->cec_initiator
||
85 fh
->mode_initiator
== CEC_MODE_NO_INITIATOR
;
88 static long cec_adap_g_caps(struct cec_adapter
*adap
,
89 struct cec_caps __user
*parg
)
91 struct cec_caps caps
= {};
93 strlcpy(caps
.driver
, adap
->devnode
.dev
.parent
->driver
->name
,
95 strlcpy(caps
.name
, adap
->name
, sizeof(caps
.name
));
96 caps
.available_log_addrs
= adap
->available_log_addrs
;
97 caps
.capabilities
= adap
->capabilities
;
98 caps
.version
= LINUX_VERSION_CODE
;
99 if (copy_to_user(parg
, &caps
, sizeof(caps
)))
104 static long cec_adap_g_phys_addr(struct cec_adapter
*adap
,
109 mutex_lock(&adap
->lock
);
110 phys_addr
= adap
->phys_addr
;
111 mutex_unlock(&adap
->lock
);
112 if (copy_to_user(parg
, &phys_addr
, sizeof(phys_addr
)))
117 static int cec_validate_phys_addr(u16 phys_addr
)
121 if (phys_addr
== CEC_PHYS_ADDR_INVALID
)
123 for (i
= 0; i
< 16; i
+= 4)
124 if (phys_addr
& (0xf << i
))
128 for (i
+= 4; i
< 16; i
+= 4)
129 if ((phys_addr
& (0xf << i
)) == 0)
134 static long cec_adap_s_phys_addr(struct cec_adapter
*adap
, struct cec_fh
*fh
,
135 bool block
, __u16 __user
*parg
)
140 if (!(adap
->capabilities
& CEC_CAP_PHYS_ADDR
))
142 if (copy_from_user(&phys_addr
, parg
, sizeof(phys_addr
)))
145 err
= cec_validate_phys_addr(phys_addr
);
148 mutex_lock(&adap
->lock
);
149 if (cec_is_busy(adap
, fh
))
152 __cec_s_phys_addr(adap
, phys_addr
, block
);
153 mutex_unlock(&adap
->lock
);
157 static long cec_adap_g_log_addrs(struct cec_adapter
*adap
,
158 struct cec_log_addrs __user
*parg
)
160 struct cec_log_addrs log_addrs
;
162 mutex_lock(&adap
->lock
);
163 log_addrs
= adap
->log_addrs
;
164 if (!adap
->is_configured
)
165 memset(log_addrs
.log_addr
, CEC_LOG_ADDR_INVALID
,
166 sizeof(log_addrs
.log_addr
));
167 mutex_unlock(&adap
->lock
);
169 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
174 static long cec_adap_s_log_addrs(struct cec_adapter
*adap
, struct cec_fh
*fh
,
175 bool block
, struct cec_log_addrs __user
*parg
)
177 struct cec_log_addrs log_addrs
;
180 if (!(adap
->capabilities
& CEC_CAP_LOG_ADDRS
))
182 if (copy_from_user(&log_addrs
, parg
, sizeof(log_addrs
)))
184 log_addrs
.flags
&= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK
|
185 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU
|
186 CEC_LOG_ADDRS_FL_CDC_ONLY
;
187 mutex_lock(&adap
->lock
);
188 if (!adap
->is_configuring
&&
189 (!log_addrs
.num_log_addrs
|| !adap
->is_configured
) &&
190 !cec_is_busy(adap
, fh
)) {
191 err
= __cec_s_log_addrs(adap
, &log_addrs
, block
);
193 log_addrs
= adap
->log_addrs
;
195 mutex_unlock(&adap
->lock
);
198 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
203 static long cec_transmit(struct cec_adapter
*adap
, struct cec_fh
*fh
,
204 bool block
, struct cec_msg __user
*parg
)
206 struct cec_msg msg
= {};
209 if (!(adap
->capabilities
& CEC_CAP_TRANSMIT
))
211 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
214 /* A CDC-Only device can only send CDC messages */
215 if ((adap
->log_addrs
.flags
& CEC_LOG_ADDRS_FL_CDC_ONLY
) &&
216 (msg
.len
== 1 || msg
.msg
[1] != CEC_MSG_CDC_MESSAGE
))
219 mutex_lock(&adap
->lock
);
220 if (adap
->log_addrs
.num_log_addrs
== 0)
222 else if (adap
->is_configuring
)
224 else if (!adap
->is_configured
&&
225 (adap
->needs_hpd
|| msg
.msg
[0] != 0xf0))
227 else if (cec_is_busy(adap
, fh
))
230 err
= cec_transmit_msg_fh(adap
, &msg
, fh
, block
);
231 mutex_unlock(&adap
->lock
);
234 if (copy_to_user(parg
, &msg
, sizeof(msg
)))
239 /* Called by CEC_RECEIVE: wait for a message to arrive */
240 static int cec_receive_msg(struct cec_fh
*fh
, struct cec_msg
*msg
, bool block
)
242 u32 timeout
= msg
->timeout
;
246 mutex_lock(&fh
->lock
);
247 /* Are there received messages queued up? */
248 if (fh
->queued_msgs
) {
249 /* Yes, return the first one */
250 struct cec_msg_entry
*entry
=
251 list_first_entry(&fh
->msgs
,
252 struct cec_msg_entry
, list
);
254 list_del(&entry
->list
);
258 mutex_unlock(&fh
->lock
);
259 /* restore original timeout value */
260 msg
->timeout
= timeout
;
264 /* No, return EAGAIN in non-blocking mode or wait */
265 mutex_unlock(&fh
->lock
);
267 /* Return when in non-blocking mode */
272 /* The user specified a timeout */
273 res
= wait_event_interruptible_timeout(fh
->wait
,
275 msecs_to_jiffies(msg
->timeout
));
281 /* Wait indefinitely */
282 res
= wait_event_interruptible(fh
->wait
,
285 /* Exit on error, otherwise loop to get the new message */
290 static long cec_receive(struct cec_adapter
*adap
, struct cec_fh
*fh
,
291 bool block
, struct cec_msg __user
*parg
)
293 struct cec_msg msg
= {};
296 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
299 err
= cec_receive_msg(fh
, &msg
, block
);
303 if (copy_to_user(parg
, &msg
, sizeof(msg
)))
308 static long cec_dqevent(struct cec_adapter
*adap
, struct cec_fh
*fh
,
309 bool block
, struct cec_event __user
*parg
)
311 struct cec_event_entry
*ev
= NULL
;
317 mutex_lock(&fh
->lock
);
318 while (!fh
->total_queued_events
&& block
) {
319 mutex_unlock(&fh
->lock
);
320 err
= wait_event_interruptible(fh
->wait
,
321 fh
->total_queued_events
);
324 mutex_lock(&fh
->lock
);
327 /* Find the oldest event */
328 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++) {
329 struct cec_event_entry
*entry
=
330 list_first_entry_or_null(&fh
->events
[i
],
331 struct cec_event_entry
, list
);
333 if (entry
&& entry
->ev
.ts
<= ts
) {
346 if (copy_to_user(parg
, &ev
->ev
, sizeof(ev
->ev
)))
348 if (ev_idx
>= CEC_NUM_CORE_EVENTS
)
350 fh
->queued_events
[ev_idx
]--;
351 fh
->total_queued_events
--;
354 mutex_unlock(&fh
->lock
);
358 static long cec_g_mode(struct cec_adapter
*adap
, struct cec_fh
*fh
,
361 u32 mode
= fh
->mode_initiator
| fh
->mode_follower
;
363 if (copy_to_user(parg
, &mode
, sizeof(mode
)))
368 static long cec_s_mode(struct cec_adapter
*adap
, struct cec_fh
*fh
,
376 if (copy_from_user(&mode
, parg
, sizeof(mode
)))
378 if (mode
& ~(CEC_MODE_INITIATOR_MSK
| CEC_MODE_FOLLOWER_MSK
)) {
379 dprintk(1, "%s: invalid mode bits set\n", __func__
);
383 mode_initiator
= mode
& CEC_MODE_INITIATOR_MSK
;
384 mode_follower
= mode
& CEC_MODE_FOLLOWER_MSK
;
386 if (mode_initiator
> CEC_MODE_EXCL_INITIATOR
||
387 mode_follower
> CEC_MODE_MONITOR_ALL
) {
388 dprintk(1, "%s: unknown mode\n", __func__
);
392 if (mode_follower
== CEC_MODE_MONITOR_ALL
&&
393 !(adap
->capabilities
& CEC_CAP_MONITOR_ALL
)) {
394 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__
);
398 if (mode_follower
== CEC_MODE_MONITOR_PIN
&&
399 !(adap
->capabilities
& CEC_CAP_MONITOR_PIN
)) {
400 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__
);
404 /* Follower modes should always be able to send CEC messages */
405 if ((mode_initiator
== CEC_MODE_NO_INITIATOR
||
406 !(adap
->capabilities
& CEC_CAP_TRANSMIT
)) &&
407 mode_follower
>= CEC_MODE_FOLLOWER
&&
408 mode_follower
<= CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
409 dprintk(1, "%s: cannot transmit\n", __func__
);
413 /* Monitor modes require CEC_MODE_NO_INITIATOR */
414 if (mode_initiator
&& mode_follower
>= CEC_MODE_MONITOR_PIN
) {
415 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
420 /* Monitor modes require CAP_NET_ADMIN */
421 if (mode_follower
>= CEC_MODE_MONITOR_PIN
&& !capable(CAP_NET_ADMIN
))
424 mutex_lock(&adap
->lock
);
426 * You can't become exclusive follower if someone else already
429 if ((mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
430 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) &&
431 adap
->cec_follower
&& adap
->cec_follower
!= fh
)
434 * You can't become exclusive initiator if someone else already
437 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
&&
438 adap
->cec_initiator
&& adap
->cec_initiator
!= fh
)
442 bool old_mon_all
= fh
->mode_follower
== CEC_MODE_MONITOR_ALL
;
443 bool new_mon_all
= mode_follower
== CEC_MODE_MONITOR_ALL
;
445 if (old_mon_all
!= new_mon_all
) {
447 err
= cec_monitor_all_cnt_inc(adap
);
449 cec_monitor_all_cnt_dec(adap
);
454 mutex_unlock(&adap
->lock
);
458 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
459 adap
->follower_cnt
--;
460 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
461 adap
->monitor_pin_cnt
--;
462 if (mode_follower
== CEC_MODE_FOLLOWER
)
463 adap
->follower_cnt
++;
464 if (mode_follower
== CEC_MODE_MONITOR_PIN
) {
465 struct cec_event ev
= {
466 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
469 ev
.event
= adap
->cec_pin_is_high
? CEC_EVENT_PIN_CEC_HIGH
:
470 CEC_EVENT_PIN_CEC_LOW
;
471 cec_queue_event_fh(fh
, &ev
, 0);
472 adap
->monitor_pin_cnt
++;
474 if (mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
475 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
477 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
;
478 adap
->cec_follower
= fh
;
479 } else if (adap
->cec_follower
== fh
) {
480 adap
->passthrough
= false;
481 adap
->cec_follower
= NULL
;
483 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
)
484 adap
->cec_initiator
= fh
;
485 else if (adap
->cec_initiator
== fh
)
486 adap
->cec_initiator
= NULL
;
487 fh
->mode_initiator
= mode_initiator
;
488 fh
->mode_follower
= mode_follower
;
489 mutex_unlock(&adap
->lock
);
493 static long cec_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
495 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
496 struct cec_fh
*fh
= filp
->private_data
;
497 struct cec_adapter
*adap
= fh
->adap
;
498 bool block
= !(filp
->f_flags
& O_NONBLOCK
);
499 void __user
*parg
= (void __user
*)arg
;
501 if (!devnode
->registered
)
505 case CEC_ADAP_G_CAPS
:
506 return cec_adap_g_caps(adap
, parg
);
508 case CEC_ADAP_G_PHYS_ADDR
:
509 return cec_adap_g_phys_addr(adap
, parg
);
511 case CEC_ADAP_S_PHYS_ADDR
:
512 return cec_adap_s_phys_addr(adap
, fh
, block
, parg
);
514 case CEC_ADAP_G_LOG_ADDRS
:
515 return cec_adap_g_log_addrs(adap
, parg
);
517 case CEC_ADAP_S_LOG_ADDRS
:
518 return cec_adap_s_log_addrs(adap
, fh
, block
, parg
);
521 return cec_transmit(adap
, fh
, block
, parg
);
524 return cec_receive(adap
, fh
, block
, parg
);
527 return cec_dqevent(adap
, fh
, block
, parg
);
530 return cec_g_mode(adap
, fh
, parg
);
533 return cec_s_mode(adap
, fh
, parg
);
540 static int cec_open(struct inode
*inode
, struct file
*filp
)
542 struct cec_devnode
*devnode
=
543 container_of(inode
->i_cdev
, struct cec_devnode
, cdev
);
544 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
545 struct cec_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
547 * Initial events that are automatically sent when the cec device is
550 struct cec_event ev
= {
551 .event
= CEC_EVENT_STATE_CHANGE
,
552 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
560 INIT_LIST_HEAD(&fh
->msgs
);
561 INIT_LIST_HEAD(&fh
->xfer_list
);
562 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++)
563 INIT_LIST_HEAD(&fh
->events
[i
]);
564 mutex_init(&fh
->lock
);
565 init_waitqueue_head(&fh
->wait
);
567 fh
->mode_initiator
= CEC_MODE_INITIATOR
;
570 err
= cec_get_device(devnode
);
576 mutex_lock(&devnode
->lock
);
577 if (list_empty(&devnode
->fhs
) &&
579 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
580 err
= adap
->ops
->adap_enable(adap
, true);
582 mutex_unlock(&devnode
->lock
);
587 filp
->private_data
= fh
;
589 /* Queue up initial state events */
590 ev
.state_change
.phys_addr
= adap
->phys_addr
;
591 ev
.state_change
.log_addr_mask
= adap
->log_addrs
.log_addr_mask
;
592 cec_queue_event_fh(fh
, &ev
, 0);
593 #ifdef CONFIG_CEC_PIN
594 if (adap
->pin
&& adap
->pin
->ops
->read_hpd
) {
595 err
= adap
->pin
->ops
->read_hpd(adap
);
597 ev
.event
= err
? CEC_EVENT_PIN_HPD_HIGH
:
598 CEC_EVENT_PIN_HPD_LOW
;
599 cec_queue_event_fh(fh
, &ev
, 0);
604 list_add(&fh
->list
, &devnode
->fhs
);
605 mutex_unlock(&devnode
->lock
);
610 /* Override for the release function */
611 static int cec_release(struct inode
*inode
, struct file
*filp
)
613 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
614 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
615 struct cec_fh
*fh
= filp
->private_data
;
618 mutex_lock(&adap
->lock
);
619 if (adap
->cec_initiator
== fh
)
620 adap
->cec_initiator
= NULL
;
621 if (adap
->cec_follower
== fh
) {
622 adap
->cec_follower
= NULL
;
623 adap
->passthrough
= false;
625 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
626 adap
->follower_cnt
--;
627 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
628 adap
->monitor_pin_cnt
--;
629 if (fh
->mode_follower
== CEC_MODE_MONITOR_ALL
)
630 cec_monitor_all_cnt_dec(adap
);
631 mutex_unlock(&adap
->lock
);
633 mutex_lock(&devnode
->lock
);
635 if (list_empty(&devnode
->fhs
) &&
637 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
638 WARN_ON(adap
->ops
->adap_enable(adap
, false));
640 mutex_unlock(&devnode
->lock
);
642 /* Unhook pending transmits from this filehandle. */
643 mutex_lock(&adap
->lock
);
644 while (!list_empty(&fh
->xfer_list
)) {
645 struct cec_data
*data
=
646 list_first_entry(&fh
->xfer_list
, struct cec_data
, xfer_list
);
648 data
->blocking
= false;
650 list_del(&data
->xfer_list
);
652 mutex_unlock(&adap
->lock
);
653 while (!list_empty(&fh
->msgs
)) {
654 struct cec_msg_entry
*entry
=
655 list_first_entry(&fh
->msgs
, struct cec_msg_entry
, list
);
657 list_del(&entry
->list
);
660 for (i
= CEC_NUM_CORE_EVENTS
; i
< CEC_NUM_EVENTS
; i
++) {
661 while (!list_empty(&fh
->events
[i
])) {
662 struct cec_event_entry
*entry
=
663 list_first_entry(&fh
->events
[i
],
664 struct cec_event_entry
, list
);
666 list_del(&entry
->list
);
672 cec_put_device(devnode
);
673 filp
->private_data
= NULL
;
677 const struct file_operations cec_devnode_fops
= {
678 .owner
= THIS_MODULE
,
680 .unlocked_ioctl
= cec_ioctl
,
681 .release
= cec_release
,