q6usm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/mutex.h>
  14. #include <linux/wait.h>
  15. #include <linux/msm_audio_ion.h>
  16. #include <linux/sched.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/slab.h>
  19. #include <linux/msm_audio.h>
  20. #include <sound/apr_audio-v2.h>
  21. #include <linux/qdsp6v2/apr_us.h>
  22. #include "q6usm.h"
  23. #define ADSP_MEMORY_MAP_SHMEM8_4K_POOL 3
  24. #define MEM_4K_OFFSET 4095
  25. #define MEM_4K_MASK 0xfffff000
  26. #define USM_SESSION_MAX 0x02 /* aDSP:USM limit */
  27. #define READDONE_IDX_STATUS 0
  28. #define WRITEDONE_IDX_STATUS 0
  29. /* Standard timeout in the asynchronous ops */
  30. #define Q6USM_TIMEOUT_JIFFIES (1*HZ) /* 1 sec */
  31. static DEFINE_MUTEX(session_lock);
  32. static struct us_client *session[USM_SESSION_MAX];
  33. static int32_t q6usm_mmapcallback(struct apr_client_data *data, void *priv);
  34. static int32_t q6usm_callback(struct apr_client_data *data, void *priv);
  35. static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
  36. uint32_t pkt_size, bool cmd_flg);
  37. struct usm_mmap {
  38. atomic_t ref_cnt;
  39. atomic_t cmd_state;
  40. wait_queue_head_t cmd_wait;
  41. void *apr;
  42. int mem_handle;
  43. };
  44. static struct usm_mmap this_mmap;
  45. static void q6usm_add_mmaphdr(struct apr_hdr *hdr,
  46. uint32_t pkt_size, bool cmd_flg, u32 token)
  47. {
  48. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  49. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  50. hdr->src_port = 0;
  51. hdr->dest_port = 0;
  52. if (cmd_flg) {
  53. hdr->token = token;
  54. atomic_set(&this_mmap.cmd_state, 1);
  55. }
  56. hdr->pkt_size = pkt_size;
  57. }
  58. static int q6usm_memory_map(phys_addr_t buf_add, int dir, uint32_t bufsz,
  59. uint32_t bufcnt, uint32_t session, uint32_t *mem_handle)
  60. {
  61. struct usm_cmd_memory_map_region mem_region_map;
  62. int rc = 0;
  63. if (this_mmap.apr == NULL) {
  64. pr_err("%s: APR handle NULL\n", __func__);
  65. return -EINVAL;
  66. }
  67. q6usm_add_mmaphdr(&mem_region_map.hdr,
  68. sizeof(struct usm_cmd_memory_map_region), true,
  69. ((session << 8) | dir));
  70. mem_region_map.hdr.opcode = USM_CMD_SHARED_MEM_MAP_REGION;
  71. mem_region_map.mempool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  72. mem_region_map.num_regions = 1;
  73. mem_region_map.flags = 0;
  74. mem_region_map.shm_addr_lsw = lower_32_bits(buf_add);
  75. mem_region_map.shm_addr_msw =
  76. msm_audio_populate_upper_32_bits(buf_add);
  77. mem_region_map.mem_size_bytes = bufsz * bufcnt;
  78. rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_region_map);
  79. if (rc < 0) {
  80. pr_err("%s: mem_map op[0x%x]rc[%d]\n",
  81. __func__, mem_region_map.hdr.opcode, rc);
  82. rc = -EINVAL;
  83. goto fail_cmd;
  84. }
  85. rc = wait_event_timeout(this_mmap.cmd_wait,
  86. (atomic_read(&this_mmap.cmd_state) == 0),
  87. Q6USM_TIMEOUT_JIFFIES);
  88. if (!rc) {
  89. rc = -ETIME;
  90. pr_err("%s: timeout. waited for memory_map\n", __func__);
  91. } else {
  92. *mem_handle = this_mmap.mem_handle;
  93. rc = 0;
  94. }
  95. fail_cmd:
  96. return rc;
  97. }
  98. int q6usm_memory_unmap(phys_addr_t buf_add, int dir, uint32_t session,
  99. uint32_t mem_handle)
  100. {
  101. struct usm_cmd_memory_unmap_region mem_unmap;
  102. int rc = 0;
  103. if (this_mmap.apr == NULL) {
  104. pr_err("%s: APR handle NULL\n", __func__);
  105. return -EINVAL;
  106. }
  107. q6usm_add_mmaphdr(&mem_unmap.hdr,
  108. sizeof(struct usm_cmd_memory_unmap_region), true,
  109. ((session << 8) | dir));
  110. mem_unmap.hdr.opcode = USM_CMD_SHARED_MEM_UNMAP_REGION;
  111. mem_unmap.mem_map_handle = mem_handle;
  112. rc = apr_send_pkt(this_mmap.apr, (uint32_t *) &mem_unmap);
  113. if (rc < 0) {
  114. pr_err("%s: mem_unmap op[0x%x] rc[%d]\n",
  115. __func__, mem_unmap.hdr.opcode, rc);
  116. goto fail_cmd;
  117. }
  118. rc = wait_event_timeout(this_mmap.cmd_wait,
  119. (atomic_read(&this_mmap.cmd_state) == 0),
  120. Q6USM_TIMEOUT_JIFFIES);
  121. if (!rc) {
  122. rc = -ETIME;
  123. pr_err("%s: timeout. waited for memory_unmap\n", __func__);
  124. } else
  125. rc = 0;
  126. fail_cmd:
  127. return rc;
  128. }
  129. static int q6usm_session_alloc(struct us_client *usc)
  130. {
  131. int ind = 0;
  132. mutex_lock(&session_lock);
  133. for (ind = 0; ind < USM_SESSION_MAX; ++ind) {
  134. if (!session[ind]) {
  135. session[ind] = usc;
  136. mutex_unlock(&session_lock);
  137. ++ind; /* session id: 0 reserved */
  138. pr_debug("%s: session[%d] was allocated\n",
  139. __func__, ind);
  140. return ind;
  141. }
  142. }
  143. mutex_unlock(&session_lock);
  144. return -ENOMEM;
  145. }
  146. static void q6usm_session_free(struct us_client *usc)
  147. {
  148. /* Session index was incremented during allocation */
  149. uint16_t ind = (uint16_t)usc->session - 1;
  150. pr_debug("%s: to free session[%d]\n", __func__, ind);
  151. if (ind < USM_SESSION_MAX) {
  152. mutex_lock(&session_lock);
  153. session[ind] = NULL;
  154. mutex_unlock(&session_lock);
  155. }
  156. }
  157. static int q6usm_us_client_buf_free(unsigned int dir,
  158. struct us_client *usc)
  159. {
  160. struct us_port_data *port;
  161. int rc = 0;
  162. if ((usc == NULL) ||
  163. ((dir != IN) && (dir != OUT)))
  164. return -EINVAL;
  165. mutex_lock(&usc->cmd_lock);
  166. port = &usc->port[dir];
  167. if (port == NULL) {
  168. mutex_unlock(&usc->cmd_lock);
  169. return -EINVAL;
  170. }
  171. if (port->data == NULL) {
  172. mutex_unlock(&usc->cmd_lock);
  173. return 0;
  174. }
  175. rc = q6usm_memory_unmap(port->phys, dir, usc->session,
  176. *((uint32_t *)port->ext));
  177. pr_debug("%s: data[%pK]phys[%llx][%pK]\n", __func__,
  178. (void *)port->data, (u64)port->phys, (void *)&port->phys);
  179. msm_audio_ion_free(port->client, port->handle);
  180. port->data = NULL;
  181. port->phys = 0;
  182. port->buf_size = 0;
  183. port->buf_cnt = 0;
  184. port->client = NULL;
  185. port->handle = NULL;
  186. mutex_unlock(&usc->cmd_lock);
  187. return rc;
  188. }
  189. int q6usm_us_param_buf_free(unsigned int dir,
  190. struct us_client *usc)
  191. {
  192. struct us_port_data *port;
  193. int rc = 0;
  194. if ((usc == NULL) ||
  195. ((dir != IN) && (dir != OUT)))
  196. return -EINVAL;
  197. mutex_lock(&usc->cmd_lock);
  198. port = &usc->port[dir];
  199. if (port == NULL) {
  200. mutex_unlock(&usc->cmd_lock);
  201. return -EINVAL;
  202. }
  203. if (port->param_buf == NULL) {
  204. mutex_unlock(&usc->cmd_lock);
  205. return 0;
  206. }
  207. rc = q6usm_memory_unmap(port->param_phys, dir, usc->session,
  208. *((uint32_t *)port->param_buf_mem_handle));
  209. pr_debug("%s: data[%pK]phys[%llx][%pK]\n", __func__,
  210. (void *)port->param_buf, (u64)port->param_phys,
  211. (void *)&port->param_phys);
  212. msm_audio_ion_free(port->param_client, port->param_handle);
  213. port->param_buf = NULL;
  214. port->param_phys = 0;
  215. port->param_buf_size = 0;
  216. port->param_client = NULL;
  217. port->param_handle = NULL;
  218. mutex_unlock(&usc->cmd_lock);
  219. return rc;
  220. }
  221. void q6usm_us_client_free(struct us_client *usc)
  222. {
  223. int loopcnt = 0;
  224. struct us_port_data *port;
  225. uint32_t *p_mem_handle = NULL;
  226. if ((usc == NULL) ||
  227. !(usc->session))
  228. return;
  229. for (loopcnt = 0; loopcnt <= OUT; ++loopcnt) {
  230. port = &usc->port[loopcnt];
  231. if (port->data == NULL)
  232. continue;
  233. pr_debug("%s: loopcnt = %d\n", __func__, loopcnt);
  234. q6usm_us_client_buf_free(loopcnt, usc);
  235. q6usm_us_param_buf_free(loopcnt, usc);
  236. }
  237. q6usm_session_free(usc);
  238. apr_deregister(usc->apr);
  239. pr_debug("%s: APR De-Register\n", __func__);
  240. if (atomic_read(&this_mmap.ref_cnt) <= 0) {
  241. pr_err("%s: APR Common Port Already Closed\n", __func__);
  242. goto done;
  243. }
  244. atomic_dec(&this_mmap.ref_cnt);
  245. if (atomic_read(&this_mmap.ref_cnt) == 0) {
  246. apr_deregister(this_mmap.apr);
  247. pr_debug("%s: APR De-Register common port\n", __func__);
  248. }
  249. done:
  250. p_mem_handle = (uint32_t *)usc->port[IN].ext;
  251. kfree(p_mem_handle);
  252. kfree(usc);
  253. pr_debug("%s:\n", __func__);
  254. }
  255. struct us_client *q6usm_us_client_alloc(
  256. void (*cb)(uint32_t, uint32_t, uint32_t *, void *),
  257. void *priv)
  258. {
  259. struct us_client *usc;
  260. uint32_t *p_mem_handle = NULL;
  261. int n;
  262. int lcnt = 0;
  263. usc = kzalloc(sizeof(struct us_client), GFP_KERNEL);
  264. if (usc == NULL)
  265. return NULL;
  266. p_mem_handle = kzalloc(sizeof(uint32_t) * 4, GFP_KERNEL);
  267. if (p_mem_handle == NULL) {
  268. kfree(usc);
  269. return NULL;
  270. }
  271. n = q6usm_session_alloc(usc);
  272. if (n <= 0)
  273. goto fail_session;
  274. usc->session = n;
  275. usc->cb = cb;
  276. usc->priv = priv;
  277. usc->apr = apr_register("ADSP", "USM",
  278. (apr_fn)q6usm_callback,
  279. ((usc->session) << 8 | 0x0001),
  280. usc);
  281. if (usc->apr == NULL) {
  282. pr_err("%s: Registration with APR failed\n", __func__);
  283. goto fail;
  284. }
  285. pr_debug("%s: Registering the common port with APR\n", __func__);
  286. if (atomic_read(&this_mmap.ref_cnt) == 0) {
  287. this_mmap.apr = apr_register("ADSP", "USM",
  288. (apr_fn)q6usm_mmapcallback,
  289. 0x0FFFFFFFF, &this_mmap);
  290. if (this_mmap.apr == NULL) {
  291. pr_err("%s: USM port registration failed\n",
  292. __func__);
  293. goto fail;
  294. }
  295. }
  296. atomic_inc(&this_mmap.ref_cnt);
  297. init_waitqueue_head(&usc->cmd_wait);
  298. mutex_init(&usc->cmd_lock);
  299. for (lcnt = 0; lcnt <= OUT; ++lcnt) {
  300. mutex_init(&usc->port[lcnt].lock);
  301. spin_lock_init(&usc->port[lcnt].dsp_lock);
  302. usc->port[lcnt].ext = (void *)p_mem_handle++;
  303. usc->port[lcnt].param_buf_mem_handle = (void *)p_mem_handle++;
  304. pr_err("%s: usc->port[%d].ext=%pK;\n",
  305. __func__, lcnt, usc->port[lcnt].ext);
  306. }
  307. atomic_set(&usc->cmd_state, 0);
  308. return usc;
  309. fail:
  310. kfree(p_mem_handle);
  311. q6usm_us_client_free(usc);
  312. return NULL;
  313. fail_session:
  314. kfree(p_mem_handle);
  315. kfree(usc);
  316. return NULL;
  317. }
  318. int q6usm_us_client_buf_alloc(unsigned int dir,
  319. struct us_client *usc,
  320. unsigned int bufsz,
  321. unsigned int bufcnt)
  322. {
  323. int rc = 0;
  324. struct us_port_data *port = NULL;
  325. unsigned int size = bufsz*bufcnt;
  326. size_t len;
  327. if ((usc == NULL) ||
  328. ((dir != IN) && (dir != OUT)) || (size == 0) ||
  329. (usc->session <= 0 || usc->session > USM_SESSION_MAX)) {
  330. pr_err("%s: wrong parameters: size=%d; bufcnt=%d\n",
  331. __func__, size, bufcnt);
  332. return -EINVAL;
  333. }
  334. mutex_lock(&usc->cmd_lock);
  335. port = &usc->port[dir];
  336. /* The size to allocate should be multiple of 4K bytes */
  337. size = PAGE_ALIGN(size);
  338. rc = msm_audio_ion_alloc("ultrasound_client",
  339. &port->client, &port->handle,
  340. size, &port->phys,
  341. &len, &port->data);
  342. if (rc) {
  343. pr_err("%s: US ION allocation failed, rc = %d\n",
  344. __func__, rc);
  345. mutex_unlock(&usc->cmd_lock);
  346. return -ENOMEM;
  347. }
  348. port->buf_cnt = bufcnt;
  349. port->buf_size = bufsz;
  350. pr_debug("%s: data[%pK]; phys[%llx]; [%pK]\n", __func__,
  351. (void *)port->data,
  352. (u64)port->phys,
  353. (void *)&port->phys);
  354. rc = q6usm_memory_map(port->phys, dir, size, 1, usc->session,
  355. (uint32_t *)port->ext);
  356. if (rc < 0) {
  357. pr_err("%s: CMD Memory_map failed\n", __func__);
  358. mutex_unlock(&usc->cmd_lock);
  359. q6usm_us_client_buf_free(dir, usc);
  360. q6usm_us_param_buf_free(dir, usc);
  361. } else {
  362. mutex_unlock(&usc->cmd_lock);
  363. rc = 0;
  364. }
  365. return rc;
  366. }
  367. int q6usm_us_param_buf_alloc(unsigned int dir,
  368. struct us_client *usc,
  369. unsigned int bufsz)
  370. {
  371. int rc = 0;
  372. struct us_port_data *port = NULL;
  373. unsigned int size = bufsz;
  374. size_t len;
  375. if ((usc == NULL) ||
  376. ((dir != IN) && (dir != OUT)) ||
  377. (usc->session <= 0 || usc->session > USM_SESSION_MAX)) {
  378. pr_err("%s: wrong parameters: direction=%d, bufsz=%d\n",
  379. __func__, dir, bufsz);
  380. return -EINVAL;
  381. }
  382. mutex_lock(&usc->cmd_lock);
  383. port = &usc->port[dir];
  384. if (bufsz == 0) {
  385. pr_debug("%s: bufsz=0, get/set param commands are forbidden\n",
  386. __func__);
  387. port->param_buf = NULL;
  388. mutex_unlock(&usc->cmd_lock);
  389. return rc;
  390. }
  391. /* The size to allocate should be multiple of 4K bytes */
  392. size = PAGE_ALIGN(size);
  393. rc = msm_audio_ion_alloc("ultrasound_client",
  394. &port->param_client, &port->param_handle,
  395. size, &port->param_phys,
  396. &len, &port->param_buf);
  397. if (rc) {
  398. pr_err("%s: US ION allocation failed, rc = %d\n",
  399. __func__, rc);
  400. mutex_unlock(&usc->cmd_lock);
  401. return -ENOMEM;
  402. }
  403. port->param_buf_size = bufsz;
  404. pr_debug("%s: param_buf[%pK]; param_phys[%llx]; [%pK]\n", __func__,
  405. (void *)port->param_buf,
  406. (u64)port->param_phys,
  407. (void *)&port->param_phys);
  408. rc = q6usm_memory_map(port->param_phys, (IN | OUT), size, 1,
  409. usc->session, (uint32_t *)port->param_buf_mem_handle);
  410. if (rc < 0) {
  411. pr_err("%s: CMD Memory_map failed\n", __func__);
  412. mutex_unlock(&usc->cmd_lock);
  413. q6usm_us_client_buf_free(dir, usc);
  414. q6usm_us_param_buf_free(dir, usc);
  415. } else {
  416. mutex_unlock(&usc->cmd_lock);
  417. rc = 0;
  418. }
  419. return rc;
  420. }
  421. static int32_t q6usm_mmapcallback(struct apr_client_data *data, void *priv)
  422. {
  423. uint32_t token;
  424. uint32_t *payload = data->payload;
  425. pr_debug("%s: ptr0[0x%x]; ptr1[0x%x]; opcode[0x%x]\n",
  426. __func__, payload[0], payload[1], data->opcode);
  427. pr_debug("%s: token[0x%x]; payload_size[%d]; src[%d]; dest[%d];\n",
  428. __func__, data->token, data->payload_size,
  429. data->src_port, data->dest_port);
  430. if (data->opcode == APR_BASIC_RSP_RESULT) {
  431. /* status field check */
  432. if (payload[1]) {
  433. pr_err("%s: wrong response[%d] on cmd [%d]\n",
  434. __func__, payload[1], payload[0]);
  435. } else {
  436. token = data->token;
  437. switch (payload[0]) {
  438. case USM_CMD_SHARED_MEM_UNMAP_REGION:
  439. if (atomic_read(&this_mmap.cmd_state)) {
  440. atomic_set(&this_mmap.cmd_state, 0);
  441. wake_up(&this_mmap.cmd_wait);
  442. }
  443. /* fallthrough */
  444. case USM_CMD_SHARED_MEM_MAP_REGION:
  445. /* For MEM_MAP, additional answer is waited, */
  446. /* therfore, no wake-up here */
  447. pr_debug("%s: cmd[0x%x]; result[0x%x]\n",
  448. __func__, payload[0], payload[1]);
  449. break;
  450. default:
  451. pr_debug("%s: wrong command[0x%x]\n",
  452. __func__, payload[0]);
  453. break;
  454. }
  455. }
  456. } else {
  457. if (data->opcode == USM_CMDRSP_SHARED_MEM_MAP_REGION) {
  458. this_mmap.mem_handle = payload[0];
  459. pr_debug("%s: memory map handle = 0x%x",
  460. __func__, payload[0]);
  461. if (atomic_read(&this_mmap.cmd_state)) {
  462. atomic_set(&this_mmap.cmd_state, 0);
  463. wake_up(&this_mmap.cmd_wait);
  464. }
  465. }
  466. }
  467. return 0;
  468. }
  469. static int32_t q6usm_callback(struct apr_client_data *data, void *priv)
  470. {
  471. struct us_client *usc = (struct us_client *)priv;
  472. unsigned long dsp_flags;
  473. uint32_t *payload = data->payload;
  474. uint32_t token = data->token;
  475. uint32_t opcode = Q6USM_EVENT_UNDEF;
  476. if (usc == NULL) {
  477. pr_err("%s: client info is NULL\n", __func__);
  478. return -EINVAL;
  479. }
  480. if (data->opcode == APR_BASIC_RSP_RESULT) {
  481. /* status field check */
  482. if (payload[1]) {
  483. pr_err("%s: wrong response[%d] on cmd [%d]\n",
  484. __func__, payload[1], payload[0]);
  485. if (usc->cb)
  486. usc->cb(data->opcode, token,
  487. (uint32_t *)data->payload, usc->priv);
  488. } else {
  489. switch (payload[0]) {
  490. case USM_SESSION_CMD_RUN:
  491. case USM_STREAM_CMD_CLOSE:
  492. if (token != usc->session) {
  493. pr_err("%s: wrong token[%d]",
  494. __func__, token);
  495. break;
  496. }
  497. case USM_STREAM_CMD_OPEN_READ:
  498. case USM_STREAM_CMD_OPEN_WRITE:
  499. case USM_STREAM_CMD_SET_ENC_PARAM:
  500. case USM_DATA_CMD_MEDIA_FORMAT_UPDATE:
  501. case USM_SESSION_CMD_SIGNAL_DETECT_MODE:
  502. case USM_STREAM_CMD_SET_PARAM:
  503. case USM_STREAM_CMD_GET_PARAM:
  504. if (atomic_read(&usc->cmd_state)) {
  505. atomic_set(&usc->cmd_state, 0);
  506. wake_up(&usc->cmd_wait);
  507. }
  508. if (usc->cb)
  509. usc->cb(data->opcode, token,
  510. (uint32_t *)data->payload,
  511. usc->priv);
  512. break;
  513. default:
  514. break;
  515. }
  516. }
  517. return 0;
  518. }
  519. switch (data->opcode) {
  520. case RESET_EVENTS: {
  521. pr_err("%s: Reset event is received: %d %d\n",
  522. __func__,
  523. data->reset_event,
  524. data->reset_proc);
  525. opcode = RESET_EVENTS;
  526. apr_reset(this_mmap.apr);
  527. this_mmap.apr = NULL;
  528. apr_reset(usc->apr);
  529. usc->apr = NULL;
  530. break;
  531. }
  532. case USM_DATA_EVENT_READ_DONE: {
  533. struct us_port_data *port = &usc->port[OUT];
  534. opcode = Q6USM_EVENT_READ_DONE;
  535. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  536. if (payload[READDONE_IDX_STATUS]) {
  537. pr_err("%s: wrong READDONE[%d]; token[%d]\n",
  538. __func__,
  539. payload[READDONE_IDX_STATUS],
  540. token);
  541. token = USM_WRONG_TOKEN;
  542. spin_unlock_irqrestore(&port->dsp_lock,
  543. dsp_flags);
  544. break;
  545. }
  546. if (port->expected_token != token) {
  547. u32 cpu_buf = port->cpu_buf;
  548. pr_err("%s: expected[%d] != token[%d]\n",
  549. __func__, port->expected_token, token);
  550. pr_debug("%s: dsp_buf=%d; cpu_buf=%d;\n",
  551. __func__, port->dsp_buf, cpu_buf);
  552. token = USM_WRONG_TOKEN;
  553. /* To prevent data handle continiue */
  554. port->expected_token = USM_WRONG_TOKEN;
  555. spin_unlock_irqrestore(&port->dsp_lock,
  556. dsp_flags);
  557. break;
  558. } /* port->expected_token != data->token */
  559. port->expected_token = token + 1;
  560. if (port->expected_token == port->buf_cnt)
  561. port->expected_token = 0;
  562. /* gap support */
  563. if (port->expected_token != port->cpu_buf) {
  564. port->dsp_buf = port->expected_token;
  565. token = port->dsp_buf; /* for callback */
  566. } else
  567. port->dsp_buf = token;
  568. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  569. break;
  570. } /* case USM_DATA_EVENT_READ_DONE */
  571. case USM_DATA_EVENT_WRITE_DONE: {
  572. struct us_port_data *port = &usc->port[IN];
  573. opcode = Q6USM_EVENT_WRITE_DONE;
  574. if (payload[WRITEDONE_IDX_STATUS]) {
  575. pr_err("%s: wrong WRITEDONE_IDX_STATUS[%d]\n",
  576. __func__,
  577. payload[WRITEDONE_IDX_STATUS]);
  578. break;
  579. }
  580. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  581. port->dsp_buf = token + 1;
  582. if (port->dsp_buf == port->buf_cnt)
  583. port->dsp_buf = 0;
  584. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  585. break;
  586. } /* case USM_DATA_EVENT_WRITE_DONE */
  587. case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT: {
  588. pr_debug("%s: US detect result: result=%d",
  589. __func__,
  590. payload[0]);
  591. opcode = Q6USM_EVENT_SIGNAL_DETECT_RESULT;
  592. break;
  593. } /* case USM_SESSION_EVENT_SIGNAL_DETECT_RESULT */
  594. default:
  595. return 0;
  596. } /* switch */
  597. if (usc->cb)
  598. usc->cb(opcode, token,
  599. data->payload, usc->priv);
  600. return 0;
  601. }
  602. uint32_t q6usm_get_virtual_address(int dir,
  603. struct us_client *usc,
  604. struct vm_area_struct *vms)
  605. {
  606. uint32_t ret = 0xffffffff;
  607. if (vms && (usc != NULL) && ((dir == IN) || (dir == OUT))) {
  608. struct us_port_data *port = &usc->port[dir];
  609. int size = PAGE_ALIGN(port->buf_size * port->buf_cnt);
  610. struct audio_buffer ab;
  611. ab.phys = port->phys;
  612. ab.data = port->data;
  613. ab.used = 1;
  614. ab.size = size;
  615. ab.actual_size = size;
  616. ab.handle = port->handle;
  617. ab.client = port->client;
  618. ret = msm_audio_ion_mmap(&ab, vms);
  619. }
  620. return ret;
  621. }
  622. static void q6usm_add_hdr(struct us_client *usc, struct apr_hdr *hdr,
  623. uint32_t pkt_size, bool cmd_flg)
  624. {
  625. mutex_lock(&usc->cmd_lock);
  626. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  627. APR_HDR_LEN(sizeof(struct apr_hdr)),
  628. APR_PKT_VER);
  629. hdr->src_svc = ((struct apr_svc *)usc->apr)->id;
  630. hdr->src_domain = APR_DOMAIN_APPS;
  631. hdr->dest_svc = APR_SVC_USM;
  632. hdr->dest_domain = APR_DOMAIN_ADSP;
  633. hdr->src_port = (usc->session << 8) | 0x0001;
  634. hdr->dest_port = (usc->session << 8) | 0x0001;
  635. if (cmd_flg) {
  636. hdr->token = usc->session;
  637. atomic_set(&usc->cmd_state, 1);
  638. }
  639. hdr->pkt_size = pkt_size;
  640. mutex_unlock(&usc->cmd_lock);
  641. }
  642. static uint32_t q6usm_ext2int_format(uint32_t ext_format)
  643. {
  644. uint32_t int_format = INVALID_FORMAT;
  645. switch (ext_format) {
  646. case FORMAT_USPS_EPOS:
  647. int_format = US_POINT_EPOS_FORMAT_V2;
  648. break;
  649. case FORMAT_USRAW:
  650. int_format = US_RAW_FORMAT_V2;
  651. break;
  652. case FORMAT_USPROX:
  653. int_format = US_PROX_FORMAT_V4;
  654. break;
  655. case FORMAT_USGES_SYNC:
  656. int_format = US_GES_SYNC_FORMAT;
  657. break;
  658. case FORMAT_USRAW_SYNC:
  659. int_format = US_RAW_SYNC_FORMAT;
  660. break;
  661. default:
  662. pr_err("%s: Invalid format[%d]\n", __func__, ext_format);
  663. break;
  664. }
  665. return int_format;
  666. }
  667. int q6usm_open_read(struct us_client *usc,
  668. uint32_t format)
  669. {
  670. uint32_t int_format = INVALID_FORMAT;
  671. int rc = 0x00;
  672. struct usm_stream_cmd_open_read open;
  673. if ((usc == NULL) || (usc->apr == NULL)) {
  674. pr_err("%s: client or its apr is NULL\n", __func__);
  675. return -EINVAL;
  676. }
  677. pr_debug("%s: session[%d]", __func__, usc->session);
  678. q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
  679. open.hdr.opcode = USM_STREAM_CMD_OPEN_READ;
  680. open.src_endpoint = 0; /* AFE */
  681. open.pre_proc_top = 0; /* No preprocessing required */
  682. int_format = q6usm_ext2int_format(format);
  683. if (int_format == INVALID_FORMAT)
  684. return -EINVAL;
  685. open.uMode = STREAM_PRIORITY_NORMAL;
  686. open.format = int_format;
  687. rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
  688. if (rc < 0) {
  689. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  690. __func__, open.hdr.opcode, rc);
  691. goto fail_cmd;
  692. }
  693. rc = wait_event_timeout(usc->cmd_wait,
  694. (atomic_read(&usc->cmd_state) == 0),
  695. Q6USM_TIMEOUT_JIFFIES);
  696. if (!rc) {
  697. rc = -ETIME;
  698. pr_err("%s: timeout, waited for OPEN_READ rc[%d]\n",
  699. __func__, rc);
  700. goto fail_cmd;
  701. } else
  702. rc = 0;
  703. fail_cmd:
  704. return rc;
  705. }
  706. int q6usm_enc_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
  707. {
  708. uint32_t int_format = INVALID_FORMAT;
  709. struct usm_stream_cmd_encdec_cfg_blk enc_cfg_obj;
  710. struct usm_stream_cmd_encdec_cfg_blk *enc_cfg = &enc_cfg_obj;
  711. int rc = 0;
  712. uint32_t total_cfg_size =
  713. sizeof(struct usm_stream_cmd_encdec_cfg_blk);
  714. uint32_t round_params_size = 0;
  715. uint8_t is_allocated = 0;
  716. if ((usc == NULL) || (us_cfg == NULL)) {
  717. pr_err("%s: wrong input", __func__);
  718. return -EINVAL;
  719. }
  720. int_format = q6usm_ext2int_format(us_cfg->format_id);
  721. if (int_format == INVALID_FORMAT) {
  722. pr_err("%s: wrong input format[%d]",
  723. __func__, us_cfg->format_id);
  724. return -EINVAL;
  725. }
  726. /* Transparent configuration data is after enc_cfg */
  727. /* Integer number of u32s is required */
  728. round_params_size = ((us_cfg->params_size + 3)/4) * 4;
  729. if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
  730. /* Dynamic allocated encdec_cfg_blk is required */
  731. /* static part use */
  732. round_params_size -= USM_MAX_CFG_DATA_SIZE;
  733. total_cfg_size += round_params_size;
  734. enc_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
  735. if (enc_cfg == NULL) {
  736. pr_err("%s: enc_cfg[%d] allocation failed\n",
  737. __func__, total_cfg_size);
  738. return -ENOMEM;
  739. }
  740. is_allocated = 1;
  741. } else
  742. round_params_size = 0;
  743. q6usm_add_hdr(usc, &enc_cfg->hdr, total_cfg_size, true);
  744. enc_cfg->hdr.opcode = USM_STREAM_CMD_SET_ENC_PARAM;
  745. enc_cfg->param_id = USM_PARAM_ID_ENCDEC_ENC_CFG_BLK;
  746. enc_cfg->param_size = sizeof(struct usm_encode_cfg_blk)+
  747. round_params_size;
  748. enc_cfg->enc_blk.frames_per_buf = 1;
  749. enc_cfg->enc_blk.format_id = int_format;
  750. enc_cfg->enc_blk.cfg_size = sizeof(struct usm_cfg_common)+
  751. USM_MAX_CFG_DATA_SIZE +
  752. round_params_size;
  753. memcpy(&(enc_cfg->enc_blk.cfg_common), &(us_cfg->cfg_common),
  754. sizeof(struct usm_cfg_common));
  755. /* Transparent data copy */
  756. memcpy(enc_cfg->enc_blk.transp_data, us_cfg->params,
  757. us_cfg->params_size);
  758. pr_debug("%s: cfg_size[%d], params_size[%d]\n",
  759. __func__,
  760. enc_cfg->enc_blk.cfg_size,
  761. us_cfg->params_size);
  762. pr_debug("%s: params[%d,%d,%d,%d, %d,%d,%d,%d]\n",
  763. __func__,
  764. enc_cfg->enc_blk.transp_data[0],
  765. enc_cfg->enc_blk.transp_data[1],
  766. enc_cfg->enc_blk.transp_data[2],
  767. enc_cfg->enc_blk.transp_data[3],
  768. enc_cfg->enc_blk.transp_data[4],
  769. enc_cfg->enc_blk.transp_data[5],
  770. enc_cfg->enc_blk.transp_data[6],
  771. enc_cfg->enc_blk.transp_data[7]
  772. );
  773. pr_debug("%s: srate:%d, ch=%d, bps= %d;\n",
  774. __func__, enc_cfg->enc_blk.cfg_common.sample_rate,
  775. enc_cfg->enc_blk.cfg_common.ch_cfg,
  776. enc_cfg->enc_blk.cfg_common.bits_per_sample);
  777. pr_debug("dmap:[0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x]; dev_id=0x%x\n",
  778. enc_cfg->enc_blk.cfg_common.data_map[0],
  779. enc_cfg->enc_blk.cfg_common.data_map[1],
  780. enc_cfg->enc_blk.cfg_common.data_map[2],
  781. enc_cfg->enc_blk.cfg_common.data_map[3],
  782. enc_cfg->enc_blk.cfg_common.data_map[4],
  783. enc_cfg->enc_blk.cfg_common.data_map[5],
  784. enc_cfg->enc_blk.cfg_common.data_map[6],
  785. enc_cfg->enc_blk.cfg_common.data_map[7],
  786. enc_cfg->enc_blk.cfg_common.dev_id);
  787. rc = apr_send_pkt(usc->apr, (uint32_t *) enc_cfg);
  788. if (rc < 0) {
  789. pr_err("%s:Comamnd open failed\n", __func__);
  790. rc = -EINVAL;
  791. goto fail_cmd;
  792. }
  793. rc = wait_event_timeout(usc->cmd_wait,
  794. (atomic_read(&usc->cmd_state) == 0),
  795. Q6USM_TIMEOUT_JIFFIES);
  796. if (!rc) {
  797. rc = -ETIME;
  798. pr_err("%s: timeout opcode[0x%x]\n",
  799. __func__, enc_cfg->hdr.opcode);
  800. } else
  801. rc = 0;
  802. fail_cmd:
  803. if (is_allocated == 1)
  804. kfree(enc_cfg);
  805. return rc;
  806. }
  807. int q6usm_dec_cfg_blk(struct us_client *usc, struct us_encdec_cfg *us_cfg)
  808. {
  809. uint32_t int_format = INVALID_FORMAT;
  810. struct usm_stream_media_format_update dec_cfg_obj;
  811. struct usm_stream_media_format_update *dec_cfg = &dec_cfg_obj;
  812. int rc = 0;
  813. uint32_t total_cfg_size = sizeof(struct usm_stream_media_format_update);
  814. uint32_t round_params_size = 0;
  815. uint8_t is_allocated = 0;
  816. if ((usc == NULL) || (us_cfg == NULL)) {
  817. pr_err("%s: wrong input", __func__);
  818. return -EINVAL;
  819. }
  820. int_format = q6usm_ext2int_format(us_cfg->format_id);
  821. if (int_format == INVALID_FORMAT) {
  822. pr_err("%s: wrong input format[%d]",
  823. __func__, us_cfg->format_id);
  824. return -EINVAL;
  825. }
  826. /* Transparent configuration data is after enc_cfg */
  827. /* Integer number of u32s is required */
  828. round_params_size = ((us_cfg->params_size + 3)/4) * 4;
  829. if (round_params_size > USM_MAX_CFG_DATA_SIZE) {
  830. /* Dynamic allocated encdec_cfg_blk is required */
  831. /* static part use */
  832. round_params_size -= USM_MAX_CFG_DATA_SIZE;
  833. total_cfg_size += round_params_size;
  834. dec_cfg = kzalloc(total_cfg_size, GFP_KERNEL);
  835. if (dec_cfg == NULL) {
  836. pr_err("%s:dec_cfg[%d] allocation failed\n",
  837. __func__, total_cfg_size);
  838. return -ENOMEM;
  839. }
  840. is_allocated = 1;
  841. } else { /* static transp_data is enough */
  842. round_params_size = 0;
  843. }
  844. q6usm_add_hdr(usc, &dec_cfg->hdr, total_cfg_size, true);
  845. dec_cfg->hdr.opcode = USM_DATA_CMD_MEDIA_FORMAT_UPDATE;
  846. dec_cfg->format_id = int_format;
  847. dec_cfg->cfg_size = sizeof(struct usm_cfg_common) +
  848. USM_MAX_CFG_DATA_SIZE +
  849. round_params_size;
  850. memcpy(&(dec_cfg->cfg_common), &(us_cfg->cfg_common),
  851. sizeof(struct usm_cfg_common));
  852. /* Transparent data copy */
  853. memcpy(dec_cfg->transp_data, us_cfg->params, us_cfg->params_size);
  854. pr_debug("%s: cfg_size[%d], params_size[%d]; parambytes[%d,%d,%d,%d]\n",
  855. __func__,
  856. dec_cfg->cfg_size,
  857. us_cfg->params_size,
  858. dec_cfg->transp_data[0],
  859. dec_cfg->transp_data[1],
  860. dec_cfg->transp_data[2],
  861. dec_cfg->transp_data[3]
  862. );
  863. rc = apr_send_pkt(usc->apr, (uint32_t *) dec_cfg);
  864. if (rc < 0) {
  865. pr_err("%s:Comamnd open failed\n", __func__);
  866. rc = -EINVAL;
  867. goto fail_cmd;
  868. }
  869. rc = wait_event_timeout(usc->cmd_wait,
  870. (atomic_read(&usc->cmd_state) == 0),
  871. Q6USM_TIMEOUT_JIFFIES);
  872. if (!rc) {
  873. rc = -ETIME;
  874. pr_err("%s: timeout opcode[0x%x]\n",
  875. __func__, dec_cfg->hdr.opcode);
  876. } else
  877. rc = 0;
  878. fail_cmd:
  879. if (is_allocated == 1)
  880. kfree(dec_cfg);
  881. return rc;
  882. }
  883. int q6usm_open_write(struct us_client *usc,
  884. uint32_t format)
  885. {
  886. int rc = 0;
  887. uint32_t int_format = INVALID_FORMAT;
  888. struct usm_stream_cmd_open_write open;
  889. if ((usc == NULL) || (usc->apr == NULL)) {
  890. pr_err("%s: APR handle NULL\n", __func__);
  891. return -EINVAL;
  892. }
  893. pr_debug("%s: session[%d]", __func__, usc->session);
  894. q6usm_add_hdr(usc, &open.hdr, sizeof(open), true);
  895. open.hdr.opcode = USM_STREAM_CMD_OPEN_WRITE;
  896. int_format = q6usm_ext2int_format(format);
  897. if (int_format == INVALID_FORMAT) {
  898. pr_err("%s: wrong format[%d]", __func__, format);
  899. return -EINVAL;
  900. }
  901. open.format = int_format;
  902. rc = apr_send_pkt(usc->apr, (uint32_t *) &open);
  903. if (rc < 0) {
  904. pr_err("%s:open failed op[0x%x]rc[%d]\n",
  905. __func__, open.hdr.opcode, rc);
  906. goto fail_cmd;
  907. }
  908. rc = wait_event_timeout(usc->cmd_wait,
  909. (atomic_read(&usc->cmd_state) == 0),
  910. Q6USM_TIMEOUT_JIFFIES);
  911. if (!rc) {
  912. rc = -ETIME;
  913. pr_err("%s:timeout. waited for OPEN_WRITR rc[%d]\n",
  914. __func__, rc);
  915. goto fail_cmd;
  916. } else
  917. rc = 0;
  918. fail_cmd:
  919. return rc;
  920. }
  921. int q6usm_run(struct us_client *usc, uint32_t flags,
  922. uint32_t msw_ts, uint32_t lsw_ts)
  923. {
  924. struct usm_stream_cmd_run run;
  925. int rc = 0;
  926. if ((usc == NULL) || (usc->apr == NULL)) {
  927. pr_err("%s: APR handle NULL\n", __func__);
  928. return -EINVAL;
  929. }
  930. q6usm_add_hdr(usc, &run.hdr, sizeof(run), true);
  931. run.hdr.opcode = USM_SESSION_CMD_RUN;
  932. run.flags = flags;
  933. run.msw_ts = msw_ts;
  934. run.lsw_ts = lsw_ts;
  935. rc = apr_send_pkt(usc->apr, (uint32_t *) &run);
  936. if (rc < 0) {
  937. pr_err("%s: Commmand run failed[%d]\n", __func__, rc);
  938. goto fail_cmd;
  939. }
  940. rc = wait_event_timeout(usc->cmd_wait,
  941. (atomic_read(&usc->cmd_state) == 0),
  942. Q6USM_TIMEOUT_JIFFIES);
  943. if (!rc) {
  944. rc = -ETIME;
  945. pr_err("%s: timeout. waited for run success rc[%d]\n",
  946. __func__, rc);
  947. } else
  948. rc = 0;
  949. fail_cmd:
  950. return rc;
  951. }
  952. int q6usm_read(struct us_client *usc, uint32_t read_ind)
  953. {
  954. struct usm_stream_cmd_read read;
  955. struct us_port_data *port = NULL;
  956. int rc = 0;
  957. u32 read_counter = 0;
  958. u32 loop_ind = 0;
  959. u64 buf_addr = 0;
  960. if ((usc == NULL) || (usc->apr == NULL)) {
  961. pr_err("%s: APR handle NULL\n", __func__);
  962. return -EINVAL;
  963. }
  964. port = &usc->port[OUT];
  965. if (read_ind > port->buf_cnt) {
  966. pr_err("%s: wrong read_ind[%d]\n",
  967. __func__, read_ind);
  968. return -EINVAL;
  969. }
  970. if (read_ind == port->cpu_buf) {
  971. pr_err("%s: no free region\n", __func__);
  972. return 0;
  973. }
  974. if (read_ind > port->cpu_buf) { /* 1 range */
  975. read_counter = read_ind - port->cpu_buf;
  976. } else { /* 2 ranges */
  977. read_counter = (port->buf_cnt - port->cpu_buf) + read_ind;
  978. }
  979. q6usm_add_hdr(usc, &read.hdr, sizeof(read), false);
  980. read.hdr.opcode = USM_DATA_CMD_READ;
  981. read.buf_size = port->buf_size;
  982. buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
  983. read.buf_addr_lsw = lower_32_bits(buf_addr);
  984. read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
  985. read.mem_map_handle = *((uint32_t *)(port->ext));
  986. for (loop_ind = 0; loop_ind < read_counter; ++loop_ind) {
  987. u32 temp_cpu_buf = port->cpu_buf;
  988. buf_addr = (u64)(port->phys) +
  989. port->buf_size * (port->cpu_buf);
  990. read.buf_addr_lsw = lower_32_bits(buf_addr);
  991. read.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
  992. read.seq_id = port->cpu_buf;
  993. read.hdr.token = port->cpu_buf;
  994. read.counter = 1;
  995. ++(port->cpu_buf);
  996. if (port->cpu_buf == port->buf_cnt)
  997. port->cpu_buf = 0;
  998. rc = apr_send_pkt(usc->apr, (uint32_t *) &read);
  999. if (rc < 0) {
  1000. port->cpu_buf = temp_cpu_buf;
  1001. pr_err("%s:read op[0x%x]rc[%d]\n",
  1002. __func__, read.hdr.opcode, rc);
  1003. break;
  1004. }
  1005. rc = 0;
  1006. } /* bufs loop */
  1007. return rc;
  1008. }
  1009. int q6usm_write(struct us_client *usc, uint32_t write_ind)
  1010. {
  1011. int rc = 0;
  1012. struct usm_stream_cmd_write cmd_write;
  1013. struct us_port_data *port = NULL;
  1014. u32 current_dsp_buf = 0;
  1015. u64 buf_addr = 0;
  1016. if ((usc == NULL) || (usc->apr == NULL)) {
  1017. pr_err("%s: APR handle NULL\n", __func__);
  1018. return -EINVAL;
  1019. }
  1020. port = &usc->port[IN];
  1021. current_dsp_buf = port->dsp_buf;
  1022. /* free region, caused by new dsp_buf report from DSP, */
  1023. /* can be only extended */
  1024. if (port->cpu_buf >= current_dsp_buf) {
  1025. /* 2 -part free region, including empty buffer */
  1026. if ((write_ind <= port->cpu_buf) &&
  1027. (write_ind > current_dsp_buf)) {
  1028. pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
  1029. __func__, write_ind,
  1030. current_dsp_buf, port->cpu_buf);
  1031. return -EINVAL;
  1032. }
  1033. } else {
  1034. /* 1 -part free region */
  1035. if ((write_ind <= port->cpu_buf) ||
  1036. (write_ind > current_dsp_buf)) {
  1037. pr_err("%s: wrong w_ind[%d]; d_buf=%d; c_buf=%d\n",
  1038. __func__, write_ind,
  1039. current_dsp_buf, port->cpu_buf);
  1040. return -EINVAL;
  1041. }
  1042. }
  1043. q6usm_add_hdr(usc, &cmd_write.hdr, sizeof(cmd_write), false);
  1044. cmd_write.hdr.opcode = USM_DATA_CMD_WRITE;
  1045. cmd_write.buf_size = port->buf_size;
  1046. buf_addr = (u64)(port->phys) + port->buf_size * (port->cpu_buf);
  1047. cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
  1048. cmd_write.buf_addr_msw = msm_audio_populate_upper_32_bits(buf_addr);
  1049. cmd_write.mem_map_handle = *((uint32_t *)(port->ext));
  1050. cmd_write.res0 = 0;
  1051. cmd_write.res1 = 0;
  1052. cmd_write.res2 = 0;
  1053. while (port->cpu_buf != write_ind) {
  1054. u32 temp_cpu_buf = port->cpu_buf;
  1055. buf_addr = (u64)(port->phys) +
  1056. port->buf_size * (port->cpu_buf);
  1057. cmd_write.buf_addr_lsw = lower_32_bits(buf_addr);
  1058. cmd_write.buf_addr_msw =
  1059. msm_audio_populate_upper_32_bits(buf_addr);
  1060. cmd_write.seq_id = port->cpu_buf;
  1061. cmd_write.hdr.token = port->cpu_buf;
  1062. ++(port->cpu_buf);
  1063. if (port->cpu_buf == port->buf_cnt)
  1064. port->cpu_buf = 0;
  1065. rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_write);
  1066. if (rc < 0) {
  1067. port->cpu_buf = temp_cpu_buf;
  1068. pr_err("%s:write op[0x%x];rc[%d];cpu_buf[%d]\n",
  1069. __func__, cmd_write.hdr.opcode,
  1070. rc, port->cpu_buf);
  1071. break;
  1072. }
  1073. rc = 0;
  1074. }
  1075. return rc;
  1076. }
  1077. bool q6usm_is_write_buf_full(struct us_client *usc, uint32_t *free_region)
  1078. {
  1079. struct us_port_data *port = NULL;
  1080. u32 cpu_buf = 0;
  1081. if ((usc == NULL) || !free_region) {
  1082. pr_err("%s: input data wrong\n", __func__);
  1083. return false;
  1084. }
  1085. port = &usc->port[IN];
  1086. cpu_buf = port->cpu_buf + 1;
  1087. if (cpu_buf == port->buf_cnt)
  1088. cpu_buf = 0;
  1089. *free_region = port->dsp_buf;
  1090. return cpu_buf == *free_region;
  1091. }
  1092. int q6usm_cmd(struct us_client *usc, int cmd)
  1093. {
  1094. struct apr_hdr hdr;
  1095. int rc = 0;
  1096. atomic_t *state;
  1097. if ((usc == NULL) || (usc->apr == NULL)) {
  1098. pr_err("%s: APR handle NULL\n", __func__);
  1099. return -EINVAL;
  1100. }
  1101. q6usm_add_hdr(usc, &hdr, sizeof(hdr), true);
  1102. switch (cmd) {
  1103. case CMD_CLOSE:
  1104. hdr.opcode = USM_STREAM_CMD_CLOSE;
  1105. state = &usc->cmd_state;
  1106. break;
  1107. default:
  1108. pr_err("%s:Invalid format[%d]\n", __func__, cmd);
  1109. goto fail_cmd;
  1110. }
  1111. rc = apr_send_pkt(usc->apr, (uint32_t *) &hdr);
  1112. if (rc < 0) {
  1113. pr_err("%s: Command 0x%x failed\n", __func__, hdr.opcode);
  1114. goto fail_cmd;
  1115. }
  1116. rc = wait_event_timeout(usc->cmd_wait, (atomic_read(state) == 0),
  1117. Q6USM_TIMEOUT_JIFFIES);
  1118. if (!rc) {
  1119. rc = -ETIME;
  1120. pr_err("%s:timeout. waited for response opcode[0x%x]\n",
  1121. __func__, hdr.opcode);
  1122. } else
  1123. rc = 0;
  1124. fail_cmd:
  1125. return rc;
  1126. }
  1127. int q6usm_set_us_detection(struct us_client *usc,
  1128. struct usm_session_cmd_detect_info *detect_info,
  1129. uint16_t detect_info_size)
  1130. {
  1131. int rc = 0;
  1132. if ((usc == NULL) ||
  1133. (detect_info_size == 0) ||
  1134. (detect_info == NULL)) {
  1135. pr_err("%s: wrong input: usc=0x%pK, inf_size=%d; info=0x%pK",
  1136. __func__,
  1137. usc,
  1138. detect_info_size,
  1139. detect_info);
  1140. return -EINVAL;
  1141. }
  1142. q6usm_add_hdr(usc, &detect_info->hdr, detect_info_size, true);
  1143. detect_info->hdr.opcode = USM_SESSION_CMD_SIGNAL_DETECT_MODE;
  1144. rc = apr_send_pkt(usc->apr, (uint32_t *)detect_info);
  1145. if (rc < 0) {
  1146. pr_err("%s:Comamnd signal detect failed\n", __func__);
  1147. return -EINVAL;
  1148. }
  1149. rc = wait_event_timeout(usc->cmd_wait,
  1150. (atomic_read(&usc->cmd_state) == 0),
  1151. Q6USM_TIMEOUT_JIFFIES);
  1152. if (!rc) {
  1153. rc = -ETIME;
  1154. pr_err("%s: CMD_SIGNAL_DETECT_MODE: timeout=%d\n",
  1155. __func__, Q6USM_TIMEOUT_JIFFIES);
  1156. } else
  1157. rc = 0;
  1158. return rc;
  1159. }
  1160. int q6usm_set_us_stream_param(int dir, struct us_client *usc,
  1161. uint32_t module_id, uint32_t param_id, uint32_t buf_size)
  1162. {
  1163. int rc = 0;
  1164. struct usm_stream_cmd_set_param cmd_set_param;
  1165. struct us_port_data *port = NULL;
  1166. if ((usc == NULL) || (usc->apr == NULL)) {
  1167. pr_err("%s: APR handle NULL\n", __func__);
  1168. return -EINVAL;
  1169. }
  1170. port = &usc->port[dir];
  1171. q6usm_add_hdr(usc, &cmd_set_param.hdr, sizeof(cmd_set_param), true);
  1172. cmd_set_param.hdr.opcode = USM_STREAM_CMD_SET_PARAM;
  1173. cmd_set_param.buf_size = buf_size;
  1174. cmd_set_param.buf_addr_msw =
  1175. msm_audio_populate_upper_32_bits(port->param_phys);
  1176. cmd_set_param.buf_addr_lsw = lower_32_bits(port->param_phys);
  1177. cmd_set_param.mem_map_handle =
  1178. *((uint32_t *)(port->param_buf_mem_handle));
  1179. cmd_set_param.module_id = module_id;
  1180. cmd_set_param.param_id = param_id;
  1181. cmd_set_param.hdr.token = 0;
  1182. rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_set_param);
  1183. if (rc < 0) {
  1184. pr_err("%s:write op[0x%x];rc[%d]\n",
  1185. __func__, cmd_set_param.hdr.opcode, rc);
  1186. }
  1187. rc = wait_event_timeout(usc->cmd_wait,
  1188. (atomic_read(&usc->cmd_state) == 0),
  1189. Q6USM_TIMEOUT_JIFFIES);
  1190. if (!rc) {
  1191. rc = -ETIME;
  1192. pr_err("%s: CMD_SET_PARAM: timeout=%d\n",
  1193. __func__, Q6USM_TIMEOUT_JIFFIES);
  1194. } else
  1195. rc = 0;
  1196. return rc;
  1197. }
  1198. int q6usm_get_us_stream_param(int dir, struct us_client *usc,
  1199. uint32_t module_id, uint32_t param_id, uint32_t buf_size)
  1200. {
  1201. int rc = 0;
  1202. struct usm_stream_cmd_get_param cmd_get_param;
  1203. struct us_port_data *port = NULL;
  1204. if ((usc == NULL) || (usc->apr == NULL)) {
  1205. pr_err("%s: APR handle NULL\n", __func__);
  1206. return -EINVAL;
  1207. }
  1208. port = &usc->port[dir];
  1209. q6usm_add_hdr(usc, &cmd_get_param.hdr, sizeof(cmd_get_param), true);
  1210. cmd_get_param.hdr.opcode = USM_STREAM_CMD_GET_PARAM;
  1211. cmd_get_param.buf_size = buf_size;
  1212. cmd_get_param.buf_addr_msw =
  1213. msm_audio_populate_upper_32_bits(port->param_phys);
  1214. cmd_get_param.buf_addr_lsw = lower_32_bits(port->param_phys);
  1215. cmd_get_param.mem_map_handle =
  1216. *((uint32_t *)(port->param_buf_mem_handle));
  1217. cmd_get_param.module_id = module_id;
  1218. cmd_get_param.param_id = param_id;
  1219. cmd_get_param.hdr.token = 0;
  1220. rc = apr_send_pkt(usc->apr, (uint32_t *) &cmd_get_param);
  1221. if (rc < 0) {
  1222. pr_err("%s:write op[0x%x];rc[%d]\n",
  1223. __func__, cmd_get_param.hdr.opcode, rc);
  1224. }
  1225. rc = wait_event_timeout(usc->cmd_wait,
  1226. (atomic_read(&usc->cmd_state) == 0),
  1227. Q6USM_TIMEOUT_JIFFIES);
  1228. if (!rc) {
  1229. rc = -ETIME;
  1230. pr_err("%s: CMD_GET_PARAM: timeout=%d\n",
  1231. __func__, Q6USM_TIMEOUT_JIFFIES);
  1232. } else
  1233. rc = 0;
  1234. return rc;
  1235. }
  1236. static int __init q6usm_init(void)
  1237. {
  1238. pr_debug("%s\n", __func__);
  1239. init_waitqueue_head(&this_mmap.cmd_wait);
  1240. memset(session, 0, sizeof(session));
  1241. return 0;
  1242. }
  1243. device_initcall(q6usm_init);