Back to home page

OSCL-LXR

 
 

    


0001 /****************************************************************************
0002 
0003    Copyright Echo Digital Audio Corporation (c) 1998 - 2004
0004    All rights reserved
0005    www.echoaudio.com
0006 
0007    This file is part of Echo Digital Audio's generic driver library.
0008 
0009    Echo Digital Audio's generic driver library is free software;
0010    you can redistribute it and/or modify it under the terms of
0011    the GNU General Public License as published by the Free Software
0012    Foundation.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program; if not, write to the Free Software
0021    Foundation, Inc., 59 Temple Place - Suite 330, Boston,
0022    MA  02111-1307, USA.
0023 
0024    *************************************************************************
0025 
0026  Translation from C++ and adaptation for use in ALSA-Driver
0027  were made by Giuliano Pochini <pochini@shiny.it>
0028 
0029 ****************************************************************************/
0030 
0031 #if PAGE_SIZE < 4096
0032 #error PAGE_SIZE is < 4k
0033 #endif
0034 
0035 static int restore_dsp_rettings(struct echoaudio *chip);
0036 
0037 
0038 /* Some vector commands involve the DSP reading or writing data to and from the
0039 comm page; if you send one of these commands to the DSP, it will complete the
0040 command and then write a non-zero value to the Handshake field in the
0041 comm page.  This function waits for the handshake to show up. */
0042 static int wait_handshake(struct echoaudio *chip)
0043 {
0044     int i;
0045 
0046     /* Wait up to 20ms for the handshake from the DSP */
0047     for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
0048         /* Look for the handshake value */
0049         barrier();
0050         if (chip->comm_page->handshake) {
0051             return 0;
0052         }
0053         udelay(1);
0054     }
0055 
0056     dev_err(chip->card->dev, "wait_handshake(): Timeout waiting for DSP\n");
0057     return -EBUSY;
0058 }
0059 
0060 
0061 
0062 /* Much of the interaction between the DSP and the driver is done via vector
0063 commands; send_vector writes a vector command to the DSP.  Typically, this
0064 causes the DSP to read or write fields in the comm page.
0065 PCI posting is not required thanks to the handshake logic. */
0066 static int send_vector(struct echoaudio *chip, u32 command)
0067 {
0068     int i;
0069 
0070     wmb();  /* Flush all pending writes before sending the command */
0071 
0072     /* Wait up to 100ms for the "vector busy" bit to be off */
0073     for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
0074         if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
0075               CHI32_VECTOR_BUSY)) {
0076             set_dsp_register(chip, CHI32_VECTOR_REG, command);
0077             /*if (i)  DE_ACT(("send_vector time: %d\n", i));*/
0078             return 0;
0079         }
0080         udelay(1);
0081     }
0082 
0083     dev_err(chip->card->dev, "timeout on send_vector\n");
0084     return -EBUSY;
0085 }
0086 
0087 
0088 
0089 /* write_dsp writes a 32-bit value to the DSP; this is used almost
0090 exclusively for loading the DSP. */
0091 static int write_dsp(struct echoaudio *chip, u32 data)
0092 {
0093     u32 status, i;
0094 
0095     for (i = 0; i < 10000000; i++) {    /* timeout = 10s */
0096         status = get_dsp_register(chip, CHI32_STATUS_REG);
0097         if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
0098             set_dsp_register(chip, CHI32_DATA_REG, data);
0099             wmb();          /* write it immediately */
0100             return 0;
0101         }
0102         udelay(1);
0103         cond_resched();
0104     }
0105 
0106     chip->bad_board = true;     /* Set true until DSP re-loaded */
0107     dev_dbg(chip->card->dev, "write_dsp: Set bad_board to true\n");
0108     return -EIO;
0109 }
0110 
0111 
0112 
0113 /* read_dsp reads a 32-bit value from the DSP; this is used almost
0114 exclusively for loading the DSP and checking the status of the ASIC. */
0115 static int read_dsp(struct echoaudio *chip, u32 *data)
0116 {
0117     u32 status, i;
0118 
0119     for (i = 0; i < READ_DSP_TIMEOUT; i++) {
0120         status = get_dsp_register(chip, CHI32_STATUS_REG);
0121         if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
0122             *data = get_dsp_register(chip, CHI32_DATA_REG);
0123             return 0;
0124         }
0125         udelay(1);
0126         cond_resched();
0127     }
0128 
0129     chip->bad_board = true;     /* Set true until DSP re-loaded */
0130     dev_err(chip->card->dev, "read_dsp: Set bad_board to true\n");
0131     return -EIO;
0132 }
0133 
0134 
0135 
0136 /****************************************************************************
0137     Firmware loading functions
0138  ****************************************************************************/
0139 
0140 /* This function is used to read back the serial number from the DSP;
0141 this is triggered by the SET_COMMPAGE_ADDR command.
0142 Only some early Echogals products have serial numbers in the ROM;
0143 the serial number is not used, but you still need to do this as
0144 part of the DSP load process. */
0145 static int read_sn(struct echoaudio *chip)
0146 {
0147     int i;
0148     u32 sn[6];
0149 
0150     for (i = 0; i < 5; i++) {
0151         if (read_dsp(chip, &sn[i])) {
0152             dev_err(chip->card->dev,
0153                 "Failed to read serial number\n");
0154             return -EIO;
0155         }
0156     }
0157     dev_dbg(chip->card->dev,
0158         "Read serial number %08x %08x %08x %08x %08x\n",
0159          sn[0], sn[1], sn[2], sn[3], sn[4]);
0160     return 0;
0161 }
0162 
0163 
0164 
0165 #ifndef ECHOCARD_HAS_ASIC
0166 /* This card has no ASIC, just return ok */
0167 static inline int check_asic_status(struct echoaudio *chip)
0168 {
0169     chip->asic_loaded = true;
0170     return 0;
0171 }
0172 
0173 #endif /* !ECHOCARD_HAS_ASIC */
0174 
0175 
0176 
0177 #ifdef ECHOCARD_HAS_ASIC
0178 
0179 /* Load ASIC code - done after the DSP is loaded */
0180 static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic)
0181 {
0182     const struct firmware *fw;
0183     int err;
0184     u32 i, size;
0185     u8 *code;
0186 
0187     err = get_firmware(&fw, chip, asic);
0188     if (err < 0) {
0189         dev_warn(chip->card->dev, "Firmware not found !\n");
0190         return err;
0191     }
0192 
0193     code = (u8 *)fw->data;
0194     size = fw->size;
0195 
0196     /* Send the "Here comes the ASIC" command */
0197     if (write_dsp(chip, cmd) < 0)
0198         goto la_error;
0199 
0200     /* Write length of ASIC file in bytes */
0201     if (write_dsp(chip, size) < 0)
0202         goto la_error;
0203 
0204     for (i = 0; i < size; i++) {
0205         if (write_dsp(chip, code[i]) < 0)
0206             goto la_error;
0207     }
0208 
0209     free_firmware(fw, chip);
0210     return 0;
0211 
0212 la_error:
0213     dev_err(chip->card->dev, "failed on write_dsp\n");
0214     free_firmware(fw, chip);
0215     return -EIO;
0216 }
0217 
0218 #endif /* ECHOCARD_HAS_ASIC */
0219 
0220 
0221 
0222 #ifdef DSP_56361
0223 
0224 /* Install the resident loader for 56361 DSPs;  The resident loader is on
0225 the EPROM on the board for 56301 DSP. The resident loader is a tiny little
0226 program that is used to load the real DSP code. */
0227 static int install_resident_loader(struct echoaudio *chip)
0228 {
0229     u32 address;
0230     int index, words, i;
0231     u16 *code;
0232     u32 status;
0233     const struct firmware *fw;
0234 
0235     /* 56361 cards only!  This check is required by the old 56301-based
0236     Mona and Gina24 */
0237     if (chip->device_id != DEVICE_ID_56361)
0238         return 0;
0239 
0240     /* Look to see if the resident loader is present.  If the resident
0241     loader is already installed, host flag 5 will be on. */
0242     status = get_dsp_register(chip, CHI32_STATUS_REG);
0243     if (status & CHI32_STATUS_REG_HF5) {
0244         dev_dbg(chip->card->dev,
0245             "Resident loader already installed; status is 0x%x\n",
0246              status);
0247         return 0;
0248     }
0249 
0250     i = get_firmware(&fw, chip, FW_361_LOADER);
0251     if (i < 0) {
0252         dev_warn(chip->card->dev, "Firmware not found !\n");
0253         return i;
0254     }
0255 
0256     /* The DSP code is an array of 16 bit words.  The array is divided up
0257     into sections.  The first word of each section is the size in words,
0258     followed by the section type.
0259     Since DSP addresses and data are 24 bits wide, they each take up two
0260     16 bit words in the array.
0261     This is a lot like the other loader loop, but it's not a loop, you
0262     don't write the memory type, and you don't write a zero at the end. */
0263 
0264     /* Set DSP format bits for 24 bit mode */
0265     set_dsp_register(chip, CHI32_CONTROL_REG,
0266              get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
0267 
0268     code = (u16 *)fw->data;
0269 
0270     /* Skip the header section; the first word in the array is the size
0271     of the first section, so the first real section of code is pointed
0272     to by Code[0]. */
0273     index = code[0];
0274 
0275     /* Skip the section size, LRS block type, and DSP memory type */
0276     index += 3;
0277 
0278     /* Get the number of DSP words to write */
0279     words = code[index++];
0280 
0281     /* Get the DSP address for this block; 24 bits, so build from two words */
0282     address = ((u32)code[index] << 16) + code[index + 1];
0283     index += 2;
0284 
0285     /* Write the count to the DSP */
0286     if (write_dsp(chip, words)) {
0287         dev_err(chip->card->dev,
0288             "install_resident_loader: Failed to write word count!\n");
0289         goto irl_error;
0290     }
0291     /* Write the DSP address */
0292     if (write_dsp(chip, address)) {
0293         dev_err(chip->card->dev,
0294             "install_resident_loader: Failed to write DSP address!\n");
0295         goto irl_error;
0296     }
0297     /* Write out this block of code to the DSP */
0298     for (i = 0; i < words; i++) {
0299         u32 data;
0300 
0301         data = ((u32)code[index] << 16) + code[index + 1];
0302         if (write_dsp(chip, data)) {
0303             dev_err(chip->card->dev,
0304                 "install_resident_loader: Failed to write DSP code\n");
0305             goto irl_error;
0306         }
0307         index += 2;
0308     }
0309 
0310     /* Wait for flag 5 to come up */
0311     for (i = 0; i < 200; i++) { /* Timeout is 50us * 200 = 10ms */
0312         udelay(50);
0313         status = get_dsp_register(chip, CHI32_STATUS_REG);
0314         if (status & CHI32_STATUS_REG_HF5)
0315             break;
0316     }
0317 
0318     if (i == 200) {
0319         dev_err(chip->card->dev, "Resident loader failed to set HF5\n");
0320         goto irl_error;
0321     }
0322 
0323     dev_dbg(chip->card->dev, "Resident loader successfully installed\n");
0324     free_firmware(fw, chip);
0325     return 0;
0326 
0327 irl_error:
0328     free_firmware(fw, chip);
0329     return -EIO;
0330 }
0331 
0332 #endif /* DSP_56361 */
0333 
0334 
0335 static int load_dsp(struct echoaudio *chip, u16 *code)
0336 {
0337     u32 address, data;
0338     int index, words, i;
0339 
0340     if (chip->dsp_code == code) {
0341         dev_warn(chip->card->dev, "DSP is already loaded!\n");
0342         return 0;
0343     }
0344     chip->bad_board = true;     /* Set true until DSP loaded */
0345     chip->dsp_code = NULL;      /* Current DSP code not loaded */
0346     chip->asic_loaded = false;  /* Loading the DSP code will reset the ASIC */
0347 
0348     dev_dbg(chip->card->dev, "load_dsp: Set bad_board to true\n");
0349 
0350     /* If this board requires a resident loader, install it. */
0351 #ifdef DSP_56361
0352     i = install_resident_loader(chip);
0353     if (i < 0)
0354         return i;
0355 #endif
0356 
0357     /* Send software reset command */
0358     if (send_vector(chip, DSP_VC_RESET) < 0) {
0359         dev_err(chip->card->dev,
0360             "LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n");
0361         return -EIO;
0362     }
0363     /* Delay 10us */
0364     udelay(10);
0365 
0366     /* Wait 10ms for HF3 to indicate that software reset is complete */
0367     for (i = 0; i < 1000; i++) {    /* Timeout is 10us * 1000 = 10ms */
0368         if (get_dsp_register(chip, CHI32_STATUS_REG) &
0369             CHI32_STATUS_REG_HF3)
0370             break;
0371         udelay(10);
0372     }
0373 
0374     if (i == 1000) {
0375         dev_err(chip->card->dev,
0376             "load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n");
0377         return -EIO;
0378     }
0379 
0380     /* Set DSP format bits for 24 bit mode now that soft reset is done */
0381     set_dsp_register(chip, CHI32_CONTROL_REG,
0382              get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
0383 
0384     /* Main loader loop */
0385 
0386     index = code[0];
0387     for (;;) {
0388         int block_type, mem_type;
0389 
0390         /* Total Block Size */
0391         index++;
0392 
0393         /* Block Type */
0394         block_type = code[index];
0395         if (block_type == 4)    /* We're finished */
0396             break;
0397 
0398         index++;
0399 
0400         /* Memory Type  P=0,X=1,Y=2 */
0401         mem_type = code[index++];
0402 
0403         /* Block Code Size */
0404         words = code[index++];
0405         if (words == 0)     /* We're finished */
0406             break;
0407 
0408         /* Start Address */
0409         address = ((u32)code[index] << 16) + code[index + 1];
0410         index += 2;
0411 
0412         if (write_dsp(chip, words) < 0) {
0413             dev_err(chip->card->dev,
0414                 "load_dsp: failed to write number of DSP words\n");
0415             return -EIO;
0416         }
0417         if (write_dsp(chip, address) < 0) {
0418             dev_err(chip->card->dev,
0419                 "load_dsp: failed to write DSP address\n");
0420             return -EIO;
0421         }
0422         if (write_dsp(chip, mem_type) < 0) {
0423             dev_err(chip->card->dev,
0424                 "load_dsp: failed to write DSP memory type\n");
0425             return -EIO;
0426         }
0427         /* Code */
0428         for (i = 0; i < words; i++, index+=2) {
0429             data = ((u32)code[index] << 16) + code[index + 1];
0430             if (write_dsp(chip, data) < 0) {
0431                 dev_err(chip->card->dev,
0432                     "load_dsp: failed to write DSP data\n");
0433                 return -EIO;
0434             }
0435         }
0436     }
0437 
0438     if (write_dsp(chip, 0) < 0) {   /* We're done!!! */
0439         dev_err(chip->card->dev,
0440             "load_dsp: Failed to write final zero\n");
0441         return -EIO;
0442     }
0443     udelay(10);
0444 
0445     for (i = 0; i < 5000; i++) {    /* Timeout is 100us * 5000 = 500ms */
0446         /* Wait for flag 4 - indicates that the DSP loaded OK */
0447         if (get_dsp_register(chip, CHI32_STATUS_REG) &
0448             CHI32_STATUS_REG_HF4) {
0449             set_dsp_register(chip, CHI32_CONTROL_REG,
0450                      get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
0451 
0452             if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
0453                 dev_err(chip->card->dev,
0454                     "load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n");
0455                 return -EIO;
0456             }
0457 
0458             if (write_dsp(chip, chip->comm_page_phys) < 0) {
0459                 dev_err(chip->card->dev,
0460                     "load_dsp: Failed to write comm page address\n");
0461                 return -EIO;
0462             }
0463 
0464             /* Get the serial number via slave mode.
0465             This is triggered by the SET_COMMPAGE_ADDR command.
0466             We don't actually use the serial number but we have to
0467             get it as part of the DSP init voodoo. */
0468             if (read_sn(chip) < 0) {
0469                 dev_err(chip->card->dev,
0470                     "load_dsp: Failed to read serial number\n");
0471                 return -EIO;
0472             }
0473 
0474             chip->dsp_code = code;      /* Show which DSP code loaded */
0475             chip->bad_board = false;    /* DSP OK */
0476             return 0;
0477         }
0478         udelay(100);
0479     }
0480 
0481     dev_err(chip->card->dev,
0482         "load_dsp: DSP load timed out waiting for HF4\n");
0483     return -EIO;
0484 }
0485 
0486 
0487 
0488 /* load_firmware takes care of loading the DSP and any ASIC code. */
0489 static int load_firmware(struct echoaudio *chip)
0490 {
0491     const struct firmware *fw;
0492     int box_type, err;
0493 
0494     if (snd_BUG_ON(!chip->comm_page))
0495         return -EPERM;
0496 
0497     /* See if the ASIC is present and working - only if the DSP is already loaded */
0498     if (chip->dsp_code) {
0499         box_type = check_asic_status(chip);
0500         if (box_type >= 0)
0501             return box_type;
0502         /* ASIC check failed; force the DSP to reload */
0503         chip->dsp_code = NULL;
0504     }
0505 
0506     err = get_firmware(&fw, chip, chip->dsp_code_to_load);
0507     if (err < 0)
0508         return err;
0509     err = load_dsp(chip, (u16 *)fw->data);
0510     free_firmware(fw, chip);
0511     if (err < 0)
0512         return err;
0513 
0514     box_type = load_asic(chip);
0515     if (box_type < 0)
0516         return box_type;    /* error */
0517 
0518     return box_type;
0519 }
0520 
0521 
0522 
0523 /****************************************************************************
0524     Mixer functions
0525  ****************************************************************************/
0526 
0527 #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
0528     defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
0529 
0530 /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
0531 static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
0532 {
0533     if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))
0534         return -EINVAL;
0535 
0536     /* Wait for the handshake (OK even if ASIC is not loaded) */
0537     if (wait_handshake(chip))
0538         return -EIO;
0539 
0540     chip->nominal_level[index] = consumer;
0541 
0542     if (consumer)
0543         chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
0544     else
0545         chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
0546 
0547     return 0;
0548 }
0549 
0550 #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
0551 
0552 
0553 
0554 /* Set the gain for a single physical output channel (dB). */
0555 static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
0556 {
0557     if (snd_BUG_ON(channel >= num_busses_out(chip)))
0558         return -EINVAL;
0559 
0560     if (wait_handshake(chip))
0561         return -EIO;
0562 
0563     /* Save the new value */
0564     chip->output_gain[channel] = gain;
0565     chip->comm_page->line_out_level[channel] = gain;
0566     return 0;
0567 }
0568 
0569 
0570 
0571 #ifdef ECHOCARD_HAS_MONITOR
0572 /* Set the monitor level from an input bus to an output bus. */
0573 static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
0574                 s8 gain)
0575 {
0576     if (snd_BUG_ON(output >= num_busses_out(chip) ||
0577             input >= num_busses_in(chip)))
0578         return -EINVAL;
0579 
0580     if (wait_handshake(chip))
0581         return -EIO;
0582 
0583     chip->monitor_gain[output][input] = gain;
0584     chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
0585     return 0;
0586 }
0587 #endif /* ECHOCARD_HAS_MONITOR */
0588 
0589 
0590 /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
0591 static int update_output_line_level(struct echoaudio *chip)
0592 {
0593     if (wait_handshake(chip))
0594         return -EIO;
0595     clear_handshake(chip);
0596     return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
0597 }
0598 
0599 
0600 
0601 /* Tell the DSP to read and update input levels in comm page */
0602 static int update_input_line_level(struct echoaudio *chip)
0603 {
0604     if (wait_handshake(chip))
0605         return -EIO;
0606     clear_handshake(chip);
0607     return send_vector(chip, DSP_VC_UPDATE_INGAIN);
0608 }
0609 
0610 
0611 
0612 /* set_meters_on turns the meters on or off.  If meters are turned on, the DSP
0613 will write the meter and clock detect values to the comm page at about 30Hz */
0614 static void set_meters_on(struct echoaudio *chip, char on)
0615 {
0616     if (on && !chip->meters_enabled) {
0617         send_vector(chip, DSP_VC_METERS_ON);
0618         chip->meters_enabled = 1;
0619     } else if (!on && chip->meters_enabled) {
0620         send_vector(chip, DSP_VC_METERS_OFF);
0621         chip->meters_enabled = 0;
0622         memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
0623                DSP_MAXPIPES);
0624         memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
0625                DSP_MAXPIPES);
0626     }
0627 }
0628 
0629 
0630 
0631 /* Fill out an the given array using the current values in the comm page.
0632 Meters are written in the comm page by the DSP in this order:
0633  Output busses
0634  Input busses
0635  Output pipes (vmixer cards only)
0636 
0637 This function assumes there are no more than 16 in/out busses or pipes
0638 Meters is an array [3][16][2] of long. */
0639 static void get_audio_meters(struct echoaudio *chip, long *meters)
0640 {
0641     unsigned int i, m, n;
0642 
0643     for (i = 0 ; i < 96; i++)
0644         meters[i] = 0;
0645 
0646     for (m = 0, n = 0, i = 0; i < num_busses_out(chip); i++, m++) {
0647         meters[n++] = chip->comm_page->vu_meter[m];
0648         meters[n++] = chip->comm_page->peak_meter[m];
0649     }
0650 
0651 #ifdef ECHOCARD_ECHO3G
0652     m = E3G_MAX_OUTPUTS;    /* Skip unused meters */
0653 #endif
0654 
0655     for (n = 32, i = 0; i < num_busses_in(chip); i++, m++) {
0656         meters[n++] = chip->comm_page->vu_meter[m];
0657         meters[n++] = chip->comm_page->peak_meter[m];
0658     }
0659 #ifdef ECHOCARD_HAS_VMIXER
0660     for (n = 64, i = 0; i < num_pipes_out(chip); i++, m++) {
0661         meters[n++] = chip->comm_page->vu_meter[m];
0662         meters[n++] = chip->comm_page->peak_meter[m];
0663     }
0664 #endif
0665 }
0666 
0667 
0668 
0669 static int restore_dsp_rettings(struct echoaudio *chip)
0670 {
0671     int i, o, err;
0672 
0673     err = check_asic_status(chip);
0674     if (err < 0)
0675         return err;
0676 
0677     /* Gina20/Darla20 only. Should be harmless for other cards. */
0678     chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
0679     chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
0680     chip->comm_page->handshake = cpu_to_le32(0xffffffff);
0681 
0682     /* Restore output busses */
0683     for (i = 0; i < num_busses_out(chip); i++) {
0684         err = set_output_gain(chip, i, chip->output_gain[i]);
0685         if (err < 0)
0686             return err;
0687     }
0688 
0689 #ifdef ECHOCARD_HAS_VMIXER
0690     for (i = 0; i < num_pipes_out(chip); i++)
0691         for (o = 0; o < num_busses_out(chip); o++) {
0692             err = set_vmixer_gain(chip, o, i,
0693                         chip->vmixer_gain[o][i]);
0694             if (err < 0)
0695                 return err;
0696         }
0697     if (update_vmixer_level(chip) < 0)
0698         return -EIO;
0699 #endif /* ECHOCARD_HAS_VMIXER */
0700 
0701 #ifdef ECHOCARD_HAS_MONITOR
0702     for (o = 0; o < num_busses_out(chip); o++)
0703         for (i = 0; i < num_busses_in(chip); i++) {
0704             err = set_monitor_gain(chip, o, i,
0705                         chip->monitor_gain[o][i]);
0706             if (err < 0)
0707                 return err;
0708         }
0709 #endif /* ECHOCARD_HAS_MONITOR */
0710 
0711 #ifdef ECHOCARD_HAS_INPUT_GAIN
0712     for (i = 0; i < num_busses_in(chip); i++) {
0713         err = set_input_gain(chip, i, chip->input_gain[i]);
0714         if (err < 0)
0715             return err;
0716     }
0717 #endif /* ECHOCARD_HAS_INPUT_GAIN */
0718 
0719     err = update_output_line_level(chip);
0720     if (err < 0)
0721         return err;
0722 
0723     err = update_input_line_level(chip);
0724     if (err < 0)
0725         return err;
0726 
0727     err = set_sample_rate(chip, chip->sample_rate);
0728     if (err < 0)
0729         return err;
0730 
0731     if (chip->meters_enabled) {
0732         err = send_vector(chip, DSP_VC_METERS_ON);
0733         if (err < 0)
0734             return err;
0735     }
0736 
0737 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
0738     if (set_digital_mode(chip, chip->digital_mode) < 0)
0739         return -EIO;
0740 #endif
0741 
0742 #ifdef ECHOCARD_HAS_DIGITAL_IO
0743     if (set_professional_spdif(chip, chip->professional_spdif) < 0)
0744         return -EIO;
0745 #endif
0746 
0747 #ifdef ECHOCARD_HAS_PHANTOM_POWER
0748     if (set_phantom_power(chip, chip->phantom_power) < 0)
0749         return -EIO;
0750 #endif
0751 
0752 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
0753     /* set_input_clock() also restores automute setting */
0754     if (set_input_clock(chip, chip->input_clock) < 0)
0755         return -EIO;
0756 #endif
0757 
0758 #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
0759     if (set_output_clock(chip, chip->output_clock) < 0)
0760         return -EIO;
0761 #endif
0762 
0763     if (wait_handshake(chip) < 0)
0764         return -EIO;
0765     clear_handshake(chip);
0766     if (send_vector(chip, DSP_VC_UPDATE_FLAGS) < 0)
0767         return -EIO;
0768 
0769     return 0;
0770 }
0771 
0772 
0773 
0774 /****************************************************************************
0775     Transport functions
0776  ****************************************************************************/
0777 
0778 /* set_audio_format() sets the format of the audio data in host memory for
0779 this pipe.  Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
0780 but they are here because they are just mono while capturing */
0781 static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
0782                  const struct audioformat *format)
0783 {
0784     u16 dsp_format;
0785 
0786     dsp_format = DSP_AUDIOFORM_SS_16LE;
0787 
0788     /* Look for super-interleave (no big-endian and 8 bits) */
0789     if (format->interleave > 2) {
0790         switch (format->bits_per_sample) {
0791         case 16:
0792             dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
0793             break;
0794         case 24:
0795             dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
0796             break;
0797         case 32:
0798             dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
0799             break;
0800         }
0801         dsp_format |= format->interleave;
0802     } else if (format->data_are_bigendian) {
0803         /* For big-endian data, only 32 bit samples are supported */
0804         switch (format->interleave) {
0805         case 1:
0806             dsp_format = DSP_AUDIOFORM_MM_32BE;
0807             break;
0808 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
0809         case 2:
0810             dsp_format = DSP_AUDIOFORM_SS_32BE;
0811             break;
0812 #endif
0813         }
0814     } else if (format->interleave == 1 &&
0815            format->bits_per_sample == 32 && !format->mono_to_stereo) {
0816         /* 32 bit little-endian mono->mono case */
0817         dsp_format = DSP_AUDIOFORM_MM_32LE;
0818     } else {
0819         /* Handle the other little-endian formats */
0820         switch (format->bits_per_sample) {
0821         case 8:
0822             if (format->interleave == 2)
0823                 dsp_format = DSP_AUDIOFORM_SS_8;
0824             else
0825                 dsp_format = DSP_AUDIOFORM_MS_8;
0826             break;
0827         default:
0828         case 16:
0829             if (format->interleave == 2)
0830                 dsp_format = DSP_AUDIOFORM_SS_16LE;
0831             else
0832                 dsp_format = DSP_AUDIOFORM_MS_16LE;
0833             break;
0834         case 24:
0835             if (format->interleave == 2)
0836                 dsp_format = DSP_AUDIOFORM_SS_24LE;
0837             else
0838                 dsp_format = DSP_AUDIOFORM_MS_24LE;
0839             break;
0840         case 32:
0841             if (format->interleave == 2)
0842                 dsp_format = DSP_AUDIOFORM_SS_32LE;
0843             else
0844                 dsp_format = DSP_AUDIOFORM_MS_32LE;
0845             break;
0846         }
0847     }
0848     dev_dbg(chip->card->dev,
0849          "set_audio_format[%d] = %x\n", pipe_index, dsp_format);
0850     chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
0851 }
0852 
0853 
0854 
0855 /* start_transport starts transport for a set of pipes.
0856 The bits 1 in channel_mask specify what pipes to start. Only the bit of the
0857 first channel must be set, regardless its interleave.
0858 Same thing for pause_ and stop_ -trasport below. */
0859 static int start_transport(struct echoaudio *chip, u32 channel_mask,
0860                u32 cyclic_mask)
0861 {
0862 
0863     if (wait_handshake(chip))
0864         return -EIO;
0865 
0866     chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
0867 
0868     if (chip->comm_page->cmd_start) {
0869         clear_handshake(chip);
0870         send_vector(chip, DSP_VC_START_TRANSFER);
0871         if (wait_handshake(chip))
0872             return -EIO;
0873         /* Keep track of which pipes are transporting */
0874         chip->active_mask |= channel_mask;
0875         chip->comm_page->cmd_start = 0;
0876         return 0;
0877     }
0878 
0879     dev_err(chip->card->dev, "start_transport: No pipes to start!\n");
0880     return -EINVAL;
0881 }
0882 
0883 
0884 
0885 static int pause_transport(struct echoaudio *chip, u32 channel_mask)
0886 {
0887 
0888     if (wait_handshake(chip))
0889         return -EIO;
0890 
0891     chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
0892     chip->comm_page->cmd_reset = 0;
0893     if (chip->comm_page->cmd_stop) {
0894         clear_handshake(chip);
0895         send_vector(chip, DSP_VC_STOP_TRANSFER);
0896         if (wait_handshake(chip))
0897             return -EIO;
0898         /* Keep track of which pipes are transporting */
0899         chip->active_mask &= ~channel_mask;
0900         chip->comm_page->cmd_stop = 0;
0901         chip->comm_page->cmd_reset = 0;
0902         return 0;
0903     }
0904 
0905     dev_dbg(chip->card->dev, "pause_transport: No pipes to stop!\n");
0906     return 0;
0907 }
0908 
0909 
0910 
0911 static int stop_transport(struct echoaudio *chip, u32 channel_mask)
0912 {
0913 
0914     if (wait_handshake(chip))
0915         return -EIO;
0916 
0917     chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
0918     chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
0919     if (chip->comm_page->cmd_reset) {
0920         clear_handshake(chip);
0921         send_vector(chip, DSP_VC_STOP_TRANSFER);
0922         if (wait_handshake(chip))
0923             return -EIO;
0924         /* Keep track of which pipes are transporting */
0925         chip->active_mask &= ~channel_mask;
0926         chip->comm_page->cmd_stop = 0;
0927         chip->comm_page->cmd_reset = 0;
0928         return 0;
0929     }
0930 
0931     dev_dbg(chip->card->dev, "stop_transport: No pipes to stop!\n");
0932     return 0;
0933 }
0934 
0935 
0936 
0937 static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
0938 {
0939     return (chip->pipe_alloc_mask & (1 << pipe_index));
0940 }
0941 
0942 
0943 
0944 /* Stops everything and turns off the DSP. All pipes should be already
0945 stopped and unallocated. */
0946 static int rest_in_peace(struct echoaudio *chip)
0947 {
0948 
0949     /* Stops all active pipes (just to be sure) */
0950     stop_transport(chip, chip->active_mask);
0951 
0952     set_meters_on(chip, false);
0953 
0954 #ifdef ECHOCARD_HAS_MIDI
0955     enable_midi_input(chip, false);
0956 #endif
0957 
0958     /* Go to sleep */
0959     if (chip->dsp_code) {
0960         /* Make load_firmware do a complete reload */
0961         chip->dsp_code = NULL;
0962         /* Put the DSP to sleep */
0963         return send_vector(chip, DSP_VC_GO_COMATOSE);
0964     }
0965     return 0;
0966 }
0967 
0968 
0969 
0970 /* Fills the comm page with default values */
0971 static int init_dsp_comm_page(struct echoaudio *chip)
0972 {
0973     /* Check if the compiler added extra padding inside the structure */
0974     if (offsetof(struct comm_page, midi_output) != 0xbe0) {
0975         dev_err(chip->card->dev,
0976             "init_dsp_comm_page() - Invalid struct comm_page structure\n");
0977         return -EPERM;
0978     }
0979 
0980     /* Init all the basic stuff */
0981     chip->card_name = ECHOCARD_NAME;
0982     chip->bad_board = true; /* Set true until DSP loaded */
0983     chip->dsp_code = NULL;  /* Current DSP code not loaded */
0984     chip->asic_loaded = false;
0985     memset(chip->comm_page, 0, sizeof(struct comm_page));
0986 
0987     /* Init the comm page */
0988     chip->comm_page->comm_size =
0989         cpu_to_le32(sizeof(struct comm_page));
0990     chip->comm_page->handshake = cpu_to_le32(0xffffffff);
0991     chip->comm_page->midi_out_free_count =
0992         cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
0993     chip->comm_page->sample_rate = cpu_to_le32(44100);
0994 
0995     /* Set line levels so we don't blast any inputs on startup */
0996     memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
0997     memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
0998 
0999     return 0;
1000 }
1001 
1002 
1003 
1004 /* This function initializes the chip structure with default values, ie. all
1005  * muted and internal clock source. Then it copies the settings to the DSP.
1006  * This MUST be called after the DSP is up and running !
1007  */
1008 static int init_line_levels(struct echoaudio *chip)
1009 {
1010     memset(chip->output_gain, ECHOGAIN_MUTED, sizeof(chip->output_gain));
1011     memset(chip->input_gain, ECHOGAIN_MUTED, sizeof(chip->input_gain));
1012     memset(chip->monitor_gain, ECHOGAIN_MUTED, sizeof(chip->monitor_gain));
1013     memset(chip->vmixer_gain, ECHOGAIN_MUTED, sizeof(chip->vmixer_gain));
1014     chip->input_clock = ECHO_CLOCK_INTERNAL;
1015     chip->output_clock = ECHO_CLOCK_WORD;
1016     chip->sample_rate = 44100;
1017     return restore_dsp_rettings(chip);
1018 }
1019 
1020 
1021 
1022 /* This is low level part of the interrupt handler.
1023 It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1024 of midi data in the input queue. */
1025 static int service_irq(struct echoaudio *chip)
1026 {
1027     int st;
1028 
1029     /* Read the DSP status register and see if this DSP generated this interrupt */
1030     if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
1031         st = 0;
1032 #ifdef ECHOCARD_HAS_MIDI
1033         /* Get and parse midi data if present */
1034         if (chip->comm_page->midi_input[0]) /* The count is at index 0 */
1035             st = midi_service_irq(chip);    /* Returns how many midi bytes we received */
1036 #endif
1037         /* Clear the hardware interrupt */
1038         chip->comm_page->midi_input[0] = 0;
1039         send_vector(chip, DSP_VC_ACK_INT);
1040         return st;
1041     }
1042     return -1;
1043 }
1044 
1045 
1046 
1047 
1048 /******************************************************************************
1049     Functions for opening and closing pipes
1050  ******************************************************************************/
1051 
1052 /* allocate_pipes is used to reserve audio pipes for your exclusive use.
1053 The call will fail if some pipes are already allocated. */
1054 static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
1055               int pipe_index, int interleave)
1056 {
1057     int i;
1058     u32 channel_mask;
1059 
1060     dev_dbg(chip->card->dev,
1061         "allocate_pipes: ch=%d int=%d\n", pipe_index, interleave);
1062 
1063     if (chip->bad_board)
1064         return -EIO;
1065 
1066     for (channel_mask = i = 0; i < interleave; i++)
1067         channel_mask |= 1 << (pipe_index + i);
1068     if (chip->pipe_alloc_mask & channel_mask) {
1069         dev_err(chip->card->dev,
1070             "allocate_pipes: channel already open\n");
1071         return -EAGAIN;
1072     }
1073 
1074     chip->comm_page->position[pipe_index] = 0;
1075     chip->pipe_alloc_mask |= channel_mask;
1076     /* This driver uses cyclic buffers only */
1077     chip->pipe_cyclic_mask |= channel_mask;
1078     pipe->index = pipe_index;
1079     pipe->interleave = interleave;
1080     pipe->state = PIPE_STATE_STOPPED;
1081 
1082     /* The counter register is where the DSP writes the 32 bit DMA
1083     position for a pipe.  The DSP is constantly updating this value as
1084     it moves data. The DMA counter is in units of bytes, not samples. */
1085     pipe->dma_counter = (__le32 *)&chip->comm_page->position[pipe_index];
1086     *pipe->dma_counter = 0;
1087     return pipe_index;
1088 }
1089 
1090 
1091 
1092 static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
1093 {
1094     u32 channel_mask;
1095     int i;
1096 
1097     if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))
1098         return -EINVAL;
1099     if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))
1100         return -EINVAL;
1101 
1102     for (channel_mask = i = 0; i < pipe->interleave; i++)
1103         channel_mask |= 1 << (pipe->index + i);
1104 
1105     chip->pipe_alloc_mask &= ~channel_mask;
1106     chip->pipe_cyclic_mask &= ~channel_mask;
1107     return 0;
1108 }
1109 
1110 
1111 
1112 /******************************************************************************
1113     Functions for managing the scatter-gather list
1114 ******************************************************************************/
1115 
1116 static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
1117 {
1118     pipe->sglist_head = 0;
1119     memset(pipe->sgpage.area, 0, PAGE_SIZE);
1120     chip->comm_page->sglist_addr[pipe->index].addr =
1121         cpu_to_le32(pipe->sgpage.addr);
1122     return 0;
1123 }
1124 
1125 
1126 
1127 static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
1128                 dma_addr_t address, size_t length)
1129 {
1130     int head = pipe->sglist_head;
1131     struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
1132 
1133     if (head < MAX_SGLIST_ENTRIES - 1) {
1134         list[head].addr = cpu_to_le32(address);
1135         list[head].size = cpu_to_le32(length);
1136         pipe->sglist_head++;
1137     } else {
1138         dev_err(chip->card->dev, "SGlist: too many fragments\n");
1139         return -ENOMEM;
1140     }
1141     return 0;
1142 }
1143 
1144 
1145 
1146 static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
1147 {
1148     return sglist_add_mapping(chip, pipe, 0, 0);
1149 }
1150 
1151 
1152 
1153 static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
1154 {
1155     return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
1156 }