audio_utils_aio.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2008 Google, Inc.
  3. * Copyright (C) 2008 HTC Corporation
  4. * Copyright (c) 2009-2020, 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. unsigned long flags = 0;
  537. struct q6audio_aio *audio = file->private_data;
  538. pr_debug("%s[%pK]\n", __func__, audio);
  539. mutex_lock(&lock);
  540. mutex_lock(&audio->lock);
  541. mutex_lock(&audio->read_lock);
  542. mutex_lock(&audio->write_lock);
  543. audio->wflush = 1;
  544. if (audio->wakelock_voted &&
  545. (audio->audio_ws_mgr != NULL) &&
  546. (audio->miscdevice != NULL)) {
  547. audio->wakelock_voted = false;
  548. mutex_lock(&audio->audio_ws_mgr->ws_lock);
  549. if ((audio->audio_ws_mgr->ref_cnt > 0) &&
  550. (--audio->audio_ws_mgr->ref_cnt == 0)) {
  551. pm_relax(audio->miscdevice->this_device);
  552. }
  553. mutex_unlock(&audio->audio_ws_mgr->ws_lock);
  554. }
  555. if (audio->enabled)
  556. audio_aio_flush(audio);
  557. audio->wflush = 0;
  558. audio->drv_ops.out_flush(audio);
  559. audio->drv_ops.in_flush(audio);
  560. audio_aio_disable(audio);
  561. audio_aio_unmap_ion_region(audio);
  562. audio_aio_reset_ion_region(audio);
  563. audio->event_abort = 1;
  564. wake_up(&audio->event_wait);
  565. audio_aio_reset_event_queue(audio);
  566. q6asm_audio_client_free(audio->ac);
  567. mutex_unlock(&audio->write_lock);
  568. mutex_unlock(&audio->read_lock);
  569. mutex_unlock(&audio->lock);
  570. mutex_destroy(&audio->lock);
  571. mutex_destroy(&audio->read_lock);
  572. mutex_destroy(&audio->write_lock);
  573. mutex_destroy(&audio->get_event_lock);
  574. unregister_volume_listener(audio);
  575. #ifdef CONFIG_DEBUG_FS
  576. debugfs_remove(audio->dentry);
  577. #endif
  578. spin_lock_irqsave(&enc_dec_lock, flags);
  579. kfree(audio->codec_cfg);
  580. kfree(audio);
  581. file->private_data = NULL;
  582. spin_unlock_irqrestore(&enc_dec_lock, flags);
  583. mutex_unlock(&lock);
  584. return 0;
  585. }
  586. int audio_aio_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  587. {
  588. int rc = 0;
  589. struct q6audio_aio *audio = file->private_data;
  590. if (!audio->enabled || audio->feedback)
  591. return -EINVAL;
  592. /* Blocking client sends more data */
  593. mutex_lock(&audio->lock);
  594. audio->drv_status |= ADRV_STATUS_FSYNC;
  595. mutex_unlock(&audio->lock);
  596. pr_debug("%s[%pK]:\n", __func__, audio);
  597. audio->eos_rsp = 0;
  598. pr_debug("%s[%pK]Wait for write done from DSP\n", __func__, audio);
  599. rc = wait_event_interruptible(audio->write_wait,
  600. (list_empty(&audio->out_queue)) ||
  601. audio->wflush || audio->stopped);
  602. if (audio->stopped || audio->wflush) {
  603. pr_debug("%s[%pK]: Audio Flushed or Stopped,this is not EOS\n"
  604. , __func__, audio);
  605. audio->wflush = 0;
  606. rc = -EBUSY;
  607. }
  608. if (rc < 0) {
  609. pr_err("%s[%pK]: wait event for list_empty failed, rc = %d\n",
  610. __func__, audio, rc);
  611. goto done;
  612. }
  613. rc = q6asm_cmd(audio->ac, CMD_EOS);
  614. pr_debug("%s[%pK]: EOS cmd sent to DSP\n", __func__, audio);
  615. if (rc < 0)
  616. pr_err_ratelimited("%s[%pK]: q6asm_cmd failed, rc = %d",
  617. __func__, audio, rc);
  618. pr_debug("%s[%pK]: wait for RENDERED_EOS from DSP\n"
  619. , __func__, audio);
  620. rc = wait_event_interruptible(audio->write_wait,
  621. (audio->eos_rsp || audio->wflush ||
  622. audio->stopped));
  623. if (rc < 0) {
  624. pr_err("%s[%pK]: wait event for eos_rsp failed, rc = %d\n",
  625. __func__, audio, rc);
  626. goto done;
  627. }
  628. if (audio->stopped || audio->wflush) {
  629. audio->wflush = 0;
  630. pr_debug("%s[%pK]: Audio Flushed or Stopped,this is not EOS\n"
  631. , __func__, audio);
  632. rc = -EBUSY;
  633. }
  634. if (audio->eos_rsp == 1)
  635. pr_debug("%s[%pK]: EOS\n", __func__, audio);
  636. done:
  637. mutex_lock(&audio->lock);
  638. audio->drv_status &= ~ADRV_STATUS_FSYNC;
  639. mutex_unlock(&audio->lock);
  640. return rc;
  641. }
  642. static int audio_aio_events_pending(struct q6audio_aio *audio)
  643. {
  644. unsigned long flags;
  645. int empty;
  646. spin_lock_irqsave(&audio->event_queue_lock, flags);
  647. empty = !list_empty(&audio->event_queue);
  648. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  649. return empty || audio->event_abort || audio->reset_event;
  650. }
  651. static long audio_aio_process_event_req_common(struct q6audio_aio *audio,
  652. struct msm_audio_event *usr_evt)
  653. {
  654. long rc;
  655. struct audio_aio_event *drv_evt = NULL;
  656. int timeout;
  657. unsigned long flags;
  658. timeout = usr_evt->timeout_ms;
  659. if (timeout > 0) {
  660. rc = wait_event_interruptible_timeout(audio->event_wait,
  661. audio_aio_events_pending
  662. (audio),
  663. msecs_to_jiffies
  664. (timeout));
  665. if (rc == 0)
  666. return -ETIMEDOUT;
  667. } else {
  668. rc = wait_event_interruptible(audio->event_wait,
  669. audio_aio_events_pending(audio));
  670. }
  671. if (rc < 0)
  672. return rc;
  673. if (audio->reset_event) {
  674. audio->reset_event = false;
  675. pr_err("In SSR, post ENETRESET err\n");
  676. return -ENETRESET;
  677. }
  678. if (audio->event_abort) {
  679. audio->event_abort = 0;
  680. return -ENODEV;
  681. }
  682. rc = 0;
  683. spin_lock_irqsave(&audio->event_queue_lock, flags);
  684. if (!list_empty(&audio->event_queue)) {
  685. drv_evt = list_first_entry(&audio->event_queue,
  686. struct audio_aio_event, list);
  687. list_del(&drv_evt->list);
  688. }
  689. if (drv_evt) {
  690. usr_evt->event_type = drv_evt->event_type;
  691. usr_evt->event_payload = drv_evt->payload;
  692. list_add_tail(&drv_evt->list, &audio->free_event_queue);
  693. } else {
  694. pr_err("%s[%pK]:Unexpected path\n", __func__, audio);
  695. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  696. return -EPERM;
  697. }
  698. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  699. if (drv_evt->event_type == AUDIO_EVENT_WRITE_DONE) {
  700. pr_debug("%s[%pK]:posted AUDIO_EVENT_WRITE_DONE to user\n",
  701. __func__, audio);
  702. mutex_lock(&audio->write_lock);
  703. audio_aio_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
  704. drv_evt->payload.aio_buf.buf_len, 0, 0);
  705. mutex_unlock(&audio->write_lock);
  706. } else if (drv_evt->event_type == AUDIO_EVENT_READ_DONE) {
  707. pr_debug("%s[%pK]:posted AUDIO_EVENT_READ_DONE to user\n",
  708. __func__, audio);
  709. mutex_lock(&audio->read_lock);
  710. audio_aio_ion_fixup(audio, drv_evt->payload.aio_buf.buf_addr,
  711. drv_evt->payload.aio_buf.buf_len, 0, 0);
  712. mutex_unlock(&audio->read_lock);
  713. }
  714. /* Some read buffer might be held up in DSP,release all
  715. * Once EOS indicated
  716. */
  717. if (audio->eos_rsp && !list_empty(&audio->in_queue)) {
  718. pr_debug("%s[%pK]:Send flush command to release read buffers held up in DSP\n",
  719. __func__, audio);
  720. mutex_lock(&audio->lock);
  721. audio_aio_flush(audio);
  722. mutex_unlock(&audio->lock);
  723. }
  724. return rc;
  725. }
  726. static long audio_aio_process_event_req(struct q6audio_aio *audio,
  727. void __user *arg)
  728. {
  729. long rc;
  730. struct msm_audio_event usr_evt;
  731. if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event))) {
  732. pr_err("%s: copy_from_user failed\n", __func__);
  733. return -EFAULT;
  734. }
  735. rc = audio_aio_process_event_req_common(audio, &usr_evt);
  736. if (copy_to_user(arg, &usr_evt, sizeof(usr_evt))) {
  737. pr_err("%s: copy_to_user failed\n", __func__);
  738. rc = -EFAULT;
  739. }
  740. return rc;
  741. }
  742. #ifdef CONFIG_COMPAT
  743. struct msm_audio_aio_buf32 {
  744. compat_uptr_t buf_addr;
  745. u32 buf_len;
  746. u32 data_len;
  747. compat_uptr_t private_data;
  748. u16 mfield_sz; /*only useful for data has meta field */
  749. };
  750. struct msm_audio_bitstream_info32 {
  751. u32 codec_type;
  752. u32 chan_info;
  753. u32 sample_rate;
  754. u32 bit_stream_info;
  755. u32 bit_rate;
  756. u32 unused[3];
  757. };
  758. struct msm_audio_bitstream_error_info32 {
  759. u32 dec_id;
  760. u32 err_msg_indicator;
  761. u32 err_type;
  762. };
  763. union msm_audio_event_payload32 {
  764. struct msm_audio_aio_buf32 aio_buf;
  765. struct msm_audio_bitstream_info32 stream_info;
  766. struct msm_audio_bitstream_error_info32 error_info;
  767. s32 reserved;
  768. };
  769. struct msm_audio_event32 {
  770. s32 event_type;
  771. s32 timeout_ms;
  772. union msm_audio_event_payload32 event_payload;
  773. };
  774. static long audio_aio_process_event_req_compat(struct q6audio_aio *audio,
  775. void __user *arg)
  776. {
  777. long rc;
  778. struct msm_audio_event32 usr_evt_32;
  779. struct msm_audio_event usr_evt;
  780. memset(&usr_evt, 0, sizeof(struct msm_audio_event));
  781. if (copy_from_user(&usr_evt_32, arg,
  782. sizeof(struct msm_audio_event32))) {
  783. pr_err("%s: copy_from_user failed\n", __func__);
  784. return -EFAULT;
  785. }
  786. usr_evt.timeout_ms = usr_evt_32.timeout_ms;
  787. rc = audio_aio_process_event_req_common(audio, &usr_evt);
  788. if (rc < 0) {
  789. pr_err("%s: audio process event failed, rc = %ld",
  790. __func__, rc);
  791. return rc;
  792. }
  793. usr_evt_32.event_type = usr_evt.event_type;
  794. switch (usr_evt_32.event_type) {
  795. case AUDIO_EVENT_SUSPEND:
  796. case AUDIO_EVENT_RESUME:
  797. case AUDIO_EVENT_WRITE_DONE:
  798. case AUDIO_EVENT_READ_DONE:
  799. usr_evt_32.event_payload.aio_buf.buf_addr =
  800. ptr_to_compat(usr_evt.event_payload.aio_buf.buf_addr);
  801. usr_evt_32.event_payload.aio_buf.buf_len =
  802. usr_evt.event_payload.aio_buf.buf_len;
  803. usr_evt_32.event_payload.aio_buf.data_len =
  804. usr_evt.event_payload.aio_buf.data_len;
  805. usr_evt_32.event_payload.aio_buf.private_data =
  806. ptr_to_compat(usr_evt.event_payload.aio_buf.private_data);
  807. usr_evt_32.event_payload.aio_buf.mfield_sz =
  808. usr_evt.event_payload.aio_buf.mfield_sz;
  809. break;
  810. case AUDIO_EVENT_STREAM_INFO:
  811. usr_evt_32.event_payload.stream_info.codec_type =
  812. usr_evt.event_payload.stream_info.codec_type;
  813. usr_evt_32.event_payload.stream_info.chan_info =
  814. usr_evt.event_payload.stream_info.chan_info;
  815. usr_evt_32.event_payload.stream_info.sample_rate =
  816. usr_evt.event_payload.stream_info.sample_rate;
  817. usr_evt_32.event_payload.stream_info.bit_stream_info =
  818. usr_evt.event_payload.stream_info.bit_stream_info;
  819. usr_evt_32.event_payload.stream_info.bit_rate =
  820. usr_evt.event_payload.stream_info.bit_rate;
  821. break;
  822. case AUDIO_EVENT_BITSTREAM_ERROR_INFO:
  823. usr_evt_32.event_payload.error_info.dec_id =
  824. usr_evt.event_payload.error_info.dec_id;
  825. usr_evt_32.event_payload.error_info.err_msg_indicator =
  826. usr_evt.event_payload.error_info.err_msg_indicator;
  827. usr_evt_32.event_payload.error_info.err_type =
  828. usr_evt.event_payload.error_info.err_type;
  829. break;
  830. default:
  831. pr_debug("%s: unknown audio event type = %d rc = %ld",
  832. __func__, usr_evt_32.event_type, rc);
  833. return rc;
  834. }
  835. if (copy_to_user(arg, &usr_evt_32, sizeof(usr_evt_32))) {
  836. pr_err("%s: copy_to_user failed\n", __func__);
  837. rc = -EFAULT;
  838. }
  839. return rc;
  840. }
  841. #endif
  842. static int audio_aio_ion_check(struct q6audio_aio *audio,
  843. void *vaddr, unsigned long len)
  844. {
  845. struct audio_aio_ion_region *region_elt;
  846. struct audio_aio_ion_region t = {.vaddr = vaddr, .len = len };
  847. list_for_each_entry(region_elt, &audio->ion_region_queue, list) {
  848. if (CONTAINS(region_elt, &t) || CONTAINS(&t, region_elt) ||
  849. OVERLAPS(region_elt, &t)) {
  850. pr_err("%s[%pK]:region (vaddr %pK len %ld) clashes with registered region (vaddr %pK paddr %pK len %ld)\n",
  851. __func__, audio, vaddr, len,
  852. region_elt->vaddr,
  853. &region_elt->paddr, region_elt->len);
  854. return -EINVAL;
  855. }
  856. }
  857. return 0;
  858. }
  859. static int audio_aio_ion_add(struct q6audio_aio *audio,
  860. struct msm_audio_ion_info *info)
  861. {
  862. dma_addr_t paddr = 0;
  863. size_t len = 0;
  864. struct audio_aio_ion_region *region;
  865. int rc = -EINVAL;
  866. struct dma_buf *dma_buf = NULL;
  867. unsigned long ionflag;
  868. void *kvaddr = NULL;
  869. pr_debug("%s[%pK]:\n", __func__, audio);
  870. region = kmalloc(sizeof(*region), GFP_KERNEL);
  871. if (!region) {
  872. rc = -ENOMEM;
  873. goto end;
  874. }
  875. rc = msm_audio_ion_import(&dma_buf, info->fd, &ionflag,
  876. 0, &paddr, &len, &kvaddr);
  877. if (rc) {
  878. pr_err("%s: msm audio ion alloc failed\n", __func__);
  879. goto import_error;
  880. }
  881. rc = audio_aio_ion_check(audio, info->vaddr, len);
  882. if (rc < 0) {
  883. pr_err("%s: audio_aio_ion_check failed\n", __func__);
  884. goto ion_error;
  885. }
  886. region->dma_buf = dma_buf;
  887. region->vaddr = info->vaddr;
  888. region->fd = info->fd;
  889. region->paddr = paddr;
  890. region->kvaddr = kvaddr;
  891. region->len = len;
  892. region->ref_cnt = 0;
  893. pr_debug("%s[%pK]:add region paddr %pK vaddr %pK, len %lu kvaddr %pK\n",
  894. __func__, audio,
  895. &region->paddr, region->vaddr, region->len,
  896. region->kvaddr);
  897. list_add_tail(&region->list, &audio->ion_region_queue);
  898. rc = q6asm_memory_map(audio->ac, paddr, IN, len, 1);
  899. if (rc < 0) {
  900. pr_err("%s[%pK]: memory map failed\n", __func__, audio);
  901. goto mmap_error;
  902. } else {
  903. goto end;
  904. }
  905. mmap_error:
  906. list_del(&region->list);
  907. ion_error:
  908. msm_audio_ion_free(dma_buf);
  909. import_error:
  910. kfree(region);
  911. end:
  912. return rc;
  913. }
  914. static int audio_aio_ion_remove(struct q6audio_aio *audio,
  915. struct msm_audio_ion_info *info)
  916. {
  917. struct audio_aio_ion_region *region;
  918. struct list_head *ptr, *next;
  919. int rc = -EINVAL;
  920. pr_debug("%s[%pK]:info fd %d vaddr %pK\n",
  921. __func__, audio, info->fd, info->vaddr);
  922. list_for_each_safe(ptr, next, &audio->ion_region_queue) {
  923. region = list_entry(ptr, struct audio_aio_ion_region, list);
  924. if ((region->fd == info->fd) &&
  925. (region->vaddr == info->vaddr)) {
  926. if (region->ref_cnt) {
  927. pr_debug("%s[%pK]:region %pK in use ref_cnt %d\n",
  928. __func__, audio, region,
  929. region->ref_cnt);
  930. break;
  931. }
  932. pr_debug("%s[%pK]:remove region fd %d vaddr %pK\n",
  933. __func__, audio, info->fd, info->vaddr);
  934. rc = q6asm_memory_unmap(audio->ac,
  935. region->paddr, IN);
  936. if (rc < 0)
  937. pr_err("%s[%pK]: memory unmap failed\n",
  938. __func__, audio);
  939. list_del(&region->list);
  940. msm_audio_ion_free(region->dma_buf);
  941. kfree(region);
  942. rc = 0;
  943. break;
  944. }
  945. }
  946. return rc;
  947. }
  948. static int audio_aio_async_write(struct q6audio_aio *audio,
  949. struct audio_aio_buffer_node *buf_node)
  950. {
  951. int rc;
  952. struct audio_client *ac;
  953. struct audio_aio_write_param param;
  954. memset(&param, 0, sizeof(param));
  955. if (!audio || !buf_node) {
  956. pr_err("%s NULL pointer audio=[0x%pK], buf_node=[0x%pK]\n",
  957. __func__, audio, buf_node);
  958. return -EINVAL;
  959. }
  960. pr_debug("%s[%pK]: Send write buff %pK phy %pK len %d meta_enable = %d\n",
  961. __func__, audio, buf_node, &buf_node->paddr,
  962. buf_node->buf.data_len,
  963. audio->buf_cfg.meta_info_enable);
  964. pr_debug("%s[%pK]: flags = 0x%x\n", __func__, audio,
  965. buf_node->meta_info.meta_in.nflags);
  966. ac = audio->ac;
  967. /* Offset with appropriate meta */
  968. if (audio->feedback) {
  969. /* Non Tunnel mode */
  970. param.paddr = buf_node->paddr + sizeof(struct dec_meta_in);
  971. param.len = buf_node->buf.data_len - sizeof(struct dec_meta_in);
  972. } else {
  973. /* Tunnel mode */
  974. param.paddr = buf_node->paddr;
  975. param.len = buf_node->buf.data_len;
  976. }
  977. param.msw_ts = buf_node->meta_info.meta_in.ntimestamp.highpart;
  978. param.lsw_ts = buf_node->meta_info.meta_in.ntimestamp.lowpart;
  979. param.flags = buf_node->meta_info.meta_in.nflags;
  980. /* If no meta_info enaled, indicate no time stamp valid */
  981. if (!audio->buf_cfg.meta_info_enable)
  982. param.flags = 0xFF00;
  983. if (buf_node->meta_info.meta_in.nflags & AUDIO_DEC_EOF_SET)
  984. param.flags |= AUDIO_DEC_EOF_SET;
  985. param.uid = ac->session;
  986. /* Read command will populate session id as token */
  987. buf_node->token = ac->session;
  988. rc = q6asm_async_write(ac, &param);
  989. if (rc < 0)
  990. pr_err_ratelimited("%s[%pK]:failed\n", __func__, audio);
  991. return rc;
  992. }
  993. void audio_aio_post_event(struct q6audio_aio *audio, int type,
  994. union msm_audio_event_payload payload)
  995. {
  996. struct audio_aio_event *e_node = NULL;
  997. unsigned long flags;
  998. spin_lock_irqsave(&audio->event_queue_lock, flags);
  999. if (!list_empty(&audio->free_event_queue)) {
  1000. e_node = list_first_entry(&audio->free_event_queue,
  1001. struct audio_aio_event, list);
  1002. list_del(&e_node->list);
  1003. } else {
  1004. e_node = kmalloc(sizeof(struct audio_aio_event), GFP_ATOMIC);
  1005. if (!e_node) {
  1006. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  1007. return;
  1008. }
  1009. }
  1010. e_node->event_type = type;
  1011. e_node->payload = payload;
  1012. list_add_tail(&e_node->list, &audio->event_queue);
  1013. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  1014. wake_up(&audio->event_wait);
  1015. }
  1016. static int audio_aio_async_read(struct q6audio_aio *audio,
  1017. struct audio_aio_buffer_node *buf_node)
  1018. {
  1019. struct audio_client *ac;
  1020. struct audio_aio_read_param param;
  1021. int rc;
  1022. pr_debug("%s[%pK]: Send read buff %pK phy %pK len %d\n",
  1023. __func__, audio, buf_node,
  1024. &buf_node->paddr, buf_node->buf.buf_len);
  1025. ac = audio->ac;
  1026. /* Provide address so driver can append nr frames information */
  1027. param.paddr = buf_node->paddr +
  1028. sizeof(struct dec_meta_out);
  1029. param.len = buf_node->buf.buf_len -
  1030. sizeof(struct dec_meta_out);
  1031. param.uid = ac->session;
  1032. /* Write command will populate session_id as token */
  1033. buf_node->token = ac->session;
  1034. rc = q6asm_async_read(ac, &param);
  1035. if (rc < 0 && rc != -ENETRESET)
  1036. pr_err_ratelimited("%s[%pK]:failed\n", __func__, audio);
  1037. return rc;
  1038. }
  1039. static int audio_aio_buf_add_shared(struct q6audio_aio *audio, u32 dir,
  1040. struct audio_aio_buffer_node *buf_node)
  1041. {
  1042. unsigned long flags;
  1043. int ret = 0;
  1044. pr_debug("%s[%pK]:node %pK dir %x buf_addr %pK buf_len %d data_len %d\n",
  1045. __func__, audio, buf_node, dir, buf_node->buf.buf_addr,
  1046. buf_node->buf.buf_len, buf_node->buf.data_len);
  1047. buf_node->paddr = audio_aio_ion_fixup(audio, buf_node->buf.buf_addr,
  1048. buf_node->buf.buf_len, 1,
  1049. &buf_node->kvaddr);
  1050. if (dir) {
  1051. /* write */
  1052. if (!buf_node->paddr ||
  1053. (buf_node->paddr & 0x1) ||
  1054. (!audio->feedback && !buf_node->buf.data_len)) {
  1055. kfree(buf_node);
  1056. return -EINVAL;
  1057. }
  1058. ret = extract_meta_out_info(audio, buf_node, 1);
  1059. if (ret) {
  1060. pr_debug("%s: extract meta failed with %d\n",
  1061. __func__, ret);
  1062. kfree(buf_node);
  1063. return ret;
  1064. }
  1065. /* Not a EOS buffer */
  1066. if (!(buf_node->meta_info.meta_in.nflags & AUDIO_DEC_EOS_SET)) {
  1067. spin_lock_irqsave(&audio->dsp_lock, flags);
  1068. ret = audio_aio_async_write(audio, buf_node);
  1069. /* EOS buffer handled in driver */
  1070. list_add_tail(&buf_node->list, &audio->out_queue);
  1071. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  1072. } else if (buf_node->meta_info.meta_in.nflags
  1073. & AUDIO_DEC_EOS_SET) {
  1074. if (!audio->wflush) {
  1075. pr_debug("%s[%pK]:Send EOS cmd at i/p\n",
  1076. __func__, audio);
  1077. /* Driver will forcefully post writedone event
  1078. * once eos ack recived from DSP
  1079. */
  1080. audio->eos_write_payload.aio_buf =
  1081. buf_node->buf;
  1082. audio->eos_flag = 1;
  1083. audio->eos_rsp = 0;
  1084. q6asm_cmd(audio->ac, CMD_EOS);
  1085. kfree(buf_node);
  1086. } else { /* Flush in progress, send back i/p
  1087. * EOS buffer as is
  1088. */
  1089. union msm_audio_event_payload event_payload;
  1090. event_payload.aio_buf = buf_node->buf;
  1091. audio_aio_post_event(audio,
  1092. AUDIO_EVENT_WRITE_DONE,
  1093. event_payload);
  1094. kfree(buf_node);
  1095. }
  1096. }
  1097. } else {
  1098. /* read */
  1099. if (!buf_node->paddr ||
  1100. (buf_node->paddr & 0x1) ||
  1101. (buf_node->buf.buf_len < PCM_BUFSZ_MIN)) {
  1102. kfree(buf_node);
  1103. return -EINVAL;
  1104. }
  1105. /* No EOS reached */
  1106. if (!audio->eos_rsp) {
  1107. spin_lock_irqsave(&audio->dsp_lock, flags);
  1108. ret = audio_aio_async_read(audio, buf_node);
  1109. /* EOS buffer handled in driver */
  1110. list_add_tail(&buf_node->list, &audio->in_queue);
  1111. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  1112. }
  1113. /* EOS reached at input side fake all upcoming read buffer to
  1114. * indicate the same
  1115. */
  1116. else {
  1117. union msm_audio_event_payload event_payload;
  1118. event_payload.aio_buf = buf_node->buf;
  1119. event_payload.aio_buf.data_len =
  1120. insert_eos_buf(audio, buf_node);
  1121. pr_debug("%s[%pK]: propagate READ_DONE as EOS done\n",
  1122. __func__, audio);
  1123. audio_aio_post_event(audio, AUDIO_EVENT_READ_DONE,
  1124. event_payload);
  1125. kfree(buf_node);
  1126. }
  1127. }
  1128. return ret;
  1129. }
  1130. #ifdef CONFIG_COMPAT
  1131. static int audio_aio_buf_add_compat(struct q6audio_aio *audio, u32 dir,
  1132. void __user *arg)
  1133. {
  1134. struct audio_aio_buffer_node *buf_node;
  1135. struct msm_audio_aio_buf32 aio_buf_32;
  1136. buf_node = kzalloc(sizeof(*buf_node), GFP_KERNEL);
  1137. if (!buf_node)
  1138. return -ENOMEM;
  1139. if (copy_from_user(&aio_buf_32, arg, sizeof(aio_buf_32))) {
  1140. kfree(buf_node);
  1141. pr_err("%s: copy_from_user failed\n", __func__);
  1142. return -EFAULT;
  1143. }
  1144. buf_node->buf.buf_addr = compat_ptr(aio_buf_32.buf_addr);
  1145. buf_node->buf.buf_len = aio_buf_32.buf_len;
  1146. buf_node->buf.data_len = aio_buf_32.data_len;
  1147. buf_node->buf.private_data = compat_ptr(aio_buf_32.private_data);
  1148. buf_node->buf.mfield_sz = aio_buf_32.mfield_sz;
  1149. return audio_aio_buf_add_shared(audio, dir, buf_node);
  1150. }
  1151. #endif
  1152. static int audio_aio_buf_add(struct q6audio_aio *audio, u32 dir,
  1153. void __user *arg)
  1154. {
  1155. struct audio_aio_buffer_node *buf_node;
  1156. buf_node = kzalloc(sizeof(*buf_node), GFP_KERNEL);
  1157. if (!buf_node)
  1158. return -ENOMEM;
  1159. if (copy_from_user(&buf_node->buf, arg, sizeof(buf_node->buf))) {
  1160. kfree(buf_node);
  1161. pr_err("%s: copy_from_user failed\n", __func__);
  1162. return -EFAULT;
  1163. }
  1164. return audio_aio_buf_add_shared(audio, dir, buf_node);
  1165. }
  1166. void audio_aio_ioport_reset(struct q6audio_aio *audio)
  1167. {
  1168. if (audio->drv_status & ADRV_STATUS_AIO_INTF) {
  1169. /* If fsync is in progress, make sure
  1170. * return value of fsync indicates
  1171. * abort due to flush
  1172. */
  1173. if (audio->drv_status & ADRV_STATUS_FSYNC) {
  1174. pr_debug("%s[%pK]:fsync in progress\n",
  1175. __func__, audio);
  1176. audio->drv_ops.out_flush(audio);
  1177. } else
  1178. audio->drv_ops.out_flush(audio);
  1179. if (audio->feedback == NON_TUNNEL_MODE)
  1180. audio->drv_ops.in_flush(audio);
  1181. }
  1182. }
  1183. int audio_aio_open(struct q6audio_aio *audio, struct file *file)
  1184. {
  1185. int rc = 0;
  1186. int i;
  1187. struct audio_aio_event *e_node = NULL;
  1188. struct list_head *ptr, *next;
  1189. /* Settings will be re-config at AUDIO_SET_CONFIG,
  1190. * but at least we need to have initial config
  1191. */
  1192. audio->str_cfg.buffer_size = FRAME_SIZE;
  1193. audio->str_cfg.buffer_count = FRAME_NUM;
  1194. audio->pcm_cfg.buffer_count = PCM_BUF_COUNT;
  1195. audio->pcm_cfg.sample_rate = 48000;
  1196. audio->pcm_cfg.channel_count = 2;
  1197. /* Only AIO interface */
  1198. if (file->f_flags & O_NONBLOCK) {
  1199. pr_debug("%s[%pK]:set to aio interface\n", __func__, audio);
  1200. audio->drv_status |= ADRV_STATUS_AIO_INTF;
  1201. audio->drv_ops.out_flush = audio_aio_async_out_flush;
  1202. audio->drv_ops.in_flush = audio_aio_async_in_flush;
  1203. q6asm_set_io_mode(audio->ac, ASYNC_IO_MODE);
  1204. } else {
  1205. pr_err_ratelimited("%s[%pK]:SIO interface not supported\n",
  1206. __func__, audio);
  1207. rc = -EACCES;
  1208. goto fail;
  1209. }
  1210. /* Initialize all locks of audio instance */
  1211. mutex_init(&audio->lock);
  1212. mutex_init(&audio->read_lock);
  1213. mutex_init(&audio->write_lock);
  1214. mutex_init(&audio->get_event_lock);
  1215. spin_lock_init(&audio->dsp_lock);
  1216. spin_lock_init(&audio->event_queue_lock);
  1217. init_waitqueue_head(&audio->cmd_wait);
  1218. init_waitqueue_head(&audio->write_wait);
  1219. init_waitqueue_head(&audio->event_wait);
  1220. INIT_LIST_HEAD(&audio->out_queue);
  1221. INIT_LIST_HEAD(&audio->in_queue);
  1222. INIT_LIST_HEAD(&audio->ion_region_queue);
  1223. INIT_LIST_HEAD(&audio->free_event_queue);
  1224. INIT_LIST_HEAD(&audio->event_queue);
  1225. audio->drv_ops.out_flush(audio);
  1226. audio->opened = 1;
  1227. audio->reset_event = false;
  1228. file->private_data = audio;
  1229. audio->codec_ioctl = audio_aio_ioctl;
  1230. audio->codec_compat_ioctl = audio_aio_compat_ioctl;
  1231. for (i = 0; i < AUDIO_EVENT_NUM; i++) {
  1232. e_node = kmalloc(sizeof(struct audio_aio_event), GFP_KERNEL);
  1233. if (e_node)
  1234. list_add_tail(&e_node->list, &audio->free_event_queue);
  1235. else {
  1236. rc = -ENOMEM;
  1237. goto cleanup;
  1238. }
  1239. }
  1240. rc = register_volume_listener(audio);
  1241. if (rc < 0)
  1242. goto cleanup;
  1243. return 0;
  1244. cleanup:
  1245. list_for_each_safe(ptr, next, &audio->free_event_queue) {
  1246. e_node = list_first_entry(&audio->free_event_queue,
  1247. struct audio_aio_event, list);
  1248. list_del(&e_node->list);
  1249. kfree(e_node);
  1250. }
  1251. fail:
  1252. return rc;
  1253. }
  1254. static long audio_aio_shared_ioctl(struct file *file, unsigned int cmd,
  1255. unsigned long arg)
  1256. {
  1257. struct q6audio_aio *audio = file->private_data;
  1258. int rc = 0;
  1259. switch (cmd) {
  1260. case AUDIO_ABORT_GET_EVENT: {
  1261. audio->event_abort = 1;
  1262. wake_up(&audio->event_wait);
  1263. break;
  1264. }
  1265. case AUDIO_OUTPORT_FLUSH: {
  1266. pr_debug("%s[%pK]:AUDIO_OUTPORT_FLUSH\n", __func__, audio);
  1267. mutex_lock(&audio->read_lock);
  1268. rc = audio_aio_outport_flush(audio);
  1269. if (rc < 0) {
  1270. pr_err_ratelimited("%s[%pK]: AUDIO_OUTPORT_FLUSH failed\n",
  1271. __func__, audio);
  1272. rc = -EINTR;
  1273. }
  1274. mutex_unlock(&audio->read_lock);
  1275. break;
  1276. }
  1277. case AUDIO_STOP: {
  1278. pr_debug("%s[%pK]: AUDIO_STOP session_id[%d]\n", __func__,
  1279. audio, audio->ac->session);
  1280. mutex_lock(&audio->lock);
  1281. audio->stopped = 1;
  1282. rc = audio_aio_flush(audio);
  1283. if (rc < 0) {
  1284. pr_err_ratelimited("%s[%pK]:Audio Stop procedure failed rc=%d\n",
  1285. __func__, audio, rc);
  1286. mutex_unlock(&audio->lock);
  1287. break;
  1288. }
  1289. audio->enabled = 0;
  1290. audio->drv_status &= ~ADRV_STATUS_PAUSE;
  1291. if (audio->drv_status & ADRV_STATUS_FSYNC) {
  1292. pr_debug("%s[%pK] Waking up the audio_aio_fsync\n",
  1293. __func__, audio);
  1294. wake_up(&audio->write_wait);
  1295. }
  1296. mutex_unlock(&audio->lock);
  1297. break;
  1298. }
  1299. case AUDIO_PAUSE: {
  1300. pr_debug("%s[%pK]:AUDIO_PAUSE %ld\n", __func__, audio, arg);
  1301. mutex_lock(&audio->lock);
  1302. if (arg == 1) {
  1303. rc = audio_aio_pause(audio);
  1304. if (rc < 0) {
  1305. pr_err_ratelimited("%s[%pK]: pause FAILED rc=%d\n",
  1306. __func__, audio, rc);
  1307. mutex_unlock(&audio->lock);
  1308. break;
  1309. }
  1310. audio->drv_status |= ADRV_STATUS_PAUSE;
  1311. } else if (arg == 0) {
  1312. if (audio->drv_status & ADRV_STATUS_PAUSE) {
  1313. rc = audio_aio_enable(audio);
  1314. if (rc)
  1315. pr_err_ratelimited("%s[%pK]: audio enable failed\n",
  1316. __func__, audio);
  1317. else {
  1318. audio->drv_status &= ~ADRV_STATUS_PAUSE;
  1319. audio->enabled = 1;
  1320. }
  1321. }
  1322. }
  1323. mutex_unlock(&audio->lock);
  1324. break;
  1325. }
  1326. case AUDIO_FLUSH: {
  1327. pr_debug("%s[%pK]: AUDIO_FLUSH sessionid[%d]\n", __func__,
  1328. audio, audio->ac->session);
  1329. mutex_lock(&audio->lock);
  1330. audio->rflush = 1;
  1331. audio->wflush = 1;
  1332. if (audio->drv_status & ADRV_STATUS_FSYNC) {
  1333. pr_debug("%s[%pK] Waking up the audio_aio_fsync\n",
  1334. __func__, audio);
  1335. wake_up(&audio->write_wait);
  1336. }
  1337. /* Flush DSP */
  1338. rc = audio_aio_flush(audio);
  1339. /* Flush input / Output buffer in software*/
  1340. audio_aio_ioport_reset(audio);
  1341. if (rc < 0) {
  1342. pr_err_ratelimited("%s[%pK]:AUDIO_FLUSH interrupted\n",
  1343. __func__, audio);
  1344. rc = -EINTR;
  1345. } else {
  1346. audio->rflush = 0;
  1347. if (audio->drv_status & ADRV_STATUS_FSYNC)
  1348. wake_up(&audio->write_wait);
  1349. else
  1350. audio->wflush = 0;
  1351. }
  1352. audio->eos_flag = 0;
  1353. audio->eos_rsp = 0;
  1354. mutex_unlock(&audio->lock);
  1355. break;
  1356. }
  1357. case AUDIO_GET_SESSION_ID: {
  1358. mutex_lock(&audio->lock);
  1359. if (copy_to_user((void *)arg, &audio->ac->session,
  1360. sizeof(u16))) {
  1361. pr_err_ratelimited("%s: copy_to_user for AUDIO_GET_SESSION_ID failed\n",
  1362. __func__);
  1363. rc = -EFAULT;
  1364. }
  1365. mutex_unlock(&audio->lock);
  1366. break;
  1367. }
  1368. case AUDIO_PM_AWAKE: {
  1369. if ((audio->audio_ws_mgr == NULL) ||
  1370. (audio->miscdevice == NULL)) {
  1371. pr_err_ratelimited("%s[%pK]: invalid ws_mgr or miscdevice",
  1372. __func__, audio);
  1373. rc = -EACCES;
  1374. break;
  1375. }
  1376. pr_debug("%s[%pK]:AUDIO_PM_AWAKE\n", __func__, audio);
  1377. mutex_lock(&audio->lock);
  1378. if (!audio->wakelock_voted) {
  1379. audio->wakelock_voted = true;
  1380. mutex_lock(&audio->audio_ws_mgr->ws_lock);
  1381. if (audio->audio_ws_mgr->ref_cnt++ == 0)
  1382. pm_stay_awake(audio->miscdevice->this_device);
  1383. mutex_unlock(&audio->audio_ws_mgr->ws_lock);
  1384. }
  1385. mutex_unlock(&audio->lock);
  1386. break;
  1387. }
  1388. case AUDIO_PM_RELAX: {
  1389. if ((audio->audio_ws_mgr == NULL) ||
  1390. (audio->miscdevice == NULL)) {
  1391. pr_err_ratelimited("%s[%pK]: invalid ws_mgr or miscdevice",
  1392. __func__, audio);
  1393. rc = -EACCES;
  1394. break;
  1395. }
  1396. pr_debug("%s[%pK]:AUDIO_PM_RELAX\n", __func__, audio);
  1397. mutex_lock(&audio->lock);
  1398. if (audio->wakelock_voted) {
  1399. audio->wakelock_voted = false;
  1400. mutex_lock(&audio->audio_ws_mgr->ws_lock);
  1401. if ((audio->audio_ws_mgr->ref_cnt > 0) &&
  1402. (--audio->audio_ws_mgr->ref_cnt == 0)) {
  1403. pm_relax(audio->miscdevice->this_device);
  1404. }
  1405. mutex_unlock(&audio->audio_ws_mgr->ws_lock);
  1406. }
  1407. mutex_unlock(&audio->lock);
  1408. break;
  1409. }
  1410. default:
  1411. pr_err_ratelimited("%s: Unknown ioctl cmd = %d", __func__, cmd);
  1412. rc = -EINVAL;
  1413. }
  1414. return rc;
  1415. }
  1416. static long audio_aio_ioctl(struct file *file, unsigned int cmd,
  1417. unsigned long arg)
  1418. {
  1419. struct q6audio_aio *audio = file->private_data;
  1420. int rc = 0;
  1421. switch (cmd) {
  1422. case AUDIO_ABORT_GET_EVENT:
  1423. case AUDIO_OUTPORT_FLUSH:
  1424. case AUDIO_STOP:
  1425. case AUDIO_PAUSE:
  1426. case AUDIO_FLUSH:
  1427. case AUDIO_GET_SESSION_ID:
  1428. case AUDIO_PM_AWAKE:
  1429. case AUDIO_PM_RELAX:
  1430. rc = audio_aio_shared_ioctl(file, cmd, arg);
  1431. break;
  1432. case AUDIO_GET_STATS: {
  1433. struct msm_audio_stats stats;
  1434. uint64_t timestamp;
  1435. memset(&stats, 0, sizeof(struct msm_audio_stats));
  1436. stats.byte_count = atomic_read(&audio->in_bytes);
  1437. stats.sample_count = atomic_read(&audio->in_samples);
  1438. rc = q6asm_get_session_time(audio->ac, &timestamp);
  1439. if (rc >= 0)
  1440. memcpy(&stats.unused[0], &timestamp, sizeof(timestamp));
  1441. else
  1442. pr_debug("Error while getting timestamp\n");
  1443. if (copy_to_user((void *)arg, &stats, sizeof(stats))) {
  1444. pr_err("%s: copy_frm_user for AUDIO_GET_STATS failed\n",
  1445. __func__);
  1446. rc = -EFAULT;
  1447. }
  1448. break;
  1449. }
  1450. case AUDIO_GET_EVENT: {
  1451. pr_debug("%s[%pK]:AUDIO_GET_EVENT\n", __func__, audio);
  1452. if (mutex_trylock(&audio->get_event_lock)) {
  1453. rc = audio_aio_process_event_req(audio,
  1454. (void __user *)arg);
  1455. mutex_unlock(&audio->get_event_lock);
  1456. } else
  1457. rc = -EBUSY;
  1458. break;
  1459. }
  1460. case AUDIO_ASYNC_WRITE: {
  1461. mutex_lock(&audio->write_lock);
  1462. if (audio->drv_status & ADRV_STATUS_FSYNC)
  1463. rc = -EBUSY;
  1464. else {
  1465. if (audio->enabled)
  1466. rc = audio_aio_buf_add(audio, 1,
  1467. (void __user *)arg);
  1468. else
  1469. rc = -EPERM;
  1470. }
  1471. mutex_unlock(&audio->write_lock);
  1472. break;
  1473. }
  1474. case AUDIO_ASYNC_READ: {
  1475. mutex_lock(&audio->read_lock);
  1476. if (audio->feedback)
  1477. rc = audio_aio_buf_add(audio, 0,
  1478. (void __user *)arg);
  1479. else
  1480. rc = -EPERM;
  1481. mutex_unlock(&audio->read_lock);
  1482. break;
  1483. }
  1484. case AUDIO_GET_STREAM_CONFIG: {
  1485. struct msm_audio_stream_config cfg;
  1486. mutex_lock(&audio->lock);
  1487. memset(&cfg, 0, sizeof(cfg));
  1488. cfg.buffer_size = audio->str_cfg.buffer_size;
  1489. cfg.buffer_count = audio->str_cfg.buffer_count;
  1490. pr_debug("%s[%pK]:GET STREAM CFG %d %d\n",
  1491. __func__, audio, cfg.buffer_size, cfg.buffer_count);
  1492. if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) {
  1493. pr_err(
  1494. "%s: copy_to_user for AUDIO_GET_STREAM_CONFIG failed\n",
  1495. __func__);
  1496. rc = -EFAULT;
  1497. }
  1498. mutex_unlock(&audio->lock);
  1499. break;
  1500. }
  1501. case AUDIO_SET_STREAM_CONFIG: {
  1502. struct msm_audio_stream_config cfg;
  1503. pr_debug("%s[%pK]:SET STREAM CONFIG\n", __func__, audio);
  1504. mutex_lock(&audio->lock);
  1505. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  1506. pr_err(
  1507. "%s: copy_from_user for AUDIO_SET_STREAM_CONFIG failed\n",
  1508. __func__);
  1509. rc = -EFAULT;
  1510. mutex_unlock(&audio->lock);
  1511. break;
  1512. }
  1513. audio->str_cfg.buffer_size = FRAME_SIZE;
  1514. audio->str_cfg.buffer_count = FRAME_NUM;
  1515. rc = 0;
  1516. mutex_unlock(&audio->lock);
  1517. break;
  1518. }
  1519. case AUDIO_GET_CONFIG: {
  1520. struct msm_audio_config cfg;
  1521. mutex_lock(&audio->lock);
  1522. if (copy_to_user((void *)arg, &audio->pcm_cfg, sizeof(cfg))) {
  1523. pr_err(
  1524. "%s: copy_to_user for AUDIO_GET_CONFIG failed\n",
  1525. __func__);
  1526. rc = -EFAULT;
  1527. }
  1528. mutex_unlock(&audio->lock);
  1529. break;
  1530. }
  1531. case AUDIO_SET_CONFIG: {
  1532. struct msm_audio_config config;
  1533. pr_debug("%s[%pK]:AUDIO_SET_CONFIG\n", __func__, audio);
  1534. mutex_lock(&audio->lock);
  1535. if (copy_from_user(&config, (void *)arg, sizeof(config))) {
  1536. pr_err(
  1537. "%s: copy_from_user for AUDIO_SET_CONFIG failed\n",
  1538. __func__);
  1539. rc = -EFAULT;
  1540. mutex_unlock(&audio->lock);
  1541. break;
  1542. }
  1543. if (audio->feedback != NON_TUNNEL_MODE) {
  1544. pr_err("%s[%pK]:Not sufficient permission to change the playback mode\n",
  1545. __func__, audio);
  1546. rc = -EACCES;
  1547. mutex_unlock(&audio->lock);
  1548. break;
  1549. }
  1550. if ((config.buffer_count > PCM_BUF_COUNT) ||
  1551. (config.buffer_count == 1))
  1552. config.buffer_count = PCM_BUF_COUNT;
  1553. if (config.buffer_size < PCM_BUFSZ_MIN)
  1554. config.buffer_size = PCM_BUFSZ_MIN;
  1555. audio->pcm_cfg.buffer_count = config.buffer_count;
  1556. audio->pcm_cfg.buffer_size = config.buffer_size;
  1557. audio->pcm_cfg.channel_count = config.channel_count;
  1558. audio->pcm_cfg.sample_rate = config.sample_rate;
  1559. rc = 0;
  1560. mutex_unlock(&audio->lock);
  1561. break;
  1562. }
  1563. case AUDIO_SET_BUF_CFG: {
  1564. struct msm_audio_buf_cfg cfg;
  1565. mutex_lock(&audio->lock);
  1566. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  1567. pr_err(
  1568. "%s: copy_from_user for AUDIO_GET_BUF CONFIG failed\n",
  1569. __func__);
  1570. rc = -EFAULT;
  1571. mutex_unlock(&audio->lock);
  1572. break;
  1573. }
  1574. if ((audio->feedback == NON_TUNNEL_MODE) &&
  1575. !cfg.meta_info_enable) {
  1576. rc = -EFAULT;
  1577. mutex_unlock(&audio->lock);
  1578. break;
  1579. }
  1580. audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
  1581. pr_debug("%s[%pK]:session id %d: Set-buf-cfg: meta[%d]",
  1582. __func__, audio,
  1583. audio->ac->session, cfg.meta_info_enable);
  1584. mutex_unlock(&audio->lock);
  1585. break;
  1586. }
  1587. case AUDIO_GET_BUF_CFG: {
  1588. pr_debug("%s[%pK]:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
  1589. __func__, audio,
  1590. audio->ac->session, audio->buf_cfg.meta_info_enable,
  1591. audio->buf_cfg.frames_per_buf);
  1592. mutex_lock(&audio->lock);
  1593. if (copy_to_user((void *)arg, &audio->buf_cfg,
  1594. sizeof(struct msm_audio_buf_cfg))) {
  1595. pr_err(
  1596. "%s: copy_to_user for AUDIO_GET_BUF_CONFIG failed\n",
  1597. __func__);
  1598. rc = -EFAULT;
  1599. }
  1600. mutex_unlock(&audio->lock);
  1601. break;
  1602. }
  1603. case AUDIO_REGISTER_ION: {
  1604. struct msm_audio_ion_info info;
  1605. pr_debug("%s[%pK]:AUDIO_REGISTER_ION\n", __func__, audio);
  1606. mutex_lock(&audio->lock);
  1607. if (copy_from_user(&info, (void *)arg, sizeof(info))) {
  1608. pr_err(
  1609. "%s: copy_from_user for AUDIO_REGISTER_ION failed\n",
  1610. __func__);
  1611. rc = -EFAULT;
  1612. } else {
  1613. mutex_lock(&audio->read_lock);
  1614. mutex_lock(&audio->write_lock);
  1615. rc = audio_aio_ion_add(audio, &info);
  1616. mutex_unlock(&audio->write_lock);
  1617. mutex_unlock(&audio->read_lock);
  1618. }
  1619. mutex_unlock(&audio->lock);
  1620. break;
  1621. }
  1622. case AUDIO_DEREGISTER_ION: {
  1623. struct msm_audio_ion_info info;
  1624. mutex_lock(&audio->lock);
  1625. pr_debug("%s[%pK]:AUDIO_DEREGISTER_ION\n", __func__, audio);
  1626. if (copy_from_user(&info, (void *)arg, sizeof(info))) {
  1627. pr_err(
  1628. "%s: copy_from_user for AUDIO_DEREGISTER_ION failed\n",
  1629. __func__);
  1630. rc = -EFAULT;
  1631. } else {
  1632. mutex_lock(&audio->read_lock);
  1633. mutex_lock(&audio->write_lock);
  1634. rc = audio_aio_ion_remove(audio, &info);
  1635. mutex_unlock(&audio->write_lock);
  1636. mutex_unlock(&audio->read_lock);
  1637. }
  1638. mutex_unlock(&audio->lock);
  1639. break;
  1640. }
  1641. default:
  1642. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  1643. rc = -EINVAL;
  1644. }
  1645. return rc;
  1646. }
  1647. #ifdef CONFIG_COMPAT
  1648. struct msm_audio_stream_config32 {
  1649. u32 buffer_size;
  1650. u32 buffer_count;
  1651. };
  1652. struct msm_audio_stats32 {
  1653. u32 byte_count;
  1654. u32 sample_count;
  1655. u32 unused[2];
  1656. };
  1657. struct msm_audio_config32 {
  1658. u32 buffer_size;
  1659. u32 buffer_count;
  1660. u32 channel_count;
  1661. u32 sample_rate;
  1662. u32 type;
  1663. u32 meta_field;
  1664. u32 bits;
  1665. u32 unused[3];
  1666. };
  1667. struct msm_audio_buf_cfg32 {
  1668. u32 meta_info_enable;
  1669. u32 frames_per_buf;
  1670. };
  1671. struct msm_audio_ion_info32 {
  1672. int fd;
  1673. compat_uptr_t vaddr;
  1674. };
  1675. enum {
  1676. AUDIO_GET_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, 3,
  1677. struct msm_audio_config32),
  1678. AUDIO_SET_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, 4,
  1679. struct msm_audio_config32),
  1680. AUDIO_GET_STATS_32 = _IOR(AUDIO_IOCTL_MAGIC, 5,
  1681. struct msm_audio_stats32),
  1682. AUDIO_GET_EVENT_32 = _IOR(AUDIO_IOCTL_MAGIC, 13,
  1683. struct msm_audio_event32),
  1684. AUDIO_ASYNC_WRITE_32 = _IOW(AUDIO_IOCTL_MAGIC, 17,
  1685. struct msm_audio_aio_buf32),
  1686. AUDIO_ASYNC_READ_32 = _IOW(AUDIO_IOCTL_MAGIC, 18,
  1687. struct msm_audio_aio_buf32),
  1688. AUDIO_SET_STREAM_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, 80,
  1689. struct msm_audio_stream_config32),
  1690. AUDIO_GET_STREAM_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, 81,
  1691. struct msm_audio_stream_config32),
  1692. AUDIO_GET_BUF_CFG_32 = _IOW(AUDIO_IOCTL_MAGIC, 93,
  1693. struct msm_audio_buf_cfg32),
  1694. AUDIO_SET_BUF_CFG_32 = _IOW(AUDIO_IOCTL_MAGIC, 94,
  1695. struct msm_audio_buf_cfg32),
  1696. AUDIO_REGISTER_ION_32 = _IOW(AUDIO_IOCTL_MAGIC, 97,
  1697. struct msm_audio_ion_info32),
  1698. AUDIO_DEREGISTER_ION_32 = _IOW(AUDIO_IOCTL_MAGIC, 98,
  1699. struct msm_audio_ion_info32),
  1700. };
  1701. static long audio_aio_compat_ioctl(struct file *file, unsigned int cmd,
  1702. unsigned long arg)
  1703. {
  1704. struct q6audio_aio *audio = file->private_data;
  1705. int rc = 0;
  1706. switch (cmd) {
  1707. case AUDIO_ABORT_GET_EVENT:
  1708. case AUDIO_OUTPORT_FLUSH:
  1709. case AUDIO_STOP:
  1710. case AUDIO_PAUSE:
  1711. case AUDIO_FLUSH:
  1712. case AUDIO_GET_SESSION_ID:
  1713. case AUDIO_PM_AWAKE:
  1714. case AUDIO_PM_RELAX:
  1715. rc = audio_aio_shared_ioctl(file, cmd, arg);
  1716. break;
  1717. case AUDIO_GET_STATS_32: {
  1718. struct msm_audio_stats32 stats;
  1719. uint64_t timestamp;
  1720. memset(&stats, 0, sizeof(struct msm_audio_stats32));
  1721. stats.byte_count = atomic_read(&audio->in_bytes);
  1722. stats.sample_count = atomic_read(&audio->in_samples);
  1723. rc = q6asm_get_session_time(audio->ac, &timestamp);
  1724. if (rc >= 0)
  1725. memcpy(&stats.unused[0], &timestamp, sizeof(timestamp));
  1726. else
  1727. pr_debug("Error while getting timestamp\n");
  1728. if (copy_to_user((void *)arg, &stats, sizeof(stats))) {
  1729. pr_err(
  1730. "%s: copy_to_user for AUDIO_GET_STATS_32 failed\n",
  1731. __func__);
  1732. rc = -EFAULT;
  1733. }
  1734. break;
  1735. }
  1736. case AUDIO_GET_EVENT_32: {
  1737. pr_debug("%s[%pK]:AUDIO_GET_EVENT\n", __func__, audio);
  1738. if (mutex_trylock(&audio->get_event_lock)) {
  1739. rc = audio_aio_process_event_req_compat(audio,
  1740. (void __user *)arg);
  1741. mutex_unlock(&audio->get_event_lock);
  1742. } else
  1743. rc = -EBUSY;
  1744. break;
  1745. }
  1746. case AUDIO_ASYNC_WRITE_32: {
  1747. mutex_lock(&audio->write_lock);
  1748. if (audio->drv_status & ADRV_STATUS_FSYNC)
  1749. rc = -EBUSY;
  1750. else {
  1751. if (audio->enabled)
  1752. rc = audio_aio_buf_add_compat(audio, 1,
  1753. (void __user *)arg);
  1754. else
  1755. rc = -EPERM;
  1756. }
  1757. mutex_unlock(&audio->write_lock);
  1758. break;
  1759. }
  1760. case AUDIO_ASYNC_READ_32: {
  1761. mutex_lock(&audio->read_lock);
  1762. if (audio->feedback)
  1763. rc = audio_aio_buf_add_compat(audio, 0,
  1764. (void __user *)arg);
  1765. else
  1766. rc = -EPERM;
  1767. mutex_unlock(&audio->read_lock);
  1768. break;
  1769. }
  1770. case AUDIO_GET_STREAM_CONFIG_32: {
  1771. struct msm_audio_stream_config32 cfg;
  1772. mutex_lock(&audio->lock);
  1773. memset(&cfg, 0, sizeof(cfg));
  1774. cfg.buffer_size = audio->str_cfg.buffer_size;
  1775. cfg.buffer_count = audio->str_cfg.buffer_count;
  1776. pr_debug("%s[%pK]:GET STREAM CFG %d %d\n",
  1777. __func__, audio, cfg.buffer_size, cfg.buffer_count);
  1778. if (copy_to_user((void *)arg, &cfg, sizeof(cfg))) {
  1779. pr_err("%s: copy_to_user for AUDIO_GET_STREAM_CONFIG_32 failed\n",
  1780. __func__);
  1781. rc = -EFAULT;
  1782. }
  1783. mutex_unlock(&audio->lock);
  1784. break;
  1785. }
  1786. case AUDIO_SET_STREAM_CONFIG_32: {
  1787. struct msm_audio_stream_config32 cfg_32;
  1788. struct msm_audio_stream_config cfg;
  1789. pr_debug("%s[%pK]:SET STREAM CONFIG\n", __func__, audio);
  1790. mutex_lock(&audio->lock);
  1791. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  1792. pr_err("%s: copy_from_user for AUDIO_SET_STREAM_CONFIG_32 failed\n",
  1793. __func__);
  1794. rc = -EFAULT;
  1795. mutex_unlock(&audio->lock);
  1796. break;
  1797. }
  1798. cfg.buffer_size = cfg_32.buffer_size;
  1799. cfg.buffer_count = cfg_32.buffer_count;
  1800. audio->str_cfg.buffer_size = FRAME_SIZE;
  1801. audio->str_cfg.buffer_count = FRAME_NUM;
  1802. rc = 0;
  1803. mutex_unlock(&audio->lock);
  1804. break;
  1805. }
  1806. case AUDIO_GET_CONFIG_32: {
  1807. struct msm_audio_config32 cfg_32;
  1808. mutex_lock(&audio->lock);
  1809. memset(&cfg_32, 0, sizeof(cfg_32));
  1810. cfg_32.buffer_size = audio->pcm_cfg.buffer_size;
  1811. cfg_32.buffer_count = audio->pcm_cfg.buffer_count;
  1812. cfg_32.channel_count = audio->pcm_cfg.channel_count;
  1813. cfg_32.sample_rate = audio->pcm_cfg.sample_rate;
  1814. cfg_32.type = audio->pcm_cfg.type;
  1815. cfg_32.meta_field = audio->pcm_cfg.meta_field;
  1816. cfg_32.bits = audio->pcm_cfg.bits;
  1817. if (copy_to_user((void *)arg, &cfg_32, sizeof(cfg_32))) {
  1818. pr_err("%s: copy_to_user for AUDIO_GET_CONFIG_32 failed\n",
  1819. __func__);
  1820. rc = -EFAULT;
  1821. }
  1822. mutex_unlock(&audio->lock);
  1823. break;
  1824. }
  1825. case AUDIO_SET_CONFIG_32: {
  1826. struct msm_audio_config config;
  1827. struct msm_audio_config32 config_32;
  1828. mutex_lock(&audio->lock);
  1829. if (audio->feedback != NON_TUNNEL_MODE) {
  1830. pr_err("%s[%pK]:Not sufficient permission to change the playback mode\n",
  1831. __func__, audio);
  1832. rc = -EACCES;
  1833. mutex_unlock(&audio->lock);
  1834. break;
  1835. }
  1836. pr_debug("%s[%pK]:AUDIO_SET_CONFIG\n", __func__, audio);
  1837. if (copy_from_user(&config_32, (void *)arg,
  1838. sizeof(config_32))) {
  1839. pr_err("%s: copy_from_user for AUDIO_SET_CONFIG_32 failed\n",
  1840. __func__);
  1841. rc = -EFAULT;
  1842. mutex_unlock(&audio->lock);
  1843. break;
  1844. }
  1845. config.buffer_size = config_32.buffer_size;
  1846. config.buffer_count = config_32.buffer_count;
  1847. config.channel_count = config_32.channel_count;
  1848. config.sample_rate = config_32.sample_rate;
  1849. config.type = config_32.type;
  1850. config.meta_field = config_32.meta_field;
  1851. config.bits = config_32.bits;
  1852. if ((config.buffer_count > PCM_BUF_COUNT) ||
  1853. (config.buffer_count == 1))
  1854. config.buffer_count = PCM_BUF_COUNT;
  1855. if (config.buffer_size < PCM_BUFSZ_MIN)
  1856. config.buffer_size = PCM_BUFSZ_MIN;
  1857. audio->pcm_cfg.buffer_count = config.buffer_count;
  1858. audio->pcm_cfg.buffer_size = config.buffer_size;
  1859. audio->pcm_cfg.channel_count = config.channel_count;
  1860. audio->pcm_cfg.sample_rate = config.sample_rate;
  1861. rc = 0;
  1862. mutex_unlock(&audio->lock);
  1863. break;
  1864. }
  1865. case AUDIO_SET_BUF_CFG_32: {
  1866. struct msm_audio_buf_cfg cfg;
  1867. struct msm_audio_buf_cfg32 cfg_32;
  1868. mutex_lock(&audio->lock);
  1869. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  1870. pr_err("%s: copy_from_user for AUDIO_SET_CONFIG_32 failed\n",
  1871. __func__);
  1872. rc = -EFAULT;
  1873. mutex_unlock(&audio->lock);
  1874. break;
  1875. }
  1876. cfg.meta_info_enable = cfg_32.meta_info_enable;
  1877. cfg.frames_per_buf = cfg_32.frames_per_buf;
  1878. if ((audio->feedback == NON_TUNNEL_MODE) &&
  1879. !cfg.meta_info_enable) {
  1880. rc = -EFAULT;
  1881. mutex_unlock(&audio->lock);
  1882. break;
  1883. }
  1884. audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
  1885. pr_debug("%s[%pK]:session id %d: Set-buf-cfg: meta[%d]",
  1886. __func__, audio,
  1887. audio->ac->session, cfg.meta_info_enable);
  1888. mutex_unlock(&audio->lock);
  1889. break;
  1890. }
  1891. case AUDIO_GET_BUF_CFG_32: {
  1892. struct msm_audio_buf_cfg32 cfg_32;
  1893. pr_debug("%s[%pK]:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
  1894. __func__, audio,
  1895. audio->ac->session, audio->buf_cfg.meta_info_enable,
  1896. audio->buf_cfg.frames_per_buf);
  1897. mutex_lock(&audio->lock);
  1898. memset(&cfg_32, 0, sizeof(cfg_32));
  1899. cfg_32.meta_info_enable = audio->buf_cfg.meta_info_enable;
  1900. cfg_32.frames_per_buf = audio->buf_cfg.frames_per_buf;
  1901. if (copy_to_user((void *)arg, &cfg_32,
  1902. sizeof(struct msm_audio_buf_cfg32))) {
  1903. pr_err("%s: copy_to_user for AUDIO_GET_BUF_CFG_32 failed\n",
  1904. __func__);
  1905. rc = -EFAULT;
  1906. }
  1907. mutex_unlock(&audio->lock);
  1908. break;
  1909. }
  1910. case AUDIO_REGISTER_ION_32: {
  1911. struct msm_audio_ion_info32 info_32;
  1912. struct msm_audio_ion_info info;
  1913. pr_debug("%s[%pK]:AUDIO_REGISTER_ION\n", __func__, audio);
  1914. mutex_lock(&audio->lock);
  1915. if (copy_from_user(&info_32, (void *)arg, sizeof(info_32))) {
  1916. pr_err("%s: copy_from_user for AUDIO_REGISTER_ION_32 failed\n",
  1917. __func__);
  1918. rc = -EFAULT;
  1919. } else {
  1920. info.fd = info_32.fd;
  1921. info.vaddr = compat_ptr(info_32.vaddr);
  1922. mutex_lock(&audio->read_lock);
  1923. mutex_lock(&audio->write_lock);
  1924. rc = audio_aio_ion_add(audio, &info);
  1925. mutex_unlock(&audio->write_lock);
  1926. mutex_unlock(&audio->read_lock);
  1927. }
  1928. mutex_unlock(&audio->lock);
  1929. break;
  1930. }
  1931. case AUDIO_DEREGISTER_ION_32: {
  1932. struct msm_audio_ion_info32 info_32;
  1933. struct msm_audio_ion_info info;
  1934. mutex_lock(&audio->lock);
  1935. pr_debug("%s[%pK]:AUDIO_DEREGISTER_ION\n", __func__, audio);
  1936. if (copy_from_user(&info_32, (void *)arg, sizeof(info_32))) {
  1937. pr_err("%s: copy_from_user for AUDIO_DEREGISTER_ION_32 failed\n",
  1938. __func__);
  1939. rc = -EFAULT;
  1940. } else {
  1941. info.fd = info_32.fd;
  1942. info.vaddr = compat_ptr(info_32.vaddr);
  1943. mutex_lock(&audio->read_lock);
  1944. mutex_lock(&audio->write_lock);
  1945. rc = audio_aio_ion_remove(audio, &info);
  1946. mutex_unlock(&audio->write_lock);
  1947. mutex_unlock(&audio->read_lock);
  1948. }
  1949. mutex_unlock(&audio->lock);
  1950. break;
  1951. }
  1952. default:
  1953. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  1954. rc = -EINVAL;
  1955. }
  1956. return rc;
  1957. }
  1958. #endif