blob: 8b79969034be24853b57afda72a88566ed316d10 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/slab.h>
27#include <linux/wait.h>
28#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010029#include <linux/mutex.h>
Clemens Ladisch2493a6d2006-11-06 09:24:29 +010030#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <sound/core.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/initval.h>
38
39#include <asm/io.h>
40
41// ----------------------------------------------------------------------------
42// Debug Stuff
43// ----------------------------------------------------------------------------
44#define K1212_DEBUG_LEVEL 0
Takashi Iwai9fd91562005-11-17 10:45:48 +010045#if K1212_DEBUG_LEVEL > 0
46#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
47#else
48#define K1212_DEBUG_PRINTK(fmt,...)
49#endif
50#if K1212_DEBUG_LEVEL > 1
51#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
52#else
53#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56// ----------------------------------------------------------------------------
57// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
58// buffers are alocated as a large piece inside KorgSharedBuffer.
59// ----------------------------------------------------------------------------
60//#define K1212_LARGEALLOC 1
61
62// ----------------------------------------------------------------------------
63// Valid states of the Korg 1212 I/O card.
64// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010065enum CardState {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 K1212_STATE_NONEXISTENT, // there is no card here
67 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
68 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
69 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
70 K1212_STATE_READY, // the card can be opened by an application. Any application
71 // requests prior to this state should fail. Only an open
72 // request can be made at this state.
73 K1212_STATE_OPEN, // an application has opened the card
74 K1212_STATE_SETUP, // the card has been setup for play
75 K1212_STATE_PLAYING, // the card is playing
76 K1212_STATE_MONITOR, // the card is in the monitor mode
77 K1212_STATE_CALIBRATING, // the card is currently calibrating
78 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
79 // are in the process of cleaning things up.
80 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
Takashi Iwaifcfd3332005-11-17 15:00:46 +010081};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83// ----------------------------------------------------------------------------
84// The following enumeration defines the constants written to the card's
85// host-to-card doorbell to initiate a command.
86// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010087enum korg1212_dbcnst {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
89 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
90 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
91 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
92 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
93 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
94 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
95 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
96 // timecode value.
97 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
98 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
99 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
100 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
101 // on page 4 (local page to card).
102 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
103 // completed.
104 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107
108// ----------------------------------------------------------------------------
109// The following enumeration defines return codes
110// to the Korg 1212 I/O driver.
111// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100112enum snd_korg1212rc {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 K1212_CMDRET_Success = 0, // command was successfully placed
114 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
115 K1212_CMDRET_PMFailure, // the protected mode call failed
116 K1212_CMDRET_FailUnspecified, // unspecified failure
117 K1212_CMDRET_FailBadState, // the specified command can not be given in
118 // the card's current state. (or the wave device's
119 // state)
120 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
121 K1212_CMDRET_BadIndex, // an out of range card index was specified
122 K1212_CMDRET_BadHandle, // an invalid card handle was specified
123 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
124 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
125 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
126 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
127
128 K1212_CMDRET_BadDevice, // the specified wave device was out of range
129 K1212_CMDRET_BadFormat // the specified wave format is unsupported
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132// ----------------------------------------------------------------------------
133// The following enumeration defines the constants used to select the play
134// mode for the card in the SelectPlayMode command.
135// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100136enum PlayModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
138 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
139 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
140 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143// ----------------------------------------------------------------------------
144// The following enumeration defines the constants used to select the monitor
145// mode for the card in the SetMonitorMode command.
146// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100147enum MonitorModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
149 K1212_MONMODE_On // tells card to turn on monitor mode
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100150};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
153#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
154#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
155#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
156#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
157#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
158#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
159#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
160 // register
161#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
162 // this is the upper word of the PCI control reg.
163#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
166 // to send a command before giving up.
167#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
168 // the card.
169#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
170
171#define CARD_BOOT_DELAY_IN_MS 10
172#define CARD_BOOT_TIMEOUT 10
173#define DSP_BOOT_DELAY_IN_MS 200
174
175#define kNumBuffers 8
176#define k1212MaxCards 4
177#define k1212NumWaveDevices 6
178#define k16BitChannels 10
179#define k32BitChannels 2
180#define kAudioChannels (k16BitChannels + k32BitChannels)
181#define kPlayBufferFrames 1024
182
183#define K1212_ANALOG_CHANNELS 2
184#define K1212_SPDIF_CHANNELS 2
185#define K1212_ADAT_CHANNELS 8
186#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
187#define K1212_MIN_CHANNELS 1
188#define K1212_MAX_CHANNELS K1212_CHANNELS
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100189#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
191#define K1212_PERIODS (kNumBuffers)
192#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
193#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
194#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
195#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
196#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
197#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
198
199#define k1212MinADCSens 0x7f
200#define k1212MaxADCSens 0x00
201#define k1212MaxVolume 0x7fff
202#define k1212MaxWaveVolume 0xffff
203#define k1212MinVolume 0x0000
204#define k1212MaxVolInverted 0x8000
205
206// -----------------------------------------------------------------
207// the following bits are used for controlling interrupts in the
208// interrupt control/status reg
209// -----------------------------------------------------------------
210#define PCI_INT_ENABLE_BIT 0x00000100
211#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
212#define LOCAL_INT_ENABLE_BIT 0x00010000
213#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
214#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
215
216// -----------------------------------------------------------------
217// the following bits are defined for the PCI command register
218// -----------------------------------------------------------------
219#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
220#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
221#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
222
223// -----------------------------------------------------------------
224// the following bits are defined for the PCI status register
225// -----------------------------------------------------------------
226#define PCI_STAT_PARITY_ERROR_BIT 0x8000
227#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
228#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
229#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
230#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
231
232// ------------------------------------------------------------------------
233// the following constants are used in setting the 1212 I/O card's input
234// sensitivity.
235// ------------------------------------------------------------------------
236#define SET_SENS_LOCALINIT_BITPOS 15
237#define SET_SENS_DATA_BITPOS 10
238#define SET_SENS_CLOCK_BITPOS 8
239#define SET_SENS_LOADSHIFT_BITPOS 0
240
241#define SET_SENS_LEFTCHANID 0x00
242#define SET_SENS_RIGHTCHANID 0x01
243
244#define K1212SENSUPDATE_DELAY_IN_MS 50
245
246// --------------------------------------------------------------------------
247// WaitRTCTicks
248//
249// This function waits the specified number of real time clock ticks.
250// According to the DDK, each tick is ~0.8 microseconds.
251// The defines following the function declaration can be used for the
252// numTicksToWait parameter.
253// --------------------------------------------------------------------------
254#define ONE_RTC_TICK 1
255#define SENSCLKPULSE_WIDTH 4
256#define LOADSHIFT_DELAY 4
257#define INTERCOMMAND_DELAY 40
258#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
259 // the command register. (could be up to 180 us)
260#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
261 // from the card after sending a command.
262
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100263enum ClockSourceIndex {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
265 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
266 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
267 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
268 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
269 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
270 K1212_CLKIDX_Invalid // used to check validity of the index
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100271};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100273enum ClockSourceType {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 K1212_CLKIDX_Adat = 0, // selects source as ADAT
275 K1212_CLKIDX_Word, // selects source as S/PDIF
276 K1212_CLKIDX_Local // selects source as local clock
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100277};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100279struct KorgAudioFrame {
280 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
281 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
282 u32 timeCodeVal; /* holds the ADAT timecode value */
283};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100285struct KorgAudioBuffer {
286 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
287};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100289struct KorgSharedBuffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#ifdef K1212_LARGEALLOC
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100291 struct KorgAudioBuffer playDataBufs[kNumBuffers];
292 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293#endif
294 short volumeData[kAudioChannels];
295 u32 cardCommand;
296 u16 routeData [kAudioChannels];
297 u32 AdatTimeCode; // ADAT timecode value
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100298};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100300struct SensBits {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 union {
302 struct {
303 unsigned int leftChanVal:8;
304 unsigned int leftChanId:8;
305 } v;
306 u16 leftSensBits;
307 } l;
308 union {
309 struct {
310 unsigned int rightChanVal:8;
311 unsigned int rightChanId:8;
312 } v;
313 u16 rightSensBits;
314 } r;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100315};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100317struct snd_korg1212 {
318 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct pci_dev *pci;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100320 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int irq;
322
323 spinlock_t lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100324 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 struct timer_list timer; /* timer callback for checking ack of stop request */
327 int stop_pending_cnt; /* counter for stop pending check */
328
329 wait_queue_head_t wait;
330
331 unsigned long iomem;
332 unsigned long ioport;
333 unsigned long iomem2;
334 unsigned long irqcount;
335 unsigned long inIRQ;
336 void __iomem *iobase;
337
338 struct snd_dma_buffer dma_dsp;
339 struct snd_dma_buffer dma_play;
340 struct snd_dma_buffer dma_rec;
341 struct snd_dma_buffer dma_shared;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 u32 DataBufsSize;
344
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100345 struct KorgAudioBuffer * playDataBufsPtr;
346 struct KorgAudioBuffer * recordDataBufsPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100348 struct KorgSharedBuffer * sharedBufferPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 u32 RecDataPhy;
351 u32 PlayDataPhy;
352 unsigned long sharedBufferPhy;
353 u32 VolumeTablePhy;
354 u32 RoutingTablePhy;
355 u32 AdatTimeCodePhy;
356
357 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
358 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
359 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
360 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
361 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
362 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
363 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
364 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
365 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
366 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
367
368 size_t periodsize;
369 int channels;
370 int currentBuffer;
371
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100372 struct snd_pcm_substream *playback_substream;
373 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 pid_t capture_pid;
376 pid_t playback_pid;
377
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100378 enum CardState cardState;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int running;
380 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
381 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
382
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100383 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100385 enum ClockSourceType clkSource; // clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int clkRate; // clock rate
387
388 int volumePhase[kAudioChannels];
389
390 u16 leftADCInSens; // ADC left channel input sensitivity
391 u16 rightADCInSens; // ADC right channel input sensitivity
392
393 int opencnt; // Open/Close count
394 int setcnt; // SetupForPlay count
395 int playcnt; // TriggerPlay count
396 int errorcnt; // Error Count
397 unsigned long totalerrorcnt; // Total Error Count
398
399 int dsp_is_loaded;
400 int dsp_stop_is_processed;
401
402};
403
404MODULE_DESCRIPTION("korg1212");
405MODULE_LICENSE("GPL");
406MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
Clemens Ladisch7e0af292007-05-03 17:59:54 +0200407MODULE_FIRMWARE("korg/k1212.dsp");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
410static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
411static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
412
413module_param_array(index, int, NULL, 0444);
414MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
415module_param_array(id, charp, NULL, 0444);
416MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
417module_param_array(enable, bool, NULL, 0444);
418MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
419MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
420
Takashi Iwaif40b6892006-07-05 16:51:05 +0200421static struct pci_device_id snd_korg1212_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 {
423 .vendor = 0x10b5,
424 .device = 0x906d,
425 .subvendor = PCI_ANY_ID,
426 .subdevice = PCI_ANY_ID,
427 },
428 { 0, },
429};
430
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100431MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
432
Takashi Iwai9fd91562005-11-17 10:45:48 +0100433static char *stateName[] = {
434 "Non-existent",
435 "Uninitialized",
436 "DSP download in process",
437 "DSP download complete",
438 "Ready",
439 "Open",
440 "Setup for play",
441 "Playing",
442 "Monitor mode on",
443 "Calibrating",
444 "Invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
Takashi Iwai9fd91562005-11-17 10:45:48 +0100447static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Takashi Iwai9fd91562005-11-17 10:45:48 +0100449static char *clockSourceName[] = {
450 "ADAT at 44.1 kHz",
451 "ADAT at 48 kHz",
452 "S/PDIF at 44.1 kHz",
453 "S/PDIF at 48 kHz",
454 "local clock at 44.1 kHz",
455 "local clock at 48 kHz"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456};
457
Takashi Iwai9fd91562005-11-17 10:45:48 +0100458static char *channelName[] = {
459 "ADAT-1",
460 "ADAT-2",
461 "ADAT-3",
462 "ADAT-4",
463 "ADAT-5",
464 "ADAT-6",
465 "ADAT-7",
466 "ADAT-8",
467 "Analog-L",
468 "Analog-R",
469 "SPDIF-L",
470 "SPDIF-R",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Takashi Iwai9fd91562005-11-17 10:45:48 +0100473static u16 ClockSourceSelector[] = {
474 0x8000, // selects source as ADAT at 44.1 kHz
475 0x0000, // selects source as ADAT at 48 kHz
476 0x8001, // selects source as S/PDIF at 44.1 kHz
477 0x0001, // selects source as S/PDIF at 48 kHz
478 0x8002, // selects source as local clock at 44.1 kHz
479 0x0002 // selects source as local clock at 48 kHz
480};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100482union swap_u32 { unsigned char c[4]; u32 i; };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484#ifdef SNDRV_BIG_ENDIAN
485static u32 LowerWordSwap(u32 swappee)
486#else
487static u32 UpperWordSwap(u32 swappee)
488#endif
489{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100490 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 swapper.i = swappee;
493 retVal.c[2] = swapper.c[3];
494 retVal.c[3] = swapper.c[2];
495 retVal.c[1] = swapper.c[1];
496 retVal.c[0] = swapper.c[0];
497
498 return retVal.i;
499}
500
501#ifdef SNDRV_BIG_ENDIAN
502static u32 UpperWordSwap(u32 swappee)
503#else
504static u32 LowerWordSwap(u32 swappee)
505#endif
506{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100507 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 swapper.i = swappee;
510 retVal.c[2] = swapper.c[2];
511 retVal.c[3] = swapper.c[3];
512 retVal.c[1] = swapper.c[0];
513 retVal.c[0] = swapper.c[1];
514
515 return retVal.i;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
519#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
520#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
521#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
522
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100523static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
524 enum korg1212_dbcnst doorbellVal,
525 u32 mailBox0Val, u32 mailBox1Val,
526 u32 mailBox2Val, u32 mailBox3Val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 u32 retryCount;
529 u16 mailBox3Lo;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100530 int rc = K1212_CMDRET_Success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (!korg1212->outDoorbellPtr) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100533 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return K1212_CMDRET_CardUninitialized;
535 }
536
Takashi Iwai9fd91562005-11-17 10:45:48 +0100537 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
538 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
540 writel(mailBox3Val, korg1212->mailbox3Ptr);
541 writel(mailBox2Val, korg1212->mailbox2Ptr);
542 writel(mailBox1Val, korg1212->mailbox1Ptr);
543 writel(mailBox0Val, korg1212->mailbox0Ptr);
544 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
545
546 // --------------------------------------------------------------
547 // the reboot command will not give an acknowledgement.
548 // --------------------------------------------------------------
549 if ( doorbellVal == K1212_DB_RebootCard ||
550 doorbellVal == K1212_DB_BootFromDSPPage4 ||
551 doorbellVal == K1212_DB_StartDSPDownload ) {
552 rc = K1212_CMDRET_Success;
553 break;
554 }
555
556 // --------------------------------------------------------------
557 // See if the card acknowledged the command. Wait a bit, then
558 // read in the low word of mailbox3. If the MSB is set and the
559 // low byte is equal to the doorbell value, then it ack'd.
560 // --------------------------------------------------------------
561 udelay(COMMAND_ACK_DELAY);
562 mailBox3Lo = readl(korg1212->mailbox3Ptr);
563 if (mailBox3Lo & COMMAND_ACK_MASK) {
564 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100565 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rc = K1212_CMDRET_Success;
567 break;
568 }
569 }
570 }
571 korg1212->cmdRetryCount += retryCount;
572
573 if (retryCount >= MAX_COMMAND_RETRIES) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100574 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 rc = K1212_CMDRET_NoAckFromCard;
576 }
577
578 return rc;
579}
580
581/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100582static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 if (! korg1212->stop_pending_cnt) {
585 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
586 /* program the timer */
587 korg1212->stop_pending_cnt = HZ;
588 korg1212->timer.expires = jiffies + 1;
589 add_timer(&korg1212->timer);
590 }
591}
592
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100593static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 unsigned long flags;
596 spin_lock_irqsave(&korg1212->lock, flags);
597 korg1212->dsp_stop_is_processed = 0;
598 snd_korg1212_SendStop(korg1212);
599 spin_unlock_irqrestore(&korg1212->lock, flags);
600 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
601}
602
603/* timer callback for checking the ack of stop request */
604static void snd_korg1212_timer_func(unsigned long data)
605{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100606 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100607 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Takashi Iwaib32425a2005-11-18 18:52:14 +0100609 spin_lock_irqsave(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (korg1212->sharedBufferPtr->cardCommand == 0) {
611 /* ack'ed */
612 korg1212->stop_pending_cnt = 0;
613 korg1212->dsp_stop_is_processed = 1;
614 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100615 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
616 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else {
618 if (--korg1212->stop_pending_cnt > 0) {
619 /* reprogram timer */
620 korg1212->timer.expires = jiffies + 1;
621 add_timer(&korg1212->timer);
622 } else {
623 snd_printd("korg1212_timer_func timeout\n");
624 korg1212->sharedBufferPtr->cardCommand = 0;
625 korg1212->dsp_stop_is_processed = 1;
626 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100627 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
628 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 }
Takashi Iwaib32425a2005-11-18 18:52:14 +0100631 spin_unlock_irqrestore(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100634static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 unsigned long flags;
Takashi Iwai9fd91562005-11-17 10:45:48 +0100637 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 udelay(INTERCOMMAND_DELAY);
640 spin_lock_irqsave(&korg1212->lock, flags);
641 korg1212->idleMonitorOn = 1;
642 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
643 K1212_MODE_MonitorOn, 0, 0, 0);
644 spin_unlock_irqrestore(&korg1212->lock, flags);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100645 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100648static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 if (korg1212->idleMonitorOn) {
651 snd_korg1212_SendStopAndWait(korg1212);
652 korg1212->idleMonitorOn = 0;
653 }
654}
655
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100656static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 korg1212->cardState = csState;
659}
660
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100661static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100663 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
664 stateName[korg1212->cardState], korg1212->opencnt);
Ingo Molnar62932df2006-01-16 16:34:20 +0100665 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (korg1212->opencnt++ == 0) {
667 snd_korg1212_TurnOffIdleMonitor(korg1212);
668 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
669 }
670
Ingo Molnar62932df2006-01-16 16:34:20 +0100671 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 1;
673}
674
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100675static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100677 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
678 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Ingo Molnar62932df2006-01-16 16:34:20 +0100680 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (--(korg1212->opencnt)) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100682 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684 }
685
686 if (korg1212->cardState == K1212_STATE_SETUP) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100687 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 K1212_MODE_StopPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100689 if (rc)
690 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
691 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (rc != K1212_CMDRET_Success) {
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 } else if (korg1212->cardState > K1212_STATE_SETUP) {
697 snd_korg1212_SendStopAndWait(korg1212);
698 }
699
700 if (korg1212->cardState > K1212_STATE_READY) {
701 snd_korg1212_TurnOnIdleMonitor(korg1212);
702 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
703 }
704
Ingo Molnar62932df2006-01-16 16:34:20 +0100705 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707}
708
709/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100710static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100712 int rc;
713
714 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
715 stateName[korg1212->cardState], korg1212->setcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (korg1212->setcnt++)
718 return 0;
719
720 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
721 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
722 K1212_MODE_SetupPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100723 if (rc)
724 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
725 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (rc != K1212_CMDRET_Success) {
727 return 1;
728 }
729 return 0;
730}
731
732/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100733static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100735 int rc;
736
737 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
738 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (korg1212->playcnt++)
741 return 0;
742
743 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
744 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100745 if (rc)
746 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
747 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (rc != K1212_CMDRET_Success) {
749 return 1;
750 }
751 return 0;
752}
753
754/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100755static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100757 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
758 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (--(korg1212->playcnt))
761 return 0;
762
763 korg1212->setcnt = 0;
764
765 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
766 snd_korg1212_SendStop(korg1212);
767
768 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
769 return 0;
770}
771
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100772static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 writel(PCI_INT_ENABLE_BIT |
775 PCI_DOORBELL_INT_ENABLE_BIT |
776 LOCAL_INT_ENABLE_BIT |
777 LOCAL_DOORBELL_INT_ENABLE_BIT |
778 LOCAL_DMA1_INT_ENABLE_BIT,
779 korg1212->statusRegPtr);
780}
781
782#if 0 /* not used */
783
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100784static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
785 enum MonitorModeSelector mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100787 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
788 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 switch (mode) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100791 case K1212_MONMODE_Off:
792 if (korg1212->cardState != K1212_STATE_MONITOR)
793 return 0;
794 else {
795 snd_korg1212_SendStopAndWait(korg1212);
796 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
797 }
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Takashi Iwai9fd91562005-11-17 10:45:48 +0100800 case K1212_MONMODE_On:
801 if (korg1212->cardState != K1212_STATE_OPEN)
802 return 0;
803 else {
804 int rc;
805 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
806 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
807 K1212_MODE_MonitorOn, 0, 0, 0);
808 if (rc != K1212_CMDRET_Success)
809 return 0;
810 }
811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Takashi Iwai9fd91562005-11-17 10:45:48 +0100813 default:
814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 return 1;
818}
819
820#endif /* not used */
821
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100822static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100824 if (korg1212->playback_pid != korg1212->capture_pid &&
825 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Takashi Iwai9fd91562005-11-17 10:45:48 +0100828 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100831static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100833 static enum ClockSourceIndex s44[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100834 K1212_CLKIDX_AdatAt44_1K,
835 K1212_CLKIDX_WordAt44_1K,
836 K1212_CLKIDX_LocalAt44_1K
837 };
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100838 static enum ClockSourceIndex s48[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100839 K1212_CLKIDX_AdatAt48K,
840 K1212_CLKIDX_WordAt48K,
841 K1212_CLKIDX_LocalAt48K
842 };
843 int parm, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Takashi Iwai9fd91562005-11-17 10:45:48 +0100845 if (!snd_korg1212_use_is_exclusive (korg1212))
846 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100848 switch (rate) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100849 case 44100:
850 parm = s44[korg1212->clkSource];
851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Takashi Iwai9fd91562005-11-17 10:45:48 +0100853 case 48000:
854 parm = s48[korg1212->clkSource];
855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Takashi Iwai9fd91562005-11-17 10:45:48 +0100857 default:
858 return -EINVAL;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 korg1212->clkSrcRate = parm;
862 korg1212->clkRate = rate;
863
864 udelay(INTERCOMMAND_DELAY);
865 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
866 ClockSourceSelector[korg1212->clkSrcRate],
867 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100868 if (rc)
869 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
870 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return 0;
873}
874
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100875static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877
Takashi Iwai9fd91562005-11-17 10:45:48 +0100878 if (source < 0 || source > 2)
879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 korg1212->clkSource = source;
882
883 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
884
885 return 0;
886}
887
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100888static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 writel(0, korg1212->statusRegPtr);
891}
892
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100893static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100895 struct SensBits sensVals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int bitPosition;
897 int channel;
898 int clkIs48K;
899 int monModeSet;
900 u16 controlValue; // this keeps the current value to be written to
901 // the card's eeprom control register.
902 u16 count;
903 unsigned long flags;
904
Takashi Iwai9fd91562005-11-17 10:45:48 +0100905 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
906 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 // ----------------------------------------------------------------------------
909 // initialize things. The local init bit is always set when writing to the
910 // card's control register.
911 // ----------------------------------------------------------------------------
912 controlValue = 0;
913 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
914
915 // ----------------------------------------------------------------------------
916 // make sure the card is not in monitor mode when we do this update.
917 // ----------------------------------------------------------------------------
918 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
919 monModeSet = 1;
920 snd_korg1212_SendStopAndWait(korg1212);
921 } else
922 monModeSet = 0;
923
924 spin_lock_irqsave(&korg1212->lock, flags);
925
926 // ----------------------------------------------------------------------------
927 // we are about to send new values to the card, so clear the new values queued
928 // flag. Also, clear out mailbox 3, so we don't lockup.
929 // ----------------------------------------------------------------------------
930 writel(0, korg1212->mailbox3Ptr);
931 udelay(LOADSHIFT_DELAY);
932
933 // ----------------------------------------------------------------------------
934 // determine whether we are running a 48K or 44.1K clock. This info is used
935 // later when setting the SPDIF FF after the volume has been shifted in.
936 // ----------------------------------------------------------------------------
937 switch (korg1212->clkSrcRate) {
938 case K1212_CLKIDX_AdatAt44_1K:
939 case K1212_CLKIDX_WordAt44_1K:
940 case K1212_CLKIDX_LocalAt44_1K:
941 clkIs48K = 0;
942 break;
943
944 case K1212_CLKIDX_WordAt48K:
945 case K1212_CLKIDX_AdatAt48K:
946 case K1212_CLKIDX_LocalAt48K:
947 default:
948 clkIs48K = 1;
949 break;
950 }
951
952 // ----------------------------------------------------------------------------
953 // start the update. Setup the bit structure and then shift the bits.
954 // ----------------------------------------------------------------------------
955 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
956 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
957 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
958 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
959
960 // ----------------------------------------------------------------------------
961 // now start shifting the bits in. Start with the left channel then the right.
962 // ----------------------------------------------------------------------------
963 for (channel = 0; channel < 2; channel++) {
964
965 // ----------------------------------------------------------------------------
966 // Bring the load/shift line low, then wait - the spec says >150ns from load/
967 // shift low to the first rising edge of the clock.
968 // ----------------------------------------------------------------------------
969 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
970 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
971 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
972 udelay(LOADSHIFT_DELAY);
973
974 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
Takashi Iwai9fd91562005-11-17 10:45:48 +0100975 if (channel == 0) {
976 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100977 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
Takashi Iwai9fd91562005-11-17 10:45:48 +0100978 else
979 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
980 } else {
981 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
982 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
983 else
984 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
988 writew(controlValue, korg1212->sensRegPtr); // clock goes low
989 udelay(SENSCLKPULSE_WIDTH);
990 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
991 writew(controlValue, korg1212->sensRegPtr); // clock goes high
992 udelay(SENSCLKPULSE_WIDTH);
993 }
994
995 // ----------------------------------------------------------------------------
996 // finish up SPDIF for left. Bring the load/shift line high, then write a one
997 // bit if the clock rate is 48K otherwise write 0.
998 // ----------------------------------------------------------------------------
999 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1000 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1001 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1002 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1003 udelay(SENSCLKPULSE_WIDTH);
1004
1005 if (clkIs48K)
1006 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1007
1008 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1009 udelay(ONE_RTC_TICK);
1010 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1011 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1012 udelay(SENSCLKPULSE_WIDTH);
1013 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1014 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1015 udelay(SENSCLKPULSE_WIDTH);
1016 }
1017
1018 // ----------------------------------------------------------------------------
1019 // The update is complete. Set a timeout. This is the inter-update delay.
1020 // Also, if the card was in monitor mode, restore it.
1021 // ----------------------------------------------------------------------------
1022 for (count = 0; count < 10; count++)
1023 udelay(SENSCLKPULSE_WIDTH);
1024
1025 if (monModeSet) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001026 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 K1212_MODE_MonitorOn, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001028 if (rc)
1029 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1030 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
1033 spin_unlock_irqrestore(&korg1212->lock, flags);
1034
1035 return 1;
1036}
1037
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001038static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001040 int channel, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Takashi Iwai9fd91562005-11-17 10:45:48 +01001042 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1043 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 // ----------------------------------------------------
1046 // tell the card to boot
1047 // ----------------------------------------------------
1048 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1049
Takashi Iwai9fd91562005-11-17 10:45:48 +01001050 if (rc)
1051 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1052 rc, stateName[korg1212->cardState]);
1053 msleep(DSP_BOOT_DELAY_IN_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 // --------------------------------------------------------------------------------
1056 // Let the card know where all the buffers are.
1057 // --------------------------------------------------------------------------------
1058 rc = snd_korg1212_Send1212Command(korg1212,
1059 K1212_DB_ConfigureBufferMemory,
1060 LowerWordSwap(korg1212->PlayDataPhy),
1061 LowerWordSwap(korg1212->RecDataPhy),
1062 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1063 // is based on 2 buffers
1064 0
1065 );
1066
Takashi Iwai9fd91562005-11-17 10:45:48 +01001067 if (rc)
1068 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1069 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 udelay(INTERCOMMAND_DELAY);
1072
1073 rc = snd_korg1212_Send1212Command(korg1212,
1074 K1212_DB_ConfigureMiscMemory,
1075 LowerWordSwap(korg1212->VolumeTablePhy),
1076 LowerWordSwap(korg1212->RoutingTablePhy),
1077 LowerWordSwap(korg1212->AdatTimeCodePhy),
1078 0
1079 );
1080
Takashi Iwai9fd91562005-11-17 10:45:48 +01001081 if (rc)
1082 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1083 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 // --------------------------------------------------------------------------------
1086 // Initialize the routing and volume tables, then update the card's state.
1087 // --------------------------------------------------------------------------------
1088 udelay(INTERCOMMAND_DELAY);
1089
1090 for (channel = 0; channel < kAudioChannels; channel++) {
1091 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1092 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1093 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1094 }
1095
1096 snd_korg1212_WriteADCSensitivity(korg1212);
1097
1098 udelay(INTERCOMMAND_DELAY);
1099 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1100 ClockSourceSelector[korg1212->clkSrcRate],
1101 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001102 if (rc)
1103 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1104 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Takashi Iwai9fd91562005-11-17 10:45:48 +01001106 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1108
Takashi Iwai9fd91562005-11-17 10:45:48 +01001109 if (rc)
1110 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1111 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1114}
1115
David Howells7d12e782006-10-05 14:55:46 +01001116static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 u32 doorbellValue;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001119 struct snd_korg1212 *korg1212 = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 doorbellValue = readl(korg1212->inDoorbellPtr);
1122
1123 if (!doorbellValue)
1124 return IRQ_NONE;
1125
1126 spin_lock(&korg1212->lock);
1127
1128 writel(doorbellValue, korg1212->inDoorbellPtr);
1129
1130 korg1212->irqcount++;
1131
1132 korg1212->inIRQ++;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 switch (doorbellValue) {
1135 case K1212_DB_DSPDownloadDone:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001136 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1137 korg1212->irqcount, doorbellValue,
1138 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1140 korg1212->dsp_is_loaded = 1;
1141 wake_up(&korg1212->wait);
1142 }
1143 break;
1144
1145 // ------------------------------------------------------------------------
1146 // an error occurred - stop the card
1147 // ------------------------------------------------------------------------
1148 case K1212_DB_DMAERROR:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001149 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1150 korg1212->irqcount, doorbellValue,
1151 stateName[korg1212->cardState]);
1152 snd_printk(KERN_ERR "korg1212: DMA Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 korg1212->errorcnt++;
1154 korg1212->totalerrorcnt++;
1155 korg1212->sharedBufferPtr->cardCommand = 0;
1156 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1157 break;
1158
1159 // ------------------------------------------------------------------------
1160 // the card has stopped by our request. Clear the command word and signal
1161 // the semaphore in case someone is waiting for this.
1162 // ------------------------------------------------------------------------
1163 case K1212_DB_CARDSTOPPED:
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001164 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
Takashi Iwai9fd91562005-11-17 10:45:48 +01001165 korg1212->irqcount, doorbellValue,
1166 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 korg1212->sharedBufferPtr->cardCommand = 0;
1168 break;
1169
1170 default:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001171 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001172 korg1212->irqcount, doorbellValue,
1173 korg1212->currentBuffer, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1175 korg1212->currentBuffer++;
1176
1177 if (korg1212->currentBuffer >= kNumBuffers)
1178 korg1212->currentBuffer = 0;
1179
1180 if (!korg1212->running)
1181 break;
1182
1183 if (korg1212->capture_substream) {
1184 spin_unlock(&korg1212->lock);
1185 snd_pcm_period_elapsed(korg1212->capture_substream);
1186 spin_lock(&korg1212->lock);
1187 }
1188
1189 if (korg1212->playback_substream) {
1190 spin_unlock(&korg1212->lock);
1191 snd_pcm_period_elapsed(korg1212->playback_substream);
1192 spin_lock(&korg1212->lock);
1193 }
1194 }
1195 break;
1196 }
1197
1198 korg1212->inIRQ--;
1199
1200 spin_unlock(&korg1212->lock);
1201
1202 return IRQ_HANDLED;
1203}
1204
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001205static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001207 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Takashi Iwai9fd91562005-11-17 10:45:48 +01001209 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1210 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 // ---------------------------------------------------------------
1213 // verify the state of the card before proceeding.
1214 // ---------------------------------------------------------------
Takashi Iwai9fd91562005-11-17 10:45:48 +01001215 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1221 UpperWordSwap(korg1212->dma_dsp.addr),
1222 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001223 if (rc)
1224 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1225 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 korg1212->dsp_is_loaded = 0;
1228 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1229 if (! korg1212->dsp_is_loaded )
1230 return -EBUSY; /* timeout */
1231
1232 snd_korg1212_OnDSPDownloadComplete(korg1212);
1233
1234 return 0;
1235}
1236
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001237static struct snd_pcm_hardware snd_korg1212_playback_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 .info = (SNDRV_PCM_INFO_MMAP |
1240 SNDRV_PCM_INFO_MMAP_VALID |
1241 SNDRV_PCM_INFO_INTERLEAVED),
1242 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1243 .rates = (SNDRV_PCM_RATE_44100 |
1244 SNDRV_PCM_RATE_48000),
1245 .rate_min = 44100,
1246 .rate_max = 48000,
1247 .channels_min = K1212_MIN_CHANNELS,
1248 .channels_max = K1212_MAX_CHANNELS,
1249 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1250 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1251 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1252 .periods_min = K1212_PERIODS,
1253 .periods_max = K1212_PERIODS,
1254 .fifo_size = 0,
1255};
1256
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001257static struct snd_pcm_hardware snd_korg1212_capture_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 .info = (SNDRV_PCM_INFO_MMAP |
1260 SNDRV_PCM_INFO_MMAP_VALID |
1261 SNDRV_PCM_INFO_INTERLEAVED),
1262 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1263 .rates = (SNDRV_PCM_RATE_44100 |
1264 SNDRV_PCM_RATE_48000),
1265 .rate_min = 44100,
1266 .rate_max = 48000,
1267 .channels_min = K1212_MIN_CHANNELS,
1268 .channels_max = K1212_MAX_CHANNELS,
1269 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1270 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1271 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1272 .periods_min = K1212_PERIODS,
1273 .periods_max = K1212_PERIODS,
1274 .fifo_size = 0,
1275};
1276
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001277static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001279 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int i;
1281
Takashi Iwai9fd91562005-11-17 10:45:48 +01001282 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1283 pos, offset, size, count);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001284 if (snd_BUG_ON(pos + count > K1212_MAX_SAMPLES))
1285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 for (i=0; i < count; i++) {
1288#if K1212_DEBUG_LEVEL > 0
1289 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1290 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001291 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1292 dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return -EFAULT;
1294 }
1295#endif
1296 memset((void*) dst + offset, 0, size);
1297 dst++;
1298 }
1299
1300 return 0;
1301}
1302
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001303static 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 -07001304{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001305 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int i, rc;
1307
Takashi Iwai9fd91562005-11-17 10:45:48 +01001308 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1309 pos, offset, size);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001310 if (snd_BUG_ON(pos + count > K1212_MAX_SAMPLES))
1311 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 for (i=0; i < count; i++) {
1314#if K1212_DEBUG_LEVEL > 0
1315 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1316 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001317 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 -07001318 return -EFAULT;
1319 }
1320#endif
1321 rc = copy_to_user(dst + offset, src, size);
1322 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 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 -07001324 return -EFAULT;
1325 }
1326 src++;
1327 dst += size;
1328 }
1329
1330 return 0;
1331}
1332
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001333static 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 -07001334{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001335 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int i, rc;
1337
Takashi Iwai9fd91562005-11-17 10:45:48 +01001338 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1339 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Takashi Iwaida3cec32008-08-08 17:12:14 +02001341 if (snd_BUG_ON(pos + count > K1212_MAX_SAMPLES))
1342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 for (i=0; i < count; i++) {
1345#if K1212_DEBUG_LEVEL > 0
1346 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1347 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001348 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 -07001349 return -EFAULT;
1350 }
1351#endif
1352 rc = copy_from_user((void*) dst + offset, src, size);
1353 if (rc) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001354 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 -07001355 return -EFAULT;
1356 }
1357 dst++;
1358 src += size;
1359 }
1360
1361 return 0;
1362}
1363
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001364static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001366 struct snd_korg1212 *korg1212 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai9fd91562005-11-17 10:45:48 +01001368 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1369 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 korg1212->pcm = NULL;
1372}
1373
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001374static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001377 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1378 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Takashi Iwai9fd91562005-11-17 10:45:48 +01001380 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1381 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 snd_korg1212_OpenCard(korg1212);
1384
1385 runtime->hw = snd_korg1212_playback_info;
1386 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1387
1388 spin_lock_irqsave(&korg1212->lock, flags);
1389
1390 korg1212->playback_substream = substream;
1391 korg1212->playback_pid = current->pid;
1392 korg1212->periodsize = K1212_PERIODS;
1393 korg1212->channels = K1212_CHANNELS;
1394 korg1212->errorcnt = 0;
1395
1396 spin_unlock_irqrestore(&korg1212->lock, flags);
1397
1398 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1399 return 0;
1400}
1401
1402
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001403static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001406 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1407 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Takashi Iwai9fd91562005-11-17 10:45:48 +01001409 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1410 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 snd_korg1212_OpenCard(korg1212);
1413
1414 runtime->hw = snd_korg1212_capture_info;
1415 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1416
1417 spin_lock_irqsave(&korg1212->lock, flags);
1418
1419 korg1212->capture_substream = substream;
1420 korg1212->capture_pid = current->pid;
1421 korg1212->periodsize = K1212_PERIODS;
1422 korg1212->channels = K1212_CHANNELS;
1423
1424 spin_unlock_irqrestore(&korg1212->lock, flags);
1425
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001426 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1427 kPlayBufferFrames, kPlayBufferFrames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429}
1430
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001431static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001434 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Takashi Iwai9fd91562005-11-17 10:45:48 +01001436 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1437 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1440
1441 spin_lock_irqsave(&korg1212->lock, flags);
1442
1443 korg1212->playback_pid = -1;
1444 korg1212->playback_substream = NULL;
1445 korg1212->periodsize = 0;
1446
1447 spin_unlock_irqrestore(&korg1212->lock, flags);
1448
1449 snd_korg1212_CloseCard(korg1212);
1450 return 0;
1451}
1452
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001453static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001456 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Takashi Iwai9fd91562005-11-17 10:45:48 +01001458 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1459 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 spin_lock_irqsave(&korg1212->lock, flags);
1462
1463 korg1212->capture_pid = -1;
1464 korg1212->capture_substream = NULL;
1465 korg1212->periodsize = 0;
1466
1467 spin_unlock_irqrestore(&korg1212->lock, flags);
1468
1469 snd_korg1212_CloseCard(korg1212);
1470 return 0;
1471}
1472
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001473static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned int cmd, void *arg)
1475{
Takashi Iwai9fd91562005-11-17 10:45:48 +01001476 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001479 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 info->offset = 0;
1481 info->first = info->channel * 16;
1482 info->step = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 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 -07001484 return 0;
1485 }
1486
1487 return snd_pcm_lib_ioctl(substream, cmd, arg);
1488}
1489
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001490static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1491 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001494 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int err;
1496 pid_t this_pid;
1497 pid_t other_pid;
1498
Takashi Iwai9fd91562005-11-17 10:45:48 +01001499 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1500 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 spin_lock_irqsave(&korg1212->lock, flags);
1503
1504 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1505 this_pid = korg1212->playback_pid;
1506 other_pid = korg1212->capture_pid;
1507 } else {
1508 this_pid = korg1212->capture_pid;
1509 other_pid = korg1212->playback_pid;
1510 }
1511
1512 if ((other_pid > 0) && (this_pid != other_pid)) {
1513
1514 /* The other stream is open, and not by the same
1515 task as this one. Make sure that the parameters
1516 that matter are the same.
1517 */
1518
1519 if ((int)params_rate(params) != korg1212->clkRate) {
1520 spin_unlock_irqrestore(&korg1212->lock, flags);
1521 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1522 return -EBUSY;
1523 }
1524
1525 spin_unlock_irqrestore(&korg1212->lock, flags);
1526 return 0;
1527 }
1528
1529 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1530 spin_unlock_irqrestore(&korg1212->lock, flags);
1531 return err;
1532 }
1533
1534 korg1212->channels = params_channels(params);
1535 korg1212->periodsize = K1212_PERIOD_BYTES;
1536
1537 spin_unlock_irqrestore(&korg1212->lock, flags);
1538
1539 return 0;
1540}
1541
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001542static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001544 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int rc;
1546
Takashi Iwai9fd91562005-11-17 10:45:48 +01001547 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1548 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 spin_lock_irq(&korg1212->lock);
1551
1552 /* FIXME: we should wait for ack! */
1553 if (korg1212->stop_pending_cnt > 0) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001554 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1555 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 spin_unlock_irq(&korg1212->lock);
1557 return -EAGAIN;
1558 /*
1559 korg1212->sharedBufferPtr->cardCommand = 0;
1560 del_timer(&korg1212->timer);
1561 korg1212->stop_pending_cnt = 0;
1562 */
1563 }
1564
1565 rc = snd_korg1212_SetupForPlay(korg1212);
1566
1567 korg1212->currentBuffer = 0;
1568
1569 spin_unlock_irq(&korg1212->lock);
1570
1571 return rc ? -EINVAL : 0;
1572}
1573
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001574static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int cmd)
1576{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001577 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int rc;
1579
Takashi Iwai9fd91562005-11-17 10:45:48 +01001580 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1581 stateName[korg1212->cardState], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 spin_lock(&korg1212->lock);
1584 switch (cmd) {
1585 case SNDRV_PCM_TRIGGER_START:
1586/*
1587 if (korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001588 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 break;
1590 }
1591*/
1592 korg1212->running++;
1593 rc = snd_korg1212_TriggerPlay(korg1212);
1594 break;
1595
1596 case SNDRV_PCM_TRIGGER_STOP:
1597/*
1598 if (!korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001599 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
1601 }
1602*/
1603 korg1212->running--;
1604 rc = snd_korg1212_StopPlay(korg1212);
1605 break;
1606
1607 default:
1608 rc = 1;
1609 break;
1610 }
1611 spin_unlock(&korg1212->lock);
1612 return rc ? -EINVAL : 0;
1613}
1614
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001615static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001617 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 snd_pcm_uframes_t pos;
1619
1620 pos = korg1212->currentBuffer * kPlayBufferFrames;
1621
Takashi Iwai9fd91562005-11-17 10:45:48 +01001622 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1623 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 return pos;
1626}
1627
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001628static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001630 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 snd_pcm_uframes_t pos;
1632
1633 pos = korg1212->currentBuffer * kPlayBufferFrames;
1634
Takashi Iwai9fd91562005-11-17 10:45:48 +01001635 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1636 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 return pos;
1639}
1640
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001641static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int channel, /* not used (interleaved data) */
1643 snd_pcm_uframes_t pos,
1644 void __user *src,
1645 snd_pcm_uframes_t count)
1646{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001647 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Takashi Iwai9fd91562005-11-17 10:45:48 +01001649 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1650 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1653
1654}
1655
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001656static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int channel, /* not used (interleaved data) */
1658 snd_pcm_uframes_t pos,
1659 snd_pcm_uframes_t count)
1660{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001661 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Takashi Iwai9fd91562005-11-17 10:45:48 +01001663 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1664 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1667}
1668
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001669static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int channel, /* not used (interleaved data) */
1671 snd_pcm_uframes_t pos,
1672 void __user *dst,
1673 snd_pcm_uframes_t count)
1674{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001675 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Takashi Iwai9fd91562005-11-17 10:45:48 +01001677 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1678 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1681}
1682
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001683static struct snd_pcm_ops snd_korg1212_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 .open = snd_korg1212_playback_open,
1685 .close = snd_korg1212_playback_close,
1686 .ioctl = snd_korg1212_ioctl,
1687 .hw_params = snd_korg1212_hw_params,
1688 .prepare = snd_korg1212_prepare,
1689 .trigger = snd_korg1212_trigger,
1690 .pointer = snd_korg1212_playback_pointer,
1691 .copy = snd_korg1212_playback_copy,
1692 .silence = snd_korg1212_playback_silence,
1693};
1694
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001695static struct snd_pcm_ops snd_korg1212_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .open = snd_korg1212_capture_open,
1697 .close = snd_korg1212_capture_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_capture_pointer,
1703 .copy = snd_korg1212_capture_copy,
1704};
1705
1706/*
1707 * Control Interface
1708 */
1709
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001710static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1711 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1714 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1715 return 0;
1716}
1717
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001718static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001721 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 int i = kcontrol->private_value;
1723
1724 spin_lock_irq(&korg1212->lock);
1725
1726 u->value.integer.value[0] = korg1212->volumePhase[i];
1727
1728 if (i >= 8)
1729 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1730
1731 spin_unlock_irq(&korg1212->lock);
1732
1733 return 0;
1734}
1735
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001736static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001739 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int change = 0;
1741 int i, val;
1742
1743 spin_lock_irq(&korg1212->lock);
1744
1745 i = kcontrol->private_value;
1746
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001747 korg1212->volumePhase[i] = !!u->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1750
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001751 if ((u->value.integer.value[0] != 0) != (val < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1753 korg1212->sharedBufferPtr->volumeData[i] = val;
1754 change = 1;
1755 }
1756
1757 if (i >= 8) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001758 korg1212->volumePhase[i+1] = !!u->value.integer.value[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1761
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001762 if ((u->value.integer.value[1] != 0) != (val < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1764 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1765 change = 1;
1766 }
1767 }
1768
1769 spin_unlock_irq(&korg1212->lock);
1770
1771 return change;
1772}
1773
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001774static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1775 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1778 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1779 uinfo->value.integer.min = k1212MinVolume;
1780 uinfo->value.integer.max = k1212MaxVolume;
1781 return 0;
1782}
1783
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001784static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1785 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001787 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int i;
1789
1790 spin_lock_irq(&korg1212->lock);
1791
1792 i = kcontrol->private_value;
1793 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1794
1795 if (i >= 8)
1796 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1797
1798 spin_unlock_irq(&korg1212->lock);
1799
1800 return 0;
1801}
1802
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001803static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001806 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int change = 0;
1808 int i;
1809 int val;
1810
1811 spin_lock_irq(&korg1212->lock);
1812
1813 i = kcontrol->private_value;
1814
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001815 if (u->value.integer.value[0] >= k1212MinVolume &&
1816 u->value.integer.value[0] >= k1212MaxVolume &&
1817 u->value.integer.value[0] !=
1818 abs(korg1212->sharedBufferPtr->volumeData[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1820 val *= u->value.integer.value[0];
1821 korg1212->sharedBufferPtr->volumeData[i] = val;
1822 change = 1;
1823 }
1824
1825 if (i >= 8) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001826 if (u->value.integer.value[1] >= k1212MinVolume &&
1827 u->value.integer.value[1] >= k1212MaxVolume &&
1828 u->value.integer.value[1] !=
1829 abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1831 val *= u->value.integer.value[1];
1832 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1833 change = 1;
1834 }
1835 }
1836
1837 spin_unlock_irq(&korg1212->lock);
1838
1839 return change;
1840}
1841
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001842static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1843 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1846 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1847 uinfo->value.enumerated.items = kAudioChannels;
1848 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1849 uinfo->value.enumerated.item = kAudioChannels-1;
1850 }
1851 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1852 return 0;
1853}
1854
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001855static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001858 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 int i;
1860
1861 spin_lock_irq(&korg1212->lock);
1862
1863 i = kcontrol->private_value;
1864 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1865
1866 if (i >= 8)
1867 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1868
1869 spin_unlock_irq(&korg1212->lock);
1870
1871 return 0;
1872}
1873
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001874static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1875 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001877 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 int change = 0, i;
1879
1880 spin_lock_irq(&korg1212->lock);
1881
1882 i = kcontrol->private_value;
1883
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001884 if (u->value.enumerated.item[0] < kAudioChannels &&
1885 u->value.enumerated.item[0] !=
1886 (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1888 change = 1;
1889 }
1890
1891 if (i >= 8) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001892 if (u->value.enumerated.item[1] < kAudioChannels &&
1893 u->value.enumerated.item[1] !=
1894 (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1896 change = 1;
1897 }
1898 }
1899
1900 spin_unlock_irq(&korg1212->lock);
1901
1902 return change;
1903}
1904
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001905static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1909 uinfo->count = 2;
1910 uinfo->value.integer.min = k1212MaxADCSens;
1911 uinfo->value.integer.max = k1212MinADCSens;
1912 return 0;
1913}
1914
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001915static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1916 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001918 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 spin_lock_irq(&korg1212->lock);
1921
1922 u->value.integer.value[0] = korg1212->leftADCInSens;
1923 u->value.integer.value[1] = korg1212->rightADCInSens;
1924
1925 spin_unlock_irq(&korg1212->lock);
1926
1927 return 0;
1928}
1929
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001930static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1931 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001933 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 int change = 0;
1935
1936 spin_lock_irq(&korg1212->lock);
1937
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001938 if (u->value.integer.value[0] >= k1212MinADCSens &&
1939 u->value.integer.value[0] <= k1212MaxADCSens &&
1940 u->value.integer.value[0] != korg1212->leftADCInSens) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 korg1212->leftADCInSens = u->value.integer.value[0];
1942 change = 1;
1943 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001944 if (u->value.integer.value[1] >= k1212MinADCSens &&
1945 u->value.integer.value[1] <= k1212MaxADCSens &&
1946 u->value.integer.value[1] != korg1212->rightADCInSens) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 korg1212->rightADCInSens = u->value.integer.value[1];
1948 change = 1;
1949 }
1950
1951 spin_unlock_irq(&korg1212->lock);
1952
1953 if (change)
1954 snd_korg1212_WriteADCSensitivity(korg1212);
1955
1956 return change;
1957}
1958
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001959static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1963 uinfo->count = 1;
1964 uinfo->value.enumerated.items = 3;
1965 if (uinfo->value.enumerated.item > 2) {
1966 uinfo->value.enumerated.item = 2;
1967 }
1968 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1969 return 0;
1970}
1971
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001972static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1973 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001975 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 spin_lock_irq(&korg1212->lock);
1978
1979 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1980
1981 spin_unlock_irq(&korg1212->lock);
1982 return 0;
1983}
1984
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001985static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001988 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 unsigned int val;
1990 int change;
1991
1992 val = ucontrol->value.enumerated.item[0] % 3;
1993 spin_lock_irq(&korg1212->lock);
1994 change = val != korg1212->clkSource;
1995 snd_korg1212_SetClockSource(korg1212, val);
1996 spin_unlock_irq(&korg1212->lock);
1997 return change;
1998}
1999
2000#define MON_MIXER(ord,c_name) \
2001 { \
2002 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2003 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2004 .name = c_name " Monitor Volume", \
2005 .info = snd_korg1212_control_volume_info, \
2006 .get = snd_korg1212_control_volume_get, \
2007 .put = snd_korg1212_control_volume_put, \
2008 .private_value = ord, \
2009 }, \
2010 { \
2011 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2012 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2013 .name = c_name " Monitor Route", \
2014 .info = snd_korg1212_control_route_info, \
2015 .get = snd_korg1212_control_route_get, \
2016 .put = snd_korg1212_control_route_put, \
2017 .private_value = ord, \
2018 }, \
2019 { \
2020 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 .name = c_name " Monitor Phase Invert", \
2023 .info = snd_korg1212_control_phase_info, \
2024 .get = snd_korg1212_control_phase_get, \
2025 .put = snd_korg1212_control_phase_put, \
2026 .private_value = ord, \
2027 }
2028
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002029static struct snd_kcontrol_new snd_korg1212_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 MON_MIXER(8, "Analog"),
2031 MON_MIXER(10, "SPDIF"),
2032 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2033 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2034 {
2035 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002036 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 .name = "Sync Source",
2038 .info = snd_korg1212_control_sync_info,
2039 .get = snd_korg1212_control_sync_get,
2040 .put = snd_korg1212_control_sync_put,
2041 },
2042 {
2043 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2044 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2045 .name = "ADC Attenuation",
2046 .info = snd_korg1212_control_info,
2047 .get = snd_korg1212_control_get,
2048 .put = snd_korg1212_control_put,
2049 }
2050};
2051
2052/*
2053 * proc interface
2054 */
2055
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002056static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2057 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 int n;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002060 struct snd_korg1212 *korg1212 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 snd_iprintf(buffer, korg1212->card->longname);
2063 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2064 snd_iprintf(buffer, "\nGeneral settings\n");
2065 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2066 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2067 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2068 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2069 snd_iprintf(buffer, " Volume Info:\n");
2070 for (n=0; n<kAudioChannels; n++)
2071 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2072 channelName[n],
2073 channelName[korg1212->sharedBufferPtr->routeData[n]],
2074 korg1212->sharedBufferPtr->volumeData[n]);
2075 snd_iprintf(buffer, "\nGeneral status\n");
2076 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2077 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2078 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2079 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2080 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2081 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2082}
2083
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002084static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002086 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002089 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
2092static int
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002093snd_korg1212_free(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 snd_korg1212_TurnOffIdleMonitor(korg1212);
2096
2097 if (korg1212->irq >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 snd_korg1212_DisableCardInterrupts(korg1212);
Takashi Iwai9fd91562005-11-17 10:45:48 +01002099 free_irq(korg1212->irq, korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 korg1212->irq = -1;
2101 }
2102
2103 if (korg1212->iobase != NULL) {
2104 iounmap(korg1212->iobase);
2105 korg1212->iobase = NULL;
2106 }
2107
2108 pci_release_regions(korg1212->pci);
2109
2110 // ----------------------------------------------------
2111 // free up memory resources used for the DSP download.
2112 // ----------------------------------------------------
2113 if (korg1212->dma_dsp.area) {
2114 snd_dma_free_pages(&korg1212->dma_dsp);
2115 korg1212->dma_dsp.area = NULL;
2116 }
2117
2118#ifndef K1212_LARGEALLOC
2119
2120 // ------------------------------------------------------
2121 // free up memory resources used for the Play/Rec Buffers
2122 // ------------------------------------------------------
2123 if (korg1212->dma_play.area) {
2124 snd_dma_free_pages(&korg1212->dma_play);
2125 korg1212->dma_play.area = NULL;
2126 }
2127
2128 if (korg1212->dma_rec.area) {
2129 snd_dma_free_pages(&korg1212->dma_rec);
2130 korg1212->dma_rec.area = NULL;
2131 }
2132
2133#endif
2134
2135 // ----------------------------------------------------
2136 // free up memory resources used for the Shared Buffers
2137 // ----------------------------------------------------
2138 if (korg1212->dma_shared.area) {
2139 snd_dma_free_pages(&korg1212->dma_shared);
2140 korg1212->dma_shared.area = NULL;
2141 }
2142
2143 pci_disable_device(korg1212->pci);
2144 kfree(korg1212);
2145 return 0;
2146}
2147
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002148static int snd_korg1212_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002150 struct snd_korg1212 *korg1212 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return snd_korg1212_free(korg1212);
2153}
2154
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002155static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2156 struct snd_korg1212 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158{
Takashi Iwai9fd91562005-11-17 10:45:48 +01002159 int err, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 unsigned int i;
2161 unsigned ioport_size, iomem_size, iomem2_size;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002162 struct snd_korg1212 * korg1212;
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002163 const struct firmware *dsp_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002165 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 .dev_free = snd_korg1212_dev_free,
2167 };
2168
2169 * rchip = NULL;
2170 if ((err = pci_enable_device(pci)) < 0)
2171 return err;
2172
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002173 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (korg1212 == NULL) {
2175 pci_disable_device(pci);
2176 return -ENOMEM;
2177 }
2178
2179 korg1212->card = card;
2180 korg1212->pci = pci;
2181
2182 init_waitqueue_head(&korg1212->wait);
2183 spin_lock_init(&korg1212->lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002184 mutex_init(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 init_timer(&korg1212->timer);
2186 korg1212->timer.function = snd_korg1212_timer_func;
2187 korg1212->timer.data = (unsigned long)korg1212;
2188
2189 korg1212->irq = -1;
2190 korg1212->clkSource = K1212_CLKIDX_Local;
2191 korg1212->clkRate = 44100;
2192 korg1212->inIRQ = 0;
2193 korg1212->running = 0;
2194 korg1212->opencnt = 0;
2195 korg1212->playcnt = 0;
2196 korg1212->setcnt = 0;
2197 korg1212->totalerrorcnt = 0;
2198 korg1212->playback_pid = -1;
2199 korg1212->capture_pid = -1;
2200 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2201 korg1212->idleMonitorOn = 0;
2202 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2203 korg1212->leftADCInSens = k1212MaxADCSens;
2204 korg1212->rightADCInSens = k1212MaxADCSens;
2205
2206 for (i=0; i<kAudioChannels; i++)
2207 korg1212->volumePhase[i] = 0;
2208
2209 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2210 kfree(korg1212);
2211 pci_disable_device(pci);
2212 return err;
2213 }
2214
2215 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2216 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2217 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2218
2219 iomem_size = pci_resource_len(korg1212->pci, 0);
2220 ioport_size = pci_resource_len(korg1212->pci, 1);
2221 iomem2_size = pci_resource_len(korg1212->pci, 2);
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2224 " iomem = 0x%lx (%d)\n"
2225 " ioport = 0x%lx (%d)\n"
2226 " iomem = 0x%lx (%d)\n"
2227 " [%s]\n",
2228 korg1212->iomem, iomem_size,
2229 korg1212->ioport, ioport_size,
2230 korg1212->iomem2, iomem2_size,
2231 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2234 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2235 korg1212->iomem + iomem_size - 1);
2236 snd_korg1212_free(korg1212);
2237 return -EBUSY;
2238 }
2239
2240 err = request_irq(pci->irq, snd_korg1212_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002241 IRQF_SHARED,
Takashi Iwai9fd91562005-11-17 10:45:48 +01002242 "korg1212", korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 if (err) {
2245 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2246 snd_korg1212_free(korg1212);
2247 return -EBUSY;
2248 }
2249
2250 korg1212->irq = pci->irq;
2251
2252 pci_set_master(korg1212->pci);
2253
2254 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2255 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2256 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2257 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2258 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2259 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2260 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2261 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2262 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2263 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2266 " Status register = 0x%p\n"
2267 " OutDoorbell = 0x%p\n"
2268 " InDoorbell = 0x%p\n"
2269 " Mailbox0 = 0x%p\n"
2270 " Mailbox1 = 0x%p\n"
2271 " Mailbox2 = 0x%p\n"
2272 " Mailbox3 = 0x%p\n"
2273 " ControlReg = 0x%p\n"
2274 " SensReg = 0x%p\n"
2275 " IDReg = 0x%p\n"
2276 " [%s]\n",
2277 korg1212->statusRegPtr,
2278 korg1212->outDoorbellPtr,
2279 korg1212->inDoorbellPtr,
2280 korg1212->mailbox0Ptr,
2281 korg1212->mailbox1Ptr,
2282 korg1212->mailbox2Ptr,
2283 korg1212->mailbox3Ptr,
2284 korg1212->controlRegPtr,
2285 korg1212->sensRegPtr,
2286 korg1212->idRegPtr,
2287 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002290 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2291 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 snd_korg1212_free(korg1212);
2293 return -ENOMEM;
2294 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002295 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2297
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002298 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 -07002299
2300#ifndef K1212_LARGEALLOC
2301
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002302 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2305 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2306 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2307 snd_korg1212_free(korg1212);
2308 return -ENOMEM;
2309 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002310 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2314 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2317 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2318 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2319 snd_korg1212_free(korg1212);
2320 return -ENOMEM;
2321 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002322 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2326 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328#else // K1212_LARGEALLOC
2329
2330 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2331 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002332 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2333 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335#endif // K1212_LARGEALLOC
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002338 offsetof(struct KorgSharedBuffer, volumeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002340 offsetof(struct KorgSharedBuffer, routeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002342 offsetof(struct KorgSharedBuffer, AdatTimeCode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002344 err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
2345 if (err < 0) {
2346 release_firmware(dsp_code);
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002347 snd_printk(KERN_ERR "firmware not available\n");
2348 snd_korg1212_free(korg1212);
2349 return err;
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002350 }
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002353 dsp_code->size, &korg1212->dma_dsp) < 0) {
Randy Dunlap9fb62c92006-11-21 19:01:51 +01002354 snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 snd_korg1212_free(korg1212);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002356 release_firmware(dsp_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return -ENOMEM;
2358 }
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002361 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002364 memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
2365
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002366 release_firmware(dsp_code);
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2369
Takashi Iwai9fd91562005-11-17 10:45:48 +01002370 if (rc)
2371 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2374 snd_korg1212_free(korg1212);
2375 return err;
2376 }
2377
2378 snd_korg1212_EnableCardInterrupts(korg1212);
2379
2380 mdelay(CARD_BOOT_DELAY_IN_MS);
2381
2382 if (snd_korg1212_downloadDSPCode(korg1212))
2383 return -EBUSY;
2384
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002385 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 "PlayDataPhy = %08x L[%08x]\n"
2387 "korg1212: RecDataPhy = %08x L[%08x], "
2388 "VolumeTablePhy = %08x L[%08x]\n"
2389 "korg1212: RoutingTablePhy = %08x L[%08x], "
2390 "AdatTimeCodePhy = %08x L[%08x]\n",
2391 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2392 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2393 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2394 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2395 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2396 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2397
2398 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2399 return err;
2400
2401 korg1212->pcm->private_data = korg1212;
2402 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2403 strcpy(korg1212->pcm->name, "korg1212");
2404
2405 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2406
2407 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2408
2409 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2412 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2413 if (err < 0)
2414 return err;
2415 }
2416
2417 snd_korg1212_proc_init(korg1212);
2418
2419 snd_card_set_dev(card, &pci->dev);
2420
2421 * rchip = korg1212;
2422 return 0;
2423
2424}
2425
2426/*
2427 * Card initialisation
2428 */
2429
2430static int __devinit
2431snd_korg1212_probe(struct pci_dev *pci,
2432 const struct pci_device_id *pci_id)
2433{
2434 static int dev;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002435 struct snd_korg1212 *korg1212;
2436 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 int err;
2438
2439 if (dev >= SNDRV_CARDS) {
2440 return -ENODEV;
2441 }
2442 if (!enable[dev]) {
2443 dev++;
2444 return -ENOENT;
2445 }
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002446 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2447 if (err < 0)
2448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2451 snd_card_free(card);
2452 return err;
2453 }
2454
2455 strcpy(card->driver, "korg1212");
2456 strcpy(card->shortname, "korg1212");
2457 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2458 korg1212->iomem, korg1212->irq);
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 if ((err = snd_card_register(card)) < 0) {
2463 snd_card_free(card);
2464 return err;
2465 }
2466 pci_set_drvdata(pci, card);
2467 dev++;
2468 return 0;
2469}
2470
2471static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2472{
2473 snd_card_free(pci_get_drvdata(pci));
2474 pci_set_drvdata(pci, NULL);
2475}
2476
2477static struct pci_driver driver = {
2478 .name = "korg1212",
2479 .id_table = snd_korg1212_ids,
2480 .probe = snd_korg1212_probe,
2481 .remove = __devexit_p(snd_korg1212_remove),
2482};
2483
2484static int __init alsa_card_korg1212_init(void)
2485{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002486 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489static void __exit alsa_card_korg1212_exit(void)
2490{
2491 pci_unregister_driver(&driver);
2492}
2493
2494module_init(alsa_card_korg1212_init)
2495module_exit(alsa_card_korg1212_exit)