btfm_codec_btadv_interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. *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. *status = BTM_WAITING_RSP;
  111. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  112. *status != BTM_WAITING_RSP,
  113. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  114. if (ret == 0) {
  115. BTFMCODEC_ERR("failed to recevie BTM_BEARER_SWITCH_IND");
  116. ret = -MSG_INTERNAL_TIMEOUT;
  117. } else {
  118. if (*status == BTM_RSP_RECV) {
  119. ret = 0;
  120. } else if (*status == BTM_FAIL_RESP_RECV) {
  121. BTFMCODEC_ERR("Rx BTM_BEARER_SWITCH_IND with failure status");
  122. ret = -1;
  123. } else if (*status == BTM_RSP_NOT_RECV_CLIENT_KILLED) {
  124. BTFMCODEC_ERR("client killed so moving further");
  125. ret = -1;
  126. }
  127. }
  128. return ret;
  129. }
  130. int btfmcodec_initiate_hwep_configuration(struct btfmcodec_char_device *btfmcodec_dev)
  131. {
  132. wait_queue_head_t *rsp_wait_q =
  133. &btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_HWEP_CONFIG];
  134. uint8_t *status = &btfmcodec_dev->status[BTM_PKT_TYPE_HWEP_CONFIG];
  135. int ret;
  136. schedule_work(&btfmcodec_dev->wq_hwep_configure);
  137. *status = BTM_WAITING_RSP;
  138. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  139. *status != BTM_WAITING_RSP,
  140. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  141. if (ret == 0) {
  142. BTFMCODEC_ERR("failed to recevie complete hwep configure");
  143. flush_work(&btfmcodec_dev->wq_hwep_configure);
  144. ret = -1;
  145. } else {
  146. if (*status == BTM_RSP_RECV) {
  147. ret = 0;
  148. } else if (*status == BTM_FAIL_RESP_RECV ||
  149. *status == BTM_RSP_NOT_RECV_CLIENT_KILLED) {
  150. BTFMCODEC_ERR("Failed to close hwep moving back to previous state");
  151. ret = -1;
  152. }
  153. }
  154. return ret;
  155. }
  156. void btfmcodec_configure_hwep(struct btfmcodec_char_device *btfmcodec_dev)
  157. {
  158. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  159. struct btfmcodec_state_machine *state = &btfmcodec->states;
  160. int ret;
  161. uint8_t status = MSG_SUCCESS;
  162. ret = btfmcodec_initiate_hwep_configuration(btfmcodec_dev);
  163. if (ret < 0) {
  164. status = MSG_FAILED_TO_CONFIGURE_HWEP;
  165. /* Move back to BTADV_AUDIO_Connected from BT_Connecting */
  166. btfmcodec_revert_current_state(state);
  167. }
  168. ret = btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  169. btfmcodec_get_current_transport(state), status);
  170. if (status != MSG_SUCCESS)
  171. return;
  172. if (ret < 0) {
  173. ret = btfmcodec_wait_for_bearer_ind(btfmcodec_dev);
  174. if (ret < 0) {
  175. /* Move back to BTADV_AUDIO_Connected for failure cases*/
  176. BTFMCODEC_ERR("moving back to previous state");
  177. btfmcodec_revert_current_state(state);
  178. /* close HWEP */
  179. btfmcodec_initiate_hwep_shutdown(btfmcodec_dev);
  180. if (ret == -MSG_INTERNAL_TIMEOUT) {
  181. btfmcodec_frame_transport_switch_ind_pkt(btfmcodec_dev, BT,
  182. MSG_INTERNAL_TIMEOUT);
  183. }
  184. } else {
  185. /* move from BT_Connecting to BT_Connected */
  186. btfmcodec_move_to_next_state(state);
  187. }
  188. } else {
  189. /* add code to handle packet errors */
  190. }
  191. }
  192. void btfmcodec_prepare_bearer(struct btfmcodec_char_device *btfmcodec_dev,
  193. enum transport_type new_transport)
  194. {
  195. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  196. struct btfmcodec_state_machine *state = &btfmcodec->states;
  197. btfmcodec_state current_state;
  198. int ret = -1;
  199. if (new_transport > (ARRAY_SIZE(transport_type_text))) {
  200. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  201. (uint8_t) btfmcodec_get_current_transport(state),
  202. MSG_WRONG_TRANSPORT_TYPE);
  203. return;
  204. }
  205. BTFMCODEC_INFO("Rx to switch from transport type %s to %s",
  206. coverttostring(btfmcodec_get_current_transport(state)),
  207. transport_type_text[new_transport - 1]);
  208. current_state = btfmcodec_get_current_transport(state);
  209. if (new_transport == BT) {
  210. /* If BT is already active. send +ve ack to BTADV Audio Manager */
  211. if (current_state == BT_Connected ||
  212. current_state == BT_Connecting) {
  213. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  214. (uint8_t)current_state, MSG_SUCCESS);
  215. return;
  216. } else if (current_state == BTADV_AUDIO_Connected ||
  217. current_state == BTADV_AUDIO_Connecting||
  218. current_state == IDLE) {
  219. if (btfmcodec_is_valid_cache_avb(btfmcodec)) {
  220. BTFMCODEC_INFO("detected BTADV audio Gaming usecase to BT usecase");
  221. btfmcodec_set_current_state(state, BT_Connecting);
  222. btfmcodec_configure_hwep(btfmcodec_dev);
  223. } else {
  224. if (current_state != IDLE)
  225. BTFMCODEC_INFO("detected BTADV Audio lossless to IDLE");
  226. BTFMCODEC_INFO("moving to IDLE as no config available");
  227. btfmcodec_set_current_state(state, IDLE);
  228. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  229. btfmcodec_get_current_transport(state),
  230. MSG_SUCCESS);
  231. /* No need wait for bearer switch indications as BTFMCODEC
  232. * driver doesn't have configs to configure
  233. */
  234. }
  235. }
  236. } else if(new_transport == BTADV) {
  237. /* If BTADV audio is already active. send +ve ack to BTADV audio Manager */
  238. if (current_state == BTADV_AUDIO_Connecting ||
  239. current_state == BTADV_AUDIO_Connected) {
  240. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  241. (uint8_t)current_state, MSG_SUCCESS);
  242. return;
  243. } else {
  244. btfmcodec_set_current_state(state, BTADV_AUDIO_Connecting);
  245. if (btfmcodec_is_valid_cache_avb(btfmcodec)) {
  246. BTFMCODEC_INFO("detected BT to BTADV audio Gaming usecase");
  247. } else {
  248. BTFMCODEC_INFO("detected IDLE to BTADV audio lossless usecase");
  249. }
  250. ret = btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev,
  251. BTADV_AUDIO_Connecting, MSG_SUCCESS);
  252. if (ret < 0)
  253. return;
  254. /* Wait here to get Bearer switch indication */
  255. ret = btfmcodec_wait_for_bearer_ind(btfmcodec_dev);
  256. if (ret < 0) {
  257. BTFMCODEC_ERR("moving back to previous state");
  258. btfmcodec_revert_current_state(state);
  259. if (ret == -MSG_INTERNAL_TIMEOUT) {
  260. btfmcodec_frame_transport_switch_ind_pkt(
  261. btfmcodec_dev, BTADV,
  262. MSG_INTERNAL_TIMEOUT);
  263. }
  264. } else {
  265. btfmcodec_move_to_next_state(state);
  266. }
  267. if (ret < 0)
  268. return;
  269. if (btfmcodec_is_valid_cache_avb(btfmcodec)) {
  270. BTFMCODEC_INFO("Initiating BT port close...");
  271. btfmcodec_initiate_hwep_shutdown(btfmcodec_dev);
  272. }
  273. }
  274. } else if (new_transport == NONE) {
  275. /* Let ALSA handles the transport close for BT */
  276. if (current_state != BT_Connecting && current_state != BT_Connected)
  277. btfmcodec_set_current_state(state, IDLE);
  278. btfmcodec_frame_prepare_bearer_rsp_pkt(btfmcodec_dev, (uint8_t)current_state,
  279. MSG_SUCCESS);
  280. return;
  281. }
  282. }
  283. void btfmcodec_wq_prepare_bearer(struct work_struct *work)
  284. {
  285. struct btfmcodec_char_device *btfmcodec_dev = container_of(work,
  286. struct btfmcodec_char_device,
  287. wq_prepare_bearer);
  288. int idx = BTM_PKT_TYPE_PREPARE_REQ;
  289. BTFMCODEC_INFO("with new transport:%d", btfmcodec_dev->status[idx]);
  290. btfmcodec_prepare_bearer(btfmcodec_dev, btfmcodec_dev->status[idx]);
  291. }