cec.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. bool transmit_in_progress;
  138. struct task_struct *kthread_config;
  139. struct completion config_completion;
  140. struct task_struct *kthread;
  141. wait_queue_head_t kthread_waitq;
  142. wait_queue_head_t waitq;
  143. const struct cec_adap_ops *ops;
  144. void *priv;
  145. u32 capabilities;
  146. u8 available_log_addrs;
  147. u16 phys_addr;
  148. bool needs_hpd;
  149. bool is_configuring;
  150. bool is_configured;
  151. bool cec_pin_is_high;
  152. u8 last_initiator;
  153. u32 monitor_all_cnt;
  154. u32 monitor_pin_cnt;
  155. u32 follower_cnt;
  156. struct cec_fh *cec_follower;
  157. struct cec_fh *cec_initiator;
  158. bool passthrough;
  159. struct cec_log_addrs log_addrs;
  160. u32 tx_timeouts;
  161. #ifdef CONFIG_CEC_NOTIFIER
  162. struct cec_notifier *notifier;
  163. #endif
  164. #ifdef CONFIG_CEC_PIN
  165. struct cec_pin *pin;
  166. #endif
  167. struct dentry *cec_dir;
  168. struct dentry *status_file;
  169. struct dentry *error_inj_file;
  170. u16 phys_addrs[15];
  171. u32 sequence;
  172. char device_name[32];
  173. char input_phys[32];
  174. char input_drv[32];
  175. };
  176. static inline void *cec_get_drvdata(const struct cec_adapter *adap)
  177. {
  178. return adap->priv;
  179. }
  180. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  181. {
  182. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  183. }
  184. static inline bool cec_is_sink(const struct cec_adapter *adap)
  185. {
  186. return adap->phys_addr == 0;
  187. }
  188. /**
  189. * cec_is_registered() - is the CEC adapter registered?
  190. *
  191. * @adap: the CEC adapter, may be NULL.
  192. *
  193. * Return: true if the adapter is registered, false otherwise.
  194. */
  195. static inline bool cec_is_registered(const struct cec_adapter *adap)
  196. {
  197. return adap && adap->devnode.registered;
  198. }
  199. #define cec_phys_addr_exp(pa) \
  200. ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
  201. struct edid;
  202. #if IS_REACHABLE(CONFIG_CEC_CORE)
  203. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  204. void *priv, const char *name, u32 caps, u8 available_las);
  205. int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
  206. void cec_unregister_adapter(struct cec_adapter *adap);
  207. void cec_delete_adapter(struct cec_adapter *adap);
  208. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  209. bool block);
  210. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  211. bool block);
  212. void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  213. const struct edid *edid);
  214. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  215. bool block);
  216. /* Called by the adapter */
  217. void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
  218. u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
  219. u8 error_cnt, ktime_t ts);
  220. static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
  221. u8 arb_lost_cnt, u8 nack_cnt,
  222. u8 low_drive_cnt, u8 error_cnt)
  223. {
  224. cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
  225. low_drive_cnt, error_cnt, ktime_get());
  226. }
  227. /*
  228. * Simplified version of cec_transmit_done for hardware that doesn't retry
  229. * failed transmits. So this is always just one attempt in which case
  230. * the status is sufficient.
  231. */
  232. void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
  233. u8 status, ktime_t ts);
  234. static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
  235. u8 status)
  236. {
  237. cec_transmit_attempt_done_ts(adap, status, ktime_get());
  238. }
  239. void cec_received_msg_ts(struct cec_adapter *adap,
  240. struct cec_msg *msg, ktime_t ts);
  241. static inline void cec_received_msg(struct cec_adapter *adap,
  242. struct cec_msg *msg)
  243. {
  244. cec_received_msg_ts(adap, msg, ktime_get());
  245. }
  246. /**
  247. * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
  248. *
  249. * @adap: pointer to the cec adapter
  250. * @is_high: when true the CEC pin is high, otherwise it is low
  251. * @dropped_events: when true some events were dropped
  252. * @ts: the timestamp for this event
  253. *
  254. */
  255. void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
  256. bool dropped_events, ktime_t ts);
  257. /**
  258. * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
  259. *
  260. * @adap: pointer to the cec adapter
  261. * @is_high: when true the HPD pin is high, otherwise it is low
  262. * @ts: the timestamp for this event
  263. *
  264. */
  265. void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
  266. /**
  267. * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
  268. *
  269. * @adap: pointer to the cec adapter
  270. * @is_high: when true the 5V pin is high, otherwise it is low
  271. * @ts: the timestamp for this event
  272. *
  273. */
  274. void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
  275. /**
  276. * cec_get_edid_phys_addr() - find and return the physical address
  277. *
  278. * @edid: pointer to the EDID data
  279. * @size: size in bytes of the EDID data
  280. * @offset: If not %NULL then the location of the physical address
  281. * bytes in the EDID will be returned here. This is set to 0
  282. * if there is no physical address found.
  283. *
  284. * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
  285. */
  286. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  287. unsigned int *offset);
  288. #else
  289. static inline int cec_register_adapter(struct cec_adapter *adap,
  290. struct device *parent)
  291. {
  292. return 0;
  293. }
  294. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  295. {
  296. }
  297. static inline void cec_delete_adapter(struct cec_adapter *adap)
  298. {
  299. }
  300. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  301. bool block)
  302. {
  303. }
  304. static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  305. const struct edid *edid)
  306. {
  307. }
  308. static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  309. unsigned int *offset)
  310. {
  311. if (offset)
  312. *offset = 0;
  313. return CEC_PHYS_ADDR_INVALID;
  314. }
  315. #endif
  316. /**
  317. * cec_phys_addr_invalidate() - set the physical address to INVALID
  318. *
  319. * @adap: the CEC adapter
  320. *
  321. * This is a simple helper function to invalidate the physical
  322. * address.
  323. */
  324. static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
  325. {
  326. cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
  327. }
  328. /**
  329. * cec_get_edid_spa_location() - find location of the Source Physical Address
  330. *
  331. * @edid: the EDID
  332. * @size: the size of the EDID
  333. *
  334. * This EDID is expected to be a CEA-861 compliant, which means that there are
  335. * at least two blocks and one or more of the extensions blocks are CEA-861
  336. * blocks.
  337. *
  338. * The returned location is guaranteed to be <= size-2.
  339. *
  340. * This is an inline function since it is used by both CEC and V4L2.
  341. * Ideally this would go in a module shared by both, but it is overkill to do
  342. * that for just a single function.
  343. */
  344. static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
  345. unsigned int size)
  346. {
  347. unsigned int blocks = size / 128;
  348. unsigned int block;
  349. u8 d;
  350. /* Sanity check: at least 2 blocks and a multiple of the block size */
  351. if (blocks < 2 || size % 128)
  352. return 0;
  353. /*
  354. * If there are fewer extension blocks than the size, then update
  355. * 'blocks'. It is allowed to have more extension blocks than the size,
  356. * since some hardware can only read e.g. 256 bytes of the EDID, even
  357. * though more blocks are present. The first CEA-861 extension block
  358. * should normally be in block 1 anyway.
  359. */
  360. if (edid[0x7e] + 1 < blocks)
  361. blocks = edid[0x7e] + 1;
  362. for (block = 1; block < blocks; block++) {
  363. unsigned int offset = block * 128;
  364. /* Skip any non-CEA-861 extension blocks */
  365. if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
  366. continue;
  367. /* search Vendor Specific Data Block (tag 3) */
  368. d = edid[offset + 2] & 0x7f;
  369. /* Check if there are Data Blocks */
  370. if (d <= 4)
  371. continue;
  372. if (d > 4) {
  373. unsigned int i = offset + 4;
  374. unsigned int end = offset + d;
  375. /* Note: 'end' is always < 'size' */
  376. do {
  377. u8 tag = edid[i] >> 5;
  378. u8 len = edid[i] & 0x1f;
  379. if (tag == 3 && len >= 5 && i + len <= end &&
  380. edid[i + 1] == 0x03 &&
  381. edid[i + 2] == 0x0c &&
  382. edid[i + 3] == 0x00)
  383. return i + 4;
  384. i += len + 1;
  385. } while (i < end);
  386. }
  387. }
  388. return 0;
  389. }
  390. #endif /* _MEDIA_CEC_H */