btfm_codec_btadv_interface.c 10 KB

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