cec.h 18 KB

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