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