2 * cec - HDMI Consumer Electronics Control support header
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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
23 #include <linux/poll.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>
32 #include <media/cec-notifier.h>
35 * struct cec_devnode - cec device node
37 * @cdev: cec character device
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)
44 * This structure represents a cec-related device node.
46 * The @parent is a physical device. It must be set by core or device drivers
47 * before registering the node.
66 struct list_head list
;
67 struct list_head xfer_list
;
68 struct cec_adapter
*adap
;
71 struct delayed_work work
;
79 struct cec_msg_entry
{
80 struct list_head list
;
84 #define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
87 struct list_head list
;
88 struct list_head xfer_list
;
89 struct cec_adapter
*adap
;
94 wait_queue_head_t wait
;
95 unsigned int pending_events
;
96 struct cec_event events
[CEC_NUM_EVENTS
];
98 struct list_head msgs
; /* queued messages */
99 unsigned int queued_msgs
;
102 #define CEC_SIGNAL_FREE_TIME_RETRY 3
103 #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
104 #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
106 /* The nominal data bit period is 2.4 ms */
107 #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
109 struct cec_adap_ops
{
110 /* Low-level callbacks */
111 int (*adap_enable
)(struct cec_adapter
*adap
, bool enable
);
112 int (*adap_monitor_all_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
);
118 /* High-level CEC message callback */
119 int (*received
)(struct cec_adapter
*adap
, struct cec_msg
*msg
);
123 * The minimum message length you can receive (excepting poll messages) is 2.
124 * With a transfer rate of at most 36 bytes per second this makes 18 messages
125 * per second worst case.
127 * We queue at most 3 seconds worth of received messages. The CEC specification
128 * requires that messages are replied to within a second, so 3 seconds should
129 * give more than enough margin. Since most messages are actually more than 2
130 * bytes, this is in practice a lot more than 3 seconds.
132 #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
135 * The transmit queue is limited to 1 second worth of messages (worst case).
136 * Messages can be transmitted by userspace and kernel space. But for both it
137 * makes no sense to have a lot of messages queued up. One second seems
140 #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
143 struct module
*owner
;
145 struct cec_devnode devnode
;
149 struct list_head transmit_queue
;
150 unsigned int transmit_queue_sz
;
151 struct list_head wait_queue
;
152 struct cec_data
*transmitting
;
154 struct task_struct
*kthread_config
;
155 struct completion config_completion
;
157 struct task_struct
*kthread
;
158 wait_queue_head_t kthread_waitq
;
159 wait_queue_head_t waitq
;
161 const struct cec_adap_ops
*ops
;
164 u8 available_log_addrs
;
171 struct cec_fh
*cec_follower
;
172 struct cec_fh
*cec_initiator
;
174 struct cec_log_addrs log_addrs
;
176 #ifdef CONFIG_MEDIA_CEC_NOTIFIER
177 struct cec_notifier
*notifier
;
180 struct dentry
*cec_dir
;
181 struct dentry
*status_file
;
191 static inline void *cec_get_drvdata(const struct cec_adapter
*adap
)
196 static inline bool cec_has_log_addr(const struct cec_adapter
*adap
, u8 log_addr
)
198 return adap
->log_addrs
.log_addr_mask
& (1 << log_addr
);
201 static inline bool cec_is_sink(const struct cec_adapter
*adap
)
203 return adap
->phys_addr
== 0;
206 #define cec_phys_addr_exp(pa) \
207 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
209 #if IS_ENABLED(CONFIG_CEC_CORE)
210 struct cec_adapter
*cec_allocate_adapter(const struct cec_adap_ops
*ops
,
211 void *priv
, const char *name
, u32 caps
, u8 available_las
);
212 int cec_register_adapter(struct cec_adapter
*adap
, struct device
*parent
);
213 void cec_unregister_adapter(struct cec_adapter
*adap
);
214 void cec_delete_adapter(struct cec_adapter
*adap
);
216 int cec_s_log_addrs(struct cec_adapter
*adap
, struct cec_log_addrs
*log_addrs
,
218 void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
220 int cec_transmit_msg(struct cec_adapter
*adap
, struct cec_msg
*msg
,
223 /* Called by the adapter */
224 void cec_transmit_done(struct cec_adapter
*adap
, u8 status
, u8 arb_lost_cnt
,
225 u8 nack_cnt
, u8 low_drive_cnt
, u8 error_cnt
);
226 void cec_received_msg(struct cec_adapter
*adap
, struct cec_msg
*msg
);
229 * cec_get_edid_phys_addr() - find and return the physical address
231 * @edid: pointer to the EDID data
232 * @size: size in bytes of the EDID data
233 * @offset: If not %NULL then the location of the physical address
234 * bytes in the EDID will be returned here. This is set to 0
235 * if there is no physical address found.
237 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
239 u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
240 unsigned int *offset
);
243 * cec_set_edid_phys_addr() - find and set the physical address
245 * @edid: pointer to the EDID data
246 * @size: size in bytes of the EDID data
247 * @phys_addr: the new physical address
249 * This function finds the location of the physical address in the EDID
250 * and fills in the given physical address and updates the checksum
251 * at the end of the EDID block. It does nothing if the EDID doesn't
252 * contain a physical address.
254 void cec_set_edid_phys_addr(u8
*edid
, unsigned int size
, u16 phys_addr
);
257 * cec_phys_addr_for_input() - calculate the PA for an input
259 * @phys_addr: the physical address of the parent
260 * @input: the number of the input port, must be between 1 and 15
262 * This function calculates a new physical address based on the input
263 * port number. For example:
265 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
267 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
269 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
271 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
273 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
275 u16
cec_phys_addr_for_input(u16 phys_addr
, u8 input
);
278 * cec_phys_addr_validate() - validate a physical address from an EDID
280 * @phys_addr: the physical address to validate
281 * @parent: if not %NULL, then this is filled with the parents PA.
282 * @port: if not %NULL, then this is filled with the input port.
284 * This validates a physical address as read from an EDID. If the
285 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
286 * then it will return -EINVAL.
288 * The parent PA is passed into %parent and the input port is passed into
289 * %port. For example:
291 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
293 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
295 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
297 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
299 * Return: 0 if the PA is valid, -EINVAL if not.
301 int cec_phys_addr_validate(u16 phys_addr
, u16
*parent
, u16
*port
);
303 #ifdef CONFIG_MEDIA_CEC_NOTIFIER
304 void cec_register_cec_notifier(struct cec_adapter
*adap
,
305 struct cec_notifier
*notifier
);
310 static inline int cec_register_adapter(struct cec_adapter
*adap
,
311 struct device
*parent
)
316 static inline void cec_unregister_adapter(struct cec_adapter
*adap
)
320 static inline void cec_delete_adapter(struct cec_adapter
*adap
)
324 static inline void cec_s_phys_addr(struct cec_adapter
*adap
, u16 phys_addr
,
329 static inline u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
330 unsigned int *offset
)
334 return CEC_PHYS_ADDR_INVALID
;
337 static inline void cec_set_edid_phys_addr(u8
*edid
, unsigned int size
,
342 static inline u16
cec_phys_addr_for_input(u16 phys_addr
, u8 input
)
344 return CEC_PHYS_ADDR_INVALID
;
347 static inline int cec_phys_addr_validate(u16 phys_addr
, u16
*parent
, u16
*port
)
354 #endif /* _MEDIA_CEC_H */