repulse.c revision 1.17 1 1.17 dyoung /* $NetBSD: repulse.c,v 1.17 2011/07/19 15:55:27 dyoung Exp $ */
2 1.1 is
3 1.1 is /*-
4 1.1 is * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 is * All rights reserved.
6 1.1 is *
7 1.1 is * This code is derived from software contributed to The NetBSD Foundation
8 1.1 is * by Ignatios Souvatzis.
9 1.1 is *
10 1.1 is * Redistribution and use in source and binary forms, with or without
11 1.1 is * modification, are permitted provided that the following conditions
12 1.1 is * are met:
13 1.1 is * 1. Redistributions of source code must retain the above copyright
14 1.1 is * notice, this list of conditions and the following disclaimer.
15 1.1 is * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 is * notice, this list of conditions and the following disclaimer in the
17 1.1 is * documentation and/or other materials provided with the distribution.
18 1.1 is *
19 1.1 is * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 is * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 is * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 is * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 is * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 is * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 is * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 is * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 is * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 is * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 is * POSSIBILITY OF SUCH DAMAGE.
30 1.1 is */
31 1.1 is
32 1.4 aymeric #include <sys/cdefs.h>
33 1.17 dyoung __KERNEL_RCSID(0, "$NetBSD: repulse.c,v 1.17 2011/07/19 15:55:27 dyoung Exp $");
34 1.1 is
35 1.1 is #include <sys/types.h>
36 1.1 is #include <sys/param.h>
37 1.1 is #include <sys/systm.h>
38 1.1 is #include <sys/kernel.h>
39 1.3 aymeric #include <sys/device.h>
40 1.1 is #include <sys/fcntl.h> /* FREAD */
41 1.17 dyoung #include <sys/bus.h>
42 1.1 is
43 1.1 is #include <sys/audioio.h>
44 1.1 is #include <dev/audio_if.h>
45 1.1 is #include <dev/mulaw.h>
46 1.3 aymeric
47 1.1 is #include <dev/ic/ac97reg.h>
48 1.1 is #include <dev/ic/ac97var.h>
49 1.1 is
50 1.1 is #include <amiga/dev/zbusvar.h>
51 1.1 is #include <amiga/amiga/isr.h>
52 1.1 is
53 1.1 is #include <amiga/dev/repulse_firmware.h>
54 1.1 is
55 1.1 is #ifndef vu_int8_t
56 1.13 kent #define vu_int8_t volatile uint8_t
57 1.1 is #endif
58 1.1 is #ifndef vu_int16_t
59 1.13 kent #define vu_int16_t volatile uint16_t
60 1.1 is #endif
61 1.1 is #ifndef vu_int32_t
62 1.13 kent #define vu_int32_t volatile uint32_t
63 1.1 is #endif
64 1.1 is
65 1.1 is /* ac97 attachment functions */
66 1.1 is
67 1.1 is int repac_attach(void *, struct ac97_codec_if *);
68 1.13 kent int repac_read(void *, uint8_t, uint16_t *);
69 1.13 kent int repac_write(void *, uint8_t, uint16_t);
70 1.9 kent int repac_reset(void *);
71 1.1 is enum ac97_host_flag repac_flags(void *);
72 1.1 is
73 1.1 is /* audio attachment functions */
74 1.1 is
75 1.1 is void rep_close(void *);
76 1.1 is int rep_getdev(void *, struct audio_device *);
77 1.1 is int rep_get_props(void *);
78 1.1 is int rep_halt_output(void *);
79 1.1 is int rep_halt_input(void *);
80 1.1 is int rep_query_encoding(void *, struct audio_encoding *);
81 1.12 kent int rep_set_params(void *, int, int, audio_params_t *,
82 1.12 kent audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
83 1.12 kent int rep_round_blocksize(void *, int, int, const audio_params_t *);
84 1.1 is int rep_set_port(void *, mixer_ctrl_t *);
85 1.1 is int rep_get_port(void *, mixer_ctrl_t *);
86 1.1 is int rep_query_devinfo(void *, mixer_devinfo_t *);
87 1.1 is size_t rep_round_buffersize(void *, int, size_t);
88 1.1 is
89 1.1 is int rep_start_input(void *, void *, int, void (*)(void *), void *);
90 1.1 is int rep_start_output(void *, void *, int, void (*)(void *), void *);
91 1.1 is
92 1.13 kent int rep_intr(void *);
93 1.1 is
94 1.1 is
95 1.1 is /* audio attachment */
96 1.1 is
97 1.10 yamt const struct audio_hw_if rep_hw_if = {
98 1.12 kent /* open */ 0,
99 1.1 is rep_close,
100 1.1 is /* drain */ 0,
101 1.1 is rep_query_encoding,
102 1.1 is rep_set_params,
103 1.1 is rep_round_blocksize,
104 1.1 is /* commit_setting */ 0,
105 1.1 is /* init_output */ 0,
106 1.1 is /* init_input */ 0,
107 1.1 is rep_start_output,
108 1.1 is rep_start_input,
109 1.1 is rep_halt_output,
110 1.1 is rep_halt_input,
111 1.1 is /* speaker_ctl */ 0,
112 1.1 is rep_getdev,
113 1.1 is /* getfd */ 0,
114 1.1 is rep_set_port,
115 1.1 is rep_get_port,
116 1.1 is rep_query_devinfo,
117 1.1 is /* allocm */ 0,
118 1.1 is /* freem */ 0,
119 1.1 is rep_round_buffersize,
120 1.1 is /* mappage */ 0,
121 1.1 is rep_get_props,
122 1.1 is /* trigger_output */ 0,
123 1.1 is /* trigger_input */ 0,
124 1.2 augustss /* dev_ioctl */ 0,
125 1.1 is };
126 1.1 is
127 1.1 is /* hardware registers */
128 1.1 is
129 1.1 is struct repulse_hw {
130 1.1 is vu_int16_t rhw_status;
131 1.1 is vu_int16_t rhw_fifostatus; /* 0xrrrrpppp0000flag */
132 1.1 is vu_int16_t rhw_reg_address;
133 1.1 is vu_int16_t rhw_reg_data;
134 1.1 is /* 0x08 */
135 1.1 is vu_int16_t rhw_fifo_lh;
136 1.1 is vu_int16_t rhw_fifo_ll;
137 1.1 is vu_int16_t rhw_fifo_rh;
138 1.1 is vu_int16_t rhw_fifo_rl;
139 1.1 is /* 0x10 */
140 1.1 is vu_int16_t rhw_fifo_pack;
141 1.1 is vu_int16_t rhw_play_fifosz;
142 1.1 is vu_int32_t rhw_spdifin_stat;
143 1.1 is #define rhw_spdifout_stat rhw_spdifin_stat;
144 1.1 is
145 1.1 is /* 0x18 */
146 1.1 is vu_int16_t rhw_capt_fifosz;
147 1.1 is vu_int16_t rhw_version;
148 1.1 is vu_int16_t rhw_dummy1;
149 1.1 is vu_int8_t rhw_firmwareload;
150 1.1 is /* 0x1F */
151 1.1 is vu_int8_t rhw_dummy2[66 - 31];
152 1.1 is /* 0x42 */
153 1.1 is vu_int16_t rhw_reset;
154 1.1 is } /* __attribute__((packed)) */;
155 1.1 is
156 1.1 is #define REPSTATUS_PLAY 0x0001
157 1.1 is #define REPSTATUS_RECORD 0x0002
158 1.1 is #define REPSTATUS_PLAYFIFORST 0x0004
159 1.1 is #define REPSTATUS_RECFIFORST 0x0008
160 1.1 is
161 1.1 is #define REPSTATUS_REGSENDBUSY 0x0010
162 1.1 is #define REPSTATUS_LOOPBACK 0x0020
163 1.1 is #define REPSTATUS_ENSPDIFIN 0x0040
164 1.1 is #define REPSTATUS_ENSPDIFOUT 0x0080
165 1.1 is
166 1.1 is #define REPSTATUS_CODECRESET 0x0200
167 1.1 is #define REPSTATUS_SPDIFOUT24 0x0400
168 1.1 is #define REPSTATUS_SPDIFIN24 0x0800
169 1.1 is
170 1.1 is #define REPSTATUS_RECIRQENABLE 0x1000
171 1.1 is #define REPSTATUS_RECIRQACK 0x2000
172 1.1 is #define REPSTATUS_PLAYIRQENABLE 0x4000
173 1.1 is #define REPSTATUS_PLAYIRQACK 0x8000
174 1.1 is
175 1.1 is #define REPFIFO_PLAYFIFOFULL 0x0001
176 1.1 is #define REPFIFO_PLAYFIFOEMPTY 0x0002
177 1.1 is #define REPFIFO_RECFIFOFULL 0x0004
178 1.1 is #define REPFIFO_RECFIFOEMPTY 0x0008
179 1.1 is #define REPFIFO_PLAYFIFOGAUGE(x) ((x << 4) & 0xf000)
180 1.1 is #define REPFIFO_RECFIFOGAUGE(x) (x & 0xf000)
181 1.1 is
182 1.1 is /* ac97 data stream transfer functions */
183 1.13 kent void rep_read_16_stereo(struct repulse_hw *, uint8_t *, int, unsigned);
184 1.13 kent void rep_read_16_mono(struct repulse_hw *, uint8_t *, int, unsigned);
185 1.13 kent void rep_write_16_stereo(struct repulse_hw *, uint8_t *, int, unsigned);
186 1.13 kent void rep_write_16_mono(struct repulse_hw *, uint8_t *, int, unsigned);
187 1.13 kent void rep_read_8_stereo(struct repulse_hw *, uint8_t *, int, unsigned);
188 1.13 kent void rep_read_8_mono(struct repulse_hw *, uint8_t *, int, unsigned);
189 1.13 kent void rep_write_8_stereo(struct repulse_hw *, uint8_t *, int, unsigned);
190 1.13 kent void rep_write_8_mono(struct repulse_hw *, uint8_t *, int, unsigned);
191 1.1 is
192 1.1 is /* AmigaDOS Delay() ticks */
193 1.1 is
194 1.1 is #define USECPERTICK (1000000/50)
195 1.1 is
196 1.1 is /* NetBSD device attachment */
197 1.1 is
198 1.1 is struct repulse_softc {
199 1.1 is struct device sc_dev;
200 1.1 is struct isr sc_isr;
201 1.1 is struct ac97_host_if sc_achost;
202 1.1 is struct ac97_codec_if *sc_codec_if;
203 1.1 is
204 1.1 is struct repulse_hw *sc_boardp;
205 1.1 is
206 1.1 is void (*sc_captmore)(void *);
207 1.1 is void *sc_captarg;
208 1.1 is
209 1.13 kent void (*sc_captfun)(struct repulse_hw *, uint8_t *, int, unsigned);
210 1.1 is void *sc_captbuf;
211 1.1 is int sc_captscale;
212 1.1 is int sc_captbufsz;
213 1.1 is unsigned sc_captflags;
214 1.1 is
215 1.1 is
216 1.1 is void (*sc_playmore)(void *);
217 1.1 is void *sc_playarg;
218 1.13 kent void (*sc_playfun)(struct repulse_hw *, uint8_t *, int, unsigned);
219 1.1 is int sc_playscale;
220 1.1 is unsigned sc_playflags;
221 1.1 is
222 1.1 is };
223 1.1 is
224 1.1 is int repulse_match (struct device *, struct cfdata *, void *);
225 1.1 is void repulse_attach (struct device *, struct device *, void *);
226 1.1 is
227 1.6 thorpej CFATTACH_DECL(repulse, sizeof(struct repulse_softc),
228 1.6 thorpej repulse_match, repulse_attach, NULL, NULL);
229 1.1 is
230 1.1 is int
231 1.13 kent repulse_match(struct device *parent, struct cfdata *cfp, void *aux)
232 1.13 kent {
233 1.1 is struct zbus_args *zap;
234 1.1 is
235 1.1 is zap = aux;
236 1.1 is
237 1.1 is if (zap->manid != 0x4144)
238 1.1 is return (0);
239 1.1 is
240 1.1 is if (zap->prodid != 0)
241 1.1 is return (0);
242 1.1 is
243 1.1 is return (1);
244 1.1 is }
245 1.1 is
246 1.1 is void
247 1.13 kent repulse_attach(struct device *parent, struct device *self, void *aux)
248 1.13 kent {
249 1.1 is struct repulse_softc *sc;
250 1.1 is struct zbus_args *zap;
251 1.1 is struct repulse_hw *bp;
252 1.14 jmc const uint8_t *fwp;
253 1.1 is int needs_firmware;
254 1.13 kent uint16_t a;
255 1.1 is
256 1.1 is sc = (struct repulse_softc *)self;
257 1.1 is zap = aux;
258 1.1 is bp = (struct repulse_hw *)zap->va;
259 1.1 is sc->sc_boardp = bp;
260 1.1 is
261 1.1 is needs_firmware = 0;
262 1.1 is if (bp->rhw_fifostatus & 0x00f0)
263 1.1 is needs_firmware = 1;
264 1.1 is else {
265 1.1 is bp->rhw_status = 0x000c;
266 1.1 is if (bp->rhw_status != 0 || bp->rhw_fifostatus != 0x0f0a)
267 1.1 is needs_firmware = 1;
268 1.1 is }
269 1.1 is
270 1.1 is printf(": ");
271 1.1 is if (needs_firmware) {
272 1.1 is printf("loading ");
273 1.1 is bp->rhw_reset = 0;
274 1.1 is
275 1.1 is delay(1 * USECPERTICK);
276 1.3 aymeric
277 1.14 jmc for (fwp = (const uint8_t *)repulse_firmware;
278 1.1 is fwp < (repulse_firmware_size +
279 1.14 jmc (const uint8_t *)repulse_firmware); fwp++)
280 1.1 is bp->rhw_firmwareload = *fwp;
281 1.1 is
282 1.1 is delay(1 * USECPERTICK);
283 1.1 is
284 1.1 is if (bp->rhw_fifostatus & 0x00f0)
285 1.1 is goto Initerr;
286 1.1 is
287 1.1 is a = /* bp->rhw_status;
288 1.1 is a |= */ REPSTATUS_CODECRESET;
289 1.1 is bp->rhw_status = a;
290 1.1 is
291 1.1 is a = bp->rhw_status;
292 1.1 is if ((a & REPSTATUS_CODECRESET) == 0)
293 1.1 is goto Initerr;
294 1.1 is
295 1.1 is (void)bp->rhw_status;
296 1.1 is (void)bp->rhw_status;
297 1.1 is (void)bp->rhw_status;
298 1.1 is a = bp->rhw_status;
299 1.1 is a &= ~REPSTATUS_CODECRESET;
300 1.1 is bp->rhw_status = a;
301 1.1 is }
302 1.1 is
303 1.1 is printf("firmware version 0x%x\n", bp->rhw_version);
304 1.1 is
305 1.1 is sc->sc_achost.arg = sc;
306 1.1 is
307 1.1 is sc->sc_achost.reset = repac_reset;
308 1.1 is sc->sc_achost.read = repac_read;
309 1.1 is sc->sc_achost.write = repac_write;
310 1.1 is sc->sc_achost.attach = repac_attach;
311 1.1 is sc->sc_achost.flags = 0;
312 1.1 is
313 1.12 kent if (ac97_attach(&sc->sc_achost, self)) {
314 1.1 is printf("%s: error attaching codec\n", self->dv_xname);
315 1.1 is return;
316 1.1 is }
317 1.1 is
318 1.1 is #ifdef DIAGNOSTIC
319 1.3 aymeric /*
320 1.1 is * Print a warning if the codec doesn't support hardware variable
321 1.1 is * rate audio. As the initial incarnations of the Repulse board
322 1.1 is * are AC'97 2.1, it is epxected that we'll always have VRA.
323 1.1 is */
324 1.1 is /*
325 1.1 is * XXX this should be a panic(). OTOH, audio codec speed is not
326 1.1 is * important enough to do this.
327 1.1 is */
328 1.7 kent a = sc->sc_codec_if->vtbl->get_extcaps(sc->sc_codec_if);
329 1.7 kent if (!(a & AC97_EXT_AUDIO_VRA)) {
330 1.1 is printf("%s: warning: codec doesn't support "
331 1.1 is "hardware AC'97 2.0 Variable Rate Audio\n",
332 1.1 is sc->sc_dev.dv_xname);
333 1.1 is }
334 1.1 is #endif
335 1.1 is
336 1.1 is sc->sc_isr.isr_ipl = 2;
337 1.1 is sc->sc_isr.isr_arg = sc;
338 1.1 is sc->sc_isr.isr_intr = rep_intr;
339 1.1 is add_isr(&sc->sc_isr);
340 1.1 is
341 1.1 is audio_attach_mi(&rep_hw_if, sc, &sc->sc_dev);
342 1.1 is
343 1.1 is return;
344 1.1 is
345 1.1 is Initerr:
346 1.1 is printf("\n%s: firmware not successfully loaded\n", self->dv_xname);
347 1.1 is return;
348 1.3 aymeric
349 1.1 is }
350 1.1 is
351 1.13 kent int
352 1.13 kent repac_reset(void *arg)
353 1.13 kent {
354 1.13 kent struct repulse_softc *sc;
355 1.13 kent struct repulse_hw *bp;
356 1.13 kent uint16_t a;
357 1.3 aymeric
358 1.13 kent sc = arg;
359 1.13 kent bp = sc->sc_boardp;
360 1.1 is a = bp->rhw_status;
361 1.1 is a |= REPSTATUS_CODECRESET;
362 1.1 is bp->rhw_status = a;
363 1.1 is
364 1.1 is a = bp->rhw_status;
365 1.1 is #ifdef DIAGNOSTIC
366 1.1 is if ((a & REPSTATUS_CODECRESET) == 0)
367 1.1 is panic("%s: cannot set reset bit", sc->sc_dev.dv_xname);
368 1.1 is #endif
369 1.1 is
370 1.1 is a = bp->rhw_status;
371 1.1 is a = bp->rhw_status;
372 1.1 is a = bp->rhw_status;
373 1.1 is a = bp->rhw_status;
374 1.1 is a &= ~REPSTATUS_CODECRESET;
375 1.1 is bp->rhw_status = a;
376 1.9 kent return 0;
377 1.1 is }
378 1.1 is
379 1.13 kent int
380 1.13 kent repac_read(void *arg, u_int8_t reg, u_int16_t *valuep)
381 1.13 kent {
382 1.13 kent struct repulse_softc *sc;
383 1.13 kent struct repulse_hw *bp;
384 1.1 is
385 1.13 kent sc = arg;
386 1.13 kent bp = sc->sc_boardp;
387 1.13 kent while (bp->rhw_status & REPSTATUS_REGSENDBUSY)
388 1.13 kent continue;
389 1.1 is bp->rhw_reg_address = (reg & 0x7F) | 0x80;
390 1.1 is
391 1.13 kent while (bp->rhw_status & REPSTATUS_REGSENDBUSY)
392 1.13 kent continue;
393 1.1 is
394 1.1 is *valuep = bp->rhw_reg_data;
395 1.1 is
396 1.1 is return 0;
397 1.1 is }
398 1.1 is
399 1.13 kent int
400 1.13 kent repac_write(void *arg, uint8_t reg, uint16_t value)
401 1.13 kent {
402 1.13 kent struct repulse_softc *sc;
403 1.13 kent struct repulse_hw *bp;
404 1.1 is
405 1.13 kent sc = arg;
406 1.13 kent bp = sc->sc_boardp;
407 1.1 is bp->rhw_reg_data = value;
408 1.1 is bp->rhw_reg_address = reg & 0x7F;
409 1.1 is
410 1.13 kent while (bp->rhw_status & REPSTATUS_REGSENDBUSY)
411 1.13 kent continue;
412 1.1 is
413 1.1 is return 0;
414 1.1 is }
415 1.1 is
416 1.13 kent int
417 1.13 kent repac_attach(void *arg, struct ac97_codec_if *acip)
418 1.13 kent {
419 1.1 is struct repulse_softc *sc;
420 1.1 is
421 1.1 is sc = arg;
422 1.1 is sc->sc_codec_if = acip;
423 1.1 is
424 1.1 is return 0;
425 1.1 is }
426 1.1 is
427 1.1 is /* audio(9) support stuff which is not ac97-constant */
428 1.1 is
429 1.1 is void
430 1.1 is rep_close(void *arg)
431 1.1 is {
432 1.13 kent struct repulse_softc *sc;
433 1.1 is
434 1.13 kent sc = arg;
435 1.1 is rep_halt_output(sc);
436 1.1 is rep_halt_input(sc);
437 1.1 is }
438 1.1 is
439 1.1 is int
440 1.1 is rep_getdev(void *arg, struct audio_device *retp)
441 1.1 is {
442 1.13 kent struct repulse_softc *sc;
443 1.13 kent struct repulse_hw *bp;
444 1.1 is
445 1.13 kent if (retp != NULL) {
446 1.13 kent sc = arg;
447 1.13 kent bp = sc->sc_boardp;
448 1.1 is strncpy(retp->name, "Repulse", sizeof(retp->name));
449 1.1 is snprintf(retp->version, sizeof(retp->version), "0x%x",
450 1.1 is bp->rhw_version);
451 1.1 is strncpy(retp->config, "", sizeof(retp->config));
452 1.1 is }
453 1.1 is
454 1.1 is return 0;
455 1.1 is }
456 1.1 is
457 1.3 aymeric int
458 1.1 is rep_get_props(void *v)
459 1.1 is {
460 1.13 kent return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
461 1.3 aymeric }
462 1.1 is
463 1.1 is int
464 1.1 is rep_halt_output(void *arg)
465 1.1 is {
466 1.13 kent struct repulse_softc *sc;
467 1.13 kent struct repulse_hw *bp;
468 1.1 is
469 1.13 kent sc = arg;
470 1.13 kent bp = sc->sc_boardp;
471 1.1 is bp->rhw_status &= ~(REPSTATUS_PLAYIRQENABLE|REPSTATUS_PLAY);
472 1.1 is
473 1.1 is
474 1.1 is return 0;
475 1.1 is }
476 1.1 is
477 1.1 is int
478 1.1 is rep_halt_input(void *arg)
479 1.1 is {
480 1.13 kent struct repulse_softc *sc;
481 1.13 kent struct repulse_hw *bp;
482 1.1 is
483 1.13 kent sc = arg;
484 1.13 kent bp = sc->sc_boardp;
485 1.1 is bp->rhw_status &= ~(REPSTATUS_RECIRQENABLE|REPSTATUS_RECORD);
486 1.1 is
487 1.1 is return 0;
488 1.1 is }
489 1.1 is
490 1.1 is /*
491 1.1 is * Encoding support.
492 1.1 is *
493 1.1 is * TODO: add 24bit and 32bit modes here and in setparams.
494 1.1 is */
495 1.1 is
496 1.1 is const struct repulse_encoding_query {
497 1.1 is const char *name;
498 1.1 is int encoding, precision, flags;
499 1.1 is } rep_encoding_queries[] = {
500 1.1 is { AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0},
501 1.1 is { AudioEmulaw, AUDIO_ENCODING_ULAW, 8, AUDIO_ENCODINGFLAG_EMULATED},
502 1.1 is { AudioEalaw, AUDIO_ENCODING_ALAW, 8, AUDIO_ENCODINGFLAG_EMULATED},
503 1.1 is { AudioEslinear, AUDIO_ENCODING_SLINEAR, 8, 0},
504 1.1 is { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0},
505 1.1 is { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16, 0},
506 1.1 is { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16, 0},
507 1.1 is { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16, 0},
508 1.1 is };
509 1.1 is
510 1.1 is int
511 1.1 is rep_query_encoding(void *arg, struct audio_encoding *fp)
512 1.1 is {
513 1.1 is int i;
514 1.1 is const struct repulse_encoding_query *p;
515 1.1 is
516 1.1 is i = fp->index;
517 1.1 is
518 1.1 is if (i >= sizeof(rep_encoding_queries) /
519 1.1 is sizeof(struct repulse_encoding_query))
520 1.13 kent return EINVAL;
521 1.1 is
522 1.1 is p = &rep_encoding_queries[i];
523 1.1 is
524 1.1 is strncpy (fp->name, p->name, sizeof(fp->name));
525 1.1 is fp->encoding = p->encoding;
526 1.1 is fp->precision = p->precision;
527 1.1 is fp->flags = p->flags;
528 1.1 is
529 1.13 kent return 0;
530 1.1 is }
531 1.1 is
532 1.1 is /*
533 1.1 is * XXX the following three functions need to be enhanced for the FPGA s/pdif
534 1.1 is * mode. Generic ac97 versions for now.
535 1.1 is */
536 1.1 is
537 1.3 aymeric int
538 1.1 is rep_get_port(void *arg, mixer_ctrl_t *cp)
539 1.1 is {
540 1.13 kent struct repulse_softc *sc;
541 1.1 is
542 1.13 kent sc = arg;
543 1.13 kent return sc->sc_codec_if->vtbl->mixer_get_port(sc->sc_codec_if, cp);
544 1.1 is }
545 1.1 is
546 1.3 aymeric int
547 1.1 is rep_set_port(void *arg, mixer_ctrl_t *cp)
548 1.1 is {
549 1.13 kent struct repulse_softc *sc;
550 1.1 is
551 1.13 kent sc = arg;
552 1.13 kent return sc->sc_codec_if->vtbl->mixer_set_port(sc->sc_codec_if, cp);
553 1.1 is }
554 1.1 is
555 1.1 is int
556 1.13 kent rep_query_devinfo(void *arg, mixer_devinfo_t *dip)
557 1.1 is {
558 1.13 kent struct repulse_softc *sc;
559 1.1 is
560 1.13 kent sc = arg;
561 1.13 kent return sc->sc_codec_if->vtbl->query_devinfo(sc->sc_codec_if, dip);
562 1.1 is }
563 1.1 is
564 1.1 is int
565 1.12 kent rep_round_blocksize(void *arg, int blk, int mode, const audio_params_t *param)
566 1.1 is {
567 1.1 is int b1;
568 1.1 is
569 1.1 is b1 = (blk & -32);
570 1.1 is
571 1.1 is if (b1 > 65536 / 2 / 2 /* channels */ / 4 /* bytes per channel */)
572 1.1 is b1 = 65536 / 2 / 2 / 4;
573 1.13 kent return b1;
574 1.1 is }
575 1.1 is
576 1.3 aymeric size_t
577 1.1 is rep_round_buffersize(void *arg, int direction, size_t size)
578 1.1 is {
579 1.1 is return size;
580 1.1 is }
581 1.1 is
582 1.1 is
583 1.1 is int
584 1.1 is rep_set_params(void *addr, int setmode, int usemode,
585 1.12 kent audio_params_t *play, audio_params_t *rec,
586 1.12 kent stream_filter_list_t *pfil, stream_filter_list_t *rfil)
587 1.1 is {
588 1.12 kent audio_params_t hw;
589 1.13 kent struct repulse_softc *sc;
590 1.12 kent audio_params_t *p;
591 1.1 is int mode, reg;
592 1.1 is unsigned flags;
593 1.1 is u_int16_t a;
594 1.1 is
595 1.13 kent sc = addr;
596 1.1 is /* for mode in (RECORD, PLAY) */
597 1.1 is for (mode = AUMODE_RECORD; mode != -1;
598 1.1 is mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
599 1.1 is
600 1.1 is if ((setmode & mode) == 0)
601 1.1 is continue;
602 1.1 is
603 1.1 is p = mode == AUMODE_PLAY ? play : rec;
604 1.1 is
605 1.1 is /* TODO XXX we can do upto 32bit, 96000 */
606 1.1 is if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
607 1.1 is (p->precision != 8 && p->precision != 16) ||
608 1.1 is (p->channels != 1 && p->channels != 2))
609 1.13 kent return EINVAL;
610 1.1 is
611 1.1 is reg = mode == AUMODE_PLAY ?
612 1.1 is AC97_REG_PCM_FRONT_DAC_RATE : AC97_REG_PCM_LR_ADC_RATE;
613 1.1 is
614 1.13 kent repac_write(sc, reg, (uint16_t) p->sample_rate);
615 1.1 is repac_read(sc, reg, &a);
616 1.1 is p->sample_rate = a;
617 1.1 is
618 1.1 is if (mode == AUMODE_PLAY)
619 1.1 is sc->sc_playscale = p->channels * p->precision / 8;
620 1.1 is else
621 1.1 is sc->sc_captscale = p->channels * p->precision / 8;
622 1.1 is
623 1.12 kent hw = *p;
624 1.1 is
625 1.1 is /* everything else is software, alas... */
626 1.1 is /* XXX TBD signed/unsigned, *law, etc */
627 1.1 is
628 1.1 is flags = 0;
629 1.1 is if (p->encoding == AUDIO_ENCODING_ULINEAR_LE ||
630 1.1 is p->encoding == AUDIO_ENCODING_ULINEAR_BE ||
631 1.1 is p->encoding == AUDIO_ENCODING_ULINEAR)
632 1.1 is flags |= 1;
633 1.1 is
634 1.1 is if (p->encoding == AUDIO_ENCODING_SLINEAR_LE ||
635 1.1 is p->encoding == AUDIO_ENCODING_ULINEAR_LE)
636 1.1 is flags |= 2;
637 1.1 is
638 1.1 is if (mode == AUMODE_PLAY) {
639 1.1 is sc->sc_playflags = flags;
640 1.1 is if (p->encoding == AUDIO_ENCODING_ULAW) {
641 1.1 is sc->sc_playfun = p->channels == 1 ?
642 1.1 is rep_write_16_mono :
643 1.1 is rep_write_16_stereo;
644 1.1 is sc->sc_playflags = 0;
645 1.1 is sc->sc_playscale = p->channels * 2;
646 1.12 kent hw.encoding = AUDIO_ENCODING_SLINEAR_BE;
647 1.12 kent hw.precision = hw.validbits = 16;
648 1.12 kent pfil->append(pfil, mulaw_to_linear16, &hw);
649 1.1 is } else
650 1.1 is if (p->encoding == AUDIO_ENCODING_ALAW) {
651 1.1 is sc->sc_playfun = p->channels == 1 ?
652 1.1 is rep_write_16_mono :
653 1.1 is rep_write_16_stereo;
654 1.1 is sc->sc_playflags = 0;
655 1.1 is sc->sc_playscale = p->channels * 2;
656 1.12 kent hw.encoding = AUDIO_ENCODING_SLINEAR_BE;
657 1.12 kent hw.precision = hw.validbits = 16;
658 1.12 kent pfil->append(pfil, alaw_to_linear16, &hw);
659 1.1 is } else
660 1.1 is if (p->precision == 8 && p->channels == 1)
661 1.1 is sc->sc_playfun = rep_write_8_mono;
662 1.1 is else if (p->precision == 8 && p->channels == 2)
663 1.1 is sc->sc_playfun = rep_write_8_stereo;
664 1.1 is else if (p->precision == 16 && p->channels == 1)
665 1.1 is sc->sc_playfun = rep_write_16_mono;
666 1.1 is else if (p->precision == 16 && p->channels == 2)
667 1.1 is sc->sc_playfun = rep_write_16_stereo;
668 1.1 is } else {
669 1.1 is sc->sc_captflags = flags;
670 1.1 is if (p->encoding == AUDIO_ENCODING_ULAW) {
671 1.1 is sc->sc_captfun = p->channels == 1 ?
672 1.1 is rep_read_8_mono :
673 1.1 is rep_read_8_stereo;
674 1.1 is sc->sc_captflags = 0;
675 1.12 kent hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
676 1.12 kent rfil->append(rfil, linear8_to_mulaw, &hw);
677 1.1 is } else
678 1.1 is if (p->encoding == AUDIO_ENCODING_ALAW) {
679 1.1 is sc->sc_captfun = p->channels == 1 ?
680 1.1 is rep_read_8_mono :
681 1.1 is rep_read_8_stereo;
682 1.1 is sc->sc_captflags = 0;
683 1.12 kent rfil->append(rfil, linear8_to_alaw, &hw);
684 1.1 is } else
685 1.1 is if (p->precision == 8 && p->channels == 1)
686 1.1 is sc->sc_captfun = rep_read_8_mono;
687 1.1 is else if (p->precision == 8 && p->channels == 2)
688 1.1 is sc->sc_captfun = rep_read_8_stereo;
689 1.1 is else if (p->precision == 16 && p->channels == 1)
690 1.1 is sc->sc_captfun = rep_read_16_mono;
691 1.1 is else if (p->precision == 16 && p->channels == 2)
692 1.1 is sc->sc_captfun = rep_read_16_stereo;
693 1.1 is }
694 1.8 wiz /* TBD: mu-law, A-law */
695 1.1 is }
696 1.3 aymeric return 0;
697 1.1 is }
698 1.1 is
699 1.1 is void
700 1.13 kent rep_write_8_mono(struct repulse_hw *bp, uint8_t *p, int length, unsigned flags)
701 1.1 is {
702 1.13 kent uint16_t sample;
703 1.13 kent uint16_t xor;
704 1.1 is
705 1.1 is xor = flags & 1 ? 0x8000 : 0;
706 1.1 is
707 1.1 is bp->rhw_fifo_pack = 0;
708 1.1 is
709 1.1 is while (length-- > 0) {
710 1.1 is sample = ((*p++) << 8) ^ xor;
711 1.1 is bp->rhw_fifo_lh = sample;
712 1.1 is bp->rhw_fifo_rh = sample;
713 1.1 is }
714 1.1 is }
715 1.1 is
716 1.1 is void
717 1.13 kent rep_write_8_stereo(struct repulse_hw *bp, uint8_t *p, int length,
718 1.1 is unsigned flags)
719 1.1 is {
720 1.13 kent uint16_t xor;
721 1.1 is
722 1.1 is xor = flags & 1 ? 0x8000 : 0;
723 1.1 is
724 1.1 is bp->rhw_fifo_pack = 0;
725 1.1 is
726 1.1 is while (length-- > 0) {
727 1.1 is bp->rhw_fifo_lh = ((*p++) << 8) ^ xor;
728 1.1 is bp->rhw_fifo_rh = ((*p++) << 8) ^ xor;
729 1.1 is }
730 1.1 is }
731 1.1 is
732 1.1 is void
733 1.13 kent rep_write_16_mono(struct repulse_hw *bp, uint8_t *p, int length,
734 1.1 is unsigned flags)
735 1.1 is {
736 1.13 kent uint16_t *q;
737 1.13 kent uint16_t sample;
738 1.13 kent uint16_t xor;
739 1.1 is
740 1.13 kent q = (uint16_t *)p;
741 1.1 is xor = flags & 1 ? 0x8000 : 0;
742 1.1 is
743 1.1 is bp->rhw_fifo_pack = 0;
744 1.1 is
745 1.1 is if (flags & 2) {
746 1.1 is while (length > 0) {
747 1.1 is sample = bswap16(*q++) ^ xor;
748 1.1 is bp->rhw_fifo_lh = sample;
749 1.1 is bp->rhw_fifo_rh = sample;
750 1.1 is length -= 2;
751 1.1 is }
752 1.1 is return;
753 1.1 is }
754 1.1 is
755 1.1 is while (length > 0) {
756 1.1 is sample = (*q++) ^ xor;
757 1.1 is bp->rhw_fifo_lh = sample;
758 1.1 is bp->rhw_fifo_rh = sample;
759 1.1 is length -= 2;
760 1.1 is }
761 1.1 is }
762 1.1 is
763 1.1 is void
764 1.13 kent rep_write_16_stereo(struct repulse_hw *bp, uint8_t *p, int length,
765 1.1 is unsigned flags)
766 1.1 is {
767 1.13 kent uint16_t *q;
768 1.13 kent uint16_t xor;
769 1.1 is
770 1.13 kent q = (uint16_t *)p;
771 1.1 is xor = flags & 1 ? 0x8000 : 0;
772 1.1 is
773 1.1 is bp->rhw_fifo_pack = 0;
774 1.1 is
775 1.1 is if (flags & 2) {
776 1.1 is while (length > 0) {
777 1.1 is bp->rhw_fifo_lh = bswap16(*q++) ^ xor;
778 1.1 is bp->rhw_fifo_rh = bswap16(*q++) ^ xor;
779 1.1 is length -= 4;
780 1.1 is }
781 1.1 is return;
782 1.1 is }
783 1.1 is while (length > 0) {
784 1.1 is bp->rhw_fifo_lh = (*q++) ^ xor;
785 1.1 is bp->rhw_fifo_rh = (*q++) ^ xor;
786 1.1 is length -= 4;
787 1.1 is }
788 1.1 is }
789 1.1 is
790 1.1 is void
791 1.13 kent rep_read_8_mono(struct repulse_hw *bp, uint8_t *p, int length, unsigned flags)
792 1.1 is {
793 1.13 kent uint16_t v;
794 1.13 kent uint16_t xor;
795 1.1 is
796 1.1 is xor = flags & 1 ? 0x8000 : 0;
797 1.1 is
798 1.1 is while (length > 0) {
799 1.1 is *p++ = (bp->rhw_fifo_lh ^ xor) >> 8;
800 1.1 is v = bp->rhw_fifo_rh;
801 1.1 is length--;
802 1.1 is }
803 1.1 is }
804 1.1 is
805 1.1 is void
806 1.13 kent rep_read_16_mono(struct repulse_hw *bp, uint8_t *p, int length, unsigned flags)
807 1.1 is {
808 1.13 kent uint16_t *q;
809 1.13 kent uint16_t v;
810 1.13 kent uint16_t xor;
811 1.1 is
812 1.13 kent q = (uint16_t *)p;
813 1.1 is xor = flags & 1 ? 0x8000 : 0;
814 1.1 is
815 1.1 is if (flags & 2) {
816 1.1 is while (length > 0) {
817 1.1 is *q++ = bswap16(bp->rhw_fifo_lh ^ xor);
818 1.1 is v = bp->rhw_fifo_rh;
819 1.1 is length -= 2;
820 1.1 is }
821 1.1 is return;
822 1.1 is }
823 1.1 is
824 1.1 is while (length > 0) {
825 1.1 is *q++ = bp->rhw_fifo_lh ^ xor;
826 1.1 is v = bp->rhw_fifo_rh;
827 1.1 is length -= 2;
828 1.1 is }
829 1.1 is }
830 1.1 is
831 1.1 is void
832 1.13 kent rep_read_8_stereo(struct repulse_hw *bp, uint8_t *p, int length,
833 1.1 is unsigned flags)
834 1.1 is {
835 1.13 kent uint16_t xor;
836 1.1 is
837 1.1 is xor = flags & 1 ? 0x8000 : 0;
838 1.1 is while (length > 0) {
839 1.1 is *p++ = (bp->rhw_fifo_lh ^ xor) >> 8;
840 1.1 is *p++ = (bp->rhw_fifo_rh ^ xor) >> 8;
841 1.1 is length -= 2;
842 1.1 is }
843 1.1 is }
844 1.1 is
845 1.1 is void
846 1.13 kent rep_read_16_stereo(struct repulse_hw *bp, uint8_t *p, int length,
847 1.1 is unsigned flags)
848 1.1 is {
849 1.13 kent uint16_t *q;
850 1.13 kent uint16_t xor;
851 1.1 is
852 1.13 kent q = (uint16_t *)p;
853 1.1 is xor = flags & 1 ? 0x8000 : 0;
854 1.1 is
855 1.1 is if (flags & 2) {
856 1.1 is while (length > 0) {
857 1.1 is *q++ = bswap16(bp->rhw_fifo_lh ^ xor);
858 1.1 is *q++ = bswap16(bp->rhw_fifo_rh ^ xor);
859 1.1 is length -= 4;
860 1.1 is }
861 1.1 is return;
862 1.1 is }
863 1.1 is while (length > 0) {
864 1.1 is *q++ = bp->rhw_fifo_lh ^ xor;
865 1.1 is *q++ = bp->rhw_fifo_rh ^ xor;
866 1.1 is length -= 4;
867 1.1 is }
868 1.1 is }
869 1.1 is
870 1.1 is /*
871 1.1 is * At this point the transfer function is set.
872 1.1 is */
873 1.1 is
874 1.1 is int
875 1.1 is rep_start_output(void *addr, void *block, int blksize,
876 1.13 kent void (*intr)(void*), void *intrarg)
877 1.13 kent {
878 1.1 is struct repulse_softc *sc;
879 1.13 kent uint8_t *buf;
880 1.1 is struct repulse_hw *bp;
881 1.13 kent uint16_t status;
882 1.1 is
883 1.1 is
884 1.1 is sc = addr;
885 1.1 is bp = sc->sc_boardp;
886 1.1 is buf = block;
887 1.1 is
888 1.1 is /* TODO: prepare hw if necessary */
889 1.1 is status = bp->rhw_status;
890 1.1 is if (!(status & REPSTATUS_PLAY))
891 1.1 is bp->rhw_status = status |
892 1.1 is REPSTATUS_PLAY | REPSTATUS_PLAYFIFORST;
893 1.1 is
894 1.1 is /* copy data */
895 1.1 is (*sc->sc_playfun)(bp, buf, blksize, sc->sc_playflags);
896 1.1 is
897 1.1 is /* TODO: set hw if necessary */
898 1.1 is if (intr) {
899 1.1 is bp->rhw_status |= REPSTATUS_PLAYIRQENABLE;
900 1.1 is bp->rhw_play_fifosz = blksize / sc->sc_playscale / 2;
901 1.1 is /* /2: give us time to return on the first call */
902 1.1 is }
903 1.1 is
904 1.1 is /* save callback function */
905 1.1 is sc->sc_playarg = intrarg;
906 1.1 is sc->sc_playmore = intr;
907 1.1 is
908 1.1 is return 0;
909 1.1 is }
910 1.1 is
911 1.1 is int
912 1.1 is rep_start_input(void *addr, void *block, int blksize,
913 1.13 kent void (*intr)(void*), void *intrarg)
914 1.13 kent {
915 1.1 is struct repulse_softc *sc;
916 1.1 is struct repulse_hw *bp;
917 1.13 kent uint16_t status;
918 1.1 is
919 1.1 is sc = addr;
920 1.1 is bp = sc->sc_boardp;
921 1.1 is
922 1.1 is sc->sc_captbuf = block;
923 1.1 is sc->sc_captbufsz = blksize;
924 1.1 is sc->sc_captarg = intrarg;
925 1.1 is sc->sc_captmore = intr;
926 1.1 is
927 1.1 is status = bp->rhw_status;
928 1.1 is if (!(status & REPSTATUS_RECORD))
929 1.1 is bp->rhw_status = status | REPSTATUS_RECORD
930 1.1 is | REPSTATUS_RECFIFORST;
931 1.1 is
932 1.1 is bp->rhw_status |= REPSTATUS_RECIRQENABLE;
933 1.1 is bp->rhw_capt_fifosz = blksize / sc->sc_captscale;
934 1.1 is
935 1.1 is return 0;
936 1.1 is }
937 1.1 is
938 1.1 is /* irq handler */
939 1.1 is
940 1.1 is int
941 1.13 kent rep_intr(void *tag)
942 1.13 kent {
943 1.1 is struct repulse_softc *sc;
944 1.1 is struct repulse_hw *bp;
945 1.1 is int foundone;
946 1.13 kent uint16_t status;
947 1.1 is
948 1.1 is foundone = 0;
949 1.1 is
950 1.1 is sc = tag;
951 1.1 is bp = sc->sc_boardp;
952 1.1 is status = bp->rhw_status;
953 1.1 is
954 1.1 is if (status & REPSTATUS_PLAYIRQACK) {
955 1.1 is foundone = 1;
956 1.1 is status &= ~REPSTATUS_PLAYIRQENABLE;
957 1.1 is bp->rhw_status = status;
958 1.1 is (*sc->sc_playmore)(sc->sc_playarg);
959 1.1 is }
960 1.1 is
961 1.1 is if (status & REPSTATUS_RECIRQACK) {
962 1.1 is foundone = 1;
963 1.1 is status &= ~REPSTATUS_RECIRQENABLE;
964 1.1 is bp->rhw_status = status;
965 1.1 is (*sc->sc_captfun)(bp, sc->sc_captbuf, sc->sc_captbufsz,
966 1.1 is sc->sc_captflags);
967 1.1 is (*sc->sc_captmore)(sc->sc_captarg);
968 1.1 is }
969 1.1 is
970 1.1 is return foundone;
971 1.1 is }
972