audio_utils_aio.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2008 Google, Inc.
  3. * Copyright (C) 2008 HTC Corporation
  4. * Copyright (c) 2009-2019, The Linux Foundation. All rights reserved.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/sched.h>
  21. #include <linux/wait.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/slab.h>
  24. #include <linux/atomic.h>
  25. #include <asm/ioctls.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/compat.h>
  28. #include <dsp/msm_audio_ion.h>
  29. #include "audio_utils_aio.h"
  30. #ifdef CONFIG_USE_DEV_CTRL_VOLUME
  31. #include <linux/qdsp6v2/audio_dev_ctl.h>
  32. #endif /*CONFIG_USE_DEV_CTRL_VOLUME*/
  33. static DEFINE_MUTEX(lock);
  34. #ifdef CONFIG_DEBUG_FS
  35. int audio_aio_debug_open(struct inode *inode, struct file *file)
  36. {
  37. file->private_data = inode->i_private;
  38. return 0;
  39. }
  40. ssize_t audio_aio_debug_read(struct file *file, char __user *buf,
  41. size_t count, loff_t *ppos)
  42. {
  43. const int debug_bufmax = 4096;
  44. static char buffer[4096];
  45. int n = 0;
  46. struct q6audio_aio *audio;
  47. mutex_lock(&lock);
  48. if (file->private_data != NULL) {
  49. audio = file->private_data;
  50. mutex_lock(&audio->lock);
  51. n = scnprintf(buffer, debug_bufmax, "opened %d\n",
  52. audio->opened);
  53. n += scnprintf(buffer + n, debug_bufmax - n,
  54. "enabled %d\n", audio->enabled);
  55. n += scnprintf(buffer + n, debug_bufmax - n,
  56. "stopped %d\n", audio->stopped);
  57. n += scnprintf(buffer + n, debug_bufmax - n,
  58. "feedback %d\n", audio->feedback);
  59. mutex_unlock(&audio->lock);
  60. /* Following variables are only useful for debugging when
  61. * when playback halts unexpectedly. Thus, no mutual exclusion
  62. * enforced
  63. */
  64. n += scnprintf(buffer + n, debug_bufmax - n,
  65. "wflush %d\n", audio->wflush);
  66. n += scnprintf(buffer + n, debug_bufmax - n,
  67. "rflush %d\n", audio->rflush);
  68. n += scnprintf(buffer + n, debug_bufmax - n,
  69. "inqueue empty %d\n",
  70. list_empty(&audio->in_queue));
  71. n += scnprintf(buffer + n, debug_bufmax - n,
  72. "outqueue empty %d\n",
  73. list_empty(&audio->out_queue));
  74. }
  75. mutex_unlock(&lock);
  76. buffer[n] = 0;
  77. return simple_read_from_buffer(buf, count, ppos, buffer, n);
  78. }
  79. #endif
  80. static long audio_aio_ioctl(struct file *file, unsigned int cmd,
  81. unsigned long arg);
  82. #ifdef CONFIG_COMPAT
  83. static long audio_aio_compat_ioctl(struct file *file, unsigned int cmd,
  84. unsigned long arg);
  85. #else
  86. #define audio_aio_compat_ioctl NULL
  87. #endif
  88. int insert_eos_buf(struct q6audio_aio *audio,
  89. struct audio_aio_buffer_node *buf_node)
  90. {
  91. struct dec_meta_out *eos_buf = buf_node->kvaddr;
  92. pr_debug("%s[%pK]:insert_eos_buf\n", __func__, audio);
  93. eos_buf->num_of_frames = 0xFFFFFFFF;
  94. eos_buf->meta_out_dsp[0].offset_to_frame = 0x0;
  95. eos_buf->meta_out_dsp[0].nflags = AUDIO_DEC_EOS_SET;
  96. return sizeof(struct dec_meta_out) +
  97. sizeof(eos_buf->meta_out_dsp[0]);
  98. }
  99. /* Routine which updates read buffers of driver/dsp,
  100. * for flush operation as DSP output might not have proper
  101. * value set
  102. */
  103. static int insert_meta_data_flush(struct q6audio_aio *audio,
  104. struct audio_aio_buffer_node *buf_node)
  105. {
  106. struct dec_meta_out *meta_data = buf_node->kvaddr;
  107. meta_data->num_of_frames = 0x0;
  108. meta_data->meta_out_dsp[0].offset_to_frame = 0x0;
  109. meta_data->meta_out_dsp[0].nflags = 0x0;
  110. return sizeof(struct dec_meta_out) +
  111. sizeof(meta_data->meta_out_dsp[0]);
  112. }
  113. static int audio_aio_ion_lookup_vaddr(struct q6audio_aio *audio, void *addr,
  114. unsigned long len,
  115. struct audio_aio_ion_region **region)
  116. {
  117. struct audio_aio_ion_region *region_elt;
  118. int match_count = 0;
  119. *region = NULL;
  120. /* returns physical address or zero */
  121. list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
  122. if (addr >= region_elt->vaddr &&
  123. addr < region_elt->vaddr + region_elt->len &&
  124. addr + len <= region_elt->vaddr + region_elt->len &&
  125. addr + len > addr) {
  126. /* to avoid integer addition overflow */
  127. /* offset since we could pass vaddr inside a registered
  128. * ion buffer
  129. */
  130. match_count++;
  131. if (!*region)
  132. *region = region_elt;
  133. }
  134. }
  135. if (match_count > 1) {
  136. pr_err("%s[%pK]:multiple hits for vaddr %pK, len %ld\n",
  137. __func__, audio, addr, len);
  138. list_for_each_entry(region_elt, &audio->ion_region_queue,
  139. list) {
  140. if (addr >= region_elt->vaddr &&
  141. addr < region_elt->vaddr + region_elt->len &&
  142. addr + len <= region_elt->vaddr + region_elt->len &&
  143. addr + len > addr)
  144. pr_err("\t%s[%pK]:%pK, %ld --> %pK\n",
  145. __func__, audio,
  146. region_elt->vaddr,
  147. region_elt->len,
  148. &region_elt->paddr);
  149. }
  150. }
  151. return *region ? 0 : -1;
  152. }
  153. static phys_addr_t audio_aio_ion_fixup(struct q6audio_aio *audio, void *addr,
  154. unsigned long len, int ref_up, void **kvaddr)
  155. {
  156. struct audio_aio_ion_region *region;
  157. phys_addr_t paddr;
  158. int ret;
  159. ret = audio_aio_ion_lookup_vaddr(audio, addr, len, &region);
  160. if (ret) {
  161. pr_err("%s[%pK]:lookup (%pK, %ld) failed\n",
  162. __func__, audio, addr, len);
  163. return 0;
  164. }
  165. if (ref_up)
  166. region->ref_cnt++;
  167. else
  168. region->ref_cnt--;
  169. pr_debug("%s[%pK]:found region %pK ref_cnt %d\n",
  170. __func__, audio, region, region->ref_cnt);
  171. paddr = region->paddr + (addr - region->vaddr);
  172. /* provide kernel virtual address for accessing meta information */
  173. if (kvaddr)
  174. *kvaddr = (void *) (region->kvaddr + (addr - region->vaddr));
  175. return paddr;
  176. }
  177. static int audio_aio_pause(struct q6audio_aio *audio)
  178. {
  179. int rc = -EINVAL;
  180. pr_debug("%s[%pK], enabled = %d\n", __func__, audio,
  181. audio->enabled);
  182. if (audio->enabled) {
  183. rc = q6asm_cmd(audio->ac, CMD_PAUSE);
  184. if (rc < 0)
  185. pr_err_ratelimited("%s[%pK]: pause cmd failed rc=%d\n",
  186. __func__, audio, rc);
  187. if (rc == 0) {
  188. /* Send suspend only if pause was successful */
  189. rc = q6asm_cmd(audio->ac, CMD_SUSPEND);
  190. if (rc < 0)
  191. pr_err_ratelimited("%s[%pK]: suspend cmd failed rc=%d\n",
  192. __func__, audio, rc);
  193. } else
  194. pr_err_ratelimited("%s[%pK]: not sending suspend since pause failed\n",
  195. __func__, audio);
  196. } else
  197. pr_err("%s[%pK]: Driver not enabled\n", __func__, audio);
  198. return rc;
  199. }
  200. static int audio_aio_flush(struct q6audio_aio *audio)
  201. {
  202. int rc = 0;
  203. if (audio->enabled) {
  204. /* Implicitly issue a pause to the decoder before flushing if
  205. * it is not in pause state
  206. */
  207. if (!(audio->drv_status & ADRV_STATUS_PAUSE)) {
  208. rc = audio_aio_pause(audio);
  209. if (rc < 0)
  210. pr_err_ratelimited("%s[%pK}: pause cmd failed rc=%d\n",
  211. __func__, audio,
  212. rc);
  213. else
  214. audio->drv_status |= ADRV_STATUS_PAUSE;
  215. }
  216. rc = q6asm_cmd(audio->ac, CMD_FLUSH);
  217. if (rc < 0)
  218. pr_err_ratelimited("%s[%pK]: flush cmd failed rc=%d\n",
  219. __func__, audio, rc);
  220. /* Not in stop state, reenable the stream */
  221. if (audio->stopped == 0) {
  222. rc = audio_aio_enable(audio);
  223. if (rc)
  224. pr_err_ratelimited("%s[%pK]:audio re-enable failed\n",
  225. __func__, audio);
  226. else {
  227. audio->enabled = 1;
  228. if (audio->drv_status & ADRV_STATUS_PAUSE)
  229. audio->drv_status &= ~ADRV_STATUS_PAUSE;
  230. }
  231. }
  232. }
  233. pr_debug("%s[%pK]:in_bytes %d\n",
  234. __func__, audio, atomic_read(&audio->in_bytes));
  235. pr_debug("%s[%pK]:in_samples %d\n",
  236. __func__, audio, atomic_read(&audio->in_samples));
  237. atomic_set(&audio->in_bytes, 0);
  238. atomic_set(&audio->in_samples, 0);
  239. return rc;
  240. }
  241. static int audio_aio_outport_flush(struct q6audio_aio *audio)
  242. {
  243. int rc;
  244. rc = q6asm_cmd(audio->ac, CMD_OUT_FLUSH);
  245. if (rc < 0)
  246. pr_err_ratelimited("%s[%pK}: output port flush cmd failed rc=%d\n",
  247. __func__, audio, rc);
  248. return rc;
  249. }
  250. /* Write buffer to DSP / Handle Ack from DSP */
  251. void audio_aio_async_write_ack(struct q6audio_aio *audio, uint32_t token,
  252. uint32_t *payload)
  253. {
  254. unsigned long flags;
  255. union msm_audio_event_payload event_payload;
  256. struct audio_aio_buffer_node *used_buf;
  257. /* No active flush in progress */
  258. if (audio->wflush)
  259. return;
  260. spin_lock_irqsave(&audio->dsp_lock, flags);
  261. if (list_empty(&audio->out_queue)) {
  262. pr_warn("%s: ignore unexpected event from dsp\n", __func__);
  263. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  264. return;
  265. }
  266. used_buf = list_first_entry(&audio->out_queue,
  267. struct audio_aio_buffer_node, list);
  268. if (token == used_buf->token) {
  269. list_del(&used_buf->list);
  270. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  271. pr_debug("%s[%pK]:consumed buffer\n", __func__, audio);
  272. event_payload.aio_buf = used_buf->buf;
  273. audio_aio_post_event(audio, AUDIO_EVENT_WRITE_DONE,
  274. event_payload);
  275. kfree(used_buf);
  276. if (list_empty(&audio->out_queue) &&
  277. (audio->drv_status & ADRV_STATUS_FSYNC)) {
  278. pr_debug("%s[%pK]: list is empty, reached EOS in Tunnel\n",
  279. __func__, audio);
  280. wake_up(&audio->write_wait);
  281. }
  282. } else {
  283. pr_err("%s[%pK]:expected=%x ret=%x\n",
  284. __func__, audio, used_buf->token, token);
  285. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  286. }
  287. }
  288. /* ------------------- device --------------------- */
  289. void audio_aio_async_out_flush(struct q6audio_aio *audio)
  290. {
  291. struct audio_aio_buffer_node *buf_node;
  292. struct list_head *ptr, *next;
  293. union msm_audio_event_payload payload;
  294. unsigned long flags;
  295. pr_debug("%s[%pK}\n", __func__, audio);
  296. /* EOS followed by flush, EOS response not guranteed, free EOS i/p
  297. * buffer
  298. */
  299. spin_lock_irqsave(&audio->dsp_lock, flags);
  300. if (audio->eos_flag && (audio->eos_write_payload.aio_buf.buf_addr)) {
  301. pr_debug("%s[%pK]: EOS followed by flush received,acknowledge eos i/p buffer immediately\n",
  302. __func__, audio);
  303. audio_aio_post_event(audio, AUDIO_EVENT_WRITE_DONE,
  304. audio->eos_write_payload);
  305. memset(&audio->eos_write_payload, 0,
  306. sizeof(union msm_audio_event_payload));
  307. }
  308. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  309. list_for_each_safe(ptr, next, &audio->out_queue) {
  310. buf_node = list_entry(ptr, struct audio_aio_buffer_node, list);
  311. list_del(&buf_node->list);
  312. payload.aio_buf = buf_node->buf;
  313. audio_aio_post_event(audio, AUDIO_EVENT_WRITE_DONE, payload);
  314. kfree(buf_node);
  315. pr_debug("%s[%pK]: Propagate WRITE_DONE during flush\n",
  316. __func__, audio);
  317. }
  318. }
  319. void audio_aio_async_in_flush(struct q6audio_aio *audio)
  320. {
  321. struct audio_aio_buffer_node *buf_node;
  322. struct list_head *ptr, *next;
  323. union msm_audio_event_payload payload;
  324. pr_debug("%s[%pK]\n", __func__, audio);
  325. list_for_each_safe(ptr, next, &audio->in_queue) {
  326. buf_node = list_entry(ptr, struct audio_aio_buffer_node, list);
  327. list_del(&buf_node->list);
  328. /* Forcefull send o/p eos buffer after flush, if no eos response
  329. * received by dsp even after sending eos command
  330. */
  331. if ((audio->eos_rsp != 1) && audio->eos_flag) {
  332. pr_debug("%s[%pK]: send eos on o/p buffer during flush\n",
  333. __func__, audio);
  334. payload.aio_buf = buf_node->buf;
  335. payload.aio_buf.data_len =
  336. insert_eos_buf(audio, buf_node);
  337. audio->eos_flag = 0;
  338. } else {
  339. payload.aio_buf = buf_node->buf;
  340. payload.aio_buf.data_len =
  341. insert_meta_data_flush(audio, buf_node);
  342. }
  343. audio_aio_post_event(audio, AUDIO_EVENT_READ_DONE, payload);
  344. kfree(buf_node);
  345. pr_debug("%s[%pK]: Propagate READ_DONE during flush\n",
  346. __func__, audio);
  347. }
  348. }
  349. int audio_aio_enable(struct q6audio_aio *audio)
  350. {
  351. /* 2nd arg: 0 -> run immediately
  352. * 3rd arg: 0 -> msw_ts,
  353. * 4th arg: 0 ->lsw_ts
  354. */
  355. return q6asm_run(audio->ac, 0x00, 0x00, 0x00);
  356. }
  357. int audio_aio_disable(struct q6audio_aio *audio)
  358. {
  359. int rc = 0;
  360. if (audio->opened) {
  361. audio->enabled = 0;
  362. audio->opened = 0;
  363. pr_debug("%s[%pK]: inbytes[%d] insamples[%d]\n", __func__,
  364. audio, atomic_read(&audio->in_bytes),
  365. atomic_read(&audio->in_samples));
  366. /* Close the session */
  367. rc = q6asm_cmd(audio->ac, CMD_CLOSE);
  368. if (rc < 0)
  369. pr_err_ratelimited("%s[%pK]:Failed to close the session rc=%d\n",
  370. __func__, audio, rc);
  371. audio->stopped = 1;
  372. wake_up(&audio->write_wait);
  373. wake_up(&audio->cmd_wait);
  374. }
  375. pr_debug("%s[%pK]:enabled[%d]\n", __func__, audio, audio->enabled);
  376. return rc;
  377. }
  378. void audio_aio_reset_ion_region(struct q6audio_aio *audio)
  379. {
  380. struct audio_aio_ion_region *region;
  381. struct list_head *ptr, *next;
  382. list_for_each_safe(ptr, next, &audio->ion_region_queue) {
  383. region = list_entry(ptr, struct audio_aio_ion_region, list);
  384. list_del(&region->list);
  385. msm_audio_ion_free(region->dma_buf);
  386. kfree(region);
  387. }
  388. }
  389. void audio_aio_reset_event_queue(struct q6audio_aio *audio)
  390. {
  391. unsigned long flags;
  392. struct audio_aio_event *drv_evt;
  393. struct list_head *ptr, *next;
  394. spin_lock_irqsave(&audio->event_queue_lock, flags);
  395. list_for_each_safe(ptr, next, &audio->event_queue) {
  396. drv_evt = list_first_entry(&audio->event_queue,
  397. struct audio_aio_event, list);
  398. list_del(&drv_evt->list);
  399. kfree(drv_evt);
  400. }
  401. list_for_each_safe(ptr, next, &audio->free_event_queue) {
  402. drv_evt = list_first_entry(&audio->free_event_queue,
  403. struct audio_aio_event, list);
  404. list_del(&drv_evt->list);
  405. kfree(drv_evt);
  406. }
  407. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  408. }
  409. static void audio_aio_unmap_ion_region(struct q6audio_aio *audio)
  410. {
  411. struct audio_aio_ion_region *region;
  412. struct list_head *ptr, *next;
  413. int rc = -EINVAL;
  414. pr_debug("%s[%pK]:\n", __func__, audio);
  415. list_for_each_safe(ptr, next, &audio->ion_region_queue) {
  416. region = list_entry(ptr, struct audio_aio_ion_region, list);
  417. if (region != NULL) {
  418. pr_debug("%s[%pK]: phy_address = 0x%pK\n",
  419. __func__, audio, &region->paddr);
  420. rc = q6asm_memory_unmap(audio->ac,
  421. region->paddr, IN);
  422. if (rc < 0)
  423. pr_err("%s[%pK]: memory unmap failed\n",
  424. __func__, audio);
  425. }
  426. }
  427. }
  428. #ifdef CONFIG_USE_DEV_CTRL_VOLUME
  429. static void audio_aio_listner(u32 evt_id, union auddev_evt_data *evt_payload,
  430. void *private_data)
  431. {
  432. struct q6audio_aio *audio = (struct q6audio_aio *) private_data;
  433. int rc = 0;
  434. switch (evt_id) {
  435. case AUDDEV_EVT_STREAM_VOL_CHG:
  436. audio->volume = evt_payload->session_vol;
  437. pr_debug("%s[%pK]: AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d, enabled = %d\n",
  438. __func__, audio, audio->volume, audio->enabled);
  439. if (audio->enabled == 1) {
  440. if (audio->ac) {
  441. rc = q6asm_set_volume(audio->ac, audio->volume);
  442. if (rc < 0) {
  443. pr_err("%s[%pK]: Send Volume command failed rc=%d\n",
  444. __func__, audio, rc);
  445. }
  446. }
  447. }
  448. break;
  449. default:
  450. pr_err("%s[%pK]:ERROR:wrong event\n", __func__, audio);
  451. break;
  452. }
  453. }
  454. int register_volume_listener(struct q6audio_aio *audio)
  455. {
  456. int rc = 0;
  457. audio->device_events = AUDDEV_EVT_STREAM_VOL_CHG;
  458. audio->drv_status &= ~ADRV_STATUS_PAUSE;
  459. rc = auddev_register_evt_listner(audio->device_events,
  460. AUDDEV_CLNT_DEC,
  461. audio->ac->session,
  462. audio_aio_listner,
  463. (void *)audio);
  464. if (rc < 0) {
  465. pr_err("%s[%pK]: Event listener failed\n", __func__, audio);
  466. rc = -EACCES;
  467. }
  468. return rc;
  469. }
  470. void unregister_volume_listener(struct q6audio_aio *audio)
  471. {
  472. auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->ac->session);
  473. }
  474. int enable_volume_ramp(struct q6audio_aio *audio)
  475. {
  476. int rc = 0;
  477. struct asm_softpause_params softpause;
  478. struct asm_softvolume_params softvol;
  479. if (audio->ac == NULL)
  480. return -EINVAL;
  481. pr_debug("%s[%pK]\n", __func__, audio);
  482. softpause.enable = SOFT_PAUSE_ENABLE;
  483. softpause.period = SOFT_PAUSE_PERIOD;
  484. softpause.step = SOFT_PAUSE_STEP;
  485. softpause.rampingcurve = SOFT_PAUSE_CURVE_LINEAR;
  486. softvol.period = SOFT_VOLUME_PERIOD;
  487. softvol.step = SOFT_VOLUME_STEP;
  488. softvol.rampingcurve = SOFT_VOLUME_CURVE_LINEAR;
  489. if (softpause.rampingcurve == SOFT_PAUSE_CURVE_LINEAR)
  490. softpause.step = SOFT_PAUSE_STEP_LINEAR;
  491. if (softvol.rampingcurve == SOFT_VOLUME_CURVE_LINEAR)
  492. softvol.step = SOFT_VOLUME_STEP_LINEAR;
  493. rc = q6asm_set_volume(audio->ac, audio->volume);
  494. if (rc < 0) {
  495. pr_err("%s: Send Volume command failed rc=%d\n",
  496. __func__, rc);
  497. return rc;
  498. }
  499. rc = q6asm_set_softpause(audio->ac, &softpause);
  500. if (rc < 0) {
  501. pr_err("%s: Send SoftPause Param failed rc=%d\n",
  502. __func__, rc);
  503. return rc;
  504. }
  505. rc = q6asm_set_softvolume(audio->ac, &softvol);
  506. if (rc < 0) {
  507. pr_err("%s: Send SoftVolume Param failed rc=%d\n",
  508. __func__, rc);
  509. return rc;
  510. }
  511. /* disable mute by default */
  512. rc = q6asm_set_mute(audio->ac, 0);
  513. if (rc < 0) {
  514. pr_err("%s: Send mute command failed rc=%d\n",
  515. __func__, rc);
  516. return rc;
  517. }
  518. return rc;
  519. }
  520. #else /*CONFIG_USE_DEV_CTRL_VOLUME*/
  521. int register_volume_listener(struct q6audio_aio *audio)
  522. {
  523. return 0;/* do nothing */
  524. }
  525. void unregister_volume_listener(struct q6audio_aio *audio)
  526. {
  527. return;/* do nothing */
  528. }
  529. int enable_volume_ramp(struct q6audio_aio *audio)
  530. {
  531. return 0; /* do nothing */
  532. }
  533. #endif /*CONFIG_USE_DEV_CTRL_VOLUME*/
  534. int audio_aio_release(struct inode *inode, struct file *file)
  535. {
  536. struct q6audio_aio *audio = file->private_data;
  537. pr_debug("%s[%pK]\n", __func__, audio);
  538. mutex_lock(&lock);
  539. mutex_lock(&audio->lock);
  540. mutex_lock(&audio->read_lock);
  541. mutex_lock(&audio->write_lock);
  542. audio->wflush = 1;
  543. if (audio->wakelock_voted &&
  544. (audio->audio_ws_mgr != NULL) &&
  545. (audio->miscdevice != NULL)) {
  546. audio->wakelock_voted = false;
  547. mutex_lock(&audio->audio_ws_mgr->ws_lock);
  548. if ((audio->audio_ws_mgr->ref_cnt > 0) &&
  549. (--audio->audio_ws_mgr->ref_cnt == 0)) {
  550. pm_relax(audio->miscdevice->this_device);
  551. }
  552. mutex_unlock(&audio->audio_ws_mgr->ws_lock);
  553. }
  554. if (audio->enabled)
  555. audio_aio_flush(audio);
  556. audio->wflush = 0;
  557. audio->drv_ops.out_flush(audio);
  558. audio->drv_ops.in_flush(audio);
  559. audio_aio_disable(audio);
  560. audio_aio_unmap_ion_region(audio);
  561. audio_aio_reset_ion_region(audio);
  562. audio->event_abort = 1;
  563. wake_up(&audio->event_wait);
  564. audio_aio_reset_event_queue(audio);
  565. q6asm_audio_client_free(audio->ac);
  566. mutex_unlock(&audio->write_lock);
  567. mutex_unlock(&audio->read_lock);
  568. mutex_unlock(&audio->lock);
  569. mutex_destroy(&audio->lock);
  570. mutex_destroy(&audio->read_lock);
  571. mutex_destroy(&audio->write_lock);
  572. mutex_destroy(&audio->get_event_lock);
  573. unregister_volume_listener(audio);
  574. #ifdef CONFIG_DEBUG_FS
  575. debugfs_remove(audio->dentry);
  576. #endif
  577. kfree(audio->codec_cfg);
  578. kfree(audio);
  579. file->private_data = NULL;
  580. mutex_unlock(&lock);
  581. return 0;
  582. }
  583. int audio_aio_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  584. {
  585. int rc = 0;
  586. struct q6audio_aio *audio = file->private_data;
  587. if (!audio->enabled || audio->feedback)
  588. return -EINVAL;
  589. /* Blocking client sends more data */
  590. mutex_lock(&audio->lock);
  591. audio->drv_status |= ADRV_STATUS_FSYNC;
  592. mutex_unlock(&audio->lock);
  593. pr_debug("%s[%pK]:\n", __func__, audio);
  594. audio->eos_rsp = 0;
  595. pr_debug("%s[%pK]Wait for write done from DSP\n", __func__, audio);
  596. rc = wait_event_interruptible(audio->write_wait,
  597. (list_empty(&audio->out_queue)) ||
  598. audio->wflush || audio->stopped);
  599. if (audio->stopped || audio->wflush) {
  600. pr_debug("%s[%pK]: Audio Flushed or Stopped,this is not EOS\n"
  601. , __func__, audio);
  602. audio->wflush = 0;
  603. rc = -EBUSY;
  604. }
  605. if (rc < 0) {
  606. pr_err("%s[%pK]: wait event for list_empty failed, rc = %d\n",
  607. __func__, audio, rc);
  608. goto done;
  609. }
  610. rc = q6asm_cmd(audio->ac, CMD_EOS);
  611. pr_debug("%s[%pK]: EOS cmd sent to DSP\n", __func__, audio);
  612. if (rc < 0)
  613. pr_err_ratelimited("%s[%pK]: q6asm_cmd failed, rc = %d",
  614. __func__, audio, rc);
  615. pr_debug("%s[%pK]: wait for RENDERED_EOS from DSP\n"
  616. , __func__, audio);
  617. rc = wait_event_interruptible(audio->write_wait,
  618. (audio->eos_rsp || audio->wflush ||
  619. audio->stopped));
  620. if (rc < 0) {
  621. pr_err("%s[%pK]: wait event for eos_rsp failed, rc = %d\n",
  622. __func__, audio, rc);
  623. goto done;
  624. }
  625. if (audio->stopped || audio->wflush) {
  626. audio->wflush = 0;
  627. pr_debug("%s[%pK]: Audio Flushed or Stopped,this is not EOS\n"
  628. , __func__, audio);
  629. rc = -EBUSY;
  630. }
  631. if (audio->eos_rsp == 1)
  632. pr_debug("%s[%pK]: EOS\n", __func__, audio);
  633. done:
  634. mutex_lock(&audio->lock);
  635. audio->drv_status &= ~ADRV_STATUS_FSYNC;
  636. mutex_unlock(&audio->lock);
  637. return rc;
  638. }
  639. static int audio_aio_events_pending(struct q6audio_aio *audio)
  640. {
  641. unsigned long flags;
  642. int empty;
  643. spin_lock_irqsave(&audio->event_queue_lock, flags);
  644. empty = !list_empty(&audio->event_queue);
  645. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  646. return empty || audio->event_abort || audio->reset_event;
  647. }
  648. static long audio_aio_process_event_req_common(struct q6audio_aio *audio,
  649. struct msm_audio_event *usr_evt)
  650. {
  651. long rc;
  652. struct audio_aio_event *drv_evt = NULL;
  653. int timeout;
  654. unsigned long flags;
  655. timeout = usr_evt->timeout_ms;
  656. if (timeout > 0) {
  657. rc = wait_event_interruptible_timeout(audio->event_wait,
  658. audio_aio_events_pending
  659. (audio),
  660. msecs_to_jiffies
  661. (timeout));
  662. if (rc == 0)
  663. return -ETIMEDOUT;
  664. } else {
  665. rc = wait_event_interruptible(audio->event_wait,
  666. audio_aio_events_pending(audio));
  667. }
  668. if (rc < 0)
  669. return rc;
  670. if (audio->reset_event) {
  671. audio->reset_event = false;
  672. pr_err("In SSR, post ENETRESET err\n");
  673. return -ENETRESET;
  674. }
  675. if (audio->event_abort) {
  676. audio->event_abort = 0;
  677. return -ENODEV;
  678. }
  679. rc = 0;
  680. spin_lock_irqsave(&audio->event_queue_lock, flags);
  681. if (!list_empty(&audio->event_queue)) {
  682. drv_evt = list_first_entry(&audio->event_queue,
  683. struct audio_aio_event, list);
  684. list_del(&drv_evt->list);
  685. }
  686. if (drv_evt) {
  687. usr_evt->event_type = drv_evt->event_type;
  688. usr_evt->event_payload = drv_evt->payload;
  689. list_add_tail(&drv_evt->list, &audio->free_event_queue);
  690. } else {
  691. pr_err("%s[%pK]:Unexpected path\n", __func__, audio);
  692. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  693. return -EPERM;
  694. }
  695. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  696. if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE) {
  697. pr_debug("%s[%pK]:posted AUDIO_EVENT_WRITE_DONE to user\n",
  698. __func__, audio);
  699. mutex_lock(&audio->write_lock);
  700. audio_aio_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
  701. drv_evt->payload.aio_buf.buf_len, 0, 0);
  702. mutex_unlock(&audio->write_lock);
  703. } else if (drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
  704. pr_debug("%s[%pK]:posted AUDIO_EVENT_READ_DONE to user\n",
  705. __func__, audio);
  706. mutex_lock(&audio->read_lock);
  707. audio_aio_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
  708. drv_evt->payload.aio_buf.buf_len, 0, 0);
  709. mutex_unlock(&audio->read_lock);
  710. }
  711. /* Some read buffer might be held up in DSP,release all
  712. * Once EOS indicated
  713. */
  714. if (audio->eos_rsp && !list_empty(&audio->in_queue)) {
  715. pr_debug("%s[%pK]:Send flush command to release read buffers held up in DSP\n",
  716. __func__, audio);
  717. mutex_lock(&audio->lock);
  718. audio_aio_flush(audio);
  719. mutex_unlock(&audio->lock);
  720. }
  721. return rc;
  722. }
  723. static long audio_aio_process_event_req(struct q6audio_aio *audio,
  724. void __user *arg)
  725. {
  726. long rc;
  727. struct msm_audio_event usr_evt;
  728. if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event))) {
  729. pr_err("%s: copy_from_user failed\n", __func__);
  730. return -EFAULT;
  731. }
  732. rc = audio_aio_process_event_req_common(audio, &usr_evt);
  733. if (copy_to_user(arg, &usr_evt, sizeof(usr_evt))) {
  734. pr_err("%s: copy_to_user failed\n", __func__);
  735. rc = -EFAULT;
  736. }
  737. return rc;
  738. }
  739. #ifdef CONFIG_COMPAT
  740. struct msm_audio_aio_buf32 {
  741. compat_uptr_t buf_addr;
  742. u32 buf_len;
  743. u32 data_len;
  744. compat_uptr_t private_data;
  745. u16 mfield_sz; /*only useful for data has meta field */
  746. };
  747. struct msm_audio_bitstream_info32 {
  748. u32 codec_type;
  749. u32 chan_info;
  750. u32 sample_rate;
  751. u32 bit_stream_info;
  752. u32 bit_rate;
  753. u32 unused[3];
  754. };
  755. struct msm_audio_bitstream_error_info32 {
  756. u32 dec_id;
  757. u32 err_msg_indicator;
  758. u32 err_type;
  759. };
  760. union msm_audio_event_payload32 {
  761. struct msm_audio_aio_buf32 aio_buf;
  762. struct msm_audio_bitstream_info32 stream_info;
  763. struct msm_audio_bitstream_error_info32 error_info;
  764. s32 reserved;
  765. };
  766. struct msm_audio_event32 {
  767. s32 event_type;
  768. s32 timeout_ms;
  769. union msm_audio_event_payload32 event_payload;
  770. };
  771. static long audio_aio_process_event_req_compat(struct q6audio_aio *audio,
  772. void __user *arg)
  773. {
  774. long rc;
  775. struct msm_audio_event32 usr_evt_32;
  776. struct msm_audio_event usr_evt;
  777. memset(&usr_evt, 0, sizeof(struct msm_audio_event));
  778. if (copy_from_user(&usr_evt_32, arg,
  779. sizeof(struct msm_audio_event32))) {
  780. pr_err("%s: copy_from_user failed\n", __func__);
  781. return -EFAULT;
  782. }
  783. usr_evt.timeout_ms = usr_evt_32.timeout_ms;
  784. rc = audio_aio_process_event_req_common(audio, &usr_evt);
  785. if (rc < 0) {
  786. pr_err("%s: audio process event failed, rc = %ld",
  787. __func__, rc);
  788. return rc;
  789. }
  790. usr_evt_32.event_type = usr_evt.event_type;
  791. switch (usr_evt_32.event_type) {
  792. case AUDIO_EVENT_SUSPEND:
  793. case AUDIO_EVENT_RESUME:
  794. case AUDIO_EVENT_WRITE_DONE:
  795. case AUDIO_EVENT_READ_DONE:
  796. usr_evt_32.event_payload.aio_buf.buf_addr =
  797. ptr_to_compat(usr_evt.event_payload.aio_buf.buf_addr);
  798. usr_evt_32.event_payload.aio_buf.buf_len =
  799. usr_evt.event_payload.aio_buf.buf_len;
  800. usr_evt_32.event_payload.aio_buf.data_len =
  801. usr_evt.event_payload.aio_buf.data_len;
  802. usr_evt_32.event_payload.aio_buf.private_data =
  803. ptr_to_compat(usr_evt.event_payload.aio_buf.private_data);
  804. usr_evt_32.event_payload.aio_buf.mfield_sz =
  805. usr_evt.event_payload.aio_buf.mfield_sz;
  806. break;
  807. case AUDIO_EVENT_STREAM_INFO:
  808. usr_evt_32.event_payload.stream_info.codec_type =
  809. usr_evt.event_payload.stream_info.codec_type;
  810. usr_evt_32.event_payload.stream_info.chan_info =
  811. usr_evt.event_payload.stream_info.chan_info;
  812. usr_evt_32.event_payload.stream_info.sample_rate =
  813. usr_evt.event_payload.stream_info.sample_rate;
  814. usr_evt_32.event_payload.stream_info.bit_stream_info =
  815. usr_evt.event_payload.stream_info.bit_stream_info;
  816. usr_evt_32.event_payload.stream_info.bit_rate =
  817. usr_evt.event_payload.stream_info.bit_rate;
  818. break;
  819. case AUDIO_EVENT_BITSTREAM_ERROR_INFO:
  820. usr_evt_32.event_payload.error_info.dec_id =
  821. usr_evt.event_payload.error_info.dec_id;
  822. usr_evt_32.event_payload.error_info.err_msg_indicator =
  823. usr_evt.event_payload.error_info.err_msg_indicator;
  824. usr_evt_32.event_payload.error_info.err_type =
  825. usr_evt.event_payload.error_info.err_type;
  826. break;
  827. default:
  828. pr_debug("%s: unknown audio event type = %d rc = %ld",
  829. __func__, usr_evt_32.event_type, rc);
  830. return rc;
  831. }
  832. if (copy_to_user(arg, &usr_evt_32, sizeof(usr_evt_32))) {
  833. pr_err("%s: copy_to_user failed\n", __func__);
  834. rc = -EFAULT;
  835. }
  836. return rc;
  837. }
  838. #endif
  839. static int audio_aio_ion_check(struct q6audio_aio *audio,
  840. void *vaddr, unsigned long len)
  841. {
  842. struct audio_aio_ion_region *region_elt;
  843. struct audio_aio_ion_region t = {.vaddr = vaddr, .len = len };
  844. list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
  845. if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
  846. OVERLAPS(region_elt, &t)) {
  847. pr_err("%s[%pK]:region (vaddr %pK len %ld) clashes with registered region (vaddr %pK paddr %pK len %ld)\n",
  848. __func__, audio, vaddr, len,
  849. region_elt->vaddr,
  850. &region_elt->paddr, region_elt->len);
  851. return -EINVAL;
  852. }
  853. }
  854. return 0;
  855. }
  856. static int audio_aio_ion_add(struct q6audio_aio *audio,
  857. struct msm_audio_ion_info *info)
  858. {
  859. dma_addr_t paddr = 0;
  860. size_t len = 0;
  861. struct audio_aio_ion_region *region;
  862. int rc = -EINVAL;
  863. struct dma_buf *dma_buf = NULL;
  864. unsigned long ionflag;
  865. void *kvaddr = NULL;
  866. pr_debug("%s[%pK]:\n", __func__, audio);
  867. region = kmalloc(sizeof(*region), GFP_KERNEL);
  868. if (!region) {
  869. rc = -ENOMEM;
  870. goto end;
  871. }
  872. rc = msm_audio_ion_import(&dma_buf, info->fd, &ionflag,
  873. 0, &paddr, &len, &kvaddr);
  874. if (rc) {
  875. pr_err("%s: msm audio ion alloc failed\n", __func__);
  876. goto import_error;
  877. }
  878. rc = audio_aio_ion_check(audio, info->vaddr, len);
  879. if (rc < 0) {
  880. pr_err("%s: audio_aio_ion_check failed\n", __func__);
  881. goto ion_error;
  882. }
  883. region->dma_buf = dma_buf;
  884. region->vaddr = info->vaddr;
  885. region->fd = info->fd;
  886. region->paddr = paddr;
  887. region->kvaddr = kvaddr;
  888. region->len = len;
  889. region->ref_cnt = 0;
  890. pr_debug("%s[%pK]:add region paddr %pK vaddr %pK, len %lu kvaddr %pK\n",
  891. __func__, audio,
  892. &region->paddr, region->vaddr, region->len,
  893. region->kvaddr);
  894. list_add_tail(&region->list, &audio->ion_region_queue);
  895. rc = q6asm_memory_map(audio->ac, paddr, IN, len, 1);
  896. if (rc < 0) {
  897. pr_err("%s[%pK]: memory map failed\n", __func__, audio);
  898. goto mmap_error;
  899. } else {
  900. goto end;
  901. }
  902. mmap_error:
  903. list_del(&region->list);
  904. ion_error:
  905. msm_audio_ion_free(dma_buf);
  906. import_error:
  907. kfree(region);
  908. end:
  909. return rc;
  910. }
  911. static int audio_aio_ion_remove(struct q6audio_aio *audio,
  912. struct msm_audio_ion_info *info)
  913. {
  914. struct audio_aio_ion_region *region;
  915. struct list_head *ptr, *next;
  916. int rc = -EINVAL;
  917. pr_debug("%s[%pK]:info fd %d vaddr %pK\n",
  918. __func__, audio, info->fd, info->vaddr);
  919. list_for_each_safe(ptr, next, &audio->ion_region_queue) {
  920. region = list_entry(ptr, struct audio_aio_ion_region, list);
  921. if ((region->fd == info->fd) &&
  922. (region->vaddr == info->vaddr)) {
  923. if (region->ref_cnt) {
  924. pr_debug("%s[%pK]:region %pK in use ref_cnt %d\n",
  925. __func__, audio, region,
  926. region->ref_cnt);
  927. break;
  928. }
  929. pr_debug("%s[%pK]:remove region fd %d vaddr %pK\n",
  930. __func__, audio, info->fd, info->vaddr);
  931. rc = q6asm_memory_unmap(audio->ac,
  932. region->paddr, IN);
  933. if (rc < 0)
  934. pr_err("%s[%pK]: memory unmap failed\n",
  935. __func__, audio);
  936. list_del(&region->list);
  937. msm_audio_ion_free(region->dma_buf);
  938. kfree(region);
  939. rc = 0;
  940. break;
  941. }
  942. }
  943. return rc;
  944. }
  945. static int audio_aio_async_write(struct q6audio_aio *audio,
  946. struct audio_aio_buffer_node *buf_node)
  947. {
  948. int rc;
  949. struct audio_client *ac;
  950. struct audio_aio_write_param param;
  951. memset(&param, 0, sizeof(param));
  952. if (!audio || !buf_node) {
  953. pr_err("%s NULL pointer audio=[0x%pK], buf_node=[0x%pK]\n",
  954. __func__, audio, buf_node);
  955. return -EINVAL;
  956. }
  957. pr_debug("%s[%pK]: Send write buff %pK phy %pK len %d meta_enable = %d\n",
  958. __func__, audio, buf_node, &buf_node->paddr,
  959. buf_node->buf.data_len,
  960. audio->buf_cfg.meta_info_enable);
  961. pr_debug("%s[%pK]: flags = 0x%x\n", __func__, audio,
  962. buf_node->meta_info.meta_in.nflags);
  963. ac = audio->ac;
  964. /* Offset with appropriate meta */
  965. if (audio->feedback) {
  966. /* Non Tunnel mode */
  967. param.paddr = buf_node->paddr + sizeof(struct dec_meta_in);
  968. param.len = buf_node->buf.data_len - sizeof(struct dec_meta_in);
  969. } else {
  970. /* Tunnel mode */
  971. param.paddr = buf_node->paddr;
  972. param.len = buf_node->buf.data_len;
  973. }
  974. param.msw_ts = buf_node->meta_info.meta_in.ntimestamp.highpart;
  975. param.lsw_ts = buf_node->meta_info.meta_in.ntimestamp.lowpart;
  976. param.flags = buf_node->meta_info.meta_in.nflags;
  977. /* If no meta_info enaled, indicate no time stamp valid */
  978. if (!audio->buf_cfg.meta_info_enable)
  979. param.flags = 0xFF00;
  980. if (buf_node->meta_info.meta_in.nflags & AUDIO_DEC_EOF_SET)
  981. param.flags |= AUDIO_DEC_EOF_SET;
  982. param.uid = ac->session;
  983. /* Read command will populate session id as token */
  984. buf_node->token = ac->session;
  985. rc = q6asm_async_write(ac, &param);
  986. if (rc < 0)
  987. pr_err_ratelimited("%s[%pK]:failed\n", __func__, audio);
  988. return rc;
  989. }
  990. void audio_aio_post_event(struct q6audio_aio *audio, int type,
  991. union msm_audio_event_payload payload)
  992. {
  993. struct audio_aio_event *e_node = NULL;
  994. unsigned long flags;
  995. spin_lock_irqsave(&audio->event_queue_lock, flags);
  996. if (!list_empty(&audio->free_event_queue)) {
  997. e_node = list_first_entry(&audio->free_event_queue,
  998. struct audio_aio_event, list);
  999. list_del(&e_node->list);
  1000. } else {
  1001. e_node = kmalloc(sizeof(struct audio_aio_event), GFP_ATOMIC);
  1002. if (!e_node) {
  1003. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  1004. return;
  1005. }
  1006. }
  1007. e_node->event_type = type;
  1008. e_node->payload = payload;
  1009. list_add_tail(&e_node->list, &audio->event_queue);
  1010. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  1011. wake_up(&audio->event_wait);
  1012. }
  1013. static int audio_aio_async_read(struct q6audio_aio *audio,
  1014. struct audio_aio_buffer_node *buf_node)
  1015. {
  1016. struct audio_client *ac;
  1017. struct audio_aio_read_param param;
  1018. int rc;
  1019. pr_debug("%s[%pK]: Send read buff %pK phy %pK len %d\n",
  1020. __func__, audio, buf_node,
  1021. &buf_node->paddr, buf_node->buf.buf_len);
  1022. ac = audio->ac;
  1023. /* Provide address so driver can append nr frames information */
  1024. param.paddr = buf_node->paddr +
  1025. sizeof(struct dec_meta_out);
  1026. param.len = buf_node->buf.buf_len -
  1027. sizeof(struct dec_meta_out);
  1028. param.uid = ac->session;
  1029. /* Write command will populate session_id as token */
  1030. buf_node->token = ac->session;
  1031. rc = q6asm_async_read(ac, &param);
  1032. if (rc < 0 && rc != -ENETRESET)
  1033. pr_err_ratelimited("%s[%pK]:failed\n", __func__, audio);
  1034. return rc;
  1035. }
  1036. static int audio_aio_buf_add_shared(struct q6audio_aio *audio, u32 dir,
  1037. struct audio_aio_buffer_node *buf_node)
  1038. {
  1039. unsigned long flags;
  1040. int ret = 0;
  1041. pr_debug("%s[%pK]:node %pK dir %x buf_addr %pK buf_len %d data_len %d\n",
  1042. __func__, audio, buf_node, dir, buf_node->buf.buf_addr,
  1043. buf_node->buf.buf_len, buf_node->buf.data_len);
  1044. buf_node->paddr = audio_aio_ion_fixup(audio, buf_node->buf.buf_addr,
  1045. buf_node->buf.buf_len, 1,
  1046. &buf_node->kvaddr);
  1047. if (dir) {
  1048. /* write */
  1049. if (!buf_node->paddr ||
  1050. (buf_node->paddr & 0x1) ||
  1051. (!audio->feedback && !buf_node->buf.data_len)) {
  1052. kfree(buf_node);
  1053. return -EINVAL;
  1054. }
  1055. ret = extract_meta_out_info(audio, buf_node, 1);
  1056. if (ret) {
  1057. pr_debug("%s: extract meta failed with %d\n",
  1058. __func__, ret);
  1059. kfree(buf_node);
  1060. return ret;
  1061. }
  1062. /* Not a EOS buffer */
  1063. if (!(buf_node->meta_info.meta_in.nflags & AUDIO_DEC_EOS_SET)) {
  1064. spin_lock_irqsave(&audio->dsp_lock, flags);
  1065. ret = audio_aio_async_write(audio, buf_node);
  1066. /* EOS buffer handled in driver */
  1067. list_add_tail(&buf_node->list, &audio->out_queue);
  1068. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  1069. } else if (buf_node->meta_info.meta_in.nflags
  1070. & AUDIO_DEC_EOS_SET) {
  1071. if (!audio->wflush) {
  1072. pr_debug("%s[%pK]:Send EOS cmd at i/p\n",
  1073. __func__, audio);
  1074. /* Driver will forcefully post writedone event
  1075. * once eos ack recived from DSP
  1076. */
  1077. audio->eos_write_payload.aio_buf =
  1078. buf_node->buf;
  1079. audio->eos_flag = 1;
  1080. audio->eos_rsp = 0;
  1081. q6asm_cmd(audio->ac, CMD_EOS);
  1082. kfree(buf_node);
  1083. } else { /* Flush in progress, send back i/p
  1084. * EOS buffer as is
  1085. */
  1086. union msm_audio_event_payload event_payload;
  1087. event_payload.aio_buf = buf_node->buf;
  1088. audio_aio_post_event(audio,
  1089. AUDIO_EVENT_WRITE_DONE,
  1090. event_payload);
  1091. kfree(buf_node);
  1092. }
  1093. }
  1094. } else {
  1095. /* read */
  1096. if (!buf_node->paddr ||
  1097. (buf_node->paddr & 0x1) ||
  1098. (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
  1099. kfree(buf_node);
  1100. return -EINVAL;
  1101. }
  1102. /* No EOS reached */
  1103. if (!audio->eos_rsp) {
  1104. spin_lock_irqsave(&audio->dsp_lock, flags);
  1105. ret = audio_aio_async_read(audio, buf_node);
  1106. /* EOS buffer handled in driver */
  1107. list_add_tail(&buf_node->list, &audio->in_queue);
  1108. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  1109. }
  1110. /* EOS reached at input side fake all upcoming read buffer to
  1111. * indicate the same
  1112. */
  1113. else {
  1114. union msm_audio_event_payload event_payload;
  1115. event_payload.aio_buf = buf_node->buf;
  1116. event_payload.aio_buf.data_len =
  1117. insert_eos_buf(audio, buf_node);
  1118. pr_debug("%s[%pK]: propagate READ_DONE as EOS done\n",
  1119. __func__, audio);
  1120. audio_aio_post_event(audio, AUDIO_EVENT_READ_DONE,
  1121. event_payload);
  1122. kfree(buf_node);
  1123. }
  1124. }
  1125. return ret;
  1126. }
  1127. #ifdef CONFIG_COMPAT
  1128. static int audio_aio_buf_add_compat(struct q6audio_aio *audio, u32 dir,
  1129. void __user *arg)
  1130. {
  1131. struct audio_aio_buffer_node *buf_node;
  1132. struct msm_audio_aio_buf32 aio_buf_32;
  1133. buf_node = kzalloc(sizeof(*buf_node), GFP_KERNEL);
  1134. if (!buf_node)
  1135. return -ENOMEM;
  1136. if (copy_from_user(&aio_buf_32, arg, sizeof(aio_buf_32))) {
  1137. kfree(buf_node);
  1138. pr_err("%s: copy_from_user failed\n", __func__);
  1139. return -EFAULT;
  1140. }
  1141. buf_node->buf.buf_addr = compat_ptr(aio_buf_32.buf_addr);
  1142. buf_node->buf.buf_len = aio_buf_32.buf_len;
  1143. buf_node->buf.data_len = aio_buf_32.data_len;
  1144. buf_node->buf.private_data = compat_ptr(aio_buf_32.private_data);
  1145. buf_node->buf.mfield_sz = aio_buf_32.mfield_sz;
  1146. return audio_aio_buf_add_shared(audio, dir, buf_node);
  1147. }
  1148. #endif
  1149. static int audio_aio_buf_add(struct q6audio_aio *audio, u32 dir,
  1150. void __user *arg)
  1151. {
  1152. struct audio_aio_buffer_node *buf_node;
  1153. buf_node = kzalloc(sizeof(*buf_node), GFP_KERNEL);
  1154. if (!buf_node)
  1155. return -ENOMEM;
  1156. if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
  1157. kfree(buf_node);
  1158. pr_err("%s: copy_from_user failed\n", __func__);
  1159. return -EFAULT;
  1160. }
  1161. return audio_aio_buf_add_shared(audio, dir, buf_node);
  1162. }
  1163. void audio_aio_ioport_reset(struct q6audio_aio *audio)
  1164. {
  1165. if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
  1166. /* If fsync is in progress, make sure
  1167. * return value of fsync indicates
  1168. * abort due to flush
  1169. */
  1170. if (audio->drv_status & ADRV_STATUS_FSYNC) {
  1171. pr_debug("%s[%pK]:fsync in progress\n",
  1172. __func__, audio);
  1173. audio->drv_ops.out_flush(audio);
  1174. } else
  1175. audio->drv_ops.out_flush(audio);
  1176. if (audio->feedback == NON_TUNNEL_MODE)
  1177. audio->drv_ops.in_flush(audio);
  1178. }
  1179. }
  1180. int audio_aio_open(struct q6audio_aio *audio, struct file *file)
  1181. {
  1182. int rc = 0;
  1183. int i;
  1184. struct audio_aio_event *e_node = NULL;
  1185. struct list_head *ptr, *next;
  1186. /* Settings will be re-config at AUDIO_SET_CONFIG,
  1187. * but at least we need to have initial config
  1188. */
  1189. audio->str_cfg.buffer_size = FRAME_SIZE;
  1190. audio->str_cfg.buffer_count = FRAME_NUM;
  1191. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  1192. audio->pcm_cfg.sample_rate = 48000;
  1193. audio->pcm_cfg.channel_count = 2;
  1194. /* Only AIO interface */
  1195. if (file->f_flags & O_NONBLOCK) {
  1196. pr_debug("%s[%pK]:set to aio interface\n", __func__, audio);
  1197. audio->drv_status |= ADRV_STATUS_AIO_INTF;
  1198. audio->drv_ops.out_flush = audio_aio_async_out_flush;
  1199. audio->drv_ops.in_flush = audio_aio_async_in_flush;
  1200. q6asm_set_io_mode(audio->ac, ASYNC_IO_MODE);
  1201. } else {
  1202. pr_err_ratelimited("%s[%pK]:SIO interface not supported\n",
  1203. __func__, audio);
  1204. rc = -EACCES;
  1205. goto fail;
  1206. }
  1207. /* Initialize all locks of audio instance */
  1208. mutex_init(&audio->lock);
  1209. mutex_init(&audio->read_lock);
  1210. mutex_init(&audio->write_lock);
  1211. mutex_init(&audio->get_event_lock);
  1212. spin_lock_init(&audio->dsp_lock);
  1213. spin_lock_init(&audio->event_queue_lock);
  1214. init_waitqueue_head(&audio->cmd_wait);
  1215. init_waitqueue_head(&audio->write_wait);
  1216. init_waitqueue_head(&audio->event_wait);
  1217. INIT_LIST_HEAD(&audio->out_queue);
  1218. INIT_LIST_HEAD(&audio->in_queue);
  1219. INIT_LIST_HEAD(&audio->ion_region_queue);
  1220. INIT_LIST_HEAD(&audio->free_event_queue);
  1221. INIT_LIST_HEAD(&audio->event_queue);
  1222. audio->drv_ops.out_flush(audio);
  1223. audio->opened = 1;
  1224. audio->reset_event = false;
  1225. file->private_data = audio;
  1226. audio->codec_ioctl = audio_aio_ioctl;
  1227. audio->codec_compat_ioctl = audio_aio_compat_ioctl;
  1228. for (i = 0; i < AUDIO_EVENT_NUM; i++) {
  1229. e_node = kmalloc(sizeof(struct audio_aio_event), GFP_KERNEL);
  1230. if (e_node)
  1231. list_add_tail(&e_node->list, &audio->free_event_queue);
  1232. else {
  1233. rc = -ENOMEM;
  1234. goto cleanup;
  1235. }
  1236. }
  1237. rc = register_volume_listener(audio);
  1238. if (rc < 0)
  1239. goto cleanup;
  1240. return 0;
  1241. cleanup:
  1242. list_for_each_safe(ptr, next, &audio->free_event_queue) {
  1243. e_node = list_first_entry(&audio->free_event_queue,
  1244. struct audio_aio_event, list);
  1245. list_del(&e_node->list);
  1246. kfree(e_node);
  1247. }
  1248. fail:
  1249. return rc;
  1250. }
  1251. static long audio_aio_shared_ioctl(struct file *file, unsigned int cmd,
  1252. unsigned long arg)
  1253. {
  1254. struct q6audio_aio *audio = file->private_data;
  1255. int rc = 0;
  1256. switch (cmd) {
  1257. case AUDIO_ABORT_GET_EVENT: {
  1258. audio->event_abort = 1;
  1259. wake_up(&audio->event_wait);
  1260. break;
  1261. }
  1262. case AUDIO_OUTPORT_FLUSH: {
  1263. pr_debug("%s[%pK]:AUDIO_OUTPORT_FLUSH\n", __func__, audio);
  1264. mutex_lock(&audio->read_lock);
  1265. rc = audio_aio_outport_flush(audio);
  1266. if (rc < 0) {
  1267. pr_err_ratelimited("%s[%pK]: AUDIO_OUTPORT_FLUSH failed\n",
  1268. __func__, audio);
  1269. rc = -EINTR;
  1270. }
  1271. mutex_unlock(&audio->read_lock);
  1272. break;
  1273. }
  1274. case AUDIO_STOP: {
  1275. pr_debug("%s[%pK]: AUDIO_STOP session_id[%d]\n", __func__,
  1276. audio, audio->ac->session);
  1277. mutex_lock(&audio->lock);
  1278. audio->stopped = 1;
  1279. rc = audio_aio_flush(audio);
  1280. if (rc < 0) {
  1281. pr_err_ratelimited("%s[%pK]:Audio Stop procedure failed rc=%d\n",
  1282. __func__, audio, rc);
  1283. mutex_unlock(&audio->lock);
  1284. break;
  1285. }
  1286. audio->enabled = 0;
  1287. audio->drv_status &= ~ADRV_STATUS_PAUSE;
  1288. if (audio->drv_status & ADRV_STATUS_FSYNC) {
  1289. pr_debug("%s[%pK] Waking up the audio_aio_fsync\n",
  1290. __func__, audio);
  1291. wake_up(&audio->write_wait);
  1292. }
  1293. mutex_unlock(&audio->lock);
  1294. break;
  1295. }
  1296. case AUDIO_PAUSE: {
  1297. pr_debug("%s[%pK]:AUDIO_PAUSE %ld\n", __func__, audio, arg);
  1298. mutex_lock(&audio->lock);
  1299. if (arg == 1) {
  1300. rc = audio_aio_pause(audio);
  1301. if (rc < 0) {
  1302. pr_err_ratelimited("%s[%pK]: pause FAILED rc=%d\n",
  1303. __func__, audio, rc);
  1304. mutex_unlock(&audio->lock);
  1305. break;
  1306. }
  1307. audio->drv_status |= ADRV_STATUS_PAUSE;
  1308. } else if (arg == 0) {
  1309. if (audio->drv_status & ADRV_STATUS_PAUSE) {
  1310. rc = audio_aio_enable(audio);
  1311. if (rc)
  1312. pr_err_ratelimited("%s[%pK]: audio enable failed\n",
  1313. __func__, audio);
  1314. else {
  1315. audio->drv_status &= ~ADRV_STATUS_PAUSE;
  1316. audio->enabled = 1;
  1317. }
  1318. }
  1319. }
  1320. mutex_unlock(&audio->lock);
  1321. break;
  1322. }
  1323. case AUDIO_FLUSH: {
  1324. pr_debug("%s[%pK]: AUDIO_FLUSH sessionid[%d]\n", __func__,
  1325. audio, audio->ac->session);
  1326. mutex_lock(&audio->lock);
  1327. audio->rflush = 1;
  1328. audio->wflush = 1;
  1329. if (audio->drv_status & ADRV_STATUS_FSYNC) {
  1330. pr_debug("%s[%pK] Waking up the audio_aio_fsync\n",
  1331. __func__, audio);
  1332. wake_up(&audio->write_wait);
  1333. }
  1334. /* Flush DSP */
  1335. rc = audio_aio_flush(audio);
  1336. /* Flush input / Output buffer in software*/
  1337. audio_aio_ioport_reset(audio);
  1338. if (rc < 0) {
  1339. pr_err_ratelimited("%s[%pK]:AUDIO_FLUSH interrupted\n",
  1340. __func__, audio);
  1341. rc = -EINTR;
  1342. } else {
  1343. audio->rflush = 0;
  1344. if (audio->drv_status & ADRV_STATUS_FSYNC)
  1345. wake_up(&audio->write_wait);
  1346. else
  1347. audio->wflush = 0;
  1348. }
  1349. audio->eos_flag = 0;
  1350. audio->eos_rsp = 0;
  1351. mutex_unlock(&audio->lock);
  1352. break;
  1353. }
  1354. case AUDIO_GET_SESSION_ID: {
  1355. mutex_lock(&audio->lock);
  1356. if (copy_to_user((void *)arg, &audio->ac->session,
  1357. sizeof(u16))) {
  1358. pr_err_ratelimited("%s: copy_to_user for AUDIO_GET_SESSION_ID failed\n",
  1359. __func__);
  1360. rc = -EFAULT;
  1361. }
  1362. mutex_unlock(&audio->lock);
  1363. break;
  1364. }
  1365. case AUDIO_PM_AWAKE: {
  1366. if ((audio->audio_ws_mgr == NULL) ||
  1367. (audio->miscdevice == NULL)) {
  1368. pr_err_ratelimited("%s[%pK]: invalid ws_mgr or miscdevice",
  1369. __func__, audio);
  1370. rc = -EACCES;
  1371. break;
  1372. }
  1373. pr_debug("%s[%pK]:AUDIO_PM_AWAKE\n", __func__, audio);
  1374. mutex_lock(&audio->lock);
  1375. if (!audio->wakelock_voted) {
  1376. audio->wakelock_voted = true;
  1377. mutex_lock(&audio->audio_ws_mgr->ws_lock);
  1378. if (audio->audio_ws_mgr->ref_cnt++ == 0)
  1379. pm_stay_awake(audio->miscdevice->this_device);
  1380. mutex_unlock(&audio->audio_ws_mgr->ws_lock);
  1381. }
  1382. mutex_unlock(&audio->lock);
  1383. break;
  1384. }
  1385. case AUDIO_PM_RELAX: {
  1386. if ((audio->audio_ws_mgr == NULL) ||
  1387. (audio->miscdevice == NULL)) {
  1388. pr_err_ratelimited("%s[%pK]: invalid ws_mgr or miscdevice",
  1389. __func__, audio);
  1390. rc = -EACCES;
  1391. break;
  1392. }
  1393. pr_debug("%s[%pK]:AUDIO_PM_RELAX\n", __func__, audio);
  1394. mutex_lock(&audio->lock);
  1395. if (audio->wakelock_voted) {
  1396. audio->wakelock_voted = false;
  1397. mutex_lock(&audio->audio_ws_mgr->ws_lock);
  1398. if ((audio->audio_ws_mgr->ref_cnt > 0) &&
  1399. (--audio->audio_ws_mgr->ref_cnt == 0)) {
  1400. pm_relax(audio->miscdevice->this_device);
  1401. }
  1402. mutex_unlock(&audio->audio_ws_mgr->ws_lock);
  1403. }
  1404. mutex_unlock(&audio->lock);
  1405. break;
  1406. }
  1407. default:
  1408. pr_err_ratelimited("%s: Unknown ioctl cmd = %d", __func__, cmd);
  1409. rc = -EINVAL;
  1410. }
  1411. return rc;
  1412. }
  1413. static long audio_aio_ioctl(struct file *file, unsigned int cmd,
  1414. unsigned long arg)
  1415. {
  1416. struct q6audio_aio *audio = file->private_data;
  1417. int rc = 0;
  1418. switch (cmd) {
  1419. case AUDIO_ABORT_GET_EVENT:
  1420. case AUDIO_OUTPORT_FLUSH:
  1421. case AUDIO_STOP:
  1422. case AUDIO_PAUSE:
  1423. case AUDIO_FLUSH:
  1424. case AUDIO_GET_SESSION_ID:
  1425. case AUDIO_PM_AWAKE:
  1426. case AUDIO_PM_RELAX:
  1427. rc = audio_aio_shared_ioctl(file, cmd, arg);
  1428. break;
  1429. case AUDIO_GET_STATS: {
  1430. struct msm_audio_stats stats;
  1431. uint64_t timestamp;
  1432. memset(&stats, 0, sizeof(struct msm_audio_stats));
  1433. stats.byte_count = atomic_read(&audio->in_bytes);
  1434. stats.sample_count = atomic_read(&audio->in_samples);
  1435. rc = q6asm_get_session_time(audio->ac, &timestamp);
  1436. if (rc >= 0)
  1437. memcpy(&stats.unused[0], &timestamp, sizeof(timestamp));
  1438. else
  1439. pr_debug("Error while getting timestamp\n");
  1440. if (copy_to_user((void *)arg, &stats, sizeof(stats))) {
  1441. pr_err("%s: copy_frm_user for AUDIO_GET_STATS failed\n",
  1442. __func__);
  1443. rc = -EFAULT;
  1444. }
  1445. break;
  1446. }
  1447. case AUDIO_GET_EVENT: {
  1448. pr_debug("%s[%pK]:AUDIO_GET_EVENT\n", __func__, audio);
  1449. if (mutex_trylock(&audio->get_event_lock)) {
  1450. rc = audio_aio_process_event_req(audio,
  1451. (void __user *)arg);
  1452. mutex_unlock(&audio->get_event_lock);
  1453. } else
  1454. rc = -EBUSY;
  1455. break;
  1456. }
  1457. case AUDIO_ASYNC_WRITE: {
  1458. mutex_lock(&audio->write_lock);
  1459. if (audio->drv_status & ADRV_STATUS_FSYNC)
  1460. rc = -EBUSY;
  1461. else {
  1462. if (audio->enabled)
  1463. rc = audio_aio_buf_add(audio, 1,
  1464. (void __user *)arg);
  1465. else
  1466. rc = -EPERM;
  1467. }
  1468. mutex_unlock(&audio->write_lock);
  1469. break;
  1470. }
  1471. case AUDIO_ASYNC_READ: {
  1472. mutex_lock(&audio->read_lock);
  1473. if (audio->feedback)
  1474. rc = audio_aio_buf_add(audio, 0,
  1475. (void __user *)arg);
  1476. else
  1477. rc = -EPERM;
  1478. mutex_unlock(&audio->read_lock);
  1479. break;
  1480. }
  1481. case AUDIO_GET_STREAM_CONFIG: {
  1482. struct msm_audio_stream_config cfg;
  1483. mutex_lock(&audio->lock);
  1484. memset(&cfg, 0, sizeof(cfg));
  1485. cfg.buffer_size = audio->str_cfg.buffer_size;
  1486. cfg.buffer_count = audio->str_cfg.buffer_count;
  1487. pr_debug("%s[%pK]:GET STREAM CFG %d %d\n",
  1488. __func__, audio, cfg.buffer_size, cfg.buffer_count);
  1489. if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) {
  1490. pr_err(
  1491. "%s: copy_to_user for AUDIO_GET_STREAM_CONFIG failed\n",
  1492. __func__);
  1493. rc = -EFAULT;
  1494. }
  1495. mutex_unlock(&audio->lock);
  1496. break;
  1497. }
  1498. case AUDIO_SET_STREAM_CONFIG: {
  1499. struct msm_audio_stream_config cfg;
  1500. pr_debug("%s[%pK]:SET STREAM CONFIG\n", __func__, audio);
  1501. mutex_lock(&audio->lock);
  1502. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  1503. pr_err(
  1504. "%s: copy_from_user for AUDIO_SET_STREAM_CONFIG failed\n",
  1505. __func__);
  1506. rc = -EFAULT;
  1507. mutex_unlock(&audio->lock);
  1508. break;
  1509. }
  1510. audio->str_cfg.buffer_size = FRAME_SIZE;
  1511. audio->str_cfg.buffer_count = FRAME_NUM;
  1512. rc = 0;
  1513. mutex_unlock(&audio->lock);
  1514. break;
  1515. }
  1516. case AUDIO_GET_CONFIG: {
  1517. struct msm_audio_config cfg;
  1518. mutex_lock(&audio->lock);
  1519. if (copy_to_user((void *)arg, &audio->pcm_cfg, sizeof(cfg))) {
  1520. pr_err(
  1521. "%s: copy_to_user for AUDIO_GET_CONFIG failed\n",
  1522. __func__);
  1523. rc = -EFAULT;
  1524. }
  1525. mutex_unlock(&audio->lock);
  1526. break;
  1527. }
  1528. case AUDIO_SET_CONFIG: {
  1529. struct msm_audio_config config;
  1530. pr_debug("%s[%pK]:AUDIO_SET_CONFIG\n", __func__, audio);
  1531. mutex_lock(&audio->lock);
  1532. if (copy_from_user(&config, (void *)arg, sizeof(config))) {
  1533. pr_err(
  1534. "%s: copy_from_user for AUDIO_SET_CONFIG failed\n",
  1535. __func__);
  1536. rc = -EFAULT;
  1537. mutex_unlock(&audio->lock);
  1538. break;
  1539. }
  1540. if (audio->feedback != NON_TUNNEL_MODE) {
  1541. pr_err("%s[%pK]:Not sufficient permission to change the playback mode\n",
  1542. __func__, audio);
  1543. rc = -EACCES;
  1544. mutex_unlock(&audio->lock);
  1545. break;
  1546. }
  1547. if ((config.buffer_count > PCM_BUF_COUNT) ||
  1548. (config.buffer_count == 1))
  1549. config.buffer_count = PCM_BUF_COUNT;
  1550. if (config.buffer_size < PCM_BUFSZ_MIN)
  1551. config.buffer_size = PCM_BUFSZ_MIN;
  1552. audio->pcm_cfg.buffer_count = config.buffer_count;
  1553. audio->pcm_cfg.buffer_size = config.buffer_size;
  1554. audio->pcm_cfg.channel_count = config.channel_count;
  1555. audio->pcm_cfg.sample_rate = config.sample_rate;
  1556. rc = 0;
  1557. mutex_unlock(&audio->lock);
  1558. break;
  1559. }
  1560. case AUDIO_SET_BUF_CFG: {
  1561. struct msm_audio_buf_cfg cfg;
  1562. mutex_lock(&audio->lock);
  1563. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  1564. pr_err(
  1565. "%s: copy_from_user for AUDIO_GET_BUF CONFIG failed\n",
  1566. __func__);
  1567. rc = -EFAULT;
  1568. mutex_unlock(&audio->lock);
  1569. break;
  1570. }
  1571. if ((audio->feedback == NON_TUNNEL_MODE) &&
  1572. !cfg.meta_info_enable) {
  1573. rc = -EFAULT;
  1574. mutex_unlock(&audio->lock);
  1575. break;
  1576. }
  1577. audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
  1578. pr_debug("%s[%pK]:session id %d: Set-buf-cfg: meta[%d]",
  1579. __func__, audio,
  1580. audio->ac->session, cfg.meta_info_enable);
  1581. mutex_unlock(&audio->lock);
  1582. break;
  1583. }
  1584. case AUDIO_GET_BUF_CFG: {
  1585. pr_debug("%s[%pK]:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
  1586. __func__, audio,
  1587. audio->ac->session, audio->buf_cfg.meta_info_enable,
  1588. audio->buf_cfg.frames_per_buf);
  1589. mutex_lock(&audio->lock);
  1590. if (copy_to_user((void *)arg, &audio->buf_cfg,
  1591. sizeof(struct msm_audio_buf_cfg))) {
  1592. pr_err(
  1593. "%s: copy_to_user for AUDIO_GET_BUF_CONFIG failed\n",
  1594. __func__);
  1595. rc = -EFAULT;
  1596. }
  1597. mutex_unlock(&audio->lock);
  1598. break;
  1599. }
  1600. case AUDIO_REGISTER_ION: {
  1601. struct msm_audio_ion_info info;
  1602. pr_debug("%s[%pK]:AUDIO_REGISTER_ION\n", __func__, audio);
  1603. mutex_lock(&audio->lock);
  1604. if (copy_from_user(&info, (void *)arg, sizeof(info))) {
  1605. pr_err(
  1606. "%s: copy_from_user for AUDIO_REGISTER_ION failed\n",
  1607. __func__);
  1608. rc = -EFAULT;
  1609. } else {
  1610. mutex_lock(&audio->read_lock);
  1611. mutex_lock(&audio->write_lock);
  1612. rc = audio_aio_ion_add(audio, &info);
  1613. mutex_unlock(&audio->write_lock);
  1614. mutex_unlock(&audio->read_lock);
  1615. }
  1616. mutex_unlock(&audio->lock);
  1617. break;
  1618. }
  1619. case AUDIO_DEREGISTER_ION: {
  1620. struct msm_audio_ion_info info;
  1621. mutex_lock(&audio->lock);
  1622. pr_debug("%s[%pK]:AUDIO_DEREGISTER_ION\n", __func__, audio);
  1623. if (copy_from_user(&info, (void *)arg, sizeof(info))) {
  1624. pr_err(
  1625. "%s: copy_from_user for AUDIO_DEREGISTER_ION failed\n",
  1626. __func__);
  1627. rc = -EFAULT;
  1628. } else {
  1629. mutex_lock(&audio->read_lock);
  1630. mutex_lock(&audio->write_lock);
  1631. rc = audio_aio_ion_remove(audio, &info);
  1632. mutex_unlock(&audio->write_lock);
  1633. mutex_unlock(&audio->read_lock);
  1634. }
  1635. mutex_unlock(&audio->lock);
  1636. break;
  1637. }
  1638. default:
  1639. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  1640. rc = -EINVAL;
  1641. }
  1642. return rc;
  1643. }
  1644. #ifdef CONFIG_COMPAT
  1645. struct msm_audio_stream_config32 {
  1646. u32 buffer_size;
  1647. u32 buffer_count;
  1648. };
  1649. struct msm_audio_stats32 {
  1650. u32 byte_count;
  1651. u32 sample_count;
  1652. u32 unused[2];
  1653. };
  1654. struct msm_audio_config32 {
  1655. u32 buffer_size;
  1656. u32 buffer_count;
  1657. u32 channel_count;
  1658. u32 sample_rate;
  1659. u32 type;
  1660. u32 meta_field;
  1661. u32 bits;
  1662. u32 unused[3];
  1663. };
  1664. struct msm_audio_buf_cfg32 {
  1665. u32 meta_info_enable;
  1666. u32 frames_per_buf;
  1667. };
  1668. struct msm_audio_ion_info32 {
  1669. int fd;
  1670. compat_uptr_t vaddr;
  1671. };
  1672. enum {
  1673. AUDIO_GET_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, 3,
  1674. struct msm_audio_config32),
  1675. AUDIO_SET_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, 4,
  1676. struct msm_audio_config32),
  1677. AUDIO_GET_STATS_32 = _IOR(AUDIO_IOCTL_MAGIC, 5,
  1678. struct msm_audio_stats32),
  1679. AUDIO_GET_EVENT_32 = _IOR(AUDIO_IOCTL_MAGIC, 13,
  1680. struct msm_audio_event32),
  1681. AUDIO_ASYNC_WRITE_32 = _IOW(AUDIO_IOCTL_MAGIC, 17,
  1682. struct msm_audio_aio_buf32),
  1683. AUDIO_ASYNC_READ_32 = _IOW(AUDIO_IOCTL_MAGIC, 18,
  1684. struct msm_audio_aio_buf32),
  1685. AUDIO_SET_STREAM_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, 80,
  1686. struct msm_audio_stream_config32),
  1687. AUDIO_GET_STREAM_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, 81,
  1688. struct msm_audio_stream_config32),
  1689. AUDIO_GET_BUF_CFG_32 = _IOW(AUDIO_IOCTL_MAGIC, 93,
  1690. struct msm_audio_buf_cfg32),
  1691. AUDIO_SET_BUF_CFG_32 = _IOW(AUDIO_IOCTL_MAGIC, 94,
  1692. struct msm_audio_buf_cfg32),
  1693. AUDIO_REGISTER_ION_32 = _IOW(AUDIO_IOCTL_MAGIC, 97,
  1694. struct msm_audio_ion_info32),
  1695. AUDIO_DEREGISTER_ION_32 = _IOW(AUDIO_IOCTL_MAGIC, 98,
  1696. struct msm_audio_ion_info32),
  1697. };
  1698. static long audio_aio_compat_ioctl(struct file *file, unsigned int cmd,
  1699. unsigned long arg)
  1700. {
  1701. struct q6audio_aio *audio = file->private_data;
  1702. int rc = 0;
  1703. switch (cmd) {
  1704. case AUDIO_ABORT_GET_EVENT:
  1705. case AUDIO_OUTPORT_FLUSH:
  1706. case AUDIO_STOP:
  1707. case AUDIO_PAUSE:
  1708. case AUDIO_FLUSH:
  1709. case AUDIO_GET_SESSION_ID:
  1710. case AUDIO_PM_AWAKE:
  1711. case AUDIO_PM_RELAX:
  1712. rc = audio_aio_shared_ioctl(file, cmd, arg);
  1713. break;
  1714. case AUDIO_GET_STATS_32: {
  1715. struct msm_audio_stats32 stats;
  1716. uint64_t timestamp;
  1717. memset(&stats, 0, sizeof(struct msm_audio_stats32));
  1718. stats.byte_count = atomic_read(&audio->in_bytes);
  1719. stats.sample_count = atomic_read(&audio->in_samples);
  1720. rc = q6asm_get_session_time(audio->ac, &timestamp);
  1721. if (rc >= 0)
  1722. memcpy(&stats.unused[0], &timestamp, sizeof(timestamp));
  1723. else
  1724. pr_debug("Error while getting timestamp\n");
  1725. if (copy_to_user((void *)arg, &stats, sizeof(stats))) {
  1726. pr_err(
  1727. "%s: copy_to_user for AUDIO_GET_STATS_32 failed\n",
  1728. __func__);
  1729. rc = -EFAULT;
  1730. }
  1731. break;
  1732. }
  1733. case AUDIO_GET_EVENT_32: {
  1734. pr_debug("%s[%pK]:AUDIO_GET_EVENT\n", __func__, audio);
  1735. if (mutex_trylock(&audio->get_event_lock)) {
  1736. rc = audio_aio_process_event_req_compat(audio,
  1737. (void __user *)arg);
  1738. mutex_unlock(&audio->get_event_lock);
  1739. } else
  1740. rc = -EBUSY;
  1741. break;
  1742. }
  1743. case AUDIO_ASYNC_WRITE_32: {
  1744. mutex_lock(&audio->write_lock);
  1745. if (audio->drv_status & ADRV_STATUS_FSYNC)
  1746. rc = -EBUSY;
  1747. else {
  1748. if (audio->enabled)
  1749. rc = audio_aio_buf_add_compat(audio, 1,
  1750. (void __user *)arg);
  1751. else
  1752. rc = -EPERM;
  1753. }
  1754. mutex_unlock(&audio->write_lock);
  1755. break;
  1756. }
  1757. case AUDIO_ASYNC_READ_32: {
  1758. mutex_lock(&audio->read_lock);
  1759. if (audio->feedback)
  1760. rc = audio_aio_buf_add_compat(audio, 0,
  1761. (void __user *)arg);
  1762. else
  1763. rc = -EPERM;
  1764. mutex_unlock(&audio->read_lock);
  1765. break;
  1766. }
  1767. case AUDIO_GET_STREAM_CONFIG_32: {
  1768. struct msm_audio_stream_config32 cfg;
  1769. mutex_lock(&audio->lock);
  1770. memset(&cfg, 0, sizeof(cfg));
  1771. cfg.buffer_size = audio->str_cfg.buffer_size;
  1772. cfg.buffer_count = audio->str_cfg.buffer_count;
  1773. pr_debug("%s[%pK]:GET STREAM CFG %d %d\n",
  1774. __func__, audio, cfg.buffer_size, cfg.buffer_count);
  1775. if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) {
  1776. pr_err("%s: copy_to_user for AUDIO_GET_STREAM_CONFIG_32 failed\n",
  1777. __func__);
  1778. rc = -EFAULT;
  1779. }
  1780. mutex_unlock(&audio->lock);
  1781. break;
  1782. }
  1783. case AUDIO_SET_STREAM_CONFIG_32: {
  1784. struct msm_audio_stream_config32 cfg_32;
  1785. struct msm_audio_stream_config cfg;
  1786. pr_debug("%s[%pK]:SET STREAM CONFIG\n", __func__, audio);
  1787. mutex_lock(&audio->lock);
  1788. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  1789. pr_err("%s: copy_from_user for AUDIO_SET_STREAM_CONFIG_32 failed\n",
  1790. __func__);
  1791. rc = -EFAULT;
  1792. mutex_unlock(&audio->lock);
  1793. break;
  1794. }
  1795. cfg.buffer_size = cfg_32.buffer_size;
  1796. cfg.buffer_count = cfg_32.buffer_count;
  1797. audio->str_cfg.buffer_size = FRAME_SIZE;
  1798. audio->str_cfg.buffer_count = FRAME_NUM;
  1799. rc = 0;
  1800. mutex_unlock(&audio->lock);
  1801. break;
  1802. }
  1803. case AUDIO_GET_CONFIG_32: {
  1804. struct msm_audio_config32 cfg_32;
  1805. mutex_lock(&audio->lock);
  1806. memset(&cfg_32, 0, sizeof(cfg_32));
  1807. cfg_32.buffer_size = audio->pcm_cfg.buffer_size;
  1808. cfg_32.buffer_count = audio->pcm_cfg.buffer_count;
  1809. cfg_32.channel_count = audio->pcm_cfg.channel_count;
  1810. cfg_32.sample_rate = audio->pcm_cfg.sample_rate;
  1811. cfg_32.type = audio->pcm_cfg.type;
  1812. cfg_32.meta_field = audio->pcm_cfg.meta_field;
  1813. cfg_32.bits = audio->pcm_cfg.bits;
  1814. if (copy_to_user((void *)arg, &cfg_32, sizeof(cfg_32))) {
  1815. pr_err("%s: copy_to_user for AUDIO_GET_CONFIG_32 failed\n",
  1816. __func__);
  1817. rc = -EFAULT;
  1818. }
  1819. mutex_unlock(&audio->lock);
  1820. break;
  1821. }
  1822. case AUDIO_SET_CONFIG_32: {
  1823. struct msm_audio_config config;
  1824. struct msm_audio_config32 config_32;
  1825. mutex_lock(&audio->lock);
  1826. if (audio->feedback != NON_TUNNEL_MODE) {
  1827. pr_err("%s[%pK]:Not sufficient permission to change the playback mode\n",
  1828. __func__, audio);
  1829. rc = -EACCES;
  1830. mutex_unlock(&audio->lock);
  1831. break;
  1832. }
  1833. pr_debug("%s[%pK]:AUDIO_SET_CONFIG\n", __func__, audio);
  1834. if (copy_from_user(&config_32, (void *)arg,
  1835. sizeof(config_32))) {
  1836. pr_err("%s: copy_from_user for AUDIO_SET_CONFIG_32 failed\n",
  1837. __func__);
  1838. rc = -EFAULT;
  1839. mutex_unlock(&audio->lock);
  1840. break;
  1841. }
  1842. config.buffer_size = config_32.buffer_size;
  1843. config.buffer_count = config_32.buffer_count;
  1844. config.channel_count = config_32.channel_count;
  1845. config.sample_rate = config_32.sample_rate;
  1846. config.type = config_32.type;
  1847. config.meta_field = config_32.meta_field;
  1848. config.bits = config_32.bits;
  1849. if ((config.buffer_count > PCM_BUF_COUNT) ||
  1850. (config.buffer_count == 1))
  1851. config.buffer_count = PCM_BUF_COUNT;
  1852. if (config.buffer_size < PCM_BUFSZ_MIN)
  1853. config.buffer_size = PCM_BUFSZ_MIN;
  1854. audio->pcm_cfg.buffer_count = config.buffer_count;
  1855. audio->pcm_cfg.buffer_size = config.buffer_size;
  1856. audio->pcm_cfg.channel_count = config.channel_count;
  1857. audio->pcm_cfg.sample_rate = config.sample_rate;
  1858. rc = 0;
  1859. mutex_unlock(&audio->lock);
  1860. break;
  1861. }
  1862. case AUDIO_SET_BUF_CFG_32: {
  1863. struct msm_audio_buf_cfg cfg;
  1864. struct msm_audio_buf_cfg32 cfg_32;
  1865. mutex_lock(&audio->lock);
  1866. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  1867. pr_err("%s: copy_from_user for AUDIO_SET_CONFIG_32 failed\n",
  1868. __func__);
  1869. rc = -EFAULT;
  1870. mutex_unlock(&audio->lock);
  1871. break;
  1872. }
  1873. cfg.meta_info_enable = cfg_32.meta_info_enable;
  1874. cfg.frames_per_buf = cfg_32.frames_per_buf;
  1875. if ((audio->feedback == NON_TUNNEL_MODE) &&
  1876. !cfg.meta_info_enable) {
  1877. rc = -EFAULT;
  1878. mutex_unlock(&audio->lock);
  1879. break;
  1880. }
  1881. audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
  1882. pr_debug("%s[%pK]:session id %d: Set-buf-cfg: meta[%d]",
  1883. __func__, audio,
  1884. audio->ac->session, cfg.meta_info_enable);
  1885. mutex_unlock(&audio->lock);
  1886. break;
  1887. }
  1888. case AUDIO_GET_BUF_CFG_32: {
  1889. struct msm_audio_buf_cfg32 cfg_32;
  1890. pr_debug("%s[%pK]:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
  1891. __func__, audio,
  1892. audio->ac->session, audio->buf_cfg.meta_info_enable,
  1893. audio->buf_cfg.frames_per_buf);
  1894. mutex_lock(&audio->lock);
  1895. memset(&cfg_32, 0, sizeof(cfg_32));
  1896. cfg_32.meta_info_enable = audio->buf_cfg.meta_info_enable;
  1897. cfg_32.frames_per_buf = audio->buf_cfg.frames_per_buf;
  1898. if (copy_to_user((void *)arg, &cfg_32,
  1899. sizeof(struct msm_audio_buf_cfg32))) {
  1900. pr_err("%s: copy_to_user for AUDIO_GET_BUF_CFG_32 failed\n",
  1901. __func__);
  1902. rc = -EFAULT;
  1903. }
  1904. mutex_unlock(&audio->lock);
  1905. break;
  1906. }
  1907. case AUDIO_REGISTER_ION_32: {
  1908. struct msm_audio_ion_info32 info_32;
  1909. struct msm_audio_ion_info info;
  1910. pr_debug("%s[%pK]:AUDIO_REGISTER_ION\n", __func__, audio);
  1911. mutex_lock(&audio->lock);
  1912. if (copy_from_user(&info_32, (void *)arg, sizeof(info_32))) {
  1913. pr_err("%s: copy_from_user for AUDIO_REGISTER_ION_32 failed\n",
  1914. __func__);
  1915. rc = -EFAULT;
  1916. } else {
  1917. info.fd = info_32.fd;
  1918. info.vaddr = compat_ptr(info_32.vaddr);
  1919. mutex_lock(&audio->read_lock);
  1920. mutex_lock(&audio->write_lock);
  1921. rc = audio_aio_ion_add(audio, &info);
  1922. mutex_unlock(&audio->write_lock);
  1923. mutex_unlock(&audio->read_lock);
  1924. }
  1925. mutex_unlock(&audio->lock);
  1926. break;
  1927. }
  1928. case AUDIO_DEREGISTER_ION_32: {
  1929. struct msm_audio_ion_info32 info_32;
  1930. struct msm_audio_ion_info info;
  1931. mutex_lock(&audio->lock);
  1932. pr_debug("%s[%pK]:AUDIO_DEREGISTER_ION\n", __func__, audio);
  1933. if (copy_from_user(&info_32, (void *)arg, sizeof(info_32))) {
  1934. pr_err("%s: copy_from_user for AUDIO_DEREGISTER_ION_32 failed\n",
  1935. __func__);
  1936. rc = -EFAULT;
  1937. } else {
  1938. info.fd = info_32.fd;
  1939. info.vaddr = compat_ptr(info_32.vaddr);
  1940. mutex_lock(&audio->read_lock);
  1941. mutex_lock(&audio->write_lock);
  1942. rc = audio_aio_ion_remove(audio, &info);
  1943. mutex_unlock(&audio->write_lock);
  1944. mutex_unlock(&audio->read_lock);
  1945. }
  1946. mutex_unlock(&audio->lock);
  1947. break;
  1948. }
  1949. default:
  1950. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  1951. rc = -EINVAL;
  1952. }
  1953. return rc;
  1954. }
  1955. #endif