blob: 4e189a93f475ee268673264fd306483662309b5f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the Korg 1212 IO PCI card
3 *
4 * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/slab.h>
27#include <linux/wait.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040028#include <linux/module.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010029#include <linux/mutex.h>
Clemens Ladisch2493a6d2006-11-06 09:24:29 +010030#include <linux/firmware.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010031#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040// ----------------------------------------------------------------------------
41// Debug Stuff
42// ----------------------------------------------------------------------------
43#define K1212_DEBUG_LEVEL 0
Takashi Iwai9fd91562005-11-17 10:45:48 +010044#if K1212_DEBUG_LEVEL > 0
45#define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
46#else
47#define K1212_DEBUG_PRINTK(fmt,...)
48#endif
49#if K1212_DEBUG_LEVEL > 1
50#define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
51#else
52#define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
53#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55// ----------------------------------------------------------------------------
56// Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
57// buffers are alocated as a large piece inside KorgSharedBuffer.
58// ----------------------------------------------------------------------------
59//#define K1212_LARGEALLOC 1
60
61// ----------------------------------------------------------------------------
62// Valid states of the Korg 1212 I/O card.
63// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010064enum CardState {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 K1212_STATE_NONEXISTENT, // there is no card here
66 K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
67 K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
68 K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
69 K1212_STATE_READY, // the card can be opened by an application. Any application
70 // requests prior to this state should fail. Only an open
71 // request can be made at this state.
72 K1212_STATE_OPEN, // an application has opened the card
73 K1212_STATE_SETUP, // the card has been setup for play
74 K1212_STATE_PLAYING, // the card is playing
75 K1212_STATE_MONITOR, // the card is in the monitor mode
76 K1212_STATE_CALIBRATING, // the card is currently calibrating
77 K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
78 // are in the process of cleaning things up.
79 K1212_STATE_MAX_STATE // state values of this and beyond are invalid
Takashi Iwaifcfd3332005-11-17 15:00:46 +010080};
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82// ----------------------------------------------------------------------------
83// The following enumeration defines the constants written to the card's
84// host-to-card doorbell to initiate a command.
85// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +010086enum korg1212_dbcnst {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
88 K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
89 K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
90 K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
91 K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
92 K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
93 K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
94 K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
95 // timecode value.
96 K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
97 K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
98 K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
99 K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
100 // on page 4 (local page to card).
101 K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
102 // completed.
103 K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106
107// ----------------------------------------------------------------------------
108// The following enumeration defines return codes
109// to the Korg 1212 I/O driver.
110// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100111enum snd_korg1212rc {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 K1212_CMDRET_Success = 0, // command was successfully placed
113 K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
114 K1212_CMDRET_PMFailure, // the protected mode call failed
115 K1212_CMDRET_FailUnspecified, // unspecified failure
116 K1212_CMDRET_FailBadState, // the specified command can not be given in
117 // the card's current state. (or the wave device's
118 // state)
119 K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
120 K1212_CMDRET_BadIndex, // an out of range card index was specified
121 K1212_CMDRET_BadHandle, // an invalid card handle was specified
122 K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
123 K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
124 K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
125 K1212_CMDRET_BadParams, // bad parameters were provided by the caller
126
127 K1212_CMDRET_BadDevice, // the specified wave device was out of range
128 K1212_CMDRET_BadFormat // the specified wave format is unsupported
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131// ----------------------------------------------------------------------------
132// The following enumeration defines the constants used to select the play
133// mode for the card in the SelectPlayMode command.
134// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100135enum PlayModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
137 K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
138 K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
139 K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100140};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142// ----------------------------------------------------------------------------
143// The following enumeration defines the constants used to select the monitor
144// mode for the card in the SetMonitorMode command.
145// ----------------------------------------------------------------------------
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100146enum MonitorModeSelector {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
148 K1212_MONMODE_On // tells card to turn on monitor mode
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100149};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
152#define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
153#define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
154#define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
155#define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
156#define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
157#define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
158#define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
159 // register
160#define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
161 // this is the upper word of the PCI control reg.
162#define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
165 // to send a command before giving up.
166#define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
167 // the card.
168#define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
169
170#define CARD_BOOT_DELAY_IN_MS 10
171#define CARD_BOOT_TIMEOUT 10
172#define DSP_BOOT_DELAY_IN_MS 200
173
174#define kNumBuffers 8
175#define k1212MaxCards 4
176#define k1212NumWaveDevices 6
177#define k16BitChannels 10
178#define k32BitChannels 2
179#define kAudioChannels (k16BitChannels + k32BitChannels)
180#define kPlayBufferFrames 1024
181
182#define K1212_ANALOG_CHANNELS 2
183#define K1212_SPDIF_CHANNELS 2
184#define K1212_ADAT_CHANNELS 8
185#define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
186#define K1212_MIN_CHANNELS 1
187#define K1212_MAX_CHANNELS K1212_CHANNELS
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100188#define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
190#define K1212_PERIODS (kNumBuffers)
191#define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
192#define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
193#define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
194#define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
195#define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
196#define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
197
Takashi Iwaifbaf6a52012-08-30 07:57:38 -0700198#define k1212MinADCSens 0x00
199#define k1212MaxADCSens 0x7f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define k1212MaxVolume 0x7fff
201#define k1212MaxWaveVolume 0xffff
202#define k1212MinVolume 0x0000
203#define k1212MaxVolInverted 0x8000
204
205// -----------------------------------------------------------------
206// the following bits are used for controlling interrupts in the
207// interrupt control/status reg
208// -----------------------------------------------------------------
209#define PCI_INT_ENABLE_BIT 0x00000100
210#define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
211#define LOCAL_INT_ENABLE_BIT 0x00010000
212#define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
213#define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
214
215// -----------------------------------------------------------------
216// the following bits are defined for the PCI command register
217// -----------------------------------------------------------------
218#define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
219#define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
220#define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
221
222// -----------------------------------------------------------------
223// the following bits are defined for the PCI status register
224// -----------------------------------------------------------------
225#define PCI_STAT_PARITY_ERROR_BIT 0x8000
226#define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
227#define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
228#define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
229#define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
230
231// ------------------------------------------------------------------------
232// the following constants are used in setting the 1212 I/O card's input
233// sensitivity.
234// ------------------------------------------------------------------------
235#define SET_SENS_LOCALINIT_BITPOS 15
236#define SET_SENS_DATA_BITPOS 10
237#define SET_SENS_CLOCK_BITPOS 8
238#define SET_SENS_LOADSHIFT_BITPOS 0
239
240#define SET_SENS_LEFTCHANID 0x00
241#define SET_SENS_RIGHTCHANID 0x01
242
243#define K1212SENSUPDATE_DELAY_IN_MS 50
244
245// --------------------------------------------------------------------------
246// WaitRTCTicks
247//
248// This function waits the specified number of real time clock ticks.
249// According to the DDK, each tick is ~0.8 microseconds.
250// The defines following the function declaration can be used for the
251// numTicksToWait parameter.
252// --------------------------------------------------------------------------
253#define ONE_RTC_TICK 1
254#define SENSCLKPULSE_WIDTH 4
255#define LOADSHIFT_DELAY 4
256#define INTERCOMMAND_DELAY 40
257#define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
258 // the command register. (could be up to 180 us)
259#define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
260 // from the card after sending a command.
261
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100262enum ClockSourceIndex {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
264 K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
265 K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
266 K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
267 K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
268 K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
269 K1212_CLKIDX_Invalid // used to check validity of the index
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100270};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100272enum ClockSourceType {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 K1212_CLKIDX_Adat = 0, // selects source as ADAT
274 K1212_CLKIDX_Word, // selects source as S/PDIF
275 K1212_CLKIDX_Local // selects source as local clock
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100278struct KorgAudioFrame {
279 u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
280 u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
281 u32 timeCodeVal; /* holds the ADAT timecode value */
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100284struct KorgAudioBuffer {
285 struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
286};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100288struct KorgSharedBuffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#ifdef K1212_LARGEALLOC
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100290 struct KorgAudioBuffer playDataBufs[kNumBuffers];
291 struct KorgAudioBuffer recordDataBufs[kNumBuffers];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#endif
293 short volumeData[kAudioChannels];
294 u32 cardCommand;
295 u16 routeData [kAudioChannels];
296 u32 AdatTimeCode; // ADAT timecode value
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100297};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100299struct SensBits {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 union {
301 struct {
302 unsigned int leftChanVal:8;
303 unsigned int leftChanId:8;
304 } v;
305 u16 leftSensBits;
306 } l;
307 union {
308 struct {
309 unsigned int rightChanVal:8;
310 unsigned int rightChanId:8;
311 } v;
312 u16 rightSensBits;
313 } r;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100314};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100316struct snd_korg1212 {
317 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct pci_dev *pci;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100319 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int irq;
321
322 spinlock_t lock;
Ingo Molnar62932df2006-01-16 16:34:20 +0100323 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 struct timer_list timer; /* timer callback for checking ack of stop request */
326 int stop_pending_cnt; /* counter for stop pending check */
327
328 wait_queue_head_t wait;
329
330 unsigned long iomem;
331 unsigned long ioport;
332 unsigned long iomem2;
333 unsigned long irqcount;
334 unsigned long inIRQ;
335 void __iomem *iobase;
336
337 struct snd_dma_buffer dma_dsp;
338 struct snd_dma_buffer dma_play;
339 struct snd_dma_buffer dma_rec;
340 struct snd_dma_buffer dma_shared;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 u32 DataBufsSize;
343
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100344 struct KorgAudioBuffer * playDataBufsPtr;
345 struct KorgAudioBuffer * recordDataBufsPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100347 struct KorgSharedBuffer * sharedBufferPtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 u32 RecDataPhy;
350 u32 PlayDataPhy;
351 unsigned long sharedBufferPhy;
352 u32 VolumeTablePhy;
353 u32 RoutingTablePhy;
354 u32 AdatTimeCodePhy;
355
356 u32 __iomem * statusRegPtr; // address of the interrupt status/control register
357 u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
358 u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
359 u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
360 u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
361 u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
362 u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
363 u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
364 u16 __iomem * sensRegPtr; // address of the sensitivity setting register
365 u32 __iomem * idRegPtr; // address of the device and vendor ID registers
366
367 size_t periodsize;
368 int channels;
369 int currentBuffer;
370
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100371 struct snd_pcm_substream *playback_substream;
372 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 pid_t capture_pid;
375 pid_t playback_pid;
376
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100377 enum CardState cardState;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int running;
379 int idleMonitorOn; // indicates whether the card is in idle monitor mode.
380 u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
381
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100382 enum ClockSourceIndex clkSrcRate; // sample rate and clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100384 enum ClockSourceType clkSource; // clock source
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int clkRate; // clock rate
386
387 int volumePhase[kAudioChannels];
388
389 u16 leftADCInSens; // ADC left channel input sensitivity
390 u16 rightADCInSens; // ADC right channel input sensitivity
391
392 int opencnt; // Open/Close count
393 int setcnt; // SetupForPlay count
394 int playcnt; // TriggerPlay count
395 int errorcnt; // Error Count
396 unsigned long totalerrorcnt; // Total Error Count
397
398 int dsp_is_loaded;
399 int dsp_stop_is_processed;
400
401};
402
403MODULE_DESCRIPTION("korg1212");
404MODULE_LICENSE("GPL");
405MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
Clemens Ladisch7e0af292007-05-03 17:59:54 +0200406MODULE_FIRMWARE("korg/k1212.dsp");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
409static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +1030410static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412module_param_array(index, int, NULL, 0444);
413MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
414module_param_array(id, charp, NULL, 0444);
415MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
416module_param_array(enable, bool, NULL, 0444);
417MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
418MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
419
Benoit Taine9baa3c32014-08-08 15:56:03 +0200420static const struct pci_device_id snd_korg1212_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 {
422 .vendor = 0x10b5,
423 .device = 0x906d,
424 .subvendor = PCI_ANY_ID,
425 .subdevice = PCI_ANY_ID,
426 },
427 { 0, },
428};
429
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100430MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
431
Takashi Iwai9fd91562005-11-17 10:45:48 +0100432static char *stateName[] = {
433 "Non-existent",
434 "Uninitialized",
435 "DSP download in process",
436 "DSP download complete",
437 "Ready",
438 "Open",
439 "Setup for play",
440 "Playing",
441 "Monitor mode on",
442 "Calibrating",
443 "Invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444};
445
Takashi Iwaif8612372014-10-20 18:21:58 +0200446static const char * const clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Takashi Iwaif8612372014-10-20 18:21:58 +0200448static const char * const clockSourceName[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100449 "ADAT at 44.1 kHz",
450 "ADAT at 48 kHz",
451 "S/PDIF at 44.1 kHz",
452 "S/PDIF at 48 kHz",
453 "local clock at 44.1 kHz",
454 "local clock at 48 kHz"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455};
456
Takashi Iwaif8612372014-10-20 18:21:58 +0200457static const char * const channelName[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100458 "ADAT-1",
459 "ADAT-2",
460 "ADAT-3",
461 "ADAT-4",
462 "ADAT-5",
463 "ADAT-6",
464 "ADAT-7",
465 "ADAT-8",
466 "Analog-L",
467 "Analog-R",
468 "SPDIF-L",
469 "SPDIF-R",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470};
471
Takashi Iwai9fd91562005-11-17 10:45:48 +0100472static u16 ClockSourceSelector[] = {
473 0x8000, // selects source as ADAT at 44.1 kHz
474 0x0000, // selects source as ADAT at 48 kHz
475 0x8001, // selects source as S/PDIF at 44.1 kHz
476 0x0001, // selects source as S/PDIF at 48 kHz
477 0x8002, // selects source as local clock at 44.1 kHz
478 0x0002 // selects source as local clock at 48 kHz
479};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100481union swap_u32 { unsigned char c[4]; u32 i; };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483#ifdef SNDRV_BIG_ENDIAN
484static u32 LowerWordSwap(u32 swappee)
485#else
486static u32 UpperWordSwap(u32 swappee)
487#endif
488{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100489 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 swapper.i = swappee;
492 retVal.c[2] = swapper.c[3];
493 retVal.c[3] = swapper.c[2];
494 retVal.c[1] = swapper.c[1];
495 retVal.c[0] = swapper.c[0];
496
497 return retVal.i;
498}
499
500#ifdef SNDRV_BIG_ENDIAN
501static u32 UpperWordSwap(u32 swappee)
502#else
503static u32 LowerWordSwap(u32 swappee)
504#endif
505{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100506 union swap_u32 retVal, swapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 swapper.i = swappee;
509 retVal.c[2] = swapper.c[2];
510 retVal.c[3] = swapper.c[3];
511 retVal.c[1] = swapper.c[0];
512 retVal.c[0] = swapper.c[1];
513
514 return retVal.i;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
518#define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
519#define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
520#define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
521
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100522static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
523 enum korg1212_dbcnst doorbellVal,
524 u32 mailBox0Val, u32 mailBox1Val,
525 u32 mailBox2Val, u32 mailBox3Val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 u32 retryCount;
528 u16 mailBox3Lo;
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100529 int rc = K1212_CMDRET_Success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (!korg1212->outDoorbellPtr) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100532 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return K1212_CMDRET_CardUninitialized;
534 }
535
Takashi Iwai9fd91562005-11-17 10:45:48 +0100536 K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
537 doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
539 writel(mailBox3Val, korg1212->mailbox3Ptr);
540 writel(mailBox2Val, korg1212->mailbox2Ptr);
541 writel(mailBox1Val, korg1212->mailbox1Ptr);
542 writel(mailBox0Val, korg1212->mailbox0Ptr);
543 writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
544
545 // --------------------------------------------------------------
546 // the reboot command will not give an acknowledgement.
547 // --------------------------------------------------------------
548 if ( doorbellVal == K1212_DB_RebootCard ||
549 doorbellVal == K1212_DB_BootFromDSPPage4 ||
550 doorbellVal == K1212_DB_StartDSPDownload ) {
551 rc = K1212_CMDRET_Success;
552 break;
553 }
554
555 // --------------------------------------------------------------
556 // See if the card acknowledged the command. Wait a bit, then
557 // read in the low word of mailbox3. If the MSB is set and the
558 // low byte is equal to the doorbell value, then it ack'd.
559 // --------------------------------------------------------------
560 udelay(COMMAND_ACK_DELAY);
561 mailBox3Lo = readl(korg1212->mailbox3Ptr);
562 if (mailBox3Lo & COMMAND_ACK_MASK) {
563 if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100564 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = K1212_CMDRET_Success;
566 break;
567 }
568 }
569 }
570 korg1212->cmdRetryCount += retryCount;
571
572 if (retryCount >= MAX_COMMAND_RETRIES) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100573 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rc = K1212_CMDRET_NoAckFromCard;
575 }
576
577 return rc;
578}
579
580/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100581static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 if (! korg1212->stop_pending_cnt) {
584 korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
585 /* program the timer */
586 korg1212->stop_pending_cnt = HZ;
Takashi Iwai3d6f0e02015-01-19 11:34:18 +0100587 mod_timer(&korg1212->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589}
590
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100591static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 unsigned long flags;
594 spin_lock_irqsave(&korg1212->lock, flags);
595 korg1212->dsp_stop_is_processed = 0;
596 snd_korg1212_SendStop(korg1212);
597 spin_unlock_irqrestore(&korg1212->lock, flags);
598 wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
599}
600
601/* timer callback for checking the ack of stop request */
Kees Cook7211ec62017-10-25 08:09:27 -0700602static void snd_korg1212_timer_func(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Kees Cook7211ec62017-10-25 08:09:27 -0700604 struct snd_korg1212 *korg1212 = from_timer(korg1212, t, timer);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100605 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Takashi Iwaib32425a2005-11-18 18:52:14 +0100607 spin_lock_irqsave(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (korg1212->sharedBufferPtr->cardCommand == 0) {
609 /* ack'ed */
610 korg1212->stop_pending_cnt = 0;
611 korg1212->dsp_stop_is_processed = 1;
612 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100613 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
614 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } else {
616 if (--korg1212->stop_pending_cnt > 0) {
617 /* reprogram timer */
Takashi Iwai3d6f0e02015-01-19 11:34:18 +0100618 mod_timer(&korg1212->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } else {
620 snd_printd("korg1212_timer_func timeout\n");
621 korg1212->sharedBufferPtr->cardCommand = 0;
622 korg1212->dsp_stop_is_processed = 1;
623 wake_up(&korg1212->wait);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100624 K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
625 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 }
Takashi Iwaib32425a2005-11-18 18:52:14 +0100628 spin_unlock_irqrestore(&korg1212->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100631static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 unsigned long flags;
Takashi Iwai9fd91562005-11-17 10:45:48 +0100634 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 udelay(INTERCOMMAND_DELAY);
637 spin_lock_irqsave(&korg1212->lock, flags);
638 korg1212->idleMonitorOn = 1;
639 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
640 K1212_MODE_MonitorOn, 0, 0, 0);
641 spin_unlock_irqrestore(&korg1212->lock, flags);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100642 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100645static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 if (korg1212->idleMonitorOn) {
648 snd_korg1212_SendStopAndWait(korg1212);
649 korg1212->idleMonitorOn = 0;
650 }
651}
652
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100653static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 korg1212->cardState = csState;
656}
657
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100658static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100660 K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
661 stateName[korg1212->cardState], korg1212->opencnt);
Ingo Molnar62932df2006-01-16 16:34:20 +0100662 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (korg1212->opencnt++ == 0) {
664 snd_korg1212_TurnOffIdleMonitor(korg1212);
665 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
666 }
667
Ingo Molnar62932df2006-01-16 16:34:20 +0100668 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 1;
670}
671
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100672static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100674 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
675 stateName[korg1212->cardState], korg1212->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Ingo Molnar62932df2006-01-16 16:34:20 +0100677 mutex_lock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (--(korg1212->opencnt)) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100679 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681 }
682
683 if (korg1212->cardState == K1212_STATE_SETUP) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100684 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 K1212_MODE_StopPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100686 if (rc)
687 K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
688 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (rc != K1212_CMDRET_Success) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100690 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692 }
693 } else if (korg1212->cardState > K1212_STATE_SETUP) {
694 snd_korg1212_SendStopAndWait(korg1212);
695 }
696
697 if (korg1212->cardState > K1212_STATE_READY) {
698 snd_korg1212_TurnOnIdleMonitor(korg1212);
699 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
700 }
701
Ingo Molnar62932df2006-01-16 16:34:20 +0100702 mutex_unlock(&korg1212->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}
705
706/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100707static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100709 int rc;
710
711 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
712 stateName[korg1212->cardState], korg1212->setcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (korg1212->setcnt++)
715 return 0;
716
717 snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
718 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
719 K1212_MODE_SetupPlay, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100720 if (rc)
721 K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
722 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (rc != K1212_CMDRET_Success) {
724 return 1;
725 }
726 return 0;
727}
728
729/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100730static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100732 int rc;
733
734 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
735 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (korg1212->playcnt++)
738 return 0;
739
740 snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
741 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100742 if (rc)
743 K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
744 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (rc != K1212_CMDRET_Success) {
746 return 1;
747 }
748 return 0;
749}
750
751/* spinlock already held */
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100752static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100754 K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
755 stateName[korg1212->cardState], korg1212->playcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (--(korg1212->playcnt))
758 return 0;
759
760 korg1212->setcnt = 0;
761
762 if (korg1212->cardState != K1212_STATE_ERRORSTOP)
763 snd_korg1212_SendStop(korg1212);
764
765 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
766 return 0;
767}
768
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100769static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 writel(PCI_INT_ENABLE_BIT |
772 PCI_DOORBELL_INT_ENABLE_BIT |
773 LOCAL_INT_ENABLE_BIT |
774 LOCAL_DOORBELL_INT_ENABLE_BIT |
775 LOCAL_DMA1_INT_ENABLE_BIT,
776 korg1212->statusRegPtr);
777}
778
779#if 0 /* not used */
780
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100781static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
782 enum MonitorModeSelector mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100784 K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
785 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 switch (mode) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100788 case K1212_MONMODE_Off:
789 if (korg1212->cardState != K1212_STATE_MONITOR)
790 return 0;
791 else {
792 snd_korg1212_SendStopAndWait(korg1212);
793 snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
794 }
795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Takashi Iwai9fd91562005-11-17 10:45:48 +0100797 case K1212_MONMODE_On:
798 if (korg1212->cardState != K1212_STATE_OPEN)
799 return 0;
800 else {
801 int rc;
802 snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
803 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
804 K1212_MODE_MonitorOn, 0, 0, 0);
805 if (rc != K1212_CMDRET_Success)
806 return 0;
807 }
808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Takashi Iwai9fd91562005-11-17 10:45:48 +0100810 default:
811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 return 1;
815}
816
817#endif /* not used */
818
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100819static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Takashi Iwai9fd91562005-11-17 10:45:48 +0100821 if (korg1212->playback_pid != korg1212->capture_pid &&
822 korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Takashi Iwai9fd91562005-11-17 10:45:48 +0100825 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100828static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100830 static enum ClockSourceIndex s44[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100831 K1212_CLKIDX_AdatAt44_1K,
832 K1212_CLKIDX_WordAt44_1K,
833 K1212_CLKIDX_LocalAt44_1K
834 };
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100835 static enum ClockSourceIndex s48[] = {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100836 K1212_CLKIDX_AdatAt48K,
837 K1212_CLKIDX_WordAt48K,
838 K1212_CLKIDX_LocalAt48K
839 };
840 int parm, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Takashi Iwai9fd91562005-11-17 10:45:48 +0100842 if (!snd_korg1212_use_is_exclusive (korg1212))
843 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100845 switch (rate) {
Takashi Iwai9fd91562005-11-17 10:45:48 +0100846 case 44100:
847 parm = s44[korg1212->clkSource];
848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Takashi Iwai9fd91562005-11-17 10:45:48 +0100850 case 48000:
851 parm = s48[korg1212->clkSource];
852 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Takashi Iwai9fd91562005-11-17 10:45:48 +0100854 default:
855 return -EINVAL;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 korg1212->clkSrcRate = parm;
859 korg1212->clkRate = rate;
860
861 udelay(INTERCOMMAND_DELAY);
862 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
863 ClockSourceSelector[korg1212->clkSrcRate],
864 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +0100865 if (rc)
866 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
867 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 return 0;
870}
871
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100872static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874
Takashi Iwai9fd91562005-11-17 10:45:48 +0100875 if (source < 0 || source > 2)
876 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 korg1212->clkSource = source;
879
880 snd_korg1212_SetRate(korg1212, korg1212->clkRate);
881
882 return 0;
883}
884
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100885static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 writel(0, korg1212->statusRegPtr);
888}
889
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100890static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100892 struct SensBits sensVals;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int bitPosition;
894 int channel;
895 int clkIs48K;
896 int monModeSet;
897 u16 controlValue; // this keeps the current value to be written to
898 // the card's eeprom control register.
899 u16 count;
900 unsigned long flags;
901
Takashi Iwai9fd91562005-11-17 10:45:48 +0100902 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
903 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 // ----------------------------------------------------------------------------
906 // initialize things. The local init bit is always set when writing to the
907 // card's control register.
908 // ----------------------------------------------------------------------------
909 controlValue = 0;
910 SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
911
912 // ----------------------------------------------------------------------------
913 // make sure the card is not in monitor mode when we do this update.
914 // ----------------------------------------------------------------------------
915 if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
916 monModeSet = 1;
917 snd_korg1212_SendStopAndWait(korg1212);
918 } else
919 monModeSet = 0;
920
921 spin_lock_irqsave(&korg1212->lock, flags);
922
923 // ----------------------------------------------------------------------------
924 // we are about to send new values to the card, so clear the new values queued
925 // flag. Also, clear out mailbox 3, so we don't lockup.
926 // ----------------------------------------------------------------------------
927 writel(0, korg1212->mailbox3Ptr);
928 udelay(LOADSHIFT_DELAY);
929
930 // ----------------------------------------------------------------------------
931 // determine whether we are running a 48K or 44.1K clock. This info is used
932 // later when setting the SPDIF FF after the volume has been shifted in.
933 // ----------------------------------------------------------------------------
934 switch (korg1212->clkSrcRate) {
935 case K1212_CLKIDX_AdatAt44_1K:
936 case K1212_CLKIDX_WordAt44_1K:
937 case K1212_CLKIDX_LocalAt44_1K:
938 clkIs48K = 0;
939 break;
940
941 case K1212_CLKIDX_WordAt48K:
942 case K1212_CLKIDX_AdatAt48K:
943 case K1212_CLKIDX_LocalAt48K:
944 default:
945 clkIs48K = 1;
946 break;
947 }
948
949 // ----------------------------------------------------------------------------
950 // start the update. Setup the bit structure and then shift the bits.
951 // ----------------------------------------------------------------------------
952 sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
953 sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
954 sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
955 sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
956
957 // ----------------------------------------------------------------------------
958 // now start shifting the bits in. Start with the left channel then the right.
959 // ----------------------------------------------------------------------------
960 for (channel = 0; channel < 2; channel++) {
961
962 // ----------------------------------------------------------------------------
963 // Bring the load/shift line low, then wait - the spec says >150ns from load/
964 // shift low to the first rising edge of the clock.
965 // ----------------------------------------------------------------------------
966 ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
967 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
968 writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
969 udelay(LOADSHIFT_DELAY);
970
971 for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
Takashi Iwai9fd91562005-11-17 10:45:48 +0100972 if (channel == 0) {
973 if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
Takashi Iwaifcfd3332005-11-17 15:00:46 +0100974 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
Takashi Iwai9fd91562005-11-17 10:45:48 +0100975 else
976 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
977 } else {
978 if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
979 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
980 else
981 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
985 writew(controlValue, korg1212->sensRegPtr); // clock goes low
986 udelay(SENSCLKPULSE_WIDTH);
987 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
988 writew(controlValue, korg1212->sensRegPtr); // clock goes high
989 udelay(SENSCLKPULSE_WIDTH);
990 }
991
992 // ----------------------------------------------------------------------------
993 // finish up SPDIF for left. Bring the load/shift line high, then write a one
994 // bit if the clock rate is 48K otherwise write 0.
995 // ----------------------------------------------------------------------------
996 ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
997 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
998 SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
999 writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
1000 udelay(SENSCLKPULSE_WIDTH);
1001
1002 if (clkIs48K)
1003 SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
1004
1005 writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
1006 udelay(ONE_RTC_TICK);
1007 SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1008 writew(controlValue, korg1212->sensRegPtr); // clock goes high
1009 udelay(SENSCLKPULSE_WIDTH);
1010 ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
1011 writew(controlValue, korg1212->sensRegPtr); // clock goes low
1012 udelay(SENSCLKPULSE_WIDTH);
1013 }
1014
1015 // ----------------------------------------------------------------------------
1016 // The update is complete. Set a timeout. This is the inter-update delay.
1017 // Also, if the card was in monitor mode, restore it.
1018 // ----------------------------------------------------------------------------
1019 for (count = 0; count < 10; count++)
1020 udelay(SENSCLKPULSE_WIDTH);
1021
1022 if (monModeSet) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001023 int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 K1212_MODE_MonitorOn, 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001025 if (rc)
1026 K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
1027 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 spin_unlock_irqrestore(&korg1212->lock, flags);
1031
1032 return 1;
1033}
1034
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001035static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001037 int channel, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Takashi Iwai9fd91562005-11-17 10:45:48 +01001039 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
1040 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 // ----------------------------------------------------
1043 // tell the card to boot
1044 // ----------------------------------------------------
1045 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
1046
Takashi Iwai9fd91562005-11-17 10:45:48 +01001047 if (rc)
1048 K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
1049 rc, stateName[korg1212->cardState]);
1050 msleep(DSP_BOOT_DELAY_IN_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 // --------------------------------------------------------------------------------
1053 // Let the card know where all the buffers are.
1054 // --------------------------------------------------------------------------------
1055 rc = snd_korg1212_Send1212Command(korg1212,
1056 K1212_DB_ConfigureBufferMemory,
1057 LowerWordSwap(korg1212->PlayDataPhy),
1058 LowerWordSwap(korg1212->RecDataPhy),
1059 ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
1060 // is based on 2 buffers
1061 0
1062 );
1063
Takashi Iwai9fd91562005-11-17 10:45:48 +01001064 if (rc)
1065 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
1066 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 udelay(INTERCOMMAND_DELAY);
1069
1070 rc = snd_korg1212_Send1212Command(korg1212,
1071 K1212_DB_ConfigureMiscMemory,
1072 LowerWordSwap(korg1212->VolumeTablePhy),
1073 LowerWordSwap(korg1212->RoutingTablePhy),
1074 LowerWordSwap(korg1212->AdatTimeCodePhy),
1075 0
1076 );
1077
Takashi Iwai9fd91562005-11-17 10:45:48 +01001078 if (rc)
1079 K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
1080 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 // --------------------------------------------------------------------------------
1083 // Initialize the routing and volume tables, then update the card's state.
1084 // --------------------------------------------------------------------------------
1085 udelay(INTERCOMMAND_DELAY);
1086
1087 for (channel = 0; channel < kAudioChannels; channel++) {
1088 korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
1089 //korg1212->sharedBufferPtr->routeData[channel] = channel;
1090 korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
1091 }
1092
1093 snd_korg1212_WriteADCSensitivity(korg1212);
1094
1095 udelay(INTERCOMMAND_DELAY);
1096 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
1097 ClockSourceSelector[korg1212->clkSrcRate],
1098 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001099 if (rc)
1100 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
1101 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Takashi Iwai9fd91562005-11-17 10:45:48 +01001103 rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
1105
Takashi Iwai9fd91562005-11-17 10:45:48 +01001106 if (rc)
1107 K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
1108 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
1111}
1112
David Howells7d12e782006-10-05 14:55:46 +01001113static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 u32 doorbellValue;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001116 struct snd_korg1212 *korg1212 = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 doorbellValue = readl(korg1212->inDoorbellPtr);
1119
1120 if (!doorbellValue)
1121 return IRQ_NONE;
1122
1123 spin_lock(&korg1212->lock);
1124
1125 writel(doorbellValue, korg1212->inDoorbellPtr);
1126
1127 korg1212->irqcount++;
1128
1129 korg1212->inIRQ++;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 switch (doorbellValue) {
1132 case K1212_DB_DSPDownloadDone:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001133 K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
1134 korg1212->irqcount, doorbellValue,
1135 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
1137 korg1212->dsp_is_loaded = 1;
1138 wake_up(&korg1212->wait);
1139 }
1140 break;
1141
1142 // ------------------------------------------------------------------------
1143 // an error occurred - stop the card
1144 // ------------------------------------------------------------------------
1145 case K1212_DB_DMAERROR:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001146 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
1147 korg1212->irqcount, doorbellValue,
1148 stateName[korg1212->cardState]);
1149 snd_printk(KERN_ERR "korg1212: DMA Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 korg1212->errorcnt++;
1151 korg1212->totalerrorcnt++;
1152 korg1212->sharedBufferPtr->cardCommand = 0;
1153 snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
1154 break;
1155
1156 // ------------------------------------------------------------------------
1157 // the card has stopped by our request. Clear the command word and signal
1158 // the semaphore in case someone is waiting for this.
1159 // ------------------------------------------------------------------------
1160 case K1212_DB_CARDSTOPPED:
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001161 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
Takashi Iwai9fd91562005-11-17 10:45:48 +01001162 korg1212->irqcount, doorbellValue,
1163 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 korg1212->sharedBufferPtr->cardCommand = 0;
1165 break;
1166
1167 default:
Takashi Iwai9fd91562005-11-17 10:45:48 +01001168 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001169 korg1212->irqcount, doorbellValue,
1170 korg1212->currentBuffer, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
1172 korg1212->currentBuffer++;
1173
1174 if (korg1212->currentBuffer >= kNumBuffers)
1175 korg1212->currentBuffer = 0;
1176
1177 if (!korg1212->running)
1178 break;
1179
1180 if (korg1212->capture_substream) {
1181 spin_unlock(&korg1212->lock);
1182 snd_pcm_period_elapsed(korg1212->capture_substream);
1183 spin_lock(&korg1212->lock);
1184 }
1185
1186 if (korg1212->playback_substream) {
1187 spin_unlock(&korg1212->lock);
1188 snd_pcm_period_elapsed(korg1212->playback_substream);
1189 spin_lock(&korg1212->lock);
1190 }
1191 }
1192 break;
1193 }
1194
1195 korg1212->inIRQ--;
1196
1197 spin_unlock(&korg1212->lock);
1198
1199 return IRQ_HANDLED;
1200}
1201
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001202static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001204 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Takashi Iwai9fd91562005-11-17 10:45:48 +01001206 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
1207 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 // ---------------------------------------------------------------
1210 // verify the state of the card before proceeding.
1211 // ---------------------------------------------------------------
Takashi Iwai9fd91562005-11-17 10:45:48 +01001212 if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
1218 UpperWordSwap(korg1212->dma_dsp.addr),
1219 0, 0, 0);
Takashi Iwai9fd91562005-11-17 10:45:48 +01001220 if (rc)
1221 K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
1222 rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 korg1212->dsp_is_loaded = 0;
1225 wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
1226 if (! korg1212->dsp_is_loaded )
1227 return -EBUSY; /* timeout */
1228
1229 snd_korg1212_OnDSPDownloadComplete(korg1212);
1230
1231 return 0;
1232}
1233
Bhumika Goyal5875adc2017-08-12 21:01:16 +05301234static const struct snd_pcm_hardware snd_korg1212_playback_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 .info = (SNDRV_PCM_INFO_MMAP |
1237 SNDRV_PCM_INFO_MMAP_VALID |
Takashi Iwai2008f132009-04-28 12:25:59 +02001238 SNDRV_PCM_INFO_INTERLEAVED |
1239 SNDRV_PCM_INFO_BATCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1241 .rates = (SNDRV_PCM_RATE_44100 |
1242 SNDRV_PCM_RATE_48000),
1243 .rate_min = 44100,
1244 .rate_max = 48000,
1245 .channels_min = K1212_MIN_CHANNELS,
1246 .channels_max = K1212_MAX_CHANNELS,
1247 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1248 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1249 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1250 .periods_min = K1212_PERIODS,
1251 .periods_max = K1212_PERIODS,
1252 .fifo_size = 0,
1253};
1254
Bhumika Goyal5875adc2017-08-12 21:01:16 +05301255static const struct snd_pcm_hardware snd_korg1212_capture_info =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 .info = (SNDRV_PCM_INFO_MMAP |
1258 SNDRV_PCM_INFO_MMAP_VALID |
Takashi Iwai2008f132009-04-28 12:25:59 +02001259 SNDRV_PCM_INFO_INTERLEAVED |
1260 SNDRV_PCM_INFO_BATCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1262 .rates = (SNDRV_PCM_RATE_44100 |
1263 SNDRV_PCM_RATE_48000),
1264 .rate_min = 44100,
1265 .rate_max = 48000,
1266 .channels_min = K1212_MIN_CHANNELS,
1267 .channels_max = K1212_MAX_CHANNELS,
1268 .buffer_bytes_max = K1212_MAX_BUF_SIZE,
1269 .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
1270 .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
1271 .periods_min = K1212_PERIODS,
1272 .periods_max = K1212_PERIODS,
1273 .fifo_size = 0,
1274};
1275
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001276static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001278 struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int i;
1280
Takashi Iwai9fd91562005-11-17 10:45:48 +01001281 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
1282 pos, offset, size, count);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001283 if (snd_BUG_ON(pos + count > K1212_MAX_SAMPLES))
1284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 for (i=0; i < count; i++) {
1287#if K1212_DEBUG_LEVEL > 0
1288 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1289 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001290 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
1291 dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return -EFAULT;
1293 }
1294#endif
1295 memset((void*) dst + offset, 0, size);
1296 dst++;
1297 }
1298
1299 return 0;
1300}
1301
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001302static int snd_korg1212_copy_to(struct snd_pcm_substream *substream,
1303 void __user *dst, int pos, int count,
1304 bool in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001306 struct snd_pcm_runtime *runtime = substream->runtime;
1307 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1308 struct KorgAudioFrame *src;
1309 int i, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001311 pos = bytes_to_frames(runtime, pos);
1312 count = bytes_to_frames(runtime, count);
1313 size = korg1212->channels * 2;
1314 src = korg1212->recordDataBufsPtr[0].bufferData + pos;
1315 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d size=%d count=%d\n",
1316 pos, size, count);
Takashi Iwaida3cec32008-08-08 17:12:14 +02001317 if (snd_BUG_ON(pos + count > K1212_MAX_SAMPLES))
1318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 for (i=0; i < count; i++) {
1321#if K1212_DEBUG_LEVEL > 0
1322 if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
1323 (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001324 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EFAULT;
1326 }
1327#endif
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001328 if (in_kernel)
Takashi Iwai07014922018-07-25 23:00:56 +02001329 memcpy((__force void *)dst, src, size);
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001330 else if (copy_to_user(dst, src, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 src++;
1333 dst += size;
1334 }
1335
1336 return 0;
1337}
1338
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001339static int snd_korg1212_copy_from(struct snd_pcm_substream *substream,
1340 void __user *src, int pos, int count,
1341 bool in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001343 struct snd_pcm_runtime *runtime = substream->runtime;
1344 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1345 struct KorgAudioFrame *dst;
1346 int i, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001348 pos = bytes_to_frames(runtime, pos);
1349 count = bytes_to_frames(runtime, count);
1350 size = korg1212->channels * 2;
1351 dst = korg1212->playDataBufsPtr[0].bufferData + pos;
1352
1353 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d size=%d count=%d\n",
1354 pos, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Takashi Iwaida3cec32008-08-08 17:12:14 +02001356 if (snd_BUG_ON(pos + count > K1212_MAX_SAMPLES))
1357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 for (i=0; i < count; i++) {
1360#if K1212_DEBUG_LEVEL > 0
1361 if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
1362 (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001363 printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EFAULT;
1365 }
1366#endif
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001367 if (in_kernel)
Takashi Iwai07014922018-07-25 23:00:56 +02001368 memcpy(dst, (__force void *)src, size);
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001369 else if (copy_from_user(dst, src, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 dst++;
1372 src += size;
1373 }
1374
1375 return 0;
1376}
1377
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001378static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001380 struct snd_korg1212 *korg1212 = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Takashi Iwai9fd91562005-11-17 10:45:48 +01001382 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
1383 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 korg1212->pcm = NULL;
1386}
1387
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001388static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001391 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1392 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Takashi Iwai9fd91562005-11-17 10:45:48 +01001394 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
1395 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 snd_korg1212_OpenCard(korg1212);
1398
1399 runtime->hw = snd_korg1212_playback_info;
1400 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
1401
1402 spin_lock_irqsave(&korg1212->lock, flags);
1403
1404 korg1212->playback_substream = substream;
1405 korg1212->playback_pid = current->pid;
1406 korg1212->periodsize = K1212_PERIODS;
1407 korg1212->channels = K1212_CHANNELS;
1408 korg1212->errorcnt = 0;
1409
1410 spin_unlock_irqrestore(&korg1212->lock, flags);
1411
Lars-Peter Clausen9c9cb682015-10-18 15:39:17 +02001412 snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1413 kPlayBufferFrames);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 0;
1416}
1417
1418
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001419static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001422 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
1423 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Takashi Iwai9fd91562005-11-17 10:45:48 +01001425 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
1426 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 snd_korg1212_OpenCard(korg1212);
1429
1430 runtime->hw = snd_korg1212_capture_info;
1431 snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
1432
1433 spin_lock_irqsave(&korg1212->lock, flags);
1434
1435 korg1212->capture_substream = substream;
1436 korg1212->capture_pid = current->pid;
1437 korg1212->periodsize = K1212_PERIODS;
1438 korg1212->channels = K1212_CHANNELS;
1439
1440 spin_unlock_irqrestore(&korg1212->lock, flags);
1441
Lars-Peter Clausen9c9cb682015-10-18 15:39:17 +02001442 snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1443 kPlayBufferFrames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return 0;
1445}
1446
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001447static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001450 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Takashi Iwai9fd91562005-11-17 10:45:48 +01001452 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
1453 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
1456
1457 spin_lock_irqsave(&korg1212->lock, flags);
1458
1459 korg1212->playback_pid = -1;
1460 korg1212->playback_substream = NULL;
1461 korg1212->periodsize = 0;
1462
1463 spin_unlock_irqrestore(&korg1212->lock, flags);
1464
1465 snd_korg1212_CloseCard(korg1212);
1466 return 0;
1467}
1468
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001469static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001472 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Takashi Iwai9fd91562005-11-17 10:45:48 +01001474 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
1475 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 spin_lock_irqsave(&korg1212->lock, flags);
1478
1479 korg1212->capture_pid = -1;
1480 korg1212->capture_substream = NULL;
1481 korg1212->periodsize = 0;
1482
1483 spin_unlock_irqrestore(&korg1212->lock, flags);
1484
1485 snd_korg1212_CloseCard(korg1212);
1486 return 0;
1487}
1488
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001489static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 unsigned int cmd, void *arg)
1491{
Takashi Iwai9fd91562005-11-17 10:45:48 +01001492 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001495 struct snd_pcm_channel_info *info = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 info->offset = 0;
1497 info->first = info->channel * 16;
1498 info->step = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501 }
1502
1503 return snd_pcm_lib_ioctl(substream, cmd, arg);
1504}
1505
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001506static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
1507 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 unsigned long flags;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001510 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int err;
1512 pid_t this_pid;
1513 pid_t other_pid;
1514
Takashi Iwai9fd91562005-11-17 10:45:48 +01001515 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
1516 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 spin_lock_irqsave(&korg1212->lock, flags);
1519
1520 if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1521 this_pid = korg1212->playback_pid;
1522 other_pid = korg1212->capture_pid;
1523 } else {
1524 this_pid = korg1212->capture_pid;
1525 other_pid = korg1212->playback_pid;
1526 }
1527
1528 if ((other_pid > 0) && (this_pid != other_pid)) {
1529
1530 /* The other stream is open, and not by the same
1531 task as this one. Make sure that the parameters
1532 that matter are the same.
1533 */
1534
1535 if ((int)params_rate(params) != korg1212->clkRate) {
1536 spin_unlock_irqrestore(&korg1212->lock, flags);
1537 _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
1538 return -EBUSY;
1539 }
1540
1541 spin_unlock_irqrestore(&korg1212->lock, flags);
1542 return 0;
1543 }
1544
1545 if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
1546 spin_unlock_irqrestore(&korg1212->lock, flags);
1547 return err;
1548 }
1549
1550 korg1212->channels = params_channels(params);
1551 korg1212->periodsize = K1212_PERIOD_BYTES;
1552
1553 spin_unlock_irqrestore(&korg1212->lock, flags);
1554
1555 return 0;
1556}
1557
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001558static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001560 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int rc;
1562
Takashi Iwai9fd91562005-11-17 10:45:48 +01001563 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
1564 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 spin_lock_irq(&korg1212->lock);
1567
1568 /* FIXME: we should wait for ack! */
1569 if (korg1212->stop_pending_cnt > 0) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001570 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
1571 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 spin_unlock_irq(&korg1212->lock);
1573 return -EAGAIN;
1574 /*
1575 korg1212->sharedBufferPtr->cardCommand = 0;
1576 del_timer(&korg1212->timer);
1577 korg1212->stop_pending_cnt = 0;
1578 */
1579 }
1580
1581 rc = snd_korg1212_SetupForPlay(korg1212);
1582
1583 korg1212->currentBuffer = 0;
1584
1585 spin_unlock_irq(&korg1212->lock);
1586
1587 return rc ? -EINVAL : 0;
1588}
1589
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001590static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int cmd)
1592{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001593 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int rc;
1595
Takashi Iwai9fd91562005-11-17 10:45:48 +01001596 K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
1597 stateName[korg1212->cardState], cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 spin_lock(&korg1212->lock);
1600 switch (cmd) {
1601 case SNDRV_PCM_TRIGGER_START:
1602/*
1603 if (korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001604 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 }
1607*/
1608 korg1212->running++;
1609 rc = snd_korg1212_TriggerPlay(korg1212);
1610 break;
1611
1612 case SNDRV_PCM_TRIGGER_STOP:
1613/*
1614 if (!korg1212->running) {
Takashi Iwai9fd91562005-11-17 10:45:48 +01001615 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
1617 }
1618*/
1619 korg1212->running--;
1620 rc = snd_korg1212_StopPlay(korg1212);
1621 break;
1622
1623 default:
1624 rc = 1;
1625 break;
1626 }
1627 spin_unlock(&korg1212->lock);
1628 return rc ? -EINVAL : 0;
1629}
1630
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001631static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001633 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 snd_pcm_uframes_t pos;
1635
1636 pos = korg1212->currentBuffer * kPlayBufferFrames;
1637
Takashi Iwai9fd91562005-11-17 10:45:48 +01001638 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
1639 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 return pos;
1642}
1643
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001644static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001646 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 snd_pcm_uframes_t pos;
1648
1649 pos = korg1212->currentBuffer * kPlayBufferFrames;
1650
Takashi Iwai9fd91562005-11-17 10:45:48 +01001651 K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
1652 stateName[korg1212->cardState], pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 return pos;
1655}
1656
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001657static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001658 int channel, unsigned long pos,
1659 void __user *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001661 return snd_korg1212_copy_from(substream, src, pos, count, false);
1662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001664static int snd_korg1212_playback_copy_kernel(struct snd_pcm_substream *substream,
1665 int channel, unsigned long pos,
1666 void *src, unsigned long count)
1667{
1668 return snd_korg1212_copy_from(substream, (void __user *)src,
1669 pos, count, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001672static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 int channel, /* not used (interleaved data) */
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001674 unsigned long pos,
1675 unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001677 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001678 struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001680 return snd_korg1212_silence(korg1212, bytes_to_frames(runtime, pos),
1681 bytes_to_frames(runtime, count),
1682 0, korg1212->channels * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001685static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001686 int channel, unsigned long pos,
1687 void __user *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001689 return snd_korg1212_copy_to(substream, dst, pos, count, false);
1690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001692static int snd_korg1212_capture_copy_kernel(struct snd_pcm_substream *substream,
1693 int channel, unsigned long pos,
1694 void *dst, unsigned long count)
1695{
1696 return snd_korg1212_copy_to(substream, (void __user *)dst,
1697 pos, count, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Julia Lawall6769e9882016-09-02 00:13:10 +02001700static const struct snd_pcm_ops snd_korg1212_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 .open = snd_korg1212_playback_open,
1702 .close = snd_korg1212_playback_close,
1703 .ioctl = snd_korg1212_ioctl,
1704 .hw_params = snd_korg1212_hw_params,
1705 .prepare = snd_korg1212_prepare,
1706 .trigger = snd_korg1212_trigger,
1707 .pointer = snd_korg1212_playback_pointer,
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001708 .copy_user = snd_korg1212_playback_copy,
1709 .copy_kernel = snd_korg1212_playback_copy_kernel,
1710 .fill_silence = snd_korg1212_playback_silence,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711};
1712
Julia Lawall6769e9882016-09-02 00:13:10 +02001713static const struct snd_pcm_ops snd_korg1212_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .open = snd_korg1212_capture_open,
1715 .close = snd_korg1212_capture_close,
1716 .ioctl = snd_korg1212_ioctl,
1717 .hw_params = snd_korg1212_hw_params,
1718 .prepare = snd_korg1212_prepare,
1719 .trigger = snd_korg1212_trigger,
1720 .pointer = snd_korg1212_capture_pointer,
Takashi Iwai6c6ba112017-05-10 20:19:17 +02001721 .copy_user = snd_korg1212_capture_copy,
1722 .copy_kernel = snd_korg1212_capture_copy_kernel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723};
1724
1725/*
1726 * Control Interface
1727 */
1728
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001729static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1733 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1734 return 0;
1735}
1736
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001737static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001740 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int i = kcontrol->private_value;
1742
1743 spin_lock_irq(&korg1212->lock);
1744
1745 u->value.integer.value[0] = korg1212->volumePhase[i];
1746
1747 if (i >= 8)
1748 u->value.integer.value[1] = korg1212->volumePhase[i+1];
1749
1750 spin_unlock_irq(&korg1212->lock);
1751
1752 return 0;
1753}
1754
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001755static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001758 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 int change = 0;
1760 int i, val;
1761
1762 spin_lock_irq(&korg1212->lock);
1763
1764 i = kcontrol->private_value;
1765
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001766 korg1212->volumePhase[i] = !!u->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
1769
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001770 if ((u->value.integer.value[0] != 0) != (val < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
1772 korg1212->sharedBufferPtr->volumeData[i] = val;
1773 change = 1;
1774 }
1775
1776 if (i >= 8) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001777 korg1212->volumePhase[i+1] = !!u->value.integer.value[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
1780
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001781 if ((u->value.integer.value[1] != 0) != (val < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
1783 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1784 change = 1;
1785 }
1786 }
1787
1788 spin_unlock_irq(&korg1212->lock);
1789
1790 return change;
1791}
1792
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001793static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
1794 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1797 uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
1798 uinfo->value.integer.min = k1212MinVolume;
1799 uinfo->value.integer.max = k1212MaxVolume;
1800 return 0;
1801}
1802
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001803static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001806 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int i;
1808
1809 spin_lock_irq(&korg1212->lock);
1810
1811 i = kcontrol->private_value;
1812 u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
1813
1814 if (i >= 8)
1815 u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
1816
1817 spin_unlock_irq(&korg1212->lock);
1818
1819 return 0;
1820}
1821
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001822static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
1823 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001825 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int change = 0;
1827 int i;
1828 int val;
1829
1830 spin_lock_irq(&korg1212->lock);
1831
1832 i = kcontrol->private_value;
1833
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001834 if (u->value.integer.value[0] >= k1212MinVolume &&
1835 u->value.integer.value[0] >= k1212MaxVolume &&
1836 u->value.integer.value[0] !=
1837 abs(korg1212->sharedBufferPtr->volumeData[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 val = korg1212->volumePhase[i] > 0 ? -1 : 1;
1839 val *= u->value.integer.value[0];
1840 korg1212->sharedBufferPtr->volumeData[i] = val;
1841 change = 1;
1842 }
1843
1844 if (i >= 8) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001845 if (u->value.integer.value[1] >= k1212MinVolume &&
1846 u->value.integer.value[1] >= k1212MaxVolume &&
1847 u->value.integer.value[1] !=
1848 abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
1850 val *= u->value.integer.value[1];
1851 korg1212->sharedBufferPtr->volumeData[i+1] = val;
1852 change = 1;
1853 }
1854 }
1855
1856 spin_unlock_irq(&korg1212->lock);
1857
1858 return change;
1859}
1860
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001861static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
1862 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Takashi Iwaif8612372014-10-20 18:21:58 +02001864 return snd_ctl_enum_info(uinfo,
1865 (kcontrol->private_value >= 8) ? 2 : 1,
1866 kAudioChannels, channelName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001869static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001872 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 int i;
1874
1875 spin_lock_irq(&korg1212->lock);
1876
1877 i = kcontrol->private_value;
1878 u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
1879
1880 if (i >= 8)
1881 u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
1882
1883 spin_unlock_irq(&korg1212->lock);
1884
1885 return 0;
1886}
1887
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001888static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001891 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int change = 0, i;
1893
1894 spin_lock_irq(&korg1212->lock);
1895
1896 i = kcontrol->private_value;
1897
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001898 if (u->value.enumerated.item[0] < kAudioChannels &&
1899 u->value.enumerated.item[0] !=
1900 (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
1902 change = 1;
1903 }
1904
1905 if (i >= 8) {
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001906 if (u->value.enumerated.item[1] < kAudioChannels &&
1907 u->value.enumerated.item[1] !=
1908 (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
1910 change = 1;
1911 }
1912 }
1913
1914 spin_unlock_irq(&korg1212->lock);
1915
1916 return change;
1917}
1918
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001919static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1923 uinfo->count = 2;
1924 uinfo->value.integer.min = k1212MaxADCSens;
1925 uinfo->value.integer.max = k1212MinADCSens;
1926 return 0;
1927}
1928
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001929static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001932 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 spin_lock_irq(&korg1212->lock);
1935
1936 u->value.integer.value[0] = korg1212->leftADCInSens;
1937 u->value.integer.value[1] = korg1212->rightADCInSens;
1938
1939 spin_unlock_irq(&korg1212->lock);
1940
1941 return 0;
1942}
1943
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001944static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_value *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001947 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int change = 0;
1949
1950 spin_lock_irq(&korg1212->lock);
1951
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001952 if (u->value.integer.value[0] >= k1212MinADCSens &&
1953 u->value.integer.value[0] <= k1212MaxADCSens &&
1954 u->value.integer.value[0] != korg1212->leftADCInSens) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 korg1212->leftADCInSens = u->value.integer.value[0];
1956 change = 1;
1957 }
Takashi Iwai4e98d6a2007-11-15 15:58:13 +01001958 if (u->value.integer.value[1] >= k1212MinADCSens &&
1959 u->value.integer.value[1] <= k1212MaxADCSens &&
1960 u->value.integer.value[1] != korg1212->rightADCInSens) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 korg1212->rightADCInSens = u->value.integer.value[1];
1962 change = 1;
1963 }
1964
1965 spin_unlock_irq(&korg1212->lock);
1966
1967 if (change)
1968 snd_korg1212_WriteADCSensitivity(korg1212);
1969
1970 return change;
1971}
1972
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001973static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
1974 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Takashi Iwaif8612372014-10-20 18:21:58 +02001976 return snd_ctl_enum_info(uinfo, 1, 3, clockSourceTypeName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001979static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001982 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 spin_lock_irq(&korg1212->lock);
1985
1986 ucontrol->value.enumerated.item[0] = korg1212->clkSource;
1987
1988 spin_unlock_irq(&korg1212->lock);
1989 return 0;
1990}
1991
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001992static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
1993 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01001995 struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 unsigned int val;
1997 int change;
1998
1999 val = ucontrol->value.enumerated.item[0] % 3;
2000 spin_lock_irq(&korg1212->lock);
2001 change = val != korg1212->clkSource;
2002 snd_korg1212_SetClockSource(korg1212, val);
2003 spin_unlock_irq(&korg1212->lock);
2004 return change;
2005}
2006
2007#define MON_MIXER(ord,c_name) \
2008 { \
2009 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2011 .name = c_name " Monitor Volume", \
2012 .info = snd_korg1212_control_volume_info, \
2013 .get = snd_korg1212_control_volume_get, \
2014 .put = snd_korg1212_control_volume_put, \
2015 .private_value = ord, \
2016 }, \
2017 { \
2018 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
2019 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2020 .name = c_name " Monitor Route", \
2021 .info = snd_korg1212_control_route_info, \
2022 .get = snd_korg1212_control_route_get, \
2023 .put = snd_korg1212_control_route_put, \
2024 .private_value = ord, \
2025 }, \
2026 { \
2027 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002028 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 .name = c_name " Monitor Phase Invert", \
2030 .info = snd_korg1212_control_phase_info, \
2031 .get = snd_korg1212_control_phase_get, \
2032 .put = snd_korg1212_control_phase_put, \
2033 .private_value = ord, \
2034 }
2035
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002036static struct snd_kcontrol_new snd_korg1212_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 MON_MIXER(8, "Analog"),
2038 MON_MIXER(10, "SPDIF"),
2039 MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
2040 MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
2041 {
2042 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02002043 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 .name = "Sync Source",
2045 .info = snd_korg1212_control_sync_info,
2046 .get = snd_korg1212_control_sync_get,
2047 .put = snd_korg1212_control_sync_put,
2048 },
2049 {
2050 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
2051 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2052 .name = "ADC Attenuation",
2053 .info = snd_korg1212_control_info,
2054 .get = snd_korg1212_control_get,
2055 .put = snd_korg1212_control_put,
2056 }
2057};
2058
2059/*
2060 * proc interface
2061 */
2062
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002063static void snd_korg1212_proc_read(struct snd_info_entry *entry,
2064 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 int n;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002067 struct snd_korg1212 *korg1212 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 snd_iprintf(buffer, korg1212->card->longname);
2070 snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
2071 snd_iprintf(buffer, "\nGeneral settings\n");
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002072 snd_iprintf(buffer, " period size: %zd bytes\n", K1212_PERIOD_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
2074 snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
2075 snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
2076 snd_iprintf(buffer, " Volume Info:\n");
2077 for (n=0; n<kAudioChannels; n++)
2078 snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
2079 channelName[n],
2080 channelName[korg1212->sharedBufferPtr->routeData[n]],
2081 korg1212->sharedBufferPtr->volumeData[n]);
2082 snd_iprintf(buffer, "\nGeneral status\n");
2083 snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
2084 snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
2085 snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
2086 snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
2087 snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
2088 snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
2089}
2090
Bill Pembertone23e7a12012-12-06 12:35:10 -05002091static void snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002093 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002096 snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099static int
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002100snd_korg1212_free(struct snd_korg1212 *korg1212)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 snd_korg1212_TurnOffIdleMonitor(korg1212);
2103
2104 if (korg1212->irq >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 snd_korg1212_DisableCardInterrupts(korg1212);
Takashi Iwai9fd91562005-11-17 10:45:48 +01002106 free_irq(korg1212->irq, korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 korg1212->irq = -1;
2108 }
2109
2110 if (korg1212->iobase != NULL) {
2111 iounmap(korg1212->iobase);
2112 korg1212->iobase = NULL;
2113 }
2114
2115 pci_release_regions(korg1212->pci);
2116
2117 // ----------------------------------------------------
2118 // free up memory resources used for the DSP download.
2119 // ----------------------------------------------------
2120 if (korg1212->dma_dsp.area) {
2121 snd_dma_free_pages(&korg1212->dma_dsp);
2122 korg1212->dma_dsp.area = NULL;
2123 }
2124
2125#ifndef K1212_LARGEALLOC
2126
2127 // ------------------------------------------------------
2128 // free up memory resources used for the Play/Rec Buffers
2129 // ------------------------------------------------------
2130 if (korg1212->dma_play.area) {
2131 snd_dma_free_pages(&korg1212->dma_play);
2132 korg1212->dma_play.area = NULL;
2133 }
2134
2135 if (korg1212->dma_rec.area) {
2136 snd_dma_free_pages(&korg1212->dma_rec);
2137 korg1212->dma_rec.area = NULL;
2138 }
2139
2140#endif
2141
2142 // ----------------------------------------------------
2143 // free up memory resources used for the Shared Buffers
2144 // ----------------------------------------------------
2145 if (korg1212->dma_shared.area) {
2146 snd_dma_free_pages(&korg1212->dma_shared);
2147 korg1212->dma_shared.area = NULL;
2148 }
2149
2150 pci_disable_device(korg1212->pci);
2151 kfree(korg1212);
2152 return 0;
2153}
2154
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002155static int snd_korg1212_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002157 struct snd_korg1212 *korg1212 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return snd_korg1212_free(korg1212);
2160}
2161
Bill Pembertone23e7a12012-12-06 12:35:10 -05002162static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
2163 struct snd_korg1212 **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165{
Takashi Iwai9fd91562005-11-17 10:45:48 +01002166 int err, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 unsigned int i;
2168 unsigned ioport_size, iomem_size, iomem2_size;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002169 struct snd_korg1212 * korg1212;
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002170 const struct firmware *dsp_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002172 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .dev_free = snd_korg1212_dev_free,
2174 };
2175
2176 * rchip = NULL;
2177 if ((err = pci_enable_device(pci)) < 0)
2178 return err;
2179
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002180 korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (korg1212 == NULL) {
2182 pci_disable_device(pci);
2183 return -ENOMEM;
2184 }
2185
2186 korg1212->card = card;
2187 korg1212->pci = pci;
2188
2189 init_waitqueue_head(&korg1212->wait);
2190 spin_lock_init(&korg1212->lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002191 mutex_init(&korg1212->open_mutex);
Kees Cook7211ec62017-10-25 08:09:27 -07002192 timer_setup(&korg1212->timer, snd_korg1212_timer_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 korg1212->irq = -1;
2195 korg1212->clkSource = K1212_CLKIDX_Local;
2196 korg1212->clkRate = 44100;
2197 korg1212->inIRQ = 0;
2198 korg1212->running = 0;
2199 korg1212->opencnt = 0;
2200 korg1212->playcnt = 0;
2201 korg1212->setcnt = 0;
2202 korg1212->totalerrorcnt = 0;
2203 korg1212->playback_pid = -1;
2204 korg1212->capture_pid = -1;
2205 snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
2206 korg1212->idleMonitorOn = 0;
2207 korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
2208 korg1212->leftADCInSens = k1212MaxADCSens;
2209 korg1212->rightADCInSens = k1212MaxADCSens;
2210
2211 for (i=0; i<kAudioChannels; i++)
2212 korg1212->volumePhase[i] = 0;
2213
2214 if ((err = pci_request_regions(pci, "korg1212")) < 0) {
2215 kfree(korg1212);
2216 pci_disable_device(pci);
2217 return err;
2218 }
2219
2220 korg1212->iomem = pci_resource_start(korg1212->pci, 0);
2221 korg1212->ioport = pci_resource_start(korg1212->pci, 1);
2222 korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
2223
2224 iomem_size = pci_resource_len(korg1212->pci, 0);
2225 ioport_size = pci_resource_len(korg1212->pci, 1);
2226 iomem2_size = pci_resource_len(korg1212->pci, 2);
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
2229 " iomem = 0x%lx (%d)\n"
2230 " ioport = 0x%lx (%d)\n"
2231 " iomem = 0x%lx (%d)\n"
2232 " [%s]\n",
2233 korg1212->iomem, iomem_size,
2234 korg1212->ioport, ioport_size,
2235 korg1212->iomem2, iomem2_size,
2236 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
2239 snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
2240 korg1212->iomem + iomem_size - 1);
2241 snd_korg1212_free(korg1212);
2242 return -EBUSY;
2243 }
2244
2245 err = request_irq(pci->irq, snd_korg1212_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002246 IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002247 KBUILD_MODNAME, korg1212);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 if (err) {
2250 snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
2251 snd_korg1212_free(korg1212);
2252 return -EBUSY;
2253 }
2254
2255 korg1212->irq = pci->irq;
2256
2257 pci_set_master(korg1212->pci);
2258
2259 korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
2260 korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
2261 korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
2262 korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
2263 korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
2264 korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
2265 korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
2266 korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
2267 korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
2268 korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
2271 " Status register = 0x%p\n"
2272 " OutDoorbell = 0x%p\n"
2273 " InDoorbell = 0x%p\n"
2274 " Mailbox0 = 0x%p\n"
2275 " Mailbox1 = 0x%p\n"
2276 " Mailbox2 = 0x%p\n"
2277 " Mailbox3 = 0x%p\n"
2278 " ControlReg = 0x%p\n"
2279 " SensReg = 0x%p\n"
2280 " IDReg = 0x%p\n"
2281 " [%s]\n",
2282 korg1212->statusRegPtr,
2283 korg1212->outDoorbellPtr,
2284 korg1212->inDoorbellPtr,
2285 korg1212->mailbox0Ptr,
2286 korg1212->mailbox1Ptr,
2287 korg1212->mailbox2Ptr,
2288 korg1212->mailbox3Ptr,
2289 korg1212->controlRegPtr,
2290 korg1212->sensRegPtr,
2291 korg1212->idRegPtr,
2292 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002295 sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002296 snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%zd bytes)\n", sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 snd_korg1212_free(korg1212);
2298 return -ENOMEM;
2299 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002300 korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
2302
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002303 K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305#ifndef K1212_LARGEALLOC
2306
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002307 korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2310 korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
2311 snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2312 snd_korg1212_free(korg1212);
2313 return -ENOMEM;
2314 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002315 korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 korg1212->PlayDataPhy = korg1212->dma_play.addr;
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
2319 korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2322 korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
2323 snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
2324 snd_korg1212_free(korg1212);
2325 return -ENOMEM;
2326 }
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002327 korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 korg1212->RecDataPhy = korg1212->dma_rec.addr;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
2331 korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333#else // K1212_LARGEALLOC
2334
2335 korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
2336 korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002337 korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
2338 korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340#endif // K1212_LARGEALLOC
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002343 offsetof(struct KorgSharedBuffer, volumeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002345 offsetof(struct KorgSharedBuffer, routeData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002347 offsetof(struct KorgSharedBuffer, AdatTimeCode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002349 err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
2350 if (err < 0) {
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002351 snd_printk(KERN_ERR "firmware not available\n");
2352 snd_korg1212_free(korg1212);
2353 return err;
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002354 }
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002357 dsp_code->size, &korg1212->dma_dsp) < 0) {
Randy Dunlap9fb62c92006-11-21 19:01:51 +01002358 snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 snd_korg1212_free(korg1212);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002360 release_firmware(dsp_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return -ENOMEM;
2362 }
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002365 korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002368 memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
2369
Takashi Iwaib7dd2b32007-04-26 14:13:44 +02002370 release_firmware(dsp_code);
Clemens Ladisch2493a6d2006-11-06 09:24:29 +01002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
2373
Takashi Iwai9fd91562005-11-17 10:45:48 +01002374 if (rc)
2375 K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
2378 snd_korg1212_free(korg1212);
2379 return err;
2380 }
2381
2382 snd_korg1212_EnableCardInterrupts(korg1212);
2383
2384 mdelay(CARD_BOOT_DELAY_IN_MS);
2385
2386 if (snd_korg1212_downloadDSPCode(korg1212))
2387 return -EBUSY;
2388
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002389 K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 "PlayDataPhy = %08x L[%08x]\n"
2391 "korg1212: RecDataPhy = %08x L[%08x], "
2392 "VolumeTablePhy = %08x L[%08x]\n"
2393 "korg1212: RoutingTablePhy = %08x L[%08x], "
2394 "AdatTimeCodePhy = %08x L[%08x]\n",
2395 (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
2396 korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
2397 korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
2398 korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
2399 korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
2400 korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
2401
2402 if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
2403 return err;
2404
2405 korg1212->pcm->private_data = korg1212;
2406 korg1212->pcm->private_free = snd_korg1212_free_pcm;
2407 strcpy(korg1212->pcm->name, "korg1212");
2408
2409 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
2410
2411 snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
2412
2413 korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
2416 err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
2417 if (err < 0)
2418 return err;
2419 }
2420
2421 snd_korg1212_proc_init(korg1212);
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 * rchip = korg1212;
2424 return 0;
2425
2426}
2427
2428/*
2429 * Card initialisation
2430 */
2431
Bill Pembertone23e7a12012-12-06 12:35:10 -05002432static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433snd_korg1212_probe(struct pci_dev *pci,
2434 const struct pci_device_id *pci_id)
2435{
2436 static int dev;
Takashi Iwaifcfd3332005-11-17 15:00:46 +01002437 struct snd_korg1212 *korg1212;
2438 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 int err;
2440
2441 if (dev >= SNDRV_CARDS) {
2442 return -ENODEV;
2443 }
2444 if (!enable[dev]) {
2445 dev++;
2446 return -ENOENT;
2447 }
Takashi Iwai60c57722014-01-29 14:20:19 +01002448 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2449 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002450 if (err < 0)
2451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
2454 snd_card_free(card);
2455 return err;
2456 }
2457
2458 strcpy(card->driver, "korg1212");
2459 strcpy(card->shortname, "korg1212");
2460 sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
2461 korg1212->iomem, korg1212->irq);
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 if ((err = snd_card_register(card)) < 0) {
2466 snd_card_free(card);
2467 return err;
2468 }
2469 pci_set_drvdata(pci, card);
2470 dev++;
2471 return 0;
2472}
2473
Bill Pembertone23e7a12012-12-06 12:35:10 -05002474static void snd_korg1212_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002479static struct pci_driver korg1212_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002480 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 .id_table = snd_korg1212_ids,
2482 .probe = snd_korg1212_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002483 .remove = snd_korg1212_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484};
2485
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002486module_pci_driver(korg1212_driver);