cec.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * @parent: parent device
  36. * @minor: device node minor number
  37. * @registered: the device was correctly registered
  38. * @unregistered: the device was unregistered
  39. * @fhs_lock: lock to control access to the filehandle list
  40. * @fhs: the list of open filehandles (cec_fh)
  41. *
  42. * This structure represents a cec-related device node.
  43. *
  44. * The @parent is a physical device. It must be set by core or device drivers
  45. * before registering the node.
  46. */
  47. struct cec_devnode {
  48. /* sysfs */
  49. struct device dev;
  50. struct cdev cdev;
  51. struct device *parent;
  52. /* device info */
  53. int minor;
  54. bool registered;
  55. bool unregistered;
  56. struct list_head fhs;
  57. struct mutex lock;
  58. };
  59. struct cec_adapter;
  60. struct cec_data;
  61. struct cec_data {
  62. struct list_head list;
  63. struct list_head xfer_list;
  64. struct cec_adapter *adap;
  65. struct cec_msg msg;
  66. struct cec_fh *fh;
  67. struct delayed_work work;
  68. struct completion c;
  69. u8 attempts;
  70. bool new_initiator;
  71. bool blocking;
  72. bool completed;
  73. };
  74. struct cec_msg_entry {
  75. struct list_head list;
  76. struct cec_msg msg;
  77. };
  78. #define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
  79. struct cec_fh {
  80. struct list_head list;
  81. struct list_head xfer_list;
  82. struct cec_adapter *adap;
  83. u8 mode_initiator;
  84. u8 mode_follower;
  85. /* Events */
  86. wait_queue_head_t wait;
  87. unsigned int pending_events;
  88. struct cec_event events[CEC_NUM_EVENTS];
  89. struct mutex lock;
  90. struct list_head msgs; /* queued messages */
  91. unsigned int queued_msgs;
  92. };
  93. #define CEC_SIGNAL_FREE_TIME_RETRY 3
  94. #define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
  95. #define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
  96. /* The nominal data bit period is 2.4 ms */
  97. #define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
  98. struct cec_adap_ops {
  99. /* Low-level callbacks */
  100. int (*adap_enable)(struct cec_adapter *adap, bool enable);
  101. int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
  102. int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
  103. int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
  104. u32 signal_free_time, struct cec_msg *msg);
  105. void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
  106. /* High-level CEC message callback */
  107. int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
  108. };
  109. /*
  110. * The minimum message length you can receive (excepting poll messages) is 2.
  111. * With a transfer rate of at most 36 bytes per second this makes 18 messages
  112. * per second worst case.
  113. *
  114. * We queue at most 3 seconds worth of received messages. The CEC specification
  115. * requires that messages are replied to within a second, so 3 seconds should
  116. * give more than enough margin. Since most messages are actually more than 2
  117. * bytes, this is in practice a lot more than 3 seconds.
  118. */
  119. #define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
  120. /*
  121. * The transmit queue is limited to 1 second worth of messages (worst case).
  122. * Messages can be transmitted by userspace and kernel space. But for both it
  123. * makes no sense to have a lot of messages queued up. One second seems
  124. * reasonable.
  125. */
  126. #define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
  127. struct cec_adapter {
  128. struct module *owner;
  129. char name[32];
  130. struct cec_devnode devnode;
  131. struct mutex lock;
  132. struct rc_dev *rc;
  133. struct list_head transmit_queue;
  134. unsigned int transmit_queue_sz;
  135. struct list_head wait_queue;
  136. struct cec_data *transmitting;
  137. struct task_struct *kthread_config;
  138. struct completion config_completion;
  139. struct task_struct *kthread;
  140. wait_queue_head_t kthread_waitq;
  141. wait_queue_head_t waitq;
  142. const struct cec_adap_ops *ops;
  143. void *priv;
  144. u32 capabilities;
  145. u8 available_log_addrs;
  146. u16 phys_addr;
  147. bool is_configuring;
  148. bool is_configured;
  149. u32 monitor_all_cnt;
  150. u32 follower_cnt;
  151. struct cec_fh *cec_follower;
  152. struct cec_fh *cec_initiator;
  153. bool passthrough;
  154. struct cec_log_addrs log_addrs;
  155. struct dentry *cec_dir;
  156. struct dentry *status_file;
  157. u16 phys_addrs[15];
  158. u32 sequence;
  159. char input_name[32];
  160. char input_phys[32];
  161. char input_drv[32];
  162. };
  163. static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
  164. {
  165. return adap->log_addrs.log_addr_mask & (1 << log_addr);
  166. }
  167. static inline bool cec_is_sink(const struct cec_adapter *adap)
  168. {
  169. return adap->phys_addr == 0;
  170. }
  171. #if IS_ENABLED(CONFIG_MEDIA_CEC)
  172. struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
  173. void *priv, const char *name, u32 caps, u8 available_las,
  174. struct device *parent);
  175. int cec_register_adapter(struct cec_adapter *adap);
  176. void cec_unregister_adapter(struct cec_adapter *adap);
  177. void cec_delete_adapter(struct cec_adapter *adap);
  178. int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
  179. bool block);
  180. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  181. bool block);
  182. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  183. bool block);
  184. /* Called by the adapter */
  185. void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
  186. u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
  187. void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
  188. #else
  189. static inline int cec_register_adapter(struct cec_adapter *adap)
  190. {
  191. return 0;
  192. }
  193. static inline void cec_unregister_adapter(struct cec_adapter *adap)
  194. {
  195. }
  196. static inline void cec_delete_adapter(struct cec_adapter *adap)
  197. {
  198. }
  199. static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
  200. bool block)
  201. {
  202. }
  203. #endif
  204. #endif /* _MEDIA_CEC_H */