]> err.no Git - linux-2.6/blob - sound/pci/ymfpci/ymfpci_main.c
[ALSA] ymfpci: fix volume handling of the 44.1 kHz slot
[linux-2.6] / sound / pci / ymfpci / ymfpci_main.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Routines for control of YMF724/740/744/754 chips
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  */
20
21 #include <sound/driver.h>
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/pci.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/tlv.h>
35 #include <sound/ymfpci.h>
36 #include <sound/asoundef.h>
37 #include <sound/mpu401.h>
38
39 #include <asm/io.h>
40 #include <asm/byteorder.h>
41
42 /*
43  *  common I/O routines
44  */
45
46 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
47
48 static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
49 {
50         return readb(chip->reg_area_virt + offset);
51 }
52
53 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
54 {
55         writeb(val, chip->reg_area_virt + offset);
56 }
57
58 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
59 {
60         return readw(chip->reg_area_virt + offset);
61 }
62
63 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
64 {
65         writew(val, chip->reg_area_virt + offset);
66 }
67
68 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
69 {
70         return readl(chip->reg_area_virt + offset);
71 }
72
73 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
74 {
75         writel(val, chip->reg_area_virt + offset);
76 }
77
78 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
79 {
80         unsigned long end_time;
81         u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
82         
83         end_time = jiffies + msecs_to_jiffies(750);
84         do {
85                 if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
86                         return 0;
87                 set_current_state(TASK_UNINTERRUPTIBLE);
88                 schedule_timeout_uninterruptible(1);
89         } while (time_before(jiffies, end_time));
90         snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
91         return -EBUSY;
92 }
93
94 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
95 {
96         struct snd_ymfpci *chip = ac97->private_data;
97         u32 cmd;
98         
99         snd_ymfpci_codec_ready(chip, 0);
100         cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
101         snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
102 }
103
104 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
105 {
106         struct snd_ymfpci *chip = ac97->private_data;
107
108         if (snd_ymfpci_codec_ready(chip, 0))
109                 return ~0;
110         snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
111         if (snd_ymfpci_codec_ready(chip, 0))
112                 return ~0;
113         if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
114                 int i;
115                 for (i = 0; i < 600; i++)
116                         snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
117         }
118         return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
119 }
120
121 /*
122  *  Misc routines
123  */
124
125 static u32 snd_ymfpci_calc_delta(u32 rate)
126 {
127         switch (rate) {
128         case 8000:      return 0x02aaab00;
129         case 11025:     return 0x03accd00;
130         case 16000:     return 0x05555500;
131         case 22050:     return 0x07599a00;
132         case 32000:     return 0x0aaaab00;
133         case 44100:     return 0x0eb33300;
134         default:        return ((rate << 16) / 375) << 5;
135         }
136 }
137
138 static u32 def_rate[8] = {
139         100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
140 };
141
142 static u32 snd_ymfpci_calc_lpfK(u32 rate)
143 {
144         u32 i;
145         static u32 val[8] = {
146                 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
147                 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
148         };
149         
150         if (rate == 44100)
151                 return 0x40000000;      /* FIXME: What's the right value? */
152         for (i = 0; i < 8; i++)
153                 if (rate <= def_rate[i])
154                         return val[i];
155         return val[0];
156 }
157
158 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
159 {
160         u32 i;
161         static u32 val[8] = {
162                 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
163                 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
164         };
165         
166         if (rate == 44100)
167                 return 0x370A0000;
168         for (i = 0; i < 8; i++)
169                 if (rate <= def_rate[i])
170                         return val[i];
171         return val[0];
172 }
173
174 /*
175  *  Hardware start management
176  */
177
178 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
179 {
180         unsigned long flags;
181
182         spin_lock_irqsave(&chip->reg_lock, flags);
183         if (chip->start_count++ > 0)
184                 goto __end;
185         snd_ymfpci_writel(chip, YDSXGR_MODE,
186                           snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
187         chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
188       __end:
189         spin_unlock_irqrestore(&chip->reg_lock, flags);
190 }
191
192 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
193 {
194         unsigned long flags;
195         long timeout = 1000;
196
197         spin_lock_irqsave(&chip->reg_lock, flags);
198         if (--chip->start_count > 0)
199                 goto __end;
200         snd_ymfpci_writel(chip, YDSXGR_MODE,
201                           snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
202         while (timeout-- > 0) {
203                 if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
204                         break;
205         }
206         if (atomic_read(&chip->interrupt_sleep_count)) {
207                 atomic_set(&chip->interrupt_sleep_count, 0);
208                 wake_up(&chip->interrupt_sleep);
209         }
210       __end:
211         spin_unlock_irqrestore(&chip->reg_lock, flags);
212 }
213
214 /*
215  *  Playback voice management
216  */
217
218 static int voice_alloc(struct snd_ymfpci *chip,
219                        enum snd_ymfpci_voice_type type, int pair,
220                        struct snd_ymfpci_voice **rvoice)
221 {
222         struct snd_ymfpci_voice *voice, *voice2;
223         int idx;
224         
225         *rvoice = NULL;
226         for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
227                 voice = &chip->voices[idx];
228                 voice2 = pair ? &chip->voices[idx+1] : NULL;
229                 if (voice->use || (voice2 && voice2->use))
230                         continue;
231                 voice->use = 1;
232                 if (voice2)
233                         voice2->use = 1;
234                 switch (type) {
235                 case YMFPCI_PCM:
236                         voice->pcm = 1;
237                         if (voice2)
238                                 voice2->pcm = 1;
239                         break;
240                 case YMFPCI_SYNTH:
241                         voice->synth = 1;
242                         break;
243                 case YMFPCI_MIDI:
244                         voice->midi = 1;
245                         break;
246                 }
247                 snd_ymfpci_hw_start(chip);
248                 if (voice2)
249                         snd_ymfpci_hw_start(chip);
250                 *rvoice = voice;
251                 return 0;
252         }
253         return -ENOMEM;
254 }
255
256 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
257                                   enum snd_ymfpci_voice_type type, int pair,
258                                   struct snd_ymfpci_voice **rvoice)
259 {
260         unsigned long flags;
261         int result;
262         
263         snd_assert(rvoice != NULL, return -EINVAL);
264         snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
265         
266         spin_lock_irqsave(&chip->voice_lock, flags);
267         for (;;) {
268                 result = voice_alloc(chip, type, pair, rvoice);
269                 if (result == 0 || type != YMFPCI_PCM)
270                         break;
271                 /* TODO: synth/midi voice deallocation */
272                 break;
273         }
274         spin_unlock_irqrestore(&chip->voice_lock, flags);       
275         return result;          
276 }
277
278 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
279 {
280         unsigned long flags;
281         
282         snd_assert(pvoice != NULL, return -EINVAL);
283         snd_ymfpci_hw_stop(chip);
284         spin_lock_irqsave(&chip->voice_lock, flags);
285         if (pvoice->number == chip->src441_used) {
286                 chip->src441_used = -1;
287                 pvoice->ypcm->use_441_slot = 0;
288         }
289         pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
290         pvoice->ypcm = NULL;
291         pvoice->interrupt = NULL;
292         spin_unlock_irqrestore(&chip->voice_lock, flags);
293         return 0;
294 }
295
296 /*
297  *  PCM part
298  */
299
300 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
301 {
302         struct snd_ymfpci_pcm *ypcm;
303         u32 pos, delta;
304         
305         if ((ypcm = voice->ypcm) == NULL)
306                 return;
307         if (ypcm->substream == NULL)
308                 return;
309         spin_lock(&chip->reg_lock);
310         if (ypcm->running) {
311                 pos = le32_to_cpu(voice->bank[chip->active_bank].start);
312                 if (pos < ypcm->last_pos)
313                         delta = pos + (ypcm->buffer_size - ypcm->last_pos);
314                 else
315                         delta = pos - ypcm->last_pos;
316                 ypcm->period_pos += delta;
317                 ypcm->last_pos = pos;
318                 if (ypcm->period_pos >= ypcm->period_size) {
319                         // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
320                         ypcm->period_pos %= ypcm->period_size;
321                         spin_unlock(&chip->reg_lock);
322                         snd_pcm_period_elapsed(ypcm->substream);
323                         spin_lock(&chip->reg_lock);
324                 }
325
326                 if (unlikely(ypcm->update_pcm_vol)) {
327                         unsigned int subs = ypcm->substream->number;
328                         unsigned int next_bank = 1 - chip->active_bank;
329                         struct snd_ymfpci_playback_bank *bank;
330                         u32 volume;
331                         
332                         bank = &voice->bank[next_bank];
333                         volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
334                         bank->left_gain_end = volume;
335                         if (ypcm->output_rear)
336                                 bank->eff2_gain_end = volume;
337                         if (ypcm->voices[1])
338                                 bank = &ypcm->voices[1]->bank[next_bank];
339                         volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
340                         bank->right_gain_end = volume;
341                         if (ypcm->output_rear)
342                                 bank->eff3_gain_end = volume;
343                         ypcm->update_pcm_vol--;
344                 }
345         }
346         spin_unlock(&chip->reg_lock);
347 }
348
349 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
350 {
351         struct snd_pcm_runtime *runtime = substream->runtime;
352         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
353         struct snd_ymfpci *chip = ypcm->chip;
354         u32 pos, delta;
355         
356         spin_lock(&chip->reg_lock);
357         if (ypcm->running) {
358                 pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
359                 if (pos < ypcm->last_pos)
360                         delta = pos + (ypcm->buffer_size - ypcm->last_pos);
361                 else
362                         delta = pos - ypcm->last_pos;
363                 ypcm->period_pos += delta;
364                 ypcm->last_pos = pos;
365                 if (ypcm->period_pos >= ypcm->period_size) {
366                         ypcm->period_pos %= ypcm->period_size;
367                         // printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
368                         spin_unlock(&chip->reg_lock);
369                         snd_pcm_period_elapsed(substream);
370                         spin_lock(&chip->reg_lock);
371                 }
372         }
373         spin_unlock(&chip->reg_lock);
374 }
375
376 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
377                                        int cmd)
378 {
379         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
380         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
381         struct snd_kcontrol *kctl = NULL;
382         int result = 0;
383
384         spin_lock(&chip->reg_lock);
385         if (ypcm->voices[0] == NULL) {
386                 result = -EINVAL;
387                 goto __unlock;
388         }
389         switch (cmd) {
390         case SNDRV_PCM_TRIGGER_START:
391         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
392         case SNDRV_PCM_TRIGGER_RESUME:
393                 chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
394                 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
395                         chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
396                 ypcm->running = 1;
397                 break;
398         case SNDRV_PCM_TRIGGER_STOP:
399                 if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
400                         kctl = chip->pcm_mixer[substream->number].ctl;
401                         kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
402                 }
403                 /* fall through */
404         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
405         case SNDRV_PCM_TRIGGER_SUSPEND:
406                 chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
407                 if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
408                         chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
409                 ypcm->running = 0;
410                 break;
411         default:
412                 result = -EINVAL;
413                 break;
414         }
415       __unlock:
416         spin_unlock(&chip->reg_lock);
417         if (kctl)
418                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
419         return result;
420 }
421 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
422                                       int cmd)
423 {
424         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
425         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
426         int result = 0;
427         u32 tmp;
428
429         spin_lock(&chip->reg_lock);
430         switch (cmd) {
431         case SNDRV_PCM_TRIGGER_START:
432         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
433         case SNDRV_PCM_TRIGGER_RESUME:
434                 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
435                 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
436                 ypcm->running = 1;
437                 break;
438         case SNDRV_PCM_TRIGGER_STOP:
439         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
440         case SNDRV_PCM_TRIGGER_SUSPEND:
441                 tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
442                 snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
443                 ypcm->running = 0;
444                 break;
445         default:
446                 result = -EINVAL;
447                 break;
448         }
449         spin_unlock(&chip->reg_lock);
450         return result;
451 }
452
453 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
454 {
455         int err;
456
457         if (ypcm->voices[1] != NULL && voices < 2) {
458                 snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
459                 ypcm->voices[1] = NULL;
460         }
461         if (voices == 1 && ypcm->voices[0] != NULL)
462                 return 0;               /* already allocated */
463         if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
464                 return 0;               /* already allocated */
465         if (voices > 1) {
466                 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
467                         snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
468                         ypcm->voices[0] = NULL;
469                 }               
470         }
471         err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
472         if (err < 0)
473                 return err;
474         ypcm->voices[0]->ypcm = ypcm;
475         ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
476         if (voices > 1) {
477                 ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
478                 ypcm->voices[1]->ypcm = ypcm;
479         }
480         return 0;
481 }
482
483 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
484                                       struct snd_pcm_runtime *runtime,
485                                       int has_pcm_volume)
486 {
487         struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
488         u32 format;
489         u32 delta = snd_ymfpci_calc_delta(runtime->rate);
490         u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
491         u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
492         struct snd_ymfpci_playback_bank *bank;
493         unsigned int nbank;
494         u32 vol_left, vol_right;
495         u8 use_left, use_right;
496         unsigned long flags;
497
498         snd_assert(voice != NULL, return);
499         if (runtime->channels == 1) {
500                 use_left = 1;
501                 use_right = 1;
502         } else {
503                 use_left = (voiceidx & 1) == 0;
504                 use_right = !use_left;
505         }
506         if (has_pcm_volume) {
507                 vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
508                                        [ypcm->substream->number].left << 15);
509                 vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
510                                         [ypcm->substream->number].right << 15);
511         } else {
512                 vol_left = cpu_to_le32(0x40000000);
513                 vol_right = cpu_to_le32(0x40000000);
514         }
515         spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
516         format = runtime->channels == 2 ? 0x00010000 : 0;
517         if (snd_pcm_format_width(runtime->format) == 8)
518                 format |= 0x80000000;
519         else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
520                  runtime->rate == 44100 && runtime->channels == 2 &&
521                  voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
522                                    ypcm->chip->src441_used == voice->number)) {
523                 ypcm->chip->src441_used = voice->number;
524                 ypcm->use_441_slot = 1;
525                 format |= 0x10000000;
526         }
527         if (ypcm->chip->src441_used == voice->number &&
528             (format & 0x10000000) == 0) {
529                 ypcm->chip->src441_used = -1;
530                 ypcm->use_441_slot = 0;
531         }
532         if (runtime->channels == 2 && (voiceidx & 1) != 0)
533                 format |= 1;
534         spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
535         for (nbank = 0; nbank < 2; nbank++) {
536                 bank = &voice->bank[nbank];
537                 memset(bank, 0, sizeof(*bank));
538                 bank->format = cpu_to_le32(format);
539                 bank->base = cpu_to_le32(runtime->dma_addr);
540                 bank->loop_end = cpu_to_le32(ypcm->buffer_size);
541                 bank->lpfQ = cpu_to_le32(lpfQ);
542                 bank->delta =
543                 bank->delta_end = cpu_to_le32(delta);
544                 bank->lpfK =
545                 bank->lpfK_end = cpu_to_le32(lpfK);
546                 bank->eg_gain =
547                 bank->eg_gain_end = cpu_to_le32(0x40000000);
548
549                 if (ypcm->output_front) {
550                         if (use_left) {
551                                 bank->left_gain =
552                                 bank->left_gain_end = vol_left;
553                         }
554                         if (use_right) {
555                                 bank->right_gain =
556                                 bank->right_gain_end = vol_right;
557                         }
558                 }
559                 if (ypcm->output_rear) {
560                         if (!ypcm->swap_rear) {
561                                 if (use_left) {
562                                         bank->eff2_gain =
563                                         bank->eff2_gain_end = vol_left;
564                                 }
565                                 if (use_right) {
566                                         bank->eff3_gain =
567                                         bank->eff3_gain_end = vol_right;
568                                 }
569                         } else {
570                                 /* The SPDIF out channels seem to be swapped, so we have
571                                  * to swap them here, too.  The rear analog out channels
572                                  * will be wrong, but otherwise AC3 would not work.
573                                  */
574                                 if (use_left) {
575                                         bank->eff3_gain =
576                                         bank->eff3_gain_end = vol_left;
577                                 }
578                                 if (use_right) {
579                                         bank->eff2_gain =
580                                         bank->eff2_gain_end = vol_right;
581                                 }
582                         }
583                 }
584         }
585 }
586
587 static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
588 {
589         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
590                                 4096, &chip->ac3_tmp_base) < 0)
591                 return -ENOMEM;
592
593         chip->bank_effect[3][0]->base =
594         chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
595         chip->bank_effect[3][0]->loop_end =
596         chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
597         chip->bank_effect[4][0]->base =
598         chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
599         chip->bank_effect[4][0]->loop_end =
600         chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
601
602         spin_lock_irq(&chip->reg_lock);
603         snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
604                           snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
605         spin_unlock_irq(&chip->reg_lock);
606         return 0;
607 }
608
609 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
610 {
611         spin_lock_irq(&chip->reg_lock);
612         snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
613                           snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
614         spin_unlock_irq(&chip->reg_lock);
615         // snd_ymfpci_irq_wait(chip);
616         if (chip->ac3_tmp_base.area) {
617                 snd_dma_free_pages(&chip->ac3_tmp_base);
618                 chip->ac3_tmp_base.area = NULL;
619         }
620         return 0;
621 }
622
623 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
624                                          struct snd_pcm_hw_params *hw_params)
625 {
626         struct snd_pcm_runtime *runtime = substream->runtime;
627         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
628         int err;
629
630         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
631                 return err;
632         if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
633                 return err;
634         return 0;
635 }
636
637 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
638 {
639         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
640         struct snd_pcm_runtime *runtime = substream->runtime;
641         struct snd_ymfpci_pcm *ypcm;
642         
643         if (runtime->private_data == NULL)
644                 return 0;
645         ypcm = runtime->private_data;
646
647         /* wait, until the PCI operations are not finished */
648         snd_ymfpci_irq_wait(chip);
649         snd_pcm_lib_free_pages(substream);
650         if (ypcm->voices[1]) {
651                 snd_ymfpci_voice_free(chip, ypcm->voices[1]);
652                 ypcm->voices[1] = NULL;
653         }
654         if (ypcm->voices[0]) {
655                 snd_ymfpci_voice_free(chip, ypcm->voices[0]);
656                 ypcm->voices[0] = NULL;
657         }
658         return 0;
659 }
660
661 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
662 {
663         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
664         struct snd_pcm_runtime *runtime = substream->runtime;
665         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
666         struct snd_kcontrol *kctl;
667         unsigned int nvoice;
668
669         ypcm->period_size = runtime->period_size;
670         ypcm->buffer_size = runtime->buffer_size;
671         ypcm->period_pos = 0;
672         ypcm->last_pos = 0;
673         for (nvoice = 0; nvoice < runtime->channels; nvoice++)
674                 snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
675                                           substream->pcm == chip->pcm);
676
677         if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
678                 kctl = chip->pcm_mixer[substream->number].ctl;
679                 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
680                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
681         }
682         return 0;
683 }
684
685 static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
686                                         struct snd_pcm_hw_params *hw_params)
687 {
688         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
689 }
690
691 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
692 {
693         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
694
695         /* wait, until the PCI operations are not finished */
696         snd_ymfpci_irq_wait(chip);
697         return snd_pcm_lib_free_pages(substream);
698 }
699
700 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
701 {
702         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
703         struct snd_pcm_runtime *runtime = substream->runtime;
704         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
705         struct snd_ymfpci_capture_bank * bank;
706         int nbank;
707         u32 rate, format;
708
709         ypcm->period_size = runtime->period_size;
710         ypcm->buffer_size = runtime->buffer_size;
711         ypcm->period_pos = 0;
712         ypcm->last_pos = 0;
713         ypcm->shift = 0;
714         rate = ((48000 * 4096) / runtime->rate) - 1;
715         format = 0;
716         if (runtime->channels == 2) {
717                 format |= 2;
718                 ypcm->shift++;
719         }
720         if (snd_pcm_format_width(runtime->format) == 8)
721                 format |= 1;
722         else
723                 ypcm->shift++;
724         switch (ypcm->capture_bank_number) {
725         case 0:
726                 snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
727                 snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
728                 break;
729         case 1:
730                 snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
731                 snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
732                 break;
733         }
734         for (nbank = 0; nbank < 2; nbank++) {
735                 bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
736                 bank->base = cpu_to_le32(runtime->dma_addr);
737                 bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
738                 bank->start = 0;
739                 bank->num_of_loops = 0;
740         }
741         return 0;
742 }
743
744 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
745 {
746         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
747         struct snd_pcm_runtime *runtime = substream->runtime;
748         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
749         struct snd_ymfpci_voice *voice = ypcm->voices[0];
750
751         if (!(ypcm->running && voice))
752                 return 0;
753         return le32_to_cpu(voice->bank[chip->active_bank].start);
754 }
755
756 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
757 {
758         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
759         struct snd_pcm_runtime *runtime = substream->runtime;
760         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
761
762         if (!ypcm->running)
763                 return 0;
764         return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
765 }
766
767 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
768 {
769         wait_queue_t wait;
770         int loops = 4;
771
772         while (loops-- > 0) {
773                 if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
774                         continue;
775                 init_waitqueue_entry(&wait, current);
776                 add_wait_queue(&chip->interrupt_sleep, &wait);
777                 atomic_inc(&chip->interrupt_sleep_count);
778                 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
779                 remove_wait_queue(&chip->interrupt_sleep, &wait);
780         }
781 }
782
783 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
784 {
785         struct snd_ymfpci *chip = dev_id;
786         u32 status, nvoice, mode;
787         struct snd_ymfpci_voice *voice;
788
789         status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
790         if (status & 0x80000000) {
791                 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
792                 spin_lock(&chip->voice_lock);
793                 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
794                         voice = &chip->voices[nvoice];
795                         if (voice->interrupt)
796                                 voice->interrupt(chip, voice);
797                 }
798                 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
799                         if (chip->capture_substream[nvoice])
800                                 snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
801                 }
802 #if 0
803                 for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
804                         if (chip->effect_substream[nvoice])
805                                 snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
806                 }
807 #endif
808                 spin_unlock(&chip->voice_lock);
809                 spin_lock(&chip->reg_lock);
810                 snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
811                 mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
812                 snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
813                 spin_unlock(&chip->reg_lock);
814
815                 if (atomic_read(&chip->interrupt_sleep_count)) {
816                         atomic_set(&chip->interrupt_sleep_count, 0);
817                         wake_up(&chip->interrupt_sleep);
818                 }
819         }
820
821         status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
822         if (status & 1) {
823                 if (chip->timer)
824                         snd_timer_interrupt(chip->timer, chip->timer->sticks);
825         }
826         snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
827
828         if (chip->rawmidi)
829                 snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
830         return IRQ_HANDLED;
831 }
832
833 static struct snd_pcm_hardware snd_ymfpci_playback =
834 {
835         .info =                 (SNDRV_PCM_INFO_MMAP |
836                                  SNDRV_PCM_INFO_MMAP_VALID | 
837                                  SNDRV_PCM_INFO_INTERLEAVED |
838                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
839                                  SNDRV_PCM_INFO_PAUSE |
840                                  SNDRV_PCM_INFO_RESUME),
841         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
842         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
843         .rate_min =             8000,
844         .rate_max =             48000,
845         .channels_min =         1,
846         .channels_max =         2,
847         .buffer_bytes_max =     256 * 1024, /* FIXME: enough? */
848         .period_bytes_min =     64,
849         .period_bytes_max =     256 * 1024, /* FIXME: enough? */
850         .periods_min =          3,
851         .periods_max =          1024,
852         .fifo_size =            0,
853 };
854
855 static struct snd_pcm_hardware snd_ymfpci_capture =
856 {
857         .info =                 (SNDRV_PCM_INFO_MMAP |
858                                  SNDRV_PCM_INFO_MMAP_VALID |
859                                  SNDRV_PCM_INFO_INTERLEAVED |
860                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
861                                  SNDRV_PCM_INFO_PAUSE |
862                                  SNDRV_PCM_INFO_RESUME),
863         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
864         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
865         .rate_min =             8000,
866         .rate_max =             48000,
867         .channels_min =         1,
868         .channels_max =         2,
869         .buffer_bytes_max =     256 * 1024, /* FIXME: enough? */
870         .period_bytes_min =     64,
871         .period_bytes_max =     256 * 1024, /* FIXME: enough? */
872         .periods_min =          3,
873         .periods_max =          1024,
874         .fifo_size =            0,
875 };
876
877 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
878 {
879         kfree(runtime->private_data);
880 }
881
882 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
883 {
884         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
885         struct snd_pcm_runtime *runtime = substream->runtime;
886         struct snd_ymfpci_pcm *ypcm;
887
888         ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
889         if (ypcm == NULL)
890                 return -ENOMEM;
891         ypcm->chip = chip;
892         ypcm->type = PLAYBACK_VOICE;
893         ypcm->substream = substream;
894         runtime->hw = snd_ymfpci_playback;
895         runtime->private_data = ypcm;
896         runtime->private_free = snd_ymfpci_pcm_free_substream;
897         /* FIXME? True value is 256/48 = 5.33333 ms */
898         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
899         return 0;
900 }
901
902 /* call with spinlock held */
903 static void ymfpci_open_extension(struct snd_ymfpci *chip)
904 {
905         if (! chip->rear_opened) {
906                 if (! chip->spdif_opened) /* set AC3 */
907                         snd_ymfpci_writel(chip, YDSXGR_MODE,
908                                           snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
909                 /* enable second codec (4CHEN) */
910                 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
911                                   (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
912         }
913 }
914
915 /* call with spinlock held */
916 static void ymfpci_close_extension(struct snd_ymfpci *chip)
917 {
918         if (! chip->rear_opened) {
919                 if (! chip->spdif_opened)
920                         snd_ymfpci_writel(chip, YDSXGR_MODE,
921                                           snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
922                 snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
923                                   (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
924         }
925 }
926
927 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
928 {
929         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
930         struct snd_pcm_runtime *runtime = substream->runtime;
931         struct snd_ymfpci_pcm *ypcm;
932         int err;
933         
934         if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
935                 return err;
936         ypcm = runtime->private_data;
937         ypcm->output_front = 1;
938         ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
939         ypcm->swap_rear = 0;
940         spin_lock_irq(&chip->reg_lock);
941         if (ypcm->output_rear) {
942                 ymfpci_open_extension(chip);
943                 chip->rear_opened++;
944         }
945         spin_unlock_irq(&chip->reg_lock);
946         return 0;
947 }
948
949 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
950 {
951         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
952         struct snd_pcm_runtime *runtime = substream->runtime;
953         struct snd_ymfpci_pcm *ypcm;
954         int err;
955         
956         if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
957                 return err;
958         ypcm = runtime->private_data;
959         ypcm->output_front = 0;
960         ypcm->output_rear = 1;
961         ypcm->swap_rear = 1;
962         spin_lock_irq(&chip->reg_lock);
963         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
964                           snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
965         ymfpci_open_extension(chip);
966         chip->spdif_pcm_bits = chip->spdif_bits;
967         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
968         chip->spdif_opened++;
969         spin_unlock_irq(&chip->reg_lock);
970
971         chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
972         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
973                        SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
974         return 0;
975 }
976
977 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
978 {
979         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
980         struct snd_pcm_runtime *runtime = substream->runtime;
981         struct snd_ymfpci_pcm *ypcm;
982         int err;
983         
984         if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
985                 return err;
986         ypcm = runtime->private_data;
987         ypcm->output_front = 0;
988         ypcm->output_rear = 1;
989         ypcm->swap_rear = 0;
990         spin_lock_irq(&chip->reg_lock);
991         ymfpci_open_extension(chip);
992         chip->rear_opened++;
993         spin_unlock_irq(&chip->reg_lock);
994         return 0;
995 }
996
997 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
998                                    u32 capture_bank_number)
999 {
1000         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1001         struct snd_pcm_runtime *runtime = substream->runtime;
1002         struct snd_ymfpci_pcm *ypcm;
1003
1004         ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1005         if (ypcm == NULL)
1006                 return -ENOMEM;
1007         ypcm->chip = chip;
1008         ypcm->type = capture_bank_number + CAPTURE_REC;
1009         ypcm->substream = substream;    
1010         ypcm->capture_bank_number = capture_bank_number;
1011         chip->capture_substream[capture_bank_number] = substream;
1012         runtime->hw = snd_ymfpci_capture;
1013         /* FIXME? True value is 256/48 = 5.33333 ms */
1014         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
1015         runtime->private_data = ypcm;
1016         runtime->private_free = snd_ymfpci_pcm_free_substream;
1017         snd_ymfpci_hw_start(chip);
1018         return 0;
1019 }
1020
1021 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1022 {
1023         return snd_ymfpci_capture_open(substream, 0);
1024 }
1025
1026 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1027 {
1028         return snd_ymfpci_capture_open(substream, 1);
1029 }
1030
1031 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1032 {
1033         return 0;
1034 }
1035
1036 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1037 {
1038         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1039         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1040
1041         spin_lock_irq(&chip->reg_lock);
1042         if (ypcm->output_rear && chip->rear_opened > 0) {
1043                 chip->rear_opened--;
1044                 ymfpci_close_extension(chip);
1045         }
1046         spin_unlock_irq(&chip->reg_lock);
1047         return snd_ymfpci_playback_close_1(substream);
1048 }
1049
1050 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1051 {
1052         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1053
1054         spin_lock_irq(&chip->reg_lock);
1055         chip->spdif_opened = 0;
1056         ymfpci_close_extension(chip);
1057         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1058                           snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1059         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1060         spin_unlock_irq(&chip->reg_lock);
1061         chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1062         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1063                        SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1064         return snd_ymfpci_playback_close_1(substream);
1065 }
1066
1067 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1068 {
1069         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1070
1071         spin_lock_irq(&chip->reg_lock);
1072         if (chip->rear_opened > 0) {
1073                 chip->rear_opened--;
1074                 ymfpci_close_extension(chip);
1075         }
1076         spin_unlock_irq(&chip->reg_lock);
1077         return snd_ymfpci_playback_close_1(substream);
1078 }
1079
1080 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1081 {
1082         struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1083         struct snd_pcm_runtime *runtime = substream->runtime;
1084         struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1085
1086         if (ypcm != NULL) {
1087                 chip->capture_substream[ypcm->capture_bank_number] = NULL;
1088                 snd_ymfpci_hw_stop(chip);
1089         }
1090         return 0;
1091 }
1092
1093 static struct snd_pcm_ops snd_ymfpci_playback_ops = {
1094         .open =                 snd_ymfpci_playback_open,
1095         .close =                snd_ymfpci_playback_close,
1096         .ioctl =                snd_pcm_lib_ioctl,
1097         .hw_params =            snd_ymfpci_playback_hw_params,
1098         .hw_free =              snd_ymfpci_playback_hw_free,
1099         .prepare =              snd_ymfpci_playback_prepare,
1100         .trigger =              snd_ymfpci_playback_trigger,
1101         .pointer =              snd_ymfpci_playback_pointer,
1102 };
1103
1104 static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1105         .open =                 snd_ymfpci_capture_rec_open,
1106         .close =                snd_ymfpci_capture_close,
1107         .ioctl =                snd_pcm_lib_ioctl,
1108         .hw_params =            snd_ymfpci_capture_hw_params,
1109         .hw_free =              snd_ymfpci_capture_hw_free,
1110         .prepare =              snd_ymfpci_capture_prepare,
1111         .trigger =              snd_ymfpci_capture_trigger,
1112         .pointer =              snd_ymfpci_capture_pointer,
1113 };
1114
1115 int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1116 {
1117         struct snd_pcm *pcm;
1118         int err;
1119
1120         if (rpcm)
1121                 *rpcm = NULL;
1122         if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1123                 return err;
1124         pcm->private_data = chip;
1125
1126         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1127         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1128
1129         /* global setup */
1130         pcm->info_flags = 0;
1131         strcpy(pcm->name, "YMFPCI");
1132         chip->pcm = pcm;
1133
1134         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1135                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1136
1137         if (rpcm)
1138                 *rpcm = pcm;
1139         return 0;
1140 }
1141
1142 static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1143         .open =                 snd_ymfpci_capture_ac97_open,
1144         .close =                snd_ymfpci_capture_close,
1145         .ioctl =                snd_pcm_lib_ioctl,
1146         .hw_params =            snd_ymfpci_capture_hw_params,
1147         .hw_free =              snd_ymfpci_capture_hw_free,
1148         .prepare =              snd_ymfpci_capture_prepare,
1149         .trigger =              snd_ymfpci_capture_trigger,
1150         .pointer =              snd_ymfpci_capture_pointer,
1151 };
1152
1153 int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1154 {
1155         struct snd_pcm *pcm;
1156         int err;
1157
1158         if (rpcm)
1159                 *rpcm = NULL;
1160         if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1161                 return err;
1162         pcm->private_data = chip;
1163
1164         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1165
1166         /* global setup */
1167         pcm->info_flags = 0;
1168         sprintf(pcm->name, "YMFPCI - %s",
1169                 chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1170         chip->pcm2 = pcm;
1171
1172         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1173                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1174
1175         if (rpcm)
1176                 *rpcm = pcm;
1177         return 0;
1178 }
1179
1180 static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1181         .open =                 snd_ymfpci_playback_spdif_open,
1182         .close =                snd_ymfpci_playback_spdif_close,
1183         .ioctl =                snd_pcm_lib_ioctl,
1184         .hw_params =            snd_ymfpci_playback_hw_params,
1185         .hw_free =              snd_ymfpci_playback_hw_free,
1186         .prepare =              snd_ymfpci_playback_prepare,
1187         .trigger =              snd_ymfpci_playback_trigger,
1188         .pointer =              snd_ymfpci_playback_pointer,
1189 };
1190
1191 int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1192 {
1193         struct snd_pcm *pcm;
1194         int err;
1195
1196         if (rpcm)
1197                 *rpcm = NULL;
1198         if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1199                 return err;
1200         pcm->private_data = chip;
1201
1202         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1203
1204         /* global setup */
1205         pcm->info_flags = 0;
1206         strcpy(pcm->name, "YMFPCI - IEC958");
1207         chip->pcm_spdif = pcm;
1208
1209         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1210                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1211
1212         if (rpcm)
1213                 *rpcm = pcm;
1214         return 0;
1215 }
1216
1217 static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1218         .open =                 snd_ymfpci_playback_4ch_open,
1219         .close =                snd_ymfpci_playback_4ch_close,
1220         .ioctl =                snd_pcm_lib_ioctl,
1221         .hw_params =            snd_ymfpci_playback_hw_params,
1222         .hw_free =              snd_ymfpci_playback_hw_free,
1223         .prepare =              snd_ymfpci_playback_prepare,
1224         .trigger =              snd_ymfpci_playback_trigger,
1225         .pointer =              snd_ymfpci_playback_pointer,
1226 };
1227
1228 int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
1229 {
1230         struct snd_pcm *pcm;
1231         int err;
1232
1233         if (rpcm)
1234                 *rpcm = NULL;
1235         if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1236                 return err;
1237         pcm->private_data = chip;
1238
1239         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1240
1241         /* global setup */
1242         pcm->info_flags = 0;
1243         strcpy(pcm->name, "YMFPCI - Rear PCM");
1244         chip->pcm_4ch = pcm;
1245
1246         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1247                                               snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
1248
1249         if (rpcm)
1250                 *rpcm = pcm;
1251         return 0;
1252 }
1253
1254 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1255 {
1256         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1257         uinfo->count = 1;
1258         return 0;
1259 }
1260
1261 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1262                                         struct snd_ctl_elem_value *ucontrol)
1263 {
1264         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1265
1266         spin_lock_irq(&chip->reg_lock);
1267         ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1268         ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1269         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1270         spin_unlock_irq(&chip->reg_lock);
1271         return 0;
1272 }
1273
1274 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1275                                          struct snd_ctl_elem_value *ucontrol)
1276 {
1277         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1278         unsigned int val;
1279         int change;
1280
1281         val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1282               (ucontrol->value.iec958.status[1] << 8);
1283         spin_lock_irq(&chip->reg_lock);
1284         change = chip->spdif_bits != val;
1285         chip->spdif_bits = val;
1286         if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1287                 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1288         spin_unlock_irq(&chip->reg_lock);
1289         return change;
1290 }
1291
1292 static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
1293 {
1294         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1295         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1296         .info =         snd_ymfpci_spdif_default_info,
1297         .get =          snd_ymfpci_spdif_default_get,
1298         .put =          snd_ymfpci_spdif_default_put
1299 };
1300
1301 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1302 {
1303         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1304         uinfo->count = 1;
1305         return 0;
1306 }
1307
1308 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1309                                       struct snd_ctl_elem_value *ucontrol)
1310 {
1311         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1312
1313         spin_lock_irq(&chip->reg_lock);
1314         ucontrol->value.iec958.status[0] = 0x3e;
1315         ucontrol->value.iec958.status[1] = 0xff;
1316         spin_unlock_irq(&chip->reg_lock);
1317         return 0;
1318 }
1319
1320 static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
1321 {
1322         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1323         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1324         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1325         .info =         snd_ymfpci_spdif_mask_info,
1326         .get =          snd_ymfpci_spdif_mask_get,
1327 };
1328
1329 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1330 {
1331         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1332         uinfo->count = 1;
1333         return 0;
1334 }
1335
1336 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1337                                         struct snd_ctl_elem_value *ucontrol)
1338 {
1339         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1340
1341         spin_lock_irq(&chip->reg_lock);
1342         ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1343         ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1344         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1345         spin_unlock_irq(&chip->reg_lock);
1346         return 0;
1347 }
1348
1349 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1350                                         struct snd_ctl_elem_value *ucontrol)
1351 {
1352         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1353         unsigned int val;
1354         int change;
1355
1356         val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1357               (ucontrol->value.iec958.status[1] << 8);
1358         spin_lock_irq(&chip->reg_lock);
1359         change = chip->spdif_pcm_bits != val;
1360         chip->spdif_pcm_bits = val;
1361         if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1362                 snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1363         spin_unlock_irq(&chip->reg_lock);
1364         return change;
1365 }
1366
1367 static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
1368 {
1369         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1370         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1371         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1372         .info =         snd_ymfpci_spdif_stream_info,
1373         .get =          snd_ymfpci_spdif_stream_get,
1374         .put =          snd_ymfpci_spdif_stream_put
1375 };
1376
1377 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1378 {
1379         static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};
1380
1381         info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1382         info->count = 1;
1383         info->value.enumerated.items = 3;
1384         if (info->value.enumerated.item > 2)
1385                 info->value.enumerated.item = 2;
1386         strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
1387         return 0;
1388 }
1389
1390 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1391 {
1392         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1393         u16 reg;
1394
1395         spin_lock_irq(&chip->reg_lock);
1396         reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1397         spin_unlock_irq(&chip->reg_lock);
1398         if (!(reg & 0x100))
1399                 value->value.enumerated.item[0] = 0;
1400         else
1401                 value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1402         return 0;
1403 }
1404
1405 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1406 {
1407         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1408         u16 reg, old_reg;
1409
1410         spin_lock_irq(&chip->reg_lock);
1411         old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1412         if (value->value.enumerated.item[0] == 0)
1413                 reg = old_reg & ~0x100;
1414         else
1415                 reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1416         snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1417         spin_unlock_irq(&chip->reg_lock);
1418         return reg != old_reg;
1419 }
1420
1421 static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
1422         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE,
1423         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1424         .name =         "Direct Recording Source",
1425         .info =         snd_ymfpci_drec_source_info,
1426         .get =          snd_ymfpci_drec_source_get,
1427         .put =          snd_ymfpci_drec_source_put
1428 };
1429
1430 /*
1431  *  Mixer controls
1432  */
1433
1434 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1435 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1436   .info = snd_ymfpci_info_single, \
1437   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1438   .private_value = ((reg) | ((shift) << 16)) }
1439
1440 #define snd_ymfpci_info_single          snd_ctl_boolean_mono_info
1441
1442 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1443                                  struct snd_ctl_elem_value *ucontrol)
1444 {
1445         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1446         int reg = kcontrol->private_value & 0xffff;
1447         unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1448         unsigned int mask = 1;
1449         
1450         switch (reg) {
1451         case YDSXGR_SPDIFOUTCTRL: break;
1452         case YDSXGR_SPDIFINCTRL: break;
1453         default: return -EINVAL;
1454         }
1455         ucontrol->value.integer.value[0] =
1456                 (snd_ymfpci_readl(chip, reg) >> shift) & mask;
1457         return 0;
1458 }
1459
1460 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1461                                  struct snd_ctl_elem_value *ucontrol)
1462 {
1463         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1464         int reg = kcontrol->private_value & 0xffff;
1465         unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1466         unsigned int mask = 1;
1467         int change;
1468         unsigned int val, oval;
1469         
1470         switch (reg) {
1471         case YDSXGR_SPDIFOUTCTRL: break;
1472         case YDSXGR_SPDIFINCTRL: break;
1473         default: return -EINVAL;
1474         }
1475         val = (ucontrol->value.integer.value[0] & mask);
1476         val <<= shift;
1477         spin_lock_irq(&chip->reg_lock);
1478         oval = snd_ymfpci_readl(chip, reg);
1479         val = (oval & ~(mask << shift)) | val;
1480         change = val != oval;
1481         snd_ymfpci_writel(chip, reg, val);
1482         spin_unlock_irq(&chip->reg_lock);
1483         return change;
1484 }
1485
1486 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1487
1488 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1489 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1490   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1491   .info = snd_ymfpci_info_double, \
1492   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1493   .private_value = reg, \
1494   .tlv = { .p = db_scale_native } }
1495
1496 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1497 {
1498         unsigned int reg = kcontrol->private_value;
1499
1500         if (reg < 0x80 || reg >= 0xc0)
1501                 return -EINVAL;
1502         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1503         uinfo->count = 2;
1504         uinfo->value.integer.min = 0;
1505         uinfo->value.integer.max = 16383;
1506         return 0;
1507 }
1508
1509 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1510 {
1511         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1512         unsigned int reg = kcontrol->private_value;
1513         unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1514         unsigned int val;
1515         
1516         if (reg < 0x80 || reg >= 0xc0)
1517                 return -EINVAL;
1518         spin_lock_irq(&chip->reg_lock);
1519         val = snd_ymfpci_readl(chip, reg);
1520         spin_unlock_irq(&chip->reg_lock);
1521         ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1522         ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1523         return 0;
1524 }
1525
1526 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1527 {
1528         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1529         unsigned int reg = kcontrol->private_value;
1530         unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1531         int change;
1532         unsigned int val1, val2, oval;
1533         
1534         if (reg < 0x80 || reg >= 0xc0)
1535                 return -EINVAL;
1536         val1 = ucontrol->value.integer.value[0] & mask;
1537         val2 = ucontrol->value.integer.value[1] & mask;
1538         val1 <<= shift_left;
1539         val2 <<= shift_right;
1540         spin_lock_irq(&chip->reg_lock);
1541         oval = snd_ymfpci_readl(chip, reg);
1542         val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1543         change = val1 != oval;
1544         snd_ymfpci_writel(chip, reg, val1);
1545         spin_unlock_irq(&chip->reg_lock);
1546         return change;
1547 }
1548
1549 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1550                                        struct snd_ctl_elem_value *ucontrol)
1551 {
1552         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1553         unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1554         unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1555         int change;
1556         unsigned int value, oval;
1557         
1558         value = ucontrol->value.integer.value[0] & 0x3fff;
1559         value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1560         spin_lock_irq(&chip->reg_lock);
1561         oval = snd_ymfpci_readl(chip, reg);
1562         change = value != oval;
1563         snd_ymfpci_writel(chip, reg, value);
1564         snd_ymfpci_writel(chip, reg2, value);
1565         spin_unlock_irq(&chip->reg_lock);
1566         return change;
1567 }
1568
1569 /*
1570  * 4ch duplication
1571  */
1572 #define snd_ymfpci_info_dup4ch          snd_ctl_boolean_mono_info
1573
1574 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1575 {
1576         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1577         ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1578         return 0;
1579 }
1580
1581 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1582 {
1583         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1584         int change;
1585         change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1586         if (change)
1587                 chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1588         return change;
1589 }
1590
1591
1592 static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1593 {
1594         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1595         .name = "Wave Playback Volume",
1596         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1597                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1598         .info = snd_ymfpci_info_double,
1599         .get = snd_ymfpci_get_double,
1600         .put = snd_ymfpci_put_nativedacvol,
1601         .private_value = YDSXGR_NATIVEDACOUTVOL,
1602         .tlv = { .p = db_scale_native },
1603 },
1604 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1605 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1606 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1607 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1608 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1609 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1610 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1611 YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
1612 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1613 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1614 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1615 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1616 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1617 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1618 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1619 {
1620         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1621         .name = "4ch Duplication",
1622         .info = snd_ymfpci_info_dup4ch,
1623         .get = snd_ymfpci_get_dup4ch,
1624         .put = snd_ymfpci_put_dup4ch,
1625 },
1626 };
1627
1628
1629 /*
1630  * GPIO
1631  */
1632
1633 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1634 {
1635         u16 reg, mode;
1636         unsigned long flags;
1637
1638         spin_lock_irqsave(&chip->reg_lock, flags);
1639         reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1640         reg &= ~(1 << (pin + 8));
1641         reg |= (1 << pin);
1642         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1643         /* set the level mode for input line */
1644         mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1645         mode &= ~(3 << (pin * 2));
1646         snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1647         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1648         mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1649         spin_unlock_irqrestore(&chip->reg_lock, flags);
1650         return (mode >> pin) & 1;
1651 }
1652
1653 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1654 {
1655         u16 reg;
1656         unsigned long flags;
1657
1658         spin_lock_irqsave(&chip->reg_lock, flags);
1659         reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1660         reg &= ~(1 << pin);
1661         reg &= ~(1 << (pin + 8));
1662         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1663         snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1664         snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1665         spin_unlock_irqrestore(&chip->reg_lock, flags);
1666
1667         return 0;
1668 }
1669
1670 #define snd_ymfpci_gpio_sw_info         snd_ctl_boolean_mono_info
1671
1672 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1673 {
1674         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1675         int pin = (int)kcontrol->private_value;
1676         ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1677         return 0;
1678 }
1679
1680 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1681 {
1682         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1683         int pin = (int)kcontrol->private_value;
1684
1685         if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1686                 snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1687                 ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1688                 return 1;
1689         }
1690         return 0;
1691 }
1692
1693 static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
1694         .name = "Shared Rear/Line-In Switch",
1695         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1696         .info = snd_ymfpci_gpio_sw_info,
1697         .get = snd_ymfpci_gpio_sw_get,
1698         .put = snd_ymfpci_gpio_sw_put,
1699         .private_value = 2,
1700 };
1701
1702 /*
1703  * PCM voice volume
1704  */
1705
1706 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1707                                    struct snd_ctl_elem_info *uinfo)
1708 {
1709         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1710         uinfo->count = 2;
1711         uinfo->value.integer.min = 0;
1712         uinfo->value.integer.max = 0x8000;
1713         return 0;
1714 }
1715
1716 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1717                                   struct snd_ctl_elem_value *ucontrol)
1718 {
1719         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1720         unsigned int subs = kcontrol->id.subdevice;
1721
1722         ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1723         ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1724         return 0;
1725 }
1726
1727 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1728                                   struct snd_ctl_elem_value *ucontrol)
1729 {
1730         struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1731         unsigned int subs = kcontrol->id.subdevice;
1732         struct snd_pcm_substream *substream;
1733         unsigned long flags;
1734
1735         if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1736             ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1737                 chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1738                 chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1739
1740                 substream = (struct snd_pcm_substream *)kcontrol->private_value;
1741                 spin_lock_irqsave(&chip->voice_lock, flags);
1742                 if (substream->runtime && substream->runtime->private_data) {
1743                         struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1744                         if (!ypcm->use_441_slot)
1745                                 ypcm->update_pcm_vol = 2;
1746                 }
1747                 spin_unlock_irqrestore(&chip->voice_lock, flags);
1748                 return 1;
1749         }
1750         return 0;
1751 }
1752
1753 static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
1754         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1755         .name = "PCM Playback Volume",
1756         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1757                 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1758         .info = snd_ymfpci_pcm_vol_info,
1759         .get = snd_ymfpci_pcm_vol_get,
1760         .put = snd_ymfpci_pcm_vol_put,
1761 };
1762
1763
1764 /*
1765  *  Mixer routines
1766  */
1767
1768 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1769 {
1770         struct snd_ymfpci *chip = bus->private_data;
1771         chip->ac97_bus = NULL;
1772 }
1773
1774 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1775 {
1776         struct snd_ymfpci *chip = ac97->private_data;
1777         chip->ac97 = NULL;
1778 }
1779
1780 int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1781 {
1782         struct snd_ac97_template ac97;
1783         struct snd_kcontrol *kctl;
1784         struct snd_pcm_substream *substream;
1785         unsigned int idx;
1786         int err;
1787         static struct snd_ac97_bus_ops ops = {
1788                 .write = snd_ymfpci_codec_write,
1789                 .read = snd_ymfpci_codec_read,
1790         };
1791
1792         if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1793                 return err;
1794         chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1795         chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1796
1797         memset(&ac97, 0, sizeof(ac97));
1798         ac97.private_data = chip;
1799         ac97.private_free = snd_ymfpci_mixer_free_ac97;
1800         if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1801                 return err;
1802
1803         /* to be sure */
1804         snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1805                              AC97_EA_VRA|AC97_EA_VRM, 0);
1806
1807         for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1808                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1809                         return err;
1810         }
1811
1812         /* add S/PDIF control */
1813         snd_assert(chip->pcm_spdif != NULL, return -EIO);
1814         if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1815                 return err;
1816         kctl->id.device = chip->pcm_spdif->device;
1817         if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1818                 return err;
1819         kctl->id.device = chip->pcm_spdif->device;
1820         if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1821                 return err;
1822         kctl->id.device = chip->pcm_spdif->device;
1823         chip->spdif_pcm_ctl = kctl;
1824
1825         /* direct recording source */
1826         if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1827             (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1828                 return err;
1829
1830         /*
1831          * shared rear/line-in
1832          */
1833         if (rear_switch) {
1834                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1835                         return err;
1836         }
1837
1838         /* per-voice volume */
1839         substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1840         for (idx = 0; idx < 32; ++idx) {
1841                 kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1842                 if (!kctl)
1843                         return -ENOMEM;
1844                 kctl->id.device = chip->pcm->device;
1845                 kctl->id.subdevice = idx;
1846                 kctl->private_value = (unsigned long)substream;
1847                 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1848                         return err;
1849                 chip->pcm_mixer[idx].left = 0x8000;
1850                 chip->pcm_mixer[idx].right = 0x8000;
1851                 chip->pcm_mixer[idx].ctl = kctl;
1852                 substream = substream->next;
1853         }
1854
1855         return 0;
1856 }
1857
1858
1859 /*
1860  * timer
1861  */
1862
1863 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1864 {
1865         struct snd_ymfpci *chip;
1866         unsigned long flags;
1867         unsigned int count;
1868
1869         chip = snd_timer_chip(timer);
1870         count = (timer->sticks << 1) - 1;
1871         spin_lock_irqsave(&chip->reg_lock, flags);
1872         snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1873         snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1874         spin_unlock_irqrestore(&chip->reg_lock, flags);
1875         return 0;
1876 }
1877
1878 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1879 {
1880         struct snd_ymfpci *chip;
1881         unsigned long flags;
1882
1883         chip = snd_timer_chip(timer);
1884         spin_lock_irqsave(&chip->reg_lock, flags);
1885         snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1886         spin_unlock_irqrestore(&chip->reg_lock, flags);
1887         return 0;
1888 }
1889
1890 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1891                                                unsigned long *num, unsigned long *den)
1892 {
1893         *num = 1;
1894         *den = 48000;
1895         return 0;
1896 }
1897
1898 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1899         .flags = SNDRV_TIMER_HW_AUTO,
1900         .resolution = 20833, /* 1/fs = 20.8333...us */
1901         .ticks = 0x8000,
1902         .start = snd_ymfpci_timer_start,
1903         .stop = snd_ymfpci_timer_stop,
1904         .precise_resolution = snd_ymfpci_timer_precise_resolution,
1905 };
1906
1907 int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1908 {
1909         struct snd_timer *timer = NULL;
1910         struct snd_timer_id tid;
1911         int err;
1912
1913         tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1914         tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1915         tid.card = chip->card->number;
1916         tid.device = device;
1917         tid.subdevice = 0;
1918         if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1919                 strcpy(timer->name, "YMFPCI timer");
1920                 timer->private_data = chip;
1921                 timer->hw = snd_ymfpci_timer_hw;
1922         }
1923         chip->timer = timer;
1924         return err;
1925 }
1926
1927
1928 /*
1929  *  proc interface
1930  */
1931
1932 static void snd_ymfpci_proc_read(struct snd_info_entry *entry, 
1933                                  struct snd_info_buffer *buffer)
1934 {
1935         struct snd_ymfpci *chip = entry->private_data;
1936         int i;
1937         
1938         snd_iprintf(buffer, "YMFPCI\n\n");
1939         for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1940                 snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1941 }
1942
1943 static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1944 {
1945         struct snd_info_entry *entry;
1946         
1947         if (! snd_card_proc_new(card, "ymfpci", &entry))
1948                 snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
1949         return 0;
1950 }
1951
1952 /*
1953  *  initialization routines
1954  */
1955
1956 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1957 {
1958         u8 cmd;
1959
1960         pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1961 #if 0 // force to reset
1962         if (cmd & 0x03) {
1963 #endif
1964                 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1965                 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1966                 pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1967                 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1968                 pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1969 #if 0
1970         }
1971 #endif
1972 }
1973
1974 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1975 {
1976         snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1977 }
1978
1979 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1980 {
1981         u32 val;
1982         int timeout = 1000;
1983
1984         val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1985         if (val)
1986                 snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
1987         while (timeout-- > 0) {
1988                 val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
1989                 if ((val & 0x00000002) == 0)
1990                         break;
1991         }
1992 }
1993
1994 #ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
1995
1996 #include "ymfpci_image.h"
1997
1998 static struct firmware snd_ymfpci_dsp_microcode = {
1999         .size = YDSXG_DSPLENGTH,
2000         .data = (u8 *)DspInst,
2001 };
2002 static struct firmware snd_ymfpci_controller_microcode = {
2003         .size = YDSXG_CTRLLENGTH,
2004         .data = (u8 *)CntrlInst,
2005 };
2006 static struct firmware snd_ymfpci_controller_1e_microcode = {
2007         .size = YDSXG_CTRLLENGTH,
2008         .data = (u8 *)CntrlInst1E,
2009 };
2010 #endif
2011
2012 #ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2013 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2014 {
2015         chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
2016         if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2017             chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2018             chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2019             chip->device_id == PCI_DEVICE_ID_YAMAHA_754)
2020                 chip->controller_microcode =
2021                         &snd_ymfpci_controller_1e_microcode;
2022         else
2023                 chip->controller_microcode =
2024                         &snd_ymfpci_controller_microcode;
2025         return 0;
2026 }
2027
2028 #else /* use fw_loader */
2029
2030 #ifdef __LITTLE_ENDIAN
2031 static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
2032 #else
2033 static void snd_ymfpci_convert_from_le(const struct firmware *fw)
2034 {
2035         int i;
2036         u32 *data = (u32 *)fw->data;
2037
2038         for (i = 0; i < fw->size / 4; ++i)
2039                 le32_to_cpus(&data[i]);
2040 }
2041 #endif
2042
2043 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2044 {
2045         int err, is_1e;
2046         const char *name;
2047
2048         err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2049                                &chip->pci->dev);
2050         if (err >= 0) {
2051                 if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
2052                         snd_ymfpci_convert_from_le(chip->dsp_microcode);
2053                 else {
2054                         snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2055                         err = -EINVAL;
2056                 }
2057         }
2058         if (err < 0)
2059                 return err;
2060         is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2061                 chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2062                 chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2063                 chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2064         name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2065         err = request_firmware(&chip->controller_microcode, name,
2066                                &chip->pci->dev);
2067         if (err >= 0) {
2068                 if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
2069                         snd_ymfpci_convert_from_le(chip->controller_microcode);
2070                 else {
2071                         snd_printk(KERN_ERR "controller microcode"
2072                                    " has wrong size\n");
2073                         err = -EINVAL;
2074                 }
2075         }
2076         if (err < 0)
2077                 return err;
2078         return 0;
2079 }
2080
2081 MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2082 MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2083 MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2084
2085 #endif
2086
2087 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2088 {
2089         int i;
2090         u16 ctrl;
2091         u32 *inst;
2092
2093         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2094         snd_ymfpci_disable_dsp(chip);
2095         snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2096         snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2097         snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2098         snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2099         snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2100         snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2101         snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2102         ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2103         snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2104
2105         /* setup DSP instruction code */
2106         inst = (u32 *)chip->dsp_microcode->data;
2107         for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2108                 snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
2109
2110         /* setup control instruction code */
2111         inst = (u32 *)chip->controller_microcode->data;
2112         for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2113                 snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);
2114
2115         snd_ymfpci_enable_dsp(chip);
2116 }
2117
2118 static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2119 {
2120         long size, playback_ctrl_size;
2121         int voice, bank, reg;
2122         u8 *ptr;
2123         dma_addr_t ptr_addr;
2124
2125         playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2126         chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2127         chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2128         chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2129         chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2130         
2131         size = ALIGN(playback_ctrl_size, 0x100) +
2132                ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2133                ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2134                ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2135                chip->work_size;
2136         /* work_ptr must be aligned to 256 bytes, but it's already
2137            covered with the kernel page allocation mechanism */
2138         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2139                                 size, &chip->work_ptr) < 0) 
2140                 return -ENOMEM;
2141         ptr = chip->work_ptr.area;
2142         ptr_addr = chip->work_ptr.addr;
2143         memset(ptr, 0, size);   /* for sure */
2144
2145         chip->bank_base_playback = ptr;
2146         chip->bank_base_playback_addr = ptr_addr;
2147         chip->ctrl_playback = (u32 *)ptr;
2148         chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2149         ptr += ALIGN(playback_ctrl_size, 0x100);
2150         ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2151         for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2152                 chip->voices[voice].number = voice;
2153                 chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2154                 chip->voices[voice].bank_addr = ptr_addr;
2155                 for (bank = 0; bank < 2; bank++) {
2156                         chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2157                         ptr += chip->bank_size_playback;
2158                         ptr_addr += chip->bank_size_playback;
2159                 }
2160         }
2161         ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2162         ptr_addr = ALIGN(ptr_addr, 0x100);
2163         chip->bank_base_capture = ptr;
2164         chip->bank_base_capture_addr = ptr_addr;
2165         for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2166                 for (bank = 0; bank < 2; bank++) {
2167                         chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2168                         ptr += chip->bank_size_capture;
2169                         ptr_addr += chip->bank_size_capture;
2170                 }
2171         ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2172         ptr_addr = ALIGN(ptr_addr, 0x100);
2173         chip->bank_base_effect = ptr;
2174         chip->bank_base_effect_addr = ptr_addr;
2175         for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2176                 for (bank = 0; bank < 2; bank++) {
2177                         chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2178                         ptr += chip->bank_size_effect;
2179                         ptr_addr += chip->bank_size_effect;
2180                 }
2181         ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2182         ptr_addr = ALIGN(ptr_addr, 0x100);
2183         chip->work_base = ptr;
2184         chip->work_base_addr = ptr_addr;
2185         
2186         snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );
2187
2188         snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2189         snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2190         snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2191         snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2192         snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2193
2194         /* S/PDIF output initialization */
2195         chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2196         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2197         snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2198
2199         /* S/PDIF input initialization */
2200         snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2201
2202         /* digital mixer setup */
2203         for (reg = 0x80; reg < 0xc0; reg += 4)
2204                 snd_ymfpci_writel(chip, reg, 0);
2205         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2206         snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2207         snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2208         snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2209         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2210         snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2211         snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2212         
2213         return 0;
2214 }
2215
2216 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2217 {
2218         u16 ctrl;
2219
2220         snd_assert(chip != NULL, return -EINVAL);
2221
2222         if (chip->res_reg_area) {       /* don't touch busy hardware */
2223                 snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2224                 snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2225                 snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2226                 snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2227                 snd_ymfpci_disable_dsp(chip);
2228                 snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2229                 snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2230                 snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2231                 snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2232                 snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2233                 ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2234                 snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2235         }
2236
2237         snd_ymfpci_ac3_done(chip);
2238
2239         /* Set PCI device to D3 state */
2240 #if 0
2241         /* FIXME: temporarily disabled, otherwise we cannot fire up
2242          * the chip again unless reboot.  ACPI bug?
2243          */
2244         pci_set_power_state(chip->pci, 3);
2245 #endif
2246
2247 #ifdef CONFIG_PM
2248         vfree(chip->saved_regs);
2249 #endif
2250         release_and_free_resource(chip->mpu_res);
2251         release_and_free_resource(chip->fm_res);
2252         snd_ymfpci_free_gameport(chip);
2253         if (chip->reg_area_virt)
2254                 iounmap(chip->reg_area_virt);
2255         if (chip->work_ptr.area)
2256                 snd_dma_free_pages(&chip->work_ptr);
2257         
2258         if (chip->irq >= 0)
2259                 free_irq(chip->irq, chip);
2260         release_and_free_resource(chip->res_reg_area);
2261
2262         pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2263         
2264         pci_disable_device(chip->pci);
2265 #ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
2266         release_firmware(chip->dsp_microcode);
2267         release_firmware(chip->controller_microcode);
2268 #endif
2269         kfree(chip);
2270         return 0;
2271 }
2272
2273 static int snd_ymfpci_dev_free(struct snd_device *device)
2274 {
2275         struct snd_ymfpci *chip = device->device_data;
2276         return snd_ymfpci_free(chip);
2277 }
2278
2279 #ifdef CONFIG_PM
2280 static int saved_regs_index[] = {
2281         /* spdif */
2282         YDSXGR_SPDIFOUTCTRL,
2283         YDSXGR_SPDIFOUTSTATUS,
2284         YDSXGR_SPDIFINCTRL,
2285         /* volumes */
2286         YDSXGR_PRIADCLOOPVOL,
2287         YDSXGR_NATIVEDACINVOL,
2288         YDSXGR_NATIVEDACOUTVOL,
2289         YDSXGR_BUF441OUTVOL,
2290         YDSXGR_NATIVEADCINVOL,
2291         YDSXGR_SPDIFLOOPVOL,
2292         YDSXGR_SPDIFOUTVOL,
2293         YDSXGR_ZVOUTVOL,
2294         YDSXGR_LEGACYOUTVOL,
2295         /* address bases */
2296         YDSXGR_PLAYCTRLBASE,
2297         YDSXGR_RECCTRLBASE,
2298         YDSXGR_EFFCTRLBASE,
2299         YDSXGR_WORKBASE,
2300         /* capture set up */
2301         YDSXGR_MAPOFREC,
2302         YDSXGR_RECFORMAT,
2303         YDSXGR_RECSLOTSR,
2304         YDSXGR_ADCFORMAT,
2305         YDSXGR_ADCSLOTSR,
2306 };
2307 #define YDSXGR_NUM_SAVED_REGS   ARRAY_SIZE(saved_regs_index)
2308
2309 int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
2310 {
2311         struct snd_card *card = pci_get_drvdata(pci);
2312         struct snd_ymfpci *chip = card->private_data;
2313         unsigned int i;
2314         
2315         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2316         snd_pcm_suspend_all(chip->pcm);
2317         snd_pcm_suspend_all(chip->pcm2);
2318         snd_pcm_suspend_all(chip->pcm_spdif);
2319         snd_pcm_suspend_all(chip->pcm_4ch);
2320         snd_ac97_suspend(chip->ac97);
2321         for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2322                 chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2323         chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2324         snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2325         snd_ymfpci_disable_dsp(chip);
2326         pci_disable_device(pci);
2327         pci_save_state(pci);
2328         pci_set_power_state(pci, pci_choose_state(pci, state));
2329         return 0;
2330 }
2331
2332 int snd_ymfpci_resume(struct pci_dev *pci)
2333 {
2334         struct snd_card *card = pci_get_drvdata(pci);
2335         struct snd_ymfpci *chip = card->private_data;
2336         unsigned int i;
2337
2338         pci_set_power_state(pci, PCI_D0);
2339         pci_restore_state(pci);
2340         if (pci_enable_device(pci) < 0) {
2341                 printk(KERN_ERR "ymfpci: pci_enable_device failed, "
2342                        "disabling device\n");
2343                 snd_card_disconnect(card);
2344                 return -EIO;
2345         }
2346         pci_set_master(pci);
2347         snd_ymfpci_aclink_reset(pci);
2348         snd_ymfpci_codec_ready(chip, 0);
2349         snd_ymfpci_download_image(chip);
2350         udelay(100);
2351
2352         for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2353                 snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2354
2355         snd_ac97_resume(chip->ac97);
2356
2357         /* start hw again */
2358         if (chip->start_count > 0) {
2359                 spin_lock_irq(&chip->reg_lock);
2360                 snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2361                 chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2362                 spin_unlock_irq(&chip->reg_lock);
2363         }
2364         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2365         return 0;
2366 }
2367 #endif /* CONFIG_PM */
2368
2369 int __devinit snd_ymfpci_create(struct snd_card *card,
2370                                 struct pci_dev * pci,
2371                                 unsigned short old_legacy_ctrl,
2372                                 struct snd_ymfpci ** rchip)
2373 {
2374         struct snd_ymfpci *chip;
2375         int err;
2376         static struct snd_device_ops ops = {
2377                 .dev_free =     snd_ymfpci_dev_free,
2378         };
2379         
2380         *rchip = NULL;
2381
2382         /* enable PCI device */
2383         if ((err = pci_enable_device(pci)) < 0)
2384                 return err;
2385
2386         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2387         if (chip == NULL) {
2388                 pci_disable_device(pci);
2389                 return -ENOMEM;
2390         }
2391         chip->old_legacy_ctrl = old_legacy_ctrl;
2392         spin_lock_init(&chip->reg_lock);
2393         spin_lock_init(&chip->voice_lock);
2394         init_waitqueue_head(&chip->interrupt_sleep);
2395         atomic_set(&chip->interrupt_sleep_count, 0);
2396         chip->card = card;
2397         chip->pci = pci;
2398         chip->irq = -1;
2399         chip->device_id = pci->device;
2400         chip->rev = pci->revision;
2401         chip->reg_area_phys = pci_resource_start(pci, 0);
2402         chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2403         pci_set_master(pci);
2404         chip->src441_used = -1;
2405
2406         if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2407                 snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2408                 snd_ymfpci_free(chip);
2409                 return -EBUSY;
2410         }
2411         if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2412                         "YMFPCI", chip)) {
2413                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2414                 snd_ymfpci_free(chip);
2415                 return -EBUSY;
2416         }
2417         chip->irq = pci->irq;
2418
2419         snd_ymfpci_aclink_reset(pci);
2420         if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2421                 snd_ymfpci_free(chip);
2422                 return -EIO;
2423         }
2424
2425         err = snd_ymfpci_request_firmware(chip);
2426         if (err < 0) {
2427                 snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2428                 snd_ymfpci_free(chip);
2429                 return err;
2430         }
2431         snd_ymfpci_download_image(chip);
2432
2433         udelay(100); /* seems we need a delay after downloading image.. */
2434
2435         if (snd_ymfpci_memalloc(chip) < 0) {
2436                 snd_ymfpci_free(chip);
2437                 return -EIO;
2438         }
2439
2440         if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
2441                 snd_ymfpci_free(chip);
2442                 return err;
2443         }
2444
2445 #ifdef CONFIG_PM
2446         chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
2447         if (chip->saved_regs == NULL) {
2448                 snd_ymfpci_free(chip);
2449                 return -ENOMEM;
2450         }
2451 #endif
2452
2453         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2454                 snd_ymfpci_free(chip);
2455                 return err;
2456         }
2457
2458         snd_ymfpci_proc_init(card, chip);
2459
2460         snd_card_set_dev(card, &pci->dev);
2461
2462         *rchip = chip;
2463         return 0;
2464 }