2 **********************************************************************
3 * cardwo.c - PCM output HAL for emu10k1 driver
4 * Copyright 1999, 2000 Creative Labs, Inc.
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * October 20, 1999 Bertrand Lee base code release
12 **********************************************************************
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the Free
26 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
29 **********************************************************************
32 #include <linux/poll.h>
39 static u32 samplerate_to_linearpitch(u32 samplingrate)
41 samplingrate = (samplingrate << 8) / 375;
42 return (samplingrate >> 1) + (samplingrate & 1);
45 static void query_format(struct emu10k1_wavedevice *wave_dev, struct wave_format *wave_fmt)
47 int i, j, do_passthrough = 0, is_ac3 = 0;
48 struct emu10k1_card *card = wave_dev->card;
49 struct woinst *woinst = wave_dev->woinst;
51 if ((wave_fmt->channels > 2) && (wave_fmt->id != AFMT_S16_LE) && (wave_fmt->id != AFMT_U8))
52 wave_fmt->channels = 2;
54 if ((wave_fmt->channels < 1) || (wave_fmt->channels > WAVEOUT_MAXVOICES))
55 wave_fmt->channels = 2;
57 if (wave_fmt->channels == 2)
58 woinst->num_voices = 1;
60 woinst->num_voices = wave_fmt->channels;
62 if (wave_fmt->samplingrate >= 0x2ee00)
63 wave_fmt->samplingrate = 0x2ee00;
65 wave_fmt->passthrough = 0;
66 do_passthrough = is_ac3 = 0;
68 if (card->pt.selected)
71 switch (wave_fmt->id) {
73 wave_fmt->bitsperchannel = 16;
76 wave_fmt->bitsperchannel = 8;
83 wave_fmt->id = AFMT_S16_LE;
84 wave_fmt->bitsperchannel = 16;
88 /* currently only one waveout instance may use pass-through */
89 if (woinst->state != WAVE_STATE_CLOSED ||
90 card->pt.state != PT_STATE_INACTIVE ||
91 (wave_fmt->samplingrate != 48000 && !is_ac3)) {
92 DPF(2, "unable to set pass-through mode\n");
93 } else if (USE_PT_METHOD1) {
94 i = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.intr_gpr_name);
95 j = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.enable_gpr_name);
97 DPF(2, "unable to set pass-through mode\n");
99 wave_fmt->samplingrate = 48000;
100 wave_fmt->channels = 2;
101 card->pt.pos_gpr = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name,
102 card->pt.pos_gpr_name);
103 wave_fmt->passthrough = 1;
104 card->pt.intr_gpr = i;
105 card->pt.enable_gpr = j;
106 card->pt.state = PT_STATE_INACTIVE;
108 DPD(2, "is_ac3 is %d\n", is_ac3);
109 card->pt.ac3data = is_ac3;
110 wave_fmt->bitsperchannel = 16;
113 DPF(2, "Using Passthrough Method 2\n");
114 card->pt.enable_gpr = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name,
115 card->pt.enable_gpr_name);
116 wave_fmt->passthrough = 2;
117 wave_fmt->bitsperchannel = 16;
121 wave_fmt->bytesperchannel = wave_fmt->bitsperchannel >> 3;
122 wave_fmt->bytespersample = wave_fmt->channels * wave_fmt->bytesperchannel;
123 wave_fmt->bytespersec = wave_fmt->bytespersample * wave_fmt->samplingrate;
125 if (wave_fmt->channels == 2)
126 wave_fmt->bytespervoicesample = wave_fmt->channels * wave_fmt->bytesperchannel;
128 wave_fmt->bytespervoicesample = wave_fmt->bytesperchannel;
131 static int get_voice(struct emu10k1_card *card, struct woinst *woinst, unsigned int voicenum)
133 struct emu_voice *voice = &woinst->voice[voicenum];
135 /* Allocate voices here, if no voices available, return error. */
137 voice->usage = VOICE_USAGE_PLAYBACK;
141 if (woinst->format.channels == 2)
142 voice->flags |= VOICE_FLAGS_STEREO;
144 if (woinst->format.bitsperchannel == 16)
145 voice->flags |= VOICE_FLAGS_16BIT;
147 if (emu10k1_voice_alloc(card, voice) < 0) {
148 voice->usage = VOICE_USAGE_FREE;
152 /* Calculate pitch */
153 voice->initial_pitch = (u16) (srToPitch(woinst->format.samplingrate) >> 8);
154 voice->pitch_target = samplerate_to_linearpitch(woinst->format.samplingrate);
156 DPD(2, "Initial pitch --> %#x\n", voice->initial_pitch);
158 voice->startloop = (voice->mem.emupageindex << 12) /
159 woinst->format.bytespervoicesample;
160 voice->endloop = voice->startloop + woinst->buffer.size / woinst->format.bytespervoicesample;
161 voice->start = voice->startloop;
164 voice->params[0].volume_target = 0xffff;
165 voice->params[0].initial_fc = 0xff;
166 voice->params[0].initial_attn = 0x00;
167 voice->params[0].byampl_env_sustain = 0x7f;
168 voice->params[0].byampl_env_decay = 0x7f;
171 if (voice->flags & VOICE_FLAGS_STEREO) {
172 if (woinst->format.passthrough == 2) {
173 voice->params[0].send_routing = voice->params[1].send_routing = card->waveout.send_routing[ROUTE_PT];
174 voice->params[0].send_routing2 = voice->params[1].send_routing2 = card->waveout.send_routing2[ROUTE_PT];
175 voice->params[0].send_dcba = 0xff;
176 voice->params[1].send_dcba = 0xff00;
177 voice->params[0].send_hgfe = voice->params[1].send_hgfe=0;
179 voice->params[0].send_dcba = card->waveout.send_dcba[SEND_LEFT];
180 voice->params[0].send_hgfe = card->waveout.send_hgfe[SEND_LEFT];
181 voice->params[1].send_dcba = card->waveout.send_dcba[SEND_RIGHT];
182 voice->params[1].send_hgfe = card->waveout.send_hgfe[SEND_RIGHT];
184 if (woinst->device) {
186 voice->params[0].send_routing = voice->params[1].send_routing = card->waveout.send_routing[ROUTE_PCM1];
187 voice->params[0].send_routing2 = voice->params[1].send_routing2 = card->waveout.send_routing2[ROUTE_PCM1];
189 voice->params[0].send_routing = voice->params[1].send_routing = card->waveout.send_routing[ROUTE_PCM];
190 voice->params[0].send_routing2 = voice->params[1].send_routing2 = card->waveout.send_routing2[ROUTE_PCM];
194 voice->params[1].volume_target = 0xffff;
195 voice->params[1].initial_fc = 0xff;
196 voice->params[1].initial_attn = 0x00;
197 voice->params[1].byampl_env_sustain = 0x7f;
198 voice->params[1].byampl_env_decay = 0x7f;
200 if (woinst->num_voices > 1) {
202 voice->params[0].send_dcba=0xff;
203 voice->params[0].send_hgfe=0;
204 if (card->is_audigy) {
205 voice->params[0].send_routing = 0x3f3f3f00 + card->mchannel_fx + voicenum;
206 voice->params[0].send_routing2 = 0x3f3f3f3f;
208 voice->params[0].send_routing = 0xfff0 + card->mchannel_fx + voicenum;
212 voice->params[0].send_dcba = card->waveout.send_dcba[SEND_MONO];
213 voice->params[0].send_hgfe = card->waveout.send_hgfe[SEND_MONO];
215 if (woinst->device) {
216 voice->params[0].send_routing = card->waveout.send_routing[ROUTE_PCM1];
217 voice->params[0].send_routing2 = card->waveout.send_routing2[ROUTE_PCM1];
219 voice->params[0].send_routing = card->waveout.send_routing[ROUTE_PCM];
220 voice->params[0].send_routing2 = card->waveout.send_routing2[ROUTE_PCM];
225 DPD(2, "voice: startloop=%#x, endloop=%#x\n", voice->startloop, voice->endloop);
227 emu10k1_voice_playback_setup(voice);
232 int emu10k1_waveout_open(struct emu10k1_wavedevice *wave_dev)
234 struct emu10k1_card *card = wave_dev->card;
235 struct woinst *woinst = wave_dev->woinst;
236 struct waveout_buffer *buffer = &woinst->buffer;
237 unsigned int voicenum;
240 DPF(2, "emu10k1_waveout_open()\n");
242 for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
243 if (emu10k1_voice_alloc_buffer(card, &woinst->voice[voicenum].mem, woinst->buffer.pages) < 0) {
245 emu10k1_waveout_close(wave_dev);
249 if (get_voice(card, woinst, voicenum) < 0) {
251 emu10k1_waveout_close(wave_dev);
256 buffer->fill_silence = 0;
257 buffer->silence_bytes = 0;
258 buffer->silence_pos = 0;
260 buffer->free_bytes = woinst->buffer.size;
262 delay = (48000 * woinst->buffer.fragment_size) /
263 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
265 emu10k1_timer_install(card, &woinst->timer, delay);
267 woinst->state = WAVE_STATE_OPEN;
272 void emu10k1_waveout_close(struct emu10k1_wavedevice *wave_dev)
274 struct emu10k1_card *card = wave_dev->card;
275 struct woinst *woinst = wave_dev->woinst;
276 unsigned int voicenum;
278 DPF(2, "emu10k1_waveout_close()\n");
280 emu10k1_waveout_stop(wave_dev);
282 emu10k1_timer_uninstall(card, &woinst->timer);
284 for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
285 emu10k1_voice_free(&woinst->voice[voicenum]);
286 emu10k1_voice_free_buffer(card, &woinst->voice[voicenum].mem);
289 woinst->state = WAVE_STATE_CLOSED;
292 void emu10k1_waveout_start(struct emu10k1_wavedevice *wave_dev)
294 struct emu10k1_card *card = wave_dev->card;
295 struct woinst *woinst = wave_dev->woinst;
296 struct pt_data *pt = &card->pt;
298 DPF(2, "emu10k1_waveout_start()\n");
300 if (woinst->format.passthrough == 2) {
301 emu10k1_pt_setup(wave_dev);
302 sblive_writeptr(card, (card->is_audigy ? A_GPR_BASE : GPR_BASE) + pt->enable_gpr, 0, 1);
303 pt->state = PT_STATE_PLAYING;
307 emu10k1_voices_start(woinst->voice, woinst->num_voices, woinst->total_played);
309 emu10k1_timer_enable(card, &woinst->timer);
311 woinst->state |= WAVE_STATE_STARTED;
314 int emu10k1_waveout_setformat(struct emu10k1_wavedevice *wave_dev, struct wave_format *format)
316 struct emu10k1_card *card = wave_dev->card;
317 struct woinst *woinst = wave_dev->woinst;
318 unsigned int voicenum;
321 DPF(2, "emu10k1_waveout_setformat()\n");
323 if (woinst->state & WAVE_STATE_STARTED)
326 query_format(wave_dev, format);
328 if (woinst->format.samplingrate != format->samplingrate ||
329 woinst->format.channels != format->channels ||
330 woinst->format.bitsperchannel != format->bitsperchannel) {
332 woinst->format = *format;
334 if (woinst->state == WAVE_STATE_CLOSED)
337 emu10k1_timer_uninstall(card, &woinst->timer);
339 for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
340 emu10k1_voice_free(&woinst->voice[voicenum]);
342 if (get_voice(card, woinst, voicenum) < 0) {
344 emu10k1_waveout_close(wave_dev);
349 delay = (48000 * woinst->buffer.fragment_size) /
350 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
352 emu10k1_timer_install(card, &woinst->timer, delay);
358 void emu10k1_waveout_stop(struct emu10k1_wavedevice *wave_dev)
360 struct emu10k1_card *card = wave_dev->card;
361 struct woinst *woinst = wave_dev->woinst;
363 DPF(2, "emu10k1_waveout_stop()\n");
365 if (!(woinst->state & WAVE_STATE_STARTED))
368 emu10k1_timer_disable(card, &woinst->timer);
370 /* Stop actual voices */
371 emu10k1_voices_stop(woinst->voice, woinst->num_voices);
373 emu10k1_waveout_update(woinst);
375 woinst->state &= ~WAVE_STATE_STARTED;
379 * emu10k1_waveout_getxfersize -
381 * gives the total free bytes on the voice buffer, including silence bytes
382 * (basically: total_free_bytes = free_bytes + silence_bytes).
385 void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 *total_free_bytes)
387 struct waveout_buffer *buffer = &woinst->buffer;
390 if (woinst->mmapped) {
391 *total_free_bytes = buffer->free_bytes;
395 pending_bytes = buffer->size - buffer->free_bytes;
397 buffer->fill_silence = (pending_bytes < (signed) buffer->fragment_size * 2) ? 1 : 0;
399 if (pending_bytes > (signed) buffer->silence_bytes) {
400 *total_free_bytes = (buffer->free_bytes + buffer->silence_bytes);
402 *total_free_bytes = buffer->size;
403 buffer->silence_bytes = pending_bytes;
404 if (pending_bytes < 0) {
405 buffer->silence_pos = buffer->hw_pos;
406 buffer->silence_bytes = 0;
407 buffer->free_bytes = buffer->size;
408 DPF(1, "buffer underrun\n");
416 * copies a block of pcm data to a voice buffer.
417 * Notice that the voice buffer is actually a set of disjointed memory pages.
420 static void copy_block(void **dst, u32 str, u8 __user *src, u32 len)
426 pg = str / PAGE_SIZE;
427 pgoff = str % PAGE_SIZE;
429 if (len > PAGE_SIZE - pgoff) {
430 k = PAGE_SIZE - pgoff;
431 if (__copy_from_user((u8 *)dst[pg] + pgoff, src, k))
434 while (len > PAGE_SIZE) {
435 if (__copy_from_user(dst[++pg], src + k, PAGE_SIZE))
440 if (__copy_from_user(dst[++pg], src + k, len))
444 __copy_from_user((u8 *)dst[pg] + pgoff, src, len);
450 * copies a block of pcm data containing n interleaved channels to n mono voice buffers.
451 * Notice that the voice buffer is actually a set of disjointed memory pages.
454 static void copy_ilv_block(struct woinst *woinst, u32 str, u8 __user *src, u32 len)
458 unsigned int voice_num;
459 struct emu_voice *voice = woinst->voice;
461 pg = str / PAGE_SIZE;
462 pgoff = str % PAGE_SIZE;
465 for (voice_num = 0; voice_num < woinst->num_voices; voice_num++) {
466 if (__copy_from_user((u8 *)(voice[voice_num].mem.addr[pg]) + pgoff, src, woinst->format.bytespervoicesample))
468 src += woinst->format.bytespervoicesample;
471 len -= woinst->format.bytespervoicesample;
473 pgoff += woinst->format.bytespervoicesample;
474 if (pgoff >= PAGE_SIZE) {
484 * fills a set voice buffers with a block of a given sample.
487 static void fill_block(struct woinst *woinst, u32 str, u8 data, u32 len)
491 unsigned int voice_num;
492 struct emu_voice *voice = woinst->voice;
495 pg = str / PAGE_SIZE;
496 pgoff = str % PAGE_SIZE;
498 if (len > PAGE_SIZE - pgoff) {
499 k = PAGE_SIZE - pgoff;
500 for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
501 memset((u8 *)voice[voice_num].mem.addr[pg] + pgoff, data, k);
503 while (len > PAGE_SIZE) {
505 for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
506 memset(voice[voice_num].mem.addr[pg], data, PAGE_SIZE);
511 for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
512 memset(voice[voice_num].mem.addr[pg], data, len);
515 for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
516 memset((u8 *)voice[voice_num].mem.addr[pg] + pgoff, data, len);
521 * emu10k1_waveout_xferdata -
523 * copies pcm data to the voice buffer. Silence samples
524 * previously added to the buffer are overwritten.
527 void emu10k1_waveout_xferdata(struct woinst *woinst, u8 __user *data, u32 *size)
529 struct waveout_buffer *buffer = &woinst->buffer;
530 struct voice_mem *mem = &woinst->voice[0].mem;
531 u32 sizetocopy, sizetocopy_now, start;
534 sizetocopy = min_t(u32, buffer->size, *size);
540 spin_lock_irqsave(&woinst->lock, flags);
541 start = (buffer->size + buffer->silence_pos - buffer->silence_bytes) % buffer->size;
543 if (sizetocopy > buffer->silence_bytes) {
544 buffer->silence_pos += sizetocopy - buffer->silence_bytes;
545 buffer->free_bytes -= sizetocopy - buffer->silence_bytes;
546 buffer->silence_bytes = 0;
548 buffer->silence_bytes -= sizetocopy;
550 spin_unlock_irqrestore(&woinst->lock, flags);
552 sizetocopy_now = buffer->size - start;
553 if (sizetocopy > sizetocopy_now) {
554 sizetocopy -= sizetocopy_now;
555 if (woinst->num_voices > 1) {
556 copy_ilv_block(woinst, start, data, sizetocopy_now);
557 copy_ilv_block(woinst, 0, data + sizetocopy_now * woinst->num_voices, sizetocopy);
559 copy_block(mem->addr, start, data, sizetocopy_now);
560 copy_block(mem->addr, 0, data + sizetocopy_now, sizetocopy);
563 if (woinst->num_voices > 1)
564 copy_ilv_block(woinst, start, data, sizetocopy);
566 copy_block(mem->addr, start, data, sizetocopy);
571 * emu10k1_waveout_fillsilence -
573 * adds samples of silence to the voice buffer so that we
574 * don't loop over stale pcm data.
577 void emu10k1_waveout_fillsilence(struct woinst *woinst)
579 struct waveout_buffer *buffer = &woinst->buffer;
580 u32 sizetocopy, sizetocopy_now, start;
584 sizetocopy = buffer->fragment_size;
586 if (woinst->format.bitsperchannel == 16)
591 spin_lock_irqsave(&woinst->lock, flags);
592 buffer->silence_bytes += sizetocopy;
593 buffer->free_bytes -= sizetocopy;
594 buffer->silence_pos %= buffer->size;
595 start = buffer->silence_pos;
596 buffer->silence_pos += sizetocopy;
597 spin_unlock_irqrestore(&woinst->lock, flags);
599 sizetocopy_now = buffer->size - start;
601 if (sizetocopy > sizetocopy_now) {
602 sizetocopy -= sizetocopy_now;
603 fill_block(woinst, start, filldata, sizetocopy_now);
604 fill_block(woinst, 0, filldata, sizetocopy);
606 fill_block(woinst, start, filldata, sizetocopy);
611 * emu10k1_waveout_update -
613 * updates the position of the voice buffer hardware pointer (hw_pos)
614 * and the number of free bytes on the buffer (free_bytes).
615 * The free bytes _don't_ include silence bytes that may have been
616 * added to the buffer.
619 void emu10k1_waveout_update(struct woinst *woinst)
624 /* There is no actual start yet */
625 if (!(woinst->state & WAVE_STATE_STARTED)) {
626 hw_pos = woinst->buffer.hw_pos;
628 /* hw_pos in sample units */
629 hw_pos = sblive_readptr(woinst->voice[0].card, CCCA_CURRADDR, woinst->voice[0].num);
631 if(hw_pos < woinst->voice[0].start)
632 hw_pos += woinst->buffer.size / woinst->format.bytespervoicesample - woinst->voice[0].start;
634 hw_pos -= woinst->voice[0].start;
636 hw_pos *= woinst->format.bytespervoicesample;
639 diff = (woinst->buffer.size + hw_pos - woinst->buffer.hw_pos) % woinst->buffer.size;
640 woinst->total_played += diff;
641 woinst->buffer.free_bytes += diff;
642 woinst->buffer.hw_pos = hw_pos;