2 * vivid-cec.c - A Virtual Video Test Driver, cec emulation
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 <media/cec.h>
22 #include "vivid-core.h"
23 #include "vivid-cec.h"
25 #define CEC_TIM_START_BIT_TOTAL 4500
26 #define CEC_TIM_START_BIT_LOW 3700
27 #define CEC_TIM_START_BIT_HIGH 800
28 #define CEC_TIM_DATA_BIT_TOTAL 2400
29 #define CEC_TIM_DATA_BIT_0_LOW 1500
30 #define CEC_TIM_DATA_BIT_0_HIGH 900
31 #define CEC_TIM_DATA_BIT_1_LOW 600
32 #define CEC_TIM_DATA_BIT_1_HIGH 1800
34 void vivid_cec_bus_free_work(struct vivid_dev
*dev
)
36 spin_lock(&dev
->cec_slock
);
37 while (!list_empty(&dev
->cec_work_list
)) {
38 struct vivid_cec_work
*cw
=
39 list_first_entry(&dev
->cec_work_list
,
40 struct vivid_cec_work
, list
);
42 spin_unlock(&dev
->cec_slock
);
43 cancel_delayed_work_sync(&cw
->work
);
44 spin_lock(&dev
->cec_slock
);
46 cec_transmit_attempt_done(cw
->adap
, CEC_TX_STATUS_LOW_DRIVE
);
49 spin_unlock(&dev
->cec_slock
);
52 static bool vivid_cec_find_dest_adap(struct vivid_dev
*dev
,
53 struct cec_adapter
*adap
, u8 dest
)
60 if (adap
!= dev
->cec_rx_adap
&& dev
->cec_rx_adap
&&
61 dev
->cec_rx_adap
->is_configured
&&
62 cec_has_log_addr(dev
->cec_rx_adap
, dest
))
65 for (i
= 0; i
< MAX_OUTPUTS
&& dev
->cec_tx_adap
[i
]; i
++) {
66 if (adap
== dev
->cec_tx_adap
[i
])
68 if (!dev
->cec_tx_adap
[i
]->is_configured
)
70 if (cec_has_log_addr(dev
->cec_tx_adap
[i
], dest
))
76 static void vivid_cec_pin_adap_events(struct cec_adapter
*adap
, ktime_t ts
,
77 const struct cec_msg
*msg
, bool nacked
)
79 unsigned int len
= nacked
? 1 : msg
->len
;
85 ts
= ktime_sub_us(ts
, (CEC_TIM_START_BIT_TOTAL
+
86 len
* 10 * CEC_TIM_DATA_BIT_TOTAL
));
87 cec_queue_pin_cec_event(adap
, false, ts
);
88 ts
= ktime_add_us(ts
, CEC_TIM_START_BIT_LOW
);
89 cec_queue_pin_cec_event(adap
, true, ts
);
90 ts
= ktime_add_us(ts
, CEC_TIM_START_BIT_HIGH
);
92 for (i
= 0; i
< 10 * len
; i
++) {
95 bit
= msg
->msg
[i
/ 10] & (0x80 >> (i
% 10));
98 bit
= i
/ 10 == msg
->len
- 1;
101 bit
= cec_msg_is_broadcast(msg
) ^ nacked
;
104 cec_queue_pin_cec_event(adap
, false, ts
);
106 ts
= ktime_add_us(ts
, CEC_TIM_DATA_BIT_1_LOW
);
108 ts
= ktime_add_us(ts
, CEC_TIM_DATA_BIT_0_LOW
);
109 cec_queue_pin_cec_event(adap
, true, ts
);
111 ts
= ktime_add_us(ts
, CEC_TIM_DATA_BIT_1_HIGH
);
113 ts
= ktime_add_us(ts
, CEC_TIM_DATA_BIT_0_HIGH
);
117 static void vivid_cec_pin_events(struct vivid_dev
*dev
,
118 const struct cec_msg
*msg
, bool nacked
)
120 ktime_t ts
= ktime_get();
123 vivid_cec_pin_adap_events(dev
->cec_rx_adap
, ts
, msg
, nacked
);
124 for (i
= 0; i
< MAX_OUTPUTS
; i
++)
125 vivid_cec_pin_adap_events(dev
->cec_tx_adap
[i
], ts
, msg
, nacked
);
128 static void vivid_cec_xfer_done_worker(struct work_struct
*work
)
130 struct vivid_cec_work
*cw
=
131 container_of(work
, struct vivid_cec_work
, work
.work
);
132 struct vivid_dev
*dev
= cw
->dev
;
133 struct cec_adapter
*adap
= cw
->adap
;
134 u8 dest
= cec_msg_destination(&cw
->msg
);
138 valid_dest
= cec_msg_is_broadcast(&cw
->msg
);
140 valid_dest
= vivid_cec_find_dest_adap(dev
, adap
, dest
);
142 cw
->tx_status
= valid_dest
? CEC_TX_STATUS_OK
: CEC_TX_STATUS_NACK
;
143 spin_lock(&dev
->cec_slock
);
144 dev
->cec_xfer_time_jiffies
= 0;
145 dev
->cec_xfer_start_jiffies
= 0;
147 spin_unlock(&dev
->cec_slock
);
148 vivid_cec_pin_events(dev
, &cw
->msg
, !valid_dest
);
149 cec_transmit_attempt_done(cw
->adap
, cw
->tx_status
);
151 /* Broadcast message */
152 if (adap
!= dev
->cec_rx_adap
)
153 cec_received_msg(dev
->cec_rx_adap
, &cw
->msg
);
154 for (i
= 0; i
< MAX_OUTPUTS
&& dev
->cec_tx_adap
[i
]; i
++)
155 if (adap
!= dev
->cec_tx_adap
[i
])
156 cec_received_msg(dev
->cec_tx_adap
[i
], &cw
->msg
);
160 static void vivid_cec_xfer_try_worker(struct work_struct
*work
)
162 struct vivid_cec_work
*cw
=
163 container_of(work
, struct vivid_cec_work
, work
.work
);
164 struct vivid_dev
*dev
= cw
->dev
;
166 spin_lock(&dev
->cec_slock
);
167 if (dev
->cec_xfer_time_jiffies
) {
169 spin_unlock(&dev
->cec_slock
);
170 cec_transmit_attempt_done(cw
->adap
, CEC_TX_STATUS_ARB_LOST
);
173 INIT_DELAYED_WORK(&cw
->work
, vivid_cec_xfer_done_worker
);
174 dev
->cec_xfer_start_jiffies
= jiffies
;
175 dev
->cec_xfer_time_jiffies
= usecs_to_jiffies(cw
->usecs
);
176 spin_unlock(&dev
->cec_slock
);
177 schedule_delayed_work(&cw
->work
, dev
->cec_xfer_time_jiffies
);
181 static int vivid_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
183 adap
->cec_pin_is_high
= true;
187 static int vivid_cec_adap_log_addr(struct cec_adapter
*adap
, u8 log_addr
)
193 * One data bit takes 2400 us, each byte needs 10 bits so that's 24000 us
196 #define USECS_PER_BYTE 24000
198 static int vivid_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
199 u32 signal_free_time
, struct cec_msg
*msg
)
201 struct vivid_dev
*dev
= cec_get_drvdata(adap
);
202 struct vivid_cec_work
*cw
= kzalloc(sizeof(*cw
), GFP_KERNEL
);
203 long delta_jiffies
= 0;
209 cw
->usecs
= CEC_FREE_TIME_TO_USEC(signal_free_time
) +
210 msg
->len
* USECS_PER_BYTE
;
213 spin_lock(&dev
->cec_slock
);
214 list_add(&cw
->list
, &dev
->cec_work_list
);
215 if (dev
->cec_xfer_time_jiffies
== 0) {
216 INIT_DELAYED_WORK(&cw
->work
, vivid_cec_xfer_done_worker
);
217 dev
->cec_xfer_start_jiffies
= jiffies
;
218 dev
->cec_xfer_time_jiffies
= usecs_to_jiffies(cw
->usecs
);
219 delta_jiffies
= dev
->cec_xfer_time_jiffies
;
221 INIT_DELAYED_WORK(&cw
->work
, vivid_cec_xfer_try_worker
);
222 delta_jiffies
= dev
->cec_xfer_start_jiffies
+
223 dev
->cec_xfer_time_jiffies
- jiffies
;
225 spin_unlock(&dev
->cec_slock
);
226 schedule_delayed_work(&cw
->work
, delta_jiffies
< 0 ? 0 : delta_jiffies
);
230 static int vivid_received(struct cec_adapter
*adap
, struct cec_msg
*msg
)
232 struct vivid_dev
*dev
= cec_get_drvdata(adap
);
233 struct cec_msg reply
;
234 u8 dest
= cec_msg_destination(msg
);
238 if (cec_msg_is_broadcast(msg
))
239 dest
= adap
->log_addrs
.log_addr
[0];
240 cec_msg_init(&reply
, dest
, cec_msg_initiator(msg
));
242 switch (cec_msg_opcode(msg
)) {
243 case CEC_MSG_SET_OSD_STRING
:
244 if (!cec_is_sink(adap
))
246 cec_ops_set_osd_string(msg
, &disp_ctl
, osd
);
248 case CEC_OP_DISP_CTL_DEFAULT
:
249 strcpy(dev
->osd
, osd
);
250 dev
->osd_jiffies
= jiffies
;
252 case CEC_OP_DISP_CTL_UNTIL_CLEARED
:
253 strcpy(dev
->osd
, osd
);
254 dev
->osd_jiffies
= 0;
256 case CEC_OP_DISP_CTL_CLEAR
:
258 dev
->osd_jiffies
= 0;
261 cec_msg_feature_abort(&reply
, cec_msg_opcode(msg
),
262 CEC_OP_ABORT_INVALID_OP
);
263 cec_transmit_msg(adap
, &reply
, false);
273 static const struct cec_adap_ops vivid_cec_adap_ops
= {
274 .adap_enable
= vivid_cec_adap_enable
,
275 .adap_log_addr
= vivid_cec_adap_log_addr
,
276 .adap_transmit
= vivid_cec_adap_transmit
,
277 .received
= vivid_received
,
280 struct cec_adapter
*vivid_cec_alloc_adap(struct vivid_dev
*dev
,
284 char name
[sizeof(dev
->vid_out_dev
.name
) + 2];
285 u32 caps
= CEC_CAP_DEFAULTS
| CEC_CAP_MONITOR_ALL
| CEC_CAP_MONITOR_PIN
;
287 snprintf(name
, sizeof(name
), "%s%d",
288 is_source
? dev
->vid_out_dev
.name
: dev
->vid_cap_dev
.name
,
290 return cec_allocate_adapter(&vivid_cec_adap_ops
, dev
,