rme9652.c 73 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11 12
/*
 *   ALSA driver for RME Digi9652 audio interfaces 
 *
 *	Copyright (c) 1999 IEM - Winfried Ritsch
 *      Copyright (c) 1999-2001  Paul Davis
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
13
#include <linux/module.h>
14
#include <linux/io.h>
15
#include <linux/nospec.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21 22 23 24 25 26 27

#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/info.h>
#include <sound/asoundef.h>
#include <sound/initval.h>

#include <asm/current.h>

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
28 29
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
static bool precise_ptr[SNDRV_CARDS];			/* Enable precise pointer */
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for RME Digi9652 (Hammerfall) soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for RME Digi9652 (Hammerfall) soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable/disable specific RME96{52,36} soundcards.");
module_param_array(precise_ptr, bool, NULL, 0444);
MODULE_PARM_DESC(precise_ptr, "Enable precise pointer (doesn't work reliably).");
MODULE_AUTHOR("Paul Davis <pbd@op.net>, Winfried Ritsch");
MODULE_DESCRIPTION("RME Digi9652/Digi9636");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{RME,Hammerfall},"
		"{RME,Hammerfall-Light}}");

/* The Hammerfall has two sets of 24 ADAT + 2 S/PDIF channels, one for
   capture, one for playback. Both the ADAT and S/PDIF channels appear
   to the host CPU in the same block of memory. There is no functional
   difference between them in terms of access.
   
   The Hammerfall Light is identical to the Hammerfall, except that it
   has 2 sets 18 channels (16 ADAT + 2 S/PDIF) for capture and playback.
*/

#define RME9652_NCHANNELS       26
#define RME9636_NCHANNELS       18

/* Preferred sync source choices - used by "sync_pref" control switch */

#define RME9652_SYNC_FROM_SPDIF 0
#define RME9652_SYNC_FROM_ADAT1 1
#define RME9652_SYNC_FROM_ADAT2 2
#define RME9652_SYNC_FROM_ADAT3 3

/* Possible sources of S/PDIF input */

#define RME9652_SPDIFIN_OPTICAL 0	/* optical (ADAT1) */
#define RME9652_SPDIFIN_COAXIAL 1	/* coaxial (RCA) */
#define RME9652_SPDIFIN_INTERN  2	/* internal (CDROM) */

/* ------------- Status-Register bits --------------------- */

#define RME9652_IRQ	   (1<<0)	/* IRQ is High if not reset by irq_clear */
#define RME9652_lock_2	   (1<<1)	/* ADAT 3-PLL: 1=locked, 0=unlocked */
#define RME9652_lock_1	   (1<<2)	/* ADAT 2-PLL: 1=locked, 0=unlocked */
#define RME9652_lock_0	   (1<<3)	/* ADAT 1-PLL: 1=locked, 0=unlocked */
#define RME9652_fs48	   (1<<4)	/* sample rate is 0=44.1/88.2,1=48/96 Khz */
#define RME9652_wsel_rd	   (1<<5)	/* if Word-Clock is used and valid then 1 */
                                        /* bits 6-15 encode h/w buffer pointer position */
#define RME9652_sync_2	   (1<<16)	/* if ADAT-IN 3 in sync to system clock */
#define RME9652_sync_1	   (1<<17)	/* if ADAT-IN 2 in sync to system clock */
#define RME9652_sync_0	   (1<<18)	/* if ADAT-IN 1 in sync to system clock */
#define RME9652_DS_rd	   (1<<19)	/* 1=Double Speed Mode, 0=Normal Speed */
#define RME9652_tc_busy	   (1<<20)	/* 1=time-code copy in progress (960ms) */
#define RME9652_tc_out	   (1<<21)	/* time-code out bit */
#define RME9652_F_0	   (1<<22)	/* 000=64kHz, 100=88.2kHz, 011=96kHz  */
#define RME9652_F_1	   (1<<23)	/* 111=32kHz, 110=44.1kHz, 101=48kHz, */
#define RME9652_F_2	   (1<<24)	/* external Crystal Chip if ERF=1 */
#define RME9652_ERF	   (1<<25)	/* Error-Flag of SDPIF Receiver (1=No Lock) */
#define RME9652_buffer_id  (1<<26)	/* toggles by each interrupt on rec/play */
#define RME9652_tc_valid   (1<<27)	/* 1 = a signal is detected on time-code input */
#define RME9652_SPDIF_READ (1<<28)      /* byte available from Rev 1.5+ S/PDIF interface */

#define RME9652_sync	  (RME9652_sync_0|RME9652_sync_1|RME9652_sync_2)
#define RME9652_lock	  (RME9652_lock_0|RME9652_lock_1|RME9652_lock_2)
#define RME9652_F	  (RME9652_F_0|RME9652_F_1|RME9652_F_2)
#define rme9652_decode_spdif_rate(x) ((x)>>22)

/* Bit 6..15 : h/w buffer pointer */

#define RME9652_buf_pos	  0x000FFC0

/* Bits 31,30,29 are bits 5,4,3 of h/w pointer position on later
   Rev G EEPROMS and Rev 1.5 cards or later.
*/ 

#define RME9652_REV15_buf_pos(x) ((((x)&0xE0000000)>>26)|((x)&RME9652_buf_pos))

/* amount of io space we remap for register access. i'm not sure we
   even need this much, but 1K is nice round number :)
*/

#define RME9652_IO_EXTENT     1024

#define RME9652_init_buffer       0
#define RME9652_play_buffer       32	/* holds ptr to 26x64kBit host RAM */
#define RME9652_rec_buffer        36	/* holds ptr to 26x64kBit host RAM */
#define RME9652_control_register  64
#define RME9652_irq_clear         96
#define RME9652_time_code         100	/* useful if used with alesis adat */
#define RME9652_thru_base         128	/* 132...228 Thru for 26 channels */

/* Read-only registers */

/* Writing to any of the register locations writes to the status
   register. We'll use the first location as our point of access.
*/

#define RME9652_status_register    0

/* --------- Control-Register Bits ---------------- */


#define RME9652_start_bit	   (1<<0)	/* start record/play */
                                                /* bits 1-3 encode buffersize/latency */
#define RME9652_Master		   (1<<4)	/* Clock Mode Master=1,Slave/Auto=0 */
Joe Perches's avatar
Joe Perches committed
136
#define RME9652_IE		   (1<<5)	/* Interrupt Enable */
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
#define RME9652_freq		   (1<<6)       /* samplerate 0=44.1/88.2, 1=48/96 kHz */
#define RME9652_freq1		   (1<<7)       /* if 0, 32kHz, else always 1 */
#define RME9652_DS                 (1<<8)	/* Doule Speed 0=44.1/48, 1=88.2/96 Khz */
#define RME9652_PRO		   (1<<9)	/* S/PDIF out: 0=consumer, 1=professional */
#define RME9652_EMP		   (1<<10)	/*  Emphasis 0=None, 1=ON */
#define RME9652_Dolby		   (1<<11)	/*  Non-audio bit 1=set, 0=unset */
#define RME9652_opt_out	           (1<<12)	/* Use 1st optical OUT as SPDIF: 1=yes,0=no */
#define RME9652_wsel		   (1<<13)	/* use Wordclock as sync (overwrites master) */
#define RME9652_inp_0		   (1<<14)	/* SPDIF-IN: 00=optical (ADAT1),     */
#define RME9652_inp_1		   (1<<15)	/* 01=koaxial (Cinch), 10=Internal CDROM */
#define RME9652_SyncPref_ADAT2	   (1<<16)
#define RME9652_SyncPref_ADAT3	   (1<<17)
#define RME9652_SPDIF_RESET        (1<<18)      /* Rev 1.5+: h/w S/PDIF receiver */
#define RME9652_SPDIF_SELECT       (1<<19)
#define RME9652_SPDIF_CLOCK        (1<<20)
#define RME9652_SPDIF_WRITE        (1<<21)
#define RME9652_ADAT1_INTERNAL     (1<<22)      /* Rev 1.5+: if set, internal CD connector carries ADAT */

/* buffersize = 512Bytes * 2^n, where n is made from Bit2 ... Bit0 */

#define RME9652_latency            0x0e
#define rme9652_encode_latency(x)  (((x)&0x7)<<1)
#define rme9652_decode_latency(x)  (((x)>>1)&0x7)
#define rme9652_running_double_speed(s) ((s)->control_register & RME9652_DS)
#define RME9652_inp                (RME9652_inp_0|RME9652_inp_1)
#define rme9652_encode_spdif_in(x) (((x)&0x3)<<14)
#define rme9652_decode_spdif_in(x) (((x)>>14)&0x3)

#define RME9652_SyncPref_Mask      (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)
#define RME9652_SyncPref_ADAT1	   0
#define RME9652_SyncPref_SPDIF	   (RME9652_SyncPref_ADAT2|RME9652_SyncPref_ADAT3)

/* the size of a substream (1 mono data stream) */

#define RME9652_CHANNEL_BUFFER_SAMPLES  (16*1024)
#define RME9652_CHANNEL_BUFFER_BYTES    (4*RME9652_CHANNEL_BUFFER_SAMPLES)

/* the size of the area we need to allocate for DMA transfers. the
   size is the same regardless of the number of channels - the 
   9636 still uses the same memory area.

   Note that we allocate 1 more channel than is apparently needed
   because the h/w seems to write 1 byte beyond the end of the last
   page. Sigh.
*/

#define RME9652_DMA_AREA_BYTES ((RME9652_NCHANNELS+1) * RME9652_CHANNEL_BUFFER_BYTES)
#define RME9652_DMA_AREA_KILOBYTES (RME9652_DMA_AREA_BYTES/1024)

186
struct snd_rme9652 {
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	int dev;

	spinlock_t lock;
	int irq;
	unsigned long port;
	void __iomem *iobase;
	
	int precise_ptr;

	u32 control_register;	/* cached value */
	u32 thru_bits;		/* thru 1=on, 0=off channel 1=Bit1... channel 26= Bit26 */

	u32 creg_spdif;
	u32 creg_spdif_stream;

	char *card_name;		/* hammerfall or hammerfall light names */

        size_t hw_offsetmask;     	/* &-with status register to get real hw_offset */
	size_t prev_hw_offset;		/* previous hw offset */
	size_t max_jitter;		/* maximum jitter in frames for 
					   hw pointer */
	size_t period_bytes;		/* guess what this is */

	unsigned char ds_channels;
	unsigned char ss_channels;	/* different for hammerfall/hammerfall-light */

	struct snd_dma_buffer playback_dma_buf;
	struct snd_dma_buffer capture_dma_buf;

	unsigned char *capture_buffer;	/* suitably aligned address */
	unsigned char *playback_buffer;	/* suitably aligned address */

	pid_t capture_pid;
	pid_t playback_pid;

222 223
	struct snd_pcm_substream *capture_substream;
	struct snd_pcm_substream *playback_substream;
Linus Torvalds's avatar
Linus Torvalds committed
224 225 226 227 228 229 230 231
	int running;

        int passthru;                   /* non-zero if doing pass-thru */
        int hw_rev;                     /* h/w rev * 10 (i.e. 1.5 has hw_rev = 15) */

	int last_spdif_sample_rate;	/* so that we can catch externally ... */
	int last_adat_sample_rate;	/* ... induced rate changes            */

232
	const char *channel_map;
Linus Torvalds's avatar
Linus Torvalds committed
233

234 235
	struct snd_card *card;
	struct snd_pcm *pcm;
Linus Torvalds's avatar
Linus Torvalds committed
236
	struct pci_dev *pci;
237
	struct snd_kcontrol *spdif_ctl;
Linus Torvalds's avatar
Linus Torvalds committed
238

239
};
Linus Torvalds's avatar
Linus Torvalds committed
240 241 242 243 244 245 246 247 248

/* These tables map the ALSA channels 1..N to the channels that we
   need to use in order to find the relevant channel buffer. RME
   refer to this kind of mapping as between "the ADAT channel and
   the DMA channel." We index it using the logical audio channel,
   and the value is the DMA channel (i.e. channel buffer number)
   where the data for that channel can be read/written from/to.
*/

249
static const char channel_map_9652_ss[26] = {
Linus Torvalds's avatar
Linus Torvalds committed
250 251 252 253
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
	18, 19, 20, 21, 22, 23, 24, 25
};

254
static const char channel_map_9636_ss[26] = {
Linus Torvalds's avatar
Linus Torvalds committed
255 256 257 258 259 260 261
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
	/* channels 16 and 17 are S/PDIF */
	24, 25,
	/* channels 18-25 don't exist */
	-1, -1, -1, -1, -1, -1, -1, -1
};

262
static const char channel_map_9652_ds[26] = {
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267 268 269 270
	/* ADAT channels are remapped */
	1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
	/* channels 12 and 13 are S/PDIF */
	24, 25,
	/* others don't exist */
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

271
static const char channel_map_9636_ds[26] = {
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274
	/* ADAT channels are remapped */
	1, 3, 5, 7, 9, 11, 13, 15,
	/* channels 8 and 9 are S/PDIF */
275
	24, 25,
Linus Torvalds's avatar
Linus Torvalds committed
276 277 278 279 280 281
	/* others don't exist */
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
{
282
	return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev, size, dmab);
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285 286
}

static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
{
287 288
	if (dmab->area)
		snd_dma_free_pages(dmab);
Linus Torvalds's avatar
Linus Torvalds committed
289 290 291
}


292
static const struct pci_device_id snd_rme9652_ids[] = {
Linus Torvalds's avatar
Linus Torvalds committed
293 294 295 296 297 298 299 300 301 302 303
	{
		.vendor	   = 0x10ee,
		.device	   = 0x3fc4,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
	},	/* RME Digi9652 */
	{ 0, },
};

MODULE_DEVICE_TABLE(pci, snd_rme9652_ids);

304
static inline void rme9652_write(struct snd_rme9652 *rme9652, int reg, int val)
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307 308
{
	writel(val, rme9652->iobase + reg);
}

309
static inline unsigned int rme9652_read(struct snd_rme9652 *rme9652, int reg)
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312 313
{
	return readl(rme9652->iobase + reg);
}

314
static inline int snd_rme9652_use_is_exclusive(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
315 316 317 318 319 320 321 322 323 324 325 326 327
{
	unsigned long flags;
	int ret = 1;

	spin_lock_irqsave(&rme9652->lock, flags);
	if ((rme9652->playback_pid != rme9652->capture_pid) &&
	    (rme9652->playback_pid >= 0) && (rme9652->capture_pid >= 0)) {
		ret = 0;
	}
	spin_unlock_irqrestore(&rme9652->lock, flags);
	return ret;
}

328
static inline int rme9652_adat_sample_rate(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335 336 337 338
{
	if (rme9652_running_double_speed(rme9652)) {
		return (rme9652_read(rme9652, RME9652_status_register) &
			RME9652_fs48) ? 96000 : 88200;
	} else {
		return (rme9652_read(rme9652, RME9652_status_register) &
			RME9652_fs48) ? 48000 : 44100;
	}
}

339
static inline void rme9652_compute_period_size(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
340 341 342 343 344 345 346 347 348 349
{
	unsigned int i;

	i = rme9652->control_register & RME9652_latency;
	rme9652->period_bytes = 1 << ((rme9652_decode_latency(i) + 8));
	rme9652->hw_offsetmask = 
		(rme9652->period_bytes * 2 - 1) & RME9652_buf_pos;
	rme9652->max_jitter = 80;
}

350
static snd_pcm_uframes_t rme9652_hw_pointer(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
{
	int status;
	unsigned int offset, frag;
	snd_pcm_uframes_t period_size = rme9652->period_bytes / 4;
	snd_pcm_sframes_t delta;

	status = rme9652_read(rme9652, RME9652_status_register);
	if (!rme9652->precise_ptr)
		return (status & RME9652_buffer_id) ? period_size : 0;
	offset = status & RME9652_buf_pos;

	/* The hardware may give a backward movement for up to 80 frames
           Martin Kirst <martin.kirst@freenet.de> knows the details.
	*/

	delta = rme9652->prev_hw_offset - offset;
	delta &= 0xffff;
	if (delta <= (snd_pcm_sframes_t)rme9652->max_jitter * 4)
		offset = rme9652->prev_hw_offset;
	else
		rme9652->prev_hw_offset = offset;
	offset &= rme9652->hw_offsetmask;
	offset /= 4;
	frag = status & RME9652_buffer_id;

	if (offset < period_size) {
		if (offset > rme9652->max_jitter) {
			if (frag)
379 380 381
				dev_err(rme9652->card->dev,
					"Unexpected hw_pointer position (bufid == 0): status: %x offset: %d\n",
					status, offset);
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384
		} else if (!frag)
			return 0;
		offset -= rme9652->max_jitter;
385
		if ((int)offset < 0)
Linus Torvalds's avatar
Linus Torvalds committed
386 387 388 389
			offset += period_size * 2;
	} else {
		if (offset > period_size + rme9652->max_jitter) {
			if (!frag)
390 391 392
				dev_err(rme9652->card->dev,
					"Unexpected hw_pointer position (bufid == 1): status: %x offset: %d\n",
					status, offset);
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396 397 398 399 400
		} else if (frag)
			return period_size;
		offset -= rme9652->max_jitter;
	}

	return offset;
}

401
static inline void rme9652_reset_hw_pointer(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
{
	int i;

	/* reset the FIFO pointer to zero. We do this by writing to 8
	   registers, each of which is a 32bit wide register, and set
	   them all to zero. Note that s->iobase is a pointer to
	   int32, not pointer to char.  
	*/

	for (i = 0; i < 8; i++) {
		rme9652_write(rme9652, i * 4, 0);
		udelay(10);
	}
	rme9652->prev_hw_offset = 0;
}

418
static inline void rme9652_start(struct snd_rme9652 *s)
Linus Torvalds's avatar
Linus Torvalds committed
419 420 421 422 423
{
	s->control_register |= (RME9652_IE | RME9652_start_bit);
	rme9652_write(s, RME9652_control_register, s->control_register);
}

424
static inline void rme9652_stop(struct snd_rme9652 *s)
Linus Torvalds's avatar
Linus Torvalds committed
425 426 427 428 429
{
	s->control_register &= ~(RME9652_start_bit | RME9652_IE);
	rme9652_write(s, RME9652_control_register, s->control_register);
}

430
static int rme9652_set_interrupt_interval(struct snd_rme9652 *s,
Linus Torvalds's avatar
Linus Torvalds committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
					  unsigned int frames)
{
	int restart = 0;
	int n;

	spin_lock_irq(&s->lock);

	if ((restart = s->running)) {
		rme9652_stop(s);
	}

	frames >>= 7;
	n = 0;
	while (frames) {
		n++;
		frames >>= 1;
	}

	s->control_register &= ~RME9652_latency;
	s->control_register |= rme9652_encode_latency(n);

	rme9652_write(s, RME9652_control_register, s->control_register);

	rme9652_compute_period_size(s);

	if (restart)
		rme9652_start(s);

	spin_unlock_irq(&s->lock);

	return 0;
}

464
static int rme9652_set_rate(struct snd_rme9652 *rme9652, int rate)
Linus Torvalds's avatar
Linus Torvalds committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
{
	int restart;
	int reject_if_open = 0;
	int xrate;

	if (!snd_rme9652_use_is_exclusive (rme9652)) {
		return -EBUSY;
	}

	/* Changing from a "single speed" to a "double speed" rate is
	   not allowed if any substreams are open. This is because
	   such a change causes a shift in the location of 
	   the DMA buffers and a reduction in the number of available
	   buffers. 

	   Note that a similar but essentially insoluble problem
	   exists for externally-driven rate changes. All we can do
	   is to flag rate changes in the read/write routines.
	 */

	spin_lock_irq(&rme9652->lock);
	xrate = rme9652_adat_sample_rate(rme9652);

	switch (rate) {
	case 44100:
		if (xrate > 48000) {
			reject_if_open = 1;
		}
		rate = 0;
		break;
	case 48000:
		if (xrate > 48000) {
			reject_if_open = 1;
		}
		rate = RME9652_freq;
		break;
	case 88200:
		if (xrate < 48000) {
			reject_if_open = 1;
		}
		rate = RME9652_DS;
		break;
	case 96000:
		if (xrate < 48000) {
			reject_if_open = 1;
		}
		rate = RME9652_DS | RME9652_freq;
		break;
	default:
		spin_unlock_irq(&rme9652->lock);
		return -EINVAL;
	}

	if (reject_if_open && (rme9652->capture_pid >= 0 || rme9652->playback_pid >= 0)) {
		spin_unlock_irq(&rme9652->lock);
		return -EBUSY;
	}

	if ((restart = rme9652->running)) {
		rme9652_stop(rme9652);
	}
	rme9652->control_register &= ~(RME9652_freq | RME9652_DS);
	rme9652->control_register |= rate;
	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	if (restart) {
		rme9652_start(rme9652);
	}

	if (rate & RME9652_DS) {
		if (rme9652->ss_channels == RME9652_NCHANNELS) {
			rme9652->channel_map = channel_map_9652_ds;
		} else {
			rme9652->channel_map = channel_map_9636_ds;
		}
	} else {
		if (rme9652->ss_channels == RME9652_NCHANNELS) {
			rme9652->channel_map = channel_map_9652_ss;
		} else {
			rme9652->channel_map = channel_map_9636_ss;
		}
	}

	spin_unlock_irq(&rme9652->lock);
	return 0;
}

552
static void rme9652_set_thru(struct snd_rme9652 *rme9652, int channel, int enable)
Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
{
	int i;

	rme9652->passthru = 0;

	if (channel < 0) {

		/* set thru for all channels */

		if (enable) {
			for (i = 0; i < RME9652_NCHANNELS; i++) {
				rme9652->thru_bits |= (1 << i);
				rme9652_write(rme9652, RME9652_thru_base + i * 4, 1);
			}
		} else {
			for (i = 0; i < RME9652_NCHANNELS; i++) {
				rme9652->thru_bits &= ~(1 << i);
				rme9652_write(rme9652, RME9652_thru_base + i * 4, 0);
			}
		}

	} else {
		int mapped_channel;

		mapped_channel = rme9652->channel_map[channel];

		if (enable) {
			rme9652->thru_bits |= (1 << mapped_channel);
		} else {
			rme9652->thru_bits &= ~(1 << mapped_channel);
		}

		rme9652_write(rme9652,
			       RME9652_thru_base + mapped_channel * 4,
			       enable ? 1 : 0);			       
	}
}

591
static int rme9652_set_passthru(struct snd_rme9652 *rme9652, int onoff)
Linus Torvalds's avatar
Linus Torvalds committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
{
	if (onoff) {
		rme9652_set_thru(rme9652, -1, 1);

		/* we don't want interrupts, so do a
		   custom version of rme9652_start().
		*/

		rme9652->control_register =
			RME9652_inp_0 | 
			rme9652_encode_latency(7) |
			RME9652_start_bit;

		rme9652_reset_hw_pointer(rme9652);

		rme9652_write(rme9652, RME9652_control_register,
			      rme9652->control_register);
		rme9652->passthru = 1;
	} else {
		rme9652_set_thru(rme9652, -1, 0);
		rme9652_stop(rme9652);		
		rme9652->passthru = 0;
	}

	return 0;
}

619
static void rme9652_spdif_set_bit (struct snd_rme9652 *rme9652, int mask, int onoff)
Linus Torvalds's avatar
Linus Torvalds committed
620 621 622 623 624 625 626 627 628
{
	if (onoff) 
		rme9652->control_register |= mask;
	else 
		rme9652->control_register &= ~mask;
		
	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);
}

629
static void rme9652_spdif_write_byte (struct snd_rme9652 *rme9652, const int val)
Linus Torvalds's avatar
Linus Torvalds committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
{
	long mask;
	long i;

	for (i = 0, mask = 0x80; i < 8; i++, mask >>= 1) {
		if (val & mask)
			rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 1);
		else 
			rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_WRITE, 0);

		rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
		rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
	}
}

645
static int rme9652_spdif_read_byte (struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
{
	long mask;
	long val;
	long i;

	val = 0;

	for (i = 0, mask = 0x80;  i < 8; i++, mask >>= 1) {
		rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 1);
		if (rme9652_read (rme9652, RME9652_status_register) & RME9652_SPDIF_READ)
			val |= mask;
		rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_CLOCK, 0);
	}

	return val;
}

663
static void rme9652_write_spdif_codec (struct snd_rme9652 *rme9652, const int address, const int data)
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666 667 668 669 670 671 672
{
	rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
	rme9652_spdif_write_byte (rme9652, 0x20);
	rme9652_spdif_write_byte (rme9652, address);
	rme9652_spdif_write_byte (rme9652, data);
	rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
}


673
static int rme9652_spdif_read_codec (struct snd_rme9652 *rme9652, const int address)
Linus Torvalds's avatar
Linus Torvalds committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
{
	int ret;

	rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);
	rme9652_spdif_write_byte (rme9652, 0x20);
	rme9652_spdif_write_byte (rme9652, address);
	rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);
	rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 1);

	rme9652_spdif_write_byte (rme9652, 0x21);
	ret = rme9652_spdif_read_byte (rme9652);
	rme9652_spdif_set_bit (rme9652, RME9652_SPDIF_SELECT, 0);

	return ret;
}

690
static void rme9652_initialize_spdif_receiver (struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
691 692 693 694 695 696 697 698 699 700
{
	/* XXX what unsets this ? */

	rme9652->control_register |= RME9652_SPDIF_RESET;

	rme9652_write_spdif_codec (rme9652, 4, 0x40);
	rme9652_write_spdif_codec (rme9652, 17, 0x13);
	rme9652_write_spdif_codec (rme9652, 6, 0x02);
}

701
static inline int rme9652_spdif_sample_rate(struct snd_rme9652 *s)
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
{
	unsigned int rate_bits;

	if (rme9652_read(s, RME9652_status_register) & RME9652_ERF) {
		return -1;	/* error condition */
	}
	
	if (s->hw_rev == 15) {

		int x, y, ret;
		
		x = rme9652_spdif_read_codec (s, 30);

		if (x != 0) 
			y = 48000 * 64 / x;
		else
			y = 0;

		if      (y > 30400 && y < 33600)  ret = 32000; 
		else if (y > 41900 && y < 46000)  ret = 44100;
		else if (y > 46000 && y < 50400)  ret = 48000;
		else if (y > 60800 && y < 67200)  ret = 64000;
		else if (y > 83700 && y < 92000)  ret = 88200;
		else if (y > 92000 && y < 100000) ret = 96000;
		else                              ret = 0;
		return ret;
	}

	rate_bits = rme9652_read(s, RME9652_status_register) & RME9652_F;

	switch (rme9652_decode_spdif_rate(rate_bits)) {
	case 0x7:
		return 32000;
		break;

	case 0x6:
		return 44100;
		break;

	case 0x5:
		return 48000;
		break;

	case 0x4:
		return 88200;
		break;

	case 0x3:
		return 96000;
		break;

	case 0x0:
		return 64000;
		break;

	default:
758 759
		dev_err(s->card->dev,
			"%s: unknown S/PDIF input rate (bits = 0x%x)\n",
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762 763 764 765 766 767 768 769
			   s->card_name, rate_bits);
		return 0;
		break;
	}
}

/*-----------------------------------------------------------------------------
  Control Interface
  ----------------------------------------------------------------------------*/

770
static u32 snd_rme9652_convert_from_aes(struct snd_aes_iec958 *aes)
Linus Torvalds's avatar
Linus Torvalds committed
771 772 773 774 775 776 777 778 779 780 781
{
	u32 val = 0;
	val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? RME9652_PRO : 0;
	val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? RME9652_Dolby : 0;
	if (val & RME9652_PRO)
		val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? RME9652_EMP : 0;
	else
		val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? RME9652_EMP : 0;
	return val;
}

782
static void snd_rme9652_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787 788 789 790 791
{
	aes->status[0] = ((val & RME9652_PRO) ? IEC958_AES0_PROFESSIONAL : 0) |
			 ((val & RME9652_Dolby) ? IEC958_AES0_NONAUDIO : 0);
	if (val & RME9652_PRO)
		aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
	else
		aes->status[0] |= (val & RME9652_EMP) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
}

792
static int snd_rme9652_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
793 794 795 796 797 798
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

799
static int snd_rme9652_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
800
{
801
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
802 803 804 805 806
	
	snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif);
	return 0;
}

807
static int snd_rme9652_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
808
{
809
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813 814 815 816 817 818 819 820
	int change;
	u32 val;
	
	val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
	spin_lock_irq(&rme9652->lock);
	change = val != rme9652->creg_spdif;
	rme9652->creg_spdif = val;
	spin_unlock_irq(&rme9652->lock);
	return change;
}

821
static int snd_rme9652_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
822 823 824 825 826 827
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

828
static int snd_rme9652_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
829
{
830
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835
	
	snd_rme9652_convert_to_aes(&ucontrol->value.iec958, rme9652->creg_spdif_stream);
	return 0;
}

836
static int snd_rme9652_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
837
{
838
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
839 840 841 842 843 844 845 846 847 848 849 850 851
	int change;
	u32 val;
	
	val = snd_rme9652_convert_from_aes(&ucontrol->value.iec958);
	spin_lock_irq(&rme9652->lock);
	change = val != rme9652->creg_spdif_stream;
	rme9652->creg_spdif_stream = val;
	rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= val);
	spin_unlock_irq(&rme9652->lock);
	return change;
}

852
static int snd_rme9652_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
853 854 855 856 857 858
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

859
static int snd_rme9652_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
860 861 862 863 864 865
{
	ucontrol->value.iec958.status[0] = kcontrol->private_value;
	return 0;
}

#define RME9652_ADAT1_IN(xname, xindex) \
866
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
867 868 869 870
  .info = snd_rme9652_info_adat1_in, \
  .get = snd_rme9652_get_adat1_in, \
  .put = snd_rme9652_put_adat1_in }

871
static unsigned int rme9652_adat1_in(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
872 873 874 875 876 877
{
	if (rme9652->control_register & RME9652_ADAT1_INTERNAL)
		return 1; 
	return 0;
}

878
static int rme9652_set_adat1_input(struct snd_rme9652 *rme9652, int internal)
Linus Torvalds's avatar
Linus Torvalds committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
{
	int restart = 0;

	if (internal) {
		rme9652->control_register |= RME9652_ADAT1_INTERNAL;
	} else {
		rme9652->control_register &= ~RME9652_ADAT1_INTERNAL;
	}

	/* XXX do we actually need to stop the card when we do this ? */

	if ((restart = rme9652->running)) {
		rme9652_stop(rme9652);
	}

	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	if (restart) {
		rme9652_start(rme9652);
	}

	return 0;
}

903
static int snd_rme9652_info_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
904
{
905
	static const char * const texts[2] = {"ADAT1", "Internal"};
Linus Torvalds's avatar
Linus Torvalds committed
906

907
	return snd_ctl_enum_info(uinfo, 1, 2, texts);
Linus Torvalds's avatar
Linus Torvalds committed
908 909
}

910
static int snd_rme9652_get_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
911
{
912
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
913 914 915 916 917 918 919
	
	spin_lock_irq(&rme9652->lock);
	ucontrol->value.enumerated.item[0] = rme9652_adat1_in(rme9652);
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

920
static int snd_rme9652_put_adat1_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
921
{
922
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
	int change;
	unsigned int val;
	
	if (!snd_rme9652_use_is_exclusive(rme9652))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0] % 2;
	spin_lock_irq(&rme9652->lock);
	change = val != rme9652_adat1_in(rme9652);
	if (change)
		rme9652_set_adat1_input(rme9652, val);
	spin_unlock_irq(&rme9652->lock);
	return change;
}

#define RME9652_SPDIF_IN(xname, xindex) \
938
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941
  .info = snd_rme9652_info_spdif_in, \
  .get = snd_rme9652_get_spdif_in, .put = snd_rme9652_put_spdif_in }

942
static unsigned int rme9652_spdif_in(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
943 944 945 946 947
{
	return rme9652_decode_spdif_in(rme9652->control_register &
				       RME9652_inp);
}

948
static int rme9652_set_spdif_input(struct snd_rme9652 *rme9652, int in)
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
{
	int restart = 0;

	rme9652->control_register &= ~RME9652_inp;
	rme9652->control_register |= rme9652_encode_spdif_in(in);

	if ((restart = rme9652->running)) {
		rme9652_stop(rme9652);
	}

	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	if (restart) {
		rme9652_start(rme9652);
	}

	return 0;
}

968
static int snd_rme9652_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
969
{
970
	static const char * const texts[3] = {"ADAT1", "Coaxial", "Internal"};
Linus Torvalds's avatar
Linus Torvalds committed
971

972
	return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds's avatar
Linus Torvalds committed
973 974
}

975
static int snd_rme9652_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
976
{
977
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
978 979 980 981 982 983 984
	
	spin_lock_irq(&rme9652->lock);
	ucontrol->value.enumerated.item[0] = rme9652_spdif_in(rme9652);
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

985
static int snd_rme9652_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
986
{
987
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	int change;
	unsigned int val;
	
	if (!snd_rme9652_use_is_exclusive(rme9652))
		return -EBUSY;
	val = ucontrol->value.enumerated.item[0] % 3;
	spin_lock_irq(&rme9652->lock);
	change = val != rme9652_spdif_in(rme9652);
	if (change)
		rme9652_set_spdif_input(rme9652, val);
	spin_unlock_irq(&rme9652->lock);
	return change;
}

#define RME9652_SPDIF_OUT(xname, xindex) \
1003
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1004 1005 1006
  .info = snd_rme9652_info_spdif_out, \
  .get = snd_rme9652_get_spdif_out, .put = snd_rme9652_put_spdif_out }

1007
static int rme9652_spdif_out(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1008 1009 1010 1011
{
	return (rme9652->control_register & RME9652_opt_out) ? 1 : 0;
}

1012
static int rme9652_set_spdif_output(struct snd_rme9652 *rme9652, int out)
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
{
	int restart = 0;

	if (out) {
		rme9652->control_register |= RME9652_opt_out;
	} else {
		rme9652->control_register &= ~RME9652_opt_out;
	}

	if ((restart = rme9652->running)) {
		rme9652_stop(rme9652);
	}

	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	if (restart) {
		rme9652_start(rme9652);
	}

	return 0;
}

1035
#define snd_rme9652_info_spdif_out	snd_ctl_boolean_mono_info
Linus Torvalds's avatar
Linus Torvalds committed
1036

1037
static int snd_rme9652_get_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1038
{
1039
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1040 1041 1042 1043 1044 1045 1046
	
	spin_lock_irq(&rme9652->lock);
	ucontrol->value.integer.value[0] = rme9652_spdif_out(rme9652);
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

1047
static int snd_rme9652_put_spdif_out(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1048
{
1049
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	int change;
	unsigned int val;
	
	if (!snd_rme9652_use_is_exclusive(rme9652))
		return -EBUSY;
	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&rme9652->lock);
	change = (int)val != rme9652_spdif_out(rme9652);
	rme9652_set_spdif_output(rme9652, val);
	spin_unlock_irq(&rme9652->lock);
	return change;
}

#define RME9652_SYNC_MODE(xname, xindex) \
1064
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1065 1066 1067
  .info = snd_rme9652_info_sync_mode, \
  .get = snd_rme9652_get_sync_mode, .put = snd_rme9652_put_sync_mode }

1068
static int rme9652_sync_mode(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
{
	if (rme9652->control_register & RME9652_wsel) {
		return 2;
	} else if (rme9652->control_register & RME9652_Master) {
		return 1;
	} else {
		return 0;
	}
}

1079
static int rme9652_set_sync_mode(struct snd_rme9652 *rme9652, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
{
	int restart = 0;

	switch (mode) {
	case 0:
		rme9652->control_register &=
		    ~(RME9652_Master | RME9652_wsel);
		break;
	case 1:
		rme9652->control_register =
		    (rme9652->control_register & ~RME9652_wsel) | RME9652_Master;
		break;
	case 2:
		rme9652->control_register |=
		    (RME9652_Master | RME9652_wsel);
		break;
	}

	if ((restart = rme9652->running)) {
		rme9652_stop(rme9652);
	}

	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	if (restart) {
		rme9652_start(rme9652);
	}

	return 0;
}

1111
static int snd_rme9652_info_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1112
{
1113 1114 1115
	static const char * const texts[3] = {
		"AutoSync", "Master", "Word Clock"
	};
Linus Torvalds's avatar
Linus Torvalds committed
1116

1117
	return snd_ctl_enum_info(uinfo, 1, 3, texts);
Linus Torvalds's avatar
Linus Torvalds committed
1118 1119
}

1120
static int snd_rme9652_get_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1121
{
1122
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124 1125 1126 1127 1128 1129
	
	spin_lock_irq(&rme9652->lock);
	ucontrol->value.enumerated.item[0] = rme9652_sync_mode(rme9652);
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

1130
static int snd_rme9652_put_sync_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1131
{
1132
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	int change;
	unsigned int val;
	
	val = ucontrol->value.enumerated.item[0] % 3;
	spin_lock_irq(&rme9652->lock);
	change = (int)val != rme9652_sync_mode(rme9652);
	rme9652_set_sync_mode(rme9652, val);
	spin_unlock_irq(&rme9652->lock);
	return change;
}

#define RME9652_SYNC_PREF(xname, xindex) \
1145
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1146 1147 1148
  .info = snd_rme9652_info_sync_pref, \
  .get = snd_rme9652_get_sync_pref, .put = snd_rme9652_put_sync_pref }

1149
static int rme9652_sync_pref(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
{
	switch (rme9652->control_register & RME9652_SyncPref_Mask) {
	case RME9652_SyncPref_ADAT1:
		return RME9652_SYNC_FROM_ADAT1;
	case RME9652_SyncPref_ADAT2:
		return RME9652_SYNC_FROM_ADAT2;
	case RME9652_SyncPref_ADAT3:
		return RME9652_SYNC_FROM_ADAT3;
	case RME9652_SyncPref_SPDIF:
		return RME9652_SYNC_FROM_SPDIF;
	}
	/* Not reachable */
	return 0;
}

1165
static int rme9652_set_sync_pref(struct snd_rme9652 *rme9652, int pref)
Linus Torvalds's avatar
Linus Torvalds committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
{
	int restart;

	rme9652->control_register &= ~RME9652_SyncPref_Mask;
	switch (pref) {
	case RME9652_SYNC_FROM_ADAT1:
		rme9652->control_register |= RME9652_SyncPref_ADAT1;
		break;
	case RME9652_SYNC_FROM_ADAT2:
		rme9652->control_register |= RME9652_SyncPref_ADAT2;
		break;
	case RME9652_SYNC_FROM_ADAT3:
		rme9652->control_register |= RME9652_SyncPref_ADAT3;
		break;
	case RME9652_SYNC_FROM_SPDIF:
		rme9652->control_register |= RME9652_SyncPref_SPDIF;
		break;
	}

	if ((restart = rme9652->running)) {
		rme9652_stop(rme9652);
	}

	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	if (restart) {
		rme9652_start(rme9652);
	}

	return 0;
}

1198
static int snd_rme9652_info_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1199
{
1200 1201 1202
	static const char * const texts[4] = {
		"IEC958 In", "ADAT1 In", "ADAT2 In", "ADAT3 In"
	};
1203
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1204

1205 1206 1207
	return snd_ctl_enum_info(uinfo, 1,
				 rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3,
				 texts);
Linus Torvalds's avatar
Linus Torvalds committed
1208 1209
}

1210
static int snd_rme9652_get_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1211
{
1212
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1213 1214 1215 1216 1217 1218 1219
	
	spin_lock_irq(&rme9652->lock);
	ucontrol->value.enumerated.item[0] = rme9652_sync_pref(rme9652);
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

1220
static int snd_rme9652_put_sync_pref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1221
{
1222
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	int change, max;
	unsigned int val;
	
	if (!snd_rme9652_use_is_exclusive(rme9652))
		return -EBUSY;
	max = rme9652->ss_channels == RME9652_NCHANNELS ? 4 : 3;
	val = ucontrol->value.enumerated.item[0] % max;
	spin_lock_irq(&rme9652->lock);
	change = (int)val != rme9652_sync_pref(rme9652);
	rme9652_set_sync_pref(rme9652, val);
	spin_unlock_irq(&rme9652->lock);
	return change;
}

1237
static int snd_rme9652_info_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1238
{
1239
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1240 1241 1242 1243 1244 1245 1246
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = rme9652->ss_channels;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}

1247
static int snd_rme9652_get_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1248
{
1249
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1250 1251 1252 1253 1254 1255 1256 1257 1258
	unsigned int k;
	u32 thru_bits = rme9652->thru_bits;

	for (k = 0; k < rme9652->ss_channels; ++k) {
		ucontrol->value.integer.value[k] = !!(thru_bits & (1 << k));
	}
	return 0;
}

1259
static int snd_rme9652_put_thru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1260
{
1261
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	int change;
	unsigned int chn;
	u32 thru_bits = 0;

	if (!snd_rme9652_use_is_exclusive(rme9652))
		return -EBUSY;

	for (chn = 0; chn < rme9652->ss_channels; ++chn) {
		if (ucontrol->value.integer.value[chn])
			thru_bits |= 1 << chn;
	}
	
	spin_lock_irq(&rme9652->lock);
	change = thru_bits ^ rme9652->thru_bits;
	if (change) {
		for (chn = 0; chn < rme9652->ss_channels; ++chn) {
			if (!(change & (1 << chn)))
				continue;
			rme9652_set_thru(rme9652,chn,thru_bits&(1<<chn));
		}
	}
	spin_unlock_irq(&rme9652->lock);
	return !!change;
}

#define RME9652_PASSTHRU(xname, xindex) \
1288
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1289 1290 1291 1292
  .info = snd_rme9652_info_passthru, \
  .put = snd_rme9652_put_passthru, \
  .get = snd_rme9652_get_passthru }

1293
#define snd_rme9652_info_passthru	snd_ctl_boolean_mono_info
Linus Torvalds's avatar
Linus Torvalds committed
1294

1295
static int snd_rme9652_get_passthru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1296
{
1297
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1298 1299 1300 1301 1302 1303 1304

	spin_lock_irq(&rme9652->lock);
	ucontrol->value.integer.value[0] = rme9652->passthru;
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

1305
static int snd_rme9652_put_passthru(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1306
{
1307
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
	int change;
	unsigned int val;
	int err = 0;

	if (!snd_rme9652_use_is_exclusive(rme9652))
		return -EBUSY;

	val = ucontrol->value.integer.value[0] & 1;
	spin_lock_irq(&rme9652->lock);
	change = (ucontrol->value.integer.value[0] != rme9652->passthru);
	if (change)
		err = rme9652_set_passthru(rme9652, val);
	spin_unlock_irq(&rme9652->lock);
	return err ? err : change;
}

/* Read-only switches */

#define RME9652_SPDIF_RATE(xname, xindex) \
1327
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1328 1329 1330 1331
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_rme9652_info_spdif_rate, \
  .get = snd_rme9652_get_spdif_rate }

1332
static int snd_rme9652_info_spdif_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1333 1334 1335 1336 1337 1338 1339 1340
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 96000;
	return 0;
}

1341
static int snd_rme9652_get_spdif_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1342
{
1343
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1344 1345 1346 1347 1348 1349 1350 1351
	
	spin_lock_irq(&rme9652->lock);
	ucontrol->value.integer.value[0] = rme9652_spdif_sample_rate(rme9652);
	spin_unlock_irq(&rme9652->lock);
	return 0;
}

#define RME9652_ADAT_SYNC(xname, xindex, xidx) \
1352
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1353 1354 1355 1356
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_rme9652_info_adat_sync, \
  .get = snd_rme9652_get_adat_sync, .private_value = xidx }

1357
static int snd_rme9652_info_adat_sync(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1358
{
1359 1360 1361
	static const char * const texts[4] = {
		"No Lock", "Lock", "No Lock Sync", "Lock Sync"
	};
Linus Torvalds's avatar
Linus Torvalds committed
1362

1363
	return snd_ctl_enum_info(uinfo, 1, 4, texts);
Linus Torvalds's avatar
Linus Torvalds committed
1364 1365
}

1366
static int snd_rme9652_get_adat_sync(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1367
{
1368
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
	unsigned int mask1, mask2, val;
	
	switch (kcontrol->private_value) {
	case 0: mask1 = RME9652_lock_0; mask2 = RME9652_sync_0; break;	
	case 1: mask1 = RME9652_lock_1; mask2 = RME9652_sync_1; break;	
	case 2: mask1 = RME9652_lock_2; mask2 = RME9652_sync_2; break;	
	default: return -EINVAL;
	}
	val = rme9652_read(rme9652, RME9652_status_register);
	ucontrol->value.enumerated.item[0] = (val & mask1) ? 1 : 0;
	ucontrol->value.enumerated.item[0] |= (val & mask2) ? 2 : 0;
	return 0;
}

#define RME9652_TC_VALID(xname, xindex) \
1384
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
Linus Torvalds's avatar
Linus Torvalds committed
1385 1386 1387 1388
  .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  .info = snd_rme9652_info_tc_valid, \
  .get = snd_rme9652_get_tc_valid }

1389
#define snd_rme9652_info_tc_valid	snd_ctl_boolean_mono_info
Linus Torvalds's avatar
Linus Torvalds committed
1390

1391
static int snd_rme9652_get_tc_valid(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1392
{
1393
	struct snd_rme9652 *rme9652 = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1394 1395 1396 1397 1398 1399
	
	ucontrol->value.integer.value[0] = 
		(rme9652_read(rme9652, RME9652_status_register) & RME9652_tc_valid) ? 1 : 0;
	return 0;
}

1400
#ifdef ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE
Linus Torvalds's avatar
Linus Torvalds committed
1401 1402 1403 1404 1405 1406 1407

/* FIXME: this routine needs a port to the new control API --jk */

static int snd_rme9652_get_tc_value(void *private_data,
				    snd_kswitch_t *kswitch,
				    snd_switch_t *uswitch)
{
1408
	struct snd_rme9652 *s = (struct snd_rme9652 *) private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
	u32 value;
	int i;

	uswitch->type = SNDRV_SW_TYPE_DWORD;

	if ((rme9652_read(s, RME9652_status_register) &
	     RME9652_tc_valid) == 0) {
		uswitch->value.data32[0] = 0;
		return 0;
	}

	/* timecode request */

	rme9652_write(s, RME9652_time_code, 0);

	/* XXX bug alert: loop-based timing !!!! */

	for (i = 0; i < 50; i++) {
		if (!(rme9652_read(s, i * 4) & RME9652_tc_busy))
			break;
	}

	if (!(rme9652_read(s, i * 4) & RME9652_tc_busy)) {
		return -EIO;
	}

	value = 0;

	for (i = 0; i < 32; i++) {
		value >>= 1;

		if (rme9652_read(s, i * 4) & RME9652_tc_out)
			value |= 0x80000000;
	}

	if (value > 2 * 60 * 48000) {
		value -= 2 * 60 * 48000;
	} else {
		value = 0;
	}

	uswitch->value.data32[0] = value;

	return 0;
}

#endif				/* ALSA_HAS_STANDARD_WAY_OF_RETURNING_TIMECODE */

1457
static const struct snd_kcontrol_new snd_rme9652_controls[] = {
Linus Torvalds's avatar
Linus Torvalds committed
1458
{
1459
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
1460 1461 1462 1463 1464 1465 1466
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
	.info =		snd_rme9652_control_spdif_info,
	.get =		snd_rme9652_control_spdif_get,
	.put =		snd_rme9652_control_spdif_put,
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1467
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
1468 1469 1470 1471 1472 1473 1474
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
	.info =		snd_rme9652_control_spdif_stream_info,
	.get =		snd_rme9652_control_spdif_stream_get,
	.put =		snd_rme9652_control_spdif_stream_put,
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1475
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
1476 1477 1478 1479 1480 1481 1482 1483 1484
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
	.info =		snd_rme9652_control_spdif_mask_info,
	.get =		snd_rme9652_control_spdif_mask_get,
	.private_value = IEC958_AES0_NONAUDIO |
			IEC958_AES0_PROFESSIONAL |
			IEC958_AES0_CON_EMPHASIS,	                                                                                      
},
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1485
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
	.info =		snd_rme9652_control_spdif_mask_info,
	.get =		snd_rme9652_control_spdif_mask_get,
	.private_value = IEC958_AES0_NONAUDIO |
			IEC958_AES0_PROFESSIONAL |
			IEC958_AES0_PRO_EMPHASIS,
},
RME9652_SPDIF_IN("IEC958 Input Connector", 0),
RME9652_SPDIF_OUT("IEC958 Output also on ADAT1", 0),
RME9652_SYNC_MODE("Sync Mode", 0),
RME9652_SYNC_PREF("Preferred Sync Source", 0),
{
1498
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds's avatar
Linus Torvalds committed
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	.name = "Channels Thru",
	.index = 0,
	.info = snd_rme9652_info_thru,
	.get = snd_rme9652_get_thru,
	.put = snd_rme9652_put_thru,
},
RME9652_SPDIF_RATE("IEC958 Sample Rate", 0),
RME9652_ADAT_SYNC("ADAT1 Sync Check", 0, 0),
RME9652_ADAT_SYNC("ADAT2 Sync Check", 0, 1),
RME9652_TC_VALID("Timecode Valid", 0),
RME9652_PASSTHRU("Passthru", 0)
};

1512
static const struct snd_kcontrol_new snd_rme9652_adat3_check =
Linus Torvalds's avatar
Linus Torvalds committed
1513 1514
RME9652_ADAT_SYNC("ADAT3 Sync Check", 0, 2);

1515
static const struct snd_kcontrol_new snd_rme9652_adat1_input =
Linus Torvalds's avatar
Linus Torvalds committed
1516 1517
RME9652_ADAT1_IN("ADAT1 Input Source", 0);

1518
static int snd_rme9652_create_controls(struct snd_card *card, struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1519 1520 1521
{
	unsigned int idx;
	int err;
1522
	struct snd_kcontrol *kctl;
Linus Torvalds's avatar
Linus Torvalds committed
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

	for (idx = 0; idx < ARRAY_SIZE(snd_rme9652_controls); idx++) {
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_controls[idx], rme9652))) < 0)
			return err;
		if (idx == 1)	/* IEC958 (S/PDIF) Stream */
			rme9652->spdif_ctl = kctl;
	}

	if (rme9652->ss_channels == RME9652_NCHANNELS)
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_adat3_check, rme9652))) < 0)
			return err;

	if (rme9652->hw_rev >= 15)
		if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_rme9652_adat1_input, rme9652))) < 0)
			return err;

	return 0;
}

/*------------------------------------------------------------
   /proc interface 
 ------------------------------------------------------------*/

static void
1547
snd_rme9652_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds's avatar
Linus Torvalds committed
1548
{
1549
	struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) entry->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
	u32 thru_bits = rme9652->thru_bits;
	int show_auto_sync_source = 0;
	int i;
	unsigned int status;
	int x;

	status = rme9652_read(rme9652, RME9652_status_register);

	snd_iprintf(buffer, "%s (Card #%d)\n", rme9652->card_name, rme9652->card->number + 1);
	snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
		    rme9652->capture_buffer, rme9652->playback_buffer);
	snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
		    rme9652->irq, rme9652->port, (unsigned long)rme9652->iobase);
	snd_iprintf(buffer, "Control register: %x\n", rme9652->control_register);

	snd_iprintf(buffer, "\n");

	x = 1 << (6 + rme9652_decode_latency(rme9652->control_register & 
					     RME9652_latency));

	snd_iprintf(buffer, "Latency: %d samples (2 periods of %lu bytes)\n", 
		    x, (unsigned long) rme9652->period_bytes);
	snd_iprintf(buffer, "Hardware pointer (frames): %ld\n",
		    rme9652_hw_pointer(rme9652));
	snd_iprintf(buffer, "Passthru: %s\n",
		    rme9652->passthru ? "yes" : "no");

	if ((rme9652->control_register & (RME9652_Master | RME9652_wsel)) == 0) {
		snd_iprintf(buffer, "Clock mode: autosync\n");
		show_auto_sync_source = 1;
	} else if (rme9652->control_register & RME9652_wsel) {
		if (status & RME9652_wsel_rd) {
			snd_iprintf(buffer, "Clock mode: word clock\n");
		} else {
			snd_iprintf(buffer, "Clock mode: word clock (no signal)\n");
		}
	} else {
		snd_iprintf(buffer, "Clock mode: master\n");
	}

	if (show_auto_sync_source) {
		switch (rme9652->control_register & RME9652_SyncPref_Mask) {
		case RME9652_SyncPref_ADAT1:
			snd_iprintf(buffer, "Pref. sync source: ADAT1\n");
			break;
		case RME9652_SyncPref_ADAT2:
			snd_iprintf(buffer, "Pref. sync source: ADAT2\n");
			break;
		case RME9652_SyncPref_ADAT3:
			snd_iprintf(buffer, "Pref. sync source: ADAT3\n");
			break;
		case RME9652_SyncPref_SPDIF:
			snd_iprintf(buffer, "Pref. sync source: IEC958\n");
			break;
		default:
			snd_iprintf(buffer, "Pref. sync source: ???\n");
		}
	}

	if (rme9652->hw_rev >= 15)
		snd_iprintf(buffer, "\nADAT1 Input source: %s\n",
			    (rme9652->control_register & RME9652_ADAT1_INTERNAL) ?
			    "Internal" : "ADAT1 optical");

	snd_iprintf(buffer, "\n");

	switch (rme9652_decode_spdif_in(rme9652->control_register & 
					RME9652_inp)) {
	case RME9652_SPDIFIN_OPTICAL:
		snd_iprintf(buffer, "IEC958 input: ADAT1\n");
		break;
	case RME9652_SPDIFIN_COAXIAL:
		snd_iprintf(buffer, "IEC958 input: Coaxial\n");
		break;
	case RME9652_SPDIFIN_INTERN:
		snd_iprintf(buffer, "IEC958 input: Internal\n");
		break;
	default:
		snd_iprintf(buffer, "IEC958 input: ???\n");
		break;
	}

	if (rme9652->control_register & RME9652_opt_out) {
		snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
	} else {
		snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
	}

	if (rme9652->control_register & RME9652_PRO) {
		snd_iprintf(buffer, "IEC958 quality: Professional\n");
	} else {
		snd_iprintf(buffer, "IEC958 quality: Consumer\n");
	}

	if (rme9652->control_register & RME9652_EMP) {
		snd_iprintf(buffer, "IEC958 emphasis: on\n");
	} else {
		snd_iprintf(buffer, "IEC958 emphasis: off\n");
	}

	if (rme9652->control_register & RME9652_Dolby) {
		snd_iprintf(buffer, "IEC958 Dolby: on\n");
	} else {
		snd_iprintf(buffer, "IEC958 Dolby: off\n");
	}

	i = rme9652_spdif_sample_rate(rme9652);

	if (i < 0) {
		snd_iprintf(buffer,
			    "IEC958 sample rate: error flag set\n");
	} else if (i == 0) {
		snd_iprintf(buffer, "IEC958 sample rate: undetermined\n");
	} else {
		snd_iprintf(buffer, "IEC958 sample rate: %d\n", i);
	}

	snd_iprintf(buffer, "\n");

	snd_iprintf(buffer, "ADAT Sample rate: %dHz\n",
		    rme9652_adat_sample_rate(rme9652));

	/* Sync Check */

	x = status & RME9652_sync_0;
	if (status & RME9652_lock_0) {
		snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
	} else {
		snd_iprintf(buffer, "ADAT1: No Lock\n");
	}

	x = status & RME9652_sync_1;
	if (status & RME9652_lock_1) {
		snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
	} else {
		snd_iprintf(buffer, "ADAT2: No Lock\n");
	}

	x = status & RME9652_sync_2;
	if (status & RME9652_lock_2) {
		snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
	} else {
		snd_iprintf(buffer, "ADAT3: No Lock\n");
	}

	snd_iprintf(buffer, "\n");

	snd_iprintf(buffer, "Timecode signal: %s\n",
		    (status & RME9652_tc_valid) ? "yes" : "no");

	/* thru modes */

	snd_iprintf(buffer, "Punch Status:\n\n");

	for (i = 0; i < rme9652->ss_channels; i++) {
		if (thru_bits & (1 << i)) {
			snd_iprintf(buffer, "%2d:  on ", i + 1);
		} else {
			snd_iprintf(buffer, "%2d: off ", i + 1);
		}

		if (((i + 1) % 8) == 0) {
			snd_iprintf(buffer, "\n");
		}
	}

	snd_iprintf(buffer, "\n");
}

1719
static void snd_rme9652_proc_init(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1720
{
1721 1722
	snd_card_ro_proc_new(rme9652->card, "rme9652", rme9652,
			     snd_rme9652_proc_read);
Linus Torvalds's avatar
Linus Torvalds committed
1723 1724
}

1725
static void snd_rme9652_free_buffers(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1726 1727 1728 1729 1730
{
	snd_hammerfall_free_buffer(&rme9652->capture_dma_buf, rme9652->pci);
	snd_hammerfall_free_buffer(&rme9652->playback_dma_buf, rme9652->pci);
}

1731
static int snd_rme9652_free(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1732 1733 1734 1735 1736 1737 1738
{
	if (rme9652->irq >= 0)
		rme9652_stop(rme9652);
	snd_rme9652_free_buffers(rme9652);

	if (rme9652->irq >= 0)
		free_irq(rme9652->irq, (void *)rme9652);
1739
	iounmap(rme9652->iobase);
Linus Torvalds's avatar
Linus Torvalds committed
1740 1741 1742 1743 1744 1745 1746
	if (rme9652->port)
		pci_release_regions(rme9652->pci);

	pci_disable_device(rme9652->pci);
	return 0;
}

1747
static int snd_rme9652_initialize_memory(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1748 1749 1750 1751 1752 1753 1754
{
	unsigned long pb_bus, cb_bus;

	if (snd_hammerfall_get_buffer(rme9652->pci, &rme9652->capture_dma_buf, RME9652_DMA_AREA_BYTES) < 0 ||
	    snd_hammerfall_get_buffer(rme9652->pci, &rme9652->playback_dma_buf, RME9652_DMA_AREA_BYTES) < 0) {
		if (rme9652->capture_dma_buf.area)
			snd_dma_free_pages(&rme9652->capture_dma_buf);
1755 1756
		dev_err(rme9652->card->dev,
			"%s: no buffers available\n", rme9652->card_name);
Linus Torvalds's avatar
Linus Torvalds committed
1757 1758 1759 1760 1761
		return -ENOMEM;
	}

	/* Align to bus-space 64K boundary */

Clemens Ladisch's avatar
Clemens Ladisch committed
1762 1763
	cb_bus = ALIGN(rme9652->capture_dma_buf.addr, 0x10000ul);
	pb_bus = ALIGN(rme9652->playback_dma_buf.addr, 0x10000ul);
Linus Torvalds's avatar
Linus Torvalds committed
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775

	/* Tell the card where it is */

	rme9652_write(rme9652, RME9652_rec_buffer, cb_bus);
	rme9652_write(rme9652, RME9652_play_buffer, pb_bus);

	rme9652->capture_buffer = rme9652->capture_dma_buf.area + (cb_bus - rme9652->capture_dma_buf.addr);
	rme9652->playback_buffer = rme9652->playback_dma_buf.area + (pb_bus - rme9652->playback_dma_buf.addr);

	return 0;
}

1776
static void snd_rme9652_set_defaults(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
1777 1778 1779 1780 1781
{
	unsigned int k;

	/* ASSUMPTION: rme9652->lock is either held, or
	   there is no need to hold it (e.g. during module
Joe Perches's avatar
Joe Perches committed
1782
	   initialization).
Linus Torvalds's avatar
Linus Torvalds committed
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	 */

	/* set defaults:

	   SPDIF Input via Coax 
	   autosync clock mode
	   maximum latency (7 = 8192 samples, 64Kbyte buffer,
	   which implies 2 4096 sample, 32Kbyte periods).
	   
	   if rev 1.5, initialize the S/PDIF receiver.

	 */

	rme9652->control_register =
	    RME9652_inp_0 | rme9652_encode_latency(7);

	rme9652_write(rme9652, RME9652_control_register, rme9652->control_register);

	rme9652_reset_hw_pointer(rme9652);
	rme9652_compute_period_size(rme9652);

	/* default: thru off for all channels */

	for (k = 0; k < RME9652_NCHANNELS; ++k)
		rme9652_write(rme9652, RME9652_thru_base + k * 4, 0);

	rme9652->thru_bits = 0;
	rme9652->passthru = 0;

	/* set a default rate so that the channel map is set up */

	rme9652_set_rate(rme9652, 48000);
}

1817
static irqreturn_t snd_rme9652_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
1818
{
1819
	struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836

	if (!(rme9652_read(rme9652, RME9652_status_register) & RME9652_IRQ)) {
		return IRQ_NONE;
	}

	rme9652_write(rme9652, RME9652_irq_clear, 0);

	if (rme9652->capture_substream) {
		snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
	}

	if (rme9652->playback_substream) {
		snd_pcm_period_elapsed(rme9652->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
	}
	return IRQ_HANDLED;
}

1837
static snd_pcm_uframes_t snd_rme9652_hw_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1838
{
1839
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1840 1841 1842
	return rme9652_hw_pointer(rme9652);
}

1843
static char *rme9652_channel_buffer_location(struct snd_rme9652 *rme9652,
Linus Torvalds's avatar
Linus Torvalds committed
1844 1845 1846 1847 1848 1849
					     int stream,
					     int channel)

{
	int mapped_channel;

1850 1851
	if (snd_BUG_ON(channel < 0 || channel >= RME9652_NCHANNELS))
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
        
	if ((mapped_channel = rme9652->channel_map[channel]) < 0) {
		return NULL;
	}
	
	if (stream == SNDRV_PCM_STREAM_CAPTURE) {
		return rme9652->capture_buffer +
			(mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
	} else {
		return rme9652->playback_buffer +
			(mapped_channel * RME9652_CHANNEL_BUFFER_BYTES);
	}
}

1866 1867 1868
static int snd_rme9652_playback_copy(struct snd_pcm_substream *substream,
				     int channel, unsigned long pos,
				     void __user *src, unsigned long count)
Linus Torvalds's avatar
Linus Torvalds committed
1869
{
1870
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1871 1872
	char *channel_buf;

1873
	if (snd_BUG_ON(pos + count > RME9652_CHANNEL_BUFFER_BYTES))
1874
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1875 1876 1877 1878

	channel_buf = rme9652_channel_buffer_location (rme9652,
						       substream->pstr->stream,
						       channel);
1879 1880
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
1881
	if (copy_from_user(channel_buf + pos, src, count))
Linus Torvalds's avatar
Linus Torvalds committed
1882
		return -EFAULT;
1883
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1884 1885
}

1886 1887 1888
static int snd_rme9652_playback_copy_kernel(struct snd_pcm_substream *substream,
					    int channel, unsigned long pos,
					    void *src, unsigned long count)
Linus Torvalds's avatar
Linus Torvalds committed
1889
{
1890
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1891 1892
	char *channel_buf;

1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
	channel_buf = rme9652_channel_buffer_location(rme9652,
						      substream->pstr->stream,
						      channel);
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
	memcpy(channel_buf + pos, src, count);
	return 0;
}

static int snd_rme9652_capture_copy(struct snd_pcm_substream *substream,
				    int channel, unsigned long pos,
				    void __user *dst, unsigned long count)
{
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
	char *channel_buf;

	if (snd_BUG_ON(pos + count > RME9652_CHANNEL_BUFFER_BYTES))
1910
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1911 1912 1913 1914

	channel_buf = rme9652_channel_buffer_location (rme9652,
						       substream->pstr->stream,
						       channel);
1915 1916
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
1917
	if (copy_to_user(dst, channel_buf + pos, count))
Linus Torvalds's avatar
Linus Torvalds committed
1918
		return -EFAULT;
1919
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1920 1921
}

1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
static int snd_rme9652_capture_copy_kernel(struct snd_pcm_substream *substream,
					   int channel, unsigned long pos,
					   void *dst, unsigned long count)
{
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
	char *channel_buf;

	channel_buf = rme9652_channel_buffer_location(rme9652,
						      substream->pstr->stream,
						      channel);
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
	memcpy(dst, channel_buf + pos, count);
	return 0;
}

static int snd_rme9652_hw_silence(struct snd_pcm_substream *substream,
				  int channel, unsigned long pos,
				  unsigned long count)
Linus Torvalds's avatar
Linus Torvalds committed
1941
{
1942
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1943 1944 1945 1946 1947
	char *channel_buf;

	channel_buf = rme9652_channel_buffer_location (rme9652,
						       substream->pstr->stream,
						       channel);
1948 1949
	if (snd_BUG_ON(!channel_buf))
		return -EIO;
1950 1951
	memset(channel_buf + pos, 0, count);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1952 1953
}

1954
static int snd_rme9652_reset(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1955
{
1956 1957 1958
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
Linus Torvalds's avatar
Linus Torvalds committed
1959 1960 1961 1962 1963 1964 1965 1966 1967
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		other = rme9652->capture_substream;
	else
		other = rme9652->playback_substream;
	if (rme9652->running)
		runtime->status->hw_ptr = rme9652_hw_pointer(rme9652);
	else
		runtime->status->hw_ptr = 0;
	if (other) {
1968 1969
		struct snd_pcm_substream *s;
		struct snd_pcm_runtime *oruntime = other->runtime;
1970
		snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds's avatar
Linus Torvalds committed
1971 1972 1973 1974 1975 1976 1977 1978 1979
			if (s == other) {
				oruntime->status->hw_ptr = runtime->status->hw_ptr;
				break;
			}
		}
	}
	return 0;
}

1980 1981
static int snd_rme9652_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params)
Linus Torvalds's avatar
Linus Torvalds committed
1982
{
1983
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
	int err;
	pid_t this_pid;
	pid_t other_pid;

	spin_lock_irq(&rme9652->lock);

	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		rme9652->control_register &= ~(RME9652_PRO | RME9652_Dolby | RME9652_EMP);
		rme9652_write(rme9652, RME9652_control_register, rme9652->control_register |= rme9652->creg_spdif_stream);
		this_pid = rme9652->playback_pid;
		other_pid = rme9652->capture_pid;
	} else {
		this_pid = rme9652->capture_pid;
		other_pid = rme9652->playback_pid;
	}

	if ((other_pid > 0) && (this_pid != other_pid)) {

		/* The other stream is open, and not by the same
		   task as this one. Make sure that the parameters
		   that matter are the same.
		 */

		if ((int)params_rate(params) !=
		    rme9652_adat_sample_rate(rme9652)) {
			spin_unlock_irq(&rme9652->lock);
			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
			return -EBUSY;
		}

		if (params_period_size(params) != rme9652->period_bytes / 4) {
			spin_unlock_irq(&rme9652->lock);
			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
			return -EBUSY;
		}

		/* We're fine. */

		spin_unlock_irq(&rme9652->lock);
 		return 0;

	} else {
		spin_unlock_irq(&rme9652->lock);
	}

	/* how to make sure that the rate matches an externally-set one ?
	 */

	if ((err = rme9652_set_rate(rme9652, params_rate(params))) < 0) {
		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
		return err;
	}

	if ((err = rme9652_set_interrupt_interval(rme9652, params_period_size(params))) < 0) {
		_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
		return err;
	}

	return 0;
}

2045 2046
static int snd_rme9652_channel_info(struct snd_pcm_substream *substream,
				    struct snd_pcm_channel_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
2047
{
2048
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
2049 2050
	int chn;

2051 2052
	if (snd_BUG_ON(info->channel >= RME9652_NCHANNELS))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2053

2054 2055 2056
	chn = rme9652->channel_map[array_index_nospec(info->channel,
						      RME9652_NCHANNELS)];
	if (chn < 0)
Linus Torvalds's avatar
Linus Torvalds committed
2057 2058 2059 2060 2061 2062 2063 2064
		return -EINVAL;

	info->offset = chn * RME9652_CHANNEL_BUFFER_BYTES;
	info->first = 0;
	info->step = 32;
	return 0;
}

2065
static int snd_rme9652_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
2066 2067 2068 2069 2070 2071 2072 2073 2074
			     unsigned int cmd, void *arg)
{
	switch (cmd) {
	case SNDRV_PCM_IOCTL1_RESET:
	{
		return snd_rme9652_reset(substream);
	}
	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
	{
2075
		struct snd_pcm_channel_info *info = arg;
Linus Torvalds's avatar
Linus Torvalds committed
2076 2077 2078 2079 2080 2081 2082 2083 2084
		return snd_rme9652_channel_info(substream, info);
	}
	default:
		break;
	}

	return snd_pcm_lib_ioctl(substream, cmd, arg);
}

2085
static void rme9652_silence_playback(struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
2086 2087 2088 2089
{
	memset(rme9652->playback_buffer, 0, RME9652_DMA_AREA_BYTES);
}

2090
static int snd_rme9652_trigger(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
2091 2092
			       int cmd)
{
2093 2094
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
	struct snd_pcm_substream *other;
Linus Torvalds's avatar
Linus Torvalds committed
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
	int running;
	spin_lock(&rme9652->lock);
	running = rme9652->running;
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		running |= 1 << substream->stream;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		running &= ~(1 << substream->stream);
		break;
	default:
		snd_BUG();
		spin_unlock(&rme9652->lock);
		return -EINVAL;
	}
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		other = rme9652->capture_substream;
	else
		other = rme9652->playback_substream;

	if (other) {
2116
		struct snd_pcm_substream *s;
2117
		snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds's avatar
Linus Torvalds committed
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
			if (s == other) {
				snd_pcm_trigger_done(s, substream);
				if (cmd == SNDRV_PCM_TRIGGER_START)
					running |= 1 << s->stream;
				else
					running &= ~(1 << s->stream);
				goto _ok;
			}
		}
		if (cmd == SNDRV_PCM_TRIGGER_START) {
			if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
			    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
				rme9652_silence_playback(rme9652);
		} else {
			if (running &&
			    substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
				rme9652_silence_playback(rme9652);
		}
	} else {
		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 
			rme9652_silence_playback(rme9652);
	}
 _ok:
	snd_pcm_trigger_done(substream, substream);
	if (!rme9652->running && running)
		rme9652_start(rme9652);
	else if (rme9652->running && !running)
		rme9652_stop(rme9652);
	rme9652->running = running;
	spin_unlock(&rme9652->lock);

	return 0;
}

2152
static int snd_rme9652_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
2153
{
2154
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
2155 2156 2157 2158 2159 2160
	unsigned long flags;

	spin_lock_irqsave(&rme9652->lock, flags);
	if (!rme9652->running)
		rme9652_reset_hw_pointer(rme9652);
	spin_unlock_irqrestore(&rme9652->lock, flags);
2161
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2162 2163
}

2164
static const struct snd_pcm_hardware snd_rme9652_playback_subinfo =
Linus Torvalds's avatar
Linus Torvalds committed
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
{
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_NONINTERLEAVED |
				 SNDRV_PCM_INFO_SYNC_START |
				 SNDRV_PCM_INFO_DOUBLE),
	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
	.rates =		(SNDRV_PCM_RATE_44100 | 
				 SNDRV_PCM_RATE_48000 | 
				 SNDRV_PCM_RATE_88200 | 
				 SNDRV_PCM_RATE_96000),
	.rate_min =		44100,
	.rate_max =		96000,
	.channels_min =		10,
	.channels_max =		26,
	.buffer_bytes_max =	RME9652_CHANNEL_BUFFER_BYTES * 26,
	.period_bytes_min =	(64 * 4) * 10,
	.period_bytes_max =	(8192 * 4) * 26,
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0,
};

2188
static const struct snd_pcm_hardware snd_rme9652_capture_subinfo =
Linus Torvalds's avatar
Linus Torvalds committed
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
{
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_NONINTERLEAVED |
				 SNDRV_PCM_INFO_SYNC_START),
	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
	.rates =		(SNDRV_PCM_RATE_44100 | 
				 SNDRV_PCM_RATE_48000 | 
				 SNDRV_PCM_RATE_88200 | 
				 SNDRV_PCM_RATE_96000),
	.rate_min =		44100,
	.rate_max =		96000,
	.channels_min =		10,
	.channels_max =		26,
	.buffer_bytes_max =	RME9652_CHANNEL_BUFFER_BYTES *26,
	.period_bytes_min =	(64 * 4) * 10,
	.period_bytes_max =	(8192 * 4) * 26,
	.periods_min =		2,
	.periods_max =		2,
	.fifo_size =		0,
};

2211
static const unsigned int period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
Linus Torvalds's avatar
Linus Torvalds committed
2212

2213
static const struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
Linus Torvalds's avatar
Linus Torvalds committed
2214 2215 2216 2217 2218
	.count = ARRAY_SIZE(period_sizes),
	.list = period_sizes,
	.mask = 0
};

2219 2220
static int snd_rme9652_hw_rule_channels(struct snd_pcm_hw_params *params,
					struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
2221
{
2222 2223
	struct snd_rme9652 *rme9652 = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
Linus Torvalds's avatar
Linus Torvalds committed
2224 2225 2226 2227
	unsigned int list[2] = { rme9652->ds_channels, rme9652->ss_channels };
	return snd_interval_list(c, 2, list, 0);
}

2228 2229
static int snd_rme9652_hw_rule_channels_rate(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
2230
{
2231 2232 2233
	struct snd_rme9652 *rme9652 = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds's avatar
Linus Torvalds committed
2234
	if (r->min > 48000) {
2235
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
2236 2237 2238 2239 2240 2241
			.min = rme9652->ds_channels,
			.max = rme9652->ds_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	} else if (r->max < 88200) {
2242
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
2243 2244 2245 2246 2247 2248 2249 2250 2251
			.min = rme9652->ss_channels,
			.max = rme9652->ss_channels,
			.integer = 1,
		};
		return snd_interval_refine(c, &t);
	}
	return 0;
}

2252 2253
static int snd_rme9652_hw_rule_rate_channels(struct snd_pcm_hw_params *params,
					     struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
2254
{
2255 2256 2257
	struct snd_rme9652 *rme9652 = rule->private;
	struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Linus Torvalds's avatar
Linus Torvalds committed
2258
	if (c->min >= rme9652->ss_channels) {
2259
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
2260 2261 2262 2263 2264 2265
			.min = 44100,
			.max = 48000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	} else if (c->max <= rme9652->ds_channels) {
2266
		struct snd_interval t = {
Linus Torvalds's avatar
Linus Torvalds committed
2267 2268 2269 2270 2271 2272 2273 2274 2275
			.min = 88200,
			.max = 96000,
			.integer = 1,
		};
		return snd_interval_refine(r, &t);
	}
	return 0;
}

2276
static int snd_rme9652_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
2277
{
2278 2279
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317

	spin_lock_irq(&rme9652->lock);

	snd_pcm_set_sync(substream);

        runtime->hw = snd_rme9652_playback_subinfo;
	runtime->dma_area = rme9652->playback_buffer;
	runtime->dma_bytes = RME9652_DMA_AREA_BYTES;

	if (rme9652->capture_substream == NULL) {
		rme9652_stop(rme9652);
		rme9652_set_thru(rme9652, -1, 0);
	}

	rme9652->playback_pid = current->pid;
	rme9652->playback_substream = substream;

	spin_unlock_irq(&rme9652->lock);

	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_rme9652_hw_rule_channels, rme9652,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_rme9652_hw_rule_channels_rate, rme9652,
			     SNDRV_PCM_HW_PARAM_RATE, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			     snd_rme9652_hw_rule_rate_channels, rme9652,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);

	rme9652->creg_spdif_stream = rme9652->creg_spdif;
	rme9652->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
		       SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
	return 0;
}

2318
static int snd_rme9652_playback_release(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
2319
{
2320
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

	spin_lock_irq(&rme9652->lock);

	rme9652->playback_pid = -1;
	rme9652->playback_substream = NULL;

	spin_unlock_irq(&rme9652->lock);

	rme9652->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	snd_ctl_notify(rme9652->card, SNDRV_CTL_EVENT_MASK_VALUE |
		       SNDRV_CTL_EVENT_MASK_INFO, &rme9652->spdif_ctl->id);
	return 0;
}


2336
static int snd_rme9652_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
2337
{
2338 2339
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

	spin_lock_irq(&rme9652->lock);

	snd_pcm_set_sync(substream);

	runtime->hw = snd_rme9652_capture_subinfo;
	runtime->dma_area = rme9652->capture_buffer;
	runtime->dma_bytes = RME9652_DMA_AREA_BYTES;

	if (rme9652->playback_substream == NULL) {
		rme9652_stop(rme9652);
		rme9652_set_thru(rme9652, -1, 0);
	}

	rme9652->capture_pid = current->pid;
	rme9652->capture_substream = substream;

	spin_unlock_irq(&rme9652->lock);

	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_period_sizes);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_rme9652_hw_rule_channels, rme9652,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
			     snd_rme9652_hw_rule_channels_rate, rme9652,
			     SNDRV_PCM_HW_PARAM_RATE, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			     snd_rme9652_hw_rule_rate_channels, rme9652,
			     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
	return 0;
}

2373
static int snd_rme9652_capture_release(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
2374
{
2375
	struct snd_rme9652 *rme9652 = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385

	spin_lock_irq(&rme9652->lock);

	rme9652->capture_pid = -1;
	rme9652->capture_substream = NULL;

	spin_unlock_irq(&rme9652->lock);
	return 0;
}

2386
static const struct snd_pcm_ops snd_rme9652_playback_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
2387 2388 2389 2390 2391 2392 2393
	.open =		snd_rme9652_playback_open,
	.close =	snd_rme9652_playback_release,
	.ioctl =	snd_rme9652_ioctl,
	.hw_params =	snd_rme9652_hw_params,
	.prepare =	snd_rme9652_prepare,
	.trigger =	snd_rme9652_trigger,
	.pointer =	snd_rme9652_hw_pointer,
2394 2395 2396
	.copy_user =	snd_rme9652_playback_copy,
	.copy_kernel =	snd_rme9652_playback_copy_kernel,
	.fill_silence =	snd_rme9652_hw_silence,
Linus Torvalds's avatar
Linus Torvalds committed
2397 2398
};

2399
static const struct snd_pcm_ops snd_rme9652_capture_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
2400 2401 2402 2403 2404 2405 2406
	.open =		snd_rme9652_capture_open,
	.close =	snd_rme9652_capture_release,
	.ioctl =	snd_rme9652_ioctl,
	.hw_params =	snd_rme9652_hw_params,
	.prepare =	snd_rme9652_prepare,
	.trigger =	snd_rme9652_trigger,
	.pointer =	snd_rme9652_hw_pointer,
2407 2408
	.copy_user =	snd_rme9652_capture_copy,
	.copy_kernel =	snd_rme9652_capture_copy_kernel,
Linus Torvalds's avatar
Linus Torvalds committed
2409 2410
};

2411 2412
static int snd_rme9652_create_pcm(struct snd_card *card,
				  struct snd_rme9652 *rme9652)
Linus Torvalds's avatar
Linus Torvalds committed
2413
{
2414
	struct snd_pcm *pcm;
Linus Torvalds's avatar
Linus Torvalds committed
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
	int err;

	if ((err = snd_pcm_new(card,
			       rme9652->card_name,
			       0, 1, 1, &pcm)) < 0) {
		return err;
	}

	rme9652->pcm = pcm;
	pcm->private_data = rme9652;
	strcpy(pcm->name, rme9652->card_name);

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_rme9652_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_rme9652_capture_ops);

	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;

	return 0;
}

2435 2436 2437
static int snd_rme9652_create(struct snd_card *card,
			      struct snd_rme9652 *rme9652,
			      int precise_ptr)
Linus Torvalds's avatar
Linus Torvalds committed
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
{
	struct pci_dev *pci = rme9652->pci;
	int err;
	int status;
	unsigned short rev;

	rme9652->irq = -1;
	rme9652->card = card;

	pci_read_config_word(rme9652->pci, PCI_CLASS_REVISION, &rev);

	switch (rev & 0xff) {
	case 3:
	case 4:
	case 8:
	case 9:
		break;

	default:
		/* who knows? */
		return -ENODEV;
	}

	if ((err = pci_enable_device(pci)) < 0)
		return err;

	spin_lock_init(&rme9652->lock);

	if ((err = pci_request_regions(pci, "rme9652")) < 0)
		return err;
	rme9652->port = pci_resource_start(pci, 0);
	rme9652->iobase = ioremap_nocache(rme9652->port, RME9652_IO_EXTENT);
	if (rme9652->iobase == NULL) {
2471 2472
		dev_err(card->dev, "unable to remap region 0x%lx-0x%lx\n",
			rme9652->port, rme9652->port + RME9652_IO_EXTENT - 1);
Linus Torvalds's avatar
Linus Torvalds committed
2473 2474 2475
		return -EBUSY;
	}
	
2476
	if (request_irq(pci->irq, snd_rme9652_interrupt, IRQF_SHARED,
2477
			KBUILD_MODNAME, rme9652)) {
2478
		dev_err(card->dev, "unable to request IRQ %d\n", pci->irq);
Linus Torvalds's avatar
Linus Torvalds committed
2479 2480 2481
		return -EBUSY;
	}
	rme9652->irq = pci->irq;
2482
	card->sync_irq = rme9652->irq;
Linus Torvalds's avatar
Linus Torvalds committed
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
	rme9652->precise_ptr = precise_ptr;

	/* Determine the h/w rev level of the card. This seems like
	   a particularly kludgy way to encode it, but its what RME
	   chose to do, so we follow them ...
	*/

	status = rme9652_read(rme9652, RME9652_status_register);
	if (rme9652_decode_spdif_rate(status&RME9652_F) == 1) {
		rme9652->hw_rev = 15;
	} else {
		rme9652->hw_rev = 11;
	}

	/* Differentiate between the standard Hammerfall, and the
	   "Light", which does not have the expansion board. This
	   method comes from information received from Mathhias
	   Clausen at RME. Display the EEPROM and h/w revID where
	   relevant.  
	*/

	switch (rev) {
	case 8: /* original eprom */
		strcpy(card->driver, "RME9636");
		if (rme9652->hw_rev == 15) {
			rme9652->card_name = "RME Digi9636 (Rev 1.5)";
		} else {
			rme9652->card_name = "RME Digi9636";
		}
		rme9652->ss_channels = RME9636_NCHANNELS;
		break;
	case 9: /* W36_G EPROM */
		strcpy(card->driver, "RME9636");
		rme9652->card_name = "RME Digi9636 (Rev G)";
		rme9652->ss_channels = RME9636_NCHANNELS;
		break;
	case 4: /* W52_G EPROM */
		strcpy(card->driver, "RME9652");
		rme9652->card_name = "RME Digi9652 (Rev G)";
		rme9652->ss_channels = RME9652_NCHANNELS;
		break;
	case 3: /* original eprom */
		strcpy(card->driver, "RME9652");
		if (rme9652->hw_rev == 15) {
			rme9652->card_name = "RME Digi9652 (Rev 1.5)";
		} else {
			rme9652->card_name = "RME Digi9652";
		}
		rme9652->ss_channels = RME9652_NCHANNELS;
		break;
	}

	rme9652->ds_channels = (rme9652->ss_channels - 2) / 2 + 2;

	pci_set_master(rme9652->pci);

	if ((err = snd_rme9652_initialize_memory(rme9652)) < 0) {
		return err;
	}

	if ((err = snd_rme9652_create_pcm(card, rme9652)) < 0) {
		return err;
	}

	if ((err = snd_rme9652_create_controls(card, rme9652)) < 0) {
		return err;
	}

	snd_rme9652_proc_init(rme9652);

	rme9652->last_spdif_sample_rate = -1;
	rme9652->last_adat_sample_rate = -1;
	rme9652->playback_pid = -1;
	rme9652->capture_pid = -1;
	rme9652->capture_substream = NULL;
	rme9652->playback_substream = NULL;

	snd_rme9652_set_defaults(rme9652);

	if (rme9652->hw_rev == 15) {
		rme9652_initialize_spdif_receiver (rme9652);
	}

	return 0;
}

2569
static void snd_rme9652_card_free(struct snd_card *card)
Linus Torvalds's avatar
Linus Torvalds committed
2570
{
2571
	struct snd_rme9652 *rme9652 = (struct snd_rme9652 *) card->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
2572 2573 2574 2575 2576

	if (rme9652)
		snd_rme9652_free(rme9652);
}

2577 2578
static int snd_rme9652_probe(struct pci_dev *pci,
			     const struct pci_device_id *pci_id)
Linus Torvalds's avatar
Linus Torvalds committed
2579 2580
{
	static int dev;
2581 2582
	struct snd_rme9652 *rme9652;
	struct snd_card *card;
Linus Torvalds's avatar
Linus Torvalds committed
2583 2584 2585 2586 2587 2588 2589 2590 2591
	int err;

	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

2592 2593
	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
			   sizeof(struct snd_rme9652), &card);
Linus Torvalds's avatar
Linus Torvalds committed
2594

2595 2596
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
2597

2598
	rme9652 = (struct snd_rme9652 *) card->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
2599 2600 2601
	card->private_free = snd_rme9652_card_free;
	rme9652->dev = dev;
	rme9652->pci = pci;
2602 2603 2604
	err = snd_rme9652_create(card, rme9652, precise_ptr[dev]);
	if (err)
		goto free_card;
Linus Torvalds's avatar
Linus Torvalds committed
2605 2606 2607 2608 2609

	strcpy(card->shortname, rme9652->card_name);

	sprintf(card->longname, "%s at 0x%lx, irq %d",
		card->shortname, rme9652->port, rme9652->irq);
2610 2611 2612
	err = snd_card_register(card);
	if (err) {
free_card:
Linus Torvalds's avatar
Linus Torvalds committed
2613 2614 2615 2616 2617 2618 2619 2620
		snd_card_free(card);
		return err;
	}
	pci_set_drvdata(pci, card);
	dev++;
	return 0;
}

2621
static void snd_rme9652_remove(struct pci_dev *pci)
Linus Torvalds's avatar
Linus Torvalds committed
2622 2623 2624 2625
{
	snd_card_free(pci_get_drvdata(pci));
}

2626
static struct pci_driver rme9652_driver = {
2627
	.name	  = KBUILD_MODNAME,
Linus Torvalds's avatar
Linus Torvalds committed
2628 2629
	.id_table = snd_rme9652_ids,
	.probe	  = snd_rme9652_probe,
2630
	.remove	  = snd_rme9652_remove,
Linus Torvalds's avatar
Linus Torvalds committed
2631 2632
};

2633
module_pci_driver(rme9652_driver);