vx_pcm.c 35 KB

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