Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   ALSA driver for RME Digi9652 audio interfaces 
0004  *
0005  *  Copyright (c) 1999 IEM - Winfried Ritsch
0006  *      Copyright (c) 1999-2001  Paul Davis
0007  */
0008 
0009 #include <linux/delay.h>
0010 #include <linux/init.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/pci.h>
0013 #include <linux/module.h>
0014 #include <linux/io.h>
0015 #include <linux/nospec.h>
0016 
0017 #include <sound/core.h>
0018 #include <sound/control.h>
0019 #include <sound/pcm.h>
0020 #include <sound/info.h>
0021 #include <sound/asoundef.h>
0022 #include <sound/initval.h>
0023 
0024 #include <asm/current.h>
0025 
0026 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;  /* Index 0-MAX */
0027 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;   /* ID for this card */
0028 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
0029 static bool precise_ptr[SNDRV_CARDS];           /* Enable precise pointer */
0030 
0031 module_param_array(index, int, NULL, 0444);
0032 MODULE_PARM_DESC(index, "Index value for RME Digi9652 (Hammerfall) soundcard.");
0033 module_param_array(id, charp, NULL, 0444);
0034 MODULE_PARM_DESC(id, "ID string for RME Digi9652 (Hammerfall) soundcard.");
0035 module_param_array(enable, bool, NULL, 0444);
0036 MODULE_PARM_DESC(enable, "Enable/disable specific RME96{52,36} soundcards.");
0037 module_param_array(precise_ptr, bool, NULL, 0444);
0038 MODULE_PARM_DESC(precise_ptr, "Enable precise pointer (doesn't work reliably).");
0039 MODULE_AUTHOR("Paul Davis <pbd@op.net>, Winfried Ritsch");
0040 MODULE_DESCRIPTION("RME Digi9652/Digi9636");
0041 MODULE_LICENSE("GPL");
0042 
0043 /* The Hammerfall has two sets of 24 ADAT + 2 S/PDIF channels, one for
0044    capture, one for playback. Both the ADAT and S/PDIF channels appear
0045    to the host CPU in the same block of memory. There is no functional
0046    difference between them in terms of access.
0047    
0048    The Hammerfall Light is identical to the Hammerfall, except that it
0049    has 2 sets 18 channels (16 ADAT + 2 S/PDIF) for capture and playback.
0050 */
0051 
0052 #define RME9652_NCHANNELS       26
0053 #define RME9636_NCHANNELS       18
0054 
0055 /* Preferred sync source choices - used by "sync_pref" control switch */
0056 
0057 #define RME9652_SYNC_FROM_SPDIF 0
0058 #define RME9652_SYNC_FROM_ADAT1 1
0059 #define RME9652_SYNC_FROM_ADAT2 2
0060 #define RME9652_SYNC_FROM_ADAT3 3
0061 
0062 /* Possible sources of S/PDIF input */
0063 
0064 #define RME9652_SPDIFIN_OPTICAL 0   /* optical (ADAT1) */
0065 #define RME9652_SPDIFIN_COAXIAL 1   /* coaxial (RCA) */
0066 #define RME9652_SPDIFIN_INTERN  2   /* internal (CDROM) */
0067 
0068 /* ------------- Status-Register bits --------------------- */
0069 
0070 #define RME9652_IRQ    (1<<0)   /* IRQ is High if not reset by irq_clear */
0071 #define RME9652_lock_2     (1<<1)   /* ADAT 3-PLL: 1=locked, 0=unlocked */
0072 #define RME9652_lock_1     (1<<2)   /* ADAT 2-PLL: 1=locked, 0=unlocked */
0073 #define RME9652_lock_0     (1<<3)   /* ADAT 1-PLL: 1=locked, 0=unlocked */
0074 #define RME9652_fs48       (1<<4)   /* sample rate is 0=44.1/88.2,1=48/96 Khz */
0075 #define RME9652_wsel_rd    (1<<5)   /* if Word-Clock is used and valid then 1 */
0076                                         /* bits 6-15 encode h/w buffer pointer position */
0077 #define RME9652_sync_2     (1<<16)  /* if ADAT-IN 3 in sync to system clock */
0078 #define RME9652_sync_1     (1<<17)  /* if ADAT-IN 2 in sync to system clock */
0079 #define RME9652_sync_0     (1<<18)  /* if ADAT-IN 1 in sync to system clock */
0080 #define RME9652_DS_rd      (1<<19)  /* 1=Double Speed Mode, 0=Normal Speed */
0081 #define RME9652_tc_busy    (1<<20)  /* 1=time-code copy in progress (960ms) */
0082 #define RME9652_tc_out     (1<<21)  /* time-code out bit */
0083 #define RME9652_F_0    (1<<22)  /* 000=64kHz, 100=88.2kHz, 011=96kHz  */
0084 #define RME9652_F_1    (1<<23)  /* 111=32kHz, 110=44.1kHz, 101=48kHz, */
0085 #define RME9652_F_2    (1<<24)  /* external Crystal Chip if ERF=1 */
0086 #define RME9652_ERF    (1<<25)  /* Error-Flag of SDPIF Receiver (1=No Lock) */
0087 #define RME9652_buffer_id  (1<<26)  /* toggles by each interrupt on rec/play */
0088 #define RME9652_tc_valid   (1<<27)  /* 1 = a signal is detected on time-code input */
0089 #define RME9652_SPDIF_READ (1<<28)      /* byte available from Rev 1.5+ S/PDIF interface */
0090 
0091 #define RME9652_sync      (RME9652_sync_0|RME9652_sync_1|RME9652_sync_2)
0092 #define RME9652_lock      (RME9652_lock_0|RME9652_lock_1|RME9652_lock_2)
0093 #define RME9652_F     (RME9652_F_0|RME9652_F_1|RME9652_F_2)
0094 #define rme9652_decode_spdif_rate(x) ((x)>>22)
0095 
0096 /* Bit 6..15 : h/w buffer pointer */
0097 
0098 #define RME9652_buf_pos   0x000FFC0
0099 
0100 /* Bits 31,30,29 are bits 5,4,3 of h/w pointer position on later
0101    Rev G EEPROMS and Rev 1.5 cards or later.
0102 */ 
0103 
0104 #define RME9652_REV15_buf_pos(x) ((((x)&0xE0000000)>>26)|((x)&RME9652_buf_pos))
0105 
0106 /* amount of io space we remap for register access. i'm not sure we
0107    even need this much, but 1K is nice round number :)
0108 */
0109 
0110 #define RME9652_IO_EXTENT     1024
0111 
0112 #define RME9652_init_buffer       0
0113 #define RME9652_play_buffer       32    /* holds ptr to 26x64kBit host RAM */
0114 #define RME9652_rec_buffer        36    /* holds ptr to 26x64kBit host RAM */
0115 #define RME9652_control_register  64
0116 #define RME9652_irq_clear         96
0117 #define RME9652_time_code         100   /* useful if used with alesis adat */
0118 #define RME9652_thru_base         128   /* 132...228 Thru for 26 channels */
0119 
0120 /* Read-only registers */
0121 
0122 /* Writing to any of the register locations writes to the status
0123    register. We'll use the first location as our point of access.
0124 */
0125 
0126 #define RME9652_status_register    0
0127 
0128 /* --------- Control-Register Bits ---------------- */
0129 
0130 
0131 #define RME9652_start_bit      (1<<0)   /* start record/play */
0132                                                 /* bits 1-3 encode buffersize/latency */
0133 #define RME9652_Master         (1<<4)   /* Clock Mode Master=1,Slave/Auto=0 */
0134 #define RME9652_IE         (1<<5)   /* Interrupt Enable */
0135 #define RME9652_freq           (1<<6)       /* samplerate 0=44.1/88.2, 1=48/96 kHz */
0136 #define RME9652_freq1          (1<<7)       /* if 0, 32kHz, else always 1 */
0137 #define RME9652_DS                 (1<<8)   /* Doule Speed 0=44.1/48, 1=88.2/96 Khz */
0138 #define RME9652_PRO        (1<<9)   /* S/PDIF out: 0=consumer, 1=professional */
0139 #define RME9652_EMP        (1<<10)  /*  Emphasis 0=None, 1=ON */
0140 #define RME9652_Dolby          (1<<11)  /*  Non-audio bit 1=set, 0=unset */
0141 #define RME9652_opt_out            (1<<12)  /* Use 1st optical OUT as SPDIF: 1=yes,0=no */
0142 #define RME9652_wsel           (1<<13)  /* use Wordclock as sync (overwrites master) */
0143 #define RME9652_inp_0          (1<<14)  /* SPDIF-IN: 00=optical (ADAT1),     */
0144 #define RME9652_inp_1          (1<<15)  /* 01=koaxial (Cinch), 10=Internal CDROM */
0145 #define RME9652_SyncPref_ADAT2     (1<<16)
0146 #define RME9652_SyncPref_ADAT3     (1<<17)
0147 #define RME9652_SPDIF_RESET        (1<<18)      /* Rev 1.5+: h/w S/PDIF receiver */
0148 #define RME9652_SPDIF_SELECT       (1<<19)
0149 #define RME9652_SPDIF_CLOCK        (1<<20)
0150 #define RME9652_SPDIF_WRITE        (1<<21)
0151 #define RME9652_ADAT1_INTERNAL     (1<<22)      /* Rev 1.5+: if set, internal CD connector carries ADAT */
0152 
0153 /* buffersize = 512Bytes * 2^n, where n is made from Bit2 ... Bit0 */
0154 
0155 #define RME9652_latency            0x0e
0156 #define rme9652_encode_latency(x)  (((x)&0x7)<<1)
0157 #define rme9652_decode_latency(x)  (((x)>>1)&0x7)
0158 #define rme9652_running_double_speed(s) ((s)->control_register & RME9652_DS)
0159 #define RME9652_inp                (RME9652_inp_0|RME9652_inp_1)
0160 #define rme9652_encode_spdif_in(x) (((x)&0x3)<<14)
0161 #define rme9652_decode_spdif_in(x) (((x)>>14)&0x3)
0162 
0163 #define RME9652_SyncPref_Mask      (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
0164 #define RME9652_SyncPref_ADAT1     0
0165 #define RME9652_SyncPref_SPDIF     (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
0166 
0167 /* the size of a substream (1 mono data stream) */
0168 
0169 #define RME9652_CHANNEL_BUFFER_SAMPLES  (16*1024)
0170 #define RME9652_CHANNEL_BUFFER_BYTES    (4*RME9652_CHANNEL_BUFFER_SAMPLES)
0171 
0172 /* the size of the area we need to allocate for DMA transfers. the
0173    size is the same regardless of the number of channels - the 
0174    9636 still uses the same memory area.
0175 
0176    Note that we allocate 1 more channel than is apparently needed
0177    because the h/w seems to write 1 byte beyond the end of the last
0178    page. Sigh.
0179 */
0180 
0181 #define RME9652_DMA_AREA_BYTES ((RME9652_NCHANNELS+1) * RME9652_CHANNEL_BUFFER_BYTES)
0182 #define RME9652_DMA_AREA_KILOBYTES (RME9652_DMA_AREA_BYTES/1024)
0183 
0184 struct snd_rme9652 {
0185     int dev;
0186 
0187     spinlock_t lock;
0188     int irq;
0189     unsigned long port;
0190     void __iomem *iobase;
0191     
0192     int precise_ptr;
0193 
0194     u32 control_register;   /* cached value */
0195     u32 thru_bits;      /* thru 1=on, 0=off channel 1=Bit1... channel 26= Bit26 */
0196 
0197     u32 creg_spdif;
0198     u32 creg_spdif_stream;
0199 
0200     char *card_name;        /* hammerfall or hammerfall light names */
0201 
0202         size_t hw_offsetmask;       /* &-with status register to get real hw_offset */
0203     size_t prev_hw_offset;      /* previous hw offset */
0204     size_t max_jitter;      /* maximum jitter in frames for 
0205                        hw pointer */
0206     size_t period_bytes;        /* guess what this is */
0207 
0208     unsigned char ds_channels;
0209     unsigned char ss_channels;  /* different for hammerfall/hammerfall-light */
0210 
0211     /* DMA buffers; those are copied instances from the original snd_dma_buf
0212      * objects (which are managed via devres) for the address alignments
0213      */
0214     struct snd_dma_buffer playback_dma_buf;
0215     struct snd_dma_buffer capture_dma_buf;
0216 
0217     unsigned char *capture_buffer;  /* suitably aligned address */
0218     unsigned char *playback_buffer; /* suitably aligned address */
0219 
0220     pid_t capture_pid;
0221     pid_t playback_pid;
0222 
0223     struct snd_pcm_substream *capture_substream;
0224     struct snd_pcm_substream *playback_substream;
0225     int running;
0226 
0227         int passthru;                   /* non-zero if doing pass-thru */
0228         int hw_rev;                     /* h/w rev * 10 (i.e. 1.5 has hw_rev = 15) */
0229 
0230     int last_spdif_sample_rate; /* so that we can catch externally ... */
0231     int last_adat_sample_rate;  /* ... induced rate changes            */
0232 
0233     const char *channel_map;
0234 
0235     struct snd_card *card;
0236     struct snd_pcm *pcm;
0237     struct pci_dev *pci;
0238     struct snd_kcontrol *spdif_ctl;
0239 
0240 };
0241 
0242 /* These tables map the ALSA channels 1..N to the channels that we
0243    need to use in order to find the relevant channel buffer. RME
0244    refer to this kind of mapping as between "the ADAT channel and
0245    the DMA channel." We index it using the logical audio channel,
0246    and the value is the DMA channel (i.e. channel buffer number)
0247    where the data for that channel can be read/written from/to.
0248 */
0249 
0250 static const char channel_map_9652_ss[26] = {
0251     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
0252     18, 19, 20, 21, 22, 23, 24, 25
0253 };
0254 
0255 static const char channel_map_9636_ss[26] = {
0256     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
0257     /* channels 16 and 17 are S/PDIF */
0258     24, 25,
0259     /* channels 18-25 don't exist */
0260     -1, -1, -1, -1, -1, -1, -1, -1
0261 };
0262 
0263 static const char channel_map_9652_ds[26] = {
0264     /* ADAT channels are remapped */
0265     1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
0266     /* channels 12 and 13 are S/PDIF */
0267     24, 25,
0268     /* others don't exist */
0269     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
0270 };
0271 
0272 static const char channel_map_9636_ds[26] = {
0273     /* ADAT channels are remapped */
0274     1, 3, 5, 7, 9, 11, 13, 15,
0275     /* channels 8 and 9 are S/PDIF */
0276     24, 25,
0277     /* others don't exist */
0278     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
0279 };
0280 
0281 static struct snd_dma_buffer *
0282 snd_hammerfall_get_buffer(struct pci_dev *pci, size_t size)
0283 {
0284     return snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV, size);
0285 }
0286 
0287 static const struct pci_device_id snd_rme9652_ids[] = {
0288     {
0289         .vendor    = 0x10ee,
0290         .device    = 0x3fc4,
0291         .subvendor = PCI_ANY_ID,
0292         .subdevice = PCI_ANY_ID,
0293     },  /* RME Digi9652 */
0294     { 0, },
0295 };
0296 
0297 MODULE_DEVICE_TABLE(pci, snd_rme9652_ids);
0298 
0299 static inline void rme9652_write(struct snd_rme9652 *rme9652, int reg, int val)
0300 {
0301     writel(val, rme9652->iobase + reg);
0302 }
0303 
0304 static inline unsigned int rme9652_read(struct snd_rme9652 *rme9652, int reg)
0305 {
0306     return readl(rme9652->iobase + reg);
0307 }
0308 
0309 static inline int snd_rme9652_use_is_exclusive(struct snd_rme9652 *rme9652)
0310 {
0311     unsigned long flags;
0312     int ret = 1;
0313 
0314     spin_lock_irqsave(&rme9652->lock, flags);
0315     if ((rme9652->playback_pid != rme9652->capture_pid) &&
0316         (rme9652->playback_pid >= 0) && (rme9652->capture_pid >= 0)) {
0317         ret = 0;
0318     }
0319     spin_unlock_irqrestore(&rme9652->lock, flags);
0320     return ret;
0321 }
0322 
0323 static inline int rme9652_adat_sample_rate(struct snd_rme9652 *rme9652)
0324 {
0325     if (rme9652_running_double_speed(rme9652)) {
0326         return (rme9652_read(rme9652, RME9652_status_register) &
0327             RME9652_fs48) ? 96000 : 88200;
0328     } else {
0329         return (rme9652_read(rme9652, RME9652_status_register) &
0330             RME9652_fs48) ? 48000 : 44100;
0331     }
0332 }
0333 
0334 static inline void rme9652_compute_period_size(struct snd_rme9652 *rme9652)
0335 {
0336     unsigned int i;
0337 
0338     i = rme9652->control_register & RME9652_latency;
0339     rme9652->period_bytes = 1 << ((rme9652_decode_latency(i) + 8));
0340     rme9652->hw_offsetmask = 
0341         (rme9652->period_bytes * 2 - 1) & RME9652_buf_pos;
0342     rme9652->max_jitter = 80;
0343 }
0344 
0345 static snd_pcm_uframes_t rme9652_hw_pointer(struct snd_rme9652 *rme9652)
0346 {
0347     int status;
0348     unsigned int offset, frag;
0349     snd_pcm_uframes_t period_size = rme9652->period_bytes / 4;
0350     snd_pcm_sframes_t delta;
0351 
0352     status = rme9652_read(rme9652, RME9652_status_register);
0353     if (!rme9652->precise_ptr)
0354         return (status & RME9652_buffer_id) ? period_size : 0;
0355     offset = status & RME9652_buf_pos;
0356 
0357     /* The hardware may give a backward movement for up to 80 frames
0358            Martin Kirst <martin.kirst@freenet.de> knows the details.
0359     */
0360 
0361     delta = rme9652->prev_hw_offset - offset;
0362     delta &= 0xffff;
0363     if (delta <= (snd_pcm_sframes_t)rme9652->max_jitter * 4)
0364         offset = rme9652->prev_hw_offset;
0365     else
0366         rme9652->prev_hw_offset = offset;
0367     offset &= rme9652->hw_offsetmask;
0368     offset /= 4;
0369     frag = status & RME9652_buffer_id;
0370 
0371     if (offset < period_size) {
0372         if (offset > rme9652->max_jitter) {
0373             if (frag)
0374                 dev_err(rme9652->card->dev,
0375                     "Unexpected hw_pointer position (bufid == 0): status: %x offset: %d\n",
0376                     status, offset);
0377         } else if (!frag)
0378             return 0;
0379         offset -= rme9652->max_jitter;
0380         if ((int)offset < 0)
0381             offset += period_size * 2;
0382     } else {
0383         if (offset > period_size + rme9652->max_jitter) {
0384             if (!frag)
0385                 dev_err(rme9652->card->dev,
0386                     "Unexpected hw_pointer position (bufid == 1): status: %x offset: %d\n",
0387                     status, offset);
0388         } else if (frag)
0389             return period_size;
0390         offset -= rme9652->max_jitter;
0391     }
0392 
0393     return offset;
0394 }
0395 
0396 static inline void rme9652_reset_hw_pointer(struct snd_rme9652 *rme9652)
0397 {
0398     int i;
0399 
0400     /* reset the FIFO pointer to zero. We do this by writing to 8
0401        registers, each of which is a 32bit wide register, and set
0402        them all to zero. Note that s->iobase is a pointer to
0403        int32, not pointer to char.  
0404     */
0405 
0406     for (i = 0; i < 8; i++) {
0407         rme9652_write(rme9652, i * 4, 0);
0408         udelay(10);
0409     }
0410     rme9652->prev_hw_offset = 0;
0411 }
0412 
0413 static inline void rme9652_start(struct snd_rme9652 *s)
0414 {
0415     s->control_register |= (RME9652_IE | RME9652_start_bit);
0416     rme9652_write(s, RME9652_control_register, s->control_register);
0417 }
0418 
0419 static inline void rme9652_stop(struct snd_rme9652 *s)
0420 {
0421     s->control_register &= ~(RME9652_start_bit | RME9652_IE);
0422     rme9652_write(s, RME9652_control_register, s->control_register);
0423 }
0424 
0425 static int rme9652_set_interrupt_interval(struct snd_rme9652 *s,
0426                       unsigned int frames)
0427 {
0428     int restart = 0;
0429     int n;
0430 
0431     spin_lock_irq(&s->lock);
0432 
0433     restart = s->running;
0434     if (restart)
0435         rme9652_stop(s);
0436 
0437     frames >>= 7;
0438     n = 0;
0439     while (frames) {
0440         n++;
0441         frames >>= 1;
0442     }
0443 
0444     s->control_register &= ~RME9652_latency;
0445     s->control_register |= rme9652_encode_latency(n);
0446 
0447     rme9652_write(s, RME9652_control_register, s->control_register);
0448 
0449     rme9652_compute_period_size(s);
0450 
0451     if (restart)
0452         rme9652_start(s);
0453 
0454     spin_unlock_irq(&s->lock);
0455 
0456     return 0;
0457 }
0458 
0459 static int rme9652_set_rate(struct snd_rme9652 *rme9652, int rate)
0460 {
0461     int restart;
0462     int reject_if_open = 0;
0463     int xrate;
0464 
0465     if (!snd_rme9652_use_is_exclusive (rme9652)) {
0466         return -EBUSY;
0467     }
0468 
0469     /* Changing from a "single speed" to a "double speed" rate is
0470        not allowed if any substreams are open. This is because
0471        such a change causes a shift in the location of 
0472        the DMA buffers and a reduction in the number of available
0473        buffers. 
0474 
0475        Note that a similar but essentially insoluble problem
0476        exists for externally-driven rate changes. All we can do
0477        is to flag rate changes in the read/write routines.
0478      */
0479 
0480     spin_lock_irq(&rme9652->lock);
0481     xrate = rme9652_adat_sample_rate(rme9652);
0482 
0483     switch (rate) {
0484     case 44100:
0485         if (xrate > 48000) {
0486             reject_if_open = 1;
0487         }
0488         rate = 0;
0489         break;
0490     case 48000:
0491         if (xrate > 48000) {
0492             reject_if_open = 1;
0493         }
0494         rate = RME9652_freq;
0495         break;
0496     case 88200:
0497         if (xrate < 48000) {
0498             reject_if_open = 1;
0499         }
0500         rate = RME9652_DS;
0501         break;
0502     case 96000:
0503         if (xrate < 48000) {
0504             reject_if_open = 1;
0505         }
0506         rate = RME9652_DS | RME9652_freq;
0507         break;
0508     default:
0509         spin_unlock_irq(&rme9652->lock);
0510         return -EINVAL;
0511     }
0512 
0513     if (reject_if_open && (rme9652->capture_pid >= 0 || rme9652->playback_pid >= 0)) {
0514         spin_unlock_irq(&rme9652->lock);
0515         return -EBUSY;
0516     }
0517 
0518     restart = rme9652->running;
0519     if (restart)
0520         rme9652_stop(rme9652);
0521     rme9652->control_register &= ~(RME9652_freq | RME9652_DS);
0522     rme9652->control_register |= rate;
0523     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
0524 
0525     if (restart)
0526         rme9652_start(rme9652);
0527 
0528     if (rate & RME9652_DS) {
0529         if (rme9652->ss_channels == RME9652_NCHANNELS) {
0530             rme9652->channel_map = channel_map_9652_ds;
0531         } else {
0532             rme9652->channel_map = channel_map_9636_ds;
0533         }
0534     } else {
0535         if (rme9652->ss_channels == RME9652_NCHANNELS) {
0536             rme9652->channel_map = channel_map_9652_ss;
0537         } else {
0538             rme9652->channel_map = channel_map_9636_ss;
0539         }
0540     }
0541 
0542     spin_unlock_irq(&rme9652->lock);
0543     return 0;
0544 }
0545 
0546 static void rme9652_set_thru(struct snd_rme9652 *rme9652, int channel, int enable)
0547 {
0548     int i;
0549 
0550     rme9652->passthru = 0;
0551 
0552     if (channel < 0) {
0553 
0554         /* set thru for all channels */
0555 
0556         if (enable) {
0557             for (i = 0; i < RME9652_NCHANNELS; i++) {
0558                 rme9652->thru_bits |= (1 << i);
0559                 rme9652_write(rme9652, RME9652_thru_base + i * 4, 1);
0560             }
0561         } else {
0562             for (i = 0; i < RME9652_NCHANNELS; i++) {
0563                 rme9652->thru_bits &= ~(1 << i);
0564                 rme9652_write(rme9652, RME9652_thru_base + i * 4, 0);
0565             }
0566         }
0567 
0568     } else {
0569         int mapped_channel;
0570 
0571         mapped_channel = rme9652->channel_map[channel];
0572 
0573         if (enable) {
0574             rme9652->thru_bits |= (1 << mapped_channel);
0575         } else {
0576             rme9652->thru_bits &= ~(1 << mapped_channel);
0577         }
0578 
0579         rme9652_write(rme9652,
0580                    RME9652_thru_base + mapped_channel * 4,
0581                    enable ? 1 : 0);                
0582     }
0583 }
0584 
0585 static int rme9652_set_passthru(struct snd_rme9652 *rme9652, int onoff)
0586 {
0587     if (onoff) {
0588         rme9652_set_thru(rme9652, -1, 1);
0589 
0590         /* we don't want interrupts, so do a
0591            custom version of rme9652_start().
0592         */
0593 
0594         rme9652->control_register =
0595             RME9652_inp_0 | 
0596             rme9652_encode_latency(7) |
0597             RME9652_start_bit;
0598 
0599         rme9652_reset_hw_pointer(rme9652);
0600 
0601         rme9652_write(rme9652, RME9652_control_register,
0602                   rme9652->control_register);
0603         rme9652->passthru = 1;
0604     } else {
0605         rme9652_set_thru(rme9652, -1, 0);
0606         rme9652_stop(rme9652);      
0607         rme9652->passthru = 0;
0608     }
0609 
0610     return 0;
0611 }
0612 
0613 static void rme9652_spdif_set_bit (struct snd_rme9652 *rme9652, int mask, int onoff)
0614 {
0615     if (onoff) 
0616         rme9652->control_register |= mask;
0617     else 
0618         rme9652->control_register &= ~mask;
0619         
0620     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
0621 }
0622 
0623 static void rme9652_spdif_write_byte (struct snd_rme9652 *rme9652, const int val)
0624 {
0625     long mask;
0626     long i;
0627 
0628     for (i = 0, mask = 0x80; i < 8; i++, mask >>= 1) {
0629         if (val & mask)
0630             rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 1);
0631         else 
0632             rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 0);
0633 
0634         rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
0635         rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
0636     }
0637 }
0638 
0639 static int rme9652_spdif_read_byte (struct snd_rme9652 *rme9652)
0640 {
0641     long mask;
0642     long val;
0643     long i;
0644 
0645     val = 0;
0646 
0647     for (i = 0, mask = 0x80;  i < 8; i++, mask >>= 1) {
0648         rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
0649         if (rme9652_read (rme9652, RME9652_status_register) & RME9652_SPDIF_READ)
0650             val |= mask;
0651         rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
0652     }
0653 
0654     return val;
0655 }
0656 
0657 static void rme9652_write_spdif_codec (struct snd_rme9652 *rme9652, const int address, const int data)
0658 {
0659     rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
0660     rme9652_spdif_write_byte (rme9652, 0x20);
0661     rme9652_spdif_write_byte (rme9652, address);
0662     rme9652_spdif_write_byte (rme9652, data);
0663     rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
0664 }
0665 
0666 
0667 static int rme9652_spdif_read_codec (struct snd_rme9652 *rme9652, const int address)
0668 {
0669     int ret;
0670 
0671     rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
0672     rme9652_spdif_write_byte (rme9652, 0x20);
0673     rme9652_spdif_write_byte (rme9652, address);
0674     rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
0675     rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
0676 
0677     rme9652_spdif_write_byte (rme9652, 0x21);
0678     ret = rme9652_spdif_read_byte (rme9652);
0679     rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
0680 
0681     return ret;
0682 }
0683 
0684 static void rme9652_initialize_spdif_receiver (struct snd_rme9652 *rme9652)
0685 {
0686     /* XXX what unsets this ? */
0687 
0688     rme9652->control_register |= RME9652_SPDIF_RESET;
0689 
0690     rme9652_write_spdif_codec (rme9652, 4, 0x40);
0691     rme9652_write_spdif_codec (rme9652, 17, 0x13);
0692     rme9652_write_spdif_codec (rme9652, 6, 0x02);
0693 }
0694 
0695 static inline int rme9652_spdif_sample_rate(struct snd_rme9652 *s)
0696 {
0697     unsigned int rate_bits;
0698 
0699     if (rme9652_read(s, RME9652_status_register) & RME9652_ERF) {
0700         return -1;  /* error condition */
0701     }
0702     
0703     if (s->hw_rev == 15) {
0704 
0705         int x, y, ret;
0706         
0707         x = rme9652_spdif_read_codec (s, 30);
0708 
0709         if (x != 0) 
0710             y = 48000 * 64 / x;
0711         else
0712             y = 0;
0713 
0714         if      (y > 30400 && y < 33600)  ret = 32000; 
0715         else if (y > 41900 && y < 46000)  ret = 44100;
0716         else if (y > 46000 && y < 50400)  ret = 48000;
0717         else if (y > 60800 && y < 67200)  ret = 64000;
0718         else if (y > 83700 && y < 92000)  ret = 88200;
0719         else if (y > 92000 && y < 100000) ret = 96000;
0720         else                              ret = 0;
0721         return ret;
0722     }
0723 
0724     rate_bits = rme9652_read(s, RME9652_status_register) & RME9652_F;
0725 
0726     switch (rme9652_decode_spdif_rate(rate_bits)) {
0727     case 0x7:
0728         return 32000;
0729 
0730     case 0x6:
0731         return 44100;
0732 
0733     case 0x5:
0734         return 48000;
0735 
0736     case 0x4:
0737         return 88200;
0738 
0739     case 0x3:
0740         return 96000;
0741 
0742     case 0x0:
0743         return 64000;
0744 
0745     default:
0746         dev_err(s->card->dev,
0747             "%s: unknown S/PDIF input rate (bits = 0x%x)\n",
0748                s->card_name, rate_bits);
0749         return 0;
0750     }
0751 }
0752 
0753 /*-----------------------------------------------------------------------------
0754   Control Interface
0755   ----------------------------------------------------------------------------*/
0756 
0757 static u32 snd_rme9652_convert_from_aes(struct snd_aes_iec958 *aes)
0758 {
0759     u32 val = 0;
0760     val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME9652_PRO : 0;
0761     val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME9652_Dolby : 0;
0762     if (val & RME9652_PRO)
0763         val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME9652_EMP : 0;
0764     else
0765         val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME9652_EMP : 0;
0766     return val;
0767 }
0768 
0769 static void snd_rme9652_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
0770 {
0771     aes->status[0] = ((val & RME9652_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
0772              ((val & RME9652_Dolby) ? IEC958_AES0_NONAUDIO : 0);
0773     if (val & RME9652_PRO)
0774         aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
0775     else
0776         aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
0777 }
0778 
0779 static int snd_rme9652_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0780 {
0781     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
0782     uinfo->count = 1;
0783     return 0;
0784 }
0785 
0786 static int snd_rme9652_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0787 {
0788     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0789     
0790     snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif);
0791     return 0;
0792 }
0793 
0794 static int snd_rme9652_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0795 {
0796     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0797     int change;
0798     u32 val;
0799     
0800     val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
0801     spin_lock_irq(&rme9652->lock);
0802     change = val != rme9652->creg_spdif;
0803     rme9652->creg_spdif = val;
0804     spin_unlock_irq(&rme9652->lock);
0805     return change;
0806 }
0807 
0808 static int snd_rme9652_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0809 {
0810     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
0811     uinfo->count = 1;
0812     return 0;
0813 }
0814 
0815 static int snd_rme9652_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0816 {
0817     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0818     
0819     snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif_stream);
0820     return 0;
0821 }
0822 
0823 static int snd_rme9652_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0824 {
0825     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0826     int change;
0827     u32 val;
0828     
0829     val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
0830     spin_lock_irq(&rme9652->lock);
0831     change = val != rme9652->creg_spdif_stream;
0832     rme9652->creg_spdif_stream = val;
0833     rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
0834     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= val);
0835     spin_unlock_irq(&rme9652->lock);
0836     return change;
0837 }
0838 
0839 static int snd_rme9652_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0840 {
0841     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
0842     uinfo->count = 1;
0843     return 0;
0844 }
0845 
0846 static int snd_rme9652_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0847 {
0848     ucontrol->value.iec958.status[0] = kcontrol->private_value;
0849     return 0;
0850 }
0851 
0852 #define RME9652_ADAT1_IN(xname, xindex) \
0853 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0854   .info = snd_rme9652_info_adat1_in, \
0855   .get = snd_rme9652_get_adat1_in, \
0856   .put = snd_rme9652_put_adat1_in }
0857 
0858 static unsigned int rme9652_adat1_in(struct snd_rme9652 *rme9652)
0859 {
0860     if (rme9652->control_register & RME9652_ADAT1_INTERNAL)
0861         return 1; 
0862     return 0;
0863 }
0864 
0865 static int rme9652_set_adat1_input(struct snd_rme9652 *rme9652, int internal)
0866 {
0867     int restart = 0;
0868 
0869     if (internal) {
0870         rme9652->control_register |= RME9652_ADAT1_INTERNAL;
0871     } else {
0872         rme9652->control_register &= ~RME9652_ADAT1_INTERNAL;
0873     }
0874 
0875     /* XXX do we actually need to stop the card when we do this ? */
0876 
0877     restart = rme9652->running;
0878     if (restart)
0879         rme9652_stop(rme9652);
0880 
0881     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
0882 
0883     if (restart)
0884         rme9652_start(rme9652);
0885 
0886     return 0;
0887 }
0888 
0889 static int snd_rme9652_info_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0890 {
0891     static const char * const texts[2] = {"ADAT1", "Internal"};
0892 
0893     return snd_ctl_enum_info(uinfo, 1, 2, texts);
0894 }
0895 
0896 static int snd_rme9652_get_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0897 {
0898     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0899     
0900     spin_lock_irq(&rme9652->lock);
0901     ucontrol->value.enumerated.item[0] = rme9652_adat1_in(rme9652);
0902     spin_unlock_irq(&rme9652->lock);
0903     return 0;
0904 }
0905 
0906 static int snd_rme9652_put_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0907 {
0908     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0909     int change;
0910     unsigned int val;
0911     
0912     if (!snd_rme9652_use_is_exclusive(rme9652))
0913         return -EBUSY;
0914     val = ucontrol->value.enumerated.item[0] % 2;
0915     spin_lock_irq(&rme9652->lock);
0916     change = val != rme9652_adat1_in(rme9652);
0917     if (change)
0918         rme9652_set_adat1_input(rme9652, val);
0919     spin_unlock_irq(&rme9652->lock);
0920     return change;
0921 }
0922 
0923 #define RME9652_SPDIF_IN(xname, xindex) \
0924 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0925   .info = snd_rme9652_info_spdif_in, \
0926   .get = snd_rme9652_get_spdif_in, .put = snd_rme9652_put_spdif_in }
0927 
0928 static unsigned int rme9652_spdif_in(struct snd_rme9652 *rme9652)
0929 {
0930     return rme9652_decode_spdif_in(rme9652->control_register &
0931                        RME9652_inp);
0932 }
0933 
0934 static int rme9652_set_spdif_input(struct snd_rme9652 *rme9652, int in)
0935 {
0936     int restart = 0;
0937 
0938     rme9652->control_register &= ~RME9652_inp;
0939     rme9652->control_register |= rme9652_encode_spdif_in(in);
0940 
0941     restart = rme9652->running;
0942     if (restart)
0943         rme9652_stop(rme9652);
0944 
0945     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
0946 
0947     if (restart)
0948         rme9652_start(rme9652);
0949 
0950     return 0;
0951 }
0952 
0953 static int snd_rme9652_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
0954 {
0955     static const char * const texts[3] = {"ADAT1", "Coaxial", "Internal"};
0956 
0957     return snd_ctl_enum_info(uinfo, 1, 3, texts);
0958 }
0959 
0960 static int snd_rme9652_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0961 {
0962     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0963     
0964     spin_lock_irq(&rme9652->lock);
0965     ucontrol->value.enumerated.item[0] = rme9652_spdif_in(rme9652);
0966     spin_unlock_irq(&rme9652->lock);
0967     return 0;
0968 }
0969 
0970 static int snd_rme9652_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
0971 {
0972     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
0973     int change;
0974     unsigned int val;
0975     
0976     if (!snd_rme9652_use_is_exclusive(rme9652))
0977         return -EBUSY;
0978     val = ucontrol->value.enumerated.item[0] % 3;
0979     spin_lock_irq(&rme9652->lock);
0980     change = val != rme9652_spdif_in(rme9652);
0981     if (change)
0982         rme9652_set_spdif_input(rme9652, val);
0983     spin_unlock_irq(&rme9652->lock);
0984     return change;
0985 }
0986 
0987 #define RME9652_SPDIF_OUT(xname, xindex) \
0988 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0989   .info = snd_rme9652_info_spdif_out, \
0990   .get = snd_rme9652_get_spdif_out, .put = snd_rme9652_put_spdif_out }
0991 
0992 static int rme9652_spdif_out(struct snd_rme9652 *rme9652)
0993 {
0994     return (rme9652->control_register & RME9652_opt_out) ? 1 : 0;
0995 }
0996 
0997 static int rme9652_set_spdif_output(struct snd_rme9652 *rme9652, int out)
0998 {
0999     int restart = 0;
1000 
1001     if (out) {
1002         rme9652->control_register |= RME9652_opt_out;
1003     } else {
1004         rme9652->control_register &= ~RME9652_opt_out;
1005     }
1006 
1007     restart = rme9652->running;
1008     if (restart)
1009         rme9652_stop(rme9652);
1010 
1011     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1012 
1013     if (restart)
1014         rme9652_start(rme9652);
1015 
1016     return 0;
1017 }
1018 
1019 #define snd_rme9652_info_spdif_out  snd_ctl_boolean_mono_info
1020 
1021 static int snd_rme9652_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1022 {
1023     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1024     
1025     spin_lock_irq(&rme9652->lock);
1026     ucontrol->value.integer.value[0] = rme9652_spdif_out(rme9652);
1027     spin_unlock_irq(&rme9652->lock);
1028     return 0;
1029 }
1030 
1031 static int snd_rme9652_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1032 {
1033     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1034     int change;
1035     unsigned int val;
1036     
1037     if (!snd_rme9652_use_is_exclusive(rme9652))
1038         return -EBUSY;
1039     val = ucontrol->value.integer.value[0] & 1;
1040     spin_lock_irq(&rme9652->lock);
1041     change = (int)val != rme9652_spdif_out(rme9652);
1042     rme9652_set_spdif_output(rme9652, val);
1043     spin_unlock_irq(&rme9652->lock);
1044     return change;
1045 }
1046 
1047 #define RME9652_SYNC_MODE(xname, xindex) \
1048 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1049   .info = snd_rme9652_info_sync_mode, \
1050   .get = snd_rme9652_get_sync_mode, .put = snd_rme9652_put_sync_mode }
1051 
1052 static int rme9652_sync_mode(struct snd_rme9652 *rme9652)
1053 {
1054     if (rme9652->control_register & RME9652_wsel) {
1055         return 2;
1056     } else if (rme9652->control_register & RME9652_Master) {
1057         return 1;
1058     } else {
1059         return 0;
1060     }
1061 }
1062 
1063 static int rme9652_set_sync_mode(struct snd_rme9652 *rme9652, int mode)
1064 {
1065     int restart = 0;
1066 
1067     switch (mode) {
1068     case 0:
1069         rme9652->control_register &=
1070             ~(RME9652_Master | RME9652_wsel);
1071         break;
1072     case 1:
1073         rme9652->control_register =
1074             (rme9652->control_register & ~RME9652_wsel) | RME9652_Master;
1075         break;
1076     case 2:
1077         rme9652->control_register |=
1078             (RME9652_Master | RME9652_wsel);
1079         break;
1080     }
1081 
1082     restart = rme9652->running;
1083     if (restart)
1084         rme9652_stop(rme9652);
1085 
1086     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1087 
1088     if (restart)
1089         rme9652_start(rme9652);
1090 
1091     return 0;
1092 }
1093 
1094 static int snd_rme9652_info_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1095 {
1096     static const char * const texts[3] = {
1097         "AutoSync", "Master", "Word Clock"
1098     };
1099 
1100     return snd_ctl_enum_info(uinfo, 1, 3, texts);
1101 }
1102 
1103 static int snd_rme9652_get_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1104 {
1105     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1106     
1107     spin_lock_irq(&rme9652->lock);
1108     ucontrol->value.enumerated.item[0] = rme9652_sync_mode(rme9652);
1109     spin_unlock_irq(&rme9652->lock);
1110     return 0;
1111 }
1112 
1113 static int snd_rme9652_put_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1114 {
1115     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1116     int change;
1117     unsigned int val;
1118     
1119     val = ucontrol->value.enumerated.item[0] % 3;
1120     spin_lock_irq(&rme9652->lock);
1121     change = (int)val != rme9652_sync_mode(rme9652);
1122     rme9652_set_sync_mode(rme9652, val);
1123     spin_unlock_irq(&rme9652->lock);
1124     return change;
1125 }
1126 
1127 #define RME9652_SYNC_PREF(xname, xindex) \
1128 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1129   .info = snd_rme9652_info_sync_pref, \
1130   .get = snd_rme9652_get_sync_pref, .put = snd_rme9652_put_sync_pref }
1131 
1132 static int rme9652_sync_pref(struct snd_rme9652 *rme9652)
1133 {
1134     switch (rme9652->control_register & RME9652_SyncPref_Mask) {
1135     case RME9652_SyncPref_ADAT1:
1136         return RME9652_SYNC_FROM_ADAT1;
1137     case RME9652_SyncPref_ADAT2:
1138         return RME9652_SYNC_FROM_ADAT2;
1139     case RME9652_SyncPref_ADAT3:
1140         return RME9652_SYNC_FROM_ADAT3;
1141     case RME9652_SyncPref_SPDIF:
1142         return RME9652_SYNC_FROM_SPDIF;
1143     }
1144     /* Not reachable */
1145     return 0;
1146 }
1147 
1148 static int rme9652_set_sync_pref(struct snd_rme9652 *rme9652, int pref)
1149 {
1150     int restart;
1151 
1152     rme9652->control_register &= ~RME9652_SyncPref_Mask;
1153     switch (pref) {
1154     case RME9652_SYNC_FROM_ADAT1:
1155         rme9652->control_register |= RME9652_SyncPref_ADAT1;
1156         break;
1157     case RME9652_SYNC_FROM_ADAT2:
1158         rme9652->control_register |= RME9652_SyncPref_ADAT2;
1159         break;
1160     case RME9652_SYNC_FROM_ADAT3:
1161         rme9652->control_register |= RME9652_SyncPref_ADAT3;
1162         break;
1163     case RME9652_SYNC_FROM_SPDIF:
1164         rme9652->control_register |= RME9652_SyncPref_SPDIF;
1165         break;
1166     }
1167 
1168     restart = rme9652->running;
1169     if (restart)
1170         rme9652_stop(rme9652);
1171 
1172     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1173 
1174     if (restart)
1175         rme9652_start(rme9652);
1176 
1177     return 0;
1178 }
1179 
1180 static int snd_rme9652_info_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1181 {
1182     static const char * const texts[4] = {
1183         "IEC958 In", "ADAT1 In", "ADAT2 In", "ADAT3 In"
1184     };
1185     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1186 
1187     return snd_ctl_enum_info(uinfo, 1,
1188                  rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3,
1189                  texts);
1190 }
1191 
1192 static int snd_rme9652_get_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1193 {
1194     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1195     
1196     spin_lock_irq(&rme9652->lock);
1197     ucontrol->value.enumerated.item[0] = rme9652_sync_pref(rme9652);
1198     spin_unlock_irq(&rme9652->lock);
1199     return 0;
1200 }
1201 
1202 static int snd_rme9652_put_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1203 {
1204     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1205     int change, max;
1206     unsigned int val;
1207     
1208     if (!snd_rme9652_use_is_exclusive(rme9652))
1209         return -EBUSY;
1210     max = rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3;
1211     val = ucontrol->value.enumerated.item[0] % max;
1212     spin_lock_irq(&rme9652->lock);
1213     change = (int)val != rme9652_sync_pref(rme9652);
1214     rme9652_set_sync_pref(rme9652, val);
1215     spin_unlock_irq(&rme9652->lock);
1216     return change;
1217 }
1218 
1219 static int snd_rme9652_info_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1220 {
1221     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1222     uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1223     uinfo->count = rme9652->ss_channels;
1224     uinfo->value.integer.min = 0;
1225     uinfo->value.integer.max = 1;
1226     return 0;
1227 }
1228 
1229 static int snd_rme9652_get_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1230 {
1231     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1232     unsigned int k;
1233     u32 thru_bits = rme9652->thru_bits;
1234 
1235     for (k = 0; k < rme9652->ss_channels; ++k) {
1236         ucontrol->value.integer.value[k] = !!(thru_bits & (1 << k));
1237     }
1238     return 0;
1239 }
1240 
1241 static int snd_rme9652_put_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1242 {
1243     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1244     int change;
1245     unsigned int chn;
1246     u32 thru_bits = 0;
1247 
1248     if (!snd_rme9652_use_is_exclusive(rme9652))
1249         return -EBUSY;
1250 
1251     for (chn = 0; chn < rme9652->ss_channels; ++chn) {
1252         if (ucontrol->value.integer.value[chn])
1253             thru_bits |= 1 << chn;
1254     }
1255     
1256     spin_lock_irq(&rme9652->lock);
1257     change = thru_bits ^ rme9652->thru_bits;
1258     if (change) {
1259         for (chn = 0; chn < rme9652->ss_channels; ++chn) {
1260             if (!(change & (1 << chn)))
1261                 continue;
1262             rme9652_set_thru(rme9652,chn,thru_bits&(1<<chn));
1263         }
1264     }
1265     spin_unlock_irq(&rme9652->lock);
1266     return !!change;
1267 }
1268 
1269 #define RME9652_PASSTHRU(xname, xindex) \
1270 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1271   .info = snd_rme9652_info_passthru, \
1272   .put = snd_rme9652_put_passthru, \
1273   .get = snd_rme9652_get_passthru }
1274 
1275 #define snd_rme9652_info_passthru   snd_ctl_boolean_mono_info
1276 
1277 static int snd_rme9652_get_passthru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1278 {
1279     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1280 
1281     spin_lock_irq(&rme9652->lock);
1282     ucontrol->value.integer.value[0] = rme9652->passthru;
1283     spin_unlock_irq(&rme9652->lock);
1284     return 0;
1285 }
1286 
1287 static int snd_rme9652_put_passthru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1288 {
1289     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1290     int change;
1291     unsigned int val;
1292     int err = 0;
1293 
1294     if (!snd_rme9652_use_is_exclusive(rme9652))
1295         return -EBUSY;
1296 
1297     val = ucontrol->value.integer.value[0] & 1;
1298     spin_lock_irq(&rme9652->lock);
1299     change = (ucontrol->value.integer.value[0] != rme9652->passthru);
1300     if (change)
1301         err = rme9652_set_passthru(rme9652, val);
1302     spin_unlock_irq(&rme9652->lock);
1303     return err ? err : change;
1304 }
1305 
1306 /* Read-only switches */
1307 
1308 #define RME9652_SPDIF_RATE(xname, xindex) \
1309 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1310   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1311   .info = snd_rme9652_info_spdif_rate, \
1312   .get = snd_rme9652_get_spdif_rate }
1313 
1314 static int snd_rme9652_info_spdif_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1315 {
1316     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1317     uinfo->count = 1;
1318     uinfo->value.integer.min = 0;
1319     uinfo->value.integer.max = 96000;
1320     return 0;
1321 }
1322 
1323 static int snd_rme9652_get_spdif_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1324 {
1325     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1326     
1327     spin_lock_irq(&rme9652->lock);
1328     ucontrol->value.integer.value[0] = rme9652_spdif_sample_rate(rme9652);
1329     spin_unlock_irq(&rme9652->lock);
1330     return 0;
1331 }
1332 
1333 #define RME9652_ADAT_SYNC(xname, xindex, xidx) \
1334 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1335   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1336   .info = snd_rme9652_info_adat_sync, \
1337   .get = snd_rme9652_get_adat_sync, .private_value = xidx }
1338 
1339 static int snd_rme9652_info_adat_sync(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1340 {
1341     static const char * const texts[4] = {
1342         "No Lock", "Lock", "No Lock Sync", "Lock Sync"
1343     };
1344 
1345     return snd_ctl_enum_info(uinfo, 1, 4, texts);
1346 }
1347 
1348 static int snd_rme9652_get_adat_sync(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1349 {
1350     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1351     unsigned int mask1, mask2, val;
1352     
1353     switch (kcontrol->private_value) {
1354     case 0: mask1 = RME9652_lock_0; mask2 = RME9652_sync_0; break;  
1355     case 1: mask1 = RME9652_lock_1; mask2 = RME9652_sync_1; break;  
1356     case 2: mask1 = RME9652_lock_2; mask2 = RME9652_sync_2; break;  
1357     default: return -EINVAL;
1358     }
1359     val = rme9652_read(rme9652, RME9652_status_register);
1360     ucontrol->value.enumerated.item[0] = (val & mask1) ? 1 : 0;
1361     ucontrol->value.enumerated.item[0] |= (val & mask2) ? 2 : 0;
1362     return 0;
1363 }
1364 
1365 #define RME9652_TC_VALID(xname, xindex) \
1366 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1367   .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
1368   .info = snd_rme9652_info_tc_valid, \
1369   .get = snd_rme9652_get_tc_valid }
1370 
1371 #define snd_rme9652_info_tc_valid   snd_ctl_boolean_mono_info
1372 
1373 static int snd_rme9652_get_tc_valid(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1374 {
1375     struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
1376     
1377     ucontrol->value.integer.value[0] = 
1378         (rme9652_read(rme9652, RME9652_status_register) & RME9652_tc_valid) ? 1 : 0;
1379     return 0;
1380 }
1381 
1382 #ifdef ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE
1383 
1384 /* FIXME: this routine needs a port to the new control API --jk */
1385 
1386 static int snd_rme9652_get_tc_value(void *private_data,
1387                     snd_kswitch_t *kswitch,
1388                     snd_switch_t *uswitch)
1389 {
1390     struct snd_rme9652 *s = (struct snd_rme9652 *) private_data;
1391     u32 value;
1392     int i;
1393 
1394     uswitch->type = SNDRV_SW_TYPE_DWORD;
1395 
1396     if ((rme9652_read(s, RME9652_status_register) &
1397          RME9652_tc_valid) == 0) {
1398         uswitch->value.data32[0] = 0;
1399         return 0;
1400     }
1401 
1402     /* timecode request */
1403 
1404     rme9652_write(s, RME9652_time_code, 0);
1405 
1406     /* XXX bug alert: loop-based timing !!!! */
1407 
1408     for (i = 0; i < 50; i++) {
1409         if (!(rme9652_read(s, i * 4) & RME9652_tc_busy))
1410             break;
1411     }
1412 
1413     if (!(rme9652_read(s, i * 4) & RME9652_tc_busy)) {
1414         return -EIO;
1415     }
1416 
1417     value = 0;
1418 
1419     for (i = 0; i < 32; i++) {
1420         value >>= 1;
1421 
1422         if (rme9652_read(s, i * 4) & RME9652_tc_out)
1423             value |= 0x80000000;
1424     }
1425 
1426     if (value > 2 * 60 * 48000) {
1427         value -= 2 * 60 * 48000;
1428     } else {
1429         value = 0;
1430     }
1431 
1432     uswitch->value.data32[0] = value;
1433 
1434     return 0;
1435 }
1436 
1437 #endif              /* ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE */
1438 
1439 static const struct snd_kcontrol_new snd_rme9652_controls[] = {
1440 {
1441     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1442     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1443     .info =     snd_rme9652_control_spdif_info,
1444     .get =      snd_rme9652_control_spdif_get,
1445     .put =      snd_rme9652_control_spdif_put,
1446 },
1447 {
1448     .access =   SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1449     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1450     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1451     .info =     snd_rme9652_control_spdif_stream_info,
1452     .get =      snd_rme9652_control_spdif_stream_get,
1453     .put =      snd_rme9652_control_spdif_stream_put,
1454 },
1455 {
1456     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1457     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1458     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1459     .info =     snd_rme9652_control_spdif_mask_info,
1460     .get =      snd_rme9652_control_spdif_mask_get,
1461     .private_value = IEC958_AES0_NONAUDIO |
1462             IEC958_AES0_PROFESSIONAL |
1463             IEC958_AES0_CON_EMPHASIS,                                                                                         
1464 },
1465 {
1466     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1467     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
1468     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1469     .info =     snd_rme9652_control_spdif_mask_info,
1470     .get =      snd_rme9652_control_spdif_mask_get,
1471     .private_value = IEC958_AES0_NONAUDIO |
1472             IEC958_AES0_PROFESSIONAL |
1473             IEC958_AES0_PRO_EMPHASIS,
1474 },
1475 RME9652_SPDIF_IN("IEC958 Input Connector", 0),
1476 RME9652_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
1477 RME9652_SYNC_MODE("Sync Mode", 0),
1478 RME9652_SYNC_PREF("Preferred Sync Source", 0),
1479 {
1480     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1481     .name = "Channels Thru",
1482     .index = 0,
1483     .info = snd_rme9652_info_thru,
1484     .get = snd_rme9652_get_thru,
1485     .put = snd_rme9652_put_thru,
1486 },
1487 RME9652_SPDIF_RATE("IEC958 Sample Rate", 0),
1488 RME9652_ADAT_SYNC("ADAT1 Sync Check", 0, 0),
1489 RME9652_ADAT_SYNC("ADAT2 Sync Check", 0, 1),
1490 RME9652_TC_VALID("Timecode Valid", 0),
1491 RME9652_PASSTHRU("Passthru", 0)
1492 };
1493 
1494 static const struct snd_kcontrol_new snd_rme9652_adat3_check =
1495 RME9652_ADAT_SYNC("ADAT3 Sync Check", 0, 2);
1496 
1497 static const struct snd_kcontrol_new snd_rme9652_adat1_input =
1498 RME9652_ADAT1_IN("ADAT1 Input Source", 0);
1499 
1500 static int snd_rme9652_create_controls(struct snd_card *card, struct snd_rme9652 *rme9652)
1501 {
1502     unsigned int idx;
1503     int err;
1504     struct snd_kcontrol *kctl;
1505 
1506     for (idx = 0; idx < ARRAY_SIZE(snd_rme9652_controls); idx++) {
1507         kctl = snd_ctl_new1(&snd_rme9652_controls[idx], rme9652);
1508         err = snd_ctl_add(card, kctl);
1509         if (err < 0)
1510             return err;
1511         if (idx == 1)   /* IEC958 (S/PDIF) Stream */
1512             rme9652->spdif_ctl = kctl;
1513     }
1514 
1515     if (rme9652->ss_channels == RME9652_NCHANNELS) {
1516         kctl = snd_ctl_new1(&snd_rme9652_adat3_check, rme9652);
1517         err = snd_ctl_add(card, kctl);
1518         if (err < 0)
1519             return err;
1520     }
1521 
1522     if (rme9652->hw_rev >= 15) {
1523         kctl = snd_ctl_new1(&snd_rme9652_adat1_input, rme9652);
1524         err = snd_ctl_add(card, kctl);
1525         if (err < 0)
1526             return err;
1527     }
1528 
1529     return 0;
1530 }
1531 
1532 /*------------------------------------------------------------
1533    /proc interface 
1534  ------------------------------------------------------------*/
1535 
1536 static void
1537 snd_rme9652_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1538 {
1539     struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) entry->private_data;
1540     u32 thru_bits = rme9652->thru_bits;
1541     int show_auto_sync_source = 0;
1542     int i;
1543     unsigned int status;
1544     int x;
1545 
1546     status = rme9652_read(rme9652, RME9652_status_register);
1547 
1548     snd_iprintf(buffer, "%s (Card #%d)\n", rme9652->card_name, rme9652->card->number + 1);
1549     snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
1550             rme9652->capture_buffer, rme9652->playback_buffer);
1551     snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
1552             rme9652->irq, rme9652->port, (unsigned long)rme9652->iobase);
1553     snd_iprintf(buffer, "Control register: %x\n", rme9652->control_register);
1554 
1555     snd_iprintf(buffer, "\n");
1556 
1557     x = 1 << (6 + rme9652_decode_latency(rme9652->control_register & 
1558                          RME9652_latency));
1559 
1560     snd_iprintf(buffer, "Latency: %d samples (2 periods of %lu bytes)\n", 
1561             x, (unsigned long) rme9652->period_bytes);
1562     snd_iprintf(buffer, "Hardware pointer (frames): %ld\n",
1563             rme9652_hw_pointer(rme9652));
1564     snd_iprintf(buffer, "Passthru: %s\n",
1565             rme9652->passthru ? "yes" : "no");
1566 
1567     if ((rme9652->control_register & (RME9652_Master | RME9652_wsel)) == 0) {
1568         snd_iprintf(buffer, "Clock mode: autosync\n");
1569         show_auto_sync_source = 1;
1570     } else if (rme9652->control_register & RME9652_wsel) {
1571         if (status & RME9652_wsel_rd) {
1572             snd_iprintf(buffer, "Clock mode: word clock\n");
1573         } else {
1574             snd_iprintf(buffer, "Clock mode: word clock (no signal)\n");
1575         }
1576     } else {
1577         snd_iprintf(buffer, "Clock mode: master\n");
1578     }
1579 
1580     if (show_auto_sync_source) {
1581         switch (rme9652->control_register & RME9652_SyncPref_Mask) {
1582         case RME9652_SyncPref_ADAT1:
1583             snd_iprintf(buffer, "Pref. sync source: ADAT1\n");
1584             break;
1585         case RME9652_SyncPref_ADAT2:
1586             snd_iprintf(buffer, "Pref. sync source: ADAT2\n");
1587             break;
1588         case RME9652_SyncPref_ADAT3:
1589             snd_iprintf(buffer, "Pref. sync source: ADAT3\n");
1590             break;
1591         case RME9652_SyncPref_SPDIF:
1592             snd_iprintf(buffer, "Pref. sync source: IEC958\n");
1593             break;
1594         default:
1595             snd_iprintf(buffer, "Pref. sync source: ???\n");
1596         }
1597     }
1598 
1599     if (rme9652->hw_rev >= 15)
1600         snd_iprintf(buffer, "\nADAT1 Input source: %s\n",
1601                 (rme9652->control_register & RME9652_ADAT1_INTERNAL) ?
1602                 "Internal" : "ADAT1 optical");
1603 
1604     snd_iprintf(buffer, "\n");
1605 
1606     switch (rme9652_decode_spdif_in(rme9652->control_register & 
1607                     RME9652_inp)) {
1608     case RME9652_SPDIFIN_OPTICAL:
1609         snd_iprintf(buffer, "IEC958 input: ADAT1\n");
1610         break;
1611     case RME9652_SPDIFIN_COAXIAL:
1612         snd_iprintf(buffer, "IEC958 input: Coaxial\n");
1613         break;
1614     case RME9652_SPDIFIN_INTERN:
1615         snd_iprintf(buffer, "IEC958 input: Internal\n");
1616         break;
1617     default:
1618         snd_iprintf(buffer, "IEC958 input: ???\n");
1619         break;
1620     }
1621 
1622     if (rme9652->control_register & RME9652_opt_out) {
1623         snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
1624     } else {
1625         snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
1626     }
1627 
1628     if (rme9652->control_register & RME9652_PRO) {
1629         snd_iprintf(buffer, "IEC958 quality: Professional\n");
1630     } else {
1631         snd_iprintf(buffer, "IEC958 quality: Consumer\n");
1632     }
1633 
1634     if (rme9652->control_register & RME9652_EMP) {
1635         snd_iprintf(buffer, "IEC958 emphasis: on\n");
1636     } else {
1637         snd_iprintf(buffer, "IEC958 emphasis: off\n");
1638     }
1639 
1640     if (rme9652->control_register & RME9652_Dolby) {
1641         snd_iprintf(buffer, "IEC958 Dolby: on\n");
1642     } else {
1643         snd_iprintf(buffer, "IEC958 Dolby: off\n");
1644     }
1645 
1646     i = rme9652_spdif_sample_rate(rme9652);
1647 
1648     if (i < 0) {
1649         snd_iprintf(buffer,
1650                 "IEC958 sample rate: error flag set\n");
1651     } else if (i == 0) {
1652         snd_iprintf(buffer, "IEC958 sample rate: undetermined\n");
1653     } else {
1654         snd_iprintf(buffer, "IEC958 sample rate: %d\n", i);
1655     }
1656 
1657     snd_iprintf(buffer, "\n");
1658 
1659     snd_iprintf(buffer, "ADAT Sample rate: %dHz\n",
1660             rme9652_adat_sample_rate(rme9652));
1661 
1662     /* Sync Check */
1663 
1664     x = status & RME9652_sync_0;
1665     if (status & RME9652_lock_0) {
1666         snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
1667     } else {
1668         snd_iprintf(buffer, "ADAT1: No Lock\n");
1669     }
1670 
1671     x = status & RME9652_sync_1;
1672     if (status & RME9652_lock_1) {
1673         snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
1674     } else {
1675         snd_iprintf(buffer, "ADAT2: No Lock\n");
1676     }
1677 
1678     x = status & RME9652_sync_2;
1679     if (status & RME9652_lock_2) {
1680         snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
1681     } else {
1682         snd_iprintf(buffer, "ADAT3: No Lock\n");
1683     }
1684 
1685     snd_iprintf(buffer, "\n");
1686 
1687     snd_iprintf(buffer, "Timecode signal: %s\n",
1688             (status & RME9652_tc_valid) ? "yes" : "no");
1689 
1690     /* thru modes */
1691 
1692     snd_iprintf(buffer, "Punch Status:\n\n");
1693 
1694     for (i = 0; i < rme9652->ss_channels; i++) {
1695         if (thru_bits & (1 << i)) {
1696             snd_iprintf(buffer, "%2d:  on ", i + 1);
1697         } else {
1698             snd_iprintf(buffer, "%2d: off ", i + 1);
1699         }
1700 
1701         if (((i + 1) % 8) == 0) {
1702             snd_iprintf(buffer, "\n");
1703         }
1704     }
1705 
1706     snd_iprintf(buffer, "\n");
1707 }
1708 
1709 static void snd_rme9652_proc_init(struct snd_rme9652 *rme9652)
1710 {
1711     snd_card_ro_proc_new(rme9652->card, "rme9652", rme9652,
1712                  snd_rme9652_proc_read);
1713 }
1714 
1715 static void snd_rme9652_card_free(struct snd_card *card)
1716 {
1717     struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) card->private_data;
1718 
1719     if (rme9652->irq >= 0)
1720         rme9652_stop(rme9652);
1721 }
1722 
1723 static int snd_rme9652_initialize_memory(struct snd_rme9652 *rme9652)
1724 {
1725     struct snd_dma_buffer *capture_dma, *playback_dma;
1726 
1727     capture_dma = snd_hammerfall_get_buffer(rme9652->pci, RME9652_DMA_AREA_BYTES);
1728     playback_dma = snd_hammerfall_get_buffer(rme9652->pci, RME9652_DMA_AREA_BYTES);
1729     if (!capture_dma || !playback_dma) {
1730         dev_err(rme9652->card->dev,
1731             "%s: no buffers available\n", rme9652->card_name);
1732         return -ENOMEM;
1733     }
1734 
1735     /* copy to the own data for alignment */
1736     rme9652->capture_dma_buf = *capture_dma;
1737     rme9652->playback_dma_buf = *playback_dma;
1738 
1739     /* Align to bus-space 64K boundary */
1740     rme9652->capture_dma_buf.addr = ALIGN(capture_dma->addr, 0x10000ul);
1741     rme9652->playback_dma_buf.addr = ALIGN(playback_dma->addr, 0x10000ul);
1742 
1743     /* Tell the card where it is */
1744     rme9652_write(rme9652, RME9652_rec_buffer, rme9652->capture_dma_buf.addr);
1745     rme9652_write(rme9652, RME9652_play_buffer, rme9652->playback_dma_buf.addr);
1746 
1747     rme9652->capture_dma_buf.area += rme9652->capture_dma_buf.addr - capture_dma->addr;
1748     rme9652->playback_dma_buf.area += rme9652->playback_dma_buf.addr - playback_dma->addr;
1749     rme9652->capture_buffer = rme9652->capture_dma_buf.area;
1750     rme9652->playback_buffer = rme9652->playback_dma_buf.area;
1751 
1752     return 0;
1753 }
1754 
1755 static void snd_rme9652_set_defaults(struct snd_rme9652 *rme9652)
1756 {
1757     unsigned int k;
1758 
1759     /* ASSUMPTION: rme9652->lock is either held, or
1760        there is no need to hold it (e.g. during module
1761        initialization).
1762      */
1763 
1764     /* set defaults:
1765 
1766        SPDIF Input via Coax 
1767        autosync clock mode
1768        maximum latency (7 = 8192 samples, 64Kbyte buffer,
1769        which implies 2 4096 sample, 32Kbyte periods).
1770        
1771        if rev 1.5, initialize the S/PDIF receiver.
1772 
1773      */
1774 
1775     rme9652->control_register =
1776         RME9652_inp_0 | rme9652_encode_latency(7);
1777 
1778     rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
1779 
1780     rme9652_reset_hw_pointer(rme9652);
1781     rme9652_compute_period_size(rme9652);
1782 
1783     /* default: thru off for all channels */
1784 
1785     for (k = 0; k < RME9652_NCHANNELS; ++k)
1786         rme9652_write(rme9652, RME9652_thru_base + k * 4, 0);
1787 
1788     rme9652->thru_bits = 0;
1789     rme9652->passthru = 0;
1790 
1791     /* set a default rate so that the channel map is set up */
1792 
1793     rme9652_set_rate(rme9652, 48000);
1794 }
1795 
1796 static irqreturn_t snd_rme9652_interrupt(int irq, void *dev_id)
1797 {
1798     struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) dev_id;
1799 
1800     if (!(rme9652_read(rme9652, RME9652_status_register) & RME9652_IRQ)) {
1801         return IRQ_NONE;
1802     }
1803 
1804     rme9652_write(rme9652, RME9652_irq_clear, 0);
1805 
1806     if (rme9652->capture_substream) {
1807         snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
1808     }
1809 
1810     if (rme9652->playback_substream) {
1811         snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
1812     }
1813     return IRQ_HANDLED;
1814 }
1815 
1816 static snd_pcm_uframes_t snd_rme9652_hw_pointer(struct snd_pcm_substream *substream)
1817 {
1818     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1819     return rme9652_hw_pointer(rme9652);
1820 }
1821 
1822 static char *rme9652_channel_buffer_location(struct snd_rme9652 *rme9652,
1823                          int stream,
1824                          int channel)
1825 
1826 {
1827     int mapped_channel;
1828 
1829     if (snd_BUG_ON(channel < 0 || channel >= RME9652_NCHANNELS))
1830         return NULL;
1831         
1832     mapped_channel = rme9652->channel_map[channel];
1833     if (mapped_channel < 0)
1834         return NULL;
1835     
1836     if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1837         return rme9652->capture_buffer +
1838             (mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
1839     } else {
1840         return rme9652->playback_buffer +
1841             (mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
1842     }
1843 }
1844 
1845 static int snd_rme9652_playback_copy(struct snd_pcm_substream *substream,
1846                      int channel, unsigned long pos,
1847                      void __user *src, unsigned long count)
1848 {
1849     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1850     char *channel_buf;
1851 
1852     if (snd_BUG_ON(pos + count > RME9652_CHANNEL_BUFFER_BYTES))
1853         return -EINVAL;
1854 
1855     channel_buf = rme9652_channel_buffer_location (rme9652,
1856                                substream->pstr->stream,
1857                                channel);
1858     if (snd_BUG_ON(!channel_buf))
1859         return -EIO;
1860     if (copy_from_user(channel_buf + pos, src, count))
1861         return -EFAULT;
1862     return 0;
1863 }
1864 
1865 static int snd_rme9652_playback_copy_kernel(struct snd_pcm_substream *substream,
1866                         int channel, unsigned long pos,
1867                         void *src, unsigned long count)
1868 {
1869     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1870     char *channel_buf;
1871 
1872     channel_buf = rme9652_channel_buffer_location(rme9652,
1873                               substream->pstr->stream,
1874                               channel);
1875     if (snd_BUG_ON(!channel_buf))
1876         return -EIO;
1877     memcpy(channel_buf + pos, src, count);
1878     return 0;
1879 }
1880 
1881 static int snd_rme9652_capture_copy(struct snd_pcm_substream *substream,
1882                     int channel, unsigned long pos,
1883                     void __user *dst, unsigned long count)
1884 {
1885     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1886     char *channel_buf;
1887 
1888     if (snd_BUG_ON(pos + count > RME9652_CHANNEL_BUFFER_BYTES))
1889         return -EINVAL;
1890 
1891     channel_buf = rme9652_channel_buffer_location (rme9652,
1892                                substream->pstr->stream,
1893                                channel);
1894     if (snd_BUG_ON(!channel_buf))
1895         return -EIO;
1896     if (copy_to_user(dst, channel_buf + pos, count))
1897         return -EFAULT;
1898     return 0;
1899 }
1900 
1901 static int snd_rme9652_capture_copy_kernel(struct snd_pcm_substream *substream,
1902                        int channel, unsigned long pos,
1903                        void *dst, unsigned long count)
1904 {
1905     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1906     char *channel_buf;
1907 
1908     channel_buf = rme9652_channel_buffer_location(rme9652,
1909                               substream->pstr->stream,
1910                               channel);
1911     if (snd_BUG_ON(!channel_buf))
1912         return -EIO;
1913     memcpy(dst, channel_buf + pos, count);
1914     return 0;
1915 }
1916 
1917 static int snd_rme9652_hw_silence(struct snd_pcm_substream *substream,
1918                   int channel, unsigned long pos,
1919                   unsigned long count)
1920 {
1921     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1922     char *channel_buf;
1923 
1924     channel_buf = rme9652_channel_buffer_location (rme9652,
1925                                substream->pstr->stream,
1926                                channel);
1927     if (snd_BUG_ON(!channel_buf))
1928         return -EIO;
1929     memset(channel_buf + pos, 0, count);
1930     return 0;
1931 }
1932 
1933 static int snd_rme9652_reset(struct snd_pcm_substream *substream)
1934 {
1935     struct snd_pcm_runtime *runtime = substream->runtime;
1936     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1937     struct snd_pcm_substream *other;
1938     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1939         other = rme9652->capture_substream;
1940     else
1941         other = rme9652->playback_substream;
1942     if (rme9652->running)
1943         runtime->status->hw_ptr = rme9652_hw_pointer(rme9652);
1944     else
1945         runtime->status->hw_ptr = 0;
1946     if (other) {
1947         struct snd_pcm_substream *s;
1948         struct snd_pcm_runtime *oruntime = other->runtime;
1949         snd_pcm_group_for_each_entry(s, substream) {
1950             if (s == other) {
1951                 oruntime->status->hw_ptr = runtime->status->hw_ptr;
1952                 break;
1953             }
1954         }
1955     }
1956     return 0;
1957 }
1958 
1959 static int snd_rme9652_hw_params(struct snd_pcm_substream *substream,
1960                  struct snd_pcm_hw_params *params)
1961 {
1962     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
1963     int err;
1964     pid_t this_pid;
1965     pid_t other_pid;
1966 
1967     spin_lock_irq(&rme9652->lock);
1968 
1969     if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1970         rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
1971         rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= rme9652->creg_spdif_stream);
1972         this_pid = rme9652->playback_pid;
1973         other_pid = rme9652->capture_pid;
1974     } else {
1975         this_pid = rme9652->capture_pid;
1976         other_pid = rme9652->playback_pid;
1977     }
1978 
1979     if ((other_pid > 0) && (this_pid != other_pid)) {
1980 
1981         /* The other stream is open, and not by the same
1982            task as this one. Make sure that the parameters
1983            that matter are the same.
1984          */
1985 
1986         if ((int)params_rate(params) !=
1987             rme9652_adat_sample_rate(rme9652)) {
1988             spin_unlock_irq(&rme9652->lock);
1989             _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1990             return -EBUSY;
1991         }
1992 
1993         if (params_period_size(params) != rme9652->period_bytes / 4) {
1994             spin_unlock_irq(&rme9652->lock);
1995             _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1996             return -EBUSY;
1997         }
1998 
1999         /* We're fine. */
2000 
2001         spin_unlock_irq(&rme9652->lock);
2002         return 0;
2003 
2004     } else {
2005         spin_unlock_irq(&rme9652->lock);
2006     }
2007 
2008     /* how to make sure that the rate matches an externally-set one ?
2009      */
2010 
2011     err = rme9652_set_rate(rme9652, params_rate(params));
2012     if (err < 0) {
2013         _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
2014         return err;
2015     }
2016 
2017     err = rme9652_set_interrupt_interval(rme9652, params_period_size(params));
2018     if (err < 0) {
2019         _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2020         return err;
2021     }
2022 
2023     return 0;
2024 }
2025 
2026 static int snd_rme9652_channel_info(struct snd_pcm_substream *substream,
2027                     struct snd_pcm_channel_info *info)
2028 {
2029     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2030     int chn;
2031 
2032     if (snd_BUG_ON(info->channel >= RME9652_NCHANNELS))
2033         return -EINVAL;
2034 
2035     chn = rme9652->channel_map[array_index_nospec(info->channel,
2036                               RME9652_NCHANNELS)];
2037     if (chn < 0)
2038         return -EINVAL;
2039 
2040     info->offset = chn * RME9652_CHANNEL_BUFFER_BYTES;
2041     info->first = 0;
2042     info->step = 32;
2043     return 0;
2044 }
2045 
2046 static int snd_rme9652_ioctl(struct snd_pcm_substream *substream,
2047                  unsigned int cmd, void *arg)
2048 {
2049     switch (cmd) {
2050     case SNDRV_PCM_IOCTL1_RESET:
2051     {
2052         return snd_rme9652_reset(substream);
2053     }
2054     case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
2055     {
2056         struct snd_pcm_channel_info *info = arg;
2057         return snd_rme9652_channel_info(substream, info);
2058     }
2059     default:
2060         break;
2061     }
2062 
2063     return snd_pcm_lib_ioctl(substream, cmd, arg);
2064 }
2065 
2066 static void rme9652_silence_playback(struct snd_rme9652 *rme9652)
2067 {
2068     memset(rme9652->playback_buffer, 0, RME9652_DMA_AREA_BYTES);
2069 }
2070 
2071 static int snd_rme9652_trigger(struct snd_pcm_substream *substream,
2072                    int cmd)
2073 {
2074     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2075     struct snd_pcm_substream *other;
2076     int running;
2077     spin_lock(&rme9652->lock);
2078     running = rme9652->running;
2079     switch (cmd) {
2080     case SNDRV_PCM_TRIGGER_START:
2081         running |= 1 << substream->stream;
2082         break;
2083     case SNDRV_PCM_TRIGGER_STOP:
2084         running &= ~(1 << substream->stream);
2085         break;
2086     default:
2087         snd_BUG();
2088         spin_unlock(&rme9652->lock);
2089         return -EINVAL;
2090     }
2091     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2092         other = rme9652->capture_substream;
2093     else
2094         other = rme9652->playback_substream;
2095 
2096     if (other) {
2097         struct snd_pcm_substream *s;
2098         snd_pcm_group_for_each_entry(s, substream) {
2099             if (s == other) {
2100                 snd_pcm_trigger_done(s, substream);
2101                 if (cmd == SNDRV_PCM_TRIGGER_START)
2102                     running |= 1 << s->stream;
2103                 else
2104                     running &= ~(1 << s->stream);
2105                 goto _ok;
2106             }
2107         }
2108         if (cmd == SNDRV_PCM_TRIGGER_START) {
2109             if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
2110                 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2111                 rme9652_silence_playback(rme9652);
2112         } else {
2113             if (running &&
2114                 substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2115                 rme9652_silence_playback(rme9652);
2116         }
2117     } else {
2118         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 
2119             rme9652_silence_playback(rme9652);
2120     }
2121  _ok:
2122     snd_pcm_trigger_done(substream, substream);
2123     if (!rme9652->running && running)
2124         rme9652_start(rme9652);
2125     else if (rme9652->running && !running)
2126         rme9652_stop(rme9652);
2127     rme9652->running = running;
2128     spin_unlock(&rme9652->lock);
2129 
2130     return 0;
2131 }
2132 
2133 static int snd_rme9652_prepare(struct snd_pcm_substream *substream)
2134 {
2135     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2136     unsigned long flags;
2137 
2138     spin_lock_irqsave(&rme9652->lock, flags);
2139     if (!rme9652->running)
2140         rme9652_reset_hw_pointer(rme9652);
2141     spin_unlock_irqrestore(&rme9652->lock, flags);
2142     return 0;
2143 }
2144 
2145 static const struct snd_pcm_hardware snd_rme9652_playback_subinfo =
2146 {
2147     .info =         (SNDRV_PCM_INFO_MMAP |
2148                  SNDRV_PCM_INFO_MMAP_VALID |
2149                  SNDRV_PCM_INFO_NONINTERLEAVED |
2150                  SNDRV_PCM_INFO_SYNC_START |
2151                  SNDRV_PCM_INFO_DOUBLE),
2152     .formats =      SNDRV_PCM_FMTBIT_S32_LE,
2153     .rates =        (SNDRV_PCM_RATE_44100 | 
2154                  SNDRV_PCM_RATE_48000 | 
2155                  SNDRV_PCM_RATE_88200 | 
2156                  SNDRV_PCM_RATE_96000),
2157     .rate_min =     44100,
2158     .rate_max =     96000,
2159     .channels_min =     10,
2160     .channels_max =     26,
2161     .buffer_bytes_max = RME9652_CHANNEL_BUFFER_BYTES * 26,
2162     .period_bytes_min = (64 * 4) * 10,
2163     .period_bytes_max = (8192 * 4) * 26,
2164     .periods_min =      2,
2165     .periods_max =      2,
2166     .fifo_size =        0,
2167 };
2168 
2169 static const struct snd_pcm_hardware snd_rme9652_capture_subinfo =
2170 {
2171     .info =         (SNDRV_PCM_INFO_MMAP |
2172                  SNDRV_PCM_INFO_MMAP_VALID |
2173                  SNDRV_PCM_INFO_NONINTERLEAVED |
2174                  SNDRV_PCM_INFO_SYNC_START),
2175     .formats =      SNDRV_PCM_FMTBIT_S32_LE,
2176     .rates =        (SNDRV_PCM_RATE_44100 | 
2177                  SNDRV_PCM_RATE_48000 | 
2178                  SNDRV_PCM_RATE_88200 | 
2179                  SNDRV_PCM_RATE_96000),
2180     .rate_min =     44100,
2181     .rate_max =     96000,
2182     .channels_min =     10,
2183     .channels_max =     26,
2184     .buffer_bytes_max = RME9652_CHANNEL_BUFFER_BYTES *26,
2185     .period_bytes_min = (64 * 4) * 10,
2186     .period_bytes_max = (8192 * 4) * 26,
2187     .periods_min =      2,
2188     .periods_max =      2,
2189     .fifo_size =        0,
2190 };
2191 
2192 static const unsigned int period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
2193 
2194 static const struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
2195     .count = ARRAY_SIZE(period_sizes),
2196     .list = period_sizes,
2197     .mask = 0
2198 };
2199 
2200 static int snd_rme9652_hw_rule_channels(struct snd_pcm_hw_params *params,
2201                     struct snd_pcm_hw_rule *rule)
2202 {
2203     struct snd_rme9652 *rme9652 = rule->private;
2204     struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2205     unsigned int list[2] = { rme9652->ds_channels, rme9652->ss_channels };
2206     return snd_interval_list(c, 2, list, 0);
2207 }
2208 
2209 static int snd_rme9652_hw_rule_channels_rate(struct snd_pcm_hw_params *params,
2210                          struct snd_pcm_hw_rule *rule)
2211 {
2212     struct snd_rme9652 *rme9652 = rule->private;
2213     struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2214     struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
2215     if (r->min > 48000) {
2216         struct snd_interval t = {
2217             .min = rme9652->ds_channels,
2218             .max = rme9652->ds_channels,
2219             .integer = 1,
2220         };
2221         return snd_interval_refine(c, &t);
2222     } else if (r->max < 88200) {
2223         struct snd_interval t = {
2224             .min = rme9652->ss_channels,
2225             .max = rme9652->ss_channels,
2226             .integer = 1,
2227         };
2228         return snd_interval_refine(c, &t);
2229     }
2230     return 0;
2231 }
2232 
2233 static int snd_rme9652_hw_rule_rate_channels(struct snd_pcm_hw_params *params,
2234                          struct snd_pcm_hw_rule *rule)
2235 {
2236     struct snd_rme9652 *rme9652 = rule->private;
2237     struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
2238     struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
2239     if (c->min >= rme9652->ss_channels) {
2240         struct snd_interval t = {
2241             .min = 44100,
2242             .max = 48000,
2243             .integer = 1,
2244         };
2245         return snd_interval_refine(r, &t);
2246     } else if (c->max <= rme9652->ds_channels) {
2247         struct snd_interval t = {
2248             .min = 88200,
2249             .max = 96000,
2250             .integer = 1,
2251         };
2252         return snd_interval_refine(r, &t);
2253     }
2254     return 0;
2255 }
2256 
2257 static int snd_rme9652_playback_open(struct snd_pcm_substream *substream)
2258 {
2259     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2260     struct snd_pcm_runtime *runtime = substream->runtime;
2261 
2262     spin_lock_irq(&rme9652->lock);
2263 
2264     snd_pcm_set_sync(substream);
2265 
2266         runtime->hw = snd_rme9652_playback_subinfo;
2267     snd_pcm_set_runtime_buffer(substream, &rme9652->playback_dma_buf);
2268 
2269     if (rme9652->capture_substream == NULL) {
2270         rme9652_stop(rme9652);
2271         rme9652_set_thru(rme9652, -1, 0);
2272     }
2273 
2274     rme9652->playback_pid = current->pid;
2275     rme9652->playback_substream = substream;
2276 
2277     spin_unlock_irq(&rme9652->lock);
2278 
2279     snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
2280     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
2281     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2282                  snd_rme9652_hw_rule_channels, rme9652,
2283                  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2284     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2285                  snd_rme9652_hw_rule_channels_rate, rme9652,
2286                  SNDRV_PCM_HW_PARAM_RATE, -1);
2287     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2288                  snd_rme9652_hw_rule_rate_channels, rme9652,
2289                  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2290 
2291     rme9652->creg_spdif_stream = rme9652->creg_spdif;
2292     rme9652->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2293     snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
2294                SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
2295     return 0;
2296 }
2297 
2298 static int snd_rme9652_playback_release(struct snd_pcm_substream *substream)
2299 {
2300     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2301 
2302     spin_lock_irq(&rme9652->lock);
2303 
2304     rme9652->playback_pid = -1;
2305     rme9652->playback_substream = NULL;
2306 
2307     spin_unlock_irq(&rme9652->lock);
2308 
2309     rme9652->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
2310     snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
2311                SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
2312     return 0;
2313 }
2314 
2315 
2316 static int snd_rme9652_capture_open(struct snd_pcm_substream *substream)
2317 {
2318     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2319     struct snd_pcm_runtime *runtime = substream->runtime;
2320 
2321     spin_lock_irq(&rme9652->lock);
2322 
2323     snd_pcm_set_sync(substream);
2324 
2325     runtime->hw = snd_rme9652_capture_subinfo;
2326     snd_pcm_set_runtime_buffer(substream, &rme9652->capture_dma_buf);
2327 
2328     if (rme9652->playback_substream == NULL) {
2329         rme9652_stop(rme9652);
2330         rme9652_set_thru(rme9652, -1, 0);
2331     }
2332 
2333     rme9652->capture_pid = current->pid;
2334     rme9652->capture_substream = substream;
2335 
2336     spin_unlock_irq(&rme9652->lock);
2337 
2338     snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
2339     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
2340     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2341                  snd_rme9652_hw_rule_channels, rme9652,
2342                  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2343     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2344                  snd_rme9652_hw_rule_channels_rate, rme9652,
2345                  SNDRV_PCM_HW_PARAM_RATE, -1);
2346     snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2347                  snd_rme9652_hw_rule_rate_channels, rme9652,
2348                  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2349     return 0;
2350 }
2351 
2352 static int snd_rme9652_capture_release(struct snd_pcm_substream *substream)
2353 {
2354     struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
2355 
2356     spin_lock_irq(&rme9652->lock);
2357 
2358     rme9652->capture_pid = -1;
2359     rme9652->capture_substream = NULL;
2360 
2361     spin_unlock_irq(&rme9652->lock);
2362     return 0;
2363 }
2364 
2365 static const struct snd_pcm_ops snd_rme9652_playback_ops = {
2366     .open =     snd_rme9652_playback_open,
2367     .close =    snd_rme9652_playback_release,
2368     .ioctl =    snd_rme9652_ioctl,
2369     .hw_params =    snd_rme9652_hw_params,
2370     .prepare =  snd_rme9652_prepare,
2371     .trigger =  snd_rme9652_trigger,
2372     .pointer =  snd_rme9652_hw_pointer,
2373     .copy_user =    snd_rme9652_playback_copy,
2374     .copy_kernel =  snd_rme9652_playback_copy_kernel,
2375     .fill_silence = snd_rme9652_hw_silence,
2376 };
2377 
2378 static const struct snd_pcm_ops snd_rme9652_capture_ops = {
2379     .open =     snd_rme9652_capture_open,
2380     .close =    snd_rme9652_capture_release,
2381     .ioctl =    snd_rme9652_ioctl,
2382     .hw_params =    snd_rme9652_hw_params,
2383     .prepare =  snd_rme9652_prepare,
2384     .trigger =  snd_rme9652_trigger,
2385     .pointer =  snd_rme9652_hw_pointer,
2386     .copy_user =    snd_rme9652_capture_copy,
2387     .copy_kernel =  snd_rme9652_capture_copy_kernel,
2388 };
2389 
2390 static int snd_rme9652_create_pcm(struct snd_card *card,
2391                   struct snd_rme9652 *rme9652)
2392 {
2393     struct snd_pcm *pcm;
2394     int err;
2395 
2396     err = snd_pcm_new(card, rme9652->card_name, 0, 1, 1, &pcm);
2397     if (err < 0)
2398         return err;
2399 
2400     rme9652->pcm = pcm;
2401     pcm->private_data = rme9652;
2402     strcpy(pcm->name, rme9652->card_name);
2403 
2404     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme9652_playback_ops);
2405     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme9652_capture_ops);
2406 
2407     pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2408 
2409     return 0;
2410 }
2411 
2412 static int snd_rme9652_create(struct snd_card *card,
2413                   struct snd_rme9652 *rme9652,
2414                   int precise_ptr)
2415 {
2416     struct pci_dev *pci = rme9652->pci;
2417     int err;
2418     int status;
2419     unsigned short rev;
2420 
2421     rme9652->irq = -1;
2422     rme9652->card = card;
2423 
2424     pci_read_config_word(rme9652->pci, PCI_CLASS_REVISION, &rev);
2425 
2426     switch (rev & 0xff) {
2427     case 3:
2428     case 4:
2429     case 8:
2430     case 9:
2431         break;
2432 
2433     default:
2434         /* who knows? */
2435         return -ENODEV;
2436     }
2437 
2438     err = pcim_enable_device(pci);
2439     if (err < 0)
2440         return err;
2441 
2442     spin_lock_init(&rme9652->lock);
2443 
2444     err = pci_request_regions(pci, "rme9652");
2445     if (err < 0)
2446         return err;
2447     rme9652->port = pci_resource_start(pci, 0);
2448     rme9652->iobase = devm_ioremap(&pci->dev, rme9652->port, RME9652_IO_EXTENT);
2449     if (rme9652->iobase == NULL) {
2450         dev_err(card->dev, "unable to remap region 0x%lx-0x%lx\n",
2451             rme9652->port, rme9652->port + RME9652_IO_EXTENT - 1);
2452         return -EBUSY;
2453     }
2454     
2455     if (devm_request_irq(&pci->dev, pci->irq, snd_rme9652_interrupt,
2456                  IRQF_SHARED, KBUILD_MODNAME, rme9652)) {
2457         dev_err(card->dev, "unable to request IRQ %d\n", pci->irq);
2458         return -EBUSY;
2459     }
2460     rme9652->irq = pci->irq;
2461     card->sync_irq = rme9652->irq;
2462     rme9652->precise_ptr = precise_ptr;
2463 
2464     /* Determine the h/w rev level of the card. This seems like
2465        a particularly kludgy way to encode it, but its what RME
2466        chose to do, so we follow them ...
2467     */
2468 
2469     status = rme9652_read(rme9652, RME9652_status_register);
2470     if (rme9652_decode_spdif_rate(status&RME9652_F) == 1) {
2471         rme9652->hw_rev = 15;
2472     } else {
2473         rme9652->hw_rev = 11;
2474     }
2475 
2476     /* Differentiate between the standard Hammerfall, and the
2477        "Light", which does not have the expansion board. This
2478        method comes from information received from Mathhias
2479        Clausen at RME. Display the EEPROM and h/w revID where
2480        relevant.  
2481     */
2482 
2483     switch (rev) {
2484     case 8: /* original eprom */
2485         strcpy(card->driver, "RME9636");
2486         if (rme9652->hw_rev == 15) {
2487             rme9652->card_name = "RME Digi9636 (Rev 1.5)";
2488         } else {
2489             rme9652->card_name = "RME Digi9636";
2490         }
2491         rme9652->ss_channels = RME9636_NCHANNELS;
2492         break;
2493     case 9: /* W36_G EPROM */
2494         strcpy(card->driver, "RME9636");
2495         rme9652->card_name = "RME Digi9636 (Rev G)";
2496         rme9652->ss_channels = RME9636_NCHANNELS;
2497         break;
2498     case 4: /* W52_G EPROM */
2499         strcpy(card->driver, "RME9652");
2500         rme9652->card_name = "RME Digi9652 (Rev G)";
2501         rme9652->ss_channels = RME9652_NCHANNELS;
2502         break;
2503     case 3: /* original eprom */
2504         strcpy(card->driver, "RME9652");
2505         if (rme9652->hw_rev == 15) {
2506             rme9652->card_name = "RME Digi9652 (Rev 1.5)";
2507         } else {
2508             rme9652->card_name = "RME Digi9652";
2509         }
2510         rme9652->ss_channels = RME9652_NCHANNELS;
2511         break;
2512     }
2513 
2514     rme9652->ds_channels = (rme9652->ss_channels - 2) / 2 + 2;
2515 
2516     pci_set_master(rme9652->pci);
2517 
2518     err = snd_rme9652_initialize_memory(rme9652);
2519     if (err < 0)
2520         return err;
2521 
2522     err = snd_rme9652_create_pcm(card, rme9652);
2523     if (err < 0)
2524         return err;
2525 
2526     err = snd_rme9652_create_controls(card, rme9652);
2527     if (err < 0)
2528         return err;
2529 
2530     snd_rme9652_proc_init(rme9652);
2531 
2532     rme9652->last_spdif_sample_rate = -1;
2533     rme9652->last_adat_sample_rate = -1;
2534     rme9652->playback_pid = -1;
2535     rme9652->capture_pid = -1;
2536     rme9652->capture_substream = NULL;
2537     rme9652->playback_substream = NULL;
2538 
2539     snd_rme9652_set_defaults(rme9652);
2540 
2541     if (rme9652->hw_rev == 15) {
2542         rme9652_initialize_spdif_receiver (rme9652);
2543     }
2544 
2545     return 0;
2546 }
2547 
2548 static int snd_rme9652_probe(struct pci_dev *pci,
2549                  const struct pci_device_id *pci_id)
2550 {
2551     static int dev;
2552     struct snd_rme9652 *rme9652;
2553     struct snd_card *card;
2554     int err;
2555 
2556     if (dev >= SNDRV_CARDS)
2557         return -ENODEV;
2558     if (!enable[dev]) {
2559         dev++;
2560         return -ENOENT;
2561     }
2562 
2563     err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2564                 sizeof(struct snd_rme9652), &card);
2565 
2566     if (err < 0)
2567         return err;
2568 
2569     rme9652 = (struct snd_rme9652 *) card->private_data;
2570     card->private_free = snd_rme9652_card_free;
2571     rme9652->dev = dev;
2572     rme9652->pci = pci;
2573     err = snd_rme9652_create(card, rme9652, precise_ptr[dev]);
2574     if (err)
2575         goto error;
2576 
2577     strcpy(card->shortname, rme9652->card_name);
2578 
2579     sprintf(card->longname, "%s at 0x%lx, irq %d",
2580         card->shortname, rme9652->port, rme9652->irq);
2581     err = snd_card_register(card);
2582     if (err)
2583         goto error;
2584     pci_set_drvdata(pci, card);
2585     dev++;
2586     return 0;
2587 
2588  error:
2589     snd_card_free(card);
2590     return err;
2591 }
2592 
2593 static struct pci_driver rme9652_driver = {
2594     .name     = KBUILD_MODNAME,
2595     .id_table = snd_rme9652_ids,
2596     .probe    = snd_rme9652_probe,
2597 };
2598 
2599 module_pci_driver(rme9652_driver);