cec.h 18 KB

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