Various drivers' irq handlers: kill dead code, needless casts
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / pci / korg1212 / korg1212.c
CommitLineData
1da177e4
LT
1/*
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>
62932df8 30#include <linux/mutex.h>
1da177e4
LT
31
32#include <sound/core.h>
33#include <sound/info.h>
34#include <sound/control.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/initval.h>
38
39#include <asm/io.h>
40
41// ----------------------------------------------------------------------------
42// Debug Stuff
43// ----------------------------------------------------------------------------
44#define K1212_DEBUG_LEVEL 0
9fd9156c
TI
45#if K1212_DEBUG_LEVEL > 0
46#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
47#else
48#define K1212_DEBUG_PRINTK(fmt,...)
49#endif
50#if K1212_DEBUG_LEVEL > 1
51#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
52#else
53#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
54#endif
1da177e4
LT
55
56// ----------------------------------------------------------------------------
57// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
58// buffers are alocated as a large piece inside KorgSharedBuffer.
59// ----------------------------------------------------------------------------
60//#define K1212_LARGEALLOC 1
61
62// ----------------------------------------------------------------------------
63// Valid states of the Korg 1212 I/O card.
64// ----------------------------------------------------------------------------
fcfd3332 65enum CardState {
1da177e4
LT
66 K1212_STATE_NONEXISTENT, // there is no card here
67 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
68 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
69 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
70 K1212_STATE_READY, // the card can be opened by an application. Any application
71 // requests prior to this state should fail. Only an open
72 // request can be made at this state.
73 K1212_STATE_OPEN, // an application has opened the card
74 K1212_STATE_SETUP, // the card has been setup for play
75 K1212_STATE_PLAYING, // the card is playing
76 K1212_STATE_MONITOR, // the card is in the monitor mode
77 K1212_STATE_CALIBRATING, // the card is currently calibrating
78 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
79 // are in the process of cleaning things up.
80 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
fcfd3332 81};
1da177e4
LT
82
83// ----------------------------------------------------------------------------
84// The following enumeration defines the constants written to the card's
85// host-to-card doorbell to initiate a command.
86// ----------------------------------------------------------------------------
fcfd3332 87enum korg1212_dbcnst {
1da177e4
LT
88 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
89 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
90 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
91 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
92 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
93 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
94 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
95 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
96 // timecode value.
97 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
98 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
99 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
100 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
101 // on page 4 (local page to card).
102 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
103 // completed.
104 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
fcfd3332 105};
1da177e4
LT
106
107
108// ----------------------------------------------------------------------------
109// The following enumeration defines return codes
110// to the Korg 1212 I/O driver.
111// ----------------------------------------------------------------------------
fcfd3332 112enum snd_korg1212rc {
1da177e4
LT
113 K1212_CMDRET_Success = 0, // command was successfully placed
114 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
115 K1212_CMDRET_PMFailure, // the protected mode call failed
116 K1212_CMDRET_FailUnspecified, // unspecified failure
117 K1212_CMDRET_FailBadState, // the specified command can not be given in
118 // the card's current state. (or the wave device's
119 // state)
120 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
121 K1212_CMDRET_BadIndex, // an out of range card index was specified
122 K1212_CMDRET_BadHandle, // an invalid card handle was specified
123 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
124 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
125 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
126 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
127
128 K1212_CMDRET_BadDevice, // the specified wave device was out of range
129 K1212_CMDRET_BadFormat // the specified wave format is unsupported
fcfd3332 130};
1da177e4
LT
131
132// ----------------------------------------------------------------------------
133// The following enumeration defines the constants used to select the play
134// mode for the card in the SelectPlayMode command.
135// ----------------------------------------------------------------------------
fcfd3332 136enum PlayModeSelector {
1da177e4
LT
137 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
138 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
139 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
140 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
fcfd3332 141};
1da177e4
LT
142
143// ----------------------------------------------------------------------------
144// The following enumeration defines the constants used to select the monitor
145// mode for the card in the SetMonitorMode command.
146// ----------------------------------------------------------------------------
fcfd3332 147enum MonitorModeSelector {
1da177e4
LT
148 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
149 K1212_MONMODE_On // tells card to turn on monitor mode
fcfd3332 150};
1da177e4
LT
151
152#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
153#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
154#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
155#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
156#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
157#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
158#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
159#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
160 // register
161#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
162 // this is the upper word of the PCI control reg.
163#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
164
165#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
166 // from the card after sending a command.
167#define INTERCOMMAND_DELAY 40
168#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
169 // to send a command before giving up.
170#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
171 // the card.
172#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
173
174#define CARD_BOOT_DELAY_IN_MS 10
175#define CARD_BOOT_TIMEOUT 10
176#define DSP_BOOT_DELAY_IN_MS 200
177
178#define kNumBuffers 8
179#define k1212MaxCards 4
180#define k1212NumWaveDevices 6
181#define k16BitChannels 10
182#define k32BitChannels 2
183#define kAudioChannels (k16BitChannels + k32BitChannels)
184#define kPlayBufferFrames 1024
185
186#define K1212_ANALOG_CHANNELS 2
187#define K1212_SPDIF_CHANNELS 2
188#define K1212_ADAT_CHANNELS 8
189#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
190#define K1212_MIN_CHANNELS 1
191#define K1212_MAX_CHANNELS K1212_CHANNELS
fcfd3332 192#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
1da177e4
LT
193#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
194#define K1212_PERIODS (kNumBuffers)
195#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
196#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
197#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
198#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
199#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
200#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
201
202#define k1212MinADCSens 0x7f
203#define k1212MaxADCSens 0x00
204#define k1212MaxVolume 0x7fff
205#define k1212MaxWaveVolume 0xffff
206#define k1212MinVolume 0x0000
207#define k1212MaxVolInverted 0x8000
208
209// -----------------------------------------------------------------
210// the following bits are used for controlling interrupts in the
211// interrupt control/status reg
212// -----------------------------------------------------------------
213#define PCI_INT_ENABLE_BIT 0x00000100
214#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
215#define LOCAL_INT_ENABLE_BIT 0x00010000
216#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
217#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
218
219// -----------------------------------------------------------------
220// the following bits are defined for the PCI command register
221// -----------------------------------------------------------------
222#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
223#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
224#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
225
226// -----------------------------------------------------------------
227// the following bits are defined for the PCI status register
228// -----------------------------------------------------------------
229#define PCI_STAT_PARITY_ERROR_BIT 0x8000
230#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
231#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
232#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
233#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
234
235// ------------------------------------------------------------------------
236// the following constants are used in setting the 1212 I/O card's input
237// sensitivity.
238// ------------------------------------------------------------------------
239#define SET_SENS_LOCALINIT_BITPOS 15
240#define SET_SENS_DATA_BITPOS 10
241#define SET_SENS_CLOCK_BITPOS 8
242#define SET_SENS_LOADSHIFT_BITPOS 0
243
244#define SET_SENS_LEFTCHANID 0x00
245#define SET_SENS_RIGHTCHANID 0x01
246
247#define K1212SENSUPDATE_DELAY_IN_MS 50
248
249// --------------------------------------------------------------------------
250// WaitRTCTicks
251//
252// This function waits the specified number of real time clock ticks.
253// According to the DDK, each tick is ~0.8 microseconds.
254// The defines following the function declaration can be used for the
255// numTicksToWait parameter.
256// --------------------------------------------------------------------------
257#define ONE_RTC_TICK 1
258#define SENSCLKPULSE_WIDTH 4
259#define LOADSHIFT_DELAY 4
260#define INTERCOMMAND_DELAY 40
261#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
262 // the command register. (could be up to 180 us)
263#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
264 // from the card after sending a command.
265
266#include "korg1212-firmware.h"
267
fcfd3332 268enum ClockSourceIndex {
1da177e4
LT
269 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
270 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
271 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
272 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
273 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
274 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
275 K1212_CLKIDX_Invalid // used to check validity of the index
fcfd3332 276};
1da177e4 277
fcfd3332 278enum ClockSourceType {
1da177e4
LT
279 K1212_CLKIDX_Adat = 0, // selects source as ADAT
280 K1212_CLKIDX_Word, // selects source as S/PDIF
281 K1212_CLKIDX_Local // selects source as local clock
fcfd3332 282};
1da177e4 283
fcfd3332
TI
284struct KorgAudioFrame {
285 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
286 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
287 u32 timeCodeVal; /* holds the ADAT timecode value */
288};
1da177e4 289
fcfd3332
TI
290struct KorgAudioBuffer {
291 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
292};
1da177e4 293
fcfd3332 294struct KorgSharedBuffer {
1da177e4 295#ifdef K1212_LARGEALLOC
fcfd3332
TI
296 struct KorgAudioBuffer playDataBufs[kNumBuffers];
297 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
1da177e4
LT
298#endif
299 short volumeData[kAudioChannels];
300 u32 cardCommand;
301 u16 routeData [kAudioChannels];
302 u32 AdatTimeCode; // ADAT timecode value
fcfd3332 303};
1da177e4 304
fcfd3332 305struct SensBits {
1da177e4
LT
306 union {
307 struct {
308 unsigned int leftChanVal:8;
309 unsigned int leftChanId:8;
310 } v;
311 u16 leftSensBits;
312 } l;
313 union {
314 struct {
315 unsigned int rightChanVal:8;
316 unsigned int rightChanId:8;
317 } v;
318 u16 rightSensBits;
319 } r;
fcfd3332 320};
1da177e4 321
fcfd3332
TI
322struct snd_korg1212 {
323 struct snd_card *card;
1da177e4 324 struct pci_dev *pci;
fcfd3332 325 struct snd_pcm *pcm;
1da177e4
LT
326 int irq;
327
328 spinlock_t lock;
62932df8 329 struct mutex open_mutex;
1da177e4
LT
330
331 struct timer_list timer; /* timer callback for checking ack of stop request */
332 int stop_pending_cnt; /* counter for stop pending check */
333
334 wait_queue_head_t wait;
335
336 unsigned long iomem;
337 unsigned long ioport;
338 unsigned long iomem2;
339 unsigned long irqcount;
340 unsigned long inIRQ;
341 void __iomem *iobase;
342
343 struct snd_dma_buffer dma_dsp;
344 struct snd_dma_buffer dma_play;
345 struct snd_dma_buffer dma_rec;
346 struct snd_dma_buffer dma_shared;
347
348 u32 dspCodeSize;
349
350 u32 DataBufsSize;
351
fcfd3332
TI
352 struct KorgAudioBuffer * playDataBufsPtr;
353 struct KorgAudioBuffer * recordDataBufsPtr;
1da177e4 354
fcfd3332 355 struct KorgSharedBuffer * sharedBufferPtr;
1da177e4
LT
356
357 u32 RecDataPhy;
358 u32 PlayDataPhy;
359 unsigned long sharedBufferPhy;
360 u32 VolumeTablePhy;
361 u32 RoutingTablePhy;
362 u32 AdatTimeCodePhy;
363
364 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
365 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
366 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
367 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
368 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
369 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
370 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
371 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
372 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
373 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
374
375 size_t periodsize;
376 int channels;
377 int currentBuffer;
378
fcfd3332
TI
379 struct snd_pcm_substream *playback_substream;
380 struct snd_pcm_substream *capture_substream;
1da177e4
LT
381
382 pid_t capture_pid;
383 pid_t playback_pid;
384
fcfd3332 385 enum CardState cardState;
1da177e4
LT
386 int running;
387 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
388 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
389
fcfd3332 390 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
1da177e4 391
fcfd3332 392 enum ClockSourceType clkSource; // clock source
1da177e4
LT
393 int clkRate; // clock rate
394
395 int volumePhase[kAudioChannels];
396
397 u16 leftADCInSens; // ADC left channel input sensitivity
398 u16 rightADCInSens; // ADC right channel input sensitivity
399
400 int opencnt; // Open/Close count
401 int setcnt; // SetupForPlay count
402 int playcnt; // TriggerPlay count
403 int errorcnt; // Error Count
404 unsigned long totalerrorcnt; // Total Error Count
405
406 int dsp_is_loaded;
407 int dsp_stop_is_processed;
408
409};
410
411MODULE_DESCRIPTION("korg1212");
412MODULE_LICENSE("GPL");
413MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
414
415static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
416static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
417static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
418
419module_param_array(index, int, NULL, 0444);
420MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
421module_param_array(id, charp, NULL, 0444);
422MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
423module_param_array(enable, bool, NULL, 0444);
424MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
425MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
426
f40b6890 427static struct pci_device_id snd_korg1212_ids[] = {
1da177e4
LT
428 {
429 .vendor = 0x10b5,
430 .device = 0x906d,
431 .subvendor = PCI_ANY_ID,
432 .subdevice = PCI_ANY_ID,
433 },
434 { 0, },
435};
436
fcfd3332
TI
437MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
438
9fd9156c
TI
439static char *stateName[] = {
440 "Non-existent",
441 "Uninitialized",
442 "DSP download in process",
443 "DSP download complete",
444 "Ready",
445 "Open",
446 "Setup for play",
447 "Playing",
448 "Monitor mode on",
449 "Calibrating",
450 "Invalid"
1da177e4
LT
451};
452
9fd9156c 453static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
1da177e4 454
9fd9156c
TI
455static char *clockSourceName[] = {
456 "ADAT at 44.1 kHz",
457 "ADAT at 48 kHz",
458 "S/PDIF at 44.1 kHz",
459 "S/PDIF at 48 kHz",
460 "local clock at 44.1 kHz",
461 "local clock at 48 kHz"
1da177e4
LT
462};
463
9fd9156c
TI
464static char *channelName[] = {
465 "ADAT-1",
466 "ADAT-2",
467 "ADAT-3",
468 "ADAT-4",
469 "ADAT-5",
470 "ADAT-6",
471 "ADAT-7",
472 "ADAT-8",
473 "Analog-L",
474 "Analog-R",
475 "SPDIF-L",
476 "SPDIF-R",
1da177e4
LT
477};
478
9fd9156c
TI
479static u16 ClockSourceSelector[] = {
480 0x8000, // selects source as ADAT at 44.1 kHz
481 0x0000, // selects source as ADAT at 48 kHz
482 0x8001, // selects source as S/PDIF at 44.1 kHz
483 0x0001, // selects source as S/PDIF at 48 kHz
484 0x8002, // selects source as local clock at 44.1 kHz
485 0x0002 // selects source as local clock at 48 kHz
486};
1da177e4 487
fcfd3332 488union swap_u32 { unsigned char c[4]; u32 i; };
1da177e4
LT
489
490#ifdef SNDRV_BIG_ENDIAN
491static u32 LowerWordSwap(u32 swappee)
492#else
493static u32 UpperWordSwap(u32 swappee)
494#endif
495{
fcfd3332 496 union swap_u32 retVal, swapper;
1da177e4
LT
497
498 swapper.i = swappee;
499 retVal.c[2] = swapper.c[3];
500 retVal.c[3] = swapper.c[2];
501 retVal.c[1] = swapper.c[1];
502 retVal.c[0] = swapper.c[0];
503
504 return retVal.i;
505}
506
507#ifdef SNDRV_BIG_ENDIAN
508static u32 UpperWordSwap(u32 swappee)
509#else
510static u32 LowerWordSwap(u32 swappee)
511#endif
512{
fcfd3332 513 union swap_u32 retVal, swapper;
1da177e4
LT
514
515 swapper.i = swappee;
516 retVal.c[2] = swapper.c[2];
517 retVal.c[3] = swapper.c[3];
518 retVal.c[1] = swapper.c[0];
519 retVal.c[0] = swapper.c[1];
520
521 return retVal.i;
522}
523
1da177e4
LT
524#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
525#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
526#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
527#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
528
fcfd3332
TI
529static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
530 enum korg1212_dbcnst doorbellVal,
531 u32 mailBox0Val, u32 mailBox1Val,
532 u32 mailBox2Val, u32 mailBox3Val)
1da177e4
LT
533{
534 u32 retryCount;
535 u16 mailBox3Lo;
fcfd3332 536 int rc = K1212_CMDRET_Success;
1da177e4
LT
537
538 if (!korg1212->outDoorbellPtr) {
9fd9156c 539 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
1da177e4
LT
540 return K1212_CMDRET_CardUninitialized;
541 }
542
9fd9156c
TI
543 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
544 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
1da177e4
LT
545 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
546 writel(mailBox3Val, korg1212->mailbox3Ptr);
547 writel(mailBox2Val, korg1212->mailbox2Ptr);
548 writel(mailBox1Val, korg1212->mailbox1Ptr);
549 writel(mailBox0Val, korg1212->mailbox0Ptr);
550 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
551
552 // --------------------------------------------------------------
553 // the reboot command will not give an acknowledgement.
554 // --------------------------------------------------------------
555 if ( doorbellVal == K1212_DB_RebootCard ||
556 doorbellVal == K1212_DB_BootFromDSPPage4 ||
557 doorbellVal == K1212_DB_StartDSPDownload ) {
558 rc = K1212_CMDRET_Success;
559 break;
560 }
561
562 // --------------------------------------------------------------
563 // See if the card acknowledged the command. Wait a bit, then
564 // read in the low word of mailbox3. If the MSB is set and the
565 // low byte is equal to the doorbell value, then it ack'd.
566 // --------------------------------------------------------------
567 udelay(COMMAND_ACK_DELAY);
568 mailBox3Lo = readl(korg1212->mailbox3Ptr);
569 if (mailBox3Lo & COMMAND_ACK_MASK) {
570 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
9fd9156c 571 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
1da177e4
LT
572 rc = K1212_CMDRET_Success;
573 break;
574 }
575 }
576 }
577 korg1212->cmdRetryCount += retryCount;
578
579 if (retryCount >= MAX_COMMAND_RETRIES) {
9fd9156c 580 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
1da177e4
LT
581 rc = K1212_CMDRET_NoAckFromCard;
582 }
583
584 return rc;
585}
586
587/* spinlock already held */
fcfd3332 588static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
1da177e4
LT
589{
590 if (! korg1212->stop_pending_cnt) {
591 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
592 /* program the timer */
593 korg1212->stop_pending_cnt = HZ;
594 korg1212->timer.expires = jiffies + 1;
595 add_timer(&korg1212->timer);
596 }
597}
598
fcfd3332 599static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
1da177e4
LT
600{
601 unsigned long flags;
602 spin_lock_irqsave(&korg1212->lock, flags);
603 korg1212->dsp_stop_is_processed = 0;
604 snd_korg1212_SendStop(korg1212);
605 spin_unlock_irqrestore(&korg1212->lock, flags);
606 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
607}
608
609/* timer callback for checking the ack of stop request */
610static void snd_korg1212_timer_func(unsigned long data)
611{
fcfd3332 612 struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
b32425ac 613 unsigned long flags;
1da177e4 614
b32425ac 615 spin_lock_irqsave(&korg1212->lock, flags);
1da177e4
LT
616 if (korg1212->sharedBufferPtr->cardCommand == 0) {
617 /* ack'ed */
618 korg1212->stop_pending_cnt = 0;
619 korg1212->dsp_stop_is_processed = 1;
620 wake_up(&korg1212->wait);
9fd9156c
TI
621 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
622 stateName[korg1212->cardState]);
1da177e4
LT
623 } else {
624 if (--korg1212->stop_pending_cnt > 0) {
625 /* reprogram timer */
626 korg1212->timer.expires = jiffies + 1;
627 add_timer(&korg1212->timer);
628 } else {
629 snd_printd("korg1212_timer_func timeout\n");
630 korg1212->sharedBufferPtr->cardCommand = 0;
631 korg1212->dsp_stop_is_processed = 1;
632 wake_up(&korg1212->wait);
9fd9156c
TI
633 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
634 stateName[korg1212->cardState]);
1da177e4
LT
635 }
636 }
b32425ac 637 spin_unlock_irqrestore(&korg1212->lock, flags);
1da177e4
LT
638}
639
fcfd3332 640static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
1da177e4
LT
641{
642 unsigned long flags;
9fd9156c 643 int rc;
1da177e4
LT
644
645 udelay(INTERCOMMAND_DELAY);
646 spin_lock_irqsave(&korg1212->lock, flags);
647 korg1212->idleMonitorOn = 1;
648 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
649 K1212_MODE_MonitorOn, 0, 0, 0);
650 spin_unlock_irqrestore(&korg1212->lock, flags);
9fd9156c 651 return rc;
1da177e4
LT
652}
653
fcfd3332 654static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
1da177e4
LT
655{
656 if (korg1212->idleMonitorOn) {
657 snd_korg1212_SendStopAndWait(korg1212);
658 korg1212->idleMonitorOn = 0;
659 }
660}
661
fcfd3332 662static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
1da177e4
LT
663{
664 korg1212->cardState = csState;
665}
666
fcfd3332 667static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
1da177e4 668{
9fd9156c
TI
669 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
670 stateName[korg1212->cardState], korg1212->opencnt);
62932df8 671 mutex_lock(&korg1212->open_mutex);
1da177e4
LT
672 if (korg1212->opencnt++ == 0) {
673 snd_korg1212_TurnOffIdleMonitor(korg1212);
674 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
675 }
676
62932df8 677 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
678 return 1;
679}
680
fcfd3332 681static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
1da177e4 682{
9fd9156c
TI
683 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
684 stateName[korg1212->cardState], korg1212->opencnt);
1da177e4 685
62932df8 686 mutex_lock(&korg1212->open_mutex);
1da177e4 687 if (--(korg1212->opencnt)) {
62932df8 688 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
689 return 0;
690 }
691
692 if (korg1212->cardState == K1212_STATE_SETUP) {
9fd9156c 693 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1da177e4 694 K1212_MODE_StopPlay, 0, 0, 0);
9fd9156c
TI
695 if (rc)
696 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
697 rc, stateName[korg1212->cardState]);
1da177e4 698 if (rc != K1212_CMDRET_Success) {
62932df8 699 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
700 return 0;
701 }
702 } else if (korg1212->cardState > K1212_STATE_SETUP) {
703 snd_korg1212_SendStopAndWait(korg1212);
704 }
705
706 if (korg1212->cardState > K1212_STATE_READY) {
707 snd_korg1212_TurnOnIdleMonitor(korg1212);
708 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
709 }
710
62932df8 711 mutex_unlock(&korg1212->open_mutex);
1da177e4
LT
712 return 0;
713}
714
715/* spinlock already held */
fcfd3332 716static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
1da177e4 717{
9fd9156c
TI
718 int rc;
719
720 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
721 stateName[korg1212->cardState], korg1212->setcnt);
1da177e4
LT
722
723 if (korg1212->setcnt++)
724 return 0;
725
726 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
727 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
728 K1212_MODE_SetupPlay, 0, 0, 0);
9fd9156c
TI
729 if (rc)
730 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
731 rc, stateName[korg1212->cardState]);
1da177e4
LT
732 if (rc != K1212_CMDRET_Success) {
733 return 1;
734 }
735 return 0;
736}
737
738/* spinlock already held */
fcfd3332 739static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
1da177e4 740{
9fd9156c
TI
741 int rc;
742
743 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
744 stateName[korg1212->cardState], korg1212->playcnt);
1da177e4
LT
745
746 if (korg1212->playcnt++)
747 return 0;
748
749 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
750 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
9fd9156c
TI
751 if (rc)
752 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
753 rc, stateName[korg1212->cardState]);
1da177e4
LT
754 if (rc != K1212_CMDRET_Success) {
755 return 1;
756 }
757 return 0;
758}
759
760/* spinlock already held */
fcfd3332 761static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
1da177e4 762{
9fd9156c
TI
763 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
764 stateName[korg1212->cardState], korg1212->playcnt);
1da177e4
LT
765
766 if (--(korg1212->playcnt))
767 return 0;
768
769 korg1212->setcnt = 0;
770
771 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
772 snd_korg1212_SendStop(korg1212);
773
774 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
775 return 0;
776}
777
fcfd3332 778static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
1da177e4
LT
779{
780 writel(PCI_INT_ENABLE_BIT |
781 PCI_DOORBELL_INT_ENABLE_BIT |
782 LOCAL_INT_ENABLE_BIT |
783 LOCAL_DOORBELL_INT_ENABLE_BIT |
784 LOCAL_DMA1_INT_ENABLE_BIT,
785 korg1212->statusRegPtr);
786}
787
788#if 0 /* not used */
789
fcfd3332
TI
790static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
791 enum MonitorModeSelector mode)
1da177e4 792{
9fd9156c
TI
793 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
794 stateName[korg1212->cardState]);
1da177e4
LT
795
796 switch (mode) {
9fd9156c
TI
797 case K1212_MONMODE_Off:
798 if (korg1212->cardState != K1212_STATE_MONITOR)
799 return 0;
800 else {
801 snd_korg1212_SendStopAndWait(korg1212);
802 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
803 }
804 break;
805
806 case K1212_MONMODE_On:
807 if (korg1212->cardState != K1212_STATE_OPEN)
808 return 0;
809 else {
810 int rc;
811 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
812 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
813 K1212_MODE_MonitorOn, 0, 0, 0);
814 if (rc != K1212_CMDRET_Success)
815 return 0;
816 }
817 break;
1da177e4 818
9fd9156c
TI
819 default:
820 return 0;
1da177e4
LT
821 }
822
823 return 1;
824}
825
826#endif /* not used */
827
fcfd3332 828static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
1da177e4 829{
9fd9156c
TI
830 if (korg1212->playback_pid != korg1212->capture_pid &&
831 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
832 return 0;
1da177e4 833
9fd9156c 834 return 1;
1da177e4
LT
835}
836
fcfd3332 837static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
1da177e4 838{
fcfd3332 839 static enum ClockSourceIndex s44[] = {
9fd9156c
TI
840 K1212_CLKIDX_AdatAt44_1K,
841 K1212_CLKIDX_WordAt44_1K,
842 K1212_CLKIDX_LocalAt44_1K
843 };
fcfd3332 844 static enum ClockSourceIndex s48[] = {
9fd9156c
TI
845 K1212_CLKIDX_AdatAt48K,
846 K1212_CLKIDX_WordAt48K,
847 K1212_CLKIDX_LocalAt48K
848 };
849 int parm, rc;
1da177e4 850
9fd9156c
TI
851 if (!snd_korg1212_use_is_exclusive (korg1212))
852 return -EBUSY;
1da177e4 853
fcfd3332 854 switch (rate) {
9fd9156c
TI
855 case 44100:
856 parm = s44[korg1212->clkSource];
857 break;
1da177e4 858
9fd9156c
TI
859 case 48000:
860 parm = s48[korg1212->clkSource];
861 break;
1da177e4 862
9fd9156c
TI
863 default:
864 return -EINVAL;
865 }
1da177e4
LT
866
867 korg1212->clkSrcRate = parm;
868 korg1212->clkRate = rate;
869
870 udelay(INTERCOMMAND_DELAY);
871 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
872 ClockSourceSelector[korg1212->clkSrcRate],
873 0, 0, 0);
9fd9156c
TI
874 if (rc)
875 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
876 rc, stateName[korg1212->cardState]);
1da177e4
LT
877
878 return 0;
879}
880
fcfd3332 881static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
1da177e4
LT
882{
883
9fd9156c
TI
884 if (source < 0 || source > 2)
885 return -EINVAL;
1da177e4
LT
886
887 korg1212->clkSource = source;
888
889 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
890
891 return 0;
892}
893
fcfd3332 894static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
1da177e4
LT
895{
896 writel(0, korg1212->statusRegPtr);
897}
898
fcfd3332 899static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
1da177e4 900{
fcfd3332 901 struct SensBits sensVals;
1da177e4
LT
902 int bitPosition;
903 int channel;
904 int clkIs48K;
905 int monModeSet;
906 u16 controlValue; // this keeps the current value to be written to
907 // the card's eeprom control register.
908 u16 count;
909 unsigned long flags;
910
9fd9156c
TI
911 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
912 stateName[korg1212->cardState]);
1da177e4
LT
913
914 // ----------------------------------------------------------------------------
915 // initialize things. The local init bit is always set when writing to the
916 // card's control register.
917 // ----------------------------------------------------------------------------
918 controlValue = 0;
919 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
920
921 // ----------------------------------------------------------------------------
922 // make sure the card is not in monitor mode when we do this update.
923 // ----------------------------------------------------------------------------
924 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
925 monModeSet = 1;
926 snd_korg1212_SendStopAndWait(korg1212);
927 } else
928 monModeSet = 0;
929
930 spin_lock_irqsave(&korg1212->lock, flags);
931
932 // ----------------------------------------------------------------------------
933 // we are about to send new values to the card, so clear the new values queued
934 // flag. Also, clear out mailbox 3, so we don't lockup.
935 // ----------------------------------------------------------------------------
936 writel(0, korg1212->mailbox3Ptr);
937 udelay(LOADSHIFT_DELAY);
938
939 // ----------------------------------------------------------------------------
940 // determine whether we are running a 48K or 44.1K clock. This info is used
941 // later when setting the SPDIF FF after the volume has been shifted in.
942 // ----------------------------------------------------------------------------
943 switch (korg1212->clkSrcRate) {
944 case K1212_CLKIDX_AdatAt44_1K:
945 case K1212_CLKIDX_WordAt44_1K:
946 case K1212_CLKIDX_LocalAt44_1K:
947 clkIs48K = 0;
948 break;
949
950 case K1212_CLKIDX_WordAt48K:
951 case K1212_CLKIDX_AdatAt48K:
952 case K1212_CLKIDX_LocalAt48K:
953 default:
954 clkIs48K = 1;
955 break;
956 }
957
958 // ----------------------------------------------------------------------------
959 // start the update. Setup the bit structure and then shift the bits.
960 // ----------------------------------------------------------------------------
961 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
962 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
963 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
964 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
965
966 // ----------------------------------------------------------------------------
967 // now start shifting the bits in. Start with the left channel then the right.
968 // ----------------------------------------------------------------------------
969 for (channel = 0; channel < 2; channel++) {
970
971 // ----------------------------------------------------------------------------
972 // Bring the load/shift line low, then wait - the spec says >150ns from load/
973 // shift low to the first rising edge of the clock.
974 // ----------------------------------------------------------------------------
975 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
976 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
977 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
978 udelay(LOADSHIFT_DELAY);
979
980 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
9fd9156c
TI
981 if (channel == 0) {
982 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
fcfd3332 983 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
9fd9156c
TI
984 else
985 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
986 } else {
987 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
988 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
989 else
990 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
991 }
1da177e4
LT
992
993 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
994 writew(controlValue, korg1212->sensRegPtr); // clock goes low
995 udelay(SENSCLKPULSE_WIDTH);
996 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
997 writew(controlValue, korg1212->sensRegPtr); // clock goes high
998 udelay(SENSCLKPULSE_WIDTH);
999 }
1000
1001 // ----------------------------------------------------------------------------
1002 // finish up SPDIF for left. Bring the load/shift line high, then write a one
1003 // bit if the clock rate is 48K otherwise write 0.
1004 // ----------------------------------------------------------------------------
1005 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1006 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1007 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
1008 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1009 udelay(SENSCLKPULSE_WIDTH);
1010
1011 if (clkIs48K)
1012 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1013
1014 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1015 udelay(ONE_RTC_TICK);
1016 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1017 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1018 udelay(SENSCLKPULSE_WIDTH);
1019 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1020 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1021 udelay(SENSCLKPULSE_WIDTH);
1022 }
1023
1024 // ----------------------------------------------------------------------------
1025 // The update is complete. Set a timeout. This is the inter-update delay.
1026 // Also, if the card was in monitor mode, restore it.
1027 // ----------------------------------------------------------------------------
1028 for (count = 0; count < 10; count++)
1029 udelay(SENSCLKPULSE_WIDTH);
1030
1031 if (monModeSet) {
fcfd3332 1032 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
1da177e4 1033 K1212_MODE_MonitorOn, 0, 0, 0);
9fd9156c
TI
1034 if (rc)
1035 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1036 rc, stateName[korg1212->cardState]);
1da177e4
LT
1037 }
1038
1039 spin_unlock_irqrestore(&korg1212->lock, flags);
1040
1041 return 1;
1042}
1043
fcfd3332 1044static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
1da177e4 1045{
fcfd3332 1046 int channel, rc;
1da177e4 1047
9fd9156c
TI
1048 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1049 stateName[korg1212->cardState]);
1da177e4
LT
1050
1051 // ----------------------------------------------------
1052 // tell the card to boot
1053 // ----------------------------------------------------
1054 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1055
9fd9156c
TI
1056 if (rc)
1057 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1058 rc, stateName[korg1212->cardState]);
1059 msleep(DSP_BOOT_DELAY_IN_MS);
1da177e4
LT
1060
1061 // --------------------------------------------------------------------------------
1062 // Let the card know where all the buffers are.
1063 // --------------------------------------------------------------------------------
1064 rc = snd_korg1212_Send1212Command(korg1212,
1065 K1212_DB_ConfigureBufferMemory,
1066 LowerWordSwap(korg1212->PlayDataPhy),
1067 LowerWordSwap(korg1212->RecDataPhy),
1068 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1069 // is based on 2 buffers
1070 0
1071 );
1072
9fd9156c
TI
1073 if (rc)
1074 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1075 rc, stateName[korg1212->cardState]);
1da177e4
LT
1076
1077 udelay(INTERCOMMAND_DELAY);
1078
1079 rc = snd_korg1212_Send1212Command(korg1212,
1080 K1212_DB_ConfigureMiscMemory,
1081 LowerWordSwap(korg1212->VolumeTablePhy),
1082 LowerWordSwap(korg1212->RoutingTablePhy),
1083 LowerWordSwap(korg1212->AdatTimeCodePhy),
1084 0
1085 );
1086
9fd9156c
TI
1087 if (rc)
1088 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1089 rc, stateName[korg1212->cardState]);
1da177e4
LT
1090
1091 // --------------------------------------------------------------------------------
1092 // Initialize the routing and volume tables, then update the card's state.
1093 // --------------------------------------------------------------------------------
1094 udelay(INTERCOMMAND_DELAY);
1095
1096 for (channel = 0; channel < kAudioChannels; channel++) {
1097 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1098 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1099 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1100 }
1101
1102 snd_korg1212_WriteADCSensitivity(korg1212);
1103
1104 udelay(INTERCOMMAND_DELAY);
1105 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1106 ClockSourceSelector[korg1212->clkSrcRate],
1107 0, 0, 0);
9fd9156c
TI
1108 if (rc)
1109 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1110 rc, stateName[korg1212->cardState]);
1da177e4 1111
9fd9156c 1112 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
1da177e4
LT
1113 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1114
9fd9156c
TI
1115 if (rc)
1116 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1117 rc, stateName[korg1212->cardState]);
1da177e4
LT
1118
1119 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1120}
1121
7d12e780 1122static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
1da177e4
LT
1123{
1124 u32 doorbellValue;
fcfd3332 1125 struct snd_korg1212 *korg1212 = dev_id;
1da177e4 1126
1da177e4
LT
1127 doorbellValue = readl(korg1212->inDoorbellPtr);
1128
1129 if (!doorbellValue)
1130 return IRQ_NONE;
1131
1132 spin_lock(&korg1212->lock);
1133
1134 writel(doorbellValue, korg1212->inDoorbellPtr);
1135
1136 korg1212->irqcount++;
1137
1138 korg1212->inIRQ++;
1139
1da177e4
LT
1140 switch (doorbellValue) {
1141 case K1212_DB_DSPDownloadDone:
9fd9156c
TI
1142 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1143 korg1212->irqcount, doorbellValue,
1144 stateName[korg1212->cardState]);
1da177e4
LT
1145 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1146 korg1212->dsp_is_loaded = 1;
1147 wake_up(&korg1212->wait);
1148 }
1149 break;
1150
1151 // ------------------------------------------------------------------------
1152 // an error occurred - stop the card
1153 // ------------------------------------------------------------------------
1154 case K1212_DB_DMAERROR:
9fd9156c
TI
1155 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1156 korg1212->irqcount, doorbellValue,
1157 stateName[korg1212->cardState]);
1158 snd_printk(KERN_ERR "korg1212: DMA Error\n");
1da177e4
LT
1159 korg1212->errorcnt++;
1160 korg1212->totalerrorcnt++;
1161 korg1212->sharedBufferPtr->cardCommand = 0;
1162 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1163 break;
1164
1165 // ------------------------------------------------------------------------
1166 // the card has stopped by our request. Clear the command word and signal
1167 // the semaphore in case someone is waiting for this.
1168 // ------------------------------------------------------------------------
1169 case K1212_DB_CARDSTOPPED:
fcfd3332 1170 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
9fd9156c
TI
1171 korg1212->irqcount, doorbellValue,
1172 stateName[korg1212->cardState]);
1da177e4
LT
1173 korg1212->sharedBufferPtr->cardCommand = 0;
1174 break;
1175
1176 default:
9fd9156c 1177 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
fcfd3332
TI
1178 korg1212->irqcount, doorbellValue,
1179 korg1212->currentBuffer, stateName[korg1212->cardState]);
1da177e4
LT
1180 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1181 korg1212->currentBuffer++;
1182
1183 if (korg1212->currentBuffer >= kNumBuffers)
1184 korg1212->currentBuffer = 0;
1185
1186 if (!korg1212->running)
1187 break;
1188
1189 if (korg1212->capture_substream) {
1190 spin_unlock(&korg1212->lock);
1191 snd_pcm_period_elapsed(korg1212->capture_substream);
1192 spin_lock(&korg1212->lock);
1193 }
1194
1195 if (korg1212->playback_substream) {
1196 spin_unlock(&korg1212->lock);
1197 snd_pcm_period_elapsed(korg1212->playback_substream);
1198 spin_lock(&korg1212->lock);
1199 }
1200 }
1201 break;
1202 }
1203
1204 korg1212->inIRQ--;
1205
1206 spin_unlock(&korg1212->lock);
1207
1208 return IRQ_HANDLED;
1209}
1210
fcfd3332 1211static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
1da177e4 1212{
fcfd3332 1213 int rc;
1da177e4 1214
9fd9156c
TI
1215 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1216 stateName[korg1212->cardState]);
1da177e4
LT
1217
1218 // ---------------------------------------------------------------
1219 // verify the state of the card before proceeding.
1220 // ---------------------------------------------------------------
9fd9156c 1221 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
1da177e4 1222 return 1;
1da177e4
LT
1223
1224 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1225
1226 memcpy(korg1212->dma_dsp.area, dspCode, korg1212->dspCodeSize);
1227
1228 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1229 UpperWordSwap(korg1212->dma_dsp.addr),
1230 0, 0, 0);
9fd9156c
TI
1231 if (rc)
1232 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1233 rc, stateName[korg1212->cardState]);
1da177e4
LT
1234
1235 korg1212->dsp_is_loaded = 0;
1236 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1237 if (! korg1212->dsp_is_loaded )
1238 return -EBUSY; /* timeout */
1239
1240 snd_korg1212_OnDSPDownloadComplete(korg1212);
1241
1242 return 0;
1243}
1244
fcfd3332 1245static struct snd_pcm_hardware snd_korg1212_playback_info =
1da177e4
LT
1246{
1247 .info = (SNDRV_PCM_INFO_MMAP |
1248 SNDRV_PCM_INFO_MMAP_VALID |
1249 SNDRV_PCM_INFO_INTERLEAVED),
1250 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1251 .rates = (SNDRV_PCM_RATE_44100 |
1252 SNDRV_PCM_RATE_48000),
1253 .rate_min = 44100,
1254 .rate_max = 48000,
1255 .channels_min = K1212_MIN_CHANNELS,
1256 .channels_max = K1212_MAX_CHANNELS,
1257 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1258 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1259 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1260 .periods_min = K1212_PERIODS,
1261 .periods_max = K1212_PERIODS,
1262 .fifo_size = 0,
1263};
1264
fcfd3332 1265static struct snd_pcm_hardware snd_korg1212_capture_info =
1da177e4
LT
1266{
1267 .info = (SNDRV_PCM_INFO_MMAP |
1268 SNDRV_PCM_INFO_MMAP_VALID |
1269 SNDRV_PCM_INFO_INTERLEAVED),
1270 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1271 .rates = (SNDRV_PCM_RATE_44100 |
1272 SNDRV_PCM_RATE_48000),
1273 .rate_min = 44100,
1274 .rate_max = 48000,
1275 .channels_min = K1212_MIN_CHANNELS,
1276 .channels_max = K1212_MAX_CHANNELS,
1277 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1278 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1279 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1280 .periods_min = K1212_PERIODS,
1281 .periods_max = K1212_PERIODS,
1282 .fifo_size = 0,
1283};
1284
fcfd3332 1285static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
1da177e4 1286{
fcfd3332 1287 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1da177e4
LT
1288 int i;
1289
9fd9156c
TI
1290 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1291 pos, offset, size, count);
1da177e4
LT
1292 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1293
1294 for (i=0; i < count; i++) {
1295#if K1212_DEBUG_LEVEL > 0
1296 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1297 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
9fd9156c
TI
1298 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1299 dst, i);
1da177e4
LT
1300 return -EFAULT;
1301 }
1302#endif
1303 memset((void*) dst + offset, 0, size);
1304 dst++;
1305 }
1306
1307 return 0;
1308}
1309
fcfd3332 1310static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
1da177e4 1311{
fcfd3332 1312 struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1da177e4
LT
1313 int i, rc;
1314
9fd9156c
TI
1315 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
1316 pos, offset, size);
1da177e4
LT
1317 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1318
1319 for (i=0; i < count; i++) {
1320#if K1212_DEBUG_LEVEL > 0
1321 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1322 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
9fd9156c 1323 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1324 return -EFAULT;
1325 }
1326#endif
1327 rc = copy_to_user(dst + offset, src, size);
1328 if (rc) {
1da177e4 1329 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1330 return -EFAULT;
1331 }
1332 src++;
1333 dst += size;
1334 }
1335
1336 return 0;
1337}
1338
fcfd3332 1339static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
1da177e4 1340{
fcfd3332 1341 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1da177e4
LT
1342 int i, rc;
1343
9fd9156c
TI
1344 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
1345 pos, offset, size, count);
1da177e4
LT
1346
1347 snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
1348
1349 for (i=0; i < count; i++) {
1350#if K1212_DEBUG_LEVEL > 0
1351 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1352 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
fcfd3332 1353 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1354 return -EFAULT;
1355 }
1356#endif
1357 rc = copy_from_user((void*) dst + offset, src, size);
1358 if (rc) {
fcfd3332 1359 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
1da177e4
LT
1360 return -EFAULT;
1361 }
1362 dst++;
1363 src += size;
1364 }
1365
1366 return 0;
1367}
1368
fcfd3332 1369static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
1da177e4 1370{
fcfd3332 1371 struct snd_korg1212 *korg1212 = pcm->private_data;
1da177e4 1372
9fd9156c
TI
1373 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1374 stateName[korg1212->cardState]);
1da177e4
LT
1375
1376 korg1212->pcm = NULL;
1377}
1378
fcfd3332 1379static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
1da177e4
LT
1380{
1381 unsigned long flags;
fcfd3332
TI
1382 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1383 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 1384
9fd9156c
TI
1385 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1386 stateName[korg1212->cardState]);
1da177e4
LT
1387
1388 snd_pcm_set_sync(substream); // ???
1389
1390 snd_korg1212_OpenCard(korg1212);
1391
1392 runtime->hw = snd_korg1212_playback_info;
1393 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1394
1395 spin_lock_irqsave(&korg1212->lock, flags);
1396
1397 korg1212->playback_substream = substream;
1398 korg1212->playback_pid = current->pid;
1399 korg1212->periodsize = K1212_PERIODS;
1400 korg1212->channels = K1212_CHANNELS;
1401 korg1212->errorcnt = 0;
1402
1403 spin_unlock_irqrestore(&korg1212->lock, flags);
1404
1405 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
1406 return 0;
1407}
1408
1409
fcfd3332 1410static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
1da177e4
LT
1411{
1412 unsigned long flags;
fcfd3332
TI
1413 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1414 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4 1415
9fd9156c
TI
1416 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1417 stateName[korg1212->cardState]);
1da177e4
LT
1418
1419 snd_pcm_set_sync(substream);
1420
1421 snd_korg1212_OpenCard(korg1212);
1422
1423 runtime->hw = snd_korg1212_capture_info;
1424 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1425
1426 spin_lock_irqsave(&korg1212->lock, flags);
1427
1428 korg1212->capture_substream = substream;
1429 korg1212->capture_pid = current->pid;
1430 korg1212->periodsize = K1212_PERIODS;
1431 korg1212->channels = K1212_CHANNELS;
1432
1433 spin_unlock_irqrestore(&korg1212->lock, flags);
1434
fcfd3332
TI
1435 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1436 kPlayBufferFrames, kPlayBufferFrames);
1da177e4
LT
1437 return 0;
1438}
1439
fcfd3332 1440static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
1da177e4
LT
1441{
1442 unsigned long flags;
fcfd3332 1443 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1444
9fd9156c
TI
1445 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1446 stateName[korg1212->cardState]);
1da177e4
LT
1447
1448 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1449
1450 spin_lock_irqsave(&korg1212->lock, flags);
1451
1452 korg1212->playback_pid = -1;
1453 korg1212->playback_substream = NULL;
1454 korg1212->periodsize = 0;
1455
1456 spin_unlock_irqrestore(&korg1212->lock, flags);
1457
1458 snd_korg1212_CloseCard(korg1212);
1459 return 0;
1460}
1461
fcfd3332 1462static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
1da177e4
LT
1463{
1464 unsigned long flags;
fcfd3332 1465 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1466
9fd9156c
TI
1467 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1468 stateName[korg1212->cardState]);
1da177e4
LT
1469
1470 spin_lock_irqsave(&korg1212->lock, flags);
1471
1472 korg1212->capture_pid = -1;
1473 korg1212->capture_substream = NULL;
1474 korg1212->periodsize = 0;
1475
1476 spin_unlock_irqrestore(&korg1212->lock, flags);
1477
1478 snd_korg1212_CloseCard(korg1212);
1479 return 0;
1480}
1481
fcfd3332 1482static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
1483 unsigned int cmd, void *arg)
1484{
9fd9156c 1485 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
1da177e4
LT
1486
1487 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
fcfd3332 1488 struct snd_pcm_channel_info *info = arg;
1da177e4
LT
1489 info->offset = 0;
1490 info->first = info->channel * 16;
1491 info->step = 256;
1da177e4 1492 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
1da177e4
LT
1493 return 0;
1494 }
1495
1496 return snd_pcm_lib_ioctl(substream, cmd, arg);
1497}
1498
fcfd3332
TI
1499static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1500 struct snd_pcm_hw_params *params)
1da177e4
LT
1501{
1502 unsigned long flags;
fcfd3332 1503 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1504 int err;
1505 pid_t this_pid;
1506 pid_t other_pid;
1507
9fd9156c
TI
1508 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1509 stateName[korg1212->cardState]);
1da177e4
LT
1510
1511 spin_lock_irqsave(&korg1212->lock, flags);
1512
1513 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1514 this_pid = korg1212->playback_pid;
1515 other_pid = korg1212->capture_pid;
1516 } else {
1517 this_pid = korg1212->capture_pid;
1518 other_pid = korg1212->playback_pid;
1519 }
1520
1521 if ((other_pid > 0) && (this_pid != other_pid)) {
1522
1523 /* The other stream is open, and not by the same
1524 task as this one. Make sure that the parameters
1525 that matter are the same.
1526 */
1527
1528 if ((int)params_rate(params) != korg1212->clkRate) {
1529 spin_unlock_irqrestore(&korg1212->lock, flags);
1530 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1531 return -EBUSY;
1532 }
1533
1534 spin_unlock_irqrestore(&korg1212->lock, flags);
1535 return 0;
1536 }
1537
1538 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1539 spin_unlock_irqrestore(&korg1212->lock, flags);
1540 return err;
1541 }
1542
1543 korg1212->channels = params_channels(params);
1544 korg1212->periodsize = K1212_PERIOD_BYTES;
1545
1546 spin_unlock_irqrestore(&korg1212->lock, flags);
1547
1548 return 0;
1549}
1550
fcfd3332 1551static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
1da177e4 1552{
fcfd3332 1553 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1554 int rc;
1555
9fd9156c
TI
1556 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1557 stateName[korg1212->cardState]);
1da177e4
LT
1558
1559 spin_lock_irq(&korg1212->lock);
1560
1561 /* FIXME: we should wait for ack! */
1562 if (korg1212->stop_pending_cnt > 0) {
9fd9156c
TI
1563 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1564 stateName[korg1212->cardState]);
1da177e4
LT
1565 spin_unlock_irq(&korg1212->lock);
1566 return -EAGAIN;
1567 /*
1568 korg1212->sharedBufferPtr->cardCommand = 0;
1569 del_timer(&korg1212->timer);
1570 korg1212->stop_pending_cnt = 0;
1571 */
1572 }
1573
1574 rc = snd_korg1212_SetupForPlay(korg1212);
1575
1576 korg1212->currentBuffer = 0;
1577
1578 spin_unlock_irq(&korg1212->lock);
1579
1580 return rc ? -EINVAL : 0;
1581}
1582
fcfd3332 1583static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
1da177e4
LT
1584 int cmd)
1585{
fcfd3332 1586 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1587 int rc;
1588
9fd9156c
TI
1589 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1590 stateName[korg1212->cardState], cmd);
1da177e4
LT
1591
1592 spin_lock(&korg1212->lock);
1593 switch (cmd) {
1594 case SNDRV_PCM_TRIGGER_START:
1595/*
1596 if (korg1212->running) {
9fd9156c 1597 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
1da177e4
LT
1598 break;
1599 }
1600*/
1601 korg1212->running++;
1602 rc = snd_korg1212_TriggerPlay(korg1212);
1603 break;
1604
1605 case SNDRV_PCM_TRIGGER_STOP:
1606/*
1607 if (!korg1212->running) {
9fd9156c 1608 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
1da177e4
LT
1609 break;
1610 }
1611*/
1612 korg1212->running--;
1613 rc = snd_korg1212_StopPlay(korg1212);
1614 break;
1615
1616 default:
1617 rc = 1;
1618 break;
1619 }
1620 spin_unlock(&korg1212->lock);
1621 return rc ? -EINVAL : 0;
1622}
1623
fcfd3332 1624static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
1da177e4 1625{
fcfd3332 1626 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1627 snd_pcm_uframes_t pos;
1628
1629 pos = korg1212->currentBuffer * kPlayBufferFrames;
1630
9fd9156c
TI
1631 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1632 stateName[korg1212->cardState], pos);
1da177e4
LT
1633
1634 return pos;
1635}
1636
fcfd3332 1637static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
1da177e4 1638{
fcfd3332 1639 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4
LT
1640 snd_pcm_uframes_t pos;
1641
1642 pos = korg1212->currentBuffer * kPlayBufferFrames;
1643
9fd9156c
TI
1644 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1645 stateName[korg1212->cardState], pos);
1da177e4
LT
1646
1647 return pos;
1648}
1649
fcfd3332 1650static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
1da177e4
LT
1651 int channel, /* not used (interleaved data) */
1652 snd_pcm_uframes_t pos,
1653 void __user *src,
1654 snd_pcm_uframes_t count)
1655{
fcfd3332 1656 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1657
9fd9156c
TI
1658 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
1659 stateName[korg1212->cardState], pos, count);
1da177e4
LT
1660
1661 return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
1662
1663}
1664
fcfd3332 1665static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
1da177e4
LT
1666 int channel, /* not used (interleaved data) */
1667 snd_pcm_uframes_t pos,
1668 snd_pcm_uframes_t count)
1669{
fcfd3332 1670 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1671
9fd9156c
TI
1672 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
1673 stateName[korg1212->cardState]);
1da177e4
LT
1674
1675 return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
1676}
1677
fcfd3332 1678static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
1da177e4
LT
1679 int channel, /* not used (interleaved data) */
1680 snd_pcm_uframes_t pos,
1681 void __user *dst,
1682 snd_pcm_uframes_t count)
1683{
fcfd3332 1684 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1da177e4 1685
9fd9156c
TI
1686 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
1687 stateName[korg1212->cardState], pos, count);
1da177e4
LT
1688
1689 return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
1690}
1691
fcfd3332 1692static struct snd_pcm_ops snd_korg1212_playback_ops = {
1da177e4
LT
1693 .open = snd_korg1212_playback_open,
1694 .close = snd_korg1212_playback_close,
1695 .ioctl = snd_korg1212_ioctl,
1696 .hw_params = snd_korg1212_hw_params,
1697 .prepare = snd_korg1212_prepare,
1698 .trigger = snd_korg1212_trigger,
1699 .pointer = snd_korg1212_playback_pointer,
1700 .copy = snd_korg1212_playback_copy,
1701 .silence = snd_korg1212_playback_silence,
1702};
1703
fcfd3332 1704static struct snd_pcm_ops snd_korg1212_capture_ops = {
1da177e4
LT
1705 .open = snd_korg1212_capture_open,
1706 .close = snd_korg1212_capture_close,
1707 .ioctl = snd_korg1212_ioctl,
1708 .hw_params = snd_korg1212_hw_params,
1709 .prepare = snd_korg1212_prepare,
1710 .trigger = snd_korg1212_trigger,
1711 .pointer = snd_korg1212_capture_pointer,
1712 .copy = snd_korg1212_capture_copy,
1713};
1714
1715/*
1716 * Control Interface
1717 */
1718
fcfd3332
TI
1719static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1721{
1722 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1723 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1724 return 0;
1725}
1726
fcfd3332
TI
1727static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *u)
1da177e4 1729{
fcfd3332 1730 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1731 int i = kcontrol->private_value;
1732
1733 spin_lock_irq(&korg1212->lock);
1734
1735 u->value.integer.value[0] = korg1212->volumePhase[i];
1736
1737 if (i >= 8)
1738 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1739
1740 spin_unlock_irq(&korg1212->lock);
1741
1742 return 0;
1743}
1744
fcfd3332
TI
1745static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_value *u)
1da177e4 1747{
fcfd3332 1748 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1749 int change = 0;
1750 int i, val;
1751
1752 spin_lock_irq(&korg1212->lock);
1753
1754 i = kcontrol->private_value;
1755
1756 korg1212->volumePhase[i] = u->value.integer.value[0];
1757
1758 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1759
1760 if ((u->value.integer.value[0] > 0) != (val < 0)) {
1761 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1762 korg1212->sharedBufferPtr->volumeData[i] = val;
1763 change = 1;
1764 }
1765
1766 if (i >= 8) {
1767 korg1212->volumePhase[i+1] = u->value.integer.value[1];
1768
1769 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1770
1771 if ((u->value.integer.value[1] > 0) != (val < 0)) {
1772 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1773 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1774 change = 1;
1775 }
1776 }
1777
1778 spin_unlock_irq(&korg1212->lock);
1779
1780 return change;
1781}
1782
fcfd3332
TI
1783static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1785{
1786 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1787 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1788 uinfo->value.integer.min = k1212MinVolume;
1789 uinfo->value.integer.max = k1212MaxVolume;
1790 return 0;
1791}
1792
fcfd3332
TI
1793static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1794 struct snd_ctl_elem_value *u)
1da177e4 1795{
fcfd3332 1796 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1797 int i;
1798
1799 spin_lock_irq(&korg1212->lock);
1800
1801 i = kcontrol->private_value;
1802 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1803
1804 if (i >= 8)
1805 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1806
1807 spin_unlock_irq(&korg1212->lock);
1808
1809 return 0;
1810}
1811
fcfd3332
TI
1812static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1813 struct snd_ctl_elem_value *u)
1da177e4 1814{
fcfd3332 1815 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1816 int change = 0;
1817 int i;
1818 int val;
1819
1820 spin_lock_irq(&korg1212->lock);
1821
1822 i = kcontrol->private_value;
1823
1824 if (u->value.integer.value[0] != abs(korg1212->sharedBufferPtr->volumeData[i])) {
1825 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1826 val *= u->value.integer.value[0];
1827 korg1212->sharedBufferPtr->volumeData[i] = val;
1828 change = 1;
1829 }
1830
1831 if (i >= 8) {
1832 if (u->value.integer.value[1] != abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
1833 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1834 val *= u->value.integer.value[1];
1835 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1836 change = 1;
1837 }
1838 }
1839
1840 spin_unlock_irq(&korg1212->lock);
1841
1842 return change;
1843}
1844
fcfd3332
TI
1845static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1847{
1848 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1849 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1850 uinfo->value.enumerated.items = kAudioChannels;
1851 if (uinfo->value.enumerated.item > kAudioChannels-1) {
1852 uinfo->value.enumerated.item = kAudioChannels-1;
1853 }
1854 strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
1855 return 0;
1856}
1857
fcfd3332
TI
1858static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1859 struct snd_ctl_elem_value *u)
1da177e4 1860{
fcfd3332 1861 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1862 int i;
1863
1864 spin_lock_irq(&korg1212->lock);
1865
1866 i = kcontrol->private_value;
1867 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1868
1869 if (i >= 8)
1870 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1871
1872 spin_unlock_irq(&korg1212->lock);
1873
1874 return 0;
1875}
1876
fcfd3332
TI
1877static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *u)
1da177e4 1879{
fcfd3332 1880 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1881 int change = 0, i;
1882
1883 spin_lock_irq(&korg1212->lock);
1884
1885 i = kcontrol->private_value;
1886
1887 if (u->value.enumerated.item[0] != (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
1888 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1889 change = 1;
1890 }
1891
1892 if (i >= 8) {
1893 if (u->value.enumerated.item[1] != (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
1894 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1895 change = 1;
1896 }
1897 }
1898
1899 spin_unlock_irq(&korg1212->lock);
1900
1901 return change;
1902}
1903
fcfd3332
TI
1904static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1906{
1907 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1908 uinfo->count = 2;
1909 uinfo->value.integer.min = k1212MaxADCSens;
1910 uinfo->value.integer.max = k1212MinADCSens;
1911 return 0;
1912}
1913
fcfd3332
TI
1914static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1915 struct snd_ctl_elem_value *u)
1da177e4 1916{
fcfd3332 1917 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1918
1919 spin_lock_irq(&korg1212->lock);
1920
1921 u->value.integer.value[0] = korg1212->leftADCInSens;
1922 u->value.integer.value[1] = korg1212->rightADCInSens;
1923
1924 spin_unlock_irq(&korg1212->lock);
1925
1926 return 0;
1927}
1928
fcfd3332
TI
1929static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *u)
1da177e4 1931{
fcfd3332 1932 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1933 int change = 0;
1934
1935 spin_lock_irq(&korg1212->lock);
1936
1937 if (u->value.integer.value[0] != korg1212->leftADCInSens) {
1938 korg1212->leftADCInSens = u->value.integer.value[0];
1939 change = 1;
1940 }
1941 if (u->value.integer.value[1] != korg1212->rightADCInSens) {
1942 korg1212->rightADCInSens = u->value.integer.value[1];
1943 change = 1;
1944 }
1945
1946 spin_unlock_irq(&korg1212->lock);
1947
1948 if (change)
1949 snd_korg1212_WriteADCSensitivity(korg1212);
1950
1951 return change;
1952}
1953
fcfd3332
TI
1954static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1956{
1957 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1958 uinfo->count = 1;
1959 uinfo->value.enumerated.items = 3;
1960 if (uinfo->value.enumerated.item > 2) {
1961 uinfo->value.enumerated.item = 2;
1962 }
1963 strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
1964 return 0;
1965}
1966
fcfd3332
TI
1967static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1968 struct snd_ctl_elem_value *ucontrol)
1da177e4 1969{
fcfd3332 1970 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1971
1972 spin_lock_irq(&korg1212->lock);
1973
1974 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1975
1976 spin_unlock_irq(&korg1212->lock);
1977 return 0;
1978}
1979
fcfd3332
TI
1980static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_value *ucontrol)
1da177e4 1982{
fcfd3332 1983 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1984 unsigned int val;
1985 int change;
1986
1987 val = ucontrol->value.enumerated.item[0] % 3;
1988 spin_lock_irq(&korg1212->lock);
1989 change = val != korg1212->clkSource;
1990 snd_korg1212_SetClockSource(korg1212, val);
1991 spin_unlock_irq(&korg1212->lock);
1992 return change;
1993}
1994
1995#define MON_MIXER(ord,c_name) \
1996 { \
1997 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
1998 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1999 .name = c_name " Monitor Volume", \
2000 .info = snd_korg1212_control_volume_info, \
2001 .get = snd_korg1212_control_volume_get, \
2002 .put = snd_korg1212_control_volume_put, \
2003 .private_value = ord, \
2004 }, \
2005 { \
2006 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2007 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2008 .name = c_name " Monitor Route", \
2009 .info = snd_korg1212_control_route_info, \
2010 .get = snd_korg1212_control_route_get, \
2011 .put = snd_korg1212_control_route_put, \
2012 .private_value = ord, \
2013 }, \
2014 { \
2015 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
67ed4161 2016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1da177e4
LT
2017 .name = c_name " Monitor Phase Invert", \
2018 .info = snd_korg1212_control_phase_info, \
2019 .get = snd_korg1212_control_phase_get, \
2020 .put = snd_korg1212_control_phase_put, \
2021 .private_value = ord, \
2022 }
2023
fcfd3332 2024static struct snd_kcontrol_new snd_korg1212_controls[] = {
1da177e4
LT
2025 MON_MIXER(8, "Analog"),
2026 MON_MIXER(10, "SPDIF"),
2027 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2028 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2029 {
2030 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
67ed4161 2031 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1da177e4
LT
2032 .name = "Sync Source",
2033 .info = snd_korg1212_control_sync_info,
2034 .get = snd_korg1212_control_sync_get,
2035 .put = snd_korg1212_control_sync_put,
2036 },
2037 {
2038 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2039 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2040 .name = "ADC Attenuation",
2041 .info = snd_korg1212_control_info,
2042 .get = snd_korg1212_control_get,
2043 .put = snd_korg1212_control_put,
2044 }
2045};
2046
2047/*
2048 * proc interface
2049 */
2050
fcfd3332
TI
2051static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2052 struct snd_info_buffer *buffer)
1da177e4
LT
2053{
2054 int n;
fcfd3332 2055 struct snd_korg1212 *korg1212 = entry->private_data;
1da177e4
LT
2056
2057 snd_iprintf(buffer, korg1212->card->longname);
2058 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2059 snd_iprintf(buffer, "\nGeneral settings\n");
2060 snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
2061 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2062 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2063 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2064 snd_iprintf(buffer, " Volume Info:\n");
2065 for (n=0; n<kAudioChannels; n++)
2066 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2067 channelName[n],
2068 channelName[korg1212->sharedBufferPtr->routeData[n]],
2069 korg1212->sharedBufferPtr->volumeData[n]);
2070 snd_iprintf(buffer, "\nGeneral status\n");
2071 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2072 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2073 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2074 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2075 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2076 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2077}
2078
fcfd3332 2079static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
1da177e4 2080{
fcfd3332 2081 struct snd_info_entry *entry;
1da177e4
LT
2082
2083 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
bf850204 2084 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
1da177e4
LT
2085}
2086
2087static int
fcfd3332 2088snd_korg1212_free(struct snd_korg1212 *korg1212)
1da177e4
LT
2089{
2090 snd_korg1212_TurnOffIdleMonitor(korg1212);
2091
2092 if (korg1212->irq >= 0) {
2093 synchronize_irq(korg1212->irq);
2094 snd_korg1212_DisableCardInterrupts(korg1212);
9fd9156c 2095 free_irq(korg1212->irq, korg1212);
1da177e4
LT
2096 korg1212->irq = -1;
2097 }
2098
2099 if (korg1212->iobase != NULL) {
2100 iounmap(korg1212->iobase);
2101 korg1212->iobase = NULL;
2102 }
2103
2104 pci_release_regions(korg1212->pci);
2105
2106 // ----------------------------------------------------
2107 // free up memory resources used for the DSP download.
2108 // ----------------------------------------------------
2109 if (korg1212->dma_dsp.area) {
2110 snd_dma_free_pages(&korg1212->dma_dsp);
2111 korg1212->dma_dsp.area = NULL;
2112 }
2113
2114#ifndef K1212_LARGEALLOC
2115
2116 // ------------------------------------------------------
2117 // free up memory resources used for the Play/Rec Buffers
2118 // ------------------------------------------------------
2119 if (korg1212->dma_play.area) {
2120 snd_dma_free_pages(&korg1212->dma_play);
2121 korg1212->dma_play.area = NULL;
2122 }
2123
2124 if (korg1212->dma_rec.area) {
2125 snd_dma_free_pages(&korg1212->dma_rec);
2126 korg1212->dma_rec.area = NULL;
2127 }
2128
2129#endif
2130
2131 // ----------------------------------------------------
2132 // free up memory resources used for the Shared Buffers
2133 // ----------------------------------------------------
2134 if (korg1212->dma_shared.area) {
2135 snd_dma_free_pages(&korg1212->dma_shared);
2136 korg1212->dma_shared.area = NULL;
2137 }
2138
2139 pci_disable_device(korg1212->pci);
2140 kfree(korg1212);
2141 return 0;
2142}
2143
fcfd3332 2144static int snd_korg1212_dev_free(struct snd_device *device)
1da177e4 2145{
fcfd3332 2146 struct snd_korg1212 *korg1212 = device->device_data;
1da177e4 2147 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
1da177e4
LT
2148 return snd_korg1212_free(korg1212);
2149}
2150
fcfd3332
TI
2151static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2152 struct snd_korg1212 ** rchip)
1da177e4
LT
2153
2154{
9fd9156c 2155 int err, rc;
1da177e4
LT
2156 unsigned int i;
2157 unsigned ioport_size, iomem_size, iomem2_size;
fcfd3332 2158 struct snd_korg1212 * korg1212;
1da177e4 2159
fcfd3332 2160 static struct snd_device_ops ops = {
1da177e4
LT
2161 .dev_free = snd_korg1212_dev_free,
2162 };
2163
2164 * rchip = NULL;
2165 if ((err = pci_enable_device(pci)) < 0)
2166 return err;
2167
e560d8d8 2168 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
1da177e4
LT
2169 if (korg1212 == NULL) {
2170 pci_disable_device(pci);
2171 return -ENOMEM;
2172 }
2173
2174 korg1212->card = card;
2175 korg1212->pci = pci;
2176
2177 init_waitqueue_head(&korg1212->wait);
2178 spin_lock_init(&korg1212->lock);
62932df8 2179 mutex_init(&korg1212->open_mutex);
1da177e4
LT
2180 init_timer(&korg1212->timer);
2181 korg1212->timer.function = snd_korg1212_timer_func;
2182 korg1212->timer.data = (unsigned long)korg1212;
2183
2184 korg1212->irq = -1;
2185 korg1212->clkSource = K1212_CLKIDX_Local;
2186 korg1212->clkRate = 44100;
2187 korg1212->inIRQ = 0;
2188 korg1212->running = 0;
2189 korg1212->opencnt = 0;
2190 korg1212->playcnt = 0;
2191 korg1212->setcnt = 0;
2192 korg1212->totalerrorcnt = 0;
2193 korg1212->playback_pid = -1;
2194 korg1212->capture_pid = -1;
2195 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2196 korg1212->idleMonitorOn = 0;
2197 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2198 korg1212->leftADCInSens = k1212MaxADCSens;
2199 korg1212->rightADCInSens = k1212MaxADCSens;
2200
2201 for (i=0; i<kAudioChannels; i++)
2202 korg1212->volumePhase[i] = 0;
2203
2204 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2205 kfree(korg1212);
2206 pci_disable_device(pci);
2207 return err;
2208 }
2209
2210 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2211 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2212 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2213
2214 iomem_size = pci_resource_len(korg1212->pci, 0);
2215 ioport_size = pci_resource_len(korg1212->pci, 1);
2216 iomem2_size = pci_resource_len(korg1212->pci, 2);
2217
1da177e4
LT
2218 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2219 " iomem = 0x%lx (%d)\n"
2220 " ioport = 0x%lx (%d)\n"
2221 " iomem = 0x%lx (%d)\n"
2222 " [%s]\n",
2223 korg1212->iomem, iomem_size,
2224 korg1212->ioport, ioport_size,
2225 korg1212->iomem2, iomem2_size,
2226 stateName[korg1212->cardState]);
1da177e4
LT
2227
2228 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2229 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2230 korg1212->iomem + iomem_size - 1);
2231 snd_korg1212_free(korg1212);
2232 return -EBUSY;
2233 }
2234
2235 err = request_irq(pci->irq, snd_korg1212_interrupt,
65ca68b3 2236 IRQF_DISABLED|IRQF_SHARED,
9fd9156c 2237 "korg1212", korg1212);
1da177e4
LT
2238
2239 if (err) {
2240 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2241 snd_korg1212_free(korg1212);
2242 return -EBUSY;
2243 }
2244
2245 korg1212->irq = pci->irq;
2246
2247 pci_set_master(korg1212->pci);
2248
2249 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2250 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2251 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2252 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2253 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2254 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2255 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2256 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2257 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2258 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2259
1da177e4
LT
2260 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2261 " Status register = 0x%p\n"
2262 " OutDoorbell = 0x%p\n"
2263 " InDoorbell = 0x%p\n"
2264 " Mailbox0 = 0x%p\n"
2265 " Mailbox1 = 0x%p\n"
2266 " Mailbox2 = 0x%p\n"
2267 " Mailbox3 = 0x%p\n"
2268 " ControlReg = 0x%p\n"
2269 " SensReg = 0x%p\n"
2270 " IDReg = 0x%p\n"
2271 " [%s]\n",
2272 korg1212->statusRegPtr,
2273 korg1212->outDoorbellPtr,
2274 korg1212->inDoorbellPtr,
2275 korg1212->mailbox0Ptr,
2276 korg1212->mailbox1Ptr,
2277 korg1212->mailbox2Ptr,
2278 korg1212->mailbox3Ptr,
2279 korg1212->controlRegPtr,
2280 korg1212->sensRegPtr,
2281 korg1212->idRegPtr,
2282 stateName[korg1212->cardState]);
1da177e4
LT
2283
2284 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
fcfd3332
TI
2285 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
2286 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
1da177e4
LT
2287 snd_korg1212_free(korg1212);
2288 return -ENOMEM;
2289 }
fcfd3332 2290 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
1da177e4
LT
2291 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2292
fcfd3332 2293 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
1da177e4
LT
2294
2295#ifndef K1212_LARGEALLOC
2296
fcfd3332 2297 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
1da177e4
LT
2298
2299 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2300 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2301 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2302 snd_korg1212_free(korg1212);
2303 return -ENOMEM;
2304 }
fcfd3332 2305 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
1da177e4
LT
2306 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2307
1da177e4
LT
2308 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2309 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
1da177e4
LT
2310
2311 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2312 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2313 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2314 snd_korg1212_free(korg1212);
2315 return -ENOMEM;
2316 }
fcfd3332 2317 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
1da177e4
LT
2318 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2319
1da177e4
LT
2320 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2321 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
1da177e4
LT
2322
2323#else // K1212_LARGEALLOC
2324
2325 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2326 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
fcfd3332
TI
2327 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2328 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
1da177e4
LT
2329
2330#endif // K1212_LARGEALLOC
2331
2332 korg1212->dspCodeSize = sizeof (dspCode);
2333
2334 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
fcfd3332 2335 offsetof(struct KorgSharedBuffer, volumeData);
1da177e4 2336 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
fcfd3332 2337 offsetof(struct KorgSharedBuffer, routeData);
1da177e4 2338 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
fcfd3332 2339 offsetof(struct KorgSharedBuffer, AdatTimeCode);
1da177e4
LT
2340
2341 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2342 korg1212->dspCodeSize, &korg1212->dma_dsp) < 0) {
2343 snd_printk(KERN_ERR "korg1212: can not allocate dsp code memory (%d bytes)\n", korg1212->dspCodeSize);
2344 snd_korg1212_free(korg1212);
2345 return -ENOMEM;
2346 }
2347
1da177e4
LT
2348 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
2349 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, korg1212->dspCodeSize,
2350 stateName[korg1212->cardState]);
1da177e4
LT
2351
2352 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2353
9fd9156c
TI
2354 if (rc)
2355 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
1da177e4
LT
2356
2357 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2358 snd_korg1212_free(korg1212);
2359 return err;
2360 }
2361
2362 snd_korg1212_EnableCardInterrupts(korg1212);
2363
2364 mdelay(CARD_BOOT_DELAY_IN_MS);
2365
2366 if (snd_korg1212_downloadDSPCode(korg1212))
2367 return -EBUSY;
2368
fcfd3332 2369 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
1da177e4
LT
2370 "PlayDataPhy = %08x L[%08x]\n"
2371 "korg1212: RecDataPhy = %08x L[%08x], "
2372 "VolumeTablePhy = %08x L[%08x]\n"
2373 "korg1212: RoutingTablePhy = %08x L[%08x], "
2374 "AdatTimeCodePhy = %08x L[%08x]\n",
2375 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2376 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2377 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2378 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2379 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2380 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2381
2382 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2383 return err;
2384
2385 korg1212->pcm->private_data = korg1212;
2386 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2387 strcpy(korg1212->pcm->name, "korg1212");
2388
2389 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2390
2391 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2392
2393 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2394
1da177e4
LT
2395 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2396 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2397 if (err < 0)
2398 return err;
2399 }
2400
2401 snd_korg1212_proc_init(korg1212);
2402
2403 snd_card_set_dev(card, &pci->dev);
2404
2405 * rchip = korg1212;
2406 return 0;
2407
2408}
2409
2410/*
2411 * Card initialisation
2412 */
2413
2414static int __devinit
2415snd_korg1212_probe(struct pci_dev *pci,
2416 const struct pci_device_id *pci_id)
2417{
2418 static int dev;
fcfd3332
TI
2419 struct snd_korg1212 *korg1212;
2420 struct snd_card *card;
1da177e4
LT
2421 int err;
2422
2423 if (dev >= SNDRV_CARDS) {
2424 return -ENODEV;
2425 }
2426 if (!enable[dev]) {
2427 dev++;
2428 return -ENOENT;
2429 }
2430 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2431 if (card == NULL)
2432 return -ENOMEM;
2433
2434 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2435 snd_card_free(card);
2436 return err;
2437 }
2438
2439 strcpy(card->driver, "korg1212");
2440 strcpy(card->shortname, "korg1212");
2441 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2442 korg1212->iomem, korg1212->irq);
2443
1da177e4 2444 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
1da177e4
LT
2445
2446 if ((err = snd_card_register(card)) < 0) {
2447 snd_card_free(card);
2448 return err;
2449 }
2450 pci_set_drvdata(pci, card);
2451 dev++;
2452 return 0;
2453}
2454
2455static void __devexit snd_korg1212_remove(struct pci_dev *pci)
2456{
2457 snd_card_free(pci_get_drvdata(pci));
2458 pci_set_drvdata(pci, NULL);
2459}
2460
2461static struct pci_driver driver = {
2462 .name = "korg1212",
2463 .id_table = snd_korg1212_ids,
2464 .probe = snd_korg1212_probe,
2465 .remove = __devexit_p(snd_korg1212_remove),
2466};
2467
2468static int __init alsa_card_korg1212_init(void)
2469{
01d25d46 2470 return pci_register_driver(&driver);
1da177e4
LT
2471}
2472
2473static void __exit alsa_card_korg1212_exit(void)
2474{
2475 pci_unregister_driver(&driver);
2476}
2477
2478module_init(alsa_card_korg1212_init)
2479module_exit(alsa_card_korg1212_exit)