1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-cec.c - A Virtual Video Test Driver, cec emulation
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
10 #include "vivid-core.h"
11 #include "vivid-cec.h"
13 #define CEC_TIM_START_BIT_TOTAL 4500
14 #define CEC_TIM_START_BIT_LOW 3700
15 #define CEC_TIM_START_BIT_HIGH 800
16 #define CEC_TIM_DATA_BIT_TOTAL 2400
17 #define CEC_TIM_DATA_BIT_0_LOW 1500
18 #define CEC_TIM_DATA_BIT_0_HIGH 900
19 #define CEC_TIM_DATA_BIT_1_LOW 600
20 #define CEC_TIM_DATA_BIT_1_HIGH 1800
22 void vivid_cec_bus_free_work(struct vivid_dev *dev)
24 spin_lock(&dev->cec_slock);
25 while (!list_empty(&dev->cec_work_list)) {
26 struct vivid_cec_work *cw =
27 list_first_entry(&dev->cec_work_list,
28 struct vivid_cec_work, list);
30 spin_unlock(&dev->cec_slock);
31 cancel_delayed_work_sync(&cw->work);
32 spin_lock(&dev->cec_slock);
34 cec_transmit_attempt_done(cw->adap, CEC_TX_STATUS_LOW_DRIVE);
37 spin_unlock(&dev->cec_slock);
40 static bool vivid_cec_find_dest_adap(struct vivid_dev *dev,
41 struct cec_adapter *adap, u8 dest)
48 if (adap != dev->cec_rx_adap && dev->cec_rx_adap &&
49 dev->cec_rx_adap->is_configured &&
50 cec_has_log_addr(dev->cec_rx_adap, dest))
53 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++) {
54 if (adap == dev->cec_tx_adap[i])
56 if (!dev->cec_tx_adap[i]->is_configured)
58 if (cec_has_log_addr(dev->cec_tx_adap[i], dest))
64 static void vivid_cec_pin_adap_events(struct cec_adapter *adap, ktime_t ts,
65 const struct cec_msg *msg, bool nacked)
67 unsigned int len = nacked ? 1 : msg->len;
75 * Suffix ULL on constant 10 makes the expression
76 * CEC_TIM_START_BIT_TOTAL + 10ULL * len * CEC_TIM_DATA_BIT_TOTAL
77 * to be evaluated using 64-bit unsigned arithmetic (u64), which
78 * is what ktime_sub_us expects as second argument.
80 ts = ktime_sub_us(ts, CEC_TIM_START_BIT_TOTAL +
81 10ULL * len * CEC_TIM_DATA_BIT_TOTAL);
82 cec_queue_pin_cec_event(adap, false, false, ts);
83 ts = ktime_add_us(ts, CEC_TIM_START_BIT_LOW);
84 cec_queue_pin_cec_event(adap, true, false, ts);
85 ts = ktime_add_us(ts, CEC_TIM_START_BIT_HIGH);
87 for (i = 0; i < 10 * len; i++) {
90 bit = msg->msg[i / 10] & (0x80 >> (i % 10));
93 bit = i / 10 == msg->len - 1;
96 bit = cec_msg_is_broadcast(msg) ^ nacked;
99 cec_queue_pin_cec_event(adap, false, false, ts);
101 ts = ktime_add_us(ts, CEC_TIM_DATA_BIT_1_LOW);
103 ts = ktime_add_us(ts, CEC_TIM_DATA_BIT_0_LOW);
104 cec_queue_pin_cec_event(adap, true, false, ts);
106 ts = ktime_add_us(ts, CEC_TIM_DATA_BIT_1_HIGH);
108 ts = ktime_add_us(ts, CEC_TIM_DATA_BIT_0_HIGH);
112 static void vivid_cec_pin_events(struct vivid_dev *dev,
113 const struct cec_msg *msg, bool nacked)
115 ktime_t ts = ktime_get();
118 vivid_cec_pin_adap_events(dev->cec_rx_adap, ts, msg, nacked);
119 for (i = 0; i < MAX_OUTPUTS; i++)
120 vivid_cec_pin_adap_events(dev->cec_tx_adap[i], ts, msg, nacked);
123 static void vivid_cec_xfer_done_worker(struct work_struct *work)
125 struct vivid_cec_work *cw =
126 container_of(work, struct vivid_cec_work, work.work);
127 struct vivid_dev *dev = cw->dev;
128 struct cec_adapter *adap = cw->adap;
129 u8 dest = cec_msg_destination(&cw->msg);
133 valid_dest = cec_msg_is_broadcast(&cw->msg);
135 valid_dest = vivid_cec_find_dest_adap(dev, adap, dest);
137 cw->tx_status = valid_dest ? CEC_TX_STATUS_OK : CEC_TX_STATUS_NACK;
138 spin_lock(&dev->cec_slock);
139 dev->cec_xfer_time_jiffies = 0;
140 dev->cec_xfer_start_jiffies = 0;
142 spin_unlock(&dev->cec_slock);
143 vivid_cec_pin_events(dev, &cw->msg, !valid_dest);
144 cec_transmit_attempt_done(cw->adap, cw->tx_status);
146 /* Broadcast message */
147 if (adap != dev->cec_rx_adap)
148 cec_received_msg(dev->cec_rx_adap, &cw->msg);
149 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
150 if (adap != dev->cec_tx_adap[i])
151 cec_received_msg(dev->cec_tx_adap[i], &cw->msg);
155 static void vivid_cec_xfer_try_worker(struct work_struct *work)
157 struct vivid_cec_work *cw =
158 container_of(work, struct vivid_cec_work, work.work);
159 struct vivid_dev *dev = cw->dev;
161 spin_lock(&dev->cec_slock);
162 if (dev->cec_xfer_time_jiffies) {
164 spin_unlock(&dev->cec_slock);
165 cec_transmit_attempt_done(cw->adap, CEC_TX_STATUS_ARB_LOST);
168 INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_done_worker);
169 dev->cec_xfer_start_jiffies = jiffies;
170 dev->cec_xfer_time_jiffies = usecs_to_jiffies(cw->usecs);
171 spin_unlock(&dev->cec_slock);
172 schedule_delayed_work(&cw->work, dev->cec_xfer_time_jiffies);
176 static int vivid_cec_adap_enable(struct cec_adapter *adap, bool enable)
178 adap->cec_pin_is_high = true;
182 static int vivid_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
188 * One data bit takes 2400 us, each byte needs 10 bits so that's 24000 us
191 #define USECS_PER_BYTE 24000
193 static int vivid_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
194 u32 signal_free_time, struct cec_msg *msg)
196 struct vivid_dev *dev = cec_get_drvdata(adap);
197 struct vivid_cec_work *cw = kzalloc(sizeof(*cw), GFP_KERNEL);
198 long delta_jiffies = 0;
204 cw->usecs = CEC_FREE_TIME_TO_USEC(signal_free_time) +
205 msg->len * USECS_PER_BYTE;
208 spin_lock(&dev->cec_slock);
209 list_add(&cw->list, &dev->cec_work_list);
210 if (dev->cec_xfer_time_jiffies == 0) {
211 INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_done_worker);
212 dev->cec_xfer_start_jiffies = jiffies;
213 dev->cec_xfer_time_jiffies = usecs_to_jiffies(cw->usecs);
214 delta_jiffies = dev->cec_xfer_time_jiffies;
216 INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_try_worker);
217 delta_jiffies = dev->cec_xfer_start_jiffies +
218 dev->cec_xfer_time_jiffies - jiffies;
220 spin_unlock(&dev->cec_slock);
221 schedule_delayed_work(&cw->work, delta_jiffies < 0 ? 0 : delta_jiffies);
225 static int vivid_received(struct cec_adapter *adap, struct cec_msg *msg)
227 struct vivid_dev *dev = cec_get_drvdata(adap);
228 struct cec_msg reply;
229 u8 dest = cec_msg_destination(msg);
233 if (cec_msg_is_broadcast(msg))
234 dest = adap->log_addrs.log_addr[0];
235 cec_msg_init(&reply, dest, cec_msg_initiator(msg));
237 switch (cec_msg_opcode(msg)) {
238 case CEC_MSG_SET_OSD_STRING:
239 if (!cec_is_sink(adap))
241 cec_ops_set_osd_string(msg, &disp_ctl, osd);
243 case CEC_OP_DISP_CTL_DEFAULT:
244 strcpy(dev->osd, osd);
245 dev->osd_jiffies = jiffies;
247 case CEC_OP_DISP_CTL_UNTIL_CLEARED:
248 strcpy(dev->osd, osd);
249 dev->osd_jiffies = 0;
251 case CEC_OP_DISP_CTL_CLEAR:
253 dev->osd_jiffies = 0;
256 cec_msg_feature_abort(&reply, cec_msg_opcode(msg),
257 CEC_OP_ABORT_INVALID_OP);
258 cec_transmit_msg(adap, &reply, false);
268 static const struct cec_adap_ops vivid_cec_adap_ops = {
269 .adap_enable = vivid_cec_adap_enable,
270 .adap_log_addr = vivid_cec_adap_log_addr,
271 .adap_transmit = vivid_cec_adap_transmit,
272 .received = vivid_received,
275 struct cec_adapter *vivid_cec_alloc_adap(struct vivid_dev *dev,
279 char name[sizeof(dev->vid_out_dev.name) + 2];
280 u32 caps = CEC_CAP_DEFAULTS | CEC_CAP_MONITOR_ALL | CEC_CAP_MONITOR_PIN;
282 snprintf(name, sizeof(name), "%s%d",
283 is_source ? dev->vid_out_dev.name : dev->vid_cap_dev.name,
285 return cec_allocate_adapter(&vivid_cec_adap_ops, dev,