vx_pcm.c 34 KB

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