2 * Driver for HAL2 sound processors
3 * Copyright (c) 2001, 2002 Ladislav Michl <ladis@psi.cz>
5 * Based on Ulf Carlsson's code.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * /dev/dsp standard dsp device, (mostly) OSS compatible
22 * /dev/mixer standard mixer device, (mostly) OSS compatible
25 * + Driver currently supports indigo mode only.
26 * + Recording doesn't work. I guess that it is caused by PBUS channel
27 * misconfiguration, but until I get relevant info I'm unable to fix it.
30 #include <linux/module.h>
31 #include <linux/sched.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/sound.h>
36 #include <linux/soundcard.h>
38 #include <asm/uaccess.h>
39 #include <asm/sgi/sgint23.h>
44 #define DEBUG(args...) printk(args)
46 #define DEBUG(args...)
50 #define DEBUG_MIX(args...) printk(args)
52 #define DEBUG_MIX(args...)
55 #define H2_INDIRECT_WAIT(regs) while (regs->isr & H2_ISR_TSTATUS);
57 #define H2_READ_ADDR(addr) (addr | (1<<7))
58 #define H2_WRITE_ADDR(addr) (addr)
60 static char *hal2str = "HAL2 audio";
61 static int ibuffers = 32;
62 static int obuffers = 32;
64 /* I doubt anyone has a machine with two HAL2 cards. It's possible to
65 * have two HPC's, so it is probably possible to have two HAL2 cards.
66 * Try to deal with it, but note that it is not tested.
69 static hal2_card_t* hal2_card[MAXCARDS];
72 unsigned char idx:4, avail:1;
73 } mixtable[SOUND_MIXER_NRDEVICES] = {
74 [SOUND_MIXER_PCM] = { H2_MIX_OUTPUT_ATT, 1 }, /* voice */
75 [SOUND_MIXER_MIC] = { H2_MIX_INPUT_GAIN, 1 }, /* mic */
78 #define H2_SUPPORTED_FORMATS (AFMT_S16_LE | AFMT_S16_BE)
80 static inline void hal2_isr_write(hal2_card_t *hal2, u32 val)
82 hal2->ctl_regs->isr = val;
85 static inline u32 hal2_isr_look(hal2_card_t *hal2)
87 return hal2->ctl_regs->isr;
90 static inline u32 hal2_rev_look(hal2_card_t *hal2)
92 return hal2->ctl_regs->rev;
96 static u16 hal2_i_look16(hal2_card_t *hal2, u32 addr)
98 hal2_ctl_regs_t *regs = hal2->ctl_regs;
100 regs->iar = H2_READ_ADDR(addr);
101 H2_INDIRECT_WAIT(regs);
102 return (regs->idr0 & 0xffff);
106 static u32 hal2_i_look32(hal2_card_t *hal2, u32 addr)
109 hal2_ctl_regs_t *regs = hal2->ctl_regs;
111 regs->iar = H2_READ_ADDR(addr);
112 H2_INDIRECT_WAIT(regs);
113 ret = regs->idr0 & 0xffff;
114 regs->iar = H2_READ_ADDR(addr | 0x1);
115 H2_INDIRECT_WAIT(regs);
116 ret |= (regs->idr0 & 0xffff) << 16;
120 static void hal2_i_write16(hal2_card_t *hal2, u32 addr, u16 val)
122 hal2_ctl_regs_t *regs = hal2->ctl_regs;
128 regs->iar = H2_WRITE_ADDR(addr);
129 H2_INDIRECT_WAIT(regs);
132 static void hal2_i_write32(hal2_card_t *hal2, u32 addr, u32 val)
134 hal2_ctl_regs_t *regs = hal2->ctl_regs;
136 regs->idr0 = val & 0xffff;
137 regs->idr1 = val >> 16;
140 regs->iar = H2_WRITE_ADDR(addr);
141 H2_INDIRECT_WAIT(regs);
144 static void hal2_i_setbit16(hal2_card_t *hal2, u32 addr, u16 bit)
146 hal2_ctl_regs_t *regs = hal2->ctl_regs;
148 regs->iar = H2_READ_ADDR(addr);
149 H2_INDIRECT_WAIT(regs);
150 regs->idr0 = regs->idr0 | bit;
154 regs->iar = H2_WRITE_ADDR(addr);
155 H2_INDIRECT_WAIT(regs);
158 static void hal2_i_setbit32(hal2_card_t *hal2, u32 addr, u32 bit)
161 hal2_ctl_regs_t *regs = hal2->ctl_regs;
163 regs->iar = H2_READ_ADDR(addr);
164 H2_INDIRECT_WAIT(regs);
165 tmp = regs->idr0 | (regs->idr1 << 16) | bit;
166 regs->idr0 = tmp & 0xffff;
167 regs->idr1 = tmp >> 16;
170 regs->iar = H2_WRITE_ADDR(addr);
171 H2_INDIRECT_WAIT(regs);
174 static void hal2_i_clearbit16(hal2_card_t *hal2, u32 addr, u16 bit)
176 hal2_ctl_regs_t *regs = hal2->ctl_regs;
178 regs->iar = H2_READ_ADDR(addr);
179 H2_INDIRECT_WAIT(regs);
180 regs->idr0 = regs->idr0 & ~bit;
184 regs->iar = H2_WRITE_ADDR(addr);
185 H2_INDIRECT_WAIT(regs);
189 static void hal2_i_clearbit32(hal2_card_t *hal2, u32 addr, u32 bit)
192 hal2_ctl_regs_t *regs = hal2->ctl_regs;
194 regs->iar = H2_READ_ADDR(addr);
195 H2_INDIRECT_WAIT(regs);
196 tmp = (regs->idr0 | (regs->idr1 << 16)) & ~bit;
197 regs->idr0 = tmp & 0xffff;
198 regs->idr1 = tmp >> 16;
201 regs->iar = H2_WRITE_ADDR(addr);
202 H2_INDIRECT_WAIT(regs);
207 static void hal2_dump_regs(hal2_card_t *hal2)
209 printk("isr: %08hx ", hal2_isr_look(hal2));
210 printk("rev: %08hx\n", hal2_rev_look(hal2));
211 printk("relay: %04hx\n", hal2_i_look16(hal2, H2I_RELAY_C));
212 printk("port en: %04hx ", hal2_i_look16(hal2, H2I_DMA_PORT_EN));
213 printk("dma end: %04hx ", hal2_i_look16(hal2, H2I_DMA_END));
214 printk("dma drv: %04hx\n", hal2_i_look16(hal2, H2I_DMA_DRV));
215 printk("syn ctl: %04hx ", hal2_i_look16(hal2, H2I_SYNTH_C));
216 printk("aesrx ctl: %04hx ", hal2_i_look16(hal2, H2I_AESRX_C));
217 printk("aestx ctl: %04hx ", hal2_i_look16(hal2, H2I_AESTX_C));
218 printk("dac ctl1: %04hx ", hal2_i_look16(hal2, H2I_ADC_C1));
219 printk("dac ctl2: %08lx ", hal2_i_look32(hal2, H2I_ADC_C2));
220 printk("adc ctl1: %04hx ", hal2_i_look16(hal2, H2I_DAC_C1));
221 printk("adc ctl2: %08lx ", hal2_i_look32(hal2, H2I_DAC_C2));
222 printk("syn map: %04hx\n", hal2_i_look16(hal2, H2I_SYNTH_MAP_C));
223 printk("bres1 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES1_C1));
224 printk("bres1 ctl2: %04lx ", hal2_i_look32(hal2, H2I_BRES1_C2));
225 printk("bres2 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES2_C1));
226 printk("bres2 ctl2: %04lx ", hal2_i_look32(hal2, H2I_BRES2_C2));
227 printk("bres3 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES3_C1));
228 printk("bres3 ctl2: %04lx\n", hal2_i_look32(hal2, H2I_BRES3_C2));
232 static hal2_card_t* hal2_dsp_find_card(int minor)
236 for (i = 0; i < MAXCARDS; i++)
237 if (hal2_card[i] != NULL && hal2_card[i]->dev_dsp == minor)
242 static hal2_card_t* hal2_mixer_find_card(int minor)
246 for (i = 0; i < MAXCARDS; i++)
247 if (hal2_card[i] != NULL && hal2_card[i]->dev_mixer == minor)
253 static void hal2_dac_interrupt(hal2_codec_t *dac)
257 spin_lock(&dac->lock);
259 /* if tail buffer contains zero samples DMA stream was already
261 running = dac->tail->info.cnt;
262 dac->tail->info.cnt = 0;
263 dac->tail->info.desc.cntinfo = HPCDMA_XIE | HPCDMA_EOX;
264 dma_cache_wback_inv((unsigned long) dac->tail,
265 sizeof(struct hpc_dma_desc));
266 /* we just proccessed empty buffer, don't update tail pointer */
268 dac->tail = dac->tail->info.next;
270 spin_unlock(&dac->lock);
272 wake_up(&dac->dma_wait);
275 static void hal2_adc_interrupt(hal2_codec_t *adc)
279 spin_lock(&adc->lock);
281 /* if head buffer contains nonzero samples DMA stream was already
283 running = !adc->head->info.cnt;
284 adc->head->info.cnt = H2_BUFFER_SIZE;
285 adc->head->info.desc.cntinfo = HPCDMA_XIE | HPCDMA_EOX;
286 dma_cache_wback_inv((unsigned long) adc->head,
287 sizeof(struct hpc_dma_desc));
288 /* we just proccessed empty buffer, don't update head pointer */
290 dma_cache_inv((unsigned long) adc->head->data, H2_BUFFER_SIZE);
291 adc->head = adc->head->info.next;
294 spin_unlock(&adc->lock);
296 wake_up(&adc->dma_wait);
299 static void hal2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
301 hal2_card_t *hal2 = (hal2_card_t*)dev_id;
303 /* decide what caused this interrupt */
304 if (hal2->dac.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT)
305 hal2_dac_interrupt(&hal2->dac);
306 if (hal2->adc.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT)
307 hal2_adc_interrupt(&hal2->adc);
310 static int hal2_compute_rate(hal2_codec_t *codec, unsigned int rate)
314 /* We default to 44.1 kHz and if it isn't possible to fall back to
315 * 48.0 kHz with the needed adjustments of real_rate.
318 DEBUG("rate: %d\n", rate);
320 /* Refer to CS4216 data sheet */
326 /* Note: This is NOT the way they set up the bresenham clock generators
327 * in the specification. I've tried to implement that method but it
328 * doesn't work. It's probably another silly bug in the spec.
330 * I accidently discovered this method while I was testing and it seems
331 * to work very well with all frequencies, and thee shall follow rule #1
335 if (44100 % rate == 0) {
337 if (inc < 1) inc = 1;
338 codec->master = 44100;
341 if (inc < 1) inc = 1;
343 codec->master = 48000;
348 DEBUG("real_rate: %d\n", rate);
353 static void hal2_set_dac_rate(hal2_card_t *hal2)
355 unsigned int master = hal2->dac.master;
356 int inc = hal2->dac.inc;
357 int mod = hal2->dac.mod;
359 DEBUG("master: %d inc: %d mod: %d\n", master, inc, mod);
361 hal2_i_write16(hal2, H2I_BRES1_C1, (master == 44100) ? 1 : 0);
362 hal2_i_write32(hal2, H2I_BRES1_C2, ((0xffff & (mod - inc - 1)) << 16) | 1);
365 static void hal2_set_adc_rate(hal2_card_t *hal2)
367 unsigned int master = hal2->adc.master;
368 int inc = hal2->adc.inc;
369 int mod = hal2->adc.mod;
371 DEBUG("master: %d inc: %d mod: %d\n", master, inc, mod);
373 hal2_i_write16(hal2, H2I_BRES2_C1, (master == 44100) ? 1 : 0);
374 hal2_i_write32(hal2, H2I_BRES2_C2, ((0xffff & (mod - inc - 1)) << 16) | 1);
377 static void hal2_setup_dac(hal2_card_t *hal2)
379 unsigned int fifobeg, fifoend, highwater, sample_size;
380 hal2_pbus_t *pbus = &hal2->dac.pbus;
382 DEBUG("hal2_setup_dac\n");
384 /* Now we set up some PBUS information. The PBUS needs information about
385 * what portion of the fifo it will use. If it's receiving or
386 * transmitting, and finally whether the stream is little endian or big
387 * endian. The information is written later, on the start call.
389 sample_size = 2 * hal2->dac.voices;
391 /* Fifo should be set to hold exactly four samples. Highwater mark
392 * should be set to two samples. */
393 highwater = (sample_size * 2) >> 1; /* halfwords */
394 fifobeg = 0; /* playback is first */
395 fifoend = (sample_size * 4) >> 3; /* doublewords */
396 pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_LD |
397 (highwater << 8) | (fifobeg << 16) | (fifoend << 24);
398 /* We disable everything before we do anything at all */
399 pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
400 hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);
401 hal2_i_clearbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
402 /* Setup the HAL2 for playback */
403 hal2_set_dac_rate(hal2);
404 /* We are using 1st Bresenham clock generator for playback */
405 hal2_i_write16(hal2, H2I_DAC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)
406 | (1 << H2I_C1_CLKID_SHIFT)
407 | (hal2->dac.voices << H2I_C1_DATAT_SHIFT));
410 static void hal2_setup_adc(hal2_card_t *hal2)
412 unsigned int fifobeg, fifoend, highwater, sample_size;
413 hal2_pbus_t *pbus = &hal2->adc.pbus;
415 DEBUG("hal2_setup_adc\n");
417 sample_size = 2 * hal2->adc.voices;
419 highwater = (sample_size * 2) >> 1; /* halfwords */
420 fifobeg = (4 * 4) >> 3; /* record is second */
421 fifoend = (4 * 4 + sample_size * 4) >> 3; /* doublewords */
422 pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_RCV | HPC3_PDMACTRL_LD |
423 (highwater << 8) | (fifobeg << 16) | (fifoend << 24);
424 pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
425 hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);
426 hal2_i_clearbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
427 /* Setup the HAL2 for record */
428 hal2_set_adc_rate(hal2);
429 /* We are using 2nd Bresenham clock generator for record */
430 hal2_i_write16(hal2, H2I_ADC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)
431 | (2 << H2I_C1_CLKID_SHIFT)
432 | (hal2->adc.voices << H2I_C1_DATAT_SHIFT));
435 static void hal2_start_dac(hal2_card_t *hal2)
437 hal2_pbus_t *pbus = &hal2->dac.pbus;
439 DEBUG("hal2_start_dac\n");
441 pbus->pbus->pbdma_dptr = PHYSADDR(hal2->dac.tail);
442 pbus->pbus->pbdma_ctrl = pbus->ctrl | HPC3_PDMACTRL_ACT;
445 if (hal2->dac.format & AFMT_S16_LE)
446 hal2_i_setbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);
448 hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);
450 hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
452 hal2_i_setbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);
455 static void hal2_start_adc(hal2_card_t *hal2)
457 hal2_pbus_t *pbus = &hal2->adc.pbus;
459 DEBUG("hal2_start_adc\n");
461 pbus->pbus->pbdma_dptr = PHYSADDR(hal2->adc.head);
462 pbus->pbus->pbdma_ctrl = pbus->ctrl | HPC3_PDMACTRL_ACT;
465 if (hal2->adc.format & AFMT_S16_LE)
466 hal2_i_setbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);
468 hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);
470 hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
472 hal2_i_setbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);
475 static inline void hal2_stop_dac(hal2_card_t *hal2)
477 DEBUG("hal2_stop_dac\n");
479 hal2->dac.pbus.pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
480 /* The HAL2 itself may remain enabled safely */
483 static inline void hal2_stop_adc(hal2_card_t *hal2)
485 DEBUG("hal2_stop_adc\n");
487 hal2->adc.pbus.pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
490 #define hal2_alloc_dac_dmabuf(hal2) hal2_alloc_dmabuf(hal2, 1)
491 #define hal2_alloc_adc_dmabuf(hal2) hal2_alloc_dmabuf(hal2, 0)
492 static int hal2_alloc_dmabuf(hal2_card_t *hal2, int is_dac)
494 int buffers, cntinfo;
495 hal2_buf_t *buf, *prev;
501 cntinfo = HPCDMA_XIE | HPCDMA_EOX;
505 cntinfo = HPCDMA_XIE | H2_BUFFER_SIZE;
508 DEBUG("allocating %d DMA buffers.\n", buffers);
510 buf = (hal2_buf_t*) get_zeroed_page(GFP_KERNEL);
517 buf->info.desc.pbuf = PHYSADDR(&buf->data);
518 buf->info.desc.cntinfo = cntinfo;
521 buf = (hal2_buf_t*) get_zeroed_page(GFP_KERNEL);
523 printk("HAL2: Not enough memory for DMA buffer.\n");
527 free_page((unsigned long) buf);
528 buf = prev->info.next;
532 prev->info.next = buf;
533 prev->info.desc.pnext = PHYSADDR(buf);
534 /* The PBUS can prolly not read this stuff when it's in
535 * the cache so we have to flush it back to main memory
537 dma_cache_wback_inv((unsigned long) prev, PAGE_SIZE);
539 buf->info.desc.pbuf = PHYSADDR(&buf->data);
540 buf->info.desc.cntinfo = cntinfo;
542 buf->info.next = codec->head;
543 buf->info.desc.pnext = PHYSADDR(codec->head);
544 dma_cache_wback_inv((unsigned long) buf, PAGE_SIZE);
549 #define hal2_free_dac_dmabuf(hal2) hal2_free_dmabuf(hal2, 1)
550 #define hal2_free_adc_dmabuf(hal2) hal2_free_dmabuf(hal2, 0)
551 static void hal2_free_dmabuf(hal2_card_t *hal2, int is_dac)
553 hal2_buf_t *buf, *next;
554 hal2_codec_t *codec = (is_dac) ? &hal2->dac : &hal2->adc;
559 buf = codec->head->info.next;
560 codec->head->info.next = NULL;
562 next = buf->info.next;
563 free_page((unsigned long) buf);
566 codec->head = codec->tail = NULL;
570 * Add 'count' bytes to 'buffer' from DMA ring buffers. Return number of
571 * bytes added or -EFAULT if copy_from_user failed.
573 static int hal2_get_buffer(hal2_card_t *hal2, char *buffer, int count)
577 hal2_codec_t *adc = &hal2->adc;
579 spin_lock_irqsave(&adc->lock, flags);
581 DEBUG("getting %d bytes ", count);
583 /* enable DMA stream if there are no data */
584 if (!(adc->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT) &&
585 adc->tail->info.cnt == 0)
586 hal2_start_adc(hal2);
590 while (adc->tail->info.cnt > 0 && count > 0) {
591 size = min(adc->tail->info.cnt, count);
592 spin_unlock_irqrestore(&adc->lock, flags);
594 if (copy_to_user(buffer, &adc->tail->data[H2_BUFFER_SIZE-size],
600 spin_lock_irqsave(&adc->lock, flags);
602 adc->tail->info.cnt -= size;
603 /* buffer is empty, update tail pointer */
604 if (adc->tail->info.cnt == 0) {
605 adc->tail->info.desc.cntinfo = HPCDMA_XIE |
607 dma_cache_wback_inv((unsigned long) adc->tail,
608 sizeof(struct hpc_dma_desc));
609 adc->tail = adc->tail->info.next;
610 /* enable DMA stream again if needed */
611 if (!(adc->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT))
612 hal2_start_adc(hal2);
619 DEBUG("(%d) ", size);
621 spin_unlock_irqrestore(&adc->lock, flags);
629 * Add 'count' bytes from 'buffer' to DMA ring buffers. Return number of
630 * bytes added or -EFAULT if copy_from_user failed.
632 static int hal2_add_buffer(hal2_card_t *hal2, char *buffer, int count)
636 hal2_codec_t *dac = &hal2->dac;
638 spin_lock_irqsave(&dac->lock, flags);
640 DEBUG("adding %d bytes ", count);
642 while (dac->head->info.cnt == 0 && count > 0) {
643 size = min((int)H2_BUFFER_SIZE, count);
644 spin_unlock_irqrestore(&dac->lock, flags);
646 if (copy_from_user(dac->head->data, buffer, size)) {
650 spin_lock_irqsave(&dac->lock, flags);
652 dac->head->info.desc.cntinfo = size | HPCDMA_XIE;
653 dac->head->info.cnt = size;
654 dma_cache_wback_inv((unsigned long) dac->head,
655 size + PAGE_SIZE - H2_BUFFER_SIZE);
659 dac->head = dac->head->info.next;
661 DEBUG("(%d) ", size);
663 if (!(dac->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT) && ret > 0)
664 hal2_start_dac(hal2);
666 spin_unlock_irqrestore(&dac->lock, flags);
673 #define hal2_reset_dac_pointer(hal2) hal2_reset_pointer(hal2, 1)
674 #define hal2_reset_adc_pointer(hal2) hal2_reset_pointer(hal2, 0)
675 static void hal2_reset_pointer(hal2_card_t *hal2, int is_dac)
677 hal2_codec_t *codec = (is_dac) ? &hal2->dac : &hal2->adc;
679 DEBUG("hal2_reset_pointer\n");
681 codec->tail = codec->head;
683 codec->tail->info.desc.cntinfo = HPCDMA_XIE | (is_dac) ?
684 HPCDMA_EOX : H2_BUFFER_SIZE;
685 codec->tail->info.cnt = 0;
686 dma_cache_wback_inv((unsigned long) codec->tail,
687 sizeof(struct hpc_dma_desc));
688 codec->tail = codec->tail->info.next;
689 } while (codec->tail != codec->head);
692 static int hal2_sync_dac(hal2_card_t *hal2)
694 DECLARE_WAITQUEUE(wait, current);
695 hal2_codec_t *dac = &hal2->dac;
697 signed long timeout = 1000 * H2_BUFFER_SIZE * 2 * dac->voices *
698 HZ / dac->sample_rate / 900;
702 while (dac->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT) {
703 add_wait_queue(&dac->dma_wait, &wait);
704 set_current_state(TASK_INTERRUPTIBLE);
705 if (!schedule_timeout(timeout))
706 /* We may get bogus timeout when system is
708 if (dac->tail->info.cnt) {
709 printk("HAL2: timeout...\n");
712 if (signal_pending(current))
716 hal2_reset_dac_pointer(hal2);
718 remove_wait_queue(&dac->dma_wait, &wait);
726 static int hal2_write_mixer(hal2_card_t *hal2, int index, int vol)
730 DEBUG_MIX("mixer %d write\n", index);
732 if (index >= SOUND_MIXER_NRDEVICES || !mixtable[index].avail)
735 r = (vol >> 8) & 0xff;
742 hal2->mixer.volume[mixtable[index].idx] = l | (r << 8);
744 switch (mixtable[index].idx) {
745 case H2_MIX_OUTPUT_ATT: {
747 DEBUG_MIX("output attenuator %d,%d\n", l, r);
750 unsigned int tmp = hal2_i_look32(hal2, H2I_DAC_C2);
752 tmp &= ~(H2I_C2_L_ATT_M | H2I_C2_R_ATT_M | H2I_C2_MUTE);
754 /* Attenuator has five bits */
755 l = (31 * (100 - l) / 99);
756 r = (31 * (100 - r) / 99);
758 DEBUG_MIX("left: %d, right %d\n", l, r);
760 tmp |= (l << H2I_C2_L_ATT_SHIFT) & H2I_C2_L_ATT_M;
761 tmp |= (r << H2I_C2_R_ATT_SHIFT) & H2I_C2_R_ATT_M;
762 hal2_i_write32(hal2, H2I_DAC_C2, tmp);
764 hal2_i_setbit32(hal2, H2I_DAC_C2, H2I_C2_MUTE);
766 case H2_MIX_INPUT_GAIN: {
773 static void hal2_init_mixer(hal2_card_t *hal2)
777 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
778 hal2_write_mixer(hal2, i, 100 | (100 << 8));
782 static int hal2_mixer_ioctl(hal2_card_t *hal2, unsigned int cmd,
787 if (cmd == SOUND_MIXER_INFO) {
790 strncpy(info.id, hal2str, sizeof(info.id));
791 strncpy(info.name, hal2str, sizeof(info.name));
792 info.modify_counter = hal2->mixer.modcnt;
793 if (copy_to_user((void *)arg, &info, sizeof(info)))
797 if (cmd == SOUND_OLD_MIXER_INFO) {
798 _old_mixer_info info;
800 strncpy(info.id, hal2str, sizeof(info.id));
801 strncpy(info.name, hal2str, sizeof(info.name));
802 if (copy_to_user((void *)arg, &info, sizeof(info)))
806 if (cmd == OSS_GETVERSION)
807 return put_user(SOUND_VERSION, (int *)arg);
809 if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
812 if (_IOC_DIR(cmd) == _IOC_READ) {
813 switch (_IOC_NR(cmd)) {
814 /* Give the current record source */
815 case SOUND_MIXER_RECSRC:
818 /* Give the supported mixers, all of them support stereo */
819 case SOUND_MIXER_DEVMASK:
820 case SOUND_MIXER_STEREODEVS: {
823 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
824 if (mixtable[i].avail)
828 /* Arg contains a bit for each supported recording source */
829 case SOUND_MIXER_RECMASK:
832 case SOUND_MIXER_CAPS:
835 /* Read a specific mixer */
837 int i = _IOC_NR(cmd);
839 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
841 val = hal2->mixer.volume[mixtable[i].idx];
845 return put_user(val, (int *)arg);
848 if (_IOC_DIR(cmd) != (_IOC_WRITE|_IOC_READ))
851 hal2->mixer.modcnt++;
853 if (get_user(val, (int *)arg))
856 switch (_IOC_NR(cmd)) {
857 /* Arg contains a bit for each recording source */
858 case SOUND_MIXER_RECSRC:
859 return 0; /* FIXME */
861 return hal2_write_mixer(hal2, _IOC_NR(cmd), val);
867 static int hal2_open_mixdev(struct inode *inode, struct file *file)
869 hal2_card_t *hal2 = hal2_mixer_find_card(MINOR(inode->i_rdev));
872 file->private_data = hal2;
878 static int hal2_release_mixdev(struct inode *inode, struct file *file)
883 static int hal2_ioctl_mixdev(struct inode *inode, struct file *file,
884 unsigned int cmd, unsigned long arg)
886 return hal2_mixer_ioctl((hal2_card_t *)file->private_data, cmd, arg);
890 static int hal2_ioctl(struct inode *inode, struct file *file,
891 unsigned int cmd, unsigned long arg)
894 hal2_card_t *hal2 = (hal2_card_t *) file->private_data;
898 return put_user(SOUND_VERSION, (int *)arg);
900 case SNDCTL_DSP_SYNC:
901 if (file->f_mode & FMODE_WRITE)
902 return hal2_sync_dac(hal2);
905 case SNDCTL_DSP_SETDUPLEX:
908 case SNDCTL_DSP_GETCAPS:
909 return put_user(DSP_CAP_DUPLEX | DSP_CAP_MULTI, (int *)arg);
911 case SNDCTL_DSP_RESET:
912 if (file->f_mode & FMODE_READ) {
914 hal2_reset_adc_pointer(hal2);
916 if (file->f_mode & FMODE_WRITE) {
918 hal2_reset_dac_pointer(hal2);
922 case SNDCTL_DSP_SPEED:
923 if (get_user(val, (int *)arg))
925 if (file->f_mode & FMODE_READ) {
927 val = hal2_compute_rate(&hal2->adc, val);
928 hal2->adc.sample_rate = val;
929 hal2_set_adc_rate(hal2);
931 if (file->f_mode & FMODE_WRITE) {
933 val = hal2_compute_rate(&hal2->dac, val);
934 hal2->dac.sample_rate = val;
935 hal2_set_dac_rate(hal2);
937 return put_user(val, (int *)arg);
939 case SNDCTL_DSP_STEREO:
940 if (get_user(val, (int *)arg))
942 if (file->f_mode & FMODE_READ) {
944 hal2->adc.voices = (val) ? 2 : 1;
945 hal2_setup_adc(hal2);
947 if (file->f_mode & FMODE_WRITE) {
949 hal2->dac.voices = (val) ? 2 : 1;
950 hal2_setup_dac(hal2);
954 case SNDCTL_DSP_CHANNELS:
955 if (get_user(val, (int *)arg))
958 if (file->f_mode & FMODE_READ) {
960 hal2->adc.voices = (val == 1) ? 1 : 2;
961 hal2_setup_adc(hal2);
963 if (file->f_mode & FMODE_WRITE) {
965 hal2->dac.voices = (val == 1) ? 1 : 2;
966 hal2_setup_dac(hal2);
970 if (file->f_mode & FMODE_READ)
971 val = hal2->adc.voices;
972 if (file->f_mode & FMODE_WRITE)
973 val = hal2->dac.voices;
974 return put_user(val, (int *)arg);
976 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
977 return put_user(H2_SUPPORTED_FORMATS, (int *)arg);
979 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
980 if (get_user(val, (int *)arg))
982 if (val != AFMT_QUERY) {
983 if (!(val & H2_SUPPORTED_FORMATS))
985 if (file->f_mode & FMODE_READ) {
987 hal2->adc.format = val;
988 hal2_setup_adc(hal2);
990 if (file->f_mode & FMODE_WRITE) {
992 hal2->dac.format = val;
993 hal2_setup_dac(hal2);
997 if (file->f_mode & FMODE_READ)
998 val = hal2->adc.format;
999 if (file->f_mode & FMODE_WRITE)
1000 val = hal2->dac.format;
1002 return put_user(val, (int *)arg);
1004 case SNDCTL_DSP_POST:
1007 case SNDCTL_DSP_GETOSPACE: {
1008 unsigned long flags;
1009 audio_buf_info info;
1011 hal2_codec_t *dac = &hal2->dac;
1013 if (!(file->f_mode & FMODE_WRITE))
1016 spin_lock_irqsave(&dac->lock, flags);
1019 while (buf->info.cnt == 0 && buf != dac->tail) {
1021 buf = buf->info.next;
1023 spin_unlock_irqrestore(&dac->lock, flags);
1025 info.fragstotal = obuffers;
1026 info.fragsize = H2_BUFFER_SIZE;
1027 info.bytes = info.fragsize * info.fragments;
1029 return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
1032 case SNDCTL_DSP_GETISPACE: {
1033 unsigned long flags;
1034 audio_buf_info info;
1036 hal2_codec_t *adc = &hal2->adc;
1038 if (!(file->f_mode & FMODE_READ))
1041 spin_lock_irqsave(&adc->lock, flags);
1045 while (buf->info.cnt > 0 && buf != adc->head) {
1047 info.bytes += buf->info.cnt;
1048 buf = buf->info.next;
1050 spin_unlock_irqrestore(&adc->lock, flags);
1052 info.fragstotal = ibuffers;
1053 info.fragsize = H2_BUFFER_SIZE;
1055 return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
1058 case SNDCTL_DSP_NONBLOCK:
1059 file->f_flags |= O_NONBLOCK;
1062 case SNDCTL_DSP_GETBLKSIZE:
1063 return put_user(H2_BUFFER_SIZE, (int *)arg);
1065 case SNDCTL_DSP_SETFRAGMENT:
1068 case SOUND_PCM_READ_RATE:
1070 if (file->f_mode & FMODE_READ)
1071 val = hal2->adc.sample_rate;
1072 if (file->f_mode & FMODE_WRITE)
1073 val = hal2->dac.sample_rate;
1074 return put_user(val, (int *)arg);
1076 case SOUND_PCM_READ_CHANNELS:
1078 if (file->f_mode & FMODE_READ)
1079 val = hal2->adc.voices;
1080 if (file->f_mode & FMODE_WRITE)
1081 val = hal2->dac.voices;
1082 return put_user(val, (int *)arg);
1084 case SOUND_PCM_READ_BITS:
1086 return put_user(val, (int *)arg);
1089 return hal2_mixer_ioctl(hal2, cmd, arg);
1092 static ssize_t hal2_read(struct file *file, char *buffer,
1093 size_t count, loff_t *ppos)
1096 hal2_card_t *hal2 = (hal2_card_t *) file->private_data;
1097 hal2_codec_t *adc = &hal2->adc;
1101 if (ppos != &file->f_pos)
1106 if (file->f_flags & O_NONBLOCK) {
1107 err = hal2_get_buffer(hal2, buffer, count);
1108 err = err == 0 ? -EAGAIN : err;
1112 signed long timeout = 1000 * H2_BUFFER_SIZE *
1113 2 * adc->voices * HZ / adc->sample_rate / 900;
1114 DECLARE_WAITQUEUE(wait, current);
1117 err = hal2_get_buffer(hal2, buffer, count);
1124 if (count > 0 && err >= 0) {
1125 add_wait_queue(&adc->dma_wait, &wait);
1126 set_current_state(TASK_INTERRUPTIBLE);
1127 /* Well, it is possible, that interrupt already
1128 * arrived. Hmm, shit happens, we have one more
1129 * buffer filled ;) */
1130 if (!schedule_timeout(timeout))
1131 /* We may get bogus timeout when system
1132 * is heavily loaded */
1133 if (!adc->tail->info.cnt) {
1134 printk("HAL2: timeout...\n");
1135 hal2_stop_adc(hal2);
1136 hal2_reset_adc_pointer(hal2);
1139 if (signal_pending(current))
1141 remove_wait_queue(&adc->dma_wait, &wait);
1143 } while (count > 0 && err >= 0);
1152 static ssize_t hal2_write(struct file *file, const char *buffer,
1153 size_t count, loff_t *ppos)
1156 char *buf = (char*) buffer;
1157 hal2_card_t *hal2 = (hal2_card_t *) file->private_data;
1158 hal2_codec_t *dac = &hal2->dac;
1162 if (ppos != &file->f_pos)
1167 if (file->f_flags & O_NONBLOCK) {
1168 err = hal2_add_buffer(hal2, buf, count);
1169 err = err == 0 ? -EAGAIN : err;
1173 signed long timeout = 1000 * H2_BUFFER_SIZE *
1174 2 * dac->voices * HZ / dac->sample_rate / 900;
1175 DECLARE_WAITQUEUE(wait, current);
1178 err = hal2_add_buffer(hal2, buf, count);
1185 if (count > 0 && err >= 0) {
1186 add_wait_queue(&dac->dma_wait, &wait);
1187 set_current_state(TASK_INTERRUPTIBLE);
1188 /* Well, it is possible, that interrupt already
1189 * arrived. Hmm, shit happens, we have one more
1191 if (!schedule_timeout(timeout))
1192 /* We may get bogus timeout when system
1193 * is heavily loaded */
1194 if (dac->head->info.cnt) {
1195 printk("HAL2: timeout...\n");
1196 hal2_stop_dac(hal2);
1197 hal2_reset_dac_pointer(hal2);
1200 if (signal_pending(current))
1202 remove_wait_queue(&dac->dma_wait, &wait);
1204 } while (count > 0 && err >= 0);
1212 static unsigned int hal2_poll(struct file *file, struct poll_table_struct *wait)
1214 unsigned long flags;
1215 unsigned int mask = 0;
1216 hal2_card_t *hal2 = (hal2_card_t *) file->private_data;
1218 if (file->f_mode & FMODE_READ) {
1219 hal2_codec_t *adc = &hal2->adc;
1221 poll_wait(file, &hal2->adc.dma_wait, wait);
1222 spin_lock_irqsave(&adc->lock, flags);
1223 if (adc->tail->info.cnt > 0)
1225 spin_unlock_irqrestore(&adc->lock, flags);
1228 if (file->f_mode & FMODE_WRITE) {
1229 hal2_codec_t *dac = &hal2->dac;
1231 poll_wait(file, &dac->dma_wait, wait);
1232 spin_lock_irqsave(&dac->lock, flags);
1233 if (dac->head->info.cnt == 0)
1235 spin_unlock_irqrestore(&dac->lock, flags);
1241 static int hal2_open(struct inode *inode, struct file *file)
1244 hal2_card_t *hal2 = hal2_dsp_find_card(MINOR(inode->i_rdev));
1246 DEBUG("opening audio device.\n");
1249 printk("HAL2: Whee?! Open door and go away!\n");
1252 file->private_data = hal2;
1254 if (file->f_mode & FMODE_READ) {
1255 if (hal2->adc.usecount)
1258 /* OSS spec wanted us to use 8 bit, 8 kHz mono by default,
1259 * but HAL2 can't do 8bit audio */
1260 hal2->adc.format = AFMT_S16_BE;
1261 hal2->adc.voices = 1;
1262 hal2->adc.sample_rate = hal2_compute_rate(&hal2->adc, 8000);
1263 hal2_set_adc_rate(hal2);
1265 /* alloc DMA buffers */
1266 err = hal2_alloc_adc_dmabuf(hal2);
1269 hal2_setup_adc(hal2);
1271 hal2->adc.usecount++;
1274 if (file->f_mode & FMODE_WRITE) {
1275 if (hal2->dac.usecount)
1278 hal2->dac.format = AFMT_S16_BE;
1279 hal2->dac.voices = 1;
1280 hal2->dac.sample_rate = hal2_compute_rate(&hal2->dac, 8000);
1281 hal2_set_dac_rate(hal2);
1283 /* alloc DMA buffers */
1284 err = hal2_alloc_dac_dmabuf(hal2);
1287 hal2_setup_dac(hal2);
1289 hal2->dac.usecount++;
1295 static int hal2_release(struct inode *inode, struct file *file)
1297 hal2_card_t *hal2 = (hal2_card_t *) file->private_data;
1299 if (file->f_mode & FMODE_READ) {
1300 hal2_stop_adc(hal2);
1301 hal2_free_adc_dmabuf(hal2);
1302 hal2->adc.usecount--;
1305 if (file->f_mode & FMODE_WRITE) {
1306 hal2_sync_dac(hal2);
1307 hal2_free_dac_dmabuf(hal2);
1308 hal2->dac.usecount--;
1314 static struct file_operations hal2_audio_fops = {
1322 release: hal2_release,
1325 static struct file_operations hal2_mixer_fops = {
1328 ioctl: hal2_ioctl_mixdev,
1329 open: hal2_open_mixdev,
1330 release: hal2_release_mixdev,
1333 static int hal2_request_irq(hal2_card_t *hal2, int irq)
1335 unsigned long flags;
1338 save_and_cli(flags);
1339 if (request_irq(irq, hal2_interrupt, SA_SHIRQ, hal2str, hal2)) {
1340 printk(KERN_ERR "HAL2: Can't get irq %d\n", irq);
1343 restore_flags(flags);
1347 static int hal2_alloc_resources(hal2_card_t *hal2, struct hpc3_regs *hpc3)
1351 pbus = &hal2->dac.pbus;
1353 pbus->pbus = &hpc3->pbdma[pbus->pbusnr];
1354 /* The spec says that we should write 0x08248844 but that's WRONG. HAL2
1355 * does 8 bit DMA, not 16 bit even if it generates 16 bit audio. */
1356 hpc3->pbus_dmacfgs[pbus->pbusnr][0] = 0x08208844; /* Magic :-) */
1358 pbus = &hal2->adc.pbus;
1360 pbus->pbus = &hpc3->pbdma[pbus->pbusnr];
1361 hpc3->pbus_dmacfgs[pbus->pbusnr][0] = 0x08208844; /* Magic :-) */
1363 return hal2_request_irq(hal2, SGI_HPCDMA_IRQ);
1366 static void hal2_init_codec(hal2_codec_t *codec)
1368 init_waitqueue_head(&codec->dma_wait);
1369 init_MUTEX(&codec->sem);
1370 spin_lock_init(&codec->lock);
1373 static void hal2_free_resources(hal2_card_t *hal2)
1375 free_irq(SGI_HPCDMA_IRQ, hal2);
1378 static int hal2_detect(hal2_card_t *hal2)
1380 unsigned short board, major, minor;
1384 hal2_isr_write(hal2, 0);
1387 hal2_isr_write(hal2, H2_ISR_GLOBAL_RESET_N | H2_ISR_CODEC_RESET_N);
1389 hal2_i_write16(hal2, H2I_RELAY_C, H2I_RELAY_C_STATE);
1391 if ((rev = hal2_rev_look(hal2)) & H2_REV_AUDIO_PRESENT) {
1392 DEBUG("HAL2: no device detected, rev: 0x%04hx\n", rev);
1396 board = (rev & H2_REV_BOARD_M) >> 12;
1397 major = (rev & H2_REV_MAJOR_CHIP_M) >> 4;
1398 minor = (rev & H2_REV_MINOR_CHIP_M);
1400 printk("SGI HAL2 Processor revision %i.%i.%i detected\n",
1401 board, major, minor);
1403 if (board != 4 || major != 1 || minor != 0)
1404 printk( "Other revision than 4.1.0 detected. "
1405 "Your card is probably unsupported\n");
1410 static int hal2_init_card(hal2_card_t **phal2, struct hpc3_regs *hpc3,
1411 unsigned long hpc3_base)
1416 hal2 = (hal2_card_t *) kmalloc(sizeof(hal2_card_t), GFP_KERNEL);
1419 memset(hal2, 0, sizeof(hal2_card_t));
1421 hal2->ctl_regs = (hal2_ctl_regs_t *) KSEG1ADDR(hpc3_base + H2_CTL_PIO);
1422 hal2->aes_regs = (hal2_aes_regs_t *) KSEG1ADDR(hpc3_base + H2_AES_PIO);
1423 hal2->vol_regs = (hal2_vol_regs_t *) KSEG1ADDR(hpc3_base + H2_VOL_PIO);
1424 hal2->syn_regs = (hal2_syn_regs_t *) KSEG1ADDR(hpc3_base + H2_SYN_PIO);
1426 if (hal2_detect(hal2) < 0) {
1427 printk("HAL2 audio processor not found\n");
1432 hal2_init_codec(&hal2->dac);
1433 hal2_init_codec(&hal2->adc);
1435 ret = hal2_alloc_resources(hal2, hpc3);
1439 hal2_init_mixer(hal2);
1441 hal2->dev_dsp = register_sound_dsp(&hal2_audio_fops, -1);
1442 if (hal2->dev_dsp < 0) {
1443 ret = hal2->dev_dsp;
1447 hal2->dev_mixer = register_sound_mixer(&hal2_mixer_fops, -1);
1448 if (hal2->dev_mixer < 0) {
1449 ret = hal2->dev_mixer;
1456 unregister_sound_dsp(hal2->dev_dsp);
1458 hal2_free_resources(hal2);
1466 * We are assuming only one HAL2 card. If you ever meet machine with more than
1467 * one, tell immediately about it to someone. Preferably to me. --ladis
1469 static int __init init_hal2(void)
1473 for (i = 0; i < MAXCARDS; i++)
1474 hal2_card[i] = NULL;
1476 return hal2_init_card(&hal2_card[0], hpc3c0, HPC3_CHIP0_PBASE);
1479 static void __exit exit_hal2(void)
1483 for (i = 0; i < MAXCARDS; i++)
1485 hal2_free_resources(hal2_card[i]);
1486 unregister_sound_dsp(hal2_card[i]->dev_dsp);
1487 unregister_sound_mixer(hal2_card[i]->dev_mixer);
1488 kfree(hal2_card[i]);
1492 module_init(init_hal2);
1493 module_exit(exit_hal2);
1495 MODULE_DESCRIPTION("OSS compatible driver for SGI HAL2 audio");
1496 MODULE_AUTHOR("Ladislav Michl");
1497 MODULE_LICENSE("GPL");