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