audio_utils_aio.c 58 KB

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