blob: a8a6a5c41a7f57fbf2461de8bfa4afef4ed0f321 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the Korg 1212 IO PCI card
3 *
4 * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/slab.h>
28#include <linux/wait.h>
29#include <linux/moduleparam.h>
30
31#include <sound/core.h>
32#include <sound/info.h>
33#include <sound/control.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/initval.h>
37
38#include <asm/io.h>
39
40// ----------------------------------------------------------------------------
41// Debug Stuff
42// ----------------------------------------------------------------------------
43#define K1212_DEBUG_LEVEL 0
Takashi Iwai9fd91562005-11-17 10:45:48 +010044#if K1212_DEBUG_LEVEL > 0
45#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
46#else
47#define K1212_DEBUG_PRINTK(fmt,...)
48#endif
49#if K1212_DEBUG_LEVEL > 1
50#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
51#else
52#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
53#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55// ----------------------------------------------------------------------------
56// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
57// buffers are alocated as a large piece inside KorgSharedBuffer.
58// ----------------------------------------------------------------------------
59//#define K1212_LARGEALLOC 1
60
61// ----------------------------------------------------------------------------
62// Valid states of the Korg 1212 I/O card.
63// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010064enum CardState {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 K1212_STATE_NONEXISTENT, // there is no card here
66 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
67 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
68 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
69 K1212_STATE_READY, // the card can be opened by an application. Any application
70 // requests prior to this state should fail. Only an open
71 // request can be made at this state.
72 K1212_STATE_OPEN, // an application has opened the card
73 K1212_STATE_SETUP, // the card has been setup for play
74 K1212_STATE_PLAYING, // the card is playing
75 K1212_STATE_MONITOR, // the card is in the monitor mode
76 K1212_STATE_CALIBRATING, // the card is currently calibrating
77 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
78 // are in the process of cleaning things up.
79 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
Takashi Iwaifcfd3332005-11-17 15:00:46 +010080};
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82// ----------------------------------------------------------------------------
83// The following enumeration defines the constants written to the card's
84// host-to-card doorbell to initiate a command.
85// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010086enum korg1212_dbcnst {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
88 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
89 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
90 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
91 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
92 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
93 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
94 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
95 // timecode value.
96 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
97 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
98 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
99 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
100 // on page 4 (local page to card).
101 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
102 // completed.
103 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106
107// ----------------------------------------------------------------------------
108// The following enumeration defines return codes
109// to the Korg 1212 I/O driver.
110// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100111enum snd_korg1212rc {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 K1212_CMDRET_Success = 0, // command was successfully placed
113 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
114 K1212_CMDRET_PMFailure, // the protected mode call failed
115 K1212_CMDRET_FailUnspecified, // unspecified failure
116 K1212_CMDRET_FailBadState, // the specified command can not be given in
117 // the card's current state. (or the wave device's
118 // state)
119 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
120 K1212_CMDRET_BadIndex, // an out of range card index was specified
121 K1212_CMDRET_BadHandle, // an invalid card handle was specified
122 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
123 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
124 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
125 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
126
127 K1212_CMDRET_BadDevice, // the specified wave device was out of range
128 K1212_CMDRET_BadFormat // the specified wave format is unsupported
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131// ----------------------------------------------------------------------------
132// The following enumeration defines the constants used to select the play
133// mode for the card in the SelectPlayMode command.
134// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100135enum PlayModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
137 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
138 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
139 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100140};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142// ----------------------------------------------------------------------------
143// The following enumeration defines the constants used to select the monitor
144// mode for the card in the SetMonitorMode command.
145// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100146enum MonitorModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
148 K1212_MONMODE_On // tells card to turn on monitor mode
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100149};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
152#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
153#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
154#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
155#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
156#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
157#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
158#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
159 // register
160#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
161 // this is the upper word of the PCI control reg.
162#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
163
164#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
165 // from the card after sending a command.
166#define INTERCOMMAND_DELAY 40
167#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
168 // to send a command before giving up.
169#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
170 // the card.
171#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
172
173#define CARD_BOOT_DELAY_IN_MS 10
174#define CARD_BOOT_TIMEOUT 10
175#define DSP_BOOT_DELAY_IN_MS 200
176
177#define kNumBuffers 8
178#define k1212MaxCards 4
179#define k1212NumWaveDevices 6
180#define k16BitChannels 10
181#define k32BitChannels 2
182#define kAudioChannels (k16BitChannels + k32BitChannels)
183#define kPlayBufferFrames 1024
184
185#define K1212_ANALOG_CHANNELS 2
186#define K1212_SPDIF_CHANNELS 2
187#define K1212_ADAT_CHANNELS 8
188#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
189#define K1212_MIN_CHANNELS 1
190#define K1212_MAX_CHANNELS K1212_CHANNELS
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100191#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
193#define K1212_PERIODS (kNumBuffers)
194#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
195#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
196#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
197#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
198#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
199#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
200
201#define k1212MinADCSens 0x7f
202#define k1212MaxADCSens 0x00
203#define k1212MaxVolume 0x7fff
204#define k1212MaxWaveVolume 0xffff
205#define k1212MinVolume 0x0000
206#define k1212MaxVolInverted 0x8000
207
208// -----------------------------------------------------------------
209// the following bits are used for controlling interrupts in the
210// interrupt control/status reg
211// -----------------------------------------------------------------
212#define PCI_INT_ENABLE_BIT 0x00000100
213#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
214#define LOCAL_INT_ENABLE_BIT 0x00010000
215#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
216#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
217
218// -----------------------------------------------------------------
219// the following bits are defined for the PCI command register
220// -----------------------------------------------------------------
221#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
222#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
223#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
224
225// -----------------------------------------------------------------
226// the following bits are defined for the PCI status register
227// -----------------------------------------------------------------
228#define PCI_STAT_PARITY_ERROR_BIT 0x8000
229#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
230#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
231#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
232#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
233
234// ------------------------------------------------------------------------
235// the following constants are used in setting the 1212 I/O card's input
236// sensitivity.
237// ------------------------------------------------------------------------
238#define SET_SENS_LOCALINIT_BITPOS 15
239#define SET_SENS_DATA_BITPOS 10
240#define SET_SENS_CLOCK_BITPOS 8
241#define SET_SENS_LOADSHIFT_BITPOS 0
242
243#define SET_SENS_LEFTCHANID 0x00
244#define SET_SENS_RIGHTCHANID 0x01
245
246#define K1212SENSUPDATE_DELAY_IN_MS 50
247
248// --------------------------------------------------------------------------
249// WaitRTCTicks
250//
251// This function waits the specified number of real time clock ticks.
252// According to the DDK, each tick is ~0.8 microseconds.
253// The defines following the function declaration can be used for the
254// numTicksToWait parameter.
255// --------------------------------------------------------------------------
256#define ONE_RTC_TICK 1
257#define SENSCLKPULSE_WIDTH 4
258#define LOADSHIFT_DELAY 4
259#define INTERCOMMAND_DELAY 40
260#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
261 // the command register. (could be up to 180 us)
262#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
263 // from the card after sending a command.
264
265#include "korg1212-firmware.h"
266
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100267enum ClockSourceIndex {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
269 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
270 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
271 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
272 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
273 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
274 K1212_CLKIDX_Invalid // used to check validity of the index
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100277enum ClockSourceType {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 K1212_CLKIDX_Adat = 0, // selects source as ADAT
279 K1212_CLKIDX_Word, // selects source as S/PDIF
280 K1212_CLKIDX_Local // selects source as local clock
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100281};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100283struct KorgAudioFrame {
284 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
285 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
286 u32 timeCodeVal; /* holds the ADAT timecode value */
287};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100289struct KorgAudioBuffer {
290 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
291};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100293struct KorgSharedBuffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#ifdef K1212_LARGEALLOC
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100295 struct KorgAudioBuffer playDataBufs[kNumBuffers];
296 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif
298 short volumeData[kAudioChannels];
299 u32 cardCommand;
300 u16 routeData [kAudioChannels];
301 u32 AdatTimeCode; // ADAT timecode value
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100302};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100304struct SensBits {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 union {
306 struct {
307 unsigned int leftChanVal:8;
308 unsigned int leftChanId:8;
309 } v;
310 u16 leftSensBits;
311 } l;
312 union {
313 struct {
314 unsigned int rightChanVal:8;
315 unsigned int rightChanId:8;
316 } v;
317 u16 rightSensBits;
318 } r;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100319};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100321struct snd_korg1212 {
322 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct pci_dev *pci;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100324 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int irq;
326
327 spinlock_t lock;
328 struct semaphore open_mutex;
329
330 struct timer_list timer; /* timer callback for checking ack of stop request */
331 int stop_pending_cnt; /* counter for stop pending check */
332
333 wait_queue_head_t wait;
334
335 unsigned long iomem;
336 unsigned long ioport;
337 unsigned long iomem2;
338 unsigned long irqcount;
339 unsigned long inIRQ;
340 void __iomem *iobase;
341
342 struct snd_dma_buffer dma_dsp;
343 struct snd_dma_buffer dma_play;
344 struct snd_dma_buffer dma_rec;
345 struct snd_dma_buffer dma_shared;
346
347 u32 dspCodeSize;
348
349 u32 DataBufsSize;
350
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100351 struct KorgAudioBuffer * playDataBufsPtr;
352 struct KorgAudioBuffer * recordDataBufsPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100354 struct KorgSharedBuffer * sharedBufferPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 u32 RecDataPhy;
357 u32 PlayDataPhy;
358 unsigned long sharedBufferPhy;
359 u32 VolumeTablePhy;
360 u32 RoutingTablePhy;
361 u32 AdatTimeCodePhy;
362
363 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
364 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
365 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
366 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
367 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
368 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
369 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
370 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
371 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
372 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
373
374 size_t periodsize;
375 int channels;
376 int currentBuffer;
377
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100378 struct snd_pcm_substream *playback_substream;
379 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 pid_t capture_pid;
382 pid_t playback_pid;
383
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100384 enum CardState cardState;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int running;
386 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
387 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
388
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100389 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100391 enum ClockSourceType clkSource; // clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int clkRate; // clock rate
393
394 int volumePhase[kAudioChannels];
395
396 u16 leftADCInSens; // ADC left channel input sensitivity
397 u16 rightADCInSens; // ADC right channel input sensitivity
398
399 int opencnt; // Open/Close count
400 int setcnt; // SetupForPlay count
401 int playcnt; // TriggerPlay count
402 int errorcnt; // Error Count
403 unsigned long totalerrorcnt; // Total Error Count
404
405 int dsp_is_loaded;
406 int dsp_stop_is_processed;
407
408};
409
410MODULE_DESCRIPTION("korg1212");
411MODULE_LICENSE("GPL");
412MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
413
414static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
415static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
416static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
417
418module_param_array(index, int, NULL, 0444);
419MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
420module_param_array(id, charp, NULL, 0444);
421MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
422module_param_array(enable, bool, NULL, 0444);
423MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
424MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
425
426static struct pci_device_id snd_korg1212_ids[] = {
427 {
428 .vendor = 0x10b5,
429 .device = 0x906d,
430 .subvendor = PCI_ANY_ID,
431 .subdevice = PCI_ANY_ID,
432 },
433 { 0, },
434};
435
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100436MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
437
Takashi Iwai9fd91562005-11-17 10:45:48 +0100438static char *stateName[] = {
439 "Non-existent",
440 "Uninitialized",
441 "DSP download in process",
442 "DSP download complete",
443 "Ready",
444 "Open",
445 "Setup for play",
446 "Playing",
447 "Monitor mode on",
448 "Calibrating",
449 "Invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450};
451
Takashi Iwai9fd91562005-11-17 10:45:48 +0100452static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Takashi Iwai9fd91562005-11-17 10:45:48 +0100454static char *clockSourceName[] = {
455 "ADAT at 44.1 kHz",
456 "ADAT at 48 kHz",
457 "S/PDIF at 44.1 kHz",
458 "S/PDIF at 48 kHz",
459 "local clock at 44.1 kHz",
460 "local clock at 48 kHz"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
Takashi Iwai9fd91562005-11-17 10:45:48 +0100463static char *channelName[] = {
464 "ADAT-1",
465 "ADAT-2",
466 "ADAT-3",
467 "ADAT-4",
468 "ADAT-5",
469 "ADAT-6",
470 "ADAT-7",
471 "ADAT-8",
472 "Analog-L",
473 "Analog-R",
474 "SPDIF-L",
475 "SPDIF-R",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
Takashi Iwai9fd91562005-11-17 10:45:48 +0100478static u16 ClockSourceSelector[] = {
479 0x8000, // selects source as ADAT at 44.1 kHz
480 0x0000, // selects source as ADAT at 48 kHz
481 0x8001, // selects source as S/PDIF at 44.1 kHz
482 0x0001, // selects source as S/PDIF at 48 kHz
483 0x8002, // selects source as local clock at 44.1 kHz
484 0x0002 // selects source as local clock at 48 kHz
485};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100487union swap_u32 { unsigned char c[4]; u32 i; };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489#ifdef SNDRV_BIG_ENDIAN
490static u32 LowerWordSwap(u32 swappee)
491#else
492static u32 UpperWordSwap(u32 swappee)
493#endif
494{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100495 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 swapper.i = swappee;
498 retVal.c[2] = swapper.c[3];
499 retVal.c[3] = swapper.c[2];
500 retVal.c[1] = swapper.c[1];
501 retVal.c[0] = swapper.c[0];
502
503 return retVal.i;
504}
505
506#ifdef SNDRV_BIG_ENDIAN
507static u32 UpperWordSwap(u32 swappee)
508#else
509static u32 LowerWordSwap(u32 swappee)
510#endif
511{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100512 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 swapper.i = swappee;
515 retVal.c[2] = swapper.c[2];
516 retVal.c[3] = swapper.c[3];
517 retVal.c[1] = swapper.c[0];
518 retVal.c[0] = swapper.c[1];
519
520 return retVal.i;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
524#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
525#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
526#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
527
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100528static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
529 enum korg1212_dbcnst doorbellVal,
530 u32 mailBox0Val, u32 mailBox1Val,
531 u32 mailBox2Val, u32 mailBox3Val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 u32 retryCount;
534 u16 mailBox3Lo;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100535 int rc = K1212_CMDRET_Success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (!korg1212->outDoorbellPtr) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100538 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return K1212_CMDRET_CardUninitialized;
540 }
541
Takashi Iwai9fd91562005-11-17 10:45:48 +0100542 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
543 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
545 writel(mailBox3Val, korg1212->mailbox3Ptr);
546 writel(mailBox2Val, korg1212->mailbox2Ptr);
547 writel(mailBox1Val, korg1212->mailbox1Ptr);
548 writel(mailBox0Val, korg1212->mailbox0Ptr);
549 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
550
551 // --------------------------------------------------------------
552 // the reboot command will not give an acknowledgement.
553 // --------------------------------------------------------------
554 if ( doorbellVal == K1212_DB_RebootCard ||
555 doorbellVal == K1212_DB_BootFromDSPPage4 ||
556 doorbellVal == K1212_DB_StartDSPDownload ) {
557 rc = K1212_CMDRET_Success;
558 break;
559 }
560
561 // --------------------------------------------------------------
562 // See if the card acknowledged the command. Wait a bit, then
563 // read in the low word of mailbox3. If the MSB is set and the
564 // low byte is equal to the doorbell value, then it ack'd.
565 // --------------------------------------------------------------
566 udelay(COMMAND_ACK_DELAY);
567 mailBox3Lo = readl(korg1212->mailbox3Ptr);
568 if (mailBox3Lo & COMMAND_ACK_MASK) {
569 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100570 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 rc = K1212_CMDRET_Success;
572 break;
573 }
574 }
575 }
576 korg1212->cmdRetryCount += retryCount;
577
578 if (retryCount >= MAX_COMMAND_RETRIES) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100579 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 rc = K1212_CMDRET_NoAckFromCard;
581 }
582
583 return rc;
584}
585
586/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100587static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 if (! korg1212->stop_pending_cnt) {
590 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
591 /* program the timer */
592 korg1212->stop_pending_cnt = HZ;
593 korg1212->timer.expires = jiffies + 1;
594 add_timer(&korg1212->timer);
595 }
596}
597
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100598static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 unsigned long flags;
601 spin_lock_irqsave(&korg1212->lock, flags);
602 korg1212->dsp_stop_is_processed = 0;
603 snd_korg1212_SendStop(korg1212);
604 spin_unlock_irqrestore(&korg1212->lock, flags);
605 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
606}
607
608/* timer callback for checking the ack of stop request */
609static void snd_korg1212_timer_func(unsigned long data)
610{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100611 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 spin_lock(&korg1212->lock);
614 if (korg1212->sharedBufferPtr->cardCommand == 0) {
615 /* ack'ed */
616 korg1212->stop_pending_cnt = 0;
617 korg1212->dsp_stop_is_processed = 1;
618 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100619 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
620 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 } else {
622 if (--korg1212->stop_pending_cnt > 0) {
623 /* reprogram timer */
624 korg1212->timer.expires = jiffies + 1;
625 add_timer(&korg1212->timer);
626 } else {
627 snd_printd("korg1212_timer_func timeout\n");
628 korg1212->sharedBufferPtr->cardCommand = 0;
629 korg1212->dsp_stop_is_processed = 1;
630 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100631 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
632 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 }
635 spin_unlock(&korg1212->lock);
636}
637
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100638static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 unsigned long flags;
Takashi Iwai9fd91562005-11-17 10:45:48 +0100641 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 udelay(INTERCOMMAND_DELAY);
644 spin_lock_irqsave(&korg1212->lock, flags);
645 korg1212->idleMonitorOn = 1;
646 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
647 K1212_MODE_MonitorOn, 0, 0, 0);
648 spin_unlock_irqrestore(&korg1212->lock, flags);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100649 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100652static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 if (korg1212->idleMonitorOn) {
655 snd_korg1212_SendStopAndWait(korg1212);
656 korg1212->idleMonitorOn = 0;
657 }
658}
659
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100660static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 korg1212->cardState = csState;
663}
664
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100665static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100667 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
668 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 down(&korg1212->open_mutex);
670 if (korg1212->opencnt++ == 0) {
671 snd_korg1212_TurnOffIdleMonitor(korg1212);
672 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
673 }
674
675 up(&korg1212->open_mutex);
676 return 1;
677}
678
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100679static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100681 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
682 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 down(&korg1212->open_mutex);
685 if (--(korg1212->opencnt)) {
686 up(&korg1212->open_mutex);
687 return 0;
688 }
689
690 if (korg1212->cardState == K1212_STATE_SETUP) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100691 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 K1212_MODE_StopPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100693 if (rc)
694 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
695 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (rc != K1212_CMDRET_Success) {
697 up(&korg1212->open_mutex);
698 return 0;
699 }
700 } else if (korg1212->cardState > K1212_STATE_SETUP) {
701 snd_korg1212_SendStopAndWait(korg1212);
702 }
703
704 if (korg1212->cardState > K1212_STATE_READY) {
705 snd_korg1212_TurnOnIdleMonitor(korg1212);
706 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
707 }
708
709 up(&korg1212->open_mutex);
710 return 0;
711}
712
713/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100714static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100716 int rc;
717
718 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
719 stateName[korg1212->cardState], korg1212->setcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (korg1212->setcnt++)
722 return 0;
723
724 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
725 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
726 K1212_MODE_SetupPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100727 if (rc)
728 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
729 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (rc != K1212_CMDRET_Success) {
731 return 1;
732 }
733 return 0;
734}
735
736/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100737static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100739 int rc;
740
741 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
742 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (korg1212->playcnt++)
745 return 0;
746
747 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
748 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100749 if (rc)
750 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
751 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (rc != K1212_CMDRET_Success) {
753 return 1;
754 }
755 return 0;
756}
757
758/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100759static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100761 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
762 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (--(korg1212->playcnt))
765 return 0;
766
767 korg1212->setcnt = 0;
768
769 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
770 snd_korg1212_SendStop(korg1212);
771
772 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
773 return 0;
774}
775
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100776static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 writel(PCI_INT_ENABLE_BIT |
779 PCI_DOORBELL_INT_ENABLE_BIT |
780 LOCAL_INT_ENABLE_BIT |
781 LOCAL_DOORBELL_INT_ENABLE_BIT |
782 LOCAL_DMA1_INT_ENABLE_BIT,
783 korg1212->statusRegPtr);
784}
785
786#if 0 /* not used */
787
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100788static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
789 enum MonitorModeSelector mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100791 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
792 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 switch (mode) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100795 case K1212_MONMODE_Off:
796 if (korg1212->cardState != K1212_STATE_MONITOR)
797 return 0;
798 else {
799 snd_korg1212_SendStopAndWait(korg1212);
800 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
801 }
802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Takashi Iwai9fd91562005-11-17 10:45:48 +0100804 case K1212_MONMODE_On:
805 if (korg1212->cardState != K1212_STATE_OPEN)
806 return 0;
807 else {
808 int rc;
809 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
810 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
811 K1212_MODE_MonitorOn, 0, 0, 0);
812 if (rc != K1212_CMDRET_Success)
813 return 0;
814 }
815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Takashi Iwai9fd91562005-11-17 10:45:48 +0100817 default:
818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 return 1;
822}
823
824#endif /* not used */
825
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100826static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100828 if (korg1212->playback_pid != korg1212->capture_pid &&
829 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Takashi Iwai9fd91562005-11-17 10:45:48 +0100832 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100835static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100837 static enum ClockSourceIndex s44[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100838 K1212_CLKIDX_AdatAt44_1K,
839 K1212_CLKIDX_WordAt44_1K,
840 K1212_CLKIDX_LocalAt44_1K
841 };
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100842 static enum ClockSourceIndex s48[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100843 K1212_CLKIDX_AdatAt48K,
844 K1212_CLKIDX_WordAt48K,
845 K1212_CLKIDX_LocalAt48K
846 };
847 int parm, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Takashi Iwai9fd91562005-11-17 10:45:48 +0100849 if (!snd_korg1212_use_is_exclusive (korg1212))
850 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100852 switch (rate) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100853 case 44100:
854 parm = s44[korg1212->clkSource];
855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Takashi Iwai9fd91562005-11-17 10:45:48 +0100857 case 48000:
858 parm = s48[korg1212->clkSource];
859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Takashi Iwai9fd91562005-11-17 10:45:48 +0100861 default:
862 return -EINVAL;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 korg1212->clkSrcRate = parm;
866 korg1212->clkRate = rate;
867
868 udelay(INTERCOMMAND_DELAY);
869 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
870 ClockSourceSelector[korg1212->clkSrcRate],
871 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100872 if (rc)
873 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
874 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 return 0;
877}
878
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100879static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881
Takashi Iwai9fd91562005-11-17 10:45:48 +0100882 if (source < 0 || source > 2)
883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 korg1212->clkSource = source;
886
887 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
888
889 return 0;
890}
891
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100892static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 writel(0, korg1212->statusRegPtr);
895}
896
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100897static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100899 struct SensBits sensVals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 int bitPosition;
901 int channel;
902 int clkIs48K;
903 int monModeSet;
904 u16 controlValue; // this keeps the current value to be written to
905 // the card's eeprom control register.
906 u16 count;
907 unsigned long flags;
908
Takashi Iwai9fd91562005-11-17 10:45:48 +0100909 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
910 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 // ----------------------------------------------------------------------------
913 // initialize things. The local init bit is always set when writing to the
914 // card's control register.
915 // ----------------------------------------------------------------------------
916 controlValue = 0;
917 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
918
919 // ----------------------------------------------------------------------------
920 // make sure the card is not in monitor mode when we do this update.
921 // ----------------------------------------------------------------------------
922 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
923 monModeSet = 1;
924 snd_korg1212_SendStopAndWait(korg1212);
925 } else
926 monModeSet = 0;
927
928 spin_lock_irqsave(&korg1212->lock, flags);
929
930 // ----------------------------------------------------------------------------
931 // we are about to send new values to the card, so clear the new values queued
932 // flag. Also, clear out mailbox 3, so we don't lockup.
933 // ----------------------------------------------------------------------------
934 writel(0, korg1212->mailbox3Ptr);
935 udelay(LOADSHIFT_DELAY);
936
937 // ----------------------------------------------------------------------------
938 // determine whether we are running a 48K or 44.1K clock. This info is used
939 // later when setting the SPDIF FF after the volume has been shifted in.
940 // ----------------------------------------------------------------------------
941 switch (korg1212->clkSrcRate) {
942 case K1212_CLKIDX_AdatAt44_1K:
943 case K1212_CLKIDX_WordAt44_1K:
944 case K1212_CLKIDX_LocalAt44_1K:
945 clkIs48K = 0;
946 break;
947
948 case K1212_CLKIDX_WordAt48K:
949 case K1212_CLKIDX_AdatAt48K:
950 case K1212_CLKIDX_LocalAt48K:
951 default:
952 clkIs48K = 1;
953 break;
954 }
955
956 // ----------------------------------------------------------------------------
957 // start the update. Setup the bit structure and then shift the bits.
958 // ----------------------------------------------------------------------------
959 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
960 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
961 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
962 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
963
964 // ----------------------------------------------------------------------------
965 // now start shifting the bits in. Start with the left channel then the right.
966 // ----------------------------------------------------------------------------
967 for (channel = 0; channel < 2; channel++) {
968
969 // ----------------------------------------------------------------------------
970 // Bring the load/shift line low, then wait - the spec says >150ns from load/
971 // shift low to the first rising edge of the clock.
972 // ----------------------------------------------------------------------------
973 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
974 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
975 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
976 udelay(LOADSHIFT_DELAY);
977
978 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
Takashi Iwai9fd91562005-11-17 10:45:48 +0100979 if (channel == 0) {
980 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100981 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
Takashi Iwai9fd91562005-11-17 10:45:48 +0100982 else
983 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
984 } else {
985 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
986 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
987 else
988 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
992 writew(controlValue, korg1212->sensRegPtr); // clock goes low
993 udelay(SENSCLKPULSE_WIDTH);
994 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
995 writew(controlValue, korg1212->sensRegPtr); // clock goes high
996 udelay(SENSCLKPULSE_WIDTH);
997 }
998
999 // ----------------------------------------------------------------------------
1000 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1001 // bit if the clock rate is 48K otherwise write 0.
1002 // ----------------------------------------------------------------------------
1003 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1004 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1005 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1006 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1007 udelay(SENSCLKPULSE_WIDTH);
1008
1009 if (clkIs48K)
1010 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1011
1012 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1013 udelay(ONE_RTC_TICK);
1014 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1015 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1016 udelay(SENSCLKPULSE_WIDTH);
1017 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1018 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1019 udelay(SENSCLKPULSE_WIDTH);
1020 }
1021
1022 // ----------------------------------------------------------------------------
1023 // The update is complete. Set a timeout. This is the inter-update delay.
1024 // Also, if the card was in monitor mode, restore it.
1025 // ----------------------------------------------------------------------------
1026 for (count = 0; count < 10; count++)
1027 udelay(SENSCLKPULSE_WIDTH);
1028
1029 if (monModeSet) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001030 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 K1212_MODE_MonitorOn, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001032 if (rc)
1033 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1034 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 spin_unlock_irqrestore(&korg1212->lock, flags);
1038
1039 return 1;
1040}
1041
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001042static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001044 int channel, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Takashi Iwai9fd91562005-11-17 10:45:48 +01001046 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1047 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 // ----------------------------------------------------
1050 // tell the card to boot
1051 // ----------------------------------------------------
1052 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1053
Takashi Iwai9fd91562005-11-17 10:45:48 +01001054 if (rc)
1055 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1056 rc, stateName[korg1212->cardState]);
1057 msleep(DSP_BOOT_DELAY_IN_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 // --------------------------------------------------------------------------------
1060 // Let the card know where all the buffers are.
1061 // --------------------------------------------------------------------------------
1062 rc = snd_korg1212_Send1212Command(korg1212,
1063 K1212_DB_ConfigureBufferMemory,
1064 LowerWordSwap(korg1212->PlayDataPhy),
1065 LowerWordSwap(korg1212->RecDataPhy),
1066 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1067 // is based on 2 buffers
1068 0
1069 );
1070
Takashi Iwai9fd91562005-11-17 10:45:48 +01001071 if (rc)
1072 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1073 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 udelay(INTERCOMMAND_DELAY);
1076
1077 rc = snd_korg1212_Send1212Command(korg1212,
1078 K1212_DB_ConfigureMiscMemory,
1079 LowerWordSwap(korg1212->VolumeTablePhy),
1080 LowerWordSwap(korg1212->RoutingTablePhy),
1081 LowerWordSwap(korg1212->AdatTimeCodePhy),
1082 0
1083 );
1084
Takashi Iwai9fd91562005-11-17 10:45:48 +01001085 if (rc)
1086 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1087 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 // --------------------------------------------------------------------------------
1090 // Initialize the routing and volume tables, then update the card's state.
1091 // --------------------------------------------------------------------------------
1092 udelay(INTERCOMMAND_DELAY);
1093
1094 for (channel = 0; channel < kAudioChannels; channel++) {
1095 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1096 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1097 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1098 }
1099
1100 snd_korg1212_WriteADCSensitivity(korg1212);
1101
1102 udelay(INTERCOMMAND_DELAY);
1103 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1104 ClockSourceSelector[korg1212->clkSrcRate],
1105 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001106 if (rc)
1107 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1108 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Takashi Iwai9fd91562005-11-17 10:45:48 +01001110 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1112
Takashi Iwai9fd91562005-11-17 10:45:48 +01001113 if (rc)
1114 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1115 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1118}
1119
1120static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1121{
1122 u32 doorbellValue;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001123 struct snd_korg1212 *korg1212 = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if(irq != korg1212->irq)
1126 return IRQ_NONE;
1127
1128 doorbellValue = readl(korg1212->inDoorbellPtr);
1129
1130 if (!doorbellValue)
1131 return IRQ_NONE;
1132
1133 spin_lock(&korg1212->lock);
1134
1135 writel(doorbellValue, korg1212->inDoorbellPtr);
1136
1137 korg1212->irqcount++;
1138
1139 korg1212->inIRQ++;
1140
1141
1142 switch (doorbellValue) {
1143 case K1212_DB_DSPDownloadDone:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001144 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1145 korg1212->irqcount, doorbellValue,
1146 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1148 korg1212->dsp_is_loaded = 1;
1149 wake_up(&korg1212->wait);
1150 }
1151 break;
1152
1153 // ------------------------------------------------------------------------
1154 // an error occurred - stop the card
1155 // ------------------------------------------------------------------------
1156 case K1212_DB_DMAERROR:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001157 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1158 korg1212->irqcount, doorbellValue,
1159 stateName[korg1212->cardState]);
1160 snd_printk(KERN_ERR "korg1212: DMA Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 korg1212->errorcnt++;
1162 korg1212->totalerrorcnt++;
1163 korg1212->sharedBufferPtr->cardCommand = 0;
1164 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1165 break;
1166
1167 // ------------------------------------------------------------------------
1168 // the card has stopped by our request. Clear the command word and signal
1169 // the semaphore in case someone is waiting for this.
1170 // ------------------------------------------------------------------------
1171 case K1212_DB_CARDSTOPPED:
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001172 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
Takashi Iwai9fd91562005-11-17 10:45:48 +01001173 korg1212->irqcount, doorbellValue,
1174 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 korg1212->sharedBufferPtr->cardCommand = 0;
1176 break;
1177
1178 default:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001179 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001180 korg1212->irqcount, doorbellValue,
1181 korg1212->currentBuffer, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1183 korg1212->currentBuffer++;
1184
1185 if (korg1212->currentBuffer >= kNumBuffers)
1186 korg1212->currentBuffer = 0;
1187
1188 if (!korg1212->running)
1189 break;
1190
1191 if (korg1212->capture_substream) {
1192 spin_unlock(&korg1212->lock);
1193 snd_pcm_period_elapsed(korg1212->capture_substream);
1194 spin_lock(&korg1212->lock);
1195 }
1196
1197 if (korg1212->playback_substream) {
1198 spin_unlock(&korg1212->lock);
1199 snd_pcm_period_elapsed(korg1212->playback_substream);
1200 spin_lock(&korg1212->lock);
1201 }
1202 }
1203 break;
1204 }
1205
1206 korg1212->inIRQ--;
1207
1208 spin_unlock(&korg1212->lock);
1209
1210 return IRQ_HANDLED;
1211}
1212
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001213static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001215 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Takashi Iwai9fd91562005-11-17 10:45:48 +01001217 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1218 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 // ---------------------------------------------------------------
1221 // verify the state of the card before proceeding.
1222 // ---------------------------------------------------------------
Takashi Iwai9fd91562005-11-17 10:45:48 +01001223 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1227
1228 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1229
1230 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1231 UpperWordSwap(korg1212->dma_dsp.addr),
1232 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001233 if (rc)
1234 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1235 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 korg1212->dsp_is_loaded = 0;
1238 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1239 if (! korg1212->dsp_is_loaded )
1240 return -EBUSY; /* timeout */
1241
1242 snd_korg1212_OnDSPDownloadComplete(korg1212);
1243
1244 return 0;
1245}
1246
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001247static struct snd_pcm_hardware snd_korg1212_playback_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 .info = (SNDRV_PCM_INFO_MMAP |
1250 SNDRV_PCM_INFO_MMAP_VALID |
1251 SNDRV_PCM_INFO_INTERLEAVED),
1252 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1253 .rates = (SNDRV_PCM_RATE_44100 |
1254 SNDRV_PCM_RATE_48000),
1255 .rate_min = 44100,
1256 .rate_max = 48000,
1257 .channels_min = K1212_MIN_CHANNELS,
1258 .channels_max = K1212_MAX_CHANNELS,
1259 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1260 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1261 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1262 .periods_min = K1212_PERIODS,
1263 .periods_max = K1212_PERIODS,
1264 .fifo_size = 0,
1265};
1266
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001267static struct snd_pcm_hardware snd_korg1212_capture_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 .info = (SNDRV_PCM_INFO_MMAP |
1270 SNDRV_PCM_INFO_MMAP_VALID |
1271 SNDRV_PCM_INFO_INTERLEAVED),
1272 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1273 .rates = (SNDRV_PCM_RATE_44100 |
1274 SNDRV_PCM_RATE_48000),
1275 .rate_min = 44100,
1276 .rate_max = 48000,
1277 .channels_min = K1212_MIN_CHANNELS,
1278 .channels_max = K1212_MAX_CHANNELS,
1279 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1280 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1281 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1282 .periods_min = K1212_PERIODS,
1283 .periods_max = K1212_PERIODS,
1284 .fifo_size = 0,
1285};
1286
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001287static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001289 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int i;
1291
Takashi Iwai9fd91562005-11-17 10:45:48 +01001292 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1293 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1295
1296 for (i=0; i < count; i++) {
1297#if K1212_DEBUG_LEVEL > 0
1298 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1299 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001300 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1301 dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -EFAULT;
1303 }
1304#endif
1305 memset((void*) dst + offset, 0, size);
1306 dst++;
1307 }
1308
1309 return 0;
1310}
1311
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001312static 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 -07001313{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001314 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int i, rc;
1316
Takashi Iwai9fd91562005-11-17 10:45:48 +01001317 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1318 pos, offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1320
1321 for (i=0; i < count; i++) {
1322#if K1212_DEBUG_LEVEL > 0
1323 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1324 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001325 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 -07001326 return -EFAULT;
1327 }
1328#endif
1329 rc = copy_to_user(dst + offset, src, size);
1330 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 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 -07001332 return -EFAULT;
1333 }
1334 src++;
1335 dst += size;
1336 }
1337
1338 return 0;
1339}
1340
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001341static 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 -07001342{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001343 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int i, rc;
1345
Takashi Iwai9fd91562005-11-17 10:45:48 +01001346 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1347 pos, offset, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1350
1351 for (i=0; i < count; i++) {
1352#if K1212_DEBUG_LEVEL > 0
1353 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1354 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001355 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 -07001356 return -EFAULT;
1357 }
1358#endif
1359 rc = copy_from_user((void*) dst + offset, src, size);
1360 if (rc) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001361 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 -07001362 return -EFAULT;
1363 }
1364 dst++;
1365 src += size;
1366 }
1367
1368 return 0;
1369}
1370
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001371static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001373 struct snd_korg1212 *korg1212 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Takashi Iwai9fd91562005-11-17 10:45:48 +01001375 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1376 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 korg1212->pcm = NULL;
1379}
1380
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001381static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001384 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1385 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Takashi Iwai9fd91562005-11-17 10:45:48 +01001387 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1388 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 snd_pcm_set_sync(substream); // ???
1391
1392 snd_korg1212_OpenCard(korg1212);
1393
1394 runtime->hw = snd_korg1212_playback_info;
1395 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1396
1397 spin_lock_irqsave(&korg1212->lock, flags);
1398
1399 korg1212->playback_substream = substream;
1400 korg1212->playback_pid = current->pid;
1401 korg1212->periodsize = K1212_PERIODS;
1402 korg1212->channels = K1212_CHANNELS;
1403 korg1212->errorcnt = 0;
1404
1405 spin_unlock_irqrestore(&korg1212->lock, flags);
1406
1407 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1408 return 0;
1409}
1410
1411
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001412static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001415 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1416 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Takashi Iwai9fd91562005-11-17 10:45:48 +01001418 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1419 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 snd_pcm_set_sync(substream);
1422
1423 snd_korg1212_OpenCard(korg1212);
1424
1425 runtime->hw = snd_korg1212_capture_info;
1426 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1427
1428 spin_lock_irqsave(&korg1212->lock, flags);
1429
1430 korg1212->capture_substream = substream;
1431 korg1212->capture_pid = current->pid;
1432 korg1212->periodsize = K1212_PERIODS;
1433 korg1212->channels = K1212_CHANNELS;
1434
1435 spin_unlock_irqrestore(&korg1212->lock, flags);
1436
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001437 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1438 kPlayBufferFrames, kPlayBufferFrames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
1440}
1441
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001442static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001445 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Takashi Iwai9fd91562005-11-17 10:45:48 +01001447 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1448 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1451
1452 spin_lock_irqsave(&korg1212->lock, flags);
1453
1454 korg1212->playback_pid = -1;
1455 korg1212->playback_substream = NULL;
1456 korg1212->periodsize = 0;
1457
1458 spin_unlock_irqrestore(&korg1212->lock, flags);
1459
1460 snd_korg1212_CloseCard(korg1212);
1461 return 0;
1462}
1463
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001464static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001467 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Takashi Iwai9fd91562005-11-17 10:45:48 +01001469 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1470 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 spin_lock_irqsave(&korg1212->lock, flags);
1473
1474 korg1212->capture_pid = -1;
1475 korg1212->capture_substream = NULL;
1476 korg1212->periodsize = 0;
1477
1478 spin_unlock_irqrestore(&korg1212->lock, flags);
1479
1480 snd_korg1212_CloseCard(korg1212);
1481 return 0;
1482}
1483
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001484static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 unsigned int cmd, void *arg)
1486{
Takashi Iwai9fd91562005-11-17 10:45:48 +01001487 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001490 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 info->offset = 0;
1492 info->first = info->channel * 16;
1493 info->step = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 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 -07001495 return 0;
1496 }
1497
1498 return snd_pcm_lib_ioctl(substream, cmd, arg);
1499}
1500
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001501static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1502 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001505 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int err;
1507 pid_t this_pid;
1508 pid_t other_pid;
1509
Takashi Iwai9fd91562005-11-17 10:45:48 +01001510 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1511 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 spin_lock_irqsave(&korg1212->lock, flags);
1514
1515 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1516 this_pid = korg1212->playback_pid;
1517 other_pid = korg1212->capture_pid;
1518 } else {
1519 this_pid = korg1212->capture_pid;
1520 other_pid = korg1212->playback_pid;
1521 }
1522
1523 if ((other_pid > 0) && (this_pid != other_pid)) {
1524
1525 /* The other stream is open, and not by the same
1526 task as this one. Make sure that the parameters
1527 that matter are the same.
1528 */
1529
1530 if ((int)params_rate(params) != korg1212->clkRate) {
1531 spin_unlock_irqrestore(&korg1212->lock, flags);
1532 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1533 return -EBUSY;
1534 }
1535
1536 spin_unlock_irqrestore(&korg1212->lock, flags);
1537 return 0;
1538 }
1539
1540 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1541 spin_unlock_irqrestore(&korg1212->lock, flags);
1542 return err;
1543 }
1544
1545 korg1212->channels = params_channels(params);
1546 korg1212->periodsize = K1212_PERIOD_BYTES;
1547
1548 spin_unlock_irqrestore(&korg1212->lock, flags);
1549
1550 return 0;
1551}
1552
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001553static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001555 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int rc;
1557
Takashi Iwai9fd91562005-11-17 10:45:48 +01001558 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1559 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 spin_lock_irq(&korg1212->lock);
1562
1563 /* FIXME: we should wait for ack! */
1564 if (korg1212->stop_pending_cnt > 0) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001565 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1566 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 spin_unlock_irq(&korg1212->lock);
1568 return -EAGAIN;
1569 /*
1570 korg1212->sharedBufferPtr->cardCommand = 0;
1571 del_timer(&korg1212->timer);
1572 korg1212->stop_pending_cnt = 0;
1573 */
1574 }
1575
1576 rc = snd_korg1212_SetupForPlay(korg1212);
1577
1578 korg1212->currentBuffer = 0;
1579
1580 spin_unlock_irq(&korg1212->lock);
1581
1582 return rc ? -EINVAL : 0;
1583}
1584
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001585static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int cmd)
1587{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001588 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int rc;
1590
Takashi Iwai9fd91562005-11-17 10:45:48 +01001591 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1592 stateName[korg1212->cardState], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 spin_lock(&korg1212->lock);
1595 switch (cmd) {
1596 case SNDRV_PCM_TRIGGER_START:
1597/*
1598 if (korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001599 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
1601 }
1602*/
1603 korg1212->running++;
1604 rc = snd_korg1212_TriggerPlay(korg1212);
1605 break;
1606
1607 case SNDRV_PCM_TRIGGER_STOP:
1608/*
1609 if (!korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001610 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 break;
1612 }
1613*/
1614 korg1212->running--;
1615 rc = snd_korg1212_StopPlay(korg1212);
1616 break;
1617
1618 default:
1619 rc = 1;
1620 break;
1621 }
1622 spin_unlock(&korg1212->lock);
1623 return rc ? -EINVAL : 0;
1624}
1625
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001626static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001628 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 snd_pcm_uframes_t pos;
1630
1631 pos = korg1212->currentBuffer * kPlayBufferFrames;
1632
Takashi Iwai9fd91562005-11-17 10:45:48 +01001633 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1634 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 return pos;
1637}
1638
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001639static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001641 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 snd_pcm_uframes_t pos;
1643
1644 pos = korg1212->currentBuffer * kPlayBufferFrames;
1645
Takashi Iwai9fd91562005-11-17 10:45:48 +01001646 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1647 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 return pos;
1650}
1651
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001652static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int channel, /* not used (interleaved data) */
1654 snd_pcm_uframes_t pos,
1655 void __user *src,
1656 snd_pcm_uframes_t count)
1657{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001658 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Takashi Iwai9fd91562005-11-17 10:45:48 +01001660 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1661 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1664
1665}
1666
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001667static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int channel, /* not used (interleaved data) */
1669 snd_pcm_uframes_t pos,
1670 snd_pcm_uframes_t count)
1671{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001672 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Takashi Iwai9fd91562005-11-17 10:45:48 +01001674 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1675 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1678}
1679
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001680static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int channel, /* not used (interleaved data) */
1682 snd_pcm_uframes_t pos,
1683 void __user *dst,
1684 snd_pcm_uframes_t count)
1685{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001686 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Takashi Iwai9fd91562005-11-17 10:45:48 +01001688 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1689 stateName[korg1212->cardState], pos, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1692}
1693
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001694static struct snd_pcm_ops snd_korg1212_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 .open = snd_korg1212_playback_open,
1696 .close = snd_korg1212_playback_close,
1697 .ioctl = snd_korg1212_ioctl,
1698 .hw_params = snd_korg1212_hw_params,
1699 .prepare = snd_korg1212_prepare,
1700 .trigger = snd_korg1212_trigger,
1701 .pointer = snd_korg1212_playback_pointer,
1702 .copy = snd_korg1212_playback_copy,
1703 .silence = snd_korg1212_playback_silence,
1704};
1705
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001706static struct snd_pcm_ops snd_korg1212_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 .open = snd_korg1212_capture_open,
1708 .close = snd_korg1212_capture_close,
1709 .ioctl = snd_korg1212_ioctl,
1710 .hw_params = snd_korg1212_hw_params,
1711 .prepare = snd_korg1212_prepare,
1712 .trigger = snd_korg1212_trigger,
1713 .pointer = snd_korg1212_capture_pointer,
1714 .copy = snd_korg1212_capture_copy,
1715};
1716
1717/*
1718 * Control Interface
1719 */
1720
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001721static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1725 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1726 return 0;
1727}
1728
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001729static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001732 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 int i = kcontrol->private_value;
1734
1735 spin_lock_irq(&korg1212->lock);
1736
1737 u->value.integer.value[0] = korg1212->volumePhase[i];
1738
1739 if (i >= 8)
1740 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1741
1742 spin_unlock_irq(&korg1212->lock);
1743
1744 return 0;
1745}
1746
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001747static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001750 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 int change = 0;
1752 int i, val;
1753
1754 spin_lock_irq(&korg1212->lock);
1755
1756 i = kcontrol->private_value;
1757
1758 korg1212->volumePhase[i] = u->value.integer.value[0];
1759
1760 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1761
1762 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1763 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1764 korg1212->sharedBufferPtr->volumeData[i] = val;
1765 change = 1;
1766 }
1767
1768 if (i >= 8) {
1769 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1770
1771 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1772
1773 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1774 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1775 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1776 change = 1;
1777 }
1778 }
1779
1780 spin_unlock_irq(&korg1212->lock);
1781
1782 return change;
1783}
1784
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001785static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1786 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1789 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1790 uinfo->value.integer.min = k1212MinVolume;
1791 uinfo->value.integer.max = k1212MaxVolume;
1792 return 0;
1793}
1794
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001795static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001798 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int i;
1800
1801 spin_lock_irq(&korg1212->lock);
1802
1803 i = kcontrol->private_value;
1804 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1805
1806 if (i >= 8)
1807 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1808
1809 spin_unlock_irq(&korg1212->lock);
1810
1811 return 0;
1812}
1813
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001814static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001817 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 int change = 0;
1819 int i;
1820 int val;
1821
1822 spin_lock_irq(&korg1212->lock);
1823
1824 i = kcontrol->private_value;
1825
1826 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1827 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1828 val *= u->value.integer.value[0];
1829 korg1212->sharedBufferPtr->volumeData[i] = val;
1830 change = 1;
1831 }
1832
1833 if (i >= 8) {
1834 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1835 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1836 val *= u->value.integer.value[1];
1837 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1838 change = 1;
1839 }
1840 }
1841
1842 spin_unlock_irq(&korg1212->lock);
1843
1844 return change;
1845}
1846
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001847static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1851 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1852 uinfo->value.enumerated.items = kAudioChannels;
1853 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1854 uinfo->value.enumerated.item = kAudioChannels-1;
1855 }
1856 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1857 return 0;
1858}
1859
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001860static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001863 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int i;
1865
1866 spin_lock_irq(&korg1212->lock);
1867
1868 i = kcontrol->private_value;
1869 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1870
1871 if (i >= 8)
1872 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1873
1874 spin_unlock_irq(&korg1212->lock);
1875
1876 return 0;
1877}
1878
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001879static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1880 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001882 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int change = 0, i;
1884
1885 spin_lock_irq(&korg1212->lock);
1886
1887 i = kcontrol->private_value;
1888
1889 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1890 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1891 change = 1;
1892 }
1893
1894 if (i >= 8) {
1895 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1896 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1897 change = 1;
1898 }
1899 }
1900
1901 spin_unlock_irq(&korg1212->lock);
1902
1903 return change;
1904}
1905
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001906static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1910 uinfo->count = 2;
1911 uinfo->value.integer.min = k1212MaxADCSens;
1912 uinfo->value.integer.max = k1212MinADCSens;
1913 return 0;
1914}
1915
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001916static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1917 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001919 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 spin_lock_irq(&korg1212->lock);
1922
1923 u->value.integer.value[0] = korg1212->leftADCInSens;
1924 u->value.integer.value[1] = korg1212->rightADCInSens;
1925
1926 spin_unlock_irq(&korg1212->lock);
1927
1928 return 0;
1929}
1930
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001931static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001934 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 int change = 0;
1936
1937 spin_lock_irq(&korg1212->lock);
1938
1939 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1940 korg1212->leftADCInSens = u->value.integer.value[0];
1941 change = 1;
1942 }
1943 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1944 korg1212->rightADCInSens = u->value.integer.value[1];
1945 change = 1;
1946 }
1947
1948 spin_unlock_irq(&korg1212->lock);
1949
1950 if (change)
1951 snd_korg1212_WriteADCSensitivity(korg1212);
1952
1953 return change;
1954}
1955
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001956static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1960 uinfo->count = 1;
1961 uinfo->value.enumerated.items = 3;
1962 if (uinfo->value.enumerated.item > 2) {
1963 uinfo->value.enumerated.item = 2;
1964 }
1965 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1966 return 0;
1967}
1968
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001969static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001972 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 spin_lock_irq(&korg1212->lock);
1975
1976 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1977
1978 spin_unlock_irq(&korg1212->lock);
1979 return 0;
1980}
1981
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001982static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1983 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001985 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned int val;
1987 int change;
1988
1989 val = ucontrol->value.enumerated.item[0] % 3;
1990 spin_lock_irq(&korg1212->lock);
1991 change = val != korg1212->clkSource;
1992 snd_korg1212_SetClockSource(korg1212, val);
1993 spin_unlock_irq(&korg1212->lock);
1994 return change;
1995}
1996
1997#define MON_MIXER(ord,c_name) \
1998 { \
1999 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2000 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2001 .name = c_name " Monitor Volume", \
2002 .info = snd_korg1212_control_volume_info, \
2003 .get = snd_korg1212_control_volume_get, \
2004 .put = snd_korg1212_control_volume_put, \
2005 .private_value = ord, \
2006 }, \
2007 { \
2008 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2009 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2010 .name = c_name " Monitor Route", \
2011 .info = snd_korg1212_control_route_info, \
2012 .get = snd_korg1212_control_route_get, \
2013 .put = snd_korg1212_control_route_put, \
2014 .private_value = ord, \
2015 }, \
2016 { \
2017 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002018 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 .name = c_name " Monitor Phase Invert", \
2020 .info = snd_korg1212_control_phase_info, \
2021 .get = snd_korg1212_control_phase_get, \
2022 .put = snd_korg1212_control_phase_put, \
2023 .private_value = ord, \
2024 }
2025
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002026static struct snd_kcontrol_new snd_korg1212_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 MON_MIXER(8, "Analog"),
2028 MON_MIXER(10, "SPDIF"),
2029 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2030 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2031 {
2032 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 .name = "Sync Source",
2035 .info = snd_korg1212_control_sync_info,
2036 .get = snd_korg1212_control_sync_get,
2037 .put = snd_korg1212_control_sync_put,
2038 },
2039 {
2040 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2041 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2042 .name = "ADC Attenuation",
2043 .info = snd_korg1212_control_info,
2044 .get = snd_korg1212_control_get,
2045 .put = snd_korg1212_control_put,
2046 }
2047};
2048
2049/*
2050 * proc interface
2051 */
2052
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002053static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2054 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int n;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002057 struct snd_korg1212 *korg1212 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 snd_iprintf(buffer, korg1212->card->longname);
2060 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2061 snd_iprintf(buffer, "\nGeneral settings\n");
2062 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2063 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2064 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2065 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2066 snd_iprintf(buffer, " Volume Info:\n");
2067 for (n=0; n<kAudioChannels; n++)
2068 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2069 channelName[n],
2070 channelName[korg1212->sharedBufferPtr->routeData[n]],
2071 korg1212->sharedBufferPtr->volumeData[n]);
2072 snd_iprintf(buffer, "\nGeneral status\n");
2073 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2074 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2075 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2076 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2077 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2078 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2079}
2080
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002081static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002083 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
2086 snd_info_set_text_ops(entry, korg1212, 1024, snd_korg1212_proc_read);
2087}
2088
2089static int
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002090snd_korg1212_free(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 snd_korg1212_TurnOffIdleMonitor(korg1212);
2093
2094 if (korg1212->irq >= 0) {
2095 synchronize_irq(korg1212->irq);
2096 snd_korg1212_DisableCardInterrupts(korg1212);
Takashi Iwai9fd91562005-11-17 10:45:48 +01002097 free_irq(korg1212->irq, korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 korg1212->irq = -1;
2099 }
2100
2101 if (korg1212->iobase != NULL) {
2102 iounmap(korg1212->iobase);
2103 korg1212->iobase = NULL;
2104 }
2105
2106 pci_release_regions(korg1212->pci);
2107
2108 // ----------------------------------------------------
2109 // free up memory resources used for the DSP download.
2110 // ----------------------------------------------------
2111 if (korg1212->dma_dsp.area) {
2112 snd_dma_free_pages(&korg1212->dma_dsp);
2113 korg1212->dma_dsp.area = NULL;
2114 }
2115
2116#ifndef K1212_LARGEALLOC
2117
2118 // ------------------------------------------------------
2119 // free up memory resources used for the Play/Rec Buffers
2120 // ------------------------------------------------------
2121 if (korg1212->dma_play.area) {
2122 snd_dma_free_pages(&korg1212->dma_play);
2123 korg1212->dma_play.area = NULL;
2124 }
2125
2126 if (korg1212->dma_rec.area) {
2127 snd_dma_free_pages(&korg1212->dma_rec);
2128 korg1212->dma_rec.area = NULL;
2129 }
2130
2131#endif
2132
2133 // ----------------------------------------------------
2134 // free up memory resources used for the Shared Buffers
2135 // ----------------------------------------------------
2136 if (korg1212->dma_shared.area) {
2137 snd_dma_free_pages(&korg1212->dma_shared);
2138 korg1212->dma_shared.area = NULL;
2139 }
2140
2141 pci_disable_device(korg1212->pci);
2142 kfree(korg1212);
2143 return 0;
2144}
2145
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002146static int snd_korg1212_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002148 struct snd_korg1212 *korg1212 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return snd_korg1212_free(korg1212);
2151}
2152
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002153static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2154 struct snd_korg1212 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156{
Takashi Iwai9fd91562005-11-17 10:45:48 +01002157 int err, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 unsigned int i;
2159 unsigned ioport_size, iomem_size, iomem2_size;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002160 struct snd_korg1212 * korg1212;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002162 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 .dev_free = snd_korg1212_dev_free,
2164 };
2165
2166 * rchip = NULL;
2167 if ((err = pci_enable_device(pci)) < 0)
2168 return err;
2169
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002170 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (korg1212 == NULL) {
2172 pci_disable_device(pci);
2173 return -ENOMEM;
2174 }
2175
2176 korg1212->card = card;
2177 korg1212->pci = pci;
2178
2179 init_waitqueue_head(&korg1212->wait);
2180 spin_lock_init(&korg1212->lock);
2181 init_MUTEX(&korg1212->open_mutex);
2182 init_timer(&korg1212->timer);
2183 korg1212->timer.function = snd_korg1212_timer_func;
2184 korg1212->timer.data = (unsigned long)korg1212;
2185
2186 korg1212->irq = -1;
2187 korg1212->clkSource = K1212_CLKIDX_Local;
2188 korg1212->clkRate = 44100;
2189 korg1212->inIRQ = 0;
2190 korg1212->running = 0;
2191 korg1212->opencnt = 0;
2192 korg1212->playcnt = 0;
2193 korg1212->setcnt = 0;
2194 korg1212->totalerrorcnt = 0;
2195 korg1212->playback_pid = -1;
2196 korg1212->capture_pid = -1;
2197 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2198 korg1212->idleMonitorOn = 0;
2199 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2200 korg1212->leftADCInSens = k1212MaxADCSens;
2201 korg1212->rightADCInSens = k1212MaxADCSens;
2202
2203 for (i=0; i<kAudioChannels; i++)
2204 korg1212->volumePhase[i] = 0;
2205
2206 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2207 kfree(korg1212);
2208 pci_disable_device(pci);
2209 return err;
2210 }
2211
2212 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2213 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2214 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2215
2216 iomem_size = pci_resource_len(korg1212->pci, 0);
2217 ioport_size = pci_resource_len(korg1212->pci, 1);
2218 iomem2_size = pci_resource_len(korg1212->pci, 2);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2221 " iomem = 0x%lx (%d)\n"
2222 " ioport = 0x%lx (%d)\n"
2223 " iomem = 0x%lx (%d)\n"
2224 " [%s]\n",
2225 korg1212->iomem, iomem_size,
2226 korg1212->ioport, ioport_size,
2227 korg1212->iomem2, iomem2_size,
2228 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2231 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2232 korg1212->iomem + iomem_size - 1);
2233 snd_korg1212_free(korg1212);
2234 return -EBUSY;
2235 }
2236
2237 err = request_irq(pci->irq, snd_korg1212_interrupt,
2238 SA_INTERRUPT|SA_SHIRQ,
Takashi Iwai9fd91562005-11-17 10:45:48 +01002239 "korg1212", korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 if (err) {
2242 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2243 snd_korg1212_free(korg1212);
2244 return -EBUSY;
2245 }
2246
2247 korg1212->irq = pci->irq;
2248
2249 pci_set_master(korg1212->pci);
2250
2251 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2252 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2253 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2254 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2255 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2256 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2257 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2258 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2259 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2260 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2263 " Status register = 0x%p\n"
2264 " OutDoorbell = 0x%p\n"
2265 " InDoorbell = 0x%p\n"
2266 " Mailbox0 = 0x%p\n"
2267 " Mailbox1 = 0x%p\n"
2268 " Mailbox2 = 0x%p\n"
2269 " Mailbox3 = 0x%p\n"
2270 " ControlReg = 0x%p\n"
2271 " SensReg = 0x%p\n"
2272 " IDReg = 0x%p\n"
2273 " [%s]\n",
2274 korg1212->statusRegPtr,
2275 korg1212->outDoorbellPtr,
2276 korg1212->inDoorbellPtr,
2277 korg1212->mailbox0Ptr,
2278 korg1212->mailbox1Ptr,
2279 korg1212->mailbox2Ptr,
2280 korg1212->mailbox3Ptr,
2281 korg1212->controlRegPtr,
2282 korg1212->sensRegPtr,
2283 korg1212->idRegPtr,
2284 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002287 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2288 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 snd_korg1212_free(korg1212);
2290 return -ENOMEM;
2291 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002292 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2294
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002295 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 -07002296
2297#ifndef K1212_LARGEALLOC
2298
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002299 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2302 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2303 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2304 snd_korg1212_free(korg1212);
2305 return -ENOMEM;
2306 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002307 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2311 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2314 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2315 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2316 snd_korg1212_free(korg1212);
2317 return -ENOMEM;
2318 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002319 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2323 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325#else // K1212_LARGEALLOC
2326
2327 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2328 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002329 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2330 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332#endif // K1212_LARGEALLOC
2333
2334 korg1212->dspCodeSize = sizeof (dspCode);
2335
2336 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002337 offsetof(struct KorgSharedBuffer, volumeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002339 offsetof(struct KorgSharedBuffer, routeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002341 offsetof(struct KorgSharedBuffer, AdatTimeCode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2344 korg1212->dspCodeSize, &korg1212->dma_dsp) < 0) {
2345 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", korg1212->dspCodeSize);
2346 snd_korg1212_free(korg1212);
2347 return -ENOMEM;
2348 }
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2351 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, korg1212->dspCodeSize,
2352 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2355
Takashi Iwai9fd91562005-11-17 10:45:48 +01002356 if (rc)
2357 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2360 snd_korg1212_free(korg1212);
2361 return err;
2362 }
2363
2364 snd_korg1212_EnableCardInterrupts(korg1212);
2365
2366 mdelay(CARD_BOOT_DELAY_IN_MS);
2367
2368 if (snd_korg1212_downloadDSPCode(korg1212))
2369 return -EBUSY;
2370
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002371 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 "PlayDataPhy = %08x L[%08x]\n"
2373 "korg1212: RecDataPhy = %08x L[%08x], "
2374 "VolumeTablePhy = %08x L[%08x]\n"
2375 "korg1212: RoutingTablePhy = %08x L[%08x], "
2376 "AdatTimeCodePhy = %08x L[%08x]\n",
2377 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2378 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2379 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2380 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2381 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2382 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2383
2384 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2385 return err;
2386
2387 korg1212->pcm->private_data = korg1212;
2388 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2389 strcpy(korg1212->pcm->name, "korg1212");
2390
2391 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2392
2393 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2394
2395 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2398 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2399 if (err < 0)
2400 return err;
2401 }
2402
2403 snd_korg1212_proc_init(korg1212);
2404
2405 snd_card_set_dev(card, &pci->dev);
2406
2407 * rchip = korg1212;
2408 return 0;
2409
2410}
2411
2412/*
2413 * Card initialisation
2414 */
2415
2416static int __devinit
2417snd_korg1212_probe(struct pci_dev *pci,
2418 const struct pci_device_id *pci_id)
2419{
2420 static int dev;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002421 struct snd_korg1212 *korg1212;
2422 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 int err;
2424
2425 if (dev >= SNDRV_CARDS) {
2426 return -ENODEV;
2427 }
2428 if (!enable[dev]) {
2429 dev++;
2430 return -ENOENT;
2431 }
2432 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2433 if (card == NULL)
2434 return -ENOMEM;
2435
2436 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2437 snd_card_free(card);
2438 return err;
2439 }
2440
2441 strcpy(card->driver, "korg1212");
2442 strcpy(card->shortname, "korg1212");
2443 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2444 korg1212->iomem, korg1212->irq);
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 if ((err = snd_card_register(card)) < 0) {
2449 snd_card_free(card);
2450 return err;
2451 }
2452 pci_set_drvdata(pci, card);
2453 dev++;
2454 return 0;
2455}
2456
2457static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2458{
2459 snd_card_free(pci_get_drvdata(pci));
2460 pci_set_drvdata(pci, NULL);
2461}
2462
2463static struct pci_driver driver = {
2464 .name = "korg1212",
2465 .id_table = snd_korg1212_ids,
2466 .probe = snd_korg1212_probe,
2467 .remove = __devexit_p(snd_korg1212_remove),
2468};
2469
2470static int __init alsa_card_korg1212_init(void)
2471{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002472 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475static void __exit alsa_card_korg1212_exit(void)
2476{
2477 pci_unregister_driver(&driver);
2478}
2479
2480module_init(alsa_card_korg1212_init)
2481module_exit(alsa_card_korg1212_exit)