neo.c revision 1.32 1 /* $NetBSD: neo.c,v 1.32 2006/09/02 07:23:53 christos Exp $ */
2
3 /*
4 * Copyright (c) 1999 Cameron Grant <gandalf (at) vilnya.demon.co.uk>
5 * All rights reserved.
6 *
7 * Derived from the public domain Linux driver
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * FreeBSD: src/sys/dev/sound/pci/neomagic.c,v 1.8 2000/03/20 15:30:50 cg Exp
31 * OpenBSD: neo.c,v 1.4 2000/07/19 09:04:37 csapuntz Exp
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: neo.c,v 1.32 2006/09/02 07:23:53 christos Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/device.h>
42
43 #include <machine/bus.h>
44
45 #include <dev/pci/pcidevs.h>
46 #include <dev/pci/pcivar.h>
47
48 #include <dev/pci/neoreg.h>
49 #include <dev/pci/neo-coeff.h>
50
51 #include <sys/audioio.h>
52 #include <dev/audio_if.h>
53 #include <dev/mulaw.h>
54 #include <dev/auconv.h>
55
56 #include <dev/ic/ac97var.h>
57
58
59 /* -------------------------------------------------------------------- */
60 /*
61 * As of 04/13/00, public documentation on the Neomagic 256 is not available.
62 * These comments were gleaned by looking at the driver carefully.
63 *
64 * The Neomagic 256 AV/ZX chips provide both video and audio capabilities
65 * on one chip. About 2-6 megabytes of memory are associated with
66 * the chip. Most of this goes to video frame buffers, but some is used for
67 * audio buffering
68 *
69 * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA.
70 * Instead, the chip allows you to carve out two ring buffers out of its
71 * memory. However you carve this and how much you can carve seems to be
72 * voodoo. The algorithm is in nm_init.
73 *
74 * Most Neomagic audio chips use the AC-97 codec interface. However, there
75 * seem to be a select few chips 256AV chips that do not support AC-97.
76 * This driver does not support them but there are rumors that it
77 * might work with wss isa drivers. This might require some playing around
78 * with your BIOS.
79 *
80 * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of
81 * them describe a memory region. The frame buffer is the first region
82 * and the register set is the secodn region.
83 *
84 * The register manipulation logic is taken from the Linux driver,
85 * which is in the public domain.
86 *
87 * The Neomagic is even nice enough to map the AC-97 codec registers into
88 * the register space to allow direct manipulation. Watch out, accessing
89 * AC-97 registers on the Neomagic requires great delicateness, otherwise
90 * the thing will hang the PCI bus, rendering your system frozen.
91 *
92 * For one, it seems the Neomagic status register that reports AC-97
93 * readiness should NOT be polled more often than once each 1ms.
94 *
95 * Also, writes to the AC-97 register space may take order 40us to
96 * complete.
97 *
98 * Unlike many sound engines, the Neomagic does not support (as far as
99 * we know :) the notion of interrupting every n bytes transferred,
100 * unlike many DMA engines. Instead, it allows you to specify one
101 * location in each ring buffer (called the watermark). When the chip
102 * passes that location while playing, it signals an interrupt.
103 *
104 * The ring buffer size is currently 16k. That is about 100ms of audio
105 * at 44.1kHz/stero/16 bit. However, to keep the buffer full, interrupts
106 * are generated more often than that, so 20-40 interrupts per second
107 * should not be unexpected. Increasing BUFFSIZE should help minimize
108 * of glitches due to drivers that spend to much time looping at high
109 * privelege levels as well as the impact of badly written audio
110 * interface clients.
111 *
112 * TO-DO list:
113 * Figure out interaction with video stuff (look at Xfree86 driver?)
114 *
115 * Figure out how to shrink that huge table neo-coeff.h
116 */
117
118 #define NM_BUFFSIZE 16384
119
120 /* device private data */
121 struct neo_softc {
122 struct device dev;
123
124 bus_space_tag_t bufiot;
125 bus_space_handle_t bufioh;
126
127 bus_space_tag_t regiot;
128 bus_space_handle_t regioh;
129
130 uint32_t type;
131 void *ih;
132
133 void (*pintr)(void *); /* DMA completion intr handler */
134 void *parg; /* arg for intr() */
135
136 void (*rintr)(void *); /* DMA completion intr handler */
137 void *rarg; /* arg for intr() */
138
139 vaddr_t buf_vaddr;
140 vaddr_t rbuf_vaddr;
141 vaddr_t pbuf_vaddr;
142 int pbuf_allocated;
143 int rbuf_allocated;
144
145 bus_addr_t buf_pciaddr;
146 bus_addr_t rbuf_pciaddr;
147 bus_addr_t pbuf_pciaddr;
148
149 uint32_t ac97_base, ac97_status, ac97_busy;
150 uint32_t buftop, pbuf, rbuf, cbuf, acbuf;
151 uint32_t playint, recint, misc1int, misc2int;
152 uint32_t irsz, badintr;
153
154 uint32_t pbufsize;
155 uint32_t rbufsize;
156
157 uint32_t pblksize;
158 uint32_t rblksize;
159
160 uint32_t pwmark;
161 uint32_t rwmark;
162
163 struct ac97_codec_if *codec_if;
164 struct ac97_host_if host_if;
165
166 void *powerhook;
167 };
168
169 /* -------------------------------------------------------------------- */
170
171 /*
172 * prototypes
173 */
174
175 static int nm_waitcd(struct neo_softc *);
176 static int nm_loadcoeff(struct neo_softc *, int, int);
177 static int nm_init(struct neo_softc *);
178
179 static int neo_match(struct device *, struct cfdata *, void *);
180 static void neo_attach(struct device *, struct device *, void *);
181 static int neo_intr(void *);
182
183 static int neo_query_encoding(void *, struct audio_encoding *);
184 static int neo_set_params(void *, int, int, audio_params_t *,
185 audio_params_t *, stream_filter_list_t *,
186 stream_filter_list_t *);
187 static int neo_round_blocksize(void *, int, int, const audio_params_t *);
188 static int neo_trigger_output(void *, void *, void *, int,
189 void (*)(void *), void *,
190 const audio_params_t *);
191 static int neo_trigger_input(void *, void *, void *, int,
192 void (*)(void *), void *,
193 const audio_params_t *);
194 static int neo_halt_output(void *);
195 static int neo_halt_input(void *);
196 static int neo_getdev(void *, struct audio_device *);
197 static int neo_mixer_set_port(void *, mixer_ctrl_t *);
198 static int neo_mixer_get_port(void *, mixer_ctrl_t *);
199 static int neo_attach_codec(void *, struct ac97_codec_if *);
200 static int neo_read_codec(void *, uint8_t, uint16_t *);
201 static int neo_write_codec(void *, uint8_t, uint16_t);
202 static int neo_reset_codec(void *);
203 static enum ac97_host_flags neo_flags_codec(void *);
204 static int neo_query_devinfo(void *, mixer_devinfo_t *);
205 static void * neo_malloc(void *, int, size_t, struct malloc_type *, int);
206 static void neo_free(void *, void *, struct malloc_type *);
207 static size_t neo_round_buffersize(void *, int, size_t);
208 static paddr_t neo_mappage(void *, void *, off_t, int);
209 static int neo_get_props(void *);
210 static void neo_power(int, void *);
211
212 CFATTACH_DECL(neo, sizeof(struct neo_softc),
213 neo_match, neo_attach, NULL, NULL);
214
215 static struct audio_device neo_device = {
216 "NeoMagic 256",
217 "",
218 "neo"
219 };
220
221 /* The actual rates supported by the card. */
222 static const int samplerates[9] = {
223 8000,
224 11025,
225 16000,
226 22050,
227 24000,
228 32000,
229 44100,
230 48000,
231 99999999
232 };
233
234 #define NEO_NFORMATS 4
235 static const struct audio_format neo_formats[NEO_NFORMATS] = {
236 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
237 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
238 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
239 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
240 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
241 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
242 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
243 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
244 };
245
246 /* -------------------------------------------------------------------- */
247
248 static const struct audio_hw_if neo_hw_if = {
249 NULL, /* open */
250 NULL, /* close */
251 NULL, /* drain */
252 neo_query_encoding,
253 neo_set_params,
254 neo_round_blocksize,
255 NULL, /* commit_setting */
256 NULL, /* init_output */
257 NULL, /* init_input */
258 NULL, /* start_output */
259 NULL, /* start_input */
260 neo_halt_output,
261 neo_halt_input,
262 NULL, /* speaker_ctl */
263 neo_getdev,
264 NULL, /* getfd */
265 neo_mixer_set_port,
266 neo_mixer_get_port,
267 neo_query_devinfo,
268 neo_malloc,
269 neo_free,
270 neo_round_buffersize,
271 neo_mappage,
272 neo_get_props,
273 neo_trigger_output,
274 neo_trigger_input,
275 NULL,
276 NULL,
277 };
278
279 /* -------------------------------------------------------------------- */
280
281 #define nm_rd_1(sc, regno) \
282 bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
283
284 #define nm_rd_2(sc, regno) \
285 bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
286
287 #define nm_rd_4(sc, regno) \
288 bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
289
290 #define nm_wr_1(sc, regno, val) \
291 bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
292
293 #define nm_wr_2(sc, regno, val) \
294 bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
295
296 #define nm_wr_4(sc, regno, val) \
297 bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
298
299 #define nm_rdbuf_4(sc, regno) \
300 bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
301
302 #define nm_wrbuf_1(sc, regno, val) \
303 bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
304
305 /* ac97 codec */
306 static int
307 nm_waitcd(struct neo_softc *sc)
308 {
309 int cnt;
310 int fail;
311
312 cnt = 10;
313 fail = 1;
314 while (cnt-- > 0) {
315 if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
316 DELAY(100);
317 else {
318 fail = 0;
319 break;
320 }
321 }
322 return fail;
323 }
324
325 static void
326 nm_ackint(struct neo_softc *sc, uint32_t num)
327 {
328
329 switch (sc->type) {
330 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
331 nm_wr_2(sc, NM_INT_REG, num << 1);
332 break;
333
334 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
335 nm_wr_4(sc, NM_INT_REG, num);
336 break;
337 }
338 }
339
340 static int
341 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
342 {
343 int ofs, sz, i;
344 uint32_t addr;
345
346 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
347 if (dir == AUMODE_RECORD)
348 num += 8;
349 sz = coefficientSizes[num];
350 ofs = 0;
351 while (num-- > 0)
352 ofs+= coefficientSizes[num];
353 for (i = 0; i < sz; i++)
354 nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
355 nm_wr_4(sc, addr, sc->cbuf);
356 if (dir == AUMODE_PLAY)
357 sz--;
358 nm_wr_4(sc, addr + 4, sc->cbuf + sz);
359 return 0;
360 }
361
362 /* The interrupt handler */
363 static int
364 neo_intr(void *p)
365 {
366 struct neo_softc *sc;
367 int status, x;
368 int rv;
369
370 sc = (struct neo_softc *)p;
371 rv = 0;
372 status = (sc->irsz == 2) ?
373 nm_rd_2(sc, NM_INT_REG) :
374 nm_rd_4(sc, NM_INT_REG);
375
376 if (status & sc->playint) {
377 status &= ~sc->playint;
378
379 sc->pwmark += sc->pblksize;
380 sc->pwmark %= sc->pbufsize;
381
382 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
383
384 nm_ackint(sc, sc->playint);
385
386 if (sc->pintr)
387 (*sc->pintr)(sc->parg);
388
389 rv = 1;
390 }
391 if (status & sc->recint) {
392 status &= ~sc->recint;
393
394 sc->rwmark += sc->rblksize;
395 sc->rwmark %= sc->rbufsize;
396 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
397 nm_ackint(sc, sc->recint);
398 if (sc->rintr)
399 (*sc->rintr)(sc->rarg);
400
401 rv = 1;
402 }
403 if (status & sc->misc1int) {
404 status &= ~sc->misc1int;
405 nm_ackint(sc, sc->misc1int);
406 x = nm_rd_1(sc, 0x400);
407 nm_wr_1(sc, 0x400, x | 2);
408 printf("%s: misc int 1\n", sc->dev.dv_xname);
409 rv = 1;
410 }
411 if (status & sc->misc2int) {
412 status &= ~sc->misc2int;
413 nm_ackint(sc, sc->misc2int);
414 x = nm_rd_1(sc, 0x400);
415 nm_wr_1(sc, 0x400, x & ~2);
416 printf("%s: misc int 2\n", sc->dev.dv_xname);
417 rv = 1;
418 }
419 if (status) {
420 status &= ~sc->misc2int;
421 nm_ackint(sc, sc->misc2int);
422 printf("%s: unknown int\n", sc->dev.dv_xname);
423 rv = 1;
424 }
425
426 return rv;
427 }
428
429 /* -------------------------------------------------------------------- */
430
431 /*
432 * Probe and attach the card
433 */
434
435 static int
436 nm_init(struct neo_softc *sc)
437 {
438 uint32_t ofs, i;
439
440 switch (sc->type) {
441 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
442 sc->ac97_base = NM_MIXER_OFFSET;
443 sc->ac97_status = NM_MIXER_STATUS_OFFSET;
444 sc->ac97_busy = NM_MIXER_READY_MASK;
445
446 sc->buftop = 2560 * 1024;
447
448 sc->irsz = 2;
449 sc->playint = NM_PLAYBACK_INT;
450 sc->recint = NM_RECORD_INT;
451 sc->misc1int = NM_MISC_INT_1;
452 sc->misc2int = NM_MISC_INT_2;
453 break;
454
455 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
456 sc->ac97_base = NM_MIXER_OFFSET;
457 sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
458 sc->ac97_busy = NM2_MIXER_READY_MASK;
459
460 sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
461
462 sc->irsz = 4;
463 sc->playint = NM2_PLAYBACK_INT;
464 sc->recint = NM2_RECORD_INT;
465 sc->misc1int = NM2_MISC_INT_1;
466 sc->misc2int = NM2_MISC_INT_2;
467 break;
468 #ifdef DIAGNOSTIC
469 default:
470 panic("nm_init: impossible");
471 #endif
472 }
473
474 sc->badintr = 0;
475 ofs = sc->buftop - 0x0400;
476 sc->buftop -= 0x1400;
477
478 if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
479 i = nm_rdbuf_4(sc, ofs + 4);
480 if (i != 0 && i != 0xffffffff)
481 sc->buftop = i;
482 }
483
484 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
485 sc->rbuf = sc->cbuf - NM_BUFFSIZE;
486 sc->pbuf = sc->rbuf - NM_BUFFSIZE;
487 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
488
489 sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
490 sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
491 sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
492
493 sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
494 sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
495
496 nm_wr_1(sc, 0, 0x11);
497 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
498 nm_wr_2(sc, 0x214, 0);
499
500 return 0;
501 }
502
503 static int
504 neo_match(struct device *parent, struct cfdata *match, void *aux)
505 {
506 struct pci_attach_args *pa;
507 pcireg_t subdev;
508
509 pa = aux;
510 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
511 return 0;
512
513 subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
514
515 switch (PCI_PRODUCT(pa->pa_id)) {
516 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
517 /*
518 * We have to weed-out the non-AC'97 versions of
519 * the chip (i.e. the ones that are known to work
520 * in WSS emulation mode), as they won't work with
521 * this driver.
522 */
523 switch (PCI_VENDOR(subdev)) {
524 case PCI_VENDOR_DELL:
525 switch (PCI_PRODUCT(subdev)) {
526 case 0x008f:
527 return 0;
528 }
529 break;
530
531 case PCI_VENDOR_HP:
532 switch (PCI_PRODUCT(subdev)) {
533 case 0x0007:
534 return 0;
535 }
536 break;
537
538 case PCI_VENDOR_IBM:
539 switch (PCI_PRODUCT(subdev)) {
540 case 0x00dd:
541 return 0;
542 }
543 break;
544 }
545 return 1;
546
547 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
548 return 1;
549 }
550
551 return 0;
552 }
553
554 static void
555 neo_power(int why, void *addr)
556 {
557 struct neo_softc *sc;
558
559 sc = (struct neo_softc *)addr;
560 if (why == PWR_RESUME) {
561 nm_init(sc);
562 sc->codec_if->vtbl->restore_ports(sc->codec_if);
563 }
564 }
565
566 static void
567 neo_attach(struct device *parent, struct device *self, void *aux)
568 {
569 struct neo_softc *sc;
570 struct pci_attach_args *pa;
571 pci_chipset_tag_t pc;
572 char const *intrstr;
573 pci_intr_handle_t ih;
574 pcireg_t csr;
575
576 sc = (struct neo_softc *)self;
577 pa = (struct pci_attach_args *)aux;
578 pc = pa->pa_pc;
579
580 sc->type = PCI_PRODUCT(pa->pa_id);
581
582 printf(": NeoMagic 256%s audio\n",
583 sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
584
585 /* Map I/O register */
586 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
587 &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
588 printf("%s: can't map buffer\n", sc->dev.dv_xname);
589 return;
590 }
591
592 if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
593 BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) {
594 printf("%s: can't map registers\n", sc->dev.dv_xname);
595 return;
596 }
597
598 /* Map and establish the interrupt. */
599 if (pci_intr_map(pa, &ih)) {
600 printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
601 return;
602 }
603
604 intrstr = pci_intr_string(pc, ih);
605 sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
606
607 if (sc->ih == NULL) {
608 printf("%s: couldn't establish interrupt",
609 sc->dev.dv_xname);
610 if (intrstr != NULL)
611 printf(" at %s", intrstr);
612 printf("\n");
613 return;
614 }
615 printf("%s: interrupting at %s\n", sc->dev.dv_xname, intrstr);
616
617 if (nm_init(sc) != 0)
618 return;
619
620 /* Enable the device. */
621 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
622 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
623 csr | PCI_COMMAND_MASTER_ENABLE);
624
625 sc->host_if.arg = sc;
626
627 sc->host_if.attach = neo_attach_codec;
628 sc->host_if.read = neo_read_codec;
629 sc->host_if.write = neo_write_codec;
630 sc->host_if.reset = neo_reset_codec;
631 sc->host_if.flags = neo_flags_codec;
632
633 if (ac97_attach(&sc->host_if, self) != 0)
634 return;
635
636 sc->powerhook = powerhook_establish(neo_power, sc);
637
638 audio_attach_mi(&neo_hw_if, sc, &sc->dev);
639 }
640
641 static int
642 neo_read_codec(void *v, uint8_t a, uint16_t *d)
643 {
644 struct neo_softc *sc;
645
646 sc = v;
647 if (!nm_waitcd(sc)) {
648 *d = nm_rd_2(sc, sc->ac97_base + a);
649 DELAY(1000);
650 return 0;
651 }
652
653 return ENXIO;
654 }
655
656
657 static int
658 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
659 {
660 struct neo_softc *sc;
661 int cnt;
662
663 sc = v;
664 cnt = 3;
665 if (!nm_waitcd(sc)) {
666 while (cnt-- > 0) {
667 nm_wr_2(sc, sc->ac97_base + a, d);
668 if (!nm_waitcd(sc)) {
669 DELAY(1000);
670 return 0;
671 }
672 }
673 }
674
675 return ENXIO;
676 }
677
678 static int
679 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
680 {
681 struct neo_softc *sc;
682
683 sc = v;
684 sc->codec_if = codec_if;
685 return 0;
686 }
687
688 static int
689 neo_reset_codec(void *v)
690 {
691 struct neo_softc *sc;
692
693 sc = v;
694 nm_wr_1(sc, 0x6c0, 0x01);
695 nm_wr_1(sc, 0x6cc, 0x87);
696 nm_wr_1(sc, 0x6cc, 0x80);
697 nm_wr_1(sc, 0x6cc, 0x00);
698 return 0;
699 }
700
701 static enum ac97_host_flags
702 neo_flags_codec(void *v)
703 {
704
705 return AC97_HOST_DONT_READ;
706 }
707
708 static int
709 neo_query_encoding(void *addr, struct audio_encoding *fp)
710 {
711
712 switch (fp->index) {
713 case 0:
714 strcpy(fp->name, AudioEulinear);
715 fp->encoding = AUDIO_ENCODING_ULINEAR;
716 fp->precision = 8;
717 fp->flags = 0;
718 return 0;
719 case 1:
720 strcpy(fp->name, AudioEmulaw);
721 fp->encoding = AUDIO_ENCODING_ULAW;
722 fp->precision = 8;
723 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
724 return 0;
725 case 2:
726 strcpy(fp->name, AudioEalaw);
727 fp->encoding = AUDIO_ENCODING_ALAW;
728 fp->precision = 8;
729 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
730 return 0;
731 case 3:
732 strcpy(fp->name, AudioEslinear);
733 fp->encoding = AUDIO_ENCODING_SLINEAR;
734 fp->precision = 8;
735 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
736 return (0);
737 case 4:
738 strcpy(fp->name, AudioEslinear_le);
739 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
740 fp->precision = 16;
741 fp->flags = 0;
742 return 0;
743 case 5:
744 strcpy(fp->name, AudioEulinear_le);
745 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
746 fp->precision = 16;
747 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
748 return 0;
749 case 6:
750 strcpy(fp->name, AudioEslinear_be);
751 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
752 fp->precision = 16;
753 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
754 return 0;
755 case 7:
756 strcpy(fp->name, AudioEulinear_be);
757 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
758 fp->precision = 16;
759 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
760 return 0;
761 default:
762 return EINVAL;
763 }
764 }
765
766 /* Todo: don't commit settings to card until we've verified all parameters */
767 static int
768 neo_set_params(void *addr, int setmode, int usemode, audio_params_t *play,
769 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
770 {
771 struct neo_softc *sc;
772 audio_params_t *p;
773 stream_filter_list_t *fil;
774 uint32_t base;
775 uint8_t x;
776 int mode, i;
777
778 sc = addr;
779 for (mode = AUMODE_RECORD; mode != -1;
780 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
781 if ((setmode & mode) == 0)
782 continue;
783
784 p = mode == AUMODE_PLAY ? play : rec;
785
786 if (p == NULL) continue;
787
788 for (x = 0; x < 8; x++) {
789 if (p->sample_rate <
790 (samplerates[x] + samplerates[x + 1]) / 2)
791 break;
792 }
793 if (x == 8)
794 return EINVAL;
795
796 p->sample_rate = samplerates[x];
797 nm_loadcoeff(sc, mode, x);
798
799 x <<= 4;
800 x &= NM_RATE_MASK;
801 if (p->precision == 16)
802 x |= NM_RATE_BITS_16;
803 if (p->channels == 2)
804 x |= NM_RATE_STEREO;
805
806 base = (mode == AUMODE_PLAY)?
807 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
808 nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
809
810 fil = mode == AUMODE_PLAY ? pfil : rfil;
811 i = auconv_set_converter(neo_formats, NEO_NFORMATS,
812 mode, p, FALSE, fil);
813 if (i < 0)
814 return EINVAL;
815 }
816
817 return 0;
818 }
819
820 static int
821 neo_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
822 {
823
824 return NM_BUFFSIZE / 2;
825 }
826
827 static int
828 neo_trigger_output(void *addr, void *start, void *end, int blksize,
829 void (*intr)(void *), void *arg, const audio_params_t *param)
830 {
831 struct neo_softc *sc;
832 int ssz;
833
834 sc = addr;
835 sc->pintr = intr;
836 sc->parg = arg;
837
838 ssz = (param->precision == 16) ? 2 : 1;
839 if (param->channels == 2)
840 ssz <<= 1;
841
842 sc->pbufsize = ((char*)end - (char *)start);
843 sc->pblksize = blksize;
844 sc->pwmark = blksize;
845
846 nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
847 nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
848 nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
849 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
850 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
851 NM_PLAYBACK_ENABLE_FLAG);
852 nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
853
854 return 0;
855 }
856
857 static int
858 neo_trigger_input(void *addr, void *start, void *end, int blksize,
859 void (*intr)(void *), void *arg, const audio_params_t *param)
860 {
861 struct neo_softc *sc;
862 int ssz;
863
864 sc = addr;
865 sc->rintr = intr;
866 sc->rarg = arg;
867
868 ssz = (param->precision == 16) ? 2 : 1;
869 if (param->channels == 2)
870 ssz <<= 1;
871
872 sc->rbufsize = ((char*)end - (char *)start);
873 sc->rblksize = blksize;
874 sc->rwmark = blksize;
875
876 nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
877 nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
878 nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
879 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
880 nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
881 NM_RECORD_ENABLE_FLAG);
882
883 return 0;
884 }
885
886 static int
887 neo_halt_output(void *addr)
888 {
889 struct neo_softc *sc;
890
891 sc = (struct neo_softc *)addr;
892 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
893 nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
894 sc->pintr = 0;
895
896 return 0;
897 }
898
899 static int
900 neo_halt_input(void *addr)
901 {
902 struct neo_softc *sc;
903
904 sc = (struct neo_softc *)addr;
905 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
906 sc->rintr = 0;
907
908 return 0;
909 }
910
911 static int
912 neo_getdev(void *addr, struct audio_device *retp)
913 {
914
915 *retp = neo_device;
916 return 0;
917 }
918
919 static int
920 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
921 {
922 struct neo_softc *sc;
923
924 sc = addr;
925 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
926 }
927
928 static int
929 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
930 {
931 struct neo_softc *sc;
932
933 sc = addr;
934 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
935 }
936
937 static int
938 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
939 {
940 struct neo_softc *sc;
941
942 sc = addr;
943 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
944 }
945
946 static void *
947 neo_malloc(void *addr, int direction, size_t size, struct malloc_type *pool,
948 int flags)
949 {
950 struct neo_softc *sc;
951 void *rv;
952
953 sc = addr;
954 rv = NULL;
955 switch (direction) {
956 case AUMODE_PLAY:
957 if (sc->pbuf_allocated == 0) {
958 rv = (void *) sc->pbuf_vaddr;
959 sc->pbuf_allocated = 1;
960 }
961 break;
962
963 case AUMODE_RECORD:
964 if (sc->rbuf_allocated == 0) {
965 rv = (void *) sc->rbuf_vaddr;
966 sc->rbuf_allocated = 1;
967 }
968 break;
969 }
970
971 return rv;
972 }
973
974 static void
975 neo_free(void *addr, void *ptr, struct malloc_type *pool)
976 {
977 struct neo_softc *sc;
978 vaddr_t v;
979
980 sc = addr;
981 v = (vaddr_t)ptr;
982 if (v == sc->pbuf_vaddr)
983 sc->pbuf_allocated = 0;
984 else if (v == sc->rbuf_vaddr)
985 sc->rbuf_allocated = 0;
986 else
987 printf("neo_free: bad address %p\n", ptr);
988 }
989
990 static size_t
991 neo_round_buffersize(void *addr, int direction, size_t size)
992 {
993
994 return NM_BUFFSIZE;
995 }
996
997 static paddr_t
998 neo_mappage(void *addr, void *mem, off_t off, int prot)
999 {
1000 struct neo_softc *sc;
1001 vaddr_t v;
1002 bus_addr_t pciaddr;
1003
1004 sc = addr;
1005 v = (vaddr_t)mem;
1006 if (v == sc->pbuf_vaddr)
1007 pciaddr = sc->pbuf_pciaddr;
1008 else if (v == sc->rbuf_vaddr)
1009 pciaddr = sc->rbuf_pciaddr;
1010 else
1011 return -1;
1012
1013 return bus_space_mmap(sc->bufiot, pciaddr, off, prot,
1014 BUS_SPACE_MAP_LINEAR);
1015 }
1016
1017 static int
1018 neo_get_props(void *addr)
1019 {
1020
1021 return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
1022 AUDIO_PROP_FULLDUPLEX;
1023 }
1024