| 1 | /* $Id: cardwo.c 142 2000-04-23 14:55:46Z ktk $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | **********************************************************************
|
|---|
| 5 | * cardwo.c - PCM output HAL for emu10k1 driver
|
|---|
| 6 | * Copyright 1999, 2000 Creative Labs, Inc.
|
|---|
| 7 | *
|
|---|
| 8 | **********************************************************************
|
|---|
| 9 | *
|
|---|
| 10 | * Date Author Summary of changes
|
|---|
| 11 | * ---- ------ ------------------
|
|---|
| 12 | * October 20, 1999 Bertrand Lee base code release
|
|---|
| 13 | *
|
|---|
| 14 | **********************************************************************
|
|---|
| 15 | *
|
|---|
| 16 | * This program is free software; you can redistribute it and/or
|
|---|
| 17 | * modify it under the terms of the GNU General Public License as
|
|---|
| 18 | * published by the Free Software Foundation; either version 2 of
|
|---|
| 19 | * the License, or (at your option) any later version.
|
|---|
| 20 | *
|
|---|
| 21 | * This program is distributed in the hope that it will be useful,
|
|---|
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 24 | * GNU General Public License for more details.
|
|---|
| 25 | *
|
|---|
| 26 | * You should have received a copy of the GNU General Public
|
|---|
| 27 | * License along with this program; if not, write to the Free
|
|---|
| 28 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
|
|---|
| 29 | * USA.
|
|---|
| 30 | *
|
|---|
| 31 | **********************************************************************
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #include "hwaccess.h"
|
|---|
| 35 | #include "cardwo.h"
|
|---|
| 36 | #include "audio.h"
|
|---|
| 37 |
|
|---|
| 38 | /* Volume calcs */
|
|---|
| 39 |
|
|---|
| 40 | static int set_volume_instance(struct emu10k1_waveout *card_waveout, struct wave_out *wave_out, struct voice_param *left)
|
|---|
| 41 | {
|
|---|
| 42 | /* only applicable for playback */
|
|---|
| 43 | u32 volL, volR, vol = 0;
|
|---|
| 44 |
|
|---|
| 45 | volL = (wave_out->localvol & 0xffff);
|
|---|
| 46 | volR = ((wave_out->localvol >> 16) & 0xffff);
|
|---|
| 47 |
|
|---|
| 48 | if (wave_out->globalvolFactor) {
|
|---|
| 49 | volL = ((u32) (((u16) card_waveout->globalvol & 0xffff) * (u16) volL)) / 0xffff;
|
|---|
| 50 | volR = ((u32) (((u16) (card_waveout->globalvol >> 16) & 0xffff) * ((u16) volR))) / 0xffff;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /* BIG ASSUMPTION HERE THAT DEFAULT WAVE PAN/AUX IS 0xff/0xff */
|
|---|
| 54 | /* New volume and pan */
|
|---|
| 55 |
|
|---|
| 56 | if (volL == volR) {
|
|---|
| 57 | vol = volL;
|
|---|
| 58 | left->send_c = 0xff;
|
|---|
| 59 | left->send_b = 0xff;
|
|---|
| 60 | } else {
|
|---|
| 61 | if (volL > volR) {
|
|---|
| 62 | vol = volL;
|
|---|
| 63 | left->send_c = 0xff;
|
|---|
| 64 | left->send_b = (char) ((volR * 255) / vol);
|
|---|
| 65 | } else {
|
|---|
| 66 | vol = volR;
|
|---|
| 67 | left->send_b = 0xff;
|
|---|
| 68 | left->send_c = (char) ((volL * 255) / vol);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | left->initial_attn = 0xff & sumVolumeToAttenuation(vol * 2);
|
|---|
| 73 |
|
|---|
| 74 | return vol;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | static void query_format(struct wave_format *wave_fmt)
|
|---|
| 78 | {
|
|---|
| 79 | if ((wave_fmt->channels != 1) && (wave_fmt->channels != 2))
|
|---|
| 80 | wave_fmt->channels = 2;
|
|---|
| 81 |
|
|---|
| 82 | if (wave_fmt->samplingrate >= 0x2EE00)
|
|---|
| 83 | wave_fmt->samplingrate = 0x2EE00;
|
|---|
| 84 |
|
|---|
| 85 | if ((wave_fmt->bitsperchannel != 8) && (wave_fmt->bitsperchannel != 16))
|
|---|
| 86 | wave_fmt->bitsperchannel = 16;
|
|---|
| 87 |
|
|---|
| 88 | return;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static int alloc_xferbuffer(struct emu10k1_card *card, struct wave_out *wave_out, u32 * size, void ***buffer)
|
|---|
| 92 | {
|
|---|
| 93 | u32 numpages, reqsize, pageindex, pagecount;
|
|---|
| 94 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 95 | unsigned long busaddx;
|
|---|
| 96 | int i;
|
|---|
| 97 |
|
|---|
| 98 | reqsize = *size;
|
|---|
| 99 | numpages = reqsize / PAGE_SIZE;
|
|---|
| 100 |
|
|---|
| 101 | /* If size is not a multiple of PAGE_SIZE then we need to round up */
|
|---|
| 102 | if (reqsize % PAGE_SIZE)
|
|---|
| 103 | numpages += 1;
|
|---|
| 104 |
|
|---|
| 105 | DPD(2, "requested pages is: %d\n", numpages);
|
|---|
| 106 |
|
|---|
| 107 | wavexferbuf->numpages = numpages;
|
|---|
| 108 |
|
|---|
| 109 | /* Only for playback, request for emu address space */
|
|---|
| 110 | /* Support non page-aligned buffer, don't need interpolation page */
|
|---|
| 111 |
|
|---|
| 112 | if ((wave_out->emupageindex = emu10k1_addxmgr_alloc(numpages * PAGE_SIZE, card)) < 0)
|
|---|
| 113 | return CTSTATUS_ERROR;
|
|---|
| 114 |
|
|---|
| 115 | if ((wave_out->pagetable = (void **) kmalloc(sizeof(void *) * numpages, GFP_KERNEL)) == NULL)
|
|---|
| 116 | return CTSTATUS_ERROR;
|
|---|
| 117 |
|
|---|
| 118 | /* Fill in virtual memory table */
|
|---|
| 119 | for (pagecount = 0; pagecount < numpages; pagecount++) {
|
|---|
| 120 | if ((wave_out->pagetable[pagecount] = (void *) __get_free_page(GFP_KERNEL)) == NULL) {
|
|---|
| 121 | wavexferbuf->numpages = pagecount;
|
|---|
| 122 | return CTSTATUS_ERROR;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | DPD(2, "Virtual Addx: %p\n", wave_out->pagetable[pagecount]);
|
|---|
| 126 |
|
|---|
| 127 | for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
|
|---|
| 128 | busaddx = virt_to_bus((u8 *) wave_out->pagetable[pagecount] + i * EMUPAGESIZE);
|
|---|
| 129 |
|
|---|
| 130 | DPD(3, "Bus Addx: %lx\n", busaddx);
|
|---|
| 131 |
|
|---|
| 132 | pageindex = wave_out->emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
|
|---|
| 133 |
|
|---|
| 134 | ((u32 *) card->virtualpagetable->virtaddx)[pageindex] = ((u32) busaddx * 2) | pageindex;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | *buffer = wave_out->pagetable;
|
|---|
| 139 |
|
|---|
| 140 | return CTSTATUS_SUCCESS;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | static int get_xferbuffer(struct emu10k1_card *card, struct wave_out *wave_out, u32 * size)
|
|---|
| 144 | {
|
|---|
| 145 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 146 | void **buffer;
|
|---|
| 147 |
|
|---|
| 148 | wavexferbuf->xferpos = 0;
|
|---|
| 149 | wavexferbuf->silence_xferpos = 0;
|
|---|
| 150 | wavexferbuf->stopposition = 0;
|
|---|
| 151 | wavexferbuf->is_stereo = (wave_out->wave_fmt.channels == 2) ? 1 : 0;
|
|---|
| 152 | wavexferbuf->is_16bit = (wave_out->wave_fmt.bitsperchannel == 16) ? 1 : 0;
|
|---|
| 153 | wavexferbuf->bytespersample = (wavexferbuf->is_stereo + 1) * (wavexferbuf->is_16bit + 1);
|
|---|
| 154 |
|
|---|
| 155 | if (alloc_xferbuffer(card, wave_out, size, &buffer) != CTSTATUS_SUCCESS)
|
|---|
| 156 | return CTSTATUS_ERROR;
|
|---|
| 157 |
|
|---|
| 158 | /* xferbufsize contains actual transfer buffer size */
|
|---|
| 159 | wavexferbuf->xferbufsize = *size;
|
|---|
| 160 | wavexferbuf->xferbuffer = buffer;
|
|---|
| 161 |
|
|---|
| 162 | return CTSTATUS_SUCCESS;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | static void dealloc_xferbuffer(struct emu10k1_card *card, struct wave_out *wave_out)
|
|---|
| 166 | {
|
|---|
| 167 | u32 pagecount, pageindex;
|
|---|
| 168 | int i;
|
|---|
| 169 |
|
|---|
| 170 | if (wave_out->pagetable != NULL) {
|
|---|
| 171 | for (pagecount = 0; pagecount < wave_out->wavexferbuf->numpages; pagecount++) {
|
|---|
| 172 | free_page((unsigned long) wave_out->pagetable[pagecount]);
|
|---|
| 173 |
|
|---|
| 174 | for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
|
|---|
| 175 | pageindex = wave_out->emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
|
|---|
| 176 | ((u32 *) card->virtualpagetable->virtaddx)[pageindex] = (card->silentpage->busaddx * 2) | pageindex;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | kfree(wave_out->pagetable);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | emu10k1_addxmgr_free(card, wave_out->emupageindex);
|
|---|
| 183 |
|
|---|
| 184 | return;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | static int get_voice(struct emu10k1_card *card, struct wave_out *wave_out, int device)
|
|---|
| 188 | {
|
|---|
| 189 | struct emu10k1_waveout *card_waveout = card->waveout;
|
|---|
| 190 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 191 | struct voice_allocdesc voice_allocdesc;
|
|---|
| 192 | struct voice_param *left, *right;
|
|---|
| 193 | u32 size;
|
|---|
| 194 |
|
|---|
| 195 | /* Allocate voices here, if no voices available, return error.
|
|---|
| 196 | * Init voice_allocdesc first.*/
|
|---|
| 197 |
|
|---|
| 198 | voice_allocdesc.usage = VOICEMGR_USAGE_PLAYBACK;
|
|---|
| 199 |
|
|---|
| 200 | voice_allocdesc.flags = 0;
|
|---|
| 201 |
|
|---|
| 202 | if (device == 1)
|
|---|
| 203 | voice_allocdesc.flags |= VOICEMGR_FLAGS_FXRT2;
|
|---|
| 204 |
|
|---|
| 205 | if (wave_out->wave_fmt.channels == 1)
|
|---|
| 206 | voice_allocdesc.flags |= VOICEMGR_FLAGS_MONO;
|
|---|
| 207 |
|
|---|
| 208 | if (wave_out->wave_fmt.bitsperchannel == 16)
|
|---|
| 209 | voice_allocdesc.flags |= VOICEMGR_FLAGS_16BIT;
|
|---|
| 210 |
|
|---|
| 211 | if ((wave_out->voice = emu10k1_voice_alloc(&card->voicemgr, &voice_allocdesc)) == NULL)
|
|---|
| 212 | return CTSTATUS_ERROR;
|
|---|
| 213 |
|
|---|
| 214 | /* voice initialization */
|
|---|
| 215 |
|
|---|
| 216 | left = &wave_out->voice->params;
|
|---|
| 217 |
|
|---|
| 218 | /* Calculate pitch */
|
|---|
| 219 | left->initial_pitch = (u16) (srToPitch(wave_out->wave_fmt.samplingrate) >> 8);
|
|---|
| 220 |
|
|---|
| 221 | DPD(2, "Initial pitch --> %x\n", left->initial_pitch);
|
|---|
| 222 |
|
|---|
| 223 | /* Easy way out.. gotta calculate value */
|
|---|
| 224 | left->pitch_target = 0;
|
|---|
| 225 | left->volume_target = 0;
|
|---|
| 226 | left->FC_target = 0;
|
|---|
| 227 |
|
|---|
| 228 | left->byampl_env_sustain = 0x7f;
|
|---|
| 229 | left->byampl_env_decay = 0x7f;
|
|---|
| 230 |
|
|---|
| 231 | if (wave_out->globalreverbFactor) {
|
|---|
| 232 | u8 t = (card_waveout->globalreverb & 0xff) + (wave_out->localreverb & 0xff);
|
|---|
| 233 |
|
|---|
| 234 | left->send_a = (t > 255) ? 255 : t;
|
|---|
| 235 | } else {
|
|---|
| 236 | left->send_a = 0;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | if (wave_out->globalchorusFactor) {
|
|---|
| 240 | u8 t = (card_waveout->globalchorus & 0xff) + (wave_out->localchorus & 0xff);
|
|---|
| 241 |
|
|---|
| 242 | left->send_d = (t > 255) ? 255 : t;
|
|---|
| 243 | } else {
|
|---|
| 244 | left->send_d = 0;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | set_volume_instance(card_waveout, wave_out, left);
|
|---|
| 248 |
|
|---|
| 249 | left->pan_target = left->send_c;
|
|---|
| 250 | left->aux_target = left->send_b;
|
|---|
| 251 |
|
|---|
| 252 | size = wavexferbuf->xferbufsize / wavexferbuf->bytespersample;
|
|---|
| 253 | left->start = 2 * (wave_out->emupageindex << 11) / wavexferbuf->bytespersample;
|
|---|
| 254 | left->end = left->start + size;
|
|---|
| 255 | left->startloop = left->start;
|
|---|
| 256 | left->endloop = left->end;
|
|---|
| 257 |
|
|---|
| 258 | if (wave_out->voice->linked_voice) {
|
|---|
| 259 | DPF(2, "is stereo\n");
|
|---|
| 260 | right = &wave_out->voice->linked_voice->params;
|
|---|
| 261 |
|
|---|
| 262 | right->initial_pitch = left->initial_pitch;
|
|---|
| 263 |
|
|---|
| 264 | /* Easy way out.. gotta calculate value */
|
|---|
| 265 | right->pitch_target = 0;
|
|---|
| 266 | right->volume_target = 0;
|
|---|
| 267 | right->FC_target = 0;
|
|---|
| 268 |
|
|---|
| 269 | right->byampl_env_sustain = 0x7f;
|
|---|
| 270 | right->byampl_env_decay = 0x7f;
|
|---|
| 271 |
|
|---|
| 272 | right->send_d = left->send_d;
|
|---|
| 273 | right->send_a = left->send_a;
|
|---|
| 274 |
|
|---|
| 275 | /* Left output of right channel is always zero */
|
|---|
| 276 | right->send_c = 0;
|
|---|
| 277 |
|
|---|
| 278 | /* Update right channel aux */
|
|---|
| 279 | right->pan_target = 0;
|
|---|
| 280 | right->send_b = left->send_b;
|
|---|
| 281 | right->aux_target = right->send_b;
|
|---|
| 282 |
|
|---|
| 283 | /* Zero out right output of left channel */
|
|---|
| 284 | left->send_b = 0;
|
|---|
| 285 | left->aux_target = 0;
|
|---|
| 286 |
|
|---|
| 287 | /* Update right channel attenuation */
|
|---|
| 288 | right->initial_attn = left->initial_attn;
|
|---|
| 289 |
|
|---|
| 290 | right->start = left->start;
|
|---|
| 291 | right->end = left->end;
|
|---|
| 292 | right->startloop = left->startloop;
|
|---|
| 293 | right->endloop = left->endloop;
|
|---|
| 294 |
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | DPD(2, "voice: start=%x, end=%x, startloop=%x, endloop=%x\n", left->start, left->end, left->startloop, left->endloop);
|
|---|
| 298 |
|
|---|
| 299 | return CTSTATUS_SUCCESS;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | int emu10k1_waveout_open(struct emu10k1_wavedevice *wave_dev)
|
|---|
| 303 | {
|
|---|
| 304 | struct emu10k1_card *card = wave_dev->card;
|
|---|
| 305 | struct woinst *woinst = wave_dev->woinst;
|
|---|
| 306 | struct wave_out *wave_out;
|
|---|
| 307 | u32 bytespersec, delay;
|
|---|
| 308 | u32 buffsize;
|
|---|
| 309 |
|
|---|
| 310 | DPF(2, "emu10k1_waveout_open()\n");
|
|---|
| 311 |
|
|---|
| 312 | if ((wave_out = (struct wave_out *) kmalloc(sizeof(struct wave_out), GFP_KERNEL)) == NULL) {
|
|---|
| 313 | ERROR();
|
|---|
| 314 | emu10k1_waveout_close(wave_dev);
|
|---|
| 315 | return CTSTATUS_ERROR;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | woinst->wave_out = wave_out;
|
|---|
| 319 |
|
|---|
| 320 | /* Init channel object */
|
|---|
| 321 | wave_out->state = CARDWAVE_STATE_STOPPED;
|
|---|
| 322 | wave_out->wave_fmt = woinst->wave_fmt;
|
|---|
| 323 | wave_out->voice = NULL;
|
|---|
| 324 | wave_out->emupageindex = -1;
|
|---|
| 325 | wave_out->wavexferbuf = NULL;
|
|---|
| 326 | wave_out->pagetable = NULL;
|
|---|
| 327 | wave_out->timer = NULL;
|
|---|
| 328 |
|
|---|
| 329 | /* Assign default local volume */
|
|---|
| 330 | /* FIXME: Should we be maxing the initial values like this? */
|
|---|
| 331 | wave_out->localvol = 0xffffffff;
|
|---|
| 332 | wave_out->localreverb = 0xffffffff;
|
|---|
| 333 | wave_out->localchorus = 0xffffffff;
|
|---|
| 334 | wave_out->globalvolFactor = 0xffff;
|
|---|
| 335 | wave_out->globalreverbFactor = 0xffff;
|
|---|
| 336 | wave_out->globalchorusFactor = 0xffff;
|
|---|
| 337 |
|
|---|
| 338 | wave_out->setpos = 0;
|
|---|
| 339 | wave_out->position = 0;
|
|---|
| 340 |
|
|---|
| 341 | wave_out->fill_silence = 0;
|
|---|
| 342 |
|
|---|
| 343 | if ((wave_out->wavexferbuf = (struct wave_xferbuf *) kmalloc(sizeof(struct wave_xferbuf), GFP_KERNEL)) == NULL) {
|
|---|
| 344 | ERROR();
|
|---|
| 345 | emu10k1_waveout_close(wave_dev);
|
|---|
| 346 | return CTSTATUS_ERROR;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | buffsize = woinst->fragment_size * woinst->numfrags;
|
|---|
| 350 |
|
|---|
| 351 | if (get_xferbuffer(card, wave_out, &buffsize) != CTSTATUS_SUCCESS) {
|
|---|
| 352 | ERROR();
|
|---|
| 353 | emu10k1_waveout_close(wave_dev);
|
|---|
| 354 | return CTSTATUS_ERROR;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | woinst->fragment_size = buffsize / woinst->numfrags;
|
|---|
| 358 | wave_out->callbacksize = woinst->fragment_size;
|
|---|
| 359 |
|
|---|
| 360 | if (get_voice(card, wave_out, woinst->device) != CTSTATUS_SUCCESS) {
|
|---|
| 361 | ERROR();
|
|---|
| 362 | emu10k1_waveout_close(wave_dev);
|
|---|
| 363 | return CTSTATUS_ERROR;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | bytespersec = wave_out->wave_fmt.channels * (wave_out->wave_fmt.bitsperchannel >> 3) * (wave_out->wave_fmt.samplingrate);
|
|---|
| 367 | delay = (48000 * wave_out->callbacksize) / bytespersec;
|
|---|
| 368 |
|
|---|
| 369 | if ((wave_out->timer = emu10k1_timer_install(card, emu10k1_waveout_bh, (unsigned long) wave_dev, delay / 2)) == NULL) {
|
|---|
| 370 | ERROR();
|
|---|
| 371 | emu10k1_waveout_close(wave_dev);
|
|---|
| 372 | return CTSTATUS_ERROR;
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | return CTSTATUS_SUCCESS;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | void emu10k1_waveout_close(struct emu10k1_wavedevice *wave_dev)
|
|---|
| 379 | {
|
|---|
| 380 | struct emu10k1_card *card = wave_dev->card;
|
|---|
| 381 | struct wave_out *wave_out = wave_dev->woinst->wave_out;
|
|---|
| 382 |
|
|---|
| 383 | DPF(2, "emu10k1_waveout_close()\n");
|
|---|
| 384 |
|
|---|
| 385 | if (wave_out->state != CARDWAVE_STATE_STOPPED)
|
|---|
| 386 | emu10k1_waveout_stop(wave_dev);
|
|---|
| 387 |
|
|---|
| 388 | if (wave_out->timer != NULL)
|
|---|
| 389 | emu10k1_timer_uninstall(card, wave_out->timer);
|
|---|
| 390 |
|
|---|
| 391 | if (wave_out->voice != NULL)
|
|---|
| 392 | emu10k1_voice_free(&card->voicemgr, wave_out->voice);
|
|---|
| 393 |
|
|---|
| 394 | if (wave_out->emupageindex >= 0)
|
|---|
| 395 | dealloc_xferbuffer(card, wave_out);
|
|---|
| 396 |
|
|---|
| 397 | if (wave_out->wavexferbuf != NULL)
|
|---|
| 398 | kfree(wave_out->wavexferbuf);
|
|---|
| 399 |
|
|---|
| 400 | kfree(wave_out);
|
|---|
| 401 | wave_dev->woinst->wave_out = NULL;
|
|---|
| 402 |
|
|---|
| 403 | return;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | int emu10k1_waveout_start(struct emu10k1_wavedevice *wave_dev)
|
|---|
| 407 | {
|
|---|
| 408 | struct emu10k1_card *card = wave_dev->card;
|
|---|
| 409 | struct wave_out *wave_out = wave_dev->woinst->wave_out;
|
|---|
| 410 | u32 start, startPosition;
|
|---|
| 411 |
|
|---|
| 412 | DPF(2, "emu10k1_waveout_start()\n");
|
|---|
| 413 |
|
|---|
| 414 | /* If already started, return success */
|
|---|
| 415 | if (wave_out->state == CARDWAVE_STATE_STARTED)
|
|---|
| 416 | return CTSTATUS_SUCCESS;
|
|---|
| 417 |
|
|---|
| 418 | if (wave_out->state == CARDWAVE_STATE_STOPPED && wave_out->setpos)
|
|---|
| 419 | startPosition = wave_out->position / (wave_out->wavexferbuf->bytespersample);
|
|---|
| 420 | else
|
|---|
| 421 | startPosition = wave_out->wavexferbuf->stopposition;
|
|---|
| 422 |
|
|---|
| 423 | start = wave_out->voice->params.start;
|
|---|
| 424 | wave_out->voice->params.start += startPosition;
|
|---|
| 425 |
|
|---|
| 426 | DPD(2, "CA is %x\n", wave_out->voice->params.start);
|
|---|
| 427 |
|
|---|
| 428 | emu10k1_voice_playback_setup(wave_out->voice);
|
|---|
| 429 |
|
|---|
| 430 | wave_out->voice->params.start = start;
|
|---|
| 431 |
|
|---|
| 432 | /* Actual start */
|
|---|
| 433 | emu10k1_voice_start(wave_out->voice);
|
|---|
| 434 |
|
|---|
| 435 | wave_out->state = CARDWAVE_STATE_STARTED;
|
|---|
| 436 | wave_out->setpos = 0;
|
|---|
| 437 |
|
|---|
| 438 | emu10k1_timer_enable(card, wave_out->timer);
|
|---|
| 439 |
|
|---|
| 440 | return CTSTATUS_SUCCESS;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | int emu10k1_waveout_setformat(struct emu10k1_wavedevice *wave_dev)
|
|---|
| 444 | {
|
|---|
| 445 | struct emu10k1_card *card = wave_dev->card;
|
|---|
| 446 | struct woinst *woinst = wave_dev->woinst;
|
|---|
| 447 | struct wave_out *wave_out = woinst->wave_out;
|
|---|
| 448 | u32 bytespersec, delay;
|
|---|
| 449 |
|
|---|
| 450 | DPF(2, "emu10k1_waveout_setformat()\n");
|
|---|
| 451 |
|
|---|
| 452 | query_format(&woinst->wave_fmt);
|
|---|
| 453 |
|
|---|
| 454 | if (wave_out == NULL)
|
|---|
| 455 | return CTSTATUS_SUCCESS;
|
|---|
| 456 |
|
|---|
| 457 | if (wave_out->state == CARDWAVE_STATE_STARTED) {
|
|---|
| 458 | woinst->wave_fmt = wave_out->wave_fmt;
|
|---|
| 459 | return CTSTATUS_SUCCESS;
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | if ((wave_out->wave_fmt.samplingrate != woinst->wave_fmt.samplingrate)
|
|---|
| 463 | || (wave_out->wave_fmt.bitsperchannel != woinst->wave_fmt.bitsperchannel)
|
|---|
| 464 | || (wave_out->wave_fmt.channels != woinst->wave_fmt.channels)) {
|
|---|
| 465 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 466 |
|
|---|
| 467 | emu10k1_timer_uninstall(card, wave_out->timer);
|
|---|
| 468 |
|
|---|
| 469 | emu10k1_voice_free(&card->voicemgr, wave_out->voice);
|
|---|
| 470 |
|
|---|
| 471 | wave_out->wave_fmt = woinst->wave_fmt;
|
|---|
| 472 | wave_out->timer = NULL;
|
|---|
| 473 |
|
|---|
| 474 | wavexferbuf->xferpos = 0;
|
|---|
| 475 | wavexferbuf->silence_xferpos = 0;
|
|---|
| 476 | wavexferbuf->stopposition = 0;
|
|---|
| 477 | wavexferbuf->is_stereo = (wave_out->wave_fmt.channels == 2) ? 1 : 0;
|
|---|
| 478 | wavexferbuf->is_16bit = (wave_out->wave_fmt.bitsperchannel == 16) ? 1 : 0;
|
|---|
| 479 | wavexferbuf->bytespersample = (wavexferbuf->is_stereo + 1) * (wavexferbuf->is_16bit + 1);
|
|---|
| 480 |
|
|---|
| 481 | if (get_voice(card, wave_out, woinst->device) != CTSTATUS_SUCCESS) {
|
|---|
| 482 | ERROR();
|
|---|
| 483 | emu10k1_waveout_close(wave_dev);
|
|---|
| 484 | return CTSTATUS_ERROR;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | bytespersec = wave_out->wave_fmt.channels * (wave_out->wave_fmt.bitsperchannel >> 3) * (wave_out->wave_fmt.samplingrate);
|
|---|
| 488 | delay = (48000 * wave_out->callbacksize) / bytespersec;
|
|---|
| 489 |
|
|---|
| 490 | if ((wave_out->timer = emu10k1_timer_install(card, emu10k1_waveout_bh, (unsigned long) wave_dev, delay / 2)) == NULL) {
|
|---|
| 491 | ERROR();
|
|---|
| 492 | emu10k1_waveout_close(wave_dev);
|
|---|
| 493 | return CTSTATUS_ERROR;
|
|---|
| 494 | }
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | return CTSTATUS_SUCCESS;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | void emu10k1_waveout_stop(struct emu10k1_wavedevice *wave_dev)
|
|---|
| 501 | {
|
|---|
| 502 | struct emu10k1_card *card = wave_dev->card;
|
|---|
| 503 | struct wave_out *wave_out = wave_dev->woinst->wave_out;
|
|---|
| 504 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 505 | u32 samples = 32;
|
|---|
| 506 | u32 position;
|
|---|
| 507 |
|
|---|
| 508 | DPF(2, "emu10k1_waveout_stop()\n");
|
|---|
| 509 |
|
|---|
| 510 | if (wave_out->state == CARDWAVE_STATE_STOPPED)
|
|---|
| 511 | return;
|
|---|
| 512 |
|
|---|
| 513 | emu10k1_timer_disable(card, wave_out->timer);
|
|---|
| 514 |
|
|---|
| 515 | /* Stop actual voice */
|
|---|
| 516 | emu10k1_voice_stop(wave_out->voice);
|
|---|
| 517 |
|
|---|
| 518 | /* Save the stop position */
|
|---|
| 519 | emu10k1_voice_getcontrol(wave_out->voice, CCCA_CURRADDR, &wavexferbuf->stopposition);
|
|---|
| 520 |
|
|---|
| 521 | wavexferbuf->stopposition -= wave_out->voice->params.start;
|
|---|
| 522 |
|
|---|
| 523 | /* Refer to voicemgr.c, CA is not started at zero. We need to take this into account. */
|
|---|
| 524 | position = wavexferbuf->stopposition * wavexferbuf->bytespersample;
|
|---|
| 525 |
|
|---|
| 526 | if (!wavexferbuf->is_16bit)
|
|---|
| 527 | samples <<= 1;
|
|---|
| 528 |
|
|---|
| 529 | if (wavexferbuf->is_stereo)
|
|---|
| 530 | samples <<= 1;
|
|---|
| 531 |
|
|---|
| 532 | samples -= 4;
|
|---|
| 533 |
|
|---|
| 534 | if (position >= samples * (wavexferbuf->is_16bit + 1))
|
|---|
| 535 | position -= samples * (wavexferbuf->is_16bit + 1);
|
|---|
| 536 | else
|
|---|
| 537 | position += wavexferbuf->xferbufsize - samples * (wavexferbuf->is_16bit + 1);
|
|---|
| 538 |
|
|---|
| 539 | wavexferbuf->stopposition = position / wavexferbuf->bytespersample;
|
|---|
| 540 |
|
|---|
| 541 | DPD(2, "position is %x\n", wavexferbuf->stopposition);
|
|---|
| 542 |
|
|---|
| 543 | wave_out->state = CARDWAVE_STATE_STOPPED;
|
|---|
| 544 | wave_out->setpos = 0;
|
|---|
| 545 | wave_out->position = 0;
|
|---|
| 546 |
|
|---|
| 547 | return;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 * size)
|
|---|
| 551 | {
|
|---|
| 552 | struct wave_out *wave_out = woinst->wave_out;
|
|---|
| 553 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 554 | u32 curpos = woinst->curpos;
|
|---|
| 555 | u32 pending;
|
|---|
| 556 |
|
|---|
| 557 | if ((curpos > wavexferbuf->silence_xferpos)
|
|---|
| 558 | || ((curpos == wavexferbuf->silence_xferpos)
|
|---|
| 559 | && (wave_out->state == CARDWAVE_STATE_STARTED))
|
|---|
| 560 | || ((curpos == wavexferbuf->silence_xferpos) && (wavexferbuf->silence_xferpos != 0)
|
|---|
| 561 | && (wave_out->state == CARDWAVE_STATE_STOPPED))) {
|
|---|
| 562 | *size = curpos - wavexferbuf->silence_xferpos;
|
|---|
| 563 | } else {
|
|---|
| 564 | *size = curpos - wavexferbuf->silence_xferpos + wavexferbuf->xferbufsize;
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | pending = wavexferbuf->xferbufsize - *size;
|
|---|
| 568 |
|
|---|
| 569 | wave_out->fill_silence = (pending < wave_out->callbacksize) ? 1 : 0;
|
|---|
| 570 |
|
|---|
| 571 | if (pending > woinst->silence_bytes) {
|
|---|
| 572 | *size += woinst->silence_bytes;
|
|---|
| 573 | } else {
|
|---|
| 574 | *size = wavexferbuf->xferbufsize;
|
|---|
| 575 | wavexferbuf->xferpos = curpos;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | return;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | static void copy_block(u32 dst, u8 * src, u32 len, void **pt)
|
|---|
| 582 | {
|
|---|
| 583 | int i, j, k;
|
|---|
| 584 |
|
|---|
| 585 | i = dst / PAGE_SIZE;
|
|---|
| 586 | j = dst % PAGE_SIZE;
|
|---|
| 587 | k = (len > PAGE_SIZE - j) ? PAGE_SIZE - j : len;
|
|---|
| 588 | #ifdef TARGET_OS2
|
|---|
| 589 | copy_from_user((u8 *)pt[i] + j, src, k);
|
|---|
| 590 | #else
|
|---|
| 591 | copy_from_user(pt[i] + j, src, k);
|
|---|
| 592 | #endif
|
|---|
| 593 | len -= k;
|
|---|
| 594 | while (len >= PAGE_SIZE) {
|
|---|
| 595 | copy_from_user(pt[++i], src + k, PAGE_SIZE);
|
|---|
| 596 | k += PAGE_SIZE;
|
|---|
| 597 | len -= PAGE_SIZE;
|
|---|
| 598 | }
|
|---|
| 599 | copy_from_user(pt[++i], src + k, len);
|
|---|
| 600 |
|
|---|
| 601 | return;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | static void fill_block(u32 dst, u8 val, u32 len, void **pt)
|
|---|
| 605 | {
|
|---|
| 606 | int i, j, k;
|
|---|
| 607 |
|
|---|
| 608 | i = dst / PAGE_SIZE;
|
|---|
| 609 | j = dst % PAGE_SIZE;
|
|---|
| 610 | k = (len > PAGE_SIZE - j) ? PAGE_SIZE - j : len;
|
|---|
| 611 | #ifdef TARGET_OS2
|
|---|
| 612 | memset((u8 *)pt[i] + j, val, k);
|
|---|
| 613 | #else
|
|---|
| 614 | memset(pt[i] + j, val, k);
|
|---|
| 615 | #endif
|
|---|
| 616 | len -= k;
|
|---|
| 617 | while (len >= PAGE_SIZE) {
|
|---|
| 618 | memset(pt[++i], val, PAGE_SIZE);
|
|---|
| 619 | len -= PAGE_SIZE;
|
|---|
| 620 | }
|
|---|
| 621 | memset(pt[++i], val, len);
|
|---|
| 622 |
|
|---|
| 623 | return;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | void emu10k1_waveout_xferdata(struct woinst *woinst, u8 * data, u32 * size)
|
|---|
| 627 | {
|
|---|
| 628 | struct wave_out *wave_out = woinst->wave_out;
|
|---|
| 629 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 630 | u32 sizetocopy, sizetocopy_now, start;
|
|---|
| 631 | unsigned long flags;
|
|---|
| 632 |
|
|---|
| 633 | sizetocopy = min(wavexferbuf->xferbufsize, *size);
|
|---|
| 634 | *size = sizetocopy;
|
|---|
| 635 |
|
|---|
| 636 | if (!sizetocopy)
|
|---|
| 637 | return;
|
|---|
| 638 |
|
|---|
| 639 | spin_lock_irqsave(&woinst->lock, flags);
|
|---|
| 640 | woinst->silence_bytes = 0;
|
|---|
| 641 | sizetocopy_now = wavexferbuf->xferbufsize - wavexferbuf->xferpos;
|
|---|
| 642 |
|
|---|
| 643 | start = wavexferbuf->xferpos;
|
|---|
| 644 |
|
|---|
| 645 | if (sizetocopy > sizetocopy_now) {
|
|---|
| 646 | sizetocopy -= sizetocopy_now;
|
|---|
| 647 | wavexferbuf->xferpos = sizetocopy;
|
|---|
| 648 | wavexferbuf->silence_xferpos = wavexferbuf->xferpos;
|
|---|
| 649 | spin_unlock_irqrestore(&woinst->lock, flags);
|
|---|
| 650 |
|
|---|
| 651 | copy_block(start, data, sizetocopy_now, wavexferbuf->xferbuffer);
|
|---|
| 652 | copy_block(0, data + sizetocopy_now, sizetocopy, wavexferbuf->xferbuffer);
|
|---|
| 653 | } else {
|
|---|
| 654 | if (sizetocopy == sizetocopy_now)
|
|---|
| 655 | wavexferbuf->xferpos = 0;
|
|---|
| 656 | else
|
|---|
| 657 | wavexferbuf->xferpos += sizetocopy;
|
|---|
| 658 |
|
|---|
| 659 | wavexferbuf->silence_xferpos = wavexferbuf->xferpos;
|
|---|
| 660 | spin_unlock_irqrestore(&woinst->lock, flags);
|
|---|
| 661 |
|
|---|
| 662 | copy_block(start, data, sizetocopy, wavexferbuf->xferbuffer);
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | return;
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | void emu10k1_waveout_fillsilence(struct woinst *woinst)
|
|---|
| 669 | {
|
|---|
| 670 | struct wave_out *wave_out = woinst->wave_out;
|
|---|
| 671 | struct wave_xferbuf *wavexferbuf = wave_out->wavexferbuf;
|
|---|
| 672 | u16 filldata;
|
|---|
| 673 | u32 sizetocopy, sizetocopy_now, start;
|
|---|
| 674 | unsigned long flags;
|
|---|
| 675 |
|
|---|
| 676 | sizetocopy = wave_out->callbacksize;
|
|---|
| 677 |
|
|---|
| 678 | if (wave_out->wave_fmt.bitsperchannel == 8)
|
|---|
| 679 | filldata = 0x8080;
|
|---|
| 680 | else
|
|---|
| 681 | filldata = 0x0000;
|
|---|
| 682 |
|
|---|
| 683 | spin_lock_irqsave(&woinst->lock, flags);
|
|---|
| 684 | woinst->silence_bytes += sizetocopy;
|
|---|
| 685 | sizetocopy_now = wavexferbuf->xferbufsize - wavexferbuf->silence_xferpos;
|
|---|
| 686 | start = wavexferbuf->silence_xferpos;
|
|---|
| 687 |
|
|---|
| 688 | if (sizetocopy > sizetocopy_now) {
|
|---|
| 689 | sizetocopy -= sizetocopy_now;
|
|---|
| 690 | wavexferbuf->silence_xferpos = sizetocopy;
|
|---|
| 691 | spin_unlock_irqrestore(&woinst->lock, flags);
|
|---|
| 692 | fill_block(start, filldata, sizetocopy_now, wavexferbuf->xferbuffer);
|
|---|
| 693 | fill_block(0, filldata, sizetocopy, wavexferbuf->xferbuffer);
|
|---|
| 694 | } else {
|
|---|
| 695 | if (sizetocopy == sizetocopy_now)
|
|---|
| 696 | wavexferbuf->silence_xferpos = 0;
|
|---|
| 697 | else
|
|---|
| 698 | wavexferbuf->silence_xferpos += sizetocopy;
|
|---|
| 699 |
|
|---|
| 700 | spin_unlock_irqrestore(&woinst->lock, flags);
|
|---|
| 701 |
|
|---|
| 702 | fill_block(start, filldata, sizetocopy, wavexferbuf->xferbuffer);
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | return;
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | void emu10k1_waveout_update(struct woinst *woinst)
|
|---|
| 709 | {
|
|---|
| 710 | struct wave_out *wave_out = woinst->wave_out;
|
|---|
| 711 | u32 curpos;
|
|---|
| 712 |
|
|---|
| 713 | /* There is no actual start yet */
|
|---|
| 714 | if (wave_out->state == CARDWAVE_STATE_STOPPED) {
|
|---|
| 715 | if (wave_out->setpos)
|
|---|
| 716 | curpos = wave_out->position;
|
|---|
| 717 | else
|
|---|
| 718 | curpos = wave_out->wavexferbuf->stopposition * wave_out->wavexferbuf->bytespersample;
|
|---|
| 719 | } else {
|
|---|
| 720 | u32 samples;
|
|---|
| 721 |
|
|---|
| 722 | emu10k1_voice_getcontrol(wave_out->voice, CCCA_CURRADDR, &curpos);
|
|---|
| 723 |
|
|---|
| 724 | curpos -= wave_out->voice->params.start;
|
|---|
| 725 |
|
|---|
| 726 | /* Get number of bytes in play buffer per channel.
|
|---|
| 727 | * If 8 bit mode is enabled, this needs to be changed. */
|
|---|
| 728 | curpos *= wave_out->wavexferbuf->bytespersample;
|
|---|
| 729 |
|
|---|
| 730 | /* Refer to voicemgr.c, CA is not started at zero.
|
|---|
| 731 | * We need to take this into account. */
|
|---|
| 732 | samples = 64 * (wave_out->wavexferbuf->is_stereo + 1) - 4 * (wave_out->wavexferbuf->is_16bit + 1);
|
|---|
| 733 |
|
|---|
| 734 | if (curpos >= samples)
|
|---|
| 735 | curpos -= samples;
|
|---|
| 736 | else
|
|---|
| 737 | curpos += wave_out->wavexferbuf->xferbufsize - samples;
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | woinst->total_played += curpos - woinst->curpos;
|
|---|
| 741 |
|
|---|
| 742 | if (curpos < woinst->curpos)
|
|---|
| 743 | woinst->total_played += woinst->fragment_size * woinst->numfrags;
|
|---|
| 744 |
|
|---|
| 745 | woinst->curpos = curpos;
|
|---|
| 746 |
|
|---|
| 747 | return;
|
|---|
| 748 | }
|
|---|