1
2
3
4
5
6
7
8#ifndef _MEDIA_CEC_H
9#define _MEDIA_CEC_H
10
11#include <linux/poll.h>
12#include <linux/fs.h>
13#include <linux/debugfs.h>
14#include <linux/device.h>
15#include <linux/cdev.h>
16#include <linux/kthread.h>
17#include <linux/timer.h>
18#include <linux/cec-funcs.h>
19#include <media/rc-core.h>
20#include <media/cec-notifier.h>
21
22#define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
23 CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40struct cec_devnode {
41
42 struct device dev;
43 struct cdev cdev;
44
45
46 int minor;
47 bool registered;
48 bool unregistered;
49 struct list_head fhs;
50 struct mutex lock;
51};
52
53struct cec_adapter;
54struct cec_data;
55struct cec_pin;
56
57struct cec_data {
58 struct list_head list;
59 struct list_head xfer_list;
60 struct cec_adapter *adap;
61 struct cec_msg msg;
62 struct cec_fh *fh;
63 struct delayed_work work;
64 struct completion c;
65 u8 attempts;
66 bool blocking;
67 bool completed;
68};
69
70struct cec_msg_entry {
71 struct list_head list;
72 struct cec_msg msg;
73};
74
75struct cec_event_entry {
76 struct list_head list;
77 struct cec_event ev;
78};
79
80#define CEC_NUM_CORE_EVENTS 2
81#define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
82
83struct cec_fh {
84 struct list_head list;
85 struct list_head xfer_list;
86 struct cec_adapter *adap;
87 u8 mode_initiator;
88 u8 mode_follower;
89
90
91 wait_queue_head_t wait;
92 struct mutex lock;
93 struct list_head events[CEC_NUM_EVENTS];
94 u16 queued_events[CEC_NUM_EVENTS];
95 unsigned int total_queued_events;
96 struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
97 struct list_head msgs;
98 unsigned int queued_msgs;
99};
100
101#define CEC_SIGNAL_FREE_TIME_RETRY 3
102#define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
103#define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
104
105
106#define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
107
108struct cec_adap_ops {
109
110 int (*adap_enable)(struct cec_adapter *adap, bool enable);
111 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
112 int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
113 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
114 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
115 u32 signal_free_time, struct cec_msg *msg);
116 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
117 void (*adap_free)(struct cec_adapter *adap);
118
119
120 int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
121 bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
122
123
124 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
125};
126
127
128
129
130
131
132
133
134
135
136
137#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
138
139
140
141
142
143
144
145#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
146
147struct cec_adapter {
148 struct module *owner;
149 char name[32];
150 struct cec_devnode devnode;
151 struct mutex lock;
152 struct rc_dev *rc;
153
154 struct list_head transmit_queue;
155 unsigned int transmit_queue_sz;
156 struct list_head wait_queue;
157 struct cec_data *transmitting;
158 bool transmit_in_progress;
159
160 struct task_struct *kthread_config;
161 struct completion config_completion;
162
163 struct task_struct *kthread;
164 wait_queue_head_t kthread_waitq;
165 wait_queue_head_t waitq;
166
167 const struct cec_adap_ops *ops;
168 void *priv;
169 u32 capabilities;
170 u8 available_log_addrs;
171
172 u16 phys_addr;
173 bool needs_hpd;
174 bool is_configuring;
175 bool is_configured;
176 bool cec_pin_is_high;
177 u8 last_initiator;
178 u32 monitor_all_cnt;
179 u32 monitor_pin_cnt;
180 u32 follower_cnt;
181 struct cec_fh *cec_follower;
182 struct cec_fh *cec_initiator;
183 bool passthrough;
184 struct cec_log_addrs log_addrs;
185
186 u32 tx_timeouts;
187
188#ifdef CONFIG_CEC_NOTIFIER
189 struct cec_notifier *notifier;
190#endif
191#ifdef CONFIG_CEC_PIN
192 struct cec_pin *pin;
193#endif
194
195 struct dentry *cec_dir;
196 struct dentry *status_file;
197 struct dentry *error_inj_file;
198
199 u16 phys_addrs[15];
200 u32 sequence;
201
202 char input_phys[32];
203};
204
205static inline void *cec_get_drvdata(const struct cec_adapter *adap)
206{
207 return adap->priv;
208}
209
210static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
211{
212 return adap->log_addrs.log_addr_mask & (1 << log_addr);
213}
214
215static inline bool cec_is_sink(const struct cec_adapter *adap)
216{
217 return adap->phys_addr == 0;
218}
219
220
221
222
223
224
225
226
227static inline bool cec_is_registered(const struct cec_adapter *adap)
228{
229 return adap && adap->devnode.registered;
230}
231
232#define cec_phys_addr_exp(pa) \
233 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
234
235struct edid;
236
237#if IS_REACHABLE(CONFIG_CEC_CORE)
238struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
239 void *priv, const char *name, u32 caps, u8 available_las);
240int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
241void cec_unregister_adapter(struct cec_adapter *adap);
242void cec_delete_adapter(struct cec_adapter *adap);
243
244int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
245 bool block);
246void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
247 bool block);
248void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
249 const struct edid *edid);
250int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
251 bool block);
252
253
254void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
255 u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
256 u8 error_cnt, ktime_t ts);
257
258static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
259 u8 arb_lost_cnt, u8 nack_cnt,
260 u8 low_drive_cnt, u8 error_cnt)
261{
262 cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
263 low_drive_cnt, error_cnt, ktime_get());
264}
265
266
267
268
269
270void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
271 u8 status, ktime_t ts);
272
273static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
274 u8 status)
275{
276 cec_transmit_attempt_done_ts(adap, status, ktime_get());
277}
278
279void cec_received_msg_ts(struct cec_adapter *adap,
280 struct cec_msg *msg, ktime_t ts);
281
282static inline void cec_received_msg(struct cec_adapter *adap,
283 struct cec_msg *msg)
284{
285 cec_received_msg_ts(adap, msg, ktime_get());
286}
287
288
289
290
291
292
293
294
295
296
297void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
298 bool dropped_events, ktime_t ts);
299
300
301
302
303
304
305
306
307
308void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
309
310
311
312
313
314
315
316
317
318void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
319
320
321
322
323
324
325
326
327
328
329
330
331u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
332 unsigned int *offset);
333
334#else
335
336static inline int cec_register_adapter(struct cec_adapter *adap,
337 struct device *parent)
338{
339 return 0;
340}
341
342static inline void cec_unregister_adapter(struct cec_adapter *adap)
343{
344}
345
346static inline void cec_delete_adapter(struct cec_adapter *adap)
347{
348}
349
350static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
351 bool block)
352{
353}
354
355static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
356 const struct edid *edid)
357{
358}
359
360static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
361 unsigned int *offset)
362{
363 if (offset)
364 *offset = 0;
365 return CEC_PHYS_ADDR_INVALID;
366}
367
368#endif
369
370
371
372
373
374
375
376
377
378static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
379{
380 cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
381}
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
400 unsigned int size)
401{
402 unsigned int blocks = size / 128;
403 unsigned int block;
404 u8 d;
405
406
407 if (blocks < 2 || size % 128)
408 return 0;
409
410
411
412
413
414
415
416
417 if (edid[0x7e] + 1 < blocks)
418 blocks = edid[0x7e] + 1;
419
420 for (block = 1; block < blocks; block++) {
421 unsigned int offset = block * 128;
422
423
424 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
425 continue;
426
427
428 d = edid[offset + 2] & 0x7f;
429
430 if (d <= 4)
431 continue;
432 if (d > 4) {
433 unsigned int i = offset + 4;
434 unsigned int end = offset + d;
435
436
437 do {
438 u8 tag = edid[i] >> 5;
439 u8 len = edid[i] & 0x1f;
440
441 if (tag == 3 && len >= 5 && i + len <= end &&
442 edid[i + 1] == 0x03 &&
443 edid[i + 2] == 0x0c &&
444 edid[i + 3] == 0x00)
445 return i + 4;
446 i += len + 1;
447 } while (i < end);
448 }
449 }
450 return 0;
451}
452
453#endif
454