1 /**************************************************************************** 2 3 Copyright Echo Digital Audio Corporation (c) 1998 - 2004 4 All rights reserved 5 www.echoaudio.com 6 7 This file is part of Echo Digital Audio's generic driver library. 8 9 Echo Digital Audio's generic driver library is free software; 10 you can redistribute it and/or modify it under the terms of 11 the GNU General Public License as published by the Free Software 12 Foundation. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, 22 MA 02111-1307, USA. 23 24 ************************************************************************* 25 26 Translation from C++ and adaptation for use in ALSA-Driver 27 were made by Giuliano Pochini <pochini@shiny.it> 28 29 ****************************************************************************/ 30 31 #if PAGE_SIZE < 4096 32 #error PAGE_SIZE is < 4k 33 #endif 34 35 static int restore_dsp_rettings(struct echoaudio *chip); 36 37 38 /* Some vector commands involve the DSP reading or writing data to and from the 39 comm page; if you send one of these commands to the DSP, it will complete the 40 command and then write a non-zero value to the Handshake field in the 41 comm page. This function waits for the handshake to show up. */ 42 static int wait_handshake(struct echoaudio *chip) 43 { 44 int i; 45 46 /* Wait up to 20ms for the handshake from the DSP */ 47 for (i = 0; i < HANDSHAKE_TIMEOUT; i++) { 48 /* Look for the handshake value */ 49 barrier(); 50 if (chip->comm_page->handshake) { 51 return 0; 52 } 53 udelay(1); 54 } 55 56 snd_printk(KERN_ERR "wait_handshake(): Timeout waiting for DSP\n"); 57 return -EBUSY; 58 } 59 60 61 62 /* Much of the interaction between the DSP and the driver is done via vector 63 commands; send_vector writes a vector command to the DSP. Typically, this 64 causes the DSP to read or write fields in the comm page. 65 PCI posting is not required thanks to the handshake logic. */ 66 static int send_vector(struct echoaudio *chip, u32 command) 67 { 68 int i; 69 70 wmb(); /* Flush all pending writes before sending the command */ 71 72 /* Wait up to 100ms for the "vector busy" bit to be off */ 73 for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) { 74 if (!(get_dsp_register(chip, CHI32_VECTOR_REG) & 75 CHI32_VECTOR_BUSY)) { 76 set_dsp_register(chip, CHI32_VECTOR_REG, command); 77 /*if (i) DE_ACT(("send_vector time: %d\n", i));*/ 78 return 0; 79 } 80 udelay(1); 81 } 82 83 DE_ACT((KERN_ERR "timeout on send_vector\n")); 84 return -EBUSY; 85 } 86 87 88 89 /* write_dsp writes a 32-bit value to the DSP; this is used almost 90 exclusively for loading the DSP. */ 91 static int write_dsp(struct echoaudio *chip, u32 data) 92 { 93 u32 status, i; 94 95 for (i = 0; i < 10000000; i++) { /* timeout = 10s */ 96 status = get_dsp_register(chip, CHI32_STATUS_REG); 97 if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) { 98 set_dsp_register(chip, CHI32_DATA_REG, data); 99 wmb(); /* write it immediately */ 100 return 0; 101 } 102 udelay(1); 103 cond_resched(); 104 } 105 106 chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */ 107 DE_ACT((KERN_ERR "write_dsp: Set bad_board to TRUE\n")); 108 return -EIO; 109 } 110 111 112 113 /* read_dsp reads a 32-bit value from the DSP; this is used almost 114 exclusively for loading the DSP and checking the status of the ASIC. */ 115 static int read_dsp(struct echoaudio *chip, u32 *data) 116 { 117 u32 status, i; 118 119 for (i = 0; i < READ_DSP_TIMEOUT; i++) { 120 status = get_dsp_register(chip, CHI32_STATUS_REG); 121 if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) { 122 *data = get_dsp_register(chip, CHI32_DATA_REG); 123 return 0; 124 } 125 udelay(1); 126 cond_resched(); 127 } 128 129 chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */ 130 DE_INIT((KERN_ERR "read_dsp: Set bad_board to TRUE\n")); 131 return -EIO; 132 } 133 134 135 136 /**************************************************************************** 137 Firmware loading functions 138 ****************************************************************************/ 139 140 /* This function is used to read back the serial number from the DSP; 141 this is triggered by the SET_COMMPAGE_ADDR command. 142 Only some early Echogals products have serial numbers in the ROM; 143 the serial number is not used, but you still need to do this as 144 part of the DSP load process. */ 145 static int read_sn(struct echoaudio *chip) 146 { 147 int i; 148 u32 sn[6]; 149 150 for (i = 0; i < 5; i++) { 151 if (read_dsp(chip, &sn[i])) { 152 snd_printk(KERN_ERR "Failed to read serial number\n"); 153 return -EIO; 154 } 155 } 156 DE_INIT(("Read serial number %08x %08x %08x %08x %08x\n", 157 sn[0], sn[1], sn[2], sn[3], sn[4])); 158 return 0; 159 } 160 161 162 163 #ifndef ECHOCARD_HAS_ASIC 164 /* This card has no ASIC, just return ok */ 165 static inline int check_asic_status(struct echoaudio *chip) 166 { 167 chip->asic_loaded = TRUE; 168 return 0; 169 } 170 171 #endif /* !ECHOCARD_HAS_ASIC */ 172 173 174 175 #ifdef ECHOCARD_HAS_ASIC 176 177 /* Load ASIC code - done after the DSP is loaded */ 178 static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic) 179 { 180 const struct firmware *fw; 181 int err; 182 u32 i, size; 183 u8 *code; 184 185 err = get_firmware(&fw, chip, asic); 186 if (err < 0) { 187 snd_printk(KERN_WARNING "Firmware not found !\n"); 188 return err; 189 } 190 191 code = (u8 *)fw->data; 192 size = fw->size; 193 194 /* Send the "Here comes the ASIC" command */ 195 if (write_dsp(chip, cmd) < 0) 196 goto la_error; 197 198 /* Write length of ASIC file in bytes */ 199 if (write_dsp(chip, size) < 0) 200 goto la_error; 201 202 for (i = 0; i < size; i++) { 203 if (write_dsp(chip, code[i]) < 0) 204 goto la_error; 205 } 206 207 DE_INIT(("ASIC loaded\n")); 208 free_firmware(fw); 209 return 0; 210 211 la_error: 212 DE_INIT(("failed on write_dsp\n")); 213 free_firmware(fw); 214 return -EIO; 215 } 216 217 #endif /* ECHOCARD_HAS_ASIC */ 218 219 220 221 #ifdef DSP_56361 222 223 /* Install the resident loader for 56361 DSPs; The resident loader is on 224 the EPROM on the board for 56301 DSP. The resident loader is a tiny little 225 program that is used to load the real DSP code. */ 226 static int install_resident_loader(struct echoaudio *chip) 227 { 228 u32 address; 229 int index, words, i; 230 u16 *code; 231 u32 status; 232 const struct firmware *fw; 233 234 /* 56361 cards only! This check is required by the old 56301-based 235 Mona and Gina24 */ 236 if (chip->device_id != DEVICE_ID_56361) 237 return 0; 238 239 /* Look to see if the resident loader is present. If the resident 240 loader is already installed, host flag 5 will be on. */ 241 status = get_dsp_register(chip, CHI32_STATUS_REG); 242 if (status & CHI32_STATUS_REG_HF5) { 243 DE_INIT(("Resident loader already installed; status is 0x%x\n", 244 status)); 245 return 0; 246 } 247 248 i = get_firmware(&fw, chip, FW_361_LOADER); 249 if (i < 0) { 250 snd_printk(KERN_WARNING "Firmware not found !\n"); 251 return i; 252 } 253 254 /* The DSP code is an array of 16 bit words. The array is divided up 255 into sections. The first word of each section is the size in words, 256 followed by the section type. 257 Since DSP addresses and data are 24 bits wide, they each take up two 258 16 bit words in the array. 259 This is a lot like the other loader loop, but it's not a loop, you 260 don't write the memory type, and you don't write a zero at the end. */ 261 262 /* Set DSP format bits for 24 bit mode */ 263 set_dsp_register(chip, CHI32_CONTROL_REG, 264 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900); 265 266 code = (u16 *)fw->data; 267 268 /* Skip the header section; the first word in the array is the size 269 of the first section, so the first real section of code is pointed 270 to by Code[0]. */ 271 index = code[0]; 272 273 /* Skip the section size, LRS block type, and DSP memory type */ 274 index += 3; 275 276 /* Get the number of DSP words to write */ 277 words = code[index++]; 278 279 /* Get the DSP address for this block; 24 bits, so build from two words */ 280 address = ((u32)code[index] << 16) + code[index + 1]; 281 index += 2; 282 283 /* Write the count to the DSP */ 284 if (write_dsp(chip, words)) { 285 DE_INIT(("install_resident_loader: Failed to write word count!\n")); 286 goto irl_error; 287 } 288 /* Write the DSP address */ 289 if (write_dsp(chip, address)) { 290 DE_INIT(("install_resident_loader: Failed to write DSP address!\n")); 291 goto irl_error; 292 } 293 /* Write out this block of code to the DSP */ 294 for (i = 0; i < words; i++) { 295 u32 data; 296 297 data = ((u32)code[index] << 16) + code[index + 1]; 298 if (write_dsp(chip, data)) { 299 DE_INIT(("install_resident_loader: Failed to write DSP code\n")); 300 goto irl_error; 301 } 302 index += 2; 303 } 304 305 /* Wait for flag 5 to come up */ 306 for (i = 0; i < 200; i++) { /* Timeout is 50us * 200 = 10ms */ 307 udelay(50); 308 status = get_dsp_register(chip, CHI32_STATUS_REG); 309 if (status & CHI32_STATUS_REG_HF5) 310 break; 311 } 312 313 if (i == 200) { 314 DE_INIT(("Resident loader failed to set HF5\n")); 315 goto irl_error; 316 } 317 318 DE_INIT(("Resident loader successfully installed\n")); 319 free_firmware(fw); 320 return 0; 321 322 irl_error: 323 free_firmware(fw); 324 return -EIO; 325 } 326 327 #endif /* DSP_56361 */ 328 329 330 static int load_dsp(struct echoaudio *chip, u16 *code) 331 { 332 u32 address, data; 333 int index, words, i; 334 335 if (chip->dsp_code == code) { 336 DE_INIT(("DSP is already loaded!\n")); 337 return 0; 338 } 339 chip->bad_board = TRUE; /* Set TRUE until DSP loaded */ 340 chip->dsp_code = NULL; /* Current DSP code not loaded */ 341 chip->asic_loaded = FALSE; /* Loading the DSP code will reset the ASIC */ 342 343 DE_INIT(("load_dsp: Set bad_board to TRUE\n")); 344 345 /* If this board requires a resident loader, install it. */ 346 #ifdef DSP_56361 347 if ((i = install_resident_loader(chip)) < 0) 348 return i; 349 #endif 350 351 /* Send software reset command */ 352 if (send_vector(chip, DSP_VC_RESET) < 0) { 353 DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n")); 354 return -EIO; 355 } 356 /* Delay 10us */ 357 udelay(10); 358 359 /* Wait 10ms for HF3 to indicate that software reset is complete */ 360 for (i = 0; i < 1000; i++) { /* Timeout is 10us * 1000 = 10ms */ 361 if (get_dsp_register(chip, CHI32_STATUS_REG) & 362 CHI32_STATUS_REG_HF3) 363 break; 364 udelay(10); 365 } 366 367 if (i == 1000) { 368 DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n")); 369 return -EIO; 370 } 371 372 /* Set DSP format bits for 24 bit mode now that soft reset is done */ 373 set_dsp_register(chip, CHI32_CONTROL_REG, 374 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900); 375 376 /* Main loader loop */ 377 378 index = code[0]; 379 for (;;) { 380 int block_type, mem_type; 381 382 /* Total Block Size */ 383 index++; 384 385 /* Block Type */ 386 block_type = code[index]; 387 if (block_type == 4) /* We're finished */ 388 break; 389 390 index++; 391 392 /* Memory Type P=0,X=1,Y=2 */ 393 mem_type = code[index++]; 394 395 /* Block Code Size */ 396 words = code[index++]; 397 if (words == 0) /* We're finished */ 398 break; 399 400 /* Start Address */ 401 address = ((u32)code[index] << 16) + code[index + 1]; 402 index += 2; 403 404 if (write_dsp(chip, words) < 0) { 405 DE_INIT(("load_dsp: failed to write number of DSP words\n")); 406 return -EIO; 407 } 408 if (write_dsp(chip, address) < 0) { 409 DE_INIT(("load_dsp: failed to write DSP address\n")); 410 return -EIO; 411 } 412 if (write_dsp(chip, mem_type) < 0) { 413 DE_INIT(("load_dsp: failed to write DSP memory type\n")); 414 return -EIO; 415 } 416 /* Code */ 417 for (i = 0; i < words; i++, index+=2) { 418 data = ((u32)code[index] << 16) + code[index + 1]; 419 if (write_dsp(chip, data) < 0) { 420 DE_INIT(("load_dsp: failed to write DSP data\n")); 421 return -EIO; 422 } 423 } 424 } 425 426 if (write_dsp(chip, 0) < 0) { /* We're done!!! */ 427 DE_INIT(("load_dsp: Failed to write final zero\n")); 428 return -EIO; 429 } 430 udelay(10); 431 432 for (i = 0; i < 5000; i++) { /* Timeout is 100us * 5000 = 500ms */ 433 /* Wait for flag 4 - indicates that the DSP loaded OK */ 434 if (get_dsp_register(chip, CHI32_STATUS_REG) & 435 CHI32_STATUS_REG_HF4) { 436 set_dsp_register(chip, CHI32_CONTROL_REG, 437 get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00); 438 439 if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) { 440 DE_INIT(("load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n")); 441 return -EIO; 442 } 443 444 if (write_dsp(chip, chip->comm_page_phys) < 0) { 445 DE_INIT(("load_dsp: Failed to write comm page address\n")); 446 return -EIO; 447 } 448 449 /* Get the serial number via slave mode. 450 This is triggered by the SET_COMMPAGE_ADDR command. 451 We don't actually use the serial number but we have to 452 get it as part of the DSP init voodoo. */ 453 if (read_sn(chip) < 0) { 454 DE_INIT(("load_dsp: Failed to read serial number\n")); 455 return -EIO; 456 } 457 458 chip->dsp_code = code; /* Show which DSP code loaded */ 459 chip->bad_board = FALSE; /* DSP OK */ 460 DE_INIT(("load_dsp: OK!\n")); 461 return 0; 462 } 463 udelay(100); 464 } 465 466 DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n")); 467 return -EIO; 468 } 469 470 471 472 /* load_firmware takes care of loading the DSP and any ASIC code. */ 473 static int load_firmware(struct echoaudio *chip) 474 { 475 const struct firmware *fw; 476 int box_type, err; 477 478 if (snd_BUG_ON(!chip->dsp_code_to_load || !chip->comm_page)) 479 return -EPERM; 480 481 /* See if the ASIC is present and working - only if the DSP is already loaded */ 482 if (chip->dsp_code) { 483 if ((box_type = check_asic_status(chip)) >= 0) 484 return box_type; 485 /* ASIC check failed; force the DSP to reload */ 486 chip->dsp_code = NULL; 487 } 488 489 err = get_firmware(&fw, chip, chip->dsp_code_to_load); 490 if (err < 0) 491 return err; 492 err = load_dsp(chip, (u16 *)fw->data); 493 free_firmware(fw); 494 if (err < 0) 495 return err; 496 497 if ((box_type = load_asic(chip)) < 0) 498 return box_type; /* error */ 499 500 if ((err = restore_dsp_rettings(chip)) < 0) 501 return err; 502 503 return box_type; 504 } 505 506 507 508 /**************************************************************************** 509 Mixer functions 510 ****************************************************************************/ 511 512 #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \ 513 defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL) 514 515 /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */ 516 static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer) 517 { 518 if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip))) 519 return -EINVAL; 520 521 /* Wait for the handshake (OK even if ASIC is not loaded) */ 522 if (wait_handshake(chip)) 523 return -EIO; 524 525 chip->nominal_level[index] = consumer; 526 527 if (consumer) 528 chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index); 529 else 530 chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index); 531 532 return 0; 533 } 534 535 #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */ 536 537 538 539 /* Set the gain for a single physical output channel (dB). */ 540 static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain) 541 { 542 if (snd_BUG_ON(channel >= num_busses_out(chip))) 543 return -EINVAL; 544 545 if (wait_handshake(chip)) 546 return -EIO; 547 548 /* Save the new value */ 549 chip->output_gain[channel] = gain; 550 chip->comm_page->line_out_level[channel] = gain; 551 return 0; 552 } 553 554 555 556 #ifdef ECHOCARD_HAS_MONITOR 557 /* Set the monitor level from an input bus to an output bus. */ 558 static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input, 559 s8 gain) 560 { 561 if (snd_BUG_ON(output >= num_busses_out(chip) || 562 input >= num_busses_in(chip))) 563 return -EINVAL; 564 565 if (wait_handshake(chip)) 566 return -EIO; 567 568 chip->monitor_gain[output][input] = gain; 569 chip->comm_page->monitors[monitor_index(chip, output, input)] = gain; 570 return 0; 571 } 572 #endif /* ECHOCARD_HAS_MONITOR */ 573 574 575 /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */ 576 static int update_output_line_level(struct echoaudio *chip) 577 { 578 if (wait_handshake(chip)) 579 return -EIO; 580 clear_handshake(chip); 581 return send_vector(chip, DSP_VC_UPDATE_OUTVOL); 582 } 583 584 585 586 /* Tell the DSP to read and update input levels in comm page */ 587 static int update_input_line_level(struct echoaudio *chip) 588 { 589 if (wait_handshake(chip)) 590 return -EIO; 591 clear_handshake(chip); 592 return send_vector(chip, DSP_VC_UPDATE_INGAIN); 593 } 594 595 596 597 /* set_meters_on turns the meters on or off. If meters are turned on, the DSP 598 will write the meter and clock detect values to the comm page at about 30Hz */ 599 static void set_meters_on(struct echoaudio *chip, char on) 600 { 601 if (on && !chip->meters_enabled) { 602 send_vector(chip, DSP_VC_METERS_ON); 603 chip->meters_enabled = 1; 604 } else if (!on && chip->meters_enabled) { 605 send_vector(chip, DSP_VC_METERS_OFF); 606 chip->meters_enabled = 0; 607 memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED, 608 DSP_MAXPIPES); 609 memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED, 610 DSP_MAXPIPES); 611 } 612 } 613 614 615 616 /* Fill out an the given array using the current values in the comm page. 617 Meters are written in the comm page by the DSP in this order: 618 Output busses 619 Input busses 620 Output pipes (vmixer cards only) 621 622 This function assumes there are no more than 16 in/out busses or pipes 623 Meters is an array [3][16][2] of long. */ 624 static void get_audio_meters(struct echoaudio *chip, long *meters) 625 { 626 int i, m, n; 627 628 m = 0; 629 n = 0; 630 for (i = 0; i < num_busses_out(chip); i++, m++) { 631 meters[n++] = chip->comm_page->vu_meter[m]; 632 meters[n++] = chip->comm_page->peak_meter[m]; 633 } 634 for (; n < 32; n++) 635 meters[n] = 0; 636 637 #ifdef ECHOCARD_ECHO3G 638 m = E3G_MAX_OUTPUTS; /* Skip unused meters */ 639 #endif 640 641 for (i = 0; i < num_busses_in(chip); i++, m++) { 642 meters[n++] = chip->comm_page->vu_meter[m]; 643 meters[n++] = chip->comm_page->peak_meter[m]; 644 } 645 for (; n < 64; n++) 646 meters[n] = 0; 647 648 #ifdef ECHOCARD_HAS_VMIXER 649 for (i = 0; i < num_pipes_out(chip); i++, m++) { 650 meters[n++] = chip->comm_page->vu_meter[m]; 651 meters[n++] = chip->comm_page->peak_meter[m]; 652 } 653 #endif 654 for (; n < 96; n++) 655 meters[n] = 0; 656 } 657 658 659 660 static int restore_dsp_rettings(struct echoaudio *chip) 661 { 662 int err; 663 DE_INIT(("restore_dsp_settings\n")); 664 665 if ((err = check_asic_status(chip)) < 0) 666 return err; 667 668 /* @ Gina20/Darla20 only. Should be harmless for other cards. */ 669 chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF; 670 chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF; 671 chip->comm_page->handshake = 0xffffffff; 672 673 if ((err = set_sample_rate(chip, chip->sample_rate)) < 0) 674 return err; 675 676 if (chip->meters_enabled) 677 if (send_vector(chip, DSP_VC_METERS_ON) < 0) 678 return -EIO; 679 680 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK 681 if (set_input_clock(chip, chip->input_clock) < 0) 682 return -EIO; 683 #endif 684 685 #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH 686 if (set_output_clock(chip, chip->output_clock) < 0) 687 return -EIO; 688 #endif 689 690 if (update_output_line_level(chip) < 0) 691 return -EIO; 692 693 if (update_input_line_level(chip) < 0) 694 return -EIO; 695 696 #ifdef ECHOCARD_HAS_VMIXER 697 if (update_vmixer_level(chip) < 0) 698 return -EIO; 699 #endif 700 701 if (wait_handshake(chip) < 0) 702 return -EIO; 703 clear_handshake(chip); 704 705 DE_INIT(("restore_dsp_rettings done\n")); 706 return send_vector(chip, DSP_VC_UPDATE_FLAGS); 707 } 708 709 710 711 /**************************************************************************** 712 Transport functions 713 ****************************************************************************/ 714 715 /* set_audio_format() sets the format of the audio data in host memory for 716 this pipe. Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA 717 but they are here because they are just mono while capturing */ 718 static void set_audio_format(struct echoaudio *chip, u16 pipe_index, 719 const struct audioformat *format) 720 { 721 u16 dsp_format; 722 723 dsp_format = DSP_AUDIOFORM_SS_16LE; 724 725 /* Look for super-interleave (no big-endian and 8 bits) */ 726 if (format->interleave > 2) { 727 switch (format->bits_per_sample) { 728 case 16: 729 dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE; 730 break; 731 case 24: 732 dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE; 733 break; 734 case 32: 735 dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE; 736 break; 737 } 738 dsp_format |= format->interleave; 739 } else if (format->data_are_bigendian) { 740 /* For big-endian data, only 32 bit samples are supported */ 741 switch (format->interleave) { 742 case 1: 743 dsp_format = DSP_AUDIOFORM_MM_32BE; 744 break; 745 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32 746 case 2: 747 dsp_format = DSP_AUDIOFORM_SS_32BE; 748 break; 749 #endif 750 } 751 } else if (format->interleave == 1 && 752 format->bits_per_sample == 32 && !format->mono_to_stereo) { 753 /* 32 bit little-endian mono->mono case */ 754 dsp_format = DSP_AUDIOFORM_MM_32LE; 755 } else { 756 /* Handle the other little-endian formats */ 757 switch (format->bits_per_sample) { 758 case 8: 759 if (format->interleave == 2) 760 dsp_format = DSP_AUDIOFORM_SS_8; 761 else 762 dsp_format = DSP_AUDIOFORM_MS_8; 763 break; 764 default: 765 case 16: 766 if (format->interleave == 2) 767 dsp_format = DSP_AUDIOFORM_SS_16LE; 768 else 769 dsp_format = DSP_AUDIOFORM_MS_16LE; 770 break; 771 case 24: 772 if (format->interleave == 2) 773 dsp_format = DSP_AUDIOFORM_SS_24LE; 774 else 775 dsp_format = DSP_AUDIOFORM_MS_24LE; 776 break; 777 case 32: 778 if (format->interleave == 2) 779 dsp_format = DSP_AUDIOFORM_SS_32LE; 780 else 781 dsp_format = DSP_AUDIOFORM_MS_32LE; 782 break; 783 } 784 } 785 DE_ACT(("set_audio_format[%d] = %x\n", pipe_index, dsp_format)); 786 chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format); 787 } 788 789 790 791 /* start_transport starts transport for a set of pipes. 792 The bits 1 in channel_mask specify what pipes to start. Only the bit of the 793 first channel must be set, regardless its interleave. 794 Same thing for pause_ and stop_ -trasport below. */ 795 static int start_transport(struct echoaudio *chip, u32 channel_mask, 796 u32 cyclic_mask) 797 { 798 DE_ACT(("start_transport %x\n", channel_mask)); 799 800 if (wait_handshake(chip)) 801 return -EIO; 802 803 chip->comm_page->cmd_start |= cpu_to_le32(channel_mask); 804 805 if (chip->comm_page->cmd_start) { 806 clear_handshake(chip); 807 send_vector(chip, DSP_VC_START_TRANSFER); 808 if (wait_handshake(chip)) 809 return -EIO; 810 /* Keep track of which pipes are transporting */ 811 chip->active_mask |= channel_mask; 812 chip->comm_page->cmd_start = 0; 813 return 0; 814 } 815 816 DE_ACT(("start_transport: No pipes to start!\n")); 817 return -EINVAL; 818 } 819 820 821 822 static int pause_transport(struct echoaudio *chip, u32 channel_mask) 823 { 824 DE_ACT(("pause_transport %x\n", channel_mask)); 825 826 if (wait_handshake(chip)) 827 return -EIO; 828 829 chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask); 830 chip->comm_page->cmd_reset = 0; 831 if (chip->comm_page->cmd_stop) { 832 clear_handshake(chip); 833 send_vector(chip, DSP_VC_STOP_TRANSFER); 834 if (wait_handshake(chip)) 835 return -EIO; 836 /* Keep track of which pipes are transporting */ 837 chip->active_mask &= ~channel_mask; 838 chip->comm_page->cmd_stop = 0; 839 chip->comm_page->cmd_reset = 0; 840 return 0; 841 } 842 843 DE_ACT(("pause_transport: No pipes to stop!\n")); 844 return 0; 845 } 846 847 848 849 static int stop_transport(struct echoaudio *chip, u32 channel_mask) 850 { 851 DE_ACT(("stop_transport %x\n", channel_mask)); 852 853 if (wait_handshake(chip)) 854 return -EIO; 855 856 chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask); 857 chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask); 858 if (chip->comm_page->cmd_reset) { 859 clear_handshake(chip); 860 send_vector(chip, DSP_VC_STOP_TRANSFER); 861 if (wait_handshake(chip)) 862 return -EIO; 863 /* Keep track of which pipes are transporting */ 864 chip->active_mask &= ~channel_mask; 865 chip->comm_page->cmd_stop = 0; 866 chip->comm_page->cmd_reset = 0; 867 return 0; 868 } 869 870 DE_ACT(("stop_transport: No pipes to stop!\n")); 871 return 0; 872 } 873 874 875 876 static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index) 877 { 878 return (chip->pipe_alloc_mask & (1 << pipe_index)); 879 } 880 881 882 883 /* Stops everything and turns off the DSP. All pipes should be already 884 stopped and unallocated. */ 885 static int rest_in_peace(struct echoaudio *chip) 886 { 887 DE_ACT(("rest_in_peace() open=%x\n", chip->pipe_alloc_mask)); 888 889 /* Stops all active pipes (just to be sure) */ 890 stop_transport(chip, chip->active_mask); 891 892 set_meters_on(chip, FALSE); 893 894 #ifdef ECHOCARD_HAS_MIDI 895 enable_midi_input(chip, FALSE); 896 #endif 897 898 /* Go to sleep */ 899 if (chip->dsp_code) { 900 /* Make load_firmware do a complete reload */ 901 chip->dsp_code = NULL; 902 /* Put the DSP to sleep */ 903 return send_vector(chip, DSP_VC_GO_COMATOSE); 904 } 905 return 0; 906 } 907 908 909 910 /* Fills the comm page with default values */ 911 static int init_dsp_comm_page(struct echoaudio *chip) 912 { 913 /* Check if the compiler added extra padding inside the structure */ 914 if (offsetof(struct comm_page, midi_output) != 0xbe0) { 915 DE_INIT(("init_dsp_comm_page() - Invalid struct comm_page structure\n")); 916 return -EPERM; 917 } 918 919 /* Init all the basic stuff */ 920 chip->card_name = ECHOCARD_NAME; 921 chip->bad_board = TRUE; /* Set TRUE until DSP loaded */ 922 chip->dsp_code = NULL; /* Current DSP code not loaded */ 923 chip->digital_mode = DIGITAL_MODE_NONE; 924 chip->input_clock = ECHO_CLOCK_INTERNAL; 925 chip->output_clock = ECHO_CLOCK_WORD; 926 chip->asic_loaded = FALSE; 927 memset(chip->comm_page, 0, sizeof(struct comm_page)); 928 929 /* Init the comm page */ 930 chip->comm_page->comm_size = 931 cpu_to_le32(sizeof(struct comm_page)); 932 chip->comm_page->handshake = 0xffffffff; 933 chip->comm_page->midi_out_free_count = 934 cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE); 935 chip->comm_page->sample_rate = cpu_to_le32(44100); 936 chip->sample_rate = 44100; 937 938 /* Set line levels so we don't blast any inputs on startup */ 939 memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE); 940 memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE); 941 942 return 0; 943 } 944 945 946 947 /* This function initializes the several volume controls for busses and pipes. 948 This MUST be called after the DSP is up and running ! */ 949 static int init_line_levels(struct echoaudio *chip) 950 { 951 int st, i, o; 952 953 DE_INIT(("init_line_levels\n")); 954 955 /* Mute output busses */ 956 for (i = 0; i < num_busses_out(chip); i++) 957 if ((st = set_output_gain(chip, i, ECHOGAIN_MUTED))) 958 return st; 959 if ((st = update_output_line_level(chip))) 960 return st; 961 962 #ifdef ECHOCARD_HAS_VMIXER 963 /* Mute the Vmixer */ 964 for (i = 0; i < num_pipes_out(chip); i++) 965 for (o = 0; o < num_busses_out(chip); o++) 966 if ((st = set_vmixer_gain(chip, o, i, ECHOGAIN_MUTED))) 967 return st; 968 if ((st = update_vmixer_level(chip))) 969 return st; 970 #endif /* ECHOCARD_HAS_VMIXER */ 971 972 #ifdef ECHOCARD_HAS_MONITOR 973 /* Mute the monitor mixer */ 974 for (o = 0; o < num_busses_out(chip); o++) 975 for (i = 0; i < num_busses_in(chip); i++) 976 if ((st = set_monitor_gain(chip, o, i, ECHOGAIN_MUTED))) 977 return st; 978 if ((st = update_output_line_level(chip))) 979 return st; 980 #endif /* ECHOCARD_HAS_MONITOR */ 981 982 #ifdef ECHOCARD_HAS_INPUT_GAIN 983 for (i = 0; i < num_busses_in(chip); i++) 984 if ((st = set_input_gain(chip, i, ECHOGAIN_MUTED))) 985 return st; 986 if ((st = update_input_line_level(chip))) 987 return st; 988 #endif /* ECHOCARD_HAS_INPUT_GAIN */ 989 990 return 0; 991 } 992 993 994 995 /* This is low level part of the interrupt handler. 996 It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number 997 of midi data in the input queue. */ 998 static int service_irq(struct echoaudio *chip) 999 { 1000 int st; 1001 1002 /* Read the DSP status register and see if this DSP generated this interrupt */ 1003 if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) { 1004 st = 0; 1005 #ifdef ECHOCARD_HAS_MIDI 1006 /* Get and parse midi data if present */ 1007 if (chip->comm_page->midi_input[0]) /* The count is at index 0 */ 1008 st = midi_service_irq(chip); /* Returns how many midi bytes we received */ 1009 #endif 1010 /* Clear the hardware interrupt */ 1011 chip->comm_page->midi_input[0] = 0; 1012 send_vector(chip, DSP_VC_ACK_INT); 1013 return st; 1014 } 1015 return -1; 1016 } 1017 1018 1019 1020 1021 /****************************************************************************** 1022 Functions for opening and closing pipes 1023 ******************************************************************************/ 1024 1025 /* allocate_pipes is used to reserve audio pipes for your exclusive use. 1026 The call will fail if some pipes are already allocated. */ 1027 static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe, 1028 int pipe_index, int interleave) 1029 { 1030 int i; 1031 u32 channel_mask; 1032 char is_cyclic; 1033 1034 DE_ACT(("allocate_pipes: ch=%d int=%d\n", pipe_index, interleave)); 1035 1036 if (chip->bad_board) 1037 return -EIO; 1038 1039 is_cyclic = 1; /* This driver uses cyclic buffers only */ 1040 1041 for (channel_mask = i = 0; i < interleave; i++) 1042 channel_mask |= 1 << (pipe_index + i); 1043 if (chip->pipe_alloc_mask & channel_mask) { 1044 DE_ACT(("allocate_pipes: channel already open\n")); 1045 return -EAGAIN; 1046 } 1047 1048 chip->comm_page->position[pipe_index] = 0; 1049 chip->pipe_alloc_mask |= channel_mask; 1050 if (is_cyclic) 1051 chip->pipe_cyclic_mask |= channel_mask; 1052 pipe->index = pipe_index; 1053 pipe->interleave = interleave; 1054 pipe->state = PIPE_STATE_STOPPED; 1055 1056 /* The counter register is where the DSP writes the 32 bit DMA 1057 position for a pipe. The DSP is constantly updating this value as 1058 it moves data. The DMA counter is in units of bytes, not samples. */ 1059 pipe->dma_counter = &chip->comm_page->position[pipe_index]; 1060 *pipe->dma_counter = 0; 1061 DE_ACT(("allocate_pipes: ok\n")); 1062 return pipe_index; 1063 } 1064 1065 1066 1067 static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe) 1068 { 1069 u32 channel_mask; 1070 int i; 1071 1072 DE_ACT(("free_pipes: Pipe %d\n", pipe->index)); 1073 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index))) 1074 return -EINVAL; 1075 if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED)) 1076 return -EINVAL; 1077 1078 for (channel_mask = i = 0; i < pipe->interleave; i++) 1079 channel_mask |= 1 << (pipe->index + i); 1080 1081 chip->pipe_alloc_mask &= ~channel_mask; 1082 chip->pipe_cyclic_mask &= ~channel_mask; 1083 return 0; 1084 } 1085 1086 1087 1088 /****************************************************************************** 1089 Functions for managing the scatter-gather list 1090 ******************************************************************************/ 1091 1092 static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe) 1093 { 1094 pipe->sglist_head = 0; 1095 memset(pipe->sgpage.area, 0, PAGE_SIZE); 1096 chip->comm_page->sglist_addr[pipe->index].addr = 1097 cpu_to_le32(pipe->sgpage.addr); 1098 return 0; 1099 } 1100 1101 1102 1103 static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe, 1104 dma_addr_t address, size_t length) 1105 { 1106 int head = pipe->sglist_head; 1107 struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area; 1108 1109 if (head < MAX_SGLIST_ENTRIES - 1) { 1110 list[head].addr = cpu_to_le32(address); 1111 list[head].size = cpu_to_le32(length); 1112 pipe->sglist_head++; 1113 } else { 1114 DE_ACT(("SGlist: too many fragments\n")); 1115 return -ENOMEM; 1116 } 1117 return 0; 1118 } 1119 1120 1121 1122 static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe) 1123 { 1124 return sglist_add_mapping(chip, pipe, 0, 0); 1125 } 1126 1127 1128 1129 static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe) 1130 { 1131 return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0); 1132 } 1133