blob: bd7c816bf6a7ab71f9fd4d448c5d6d8ce817f98a [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>
Clemens Ladisch2493a6d2006-11-06 09:24:29 +010031#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <sound/core.h>
34#include <sound/info.h>
35#include <sound/control.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/initval.h>
39
40#include <asm/io.h>
41
42// ----------------------------------------------------------------------------
43// Debug Stuff
44// ----------------------------------------------------------------------------
45#define K1212_DEBUG_LEVEL 0
Takashi Iwai9fd91562005-11-17 10:45:48 +010046#if K1212_DEBUG_LEVEL > 0
47#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
48#else
49#define K1212_DEBUG_PRINTK(fmt,...)
50#endif
51#if K1212_DEBUG_LEVEL > 1
52#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
53#else
54#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
55#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57// ----------------------------------------------------------------------------
58// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
59// buffers are alocated as a large piece inside KorgSharedBuffer.
60// ----------------------------------------------------------------------------
61//#define K1212_LARGEALLOC 1
62
63// ----------------------------------------------------------------------------
64// Valid states of the Korg 1212 I/O card.
65// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010066enum CardState {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 K1212_STATE_NONEXISTENT, // there is no card here
68 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
69 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
70 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
71 K1212_STATE_READY, // the card can be opened by an application. Any application
72 // requests prior to this state should fail. Only an open
73 // request can be made at this state.
74 K1212_STATE_OPEN, // an application has opened the card
75 K1212_STATE_SETUP, // the card has been setup for play
76 K1212_STATE_PLAYING, // the card is playing
77 K1212_STATE_MONITOR, // the card is in the monitor mode
78 K1212_STATE_CALIBRATING, // the card is currently calibrating
79 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
80 // are in the process of cleaning things up.
81 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
Takashi Iwaifcfd3332005-11-17 15:00:46 +010082};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84// ----------------------------------------------------------------------------
85// The following enumeration defines the constants written to the card's
86// host-to-card doorbell to initiate a command.
87// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010088enum korg1212_dbcnst {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
90 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
91 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
92 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
93 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
94 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
95 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
96 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
97 // timecode value.
98 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
99 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
100 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
101 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
102 // on page 4 (local page to card).
103 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
104 // completed.
105 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108
109// ----------------------------------------------------------------------------
110// The following enumeration defines return codes
111// to the Korg 1212 I/O driver.
112// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100113enum snd_korg1212rc {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 K1212_CMDRET_Success = 0, // command was successfully placed
115 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
116 K1212_CMDRET_PMFailure, // the protected mode call failed
117 K1212_CMDRET_FailUnspecified, // unspecified failure
118 K1212_CMDRET_FailBadState, // the specified command can not be given in
119 // the card's current state. (or the wave device's
120 // state)
121 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
122 K1212_CMDRET_BadIndex, // an out of range card index was specified
123 K1212_CMDRET_BadHandle, // an invalid card handle was specified
124 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
125 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
126 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
127 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
128
129 K1212_CMDRET_BadDevice, // the specified wave device was out of range
130 K1212_CMDRET_BadFormat // the specified wave format is unsupported
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100131};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133// ----------------------------------------------------------------------------
134// The following enumeration defines the constants used to select the play
135// mode for the card in the SelectPlayMode command.
136// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100137enum PlayModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
139 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
140 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
141 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100142};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144// ----------------------------------------------------------------------------
145// The following enumeration defines the constants used to select the monitor
146// mode for the card in the SetMonitorMode command.
147// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100148enum MonitorModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
150 K1212_MONMODE_On // tells card to turn on monitor mode
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100151};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
154#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
155#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
156#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
157#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
158#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
159#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
160#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
161 // register
162#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
163 // this is the upper word of the PCI control reg.
164#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
165
166#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
167 // from the card after sending a command.
168#define INTERCOMMAND_DELAY 40
169#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
170 // to send a command before giving up.
171#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
172 // the card.
173#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
174
175#define CARD_BOOT_DELAY_IN_MS 10
176#define CARD_BOOT_TIMEOUT 10
177#define DSP_BOOT_DELAY_IN_MS 200
178
179#define kNumBuffers 8
180#define k1212MaxCards 4
181#define k1212NumWaveDevices 6
182#define k16BitChannels 10
183#define k32BitChannels 2
184#define kAudioChannels (k16BitChannels + k32BitChannels)
185#define kPlayBufferFrames 1024
186
187#define K1212_ANALOG_CHANNELS 2
188#define K1212_SPDIF_CHANNELS 2
189#define K1212_ADAT_CHANNELS 8
190#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
191#define K1212_MIN_CHANNELS 1
192#define K1212_MAX_CHANNELS K1212_CHANNELS
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100193#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
195#define K1212_PERIODS (kNumBuffers)
196#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
197#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
198#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
199#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
200#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
201#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
202
203#define k1212MinADCSens 0x7f
204#define k1212MaxADCSens 0x00
205#define k1212MaxVolume 0x7fff
206#define k1212MaxWaveVolume 0xffff
207#define k1212MinVolume 0x0000
208#define k1212MaxVolInverted 0x8000
209
210// -----------------------------------------------------------------
211// the following bits are used for controlling interrupts in the
212// interrupt control/status reg
213// -----------------------------------------------------------------
214#define PCI_INT_ENABLE_BIT 0x00000100
215#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
216#define LOCAL_INT_ENABLE_BIT 0x00010000
217#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
218#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
219
220// -----------------------------------------------------------------
221// the following bits are defined for the PCI command register
222// -----------------------------------------------------------------
223#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
224#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
225#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
226
227// -----------------------------------------------------------------
228// the following bits are defined for the PCI status register
229// -----------------------------------------------------------------
230#define PCI_STAT_PARITY_ERROR_BIT 0x8000
231#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
232#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
233#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
234#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
235
236// ------------------------------------------------------------------------
237// the following constants are used in setting the 1212 I/O card's input
238// sensitivity.
239// ------------------------------------------------------------------------
240#define SET_SENS_LOCALINIT_BITPOS 15
241#define SET_SENS_DATA_BITPOS 10
242#define SET_SENS_CLOCK_BITPOS 8
243#define SET_SENS_LOADSHIFT_BITPOS 0
244
245#define SET_SENS_LEFTCHANID 0x00
246#define SET_SENS_RIGHTCHANID 0x01
247
248#define K1212SENSUPDATE_DELAY_IN_MS 50
249
250// --------------------------------------------------------------------------
251// WaitRTCTicks
252//
253// This function waits the specified number of real time clock ticks.
254// According to the DDK, each tick is ~0.8 microseconds.
255// The defines following the function declaration can be used for the
256// numTicksToWait parameter.
257// --------------------------------------------------------------------------
258#define ONE_RTC_TICK 1
259#define SENSCLKPULSE_WIDTH 4
260#define LOADSHIFT_DELAY 4
261#define INTERCOMMAND_DELAY 40
262#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
263 // the command register. (could be up to 180 us)
264#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
265 // from the card after sending a command.
266
Takashi Iwai8ad2da12007-02-26 15:55:43 +0100267#ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#include "korg1212-firmware.h"
Clemens Ladisch2493a6d2006-11-06 09:24:29 +0100269static const struct firmware static_dsp_code = {
270 .data = (u8 *)dspCode,
271 .size = sizeof dspCode
272};
273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100275enum ClockSourceIndex {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
277 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
278 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
279 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
280 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
281 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
282 K1212_CLKIDX_Invalid // used to check validity of the index
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100283};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100285enum ClockSourceType {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 K1212_CLKIDX_Adat = 0, // selects source as ADAT
287 K1212_CLKIDX_Word, // selects source as S/PDIF
288 K1212_CLKIDX_Local // selects source as local clock
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100289};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100291struct KorgAudioFrame {
292 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
293 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
294 u32 timeCodeVal; /* holds the ADAT timecode value */
295};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100297struct KorgAudioBuffer {
298 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
299};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100301struct KorgSharedBuffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#ifdef K1212_LARGEALLOC
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100303 struct KorgAudioBuffer playDataBufs[kNumBuffers];
304 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
306 short volumeData[kAudioChannels];
307 u32 cardCommand;
308 u16 routeData [kAudioChannels];
309 u32 AdatTimeCode; // ADAT timecode value
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100310};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100312struct SensBits {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 union {
314 struct {
315 unsigned int leftChanVal:8;
316 unsigned int leftChanId:8;
317 } v;
318 u16 leftSensBits;
319 } l;
320 union {
321 struct {
322 unsigned int rightChanVal:8;
323 unsigned int rightChanId:8;
324 } v;
325 u16 rightSensBits;
326 } r;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100327};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100329struct snd_korg1212 {
330 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct pci_dev *pci;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100332 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int irq;
334
335 spinlock_t lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100336 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 struct timer_list timer; /* timer callback for checking ack of stop request */
339 int stop_pending_cnt; /* counter for stop pending check */
340
341 wait_queue_head_t wait;
342
343 unsigned long iomem;
344 unsigned long ioport;
345 unsigned long iomem2;
346 unsigned long irqcount;
347 unsigned long inIRQ;
348 void __iomem *iobase;
349
350 struct snd_dma_buffer dma_dsp;
351 struct snd_dma_buffer dma_play;
352 struct snd_dma_buffer dma_rec;
353 struct snd_dma_buffer dma_shared;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 u32 DataBufsSize;
356
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100357 struct KorgAudioBuffer * playDataBufsPtr;
358 struct KorgAudioBuffer * recordDataBufsPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100360 struct KorgSharedBuffer * sharedBufferPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 u32 RecDataPhy;
363 u32 PlayDataPhy;
364 unsigned long sharedBufferPhy;
365 u32 VolumeTablePhy;
366 u32 RoutingTablePhy;
367 u32 AdatTimeCodePhy;
368
369 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
370 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
371 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
372 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
373 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
374 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
375 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
376 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
377 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
378 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
379
380 size_t periodsize;
381 int channels;
382 int currentBuffer;
383
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100384 struct snd_pcm_substream *playback_substream;
385 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 pid_t capture_pid;
388 pid_t playback_pid;
389
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100390 enum CardState cardState;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int running;
392 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
393 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
394
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100395 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100397 enum ClockSourceType clkSource; // clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int clkRate; // clock rate
399
400 int volumePhase[kAudioChannels];
401
402 u16 leftADCInSens; // ADC left channel input sensitivity
403 u16 rightADCInSens; // ADC right channel input sensitivity
404
405 int opencnt; // Open/Close count
406 int setcnt; // SetupForPlay count
407 int playcnt; // TriggerPlay count
408 int errorcnt; // Error Count
409 unsigned long totalerrorcnt; // Total Error Count
410
411 int dsp_is_loaded;
412 int dsp_stop_is_processed;
413
414};
415
416MODULE_DESCRIPTION("korg1212");
417MODULE_LICENSE("GPL");
418MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
419
420static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
421static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
422static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
423
424module_param_array(index, int, NULL, 0444);
425MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
426module_param_array(id, charp, NULL, 0444);
427MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
428module_param_array(enable, bool, NULL, 0444);
429MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
430MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
431
Takashi Iwaif40b6892006-07-05 16:51:05 +0200432static struct pci_device_id snd_korg1212_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 {
434 .vendor = 0x10b5,
435 .device = 0x906d,
436 .subvendor = PCI_ANY_ID,
437 .subdevice = PCI_ANY_ID,
438 },
439 { 0, },
440};
441
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100442MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
443
Takashi Iwai9fd91562005-11-17 10:45:48 +0100444static char *stateName[] = {
445 "Non-existent",
446 "Uninitialized",
447 "DSP download in process",
448 "DSP download complete",
449 "Ready",
450 "Open",
451 "Setup for play",
452 "Playing",
453 "Monitor mode on",
454 "Calibrating",
455 "Invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456};
457
Takashi Iwai9fd91562005-11-17 10:45:48 +0100458static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Takashi Iwai9fd91562005-11-17 10:45:48 +0100460static char *clockSourceName[] = {
461 "ADAT at 44.1 kHz",
462 "ADAT at 48 kHz",
463 "S/PDIF at 44.1 kHz",
464 "S/PDIF at 48 kHz",
465 "local clock at 44.1 kHz",
466 "local clock at 48 kHz"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467};
468
Takashi Iwai9fd91562005-11-17 10:45:48 +0100469static char *channelName[] = {
470 "ADAT-1",
471 "ADAT-2",
472 "ADAT-3",
473 "ADAT-4",
474 "ADAT-5",
475 "ADAT-6",
476 "ADAT-7",
477 "ADAT-8",
478 "Analog-L",
479 "Analog-R",
480 "SPDIF-L",
481 "SPDIF-R",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482};
483
Takashi Iwai9fd91562005-11-17 10:45:48 +0100484static u16 ClockSourceSelector[] = {
485 0x8000, // selects source as ADAT at 44.1 kHz
486 0x0000, // selects source as ADAT at 48 kHz
487 0x8001, // selects source as S/PDIF at 44.1 kHz
488 0x0001, // selects source as S/PDIF at 48 kHz
489 0x8002, // selects source as local clock at 44.1 kHz
490 0x0002 // selects source as local clock at 48 kHz
491};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100493union swap_u32 { unsigned char c[4]; u32 i; };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495#ifdef SNDRV_BIG_ENDIAN
496static u32 LowerWordSwap(u32 swappee)
497#else
498static u32 UpperWordSwap(u32 swappee)
499#endif
500{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100501 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 swapper.i = swappee;
504 retVal.c[2] = swapper.c[3];
505 retVal.c[3] = swapper.c[2];
506 retVal.c[1] = swapper.c[1];
507 retVal.c[0] = swapper.c[0];
508
509 return retVal.i;
510}
511
512#ifdef SNDRV_BIG_ENDIAN
513static u32 UpperWordSwap(u32 swappee)
514#else
515static u32 LowerWordSwap(u32 swappee)
516#endif
517{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100518 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 swapper.i = swappee;
521 retVal.c[2] = swapper.c[2];
522 retVal.c[3] = swapper.c[3];
523 retVal.c[1] = swapper.c[0];
524 retVal.c[0] = swapper.c[1];
525
526 return retVal.i;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
530#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
531#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
532#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
533
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100534static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
535 enum korg1212_dbcnst doorbellVal,
536 u32 mailBox0Val, u32 mailBox1Val,
537 u32 mailBox2Val, u32 mailBox3Val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 u32 retryCount;
540 u16 mailBox3Lo;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100541 int rc = K1212_CMDRET_Success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (!korg1212->outDoorbellPtr) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100544 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return K1212_CMDRET_CardUninitialized;
546 }
547
Takashi Iwai9fd91562005-11-17 10:45:48 +0100548 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
549 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
551 writel(mailBox3Val, korg1212->mailbox3Ptr);
552 writel(mailBox2Val, korg1212->mailbox2Ptr);
553 writel(mailBox1Val, korg1212->mailbox1Ptr);
554 writel(mailBox0Val, korg1212->mailbox0Ptr);
555 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
556
557 // --------------------------------------------------------------
558 // the reboot command will not give an acknowledgement.
559 // --------------------------------------------------------------
560 if ( doorbellVal == K1212_DB_RebootCard ||
561 doorbellVal == K1212_DB_BootFromDSPPage4 ||
562 doorbellVal == K1212_DB_StartDSPDownload ) {
563 rc = K1212_CMDRET_Success;
564 break;
565 }
566
567 // --------------------------------------------------------------
568 // See if the card acknowledged the command. Wait a bit, then
569 // read in the low word of mailbox3. If the MSB is set and the
570 // low byte is equal to the doorbell value, then it ack'd.
571 // --------------------------------------------------------------
572 udelay(COMMAND_ACK_DELAY);
573 mailBox3Lo = readl(korg1212->mailbox3Ptr);
574 if (mailBox3Lo & COMMAND_ACK_MASK) {
575 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100576 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rc = K1212_CMDRET_Success;
578 break;
579 }
580 }
581 }
582 korg1212->cmdRetryCount += retryCount;
583
584 if (retryCount >= MAX_COMMAND_RETRIES) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100585 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 rc = K1212_CMDRET_NoAckFromCard;
587 }
588
589 return rc;
590}
591
592/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100593static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 if (! korg1212->stop_pending_cnt) {
596 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
597 /* program the timer */
598 korg1212->stop_pending_cnt = HZ;
599 korg1212->timer.expires = jiffies + 1;
600 add_timer(&korg1212->timer);
601 }
602}
603
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100604static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 unsigned long flags;
607 spin_lock_irqsave(&korg1212->lock, flags);
608 korg1212->dsp_stop_is_processed = 0;
609 snd_korg1212_SendStop(korg1212);
610 spin_unlock_irqrestore(&korg1212->lock, flags);
611 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
612}
613
614/* timer callback for checking the ack of stop request */
615static void snd_korg1212_timer_func(unsigned long data)
616{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100617 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100618 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Takashi Iwaib32425a2005-11-18 18:52:14 +0100620 spin_lock_irqsave(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (korg1212->sharedBufferPtr->cardCommand == 0) {
622 /* ack'ed */
623 korg1212->stop_pending_cnt = 0;
624 korg1212->dsp_stop_is_processed = 1;
625 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100626 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
627 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 } else {
629 if (--korg1212->stop_pending_cnt > 0) {
630 /* reprogram timer */
631 korg1212->timer.expires = jiffies + 1;
632 add_timer(&korg1212->timer);
633 } else {
634 snd_printd("korg1212_timer_func timeout\n");
635 korg1212->sharedBufferPtr->cardCommand = 0;
636 korg1212->dsp_stop_is_processed = 1;
637 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100638 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
639 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 }
Takashi Iwaib32425a2005-11-18 18:52:14 +0100642 spin_unlock_irqrestore(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100645static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 unsigned long flags;
Takashi Iwai9fd91562005-11-17 10:45:48 +0100648 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 udelay(INTERCOMMAND_DELAY);
651 spin_lock_irqsave(&korg1212->lock, flags);
652 korg1212->idleMonitorOn = 1;
653 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
654 K1212_MODE_MonitorOn, 0, 0, 0);
655 spin_unlock_irqrestore(&korg1212->lock, flags);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100656 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100659static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 if (korg1212->idleMonitorOn) {
662 snd_korg1212_SendStopAndWait(korg1212);
663 korg1212->idleMonitorOn = 0;
664 }
665}
666
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100667static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 korg1212->cardState = csState;
670}
671
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100672static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100674 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
675 stateName[korg1212->cardState], korg1212->opencnt);
Ingo Molnar62932df2006-01-16 16:34:20 +0100676 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (korg1212->opencnt++ == 0) {
678 snd_korg1212_TurnOffIdleMonitor(korg1212);
679 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
680 }
681
Ingo Molnar62932df2006-01-16 16:34:20 +0100682 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 1;
684}
685
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100686static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100688 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
689 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Ingo Molnar62932df2006-01-16 16:34:20 +0100691 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (--(korg1212->opencnt)) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100693 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
695 }
696
697 if (korg1212->cardState == K1212_STATE_SETUP) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100698 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 K1212_MODE_StopPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100700 if (rc)
701 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
702 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (rc != K1212_CMDRET_Success) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100704 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
706 }
707 } else if (korg1212->cardState > K1212_STATE_SETUP) {
708 snd_korg1212_SendStopAndWait(korg1212);
709 }
710
711 if (korg1212->cardState > K1212_STATE_READY) {
712 snd_korg1212_TurnOnIdleMonitor(korg1212);
713 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
714 }
715
Ingo Molnar62932df2006-01-16 16:34:20 +0100716 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
720/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100721static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100723 int rc;
724
725 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
726 stateName[korg1212->cardState], korg1212->setcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (korg1212->setcnt++)
729 return 0;
730
731 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
732 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
733 K1212_MODE_SetupPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100734 if (rc)
735 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
736 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (rc != K1212_CMDRET_Success) {
738 return 1;
739 }
740 return 0;
741}
742
743/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100744static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100746 int rc;
747
748 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
749 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (korg1212->playcnt++)
752 return 0;
753
754 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
755 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100756 if (rc)
757 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
758 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (rc != K1212_CMDRET_Success) {
760 return 1;
761 }
762 return 0;
763}
764
765/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100766static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100768 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
769 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (--(korg1212->playcnt))
772 return 0;
773
774 korg1212->setcnt = 0;
775
776 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
777 snd_korg1212_SendStop(korg1212);
778
779 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
780 return 0;
781}
782
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100783static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 writel(PCI_INT_ENABLE_BIT |
786 PCI_DOORBELL_INT_ENABLE_BIT |
787 LOCAL_INT_ENABLE_BIT |
788 LOCAL_DOORBELL_INT_ENABLE_BIT |
789 LOCAL_DMA1_INT_ENABLE_BIT,
790 korg1212->statusRegPtr);
791}
792
793#if 0 /* not used */
794
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100795static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
796 enum MonitorModeSelector mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100798 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
799 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 switch (mode) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100802 case K1212_MONMODE_Off:
803 if (korg1212->cardState != K1212_STATE_MONITOR)
804 return 0;
805 else {
806 snd_korg1212_SendStopAndWait(korg1212);
807 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
808 }
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Takashi Iwai9fd91562005-11-17 10:45:48 +0100811 case K1212_MONMODE_On:
812 if (korg1212->cardState != K1212_STATE_OPEN)
813 return 0;
814 else {
815 int rc;
816 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
817 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
818 K1212_MODE_MonitorOn, 0, 0, 0);
819 if (rc != K1212_CMDRET_Success)
820 return 0;
821 }
822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Takashi Iwai9fd91562005-11-17 10:45:48 +0100824 default:
825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
828 return 1;
829}
830
831#endif /* not used */
832
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100833static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100835 if (korg1212->playback_pid != korg1212->capture_pid &&
836 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Takashi Iwai9fd91562005-11-17 10:45:48 +0100839 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100842static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100844 static enum ClockSourceIndex s44[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100845 K1212_CLKIDX_AdatAt44_1K,
846 K1212_CLKIDX_WordAt44_1K,
847 K1212_CLKIDX_LocalAt44_1K
848 };
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100849 static enum ClockSourceIndex s48[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100850 K1212_CLKIDX_AdatAt48K,
851 K1212_CLKIDX_WordAt48K,
852 K1212_CLKIDX_LocalAt48K
853 };
854 int parm, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Takashi Iwai9fd91562005-11-17 10:45:48 +0100856 if (!snd_korg1212_use_is_exclusive (korg1212))
857 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100859 switch (rate) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100860 case 44100:
861 parm = s44[korg1212->clkSource];
862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Takashi Iwai9fd91562005-11-17 10:45:48 +0100864 case 48000:
865 parm = s48[korg1212->clkSource];
866 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Takashi Iwai9fd91562005-11-17 10:45:48 +0100868 default:
869 return -EINVAL;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 korg1212->clkSrcRate = parm;
873 korg1212->clkRate = rate;
874
875 udelay(INTERCOMMAND_DELAY);
876 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
877 ClockSourceSelector[korg1212->clkSrcRate],
878 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100879 if (rc)
880 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
881 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 return 0;
884}
885
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100886static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888
Takashi Iwai9fd91562005-11-17 10:45:48 +0100889 if (source < 0 || source > 2)
890 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 korg1212->clkSource = source;
893
894 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
895
896 return 0;
897}
898
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100899static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 writel(0, korg1212->statusRegPtr);
902}
903
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100904static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100906 struct SensBits sensVals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int bitPosition;
908 int channel;
909 int clkIs48K;
910 int monModeSet;
911 u16 controlValue; // this keeps the current value to be written to
912 // the card's eeprom control register.
913 u16 count;
914 unsigned long flags;
915
Takashi Iwai9fd91562005-11-17 10:45:48 +0100916 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
917 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 // ----------------------------------------------------------------------------
920 // initialize things. The local init bit is always set when writing to the
921 // card's control register.
922 // ----------------------------------------------------------------------------
923 controlValue = 0;
924 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
925
926 // ----------------------------------------------------------------------------
927 // make sure the card is not in monitor mode when we do this update.
928 // ----------------------------------------------------------------------------
929 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
930 monModeSet = 1;
931 snd_korg1212_SendStopAndWait(korg1212);
932 } else
933 monModeSet = 0;
934
935 spin_lock_irqsave(&korg1212->lock, flags);
936
937 // ----------------------------------------------------------------------------
938 // we are about to send new values to the card, so clear the new values queued
939 // flag. Also, clear out mailbox 3, so we don't lockup.
940 // ----------------------------------------------------------------------------
941 writel(0, korg1212->mailbox3Ptr);
942 udelay(LOADSHIFT_DELAY);
943
944 // ----------------------------------------------------------------------------
945 // determine whether we are running a 48K or 44.1K clock. This info is used
946 // later when setting the SPDIF FF after the volume has been shifted in.
947 // ----------------------------------------------------------------------------
948 switch (korg1212->clkSrcRate) {
949 case K1212_CLKIDX_AdatAt44_1K:
950 case K1212_CLKIDX_WordAt44_1K:
951 case K1212_CLKIDX_LocalAt44_1K:
952 clkIs48K = 0;
953 break;
954
955 case K1212_CLKIDX_WordAt48K:
956 case K1212_CLKIDX_AdatAt48K:
957 case K1212_CLKIDX_LocalAt48K:
958 default:
959 clkIs48K = 1;
960 break;
961 }
962
963 // ----------------------------------------------------------------------------
964 // start the update. Setup the bit structure and then shift the bits.
965 // ----------------------------------------------------------------------------
966 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
967 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
968 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
969 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
970
971 // ----------------------------------------------------------------------------
972 // now start shifting the bits in. Start with the left channel then the right.
973 // ----------------------------------------------------------------------------
974 for (channel = 0; channel < 2; channel++) {
975
976 // ----------------------------------------------------------------------------
977 // Bring the load/shift line low, then wait - the spec says >150ns from load/
978 // shift low to the first rising edge of the clock.
979 // ----------------------------------------------------------------------------
980 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
981 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
982 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
983 udelay(LOADSHIFT_DELAY);
984
985 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
Takashi Iwai9fd91562005-11-17 10:45:48 +0100986 if (channel == 0) {
987 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100988 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
Takashi Iwai9fd91562005-11-17 10:45:48 +0100989 else
990 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
991 } else {
992 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
993 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
994 else
995 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
999 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1000 udelay(SENSCLKPULSE_WIDTH);
1001 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1002 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1003 udelay(SENSCLKPULSE_WIDTH);
1004 }
1005
1006 // ----------------------------------------------------------------------------
1007 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1008 // bit if the clock rate is 48K otherwise write 0.
1009 // ----------------------------------------------------------------------------
1010 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1011 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1012 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1013 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1014 udelay(SENSCLKPULSE_WIDTH);
1015
1016 if (clkIs48K)
1017 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1018
1019 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1020 udelay(ONE_RTC_TICK);
1021 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1022 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1023 udelay(SENSCLKPULSE_WIDTH);
1024 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1025 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1026 udelay(SENSCLKPULSE_WIDTH);
1027 }
1028
1029 // ----------------------------------------------------------------------------
1030 // The update is complete. Set a timeout. This is the inter-update delay.
1031 // Also, if the card was in monitor mode, restore it.
1032 // ----------------------------------------------------------------------------
1033 for (count = 0; count < 10; count++)
1034 udelay(SENSCLKPULSE_WIDTH);
1035
1036 if (monModeSet) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001037 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 K1212_MODE_MonitorOn, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001039 if (rc)
1040 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1041 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 spin_unlock_irqrestore(&korg1212->lock, flags);
1045
1046 return 1;
1047}
1048
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001049static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001051 int channel, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Takashi Iwai9fd91562005-11-17 10:45:48 +01001053 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1054 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 // ----------------------------------------------------
1057 // tell the card to boot
1058 // ----------------------------------------------------
1059 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1060
Takashi Iwai9fd91562005-11-17 10:45:48 +01001061 if (rc)
1062 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1063 rc, stateName[korg1212->cardState]);
1064 msleep(DSP_BOOT_DELAY_IN_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 // --------------------------------------------------------------------------------
1067 // Let the card know where all the buffers are.
1068 // --------------------------------------------------------------------------------
1069 rc = snd_korg1212_Send1212Command(korg1212,
1070 K1212_DB_ConfigureBufferMemory,
1071 LowerWordSwap(korg1212->PlayDataPhy),
1072 LowerWordSwap(korg1212->RecDataPhy),
1073 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1074 // is based on 2 buffers
1075 0
1076 );
1077
Takashi Iwai9fd91562005-11-17 10:45:48 +01001078 if (rc)
1079 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1080 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 udelay(INTERCOMMAND_DELAY);
1083
1084 rc = snd_korg1212_Send1212Command(korg1212,
1085 K1212_DB_ConfigureMiscMemory,
1086 LowerWordSwap(korg1212->VolumeTablePhy),
1087 LowerWordSwap(korg1212->RoutingTablePhy),
1088 LowerWordSwap(korg1212->AdatTimeCodePhy),
1089 0
1090 );
1091
Takashi Iwai9fd91562005-11-17 10:45:48 +01001092 if (rc)
1093 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1094 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 // --------------------------------------------------------------------------------
1097 // Initialize the routing and volume tables, then update the card's state.
1098 // --------------------------------------------------------------------------------
1099 udelay(INTERCOMMAND_DELAY);
1100
1101 for (channel = 0; channel < kAudioChannels; channel++) {
1102 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1103 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1104 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1105 }
1106
1107 snd_korg1212_WriteADCSensitivity(korg1212);
1108
1109 udelay(INTERCOMMAND_DELAY);
1110 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1111 ClockSourceSelector[korg1212->clkSrcRate],
1112 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001113 if (rc)
1114 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1115 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Takashi Iwai9fd91562005-11-17 10:45:48 +01001117 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1119
Takashi Iwai9fd91562005-11-17 10:45:48 +01001120 if (rc)
1121 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1122 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1125}
1126
David Howells7d12e782006-10-05 14:55:46 +01001127static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 u32 doorbellValue;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001130 struct snd_korg1212 *korg1212 = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 doorbellValue = readl(korg1212->inDoorbellPtr);
1133
1134 if (!doorbellValue)
1135 return IRQ_NONE;
1136
1137 spin_lock(&korg1212->lock);
1138
1139 writel(doorbellValue, korg1212->inDoorbellPtr);
1140
1141 korg1212->irqcount++;
1142
1143 korg1212->inIRQ++;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 switch (doorbellValue) {
1146 case K1212_DB_DSPDownloadDone:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001147 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1148 korg1212->irqcount, doorbellValue,
1149 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1151 korg1212->dsp_is_loaded = 1;
1152 wake_up(&korg1212->wait);
1153 }
1154 break;
1155
1156 // ------------------------------------------------------------------------
1157 // an error occurred - stop the card
1158 // ------------------------------------------------------------------------
1159 case K1212_DB_DMAERROR:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001160 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1161 korg1212->irqcount, doorbellValue,
1162 stateName[korg1212->cardState]);
1163 snd_printk(KERN_ERR "korg1212: DMA Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 korg1212->errorcnt++;
1165 korg1212->totalerrorcnt++;
1166 korg1212->sharedBufferPtr->cardCommand = 0;
1167 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1168 break;
1169
1170 // ------------------------------------------------------------------------
1171 // the card has stopped by our request. Clear the command word and signal
1172 // the semaphore in case someone is waiting for this.
1173 // ------------------------------------------------------------------------
1174 case K1212_DB_CARDSTOPPED:
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001175 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
Takashi Iwai9fd91562005-11-17 10:45:48 +01001176 korg1212->irqcount, doorbellValue,
1177 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 korg1212->sharedBufferPtr->cardCommand = 0;
1179 break;
1180
1181 default:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001182 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001183 korg1212->irqcount, doorbellValue,
1184 korg1212->currentBuffer, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1186 korg1212->currentBuffer++;
1187
1188 if (korg1212->currentBuffer >= kNumBuffers)
1189 korg1212->currentBuffer = 0;
1190
1191 if (!korg1212->running)
1192 break;
1193
1194 if (korg1212->capture_substream) {
1195 spin_unlock(&korg1212->lock);
1196 snd_pcm_period_elapsed(korg1212->capture_substream);
1197 spin_lock(&korg1212->lock);
1198 }
1199
1200 if (korg1212->playback_substream) {
1201 spin_unlock(&korg1212->lock);
1202 snd_pcm_period_elapsed(korg1212->playback_substream);
1203 spin_lock(&korg1212->lock);
1204 }
1205 }
1206 break;
1207 }
1208
1209 korg1212->inIRQ--;
1210
1211 spin_unlock(&korg1212->lock);
1212
1213 return IRQ_HANDLED;
1214}
1215
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001216static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001218 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Takashi Iwai9fd91562005-11-17 10:45:48 +01001220 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1221 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 // ---------------------------------------------------------------
1224 // verify the state of the card before proceeding.
1225 // ---------------------------------------------------------------
Takashi Iwai9fd91562005-11-17 10:45:48 +01001226 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1232 UpperWordSwap(korg1212->dma_dsp.addr),
1233 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001234 if (rc)
1235 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1236 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 korg1212->dsp_is_loaded = 0;
1239 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1240 if (! korg1212->dsp_is_loaded )
1241 return -EBUSY; /* timeout */
1242
1243 snd_korg1212_OnDSPDownloadComplete(korg1212);
1244
1245 return 0;
1246}
1247
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001248static struct snd_pcm_hardware snd_korg1212_playback_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 .info = (SNDRV_PCM_INFO_MMAP |
1251 SNDRV_PCM_INFO_MMAP_VALID |
1252 SNDRV_PCM_INFO_INTERLEAVED),
1253 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1254 .rates = (SNDRV_PCM_RATE_44100 |
1255 SNDRV_PCM_RATE_48000),
1256 .rate_min = 44100,
1257 .rate_max = 48000,
1258 .channels_min = K1212_MIN_CHANNELS,
1259 .channels_max = K1212_MAX_CHANNELS,
1260 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1261 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1262 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1263 .periods_min = K1212_PERIODS,
1264 .periods_max = K1212_PERIODS,
1265 .fifo_size = 0,
1266};
1267
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001268static struct snd_pcm_hardware snd_korg1212_capture_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 .info = (SNDRV_PCM_INFO_MMAP |
1271 SNDRV_PCM_INFO_MMAP_VALID |
1272 SNDRV_PCM_INFO_INTERLEAVED),
1273 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1274 .rates = (SNDRV_PCM_RATE_44100 |
1275 SNDRV_PCM_RATE_48000),
1276 .rate_min = 44100,
1277 .rate_max = 48000,
1278 .channels_min = K1212_MIN_CHANNELS,
1279 .channels_max = K1212_MAX_CHANNELS,
1280 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1281 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1282 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1283 .periods_min = K1212_PERIODS,
1284 .periods_max = K1212_PERIODS,
1285 .fifo_size = 0,
1286};
1287
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001288static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001290 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int i;
1292
Takashi Iwai9fd91562005-11-17 10:45:48 +01001293 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1294 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1296
1297 for (i=0; i < count; i++) {
1298#if K1212_DEBUG_LEVEL > 0
1299 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1300 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001301 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1302 dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EFAULT;
1304 }
1305#endif
1306 memset((void*) dst + offset, 0, size);
1307 dst++;
1308 }
1309
1310 return 0;
1311}
1312
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001313static 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 -07001314{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001315 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int i, rc;
1317
Takashi Iwai9fd91562005-11-17 10:45:48 +01001318 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1319 pos, offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1321
1322 for (i=0; i < count; i++) {
1323#if K1212_DEBUG_LEVEL > 0
1324 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1325 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001326 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 -07001327 return -EFAULT;
1328 }
1329#endif
1330 rc = copy_to_user(dst + offset, src, size);
1331 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 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 -07001333 return -EFAULT;
1334 }
1335 src++;
1336 dst += size;
1337 }
1338
1339 return 0;
1340}
1341
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001342static 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 -07001343{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001344 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int i, rc;
1346
Takashi Iwai9fd91562005-11-17 10:45:48 +01001347 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1348 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1351
1352 for (i=0; i < count; i++) {
1353#if K1212_DEBUG_LEVEL > 0
1354 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1355 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001356 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 -07001357 return -EFAULT;
1358 }
1359#endif
1360 rc = copy_from_user((void*) dst + offset, src, size);
1361 if (rc) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001362 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 -07001363 return -EFAULT;
1364 }
1365 dst++;
1366 src += size;
1367 }
1368
1369 return 0;
1370}
1371
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001372static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001374 struct snd_korg1212 *korg1212 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Takashi Iwai9fd91562005-11-17 10:45:48 +01001376 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1377 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 korg1212->pcm = NULL;
1380}
1381
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001382static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001385 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1386 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Takashi Iwai9fd91562005-11-17 10:45:48 +01001388 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1389 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 snd_pcm_set_sync(substream); // ???
1392
1393 snd_korg1212_OpenCard(korg1212);
1394
1395 runtime->hw = snd_korg1212_playback_info;
1396 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1397
1398 spin_lock_irqsave(&korg1212->lock, flags);
1399
1400 korg1212->playback_substream = substream;
1401 korg1212->playback_pid = current->pid;
1402 korg1212->periodsize = K1212_PERIODS;
1403 korg1212->channels = K1212_CHANNELS;
1404 korg1212->errorcnt = 0;
1405
1406 spin_unlock_irqrestore(&korg1212->lock, flags);
1407
1408 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1409 return 0;
1410}
1411
1412
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001413static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001416 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1417 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Takashi Iwai9fd91562005-11-17 10:45:48 +01001419 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1420 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 snd_pcm_set_sync(substream);
1423
1424 snd_korg1212_OpenCard(korg1212);
1425
1426 runtime->hw = snd_korg1212_capture_info;
1427 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1428
1429 spin_lock_irqsave(&korg1212->lock, flags);
1430
1431 korg1212->capture_substream = substream;
1432 korg1212->capture_pid = current->pid;
1433 korg1212->periodsize = K1212_PERIODS;
1434 korg1212->channels = K1212_CHANNELS;
1435
1436 spin_unlock_irqrestore(&korg1212->lock, flags);
1437
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001438 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1439 kPlayBufferFrames, kPlayBufferFrames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
1441}
1442
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001443static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001446 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Takashi Iwai9fd91562005-11-17 10:45:48 +01001448 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1449 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1452
1453 spin_lock_irqsave(&korg1212->lock, flags);
1454
1455 korg1212->playback_pid = -1;
1456 korg1212->playback_substream = NULL;
1457 korg1212->periodsize = 0;
1458
1459 spin_unlock_irqrestore(&korg1212->lock, flags);
1460
1461 snd_korg1212_CloseCard(korg1212);
1462 return 0;
1463}
1464
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001465static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001468 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Takashi Iwai9fd91562005-11-17 10:45:48 +01001470 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1471 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 spin_lock_irqsave(&korg1212->lock, flags);
1474
1475 korg1212->capture_pid = -1;
1476 korg1212->capture_substream = NULL;
1477 korg1212->periodsize = 0;
1478
1479 spin_unlock_irqrestore(&korg1212->lock, flags);
1480
1481 snd_korg1212_CloseCard(korg1212);
1482 return 0;
1483}
1484
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001485static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 unsigned int cmd, void *arg)
1487{
Takashi Iwai9fd91562005-11-17 10:45:48 +01001488 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001491 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 info->offset = 0;
1493 info->first = info->channel * 16;
1494 info->step = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 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 -07001496 return 0;
1497 }
1498
1499 return snd_pcm_lib_ioctl(substream, cmd, arg);
1500}
1501
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001502static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1503 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001506 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int err;
1508 pid_t this_pid;
1509 pid_t other_pid;
1510
Takashi Iwai9fd91562005-11-17 10:45:48 +01001511 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1512 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 spin_lock_irqsave(&korg1212->lock, flags);
1515
1516 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1517 this_pid = korg1212->playback_pid;
1518 other_pid = korg1212->capture_pid;
1519 } else {
1520 this_pid = korg1212->capture_pid;
1521 other_pid = korg1212->playback_pid;
1522 }
1523
1524 if ((other_pid > 0) && (this_pid != other_pid)) {
1525
1526 /* The other stream is open, and not by the same
1527 task as this one. Make sure that the parameters
1528 that matter are the same.
1529 */
1530
1531 if ((int)params_rate(params) != korg1212->clkRate) {
1532 spin_unlock_irqrestore(&korg1212->lock, flags);
1533 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1534 return -EBUSY;
1535 }
1536
1537 spin_unlock_irqrestore(&korg1212->lock, flags);
1538 return 0;
1539 }
1540
1541 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1542 spin_unlock_irqrestore(&korg1212->lock, flags);
1543 return err;
1544 }
1545
1546 korg1212->channels = params_channels(params);
1547 korg1212->periodsize = K1212_PERIOD_BYTES;
1548
1549 spin_unlock_irqrestore(&korg1212->lock, flags);
1550
1551 return 0;
1552}
1553
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001554static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001556 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 int rc;
1558
Takashi Iwai9fd91562005-11-17 10:45:48 +01001559 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1560 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 spin_lock_irq(&korg1212->lock);
1563
1564 /* FIXME: we should wait for ack! */
1565 if (korg1212->stop_pending_cnt > 0) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001566 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1567 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 spin_unlock_irq(&korg1212->lock);
1569 return -EAGAIN;
1570 /*
1571 korg1212->sharedBufferPtr->cardCommand = 0;
1572 del_timer(&korg1212->timer);
1573 korg1212->stop_pending_cnt = 0;
1574 */
1575 }
1576
1577 rc = snd_korg1212_SetupForPlay(korg1212);
1578
1579 korg1212->currentBuffer = 0;
1580
1581 spin_unlock_irq(&korg1212->lock);
1582
1583 return rc ? -EINVAL : 0;
1584}
1585
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001586static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int cmd)
1588{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001589 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int rc;
1591
Takashi Iwai9fd91562005-11-17 10:45:48 +01001592 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1593 stateName[korg1212->cardState], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 spin_lock(&korg1212->lock);
1596 switch (cmd) {
1597 case SNDRV_PCM_TRIGGER_START:
1598/*
1599 if (korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001600 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 }
1603*/
1604 korg1212->running++;
1605 rc = snd_korg1212_TriggerPlay(korg1212);
1606 break;
1607
1608 case SNDRV_PCM_TRIGGER_STOP:
1609/*
1610 if (!korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001611 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 break;
1613 }
1614*/
1615 korg1212->running--;
1616 rc = snd_korg1212_StopPlay(korg1212);
1617 break;
1618
1619 default:
1620 rc = 1;
1621 break;
1622 }
1623 spin_unlock(&korg1212->lock);
1624 return rc ? -EINVAL : 0;
1625}
1626
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001627static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001629 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 snd_pcm_uframes_t pos;
1631
1632 pos = korg1212->currentBuffer * kPlayBufferFrames;
1633
Takashi Iwai9fd91562005-11-17 10:45:48 +01001634 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1635 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 return pos;
1638}
1639
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001640static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001642 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 snd_pcm_uframes_t pos;
1644
1645 pos = korg1212->currentBuffer * kPlayBufferFrames;
1646
Takashi Iwai9fd91562005-11-17 10:45:48 +01001647 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1648 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 return pos;
1651}
1652
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001653static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int channel, /* not used (interleaved data) */
1655 snd_pcm_uframes_t pos,
1656 void __user *src,
1657 snd_pcm_uframes_t count)
1658{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001659 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Takashi Iwai9fd91562005-11-17 10:45:48 +01001661 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1662 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1665
1666}
1667
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001668static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int channel, /* not used (interleaved data) */
1670 snd_pcm_uframes_t pos,
1671 snd_pcm_uframes_t count)
1672{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001673 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Takashi Iwai9fd91562005-11-17 10:45:48 +01001675 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1676 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1679}
1680
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001681static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int channel, /* not used (interleaved data) */
1683 snd_pcm_uframes_t pos,
1684 void __user *dst,
1685 snd_pcm_uframes_t count)
1686{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001687 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Takashi Iwai9fd91562005-11-17 10:45:48 +01001689 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1690 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1693}
1694
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001695static struct snd_pcm_ops snd_korg1212_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .open = snd_korg1212_playback_open,
1697 .close = snd_korg1212_playback_close,
1698 .ioctl = snd_korg1212_ioctl,
1699 .hw_params = snd_korg1212_hw_params,
1700 .prepare = snd_korg1212_prepare,
1701 .trigger = snd_korg1212_trigger,
1702 .pointer = snd_korg1212_playback_pointer,
1703 .copy = snd_korg1212_playback_copy,
1704 .silence = snd_korg1212_playback_silence,
1705};
1706
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001707static struct snd_pcm_ops snd_korg1212_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 .open = snd_korg1212_capture_open,
1709 .close = snd_korg1212_capture_close,
1710 .ioctl = snd_korg1212_ioctl,
1711 .hw_params = snd_korg1212_hw_params,
1712 .prepare = snd_korg1212_prepare,
1713 .trigger = snd_korg1212_trigger,
1714 .pointer = snd_korg1212_capture_pointer,
1715 .copy = snd_korg1212_capture_copy,
1716};
1717
1718/*
1719 * Control Interface
1720 */
1721
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001722static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1723 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1726 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1727 return 0;
1728}
1729
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001730static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001733 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int i = kcontrol->private_value;
1735
1736 spin_lock_irq(&korg1212->lock);
1737
1738 u->value.integer.value[0] = korg1212->volumePhase[i];
1739
1740 if (i >= 8)
1741 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1742
1743 spin_unlock_irq(&korg1212->lock);
1744
1745 return 0;
1746}
1747
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001748static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1749 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001751 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 int change = 0;
1753 int i, val;
1754
1755 spin_lock_irq(&korg1212->lock);
1756
1757 i = kcontrol->private_value;
1758
1759 korg1212->volumePhase[i] = u->value.integer.value[0];
1760
1761 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1762
1763 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1764 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1765 korg1212->sharedBufferPtr->volumeData[i] = val;
1766 change = 1;
1767 }
1768
1769 if (i >= 8) {
1770 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1771
1772 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1773
1774 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1775 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1776 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1777 change = 1;
1778 }
1779 }
1780
1781 spin_unlock_irq(&korg1212->lock);
1782
1783 return change;
1784}
1785
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001786static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1787 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1790 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1791 uinfo->value.integer.min = k1212MinVolume;
1792 uinfo->value.integer.max = k1212MaxVolume;
1793 return 0;
1794}
1795
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001796static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001799 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 int i;
1801
1802 spin_lock_irq(&korg1212->lock);
1803
1804 i = kcontrol->private_value;
1805 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1806
1807 if (i >= 8)
1808 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1809
1810 spin_unlock_irq(&korg1212->lock);
1811
1812 return 0;
1813}
1814
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001815static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001818 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int change = 0;
1820 int i;
1821 int val;
1822
1823 spin_lock_irq(&korg1212->lock);
1824
1825 i = kcontrol->private_value;
1826
1827 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1828 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1829 val *= u->value.integer.value[0];
1830 korg1212->sharedBufferPtr->volumeData[i] = val;
1831 change = 1;
1832 }
1833
1834 if (i >= 8) {
1835 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1836 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1837 val *= u->value.integer.value[1];
1838 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1839 change = 1;
1840 }
1841 }
1842
1843 spin_unlock_irq(&korg1212->lock);
1844
1845 return change;
1846}
1847
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001848static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1849 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1852 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1853 uinfo->value.enumerated.items = kAudioChannels;
1854 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1855 uinfo->value.enumerated.item = kAudioChannels-1;
1856 }
1857 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1858 return 0;
1859}
1860
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001861static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1862 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001864 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 int i;
1866
1867 spin_lock_irq(&korg1212->lock);
1868
1869 i = kcontrol->private_value;
1870 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1871
1872 if (i >= 8)
1873 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1874
1875 spin_unlock_irq(&korg1212->lock);
1876
1877 return 0;
1878}
1879
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001880static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1881 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001883 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 int change = 0, i;
1885
1886 spin_lock_irq(&korg1212->lock);
1887
1888 i = kcontrol->private_value;
1889
1890 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1891 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1892 change = 1;
1893 }
1894
1895 if (i >= 8) {
1896 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1897 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1898 change = 1;
1899 }
1900 }
1901
1902 spin_unlock_irq(&korg1212->lock);
1903
1904 return change;
1905}
1906
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001907static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1908 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1911 uinfo->count = 2;
1912 uinfo->value.integer.min = k1212MaxADCSens;
1913 uinfo->value.integer.max = k1212MinADCSens;
1914 return 0;
1915}
1916
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001917static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001920 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 spin_lock_irq(&korg1212->lock);
1923
1924 u->value.integer.value[0] = korg1212->leftADCInSens;
1925 u->value.integer.value[1] = korg1212->rightADCInSens;
1926
1927 spin_unlock_irq(&korg1212->lock);
1928
1929 return 0;
1930}
1931
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001932static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1933 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001935 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int change = 0;
1937
1938 spin_lock_irq(&korg1212->lock);
1939
1940 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1941 korg1212->leftADCInSens = u->value.integer.value[0];
1942 change = 1;
1943 }
1944 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1945 korg1212->rightADCInSens = u->value.integer.value[1];
1946 change = 1;
1947 }
1948
1949 spin_unlock_irq(&korg1212->lock);
1950
1951 if (change)
1952 snd_korg1212_WriteADCSensitivity(korg1212);
1953
1954 return change;
1955}
1956
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001957static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1958 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1961 uinfo->count = 1;
1962 uinfo->value.enumerated.items = 3;
1963 if (uinfo->value.enumerated.item > 2) {
1964 uinfo->value.enumerated.item = 2;
1965 }
1966 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1967 return 0;
1968}
1969
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001970static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1971 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001973 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 spin_lock_irq(&korg1212->lock);
1976
1977 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1978
1979 spin_unlock_irq(&korg1212->lock);
1980 return 0;
1981}
1982
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001983static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001986 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 unsigned int val;
1988 int change;
1989
1990 val = ucontrol->value.enumerated.item[0] % 3;
1991 spin_lock_irq(&korg1212->lock);
1992 change = val != korg1212->clkSource;
1993 snd_korg1212_SetClockSource(korg1212, val);
1994 spin_unlock_irq(&korg1212->lock);
1995 return change;
1996}
1997
1998#define MON_MIXER(ord,c_name) \
1999 { \
2000 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2001 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2002 .name = c_name " Monitor Volume", \
2003 .info = snd_korg1212_control_volume_info, \
2004 .get = snd_korg1212_control_volume_get, \
2005 .put = snd_korg1212_control_volume_put, \
2006 .private_value = ord, \
2007 }, \
2008 { \
2009 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2011 .name = c_name " Monitor Route", \
2012 .info = snd_korg1212_control_route_info, \
2013 .get = snd_korg1212_control_route_get, \
2014 .put = snd_korg1212_control_route_put, \
2015 .private_value = ord, \
2016 }, \
2017 { \
2018 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002019 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 .name = c_name " Monitor Phase Invert", \
2021 .info = snd_korg1212_control_phase_info, \
2022 .get = snd_korg1212_control_phase_get, \
2023 .put = snd_korg1212_control_phase_put, \
2024 .private_value = ord, \
2025 }
2026
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002027static struct snd_kcontrol_new snd_korg1212_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 MON_MIXER(8, "Analog"),
2029 MON_MIXER(10, "SPDIF"),
2030 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2031 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2032 {
2033 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002034 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 .name = "Sync Source",
2036 .info = snd_korg1212_control_sync_info,
2037 .get = snd_korg1212_control_sync_get,
2038 .put = snd_korg1212_control_sync_put,
2039 },
2040 {
2041 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2042 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2043 .name = "ADC Attenuation",
2044 .info = snd_korg1212_control_info,
2045 .get = snd_korg1212_control_get,
2046 .put = snd_korg1212_control_put,
2047 }
2048};
2049
2050/*
2051 * proc interface
2052 */
2053
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002054static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2055 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 int n;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002058 struct snd_korg1212 *korg1212 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 snd_iprintf(buffer, korg1212->card->longname);
2061 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2062 snd_iprintf(buffer, "\nGeneral settings\n");
2063 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2064 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2065 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2066 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2067 snd_iprintf(buffer, " Volume Info:\n");
2068 for (n=0; n<kAudioChannels; n++)
2069 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2070 channelName[n],
2071 channelName[korg1212->sharedBufferPtr->routeData[n]],
2072 korg1212->sharedBufferPtr->volumeData[n]);
2073 snd_iprintf(buffer, "\nGeneral status\n");
2074 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2075 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2076 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2077 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2078 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2079 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2080}
2081
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002082static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002084 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002087 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090static int
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002091snd_korg1212_free(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 snd_korg1212_TurnOffIdleMonitor(korg1212);
2094
2095 if (korg1212->irq >= 0) {
2096 synchronize_irq(korg1212->irq);
2097 snd_korg1212_DisableCardInterrupts(korg1212);
Takashi Iwai9fd91562005-11-17 10:45:48 +01002098 free_irq(korg1212->irq, korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 korg1212->irq = -1;
2100 }
2101
2102 if (korg1212->iobase != NULL) {
2103 iounmap(korg1212->iobase);
2104 korg1212->iobase = NULL;
2105 }
2106
2107 pci_release_regions(korg1212->pci);
2108
2109 // ----------------------------------------------------
2110 // free up memory resources used for the DSP download.
2111 // ----------------------------------------------------
2112 if (korg1212->dma_dsp.area) {
2113 snd_dma_free_pages(&korg1212->dma_dsp);
2114 korg1212->dma_dsp.area = NULL;
2115 }
2116
2117#ifndef K1212_LARGEALLOC
2118
2119 // ------------------------------------------------------
2120 // free up memory resources used for the Play/Rec Buffers
2121 // ------------------------------------------------------
2122 if (korg1212->dma_play.area) {
2123 snd_dma_free_pages(&korg1212->dma_play);
2124 korg1212->dma_play.area = NULL;
2125 }
2126
2127 if (korg1212->dma_rec.area) {
2128 snd_dma_free_pages(&korg1212->dma_rec);
2129 korg1212->dma_rec.area = NULL;
2130 }
2131
2132#endif
2133
2134 // ----------------------------------------------------
2135 // free up memory resources used for the Shared Buffers
2136 // ----------------------------------------------------
2137 if (korg1212->dma_shared.area) {
2138 snd_dma_free_pages(&korg1212->dma_shared);
2139 korg1212->dma_shared.area = NULL;
2140 }
2141
2142 pci_disable_device(korg1212->pci);
2143 kfree(korg1212);
2144 return 0;
2145}
2146
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002147static int snd_korg1212_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002149 struct snd_korg1212 *korg1212 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return snd_korg1212_free(korg1212);
2152}
2153
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002154static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2155 struct snd_korg1212 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157{
Takashi Iwai9fd91562005-11-17 10:45:48 +01002158 int err, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 unsigned int i;
2160 unsigned ioport_size, iomem_size, iomem2_size;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002161 struct snd_korg1212 * korg1212;
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002162 const struct firmware *dsp_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002164 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .dev_free = snd_korg1212_dev_free,
2166 };
2167
2168 * rchip = NULL;
2169 if ((err = pci_enable_device(pci)) < 0)
2170 return err;
2171
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002172 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (korg1212 == NULL) {
2174 pci_disable_device(pci);
2175 return -ENOMEM;
2176 }
2177
2178 korg1212->card = card;
2179 korg1212->pci = pci;
2180
2181 init_waitqueue_head(&korg1212->wait);
2182 spin_lock_init(&korg1212->lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002183 mutex_init(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 init_timer(&korg1212->timer);
2185 korg1212->timer.function = snd_korg1212_timer_func;
2186 korg1212->timer.data = (unsigned long)korg1212;
2187
2188 korg1212->irq = -1;
2189 korg1212->clkSource = K1212_CLKIDX_Local;
2190 korg1212->clkRate = 44100;
2191 korg1212->inIRQ = 0;
2192 korg1212->running = 0;
2193 korg1212->opencnt = 0;
2194 korg1212->playcnt = 0;
2195 korg1212->setcnt = 0;
2196 korg1212->totalerrorcnt = 0;
2197 korg1212->playback_pid = -1;
2198 korg1212->capture_pid = -1;
2199 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2200 korg1212->idleMonitorOn = 0;
2201 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2202 korg1212->leftADCInSens = k1212MaxADCSens;
2203 korg1212->rightADCInSens = k1212MaxADCSens;
2204
2205 for (i=0; i<kAudioChannels; i++)
2206 korg1212->volumePhase[i] = 0;
2207
2208 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2209 kfree(korg1212);
2210 pci_disable_device(pci);
2211 return err;
2212 }
2213
2214 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2215 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2216 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2217
2218 iomem_size = pci_resource_len(korg1212->pci, 0);
2219 ioport_size = pci_resource_len(korg1212->pci, 1);
2220 iomem2_size = pci_resource_len(korg1212->pci, 2);
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2223 " iomem = 0x%lx (%d)\n"
2224 " ioport = 0x%lx (%d)\n"
2225 " iomem = 0x%lx (%d)\n"
2226 " [%s]\n",
2227 korg1212->iomem, iomem_size,
2228 korg1212->ioport, ioport_size,
2229 korg1212->iomem2, iomem2_size,
2230 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2233 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2234 korg1212->iomem + iomem_size - 1);
2235 snd_korg1212_free(korg1212);
2236 return -EBUSY;
2237 }
2238
2239 err = request_irq(pci->irq, snd_korg1212_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002240 IRQF_SHARED,
Takashi Iwai9fd91562005-11-17 10:45:48 +01002241 "korg1212", korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 if (err) {
2244 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2245 snd_korg1212_free(korg1212);
2246 return -EBUSY;
2247 }
2248
2249 korg1212->irq = pci->irq;
2250
2251 pci_set_master(korg1212->pci);
2252
2253 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2254 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2255 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2256 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2257 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2258 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2259 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2260 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2261 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2262 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2265 " Status register = 0x%p\n"
2266 " OutDoorbell = 0x%p\n"
2267 " InDoorbell = 0x%p\n"
2268 " Mailbox0 = 0x%p\n"
2269 " Mailbox1 = 0x%p\n"
2270 " Mailbox2 = 0x%p\n"
2271 " Mailbox3 = 0x%p\n"
2272 " ControlReg = 0x%p\n"
2273 " SensReg = 0x%p\n"
2274 " IDReg = 0x%p\n"
2275 " [%s]\n",
2276 korg1212->statusRegPtr,
2277 korg1212->outDoorbellPtr,
2278 korg1212->inDoorbellPtr,
2279 korg1212->mailbox0Ptr,
2280 korg1212->mailbox1Ptr,
2281 korg1212->mailbox2Ptr,
2282 korg1212->mailbox3Ptr,
2283 korg1212->controlRegPtr,
2284 korg1212->sensRegPtr,
2285 korg1212->idRegPtr,
2286 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002289 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2290 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 snd_korg1212_free(korg1212);
2292 return -ENOMEM;
2293 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002294 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2296
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002297 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 -07002298
2299#ifndef K1212_LARGEALLOC
2300
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002301 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2304 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2305 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2306 snd_korg1212_free(korg1212);
2307 return -ENOMEM;
2308 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002309 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2313 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2316 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2317 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2318 snd_korg1212_free(korg1212);
2319 return -ENOMEM;
2320 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002321 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2325 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327#else // K1212_LARGEALLOC
2328
2329 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2330 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002331 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2332 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334#endif // K1212_LARGEALLOC
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002337 offsetof(struct KorgSharedBuffer, volumeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002339 offsetof(struct KorgSharedBuffer, routeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002341 offsetof(struct KorgSharedBuffer, AdatTimeCode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002343#ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2344 dsp_code = &static_dsp_code;
2345#else
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002346 err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
2347 if (err < 0) {
2348 release_firmware(dsp_code);
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002349 snd_printk(KERN_ERR "firmware not available\n");
2350 snd_korg1212_free(korg1212);
2351 return err;
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002352 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002353#endif
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002356 dsp_code->size, &korg1212->dma_dsp) < 0) {
Randy Dunlap9fb62c92006-11-21 19:01:51 +01002357 snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 snd_korg1212_free(korg1212);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002359#ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2360 release_firmware(dsp_code);
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return -ENOMEM;
2363 }
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002366 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002369 memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
2370
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002371#ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
2372 release_firmware(dsp_code);
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002373#endif
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2376
Takashi Iwai9fd91562005-11-17 10:45:48 +01002377 if (rc)
2378 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2381 snd_korg1212_free(korg1212);
2382 return err;
2383 }
2384
2385 snd_korg1212_EnableCardInterrupts(korg1212);
2386
2387 mdelay(CARD_BOOT_DELAY_IN_MS);
2388
2389 if (snd_korg1212_downloadDSPCode(korg1212))
2390 return -EBUSY;
2391
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002392 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 "PlayDataPhy = %08x L[%08x]\n"
2394 "korg1212: RecDataPhy = %08x L[%08x], "
2395 "VolumeTablePhy = %08x L[%08x]\n"
2396 "korg1212: RoutingTablePhy = %08x L[%08x], "
2397 "AdatTimeCodePhy = %08x L[%08x]\n",
2398 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2399 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2400 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2401 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2402 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2403 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2404
2405 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2406 return err;
2407
2408 korg1212->pcm->private_data = korg1212;
2409 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2410 strcpy(korg1212->pcm->name, "korg1212");
2411
2412 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2413
2414 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2415
2416 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2419 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2420 if (err < 0)
2421 return err;
2422 }
2423
2424 snd_korg1212_proc_init(korg1212);
2425
2426 snd_card_set_dev(card, &pci->dev);
2427
2428 * rchip = korg1212;
2429 return 0;
2430
2431}
2432
2433/*
2434 * Card initialisation
2435 */
2436
2437static int __devinit
2438snd_korg1212_probe(struct pci_dev *pci,
2439 const struct pci_device_id *pci_id)
2440{
2441 static int dev;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002442 struct snd_korg1212 *korg1212;
2443 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int err;
2445
2446 if (dev >= SNDRV_CARDS) {
2447 return -ENODEV;
2448 }
2449 if (!enable[dev]) {
2450 dev++;
2451 return -ENOENT;
2452 }
2453 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2454 if (card == NULL)
2455 return -ENOMEM;
2456
2457 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2458 snd_card_free(card);
2459 return err;
2460 }
2461
2462 strcpy(card->driver, "korg1212");
2463 strcpy(card->shortname, "korg1212");
2464 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2465 korg1212->iomem, korg1212->irq);
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if ((err = snd_card_register(card)) < 0) {
2470 snd_card_free(card);
2471 return err;
2472 }
2473 pci_set_drvdata(pci, card);
2474 dev++;
2475 return 0;
2476}
2477
2478static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2479{
2480 snd_card_free(pci_get_drvdata(pci));
2481 pci_set_drvdata(pci, NULL);
2482}
2483
2484static struct pci_driver driver = {
2485 .name = "korg1212",
2486 .id_table = snd_korg1212_ids,
2487 .probe = snd_korg1212_probe,
2488 .remove = __devexit_p(snd_korg1212_remove),
2489};
2490
2491static int __init alsa_card_korg1212_init(void)
2492{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002493 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
2496static void __exit alsa_card_korg1212_exit(void)
2497{
2498 pci_unregister_driver(&driver);
2499}
2500
2501module_init(alsa_card_korg1212_init)
2502module_exit(alsa_card_korg1212_exit)