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