cec.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * cec - HDMI Consumer Electronics Control support header
  3. *
  4. * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #ifndef _MEDIA_CEC_H
  20. #define _MEDIA_CEC_H
  21. #include <linux/poll.h>
  22. #include <linux/fs.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/device.h>
  25. #include <linux/cdev.h>
  26. #include <linux/kthread.h>
  27. #include <linux/timer.h>
  28. #include <linux/cec-funcs.h>
  29. #include <media/rc-core.h>
  30. #include <media/cec-notifier.h>
  31. /**
  32. * struct cec_devnode - cec device node
  33. * @dev: cec device
  34. * @cdev: cec character device
  35. * @minor: device node minor number
  36. * @registered: the device was correctly registered
  37. * @unregistered: the device was unregistered
  38. * @fhs_lock: lock to control access to the filehandle list
  39. * @fhs: the list of open filehandles (cec_fh)
  40. *
  41. * This structure represents a cec-related device node.
  42. *
  43. * The @parent is a physical device. It must be set by core or device drivers
  44. * before registering the node.
  45. */
  46. struct cec_devnode {
  47. /* sysfs */
  48. struct device dev;
  49. struct cdev cdev;
  50. /* device info */
  51. int minor;
  52. bool registered;
  53. bool unregistered;
  54. struct list_head fhs;
  55. struct mutex lock;
  56. };
  57. struct cec_adapter;
  58. struct cec_data;
  59. struct cec_data {
  60. struct list_head list;
  61. struct list_head xfer_list;
  62. struct cec_adapter *adap;
  63. struct cec_msg msg;
  64. struct cec_fh *fh;
  65. struct delayed_work work;
  66. struct completion c;
  67. u8 attempts;
  68. bool new_initiator;
  69. bool blocking;
  70. bool completed;
  71. };
  72. struct cec_msg_entry {
  73. struct list_head list;
  74. struct cec_msg msg;
  75. };
  76. #define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
  77. struct cec_fh {
  78. struct list_head list;
  79. struct list_head xfer_list;
  80. struct cec_adapter *adap;
  81. u8 mode_initiator;
  82. u8 mode_follower;
  83. /* Events */
  84. wait_queue_head_t wait;
  85. unsigned int pending_events;
  86. struct cec_event events[CEC_NUM_EVENTS];
  87. struct mutex lock;
  88. struct list_head msgs; /* queued messages */
  89. unsigned int queued_msgs;
  90. };
  91. #define CEC_SIGNAL_FREE_TIME_RETRY 3
  92. #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
  93. #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
  94. /* The nominal data bit period is 2.4 ms */
  95. #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
  96. struct cec_adap_ops {
  97. /* Low-level callbacks */
  98. int (*adap_enable)(struct cec_adapter *adap, bool enable);
  99. int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  100. int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  101. int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  102. u32 signal_free_time, struct cec_msg *msg);
  103. void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
  104. /* High-level CEC message callback */
  105. int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
  106. };
  107. /*
  108. * The minimum message length you can receive (excepting poll messages) is 2.
  109. * With a transfer rate of at most 36 bytes per second this makes 18 messages
  110. * per second worst case.
  111. *
  112. * We queue at most 3 seconds worth of received messages. The CEC specification
  113. * requires that messages are replied to within a second, so 3 seconds should
  114. * give more than enough margin. Since most messages are actually more than 2
  115. * bytes, this is in practice a lot more than 3 seconds.
  116. */
  117. #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
  118. /*
  119. * The transmit queue is limited to 1 second worth of messages (worst case).
  120. * Messages can be transmitted by userspace and kernel space. But for both it
  121. * makes no sense to have a lot of messages queued up. One second seems
  122. * reasonable.
  123. */
  124. #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
  125. struct cec_adapter {
  126. struct module *owner;
  127. char name[32];
  128. struct cec_devnode devnode;
  129. struct mutex lock;
  130. struct rc_dev *rc;
  131. struct list_head transmit_queue;
  132. unsigned int transmit_queue_sz;
  133. struct list_head wait_queue;
  134. struct cec_data *transmitting;
  135. struct task_struct *kthread_config;
  136. struct completion config_completion;
  137. struct task_struct *kthread;
  138. wait_queue_head_t kthread_waitq;
  139. wait_queue_head_t waitq;
  140. const struct cec_adap_ops *ops;
  141. void *priv;
  142. u32 capabilities;
  143. u8 available_log_addrs;
  144. u16 phys_addr;
  145. bool needs_hpd;
  146. bool is_configuring;
  147. bool is_configured;
  148. u32 monitor_all_cnt;
  149. u32 follower_cnt;
  150. struct cec_fh *cec_follower;
  151. struct cec_fh *cec_initiator;
  152. bool passthrough;
  153. struct cec_log_addrs log_addrs;
  154. #ifdef CONFIG_CEC_NOTIFIER
  155. struct cec_notifier *notifier;
  156. #endif
  157. struct dentry *cec_dir;
  158. struct dentry *status_file;
  159. u16 phys_addrs[15];
  160. u32 sequence;
  161. char input_name[32];
  162. char input_phys[32];
  163. char input_drv[32];
  164. };
  165. static inline void *cec_get_drvdata(const struct cec_adapter *adap)
  166. {
  167. return adap->priv;
  168. }
  169. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  170. {
  171. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  172. }
  173. static inline bool cec_is_sink(const struct cec_adapter *adap)
  174. {
  175. return adap->phys_addr == 0;
  176. }
  177. #define cec_phys_addr_exp(pa) \
  178. ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
  179. struct edid;
  180. #if IS_REACHABLE(CONFIG_CEC_CORE)
  181. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  182. void *priv, const char *name, u32 caps, u8 available_las);
  183. int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
  184. void cec_unregister_adapter(struct cec_adapter *adap);
  185. void cec_delete_adapter(struct cec_adapter *adap);
  186. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  187. bool block);
  188. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  189. bool block);
  190. void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  191. const struct edid *edid);
  192. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  193. bool block);
  194. /* Called by the adapter */
  195. void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
  196. u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
  197. /*
  198. * Simplified version of cec_transmit_done for hardware that doesn't retry
  199. * failed transmits. So this is always just one attempt in which case
  200. * the status is sufficient.
  201. */
  202. void cec_transmit_attempt_done(struct cec_adapter *adap, u8 status);
  203. void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
  204. /**
  205. * cec_get_edid_phys_addr() - find and return the physical address
  206. *
  207. * @edid: pointer to the EDID data
  208. * @size: size in bytes of the EDID data
  209. * @offset: If not %NULL then the location of the physical address
  210. * bytes in the EDID will be returned here. This is set to 0
  211. * if there is no physical address found.
  212. *
  213. * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
  214. */
  215. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  216. unsigned int *offset);
  217. /**
  218. * cec_set_edid_phys_addr() - find and set the physical address
  219. *
  220. * @edid: pointer to the EDID data
  221. * @size: size in bytes of the EDID data
  222. * @phys_addr: the new physical address
  223. *
  224. * This function finds the location of the physical address in the EDID
  225. * and fills in the given physical address and updates the checksum
  226. * at the end of the EDID block. It does nothing if the EDID doesn't
  227. * contain a physical address.
  228. */
  229. void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
  230. /**
  231. * cec_phys_addr_for_input() - calculate the PA for an input
  232. *
  233. * @phys_addr: the physical address of the parent
  234. * @input: the number of the input port, must be between 1 and 15
  235. *
  236. * This function calculates a new physical address based on the input
  237. * port number. For example:
  238. *
  239. * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
  240. *
  241. * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
  242. *
  243. * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
  244. *
  245. * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
  246. *
  247. * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
  248. */
  249. u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
  250. /**
  251. * cec_phys_addr_validate() - validate a physical address from an EDID
  252. *
  253. * @phys_addr: the physical address to validate
  254. * @parent: if not %NULL, then this is filled with the parents PA.
  255. * @port: if not %NULL, then this is filled with the input port.
  256. *
  257. * This validates a physical address as read from an EDID. If the
  258. * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
  259. * then it will return -EINVAL.
  260. *
  261. * The parent PA is passed into %parent and the input port is passed into
  262. * %port. For example:
  263. *
  264. * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
  265. *
  266. * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
  267. *
  268. * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
  269. *
  270. * PA = f.f.f.f: has parent f.f.f.f and input port 0.
  271. *
  272. * Return: 0 if the PA is valid, -EINVAL if not.
  273. */
  274. int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
  275. #ifdef CONFIG_CEC_NOTIFIER
  276. void cec_register_cec_notifier(struct cec_adapter *adap,
  277. struct cec_notifier *notifier);
  278. #endif
  279. #else
  280. static inline int cec_register_adapter(struct cec_adapter *adap,
  281. struct device *parent)
  282. {
  283. return 0;
  284. }
  285. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  286. {
  287. }
  288. static inline void cec_delete_adapter(struct cec_adapter *adap)
  289. {
  290. }
  291. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  292. bool block)
  293. {
  294. }
  295. static inline void cec_s_phys_addr_from_edid(struct cec_adapter *adap,
  296. const struct edid *edid)
  297. {
  298. }
  299. static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  300. unsigned int *offset)
  301. {
  302. if (offset)
  303. *offset = 0;
  304. return CEC_PHYS_ADDR_INVALID;
  305. }
  306. static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
  307. u16 phys_addr)
  308. {
  309. }
  310. static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
  311. {
  312. return CEC_PHYS_ADDR_INVALID;
  313. }
  314. static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  315. {
  316. return 0;
  317. }
  318. #endif
  319. /**
  320. * cec_phys_addr_invalidate() - set the physical address to INVALID
  321. *
  322. * @adap: the CEC adapter
  323. *
  324. * This is a simple helper function to invalidate the physical
  325. * address.
  326. */
  327. static inline void cec_phys_addr_invalidate(struct cec_adapter *adap)
  328. {
  329. cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
  330. }
  331. #endif /* _MEDIA_CEC_H */