audio_utils_aio.c 58 KB

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