btfm_codec_btadv_interface.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 202333 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include "btfm_codec.h"
  6. #include "btfm_codec_pkt.h"
  7. #include "btfm_codec_btadv_interface.h"
  8. void btfmcodec_initiate_hwep_shutdown(struct btfmcodec_char_device *btfmcodec_dev)
  9. {
  10. wait_queue_head_t *rsp_wait_q =
  11. &btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_HWEP_SHUTDOWN];
  12. int ret;
  13. uint8_t *status = &btfmcodec_dev->status[BTM_PKT_TYPE_HWEP_SHUTDOWN];
  14. *status = BTM_WAITING_RSP;
  15. BTFMCODEC_INFO("queuing work to shutdown");
  16. schedule_work(&btfmcodec_dev->wq_hwep_shutdown);
  17. BTFMCODEC_INFO("waiting here for shutdown");
  18. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  19. *status != BTM_WAITING_RSP,
  20. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  21. /* Rethink of having a new packet to notify transport switch error */
  22. if (ret == 0) {
  23. BTFMCODEC_ERR("failed to recevie to complete hwep_shutdown");
  24. flush_work(&btfmcodec_dev->wq_hwep_shutdown);
  25. } else {
  26. if (*status == BTM_RSP_RECV) {
  27. BTFMCODEC_ERR("sucessfully closed hwep");
  28. return;
  29. } else if (*status == BTM_FAIL_RESP_RECV) {
  30. BTFMCODEC_ERR("Failed to close hwep");
  31. return;
  32. }
  33. }
  34. }
  35. void btfmcodec_move_to_next_state(struct btfmcodec_state_machine *state)
  36. {
  37. mutex_lock(&state->state_machine_lock);
  38. if (state->current_state == BT_Connecting ||
  39. state->current_state == BTADV_AUDIO_Connecting) {
  40. state->current_state += 1;
  41. BTFMCODEC_INFO("moving from %s to %s",
  42. coverttostring(state->current_state -1 ),
  43. coverttostring(state->current_state));
  44. } else {
  45. BTFMCODEC_ERR("State machine might have gone bad. reseting to default");
  46. state->current_state = IDLE;
  47. }
  48. state->prev_state = IDLE;
  49. mutex_unlock(&state->state_machine_lock);
  50. }
  51. void btfmcodec_revert_current_state(struct btfmcodec_state_machine *state)
  52. {
  53. mutex_lock(&state->state_machine_lock);
  54. BTFMCODEC_INFO("reverting from %s to %s", coverttostring(state->current_state),
  55. coverttostring(state->prev_state));
  56. state->current_state = state->prev_state;
  57. state->prev_state = IDLE;
  58. mutex_unlock(&state->state_machine_lock);
  59. }
  60. void btfmcodec_set_current_state(struct btfmcodec_state_machine *state,
  61. btfmcodec_state current_state)
  62. {
  63. mutex_lock(&state->state_machine_lock);
  64. BTFMCODEC_INFO("moving from %s to %s", coverttostring(state->current_state),
  65. coverttostring(current_state));
  66. state->prev_state = state->current_state;
  67. state->current_state = current_state;
  68. mutex_unlock(&state->state_machine_lock);
  69. }
  70. btfmcodec_state btfmcodec_get_current_transport(struct
  71. btfmcodec_state_machine *state)
  72. {
  73. btfmcodec_state current_state;
  74. mutex_lock(&state->state_machine_lock);
  75. current_state = state->current_state;
  76. mutex_unlock(&state->state_machine_lock);
  77. return current_state;
  78. }
  79. int btfmcodec_frame_transport_switch_ind_pkt(struct btfmcodec_char_device *btfmcodec_dev,
  80. uint8_t active_transport,
  81. uint8_t status)
  82. {
  83. struct btm_ctrl_pkt rsp;
  84. rsp.opcode = BTM_BTFMCODEC_TRANSPORT_SWITCH_FAILED_IND;
  85. rsp.len = BTM_PREPARE_AUDIO_BEARER_SWITCH_RSP_LEN;
  86. rsp.active_transport = active_transport;
  87. rsp.status = status;
  88. return btfmcodec_dev_enqueue_pkt(btfmcodec_dev, &rsp, (rsp.len +
  89. BTM_HEADER_LEN));
  90. }
  91. int btfmcodec_frame_prepare_bearer_rsp_pkt(struct btfmcodec_char_device *btfmcodec_dev,
  92. uint8_t active_transport,
  93. uint8_t status)
  94. {
  95. struct btm_ctrl_pkt rsp;
  96. rsp.opcode = BTM_BTFMCODEC_PREPARE_AUDIO_BEARER_SWITCH_RSP;
  97. rsp.len =BTM_PREPARE_AUDIO_BEARER_SWITCH_RSP_LEN;
  98. rsp.active_transport = active_transport;
  99. rsp.status = status;
  100. return btfmcodec_dev_enqueue_pkt(btfmcodec_dev, &rsp, (rsp.len +
  101. BTM_HEADER_LEN));
  102. }
  103. int btfmcodec_wait_for_bearer_ind(struct btfmcodec_char_device *btfmcodec_dev)
  104. {
  105. wait_queue_head_t *rsp_wait_q =
  106. &btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_BEARER_SWITCH_IND];
  107. int ret;
  108. uint8_t *status = &btfmcodec_dev->status[BTM_PKT_TYPE_BEARER_SWITCH_IND];
  109. *status = BTM_WAITING_RSP;
  110. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  111. *status != BTM_WAITING_RSP,
  112. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  113. if (ret == 0) {
  114. BTFMCODEC_ERR("failed to recevie BTM_BEARER_SWITCH_IND");
  115. ret = -MSG_INTERNAL_TIMEOUT;
  116. } else {
  117. if (*status == BTM_RSP_RECV) {
  118. ret = 0;
  119. } else if (*status == BTM_FAIL_RESP_RECV) {
  120. BTFMCODEC_ERR("Rx BTM_BEARER_SWITCH_IND with failure status");
  121. ret = -1;
  122. }
  123. }
  124. return ret;
  125. }
  126. int btfmcodec_initiate_hwep_configuration(struct btfmcodec_char_device *btfmcodec_dev)
  127. {
  128. wait_queue_head_t *rsp_wait_q =
  129. &btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_HWEP_CONFIG];
  130. uint8_t *status = &btfmcodec_dev->status[BTM_PKT_TYPE_HWEP_CONFIG];
  131. int ret;
  132. schedule_work(&btfmcodec_dev->wq_hwep_configure);
  133. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  134. *status != BTM_WAITING_RSP,
  135. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  136. if (ret == 0) {
  137. BTFMCODEC_ERR("failed to recevie complete hwep configure");
  138. flush_work(&btfmcodec_dev->wq_hwep_configure);
  139. ret = -1;
  140. } else {
  141. if (*status == BTM_RSP_RECV) {
  142. ret = 0;
  143. } else if (*status == BTM_FAIL_RESP_RECV) {
  144. BTFMCODEC_ERR("Failed to close hwep moving back to previous state");
  145. ret = -1;
  146. }
  147. }
  148. return ret;
  149. }
  150. void btfmcodec_configure_hwep(struct btfmcodec_char_device *btfmcodec_dev)
  151. {
  152. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  153. struct btfmcodec_state_machine *state = &btfmcodec->states;
  154. int ret;
  155. uint8_t status = MSG_SUCCESS;
  156. ret = btfmcodec_initiate_hwep_configuration(btfmcodec_dev);
  157. if (ret < 0) {
  158. status = MSG_FAILED_TO_CONFIGURE_HWEP;
  159. /* Move back to BTADV_AUDIO_Connected from BT_Connecting */
  160. btfmcodec_revert_current_state(state);
  161. }
  162. ret = btfmcodec_frame_transport_switch_ind_pkt(btfmcodec_dev,
  163. btfmcodec_get_current_transport(state), status);
  164. if (status != MSG_SUCCESS)
  165. return;
  166. if (ret < 0) {
  167. ret = btfmcodec_wait_for_bearer_ind(btfmcodec_dev);
  168. if (ret < 0) {
  169. /* Move back to BTADV_AUDIO_Connected for failure cases*/
  170. BTFMCODEC_ERR("moving back to previous state");
  171. btfmcodec_revert_current_state(state);
  172. /* close HWEP */
  173. btfmcodec_initiate_hwep_shutdown(btfmcodec_dev);
  174. if (ret == -MSG_INTERNAL_TIMEOUT) {
  175. btfmcodec_frame_transport_switch_ind_pkt(btfmcodec_dev, BT,
  176. MSG_INTERNAL_TIMEOUT);
  177. }
  178. } else {
  179. /* move from BT_Connecting to BT_Connected */
  180. btfmcodec_move_to_next_state(state);
  181. }
  182. } else {
  183. /* add code to handle packet errors */
  184. }
  185. }
  186. void btfmcodec_prepare_bearer(struct btfmcodec_char_device *btfmcodec_dev,
  187. enum transport_type new_transport)
  188. {
  189. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  190. struct btfmcodec_state_machine *state = &btfmcodec->states;
  191. btfmcodec_state current_state;
  192. int ret = -1;
  193. if (new_transport > (ARRAY_SIZE(transport_type_text))) {
  194. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  195. (uint8_t) btfmcodec_get_current_transport(state),
  196. MSG_WRONG_TRANSPORT_TYPE);
  197. return;
  198. }
  199. BTFMCODEC_INFO("Rx to switch from transport type %s to %s",
  200. coverttostring(btfmcodec_get_current_transport(state)),
  201. transport_type_text[new_transport - 1]);
  202. current_state = btfmcodec_get_current_transport(state);
  203. if (new_transport == BT) {
  204. /* If BT is already active. send +ve ack to BTADV Audio Manager */
  205. if (current_state == BT_Connected ||
  206. current_state == BT_Connecting) {
  207. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  208. (uint8_t)current_state, MSG_SUCCESS);
  209. return;
  210. } else if (current_state == BTADV_AUDIO_Connected ||
  211. current_state == BTADV_AUDIO_Connecting||
  212. current_state == IDLE) {
  213. if (btfmcodec_is_valid_cache_avb(btfmcodec)) {
  214. BTFMCODEC_INFO("detected BTADV audio Gaming usecase to BT usecase");
  215. btfmcodec_set_current_state(state, BT_Connecting);
  216. btfmcodec_configure_hwep(btfmcodec_dev);
  217. } else {
  218. if (current_state != IDLE)
  219. BTFMCODEC_INFO("detected BTADV Audio lossless to IDLE");
  220. BTFMCODEC_INFO("moving to IDLE as no config available");
  221. btfmcodec_set_current_state(state, IDLE);
  222. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  223. btfmcodec_get_current_transport(state),
  224. MSG_SUCCESS);
  225. /* No need wait for bearer switch indications as BTFMCODEC
  226. * driver doesn't have configs to configure
  227. */
  228. }
  229. }
  230. } else if(new_transport == BTADV) {
  231. /* If BTADV audio is already active. send +ve ack to BTADV audio Manager */
  232. if (current_state == BTADV_AUDIO_Connecting ||
  233. current_state == BTADV_AUDIO_Connected) {
  234. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  235. (uint8_t)current_state, MSG_SUCCESS);
  236. return;
  237. } else {
  238. btfmcodec_set_current_state(state, BTADV_AUDIO_Connecting);
  239. if (btfmcodec_is_valid_cache_avb(btfmcodec)) {
  240. BTFMCODEC_INFO("detected BT to BTADV audio Gaming usecase");
  241. } else {
  242. BTFMCODEC_INFO("detected IDLE to BTADV audio lossless usecase");
  243. }
  244. ret = btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  245. BTADV_AUDIO_Connecting, MSG_SUCCESS);
  246. if (ret < 0)
  247. return;
  248. /* Wait here to get Bearer switch indication */
  249. ret = btfmcodec_wait_for_bearer_ind(btfmcodec_dev);
  250. if (ret < 0) {
  251. BTFMCODEC_ERR("moving back to previous state");
  252. btfmcodec_revert_current_state(state);
  253. if (ret == -MSG_INTERNAL_TIMEOUT) {
  254. btfmcodec_frame_transport_switch_ind_pkt(
  255. btfmcodec_dev, BTADV,
  256. MSG_INTERNAL_TIMEOUT);
  257. }
  258. } else {
  259. btfmcodec_move_to_next_state(state);
  260. }
  261. if (ret < 0)
  262. return;
  263. if (btfmcodec_is_valid_cache_avb(btfmcodec)) {
  264. BTFMCODEC_INFO("Initiating BT port close...");
  265. btfmcodec_initiate_hwep_shutdown(btfmcodec_dev);
  266. }
  267. }
  268. }
  269. }
  270. void btfmcodec_wq_prepare_bearer(struct work_struct *work)
  271. {
  272. struct btfmcodec_char_device *btfmcodec_dev = container_of(work,
  273. struct btfmcodec_char_device,
  274. wq_prepare_bearer);
  275. int idx = BTM_PKT_TYPE_PREPARE_REQ;
  276. BTFMCODEC_INFO(": with new transport:%d", btfmcodec_dev->status[idx]);
  277. btfmcodec_prepare_bearer(btfmcodec_dev, btfmcodec_dev->status[idx]);
  278. }