vx_pcm.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * Driver for Digigram VX soundcards
  3. *
  4. * PCM part
  5. *
  6. * Copyright (c) 2002,2003 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * STRATEGY
  24. * for playback, we send series of "chunks", which size is equal with the
  25. * IBL size, typically 126 samples. at each end of chunk, the end-of-buffer
  26. * interrupt is notified, and the interrupt handler will feed the next chunk.
  27. *
  28. * the current position is calculated from the sample count RMH.
  29. * pipe->transferred is the counter of data which has been already transferred.
  30. * if this counter reaches to the period size, snd_pcm_period_elapsed() will
  31. * be issued.
  32. *
  33. * for capture, the situation is much easier.
  34. * to get a low latency response, we'll check the capture streams at each
  35. * interrupt (capture stream has no EOB notification). if the pending
  36. * data is accumulated to the period size, snd_pcm_period_elapsed() is
  37. * called and the pointer is updated.
  38. *
  39. * the current point of read buffer is kept in pipe->hw_ptr. note that
  40. * this is in bytes.
  41. *
  42. *
  43. * TODO
  44. * - linked trigger for full-duplex mode.
  45. * - scheduled action on the stream.
  46. */
  47. #include <linux/slab.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/delay.h>
  50. #include <sound/core.h>
  51. #include <sound/asoundef.h>
  52. #include <sound/pcm.h>
  53. #include <sound/vx_core.h>
  54. #include "vx_cmd.h"
  55. /*
  56. * we use a vmalloc'ed (sg-)buffer
  57. */
  58. /* get the physical page pointer on the given offset */
  59. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  60. unsigned long offset)
  61. {
  62. void *pageptr = subs->runtime->dma_area + offset;
  63. return vmalloc_to_page(pageptr);
  64. }
  65. /*
  66. * allocate a buffer via vmalloc_32().
  67. * called from hw_params
  68. * NOTE: this may be called not only once per pcm open!
  69. */
  70. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
  71. {
  72. struct snd_pcm_runtime *runtime = subs->runtime;
  73. if (runtime->dma_area) {
  74. /* already allocated */
  75. if (runtime->dma_bytes >= size)
  76. return 0; /* already enough large */
  77. vfree(runtime->dma_area);
  78. }
  79. runtime->dma_area = vmalloc_32(size);
  80. if (! runtime->dma_area)
  81. return -ENOMEM;
  82. memset(runtime->dma_area, 0, size);
  83. runtime->dma_bytes = size;
  84. return 1; /* changed */
  85. }
  86. /*
  87. * free the buffer.
  88. * called from hw_free callback
  89. * NOTE: this may be called not only once per pcm open!
  90. */
  91. static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
  92. {
  93. struct snd_pcm_runtime *runtime = subs->runtime;
  94. vfree(runtime->dma_area);
  95. runtime->dma_area = NULL;
  96. return 0;
  97. }
  98. /*
  99. * read three pending pcm bytes via inb()
  100. */
  101. static void vx_pcm_read_per_bytes(struct vx_core *chip, struct snd_pcm_runtime *runtime,
  102. struct vx_pipe *pipe)
  103. {
  104. int offset = pipe->hw_ptr;
  105. unsigned char *buf = (unsigned char *)(runtime->dma_area + offset);
  106. *buf++ = vx_inb(chip, RXH);
  107. if (++offset >= pipe->buffer_bytes) {
  108. offset = 0;
  109. buf = (unsigned char *)runtime->dma_area;
  110. }
  111. *buf++ = vx_inb(chip, RXM);
  112. if (++offset >= pipe->buffer_bytes) {
  113. offset = 0;
  114. buf = (unsigned char *)runtime->dma_area;
  115. }
  116. *buf++ = vx_inb(chip, RXL);
  117. if (++offset >= pipe->buffer_bytes) {
  118. offset = 0;
  119. buf = (unsigned char *)runtime->dma_area;
  120. }
  121. pipe->hw_ptr = offset;
  122. }
  123. /*
  124. * vx_set_pcx_time - convert from the PC time to the RMH status time.
  125. * @pc_time: the pointer for the PC-time to set
  126. * @dsp_time: the pointer for RMH status time array
  127. */
  128. static void vx_set_pcx_time(struct vx_core *chip, pcx_time_t *pc_time,
  129. unsigned int *dsp_time)
  130. {
  131. dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
  132. dsp_time[1] = (unsigned int)(*pc_time) & MASK_DSP_WORD;
  133. }
  134. /*
  135. * vx_set_differed_time - set the differed time if specified
  136. * @rmh: the rmh record to modify
  137. * @pipe: the pipe to be checked
  138. *
  139. * if the pipe is programmed with the differed time, set the DSP time
  140. * on the rmh and changes its command length.
  141. *
  142. * returns the increase of the command length.
  143. */
  144. static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
  145. struct vx_pipe *pipe)
  146. {
  147. /* Update The length added to the RMH command by the timestamp */
  148. if (! (pipe->differed_type & DC_DIFFERED_DELAY))
  149. return 0;
  150. /* Set the T bit */
  151. rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
  152. /* Time stamp is the 1st following parameter */
  153. vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
  154. /* Add the flags to a notified differed command */
  155. if (pipe->differed_type & DC_NOTIFY_DELAY)
  156. rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;
  157. /* Add the flags to a multiple differed command */
  158. if (pipe->differed_type & DC_MULTIPLE_DELAY)
  159. rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;
  160. /* Add the flags to a stream-time differed command */
  161. if (pipe->differed_type & DC_STREAM_TIME_DELAY)
  162. rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
  163. rmh->LgCmd += 2;
  164. return 2;
  165. }
  166. /*
  167. * vx_set_stream_format - send the stream format command
  168. * @pipe: the affected pipe
  169. * @data: format bitmask
  170. */
  171. static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
  172. unsigned int data)
  173. {
  174. struct vx_rmh rmh;
  175. vx_init_rmh(&rmh, pipe->is_capture ?
  176. CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
  177. rmh.Cmd[0] |= pipe->number << FIELD_SIZE;
  178. /* Command might be longer since we may have to add a timestamp */
  179. vx_set_differed_time(chip, &rmh, pipe);
  180. rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
  181. rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
  182. rmh.LgCmd += 2;
  183. return vx_send_msg(chip, &rmh);
  184. }
  185. /*
  186. * vx_set_format - set the format of a pipe
  187. * @pipe: the affected pipe
  188. * @runtime: pcm runtime instance to be referred
  189. *
  190. * returns 0 if successful, or a negative error code.
  191. */
  192. static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
  193. struct snd_pcm_runtime *runtime)
  194. {
  195. unsigned int header = HEADER_FMT_BASE;
  196. if (runtime->channels == 1)
  197. header |= HEADER_FMT_MONO;
  198. if (snd_pcm_format_little_endian(runtime->format))
  199. header |= HEADER_FMT_INTEL;
  200. if (runtime->rate < 32000 && runtime->rate > 11025)
  201. header |= HEADER_FMT_UPTO32;
  202. else if (runtime->rate <= 11025)
  203. header |= HEADER_FMT_UPTO11;
  204. switch (snd_pcm_format_physical_width(runtime->format)) {
  205. // case 8: break;
  206. case 16: header |= HEADER_FMT_16BITS; break;
  207. case 24: header |= HEADER_FMT_24BITS; break;
  208. default :
  209. snd_BUG();
  210. return -EINVAL;
  211. };
  212. return vx_set_stream_format(chip, pipe, header);
  213. }
  214. /*
  215. * set / query the IBL size
  216. */
  217. static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
  218. {
  219. int err;
  220. struct vx_rmh rmh;
  221. vx_init_rmh(&rmh, CMD_IBL);
  222. rmh.Cmd[0] |= info->size & 0x03ffff;
  223. err = vx_send_msg(chip, &rmh);
  224. if (err < 0)
  225. return err;
  226. info->size = rmh.Stat[0];
  227. info->max_size = rmh.Stat[1];
  228. info->min_size = rmh.Stat[2];
  229. info->granularity = rmh.Stat[3];
  230. snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
  231. info->size, info->max_size, info->min_size, info->granularity);
  232. return 0;
  233. }
  234. /*
  235. * vx_get_pipe_state - get the state of a pipe
  236. * @pipe: the pipe to be checked
  237. * @state: the pointer for the returned state
  238. *
  239. * checks the state of a given pipe, and stores the state (1 = running,
  240. * 0 = paused) on the given pointer.
  241. *
  242. * called from trigger callback only
  243. */
  244. static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
  245. {
  246. int err;
  247. struct vx_rmh rmh;
  248. vx_init_rmh(&rmh, CMD_PIPE_STATE);
  249. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  250. err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  251. if (! err)
  252. *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
  253. return err;
  254. }
  255. /*
  256. * vx_query_hbuffer_size - query available h-buffer size in bytes
  257. * @pipe: the pipe to be checked
  258. *
  259. * return the available size on h-buffer in bytes,
  260. * or a negative error code.
  261. *
  262. * NOTE: calling this function always switches to the stream mode.
  263. * you'll need to disconnect the host to get back to the
  264. * normal mode.
  265. */
  266. static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
  267. {
  268. int result;
  269. struct vx_rmh rmh;
  270. vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
  271. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  272. if (pipe->is_capture)
  273. rmh.Cmd[0] |= 0x00000001;
  274. result = vx_send_msg(chip, &rmh);
  275. if (! result)
  276. result = rmh.Stat[0] & 0xffff;
  277. return result;
  278. }
  279. /*
  280. * vx_pipe_can_start - query whether a pipe is ready for start
  281. * @pipe: the pipe to be checked
  282. *
  283. * return 1 if ready, 0 if not ready, and negative value on error.
  284. *
  285. * called from trigger callback only
  286. */
  287. static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
  288. {
  289. int err;
  290. struct vx_rmh rmh;
  291. vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
  292. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  293. rmh.Cmd[0] |= 1;
  294. err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  295. if (! err) {
  296. if (rmh.Stat[0])
  297. err = 1;
  298. }
  299. return err;
  300. }
  301. /*
  302. * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
  303. * @pipe: the pipe to be configured
  304. */
  305. static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
  306. {
  307. struct vx_rmh rmh;
  308. vx_init_rmh(&rmh, CMD_CONF_PIPE);
  309. if (pipe->is_capture)
  310. rmh.Cmd[0] |= COMMAND_RECORD_MASK;
  311. rmh.Cmd[1] = 1 << pipe->number;
  312. return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  313. }
  314. /*
  315. * vx_send_irqa - trigger IRQA
  316. */
  317. static int vx_send_irqa(struct vx_core *chip)
  318. {
  319. struct vx_rmh rmh;
  320. vx_init_rmh(&rmh, CMD_SEND_IRQA);
  321. return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  322. }
  323. #define MAX_WAIT_FOR_DSP 250
  324. /*
  325. * vx boards do not support inter-card sync, besides
  326. * only 126 samples require to be prepared before a pipe can start
  327. */
  328. #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
  329. #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
  330. /*
  331. * vx_toggle_pipe - start / pause a pipe
  332. * @pipe: the pipe to be triggered
  333. * @state: start = 1, pause = 0
  334. *
  335. * called from trigger callback only
  336. *
  337. */
  338. static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
  339. {
  340. int err, i, cur_state;
  341. /* Check the pipe is not already in the requested state */
  342. if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
  343. return -EBADFD;
  344. if (state == cur_state)
  345. return 0;
  346. /* If a start is requested, ask the DSP to get prepared
  347. * and wait for a positive acknowledge (when there are
  348. * enough sound buffer for this pipe)
  349. */
  350. if (state) {
  351. for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
  352. err = vx_pipe_can_start(chip, pipe);
  353. if (err > 0)
  354. break;
  355. /* Wait for a few, before asking again
  356. * to avoid flooding the DSP with our requests
  357. */
  358. mdelay(1);
  359. }
  360. }
  361. if ((err = vx_conf_pipe(chip, pipe)) < 0)
  362. return err;
  363. if ((err = vx_send_irqa(chip)) < 0)
  364. return err;
  365. /* If it completes successfully, wait for the pipes
  366. * reaching the expected state before returning
  367. * Check one pipe only (since they are synchronous)
  368. */
  369. for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
  370. err = vx_get_pipe_state(chip, pipe, &cur_state);
  371. if (err < 0 || cur_state == state)
  372. break;
  373. err = -EIO;
  374. mdelay(1);
  375. }
  376. return err < 0 ? -EIO : 0;
  377. }
  378. /*
  379. * vx_stop_pipe - stop a pipe
  380. * @pipe: the pipe to be stopped
  381. *
  382. * called from trigger callback only
  383. */
  384. static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
  385. {
  386. struct vx_rmh rmh;
  387. vx_init_rmh(&rmh, CMD_STOP_PIPE);
  388. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  389. return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  390. }
  391. /*
  392. * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
  393. * @capture: 0 = playback, 1 = capture operation
  394. * @audioid: the audio id to be assigned
  395. * @num_audio: number of audio channels
  396. * @pipep: the returned pipe instance
  397. *
  398. * return 0 on success, or a negative error code.
  399. */
  400. static int vx_alloc_pipe(struct vx_core *chip, int capture,
  401. int audioid, int num_audio,
  402. struct vx_pipe **pipep)
  403. {
  404. int err;
  405. struct vx_pipe *pipe;
  406. struct vx_rmh rmh;
  407. int data_mode;
  408. *pipep = NULL;
  409. vx_init_rmh(&rmh, CMD_RES_PIPE);
  410. vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
  411. #if 0 // NYI
  412. if (underrun_skip_sound)
  413. rmh.Cmd[0] |= BIT_SKIP_SOUND;
  414. #endif // NYI
  415. data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
  416. if (! capture && data_mode)
  417. rmh.Cmd[0] |= BIT_DATA_MODE;
  418. err = vx_send_msg(chip, &rmh);
  419. if (err < 0)
  420. return err;
  421. /* initialize the pipe record */
  422. pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
  423. if (! pipe) {
  424. /* release the pipe */
  425. vx_init_rmh(&rmh, CMD_FREE_PIPE);
  426. vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
  427. vx_send_msg(chip, &rmh);
  428. return -ENOMEM;
  429. }
  430. /* the pipe index should be identical with the audio index */
  431. pipe->number = audioid;
  432. pipe->is_capture = capture;
  433. pipe->channels = num_audio;
  434. pipe->differed_type = 0;
  435. pipe->pcx_time = 0;
  436. pipe->data_mode = data_mode;
  437. *pipep = pipe;
  438. return 0;
  439. }
  440. /*
  441. * vx_free_pipe - release a pipe
  442. * @pipe: pipe to be released
  443. */
  444. static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
  445. {
  446. struct vx_rmh rmh;
  447. vx_init_rmh(&rmh, CMD_FREE_PIPE);
  448. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  449. vx_send_msg(chip, &rmh);
  450. kfree(pipe);
  451. return 0;
  452. }
  453. /*
  454. * vx_start_stream - start the stream
  455. *
  456. * called from trigger callback only
  457. */
  458. static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
  459. {
  460. struct vx_rmh rmh;
  461. vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
  462. vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
  463. vx_set_differed_time(chip, &rmh, pipe);
  464. return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  465. }
  466. /*
  467. * vx_stop_stream - stop the stream
  468. *
  469. * called from trigger callback only
  470. */
  471. static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
  472. {
  473. struct vx_rmh rmh;
  474. vx_init_rmh(&rmh, CMD_STOP_STREAM);
  475. vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
  476. return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
  477. }
  478. /*
  479. * playback hw information
  480. */
  481. static struct snd_pcm_hardware vx_pcm_playback_hw = {
  482. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  483. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
  484. /*SNDRV_PCM_INFO_RESUME*/),
  485. .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
  486. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
  487. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  488. .rate_min = 5000,
  489. .rate_max = 48000,
  490. .channels_min = 1,
  491. .channels_max = 2,
  492. .buffer_bytes_max = (128*1024),
  493. .period_bytes_min = 126,
  494. .period_bytes_max = (128*1024),
  495. .periods_min = 2,
  496. .periods_max = VX_MAX_PERIODS,
  497. .fifo_size = 126,
  498. };
  499. static void vx_pcm_delayed_start(unsigned long arg);
  500. /*
  501. * vx_pcm_playback_open - open callback for playback
  502. */
  503. static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
  504. {
  505. struct snd_pcm_runtime *runtime = subs->runtime;
  506. struct vx_core *chip = snd_pcm_substream_chip(subs);
  507. struct vx_pipe *pipe = NULL;
  508. unsigned int audio;
  509. int err;
  510. if (chip->chip_status & VX_STAT_IS_STALE)
  511. return -EBUSY;
  512. audio = subs->pcm->device * 2;
  513. if (snd_BUG_ON(audio >= chip->audio_outs))
  514. return -EINVAL;
  515. /* playback pipe may have been already allocated for monitoring */
  516. pipe = chip->playback_pipes[audio];
  517. if (! pipe) {
  518. /* not allocated yet */
  519. err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
  520. if (err < 0)
  521. return err;
  522. chip->playback_pipes[audio] = pipe;
  523. }
  524. /* open for playback */
  525. pipe->references++;
  526. pipe->substream = subs;
  527. tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
  528. chip->playback_pipes[audio] = pipe;
  529. runtime->hw = vx_pcm_playback_hw;
  530. runtime->hw.period_bytes_min = chip->ibl.size;
  531. runtime->private_data = pipe;
  532. /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
  533. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
  534. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
  535. return 0;
  536. }
  537. /*
  538. * vx_pcm_playback_close - close callback for playback
  539. */
  540. static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
  541. {
  542. struct vx_core *chip = snd_pcm_substream_chip(subs);
  543. struct vx_pipe *pipe;
  544. if (! subs->runtime->private_data)
  545. return -EINVAL;
  546. pipe = subs->runtime->private_data;
  547. if (--pipe->references == 0) {
  548. chip->playback_pipes[pipe->number] = NULL;
  549. vx_free_pipe(chip, pipe);
  550. }
  551. return 0;
  552. }
  553. /*
  554. * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
  555. * @pipe: the pipe to notify
  556. *
  557. * NB: call with a certain lock.
  558. */
  559. static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
  560. {
  561. int err;
  562. struct vx_rmh rmh; /* use a temporary rmh here */
  563. /* Toggle Dsp Host Interface into Message mode */
  564. vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
  565. vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
  566. vx_set_stream_cmd_params(&rmh, 0, pipe->number);
  567. err = vx_send_msg_nolock(chip, &rmh);
  568. if (err < 0)
  569. return err;
  570. /* Toggle Dsp Host Interface back to sound transfer mode */
  571. vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
  572. return 0;
  573. }
  574. /*
  575. * vx_pcm_playback_transfer_chunk - transfer a single chunk
  576. * @subs: substream
  577. * @pipe: the pipe to transfer
  578. * @size: chunk size in bytes
  579. *
  580. * transfer a single buffer chunk. EOB notificaton is added after that.
  581. * called from the interrupt handler, too.
  582. *
  583. * return 0 if ok.
  584. */
  585. static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
  586. struct snd_pcm_runtime *runtime,
  587. struct vx_pipe *pipe, int size)
  588. {
  589. int space, err = 0;
  590. space = vx_query_hbuffer_size(chip, pipe);
  591. if (space < 0) {
  592. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  593. vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
  594. snd_printd("error hbuffer\n");
  595. return space;
  596. }
  597. if (space < size) {
  598. vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
  599. snd_printd("no enough hbuffer space %d\n", space);
  600. return -EIO; /* XRUN */
  601. }
  602. /* we don't need irqsave here, because this function
  603. * is called from either trigger callback or irq handler
  604. */
  605. spin_lock(&chip->lock);
  606. vx_pseudo_dma_write(chip, runtime, pipe, size);
  607. err = vx_notify_end_of_buffer(chip, pipe);
  608. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  609. vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
  610. spin_unlock(&chip->lock);
  611. return err;
  612. }
  613. /*
  614. * update the position of the given pipe.
  615. * pipe->position is updated and wrapped within the buffer size.
  616. * pipe->transferred is updated, too, but the size is not wrapped,
  617. * so that the caller can check the total transferred size later
  618. * (to call snd_pcm_period_elapsed).
  619. */
  620. static int vx_update_pipe_position(struct vx_core *chip,
  621. struct snd_pcm_runtime *runtime,
  622. struct vx_pipe *pipe)
  623. {
  624. struct vx_rmh rmh;
  625. int err, update;
  626. u64 count;
  627. vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
  628. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  629. err = vx_send_msg(chip, &rmh);
  630. if (err < 0)
  631. return err;
  632. count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
  633. update = (int)(count - pipe->cur_count);
  634. pipe->cur_count = count;
  635. pipe->position += update;
  636. if (pipe->position >= (int)runtime->buffer_size)
  637. pipe->position %= runtime->buffer_size;
  638. pipe->transferred += update;
  639. return 0;
  640. }
  641. /*
  642. * transfer the pending playback buffer data to DSP
  643. * called from interrupt handler
  644. */
  645. static void vx_pcm_playback_transfer(struct vx_core *chip,
  646. struct snd_pcm_substream *subs,
  647. struct vx_pipe *pipe, int nchunks)
  648. {
  649. int i, err;
  650. struct snd_pcm_runtime *runtime = subs->runtime;
  651. if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
  652. return;
  653. for (i = 0; i < nchunks; i++) {
  654. if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
  655. chip->ibl.size)) < 0)
  656. return;
  657. }
  658. }
  659. /*
  660. * update the playback position and call snd_pcm_period_elapsed() if necessary
  661. * called from interrupt handler
  662. */
  663. static void vx_pcm_playback_update(struct vx_core *chip,
  664. struct snd_pcm_substream *subs,
  665. struct vx_pipe *pipe)
  666. {
  667. int err;
  668. struct snd_pcm_runtime *runtime = subs->runtime;
  669. if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
  670. if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
  671. return;
  672. if (pipe->transferred >= (int)runtime->period_size) {
  673. pipe->transferred %= runtime->period_size;
  674. snd_pcm_period_elapsed(subs);
  675. }
  676. }
  677. }
  678. /*
  679. * start the stream and pipe.
  680. * this function is called from tasklet, which is invoked by the trigger
  681. * START callback.
  682. */
  683. static void vx_pcm_delayed_start(unsigned long arg)
  684. {
  685. struct snd_pcm_substream *subs = (struct snd_pcm_substream *)arg;
  686. struct vx_core *chip = subs->pcm->private_data;
  687. struct vx_pipe *pipe = subs->runtime->private_data;
  688. int err;
  689. /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
  690. if ((err = vx_start_stream(chip, pipe)) < 0) {
  691. snd_printk(KERN_ERR "vx: cannot start stream\n");
  692. return;
  693. }
  694. if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
  695. snd_printk(KERN_ERR "vx: cannot start pipe\n");
  696. return;
  697. }
  698. /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
  699. }
  700. /*
  701. * vx_pcm_playback_trigger - trigger callback for playback
  702. */
  703. static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
  704. {
  705. struct vx_core *chip = snd_pcm_substream_chip(subs);
  706. struct vx_pipe *pipe = subs->runtime->private_data;
  707. int err;
  708. if (chip->chip_status & VX_STAT_IS_STALE)
  709. return -EBUSY;
  710. switch (cmd) {
  711. case SNDRV_PCM_TRIGGER_START:
  712. case SNDRV_PCM_TRIGGER_RESUME:
  713. if (! pipe->is_capture)
  714. vx_pcm_playback_transfer(chip, subs, pipe, 2);
  715. /* FIXME:
  716. * we trigger the pipe using tasklet, so that the interrupts are
  717. * issued surely after the trigger is completed.
  718. */
  719. tasklet_hi_schedule(&pipe->start_tq);
  720. chip->pcm_running++;
  721. pipe->running = 1;
  722. break;
  723. case SNDRV_PCM_TRIGGER_STOP:
  724. case SNDRV_PCM_TRIGGER_SUSPEND:
  725. vx_toggle_pipe(chip, pipe, 0);
  726. vx_stop_pipe(chip, pipe);
  727. vx_stop_stream(chip, pipe);
  728. chip->pcm_running--;
  729. pipe->running = 0;
  730. break;
  731. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  732. if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
  733. return err;
  734. break;
  735. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  736. if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
  737. return err;
  738. break;
  739. default:
  740. return -EINVAL;
  741. }
  742. return 0;
  743. }
  744. /*
  745. * vx_pcm_playback_pointer - pointer callback for playback
  746. */
  747. static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
  748. {
  749. struct snd_pcm_runtime *runtime = subs->runtime;
  750. struct vx_pipe *pipe = runtime->private_data;
  751. return pipe->position;
  752. }
  753. /*
  754. * vx_pcm_hw_params - hw_params callback for playback and capture
  755. */
  756. static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
  757. struct snd_pcm_hw_params *hw_params)
  758. {
  759. return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
  760. }
  761. /*
  762. * vx_pcm_hw_free - hw_free callback for playback and capture
  763. */
  764. static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
  765. {
  766. return snd_pcm_free_vmalloc_buffer(subs);
  767. }
  768. /*
  769. * vx_pcm_prepare - prepare callback for playback and capture
  770. */
  771. static int vx_pcm_prepare(struct snd_pcm_substream *subs)
  772. {
  773. struct vx_core *chip = snd_pcm_substream_chip(subs);
  774. struct snd_pcm_runtime *runtime = subs->runtime;
  775. struct vx_pipe *pipe = runtime->private_data;
  776. int err, data_mode;
  777. // int max_size, nchunks;
  778. if (chip->chip_status & VX_STAT_IS_STALE)
  779. return -EBUSY;
  780. data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
  781. if (data_mode != pipe->data_mode && ! pipe->is_capture) {
  782. /* IEC958 status (raw-mode) was changed */
  783. /* we reopen the pipe */
  784. struct vx_rmh rmh;
  785. snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
  786. vx_init_rmh(&rmh, CMD_FREE_PIPE);
  787. vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
  788. if ((err = vx_send_msg(chip, &rmh)) < 0)
  789. return err;
  790. vx_init_rmh(&rmh, CMD_RES_PIPE);
  791. vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
  792. if (data_mode)
  793. rmh.Cmd[0] |= BIT_DATA_MODE;
  794. if ((err = vx_send_msg(chip, &rmh)) < 0)
  795. return err;
  796. pipe->data_mode = data_mode;
  797. }
  798. if (chip->pcm_running && chip->freq != runtime->rate) {
  799. snd_printk(KERN_ERR "vx: cannot set different clock %d "
  800. "from the current %d\n", runtime->rate, chip->freq);
  801. return -EINVAL;
  802. }
  803. vx_set_clock(chip, runtime->rate);
  804. if ((err = vx_set_format(chip, pipe, runtime)) < 0)
  805. return err;
  806. if (vx_is_pcmcia(chip)) {
  807. pipe->align = 2; /* 16bit word */
  808. } else {
  809. pipe->align = 4; /* 32bit word */
  810. }
  811. pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
  812. pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
  813. pipe->hw_ptr = 0;
  814. /* set the timestamp */
  815. vx_update_pipe_position(chip, runtime, pipe);
  816. /* clear again */
  817. pipe->transferred = 0;
  818. pipe->position = 0;
  819. pipe->prepared = 1;
  820. return 0;
  821. }
  822. /*
  823. * operators for PCM playback
  824. */
  825. static struct snd_pcm_ops vx_pcm_playback_ops = {
  826. .open = vx_pcm_playback_open,
  827. .close = vx_pcm_playback_close,
  828. .ioctl = snd_pcm_lib_ioctl,
  829. .hw_params = vx_pcm_hw_params,
  830. .hw_free = vx_pcm_hw_free,
  831. .prepare = vx_pcm_prepare,
  832. .trigger = vx_pcm_trigger,
  833. .pointer = vx_pcm_playback_pointer,
  834. .page = snd_pcm_get_vmalloc_page,
  835. };
  836. /*
  837. * playback hw information
  838. */
  839. static struct snd_pcm_hardware vx_pcm_capture_hw = {
  840. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  841. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
  842. /*SNDRV_PCM_INFO_RESUME*/),
  843. .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
  844. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
  845. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  846. .rate_min = 5000,
  847. .rate_max = 48000,
  848. .channels_min = 1,
  849. .channels_max = 2,
  850. .buffer_bytes_max = (128*1024),
  851. .period_bytes_min = 126,
  852. .period_bytes_max = (128*1024),
  853. .periods_min = 2,
  854. .periods_max = VX_MAX_PERIODS,
  855. .fifo_size = 126,
  856. };
  857. /*
  858. * vx_pcm_capture_open - open callback for capture
  859. */
  860. static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
  861. {
  862. struct snd_pcm_runtime *runtime = subs->runtime;
  863. struct vx_core *chip = snd_pcm_substream_chip(subs);
  864. struct vx_pipe *pipe;
  865. struct vx_pipe *pipe_out_monitoring = NULL;
  866. unsigned int audio;
  867. int err;
  868. if (chip->chip_status & VX_STAT_IS_STALE)
  869. return -EBUSY;
  870. audio = subs->pcm->device * 2;
  871. if (snd_BUG_ON(audio >= chip->audio_ins))
  872. return -EINVAL;
  873. err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
  874. if (err < 0)
  875. return err;
  876. pipe->substream = subs;
  877. tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
  878. chip->capture_pipes[audio] = pipe;
  879. /* check if monitoring is needed */
  880. if (chip->audio_monitor_active[audio]) {
  881. pipe_out_monitoring = chip->playback_pipes[audio];
  882. if (! pipe_out_monitoring) {
  883. /* allocate a pipe */
  884. err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
  885. if (err < 0)
  886. return err;
  887. chip->playback_pipes[audio] = pipe_out_monitoring;
  888. }
  889. pipe_out_monitoring->references++;
  890. /*
  891. if an output pipe is available, it's audios still may need to be
  892. unmuted. hence we'll have to call a mixer entry point.
  893. */
  894. vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
  895. chip->audio_monitor_active[audio]);
  896. /* assuming stereo */
  897. vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
  898. chip->audio_monitor_active[audio+1]);
  899. }
  900. pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
  901. runtime->hw = vx_pcm_capture_hw;
  902. runtime->hw.period_bytes_min = chip->ibl.size;
  903. runtime->private_data = pipe;
  904. /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
  905. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
  906. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
  907. return 0;
  908. }
  909. /*
  910. * vx_pcm_capture_close - close callback for capture
  911. */
  912. static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
  913. {
  914. struct vx_core *chip = snd_pcm_substream_chip(subs);
  915. struct vx_pipe *pipe;
  916. struct vx_pipe *pipe_out_monitoring;
  917. if (! subs->runtime->private_data)
  918. return -EINVAL;
  919. pipe = subs->runtime->private_data;
  920. chip->capture_pipes[pipe->number] = NULL;
  921. pipe_out_monitoring = pipe->monitoring_pipe;
  922. /*
  923. if an output pipe is attached to this input,
  924. check if it needs to be released.
  925. */
  926. if (pipe_out_monitoring) {
  927. if (--pipe_out_monitoring->references == 0) {
  928. vx_free_pipe(chip, pipe_out_monitoring);
  929. chip->playback_pipes[pipe->number] = NULL;
  930. pipe->monitoring_pipe = NULL;
  931. }
  932. }
  933. vx_free_pipe(chip, pipe);
  934. return 0;
  935. }
  936. #define DMA_READ_ALIGN 6 /* hardware alignment for read */
  937. /*
  938. * vx_pcm_capture_update - update the capture buffer
  939. */
  940. static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
  941. struct vx_pipe *pipe)
  942. {
  943. int size, space, count;
  944. struct snd_pcm_runtime *runtime = subs->runtime;
  945. if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
  946. return;
  947. size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
  948. if (! size)
  949. return;
  950. size = frames_to_bytes(runtime, size);
  951. space = vx_query_hbuffer_size(chip, pipe);
  952. if (space < 0)
  953. goto _error;
  954. if (size > space)
  955. size = space;
  956. size = (size / 3) * 3; /* align to 3 bytes */
  957. if (size < DMA_READ_ALIGN)
  958. goto _error;
  959. /* keep the last 6 bytes, they will be read after disconnection */
  960. count = size - DMA_READ_ALIGN;
  961. /* read bytes until the current pointer reaches to the aligned position
  962. * for word-transfer
  963. */
  964. while (count > 0) {
  965. if ((pipe->hw_ptr % pipe->align) == 0)
  966. break;
  967. if (vx_wait_for_rx_full(chip) < 0)
  968. goto _error;
  969. vx_pcm_read_per_bytes(chip, runtime, pipe);
  970. count -= 3;
  971. }
  972. if (count > 0) {
  973. /* ok, let's accelerate! */
  974. int align = pipe->align * 3;
  975. space = (count / align) * align;
  976. vx_pseudo_dma_read(chip, runtime, pipe, space);
  977. count -= space;
  978. }
  979. /* read the rest of bytes */
  980. while (count > 0) {
  981. if (vx_wait_for_rx_full(chip) < 0)
  982. goto _error;
  983. vx_pcm_read_per_bytes(chip, runtime, pipe);
  984. count -= 3;
  985. }
  986. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  987. vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
  988. /* read the last pending 6 bytes */
  989. count = DMA_READ_ALIGN;
  990. while (count > 0) {
  991. vx_pcm_read_per_bytes(chip, runtime, pipe);
  992. count -= 3;
  993. }
  994. /* update the position */
  995. pipe->transferred += size;
  996. if (pipe->transferred >= pipe->period_bytes) {
  997. pipe->transferred %= pipe->period_bytes;
  998. snd_pcm_period_elapsed(subs);
  999. }
  1000. return;
  1001. _error:
  1002. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  1003. vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
  1004. return;
  1005. }
  1006. /*
  1007. * vx_pcm_capture_pointer - pointer callback for capture
  1008. */
  1009. static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
  1010. {
  1011. struct snd_pcm_runtime *runtime = subs->runtime;
  1012. struct vx_pipe *pipe = runtime->private_data;
  1013. return bytes_to_frames(runtime, pipe->hw_ptr);
  1014. }
  1015. /*
  1016. * operators for PCM capture
  1017. */
  1018. static struct snd_pcm_ops vx_pcm_capture_ops = {
  1019. .open = vx_pcm_capture_open,
  1020. .close = vx_pcm_capture_close,
  1021. .ioctl = snd_pcm_lib_ioctl,
  1022. .hw_params = vx_pcm_hw_params,
  1023. .hw_free = vx_pcm_hw_free,
  1024. .prepare = vx_pcm_prepare,
  1025. .trigger = vx_pcm_trigger,
  1026. .pointer = vx_pcm_capture_pointer,
  1027. .page = snd_pcm_get_vmalloc_page,
  1028. };
  1029. /*
  1030. * interrupt handler for pcm streams
  1031. */
  1032. void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
  1033. {
  1034. unsigned int i;
  1035. struct vx_pipe *pipe;
  1036. #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
  1037. if (events & EVENT_MASK) {
  1038. vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
  1039. if (events & ASYNC_EVENTS_PENDING)
  1040. chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
  1041. if (events & END_OF_BUFFER_EVENTS_PENDING)
  1042. chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
  1043. if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
  1044. snd_printdd(KERN_ERR "msg send error!!\n");
  1045. return;
  1046. }
  1047. i = 1;
  1048. while (i < chip->irq_rmh.LgStat) {
  1049. int p, buf, capture, eob;
  1050. p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
  1051. capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
  1052. eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
  1053. i++;
  1054. if (events & ASYNC_EVENTS_PENDING)
  1055. i++;
  1056. buf = 1; /* force to transfer */
  1057. if (events & END_OF_BUFFER_EVENTS_PENDING) {
  1058. if (eob)
  1059. buf = chip->irq_rmh.Stat[i];
  1060. i++;
  1061. }
  1062. if (capture)
  1063. continue;
  1064. if (snd_BUG_ON(p < 0 || p >= chip->audio_outs))
  1065. continue;
  1066. pipe = chip->playback_pipes[p];
  1067. if (pipe && pipe->substream) {
  1068. vx_pcm_playback_update(chip, pipe->substream, pipe);
  1069. vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
  1070. }
  1071. }
  1072. }
  1073. /* update the capture pcm pointers as frequently as possible */
  1074. for (i = 0; i < chip->audio_ins; i++) {
  1075. pipe = chip->capture_pipes[i];
  1076. if (pipe && pipe->substream)
  1077. vx_pcm_capture_update(chip, pipe->substream, pipe);
  1078. }
  1079. }
  1080. /*
  1081. * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
  1082. */
  1083. static int vx_init_audio_io(struct vx_core *chip)
  1084. {
  1085. struct vx_rmh rmh;
  1086. int preferred;
  1087. vx_init_rmh(&rmh, CMD_SUPPORTED);
  1088. if (vx_send_msg(chip, &rmh) < 0) {
  1089. snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
  1090. return -ENXIO;
  1091. }
  1092. chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
  1093. chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
  1094. chip->audio_info = rmh.Stat[1];
  1095. /* allocate pipes */
  1096. chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
  1097. if (!chip->playback_pipes)
  1098. return -ENOMEM;
  1099. chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
  1100. if (!chip->capture_pipes) {
  1101. kfree(chip->playback_pipes);
  1102. return -ENOMEM;
  1103. }
  1104. preferred = chip->ibl.size;
  1105. chip->ibl.size = 0;
  1106. vx_set_ibl(chip, &chip->ibl); /* query the info */
  1107. if (preferred > 0) {
  1108. chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
  1109. chip->ibl.granularity) * chip->ibl.granularity;
  1110. if (chip->ibl.size > chip->ibl.max_size)
  1111. chip->ibl.size = chip->ibl.max_size;
  1112. } else
  1113. chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
  1114. vx_set_ibl(chip, &chip->ibl);
  1115. return 0;
  1116. }
  1117. /*
  1118. * free callback for pcm
  1119. */
  1120. static void snd_vx_pcm_free(struct snd_pcm *pcm)
  1121. {
  1122. struct vx_core *chip = pcm->private_data;
  1123. chip->pcm[pcm->device] = NULL;
  1124. kfree(chip->playback_pipes);
  1125. chip->playback_pipes = NULL;
  1126. kfree(chip->capture_pipes);
  1127. chip->capture_pipes = NULL;
  1128. }
  1129. /*
  1130. * snd_vx_pcm_new - create and initialize a pcm
  1131. */
  1132. int snd_vx_pcm_new(struct vx_core *chip)
  1133. {
  1134. struct snd_pcm *pcm;
  1135. unsigned int i;
  1136. int err;
  1137. if ((err = vx_init_audio_io(chip)) < 0)
  1138. return err;
  1139. for (i = 0; i < chip->hw->num_codecs; i++) {
  1140. unsigned int outs, ins;
  1141. outs = chip->audio_outs > i * 2 ? 1 : 0;
  1142. ins = chip->audio_ins > i * 2 ? 1 : 0;
  1143. if (! outs && ! ins)
  1144. break;
  1145. err = snd_pcm_new(chip->card, "VX PCM", i,
  1146. outs, ins, &pcm);
  1147. if (err < 0)
  1148. return err;
  1149. if (outs)
  1150. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
  1151. if (ins)
  1152. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
  1153. pcm->private_data = chip;
  1154. pcm->private_free = snd_vx_pcm_free;
  1155. pcm->info_flags = 0;
  1156. strcpy(pcm->name, chip->card->shortname);
  1157. chip->pcm[i] = pcm;
  1158. }
  1159. return 0;
  1160. }