vx_pcm.c 33 KB

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