Blame view

include/media/cec.h 11.9 KB
a56960e8b   Hans Verkuil   [media] cec: add ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /*
   * cec - HDMI Consumer Electronics Control support header
   *
   * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   *
   * This program is free software; you may redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; version 2 of the License.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   */
  
  #ifndef _MEDIA_CEC_H
  #define _MEDIA_CEC_H
  
  #include <linux/poll.h>
  #include <linux/fs.h>
  #include <linux/debugfs.h>
  #include <linux/device.h>
  #include <linux/cdev.h>
  #include <linux/kthread.h>
  #include <linux/timer.h>
  #include <linux/cec-funcs.h>
  #include <media/rc-core.h>
e3a93adcc   Hans Verkuil   [media] cec: inte...
32
  #include <media/cec-notifier.h>
a56960e8b   Hans Verkuil   [media] cec: add ...
33

ee0c503ea   Hans Verkuil   media: media/cec....
34
35
  #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
  			  CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
a56960e8b   Hans Verkuil   [media] cec: add ...
36
37
38
39
  /**
   * struct cec_devnode - cec device node
   * @dev:	cec device
   * @cdev:	cec character device
a56960e8b   Hans Verkuil   [media] cec: add ...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
   * @minor:	device node minor number
   * @registered:	the device was correctly registered
   * @unregistered: the device was unregistered
   * @fhs_lock:	lock to control access to the filehandle list
   * @fhs:	the list of open filehandles (cec_fh)
   *
   * This structure represents a cec-related device node.
   *
   * The @parent is a physical device. It must be set by core or device drivers
   * before registering the node.
   */
  struct cec_devnode {
  	/* sysfs */
  	struct device dev;
  	struct cdev cdev;
a56960e8b   Hans Verkuil   [media] cec: add ...
55
56
57
58
59
  
  	/* device info */
  	int minor;
  	bool registered;
  	bool unregistered;
a56960e8b   Hans Verkuil   [media] cec: add ...
60
  	struct list_head fhs;
62148f093   Hans Verkuil   [media] cec: rena...
61
  	struct mutex lock;
a56960e8b   Hans Verkuil   [media] cec: add ...
62
63
64
65
  };
  
  struct cec_adapter;
  struct cec_data;
ea5c8ef29   Hans Verkuil   media: cec-pin: a...
66
  struct cec_pin;
a56960e8b   Hans Verkuil   [media] cec: add ...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  
  struct cec_data {
  	struct list_head list;
  	struct list_head xfer_list;
  	struct cec_adapter *adap;
  	struct cec_msg msg;
  	struct cec_fh *fh;
  	struct delayed_work work;
  	struct completion c;
  	u8 attempts;
  	bool new_initiator;
  	bool blocking;
  	bool completed;
  };
  
  struct cec_msg_entry {
  	struct list_head	list;
  	struct cec_msg		msg;
  };
6b2bbb087   Hans Verkuil   media: cec: rewor...
86
87
88
89
90
91
  struct cec_event_entry {
  	struct list_head	list;
  	struct cec_event	ev;
  };
  
  #define CEC_NUM_CORE_EVENTS 2
9a6b2a874   Hans Verkuil   media: cec: renam...
92
  #define CEC_NUM_EVENTS CEC_EVENT_PIN_CEC_HIGH
a56960e8b   Hans Verkuil   [media] cec: add ...
93
94
95
96
97
98
99
100
101
102
  
  struct cec_fh {
  	struct list_head	list;
  	struct list_head	xfer_list;
  	struct cec_adapter	*adap;
  	u8			mode_initiator;
  	u8			mode_follower;
  
  	/* Events */
  	wait_queue_head_t	wait;
a56960e8b   Hans Verkuil   [media] cec: add ...
103
  	struct mutex		lock;
6b2bbb087   Hans Verkuil   media: cec: rewor...
104
105
106
107
  	struct list_head	events[CEC_NUM_EVENTS]; /* queued events */
  	u8			queued_events[CEC_NUM_EVENTS];
  	unsigned int		total_queued_events;
  	struct cec_event_entry	core_events[CEC_NUM_CORE_EVENTS];
a56960e8b   Hans Verkuil   [media] cec: add ...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  	struct list_head	msgs; /* queued messages */
  	unsigned int		queued_msgs;
  };
  
  #define CEC_SIGNAL_FREE_TIME_RETRY		3
  #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR	5
  #define CEC_SIGNAL_FREE_TIME_NEXT_XFER		7
  
  /* The nominal data bit period is 2.4 ms */
  #define CEC_FREE_TIME_TO_USEC(ft)		((ft) * 2400)
  
  struct cec_adap_ops {
  	/* Low-level callbacks */
  	int (*adap_enable)(struct cec_adapter *adap, bool enable);
  	int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  	int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  	int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  			     u32 signal_free_time, struct cec_msg *msg);
  	void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
e6259b5f7   Hans Verkuil   media: cec: add a...
127
  	void (*adap_free)(struct cec_adapter *adap);
a56960e8b   Hans Verkuil   [media] cec: add ...
128
129
130
131
132
133
134
135
136
137
  
  	/* High-level CEC message callback */
  	int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
  };
  
  /*
   * The minimum message length you can receive (excepting poll messages) is 2.
   * With a transfer rate of at most 36 bytes per second this makes 18 messages
   * per second worst case.
   *
11065f853   Hans Verkuil   [media] cec: limi...
138
139
140
141
   * We queue at most 3 seconds worth of received messages. The CEC specification
   * requires that messages are replied to within a second, so 3 seconds should
   * give more than enough margin. Since most messages are actually more than 2
   * bytes, this is in practice a lot more than 3 seconds.
a56960e8b   Hans Verkuil   [media] cec: add ...
142
   */
11065f853   Hans Verkuil   [media] cec: limi...
143
144
145
146
147
148
149
150
151
  #define CEC_MAX_MSG_RX_QUEUE_SZ		(18 * 3)
  
  /*
   * The transmit queue is limited to 1 second worth of messages (worst case).
   * Messages can be transmitted by userspace and kernel space. But for both it
   * makes no sense to have a lot of messages queued up. One second seems
   * reasonable.
   */
  #define CEC_MAX_MSG_TX_QUEUE_SZ		(18 * 1)
a56960e8b   Hans Verkuil   [media] cec: add ...
152
153
154
155
156
157
158
159
160
  
  struct cec_adapter {
  	struct module *owner;
  	char name[32];
  	struct cec_devnode devnode;
  	struct mutex lock;
  	struct rc_dev *rc;
  
  	struct list_head transmit_queue;
11065f853   Hans Verkuil   [media] cec: limi...
161
  	unsigned int transmit_queue_sz;
a56960e8b   Hans Verkuil   [media] cec: add ...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  	struct list_head wait_queue;
  	struct cec_data *transmitting;
  
  	struct task_struct *kthread_config;
  	struct completion config_completion;
  
  	struct task_struct *kthread;
  	wait_queue_head_t kthread_waitq;
  	wait_queue_head_t waitq;
  
  	const struct cec_adap_ops *ops;
  	void *priv;
  	u32 capabilities;
  	u8 available_log_addrs;
  
  	u16 phys_addr;
f902c1e95   Hans Verkuil   [media] cec: add ...
178
  	bool needs_hpd;
a56960e8b   Hans Verkuil   [media] cec: add ...
179
180
  	bool is_configuring;
  	bool is_configured;
28e11b15b   Hans Verkuil   media: cec: repla...
181
  	bool cec_pin_is_high;
a56960e8b   Hans Verkuil   [media] cec: add ...
182
  	u32 monitor_all_cnt;
b8d62f50b   Hans Verkuil   media: cec: add c...
183
  	u32 monitor_pin_cnt;
a56960e8b   Hans Verkuil   [media] cec: add ...
184
185
186
187
188
  	u32 follower_cnt;
  	struct cec_fh *cec_follower;
  	struct cec_fh *cec_initiator;
  	bool passthrough;
  	struct cec_log_addrs log_addrs;
bb789e03f   Hans Verkuil   media: cec: impro...
189
  	u32 tx_timeouts;
a9a249a2c   Hans Verkuil   media: cec: fix r...
190
191
192
193
194
  #ifdef CONFIG_MEDIA_CEC_RC
  	bool rc_repeating;
  	int rc_last_scancode;
  	u64 rc_last_keypress;
  #endif
e94c32818   Hans Verkuil   [media] cec: rena...
195
  #ifdef CONFIG_CEC_NOTIFIER
e3a93adcc   Hans Verkuil   [media] cec: inte...
196
197
  	struct cec_notifier *notifier;
  #endif
ea5c8ef29   Hans Verkuil   media: cec-pin: a...
198
199
200
  #ifdef CONFIG_CEC_PIN
  	struct cec_pin *pin;
  #endif
e3a93adcc   Hans Verkuil   [media] cec: inte...
201

a56960e8b   Hans Verkuil   [media] cec: add ...
202
203
204
205
206
  	struct dentry *cec_dir;
  	struct dentry *status_file;
  
  	u16 phys_addrs[15];
  	u32 sequence;
518f4b26b   Sean Young   media: rc-core: r...
207
  	char device_name[32];
a56960e8b   Hans Verkuil   [media] cec: add ...
208
209
210
  	char input_phys[32];
  	char input_drv[32];
  };
0b0b97dcc   Jose Abreu   [media] cec: Add ...
211
212
213
214
  static inline void *cec_get_drvdata(const struct cec_adapter *adap)
  {
  	return adap->priv;
  }
a56960e8b   Hans Verkuil   [media] cec: add ...
215
216
217
218
219
220
221
222
223
  static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  {
  	return adap->log_addrs.log_addr_mask & (1 << log_addr);
  }
  
  static inline bool cec_is_sink(const struct cec_adapter *adap)
  {
  	return adap->phys_addr == 0;
  }
ee7e98713   Hans Verkuil   [media] cec.h: me...
224
225
  #define cec_phys_addr_exp(pa) \
  	((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
23111ec30   Hans Verkuil   [media] cec: add ...
226
  struct edid;
f9f314f32   Hans Verkuil   [media] media/cec...
227
  #if IS_REACHABLE(CONFIG_CEC_CORE)
a56960e8b   Hans Verkuil   [media] cec: add ...
228
  struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
f51e80804   Hans Verkuil   [media] cec: pass...
229
230
  		void *priv, const char *name, u32 caps, u8 available_las);
  int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
a56960e8b   Hans Verkuil   [media] cec: add ...
231
232
233
234
235
236
237
  void cec_unregister_adapter(struct cec_adapter *adap);
  void cec_delete_adapter(struct cec_adapter *adap);
  
  int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  		    bool block);
  void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  		     bool block);
23111ec30   Hans Verkuil   [media] cec: add ...
238
239
  void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  			       const struct edid *edid);
a56960e8b   Hans Verkuil   [media] cec: add ...
240
241
242
243
  int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  		     bool block);
  
  /* Called by the adapter */
0861ad14c   Hans Verkuil   media: cec: add *...
244
245
246
247
248
249
250
251
252
253
254
  void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
  			  u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
  			  u8 error_cnt, ktime_t ts);
  
  static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
  				     u8 arb_lost_cnt, u8 nack_cnt,
  				     u8 low_drive_cnt, u8 error_cnt)
  {
  	cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
  			     low_drive_cnt, error_cnt, ktime_get());
  }
c94cdc1e0   Hans Verkuil   [media] cec: add ...
255
256
257
258
259
  /*
   * Simplified version of cec_transmit_done for hardware that doesn't retry
   * failed transmits. So this is always just one attempt in which case
   * the status is sufficient.
   */
0861ad14c   Hans Verkuil   media: cec: add *...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
  				  u8 status, ktime_t ts);
  
  static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
  					     u8 status)
  {
  	cec_transmit_attempt_done_ts(adap, status, ktime_get());
  }
  
  void cec_received_msg_ts(struct cec_adapter *adap,
  			 struct cec_msg *msg, ktime_t ts);
  
  static inline void cec_received_msg(struct cec_adapter *adap,
  				    struct cec_msg *msg)
  {
  	cec_received_msg_ts(adap, msg, ktime_get());
  }
a56960e8b   Hans Verkuil   [media] cec: add ...
277

ee7e98713   Hans Verkuil   [media] cec.h: me...
278
  /**
9a6b2a874   Hans Verkuil   media: cec: renam...
279
   * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
b8d62f50b   Hans Verkuil   media: cec: add c...
280
281
   *
   * @adap:	pointer to the cec adapter
9a6b2a874   Hans Verkuil   media: cec: renam...
282
   * @is_high:	when true the CEC pin is high, otherwise it is low
b8d62f50b   Hans Verkuil   media: cec: add c...
283
284
285
   * @ts:		the timestamp for this event
   *
   */
9a6b2a874   Hans Verkuil   media: cec: renam...
286
287
  void cec_queue_pin_cec_event(struct cec_adapter *adap,
  			     bool is_high, ktime_t ts);
b8d62f50b   Hans Verkuil   media: cec: add c...
288
289
  
  /**
ee7e98713   Hans Verkuil   [media] cec.h: me...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
   * cec_get_edid_phys_addr() - find and return the physical address
   *
   * @edid:	pointer to the EDID data
   * @size:	size in bytes of the EDID data
   * @offset:	If not %NULL then the location of the physical address
   *		bytes in the EDID will be returned here. This is set to 0
   *		if there is no physical address found.
   *
   * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
   */
  u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  			   unsigned int *offset);
  
  /**
   * cec_set_edid_phys_addr() - find and set the physical address
   *
   * @edid:	pointer to the EDID data
   * @size:	size in bytes of the EDID data
   * @phys_addr:	the new physical address
   *
   * This function finds the location of the physical address in the EDID
   * and fills in the given physical address and updates the checksum
   * at the end of the EDID block. It does nothing if the EDID doesn't
   * contain a physical address.
   */
  void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
  
  /**
   * cec_phys_addr_for_input() - calculate the PA for an input
   *
   * @phys_addr:	the physical address of the parent
   * @input:	the number of the input port, must be between 1 and 15
   *
   * This function calculates a new physical address based on the input
   * port number. For example:
   *
   * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
   *
   * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
   *
   * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
   *
   * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
   *
   * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
   */
  u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
  
  /**
   * cec_phys_addr_validate() - validate a physical address from an EDID
   *
   * @phys_addr:	the physical address to validate
   * @parent:	if not %NULL, then this is filled with the parents PA.
   * @port:	if not %NULL, then this is filled with the input port.
   *
   * This validates a physical address as read from an EDID. If the
   * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
   * then it will return -EINVAL.
   *
   * The parent PA is passed into %parent and the input port is passed into
   * %port. For example:
   *
   * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
   *
   * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
   *
   * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
   *
   * PA = f.f.f.f: has parent f.f.f.f and input port 0.
   *
   * Return: 0 if the PA is valid, -EINVAL if not.
   */
  int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
a56960e8b   Hans Verkuil   [media] cec: add ...
363
  #else
f51e80804   Hans Verkuil   [media] cec: pass...
364
365
  static inline int cec_register_adapter(struct cec_adapter *adap,
  				       struct device *parent)
a56960e8b   Hans Verkuil   [media] cec: add ...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  {
  	return 0;
  }
  
  static inline void cec_unregister_adapter(struct cec_adapter *adap)
  {
  }
  
  static inline void cec_delete_adapter(struct cec_adapter *adap)
  {
  }
  
  static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  				   bool block)
  {
  }
23111ec30   Hans Verkuil   [media] cec: add ...
382
383
384
385
  static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  					     const struct edid *edid)
  {
  }
ee7e98713   Hans Verkuil   [media] cec.h: me...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  					 unsigned int *offset)
  {
  	if (offset)
  		*offset = 0;
  	return CEC_PHYS_ADDR_INVALID;
  }
  
  static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
  					  u16 phys_addr)
  {
  }
  
  static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
  {
  	return CEC_PHYS_ADDR_INVALID;
  }
  
  static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  {
  	return 0;
  }
a56960e8b   Hans Verkuil   [media] cec: add ...
408
  #endif
135327899   Hans Verkuil   [media] cec: add ...
409
410
411
412
413
414
415
416
417
418
419
420
  /**
   * cec_phys_addr_invalidate() - set the physical address to INVALID
   *
   * @adap:	the CEC adapter
   *
   * This is a simple helper function to invalidate the physical
   * address.
   */
  static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
  {
  	cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
  }
a56960e8b   Hans Verkuil   [media] cec: add ...
421
  #endif /* _MEDIA_CEC_H */