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