Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / korg1212 / korg1212.c
blobbb1de20081760d7857652b85e2db3164c305e3c0
1 /*
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>
38 #include <asm/io.h>
40 // ----------------------------------------------------------------------------
41 // Debug Stuff
42 // ----------------------------------------------------------------------------
43 #define K1212_DEBUG_LEVEL 0
44 #define K1212_DEBUG_PRINTK printk
45 //#define K1212_DEBUG_PRINTK(x...) printk("<0>" x)
47 // ----------------------------------------------------------------------------
48 // Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
49 // buffers are alocated as a large piece inside KorgSharedBuffer.
50 // ----------------------------------------------------------------------------
51 //#define K1212_LARGEALLOC 1
53 // ----------------------------------------------------------------------------
54 // Valid states of the Korg 1212 I/O card.
55 // ----------------------------------------------------------------------------
56 typedef enum {
57 K1212_STATE_NONEXISTENT, // there is no card here
58 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
59 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
60 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
61 K1212_STATE_READY, // the card can be opened by an application. Any application
62 // requests prior to this state should fail. Only an open
63 // request can be made at this state.
64 K1212_STATE_OPEN, // an application has opened the card
65 K1212_STATE_SETUP, // the card has been setup for play
66 K1212_STATE_PLAYING, // the card is playing
67 K1212_STATE_MONITOR, // the card is in the monitor mode
68 K1212_STATE_CALIBRATING, // the card is currently calibrating
69 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
70 // are in the process of cleaning things up.
71 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
72 } CardState;
74 // ----------------------------------------------------------------------------
75 // The following enumeration defines the constants written to the card's
76 // host-to-card doorbell to initiate a command.
77 // ----------------------------------------------------------------------------
78 typedef enum {
79 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
80 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
81 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
82 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
83 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
84 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
85 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
86 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
87 // timecode value.
88 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
89 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
90 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
91 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
92 // on page 4 (local page to card).
93 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
94 // completed.
95 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
96 } korg1212_dbcnst_t;
99 // ----------------------------------------------------------------------------
100 // The following enumeration defines return codes
101 // to the Korg 1212 I/O driver.
102 // ----------------------------------------------------------------------------
103 typedef enum {
104 K1212_CMDRET_Success = 0, // command was successfully placed
105 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
106 K1212_CMDRET_PMFailure, // the protected mode call failed
107 K1212_CMDRET_FailUnspecified, // unspecified failure
108 K1212_CMDRET_FailBadState, // the specified command can not be given in
109 // the card's current state. (or the wave device's
110 // state)
111 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
112 K1212_CMDRET_BadIndex, // an out of range card index was specified
113 K1212_CMDRET_BadHandle, // an invalid card handle was specified
114 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
115 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
116 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
117 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
119 K1212_CMDRET_BadDevice, // the specified wave device was out of range
120 K1212_CMDRET_BadFormat // the specified wave format is unsupported
121 } snd_korg1212rc;
123 // ----------------------------------------------------------------------------
124 // The following enumeration defines the constants used to select the play
125 // mode for the card in the SelectPlayMode command.
126 // ----------------------------------------------------------------------------
127 typedef enum {
128 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
129 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
130 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
131 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
132 } PlayModeSelector;
134 // ----------------------------------------------------------------------------
135 // The following enumeration defines the constants used to select the monitor
136 // mode for the card in the SetMonitorMode command.
137 // ----------------------------------------------------------------------------
138 typedef enum {
139 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
140 K1212_MONMODE_On // tells card to turn on monitor mode
141 } MonitorModeSelector;
143 #define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
144 #define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
145 #define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
146 #define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
147 #define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
148 #define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
149 #define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
150 #define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
151 // register
152 #define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
153 // this is the upper word of the PCI control reg.
154 #define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
156 #define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
157 // from the card after sending a command.
158 #define INTERCOMMAND_DELAY 40
159 #define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
160 // to send a command before giving up.
161 #define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
162 // the card.
163 #define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
165 #define CARD_BOOT_DELAY_IN_MS 10
166 #define CARD_BOOT_TIMEOUT 10
167 #define DSP_BOOT_DELAY_IN_MS 200
169 #define kNumBuffers 8
170 #define k1212MaxCards 4
171 #define k1212NumWaveDevices 6
172 #define k16BitChannels 10
173 #define k32BitChannels 2
174 #define kAudioChannels (k16BitChannels + k32BitChannels)
175 #define kPlayBufferFrames 1024
177 #define K1212_ANALOG_CHANNELS 2
178 #define K1212_SPDIF_CHANNELS 2
179 #define K1212_ADAT_CHANNELS 8
180 #define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
181 #define K1212_MIN_CHANNELS 1
182 #define K1212_MAX_CHANNELS K1212_CHANNELS
183 #define K1212_FRAME_SIZE (sizeof(KorgAudioFrame))
184 #define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
185 #define K1212_PERIODS (kNumBuffers)
186 #define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
187 #define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
188 #define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
189 #define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
190 #define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
191 #define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
193 #define k1212MinADCSens 0x7f
194 #define k1212MaxADCSens 0x00
195 #define k1212MaxVolume 0x7fff
196 #define k1212MaxWaveVolume 0xffff
197 #define k1212MinVolume 0x0000
198 #define k1212MaxVolInverted 0x8000
200 // -----------------------------------------------------------------
201 // the following bits are used for controlling interrupts in the
202 // interrupt control/status reg
203 // -----------------------------------------------------------------
204 #define PCI_INT_ENABLE_BIT 0x00000100
205 #define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
206 #define LOCAL_INT_ENABLE_BIT 0x00010000
207 #define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
208 #define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
210 // -----------------------------------------------------------------
211 // the following bits are defined for the PCI command register
212 // -----------------------------------------------------------------
213 #define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
214 #define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
215 #define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
217 // -----------------------------------------------------------------
218 // the following bits are defined for the PCI status register
219 // -----------------------------------------------------------------
220 #define PCI_STAT_PARITY_ERROR_BIT 0x8000
221 #define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
222 #define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
223 #define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
224 #define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
226 // ------------------------------------------------------------------------
227 // the following constants are used in setting the 1212 I/O card's input
228 // sensitivity.
229 // ------------------------------------------------------------------------
230 #define SET_SENS_LOCALINIT_BITPOS 15
231 #define SET_SENS_DATA_BITPOS 10
232 #define SET_SENS_CLOCK_BITPOS 8
233 #define SET_SENS_LOADSHIFT_BITPOS 0
235 #define SET_SENS_LEFTCHANID 0x00
236 #define SET_SENS_RIGHTCHANID 0x01
238 #define K1212SENSUPDATE_DELAY_IN_MS 50
240 // --------------------------------------------------------------------------
241 // WaitRTCTicks
243 // This function waits the specified number of real time clock ticks.
244 // According to the DDK, each tick is ~0.8 microseconds.
245 // The defines following the function declaration can be used for the
246 // numTicksToWait parameter.
247 // --------------------------------------------------------------------------
248 #define ONE_RTC_TICK 1
249 #define SENSCLKPULSE_WIDTH 4
250 #define LOADSHIFT_DELAY 4
251 #define INTERCOMMAND_DELAY 40
252 #define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
253 // the command register. (could be up to 180 us)
254 #define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
255 // from the card after sending a command.
257 #include "korg1212-firmware.h"
259 typedef struct _snd_korg1212 korg1212_t;
261 typedef u16 K1212Sample; // channels 0-9 use 16 bit samples
262 typedef u32 K1212SpdifSample; // channels 10-11 use 32 bits - only 20 are sent
263 // across S/PDIF.
264 typedef u32 K1212TimeCodeSample; // holds the ADAT timecode value
266 typedef enum {
267 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
268 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
269 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
270 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
271 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
272 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
273 K1212_CLKIDX_Invalid // used to check validity of the index
274 } ClockSourceIndex;
276 typedef enum {
277 K1212_CLKIDX_Adat = 0, // selects source as ADAT
278 K1212_CLKIDX_Word, // selects source as S/PDIF
279 K1212_CLKIDX_Local // selects source as local clock
280 } ClockSourceType;
282 typedef struct KorgAudioFrame {
283 K1212Sample frameData16[k16BitChannels];
284 K1212SpdifSample frameData32[k32BitChannels];
285 K1212TimeCodeSample timeCodeVal;
286 } KorgAudioFrame;
288 typedef struct KorgAudioBuffer {
289 KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
290 } KorgAudioBuffer;
292 typedef struct KorgSharedBuffer {
293 #ifdef K1212_LARGEALLOC
294 KorgAudioBuffer playDataBufs[kNumBuffers];
295 KorgAudioBuffer recordDataBufs[kNumBuffers];
296 #endif
297 short volumeData[kAudioChannels];
298 u32 cardCommand;
299 u16 routeData [kAudioChannels];
300 u32 AdatTimeCode; // ADAT timecode value
301 } KorgSharedBuffer;
303 typedef struct SensBits {
304 union {
305 struct {
306 unsigned int leftChanVal:8;
307 unsigned int leftChanId:8;
308 } v;
309 u16 leftSensBits;
310 } l;
311 union {
312 struct {
313 unsigned int rightChanVal:8;
314 unsigned int rightChanId:8;
315 } v;
316 u16 rightSensBits;
317 } r;
318 } SensBits;
320 struct _snd_korg1212 {
321 snd_card_t *card;
322 struct pci_dev *pci;
323 snd_pcm_t *pcm;
324 int irq;
326 spinlock_t lock;
327 struct semaphore open_mutex;
329 struct timer_list timer; /* timer callback for checking ack of stop request */
330 int stop_pending_cnt; /* counter for stop pending check */
332 wait_queue_head_t wait;
334 unsigned long iomem;
335 unsigned long ioport;
336 unsigned long iomem2;
337 unsigned long irqcount;
338 unsigned long inIRQ;
339 void __iomem *iobase;
341 struct snd_dma_buffer dma_dsp;
342 struct snd_dma_buffer dma_play;
343 struct snd_dma_buffer dma_rec;
344 struct snd_dma_buffer dma_shared;
346 u32 dspCodeSize;
348 u32 DataBufsSize;
350 KorgAudioBuffer * playDataBufsPtr;
351 KorgAudioBuffer * recordDataBufsPtr;
353 KorgSharedBuffer * sharedBufferPtr;
355 u32 RecDataPhy;
356 u32 PlayDataPhy;
357 unsigned long sharedBufferPhy;
358 u32 VolumeTablePhy;
359 u32 RoutingTablePhy;
360 u32 AdatTimeCodePhy;
362 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
363 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
364 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
365 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
366 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
367 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
368 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
369 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
370 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
371 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
373 size_t periodsize;
374 int channels;
375 int currentBuffer;
377 snd_pcm_substream_t *playback_substream;
378 snd_pcm_substream_t *capture_substream;
380 pid_t capture_pid;
381 pid_t playback_pid;
383 CardState cardState;
384 int running;
385 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
386 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
388 ClockSourceIndex clkSrcRate; // sample rate and clock source
390 ClockSourceType clkSource; // clock source
391 int clkRate; // clock rate
393 int volumePhase[kAudioChannels];
395 u16 leftADCInSens; // ADC left channel input sensitivity
396 u16 rightADCInSens; // ADC right channel input sensitivity
398 int opencnt; // Open/Close count
399 int setcnt; // SetupForPlay count
400 int playcnt; // TriggerPlay count
401 int errorcnt; // Error Count
402 unsigned long totalerrorcnt; // Total Error Count
404 int dsp_is_loaded;
405 int dsp_stop_is_processed;
409 MODULE_DESCRIPTION("korg1212");
410 MODULE_LICENSE("GPL");
411 MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
413 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
414 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
415 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
417 module_param_array(index, int, NULL, 0444);
418 MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
419 module_param_array(id, charp, NULL, 0444);
420 MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
421 module_param_array(enable, bool, NULL, 0444);
422 MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
423 MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
425 static struct pci_device_id snd_korg1212_ids[] = {
427 .vendor = 0x10b5,
428 .device = 0x906d,
429 .subvendor = PCI_ANY_ID,
430 .subdevice = PCI_ANY_ID,
432 { 0, },
435 static char* stateName[] = {
436 "Non-existent",
437 "Uninitialized",
438 "DSP download in process",
439 "DSP download complete",
440 "Ready",
441 "Open",
442 "Setup for play",
443 "Playing",
444 "Monitor mode on",
445 "Calibrating"
446 "Invalid"
449 static char* clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
451 static char* clockSourceName[] = {
452 "ADAT at 44.1 kHz",
453 "ADAT at 48 kHz",
454 "S/PDIF at 44.1 kHz",
455 "S/PDIF at 48 kHz",
456 "local clock at 44.1 kHz",
457 "local clock at 48 kHz"
460 static char* channelName[] = {
461 "ADAT-1",
462 "ADAT-2",
463 "ADAT-3",
464 "ADAT-4",
465 "ADAT-5",
466 "ADAT-6",
467 "ADAT-7",
468 "ADAT-8",
469 "Analog-L",
470 "Analog-R",
471 "SPDIF-L",
472 "SPDIF-R",
475 static u16 ClockSourceSelector[] =
476 {0x8000, // selects source as ADAT at 44.1 kHz
477 0x0000, // selects source as ADAT at 48 kHz
478 0x8001, // selects source as S/PDIF at 44.1 kHz
479 0x0001, // selects source as S/PDIF at 48 kHz
480 0x8002, // selects source as local clock at 44.1 kHz
481 0x0002 // selects source as local clock at 48 kHz
484 static snd_korg1212rc rc;
486 MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
488 typedef union swap_u32 { unsigned char c[4]; u32 i; } swap_u32;
490 #ifdef SNDRV_BIG_ENDIAN
491 static u32 LowerWordSwap(u32 swappee)
492 #else
493 static u32 UpperWordSwap(u32 swappee)
494 #endif
496 swap_u32 retVal, swapper;
498 swapper.i = swappee;
499 retVal.c[2] = swapper.c[3];
500 retVal.c[3] = swapper.c[2];
501 retVal.c[1] = swapper.c[1];
502 retVal.c[0] = swapper.c[0];
504 return retVal.i;
507 #ifdef SNDRV_BIG_ENDIAN
508 static u32 UpperWordSwap(u32 swappee)
509 #else
510 static u32 LowerWordSwap(u32 swappee)
511 #endif
513 swap_u32 retVal, swapper;
515 swapper.i = swappee;
516 retVal.c[2] = swapper.c[2];
517 retVal.c[3] = swapper.c[3];
518 retVal.c[1] = swapper.c[0];
519 retVal.c[0] = swapper.c[1];
521 return retVal.i;
524 #if 0 /* not used */
526 static u32 EndianSwap(u32 swappee)
528 swap_u32 retVal, swapper;
530 swapper.i = swappee;
531 retVal.c[0] = swapper.c[3];
532 retVal.c[1] = swapper.c[2];
533 retVal.c[2] = swapper.c[1];
534 retVal.c[3] = swapper.c[0];
536 return retVal.i;
539 #endif /* not used */
541 #define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
542 #define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
543 #define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
544 #define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
546 static snd_korg1212rc snd_korg1212_Send1212Command(korg1212_t *korg1212, korg1212_dbcnst_t doorbellVal,
547 u32 mailBox0Val, u32 mailBox1Val, u32 mailBox2Val, u32 mailBox3Val)
549 u32 retryCount;
550 u16 mailBox3Lo;
551 snd_korg1212rc rc = K1212_CMDRET_Success;
553 if (!korg1212->outDoorbellPtr) {
554 #if K1212_DEBUG_LEVEL > 1
555 K1212_DEBUG_PRINTK("K1212_DEBUG: CardUninitialized\n");
556 #endif
557 return K1212_CMDRET_CardUninitialized;
560 #if K1212_DEBUG_LEVEL > 0
561 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n", doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
562 #endif
563 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
564 writel(mailBox3Val, korg1212->mailbox3Ptr);
565 writel(mailBox2Val, korg1212->mailbox2Ptr);
566 writel(mailBox1Val, korg1212->mailbox1Ptr);
567 writel(mailBox0Val, korg1212->mailbox0Ptr);
568 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
570 // --------------------------------------------------------------
571 // the reboot command will not give an acknowledgement.
572 // --------------------------------------------------------------
573 if ( doorbellVal == K1212_DB_RebootCard ||
574 doorbellVal == K1212_DB_BootFromDSPPage4 ||
575 doorbellVal == K1212_DB_StartDSPDownload ) {
576 rc = K1212_CMDRET_Success;
577 break;
580 // --------------------------------------------------------------
581 // See if the card acknowledged the command. Wait a bit, then
582 // read in the low word of mailbox3. If the MSB is set and the
583 // low byte is equal to the doorbell value, then it ack'd.
584 // --------------------------------------------------------------
585 udelay(COMMAND_ACK_DELAY);
586 mailBox3Lo = readl(korg1212->mailbox3Ptr);
587 if (mailBox3Lo & COMMAND_ACK_MASK) {
588 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
589 #if K1212_DEBUG_LEVEL > 1
590 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- Success\n");
591 #endif
592 rc = K1212_CMDRET_Success;
593 break;
597 korg1212->cmdRetryCount += retryCount;
599 if (retryCount >= MAX_COMMAND_RETRIES) {
600 #if K1212_DEBUG_LEVEL > 1
601 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- NoAckFromCard\n");
602 #endif
603 rc = K1212_CMDRET_NoAckFromCard;
606 return rc;
609 /* spinlock already held */
610 static void snd_korg1212_SendStop(korg1212_t *korg1212)
612 if (! korg1212->stop_pending_cnt) {
613 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
614 /* program the timer */
615 korg1212->stop_pending_cnt = HZ;
616 korg1212->timer.expires = jiffies + 1;
617 add_timer(&korg1212->timer);
621 static void snd_korg1212_SendStopAndWait(korg1212_t *korg1212)
623 unsigned long flags;
624 spin_lock_irqsave(&korg1212->lock, flags);
625 korg1212->dsp_stop_is_processed = 0;
626 snd_korg1212_SendStop(korg1212);
627 spin_unlock_irqrestore(&korg1212->lock, flags);
628 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
631 /* timer callback for checking the ack of stop request */
632 static void snd_korg1212_timer_func(unsigned long data)
634 korg1212_t *korg1212 = (korg1212_t *) data;
636 spin_lock(&korg1212->lock);
637 if (korg1212->sharedBufferPtr->cardCommand == 0) {
638 /* ack'ed */
639 korg1212->stop_pending_cnt = 0;
640 korg1212->dsp_stop_is_processed = 1;
641 wake_up(&korg1212->wait);
642 #if K1212_DEBUG_LEVEL > 1
643 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop ack'ed [%s]\n", stateName[korg1212->cardState]);
644 #endif
645 } else {
646 if (--korg1212->stop_pending_cnt > 0) {
647 /* reprogram timer */
648 korg1212->timer.expires = jiffies + 1;
649 add_timer(&korg1212->timer);
650 } else {
651 snd_printd("korg1212_timer_func timeout\n");
652 korg1212->sharedBufferPtr->cardCommand = 0;
653 korg1212->dsp_stop_is_processed = 1;
654 wake_up(&korg1212->wait);
655 #if K1212_DEBUG_LEVEL > 0
656 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n", stateName[korg1212->cardState]);
657 #endif
660 spin_unlock(&korg1212->lock);
663 static void snd_korg1212_TurnOnIdleMonitor(korg1212_t *korg1212)
665 unsigned long flags;
667 udelay(INTERCOMMAND_DELAY);
668 spin_lock_irqsave(&korg1212->lock, flags);
669 korg1212->idleMonitorOn = 1;
670 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
671 K1212_MODE_MonitorOn, 0, 0, 0);
672 spin_unlock_irqrestore(&korg1212->lock, flags);
675 static void snd_korg1212_TurnOffIdleMonitor(korg1212_t *korg1212)
677 if (korg1212->idleMonitorOn) {
678 snd_korg1212_SendStopAndWait(korg1212);
679 korg1212->idleMonitorOn = 0;
683 static inline void snd_korg1212_setCardState(korg1212_t * korg1212, CardState csState)
685 korg1212->cardState = csState;
688 static int snd_korg1212_OpenCard(korg1212_t * korg1212)
690 #if K1212_DEBUG_LEVEL > 0
691 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n", stateName[korg1212->cardState], korg1212->opencnt);
692 #endif
693 down(&korg1212->open_mutex);
694 if (korg1212->opencnt++ == 0) {
695 snd_korg1212_TurnOffIdleMonitor(korg1212);
696 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
699 up(&korg1212->open_mutex);
700 return 1;
703 static int snd_korg1212_CloseCard(korg1212_t * korg1212)
705 #if K1212_DEBUG_LEVEL > 0
706 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n", stateName[korg1212->cardState], korg1212->opencnt);
707 #endif
709 down(&korg1212->open_mutex);
710 if (--(korg1212->opencnt)) {
711 up(&korg1212->open_mutex);
712 return 0;
715 if (korg1212->cardState == K1212_STATE_SETUP) {
716 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
717 K1212_MODE_StopPlay, 0, 0, 0);
718 #if K1212_DEBUG_LEVEL > 0
719 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
720 #endif
722 if (rc != K1212_CMDRET_Success) {
723 up(&korg1212->open_mutex);
724 return 0;
726 } else if (korg1212->cardState > K1212_STATE_SETUP) {
727 snd_korg1212_SendStopAndWait(korg1212);
730 if (korg1212->cardState > K1212_STATE_READY) {
731 snd_korg1212_TurnOnIdleMonitor(korg1212);
732 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
735 up(&korg1212->open_mutex);
736 return 0;
739 /* spinlock already held */
740 static int snd_korg1212_SetupForPlay(korg1212_t * korg1212)
742 #if K1212_DEBUG_LEVEL > 0
743 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n", stateName[korg1212->cardState], korg1212->setcnt);
744 #endif
746 if (korg1212->setcnt++)
747 return 0;
749 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
750 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
751 K1212_MODE_SetupPlay, 0, 0, 0);
753 #if K1212_DEBUG_LEVEL > 0
754 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
755 #endif
756 if (rc != K1212_CMDRET_Success) {
757 return 1;
759 return 0;
762 /* spinlock already held */
763 static int snd_korg1212_TriggerPlay(korg1212_t * korg1212)
765 #if K1212_DEBUG_LEVEL > 0
766 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n", stateName[korg1212->cardState], korg1212->playcnt);
767 #endif
769 if (korg1212->playcnt++)
770 return 0;
772 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
773 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
775 #if K1212_DEBUG_LEVEL > 0
776 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
777 #endif
779 if (rc != K1212_CMDRET_Success) {
780 return 1;
782 return 0;
785 /* spinlock already held */
786 static int snd_korg1212_StopPlay(korg1212_t * korg1212)
788 #if K1212_DEBUG_LEVEL > 0
789 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n", stateName[korg1212->cardState], korg1212->playcnt);
790 #endif
792 if (--(korg1212->playcnt))
793 return 0;
795 korg1212->setcnt = 0;
797 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
798 snd_korg1212_SendStop(korg1212);
800 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
801 return 0;
804 static void snd_korg1212_EnableCardInterrupts(korg1212_t * korg1212)
806 writel(PCI_INT_ENABLE_BIT |
807 PCI_DOORBELL_INT_ENABLE_BIT |
808 LOCAL_INT_ENABLE_BIT |
809 LOCAL_DOORBELL_INT_ENABLE_BIT |
810 LOCAL_DMA1_INT_ENABLE_BIT,
811 korg1212->statusRegPtr);
814 #if 0 /* not used */
816 static int snd_korg1212_SetMonitorMode(korg1212_t *korg1212, MonitorModeSelector mode)
818 #if K1212_DEBUG_LEVEL > 0
819 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n", stateName[korg1212->cardState]);
820 #endif
822 switch (mode) {
823 case K1212_MONMODE_Off:
824 if (korg1212->cardState != K1212_STATE_MONITOR) {
825 return 0;
826 } else {
827 snd_korg1212_SendStopAndWait(korg1212);
828 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
830 break;
832 case K1212_MONMODE_On:
833 if (korg1212->cardState != K1212_STATE_OPEN) {
834 return 0;
835 } else {
836 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
837 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
838 K1212_MODE_MonitorOn, 0, 0, 0);
839 if (rc != K1212_CMDRET_Success) {
840 return 0;
843 break;
845 default:
846 return 0;
849 return 1;
852 #endif /* not used */
854 static inline int snd_korg1212_use_is_exclusive(korg1212_t *korg1212)
856 int ret = 1;
858 if ((korg1212->playback_pid != korg1212->capture_pid) &&
859 (korg1212->playback_pid >= 0) && (korg1212->capture_pid >= 0)) {
860 ret = 0;
862 return ret;
865 static int snd_korg1212_SetRate(korg1212_t *korg1212, int rate)
867 static ClockSourceIndex s44[] = { K1212_CLKIDX_AdatAt44_1K,
868 K1212_CLKIDX_WordAt44_1K,
869 K1212_CLKIDX_LocalAt44_1K };
870 static ClockSourceIndex s48[] = {
871 K1212_CLKIDX_AdatAt48K,
872 K1212_CLKIDX_WordAt48K,
873 K1212_CLKIDX_LocalAt48K };
874 int parm;
876 if (!snd_korg1212_use_is_exclusive (korg1212)) {
877 return -EBUSY;
880 switch(rate) {
881 case 44100:
882 parm = s44[korg1212->clkSource];
883 break;
885 case 48000:
886 parm = s48[korg1212->clkSource];
887 break;
889 default:
890 return -EINVAL;
893 korg1212->clkSrcRate = parm;
894 korg1212->clkRate = rate;
896 udelay(INTERCOMMAND_DELAY);
897 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
898 ClockSourceSelector[korg1212->clkSrcRate],
899 0, 0, 0);
901 #if K1212_DEBUG_LEVEL > 0
902 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
903 #endif
905 return 0;
908 static int snd_korg1212_SetClockSource(korg1212_t *korg1212, int source)
911 if (source<0 || source >2)
912 return -EINVAL;
914 korg1212->clkSource = source;
916 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
918 return 0;
921 static void snd_korg1212_DisableCardInterrupts(korg1212_t *korg1212)
923 writel(0, korg1212->statusRegPtr);
926 static int snd_korg1212_WriteADCSensitivity(korg1212_t *korg1212)
928 SensBits sensVals;
929 int bitPosition;
930 int channel;
931 int clkIs48K;
932 int monModeSet;
933 u16 controlValue; // this keeps the current value to be written to
934 // the card's eeprom control register.
935 u16 count;
936 unsigned long flags;
938 #if K1212_DEBUG_LEVEL > 0
939 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n", stateName[korg1212->cardState]);
940 #endif
942 // ----------------------------------------------------------------------------
943 // initialize things. The local init bit is always set when writing to the
944 // card's control register.
945 // ----------------------------------------------------------------------------
946 controlValue = 0;
947 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
949 // ----------------------------------------------------------------------------
950 // make sure the card is not in monitor mode when we do this update.
951 // ----------------------------------------------------------------------------
952 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
953 monModeSet = 1;
954 snd_korg1212_SendStopAndWait(korg1212);
955 } else
956 monModeSet = 0;
958 spin_lock_irqsave(&korg1212->lock, flags);
960 // ----------------------------------------------------------------------------
961 // we are about to send new values to the card, so clear the new values queued
962 // flag. Also, clear out mailbox 3, so we don't lockup.
963 // ----------------------------------------------------------------------------
964 writel(0, korg1212->mailbox3Ptr);
965 udelay(LOADSHIFT_DELAY);
967 // ----------------------------------------------------------------------------
968 // determine whether we are running a 48K or 44.1K clock. This info is used
969 // later when setting the SPDIF FF after the volume has been shifted in.
970 // ----------------------------------------------------------------------------
971 switch (korg1212->clkSrcRate) {
972 case K1212_CLKIDX_AdatAt44_1K:
973 case K1212_CLKIDX_WordAt44_1K:
974 case K1212_CLKIDX_LocalAt44_1K:
975 clkIs48K = 0;
976 break;
978 case K1212_CLKIDX_WordAt48K:
979 case K1212_CLKIDX_AdatAt48K:
980 case K1212_CLKIDX_LocalAt48K:
981 default:
982 clkIs48K = 1;
983 break;
986 // ----------------------------------------------------------------------------
987 // start the update. Setup the bit structure and then shift the bits.
988 // ----------------------------------------------------------------------------
989 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
990 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
991 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
992 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
994 // ----------------------------------------------------------------------------
995 // now start shifting the bits in. Start with the left channel then the right.
996 // ----------------------------------------------------------------------------
997 for (channel = 0; channel < 2; channel++) {
999 // ----------------------------------------------------------------------------
1000 // Bring the load/shift line low, then wait - the spec says >150ns from load/
1001 // shift low to the first rising edge of the clock.
1002 // ----------------------------------------------------------------------------
1003 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1004 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1005 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
1006 udelay(LOADSHIFT_DELAY);
1008 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
1009 if (channel == 0) {
1010 if (sensVals.l.leftSensBits & (0x0001 << bitPosition)) {
1011 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
1012 } else {
1013 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
1015 } else {
1016 if (sensVals.r.rightSensBits & (0x0001 << bitPosition)) {
1017 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
1018 } else {
1019 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
1023 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1024 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1025 udelay(SENSCLKPULSE_WIDTH);
1026 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1027 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1028 udelay(SENSCLKPULSE_WIDTH);
1031 // ----------------------------------------------------------------------------
1032 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1033 // bit if the clock rate is 48K otherwise write 0.
1034 // ----------------------------------------------------------------------------
1035 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1036 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1037 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1038 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1039 udelay(SENSCLKPULSE_WIDTH);
1041 if (clkIs48K)
1042 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1044 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1045 udelay(ONE_RTC_TICK);
1046 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1047 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1048 udelay(SENSCLKPULSE_WIDTH);
1049 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1050 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1051 udelay(SENSCLKPULSE_WIDTH);
1054 // ----------------------------------------------------------------------------
1055 // The update is complete. Set a timeout. This is the inter-update delay.
1056 // Also, if the card was in monitor mode, restore it.
1057 // ----------------------------------------------------------------------------
1058 for (count = 0; count < 10; count++)
1059 udelay(SENSCLKPULSE_WIDTH);
1061 if (monModeSet) {
1062 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1063 K1212_MODE_MonitorOn, 0, 0, 0);
1064 #if K1212_DEBUG_LEVEL > 0
1065 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1066 #endif
1070 spin_unlock_irqrestore(&korg1212->lock, flags);
1072 return 1;
1075 static void snd_korg1212_OnDSPDownloadComplete(korg1212_t *korg1212)
1077 int channel;
1079 #if K1212_DEBUG_LEVEL > 0
1080 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n", stateName[korg1212->cardState]);
1081 #endif
1083 // ----------------------------------------------------
1084 // tell the card to boot
1085 // ----------------------------------------------------
1086 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1088 #if K1212_DEBUG_LEVEL > 0
1089 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1090 #endif
1091 mdelay(DSP_BOOT_DELAY_IN_MS);
1093 // --------------------------------------------------------------------------------
1094 // Let the card know where all the buffers are.
1095 // --------------------------------------------------------------------------------
1096 rc = snd_korg1212_Send1212Command(korg1212,
1097 K1212_DB_ConfigureBufferMemory,
1098 LowerWordSwap(korg1212->PlayDataPhy),
1099 LowerWordSwap(korg1212->RecDataPhy),
1100 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1101 // is based on 2 buffers
1105 #if K1212_DEBUG_LEVEL > 0
1106 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1107 #endif
1109 udelay(INTERCOMMAND_DELAY);
1111 rc = snd_korg1212_Send1212Command(korg1212,
1112 K1212_DB_ConfigureMiscMemory,
1113 LowerWordSwap(korg1212->VolumeTablePhy),
1114 LowerWordSwap(korg1212->RoutingTablePhy),
1115 LowerWordSwap(korg1212->AdatTimeCodePhy),
1119 #if K1212_DEBUG_LEVEL > 0
1120 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1121 #endif
1124 // --------------------------------------------------------------------------------
1125 // Initialize the routing and volume tables, then update the card's state.
1126 // --------------------------------------------------------------------------------
1127 udelay(INTERCOMMAND_DELAY);
1129 for (channel = 0; channel < kAudioChannels; channel++) {
1130 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1131 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1132 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1135 snd_korg1212_WriteADCSensitivity(korg1212);
1137 udelay(INTERCOMMAND_DELAY);
1138 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1139 ClockSourceSelector[korg1212->clkSrcRate],
1140 0, 0, 0);
1141 #if K1212_DEBUG_LEVEL > 0
1142 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1143 #endif
1145 snd_korg1212_TurnOnIdleMonitor(korg1212);
1146 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1148 #if K1212_DEBUG_LEVEL > 0
1149 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1150 #endif
1152 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1155 static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1157 u32 doorbellValue;
1158 korg1212_t *korg1212 = dev_id;
1160 if(irq != korg1212->irq)
1161 return IRQ_NONE;
1163 doorbellValue = readl(korg1212->inDoorbellPtr);
1165 if (!doorbellValue)
1166 return IRQ_NONE;
1168 spin_lock(&korg1212->lock);
1170 writel(doorbellValue, korg1212->inDoorbellPtr);
1172 korg1212->irqcount++;
1174 korg1212->inIRQ++;
1177 switch (doorbellValue) {
1178 case K1212_DB_DSPDownloadDone:
1179 #if K1212_DEBUG_LEVEL > 0
1180 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n", korg1212->irqcount, doorbellValue, stateName[korg1212->cardState]);
1181 #endif
1182 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1183 korg1212->dsp_is_loaded = 1;
1184 wake_up(&korg1212->wait);
1186 break;
1188 // ------------------------------------------------------------------------
1189 // an error occurred - stop the card
1190 // ------------------------------------------------------------------------
1191 case K1212_DB_DMAERROR:
1192 #if K1212_DEBUG_LEVEL > 1
1193 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n", korg1212->irqcount, doorbellValue, stateName[korg1212->cardState]);
1194 #endif
1195 snd_printk(KERN_ERR "korg1212: DMA Error\n");
1196 korg1212->errorcnt++;
1197 korg1212->totalerrorcnt++;
1198 korg1212->sharedBufferPtr->cardCommand = 0;
1199 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1200 break;
1202 // ------------------------------------------------------------------------
1203 // the card has stopped by our request. Clear the command word and signal
1204 // the semaphore in case someone is waiting for this.
1205 // ------------------------------------------------------------------------
1206 case K1212_DB_CARDSTOPPED:
1207 #if K1212_DEBUG_LEVEL > 1
1208 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n", korg1212->irqcount, doorbellValue, stateName[korg1212->cardState]);
1209 #endif
1210 korg1212->sharedBufferPtr->cardCommand = 0;
1211 break;
1213 default:
1214 #if K1212_DEBUG_LEVEL > 3
1215 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n", korg1212->irqcount, doorbellValue,
1216 korg1212->currentBuffer, stateName[korg1212->cardState]);
1217 #endif
1218 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1219 korg1212->currentBuffer++;
1221 if (korg1212->currentBuffer >= kNumBuffers)
1222 korg1212->currentBuffer = 0;
1224 if (!korg1212->running)
1225 break;
1227 if (korg1212->capture_substream) {
1228 spin_unlock(&korg1212->lock);
1229 snd_pcm_period_elapsed(korg1212->capture_substream);
1230 spin_lock(&korg1212->lock);
1233 if (korg1212->playback_substream) {
1234 spin_unlock(&korg1212->lock);
1235 snd_pcm_period_elapsed(korg1212->playback_substream);
1236 spin_lock(&korg1212->lock);
1239 break;
1242 korg1212->inIRQ--;
1244 spin_unlock(&korg1212->lock);
1246 return IRQ_HANDLED;
1249 static int snd_korg1212_downloadDSPCode(korg1212_t *korg1212)
1252 #if K1212_DEBUG_LEVEL > 0
1253 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n", stateName[korg1212->cardState]);
1254 #endif
1256 // ---------------------------------------------------------------
1257 // verify the state of the card before proceeding.
1258 // ---------------------------------------------------------------
1259 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS) {
1260 return 1;
1263 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1265 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1267 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1268 UpperWordSwap(korg1212->dma_dsp.addr),
1269 0, 0, 0);
1271 #if K1212_DEBUG_LEVEL > 0
1272 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1273 #endif
1275 korg1212->dsp_is_loaded = 0;
1276 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1277 if (! korg1212->dsp_is_loaded )
1278 return -EBUSY; /* timeout */
1280 snd_korg1212_OnDSPDownloadComplete(korg1212);
1282 return 0;
1285 static snd_pcm_hardware_t snd_korg1212_playback_info =
1287 .info = (SNDRV_PCM_INFO_MMAP |
1288 SNDRV_PCM_INFO_MMAP_VALID |
1289 SNDRV_PCM_INFO_INTERLEAVED),
1290 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1291 .rates = (SNDRV_PCM_RATE_44100 |
1292 SNDRV_PCM_RATE_48000),
1293 .rate_min = 44100,
1294 .rate_max = 48000,
1295 .channels_min = K1212_MIN_CHANNELS,
1296 .channels_max = K1212_MAX_CHANNELS,
1297 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1298 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1299 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1300 .periods_min = K1212_PERIODS,
1301 .periods_max = K1212_PERIODS,
1302 .fifo_size = 0,
1305 static snd_pcm_hardware_t snd_korg1212_capture_info =
1307 .info = (SNDRV_PCM_INFO_MMAP |
1308 SNDRV_PCM_INFO_MMAP_VALID |
1309 SNDRV_PCM_INFO_INTERLEAVED),
1310 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1311 .rates = (SNDRV_PCM_RATE_44100 |
1312 SNDRV_PCM_RATE_48000),
1313 .rate_min = 44100,
1314 .rate_max = 48000,
1315 .channels_min = K1212_MIN_CHANNELS,
1316 .channels_max = K1212_MAX_CHANNELS,
1317 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1318 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1319 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1320 .periods_min = K1212_PERIODS,
1321 .periods_max = K1212_PERIODS,
1322 .fifo_size = 0,
1325 static int snd_korg1212_silence(korg1212_t *korg1212, int pos, int count, int offset, int size)
1327 KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1328 int i;
1330 #if K1212_DEBUG_LEVEL > 2
1331 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n", pos, offset, size, count);
1332 #endif
1333 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1335 for (i=0; i < count; i++) {
1336 #if K1212_DEBUG_LEVEL > 0
1337 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1338 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1339 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n", dst, i);
1340 return -EFAULT;
1342 #endif
1343 memset((void*) dst + offset, 0, size);
1344 dst++;
1347 return 0;
1350 static int snd_korg1212_copy_to(korg1212_t *korg1212, void __user *dst, int pos, int count, int offset, int size)
1352 KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1353 int i, rc;
1355 #if K1212_DEBUG_LEVEL > 2
1356 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n", pos, offset, size);
1357 #endif
1358 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1360 for (i=0; i < count; i++) {
1361 #if K1212_DEBUG_LEVEL > 0
1362 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1363 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
1364 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1365 return -EFAULT;
1367 #endif
1368 rc = copy_to_user(dst + offset, src, size);
1369 if (rc) {
1370 #if K1212_DEBUG_LEVEL > 0
1371 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1372 #endif
1373 return -EFAULT;
1375 src++;
1376 dst += size;
1379 return 0;
1382 static int snd_korg1212_copy_from(korg1212_t *korg1212, void __user *src, int pos, int count, int offset, int size)
1384 KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1385 int i, rc;
1387 #if K1212_DEBUG_LEVEL > 2
1388 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n", pos, offset, size, count);
1389 #endif
1391 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1393 for (i=0; i < count; i++) {
1394 #if K1212_DEBUG_LEVEL > 0
1395 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1396 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
1397 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1398 return -EFAULT;
1400 #endif
1401 rc = copy_from_user((void*) dst + offset, src, size);
1402 if (rc) {
1403 #if K1212_DEBUG_LEVEL > 0
1404 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1405 #endif
1406 return -EFAULT;
1408 dst++;
1409 src += size;
1412 return 0;
1415 static void snd_korg1212_free_pcm(snd_pcm_t *pcm)
1417 korg1212_t *korg1212 = (korg1212_t *) pcm->private_data;
1419 #if K1212_DEBUG_LEVEL > 0
1420 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n", stateName[korg1212->cardState]);
1421 #endif
1423 korg1212->pcm = NULL;
1426 static int snd_korg1212_playback_open(snd_pcm_substream_t *substream)
1428 unsigned long flags;
1429 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1430 snd_pcm_runtime_t *runtime = substream->runtime;
1432 #if K1212_DEBUG_LEVEL > 0
1433 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n", stateName[korg1212->cardState]);
1434 #endif
1436 snd_pcm_set_sync(substream); // ???
1438 snd_korg1212_OpenCard(korg1212);
1440 runtime->hw = snd_korg1212_playback_info;
1441 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1443 spin_lock_irqsave(&korg1212->lock, flags);
1445 korg1212->playback_substream = substream;
1446 korg1212->playback_pid = current->pid;
1447 korg1212->periodsize = K1212_PERIODS;
1448 korg1212->channels = K1212_CHANNELS;
1449 korg1212->errorcnt = 0;
1451 spin_unlock_irqrestore(&korg1212->lock, flags);
1453 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1454 return 0;
1458 static int snd_korg1212_capture_open(snd_pcm_substream_t *substream)
1460 unsigned long flags;
1461 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1462 snd_pcm_runtime_t *runtime = substream->runtime;
1464 #if K1212_DEBUG_LEVEL > 0
1465 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n", stateName[korg1212->cardState]);
1466 #endif
1468 snd_pcm_set_sync(substream);
1470 snd_korg1212_OpenCard(korg1212);
1472 runtime->hw = snd_korg1212_capture_info;
1473 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1475 spin_lock_irqsave(&korg1212->lock, flags);
1477 korg1212->capture_substream = substream;
1478 korg1212->capture_pid = current->pid;
1479 korg1212->periodsize = K1212_PERIODS;
1480 korg1212->channels = K1212_CHANNELS;
1482 spin_unlock_irqrestore(&korg1212->lock, flags);
1484 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1485 return 0;
1488 static int snd_korg1212_playback_close(snd_pcm_substream_t *substream)
1490 unsigned long flags;
1491 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1493 #if K1212_DEBUG_LEVEL > 0
1494 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n", stateName[korg1212->cardState]);
1495 #endif
1497 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1499 spin_lock_irqsave(&korg1212->lock, flags);
1501 korg1212->playback_pid = -1;
1502 korg1212->playback_substream = NULL;
1503 korg1212->periodsize = 0;
1505 spin_unlock_irqrestore(&korg1212->lock, flags);
1507 snd_korg1212_CloseCard(korg1212);
1508 return 0;
1511 static int snd_korg1212_capture_close(snd_pcm_substream_t *substream)
1513 unsigned long flags;
1514 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1516 #if K1212_DEBUG_LEVEL > 0
1517 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n", stateName[korg1212->cardState]);
1518 #endif
1520 spin_lock_irqsave(&korg1212->lock, flags);
1522 korg1212->capture_pid = -1;
1523 korg1212->capture_substream = NULL;
1524 korg1212->periodsize = 0;
1526 spin_unlock_irqrestore(&korg1212->lock, flags);
1528 snd_korg1212_CloseCard(korg1212);
1529 return 0;
1532 static int snd_korg1212_ioctl(snd_pcm_substream_t *substream,
1533 unsigned int cmd, void *arg)
1535 #if K1212_DEBUG_LEVEL > 0
1536 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1537 #endif
1539 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
1540 snd_pcm_channel_info_t *info = arg;
1541 info->offset = 0;
1542 info->first = info->channel * 16;
1543 info->step = 256;
1544 #if K1212_DEBUG_LEVEL > 0
1545 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
1546 #endif
1547 return 0;
1550 return snd_pcm_lib_ioctl(substream, cmd, arg);
1553 static int snd_korg1212_hw_params(snd_pcm_substream_t *substream,
1554 snd_pcm_hw_params_t *params)
1556 unsigned long flags;
1557 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1558 int err;
1559 pid_t this_pid;
1560 pid_t other_pid;
1562 #if K1212_DEBUG_LEVEL > 0
1563 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n", stateName[korg1212->cardState]);
1564 #endif
1566 spin_lock_irqsave(&korg1212->lock, flags);
1568 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1569 this_pid = korg1212->playback_pid;
1570 other_pid = korg1212->capture_pid;
1571 } else {
1572 this_pid = korg1212->capture_pid;
1573 other_pid = korg1212->playback_pid;
1576 if ((other_pid > 0) && (this_pid != other_pid)) {
1578 /* The other stream is open, and not by the same
1579 task as this one. Make sure that the parameters
1580 that matter are the same.
1583 if ((int)params_rate(params) != korg1212->clkRate) {
1584 spin_unlock_irqrestore(&korg1212->lock, flags);
1585 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1586 return -EBUSY;
1589 spin_unlock_irqrestore(&korg1212->lock, flags);
1590 return 0;
1593 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1594 spin_unlock_irqrestore(&korg1212->lock, flags);
1595 return err;
1598 korg1212->channels = params_channels(params);
1599 korg1212->periodsize = K1212_PERIOD_BYTES;
1601 spin_unlock_irqrestore(&korg1212->lock, flags);
1603 return 0;
1606 static int snd_korg1212_prepare(snd_pcm_substream_t *substream)
1608 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1609 int rc;
1611 #if K1212_DEBUG_LEVEL > 0
1612 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n", stateName[korg1212->cardState]);
1613 #endif
1615 spin_lock_irq(&korg1212->lock);
1617 /* FIXME: we should wait for ack! */
1618 if (korg1212->stop_pending_cnt > 0) {
1619 #if K1212_DEBUG_LEVEL > 0
1620 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n", stateName[korg1212->cardState]);
1621 #endif
1622 spin_unlock_irq(&korg1212->lock);
1623 return -EAGAIN;
1625 korg1212->sharedBufferPtr->cardCommand = 0;
1626 del_timer(&korg1212->timer);
1627 korg1212->stop_pending_cnt = 0;
1631 rc = snd_korg1212_SetupForPlay(korg1212);
1633 korg1212->currentBuffer = 0;
1635 spin_unlock_irq(&korg1212->lock);
1637 return rc ? -EINVAL : 0;
1640 static int snd_korg1212_trigger(snd_pcm_substream_t *substream,
1641 int cmd)
1643 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1644 int rc;
1646 #if K1212_DEBUG_LEVEL > 0
1647 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n", stateName[korg1212->cardState], cmd);
1648 #endif
1650 spin_lock(&korg1212->lock);
1651 switch (cmd) {
1652 case SNDRV_PCM_TRIGGER_START:
1654 if (korg1212->running) {
1655 #if K1212_DEBUG_LEVEL > 1
1656 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
1657 #endif
1658 break;
1661 korg1212->running++;
1662 rc = snd_korg1212_TriggerPlay(korg1212);
1663 break;
1665 case SNDRV_PCM_TRIGGER_STOP:
1667 if (!korg1212->running) {
1668 #if K1212_DEBUG_LEVEL > 1
1669 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
1670 #endif
1671 break;
1674 korg1212->running--;
1675 rc = snd_korg1212_StopPlay(korg1212);
1676 break;
1678 default:
1679 rc = 1;
1680 break;
1682 spin_unlock(&korg1212->lock);
1683 return rc ? -EINVAL : 0;
1686 static snd_pcm_uframes_t snd_korg1212_playback_pointer(snd_pcm_substream_t *substream)
1688 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1689 snd_pcm_uframes_t pos;
1691 pos = korg1212->currentBuffer * kPlayBufferFrames;
1693 #if K1212_DEBUG_LEVEL > 2
1694 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1695 stateName[korg1212->cardState], pos);
1696 #endif
1698 return pos;
1701 static snd_pcm_uframes_t snd_korg1212_capture_pointer(snd_pcm_substream_t *substream)
1703 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1704 snd_pcm_uframes_t pos;
1706 pos = korg1212->currentBuffer * kPlayBufferFrames;
1708 #if K1212_DEBUG_LEVEL > 2
1709 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1710 stateName[korg1212->cardState], pos);
1711 #endif
1713 return pos;
1716 static int snd_korg1212_playback_copy(snd_pcm_substream_t *substream,
1717 int channel, /* not used (interleaved data) */
1718 snd_pcm_uframes_t pos,
1719 void __user *src,
1720 snd_pcm_uframes_t count)
1722 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1724 #if K1212_DEBUG_LEVEL > 2
1725 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n", stateName[korg1212->cardState], pos, count);
1726 #endif
1728 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1732 static int snd_korg1212_playback_silence(snd_pcm_substream_t *substream,
1733 int channel, /* not used (interleaved data) */
1734 snd_pcm_uframes_t pos,
1735 snd_pcm_uframes_t count)
1737 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1739 #if K1212_DEBUG_LEVEL > 0
1740 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n", stateName[korg1212->cardState]);
1741 #endif
1743 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1746 static int snd_korg1212_capture_copy(snd_pcm_substream_t *substream,
1747 int channel, /* not used (interleaved data) */
1748 snd_pcm_uframes_t pos,
1749 void __user *dst,
1750 snd_pcm_uframes_t count)
1752 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1754 #if K1212_DEBUG_LEVEL > 2
1755 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n", stateName[korg1212->cardState], pos, count);
1756 #endif
1758 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1761 static snd_pcm_ops_t snd_korg1212_playback_ops = {
1762 .open = snd_korg1212_playback_open,
1763 .close = snd_korg1212_playback_close,
1764 .ioctl = snd_korg1212_ioctl,
1765 .hw_params = snd_korg1212_hw_params,
1766 .prepare = snd_korg1212_prepare,
1767 .trigger = snd_korg1212_trigger,
1768 .pointer = snd_korg1212_playback_pointer,
1769 .copy = snd_korg1212_playback_copy,
1770 .silence = snd_korg1212_playback_silence,
1773 static snd_pcm_ops_t snd_korg1212_capture_ops = {
1774 .open = snd_korg1212_capture_open,
1775 .close = snd_korg1212_capture_close,
1776 .ioctl = snd_korg1212_ioctl,
1777 .hw_params = snd_korg1212_hw_params,
1778 .prepare = snd_korg1212_prepare,
1779 .trigger = snd_korg1212_trigger,
1780 .pointer = snd_korg1212_capture_pointer,
1781 .copy = snd_korg1212_capture_copy,
1785 * Control Interface
1788 static int snd_korg1212_control_phase_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1790 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1791 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1792 return 0;
1795 static int snd_korg1212_control_phase_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1797 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1798 int i = kcontrol->private_value;
1800 spin_lock_irq(&korg1212->lock);
1802 u->value.integer.value[0] = korg1212->volumePhase[i];
1804 if (i >= 8)
1805 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1807 spin_unlock_irq(&korg1212->lock);
1809 return 0;
1812 static int snd_korg1212_control_phase_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1814 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1815 int change = 0;
1816 int i, val;
1818 spin_lock_irq(&korg1212->lock);
1820 i = kcontrol->private_value;
1822 korg1212->volumePhase[i] = u->value.integer.value[0];
1824 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1826 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1827 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1828 korg1212->sharedBufferPtr->volumeData[i] = val;
1829 change = 1;
1832 if (i >= 8) {
1833 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1835 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1837 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1838 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1839 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1840 change = 1;
1844 spin_unlock_irq(&korg1212->lock);
1846 return change;
1849 static int snd_korg1212_control_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1851 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1852 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1853 uinfo->value.integer.min = k1212MinVolume;
1854 uinfo->value.integer.max = k1212MaxVolume;
1855 return 0;
1858 static int snd_korg1212_control_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1860 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1861 int i;
1863 spin_lock_irq(&korg1212->lock);
1865 i = kcontrol->private_value;
1866 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1868 if (i >= 8)
1869 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1871 spin_unlock_irq(&korg1212->lock);
1873 return 0;
1876 static int snd_korg1212_control_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1878 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1879 int change = 0;
1880 int i;
1881 int val;
1883 spin_lock_irq(&korg1212->lock);
1885 i = kcontrol->private_value;
1887 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1888 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1889 val *= u->value.integer.value[0];
1890 korg1212->sharedBufferPtr->volumeData[i] = val;
1891 change = 1;
1894 if (i >= 8) {
1895 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1896 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1897 val *= u->value.integer.value[1];
1898 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1899 change = 1;
1903 spin_unlock_irq(&korg1212->lock);
1905 return change;
1908 static int snd_korg1212_control_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1910 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1911 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1912 uinfo->value.enumerated.items = kAudioChannels;
1913 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1914 uinfo->value.enumerated.item = kAudioChannels-1;
1916 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1917 return 0;
1920 static int snd_korg1212_control_route_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1922 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1923 int i;
1925 spin_lock_irq(&korg1212->lock);
1927 i = kcontrol->private_value;
1928 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1930 if (i >= 8)
1931 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1933 spin_unlock_irq(&korg1212->lock);
1935 return 0;
1938 static int snd_korg1212_control_route_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1940 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1941 int change = 0, i;
1943 spin_lock_irq(&korg1212->lock);
1945 i = kcontrol->private_value;
1947 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1948 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1949 change = 1;
1952 if (i >= 8) {
1953 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1954 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1955 change = 1;
1959 spin_unlock_irq(&korg1212->lock);
1961 return change;
1964 static int snd_korg1212_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1966 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1967 uinfo->count = 2;
1968 uinfo->value.integer.min = k1212MaxADCSens;
1969 uinfo->value.integer.max = k1212MinADCSens;
1970 return 0;
1973 static int snd_korg1212_control_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1975 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1977 spin_lock_irq(&korg1212->lock);
1979 u->value.integer.value[0] = korg1212->leftADCInSens;
1980 u->value.integer.value[1] = korg1212->rightADCInSens;
1982 spin_unlock_irq(&korg1212->lock);
1984 return 0;
1987 static int snd_korg1212_control_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1989 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1990 int change = 0;
1992 spin_lock_irq(&korg1212->lock);
1994 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1995 korg1212->leftADCInSens = u->value.integer.value[0];
1996 change = 1;
1998 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1999 korg1212->rightADCInSens = u->value.integer.value[1];
2000 change = 1;
2003 spin_unlock_irq(&korg1212->lock);
2005 if (change)
2006 snd_korg1212_WriteADCSensitivity(korg1212);
2008 return change;
2011 static int snd_korg1212_control_sync_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2013 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2014 uinfo->count = 1;
2015 uinfo->value.enumerated.items = 3;
2016 if (uinfo->value.enumerated.item > 2) {
2017 uinfo->value.enumerated.item = 2;
2019 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
2020 return 0;
2023 static int snd_korg1212_control_sync_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2025 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
2027 spin_lock_irq(&korg1212->lock);
2029 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
2031 spin_unlock_irq(&korg1212->lock);
2032 return 0;
2035 static int snd_korg1212_control_sync_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2037 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
2038 unsigned int val;
2039 int change;
2041 val = ucontrol->value.enumerated.item[0] % 3;
2042 spin_lock_irq(&korg1212->lock);
2043 change = val != korg1212->clkSource;
2044 snd_korg1212_SetClockSource(korg1212, val);
2045 spin_unlock_irq(&korg1212->lock);
2046 return change;
2049 #define MON_MIXER(ord,c_name) \
2051 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2052 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2053 .name = c_name " Monitor Volume", \
2054 .info = snd_korg1212_control_volume_info, \
2055 .get = snd_korg1212_control_volume_get, \
2056 .put = snd_korg1212_control_volume_put, \
2057 .private_value = ord, \
2058 }, \
2060 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2061 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2062 .name = c_name " Monitor Route", \
2063 .info = snd_korg1212_control_route_info, \
2064 .get = snd_korg1212_control_route_get, \
2065 .put = snd_korg1212_control_route_put, \
2066 .private_value = ord, \
2067 }, \
2069 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2070 .iface = SNDRV_CTL_ELEM_IFACE_PCM, \
2071 .name = c_name " Monitor Phase Invert", \
2072 .info = snd_korg1212_control_phase_info, \
2073 .get = snd_korg1212_control_phase_get, \
2074 .put = snd_korg1212_control_phase_put, \
2075 .private_value = ord, \
2078 static snd_kcontrol_new_t snd_korg1212_controls[] = {
2079 MON_MIXER(8, "Analog"),
2080 MON_MIXER(10, "SPDIF"),
2081 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2082 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2084 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2085 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2086 .name = "Sync Source",
2087 .info = snd_korg1212_control_sync_info,
2088 .get = snd_korg1212_control_sync_get,
2089 .put = snd_korg1212_control_sync_put,
2092 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2093 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2094 .name = "ADC Attenuation",
2095 .info = snd_korg1212_control_info,
2096 .get = snd_korg1212_control_get,
2097 .put = snd_korg1212_control_put,
2102 * proc interface
2105 static void snd_korg1212_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
2107 int n;
2108 korg1212_t *korg1212 = (korg1212_t *)entry->private_data;
2110 snd_iprintf(buffer, korg1212->card->longname);
2111 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2112 snd_iprintf(buffer, "\nGeneral settings\n");
2113 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2114 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2115 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2116 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2117 snd_iprintf(buffer, " Volume Info:\n");
2118 for (n=0; n<kAudioChannels; n++)
2119 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2120 channelName[n],
2121 channelName[korg1212->sharedBufferPtr->routeData[n]],
2122 korg1212->sharedBufferPtr->volumeData[n]);
2123 snd_iprintf(buffer, "\nGeneral status\n");
2124 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2125 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2126 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2127 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2128 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2129 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2132 static void __devinit snd_korg1212_proc_init(korg1212_t *korg1212)
2134 snd_info_entry_t *entry;
2136 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2137 snd_info_set_text_ops(entry, korg1212, 1024, snd_korg1212_proc_read);
2140 static int
2141 snd_korg1212_free(korg1212_t *korg1212)
2143 snd_korg1212_TurnOffIdleMonitor(korg1212);
2145 if (korg1212->irq >= 0) {
2146 synchronize_irq(korg1212->irq);
2147 snd_korg1212_DisableCardInterrupts(korg1212);
2148 free_irq(korg1212->irq, (void *)korg1212);
2149 korg1212->irq = -1;
2152 if (korg1212->iobase != NULL) {
2153 iounmap(korg1212->iobase);
2154 korg1212->iobase = NULL;
2157 pci_release_regions(korg1212->pci);
2159 // ----------------------------------------------------
2160 // free up memory resources used for the DSP download.
2161 // ----------------------------------------------------
2162 if (korg1212->dma_dsp.area) {
2163 snd_dma_free_pages(&korg1212->dma_dsp);
2164 korg1212->dma_dsp.area = NULL;
2167 #ifndef K1212_LARGEALLOC
2169 // ------------------------------------------------------
2170 // free up memory resources used for the Play/Rec Buffers
2171 // ------------------------------------------------------
2172 if (korg1212->dma_play.area) {
2173 snd_dma_free_pages(&korg1212->dma_play);
2174 korg1212->dma_play.area = NULL;
2177 if (korg1212->dma_rec.area) {
2178 snd_dma_free_pages(&korg1212->dma_rec);
2179 korg1212->dma_rec.area = NULL;
2182 #endif
2184 // ----------------------------------------------------
2185 // free up memory resources used for the Shared Buffers
2186 // ----------------------------------------------------
2187 if (korg1212->dma_shared.area) {
2188 snd_dma_free_pages(&korg1212->dma_shared);
2189 korg1212->dma_shared.area = NULL;
2192 pci_disable_device(korg1212->pci);
2193 kfree(korg1212);
2194 return 0;
2197 static int snd_korg1212_dev_free(snd_device_t *device)
2199 korg1212_t *korg1212 = device->device_data;
2200 #if K1212_DEBUG_LEVEL > 0
2201 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
2202 #endif
2203 return snd_korg1212_free(korg1212);
2206 static int __devinit snd_korg1212_create(snd_card_t * card, struct pci_dev *pci,
2207 korg1212_t ** rchip)
2210 int err;
2211 unsigned int i;
2212 unsigned ioport_size, iomem_size, iomem2_size;
2213 korg1212_t * korg1212;
2215 static snd_device_ops_t ops = {
2216 .dev_free = snd_korg1212_dev_free,
2219 * rchip = NULL;
2220 if ((err = pci_enable_device(pci)) < 0)
2221 return err;
2223 korg1212 = kcalloc(1, sizeof(*korg1212), GFP_KERNEL);
2224 if (korg1212 == NULL) {
2225 pci_disable_device(pci);
2226 return -ENOMEM;
2229 korg1212->card = card;
2230 korg1212->pci = pci;
2232 init_waitqueue_head(&korg1212->wait);
2233 spin_lock_init(&korg1212->lock);
2234 init_MUTEX(&korg1212->open_mutex);
2235 init_timer(&korg1212->timer);
2236 korg1212->timer.function = snd_korg1212_timer_func;
2237 korg1212->timer.data = (unsigned long)korg1212;
2239 korg1212->irq = -1;
2240 korg1212->clkSource = K1212_CLKIDX_Local;
2241 korg1212->clkRate = 44100;
2242 korg1212->inIRQ = 0;
2243 korg1212->running = 0;
2244 korg1212->opencnt = 0;
2245 korg1212->playcnt = 0;
2246 korg1212->setcnt = 0;
2247 korg1212->totalerrorcnt = 0;
2248 korg1212->playback_pid = -1;
2249 korg1212->capture_pid = -1;
2250 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2251 korg1212->idleMonitorOn = 0;
2252 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2253 korg1212->leftADCInSens = k1212MaxADCSens;
2254 korg1212->rightADCInSens = k1212MaxADCSens;
2256 for (i=0; i<kAudioChannels; i++)
2257 korg1212->volumePhase[i] = 0;
2259 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2260 kfree(korg1212);
2261 pci_disable_device(pci);
2262 return err;
2265 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2266 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2267 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2269 iomem_size = pci_resource_len(korg1212->pci, 0);
2270 ioport_size = pci_resource_len(korg1212->pci, 1);
2271 iomem2_size = pci_resource_len(korg1212->pci, 2);
2273 #if K1212_DEBUG_LEVEL > 0
2274 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2275 " iomem = 0x%lx (%d)\n"
2276 " ioport = 0x%lx (%d)\n"
2277 " iomem = 0x%lx (%d)\n"
2278 " [%s]\n",
2279 korg1212->iomem, iomem_size,
2280 korg1212->ioport, ioport_size,
2281 korg1212->iomem2, iomem2_size,
2282 stateName[korg1212->cardState]);
2283 #endif
2285 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2286 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2287 korg1212->iomem + iomem_size - 1);
2288 snd_korg1212_free(korg1212);
2289 return -EBUSY;
2292 err = request_irq(pci->irq, snd_korg1212_interrupt,
2293 SA_INTERRUPT|SA_SHIRQ,
2294 "korg1212", (void *) korg1212);
2296 if (err) {
2297 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2298 snd_korg1212_free(korg1212);
2299 return -EBUSY;
2302 korg1212->irq = pci->irq;
2304 pci_set_master(korg1212->pci);
2306 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2307 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2308 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2309 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2310 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2311 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2312 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2313 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2314 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2315 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2317 #if K1212_DEBUG_LEVEL > 0
2318 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2319 " Status register = 0x%p\n"
2320 " OutDoorbell = 0x%p\n"
2321 " InDoorbell = 0x%p\n"
2322 " Mailbox0 = 0x%p\n"
2323 " Mailbox1 = 0x%p\n"
2324 " Mailbox2 = 0x%p\n"
2325 " Mailbox3 = 0x%p\n"
2326 " ControlReg = 0x%p\n"
2327 " SensReg = 0x%p\n"
2328 " IDReg = 0x%p\n"
2329 " [%s]\n",
2330 korg1212->statusRegPtr,
2331 korg1212->outDoorbellPtr,
2332 korg1212->inDoorbellPtr,
2333 korg1212->mailbox0Ptr,
2334 korg1212->mailbox1Ptr,
2335 korg1212->mailbox2Ptr,
2336 korg1212->mailbox3Ptr,
2337 korg1212->controlRegPtr,
2338 korg1212->sensRegPtr,
2339 korg1212->idRegPtr,
2340 stateName[korg1212->cardState]);
2341 #endif
2343 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2344 sizeof(KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2345 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(KorgSharedBuffer));
2346 snd_korg1212_free(korg1212);
2347 return -ENOMEM;
2349 korg1212->sharedBufferPtr = (KorgSharedBuffer *)korg1212->dma_shared.area;
2350 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2352 #if K1212_DEBUG_LEVEL > 0
2353 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(KorgSharedBuffer));
2354 #endif
2356 #ifndef K1212_LARGEALLOC
2358 korg1212->DataBufsSize = sizeof(KorgAudioBuffer) * kNumBuffers;
2360 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2361 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2362 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2363 snd_korg1212_free(korg1212);
2364 return -ENOMEM;
2366 korg1212->playDataBufsPtr = (KorgAudioBuffer *)korg1212->dma_play.area;
2367 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2369 #if K1212_DEBUG_LEVEL > 0
2370 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2371 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
2372 #endif
2374 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2375 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2376 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2377 snd_korg1212_free(korg1212);
2378 return -ENOMEM;
2380 korg1212->recordDataBufsPtr = (KorgAudioBuffer *)korg1212->dma_rec.area;
2381 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2383 #if K1212_DEBUG_LEVEL > 0
2384 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2385 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
2386 #endif
2388 #else // K1212_LARGEALLOC
2390 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2391 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
2392 korg1212->PlayDataPhy = (u32) &((KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2393 korg1212->RecDataPhy = (u32) &((KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
2395 #endif // K1212_LARGEALLOC
2397 korg1212->dspCodeSize = sizeof (dspCode);
2399 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
2400 offsetof(KorgSharedBuffer, volumeData);
2401 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
2402 offsetof(KorgSharedBuffer, routeData);
2403 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
2404 offsetof(KorgSharedBuffer, AdatTimeCode);
2406 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2407 korg1212->dspCodeSize, &korg1212->dma_dsp) < 0) {
2408 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", korg1212->dspCodeSize);
2409 snd_korg1212_free(korg1212);
2410 return -ENOMEM;
2413 #if K1212_DEBUG_LEVEL > 0
2414 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2415 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, korg1212->dspCodeSize,
2416 stateName[korg1212->cardState]);
2417 #endif
2419 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2421 #if K1212_DEBUG_LEVEL > 0
2422 if (rc) K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
2423 #endif
2425 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2426 snd_korg1212_free(korg1212);
2427 return err;
2430 snd_korg1212_EnableCardInterrupts(korg1212);
2432 mdelay(CARD_BOOT_DELAY_IN_MS);
2434 if (snd_korg1212_downloadDSPCode(korg1212))
2435 return -EBUSY;
2437 snd_printk(KERN_ERR
2438 "korg1212: dspMemPhy = %08x U[%08x], "
2439 "PlayDataPhy = %08x L[%08x]\n"
2440 "korg1212: RecDataPhy = %08x L[%08x], "
2441 "VolumeTablePhy = %08x L[%08x]\n"
2442 "korg1212: RoutingTablePhy = %08x L[%08x], "
2443 "AdatTimeCodePhy = %08x L[%08x]\n",
2444 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2445 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2446 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2447 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2448 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2449 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2451 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2452 return err;
2454 korg1212->pcm->private_data = korg1212;
2455 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2456 strcpy(korg1212->pcm->name, "korg1212");
2458 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2460 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2462 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2464 //snd_pcm_lib_preallocate_pages_for_all(korg1212->pcm,
2465 // K1212_MAX_BUF_SIZE, K1212_MAX_BUF_SIZE, GFP_KERNEL);
2467 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2468 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2469 if (err < 0)
2470 return err;
2473 snd_korg1212_proc_init(korg1212);
2475 snd_card_set_dev(card, &pci->dev);
2477 * rchip = korg1212;
2478 return 0;
2483 * Card initialisation
2486 static int __devinit
2487 snd_korg1212_probe(struct pci_dev *pci,
2488 const struct pci_device_id *pci_id)
2490 static int dev;
2491 korg1212_t *korg1212;
2492 snd_card_t *card;
2493 int err;
2495 if (dev >= SNDRV_CARDS) {
2496 return -ENODEV;
2498 if (!enable[dev]) {
2499 dev++;
2500 return -ENOENT;
2502 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2503 if (card == NULL)
2504 return -ENOMEM;
2506 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2507 snd_card_free(card);
2508 return err;
2511 strcpy(card->driver, "korg1212");
2512 strcpy(card->shortname, "korg1212");
2513 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2514 korg1212->iomem, korg1212->irq);
2516 #if K1212_DEBUG_LEVEL > 0
2517 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
2518 #endif
2520 if ((err = snd_card_register(card)) < 0) {
2521 snd_card_free(card);
2522 return err;
2524 pci_set_drvdata(pci, card);
2525 dev++;
2526 return 0;
2529 static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2531 snd_card_free(pci_get_drvdata(pci));
2532 pci_set_drvdata(pci, NULL);
2535 static struct pci_driver driver = {
2536 .name = "korg1212",
2537 .id_table = snd_korg1212_ids,
2538 .probe = snd_korg1212_probe,
2539 .remove = __devexit_p(snd_korg1212_remove),
2542 static int __init alsa_card_korg1212_init(void)
2544 return pci_module_init(&driver);
2547 static void __exit alsa_card_korg1212_exit(void)
2549 pci_unregister_driver(&driver);
2552 module_init(alsa_card_korg1212_init)
2553 module_exit(alsa_card_korg1212_exit)