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 __poll_t
cec_poll(struct file
*filp
,
47 struct poll_table_struct
*poll
)
49 struct cec_fh
*fh
= filp
->private_data
;
50 struct cec_adapter
*adap
= fh
->adap
;
53 if (!cec_is_registered(adap
))
54 return EPOLLERR
| EPOLLHUP
;
55 mutex_lock(&adap
->lock
);
56 if (adap
->is_configured
&&
57 adap
->transmit_queue_sz
< CEC_MAX_MSG_TX_QUEUE_SZ
)
58 res
|= EPOLLOUT
| EPOLLWRNORM
;
60 res
|= EPOLLIN
| EPOLLRDNORM
;
61 if (fh
->total_queued_events
)
63 poll_wait(filp
, &fh
->wait
, poll
);
64 mutex_unlock(&adap
->lock
);
68 static bool cec_is_busy(const struct cec_adapter
*adap
,
69 const struct cec_fh
*fh
)
71 bool valid_initiator
= adap
->cec_initiator
&& adap
->cec_initiator
== fh
;
72 bool valid_follower
= adap
->cec_follower
&& adap
->cec_follower
== fh
;
75 * Exclusive initiators and followers can always access the CEC adapter
77 if (valid_initiator
|| valid_follower
)
80 * All others can only access the CEC adapter if there is no
81 * exclusive initiator and they are in INITIATOR mode.
83 return adap
->cec_initiator
||
84 fh
->mode_initiator
== CEC_MODE_NO_INITIATOR
;
87 static long cec_adap_g_caps(struct cec_adapter
*adap
,
88 struct cec_caps __user
*parg
)
90 struct cec_caps caps
= {};
92 strlcpy(caps
.driver
, adap
->devnode
.dev
.parent
->driver
->name
,
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
)))
103 static long cec_adap_g_phys_addr(struct cec_adapter
*adap
,
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
)))
116 static long cec_adap_s_phys_addr(struct cec_adapter
*adap
, struct cec_fh
*fh
,
117 bool block
, __u16 __user
*parg
)
122 if (!(adap
->capabilities
& CEC_CAP_PHYS_ADDR
))
124 if (copy_from_user(&phys_addr
, parg
, sizeof(phys_addr
)))
127 err
= cec_phys_addr_validate(phys_addr
, NULL
, NULL
);
130 mutex_lock(&adap
->lock
);
131 if (cec_is_busy(adap
, fh
))
134 __cec_s_phys_addr(adap
, phys_addr
, block
);
135 mutex_unlock(&adap
->lock
);
139 static long cec_adap_g_log_addrs(struct cec_adapter
*adap
,
140 struct cec_log_addrs __user
*parg
)
142 struct cec_log_addrs log_addrs
;
144 mutex_lock(&adap
->lock
);
145 log_addrs
= adap
->log_addrs
;
146 if (!adap
->is_configured
)
147 memset(log_addrs
.log_addr
, CEC_LOG_ADDR_INVALID
,
148 sizeof(log_addrs
.log_addr
));
149 mutex_unlock(&adap
->lock
);
151 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
156 static long cec_adap_s_log_addrs(struct cec_adapter
*adap
, struct cec_fh
*fh
,
157 bool block
, struct cec_log_addrs __user
*parg
)
159 struct cec_log_addrs log_addrs
;
162 if (!(adap
->capabilities
& CEC_CAP_LOG_ADDRS
))
164 if (copy_from_user(&log_addrs
, parg
, sizeof(log_addrs
)))
166 log_addrs
.flags
&= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK
|
167 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU
|
168 CEC_LOG_ADDRS_FL_CDC_ONLY
;
169 mutex_lock(&adap
->lock
);
170 if (!adap
->is_configuring
&&
171 (!log_addrs
.num_log_addrs
|| !adap
->is_configured
) &&
172 !cec_is_busy(adap
, fh
)) {
173 err
= __cec_s_log_addrs(adap
, &log_addrs
, block
);
175 log_addrs
= adap
->log_addrs
;
177 mutex_unlock(&adap
->lock
);
180 if (copy_to_user(parg
, &log_addrs
, sizeof(log_addrs
)))
185 static long cec_transmit(struct cec_adapter
*adap
, struct cec_fh
*fh
,
186 bool block
, struct cec_msg __user
*parg
)
188 struct cec_msg msg
= {};
191 if (!(adap
->capabilities
& CEC_CAP_TRANSMIT
))
193 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
196 /* A CDC-Only device can only send CDC messages */
197 if ((adap
->log_addrs
.flags
& CEC_LOG_ADDRS_FL_CDC_ONLY
) &&
198 (msg
.len
== 1 || msg
.msg
[1] != CEC_MSG_CDC_MESSAGE
))
201 mutex_lock(&adap
->lock
);
202 if (adap
->log_addrs
.num_log_addrs
== 0)
204 else if (adap
->is_configuring
)
206 else if (!adap
->is_configured
&&
207 (adap
->needs_hpd
|| msg
.msg
[0] != 0xf0))
209 else if (cec_is_busy(adap
, fh
))
212 err
= cec_transmit_msg_fh(adap
, &msg
, fh
, block
);
213 mutex_unlock(&adap
->lock
);
216 if (copy_to_user(parg
, &msg
, sizeof(msg
)))
221 /* Called by CEC_RECEIVE: wait for a message to arrive */
222 static int cec_receive_msg(struct cec_fh
*fh
, struct cec_msg
*msg
, bool block
)
224 u32 timeout
= msg
->timeout
;
228 mutex_lock(&fh
->lock
);
229 /* Are there received messages queued up? */
230 if (fh
->queued_msgs
) {
231 /* Yes, return the first one */
232 struct cec_msg_entry
*entry
=
233 list_first_entry(&fh
->msgs
,
234 struct cec_msg_entry
, list
);
236 list_del(&entry
->list
);
240 mutex_unlock(&fh
->lock
);
241 /* restore original timeout value */
242 msg
->timeout
= timeout
;
246 /* No, return EAGAIN in non-blocking mode or wait */
247 mutex_unlock(&fh
->lock
);
249 /* Return when in non-blocking mode */
254 /* The user specified a timeout */
255 res
= wait_event_interruptible_timeout(fh
->wait
,
257 msecs_to_jiffies(msg
->timeout
));
263 /* Wait indefinitely */
264 res
= wait_event_interruptible(fh
->wait
,
267 /* Exit on error, otherwise loop to get the new message */
272 static long cec_receive(struct cec_adapter
*adap
, struct cec_fh
*fh
,
273 bool block
, struct cec_msg __user
*parg
)
275 struct cec_msg msg
= {};
278 if (copy_from_user(&msg
, parg
, sizeof(msg
)))
281 err
= cec_receive_msg(fh
, &msg
, block
);
285 if (copy_to_user(parg
, &msg
, sizeof(msg
)))
290 static long cec_dqevent(struct cec_adapter
*adap
, struct cec_fh
*fh
,
291 bool block
, struct cec_event __user
*parg
)
293 struct cec_event_entry
*ev
= NULL
;
299 mutex_lock(&fh
->lock
);
300 while (!fh
->total_queued_events
&& block
) {
301 mutex_unlock(&fh
->lock
);
302 err
= wait_event_interruptible(fh
->wait
,
303 fh
->total_queued_events
);
306 mutex_lock(&fh
->lock
);
309 /* Find the oldest event */
310 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++) {
311 struct cec_event_entry
*entry
=
312 list_first_entry_or_null(&fh
->events
[i
],
313 struct cec_event_entry
, list
);
315 if (entry
&& entry
->ev
.ts
<= ts
) {
328 if (copy_to_user(parg
, &ev
->ev
, sizeof(ev
->ev
)))
330 if (ev_idx
>= CEC_NUM_CORE_EVENTS
)
332 fh
->queued_events
[ev_idx
]--;
333 fh
->total_queued_events
--;
336 mutex_unlock(&fh
->lock
);
340 static long cec_g_mode(struct cec_adapter
*adap
, struct cec_fh
*fh
,
343 u32 mode
= fh
->mode_initiator
| fh
->mode_follower
;
345 if (copy_to_user(parg
, &mode
, sizeof(mode
)))
350 static long cec_s_mode(struct cec_adapter
*adap
, struct cec_fh
*fh
,
356 bool send_pin_event
= false;
359 if (copy_from_user(&mode
, parg
, sizeof(mode
)))
361 if (mode
& ~(CEC_MODE_INITIATOR_MSK
| CEC_MODE_FOLLOWER_MSK
)) {
362 dprintk(1, "%s: invalid mode bits set\n", __func__
);
366 mode_initiator
= mode
& CEC_MODE_INITIATOR_MSK
;
367 mode_follower
= mode
& CEC_MODE_FOLLOWER_MSK
;
369 if (mode_initiator
> CEC_MODE_EXCL_INITIATOR
||
370 mode_follower
> CEC_MODE_MONITOR_ALL
) {
371 dprintk(1, "%s: unknown mode\n", __func__
);
375 if (mode_follower
== CEC_MODE_MONITOR_ALL
&&
376 !(adap
->capabilities
& CEC_CAP_MONITOR_ALL
)) {
377 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__
);
381 if (mode_follower
== CEC_MODE_MONITOR_PIN
&&
382 !(adap
->capabilities
& CEC_CAP_MONITOR_PIN
)) {
383 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__
);
387 /* Follower modes should always be able to send CEC messages */
388 if ((mode_initiator
== CEC_MODE_NO_INITIATOR
||
389 !(adap
->capabilities
& CEC_CAP_TRANSMIT
)) &&
390 mode_follower
>= CEC_MODE_FOLLOWER
&&
391 mode_follower
<= CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
392 dprintk(1, "%s: cannot transmit\n", __func__
);
396 /* Monitor modes require CEC_MODE_NO_INITIATOR */
397 if (mode_initiator
&& mode_follower
>= CEC_MODE_MONITOR_PIN
) {
398 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
403 /* Monitor modes require CAP_NET_ADMIN */
404 if (mode_follower
>= CEC_MODE_MONITOR_PIN
&& !capable(CAP_NET_ADMIN
))
407 mutex_lock(&adap
->lock
);
409 * You can't become exclusive follower if someone else already
412 if ((mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
413 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) &&
414 adap
->cec_follower
&& adap
->cec_follower
!= fh
)
417 * You can't become exclusive initiator if someone else already
420 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
&&
421 adap
->cec_initiator
&& adap
->cec_initiator
!= fh
)
425 bool old_mon_all
= fh
->mode_follower
== CEC_MODE_MONITOR_ALL
;
426 bool new_mon_all
= mode_follower
== CEC_MODE_MONITOR_ALL
;
428 if (old_mon_all
!= new_mon_all
) {
430 err
= cec_monitor_all_cnt_inc(adap
);
432 cec_monitor_all_cnt_dec(adap
);
437 bool old_mon_pin
= fh
->mode_follower
== CEC_MODE_MONITOR_PIN
;
438 bool new_mon_pin
= mode_follower
== CEC_MODE_MONITOR_PIN
;
440 if (old_mon_pin
!= new_mon_pin
) {
441 send_pin_event
= new_mon_pin
;
443 err
= cec_monitor_pin_cnt_inc(adap
);
445 cec_monitor_pin_cnt_dec(adap
);
450 mutex_unlock(&adap
->lock
);
454 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
455 adap
->follower_cnt
--;
456 if (mode_follower
== CEC_MODE_FOLLOWER
)
457 adap
->follower_cnt
++;
458 if (send_pin_event
) {
459 struct cec_event ev
= {
460 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
463 ev
.event
= adap
->cec_pin_is_high
? CEC_EVENT_PIN_CEC_HIGH
:
464 CEC_EVENT_PIN_CEC_LOW
;
465 cec_queue_event_fh(fh
, &ev
, 0);
467 if (mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
468 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
470 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
;
471 adap
->cec_follower
= fh
;
472 } else if (adap
->cec_follower
== fh
) {
473 adap
->passthrough
= false;
474 adap
->cec_follower
= NULL
;
476 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
)
477 adap
->cec_initiator
= fh
;
478 else if (adap
->cec_initiator
== fh
)
479 adap
->cec_initiator
= NULL
;
480 fh
->mode_initiator
= mode_initiator
;
481 fh
->mode_follower
= mode_follower
;
482 mutex_unlock(&adap
->lock
);
486 static long cec_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
488 struct cec_fh
*fh
= filp
->private_data
;
489 struct cec_adapter
*adap
= fh
->adap
;
490 bool block
= !(filp
->f_flags
& O_NONBLOCK
);
491 void __user
*parg
= (void __user
*)arg
;
493 if (!cec_is_registered(adap
))
497 case CEC_ADAP_G_CAPS
:
498 return cec_adap_g_caps(adap
, parg
);
500 case CEC_ADAP_G_PHYS_ADDR
:
501 return cec_adap_g_phys_addr(adap
, parg
);
503 case CEC_ADAP_S_PHYS_ADDR
:
504 return cec_adap_s_phys_addr(adap
, fh
, block
, parg
);
506 case CEC_ADAP_G_LOG_ADDRS
:
507 return cec_adap_g_log_addrs(adap
, parg
);
509 case CEC_ADAP_S_LOG_ADDRS
:
510 return cec_adap_s_log_addrs(adap
, fh
, block
, parg
);
513 return cec_transmit(adap
, fh
, block
, parg
);
516 return cec_receive(adap
, fh
, block
, parg
);
519 return cec_dqevent(adap
, fh
, block
, parg
);
522 return cec_g_mode(adap
, fh
, parg
);
525 return cec_s_mode(adap
, fh
, parg
);
532 static int cec_open(struct inode
*inode
, struct file
*filp
)
534 struct cec_devnode
*devnode
=
535 container_of(inode
->i_cdev
, struct cec_devnode
, cdev
);
536 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
537 struct cec_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
539 * Initial events that are automatically sent when the cec device is
542 struct cec_event ev
= {
543 .event
= CEC_EVENT_STATE_CHANGE
,
544 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
552 INIT_LIST_HEAD(&fh
->msgs
);
553 INIT_LIST_HEAD(&fh
->xfer_list
);
554 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++)
555 INIT_LIST_HEAD(&fh
->events
[i
]);
556 mutex_init(&fh
->lock
);
557 init_waitqueue_head(&fh
->wait
);
559 fh
->mode_initiator
= CEC_MODE_INITIATOR
;
562 err
= cec_get_device(devnode
);
568 mutex_lock(&devnode
->lock
);
569 if (list_empty(&devnode
->fhs
) &&
571 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
572 err
= adap
->ops
->adap_enable(adap
, true);
574 mutex_unlock(&devnode
->lock
);
579 filp
->private_data
= fh
;
581 /* Queue up initial state events */
582 ev
.state_change
.phys_addr
= adap
->phys_addr
;
583 ev
.state_change
.log_addr_mask
= adap
->log_addrs
.log_addr_mask
;
584 cec_queue_event_fh(fh
, &ev
, 0);
585 #ifdef CONFIG_CEC_PIN
586 if (adap
->pin
&& adap
->pin
->ops
->read_hpd
) {
587 err
= adap
->pin
->ops
->read_hpd(adap
);
589 ev
.event
= err
? CEC_EVENT_PIN_HPD_HIGH
:
590 CEC_EVENT_PIN_HPD_LOW
;
591 cec_queue_event_fh(fh
, &ev
, 0);
596 list_add(&fh
->list
, &devnode
->fhs
);
597 mutex_unlock(&devnode
->lock
);
602 /* Override for the release function */
603 static int cec_release(struct inode
*inode
, struct file
*filp
)
605 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
606 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
607 struct cec_fh
*fh
= filp
->private_data
;
610 mutex_lock(&adap
->lock
);
611 if (adap
->cec_initiator
== fh
)
612 adap
->cec_initiator
= NULL
;
613 if (adap
->cec_follower
== fh
) {
614 adap
->cec_follower
= NULL
;
615 adap
->passthrough
= false;
617 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
618 adap
->follower_cnt
--;
619 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
620 cec_monitor_pin_cnt_dec(adap
);
621 if (fh
->mode_follower
== CEC_MODE_MONITOR_ALL
)
622 cec_monitor_all_cnt_dec(adap
);
623 mutex_unlock(&adap
->lock
);
625 mutex_lock(&devnode
->lock
);
627 if (cec_is_registered(adap
) && list_empty(&devnode
->fhs
) &&
628 !adap
->needs_hpd
&& adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
629 WARN_ON(adap
->ops
->adap_enable(adap
, false));
631 mutex_unlock(&devnode
->lock
);
633 /* Unhook pending transmits from this filehandle. */
634 mutex_lock(&adap
->lock
);
635 while (!list_empty(&fh
->xfer_list
)) {
636 struct cec_data
*data
=
637 list_first_entry(&fh
->xfer_list
, struct cec_data
, xfer_list
);
639 data
->blocking
= false;
641 list_del(&data
->xfer_list
);
643 mutex_unlock(&adap
->lock
);
644 while (!list_empty(&fh
->msgs
)) {
645 struct cec_msg_entry
*entry
=
646 list_first_entry(&fh
->msgs
, struct cec_msg_entry
, list
);
648 list_del(&entry
->list
);
651 for (i
= CEC_NUM_CORE_EVENTS
; i
< CEC_NUM_EVENTS
; i
++) {
652 while (!list_empty(&fh
->events
[i
])) {
653 struct cec_event_entry
*entry
=
654 list_first_entry(&fh
->events
[i
],
655 struct cec_event_entry
, list
);
657 list_del(&entry
->list
);
663 cec_put_device(devnode
);
664 filp
->private_data
= NULL
;
668 const struct file_operations cec_devnode_fops
= {
669 .owner
= THIS_MODULE
,
671 .unlocked_ioctl
= cec_ioctl
,
672 .release
= cec_release
,