cec.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. #define CEC_CAP_DEFAULTS (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | \
  19. CEC_CAP_PASSTHROUGH | CEC_CAP_RC)
  20. /**
  21. * struct cec_devnode - cec device node
  22. * @dev: cec device
  23. * @cdev: cec character device
  24. * @minor: device node minor number
  25. * @lock: lock to serialize open/release and registration
  26. * @registered: the device was correctly registered
  27. * @unregistered: the device was unregistered
  28. * @lock_fhs: lock to control access to @fhs
  29. * @fhs: the list of open filehandles (cec_fh)
  30. *
  31. * This structure represents a cec-related device node.
  32. *
  33. * To add or remove filehandles from @fhs the @lock must be taken first,
  34. * followed by @lock_fhs. It is safe to access @fhs if either lock is held.
  35. *
  36. * The @parent is a physical device. It must be set by core or device drivers
  37. * before registering the node.
  38. */
  39. struct cec_devnode {
  40. /* sysfs */
  41. struct device dev;
  42. struct cdev cdev;
  43. /* device info */
  44. int minor;
  45. /* serialize open/release and registration */
  46. struct mutex lock;
  47. bool registered;
  48. bool unregistered;
  49. /* protect access to fhs */
  50. struct mutex lock_fhs;
  51. struct list_head fhs;
  52. };
  53. struct cec_adapter;
  54. struct cec_data;
  55. struct cec_pin;
  56. struct cec_notifier;
  57. struct cec_data {
  58. struct list_head list;
  59. struct list_head xfer_list;
  60. struct cec_adapter *adap;
  61. struct cec_msg msg;
  62. struct cec_fh *fh;
  63. struct delayed_work work;
  64. struct completion c;
  65. u8 attempts;
  66. bool blocking;
  67. bool completed;
  68. };
  69. struct cec_msg_entry {
  70. struct list_head list;
  71. struct cec_msg msg;
  72. };
  73. struct cec_event_entry {
  74. struct list_head list;
  75. struct cec_event ev;
  76. };
  77. #define CEC_NUM_CORE_EVENTS 2
  78. #define CEC_NUM_EVENTS CEC_EVENT_PIN_5V_HIGH
  79. struct cec_fh {
  80. struct list_head list;
  81. struct list_head xfer_list;
  82. struct cec_adapter *adap;
  83. u8 mode_initiator;
  84. u8 mode_follower;
  85. /* Events */
  86. wait_queue_head_t wait;
  87. struct mutex lock;
  88. struct list_head events[CEC_NUM_EVENTS]; /* queued events */
  89. u16 queued_events[CEC_NUM_EVENTS];
  90. unsigned int total_queued_events;
  91. struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
  92. struct list_head msgs; /* queued messages */
  93. unsigned int queued_msgs;
  94. };
  95. #define CEC_SIGNAL_FREE_TIME_RETRY 3
  96. #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
  97. #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
  98. /* The nominal data bit period is 2.4 ms */
  99. #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
  100. struct cec_adap_ops {
  101. /* Low-level callbacks */
  102. int (*adap_enable)(struct cec_adapter *adap, bool enable);
  103. int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  104. int (*adap_monitor_pin_enable)(struct cec_adapter *adap, bool enable);
  105. int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  106. void (*adap_configured)(struct cec_adapter *adap, bool configured);
  107. int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  108. u32 signal_free_time, struct cec_msg *msg);
  109. void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
  110. void (*adap_free)(struct cec_adapter *adap);
  111. /* Error injection callbacks */
  112. int (*error_inj_show)(struct cec_adapter *adap, struct seq_file *sf);
  113. bool (*error_inj_parse_line)(struct cec_adapter *adap, char *line);
  114. /* High-level CEC message callback */
  115. int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
  116. };
  117. /*
  118. * The minimum message length you can receive (excepting poll messages) is 2.
  119. * With a transfer rate of at most 36 bytes per second this makes 18 messages
  120. * per second worst case.
  121. *
  122. * We queue at most 3 seconds worth of received messages. The CEC specification
  123. * requires that messages are replied to within a second, so 3 seconds should
  124. * give more than enough margin. Since most messages are actually more than 2
  125. * bytes, this is in practice a lot more than 3 seconds.
  126. */
  127. #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
  128. /*
  129. * The transmit queue is limited to 1 second worth of messages (worst case).
  130. * Messages can be transmitted by userspace and kernel space. But for both it
  131. * makes no sense to have a lot of messages queued up. One second seems
  132. * reasonable.
  133. */
  134. #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
  135. /**
  136. * struct cec_adapter - cec adapter structure
  137. * @owner: module owner
  138. * @name: name of the CEC adapter
  139. * @devnode: device node for the /dev/cecX device
  140. * @lock: mutex controlling access to this structure
  141. * @rc: remote control device
  142. * @transmit_queue: queue of pending transmits
  143. * @transmit_queue_sz: number of pending transmits
  144. * @wait_queue: queue of transmits waiting for a reply
  145. * @transmitting: CEC messages currently being transmitted
  146. * @transmit_in_progress: true if a transmit is in progress
  147. * @transmit_in_progress_aborted: true if a transmit is in progress is to be
  148. * aborted. This happens if the logical address is
  149. * invalidated while the transmit is ongoing. In that
  150. * case the transmit will finish, but will not retransmit
  151. * and be marked as ABORTED.
  152. * @xfer_timeout_ms: the transfer timeout in ms.
  153. * If 0, then timeout after 2.1 ms.
  154. * @kthread_config: kthread used to configure a CEC adapter
  155. * @config_completion: used to signal completion of the config kthread
  156. * @kthread: main CEC processing thread
  157. * @kthread_waitq: main CEC processing wait_queue
  158. * @ops: cec adapter ops
  159. * @priv: cec driver's private data
  160. * @capabilities: cec adapter capabilities
  161. * @available_log_addrs: maximum number of available logical addresses
  162. * @phys_addr: the current physical address
  163. * @needs_hpd: if true, then the HDMI HotPlug Detect pin must be high
  164. * in order to transmit or receive CEC messages. This is usually a HW
  165. * limitation.
  166. * @is_enabled: the CEC adapter is enabled
  167. * @is_configuring: the CEC adapter is configuring (i.e. claiming LAs)
  168. * @must_reconfigure: while configuring, the PA changed, so reclaim LAs
  169. * @is_configured: the CEC adapter is configured (i.e. has claimed LAs)
  170. * @cec_pin_is_high: if true then the CEC pin is high. Only used with the
  171. * CEC pin framework.
  172. * @adap_controls_phys_addr: if true, then the CEC adapter controls the
  173. * physical address, i.e. the CEC hardware can detect HPD changes and
  174. * read the EDID and is not dependent on an external HDMI driver.
  175. * Drivers that need this can set this field to true after the
  176. * cec_allocate_adapter() call.
  177. * @last_initiator: the initiator of the last transmitted message.
  178. * @monitor_all_cnt: number of filehandles monitoring all msgs
  179. * @monitor_pin_cnt: number of filehandles monitoring pin changes
  180. * @follower_cnt: number of filehandles in follower mode
  181. * @cec_follower: filehandle of the exclusive follower
  182. * @cec_initiator: filehandle of the exclusive initiator
  183. * @passthrough: if true, then the exclusive follower is in
  184. * passthrough mode.
  185. * @log_addrs: current logical addresses
  186. * @conn_info: current connector info
  187. * @tx_timeouts: number of transmit timeouts
  188. * @notifier: CEC notifier
  189. * @pin: CEC pin status struct
  190. * @cec_dir: debugfs cec directory
  191. * @status_file: debugfs cec status file
  192. * @error_inj_file: debugfs cec error injection file
  193. * @sequence: transmit sequence counter
  194. * @input_phys: remote control input_phys name
  195. *
  196. * This structure represents a cec adapter.
  197. */
  198. struct cec_adapter {
  199. struct module *owner;
  200. char name[32];
  201. struct cec_devnode devnode;
  202. struct mutex lock;
  203. struct rc_dev *rc;
  204. struct list_head transmit_queue;
  205. unsigned int transmit_queue_sz;
  206. struct list_head wait_queue;
  207. struct cec_data *transmitting;
  208. bool transmit_in_progress;
  209. bool transmit_in_progress_aborted;
  210. unsigned int xfer_timeout_ms;
  211. struct task_struct *kthread_config;
  212. struct completion config_completion;
  213. struct task_struct *kthread;
  214. wait_queue_head_t kthread_waitq;
  215. const struct cec_adap_ops *ops;
  216. void *priv;
  217. u32 capabilities;
  218. u8 available_log_addrs;
  219. u16 phys_addr;
  220. bool needs_hpd;
  221. bool is_enabled;
  222. bool is_configuring;
  223. bool must_reconfigure;
  224. bool is_configured;
  225. bool cec_pin_is_high;
  226. bool adap_controls_phys_addr;
  227. u8 last_initiator;
  228. u32 monitor_all_cnt;
  229. u32 monitor_pin_cnt;
  230. u32 follower_cnt;
  231. struct cec_fh *cec_follower;
  232. struct cec_fh *cec_initiator;
  233. bool passthrough;
  234. struct cec_log_addrs log_addrs;
  235. struct cec_connector_info conn_info;
  236. u32 tx_timeouts;
  237. #ifdef CONFIG_CEC_NOTIFIER
  238. struct cec_notifier *notifier;
  239. #endif
  240. #ifdef CONFIG_CEC_PIN
  241. struct cec_pin *pin;
  242. #endif
  243. struct dentry *cec_dir;
  244. u32 sequence;
  245. char input_phys[32];
  246. };
  247. static inline void *cec_get_drvdata(const struct cec_adapter *adap)
  248. {
  249. return adap->priv;
  250. }
  251. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  252. {
  253. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  254. }
  255. static inline bool cec_is_sink(const struct cec_adapter *adap)
  256. {
  257. return adap->phys_addr == 0;
  258. }
  259. /**
  260. * cec_is_registered() - is the CEC adapter registered?
  261. *
  262. * @adap: the CEC adapter, may be NULL.
  263. *
  264. * Return: true if the adapter is registered, false otherwise.
  265. */
  266. static inline bool cec_is_registered(const struct cec_adapter *adap)
  267. {
  268. return adap && adap->devnode.registered;
  269. }
  270. #define cec_phys_addr_exp(pa) \
  271. ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
  272. struct edid;
  273. struct drm_connector;
  274. #if IS_REACHABLE(CONFIG_CEC_CORE)
  275. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  276. void *priv, const char *name, u32 caps, u8 available_las);
  277. int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
  278. void cec_unregister_adapter(struct cec_adapter *adap);
  279. void cec_delete_adapter(struct cec_adapter *adap);
  280. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  281. bool block);
  282. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  283. bool block);
  284. void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  285. const struct edid *edid);
  286. void cec_s_conn_info(struct cec_adapter *adap,
  287. const struct cec_connector_info *conn_info);
  288. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  289. bool block);
  290. /* Called by the adapter */
  291. void cec_transmit_done_ts(struct cec_adapter *adap, u8 status,
  292. u8 arb_lost_cnt, u8 nack_cnt, u8 low_drive_cnt,
  293. u8 error_cnt, ktime_t ts);
  294. static inline void cec_transmit_done(struct cec_adapter *adap, u8 status,
  295. u8 arb_lost_cnt, u8 nack_cnt,
  296. u8 low_drive_cnt, u8 error_cnt)
  297. {
  298. cec_transmit_done_ts(adap, status, arb_lost_cnt, nack_cnt,
  299. low_drive_cnt, error_cnt, ktime_get());
  300. }
  301. /*
  302. * Simplified version of cec_transmit_done for hardware that doesn't retry
  303. * failed transmits. So this is always just one attempt in which case
  304. * the status is sufficient.
  305. */
  306. void cec_transmit_attempt_done_ts(struct cec_adapter *adap,
  307. u8 status, ktime_t ts);
  308. static inline void cec_transmit_attempt_done(struct cec_adapter *adap,
  309. u8 status)
  310. {
  311. cec_transmit_attempt_done_ts(adap, status, ktime_get());
  312. }
  313. void cec_received_msg_ts(struct cec_adapter *adap,
  314. struct cec_msg *msg, ktime_t ts);
  315. static inline void cec_received_msg(struct cec_adapter *adap,
  316. struct cec_msg *msg)
  317. {
  318. cec_received_msg_ts(adap, msg, ktime_get());
  319. }
  320. /**
  321. * cec_queue_pin_cec_event() - queue a CEC pin event with a given timestamp.
  322. *
  323. * @adap: pointer to the cec adapter
  324. * @is_high: when true the CEC pin is high, otherwise it is low
  325. * @dropped_events: when true some events were dropped
  326. * @ts: the timestamp for this event
  327. *
  328. */
  329. void cec_queue_pin_cec_event(struct cec_adapter *adap, bool is_high,
  330. bool dropped_events, ktime_t ts);
  331. /**
  332. * cec_queue_pin_hpd_event() - queue a pin event with a given timestamp.
  333. *
  334. * @adap: pointer to the cec adapter
  335. * @is_high: when true the HPD pin is high, otherwise it is low
  336. * @ts: the timestamp for this event
  337. *
  338. */
  339. void cec_queue_pin_hpd_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
  340. /**
  341. * cec_queue_pin_5v_event() - queue a pin event with a given timestamp.
  342. *
  343. * @adap: pointer to the cec adapter
  344. * @is_high: when true the 5V pin is high, otherwise it is low
  345. * @ts: the timestamp for this event
  346. *
  347. */
  348. void cec_queue_pin_5v_event(struct cec_adapter *adap, bool is_high, ktime_t ts);
  349. /**
  350. * cec_get_edid_phys_addr() - find and return the physical address
  351. *
  352. * @edid: pointer to the EDID data
  353. * @size: size in bytes of the EDID data
  354. * @offset: If not %NULL then the location of the physical address
  355. * bytes in the EDID will be returned here. This is set to 0
  356. * if there is no physical address found.
  357. *
  358. * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
  359. */
  360. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  361. unsigned int *offset);
  362. void cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
  363. const struct drm_connector *connector);
  364. #else
  365. static inline int cec_register_adapter(struct cec_adapter *adap,
  366. struct device *parent)
  367. {
  368. return 0;
  369. }
  370. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  371. {
  372. }
  373. static inline void cec_delete_adapter(struct cec_adapter *adap)
  374. {
  375. }
  376. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  377. bool block)
  378. {
  379. }
  380. static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  381. const struct edid *edid)
  382. {
  383. }
  384. static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  385. unsigned int *offset)
  386. {
  387. if (offset)
  388. *offset = 0;
  389. return CEC_PHYS_ADDR_INVALID;
  390. }
  391. static inline void cec_s_conn_info(struct cec_adapter *adap,
  392. const struct cec_connector_info *conn_info)
  393. {
  394. }
  395. static inline void
  396. cec_fill_conn_info_from_drm(struct cec_connector_info *conn_info,
  397. const struct drm_connector *connector)
  398. {
  399. memset(conn_info, 0, sizeof(*conn_info));
  400. }
  401. #endif
  402. /**
  403. * cec_phys_addr_invalidate() - set the physical address to INVALID
  404. *
  405. * @adap: the CEC adapter
  406. *
  407. * This is a simple helper function to invalidate the physical
  408. * address.
  409. */
  410. static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
  411. {
  412. cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
  413. }
  414. /**
  415. * cec_get_edid_spa_location() - find location of the Source Physical Address
  416. *
  417. * @edid: the EDID
  418. * @size: the size of the EDID
  419. *
  420. * This EDID is expected to be a CEA-861 compliant, which means that there are
  421. * at least two blocks and one or more of the extensions blocks are CEA-861
  422. * blocks.
  423. *
  424. * The returned location is guaranteed to be <= size-2.
  425. *
  426. * This is an inline function since it is used by both CEC and V4L2.
  427. * Ideally this would go in a module shared by both, but it is overkill to do
  428. * that for just a single function.
  429. */
  430. static inline unsigned int cec_get_edid_spa_location(const u8 *edid,
  431. unsigned int size)
  432. {
  433. unsigned int blocks = size / 128;
  434. unsigned int block;
  435. u8 d;
  436. /* Sanity check: at least 2 blocks and a multiple of the block size */
  437. if (blocks < 2 || size % 128)
  438. return 0;
  439. /*
  440. * If there are fewer extension blocks than the size, then update
  441. * 'blocks'. It is allowed to have more extension blocks than the size,
  442. * since some hardware can only read e.g. 256 bytes of the EDID, even
  443. * though more blocks are present. The first CEA-861 extension block
  444. * should normally be in block 1 anyway.
  445. */
  446. if (edid[0x7e] + 1 < blocks)
  447. blocks = edid[0x7e] + 1;
  448. for (block = 1; block < blocks; block++) {
  449. unsigned int offset = block * 128;
  450. /* Skip any non-CEA-861 extension blocks */
  451. if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
  452. continue;
  453. /* search Vendor Specific Data Block (tag 3) */
  454. d = edid[offset + 2] & 0x7f;
  455. /* Check if there are Data Blocks */
  456. if (d <= 4)
  457. continue;
  458. if (d > 4) {
  459. unsigned int i = offset + 4;
  460. unsigned int end = offset + d;
  461. /* Note: 'end' is always < 'size' */
  462. do {
  463. u8 tag = edid[i] >> 5;
  464. u8 len = edid[i] & 0x1f;
  465. if (tag == 3 && len >= 5 && i + len <= end &&
  466. edid[i + 1] == 0x03 &&
  467. edid[i + 2] == 0x0c &&
  468. edid[i + 3] == 0x00)
  469. return i + 4;
  470. i += len + 1;
  471. } while (i < end);
  472. }
  473. }
  474. return 0;
  475. }
  476. #endif /* _MEDIA_CEC_H */