blob: 345eefeedb394628c9f8ffb224fc54ce278ebb76 [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>
Ingo Molnar62932df2006-01-16 16:34:20 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <sound/core.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/initval.h>
38
39#include <asm/io.h>
40
41// ----------------------------------------------------------------------------
42// Debug Stuff
43// ----------------------------------------------------------------------------
44#define K1212_DEBUG_LEVEL 0
Takashi Iwai9fd91562005-11-17 10:45:48 +010045#if K1212_DEBUG_LEVEL > 0
46#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
47#else
48#define K1212_DEBUG_PRINTK(fmt,...)
49#endif
50#if K1212_DEBUG_LEVEL > 1
51#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
52#else
53#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56// ----------------------------------------------------------------------------
57// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
58// buffers are alocated as a large piece inside KorgSharedBuffer.
59// ----------------------------------------------------------------------------
60//#define K1212_LARGEALLOC 1
61
62// ----------------------------------------------------------------------------
63// Valid states of the Korg 1212 I/O card.
64// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010065enum CardState {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 K1212_STATE_NONEXISTENT, // there is no card here
67 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
68 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
69 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
70 K1212_STATE_READY, // the card can be opened by an application. Any application
71 // requests prior to this state should fail. Only an open
72 // request can be made at this state.
73 K1212_STATE_OPEN, // an application has opened the card
74 K1212_STATE_SETUP, // the card has been setup for play
75 K1212_STATE_PLAYING, // the card is playing
76 K1212_STATE_MONITOR, // the card is in the monitor mode
77 K1212_STATE_CALIBRATING, // the card is currently calibrating
78 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
79 // are in the process of cleaning things up.
80 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
Takashi Iwaifcfd3332005-11-17 15:00:46 +010081};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83// ----------------------------------------------------------------------------
84// The following enumeration defines the constants written to the card's
85// host-to-card doorbell to initiate a command.
86// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010087enum korg1212_dbcnst {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
89 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
90 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
91 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
92 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
93 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
94 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
95 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
96 // timecode value.
97 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
98 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
99 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
100 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
101 // on page 4 (local page to card).
102 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
103 // completed.
104 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107
108// ----------------------------------------------------------------------------
109// The following enumeration defines return codes
110// to the Korg 1212 I/O driver.
111// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100112enum snd_korg1212rc {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 K1212_CMDRET_Success = 0, // command was successfully placed
114 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
115 K1212_CMDRET_PMFailure, // the protected mode call failed
116 K1212_CMDRET_FailUnspecified, // unspecified failure
117 K1212_CMDRET_FailBadState, // the specified command can not be given in
118 // the card's current state. (or the wave device's
119 // state)
120 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
121 K1212_CMDRET_BadIndex, // an out of range card index was specified
122 K1212_CMDRET_BadHandle, // an invalid card handle was specified
123 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
124 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
125 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
126 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
127
128 K1212_CMDRET_BadDevice, // the specified wave device was out of range
129 K1212_CMDRET_BadFormat // the specified wave format is unsupported
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132// ----------------------------------------------------------------------------
133// The following enumeration defines the constants used to select the play
134// mode for the card in the SelectPlayMode command.
135// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100136enum PlayModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
138 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
139 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
140 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143// ----------------------------------------------------------------------------
144// The following enumeration defines the constants used to select the monitor
145// mode for the card in the SetMonitorMode command.
146// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100147enum MonitorModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
149 K1212_MONMODE_On // tells card to turn on monitor mode
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100150};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
153#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
154#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
155#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
156#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
157#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
158#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
159#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
160 // register
161#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
162 // this is the upper word of the PCI control reg.
163#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
164
165#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
166 // from the card after sending a command.
167#define INTERCOMMAND_DELAY 40
168#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
169 // to send a command before giving up.
170#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
171 // the card.
172#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
173
174#define CARD_BOOT_DELAY_IN_MS 10
175#define CARD_BOOT_TIMEOUT 10
176#define DSP_BOOT_DELAY_IN_MS 200
177
178#define kNumBuffers 8
179#define k1212MaxCards 4
180#define k1212NumWaveDevices 6
181#define k16BitChannels 10
182#define k32BitChannels 2
183#define kAudioChannels (k16BitChannels + k32BitChannels)
184#define kPlayBufferFrames 1024
185
186#define K1212_ANALOG_CHANNELS 2
187#define K1212_SPDIF_CHANNELS 2
188#define K1212_ADAT_CHANNELS 8
189#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
190#define K1212_MIN_CHANNELS 1
191#define K1212_MAX_CHANNELS K1212_CHANNELS
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100192#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
194#define K1212_PERIODS (kNumBuffers)
195#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
196#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
197#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
198#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
199#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
200#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
201
202#define k1212MinADCSens 0x7f
203#define k1212MaxADCSens 0x00
204#define k1212MaxVolume 0x7fff
205#define k1212MaxWaveVolume 0xffff
206#define k1212MinVolume 0x0000
207#define k1212MaxVolInverted 0x8000
208
209// -----------------------------------------------------------------
210// the following bits are used for controlling interrupts in the
211// interrupt control/status reg
212// -----------------------------------------------------------------
213#define PCI_INT_ENABLE_BIT 0x00000100
214#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
215#define LOCAL_INT_ENABLE_BIT 0x00010000
216#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
217#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
218
219// -----------------------------------------------------------------
220// the following bits are defined for the PCI command register
221// -----------------------------------------------------------------
222#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
223#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
224#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
225
226// -----------------------------------------------------------------
227// the following bits are defined for the PCI status register
228// -----------------------------------------------------------------
229#define PCI_STAT_PARITY_ERROR_BIT 0x8000
230#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
231#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
232#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
233#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
234
235// ------------------------------------------------------------------------
236// the following constants are used in setting the 1212 I/O card's input
237// sensitivity.
238// ------------------------------------------------------------------------
239#define SET_SENS_LOCALINIT_BITPOS 15
240#define SET_SENS_DATA_BITPOS 10
241#define SET_SENS_CLOCK_BITPOS 8
242#define SET_SENS_LOADSHIFT_BITPOS 0
243
244#define SET_SENS_LEFTCHANID 0x00
245#define SET_SENS_RIGHTCHANID 0x01
246
247#define K1212SENSUPDATE_DELAY_IN_MS 50
248
249// --------------------------------------------------------------------------
250// WaitRTCTicks
251//
252// This function waits the specified number of real time clock ticks.
253// According to the DDK, each tick is ~0.8 microseconds.
254// The defines following the function declaration can be used for the
255// numTicksToWait parameter.
256// --------------------------------------------------------------------------
257#define ONE_RTC_TICK 1
258#define SENSCLKPULSE_WIDTH 4
259#define LOADSHIFT_DELAY 4
260#define INTERCOMMAND_DELAY 40
261#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
262 // the command register. (could be up to 180 us)
263#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
264 // from the card after sending a command.
265
266#include "korg1212-firmware.h"
267
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100268enum ClockSourceIndex {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
270 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
271 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
272 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
273 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
274 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
275 K1212_CLKIDX_Invalid // used to check validity of the index
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100278enum ClockSourceType {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 K1212_CLKIDX_Adat = 0, // selects source as ADAT
280 K1212_CLKIDX_Word, // selects source as S/PDIF
281 K1212_CLKIDX_Local // selects source as local clock
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100284struct KorgAudioFrame {
285 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
286 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
287 u32 timeCodeVal; /* holds the ADAT timecode value */
288};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100290struct KorgAudioBuffer {
291 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
292};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100294struct KorgSharedBuffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifdef K1212_LARGEALLOC
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100296 struct KorgAudioBuffer playDataBufs[kNumBuffers];
297 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
299 short volumeData[kAudioChannels];
300 u32 cardCommand;
301 u16 routeData [kAudioChannels];
302 u32 AdatTimeCode; // ADAT timecode value
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100303};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100305struct SensBits {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 union {
307 struct {
308 unsigned int leftChanVal:8;
309 unsigned int leftChanId:8;
310 } v;
311 u16 leftSensBits;
312 } l;
313 union {
314 struct {
315 unsigned int rightChanVal:8;
316 unsigned int rightChanId:8;
317 } v;
318 u16 rightSensBits;
319 } r;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100320};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100322struct snd_korg1212 {
323 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct pci_dev *pci;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100325 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int irq;
327
328 spinlock_t lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100329 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 struct timer_list timer; /* timer callback for checking ack of stop request */
332 int stop_pending_cnt; /* counter for stop pending check */
333
334 wait_queue_head_t wait;
335
336 unsigned long iomem;
337 unsigned long ioport;
338 unsigned long iomem2;
339 unsigned long irqcount;
340 unsigned long inIRQ;
341 void __iomem *iobase;
342
343 struct snd_dma_buffer dma_dsp;
344 struct snd_dma_buffer dma_play;
345 struct snd_dma_buffer dma_rec;
346 struct snd_dma_buffer dma_shared;
347
348 u32 dspCodeSize;
349
350 u32 DataBufsSize;
351
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100352 struct KorgAudioBuffer * playDataBufsPtr;
353 struct KorgAudioBuffer * recordDataBufsPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100355 struct KorgSharedBuffer * sharedBufferPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 u32 RecDataPhy;
358 u32 PlayDataPhy;
359 unsigned long sharedBufferPhy;
360 u32 VolumeTablePhy;
361 u32 RoutingTablePhy;
362 u32 AdatTimeCodePhy;
363
364 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
365 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
366 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
367 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
368 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
369 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
370 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
371 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
372 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
373 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
374
375 size_t periodsize;
376 int channels;
377 int currentBuffer;
378
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100379 struct snd_pcm_substream *playback_substream;
380 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 pid_t capture_pid;
383 pid_t playback_pid;
384
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100385 enum CardState cardState;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int running;
387 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
388 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
389
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100390 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100392 enum ClockSourceType clkSource; // clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int clkRate; // clock rate
394
395 int volumePhase[kAudioChannels];
396
397 u16 leftADCInSens; // ADC left channel input sensitivity
398 u16 rightADCInSens; // ADC right channel input sensitivity
399
400 int opencnt; // Open/Close count
401 int setcnt; // SetupForPlay count
402 int playcnt; // TriggerPlay count
403 int errorcnt; // Error Count
404 unsigned long totalerrorcnt; // Total Error Count
405
406 int dsp_is_loaded;
407 int dsp_stop_is_processed;
408
409};
410
411MODULE_DESCRIPTION("korg1212");
412MODULE_LICENSE("GPL");
413MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
414
415static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
416static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
417static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
418
419module_param_array(index, int, NULL, 0444);
420MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
421module_param_array(id, charp, NULL, 0444);
422MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
423module_param_array(enable, bool, NULL, 0444);
424MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
425MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
426
Takashi Iwaif40b6892006-07-05 16:51:05 +0200427static struct pci_device_id snd_korg1212_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 {
429 .vendor = 0x10b5,
430 .device = 0x906d,
431 .subvendor = PCI_ANY_ID,
432 .subdevice = PCI_ANY_ID,
433 },
434 { 0, },
435};
436
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100437MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
438
Takashi Iwai9fd91562005-11-17 10:45:48 +0100439static char *stateName[] = {
440 "Non-existent",
441 "Uninitialized",
442 "DSP download in process",
443 "DSP download complete",
444 "Ready",
445 "Open",
446 "Setup for play",
447 "Playing",
448 "Monitor mode on",
449 "Calibrating",
450 "Invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
Takashi Iwai9fd91562005-11-17 10:45:48 +0100453static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Takashi Iwai9fd91562005-11-17 10:45:48 +0100455static char *clockSourceName[] = {
456 "ADAT at 44.1 kHz",
457 "ADAT at 48 kHz",
458 "S/PDIF at 44.1 kHz",
459 "S/PDIF at 48 kHz",
460 "local clock at 44.1 kHz",
461 "local clock at 48 kHz"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Takashi Iwai9fd91562005-11-17 10:45:48 +0100464static char *channelName[] = {
465 "ADAT-1",
466 "ADAT-2",
467 "ADAT-3",
468 "ADAT-4",
469 "ADAT-5",
470 "ADAT-6",
471 "ADAT-7",
472 "ADAT-8",
473 "Analog-L",
474 "Analog-R",
475 "SPDIF-L",
476 "SPDIF-R",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477};
478
Takashi Iwai9fd91562005-11-17 10:45:48 +0100479static u16 ClockSourceSelector[] = {
480 0x8000, // selects source as ADAT at 44.1 kHz
481 0x0000, // selects source as ADAT at 48 kHz
482 0x8001, // selects source as S/PDIF at 44.1 kHz
483 0x0001, // selects source as S/PDIF at 48 kHz
484 0x8002, // selects source as local clock at 44.1 kHz
485 0x0002 // selects source as local clock at 48 kHz
486};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100488union swap_u32 { unsigned char c[4]; u32 i; };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490#ifdef SNDRV_BIG_ENDIAN
491static u32 LowerWordSwap(u32 swappee)
492#else
493static u32 UpperWordSwap(u32 swappee)
494#endif
495{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100496 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
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{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100513 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
525#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
526#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
527#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
528
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100529static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
530 enum korg1212_dbcnst doorbellVal,
531 u32 mailBox0Val, u32 mailBox1Val,
532 u32 mailBox2Val, u32 mailBox3Val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 u32 retryCount;
535 u16 mailBox3Lo;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100536 int rc = K1212_CMDRET_Success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (!korg1212->outDoorbellPtr) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100539 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return K1212_CMDRET_CardUninitialized;
541 }
542
Takashi Iwai9fd91562005-11-17 10:45:48 +0100543 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
544 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
546 writel(mailBox3Val, korg1212->mailbox3Ptr);
547 writel(mailBox2Val, korg1212->mailbox2Ptr);
548 writel(mailBox1Val, korg1212->mailbox1Ptr);
549 writel(mailBox0Val, korg1212->mailbox0Ptr);
550 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
551
552 // --------------------------------------------------------------
553 // the reboot command will not give an acknowledgement.
554 // --------------------------------------------------------------
555 if ( doorbellVal == K1212_DB_RebootCard ||
556 doorbellVal == K1212_DB_BootFromDSPPage4 ||
557 doorbellVal == K1212_DB_StartDSPDownload ) {
558 rc = K1212_CMDRET_Success;
559 break;
560 }
561
562 // --------------------------------------------------------------
563 // See if the card acknowledged the command. Wait a bit, then
564 // read in the low word of mailbox3. If the MSB is set and the
565 // low byte is equal to the doorbell value, then it ack'd.
566 // --------------------------------------------------------------
567 udelay(COMMAND_ACK_DELAY);
568 mailBox3Lo = readl(korg1212->mailbox3Ptr);
569 if (mailBox3Lo & COMMAND_ACK_MASK) {
570 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100571 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rc = K1212_CMDRET_Success;
573 break;
574 }
575 }
576 }
577 korg1212->cmdRetryCount += retryCount;
578
579 if (retryCount >= MAX_COMMAND_RETRIES) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100580 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 rc = K1212_CMDRET_NoAckFromCard;
582 }
583
584 return rc;
585}
586
587/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100588static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 if (! korg1212->stop_pending_cnt) {
591 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
592 /* program the timer */
593 korg1212->stop_pending_cnt = HZ;
594 korg1212->timer.expires = jiffies + 1;
595 add_timer(&korg1212->timer);
596 }
597}
598
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100599static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 unsigned long flags;
602 spin_lock_irqsave(&korg1212->lock, flags);
603 korg1212->dsp_stop_is_processed = 0;
604 snd_korg1212_SendStop(korg1212);
605 spin_unlock_irqrestore(&korg1212->lock, flags);
606 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
607}
608
609/* timer callback for checking the ack of stop request */
610static void snd_korg1212_timer_func(unsigned long data)
611{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100612 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100613 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Takashi Iwaib32425a2005-11-18 18:52:14 +0100615 spin_lock_irqsave(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (korg1212->sharedBufferPtr->cardCommand == 0) {
617 /* ack'ed */
618 korg1212->stop_pending_cnt = 0;
619 korg1212->dsp_stop_is_processed = 1;
620 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100621 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
622 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else {
624 if (--korg1212->stop_pending_cnt > 0) {
625 /* reprogram timer */
626 korg1212->timer.expires = jiffies + 1;
627 add_timer(&korg1212->timer);
628 } else {
629 snd_printd("korg1212_timer_func timeout\n");
630 korg1212->sharedBufferPtr->cardCommand = 0;
631 korg1212->dsp_stop_is_processed = 1;
632 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100633 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
634 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 }
Takashi Iwaib32425a2005-11-18 18:52:14 +0100637 spin_unlock_irqrestore(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100640static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 unsigned long flags;
Takashi Iwai9fd91562005-11-17 10:45:48 +0100643 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 udelay(INTERCOMMAND_DELAY);
646 spin_lock_irqsave(&korg1212->lock, flags);
647 korg1212->idleMonitorOn = 1;
648 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
649 K1212_MODE_MonitorOn, 0, 0, 0);
650 spin_unlock_irqrestore(&korg1212->lock, flags);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100651 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100654static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 if (korg1212->idleMonitorOn) {
657 snd_korg1212_SendStopAndWait(korg1212);
658 korg1212->idleMonitorOn = 0;
659 }
660}
661
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100662static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 korg1212->cardState = csState;
665}
666
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100667static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100669 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
670 stateName[korg1212->cardState], korg1212->opencnt);
Ingo Molnar62932df2006-01-16 16:34:20 +0100671 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (korg1212->opencnt++ == 0) {
673 snd_korg1212_TurnOffIdleMonitor(korg1212);
674 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
675 }
676
Ingo Molnar62932df2006-01-16 16:34:20 +0100677 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 1;
679}
680
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100681static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100683 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
684 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Ingo Molnar62932df2006-01-16 16:34:20 +0100686 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (--(korg1212->opencnt)) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100688 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690 }
691
692 if (korg1212->cardState == K1212_STATE_SETUP) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100693 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 K1212_MODE_StopPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100695 if (rc)
696 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
697 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (rc != K1212_CMDRET_Success) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100699 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701 }
702 } else if (korg1212->cardState > K1212_STATE_SETUP) {
703 snd_korg1212_SendStopAndWait(korg1212);
704 }
705
706 if (korg1212->cardState > K1212_STATE_READY) {
707 snd_korg1212_TurnOnIdleMonitor(korg1212);
708 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
709 }
710
Ingo Molnar62932df2006-01-16 16:34:20 +0100711 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
715/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100716static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100718 int rc;
719
720 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
721 stateName[korg1212->cardState], korg1212->setcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (korg1212->setcnt++)
724 return 0;
725
726 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
727 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
728 K1212_MODE_SetupPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100729 if (rc)
730 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
731 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (rc != K1212_CMDRET_Success) {
733 return 1;
734 }
735 return 0;
736}
737
738/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100739static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100741 int rc;
742
743 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
744 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (korg1212->playcnt++)
747 return 0;
748
749 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
750 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100751 if (rc)
752 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
753 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (rc != K1212_CMDRET_Success) {
755 return 1;
756 }
757 return 0;
758}
759
760/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100761static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100763 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
764 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (--(korg1212->playcnt))
767 return 0;
768
769 korg1212->setcnt = 0;
770
771 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
772 snd_korg1212_SendStop(korg1212);
773
774 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
775 return 0;
776}
777
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100778static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 writel(PCI_INT_ENABLE_BIT |
781 PCI_DOORBELL_INT_ENABLE_BIT |
782 LOCAL_INT_ENABLE_BIT |
783 LOCAL_DOORBELL_INT_ENABLE_BIT |
784 LOCAL_DMA1_INT_ENABLE_BIT,
785 korg1212->statusRegPtr);
786}
787
788#if 0 /* not used */
789
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100790static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
791 enum MonitorModeSelector mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100793 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
794 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 switch (mode) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100797 case K1212_MONMODE_Off:
798 if (korg1212->cardState != K1212_STATE_MONITOR)
799 return 0;
800 else {
801 snd_korg1212_SendStopAndWait(korg1212);
802 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
803 }
804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Takashi Iwai9fd91562005-11-17 10:45:48 +0100806 case K1212_MONMODE_On:
807 if (korg1212->cardState != K1212_STATE_OPEN)
808 return 0;
809 else {
810 int rc;
811 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
812 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
813 K1212_MODE_MonitorOn, 0, 0, 0);
814 if (rc != K1212_CMDRET_Success)
815 return 0;
816 }
817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai9fd91562005-11-17 10:45:48 +0100819 default:
820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 return 1;
824}
825
826#endif /* not used */
827
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100828static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100830 if (korg1212->playback_pid != korg1212->capture_pid &&
831 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Takashi Iwai9fd91562005-11-17 10:45:48 +0100834 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100837static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100839 static enum ClockSourceIndex s44[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100840 K1212_CLKIDX_AdatAt44_1K,
841 K1212_CLKIDX_WordAt44_1K,
842 K1212_CLKIDX_LocalAt44_1K
843 };
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100844 static enum ClockSourceIndex s48[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100845 K1212_CLKIDX_AdatAt48K,
846 K1212_CLKIDX_WordAt48K,
847 K1212_CLKIDX_LocalAt48K
848 };
849 int parm, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Takashi Iwai9fd91562005-11-17 10:45:48 +0100851 if (!snd_korg1212_use_is_exclusive (korg1212))
852 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100854 switch (rate) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100855 case 44100:
856 parm = s44[korg1212->clkSource];
857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Takashi Iwai9fd91562005-11-17 10:45:48 +0100859 case 48000:
860 parm = s48[korg1212->clkSource];
861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Takashi Iwai9fd91562005-11-17 10:45:48 +0100863 default:
864 return -EINVAL;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 korg1212->clkSrcRate = parm;
868 korg1212->clkRate = rate;
869
870 udelay(INTERCOMMAND_DELAY);
871 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
872 ClockSourceSelector[korg1212->clkSrcRate],
873 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100874 if (rc)
875 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
876 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return 0;
879}
880
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100881static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883
Takashi Iwai9fd91562005-11-17 10:45:48 +0100884 if (source < 0 || source > 2)
885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 korg1212->clkSource = source;
888
889 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
890
891 return 0;
892}
893
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100894static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 writel(0, korg1212->statusRegPtr);
897}
898
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100899static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100901 struct SensBits sensVals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int bitPosition;
903 int channel;
904 int clkIs48K;
905 int monModeSet;
906 u16 controlValue; // this keeps the current value to be written to
907 // the card's eeprom control register.
908 u16 count;
909 unsigned long flags;
910
Takashi Iwai9fd91562005-11-17 10:45:48 +0100911 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
912 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 // ----------------------------------------------------------------------------
915 // initialize things. The local init bit is always set when writing to the
916 // card's control register.
917 // ----------------------------------------------------------------------------
918 controlValue = 0;
919 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
920
921 // ----------------------------------------------------------------------------
922 // make sure the card is not in monitor mode when we do this update.
923 // ----------------------------------------------------------------------------
924 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
925 monModeSet = 1;
926 snd_korg1212_SendStopAndWait(korg1212);
927 } else
928 monModeSet = 0;
929
930 spin_lock_irqsave(&korg1212->lock, flags);
931
932 // ----------------------------------------------------------------------------
933 // we are about to send new values to the card, so clear the new values queued
934 // flag. Also, clear out mailbox 3, so we don't lockup.
935 // ----------------------------------------------------------------------------
936 writel(0, korg1212->mailbox3Ptr);
937 udelay(LOADSHIFT_DELAY);
938
939 // ----------------------------------------------------------------------------
940 // determine whether we are running a 48K or 44.1K clock. This info is used
941 // later when setting the SPDIF FF after the volume has been shifted in.
942 // ----------------------------------------------------------------------------
943 switch (korg1212->clkSrcRate) {
944 case K1212_CLKIDX_AdatAt44_1K:
945 case K1212_CLKIDX_WordAt44_1K:
946 case K1212_CLKIDX_LocalAt44_1K:
947 clkIs48K = 0;
948 break;
949
950 case K1212_CLKIDX_WordAt48K:
951 case K1212_CLKIDX_AdatAt48K:
952 case K1212_CLKIDX_LocalAt48K:
953 default:
954 clkIs48K = 1;
955 break;
956 }
957
958 // ----------------------------------------------------------------------------
959 // start the update. Setup the bit structure and then shift the bits.
960 // ----------------------------------------------------------------------------
961 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
962 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
963 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
964 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
965
966 // ----------------------------------------------------------------------------
967 // now start shifting the bits in. Start with the left channel then the right.
968 // ----------------------------------------------------------------------------
969 for (channel = 0; channel < 2; channel++) {
970
971 // ----------------------------------------------------------------------------
972 // Bring the load/shift line low, then wait - the spec says >150ns from load/
973 // shift low to the first rising edge of the clock.
974 // ----------------------------------------------------------------------------
975 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
976 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
977 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
978 udelay(LOADSHIFT_DELAY);
979
980 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
Takashi Iwai9fd91562005-11-17 10:45:48 +0100981 if (channel == 0) {
982 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100983 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
Takashi Iwai9fd91562005-11-17 10:45:48 +0100984 else
985 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
986 } else {
987 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
988 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
989 else
990 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
994 writew(controlValue, korg1212->sensRegPtr); // clock goes low
995 udelay(SENSCLKPULSE_WIDTH);
996 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
997 writew(controlValue, korg1212->sensRegPtr); // clock goes high
998 udelay(SENSCLKPULSE_WIDTH);
999 }
1000
1001 // ----------------------------------------------------------------------------
1002 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1003 // bit if the clock rate is 48K otherwise write 0.
1004 // ----------------------------------------------------------------------------
1005 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1006 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1007 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1008 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1009 udelay(SENSCLKPULSE_WIDTH);
1010
1011 if (clkIs48K)
1012 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1013
1014 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1015 udelay(ONE_RTC_TICK);
1016 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1017 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1018 udelay(SENSCLKPULSE_WIDTH);
1019 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1020 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1021 udelay(SENSCLKPULSE_WIDTH);
1022 }
1023
1024 // ----------------------------------------------------------------------------
1025 // The update is complete. Set a timeout. This is the inter-update delay.
1026 // Also, if the card was in monitor mode, restore it.
1027 // ----------------------------------------------------------------------------
1028 for (count = 0; count < 10; count++)
1029 udelay(SENSCLKPULSE_WIDTH);
1030
1031 if (monModeSet) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001032 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 K1212_MODE_MonitorOn, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001034 if (rc)
1035 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1036 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 spin_unlock_irqrestore(&korg1212->lock, flags);
1040
1041 return 1;
1042}
1043
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001044static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001046 int channel, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Takashi Iwai9fd91562005-11-17 10:45:48 +01001048 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1049 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 // ----------------------------------------------------
1052 // tell the card to boot
1053 // ----------------------------------------------------
1054 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1055
Takashi Iwai9fd91562005-11-17 10:45:48 +01001056 if (rc)
1057 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1058 rc, stateName[korg1212->cardState]);
1059 msleep(DSP_BOOT_DELAY_IN_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 // --------------------------------------------------------------------------------
1062 // Let the card know where all the buffers are.
1063 // --------------------------------------------------------------------------------
1064 rc = snd_korg1212_Send1212Command(korg1212,
1065 K1212_DB_ConfigureBufferMemory,
1066 LowerWordSwap(korg1212->PlayDataPhy),
1067 LowerWordSwap(korg1212->RecDataPhy),
1068 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1069 // is based on 2 buffers
1070 0
1071 );
1072
Takashi Iwai9fd91562005-11-17 10:45:48 +01001073 if (rc)
1074 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1075 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 udelay(INTERCOMMAND_DELAY);
1078
1079 rc = snd_korg1212_Send1212Command(korg1212,
1080 K1212_DB_ConfigureMiscMemory,
1081 LowerWordSwap(korg1212->VolumeTablePhy),
1082 LowerWordSwap(korg1212->RoutingTablePhy),
1083 LowerWordSwap(korg1212->AdatTimeCodePhy),
1084 0
1085 );
1086
Takashi Iwai9fd91562005-11-17 10:45:48 +01001087 if (rc)
1088 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1089 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 // --------------------------------------------------------------------------------
1092 // Initialize the routing and volume tables, then update the card's state.
1093 // --------------------------------------------------------------------------------
1094 udelay(INTERCOMMAND_DELAY);
1095
1096 for (channel = 0; channel < kAudioChannels; channel++) {
1097 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1098 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1099 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1100 }
1101
1102 snd_korg1212_WriteADCSensitivity(korg1212);
1103
1104 udelay(INTERCOMMAND_DELAY);
1105 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1106 ClockSourceSelector[korg1212->clkSrcRate],
1107 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001108 if (rc)
1109 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1110 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Takashi Iwai9fd91562005-11-17 10:45:48 +01001112 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1114
Takashi Iwai9fd91562005-11-17 10:45:48 +01001115 if (rc)
1116 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1117 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1120}
1121
David Howells7d12e782006-10-05 14:55:46 +01001122static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 u32 doorbellValue;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001125 struct snd_korg1212 *korg1212 = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 doorbellValue = readl(korg1212->inDoorbellPtr);
1128
1129 if (!doorbellValue)
1130 return IRQ_NONE;
1131
1132 spin_lock(&korg1212->lock);
1133
1134 writel(doorbellValue, korg1212->inDoorbellPtr);
1135
1136 korg1212->irqcount++;
1137
1138 korg1212->inIRQ++;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 switch (doorbellValue) {
1141 case K1212_DB_DSPDownloadDone:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001142 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1143 korg1212->irqcount, doorbellValue,
1144 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1146 korg1212->dsp_is_loaded = 1;
1147 wake_up(&korg1212->wait);
1148 }
1149 break;
1150
1151 // ------------------------------------------------------------------------
1152 // an error occurred - stop the card
1153 // ------------------------------------------------------------------------
1154 case K1212_DB_DMAERROR:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001155 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1156 korg1212->irqcount, doorbellValue,
1157 stateName[korg1212->cardState]);
1158 snd_printk(KERN_ERR "korg1212: DMA Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 korg1212->errorcnt++;
1160 korg1212->totalerrorcnt++;
1161 korg1212->sharedBufferPtr->cardCommand = 0;
1162 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1163 break;
1164
1165 // ------------------------------------------------------------------------
1166 // the card has stopped by our request. Clear the command word and signal
1167 // the semaphore in case someone is waiting for this.
1168 // ------------------------------------------------------------------------
1169 case K1212_DB_CARDSTOPPED:
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001170 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
Takashi Iwai9fd91562005-11-17 10:45:48 +01001171 korg1212->irqcount, doorbellValue,
1172 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 korg1212->sharedBufferPtr->cardCommand = 0;
1174 break;
1175
1176 default:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001177 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001178 korg1212->irqcount, doorbellValue,
1179 korg1212->currentBuffer, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1181 korg1212->currentBuffer++;
1182
1183 if (korg1212->currentBuffer >= kNumBuffers)
1184 korg1212->currentBuffer = 0;
1185
1186 if (!korg1212->running)
1187 break;
1188
1189 if (korg1212->capture_substream) {
1190 spin_unlock(&korg1212->lock);
1191 snd_pcm_period_elapsed(korg1212->capture_substream);
1192 spin_lock(&korg1212->lock);
1193 }
1194
1195 if (korg1212->playback_substream) {
1196 spin_unlock(&korg1212->lock);
1197 snd_pcm_period_elapsed(korg1212->playback_substream);
1198 spin_lock(&korg1212->lock);
1199 }
1200 }
1201 break;
1202 }
1203
1204 korg1212->inIRQ--;
1205
1206 spin_unlock(&korg1212->lock);
1207
1208 return IRQ_HANDLED;
1209}
1210
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001211static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001213 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Takashi Iwai9fd91562005-11-17 10:45:48 +01001215 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1216 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 // ---------------------------------------------------------------
1219 // verify the state of the card before proceeding.
1220 // ---------------------------------------------------------------
Takashi Iwai9fd91562005-11-17 10:45:48 +01001221 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1225
1226 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1227
1228 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1229 UpperWordSwap(korg1212->dma_dsp.addr),
1230 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001231 if (rc)
1232 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1233 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 korg1212->dsp_is_loaded = 0;
1236 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1237 if (! korg1212->dsp_is_loaded )
1238 return -EBUSY; /* timeout */
1239
1240 snd_korg1212_OnDSPDownloadComplete(korg1212);
1241
1242 return 0;
1243}
1244
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001245static struct snd_pcm_hardware snd_korg1212_playback_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 .info = (SNDRV_PCM_INFO_MMAP |
1248 SNDRV_PCM_INFO_MMAP_VALID |
1249 SNDRV_PCM_INFO_INTERLEAVED),
1250 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1251 .rates = (SNDRV_PCM_RATE_44100 |
1252 SNDRV_PCM_RATE_48000),
1253 .rate_min = 44100,
1254 .rate_max = 48000,
1255 .channels_min = K1212_MIN_CHANNELS,
1256 .channels_max = K1212_MAX_CHANNELS,
1257 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1258 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1259 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1260 .periods_min = K1212_PERIODS,
1261 .periods_max = K1212_PERIODS,
1262 .fifo_size = 0,
1263};
1264
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001265static struct snd_pcm_hardware snd_korg1212_capture_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 .info = (SNDRV_PCM_INFO_MMAP |
1268 SNDRV_PCM_INFO_MMAP_VALID |
1269 SNDRV_PCM_INFO_INTERLEAVED),
1270 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1271 .rates = (SNDRV_PCM_RATE_44100 |
1272 SNDRV_PCM_RATE_48000),
1273 .rate_min = 44100,
1274 .rate_max = 48000,
1275 .channels_min = K1212_MIN_CHANNELS,
1276 .channels_max = K1212_MAX_CHANNELS,
1277 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1278 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1279 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1280 .periods_min = K1212_PERIODS,
1281 .periods_max = K1212_PERIODS,
1282 .fifo_size = 0,
1283};
1284
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001285static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001287 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int i;
1289
Takashi Iwai9fd91562005-11-17 10:45:48 +01001290 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1291 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1293
1294 for (i=0; i < count; i++) {
1295#if K1212_DEBUG_LEVEL > 0
1296 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1297 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001298 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1299 dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EFAULT;
1301 }
1302#endif
1303 memset((void*) dst + offset, 0, size);
1304 dst++;
1305 }
1306
1307 return 0;
1308}
1309
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001310static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001312 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int i, rc;
1314
Takashi Iwai9fd91562005-11-17 10:45:48 +01001315 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1316 pos, offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1318
1319 for (i=0; i < count; i++) {
1320#if K1212_DEBUG_LEVEL > 0
1321 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1322 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001323 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EFAULT;
1325 }
1326#endif
1327 rc = copy_to_user(dst + offset, src, size);
1328 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EFAULT;
1331 }
1332 src++;
1333 dst += size;
1334 }
1335
1336 return 0;
1337}
1338
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001339static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001341 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int i, rc;
1343
Takashi Iwai9fd91562005-11-17 10:45:48 +01001344 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1345 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1348
1349 for (i=0; i < count; i++) {
1350#if K1212_DEBUG_LEVEL > 0
1351 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1352 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001353 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
1355 }
1356#endif
1357 rc = copy_from_user((void*) dst + offset, src, size);
1358 if (rc) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001359 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EFAULT;
1361 }
1362 dst++;
1363 src += size;
1364 }
1365
1366 return 0;
1367}
1368
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001369static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001371 struct snd_korg1212 *korg1212 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Takashi Iwai9fd91562005-11-17 10:45:48 +01001373 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1374 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 korg1212->pcm = NULL;
1377}
1378
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001379static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001382 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1383 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Takashi Iwai9fd91562005-11-17 10:45:48 +01001385 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1386 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 snd_pcm_set_sync(substream); // ???
1389
1390 snd_korg1212_OpenCard(korg1212);
1391
1392 runtime->hw = snd_korg1212_playback_info;
1393 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1394
1395 spin_lock_irqsave(&korg1212->lock, flags);
1396
1397 korg1212->playback_substream = substream;
1398 korg1212->playback_pid = current->pid;
1399 korg1212->periodsize = K1212_PERIODS;
1400 korg1212->channels = K1212_CHANNELS;
1401 korg1212->errorcnt = 0;
1402
1403 spin_unlock_irqrestore(&korg1212->lock, flags);
1404
1405 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1406 return 0;
1407}
1408
1409
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001410static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001413 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1414 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Takashi Iwai9fd91562005-11-17 10:45:48 +01001416 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1417 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 snd_pcm_set_sync(substream);
1420
1421 snd_korg1212_OpenCard(korg1212);
1422
1423 runtime->hw = snd_korg1212_capture_info;
1424 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1425
1426 spin_lock_irqsave(&korg1212->lock, flags);
1427
1428 korg1212->capture_substream = substream;
1429 korg1212->capture_pid = current->pid;
1430 korg1212->periodsize = K1212_PERIODS;
1431 korg1212->channels = K1212_CHANNELS;
1432
1433 spin_unlock_irqrestore(&korg1212->lock, flags);
1434
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001435 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1436 kPlayBufferFrames, kPlayBufferFrames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438}
1439
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001440static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001443 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Takashi Iwai9fd91562005-11-17 10:45:48 +01001445 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1446 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1449
1450 spin_lock_irqsave(&korg1212->lock, flags);
1451
1452 korg1212->playback_pid = -1;
1453 korg1212->playback_substream = NULL;
1454 korg1212->periodsize = 0;
1455
1456 spin_unlock_irqrestore(&korg1212->lock, flags);
1457
1458 snd_korg1212_CloseCard(korg1212);
1459 return 0;
1460}
1461
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001462static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001465 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Takashi Iwai9fd91562005-11-17 10:45:48 +01001467 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1468 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 spin_lock_irqsave(&korg1212->lock, flags);
1471
1472 korg1212->capture_pid = -1;
1473 korg1212->capture_substream = NULL;
1474 korg1212->periodsize = 0;
1475
1476 spin_unlock_irqrestore(&korg1212->lock, flags);
1477
1478 snd_korg1212_CloseCard(korg1212);
1479 return 0;
1480}
1481
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001482static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 unsigned int cmd, void *arg)
1484{
Takashi Iwai9fd91562005-11-17 10:45:48 +01001485 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001488 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 info->offset = 0;
1490 info->first = info->channel * 16;
1491 info->step = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494 }
1495
1496 return snd_pcm_lib_ioctl(substream, cmd, arg);
1497}
1498
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001499static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1500 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001503 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int err;
1505 pid_t this_pid;
1506 pid_t other_pid;
1507
Takashi Iwai9fd91562005-11-17 10:45:48 +01001508 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1509 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 spin_lock_irqsave(&korg1212->lock, flags);
1512
1513 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1514 this_pid = korg1212->playback_pid;
1515 other_pid = korg1212->capture_pid;
1516 } else {
1517 this_pid = korg1212->capture_pid;
1518 other_pid = korg1212->playback_pid;
1519 }
1520
1521 if ((other_pid > 0) && (this_pid != other_pid)) {
1522
1523 /* The other stream is open, and not by the same
1524 task as this one. Make sure that the parameters
1525 that matter are the same.
1526 */
1527
1528 if ((int)params_rate(params) != korg1212->clkRate) {
1529 spin_unlock_irqrestore(&korg1212->lock, flags);
1530 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1531 return -EBUSY;
1532 }
1533
1534 spin_unlock_irqrestore(&korg1212->lock, flags);
1535 return 0;
1536 }
1537
1538 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1539 spin_unlock_irqrestore(&korg1212->lock, flags);
1540 return err;
1541 }
1542
1543 korg1212->channels = params_channels(params);
1544 korg1212->periodsize = K1212_PERIOD_BYTES;
1545
1546 spin_unlock_irqrestore(&korg1212->lock, flags);
1547
1548 return 0;
1549}
1550
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001551static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001553 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int rc;
1555
Takashi Iwai9fd91562005-11-17 10:45:48 +01001556 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1557 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 spin_lock_irq(&korg1212->lock);
1560
1561 /* FIXME: we should wait for ack! */
1562 if (korg1212->stop_pending_cnt > 0) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001563 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1564 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 spin_unlock_irq(&korg1212->lock);
1566 return -EAGAIN;
1567 /*
1568 korg1212->sharedBufferPtr->cardCommand = 0;
1569 del_timer(&korg1212->timer);
1570 korg1212->stop_pending_cnt = 0;
1571 */
1572 }
1573
1574 rc = snd_korg1212_SetupForPlay(korg1212);
1575
1576 korg1212->currentBuffer = 0;
1577
1578 spin_unlock_irq(&korg1212->lock);
1579
1580 return rc ? -EINVAL : 0;
1581}
1582
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001583static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int cmd)
1585{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001586 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int rc;
1588
Takashi Iwai9fd91562005-11-17 10:45:48 +01001589 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1590 stateName[korg1212->cardState], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 spin_lock(&korg1212->lock);
1593 switch (cmd) {
1594 case SNDRV_PCM_TRIGGER_START:
1595/*
1596 if (korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001597 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 break;
1599 }
1600*/
1601 korg1212->running++;
1602 rc = snd_korg1212_TriggerPlay(korg1212);
1603 break;
1604
1605 case SNDRV_PCM_TRIGGER_STOP:
1606/*
1607 if (!korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001608 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
1610 }
1611*/
1612 korg1212->running--;
1613 rc = snd_korg1212_StopPlay(korg1212);
1614 break;
1615
1616 default:
1617 rc = 1;
1618 break;
1619 }
1620 spin_unlock(&korg1212->lock);
1621 return rc ? -EINVAL : 0;
1622}
1623
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001624static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001626 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 snd_pcm_uframes_t pos;
1628
1629 pos = korg1212->currentBuffer * kPlayBufferFrames;
1630
Takashi Iwai9fd91562005-11-17 10:45:48 +01001631 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1632 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 return pos;
1635}
1636
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001637static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001639 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 snd_pcm_uframes_t pos;
1641
1642 pos = korg1212->currentBuffer * kPlayBufferFrames;
1643
Takashi Iwai9fd91562005-11-17 10:45:48 +01001644 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1645 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 return pos;
1648}
1649
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001650static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int channel, /* not used (interleaved data) */
1652 snd_pcm_uframes_t pos,
1653 void __user *src,
1654 snd_pcm_uframes_t count)
1655{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001656 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Takashi Iwai9fd91562005-11-17 10:45:48 +01001658 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1659 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1662
1663}
1664
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001665static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int channel, /* not used (interleaved data) */
1667 snd_pcm_uframes_t pos,
1668 snd_pcm_uframes_t count)
1669{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001670 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Takashi Iwai9fd91562005-11-17 10:45:48 +01001672 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1673 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1676}
1677
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001678static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int channel, /* not used (interleaved data) */
1680 snd_pcm_uframes_t pos,
1681 void __user *dst,
1682 snd_pcm_uframes_t count)
1683{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001684 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Takashi Iwai9fd91562005-11-17 10:45:48 +01001686 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1687 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1690}
1691
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001692static struct snd_pcm_ops snd_korg1212_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 .open = snd_korg1212_playback_open,
1694 .close = snd_korg1212_playback_close,
1695 .ioctl = snd_korg1212_ioctl,
1696 .hw_params = snd_korg1212_hw_params,
1697 .prepare = snd_korg1212_prepare,
1698 .trigger = snd_korg1212_trigger,
1699 .pointer = snd_korg1212_playback_pointer,
1700 .copy = snd_korg1212_playback_copy,
1701 .silence = snd_korg1212_playback_silence,
1702};
1703
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001704static struct snd_pcm_ops snd_korg1212_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 .open = snd_korg1212_capture_open,
1706 .close = snd_korg1212_capture_close,
1707 .ioctl = snd_korg1212_ioctl,
1708 .hw_params = snd_korg1212_hw_params,
1709 .prepare = snd_korg1212_prepare,
1710 .trigger = snd_korg1212_trigger,
1711 .pointer = snd_korg1212_capture_pointer,
1712 .copy = snd_korg1212_capture_copy,
1713};
1714
1715/*
1716 * Control Interface
1717 */
1718
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001719static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1723 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1724 return 0;
1725}
1726
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001727static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001730 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int i = kcontrol->private_value;
1732
1733 spin_lock_irq(&korg1212->lock);
1734
1735 u->value.integer.value[0] = korg1212->volumePhase[i];
1736
1737 if (i >= 8)
1738 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1739
1740 spin_unlock_irq(&korg1212->lock);
1741
1742 return 0;
1743}
1744
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001745static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001748 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int change = 0;
1750 int i, val;
1751
1752 spin_lock_irq(&korg1212->lock);
1753
1754 i = kcontrol->private_value;
1755
1756 korg1212->volumePhase[i] = u->value.integer.value[0];
1757
1758 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1759
1760 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1761 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1762 korg1212->sharedBufferPtr->volumeData[i] = val;
1763 change = 1;
1764 }
1765
1766 if (i >= 8) {
1767 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1768
1769 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1770
1771 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1772 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1773 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1774 change = 1;
1775 }
1776 }
1777
1778 spin_unlock_irq(&korg1212->lock);
1779
1780 return change;
1781}
1782
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001783static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1787 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1788 uinfo->value.integer.min = k1212MinVolume;
1789 uinfo->value.integer.max = k1212MaxVolume;
1790 return 0;
1791}
1792
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001793static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1794 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001796 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int i;
1798
1799 spin_lock_irq(&korg1212->lock);
1800
1801 i = kcontrol->private_value;
1802 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1803
1804 if (i >= 8)
1805 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1806
1807 spin_unlock_irq(&korg1212->lock);
1808
1809 return 0;
1810}
1811
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001812static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1813 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001815 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int change = 0;
1817 int i;
1818 int val;
1819
1820 spin_lock_irq(&korg1212->lock);
1821
1822 i = kcontrol->private_value;
1823
1824 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1825 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1826 val *= u->value.integer.value[0];
1827 korg1212->sharedBufferPtr->volumeData[i] = val;
1828 change = 1;
1829 }
1830
1831 if (i >= 8) {
1832 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1833 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1834 val *= u->value.integer.value[1];
1835 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1836 change = 1;
1837 }
1838 }
1839
1840 spin_unlock_irq(&korg1212->lock);
1841
1842 return change;
1843}
1844
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001845static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1849 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1850 uinfo->value.enumerated.items = kAudioChannels;
1851 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1852 uinfo->value.enumerated.item = kAudioChannels-1;
1853 }
1854 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1855 return 0;
1856}
1857
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001858static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1859 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001861 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int i;
1863
1864 spin_lock_irq(&korg1212->lock);
1865
1866 i = kcontrol->private_value;
1867 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1868
1869 if (i >= 8)
1870 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1871
1872 spin_unlock_irq(&korg1212->lock);
1873
1874 return 0;
1875}
1876
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001877static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001880 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int change = 0, i;
1882
1883 spin_lock_irq(&korg1212->lock);
1884
1885 i = kcontrol->private_value;
1886
1887 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1888 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1889 change = 1;
1890 }
1891
1892 if (i >= 8) {
1893 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1894 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1895 change = 1;
1896 }
1897 }
1898
1899 spin_unlock_irq(&korg1212->lock);
1900
1901 return change;
1902}
1903
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001904static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1908 uinfo->count = 2;
1909 uinfo->value.integer.min = k1212MaxADCSens;
1910 uinfo->value.integer.max = k1212MinADCSens;
1911 return 0;
1912}
1913
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001914static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1915 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001917 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 spin_lock_irq(&korg1212->lock);
1920
1921 u->value.integer.value[0] = korg1212->leftADCInSens;
1922 u->value.integer.value[1] = korg1212->rightADCInSens;
1923
1924 spin_unlock_irq(&korg1212->lock);
1925
1926 return 0;
1927}
1928
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001929static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001932 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 int change = 0;
1934
1935 spin_lock_irq(&korg1212->lock);
1936
1937 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1938 korg1212->leftADCInSens = u->value.integer.value[0];
1939 change = 1;
1940 }
1941 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1942 korg1212->rightADCInSens = u->value.integer.value[1];
1943 change = 1;
1944 }
1945
1946 spin_unlock_irq(&korg1212->lock);
1947
1948 if (change)
1949 snd_korg1212_WriteADCSensitivity(korg1212);
1950
1951 return change;
1952}
1953
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001954static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1958 uinfo->count = 1;
1959 uinfo->value.enumerated.items = 3;
1960 if (uinfo->value.enumerated.item > 2) {
1961 uinfo->value.enumerated.item = 2;
1962 }
1963 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1964 return 0;
1965}
1966
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001967static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1968 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001970 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 spin_lock_irq(&korg1212->lock);
1973
1974 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1975
1976 spin_unlock_irq(&korg1212->lock);
1977 return 0;
1978}
1979
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001980static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001983 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 unsigned int val;
1985 int change;
1986
1987 val = ucontrol->value.enumerated.item[0] % 3;
1988 spin_lock_irq(&korg1212->lock);
1989 change = val != korg1212->clkSource;
1990 snd_korg1212_SetClockSource(korg1212, val);
1991 spin_unlock_irq(&korg1212->lock);
1992 return change;
1993}
1994
1995#define MON_MIXER(ord,c_name) \
1996 { \
1997 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
1998 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1999 .name = c_name " Monitor Volume", \
2000 .info = snd_korg1212_control_volume_info, \
2001 .get = snd_korg1212_control_volume_get, \
2002 .put = snd_korg1212_control_volume_put, \
2003 .private_value = ord, \
2004 }, \
2005 { \
2006 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2007 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2008 .name = c_name " Monitor Route", \
2009 .info = snd_korg1212_control_route_info, \
2010 .get = snd_korg1212_control_route_get, \
2011 .put = snd_korg1212_control_route_put, \
2012 .private_value = ord, \
2013 }, \
2014 { \
2015 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 .name = c_name " Monitor Phase Invert", \
2018 .info = snd_korg1212_control_phase_info, \
2019 .get = snd_korg1212_control_phase_get, \
2020 .put = snd_korg1212_control_phase_put, \
2021 .private_value = ord, \
2022 }
2023
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002024static struct snd_kcontrol_new snd_korg1212_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 MON_MIXER(8, "Analog"),
2026 MON_MIXER(10, "SPDIF"),
2027 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2028 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2029 {
2030 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002031 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 .name = "Sync Source",
2033 .info = snd_korg1212_control_sync_info,
2034 .get = snd_korg1212_control_sync_get,
2035 .put = snd_korg1212_control_sync_put,
2036 },
2037 {
2038 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2039 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2040 .name = "ADC Attenuation",
2041 .info = snd_korg1212_control_info,
2042 .get = snd_korg1212_control_get,
2043 .put = snd_korg1212_control_put,
2044 }
2045};
2046
2047/*
2048 * proc interface
2049 */
2050
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002051static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2052 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 int n;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002055 struct snd_korg1212 *korg1212 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 snd_iprintf(buffer, korg1212->card->longname);
2058 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2059 snd_iprintf(buffer, "\nGeneral settings\n");
2060 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2061 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2062 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2063 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2064 snd_iprintf(buffer, " Volume Info:\n");
2065 for (n=0; n<kAudioChannels; n++)
2066 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2067 channelName[n],
2068 channelName[korg1212->sharedBufferPtr->routeData[n]],
2069 korg1212->sharedBufferPtr->volumeData[n]);
2070 snd_iprintf(buffer, "\nGeneral status\n");
2071 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2072 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2073 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2074 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2075 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2076 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2077}
2078
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002079static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002081 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002084 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087static int
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002088snd_korg1212_free(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 snd_korg1212_TurnOffIdleMonitor(korg1212);
2091
2092 if (korg1212->irq >= 0) {
2093 synchronize_irq(korg1212->irq);
2094 snd_korg1212_DisableCardInterrupts(korg1212);
Takashi Iwai9fd91562005-11-17 10:45:48 +01002095 free_irq(korg1212->irq, korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 korg1212->irq = -1;
2097 }
2098
2099 if (korg1212->iobase != NULL) {
2100 iounmap(korg1212->iobase);
2101 korg1212->iobase = NULL;
2102 }
2103
2104 pci_release_regions(korg1212->pci);
2105
2106 // ----------------------------------------------------
2107 // free up memory resources used for the DSP download.
2108 // ----------------------------------------------------
2109 if (korg1212->dma_dsp.area) {
2110 snd_dma_free_pages(&korg1212->dma_dsp);
2111 korg1212->dma_dsp.area = NULL;
2112 }
2113
2114#ifndef K1212_LARGEALLOC
2115
2116 // ------------------------------------------------------
2117 // free up memory resources used for the Play/Rec Buffers
2118 // ------------------------------------------------------
2119 if (korg1212->dma_play.area) {
2120 snd_dma_free_pages(&korg1212->dma_play);
2121 korg1212->dma_play.area = NULL;
2122 }
2123
2124 if (korg1212->dma_rec.area) {
2125 snd_dma_free_pages(&korg1212->dma_rec);
2126 korg1212->dma_rec.area = NULL;
2127 }
2128
2129#endif
2130
2131 // ----------------------------------------------------
2132 // free up memory resources used for the Shared Buffers
2133 // ----------------------------------------------------
2134 if (korg1212->dma_shared.area) {
2135 snd_dma_free_pages(&korg1212->dma_shared);
2136 korg1212->dma_shared.area = NULL;
2137 }
2138
2139 pci_disable_device(korg1212->pci);
2140 kfree(korg1212);
2141 return 0;
2142}
2143
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002144static int snd_korg1212_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002146 struct snd_korg1212 *korg1212 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return snd_korg1212_free(korg1212);
2149}
2150
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002151static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2152 struct snd_korg1212 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154{
Takashi Iwai9fd91562005-11-17 10:45:48 +01002155 int err, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 unsigned int i;
2157 unsigned ioport_size, iomem_size, iomem2_size;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002158 struct snd_korg1212 * korg1212;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002160 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 .dev_free = snd_korg1212_dev_free,
2162 };
2163
2164 * rchip = NULL;
2165 if ((err = pci_enable_device(pci)) < 0)
2166 return err;
2167
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002168 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (korg1212 == NULL) {
2170 pci_disable_device(pci);
2171 return -ENOMEM;
2172 }
2173
2174 korg1212->card = card;
2175 korg1212->pci = pci;
2176
2177 init_waitqueue_head(&korg1212->wait);
2178 spin_lock_init(&korg1212->lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002179 mutex_init(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 init_timer(&korg1212->timer);
2181 korg1212->timer.function = snd_korg1212_timer_func;
2182 korg1212->timer.data = (unsigned long)korg1212;
2183
2184 korg1212->irq = -1;
2185 korg1212->clkSource = K1212_CLKIDX_Local;
2186 korg1212->clkRate = 44100;
2187 korg1212->inIRQ = 0;
2188 korg1212->running = 0;
2189 korg1212->opencnt = 0;
2190 korg1212->playcnt = 0;
2191 korg1212->setcnt = 0;
2192 korg1212->totalerrorcnt = 0;
2193 korg1212->playback_pid = -1;
2194 korg1212->capture_pid = -1;
2195 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2196 korg1212->idleMonitorOn = 0;
2197 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2198 korg1212->leftADCInSens = k1212MaxADCSens;
2199 korg1212->rightADCInSens = k1212MaxADCSens;
2200
2201 for (i=0; i<kAudioChannels; i++)
2202 korg1212->volumePhase[i] = 0;
2203
2204 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2205 kfree(korg1212);
2206 pci_disable_device(pci);
2207 return err;
2208 }
2209
2210 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2211 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2212 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2213
2214 iomem_size = pci_resource_len(korg1212->pci, 0);
2215 ioport_size = pci_resource_len(korg1212->pci, 1);
2216 iomem2_size = pci_resource_len(korg1212->pci, 2);
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2219 " iomem = 0x%lx (%d)\n"
2220 " ioport = 0x%lx (%d)\n"
2221 " iomem = 0x%lx (%d)\n"
2222 " [%s]\n",
2223 korg1212->iomem, iomem_size,
2224 korg1212->ioport, ioport_size,
2225 korg1212->iomem2, iomem2_size,
2226 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2229 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2230 korg1212->iomem + iomem_size - 1);
2231 snd_korg1212_free(korg1212);
2232 return -EBUSY;
2233 }
2234
2235 err = request_irq(pci->irq, snd_korg1212_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002236 IRQF_SHARED,
Takashi Iwai9fd91562005-11-17 10:45:48 +01002237 "korg1212", korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 if (err) {
2240 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2241 snd_korg1212_free(korg1212);
2242 return -EBUSY;
2243 }
2244
2245 korg1212->irq = pci->irq;
2246
2247 pci_set_master(korg1212->pci);
2248
2249 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2250 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2251 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2252 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2253 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2254 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2255 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2256 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2257 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2258 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2261 " Status register = 0x%p\n"
2262 " OutDoorbell = 0x%p\n"
2263 " InDoorbell = 0x%p\n"
2264 " Mailbox0 = 0x%p\n"
2265 " Mailbox1 = 0x%p\n"
2266 " Mailbox2 = 0x%p\n"
2267 " Mailbox3 = 0x%p\n"
2268 " ControlReg = 0x%p\n"
2269 " SensReg = 0x%p\n"
2270 " IDReg = 0x%p\n"
2271 " [%s]\n",
2272 korg1212->statusRegPtr,
2273 korg1212->outDoorbellPtr,
2274 korg1212->inDoorbellPtr,
2275 korg1212->mailbox0Ptr,
2276 korg1212->mailbox1Ptr,
2277 korg1212->mailbox2Ptr,
2278 korg1212->mailbox3Ptr,
2279 korg1212->controlRegPtr,
2280 korg1212->sensRegPtr,
2281 korg1212->idRegPtr,
2282 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002285 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2286 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 snd_korg1212_free(korg1212);
2288 return -ENOMEM;
2289 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002290 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2292
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002293 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295#ifndef K1212_LARGEALLOC
2296
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002297 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2300 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2301 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2302 snd_korg1212_free(korg1212);
2303 return -ENOMEM;
2304 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002305 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2309 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2312 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2313 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2314 snd_korg1212_free(korg1212);
2315 return -ENOMEM;
2316 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002317 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2321 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323#else // K1212_LARGEALLOC
2324
2325 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2326 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002327 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2328 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330#endif // K1212_LARGEALLOC
2331
2332 korg1212->dspCodeSize = sizeof (dspCode);
2333
2334 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002335 offsetof(struct KorgSharedBuffer, volumeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002337 offsetof(struct KorgSharedBuffer, routeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002339 offsetof(struct KorgSharedBuffer, AdatTimeCode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2342 korg1212->dspCodeSize, &korg1212->dma_dsp) < 0) {
2343 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", korg1212->dspCodeSize);
2344 snd_korg1212_free(korg1212);
2345 return -ENOMEM;
2346 }
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2349 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, korg1212->dspCodeSize,
2350 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2353
Takashi Iwai9fd91562005-11-17 10:45:48 +01002354 if (rc)
2355 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2358 snd_korg1212_free(korg1212);
2359 return err;
2360 }
2361
2362 snd_korg1212_EnableCardInterrupts(korg1212);
2363
2364 mdelay(CARD_BOOT_DELAY_IN_MS);
2365
2366 if (snd_korg1212_downloadDSPCode(korg1212))
2367 return -EBUSY;
2368
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002369 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 "PlayDataPhy = %08x L[%08x]\n"
2371 "korg1212: RecDataPhy = %08x L[%08x], "
2372 "VolumeTablePhy = %08x L[%08x]\n"
2373 "korg1212: RoutingTablePhy = %08x L[%08x], "
2374 "AdatTimeCodePhy = %08x L[%08x]\n",
2375 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2376 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2377 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2378 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2379 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2380 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2381
2382 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2383 return err;
2384
2385 korg1212->pcm->private_data = korg1212;
2386 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2387 strcpy(korg1212->pcm->name, "korg1212");
2388
2389 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2390
2391 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2392
2393 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2396 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2397 if (err < 0)
2398 return err;
2399 }
2400
2401 snd_korg1212_proc_init(korg1212);
2402
2403 snd_card_set_dev(card, &pci->dev);
2404
2405 * rchip = korg1212;
2406 return 0;
2407
2408}
2409
2410/*
2411 * Card initialisation
2412 */
2413
2414static int __devinit
2415snd_korg1212_probe(struct pci_dev *pci,
2416 const struct pci_device_id *pci_id)
2417{
2418 static int dev;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002419 struct snd_korg1212 *korg1212;
2420 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 int err;
2422
2423 if (dev >= SNDRV_CARDS) {
2424 return -ENODEV;
2425 }
2426 if (!enable[dev]) {
2427 dev++;
2428 return -ENOENT;
2429 }
2430 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2431 if (card == NULL)
2432 return -ENOMEM;
2433
2434 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2435 snd_card_free(card);
2436 return err;
2437 }
2438
2439 strcpy(card->driver, "korg1212");
2440 strcpy(card->shortname, "korg1212");
2441 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2442 korg1212->iomem, korg1212->irq);
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 if ((err = snd_card_register(card)) < 0) {
2447 snd_card_free(card);
2448 return err;
2449 }
2450 pci_set_drvdata(pci, card);
2451 dev++;
2452 return 0;
2453}
2454
2455static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2456{
2457 snd_card_free(pci_get_drvdata(pci));
2458 pci_set_drvdata(pci, NULL);
2459}
2460
2461static struct pci_driver driver = {
2462 .name = "korg1212",
2463 .id_table = snd_korg1212_ids,
2464 .probe = snd_korg1212_probe,
2465 .remove = __devexit_p(snd_korg1212_remove),
2466};
2467
2468static int __init alsa_card_korg1212_init(void)
2469{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002470 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473static void __exit alsa_card_korg1212_exit(void)
2474{
2475 pci_unregister_driver(&driver);
2476}
2477
2478module_init(alsa_card_korg1212_init)
2479module_exit(alsa_card_korg1212_exit)