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 MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
167 // to send a command before giving up.
168 #define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
170 #define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
172 #define CARD_BOOT_DELAY_IN_MS 10
173 #define CARD_BOOT_TIMEOUT 10
174 #define DSP_BOOT_DELAY_IN_MS 200
176 #define kNumBuffers 8
177 #define k1212MaxCards 4
178 #define k1212NumWaveDevices 6
179 #define k16BitChannels 10
180 #define k32BitChannels 2
181 #define kAudioChannels (k16BitChannels + k32BitChannels)
182 #define kPlayBufferFrames 1024
184 #define K1212_ANALOG_CHANNELS 2
185 #define K1212_SPDIF_CHANNELS 2
186 #define K1212_ADAT_CHANNELS 8
187 #define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
188 #define K1212_MIN_CHANNELS 1
189 #define K1212_MAX_CHANNELS K1212_CHANNELS
190 #define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
191 #define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
192 #define K1212_PERIODS (kNumBuffers)
193 #define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
194 #define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
195 #define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
196 #define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
197 #define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
198 #define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
200 #define k1212MinADCSens 0x7f
201 #define k1212MaxADCSens 0x00
202 #define k1212MaxVolume 0x7fff
203 #define k1212MaxWaveVolume 0xffff
204 #define k1212MinVolume 0x0000
205 #define k1212MaxVolInverted 0x8000
207 // -----------------------------------------------------------------
208 // the following bits are used for controlling interrupts in the
209 // interrupt control/status reg
210 // -----------------------------------------------------------------
211 #define PCI_INT_ENABLE_BIT 0x00000100
212 #define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
213 #define LOCAL_INT_ENABLE_BIT 0x00010000
214 #define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
215 #define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
217 // -----------------------------------------------------------------
218 // the following bits are defined for the PCI command register
219 // -----------------------------------------------------------------
220 #define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
221 #define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
222 #define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
224 // -----------------------------------------------------------------
225 // the following bits are defined for the PCI status register
226 // -----------------------------------------------------------------
227 #define PCI_STAT_PARITY_ERROR_BIT 0x8000
228 #define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
229 #define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
230 #define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
231 #define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
233 // ------------------------------------------------------------------------
234 // the following constants are used in setting the 1212 I/O card's input
236 // ------------------------------------------------------------------------
237 #define SET_SENS_LOCALINIT_BITPOS 15
238 #define SET_SENS_DATA_BITPOS 10
239 #define SET_SENS_CLOCK_BITPOS 8
240 #define SET_SENS_LOADSHIFT_BITPOS 0
242 #define SET_SENS_LEFTCHANID 0x00
243 #define SET_SENS_RIGHTCHANID 0x01
245 #define K1212SENSUPDATE_DELAY_IN_MS 50
247 // --------------------------------------------------------------------------
250 // This function waits the specified number of real time clock ticks.
251 // According to the DDK, each tick is ~0.8 microseconds.
252 // The defines following the function declaration can be used for the
253 // numTicksToWait parameter.
254 // --------------------------------------------------------------------------
255 #define ONE_RTC_TICK 1
256 #define SENSCLKPULSE_WIDTH 4
257 #define LOADSHIFT_DELAY 4
258 #define INTERCOMMAND_DELAY 40
259 #define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
260 // the command register. (could be up to 180 us)
261 #define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
262 // from the card after sending a command.
264 #ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
265 #include "korg1212-firmware.h"
266 static const struct firmware static_dsp_code = {
267 .data = (u8 *)dspCode,
268 .size = sizeof dspCode
272 enum ClockSourceIndex {
273 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
274 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
275 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
276 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
277 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
278 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
279 K1212_CLKIDX_Invalid // used to check validity of the index
282 enum ClockSourceType {
283 K1212_CLKIDX_Adat = 0, // selects source as ADAT
284 K1212_CLKIDX_Word, // selects source as S/PDIF
285 K1212_CLKIDX_Local // selects source as local clock
288 struct KorgAudioFrame {
289 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
290 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
291 u32 timeCodeVal; /* holds the ADAT timecode value */
294 struct KorgAudioBuffer {
295 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
298 struct KorgSharedBuffer {
299 #ifdef K1212_LARGEALLOC
300 struct KorgAudioBuffer playDataBufs[kNumBuffers];
301 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
303 short volumeData[kAudioChannels];
305 u16 routeData [kAudioChannels];
306 u32 AdatTimeCode; // ADAT timecode value
312 unsigned int leftChanVal:8;
313 unsigned int leftChanId:8;
319 unsigned int rightChanVal:8;
320 unsigned int rightChanId:8;
326 struct snd_korg1212 {
327 struct snd_card *card;
333 struct mutex open_mutex;
335 struct timer_list timer; /* timer callback for checking ack of stop request */
336 int stop_pending_cnt; /* counter for stop pending check */
338 wait_queue_head_t wait;
341 unsigned long ioport;
342 unsigned long iomem2;
343 unsigned long irqcount;
345 void __iomem *iobase;
347 struct snd_dma_buffer dma_dsp;
348 struct snd_dma_buffer dma_play;
349 struct snd_dma_buffer dma_rec;
350 struct snd_dma_buffer dma_shared;
354 struct KorgAudioBuffer * playDataBufsPtr;
355 struct KorgAudioBuffer * recordDataBufsPtr;
357 struct KorgSharedBuffer * sharedBufferPtr;
361 unsigned long sharedBufferPhy;
366 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
367 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
368 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
369 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
370 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
371 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
372 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
373 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
374 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
375 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
381 struct snd_pcm_substream *playback_substream;
382 struct snd_pcm_substream *capture_substream;
387 enum CardState cardState;
389 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
390 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
392 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
394 enum ClockSourceType clkSource; // clock source
395 int clkRate; // clock rate
397 int volumePhase[kAudioChannels];
399 u16 leftADCInSens; // ADC left channel input sensitivity
400 u16 rightADCInSens; // ADC right channel input sensitivity
402 int opencnt; // Open/Close count
403 int setcnt; // SetupForPlay count
404 int playcnt; // TriggerPlay count
405 int errorcnt; // Error Count
406 unsigned long totalerrorcnt; // Total Error Count
409 int dsp_stop_is_processed;
413 MODULE_DESCRIPTION("korg1212");
414 MODULE_LICENSE("GPL");
415 MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
416 #ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
417 MODULE_FIRMWARE("korg/k1212.dsp");
420 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
421 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
422 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
424 module_param_array(index, int, NULL, 0444);
425 MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
426 module_param_array(id, charp, NULL, 0444);
427 MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
428 module_param_array(enable, bool, NULL, 0444);
429 MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
430 MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
432 static struct pci_device_id snd_korg1212_ids[] = {
436 .subvendor = PCI_ANY_ID,
437 .subdevice = PCI_ANY_ID,
442 MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
444 static char *stateName[] = {
447 "DSP download in process",
448 "DSP download complete",
458 static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
460 static char *clockSourceName[] = {
463 "S/PDIF at 44.1 kHz",
465 "local clock at 44.1 kHz",
466 "local clock at 48 kHz"
469 static char *channelName[] = {
484 static u16 ClockSourceSelector[] = {
485 0x8000, // selects source as ADAT at 44.1 kHz
486 0x0000, // selects source as ADAT at 48 kHz
487 0x8001, // selects source as S/PDIF at 44.1 kHz
488 0x0001, // selects source as S/PDIF at 48 kHz
489 0x8002, // selects source as local clock at 44.1 kHz
490 0x0002 // selects source as local clock at 48 kHz
493 union swap_u32 { unsigned char c[4]; u32 i; };
495 #ifdef SNDRV_BIG_ENDIAN
496 static u32 LowerWordSwap(u32 swappee)
498 static u32 UpperWordSwap(u32 swappee)
501 union swap_u32 retVal, swapper;
504 retVal.c[2] = swapper.c[3];
505 retVal.c[3] = swapper.c[2];
506 retVal.c[1] = swapper.c[1];
507 retVal.c[0] = swapper.c[0];
512 #ifdef SNDRV_BIG_ENDIAN
513 static u32 UpperWordSwap(u32 swappee)
515 static u32 LowerWordSwap(u32 swappee)
518 union swap_u32 retVal, swapper;
521 retVal.c[2] = swapper.c[2];
522 retVal.c[3] = swapper.c[3];
523 retVal.c[1] = swapper.c[0];
524 retVal.c[0] = swapper.c[1];
529 #define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
530 #define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
531 #define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
532 #define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
534 static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
535 enum korg1212_dbcnst doorbellVal,
536 u32 mailBox0Val, u32 mailBox1Val,
537 u32 mailBox2Val, u32 mailBox3Val)
541 int rc = K1212_CMDRET_Success;
543 if (!korg1212->outDoorbellPtr) {
544 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
545 return K1212_CMDRET_CardUninitialized;
548 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
549 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
550 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
551 writel(mailBox3Val, korg1212->mailbox3Ptr);
552 writel(mailBox2Val, korg1212->mailbox2Ptr);
553 writel(mailBox1Val, korg1212->mailbox1Ptr);
554 writel(mailBox0Val, korg1212->mailbox0Ptr);
555 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
557 // --------------------------------------------------------------
558 // the reboot command will not give an acknowledgement.
559 // --------------------------------------------------------------
560 if ( doorbellVal == K1212_DB_RebootCard ||
561 doorbellVal == K1212_DB_BootFromDSPPage4 ||
562 doorbellVal == K1212_DB_StartDSPDownload ) {
563 rc = K1212_CMDRET_Success;
567 // --------------------------------------------------------------
568 // See if the card acknowledged the command. Wait a bit, then
569 // read in the low word of mailbox3. If the MSB is set and the
570 // low byte is equal to the doorbell value, then it ack'd.
571 // --------------------------------------------------------------
572 udelay(COMMAND_ACK_DELAY);
573 mailBox3Lo = readl(korg1212->mailbox3Ptr);
574 if (mailBox3Lo & COMMAND_ACK_MASK) {
575 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
576 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
577 rc = K1212_CMDRET_Success;
582 korg1212->cmdRetryCount += retryCount;
584 if (retryCount >= MAX_COMMAND_RETRIES) {
585 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
586 rc = K1212_CMDRET_NoAckFromCard;
592 /* spinlock already held */
593 static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
595 if (! korg1212->stop_pending_cnt) {
596 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
597 /* program the timer */
598 korg1212->stop_pending_cnt = HZ;
599 korg1212->timer.expires = jiffies + 1;
600 add_timer(&korg1212->timer);
604 static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
607 spin_lock_irqsave(&korg1212->lock, flags);
608 korg1212->dsp_stop_is_processed = 0;
609 snd_korg1212_SendStop(korg1212);
610 spin_unlock_irqrestore(&korg1212->lock, flags);
611 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
614 /* timer callback for checking the ack of stop request */
615 static void snd_korg1212_timer_func(unsigned long data)
617 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
620 spin_lock_irqsave(&korg1212->lock, flags);
621 if (korg1212->sharedBufferPtr->cardCommand == 0) {
623 korg1212->stop_pending_cnt = 0;
624 korg1212->dsp_stop_is_processed = 1;
625 wake_up(&korg1212->wait);
626 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
627 stateName[korg1212->cardState]);
629 if (--korg1212->stop_pending_cnt > 0) {
630 /* reprogram timer */
631 korg1212->timer.expires = jiffies + 1;
632 add_timer(&korg1212->timer);
634 snd_printd("korg1212_timer_func timeout\n");
635 korg1212->sharedBufferPtr->cardCommand = 0;
636 korg1212->dsp_stop_is_processed = 1;
637 wake_up(&korg1212->wait);
638 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
639 stateName[korg1212->cardState]);
642 spin_unlock_irqrestore(&korg1212->lock, flags);
645 static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
650 udelay(INTERCOMMAND_DELAY);
651 spin_lock_irqsave(&korg1212->lock, flags);
652 korg1212->idleMonitorOn = 1;
653 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
654 K1212_MODE_MonitorOn, 0, 0, 0);
655 spin_unlock_irqrestore(&korg1212->lock, flags);
659 static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
661 if (korg1212->idleMonitorOn) {
662 snd_korg1212_SendStopAndWait(korg1212);
663 korg1212->idleMonitorOn = 0;
667 static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
669 korg1212->cardState = csState;
672 static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
674 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
675 stateName[korg1212->cardState], korg1212->opencnt);
676 mutex_lock(&korg1212->open_mutex);
677 if (korg1212->opencnt++ == 0) {
678 snd_korg1212_TurnOffIdleMonitor(korg1212);
679 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
682 mutex_unlock(&korg1212->open_mutex);
686 static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
688 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
689 stateName[korg1212->cardState], korg1212->opencnt);
691 mutex_lock(&korg1212->open_mutex);
692 if (--(korg1212->opencnt)) {
693 mutex_unlock(&korg1212->open_mutex);
697 if (korg1212->cardState == K1212_STATE_SETUP) {
698 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
699 K1212_MODE_StopPlay, 0, 0, 0);
701 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
702 rc, stateName[korg1212->cardState]);
703 if (rc != K1212_CMDRET_Success) {
704 mutex_unlock(&korg1212->open_mutex);
707 } else if (korg1212->cardState > K1212_STATE_SETUP) {
708 snd_korg1212_SendStopAndWait(korg1212);
711 if (korg1212->cardState > K1212_STATE_READY) {
712 snd_korg1212_TurnOnIdleMonitor(korg1212);
713 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
716 mutex_unlock(&korg1212->open_mutex);
720 /* spinlock already held */
721 static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
725 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
726 stateName[korg1212->cardState], korg1212->setcnt);
728 if (korg1212->setcnt++)
731 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
732 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
733 K1212_MODE_SetupPlay, 0, 0, 0);
735 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
736 rc, stateName[korg1212->cardState]);
737 if (rc != K1212_CMDRET_Success) {
743 /* spinlock already held */
744 static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
748 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
749 stateName[korg1212->cardState], korg1212->playcnt);
751 if (korg1212->playcnt++)
754 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
755 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
757 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
758 rc, stateName[korg1212->cardState]);
759 if (rc != K1212_CMDRET_Success) {
765 /* spinlock already held */
766 static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
768 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
769 stateName[korg1212->cardState], korg1212->playcnt);
771 if (--(korg1212->playcnt))
774 korg1212->setcnt = 0;
776 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
777 snd_korg1212_SendStop(korg1212);
779 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
783 static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
785 writel(PCI_INT_ENABLE_BIT |
786 PCI_DOORBELL_INT_ENABLE_BIT |
787 LOCAL_INT_ENABLE_BIT |
788 LOCAL_DOORBELL_INT_ENABLE_BIT |
789 LOCAL_DMA1_INT_ENABLE_BIT,
790 korg1212->statusRegPtr);
795 static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
796 enum MonitorModeSelector mode)
798 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
799 stateName[korg1212->cardState]);
802 case K1212_MONMODE_Off:
803 if (korg1212->cardState != K1212_STATE_MONITOR)
806 snd_korg1212_SendStopAndWait(korg1212);
807 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
811 case K1212_MONMODE_On:
812 if (korg1212->cardState != K1212_STATE_OPEN)
816 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
817 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
818 K1212_MODE_MonitorOn, 0, 0, 0);
819 if (rc != K1212_CMDRET_Success)
831 #endif /* not used */
833 static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
835 if (korg1212->playback_pid != korg1212->capture_pid &&
836 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
842 static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
844 static enum ClockSourceIndex s44[] = {
845 K1212_CLKIDX_AdatAt44_1K,
846 K1212_CLKIDX_WordAt44_1K,
847 K1212_CLKIDX_LocalAt44_1K
849 static enum ClockSourceIndex s48[] = {
850 K1212_CLKIDX_AdatAt48K,
851 K1212_CLKIDX_WordAt48K,
852 K1212_CLKIDX_LocalAt48K
856 if (!snd_korg1212_use_is_exclusive (korg1212))
861 parm = s44[korg1212->clkSource];
865 parm = s48[korg1212->clkSource];
872 korg1212->clkSrcRate = parm;
873 korg1212->clkRate = rate;
875 udelay(INTERCOMMAND_DELAY);
876 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
877 ClockSourceSelector[korg1212->clkSrcRate],
880 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
881 rc, stateName[korg1212->cardState]);
886 static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
889 if (source < 0 || source > 2)
892 korg1212->clkSource = source;
894 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
899 static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
901 writel(0, korg1212->statusRegPtr);
904 static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
906 struct SensBits sensVals;
911 u16 controlValue; // this keeps the current value to be written to
912 // the card's eeprom control register.
916 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
917 stateName[korg1212->cardState]);
919 // ----------------------------------------------------------------------------
920 // initialize things. The local init bit is always set when writing to the
921 // card's control register.
922 // ----------------------------------------------------------------------------
924 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
926 // ----------------------------------------------------------------------------
927 // make sure the card is not in monitor mode when we do this update.
928 // ----------------------------------------------------------------------------
929 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
931 snd_korg1212_SendStopAndWait(korg1212);
935 spin_lock_irqsave(&korg1212->lock, flags);
937 // ----------------------------------------------------------------------------
938 // we are about to send new values to the card, so clear the new values queued
939 // flag. Also, clear out mailbox 3, so we don't lockup.
940 // ----------------------------------------------------------------------------
941 writel(0, korg1212->mailbox3Ptr);
942 udelay(LOADSHIFT_DELAY);
944 // ----------------------------------------------------------------------------
945 // determine whether we are running a 48K or 44.1K clock. This info is used
946 // later when setting the SPDIF FF after the volume has been shifted in.
947 // ----------------------------------------------------------------------------
948 switch (korg1212->clkSrcRate) {
949 case K1212_CLKIDX_AdatAt44_1K:
950 case K1212_CLKIDX_WordAt44_1K:
951 case K1212_CLKIDX_LocalAt44_1K:
955 case K1212_CLKIDX_WordAt48K:
956 case K1212_CLKIDX_AdatAt48K:
957 case K1212_CLKIDX_LocalAt48K:
963 // ----------------------------------------------------------------------------
964 // start the update. Setup the bit structure and then shift the bits.
965 // ----------------------------------------------------------------------------
966 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
967 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
968 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
969 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
971 // ----------------------------------------------------------------------------
972 // now start shifting the bits in. Start with the left channel then the right.
973 // ----------------------------------------------------------------------------
974 for (channel = 0; channel < 2; channel++) {
976 // ----------------------------------------------------------------------------
977 // Bring the load/shift line low, then wait - the spec says >150ns from load/
978 // shift low to the first rising edge of the clock.
979 // ----------------------------------------------------------------------------
980 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
981 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
982 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
983 udelay(LOADSHIFT_DELAY);
985 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
987 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
988 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
990 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
992 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
993 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
995 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
998 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
999 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1000 udelay(SENSCLKPULSE_WIDTH);
1001 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1002 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1003 udelay(SENSCLKPULSE_WIDTH);
1006 // ----------------------------------------------------------------------------
1007 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1008 // bit if the clock rate is 48K otherwise write 0.
1009 // ----------------------------------------------------------------------------
1010 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1011 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1012 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1013 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1014 udelay(SENSCLKPULSE_WIDTH);
1017 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1019 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1020 udelay(ONE_RTC_TICK);
1021 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1022 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1023 udelay(SENSCLKPULSE_WIDTH);
1024 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1025 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1026 udelay(SENSCLKPULSE_WIDTH);
1029 // ----------------------------------------------------------------------------
1030 // The update is complete. Set a timeout. This is the inter-update delay.
1031 // Also, if the card was in monitor mode, restore it.
1032 // ----------------------------------------------------------------------------
1033 for (count = 0; count < 10; count++)
1034 udelay(SENSCLKPULSE_WIDTH);
1037 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1038 K1212_MODE_MonitorOn, 0, 0, 0);
1040 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1041 rc, stateName[korg1212->cardState]);
1044 spin_unlock_irqrestore(&korg1212->lock, flags);
1049 static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
1053 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1054 stateName[korg1212->cardState]);
1056 // ----------------------------------------------------
1057 // tell the card to boot
1058 // ----------------------------------------------------
1059 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1062 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1063 rc, stateName[korg1212->cardState]);
1064 msleep(DSP_BOOT_DELAY_IN_MS);
1066 // --------------------------------------------------------------------------------
1067 // Let the card know where all the buffers are.
1068 // --------------------------------------------------------------------------------
1069 rc = snd_korg1212_Send1212Command(korg1212,
1070 K1212_DB_ConfigureBufferMemory,
1071 LowerWordSwap(korg1212->PlayDataPhy),
1072 LowerWordSwap(korg1212->RecDataPhy),
1073 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1074 // is based on 2 buffers
1079 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1080 rc, stateName[korg1212->cardState]);
1082 udelay(INTERCOMMAND_DELAY);
1084 rc = snd_korg1212_Send1212Command(korg1212,
1085 K1212_DB_ConfigureMiscMemory,
1086 LowerWordSwap(korg1212->VolumeTablePhy),
1087 LowerWordSwap(korg1212->RoutingTablePhy),
1088 LowerWordSwap(korg1212->AdatTimeCodePhy),
1093 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1094 rc, stateName[korg1212->cardState]);
1096 // --------------------------------------------------------------------------------
1097 // Initialize the routing and volume tables, then update the card's state.
1098 // --------------------------------------------------------------------------------
1099 udelay(INTERCOMMAND_DELAY);
1101 for (channel = 0; channel < kAudioChannels; channel++) {
1102 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1103 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1104 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1107 snd_korg1212_WriteADCSensitivity(korg1212);
1109 udelay(INTERCOMMAND_DELAY);
1110 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1111 ClockSourceSelector[korg1212->clkSrcRate],
1114 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1115 rc, stateName[korg1212->cardState]);
1117 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
1118 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1121 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1122 rc, stateName[korg1212->cardState]);
1124 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1127 static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
1130 struct snd_korg1212 *korg1212 = dev_id;
1132 doorbellValue = readl(korg1212->inDoorbellPtr);
1137 spin_lock(&korg1212->lock);
1139 writel(doorbellValue, korg1212->inDoorbellPtr);
1141 korg1212->irqcount++;
1145 switch (doorbellValue) {
1146 case K1212_DB_DSPDownloadDone:
1147 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1148 korg1212->irqcount, doorbellValue,
1149 stateName[korg1212->cardState]);
1150 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1151 korg1212->dsp_is_loaded = 1;
1152 wake_up(&korg1212->wait);
1156 // ------------------------------------------------------------------------
1157 // an error occurred - stop the card
1158 // ------------------------------------------------------------------------
1159 case K1212_DB_DMAERROR:
1160 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1161 korg1212->irqcount, doorbellValue,
1162 stateName[korg1212->cardState]);
1163 snd_printk(KERN_ERR "korg1212: DMA Error\n");
1164 korg1212->errorcnt++;
1165 korg1212->totalerrorcnt++;
1166 korg1212->sharedBufferPtr->cardCommand = 0;
1167 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1170 // ------------------------------------------------------------------------
1171 // the card has stopped by our request. Clear the command word and signal
1172 // the semaphore in case someone is waiting for this.
1173 // ------------------------------------------------------------------------
1174 case K1212_DB_CARDSTOPPED:
1175 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
1176 korg1212->irqcount, doorbellValue,
1177 stateName[korg1212->cardState]);
1178 korg1212->sharedBufferPtr->cardCommand = 0;
1182 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
1183 korg1212->irqcount, doorbellValue,
1184 korg1212->currentBuffer, stateName[korg1212->cardState]);
1185 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1186 korg1212->currentBuffer++;
1188 if (korg1212->currentBuffer >= kNumBuffers)
1189 korg1212->currentBuffer = 0;
1191 if (!korg1212->running)
1194 if (korg1212->capture_substream) {
1195 spin_unlock(&korg1212->lock);
1196 snd_pcm_period_elapsed(korg1212->capture_substream);
1197 spin_lock(&korg1212->lock);
1200 if (korg1212->playback_substream) {
1201 spin_unlock(&korg1212->lock);
1202 snd_pcm_period_elapsed(korg1212->playback_substream);
1203 spin_lock(&korg1212->lock);
1211 spin_unlock(&korg1212->lock);
1216 static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
1220 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1221 stateName[korg1212->cardState]);
1223 // ---------------------------------------------------------------
1224 // verify the state of the card before proceeding.
1225 // ---------------------------------------------------------------
1226 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
1229 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1231 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1232 UpperWordSwap(korg1212->dma_dsp.addr),
1235 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1236 rc, stateName[korg1212->cardState]);
1238 korg1212->dsp_is_loaded = 0;
1239 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1240 if (! korg1212->dsp_is_loaded )
1241 return -EBUSY; /* timeout */
1243 snd_korg1212_OnDSPDownloadComplete(korg1212);
1248 static struct snd_pcm_hardware snd_korg1212_playback_info =
1250 .info = (SNDRV_PCM_INFO_MMAP |
1251 SNDRV_PCM_INFO_MMAP_VALID |
1252 SNDRV_PCM_INFO_INTERLEAVED),
1253 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1254 .rates = (SNDRV_PCM_RATE_44100 |
1255 SNDRV_PCM_RATE_48000),
1258 .channels_min = K1212_MIN_CHANNELS,
1259 .channels_max = K1212_MAX_CHANNELS,
1260 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1261 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1262 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1263 .periods_min = K1212_PERIODS,
1264 .periods_max = K1212_PERIODS,
1268 static struct snd_pcm_hardware snd_korg1212_capture_info =
1270 .info = (SNDRV_PCM_INFO_MMAP |
1271 SNDRV_PCM_INFO_MMAP_VALID |
1272 SNDRV_PCM_INFO_INTERLEAVED),
1273 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1274 .rates = (SNDRV_PCM_RATE_44100 |
1275 SNDRV_PCM_RATE_48000),
1278 .channels_min = K1212_MIN_CHANNELS,
1279 .channels_max = K1212_MAX_CHANNELS,
1280 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1281 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1282 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1283 .periods_min = K1212_PERIODS,
1284 .periods_max = K1212_PERIODS,
1288 static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
1290 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1293 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1294 pos, offset, size, count);
1295 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1297 for (i=0; i < count; i++) {
1298 #if K1212_DEBUG_LEVEL > 0
1299 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1300 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1301 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1306 memset((void*) dst + offset, 0, size);
1313 static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
1315 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1318 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1320 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1322 for (i=0; i < count; i++) {
1323 #if K1212_DEBUG_LEVEL > 0
1324 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1325 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
1326 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1330 rc = copy_to_user(dst + offset, src, size);
1332 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1342 static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
1344 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1347 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1348 pos, offset, size, count);
1350 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1352 for (i=0; i < count; i++) {
1353 #if K1212_DEBUG_LEVEL > 0
1354 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1355 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1356 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1360 rc = copy_from_user((void*) dst + offset, src, size);
1362 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1372 static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
1374 struct snd_korg1212 *korg1212 = pcm->private_data;
1376 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1377 stateName[korg1212->cardState]);
1379 korg1212->pcm = NULL;
1382 static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
1384 unsigned long flags;
1385 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1386 struct snd_pcm_runtime *runtime = substream->runtime;
1388 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1389 stateName[korg1212->cardState]);
1391 snd_korg1212_OpenCard(korg1212);
1393 runtime->hw = snd_korg1212_playback_info;
1394 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1396 spin_lock_irqsave(&korg1212->lock, flags);
1398 korg1212->playback_substream = substream;
1399 korg1212->playback_pid = current->pid;
1400 korg1212->periodsize = K1212_PERIODS;
1401 korg1212->channels = K1212_CHANNELS;
1402 korg1212->errorcnt = 0;
1404 spin_unlock_irqrestore(&korg1212->lock, flags);
1406 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1411 static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
1413 unsigned long flags;
1414 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1415 struct snd_pcm_runtime *runtime = substream->runtime;
1417 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1418 stateName[korg1212->cardState]);
1420 snd_korg1212_OpenCard(korg1212);
1422 runtime->hw = snd_korg1212_capture_info;
1423 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1425 spin_lock_irqsave(&korg1212->lock, flags);
1427 korg1212->capture_substream = substream;
1428 korg1212->capture_pid = current->pid;
1429 korg1212->periodsize = K1212_PERIODS;
1430 korg1212->channels = K1212_CHANNELS;
1432 spin_unlock_irqrestore(&korg1212->lock, flags);
1434 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1435 kPlayBufferFrames, kPlayBufferFrames);
1439 static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
1441 unsigned long flags;
1442 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1444 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1445 stateName[korg1212->cardState]);
1447 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1449 spin_lock_irqsave(&korg1212->lock, flags);
1451 korg1212->playback_pid = -1;
1452 korg1212->playback_substream = NULL;
1453 korg1212->periodsize = 0;
1455 spin_unlock_irqrestore(&korg1212->lock, flags);
1457 snd_korg1212_CloseCard(korg1212);
1461 static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
1463 unsigned long flags;
1464 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1466 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1467 stateName[korg1212->cardState]);
1469 spin_lock_irqsave(&korg1212->lock, flags);
1471 korg1212->capture_pid = -1;
1472 korg1212->capture_substream = NULL;
1473 korg1212->periodsize = 0;
1475 spin_unlock_irqrestore(&korg1212->lock, flags);
1477 snd_korg1212_CloseCard(korg1212);
1481 static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
1482 unsigned int cmd, void *arg)
1484 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1486 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
1487 struct snd_pcm_channel_info *info = arg;
1489 info->first = info->channel * 16;
1491 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
1495 return snd_pcm_lib_ioctl(substream, cmd, arg);
1498 static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1499 struct snd_pcm_hw_params *params)
1501 unsigned long flags;
1502 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1507 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1508 stateName[korg1212->cardState]);
1510 spin_lock_irqsave(&korg1212->lock, flags);
1512 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1513 this_pid = korg1212->playback_pid;
1514 other_pid = korg1212->capture_pid;
1516 this_pid = korg1212->capture_pid;
1517 other_pid = korg1212->playback_pid;
1520 if ((other_pid > 0) && (this_pid != other_pid)) {
1522 /* The other stream is open, and not by the same
1523 task as this one. Make sure that the parameters
1524 that matter are the same.
1527 if ((int)params_rate(params) != korg1212->clkRate) {
1528 spin_unlock_irqrestore(&korg1212->lock, flags);
1529 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1533 spin_unlock_irqrestore(&korg1212->lock, flags);
1537 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1538 spin_unlock_irqrestore(&korg1212->lock, flags);
1542 korg1212->channels = params_channels(params);
1543 korg1212->periodsize = K1212_PERIOD_BYTES;
1545 spin_unlock_irqrestore(&korg1212->lock, flags);
1550 static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
1552 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1555 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1556 stateName[korg1212->cardState]);
1558 spin_lock_irq(&korg1212->lock);
1560 /* FIXME: we should wait for ack! */
1561 if (korg1212->stop_pending_cnt > 0) {
1562 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1563 stateName[korg1212->cardState]);
1564 spin_unlock_irq(&korg1212->lock);
1567 korg1212->sharedBufferPtr->cardCommand = 0;
1568 del_timer(&korg1212->timer);
1569 korg1212->stop_pending_cnt = 0;
1573 rc = snd_korg1212_SetupForPlay(korg1212);
1575 korg1212->currentBuffer = 0;
1577 spin_unlock_irq(&korg1212->lock);
1579 return rc ? -EINVAL : 0;
1582 static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
1585 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1588 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1589 stateName[korg1212->cardState], cmd);
1591 spin_lock(&korg1212->lock);
1593 case SNDRV_PCM_TRIGGER_START:
1595 if (korg1212->running) {
1596 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
1600 korg1212->running++;
1601 rc = snd_korg1212_TriggerPlay(korg1212);
1604 case SNDRV_PCM_TRIGGER_STOP:
1606 if (!korg1212->running) {
1607 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
1611 korg1212->running--;
1612 rc = snd_korg1212_StopPlay(korg1212);
1619 spin_unlock(&korg1212->lock);
1620 return rc ? -EINVAL : 0;
1623 static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
1625 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1626 snd_pcm_uframes_t pos;
1628 pos = korg1212->currentBuffer * kPlayBufferFrames;
1630 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1631 stateName[korg1212->cardState], pos);
1636 static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
1638 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1639 snd_pcm_uframes_t pos;
1641 pos = korg1212->currentBuffer * kPlayBufferFrames;
1643 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1644 stateName[korg1212->cardState], pos);
1649 static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
1650 int channel, /* not used (interleaved data) */
1651 snd_pcm_uframes_t pos,
1653 snd_pcm_uframes_t count)
1655 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1657 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1658 stateName[korg1212->cardState], pos, count);
1660 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1664 static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
1665 int channel, /* not used (interleaved data) */
1666 snd_pcm_uframes_t pos,
1667 snd_pcm_uframes_t count)
1669 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1671 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1672 stateName[korg1212->cardState]);
1674 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1677 static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
1678 int channel, /* not used (interleaved data) */
1679 snd_pcm_uframes_t pos,
1681 snd_pcm_uframes_t count)
1683 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1685 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1686 stateName[korg1212->cardState], pos, count);
1688 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1691 static struct snd_pcm_ops snd_korg1212_playback_ops = {
1692 .open = snd_korg1212_playback_open,
1693 .close = snd_korg1212_playback_close,
1694 .ioctl = snd_korg1212_ioctl,
1695 .hw_params = snd_korg1212_hw_params,
1696 .prepare = snd_korg1212_prepare,
1697 .trigger = snd_korg1212_trigger,
1698 .pointer = snd_korg1212_playback_pointer,
1699 .copy = snd_korg1212_playback_copy,
1700 .silence = snd_korg1212_playback_silence,
1703 static struct snd_pcm_ops snd_korg1212_capture_ops = {
1704 .open = snd_korg1212_capture_open,
1705 .close = snd_korg1212_capture_close,
1706 .ioctl = snd_korg1212_ioctl,
1707 .hw_params = snd_korg1212_hw_params,
1708 .prepare = snd_korg1212_prepare,
1709 .trigger = snd_korg1212_trigger,
1710 .pointer = snd_korg1212_capture_pointer,
1711 .copy = snd_korg1212_capture_copy,
1718 static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_info *uinfo)
1721 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1722 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1726 static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *u)
1729 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1730 int i = kcontrol->private_value;
1732 spin_lock_irq(&korg1212->lock);
1734 u->value.integer.value[0] = korg1212->volumePhase[i];
1737 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1739 spin_unlock_irq(&korg1212->lock);
1744 static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1745 struct snd_ctl_elem_value *u)
1747 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1751 spin_lock_irq(&korg1212->lock);
1753 i = kcontrol->private_value;
1755 korg1212->volumePhase[i] = !!u->value.integer.value[0];
1757 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1759 if ((u->value.integer.value[0] != 0) != (val < 0)) {
1760 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1761 korg1212->sharedBufferPtr->volumeData[i] = val;
1766 korg1212->volumePhase[i+1] = !!u->value.integer.value[1];
1768 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1770 if ((u->value.integer.value[1] != 0) != (val < 0)) {
1771 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1772 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1777 spin_unlock_irq(&korg1212->lock);
1782 static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_info *uinfo)
1785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1786 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1787 uinfo->value.integer.min = k1212MinVolume;
1788 uinfo->value.integer.max = k1212MaxVolume;
1792 static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1793 struct snd_ctl_elem_value *u)
1795 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1798 spin_lock_irq(&korg1212->lock);
1800 i = kcontrol->private_value;
1801 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1804 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1806 spin_unlock_irq(&korg1212->lock);
1811 static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1812 struct snd_ctl_elem_value *u)
1814 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1819 spin_lock_irq(&korg1212->lock);
1821 i = kcontrol->private_value;
1823 if (u->value.integer.value[0] >= k1212MinVolume &&
1824 u->value.integer.value[0] >= k1212MaxVolume &&
1825 u->value.integer.value[0] !=
1826 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] >= k1212MinVolume &&
1835 u->value.integer.value[1] >= k1212MaxVolume &&
1836 u->value.integer.value[1] !=
1837 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] < kAudioChannels &&
1893 u->value.enumerated.item[0] !=
1894 (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1895 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1900 if (u->value.enumerated.item[1] < kAudioChannels &&
1901 u->value.enumerated.item[1] !=
1902 (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1903 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1908 spin_unlock_irq(&korg1212->lock);
1913 static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_info *uinfo)
1916 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1918 uinfo->value.integer.min = k1212MaxADCSens;
1919 uinfo->value.integer.max = k1212MinADCSens;
1923 static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1924 struct snd_ctl_elem_value *u)
1926 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1928 spin_lock_irq(&korg1212->lock);
1930 u->value.integer.value[0] = korg1212->leftADCInSens;
1931 u->value.integer.value[1] = korg1212->rightADCInSens;
1933 spin_unlock_irq(&korg1212->lock);
1938 static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1939 struct snd_ctl_elem_value *u)
1941 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1944 spin_lock_irq(&korg1212->lock);
1946 if (u->value.integer.value[0] >= k1212MinADCSens &&
1947 u->value.integer.value[0] <= k1212MaxADCSens &&
1948 u->value.integer.value[0] != korg1212->leftADCInSens) {
1949 korg1212->leftADCInSens = u->value.integer.value[0];
1952 if (u->value.integer.value[1] >= k1212MinADCSens &&
1953 u->value.integer.value[1] <= k1212MaxADCSens &&
1954 u->value.integer.value[1] != korg1212->rightADCInSens) {
1955 korg1212->rightADCInSens = u->value.integer.value[1];
1959 spin_unlock_irq(&korg1212->lock);
1962 snd_korg1212_WriteADCSensitivity(korg1212);
1967 static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1968 struct snd_ctl_elem_info *uinfo)
1970 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1972 uinfo->value.enumerated.items = 3;
1973 if (uinfo->value.enumerated.item > 2) {
1974 uinfo->value.enumerated.item = 2;
1976 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1980 static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_value *ucontrol)
1983 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1985 spin_lock_irq(&korg1212->lock);
1987 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1989 spin_unlock_irq(&korg1212->lock);
1993 static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1994 struct snd_ctl_elem_value *ucontrol)
1996 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
2000 val = ucontrol->value.enumerated.item[0] % 3;
2001 spin_lock_irq(&korg1212->lock);
2002 change = val != korg1212->clkSource;
2003 snd_korg1212_SetClockSource(korg1212, val);
2004 spin_unlock_irq(&korg1212->lock);
2008 #define MON_MIXER(ord,c_name) \
2010 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2011 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2012 .name = c_name " Monitor Volume", \
2013 .info = snd_korg1212_control_volume_info, \
2014 .get = snd_korg1212_control_volume_get, \
2015 .put = snd_korg1212_control_volume_put, \
2016 .private_value = ord, \
2019 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2021 .name = c_name " Monitor Route", \
2022 .info = snd_korg1212_control_route_info, \
2023 .get = snd_korg1212_control_route_get, \
2024 .put = snd_korg1212_control_route_put, \
2025 .private_value = ord, \
2028 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2029 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2030 .name = c_name " Monitor Phase Invert", \
2031 .info = snd_korg1212_control_phase_info, \
2032 .get = snd_korg1212_control_phase_get, \
2033 .put = snd_korg1212_control_phase_put, \
2034 .private_value = ord, \
2037 static struct snd_kcontrol_new snd_korg1212_controls[] = {
2038 MON_MIXER(8, "Analog"),
2039 MON_MIXER(10, "SPDIF"),
2040 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2041 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2043 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2044 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2045 .name = "Sync Source",
2046 .info = snd_korg1212_control_sync_info,
2047 .get = snd_korg1212_control_sync_get,
2048 .put = snd_korg1212_control_sync_put,
2051 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2052 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2053 .name = "ADC Attenuation",
2054 .info = snd_korg1212_control_info,
2055 .get = snd_korg1212_control_get,
2056 .put = snd_korg1212_control_put,
2064 static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2065 struct snd_info_buffer *buffer)
2068 struct snd_korg1212 *korg1212 = entry->private_data;
2070 snd_iprintf(buffer, korg1212->card->longname);
2071 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2072 snd_iprintf(buffer, "\nGeneral settings\n");
2073 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2074 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2075 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2076 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2077 snd_iprintf(buffer, " Volume Info:\n");
2078 for (n=0; n<kAudioChannels; n++)
2079 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2081 channelName[korg1212->sharedBufferPtr->routeData[n]],
2082 korg1212->sharedBufferPtr->volumeData[n]);
2083 snd_iprintf(buffer, "\nGeneral status\n");
2084 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2085 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2086 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2087 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2088 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2089 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2092 static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
2094 struct snd_info_entry *entry;
2096 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2097 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
2101 snd_korg1212_free(struct snd_korg1212 *korg1212)
2103 snd_korg1212_TurnOffIdleMonitor(korg1212);
2105 if (korg1212->irq >= 0) {
2106 synchronize_irq(korg1212->irq);
2107 snd_korg1212_DisableCardInterrupts(korg1212);
2108 free_irq(korg1212->irq, korg1212);
2112 if (korg1212->iobase != NULL) {
2113 iounmap(korg1212->iobase);
2114 korg1212->iobase = NULL;
2117 pci_release_regions(korg1212->pci);
2119 // ----------------------------------------------------
2120 // free up memory resources used for the DSP download.
2121 // ----------------------------------------------------
2122 if (korg1212->dma_dsp.area) {
2123 snd_dma_free_pages(&korg1212->dma_dsp);
2124 korg1212->dma_dsp.area = NULL;
2127 #ifndef K1212_LARGEALLOC
2129 // ------------------------------------------------------
2130 // free up memory resources used for the Play/Rec Buffers
2131 // ------------------------------------------------------
2132 if (korg1212->dma_play.area) {
2133 snd_dma_free_pages(&korg1212->dma_play);
2134 korg1212->dma_play.area = NULL;
2137 if (korg1212->dma_rec.area) {
2138 snd_dma_free_pages(&korg1212->dma_rec);
2139 korg1212->dma_rec.area = NULL;
2144 // ----------------------------------------------------
2145 // free up memory resources used for the Shared Buffers
2146 // ----------------------------------------------------
2147 if (korg1212->dma_shared.area) {
2148 snd_dma_free_pages(&korg1212->dma_shared);
2149 korg1212->dma_shared.area = NULL;
2152 pci_disable_device(korg1212->pci);
2157 static int snd_korg1212_dev_free(struct snd_device *device)
2159 struct snd_korg1212 *korg1212 = device->device_data;
2160 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
2161 return snd_korg1212_free(korg1212);
2164 static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2165 struct snd_korg1212 ** rchip)
2170 unsigned ioport_size, iomem_size, iomem2_size;
2171 struct snd_korg1212 * korg1212;
2172 const struct firmware *dsp_code;
2174 static struct snd_device_ops ops = {
2175 .dev_free = snd_korg1212_dev_free,
2179 if ((err = pci_enable_device(pci)) < 0)
2182 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
2183 if (korg1212 == NULL) {
2184 pci_disable_device(pci);
2188 korg1212->card = card;
2189 korg1212->pci = pci;
2191 init_waitqueue_head(&korg1212->wait);
2192 spin_lock_init(&korg1212->lock);
2193 mutex_init(&korg1212->open_mutex);
2194 init_timer(&korg1212->timer);
2195 korg1212->timer.function = snd_korg1212_timer_func;
2196 korg1212->timer.data = (unsigned long)korg1212;
2199 korg1212->clkSource = K1212_CLKIDX_Local;
2200 korg1212->clkRate = 44100;
2201 korg1212->inIRQ = 0;
2202 korg1212->running = 0;
2203 korg1212->opencnt = 0;
2204 korg1212->playcnt = 0;
2205 korg1212->setcnt = 0;
2206 korg1212->totalerrorcnt = 0;
2207 korg1212->playback_pid = -1;
2208 korg1212->capture_pid = -1;
2209 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2210 korg1212->idleMonitorOn = 0;
2211 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2212 korg1212->leftADCInSens = k1212MaxADCSens;
2213 korg1212->rightADCInSens = k1212MaxADCSens;
2215 for (i=0; i<kAudioChannels; i++)
2216 korg1212->volumePhase[i] = 0;
2218 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2220 pci_disable_device(pci);
2224 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2225 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2226 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2228 iomem_size = pci_resource_len(korg1212->pci, 0);
2229 ioport_size = pci_resource_len(korg1212->pci, 1);
2230 iomem2_size = pci_resource_len(korg1212->pci, 2);
2232 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2233 " iomem = 0x%lx (%d)\n"
2234 " ioport = 0x%lx (%d)\n"
2235 " iomem = 0x%lx (%d)\n"
2237 korg1212->iomem, iomem_size,
2238 korg1212->ioport, ioport_size,
2239 korg1212->iomem2, iomem2_size,
2240 stateName[korg1212->cardState]);
2242 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2243 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2244 korg1212->iomem + iomem_size - 1);
2245 snd_korg1212_free(korg1212);
2249 err = request_irq(pci->irq, snd_korg1212_interrupt,
2251 "korg1212", korg1212);
2254 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2255 snd_korg1212_free(korg1212);
2259 korg1212->irq = pci->irq;
2261 pci_set_master(korg1212->pci);
2263 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2264 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2265 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2266 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2267 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2268 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2269 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2270 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2271 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2272 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2274 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2275 " Status register = 0x%p\n"
2276 " OutDoorbell = 0x%p\n"
2277 " InDoorbell = 0x%p\n"
2278 " Mailbox0 = 0x%p\n"
2279 " Mailbox1 = 0x%p\n"
2280 " Mailbox2 = 0x%p\n"
2281 " Mailbox3 = 0x%p\n"
2282 " ControlReg = 0x%p\n"
2286 korg1212->statusRegPtr,
2287 korg1212->outDoorbellPtr,
2288 korg1212->inDoorbellPtr,
2289 korg1212->mailbox0Ptr,
2290 korg1212->mailbox1Ptr,
2291 korg1212->mailbox2Ptr,
2292 korg1212->mailbox3Ptr,
2293 korg1212->controlRegPtr,
2294 korg1212->sensRegPtr,
2296 stateName[korg1212->cardState]);
2298 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2299 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2300 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
2301 snd_korg1212_free(korg1212);
2304 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
2305 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2307 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
2309 #ifndef K1212_LARGEALLOC
2311 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
2313 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2314 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2315 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2316 snd_korg1212_free(korg1212);
2319 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
2320 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2322 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2323 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
2325 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2326 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2327 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2328 snd_korg1212_free(korg1212);
2331 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
2332 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2334 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2335 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
2337 #else // K1212_LARGEALLOC
2339 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2340 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
2341 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2342 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
2344 #endif // K1212_LARGEALLOC
2346 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
2347 offsetof(struct KorgSharedBuffer, volumeData);
2348 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
2349 offsetof(struct KorgSharedBuffer, routeData);
2350 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
2351 offsetof(struct KorgSharedBuffer, AdatTimeCode);
2353 #ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2354 dsp_code = &static_dsp_code;
2356 err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
2358 release_firmware(dsp_code);
2359 snd_printk(KERN_ERR "firmware not available\n");
2360 snd_korg1212_free(korg1212);
2365 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2366 dsp_code->size, &korg1212->dma_dsp) < 0) {
2367 snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
2368 snd_korg1212_free(korg1212);
2369 #ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2370 release_firmware(dsp_code);
2375 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2376 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
2377 stateName[korg1212->cardState]);
2379 memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
2381 #ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2382 release_firmware(dsp_code);
2385 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2388 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
2390 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2391 snd_korg1212_free(korg1212);
2395 snd_korg1212_EnableCardInterrupts(korg1212);
2397 mdelay(CARD_BOOT_DELAY_IN_MS);
2399 if (snd_korg1212_downloadDSPCode(korg1212))
2402 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
2403 "PlayDataPhy = %08x L[%08x]\n"
2404 "korg1212: RecDataPhy = %08x L[%08x], "
2405 "VolumeTablePhy = %08x L[%08x]\n"
2406 "korg1212: RoutingTablePhy = %08x L[%08x], "
2407 "AdatTimeCodePhy = %08x L[%08x]\n",
2408 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2409 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2410 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2411 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2412 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2413 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2415 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2418 korg1212->pcm->private_data = korg1212;
2419 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2420 strcpy(korg1212->pcm->name, "korg1212");
2422 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2424 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2426 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2428 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2429 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2434 snd_korg1212_proc_init(korg1212);
2436 snd_card_set_dev(card, &pci->dev);
2444 * Card initialisation
2447 static int __devinit
2448 snd_korg1212_probe(struct pci_dev *pci,
2449 const struct pci_device_id *pci_id)
2452 struct snd_korg1212 *korg1212;
2453 struct snd_card *card;
2456 if (dev >= SNDRV_CARDS) {
2463 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2467 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2468 snd_card_free(card);
2472 strcpy(card->driver, "korg1212");
2473 strcpy(card->shortname, "korg1212");
2474 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2475 korg1212->iomem, korg1212->irq);
2477 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
2479 if ((err = snd_card_register(card)) < 0) {
2480 snd_card_free(card);
2483 pci_set_drvdata(pci, card);
2488 static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2490 snd_card_free(pci_get_drvdata(pci));
2491 pci_set_drvdata(pci, NULL);
2494 static struct pci_driver driver = {
2496 .id_table = snd_korg1212_ids,
2497 .probe = snd_korg1212_probe,
2498 .remove = __devexit_p(snd_korg1212_remove),
2501 static int __init alsa_card_korg1212_init(void)
2503 return pci_register_driver(&driver);
2506 static void __exit alsa_card_korg1212_exit(void)
2508 pci_unregister_driver(&driver);
2511 module_init(alsa_card_korg1212_init)
2512 module_exit(alsa_card_korg1212_exit)