blob: a110d664f62652d10d37782af38fb165c14115fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the Korg 1212 IO PCI card
3 *
4 * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/slab.h>
28#include <linux/wait.h>
29#include <linux/moduleparam.h>
30
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>
37
38#include <asm/io.h>
39
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)
46
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
52
53// ----------------------------------------------------------------------------
54// Valid states of the Korg 1212 I/O card.
55// ----------------------------------------------------------------------------
56typedef 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;
73
74// ----------------------------------------------------------------------------
75// The following enumeration defines the constants written to the card's
76// host-to-card doorbell to initiate a command.
77// ----------------------------------------------------------------------------
78typedef 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;
97
98
99// ----------------------------------------------------------------------------
100// The following enumeration defines return codes
101// to the Korg 1212 I/O driver.
102// ----------------------------------------------------------------------------
103typedef 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
118
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;
122
123// ----------------------------------------------------------------------------
124// The following enumeration defines the constants used to select the play
125// mode for the card in the SelectPlayMode command.
126// ----------------------------------------------------------------------------
127typedef 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;
133
134// ----------------------------------------------------------------------------
135// The following enumeration defines the constants used to select the monitor
136// mode for the card in the SetMonitorMode command.
137// ----------------------------------------------------------------------------
138typedef 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;
142
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
155
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
164
165#define CARD_BOOT_DELAY_IN_MS 10
166#define CARD_BOOT_TIMEOUT 10
167#define DSP_BOOT_DELAY_IN_MS 200
168
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
176
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)
192
193#define k1212MinADCSens 0x7f
194#define k1212MaxADCSens 0x00
195#define k1212MaxVolume 0x7fff
196#define k1212MaxWaveVolume 0xffff
197#define k1212MinVolume 0x0000
198#define k1212MaxVolInverted 0x8000
199
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
209
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
216
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
225
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
234
235#define SET_SENS_LEFTCHANID 0x00
236#define SET_SENS_RIGHTCHANID 0x01
237
238#define K1212SENSUPDATE_DELAY_IN_MS 50
239
240// --------------------------------------------------------------------------
241// WaitRTCTicks
242//
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.
256
257#include "korg1212-firmware.h"
258
259typedef struct _snd_korg1212 korg1212_t;
260
261typedef u16 K1212Sample; // channels 0-9 use 16 bit samples
262typedef u32 K1212SpdifSample; // channels 10-11 use 32 bits - only 20 are sent
263 // across S/PDIF.
264typedef u32 K1212TimeCodeSample; // holds the ADAT timecode value
265
266typedef 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;
275
276typedef 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;
281
282typedef struct KorgAudioFrame {
283 K1212Sample frameData16[k16BitChannels];
284 K1212SpdifSample frameData32[k32BitChannels];
285 K1212TimeCodeSample timeCodeVal;
286} KorgAudioFrame;
287
288typedef struct KorgAudioBuffer {
289 KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
290} KorgAudioBuffer;
291
292typedef 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;
302
303typedef 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;
319
320struct _snd_korg1212 {
321 snd_card_t *card;
322 struct pci_dev *pci;
323 snd_pcm_t *pcm;
324 int irq;
325
326 spinlock_t lock;
327 struct semaphore open_mutex;
328
329 struct timer_list timer; /* timer callback for checking ack of stop request */
330 int stop_pending_cnt; /* counter for stop pending check */
331
332 wait_queue_head_t wait;
333
334 unsigned long iomem;
335 unsigned long ioport;
336 unsigned long iomem2;
337 unsigned long irqcount;
338 unsigned long inIRQ;
339 void __iomem *iobase;
340
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;
345
346 u32 dspCodeSize;
347
348 u32 DataBufsSize;
349
350 KorgAudioBuffer * playDataBufsPtr;
351 KorgAudioBuffer * recordDataBufsPtr;
352
353 KorgSharedBuffer * sharedBufferPtr;
354
355 u32 RecDataPhy;
356 u32 PlayDataPhy;
357 unsigned long sharedBufferPhy;
358 u32 VolumeTablePhy;
359 u32 RoutingTablePhy;
360 u32 AdatTimeCodePhy;
361
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
372
373 size_t periodsize;
374 int channels;
375 int currentBuffer;
376
377 snd_pcm_substream_t *playback_substream;
378 snd_pcm_substream_t *capture_substream;
379
380 pid_t capture_pid;
381 pid_t playback_pid;
382
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.
387
388 ClockSourceIndex clkSrcRate; // sample rate and clock source
389
390 ClockSourceType clkSource; // clock source
391 int clkRate; // clock rate
392
393 int volumePhase[kAudioChannels];
394
395 u16 leftADCInSens; // ADC left channel input sensitivity
396 u16 rightADCInSens; // ADC right channel input sensitivity
397
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
403
404 int dsp_is_loaded;
405 int dsp_stop_is_processed;
406
407};
408
409MODULE_DESCRIPTION("korg1212");
410MODULE_LICENSE("GPL");
411MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
412
413static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
414static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
415static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
416
417module_param_array(index, int, NULL, 0444);
418MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
419module_param_array(id, charp, NULL, 0444);
420MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
421module_param_array(enable, bool, NULL, 0444);
422MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
423MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
424
425static struct pci_device_id snd_korg1212_ids[] = {
426 {
427 .vendor = 0x10b5,
428 .device = 0x906d,
429 .subvendor = PCI_ANY_ID,
430 .subdevice = PCI_ANY_ID,
431 },
432 { 0, },
433};
434
435static 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",
Clemens Ladisch315e3bd2005-10-04 08:42:10 +0200445 "Calibrating",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 "Invalid"
447};
448
449static char* clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
450
451static 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"
458};
459
460static 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",
473};
474
475static 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
482 };
483
484static snd_korg1212rc rc;
485
486MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
487
488typedef union swap_u32 { unsigned char c[4]; u32 i; } swap_u32;
489
490#ifdef SNDRV_BIG_ENDIAN
491static u32 LowerWordSwap(u32 swappee)
492#else
493static u32 UpperWordSwap(u32 swappee)
494#endif
495{
496 swap_u32 retVal, swapper;
497
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];
503
504 return retVal.i;
505}
506
507#ifdef SNDRV_BIG_ENDIAN
508static u32 UpperWordSwap(u32 swappee)
509#else
510static u32 LowerWordSwap(u32 swappee)
511#endif
512{
513 swap_u32 retVal, swapper;
514
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];
520
521 return retVal.i;
522}
523
524#if 0 /* not used */
525
526static u32 EndianSwap(u32 swappee)
527{
528 swap_u32 retVal, swapper;
529
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];
535
536 return retVal.i;
537}
538
539#endif /* not used */
540
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)
545
546static snd_korg1212rc snd_korg1212_Send1212Command(korg1212_t *korg1212, korg1212_dbcnst_t doorbellVal,
547 u32 mailBox0Val, u32 mailBox1Val, u32 mailBox2Val, u32 mailBox3Val)
548{
549 u32 retryCount;
550 u16 mailBox3Lo;
551 snd_korg1212rc rc = K1212_CMDRET_Success;
552
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;
558 }
559
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
569
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;
578 }
579
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;
594 }
595 }
596 }
597 korg1212->cmdRetryCount += retryCount;
598
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;
604 }
605
606 return rc;
607}
608
609/* spinlock already held */
610static void snd_korg1212_SendStop(korg1212_t *korg1212)
611{
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);
618 }
619}
620
621static void snd_korg1212_SendStopAndWait(korg1212_t *korg1212)
622{
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);
629}
630
631/* timer callback for checking the ack of stop request */
632static void snd_korg1212_timer_func(unsigned long data)
633{
634 korg1212_t *korg1212 = (korg1212_t *) data;
635
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
658 }
659 }
660 spin_unlock(&korg1212->lock);
661}
662
663static void snd_korg1212_TurnOnIdleMonitor(korg1212_t *korg1212)
664{
665 unsigned long flags;
666
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);
673}
674
675static void snd_korg1212_TurnOffIdleMonitor(korg1212_t *korg1212)
676{
677 if (korg1212->idleMonitorOn) {
678 snd_korg1212_SendStopAndWait(korg1212);
679 korg1212->idleMonitorOn = 0;
680 }
681}
682
683static inline void snd_korg1212_setCardState(korg1212_t * korg1212, CardState csState)
684{
685 korg1212->cardState = csState;
686}
687
688static int snd_korg1212_OpenCard(korg1212_t * korg1212)
689{
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);
697 }
698
699 up(&korg1212->open_mutex);
700 return 1;
701}
702
703static int snd_korg1212_CloseCard(korg1212_t * korg1212)
704{
705#if K1212_DEBUG_LEVEL > 0
706 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n", stateName[korg1212->cardState], korg1212->opencnt);
707#endif
708
709 down(&korg1212->open_mutex);
710 if (--(korg1212->opencnt)) {
711 up(&korg1212->open_mutex);
712 return 0;
713 }
714
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
721
722 if (rc != K1212_CMDRET_Success) {
723 up(&korg1212->open_mutex);
724 return 0;
725 }
726 } else if (korg1212->cardState > K1212_STATE_SETUP) {
727 snd_korg1212_SendStopAndWait(korg1212);
728 }
729
730 if (korg1212->cardState > K1212_STATE_READY) {
731 snd_korg1212_TurnOnIdleMonitor(korg1212);
732 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
733 }
734
735 up(&korg1212->open_mutex);
736 return 0;
737}
738
739/* spinlock already held */
740static int snd_korg1212_SetupForPlay(korg1212_t * korg1212)
741{
742#if K1212_DEBUG_LEVEL > 0
743 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n", stateName[korg1212->cardState], korg1212->setcnt);
744#endif
745
746 if (korg1212->setcnt++)
747 return 0;
748
749 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
750 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
751 K1212_MODE_SetupPlay, 0, 0, 0);
752
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;
758 }
759 return 0;
760}
761
762/* spinlock already held */
763static int snd_korg1212_TriggerPlay(korg1212_t * korg1212)
764{
765#if K1212_DEBUG_LEVEL > 0
766 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n", stateName[korg1212->cardState], korg1212->playcnt);
767#endif
768
769 if (korg1212->playcnt++)
770 return 0;
771
772 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
773 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
774
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
778
779 if (rc != K1212_CMDRET_Success) {
780 return 1;
781 }
782 return 0;
783}
784
785/* spinlock already held */
786static int snd_korg1212_StopPlay(korg1212_t * korg1212)
787{
788#if K1212_DEBUG_LEVEL > 0
789 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n", stateName[korg1212->cardState], korg1212->playcnt);
790#endif
791
792 if (--(korg1212->playcnt))
793 return 0;
794
795 korg1212->setcnt = 0;
796
797 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
798 snd_korg1212_SendStop(korg1212);
799
800 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
801 return 0;
802}
803
804static void snd_korg1212_EnableCardInterrupts(korg1212_t * korg1212)
805{
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);
812}
813
814#if 0 /* not used */
815
816static int snd_korg1212_SetMonitorMode(korg1212_t *korg1212, MonitorModeSelector mode)
817{
818#if K1212_DEBUG_LEVEL > 0
819 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n", stateName[korg1212->cardState]);
820#endif
821
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);
829 }
830 break;
831
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;
841 }
842 }
843 break;
844
845 default:
846 return 0;
847 }
848
849 return 1;
850}
851
852#endif /* not used */
853
854static inline int snd_korg1212_use_is_exclusive(korg1212_t *korg1212)
855{
856 int ret = 1;
857
858 if ((korg1212->playback_pid != korg1212->capture_pid) &&
859 (korg1212->playback_pid >= 0) && (korg1212->capture_pid >= 0)) {
860 ret = 0;
861 }
862 return ret;
863}
864
865static int snd_korg1212_SetRate(korg1212_t *korg1212, int rate)
866{
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;
875
876 if (!snd_korg1212_use_is_exclusive (korg1212)) {
877 return -EBUSY;
878 }
879
880 switch(rate) {
881 case 44100:
882 parm = s44[korg1212->clkSource];
883 break;
884
885 case 48000:
886 parm = s48[korg1212->clkSource];
887 break;
888
889 default:
890 return -EINVAL;
891 }
892
893 korg1212->clkSrcRate = parm;
894 korg1212->clkRate = rate;
895
896 udelay(INTERCOMMAND_DELAY);
897 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
898 ClockSourceSelector[korg1212->clkSrcRate],
899 0, 0, 0);
900
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
904
905 return 0;
906}
907
908static int snd_korg1212_SetClockSource(korg1212_t *korg1212, int source)
909{
910
911 if (source<0 || source >2)
912 return -EINVAL;
913
914 korg1212->clkSource = source;
915
916 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
917
918 return 0;
919}
920
921static void snd_korg1212_DisableCardInterrupts(korg1212_t *korg1212)
922{
923 writel(0, korg1212->statusRegPtr);
924}
925
926static int snd_korg1212_WriteADCSensitivity(korg1212_t *korg1212)
927{
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;
937
938#if K1212_DEBUG_LEVEL > 0
939 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n", stateName[korg1212->cardState]);
940#endif
941
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
948
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;
957
958 spin_lock_irqsave(&korg1212->lock, flags);
959
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);
966
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;
977
978 case K1212_CLKIDX_WordAt48K:
979 case K1212_CLKIDX_AdatAt48K:
980 case K1212_CLKIDX_LocalAt48K:
981 default:
982 clkIs48K = 1;
983 break;
984 }
985
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;
993
994 // ----------------------------------------------------------------------------
995 // now start shifting the bits in. Start with the left channel then the right.
996 // ----------------------------------------------------------------------------
997 for (channel = 0; channel < 2; channel++) {
998
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);
1007
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
1014 }
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
1020 }
1021 }
1022
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);
1029 }
1030
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);
1040
1041 if (clkIs48K)
1042 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1043
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);
1052 }
1053
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);
1060
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
1067
1068 }
1069
1070 spin_unlock_irqrestore(&korg1212->lock, flags);
1071
1072 return 1;
1073}
1074
1075static void snd_korg1212_OnDSPDownloadComplete(korg1212_t *korg1212)
1076{
1077 int channel;
1078
1079#if K1212_DEBUG_LEVEL > 0
1080 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n", stateName[korg1212->cardState]);
1081#endif
1082
1083 // ----------------------------------------------------
1084 // tell the card to boot
1085 // ----------------------------------------------------
1086 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1087
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);
1092
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
1102 0
1103 );
1104
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
1108
1109 udelay(INTERCOMMAND_DELAY);
1110
1111 rc = snd_korg1212_Send1212Command(korg1212,
1112 K1212_DB_ConfigureMiscMemory,
1113 LowerWordSwap(korg1212->VolumeTablePhy),
1114 LowerWordSwap(korg1212->RoutingTablePhy),
1115 LowerWordSwap(korg1212->AdatTimeCodePhy),
1116 0
1117 );
1118
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
1122
1123
1124 // --------------------------------------------------------------------------------
1125 // Initialize the routing and volume tables, then update the card's state.
1126 // --------------------------------------------------------------------------------
1127 udelay(INTERCOMMAND_DELAY);
1128
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);
1133 }
1134
1135 snd_korg1212_WriteADCSensitivity(korg1212);
1136
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
1144
1145 snd_korg1212_TurnOnIdleMonitor(korg1212);
1146 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1147
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
1151
1152 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1153}
1154
1155static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1156{
1157 u32 doorbellValue;
1158 korg1212_t *korg1212 = dev_id;
1159
1160 if(irq != korg1212->irq)
1161 return IRQ_NONE;
1162
1163 doorbellValue = readl(korg1212->inDoorbellPtr);
1164
1165 if (!doorbellValue)
1166 return IRQ_NONE;
1167
1168 spin_lock(&korg1212->lock);
1169
1170 writel(doorbellValue, korg1212->inDoorbellPtr);
1171
1172 korg1212->irqcount++;
1173
1174 korg1212->inIRQ++;
1175
1176
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);
1185 }
1186 break;
1187
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;
1201
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;
1212
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++;
1220
1221 if (korg1212->currentBuffer >= kNumBuffers)
1222 korg1212->currentBuffer = 0;
1223
1224 if (!korg1212->running)
1225 break;
1226
1227 if (korg1212->capture_substream) {
1228 spin_unlock(&korg1212->lock);
1229 snd_pcm_period_elapsed(korg1212->capture_substream);
1230 spin_lock(&korg1212->lock);
1231 }
1232
1233 if (korg1212->playback_substream) {
1234 spin_unlock(&korg1212->lock);
1235 snd_pcm_period_elapsed(korg1212->playback_substream);
1236 spin_lock(&korg1212->lock);
1237 }
1238 }
1239 break;
1240 }
1241
1242 korg1212->inIRQ--;
1243
1244 spin_unlock(&korg1212->lock);
1245
1246 return IRQ_HANDLED;
1247}
1248
1249static int snd_korg1212_downloadDSPCode(korg1212_t *korg1212)
1250{
1251
1252#if K1212_DEBUG_LEVEL > 0
1253 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n", stateName[korg1212->cardState]);
1254#endif
1255
1256 // ---------------------------------------------------------------
1257 // verify the state of the card before proceeding.
1258 // ---------------------------------------------------------------
1259 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS) {
1260 return 1;
1261 }
1262
1263 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1264
1265 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1266
1267 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1268 UpperWordSwap(korg1212->dma_dsp.addr),
1269 0, 0, 0);
1270
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
1274
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 */
1279
1280 snd_korg1212_OnDSPDownloadComplete(korg1212);
1281
1282 return 0;
1283}
1284
1285static snd_pcm_hardware_t snd_korg1212_playback_info =
1286{
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,
1303};
1304
1305static snd_pcm_hardware_t snd_korg1212_capture_info =
1306{
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,
1323};
1324
1325static int snd_korg1212_silence(korg1212_t *korg1212, int pos, int count, int offset, int size)
1326{
1327 KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1328 int i;
1329
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);
1334
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;
1341 }
1342#endif
1343 memset((void*) dst + offset, 0, size);
1344 dst++;
1345 }
1346
1347 return 0;
1348}
1349
1350static int snd_korg1212_copy_to(korg1212_t *korg1212, void __user *dst, int pos, int count, int offset, int size)
1351{
1352 KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1353 int i, rc;
1354
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);
1359
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;
1366 }
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;
1374 }
1375 src++;
1376 dst += size;
1377 }
1378
1379 return 0;
1380}
1381
1382static int snd_korg1212_copy_from(korg1212_t *korg1212, void __user *src, int pos, int count, int offset, int size)
1383{
1384 KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1385 int i, rc;
1386
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
1390
1391 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1392
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;
1399 }
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;
1407 }
1408 dst++;
1409 src += size;
1410 }
1411
1412 return 0;
1413}
1414
1415static void snd_korg1212_free_pcm(snd_pcm_t *pcm)
1416{
1417 korg1212_t *korg1212 = (korg1212_t *) pcm->private_data;
1418
1419#if K1212_DEBUG_LEVEL > 0
1420 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n", stateName[korg1212->cardState]);
1421#endif
1422
1423 korg1212->pcm = NULL;
1424}
1425
1426static int snd_korg1212_playback_open(snd_pcm_substream_t *substream)
1427{
1428 unsigned long flags;
1429 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1430 snd_pcm_runtime_t *runtime = substream->runtime;
1431
1432#if K1212_DEBUG_LEVEL > 0
1433 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n", stateName[korg1212->cardState]);
1434#endif
1435
1436 snd_pcm_set_sync(substream); // ???
1437
1438 snd_korg1212_OpenCard(korg1212);
1439
1440 runtime->hw = snd_korg1212_playback_info;
1441 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1442
1443 spin_lock_irqsave(&korg1212->lock, flags);
1444
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;
1450
1451 spin_unlock_irqrestore(&korg1212->lock, flags);
1452
1453 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1454 return 0;
1455}
1456
1457
1458static int snd_korg1212_capture_open(snd_pcm_substream_t *substream)
1459{
1460 unsigned long flags;
1461 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1462 snd_pcm_runtime_t *runtime = substream->runtime;
1463
1464#if K1212_DEBUG_LEVEL > 0
1465 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n", stateName[korg1212->cardState]);
1466#endif
1467
1468 snd_pcm_set_sync(substream);
1469
1470 snd_korg1212_OpenCard(korg1212);
1471
1472 runtime->hw = snd_korg1212_capture_info;
1473 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1474
1475 spin_lock_irqsave(&korg1212->lock, flags);
1476
1477 korg1212->capture_substream = substream;
1478 korg1212->capture_pid = current->pid;
1479 korg1212->periodsize = K1212_PERIODS;
1480 korg1212->channels = K1212_CHANNELS;
1481
1482 spin_unlock_irqrestore(&korg1212->lock, flags);
1483
1484 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1485 return 0;
1486}
1487
1488static int snd_korg1212_playback_close(snd_pcm_substream_t *substream)
1489{
1490 unsigned long flags;
1491 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1492
1493#if K1212_DEBUG_LEVEL > 0
1494 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n", stateName[korg1212->cardState]);
1495#endif
1496
1497 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1498
1499 spin_lock_irqsave(&korg1212->lock, flags);
1500
1501 korg1212->playback_pid = -1;
1502 korg1212->playback_substream = NULL;
1503 korg1212->periodsize = 0;
1504
1505 spin_unlock_irqrestore(&korg1212->lock, flags);
1506
1507 snd_korg1212_CloseCard(korg1212);
1508 return 0;
1509}
1510
1511static int snd_korg1212_capture_close(snd_pcm_substream_t *substream)
1512{
1513 unsigned long flags;
1514 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1515
1516#if K1212_DEBUG_LEVEL > 0
1517 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n", stateName[korg1212->cardState]);
1518#endif
1519
1520 spin_lock_irqsave(&korg1212->lock, flags);
1521
1522 korg1212->capture_pid = -1;
1523 korg1212->capture_substream = NULL;
1524 korg1212->periodsize = 0;
1525
1526 spin_unlock_irqrestore(&korg1212->lock, flags);
1527
1528 snd_korg1212_CloseCard(korg1212);
1529 return 0;
1530}
1531
1532static int snd_korg1212_ioctl(snd_pcm_substream_t *substream,
1533 unsigned int cmd, void *arg)
1534{
1535#if K1212_DEBUG_LEVEL > 0
1536 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1537#endif
1538
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;
1548 }
1549
1550 return snd_pcm_lib_ioctl(substream, cmd, arg);
1551}
1552
1553static int snd_korg1212_hw_params(snd_pcm_substream_t *substream,
1554 snd_pcm_hw_params_t *params)
1555{
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;
1561
1562#if K1212_DEBUG_LEVEL > 0
1563 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n", stateName[korg1212->cardState]);
1564#endif
1565
1566 spin_lock_irqsave(&korg1212->lock, flags);
1567
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;
1574 }
1575
1576 if ((other_pid > 0) && (this_pid != other_pid)) {
1577
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.
1581 */
1582
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;
1587 }
1588
1589 spin_unlock_irqrestore(&korg1212->lock, flags);
1590 return 0;
1591 }
1592
1593 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1594 spin_unlock_irqrestore(&korg1212->lock, flags);
1595 return err;
1596 }
1597
1598 korg1212->channels = params_channels(params);
1599 korg1212->periodsize = K1212_PERIOD_BYTES;
1600
1601 spin_unlock_irqrestore(&korg1212->lock, flags);
1602
1603 return 0;
1604}
1605
1606static int snd_korg1212_prepare(snd_pcm_substream_t *substream)
1607{
1608 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1609 int rc;
1610
1611#if K1212_DEBUG_LEVEL > 0
1612 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n", stateName[korg1212->cardState]);
1613#endif
1614
1615 spin_lock_irq(&korg1212->lock);
1616
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;
1624 /*
1625 korg1212->sharedBufferPtr->cardCommand = 0;
1626 del_timer(&korg1212->timer);
1627 korg1212->stop_pending_cnt = 0;
1628 */
1629 }
1630
1631 rc = snd_korg1212_SetupForPlay(korg1212);
1632
1633 korg1212->currentBuffer = 0;
1634
1635 spin_unlock_irq(&korg1212->lock);
1636
1637 return rc ? -EINVAL : 0;
1638}
1639
1640static int snd_korg1212_trigger(snd_pcm_substream_t *substream,
1641 int cmd)
1642{
1643 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1644 int rc;
1645
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
1649
1650 spin_lock(&korg1212->lock);
1651 switch (cmd) {
1652 case SNDRV_PCM_TRIGGER_START:
1653/*
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;
1659 }
1660*/
1661 korg1212->running++;
1662 rc = snd_korg1212_TriggerPlay(korg1212);
1663 break;
1664
1665 case SNDRV_PCM_TRIGGER_STOP:
1666/*
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;
1672 }
1673*/
1674 korg1212->running--;
1675 rc = snd_korg1212_StopPlay(korg1212);
1676 break;
1677
1678 default:
1679 rc = 1;
1680 break;
1681 }
1682 spin_unlock(&korg1212->lock);
1683 return rc ? -EINVAL : 0;
1684}
1685
1686static snd_pcm_uframes_t snd_korg1212_playback_pointer(snd_pcm_substream_t *substream)
1687{
1688 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1689 snd_pcm_uframes_t pos;
1690
1691 pos = korg1212->currentBuffer * kPlayBufferFrames;
1692
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
1697
1698 return pos;
1699}
1700
1701static snd_pcm_uframes_t snd_korg1212_capture_pointer(snd_pcm_substream_t *substream)
1702{
1703 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1704 snd_pcm_uframes_t pos;
1705
1706 pos = korg1212->currentBuffer * kPlayBufferFrames;
1707
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
1712
1713 return pos;
1714}
1715
1716static 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)
1721{
1722 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1723
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
1727
1728 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1729
1730}
1731
1732static 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)
1736{
1737 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1738
1739#if K1212_DEBUG_LEVEL > 0
1740 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n", stateName[korg1212->cardState]);
1741#endif
1742
1743 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1744}
1745
1746static 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)
1751{
1752 korg1212_t *korg1212 = snd_pcm_substream_chip(substream);
1753
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
1757
1758 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1759}
1760
1761static 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,
1771};
1772
1773static 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,
1782};
1783
1784/*
1785 * Control Interface
1786 */
1787
1788static int snd_korg1212_control_phase_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1789{
1790 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1791 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1792 return 0;
1793}
1794
1795static int snd_korg1212_control_phase_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1796{
1797 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1798 int i = kcontrol->private_value;
1799
1800 spin_lock_irq(&korg1212->lock);
1801
1802 u->value.integer.value[0] = korg1212->volumePhase[i];
1803
1804 if (i >= 8)
1805 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1806
1807 spin_unlock_irq(&korg1212->lock);
1808
1809 return 0;
1810}
1811
1812static int snd_korg1212_control_phase_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1813{
1814 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1815 int change = 0;
1816 int i, val;
1817
1818 spin_lock_irq(&korg1212->lock);
1819
1820 i = kcontrol->private_value;
1821
1822 korg1212->volumePhase[i] = u->value.integer.value[0];
1823
1824 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1825
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;
1830 }
1831
1832 if (i >= 8) {
1833 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1834
1835 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1836
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;
1841 }
1842 }
1843
1844 spin_unlock_irq(&korg1212->lock);
1845
1846 return change;
1847}
1848
1849static int snd_korg1212_control_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1850{
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;
1856}
1857
1858static int snd_korg1212_control_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1859{
1860 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1861 int i;
1862
1863 spin_lock_irq(&korg1212->lock);
1864
1865 i = kcontrol->private_value;
1866 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1867
1868 if (i >= 8)
1869 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1870
1871 spin_unlock_irq(&korg1212->lock);
1872
1873 return 0;
1874}
1875
1876static int snd_korg1212_control_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1877{
1878 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1879 int change = 0;
1880 int i;
1881 int val;
1882
1883 spin_lock_irq(&korg1212->lock);
1884
1885 i = kcontrol->private_value;
1886
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;
1892 }
1893
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;
1900 }
1901 }
1902
1903 spin_unlock_irq(&korg1212->lock);
1904
1905 return change;
1906}
1907
1908static int snd_korg1212_control_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1909{
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;
1915 }
1916 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1917 return 0;
1918}
1919
1920static int snd_korg1212_control_route_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1921{
1922 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1923 int i;
1924
1925 spin_lock_irq(&korg1212->lock);
1926
1927 i = kcontrol->private_value;
1928 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1929
1930 if (i >= 8)
1931 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1932
1933 spin_unlock_irq(&korg1212->lock);
1934
1935 return 0;
1936}
1937
1938static int snd_korg1212_control_route_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1939{
1940 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1941 int change = 0, i;
1942
1943 spin_lock_irq(&korg1212->lock);
1944
1945 i = kcontrol->private_value;
1946
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;
1950 }
1951
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;
1956 }
1957 }
1958
1959 spin_unlock_irq(&korg1212->lock);
1960
1961 return change;
1962}
1963
1964static int snd_korg1212_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1965{
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;
1971}
1972
1973static int snd_korg1212_control_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1974{
1975 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1976
1977 spin_lock_irq(&korg1212->lock);
1978
1979 u->value.integer.value[0] = korg1212->leftADCInSens;
1980 u->value.integer.value[1] = korg1212->rightADCInSens;
1981
1982 spin_unlock_irq(&korg1212->lock);
1983
1984 return 0;
1985}
1986
1987static int snd_korg1212_control_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *u)
1988{
1989 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
1990 int change = 0;
1991
1992 spin_lock_irq(&korg1212->lock);
1993
1994 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1995 korg1212->leftADCInSens = u->value.integer.value[0];
1996 change = 1;
1997 }
1998 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1999 korg1212->rightADCInSens = u->value.integer.value[1];
2000 change = 1;
2001 }
2002
2003 spin_unlock_irq(&korg1212->lock);
2004
2005 if (change)
2006 snd_korg1212_WriteADCSensitivity(korg1212);
2007
2008 return change;
2009}
2010
2011static int snd_korg1212_control_sync_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2012{
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;
2018 }
2019 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
2020 return 0;
2021}
2022
2023static int snd_korg1212_control_sync_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2024{
2025 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
2026
2027 spin_lock_irq(&korg1212->lock);
2028
2029 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
2030
2031 spin_unlock_irq(&korg1212->lock);
2032 return 0;
2033}
2034
2035static int snd_korg1212_control_sync_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2036{
2037 korg1212_t *korg1212 = snd_kcontrol_chip(kcontrol);
2038 unsigned int val;
2039 int change;
2040
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;
2047}
2048
2049#define MON_MIXER(ord,c_name) \
2050 { \
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 }, \
2059 { \
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 }, \
2068 { \
2069 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002070 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 .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, \
2076 }
2077
2078static 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"),
2083 {
2084 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002085 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 .name = "Sync Source",
2087 .info = snd_korg1212_control_sync_info,
2088 .get = snd_korg1212_control_sync_get,
2089 .put = snd_korg1212_control_sync_put,
2090 },
2091 {
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,
2098 }
2099};
2100
2101/*
2102 * proc interface
2103 */
2104
2105static void snd_korg1212_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
2106{
2107 int n;
2108 korg1212_t *korg1212 = (korg1212_t *)entry->private_data;
2109
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);
2130}
2131
2132static void __devinit snd_korg1212_proc_init(korg1212_t *korg1212)
2133{
2134 snd_info_entry_t *entry;
2135
2136 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2137 snd_info_set_text_ops(entry, korg1212, 1024, snd_korg1212_proc_read);
2138}
2139
2140static int
2141snd_korg1212_free(korg1212_t *korg1212)
2142{
2143 snd_korg1212_TurnOffIdleMonitor(korg1212);
2144
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;
2150 }
2151
2152 if (korg1212->iobase != NULL) {
2153 iounmap(korg1212->iobase);
2154 korg1212->iobase = NULL;
2155 }
2156
2157 pci_release_regions(korg1212->pci);
2158
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;
2165 }
2166
2167#ifndef K1212_LARGEALLOC
2168
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;
2175 }
2176
2177 if (korg1212->dma_rec.area) {
2178 snd_dma_free_pages(&korg1212->dma_rec);
2179 korg1212->dma_rec.area = NULL;
2180 }
2181
2182#endif
2183
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;
2190 }
2191
2192 pci_disable_device(korg1212->pci);
2193 kfree(korg1212);
2194 return 0;
2195}
2196
2197static int snd_korg1212_dev_free(snd_device_t *device)
2198{
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);
2204}
2205
2206static int __devinit snd_korg1212_create(snd_card_t * card, struct pci_dev *pci,
2207 korg1212_t ** rchip)
2208
2209{
2210 int err;
2211 unsigned int i;
2212 unsigned ioport_size, iomem_size, iomem2_size;
2213 korg1212_t * korg1212;
2214
2215 static snd_device_ops_t ops = {
2216 .dev_free = snd_korg1212_dev_free,
2217 };
2218
2219 * rchip = NULL;
2220 if ((err = pci_enable_device(pci)) < 0)
2221 return err;
2222
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002223 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (korg1212 == NULL) {
2225 pci_disable_device(pci);
2226 return -ENOMEM;
2227 }
2228
2229 korg1212->card = card;
2230 korg1212->pci = pci;
2231
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;
2238
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;
2255
2256 for (i=0; i<kAudioChannels; i++)
2257 korg1212->volumePhase[i] = 0;
2258
2259 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2260 kfree(korg1212);
2261 pci_disable_device(pci);
2262 return err;
2263 }
2264
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);
2268
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);
2272
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
2284
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;
2290 }
2291
2292 err = request_irq(pci->irq, snd_korg1212_interrupt,
2293 SA_INTERRUPT|SA_SHIRQ,
2294 "korg1212", (void *) korg1212);
2295
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;
2300 }
2301
2302 korg1212->irq = pci->irq;
2303
2304 pci_set_master(korg1212->pci);
2305
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);
2316
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
2342
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;
2348 }
2349 korg1212->sharedBufferPtr = (KorgSharedBuffer *)korg1212->dma_shared.area;
2350 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2351
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
2355
2356#ifndef K1212_LARGEALLOC
2357
2358 korg1212->DataBufsSize = sizeof(KorgAudioBuffer) * kNumBuffers;
2359
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;
2365 }
2366 korg1212->playDataBufsPtr = (KorgAudioBuffer *)korg1212->dma_play.area;
2367 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2368
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
2373
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;
2379 }
2380 korg1212->recordDataBufsPtr = (KorgAudioBuffer *)korg1212->dma_rec.area;
2381 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2382
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
2387
2388#else // K1212_LARGEALLOC
2389
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;
2394
2395#endif // K1212_LARGEALLOC
2396
2397 korg1212->dspCodeSize = sizeof (dspCode);
2398
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);
2405
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;
2411 }
2412
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
2418
2419 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2420
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
2424
2425 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2426 snd_korg1212_free(korg1212);
2427 return err;
2428 }
2429
2430 snd_korg1212_EnableCardInterrupts(korg1212);
2431
2432 mdelay(CARD_BOOT_DELAY_IN_MS);
2433
2434 if (snd_korg1212_downloadDSPCode(korg1212))
2435 return -EBUSY;
2436
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));
2450
2451 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2452 return err;
2453
2454 korg1212->pcm->private_data = korg1212;
2455 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2456 strcpy(korg1212->pcm->name, "korg1212");
2457
2458 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2459
2460 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2461
2462 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2463
2464 //snd_pcm_lib_preallocate_pages_for_all(korg1212->pcm,
2465 // K1212_MAX_BUF_SIZE, K1212_MAX_BUF_SIZE, GFP_KERNEL);
2466
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;
2471 }
2472
2473 snd_korg1212_proc_init(korg1212);
2474
2475 snd_card_set_dev(card, &pci->dev);
2476
2477 * rchip = korg1212;
2478 return 0;
2479
2480}
2481
2482/*
2483 * Card initialisation
2484 */
2485
2486static int __devinit
2487snd_korg1212_probe(struct pci_dev *pci,
2488 const struct pci_device_id *pci_id)
2489{
2490 static int dev;
2491 korg1212_t *korg1212;
2492 snd_card_t *card;
2493 int err;
2494
2495 if (dev >= SNDRV_CARDS) {
2496 return -ENODEV;
2497 }
2498 if (!enable[dev]) {
2499 dev++;
2500 return -ENOENT;
2501 }
2502 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2503 if (card == NULL)
2504 return -ENOMEM;
2505
2506 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2507 snd_card_free(card);
2508 return err;
2509 }
2510
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);
2515
2516#if K1212_DEBUG_LEVEL > 0
2517 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
2518#endif
2519
2520 if ((err = snd_card_register(card)) < 0) {
2521 snd_card_free(card);
2522 return err;
2523 }
2524 pci_set_drvdata(pci, card);
2525 dev++;
2526 return 0;
2527}
2528
2529static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2530{
2531 snd_card_free(pci_get_drvdata(pci));
2532 pci_set_drvdata(pci, NULL);
2533}
2534
2535static 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),
2540};
2541
2542static int __init alsa_card_korg1212_init(void)
2543{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002544 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
2547static void __exit alsa_card_korg1212_exit(void)
2548{
2549 pci_unregister_driver(&driver);
2550}
2551
2552module_init(alsa_card_korg1212_init)
2553module_exit(alsa_card_korg1212_exit)