vivid-cec.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * vivid-cec.c - A Virtual Video Test Driver, cec emulation
  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. #include <media/cec.h>
  20. #include "vivid-core.h"
  21. #include "vivid-cec.h"
  22. void vivid_cec_bus_free_work(struct vivid_dev *dev)
  23. {
  24. spin_lock(&dev->cec_slock);
  25. while (!list_empty(&dev->cec_work_list)) {
  26. struct vivid_cec_work *cw =
  27. list_first_entry(&dev->cec_work_list,
  28. struct vivid_cec_work, list);
  29. spin_unlock(&dev->cec_slock);
  30. cancel_delayed_work_sync(&cw->work);
  31. spin_lock(&dev->cec_slock);
  32. list_del(&cw->list);
  33. cec_transmit_done(cw->adap, CEC_TX_STATUS_LOW_DRIVE, 0, 0, 1, 0);
  34. kfree(cw);
  35. }
  36. spin_unlock(&dev->cec_slock);
  37. }
  38. static bool vivid_cec_find_dest_adap(struct vivid_dev *dev,
  39. struct cec_adapter *adap, u8 dest)
  40. {
  41. unsigned int i;
  42. if (dest >= 0xf)
  43. return false;
  44. if (adap != dev->cec_rx_adap && dev->cec_rx_adap &&
  45. dev->cec_rx_adap->is_configured &&
  46. cec_has_log_addr(dev->cec_rx_adap, dest))
  47. return true;
  48. for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++) {
  49. if (adap == dev->cec_tx_adap[i])
  50. continue;
  51. if (!dev->cec_tx_adap[i]->is_configured)
  52. continue;
  53. if (cec_has_log_addr(dev->cec_tx_adap[i], dest))
  54. return true;
  55. }
  56. return false;
  57. }
  58. static void vivid_cec_xfer_done_worker(struct work_struct *work)
  59. {
  60. struct vivid_cec_work *cw =
  61. container_of(work, struct vivid_cec_work, work.work);
  62. struct vivid_dev *dev = cw->dev;
  63. struct cec_adapter *adap = cw->adap;
  64. u8 dest = cec_msg_destination(&cw->msg);
  65. bool valid_dest;
  66. unsigned int i;
  67. valid_dest = cec_msg_is_broadcast(&cw->msg);
  68. if (!valid_dest)
  69. valid_dest = vivid_cec_find_dest_adap(dev, adap, dest);
  70. cw->tx_status = valid_dest ? CEC_TX_STATUS_OK : CEC_TX_STATUS_NACK;
  71. spin_lock(&dev->cec_slock);
  72. dev->cec_xfer_time_jiffies = 0;
  73. dev->cec_xfer_start_jiffies = 0;
  74. list_del(&cw->list);
  75. spin_unlock(&dev->cec_slock);
  76. cec_transmit_done(cw->adap, cw->tx_status, 0, valid_dest ? 0 : 1, 0, 0);
  77. /* Broadcast message */
  78. if (adap != dev->cec_rx_adap)
  79. cec_received_msg(dev->cec_rx_adap, &cw->msg);
  80. for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
  81. if (adap != dev->cec_tx_adap[i])
  82. cec_received_msg(dev->cec_tx_adap[i], &cw->msg);
  83. kfree(cw);
  84. }
  85. static void vivid_cec_xfer_try_worker(struct work_struct *work)
  86. {
  87. struct vivid_cec_work *cw =
  88. container_of(work, struct vivid_cec_work, work.work);
  89. struct vivid_dev *dev = cw->dev;
  90. spin_lock(&dev->cec_slock);
  91. if (dev->cec_xfer_time_jiffies) {
  92. list_del(&cw->list);
  93. spin_unlock(&dev->cec_slock);
  94. cec_transmit_done(cw->adap, CEC_TX_STATUS_ARB_LOST, 1, 0, 0, 0);
  95. kfree(cw);
  96. } else {
  97. INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_done_worker);
  98. dev->cec_xfer_start_jiffies = jiffies;
  99. dev->cec_xfer_time_jiffies = usecs_to_jiffies(cw->usecs);
  100. spin_unlock(&dev->cec_slock);
  101. schedule_delayed_work(&cw->work, dev->cec_xfer_time_jiffies);
  102. }
  103. }
  104. static int vivid_cec_adap_enable(struct cec_adapter *adap, bool enable)
  105. {
  106. return 0;
  107. }
  108. static int vivid_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  109. {
  110. return 0;
  111. }
  112. /*
  113. * One data bit takes 2400 us, each byte needs 10 bits so that's 24000 us
  114. * per byte.
  115. */
  116. #define USECS_PER_BYTE 24000
  117. static int vivid_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  118. u32 signal_free_time, struct cec_msg *msg)
  119. {
  120. struct vivid_dev *dev = adap->priv;
  121. struct vivid_cec_work *cw = kzalloc(sizeof(*cw), GFP_KERNEL);
  122. long delta_jiffies = 0;
  123. if (cw == NULL)
  124. return -ENOMEM;
  125. cw->dev = dev;
  126. cw->adap = adap;
  127. cw->usecs = CEC_FREE_TIME_TO_USEC(signal_free_time) +
  128. msg->len * USECS_PER_BYTE;
  129. cw->msg = *msg;
  130. spin_lock(&dev->cec_slock);
  131. list_add(&cw->list, &dev->cec_work_list);
  132. if (dev->cec_xfer_time_jiffies == 0) {
  133. INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_done_worker);
  134. dev->cec_xfer_start_jiffies = jiffies;
  135. dev->cec_xfer_time_jiffies = usecs_to_jiffies(cw->usecs);
  136. delta_jiffies = dev->cec_xfer_time_jiffies;
  137. } else {
  138. INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_try_worker);
  139. delta_jiffies = dev->cec_xfer_start_jiffies +
  140. dev->cec_xfer_time_jiffies - jiffies;
  141. }
  142. spin_unlock(&dev->cec_slock);
  143. schedule_delayed_work(&cw->work, delta_jiffies < 0 ? 0 : delta_jiffies);
  144. return 0;
  145. }
  146. static int vivid_received(struct cec_adapter *adap, struct cec_msg *msg)
  147. {
  148. struct vivid_dev *dev = adap->priv;
  149. struct cec_msg reply;
  150. u8 dest = cec_msg_destination(msg);
  151. u8 disp_ctl;
  152. char osd[14];
  153. if (cec_msg_is_broadcast(msg))
  154. dest = adap->log_addrs.log_addr[0];
  155. cec_msg_init(&reply, dest, cec_msg_initiator(msg));
  156. switch (cec_msg_opcode(msg)) {
  157. case CEC_MSG_SET_OSD_STRING:
  158. if (!cec_is_sink(adap))
  159. return -ENOMSG;
  160. cec_ops_set_osd_string(msg, &disp_ctl, osd);
  161. switch (disp_ctl) {
  162. case CEC_OP_DISP_CTL_DEFAULT:
  163. strcpy(dev->osd, osd);
  164. dev->osd_jiffies = jiffies;
  165. break;
  166. case CEC_OP_DISP_CTL_UNTIL_CLEARED:
  167. strcpy(dev->osd, osd);
  168. dev->osd_jiffies = 0;
  169. break;
  170. case CEC_OP_DISP_CTL_CLEAR:
  171. dev->osd[0] = 0;
  172. dev->osd_jiffies = 0;
  173. break;
  174. default:
  175. cec_msg_feature_abort(&reply, cec_msg_opcode(msg),
  176. CEC_OP_ABORT_INVALID_OP);
  177. cec_transmit_msg(adap, &reply, false);
  178. break;
  179. }
  180. break;
  181. default:
  182. return -ENOMSG;
  183. }
  184. return 0;
  185. }
  186. static const struct cec_adap_ops vivid_cec_adap_ops = {
  187. .adap_enable = vivid_cec_adap_enable,
  188. .adap_log_addr = vivid_cec_adap_log_addr,
  189. .adap_transmit = vivid_cec_adap_transmit,
  190. .received = vivid_received,
  191. };
  192. struct cec_adapter *vivid_cec_alloc_adap(struct vivid_dev *dev,
  193. unsigned int idx,
  194. bool is_source)
  195. {
  196. char name[sizeof(dev->vid_out_dev.name) + 2];
  197. u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
  198. CEC_CAP_PASSTHROUGH | CEC_CAP_RC | CEC_CAP_MONITOR_ALL;
  199. snprintf(name, sizeof(name), "%s%d",
  200. is_source ? dev->vid_out_dev.name : dev->vid_cap_dev.name,
  201. idx);
  202. return cec_allocate_adapter(&vivid_cec_adap_ops, dev,
  203. name, caps, 1);
  204. }