[ALSA] hda-intel - Disable INTX when MSI is used
[linux-2.6-block.git] / sound / pci / korg1212 / korg1212.c
CommitLineData
1da177e4
LT
1/*
2 * Driver for the Korg 1212 IO PCI card
3 *
4 * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/slab.h>
28#include <linux/wait.h>
29#include <linux/moduleparam.h>
62932df8 30#include <linux/mutex.h>
2493a6d1 31#include <linux/firmware.h>
1da177e4
LT
32
33#include <sound/core.h>
34#include <sound/info.h>
35#include <sound/control.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/initval.h>
39
40#include <asm/io.h>
41
42// ----------------------------------------------------------------------------
43// Debug Stuff
44// ----------------------------------------------------------------------------
45#define K1212_DEBUG_LEVEL 0
9fd9156c
TI
46#if K1212_DEBUG_LEVEL > 0
47#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
48#else
49#define K1212_DEBUG_PRINTK(fmt,...)
50#endif
51#if K1212_DEBUG_LEVEL > 1
52#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
53#else
54#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
55#endif
1da177e4
LT
56
57// ----------------------------------------------------------------------------
58// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
59// buffers are alocated as a large piece inside KorgSharedBuffer.
60// ----------------------------------------------------------------------------
61//#define K1212_LARGEALLOC 1
62
63// ----------------------------------------------------------------------------
64// Valid states of the Korg 1212 I/O card.
65// ----------------------------------------------------------------------------
fcfd3332 66enum CardState {
1da177e4
LT
67 K1212_STATE_NONEXISTENT, // there is no card here
68 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
69 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
70 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
71 K1212_STATE_READY, // the card can be opened by an application. Any application
72 // requests prior to this state should fail. Only an open
73 // request can be made at this state.
74 K1212_STATE_OPEN, // an application has opened the card
75 K1212_STATE_SETUP, // the card has been setup for play
76 K1212_STATE_PLAYING, // the card is playing
77 K1212_STATE_MONITOR, // the card is in the monitor mode
78 K1212_STATE_CALIBRATING, // the card is currently calibrating
79 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
80 // are in the process of cleaning things up.
81 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
fcfd3332 82};
1da177e4
LT
83
84// ----------------------------------------------------------------------------
85// The following enumeration defines the constants written to the card's
86// host-to-card doorbell to initiate a command.
87// ----------------------------------------------------------------------------
fcfd3332 88enum korg1212_dbcnst {
1da177e4
LT
89 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
90 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
91 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
92 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
93 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
94 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
95 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
96 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
97 // timecode value.
98 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
99 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
100 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
101 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
102 // on page 4 (local page to card).
103 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
104 // completed.
105 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
fcfd3332 106};
1da177e4
LT
107
108
109// ----------------------------------------------------------------------------
110// The following enumeration defines return codes
111// to the Korg 1212 I/O driver.
112// ----------------------------------------------------------------------------
fcfd3332 113enum snd_korg1212rc {
1da177e4
LT
114 K1212_CMDRET_Success = 0, // command was successfully placed
115 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
116 K1212_CMDRET_PMFailure, // the protected mode call failed
117 K1212_CMDRET_FailUnspecified, // unspecified failure
118 K1212_CMDRET_FailBadState, // the specified command can not be given in
119 // the card's current state. (or the wave device's
120 // state)
121 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
122 K1212_CMDRET_BadIndex, // an out of range card index was specified
123 K1212_CMDRET_BadHandle, // an invalid card handle was specified
124 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
125 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
126 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
127 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
128
129 K1212_CMDRET_BadDevice, // the specified wave device was out of range
130 K1212_CMDRET_BadFormat // the specified wave format is unsupported
fcfd3332 131};
1da177e4
LT
132
133// ----------------------------------------------------------------------------
134// The following enumeration defines the constants used to select the play
135// mode for the card in the SelectPlayMode command.
136// ----------------------------------------------------------------------------
fcfd3332 137enum PlayModeSelector {
1da177e4
LT
138 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
139 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
140 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
141 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
fcfd3332 142};
1da177e4
LT
143
144// ----------------------------------------------------------------------------
145// The following enumeration defines the constants used to select the monitor
146// mode for the card in the SetMonitorMode command.
147// ----------------------------------------------------------------------------
fcfd3332 148enum MonitorModeSelector {
1da177e4
LT
149 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
150 K1212_MONMODE_On // tells card to turn on monitor mode
fcfd3332 151};
1da177e4
LT
152
153#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
154#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
155#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
156#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
157#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
158#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
159#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
160#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
161 // register
162#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
163 // this is the upper word of the PCI control reg.
164#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
165
166#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
167 // from the card after sending a command.
168#define INTERCOMMAND_DELAY 40
169#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
170 // to send a command before giving up.
171#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
172 // the card.
173#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
174
175#define CARD_BOOT_DELAY_IN_MS 10
176#define CARD_BOOT_TIMEOUT 10
177#define DSP_BOOT_DELAY_IN_MS 200
178
179#define kNumBuffers 8
180#define k1212MaxCards 4
181#define k1212NumWaveDevices 6
182#define k16BitChannels 10
183#define k32BitChannels 2
184#define kAudioChannels (k16BitChannels + k32BitChannels)
185#define kPlayBufferFrames 1024
186
187#define K1212_ANALOG_CHANNELS 2
188#define K1212_SPDIF_CHANNELS 2
189#define K1212_ADAT_CHANNELS 8
190#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
191#define K1212_MIN_CHANNELS 1
192#define K1212_MAX_CHANNELS K1212_CHANNELS
fcfd3332 193#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
1da177e4
LT
194#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
195#define K1212_PERIODS (kNumBuffers)
196#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
197#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
198#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
199#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
200#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
201#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
202
203#define k1212MinADCSens 0x7f
204#define k1212MaxADCSens 0x00
205#define k1212MaxVolume 0x7fff
206#define k1212MaxWaveVolume 0xffff
207#define k1212MinVolume 0x0000
208#define k1212MaxVolInverted 0x8000
209
210// -----------------------------------------------------------------
211// the following bits are used for controlling interrupts in the
212// interrupt control/status reg
213// -----------------------------------------------------------------
214#define PCI_INT_ENABLE_BIT 0x00000100
215#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
216#define LOCAL_INT_ENABLE_BIT 0x00010000
217#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
218#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
219
220// -----------------------------------------------------------------
221// the following bits are defined for the PCI command register
222// -----------------------------------------------------------------
223#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
224#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
225#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
226
227// -----------------------------------------------------------------
228// the following bits are defined for the PCI status register
229// -----------------------------------------------------------------
230#define PCI_STAT_PARITY_ERROR_BIT 0x8000
231#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
232#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
233#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
234#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
235
236// ------------------------------------------------------------------------
237// the following constants are used in setting the 1212 I/O card's input
238// sensitivity.
239// ------------------------------------------------------------------------
240#define SET_SENS_LOCALINIT_BITPOS 15
241#define SET_SENS_DATA_BITPOS 10
242#define SET_SENS_CLOCK_BITPOS 8
243#define SET_SENS_LOADSHIFT_BITPOS 0
244
245#define SET_SENS_LEFTCHANID 0x00
246#define SET_SENS_RIGHTCHANID 0x01
247
248#define K1212SENSUPDATE_DELAY_IN_MS 50
249
250// --------------------------------------------------------------------------
251// WaitRTCTicks
252//
253// This function waits the specified number of real time clock ticks.
254// According to the DDK, each tick is ~0.8 microseconds.
255// The defines following the function declaration can be used for the
256// numTicksToWait parameter.
257// --------------------------------------------------------------------------
258#define ONE_RTC_TICK 1
259#define SENSCLKPULSE_WIDTH 4
260#define LOADSHIFT_DELAY 4
261#define INTERCOMMAND_DELAY 40
262#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
263 // the command register. (could be up to 180 us)
264#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
265 // from the card after sending a command.
266
2493a6d1
CL
267#define FIRMWARE_IN_THE_KERNEL
268
269#ifdef FIRMWARE_IN_THE_KERNEL
1da177e4 270#include "korg1212-firmware.h"
2493a6d1
CL
271static const struct firmware static_dsp_code = {
272 .data = (u8 *)dspCode,
273 .size = sizeof dspCode
274};
275#endif
1da177e4 276
fcfd3332 277enum ClockSourceIndex {
1da177e4
LT
278 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
279 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
280 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
281 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
282 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
283 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
284 K1212_CLKIDX_Invalid // used to check validity of the index
fcfd3332 285};
1da177e4 286
fcfd3332 287enum ClockSourceType {
1da177e4
LT
288 K1212_CLKIDX_Adat = 0, // selects source as ADAT
289 K1212_CLKIDX_Word, // selects source as S/PDIF
290 K1212_CLKIDX_Local // selects source as local clock
fcfd3332 291};
1da177e4 292
fcfd3332
TI
293struct KorgAudioFrame {
294 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
295 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
296 u32 timeCodeVal; /* holds the ADAT timecode value */
297};
1da177e4 298
fcfd3332
TI
299struct KorgAudioBuffer {
300 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
301};
1da177e4 302
fcfd3332 303struct KorgSharedBuffer {
1da177e4 304#ifdef K1212_LARGEALLOC
fcfd3332
TI
305 struct KorgAudioBuffer playDataBufs[kNumBuffers];
306 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
1da177e4
LT
307#endif
308 short volumeData[kAudioChannels];
309 u32 cardCommand;
310 u16 routeData [kAudioChannels];
311 u32 AdatTimeCode; // ADAT timecode value
fcfd3332 312};
1da177e4 313
fcfd3332 314struct SensBits {
1da177e4
LT
315 union {
316 struct {
317 unsigned int leftChanVal:8;
318 unsigned int leftChanId:8;
319 } v;
320 u16 leftSensBits;
321 } l;
322 union {
323 struct {
324 unsigned int rightChanVal:8;
325 unsigned int rightChanId:8;
326 } v;
327 u16 rightSensBits;
328 } r;
fcfd3332 329};
1da177e4 330
fcfd3332
TI
331struct snd_korg1212 {
332 struct snd_card *card;
1da177e4 333 struct pci_dev *pci;
fcfd3332 334 struct snd_pcm *pcm;
1da177e4
LT
335 int irq;
336
337 spinlock_t lock;
62932df8 338 struct mutex open_mutex;
1da177e4
LT
339
340 struct timer_list timer; /* timer callback for checking ack of stop request */
341 int stop_pending_cnt; /* counter for stop pending check */
342
343 wait_queue_head_t wait;
344
345 unsigned long iomem;
346 unsigned long ioport;
347 unsigned long iomem2;
348 unsigned long irqcount;
349 unsigned long inIRQ;
350 void __iomem *iobase;
351
352 struct snd_dma_buffer dma_dsp;
353 struct snd_dma_buffer dma_play;
354 struct snd_dma_buffer dma_rec;
355 struct snd_dma_buffer dma_shared;
356
1da177e4
LT
357 u32 DataBufsSize;
358
fcfd3332
TI
359 struct KorgAudioBuffer * playDataBufsPtr;
360 struct KorgAudioBuffer * recordDataBufsPtr;
1da177e4 361
fcfd3332 362 struct KorgSharedBuffer * sharedBufferPtr;
1da177e4
LT
363
364 u32 RecDataPhy;
365 u32 PlayDataPhy;
366 unsigned long sharedBufferPhy;
367 u32 VolumeTablePhy;
368 u32 RoutingTablePhy;
369 u32 AdatTimeCodePhy;
370
371 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
372 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
373 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
374 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
375 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
376 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
377 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
378 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
379 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
380 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
381
382 size_t periodsize;
383 int channels;
384 int currentBuffer;
385
fcfd3332
TI
386 struct snd_pcm_substream *playback_substream;
387 struct snd_pcm_substream *capture_substream;
1da177e4
LT
388
389 pid_t capture_pid;
390 pid_t playback_pid;
391
fcfd3332 392 enum CardState cardState;
1da177e4
LT
393 int running;
394 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
395 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
396
fcfd3332 397 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
1da177e4 398
fcfd3332 399 enum ClockSourceType clkSource; // clock source
1da177e4
LT
400 int clkRate; // clock rate
401
402 int volumePhase[kAudioChannels];
403
404 u16 leftADCInSens; // ADC left channel input sensitivity
405 u16 rightADCInSens; // ADC right channel input sensitivity
406
407 int opencnt; // Open/Close count
408 int setcnt; // SetupForPlay count
409 int playcnt; // TriggerPlay count
410 int errorcnt; // Error Count
411 unsigned long totalerrorcnt; // Total Error Count
412
413 int dsp_is_loaded;
414 int dsp_stop_is_processed;
415
416};
417
418MODULE_DESCRIPTION("korg1212");
419MODULE_LICENSE("GPL");
420MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
421
422static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
423static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
424static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
425
426module_param_array(index, int, NULL, 0444);
427MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
428module_param_array(id, charp, NULL, 0444);
429MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
430module_param_array(enable, bool, NULL, 0444);
431MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
432MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
433
f40b6890 434static struct pci_device_id snd_korg1212_ids[] = {
1da177e4
LT
435 {
436 .vendor = 0x10b5,
437 .device = 0x906d,
438 .subvendor = PCI_ANY_ID,
439 .subdevice = PCI_ANY_ID,
440 },
441 { 0, },
442};
443
fcfd3332
TI
444MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
445
9fd9156c
TI
446static char *stateName[] = {
447 "Non-existent",
448 "Uninitialized",
449 "DSP download in process",
450 "DSP download complete",
451 "Ready",
452 "Open",
453 "Setup for play",
454 "Playing",
455 "Monitor mode on",
456 "Calibrating",
457 "Invalid"
1da177e4
LT
458};
459
9fd9156c 460static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
1da177e4 461
9fd9156c
TI
462static char *clockSourceName[] = {
463 "ADAT at 44.1 kHz",
464 "ADAT at 48 kHz",
465 "S/PDIF at 44.1 kHz",
466 "S/PDIF at 48 kHz",
467 "local clock at 44.1 kHz",
468 "local clock at 48 kHz"
1da177e4
LT
469};
470
9fd9156c
TI
471static char *channelName[] = {
472 "ADAT-1",
473 "ADAT-2",
474 "ADAT-3",
475 "ADAT-4",
476 "ADAT-5",
477 "ADAT-6",
478 "ADAT-7",
479 "ADAT-8",
480 "Analog-L",
481 "Analog-R",
482 "SPDIF-L",
483 "SPDIF-R",
1da177e4
LT
484};
485
9fd9156c
TI
486static u16 ClockSourceSelector[] = {
487 0x8000, // selects source as ADAT at 44.1 kHz
488 0x0000, // selects source as ADAT at 48 kHz
489 0x8001, // selects source as S/PDIF at 44.1 kHz
490 0x0001, // selects source as S/PDIF at 48 kHz
491 0x8002, // selects source as local clock at 44.1 kHz
492 0x0002 // selects source as local clock at 48 kHz
493};
1da177e4 494
fcfd3332 495union swap_u32 { unsigned char c[4]; u32 i; };
1da177e4
LT
496
497#ifdef SNDRV_BIG_ENDIAN
498static u32 LowerWordSwap(u32 swappee)
499#else
500static u32 UpperWordSwap(u32 swappee)
501#endif
502{
fcfd3332 503 union swap_u32 retVal, swapper;
1da177e4
LT
504
505 swapper.i = swappee;
506 retVal.c[2] = swapper.c[3];
507 retVal.c[3] = swapper.c[2];
508 retVal.c[1] = swapper.c[1];
509 retVal.c[0] = swapper.c[0];
510
511 return retVal.i;
512}
513
514#ifdef SNDRV_BIG_ENDIAN
515static u32 UpperWordSwap(u32 swappee)
516#else
517static u32 LowerWordSwap(u32 swappee)
518#endif
519{
fcfd3332 520 union swap_u32 retVal, swapper;
1da177e4
LT
521
522 swapper.i = swappee;
523 retVal.c[2] = swapper.c[2];
524 retVal.c[3] = swapper.c[3];
525 retVal.c[1] = swapper.c[0];
526 retVal.c[0] = swapper.c[1];
527
528 return retVal.i;
529}
530
1da177e4
LT
531#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
532#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
533#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
534#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
535
fcfd3332
TI
536static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
537 enum korg1212_dbcnst doorbellVal,
538 u32 mailBox0Val, u32 mailBox1Val,
539 u32 mailBox2Val, u32 mailBox3Val)
1da177e4
LT
540{
541 u32 retryCount;
542 u16 mailBox3Lo;
fcfd3332 543 int rc = K1212_CMDRET_Success;
1da177e4
LT
544
545 if (!korg1212->outDoorbellPtr) {
9fd9156c 546 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
1da177e4
LT
547 return K1212_CMDRET_CardUninitialized;
548 }
549
9fd9156c
TI
550 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
551 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
1da177e4
LT
552 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
553 writel(mailBox3Val, korg1212->mailbox3Ptr);
554 writel(mailBox2Val, korg1212->mailbox2Ptr);
555 writel(mailBox1Val, korg1212->mailbox1Ptr);
556 writel(mailBox0Val, korg1212->mailbox0Ptr);
557 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
558
559 // --------------------------------------------------------------
560 // the reboot command will not give an acknowledgement.
561 // --------------------------------------------------------------
562 if ( doorbellVal == K1212_DB_RebootCard ||
563 doorbellVal == K1212_DB_BootFromDSPPage4 ||
564 doorbellVal == K1212_DB_StartDSPDownload ) {
565 rc = K1212_CMDRET_Success;
566 break;
567 }
568
569 // --------------------------------------------------------------
570 // See if the card acknowledged the command. Wait a bit, then
571 // read in the low word of mailbox3. If the MSB is set and the
572 // low byte is equal to the doorbell value, then it ack'd.
573 // --------------------------------------------------------------
574 udelay(COMMAND_ACK_DELAY);
575 mailBox3Lo = readl(korg1212->mailbox3Ptr);
576 if (mailBox3Lo & COMMAND_ACK_MASK) {
577 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
9fd9156c 578 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
1da177e4
LT
579 rc = K1212_CMDRET_Success;
580 break;
581 }
582 }
583 }
584 korg1212->cmdRetryCount += retryCount;
585
586 if (retryCount >= MAX_COMMAND_RETRIES) {
9fd9156c 587 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
1da177e4
LT
588 rc = K1212_CMDRET_NoAckFromCard;
589 }
590
591 return rc;
592}
593
594/* spinlock already held */
fcfd3332 595static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
1da177e4
LT
596{
597 if (! korg1212->stop_pending_cnt) {
598 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
599 /* program the timer */
600 korg1212->stop_pending_cnt = HZ;
601 korg1212->timer.expires = jiffies + 1;
602 add_timer(&korg1212->timer);
603 }
604}
605
fcfd3332 606static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
1da177e4
LT
607{
608 unsigned long flags;
609 spin_lock_irqsave(&korg1212->lock, flags);
610 korg1212->dsp_stop_is_processed = 0;
611 snd_korg1212_SendStop(korg1212);
612 spin_unlock_irqrestore(&korg1212->lock, flags);
613 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
614}
615
616/* timer callback for checking the ack of stop request */
617static void snd_korg1212_timer_func(unsigned long data)
618{
fcfd3332 619 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
b32425ac 620 unsigned long flags;
1da177e4 621
b32425ac 622 spin_lock_irqsave(&korg1212->lock, flags);
1da177e4
LT
623 if (korg1212->sharedBufferPtr->cardCommand == 0) {
624 /* ack'ed */
625 korg1212->stop_pending_cnt = 0;
626 korg1212->dsp_stop_is_processed = 1;
627 wake_up(&korg1212->wait);
9fd9156c
TI
628 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
629 stateName[korg1212->cardState]);
1da177e4
LT
630 } else {
631 if (--korg1212->stop_pending_cnt > 0) {
632 /* reprogram timer */
633 korg1212->timer.expires = jiffies + 1;
634 add_timer(&korg1212->timer);
635 } else {
636 snd_printd("korg1212_timer_func timeout\n");
637 korg1212->sharedBufferPtr->cardCommand = 0;
638 korg1212->dsp_stop_is_processed = 1;
639 wake_up(&korg1212->wait);
9fd9156c
TI
640 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
641 stateName[korg1212->cardState]);
1da177e4
LT
642 }
643 }
b32425ac 644 spin_unlock_irqrestore(&korg1212->lock, flags);
1da177e4
LT
645}
646
fcfd3332 647static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
1da177e4
LT
648{
649 unsigned long flags;
9fd9156c 650 int rc;
1da177e4
LT
651
652 udelay(INTERCOMMAND_DELAY);
653 spin_lock_irqsave(&korg1212->lock, flags);
654 korg1212->idleMonitorOn = 1;
655 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
656 K1212_MODE_MonitorOn, 0, 0, 0);
657 spin_unlock_irqrestore(&korg1212->lock, flags);
9fd9156c 658 return rc;
1da177e4
LT
659}
660
fcfd3332 661static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
1da177e4
LT
662{
663 if (korg1212->idleMonitorOn) {
664 snd_korg1212_SendStopAndWait(korg1212);
665 korg1212->idleMonitorOn = 0;
666 }
667}
668
fcfd3332 669static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
1da177e4
LT
670{
671 korg1212->cardState = csState;
672}
673
fcfd3332 674static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
1da177e4 675{
9fd9156c
TI
676 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
677 stateName[korg1212->cardState], korg1212->opencnt);
62932df8 678 mutex_lock(&korg1212->open_mutex);
1da177e4
LT
679 if (korg1212->opencnt++ == 0) {
680 snd_korg1212_TurnOffIdleMonitor(korg1212);
681 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
682 }
683
62932df8 684 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
685 return 1;
686}
687
fcfd3332 688static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
1da177e4 689{
9fd9156c
TI
690 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
691 stateName[korg1212->cardState], korg1212->opencnt);
1da177e4 692
62932df8 693 mutex_lock(&korg1212->open_mutex);
1da177e4 694 if (--(korg1212->opencnt)) {
62932df8 695 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
696 return 0;
697 }
698
699 if (korg1212->cardState == K1212_STATE_SETUP) {
9fd9156c 700 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1da177e4 701 K1212_MODE_StopPlay, 0, 0, 0);
9fd9156c
TI
702 if (rc)
703 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
704 rc, stateName[korg1212->cardState]);
1da177e4 705 if (rc != K1212_CMDRET_Success) {
62932df8 706 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
707 return 0;
708 }
709 } else if (korg1212->cardState > K1212_STATE_SETUP) {
710 snd_korg1212_SendStopAndWait(korg1212);
711 }
712
713 if (korg1212->cardState > K1212_STATE_READY) {
714 snd_korg1212_TurnOnIdleMonitor(korg1212);
715 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
716 }
717
62932df8 718 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
719 return 0;
720}
721
722/* spinlock already held */
fcfd3332 723static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
1da177e4 724{
9fd9156c
TI
725 int rc;
726
727 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
728 stateName[korg1212->cardState], korg1212->setcnt);
1da177e4
LT
729
730 if (korg1212->setcnt++)
731 return 0;
732
733 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
734 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
735 K1212_MODE_SetupPlay, 0, 0, 0);
9fd9156c
TI
736 if (rc)
737 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
738 rc, stateName[korg1212->cardState]);
1da177e4
LT
739 if (rc != K1212_CMDRET_Success) {
740 return 1;
741 }
742 return 0;
743}
744
745/* spinlock already held */
fcfd3332 746static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
1da177e4 747{
9fd9156c
TI
748 int rc;
749
750 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
751 stateName[korg1212->cardState], korg1212->playcnt);
1da177e4
LT
752
753 if (korg1212->playcnt++)
754 return 0;
755
756 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
757 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
9fd9156c
TI
758 if (rc)
759 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
760 rc, stateName[korg1212->cardState]);
1da177e4
LT
761 if (rc != K1212_CMDRET_Success) {
762 return 1;
763 }
764 return 0;
765}
766
767/* spinlock already held */
fcfd3332 768static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
1da177e4 769{
9fd9156c
TI
770 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
771 stateName[korg1212->cardState], korg1212->playcnt);
1da177e4
LT
772
773 if (--(korg1212->playcnt))
774 return 0;
775
776 korg1212->setcnt = 0;
777
778 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
779 snd_korg1212_SendStop(korg1212);
780
781 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
782 return 0;
783}
784
fcfd3332 785static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
1da177e4
LT
786{
787 writel(PCI_INT_ENABLE_BIT |
788 PCI_DOORBELL_INT_ENABLE_BIT |
789 LOCAL_INT_ENABLE_BIT |
790 LOCAL_DOORBELL_INT_ENABLE_BIT |
791 LOCAL_DMA1_INT_ENABLE_BIT,
792 korg1212->statusRegPtr);
793}
794
795#if 0 /* not used */
796
fcfd3332
TI
797static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
798 enum MonitorModeSelector mode)
1da177e4 799{
9fd9156c
TI
800 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
801 stateName[korg1212->cardState]);
1da177e4
LT
802
803 switch (mode) {
9fd9156c
TI
804 case K1212_MONMODE_Off:
805 if (korg1212->cardState != K1212_STATE_MONITOR)
806 return 0;
807 else {
808 snd_korg1212_SendStopAndWait(korg1212);
809 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
810 }
811 break;
812
813 case K1212_MONMODE_On:
814 if (korg1212->cardState != K1212_STATE_OPEN)
815 return 0;
816 else {
817 int rc;
818 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
819 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
820 K1212_MODE_MonitorOn, 0, 0, 0);
821 if (rc != K1212_CMDRET_Success)
822 return 0;
823 }
824 break;
1da177e4 825
9fd9156c
TI
826 default:
827 return 0;
1da177e4
LT
828 }
829
830 return 1;
831}
832
833#endif /* not used */
834
fcfd3332 835static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
1da177e4 836{
9fd9156c
TI
837 if (korg1212->playback_pid != korg1212->capture_pid &&
838 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
839 return 0;
1da177e4 840
9fd9156c 841 return 1;
1da177e4
LT
842}
843
fcfd3332 844static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
1da177e4 845{
fcfd3332 846 static enum ClockSourceIndex s44[] = {
9fd9156c
TI
847 K1212_CLKIDX_AdatAt44_1K,
848 K1212_CLKIDX_WordAt44_1K,
849 K1212_CLKIDX_LocalAt44_1K
850 };
fcfd3332 851 static enum ClockSourceIndex s48[] = {
9fd9156c
TI
852 K1212_CLKIDX_AdatAt48K,
853 K1212_CLKIDX_WordAt48K,
854 K1212_CLKIDX_LocalAt48K
855 };
856 int parm, rc;
1da177e4 857
9fd9156c
TI
858 if (!snd_korg1212_use_is_exclusive (korg1212))
859 return -EBUSY;
1da177e4 860
fcfd3332 861 switch (rate) {
9fd9156c
TI
862 case 44100:
863 parm = s44[korg1212->clkSource];
864 break;
1da177e4 865
9fd9156c
TI
866 case 48000:
867 parm = s48[korg1212->clkSource];
868 break;
1da177e4 869
9fd9156c
TI
870 default:
871 return -EINVAL;
872 }
1da177e4
LT
873
874 korg1212->clkSrcRate = parm;
875 korg1212->clkRate = rate;
876
877 udelay(INTERCOMMAND_DELAY);
878 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
879 ClockSourceSelector[korg1212->clkSrcRate],
880 0, 0, 0);
9fd9156c
TI
881 if (rc)
882 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
883 rc, stateName[korg1212->cardState]);
1da177e4
LT
884
885 return 0;
886}
887
fcfd3332 888static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
1da177e4
LT
889{
890
9fd9156c
TI
891 if (source < 0 || source > 2)
892 return -EINVAL;
1da177e4
LT
893
894 korg1212->clkSource = source;
895
896 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
897
898 return 0;
899}
900
fcfd3332 901static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
1da177e4
LT
902{
903 writel(0, korg1212->statusRegPtr);
904}
905
fcfd3332 906static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
1da177e4 907{
fcfd3332 908 struct SensBits sensVals;
1da177e4
LT
909 int bitPosition;
910 int channel;
911 int clkIs48K;
912 int monModeSet;
913 u16 controlValue; // this keeps the current value to be written to
914 // the card's eeprom control register.
915 u16 count;
916 unsigned long flags;
917
9fd9156c
TI
918 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
919 stateName[korg1212->cardState]);
1da177e4
LT
920
921 // ----------------------------------------------------------------------------
922 // initialize things. The local init bit is always set when writing to the
923 // card's control register.
924 // ----------------------------------------------------------------------------
925 controlValue = 0;
926 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
927
928 // ----------------------------------------------------------------------------
929 // make sure the card is not in monitor mode when we do this update.
930 // ----------------------------------------------------------------------------
931 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
932 monModeSet = 1;
933 snd_korg1212_SendStopAndWait(korg1212);
934 } else
935 monModeSet = 0;
936
937 spin_lock_irqsave(&korg1212->lock, flags);
938
939 // ----------------------------------------------------------------------------
940 // we are about to send new values to the card, so clear the new values queued
941 // flag. Also, clear out mailbox 3, so we don't lockup.
942 // ----------------------------------------------------------------------------
943 writel(0, korg1212->mailbox3Ptr);
944 udelay(LOADSHIFT_DELAY);
945
946 // ----------------------------------------------------------------------------
947 // determine whether we are running a 48K or 44.1K clock. This info is used
948 // later when setting the SPDIF FF after the volume has been shifted in.
949 // ----------------------------------------------------------------------------
950 switch (korg1212->clkSrcRate) {
951 case K1212_CLKIDX_AdatAt44_1K:
952 case K1212_CLKIDX_WordAt44_1K:
953 case K1212_CLKIDX_LocalAt44_1K:
954 clkIs48K = 0;
955 break;
956
957 case K1212_CLKIDX_WordAt48K:
958 case K1212_CLKIDX_AdatAt48K:
959 case K1212_CLKIDX_LocalAt48K:
960 default:
961 clkIs48K = 1;
962 break;
963 }
964
965 // ----------------------------------------------------------------------------
966 // start the update. Setup the bit structure and then shift the bits.
967 // ----------------------------------------------------------------------------
968 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
969 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
970 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
971 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
972
973 // ----------------------------------------------------------------------------
974 // now start shifting the bits in. Start with the left channel then the right.
975 // ----------------------------------------------------------------------------
976 for (channel = 0; channel < 2; channel++) {
977
978 // ----------------------------------------------------------------------------
979 // Bring the load/shift line low, then wait - the spec says >150ns from load/
980 // shift low to the first rising edge of the clock.
981 // ----------------------------------------------------------------------------
982 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
983 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
984 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
985 udelay(LOADSHIFT_DELAY);
986
987 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
9fd9156c
TI
988 if (channel == 0) {
989 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
fcfd3332 990 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
9fd9156c
TI
991 else
992 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
993 } else {
994 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
995 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
996 else
997 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
998 }
1da177e4
LT
999
1000 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1001 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1002 udelay(SENSCLKPULSE_WIDTH);
1003 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1004 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1005 udelay(SENSCLKPULSE_WIDTH);
1006 }
1007
1008 // ----------------------------------------------------------------------------
1009 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1010 // bit if the clock rate is 48K otherwise write 0.
1011 // ----------------------------------------------------------------------------
1012 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1013 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1014 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1015 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1016 udelay(SENSCLKPULSE_WIDTH);
1017
1018 if (clkIs48K)
1019 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1020
1021 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1022 udelay(ONE_RTC_TICK);
1023 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1024 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1025 udelay(SENSCLKPULSE_WIDTH);
1026 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1027 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1028 udelay(SENSCLKPULSE_WIDTH);
1029 }
1030
1031 // ----------------------------------------------------------------------------
1032 // The update is complete. Set a timeout. This is the inter-update delay.
1033 // Also, if the card was in monitor mode, restore it.
1034 // ----------------------------------------------------------------------------
1035 for (count = 0; count < 10; count++)
1036 udelay(SENSCLKPULSE_WIDTH);
1037
1038 if (monModeSet) {
fcfd3332 1039 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1da177e4 1040 K1212_MODE_MonitorOn, 0, 0, 0);
9fd9156c
TI
1041 if (rc)
1042 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1043 rc, stateName[korg1212->cardState]);
1da177e4
LT
1044 }
1045
1046 spin_unlock_irqrestore(&korg1212->lock, flags);
1047
1048 return 1;
1049}
1050
fcfd3332 1051static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
1da177e4 1052{
fcfd3332 1053 int channel, rc;
1da177e4 1054
9fd9156c
TI
1055 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1056 stateName[korg1212->cardState]);
1da177e4
LT
1057
1058 // ----------------------------------------------------
1059 // tell the card to boot
1060 // ----------------------------------------------------
1061 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1062
9fd9156c
TI
1063 if (rc)
1064 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1065 rc, stateName[korg1212->cardState]);
1066 msleep(DSP_BOOT_DELAY_IN_MS);
1da177e4
LT
1067
1068 // --------------------------------------------------------------------------------
1069 // Let the card know where all the buffers are.
1070 // --------------------------------------------------------------------------------
1071 rc = snd_korg1212_Send1212Command(korg1212,
1072 K1212_DB_ConfigureBufferMemory,
1073 LowerWordSwap(korg1212->PlayDataPhy),
1074 LowerWordSwap(korg1212->RecDataPhy),
1075 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1076 // is based on 2 buffers
1077 0
1078 );
1079
9fd9156c
TI
1080 if (rc)
1081 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1082 rc, stateName[korg1212->cardState]);
1da177e4
LT
1083
1084 udelay(INTERCOMMAND_DELAY);
1085
1086 rc = snd_korg1212_Send1212Command(korg1212,
1087 K1212_DB_ConfigureMiscMemory,
1088 LowerWordSwap(korg1212->VolumeTablePhy),
1089 LowerWordSwap(korg1212->RoutingTablePhy),
1090 LowerWordSwap(korg1212->AdatTimeCodePhy),
1091 0
1092 );
1093
9fd9156c
TI
1094 if (rc)
1095 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1096 rc, stateName[korg1212->cardState]);
1da177e4
LT
1097
1098 // --------------------------------------------------------------------------------
1099 // Initialize the routing and volume tables, then update the card's state.
1100 // --------------------------------------------------------------------------------
1101 udelay(INTERCOMMAND_DELAY);
1102
1103 for (channel = 0; channel < kAudioChannels; channel++) {
1104 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1105 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1106 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1107 }
1108
1109 snd_korg1212_WriteADCSensitivity(korg1212);
1110
1111 udelay(INTERCOMMAND_DELAY);
1112 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1113 ClockSourceSelector[korg1212->clkSrcRate],
1114 0, 0, 0);
9fd9156c
TI
1115 if (rc)
1116 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1117 rc, stateName[korg1212->cardState]);
1da177e4 1118
9fd9156c 1119 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
1da177e4
LT
1120 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1121
9fd9156c
TI
1122 if (rc)
1123 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1124 rc, stateName[korg1212->cardState]);
1da177e4
LT
1125
1126 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1127}
1128
7d12e780 1129static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
1da177e4
LT
1130{
1131 u32 doorbellValue;
fcfd3332 1132 struct snd_korg1212 *korg1212 = dev_id;
1da177e4 1133
1da177e4
LT
1134 doorbellValue = readl(korg1212->inDoorbellPtr);
1135
1136 if (!doorbellValue)
1137 return IRQ_NONE;
1138
1139 spin_lock(&korg1212->lock);
1140
1141 writel(doorbellValue, korg1212->inDoorbellPtr);
1142
1143 korg1212->irqcount++;
1144
1145 korg1212->inIRQ++;
1146
1da177e4
LT
1147 switch (doorbellValue) {
1148 case K1212_DB_DSPDownloadDone:
9fd9156c
TI
1149 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1150 korg1212->irqcount, doorbellValue,
1151 stateName[korg1212->cardState]);
1da177e4
LT
1152 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1153 korg1212->dsp_is_loaded = 1;
1154 wake_up(&korg1212->wait);
1155 }
1156 break;
1157
1158 // ------------------------------------------------------------------------
1159 // an error occurred - stop the card
1160 // ------------------------------------------------------------------------
1161 case K1212_DB_DMAERROR:
9fd9156c
TI
1162 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1163 korg1212->irqcount, doorbellValue,
1164 stateName[korg1212->cardState]);
1165 snd_printk(KERN_ERR "korg1212: DMA Error\n");
1da177e4
LT
1166 korg1212->errorcnt++;
1167 korg1212->totalerrorcnt++;
1168 korg1212->sharedBufferPtr->cardCommand = 0;
1169 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1170 break;
1171
1172 // ------------------------------------------------------------------------
1173 // the card has stopped by our request. Clear the command word and signal
1174 // the semaphore in case someone is waiting for this.
1175 // ------------------------------------------------------------------------
1176 case K1212_DB_CARDSTOPPED:
fcfd3332 1177 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
9fd9156c
TI
1178 korg1212->irqcount, doorbellValue,
1179 stateName[korg1212->cardState]);
1da177e4
LT
1180 korg1212->sharedBufferPtr->cardCommand = 0;
1181 break;
1182
1183 default:
9fd9156c 1184 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
fcfd3332
TI
1185 korg1212->irqcount, doorbellValue,
1186 korg1212->currentBuffer, stateName[korg1212->cardState]);
1da177e4
LT
1187 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1188 korg1212->currentBuffer++;
1189
1190 if (korg1212->currentBuffer >= kNumBuffers)
1191 korg1212->currentBuffer = 0;
1192
1193 if (!korg1212->running)
1194 break;
1195
1196 if (korg1212->capture_substream) {
1197 spin_unlock(&korg1212->lock);
1198 snd_pcm_period_elapsed(korg1212->capture_substream);
1199 spin_lock(&korg1212->lock);
1200 }
1201
1202 if (korg1212->playback_substream) {
1203 spin_unlock(&korg1212->lock);
1204 snd_pcm_period_elapsed(korg1212->playback_substream);
1205 spin_lock(&korg1212->lock);
1206 }
1207 }
1208 break;
1209 }
1210
1211 korg1212->inIRQ--;
1212
1213 spin_unlock(&korg1212->lock);
1214
1215 return IRQ_HANDLED;
1216}
1217
fcfd3332 1218static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
1da177e4 1219{
fcfd3332 1220 int rc;
1da177e4 1221
9fd9156c
TI
1222 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1223 stateName[korg1212->cardState]);
1da177e4
LT
1224
1225 // ---------------------------------------------------------------
1226 // verify the state of the card before proceeding.
1227 // ---------------------------------------------------------------
9fd9156c 1228 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
1da177e4 1229 return 1;
1da177e4
LT
1230
1231 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1232
1da177e4
LT
1233 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1234 UpperWordSwap(korg1212->dma_dsp.addr),
1235 0, 0, 0);
9fd9156c
TI
1236 if (rc)
1237 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1238 rc, stateName[korg1212->cardState]);
1da177e4
LT
1239
1240 korg1212->dsp_is_loaded = 0;
1241 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1242 if (! korg1212->dsp_is_loaded )
1243 return -EBUSY; /* timeout */
1244
1245 snd_korg1212_OnDSPDownloadComplete(korg1212);
1246
1247 return 0;
1248}
1249
fcfd3332 1250static struct snd_pcm_hardware snd_korg1212_playback_info =
1da177e4
LT
1251{
1252 .info = (SNDRV_PCM_INFO_MMAP |
1253 SNDRV_PCM_INFO_MMAP_VALID |
1254 SNDRV_PCM_INFO_INTERLEAVED),
1255 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1256 .rates = (SNDRV_PCM_RATE_44100 |
1257 SNDRV_PCM_RATE_48000),
1258 .rate_min = 44100,
1259 .rate_max = 48000,
1260 .channels_min = K1212_MIN_CHANNELS,
1261 .channels_max = K1212_MAX_CHANNELS,
1262 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1263 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1264 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1265 .periods_min = K1212_PERIODS,
1266 .periods_max = K1212_PERIODS,
1267 .fifo_size = 0,
1268};
1269
fcfd3332 1270static struct snd_pcm_hardware snd_korg1212_capture_info =
1da177e4
LT
1271{
1272 .info = (SNDRV_PCM_INFO_MMAP |
1273 SNDRV_PCM_INFO_MMAP_VALID |
1274 SNDRV_PCM_INFO_INTERLEAVED),
1275 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1276 .rates = (SNDRV_PCM_RATE_44100 |
1277 SNDRV_PCM_RATE_48000),
1278 .rate_min = 44100,
1279 .rate_max = 48000,
1280 .channels_min = K1212_MIN_CHANNELS,
1281 .channels_max = K1212_MAX_CHANNELS,
1282 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1283 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1284 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1285 .periods_min = K1212_PERIODS,
1286 .periods_max = K1212_PERIODS,
1287 .fifo_size = 0,
1288};
1289
fcfd3332 1290static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
1da177e4 1291{
fcfd3332 1292 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1da177e4
LT
1293 int i;
1294
9fd9156c
TI
1295 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1296 pos, offset, size, count);
1da177e4
LT
1297 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1298
1299 for (i=0; i < count; i++) {
1300#if K1212_DEBUG_LEVEL > 0
1301 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1302 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
9fd9156c
TI
1303 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1304 dst, i);
1da177e4
LT
1305 return -EFAULT;
1306 }
1307#endif
1308 memset((void*) dst + offset, 0, size);
1309 dst++;
1310 }
1311
1312 return 0;
1313}
1314
fcfd3332 1315static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
1da177e4 1316{
fcfd3332 1317 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1da177e4
LT
1318 int i, rc;
1319
9fd9156c
TI
1320 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1321 pos, offset, size);
1da177e4
LT
1322 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1323
1324 for (i=0; i < count; i++) {
1325#if K1212_DEBUG_LEVEL > 0
1326 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1327 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
9fd9156c 1328 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1329 return -EFAULT;
1330 }
1331#endif
1332 rc = copy_to_user(dst + offset, src, size);
1333 if (rc) {
1da177e4 1334 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1335 return -EFAULT;
1336 }
1337 src++;
1338 dst += size;
1339 }
1340
1341 return 0;
1342}
1343
fcfd3332 1344static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
1da177e4 1345{
fcfd3332 1346 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1da177e4
LT
1347 int i, rc;
1348
9fd9156c
TI
1349 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1350 pos, offset, size, count);
1da177e4
LT
1351
1352 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1353
1354 for (i=0; i < count; i++) {
1355#if K1212_DEBUG_LEVEL > 0
1356 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1357 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
fcfd3332 1358 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1359 return -EFAULT;
1360 }
1361#endif
1362 rc = copy_from_user((void*) dst + offset, src, size);
1363 if (rc) {
fcfd3332 1364 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1365 return -EFAULT;
1366 }
1367 dst++;
1368 src += size;
1369 }
1370
1371 return 0;
1372}
1373
fcfd3332 1374static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
1da177e4 1375{
fcfd3332 1376 struct snd_korg1212 *korg1212 = pcm->private_data;
1da177e4 1377
9fd9156c
TI
1378 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1379 stateName[korg1212->cardState]);
1da177e4
LT
1380
1381 korg1212->pcm = NULL;
1382}
1383
fcfd3332 1384static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
1da177e4
LT
1385{
1386 unsigned long flags;
fcfd3332
TI
1387 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1388 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 1389
9fd9156c
TI
1390 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1391 stateName[korg1212->cardState]);
1da177e4
LT
1392
1393 snd_pcm_set_sync(substream); // ???
1394
1395 snd_korg1212_OpenCard(korg1212);
1396
1397 runtime->hw = snd_korg1212_playback_info;
1398 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1399
1400 spin_lock_irqsave(&korg1212->lock, flags);
1401
1402 korg1212->playback_substream = substream;
1403 korg1212->playback_pid = current->pid;
1404 korg1212->periodsize = K1212_PERIODS;
1405 korg1212->channels = K1212_CHANNELS;
1406 korg1212->errorcnt = 0;
1407
1408 spin_unlock_irqrestore(&korg1212->lock, flags);
1409
1410 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1411 return 0;
1412}
1413
1414
fcfd3332 1415static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
1da177e4
LT
1416{
1417 unsigned long flags;
fcfd3332
TI
1418 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1419 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 1420
9fd9156c
TI
1421 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1422 stateName[korg1212->cardState]);
1da177e4
LT
1423
1424 snd_pcm_set_sync(substream);
1425
1426 snd_korg1212_OpenCard(korg1212);
1427
1428 runtime->hw = snd_korg1212_capture_info;
1429 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1430
1431 spin_lock_irqsave(&korg1212->lock, flags);
1432
1433 korg1212->capture_substream = substream;
1434 korg1212->capture_pid = current->pid;
1435 korg1212->periodsize = K1212_PERIODS;
1436 korg1212->channels = K1212_CHANNELS;
1437
1438 spin_unlock_irqrestore(&korg1212->lock, flags);
1439
fcfd3332
TI
1440 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1441 kPlayBufferFrames, kPlayBufferFrames);
1da177e4
LT
1442 return 0;
1443}
1444
fcfd3332 1445static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
1da177e4
LT
1446{
1447 unsigned long flags;
fcfd3332 1448 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1449
9fd9156c
TI
1450 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1451 stateName[korg1212->cardState]);
1da177e4
LT
1452
1453 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1454
1455 spin_lock_irqsave(&korg1212->lock, flags);
1456
1457 korg1212->playback_pid = -1;
1458 korg1212->playback_substream = NULL;
1459 korg1212->periodsize = 0;
1460
1461 spin_unlock_irqrestore(&korg1212->lock, flags);
1462
1463 snd_korg1212_CloseCard(korg1212);
1464 return 0;
1465}
1466
fcfd3332 1467static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
1da177e4
LT
1468{
1469 unsigned long flags;
fcfd3332 1470 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1471
9fd9156c
TI
1472 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1473 stateName[korg1212->cardState]);
1da177e4
LT
1474
1475 spin_lock_irqsave(&korg1212->lock, flags);
1476
1477 korg1212->capture_pid = -1;
1478 korg1212->capture_substream = NULL;
1479 korg1212->periodsize = 0;
1480
1481 spin_unlock_irqrestore(&korg1212->lock, flags);
1482
1483 snd_korg1212_CloseCard(korg1212);
1484 return 0;
1485}
1486
fcfd3332 1487static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
1488 unsigned int cmd, void *arg)
1489{
9fd9156c 1490 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1da177e4
LT
1491
1492 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
fcfd3332 1493 struct snd_pcm_channel_info *info = arg;
1da177e4
LT
1494 info->offset = 0;
1495 info->first = info->channel * 16;
1496 info->step = 256;
1da177e4 1497 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
1da177e4
LT
1498 return 0;
1499 }
1500
1501 return snd_pcm_lib_ioctl(substream, cmd, arg);
1502}
1503
fcfd3332
TI
1504static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1505 struct snd_pcm_hw_params *params)
1da177e4
LT
1506{
1507 unsigned long flags;
fcfd3332 1508 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1509 int err;
1510 pid_t this_pid;
1511 pid_t other_pid;
1512
9fd9156c
TI
1513 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1514 stateName[korg1212->cardState]);
1da177e4
LT
1515
1516 spin_lock_irqsave(&korg1212->lock, flags);
1517
1518 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1519 this_pid = korg1212->playback_pid;
1520 other_pid = korg1212->capture_pid;
1521 } else {
1522 this_pid = korg1212->capture_pid;
1523 other_pid = korg1212->playback_pid;
1524 }
1525
1526 if ((other_pid > 0) && (this_pid != other_pid)) {
1527
1528 /* The other stream is open, and not by the same
1529 task as this one. Make sure that the parameters
1530 that matter are the same.
1531 */
1532
1533 if ((int)params_rate(params) != korg1212->clkRate) {
1534 spin_unlock_irqrestore(&korg1212->lock, flags);
1535 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1536 return -EBUSY;
1537 }
1538
1539 spin_unlock_irqrestore(&korg1212->lock, flags);
1540 return 0;
1541 }
1542
1543 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1544 spin_unlock_irqrestore(&korg1212->lock, flags);
1545 return err;
1546 }
1547
1548 korg1212->channels = params_channels(params);
1549 korg1212->periodsize = K1212_PERIOD_BYTES;
1550
1551 spin_unlock_irqrestore(&korg1212->lock, flags);
1552
1553 return 0;
1554}
1555
fcfd3332 1556static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
1da177e4 1557{
fcfd3332 1558 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1559 int rc;
1560
9fd9156c
TI
1561 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1562 stateName[korg1212->cardState]);
1da177e4
LT
1563
1564 spin_lock_irq(&korg1212->lock);
1565
1566 /* FIXME: we should wait for ack! */
1567 if (korg1212->stop_pending_cnt > 0) {
9fd9156c
TI
1568 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1569 stateName[korg1212->cardState]);
1da177e4
LT
1570 spin_unlock_irq(&korg1212->lock);
1571 return -EAGAIN;
1572 /*
1573 korg1212->sharedBufferPtr->cardCommand = 0;
1574 del_timer(&korg1212->timer);
1575 korg1212->stop_pending_cnt = 0;
1576 */
1577 }
1578
1579 rc = snd_korg1212_SetupForPlay(korg1212);
1580
1581 korg1212->currentBuffer = 0;
1582
1583 spin_unlock_irq(&korg1212->lock);
1584
1585 return rc ? -EINVAL : 0;
1586}
1587
fcfd3332 1588static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
1589 int cmd)
1590{
fcfd3332 1591 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1592 int rc;
1593
9fd9156c
TI
1594 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1595 stateName[korg1212->cardState], cmd);
1da177e4
LT
1596
1597 spin_lock(&korg1212->lock);
1598 switch (cmd) {
1599 case SNDRV_PCM_TRIGGER_START:
1600/*
1601 if (korg1212->running) {
9fd9156c 1602 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
1da177e4
LT
1603 break;
1604 }
1605*/
1606 korg1212->running++;
1607 rc = snd_korg1212_TriggerPlay(korg1212);
1608 break;
1609
1610 case SNDRV_PCM_TRIGGER_STOP:
1611/*
1612 if (!korg1212->running) {
9fd9156c 1613 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
1da177e4
LT
1614 break;
1615 }
1616*/
1617 korg1212->running--;
1618 rc = snd_korg1212_StopPlay(korg1212);
1619 break;
1620
1621 default:
1622 rc = 1;
1623 break;
1624 }
1625 spin_unlock(&korg1212->lock);
1626 return rc ? -EINVAL : 0;
1627}
1628
fcfd3332 1629static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
1da177e4 1630{
fcfd3332 1631 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1632 snd_pcm_uframes_t pos;
1633
1634 pos = korg1212->currentBuffer * kPlayBufferFrames;
1635
9fd9156c
TI
1636 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1637 stateName[korg1212->cardState], pos);
1da177e4
LT
1638
1639 return pos;
1640}
1641
fcfd3332 1642static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
1da177e4 1643{
fcfd3332 1644 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1645 snd_pcm_uframes_t pos;
1646
1647 pos = korg1212->currentBuffer * kPlayBufferFrames;
1648
9fd9156c
TI
1649 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1650 stateName[korg1212->cardState], pos);
1da177e4
LT
1651
1652 return pos;
1653}
1654
fcfd3332 1655static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
1da177e4
LT
1656 int channel, /* not used (interleaved data) */
1657 snd_pcm_uframes_t pos,
1658 void __user *src,
1659 snd_pcm_uframes_t count)
1660{
fcfd3332 1661 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1662
9fd9156c
TI
1663 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1664 stateName[korg1212->cardState], pos, count);
1da177e4
LT
1665
1666 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1667
1668}
1669
fcfd3332 1670static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
1da177e4
LT
1671 int channel, /* not used (interleaved data) */
1672 snd_pcm_uframes_t pos,
1673 snd_pcm_uframes_t count)
1674{
fcfd3332 1675 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1676
9fd9156c
TI
1677 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1678 stateName[korg1212->cardState]);
1da177e4
LT
1679
1680 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1681}
1682
fcfd3332 1683static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
1da177e4
LT
1684 int channel, /* not used (interleaved data) */
1685 snd_pcm_uframes_t pos,
1686 void __user *dst,
1687 snd_pcm_uframes_t count)
1688{
fcfd3332 1689 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1690
9fd9156c
TI
1691 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1692 stateName[korg1212->cardState], pos, count);
1da177e4
LT
1693
1694 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1695}
1696
fcfd3332 1697static struct snd_pcm_ops snd_korg1212_playback_ops = {
1da177e4
LT
1698 .open = snd_korg1212_playback_open,
1699 .close = snd_korg1212_playback_close,
1700 .ioctl = snd_korg1212_ioctl,
1701 .hw_params = snd_korg1212_hw_params,
1702 .prepare = snd_korg1212_prepare,
1703 .trigger = snd_korg1212_trigger,
1704 .pointer = snd_korg1212_playback_pointer,
1705 .copy = snd_korg1212_playback_copy,
1706 .silence = snd_korg1212_playback_silence,
1707};
1708
fcfd3332 1709static struct snd_pcm_ops snd_korg1212_capture_ops = {
1da177e4
LT
1710 .open = snd_korg1212_capture_open,
1711 .close = snd_korg1212_capture_close,
1712 .ioctl = snd_korg1212_ioctl,
1713 .hw_params = snd_korg1212_hw_params,
1714 .prepare = snd_korg1212_prepare,
1715 .trigger = snd_korg1212_trigger,
1716 .pointer = snd_korg1212_capture_pointer,
1717 .copy = snd_korg1212_capture_copy,
1718};
1719
1720/*
1721 * Control Interface
1722 */
1723
fcfd3332
TI
1724static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1726{
1727 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1728 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1729 return 0;
1730}
1731
fcfd3332
TI
1732static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1733 struct snd_ctl_elem_value *u)
1da177e4 1734{
fcfd3332 1735 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1736 int i = kcontrol->private_value;
1737
1738 spin_lock_irq(&korg1212->lock);
1739
1740 u->value.integer.value[0] = korg1212->volumePhase[i];
1741
1742 if (i >= 8)
1743 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1744
1745 spin_unlock_irq(&korg1212->lock);
1746
1747 return 0;
1748}
1749
fcfd3332
TI
1750static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *u)
1da177e4 1752{
fcfd3332 1753 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1754 int change = 0;
1755 int i, val;
1756
1757 spin_lock_irq(&korg1212->lock);
1758
1759 i = kcontrol->private_value;
1760
1761 korg1212->volumePhase[i] = u->value.integer.value[0];
1762
1763 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1764
1765 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1766 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1767 korg1212->sharedBufferPtr->volumeData[i] = val;
1768 change = 1;
1769 }
1770
1771 if (i >= 8) {
1772 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1773
1774 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1775
1776 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1777 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1778 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1779 change = 1;
1780 }
1781 }
1782
1783 spin_unlock_irq(&korg1212->lock);
1784
1785 return change;
1786}
1787
fcfd3332
TI
1788static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1790{
1791 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1792 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1793 uinfo->value.integer.min = k1212MinVolume;
1794 uinfo->value.integer.max = k1212MaxVolume;
1795 return 0;
1796}
1797
fcfd3332
TI
1798static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1799 struct snd_ctl_elem_value *u)
1da177e4 1800{
fcfd3332 1801 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1802 int i;
1803
1804 spin_lock_irq(&korg1212->lock);
1805
1806 i = kcontrol->private_value;
1807 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1808
1809 if (i >= 8)
1810 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1811
1812 spin_unlock_irq(&korg1212->lock);
1813
1814 return 0;
1815}
1816
fcfd3332
TI
1817static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1818 struct snd_ctl_elem_value *u)
1da177e4 1819{
fcfd3332 1820 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1821 int change = 0;
1822 int i;
1823 int val;
1824
1825 spin_lock_irq(&korg1212->lock);
1826
1827 i = kcontrol->private_value;
1828
1829 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1830 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1831 val *= u->value.integer.value[0];
1832 korg1212->sharedBufferPtr->volumeData[i] = val;
1833 change = 1;
1834 }
1835
1836 if (i >= 8) {
1837 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1838 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1839 val *= u->value.integer.value[1];
1840 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1841 change = 1;
1842 }
1843 }
1844
1845 spin_unlock_irq(&korg1212->lock);
1846
1847 return change;
1848}
1849
fcfd3332
TI
1850static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1851 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1852{
1853 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1854 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1855 uinfo->value.enumerated.items = kAudioChannels;
1856 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1857 uinfo->value.enumerated.item = kAudioChannels-1;
1858 }
1859 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1860 return 0;
1861}
1862
fcfd3332
TI
1863static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1864 struct snd_ctl_elem_value *u)
1da177e4 1865{
fcfd3332 1866 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1867 int i;
1868
1869 spin_lock_irq(&korg1212->lock);
1870
1871 i = kcontrol->private_value;
1872 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1873
1874 if (i >= 8)
1875 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1876
1877 spin_unlock_irq(&korg1212->lock);
1878
1879 return 0;
1880}
1881
fcfd3332
TI
1882static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_value *u)
1da177e4 1884{
fcfd3332 1885 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1886 int change = 0, i;
1887
1888 spin_lock_irq(&korg1212->lock);
1889
1890 i = kcontrol->private_value;
1891
1892 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1893 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1894 change = 1;
1895 }
1896
1897 if (i >= 8) {
1898 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1899 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1900 change = 1;
1901 }
1902 }
1903
1904 spin_unlock_irq(&korg1212->lock);
1905
1906 return change;
1907}
1908
fcfd3332
TI
1909static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1911{
1912 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1913 uinfo->count = 2;
1914 uinfo->value.integer.min = k1212MaxADCSens;
1915 uinfo->value.integer.max = k1212MinADCSens;
1916 return 0;
1917}
1918
fcfd3332
TI
1919static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_value *u)
1da177e4 1921{
fcfd3332 1922 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1923
1924 spin_lock_irq(&korg1212->lock);
1925
1926 u->value.integer.value[0] = korg1212->leftADCInSens;
1927 u->value.integer.value[1] = korg1212->rightADCInSens;
1928
1929 spin_unlock_irq(&korg1212->lock);
1930
1931 return 0;
1932}
1933
fcfd3332
TI
1934static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *u)
1da177e4 1936{
fcfd3332 1937 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1938 int change = 0;
1939
1940 spin_lock_irq(&korg1212->lock);
1941
1942 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1943 korg1212->leftADCInSens = u->value.integer.value[0];
1944 change = 1;
1945 }
1946 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1947 korg1212->rightADCInSens = u->value.integer.value[1];
1948 change = 1;
1949 }
1950
1951 spin_unlock_irq(&korg1212->lock);
1952
1953 if (change)
1954 snd_korg1212_WriteADCSensitivity(korg1212);
1955
1956 return change;
1957}
1958
fcfd3332
TI
1959static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1961{
1962 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1963 uinfo->count = 1;
1964 uinfo->value.enumerated.items = 3;
1965 if (uinfo->value.enumerated.item > 2) {
1966 uinfo->value.enumerated.item = 2;
1967 }
1968 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1969 return 0;
1970}
1971
fcfd3332
TI
1972static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1973 struct snd_ctl_elem_value *ucontrol)
1da177e4 1974{
fcfd3332 1975 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1976
1977 spin_lock_irq(&korg1212->lock);
1978
1979 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1980
1981 spin_unlock_irq(&korg1212->lock);
1982 return 0;
1983}
1984
fcfd3332
TI
1985static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
1da177e4 1987{
fcfd3332 1988 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1989 unsigned int val;
1990 int change;
1991
1992 val = ucontrol->value.enumerated.item[0] % 3;
1993 spin_lock_irq(&korg1212->lock);
1994 change = val != korg1212->clkSource;
1995 snd_korg1212_SetClockSource(korg1212, val);
1996 spin_unlock_irq(&korg1212->lock);
1997 return change;
1998}
1999
2000#define MON_MIXER(ord,c_name) \
2001 { \
2002 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2003 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2004 .name = c_name " Monitor Volume", \
2005 .info = snd_korg1212_control_volume_info, \
2006 .get = snd_korg1212_control_volume_get, \
2007 .put = snd_korg1212_control_volume_put, \
2008 .private_value = ord, \
2009 }, \
2010 { \
2011 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2012 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2013 .name = c_name " Monitor Route", \
2014 .info = snd_korg1212_control_route_info, \
2015 .get = snd_korg1212_control_route_get, \
2016 .put = snd_korg1212_control_route_put, \
2017 .private_value = ord, \
2018 }, \
2019 { \
2020 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
67ed4161 2021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1da177e4
LT
2022 .name = c_name " Monitor Phase Invert", \
2023 .info = snd_korg1212_control_phase_info, \
2024 .get = snd_korg1212_control_phase_get, \
2025 .put = snd_korg1212_control_phase_put, \
2026 .private_value = ord, \
2027 }
2028
fcfd3332 2029static struct snd_kcontrol_new snd_korg1212_controls[] = {
1da177e4
LT
2030 MON_MIXER(8, "Analog"),
2031 MON_MIXER(10, "SPDIF"),
2032 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2033 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2034 {
2035 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
67ed4161 2036 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1da177e4
LT
2037 .name = "Sync Source",
2038 .info = snd_korg1212_control_sync_info,
2039 .get = snd_korg1212_control_sync_get,
2040 .put = snd_korg1212_control_sync_put,
2041 },
2042 {
2043 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2044 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2045 .name = "ADC Attenuation",
2046 .info = snd_korg1212_control_info,
2047 .get = snd_korg1212_control_get,
2048 .put = snd_korg1212_control_put,
2049 }
2050};
2051
2052/*
2053 * proc interface
2054 */
2055
fcfd3332
TI
2056static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2057 struct snd_info_buffer *buffer)
1da177e4
LT
2058{
2059 int n;
fcfd3332 2060 struct snd_korg1212 *korg1212 = entry->private_data;
1da177e4
LT
2061
2062 snd_iprintf(buffer, korg1212->card->longname);
2063 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2064 snd_iprintf(buffer, "\nGeneral settings\n");
2065 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2066 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2067 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2068 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2069 snd_iprintf(buffer, " Volume Info:\n");
2070 for (n=0; n<kAudioChannels; n++)
2071 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2072 channelName[n],
2073 channelName[korg1212->sharedBufferPtr->routeData[n]],
2074 korg1212->sharedBufferPtr->volumeData[n]);
2075 snd_iprintf(buffer, "\nGeneral status\n");
2076 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2077 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2078 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2079 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2080 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2081 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2082}
2083
fcfd3332 2084static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
1da177e4 2085{
fcfd3332 2086 struct snd_info_entry *entry;
1da177e4
LT
2087
2088 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
bf850204 2089 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
1da177e4
LT
2090}
2091
2092static int
fcfd3332 2093snd_korg1212_free(struct snd_korg1212 *korg1212)
1da177e4
LT
2094{
2095 snd_korg1212_TurnOffIdleMonitor(korg1212);
2096
2097 if (korg1212->irq >= 0) {
2098 synchronize_irq(korg1212->irq);
2099 snd_korg1212_DisableCardInterrupts(korg1212);
9fd9156c 2100 free_irq(korg1212->irq, korg1212);
1da177e4
LT
2101 korg1212->irq = -1;
2102 }
2103
2104 if (korg1212->iobase != NULL) {
2105 iounmap(korg1212->iobase);
2106 korg1212->iobase = NULL;
2107 }
2108
2109 pci_release_regions(korg1212->pci);
2110
2111 // ----------------------------------------------------
2112 // free up memory resources used for the DSP download.
2113 // ----------------------------------------------------
2114 if (korg1212->dma_dsp.area) {
2115 snd_dma_free_pages(&korg1212->dma_dsp);
2116 korg1212->dma_dsp.area = NULL;
2117 }
2118
2119#ifndef K1212_LARGEALLOC
2120
2121 // ------------------------------------------------------
2122 // free up memory resources used for the Play/Rec Buffers
2123 // ------------------------------------------------------
2124 if (korg1212->dma_play.area) {
2125 snd_dma_free_pages(&korg1212->dma_play);
2126 korg1212->dma_play.area = NULL;
2127 }
2128
2129 if (korg1212->dma_rec.area) {
2130 snd_dma_free_pages(&korg1212->dma_rec);
2131 korg1212->dma_rec.area = NULL;
2132 }
2133
2134#endif
2135
2136 // ----------------------------------------------------
2137 // free up memory resources used for the Shared Buffers
2138 // ----------------------------------------------------
2139 if (korg1212->dma_shared.area) {
2140 snd_dma_free_pages(&korg1212->dma_shared);
2141 korg1212->dma_shared.area = NULL;
2142 }
2143
2144 pci_disable_device(korg1212->pci);
2145 kfree(korg1212);
2146 return 0;
2147}
2148
fcfd3332 2149static int snd_korg1212_dev_free(struct snd_device *device)
1da177e4 2150{
fcfd3332 2151 struct snd_korg1212 *korg1212 = device->device_data;
1da177e4 2152 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
1da177e4
LT
2153 return snd_korg1212_free(korg1212);
2154}
2155
fcfd3332
TI
2156static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2157 struct snd_korg1212 ** rchip)
1da177e4
LT
2158
2159{
9fd9156c 2160 int err, rc;
1da177e4
LT
2161 unsigned int i;
2162 unsigned ioport_size, iomem_size, iomem2_size;
fcfd3332 2163 struct snd_korg1212 * korg1212;
2493a6d1 2164 const struct firmware *dsp_code;
1da177e4 2165
fcfd3332 2166 static struct snd_device_ops ops = {
1da177e4
LT
2167 .dev_free = snd_korg1212_dev_free,
2168 };
2169
2170 * rchip = NULL;
2171 if ((err = pci_enable_device(pci)) < 0)
2172 return err;
2173
e560d8d8 2174 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
1da177e4
LT
2175 if (korg1212 == NULL) {
2176 pci_disable_device(pci);
2177 return -ENOMEM;
2178 }
2179
2180 korg1212->card = card;
2181 korg1212->pci = pci;
2182
2183 init_waitqueue_head(&korg1212->wait);
2184 spin_lock_init(&korg1212->lock);
62932df8 2185 mutex_init(&korg1212->open_mutex);
1da177e4
LT
2186 init_timer(&korg1212->timer);
2187 korg1212->timer.function = snd_korg1212_timer_func;
2188 korg1212->timer.data = (unsigned long)korg1212;
2189
2190 korg1212->irq = -1;
2191 korg1212->clkSource = K1212_CLKIDX_Local;
2192 korg1212->clkRate = 44100;
2193 korg1212->inIRQ = 0;
2194 korg1212->running = 0;
2195 korg1212->opencnt = 0;
2196 korg1212->playcnt = 0;
2197 korg1212->setcnt = 0;
2198 korg1212->totalerrorcnt = 0;
2199 korg1212->playback_pid = -1;
2200 korg1212->capture_pid = -1;
2201 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2202 korg1212->idleMonitorOn = 0;
2203 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2204 korg1212->leftADCInSens = k1212MaxADCSens;
2205 korg1212->rightADCInSens = k1212MaxADCSens;
2206
2207 for (i=0; i<kAudioChannels; i++)
2208 korg1212->volumePhase[i] = 0;
2209
2210 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2211 kfree(korg1212);
2212 pci_disable_device(pci);
2213 return err;
2214 }
2215
2216 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2217 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2218 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2219
2220 iomem_size = pci_resource_len(korg1212->pci, 0);
2221 ioport_size = pci_resource_len(korg1212->pci, 1);
2222 iomem2_size = pci_resource_len(korg1212->pci, 2);
2223
1da177e4
LT
2224 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2225 " iomem = 0x%lx (%d)\n"
2226 " ioport = 0x%lx (%d)\n"
2227 " iomem = 0x%lx (%d)\n"
2228 " [%s]\n",
2229 korg1212->iomem, iomem_size,
2230 korg1212->ioport, ioport_size,
2231 korg1212->iomem2, iomem2_size,
2232 stateName[korg1212->cardState]);
1da177e4
LT
2233
2234 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2235 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2236 korg1212->iomem + iomem_size - 1);
2237 snd_korg1212_free(korg1212);
2238 return -EBUSY;
2239 }
2240
2241 err = request_irq(pci->irq, snd_korg1212_interrupt,
437a5a46 2242 IRQF_SHARED,
9fd9156c 2243 "korg1212", korg1212);
1da177e4
LT
2244
2245 if (err) {
2246 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2247 snd_korg1212_free(korg1212);
2248 return -EBUSY;
2249 }
2250
2251 korg1212->irq = pci->irq;
2252
2253 pci_set_master(korg1212->pci);
2254
2255 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2256 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2257 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2258 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2259 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2260 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2261 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2262 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2263 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2264 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2265
1da177e4
LT
2266 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2267 " Status register = 0x%p\n"
2268 " OutDoorbell = 0x%p\n"
2269 " InDoorbell = 0x%p\n"
2270 " Mailbox0 = 0x%p\n"
2271 " Mailbox1 = 0x%p\n"
2272 " Mailbox2 = 0x%p\n"
2273 " Mailbox3 = 0x%p\n"
2274 " ControlReg = 0x%p\n"
2275 " SensReg = 0x%p\n"
2276 " IDReg = 0x%p\n"
2277 " [%s]\n",
2278 korg1212->statusRegPtr,
2279 korg1212->outDoorbellPtr,
2280 korg1212->inDoorbellPtr,
2281 korg1212->mailbox0Ptr,
2282 korg1212->mailbox1Ptr,
2283 korg1212->mailbox2Ptr,
2284 korg1212->mailbox3Ptr,
2285 korg1212->controlRegPtr,
2286 korg1212->sensRegPtr,
2287 korg1212->idRegPtr,
2288 stateName[korg1212->cardState]);
1da177e4
LT
2289
2290 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
fcfd3332
TI
2291 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2292 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
1da177e4
LT
2293 snd_korg1212_free(korg1212);
2294 return -ENOMEM;
2295 }
fcfd3332 2296 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
1da177e4
LT
2297 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2298
fcfd3332 2299 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
1da177e4
LT
2300
2301#ifndef K1212_LARGEALLOC
2302
fcfd3332 2303 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
1da177e4
LT
2304
2305 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2306 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2307 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2308 snd_korg1212_free(korg1212);
2309 return -ENOMEM;
2310 }
fcfd3332 2311 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
1da177e4
LT
2312 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2313
1da177e4
LT
2314 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2315 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
1da177e4
LT
2316
2317 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2318 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2319 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2320 snd_korg1212_free(korg1212);
2321 return -ENOMEM;
2322 }
fcfd3332 2323 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
1da177e4
LT
2324 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2325
1da177e4
LT
2326 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2327 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
1da177e4
LT
2328
2329#else // K1212_LARGEALLOC
2330
2331 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2332 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
fcfd3332
TI
2333 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2334 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
1da177e4
LT
2335
2336#endif // K1212_LARGEALLOC
2337
1da177e4 2338 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
fcfd3332 2339 offsetof(struct KorgSharedBuffer, volumeData);
1da177e4 2340 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
fcfd3332 2341 offsetof(struct KorgSharedBuffer, routeData);
1da177e4 2342 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
fcfd3332 2343 offsetof(struct KorgSharedBuffer, AdatTimeCode);
1da177e4 2344
2493a6d1
CL
2345 err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
2346 if (err < 0) {
2347 release_firmware(dsp_code);
2348#ifdef FIRMWARE_IN_THE_KERNEL
2349 dsp_code = &static_dsp_code;
2350#else
2351 snd_printk(KERN_ERR "firmware not available\n");
2352 snd_korg1212_free(korg1212);
2353 return err;
2354#endif
2355 }
2356
1da177e4 2357 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2493a6d1
CL
2358 dsp_code->size, &korg1212->dma_dsp) < 0) {
2359 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", dsp_code->size);
1da177e4 2360 snd_korg1212_free(korg1212);
2493a6d1
CL
2361#ifdef FIRMWARE_IN_THE_KERNEL
2362 if (dsp_code != &static_dsp_code)
2363#endif
2364 release_firmware(dsp_code);
1da177e4
LT
2365 return -ENOMEM;
2366 }
2367
1da177e4 2368 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2493a6d1 2369 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
1da177e4 2370 stateName[korg1212->cardState]);
1da177e4 2371
2493a6d1
CL
2372 memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
2373
2374#ifdef FIRMWARE_IN_THE_KERNEL
2375 if (dsp_code != &static_dsp_code)
2376#endif
2377 release_firmware(dsp_code);
2378
1da177e4
LT
2379 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2380
9fd9156c
TI
2381 if (rc)
2382 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1da177e4
LT
2383
2384 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2385 snd_korg1212_free(korg1212);
2386 return err;
2387 }
2388
2389 snd_korg1212_EnableCardInterrupts(korg1212);
2390
2391 mdelay(CARD_BOOT_DELAY_IN_MS);
2392
2393 if (snd_korg1212_downloadDSPCode(korg1212))
2394 return -EBUSY;
2395
fcfd3332 2396 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
1da177e4
LT
2397 "PlayDataPhy = %08x L[%08x]\n"
2398 "korg1212: RecDataPhy = %08x L[%08x], "
2399 "VolumeTablePhy = %08x L[%08x]\n"
2400 "korg1212: RoutingTablePhy = %08x L[%08x], "
2401 "AdatTimeCodePhy = %08x L[%08x]\n",
2402 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2403 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2404 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2405 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2406 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2407 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2408
2409 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2410 return err;
2411
2412 korg1212->pcm->private_data = korg1212;
2413 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2414 strcpy(korg1212->pcm->name, "korg1212");
2415
2416 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2417
2418 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2419
2420 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2421
1da177e4
LT
2422 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2423 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2424 if (err < 0)
2425 return err;
2426 }
2427
2428 snd_korg1212_proc_init(korg1212);
2429
2430 snd_card_set_dev(card, &pci->dev);
2431
2432 * rchip = korg1212;
2433 return 0;
2434
2435}
2436
2437/*
2438 * Card initialisation
2439 */
2440
2441static int __devinit
2442snd_korg1212_probe(struct pci_dev *pci,
2443 const struct pci_device_id *pci_id)
2444{
2445 static int dev;
fcfd3332
TI
2446 struct snd_korg1212 *korg1212;
2447 struct snd_card *card;
1da177e4
LT
2448 int err;
2449
2450 if (dev >= SNDRV_CARDS) {
2451 return -ENODEV;
2452 }
2453 if (!enable[dev]) {
2454 dev++;
2455 return -ENOENT;
2456 }
2457 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2458 if (card == NULL)
2459 return -ENOMEM;
2460
2461 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2462 snd_card_free(card);
2463 return err;
2464 }
2465
2466 strcpy(card->driver, "korg1212");
2467 strcpy(card->shortname, "korg1212");
2468 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2469 korg1212->iomem, korg1212->irq);
2470
1da177e4 2471 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
1da177e4
LT
2472
2473 if ((err = snd_card_register(card)) < 0) {
2474 snd_card_free(card);
2475 return err;
2476 }
2477 pci_set_drvdata(pci, card);
2478 dev++;
2479 return 0;
2480}
2481
2482static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2483{
2484 snd_card_free(pci_get_drvdata(pci));
2485 pci_set_drvdata(pci, NULL);
2486}
2487
2488static struct pci_driver driver = {
2489 .name = "korg1212",
2490 .id_table = snd_korg1212_ids,
2491 .probe = snd_korg1212_probe,
2492 .remove = __devexit_p(snd_korg1212_remove),
2493};
2494
2495static int __init alsa_card_korg1212_init(void)
2496{
01d25d46 2497 return pci_register_driver(&driver);
1da177e4
LT
2498}
2499
2500static void __exit alsa_card_korg1212_exit(void)
2501{
2502 pci_unregister_driver(&driver);
2503}
2504
2505module_init(alsa_card_korg1212_init)
2506module_exit(alsa_card_korg1212_exit)