btfm_codec_interface.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include "btfm_codec.h"
  7. #include "btfm_codec_interface.h"
  8. #include "btfm_codec_pkt.h"
  9. #include "btfm_codec_btadv_interface.h"
  10. static struct snd_soc_dai_driver *btfmcodec_dai_info;
  11. uint32_t bits_per_second;
  12. static int btfm_codec_get_mixer_control(struct snd_kcontrol *kcontrol,
  13. struct snd_ctl_elem_value *ucontrol)
  14. {
  15. struct snd_soc_component *codec = kcontrol->private_data;
  16. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  17. struct hwep_data *hwepinfo = btfmcodec->hwep_info;
  18. struct btfmcodec_state_machine states = btfmcodec->states;
  19. struct snd_kcontrol_new *mixer_ctrl = hwepinfo->mixer_ctrl;
  20. struct snd_ctl_elem_id id = kcontrol->id;
  21. int num_mixer_ctrl = hwepinfo->num_mixer_ctrl;
  22. int i = 0;
  23. BTFMCODEC_DBG("");
  24. if (states.current_state != IDLE) {
  25. BTFMCODEC_WARN("Received probe when state is :%s",
  26. coverttostring(states.current_state));
  27. } else {
  28. for (; i < num_mixer_ctrl ; i++) {
  29. BTFMCODEC_DBG("checking mixer_ctrl:%s and current mixer:%s",
  30. id.name, mixer_ctrl[i].name);
  31. if (!strncmp(id.name, mixer_ctrl[i].name, 64)) {
  32. BTFMCODEC_DBG("Matched");
  33. mixer_ctrl[i].get(kcontrol, ucontrol);
  34. break;
  35. }
  36. }
  37. if (num_mixer_ctrl == i)
  38. return 0;
  39. }
  40. return 1;
  41. }
  42. static int btfmcodec_put_mixer_control(struct snd_kcontrol *kcontrol,
  43. struct snd_ctl_elem_value *ucontrol)
  44. {
  45. struct snd_soc_component *codec = kcontrol->private_data;
  46. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  47. struct hwep_data *hwepinfo = btfmcodec->hwep_info;
  48. struct btfmcodec_state_machine states = btfmcodec->states;
  49. struct snd_kcontrol_new *mixer_ctrl = hwepinfo->mixer_ctrl;
  50. struct snd_ctl_elem_id id = kcontrol->id;
  51. int num_mixer_ctrl = hwepinfo->num_mixer_ctrl;
  52. int i = 0;
  53. BTFMCODEC_DBG("");
  54. if (states.current_state != IDLE) {
  55. BTFMCODEC_WARN("Received probe when state is :%s",
  56. coverttostring(states.current_state));
  57. } else {
  58. for (; i < num_mixer_ctrl ; i++) {
  59. BTFMCODEC_DBG("checking mixer_ctrl:%s and current mixer:%s",
  60. id.name, mixer_ctrl[i].name);
  61. if (!strncmp(id.name, mixer_ctrl[i].name, 64)) {
  62. BTFMCODEC_DBG("Matched");
  63. mixer_ctrl[i].put(kcontrol, ucontrol);
  64. break;
  65. }
  66. }
  67. if (num_mixer_ctrl == i)
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. static int btfmcodec_codec_probe(struct snd_soc_component *codec)
  73. {
  74. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  75. struct btfmcodec_state_machine *state = &btfmcodec->states;
  76. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  77. int num_mixer_ctrl = hwep_info->num_mixer_ctrl;
  78. BTFMCODEC_DBG("");
  79. // ToDo: check weather probe has to allowed when state if different
  80. if (btfmcodec_get_current_transport(state)!= IDLE) {
  81. BTFMCODEC_WARN("Received probe when state is :%s",
  82. coverttostring(btfmcodec_get_current_transport(state)));
  83. } else if (hwep_info->drv && hwep_info->drv->hwep_probe) {
  84. hwep_info->drv->hwep_probe(codec);
  85. /* Register mixer control */
  86. if (hwep_info->mixer_ctrl && num_mixer_ctrl >= 1) {
  87. struct snd_kcontrol_new *mixer_ctrl;
  88. int i = 0;
  89. mixer_ctrl = (struct snd_kcontrol_new *)
  90. kzalloc(num_mixer_ctrl *
  91. sizeof(struct snd_kcontrol_new), GFP_KERNEL);
  92. if (!mixer_ctrl) {
  93. BTFMCODEC_ERR("failed to register mixer controls");
  94. goto end;
  95. }
  96. BTFMCODEC_INFO("Registering %d mixer controls", num_mixer_ctrl);
  97. memcpy(mixer_ctrl, hwep_info->mixer_ctrl, num_mixer_ctrl * sizeof(struct snd_kcontrol_new));
  98. for (; i< num_mixer_ctrl; i++) {
  99. BTFMCODEC_INFO("name of control:%s", mixer_ctrl[i].name);
  100. mixer_ctrl[i].get = btfm_codec_get_mixer_control;
  101. mixer_ctrl[i].put = btfmcodec_put_mixer_control;
  102. }
  103. snd_soc_add_component_controls(codec, mixer_ctrl, num_mixer_ctrl);
  104. BTFMCODEC_INFO("CODEC address while registering mixer ctrl:%p", codec);
  105. }
  106. }
  107. end:
  108. // ToDo to add mixer control.
  109. return 0;
  110. }
  111. static void btfmcodec_codec_remove(struct snd_soc_component *codec)
  112. {
  113. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  114. struct btfmcodec_state_machine *state = &btfmcodec->states;
  115. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  116. BTFMCODEC_DBG("");
  117. // ToDo: check whether remove has to allowed when state if different
  118. if (btfmcodec_get_current_transport(state)!= IDLE) {
  119. BTFMCODEC_WARN("Received probe when state is :%s",
  120. coverttostring(btfmcodec_get_current_transport(state)));
  121. } else if (hwep_info->drv && hwep_info->drv->hwep_remove) {
  122. hwep_info->drv->hwep_remove(codec);
  123. }
  124. }
  125. static int btfmcodec_codec_write(struct snd_soc_component *codec,
  126. unsigned int reg, unsigned int value)
  127. {
  128. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  129. struct btfmcodec_state_machine *state = &btfmcodec->states;
  130. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  131. BTFMCODEC_DBG("");
  132. // ToDo: check whether write has to allowed when state if different
  133. if (btfmcodec_get_current_transport(state)!= IDLE) {
  134. BTFMCODEC_WARN("Received probe when state is :%s",
  135. coverttostring(btfmcodec_get_current_transport(state)));
  136. } else if (hwep_info->drv && hwep_info->drv->hwep_remove) {
  137. return hwep_info->drv->hwep_write(codec, reg, value);
  138. }
  139. return 0;
  140. }
  141. static unsigned int btfmcodec_codec_read(struct snd_soc_component *codec,
  142. unsigned int reg)
  143. {
  144. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  145. struct btfmcodec_state_machine *state = &btfmcodec->states;
  146. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  147. BTFMCODEC_DBG("");
  148. // ToDo: check whether read has to allowed when state if different
  149. if (btfmcodec_get_current_transport(state)!= IDLE) {
  150. BTFMCODEC_WARN("Received probe when state is :%s",
  151. coverttostring(btfmcodec_get_current_transport(state)));
  152. } else if (hwep_info->drv && hwep_info->drv->hwep_read) {
  153. return hwep_info->drv->hwep_read(codec, reg);
  154. }
  155. return 0;
  156. }
  157. static const struct snd_soc_component_driver btfmcodec_codec_component = {
  158. .probe = btfmcodec_codec_probe,
  159. .remove = btfmcodec_codec_remove,
  160. .read = btfmcodec_codec_read,
  161. .write = btfmcodec_codec_write,
  162. };
  163. static inline void * btfmcodec_get_dai_drvdata(struct hwep_data *hwep_info)
  164. {
  165. if (!hwep_info || !hwep_info->dai_drv) return NULL;
  166. return hwep_info->dai_drv;
  167. }
  168. int btfmcodec_hwep_startup(struct btfmcodec_data *btfmcodec)
  169. {
  170. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  171. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  172. btfmcodec_get_dai_drvdata(hwep_info);
  173. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_startup) {
  174. return dai_drv->dai_ops->hwep_startup((void *)btfmcodec->hwep_info);
  175. } else {
  176. return -1;
  177. }
  178. }
  179. static int btfmcodec_dai_startup(struct snd_pcm_substream *substream,
  180. struct snd_soc_dai *dai)
  181. {
  182. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  183. struct btfmcodec_state_machine *state = &btfmcodec->states;
  184. BTFMCODEC_DBG("substream = %s stream = %d dai->name = %s",
  185. substream->name, substream->stream, dai->name);
  186. if (btfmcodec_get_current_transport(state) != IDLE &&
  187. btfmcodec_get_current_transport(state) != BT_Connected) {
  188. BTFMCODEC_DBG("Not allowing as state is:%s",
  189. coverttostring(btfmcodec_get_current_transport(state)));
  190. } else {
  191. return btfmcodec_hwep_startup(btfmcodec);
  192. }
  193. return 0;
  194. }
  195. int btfmcodec_hwep_shutdown(struct btfmcodec_data *btfmcodec, int id)
  196. {
  197. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  198. struct btfmcodec_char_device *btfmcodec_dev = btfmcodec->btfmcodec_dev;
  199. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  200. btfmcodec_get_dai_drvdata(hwep_info);
  201. struct btfmcodec_state_machine *state = &btfmcodec->states;
  202. struct btm_master_shutdown_req shutdown_req;
  203. wait_queue_head_t *rsp_wait_q =
  204. &btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_MASTER_SHUTDOWN_RSP];
  205. uint8_t *status = &btfmcodec_dev->status[BTM_PKT_TYPE_MASTER_SHUTDOWN_RSP];
  206. int ret = 0;
  207. /* for master configurations failure cases, we don't need to send
  208. * shutdown request
  209. */
  210. if (btfmcodec_get_current_transport(state) == BT_Connected) {
  211. BTFMCODEC_DBG("sending master shutdown request..");
  212. shutdown_req.opcode = BTM_BTFMCODEC_MASTER_SHUTDOWN_REQ;
  213. shutdown_req.len = BTM_MASTER_SHUTDOWN_REQ_LEN;
  214. shutdown_req.stream_id = id;
  215. /* See if we need to protect below with lock */
  216. *status = BTM_WAITING_RSP;
  217. btfmcodec_dev_enqueue_pkt(btfmcodec_dev, &shutdown_req, (shutdown_req.len +
  218. BTM_HEADER_LEN));
  219. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  220. (*status) != BTM_WAITING_RSP,
  221. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  222. if (ret == 0) {
  223. BTFMCODEC_ERR("failed to recevie response from BTADV audio Manager");
  224. } else {
  225. if (*status == BTM_RSP_RECV)
  226. ret = 0;
  227. else if (*status == BTM_FAIL_RESP_RECV ||
  228. *status == BTM_RSP_NOT_RECV_CLIENT_KILLED)
  229. ret = -1;
  230. }
  231. } else {
  232. BTFMCODEC_WARN("Not sending master shutdown request as state is:%s",
  233. coverttostring(btfmcodec_get_current_transport(state)));
  234. }
  235. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_shutdown) {
  236. dai_drv->dai_ops->hwep_shutdown((void *)btfmcodec->hwep_info, id);
  237. }
  238. return ret;
  239. }
  240. void btfmcodec_wq_hwep_shutdown(struct work_struct *work)
  241. {
  242. struct btfmcodec_char_device *btfmcodec_dev = container_of(work,
  243. struct btfmcodec_char_device,
  244. wq_hwep_shutdown);
  245. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  246. struct list_head *head = &btfmcodec->config_head;
  247. struct hwep_configurations *hwep_configs = NULL;
  248. int ret = -1;
  249. int idx = BTM_PKT_TYPE_HWEP_SHUTDOWN;
  250. BTFMCODEC_INFO(" starting shutdown");
  251. /* Just check if first Rx has to be closed first or
  252. * any order should be ok.
  253. */
  254. list_for_each_entry(hwep_configs, head, dai_list) {
  255. BTFMCODEC_INFO("shuting down dai id:%d", hwep_configs->stream_id);
  256. ret = btfmcodec_hwep_shutdown(btfmcodec, hwep_configs->stream_id);
  257. if (ret < 0) {
  258. BTFMCODEC_ERR("failed to shutdown master with id", hwep_configs->stream_id);
  259. break;
  260. }
  261. }
  262. if (ret < 0)
  263. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  264. else
  265. btfmcodec_dev->status[idx] = BTM_RSP_RECV;
  266. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  267. }
  268. static int btfmcodec_delete_configs(struct btfmcodec_data *btfmcodec, uint8_t id)
  269. {
  270. struct list_head *head = &btfmcodec->config_head;
  271. struct hwep_configurations *hwep_configs;
  272. int ret = -1;
  273. list_for_each_entry(hwep_configs, head, dai_list) {
  274. if (hwep_configs->stream_id == id) {
  275. BTFMCODEC_INFO("deleting configs with id %d", id);
  276. list_del(&hwep_configs->dai_list);
  277. ret = 1;
  278. break;
  279. }
  280. }
  281. return ret;
  282. }
  283. static void btfmcodec_dai_shutdown(struct snd_pcm_substream *substream,
  284. struct snd_soc_dai *dai)
  285. {
  286. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  287. struct btfmcodec_state_machine *state = &btfmcodec->states;
  288. BTFMCODEC_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
  289. dai->id, dai->rate);
  290. if (btfmcodec_get_current_transport(state) != IDLE &&
  291. btfmcodec_get_current_transport(state) != BT_Connected) {
  292. BTFMCODEC_WARN("not allowing shutdown as state is:%s",
  293. coverttostring(btfmcodec_get_current_transport(state)));
  294. /* Delete stored configs */
  295. btfmcodec_delete_configs(btfmcodec, dai->id);
  296. } else {
  297. /* first master shutdown has to done */
  298. btfmcodec_hwep_shutdown(btfmcodec, dai->id);
  299. btfmcodec_delete_configs(btfmcodec, dai->id);
  300. if (!btfmcodec_is_valid_cache_avb(btfmcodec))
  301. btfmcodec_set_current_state(state, IDLE);
  302. else {
  303. BTFMCODEC_WARN("valid stream id is available not updating state\n");
  304. btfmcodec_set_current_state(state, BT_Connected);
  305. }
  306. }
  307. }
  308. int btfmcodec_hwep_hw_params (struct btfmcodec_data *btfmcodec, uint32_t bps,
  309. uint32_t direction)
  310. {
  311. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  312. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  313. btfmcodec_get_dai_drvdata(hwep_info);
  314. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_hw_params) {
  315. return dai_drv->dai_ops->hwep_hw_params((void *)btfmcodec->hwep_info,
  316. bps, direction);
  317. } else {
  318. return -1;
  319. }
  320. }
  321. static int btfmcodec_dai_hw_params(struct snd_pcm_substream *substream,
  322. struct snd_pcm_hw_params *params,
  323. struct snd_soc_dai *dai)
  324. {
  325. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  326. struct btfmcodec_state_machine *state = &btfmcodec->states;
  327. uint32_t bps = params_width(params);
  328. uint32_t direction = substream->stream;
  329. BTFMCODEC_DBG("dai->name = %s DAI-ID %x rate %d bps %d num_ch %d",
  330. dai->name, dai->id, params_rate(params), params_width(params),
  331. params_channels(params));
  332. if (btfmcodec_get_current_transport(state) != IDLE &&
  333. btfmcodec_get_current_transport(state) != BT_Connected) {
  334. BTFMCODEC_WARN("caching bps as state is :%s",
  335. coverttostring(btfmcodec_get_current_transport(state)));
  336. bits_per_second = bps;
  337. } else {
  338. return btfmcodec_hwep_hw_params(btfmcodec, bps, direction);
  339. }
  340. return 0;
  341. }
  342. bool btfmcodec_is_valid_cache_avb(struct btfmcodec_data *btfmcodec)
  343. {
  344. struct list_head *head = &btfmcodec->config_head;
  345. struct hwep_configurations *hwep_configs;
  346. bool cache_avb = false;
  347. list_for_each_entry(hwep_configs, head, dai_list) {
  348. cache_avb = true;
  349. break;
  350. }
  351. return cache_avb;
  352. }
  353. static int btfmcodec_check_and_cache_configs(struct btfmcodec_data *btfmcodec,
  354. uint32_t sampling_rate, uint32_t direction,
  355. int id, uint8_t codectype)
  356. {
  357. struct list_head *head = &btfmcodec->config_head;
  358. struct hwep_configurations *hwep_configs;
  359. list_for_each_entry(hwep_configs, head, dai_list) {
  360. if (hwep_configs->stream_id == id) {
  361. BTFMCODEC_WARN("previous entry for %d is already available",
  362. id);
  363. list_del(&hwep_configs->dai_list);
  364. }
  365. }
  366. hwep_configs = kzalloc(sizeof(struct hwep_configurations),
  367. GFP_KERNEL);
  368. if (!hwep_configs) {
  369. BTFMCODEC_ERR("failed to allocate memory");
  370. return -ENOMEM;
  371. }
  372. hwep_configs->stream_id = id; /* Stream identifier */
  373. hwep_configs->sample_rate = sampling_rate;
  374. hwep_configs->bit_width = bits_per_second;
  375. hwep_configs->codectype = codectype;
  376. hwep_configs->direction = direction;
  377. list_add(&hwep_configs->dai_list, head);
  378. BTFMCODEC_INFO("added dai id:%d to list with sampling_rate :%u, direction:%u", id, sampling_rate, direction);
  379. return 1;
  380. }
  381. static int btfmcodec_configure_master(struct btfmcodec_data *btfmcodec, uint8_t id)
  382. {
  383. struct btfmcodec_char_device *btfmcodec_dev = btfmcodec->btfmcodec_dev;
  384. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  385. struct master_hwep_configurations hwep_configs;
  386. struct btm_master_config_req config_reg;
  387. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  388. btfmcodec_get_dai_drvdata(hwep_info);
  389. wait_queue_head_t *rsp_wait_q =
  390. &btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_MASTER_CONFIG_RSP];
  391. uint8_t *status = &btfmcodec_dev->status[BTM_PKT_TYPE_MASTER_CONFIG_RSP];
  392. int ret = 0;
  393. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_get_configs) {
  394. dai_drv->dai_ops->hwep_get_configs((void *)btfmcodec->hwep_info,
  395. &hwep_configs, id);
  396. } else {
  397. BTFMCODEC_ERR("No hwep_get_configs is set by hw ep driver");
  398. return -1;
  399. }
  400. BTFMCODEC_INFO("framing packet for %d", id);
  401. config_reg.opcode = BTM_BTFMCODEC_MASTER_CONFIG_REQ;
  402. config_reg.len = BTM_MASTER_CONFIG_REQ_LEN;
  403. config_reg.stream_id = hwep_configs.stream_id;
  404. config_reg.device_id = hwep_configs.device_id;
  405. config_reg.sample_rate = hwep_configs.sample_rate;
  406. config_reg.bit_width = hwep_configs.bit_width;
  407. config_reg.num_channels = hwep_configs.num_channels;
  408. config_reg.channel_num = hwep_configs.chan_num;
  409. config_reg.codec_id = hwep_configs.codectype;
  410. BTFMCODEC_DBG("================================================\n");
  411. BTFMCODEC_DBG("config_reg.len :%d", config_reg.len);
  412. BTFMCODEC_DBG("config_reg.stream_id :%d", config_reg.stream_id);
  413. BTFMCODEC_DBG("config_reg.device_id :%d", config_reg.device_id);
  414. BTFMCODEC_DBG("config_reg.sample_rate :%d", config_reg.sample_rate);
  415. BTFMCODEC_DBG("config_reg.bit_width :%d", config_reg.bit_width);
  416. BTFMCODEC_DBG("config_reg.num_channels :%d", config_reg.num_channels);
  417. BTFMCODEC_DBG("config_reg.channel_num :%d", config_reg.channel_num);
  418. BTFMCODEC_DBG("config_reg.codec_id :%d", config_reg.codec_id);
  419. BTFMCODEC_DBG("================================================\n");
  420. /* See if we need to protect below with lock */
  421. *status = BTM_WAITING_RSP;
  422. btfmcodec_dev_enqueue_pkt(btfmcodec_dev, &config_reg, (config_reg.len +
  423. BTM_HEADER_LEN));
  424. ret = wait_event_interruptible_timeout(*rsp_wait_q,
  425. (*status) != BTM_WAITING_RSP,
  426. msecs_to_jiffies(BTM_MASTER_CONFIG_RSP_TIMEOUT));
  427. if (ret == 0) {
  428. BTFMCODEC_ERR("failed to recevie response from BTADV audio Manager");
  429. ret = -ETIMEDOUT;
  430. } else {
  431. if (*status == BTM_RSP_RECV)
  432. return 0;
  433. else if (*status == BTM_FAIL_RESP_RECV ||
  434. *status == BTM_RSP_NOT_RECV_CLIENT_KILLED)
  435. return -1;
  436. }
  437. return ret;
  438. }
  439. int btfmcodec_hwep_prepare(struct btfmcodec_data *btfmcodec, uint32_t sampling_rate,
  440. uint32_t direction, int id)
  441. {
  442. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  443. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  444. btfmcodec_get_dai_drvdata(hwep_info);
  445. struct btfmcodec_state_machine *state = &btfmcodec->states;
  446. int ret;
  447. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_prepare) {
  448. ret = dai_drv->dai_ops->hwep_prepare((void *)hwep_info, sampling_rate,
  449. direction, id);
  450. if (ret == 0 && test_bit(BTADV_AUDIO_MASTER_CONFIG, &hwep_info->flags)) {
  451. ret = btfmcodec_configure_master(btfmcodec, (uint8_t)id);
  452. if (ret < 0) {
  453. BTFMCODEC_ERR("failed to configure master error %d", ret);
  454. /* close slave port and reset the state*/
  455. btfmcodec_set_current_state(state, IDLE);
  456. /* we don't need to do shutdown, ASOC is doing it */
  457. // btfmcodec_hwep_shutdown(btfmcodec, id);
  458. } else {
  459. btfmcodec_set_current_state(state, BT_Connected);
  460. }
  461. }
  462. } else {
  463. return -1;
  464. }
  465. return ret;
  466. }
  467. static int btfmcodec_dai_prepare(struct snd_pcm_substream *substream,
  468. struct snd_soc_dai *dai)
  469. {
  470. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  471. struct btfmcodec_state_machine *state = &btfmcodec->states;
  472. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  473. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  474. btfmcodec_get_dai_drvdata(hwep_info);
  475. uint8_t *codectype = dai_drv->dai_ops->hwep_codectype;
  476. uint32_t sampling_rate = dai->rate;
  477. uint32_t direction = substream->stream;
  478. int id = dai->id;
  479. int ret;
  480. BTFMCODEC_INFO("dai->name: %s, dai->id: %d, dai->rate: %d direction: %d",
  481. dai->name, id, sampling_rate, direction);
  482. if (btfmcodec_get_current_transport(state) != IDLE &&
  483. btfmcodec_get_current_transport(state) != BT_Connected) {
  484. BTFMCODEC_WARN("caching required info as state is:%s",
  485. coverttostring(btfmcodec_get_current_transport(state)));
  486. ret = btfmcodec_check_and_cache_configs(btfmcodec, sampling_rate, direction,
  487. id, *codectype);
  488. } else {
  489. ret = btfmcodec_hwep_prepare(btfmcodec, sampling_rate, direction, id);
  490. if (ret >= 0) {
  491. btfmcodec_check_and_cache_configs(btfmcodec, sampling_rate, direction,
  492. id, *codectype);
  493. }
  494. }
  495. return ret;
  496. }
  497. int btfmcodec_hwep_set_channel_map(void *hwep_info, unsigned int tx_num,
  498. unsigned int *tx_slot, unsigned int rx_num,
  499. unsigned int *rx_slot)
  500. {
  501. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  502. btfmcodec_get_dai_drvdata(hwep_info);
  503. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_set_channel_map) {
  504. return dai_drv->dai_ops->hwep_set_channel_map(hwep_info, tx_num,
  505. tx_slot, rx_num,
  506. rx_slot);
  507. } else {
  508. return -1;
  509. }
  510. }
  511. static int btfmcodec_dai_set_channel_map(struct snd_soc_dai *dai,
  512. unsigned int tx_num, unsigned int *tx_slot,
  513. unsigned int rx_num, unsigned int *rx_slot)
  514. {
  515. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  516. struct btfmcodec_state_machine states = btfmcodec->states;
  517. BTFMCODEC_DBG("");
  518. // ToDo: check whether hw_params has to allowed when state if different
  519. if (states.current_state != IDLE) {
  520. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  521. } else {
  522. return btfmcodec_hwep_set_channel_map((void *)btfmcodec->hwep_info, tx_num,
  523. tx_slot, rx_num, rx_slot);
  524. }
  525. return 0;
  526. }
  527. int btfmcodec_hwep_get_channel_map(void *hwep_info, unsigned int *tx_num,
  528. unsigned int *tx_slot, unsigned int *rx_num,
  529. unsigned int *rx_slot, int id)
  530. {
  531. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)
  532. btfmcodec_get_dai_drvdata(hwep_info);
  533. if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_get_channel_map) {
  534. return dai_drv->dai_ops->hwep_get_channel_map(hwep_info, tx_num,
  535. tx_slot, rx_num,
  536. rx_slot, id);
  537. } else {
  538. return -1;
  539. }
  540. }
  541. static int btfmcodec_dai_get_channel_map(struct snd_soc_dai *dai,
  542. unsigned int *tx_num, unsigned int *tx_slot,
  543. unsigned int *rx_num, unsigned int *rx_slot)
  544. {
  545. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  546. // struct btfmcodec_state_machine states = btfmcodec->states;
  547. BTFMCODEC_DBG("");
  548. // ToDo: get_channel_map is not needed for new driver
  549. /* if (states.current_state != IDLE) {
  550. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  551. } else {
  552. */ return btfmcodec_hwep_get_channel_map((void *)btfmcodec->hwep_info,
  553. tx_num, tx_slot, rx_num,
  554. rx_slot, dai->id);
  555. // }
  556. return 0;
  557. }
  558. void btfmcodec_wq_hwep_configure(struct work_struct *work)
  559. {
  560. struct btfmcodec_char_device *btfmcodec_dev = container_of(work,
  561. struct btfmcodec_char_device,
  562. wq_hwep_configure);
  563. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  564. struct list_head *head = &btfmcodec->config_head;
  565. struct hwep_configurations *hwep_configs = NULL;
  566. int ret;
  567. int idx = BTM_PKT_TYPE_HWEP_CONFIG;
  568. uint32_t sample_rate, direction;
  569. uint8_t id, bit_width, codectype;
  570. list_for_each_entry(hwep_configs, head, dai_list) {
  571. id = hwep_configs->stream_id;
  572. sample_rate = hwep_configs->sample_rate;
  573. bit_width = hwep_configs->bit_width;
  574. codectype = hwep_configs->codectype;
  575. direction = hwep_configs->direction;
  576. BTFMCODEC_INFO("configuring dai id:%d with sampling rate:%d bit_width:%d", id, sample_rate, bit_width);
  577. ret = btfmcodec_hwep_startup(btfmcodec);
  578. if (ret >= 0)
  579. ret = btfmcodec_hwep_hw_params(btfmcodec, bit_width, direction);
  580. if (ret >= 0)
  581. ret = btfmcodec_hwep_prepare(btfmcodec, sample_rate, direction, id);
  582. if (ret < 0) {
  583. BTFMCODEC_ERR("failed to configure hwep", hwep_configs->stream_id);
  584. break;
  585. }
  586. }
  587. if (ret < 0)
  588. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  589. else
  590. btfmcodec_dev->status[idx] = BTM_RSP_RECV;
  591. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  592. }
  593. static struct snd_soc_dai_ops btfmcodec_dai_ops = {
  594. .startup = btfmcodec_dai_startup,
  595. .shutdown = btfmcodec_dai_shutdown,
  596. .hw_params = btfmcodec_dai_hw_params,
  597. .prepare = btfmcodec_dai_prepare,
  598. .set_channel_map = btfmcodec_dai_set_channel_map,
  599. .get_channel_map = btfmcodec_dai_get_channel_map,
  600. };
  601. int btfm_register_codec(struct hwep_data *hwep_info)
  602. {
  603. struct btfmcodec_data *btfmcodec;
  604. struct btfmcodec_char_device *btfmcodec_dev;
  605. struct device *dev;
  606. struct hwep_dai_driver *dai_drv;
  607. int i, ret;
  608. btfmcodec = btfm_get_btfmcodec();
  609. btfmcodec_dev = btfmcodec->btfmcodec_dev;
  610. dev = &btfmcodec->dev;
  611. btfmcodec_dai_info = kzalloc((sizeof(struct snd_soc_dai_driver) * hwep_info->num_dai), GFP_KERNEL);
  612. if (!btfmcodec_dai_info) {
  613. BTFMCODEC_ERR("failed to allocate memory");
  614. return -ENOMEM;
  615. }
  616. for (i = 0; i < hwep_info->num_dai; i++) {
  617. dai_drv = &hwep_info->dai_drv[i];
  618. btfmcodec_dai_info[i].name = dai_drv->dai_name;
  619. btfmcodec_dai_info[i].id = dai_drv->id;
  620. btfmcodec_dai_info[i].capture = dai_drv->capture;
  621. btfmcodec_dai_info[i].playback = dai_drv->playback;
  622. btfmcodec_dai_info[i].ops = &btfmcodec_dai_ops;
  623. }
  624. BTFMCODEC_INFO("Adding %d dai support to codec", hwep_info->num_dai);
  625. BTFMCODEC_INFO("slim bus driver name:%s", dev->driver->name);
  626. ret = snd_soc_register_component(dev, &btfmcodec_codec_component,
  627. btfmcodec_dai_info, hwep_info->num_dai);
  628. BTFMCODEC_INFO("Dev node address: %p", dev);
  629. BTFMCODEC_INFO("btfmcodec address :%p, btfmcodec");
  630. BTFMCODEC_INFO("HWEPINFO address:%p", hwep_info);
  631. BTFMCODEC_INFO("btfmcodec_dev INFO address:%p", btfmcodec->btfmcodec_dev);
  632. BTFMCODEC_INFO("before wq_hwep_shutdown:%p", btfmcodec_dev->wq_hwep_shutdown);
  633. BTFMCODEC_INFO("before wq_prepare_bearer:%p", btfmcodec_dev->wq_prepare_bearer);
  634. INIT_WORK(&btfmcodec_dev->wq_hwep_shutdown, btfmcodec_wq_hwep_shutdown);
  635. INIT_WORK(&btfmcodec_dev->wq_prepare_bearer, btfmcodec_wq_prepare_bearer);
  636. INIT_WORK(&btfmcodec_dev->wq_hwep_configure, btfmcodec_wq_hwep_configure);
  637. BTFMCODEC_INFO("after wq_hwep_shutdown:%p", btfmcodec_dev->wq_hwep_shutdown);
  638. BTFMCODEC_INFO("after wq_prepare_bearer:%p", btfmcodec_dev->wq_prepare_bearer);
  639. BTFMCODEC_INFO("btfmcodec_wq_prepare_bearer:%p", btfmcodec_wq_prepare_bearer);
  640. BTFMCODEC_INFO("btfmcodec_wq_hwep_shutdown:%p", btfmcodec_wq_hwep_shutdown);
  641. return ret;
  642. }
  643. void btfm_unregister_codec(void)
  644. {
  645. struct btfmcodec_data *btfmcodec;
  646. btfmcodec = btfm_get_btfmcodec();
  647. snd_soc_unregister_component(&btfmcodec->dev);
  648. }