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>
36 static inline struct cec_devnode
*cec_devnode_data(struct file
*filp
)
38 struct cec_fh
*fh
= filp
->private_data
;
40 return &fh
->adap
->devnode
;
43 /* CEC file operations */
45 static unsigned int cec_poll(struct file
*filp
,
46 struct poll_table_struct
*poll
)
48 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
49 struct cec_fh
*fh
= filp
->private_data
;
50 struct cec_adapter
*adap
= fh
->adap
;
53 if (!devnode
->registered
)
54 return POLLERR
| POLLHUP
;
55 mutex_lock(&adap
->lock
);
56 if (adap
->is_configured
&&
57 adap
->transmit_queue_sz
< CEC_MAX_MSG_TX_QUEUE_SZ
)
58 res
|= POLLOUT
| POLLWRNORM
;
60 res
|= POLLIN
| POLLRDNORM
;
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
,
358 if (copy_from_user(&mode
, parg
, sizeof(mode
)))
360 if (mode
& ~(CEC_MODE_INITIATOR_MSK
| CEC_MODE_FOLLOWER_MSK
)) {
361 dprintk(1, "%s: invalid mode bits set\n", __func__
);
365 mode_initiator
= mode
& CEC_MODE_INITIATOR_MSK
;
366 mode_follower
= mode
& CEC_MODE_FOLLOWER_MSK
;
368 if (mode_initiator
> CEC_MODE_EXCL_INITIATOR
||
369 mode_follower
> CEC_MODE_MONITOR_ALL
) {
370 dprintk(1, "%s: unknown mode\n", __func__
);
374 if (mode_follower
== CEC_MODE_MONITOR_ALL
&&
375 !(adap
->capabilities
& CEC_CAP_MONITOR_ALL
)) {
376 dprintk(1, "%s: MONITOR_ALL not supported\n", __func__
);
380 if (mode_follower
== CEC_MODE_MONITOR_PIN
&&
381 !(adap
->capabilities
& CEC_CAP_MONITOR_PIN
)) {
382 dprintk(1, "%s: MONITOR_PIN not supported\n", __func__
);
386 /* Follower modes should always be able to send CEC messages */
387 if ((mode_initiator
== CEC_MODE_NO_INITIATOR
||
388 !(adap
->capabilities
& CEC_CAP_TRANSMIT
)) &&
389 mode_follower
>= CEC_MODE_FOLLOWER
&&
390 mode_follower
<= CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
391 dprintk(1, "%s: cannot transmit\n", __func__
);
395 /* Monitor modes require CEC_MODE_NO_INITIATOR */
396 if (mode_initiator
&& mode_follower
>= CEC_MODE_MONITOR_PIN
) {
397 dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
402 /* Monitor modes require CAP_NET_ADMIN */
403 if (mode_follower
>= CEC_MODE_MONITOR_PIN
&& !capable(CAP_NET_ADMIN
))
406 mutex_lock(&adap
->lock
);
408 * You can't become exclusive follower if someone else already
411 if ((mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
412 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) &&
413 adap
->cec_follower
&& adap
->cec_follower
!= fh
)
416 * You can't become exclusive initiator if someone else already
419 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
&&
420 adap
->cec_initiator
&& adap
->cec_initiator
!= fh
)
424 bool old_mon_all
= fh
->mode_follower
== CEC_MODE_MONITOR_ALL
;
425 bool new_mon_all
= mode_follower
== CEC_MODE_MONITOR_ALL
;
427 if (old_mon_all
!= new_mon_all
) {
429 err
= cec_monitor_all_cnt_inc(adap
);
431 cec_monitor_all_cnt_dec(adap
);
436 mutex_unlock(&adap
->lock
);
440 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
441 adap
->follower_cnt
--;
442 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
443 adap
->monitor_pin_cnt
--;
444 if (mode_follower
== CEC_MODE_FOLLOWER
)
445 adap
->follower_cnt
++;
446 if (mode_follower
== CEC_MODE_MONITOR_PIN
) {
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);
454 adap
->monitor_pin_cnt
++;
456 if (mode_follower
== CEC_MODE_EXCL_FOLLOWER
||
457 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
) {
459 mode_follower
== CEC_MODE_EXCL_FOLLOWER_PASSTHRU
;
460 adap
->cec_follower
= fh
;
461 } else if (adap
->cec_follower
== fh
) {
462 adap
->passthrough
= false;
463 adap
->cec_follower
= NULL
;
465 if (mode_initiator
== CEC_MODE_EXCL_INITIATOR
)
466 adap
->cec_initiator
= fh
;
467 else if (adap
->cec_initiator
== fh
)
468 adap
->cec_initiator
= NULL
;
469 fh
->mode_initiator
= mode_initiator
;
470 fh
->mode_follower
= mode_follower
;
471 mutex_unlock(&adap
->lock
);
475 static long cec_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
477 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
478 struct cec_fh
*fh
= filp
->private_data
;
479 struct cec_adapter
*adap
= fh
->adap
;
480 bool block
= !(filp
->f_flags
& O_NONBLOCK
);
481 void __user
*parg
= (void __user
*)arg
;
483 if (!devnode
->registered
)
487 case CEC_ADAP_G_CAPS
:
488 return cec_adap_g_caps(adap
, parg
);
490 case CEC_ADAP_G_PHYS_ADDR
:
491 return cec_adap_g_phys_addr(adap
, parg
);
493 case CEC_ADAP_S_PHYS_ADDR
:
494 return cec_adap_s_phys_addr(adap
, fh
, block
, parg
);
496 case CEC_ADAP_G_LOG_ADDRS
:
497 return cec_adap_g_log_addrs(adap
, parg
);
499 case CEC_ADAP_S_LOG_ADDRS
:
500 return cec_adap_s_log_addrs(adap
, fh
, block
, parg
);
503 return cec_transmit(adap
, fh
, block
, parg
);
506 return cec_receive(adap
, fh
, block
, parg
);
509 return cec_dqevent(adap
, fh
, block
, parg
);
512 return cec_g_mode(adap
, fh
, parg
);
515 return cec_s_mode(adap
, fh
, parg
);
522 static int cec_open(struct inode
*inode
, struct file
*filp
)
524 struct cec_devnode
*devnode
=
525 container_of(inode
->i_cdev
, struct cec_devnode
, cdev
);
526 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
527 struct cec_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
529 * Initial events that are automatically sent when the cec device is
532 struct cec_event ev_state
= {
533 .event
= CEC_EVENT_STATE_CHANGE
,
534 .flags
= CEC_EVENT_FL_INITIAL_STATE
,
542 INIT_LIST_HEAD(&fh
->msgs
);
543 INIT_LIST_HEAD(&fh
->xfer_list
);
544 for (i
= 0; i
< CEC_NUM_EVENTS
; i
++)
545 INIT_LIST_HEAD(&fh
->events
[i
]);
546 mutex_init(&fh
->lock
);
547 init_waitqueue_head(&fh
->wait
);
549 fh
->mode_initiator
= CEC_MODE_INITIATOR
;
552 err
= cec_get_device(devnode
);
558 mutex_lock(&devnode
->lock
);
559 if (list_empty(&devnode
->fhs
) &&
561 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
562 err
= adap
->ops
->adap_enable(adap
, true);
564 mutex_unlock(&devnode
->lock
);
569 filp
->private_data
= fh
;
571 /* Queue up initial state events */
572 ev_state
.state_change
.phys_addr
= adap
->phys_addr
;
573 ev_state
.state_change
.log_addr_mask
= adap
->log_addrs
.log_addr_mask
;
574 cec_queue_event_fh(fh
, &ev_state
, 0);
576 list_add(&fh
->list
, &devnode
->fhs
);
577 mutex_unlock(&devnode
->lock
);
582 /* Override for the release function */
583 static int cec_release(struct inode
*inode
, struct file
*filp
)
585 struct cec_devnode
*devnode
= cec_devnode_data(filp
);
586 struct cec_adapter
*adap
= to_cec_adapter(devnode
);
587 struct cec_fh
*fh
= filp
->private_data
;
590 mutex_lock(&adap
->lock
);
591 if (adap
->cec_initiator
== fh
)
592 adap
->cec_initiator
= NULL
;
593 if (adap
->cec_follower
== fh
) {
594 adap
->cec_follower
= NULL
;
595 adap
->passthrough
= false;
597 if (fh
->mode_follower
== CEC_MODE_FOLLOWER
)
598 adap
->follower_cnt
--;
599 if (fh
->mode_follower
== CEC_MODE_MONITOR_PIN
)
600 adap
->monitor_pin_cnt
--;
601 if (fh
->mode_follower
== CEC_MODE_MONITOR_ALL
)
602 cec_monitor_all_cnt_dec(adap
);
603 mutex_unlock(&adap
->lock
);
605 mutex_lock(&devnode
->lock
);
607 if (list_empty(&devnode
->fhs
) &&
609 adap
->phys_addr
== CEC_PHYS_ADDR_INVALID
) {
610 WARN_ON(adap
->ops
->adap_enable(adap
, false));
612 mutex_unlock(&devnode
->lock
);
614 /* Unhook pending transmits from this filehandle. */
615 mutex_lock(&adap
->lock
);
616 while (!list_empty(&fh
->xfer_list
)) {
617 struct cec_data
*data
=
618 list_first_entry(&fh
->xfer_list
, struct cec_data
, xfer_list
);
620 data
->blocking
= false;
622 list_del(&data
->xfer_list
);
624 mutex_unlock(&adap
->lock
);
625 while (!list_empty(&fh
->msgs
)) {
626 struct cec_msg_entry
*entry
=
627 list_first_entry(&fh
->msgs
, struct cec_msg_entry
, list
);
629 list_del(&entry
->list
);
632 for (i
= CEC_NUM_CORE_EVENTS
; i
< CEC_NUM_EVENTS
; i
++) {
633 while (!list_empty(&fh
->events
[i
])) {
634 struct cec_event_entry
*entry
=
635 list_first_entry(&fh
->events
[i
],
636 struct cec_event_entry
, list
);
638 list_del(&entry
->list
);
644 cec_put_device(devnode
);
645 filp
->private_data
= NULL
;
649 const struct file_operations cec_devnode_fops
= {
650 .owner
= THIS_MODULE
,
652 .unlocked_ioctl
= cec_ioctl
,
653 .release
= cec_release
,