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