korg1212.c 87.8 KB
Newer Older
L
Linus Torvalds 已提交
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 26 27 28 29
/*
 *   Driver for the Korg 1212 IO PCI card
 *
 *	Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <sound/driver.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/moduleparam.h>
30
#include <linux/mutex.h>
31
#include <linux/firmware.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45

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

#include <asm/io.h>

// ----------------------------------------------------------------------------
// Debug Stuff
// ----------------------------------------------------------------------------
#define K1212_DEBUG_LEVEL		0
46 47 48 49 50 51 52 53 54 55
#if K1212_DEBUG_LEVEL > 0
#define K1212_DEBUG_PRINTK(fmt,args...)	printk(KERN_DEBUG fmt,##args)
#else
#define K1212_DEBUG_PRINTK(fmt,...)
#endif
#if K1212_DEBUG_LEVEL > 1
#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...)	printk(KERN_DEBUG fmt,##args)
#else
#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
#endif
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65

// ----------------------------------------------------------------------------
// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all 
// buffers are alocated as a large piece inside KorgSharedBuffer.
// ----------------------------------------------------------------------------
//#define K1212_LARGEALLOC		1

// ----------------------------------------------------------------------------
// Valid states of the Korg 1212 I/O card.
// ----------------------------------------------------------------------------
66
enum CardState {
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   K1212_STATE_NONEXISTENT,		// there is no card here
   K1212_STATE_UNINITIALIZED,		// the card is awaiting DSP download
   K1212_STATE_DSP_IN_PROCESS,		// the card is currently downloading its DSP code
   K1212_STATE_DSP_COMPLETE,		// the card has finished the DSP download
   K1212_STATE_READY,			// the card can be opened by an application.  Any application
					//    requests prior to this state should fail.  Only an open
					//    request can be made at this state.
   K1212_STATE_OPEN,			// an application has opened the card
   K1212_STATE_SETUP,			// the card has been setup for play
   K1212_STATE_PLAYING,			// the card is playing
   K1212_STATE_MONITOR,			// the card is in the monitor mode
   K1212_STATE_CALIBRATING,		// the card is currently calibrating
   K1212_STATE_ERRORSTOP,		// the card has stopped itself because of an error and we
					//    are in the process of cleaning things up.
   K1212_STATE_MAX_STATE		// state values of this and beyond are invalid
82
};
L
Linus Torvalds 已提交
83 84 85 86 87

// ----------------------------------------------------------------------------
// The following enumeration defines the constants written to the card's
// host-to-card doorbell to initiate a command.
// ----------------------------------------------------------------------------
88
enum korg1212_dbcnst {
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
   K1212_DB_RequestForData        = 0,    // sent by the card to request a buffer fill.
   K1212_DB_TriggerPlay           = 1,    // starts playback/record on the card.
   K1212_DB_SelectPlayMode        = 2,    // select monitor, playback setup, or stop.
   K1212_DB_ConfigureBufferMemory = 3,    // tells card where the host audio buffers are.
   K1212_DB_RequestAdatTimecode   = 4,    // asks the card for the latest ADAT timecode value.
   K1212_DB_SetClockSourceRate    = 5,    // sets the clock source and rate for the card.
   K1212_DB_ConfigureMiscMemory   = 6,    // tells card where other buffers are.
   K1212_DB_TriggerFromAdat       = 7,    // tells card to trigger from Adat at a specific
                                          //    timecode value.
   K1212_DB_DMAERROR              = 0x80, // DMA Error - the PCI bus is congestioned.
   K1212_DB_CARDSTOPPED           = 0x81, // Card has stopped by user request.
   K1212_DB_RebootCard            = 0xA0, // instructs the card to reboot.
   K1212_DB_BootFromDSPPage4      = 0xA4, // instructs the card to boot from the DSP microcode
                                          //    on page 4 (local page to card).
   K1212_DB_DSPDownloadDone       = 0xAE, // sent by the card to indicate the download has
                                          //    completed.
   K1212_DB_StartDSPDownload      = 0xAF  // tells the card to download its DSP firmware.
106
};
L
Linus Torvalds 已提交
107 108 109 110 111 112


// ----------------------------------------------------------------------------
// The following enumeration defines return codes 
// to the Korg 1212 I/O driver.
// ----------------------------------------------------------------------------
113
enum snd_korg1212rc {
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
   K1212_CMDRET_Success         = 0,   // command was successfully placed
   K1212_CMDRET_DIOCFailure,           // the DeviceIoControl call failed
   K1212_CMDRET_PMFailure,             // the protected mode call failed
   K1212_CMDRET_FailUnspecified,       // unspecified failure
   K1212_CMDRET_FailBadState,          // the specified command can not be given in
                                       //    the card's current state. (or the wave device's
                                       //    state)
   K1212_CMDRET_CardUninitialized,     // the card is uninitialized and cannot be used
   K1212_CMDRET_BadIndex,              // an out of range card index was specified
   K1212_CMDRET_BadHandle,             // an invalid card handle was specified
   K1212_CMDRET_NoFillRoutine,         // a play request has been made before a fill routine set
   K1212_CMDRET_FillRoutineInUse,      // can't set a new fill routine while one is in use
   K1212_CMDRET_NoAckFromCard,         // the card never acknowledged a command
   K1212_CMDRET_BadParams,             // bad parameters were provided by the caller

   K1212_CMDRET_BadDevice,             // the specified wave device was out of range
   K1212_CMDRET_BadFormat              // the specified wave format is unsupported
131
};
L
Linus Torvalds 已提交
132 133 134 135 136

// ----------------------------------------------------------------------------
// The following enumeration defines the constants used to select the play
// mode for the card in the SelectPlayMode command.
// ----------------------------------------------------------------------------
137
enum PlayModeSelector {
L
Linus Torvalds 已提交
138 139 140 141
   K1212_MODE_SetupPlay  = 0x00000001,     // provides card with pre-play information
   K1212_MODE_MonitorOn  = 0x00000002,     // tells card to turn on monitor mode
   K1212_MODE_MonitorOff = 0x00000004,     // tells card to turn off monitor mode
   K1212_MODE_StopPlay   = 0x00000008      // stops playback on the card
142
};
L
Linus Torvalds 已提交
143 144 145 146 147

// ----------------------------------------------------------------------------
// The following enumeration defines the constants used to select the monitor
// mode for the card in the SetMonitorMode command.
// ----------------------------------------------------------------------------
148
enum MonitorModeSelector {
L
Linus Torvalds 已提交
149 150
   K1212_MONMODE_Off  = 0,     // tells card to turn off monitor mode
   K1212_MONMODE_On            // tells card to turn on monitor mode
151
};
L
Linus Torvalds 已提交
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 186 187 188 189 190 191 192

#define MAILBOX0_OFFSET      0x40	// location of mailbox 0 relative to base address
#define MAILBOX1_OFFSET      0x44	// location of mailbox 1 relative to base address
#define MAILBOX2_OFFSET      0x48	// location of mailbox 2 relative to base address
#define MAILBOX3_OFFSET      0x4c	// location of mailbox 3 relative to base address
#define OUT_DOORBELL_OFFSET  0x60	// location of PCI to local doorbell
#define IN_DOORBELL_OFFSET   0x64	// location of local to PCI doorbell
#define STATUS_REG_OFFSET    0x68	// location of interrupt control/status register
#define PCI_CONTROL_OFFSET   0x6c	// location of the EEPROM, PCI, User I/O, init control
					//    register
#define SENS_CONTROL_OFFSET  0x6e	// location of the input sensitivity setting register.
					//    this is the upper word of the PCI control reg.
#define DEV_VEND_ID_OFFSET   0x70	// location of the device and vendor ID register

#define COMMAND_ACK_DELAY    13        // number of RTC ticks to wait for an acknowledgement
                                        //    from the card after sending a command.
#define INTERCOMMAND_DELAY   40
#define MAX_COMMAND_RETRIES  5         // maximum number of times the driver will attempt
                                       //    to send a command before giving up.
#define COMMAND_ACK_MASK     0x8000    // the MSB is set in the command acknowledgment from
                                        //    the card.
#define DOORBELL_VAL_MASK    0x00FF    // the doorbell value is one byte

#define CARD_BOOT_DELAY_IN_MS  10
#define CARD_BOOT_TIMEOUT      10
#define DSP_BOOT_DELAY_IN_MS   200

#define kNumBuffers		8
#define k1212MaxCards		4
#define k1212NumWaveDevices	6
#define k16BitChannels		10
#define k32BitChannels		2
#define kAudioChannels		(k16BitChannels + k32BitChannels)
#define kPlayBufferFrames	1024

#define K1212_ANALOG_CHANNELS	2
#define K1212_SPDIF_CHANNELS	2
#define K1212_ADAT_CHANNELS	8
#define K1212_CHANNELS		(K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
#define K1212_MIN_CHANNELS	1
#define K1212_MAX_CHANNELS	K1212_CHANNELS
193
#define K1212_FRAME_SIZE        (sizeof(struct KorgAudioFrame))
L
Linus Torvalds 已提交
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#define K1212_MAX_SAMPLES	(kPlayBufferFrames*kNumBuffers)
#define K1212_PERIODS		(kNumBuffers)
#define K1212_PERIOD_BYTES	(K1212_FRAME_SIZE*kPlayBufferFrames)
#define K1212_BUF_SIZE          (K1212_PERIOD_BYTES*kNumBuffers)
#define K1212_ANALOG_BUF_SIZE	(K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
#define K1212_SPDIF_BUF_SIZE	(K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
#define K1212_ADAT_BUF_SIZE	(K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
#define K1212_MAX_BUF_SIZE	(K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)

#define k1212MinADCSens     0x7f
#define k1212MaxADCSens     0x00
#define k1212MaxVolume      0x7fff
#define k1212MaxWaveVolume  0xffff
#define k1212MinVolume      0x0000
#define k1212MaxVolInverted 0x8000

// -----------------------------------------------------------------
// the following bits are used for controlling interrupts in the
// interrupt control/status reg
// -----------------------------------------------------------------
#define  PCI_INT_ENABLE_BIT               0x00000100
#define  PCI_DOORBELL_INT_ENABLE_BIT      0x00000200
#define  LOCAL_INT_ENABLE_BIT             0x00010000
#define  LOCAL_DOORBELL_INT_ENABLE_BIT    0x00020000
#define  LOCAL_DMA1_INT_ENABLE_BIT        0x00080000

// -----------------------------------------------------------------
// the following bits are defined for the PCI command register
// -----------------------------------------------------------------
#define  PCI_CMD_MEM_SPACE_ENABLE_BIT     0x0002
#define  PCI_CMD_IO_SPACE_ENABLE_BIT      0x0001
#define  PCI_CMD_BUS_MASTER_ENABLE_BIT    0x0004

// -----------------------------------------------------------------
// the following bits are defined for the PCI status register
// -----------------------------------------------------------------
#define  PCI_STAT_PARITY_ERROR_BIT        0x8000
#define  PCI_STAT_SYSTEM_ERROR_BIT        0x4000
#define  PCI_STAT_MASTER_ABORT_RCVD_BIT   0x2000
#define  PCI_STAT_TARGET_ABORT_RCVD_BIT   0x1000
#define  PCI_STAT_TARGET_ABORT_SENT_BIT   0x0800

// ------------------------------------------------------------------------
// the following constants are used in setting the 1212 I/O card's input
// sensitivity.
// ------------------------------------------------------------------------
#define  SET_SENS_LOCALINIT_BITPOS        15
#define  SET_SENS_DATA_BITPOS             10
#define  SET_SENS_CLOCK_BITPOS            8
#define  SET_SENS_LOADSHIFT_BITPOS        0

#define  SET_SENS_LEFTCHANID              0x00
#define  SET_SENS_RIGHTCHANID             0x01

#define  K1212SENSUPDATE_DELAY_IN_MS      50

// --------------------------------------------------------------------------
// WaitRTCTicks
//
//    This function waits the specified number of real time clock ticks.
//    According to the DDK, each tick is ~0.8 microseconds.
//    The defines following the function declaration can be used for the
//    numTicksToWait parameter.
// --------------------------------------------------------------------------
#define ONE_RTC_TICK         1
#define SENSCLKPULSE_WIDTH   4
#define LOADSHIFT_DELAY      4
#define INTERCOMMAND_DELAY  40
#define STOPCARD_DELAY      300        // max # RTC ticks for the card to stop once we write
                                       //    the command register.  (could be up to 180 us)
#define COMMAND_ACK_DELAY   13         // number of RTC ticks to wait for an acknowledgement
                                       //    from the card after sending a command.

267
#ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
L
Linus Torvalds 已提交
268
#include "korg1212-firmware.h"
269 270 271 272 273
static const struct firmware static_dsp_code = {
	.data = (u8 *)dspCode,
	.size = sizeof dspCode
};
#endif
L
Linus Torvalds 已提交
274

275
enum ClockSourceIndex {
L
Linus Torvalds 已提交
276 277 278 279 280 281 282
   K1212_CLKIDX_AdatAt44_1K = 0,    // selects source as ADAT at 44.1 kHz
   K1212_CLKIDX_AdatAt48K,          // selects source as ADAT at 48 kHz
   K1212_CLKIDX_WordAt44_1K,        // selects source as S/PDIF at 44.1 kHz
   K1212_CLKIDX_WordAt48K,          // selects source as S/PDIF at 48 kHz
   K1212_CLKIDX_LocalAt44_1K,       // selects source as local clock at 44.1 kHz
   K1212_CLKIDX_LocalAt48K,         // selects source as local clock at 48 kHz
   K1212_CLKIDX_Invalid             // used to check validity of the index
283
};
L
Linus Torvalds 已提交
284

285
enum ClockSourceType {
L
Linus Torvalds 已提交
286 287 288
   K1212_CLKIDX_Adat = 0,    // selects source as ADAT
   K1212_CLKIDX_Word,        // selects source as S/PDIF
   K1212_CLKIDX_Local        // selects source as local clock
289
};
L
Linus Torvalds 已提交
290

291 292 293 294 295
struct KorgAudioFrame {
	u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
	u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
	u32 timeCodeVal; /* holds the ADAT timecode value */
};
L
Linus Torvalds 已提交
296

297 298 299
struct KorgAudioBuffer {
	struct KorgAudioFrame  bufferData[kPlayBufferFrames];     /* buffer definition */
};
L
Linus Torvalds 已提交
300

301
struct KorgSharedBuffer {
L
Linus Torvalds 已提交
302
#ifdef K1212_LARGEALLOC
303 304
   struct KorgAudioBuffer   playDataBufs[kNumBuffers];
   struct KorgAudioBuffer   recordDataBufs[kNumBuffers];
L
Linus Torvalds 已提交
305 306 307 308 309
#endif
   short             volumeData[kAudioChannels];
   u32               cardCommand;
   u16               routeData [kAudioChannels];
   u32               AdatTimeCode;                 // ADAT timecode value
310
};
L
Linus Torvalds 已提交
311

312
struct SensBits {
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
   union {
      struct {
         unsigned int leftChanVal:8;
         unsigned int leftChanId:8;
      } v;
      u16  leftSensBits;
   } l;
   union {
      struct {
         unsigned int rightChanVal:8;
         unsigned int rightChanId:8;
      } v;
      u16  rightSensBits;
   } r;
327
};
L
Linus Torvalds 已提交
328

329 330
struct snd_korg1212 {
        struct snd_card *card;
L
Linus Torvalds 已提交
331
        struct pci_dev *pci;
332
        struct snd_pcm *pcm;
L
Linus Torvalds 已提交
333 334 335
        int irq;

        spinlock_t    lock;
336
	struct mutex open_mutex;
L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

	struct timer_list timer;	/* timer callback for checking ack of stop request */
	int stop_pending_cnt;		/* counter for stop pending check */

        wait_queue_head_t wait;

        unsigned long iomem;
        unsigned long ioport;
	unsigned long iomem2;
        unsigned long irqcount;
        unsigned long inIRQ;
        void __iomem *iobase;

	struct snd_dma_buffer dma_dsp;
        struct snd_dma_buffer dma_play;
        struct snd_dma_buffer dma_rec;
	struct snd_dma_buffer dma_shared;

	u32 DataBufsSize;

357 358
        struct KorgAudioBuffer  * playDataBufsPtr;
        struct KorgAudioBuffer  * recordDataBufsPtr;
L
Linus Torvalds 已提交
359

360
	struct KorgSharedBuffer * sharedBufferPtr;
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

	u32 RecDataPhy;
	u32 PlayDataPhy;
	unsigned long sharedBufferPhy;
	u32 VolumeTablePhy;
	u32 RoutingTablePhy;
	u32 AdatTimeCodePhy;

        u32 __iomem * statusRegPtr;	     // address of the interrupt status/control register
        u32 __iomem * outDoorbellPtr;	     // address of the host->card doorbell register
        u32 __iomem * inDoorbellPtr;	     // address of the card->host doorbell register
        u32 __iomem * mailbox0Ptr;	     // address of mailbox 0 on the card
        u32 __iomem * mailbox1Ptr;	     // address of mailbox 1 on the card
        u32 __iomem * mailbox2Ptr;	     // address of mailbox 2 on the card
        u32 __iomem * mailbox3Ptr;	     // address of mailbox 3 on the card
        u32 __iomem * controlRegPtr;	     // address of the EEPROM, PCI, I/O, Init ctrl reg
        u16 __iomem * sensRegPtr;	     // address of the sensitivity setting register
        u32 __iomem * idRegPtr;		     // address of the device and vendor ID registers

        size_t periodsize;
	int channels;
        int currentBuffer;

384 385
        struct snd_pcm_substream *playback_substream;
        struct snd_pcm_substream *capture_substream;
L
Linus Torvalds 已提交
386 387 388 389

	pid_t capture_pid;
	pid_t playback_pid;

390
 	enum CardState cardState;
L
Linus Torvalds 已提交
391 392 393 394
        int running;
        int idleMonitorOn;           // indicates whether the card is in idle monitor mode.
        u32 cmdRetryCount;           // tracks how many times we have retried sending to the card.

395
        enum ClockSourceIndex clkSrcRate; // sample rate and clock source
L
Linus Torvalds 已提交
396

397
        enum ClockSourceType clkSource;   // clock source
L
Linus Torvalds 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        int clkRate;                 // clock rate

        int volumePhase[kAudioChannels];

        u16 leftADCInSens;           // ADC left channel input sensitivity
        u16 rightADCInSens;          // ADC right channel input sensitivity

	int opencnt;		     // Open/Close count
	int setcnt;		     // SetupForPlay count
	int playcnt;		     // TriggerPlay count
	int errorcnt;		     // Error Count
	unsigned long totalerrorcnt; // Total Error Count

	int dsp_is_loaded;
	int dsp_stop_is_processed;

};

MODULE_DESCRIPTION("korg1212");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	   /* ID for this card */
static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");

432
static struct pci_device_id snd_korg1212_ids[] = {
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441
	{
		.vendor	   = 0x10b5,
		.device	   = 0x906d,
		.subvendor = PCI_ANY_ID,
		.subdevice = PCI_ANY_ID,
	},
	{ 0, },
};

442 443
MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);

444 445 446 447 448 449 450 451 452 453 454 455
static char *stateName[] = {
	"Non-existent",
	"Uninitialized",
	"DSP download in process",
	"DSP download complete",
	"Ready",
	"Open",
	"Setup for play",
	"Playing",
	"Monitor mode on",
	"Calibrating",
	"Invalid"
L
Linus Torvalds 已提交
456 457
};

458
static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
L
Linus Torvalds 已提交
459

460 461 462 463 464 465 466
static char *clockSourceName[] = {
	"ADAT at 44.1 kHz",
	"ADAT at 48 kHz",
	"S/PDIF at 44.1 kHz",
	"S/PDIF at 48 kHz",
	"local clock at 44.1 kHz",
	"local clock at 48 kHz"
L
Linus Torvalds 已提交
467 468
};

469 470 471 472 473 474 475 476 477 478 479 480 481
static char *channelName[] = {
	"ADAT-1",
	"ADAT-2",
	"ADAT-3",
	"ADAT-4",
	"ADAT-5",
	"ADAT-6",
	"ADAT-7",
	"ADAT-8",
	"Analog-L",
	"Analog-R",
	"SPDIF-L",
	"SPDIF-R",
L
Linus Torvalds 已提交
482 483
};

484 485 486 487 488 489 490 491
static u16 ClockSourceSelector[] = {
	0x8000,   // selects source as ADAT at 44.1 kHz
	0x0000,   // selects source as ADAT at 48 kHz
	0x8001,   // selects source as S/PDIF at 44.1 kHz
	0x0001,   // selects source as S/PDIF at 48 kHz
	0x8002,   // selects source as local clock at 44.1 kHz
	0x0002    // selects source as local clock at 48 kHz
};
L
Linus Torvalds 已提交
492

493
union swap_u32 { unsigned char c[4]; u32 i; };
L
Linus Torvalds 已提交
494 495 496 497 498 499 500

#ifdef SNDRV_BIG_ENDIAN
static u32 LowerWordSwap(u32 swappee)
#else
static u32 UpperWordSwap(u32 swappee)
#endif
{
501
   union swap_u32 retVal, swapper;
L
Linus Torvalds 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

   swapper.i = swappee;
   retVal.c[2] = swapper.c[3];
   retVal.c[3] = swapper.c[2];
   retVal.c[1] = swapper.c[1];
   retVal.c[0] = swapper.c[0];

   return retVal.i;
}

#ifdef SNDRV_BIG_ENDIAN
static u32 UpperWordSwap(u32 swappee)
#else
static u32 LowerWordSwap(u32 swappee)
#endif
{
518
   union swap_u32 retVal, swapper;
L
Linus Torvalds 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

   swapper.i = swappee;
   retVal.c[2] = swapper.c[2];
   retVal.c[3] = swapper.c[3];
   retVal.c[1] = swapper.c[0];
   retVal.c[0] = swapper.c[1];

   return retVal.i;
}

#define SetBitInWord(theWord,bitPosition)       (*theWord) |= (0x0001 << bitPosition)
#define SetBitInDWord(theWord,bitPosition)      (*theWord) |= (0x00000001 << bitPosition)
#define ClearBitInWord(theWord,bitPosition)     (*theWord) &= ~(0x0001 << bitPosition)
#define ClearBitInDWord(theWord,bitPosition)    (*theWord) &= ~(0x00000001 << bitPosition)

534 535 536 537
static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
					enum korg1212_dbcnst doorbellVal,
					u32 mailBox0Val, u32 mailBox1Val,
					u32 mailBox2Val, u32 mailBox3Val)
L
Linus Torvalds 已提交
538 539 540
{
        u32 retryCount;
        u16 mailBox3Lo;
541
	int rc = K1212_CMDRET_Success;
L
Linus Torvalds 已提交
542 543

        if (!korg1212->outDoorbellPtr) {
544
		K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
L
Linus Torvalds 已提交
545 546 547
                return K1212_CMDRET_CardUninitialized;
	}

548 549
	K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
			   doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
		writel(mailBox3Val, korg1212->mailbox3Ptr);
                writel(mailBox2Val, korg1212->mailbox2Ptr);
                writel(mailBox1Val, korg1212->mailbox1Ptr);
                writel(mailBox0Val, korg1212->mailbox0Ptr);
                writel(doorbellVal, korg1212->outDoorbellPtr);  // interrupt the card

                // --------------------------------------------------------------
                // the reboot command will not give an acknowledgement.
                // --------------------------------------------------------------
                if ( doorbellVal == K1212_DB_RebootCard ||
                	doorbellVal == K1212_DB_BootFromDSPPage4 ||
                        doorbellVal == K1212_DB_StartDSPDownload ) {
                        rc = K1212_CMDRET_Success;
                        break;
                }

                // --------------------------------------------------------------
                // See if the card acknowledged the command.  Wait a bit, then
                // read in the low word of mailbox3.  If the MSB is set and the
                // low byte is equal to the doorbell value, then it ack'd.
                // --------------------------------------------------------------
                udelay(COMMAND_ACK_DELAY);
                mailBox3Lo = readl(korg1212->mailbox3Ptr);
                if (mailBox3Lo & COMMAND_ACK_MASK) {
                	if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
576
				K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584
                                rc = K1212_CMDRET_Success;
				break;
                        }
                }
	}
        korg1212->cmdRetryCount += retryCount;

	if (retryCount >= MAX_COMMAND_RETRIES) {
585
		K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
L
Linus Torvalds 已提交
586 587 588 589 590 591 592
        	rc = K1212_CMDRET_NoAckFromCard;
	}

	return rc;
}

/* spinlock already held */
593
static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603
{
	if (! korg1212->stop_pending_cnt) {
		korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
		/* program the timer */
		korg1212->stop_pending_cnt = HZ;
		korg1212->timer.expires = jiffies + 1;
		add_timer(&korg1212->timer);
	}
}

604
static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615 616
{
	unsigned long flags;
	spin_lock_irqsave(&korg1212->lock, flags);
	korg1212->dsp_stop_is_processed = 0;
	snd_korg1212_SendStop(korg1212);
	spin_unlock_irqrestore(&korg1212->lock, flags);
	wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
}

/* timer callback for checking the ack of stop request */
static void snd_korg1212_timer_func(unsigned long data)
{
617
        struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
618
	unsigned long flags;
L
Linus Torvalds 已提交
619
	
620
	spin_lock_irqsave(&korg1212->lock, flags);
L
Linus Torvalds 已提交
621 622 623 624 625
	if (korg1212->sharedBufferPtr->cardCommand == 0) {
		/* ack'ed */
		korg1212->stop_pending_cnt = 0;
		korg1212->dsp_stop_is_processed = 1;
		wake_up(&korg1212->wait);
626 627
		K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
					   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636 637
	} else {
		if (--korg1212->stop_pending_cnt > 0) {
			/* reprogram timer */
			korg1212->timer.expires = jiffies + 1;
			add_timer(&korg1212->timer);
		} else {
			snd_printd("korg1212_timer_func timeout\n");
			korg1212->sharedBufferPtr->cardCommand = 0;
			korg1212->dsp_stop_is_processed = 1;
			wake_up(&korg1212->wait);
638 639
			K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
					   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
640 641
		}
	}
642
	spin_unlock_irqrestore(&korg1212->lock, flags);
L
Linus Torvalds 已提交
643 644
}

645
static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
646 647
{
	unsigned long flags;
648
	int rc;
L
Linus Torvalds 已提交
649 650 651 652 653 654 655

        udelay(INTERCOMMAND_DELAY);
	spin_lock_irqsave(&korg1212->lock, flags);
        korg1212->idleMonitorOn = 1;
        rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
					  K1212_MODE_MonitorOn, 0, 0, 0);
        spin_unlock_irqrestore(&korg1212->lock, flags);
656
	return rc;
L
Linus Torvalds 已提交
657 658
}

659
static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
660 661 662 663 664 665 666
{
        if (korg1212->idleMonitorOn) {
		snd_korg1212_SendStopAndWait(korg1212);
                korg1212->idleMonitorOn = 0;
        }
}

667
static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
L
Linus Torvalds 已提交
668 669 670 671
{
        korg1212->cardState = csState;
}

672
static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
L
Linus Torvalds 已提交
673
{
674 675
	K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
			   stateName[korg1212->cardState], korg1212->opencnt);
676
	mutex_lock(&korg1212->open_mutex);
L
Linus Torvalds 已提交
677 678 679 680 681
        if (korg1212->opencnt++ == 0) {
		snd_korg1212_TurnOffIdleMonitor(korg1212);
		snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
	}

682
	mutex_unlock(&korg1212->open_mutex);
L
Linus Torvalds 已提交
683 684 685
        return 1;
}

686
static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
L
Linus Torvalds 已提交
687
{
688 689
	K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
			   stateName[korg1212->cardState], korg1212->opencnt);
L
Linus Torvalds 已提交
690

691
	mutex_lock(&korg1212->open_mutex);
L
Linus Torvalds 已提交
692
	if (--(korg1212->opencnt)) {
693
		mutex_unlock(&korg1212->open_mutex);
L
Linus Torvalds 已提交
694 695 696 697
		return 0;
	}

        if (korg1212->cardState == K1212_STATE_SETUP) {
698
                int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
L
Linus Torvalds 已提交
699
                                K1212_MODE_StopPlay, 0, 0, 0);
700 701 702
		if (rc)
			K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
					   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
703
		if (rc != K1212_CMDRET_Success) {
704
			mutex_unlock(&korg1212->open_mutex);
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712 713 714 715
                        return 0;
		}
        } else if (korg1212->cardState > K1212_STATE_SETUP) {
		snd_korg1212_SendStopAndWait(korg1212);
        }

        if (korg1212->cardState > K1212_STATE_READY) {
		snd_korg1212_TurnOnIdleMonitor(korg1212);
                snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
	}

716
	mutex_unlock(&korg1212->open_mutex);
L
Linus Torvalds 已提交
717 718 719 720
        return 0;
}

/* spinlock already held */
721
static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
L
Linus Torvalds 已提交
722
{
723 724 725 726
	int rc;

	K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
			   stateName[korg1212->cardState], korg1212->setcnt);
L
Linus Torvalds 已提交
727 728 729 730 731 732 733

        if (korg1212->setcnt++)
		return 0;

        snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
        rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
                                        K1212_MODE_SetupPlay, 0, 0, 0);
734 735 736
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
737 738 739 740 741 742 743
        if (rc != K1212_CMDRET_Success) {
                return 1;
        }
        return 0;
}

/* spinlock already held */
744
static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
L
Linus Torvalds 已提交
745
{
746 747 748 749
	int rc;

	K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
			   stateName[korg1212->cardState], korg1212->playcnt);
L
Linus Torvalds 已提交
750 751 752 753 754 755

        if (korg1212->playcnt++)
		return 0;

        snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
        rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
756 757 758
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
759 760 761 762 763 764 765
        if (rc != K1212_CMDRET_Success) {
                return 1;
        }
        return 0;
}

/* spinlock already held */
766
static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
L
Linus Torvalds 已提交
767
{
768 769
	K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
			   stateName[korg1212->cardState], korg1212->playcnt);
L
Linus Torvalds 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782

        if (--(korg1212->playcnt)) 
		return 0;

	korg1212->setcnt = 0;

        if (korg1212->cardState != K1212_STATE_ERRORSTOP)
		snd_korg1212_SendStop(korg1212);

	snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
        return 0;
}

783
static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
L
Linus Torvalds 已提交
784 785 786 787 788 789 790 791 792 793 794
{
	writel(PCI_INT_ENABLE_BIT            |
	       PCI_DOORBELL_INT_ENABLE_BIT   |
	       LOCAL_INT_ENABLE_BIT          |
	       LOCAL_DOORBELL_INT_ENABLE_BIT |
	       LOCAL_DMA1_INT_ENABLE_BIT,
	       korg1212->statusRegPtr);
}

#if 0 /* not used */

795 796
static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
				       enum MonitorModeSelector mode)
L
Linus Torvalds 已提交
797
{
798 799
	K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
800 801

        switch (mode) {
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
	case K1212_MONMODE_Off:
		if (korg1212->cardState != K1212_STATE_MONITOR)
			return 0;
		else {
			snd_korg1212_SendStopAndWait(korg1212);
			snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
		}
		break;

	case K1212_MONMODE_On:
		if (korg1212->cardState != K1212_STATE_OPEN)
			return 0;
		else {
			int rc;
			snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
			rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
							  K1212_MODE_MonitorOn, 0, 0, 0);
			if (rc != K1212_CMDRET_Success)
				return 0;
		}
		break;
L
Linus Torvalds 已提交
823

824 825
	default:
		return 0;
L
Linus Torvalds 已提交
826 827 828 829 830 831 832
        }

        return 1;
}

#endif /* not used */

833
static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
834
{
835 836 837
	if (korg1212->playback_pid != korg1212->capture_pid &&
	    korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
		return 0;
L
Linus Torvalds 已提交
838

839
	return 1;
L
Linus Torvalds 已提交
840 841
}

842
static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
L
Linus Torvalds 已提交
843
{
844
        static enum ClockSourceIndex s44[] = {
845 846 847 848
		K1212_CLKIDX_AdatAt44_1K,
		K1212_CLKIDX_WordAt44_1K,
		K1212_CLKIDX_LocalAt44_1K
	};
849
        static enum ClockSourceIndex s48[] = {
850 851 852 853 854
		K1212_CLKIDX_AdatAt48K,
		K1212_CLKIDX_WordAt48K,
		K1212_CLKIDX_LocalAt48K
	};
        int parm, rc;
L
Linus Torvalds 已提交
855

856 857
	if (!snd_korg1212_use_is_exclusive (korg1212))
		return -EBUSY;
L
Linus Torvalds 已提交
858

859
	switch (rate) {
860 861 862
	case 44100:
		parm = s44[korg1212->clkSource];
		break;
L
Linus Torvalds 已提交
863

864 865 866
	case 48000:
		parm = s48[korg1212->clkSource];
		break;
L
Linus Torvalds 已提交
867

868 869 870
	default:
		return -EINVAL;
	}
L
Linus Torvalds 已提交
871 872 873 874 875 876 877 878

        korg1212->clkSrcRate = parm;
        korg1212->clkRate = rate;

	udelay(INTERCOMMAND_DELAY);
	rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
					  ClockSourceSelector[korg1212->clkSrcRate],
					  0, 0, 0);
879 880 881
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
882 883 884 885

        return 0;
}

886
static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
L
Linus Torvalds 已提交
887 888
{

889 890
	if (source < 0 || source > 2)
		return -EINVAL;
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898

        korg1212->clkSource = source;

        snd_korg1212_SetRate(korg1212, korg1212->clkRate);

        return 0;
}

899
static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
900 901 902 903
{
	writel(0, korg1212->statusRegPtr);
}

904
static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
905
{
906
        struct SensBits  sensVals;
L
Linus Torvalds 已提交
907 908 909 910 911 912 913 914 915
        int       bitPosition;
        int       channel;
        int       clkIs48K;
        int       monModeSet;
        u16       controlValue;    // this keeps the current value to be written to
                                   //  the card's eeprom control register.
        u16       count;
	unsigned long flags;

916 917
	K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

        // ----------------------------------------------------------------------------
        // initialize things.  The local init bit is always set when writing to the
        // card's control register.
        // ----------------------------------------------------------------------------
        controlValue = 0;
        SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS);    // init the control value

        // ----------------------------------------------------------------------------
        // make sure the card is not in monitor mode when we do this update.
        // ----------------------------------------------------------------------------
        if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
                monModeSet = 1;
		snd_korg1212_SendStopAndWait(korg1212);
        } else
                monModeSet = 0;

	spin_lock_irqsave(&korg1212->lock, flags);

        // ----------------------------------------------------------------------------
        // we are about to send new values to the card, so clear the new values queued
        // flag.  Also, clear out mailbox 3, so we don't lockup.
        // ----------------------------------------------------------------------------
        writel(0, korg1212->mailbox3Ptr);
        udelay(LOADSHIFT_DELAY);

        // ----------------------------------------------------------------------------
        // determine whether we are running a 48K or 44.1K clock.  This info is used
        // later when setting the SPDIF FF after the volume has been shifted in.
        // ----------------------------------------------------------------------------
        switch (korg1212->clkSrcRate) {
                case K1212_CLKIDX_AdatAt44_1K:
                case K1212_CLKIDX_WordAt44_1K:
                case K1212_CLKIDX_LocalAt44_1K:
                        clkIs48K = 0;
                        break;

                case K1212_CLKIDX_WordAt48K:
                case K1212_CLKIDX_AdatAt48K:
                case K1212_CLKIDX_LocalAt48K:
                default:
                        clkIs48K = 1;
                        break;
        }

        // ----------------------------------------------------------------------------
        // start the update.  Setup the bit structure and then shift the bits.
        // ----------------------------------------------------------------------------
        sensVals.l.v.leftChanId   = SET_SENS_LEFTCHANID;
        sensVals.r.v.rightChanId  = SET_SENS_RIGHTCHANID;
        sensVals.l.v.leftChanVal  = korg1212->leftADCInSens;
        sensVals.r.v.rightChanVal = korg1212->rightADCInSens;

        // ----------------------------------------------------------------------------
        // now start shifting the bits in.  Start with the left channel then the right.
        // ----------------------------------------------------------------------------
        for (channel = 0; channel < 2; channel++) {

                // ----------------------------------------------------------------------------
                // Bring the load/shift line low, then wait - the spec says >150ns from load/
                // shift low to the first rising edge of the clock.
                // ----------------------------------------------------------------------------
                ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
                ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
                writew(controlValue, korg1212->sensRegPtr);                          // load/shift goes low
                udelay(LOADSHIFT_DELAY);

                for (bitPosition = 15; bitPosition >= 0; bitPosition--) {       // for all the bits
986 987
			if (channel == 0) {
				if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
988
                                        SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);     // data bit set high
989 990 991 992 993 994 995 996
				else
					ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);   // data bit set low
			} else {
                                if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
					SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);     // data bit set high
				else
					ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);   // data bit set low
			}
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

                        ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
                        writew(controlValue, korg1212->sensRegPtr);                       // clock goes low
                        udelay(SENSCLKPULSE_WIDTH);
                        SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
                        writew(controlValue, korg1212->sensRegPtr);                       // clock goes high
                        udelay(SENSCLKPULSE_WIDTH);
                }

                // ----------------------------------------------------------------------------
                // finish up SPDIF for left.  Bring the load/shift line high, then write a one
                // bit if the clock rate is 48K otherwise write 0.
                // ----------------------------------------------------------------------------
                ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
                ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
                SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
                writew(controlValue, korg1212->sensRegPtr);                   // load shift goes high - clk low
                udelay(SENSCLKPULSE_WIDTH);

                if (clkIs48K)
                        SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);

                writew(controlValue, korg1212->sensRegPtr);                   // set/clear data bit
                udelay(ONE_RTC_TICK);
                SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
                writew(controlValue, korg1212->sensRegPtr);                   // clock goes high
                udelay(SENSCLKPULSE_WIDTH);
                ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
                writew(controlValue, korg1212->sensRegPtr);                   // clock goes low
                udelay(SENSCLKPULSE_WIDTH);
        }

        // ----------------------------------------------------------------------------
        // The update is complete.  Set a timeout.  This is the inter-update delay.
        // Also, if the card was in monitor mode, restore it.
        // ----------------------------------------------------------------------------
        for (count = 0; count < 10; count++)
                udelay(SENSCLKPULSE_WIDTH);

        if (monModeSet) {
1037
                int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
L
Linus Torvalds 已提交
1038
                                K1212_MODE_MonitorOn, 0, 0, 0);
1039 1040 1041
	        if (rc)
			K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
					   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048
        }

	spin_unlock_irqrestore(&korg1212->lock, flags);

        return 1;
}

1049
static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
1050
{
1051
        int channel, rc;
L
Linus Torvalds 已提交
1052

1053 1054
        K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060

        // ----------------------------------------------------
        // tell the card to boot
        // ----------------------------------------------------
        rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);

1061 1062 1063 1064
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
	msleep(DSP_BOOT_DELAY_IN_MS);
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

        // --------------------------------------------------------------------------------
        // Let the card know where all the buffers are.
        // --------------------------------------------------------------------------------
        rc = snd_korg1212_Send1212Command(korg1212,
                        K1212_DB_ConfigureBufferMemory,
                        LowerWordSwap(korg1212->PlayDataPhy),
                        LowerWordSwap(korg1212->RecDataPhy),
                        ((kNumBuffers * kPlayBufferFrames) / 2),   // size given to the card
                                                                   // is based on 2 buffers
                        0
        );

1078 1079 1080
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

        udelay(INTERCOMMAND_DELAY);

        rc = snd_korg1212_Send1212Command(korg1212,
                        K1212_DB_ConfigureMiscMemory,
                        LowerWordSwap(korg1212->VolumeTablePhy),
                        LowerWordSwap(korg1212->RoutingTablePhy),
                        LowerWordSwap(korg1212->AdatTimeCodePhy),
                        0
        );

1092 1093 1094
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

        // --------------------------------------------------------------------------------
        // Initialize the routing and volume tables, then update the card's state.
        // --------------------------------------------------------------------------------
        udelay(INTERCOMMAND_DELAY);

        for (channel = 0; channel < kAudioChannels; channel++) {
                korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
                //korg1212->sharedBufferPtr->routeData[channel] = channel;
                korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
        }

        snd_korg1212_WriteADCSensitivity(korg1212);

	udelay(INTERCOMMAND_DELAY);
	rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
					  ClockSourceSelector[korg1212->clkSrcRate],
					  0, 0, 0);
1113 1114 1115
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1116

1117
	rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
L
Linus Torvalds 已提交
1118 1119
	snd_korg1212_setCardState(korg1212, K1212_STATE_READY);

1120 1121 1122
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1123 1124 1125 1126

	snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
}

1127
static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1128 1129
{
        u32 doorbellValue;
1130
        struct snd_korg1212 *korg1212 = dev_id;
L
Linus Torvalds 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

        doorbellValue = readl(korg1212->inDoorbellPtr);

        if (!doorbellValue)
		return IRQ_NONE;

	spin_lock(&korg1212->lock);

	writel(doorbellValue, korg1212->inDoorbellPtr);

        korg1212->irqcount++;

	korg1212->inIRQ++;

        switch (doorbellValue) {
                case K1212_DB_DSPDownloadDone:
1147 1148 1149
                        K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
					   korg1212->irqcount, doorbellValue,
					   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
                        if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
				korg1212->dsp_is_loaded = 1;
				wake_up(&korg1212->wait);
			}
                        break;

                // ------------------------------------------------------------------------
                // an error occurred - stop the card
                // ------------------------------------------------------------------------
                case K1212_DB_DMAERROR:
1160 1161 1162 1163
			K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
						   korg1212->irqcount, doorbellValue,
						   stateName[korg1212->cardState]);
			snd_printk(KERN_ERR "korg1212: DMA Error\n");
L
Linus Torvalds 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
			korg1212->errorcnt++;
			korg1212->totalerrorcnt++;
			korg1212->sharedBufferPtr->cardCommand = 0;
			snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
                        break;

                // ------------------------------------------------------------------------
                // the card has stopped by our request.  Clear the command word and signal
                // the semaphore in case someone is waiting for this.
                // ------------------------------------------------------------------------
                case K1212_DB_CARDSTOPPED:
1175
                        K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
1176 1177
						   korg1212->irqcount, doorbellValue,
						   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1178 1179 1180 1181
			korg1212->sharedBufferPtr->cardCommand = 0;
                        break;

                default:
1182
			K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
1183 1184
			       korg1212->irqcount, doorbellValue, 
			       korg1212->currentBuffer, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
                        if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
                                korg1212->currentBuffer++;

                                if (korg1212->currentBuffer >= kNumBuffers)
                                        korg1212->currentBuffer = 0;

                                if (!korg1212->running)
                                        break;

                                if (korg1212->capture_substream) {
					spin_unlock(&korg1212->lock);
                                        snd_pcm_period_elapsed(korg1212->capture_substream);
					spin_lock(&korg1212->lock);
                                }

                                if (korg1212->playback_substream) {
					spin_unlock(&korg1212->lock);
                                        snd_pcm_period_elapsed(korg1212->playback_substream);
					spin_lock(&korg1212->lock);
                                }
                        }
                        break;
        }

	korg1212->inIRQ--;

	spin_unlock(&korg1212->lock);

	return IRQ_HANDLED;
}

1216
static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
1217
{
1218
	int rc;
L
Linus Torvalds 已提交
1219

1220 1221
        K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1222 1223 1224 1225

        // ---------------------------------------------------------------
        // verify the state of the card before proceeding.
        // ---------------------------------------------------------------
1226
        if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
L
Linus Torvalds 已提交
1227 1228 1229 1230 1231 1232 1233
                return 1;

        snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);

        rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
                                     UpperWordSwap(korg1212->dma_dsp.addr),
                                     0, 0, 0);
1234 1235 1236
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
				   rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

	korg1212->dsp_is_loaded = 0;
	wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
	if (! korg1212->dsp_is_loaded )
		return -EBUSY; /* timeout */

	snd_korg1212_OnDSPDownloadComplete(korg1212);

        return 0;
}

1248
static struct snd_pcm_hardware snd_korg1212_playback_info =
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
{
	.info =              (SNDRV_PCM_INFO_MMAP |
                              SNDRV_PCM_INFO_MMAP_VALID |
                              SNDRV_PCM_INFO_INTERLEAVED),
	.formats =	      SNDRV_PCM_FMTBIT_S16_LE,
        .rates =              (SNDRV_PCM_RATE_44100 |
                              SNDRV_PCM_RATE_48000),
        .rate_min =           44100,
        .rate_max =           48000,
        .channels_min =       K1212_MIN_CHANNELS,
        .channels_max =       K1212_MAX_CHANNELS,
        .buffer_bytes_max =   K1212_MAX_BUF_SIZE,
        .period_bytes_min =   K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
        .period_bytes_max =   K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
        .periods_min =        K1212_PERIODS,
        .periods_max =        K1212_PERIODS,
        .fifo_size =          0,
};

1268
static struct snd_pcm_hardware snd_korg1212_capture_info =
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
{
        .info =              (SNDRV_PCM_INFO_MMAP |
                              SNDRV_PCM_INFO_MMAP_VALID |
                              SNDRV_PCM_INFO_INTERLEAVED),
        .formats =	      SNDRV_PCM_FMTBIT_S16_LE,
        .rates =	      (SNDRV_PCM_RATE_44100 |
                              SNDRV_PCM_RATE_48000),
        .rate_min =           44100,
        .rate_max =           48000,
        .channels_min =       K1212_MIN_CHANNELS,
        .channels_max =       K1212_MAX_CHANNELS,
        .buffer_bytes_max =   K1212_MAX_BUF_SIZE,
        .period_bytes_min =   K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
        .period_bytes_max =   K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
        .periods_min =        K1212_PERIODS,
        .periods_max =        K1212_PERIODS,
        .fifo_size =          0,
};

1288
static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
L
Linus Torvalds 已提交
1289
{
1290
	struct KorgAudioFrame * dst =  korg1212->playDataBufsPtr[0].bufferData + pos;
L
Linus Torvalds 已提交
1291 1292
	int i;

1293 1294
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
				   pos, offset, size, count);
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300
	snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);

	for (i=0; i < count; i++) {
#if K1212_DEBUG_LEVEL > 0
		if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
		     (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1301 1302
			printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
			       dst, i);
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
			return -EFAULT;
		}
#endif
		memset((void*) dst + offset, 0, size);
		dst++;
	}

	return 0;
}

1313
static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
L
Linus Torvalds 已提交
1314
{
1315
	struct KorgAudioFrame * src =  korg1212->recordDataBufsPtr[0].bufferData + pos;
L
Linus Torvalds 已提交
1316 1317
	int i, rc;

1318 1319
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
				   pos, offset, size);
L
Linus Torvalds 已提交
1320 1321 1322 1323 1324 1325
	snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);

	for (i=0; i < count; i++) {
#if K1212_DEBUG_LEVEL > 0
		if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
		     (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
1326
			printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
			return -EFAULT;
		}
#endif
		rc = copy_to_user(dst + offset, src, size);
		if (rc) {
			K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
			return -EFAULT;
		}
		src++;
		dst += size;
	}

	return 0;
}

1342
static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
L
Linus Torvalds 已提交
1343
{
1344
	struct KorgAudioFrame * dst =  korg1212->playDataBufsPtr[0].bufferData + pos;
L
Linus Torvalds 已提交
1345 1346
	int i, rc;

1347 1348
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
				   pos, offset, size, count);
L
Linus Torvalds 已提交
1349 1350 1351 1352 1353 1354 1355

	snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);

	for (i=0; i < count; i++) {
#if K1212_DEBUG_LEVEL > 0
		if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
		     (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1356
			printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
L
Linus Torvalds 已提交
1357 1358 1359 1360 1361
			return -EFAULT;
		}
#endif
		rc = copy_from_user((void*) dst + offset, src, size);
		if (rc) {
1362
			K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
L
Linus Torvalds 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371
			return -EFAULT;
		}
		dst++;
		src += size;
	}

	return 0;
}

1372
static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
L
Linus Torvalds 已提交
1373
{
1374
        struct snd_korg1212 *korg1212 = pcm->private_data;
L
Linus Torvalds 已提交
1375

1376 1377
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1378 1379 1380 1381

        korg1212->pcm = NULL;
}

1382
static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1383 1384
{
        unsigned long flags;
1385 1386
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
        struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1387

1388 1389
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412

        snd_pcm_set_sync(substream);    // ???

	snd_korg1212_OpenCard(korg1212);

        runtime->hw = snd_korg1212_playback_info;
	snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);

        spin_lock_irqsave(&korg1212->lock, flags);

        korg1212->playback_substream = substream;
	korg1212->playback_pid = current->pid;
        korg1212->periodsize = K1212_PERIODS;
	korg1212->channels = K1212_CHANNELS;
	korg1212->errorcnt = 0;

        spin_unlock_irqrestore(&korg1212->lock, flags);

        snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
        return 0;
}


1413
static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1414 1415
{
        unsigned long flags;
1416 1417
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
        struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1418

1419 1420
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

        snd_pcm_set_sync(substream);

	snd_korg1212_OpenCard(korg1212);

        runtime->hw = snd_korg1212_capture_info;
	snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);

        spin_lock_irqsave(&korg1212->lock, flags);

        korg1212->capture_substream = substream;
	korg1212->capture_pid = current->pid;
        korg1212->periodsize = K1212_PERIODS;
	korg1212->channels = K1212_CHANNELS;

        spin_unlock_irqrestore(&korg1212->lock, flags);

1438 1439
        snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
				     kPlayBufferFrames, kPlayBufferFrames);
L
Linus Torvalds 已提交
1440 1441 1442
        return 0;
}

1443
static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1444 1445
{
        unsigned long flags;
1446
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1447

1448 1449
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464

	snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);

        spin_lock_irqsave(&korg1212->lock, flags);

	korg1212->playback_pid = -1;
        korg1212->playback_substream = NULL;
        korg1212->periodsize = 0;

        spin_unlock_irqrestore(&korg1212->lock, flags);

	snd_korg1212_CloseCard(korg1212);
        return 0;
}

1465
static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1466 1467
{
        unsigned long flags;
1468
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1469

1470 1471
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484

        spin_lock_irqsave(&korg1212->lock, flags);

	korg1212->capture_pid = -1;
        korg1212->capture_substream = NULL;
        korg1212->periodsize = 0;

        spin_unlock_irqrestore(&korg1212->lock, flags);

	snd_korg1212_CloseCard(korg1212);
        return 0;
}

1485
static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1486 1487
			     unsigned int cmd, void *arg)
{
1488
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
L
Linus Torvalds 已提交
1489 1490

	if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
1491
		struct snd_pcm_channel_info *info = arg;
L
Linus Torvalds 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
        	info->offset = 0;
        	info->first = info->channel * 16;
        	info->step = 256;
		K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
		return 0;
	}

        return snd_pcm_lib_ioctl(substream, cmd, arg);
}

1502 1503
static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
                             struct snd_pcm_hw_params *params)
L
Linus Torvalds 已提交
1504 1505
{
        unsigned long flags;
1506
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1507 1508 1509 1510
        int err;
	pid_t this_pid;
	pid_t other_pid;

1511 1512
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553

        spin_lock_irqsave(&korg1212->lock, flags);

	if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		this_pid = korg1212->playback_pid;
		other_pid = korg1212->capture_pid;
	} else {
		this_pid = korg1212->capture_pid;
		other_pid = korg1212->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) != korg1212->clkRate) {
			spin_unlock_irqrestore(&korg1212->lock, flags);
			_snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
			return -EBUSY;
		}

        	spin_unlock_irqrestore(&korg1212->lock, flags);
	        return 0;
	}

        if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
                spin_unlock_irqrestore(&korg1212->lock, flags);
                return err;
        }

	korg1212->channels = params_channels(params);
        korg1212->periodsize = K1212_PERIOD_BYTES;

        spin_unlock_irqrestore(&korg1212->lock, flags);

        return 0;
}

1554
static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1555
{
1556
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1557 1558
	int rc;

1559 1560
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
			   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565

	spin_lock_irq(&korg1212->lock);

	/* FIXME: we should wait for ack! */
	if (korg1212->stop_pending_cnt > 0) {
1566 1567
		K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
				   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
        	spin_unlock_irq(&korg1212->lock);
		return -EAGAIN;
		/*
		korg1212->sharedBufferPtr->cardCommand = 0;
		del_timer(&korg1212->timer);
		korg1212->stop_pending_cnt = 0;
		*/
	}

        rc = snd_korg1212_SetupForPlay(korg1212);

        korg1212->currentBuffer = 0;

        spin_unlock_irq(&korg1212->lock);

	return rc ? -EINVAL : 0;
}

1586
static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1587 1588
                           int cmd)
{
1589
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1590 1591
	int rc;

1592 1593
	K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
			   stateName[korg1212->cardState], cmd);
L
Linus Torvalds 已提交
1594 1595 1596 1597 1598 1599

	spin_lock(&korg1212->lock);
        switch (cmd) {
                case SNDRV_PCM_TRIGGER_START:
/*
			if (korg1212->running) {
1600
				K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
L
Linus Torvalds 已提交
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
				break;
			}
*/
                        korg1212->running++;
                        rc = snd_korg1212_TriggerPlay(korg1212);
                        break;

                case SNDRV_PCM_TRIGGER_STOP:
/*
			if (!korg1212->running) {
1611
				K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
				break;
			}
*/
                        korg1212->running--;
                        rc = snd_korg1212_StopPlay(korg1212);
                        break;

                default:
			rc = 1;
			break;
        }
	spin_unlock(&korg1212->lock);
        return rc ? -EINVAL : 0;
}

1627
static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1628
{
1629
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1630 1631 1632 1633
        snd_pcm_uframes_t pos;

	pos = korg1212->currentBuffer * kPlayBufferFrames;

1634 1635
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n", 
				   stateName[korg1212->cardState], pos);
L
Linus Torvalds 已提交
1636 1637 1638 1639

        return pos;
}

1640
static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1641
{
1642
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1643 1644 1645 1646
        snd_pcm_uframes_t pos;

	pos = korg1212->currentBuffer * kPlayBufferFrames;

1647 1648
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
				   stateName[korg1212->cardState], pos);
L
Linus Torvalds 已提交
1649 1650 1651 1652

        return pos;
}

1653
static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1654 1655 1656 1657 1658
                        int channel, /* not used (interleaved data) */
                        snd_pcm_uframes_t pos,
                        void __user *src,
                        snd_pcm_uframes_t count)
{
1659
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1660

1661 1662
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
				   stateName[korg1212->cardState], pos, count);
L
Linus Torvalds 已提交
1663 1664 1665 1666 1667
 
	return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);

}

1668
static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1669 1670 1671 1672
                           int channel, /* not used (interleaved data) */
                           snd_pcm_uframes_t pos,
                           snd_pcm_uframes_t count)
{
1673
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1674

1675 1676
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
				   stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
1677 1678 1679 1680

	return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
}

1681
static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686
                        int channel, /* not used (interleaved data) */
                        snd_pcm_uframes_t pos,
                        void __user *dst,
                        snd_pcm_uframes_t count)
{
1687
        struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1688

1689 1690
	K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
				   stateName[korg1212->cardState], pos, count);
L
Linus Torvalds 已提交
1691 1692 1693 1694

	return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
}

1695
static struct snd_pcm_ops snd_korg1212_playback_ops = {
L
Linus Torvalds 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
        .open =		snd_korg1212_playback_open,
        .close =	snd_korg1212_playback_close,
        .ioctl =	snd_korg1212_ioctl,
        .hw_params =	snd_korg1212_hw_params,
        .prepare =	snd_korg1212_prepare,
        .trigger =	snd_korg1212_trigger,
        .pointer =	snd_korg1212_playback_pointer,
        .copy =		snd_korg1212_playback_copy,
        .silence =	snd_korg1212_playback_silence,
};

1707
static struct snd_pcm_ops snd_korg1212_capture_ops = {
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
	.open =		snd_korg1212_capture_open,
	.close =	snd_korg1212_capture_close,
	.ioctl =	snd_korg1212_ioctl,
	.hw_params =	snd_korg1212_hw_params,
	.prepare =	snd_korg1212_prepare,
	.trigger =	snd_korg1212_trigger,
	.pointer =	snd_korg1212_capture_pointer,
	.copy =		snd_korg1212_capture_copy,
};

/*
 * Control Interface
 */

1722 1723
static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1724 1725 1726 1727 1728 1729
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
	return 0;
}

1730 1731
static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1732
{
1733
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
	int i = kcontrol->private_value;

	spin_lock_irq(&korg1212->lock);

        u->value.integer.value[0] = korg1212->volumePhase[i];

	if (i >= 8)
        	u->value.integer.value[1] = korg1212->volumePhase[i+1];

	spin_unlock_irq(&korg1212->lock);

        return 0;
}

1748 1749
static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1750
{
1751
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
        int change = 0;
        int i, val;

	spin_lock_irq(&korg1212->lock);

	i = kcontrol->private_value;

	korg1212->volumePhase[i] = u->value.integer.value[0];

	val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];

	if ((u->value.integer.value[0] > 0) != (val < 0)) {
		val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
		korg1212->sharedBufferPtr->volumeData[i] = val;
		change = 1;
	}

	if (i >= 8) {
		korg1212->volumePhase[i+1] = u->value.integer.value[1];

		val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];

		if ((u->value.integer.value[1] > 0) != (val < 0)) {
			val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
			korg1212->sharedBufferPtr->volumeData[i+1] = val;
			change = 1;
		}
	}

	spin_unlock_irq(&korg1212->lock);

        return change;
}

1786 1787
static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1788 1789 1790 1791 1792 1793 1794 1795
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
        uinfo->value.integer.min = k1212MinVolume;
	uinfo->value.integer.max = k1212MaxVolume;
        return 0;
}

1796 1797
static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1798
{
1799
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
        int i;

	spin_lock_irq(&korg1212->lock);

	i = kcontrol->private_value;
        u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);

	if (i >= 8) 
                u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);

        spin_unlock_irq(&korg1212->lock);

        return 0;
}

1815 1816
static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1817
{
1818
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
        int change = 0;
        int i;
	int val;

	spin_lock_irq(&korg1212->lock);

	i = kcontrol->private_value;

	if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
		val = korg1212->volumePhase[i] > 0 ? -1 : 1;
		val *= u->value.integer.value[0];
		korg1212->sharedBufferPtr->volumeData[i] = val;
		change = 1;
	}

	if (i >= 8) {
		if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
			val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
			val *= u->value.integer.value[1];
			korg1212->sharedBufferPtr->volumeData[i+1] = val;
			change = 1;
		}
	}

	spin_unlock_irq(&korg1212->lock);

        return change;
}

1848 1849
static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
	uinfo->value.enumerated.items = kAudioChannels;
	if (uinfo->value.enumerated.item > kAudioChannels-1) {
		uinfo->value.enumerated.item = kAudioChannels-1;
	}
	strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
	return 0;
}

1861 1862
static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1863
{
1864
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
        int i;

	spin_lock_irq(&korg1212->lock);

	i = kcontrol->private_value;
	u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];

	if (i >= 8) 
		u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];

        spin_unlock_irq(&korg1212->lock);

        return 0;
}

1880 1881
static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1882
{
1883
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
        int change = 0, i;

	spin_lock_irq(&korg1212->lock);

	i = kcontrol->private_value;

	if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
		korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
		change = 1;
	}

	if (i >= 8) {
		if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
			korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
			change = 1;
		}
	}

	spin_unlock_irq(&korg1212->lock);

        return change;
}

1907 1908
static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1909 1910 1911 1912 1913 1914 1915 1916
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 2;
        uinfo->value.integer.min = k1212MaxADCSens;
	uinfo->value.integer.max = k1212MinADCSens;
        return 0;
}

1917 1918
static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1919
{
1920
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931

	spin_lock_irq(&korg1212->lock);

        u->value.integer.value[0] = korg1212->leftADCInSens;
        u->value.integer.value[1] = korg1212->rightADCInSens;

	spin_unlock_irq(&korg1212->lock);

        return 0;
}

1932 1933
static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_value *u)
L
Linus Torvalds 已提交
1934
{
1935
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
        int change = 0;

	spin_lock_irq(&korg1212->lock);

        if (u->value.integer.value[0] != korg1212->leftADCInSens) {
                korg1212->leftADCInSens = u->value.integer.value[0];
                change = 1;
        }
        if (u->value.integer.value[1] != korg1212->rightADCInSens) {
                korg1212->rightADCInSens = u->value.integer.value[1];
                change = 1;
        }

	spin_unlock_irq(&korg1212->lock);

        if (change)
                snd_korg1212_WriteADCSensitivity(korg1212);

        return change;
}

1957 1958
static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item > 2) {
		uinfo->value.enumerated.item = 2;
	}
	strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
	return 0;
}

1970 1971
static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1972
{
1973
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1974 1975 1976 1977 1978 1979 1980 1981 1982

	spin_lock_irq(&korg1212->lock);

	ucontrol->value.enumerated.item[0] = korg1212->clkSource;

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

1983 1984
static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1985
{
1986
	struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
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
	unsigned int val;
	int change;

	val = ucontrol->value.enumerated.item[0] % 3;
	spin_lock_irq(&korg1212->lock);
	change = val != korg1212->clkSource;
        snd_korg1212_SetClockSource(korg1212, val);
	spin_unlock_irq(&korg1212->lock);
	return change;
}

#define MON_MIXER(ord,c_name)									\
        {											\
                .access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,	\
                .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,					\
                .name =		c_name " Monitor Volume",					\
                .info =		snd_korg1212_control_volume_info,				\
                .get =		snd_korg1212_control_volume_get,				\
                .put =		snd_korg1212_control_volume_put,				\
		.private_value = ord,								\
        },                                                                                      \
        {											\
                .access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,	\
                .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,					\
                .name =		c_name " Monitor Route",					\
                .info =		snd_korg1212_control_route_info,				\
                .get =		snd_korg1212_control_route_get,					\
                .put =		snd_korg1212_control_route_put,					\
		.private_value = ord,								\
        },                                                                                      \
        {											\
                .access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,	\
2019
                .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,					\
L
Linus Torvalds 已提交
2020 2021 2022 2023 2024 2025 2026
                .name =		c_name " Monitor Phase Invert",					\
                .info =		snd_korg1212_control_phase_info,				\
                .get =		snd_korg1212_control_phase_get,					\
                .put =		snd_korg1212_control_phase_put,					\
		.private_value = ord,								\
        }

2027
static struct snd_kcontrol_new snd_korg1212_controls[] = {
L
Linus Torvalds 已提交
2028 2029 2030 2031 2032 2033
        MON_MIXER(8, "Analog"),
	MON_MIXER(10, "SPDIF"), 
        MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
        MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
	{
                .access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2034
                .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
                .name =		"Sync Source",
                .info =		snd_korg1212_control_sync_info,
                .get =		snd_korg1212_control_sync_get,
                .put =		snd_korg1212_control_sync_put,
        },
        {
                .access =	SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
                .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
                .name =		"ADC Attenuation",
                .info =		snd_korg1212_control_info,
                .get =		snd_korg1212_control_get,
                .put =		snd_korg1212_control_put,
        }
};

/*
 * proc interface
 */

2054 2055
static void snd_korg1212_proc_read(struct snd_info_entry *entry,
				   struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
2056 2057
{
	int n;
2058
	struct snd_korg1212 *korg1212 = entry->private_data;
L
Linus Torvalds 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081

	snd_iprintf(buffer, korg1212->card->longname);
	snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
	snd_iprintf(buffer, "\nGeneral settings\n");
	snd_iprintf(buffer, "    period size: %Zd bytes\n", K1212_PERIOD_BYTES);
	snd_iprintf(buffer, "     clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
	snd_iprintf(buffer, "  left ADC Sens: %d\n", korg1212->leftADCInSens );
	snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
        snd_iprintf(buffer, "    Volume Info:\n");
        for (n=0; n<kAudioChannels; n++)
                snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
                                    channelName[n],
                                    channelName[korg1212->sharedBufferPtr->routeData[n]],
                                    korg1212->sharedBufferPtr->volumeData[n]);
	snd_iprintf(buffer, "\nGeneral status\n");
        snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
        snd_iprintf(buffer, "     Card State: %s\n", stateName[korg1212->cardState]);
        snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
        snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
        snd_iprintf(buffer, "      Irq count: %ld\n", korg1212->irqcount);
        snd_iprintf(buffer, "    Error count: %ld\n", korg1212->totalerrorcnt);
}

2082
static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
2083
{
2084
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
2085 2086

	if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2087
		snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
L
Linus Torvalds 已提交
2088 2089 2090
}

static int
2091
snd_korg1212_free(struct snd_korg1212 *korg1212)
L
Linus Torvalds 已提交
2092 2093 2094 2095 2096 2097
{
        snd_korg1212_TurnOffIdleMonitor(korg1212);

        if (korg1212->irq >= 0) {
                synchronize_irq(korg1212->irq);                
                snd_korg1212_DisableCardInterrupts(korg1212);
2098
                free_irq(korg1212->irq, korg1212);
L
Linus Torvalds 已提交
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 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
                korg1212->irq = -1;
        }
        
        if (korg1212->iobase != NULL) {
                iounmap(korg1212->iobase);
                korg1212->iobase = NULL;
        }
        
	pci_release_regions(korg1212->pci);

        // ----------------------------------------------------
        // free up memory resources used for the DSP download.
        // ----------------------------------------------------
        if (korg1212->dma_dsp.area) {
        	snd_dma_free_pages(&korg1212->dma_dsp);
        	korg1212->dma_dsp.area = NULL;
        }

#ifndef K1212_LARGEALLOC

        // ------------------------------------------------------
        // free up memory resources used for the Play/Rec Buffers
        // ------------------------------------------------------
	if (korg1212->dma_play.area) {
		snd_dma_free_pages(&korg1212->dma_play);
		korg1212->dma_play.area = NULL;
        }

	if (korg1212->dma_rec.area) {
		snd_dma_free_pages(&korg1212->dma_rec);
		korg1212->dma_rec.area = NULL;
        }

#endif

        // ----------------------------------------------------
        // free up memory resources used for the Shared Buffers
        // ----------------------------------------------------
	if (korg1212->dma_shared.area) {
		snd_dma_free_pages(&korg1212->dma_shared);
		korg1212->dma_shared.area = NULL;
        }
        
	pci_disable_device(korg1212->pci);
        kfree(korg1212);
        return 0;
}

2147
static int snd_korg1212_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
2148
{
2149
        struct snd_korg1212 *korg1212 = device->device_data;
L
Linus Torvalds 已提交
2150 2151 2152 2153
        K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
	return snd_korg1212_free(korg1212);
}

2154 2155
static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
                                         struct snd_korg1212 ** rchip)
L
Linus Torvalds 已提交
2156 2157

{
2158
        int err, rc;
L
Linus Torvalds 已提交
2159 2160
        unsigned int i;
	unsigned ioport_size, iomem_size, iomem2_size;
2161
        struct snd_korg1212 * korg1212;
2162
	const struct firmware *dsp_code;
L
Linus Torvalds 已提交
2163

2164
        static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
2165 2166 2167 2168 2169 2170 2171
                .dev_free = snd_korg1212_dev_free,
        };

        * rchip = NULL;
        if ((err = pci_enable_device(pci)) < 0)
                return err;

2172
        korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
L
Linus Torvalds 已提交
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
        if (korg1212 == NULL) {
		pci_disable_device(pci);
                return -ENOMEM;
	}

	korg1212->card = card;
	korg1212->pci = pci;

        init_waitqueue_head(&korg1212->wait);
        spin_lock_init(&korg1212->lock);
2183
	mutex_init(&korg1212->open_mutex);
L
Linus Torvalds 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
	init_timer(&korg1212->timer);
	korg1212->timer.function = snd_korg1212_timer_func;
	korg1212->timer.data = (unsigned long)korg1212;

        korg1212->irq = -1;
        korg1212->clkSource = K1212_CLKIDX_Local;
        korg1212->clkRate = 44100;
        korg1212->inIRQ = 0;
        korg1212->running = 0;
	korg1212->opencnt = 0;
	korg1212->playcnt = 0;
	korg1212->setcnt = 0;
	korg1212->totalerrorcnt = 0;
	korg1212->playback_pid = -1;
	korg1212->capture_pid = -1;
        snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
        korg1212->idleMonitorOn = 0;
        korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
        korg1212->leftADCInSens = k1212MaxADCSens;
        korg1212->rightADCInSens = k1212MaxADCSens;

        for (i=0; i<kAudioChannels; i++)
                korg1212->volumePhase[i] = 0;

	if ((err = pci_request_regions(pci, "korg1212")) < 0) {
		kfree(korg1212);
		pci_disable_device(pci);
		return err;
	}

        korg1212->iomem = pci_resource_start(korg1212->pci, 0);
        korg1212->ioport = pci_resource_start(korg1212->pci, 1);
        korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);

	iomem_size = pci_resource_len(korg1212->pci, 0);
	ioport_size = pci_resource_len(korg1212->pci, 1);
	iomem2_size = pci_resource_len(korg1212->pci, 2);

        K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
                   "    iomem = 0x%lx (%d)\n"
		   "    ioport  = 0x%lx (%d)\n"
                   "    iomem = 0x%lx (%d)\n"
		   "    [%s]\n",
		   korg1212->iomem, iomem_size,
		   korg1212->ioport, ioport_size,
		   korg1212->iomem2, iomem2_size,
		   stateName[korg1212->cardState]);

        if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
		snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
                           korg1212->iomem + iomem_size - 1);
                snd_korg1212_free(korg1212);
                return -EBUSY;
        }

        err = request_irq(pci->irq, snd_korg1212_interrupt,
2240
                          IRQF_SHARED,
2241
                          "korg1212", korg1212);
L
Linus Torvalds 已提交
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288

        if (err) {
		snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
                snd_korg1212_free(korg1212);
                return -EBUSY;
        }

        korg1212->irq = pci->irq;

	pci_set_master(korg1212->pci);

        korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
        korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
        korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
        korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
        korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
        korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
        korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
        korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
        korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
        korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);

        K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
                   "    Status register = 0x%p\n"
                   "    OutDoorbell     = 0x%p\n"
                   "    InDoorbell      = 0x%p\n"
                   "    Mailbox0        = 0x%p\n"
                   "    Mailbox1        = 0x%p\n"
                   "    Mailbox2        = 0x%p\n"
                   "    Mailbox3        = 0x%p\n"
                   "    ControlReg      = 0x%p\n"
                   "    SensReg         = 0x%p\n"
                   "    IDReg           = 0x%p\n"
		   "    [%s]\n",
                   korg1212->statusRegPtr,
		   korg1212->outDoorbellPtr,
		   korg1212->inDoorbellPtr,
                   korg1212->mailbox0Ptr,
                   korg1212->mailbox1Ptr,
                   korg1212->mailbox2Ptr,
                   korg1212->mailbox3Ptr,
                   korg1212->controlRegPtr,
                   korg1212->sensRegPtr,
                   korg1212->idRegPtr,
		   stateName[korg1212->cardState]);

	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2289 2290
				sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
		snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
L
Linus Torvalds 已提交
2291 2292 2293
                snd_korg1212_free(korg1212);
                return -ENOMEM;
        }
2294
        korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
L
Linus Torvalds 已提交
2295 2296
        korg1212->sharedBufferPhy = korg1212->dma_shared.addr;

2297
        K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
L
Linus Torvalds 已提交
2298 2299 2300

#ifndef K1212_LARGEALLOC

2301
        korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
L
Linus Torvalds 已提交
2302 2303 2304 2305 2306 2307 2308

	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
				korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
		snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
                snd_korg1212_free(korg1212);
                return -ENOMEM;
        }
2309
	korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
L
Linus Torvalds 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
	korg1212->PlayDataPhy = korg1212->dma_play.addr;

        K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
		korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);

	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
				korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
		snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
                snd_korg1212_free(korg1212);
                return -ENOMEM;
        }
2321
        korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
L
Linus Torvalds 已提交
2322 2323 2324 2325 2326 2327 2328 2329 2330
        korg1212->RecDataPhy = korg1212->dma_rec.addr;

        K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
		korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);

#else // K1212_LARGEALLOC

        korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
        korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
2331 2332
        korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
        korg1212->RecDataPhy  = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
L
Linus Torvalds 已提交
2333 2334 2335 2336

#endif // K1212_LARGEALLOC

        korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
2337
		offsetof(struct KorgSharedBuffer, volumeData);
L
Linus Torvalds 已提交
2338
        korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
2339
		offsetof(struct KorgSharedBuffer, routeData);
L
Linus Torvalds 已提交
2340
        korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
2341
		offsetof(struct KorgSharedBuffer, AdatTimeCode);
L
Linus Torvalds 已提交
2342

2343 2344 2345
	err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
	if (err < 0) {
		release_firmware(dsp_code);
2346
#ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2347 2348 2349 2350 2351 2352 2353 2354
		dsp_code = &static_dsp_code;
#else
		snd_printk(KERN_ERR "firmware not available\n");
		snd_korg1212_free(korg1212);
		return err;
#endif
	}

L
Linus Torvalds 已提交
2355
	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2356
				dsp_code->size, &korg1212->dma_dsp) < 0) {
2357
		snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
L
Linus Torvalds 已提交
2358
                snd_korg1212_free(korg1212);
2359
#ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2360 2361 2362
		if (dsp_code != &static_dsp_code)
#endif
			release_firmware(dsp_code);
L
Linus Torvalds 已提交
2363 2364 2365 2366
                return -ENOMEM;
        }

        K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2367
		   korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
L
Linus Torvalds 已提交
2368 2369
		   stateName[korg1212->cardState]);

2370 2371
	memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);

2372
#ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2373 2374 2375 2376
	if (dsp_code != &static_dsp_code)
#endif
		release_firmware(dsp_code);

L
Linus Torvalds 已提交
2377 2378
	rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);

2379 2380
	if (rc)
		K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
L
Linus Torvalds 已提交
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393

        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
                snd_korg1212_free(korg1212);
                return err;
        }
        
	snd_korg1212_EnableCardInterrupts(korg1212);

	mdelay(CARD_BOOT_DELAY_IN_MS);

        if (snd_korg1212_downloadDSPCode(korg1212))
        	return -EBUSY;

2394
        K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
L
Linus Torvalds 已提交
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
               "PlayDataPhy = %08x L[%08x]\n"
	       "korg1212: RecDataPhy = %08x L[%08x], "
               "VolumeTablePhy = %08x L[%08x]\n"
               "korg1212: RoutingTablePhy = %08x L[%08x], "
               "AdatTimeCodePhy = %08x L[%08x]\n",
	       (int)korg1212->dma_dsp.addr,    UpperWordSwap(korg1212->dma_dsp.addr),
               korg1212->PlayDataPhy,     LowerWordSwap(korg1212->PlayDataPhy),
               korg1212->RecDataPhy,      LowerWordSwap(korg1212->RecDataPhy),
               korg1212->VolumeTablePhy,  LowerWordSwap(korg1212->VolumeTablePhy),
               korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
               korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));

        if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
                return err;

	korg1212->pcm->private_data = korg1212;
        korg1212->pcm->private_free = snd_korg1212_free_pcm;
        strcpy(korg1212->pcm->name, "korg1212");

        snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
        
	snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);

	korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;

        for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
                err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
                if (err < 0)
                        return err;
        }

        snd_korg1212_proc_init(korg1212);
        
	snd_card_set_dev(card, &pci->dev);

        * rchip = korg1212;
	return 0;

}

/*
 * Card initialisation
 */

static int __devinit
snd_korg1212_probe(struct pci_dev *pci,
		const struct pci_device_id *pci_id)
{
	static int dev;
2444 2445
	struct snd_korg1212 *korg1212;
	struct snd_card *card;
L
Linus Torvalds 已提交
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 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
	int err;

	if (dev >= SNDRV_CARDS) {
		return -ENODEV;
	}
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}
	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
        if (card == NULL)
		return -ENOMEM;

        if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
		snd_card_free(card);
		return err;
	}

	strcpy(card->driver, "korg1212");
	strcpy(card->shortname, "korg1212");
	sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
		korg1212->iomem, korg1212->irq);

        K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);

	if ((err = snd_card_register(card)) < 0) {
		snd_card_free(card);
		return err;
	}
	pci_set_drvdata(pci, card);
	dev++;
	return 0;
}

static void __devexit snd_korg1212_remove(struct pci_dev *pci)
{
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
}

static struct pci_driver driver = {
	.name = "korg1212",
	.id_table = snd_korg1212_ids,
	.probe = snd_korg1212_probe,
	.remove = __devexit_p(snd_korg1212_remove),
};

static int __init alsa_card_korg1212_init(void)
{
2495
	return pci_register_driver(&driver);
L
Linus Torvalds 已提交
2496 2497 2498 2499 2500 2501 2502 2503 2504
}

static void __exit alsa_card_korg1212_exit(void)
{
	pci_unregister_driver(&driver);
}

module_init(alsa_card_korg1212_init)
module_exit(alsa_card_korg1212_exit)