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>
31 #include <sound/core.h>
32 #include <sound/info.h>
33 #include <sound/control.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/initval.h>
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
43 #define K1212_DEBUG_LEVEL 0
44 #if K1212_DEBUG_LEVEL > 0
45 #define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
47 #define K1212_DEBUG_PRINTK(fmt,...)
49 #if K1212_DEBUG_LEVEL > 1
50 #define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
52 #define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
55 // ----------------------------------------------------------------------------
56 // Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
57 // buffers are alocated as a large piece inside KorgSharedBuffer.
58 // ----------------------------------------------------------------------------
59 //#define K1212_LARGEALLOC 1
61 // ----------------------------------------------------------------------------
62 // Valid states of the Korg 1212 I/O card.
63 // ----------------------------------------------------------------------------
65 K1212_STATE_NONEXISTENT, // there is no card here
66 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
67 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
68 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
69 K1212_STATE_READY, // the card can be opened by an application. Any application
70 // requests prior to this state should fail. Only an open
71 // request can be made at this state.
72 K1212_STATE_OPEN, // an application has opened the card
73 K1212_STATE_SETUP, // the card has been setup for play
74 K1212_STATE_PLAYING, // the card is playing
75 K1212_STATE_MONITOR, // the card is in the monitor mode
76 K1212_STATE_CALIBRATING, // the card is currently calibrating
77 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
78 // are in the process of cleaning things up.
79 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
82 // ----------------------------------------------------------------------------
83 // The following enumeration defines the constants written to the card's
84 // host-to-card doorbell to initiate a command.
85 // ----------------------------------------------------------------------------
86 enum korg1212_dbcnst {
87 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
88 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
89 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
90 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
91 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
92 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
93 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
94 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
96 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
97 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
98 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
99 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
100 // on page 4 (local page to card).
101 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
103 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
107 // ----------------------------------------------------------------------------
108 // The following enumeration defines return codes
109 // to the Korg 1212 I/O driver.
110 // ----------------------------------------------------------------------------
111 enum snd_korg1212rc {
112 K1212_CMDRET_Success = 0, // command was successfully placed
113 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
114 K1212_CMDRET_PMFailure, // the protected mode call failed
115 K1212_CMDRET_FailUnspecified, // unspecified failure
116 K1212_CMDRET_FailBadState, // the specified command can not be given in
117 // the card's current state. (or the wave device's
119 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
120 K1212_CMDRET_BadIndex, // an out of range card index was specified
121 K1212_CMDRET_BadHandle, // an invalid card handle was specified
122 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
123 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
124 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
125 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
127 K1212_CMDRET_BadDevice, // the specified wave device was out of range
128 K1212_CMDRET_BadFormat // the specified wave format is unsupported
131 // ----------------------------------------------------------------------------
132 // The following enumeration defines the constants used to select the play
133 // mode for the card in the SelectPlayMode command.
134 // ----------------------------------------------------------------------------
135 enum PlayModeSelector {
136 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
137 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
138 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
139 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
142 // ----------------------------------------------------------------------------
143 // The following enumeration defines the constants used to select the monitor
144 // mode for the card in the SetMonitorMode command.
145 // ----------------------------------------------------------------------------
146 enum MonitorModeSelector {
147 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
148 K1212_MONMODE_On // tells card to turn on monitor mode
151 #define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
152 #define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
153 #define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
154 #define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
155 #define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
156 #define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
157 #define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
158 #define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
160 #define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
161 // this is the upper word of the PCI control reg.
162 #define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
164 #define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
165 // from the card after sending a command.
166 #define INTERCOMMAND_DELAY 40
167 #define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
168 // to send a command before giving up.
169 #define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
171 #define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
173 #define CARD_BOOT_DELAY_IN_MS 10
174 #define CARD_BOOT_TIMEOUT 10
175 #define DSP_BOOT_DELAY_IN_MS 200
177 #define kNumBuffers 8
178 #define k1212MaxCards 4
179 #define k1212NumWaveDevices 6
180 #define k16BitChannels 10
181 #define k32BitChannels 2
182 #define kAudioChannels (k16BitChannels + k32BitChannels)
183 #define kPlayBufferFrames 1024
185 #define K1212_ANALOG_CHANNELS 2
186 #define K1212_SPDIF_CHANNELS 2
187 #define K1212_ADAT_CHANNELS 8
188 #define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
189 #define K1212_MIN_CHANNELS 1
190 #define K1212_MAX_CHANNELS K1212_CHANNELS
191 #define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
192 #define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
193 #define K1212_PERIODS (kNumBuffers)
194 #define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
195 #define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
196 #define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
197 #define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
198 #define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
199 #define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
201 #define k1212MinADCSens 0x7f
202 #define k1212MaxADCSens 0x00
203 #define k1212MaxVolume 0x7fff
204 #define k1212MaxWaveVolume 0xffff
205 #define k1212MinVolume 0x0000
206 #define k1212MaxVolInverted 0x8000
208 // -----------------------------------------------------------------
209 // the following bits are used for controlling interrupts in the
210 // interrupt control/status reg
211 // -----------------------------------------------------------------
212 #define PCI_INT_ENABLE_BIT 0x00000100
213 #define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
214 #define LOCAL_INT_ENABLE_BIT 0x00010000
215 #define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
216 #define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
218 // -----------------------------------------------------------------
219 // the following bits are defined for the PCI command register
220 // -----------------------------------------------------------------
221 #define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
222 #define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
223 #define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
225 // -----------------------------------------------------------------
226 // the following bits are defined for the PCI status register
227 // -----------------------------------------------------------------
228 #define PCI_STAT_PARITY_ERROR_BIT 0x8000
229 #define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
230 #define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
231 #define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
232 #define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
234 // ------------------------------------------------------------------------
235 // the following constants are used in setting the 1212 I/O card's input
237 // ------------------------------------------------------------------------
238 #define SET_SENS_LOCALINIT_BITPOS 15
239 #define SET_SENS_DATA_BITPOS 10
240 #define SET_SENS_CLOCK_BITPOS 8
241 #define SET_SENS_LOADSHIFT_BITPOS 0
243 #define SET_SENS_LEFTCHANID 0x00
244 #define SET_SENS_RIGHTCHANID 0x01
246 #define K1212SENSUPDATE_DELAY_IN_MS 50
248 // --------------------------------------------------------------------------
251 // This function waits the specified number of real time clock ticks.
252 // According to the DDK, each tick is ~0.8 microseconds.
253 // The defines following the function declaration can be used for the
254 // numTicksToWait parameter.
255 // --------------------------------------------------------------------------
256 #define ONE_RTC_TICK 1
257 #define SENSCLKPULSE_WIDTH 4
258 #define LOADSHIFT_DELAY 4
259 #define INTERCOMMAND_DELAY 40
260 #define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
261 // the command register. (could be up to 180 us)
262 #define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
263 // from the card after sending a command.
265 #include "korg1212-firmware.h"
267 enum ClockSourceIndex {
268 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
269 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
270 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
271 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
272 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
273 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
274 K1212_CLKIDX_Invalid // used to check validity of the index
277 enum ClockSourceType {
278 K1212_CLKIDX_Adat = 0, // selects source as ADAT
279 K1212_CLKIDX_Word, // selects source as S/PDIF
280 K1212_CLKIDX_Local // selects source as local clock
283 struct KorgAudioFrame {
284 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
285 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
286 u32 timeCodeVal; /* holds the ADAT timecode value */
289 struct KorgAudioBuffer {
290 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
293 struct KorgSharedBuffer {
294 #ifdef K1212_LARGEALLOC
295 struct KorgAudioBuffer playDataBufs[kNumBuffers];
296 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
298 short volumeData[kAudioChannels];
300 u16 routeData [kAudioChannels];
301 u32 AdatTimeCode; // ADAT timecode value
307 unsigned int leftChanVal:8;
308 unsigned int leftChanId:8;
314 unsigned int rightChanVal:8;
315 unsigned int rightChanId:8;
321 struct snd_korg1212 {
322 struct snd_card *card;
328 struct semaphore open_mutex;
330 struct timer_list timer; /* timer callback for checking ack of stop request */
331 int stop_pending_cnt; /* counter for stop pending check */
333 wait_queue_head_t wait;
336 unsigned long ioport;
337 unsigned long iomem2;
338 unsigned long irqcount;
340 void __iomem *iobase;
342 struct snd_dma_buffer dma_dsp;
343 struct snd_dma_buffer dma_play;
344 struct snd_dma_buffer dma_rec;
345 struct snd_dma_buffer dma_shared;
351 struct KorgAudioBuffer * playDataBufsPtr;
352 struct KorgAudioBuffer * recordDataBufsPtr;
354 struct KorgSharedBuffer * sharedBufferPtr;
358 unsigned long sharedBufferPhy;
363 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
364 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
365 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
366 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
367 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
368 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
369 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
370 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
371 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
372 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
378 struct snd_pcm_substream *playback_substream;
379 struct snd_pcm_substream *capture_substream;
384 enum CardState cardState;
386 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
387 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
389 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
391 enum ClockSourceType clkSource; // clock source
392 int clkRate; // clock rate
394 int volumePhase[kAudioChannels];
396 u16 leftADCInSens; // ADC left channel input sensitivity
397 u16 rightADCInSens; // ADC right channel input sensitivity
399 int opencnt; // Open/Close count
400 int setcnt; // SetupForPlay count
401 int playcnt; // TriggerPlay count
402 int errorcnt; // Error Count
403 unsigned long totalerrorcnt; // Total Error Count
406 int dsp_stop_is_processed;
410 MODULE_DESCRIPTION("korg1212");
411 MODULE_LICENSE("GPL");
412 MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
414 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
415 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
416 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
418 module_param_array(index, int, NULL, 0444);
419 MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
420 module_param_array(id, charp, NULL, 0444);
421 MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
422 module_param_array(enable, bool, NULL, 0444);
423 MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
424 MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
426 static struct pci_device_id snd_korg1212_ids[] = {
430 .subvendor = PCI_ANY_ID,
431 .subdevice = PCI_ANY_ID,
436 MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
438 static char *stateName[] = {
441 "DSP download in process",
442 "DSP download complete",
452 static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
454 static char *clockSourceName[] = {
457 "S/PDIF at 44.1 kHz",
459 "local clock at 44.1 kHz",
460 "local clock at 48 kHz"
463 static char *channelName[] = {
478 static u16 ClockSourceSelector[] = {
479 0x8000, // selects source as ADAT at 44.1 kHz
480 0x0000, // selects source as ADAT at 48 kHz
481 0x8001, // selects source as S/PDIF at 44.1 kHz
482 0x0001, // selects source as S/PDIF at 48 kHz
483 0x8002, // selects source as local clock at 44.1 kHz
484 0x0002 // selects source as local clock at 48 kHz
487 union swap_u32 { unsigned char c[4]; u32 i; };
489 #ifdef SNDRV_BIG_ENDIAN
490 static u32 LowerWordSwap(u32 swappee)
492 static u32 UpperWordSwap(u32 swappee)
495 union swap_u32 retVal, swapper;
498 retVal.c[2] = swapper.c[3];
499 retVal.c[3] = swapper.c[2];
500 retVal.c[1] = swapper.c[1];
501 retVal.c[0] = swapper.c[0];
506 #ifdef SNDRV_BIG_ENDIAN
507 static u32 UpperWordSwap(u32 swappee)
509 static u32 LowerWordSwap(u32 swappee)
512 union swap_u32 retVal, swapper;
515 retVal.c[2] = swapper.c[2];
516 retVal.c[3] = swapper.c[3];
517 retVal.c[1] = swapper.c[0];
518 retVal.c[0] = swapper.c[1];
523 #define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
524 #define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
525 #define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
526 #define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
528 static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
529 enum korg1212_dbcnst doorbellVal,
530 u32 mailBox0Val, u32 mailBox1Val,
531 u32 mailBox2Val, u32 mailBox3Val)
535 int rc = K1212_CMDRET_Success;
537 if (!korg1212->outDoorbellPtr) {
538 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
539 return K1212_CMDRET_CardUninitialized;
542 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
543 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
544 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
545 writel(mailBox3Val, korg1212->mailbox3Ptr);
546 writel(mailBox2Val, korg1212->mailbox2Ptr);
547 writel(mailBox1Val, korg1212->mailbox1Ptr);
548 writel(mailBox0Val, korg1212->mailbox0Ptr);
549 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
551 // --------------------------------------------------------------
552 // the reboot command will not give an acknowledgement.
553 // --------------------------------------------------------------
554 if ( doorbellVal == K1212_DB_RebootCard ||
555 doorbellVal == K1212_DB_BootFromDSPPage4 ||
556 doorbellVal == K1212_DB_StartDSPDownload ) {
557 rc = K1212_CMDRET_Success;
561 // --------------------------------------------------------------
562 // See if the card acknowledged the command. Wait a bit, then
563 // read in the low word of mailbox3. If the MSB is set and the
564 // low byte is equal to the doorbell value, then it ack'd.
565 // --------------------------------------------------------------
566 udelay(COMMAND_ACK_DELAY);
567 mailBox3Lo = readl(korg1212->mailbox3Ptr);
568 if (mailBox3Lo & COMMAND_ACK_MASK) {
569 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
570 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
571 rc = K1212_CMDRET_Success;
576 korg1212->cmdRetryCount += retryCount;
578 if (retryCount >= MAX_COMMAND_RETRIES) {
579 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
580 rc = K1212_CMDRET_NoAckFromCard;
586 /* spinlock already held */
587 static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
589 if (! korg1212->stop_pending_cnt) {
590 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
591 /* program the timer */
592 korg1212->stop_pending_cnt = HZ;
593 korg1212->timer.expires = jiffies + 1;
594 add_timer(&korg1212->timer);
598 static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
601 spin_lock_irqsave(&korg1212->lock, flags);
602 korg1212->dsp_stop_is_processed = 0;
603 snd_korg1212_SendStop(korg1212);
604 spin_unlock_irqrestore(&korg1212->lock, flags);
605 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
608 /* timer callback for checking the ack of stop request */
609 static void snd_korg1212_timer_func(unsigned long data)
611 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
613 spin_lock(&korg1212->lock);
614 if (korg1212->sharedBufferPtr->cardCommand == 0) {
616 korg1212->stop_pending_cnt = 0;
617 korg1212->dsp_stop_is_processed = 1;
618 wake_up(&korg1212->wait);
619 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
620 stateName[korg1212->cardState]);
622 if (--korg1212->stop_pending_cnt > 0) {
623 /* reprogram timer */
624 korg1212->timer.expires = jiffies + 1;
625 add_timer(&korg1212->timer);
627 snd_printd("korg1212_timer_func timeout\n");
628 korg1212->sharedBufferPtr->cardCommand = 0;
629 korg1212->dsp_stop_is_processed = 1;
630 wake_up(&korg1212->wait);
631 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
632 stateName[korg1212->cardState]);
635 spin_unlock(&korg1212->lock);
638 static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
643 udelay(INTERCOMMAND_DELAY);
644 spin_lock_irqsave(&korg1212->lock, flags);
645 korg1212->idleMonitorOn = 1;
646 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
647 K1212_MODE_MonitorOn, 0, 0, 0);
648 spin_unlock_irqrestore(&korg1212->lock, flags);
652 static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
654 if (korg1212->idleMonitorOn) {
655 snd_korg1212_SendStopAndWait(korg1212);
656 korg1212->idleMonitorOn = 0;
660 static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
662 korg1212->cardState = csState;
665 static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
667 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
668 stateName[korg1212->cardState], korg1212->opencnt);
669 down(&korg1212->open_mutex);
670 if (korg1212->opencnt++ == 0) {
671 snd_korg1212_TurnOffIdleMonitor(korg1212);
672 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
675 up(&korg1212->open_mutex);
679 static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
681 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
682 stateName[korg1212->cardState], korg1212->opencnt);
684 down(&korg1212->open_mutex);
685 if (--(korg1212->opencnt)) {
686 up(&korg1212->open_mutex);
690 if (korg1212->cardState == K1212_STATE_SETUP) {
691 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
692 K1212_MODE_StopPlay, 0, 0, 0);
694 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
695 rc, stateName[korg1212->cardState]);
696 if (rc != K1212_CMDRET_Success) {
697 up(&korg1212->open_mutex);
700 } else if (korg1212->cardState > K1212_STATE_SETUP) {
701 snd_korg1212_SendStopAndWait(korg1212);
704 if (korg1212->cardState > K1212_STATE_READY) {
705 snd_korg1212_TurnOnIdleMonitor(korg1212);
706 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
709 up(&korg1212->open_mutex);
713 /* spinlock already held */
714 static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
718 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
719 stateName[korg1212->cardState], korg1212->setcnt);
721 if (korg1212->setcnt++)
724 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
725 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
726 K1212_MODE_SetupPlay, 0, 0, 0);
728 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
729 rc, stateName[korg1212->cardState]);
730 if (rc != K1212_CMDRET_Success) {
736 /* spinlock already held */
737 static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
741 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
742 stateName[korg1212->cardState], korg1212->playcnt);
744 if (korg1212->playcnt++)
747 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
748 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
750 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
751 rc, stateName[korg1212->cardState]);
752 if (rc != K1212_CMDRET_Success) {
758 /* spinlock already held */
759 static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
761 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
762 stateName[korg1212->cardState], korg1212->playcnt);
764 if (--(korg1212->playcnt))
767 korg1212->setcnt = 0;
769 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
770 snd_korg1212_SendStop(korg1212);
772 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
776 static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
778 writel(PCI_INT_ENABLE_BIT |
779 PCI_DOORBELL_INT_ENABLE_BIT |
780 LOCAL_INT_ENABLE_BIT |
781 LOCAL_DOORBELL_INT_ENABLE_BIT |
782 LOCAL_DMA1_INT_ENABLE_BIT,
783 korg1212->statusRegPtr);
788 static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
789 enum MonitorModeSelector mode)
791 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
792 stateName[korg1212->cardState]);
795 case K1212_MONMODE_Off:
796 if (korg1212->cardState != K1212_STATE_MONITOR)
799 snd_korg1212_SendStopAndWait(korg1212);
800 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
804 case K1212_MONMODE_On:
805 if (korg1212->cardState != K1212_STATE_OPEN)
809 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
810 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
811 K1212_MODE_MonitorOn, 0, 0, 0);
812 if (rc != K1212_CMDRET_Success)
824 #endif /* not used */
826 static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
828 if (korg1212->playback_pid != korg1212->capture_pid &&
829 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
835 static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
837 static enum ClockSourceIndex s44[] = {
838 K1212_CLKIDX_AdatAt44_1K,
839 K1212_CLKIDX_WordAt44_1K,
840 K1212_CLKIDX_LocalAt44_1K
842 static enum ClockSourceIndex s48[] = {
843 K1212_CLKIDX_AdatAt48K,
844 K1212_CLKIDX_WordAt48K,
845 K1212_CLKIDX_LocalAt48K
849 if (!snd_korg1212_use_is_exclusive (korg1212))
854 parm = s44[korg1212->clkSource];
858 parm = s48[korg1212->clkSource];
865 korg1212->clkSrcRate = parm;
866 korg1212->clkRate = rate;
868 udelay(INTERCOMMAND_DELAY);
869 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
870 ClockSourceSelector[korg1212->clkSrcRate],
873 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
874 rc, stateName[korg1212->cardState]);
879 static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
882 if (source < 0 || source > 2)
885 korg1212->clkSource = source;
887 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
892 static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
894 writel(0, korg1212->statusRegPtr);
897 static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
899 struct SensBits sensVals;
904 u16 controlValue; // this keeps the current value to be written to
905 // the card's eeprom control register.
909 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
910 stateName[korg1212->cardState]);
912 // ----------------------------------------------------------------------------
913 // initialize things. The local init bit is always set when writing to the
914 // card's control register.
915 // ----------------------------------------------------------------------------
917 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
919 // ----------------------------------------------------------------------------
920 // make sure the card is not in monitor mode when we do this update.
921 // ----------------------------------------------------------------------------
922 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
924 snd_korg1212_SendStopAndWait(korg1212);
928 spin_lock_irqsave(&korg1212->lock, flags);
930 // ----------------------------------------------------------------------------
931 // we are about to send new values to the card, so clear the new values queued
932 // flag. Also, clear out mailbox 3, so we don't lockup.
933 // ----------------------------------------------------------------------------
934 writel(0, korg1212->mailbox3Ptr);
935 udelay(LOADSHIFT_DELAY);
937 // ----------------------------------------------------------------------------
938 // determine whether we are running a 48K or 44.1K clock. This info is used
939 // later when setting the SPDIF FF after the volume has been shifted in.
940 // ----------------------------------------------------------------------------
941 switch (korg1212->clkSrcRate) {
942 case K1212_CLKIDX_AdatAt44_1K:
943 case K1212_CLKIDX_WordAt44_1K:
944 case K1212_CLKIDX_LocalAt44_1K:
948 case K1212_CLKIDX_WordAt48K:
949 case K1212_CLKIDX_AdatAt48K:
950 case K1212_CLKIDX_LocalAt48K:
956 // ----------------------------------------------------------------------------
957 // start the update. Setup the bit structure and then shift the bits.
958 // ----------------------------------------------------------------------------
959 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
960 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
961 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
962 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
964 // ----------------------------------------------------------------------------
965 // now start shifting the bits in. Start with the left channel then the right.
966 // ----------------------------------------------------------------------------
967 for (channel = 0; channel < 2; channel++) {
969 // ----------------------------------------------------------------------------
970 // Bring the load/shift line low, then wait - the spec says >150ns from load/
971 // shift low to the first rising edge of the clock.
972 // ----------------------------------------------------------------------------
973 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
974 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
975 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
976 udelay(LOADSHIFT_DELAY);
978 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
980 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
981 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
983 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
985 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
986 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
988 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
991 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
992 writew(controlValue, korg1212->sensRegPtr); // clock goes low
993 udelay(SENSCLKPULSE_WIDTH);
994 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
995 writew(controlValue, korg1212->sensRegPtr); // clock goes high
996 udelay(SENSCLKPULSE_WIDTH);
999 // ----------------------------------------------------------------------------
1000 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1001 // bit if the clock rate is 48K otherwise write 0.
1002 // ----------------------------------------------------------------------------
1003 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1004 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1005 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1006 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1007 udelay(SENSCLKPULSE_WIDTH);
1010 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1012 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1013 udelay(ONE_RTC_TICK);
1014 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1015 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1016 udelay(SENSCLKPULSE_WIDTH);
1017 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1018 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1019 udelay(SENSCLKPULSE_WIDTH);
1022 // ----------------------------------------------------------------------------
1023 // The update is complete. Set a timeout. This is the inter-update delay.
1024 // Also, if the card was in monitor mode, restore it.
1025 // ----------------------------------------------------------------------------
1026 for (count = 0; count < 10; count++)
1027 udelay(SENSCLKPULSE_WIDTH);
1030 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1031 K1212_MODE_MonitorOn, 0, 0, 0);
1033 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1034 rc, stateName[korg1212->cardState]);
1037 spin_unlock_irqrestore(&korg1212->lock, flags);
1042 static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
1046 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1047 stateName[korg1212->cardState]);
1049 // ----------------------------------------------------
1050 // tell the card to boot
1051 // ----------------------------------------------------
1052 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1055 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1056 rc, stateName[korg1212->cardState]);
1057 msleep(DSP_BOOT_DELAY_IN_MS);
1059 // --------------------------------------------------------------------------------
1060 // Let the card know where all the buffers are.
1061 // --------------------------------------------------------------------------------
1062 rc = snd_korg1212_Send1212Command(korg1212,
1063 K1212_DB_ConfigureBufferMemory,
1064 LowerWordSwap(korg1212->PlayDataPhy),
1065 LowerWordSwap(korg1212->RecDataPhy),
1066 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1067 // is based on 2 buffers
1072 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1073 rc, stateName[korg1212->cardState]);
1075 udelay(INTERCOMMAND_DELAY);
1077 rc = snd_korg1212_Send1212Command(korg1212,
1078 K1212_DB_ConfigureMiscMemory,
1079 LowerWordSwap(korg1212->VolumeTablePhy),
1080 LowerWordSwap(korg1212->RoutingTablePhy),
1081 LowerWordSwap(korg1212->AdatTimeCodePhy),
1086 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1087 rc, stateName[korg1212->cardState]);
1089 // --------------------------------------------------------------------------------
1090 // Initialize the routing and volume tables, then update the card's state.
1091 // --------------------------------------------------------------------------------
1092 udelay(INTERCOMMAND_DELAY);
1094 for (channel = 0; channel < kAudioChannels; channel++) {
1095 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1096 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1097 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1100 snd_korg1212_WriteADCSensitivity(korg1212);
1102 udelay(INTERCOMMAND_DELAY);
1103 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1104 ClockSourceSelector[korg1212->clkSrcRate],
1107 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1108 rc, stateName[korg1212->cardState]);
1110 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
1111 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1114 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1115 rc, stateName[korg1212->cardState]);
1117 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1120 static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1123 struct snd_korg1212 *korg1212 = dev_id;
1125 if(irq != korg1212->irq)
1128 doorbellValue = readl(korg1212->inDoorbellPtr);
1133 spin_lock(&korg1212->lock);
1135 writel(doorbellValue, korg1212->inDoorbellPtr);
1137 korg1212->irqcount++;
1142 switch (doorbellValue) {
1143 case K1212_DB_DSPDownloadDone:
1144 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1145 korg1212->irqcount, doorbellValue,
1146 stateName[korg1212->cardState]);
1147 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1148 korg1212->dsp_is_loaded = 1;
1149 wake_up(&korg1212->wait);
1153 // ------------------------------------------------------------------------
1154 // an error occurred - stop the card
1155 // ------------------------------------------------------------------------
1156 case K1212_DB_DMAERROR:
1157 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1158 korg1212->irqcount, doorbellValue,
1159 stateName[korg1212->cardState]);
1160 snd_printk(KERN_ERR "korg1212: DMA Error\n");
1161 korg1212->errorcnt++;
1162 korg1212->totalerrorcnt++;
1163 korg1212->sharedBufferPtr->cardCommand = 0;
1164 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1167 // ------------------------------------------------------------------------
1168 // the card has stopped by our request. Clear the command word and signal
1169 // the semaphore in case someone is waiting for this.
1170 // ------------------------------------------------------------------------
1171 case K1212_DB_CARDSTOPPED:
1172 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
1173 korg1212->irqcount, doorbellValue,
1174 stateName[korg1212->cardState]);
1175 korg1212->sharedBufferPtr->cardCommand = 0;
1179 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
1180 korg1212->irqcount, doorbellValue,
1181 korg1212->currentBuffer, stateName[korg1212->cardState]);
1182 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1183 korg1212->currentBuffer++;
1185 if (korg1212->currentBuffer >= kNumBuffers)
1186 korg1212->currentBuffer = 0;
1188 if (!korg1212->running)
1191 if (korg1212->capture_substream) {
1192 spin_unlock(&korg1212->lock);
1193 snd_pcm_period_elapsed(korg1212->capture_substream);
1194 spin_lock(&korg1212->lock);
1197 if (korg1212->playback_substream) {
1198 spin_unlock(&korg1212->lock);
1199 snd_pcm_period_elapsed(korg1212->playback_substream);
1200 spin_lock(&korg1212->lock);
1208 spin_unlock(&korg1212->lock);
1213 static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
1217 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1218 stateName[korg1212->cardState]);
1220 // ---------------------------------------------------------------
1221 // verify the state of the card before proceeding.
1222 // ---------------------------------------------------------------
1223 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
1226 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1228 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1230 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1231 UpperWordSwap(korg1212->dma_dsp.addr),
1234 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1235 rc, stateName[korg1212->cardState]);
1237 korg1212->dsp_is_loaded = 0;
1238 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1239 if (! korg1212->dsp_is_loaded )
1240 return -EBUSY; /* timeout */
1242 snd_korg1212_OnDSPDownloadComplete(korg1212);
1247 static struct snd_pcm_hardware snd_korg1212_playback_info =
1249 .info = (SNDRV_PCM_INFO_MMAP |
1250 SNDRV_PCM_INFO_MMAP_VALID |
1251 SNDRV_PCM_INFO_INTERLEAVED),
1252 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1253 .rates = (SNDRV_PCM_RATE_44100 |
1254 SNDRV_PCM_RATE_48000),
1257 .channels_min = K1212_MIN_CHANNELS,
1258 .channels_max = K1212_MAX_CHANNELS,
1259 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1260 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1261 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1262 .periods_min = K1212_PERIODS,
1263 .periods_max = K1212_PERIODS,
1267 static struct snd_pcm_hardware snd_korg1212_capture_info =
1269 .info = (SNDRV_PCM_INFO_MMAP |
1270 SNDRV_PCM_INFO_MMAP_VALID |
1271 SNDRV_PCM_INFO_INTERLEAVED),
1272 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1273 .rates = (SNDRV_PCM_RATE_44100 |
1274 SNDRV_PCM_RATE_48000),
1277 .channels_min = K1212_MIN_CHANNELS,
1278 .channels_max = K1212_MAX_CHANNELS,
1279 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1280 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1281 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1282 .periods_min = K1212_PERIODS,
1283 .periods_max = K1212_PERIODS,
1287 static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
1289 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1292 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1293 pos, offset, size, count);
1294 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1296 for (i=0; i < count; i++) {
1297 #if K1212_DEBUG_LEVEL > 0
1298 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1299 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1300 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1305 memset((void*) dst + offset, 0, size);
1312 static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
1314 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1317 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1319 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1321 for (i=0; i < count; i++) {
1322 #if K1212_DEBUG_LEVEL > 0
1323 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1324 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
1325 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1329 rc = copy_to_user(dst + offset, src, size);
1331 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1341 static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
1343 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1346 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1347 pos, offset, size, count);
1349 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1351 for (i=0; i < count; i++) {
1352 #if K1212_DEBUG_LEVEL > 0
1353 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1354 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1355 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1359 rc = copy_from_user((void*) dst + offset, src, size);
1361 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1371 static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
1373 struct snd_korg1212 *korg1212 = pcm->private_data;
1375 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1376 stateName[korg1212->cardState]);
1378 korg1212->pcm = NULL;
1381 static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
1383 unsigned long flags;
1384 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1385 struct snd_pcm_runtime *runtime = substream->runtime;
1387 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1388 stateName[korg1212->cardState]);
1390 snd_pcm_set_sync(substream); // ???
1392 snd_korg1212_OpenCard(korg1212);
1394 runtime->hw = snd_korg1212_playback_info;
1395 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1397 spin_lock_irqsave(&korg1212->lock, flags);
1399 korg1212->playback_substream = substream;
1400 korg1212->playback_pid = current->pid;
1401 korg1212->periodsize = K1212_PERIODS;
1402 korg1212->channels = K1212_CHANNELS;
1403 korg1212->errorcnt = 0;
1405 spin_unlock_irqrestore(&korg1212->lock, flags);
1407 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1412 static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
1414 unsigned long flags;
1415 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1416 struct snd_pcm_runtime *runtime = substream->runtime;
1418 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1419 stateName[korg1212->cardState]);
1421 snd_pcm_set_sync(substream);
1423 snd_korg1212_OpenCard(korg1212);
1425 runtime->hw = snd_korg1212_capture_info;
1426 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1428 spin_lock_irqsave(&korg1212->lock, flags);
1430 korg1212->capture_substream = substream;
1431 korg1212->capture_pid = current->pid;
1432 korg1212->periodsize = K1212_PERIODS;
1433 korg1212->channels = K1212_CHANNELS;
1435 spin_unlock_irqrestore(&korg1212->lock, flags);
1437 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1438 kPlayBufferFrames, kPlayBufferFrames);
1442 static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
1444 unsigned long flags;
1445 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1447 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1448 stateName[korg1212->cardState]);
1450 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1452 spin_lock_irqsave(&korg1212->lock, flags);
1454 korg1212->playback_pid = -1;
1455 korg1212->playback_substream = NULL;
1456 korg1212->periodsize = 0;
1458 spin_unlock_irqrestore(&korg1212->lock, flags);
1460 snd_korg1212_CloseCard(korg1212);
1464 static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
1466 unsigned long flags;
1467 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1469 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1470 stateName[korg1212->cardState]);
1472 spin_lock_irqsave(&korg1212->lock, flags);
1474 korg1212->capture_pid = -1;
1475 korg1212->capture_substream = NULL;
1476 korg1212->periodsize = 0;
1478 spin_unlock_irqrestore(&korg1212->lock, flags);
1480 snd_korg1212_CloseCard(korg1212);
1484 static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
1485 unsigned int cmd, void *arg)
1487 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1489 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
1490 struct snd_pcm_channel_info *info = arg;
1492 info->first = info->channel * 16;
1494 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
1498 return snd_pcm_lib_ioctl(substream, cmd, arg);
1501 static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1502 struct snd_pcm_hw_params *params)
1504 unsigned long flags;
1505 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1510 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1511 stateName[korg1212->cardState]);
1513 spin_lock_irqsave(&korg1212->lock, flags);
1515 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1516 this_pid = korg1212->playback_pid;
1517 other_pid = korg1212->capture_pid;
1519 this_pid = korg1212->capture_pid;
1520 other_pid = korg1212->playback_pid;
1523 if ((other_pid > 0) && (this_pid != other_pid)) {
1525 /* The other stream is open, and not by the same
1526 task as this one. Make sure that the parameters
1527 that matter are the same.
1530 if ((int)params_rate(params) != korg1212->clkRate) {
1531 spin_unlock_irqrestore(&korg1212->lock, flags);
1532 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1536 spin_unlock_irqrestore(&korg1212->lock, flags);
1540 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1541 spin_unlock_irqrestore(&korg1212->lock, flags);
1545 korg1212->channels = params_channels(params);
1546 korg1212->periodsize = K1212_PERIOD_BYTES;
1548 spin_unlock_irqrestore(&korg1212->lock, flags);
1553 static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
1555 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1558 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1559 stateName[korg1212->cardState]);
1561 spin_lock_irq(&korg1212->lock);
1563 /* FIXME: we should wait for ack! */
1564 if (korg1212->stop_pending_cnt > 0) {
1565 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1566 stateName[korg1212->cardState]);
1567 spin_unlock_irq(&korg1212->lock);
1570 korg1212->sharedBufferPtr->cardCommand = 0;
1571 del_timer(&korg1212->timer);
1572 korg1212->stop_pending_cnt = 0;
1576 rc = snd_korg1212_SetupForPlay(korg1212);
1578 korg1212->currentBuffer = 0;
1580 spin_unlock_irq(&korg1212->lock);
1582 return rc ? -EINVAL : 0;
1585 static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
1588 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1591 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1592 stateName[korg1212->cardState], cmd);
1594 spin_lock(&korg1212->lock);
1596 case SNDRV_PCM_TRIGGER_START:
1598 if (korg1212->running) {
1599 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
1603 korg1212->running++;
1604 rc = snd_korg1212_TriggerPlay(korg1212);
1607 case SNDRV_PCM_TRIGGER_STOP:
1609 if (!korg1212->running) {
1610 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
1614 korg1212->running--;
1615 rc = snd_korg1212_StopPlay(korg1212);
1622 spin_unlock(&korg1212->lock);
1623 return rc ? -EINVAL : 0;
1626 static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
1628 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1629 snd_pcm_uframes_t pos;
1631 pos = korg1212->currentBuffer * kPlayBufferFrames;
1633 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1634 stateName[korg1212->cardState], pos);
1639 static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
1641 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1642 snd_pcm_uframes_t pos;
1644 pos = korg1212->currentBuffer * kPlayBufferFrames;
1646 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1647 stateName[korg1212->cardState], pos);
1652 static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
1653 int channel, /* not used (interleaved data) */
1654 snd_pcm_uframes_t pos,
1656 snd_pcm_uframes_t count)
1658 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1660 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1661 stateName[korg1212->cardState], pos, count);
1663 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1667 static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
1668 int channel, /* not used (interleaved data) */
1669 snd_pcm_uframes_t pos,
1670 snd_pcm_uframes_t count)
1672 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1674 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1675 stateName[korg1212->cardState]);
1677 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1680 static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
1681 int channel, /* not used (interleaved data) */
1682 snd_pcm_uframes_t pos,
1684 snd_pcm_uframes_t count)
1686 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1688 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1689 stateName[korg1212->cardState], pos, count);
1691 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1694 static struct snd_pcm_ops snd_korg1212_playback_ops = {
1695 .open = snd_korg1212_playback_open,
1696 .close = snd_korg1212_playback_close,
1697 .ioctl = snd_korg1212_ioctl,
1698 .hw_params = snd_korg1212_hw_params,
1699 .prepare = snd_korg1212_prepare,
1700 .trigger = snd_korg1212_trigger,
1701 .pointer = snd_korg1212_playback_pointer,
1702 .copy = snd_korg1212_playback_copy,
1703 .silence = snd_korg1212_playback_silence,
1706 static struct snd_pcm_ops snd_korg1212_capture_ops = {
1707 .open = snd_korg1212_capture_open,
1708 .close = snd_korg1212_capture_close,
1709 .ioctl = snd_korg1212_ioctl,
1710 .hw_params = snd_korg1212_hw_params,
1711 .prepare = snd_korg1212_prepare,
1712 .trigger = snd_korg1212_trigger,
1713 .pointer = snd_korg1212_capture_pointer,
1714 .copy = snd_korg1212_capture_copy,
1721 static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_info *uinfo)
1724 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1725 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1729 static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *u)
1732 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1733 int i = kcontrol->private_value;
1735 spin_lock_irq(&korg1212->lock);
1737 u->value.integer.value[0] = korg1212->volumePhase[i];
1740 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1742 spin_unlock_irq(&korg1212->lock);
1747 static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *u)
1750 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1754 spin_lock_irq(&korg1212->lock);
1756 i = kcontrol->private_value;
1758 korg1212->volumePhase[i] = u->value.integer.value[0];
1760 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1762 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1763 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1764 korg1212->sharedBufferPtr->volumeData[i] = val;
1769 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1771 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1773 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1774 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1775 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1780 spin_unlock_irq(&korg1212->lock);
1785 static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1786 struct snd_ctl_elem_info *uinfo)
1788 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1789 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1790 uinfo->value.integer.min = k1212MinVolume;
1791 uinfo->value.integer.max = k1212MaxVolume;
1795 static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *u)
1798 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1801 spin_lock_irq(&korg1212->lock);
1803 i = kcontrol->private_value;
1804 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1807 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1809 spin_unlock_irq(&korg1212->lock);
1814 static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *u)
1817 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1822 spin_lock_irq(&korg1212->lock);
1824 i = kcontrol->private_value;
1826 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1827 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1828 val *= u->value.integer.value[0];
1829 korg1212->sharedBufferPtr->volumeData[i] = val;
1834 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1835 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1836 val *= u->value.integer.value[1];
1837 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1842 spin_unlock_irq(&korg1212->lock);
1847 static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_info *uinfo)
1850 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1851 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1852 uinfo->value.enumerated.items = kAudioChannels;
1853 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1854 uinfo->value.enumerated.item = kAudioChannels-1;
1856 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1860 static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_value *u)
1863 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1866 spin_lock_irq(&korg1212->lock);
1868 i = kcontrol->private_value;
1869 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1872 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1874 spin_unlock_irq(&korg1212->lock);
1879 static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1880 struct snd_ctl_elem_value *u)
1882 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1885 spin_lock_irq(&korg1212->lock);
1887 i = kcontrol->private_value;
1889 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1890 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1895 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1896 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1901 spin_unlock_irq(&korg1212->lock);
1906 static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_info *uinfo)
1909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1911 uinfo->value.integer.min = k1212MaxADCSens;
1912 uinfo->value.integer.max = k1212MinADCSens;
1916 static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1917 struct snd_ctl_elem_value *u)
1919 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1921 spin_lock_irq(&korg1212->lock);
1923 u->value.integer.value[0] = korg1212->leftADCInSens;
1924 u->value.integer.value[1] = korg1212->rightADCInSens;
1926 spin_unlock_irq(&korg1212->lock);
1931 static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_value *u)
1934 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1937 spin_lock_irq(&korg1212->lock);
1939 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1940 korg1212->leftADCInSens = u->value.integer.value[0];
1943 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1944 korg1212->rightADCInSens = u->value.integer.value[1];
1948 spin_unlock_irq(&korg1212->lock);
1951 snd_korg1212_WriteADCSensitivity(korg1212);
1956 static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
1959 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1961 uinfo->value.enumerated.items = 3;
1962 if (uinfo->value.enumerated.item > 2) {
1963 uinfo->value.enumerated.item = 2;
1965 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1969 static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_value *ucontrol)
1972 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1974 spin_lock_irq(&korg1212->lock);
1976 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1978 spin_unlock_irq(&korg1212->lock);
1982 static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1983 struct snd_ctl_elem_value *ucontrol)
1985 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1989 val = ucontrol->value.enumerated.item[0] % 3;
1990 spin_lock_irq(&korg1212->lock);
1991 change = val != korg1212->clkSource;
1992 snd_korg1212_SetClockSource(korg1212, val);
1993 spin_unlock_irq(&korg1212->lock);
1997 #define MON_MIXER(ord,c_name) \
1999 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2000 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2001 .name = c_name " Monitor Volume", \
2002 .info = snd_korg1212_control_volume_info, \
2003 .get = snd_korg1212_control_volume_get, \
2004 .put = snd_korg1212_control_volume_put, \
2005 .private_value = ord, \
2008 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2009 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2010 .name = c_name " Monitor Route", \
2011 .info = snd_korg1212_control_route_info, \
2012 .get = snd_korg1212_control_route_get, \
2013 .put = snd_korg1212_control_route_put, \
2014 .private_value = ord, \
2017 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2018 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2019 .name = c_name " Monitor Phase Invert", \
2020 .info = snd_korg1212_control_phase_info, \
2021 .get = snd_korg1212_control_phase_get, \
2022 .put = snd_korg1212_control_phase_put, \
2023 .private_value = ord, \
2026 static struct snd_kcontrol_new snd_korg1212_controls[] = {
2027 MON_MIXER(8, "Analog"),
2028 MON_MIXER(10, "SPDIF"),
2029 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2030 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2032 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2034 .name = "Sync Source",
2035 .info = snd_korg1212_control_sync_info,
2036 .get = snd_korg1212_control_sync_get,
2037 .put = snd_korg1212_control_sync_put,
2040 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2041 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2042 .name = "ADC Attenuation",
2043 .info = snd_korg1212_control_info,
2044 .get = snd_korg1212_control_get,
2045 .put = snd_korg1212_control_put,
2053 static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2054 struct snd_info_buffer *buffer)
2057 struct snd_korg1212 *korg1212 = entry->private_data;
2059 snd_iprintf(buffer, korg1212->card->longname);
2060 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2061 snd_iprintf(buffer, "\nGeneral settings\n");
2062 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2063 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2064 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2065 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2066 snd_iprintf(buffer, " Volume Info:\n");
2067 for (n=0; n<kAudioChannels; n++)
2068 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2070 channelName[korg1212->sharedBufferPtr->routeData[n]],
2071 korg1212->sharedBufferPtr->volumeData[n]);
2072 snd_iprintf(buffer, "\nGeneral status\n");
2073 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2074 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2075 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2076 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2077 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2078 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2081 static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
2083 struct snd_info_entry *entry;
2085 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2086 snd_info_set_text_ops(entry, korg1212, 1024, snd_korg1212_proc_read);
2090 snd_korg1212_free(struct snd_korg1212 *korg1212)
2092 snd_korg1212_TurnOffIdleMonitor(korg1212);
2094 if (korg1212->irq >= 0) {
2095 synchronize_irq(korg1212->irq);
2096 snd_korg1212_DisableCardInterrupts(korg1212);
2097 free_irq(korg1212->irq, korg1212);
2101 if (korg1212->iobase != NULL) {
2102 iounmap(korg1212->iobase);
2103 korg1212->iobase = NULL;
2106 pci_release_regions(korg1212->pci);
2108 // ----------------------------------------------------
2109 // free up memory resources used for the DSP download.
2110 // ----------------------------------------------------
2111 if (korg1212->dma_dsp.area) {
2112 snd_dma_free_pages(&korg1212->dma_dsp);
2113 korg1212->dma_dsp.area = NULL;
2116 #ifndef K1212_LARGEALLOC
2118 // ------------------------------------------------------
2119 // free up memory resources used for the Play/Rec Buffers
2120 // ------------------------------------------------------
2121 if (korg1212->dma_play.area) {
2122 snd_dma_free_pages(&korg1212->dma_play);
2123 korg1212->dma_play.area = NULL;
2126 if (korg1212->dma_rec.area) {
2127 snd_dma_free_pages(&korg1212->dma_rec);
2128 korg1212->dma_rec.area = NULL;
2133 // ----------------------------------------------------
2134 // free up memory resources used for the Shared Buffers
2135 // ----------------------------------------------------
2136 if (korg1212->dma_shared.area) {
2137 snd_dma_free_pages(&korg1212->dma_shared);
2138 korg1212->dma_shared.area = NULL;
2141 pci_disable_device(korg1212->pci);
2146 static int snd_korg1212_dev_free(struct snd_device *device)
2148 struct snd_korg1212 *korg1212 = device->device_data;
2149 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
2150 return snd_korg1212_free(korg1212);
2153 static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2154 struct snd_korg1212 ** rchip)
2159 unsigned ioport_size, iomem_size, iomem2_size;
2160 struct snd_korg1212 * korg1212;
2162 static struct snd_device_ops ops = {
2163 .dev_free = snd_korg1212_dev_free,
2167 if ((err = pci_enable_device(pci)) < 0)
2170 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
2171 if (korg1212 == NULL) {
2172 pci_disable_device(pci);
2176 korg1212->card = card;
2177 korg1212->pci = pci;
2179 init_waitqueue_head(&korg1212->wait);
2180 spin_lock_init(&korg1212->lock);
2181 init_MUTEX(&korg1212->open_mutex);
2182 init_timer(&korg1212->timer);
2183 korg1212->timer.function = snd_korg1212_timer_func;
2184 korg1212->timer.data = (unsigned long)korg1212;
2187 korg1212->clkSource = K1212_CLKIDX_Local;
2188 korg1212->clkRate = 44100;
2189 korg1212->inIRQ = 0;
2190 korg1212->running = 0;
2191 korg1212->opencnt = 0;
2192 korg1212->playcnt = 0;
2193 korg1212->setcnt = 0;
2194 korg1212->totalerrorcnt = 0;
2195 korg1212->playback_pid = -1;
2196 korg1212->capture_pid = -1;
2197 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2198 korg1212->idleMonitorOn = 0;
2199 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2200 korg1212->leftADCInSens = k1212MaxADCSens;
2201 korg1212->rightADCInSens = k1212MaxADCSens;
2203 for (i=0; i<kAudioChannels; i++)
2204 korg1212->volumePhase[i] = 0;
2206 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2208 pci_disable_device(pci);
2212 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2213 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2214 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2216 iomem_size = pci_resource_len(korg1212->pci, 0);
2217 ioport_size = pci_resource_len(korg1212->pci, 1);
2218 iomem2_size = pci_resource_len(korg1212->pci, 2);
2220 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2221 " iomem = 0x%lx (%d)\n"
2222 " ioport = 0x%lx (%d)\n"
2223 " iomem = 0x%lx (%d)\n"
2225 korg1212->iomem, iomem_size,
2226 korg1212->ioport, ioport_size,
2227 korg1212->iomem2, iomem2_size,
2228 stateName[korg1212->cardState]);
2230 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2231 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2232 korg1212->iomem + iomem_size - 1);
2233 snd_korg1212_free(korg1212);
2237 err = request_irq(pci->irq, snd_korg1212_interrupt,
2238 SA_INTERRUPT|SA_SHIRQ,
2239 "korg1212", korg1212);
2242 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2243 snd_korg1212_free(korg1212);
2247 korg1212->irq = pci->irq;
2249 pci_set_master(korg1212->pci);
2251 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2252 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2253 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2254 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2255 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2256 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2257 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2258 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2259 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2260 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2262 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2263 " Status register = 0x%p\n"
2264 " OutDoorbell = 0x%p\n"
2265 " InDoorbell = 0x%p\n"
2266 " Mailbox0 = 0x%p\n"
2267 " Mailbox1 = 0x%p\n"
2268 " Mailbox2 = 0x%p\n"
2269 " Mailbox3 = 0x%p\n"
2270 " ControlReg = 0x%p\n"
2274 korg1212->statusRegPtr,
2275 korg1212->outDoorbellPtr,
2276 korg1212->inDoorbellPtr,
2277 korg1212->mailbox0Ptr,
2278 korg1212->mailbox1Ptr,
2279 korg1212->mailbox2Ptr,
2280 korg1212->mailbox3Ptr,
2281 korg1212->controlRegPtr,
2282 korg1212->sensRegPtr,
2284 stateName[korg1212->cardState]);
2286 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2287 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2288 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
2289 snd_korg1212_free(korg1212);
2292 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
2293 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2295 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
2297 #ifndef K1212_LARGEALLOC
2299 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
2301 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2302 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2303 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2304 snd_korg1212_free(korg1212);
2307 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
2308 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2310 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2311 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
2313 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2314 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2315 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2316 snd_korg1212_free(korg1212);
2319 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
2320 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2322 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2323 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
2325 #else // K1212_LARGEALLOC
2327 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2328 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
2329 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2330 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
2332 #endif // K1212_LARGEALLOC
2334 korg1212->dspCodeSize = sizeof (dspCode);
2336 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
2337 offsetof(struct KorgSharedBuffer, volumeData);
2338 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
2339 offsetof(struct KorgSharedBuffer, routeData);
2340 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
2341 offsetof(struct KorgSharedBuffer, AdatTimeCode);
2343 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2344 korg1212->dspCodeSize, &korg1212->dma_dsp) < 0) {
2345 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", korg1212->dspCodeSize);
2346 snd_korg1212_free(korg1212);
2350 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2351 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, korg1212->dspCodeSize,
2352 stateName[korg1212->cardState]);
2354 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2357 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
2359 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2360 snd_korg1212_free(korg1212);
2364 snd_korg1212_EnableCardInterrupts(korg1212);
2366 mdelay(CARD_BOOT_DELAY_IN_MS);
2368 if (snd_korg1212_downloadDSPCode(korg1212))
2371 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
2372 "PlayDataPhy = %08x L[%08x]\n"
2373 "korg1212: RecDataPhy = %08x L[%08x], "
2374 "VolumeTablePhy = %08x L[%08x]\n"
2375 "korg1212: RoutingTablePhy = %08x L[%08x], "
2376 "AdatTimeCodePhy = %08x L[%08x]\n",
2377 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2378 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2379 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2380 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2381 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2382 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2384 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2387 korg1212->pcm->private_data = korg1212;
2388 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2389 strcpy(korg1212->pcm->name, "korg1212");
2391 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2393 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2395 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2397 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2398 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2403 snd_korg1212_proc_init(korg1212);
2405 snd_card_set_dev(card, &pci->dev);
2413 * Card initialisation
2416 static int __devinit
2417 snd_korg1212_probe(struct pci_dev *pci,
2418 const struct pci_device_id *pci_id)
2421 struct snd_korg1212 *korg1212;
2422 struct snd_card *card;
2425 if (dev >= SNDRV_CARDS) {
2432 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2436 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2437 snd_card_free(card);
2441 strcpy(card->driver, "korg1212");
2442 strcpy(card->shortname, "korg1212");
2443 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2444 korg1212->iomem, korg1212->irq);
2446 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
2448 if ((err = snd_card_register(card)) < 0) {
2449 snd_card_free(card);
2452 pci_set_drvdata(pci, card);
2457 static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2459 snd_card_free(pci_get_drvdata(pci));
2460 pci_set_drvdata(pci, NULL);
2463 static struct pci_driver driver = {
2465 .id_table = snd_korg1212_ids,
2466 .probe = snd_korg1212_probe,
2467 .remove = __devexit_p(snd_korg1212_remove),
2470 static int __init alsa_card_korg1212_init(void)
2472 return pci_register_driver(&driver);
2475 static void __exit alsa_card_korg1212_exit(void)
2477 pci_unregister_driver(&driver);
2480 module_init(alsa_card_korg1212_init)
2481 module_exit(alsa_card_korg1212_exit)