cec.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * cec - HDMI Consumer Electronics Control support header
  4. *
  5. * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #ifndef _MEDIA_CEC_H
  8. #define _MEDIA_CEC_H
  9. #include <linux/poll.h>
  10. #include <linux/fs.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/device.h>
  13. #include <linux/cdev.h>
  14. #include <linux/kthread.h>
  15. #include <linux/timer.h>
  16. #include <linux/cec-funcs.h>
  17. #include <media/rc-core.h>
  18. #include <media/cec-notifier.h>
  19. #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
  20. CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
  21. /**
  22. * struct cec_devnode - cec device node
  23. * @dev: cec device
  24. * @cdev: cec character device
  25. * @minor: device node minor number
  26. * @registered: the device was correctly registered
  27. * @unregistered: the device was unregistered
  28. * @fhs_lock: lock to control access to the filehandle list
  29. * @fhs: the list of open filehandles (cec_fh)
  30. *
  31. * This structure represents a cec-related device node.
  32. *
  33. * The @parent is a physical device. It must be set by core or device drivers
  34. * before registering the node.
  35. */
  36. struct cec_devnode {
  37. /* sysfs */
  38. struct device dev;
  39. struct cdev cdev;
  40. /* device info */
  41. int minor;
  42. bool registered;
  43. bool unregistered;
  44. struct list_head fhs;
  45. struct mutex lock;
  46. };
  47. struct cec_adapter;
  48. struct cec_data;
  49. struct cec_pin;
  50. struct cec_data {
  51. struct list_head list;
  52. struct list_head xfer_list;
  53. struct cec_adapter *adap;
  54. struct cec_msg msg;
  55. struct cec_fh *fh;
  56. struct delayed_work work;
  57. struct completion c;
  58. u8 attempts;
  59. bool blocking;
  60. bool completed;
  61. };
  62. struct cec_msg_entry {
  63. struct list_head list;
  64. struct cec_msg msg;
  65. };
  66. struct cec_event_entry {
  67. struct list_head list;
  68. struct cec_event ev;
  69. };
  70. #define CEC_NUM_CORE_EVENTS 2
  71. #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
  72. struct cec_fh {
  73. struct list_head list;
  74. struct list_head xfer_list;
  75. struct cec_adapter *adap;
  76. u8 mode_initiator;
  77. u8 mode_follower;
  78. /* Events */
  79. wait_queue_head_t wait;
  80. struct mutex lock;
  81. struct list_head events[CEC_NUM_EVENTS]; /* queued events */
  82. u16 queued_events[CEC_NUM_EVENTS];
  83. unsigned int total_queued_events;
  84. struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
  85. struct list_head msgs; /* queued messages */
  86. unsigned int queued_msgs;
  87. };
  88. #define CEC_SIGNAL_FREE_TIME_RETRY 3
  89. #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
  90. #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
  91. /* The nominal data bit period is 2.4 ms */
  92. #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
  93. struct cec_adap_ops {
  94. /* Low-level callbacks */
  95. int (*adap_enable)(struct cec_adapter *adap, bool enable);
  96. int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  97. int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
  98. int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  99. int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  100. u32 signal_free_time, struct cec_msg *msg);
  101. void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
  102. void (*adap_free)(struct cec_adapter *adap);
  103. /* Error injection callbacks */
  104. int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
  105. bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
  106. /* High-level CEC message callback */
  107. int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
  108. };
  109. /*
  110. * The minimum message length you can receive (excepting poll messages) is 2.
  111. * With a transfer rate of at most 36 bytes per second this makes 18 messages
  112. * per second worst case.
  113. *
  114. * We queue at most 3 seconds worth of received messages. The CEC specification
  115. * requires that messages are replied to within a second, so 3 seconds should
  116. * give more than enough margin. Since most messages are actually more than 2
  117. * bytes, this is in practice a lot more than 3 seconds.
  118. */
  119. #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
  120. /*
  121. * The transmit queue is limited to 1 second worth of messages (worst case).
  122. * Messages can be transmitted by userspace and kernel space. But for both it
  123. * makes no sense to have a lot of messages queued up. One second seems
  124. * reasonable.
  125. */
  126. #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
  127. struct cec_adapter {
  128. struct module *owner;
  129. char name[32];
  130. struct cec_devnode devnode;
  131. struct mutex lock;
  132. struct rc_dev *rc;
  133. struct list_head transmit_queue;
  134. unsigned int transmit_queue_sz;
  135. struct list_head wait_queue;
  136. struct cec_data *transmitting;
  137. struct task_struct *kthread_config;
  138. struct completion config_completion;
  139. struct task_struct *kthread;
  140. wait_queue_head_t kthread_waitq;
  141. wait_queue_head_t waitq;
  142. const struct cec_adap_ops *ops;
  143. void *priv;
  144. u32 capabilities;
  145. u8 available_log_addrs;
  146. u16 phys_addr;
  147. bool needs_hpd;
  148. bool is_configuring;
  149. bool is_configured;
  150. bool cec_pin_is_high;
  151. u8 last_initiator;
  152. u32 monitor_all_cnt;
  153. u32 monitor_pin_cnt;
  154. u32 follower_cnt;
  155. struct cec_fh *cec_follower;
  156. struct cec_fh *cec_initiator;
  157. bool passthrough;
  158. struct cec_log_addrs log_addrs;
  159. u32 tx_timeouts;
  160. #ifdef CONFIG_CEC_NOTIFIER
  161. struct cec_notifier *notifier;
  162. #endif
  163. #ifdef CONFIG_CEC_PIN
  164. struct cec_pin *pin;
  165. #endif
  166. struct dentry *cec_dir;
  167. struct dentry *status_file;
  168. struct dentry *error_inj_file;
  169. u16 phys_addrs[15];
  170. u32 sequence;
  171. char input_phys[32];
  172. };
  173. static inline void *cec_get_drvdata(const struct cec_adapter *adap)
  174. {
  175. return adap->priv;
  176. }
  177. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  178. {
  179. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  180. }
  181. static inline bool cec_is_sink(const struct cec_adapter *adap)
  182. {
  183. return adap->phys_addr == 0;
  184. }
  185. /**
  186. * cec_is_registered() - is the CEC adapter registered?
  187. *
  188. * @adap: the CEC adapter, may be NULL.
  189. *
  190. * Return: true if the adapter is registered, false otherwise.
  191. */
  192. static inline bool cec_is_registered(const struct cec_adapter *adap)
  193. {
  194. return adap && adap->devnode.registered;
  195. }
  196. #define cec_phys_addr_exp(pa) \
  197. ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
  198. struct edid;
  199. #if IS_REACHABLE(CONFIG_CEC_CORE)
  200. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  201. void *priv, const char *name, u32 caps, u8 available_las);
  202. int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
  203. void cec_unregister_adapter(struct cec_adapter *adap);
  204. void cec_delete_adapter(struct cec_adapter *adap);
  205. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  206. bool block);
  207. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  208. bool block);
  209. void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  210. const struct edid *edid);
  211. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  212. bool block);
  213. /* Called by the adapter */
  214. void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
  215. u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
  216. u8 error_cnt, ktime_t ts);
  217. static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
  218. u8 arb_lost_cnt, u8 nack_cnt,
  219. u8 low_drive_cnt, u8 error_cnt)
  220. {
  221. cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
  222. low_drive_cnt, error_cnt, ktime_get());
  223. }
  224. /*
  225. * Simplified version of cec_transmit_done for hardware that doesn't retry
  226. * failed transmits. So this is always just one attempt in which case
  227. * the status is sufficient.
  228. */
  229. void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
  230. u8 status, ktime_t ts);
  231. static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
  232. u8 status)
  233. {
  234. cec_transmit_attempt_done_ts(adap, status, ktime_get());
  235. }
  236. void cec_received_msg_ts(struct cec_adapter *adap,
  237. struct cec_msg *msg, ktime_t ts);
  238. static inline void cec_received_msg(struct cec_adapter *adap,
  239. struct cec_msg *msg)
  240. {
  241. cec_received_msg_ts(adap, msg, ktime_get());
  242. }
  243. /**
  244. * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
  245. *
  246. * @adap: pointer to the cec adapter
  247. * @is_high: when true the CEC pin is high, otherwise it is low
  248. * @dropped_events: when true some events were dropped
  249. * @ts: the timestamp for this event
  250. *
  251. */
  252. void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
  253. bool dropped_events, ktime_t ts);
  254. /**
  255. * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
  256. *
  257. * @adap: pointer to the cec adapter
  258. * @is_high: when true the HPD pin is high, otherwise it is low
  259. * @ts: the timestamp for this event
  260. *
  261. */
  262. void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
  263. /**
  264. * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
  265. *
  266. * @adap: pointer to the cec adapter
  267. * @is_high: when true the 5V pin is high, otherwise it is low
  268. * @ts: the timestamp for this event
  269. *
  270. */
  271. void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
  272. /**
  273. * cec_get_edid_phys_addr() - find and return the physical address
  274. *
  275. * @edid: pointer to the EDID data
  276. * @size: size in bytes of the EDID data
  277. * @offset: If not %NULL then the location of the physical address
  278. * bytes in the EDID will be returned here. This is set to 0
  279. * if there is no physical address found.
  280. *
  281. * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
  282. */
  283. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  284. unsigned int *offset);
  285. #else
  286. static inline int cec_register_adapter(struct cec_adapter *adap,
  287. struct device *parent)
  288. {
  289. return 0;
  290. }
  291. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  292. {
  293. }
  294. static inline void cec_delete_adapter(struct cec_adapter *adap)
  295. {
  296. }
  297. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  298. bool block)
  299. {
  300. }
  301. static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  302. const struct edid *edid)
  303. {
  304. }
  305. static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  306. unsigned int *offset)
  307. {
  308. if (offset)
  309. *offset = 0;
  310. return CEC_PHYS_ADDR_INVALID;
  311. }
  312. #endif
  313. /**
  314. * cec_phys_addr_invalidate() - set the physical address to INVALID
  315. *
  316. * @adap: the CEC adapter
  317. *
  318. * This is a simple helper function to invalidate the physical
  319. * address.
  320. */
  321. static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
  322. {
  323. cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
  324. }
  325. /**
  326. * cec_get_edid_spa_location() - find location of the Source Physical Address
  327. *
  328. * @edid: the EDID
  329. * @size: the size of the EDID
  330. *
  331. * This EDID is expected to be a CEA-861 compliant, which means that there are
  332. * at least two blocks and one or more of the extensions blocks are CEA-861
  333. * blocks.
  334. *
  335. * The returned location is guaranteed to be <= size-2.
  336. *
  337. * This is an inline function since it is used by both CEC and V4L2.
  338. * Ideally this would go in a module shared by both, but it is overkill to do
  339. * that for just a single function.
  340. */
  341. static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
  342. unsigned int size)
  343. {
  344. unsigned int blocks = size / 128;
  345. unsigned int block;
  346. u8 d;
  347. /* Sanity check: at least 2 blocks and a multiple of the block size */
  348. if (blocks < 2 || size % 128)
  349. return 0;
  350. /*
  351. * If there are fewer extension blocks than the size, then update
  352. * 'blocks'. It is allowed to have more extension blocks than the size,
  353. * since some hardware can only read e.g. 256 bytes of the EDID, even
  354. * though more blocks are present. The first CEA-861 extension block
  355. * should normally be in block 1 anyway.
  356. */
  357. if (edid[0x7e] + 1 < blocks)
  358. blocks = edid[0x7e] + 1;
  359. for (block = 1; block < blocks; block++) {
  360. unsigned int offset = block * 128;
  361. /* Skip any non-CEA-861 extension blocks */
  362. if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
  363. continue;
  364. /* search Vendor Specific Data Block (tag 3) */
  365. d = edid[offset + 2] & 0x7f;
  366. /* Check if there are Data Blocks */
  367. if (d <= 4)
  368. continue;
  369. if (d > 4) {
  370. unsigned int i = offset + 4;
  371. unsigned int end = offset + d;
  372. /* Note: 'end' is always < 'size' */
  373. do {
  374. u8 tag = edid[i] >> 5;
  375. u8 len = edid[i] & 0x1f;
  376. if (tag == 3 && len >= 5 && i + len <= end &&
  377. edid[i + 1] == 0x03 &&
  378. edid[i + 2] == 0x0c &&
  379. edid[i + 3] == 0x00)
  380. return i + 4;
  381. i += len + 1;
  382. } while (i < end);
  383. }
  384. }
  385. return 0;
  386. }
  387. #endif /* _MEDIA_CEC_H */