audio_utils_aio.c 58 KB

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