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 if (!cec_is_registered(adap
))
42 return EPOLLERR
| EPOLLHUP
;
43 mutex_lock(&adap
->lock
);
44 if (adap
->is_configured
&&
45 adap
->transmit_queue_sz
< CEC_MAX_MSG_TX_QUEUE_SZ
)
46 res
|= EPOLLOUT
| EPOLLWRNORM
;
48 res
|= EPOLLIN
| EPOLLRDNORM
;
49 if (fh
->total_queued_events
)
51 poll_wait(filp
, &fh
->wait
, poll
);
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 strlcpy(caps
.driver
, adap
->devnode
.dev
.parent
->driver
->name
,
82 strlcpy(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 long cec_adap_s_phys_addr(struct cec_adapter
*adap
, struct cec_fh
*fh
,
105 bool block
, __u16 __user
*parg
)
110 if (!(adap
->capabilities
& CEC_CAP_PHYS_ADDR
))
112 if (copy_from_user(&phys_addr
, parg
, sizeof(phys_addr
)))
115 err
= cec_phys_addr_validate(phys_addr
, NULL
, NULL
);
118 mutex_lock(&adap
->lock
);
119 if (cec_is_busy(adap
, fh
))
122 __cec_s_phys_addr(adap
, phys_addr
, block
);
123 mutex_unlock(&adap
->lock
);
127 static long cec_adap_g_log_addrs(struct cec_adapter
*adap
,
128 struct cec_log_addrs __user
*parg
)
130 struct cec_log_addrs log_addrs
;
132 mutex_lock(&adap
->lock
);
133 log_addrs
= adap
->log_addrs
;
134 if (!adap
->is_configured
)
135 memset(log_addrs
.log_addr
, CEC_LOG_ADDR_INVALID
,
136 sizeof(log_addrs
.log_addr
));
137 mutex_unlock(&adap
->lock
);
139 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
144 static long cec_adap_s_log_addrs(struct cec_adapter
*adap
, struct cec_fh
*fh
,
145 bool block
, struct cec_log_addrs __user
*parg
)
147 struct cec_log_addrs log_addrs
;
150 if (!(adap
->capabilities
& CEC_CAP_LOG_ADDRS
))
152 if (copy_from_user(&log_addrs
, parg
, sizeof(log_addrs
)))
154 log_addrs
.flags
&= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK
|
155 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU
|
156 CEC_LOG_ADDRS_FL_CDC_ONLY
;
157 mutex_lock(&adap
->lock
);
158 if (!adap
->is_configuring
&&
159 (!log_addrs
.num_log_addrs
|| !adap
->is_configured
) &&
160 !cec_is_busy(adap
, fh
)) {
161 err
= __cec_s_log_addrs(adap
, &log_addrs
, block
);
163 log_addrs
= adap
->log_addrs
;
165 mutex_unlock(&adap
->lock
);
168 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
173 static long cec_transmit(struct cec_adapter
*adap
, struct cec_fh
*fh
,
174 bool block
, struct cec_msg __user
*parg
)
176 struct cec_msg msg
= {};
179 if (!(adap
->capabilities
& CEC_CAP_TRANSMIT
))
181 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
184 /* A CDC-Only device can only send CDC messages */
185 if ((adap
->log_addrs
.flags
& CEC_LOG_ADDRS_FL_CDC_ONLY
) &&
186 (msg
.len
== 1 || msg
.msg
[1] != CEC_MSG_CDC_MESSAGE
))
189 mutex_lock(&adap
->lock
);
190 if (adap
->log_addrs
.num_log_addrs
== 0)
192 else if (adap
->is_configuring
)
194 else if (!adap
->is_configured
&&
195 (adap
->needs_hpd
|| msg
.msg
[0] != 0xf0))
197 else if (cec_is_busy(adap
, fh
))
200 err
= cec_transmit_msg_fh(adap
, &msg
, fh
, block
);
201 mutex_unlock(&adap
->lock
);
204 if (copy_to_user(parg
, &msg
, sizeof(msg
)))
209 /* Called by CEC_RECEIVE: wait for a message to arrive */
210 static int cec_receive_msg(struct cec_fh
*fh
, struct cec_msg
*msg
, bool block
)
212 u32 timeout
= msg
->timeout
;
216 mutex_lock(&fh
->lock
);
217 /* Are there received messages queued up? */
218 if (fh
->queued_msgs
) {
219 /* Yes, return the first one */
220 struct cec_msg_entry
*entry
=
221 list_first_entry(&fh
->msgs
,
222 struct cec_msg_entry
, list
);
224 list_del(&entry
->list
);
228 mutex_unlock(&fh
->lock
);
229 /* restore original timeout value */
230 msg
->timeout
= timeout
;
234 /* No, return EAGAIN in non-blocking mode or wait */
235 mutex_unlock(&fh
->lock
);
237 /* Return when in non-blocking mode */
242 /* The user specified a timeout */
243 res
= wait_event_interruptible_timeout(fh
->wait
,
245 msecs_to_jiffies(msg
->timeout
));
251 /* Wait indefinitely */
252 res
= wait_event_interruptible(fh
->wait
,
255 /* Exit on error, otherwise loop to get the new message */
260 static long cec_receive(struct cec_adapter
*adap
, struct cec_fh
*fh
,
261 bool block
, struct cec_msg __user
*parg
)
263 struct cec_msg msg
= {};
266 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
269 err
= cec_receive_msg(fh
, &msg
, block
);
273 if (copy_to_user(parg
, &msg
, sizeof(msg
)))
278 static long cec_dqevent(struct cec_adapter
*adap
, struct cec_fh
*fh
,
279 bool block
, struct cec_event __user
*parg
)
281 struct cec_event_entry
*ev
= NULL
;
287 mutex_lock(&fh
->lock
);
288 while (!fh
->total_queued_events
&& block
) {
289 mutex_unlock(&fh
->lock
);
290 err
= wait_event_interruptible(fh
->wait
,
291 fh
->total_queued_events
);
294 mutex_lock(&fh
->lock
);
297 /* Find the oldest event */
298 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++) {
299 struct cec_event_entry
*entry
=
300 list_first_entry_or_null(&fh
->events
[i
],
301 struct cec_event_entry
, list
);
303 if (entry
&& entry
->ev
.ts
<= ts
) {
316 if (copy_to_user(parg
, &ev
->ev
, sizeof(ev
->ev
)))
318 if (ev_idx
>= CEC_NUM_CORE_EVENTS
)
320 fh
->queued_events
[ev_idx
]--;
321 fh
->total_queued_events
--;
324 mutex_unlock(&fh
->lock
);
328 static long cec_g_mode(struct cec_adapter
*adap
, struct cec_fh
*fh
,
331 u32 mode
= fh
->mode_initiator
| fh
->mode_follower
;
333 if (copy_to_user(parg
, &mode
, sizeof(mode
)))
338 static long cec_s_mode(struct cec_adapter
*adap
, struct cec_fh
*fh
,
344 bool send_pin_event
= false;
347 if (copy_from_user(&mode
, parg
, sizeof(mode
)))
349 if (mode
& ~(CEC_MODE_INITIATOR_MSK
| CEC_MODE_FOLLOWER_MSK
)) {
350 dprintk(1, "%s: invalid mode bits set\n", __func__
);
354 mode_initiator
= mode
& CEC_MODE_INITIATOR_MSK
;
355 mode_follower
= mode
& CEC_MODE_FOLLOWER_MSK
;
357 if (mode_initiator
> CEC_MODE_EXCL_INITIATOR
||
358 mode_follower
> CEC_MODE_MONITOR_ALL
) {
359 dprintk(1, "%s: unknown mode\n", __func__
);
363 if (mode_follower
== CEC_MODE_MONITOR_ALL
&&
364 !(adap
->capabilities
& CEC_CAP_MONITOR_ALL
)) {
365 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__
);
369 if (mode_follower
== CEC_MODE_MONITOR_PIN
&&
370 !(adap
->capabilities
& CEC_CAP_MONITOR_PIN
)) {
371 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__
);
375 /* Follower modes should always be able to send CEC messages */
376 if ((mode_initiator
== CEC_MODE_NO_INITIATOR
||
377 !(adap
->capabilities
& CEC_CAP_TRANSMIT
)) &&
378 mode_follower
>= CEC_MODE_FOLLOWER
&&
379 mode_follower
<= CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
380 dprintk(1, "%s: cannot transmit\n", __func__
);
384 /* Monitor modes require CEC_MODE_NO_INITIATOR */
385 if (mode_initiator
&& mode_follower
>= CEC_MODE_MONITOR_PIN
) {
386 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
391 /* Monitor modes require CAP_NET_ADMIN */
392 if (mode_follower
>= CEC_MODE_MONITOR_PIN
&& !capable(CAP_NET_ADMIN
))
395 mutex_lock(&adap
->lock
);
397 * You can't become exclusive follower if someone else already
400 if ((mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
401 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) &&
402 adap
->cec_follower
&& adap
->cec_follower
!= fh
)
405 * You can't become exclusive initiator if someone else already
408 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
&&
409 adap
->cec_initiator
&& adap
->cec_initiator
!= fh
)
413 bool old_mon_all
= fh
->mode_follower
== CEC_MODE_MONITOR_ALL
;
414 bool new_mon_all
= mode_follower
== CEC_MODE_MONITOR_ALL
;
416 if (old_mon_all
!= new_mon_all
) {
418 err
= cec_monitor_all_cnt_inc(adap
);
420 cec_monitor_all_cnt_dec(adap
);
425 bool old_mon_pin
= fh
->mode_follower
== CEC_MODE_MONITOR_PIN
;
426 bool new_mon_pin
= mode_follower
== CEC_MODE_MONITOR_PIN
;
428 if (old_mon_pin
!= new_mon_pin
) {
429 send_pin_event
= new_mon_pin
;
431 err
= cec_monitor_pin_cnt_inc(adap
);
433 cec_monitor_pin_cnt_dec(adap
);
438 mutex_unlock(&adap
->lock
);
442 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
443 adap
->follower_cnt
--;
444 if (mode_follower
== CEC_MODE_FOLLOWER
)
445 adap
->follower_cnt
++;
446 if (send_pin_event
) {
447 struct cec_event ev
= {
448 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
451 ev
.event
= adap
->cec_pin_is_high
? CEC_EVENT_PIN_CEC_HIGH
:
452 CEC_EVENT_PIN_CEC_LOW
;
453 cec_queue_event_fh(fh
, &ev
, 0);
455 if (mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
456 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
458 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
;
459 adap
->cec_follower
= fh
;
460 } else if (adap
->cec_follower
== fh
) {
461 adap
->passthrough
= false;
462 adap
->cec_follower
= NULL
;
464 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
)
465 adap
->cec_initiator
= fh
;
466 else if (adap
->cec_initiator
== fh
)
467 adap
->cec_initiator
= NULL
;
468 fh
->mode_initiator
= mode_initiator
;
469 fh
->mode_follower
= mode_follower
;
470 mutex_unlock(&adap
->lock
);
474 static long cec_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
476 struct cec_fh
*fh
= filp
->private_data
;
477 struct cec_adapter
*adap
= fh
->adap
;
478 bool block
= !(filp
->f_flags
& O_NONBLOCK
);
479 void __user
*parg
= (void __user
*)arg
;
481 if (!cec_is_registered(adap
))
485 case CEC_ADAP_G_CAPS
:
486 return cec_adap_g_caps(adap
, parg
);
488 case CEC_ADAP_G_PHYS_ADDR
:
489 return cec_adap_g_phys_addr(adap
, parg
);
491 case CEC_ADAP_S_PHYS_ADDR
:
492 return cec_adap_s_phys_addr(adap
, fh
, block
, parg
);
494 case CEC_ADAP_G_LOG_ADDRS
:
495 return cec_adap_g_log_addrs(adap
, parg
);
497 case CEC_ADAP_S_LOG_ADDRS
:
498 return cec_adap_s_log_addrs(adap
, fh
, block
, parg
);
501 return cec_transmit(adap
, fh
, block
, parg
);
504 return cec_receive(adap
, fh
, block
, parg
);
507 return cec_dqevent(adap
, fh
, block
, parg
);
510 return cec_g_mode(adap
, fh
, parg
);
513 return cec_s_mode(adap
, fh
, parg
);
520 static int cec_open(struct inode
*inode
, struct file
*filp
)
522 struct cec_devnode
*devnode
=
523 container_of(inode
->i_cdev
, struct cec_devnode
, cdev
);
524 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
525 struct cec_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
527 * Initial events that are automatically sent when the cec device is
530 struct cec_event ev
= {
531 .event
= CEC_EVENT_STATE_CHANGE
,
532 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
540 INIT_LIST_HEAD(&fh
->msgs
);
541 INIT_LIST_HEAD(&fh
->xfer_list
);
542 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++)
543 INIT_LIST_HEAD(&fh
->events
[i
]);
544 mutex_init(&fh
->lock
);
545 init_waitqueue_head(&fh
->wait
);
547 fh
->mode_initiator
= CEC_MODE_INITIATOR
;
550 err
= cec_get_device(devnode
);
556 mutex_lock(&devnode
->lock
);
557 if (list_empty(&devnode
->fhs
) &&
559 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
560 err
= adap
->ops
->adap_enable(adap
, true);
562 mutex_unlock(&devnode
->lock
);
567 filp
->private_data
= fh
;
569 /* Queue up initial state events */
570 ev
.state_change
.phys_addr
= adap
->phys_addr
;
571 ev
.state_change
.log_addr_mask
= adap
->log_addrs
.log_addr_mask
;
572 cec_queue_event_fh(fh
, &ev
, 0);
573 #ifdef CONFIG_CEC_PIN
574 if (adap
->pin
&& adap
->pin
->ops
->read_hpd
) {
575 err
= adap
->pin
->ops
->read_hpd(adap
);
577 ev
.event
= err
? CEC_EVENT_PIN_HPD_HIGH
:
578 CEC_EVENT_PIN_HPD_LOW
;
579 cec_queue_event_fh(fh
, &ev
, 0);
584 list_add(&fh
->list
, &devnode
->fhs
);
585 mutex_unlock(&devnode
->lock
);
590 /* Override for the release function */
591 static int cec_release(struct inode
*inode
, struct file
*filp
)
593 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
594 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
595 struct cec_fh
*fh
= filp
->private_data
;
598 mutex_lock(&adap
->lock
);
599 if (adap
->cec_initiator
== fh
)
600 adap
->cec_initiator
= NULL
;
601 if (adap
->cec_follower
== fh
) {
602 adap
->cec_follower
= NULL
;
603 adap
->passthrough
= false;
605 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
606 adap
->follower_cnt
--;
607 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
608 cec_monitor_pin_cnt_dec(adap
);
609 if (fh
->mode_follower
== CEC_MODE_MONITOR_ALL
)
610 cec_monitor_all_cnt_dec(adap
);
611 mutex_unlock(&adap
->lock
);
613 mutex_lock(&devnode
->lock
);
615 if (cec_is_registered(adap
) && list_empty(&devnode
->fhs
) &&
616 !adap
->needs_hpd
&& adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
617 WARN_ON(adap
->ops
->adap_enable(adap
, false));
619 mutex_unlock(&devnode
->lock
);
621 /* Unhook pending transmits from this filehandle. */
622 mutex_lock(&adap
->lock
);
623 while (!list_empty(&fh
->xfer_list
)) {
624 struct cec_data
*data
=
625 list_first_entry(&fh
->xfer_list
, struct cec_data
, xfer_list
);
627 data
->blocking
= false;
629 list_del(&data
->xfer_list
);
631 mutex_unlock(&adap
->lock
);
632 while (!list_empty(&fh
->msgs
)) {
633 struct cec_msg_entry
*entry
=
634 list_first_entry(&fh
->msgs
, struct cec_msg_entry
, list
);
636 list_del(&entry
->list
);
639 for (i
= CEC_NUM_CORE_EVENTS
; i
< CEC_NUM_EVENTS
; i
++) {
640 while (!list_empty(&fh
->events
[i
])) {
641 struct cec_event_entry
*entry
=
642 list_first_entry(&fh
->events
[i
],
643 struct cec_event_entry
, list
);
645 list_del(&entry
->list
);
651 cec_put_device(devnode
);
652 filp
->private_data
= NULL
;
656 const struct file_operations cec_devnode_fops
= {
657 .owner
= THIS_MODULE
,
659 .unlocked_ioctl
= cec_ioctl
,
660 .release
= cec_release
,