blob: 4721c096335ec7b05dfb5322225eecbf4321ba42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the Korg 1212 IO PCI card
3 *
4 * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/slab.h>
28#include <linux/wait.h>
29#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <sound/core.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/initval.h>
38
39#include <asm/io.h>
40
41// ----------------------------------------------------------------------------
42// Debug Stuff
43// ----------------------------------------------------------------------------
44#define K1212_DEBUG_LEVEL 0
Takashi Iwai9fd91562005-11-17 10:45:48 +010045#if K1212_DEBUG_LEVEL > 0
46#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
47#else
48#define K1212_DEBUG_PRINTK(fmt,...)
49#endif
50#if K1212_DEBUG_LEVEL > 1
51#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
52#else
53#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56// ----------------------------------------------------------------------------
57// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
58// buffers are alocated as a large piece inside KorgSharedBuffer.
59// ----------------------------------------------------------------------------
60//#define K1212_LARGEALLOC 1
61
62// ----------------------------------------------------------------------------
63// Valid states of the Korg 1212 I/O card.
64// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010065enum CardState {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 K1212_STATE_NONEXISTENT, // there is no card here
67 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
68 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
69 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
70 K1212_STATE_READY, // the card can be opened by an application. Any application
71 // requests prior to this state should fail. Only an open
72 // request can be made at this state.
73 K1212_STATE_OPEN, // an application has opened the card
74 K1212_STATE_SETUP, // the card has been setup for play
75 K1212_STATE_PLAYING, // the card is playing
76 K1212_STATE_MONITOR, // the card is in the monitor mode
77 K1212_STATE_CALIBRATING, // the card is currently calibrating
78 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
79 // are in the process of cleaning things up.
80 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
Takashi Iwaifcfd3332005-11-17 15:00:46 +010081};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83// ----------------------------------------------------------------------------
84// The following enumeration defines the constants written to the card's
85// host-to-card doorbell to initiate a command.
86// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010087enum korg1212_dbcnst {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
89 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
90 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
91 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
92 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
93 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
94 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
95 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
96 // timecode value.
97 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
98 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
99 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
100 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
101 // on page 4 (local page to card).
102 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
103 // completed.
104 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107
108// ----------------------------------------------------------------------------
109// The following enumeration defines return codes
110// to the Korg 1212 I/O driver.
111// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100112enum snd_korg1212rc {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 K1212_CMDRET_Success = 0, // command was successfully placed
114 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
115 K1212_CMDRET_PMFailure, // the protected mode call failed
116 K1212_CMDRET_FailUnspecified, // unspecified failure
117 K1212_CMDRET_FailBadState, // the specified command can not be given in
118 // the card's current state. (or the wave device's
119 // state)
120 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
121 K1212_CMDRET_BadIndex, // an out of range card index was specified
122 K1212_CMDRET_BadHandle, // an invalid card handle was specified
123 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
124 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
125 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
126 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
127
128 K1212_CMDRET_BadDevice, // the specified wave device was out of range
129 K1212_CMDRET_BadFormat // the specified wave format is unsupported
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132// ----------------------------------------------------------------------------
133// The following enumeration defines the constants used to select the play
134// mode for the card in the SelectPlayMode command.
135// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100136enum PlayModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
138 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
139 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
140 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143// ----------------------------------------------------------------------------
144// The following enumeration defines the constants used to select the monitor
145// mode for the card in the SetMonitorMode command.
146// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100147enum MonitorModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
149 K1212_MONMODE_On // tells card to turn on monitor mode
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100150};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
153#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
154#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
155#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
156#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
157#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
158#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
159#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
160 // register
161#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
162 // this is the upper word of the PCI control reg.
163#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
164
165#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
166 // from the card after sending a command.
167#define INTERCOMMAND_DELAY 40
168#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
169 // to send a command before giving up.
170#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
171 // the card.
172#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
173
174#define CARD_BOOT_DELAY_IN_MS 10
175#define CARD_BOOT_TIMEOUT 10
176#define DSP_BOOT_DELAY_IN_MS 200
177
178#define kNumBuffers 8
179#define k1212MaxCards 4
180#define k1212NumWaveDevices 6
181#define k16BitChannels 10
182#define k32BitChannels 2
183#define kAudioChannels (k16BitChannels + k32BitChannels)
184#define kPlayBufferFrames 1024
185
186#define K1212_ANALOG_CHANNELS 2
187#define K1212_SPDIF_CHANNELS 2
188#define K1212_ADAT_CHANNELS 8
189#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
190#define K1212_MIN_CHANNELS 1
191#define K1212_MAX_CHANNELS K1212_CHANNELS
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100192#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
194#define K1212_PERIODS (kNumBuffers)
195#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
196#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
197#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
198#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
199#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
200#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
201
202#define k1212MinADCSens 0x7f
203#define k1212MaxADCSens 0x00
204#define k1212MaxVolume 0x7fff
205#define k1212MaxWaveVolume 0xffff
206#define k1212MinVolume 0x0000
207#define k1212MaxVolInverted 0x8000
208
209// -----------------------------------------------------------------
210// the following bits are used for controlling interrupts in the
211// interrupt control/status reg
212// -----------------------------------------------------------------
213#define PCI_INT_ENABLE_BIT 0x00000100
214#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
215#define LOCAL_INT_ENABLE_BIT 0x00010000
216#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
217#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
218
219// -----------------------------------------------------------------
220// the following bits are defined for the PCI command register
221// -----------------------------------------------------------------
222#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
223#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
224#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
225
226// -----------------------------------------------------------------
227// the following bits are defined for the PCI status register
228// -----------------------------------------------------------------
229#define PCI_STAT_PARITY_ERROR_BIT 0x8000
230#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
231#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
232#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
233#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
234
235// ------------------------------------------------------------------------
236// the following constants are used in setting the 1212 I/O card's input
237// sensitivity.
238// ------------------------------------------------------------------------
239#define SET_SENS_LOCALINIT_BITPOS 15
240#define SET_SENS_DATA_BITPOS 10
241#define SET_SENS_CLOCK_BITPOS 8
242#define SET_SENS_LOADSHIFT_BITPOS 0
243
244#define SET_SENS_LEFTCHANID 0x00
245#define SET_SENS_RIGHTCHANID 0x01
246
247#define K1212SENSUPDATE_DELAY_IN_MS 50
248
249// --------------------------------------------------------------------------
250// WaitRTCTicks
251//
252// This function waits the specified number of real time clock ticks.
253// According to the DDK, each tick is ~0.8 microseconds.
254// The defines following the function declaration can be used for the
255// numTicksToWait parameter.
256// --------------------------------------------------------------------------
257#define ONE_RTC_TICK 1
258#define SENSCLKPULSE_WIDTH 4
259#define LOADSHIFT_DELAY 4
260#define INTERCOMMAND_DELAY 40
261#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
262 // the command register. (could be up to 180 us)
263#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
264 // from the card after sending a command.
265
266#include "korg1212-firmware.h"
267
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100268enum ClockSourceIndex {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
270 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
271 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
272 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
273 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
274 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
275 K1212_CLKIDX_Invalid // used to check validity of the index
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100278enum ClockSourceType {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 K1212_CLKIDX_Adat = 0, // selects source as ADAT
280 K1212_CLKIDX_Word, // selects source as S/PDIF
281 K1212_CLKIDX_Local // selects source as local clock
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100284struct KorgAudioFrame {
285 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
286 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
287 u32 timeCodeVal; /* holds the ADAT timecode value */
288};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100290struct KorgAudioBuffer {
291 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
292};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100294struct KorgSharedBuffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifdef K1212_LARGEALLOC
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100296 struct KorgAudioBuffer playDataBufs[kNumBuffers];
297 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
299 short volumeData[kAudioChannels];
300 u32 cardCommand;
301 u16 routeData [kAudioChannels];
302 u32 AdatTimeCode; // ADAT timecode value
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100303};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100305struct SensBits {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 union {
307 struct {
308 unsigned int leftChanVal:8;
309 unsigned int leftChanId:8;
310 } v;
311 u16 leftSensBits;
312 } l;
313 union {
314 struct {
315 unsigned int rightChanVal:8;
316 unsigned int rightChanId:8;
317 } v;
318 u16 rightSensBits;
319 } r;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100320};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100322struct snd_korg1212 {
323 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct pci_dev *pci;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100325 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int irq;
327
328 spinlock_t lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100329 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 struct timer_list timer; /* timer callback for checking ack of stop request */
332 int stop_pending_cnt; /* counter for stop pending check */
333
334 wait_queue_head_t wait;
335
336 unsigned long iomem;
337 unsigned long ioport;
338 unsigned long iomem2;
339 unsigned long irqcount;
340 unsigned long inIRQ;
341 void __iomem *iobase;
342
343 struct snd_dma_buffer dma_dsp;
344 struct snd_dma_buffer dma_play;
345 struct snd_dma_buffer dma_rec;
346 struct snd_dma_buffer dma_shared;
347
348 u32 dspCodeSize;
349
350 u32 DataBufsSize;
351
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100352 struct KorgAudioBuffer * playDataBufsPtr;
353 struct KorgAudioBuffer * recordDataBufsPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100355 struct KorgSharedBuffer * sharedBufferPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 u32 RecDataPhy;
358 u32 PlayDataPhy;
359 unsigned long sharedBufferPhy;
360 u32 VolumeTablePhy;
361 u32 RoutingTablePhy;
362 u32 AdatTimeCodePhy;
363
364 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
365 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
366 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
367 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
368 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
369 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
370 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
371 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
372 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
373 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
374
375 size_t periodsize;
376 int channels;
377 int currentBuffer;
378
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100379 struct snd_pcm_substream *playback_substream;
380 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 pid_t capture_pid;
383 pid_t playback_pid;
384
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100385 enum CardState cardState;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int running;
387 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
388 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
389
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100390 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100392 enum ClockSourceType clkSource; // clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int clkRate; // clock rate
394
395 int volumePhase[kAudioChannels];
396
397 u16 leftADCInSens; // ADC left channel input sensitivity
398 u16 rightADCInSens; // ADC right channel input sensitivity
399
400 int opencnt; // Open/Close count
401 int setcnt; // SetupForPlay count
402 int playcnt; // TriggerPlay count
403 int errorcnt; // Error Count
404 unsigned long totalerrorcnt; // Total Error Count
405
406 int dsp_is_loaded;
407 int dsp_stop_is_processed;
408
409};
410
411MODULE_DESCRIPTION("korg1212");
412MODULE_LICENSE("GPL");
413MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
414
415static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
416static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
417static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
418
419module_param_array(index, int, NULL, 0444);
420MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
421module_param_array(id, charp, NULL, 0444);
422MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
423module_param_array(enable, bool, NULL, 0444);
424MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
425MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
426
427static struct pci_device_id snd_korg1212_ids[] = {
428 {
429 .vendor = 0x10b5,
430 .device = 0x906d,
431 .subvendor = PCI_ANY_ID,
432 .subdevice = PCI_ANY_ID,
433 },
434 { 0, },
435};
436
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100437MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
438
Takashi Iwai9fd91562005-11-17 10:45:48 +0100439static char *stateName[] = {
440 "Non-existent",
441 "Uninitialized",
442 "DSP download in process",
443 "DSP download complete",
444 "Ready",
445 "Open",
446 "Setup for play",
447 "Playing",
448 "Monitor mode on",
449 "Calibrating",
450 "Invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
Takashi Iwai9fd91562005-11-17 10:45:48 +0100453static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Takashi Iwai9fd91562005-11-17 10:45:48 +0100455static char *clockSourceName[] = {
456 "ADAT at 44.1 kHz",
457 "ADAT at 48 kHz",
458 "S/PDIF at 44.1 kHz",
459 "S/PDIF at 48 kHz",
460 "local clock at 44.1 kHz",
461 "local clock at 48 kHz"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Takashi Iwai9fd91562005-11-17 10:45:48 +0100464static char *channelName[] = {
465 "ADAT-1",
466 "ADAT-2",
467 "ADAT-3",
468 "ADAT-4",
469 "ADAT-5",
470 "ADAT-6",
471 "ADAT-7",
472 "ADAT-8",
473 "Analog-L",
474 "Analog-R",
475 "SPDIF-L",
476 "SPDIF-R",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477};
478
Takashi Iwai9fd91562005-11-17 10:45:48 +0100479static u16 ClockSourceSelector[] = {
480 0x8000, // selects source as ADAT at 44.1 kHz
481 0x0000, // selects source as ADAT at 48 kHz
482 0x8001, // selects source as S/PDIF at 44.1 kHz
483 0x0001, // selects source as S/PDIF at 48 kHz
484 0x8002, // selects source as local clock at 44.1 kHz
485 0x0002 // selects source as local clock at 48 kHz
486};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100488union swap_u32 { unsigned char c[4]; u32 i; };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490#ifdef SNDRV_BIG_ENDIAN
491static u32 LowerWordSwap(u32 swappee)
492#else
493static u32 UpperWordSwap(u32 swappee)
494#endif
495{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100496 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 swapper.i = swappee;
499 retVal.c[2] = swapper.c[3];
500 retVal.c[3] = swapper.c[2];
501 retVal.c[1] = swapper.c[1];
502 retVal.c[0] = swapper.c[0];
503
504 return retVal.i;
505}
506
507#ifdef SNDRV_BIG_ENDIAN
508static u32 UpperWordSwap(u32 swappee)
509#else
510static u32 LowerWordSwap(u32 swappee)
511#endif
512{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100513 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 swapper.i = swappee;
516 retVal.c[2] = swapper.c[2];
517 retVal.c[3] = swapper.c[3];
518 retVal.c[1] = swapper.c[0];
519 retVal.c[0] = swapper.c[1];
520
521 return retVal.i;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
525#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
526#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
527#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
528
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100529static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
530 enum korg1212_dbcnst doorbellVal,
531 u32 mailBox0Val, u32 mailBox1Val,
532 u32 mailBox2Val, u32 mailBox3Val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 u32 retryCount;
535 u16 mailBox3Lo;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100536 int rc = K1212_CMDRET_Success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (!korg1212->outDoorbellPtr) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100539 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return K1212_CMDRET_CardUninitialized;
541 }
542
Takashi Iwai9fd91562005-11-17 10:45:48 +0100543 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
544 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
546 writel(mailBox3Val, korg1212->mailbox3Ptr);
547 writel(mailBox2Val, korg1212->mailbox2Ptr);
548 writel(mailBox1Val, korg1212->mailbox1Ptr);
549 writel(mailBox0Val, korg1212->mailbox0Ptr);
550 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
551
552 // --------------------------------------------------------------
553 // the reboot command will not give an acknowledgement.
554 // --------------------------------------------------------------
555 if ( doorbellVal == K1212_DB_RebootCard ||
556 doorbellVal == K1212_DB_BootFromDSPPage4 ||
557 doorbellVal == K1212_DB_StartDSPDownload ) {
558 rc = K1212_CMDRET_Success;
559 break;
560 }
561
562 // --------------------------------------------------------------
563 // See if the card acknowledged the command. Wait a bit, then
564 // read in the low word of mailbox3. If the MSB is set and the
565 // low byte is equal to the doorbell value, then it ack'd.
566 // --------------------------------------------------------------
567 udelay(COMMAND_ACK_DELAY);
568 mailBox3Lo = readl(korg1212->mailbox3Ptr);
569 if (mailBox3Lo & COMMAND_ACK_MASK) {
570 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100571 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rc = K1212_CMDRET_Success;
573 break;
574 }
575 }
576 }
577 korg1212->cmdRetryCount += retryCount;
578
579 if (retryCount >= MAX_COMMAND_RETRIES) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100580 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 rc = K1212_CMDRET_NoAckFromCard;
582 }
583
584 return rc;
585}
586
587/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100588static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 if (! korg1212->stop_pending_cnt) {
591 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
592 /* program the timer */
593 korg1212->stop_pending_cnt = HZ;
594 korg1212->timer.expires = jiffies + 1;
595 add_timer(&korg1212->timer);
596 }
597}
598
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100599static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 unsigned long flags;
602 spin_lock_irqsave(&korg1212->lock, flags);
603 korg1212->dsp_stop_is_processed = 0;
604 snd_korg1212_SendStop(korg1212);
605 spin_unlock_irqrestore(&korg1212->lock, flags);
606 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
607}
608
609/* timer callback for checking the ack of stop request */
610static void snd_korg1212_timer_func(unsigned long data)
611{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100612 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100613 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Takashi Iwaib32425a2005-11-18 18:52:14 +0100615 spin_lock_irqsave(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (korg1212->sharedBufferPtr->cardCommand == 0) {
617 /* ack'ed */
618 korg1212->stop_pending_cnt = 0;
619 korg1212->dsp_stop_is_processed = 1;
620 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100621 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
622 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else {
624 if (--korg1212->stop_pending_cnt > 0) {
625 /* reprogram timer */
626 korg1212->timer.expires = jiffies + 1;
627 add_timer(&korg1212->timer);
628 } else {
629 snd_printd("korg1212_timer_func timeout\n");
630 korg1212->sharedBufferPtr->cardCommand = 0;
631 korg1212->dsp_stop_is_processed = 1;
632 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100633 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
634 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 }
Takashi Iwaib32425a2005-11-18 18:52:14 +0100637 spin_unlock_irqrestore(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100640static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 unsigned long flags;
Takashi Iwai9fd91562005-11-17 10:45:48 +0100643 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 udelay(INTERCOMMAND_DELAY);
646 spin_lock_irqsave(&korg1212->lock, flags);
647 korg1212->idleMonitorOn = 1;
648 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
649 K1212_MODE_MonitorOn, 0, 0, 0);
650 spin_unlock_irqrestore(&korg1212->lock, flags);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100651 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100654static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 if (korg1212->idleMonitorOn) {
657 snd_korg1212_SendStopAndWait(korg1212);
658 korg1212->idleMonitorOn = 0;
659 }
660}
661
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100662static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 korg1212->cardState = csState;
665}
666
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100667static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100669 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
670 stateName[korg1212->cardState], korg1212->opencnt);
Ingo Molnar62932df2006-01-16 16:34:20 +0100671 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (korg1212->opencnt++ == 0) {
673 snd_korg1212_TurnOffIdleMonitor(korg1212);
674 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
675 }
676
Ingo Molnar62932df2006-01-16 16:34:20 +0100677 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 1;
679}
680
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100681static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100683 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
684 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Ingo Molnar62932df2006-01-16 16:34:20 +0100686 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (--(korg1212->opencnt)) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100688 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690 }
691
692 if (korg1212->cardState == K1212_STATE_SETUP) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100693 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 K1212_MODE_StopPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100695 if (rc)
696 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
697 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (rc != K1212_CMDRET_Success) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100699 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701 }
702 } else if (korg1212->cardState > K1212_STATE_SETUP) {
703 snd_korg1212_SendStopAndWait(korg1212);
704 }
705
706 if (korg1212->cardState > K1212_STATE_READY) {
707 snd_korg1212_TurnOnIdleMonitor(korg1212);
708 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
709 }
710
Ingo Molnar62932df2006-01-16 16:34:20 +0100711 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
715/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100716static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100718 int rc;
719
720 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
721 stateName[korg1212->cardState], korg1212->setcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (korg1212->setcnt++)
724 return 0;
725
726 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
727 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
728 K1212_MODE_SetupPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100729 if (rc)
730 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
731 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (rc != K1212_CMDRET_Success) {
733 return 1;
734 }
735 return 0;
736}
737
738/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100739static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100741 int rc;
742
743 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
744 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (korg1212->playcnt++)
747 return 0;
748
749 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
750 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100751 if (rc)
752 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
753 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (rc != K1212_CMDRET_Success) {
755 return 1;
756 }
757 return 0;
758}
759
760/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100761static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100763 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
764 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (--(korg1212->playcnt))
767 return 0;
768
769 korg1212->setcnt = 0;
770
771 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
772 snd_korg1212_SendStop(korg1212);
773
774 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
775 return 0;
776}
777
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100778static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 writel(PCI_INT_ENABLE_BIT |
781 PCI_DOORBELL_INT_ENABLE_BIT |
782 LOCAL_INT_ENABLE_BIT |
783 LOCAL_DOORBELL_INT_ENABLE_BIT |
784 LOCAL_DMA1_INT_ENABLE_BIT,
785 korg1212->statusRegPtr);
786}
787
788#if 0 /* not used */
789
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100790static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
791 enum MonitorModeSelector mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100793 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
794 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 switch (mode) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100797 case K1212_MONMODE_Off:
798 if (korg1212->cardState != K1212_STATE_MONITOR)
799 return 0;
800 else {
801 snd_korg1212_SendStopAndWait(korg1212);
802 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
803 }
804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Takashi Iwai9fd91562005-11-17 10:45:48 +0100806 case K1212_MONMODE_On:
807 if (korg1212->cardState != K1212_STATE_OPEN)
808 return 0;
809 else {
810 int rc;
811 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
812 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
813 K1212_MODE_MonitorOn, 0, 0, 0);
814 if (rc != K1212_CMDRET_Success)
815 return 0;
816 }
817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwai9fd91562005-11-17 10:45:48 +0100819 default:
820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 return 1;
824}
825
826#endif /* not used */
827
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100828static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100830 if (korg1212->playback_pid != korg1212->capture_pid &&
831 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Takashi Iwai9fd91562005-11-17 10:45:48 +0100834 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100837static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100839 static enum ClockSourceIndex s44[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100840 K1212_CLKIDX_AdatAt44_1K,
841 K1212_CLKIDX_WordAt44_1K,
842 K1212_CLKIDX_LocalAt44_1K
843 };
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100844 static enum ClockSourceIndex s48[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100845 K1212_CLKIDX_AdatAt48K,
846 K1212_CLKIDX_WordAt48K,
847 K1212_CLKIDX_LocalAt48K
848 };
849 int parm, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Takashi Iwai9fd91562005-11-17 10:45:48 +0100851 if (!snd_korg1212_use_is_exclusive (korg1212))
852 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100854 switch (rate) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100855 case 44100:
856 parm = s44[korg1212->clkSource];
857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Takashi Iwai9fd91562005-11-17 10:45:48 +0100859 case 48000:
860 parm = s48[korg1212->clkSource];
861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Takashi Iwai9fd91562005-11-17 10:45:48 +0100863 default:
864 return -EINVAL;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 korg1212->clkSrcRate = parm;
868 korg1212->clkRate = rate;
869
870 udelay(INTERCOMMAND_DELAY);
871 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
872 ClockSourceSelector[korg1212->clkSrcRate],
873 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100874 if (rc)
875 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
876 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return 0;
879}
880
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100881static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883
Takashi Iwai9fd91562005-11-17 10:45:48 +0100884 if (source < 0 || source > 2)
885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 korg1212->clkSource = source;
888
889 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
890
891 return 0;
892}
893
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100894static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 writel(0, korg1212->statusRegPtr);
897}
898
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100899static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100901 struct SensBits sensVals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int bitPosition;
903 int channel;
904 int clkIs48K;
905 int monModeSet;
906 u16 controlValue; // this keeps the current value to be written to
907 // the card's eeprom control register.
908 u16 count;
909 unsigned long flags;
910
Takashi Iwai9fd91562005-11-17 10:45:48 +0100911 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
912 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 // ----------------------------------------------------------------------------
915 // initialize things. The local init bit is always set when writing to the
916 // card's control register.
917 // ----------------------------------------------------------------------------
918 controlValue = 0;
919 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
920
921 // ----------------------------------------------------------------------------
922 // make sure the card is not in monitor mode when we do this update.
923 // ----------------------------------------------------------------------------
924 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
925 monModeSet = 1;
926 snd_korg1212_SendStopAndWait(korg1212);
927 } else
928 monModeSet = 0;
929
930 spin_lock_irqsave(&korg1212->lock, flags);
931
932 // ----------------------------------------------------------------------------
933 // we are about to send new values to the card, so clear the new values queued
934 // flag. Also, clear out mailbox 3, so we don't lockup.
935 // ----------------------------------------------------------------------------
936 writel(0, korg1212->mailbox3Ptr);
937 udelay(LOADSHIFT_DELAY);
938
939 // ----------------------------------------------------------------------------
940 // determine whether we are running a 48K or 44.1K clock. This info is used
941 // later when setting the SPDIF FF after the volume has been shifted in.
942 // ----------------------------------------------------------------------------
943 switch (korg1212->clkSrcRate) {
944 case K1212_CLKIDX_AdatAt44_1K:
945 case K1212_CLKIDX_WordAt44_1K:
946 case K1212_CLKIDX_LocalAt44_1K:
947 clkIs48K = 0;
948 break;
949
950 case K1212_CLKIDX_WordAt48K:
951 case K1212_CLKIDX_AdatAt48K:
952 case K1212_CLKIDX_LocalAt48K:
953 default:
954 clkIs48K = 1;
955 break;
956 }
957
958 // ----------------------------------------------------------------------------
959 // start the update. Setup the bit structure and then shift the bits.
960 // ----------------------------------------------------------------------------
961 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
962 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
963 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
964 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
965
966 // ----------------------------------------------------------------------------
967 // now start shifting the bits in. Start with the left channel then the right.
968 // ----------------------------------------------------------------------------
969 for (channel = 0; channel < 2; channel++) {
970
971 // ----------------------------------------------------------------------------
972 // Bring the load/shift line low, then wait - the spec says >150ns from load/
973 // shift low to the first rising edge of the clock.
974 // ----------------------------------------------------------------------------
975 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
976 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
977 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
978 udelay(LOADSHIFT_DELAY);
979
980 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
Takashi Iwai9fd91562005-11-17 10:45:48 +0100981 if (channel == 0) {
982 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100983 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
Takashi Iwai9fd91562005-11-17 10:45:48 +0100984 else
985 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
986 } else {
987 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
988 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
989 else
990 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
994 writew(controlValue, korg1212->sensRegPtr); // clock goes low
995 udelay(SENSCLKPULSE_WIDTH);
996 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
997 writew(controlValue, korg1212->sensRegPtr); // clock goes high
998 udelay(SENSCLKPULSE_WIDTH);
999 }
1000
1001 // ----------------------------------------------------------------------------
1002 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1003 // bit if the clock rate is 48K otherwise write 0.
1004 // ----------------------------------------------------------------------------
1005 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1006 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1007 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1008 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1009 udelay(SENSCLKPULSE_WIDTH);
1010
1011 if (clkIs48K)
1012 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1013
1014 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1015 udelay(ONE_RTC_TICK);
1016 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1017 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1018 udelay(SENSCLKPULSE_WIDTH);
1019 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1020 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1021 udelay(SENSCLKPULSE_WIDTH);
1022 }
1023
1024 // ----------------------------------------------------------------------------
1025 // The update is complete. Set a timeout. This is the inter-update delay.
1026 // Also, if the card was in monitor mode, restore it.
1027 // ----------------------------------------------------------------------------
1028 for (count = 0; count < 10; count++)
1029 udelay(SENSCLKPULSE_WIDTH);
1030
1031 if (monModeSet) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001032 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 K1212_MODE_MonitorOn, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001034 if (rc)
1035 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1036 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 spin_unlock_irqrestore(&korg1212->lock, flags);
1040
1041 return 1;
1042}
1043
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001044static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001046 int channel, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Takashi Iwai9fd91562005-11-17 10:45:48 +01001048 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1049 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 // ----------------------------------------------------
1052 // tell the card to boot
1053 // ----------------------------------------------------
1054 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1055
Takashi Iwai9fd91562005-11-17 10:45:48 +01001056 if (rc)
1057 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1058 rc, stateName[korg1212->cardState]);
1059 msleep(DSP_BOOT_DELAY_IN_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 // --------------------------------------------------------------------------------
1062 // Let the card know where all the buffers are.
1063 // --------------------------------------------------------------------------------
1064 rc = snd_korg1212_Send1212Command(korg1212,
1065 K1212_DB_ConfigureBufferMemory,
1066 LowerWordSwap(korg1212->PlayDataPhy),
1067 LowerWordSwap(korg1212->RecDataPhy),
1068 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1069 // is based on 2 buffers
1070 0
1071 );
1072
Takashi Iwai9fd91562005-11-17 10:45:48 +01001073 if (rc)
1074 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1075 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 udelay(INTERCOMMAND_DELAY);
1078
1079 rc = snd_korg1212_Send1212Command(korg1212,
1080 K1212_DB_ConfigureMiscMemory,
1081 LowerWordSwap(korg1212->VolumeTablePhy),
1082 LowerWordSwap(korg1212->RoutingTablePhy),
1083 LowerWordSwap(korg1212->AdatTimeCodePhy),
1084 0
1085 );
1086
Takashi Iwai9fd91562005-11-17 10:45:48 +01001087 if (rc)
1088 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1089 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 // --------------------------------------------------------------------------------
1092 // Initialize the routing and volume tables, then update the card's state.
1093 // --------------------------------------------------------------------------------
1094 udelay(INTERCOMMAND_DELAY);
1095
1096 for (channel = 0; channel < kAudioChannels; channel++) {
1097 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1098 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1099 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1100 }
1101
1102 snd_korg1212_WriteADCSensitivity(korg1212);
1103
1104 udelay(INTERCOMMAND_DELAY);
1105 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1106 ClockSourceSelector[korg1212->clkSrcRate],
1107 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001108 if (rc)
1109 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1110 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Takashi Iwai9fd91562005-11-17 10:45:48 +01001112 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1114
Takashi Iwai9fd91562005-11-17 10:45:48 +01001115 if (rc)
1116 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1117 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1120}
1121
1122static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1123{
1124 u32 doorbellValue;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001125 struct snd_korg1212 *korg1212 = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if(irq != korg1212->irq)
1128 return IRQ_NONE;
1129
1130 doorbellValue = readl(korg1212->inDoorbellPtr);
1131
1132 if (!doorbellValue)
1133 return IRQ_NONE;
1134
1135 spin_lock(&korg1212->lock);
1136
1137 writel(doorbellValue, korg1212->inDoorbellPtr);
1138
1139 korg1212->irqcount++;
1140
1141 korg1212->inIRQ++;
1142
1143
1144 switch (doorbellValue) {
1145 case K1212_DB_DSPDownloadDone:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001146 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1147 korg1212->irqcount, doorbellValue,
1148 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1150 korg1212->dsp_is_loaded = 1;
1151 wake_up(&korg1212->wait);
1152 }
1153 break;
1154
1155 // ------------------------------------------------------------------------
1156 // an error occurred - stop the card
1157 // ------------------------------------------------------------------------
1158 case K1212_DB_DMAERROR:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001159 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1160 korg1212->irqcount, doorbellValue,
1161 stateName[korg1212->cardState]);
1162 snd_printk(KERN_ERR "korg1212: DMA Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 korg1212->errorcnt++;
1164 korg1212->totalerrorcnt++;
1165 korg1212->sharedBufferPtr->cardCommand = 0;
1166 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1167 break;
1168
1169 // ------------------------------------------------------------------------
1170 // the card has stopped by our request. Clear the command word and signal
1171 // the semaphore in case someone is waiting for this.
1172 // ------------------------------------------------------------------------
1173 case K1212_DB_CARDSTOPPED:
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001174 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
Takashi Iwai9fd91562005-11-17 10:45:48 +01001175 korg1212->irqcount, doorbellValue,
1176 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 korg1212->sharedBufferPtr->cardCommand = 0;
1178 break;
1179
1180 default:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001181 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001182 korg1212->irqcount, doorbellValue,
1183 korg1212->currentBuffer, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1185 korg1212->currentBuffer++;
1186
1187 if (korg1212->currentBuffer >= kNumBuffers)
1188 korg1212->currentBuffer = 0;
1189
1190 if (!korg1212->running)
1191 break;
1192
1193 if (korg1212->capture_substream) {
1194 spin_unlock(&korg1212->lock);
1195 snd_pcm_period_elapsed(korg1212->capture_substream);
1196 spin_lock(&korg1212->lock);
1197 }
1198
1199 if (korg1212->playback_substream) {
1200 spin_unlock(&korg1212->lock);
1201 snd_pcm_period_elapsed(korg1212->playback_substream);
1202 spin_lock(&korg1212->lock);
1203 }
1204 }
1205 break;
1206 }
1207
1208 korg1212->inIRQ--;
1209
1210 spin_unlock(&korg1212->lock);
1211
1212 return IRQ_HANDLED;
1213}
1214
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001215static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001217 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Takashi Iwai9fd91562005-11-17 10:45:48 +01001219 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1220 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 // ---------------------------------------------------------------
1223 // verify the state of the card before proceeding.
1224 // ---------------------------------------------------------------
Takashi Iwai9fd91562005-11-17 10:45:48 +01001225 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1229
1230 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1231
1232 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1233 UpperWordSwap(korg1212->dma_dsp.addr),
1234 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001235 if (rc)
1236 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1237 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 korg1212->dsp_is_loaded = 0;
1240 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1241 if (! korg1212->dsp_is_loaded )
1242 return -EBUSY; /* timeout */
1243
1244 snd_korg1212_OnDSPDownloadComplete(korg1212);
1245
1246 return 0;
1247}
1248
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001249static struct snd_pcm_hardware snd_korg1212_playback_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 .info = (SNDRV_PCM_INFO_MMAP |
1252 SNDRV_PCM_INFO_MMAP_VALID |
1253 SNDRV_PCM_INFO_INTERLEAVED),
1254 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1255 .rates = (SNDRV_PCM_RATE_44100 |
1256 SNDRV_PCM_RATE_48000),
1257 .rate_min = 44100,
1258 .rate_max = 48000,
1259 .channels_min = K1212_MIN_CHANNELS,
1260 .channels_max = K1212_MAX_CHANNELS,
1261 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1262 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1263 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1264 .periods_min = K1212_PERIODS,
1265 .periods_max = K1212_PERIODS,
1266 .fifo_size = 0,
1267};
1268
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001269static struct snd_pcm_hardware snd_korg1212_capture_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 .info = (SNDRV_PCM_INFO_MMAP |
1272 SNDRV_PCM_INFO_MMAP_VALID |
1273 SNDRV_PCM_INFO_INTERLEAVED),
1274 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1275 .rates = (SNDRV_PCM_RATE_44100 |
1276 SNDRV_PCM_RATE_48000),
1277 .rate_min = 44100,
1278 .rate_max = 48000,
1279 .channels_min = K1212_MIN_CHANNELS,
1280 .channels_max = K1212_MAX_CHANNELS,
1281 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1282 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1283 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1284 .periods_min = K1212_PERIODS,
1285 .periods_max = K1212_PERIODS,
1286 .fifo_size = 0,
1287};
1288
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001289static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001291 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int i;
1293
Takashi Iwai9fd91562005-11-17 10:45:48 +01001294 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1295 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1297
1298 for (i=0; i < count; i++) {
1299#if K1212_DEBUG_LEVEL > 0
1300 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1301 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001302 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1303 dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EFAULT;
1305 }
1306#endif
1307 memset((void*) dst + offset, 0, size);
1308 dst++;
1309 }
1310
1311 return 0;
1312}
1313
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001314static 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 -07001315{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001316 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int i, rc;
1318
Takashi Iwai9fd91562005-11-17 10:45:48 +01001319 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1320 pos, offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1322
1323 for (i=0; i < count; i++) {
1324#if K1212_DEBUG_LEVEL > 0
1325 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1326 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001327 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 -07001328 return -EFAULT;
1329 }
1330#endif
1331 rc = copy_to_user(dst + offset, src, size);
1332 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 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 -07001334 return -EFAULT;
1335 }
1336 src++;
1337 dst += size;
1338 }
1339
1340 return 0;
1341}
1342
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001343static 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 -07001344{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001345 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int i, rc;
1347
Takashi Iwai9fd91562005-11-17 10:45:48 +01001348 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1349 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1352
1353 for (i=0; i < count; i++) {
1354#if K1212_DEBUG_LEVEL > 0
1355 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1356 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001357 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 -07001358 return -EFAULT;
1359 }
1360#endif
1361 rc = copy_from_user((void*) dst + offset, src, size);
1362 if (rc) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001363 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 -07001364 return -EFAULT;
1365 }
1366 dst++;
1367 src += size;
1368 }
1369
1370 return 0;
1371}
1372
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001373static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001375 struct snd_korg1212 *korg1212 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Takashi Iwai9fd91562005-11-17 10:45:48 +01001377 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1378 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 korg1212->pcm = NULL;
1381}
1382
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001383static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001386 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1387 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Takashi Iwai9fd91562005-11-17 10:45:48 +01001389 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1390 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 snd_pcm_set_sync(substream); // ???
1393
1394 snd_korg1212_OpenCard(korg1212);
1395
1396 runtime->hw = snd_korg1212_playback_info;
1397 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1398
1399 spin_lock_irqsave(&korg1212->lock, flags);
1400
1401 korg1212->playback_substream = substream;
1402 korg1212->playback_pid = current->pid;
1403 korg1212->periodsize = K1212_PERIODS;
1404 korg1212->channels = K1212_CHANNELS;
1405 korg1212->errorcnt = 0;
1406
1407 spin_unlock_irqrestore(&korg1212->lock, flags);
1408
1409 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1410 return 0;
1411}
1412
1413
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001414static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001417 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1418 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Takashi Iwai9fd91562005-11-17 10:45:48 +01001420 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1421 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 snd_pcm_set_sync(substream);
1424
1425 snd_korg1212_OpenCard(korg1212);
1426
1427 runtime->hw = snd_korg1212_capture_info;
1428 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1429
1430 spin_lock_irqsave(&korg1212->lock, flags);
1431
1432 korg1212->capture_substream = substream;
1433 korg1212->capture_pid = current->pid;
1434 korg1212->periodsize = K1212_PERIODS;
1435 korg1212->channels = K1212_CHANNELS;
1436
1437 spin_unlock_irqrestore(&korg1212->lock, flags);
1438
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001439 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1440 kPlayBufferFrames, kPlayBufferFrames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442}
1443
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001444static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001447 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Takashi Iwai9fd91562005-11-17 10:45:48 +01001449 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1450 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1453
1454 spin_lock_irqsave(&korg1212->lock, flags);
1455
1456 korg1212->playback_pid = -1;
1457 korg1212->playback_substream = NULL;
1458 korg1212->periodsize = 0;
1459
1460 spin_unlock_irqrestore(&korg1212->lock, flags);
1461
1462 snd_korg1212_CloseCard(korg1212);
1463 return 0;
1464}
1465
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001466static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001469 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Takashi Iwai9fd91562005-11-17 10:45:48 +01001471 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1472 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 spin_lock_irqsave(&korg1212->lock, flags);
1475
1476 korg1212->capture_pid = -1;
1477 korg1212->capture_substream = NULL;
1478 korg1212->periodsize = 0;
1479
1480 spin_unlock_irqrestore(&korg1212->lock, flags);
1481
1482 snd_korg1212_CloseCard(korg1212);
1483 return 0;
1484}
1485
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001486static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 unsigned int cmd, void *arg)
1488{
Takashi Iwai9fd91562005-11-17 10:45:48 +01001489 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001492 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 info->offset = 0;
1494 info->first = info->channel * 16;
1495 info->step = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 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 -07001497 return 0;
1498 }
1499
1500 return snd_pcm_lib_ioctl(substream, cmd, arg);
1501}
1502
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001503static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1504 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001507 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int err;
1509 pid_t this_pid;
1510 pid_t other_pid;
1511
Takashi Iwai9fd91562005-11-17 10:45:48 +01001512 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1513 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 spin_lock_irqsave(&korg1212->lock, flags);
1516
1517 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1518 this_pid = korg1212->playback_pid;
1519 other_pid = korg1212->capture_pid;
1520 } else {
1521 this_pid = korg1212->capture_pid;
1522 other_pid = korg1212->playback_pid;
1523 }
1524
1525 if ((other_pid > 0) && (this_pid != other_pid)) {
1526
1527 /* The other stream is open, and not by the same
1528 task as this one. Make sure that the parameters
1529 that matter are the same.
1530 */
1531
1532 if ((int)params_rate(params) != korg1212->clkRate) {
1533 spin_unlock_irqrestore(&korg1212->lock, flags);
1534 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1535 return -EBUSY;
1536 }
1537
1538 spin_unlock_irqrestore(&korg1212->lock, flags);
1539 return 0;
1540 }
1541
1542 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1543 spin_unlock_irqrestore(&korg1212->lock, flags);
1544 return err;
1545 }
1546
1547 korg1212->channels = params_channels(params);
1548 korg1212->periodsize = K1212_PERIOD_BYTES;
1549
1550 spin_unlock_irqrestore(&korg1212->lock, flags);
1551
1552 return 0;
1553}
1554
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001555static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001557 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int rc;
1559
Takashi Iwai9fd91562005-11-17 10:45:48 +01001560 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1561 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 spin_lock_irq(&korg1212->lock);
1564
1565 /* FIXME: we should wait for ack! */
1566 if (korg1212->stop_pending_cnt > 0) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001567 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1568 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 spin_unlock_irq(&korg1212->lock);
1570 return -EAGAIN;
1571 /*
1572 korg1212->sharedBufferPtr->cardCommand = 0;
1573 del_timer(&korg1212->timer);
1574 korg1212->stop_pending_cnt = 0;
1575 */
1576 }
1577
1578 rc = snd_korg1212_SetupForPlay(korg1212);
1579
1580 korg1212->currentBuffer = 0;
1581
1582 spin_unlock_irq(&korg1212->lock);
1583
1584 return rc ? -EINVAL : 0;
1585}
1586
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001587static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int cmd)
1589{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001590 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int rc;
1592
Takashi Iwai9fd91562005-11-17 10:45:48 +01001593 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1594 stateName[korg1212->cardState], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 spin_lock(&korg1212->lock);
1597 switch (cmd) {
1598 case SNDRV_PCM_TRIGGER_START:
1599/*
1600 if (korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001601 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603 }
1604*/
1605 korg1212->running++;
1606 rc = snd_korg1212_TriggerPlay(korg1212);
1607 break;
1608
1609 case SNDRV_PCM_TRIGGER_STOP:
1610/*
1611 if (!korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001612 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614 }
1615*/
1616 korg1212->running--;
1617 rc = snd_korg1212_StopPlay(korg1212);
1618 break;
1619
1620 default:
1621 rc = 1;
1622 break;
1623 }
1624 spin_unlock(&korg1212->lock);
1625 return rc ? -EINVAL : 0;
1626}
1627
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001628static snd_pcm_uframes_t snd_korg1212_playback_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_playback_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 snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001643 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 snd_pcm_uframes_t pos;
1645
1646 pos = korg1212->currentBuffer * kPlayBufferFrames;
1647
Takashi Iwai9fd91562005-11-17 10:45:48 +01001648 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1649 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 return pos;
1652}
1653
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001654static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int channel, /* not used (interleaved data) */
1656 snd_pcm_uframes_t pos,
1657 void __user *src,
1658 snd_pcm_uframes_t count)
1659{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001660 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Takashi Iwai9fd91562005-11-17 10:45:48 +01001662 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1663 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1666
1667}
1668
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001669static int snd_korg1212_playback_silence(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 snd_pcm_uframes_t count)
1673{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001674 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Takashi Iwai9fd91562005-11-17 10:45:48 +01001676 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1677 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1680}
1681
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001682static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int channel, /* not used (interleaved data) */
1684 snd_pcm_uframes_t pos,
1685 void __user *dst,
1686 snd_pcm_uframes_t count)
1687{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001688 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Takashi Iwai9fd91562005-11-17 10:45:48 +01001690 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1691 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1694}
1695
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001696static struct snd_pcm_ops snd_korg1212_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 .open = snd_korg1212_playback_open,
1698 .close = snd_korg1212_playback_close,
1699 .ioctl = snd_korg1212_ioctl,
1700 .hw_params = snd_korg1212_hw_params,
1701 .prepare = snd_korg1212_prepare,
1702 .trigger = snd_korg1212_trigger,
1703 .pointer = snd_korg1212_playback_pointer,
1704 .copy = snd_korg1212_playback_copy,
1705 .silence = snd_korg1212_playback_silence,
1706};
1707
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001708static struct snd_pcm_ops snd_korg1212_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .open = snd_korg1212_capture_open,
1710 .close = snd_korg1212_capture_close,
1711 .ioctl = snd_korg1212_ioctl,
1712 .hw_params = snd_korg1212_hw_params,
1713 .prepare = snd_korg1212_prepare,
1714 .trigger = snd_korg1212_trigger,
1715 .pointer = snd_korg1212_capture_pointer,
1716 .copy = snd_korg1212_capture_copy,
1717};
1718
1719/*
1720 * Control Interface
1721 */
1722
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001723static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1727 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1728 return 0;
1729}
1730
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001731static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1732 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001734 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int i = kcontrol->private_value;
1736
1737 spin_lock_irq(&korg1212->lock);
1738
1739 u->value.integer.value[0] = korg1212->volumePhase[i];
1740
1741 if (i >= 8)
1742 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1743
1744 spin_unlock_irq(&korg1212->lock);
1745
1746 return 0;
1747}
1748
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001749static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1750 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001752 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int change = 0;
1754 int i, val;
1755
1756 spin_lock_irq(&korg1212->lock);
1757
1758 i = kcontrol->private_value;
1759
1760 korg1212->volumePhase[i] = u->value.integer.value[0];
1761
1762 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1763
1764 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1765 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1766 korg1212->sharedBufferPtr->volumeData[i] = val;
1767 change = 1;
1768 }
1769
1770 if (i >= 8) {
1771 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1772
1773 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1774
1775 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1776 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1777 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1778 change = 1;
1779 }
1780 }
1781
1782 spin_unlock_irq(&korg1212->lock);
1783
1784 return change;
1785}
1786
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001787static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1791 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1792 uinfo->value.integer.min = k1212MinVolume;
1793 uinfo->value.integer.max = k1212MaxVolume;
1794 return 0;
1795}
1796
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001797static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1798 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001800 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int i;
1802
1803 spin_lock_irq(&korg1212->lock);
1804
1805 i = kcontrol->private_value;
1806 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1807
1808 if (i >= 8)
1809 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1810
1811 spin_unlock_irq(&korg1212->lock);
1812
1813 return 0;
1814}
1815
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001816static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001819 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int change = 0;
1821 int i;
1822 int val;
1823
1824 spin_lock_irq(&korg1212->lock);
1825
1826 i = kcontrol->private_value;
1827
1828 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1829 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1830 val *= u->value.integer.value[0];
1831 korg1212->sharedBufferPtr->volumeData[i] = val;
1832 change = 1;
1833 }
1834
1835 if (i >= 8) {
1836 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1837 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1838 val *= u->value.integer.value[1];
1839 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1840 change = 1;
1841 }
1842 }
1843
1844 spin_unlock_irq(&korg1212->lock);
1845
1846 return change;
1847}
1848
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001849static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1850 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1853 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1854 uinfo->value.enumerated.items = kAudioChannels;
1855 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1856 uinfo->value.enumerated.item = kAudioChannels-1;
1857 }
1858 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1859 return 0;
1860}
1861
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001862static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1863 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001865 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 int i;
1867
1868 spin_lock_irq(&korg1212->lock);
1869
1870 i = kcontrol->private_value;
1871 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1872
1873 if (i >= 8)
1874 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1875
1876 spin_unlock_irq(&korg1212->lock);
1877
1878 return 0;
1879}
1880
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001881static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1882 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001884 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 int change = 0, i;
1886
1887 spin_lock_irq(&korg1212->lock);
1888
1889 i = kcontrol->private_value;
1890
1891 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1892 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1893 change = 1;
1894 }
1895
1896 if (i >= 8) {
1897 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1898 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1899 change = 1;
1900 }
1901 }
1902
1903 spin_unlock_irq(&korg1212->lock);
1904
1905 return change;
1906}
1907
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001908static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1912 uinfo->count = 2;
1913 uinfo->value.integer.min = k1212MaxADCSens;
1914 uinfo->value.integer.max = k1212MinADCSens;
1915 return 0;
1916}
1917
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001918static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1919 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001921 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 spin_lock_irq(&korg1212->lock);
1924
1925 u->value.integer.value[0] = korg1212->leftADCInSens;
1926 u->value.integer.value[1] = korg1212->rightADCInSens;
1927
1928 spin_unlock_irq(&korg1212->lock);
1929
1930 return 0;
1931}
1932
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001933static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001936 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 int change = 0;
1938
1939 spin_lock_irq(&korg1212->lock);
1940
1941 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1942 korg1212->leftADCInSens = u->value.integer.value[0];
1943 change = 1;
1944 }
1945 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1946 korg1212->rightADCInSens = u->value.integer.value[1];
1947 change = 1;
1948 }
1949
1950 spin_unlock_irq(&korg1212->lock);
1951
1952 if (change)
1953 snd_korg1212_WriteADCSensitivity(korg1212);
1954
1955 return change;
1956}
1957
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001958static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1962 uinfo->count = 1;
1963 uinfo->value.enumerated.items = 3;
1964 if (uinfo->value.enumerated.item > 2) {
1965 uinfo->value.enumerated.item = 2;
1966 }
1967 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1968 return 0;
1969}
1970
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001971static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001974 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 spin_lock_irq(&korg1212->lock);
1977
1978 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1979
1980 spin_unlock_irq(&korg1212->lock);
1981 return 0;
1982}
1983
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001984static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001987 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 unsigned int val;
1989 int change;
1990
1991 val = ucontrol->value.enumerated.item[0] % 3;
1992 spin_lock_irq(&korg1212->lock);
1993 change = val != korg1212->clkSource;
1994 snd_korg1212_SetClockSource(korg1212, val);
1995 spin_unlock_irq(&korg1212->lock);
1996 return change;
1997}
1998
1999#define MON_MIXER(ord,c_name) \
2000 { \
2001 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2002 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2003 .name = c_name " Monitor Volume", \
2004 .info = snd_korg1212_control_volume_info, \
2005 .get = snd_korg1212_control_volume_get, \
2006 .put = snd_korg1212_control_volume_put, \
2007 .private_value = ord, \
2008 }, \
2009 { \
2010 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2011 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2012 .name = c_name " Monitor Route", \
2013 .info = snd_korg1212_control_route_info, \
2014 .get = snd_korg1212_control_route_get, \
2015 .put = snd_korg1212_control_route_put, \
2016 .private_value = ord, \
2017 }, \
2018 { \
2019 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 .name = c_name " Monitor Phase Invert", \
2022 .info = snd_korg1212_control_phase_info, \
2023 .get = snd_korg1212_control_phase_get, \
2024 .put = snd_korg1212_control_phase_put, \
2025 .private_value = ord, \
2026 }
2027
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002028static struct snd_kcontrol_new snd_korg1212_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 MON_MIXER(8, "Analog"),
2030 MON_MIXER(10, "SPDIF"),
2031 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2032 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2033 {
2034 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002035 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 .name = "Sync Source",
2037 .info = snd_korg1212_control_sync_info,
2038 .get = snd_korg1212_control_sync_get,
2039 .put = snd_korg1212_control_sync_put,
2040 },
2041 {
2042 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2043 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2044 .name = "ADC Attenuation",
2045 .info = snd_korg1212_control_info,
2046 .get = snd_korg1212_control_get,
2047 .put = snd_korg1212_control_put,
2048 }
2049};
2050
2051/*
2052 * proc interface
2053 */
2054
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002055static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2056 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 int n;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002059 struct snd_korg1212 *korg1212 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 snd_iprintf(buffer, korg1212->card->longname);
2062 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2063 snd_iprintf(buffer, "\nGeneral settings\n");
2064 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2065 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2066 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2067 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2068 snd_iprintf(buffer, " Volume Info:\n");
2069 for (n=0; n<kAudioChannels; n++)
2070 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2071 channelName[n],
2072 channelName[korg1212->sharedBufferPtr->routeData[n]],
2073 korg1212->sharedBufferPtr->volumeData[n]);
2074 snd_iprintf(buffer, "\nGeneral status\n");
2075 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2076 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2077 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2078 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2079 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2080 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2081}
2082
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002083static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002085 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2088 snd_info_set_text_ops(entry, korg1212, 1024, snd_korg1212_proc_read);
2089}
2090
2091static int
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002092snd_korg1212_free(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 snd_korg1212_TurnOffIdleMonitor(korg1212);
2095
2096 if (korg1212->irq >= 0) {
2097 synchronize_irq(korg1212->irq);
2098 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002164 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .dev_free = snd_korg1212_dev_free,
2166 };
2167
2168 * rchip = NULL;
2169 if ((err = pci_enable_device(pci)) < 0)
2170 return err;
2171
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002172 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (korg1212 == NULL) {
2174 pci_disable_device(pci);
2175 return -ENOMEM;
2176 }
2177
2178 korg1212->card = card;
2179 korg1212->pci = pci;
2180
2181 init_waitqueue_head(&korg1212->wait);
2182 spin_lock_init(&korg1212->lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002183 mutex_init(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 init_timer(&korg1212->timer);
2185 korg1212->timer.function = snd_korg1212_timer_func;
2186 korg1212->timer.data = (unsigned long)korg1212;
2187
2188 korg1212->irq = -1;
2189 korg1212->clkSource = K1212_CLKIDX_Local;
2190 korg1212->clkRate = 44100;
2191 korg1212->inIRQ = 0;
2192 korg1212->running = 0;
2193 korg1212->opencnt = 0;
2194 korg1212->playcnt = 0;
2195 korg1212->setcnt = 0;
2196 korg1212->totalerrorcnt = 0;
2197 korg1212->playback_pid = -1;
2198 korg1212->capture_pid = -1;
2199 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2200 korg1212->idleMonitorOn = 0;
2201 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2202 korg1212->leftADCInSens = k1212MaxADCSens;
2203 korg1212->rightADCInSens = k1212MaxADCSens;
2204
2205 for (i=0; i<kAudioChannels; i++)
2206 korg1212->volumePhase[i] = 0;
2207
2208 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2209 kfree(korg1212);
2210 pci_disable_device(pci);
2211 return err;
2212 }
2213
2214 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2215 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2216 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2217
2218 iomem_size = pci_resource_len(korg1212->pci, 0);
2219 ioport_size = pci_resource_len(korg1212->pci, 1);
2220 iomem2_size = pci_resource_len(korg1212->pci, 2);
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2223 " iomem = 0x%lx (%d)\n"
2224 " ioport = 0x%lx (%d)\n"
2225 " iomem = 0x%lx (%d)\n"
2226 " [%s]\n",
2227 korg1212->iomem, iomem_size,
2228 korg1212->ioport, ioport_size,
2229 korg1212->iomem2, iomem2_size,
2230 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2233 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2234 korg1212->iomem + iomem_size - 1);
2235 snd_korg1212_free(korg1212);
2236 return -EBUSY;
2237 }
2238
2239 err = request_irq(pci->irq, snd_korg1212_interrupt,
2240 SA_INTERRUPT|SA_SHIRQ,
Takashi Iwai9fd91562005-11-17 10:45:48 +01002241 "korg1212", korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 if (err) {
2244 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2245 snd_korg1212_free(korg1212);
2246 return -EBUSY;
2247 }
2248
2249 korg1212->irq = pci->irq;
2250
2251 pci_set_master(korg1212->pci);
2252
2253 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2254 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2255 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2256 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2257 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2258 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2259 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2260 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2261 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2262 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2265 " Status register = 0x%p\n"
2266 " OutDoorbell = 0x%p\n"
2267 " InDoorbell = 0x%p\n"
2268 " Mailbox0 = 0x%p\n"
2269 " Mailbox1 = 0x%p\n"
2270 " Mailbox2 = 0x%p\n"
2271 " Mailbox3 = 0x%p\n"
2272 " ControlReg = 0x%p\n"
2273 " SensReg = 0x%p\n"
2274 " IDReg = 0x%p\n"
2275 " [%s]\n",
2276 korg1212->statusRegPtr,
2277 korg1212->outDoorbellPtr,
2278 korg1212->inDoorbellPtr,
2279 korg1212->mailbox0Ptr,
2280 korg1212->mailbox1Ptr,
2281 korg1212->mailbox2Ptr,
2282 korg1212->mailbox3Ptr,
2283 korg1212->controlRegPtr,
2284 korg1212->sensRegPtr,
2285 korg1212->idRegPtr,
2286 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002289 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2290 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 snd_korg1212_free(korg1212);
2292 return -ENOMEM;
2293 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002294 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2296
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002297 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299#ifndef K1212_LARGEALLOC
2300
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002301 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2304 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2305 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2306 snd_korg1212_free(korg1212);
2307 return -ENOMEM;
2308 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002309 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2313 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2316 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2317 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2318 snd_korg1212_free(korg1212);
2319 return -ENOMEM;
2320 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002321 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2325 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327#else // K1212_LARGEALLOC
2328
2329 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2330 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002331 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2332 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334#endif // K1212_LARGEALLOC
2335
2336 korg1212->dspCodeSize = sizeof (dspCode);
2337
2338 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002339 offsetof(struct KorgSharedBuffer, volumeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002341 offsetof(struct KorgSharedBuffer, routeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002343 offsetof(struct KorgSharedBuffer, AdatTimeCode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2346 korg1212->dspCodeSize, &korg1212->dma_dsp) < 0) {
2347 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", korg1212->dspCodeSize);
2348 snd_korg1212_free(korg1212);
2349 return -ENOMEM;
2350 }
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2353 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, korg1212->dspCodeSize,
2354 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2357
Takashi Iwai9fd91562005-11-17 10:45:48 +01002358 if (rc)
2359 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2362 snd_korg1212_free(korg1212);
2363 return err;
2364 }
2365
2366 snd_korg1212_EnableCardInterrupts(korg1212);
2367
2368 mdelay(CARD_BOOT_DELAY_IN_MS);
2369
2370 if (snd_korg1212_downloadDSPCode(korg1212))
2371 return -EBUSY;
2372
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002373 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 "PlayDataPhy = %08x L[%08x]\n"
2375 "korg1212: RecDataPhy = %08x L[%08x], "
2376 "VolumeTablePhy = %08x L[%08x]\n"
2377 "korg1212: RoutingTablePhy = %08x L[%08x], "
2378 "AdatTimeCodePhy = %08x L[%08x]\n",
2379 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2380 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2381 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2382 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2383 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2384 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2385
2386 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2387 return err;
2388
2389 korg1212->pcm->private_data = korg1212;
2390 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2391 strcpy(korg1212->pcm->name, "korg1212");
2392
2393 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2394
2395 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2396
2397 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2400 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2401 if (err < 0)
2402 return err;
2403 }
2404
2405 snd_korg1212_proc_init(korg1212);
2406
2407 snd_card_set_dev(card, &pci->dev);
2408
2409 * rchip = korg1212;
2410 return 0;
2411
2412}
2413
2414/*
2415 * Card initialisation
2416 */
2417
2418static int __devinit
2419snd_korg1212_probe(struct pci_dev *pci,
2420 const struct pci_device_id *pci_id)
2421{
2422 static int dev;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002423 struct snd_korg1212 *korg1212;
2424 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 int err;
2426
2427 if (dev >= SNDRV_CARDS) {
2428 return -ENODEV;
2429 }
2430 if (!enable[dev]) {
2431 dev++;
2432 return -ENOENT;
2433 }
2434 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2435 if (card == NULL)
2436 return -ENOMEM;
2437
2438 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2439 snd_card_free(card);
2440 return err;
2441 }
2442
2443 strcpy(card->driver, "korg1212");
2444 strcpy(card->shortname, "korg1212");
2445 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2446 korg1212->iomem, korg1212->irq);
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 if ((err = snd_card_register(card)) < 0) {
2451 snd_card_free(card);
2452 return err;
2453 }
2454 pci_set_drvdata(pci, card);
2455 dev++;
2456 return 0;
2457}
2458
2459static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2460{
2461 snd_card_free(pci_get_drvdata(pci));
2462 pci_set_drvdata(pci, NULL);
2463}
2464
2465static struct pci_driver driver = {
2466 .name = "korg1212",
2467 .id_table = snd_korg1212_ids,
2468 .probe = snd_korg1212_probe,
2469 .remove = __devexit_p(snd_korg1212_remove),
2470};
2471
2472static int __init alsa_card_korg1212_init(void)
2473{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002474 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static void __exit alsa_card_korg1212_exit(void)
2478{
2479 pci_unregister_driver(&driver);
2480}
2481
2482module_init(alsa_card_korg1212_init)
2483module_exit(alsa_card_korg1212_exit)