vx_pcm.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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. snd_assert(audio < chip->audio_outs, return -EINVAL);
  514. /* playback pipe may have been already allocated for monitoring */
  515. pipe = chip->playback_pipes[audio];
  516. if (! pipe) {
  517. /* not allocated yet */
  518. err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
  519. if (err < 0)
  520. return err;
  521. chip->playback_pipes[audio] = pipe;
  522. }
  523. /* open for playback */
  524. pipe->references++;
  525. pipe->substream = subs;
  526. tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
  527. chip->playback_pipes[audio] = pipe;
  528. runtime->hw = vx_pcm_playback_hw;
  529. runtime->hw.period_bytes_min = chip->ibl.size;
  530. runtime->private_data = pipe;
  531. /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
  532. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
  533. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
  534. return 0;
  535. }
  536. /*
  537. * vx_pcm_playback_close - close callback for playback
  538. */
  539. static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
  540. {
  541. struct vx_core *chip = snd_pcm_substream_chip(subs);
  542. struct vx_pipe *pipe;
  543. if (! subs->runtime->private_data)
  544. return -EINVAL;
  545. pipe = subs->runtime->private_data;
  546. if (--pipe->references == 0) {
  547. chip->playback_pipes[pipe->number] = NULL;
  548. vx_free_pipe(chip, pipe);
  549. }
  550. return 0;
  551. }
  552. /*
  553. * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
  554. * @pipe: the pipe to notify
  555. *
  556. * NB: call with a certain lock.
  557. */
  558. static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
  559. {
  560. int err;
  561. struct vx_rmh rmh; /* use a temporary rmh here */
  562. /* Toggle Dsp Host Interface into Message mode */
  563. vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
  564. vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
  565. vx_set_stream_cmd_params(&rmh, 0, pipe->number);
  566. err = vx_send_msg_nolock(chip, &rmh);
  567. if (err < 0)
  568. return err;
  569. /* Toggle Dsp Host Interface back to sound transfer mode */
  570. vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
  571. return 0;
  572. }
  573. /*
  574. * vx_pcm_playback_transfer_chunk - transfer a single chunk
  575. * @subs: substream
  576. * @pipe: the pipe to transfer
  577. * @size: chunk size in bytes
  578. *
  579. * transfer a single buffer chunk. EOB notificaton is added after that.
  580. * called from the interrupt handler, too.
  581. *
  582. * return 0 if ok.
  583. */
  584. static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
  585. struct snd_pcm_runtime *runtime,
  586. struct vx_pipe *pipe, int size)
  587. {
  588. int space, err = 0;
  589. space = vx_query_hbuffer_size(chip, pipe);
  590. if (space < 0) {
  591. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  592. vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
  593. snd_printd("error hbuffer\n");
  594. return space;
  595. }
  596. if (space < size) {
  597. vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
  598. snd_printd("no enough hbuffer space %d\n", space);
  599. return -EIO; /* XRUN */
  600. }
  601. /* we don't need irqsave here, because this function
  602. * is called from either trigger callback or irq handler
  603. */
  604. spin_lock(&chip->lock);
  605. vx_pseudo_dma_write(chip, runtime, pipe, size);
  606. err = vx_notify_end_of_buffer(chip, pipe);
  607. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  608. vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
  609. spin_unlock(&chip->lock);
  610. return err;
  611. }
  612. /*
  613. * update the position of the given pipe.
  614. * pipe->position is updated and wrapped within the buffer size.
  615. * pipe->transferred is updated, too, but the size is not wrapped,
  616. * so that the caller can check the total transferred size later
  617. * (to call snd_pcm_period_elapsed).
  618. */
  619. static int vx_update_pipe_position(struct vx_core *chip,
  620. struct snd_pcm_runtime *runtime,
  621. struct vx_pipe *pipe)
  622. {
  623. struct vx_rmh rmh;
  624. int err, update;
  625. u64 count;
  626. vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
  627. vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
  628. err = vx_send_msg(chip, &rmh);
  629. if (err < 0)
  630. return err;
  631. count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
  632. update = (int)(count - pipe->cur_count);
  633. pipe->cur_count = count;
  634. pipe->position += update;
  635. if (pipe->position >= (int)runtime->buffer_size)
  636. pipe->position %= runtime->buffer_size;
  637. pipe->transferred += update;
  638. return 0;
  639. }
  640. /*
  641. * transfer the pending playback buffer data to DSP
  642. * called from interrupt handler
  643. */
  644. static void vx_pcm_playback_transfer(struct vx_core *chip,
  645. struct snd_pcm_substream *subs,
  646. struct vx_pipe *pipe, int nchunks)
  647. {
  648. int i, err;
  649. struct snd_pcm_runtime *runtime = subs->runtime;
  650. if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
  651. return;
  652. for (i = 0; i < nchunks; i++) {
  653. if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
  654. chip->ibl.size)) < 0)
  655. return;
  656. }
  657. }
  658. /*
  659. * update the playback position and call snd_pcm_period_elapsed() if necessary
  660. * called from interrupt handler
  661. */
  662. static void vx_pcm_playback_update(struct vx_core *chip,
  663. struct snd_pcm_substream *subs,
  664. struct vx_pipe *pipe)
  665. {
  666. int err;
  667. struct snd_pcm_runtime *runtime = subs->runtime;
  668. if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
  669. if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
  670. return;
  671. if (pipe->transferred >= (int)runtime->period_size) {
  672. pipe->transferred %= runtime->period_size;
  673. snd_pcm_period_elapsed(subs);
  674. }
  675. }
  676. }
  677. /*
  678. * start the stream and pipe.
  679. * this function is called from tasklet, which is invoked by the trigger
  680. * START callback.
  681. */
  682. static void vx_pcm_delayed_start(unsigned long arg)
  683. {
  684. struct snd_pcm_substream *subs = (struct snd_pcm_substream *)arg;
  685. struct vx_core *chip = subs->pcm->private_data;
  686. struct vx_pipe *pipe = subs->runtime->private_data;
  687. int err;
  688. /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
  689. if ((err = vx_start_stream(chip, pipe)) < 0) {
  690. snd_printk(KERN_ERR "vx: cannot start stream\n");
  691. return;
  692. }
  693. if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
  694. snd_printk(KERN_ERR "vx: cannot start pipe\n");
  695. return;
  696. }
  697. /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
  698. }
  699. /*
  700. * vx_pcm_playback_trigger - trigger callback for playback
  701. */
  702. static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
  703. {
  704. struct vx_core *chip = snd_pcm_substream_chip(subs);
  705. struct vx_pipe *pipe = subs->runtime->private_data;
  706. int err;
  707. if (chip->chip_status & VX_STAT_IS_STALE)
  708. return -EBUSY;
  709. switch (cmd) {
  710. case SNDRV_PCM_TRIGGER_START:
  711. case SNDRV_PCM_TRIGGER_RESUME:
  712. if (! pipe->is_capture)
  713. vx_pcm_playback_transfer(chip, subs, pipe, 2);
  714. /* FIXME:
  715. * we trigger the pipe using tasklet, so that the interrupts are
  716. * issued surely after the trigger is completed.
  717. */
  718. tasklet_hi_schedule(&pipe->start_tq);
  719. chip->pcm_running++;
  720. pipe->running = 1;
  721. break;
  722. case SNDRV_PCM_TRIGGER_STOP:
  723. case SNDRV_PCM_TRIGGER_SUSPEND:
  724. vx_toggle_pipe(chip, pipe, 0);
  725. vx_stop_pipe(chip, pipe);
  726. vx_stop_stream(chip, pipe);
  727. chip->pcm_running--;
  728. pipe->running = 0;
  729. break;
  730. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  731. if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
  732. return err;
  733. break;
  734. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  735. if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
  736. return err;
  737. break;
  738. default:
  739. return -EINVAL;
  740. }
  741. return 0;
  742. }
  743. /*
  744. * vx_pcm_playback_pointer - pointer callback for playback
  745. */
  746. static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
  747. {
  748. struct snd_pcm_runtime *runtime = subs->runtime;
  749. struct vx_pipe *pipe = runtime->private_data;
  750. return pipe->position;
  751. }
  752. /*
  753. * vx_pcm_hw_params - hw_params callback for playback and capture
  754. */
  755. static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
  756. struct snd_pcm_hw_params *hw_params)
  757. {
  758. return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
  759. }
  760. /*
  761. * vx_pcm_hw_free - hw_free callback for playback and capture
  762. */
  763. static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
  764. {
  765. return snd_pcm_free_vmalloc_buffer(subs);
  766. }
  767. /*
  768. * vx_pcm_prepare - prepare callback for playback and capture
  769. */
  770. static int vx_pcm_prepare(struct snd_pcm_substream *subs)
  771. {
  772. struct vx_core *chip = snd_pcm_substream_chip(subs);
  773. struct snd_pcm_runtime *runtime = subs->runtime;
  774. struct vx_pipe *pipe = runtime->private_data;
  775. int err, data_mode;
  776. // int max_size, nchunks;
  777. if (chip->chip_status & VX_STAT_IS_STALE)
  778. return -EBUSY;
  779. data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
  780. if (data_mode != pipe->data_mode && ! pipe->is_capture) {
  781. /* IEC958 status (raw-mode) was changed */
  782. /* we reopen the pipe */
  783. struct vx_rmh rmh;
  784. snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
  785. vx_init_rmh(&rmh, CMD_FREE_PIPE);
  786. vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
  787. if ((err = vx_send_msg(chip, &rmh)) < 0)
  788. return err;
  789. vx_init_rmh(&rmh, CMD_RES_PIPE);
  790. vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
  791. if (data_mode)
  792. rmh.Cmd[0] |= BIT_DATA_MODE;
  793. if ((err = vx_send_msg(chip, &rmh)) < 0)
  794. return err;
  795. pipe->data_mode = data_mode;
  796. }
  797. if (chip->pcm_running && chip->freq != runtime->rate) {
  798. snd_printk(KERN_ERR "vx: cannot set different clock %d "
  799. "from the current %d\n", runtime->rate, chip->freq);
  800. return -EINVAL;
  801. }
  802. vx_set_clock(chip, runtime->rate);
  803. if ((err = vx_set_format(chip, pipe, runtime)) < 0)
  804. return err;
  805. if (vx_is_pcmcia(chip)) {
  806. pipe->align = 2; /* 16bit word */
  807. } else {
  808. pipe->align = 4; /* 32bit word */
  809. }
  810. pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
  811. pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
  812. pipe->hw_ptr = 0;
  813. /* set the timestamp */
  814. vx_update_pipe_position(chip, runtime, pipe);
  815. /* clear again */
  816. pipe->transferred = 0;
  817. pipe->position = 0;
  818. pipe->prepared = 1;
  819. return 0;
  820. }
  821. /*
  822. * operators for PCM playback
  823. */
  824. static struct snd_pcm_ops vx_pcm_playback_ops = {
  825. .open = vx_pcm_playback_open,
  826. .close = vx_pcm_playback_close,
  827. .ioctl = snd_pcm_lib_ioctl,
  828. .hw_params = vx_pcm_hw_params,
  829. .hw_free = vx_pcm_hw_free,
  830. .prepare = vx_pcm_prepare,
  831. .trigger = vx_pcm_trigger,
  832. .pointer = vx_pcm_playback_pointer,
  833. .page = snd_pcm_get_vmalloc_page,
  834. };
  835. /*
  836. * playback hw information
  837. */
  838. static struct snd_pcm_hardware vx_pcm_capture_hw = {
  839. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  840. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
  841. /*SNDRV_PCM_INFO_RESUME*/),
  842. .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
  843. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
  844. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  845. .rate_min = 5000,
  846. .rate_max = 48000,
  847. .channels_min = 1,
  848. .channels_max = 2,
  849. .buffer_bytes_max = (128*1024),
  850. .period_bytes_min = 126,
  851. .period_bytes_max = (128*1024),
  852. .periods_min = 2,
  853. .periods_max = VX_MAX_PERIODS,
  854. .fifo_size = 126,
  855. };
  856. /*
  857. * vx_pcm_capture_open - open callback for capture
  858. */
  859. static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
  860. {
  861. struct snd_pcm_runtime *runtime = subs->runtime;
  862. struct vx_core *chip = snd_pcm_substream_chip(subs);
  863. struct vx_pipe *pipe;
  864. struct vx_pipe *pipe_out_monitoring = NULL;
  865. unsigned int audio;
  866. int err;
  867. if (chip->chip_status & VX_STAT_IS_STALE)
  868. return -EBUSY;
  869. audio = subs->pcm->device * 2;
  870. snd_assert(audio < chip->audio_ins, return -EINVAL);
  871. err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
  872. if (err < 0)
  873. return err;
  874. pipe->substream = subs;
  875. tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
  876. chip->capture_pipes[audio] = pipe;
  877. /* check if monitoring is needed */
  878. if (chip->audio_monitor_active[audio]) {
  879. pipe_out_monitoring = chip->playback_pipes[audio];
  880. if (! pipe_out_monitoring) {
  881. /* allocate a pipe */
  882. err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
  883. if (err < 0)
  884. return err;
  885. chip->playback_pipes[audio] = pipe_out_monitoring;
  886. }
  887. pipe_out_monitoring->references++;
  888. /*
  889. if an output pipe is available, it's audios still may need to be
  890. unmuted. hence we'll have to call a mixer entry point.
  891. */
  892. vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
  893. chip->audio_monitor_active[audio]);
  894. /* assuming stereo */
  895. vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
  896. chip->audio_monitor_active[audio+1]);
  897. }
  898. pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
  899. runtime->hw = vx_pcm_capture_hw;
  900. runtime->hw.period_bytes_min = chip->ibl.size;
  901. runtime->private_data = pipe;
  902. /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
  903. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
  904. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
  905. return 0;
  906. }
  907. /*
  908. * vx_pcm_capture_close - close callback for capture
  909. */
  910. static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
  911. {
  912. struct vx_core *chip = snd_pcm_substream_chip(subs);
  913. struct vx_pipe *pipe;
  914. struct vx_pipe *pipe_out_monitoring;
  915. if (! subs->runtime->private_data)
  916. return -EINVAL;
  917. pipe = subs->runtime->private_data;
  918. chip->capture_pipes[pipe->number] = NULL;
  919. pipe_out_monitoring = pipe->monitoring_pipe;
  920. /*
  921. if an output pipe is attached to this input,
  922. check if it needs to be released.
  923. */
  924. if (pipe_out_monitoring) {
  925. if (--pipe_out_monitoring->references == 0) {
  926. vx_free_pipe(chip, pipe_out_monitoring);
  927. chip->playback_pipes[pipe->number] = NULL;
  928. pipe->monitoring_pipe = NULL;
  929. }
  930. }
  931. vx_free_pipe(chip, pipe);
  932. return 0;
  933. }
  934. #define DMA_READ_ALIGN 6 /* hardware alignment for read */
  935. /*
  936. * vx_pcm_capture_update - update the capture buffer
  937. */
  938. static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
  939. struct vx_pipe *pipe)
  940. {
  941. int size, space, count;
  942. struct snd_pcm_runtime *runtime = subs->runtime;
  943. if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
  944. return;
  945. size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
  946. if (! size)
  947. return;
  948. size = frames_to_bytes(runtime, size);
  949. space = vx_query_hbuffer_size(chip, pipe);
  950. if (space < 0)
  951. goto _error;
  952. if (size > space)
  953. size = space;
  954. size = (size / 3) * 3; /* align to 3 bytes */
  955. if (size < DMA_READ_ALIGN)
  956. goto _error;
  957. /* keep the last 6 bytes, they will be read after disconnection */
  958. count = size - DMA_READ_ALIGN;
  959. /* read bytes until the current pointer reaches to the aligned position
  960. * for word-transfer
  961. */
  962. while (count > 0) {
  963. if ((pipe->hw_ptr % pipe->align) == 0)
  964. break;
  965. if (vx_wait_for_rx_full(chip) < 0)
  966. goto _error;
  967. vx_pcm_read_per_bytes(chip, runtime, pipe);
  968. count -= 3;
  969. }
  970. if (count > 0) {
  971. /* ok, let's accelerate! */
  972. int align = pipe->align * 3;
  973. space = (count / align) * align;
  974. vx_pseudo_dma_read(chip, runtime, pipe, space);
  975. count -= space;
  976. }
  977. /* read the rest of bytes */
  978. while (count > 0) {
  979. if (vx_wait_for_rx_full(chip) < 0)
  980. goto _error;
  981. vx_pcm_read_per_bytes(chip, runtime, pipe);
  982. count -= 3;
  983. }
  984. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  985. vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
  986. /* read the last pending 6 bytes */
  987. count = DMA_READ_ALIGN;
  988. while (count > 0) {
  989. vx_pcm_read_per_bytes(chip, runtime, pipe);
  990. count -= 3;
  991. }
  992. /* update the position */
  993. pipe->transferred += size;
  994. if (pipe->transferred >= pipe->period_bytes) {
  995. pipe->transferred %= pipe->period_bytes;
  996. snd_pcm_period_elapsed(subs);
  997. }
  998. return;
  999. _error:
  1000. /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
  1001. vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
  1002. return;
  1003. }
  1004. /*
  1005. * vx_pcm_capture_pointer - pointer callback for capture
  1006. */
  1007. static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
  1008. {
  1009. struct snd_pcm_runtime *runtime = subs->runtime;
  1010. struct vx_pipe *pipe = runtime->private_data;
  1011. return bytes_to_frames(runtime, pipe->hw_ptr);
  1012. }
  1013. /*
  1014. * operators for PCM capture
  1015. */
  1016. static struct snd_pcm_ops vx_pcm_capture_ops = {
  1017. .open = vx_pcm_capture_open,
  1018. .close = vx_pcm_capture_close,
  1019. .ioctl = snd_pcm_lib_ioctl,
  1020. .hw_params = vx_pcm_hw_params,
  1021. .hw_free = vx_pcm_hw_free,
  1022. .prepare = vx_pcm_prepare,
  1023. .trigger = vx_pcm_trigger,
  1024. .pointer = vx_pcm_capture_pointer,
  1025. .page = snd_pcm_get_vmalloc_page,
  1026. };
  1027. /*
  1028. * interrupt handler for pcm streams
  1029. */
  1030. void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
  1031. {
  1032. unsigned int i;
  1033. struct vx_pipe *pipe;
  1034. #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
  1035. if (events & EVENT_MASK) {
  1036. vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
  1037. if (events & ASYNC_EVENTS_PENDING)
  1038. chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
  1039. if (events & END_OF_BUFFER_EVENTS_PENDING)
  1040. chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
  1041. if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
  1042. snd_printdd(KERN_ERR "msg send error!!\n");
  1043. return;
  1044. }
  1045. i = 1;
  1046. while (i < chip->irq_rmh.LgStat) {
  1047. int p, buf, capture, eob;
  1048. p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
  1049. capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
  1050. eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
  1051. i++;
  1052. if (events & ASYNC_EVENTS_PENDING)
  1053. i++;
  1054. buf = 1; /* force to transfer */
  1055. if (events & END_OF_BUFFER_EVENTS_PENDING) {
  1056. if (eob)
  1057. buf = chip->irq_rmh.Stat[i];
  1058. i++;
  1059. }
  1060. if (capture)
  1061. continue;
  1062. snd_assert(p >= 0 && (unsigned int)p < chip->audio_outs,);
  1063. pipe = chip->playback_pipes[p];
  1064. if (pipe && pipe->substream) {
  1065. vx_pcm_playback_update(chip, pipe->substream, pipe);
  1066. vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
  1067. }
  1068. }
  1069. }
  1070. /* update the capture pcm pointers as frequently as possible */
  1071. for (i = 0; i < chip->audio_ins; i++) {
  1072. pipe = chip->capture_pipes[i];
  1073. if (pipe && pipe->substream)
  1074. vx_pcm_capture_update(chip, pipe->substream, pipe);
  1075. }
  1076. }
  1077. /*
  1078. * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
  1079. */
  1080. static int vx_init_audio_io(struct vx_core *chip)
  1081. {
  1082. struct vx_rmh rmh;
  1083. int preferred;
  1084. vx_init_rmh(&rmh, CMD_SUPPORTED);
  1085. if (vx_send_msg(chip, &rmh) < 0) {
  1086. snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
  1087. return -ENXIO;
  1088. }
  1089. chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
  1090. chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
  1091. chip->audio_info = rmh.Stat[1];
  1092. /* allocate pipes */
  1093. chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
  1094. if (!chip->playback_pipes)
  1095. return -ENOMEM;
  1096. chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
  1097. if (!chip->capture_pipes) {
  1098. kfree(chip->playback_pipes);
  1099. return -ENOMEM;
  1100. }
  1101. preferred = chip->ibl.size;
  1102. chip->ibl.size = 0;
  1103. vx_set_ibl(chip, &chip->ibl); /* query the info */
  1104. if (preferred > 0) {
  1105. chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
  1106. chip->ibl.granularity) * chip->ibl.granularity;
  1107. if (chip->ibl.size > chip->ibl.max_size)
  1108. chip->ibl.size = chip->ibl.max_size;
  1109. } else
  1110. chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
  1111. vx_set_ibl(chip, &chip->ibl);
  1112. return 0;
  1113. }
  1114. /*
  1115. * free callback for pcm
  1116. */
  1117. static void snd_vx_pcm_free(struct snd_pcm *pcm)
  1118. {
  1119. struct vx_core *chip = pcm->private_data;
  1120. chip->pcm[pcm->device] = NULL;
  1121. kfree(chip->playback_pipes);
  1122. chip->playback_pipes = NULL;
  1123. kfree(chip->capture_pipes);
  1124. chip->capture_pipes = NULL;
  1125. }
  1126. /*
  1127. * snd_vx_pcm_new - create and initialize a pcm
  1128. */
  1129. int snd_vx_pcm_new(struct vx_core *chip)
  1130. {
  1131. struct snd_pcm *pcm;
  1132. unsigned int i;
  1133. int err;
  1134. if ((err = vx_init_audio_io(chip)) < 0)
  1135. return err;
  1136. for (i = 0; i < chip->hw->num_codecs; i++) {
  1137. unsigned int outs, ins;
  1138. outs = chip->audio_outs > i * 2 ? 1 : 0;
  1139. ins = chip->audio_ins > i * 2 ? 1 : 0;
  1140. if (! outs && ! ins)
  1141. break;
  1142. err = snd_pcm_new(chip->card, "VX PCM", i,
  1143. outs, ins, &pcm);
  1144. if (err < 0)
  1145. return err;
  1146. if (outs)
  1147. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
  1148. if (ins)
  1149. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
  1150. pcm->private_data = chip;
  1151. pcm->private_free = snd_vx_pcm_free;
  1152. pcm->info_flags = 0;
  1153. strcpy(pcm->name, chip->card->shortname);
  1154. chip->pcm[i] = pcm;
  1155. }
  1156. return 0;
  1157. }