Commit | Line | Data |
---|---|---|
a56960e8 HV |
1 | /* |
2 | * cec - HDMI Consumer Electronics Control support header | |
3 | * | |
4 | * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved. | |
5 | * | |
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. | |
9 | * | |
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 | |
17 | * SOFTWARE. | |
18 | */ | |
19 | ||
20 | #ifndef _MEDIA_CEC_H | |
21 | #define _MEDIA_CEC_H | |
22 | ||
23 | #include <linux/poll.h> | |
24 | #include <linux/fs.h> | |
25 | #include <linux/debugfs.h> | |
26 | #include <linux/device.h> | |
27 | #include <linux/cdev.h> | |
28 | #include <linux/kthread.h> | |
29 | #include <linux/timer.h> | |
30 | #include <linux/cec-funcs.h> | |
31 | #include <media/rc-core.h> | |
e3a93adc | 32 | #include <media/cec-notifier.h> |
a56960e8 HV |
33 | |
34 | /** | |
35 | * struct cec_devnode - cec device node | |
36 | * @dev: cec device | |
37 | * @cdev: cec character device | |
a56960e8 HV |
38 | * @minor: device node minor number |
39 | * @registered: the device was correctly registered | |
40 | * @unregistered: the device was unregistered | |
41 | * @fhs_lock: lock to control access to the filehandle list | |
42 | * @fhs: the list of open filehandles (cec_fh) | |
43 | * | |
44 | * This structure represents a cec-related device node. | |
45 | * | |
46 | * The @parent is a physical device. It must be set by core or device drivers | |
47 | * before registering the node. | |
48 | */ | |
49 | struct cec_devnode { | |
50 | /* sysfs */ | |
51 | struct device dev; | |
52 | struct cdev cdev; | |
a56960e8 HV |
53 | |
54 | /* device info */ | |
55 | int minor; | |
56 | bool registered; | |
57 | bool unregistered; | |
a56960e8 | 58 | struct list_head fhs; |
62148f09 | 59 | struct mutex lock; |
a56960e8 HV |
60 | }; |
61 | ||
62 | struct cec_adapter; | |
63 | struct cec_data; | |
ea5c8ef2 | 64 | struct cec_pin; |
a56960e8 HV |
65 | |
66 | struct cec_data { | |
67 | struct list_head list; | |
68 | struct list_head xfer_list; | |
69 | struct cec_adapter *adap; | |
70 | struct cec_msg msg; | |
71 | struct cec_fh *fh; | |
72 | struct delayed_work work; | |
73 | struct completion c; | |
74 | u8 attempts; | |
75 | bool new_initiator; | |
76 | bool blocking; | |
77 | bool completed; | |
78 | }; | |
79 | ||
80 | struct cec_msg_entry { | |
81 | struct list_head list; | |
82 | struct cec_msg msg; | |
83 | }; | |
84 | ||
6b2bbb08 HV |
85 | struct cec_event_entry { |
86 | struct list_head list; | |
87 | struct cec_event ev; | |
88 | }; | |
89 | ||
90 | #define CEC_NUM_CORE_EVENTS 2 | |
91 | #define CEC_NUM_EVENTS CEC_EVENT_PIN_HIGH | |
a56960e8 HV |
92 | |
93 | struct cec_fh { | |
94 | struct list_head list; | |
95 | struct list_head xfer_list; | |
96 | struct cec_adapter *adap; | |
97 | u8 mode_initiator; | |
98 | u8 mode_follower; | |
99 | ||
100 | /* Events */ | |
101 | wait_queue_head_t wait; | |
a56960e8 | 102 | struct mutex lock; |
6b2bbb08 HV |
103 | struct list_head events[CEC_NUM_EVENTS]; /* queued events */ |
104 | u8 queued_events[CEC_NUM_EVENTS]; | |
105 | unsigned int total_queued_events; | |
106 | struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS]; | |
a56960e8 HV |
107 | struct list_head msgs; /* queued messages */ |
108 | unsigned int queued_msgs; | |
109 | }; | |
110 | ||
111 | #define CEC_SIGNAL_FREE_TIME_RETRY 3 | |
112 | #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5 | |
113 | #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7 | |
114 | ||
115 | /* The nominal data bit period is 2.4 ms */ | |
116 | #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400) | |
117 | ||
118 | struct cec_adap_ops { | |
119 | /* Low-level callbacks */ | |
120 | int (*adap_enable)(struct cec_adapter *adap, bool enable); | |
121 | int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable); | |
122 | int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr); | |
123 | int (*adap_transmit)(struct cec_adapter *adap, u8 attempts, | |
124 | u32 signal_free_time, struct cec_msg *msg); | |
125 | void (*adap_status)(struct cec_adapter *adap, struct seq_file *file); | |
e6259b5f | 126 | void (*adap_free)(struct cec_adapter *adap); |
a56960e8 HV |
127 | |
128 | /* High-level CEC message callback */ | |
129 | int (*received)(struct cec_adapter *adap, struct cec_msg *msg); | |
130 | }; | |
131 | ||
132 | /* | |
133 | * The minimum message length you can receive (excepting poll messages) is 2. | |
134 | * With a transfer rate of at most 36 bytes per second this makes 18 messages | |
135 | * per second worst case. | |
136 | * | |
11065f85 HV |
137 | * We queue at most 3 seconds worth of received messages. The CEC specification |
138 | * requires that messages are replied to within a second, so 3 seconds should | |
139 | * give more than enough margin. Since most messages are actually more than 2 | |
140 | * bytes, this is in practice a lot more than 3 seconds. | |
a56960e8 | 141 | */ |
11065f85 HV |
142 | #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3) |
143 | ||
144 | /* | |
145 | * The transmit queue is limited to 1 second worth of messages (worst case). | |
146 | * Messages can be transmitted by userspace and kernel space. But for both it | |
147 | * makes no sense to have a lot of messages queued up. One second seems | |
148 | * reasonable. | |
149 | */ | |
150 | #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1) | |
a56960e8 HV |
151 | |
152 | struct cec_adapter { | |
153 | struct module *owner; | |
154 | char name[32]; | |
155 | struct cec_devnode devnode; | |
156 | struct mutex lock; | |
157 | struct rc_dev *rc; | |
158 | ||
159 | struct list_head transmit_queue; | |
11065f85 | 160 | unsigned int transmit_queue_sz; |
a56960e8 HV |
161 | struct list_head wait_queue; |
162 | struct cec_data *transmitting; | |
163 | ||
164 | struct task_struct *kthread_config; | |
165 | struct completion config_completion; | |
166 | ||
167 | struct task_struct *kthread; | |
168 | wait_queue_head_t kthread_waitq; | |
169 | wait_queue_head_t waitq; | |
170 | ||
171 | const struct cec_adap_ops *ops; | |
172 | void *priv; | |
173 | u32 capabilities; | |
174 | u8 available_log_addrs; | |
175 | ||
176 | u16 phys_addr; | |
f902c1e9 | 177 | bool needs_hpd; |
a56960e8 HV |
178 | bool is_configuring; |
179 | bool is_configured; | |
180 | u32 monitor_all_cnt; | |
b8d62f50 | 181 | u32 monitor_pin_cnt; |
a56960e8 HV |
182 | u32 follower_cnt; |
183 | struct cec_fh *cec_follower; | |
184 | struct cec_fh *cec_initiator; | |
185 | bool passthrough; | |
186 | struct cec_log_addrs log_addrs; | |
187 | ||
bb789e03 HV |
188 | u32 tx_timeouts; |
189 | ||
e94c3281 | 190 | #ifdef CONFIG_CEC_NOTIFIER |
e3a93adc HV |
191 | struct cec_notifier *notifier; |
192 | #endif | |
ea5c8ef2 HV |
193 | #ifdef CONFIG_CEC_PIN |
194 | struct cec_pin *pin; | |
195 | #endif | |
e3a93adc | 196 | |
a56960e8 HV |
197 | struct dentry *cec_dir; |
198 | struct dentry *status_file; | |
199 | ||
200 | u16 phys_addrs[15]; | |
201 | u32 sequence; | |
202 | ||
203 | char input_name[32]; | |
204 | char input_phys[32]; | |
205 | char input_drv[32]; | |
206 | }; | |
207 | ||
0b0b97dc JA |
208 | static inline void *cec_get_drvdata(const struct cec_adapter *adap) |
209 | { | |
210 | return adap->priv; | |
211 | } | |
212 | ||
a56960e8 HV |
213 | static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr) |
214 | { | |
215 | return adap->log_addrs.log_addr_mask & (1 << log_addr); | |
216 | } | |
217 | ||
218 | static inline bool cec_is_sink(const struct cec_adapter *adap) | |
219 | { | |
220 | return adap->phys_addr == 0; | |
221 | } | |
222 | ||
ee7e9871 HV |
223 | #define cec_phys_addr_exp(pa) \ |
224 | ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf | |
225 | ||
23111ec3 HV |
226 | struct edid; |
227 | ||
f9f314f3 | 228 | #if IS_REACHABLE(CONFIG_CEC_CORE) |
a56960e8 | 229 | struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops, |
f51e8080 HV |
230 | void *priv, const char *name, u32 caps, u8 available_las); |
231 | int cec_register_adapter(struct cec_adapter *adap, struct device *parent); | |
a56960e8 HV |
232 | void cec_unregister_adapter(struct cec_adapter *adap); |
233 | void cec_delete_adapter(struct cec_adapter *adap); | |
234 | ||
235 | int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs, | |
236 | bool block); | |
237 | void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, | |
238 | bool block); | |
23111ec3 HV |
239 | void cec_s_phys_addr_from_edid(struct cec_adapter *adap, |
240 | const struct edid *edid); | |
a56960e8 HV |
241 | int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg, |
242 | bool block); | |
243 | ||
244 | /* Called by the adapter */ | |
0861ad14 HV |
245 | void cec_transmit_done_ts(struct cec_adapter *adap, u8 status, |
246 | u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt, | |
247 | u8 error_cnt, ktime_t ts); | |
248 | ||
249 | static inline void cec_transmit_done(struct cec_adapter *adap, u8 status, | |
250 | u8 arb_lost_cnt, u8 nack_cnt, | |
251 | u8 low_drive_cnt, u8 error_cnt) | |
252 | { | |
253 | cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt, | |
254 | low_drive_cnt, error_cnt, ktime_get()); | |
255 | } | |
c94cdc1e HV |
256 | /* |
257 | * Simplified version of cec_transmit_done for hardware that doesn't retry | |
258 | * failed transmits. So this is always just one attempt in which case | |
259 | * the status is sufficient. | |
260 | */ | |
0861ad14 HV |
261 | void cec_transmit_attempt_done_ts(struct cec_adapter *adap, |
262 | u8 status, ktime_t ts); | |
263 | ||
264 | static inline void cec_transmit_attempt_done(struct cec_adapter *adap, | |
265 | u8 status) | |
266 | { | |
267 | cec_transmit_attempt_done_ts(adap, status, ktime_get()); | |
268 | } | |
269 | ||
270 | void cec_received_msg_ts(struct cec_adapter *adap, | |
271 | struct cec_msg *msg, ktime_t ts); | |
272 | ||
273 | static inline void cec_received_msg(struct cec_adapter *adap, | |
274 | struct cec_msg *msg) | |
275 | { | |
276 | cec_received_msg_ts(adap, msg, ktime_get()); | |
277 | } | |
a56960e8 | 278 | |
b8d62f50 HV |
279 | /** |
280 | * cec_queue_pin_event() - queue a pin event with a given timestamp. | |
281 | * | |
282 | * @adap: pointer to the cec adapter | |
283 | * @is_high: when true the pin is high, otherwise it is low | |
284 | * @ts: the timestamp for this event | |
285 | * | |
286 | */ | |
287 | void cec_queue_pin_event(struct cec_adapter *adap, bool is_high, ktime_t ts); | |
288 | ||
ee7e9871 HV |
289 | /** |
290 | * cec_get_edid_phys_addr() - find and return the physical address | |
291 | * | |
292 | * @edid: pointer to the EDID data | |
293 | * @size: size in bytes of the EDID data | |
294 | * @offset: If not %NULL then the location of the physical address | |
295 | * bytes in the EDID will be returned here. This is set to 0 | |
296 | * if there is no physical address found. | |
297 | * | |
298 | * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none. | |
299 | */ | |
300 | u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size, | |
301 | unsigned int *offset); | |
302 | ||
303 | /** | |
304 | * cec_set_edid_phys_addr() - find and set the physical address | |
305 | * | |
306 | * @edid: pointer to the EDID data | |
307 | * @size: size in bytes of the EDID data | |
308 | * @phys_addr: the new physical address | |
309 | * | |
310 | * This function finds the location of the physical address in the EDID | |
311 | * and fills in the given physical address and updates the checksum | |
312 | * at the end of the EDID block. It does nothing if the EDID doesn't | |
313 | * contain a physical address. | |
314 | */ | |
315 | void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr); | |
316 | ||
317 | /** | |
318 | * cec_phys_addr_for_input() - calculate the PA for an input | |
319 | * | |
320 | * @phys_addr: the physical address of the parent | |
321 | * @input: the number of the input port, must be between 1 and 15 | |
322 | * | |
323 | * This function calculates a new physical address based on the input | |
324 | * port number. For example: | |
325 | * | |
326 | * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0 | |
327 | * | |
328 | * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0 | |
329 | * | |
330 | * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5 | |
331 | * | |
332 | * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth. | |
333 | * | |
334 | * Return: the new physical address or CEC_PHYS_ADDR_INVALID. | |
335 | */ | |
336 | u16 cec_phys_addr_for_input(u16 phys_addr, u8 input); | |
337 | ||
338 | /** | |
339 | * cec_phys_addr_validate() - validate a physical address from an EDID | |
340 | * | |
341 | * @phys_addr: the physical address to validate | |
342 | * @parent: if not %NULL, then this is filled with the parents PA. | |
343 | * @port: if not %NULL, then this is filled with the input port. | |
344 | * | |
345 | * This validates a physical address as read from an EDID. If the | |
346 | * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end), | |
347 | * then it will return -EINVAL. | |
348 | * | |
349 | * The parent PA is passed into %parent and the input port is passed into | |
350 | * %port. For example: | |
351 | * | |
352 | * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0. | |
353 | * | |
354 | * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1. | |
355 | * | |
356 | * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2. | |
357 | * | |
358 | * PA = f.f.f.f: has parent f.f.f.f and input port 0. | |
359 | * | |
360 | * Return: 0 if the PA is valid, -EINVAL if not. | |
361 | */ | |
362 | int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port); | |
363 | ||
a56960e8 HV |
364 | #else |
365 | ||
f51e8080 HV |
366 | static inline int cec_register_adapter(struct cec_adapter *adap, |
367 | struct device *parent) | |
a56960e8 HV |
368 | { |
369 | return 0; | |
370 | } | |
371 | ||
372 | static inline void cec_unregister_adapter(struct cec_adapter *adap) | |
373 | { | |
374 | } | |
375 | ||
376 | static inline void cec_delete_adapter(struct cec_adapter *adap) | |
377 | { | |
378 | } | |
379 | ||
380 | static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, | |
381 | bool block) | |
382 | { | |
383 | } | |
384 | ||
23111ec3 HV |
385 | static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap, |
386 | const struct edid *edid) | |
387 | { | |
388 | } | |
389 | ||
ee7e9871 HV |
390 | static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size, |
391 | unsigned int *offset) | |
392 | { | |
393 | if (offset) | |
394 | *offset = 0; | |
395 | return CEC_PHYS_ADDR_INVALID; | |
396 | } | |
397 | ||
398 | static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size, | |
399 | u16 phys_addr) | |
400 | { | |
401 | } | |
402 | ||
403 | static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input) | |
404 | { | |
405 | return CEC_PHYS_ADDR_INVALID; | |
406 | } | |
407 | ||
408 | static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port) | |
409 | { | |
410 | return 0; | |
411 | } | |
412 | ||
a56960e8 HV |
413 | #endif |
414 | ||
13532789 HV |
415 | /** |
416 | * cec_phys_addr_invalidate() - set the physical address to INVALID | |
417 | * | |
418 | * @adap: the CEC adapter | |
419 | * | |
420 | * This is a simple helper function to invalidate the physical | |
421 | * address. | |
422 | */ | |
423 | static inline void cec_phys_addr_invalidate(struct cec_adapter *adap) | |
424 | { | |
425 | cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false); | |
426 | } | |
427 | ||
a56960e8 | 428 | #endif /* _MEDIA_CEC_H */ |