cec.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 is_configuring;
  146. bool is_configured;
  147. u32 monitor_all_cnt;
  148. u32 follower_cnt;
  149. struct cec_fh *cec_follower;
  150. struct cec_fh *cec_initiator;
  151. bool passthrough;
  152. struct cec_log_addrs log_addrs;
  153. #ifdef CONFIG_MEDIA_CEC_NOTIFIER
  154. struct cec_notifier *notifier;
  155. #endif
  156. struct dentry *cec_dir;
  157. struct dentry *status_file;
  158. u16 phys_addrs[15];
  159. u32 sequence;
  160. char input_name[32];
  161. char input_phys[32];
  162. char input_drv[32];
  163. };
  164. static inline void *cec_get_drvdata(const struct cec_adapter *adap)
  165. {
  166. return adap->priv;
  167. }
  168. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  169. {
  170. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  171. }
  172. static inline bool cec_is_sink(const struct cec_adapter *adap)
  173. {
  174. return adap->phys_addr == 0;
  175. }
  176. #define cec_phys_addr_exp(pa) \
  177. ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
  178. #if IS_ENABLED(CONFIG_CEC_CORE)
  179. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  180. void *priv, const char *name, u32 caps, u8 available_las);
  181. int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
  182. void cec_unregister_adapter(struct cec_adapter *adap);
  183. void cec_delete_adapter(struct cec_adapter *adap);
  184. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  185. bool block);
  186. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  187. bool block);
  188. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  189. bool block);
  190. /* Called by the adapter */
  191. void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
  192. u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
  193. void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
  194. /**
  195. * cec_get_edid_phys_addr() - find and return the physical address
  196. *
  197. * @edid: pointer to the EDID data
  198. * @size: size in bytes of the EDID data
  199. * @offset: If not %NULL then the location of the physical address
  200. * bytes in the EDID will be returned here. This is set to 0
  201. * if there is no physical address found.
  202. *
  203. * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
  204. */
  205. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  206. unsigned int *offset);
  207. /**
  208. * cec_set_edid_phys_addr() - find and set the physical address
  209. *
  210. * @edid: pointer to the EDID data
  211. * @size: size in bytes of the EDID data
  212. * @phys_addr: the new physical address
  213. *
  214. * This function finds the location of the physical address in the EDID
  215. * and fills in the given physical address and updates the checksum
  216. * at the end of the EDID block. It does nothing if the EDID doesn't
  217. * contain a physical address.
  218. */
  219. void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
  220. /**
  221. * cec_phys_addr_for_input() - calculate the PA for an input
  222. *
  223. * @phys_addr: the physical address of the parent
  224. * @input: the number of the input port, must be between 1 and 15
  225. *
  226. * This function calculates a new physical address based on the input
  227. * port number. For example:
  228. *
  229. * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
  230. *
  231. * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
  232. *
  233. * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
  234. *
  235. * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
  236. *
  237. * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
  238. */
  239. u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
  240. /**
  241. * cec_phys_addr_validate() - validate a physical address from an EDID
  242. *
  243. * @phys_addr: the physical address to validate
  244. * @parent: if not %NULL, then this is filled with the parents PA.
  245. * @port: if not %NULL, then this is filled with the input port.
  246. *
  247. * This validates a physical address as read from an EDID. If the
  248. * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
  249. * then it will return -EINVAL.
  250. *
  251. * The parent PA is passed into %parent and the input port is passed into
  252. * %port. For example:
  253. *
  254. * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
  255. *
  256. * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
  257. *
  258. * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
  259. *
  260. * PA = f.f.f.f: has parent f.f.f.f and input port 0.
  261. *
  262. * Return: 0 if the PA is valid, -EINVAL if not.
  263. */
  264. int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
  265. #ifdef CONFIG_MEDIA_CEC_NOTIFIER
  266. void cec_register_cec_notifier(struct cec_adapter *adap,
  267. struct cec_notifier *notifier);
  268. #endif
  269. #else
  270. static inline int cec_register_adapter(struct cec_adapter *adap,
  271. struct device *parent)
  272. {
  273. return 0;
  274. }
  275. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  276. {
  277. }
  278. static inline void cec_delete_adapter(struct cec_adapter *adap)
  279. {
  280. }
  281. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  282. bool block)
  283. {
  284. }
  285. static inline u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  286. unsigned int *offset)
  287. {
  288. if (offset)
  289. *offset = 0;
  290. return CEC_PHYS_ADDR_INVALID;
  291. }
  292. static inline void cec_set_edid_phys_addr(u8 *edid, unsigned int size,
  293. u16 phys_addr)
  294. {
  295. }
  296. static inline u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
  297. {
  298. return CEC_PHYS_ADDR_INVALID;
  299. }
  300. static inline int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  301. {
  302. return 0;
  303. }
  304. #endif
  305. #endif /* _MEDIA_CEC_H */