audio_utils_aio.c 58 KB

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