1 // SPDX-License-Identifier: GPL-2.0-only
3 * cec-api.c - HDMI Consumer Electronics Control framework - API
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/uaccess.h>
19 #include <linux/version.h>
21 #include <media/cec-pin.h>
23 #include "cec-pin-priv.h"
25 static inline struct cec_devnode
*cec_devnode_data(struct file
*filp
)
27 struct cec_fh
*fh
= filp
->private_data
;
29 return &fh
->adap
->devnode
;
32 /* CEC file operations */
34 static __poll_t
cec_poll(struct file
*filp
,
35 struct poll_table_struct
*poll
)
37 struct cec_fh
*fh
= filp
->private_data
;
38 struct cec_adapter
*adap
= fh
->adap
;
41 poll_wait(filp
, &fh
->wait
, poll
);
42 if (!cec_is_registered(adap
))
43 return EPOLLERR
| EPOLLHUP
;
44 mutex_lock(&adap
->lock
);
45 if (adap
->is_configured
&&
46 adap
->transmit_queue_sz
< CEC_MAX_MSG_TX_QUEUE_SZ
)
47 res
|= EPOLLOUT
| EPOLLWRNORM
;
49 res
|= EPOLLIN
| EPOLLRDNORM
;
50 if (fh
->total_queued_events
)
52 mutex_unlock(&adap
->lock
);
56 static bool cec_is_busy(const struct cec_adapter
*adap
,
57 const struct cec_fh
*fh
)
59 bool valid_initiator
= adap
->cec_initiator
&& adap
->cec_initiator
== fh
;
60 bool valid_follower
= adap
->cec_follower
&& adap
->cec_follower
== fh
;
63 * Exclusive initiators and followers can always access the CEC adapter
65 if (valid_initiator
|| valid_follower
)
68 * All others can only access the CEC adapter if there is no
69 * exclusive initiator and they are in INITIATOR mode.
71 return adap
->cec_initiator
||
72 fh
->mode_initiator
== CEC_MODE_NO_INITIATOR
;
75 static long cec_adap_g_caps(struct cec_adapter
*adap
,
76 struct cec_caps __user
*parg
)
78 struct cec_caps caps
= {};
80 strscpy(caps
.driver
, adap
->devnode
.dev
.parent
->driver
->name
,
82 strscpy(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
)))
91 static long cec_adap_g_phys_addr(struct cec_adapter
*adap
,
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
)))
104 static int cec_validate_phys_addr(u16 phys_addr
)
108 if (phys_addr
== CEC_PHYS_ADDR_INVALID
)
110 for (i
= 0; i
< 16; i
+= 4)
111 if (phys_addr
& (0xf << i
))
115 for (i
+= 4; i
< 16; i
+= 4)
116 if ((phys_addr
& (0xf << i
)) == 0)
121 static long cec_adap_s_phys_addr(struct cec_adapter
*adap
, struct cec_fh
*fh
,
122 bool block
, __u16 __user
*parg
)
127 if (!(adap
->capabilities
& CEC_CAP_PHYS_ADDR
))
129 if (copy_from_user(&phys_addr
, parg
, sizeof(phys_addr
)))
132 err
= cec_validate_phys_addr(phys_addr
);
135 mutex_lock(&adap
->lock
);
136 if (cec_is_busy(adap
, fh
))
139 __cec_s_phys_addr(adap
, phys_addr
, block
);
140 mutex_unlock(&adap
->lock
);
144 static long cec_adap_g_log_addrs(struct cec_adapter
*adap
,
145 struct cec_log_addrs __user
*parg
)
147 struct cec_log_addrs log_addrs
;
149 mutex_lock(&adap
->lock
);
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.
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
);
162 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
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
)
170 struct cec_log_addrs log_addrs
;
173 if (!(adap
->capabilities
& CEC_CAP_LOG_ADDRS
))
175 if (copy_from_user(&log_addrs
, parg
, sizeof(log_addrs
)))
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
);
186 log_addrs
= adap
->log_addrs
;
188 mutex_unlock(&adap
->lock
);
191 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
196 static long cec_adap_g_connector_info(struct cec_adapter
*adap
,
197 struct cec_log_addrs __user
*parg
)
201 if (!(adap
->capabilities
& CEC_CAP_CONNECTOR_INFO
))
204 mutex_lock(&adap
->lock
);
205 if (copy_to_user(parg
, &adap
->conn_info
, sizeof(adap
->conn_info
)))
207 mutex_unlock(&adap
->lock
);
211 static long cec_transmit(struct cec_adapter
*adap
, struct cec_fh
*fh
,
212 bool block
, struct cec_msg __user
*parg
)
214 struct cec_msg msg
= {};
217 if (!(adap
->capabilities
& CEC_CAP_TRANSMIT
))
219 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
222 mutex_lock(&adap
->lock
);
223 if (adap
->log_addrs
.num_log_addrs
== 0)
225 else if (adap
->is_configuring
)
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
,
374 bool send_pin_event
= false;
377 if (copy_from_user(&mode
, parg
, sizeof(mode
)))
379 if (mode
& ~(CEC_MODE_INITIATOR_MSK
| CEC_MODE_FOLLOWER_MSK
)) {
380 dprintk(1, "%s: invalid mode bits set\n", __func__
);
384 mode_initiator
= mode
& CEC_MODE_INITIATOR_MSK
;
385 mode_follower
= mode
& CEC_MODE_FOLLOWER_MSK
;
387 if (mode_initiator
> CEC_MODE_EXCL_INITIATOR
||
388 mode_follower
> CEC_MODE_MONITOR_ALL
) {
389 dprintk(1, "%s: unknown mode\n", __func__
);
393 if (mode_follower
== CEC_MODE_MONITOR_ALL
&&
394 !(adap
->capabilities
& CEC_CAP_MONITOR_ALL
)) {
395 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__
);
399 if (mode_follower
== CEC_MODE_MONITOR_PIN
&&
400 !(adap
->capabilities
& CEC_CAP_MONITOR_PIN
)) {
401 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__
);
405 /* Follower modes should always be able to send CEC messages */
406 if ((mode_initiator
== CEC_MODE_NO_INITIATOR
||
407 !(adap
->capabilities
& CEC_CAP_TRANSMIT
)) &&
408 mode_follower
>= CEC_MODE_FOLLOWER
&&
409 mode_follower
<= CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
410 dprintk(1, "%s: cannot transmit\n", __func__
);
414 /* Monitor modes require CEC_MODE_NO_INITIATOR */
415 if (mode_initiator
&& mode_follower
>= CEC_MODE_MONITOR_PIN
) {
416 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
421 /* Monitor modes require CAP_NET_ADMIN */
422 if (mode_follower
>= CEC_MODE_MONITOR_PIN
&& !capable(CAP_NET_ADMIN
))
425 mutex_lock(&adap
->lock
);
427 * You can't become exclusive follower if someone else already
430 if ((mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
431 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) &&
432 adap
->cec_follower
&& adap
->cec_follower
!= fh
)
435 * You can't become exclusive initiator if someone else already
438 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
&&
439 adap
->cec_initiator
&& adap
->cec_initiator
!= fh
)
443 bool old_mon_all
= fh
->mode_follower
== CEC_MODE_MONITOR_ALL
;
444 bool new_mon_all
= mode_follower
== CEC_MODE_MONITOR_ALL
;
446 if (old_mon_all
!= new_mon_all
) {
448 err
= cec_monitor_all_cnt_inc(adap
);
450 cec_monitor_all_cnt_dec(adap
);
455 bool old_mon_pin
= fh
->mode_follower
== CEC_MODE_MONITOR_PIN
;
456 bool new_mon_pin
= mode_follower
== CEC_MODE_MONITOR_PIN
;
458 if (old_mon_pin
!= new_mon_pin
) {
459 send_pin_event
= new_mon_pin
;
461 err
= cec_monitor_pin_cnt_inc(adap
);
463 cec_monitor_pin_cnt_dec(adap
);
468 mutex_unlock(&adap
->lock
);
472 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
473 adap
->follower_cnt
--;
474 if (mode_follower
== CEC_MODE_FOLLOWER
)
475 adap
->follower_cnt
++;
476 if (send_pin_event
) {
477 struct cec_event ev
= {
478 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
481 ev
.event
= adap
->cec_pin_is_high
? CEC_EVENT_PIN_CEC_HIGH
:
482 CEC_EVENT_PIN_CEC_LOW
;
483 cec_queue_event_fh(fh
, &ev
, 0);
485 if (mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
486 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
488 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
;
489 adap
->cec_follower
= fh
;
490 } else if (adap
->cec_follower
== fh
) {
491 adap
->passthrough
= false;
492 adap
->cec_follower
= NULL
;
494 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
)
495 adap
->cec_initiator
= fh
;
496 else if (adap
->cec_initiator
== fh
)
497 adap
->cec_initiator
= NULL
;
498 fh
->mode_initiator
= mode_initiator
;
499 fh
->mode_follower
= mode_follower
;
500 mutex_unlock(&adap
->lock
);
504 static long cec_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
506 struct cec_fh
*fh
= filp
->private_data
;
507 struct cec_adapter
*adap
= fh
->adap
;
508 bool block
= !(filp
->f_flags
& O_NONBLOCK
);
509 void __user
*parg
= (void __user
*)arg
;
511 if (!cec_is_registered(adap
))
515 case CEC_ADAP_G_CAPS
:
516 return cec_adap_g_caps(adap
, parg
);
518 case CEC_ADAP_G_PHYS_ADDR
:
519 return cec_adap_g_phys_addr(adap
, parg
);
521 case CEC_ADAP_S_PHYS_ADDR
:
522 return cec_adap_s_phys_addr(adap
, fh
, block
, parg
);
524 case CEC_ADAP_G_LOG_ADDRS
:
525 return cec_adap_g_log_addrs(adap
, parg
);
527 case CEC_ADAP_S_LOG_ADDRS
:
528 return cec_adap_s_log_addrs(adap
, fh
, block
, parg
);
530 case CEC_ADAP_G_CONNECTOR_INFO
:
531 return cec_adap_g_connector_info(adap
, parg
);
534 return cec_transmit(adap
, fh
, block
, parg
);
537 return cec_receive(adap
, fh
, block
, parg
);
540 return cec_dqevent(adap
, fh
, block
, parg
);
543 return cec_g_mode(adap
, fh
, parg
);
546 return cec_s_mode(adap
, fh
, parg
);
553 static int cec_open(struct inode
*inode
, struct file
*filp
)
555 struct cec_devnode
*devnode
=
556 container_of(inode
->i_cdev
, struct cec_devnode
, cdev
);
557 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
558 struct cec_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
560 * Initial events that are automatically sent when the cec device is
563 struct cec_event ev
= {
564 .event
= CEC_EVENT_STATE_CHANGE
,
565 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
573 INIT_LIST_HEAD(&fh
->msgs
);
574 INIT_LIST_HEAD(&fh
->xfer_list
);
575 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++)
576 INIT_LIST_HEAD(&fh
->events
[i
]);
577 mutex_init(&fh
->lock
);
578 init_waitqueue_head(&fh
->wait
);
580 fh
->mode_initiator
= CEC_MODE_INITIATOR
;
583 err
= cec_get_device(devnode
);
589 mutex_lock(&devnode
->lock
);
590 if (list_empty(&devnode
->fhs
) &&
592 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
593 err
= adap
->ops
->adap_enable(adap
, true);
595 mutex_unlock(&devnode
->lock
);
600 filp
->private_data
= fh
;
602 /* Queue up initial state events */
603 ev
.state_change
.phys_addr
= adap
->phys_addr
;
604 ev
.state_change
.log_addr_mask
= adap
->log_addrs
.log_addr_mask
;
605 ev
.state_change
.have_conn_info
=
606 adap
->conn_info
.type
!= CEC_CONNECTOR_TYPE_NO_CONNECTOR
;
607 cec_queue_event_fh(fh
, &ev
, 0);
608 #ifdef CONFIG_CEC_PIN
609 if (adap
->pin
&& adap
->pin
->ops
->read_hpd
) {
610 err
= adap
->pin
->ops
->read_hpd(adap
);
612 ev
.event
= err
? CEC_EVENT_PIN_HPD_HIGH
:
613 CEC_EVENT_PIN_HPD_LOW
;
614 cec_queue_event_fh(fh
, &ev
, 0);
617 if (adap
->pin
&& adap
->pin
->ops
->read_5v
) {
618 err
= adap
->pin
->ops
->read_5v(adap
);
620 ev
.event
= err
? CEC_EVENT_PIN_5V_HIGH
:
621 CEC_EVENT_PIN_5V_LOW
;
622 cec_queue_event_fh(fh
, &ev
, 0);
627 list_add(&fh
->list
, &devnode
->fhs
);
628 mutex_unlock(&devnode
->lock
);
633 /* Override for the release function */
634 static int cec_release(struct inode
*inode
, struct file
*filp
)
636 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
637 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
638 struct cec_fh
*fh
= filp
->private_data
;
641 mutex_lock(&adap
->lock
);
642 if (adap
->cec_initiator
== fh
)
643 adap
->cec_initiator
= NULL
;
644 if (adap
->cec_follower
== fh
) {
645 adap
->cec_follower
= NULL
;
646 adap
->passthrough
= false;
648 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
649 adap
->follower_cnt
--;
650 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
651 cec_monitor_pin_cnt_dec(adap
);
652 if (fh
->mode_follower
== CEC_MODE_MONITOR_ALL
)
653 cec_monitor_all_cnt_dec(adap
);
654 mutex_unlock(&adap
->lock
);
656 mutex_lock(&devnode
->lock
);
658 if (cec_is_registered(adap
) && list_empty(&devnode
->fhs
) &&
659 !adap
->needs_hpd
&& adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
660 WARN_ON(adap
->ops
->adap_enable(adap
, false));
662 mutex_unlock(&devnode
->lock
);
664 /* Unhook pending transmits from this filehandle. */
665 mutex_lock(&adap
->lock
);
666 while (!list_empty(&fh
->xfer_list
)) {
667 struct cec_data
*data
=
668 list_first_entry(&fh
->xfer_list
, struct cec_data
, xfer_list
);
670 data
->blocking
= false;
672 list_del(&data
->xfer_list
);
674 mutex_unlock(&adap
->lock
);
675 while (!list_empty(&fh
->msgs
)) {
676 struct cec_msg_entry
*entry
=
677 list_first_entry(&fh
->msgs
, struct cec_msg_entry
, list
);
679 list_del(&entry
->list
);
682 for (i
= CEC_NUM_CORE_EVENTS
; i
< CEC_NUM_EVENTS
; i
++) {
683 while (!list_empty(&fh
->events
[i
])) {
684 struct cec_event_entry
*entry
=
685 list_first_entry(&fh
->events
[i
],
686 struct cec_event_entry
, list
);
688 list_del(&entry
->list
);
694 cec_put_device(devnode
);
695 filp
->private_data
= NULL
;
699 const struct file_operations cec_devnode_fops
= {
700 .owner
= THIS_MODULE
,
702 .unlocked_ioctl
= cec_ioctl
,
703 .compat_ioctl
= cec_ioctl
,
704 .release
= cec_release
,