File: /usr/src/linux/drivers/sound/ymfpci.c

1     /*
2      *  Copyright 1999 Jaroslav Kysela <perex@suse.cz>
3      *  Copyright 2000 Alan Cox <alan@redhat.com>
4      *
5      *  Yamaha YMF7xx driver.
6      *
7      *  This code is a result of high-speed collision
8      *  between ymfpci.c of ALSA and cs46xx.c of Linux.
9      *  -- Pete Zaitcev <zaitcev@yahoo.com>; 2000/09/18
10      *
11      *   This program is free software; you can redistribute it and/or modify
12      *   it under the terms of the GNU General Public License as published by
13      *   the Free Software Foundation; either version 2 of the License, or
14      *   (at your option) any later version.
15      *
16      *   This program is distributed in the hope that it will be useful,
17      *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18      *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19      *   GNU General Public License for more details.
20      *
21      *   You should have received a copy of the GNU General Public License
22      *   along with this program; if not, write to the Free Software
23      *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24      *
25      * TODO:
26      *  - Use P44Slot for 44.1 playback (beware of idle buzzing in P44Slot).
27      *  - 96KHz playback for DVD - use pitch of 2.0.
28      *  - Retain DMA buffer on close, do not wait the end of frame.
29      *  - Resolve XXX tagged questions.
30      *  - Cannot play 5133Hz.
31      *  - 2001/01/07 Consider if we can remove voice_lock, like so:
32      *     : Allocate/deallocate voices in open/close under semafore.
33      *     : We access voices in interrupt, that only for pcms that open.
34      *    voice_lock around playback_prepare closes interrupts for insane duration.
35      *  - Revisit the way voice_alloc is done - too confusing, overcomplicated.
36      *    Should support various channel types, however.
37      *  - Remove prog_dmabuf from read/write, leave it in open.
38      *  - 2001/01/07 Replace the OPL3 part of CONFIG_SOUND_YMFPCI_LEGACY code with
39      *    native synthesizer through a playback slot.
40      *  - Use new 2.3.x cache coherent PCI DMA routines instead of virt_to_bus.
41      *  - Make the thing big endian compatible. ALSA has it done.
42      */
43     
44     #include <linux/config.h>
45     #include <linux/module.h>
46     #include <linux/init.h>
47     #include <linux/ioport.h>
48     #include <linux/pci.h>
49     #include <linux/slab.h>
50     #include <linux/poll.h>
51     #include <linux/soundcard.h>
52     #include <linux/ac97_codec.h>
53     #include <linux/sound.h>
54     
55     #include <asm/io.h>
56     #include <asm/dma.h>
57     #include <asm/uaccess.h>
58     
59     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
60     # include "sound_config.h"
61     # include "mpu401.h"
62     #endif
63     #include "ymfpci.h"
64     
65     /*
66      * I do not believe in debug levels as I never can guess what
67      * part of the code is going to be problematic in the future.
68      * Don't forget to run your klogd with -c 8.
69      */
70     /* #define YMFDBG(fmt, arg...)  do{ printk(KERN_DEBUG fmt, ##arg); }while(0) */
71     #define YMFDBGW(fmt, arg...)  /* */
72     #define YMFDBGI(fmt, arg...)  /* */
73     
74     static int ymf_playback_trigger(ymfpci_t *unit, struct ymf_pcm *ypcm, int cmd);
75     static void ymf_capture_trigger(ymfpci_t *unit, struct ymf_pcm *ypcm, int cmd);
76     static void ymfpci_voice_free(ymfpci_t *unit, ymfpci_voice_t *pvoice);
77     static int ymf_capture_alloc(struct ymf_unit *unit, int *pbank);
78     static int ymf_playback_prepare(struct ymf_state *state);
79     static int ymf_capture_prepare(struct ymf_state *state);
80     static struct ymf_state *ymf_state_alloc(ymfpci_t *unit);
81     
82     static void ymfpci_aclink_reset(struct pci_dev * pci);
83     static void ymfpci_disable_dsp(ymfpci_t *unit);
84     static void ymfpci_download_image(ymfpci_t *codec);
85     static void ymf_memload(ymfpci_t *unit);
86     
87     static LIST_HEAD(ymf_devs);
88     
89     /*
90      *  constants
91      */
92     
93     static struct pci_device_id ymf_id_tbl[] __devinitdata = {
94     #define DEV(v, d, data) \
95       { PCI_VENDOR_ID_##v, PCI_DEVICE_ID_##v##_##d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)data }
96     	DEV (YAMAHA, 724,  "YMF724"),
97     	DEV (YAMAHA, 724F, "YMF724F"),
98     	DEV (YAMAHA, 740,  "YMF740"),
99     	DEV (YAMAHA, 740C, "YMF740C"),
100     	DEV (YAMAHA, 744,  "YMF744"),
101     	DEV (YAMAHA, 754,  "YMF754"),
102     #undef DEV
103     	{ }
104     };
105     MODULE_DEVICE_TABLE(pci, ymf_id_tbl);
106     
107     /*
108      *  common I/O routines
109      */
110     
111     static inline u8 ymfpci_readb(ymfpci_t *codec, u32 offset)
112     {
113     	return readb(codec->reg_area_virt + offset);
114     }
115     
116     static inline void ymfpci_writeb(ymfpci_t *codec, u32 offset, u8 val)
117     {
118     	writeb(val, codec->reg_area_virt + offset);
119     }
120     
121     static inline u16 ymfpci_readw(ymfpci_t *codec, u32 offset)
122     {
123     	return readw(codec->reg_area_virt + offset);
124     }
125     
126     static inline void ymfpci_writew(ymfpci_t *codec, u32 offset, u16 val)
127     {
128     	writew(val, codec->reg_area_virt + offset);
129     }
130     
131     static inline u32 ymfpci_readl(ymfpci_t *codec, u32 offset)
132     {
133     	return readl(codec->reg_area_virt + offset);
134     }
135     
136     static inline void ymfpci_writel(ymfpci_t *codec, u32 offset, u32 val)
137     {
138     	writel(val, codec->reg_area_virt + offset);
139     }
140     
141     static int ymfpci_codec_ready(ymfpci_t *codec, int secondary, int sched)
142     {
143     	signed long end_time;
144     	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
145     	
146     	end_time = jiffies + 3 * (HZ / 4);
147     	do {
148     		if ((ymfpci_readw(codec, reg) & 0x8000) == 0)
149     			return 0;
150     		if (sched) {
151     			set_current_state(TASK_UNINTERRUPTIBLE);
152     			schedule_timeout(1);
153     		}
154     	} while (end_time - (signed long)jiffies >= 0);
155     	printk("ymfpci_codec_ready: codec %i is not ready [0x%x]\n",
156     	    secondary, ymfpci_readw(codec, reg));
157     	return -EBUSY;
158     }
159     
160     static void ymfpci_codec_write(struct ac97_codec *dev, u8 reg, u16 val)
161     {
162     	ymfpci_t *codec = dev->private_data;
163     	u32 cmd;
164     
165     	/* XXX Do make use of dev->id */
166     	ymfpci_codec_ready(codec, 0, 0);
167     	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
168     	ymfpci_writel(codec, YDSXGR_AC97CMDDATA, cmd);
169     }
170     
171     static u16 ymfpci_codec_read(struct ac97_codec *dev, u8 reg)
172     {
173     	ymfpci_t *codec = dev->private_data;
174     
175     	if (ymfpci_codec_ready(codec, 0, 0))
176     		return ~0;
177     	ymfpci_writew(codec, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
178     	if (ymfpci_codec_ready(codec, 0, 0))
179     		return ~0;
180     	if (codec->pci->device == PCI_DEVICE_ID_YAMAHA_744 && codec->rev < 2) {
181     		int i;
182     		for (i = 0; i < 600; i++)
183     			ymfpci_readw(codec, YDSXGR_PRISTATUSDATA);
184     	}
185     	return ymfpci_readw(codec, YDSXGR_PRISTATUSDATA);
186     }
187     
188     /*
189      *  Misc routines
190      */
191     
192     /*
193      * Calculate the actual sampling rate relatetively to the base clock (48kHz).
194      */
195     static u32 ymfpci_calc_delta(u32 rate)
196     {
197     	switch (rate) {
198     	case 8000:	return 0x02aaab00;
199     	case 11025:	return 0x03accd00;
200     	case 16000:	return 0x05555500;
201     	case 22050:	return 0x07599a00;
202     	case 32000:	return 0x0aaaab00;
203     	case 44100:	return 0x0eb33300;
204     	default:	return ((rate << 16) / 48000) << 12;
205     	}
206     }
207     
208     static u32 def_rate[8] = {
209     	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
210     };
211     
212     static u32 ymfpci_calc_lpfK(u32 rate)
213     {
214     	u32 i;
215     	static u32 val[8] = {
216     		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
217     		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
218     	};
219     	
220     	if (rate == 44100)
221     		return 0x40000000;	/* FIXME: What's the right value? */
222     	for (i = 0; i < 8; i++)
223     		if (rate <= def_rate[i])
224     			return val[i];
225     	return val[0];
226     }
227     
228     static u32 ymfpci_calc_lpfQ(u32 rate)
229     {
230     	u32 i;
231     	static u32 val[8] = {
232     		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
233     		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
234     	};
235     	
236     	if (rate == 44100)
237     		return 0x370A0000;
238     	for (i = 0; i < 8; i++)
239     		if (rate <= def_rate[i])
240     			return val[i];
241     	return val[0];
242     }
243     
244     static u32 ymf_calc_lend(u32 rate)
245     {
246     	return (rate * YMF_SAMPF) / 48000;
247     }
248     
249     /*
250      * We ever allow only a few formats, but let's be generic, for smaller surprise.
251      */
252     static int ymf_pcm_format_width(int format)
253     {
254     	static int mask16 = AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE;
255     
256     	if ((format & (format-1)) != 0) {
257     		printk(KERN_ERR "ymfpci: format 0x%x is not a power of 2\n", format);
258     		return 8;
259     	}
260     
261     	if (format == AFMT_IMA_ADPCM) return 4;
262     	if ((format & mask16) != 0) return 16;
263     	return 8;
264     }
265     
266     static void ymf_pcm_update_shift(struct ymf_pcm_format *f)
267     {
268     	f->shift = 0;
269     	if (f->voices == 2)
270     		f->shift++;
271     	if (ymf_pcm_format_width(f->format) == 16)
272     		f->shift++;
273     }
274     
275     /* Are you sure 32K is not too much? See if mpg123 skips on loaded systems. */
276     #define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
277     #define DMABUF_MINORDER 1
278     
279     /* allocate DMA buffer, playback and recording buffer should be allocated seperately */
280     static int alloc_dmabuf(struct ymf_dmabuf *dmabuf)
281     {
282     	void *rawbuf = NULL;
283     	int order;
284     	struct page * map,  * mapend;
285     
286     	/* alloc as big a chunk as we can */
287     	for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
288     		if((rawbuf = (void *)__get_free_pages(GFP_KERNEL|GFP_DMA, order)))
289     			break;
290     
291     	if (!rawbuf)
292     		return -ENOMEM;
293     
294     #if 0
295     	printk(KERN_DEBUG "ymfpci: allocated %ld (order = %d) bytes at %p\n",
296     	       PAGE_SIZE << order, order, rawbuf);
297     #endif
298     
299     	dmabuf->ready  = dmabuf->mapped = 0;
300     	dmabuf->rawbuf = rawbuf;
301     	dmabuf->buforder = order;
302     
303     	/* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
304     	mapend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
305     	for (map = virt_to_page(rawbuf); map <= mapend; map++)
306     		set_bit(PG_reserved, &map->flags);
307     
308     	return 0;
309     }
310     
311     /* free DMA buffer */
312     static void dealloc_dmabuf(struct ymf_dmabuf *dmabuf)
313     {
314     	struct page *map, *mapend;
315     
316     	if (dmabuf->rawbuf) {
317     		/* undo marking the pages as reserved */
318     		mapend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
319     		for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
320     			clear_bit(PG_reserved, &map->flags);
321     		free_pages((unsigned long)dmabuf->rawbuf,dmabuf->buforder);
322     	}
323     	dmabuf->rawbuf = NULL;
324     	dmabuf->mapped = dmabuf->ready = 0;
325     }
326     
327     static int prog_dmabuf(struct ymf_state *state, int rec)
328     {
329     	struct ymf_dmabuf *dmabuf;
330     	int w_16;
331     	unsigned bufsize;
332     	unsigned long flags;
333     	int redzone;
334     	int ret;
335     
336     	w_16 = ymf_pcm_format_width(state->format.format) == 16;
337     	dmabuf = rec ? &state->rpcm.dmabuf : &state->wpcm.dmabuf;
338     
339     	spin_lock_irqsave(&state->unit->reg_lock, flags);
340     	dmabuf->hwptr = dmabuf->swptr = 0;
341     	dmabuf->total_bytes = 0;
342     	dmabuf->count = 0;
343     	spin_unlock_irqrestore(&state->unit->reg_lock, flags);
344     
345     	/* allocate DMA buffer if not allocated yet */
346     	if (!dmabuf->rawbuf)
347     		if ((ret = alloc_dmabuf(dmabuf)))
348     			return ret;
349     
350     	/*
351     	 * Create fake fragment sizes and numbers for OSS ioctls.
352     	 * Import what Doom might have set with SNDCTL_DSP_SETFRAGMENT.
353     	 */
354     	bufsize = PAGE_SIZE << dmabuf->buforder;
355     	/* lets hand out reasonable big ass buffers by default */
356     	dmabuf->fragshift = (dmabuf->buforder + PAGE_SHIFT -2);
357     	if (dmabuf->ossfragshift > 3 &&
358     	    dmabuf->ossfragshift < dmabuf->fragshift) {
359     		dmabuf->fragshift = dmabuf->ossfragshift;
360     	}
361     	dmabuf->numfrag = bufsize >> dmabuf->fragshift;
362     	while (dmabuf->numfrag < 4 && dmabuf->fragshift > 3) {
363     		dmabuf->fragshift--;
364     		dmabuf->numfrag = bufsize >> dmabuf->fragshift;
365     	}
366     	dmabuf->fragsize = 1 << dmabuf->fragshift;
367     	dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
368     
369     	if (dmabuf->ossmaxfrags >= 2 && dmabuf->ossmaxfrags < dmabuf->numfrag) {
370     		dmabuf->numfrag = dmabuf->ossmaxfrags;
371     		dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
372     
373     		redzone = ymf_calc_lend(state->format.rate);
374     		redzone <<= (state->format.shift + 1);
375     		if (dmabuf->dmasize < redzone*3) {
376     			/*
377     			 * The driver works correctly with minimum dmasize
378     			 * of redzone*2, but it produces stoppage and clicks.
379     			 * So, make it little larger for smoother sound.
380     			 * XXX Make dmasize a wholy divisible by fragsize.
381     			 */
382     //			printk(KERN_ERR "ymfpci: dmasize=%d < redzone=%d * 3\n",
383     //			    dmabuf->dmasize, redzone);
384     			dmabuf->dmasize = redzone*3;
385     		}
386     	}
387     
388     	memset(dmabuf->rawbuf, w_16 ? 0 : 0x80, dmabuf->dmasize);
389     
390     	/*
391     	 *	Now set up the ring 
392     	 */
393     
394     	/* XXX   ret = rec? cap_pre(): pbk_pre();  */
395     	spin_lock_irqsave(&state->unit->voice_lock, flags);
396     	if (rec) {
397     		if ((ret = ymf_capture_prepare(state)) != 0) {
398     			spin_unlock_irqrestore(&state->unit->voice_lock, flags);
399     			return ret;
400     		}
401     	} else {
402     		if ((ret = ymf_playback_prepare(state)) != 0) {
403     			spin_unlock_irqrestore(&state->unit->voice_lock, flags);
404     			return ret;
405     		}
406     	}
407     	spin_unlock_irqrestore(&state->unit->voice_lock, flags);
408     
409     	/* set the ready flag for the dma buffer (this comment is not stupid) */
410     	dmabuf->ready = 1;
411     
412     #if 0
413     	printk("prog_dmabuf: rate %d format 0x%x,"
414     	    " numfrag %d fragsize %d dmasize %d\n",
415     	       state->format.rate, state->format.format, dmabuf->numfrag,
416     	       dmabuf->fragsize, dmabuf->dmasize);
417     #endif
418     
419     	return 0;
420     }
421     
422     static void ymf_start_dac(struct ymf_state *state)
423     {
424     	ymf_playback_trigger(state->unit, &state->wpcm, 1);
425     }
426     
427     // static void ymf_start_adc(struct ymf_state *state)
428     // {
429     // 	ymf_capture_trigger(state->unit, &state->rpcm, 1);
430     // }
431     
432     /*
433      * Wait until output is drained.
434      * This does not kill the hardware for the sake of ioctls.
435      */
436     static void ymf_wait_dac(struct ymf_state *state)
437     {
438     	struct ymf_unit *unit = state->unit;
439     	struct ymf_pcm *ypcm = &state->wpcm;
440     	DECLARE_WAITQUEUE(waita, current);
441     	unsigned long flags;
442     
443     	add_wait_queue(&ypcm->dmabuf.wait, &waita);
444     
445     	spin_lock_irqsave(&unit->reg_lock, flags);
446     	if (ypcm->dmabuf.count != 0 && !ypcm->running) {
447     		ymf_playback_trigger(unit, ypcm, 1);
448     	}
449     
450     #if 0
451     	if (file->f_flags & O_NONBLOCK) {
452     		/*
453     		 * XXX Our  mistake is to attach DMA buffer to state
454     		 * rather than to some per-device structure.
455     		 * Cannot skip waiting, can only make it shorter.
456     		 */
457     	}
458     #endif
459     
460     	set_current_state(TASK_UNINTERRUPTIBLE);
461     	while (ypcm->running) {
462     		spin_unlock_irqrestore(&unit->reg_lock, flags);
463     		schedule();
464     		spin_lock_irqsave(&unit->reg_lock, flags);
465     		set_current_state(TASK_UNINTERRUPTIBLE);
466     	}
467     	spin_unlock_irqrestore(&unit->reg_lock, flags);
468     
469     	set_current_state(TASK_RUNNING);
470     	remove_wait_queue(&ypcm->dmabuf.wait, &waita);
471     
472     	/*
473     	 * This function may take up to 4 seconds to reach this point
474     	 * (32K circular buffer, 8000 Hz). User notices.
475     	 */
476     }
477     
478     /* Can just stop, without wait. Or can we? */
479     static void ymf_stop_adc(struct ymf_state *state)
480     {
481     	struct ymf_unit *unit = state->unit;
482     	unsigned long flags;
483     
484     	spin_lock_irqsave(&unit->reg_lock, flags);
485     	ymf_capture_trigger(unit, &state->rpcm, 0);
486     	spin_unlock_irqrestore(&unit->reg_lock, flags);
487     }
488     
489     /*
490      *  Hardware start management
491      */
492     
493     static void ymfpci_hw_start(ymfpci_t *unit)
494     {
495     	unsigned long flags;
496     
497     	spin_lock_irqsave(&unit->reg_lock, flags);
498     	if (unit->start_count++ == 0) {
499     		ymfpci_writel(unit, YDSXGR_MODE,
500     		    ymfpci_readl(unit, YDSXGR_MODE) | 3);
501     		unit->active_bank = ymfpci_readl(unit, YDSXGR_CTRLSELECT) & 1;
502     	}
503     	spin_unlock_irqrestore(&unit->reg_lock, flags);
504     }
505     
506     static void ymfpci_hw_stop(ymfpci_t *unit)
507     {
508     	unsigned long flags;
509     	long timeout = 1000;
510     
511     	spin_lock_irqsave(&unit->reg_lock, flags);
512     	if (--unit->start_count == 0) {
513     		ymfpci_writel(unit, YDSXGR_MODE,
514     		    ymfpci_readl(unit, YDSXGR_MODE) & ~3);
515     		while (timeout-- > 0) {
516     			if ((ymfpci_readl(unit, YDSXGR_STATUS) & 2) == 0)
517     				break;
518     		}
519     	}
520     	spin_unlock_irqrestore(&unit->reg_lock, flags);
521     }
522     
523     /*
524      *  Playback voice management
525      */
526     
527     static int voice_alloc(ymfpci_t *codec, ymfpci_voice_type_t type, int pair, ymfpci_voice_t *rvoice[])
528     {
529     	ymfpci_voice_t *voice, *voice2;
530     	int idx;
531     
532     	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
533     		voice = &codec->voices[idx];
534     		voice2 = pair ? &codec->voices[idx+1] : NULL;
535     		if (voice->use || (voice2 && voice2->use))
536     			continue;
537     		voice->use = 1;
538     		if (voice2)
539     			voice2->use = 1;
540     		switch (type) {
541     		case YMFPCI_PCM:
542     			voice->pcm = 1;
543     			if (voice2)
544     				voice2->pcm = 1;
545     			break;
546     		case YMFPCI_SYNTH:
547     			voice->synth = 1;
548     			break;
549     		case YMFPCI_MIDI:
550     			voice->midi = 1;
551     			break;
552     		}
553     		ymfpci_hw_start(codec);
554     		rvoice[0] = voice;
555     		if (voice2) {
556     			ymfpci_hw_start(codec);
557     			rvoice[1] = voice2;
558     		}
559     		return 0;
560     	}
561     	return -EBUSY;	/* Your audio channel is open by someone else. */
562     }
563     
564     static void ymfpci_voice_free(ymfpci_t *unit, ymfpci_voice_t *pvoice)
565     {
566     	ymfpci_hw_stop(unit);
567     	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
568     	pvoice->ypcm = NULL;
569     }
570     
571     /*
572      */
573     
574     static void ymf_pcm_interrupt(ymfpci_t *codec, ymfpci_voice_t *voice)
575     {
576     	struct ymf_pcm *ypcm;
577     	int redzone;
578     	int pos, delta, swptr;
579     	int played, distance;
580     	struct ymf_state *state;
581     	struct ymf_dmabuf *dmabuf;
582     	char silence;
583     
584     	if ((ypcm = voice->ypcm) == NULL) {
585     		return;
586     	}
587     	if ((state = ypcm->state) == NULL) {
588     		ypcm->running = 0;	// lock it
589     		return;
590     	}
591     	dmabuf = &ypcm->dmabuf;
592     	spin_lock(&codec->reg_lock);
593     	if (ypcm->running) {
594     		YMFDBGI("ymfpci: %d, intr bank %d count %d start 0x%x:%x\n",
595     		   voice->number, codec->active_bank, dmabuf->count,
596     		   voice->bank[0].start, voice->bank[1].start);
597     		silence = (ymf_pcm_format_width(state->format.format) == 16) ?
598     		    0 : 0x80;
599     		/* We need actual left-hand-side redzone size here. */
600     		redzone = ymf_calc_lend(state->format.rate);
601     		redzone <<= (state->format.shift + 1);
602     		swptr = dmabuf->swptr;
603     
604     		pos = voice->bank[codec->active_bank].start;
605     		pos <<= state->format.shift;
606     		if (pos < 0 || pos >= dmabuf->dmasize) {	/* ucode bug */
607     			printk(KERN_ERR "ymfpci%d: runaway voice %d: hwptr %d=>%d dmasize %d\n",
608     			    codec->dev_audio, voice->number,
609     			    dmabuf->hwptr, pos, dmabuf->dmasize);
610     			pos = 0;
611     		}
612     		if (pos < dmabuf->hwptr) {
613     			delta = dmabuf->dmasize - dmabuf->hwptr;
614     			memset(dmabuf->rawbuf + dmabuf->hwptr, silence, delta);
615     			delta += pos;
616     			memset(dmabuf->rawbuf, silence, pos);
617     		} else {
618     			delta = pos - dmabuf->hwptr;
619     			memset(dmabuf->rawbuf + dmabuf->hwptr, silence, delta);
620     		}
621     		dmabuf->hwptr = pos;
622     
623     		if (dmabuf->count == 0) {
624     			printk("ymfpci%d: %d: strain: hwptr %d\n",
625     			    codec->dev_audio, voice->number, dmabuf->hwptr);
626     			ymf_playback_trigger(codec, ypcm, 0);
627     		}
628     
629     		if (swptr <= pos) {
630     			distance = pos - swptr;
631     		} else {
632     			distance = dmabuf->dmasize - (swptr - pos);
633     		}
634     		if (distance < redzone) {
635     			/*
636     			 * hwptr inside redzone => DMA ran out of samples.
637     			 */
638     			if (delta < dmabuf->count) {
639     				/*
640     				 * Lost interrupt or other screwage.
641     				 */
642     				printk("ymfpci%d: %d: lost: delta %d"
643     				    " hwptr %d swptr %d distance %d count %d\n",
644     				    codec->dev_audio, voice->number, delta,
645     				    dmabuf->hwptr, swptr, distance, dmabuf->count);
646     			} else {
647     				/*
648     				 * Normal end of DMA.
649     				 */
650     //				printk("ymfpci%d: %d: done: delta %d"
651     //				    " hwptr %d swptr %d distance %d count %d\n",
652     //				    codec->dev_audio, voice->number, delta,
653     //				    dmabuf->hwptr, swptr, distance, dmabuf->count);
654     			}
655     			played = dmabuf->count;
656     			if (ypcm->running) {
657     				ymf_playback_trigger(codec, ypcm, 0);
658     			}
659     		} else {
660     			/*
661     			 * hwptr is chipping away towards a remote swptr.
662     			 * Calculate other distance and apply it to count.
663     			 */
664     			if (swptr >= pos) {
665     				distance = swptr - pos;
666     			} else {
667     				distance = dmabuf->dmasize - (pos - swptr);
668     			}
669     			if (distance < dmabuf->count) {
670     				played = dmabuf->count - distance;
671     			} else {
672     				played = 0;
673     			}
674     		}
675     
676     		dmabuf->total_bytes += played;
677     		dmabuf->count -= played;
678     		if (dmabuf->count < dmabuf->dmasize / 2) {
679     			wake_up(&dmabuf->wait);
680     		}
681     	}
682     	spin_unlock(&codec->reg_lock);
683     }
684     
685     static void ymf_cap_interrupt(ymfpci_t *unit, struct ymf_capture *cap)
686     {
687     	struct ymf_pcm *ypcm;
688     	int redzone;
689     	struct ymf_state *state;
690     	struct ymf_dmabuf *dmabuf;
691     	int pos, delta;
692     	int cnt;
693     
694     	if ((ypcm = cap->ypcm) == NULL) {
695     		return;
696     	}
697     	if ((state = ypcm->state) == NULL) {
698     		ypcm->running = 0;	// lock it
699     		return;
700     	}
701     	dmabuf = &ypcm->dmabuf;
702     	spin_lock(&unit->reg_lock);
703     	if (ypcm->running) {
704     		redzone = ymf_calc_lend(state->format.rate);
705     		redzone <<= (state->format.shift + 1);
706     
707     		pos = cap->bank[unit->active_bank].start;
708     		// pos <<= state->format.shift;
709     		if (pos < 0 || pos >= dmabuf->dmasize) {	/* ucode bug */
710     			printk(KERN_ERR "ymfpci%d: runaway capture %d: hwptr %d=>%d dmasize %d\n",
711     			    unit->dev_audio, ypcm->capture_bank_number,
712     			    dmabuf->hwptr, pos, dmabuf->dmasize);
713     			pos = 0;
714     		}
715     		if (pos < dmabuf->hwptr) {
716     			delta = dmabuf->dmasize - dmabuf->hwptr;
717     			delta += pos;
718     		} else {
719     			delta = pos - dmabuf->hwptr;
720     		}
721     		dmabuf->hwptr = pos;
722     
723     		cnt = dmabuf->count;
724     		cnt += delta;
725     		if (cnt + redzone > dmabuf->dmasize) {
726     			/* Overflow - bump swptr */
727     			dmabuf->count = dmabuf->dmasize - redzone;
728     			dmabuf->swptr = dmabuf->hwptr + redzone;
729     			if (dmabuf->swptr >= dmabuf->dmasize) {
730     				dmabuf->swptr -= dmabuf->dmasize;
731     			}
732     		} else {
733     			dmabuf->count = cnt;
734     		}
735     
736     		dmabuf->total_bytes += delta;
737     		if (dmabuf->count) {		/* && is_sleeping  XXX */
738     			wake_up(&dmabuf->wait);
739     		}
740     	}
741     	spin_unlock(&unit->reg_lock);
742     }
743     
744     static int ymf_playback_trigger(ymfpci_t *codec, struct ymf_pcm *ypcm, int cmd)
745     {
746     
747     	if (ypcm->voices[0] == NULL) {
748     		return -EINVAL;
749     	}
750     	if (cmd != 0) {
751     		codec->ctrl_playback[ypcm->voices[0]->number + 1] = virt_to_bus(ypcm->voices[0]->bank);
752     		if (ypcm->voices[1] != NULL)
753     			codec->ctrl_playback[ypcm->voices[1]->number + 1] = virt_to_bus(ypcm->voices[1]->bank);
754     		ypcm->running = 1;
755     	} else {
756     		codec->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
757     		if (ypcm->voices[1] != NULL)
758     			codec->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
759     		ypcm->running = 0;
760     	}
761     	return 0;
762     }
763     
764     static void ymf_capture_trigger(ymfpci_t *codec, struct ymf_pcm *ypcm, int cmd)
765     {
766     	u32 tmp;
767     
768     	if (cmd != 0) {
769     		tmp = ymfpci_readl(codec, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
770     		ymfpci_writel(codec, YDSXGR_MAPOFREC, tmp);
771     		ypcm->running = 1;
772     	} else {
773     		tmp = ymfpci_readl(codec, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
774     		ymfpci_writel(codec, YDSXGR_MAPOFREC, tmp);
775     		ypcm->running = 0;
776     	}
777     }
778     
779     static int ymfpci_pcm_voice_alloc(struct ymf_pcm *ypcm, int voices)
780     {
781     	struct ymf_unit *unit;
782     	int err;
783     
784     	unit = ypcm->state->unit;
785     	if (ypcm->voices[1] != NULL && voices < 2) {
786     		ymfpci_voice_free(unit, ypcm->voices[1]);
787     		ypcm->voices[1] = NULL;
788     	}
789     	if (voices == 1 && ypcm->voices[0] != NULL)
790     		return 0;		/* already allocated */
791     	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
792     		return 0;		/* already allocated */
793     	if (voices > 1) {
794     		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
795     			ymfpci_voice_free(unit, ypcm->voices[0]);
796     			ypcm->voices[0] = NULL;
797     		}		
798     		if ((err = voice_alloc(unit, YMFPCI_PCM, 1, ypcm->voices)) < 0)
799     			return err;
800     		ypcm->voices[0]->ypcm = ypcm;
801     		ypcm->voices[1]->ypcm = ypcm;
802     	} else {
803     		if ((err = voice_alloc(unit, YMFPCI_PCM, 0, ypcm->voices)) < 0)
804     			return err;
805     		ypcm->voices[0]->ypcm = ypcm;
806     	}
807     	return 0;
808     }
809     
810     static void ymf_pcm_init_voice(ymfpci_voice_t *voice, int stereo,
811         int rate, int w_16, unsigned long addr, unsigned int end, int spdif)
812     {
813     	u32 format;
814     	u32 delta = ymfpci_calc_delta(rate);
815     	u32 lpfQ = ymfpci_calc_lpfQ(rate);
816     	u32 lpfK = ymfpci_calc_lpfK(rate);
817     	ymfpci_playback_bank_t *bank;
818     	int nbank;
819     
820     	format = (stereo ? 0x00010000 : 0) | (w_16 ? 0 : 0x80000000);
821     	if (stereo)
822     		end >>= 1;
823     	if (w_16)
824     		end >>= 1;
825     	for (nbank = 0; nbank < 2; nbank++) {
826     		bank = &voice->bank[nbank];
827     		bank->format = format;
828     		bank->loop_default = 0;	/* 0-loops forever, otherwise count */
829     		bank->base = addr;
830     		bank->loop_start = 0;
831     		bank->loop_end = end;
832     		bank->loop_frac = 0;
833     		bank->eg_gain_end = 0x40000000;
834     		bank->lpfQ = lpfQ;
835     		bank->status = 0;
836     		bank->num_of_frames = 0;
837     		bank->loop_count = 0;
838     		bank->start = 0;
839     		bank->start_frac = 0;
840     		bank->delta =
841     		bank->delta_end = delta;
842     		bank->lpfK =
843     		bank->lpfK_end = lpfK;
844     		bank->eg_gain = 0x40000000;
845     		bank->lpfD1 =
846     		bank->lpfD2 = 0;
847     
848     		bank->left_gain = 
849     		bank->right_gain =
850     		bank->left_gain_end =
851     		bank->right_gain_end =
852     		bank->eff1_gain =
853     		bank->eff2_gain =
854     		bank->eff3_gain =
855     		bank->eff1_gain_end =
856     		bank->eff2_gain_end =
857     		bank->eff3_gain_end = 0;
858     
859     		if (!stereo) {
860     			if (!spdif) {
861     				bank->left_gain = 
862     				bank->right_gain =
863     				bank->left_gain_end =
864     				bank->right_gain_end = 0x40000000;
865     			} else {
866     				bank->eff2_gain =
867     				bank->eff2_gain_end =
868     				bank->eff3_gain =
869     				bank->eff3_gain_end = 0x40000000;
870     			}
871     		} else {
872     			if (!spdif) {
873     				if ((voice->number & 1) == 0) {
874     					bank->left_gain =
875     					bank->left_gain_end = 0x40000000;
876     				} else {
877     					bank->format |= 1;
878     					bank->right_gain =
879     					bank->right_gain_end = 0x40000000;
880     				}
881     			} else {
882     				if ((voice->number & 1) == 0) {
883     					bank->eff2_gain =
884     					bank->eff2_gain_end = 0x40000000;
885     				} else {
886     					bank->format |= 1;
887     					bank->eff3_gain =
888     					bank->eff3_gain_end = 0x40000000;
889     				}
890     			}
891     		}
892     	}
893     }
894     
895     /*
896      * XXX Capture channel allocation is entirely fake at the moment.
897      * We use only one channel and mark it busy as required.
898      */
899     static int ymf_capture_alloc(struct ymf_unit *unit, int *pbank)
900     {
901     	struct ymf_capture *cap;
902     	int cbank;
903     
904     	cbank = 1;		/* Only ADC slot is used for now. */
905     	cap = &unit->capture[cbank];
906     	if (cap->use)
907     		return -EBUSY;
908     	cap->use = 1;
909     	*pbank = cbank;
910     	return 0;
911     }
912     
913     static int ymf_playback_prepare(struct ymf_state *state)
914     {
915     	struct ymf_pcm *ypcm = &state->wpcm;
916     	int err, nvoice;
917     
918     	if ((err = ymfpci_pcm_voice_alloc(ypcm, state->format.voices)) < 0) {
919     		/* Somebody started 32 mpg123's in parallel? */
920     		printk(KERN_INFO "ymfpci%d: cannot allocate voice\n",
921     		    state->unit->dev_audio);
922     		return err;
923     	}
924     
925     	for (nvoice = 0; nvoice < state->format.voices; nvoice++) {
926     		ymf_pcm_init_voice(ypcm->voices[nvoice],
927     		    state->format.voices == 2, state->format.rate,
928     		    ymf_pcm_format_width(state->format.format) == 16,
929     		    virt_to_bus(ypcm->dmabuf.rawbuf), ypcm->dmabuf.dmasize,
930     		    ypcm->spdif);
931     	}
932     	return 0;
933     }
934     
935     static int ymf_capture_prepare(struct ymf_state *state)
936     {
937     	ymfpci_t *unit = state->unit;
938     	struct ymf_pcm *ypcm = &state->rpcm;
939     	ymfpci_capture_bank_t * bank;
940     	/* XXX This is confusing, gotta rename one of them banks... */
941     	int nbank;		/* flip-flop bank */
942     	int cbank;		/* input [super-]bank */
943     	struct ymf_capture *cap;
944     	u32 rate, format;
945     
946     	if (ypcm->capture_bank_number == -1) {
947     		if (ymf_capture_alloc(unit, &cbank) != 0)
948     			return -EBUSY;
949     
950     		ypcm->capture_bank_number = cbank;
951     
952     		cap = &unit->capture[cbank];
953     		cap->bank = unit->bank_capture[cbank][0];
954     		cap->ypcm = ypcm;
955     		ymfpci_hw_start(unit);
956     	}
957     
958     	// ypcm->frag_size = snd_pcm_lib_transfer_fragment(substream);
959     	// frag_size is replaced with nonfragged byte-aligned rolling buffer
960     	rate = ((48000 * 4096) / state->format.rate) - 1;
961     	format = 0;
962     	if (state->format.voices == 2)
963     		format |= 2;
964     	if (ymf_pcm_format_width(state->format.format) == 8)
965     		format |= 1;
966     	switch (ypcm->capture_bank_number) {
967     	case 0:
968     		ymfpci_writel(unit, YDSXGR_RECFORMAT, format);
969     		ymfpci_writel(unit, YDSXGR_RECSLOTSR, rate);
970     		break;
971     	case 1:
972     		ymfpci_writel(unit, YDSXGR_ADCFORMAT, format);
973     		ymfpci_writel(unit, YDSXGR_ADCSLOTSR, rate);
974     		break;
975     	}
976     	for (nbank = 0; nbank < 2; nbank++) {
977     		bank = unit->bank_capture[ypcm->capture_bank_number][nbank];
978     		bank->base = virt_to_bus(ypcm->dmabuf.rawbuf);
979     		// bank->loop_end = ypcm->dmabuf.dmasize >> state->format.shift;
980     		bank->loop_end = ypcm->dmabuf.dmasize;
981     		bank->start = 0;
982     		bank->num_of_loops = 0;
983     	}
984     #if 0 /* s/pdif */
985     	if (state->digital.dig_valid)
986     		/*state->digital.type == SND_PCM_DIG_AES_IEC958*/
987     		ymfpci_writew(codec, YDSXGR_SPDIFOUTSTATUS,
988     		    state->digital.dig_status[0] | (state->digital.dig_status[1] << 8));
989     #endif
990     	return 0;
991     }
992     
993     void ymf_interrupt(int irq, void *dev_id, struct pt_regs *regs)
994     {
995     	ymfpci_t *codec = dev_id;
996     	u32 status, nvoice, mode;
997     	struct ymf_voice *voice;
998     	struct ymf_capture *cap;
999     
1000     	status = ymfpci_readl(codec, YDSXGR_STATUS);
1001     	if (status & 0x80000000) {
1002     		codec->active_bank = ymfpci_readl(codec, YDSXGR_CTRLSELECT) & 1;
1003     		spin_lock(&codec->voice_lock);
1004     		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
1005     			voice = &codec->voices[nvoice];
1006     			if (voice->use)
1007     				ymf_pcm_interrupt(codec, voice);
1008     		}
1009     		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
1010     			cap = &codec->capture[nvoice];
1011     			if (cap->use)
1012     				ymf_cap_interrupt(codec, cap);
1013     		}
1014     		spin_unlock(&codec->voice_lock);
1015     		spin_lock(&codec->reg_lock);
1016     		ymfpci_writel(codec, YDSXGR_STATUS, 0x80000000);
1017     		mode = ymfpci_readl(codec, YDSXGR_MODE) | 2;
1018     		ymfpci_writel(codec, YDSXGR_MODE, mode);
1019     		spin_unlock(&codec->reg_lock);
1020     	}
1021     
1022     	status = ymfpci_readl(codec, YDSXGR_INTFLAG);
1023     	if (status & 1) {
1024     		/* timer handler */
1025     		ymfpci_writel(codec, YDSXGR_INTFLAG, ~0);
1026     	}
1027     }
1028     
1029     static void ymf_pcm_free_substream(struct ymf_pcm *ypcm)
1030     {
1031     	unsigned long flags;
1032     	struct ymf_unit *unit;
1033     
1034     	unit = ypcm->state->unit;
1035     
1036     	if (ypcm->type == PLAYBACK_VOICE) {
1037     		spin_lock_irqsave(&unit->voice_lock, flags);
1038     		if (ypcm->voices[1])
1039     			ymfpci_voice_free(unit, ypcm->voices[1]);
1040     		if (ypcm->voices[0])
1041     			ymfpci_voice_free(unit, ypcm->voices[0]);
1042     		spin_unlock_irqrestore(&unit->voice_lock, flags);
1043     	} else {
1044     		if (ypcm->capture_bank_number != -1) {
1045     			unit->capture[ypcm->capture_bank_number].use = 0;
1046     			ypcm->capture_bank_number = -1;
1047     			ymfpci_hw_stop(unit);
1048     		}
1049     	}
1050     }
1051     
1052     static struct ymf_state *ymf_state_alloc(ymfpci_t *unit)
1053     {
1054     	struct ymf_pcm *ypcm;
1055     	struct ymf_state *state;
1056     
1057     	if ((state = kmalloc(sizeof(struct ymf_state), GFP_KERNEL)) == NULL) {
1058     		goto out0;
1059     	}
1060     	memset(state, 0, sizeof(struct ymf_state));
1061     
1062     	ypcm = &state->wpcm;
1063     	ypcm->state = state;
1064     	ypcm->type = PLAYBACK_VOICE;
1065     	ypcm->capture_bank_number = -1;
1066     	init_waitqueue_head(&ypcm->dmabuf.wait);
1067     
1068     	ypcm = &state->rpcm;
1069     	ypcm->state = state;
1070     	ypcm->type = CAPTURE_AC97;
1071     	ypcm->capture_bank_number = -1;
1072     	init_waitqueue_head(&ypcm->dmabuf.wait);
1073     
1074     	state->unit = unit;
1075     
1076     	state->format.format = AFMT_U8;
1077     	state->format.rate = 8000;
1078     	state->format.voices = 1;
1079     	ymf_pcm_update_shift(&state->format);
1080     
1081     	return state;
1082     
1083     out0:
1084     	return NULL;
1085     }
1086     
1087     /* AES/IEC958 channel status bits */
1088     #define SND_PCM_AES0_PROFESSIONAL	(1<<0)	/* 0 = consumer, 1 = professional */
1089     #define SND_PCM_AES0_NONAUDIO		(1<<1)	/* 0 = audio, 1 = non-audio */
1090     #define SND_PCM_AES0_PRO_EMPHASIS	(7<<2)	/* mask - emphasis */
1091     #define SND_PCM_AES0_PRO_EMPHASIS_NOTID	(0<<2)	/* emphasis not indicated */
1092     #define SND_PCM_AES0_PRO_EMPHASIS_NONE	(1<<2)	/* none emphasis */
1093     #define SND_PCM_AES0_PRO_EMPHASIS_5015	(3<<2)	/* 50/15us emphasis */
1094     #define SND_PCM_AES0_PRO_EMPHASIS_CCITT	(7<<2)	/* CCITT J.17 emphasis */
1095     #define SND_PCM_AES0_PRO_FREQ_UNLOCKED	(1<<5)	/* source sample frequency: 0 = locked, 1 = unlocked */
1096     #define SND_PCM_AES0_PRO_FS		(3<<6)	/* mask - sample frequency */
1097     #define SND_PCM_AES0_PRO_FS_NOTID	(0<<6)	/* fs not indicated */
1098     #define SND_PCM_AES0_PRO_FS_44100	(1<<6)	/* 44.1kHz */
1099     #define SND_PCM_AES0_PRO_FS_48000	(2<<6)	/* 48kHz */
1100     #define SND_PCM_AES0_PRO_FS_32000	(3<<6)	/* 32kHz */
1101     #define SND_PCM_AES0_CON_NOT_COPYRIGHT	(1<<2)	/* 0 = copyright, 1 = not copyright */
1102     #define SND_PCM_AES0_CON_EMPHASIS	(7<<3)	/* mask - emphasis */
1103     #define SND_PCM_AES0_CON_EMPHASIS_NONE	(0<<3)	/* none emphasis */
1104     #define SND_PCM_AES0_CON_EMPHASIS_5015	(1<<3)	/* 50/15us emphasis */
1105     #define SND_PCM_AES0_CON_MODE		(3<<6)	/* mask - mode */
1106     #define SND_PCM_AES1_PRO_MODE		(15<<0)	/* mask - channel mode */
1107     #define SND_PCM_AES1_PRO_MODE_NOTID	(0<<0)	/* not indicated */
1108     #define SND_PCM_AES1_PRO_MODE_STEREOPHONIC (2<<0) /* stereophonic - ch A is left */
1109     #define SND_PCM_AES1_PRO_MODE_SINGLE	(4<<0)	/* single channel */
1110     #define SND_PCM_AES1_PRO_MODE_TWO	(8<<0)	/* two channels */
1111     #define SND_PCM_AES1_PRO_MODE_PRIMARY	(12<<0)	/* primary/secondary */
1112     #define SND_PCM_AES1_PRO_MODE_BYTE3	(15<<0)	/* vector to byte 3 */
1113     #define SND_PCM_AES1_PRO_USERBITS	(15<<4)	/* mask - user bits */
1114     #define SND_PCM_AES1_PRO_USERBITS_NOTID	(0<<4)	/* not indicated */
1115     #define SND_PCM_AES1_PRO_USERBITS_192	(8<<4)	/* 192-bit structure */
1116     #define SND_PCM_AES1_PRO_USERBITS_UDEF	(12<<4)	/* user defined application */
1117     #define SND_PCM_AES1_CON_CATEGORY	0x7f
1118     #define SND_PCM_AES1_CON_GENERAL	0x00
1119     #define SND_PCM_AES1_CON_EXPERIMENTAL	0x40
1120     #define SND_PCM_AES1_CON_SOLIDMEM_MASK	0x0f
1121     #define SND_PCM_AES1_CON_SOLIDMEM_ID	0x08
1122     #define SND_PCM_AES1_CON_BROADCAST1_MASK 0x07
1123     #define SND_PCM_AES1_CON_BROADCAST1_ID	0x04
1124     #define SND_PCM_AES1_CON_DIGDIGCONV_MASK 0x07
1125     #define SND_PCM_AES1_CON_DIGDIGCONV_ID	0x02
1126     #define SND_PCM_AES1_CON_ADC_COPYRIGHT_MASK 0x1f
1127     #define SND_PCM_AES1_CON_ADC_COPYRIGHT_ID 0x06
1128     #define SND_PCM_AES1_CON_ADC_MASK	0x1f
1129     #define SND_PCM_AES1_CON_ADC_ID		0x16
1130     #define SND_PCM_AES1_CON_BROADCAST2_MASK 0x0f
1131     #define SND_PCM_AES1_CON_BROADCAST2_ID	0x0e
1132     #define SND_PCM_AES1_CON_LASEROPT_MASK	0x07
1133     #define SND_PCM_AES1_CON_LASEROPT_ID	0x01
1134     #define SND_PCM_AES1_CON_MUSICAL_MASK	0x07
1135     #define SND_PCM_AES1_CON_MUSICAL_ID	0x05
1136     #define SND_PCM_AES1_CON_MAGNETIC_MASK	0x07
1137     #define SND_PCM_AES1_CON_MAGNETIC_ID	0x03
1138     #define SND_PCM_AES1_CON_IEC908_CD	(SND_PCM_AES1_CON_LASEROPT_ID|0x00)
1139     #define SND_PCM_AES1_CON_NON_IEC908_CD	(SND_PCM_AES1_CON_LASEROPT_ID|0x08)
1140     #define SND_PCM_AES1_CON_PCM_CODER	(SND_PCM_AES1_CON_DIGDIGCONV_ID|0x00)
1141     #define SND_PCM_AES1_CON_SAMPLER	(SND_PCM_AES1_CON_DIGDIGCONV_ID|0x20)
1142     #define SND_PCM_AES1_CON_MIXER		(SND_PCM_AES1_CON_DIGDIGCONV_ID|0x10)
1143     #define SND_PCM_AES1_CON_RATE_CONVERTER	(SND_PCM_AES1_CON_DIGDIGCONV_ID|0x18)
1144     #define SND_PCM_AES1_CON_SYNTHESIZER	(SND_PCM_AES1_CON_MUSICAL_ID|0x00)
1145     #define SND_PCM_AES1_CON_MICROPHONE	(SND_PCM_AES1_CON_MUSICAL_ID|0x08)
1146     #define SND_PCM_AES1_CON_DAT		(SND_PCM_AES1_CON_MAGNETIC_ID|0x00)
1147     #define SND_PCM_AES1_CON_VCR		(SND_PCM_AES1_CON_MAGNETIC_ID|0x08)
1148     #define SND_PCM_AES1_CON_ORIGINAL	(1<<7)	/* this bits depends on the category code */
1149     #define SND_PCM_AES2_PRO_SBITS		(7<<0)	/* mask - sample bits */
1150     #define SND_PCM_AES2_PRO_SBITS_20	(2<<0)	/* 20-bit - coordination */
1151     #define SND_PCM_AES2_PRO_SBITS_24	(4<<0)	/* 24-bit - main audio */
1152     #define SND_PCM_AES2_PRO_SBITS_UDEF	(6<<0)	/* user defined application */
1153     #define SND_PCM_AES2_PRO_WORDLEN	(7<<3)	/* mask - source word length */
1154     #define SND_PCM_AES2_PRO_WORDLEN_NOTID	(0<<3)	/* not indicated */
1155     #define SND_PCM_AES2_PRO_WORDLEN_22_18	(2<<3)	/* 22-bit or 18-bit */
1156     #define SND_PCM_AES2_PRO_WORDLEN_23_19	(4<<3)	/* 23-bit or 19-bit */
1157     #define SND_PCM_AES2_PRO_WORDLEN_24_20	(5<<3)	/* 24-bit or 20-bit */
1158     #define SND_PCM_AES2_PRO_WORDLEN_20_16	(6<<3)	/* 20-bit or 16-bit */
1159     #define SND_PCM_AES2_CON_SOURCE		(15<<0)	/* mask - source number */
1160     #define SND_PCM_AES2_CON_SOURCE_UNSPEC	(0<<0)	/* unspecified */
1161     #define SND_PCM_AES2_CON_CHANNEL	(15<<4)	/* mask - channel number */
1162     #define SND_PCM_AES2_CON_CHANNEL_UNSPEC	(0<<4)	/* unspecified */
1163     #define SND_PCM_AES3_CON_FS		(15<<0)	/* mask - sample frequency */
1164     #define SND_PCM_AES3_CON_FS_44100	(0<<0)	/* 44.1kHz */
1165     #define SND_PCM_AES3_CON_FS_48000	(2<<0)	/* 48kHz */
1166     #define SND_PCM_AES3_CON_FS_32000	(3<<0)	/* 32kHz */
1167     #define SND_PCM_AES3_CON_CLOCK		(3<<4)	/* mask - clock accuracy */
1168     #define SND_PCM_AES3_CON_CLOCK_1000PPM	(0<<4)	/* 1000 ppm */
1169     #define SND_PCM_AES3_CON_CLOCK_50PPM	(1<<4)	/* 50 ppm */
1170     #define SND_PCM_AES3_CON_CLOCK_VARIABLE	(2<<4)	/* variable pitch */
1171     
1172     /*
1173      * User interface
1174      */
1175     
1176     /*
1177      * in this loop, dmabuf.count signifies the amount of data that is
1178      * waiting to be copied to the user's buffer.  it is filled by the dma
1179      * machine and drained by this loop.
1180      */
1181     static ssize_t
1182     ymf_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1183     {
1184     	struct ymf_state *state = (struct ymf_state *)file->private_data;
1185     	struct ymf_dmabuf *dmabuf = &state->rpcm.dmabuf;
1186     	struct ymf_unit *unit = state->unit;
1187     	DECLARE_WAITQUEUE(waita, current);
1188     	ssize_t ret;
1189     	unsigned long flags;
1190     	unsigned int swptr;
1191     	int cnt;			/* This many to go in this revolution */
1192     
1193     	if (ppos != &file->f_pos)
1194     		return -ESPIPE;
1195     	if (dmabuf->mapped)
1196     		return -ENXIO;
1197     	if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
1198     		return ret;
1199     	ret = 0;
1200     
1201     	add_wait_queue(&dmabuf->wait, &waita);
1202     	set_current_state(TASK_INTERRUPTIBLE);
1203     	while (count > 0) {
1204     		spin_lock_irqsave(&unit->reg_lock, flags);
1205     		if (unit->suspended) {
1206     			spin_unlock_irqrestore(&unit->reg_lock, flags);
1207     			schedule();
1208     			set_current_state(TASK_INTERRUPTIBLE);
1209     			if (signal_pending(current)) {
1210     				if (!ret) ret = -EAGAIN;
1211     				break;
1212     			}
1213     			continue;
1214     		}
1215     		swptr = dmabuf->swptr;
1216     		cnt = dmabuf->dmasize - swptr;
1217     		if (dmabuf->count < cnt)
1218     			cnt = dmabuf->count;
1219     		spin_unlock_irqrestore(&unit->reg_lock, flags);
1220     
1221     		if (cnt > count)
1222     			cnt = count;
1223     		if (cnt <= 0) {
1224     			unsigned long tmo;
1225     			/* buffer is empty, start the dma machine and wait for data to be
1226     			   recorded */
1227     			spin_lock_irqsave(&state->unit->reg_lock, flags);
1228     			if (!state->rpcm.running) {
1229     				ymf_capture_trigger(state->unit, &state->rpcm, 1);
1230     			}
1231     			spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1232     			if (file->f_flags & O_NONBLOCK) {
1233     				if (!ret) ret = -EAGAIN;
1234     				break;
1235     			}
1236     			/* This isnt strictly right for the 810  but it'll do */
1237     			tmo = (dmabuf->dmasize * HZ) / (state->format.rate * 2);
1238     			tmo >>= state->format.shift;
1239     			/* There are two situations when sleep_on_timeout returns, one is when
1240     			   the interrupt is serviced correctly and the process is waked up by
1241     			   ISR ON TIME. Another is when timeout is expired, which means that
1242     			   either interrupt is NOT serviced correctly (pending interrupt) or it
1243     			   is TOO LATE for the process to be scheduled to run (scheduler latency)
1244     			   which results in a (potential) buffer overrun. And worse, there is
1245     			   NOTHING we can do to prevent it. */
1246     			tmo = schedule_timeout(tmo);
1247     			spin_lock_irqsave(&state->unit->reg_lock, flags);
1248     			set_current_state(TASK_INTERRUPTIBLE);
1249     			if (tmo == 0 && dmabuf->count == 0) {
1250     				printk(KERN_ERR "ymfpci%d: recording schedule timeout, "
1251     				    "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1252     				    state->unit->dev_audio,
1253     				    dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1254     				    dmabuf->hwptr, dmabuf->swptr);
1255     			}
1256     			spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1257     			if (signal_pending(current)) {
1258     				if (!ret) ret = -ERESTARTSYS;
1259     				break;
1260     			}
1261     			continue;
1262     		}
1263     
1264     		if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1265     			if (!ret) ret = -EFAULT;
1266     			break;
1267     		}
1268     
1269     		swptr = (swptr + cnt) % dmabuf->dmasize;
1270     
1271     		spin_lock_irqsave(&unit->reg_lock, flags);
1272     		if (unit->suspended) {
1273     			spin_unlock_irqrestore(&unit->reg_lock, flags);
1274     			continue;
1275     		}
1276     
1277     		dmabuf->swptr = swptr;
1278     		dmabuf->count -= cnt;
1279     		// spin_unlock_irqrestore(&unit->reg_lock, flags);
1280     
1281     		count -= cnt;
1282     		buffer += cnt;
1283     		ret += cnt;
1284     		// spin_lock_irqsave(&unit->reg_lock, flags);
1285     		if (!state->rpcm.running) {
1286     			ymf_capture_trigger(unit, &state->rpcm, 1);
1287     		}
1288     		spin_unlock_irqrestore(&unit->reg_lock, flags);
1289     	}
1290     	set_current_state(TASK_RUNNING);
1291     	remove_wait_queue(&dmabuf->wait, &waita);
1292     
1293     	return ret;
1294     }
1295     
1296     static ssize_t
1297     ymf_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
1298     {
1299     	struct ymf_state *state = (struct ymf_state *)file->private_data;
1300     	struct ymf_dmabuf *dmabuf = &state->wpcm.dmabuf;
1301     	struct ymf_unit *unit = state->unit;
1302     	DECLARE_WAITQUEUE(waita, current);
1303     	ssize_t ret;
1304     	unsigned long flags;
1305     	unsigned int swptr;
1306     	int cnt;			/* This many to go in this revolution */
1307     	int redzone;
1308     	int delay;
1309     
1310     	YMFDBGW("ymf_write: count %d\n", count);
1311     
1312     	if (ppos != &file->f_pos)
1313     		return -ESPIPE;
1314     	if (dmabuf->mapped)
1315     		return -ENXIO;
1316     	if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
1317     		return ret;
1318     	ret = 0;
1319     
1320     	/*
1321     	 * Alan's cs46xx works without a red zone - marvel of ingenuity.
1322     	 * We are not so brilliant... Red zone does two things:
1323     	 *  1. allows for safe start after a pause as we have no way
1324     	 *     to know what the actual, relentlessly advancing, hwptr is.
1325     	 *  2. makes computations in ymf_pcm_interrupt simpler.
1326     	 */
1327     	redzone = ymf_calc_lend(state->format.rate) << state->format.shift;
1328     	redzone *= 3;	/* 2 redzone + 1 possible uncertainty reserve. */
1329     
1330     	add_wait_queue(&dmabuf->wait, &waita);
1331     	set_current_state(TASK_INTERRUPTIBLE);
1332     	while (count > 0) {
1333     		spin_lock_irqsave(&unit->reg_lock, flags);
1334     		if (unit->suspended) {
1335     			spin_unlock_irqrestore(&unit->reg_lock, flags);
1336     			schedule();
1337     			set_current_state(TASK_INTERRUPTIBLE);
1338     			if (signal_pending(current)) {
1339     				if (!ret) ret = -EAGAIN;
1340     				break;
1341     			}
1342     			continue;
1343     		}
1344     		if (dmabuf->count < 0) {
1345     			printk(KERN_ERR
1346     			   "ymf_write: count %d, was legal in cs46xx\n",
1347     			    dmabuf->count);
1348     			dmabuf->count = 0;
1349     		}
1350     		if (dmabuf->count == 0) {
1351     			swptr = dmabuf->hwptr;
1352     			if (state->wpcm.running) {
1353     				/*
1354     				 * Add uncertainty reserve.
1355     				 */
1356     				cnt = ymf_calc_lend(state->format.rate);
1357     				cnt <<= state->format.shift;
1358     				if ((swptr += cnt) >= dmabuf->dmasize) {
1359     					swptr -= dmabuf->dmasize;
1360     				}
1361     			}
1362     			dmabuf->swptr = swptr;
1363     		} else {
1364     			/*
1365     			 * XXX This is not right if dmabuf->count is small -
1366     			 * about 2*x frame size or less. We cannot count on
1367     			 * on appending and not causing an artefact.
1368     			 * Should use a variation of the count==0 case above.
1369     			 */
1370     			swptr = dmabuf->swptr;
1371     		}
1372     		cnt = dmabuf->dmasize - swptr;
1373     		if (dmabuf->count + cnt > dmabuf->dmasize - redzone)
1374     			cnt = (dmabuf->dmasize - redzone) - dmabuf->count;
1375     		spin_unlock_irqrestore(&unit->reg_lock, flags);
1376     
1377     		if (cnt > count)
1378     			cnt = count;
1379     		if (cnt <= 0) {
1380     			YMFDBGW("ymf_write: full, count %d swptr %d\n",
1381     			   dmabuf->count, dmabuf->swptr);
1382     			/*
1383     			 * buffer is full, start the dma machine and
1384     			 * wait for data to be played
1385     			 */
1386     			spin_lock_irqsave(&unit->reg_lock, flags);
1387     			if (!state->wpcm.running) {
1388     				ymf_playback_trigger(unit, &state->wpcm, 1);
1389     			}
1390     			spin_unlock_irqrestore(&unit->reg_lock, flags);
1391     			if (file->f_flags & O_NONBLOCK) {
1392     				if (!ret) ret = -EAGAIN;
1393     				break;
1394     			}
1395     			schedule();
1396     			set_current_state(TASK_INTERRUPTIBLE);
1397     			if (signal_pending(current)) {
1398     				if (!ret) ret = -ERESTARTSYS;
1399     				break;
1400     			}
1401     			continue;
1402     		}
1403     		if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
1404     			if (!ret) ret = -EFAULT;
1405     			break;
1406     		}
1407     
1408     		if ((swptr += cnt) >= dmabuf->dmasize) {
1409     			swptr -= dmabuf->dmasize;
1410     		}
1411     
1412     		spin_lock_irqsave(&unit->reg_lock, flags);
1413     		if (unit->suspended) {
1414     			spin_unlock_irqrestore(&unit->reg_lock, flags);
1415     			continue;
1416     		}
1417     		dmabuf->swptr = swptr;
1418     		dmabuf->count += cnt;
1419     
1420     		/*
1421     		 * Start here is a bad idea - may cause startup click
1422     		 * in /bin/play when dmabuf is not full yet.
1423     		 * However, some broken applications do not make
1424     		 * any use of SNDCTL_DSP_SYNC (Doom is the worst).
1425     		 * One frame is about 5.3ms, Doom write size is 46ms.
1426     		 */
1427     		delay = state->format.rate / 20;	/* 50ms */
1428     		delay <<= state->format.shift;
1429     		if (dmabuf->count >= delay && !state->wpcm.running) {
1430     			ymf_playback_trigger(unit, &state->wpcm, 1);
1431     		}
1432     
1433     		spin_unlock_irqrestore(&unit->reg_lock, flags);
1434     
1435     		count -= cnt;
1436     		buffer += cnt;
1437     		ret += cnt;
1438     	}
1439     
1440     	set_current_state(TASK_RUNNING);
1441     	remove_wait_queue(&dmabuf->wait, &waita);
1442     
1443     	YMFDBGW("ymf_write: dmabuf.count %d\n", dmabuf->count);
1444     	return ret;
1445     }
1446     
1447     static unsigned int ymf_poll(struct file *file, struct poll_table_struct *wait)
1448     {
1449     	struct ymf_state *state = (struct ymf_state *)file->private_data;
1450     	struct ymf_dmabuf *dmabuf;
1451     	unsigned long flags;
1452     	unsigned int mask = 0;
1453     
1454     	if (file->f_mode & FMODE_WRITE)
1455     		poll_wait(file, &state->wpcm.dmabuf.wait, wait);
1456     	// if (file->f_mode & FMODE_READ)
1457     	// 	poll_wait(file, &dmabuf->wait, wait);
1458     
1459     	spin_lock_irqsave(&state->unit->reg_lock, flags);
1460     	if (file->f_mode & FMODE_READ) {
1461     		dmabuf = &state->rpcm.dmabuf;
1462     		if (dmabuf->count >= (signed)dmabuf->fragsize)
1463     			mask |= POLLIN | POLLRDNORM;
1464     	}
1465     	if (file->f_mode & FMODE_WRITE) {
1466     		dmabuf = &state->wpcm.dmabuf;
1467     		if (dmabuf->mapped) {
1468     			if (dmabuf->count >= (signed)dmabuf->fragsize)
1469     				mask |= POLLOUT | POLLWRNORM;
1470     		} else {
1471     			if ((signed)dmabuf->dmasize >= dmabuf->count + (signed)dmabuf->fragsize)
1472     				mask |= POLLOUT | POLLWRNORM;
1473     		}
1474     	}
1475     	spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1476     
1477     	return mask;
1478     }
1479     
1480     static int ymf_mmap(struct file *file, struct vm_area_struct *vma)
1481     {
1482     	struct ymf_state *state = (struct ymf_state *)file->private_data;
1483     	struct ymf_dmabuf *dmabuf = &state->wpcm.dmabuf;
1484     	int ret;
1485     	unsigned long size;
1486     
1487     	if (vma->vm_flags & VM_WRITE) {
1488     		if ((ret = prog_dmabuf(state, 0)) != 0)
1489     			return ret;
1490     	} else if (vma->vm_flags & VM_READ) {
1491     		if ((ret = prog_dmabuf(state, 1)) != 0)
1492     			return ret;
1493     	} else 
1494     		return -EINVAL;
1495     
1496     	if (vma->vm_pgoff != 0)
1497     		return -EINVAL;
1498     	size = vma->vm_end - vma->vm_start;
1499     	if (size > (PAGE_SIZE << dmabuf->buforder))
1500     		return -EINVAL;
1501     	if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf),
1502     			     size, vma->vm_page_prot))
1503     		return -EAGAIN;
1504     	dmabuf->mapped = 1;
1505     
1506     	return 0;
1507     }
1508     
1509     static int ymf_ioctl(struct inode *inode, struct file *file,
1510         unsigned int cmd, unsigned long arg)
1511     {
1512     	struct ymf_state *state = (struct ymf_state *)file->private_data;
1513     	struct ymf_dmabuf *dmabuf;
1514     	unsigned long flags;
1515     	audio_buf_info abinfo;
1516     	count_info cinfo;
1517     	int val;
1518     
1519     	switch (cmd) {
1520     	case OSS_GETVERSION:
1521     		return put_user(SOUND_VERSION, (int *)arg);
1522     
1523     	case SNDCTL_DSP_RESET:
1524     		if (file->f_mode & FMODE_WRITE) {
1525     			ymf_wait_dac(state);
1526     			dmabuf = &state->wpcm.dmabuf;
1527     			spin_lock_irqsave(&state->unit->reg_lock, flags);
1528     			dmabuf->ready = 0;
1529     			dmabuf->swptr = dmabuf->hwptr;
1530     			dmabuf->count = dmabuf->total_bytes = 0;
1531     			spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1532     		}
1533     		if (file->f_mode & FMODE_READ) {
1534     			ymf_stop_adc(state);
1535     			dmabuf = &state->rpcm.dmabuf;
1536     			spin_lock_irqsave(&state->unit->reg_lock, flags);
1537     			dmabuf->ready = 0;
1538     			dmabuf->swptr = dmabuf->hwptr;
1539     			dmabuf->count = dmabuf->total_bytes = 0;
1540     			spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1541     		}
1542     		return 0;
1543     
1544     	case SNDCTL_DSP_SYNC:
1545     		if (file->f_mode & FMODE_WRITE) {
1546     			dmabuf = &state->wpcm.dmabuf;
1547     			if (file->f_flags & O_NONBLOCK) {
1548     				spin_lock_irqsave(&state->unit->reg_lock, flags);
1549     				if (dmabuf->count != 0 && !state->wpcm.running) {
1550     					ymf_start_dac(state);
1551     				}
1552     				spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1553     			} else {
1554     				ymf_wait_dac(state);
1555     			}
1556     		}
1557     		/* XXX What does this do for reading? dmabuf->count=0; ? */
1558     		return 0;
1559     
1560     	case SNDCTL_DSP_SPEED: /* set smaple rate */
1561     		if (get_user(val, (int *)arg))
1562     			return -EFAULT;
1563     		if (val >= 8000 && val <= 48000) {
1564     			if (file->f_mode & FMODE_WRITE) {
1565     				ymf_wait_dac(state);
1566     				dmabuf = &state->wpcm.dmabuf;
1567     				spin_lock_irqsave(&state->unit->reg_lock, flags);
1568     				dmabuf->ready = 0;
1569     				state->format.rate = val;
1570     				ymf_pcm_update_shift(&state->format);
1571     				spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1572     			}
1573     			if (file->f_mode & FMODE_READ) {
1574     				ymf_stop_adc(state);
1575     				dmabuf = &state->rpcm.dmabuf;
1576     				spin_lock_irqsave(&state->unit->reg_lock, flags);
1577     				dmabuf->ready = 0;
1578     				state->format.rate = val;
1579     				ymf_pcm_update_shift(&state->format);
1580     				spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1581     			}
1582     		}
1583     		return put_user(state->format.rate, (int *)arg);
1584     
1585     	/*
1586     	 * OSS manual does not mention SNDCTL_DSP_STEREO at all.
1587     	 * All channels are mono and if you want stereo, you
1588     	 * play into two channels with SNDCTL_DSP_CHANNELS.
1589     	 * However, mpg123 calls it. I wonder, why Michael Hipp used it.
1590     	 */
1591     	case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
1592     		if (get_user(val, (int *)arg))
1593     			return -EFAULT;
1594     		if (file->f_mode & FMODE_WRITE) {
1595     			ymf_wait_dac(state); 
1596     			dmabuf = &state->wpcm.dmabuf;
1597     			spin_lock_irqsave(&state->unit->reg_lock, flags);
1598     			dmabuf->ready = 0;
1599     			state->format.voices = val ? 2 : 1;
1600     			ymf_pcm_update_shift(&state->format);
1601     			spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1602     		}
1603     		if (file->f_mode & FMODE_READ) {
1604     			ymf_stop_adc(state);
1605     			dmabuf = &state->rpcm.dmabuf;
1606     			spin_lock_irqsave(&state->unit->reg_lock, flags);
1607     			dmabuf->ready = 0;
1608     			state->format.voices = val ? 2 : 1;
1609     			ymf_pcm_update_shift(&state->format);
1610     			spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1611     		}
1612     		return 0;
1613     
1614     	case SNDCTL_DSP_GETBLKSIZE:
1615     		if (file->f_mode & FMODE_WRITE) {
1616     			if ((val = prog_dmabuf(state, 0)))
1617     				return val;
1618     			return put_user(state->wpcm.dmabuf.fragsize, (int *)arg);
1619     		}
1620     		if (file->f_mode & FMODE_READ) {
1621     			if ((val = prog_dmabuf(state, 1)))
1622     				return val;
1623     			return put_user(state->rpcm.dmabuf.fragsize, (int *)arg);
1624     		}
1625     		return -EINVAL;
1626     
1627     	case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
1628     		return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
1629     
1630     	case SNDCTL_DSP_SETFMT: /* Select sample format */
1631     		if (get_user(val, (int *)arg))
1632     			return -EFAULT;
1633     		if (val == AFMT_S16_LE || val == AFMT_U8) {
1634     			if (file->f_mode & FMODE_WRITE) {
1635     				ymf_wait_dac(state);
1636     				dmabuf = &state->wpcm.dmabuf;
1637     				spin_lock_irqsave(&state->unit->reg_lock, flags);
1638     				dmabuf->ready = 0;
1639     				state->format.format = val;
1640     				ymf_pcm_update_shift(&state->format);
1641     				spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1642     			}
1643     			if (file->f_mode & FMODE_READ) {
1644     				ymf_stop_adc(state);
1645     				dmabuf = &state->rpcm.dmabuf;
1646     				spin_lock_irqsave(&state->unit->reg_lock, flags);
1647     				dmabuf->ready = 0;
1648     				state->format.format = val;
1649     				ymf_pcm_update_shift(&state->format);
1650     				spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1651     			}
1652     		}
1653     		return put_user(state->format.format, (int *)arg);
1654     
1655     	case SNDCTL_DSP_CHANNELS:
1656     		if (get_user(val, (int *)arg))
1657     			return -EFAULT;
1658     		if (val != 0) {
1659     			if (file->f_mode & FMODE_WRITE) {
1660     				ymf_wait_dac(state);
1661     				if (val == 1 || val == 2) {
1662     					spin_lock_irqsave(&state->unit->reg_lock, flags);
1663     					dmabuf = &state->wpcm.dmabuf;
1664     					dmabuf->ready = 0;
1665     					state->format.voices = val;
1666     					ymf_pcm_update_shift(&state->format);
1667     					spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1668     				}
1669     			}
1670     			if (file->f_mode & FMODE_READ) {
1671     				ymf_stop_adc(state);
1672     				if (val == 1 || val == 2) {
1673     					spin_lock_irqsave(&state->unit->reg_lock, flags);
1674     					dmabuf = &state->rpcm.dmabuf;
1675     					dmabuf->ready = 0;
1676     					state->format.voices = val;
1677     					ymf_pcm_update_shift(&state->format);
1678     					spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1679     				}
1680     			}
1681     		}
1682     		return put_user(state->format.voices, (int *)arg);
1683     
1684     	case SNDCTL_DSP_POST:
1685     		/*
1686     		 * Quoting OSS PG:
1687     		 *    The ioctl SNDCTL_DSP_POST is a lightweight version of
1688     		 *    SNDCTL_DSP_SYNC. It just tells to the driver that there
1689     		 *    is likely to be a pause in the output. This makes it
1690     		 *    possible for the device to handle the pause more
1691     		 *    intelligently. This ioctl doesn't block the application.
1692     		 *
1693     		 * The paragraph above is a clumsy way to say "flush ioctl".
1694     		 * This ioctl is used by mpg123.
1695     		 */
1696     		spin_lock_irqsave(&state->unit->reg_lock, flags);
1697     		if (state->wpcm.dmabuf.count != 0 && !state->wpcm.running) {
1698     			ymf_start_dac(state);
1699     		}
1700     		spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1701     		return 0;
1702     
1703     	case SNDCTL_DSP_SETFRAGMENT:
1704     		if (get_user(val, (int *)arg))
1705     			return -EFAULT;
1706     		dmabuf = &state->wpcm.dmabuf;
1707     		dmabuf->ossfragshift = val & 0xffff;
1708     		dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
1709     		if (dmabuf->ossfragshift < 4)
1710     			dmabuf->ossfragshift = 4;
1711     		if (dmabuf->ossfragshift > 15)
1712     			dmabuf->ossfragshift = 15;
1713     		return 0;
1714     
1715     	case SNDCTL_DSP_GETOSPACE:
1716     		if (!(file->f_mode & FMODE_WRITE))
1717     			return -EINVAL;
1718     		dmabuf = &state->wpcm.dmabuf;
1719     		if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
1720     			return val;
1721     		spin_lock_irqsave(&state->unit->reg_lock, flags);
1722     		abinfo.fragsize = dmabuf->fragsize;
1723     		abinfo.bytes = dmabuf->dmasize - dmabuf->count;
1724     		abinfo.fragstotal = dmabuf->numfrag;
1725     		abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
1726     		spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1727     		return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1728     
1729     	case SNDCTL_DSP_GETISPACE:
1730     		if (!(file->f_mode & FMODE_READ))
1731     			return -EINVAL;
1732     		dmabuf = &state->rpcm.dmabuf;
1733     		if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
1734     			return val;
1735     		spin_lock_irqsave(&state->unit->reg_lock, flags);
1736     		abinfo.fragsize = dmabuf->fragsize;
1737     		abinfo.bytes = dmabuf->count;
1738     		abinfo.fragstotal = dmabuf->numfrag;
1739     		abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
1740     		spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1741     		return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1742     
1743     	case SNDCTL_DSP_NONBLOCK:
1744     		file->f_flags |= O_NONBLOCK;
1745     		return 0;
1746     
1747     	case SNDCTL_DSP_GETCAPS:
1748     		/* return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
1749     			    (int *)arg); */
1750     		return put_user(0, (int *)arg);
1751     
1752     	case SNDCTL_DSP_GETIPTR:
1753     		if (!(file->f_mode & FMODE_READ))
1754     			return -EINVAL;
1755     		dmabuf = &state->rpcm.dmabuf;
1756     		spin_lock_irqsave(&state->unit->reg_lock, flags);
1757     		cinfo.bytes = dmabuf->total_bytes;
1758     		cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
1759     		cinfo.ptr = dmabuf->hwptr;
1760     		/* XXX fishy - breaks invariant  count=hwptr-swptr */
1761     		if (dmabuf->mapped)
1762     			dmabuf->count &= dmabuf->fragsize-1;
1763     		spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1764     		return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1765     
1766     	case SNDCTL_DSP_GETOPTR:
1767     		if (!(file->f_mode & FMODE_WRITE))
1768     			return -EINVAL;
1769     		dmabuf = &state->wpcm.dmabuf;
1770     		spin_lock_irqsave(&state->unit->reg_lock, flags);
1771     		cinfo.bytes = dmabuf->total_bytes;
1772     		cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
1773     		cinfo.ptr = dmabuf->hwptr;
1774     		/* XXX fishy - breaks invariant  count=swptr-hwptr */
1775     		if (dmabuf->mapped)
1776     			dmabuf->count &= dmabuf->fragsize-1;
1777     		spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1778     		return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1779     
1780     	case SNDCTL_DSP_SETDUPLEX:	/* XXX TODO */
1781     		return -EINVAL;
1782     
1783     	case SOUND_PCM_READ_RATE:
1784     		return put_user(state->format.rate, (int *)arg);
1785     
1786     	case SOUND_PCM_READ_CHANNELS:
1787     		return put_user(state->format.voices, (int *)arg);
1788     
1789     	case SOUND_PCM_READ_BITS:
1790     		return put_user(AFMT_S16_LE, (int *)arg);
1791     
1792     	case SNDCTL_DSP_MAPINBUF:
1793     	case SNDCTL_DSP_MAPOUTBUF:
1794     	case SNDCTL_DSP_SETSYNCRO:
1795     	case SOUND_PCM_WRITE_FILTER:
1796     	case SOUND_PCM_READ_FILTER:
1797     		return -ENOTTY;
1798     
1799     	default:
1800     		/*
1801     		 * Some programs mix up audio devices and ioctls
1802     		 * or perhaps they expect "universal" ioctls,
1803     		 * for instance we get SNDCTL_TMR_CONTINUE here.
1804     		 */
1805     		 break;
1806     	}
1807     	return -ENOTTY;
1808     }
1809     
1810     /*
1811      * open(2)
1812      * We use upper part of the minor to distinguish between soundcards.
1813      * Channels are opened with a clone open.
1814      */
1815     static int ymf_open(struct inode *inode, struct file *file)
1816     {
1817     	struct list_head *list;
1818     	ymfpci_t *unit = NULL;
1819     	int minor;
1820     	struct ymf_state *state;
1821     	int err;
1822     
1823     	minor = MINOR(inode->i_rdev);
1824     	if ((minor & 0x0F) == 3) {	/* /dev/dspN */
1825     		;
1826     	} else {
1827     		return -ENXIO;
1828     	}
1829     
1830     	unit = NULL;	/* gcc warns */
1831     	for (list = ymf_devs.next; list != &ymf_devs; list = list->next) {
1832     		unit = list_entry(list, ymfpci_t, ymf_devs);
1833     		if (((unit->dev_audio ^ minor) & ~0x0F) == 0)
1834     			break;
1835     	}
1836     	if (list == &ymf_devs)
1837     		return -ENODEV;
1838     
1839     	down(&unit->open_sem);
1840     
1841     	if ((state = ymf_state_alloc(unit)) == NULL) {
1842     		up(&unit->open_sem);
1843     		return -ENOMEM;
1844     	}
1845     	list_add_tail(&state->chain, &unit->states);
1846     
1847     	file->private_data = state;
1848     
1849     	/*
1850     	 * ymf_read and ymf_write that we borrowed from cs46xx
1851     	 * allocate buffers with prog_dmabuf(). We call prog_dmabuf
1852     	 * here so that in case of DMA memory exhaustion open
1853     	 * fails rather than write.
1854     	 *
1855     	 * XXX prog_dmabuf allocates voice. Should allocate explicitly, above.
1856     	 */
1857     	if (file->f_mode & FMODE_WRITE) {
1858     		if (!state->wpcm.dmabuf.ready) {
1859     			if ((err = prog_dmabuf(state, 0)) != 0) {
1860     				goto out_nodma;
1861     			}
1862     		}
1863     	}
1864     	if (file->f_mode & FMODE_READ) {
1865     		if (!state->rpcm.dmabuf.ready) {
1866     			if ((err = prog_dmabuf(state, 1)) != 0) {
1867     				goto out_nodma;
1868     			}
1869     		}
1870     	}
1871     
1872     #if 0 /* test if interrupts work */
1873     	ymfpci_writew(codec, YDSXGR_TIMERCOUNT, 0xfffe);	/* ~ 680ms */
1874     	ymfpci_writeb(codec, YDSXGR_TIMERCTRL,
1875     	    (YDSXGR_TIMERCTRL_TEN|YDSXGR_TIMERCTRL_TIEN));
1876     #endif
1877     	up(&unit->open_sem);
1878     
1879     	MOD_INC_USE_COUNT;
1880     	return 0;
1881     
1882     out_nodma:
1883     	/*
1884     	 * XXX Broken custom: "goto out_xxx" in other place is
1885     	 * a nestable exception, but here it is not nestable due to semaphore.
1886     	 * XXX Doubtful technique of self-describing objects....
1887     	 */
1888     	dealloc_dmabuf(&state->wpcm.dmabuf);
1889     	dealloc_dmabuf(&state->rpcm.dmabuf);
1890     	ymf_pcm_free_substream(&state->wpcm);
1891     	ymf_pcm_free_substream(&state->rpcm);
1892     
1893     	list_del(&state->chain);
1894     	kfree(state);
1895     
1896     	up(&unit->open_sem);
1897     	return err;
1898     }
1899     
1900     static int ymf_release(struct inode *inode, struct file *file)
1901     {
1902     	struct ymf_state *state = (struct ymf_state *)file->private_data;
1903     	ymfpci_t *codec = state->unit;
1904     
1905     #if 0 /* test if interrupts work */
1906     	ymfpci_writeb(codec, YDSXGR_TIMERCTRL, 0);
1907     #endif
1908     
1909     	down(&codec->open_sem);
1910     
1911     	/*
1912     	 * XXX Solve the case of O_NONBLOCK close - don't deallocate here.
1913     	 * Deallocate when unloading the driver and we can wait.
1914     	 */
1915     	ymf_wait_dac(state);
1916     	ymf_stop_adc(state);		/* fortunately, it's immediate */
1917     	dealloc_dmabuf(&state->wpcm.dmabuf);
1918     	dealloc_dmabuf(&state->rpcm.dmabuf);
1919     	ymf_pcm_free_substream(&state->wpcm);
1920     	ymf_pcm_free_substream(&state->rpcm);
1921     
1922     	list_del(&state->chain);
1923     	file->private_data = NULL;	/* Can you tell I programmed Solaris */
1924     	kfree(state);
1925     
1926     	up(&codec->open_sem);
1927     
1928     	MOD_DEC_USE_COUNT;
1929     	return 0;
1930     }
1931     
1932     /*
1933      * Mixer operations are based on cs46xx.
1934      */
1935     static int ymf_open_mixdev(struct inode *inode, struct file *file)
1936     {
1937     	int i;
1938     	int minor = MINOR(inode->i_rdev);
1939     	struct list_head *list;
1940     	ymfpci_t *unit;
1941     
1942     	for (list = ymf_devs.next; list != &ymf_devs; list = list->next) {
1943     		unit = list_entry(list, ymfpci_t, ymf_devs);
1944     		for (i = 0; i < NR_AC97; i++) {
1945     			if (unit->ac97_codec[i] != NULL &&
1946     			    unit->ac97_codec[i]->dev_mixer == minor) {
1947     				goto match;
1948     			}
1949     		}
1950     	}
1951     	return -ENODEV;
1952     
1953      match:
1954     	file->private_data = unit->ac97_codec[i];
1955     
1956     	MOD_INC_USE_COUNT;
1957     	return 0;
1958     }
1959     
1960     static int ymf_ioctl_mixdev(struct inode *inode, struct file *file,
1961         unsigned int cmd, unsigned long arg)
1962     {
1963     	struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
1964     
1965     	return codec->mixer_ioctl(codec, cmd, arg);
1966     }
1967     
1968     static int ymf_release_mixdev(struct inode *inode, struct file *file)
1969     {
1970     	MOD_DEC_USE_COUNT;
1971     	return 0;
1972     }
1973     
1974     static /*const*/ struct file_operations ymf_fops = {
1975     	llseek:		no_llseek,
1976     	read:		ymf_read,
1977     	write:		ymf_write,
1978     	poll:		ymf_poll,
1979     	ioctl:		ymf_ioctl,
1980     	mmap:		ymf_mmap,
1981     	open:		ymf_open,
1982     	release:	ymf_release,
1983     };
1984     
1985     static /*const*/ struct file_operations ymf_mixer_fops = {
1986     	llseek:		no_llseek,
1987     	ioctl:		ymf_ioctl_mixdev,
1988     	open:		ymf_open_mixdev,
1989     	release:	ymf_release_mixdev,
1990     };
1991     
1992     /*
1993      */
1994     
1995     static int ymf_suspend(struct pci_dev *pcidev, u32 unused)
1996     {
1997     	struct ymf_unit *unit = pci_get_drvdata(pcidev);
1998     	unsigned long flags;
1999     	struct ymf_dmabuf *dmabuf;
2000     	struct list_head *p;
2001     	struct ymf_state *state;
2002     
2003     	spin_lock_irqsave(&unit->reg_lock, flags);
2004     
2005     	unit->suspended = 1;
2006     
2007     	list_for_each(p, &unit->states) {
2008     		state = list_entry(p, struct ymf_state, chain);
2009     
2010     		dmabuf = &state->wpcm.dmabuf;
2011     		dmabuf->hwptr = dmabuf->swptr = 0;
2012     		dmabuf->total_bytes = 0;
2013     		dmabuf->count = 0;
2014     
2015     		dmabuf = &state->rpcm.dmabuf;
2016     		dmabuf->hwptr = dmabuf->swptr = 0;
2017     		dmabuf->total_bytes = 0;
2018     		dmabuf->count = 0;
2019     	}
2020     
2021     	ymfpci_writel(unit, YDSXGR_NATIVEDACOUTVOL, 0);
2022     	ymfpci_disable_dsp(unit);
2023     
2024     	spin_unlock_irqrestore(&unit->reg_lock, flags);
2025     	
2026     	return 0;
2027     }
2028     
2029     static int ymf_resume(struct pci_dev *pcidev)
2030     {
2031     	struct ymf_unit *unit = pci_get_drvdata(pcidev);
2032     	unsigned long flags;
2033     	struct list_head *p;
2034     	struct ymf_state *state;
2035     
2036     	ymfpci_aclink_reset(unit->pci);
2037     	ymfpci_codec_ready(unit, 0, 1);		/* prints diag if not ready. */
2038     
2039     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2040     	/* XXX At this time the legacy registers are probably deprogrammed. */
2041     #endif
2042     
2043     	ymfpci_download_image(unit);
2044     
2045     	ymf_memload(unit);
2046     
2047     	spin_lock_irqsave(&unit->reg_lock, flags);
2048     
2049     	if (unit->start_count) {
2050     		ymfpci_writel(unit, YDSXGR_MODE, 3);
2051     		unit->active_bank = ymfpci_readl(unit, YDSXGR_CTRLSELECT) & 1;
2052     	}
2053     
2054     	unit->suspended = 0;
2055     	list_for_each(p, &unit->states) {
2056     		state = list_entry(p, struct ymf_state, chain);
2057     		wake_up(&state->wpcm.dmabuf.wait);
2058     		wake_up(&state->rpcm.dmabuf.wait);
2059     	}
2060     
2061     	spin_unlock_irqrestore(&unit->reg_lock, flags);
2062     	return 0;
2063     }
2064     
2065     /*
2066      *  initialization routines
2067      */
2068     
2069     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2070     
2071     static int ymfpci_setup_legacy(ymfpci_t *unit, struct pci_dev *pcidev)
2072     {
2073     	int v;
2074     	int mpuio = -1, oplio = -1;
2075     
2076     	switch (unit->iomidi) {
2077     	case 0x330:
2078     		mpuio = 0;
2079     		break;
2080     	case 0x300:
2081     		mpuio = 1;
2082     		break;
2083     	case 0x332:
2084     		mpuio = 2;
2085     		break;
2086     	case 0x334:
2087     		mpuio = 3;
2088     		break;
2089     	default: ;
2090     	}
2091     
2092     	switch (unit->iosynth) {
2093     	case 0x388:
2094     		oplio = 0;
2095     		break;
2096     	case 0x398:
2097     		oplio = 1;
2098     		break;
2099     	case 0x3a0:
2100     		oplio = 2;
2101     		break;
2102     	case 0x3a8:
2103     		oplio = 3;
2104     		break;
2105     	default: ;
2106     	}
2107     
2108     	if (mpuio >= 0 || oplio >= 0) {
2109     		/* 0x0020: 1 - 10 bits of I/O address decoded, 0 - 16 bits. */
2110     		v = 0x001e;
2111     		pci_write_config_word(pcidev, PCIR_LEGCTRL, v);
2112     
2113     		switch (pcidev->device) {
2114     		case PCI_DEVICE_ID_YAMAHA_724:
2115     		case PCI_DEVICE_ID_YAMAHA_740:
2116     		case PCI_DEVICE_ID_YAMAHA_724F:
2117     		case PCI_DEVICE_ID_YAMAHA_740C:
2118     			v = 0x8800;
2119     			if (mpuio >= 0) { v |= mpuio<<4; }
2120     			if (oplio >= 0) { v |= oplio; }
2121     			pci_write_config_word(pcidev, PCIR_ELEGCTRL, v);
2122     			break;
2123     
2124     		case PCI_DEVICE_ID_YAMAHA_744:
2125     		case PCI_DEVICE_ID_YAMAHA_754:
2126     			v = 0x8800;
2127     			pci_write_config_word(pcidev, PCIR_ELEGCTRL, v);
2128     			if (oplio >= 0) {
2129     				pci_write_config_word(pcidev, PCIR_OPLADR, unit->iosynth);
2130     			}
2131     			if (mpuio >= 0) {
2132     				pci_write_config_word(pcidev, PCIR_MPUADR, unit->iomidi);
2133     			}
2134     			break;
2135     
2136     		default:
2137     			printk(KERN_ERR "ymfpci: Unknown device ID: 0x%x\n",
2138     			    pcidev->device);
2139     			return -EINVAL;
2140     		}
2141     	}
2142     
2143     	return 0;
2144     }
2145     #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2146     
2147     static void ymfpci_aclink_reset(struct pci_dev * pci)
2148     {
2149     	u8 cmd;
2150     
2151     	pci_read_config_byte(pci, PCIR_DSXGCTRL, &cmd);
2152     	if (cmd & 0x03) {
2153     		pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd & 0xfc);
2154     		pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd | 0x03);
2155     		pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd & 0xfc);
2156     	}
2157     	pci_write_config_word(pci, PCIR_DSXPWRCTRL1, 0);
2158     	pci_write_config_word(pci, PCIR_DSXPWRCTRL2, 0);
2159     }
2160     
2161     static void ymfpci_enable_dsp(ymfpci_t *codec)
2162     {
2163     	ymfpci_writel(codec, YDSXGR_CONFIG, 0x00000001);
2164     }
2165     
2166     static void ymfpci_disable_dsp(ymfpci_t *codec)
2167     {
2168     	u32 val;
2169     	int timeout = 1000;
2170     
2171     	val = ymfpci_readl(codec, YDSXGR_CONFIG);
2172     	if (val)
2173     		ymfpci_writel(codec, YDSXGR_CONFIG, 0x00000000);
2174     	while (timeout-- > 0) {
2175     		val = ymfpci_readl(codec, YDSXGR_STATUS);
2176     		if ((val & 0x00000002) == 0)
2177     			break;
2178     	}
2179     }
2180     
2181     #include "ymfpci_image.h"
2182     
2183     static void ymfpci_download_image(ymfpci_t *codec)
2184     {
2185     	int i, ver_1e;
2186     	u16 ctrl;
2187     
2188     	ymfpci_writel(codec, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2189     	ymfpci_disable_dsp(codec);
2190     	ymfpci_writel(codec, YDSXGR_MODE, 0x00010000);
2191     	ymfpci_writel(codec, YDSXGR_MODE, 0x00000000);
2192     	ymfpci_writel(codec, YDSXGR_MAPOFREC, 0x00000000);
2193     	ymfpci_writel(codec, YDSXGR_MAPOFEFFECT, 0x00000000);
2194     	ymfpci_writel(codec, YDSXGR_PLAYCTRLBASE, 0x00000000);
2195     	ymfpci_writel(codec, YDSXGR_RECCTRLBASE, 0x00000000);
2196     	ymfpci_writel(codec, YDSXGR_EFFCTRLBASE, 0x00000000);
2197     	ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2198     	ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2199     
2200     	/* setup DSP instruction code */
2201     	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2202     		ymfpci_writel(codec, YDSXGR_DSPINSTRAM + (i << 2), DspInst[i]);
2203     
2204     	switch (codec->pci->device) {
2205     	case PCI_DEVICE_ID_YAMAHA_724F:
2206     	case PCI_DEVICE_ID_YAMAHA_740C:
2207     	case PCI_DEVICE_ID_YAMAHA_744:
2208     	case PCI_DEVICE_ID_YAMAHA_754:
2209     		ver_1e = 1;
2210     		break;
2211     	default:
2212     		ver_1e = 0;
2213     	}
2214     
2215     	if (ver_1e) {
2216     		/* setup control instruction code */
2217     		for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2218     			ymfpci_writel(codec, YDSXGR_CTRLINSTRAM + (i << 2), CntrlInst1E[i]);
2219     	} else {
2220     		for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2221     			ymfpci_writel(codec, YDSXGR_CTRLINSTRAM + (i << 2), CntrlInst[i]);
2222     	}
2223     
2224     	ymfpci_enable_dsp(codec);
2225     
2226     	/* 0.02s sounds not too bad, we may do schedule_timeout() later. */
2227     	mdelay(20); /* seems we need some delay after downloading image.. */
2228     }
2229     
2230     static int ymfpci_memalloc(ymfpci_t *codec)
2231     {
2232     	long size, playback_ctrl_size;
2233     	int voice, bank;
2234     	u8 *ptr;
2235     
2236     	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2237     	codec->bank_size_playback = ymfpci_readl(codec, YDSXGR_PLAYCTRLSIZE) << 2;
2238     	codec->bank_size_capture = ymfpci_readl(codec, YDSXGR_RECCTRLSIZE) << 2;
2239     	codec->bank_size_effect = ymfpci_readl(codec, YDSXGR_EFFCTRLSIZE) << 2;
2240     	codec->work_size = YDSXG_DEFAULT_WORK_SIZE;
2241     
2242     	size = ((playback_ctrl_size + 0x00ff) & ~0x00ff) +
2243     	    ((codec->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES + 0xff) & ~0xff) +
2244     	    ((codec->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES + 0xff) & ~0xff) +
2245     	    ((codec->bank_size_effect * 2 * YDSXG_EFFECT_VOICES + 0xff) & ~0xff) +
2246     	    codec->work_size;
2247     
2248     	ptr = (u8 *)kmalloc(size + 0x00ff, GFP_KERNEL);
2249     	if (ptr == NULL)
2250     		return -ENOMEM;
2251     
2252     	codec->work_ptr = ptr;
2253     	ptr += 0x00ff;
2254     	(long)ptr &= ~0x00ff;
2255     
2256     	/*
2257     	 * Hardware requires only ptr[playback_ctrl_size] zeroed,
2258     	 * but in our judgement it is a wrong kind of savings, so clear it all.
2259     	 */
2260     	memset(ptr, 0, size);
2261     
2262     	codec->bank_base_playback = ptr;
2263     	codec->ctrl_playback = (u32 *)ptr;
2264     	codec->ctrl_playback[0] = YDSXG_PLAYBACK_VOICES;
2265     	ptr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2266     	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2267     		for (bank = 0; bank < 2; bank++) {
2268     			codec->bank_playback[voice][bank] = (ymfpci_playback_bank_t *)ptr;
2269     			ptr += codec->bank_size_playback;
2270     		}
2271     		codec->voices[voice].number = voice;
2272     		codec->voices[voice].bank = codec->bank_playback[voice][0];
2273     	}
2274     	ptr += (codec->bank_size_playback + 0x00ff) & ~0x00ff;
2275     	codec->bank_base_capture = ptr;
2276     	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2277     		for (bank = 0; bank < 2; bank++) {
2278     			codec->bank_capture[voice][bank] = (ymfpci_capture_bank_t *)ptr;
2279     			ptr += codec->bank_size_capture;
2280     		}
2281     	ptr += (codec->bank_size_capture + 0x00ff) & ~0x00ff;
2282     	codec->bank_base_effect = ptr;
2283     	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2284     		for (bank = 0; bank < 2; bank++) {
2285     			codec->bank_effect[voice][bank] = (ymfpci_effect_bank_t *)ptr;
2286     			ptr += codec->bank_size_effect;
2287     		}
2288     	ptr += (codec->bank_size_effect + 0x00ff) & ~0x00ff;
2289     	codec->work_base = ptr;
2290     
2291     	return 0;
2292     }
2293     
2294     static void ymfpci_memfree(ymfpci_t *codec)
2295     {
2296     	ymfpci_writel(codec, YDSXGR_PLAYCTRLBASE, 0);
2297     	ymfpci_writel(codec, YDSXGR_RECCTRLBASE, 0);
2298     	ymfpci_writel(codec, YDSXGR_EFFCTRLBASE, 0);
2299     	ymfpci_writel(codec, YDSXGR_WORKBASE, 0);
2300     	ymfpci_writel(codec, YDSXGR_WORKSIZE, 0);
2301     	kfree(codec->work_ptr);
2302     }
2303     
2304     static void ymf_memload(ymfpci_t *unit)
2305     {
2306     
2307     	ymfpci_writel(unit, YDSXGR_PLAYCTRLBASE, virt_to_bus(unit->bank_base_playback));
2308     	ymfpci_writel(unit, YDSXGR_RECCTRLBASE, virt_to_bus(unit->bank_base_capture));
2309     	ymfpci_writel(unit, YDSXGR_EFFCTRLBASE, virt_to_bus(unit->bank_base_effect));
2310     	ymfpci_writel(unit, YDSXGR_WORKBASE, virt_to_bus(unit->work_base));
2311     	ymfpci_writel(unit, YDSXGR_WORKSIZE, unit->work_size >> 2);
2312     
2313     	/* S/PDIF output initialization */
2314     	ymfpci_writew(unit, YDSXGR_SPDIFOUTCTRL, 0);
2315     	ymfpci_writew(unit, YDSXGR_SPDIFOUTSTATUS,
2316     		SND_PCM_AES0_CON_EMPHASIS_NONE |
2317     		(SND_PCM_AES1_CON_ORIGINAL << 8) |
2318     		(SND_PCM_AES1_CON_PCM_CODER << 8));
2319     
2320     	/* S/PDIF input initialization */
2321     	ymfpci_writew(unit, YDSXGR_SPDIFINCTRL, 0);
2322     
2323     	/* move this volume setup to mixer */
2324     	ymfpci_writel(unit, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2325     	ymfpci_writel(unit, YDSXGR_BUF441OUTVOL, 0);
2326     	ymfpci_writel(unit, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2327     	ymfpci_writel(unit, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2328     }
2329     
2330     static int ymf_ac97_init(ymfpci_t *unit, int num_ac97)
2331     {
2332     	struct ac97_codec *codec;
2333     	u16 eid;
2334     
2335     	if ((codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL)) == NULL)
2336     		return -ENOMEM;
2337     	memset(codec, 0, sizeof(struct ac97_codec));
2338     
2339     	/* initialize some basic codec information, other fields will be filled
2340     	   in ac97_probe_codec */
2341     	codec->private_data = unit;
2342     	codec->id = num_ac97;
2343     
2344     	codec->codec_read = ymfpci_codec_read;
2345     	codec->codec_write = ymfpci_codec_write;
2346     
2347     	if (ac97_probe_codec(codec) == 0) {
2348     		printk("ymfpci: ac97_probe_codec failed\n");
2349     		goto out_kfree;
2350     	}
2351     
2352     	eid = ymfpci_codec_read(codec, AC97_EXTENDED_ID);
2353     	if (eid==0xFFFFFF) {
2354     		printk(KERN_WARNING "ymfpci: no codec attached ?\n");
2355     		goto out_kfree;
2356     	}
2357     
2358     	unit->ac97_features = eid;
2359     
2360     	if ((codec->dev_mixer = register_sound_mixer(&ymf_mixer_fops, -1)) < 0) {
2361     		printk(KERN_ERR "ymfpci: couldn't register mixer!\n");
2362     		goto out_kfree;
2363     	}
2364     
2365     	unit->ac97_codec[num_ac97] = codec;
2366     
2367     	return 0;
2368      out_kfree:
2369     	kfree(codec);
2370     	return -ENODEV;
2371     }
2372     
2373     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2374     # ifdef MODULE
2375     static int mpu_io     = 0;
2376     static int synth_io   = 0;
2377     MODULE_PARM(mpu_io, "i");
2378     MODULE_PARM(synth_io, "i");
2379     # else
2380     static int mpu_io     = 0x330;
2381     static int synth_io   = 0x388;
2382     # endif
2383     static int assigned;
2384     #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2385     
2386     static int __devinit ymf_probe_one(struct pci_dev *pcidev, const struct pci_device_id *ent)
2387     {
2388     	u16 ctrl;
2389     	ymfpci_t *codec;
2390     
2391     	int err;
2392     
2393     	if ((err = pci_enable_device(pcidev)) != 0) {
2394     		printk(KERN_ERR "ymfpci: pci_enable_device failed\n");
2395     		return err;
2396     	}
2397     
2398     	if ((codec = kmalloc(sizeof(ymfpci_t), GFP_KERNEL)) == NULL) {
2399     		printk(KERN_ERR "ymfpci: no core\n");
2400     		return -ENOMEM;
2401     	}
2402     	memset(codec, 0, sizeof(*codec));
2403     
2404     	spin_lock_init(&codec->reg_lock);
2405     	spin_lock_init(&codec->voice_lock);
2406     	init_MUTEX(&codec->open_sem);
2407     	INIT_LIST_HEAD(&codec->states);
2408     	codec->pci = pcidev;
2409     
2410     	pci_read_config_byte(pcidev, PCI_REVISION_ID, &codec->rev);
2411     	codec->reg_area_virt = ioremap(pci_resource_start(pcidev, 0), 0x8000);
2412     	if (codec->reg_area_virt == NULL) {
2413     		printk(KERN_ERR "ymfpci: unable to map registers\n");
2414     		goto out_free;
2415     	}
2416     
2417     	printk(KERN_INFO "ymfpci: %s at 0x%lx IRQ %d\n",
2418     	    (char *)ent->driver_data, pci_resource_start(pcidev, 0), pcidev->irq);
2419     
2420     	ymfpci_aclink_reset(pcidev);
2421     	if (ymfpci_codec_ready(codec, 0, 1) < 0)
2422     		goto out_unmap;
2423     
2424     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2425     	if (assigned == 0) {
2426     		codec->iomidi = mpu_io;
2427     		codec->iosynth = synth_io;
2428     		if (ymfpci_setup_legacy(codec, pcidev) < 0)
2429     			goto out_unmap;
2430     		assigned = 1;
2431     	}
2432     #endif
2433     
2434     	ymfpci_download_image(codec);
2435     
2436     	if (ymfpci_memalloc(codec) < 0)
2437     		goto out_disable_dsp;
2438     	ymf_memload(codec);
2439     
2440     	if (request_irq(pcidev->irq, ymf_interrupt, SA_SHIRQ, "ymfpci", codec) != 0) {
2441     		printk(KERN_ERR "ymfpci: unable to request IRQ %d\n",
2442     		    pcidev->irq);
2443     		goto out_memfree;
2444     	}
2445     
2446     	/* register /dev/dsp */
2447     	if ((codec->dev_audio = register_sound_dsp(&ymf_fops, -1)) < 0) {
2448     		printk(KERN_ERR "ymfpci%d: unable to register dsp\n",
2449     		    codec->dev_audio);
2450     		goto out_free_irq;
2451     	}
2452     
2453     	/*
2454     	 * Poke just the primary for the moment.
2455     	 */
2456     	if ((err = ymf_ac97_init(codec, 0)) != 0)
2457     		goto out_unregister_sound_dsp;
2458     
2459     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2460     	codec->opl3_data.name = "ymfpci";
2461     	codec->mpu_data.name  = "ymfpci";
2462     
2463     	codec->opl3_data.io_base = codec->iosynth;
2464     	codec->opl3_data.irq     = -1;
2465     
2466     	codec->mpu_data.io_base  = codec->iomidi;
2467     	codec->mpu_data.irq      = -1;	/* XXX Make it ours. */
2468     
2469     	if (codec->iomidi) {
2470     		if (!probe_uart401(&codec->mpu_data, THIS_MODULE)) {
2471     			codec->iomidi = 0;	/* XXX kludge */
2472     		}
2473     	}
2474     #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2475     
2476     	/* put it into driver list */
2477     	list_add_tail(&codec->ymf_devs, &ymf_devs);
2478     	pci_set_drvdata(pcidev, codec);
2479     
2480     	return 0;
2481     
2482      out_unregister_sound_dsp:
2483     	unregister_sound_dsp(codec->dev_audio);
2484      out_free_irq:
2485     	free_irq(pcidev->irq, codec);
2486      out_memfree:
2487     	ymfpci_memfree(codec);
2488      out_disable_dsp:
2489     	ymfpci_disable_dsp(codec);
2490     	ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2491     	ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2492     	ymfpci_writel(codec, YDSXGR_STATUS, ~0);
2493      out_unmap:
2494     	iounmap(codec->reg_area_virt);
2495      out_free:
2496     	kfree(codec);
2497     	return -ENODEV;
2498     }
2499     
2500     static void __devexit ymf_remove_one(struct pci_dev *pcidev)
2501     {
2502     	__u16 ctrl;
2503     	ymfpci_t *codec = pci_get_drvdata(pcidev);
2504     
2505     	/* remove from list of devices */
2506     	list_del(&codec->ymf_devs);
2507     
2508     	unregister_sound_mixer(codec->ac97_codec[0]->dev_mixer);
2509     	kfree(codec->ac97_codec[0]);
2510     	unregister_sound_dsp(codec->dev_audio);
2511     	free_irq(pcidev->irq, codec);
2512     	ymfpci_memfree(codec);
2513     	ymfpci_writel(codec, YDSXGR_STATUS, ~0);
2514     	ymfpci_disable_dsp(codec);
2515     	ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2516     	ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2517     	iounmap(codec->reg_area_virt);
2518     #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2519     	if (codec->iomidi) {
2520     		unload_uart401(&codec->mpu_data);
2521     	}
2522     #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2523     	kfree(codec);
2524     }
2525     
2526     MODULE_AUTHOR("Jaroslav Kysela");
2527     MODULE_DESCRIPTION("Yamaha YMF7xx PCI Audio");
2528     
2529     static struct pci_driver ymfpci_driver = {
2530     	name:		"ymfpci",
2531     	id_table:	ymf_id_tbl,
2532     	probe:		ymf_probe_one,
2533     	remove:         ymf_remove_one,
2534     	suspend:	ymf_suspend,
2535     	resume:		ymf_resume
2536     };
2537     
2538     static int __init ymf_init_module(void)
2539     {
2540     	return pci_module_init(&ymfpci_driver);
2541     }
2542     
2543     static void __exit ymf_cleanup_module (void)
2544     {
2545     	pci_unregister_driver(&ymfpci_driver);
2546     }
2547     
2548     module_init(ymf_init_module);
2549     module_exit(ymf_cleanup_module);
2550