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