2 * Copyright 1999 Jaroslav Kysela <perex@suse.cz>
3 * Copyright 2000 Alan Cox <alan@redhat.com>
4 * Copyright 2001 Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
5 * Copyright 2002 Pete Zaitcev <zaitcev@yahoo.com>
7 * Yamaha YMF7xx driver.
9 * This code is a result of high-speed collision
10 * between ymfpci.c of ALSA and cs46xx.c of Linux.
11 * -- Pete Zaitcev <zaitcev@yahoo.com>; 2000/09/18
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * - Use P44Slot for 44.1 playback (beware of idle buzzing in P44Slot).
29 * - 96KHz playback for DVD - use pitch of 2.0.
30 * - Retain DMA buffer on close, do not wait the end of frame.
31 * - Resolve XXX tagged questions.
32 * - Cannot play 5133Hz.
33 * - 2001/01/07 Consider if we can remove voice_lock, like so:
34 * : Allocate/deallocate voices in open/close under semafore.
35 * : We access voices in interrupt, that only for pcms that open.
36 * voice_lock around playback_prepare closes interrupts for insane duration.
37 * - Revisit the way voice_alloc is done - too confusing, overcomplicated.
38 * Should support various channel types, however.
39 * - Remove prog_dmabuf from read/write, leave it in open.
40 * - 2001/01/07 Replace the OPL3 part of CONFIG_SOUND_YMFPCI_LEGACY code with
41 * native synthesizer through a playback slot.
42 * - 2001/11/29 ac97_save_state
43 * Talk to Kai to remove ac97_save_state before it's too late!
45 * - Restore S/PDIF - Toshibas have it.
47 * Kai used pci_alloc_consistent for DMA buffer, which sounds a little
48 * unconventional. However, given how small our fragments can be,
49 * a little uncached access is perhaps better than endless flushing.
50 * On i386 and other I/O-coherent architectures pci_alloc_consistent
51 * is entirely harmless.
54 #include <linux/config.h>
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/interrupt.h>
58 #include <linux/ioport.h>
59 #include <linux/delay.h>
60 #include <linux/pci.h>
61 #include <linux/slab.h>
62 #include <linux/poll.h>
63 #include <linux/soundcard.h>
64 #include <linux/ac97_codec.h>
65 #include <linux/sound.h>
69 #include <asm/uaccess.h>
71 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
72 # include "sound_config.h"
78 * I do not believe in debug levels as I never can guess what
79 * part of the code is going to be problematic in the future.
80 * Don't forget to run your klogd with -c 8.
82 * Example (do not remove):
83 * #define YMFDBG(fmt, arg...) do{ printk(KERN_DEBUG fmt, ##arg); }while(0)
85 #define YMFDBGW(fmt, arg...) /* */ /* write counts */
86 #define YMFDBGI(fmt, arg...) /* */ /* interrupts */
87 #define YMFDBGX(fmt, arg...) /* */ /* ioctl */
89 static int ymf_playback_trigger(ymfpci_t *unit, struct ymf_pcm *ypcm, int cmd);
90 static void ymf_capture_trigger(ymfpci_t *unit, struct ymf_pcm *ypcm, int cmd);
91 static void ymfpci_voice_free(ymfpci_t *unit, ymfpci_voice_t *pvoice);
92 static int ymf_capture_alloc(struct ymf_unit *unit, int *pbank);
93 static int ymf_playback_prepare(struct ymf_state *state);
94 static int ymf_capture_prepare(struct ymf_state *state);
95 static struct ymf_state *ymf_state_alloc(ymfpci_t *unit);
97 static void ymfpci_aclink_reset(struct pci_dev * pci);
98 static void ymfpci_disable_dsp(ymfpci_t *unit);
99 static void ymfpci_download_image(ymfpci_t *codec);
100 static void ymf_memload(ymfpci_t *unit);
102 static spinlock_t ymf_devs_lock = SPIN_LOCK_UNLOCKED;
103 static LIST_HEAD(ymf_devs);
109 static struct pci_device_id ymf_id_tbl[] = {
110 #define DEV(v, d, data) \
111 { PCI_VENDOR_ID_##v, PCI_DEVICE_ID_##v##_##d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)data }
112 DEV (YAMAHA, 724, "YMF724"),
113 DEV (YAMAHA, 724F, "YMF724F"),
114 DEV (YAMAHA, 740, "YMF740"),
115 DEV (YAMAHA, 740C, "YMF740C"),
116 DEV (YAMAHA, 744, "YMF744"),
117 DEV (YAMAHA, 754, "YMF754"),
121 MODULE_DEVICE_TABLE(pci, ymf_id_tbl);
124 * common I/O routines
127 static inline u8 ymfpci_readb(ymfpci_t *codec, u32 offset)
129 return readb(codec->reg_area_virt + offset);
132 static inline void ymfpci_writeb(ymfpci_t *codec, u32 offset, u8 val)
134 writeb(val, codec->reg_area_virt + offset);
137 static inline u16 ymfpci_readw(ymfpci_t *codec, u32 offset)
139 return readw(codec->reg_area_virt + offset);
142 static inline void ymfpci_writew(ymfpci_t *codec, u32 offset, u16 val)
144 writew(val, codec->reg_area_virt + offset);
147 static inline u32 ymfpci_readl(ymfpci_t *codec, u32 offset)
149 return readl(codec->reg_area_virt + offset);
152 static inline void ymfpci_writel(ymfpci_t *codec, u32 offset, u32 val)
154 writel(val, codec->reg_area_virt + offset);
157 static int ymfpci_codec_ready(ymfpci_t *codec, int secondary, int sched)
159 signed long end_time;
160 u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
162 end_time = jiffies + 3 * (HZ / 4);
164 if ((ymfpci_readw(codec, reg) & 0x8000) == 0)
167 set_current_state(TASK_UNINTERRUPTIBLE);
170 } while (end_time - (signed long)jiffies >= 0);
171 printk(KERN_ERR "ymfpci_codec_ready: codec %i is not ready [0x%x]\n",
172 secondary, ymfpci_readw(codec, reg));
176 static void ymfpci_codec_write(struct ac97_codec *dev, u8 reg, u16 val)
178 ymfpci_t *codec = dev->private_data;
181 spin_lock(&codec->ac97_lock);
182 /* XXX Do make use of dev->id */
183 ymfpci_codec_ready(codec, 0, 0);
184 cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
185 ymfpci_writel(codec, YDSXGR_AC97CMDDATA, cmd);
186 spin_unlock(&codec->ac97_lock);
189 static u16 _ymfpci_codec_read(ymfpci_t *unit, u8 reg)
193 if (ymfpci_codec_ready(unit, 0, 0))
195 ymfpci_writew(unit, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
196 if (ymfpci_codec_ready(unit, 0, 0))
198 if (unit->pci->device == PCI_DEVICE_ID_YAMAHA_744 && unit->rev < 2) {
199 for (i = 0; i < 600; i++)
200 ymfpci_readw(unit, YDSXGR_PRISTATUSDATA);
202 return ymfpci_readw(unit, YDSXGR_PRISTATUSDATA);
205 static u16 ymfpci_codec_read(struct ac97_codec *dev, u8 reg)
207 ymfpci_t *unit = dev->private_data;
210 spin_lock(&unit->ac97_lock);
211 ret = _ymfpci_codec_read(unit, reg);
212 spin_unlock(&unit->ac97_lock);
222 * Calculate the actual sampling rate relatetively to the base clock (48kHz).
224 static u32 ymfpci_calc_delta(u32 rate)
227 case 8000: return 0x02aaab00;
228 case 11025: return 0x03accd00;
229 case 16000: return 0x05555500;
230 case 22050: return 0x07599a00;
231 case 32000: return 0x0aaaab00;
232 case 44100: return 0x0eb33300;
233 default: return ((rate << 16) / 48000) << 12;
237 static u32 def_rate[8] = {
238 100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
241 static u32 ymfpci_calc_lpfK(u32 rate)
244 static u32 val[8] = {
245 0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
246 0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
250 return 0x40000000; /* FIXME: What's the right value? */
251 for (i = 0; i < 8; i++)
252 if (rate <= def_rate[i])
257 static u32 ymfpci_calc_lpfQ(u32 rate)
260 static u32 val[8] = {
261 0x35280000, 0x34A70000, 0x32020000, 0x31770000,
262 0x31390000, 0x31C90000, 0x33D00000, 0x40000000
267 for (i = 0; i < 8; i++)
268 if (rate <= def_rate[i])
273 static u32 ymf_calc_lend(u32 rate)
275 return (rate * YMF_SAMPF) / 48000;
279 * We ever allow only a few formats, but let's be generic, for smaller surprise.
281 static int ymf_pcm_format_width(int format)
283 static int mask16 = AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE;
285 if ((format & (format-1)) != 0) {
286 printk(KERN_ERR "ymfpci: format 0x%x is not a power of 2\n", format);
290 if (format == AFMT_IMA_ADPCM) return 4;
291 if ((format & mask16) != 0) return 16;
295 static void ymf_pcm_update_shift(struct ymf_pcm_format *f)
300 if (ymf_pcm_format_width(f->format) == 16)
304 /* Are you sure 32K is not too much? See if mpg123 skips on loaded systems. */
305 #define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
306 #define DMABUF_MINORDER 1
309 * Allocate DMA buffer
311 static int alloc_dmabuf(ymfpci_t *unit, struct ymf_dmabuf *dmabuf)
316 struct page *map, *mapend;
318 /* alloc as big a chunk as we can */
319 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
320 rawbuf = pci_alloc_consistent(unit->pci, PAGE_SIZE << order, &dma_addr);
328 printk(KERN_DEBUG "ymfpci: allocated %ld (order = %d) bytes at %p\n",
329 PAGE_SIZE << order, order, rawbuf);
332 dmabuf->ready = dmabuf->mapped = 0;
333 dmabuf->rawbuf = rawbuf;
334 dmabuf->dma_addr = dma_addr;
335 dmabuf->buforder = order;
337 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
338 mapend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
339 for (map = virt_to_page(rawbuf); map <= mapend; map++)
340 set_bit(PG_reserved, &map->flags);
348 static void dealloc_dmabuf(ymfpci_t *unit, struct ymf_dmabuf *dmabuf)
350 struct page *map, *mapend;
352 if (dmabuf->rawbuf) {
353 /* undo marking the pages as reserved */
354 mapend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
355 for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
356 clear_bit(PG_reserved, &map->flags);
358 pci_free_consistent(unit->pci, PAGE_SIZE << dmabuf->buforder,
359 dmabuf->rawbuf, dmabuf->dma_addr);
361 dmabuf->rawbuf = NULL;
362 dmabuf->mapped = dmabuf->ready = 0;
365 static int prog_dmabuf(struct ymf_state *state, int rec)
367 struct ymf_dmabuf *dmabuf;
371 int redzone, redfrags;
374 w_16 = ymf_pcm_format_width(state->format.format) == 16;
375 dmabuf = rec ? &state->rpcm.dmabuf : &state->wpcm.dmabuf;
377 spin_lock_irqsave(&state->unit->reg_lock, flags);
378 dmabuf->hwptr = dmabuf->swptr = 0;
379 dmabuf->total_bytes = 0;
381 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
383 /* allocate DMA buffer if not allocated yet */
385 if ((ret = alloc_dmabuf(state->unit, dmabuf)))
389 * Create fake fragment sizes and numbers for OSS ioctls.
390 * Import what Doom might have set with SNDCTL_DSP_SETFRAGMENT.
392 bufsize = PAGE_SIZE << dmabuf->buforder;
393 /* By default we give 4 big buffers. */
394 dmabuf->fragshift = (dmabuf->buforder + PAGE_SHIFT - 2);
395 if (dmabuf->ossfragshift > 3 &&
396 dmabuf->ossfragshift < dmabuf->fragshift) {
397 /* If OSS set smaller fragments, give more smaller buffers. */
398 dmabuf->fragshift = dmabuf->ossfragshift;
400 dmabuf->fragsize = 1 << dmabuf->fragshift;
402 dmabuf->numfrag = bufsize >> dmabuf->fragshift;
403 dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
405 if (dmabuf->ossmaxfrags >= 2) {
406 redzone = ymf_calc_lend(state->format.rate);
407 redzone <<= state->format.shift;
409 redfrags = (redzone + dmabuf->fragsize-1) >> dmabuf->fragshift;
411 if (dmabuf->ossmaxfrags + redfrags < dmabuf->numfrag) {
412 dmabuf->numfrag = dmabuf->ossmaxfrags + redfrags;
413 dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
417 memset(dmabuf->rawbuf, w_16 ? 0 : 0x80, dmabuf->dmasize);
420 * Now set up the ring
423 /* XXX ret = rec? cap_pre(): pbk_pre(); */
424 spin_lock_irqsave(&state->unit->voice_lock, flags);
426 if ((ret = ymf_capture_prepare(state)) != 0) {
427 spin_unlock_irqrestore(&state->unit->voice_lock, flags);
431 if ((ret = ymf_playback_prepare(state)) != 0) {
432 spin_unlock_irqrestore(&state->unit->voice_lock, flags);
436 spin_unlock_irqrestore(&state->unit->voice_lock, flags);
438 /* set the ready flag for the dma buffer (this comment is not stupid) */
442 printk(KERN_DEBUG "prog_dmabuf: rate %d format 0x%x,"
443 " numfrag %d fragsize %d dmasize %d\n",
444 state->format.rate, state->format.format, dmabuf->numfrag,
445 dmabuf->fragsize, dmabuf->dmasize);
451 static void ymf_start_dac(struct ymf_state *state)
453 ymf_playback_trigger(state->unit, &state->wpcm, 1);
456 // static void ymf_start_adc(struct ymf_state *state)
458 // ymf_capture_trigger(state->unit, &state->rpcm, 1);
462 * Wait until output is drained.
463 * This does not kill the hardware for the sake of ioctls.
465 static void ymf_wait_dac(struct ymf_state *state)
467 struct ymf_unit *unit = state->unit;
468 struct ymf_pcm *ypcm = &state->wpcm;
469 DECLARE_WAITQUEUE(waita, current);
472 add_wait_queue(&ypcm->dmabuf.wait, &waita);
474 spin_lock_irqsave(&unit->reg_lock, flags);
475 if (ypcm->dmabuf.count != 0 && !ypcm->running) {
476 ymf_playback_trigger(unit, ypcm, 1);
480 if (file->f_flags & O_NONBLOCK) {
482 * XXX Our mistake is to attach DMA buffer to state
483 * rather than to some per-device structure.
484 * Cannot skip waiting, can only make it shorter.
489 set_current_state(TASK_UNINTERRUPTIBLE);
490 while (ypcm->running) {
491 spin_unlock_irqrestore(&unit->reg_lock, flags);
493 spin_lock_irqsave(&unit->reg_lock, flags);
494 set_current_state(TASK_UNINTERRUPTIBLE);
496 spin_unlock_irqrestore(&unit->reg_lock, flags);
498 set_current_state(TASK_RUNNING);
499 remove_wait_queue(&ypcm->dmabuf.wait, &waita);
502 * This function may take up to 4 seconds to reach this point
503 * (32K circular buffer, 8000 Hz). User notices.
507 /* Can just stop, without wait. Or can we? */
508 static void ymf_stop_adc(struct ymf_state *state)
510 struct ymf_unit *unit = state->unit;
513 spin_lock_irqsave(&unit->reg_lock, flags);
514 ymf_capture_trigger(unit, &state->rpcm, 0);
515 spin_unlock_irqrestore(&unit->reg_lock, flags);
519 * Hardware start management
522 static void ymfpci_hw_start(ymfpci_t *unit)
526 spin_lock_irqsave(&unit->reg_lock, flags);
527 if (unit->start_count++ == 0) {
528 ymfpci_writel(unit, YDSXGR_MODE,
529 ymfpci_readl(unit, YDSXGR_MODE) | 3);
530 unit->active_bank = ymfpci_readl(unit, YDSXGR_CTRLSELECT) & 1;
532 spin_unlock_irqrestore(&unit->reg_lock, flags);
535 static void ymfpci_hw_stop(ymfpci_t *unit)
540 spin_lock_irqsave(&unit->reg_lock, flags);
541 if (--unit->start_count == 0) {
542 ymfpci_writel(unit, YDSXGR_MODE,
543 ymfpci_readl(unit, YDSXGR_MODE) & ~3);
544 while (timeout-- > 0) {
545 if ((ymfpci_readl(unit, YDSXGR_STATUS) & 2) == 0)
549 spin_unlock_irqrestore(&unit->reg_lock, flags);
553 * Playback voice management
556 static int voice_alloc(ymfpci_t *codec, ymfpci_voice_type_t type, int pair, ymfpci_voice_t *rvoice[])
558 ymfpci_voice_t *voice, *voice2;
561 for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
562 voice = &codec->voices[idx];
563 voice2 = pair ? &codec->voices[idx+1] : NULL;
564 if (voice->use || (voice2 && voice2->use))
582 ymfpci_hw_start(codec);
585 ymfpci_hw_start(codec);
590 return -EBUSY; /* Your audio channel is open by someone else. */
593 static void ymfpci_voice_free(ymfpci_t *unit, ymfpci_voice_t *pvoice)
595 ymfpci_hw_stop(unit);
596 pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
603 static void ymf_pcm_interrupt(ymfpci_t *codec, ymfpci_voice_t *voice)
605 struct ymf_pcm *ypcm;
607 int pos, delta, swptr;
608 int played, distance;
609 struct ymf_state *state;
610 struct ymf_dmabuf *dmabuf;
613 if ((ypcm = voice->ypcm) == NULL) {
616 if ((state = ypcm->state) == NULL) {
617 ypcm->running = 0; // lock it
620 dmabuf = &ypcm->dmabuf;
621 spin_lock(&codec->reg_lock);
623 YMFDBGI("ymfpci: %d, intr bank %d count %d start 0x%x:%x\n",
624 voice->number, codec->active_bank, dmabuf->count,
625 le32_to_cpu(voice->bank[0].start),
626 le32_to_cpu(voice->bank[1].start));
627 silence = (ymf_pcm_format_width(state->format.format) == 16) ?
629 /* We need actual left-hand-side redzone size here. */
630 redzone = ymf_calc_lend(state->format.rate);
631 redzone <<= (state->format.shift + 1);
632 swptr = dmabuf->swptr;
634 pos = le32_to_cpu(voice->bank[codec->active_bank].start);
635 pos <<= state->format.shift;
636 if (pos < 0 || pos >= dmabuf->dmasize) { /* ucode bug */
637 printk(KERN_ERR "ymfpci%d: runaway voice %d: hwptr %d=>%d dmasize %d\n",
638 codec->dev_audio, voice->number,
639 dmabuf->hwptr, pos, dmabuf->dmasize);
642 if (pos < dmabuf->hwptr) {
643 delta = dmabuf->dmasize - dmabuf->hwptr;
644 memset(dmabuf->rawbuf + dmabuf->hwptr, silence, delta);
646 memset(dmabuf->rawbuf, silence, pos);
648 delta = pos - dmabuf->hwptr;
649 memset(dmabuf->rawbuf + dmabuf->hwptr, silence, delta);
653 if (dmabuf->count == 0) {
654 printk(KERN_ERR "ymfpci%d: %d: strain: hwptr %d\n",
655 codec->dev_audio, voice->number, dmabuf->hwptr);
656 ymf_playback_trigger(codec, ypcm, 0);
660 distance = pos - swptr;
662 distance = dmabuf->dmasize - (swptr - pos);
664 if (distance < redzone) {
666 * hwptr inside redzone => DMA ran out of samples.
668 if (delta < dmabuf->count) {
670 * Lost interrupt or other screwage.
672 printk(KERN_ERR "ymfpci%d: %d: lost: delta %d"
673 " hwptr %d swptr %d distance %d count %d\n",
674 codec->dev_audio, voice->number, delta,
675 dmabuf->hwptr, swptr, distance, dmabuf->count);
680 YMFDBGI("ymfpci%d: %d: done: delta %d"
681 " hwptr %d swptr %d distance %d count %d\n",
682 codec->dev_audio, voice->number, delta,
683 dmabuf->hwptr, swptr, distance, dmabuf->count);
685 played = dmabuf->count;
687 ymf_playback_trigger(codec, ypcm, 0);
691 * hwptr is chipping away towards a remote swptr.
692 * Calculate other distance and apply it to count.
695 distance = swptr - pos;
697 distance = dmabuf->dmasize - (pos - swptr);
699 if (distance < dmabuf->count) {
700 played = dmabuf->count - distance;
706 dmabuf->total_bytes += played;
707 dmabuf->count -= played;
708 if (dmabuf->count < dmabuf->dmasize / 2) {
709 wake_up(&dmabuf->wait);
712 spin_unlock(&codec->reg_lock);
715 static void ymf_cap_interrupt(ymfpci_t *unit, struct ymf_capture *cap)
717 struct ymf_pcm *ypcm;
719 struct ymf_state *state;
720 struct ymf_dmabuf *dmabuf;
724 if ((ypcm = cap->ypcm) == NULL) {
727 if ((state = ypcm->state) == NULL) {
728 ypcm->running = 0; // lock it
731 dmabuf = &ypcm->dmabuf;
732 spin_lock(&unit->reg_lock);
734 redzone = ymf_calc_lend(state->format.rate);
735 redzone <<= (state->format.shift + 1);
737 pos = le32_to_cpu(cap->bank[unit->active_bank].start);
738 // pos <<= state->format.shift;
739 if (pos < 0 || pos >= dmabuf->dmasize) { /* ucode bug */
740 printk(KERN_ERR "ymfpci%d: runaway capture %d: hwptr %d=>%d dmasize %d\n",
741 unit->dev_audio, ypcm->capture_bank_number,
742 dmabuf->hwptr, pos, dmabuf->dmasize);
745 if (pos < dmabuf->hwptr) {
746 delta = dmabuf->dmasize - dmabuf->hwptr;
749 delta = pos - dmabuf->hwptr;
755 if (cnt + redzone > dmabuf->dmasize) {
756 /* Overflow - bump swptr */
757 dmabuf->count = dmabuf->dmasize - redzone;
758 dmabuf->swptr = dmabuf->hwptr + redzone;
759 if (dmabuf->swptr >= dmabuf->dmasize) {
760 dmabuf->swptr -= dmabuf->dmasize;
766 dmabuf->total_bytes += delta;
767 if (dmabuf->count) { /* && is_sleeping XXX */
768 wake_up(&dmabuf->wait);
771 spin_unlock(&unit->reg_lock);
774 static int ymf_playback_trigger(ymfpci_t *codec, struct ymf_pcm *ypcm, int cmd)
777 if (ypcm->voices[0] == NULL) {
781 codec->ctrl_playback[ypcm->voices[0]->number + 1] =
782 cpu_to_le32(ypcm->voices[0]->bank_ba);
783 if (ypcm->voices[1] != NULL)
784 codec->ctrl_playback[ypcm->voices[1]->number + 1] =
785 cpu_to_le32(ypcm->voices[1]->bank_ba);
788 codec->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
789 if (ypcm->voices[1] != NULL)
790 codec->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
796 static void ymf_capture_trigger(ymfpci_t *codec, struct ymf_pcm *ypcm, int cmd)
801 tmp = ymfpci_readl(codec, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
802 ymfpci_writel(codec, YDSXGR_MAPOFREC, tmp);
805 tmp = ymfpci_readl(codec, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
806 ymfpci_writel(codec, YDSXGR_MAPOFREC, tmp);
811 static int ymfpci_pcm_voice_alloc(struct ymf_pcm *ypcm, int voices)
813 struct ymf_unit *unit;
816 unit = ypcm->state->unit;
817 if (ypcm->voices[1] != NULL && voices < 2) {
818 ymfpci_voice_free(unit, ypcm->voices[1]);
819 ypcm->voices[1] = NULL;
821 if (voices == 1 && ypcm->voices[0] != NULL)
822 return 0; /* already allocated */
823 if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
824 return 0; /* already allocated */
826 if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
827 ymfpci_voice_free(unit, ypcm->voices[0]);
828 ypcm->voices[0] = NULL;
830 if ((err = voice_alloc(unit, YMFPCI_PCM, 1, ypcm->voices)) < 0)
832 ypcm->voices[0]->ypcm = ypcm;
833 ypcm->voices[1]->ypcm = ypcm;
835 if ((err = voice_alloc(unit, YMFPCI_PCM, 0, ypcm->voices)) < 0)
837 ypcm->voices[0]->ypcm = ypcm;
842 static void ymf_pcm_init_voice(ymfpci_voice_t *voice, int stereo,
843 int rate, int w_16, unsigned long addr, unsigned int end, int spdif)
846 u32 delta = ymfpci_calc_delta(rate);
847 u32 lpfQ = ymfpci_calc_lpfQ(rate);
848 u32 lpfK = ymfpci_calc_lpfK(rate);
849 ymfpci_playback_bank_t *bank;
853 * The gain is a floating point number. According to the manual,
854 * bit 31 indicates a sign bit, bit 30 indicates an integer part,
855 * and bits [29:15] indicate a decimal fraction part. Thus,
856 * for a gain of 1.0 the constant of 0x40000000 is loaded.
858 unsigned default_gain = cpu_to_le32(0x40000000);
860 format = (stereo ? 0x00010000 : 0) | (w_16 ? 0 : 0x80000000);
865 for (nbank = 0; nbank < 2; nbank++) {
866 bank = &voice->bank[nbank];
867 bank->format = cpu_to_le32(format);
868 bank->loop_default = 0; /* 0-loops forever, otherwise count */
869 bank->base = cpu_to_le32(addr);
870 bank->loop_start = 0;
871 bank->loop_end = cpu_to_le32(end);
873 bank->eg_gain_end = default_gain;
874 bank->lpfQ = cpu_to_le32(lpfQ);
876 bank->num_of_frames = 0;
877 bank->loop_count = 0;
879 bank->start_frac = 0;
881 bank->delta_end = cpu_to_le32(delta);
883 bank->lpfK_end = cpu_to_le32(lpfK);
884 bank->eg_gain = default_gain;
890 bank->left_gain_end =
891 bank->right_gain_end =
895 bank->eff1_gain_end =
896 bank->eff2_gain_end =
897 bank->eff3_gain_end = 0;
903 bank->left_gain_end =
904 bank->right_gain_end = default_gain;
907 bank->eff2_gain_end =
909 bank->eff3_gain_end = default_gain;
913 if ((voice->number & 1) == 0) {
915 bank->left_gain_end = default_gain;
917 bank->format |= cpu_to_le32(1);
919 bank->right_gain_end = default_gain;
922 if ((voice->number & 1) == 0) {
924 bank->eff2_gain_end = default_gain;
926 bank->format |= cpu_to_le32(1);
928 bank->eff3_gain_end = default_gain;
936 * XXX Capture channel allocation is entirely fake at the moment.
937 * We use only one channel and mark it busy as required.
939 static int ymf_capture_alloc(struct ymf_unit *unit, int *pbank)
941 struct ymf_capture *cap;
944 cbank = 1; /* Only ADC slot is used for now. */
945 cap = &unit->capture[cbank];
953 static int ymf_playback_prepare(struct ymf_state *state)
955 struct ymf_pcm *ypcm = &state->wpcm;
958 if ((err = ymfpci_pcm_voice_alloc(ypcm, state->format.voices)) < 0) {
959 /* Somebody started 32 mpg123's in parallel? */
960 printk(KERN_INFO "ymfpci%d: cannot allocate voice\n",
961 state->unit->dev_audio);
965 for (nvoice = 0; nvoice < state->format.voices; nvoice++) {
966 ymf_pcm_init_voice(ypcm->voices[nvoice],
967 state->format.voices == 2, state->format.rate,
968 ymf_pcm_format_width(state->format.format) == 16,
969 ypcm->dmabuf.dma_addr, ypcm->dmabuf.dmasize,
975 static int ymf_capture_prepare(struct ymf_state *state)
977 ymfpci_t *unit = state->unit;
978 struct ymf_pcm *ypcm = &state->rpcm;
979 ymfpci_capture_bank_t * bank;
980 /* XXX This is confusing, gotta rename one of them banks... */
981 int nbank; /* flip-flop bank */
982 int cbank; /* input [super-]bank */
983 struct ymf_capture *cap;
986 if (ypcm->capture_bank_number == -1) {
987 if (ymf_capture_alloc(unit, &cbank) != 0)
990 ypcm->capture_bank_number = cbank;
992 cap = &unit->capture[cbank];
993 cap->bank = unit->bank_capture[cbank][0];
995 ymfpci_hw_start(unit);
998 // ypcm->frag_size = snd_pcm_lib_transfer_fragment(substream);
999 // frag_size is replaced with nonfragged byte-aligned rolling buffer
1000 rate = ((48000 * 4096) / state->format.rate) - 1;
1002 if (state->format.voices == 2)
1004 if (ymf_pcm_format_width(state->format.format) == 8)
1006 switch (ypcm->capture_bank_number) {
1008 ymfpci_writel(unit, YDSXGR_RECFORMAT, format);
1009 ymfpci_writel(unit, YDSXGR_RECSLOTSR, rate);
1012 ymfpci_writel(unit, YDSXGR_ADCFORMAT, format);
1013 ymfpci_writel(unit, YDSXGR_ADCSLOTSR, rate);
1016 for (nbank = 0; nbank < 2; nbank++) {
1017 bank = unit->bank_capture[ypcm->capture_bank_number][nbank];
1018 bank->base = cpu_to_le32(ypcm->dmabuf.dma_addr);
1019 // bank->loop_end = ypcm->dmabuf.dmasize >> state->format.shift;
1020 bank->loop_end = cpu_to_le32(ypcm->dmabuf.dmasize);
1022 bank->num_of_loops = 0;
1025 if (state->digital.dig_valid)
1026 /*state->digital.type == SND_PCM_DIG_AES_IEC958*/
1027 ymfpci_writew(codec, YDSXGR_SPDIFOUTSTATUS,
1028 state->digital.dig_status[0] | (state->digital.dig_status[1] << 8));
1033 static irqreturn_t ymf_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1035 ymfpci_t *codec = dev_id;
1036 u32 status, nvoice, mode;
1037 struct ymf_voice *voice;
1038 struct ymf_capture *cap;
1040 status = ymfpci_readl(codec, YDSXGR_STATUS);
1041 if (status & 0x80000000) {
1042 codec->active_bank = ymfpci_readl(codec, YDSXGR_CTRLSELECT) & 1;
1043 spin_lock(&codec->voice_lock);
1044 for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
1045 voice = &codec->voices[nvoice];
1047 ymf_pcm_interrupt(codec, voice);
1049 for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
1050 cap = &codec->capture[nvoice];
1052 ymf_cap_interrupt(codec, cap);
1054 spin_unlock(&codec->voice_lock);
1055 spin_lock(&codec->reg_lock);
1056 ymfpci_writel(codec, YDSXGR_STATUS, 0x80000000);
1057 mode = ymfpci_readl(codec, YDSXGR_MODE) | 2;
1058 ymfpci_writel(codec, YDSXGR_MODE, mode);
1059 spin_unlock(&codec->reg_lock);
1062 status = ymfpci_readl(codec, YDSXGR_INTFLAG);
1065 ymfpci_writel(codec, YDSXGR_INTFLAG, ~0);
1070 static void ymf_pcm_free_substream(struct ymf_pcm *ypcm)
1072 unsigned long flags;
1073 struct ymf_unit *unit;
1075 unit = ypcm->state->unit;
1077 if (ypcm->type == PLAYBACK_VOICE) {
1078 spin_lock_irqsave(&unit->voice_lock, flags);
1079 if (ypcm->voices[1])
1080 ymfpci_voice_free(unit, ypcm->voices[1]);
1081 if (ypcm->voices[0])
1082 ymfpci_voice_free(unit, ypcm->voices[0]);
1083 spin_unlock_irqrestore(&unit->voice_lock, flags);
1085 if (ypcm->capture_bank_number != -1) {
1086 unit->capture[ypcm->capture_bank_number].use = 0;
1087 ypcm->capture_bank_number = -1;
1088 ymfpci_hw_stop(unit);
1093 static struct ymf_state *ymf_state_alloc(ymfpci_t *unit)
1095 struct ymf_pcm *ypcm;
1096 struct ymf_state *state;
1098 if ((state = kmalloc(sizeof(struct ymf_state), GFP_KERNEL)) == NULL) {
1101 memset(state, 0, sizeof(struct ymf_state));
1103 ypcm = &state->wpcm;
1104 ypcm->state = state;
1105 ypcm->type = PLAYBACK_VOICE;
1106 ypcm->capture_bank_number = -1;
1107 init_waitqueue_head(&ypcm->dmabuf.wait);
1109 ypcm = &state->rpcm;
1110 ypcm->state = state;
1111 ypcm->type = CAPTURE_AC97;
1112 ypcm->capture_bank_number = -1;
1113 init_waitqueue_head(&ypcm->dmabuf.wait);
1117 state->format.format = AFMT_U8;
1118 state->format.rate = 8000;
1119 state->format.voices = 1;
1120 ymf_pcm_update_shift(&state->format);
1128 /* AES/IEC958 channel status bits */
1129 #define SND_PCM_AES0_PROFESSIONAL (1<<0) /* 0 = consumer, 1 = professional */
1130 #define SND_PCM_AES0_NONAUDIO (1<<1) /* 0 = audio, 1 = non-audio */
1131 #define SND_PCM_AES0_PRO_EMPHASIS (7<<2) /* mask - emphasis */
1132 #define SND_PCM_AES0_PRO_EMPHASIS_NOTID (0<<2) /* emphasis not indicated */
1133 #define SND_PCM_AES0_PRO_EMPHASIS_NONE (1<<2) /* none emphasis */
1134 #define SND_PCM_AES0_PRO_EMPHASIS_5015 (3<<2) /* 50/15us emphasis */
1135 #define SND_PCM_AES0_PRO_EMPHASIS_CCITT (7<<2) /* CCITT J.17 emphasis */
1136 #define SND_PCM_AES0_PRO_FREQ_UNLOCKED (1<<5) /* source sample frequency: 0 = locked, 1 = unlocked */
1137 #define SND_PCM_AES0_PRO_FS (3<<6) /* mask - sample frequency */
1138 #define SND_PCM_AES0_PRO_FS_NOTID (0<<6) /* fs not indicated */
1139 #define SND_PCM_AES0_PRO_FS_44100 (1<<6) /* 44.1kHz */
1140 #define SND_PCM_AES0_PRO_FS_48000 (2<<6) /* 48kHz */
1141 #define SND_PCM_AES0_PRO_FS_32000 (3<<6) /* 32kHz */
1142 #define SND_PCM_AES0_CON_NOT_COPYRIGHT (1<<2) /* 0 = copyright, 1 = not copyright */
1143 #define SND_PCM_AES0_CON_EMPHASIS (7<<3) /* mask - emphasis */
1144 #define SND_PCM_AES0_CON_EMPHASIS_NONE (0<<3) /* none emphasis */
1145 #define SND_PCM_AES0_CON_EMPHASIS_5015 (1<<3) /* 50/15us emphasis */
1146 #define SND_PCM_AES0_CON_MODE (3<<6) /* mask - mode */
1147 #define SND_PCM_AES1_PRO_MODE (15<<0) /* mask - channel mode */
1148 #define SND_PCM_AES1_PRO_MODE_NOTID (0<<0) /* not indicated */
1149 #define SND_PCM_AES1_PRO_MODE_STEREOPHONIC (2<<0) /* stereophonic - ch A is left */
1150 #define SND_PCM_AES1_PRO_MODE_SINGLE (4<<0) /* single channel */
1151 #define SND_PCM_AES1_PRO_MODE_TWO (8<<0) /* two channels */
1152 #define SND_PCM_AES1_PRO_MODE_PRIMARY (12<<0) /* primary/secondary */
1153 #define SND_PCM_AES1_PRO_MODE_BYTE3 (15<<0) /* vector to byte 3 */
1154 #define SND_PCM_AES1_PRO_USERBITS (15<<4) /* mask - user bits */
1155 #define SND_PCM_AES1_PRO_USERBITS_NOTID (0<<4) /* not indicated */
1156 #define SND_PCM_AES1_PRO_USERBITS_192 (8<<4) /* 192-bit structure */
1157 #define SND_PCM_AES1_PRO_USERBITS_UDEF (12<<4) /* user defined application */
1158 #define SND_PCM_AES1_CON_CATEGORY 0x7f
1159 #define SND_PCM_AES1_CON_GENERAL 0x00
1160 #define SND_PCM_AES1_CON_EXPERIMENTAL 0x40
1161 #define SND_PCM_AES1_CON_SOLIDMEM_MASK 0x0f
1162 #define SND_PCM_AES1_CON_SOLIDMEM_ID 0x08
1163 #define SND_PCM_AES1_CON_BROADCAST1_MASK 0x07
1164 #define SND_PCM_AES1_CON_BROADCAST1_ID 0x04
1165 #define SND_PCM_AES1_CON_DIGDIGCONV_MASK 0x07
1166 #define SND_PCM_AES1_CON_DIGDIGCONV_ID 0x02
1167 #define SND_PCM_AES1_CON_ADC_COPYRIGHT_MASK 0x1f
1168 #define SND_PCM_AES1_CON_ADC_COPYRIGHT_ID 0x06
1169 #define SND_PCM_AES1_CON_ADC_MASK 0x1f
1170 #define SND_PCM_AES1_CON_ADC_ID 0x16
1171 #define SND_PCM_AES1_CON_BROADCAST2_MASK 0x0f
1172 #define SND_PCM_AES1_CON_BROADCAST2_ID 0x0e
1173 #define SND_PCM_AES1_CON_LASEROPT_MASK 0x07
1174 #define SND_PCM_AES1_CON_LASEROPT_ID 0x01
1175 #define SND_PCM_AES1_CON_MUSICAL_MASK 0x07
1176 #define SND_PCM_AES1_CON_MUSICAL_ID 0x05
1177 #define SND_PCM_AES1_CON_MAGNETIC_MASK 0x07
1178 #define SND_PCM_AES1_CON_MAGNETIC_ID 0x03
1179 #define SND_PCM_AES1_CON_IEC908_CD (SND_PCM_AES1_CON_LASEROPT_ID|0x00)
1180 #define SND_PCM_AES1_CON_NON_IEC908_CD (SND_PCM_AES1_CON_LASEROPT_ID|0x08)
1181 #define SND_PCM_AES1_CON_PCM_CODER (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x00)
1182 #define SND_PCM_AES1_CON_SAMPLER (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x20)
1183 #define SND_PCM_AES1_CON_MIXER (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x10)
1184 #define SND_PCM_AES1_CON_RATE_CONVERTER (SND_PCM_AES1_CON_DIGDIGCONV_ID|0x18)
1185 #define SND_PCM_AES1_CON_SYNTHESIZER (SND_PCM_AES1_CON_MUSICAL_ID|0x00)
1186 #define SND_PCM_AES1_CON_MICROPHONE (SND_PCM_AES1_CON_MUSICAL_ID|0x08)
1187 #define SND_PCM_AES1_CON_DAT (SND_PCM_AES1_CON_MAGNETIC_ID|0x00)
1188 #define SND_PCM_AES1_CON_VCR (SND_PCM_AES1_CON_MAGNETIC_ID|0x08)
1189 #define SND_PCM_AES1_CON_ORIGINAL (1<<7) /* this bits depends on the category code */
1190 #define SND_PCM_AES2_PRO_SBITS (7<<0) /* mask - sample bits */
1191 #define SND_PCM_AES2_PRO_SBITS_20 (2<<0) /* 20-bit - coordination */
1192 #define SND_PCM_AES2_PRO_SBITS_24 (4<<0) /* 24-bit - main audio */
1193 #define SND_PCM_AES2_PRO_SBITS_UDEF (6<<0) /* user defined application */
1194 #define SND_PCM_AES2_PRO_WORDLEN (7<<3) /* mask - source word length */
1195 #define SND_PCM_AES2_PRO_WORDLEN_NOTID (0<<3) /* not indicated */
1196 #define SND_PCM_AES2_PRO_WORDLEN_22_18 (2<<3) /* 22-bit or 18-bit */
1197 #define SND_PCM_AES2_PRO_WORDLEN_23_19 (4<<3) /* 23-bit or 19-bit */
1198 #define SND_PCM_AES2_PRO_WORDLEN_24_20 (5<<3) /* 24-bit or 20-bit */
1199 #define SND_PCM_AES2_PRO_WORDLEN_20_16 (6<<3) /* 20-bit or 16-bit */
1200 #define SND_PCM_AES2_CON_SOURCE (15<<0) /* mask - source number */
1201 #define SND_PCM_AES2_CON_SOURCE_UNSPEC (0<<0) /* unspecified */
1202 #define SND_PCM_AES2_CON_CHANNEL (15<<4) /* mask - channel number */
1203 #define SND_PCM_AES2_CON_CHANNEL_UNSPEC (0<<4) /* unspecified */
1204 #define SND_PCM_AES3_CON_FS (15<<0) /* mask - sample frequency */
1205 #define SND_PCM_AES3_CON_FS_44100 (0<<0) /* 44.1kHz */
1206 #define SND_PCM_AES3_CON_FS_48000 (2<<0) /* 48kHz */
1207 #define SND_PCM_AES3_CON_FS_32000 (3<<0) /* 32kHz */
1208 #define SND_PCM_AES3_CON_CLOCK (3<<4) /* mask - clock accuracy */
1209 #define SND_PCM_AES3_CON_CLOCK_1000PPM (0<<4) /* 1000 ppm */
1210 #define SND_PCM_AES3_CON_CLOCK_50PPM (1<<4) /* 50 ppm */
1211 #define SND_PCM_AES3_CON_CLOCK_VARIABLE (2<<4) /* variable pitch */
1218 * in this loop, dmabuf.count signifies the amount of data that is
1219 * waiting to be copied to the user's buffer. it is filled by the dma
1220 * machine and drained by this loop.
1223 ymf_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1225 struct ymf_state *state = (struct ymf_state *)file->private_data;
1226 struct ymf_dmabuf *dmabuf = &state->rpcm.dmabuf;
1227 struct ymf_unit *unit = state->unit;
1228 DECLARE_WAITQUEUE(waita, current);
1230 unsigned long flags;
1232 int cnt; /* This many to go in this revolution */
1234 if (ppos != &file->f_pos)
1238 if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
1242 add_wait_queue(&dmabuf->wait, &waita);
1243 set_current_state(TASK_INTERRUPTIBLE);
1245 spin_lock_irqsave(&unit->reg_lock, flags);
1246 if (unit->suspended) {
1247 spin_unlock_irqrestore(&unit->reg_lock, flags);
1249 set_current_state(TASK_INTERRUPTIBLE);
1250 if (signal_pending(current)) {
1251 if (!ret) ret = -EAGAIN;
1256 swptr = dmabuf->swptr;
1257 cnt = dmabuf->dmasize - swptr;
1258 if (dmabuf->count < cnt)
1259 cnt = dmabuf->count;
1260 spin_unlock_irqrestore(&unit->reg_lock, flags);
1266 /* buffer is empty, start the dma machine and wait for data to be
1268 spin_lock_irqsave(&state->unit->reg_lock, flags);
1269 if (!state->rpcm.running) {
1270 ymf_capture_trigger(state->unit, &state->rpcm, 1);
1272 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1273 if (file->f_flags & O_NONBLOCK) {
1274 if (!ret) ret = -EAGAIN;
1277 /* This isnt strictly right for the 810 but it'll do */
1278 tmo = (dmabuf->dmasize * HZ) / (state->format.rate * 2);
1279 tmo >>= state->format.shift;
1280 /* There are two situations when sleep_on_timeout returns, one is when
1281 the interrupt is serviced correctly and the process is waked up by
1282 ISR ON TIME. Another is when timeout is expired, which means that
1283 either interrupt is NOT serviced correctly (pending interrupt) or it
1284 is TOO LATE for the process to be scheduled to run (scheduler latency)
1285 which results in a (potential) buffer overrun. And worse, there is
1286 NOTHING we can do to prevent it. */
1287 tmo = schedule_timeout(tmo);
1288 spin_lock_irqsave(&state->unit->reg_lock, flags);
1289 set_current_state(TASK_INTERRUPTIBLE);
1290 if (tmo == 0 && dmabuf->count == 0) {
1291 printk(KERN_ERR "ymfpci%d: recording schedule timeout, "
1292 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1293 state->unit->dev_audio,
1294 dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1295 dmabuf->hwptr, dmabuf->swptr);
1297 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1298 if (signal_pending(current)) {
1299 if (!ret) ret = -ERESTARTSYS;
1305 if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1306 if (!ret) ret = -EFAULT;
1310 swptr = (swptr + cnt) % dmabuf->dmasize;
1312 spin_lock_irqsave(&unit->reg_lock, flags);
1313 if (unit->suspended) {
1314 spin_unlock_irqrestore(&unit->reg_lock, flags);
1318 dmabuf->swptr = swptr;
1319 dmabuf->count -= cnt;
1320 // spin_unlock_irqrestore(&unit->reg_lock, flags);
1325 // spin_lock_irqsave(&unit->reg_lock, flags);
1326 if (!state->rpcm.running) {
1327 ymf_capture_trigger(unit, &state->rpcm, 1);
1329 spin_unlock_irqrestore(&unit->reg_lock, flags);
1331 set_current_state(TASK_RUNNING);
1332 remove_wait_queue(&dmabuf->wait, &waita);
1338 ymf_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
1340 struct ymf_state *state = (struct ymf_state *)file->private_data;
1341 struct ymf_dmabuf *dmabuf = &state->wpcm.dmabuf;
1342 struct ymf_unit *unit = state->unit;
1343 DECLARE_WAITQUEUE(waita, current);
1345 unsigned long flags;
1347 int cnt; /* This many to go in this revolution */
1351 YMFDBGW("ymf_write: count %d\n", count);
1353 if (ppos != &file->f_pos)
1357 if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
1362 * Alan's cs46xx works without a red zone - marvel of ingenuity.
1363 * We are not so brilliant... Red zone does two things:
1364 * 1. allows for safe start after a pause as we have no way
1365 * to know what the actual, relentlessly advancing, hwptr is.
1366 * 2. makes computations in ymf_pcm_interrupt simpler.
1368 redzone = ymf_calc_lend(state->format.rate) << state->format.shift;
1369 redzone *= 3; /* 2 redzone + 1 possible uncertainty reserve. */
1371 add_wait_queue(&dmabuf->wait, &waita);
1372 set_current_state(TASK_INTERRUPTIBLE);
1374 spin_lock_irqsave(&unit->reg_lock, flags);
1375 if (unit->suspended) {
1376 spin_unlock_irqrestore(&unit->reg_lock, flags);
1378 set_current_state(TASK_INTERRUPTIBLE);
1379 if (signal_pending(current)) {
1380 if (!ret) ret = -EAGAIN;
1385 if (dmabuf->count < 0) {
1387 "ymf_write: count %d, was legal in cs46xx\n",
1391 if (dmabuf->count == 0) {
1392 swptr = dmabuf->hwptr;
1393 if (state->wpcm.running) {
1395 * Add uncertainty reserve.
1397 cnt = ymf_calc_lend(state->format.rate);
1398 cnt <<= state->format.shift;
1399 if ((swptr += cnt) >= dmabuf->dmasize) {
1400 swptr -= dmabuf->dmasize;
1403 dmabuf->swptr = swptr;
1406 * XXX This is not right if dmabuf->count is small -
1407 * about 2*x frame size or less. We cannot count on
1408 * on appending and not causing an artefact.
1409 * Should use a variation of the count==0 case above.
1411 swptr = dmabuf->swptr;
1413 cnt = dmabuf->dmasize - swptr;
1414 if (dmabuf->count + cnt > dmabuf->dmasize - redzone)
1415 cnt = (dmabuf->dmasize - redzone) - dmabuf->count;
1416 spin_unlock_irqrestore(&unit->reg_lock, flags);
1421 YMFDBGW("ymf_write: full, count %d swptr %d\n",
1422 dmabuf->count, dmabuf->swptr);
1424 * buffer is full, start the dma machine and
1425 * wait for data to be played
1427 spin_lock_irqsave(&unit->reg_lock, flags);
1428 if (!state->wpcm.running) {
1429 ymf_playback_trigger(unit, &state->wpcm, 1);
1431 spin_unlock_irqrestore(&unit->reg_lock, flags);
1432 if (file->f_flags & O_NONBLOCK) {
1433 if (!ret) ret = -EAGAIN;
1437 set_current_state(TASK_INTERRUPTIBLE);
1438 if (signal_pending(current)) {
1439 if (!ret) ret = -ERESTARTSYS;
1444 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
1445 if (!ret) ret = -EFAULT;
1449 if ((swptr += cnt) >= dmabuf->dmasize) {
1450 swptr -= dmabuf->dmasize;
1453 spin_lock_irqsave(&unit->reg_lock, flags);
1454 if (unit->suspended) {
1455 spin_unlock_irqrestore(&unit->reg_lock, flags);
1458 dmabuf->swptr = swptr;
1459 dmabuf->count += cnt;
1462 * Start here is a bad idea - may cause startup click
1463 * in /bin/play when dmabuf is not full yet.
1464 * However, some broken applications do not make
1465 * any use of SNDCTL_DSP_SYNC (Doom is the worst).
1466 * One frame is about 5.3ms, Doom write size is 46ms.
1468 delay = state->format.rate / 20; /* 50ms */
1469 delay <<= state->format.shift;
1470 if (dmabuf->count >= delay && !state->wpcm.running) {
1471 ymf_playback_trigger(unit, &state->wpcm, 1);
1474 spin_unlock_irqrestore(&unit->reg_lock, flags);
1481 set_current_state(TASK_RUNNING);
1482 remove_wait_queue(&dmabuf->wait, &waita);
1484 YMFDBGW("ymf_write: ret %d dmabuf.count %d\n", ret, dmabuf->count);
1488 static unsigned int ymf_poll(struct file *file, struct poll_table_struct *wait)
1490 struct ymf_state *state = (struct ymf_state *)file->private_data;
1491 struct ymf_dmabuf *dmabuf;
1493 unsigned long flags;
1494 unsigned int mask = 0;
1496 if (file->f_mode & FMODE_WRITE)
1497 poll_wait(file, &state->wpcm.dmabuf.wait, wait);
1498 if (file->f_mode & FMODE_READ)
1499 poll_wait(file, &state->rpcm.dmabuf.wait, wait);
1501 spin_lock_irqsave(&state->unit->reg_lock, flags);
1502 if (file->f_mode & FMODE_READ) {
1503 dmabuf = &state->rpcm.dmabuf;
1504 if (dmabuf->count >= (signed)dmabuf->fragsize)
1505 mask |= POLLIN | POLLRDNORM;
1507 if (file->f_mode & FMODE_WRITE) {
1508 redzone = ymf_calc_lend(state->format.rate);
1509 redzone <<= state->format.shift;
1512 dmabuf = &state->wpcm.dmabuf;
1513 if (dmabuf->mapped) {
1514 if (dmabuf->count >= (signed)dmabuf->fragsize)
1515 mask |= POLLOUT | POLLWRNORM;
1518 * Don't select unless a full fragment is available.
1519 * Otherwise artsd does GETOSPACE, sees 0, and loops.
1521 if (dmabuf->count + redzone + dmabuf->fragsize
1523 mask |= POLLOUT | POLLWRNORM;
1526 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1531 static int ymf_mmap(struct file *file, struct vm_area_struct *vma)
1533 struct ymf_state *state = (struct ymf_state *)file->private_data;
1534 struct ymf_dmabuf *dmabuf = &state->wpcm.dmabuf;
1538 if (vma->vm_flags & VM_WRITE) {
1539 if ((ret = prog_dmabuf(state, 0)) != 0)
1541 } else if (vma->vm_flags & VM_READ) {
1542 if ((ret = prog_dmabuf(state, 1)) != 0)
1547 if (vma->vm_pgoff != 0)
1549 size = vma->vm_end - vma->vm_start;
1550 if (size > (PAGE_SIZE << dmabuf->buforder))
1552 if (remap_page_range(vma, vma->vm_start, virt_to_phys(dmabuf->rawbuf),
1553 size, vma->vm_page_prot))
1557 /* P3 */ printk(KERN_INFO "ymfpci: using memory mapped sound, untested!\n");
1561 static int ymf_ioctl(struct inode *inode, struct file *file,
1562 unsigned int cmd, unsigned long arg)
1564 struct ymf_state *state = (struct ymf_state *)file->private_data;
1565 struct ymf_dmabuf *dmabuf;
1566 unsigned long flags;
1567 audio_buf_info abinfo;
1573 case OSS_GETVERSION:
1574 YMFDBGX("ymf_ioctl: cmd 0x%x(GETVER) arg 0x%lx\n", cmd, arg);
1575 return put_user(SOUND_VERSION, (int *)arg);
1577 case SNDCTL_DSP_RESET:
1578 YMFDBGX("ymf_ioctl: cmd 0x%x(RESET)\n", cmd);
1579 if (file->f_mode & FMODE_WRITE) {
1580 ymf_wait_dac(state);
1581 dmabuf = &state->wpcm.dmabuf;
1582 spin_lock_irqsave(&state->unit->reg_lock, flags);
1584 dmabuf->swptr = dmabuf->hwptr;
1585 dmabuf->count = dmabuf->total_bytes = 0;
1586 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1588 if (file->f_mode & FMODE_READ) {
1589 ymf_stop_adc(state);
1590 dmabuf = &state->rpcm.dmabuf;
1591 spin_lock_irqsave(&state->unit->reg_lock, flags);
1593 dmabuf->swptr = dmabuf->hwptr;
1594 dmabuf->count = dmabuf->total_bytes = 0;
1595 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1599 case SNDCTL_DSP_SYNC:
1600 YMFDBGX("ymf_ioctl: cmd 0x%x(SYNC)\n", cmd);
1601 if (file->f_mode & FMODE_WRITE) {
1602 dmabuf = &state->wpcm.dmabuf;
1603 if (file->f_flags & O_NONBLOCK) {
1604 spin_lock_irqsave(&state->unit->reg_lock, flags);
1605 if (dmabuf->count != 0 && !state->wpcm.running) {
1606 ymf_start_dac(state);
1608 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1610 ymf_wait_dac(state);
1613 /* XXX What does this do for reading? dmabuf->count=0; ? */
1616 case SNDCTL_DSP_SPEED: /* set smaple rate */
1617 if (get_user(val, (int *)arg))
1619 YMFDBGX("ymf_ioctl: cmd 0x%x(SPEED) sp %d\n", cmd, val);
1620 if (val >= 8000 && val <= 48000) {
1621 if (file->f_mode & FMODE_WRITE) {
1622 ymf_wait_dac(state);
1623 dmabuf = &state->wpcm.dmabuf;
1624 spin_lock_irqsave(&state->unit->reg_lock, flags);
1626 state->format.rate = val;
1627 ymf_pcm_update_shift(&state->format);
1628 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1630 if (file->f_mode & FMODE_READ) {
1631 ymf_stop_adc(state);
1632 dmabuf = &state->rpcm.dmabuf;
1633 spin_lock_irqsave(&state->unit->reg_lock, flags);
1635 state->format.rate = val;
1636 ymf_pcm_update_shift(&state->format);
1637 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1640 return put_user(state->format.rate, (int *)arg);
1643 * OSS manual does not mention SNDCTL_DSP_STEREO at all.
1644 * All channels are mono and if you want stereo, you
1645 * play into two channels with SNDCTL_DSP_CHANNELS.
1646 * However, mpg123 calls it. I wonder, why Michael Hipp used it.
1648 case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
1649 if (get_user(val, (int *)arg))
1651 YMFDBGX("ymf_ioctl: cmd 0x%x(STEREO) st %d\n", cmd, val);
1652 if (file->f_mode & FMODE_WRITE) {
1653 ymf_wait_dac(state);
1654 dmabuf = &state->wpcm.dmabuf;
1655 spin_lock_irqsave(&state->unit->reg_lock, flags);
1657 state->format.voices = val ? 2 : 1;
1658 ymf_pcm_update_shift(&state->format);
1659 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1661 if (file->f_mode & FMODE_READ) {
1662 ymf_stop_adc(state);
1663 dmabuf = &state->rpcm.dmabuf;
1664 spin_lock_irqsave(&state->unit->reg_lock, flags);
1666 state->format.voices = val ? 2 : 1;
1667 ymf_pcm_update_shift(&state->format);
1668 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1672 case SNDCTL_DSP_GETBLKSIZE:
1673 YMFDBGX("ymf_ioctl: cmd 0x%x(GETBLK)\n", cmd);
1674 if (file->f_mode & FMODE_WRITE) {
1675 if ((val = prog_dmabuf(state, 0)))
1677 val = state->wpcm.dmabuf.fragsize;
1678 YMFDBGX("ymf_ioctl: GETBLK w %d\n", val);
1679 return put_user(val, (int *)arg);
1681 if (file->f_mode & FMODE_READ) {
1682 if ((val = prog_dmabuf(state, 1)))
1684 val = state->rpcm.dmabuf.fragsize;
1685 YMFDBGX("ymf_ioctl: GETBLK r %d\n", val);
1686 return put_user(val, (int *)arg);
1690 case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
1691 YMFDBGX("ymf_ioctl: cmd 0x%x(GETFMTS)\n", cmd);
1692 return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
1694 case SNDCTL_DSP_SETFMT: /* Select sample format */
1695 if (get_user(val, (int *)arg))
1697 YMFDBGX("ymf_ioctl: cmd 0x%x(SETFMT) fmt %d\n", cmd, val);
1698 if (val == AFMT_S16_LE || val == AFMT_U8) {
1699 if (file->f_mode & FMODE_WRITE) {
1700 ymf_wait_dac(state);
1701 dmabuf = &state->wpcm.dmabuf;
1702 spin_lock_irqsave(&state->unit->reg_lock, flags);
1704 state->format.format = val;
1705 ymf_pcm_update_shift(&state->format);
1706 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1708 if (file->f_mode & FMODE_READ) {
1709 ymf_stop_adc(state);
1710 dmabuf = &state->rpcm.dmabuf;
1711 spin_lock_irqsave(&state->unit->reg_lock, flags);
1713 state->format.format = val;
1714 ymf_pcm_update_shift(&state->format);
1715 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1718 return put_user(state->format.format, (int *)arg);
1720 case SNDCTL_DSP_CHANNELS:
1721 if (get_user(val, (int *)arg))
1723 YMFDBGX("ymf_ioctl: cmd 0x%x(CHAN) ch %d\n", cmd, val);
1725 if (file->f_mode & FMODE_WRITE) {
1726 ymf_wait_dac(state);
1727 if (val == 1 || val == 2) {
1728 spin_lock_irqsave(&state->unit->reg_lock, flags);
1729 dmabuf = &state->wpcm.dmabuf;
1731 state->format.voices = val;
1732 ymf_pcm_update_shift(&state->format);
1733 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1736 if (file->f_mode & FMODE_READ) {
1737 ymf_stop_adc(state);
1738 if (val == 1 || val == 2) {
1739 spin_lock_irqsave(&state->unit->reg_lock, flags);
1740 dmabuf = &state->rpcm.dmabuf;
1742 state->format.voices = val;
1743 ymf_pcm_update_shift(&state->format);
1744 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1748 return put_user(state->format.voices, (int *)arg);
1750 case SNDCTL_DSP_POST:
1751 YMFDBGX("ymf_ioctl: cmd 0x%x(POST)\n", cmd);
1754 * The ioctl SNDCTL_DSP_POST is a lightweight version of
1755 * SNDCTL_DSP_SYNC. It just tells to the driver that there
1756 * is likely to be a pause in the output. This makes it
1757 * possible for the device to handle the pause more
1758 * intelligently. This ioctl doesn't block the application.
1760 * The paragraph above is a clumsy way to say "flush ioctl".
1761 * This ioctl is used by mpg123.
1763 spin_lock_irqsave(&state->unit->reg_lock, flags);
1764 if (state->wpcm.dmabuf.count != 0 && !state->wpcm.running) {
1765 ymf_start_dac(state);
1767 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1770 case SNDCTL_DSP_SETFRAGMENT:
1771 if (get_user(val, (int *)arg))
1773 YMFDBGX("ymf_ioctl: cmd 0x%x(SETFRAG) fr 0x%04x:%04x(%d:%d)\n",
1775 (val >> 16) & 0xFFFF, val & 0xFFFF,
1776 (val >> 16) & 0xFFFF, val & 0xFFFF);
1777 dmabuf = &state->wpcm.dmabuf;
1778 dmabuf->ossfragshift = val & 0xffff;
1779 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
1780 if (dmabuf->ossfragshift < 4)
1781 dmabuf->ossfragshift = 4;
1782 if (dmabuf->ossfragshift > 15)
1783 dmabuf->ossfragshift = 15;
1786 case SNDCTL_DSP_GETOSPACE:
1787 YMFDBGX("ymf_ioctl: cmd 0x%x(GETOSPACE)\n", cmd);
1788 if (!(file->f_mode & FMODE_WRITE))
1790 dmabuf = &state->wpcm.dmabuf;
1791 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
1793 redzone = ymf_calc_lend(state->format.rate);
1794 redzone <<= state->format.shift;
1796 spin_lock_irqsave(&state->unit->reg_lock, flags);
1797 abinfo.fragsize = dmabuf->fragsize;
1798 abinfo.bytes = dmabuf->dmasize - dmabuf->count - redzone;
1799 abinfo.fragstotal = dmabuf->numfrag;
1800 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
1801 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1802 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1804 case SNDCTL_DSP_GETISPACE:
1805 YMFDBGX("ymf_ioctl: cmd 0x%x(GETISPACE)\n", cmd);
1806 if (!(file->f_mode & FMODE_READ))
1808 dmabuf = &state->rpcm.dmabuf;
1809 if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
1811 spin_lock_irqsave(&state->unit->reg_lock, flags);
1812 abinfo.fragsize = dmabuf->fragsize;
1813 abinfo.bytes = dmabuf->count;
1814 abinfo.fragstotal = dmabuf->numfrag;
1815 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
1816 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1817 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1819 case SNDCTL_DSP_NONBLOCK:
1820 YMFDBGX("ymf_ioctl: cmd 0x%x(NONBLOCK)\n", cmd);
1821 file->f_flags |= O_NONBLOCK;
1824 case SNDCTL_DSP_GETCAPS:
1825 YMFDBGX("ymf_ioctl: cmd 0x%x(GETCAPS)\n", cmd);
1826 /* return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
1828 return put_user(0, (int *)arg);
1830 case SNDCTL_DSP_GETIPTR:
1831 YMFDBGX("ymf_ioctl: cmd 0x%x(GETIPTR)\n", cmd);
1832 if (!(file->f_mode & FMODE_READ))
1834 dmabuf = &state->rpcm.dmabuf;
1835 spin_lock_irqsave(&state->unit->reg_lock, flags);
1836 cinfo.bytes = dmabuf->total_bytes;
1837 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
1838 cinfo.ptr = dmabuf->hwptr;
1839 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1840 YMFDBGX("ymf_ioctl: GETIPTR ptr %d bytes %d\n",
1841 cinfo.ptr, cinfo.bytes);
1842 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1844 case SNDCTL_DSP_GETOPTR:
1845 YMFDBGX("ymf_ioctl: cmd 0x%x(GETOPTR)\n", cmd);
1846 if (!(file->f_mode & FMODE_WRITE))
1848 dmabuf = &state->wpcm.dmabuf;
1849 spin_lock_irqsave(&state->unit->reg_lock, flags);
1850 cinfo.bytes = dmabuf->total_bytes;
1851 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
1852 cinfo.ptr = dmabuf->hwptr;
1853 spin_unlock_irqrestore(&state->unit->reg_lock, flags);
1854 YMFDBGX("ymf_ioctl: GETOPTR ptr %d bytes %d\n",
1855 cinfo.ptr, cinfo.bytes);
1856 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1858 case SNDCTL_DSP_SETDUPLEX:
1859 YMFDBGX("ymf_ioctl: cmd 0x%x(SETDUPLEX)\n", cmd);
1860 return 0; /* Always duplex */
1862 case SOUND_PCM_READ_RATE:
1863 YMFDBGX("ymf_ioctl: cmd 0x%x(READ_RATE)\n", cmd);
1864 return put_user(state->format.rate, (int *)arg);
1866 case SOUND_PCM_READ_CHANNELS:
1867 YMFDBGX("ymf_ioctl: cmd 0x%x(READ_CH)\n", cmd);
1868 return put_user(state->format.voices, (int *)arg);
1870 case SOUND_PCM_READ_BITS:
1871 YMFDBGX("ymf_ioctl: cmd 0x%x(READ_BITS)\n", cmd);
1872 return put_user(AFMT_S16_LE, (int *)arg);
1874 case SNDCTL_DSP_MAPINBUF:
1875 case SNDCTL_DSP_MAPOUTBUF:
1876 case SNDCTL_DSP_SETSYNCRO:
1877 case SOUND_PCM_WRITE_FILTER:
1878 case SOUND_PCM_READ_FILTER:
1879 YMFDBGX("ymf_ioctl: cmd 0x%x unsupported\n", cmd);
1884 * Some programs mix up audio devices and ioctls
1885 * or perhaps they expect "universal" ioctls,
1886 * for instance we get SNDCTL_TMR_CONTINUE here.
1887 * (mpg123 -g 100 ends here too - to be fixed.)
1889 YMFDBGX("ymf_ioctl: cmd 0x%x unknown\n", cmd);
1897 * We use upper part of the minor to distinguish between soundcards.
1898 * Channels are opened with a clone open.
1900 static int ymf_open(struct inode *inode, struct file *file)
1902 struct list_head *list;
1903 ymfpci_t *unit = NULL;
1905 struct ymf_state *state;
1908 minor = iminor(inode);
1909 if ((minor & 0x0F) == 3) { /* /dev/dspN */
1915 unit = NULL; /* gcc warns */
1916 spin_lock(&ymf_devs_lock);
1917 list_for_each(list, &ymf_devs) {
1918 unit = list_entry(list, ymfpci_t, ymf_devs);
1919 if (((unit->dev_audio ^ minor) & ~0x0F) == 0)
1922 spin_unlock(&ymf_devs_lock);
1926 down(&unit->open_sem);
1928 if ((state = ymf_state_alloc(unit)) == NULL) {
1929 up(&unit->open_sem);
1932 list_add_tail(&state->chain, &unit->states);
1934 file->private_data = state;
1937 * ymf_read and ymf_write that we borrowed from cs46xx
1938 * allocate buffers with prog_dmabuf(). We call prog_dmabuf
1939 * here so that in case of DMA memory exhaustion open
1940 * fails rather than write.
1942 * XXX prog_dmabuf allocates voice. Should allocate explicitly, above.
1944 if (file->f_mode & FMODE_WRITE) {
1945 if (!state->wpcm.dmabuf.ready) {
1946 if ((err = prog_dmabuf(state, 0)) != 0) {
1951 if (file->f_mode & FMODE_READ) {
1952 if (!state->rpcm.dmabuf.ready) {
1953 if ((err = prog_dmabuf(state, 1)) != 0) {
1959 #if 0 /* test if interrupts work */
1960 ymfpci_writew(unit, YDSXGR_TIMERCOUNT, 0xfffe); /* ~ 680ms */
1961 ymfpci_writeb(unit, YDSXGR_TIMERCTRL,
1962 (YDSXGR_TIMERCTRL_TEN|YDSXGR_TIMERCTRL_TIEN));
1964 up(&unit->open_sem);
1970 * XXX Broken custom: "goto out_xxx" in other place is
1971 * a nestable exception, but here it is not nestable due to semaphore.
1972 * XXX Doubtful technique of self-describing objects....
1974 dealloc_dmabuf(unit, &state->wpcm.dmabuf);
1975 dealloc_dmabuf(unit, &state->rpcm.dmabuf);
1976 ymf_pcm_free_substream(&state->wpcm);
1977 ymf_pcm_free_substream(&state->rpcm);
1979 list_del(&state->chain);
1982 up(&unit->open_sem);
1986 static int ymf_release(struct inode *inode, struct file *file)
1988 struct ymf_state *state = (struct ymf_state *)file->private_data;
1989 ymfpci_t *unit = state->unit;
1991 #if 0 /* test if interrupts work */
1992 ymfpci_writeb(unit, YDSXGR_TIMERCTRL, 0);
1995 down(&unit->open_sem);
1998 * XXX Solve the case of O_NONBLOCK close - don't deallocate here.
1999 * Deallocate when unloading the driver and we can wait.
2001 ymf_wait_dac(state);
2002 ymf_stop_adc(state); /* fortunately, it's immediate */
2003 dealloc_dmabuf(unit, &state->wpcm.dmabuf);
2004 dealloc_dmabuf(unit, &state->rpcm.dmabuf);
2005 ymf_pcm_free_substream(&state->wpcm);
2006 ymf_pcm_free_substream(&state->rpcm);
2008 list_del(&state->chain);
2009 file->private_data = NULL; /* Can you tell I programmed Solaris */
2012 up(&unit->open_sem);
2018 * Mixer operations are based on cs46xx.
2020 static int ymf_open_mixdev(struct inode *inode, struct file *file)
2022 int minor = iminor(inode);
2023 struct list_head *list;
2027 spin_lock(&ymf_devs_lock);
2028 list_for_each(list, &ymf_devs) {
2029 unit = list_entry(list, ymfpci_t, ymf_devs);
2030 for (i = 0; i < NR_AC97; i++) {
2031 if (unit->ac97_codec[i] != NULL &&
2032 unit->ac97_codec[i]->dev_mixer == minor) {
2033 spin_unlock(&ymf_devs_lock);
2038 spin_unlock(&ymf_devs_lock);
2042 file->private_data = unit->ac97_codec[i];
2047 static int ymf_ioctl_mixdev(struct inode *inode, struct file *file,
2048 unsigned int cmd, unsigned long arg)
2050 struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
2052 return codec->mixer_ioctl(codec, cmd, arg);
2055 static int ymf_release_mixdev(struct inode *inode, struct file *file)
2060 static /*const*/ struct file_operations ymf_fops = {
2061 .owner = THIS_MODULE,
2062 .llseek = no_llseek,
2069 .release = ymf_release,
2072 static /*const*/ struct file_operations ymf_mixer_fops = {
2073 .owner = THIS_MODULE,
2074 .llseek = no_llseek,
2075 .ioctl = ymf_ioctl_mixdev,
2076 .open = ymf_open_mixdev,
2077 .release = ymf_release_mixdev,
2083 static int ymf_suspend(struct pci_dev *pcidev, u32 unused)
2085 struct ymf_unit *unit = pci_get_drvdata(pcidev);
2086 unsigned long flags;
2087 struct ymf_dmabuf *dmabuf;
2088 struct list_head *p;
2089 struct ymf_state *state;
2090 struct ac97_codec *codec;
2093 spin_lock_irqsave(&unit->reg_lock, flags);
2095 unit->suspended = 1;
2097 for (i = 0; i < NR_AC97; i++) {
2098 if ((codec = unit->ac97_codec[i]) != NULL)
2099 ac97_save_state(codec);
2102 list_for_each(p, &unit->states) {
2103 state = list_entry(p, struct ymf_state, chain);
2105 dmabuf = &state->wpcm.dmabuf;
2106 dmabuf->hwptr = dmabuf->swptr = 0;
2107 dmabuf->total_bytes = 0;
2110 dmabuf = &state->rpcm.dmabuf;
2111 dmabuf->hwptr = dmabuf->swptr = 0;
2112 dmabuf->total_bytes = 0;
2116 ymfpci_writel(unit, YDSXGR_NATIVEDACOUTVOL, 0);
2117 ymfpci_disable_dsp(unit);
2119 spin_unlock_irqrestore(&unit->reg_lock, flags);
2124 static int ymf_resume(struct pci_dev *pcidev)
2126 struct ymf_unit *unit = pci_get_drvdata(pcidev);
2127 unsigned long flags;
2128 struct list_head *p;
2129 struct ymf_state *state;
2130 struct ac97_codec *codec;
2133 ymfpci_aclink_reset(unit->pci);
2134 ymfpci_codec_ready(unit, 0, 1); /* prints diag if not ready. */
2136 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2137 /* XXX At this time the legacy registers are probably deprogrammed. */
2140 ymfpci_download_image(unit);
2144 spin_lock_irqsave(&unit->reg_lock, flags);
2146 if (unit->start_count) {
2147 ymfpci_writel(unit, YDSXGR_MODE, 3);
2148 unit->active_bank = ymfpci_readl(unit, YDSXGR_CTRLSELECT) & 1;
2151 for (i = 0; i < NR_AC97; i++) {
2152 if ((codec = unit->ac97_codec[i]) != NULL)
2153 ac97_restore_state(codec);
2156 unit->suspended = 0;
2157 list_for_each(p, &unit->states) {
2158 state = list_entry(p, struct ymf_state, chain);
2159 wake_up(&state->wpcm.dmabuf.wait);
2160 wake_up(&state->rpcm.dmabuf.wait);
2163 spin_unlock_irqrestore(&unit->reg_lock, flags);
2168 * initialization routines
2171 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2173 static int ymfpci_setup_legacy(ymfpci_t *unit, struct pci_dev *pcidev)
2176 int mpuio = -1, oplio = -1;
2178 switch (unit->iomidi) {
2194 switch (unit->iosynth) {
2210 if (mpuio >= 0 || oplio >= 0) {
2211 /* 0x0020: 1 - 10 bits of I/O address decoded, 0 - 16 bits. */
2213 pci_write_config_word(pcidev, PCIR_LEGCTRL, v);
2215 switch (pcidev->device) {
2216 case PCI_DEVICE_ID_YAMAHA_724:
2217 case PCI_DEVICE_ID_YAMAHA_740:
2218 case PCI_DEVICE_ID_YAMAHA_724F:
2219 case PCI_DEVICE_ID_YAMAHA_740C:
2221 if (mpuio >= 0) { v |= mpuio<<4; }
2222 if (oplio >= 0) { v |= oplio; }
2223 pci_write_config_word(pcidev, PCIR_ELEGCTRL, v);
2226 case PCI_DEVICE_ID_YAMAHA_744:
2227 case PCI_DEVICE_ID_YAMAHA_754:
2229 pci_write_config_word(pcidev, PCIR_ELEGCTRL, v);
2231 pci_write_config_word(pcidev, PCIR_OPLADR, unit->iosynth);
2234 pci_write_config_word(pcidev, PCIR_MPUADR, unit->iomidi);
2239 printk(KERN_ERR "ymfpci: Unknown device ID: 0x%x\n",
2247 #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2249 static void ymfpci_aclink_reset(struct pci_dev * pci)
2254 * In the 744, 754 only 0x01 exists, 0x02 is undefined.
2255 * It does not seem to hurt to trip both regardless of revision.
2257 pci_read_config_byte(pci, PCIR_DSXGCTRL, &cmd);
2258 pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd & 0xfc);
2259 pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd | 0x03);
2260 pci_write_config_byte(pci, PCIR_DSXGCTRL, cmd & 0xfc);
2262 pci_write_config_word(pci, PCIR_DSXPWRCTRL1, 0);
2263 pci_write_config_word(pci, PCIR_DSXPWRCTRL2, 0);
2266 static void ymfpci_enable_dsp(ymfpci_t *codec)
2268 ymfpci_writel(codec, YDSXGR_CONFIG, 0x00000001);
2271 static void ymfpci_disable_dsp(ymfpci_t *codec)
2276 val = ymfpci_readl(codec, YDSXGR_CONFIG);
2278 ymfpci_writel(codec, YDSXGR_CONFIG, 0x00000000);
2279 while (timeout-- > 0) {
2280 val = ymfpci_readl(codec, YDSXGR_STATUS);
2281 if ((val & 0x00000002) == 0)
2286 #include "ymfpci_image.h"
2288 static void ymfpci_download_image(ymfpci_t *codec)
2293 ymfpci_writel(codec, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2294 ymfpci_disable_dsp(codec);
2295 ymfpci_writel(codec, YDSXGR_MODE, 0x00010000);
2296 ymfpci_writel(codec, YDSXGR_MODE, 0x00000000);
2297 ymfpci_writel(codec, YDSXGR_MAPOFREC, 0x00000000);
2298 ymfpci_writel(codec, YDSXGR_MAPOFEFFECT, 0x00000000);
2299 ymfpci_writel(codec, YDSXGR_PLAYCTRLBASE, 0x00000000);
2300 ymfpci_writel(codec, YDSXGR_RECCTRLBASE, 0x00000000);
2301 ymfpci_writel(codec, YDSXGR_EFFCTRLBASE, 0x00000000);
2302 ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2303 ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2305 /* setup DSP instruction code */
2306 for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2307 ymfpci_writel(codec, YDSXGR_DSPINSTRAM + (i << 2), DspInst[i]);
2309 switch (codec->pci->device) {
2310 case PCI_DEVICE_ID_YAMAHA_724F:
2311 case PCI_DEVICE_ID_YAMAHA_740C:
2312 case PCI_DEVICE_ID_YAMAHA_744:
2313 case PCI_DEVICE_ID_YAMAHA_754:
2321 /* setup control instruction code */
2322 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2323 ymfpci_writel(codec, YDSXGR_CTRLINSTRAM + (i << 2), CntrlInst1E[i]);
2325 for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2326 ymfpci_writel(codec, YDSXGR_CTRLINSTRAM + (i << 2), CntrlInst[i]);
2329 ymfpci_enable_dsp(codec);
2331 /* 0.02s sounds not too bad, we may do schedule_timeout() later. */
2332 mdelay(20); /* seems we need some delay after downloading image.. */
2335 static int ymfpci_memalloc(ymfpci_t *codec)
2337 unsigned int playback_ctrl_size;
2338 unsigned int bank_size_playback;
2339 unsigned int bank_size_capture;
2340 unsigned int bank_size_effect;
2347 playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2348 bank_size_playback = ymfpci_readl(codec, YDSXGR_PLAYCTRLSIZE) << 2;
2349 bank_size_capture = ymfpci_readl(codec, YDSXGR_RECCTRLSIZE) << 2;
2350 bank_size_effect = ymfpci_readl(codec, YDSXGR_EFFCTRLSIZE) << 2;
2351 codec->work_size = YDSXG_DEFAULT_WORK_SIZE;
2353 size = ((playback_ctrl_size + 0x00ff) & ~0x00ff) +
2354 ((bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES + 0xff) & ~0xff) +
2355 ((bank_size_capture * 2 * YDSXG_CAPTURE_VOICES + 0xff) & ~0xff) +
2356 ((bank_size_effect * 2 * YDSXG_EFFECT_VOICES + 0xff) & ~0xff) +
2359 ptr = pci_alloc_consistent(codec->pci, size + 0xff, &pba);
2362 codec->dma_area_va = ptr;
2363 codec->dma_area_ba = pba;
2364 codec->dma_area_size = size + 0xff;
2366 if ((off = ((uint) ptr) & 0xff) != 0) {
2372 * Hardware requires only ptr[playback_ctrl_size] zeroed,
2373 * but in our judgement it is a wrong kind of savings, so clear it all.
2375 memset(ptr, 0, size);
2377 codec->ctrl_playback = (u32 *)ptr;
2378 codec->ctrl_playback_ba = pba;
2379 codec->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2380 ptr += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2381 pba += (playback_ctrl_size + 0x00ff) & ~0x00ff;
2384 for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2385 codec->voices[voice].number = voice;
2386 codec->voices[voice].bank =
2387 (ymfpci_playback_bank_t *) (ptr + off);
2388 codec->voices[voice].bank_ba = pba + off;
2389 off += 2 * bank_size_playback; /* 2 banks */
2391 off = (off + 0xff) & ~0xff;
2396 codec->bank_base_capture = pba;
2397 for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2398 for (bank = 0; bank < 2; bank++) {
2399 codec->bank_capture[voice][bank] =
2400 (ymfpci_capture_bank_t *) (ptr + off);
2401 off += bank_size_capture;
2403 off = (off + 0xff) & ~0xff;
2408 codec->bank_base_effect = pba;
2409 for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2410 for (bank = 0; bank < 2; bank++) {
2411 codec->bank_effect[voice][bank] =
2412 (ymfpci_effect_bank_t *) (ptr + off);
2413 off += bank_size_effect;
2415 off = (off + 0xff) & ~0xff;
2419 codec->work_base = pba;
2424 static void ymfpci_memfree(ymfpci_t *codec)
2426 ymfpci_writel(codec, YDSXGR_PLAYCTRLBASE, 0);
2427 ymfpci_writel(codec, YDSXGR_RECCTRLBASE, 0);
2428 ymfpci_writel(codec, YDSXGR_EFFCTRLBASE, 0);
2429 ymfpci_writel(codec, YDSXGR_WORKBASE, 0);
2430 ymfpci_writel(codec, YDSXGR_WORKSIZE, 0);
2431 pci_free_consistent(codec->pci,
2432 codec->dma_area_size, codec->dma_area_va, codec->dma_area_ba);
2435 static void ymf_memload(ymfpci_t *unit)
2438 ymfpci_writel(unit, YDSXGR_PLAYCTRLBASE, unit->ctrl_playback_ba);
2439 ymfpci_writel(unit, YDSXGR_RECCTRLBASE, unit->bank_base_capture);
2440 ymfpci_writel(unit, YDSXGR_EFFCTRLBASE, unit->bank_base_effect);
2441 ymfpci_writel(unit, YDSXGR_WORKBASE, unit->work_base);
2442 ymfpci_writel(unit, YDSXGR_WORKSIZE, unit->work_size >> 2);
2444 /* S/PDIF output initialization */
2445 ymfpci_writew(unit, YDSXGR_SPDIFOUTCTRL, 0);
2446 ymfpci_writew(unit, YDSXGR_SPDIFOUTSTATUS,
2447 SND_PCM_AES0_CON_EMPHASIS_NONE |
2448 (SND_PCM_AES1_CON_ORIGINAL << 8) |
2449 (SND_PCM_AES1_CON_PCM_CODER << 8));
2451 /* S/PDIF input initialization */
2452 ymfpci_writew(unit, YDSXGR_SPDIFINCTRL, 0);
2454 /* move this volume setup to mixer */
2455 ymfpci_writel(unit, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2456 ymfpci_writel(unit, YDSXGR_BUF441OUTVOL, 0);
2457 ymfpci_writel(unit, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2458 ymfpci_writel(unit, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2461 static int ymf_ac97_init(ymfpci_t *unit, int num_ac97)
2463 struct ac97_codec *codec;
2466 if ((codec = ac97_alloc_codec()) == NULL)
2469 /* initialize some basic codec information, other fields will be filled
2470 in ac97_probe_codec */
2471 codec->private_data = unit;
2472 codec->id = num_ac97;
2474 codec->codec_read = ymfpci_codec_read;
2475 codec->codec_write = ymfpci_codec_write;
2477 if (ac97_probe_codec(codec) == 0) {
2478 printk(KERN_ERR "ymfpci: ac97_probe_codec failed\n");
2482 eid = ymfpci_codec_read(codec, AC97_EXTENDED_ID);
2484 printk(KERN_WARNING "ymfpci: no codec attached ?\n");
2488 unit->ac97_features = eid;
2490 if ((codec->dev_mixer = register_sound_mixer(&ymf_mixer_fops, -1)) < 0) {
2491 printk(KERN_ERR "ymfpci: couldn't register mixer!\n");
2495 unit->ac97_codec[num_ac97] = codec;
2499 ac97_release_codec(codec);
2503 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2506 static int synth_io;
2507 MODULE_PARM(mpu_io, "i");
2508 MODULE_PARM(synth_io, "i");
2510 static int mpu_io = 0x330;
2511 static int synth_io = 0x388;
2513 static int assigned;
2514 #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2516 static int __devinit ymf_probe_one(struct pci_dev *pcidev, const struct pci_device_id *ent)
2524 if ((err = pci_enable_device(pcidev)) != 0) {
2525 printk(KERN_ERR "ymfpci: pci_enable_device failed\n");
2528 base = pci_resource_start(pcidev, 0);
2530 if ((codec = kmalloc(sizeof(ymfpci_t), GFP_KERNEL)) == NULL) {
2531 printk(KERN_ERR "ymfpci: no core\n");
2534 memset(codec, 0, sizeof(*codec));
2536 spin_lock_init(&codec->reg_lock);
2537 spin_lock_init(&codec->voice_lock);
2538 spin_lock_init(&codec->ac97_lock);
2539 init_MUTEX(&codec->open_sem);
2540 INIT_LIST_HEAD(&codec->states);
2541 codec->pci = pcidev;
2543 pci_read_config_byte(pcidev, PCI_REVISION_ID, &codec->rev);
2545 if (request_mem_region(base, 0x8000, "ymfpci") == NULL) {
2546 printk(KERN_ERR "ymfpci: unable to request mem region\n");
2550 if ((codec->reg_area_virt = ioremap(base, 0x8000)) == NULL) {
2551 printk(KERN_ERR "ymfpci: unable to map registers\n");
2552 goto out_release_region;
2555 pci_set_master(pcidev);
2557 printk(KERN_INFO "ymfpci: %s at 0x%lx IRQ %d\n",
2558 (char *)ent->driver_data, base, pcidev->irq);
2560 ymfpci_aclink_reset(pcidev);
2561 if (ymfpci_codec_ready(codec, 0, 1) < 0)
2564 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2565 if (assigned == 0) {
2566 codec->iomidi = mpu_io;
2567 codec->iosynth = synth_io;
2568 if (ymfpci_setup_legacy(codec, pcidev) < 0)
2574 ymfpci_download_image(codec);
2576 if (ymfpci_memalloc(codec) < 0)
2577 goto out_disable_dsp;
2580 if (request_irq(pcidev->irq, ymf_interrupt, SA_SHIRQ, "ymfpci", codec) != 0) {
2581 printk(KERN_ERR "ymfpci: unable to request IRQ %d\n",
2586 /* register /dev/dsp */
2587 if ((codec->dev_audio = register_sound_dsp(&ymf_fops, -1)) < 0) {
2588 printk(KERN_ERR "ymfpci: unable to register dsp\n");
2593 * Poke just the primary for the moment.
2595 if ((err = ymf_ac97_init(codec, 0)) != 0)
2596 goto out_unregister_sound_dsp;
2598 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2599 codec->opl3_data.name = "ymfpci";
2600 codec->mpu_data.name = "ymfpci";
2602 codec->opl3_data.io_base = codec->iosynth;
2603 codec->opl3_data.irq = -1;
2605 codec->mpu_data.io_base = codec->iomidi;
2606 codec->mpu_data.irq = -1; /* May be different from our PCI IRQ. */
2608 if (codec->iomidi) {
2609 if (!probe_uart401(&codec->mpu_data, THIS_MODULE)) {
2610 codec->iomidi = 0; /* XXX kludge */
2613 #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2615 /* put it into driver list */
2616 spin_lock(&ymf_devs_lock);
2617 list_add_tail(&codec->ymf_devs, &ymf_devs);
2618 spin_unlock(&ymf_devs_lock);
2619 pci_set_drvdata(pcidev, codec);
2623 out_unregister_sound_dsp:
2624 unregister_sound_dsp(codec->dev_audio);
2626 free_irq(pcidev->irq, codec);
2628 ymfpci_memfree(codec);
2630 ymfpci_disable_dsp(codec);
2631 ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2632 ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2633 ymfpci_writel(codec, YDSXGR_STATUS, ~0);
2635 iounmap(codec->reg_area_virt);
2637 release_mem_region(pci_resource_start(pcidev, 0), 0x8000);
2639 if (codec->ac97_codec[0])
2640 ac97_release_codec(codec->ac97_codec[0]);
2644 static void __devexit ymf_remove_one(struct pci_dev *pcidev)
2647 ymfpci_t *codec = pci_get_drvdata(pcidev);
2649 /* remove from list of devices */
2650 spin_lock(&ymf_devs_lock);
2651 list_del(&codec->ymf_devs);
2652 spin_unlock(&ymf_devs_lock);
2654 unregister_sound_mixer(codec->ac97_codec[0]->dev_mixer);
2655 ac97_release_codec(codec->ac97_codec[0]);
2656 unregister_sound_dsp(codec->dev_audio);
2657 free_irq(pcidev->irq, codec);
2658 ymfpci_memfree(codec);
2659 ymfpci_writel(codec, YDSXGR_STATUS, ~0);
2660 ymfpci_disable_dsp(codec);
2661 ctrl = ymfpci_readw(codec, YDSXGR_GLOBALCTRL);
2662 ymfpci_writew(codec, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2663 iounmap(codec->reg_area_virt);
2664 release_mem_region(pci_resource_start(pcidev, 0), 0x8000);
2665 #ifdef CONFIG_SOUND_YMFPCI_LEGACY
2666 if (codec->iomidi) {
2667 unload_uart401(&codec->mpu_data);
2669 #endif /* CONFIG_SOUND_YMFPCI_LEGACY */
2672 MODULE_AUTHOR("Jaroslav Kysela");
2673 MODULE_DESCRIPTION("Yamaha YMF7xx PCI Audio");
2674 MODULE_LICENSE("GPL");
2676 static struct pci_driver ymfpci_driver = {
2678 .id_table = ymf_id_tbl,
2679 .probe = ymf_probe_one,
2680 .remove = __devexit_p(ymf_remove_one),
2681 .suspend = ymf_suspend,
2682 .resume = ymf_resume
2685 static int __init ymf_init_module(void)
2687 return pci_module_init(&ymfpci_driver);
2690 static void __exit ymf_cleanup_module (void)
2692 pci_unregister_driver(&ymfpci_driver);
2695 module_init(ymf_init_module);
2696 module_exit(ymf_cleanup_module);