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