2 * Driver for the Korg 1212 IO PCI card
4 * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
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.
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.
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
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>
30 #include <linux/mutex.h>
31 #include <linux/firmware.h>
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>
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
45 #define K1212_DEBUG_LEVEL 0
46 #if K1212_DEBUG_LEVEL > 0
47 #define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
49 #define K1212_DEBUG_PRINTK(fmt,...)
51 #if K1212_DEBUG_LEVEL > 1
52 #define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
54 #define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
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
63 // ----------------------------------------------------------------------------
64 // Valid states of the Korg 1212 I/O card.
65 // ----------------------------------------------------------------------------
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
84 // ----------------------------------------------------------------------------
85 // The following enumeration defines the constants written to the card's
86 // host-to-card doorbell to initiate a command.
87 // ----------------------------------------------------------------------------
88 enum korg1212_dbcnst {
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
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
105 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
109 // ----------------------------------------------------------------------------
110 // The following enumeration defines return codes
111 // to the Korg 1212 I/O driver.
112 // ----------------------------------------------------------------------------
113 enum snd_korg1212rc {
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
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
129 K1212_CMDRET_BadDevice, // the specified wave device was out of range
130 K1212_CMDRET_BadFormat // the specified wave format is unsupported
133 // ----------------------------------------------------------------------------
134 // The following enumeration defines the constants used to select the play
135 // mode for the card in the SelectPlayMode command.
136 // ----------------------------------------------------------------------------
137 enum PlayModeSelector {
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
144 // ----------------------------------------------------------------------------
145 // The following enumeration defines the constants used to select the monitor
146 // mode for the card in the SetMonitorMode command.
147 // ----------------------------------------------------------------------------
148 enum MonitorModeSelector {
149 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
150 K1212_MONMODE_On // tells card to turn on monitor mode
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
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
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
173 #define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
175 #define CARD_BOOT_DELAY_IN_MS 10
176 #define CARD_BOOT_TIMEOUT 10
177 #define DSP_BOOT_DELAY_IN_MS 200
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
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
193 #define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
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)
203 #define k1212MinADCSens 0x7f
204 #define k1212MaxADCSens 0x00
205 #define k1212MaxVolume 0x7fff
206 #define k1212MaxWaveVolume 0xffff
207 #define k1212MinVolume 0x0000
208 #define k1212MaxVolInverted 0x8000
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
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
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
236 // ------------------------------------------------------------------------
237 // the following constants are used in setting the 1212 I/O card's input
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
245 #define SET_SENS_LEFTCHANID 0x00
246 #define SET_SENS_RIGHTCHANID 0x01
248 #define K1212SENSUPDATE_DELAY_IN_MS 50
250 // --------------------------------------------------------------------------
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.
267 #define FIRMWARE_IN_THE_KERNEL
269 #ifdef FIRMWARE_IN_THE_KERNEL
270 #include "korg1212-firmware.h"
271 static const struct firmware static_dsp_code = {
272 .data = (u8 *)dspCode,
273 .size = sizeof dspCode
277 enum ClockSourceIndex {
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
287 enum ClockSourceType {
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
293 struct 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 */
299 struct KorgAudioBuffer {
300 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
303 struct KorgSharedBuffer {
304 #ifdef K1212_LARGEALLOC
305 struct KorgAudioBuffer playDataBufs[kNumBuffers];
306 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
308 short volumeData[kAudioChannels];
310 u16 routeData [kAudioChannels];
311 u32 AdatTimeCode; // ADAT timecode value
317 unsigned int leftChanVal:8;
318 unsigned int leftChanId:8;
324 unsigned int rightChanVal:8;
325 unsigned int rightChanId:8;
331 struct snd_korg1212 {
332 struct snd_card *card;
338 struct mutex open_mutex;
340 struct timer_list timer; /* timer callback for checking ack of stop request */
341 int stop_pending_cnt; /* counter for stop pending check */
343 wait_queue_head_t wait;
346 unsigned long ioport;
347 unsigned long iomem2;
348 unsigned long irqcount;
350 void __iomem *iobase;
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;
359 struct KorgAudioBuffer * playDataBufsPtr;
360 struct KorgAudioBuffer * recordDataBufsPtr;
362 struct KorgSharedBuffer * sharedBufferPtr;
366 unsigned long sharedBufferPhy;
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
386 struct snd_pcm_substream *playback_substream;
387 struct snd_pcm_substream *capture_substream;
392 enum CardState cardState;
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.
397 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
399 enum ClockSourceType clkSource; // clock source
400 int clkRate; // clock rate
402 int volumePhase[kAudioChannels];
404 u16 leftADCInSens; // ADC left channel input sensitivity
405 u16 rightADCInSens; // ADC right channel input sensitivity
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
414 int dsp_stop_is_processed;
418 MODULE_DESCRIPTION("korg1212");
419 MODULE_LICENSE("GPL");
420 MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
422 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
423 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
424 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
426 module_param_array(index, int, NULL, 0444);
427 MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
428 module_param_array(id, charp, NULL, 0444);
429 MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
430 module_param_array(enable, bool, NULL, 0444);
431 MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
432 MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
434 static struct pci_device_id snd_korg1212_ids[] = {
438 .subvendor = PCI_ANY_ID,
439 .subdevice = PCI_ANY_ID,
444 MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
446 static char *stateName[] = {
449 "DSP download in process",
450 "DSP download complete",
460 static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
462 static char *clockSourceName[] = {
465 "S/PDIF at 44.1 kHz",
467 "local clock at 44.1 kHz",
468 "local clock at 48 kHz"
471 static char *channelName[] = {
486 static 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
495 union swap_u32 { unsigned char c[4]; u32 i; };
497 #ifdef SNDRV_BIG_ENDIAN
498 static u32 LowerWordSwap(u32 swappee)
500 static u32 UpperWordSwap(u32 swappee)
503 union swap_u32 retVal, swapper;
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];
514 #ifdef SNDRV_BIG_ENDIAN
515 static u32 UpperWordSwap(u32 swappee)
517 static u32 LowerWordSwap(u32 swappee)
520 union swap_u32 retVal, swapper;
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];
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)
536 static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
537 enum korg1212_dbcnst doorbellVal,
538 u32 mailBox0Val, u32 mailBox1Val,
539 u32 mailBox2Val, u32 mailBox3Val)
543 int rc = K1212_CMDRET_Success;
545 if (!korg1212->outDoorbellPtr) {
546 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
547 return K1212_CMDRET_CardUninitialized;
550 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
551 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
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
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;
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)) {
578 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
579 rc = K1212_CMDRET_Success;
584 korg1212->cmdRetryCount += retryCount;
586 if (retryCount >= MAX_COMMAND_RETRIES) {
587 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
588 rc = K1212_CMDRET_NoAckFromCard;
594 /* spinlock already held */
595 static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
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);
606 static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
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);
616 /* timer callback for checking the ack of stop request */
617 static void snd_korg1212_timer_func(unsigned long data)
619 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
622 spin_lock_irqsave(&korg1212->lock, flags);
623 if (korg1212->sharedBufferPtr->cardCommand == 0) {
625 korg1212->stop_pending_cnt = 0;
626 korg1212->dsp_stop_is_processed = 1;
627 wake_up(&korg1212->wait);
628 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
629 stateName[korg1212->cardState]);
631 if (--korg1212->stop_pending_cnt > 0) {
632 /* reprogram timer */
633 korg1212->timer.expires = jiffies + 1;
634 add_timer(&korg1212->timer);
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);
640 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
641 stateName[korg1212->cardState]);
644 spin_unlock_irqrestore(&korg1212->lock, flags);
647 static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
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);
661 static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
663 if (korg1212->idleMonitorOn) {
664 snd_korg1212_SendStopAndWait(korg1212);
665 korg1212->idleMonitorOn = 0;
669 static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
671 korg1212->cardState = csState;
674 static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
676 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
677 stateName[korg1212->cardState], korg1212->opencnt);
678 mutex_lock(&korg1212->open_mutex);
679 if (korg1212->opencnt++ == 0) {
680 snd_korg1212_TurnOffIdleMonitor(korg1212);
681 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
684 mutex_unlock(&korg1212->open_mutex);
688 static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
690 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
691 stateName[korg1212->cardState], korg1212->opencnt);
693 mutex_lock(&korg1212->open_mutex);
694 if (--(korg1212->opencnt)) {
695 mutex_unlock(&korg1212->open_mutex);
699 if (korg1212->cardState == K1212_STATE_SETUP) {
700 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
701 K1212_MODE_StopPlay, 0, 0, 0);
703 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
704 rc, stateName[korg1212->cardState]);
705 if (rc != K1212_CMDRET_Success) {
706 mutex_unlock(&korg1212->open_mutex);
709 } else if (korg1212->cardState > K1212_STATE_SETUP) {
710 snd_korg1212_SendStopAndWait(korg1212);
713 if (korg1212->cardState > K1212_STATE_READY) {
714 snd_korg1212_TurnOnIdleMonitor(korg1212);
715 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
718 mutex_unlock(&korg1212->open_mutex);
722 /* spinlock already held */
723 static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
727 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
728 stateName[korg1212->cardState], korg1212->setcnt);
730 if (korg1212->setcnt++)
733 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
734 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
735 K1212_MODE_SetupPlay, 0, 0, 0);
737 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
738 rc, stateName[korg1212->cardState]);
739 if (rc != K1212_CMDRET_Success) {
745 /* spinlock already held */
746 static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
750 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
751 stateName[korg1212->cardState], korg1212->playcnt);
753 if (korg1212->playcnt++)
756 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
757 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
759 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
760 rc, stateName[korg1212->cardState]);
761 if (rc != K1212_CMDRET_Success) {
767 /* spinlock already held */
768 static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
770 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
771 stateName[korg1212->cardState], korg1212->playcnt);
773 if (--(korg1212->playcnt))
776 korg1212->setcnt = 0;
778 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
779 snd_korg1212_SendStop(korg1212);
781 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
785 static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
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);
797 static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
798 enum MonitorModeSelector mode)
800 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
801 stateName[korg1212->cardState]);
804 case K1212_MONMODE_Off:
805 if (korg1212->cardState != K1212_STATE_MONITOR)
808 snd_korg1212_SendStopAndWait(korg1212);
809 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
813 case K1212_MONMODE_On:
814 if (korg1212->cardState != K1212_STATE_OPEN)
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)
833 #endif /* not used */
835 static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
837 if (korg1212->playback_pid != korg1212->capture_pid &&
838 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
844 static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
846 static enum ClockSourceIndex s44[] = {
847 K1212_CLKIDX_AdatAt44_1K,
848 K1212_CLKIDX_WordAt44_1K,
849 K1212_CLKIDX_LocalAt44_1K
851 static enum ClockSourceIndex s48[] = {
852 K1212_CLKIDX_AdatAt48K,
853 K1212_CLKIDX_WordAt48K,
854 K1212_CLKIDX_LocalAt48K
858 if (!snd_korg1212_use_is_exclusive (korg1212))
863 parm = s44[korg1212->clkSource];
867 parm = s48[korg1212->clkSource];
874 korg1212->clkSrcRate = parm;
875 korg1212->clkRate = rate;
877 udelay(INTERCOMMAND_DELAY);
878 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
879 ClockSourceSelector[korg1212->clkSrcRate],
882 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
883 rc, stateName[korg1212->cardState]);
888 static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
891 if (source < 0 || source > 2)
894 korg1212->clkSource = source;
896 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
901 static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
903 writel(0, korg1212->statusRegPtr);
906 static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
908 struct SensBits sensVals;
913 u16 controlValue; // this keeps the current value to be written to
914 // the card's eeprom control register.
918 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
919 stateName[korg1212->cardState]);
921 // ----------------------------------------------------------------------------
922 // initialize things. The local init bit is always set when writing to the
923 // card's control register.
924 // ----------------------------------------------------------------------------
926 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
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) {
933 snd_korg1212_SendStopAndWait(korg1212);
937 spin_lock_irqsave(&korg1212->lock, flags);
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);
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:
957 case K1212_CLKIDX_WordAt48K:
958 case K1212_CLKIDX_AdatAt48K:
959 case K1212_CLKIDX_LocalAt48K:
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;
973 // ----------------------------------------------------------------------------
974 // now start shifting the bits in. Start with the left channel then the right.
975 // ----------------------------------------------------------------------------
976 for (channel = 0; channel < 2; channel++) {
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);
987 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
989 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
990 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
992 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
994 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
995 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
997 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
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);
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);
1019 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
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);
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);
1039 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1040 K1212_MODE_MonitorOn, 0, 0, 0);
1042 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1043 rc, stateName[korg1212->cardState]);
1046 spin_unlock_irqrestore(&korg1212->lock, flags);
1051 static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
1055 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1056 stateName[korg1212->cardState]);
1058 // ----------------------------------------------------
1059 // tell the card to boot
1060 // ----------------------------------------------------
1061 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
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);
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
1081 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1082 rc, stateName[korg1212->cardState]);
1084 udelay(INTERCOMMAND_DELAY);
1086 rc = snd_korg1212_Send1212Command(korg1212,
1087 K1212_DB_ConfigureMiscMemory,
1088 LowerWordSwap(korg1212->VolumeTablePhy),
1089 LowerWordSwap(korg1212->RoutingTablePhy),
1090 LowerWordSwap(korg1212->AdatTimeCodePhy),
1095 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1096 rc, stateName[korg1212->cardState]);
1098 // --------------------------------------------------------------------------------
1099 // Initialize the routing and volume tables, then update the card's state.
1100 // --------------------------------------------------------------------------------
1101 udelay(INTERCOMMAND_DELAY);
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);
1109 snd_korg1212_WriteADCSensitivity(korg1212);
1111 udelay(INTERCOMMAND_DELAY);
1112 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1113 ClockSourceSelector[korg1212->clkSrcRate],
1116 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1117 rc, stateName[korg1212->cardState]);
1119 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
1120 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1123 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1124 rc, stateName[korg1212->cardState]);
1126 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1129 static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
1132 struct snd_korg1212 *korg1212 = dev_id;
1134 doorbellValue = readl(korg1212->inDoorbellPtr);
1139 spin_lock(&korg1212->lock);
1141 writel(doorbellValue, korg1212->inDoorbellPtr);
1143 korg1212->irqcount++;
1147 switch (doorbellValue) {
1148 case K1212_DB_DSPDownloadDone:
1149 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1150 korg1212->irqcount, doorbellValue,
1151 stateName[korg1212->cardState]);
1152 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1153 korg1212->dsp_is_loaded = 1;
1154 wake_up(&korg1212->wait);
1158 // ------------------------------------------------------------------------
1159 // an error occurred - stop the card
1160 // ------------------------------------------------------------------------
1161 case K1212_DB_DMAERROR:
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");
1166 korg1212->errorcnt++;
1167 korg1212->totalerrorcnt++;
1168 korg1212->sharedBufferPtr->cardCommand = 0;
1169 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
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:
1177 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
1178 korg1212->irqcount, doorbellValue,
1179 stateName[korg1212->cardState]);
1180 korg1212->sharedBufferPtr->cardCommand = 0;
1184 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
1185 korg1212->irqcount, doorbellValue,
1186 korg1212->currentBuffer, stateName[korg1212->cardState]);
1187 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1188 korg1212->currentBuffer++;
1190 if (korg1212->currentBuffer >= kNumBuffers)
1191 korg1212->currentBuffer = 0;
1193 if (!korg1212->running)
1196 if (korg1212->capture_substream) {
1197 spin_unlock(&korg1212->lock);
1198 snd_pcm_period_elapsed(korg1212->capture_substream);
1199 spin_lock(&korg1212->lock);
1202 if (korg1212->playback_substream) {
1203 spin_unlock(&korg1212->lock);
1204 snd_pcm_period_elapsed(korg1212->playback_substream);
1205 spin_lock(&korg1212->lock);
1213 spin_unlock(&korg1212->lock);
1218 static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
1222 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1223 stateName[korg1212->cardState]);
1225 // ---------------------------------------------------------------
1226 // verify the state of the card before proceeding.
1227 // ---------------------------------------------------------------
1228 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
1231 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1233 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1234 UpperWordSwap(korg1212->dma_dsp.addr),
1237 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1238 rc, stateName[korg1212->cardState]);
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 */
1245 snd_korg1212_OnDSPDownloadComplete(korg1212);
1250 static struct snd_pcm_hardware snd_korg1212_playback_info =
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),
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,
1270 static struct snd_pcm_hardware snd_korg1212_capture_info =
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),
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,
1290 static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
1292 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1295 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1296 pos, offset, size, count);
1297 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
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 ) {
1303 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1308 memset((void*) dst + offset, 0, size);
1315 static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
1317 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1320 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1322 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
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 ) {
1328 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1332 rc = copy_to_user(dst + offset, src, size);
1334 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1344 static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
1346 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
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);
1352 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
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 ) {
1358 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1362 rc = copy_from_user((void*) dst + offset, src, size);
1364 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1374 static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
1376 struct snd_korg1212 *korg1212 = pcm->private_data;
1378 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1379 stateName[korg1212->cardState]);
1381 korg1212->pcm = NULL;
1384 static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
1386 unsigned long flags;
1387 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1388 struct snd_pcm_runtime *runtime = substream->runtime;
1390 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1391 stateName[korg1212->cardState]);
1393 snd_pcm_set_sync(substream); // ???
1395 snd_korg1212_OpenCard(korg1212);
1397 runtime->hw = snd_korg1212_playback_info;
1398 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1400 spin_lock_irqsave(&korg1212->lock, flags);
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;
1408 spin_unlock_irqrestore(&korg1212->lock, flags);
1410 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1415 static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
1417 unsigned long flags;
1418 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1419 struct snd_pcm_runtime *runtime = substream->runtime;
1421 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1422 stateName[korg1212->cardState]);
1424 snd_pcm_set_sync(substream);
1426 snd_korg1212_OpenCard(korg1212);
1428 runtime->hw = snd_korg1212_capture_info;
1429 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1431 spin_lock_irqsave(&korg1212->lock, flags);
1433 korg1212->capture_substream = substream;
1434 korg1212->capture_pid = current->pid;
1435 korg1212->periodsize = K1212_PERIODS;
1436 korg1212->channels = K1212_CHANNELS;
1438 spin_unlock_irqrestore(&korg1212->lock, flags);
1440 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1441 kPlayBufferFrames, kPlayBufferFrames);
1445 static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
1447 unsigned long flags;
1448 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1450 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1451 stateName[korg1212->cardState]);
1453 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1455 spin_lock_irqsave(&korg1212->lock, flags);
1457 korg1212->playback_pid = -1;
1458 korg1212->playback_substream = NULL;
1459 korg1212->periodsize = 0;
1461 spin_unlock_irqrestore(&korg1212->lock, flags);
1463 snd_korg1212_CloseCard(korg1212);
1467 static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
1469 unsigned long flags;
1470 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1472 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1473 stateName[korg1212->cardState]);
1475 spin_lock_irqsave(&korg1212->lock, flags);
1477 korg1212->capture_pid = -1;
1478 korg1212->capture_substream = NULL;
1479 korg1212->periodsize = 0;
1481 spin_unlock_irqrestore(&korg1212->lock, flags);
1483 snd_korg1212_CloseCard(korg1212);
1487 static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
1488 unsigned int cmd, void *arg)
1490 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1492 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
1493 struct snd_pcm_channel_info *info = arg;
1495 info->first = info->channel * 16;
1497 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
1501 return snd_pcm_lib_ioctl(substream, cmd, arg);
1504 static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1505 struct snd_pcm_hw_params *params)
1507 unsigned long flags;
1508 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1513 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1514 stateName[korg1212->cardState]);
1516 spin_lock_irqsave(&korg1212->lock, flags);
1518 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1519 this_pid = korg1212->playback_pid;
1520 other_pid = korg1212->capture_pid;
1522 this_pid = korg1212->capture_pid;
1523 other_pid = korg1212->playback_pid;
1526 if ((other_pid > 0) && (this_pid != other_pid)) {
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.
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);
1539 spin_unlock_irqrestore(&korg1212->lock, flags);
1543 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1544 spin_unlock_irqrestore(&korg1212->lock, flags);
1548 korg1212->channels = params_channels(params);
1549 korg1212->periodsize = K1212_PERIOD_BYTES;
1551 spin_unlock_irqrestore(&korg1212->lock, flags);
1556 static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
1558 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1561 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1562 stateName[korg1212->cardState]);
1564 spin_lock_irq(&korg1212->lock);
1566 /* FIXME: we should wait for ack! */
1567 if (korg1212->stop_pending_cnt > 0) {
1568 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1569 stateName[korg1212->cardState]);
1570 spin_unlock_irq(&korg1212->lock);
1573 korg1212->sharedBufferPtr->cardCommand = 0;
1574 del_timer(&korg1212->timer);
1575 korg1212->stop_pending_cnt = 0;
1579 rc = snd_korg1212_SetupForPlay(korg1212);
1581 korg1212->currentBuffer = 0;
1583 spin_unlock_irq(&korg1212->lock);
1585 return rc ? -EINVAL : 0;
1588 static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
1591 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1594 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1595 stateName[korg1212->cardState], cmd);
1597 spin_lock(&korg1212->lock);
1599 case SNDRV_PCM_TRIGGER_START:
1601 if (korg1212->running) {
1602 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
1606 korg1212->running++;
1607 rc = snd_korg1212_TriggerPlay(korg1212);
1610 case SNDRV_PCM_TRIGGER_STOP:
1612 if (!korg1212->running) {
1613 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
1617 korg1212->running--;
1618 rc = snd_korg1212_StopPlay(korg1212);
1625 spin_unlock(&korg1212->lock);
1626 return rc ? -EINVAL : 0;
1629 static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
1631 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1632 snd_pcm_uframes_t pos;
1634 pos = korg1212->currentBuffer * kPlayBufferFrames;
1636 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1637 stateName[korg1212->cardState], pos);
1642 static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
1644 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1645 snd_pcm_uframes_t pos;
1647 pos = korg1212->currentBuffer * kPlayBufferFrames;
1649 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1650 stateName[korg1212->cardState], pos);
1655 static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
1656 int channel, /* not used (interleaved data) */
1657 snd_pcm_uframes_t pos,
1659 snd_pcm_uframes_t count)
1661 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1663 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1664 stateName[korg1212->cardState], pos, count);
1666 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1670 static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
1671 int channel, /* not used (interleaved data) */
1672 snd_pcm_uframes_t pos,
1673 snd_pcm_uframes_t count)
1675 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1677 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1678 stateName[korg1212->cardState]);
1680 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1683 static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
1684 int channel, /* not used (interleaved data) */
1685 snd_pcm_uframes_t pos,
1687 snd_pcm_uframes_t count)
1689 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1691 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1692 stateName[korg1212->cardState], pos, count);
1694 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1697 static struct snd_pcm_ops snd_korg1212_playback_ops = {
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,
1709 static struct snd_pcm_ops snd_korg1212_capture_ops = {
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,
1724 static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_info *uinfo)
1727 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1728 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1732 static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1733 struct snd_ctl_elem_value *u)
1735 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1736 int i = kcontrol->private_value;
1738 spin_lock_irq(&korg1212->lock);
1740 u->value.integer.value[0] = korg1212->volumePhase[i];
1743 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1745 spin_unlock_irq(&korg1212->lock);
1750 static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *u)
1753 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1757 spin_lock_irq(&korg1212->lock);
1759 i = kcontrol->private_value;
1761 korg1212->volumePhase[i] = u->value.integer.value[0];
1763 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
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;
1772 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1774 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
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;
1783 spin_unlock_irq(&korg1212->lock);
1788 static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_info *uinfo)
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;
1798 static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1799 struct snd_ctl_elem_value *u)
1801 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1804 spin_lock_irq(&korg1212->lock);
1806 i = kcontrol->private_value;
1807 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1810 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1812 spin_unlock_irq(&korg1212->lock);
1817 static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1818 struct snd_ctl_elem_value *u)
1820 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1825 spin_lock_irq(&korg1212->lock);
1827 i = kcontrol->private_value;
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;
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;
1845 spin_unlock_irq(&korg1212->lock);
1850 static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1851 struct snd_ctl_elem_info *uinfo)
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;
1859 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1863 static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1864 struct snd_ctl_elem_value *u)
1866 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1869 spin_lock_irq(&korg1212->lock);
1871 i = kcontrol->private_value;
1872 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1875 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1877 spin_unlock_irq(&korg1212->lock);
1882 static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_value *u)
1885 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1888 spin_lock_irq(&korg1212->lock);
1890 i = kcontrol->private_value;
1892 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1893 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1898 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1899 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1904 spin_unlock_irq(&korg1212->lock);
1909 static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_info *uinfo)
1912 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1914 uinfo->value.integer.min = k1212MaxADCSens;
1915 uinfo->value.integer.max = k1212MinADCSens;
1919 static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_value *u)
1922 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1924 spin_lock_irq(&korg1212->lock);
1926 u->value.integer.value[0] = korg1212->leftADCInSens;
1927 u->value.integer.value[1] = korg1212->rightADCInSens;
1929 spin_unlock_irq(&korg1212->lock);
1934 static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *u)
1937 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1940 spin_lock_irq(&korg1212->lock);
1942 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1943 korg1212->leftADCInSens = u->value.integer.value[0];
1946 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1947 korg1212->rightADCInSens = u->value.integer.value[1];
1951 spin_unlock_irq(&korg1212->lock);
1954 snd_korg1212_WriteADCSensitivity(korg1212);
1959 static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_info *uinfo)
1962 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1964 uinfo->value.enumerated.items = 3;
1965 if (uinfo->value.enumerated.item > 2) {
1966 uinfo->value.enumerated.item = 2;
1968 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1972 static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1973 struct snd_ctl_elem_value *ucontrol)
1975 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1977 spin_lock_irq(&korg1212->lock);
1979 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1981 spin_unlock_irq(&korg1212->lock);
1985 static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
1988 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
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);
2000 #define MON_MIXER(ord,c_name) \
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, \
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, \
2020 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
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, \
2029 static struct snd_kcontrol_new snd_korg1212_controls[] = {
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"),
2035 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2036 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
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,
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,
2056 static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2057 struct snd_info_buffer *buffer)
2060 struct snd_korg1212 *korg1212 = entry->private_data;
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,
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);
2084 static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
2086 struct snd_info_entry *entry;
2088 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2089 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
2093 snd_korg1212_free(struct snd_korg1212 *korg1212)
2095 snd_korg1212_TurnOffIdleMonitor(korg1212);
2097 if (korg1212->irq >= 0) {
2098 synchronize_irq(korg1212->irq);
2099 snd_korg1212_DisableCardInterrupts(korg1212);
2100 free_irq(korg1212->irq, korg1212);
2104 if (korg1212->iobase != NULL) {
2105 iounmap(korg1212->iobase);
2106 korg1212->iobase = NULL;
2109 pci_release_regions(korg1212->pci);
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;
2119 #ifndef K1212_LARGEALLOC
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;
2129 if (korg1212->dma_rec.area) {
2130 snd_dma_free_pages(&korg1212->dma_rec);
2131 korg1212->dma_rec.area = NULL;
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;
2144 pci_disable_device(korg1212->pci);
2149 static int snd_korg1212_dev_free(struct snd_device *device)
2151 struct snd_korg1212 *korg1212 = device->device_data;
2152 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
2153 return snd_korg1212_free(korg1212);
2156 static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2157 struct snd_korg1212 ** rchip)
2162 unsigned ioport_size, iomem_size, iomem2_size;
2163 struct snd_korg1212 * korg1212;
2164 const struct firmware *dsp_code;
2166 static struct snd_device_ops ops = {
2167 .dev_free = snd_korg1212_dev_free,
2171 if ((err = pci_enable_device(pci)) < 0)
2174 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
2175 if (korg1212 == NULL) {
2176 pci_disable_device(pci);
2180 korg1212->card = card;
2181 korg1212->pci = pci;
2183 init_waitqueue_head(&korg1212->wait);
2184 spin_lock_init(&korg1212->lock);
2185 mutex_init(&korg1212->open_mutex);
2186 init_timer(&korg1212->timer);
2187 korg1212->timer.function = snd_korg1212_timer_func;
2188 korg1212->timer.data = (unsigned long)korg1212;
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;
2207 for (i=0; i<kAudioChannels; i++)
2208 korg1212->volumePhase[i] = 0;
2210 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2212 pci_disable_device(pci);
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);
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);
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"
2229 korg1212->iomem, iomem_size,
2230 korg1212->ioport, ioport_size,
2231 korg1212->iomem2, iomem2_size,
2232 stateName[korg1212->cardState]);
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);
2241 err = request_irq(pci->irq, snd_korg1212_interrupt,
2243 "korg1212", korg1212);
2246 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2247 snd_korg1212_free(korg1212);
2251 korg1212->irq = pci->irq;
2253 pci_set_master(korg1212->pci);
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);
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"
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,
2288 stateName[korg1212->cardState]);
2290 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
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));
2293 snd_korg1212_free(korg1212);
2296 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
2297 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2299 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
2301 #ifndef K1212_LARGEALLOC
2303 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
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);
2311 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
2312 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2314 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2315 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
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);
2323 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
2324 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2326 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2327 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
2329 #else // K1212_LARGEALLOC
2331 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2332 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
2333 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2334 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
2336 #endif // K1212_LARGEALLOC
2338 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
2339 offsetof(struct KorgSharedBuffer, volumeData);
2340 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
2341 offsetof(struct KorgSharedBuffer, routeData);
2342 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
2343 offsetof(struct KorgSharedBuffer, AdatTimeCode);
2345 err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
2347 release_firmware(dsp_code);
2348 #ifdef FIRMWARE_IN_THE_KERNEL
2349 dsp_code = &static_dsp_code;
2351 snd_printk(KERN_ERR "firmware not available\n");
2352 snd_korg1212_free(korg1212);
2357 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2358 dsp_code->size, &korg1212->dma_dsp) < 0) {
2359 snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
2360 snd_korg1212_free(korg1212);
2361 #ifdef FIRMWARE_IN_THE_KERNEL
2362 if (dsp_code != &static_dsp_code)
2364 release_firmware(dsp_code);
2368 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2369 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
2370 stateName[korg1212->cardState]);
2372 memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
2374 #ifdef FIRMWARE_IN_THE_KERNEL
2375 if (dsp_code != &static_dsp_code)
2377 release_firmware(dsp_code);
2379 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2382 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
2384 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2385 snd_korg1212_free(korg1212);
2389 snd_korg1212_EnableCardInterrupts(korg1212);
2391 mdelay(CARD_BOOT_DELAY_IN_MS);
2393 if (snd_korg1212_downloadDSPCode(korg1212))
2396 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
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));
2409 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2412 korg1212->pcm->private_data = korg1212;
2413 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2414 strcpy(korg1212->pcm->name, "korg1212");
2416 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2418 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2420 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
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));
2428 snd_korg1212_proc_init(korg1212);
2430 snd_card_set_dev(card, &pci->dev);
2438 * Card initialisation
2441 static int __devinit
2442 snd_korg1212_probe(struct pci_dev *pci,
2443 const struct pci_device_id *pci_id)
2446 struct snd_korg1212 *korg1212;
2447 struct snd_card *card;
2450 if (dev >= SNDRV_CARDS) {
2457 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2461 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2462 snd_card_free(card);
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);
2471 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
2473 if ((err = snd_card_register(card)) < 0) {
2474 snd_card_free(card);
2477 pci_set_drvdata(pci, card);
2482 static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2484 snd_card_free(pci_get_drvdata(pci));
2485 pci_set_drvdata(pci, NULL);
2488 static struct pci_driver driver = {
2490 .id_table = snd_korg1212_ids,
2491 .probe = snd_korg1212_probe,
2492 .remove = __devexit_p(snd_korg1212_remove),
2495 static int __init alsa_card_korg1212_init(void)
2497 return pci_register_driver(&driver);
2500 static void __exit alsa_card_korg1212_exit(void)
2502 pci_unregister_driver(&driver);
2505 module_init(alsa_card_korg1212_init)
2506 module_exit(alsa_card_korg1212_exit)