cec.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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-edid.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. struct dentry *cec_dir;
  154. struct dentry *status_file;
  155. u16 phys_addrs[15];
  156. u32 sequence;
  157. char input_name[32];
  158. char input_phys[32];
  159. char input_drv[32];
  160. };
  161. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  162. {
  163. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  164. }
  165. static inline bool cec_is_sink(const struct cec_adapter *adap)
  166. {
  167. return adap->phys_addr == 0;
  168. }
  169. #if IS_ENABLED(CONFIG_MEDIA_CEC_SUPPORT)
  170. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  171. void *priv, const char *name, u32 caps, u8 available_las);
  172. int cec_register_adapter(struct cec_adapter *adap, struct device *parent);
  173. void cec_unregister_adapter(struct cec_adapter *adap);
  174. void cec_delete_adapter(struct cec_adapter *adap);
  175. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  176. bool block);
  177. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  178. bool block);
  179. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  180. bool block);
  181. /* Called by the adapter */
  182. void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
  183. u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
  184. void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
  185. #else
  186. static inline int cec_register_adapter(struct cec_adapter *adap,
  187. struct device *parent)
  188. {
  189. return 0;
  190. }
  191. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  192. {
  193. }
  194. static inline void cec_delete_adapter(struct cec_adapter *adap)
  195. {
  196. }
  197. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  198. bool block)
  199. {
  200. }
  201. #endif
  202. #endif /* _MEDIA_CEC_H */