vx_pcm.c 35 KB

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