Update of /cvsroot/alsa/alsa-driver/drivers/vx
In directory sc8-pr-cvs1:/tmp/cvs-serv4339/drivers/vx

Added Files:
        .cvsignore Makefile vx_cmd.c vx_cmd.h vx_core.c vx_mixer.c 
        vx_pcm.c vx_uer.c 
Log Message:
[Patch: vx-core-in-driver.dif]

- moved VXpocket/VX222 core part into drivers/vx directory.
- the common header vx_core.h in include directory.
- changed the vx-core entry using ops table.
- added vxp440.conf.



--- NEW FILE: .cvsignore ---
.depend

--- NEW FILE: Makefile ---
TOPDIR = ../..

include $(TOPDIR)/toplevel.config
include $(TOPDIR)/Makefile.conf

#
#
#

TOPDIR = $(MAINSRCDIR)

export-objs := vx_core.o

snd-vx-lib-objs := vx_core.o vx_pcm.o vx_mixer.o vx_cmd.o vx_uer.o

obj-$(CONFIG_SND_VXPOCKET) += snd-vx-lib.o
obj-$(CONFIG_SND_VXP440) += snd-vx-lib.o
obj-$(CONFIG_SND_VX222) += snd-vx-lib.o

include $(TOPDIR)/Rules.make


--- NEW FILE: vx_cmd.c ---
/* to be in alsa-driver-specfici code */
#define __NO_VERSION__

/*
 * Driver for Digigram VX soundcards
 *
 * DSP commands
 *
 * Copyright (c) 2002 by Takashi Iwai <[EMAIL PROTECTED]>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <sound/driver.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include "vx_core.h"
#include "vx_cmd.h"

/*
 * Array of DSP commands
 */
struct vx_cmd_info vx_dsp_cmds[] = {
[CMD_VERSION] =                 { 0x010000, 2, RMH_SSIZE_FIXED, 1 },
[CMD_SUPPORTED] =               { 0x020000, 1, RMH_SSIZE_FIXED, 2 },
[CMD_TEST_IT] =                 { 0x040000, 1, RMH_SSIZE_FIXED, 1 },
[CMD_SEND_IRQA] =               { 0x070001, 1, RMH_SSIZE_FIXED, 0 },
[CMD_IBL] =                     { 0x080000, 1, RMH_SSIZE_FIXED, 4 },
[CMD_ASYNC] =                   { 0x0A0000, 1, RMH_SSIZE_ARG, 0 },
[CMD_RES_PIPE] =                { 0x400000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_FREE_PIPE] =               { 0x410000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_CONF_PIPE] =               { 0x42A101, 2, RMH_SSIZE_FIXED, 0 },
[CMD_ABORT_CONF_PIPE] =         { 0x42A100, 2, RMH_SSIZE_FIXED, 0 },
[CMD_PARAM_OUTPUT_PIPE] =       { 0x43A000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_STOP_PIPE] =               { 0x470004, 1, RMH_SSIZE_FIXED, 0 },
[CMD_PIPE_STATE] =              { 0x480000, 1, RMH_SSIZE_FIXED, 1 },
[CMD_PIPE_SPL_COUNT] =          { 0x49A000, 2, RMH_SSIZE_FIXED, 2 },
[CMD_CAN_START_PIPE] =          { 0x4b0000, 1, RMH_SSIZE_FIXED, 1 },
[CMD_SIZE_HBUFFER] =            { 0x4C0000, 1, RMH_SSIZE_FIXED, 1 },
[CMD_START_STREAM] =            { 0x80A000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_START_ONE_STREAM] =        { 0x800000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_PAUSE_STREAM] =            { 0x81A000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_PAUSE_ONE_STREAM] =        { 0x810000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_STREAM_OUT_LEVEL_ADJUST] = { 0x828000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_STOP_STREAM] =             { 0x830000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_FORMAT_STREAM_OUT] =       { 0x868000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_FORMAT_STREAM_IN] =        { 0x878800, 1, RMH_SSIZE_FIXED, 0 },
[CMD_GET_STREAM_STATE] =        { 0x890001, 2, RMH_SSIZE_FIXED, 1 },
[CMD_DROP_BYTES_AWAY] =         { 0x8A8000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_GET_REMAINING_BYTES] =     { 0x8D0800, 1, RMH_SSIZE_FIXED, 2 },
[CMD_CONNECT_AUDIO] =           { 0xC10000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_AUDIO_LEVEL_ADJUST] =      { 0xC2A000, 3, RMH_SSIZE_FIXED, 0 },
[CMD_AUDIO_VU_PIC_METER] =      { 0xC3A003, 2, RMH_SSIZE_FIXED, 1 },
[CMD_GET_AUDIO_LEVELS] =        { 0xC4A000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_GET_NOTIFY_EVENT] =        { 0x4D0000, 1, RMH_SSIZE_ARG, 0 },
[CMD_INFO_NOTIFIED] =           { 0x0B0000, 1, RMH_SSIZE_FIXED, 2 },
[CMD_ACCESS_IO_FCT] =           { 0x098000, 1, RMH_SSIZE_ARG, 0 },
[CMD_STATUS_R_BUFFERS] =        { 0x440000, 1, RMH_SSIZE_ARG, 0 },
[CMD_UPDATE_R_BUFFERS] =        { 0x848000, 4, RMH_SSIZE_FIXED, 0 },
[CMD_LOAD_EFFECT_CONTEXT] =     { 0x0c8000, 3, RMH_SSIZE_FIXED, 1 },
[CMD_EFFECT_ONE_PIPE] =         { 0x458000, 0, RMH_SSIZE_FIXED, 0 },
[CMD_MODIFY_CLOCK] =            { 0x0d0000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_STREAM1_OUT_SET_N_LEVELS] ={ 0x858000, 3, RMH_SSIZE_FIXED, 0 },
[CMD_PURGE_STREAM_DCMDS] =      { 0x8b8000, 3, RMH_SSIZE_FIXED, 0 },
[CMD_NOTIFY_PIPE_TIME] =        { 0x4e0000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_LOAD_EFFECT_CONTEXT_PACKET] = { 0x0c8000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_RELIC_R_BUFFER] =          { 0x8e0800, 1, RMH_SSIZE_FIXED, 1 },
[CMD_RESYNC_AUDIO_INPUTS] =     { 0x0e0000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_NOTIFY_STREAM_TIME] =      { 0x8f0000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_STREAM_SAMPLE_COUNT] =     { 0x900000, 1, RMH_SSIZE_FIXED, 2 },
[CMD_CONFIG_TIME_CODE] =        { 0x050000, 2, RMH_SSIZE_FIXED, 0 },
[CMD_GET_TIME_CODE] =           { 0x060000, 1, RMH_SSIZE_FIXED, 5 },
[CMD_MANAGE_SIGNAL] =           { 0x0f0000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_PARAMETER_STREAM_OUT] =    { 0x91A000, 3, RMH_SSIZE_FIXED, 0 },
[CMD_READ_BOARD_FREQ] =         { 0x030000, 1, RMH_SSIZE_FIXED, 2 },
[CMD_GET_STREAM_LEVELS] =       { 0x8c0000, 1, RMH_SSIZE_FIXED, 3 },
[CMD_PURGE_PIPE_DCMDS] =        { 0x4f8000, 3, RMH_SSIZE_FIXED, 0 },
// [CMD_SET_STREAM_OUT_EFFECTS] =       { 0x888000, 34, RMH_SSIZE_FIXED, 0 },
// [CMD_GET_STREAM_OUT_EFFECTS] =       { 0x928000, 2, RMH_SSIZE_FIXED, 32 },
[CMD_CONNECT_MONITORING] =      { 0xC00000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_STREAM2_OUT_SET_N_LEVELS] = { 0x938000, 3, RMH_SSIZE_FIXED, 0 },
[CMD_CANCEL_R_BUFFERS] =        { 0x948000, 4, RMH_SSIZE_FIXED, 0 },
[CMD_NOTIFY_END_OF_BUFFER] =    { 0x950000, 1, RMH_SSIZE_FIXED, 0 },
[CMD_GET_STREAM_VU_METER] =     { 0x95A000, 2, RMH_SSIZE_ARG, 0 },
};

/**
 * vx_init_rmh - initialize the RMH instance
 * @rmh: the rmh pointer to be initialized
 * @cmd: the rmh command to be set
 */
void vx_init_rmh(struct vx_rmh *rmh, unsigned int cmd)
{
        snd_assert(cmd < CMD_LAST_INDEX, return);
        rmh->LgCmd = vx_dsp_cmds[cmd].length;
        rmh->LgStat = vx_dsp_cmds[cmd].st_length;
        rmh->DspStat = vx_dsp_cmds[cmd].st_type;
        rmh->Cmd[0] = vx_dsp_cmds[cmd].opcode;
}


--- NEW FILE: vx_cmd.h ---
/*
 * Driver for Digigram VX soundcards
 *
 * Definitions of DSP commands
 *
 * Copyright (c) 2002 by Takashi Iwai <[EMAIL PROTECTED]>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#ifndef __VX_CMD_H
#define __VX_CMD_H

enum {
        CMD_VERSION,
        CMD_SUPPORTED,
        CMD_TEST_IT,
        CMD_SEND_IRQA,
        CMD_IBL,
        CMD_ASYNC,
        CMD_RES_PIPE,
        CMD_FREE_PIPE,
        CMD_CONF_PIPE,
        CMD_ABORT_CONF_PIPE,
        CMD_PARAM_OUTPUT_PIPE,
        CMD_STOP_PIPE,
        CMD_PIPE_STATE,
        CMD_PIPE_SPL_COUNT,
        CMD_CAN_START_PIPE,
        CMD_SIZE_HBUFFER,
        CMD_START_STREAM,
        CMD_START_ONE_STREAM,
        CMD_PAUSE_STREAM,
        CMD_PAUSE_ONE_STREAM,
        CMD_STREAM_OUT_LEVEL_ADJUST,
        CMD_STOP_STREAM,
        CMD_FORMAT_STREAM_OUT,
        CMD_FORMAT_STREAM_IN,
        CMD_GET_STREAM_STATE,
        CMD_DROP_BYTES_AWAY,
        CMD_GET_REMAINING_BYTES,
        CMD_CONNECT_AUDIO,
        CMD_AUDIO_LEVEL_ADJUST,
        CMD_AUDIO_VU_PIC_METER,
        CMD_GET_AUDIO_LEVELS,
        CMD_GET_NOTIFY_EVENT,
        CMD_INFO_NOTIFIED,
        CMD_ACCESS_IO_FCT,
        CMD_STATUS_R_BUFFERS,
        CMD_UPDATE_R_BUFFERS,
        CMD_LOAD_EFFECT_CONTEXT,
        CMD_EFFECT_ONE_PIPE,
        CMD_MODIFY_CLOCK,
        CMD_STREAM1_OUT_SET_N_LEVELS,
        CMD_PURGE_STREAM_DCMDS,
        CMD_NOTIFY_PIPE_TIME,
        CMD_LOAD_EFFECT_CONTEXT_PACKET,
        CMD_RELIC_R_BUFFER,
        CMD_RESYNC_AUDIO_INPUTS,
        CMD_NOTIFY_STREAM_TIME,
        CMD_STREAM_SAMPLE_COUNT,
        CMD_CONFIG_TIME_CODE,
        CMD_GET_TIME_CODE,
        CMD_MANAGE_SIGNAL,
        CMD_PARAMETER_STREAM_OUT,
        CMD_READ_BOARD_FREQ,
        CMD_GET_STREAM_LEVELS,
        CMD_PURGE_PIPE_DCMDS,
        // CMD_SET_STREAM_OUT_EFFECTS,
        // CMD_GET_STREAM_OUT_EFFECTS,
        CMD_CONNECT_MONITORING,
        CMD_STREAM2_OUT_SET_N_LEVELS,
        CMD_CANCEL_R_BUFFERS,
        CMD_NOTIFY_END_OF_BUFFER,
        CMD_GET_STREAM_VU_METER,
        CMD_LAST_INDEX
};

struct vx_cmd_info {
        unsigned int opcode;    /* command word */
        int length;             /* command length (in words) */
        int st_type;            /* status type (RMH_SSIZE_XXX) */
        int st_length;          /* fixed length */
};

/* Family and code op of some DSP requests. */
#define CODE_OP_PIPE_TIME                       0x004e0000
#define CODE_OP_START_STREAM                    0x00800000
#define CODE_OP_PAUSE_STREAM                    0x00810000
#define CODE_OP_OUT_STREAM_LEVEL                0x00820000
#define CODE_OP_UPDATE_R_BUFFERS                0x00840000
#define CODE_OP_OUT_STREAM1_LEVEL_CURVE         0x00850000
#define CODE_OP_OUT_STREAM2_LEVEL_CURVE         0x00930000
#define CODE_OP_OUT_STREAM_FORMAT               0x00860000
#define CODE_OP_STREAM_TIME                     0x008f0000
#define CODE_OP_OUT_STREAM_EXTRAPARAMETER       0x00910000
#define CODE_OP_OUT_AUDIO_LEVEL                 0x00c20000

#define NOTIFY_LAST_COMMAND     0x00400000

/* Values for a user delay */
#define DC_DIFFERED_DELAY       (1<<BIT_DIFFERED_COMMAND)
#define DC_NOTIFY_DELAY         (1<<BIT_NOTIFIED_COMMAND)
#define DC_HBUFFER_DELAY        (1<<BIT_TIME_RELATIVE_TO_BUFFER)
#define DC_MULTIPLE_DELAY       (1<<BIT_RESERVED)
#define DC_STREAM_TIME_DELAY    (1<<BIT_STREAM_TIME)
#define DC_CANCELLED_DELAY      (1<<BIT_CANCELLED_COMMAND)

/* Values for tiDelayed field in TIME_INFO structure,
 * and for pbPause field in PLAY_BUFFER_INFO structure
 */
#define BIT_DIFFERED_COMMAND                0
#define BIT_NOTIFIED_COMMAND                1
#define BIT_TIME_RELATIVE_TO_BUFFER         2
#define BIT_RESERVED                        3
#define BIT_STREAM_TIME                     4
#define BIT_CANCELLED_COMMAND               5

/* Access to the "Size" field of the response of the CMD_GET_NOTIFY_EVENT request. */
#define GET_NOTIFY_EVENT_SIZE_FIELD_MASK    0x000000ff

/* DSP commands general masks */
#define OPCODE_MASK                 0x00ff0000
#define DSP_DIFFERED_COMMAND_MASK   0x0000C000

/* Notifications (NOTIFY_INFO) */
#define ALL_CMDS_NOTIFIED                   0x0000  // reserved
#define START_STREAM_NOTIFIED               0x0001
#define PAUSE_STREAM_NOTIFIED               0x0002
#define OUT_STREAM_LEVEL_NOTIFIED           0x0003
#define OUT_STREAM_PARAMETER_NOTIFIED       0x0004  // left for backward compatibility
#define OUT_STREAM_FORMAT_NOTIFIED          0x0004
#define PIPE_TIME_NOTIFIED                  0x0005
#define OUT_AUDIO_LEVEL_NOTIFIED            0x0006
#define OUT_STREAM_LEVEL_CURVE_NOTIFIED     0x0007
#define STREAM_TIME_NOTIFIED                0x0008
#define OUT_STREAM_EXTRAPARAMETER_NOTIFIED  0x0009
#define UNKNOWN_COMMAND_NOTIFIED            0xffff

/* Output pipe parameters setting */
#define MASK_VALID_PIPE_MPEG_PARAM      0x000040
#define MASK_VALID_PIPE_BACKWARD_PARAM  0x000020
#define MASK_SET_PIPE_MPEG_PARAM        0x000002
#define MASK_SET_PIPE_BACKWARD_PARAM    0x000001

#define MASK_DSP_WORD           0x00FFFFFF
#define MASK_ALL_STREAM         0x00FFFFFF
#define MASK_DSP_WORD_LEVEL     0x000001FF
#define MASK_FIRST_FIELD        0x0000001F
#define FIELD_SIZE              5

#define COMMAND_RECORD_MASK     0x000800

/* PipeManagement definition bits (PIPE_DECL_INFO) */
#define P_UNDERRUN_SKIP_SOUND_MASK                              0x01
#define P_PREPARE_FOR_MPEG3_MASK                                0x02
#define P_DO_NOT_RESET_ANALOG_LEVELS                    0x04
#define P_ALLOW_UNDER_ALLOCATION_MASK                   0x08
#define P_DATA_MODE_MASK                                0x10
#define P_ASIO_BUFFER_MANAGEMENT_MASK                   0x20

#define BIT_SKIP_SOUND                                  0x08    // bit 3
#define BIT_DATA_MODE                                   0x10    // bit 4
    
/* Bits in the CMD_MODIFY_CLOCK request. */
#define CMD_MODIFY_CLOCK_FD_BIT     0x00000001
#define CMD_MODIFY_CLOCK_T_BIT      0x00000002
#define CMD_MODIFY_CLOCK_S_BIT      0x00000004

/* Access to the results of the CMD_GET_TIME_CODE RMH. */
#define TIME_CODE_V_MASK            0x00800000
#define TIME_CODE_N_MASK            0x00400000
#define TIME_CODE_B_MASK            0x00200000
#define TIME_CODE_W_MASK            0x00100000

/* Values for the CMD_MANAGE_SIGNAL RMH. */
#define MANAGE_SIGNAL_TIME_CODE     0x01
#define MANAGE_SIGNAL_MIDI          0x02

/* Values for the CMD_CONFIG_TIME_CODE RMH. */
#define CONFIG_TIME_CODE_CANCEL     0x00001000
    
/* Mask to get only the effective time from the
 * high word out of the 2 returned by the DSP
 */
#define PCX_TIME_HI_MASK        0x000fffff

/* Values for setting a H-Buffer time */
#define HBUFFER_TIME_HIGH       0x00200000
#define HBUFFER_TIME_LOW        0x00000000

#define NOTIFY_MASK_TIME_HIGH   0x00400000
#define MULTIPLE_MASK_TIME_HIGH 0x00100000
#define STREAM_MASK_TIME_HIGH   0x00800000


/*
 *
 */
extern struct vx_cmd_info vx_dsp_cmds[];

void vx_init_rmh(struct vx_rmh *rmh, unsigned int cmd);

/**
 * vx_send_pipe_cmd_params - fill first command word for pipe commands
 * @rmh: the rmh to be modified
 * @is_capture: 0 = playback, 1 = capture operation
 * @param1: first pipe-parameter
 * @param2: second pipe-parameter
 */
static inline void vx_set_pipe_cmd_params(struct vx_rmh *rmh, int is_capture,
                                          int param1, int param2)
{
        if (is_capture)
                rmh->Cmd[0] |= COMMAND_RECORD_MASK;
        rmh->Cmd[0] |= (((u32)param1 & MASK_FIRST_FIELD) << FIELD_SIZE) & 
MASK_DSP_WORD;
                
        if (param2)
                rmh->Cmd[0] |= ((u32)param2 & MASK_FIRST_FIELD) & MASK_DSP_WORD;
        
}

/**
 * vx_set_stream_cmd_params - fill first command word for stream commands
 * @rmh: the rmh to be modified
 * @is_capture: 0 = playback, 1 = capture operation
 * @pipe: the pipe index (zero-based)
 */
static inline void vx_set_stream_cmd_params(struct vx_rmh *rmh, int is_capture, int 
pipe)
{
        if (is_capture)
                rmh->Cmd[0] |= COMMAND_RECORD_MASK;
        rmh->Cmd[0] |= (((u32)pipe & MASK_FIRST_FIELD) << FIELD_SIZE) & MASK_DSP_WORD;
}

#endif /* __VX_CMD_H */

--- NEW FILE: vx_core.c ---
/* to be in alsa-driver-specfici code */
#include <linux/config.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
#define spin_lock_bh spin_lock
#define spin_unlock_bh spin_unlock
#endif
#define __NO_VERSION__

/*
 * Driver for Digigram VX soundcards
 *
 * Hardware core part
 *
 * Copyright (c) 2002 by Takashi Iwai <[EMAIL PROTECTED]>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <sound/driver.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/asoundef.h>
#include <sound/info.h>
#include <asm/io.h>
#include "vx_core.h"
#include "vx_cmd.h"

MODULE_AUTHOR("Takashi Iwai <[EMAIL PROTECTED]>");
MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
MODULE_LICENSE("GPL");


/*
 * snd_vx_delay - delay for the specified time
 * @xmsec: the time to delay in msec
 */
void snd_vx_delay(vx_core_t *chip, int xmsec)
{
        if (! chip->in_suspend && ! in_interrupt() &&
            xmsec >= 1000 / HZ) {
                set_current_state(TASK_UNINTERRUPTIBLE);
                schedule_timeout((xmsec * HZ + 999) / 1000);
        } else {
                mdelay(xmsec);
        }
}

/*
 * vx_check_reg_bit - wait for the specified bit is set/reset on a register
 * @reg: register to check
 * @mask: bit mask
 * @bit: resultant bit to be checked
 * @time: time-out of loop in msec
 *
 * returns zero if a bit matches, or a negative error code.
 */
int snd_vx_check_reg_bit(vx_core_t *chip, int reg, int mask, int bit, int time)
{
        long end_time = jiffies + (time * HZ + 999) / 1000;
        static char *reg_names[VX_REG_MAX] = {
                "ICR", "CVR", "ISR", "IVR", "RXH", "RXM", "RXL",
                "DMA", "CDSP", "RFREQ", "RUER/V2", "DATA", "MEMIRQ",
                "ACQ", "BIT0", "BIT1", "MIC0", "MIC1", "MIC2",
                "MIC3", "INTCSR", "CNTRL", "GPIOC",
                "LOFREQ", "HIFREQ", "CSUER", "RUER"
        };
        do {
                if ((snd_vx_inb(chip, reg) & mask) == bit)
                        return 0;
                //snd_vx_delay(chip, 10);
        } while (time_after_eq(end_time, jiffies));
        snd_printd(KERN_DEBUG "vx_check_reg_bit: timeout, reg=%s, mask=0x%x, 
val=0x%x\n", reg_names[reg], mask, snd_vx_inb(chip, reg));
        return -EIO;
}

/*
 * vx_send_irq_dsp - set command irq bit
 * @num: the requested IRQ type, IRQ_XXX
 *
 * this triggers the specified IRQ request
 * returns 0 if successful, or a negative error code.
 * 
 */
static int vx_send_irq_dsp(vx_core_t *chip, int num)
{
        int nirq;

        /* wait for Hc = 0 */
        if (snd_vx_check_reg_bit(chip, VX_CVR, CVR_HC, 0, 200) < 0)
                return -EIO;

        nirq = num;
        if (chip->type >= VX_TYPE_VXPOCKET)
                nirq += VXP_IRQ_OFFSET;
        vx_outb(chip, CVR, (nirq >> 1) | CVR_HC);
        return 0;
}


/*
 * vx_reset_chk - reset CHK bit on ISR
 *
 * returns 0 if successful, or a negative error code.
 */
static int vx_reset_chk(vx_core_t *chip)
{
        /* Reset irq CHK */
        if (vx_send_irq_dsp(chip, IRQ_RESET_CHK) < 0)
                return -EIO;
        /* Wait until CHK = 0 */
        if (vx_check_isr(chip, ISR_CHK, 0, 200) < 0)
                return -EIO;
        return 0;
}

/*
 * vx_transfer_end - terminate message transfer
 * @cmd: IRQ message to send (IRQ_MESS_XXX_END)
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
 * NB: call with spinlock held!
 */
static int vx_transfer_end(vx_core_t *chip, int cmd)
{
        int err;

        if ((err = vx_reset_chk(chip)) < 0)
                return err;

        /* irq MESS_READ/WRITE_END */
        if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
                return err;

        /* Wait CHK = 1 */
        if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
                return err;

        /* If error, Read RX */
        if ((err = vx_inb(chip, ISR)) & ISR_ERR) {
                if ((err = vx_wait_for_rx_full(chip)) < 0) {
                        snd_printd(KERN_DEBUG "transfer_end: error in rx_full\n");
                        return err;
                }
                err = vx_inb(chip, RXH) << 16;
                err |= vx_inb(chip, RXM) << 8;
                err |= vx_inb(chip, RXL);
                snd_printd(KERN_DEBUG "transfer_end: error = 0x%x\n", err);
                return -(VX_ERR_MASK | err);
        }
        return 0;
}

/*
 * vx_read_status - return the status rmh
 * @rmh: rmh record to store the status
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
 * NB: call with spinlock held!
 */
static int vx_read_status(vx_core_t *chip, struct vx_rmh *rmh)
{
        int i, err, val, size;

        /* no read necessary? */
        if (rmh->DspStat == RMH_SSIZE_FIXED && rmh->LgStat == 0)
                return 0;

        /* Wait for RX full (with timeout protection)
         * The first word of status is in RX
         */
        err = vx_wait_for_rx_full(chip);
        if (err < 0)
                return err;

        /* Read RX */
        val = vx_inb(chip, RXH) << 16;
        val |= vx_inb(chip, RXM) << 8;
        val |= vx_inb(chip, RXL);

        /* If status given by DSP, let's decode its size */
        switch (rmh->DspStat) {
        case RMH_SSIZE_ARG:
                size = val & 0xff;
                rmh->Stat[0] = val & 0xffff00;
                rmh->LgStat = size + 1;
                break;
        case RMH_SSIZE_MASK:
                /* Let's count the arg numbers from a mask */
                rmh->Stat[0] = val;
                size = 0;
                while (val) {
                        if (val & 0x01)
                                size++;
                        val >>= 1;
                }
                rmh->LgStat = size + 1;
                break;
        default:
                /* else retrieve the status length given by the driver */
                size = rmh->LgStat;
                rmh->Stat[0] = val;  /* Val is the status 1st word */
                size--;              /* hence adjust remaining length */
                break;
        }

        if (size < 1)
                return 0;
        snd_assert(size <= SIZE_MAX_STATUS, return -EINVAL);

        for (i = 1; i <= size; i++) {
                /* trigger an irq MESS_WRITE_NEXT */
                err = vx_send_irq_dsp(chip, IRQ_MESS_WRITE_NEXT);
                if (err < 0)
                        return err;
                /* Wait for RX full (with timeout protection) */
                err = vx_wait_for_rx_full(chip);
                if (err < 0)
                        return err;
                rmh->Stat[i] = vx_inb(chip, RXH) << 16;
                rmh->Stat[i] |= vx_inb(chip, RXM) <<  8;
                rmh->Stat[i] |= vx_inb(chip, RXL);
        }

        return vx_transfer_end(chip, IRQ_MESS_WRITE_END);
}


#define MASK_MORE_THAN_1_WORD_COMMAND   0x00008000
#define MASK_1_WORD_COMMAND             0x00ff7fff

/*
 * vx_send_msg_nolock - send a DSP message and read back the status
 * @rmh: the rmh record to send and receive
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
 * 
 * this function doesn't call spinlock at all.
 */
int vx_send_msg_nolock(vx_core_t *chip, struct vx_rmh *rmh)
{
        int i, err;
        
        if (chip->is_stale)
                return -EBUSY;

        if ((err = vx_reset_chk(chip)) < 0) {
                snd_printd(KERN_DEBUG "vx_send_msg: vx_reset_chk error\n");
                return err;
        }

#if 0
        printk(KERN_DEBUG "rmh: cmd = 0x%06x, length = %d, stype = %d\n",
               rmh->Cmd[0], rmh->LgCmd, rmh->DspStat);
        if (rmh->LgCmd > 1) {
                printk(KERN_DEBUG "  ");
                for (i = 1; i < rmh->LgCmd; i++)
                        printk("0x%06x ", rmh->Cmd[i]);
                printk("\n");
        }
#endif
        /* Check bit M is set according to length of the command */
        if (rmh->LgCmd > 1)
                rmh->Cmd[0] |= MASK_MORE_THAN_1_WORD_COMMAND;
        else
                rmh->Cmd[0] &= MASK_1_WORD_COMMAND;

        /* Wait for TX empty */
        if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
                snd_printd(KERN_DEBUG "vx_send_msg: wait tx empty error\n");
                return err;
        }

        /* Write Cmd[0] */
        vx_outb(chip, TXH, (rmh->Cmd[0] >> 16) & 0xff);
        vx_outb(chip, TXM, (rmh->Cmd[0] >> 8) & 0xff);
        vx_outb(chip, TXL, rmh->Cmd[0] & 0xff);

        /* Trigger irq MESSAGE */
        if ((err = vx_send_irq_dsp(chip, IRQ_MESSAGE)) < 0) {
                snd_printd(KERN_DEBUG "vx_send_msg: send IRQ_MESSAGE error\n");
                return err;
        }

        /* Wait for CHK = 1 */
        if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
                return err;

        /* If error, get error value from RX */
        if (vx_inb(chip, ISR) & ISR_ERR) {
                if ((err = vx_wait_for_rx_full(chip)) < 0) {
                        snd_printd(KERN_DEBUG "vx_send_msg: rx_full read error\n");
                        return err;
                }
                err = vx_inb(chip, RXH) << 16;
                err |= vx_inb(chip, RXM) << 8;
                err |= vx_inb(chip, RXL);
                snd_printd(KERN_DEBUG "msg got error = 0x%x at cmd[0]\n", err);
                err = -(VX_ERR_MASK | err);
                return err;
        }

        /* Send the other words */
        if (rmh->LgCmd > 1) {
                for (i = 1; i < rmh->LgCmd; i++) {
                        /* Wait for TX ready */
                        if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
                                snd_printd(KERN_DEBUG "vx_send_msg: tx_ready error\n");
                                return err;
                        }

                        /* Write Cmd[i] */
                        vx_outb(chip, TXH, (rmh->Cmd[i] >> 16) & 0xff);
                        vx_outb(chip, TXM, (rmh->Cmd[i] >> 8) & 0xff);
                        vx_outb(chip, TXL, rmh->Cmd[i] & 0xff);

                        /* Trigger irq MESS_READ_NEXT */
                        if ((err = vx_send_irq_dsp(chip, IRQ_MESS_READ_NEXT)) < 0) {
                                snd_printd(KERN_DEBUG "vx_send_msg: IRQ_READ_NEXT 
error\n");
                                return err;
                        }
                }
                /* Wait for TX empty */
                if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
                        snd_printd(KERN_DEBUG "vx_send_msg: TX_READY error\n");
                        return err;
                }
                /* End of transfer */
                err = vx_transfer_end(chip, IRQ_MESS_READ_END);
                if (err < 0)
                        return err;
        }

        return vx_read_status(chip, rmh);
}


/*
 * vx_send_msg - send a DSP message with spinlock
 * @rmh: the rmh record to send and receive
 *
 * returns 0 if successful, or a negative error code.
 * see vx_send_msg_nolock().
 */
int vx_send_msg(vx_core_t *chip, struct vx_rmh *rmh)
{
        int err;

        spin_lock_bh(&chip->lock);
        err = vx_send_msg_nolock(chip, rmh);
        spin_unlock_bh(&chip->lock);
        return err;
}


/*
 * vx_send_rih_nolock - send an RIH to xilinx
 * @cmd: the command to send
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
 *
 * this function doesn't call spinlock at all.
 *
 * unlike RMH, no command is sent to DSP.
 */
int vx_send_rih_nolock(vx_core_t *chip, int cmd)
{
        int err;

        if (chip->is_stale)
                return -EBUSY;

#if 0
        printk(KERN_DEBUG "send_rih: cmd = 0x%x\n", cmd);
#endif
        if ((err = vx_reset_chk(chip)) < 0)
                return err;
        /* send the IRQ */
        if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
                return err;
        /* Wait CHK = 1 */
        if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
                return err;
        /* If error, read RX */
        if (vx_inb(chip, ISR) & ISR_ERR) {
                if ((err = vx_wait_for_rx_full(chip)) < 0)
                        return err;
                err = vx_inb(chip, RXH) << 16;
                err |= vx_inb(chip, RXM) << 8;
                err |= vx_inb(chip, RXL);
                return -(VX_ERR_MASK | err);
        }
        return 0;
}


/*
 * vx_send_rih - send an RIH with spinlock
 * @cmd: the command to send
 *
 * see vx_send_rih_nolock().
 */
int vx_send_rih(vx_core_t *chip, int cmd)
{
        int err;

        spin_lock_bh(&chip->lock);
        err = vx_send_rih_nolock(chip, cmd);
        spin_unlock_bh(&chip->lock);
        return err;
}

#define END_OF_RESET_WAIT_TIME          200     /* ms */

/**
 * snd_vx_boot_xilinx - boot up the xilinx interface
 * @boot: the boot record to load
 */
int snd_vx_load_boot_image(vx_core_t *chip, const struct snd_vx_image *boot)
{
        int c, i;
        int no_fillup = chip->type != VX_TYPE_BOARD;

        snd_printdd(KERN_DEBUG "loading boot: size = %d\n", boot->length);

        /* check the length of boot image */
        snd_assert(boot->length % 3 == 0, return -EINVAL);
        c = ((u32)boot->image[0] << 16) | ((u32)boot->image[1] << 8) | boot->image[2];
        snd_assert(boot->length == (c + 2) * 3, return -EINVAL);

        /* reset dsp */
        vx_reset_dsp(chip);
        
        snd_vx_delay(chip, END_OF_RESET_WAIT_TIME); /* another wait? */

        /* download boot strap */
        for (i = 0; i < 0x600; i += 3) {
                if (i >= boot->length) {
                        if (no_fillup)
                                break;
                        if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0)
                                return -EIO;
                        vx_outb(chip, TXH, 0);
                        vx_outb(chip, TXM, 0);
                        vx_outb(chip, TXL, 0);
                } else {
                        if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0)
                                return -EIO;
                        vx_outb(chip, TXH, boot->image[i]);
                        vx_outb(chip, TXM, boot->image[i+1]);
                        vx_outb(chip, TXL, boot->image[i+2]);
                }
        }
        return 0;
}

/*
 * vx_load_dsp - load the DSP image
 */
static int vx_load_dsp(vx_core_t *chip)
{
        const struct snd_vx_image *dsp = &chip->hw->dsp;
        int i, err;
        unsigned int csum = 0;
        unsigned char *cptr;

        snd_assert(dsp->length % 3 == 0, return -EINVAL);

        vx_toggle_dac_mute(chip, 1);

        if ((err = snd_vx_load_boot_image(chip, &chip->hw->dsp_boot)) < 0)
                return err;
        snd_vx_delay(chip, 10);

        snd_printdd(KERN_DEBUG "loading dsp: size = %d\n", dsp->length);
        /* Transfert data buffer from PC to DSP */
        cptr = dsp->image;
        for (i = 0; i < dsp->length; i += 3) {
                /* Wait DSP ready for a new read */
                if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0)
                        return err;
                csum ^= *cptr;
                csum = (csum >> 24) | (csum << 8);
                vx_outb(chip, TXH, *cptr++);
                csum ^= *cptr;
                csum = (csum >> 24) | (csum << 8);
                vx_outb(chip, TXM, *cptr++);
                csum ^= *cptr;
                csum = (csum >> 24) | (csum << 8);
                vx_outb(chip, TXL, *cptr++);
        }
        snd_printdd(KERN_DEBUG "checksum = 0x%08x\n", csum);

        snd_vx_delay(chip, 200);

        err = vx_wait_isr_bit(chip, ISR_CHK);

        vx_toggle_dac_mute(chip, 0);

        return err;
}


/*
 * vx_test_irq_src - query the source of interrupts
 *
 * called from irq handler only
 */
static int vx_test_irq_src(vx_core_t *chip, int *ret)
{
        int err;

        vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
        spin_lock(&chip->lock);
        err = vx_send_msg_nolock(chip, &chip->irq_rmh);
        if (err < 0)
                *ret = 0;
        else
                *ret = chip->irq_rmh.Stat[0];
        spin_unlock(&chip->lock);
        return err;
}


/*
 * vx_interrupt - soft irq handler
 */
static void vx_interrupt(unsigned long private_data)
{
        vx_core_t *chip = snd_magic_cast(vx_core_t, (void*)private_data, return);
        int i, events;
        vx_pipe_t *pipe;
                
        if (chip->is_stale)
                return;

        if (vx_test_irq_src(chip, &events) < 0)
                return;
    
        // printk(KERN_DEBUG "IRQ events = 0x%x\n", events);

        /* We must prevent any application using this DSP
         * and block any further request until the application
         * either unregisters or reloads the DSP
         */
        if (events & FATAL_DSP_ERROR) {
                snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
                return;
        }

        /* The start on time code conditions are filled (ie the time code
         * received by the board is equal to one of those given to it).
         */
        if (events & TIME_CODE_EVENT_PENDING)
                ;

        /* The frequency has changed on the board (UER mode). */
        if (events & FREQUENCY_CHANGE_EVENT_PENDING)
                vx_change_frequency(chip);

        /* update the pcm pointers as frequently as possible */
        for (i = 0; i < chip->audio_outs; i++) {
                pipe = chip->playback_pipes[i];
                if (pipe && pipe->substream) {
                        vx_pcm_playback_update_buffer(chip, pipe->substream, pipe);
                        vx_pcm_playback_update(chip, pipe->substream, pipe);
                }
        }
        for (i = 0; i < chip->audio_ins; i++) {
                pipe = chip->capture_pipes[i];
                if (pipe && pipe->substream)
                        vx_pcm_capture_update(chip, pipe->substream, pipe);
        }
}


/**
 * snd_vx_irq_handler - interrupt handler
 */
void snd_vx_irq_handler(int irq, void *dev, struct pt_regs *regs)
{
        vx_core_t *chip = snd_magic_cast(vx_core_t, dev, return);

        if (! chip->initialized || chip->is_stale)
                return;
        if (! vx_test_and_ack(chip))
                tasklet_hi_schedule(&chip->tq);
}


/*
 */
static void vx_reset_board(vx_core_t *chip)
{
        snd_assert(chip->ops->reset_board, return);

        chip->audio_source = VX_AUDIO_SRC_LINE;
        chip->audio_source_target = chip->audio_source;
        chip->clock_source = INTERNAL_QUARTZ;
        chip->freq = 48000;
        chip->uer_detected = VX_UER_MODE_NOT_PRESENT;

        chip->ops->reset_board(chip);

        vx_reset_codec(chip);

        vx_set_internal_clock(chip, 48000);

        /* Reset the DSP */
        vx_reset_dsp(chip);

        /* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
        vx_test_and_ack(chip);
        vx_validate_irq(chip, 1);

        /* init CBits */
        chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
        vx_set_iec958_status(chip, SNDRV_PCM_DEFAULT_CON_SPDIF);
}


/*
 * proc interface
 */

static void vx_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
{
        vx_core_t *chip = snd_magic_cast(vx_core_t, entry->private_data, return);
        static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
        static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
        static char *clock_src[] = { "Internal", "External" };
        static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
        
        snd_iprintf(buffer, "%s\n", chip->card->longname);
        snd_iprintf(buffer, "DSP audio info:");
        if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
                snd_iprintf(buffer, " realtime");
        if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
                snd_iprintf(buffer, " offline");
        if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
                snd_iprintf(buffer, " mpeg1");
        if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
                snd_iprintf(buffer, " mpeg2");
        if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
                snd_iprintf(buffer, " linear8");
        if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
                snd_iprintf(buffer, " linear16");
        if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
                snd_iprintf(buffer, " linear24");
        snd_iprintf(buffer, "\n");
        snd_iprintf(buffer, "Input Source: %s\n", chip->type >= VX_TYPE_VXPOCKET ?
                    audio_src_vxp[chip->audio_source] :
                    audio_src_vx2[chip->audio_source]);
        snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
        snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
        snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
        snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
}

static void vx_proc_init(vx_core_t *chip)
{
        snd_info_entry_t *entry;

        if (! snd_card_proc_new(chip->card, "vx-status", &entry))
                snd_info_set_text_ops(entry, chip, vx_proc_read);
}


/*
 * snd_vxpocket_init - initialize VXpocket hardware
 */
int snd_vx_init(vx_core_t *chip)
{
        int err;

        snd_assert(chip->ops->load_xilinx &&
                   chip->ops->test_xilinx, return -ENXIO);

        if ((err = chip->ops->load_xilinx(chip)) < 0)
                return err;
        if ((err = chip->ops->test_xilinx(chip)) < 0)
                return err;

        vx_reset_board(chip);
        vx_validate_irq(chip, 0);
        if ((err = vx_load_dsp(chip)) < 0)
                return err;

        vx_test_and_ack(chip);
        vx_validate_irq(chip, 1);

        return 0;
}

/**
 * snd_vx_create - constructor for vx_core_t
 * @hw: hardware specific record
 *
 * this function allocates the instance and prepare for the hardware
 * initialization, which will be done via snd_vxpocket_assign_resources().
 *
 * return the instance pointer if successful, NULL in error.
 */
vx_core_t *snd_vx_create(snd_card_t *card, struct snd_vx_hardware *hw,
                         struct snd_vx_ops *ops,
                         int extra_size)
{
        vx_core_t *chip;

        snd_assert(card && hw && ops, return NULL);

        chip = snd_magic_kcalloc(vx_core_t, extra_size, GFP_KERNEL);
        if (! chip) {
                snd_printk(KERN_ERR "vxpocket: no memory\n");
                return NULL;
        }
        spin_lock_init(&chip->lock);
        spin_lock_init(&chip->irq_lock);
        chip->irq = -1;
        chip->hw = hw;
        chip->type = hw->type;
        chip->ops = ops;
        tasklet_init(&chip->tq, vx_interrupt, (unsigned long)chip);

        chip->card = card;
        card->private_data = chip;
        strcpy(card->driver, hw->name);
        sprintf(card->shortname, "Digigram %s", hw->name);

        vx_proc_init(chip);

        return chip;
}

#ifdef CONFIG_PM
/*
 * suspend
 */
void snd_vx_suspend(vx_core_t *chip)
{
        int i;

        chip->in_suspend = 1;
        for (i = 0; i < chip->hw->num_codecs; i++)
                snd_pcm_suspend_all(chip->pcm[i]);
}

/*
 * resume
 */
void snd_vx_resume(vx_core_t *chip)
{
        snd_vx_init(chip);
        /* FIXME: resume mixer config */
        chip->in_suspend = 0;
}

#endif

/*
 * module entries
 */
static int __init alsa_vx_core_init(void)
{
        return 0;
}

static void __exit alsa_vx_core_exit(void)
{
}

module_init(alsa_vx_core_init)
module_exit(alsa_vx_core_exit)

/*
 * exports
 */
EXPORT_SYMBOL(snd_vx_check_reg_bit);
EXPORT_SYMBOL(snd_vx_create);
EXPORT_SYMBOL(snd_vx_init);
EXPORT_SYMBOL(snd_vx_irq_handler);
EXPORT_SYMBOL(snd_vx_pcm_new);
EXPORT_SYMBOL(snd_vx_mixer_new);
EXPORT_SYMBOL(snd_vx_delay);
EXPORT_SYMBOL(snd_vx_load_boot_image);
#ifdef CONFIG_PM
EXPORT_SYMBOL(snd_vx_suspend);
EXPORT_SYMBOL(snd_vx_resume);
#endif

--- NEW FILE: vx_mixer.c ---
/* to be in alsa-driver-specfici code */
#define __NO_VERSION__

/*
 * Driver for Digigram VX soundcards
 *
 * Common mixer part
 *
 * Copyright (c) 2002 by Takashi Iwai <[EMAIL PROTECTED]>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <sound/driver.h>
#include <sound/core.h>
#include <sound/control.h>
#include "vx_core.h"
#include "vx_cmd.h"

#define chip_t vx_core_t


/*
 * write a codec data (24bit)
 */
static void vx_write_codec_reg(vx_core_t *chip, int codec, unsigned int data)
{
        unsigned long flags;

        if (chip->is_stale)
                return;

        spin_lock_irqsave(&chip->lock, flags);
        chip->ops->write_codec(chip, codec, data);
        spin_unlock_irqrestore(&chip->lock, flags);
}

/*
 * Data type used to access the Codec
 */
typedef union {
        u32 l;
#ifdef SNDRV_BIG_ENDIAN
        struct w {
                u16 h;
                u16 l;
        } w;
        struct b {
                u8 hh;
                u8 mh;
                u8 ml;
                u8 ll;
        } b;
#else /* LITTLE_ENDIAN */
        struct w {
                u16 l;
                u16 h;
        } w;
        struct b {
                u8 ll;
                u8 ml;
                u8 mh;
                u8 hh;
        } b;
#endif
} vx_codec_data_t;

#define SET_CDC_DATA_SEL(di,s)          ((di).b.mh = (u8) (s))
#define SET_CDC_DATA_REG(di,r)          ((di).b.ml = (u8) (r))
#define SET_CDC_DATA_VAL(di,d)          ((di).b.ll = (u8) (d))
#define SET_CDC_DATA_INIT(di)           ((di).l = 0L, 
SET_CDC_DATA_SEL(di,XX_CODEC_SELECTOR))

/*
 * set up codec register and write the value
 * @codec: the codec id, 0 or 1
 * @reg: register index
 * @val: data value
 */
static void vx_set_codec_reg(vx_core_t *chip, int codec, int reg, int val)
{
        vx_codec_data_t data;
        /* DAC control register */
        SET_CDC_DATA_INIT(data);
        SET_CDC_DATA_REG(data, reg);
        SET_CDC_DATA_VAL(data, val);
        vx_write_codec_reg(chip, codec, data.l);
}


#define ANALOG_OUT_ATTEN_MIN    0x00
#define ANALOG_OUT_ATTEN_MAX    0xe3

/*
 * vx_set_analog_output_level - set the output attenuation level
 * @codec: the output codec, 0 or 1.  (1 for VXP440 only)
 * @left: left attenuation level, 0 = 0 dB, 0xe3 = -113.5 dB
 * @right: right attenuation level
 */
static void vx_set_analog_output_level(vx_core_t *chip, int codec, int left, int right)
{
        vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_LEFT_REGISTER, left);
        vx_set_codec_reg(chip, codec, XX_CODEC_LEVEL_RIGHT_REGISTER, right);
}


/*
 * vx_toggle_dac_mute -  mute/unmute DAC
 * @mute: 0 = unmute, 1 = mute
 */

#define DAC_ATTEN_MIN   0x08
#define DAC_ATTEN_MAX   0x38

void vx_toggle_dac_mute(vx_core_t *chip, int mute)
{
        int i;
        for (i = 0; i < chip->hw->num_codecs; i++)
                vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER,
                                 mute ? DAC_ATTEN_MAX : DAC_ATTEN_MIN);
}

/*
 * vx_reset_codec - reset and initialize the codecs
 */
void vx_reset_codec(vx_core_t *chip)
{
        int i;
        int port = chip->type >= VX_TYPE_VXPOCKET ? 0x75 : 0x65;

        chip->ops->reset_codec(chip);

        for (i = 0; i < chip->hw->num_codecs; i++) {
                /* DAC control register (change level when zero crossing + mute) */
                vx_set_codec_reg(chip, i, XX_CODEC_DAC_CONTROL_REGISTER, 
DAC_ATTEN_MAX);
                /* ADC control register */
                vx_set_codec_reg(chip, i, XX_CODEC_ADC_CONTROL_REGISTER, 0x00);
                /* Port mode register */
                vx_set_codec_reg(chip, i, XX_CODEC_PORT_MODE_REGISTER, port);
                /* Clock control register */
                vx_set_codec_reg(chip, i, XX_CODEC_CLOCK_CONTROL_REGISTER, 0x00);
        }

        /* mute analog output */
        for (i = 0; i < chip->hw->num_codecs; i++) {
                chip->output_level[i][0] = ANALOG_OUT_ATTEN_MAX;
                chip->output_level[i][1] = ANALOG_OUT_ATTEN_MAX;
                vx_set_analog_output_level(chip, i,
                                           ANALOG_OUT_ATTEN_MAX,
                                           ANALOG_OUT_ATTEN_MAX);
        }
}

/*
 * change the audio input source
 * @src: the target source (VX_AUDIO_SRC_XXX)
 */
static void vx_change_audio_source(vx_core_t *chip, int src)
{
        unsigned long flags;

        if (chip->is_stale)
                return;

        spin_lock_irqsave(&chip->lock, flags);
        chip->ops->change_audio_source(chip, src);
        spin_unlock_irqrestore(&chip->lock, flags);
}


/*
 * change the audio source if necessary and possible
 * returns 1 if the source is actually changed.
 */
int vx_sync_audio_source(vx_core_t *chip)
{
        if (chip->audio_source_target == chip->audio_source ||
            chip->pcm_running)
                return 0;
        vx_change_audio_source(chip, chip->audio_source_target);
        chip->audio_source = chip->audio_source_target;
        return 1;
}


/*
 * audio level, mute, monitoring
 */
struct vx_audio_level {
        unsigned int has_level: 1;
        unsigned int has_monitor_level: 1;
        unsigned int has_mute: 1;
        unsigned int has_monitor_mute: 1;
        unsigned int mute;
        unsigned int monitor_mute;
        short level;
        short monitor_level;
};

static int vx_adjust_audio_level(vx_core_t *chip, int audio, int capture,
                                 struct vx_audio_level *info)
{
        struct vx_rmh rmh;

        if (chip->is_stale)
                return -EBUSY;

        vx_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
        if (capture)
                rmh.Cmd[0] |= COMMAND_RECORD_MASK;
        /* Add Audio IO mask */
        rmh.Cmd[1] = 1 << audio;
        rmh.Cmd[2] = 0;
        if (info->has_level) {
                rmh.Cmd[0] |=  VALID_AUDIO_IO_DIGITAL_LEVEL;
                rmh.Cmd[2] |= info->level;
        }
        if (info->has_monitor_level) {
                rmh.Cmd[0] |=  VALID_AUDIO_IO_MONITORING_LEVEL;
                rmh.Cmd[2] |= ((unsigned int)info->monitor_level << 10);
        }
        if (info->has_mute) { 
                rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_LEVEL;
                if (info->mute)
                        rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_LEVEL;
        }
        if (info->has_monitor_mute) {
                rmh.Cmd[0] |= VALID_AUDIO_IO_MUTE_MONITORING_1;
                if (info->monitor_mute)
                        rmh.Cmd[2] |= AUDIO_IO_HAS_MUTE_MONITORING_1;
        }
        return vx_send_msg(chip, &rmh);
}

    
#if 0 // not used
static int vx_read_audio_level(vx_core_t *chip, int audio, int capture,
                               struct vx_audio_level *info)
{
        int err;
        struct vx_rmh rmh;

        memset(info, 0, sizeof(*info));
        vx_init_rmh(&rmh, CMD_GET_AUDIO_LEVELS);
        if (capture)
                rmh.Cmd[0] |= COMMAND_RECORD_MASK;
        /* Add Audio IO mask */
        rmh.Cmd[1] = 1 << audio;
        err = vx_send_msg(chip, &rmh);
        if (err < 0)
                return err;
        info.level = rmh.Stat[0] & MASK_DSP_WORD_LEVEL;
        info.monitor_level = (rmh.Stat[0] >> 10) & MASK_DSP_WORD_LEVEL;
        info.mute = (rmh.Stat[i] & AUDIO_IO_HAS_MUTE_LEVEL) ? 1 : 0;
        info.monitor_mute = (rmh.Stat[i] & AUDIO_IO_HAS_MUTE_MONITORING_1) ? 1 : 0;
        return 0;
}
#endif

/*
 * set the monitoring level and mute state of the given audio
 */
static int vx_set_monitor_level(vx_core_t *chip, int audio, int level, int active)
{
        struct vx_audio_level info;

        memset(&info, 0, sizeof(info));
        info.has_monitor_level = 1;
        info.monitor_level = level;
        info.has_monitor_mute = 1;
        info.monitor_mute = !active;
        chip->audio_monitor[audio] = level;
        chip->audio_monitor_active[audio] = active;
        return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
}


/*
 * set the mute status of the given audio
 */
static int vx_set_audio_switch(vx_core_t *chip, int audio, int active)
{
        struct vx_audio_level info;

        memset(&info, 0, sizeof(info));
        info.has_mute = 1;
        info.mute = !active;
        chip->audio_active[audio] = active;
        return vx_adjust_audio_level(chip, audio, 0, &info); /* playback only */
}

/*
 * set the mute status of the given audio
 */
static int vx_set_audio_gain(vx_core_t *chip, int audio, int capture, int level)
{
        struct vx_audio_level info;

        memset(&info, 0, sizeof(info));
        info.has_level = 1;
        info.level = level;
        chip->audio_gain[capture][audio] = level;
        return vx_adjust_audio_level(chip, audio, capture, &info);
}

/*
 * reset all audio levels
 */
static void vx_reset_audio_levels(vx_core_t *chip)
{
        int i, c;
        struct vx_audio_level info;

        memset(chip->audio_gain, 0, sizeof(chip->audio_gain));
        memset(chip->audio_active, 0, sizeof(chip->audio_active));
        memset(chip->audio_monitor, 0, sizeof(chip->audio_monitor));
        memset(chip->audio_monitor_active, 0, sizeof(chip->audio_monitor_active));

        for (c = 0; c < 2; c++) {
                for (i = 0; i < chip->hw->num_ins * 2; i++) {
                        memset(&info, 0, sizeof(info));
                        if (c == 0) {
                                info.has_monitor_level = 1;
                                info.has_mute = 1;
                                info.has_monitor_mute = 1;
                        }
                        info.has_level = 1;
                        info.level = CVAL_0DB; /* default: 0dB */
                        vx_adjust_audio_level(chip, i, c, &info);
                        chip->audio_gain[c][i] = CVAL_0DB;
                        chip->audio_monitor[i] = CVAL_0DB;
                }
        }
}


/*
 * VU, peak meter record
 */

#define VU_METER_CHANNELS       2

struct vx_vu_meter {
        int saturated;
        int vu_level;
        int peak_level;
};

/*
 * get the VU and peak meter values
 * @audio: the audio index
 * @capture: 0 = playback, 1 = capture operation
 * @info: the array of vx_vu_meter records (size = 2).
 */
static int vx_get_audio_vu_meter(vx_core_t *chip, int audio, int capture, struct 
vx_vu_meter *info)
{
        struct vx_rmh rmh;
        int i, err;

        if (chip->is_stale)
                return -EBUSY;

        vx_init_rmh(&rmh, CMD_AUDIO_VU_PIC_METER);
        rmh.LgStat += 2 * VU_METER_CHANNELS;
        if (capture)
                rmh.Cmd[0] |= COMMAND_RECORD_MASK;
    
        /* Add Audio IO mask */
        rmh.Cmd[1] = 0;
        for (i = 0; i < VU_METER_CHANNELS; i++)
                rmh.Cmd[1] |= 1 << (audio + i);
        err = vx_send_msg(chip, &rmh);
        if (err < 0)
                return err;
        /* Read response */
        for (i = 0; i < 2 * VU_METER_CHANNELS; i +=2) {
                info->saturated = (rmh.Stat[0] & (1 << (audio + i))) ? 1 : 0;
                info->vu_level = rmh.Stat[i + 1];
                info->peak_level = rmh.Stat[i + 2];
                info++;
        }
        return 0;
}
   

/*
 * control API entries
 */

/*
 * output level control
 */
static int vx_output_level_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 2;
        uinfo->value.integer.min = ANALOG_OUT_ATTEN_MIN;
        uinfo->value.integer.max = ANALOG_OUT_ATTEN_MAX - ANALOG_OUT_ATTEN_MIN;
        return 0;
}

static int vx_output_level_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t 
*ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int codec = kcontrol->id.index;
        ucontrol->value.integer.value[0] = ANALOG_OUT_ATTEN_MAX - 
chip->output_level[codec][0];
        ucontrol->value.integer.value[1] = ANALOG_OUT_ATTEN_MAX - 
chip->output_level[codec][1];
        return 0;
}

static int vx_output_level_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t 
*ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int val[2];
        int codec = kcontrol->id.index;
        val[0] = ANALOG_OUT_ATTEN_MAX - ucontrol->value.integer.value[0];
        val[1] = ANALOG_OUT_ATTEN_MAX - ucontrol->value.integer.value[1];
        if (val[0] != chip->output_level[codec][0] ||
            val[1] != chip->output_level[codec][1]) {
                vx_set_analog_output_level(chip, codec, val[0], val[1]);
                chip->output_level[codec][0] = val[0];
                chip->output_level[codec][1] = val[1];
                return 1;
        }
        return 0;
}

static snd_kcontrol_new_t vx_control_output_level = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "Master Playback Volume",
        .info =         vx_output_level_info,
        .get =          vx_output_level_get,
        .put =          vx_output_level_put,
};

/*
 * audio source select
 */
static int vx_audio_src_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        static char *texts_mic[3] = {
                "Digital", "Line", "Mic"
        };
        static char *texts_vx2[2] = {
                "Digital", "Analog"
        };
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);

        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
        uinfo->count = 1;
        if (chip->type >= VX_TYPE_VXPOCKET) {
                uinfo->value.enumerated.items = 3;
                if (uinfo->value.enumerated.item > 2)
                        uinfo->value.enumerated.item = 2;
                strcpy(uinfo->value.enumerated.name,
                       texts_mic[uinfo->value.enumerated.item]);
        } else {
                uinfo->value.enumerated.items = 2;
                if (uinfo->value.enumerated.item > 1)
                        uinfo->value.enumerated.item = 1;
                strcpy(uinfo->value.enumerated.name,
                       texts_vx2[uinfo->value.enumerated.item]);
        }
        return 0;
}

static int vx_audio_src_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        ucontrol->value.enumerated.item[0] = chip->audio_source_target;
        return 0;
}

static int vx_audio_src_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        if (chip->audio_source_target != ucontrol->value.enumerated.item[0]) {
                chip->audio_source_target = ucontrol->value.enumerated.item[0];
                vx_sync_audio_source(chip);
                return 1;
        }
        return 0;
}

static snd_kcontrol_new_t vx_control_audio_src = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "Capture Source",
        .info =         vx_audio_src_info,
        .get =          vx_audio_src_get,
        .put =          vx_audio_src_put,
};

/*
 * Audio Gain
 */
static int vx_audio_gain_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 2;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = CVAL_MAX;
        return 0;
}

static int vx_audio_gain_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;
        int capture = (kcontrol->private_value >> 8) & 1;

        ucontrol->value.integer.value[0] = chip->audio_gain[capture][audio];
        ucontrol->value.integer.value[1] = chip->audio_gain[capture][audio+1];
        return 0;
}

static int vx_audio_gain_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;
        int capture = (kcontrol->private_value >> 8) & 1;

        if (ucontrol->value.integer.value[0] != chip->audio_gain[capture][audio] ||
            ucontrol->value.integer.value[1] != chip->audio_gain[capture][audio+1]) {
                vx_set_audio_gain(chip, audio, capture, 
ucontrol->value.integer.value[0]);
                vx_set_audio_gain(chip, audio+1, capture, 
ucontrol->value.integer.value[1]);
                return 1;
        }
        return 0;
}

static int vx_audio_monitor_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t 
*ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;

        ucontrol->value.integer.value[0] = chip->audio_monitor[audio];
        ucontrol->value.integer.value[1] = chip->audio_monitor[audio+1];
        return 0;
}

static int vx_audio_monitor_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t 
*ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;

        if (ucontrol->value.integer.value[0] != chip->audio_monitor[audio] ||
            ucontrol->value.integer.value[1] != chip->audio_monitor[audio+1]) {
                vx_set_monitor_level(chip, audio, ucontrol->value.integer.value[0],
                                     chip->audio_monitor_active[audio]);
                vx_set_monitor_level(chip, audio+1, ucontrol->value.integer.value[1],
                                     chip->audio_monitor_active[audio+1]);
                return 1;
        }
        return 0;
}

static int vx_audio_sw_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
        uinfo->count = 2;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = 1;
        return 0;
}

static int vx_audio_sw_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;

        ucontrol->value.integer.value[0] = chip->audio_active[audio];
        ucontrol->value.integer.value[1] = chip->audio_active[audio+1];
        return 0;
}

static int vx_audio_sw_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;

        if (ucontrol->value.integer.value[0] != chip->audio_active[audio] ||
            ucontrol->value.integer.value[1] != chip->audio_active[audio+1]) {
                vx_set_audio_switch(chip, audio, ucontrol->value.integer.value[0]);
                vx_set_audio_switch(chip, audio+1, ucontrol->value.integer.value[1]);
                return 1;
        }
        return 0;
}

static int vx_monitor_sw_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;

        ucontrol->value.integer.value[0] = chip->audio_monitor_active[audio];
        ucontrol->value.integer.value[1] = chip->audio_monitor_active[audio+1];
        return 0;
}

static int vx_monitor_sw_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        int audio = kcontrol->private_value & 0xff;

        if (ucontrol->value.integer.value[0] != chip->audio_monitor_active[audio] ||
            ucontrol->value.integer.value[1] != chip->audio_monitor_active[audio+1]) {
                vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
                                     ucontrol->value.integer.value[0]);
                vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
                                     ucontrol->value.integer.value[1]);
                return 1;
        }
        return 0;
}

static snd_kcontrol_new_t vx_control_audio_gain = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        /* name will be filled later */
        .info =         vx_audio_gain_info,
        .get =          vx_audio_gain_get,
        .put =          vx_audio_gain_put
};
static snd_kcontrol_new_t vx_control_output_switch = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "PCM Playback Switch",
        .info =         vx_audio_sw_info,
        .get =          vx_audio_sw_get,
        .put =          vx_audio_sw_put
};
static snd_kcontrol_new_t vx_control_monitor_gain = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "Monitoring Volume",
        .info =         vx_audio_gain_info,     /* shared */
        .get =          vx_audio_monitor_get,
        .put =          vx_audio_monitor_put
};
static snd_kcontrol_new_t vx_control_monitor_switch = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "Monitoring Switch",
        .info =         vx_audio_sw_info,       /* shared */
        .get =          vx_monitor_sw_get,
        .put =          vx_monitor_sw_put
};


/*
 * IEC958 status bits
 */
static int vx_iec958_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
        uinfo->count = 1;
        return 0;
}

static int vx_iec958_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);

        ucontrol->value.iec958.status[0] = (chip->uer_bits >> 0) & 0xff;
        ucontrol->value.iec958.status[1] = (chip->uer_bits >> 8) & 0xff;
        ucontrol->value.iec958.status[2] = (chip->uer_bits >> 16) & 0xff;
        ucontrol->value.iec958.status[3] = (chip->uer_bits >> 24) & 0xff;
        return 0;
}

static int vx_iec958_mask_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t 
*ucontrol)
{
        ucontrol->value.iec958.status[0] = 0xff;
        ucontrol->value.iec958.status[1] = 0xff;
        ucontrol->value.iec958.status[2] = 0xff;
        ucontrol->value.iec958.status[3] = 0xff;
        return 0;
}

static int vx_iec958_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        unsigned int val;

        val = (ucontrol->value.iec958.status[0] << 0) |
              (ucontrol->value.iec958.status[1] << 8) |
              (ucontrol->value.iec958.status[2] << 16) |
              (ucontrol->value.iec958.status[3] << 24);
        if (chip->uer_bits != val) {
                chip->uer_bits = val;
                vx_set_iec958_status(chip, val);
                return 1;
        }
        return 0;
}

static snd_kcontrol_new_t vx_control_iec958_mask = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ,
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
        .info =         vx_iec958_info, /* shared */
        .get =          vx_iec958_mask_get,
};

static snd_kcontrol_new_t vx_control_iec958 = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
        .info =         vx_iec958_info,
        .get =          vx_iec958_get,
        .put =          vx_iec958_put
};


/*
 * VU meter
 */

#define METER_MAX       0xff
#define METER_SHIFT     16

static int vx_vu_meter_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 2;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = METER_MAX;
        return 0;
}

static int vx_vu_meter_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        struct vx_vu_meter meter[2];
        int audio = kcontrol->private_value & 0xff;
        int capture = (kcontrol->private_value >> 8) & 1;

        vx_get_audio_vu_meter(chip, audio, capture, meter);
        ucontrol->value.integer.value[0] = meter[0].vu_level >> METER_SHIFT;
        ucontrol->value.integer.value[1] = meter[1].vu_level >> METER_SHIFT;
        return 0;
}

static int vx_peak_meter_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        struct vx_vu_meter meter[2];
        int audio = kcontrol->private_value & 0xff;
        int capture = (kcontrol->private_value >> 8) & 1;

        vx_get_audio_vu_meter(chip, audio, capture, meter);
        ucontrol->value.integer.value[0] = meter[0].peak_level >> METER_SHIFT;
        ucontrol->value.integer.value[1] = meter[1].peak_level >> METER_SHIFT;
        return 0;
}

static int vx_saturation_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
        uinfo->count = 2;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = 1;
        return 0;
}

static int vx_saturation_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
{
        vx_core_t *chip = snd_kcontrol_chip(kcontrol);
        struct vx_vu_meter meter[2];
        int audio = kcontrol->private_value & 0xff;

        vx_get_audio_vu_meter(chip, audio, 1, meter); /* capture only */
        ucontrol->value.integer.value[0] = meter[0].saturated;
        ucontrol->value.integer.value[1] = meter[1].saturated;
        return 0;
}

static snd_kcontrol_new_t vx_control_vu_meter = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        /* name will be filled later */
        .info =         vx_vu_meter_info,
        .get =          vx_vu_meter_get,
};

static snd_kcontrol_new_t vx_control_peak_meter = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        /* name will be filled later */
        .info =         vx_vu_meter_info,       /* shared */
        .get =          vx_peak_meter_get,
};

static snd_kcontrol_new_t vx_control_saturation = {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "Input Saturation",
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         vx_saturation_info,
        .get =          vx_saturation_get,
};



/*
 *
 */

int snd_vx_mixer_new(vx_core_t *chip)
{
        int i, c, err;
        snd_kcontrol_new_t temp;
        snd_card_t *card = chip->card;
        char name[32];

        strcpy(card->mixername, card->driver);

        /* output level controls */
        for (i = 0; i < chip->hw->num_outs; i++) {
                temp = vx_control_output_level;
                temp.index = i;
                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                        return err;
        }

        /* PCM volumes, switches, monitoring */
        for (i = 0; i < chip->hw->num_outs; i++) {
                int val = i * 2;
                temp = vx_control_audio_gain;
                temp.index = i;
                temp.name = "PCM Playback Volume";
                temp.private_value = val;
                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                        return err;
                temp = vx_control_output_switch;
                temp.index = i;
                temp.private_value = val;
                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                        return err;
                temp = vx_control_monitor_gain;
                temp.index = i;
                temp.private_value = val;
                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                        return err;
                temp = vx_control_monitor_switch;
                temp.index = i;
                temp.private_value = val;
                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                        return err;
        }
        for (i = 0; i < chip->hw->num_outs; i++) {
                temp = vx_control_audio_gain;
                temp.index = i;
                temp.name = "PCM Capture Volume";
                temp.private_value = (i * 2) | (1 << 8);
                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                        return err;
        }

        /* Audio source */
        if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_audio_src, chip))) < 0)
                return err;
        /* IEC958 controls */
        if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958_mask, chip))) < 0)
                return err;
        if ((err = snd_ctl_add(card, snd_ctl_new1(&vx_control_iec958, chip))) < 0)
                return err;
        /* VU, peak, saturation meters */
        for (c = 0; c < 1; c++) {
                static char *dir[2] = { "Output", "Input" };
                for (i = 0; i < chip->hw->num_ins; i++) {
                        int val = (i * 2) | (c << 8);
                        if (c == 1) {
                                temp = vx_control_saturation;
                                temp.index = i;
                                temp.private_value = val;
                                if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, 
chip))) < 0)
                                        return err;
                        }
                        sprintf(name, "%s VU Meter", dir[c]);
                        temp = vx_control_vu_meter;
                        temp.index = i;
                        temp.name = name;
                        temp.private_value = val;
                        if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                                return err;
                        sprintf(name, "%s Peak Meter", dir[c]);
                        temp = vx_control_peak_meter;
                        temp.index = i;
                        temp.name = name;
                        temp.private_value = val;
                        if ((err = snd_ctl_add(card, snd_ctl_new1(&temp, chip))) < 0)
                                return err;
                }
        }
        vx_reset_audio_levels(chip);
        return 0;
}

--- NEW FILE: vx_pcm.c ---
/* to be in alsa-driver-specfici code */
#include <linux/config.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
static struct page *vmalloc_to_page(void *pageptr);
#endif
#define __NO_VERSION__

/*
 * Driver for Digigram VX soundcards
 *
 * PCM part
 *
 * Copyright (c) 2002 by Takashi Iwai <[EMAIL PROTECTED]>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <sound/driver.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/pagemap.h>
#include <sound/core.h>
#include <sound/asoundef.h>
#include <sound/pcm.h>
#include "vx_core.h"
#include "vx_cmd.h"

#define chip_t  vx_core_t


/*
 * we use a vmalloc'ed (sg-)buffer
 */

/* get the physical page pointer on the given offset */
static struct page *snd_pcm_get_vmalloc_page(snd_pcm_substream_t *subs, unsigned long 
offset)
{
        void *pageptr = subs->runtime->dma_area + offset;
        return vmalloc_to_page(pageptr);
}

/*
 * hw_params callback
 * NOTE: this may be called not only once per pcm open!
 */
static int snd_pcm_alloc_vmalloc_buffer(snd_pcm_substream_t *subs, int size)
{
        snd_pcm_runtime_t *runtime = subs->runtime;
        if (runtime->dma_area) {
                if (runtime->dma_bytes >= size)
                        return 0; /* already enough large */
                vfree_nocheck(runtime->dma_area);
        }
        runtime->dma_area = vmalloc_nocheck(size);
        if (! runtime->dma_area)
                return -ENOMEM;
        runtime->dma_bytes = size;
        return 0;
}

/*
 * hw_free callback
 * NOTE: this may be called not only once per pcm open!
 */
static int snd_pcm_free_vmalloc_buffer(snd_pcm_substream_t *subs)
{
        snd_pcm_runtime_t *runtime = subs->runtime;
        if (runtime->dma_area) {
                vfree_nocheck(runtime->dma_area);
                runtime->dma_area = NULL;
        }
        return 0;
}


/*
 * vx_flush_read - read rest of bytes via normal transfer mode
 * @count: frames to read
 *
 * the data size must be aligned to 3 bytes, though.
 * NB: call with a certain lock!
 */
static void vx_flush_read(vx_core_t *chip, snd_pcm_runtime_t *runtime,
                          vx_pipe_t *pipe, int count)
{
        int offset = frames_to_bytes(runtime, pipe->hw_ptr);
        unsigned char *buf = (unsigned char *)(runtime->dma_area + offset);

        snd_assert(count % 3 == 0, return);
        pipe->hw_ptr += count;
        if (pipe->hw_ptr >= runtime->buffer_size)
                pipe->hw_ptr -= runtime->buffer_size;
        while (count > 0) {
                *buf++ = vx_inb(chip, RXH);
                if (++offset >= pipe->buffer_bytes) {
                        offset = 0;
                        buf = (unsigned char *)runtime->dma_area;
                }
                *buf++ = vx_inb(chip, RXM);
                if (++offset >= pipe->buffer_bytes) {
                        offset = 0;
                        buf = (unsigned char *)runtime->dma_area;
                }
                *buf++ = vx_inb(chip, RXL);
                if (++offset >= pipe->buffer_bytes) {
                        offset = 0;
                        buf = (unsigned char *)runtime->dma_area;
                }
                count -= 3;
        }
}

/*
 * vx_set_pcx_time - convert from the PC time to the RMH status time.
 * @pc_time: the pointer for the PC-time to set
 * @dsp_time: the pointer for RMH status time array
 */
static void vx_set_pcx_time(vx_core_t *chip, pcx_time_t *pc_time, unsigned int 
*dsp_time)
{
        dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
        dsp_time[1] = (unsigned int)(*pc_time) &  MASK_DSP_WORD;
}

/*
 * vx_set_differed_time - set the differed time if specified
 * @rmh: the rmh record to modify
 * @pipe: the pipe to be checked
 *
 * if the pipe is programmed with the differed time, set the DSP time
 * on the rmh and changes its command length.
 *
 * returns the increase of the command length.
 */
static int vx_set_differed_time(vx_core_t *chip, struct vx_rmh *rmh, vx_pipe_t *pipe)
{
        /* Update The length added to the RMH command by the timestamp */
        if (! (pipe->differed_type & DC_DIFFERED_DELAY))
                return 0;
                
        /* Set the T bit */
        rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;

        /* Time stamp is the 1st following parameter */
        vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);

        /* Add the flags to a notified differed command */
        if (pipe->differed_type & DC_NOTIFY_DELAY)
                rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;

        /* Add the flags to a multiple differed command */
        if (pipe->differed_type & DC_MULTIPLE_DELAY)
                rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;

        /* Add the flags to a stream-time differed command */
        if (pipe->differed_type & DC_STREAM_TIME_DELAY)
                rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
                
        rmh->LgCmd += 2;
        return 2;
}

/*
 * vx_set_stream_format - send the stream format command
 * @pipe: the affected pipe
 * @data: format bitmask
 */
static int vx_set_stream_format(vx_core_t *chip, vx_pipe_t *pipe, unsigned int data)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, pipe->is_capture ?
                    CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
        rmh.Cmd[0] |= pipe->number << FIELD_SIZE;

        /* Command might be longer since we may have to add a timestamp */
        vx_set_differed_time(chip, &rmh, pipe);

        rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
        rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
        rmh.LgCmd += 2;
    
        return vx_send_msg(chip, &rmh);
}


/*
 * vx_set_format - set the format of a pipe
 * @pipe: the affected pipe
 * @runtime: pcm runtime instance to be referred
 *
 * returns 0 if successful, or a negative error code.
 */
static int vx_set_format(vx_core_t *chip, vx_pipe_t *pipe,
                         snd_pcm_runtime_t *runtime)
{
        unsigned int header = HEADER_FMT_BASE;

        if (runtime->channels == 1)
                header |= HEADER_FMT_MONO;
        if (snd_pcm_format_little_endian(runtime->format))
                header |= HEADER_FMT_INTEL;
        if (runtime->rate < 32000 && runtime->rate > 11025)
                header |= HEADER_FMT_UPTO32;
        else if (runtime->rate <= 11025)
                header |= HEADER_FMT_UPTO11;

        switch (snd_pcm_format_physical_width(runtime->format)) {
        // case 8: break;
        case 16: header |= HEADER_FMT_16BITS; break;
        case 24: header |= HEADER_FMT_24BITS; break;
        default : 
                snd_BUG();
                return -EINVAL;
        };

        return vx_set_stream_format(chip, pipe, header);
}

static int vx_set_ibl(vx_core_t *chip, struct vx_ibl_info *info)
{
        int err;
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_IBL);
        rmh.Cmd[0] |= info->size & 0x03ffff;
        err = vx_send_msg(chip, &rmh);
        if (err < 0)
                return err;
        info->size = rmh.Stat[0];
        info->max_size = rmh.Stat[1];
        info->min_size = rmh.Stat[2];
        info->granularity = rmh.Stat[3];
        snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = 
%d\n",
                   info->size, info->max_size, info->min_size, info->granularity);
        return 0;
}


/*
 * vx_get_pipe_state - get the state of a pipe
 * @pipe: the pipe to be checked
 * @state: the pointer for the returned state
 *
 * checks the state of a given pipe, and stores the state (1 = running,
 * 0 = paused) on the given pointer.
 *
 * called from trigger callback only
 */
static int vx_get_pipe_state(vx_core_t *chip, vx_pipe_t *pipe, int *state)
{
        int err;
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_PIPE_STATE);
        vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
        err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 
        if (! err)
                *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
        return err;
}


/*
 * vx_query_hbuffer_size - query available h-buffer size in bytes
 * @pipe: the pipe to be checked
 *
 * return the available size on h-buffer in bytes,
 * or a negative error code.
 */
static int vx_query_hbuffer_size(vx_core_t *chip, vx_pipe_t *pipe)
{
        int result;
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
        vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
        if (pipe->is_capture)
                rmh.Cmd[0] |= 0x00000001;
        result = vx_send_msg(chip, &rmh);
        if (! result)
                result = rmh.Stat[0] & 0xffff;
        return result;
}


/*
 * vx_pipe_can_start - query whether a pipe is ready for start
 * @pipe: the pipe to be checked
 *
 * return 1 if ready, 0 if not ready, and negative value on error.
 *
 * called from trigger callback only
 */
static int vx_pipe_can_start(vx_core_t *chip, vx_pipe_t *pipe)
{
        int err;
        struct vx_rmh rmh;
        
        vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
        vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
        rmh.Cmd[0] |= 1;

        err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 
        if (! err) {
                if (rmh.Stat[0])
                        err = 1;
        }
        return err;
}

/*
 * vx_vonf_pipe - tell the pipe to stand by and wait for IRQA.
 * @pipe: the pipe to be configured
 */
static int vx_conf_pipe(vx_core_t *chip, vx_pipe_t *pipe)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_CONF_PIPE);
        if (pipe->is_capture)
                rmh.Cmd[0] |= COMMAND_RECORD_MASK;
        rmh.Cmd[1] = 1 << pipe->number;
        return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
}

/*
 * vx_send_irqa - trigger IRQA
 */
static int vx_send_irqa(vx_core_t *chip)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_SEND_IRQA);
        return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 
}

#define MAX_WAIT_FOR_DSP        250
#define CAN_START_DELAY         10
#define WAIT_STATE_DELAY        10

/*
 * vx_toggle_pipe - start / pause a pipe
 * @pipe: the pipe to be triggered
 * @state: start = 1, pause = 0
 *
 * called from trigger callback only
 *
 */
static int vx_toggle_pipe(vx_core_t *chip, vx_pipe_t *pipe, int state)
{
        int err, i, cur_state, delay;

        /* Check the pipe is not already in the requested state */
        if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
                return -EBADFD;
        if (state == cur_state)
                return 0;

        /* If a start is requested, ask the DSP to get prepared
         * and wait for a positive acknowledge (when there are
         * enough sound buffer for this pipe)
         */
        if (state) {
                int delay = CAN_START_DELAY;
                for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
                        err = vx_pipe_can_start(chip, pipe);
                        if (err > 0)
                                break;
                        /* Wait for a few, before asking again
                         * to avoid flooding the DSP with our requests
                         */
                        if ((i % 4 ) == 0)
                                delay <<= 1;
                        snd_vx_delay(chip, delay);
                }
        }
    
        if ((err = vx_conf_pipe(chip, pipe)) < 0)
                return err;

        if ((err = vx_send_irqa(chip)) < 0)
                return err;
    
        /* If it completes successfully, wait for the pipes
         * reaching the expected state before returning
         * Check one pipe only (since they are synchronous)
         */
        delay = WAIT_STATE_DELAY;
        for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
                err = vx_get_pipe_state(chip, pipe, &cur_state);
                if (err < 0 || cur_state == state)
                        break;
                err = -EIO;
                if ((i % 4 ) == 0)
                        delay <<= 1;
                snd_vx_delay(chip, delay);
        }
        return err < 0 ? -EIO : 0;
}

    
/*
 * vx_stop_pipe - stop a pipe
 * @pipe: the pipe to be stopped
 *
 * called from trigger callback only
 */
static int vx_stop_pipe(vx_core_t *chip, vx_pipe_t *pipe)
{
        struct vx_rmh rmh;
        vx_init_rmh(&rmh, CMD_STOP_PIPE);
        vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
        return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 
}


/*
 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
 * @capture: 0 = playback, 1 = capture operation
 * @audioid: the audio id to be assigned
 * @num_audio: number of audio channels
 * @pipep: the returned pipe instance
 *
 * return 0 on success, or a negative error code.
 */
static int vx_alloc_pipe(vx_core_t *chip, int capture,
                         int audioid, int num_audio,
                         vx_pipe_t **pipep)
{
        int err;
        vx_pipe_t *pipe;
        struct vx_rmh rmh;
        int data_mode;

        *pipep = 0;
        vx_init_rmh(&rmh, CMD_RES_PIPE);
        vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
#if 0   // NYI
        if (underrun_skip_sound)
                rmh.Cmd[0] |= BIT_SKIP_SOUND;
#endif  // NYI
        data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
        if (! capture && data_mode)
                rmh.Cmd[0] |= BIT_DATA_MODE;
        err = vx_send_msg(chip, &rmh);
        if (err < 0)
                return err;

        /* initialize the pipe record */
        pipe = snd_magic_kcalloc(vx_pipe_t, 0, GFP_KERNEL);
        if (! pipe) {
                /* release the pipe */
                vx_init_rmh(&rmh, CMD_FREE_PIPE);
                vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
                vx_send_msg(chip, &rmh);
                return -ENOMEM;
        }

        /* the pipe index should be identical with the audio index */
        pipe->number = audioid;
        pipe->is_capture = capture;
        pipe->channels = num_audio;
        pipe->differed_type = 0;
        pipe->pcx_time = 0;
        pipe->data_mode = data_mode;
        *pipep = pipe;

        return 0;
}


/*
 * vx_free_pipe - release a pipe
 * @pipe: pipe to be released
 */
static int vx_free_pipe(vx_core_t *chip, vx_pipe_t *pipe)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_FREE_PIPE);
        vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
        vx_send_msg(chip, &rmh);

        snd_magic_kfree(pipe);
        return 0;
}


/*
 * vx_start_stream - start the stream
 *
 * called from trigger callback only
 */
static int vx_start_stream(vx_core_t *chip, vx_pipe_t *pipe)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
        vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
        vx_set_differed_time(chip, &rmh, pipe);
        return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 
}


/*
 * vx_stop_stream - stop the stream
 *
 * called from trigger callback only
 */
static int vx_stop_stream(vx_core_t *chip, vx_pipe_t *pipe)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_STOP_STREAM);
        vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
        return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 
}


/*
 * playback hw information
 */

static snd_pcm_hardware_t vx_pcm_playback_hw = {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                                 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
        .formats =              /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE | 
SNDRV_PCM_FMTBIT_S24_3LE,
        .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
        .rate_min =             5000,
        .rate_max =             48000,
        .channels_min =         2,
        .channels_max =         2,
        .buffer_bytes_max =     (128*1024),
        .period_bytes_min =     126,
        .period_bytes_max =     (128*1024),
        .periods_min =          2,
        .periods_max =          VX_MAX_PERIODS,
        .fifo_size =            126,
};


static void vx_pcm_delayed_start(unsigned long arg);

/*
 * vx_pcm_playback_open - open callback for playback
 */
static int vx_pcm_playback_open(snd_pcm_substream_t *subs)
{
        snd_pcm_runtime_t *runtime = subs->runtime;
        vx_core_t *chip = snd_pcm_substream_chip(subs);
        vx_pipe_t *pipe;
        int audio, err;

        if (chip->is_stale)
                return -EBUSY;

        audio = subs->pcm->device * 2;
        snd_assert(audio < chip->audio_outs, return -EINVAL);
        err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo capture */
        if (err < 0)
                return err;
        pipe->substream = subs;
        tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
        chip->playback_pipes[audio] = pipe;

        runtime->hw = vx_pcm_playback_hw;
        runtime->hw.period_bytes_min = chip->ibl.size;
        runtime->private_data = pipe;

        return 0;
}

/*
 * vx_pcm_playback_close - close callback for playback
 */
static int vx_pcm_playback_close(snd_pcm_substream_t *subs)
{
        vx_core_t *chip = snd_pcm_substream_chip(subs);
        vx_pipe_t *pipe;

        if (! subs->runtime->private_data)
                return -EINVAL;
        pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, return -EINVAL);
        chip->playback_pipes[pipe->number] = 0;
        vx_free_pipe(chip, pipe);
        return 0;
}


/*
 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
 * @pipe: the pipe to notify
 *
 * NB: call with a certain lock.
 */
static int vx_notify_end_of_buffer(vx_core_t *chip, vx_pipe_t *pipe)
{
        int err;
        struct vx_rmh rmh;  /* use a temporary rmh here */

        /* Toggle Dsp Host Interface into Message mode */
        vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
        vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
        vx_set_stream_cmd_params(&rmh, 0, pipe->number);
        err = vx_send_msg_nolock(chip, &rmh);
        if (err < 0)
                return err;
        /* Toggle Dsp Host Interface back to sound transfer mode */
        vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
        return 0;
}

/*
 * vx_update_playback_buffer - update the playback buffer
 * @subs: substream
 * @pipe: the pipe to transfer
 *
 * transfer as much data as possible.
 * called from the interrupt handler, too.
 *
 * return 0 if ok.
 */
static int vx_update_playback_buffer(vx_core_t *chip, snd_pcm_runtime_t *runtime, 
vx_pipe_t *pipe)
{
        int size, space, err = 0;

        size = (int)runtime->control->appl_ptr - pipe->hw_ptr;
        if (size < 0)
                size += runtime->buffer_size;
        size = frames_to_bytes(runtime, size);
        if (! size) /* nothing to copy */
                return 0;
        space = vx_query_hbuffer_size(chip, pipe);
        if (space < 0) {
                /* disconnect the host, SIZE_HBUF command always switches to the 
stream mode */
                vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
                return space;
        }
        /* we don't need irqsave here, because this function
         * is called from either trigger callback or irq handler
         */
        spin_lock(&chip->lock); 
        if (space > size)
                space = size;
        space = (space / pipe->align) * pipe->align;
        if (! space)
                goto _finish;
        size = bytes_to_frames(runtime, space);
        vx_pseudo_dma_write(chip, runtime, pipe, space);
        pipe->chunk_transferred += size;
        if (pipe->chunk_transferred >= chip->ibl.min_size) {
                pipe->chunk_transferred = 0;
                err = vx_notify_end_of_buffer(chip, pipe);
        }
 _finish:
        /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
        vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
        spin_unlock(&chip->lock);
        return err;
}

/*
 * update the position of the given pipe.
 * pipe->position is updated and wrapped within the buffer size.
 * pipe->transferred is updated, too, but the size is not wrapped,
 * so that the caller can check the total transferred size later
 * (to call snd_pcm_period_elapsed).
 */
static int vx_update_pipe_position(vx_core_t *chip, snd_pcm_runtime_t *runtime, 
vx_pipe_t *pipe)
{
        struct vx_rmh rmh;
        int err, update;
        u64 count;

        vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
        vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
        err = vx_send_msg(chip, &rmh);
        if (err < 0)
                return err;

        count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
        update = (int)(count - pipe->cur_count);
        pipe->cur_count = count;
        pipe->position += update;
        if (pipe->position >= runtime->buffer_size)
                pipe->position %= runtime->buffer_size;
        pipe->transferred += update;
        return 0;
}

/*
 * transfer the pending playback buffer data to DSP
 * called from interrupt handler
 */
void vx_pcm_playback_update_buffer(vx_core_t *chip, snd_pcm_substream_t *subs, 
vx_pipe_t *pipe)
{
        int err;
        snd_pcm_runtime_t *runtime = subs->runtime;

        if (! pipe->prepared || chip->is_stale)
                return;
        if ((err = vx_update_playback_buffer(chip, runtime, pipe)) < 0)
                return;
}

/*
 * update the playback position and call snd_pcm_period_elapsed() if necessary
 * called from interrupt handler
 */
void vx_pcm_playback_update(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t 
*pipe)
{
        int err;
        snd_pcm_runtime_t *runtime = subs->runtime;

        if (pipe->running && ! chip->is_stale) {
                if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
                        return;
                if (pipe->transferred >= runtime->period_size) {
                        pipe->transferred %= runtime->period_size;
                        snd_pcm_period_elapsed(subs);
                }
        }
}

/*
 * start the stream and pipe.
 * this function is called from tasklet, which is invoked by the trigger
 * START callback.
 */
static void vx_pcm_delayed_start(unsigned long arg)
{
        snd_pcm_substream_t *subs = (snd_pcm_substream_t *)arg;
        vx_core_t *chip = snd_magic_cast(vx_core_t, subs->pcm->private_data, return);
        vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, 
return);
        int err;

        if ((err = vx_start_stream(chip, pipe)) < 0) {
                snd_printk(KERN_ERR "vx: cannot start stream\n");
                return;
        }
        if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
                snd_printk(KERN_ERR "vx: cannot start pipe\n");
                return;
        }
}

/*
 * vx_pcm_playback_trigger - trigger callback for playback
 */
static int vx_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
{
        vx_core_t *chip = snd_pcm_substream_chip(subs);
        vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, 
return -EINVAL);
        int err;

        if (chip->is_stale)
                return -EBUSY;

        switch (cmd) {
        case SNDRV_PCM_TRIGGER_START:
                if (! pipe->is_capture)
                        vx_update_playback_buffer(chip, subs->runtime, pipe);
                /* FIXME:
                 * we trigger the pipe using tasklet, so that the interrupts are
                 * issued surely after the trigger is completed.
                 */ 
                tasklet_hi_schedule(&pipe->start_tq);
                chip->pcm_running++;
                pipe->running = 1;
                break;
        case SNDRV_PCM_TRIGGER_STOP:
                vx_toggle_pipe(chip, pipe, 0);
                vx_stop_pipe(chip, pipe);
                vx_stop_stream(chip, pipe);
                chip->pcm_running--;
                pipe->running = 0;
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
                if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
                        return err;
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
                if (! pipe->is_capture)
                        vx_update_playback_buffer(chip, subs->runtime, pipe);
                if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
                        return err;
                break;
        default:
                return -EINVAL;
        }
        return 0;
}

/*
 * vx_pcm_playback_pointer - pointer callback for playback
 */
static snd_pcm_uframes_t vx_pcm_playback_pointer(snd_pcm_substream_t *subs)
{
        snd_pcm_runtime_t *runtime = subs->runtime;
        vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, runtime->private_data, return 
-EINVAL);
        return pipe->position;
}

/*
 * vx_pcm_hw_params - hw_params callback for playback and capture
 */
static int vx_pcm_hw_params(snd_pcm_substream_t *subs,
                                     snd_pcm_hw_params_t *hw_params)
{
        return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
}

/*
 * vx_pcm_hw_free - hw_free callback for playback and capture
 */
static int vx_pcm_hw_free(snd_pcm_substream_t *subs)
{
        return snd_pcm_free_vmalloc_buffer(subs);
}

/*
 * vx_pcm_prepare - prepare callback for playback and capture
 */
static int vx_pcm_prepare(snd_pcm_substream_t *subs)
{
        vx_core_t *chip = snd_pcm_substream_chip(subs);
        snd_pcm_runtime_t *runtime = subs->runtime;
        vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, runtime->private_data, return 
-EINVAL);
        int err, data_mode;
        // int max_size, nchunks;

        if (chip->is_stale)
                return -EBUSY;

        data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
        if (data_mode != pipe->data_mode && ! pipe->is_capture) {
                /* IEC958 status (raw-mode) was changed */
                /* we reopen the pipe */
                struct vx_rmh rmh;
                snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", 
data_mode);
                vx_init_rmh(&rmh, CMD_FREE_PIPE);
                vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
                if ((err = vx_send_msg(chip, &rmh)) < 0)
                        return err;
                vx_init_rmh(&rmh, CMD_RES_PIPE);
                vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
                if (data_mode)
                        rmh.Cmd[0] |= BIT_DATA_MODE;
                if ((err = vx_send_msg(chip, &rmh)) < 0)
                        return err;
                pipe->data_mode = data_mode;
        }

        if (chip->pcm_running && chip->freq != runtime->rate) {
                snd_printk(KERN_ERR "vx: cannot set different clock %d from the 
current %d\n", runtime->rate, chip->freq);
                return -EINVAL;
        }
        vx_set_clock(chip, runtime->rate);

        if ((err = vx_set_format(chip, pipe, runtime)) < 0)
                return err;

        if (chip->type >= VX_TYPE_VXPOCKET) {
                /* minimal DMA alignment = 6 */
                if (snd_pcm_format_physical_width(runtime->format) == 16)
                        pipe->align = runtime->channels * 6;
                else
                        pipe->align = 6;
        } else {
                /* minimal DMA alignment = 12 */
                pipe->align = 12;
        }

        pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
        pipe->hw_ptr = 0;
        pipe->chunk_transferred = 0;

        /* set the timestamp */
        vx_update_pipe_position(chip, runtime, pipe);
        /* clear again */
        pipe->transferred = 0;
        pipe->position = 0;

        pipe->prepared = 1;

        return 0;
}


/*
 * operators for PCM playback
 */
static snd_pcm_ops_t vx_pcm_playback_ops = {
        .open =         vx_pcm_playback_open,
        .close =        vx_pcm_playback_close,
        .ioctl =        snd_pcm_lib_ioctl,
        .hw_params =    vx_pcm_hw_params,
        .hw_free =      vx_pcm_hw_free,
        .prepare =      vx_pcm_prepare,
        .trigger =      vx_pcm_trigger,
        .pointer =      vx_pcm_playback_pointer,
        .page =         snd_pcm_get_vmalloc_page,
};


/*
 * playback hw information
 */

static snd_pcm_hardware_t vx_pcm_capture_hw = {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                                 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
        .formats =              /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE | 
SNDRV_PCM_FMTBIT_S24_3LE,
        .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
        .rate_min =             5000,
        .rate_max =             48000,
        .channels_min =         2,
        .channels_max =         2,
        .buffer_bytes_max =     (128*1024),
        .period_bytes_min =     126,
        .period_bytes_max =     (128*1024),
        .periods_min =          2,
        .periods_max =          VX_MAX_PERIODS,
        .fifo_size =            126,
};


/*
 * vx_pcm_capture_open - open callback for capture
 */
static int vx_pcm_capture_open(snd_pcm_substream_t *subs)
{
        snd_pcm_runtime_t *runtime = subs->runtime;
        vx_core_t *chip = snd_pcm_substream_chip(subs);
        vx_pipe_t *pipe;
        int audio, err;

        if (chip->is_stale)
                return -EBUSY;

        audio = subs->pcm->device * 2;
        snd_assert(audio < chip->audio_ins, return -EINVAL);
        err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
        if (err < 0)
                return err;
        pipe->substream = subs;
        tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
        chip->capture_pipes[audio] = pipe;

        runtime->hw = vx_pcm_capture_hw;
        runtime->hw.period_bytes_min = chip->ibl.size;
        runtime->private_data = pipe;

        return 0;
}

/*
 * vx_pcm_capture_close - close callback for capture
 */
static int vx_pcm_capture_close(snd_pcm_substream_t *subs)
{
        vx_core_t *chip = snd_pcm_substream_chip(subs);
        vx_pipe_t *pipe;

        if (! subs->runtime->private_data)
                return -EINVAL;
        pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, return -EINVAL);
        chip->capture_pipes[pipe->number] = 0;
        vx_free_pipe(chip, pipe);
        return 0;
}


/*
 * vx_pcm_capture_update - update the capture buffer
 */
void vx_pcm_capture_update(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe)
{
        int size, space;
        snd_pcm_runtime_t *runtime = subs->runtime;

        if (! pipe->prepared || chip->is_stale)
                return;

        size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
        if (! size)
                return;
        size = frames_to_bytes(runtime, size);
        space = vx_query_hbuffer_size(chip, pipe);
        if (space < 0) {
                /* disconnect the host, SIZE_HBUF command always switches to the 
stream mode */
                vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
                return;
        }
        if (size > space)
                size = space;
        size = (size / pipe->align) * pipe->align;
        if (! size)
                goto _finish;
#if 0
        if (size > pipe->align)
                vx_pseudo_dma_read(chip, runtime, pipe, size - pipe->align);
 _finish:
        /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
        vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
        vx_flush_read(chip, runtime, pipe, pipe->align);
#else
        vx_pseudo_dma_read(chip, runtime, pipe, size);
 _finish:
        vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
#endif
        pipe->transferred += bytes_to_frames(runtime, size);
        if (pipe->transferred >= runtime->period_size) {
                pipe->transferred %= runtime->period_size;
                snd_pcm_period_elapsed(subs);
        }
}

/*
 * vx_pcm_capture_pointer - pointer callback for capture
 */
static snd_pcm_uframes_t vx_pcm_capture_pointer(snd_pcm_substream_t *subs)
{
        snd_pcm_runtime_t *runtime = subs->runtime;
        vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, runtime->private_data, return 
-EINVAL);
        return pipe->hw_ptr;
}

/*
 * operators for PCM capture
 */
static snd_pcm_ops_t vx_pcm_capture_ops = {
        .open =         vx_pcm_capture_open,
        .close =        vx_pcm_capture_close,
        .ioctl =        snd_pcm_lib_ioctl,
        .hw_params =    vx_pcm_hw_params,
        .hw_free =      vx_pcm_hw_free,
        .prepare =      vx_pcm_prepare,
        .trigger =      vx_pcm_trigger,
        .pointer =      vx_pcm_capture_pointer,
        .page =         snd_pcm_get_vmalloc_page,
};


/*
 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
 */
static int vx_init_audio_io(vx_core_t *chip)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_SUPPORTED);
        if (vx_send_msg(chip, &rmh) < 0) {
                snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
                return -ENXIO;
        }

        chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
        chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
        chip->audio_info = rmh.Stat[1];

        /* allocate pipes */
        chip->playback_pipes = kmalloc(sizeof(vx_pipe_t *) * chip->audio_outs, 
GFP_KERNEL);
        chip->capture_pipes = kmalloc(sizeof(vx_pipe_t *) * chip->audio_ins, 
GFP_KERNEL);
        if (! chip->playback_pipes || ! chip->capture_pipes)
                return -ENOMEM;

        memset(chip->playback_pipes, 0, sizeof(vx_pipe_t *) * chip->audio_outs);
        memset(chip->capture_pipes, 0, sizeof(vx_pipe_t *) * chip->audio_ins);

        vx_set_ibl(chip, &chip->ibl); /* query the info */
        chip->ibl.size = chip->ibl.min_size;
        vx_set_ibl(chip, &chip->ibl); /* set to the minimum */

        return 0;
}


/*
 * free callback for pcm
 */
static void snd_vxpocket_pcm_free(snd_pcm_t *pcm)
{
        vx_core_t *chip = snd_magic_cast(vx_core_t, pcm->private_data, return);
        chip->pcm[pcm->device] = NULL;
        if (chip->playback_pipes) {
                kfree(chip->playback_pipes);
                chip->playback_pipes = 0;
        }
        if (chip->capture_pipes) {
                kfree(chip->capture_pipes);
                chip->capture_pipes = 0;
        }
}

/*
 * snd_vx_pcm_new - create and initialize a pcm
 */
int snd_vx_pcm_new(vx_core_t *chip)
{
        snd_pcm_t *pcm;
        int i, err;

        if ((err = vx_init_audio_io(chip)) < 0)
                return err;

        for (i = 0; i < chip->hw->num_codecs; i++) {
                int outs, ins;
                outs = chip->audio_outs > i * 2 ? 1 : 0;
                ins = chip->audio_ins > i * 2 ? 1 : 0;
                if (! outs && ! ins)
                        break;
                err = snd_pcm_new(chip->card, "VXPocket", i,
                                  outs, ins, &pcm);
                if (err < 0)
                        return err;
                if (outs)
                        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 
&vx_pcm_playback_ops);
                if (ins)
                        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, 
&vx_pcm_capture_ops);

                pcm->private_data = chip;
                pcm->private_free = snd_vxpocket_pcm_free;
                pcm->info_flags = 0;
                strcpy(pcm->name, chip->card->shortname);
                chip->pcm[i] = pcm;
        }

        return 0;
}


#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)

static struct page *vmalloc_to_page(void *pageptr)
{
        pgd_t *pgd;
        pmd_t *pmd;
        pte_t *pte;
        unsigned long lpage;
        struct page *page = (struct page *)NOPAGE_SIGBUS;

        lpage = VMALLOC_VMADDR(pageptr);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,18)
        spin_lock(&init_mm.page_table_lock);
#endif
        pgd = pgd_offset(&init_mm, lpage);
        pmd = pmd_offset(pgd, lpage);
        pte = pte_offset(pmd, lpage);
        page = (struct page *)pte_page(*pte);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,18)
        spin_unlock(&init_mm.page_table_lock);
#endif

        return page;
}    
#endif

--- NEW FILE: vx_uer.c ---
/* to be in alsa-driver-specfici code */
#define __NO_VERSION__

/*
 * Driver for Digigram VX soundcards
 *
 * IEC958 stuff
 *
 * Copyright (c) 2002 by Takashi Iwai <[EMAIL PROTECTED]>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <sound/driver.h>
#include <linux/delay.h>
#include <sound/core.h>
#include "vx_core.h"
#include "vx_cmd.h"


/*
 * vx_modify_board_clock - tell the board that its clock has been modified
 * @sync: DSP needs to resynchronize its FIFO
 */
static int vx_modify_board_clock(vx_core_t *chip, int sync)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_MODIFY_CLOCK);
        /* Ask the DSP to resynchronize its FIFO. */
        if (sync)
                rmh.Cmd[0] |= CMD_MODIFY_CLOCK_S_BIT;
        return vx_send_msg(chip, &rmh);
}

/*
 * vx_modify_board_inputs - resync audio inputs
 */
static int vx_modify_board_inputs(vx_core_t *chip)
{
        struct vx_rmh rmh;

        vx_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
        rmh.Cmd[0] |= 1 << 0; /* reference: AUDIO 0 */
        return vx_send_msg(chip, &rmh);
}

/*
 * vx_read_one_cbit - read one bit from UER config
 * @index: the bit index
 * returns 0 or 1.
 */
static int vx_read_one_cbit(vx_core_t *chip, int index)
{
        unsigned long flags;
        int val;
        spin_lock_irqsave(&chip->lock, flags);
        if (chip->type >= VX_TYPE_VXPOCKET) {
                vx_outb(chip, CSUER, 1); /* read */
                vx_outb(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
                val = (vx_inb(chip, RUER) >> 7) & 0x01;
        } else {
                vx_outl(chip, CSUER, 1); /* read */
                vx_outl(chip, RUER, index & XX_UER_CBITS_OFFSET_MASK);
                val = (vx_inl(chip, RUER) >> 7) & 0x01;
        }
        spin_unlock_irqrestore(&chip->lock, flags);
        return val;
}

/*
 * vx_write_one_cbit - write one bit to UER config
 * @index: the bit index
 * @val: bit value, 0 or 1
 */
static void vx_write_one_cbit(vx_core_t *chip, int index, int val)
{
        unsigned long flags;
        val = !!val;
        spin_lock_irqsave(&chip->lock, flags);
        if (chip->type >= VX_TYPE_VXPOCKET) {
                vx_outb(chip, CSUER, 0); /* write */
                vx_outb(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
        } else {
                vx_outl(chip, CSUER, 0); /* write */
                vx_outl(chip, RUER, (val << 7) | (index & XX_UER_CBITS_OFFSET_MASK));
        }
        spin_unlock_irqrestore(&chip->lock, flags);
}

/*
 * vx_read_uer_status - read the current UER status
 * @mode: pointer to store the UER mode, VX_UER_MODE_XXX
 *
 * returns the frequency of UER, or 0 if not sync,
 * or a negative error code.
 */
static int vx_read_uer_status(vx_core_t *chip, int *mode)
{
        int val, freq;

        /* Default values */
        freq = 0;

        /* Read UER status */
        if (chip->type >= VX_TYPE_VXPOCKET)
            val = vx_inb(chip, CSUER);
        else
            val = vx_inl(chip, CSUER);
        if (val < 0)
                return val;
        /* If clock is present, read frequency */
        if (val & VX_SUER_CLOCK_PRESENT_MASK) {
                switch (val & VX_SUER_FREQ_MASK) {
                case VX_SUER_FREQ_32KHz_MASK:
                        freq = 32000;
                        break;
                case VX_SUER_FREQ_44KHz_MASK:
                        freq = 44100;
                        break;
                case VX_SUER_FREQ_48KHz_MASK:
                        freq = 48000;
                        break;
                }
        }
        if (val & VX_SUER_DATA_PRESENT_MASK)
                /* bit 0 corresponds to consumer/professional bit */
                *mode = vx_read_one_cbit(chip, 0) ?
                        VX_UER_MODE_PROFESSIONAL : VX_UER_MODE_CONSUMER;
        else
                *mode = VX_UER_MODE_NOT_PRESENT;

        return freq;
}


/*
 * compute the sample clock value from frequency
 *
 * The formula is as follows:
 *
 *    HexFreq = (dword) ((double) ((double) 28224000 / (double) Frequency))
 *    switch ( HexFreq & 0x00000F00 )
 *    case 0x00000100: ;
 *    case 0x00000200:
 *    case 0x00000300: HexFreq -= 0x00000201 ;
 *    case 0x00000400:
 *    case 0x00000500:
 *    case 0x00000600:
 *    case 0x00000700: HexFreq = (dword) (((double) 28224000 / (double) (Frequency*2)) 
- 1)
 *    default        : HexFreq = (dword) ((double) 28224000 / (double) (Frequency*4)) 
- 0x000001FF
 */

static int vx_calc_clock_from_freq(vx_core_t *chip, int freq)
{
#define XX_FECH48000                    0x0000004B
#define XX_FECH32000                    0x00000171
#define XX_FECH24000                    0x0000024B
#define XX_FECH16000                    0x00000371
#define XX_FECH12000                    0x0000044B
#define XX_FECH8000                     0x00000571
#define XX_FECH44100                    0x0000007F
#define XX_FECH29400                    0x0000016F
#define XX_FECH22050                    0x0000027F
#define XX_FECH14000                    0x000003EF
#define XX_FECH11025                    0x0000047F
#define XX_FECH7350                     0x000005BF

        switch (freq) {
        case 48000:     return XX_FECH48000;
        case 44100:     return XX_FECH44100;
        case 32000:     return XX_FECH32000;
        case 29400:     return XX_FECH29400;
        case 24000:     return XX_FECH24000;
        case 22050:     return XX_FECH22050;
        case 16000:     return XX_FECH16000;
        case 14000:     return XX_FECH14000;
        case 12000:     return XX_FECH12000;
        case 11025:     return XX_FECH11025;
        case 8000:      return XX_FECH8000;
        case 7350:      return XX_FECH7350;
        default:        return freq;   /* The value is already correct */
        }
}


/*
 * vx_change_clock_source - change the clock source
 * @source: the new source
 */
static void vx_change_clock_source(vx_core_t *chip, int source)
{
        unsigned long flags;

        /* we mute DAC to prevent clicks */
        vx_toggle_dac_mute(chip, 1);
        spin_lock_irqsave(&chip->lock, flags);
        chip->ops->set_clock_source(chip, source);
        chip->clock_source = source;
        spin_unlock_irqrestore(&chip->lock, flags);
        /* unmute */
        vx_toggle_dac_mute(chip, 0);
}


/*
 * set the internal clock
 */
void vx_set_internal_clock(vx_core_t *chip, int freq)
{
        int clock;
        unsigned long flags;
        /* Get real clock value */
        clock = vx_calc_clock_from_freq(chip, freq);
        snd_printdd(KERN_DEBUG "set internal clock to 0x%x from freq %d\n", clock, 
freq);
        spin_lock_irqsave(&chip->lock, flags);
        if (chip->type >= VX_TYPE_VXPOCKET) {
                vx_outb(chip, HIFREQ, (clock >> 8) & 0x0f);
                vx_outb(chip, LOFREQ, clock & 0xff);
        } else {
                vx_outl(chip, HIFREQ, (clock >> 8) & 0x0f);
                vx_outl(chip, LOFREQ, clock & 0xff);
        }
        spin_unlock_irqrestore(&chip->lock, flags);
}


/*
 * set the iec958 status bits
 * @bits: 32-bit status bits
 */
void vx_set_iec958_status(vx_core_t *chip, unsigned int bits)
{
        int i;

        if (chip->is_stale)
                return;

        for (i = 0; i < 32; i++)
                vx_write_one_cbit(chip, i, bits & (1 << i));
}


/*
 * vx_set_clock - change the clock and audio source if necessary
 */
int vx_set_clock(vx_core_t *chip, int freq)
{
        int src_changed = 0;

        if (chip->is_stale)
                return 0;

        /* change the audio source if possible */
        vx_sync_audio_source(chip);

        switch (chip->audio_source) {
        case VX_AUDIO_SRC_DIGITAL:
                if (chip->clock_source != UER_SYNC) {
                        vx_change_clock_source(chip, UER_SYNC);
                        mdelay(6);
                        src_changed = 1;
                }
                if (chip->freq == freq)
                        return 0;
                break;
        default:
                if (chip->clock_source != INTERNAL_QUARTZ) {
                        vx_change_clock_source(chip, INTERNAL_QUARTZ);
                        src_changed = 1;
                }
                if (chip->freq == freq)
                        return 0;
                vx_set_internal_clock(chip, freq);
                if (src_changed)
                        vx_modify_board_inputs(chip);
                break;
        }
        chip->freq = freq;
        vx_modify_board_clock(chip, 1);
        return 0;
}


/*
 * vx_change_frequency - called from interrupt handler
 */
int vx_change_frequency(vx_core_t *chip)
{
        int freq;

        if (chip->is_stale)
                return 0;

        if (chip->clock_source == INTERNAL_QUARTZ)
                return 0;
        /*
         * Read the real UER board frequency
         */
        freq = vx_read_uer_status(chip, &chip->uer_detected);
        if (freq < 0)
                return freq;
        /*
         * The frequency computed by the DSP is good and
         * is different from the previous computed.
         */
        if (freq == 48000 || freq == 44100 || freq == 32000)
                chip->freq_detected = freq;

        return 0;
}



-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
Alsa-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/alsa-cvslog

Reply via email to