source: mainline/uspace/drv/audio/sb16/sb16.c@ 5b1adf5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5b1adf5 was 365f259, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

sb16: Use arrays_size macro.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#define _DDF_DATA_IMPLANT
30
31#include <errno.h>
32#include <str_error.h>
33#include <macros.h>
34#include <audio_mixer_iface.h>
35#include <audio_pcm_iface.h>
36
37#include "ddf_log.h"
38#include "dsp_commands.h"
39#include "dsp.h"
40#include "sb16.h"
41
42extern audio_mixer_iface_t sb_mixer_iface;
43extern audio_pcm_iface_t sb_pcm_iface;
44
45static ddf_dev_ops_t sb_mixer_ops = {
46 .interfaces[AUDIO_MIXER_IFACE] = &sb_mixer_iface,
47};
48
49static ddf_dev_ops_t sb_pcm_ops = {
50 .interfaces[AUDIO_PCM_BUFFER_IFACE] = &sb_pcm_iface,
51};
52
53/* ISA interrupts should be edge-triggered so there should be no need for
54 * irq code magic, but we still need to ack those interrupts ASAP. */
55static const irq_cmd_t irq_cmds[] = {
56 { .cmd = CMD_PIO_READ_8, .dstarg = 1 }, /* Address patched at runtime */
57 { .cmd = CMD_PIO_READ_8, .dstarg = 1 }, /* Address patched at runtime */
58 { .cmd = CMD_ACCEPT },
59};
60
61static inline sb_mixer_type_t sb_mixer_type_by_dsp_version(
62 unsigned major, unsigned minor)
63{
64 switch (major)
65 {
66 case 1: return SB_MIXER_NONE; /* SB 1.5 and early 2.0 = no mixer chip */
67 case 2: return (minor == 0) ? SB_MIXER_NONE : SB_MIXER_CT1335;
68 case 3: return SB_MIXER_CT1345; /* SB Pro */
69 case 4: return SB_MIXER_CT1745; /* SB 16 */
70 default: return SB_MIXER_UNKNOWN;
71 }
72}
73
74size_t sb16_irq_code_size(void)
75{
76 return ARRAY_SIZE(irq_cmds);
77}
78
79void sb16_irq_code(void *regs, int dma8, int dma16, irq_cmd_t cmds[], irq_pio_range_t ranges[])
80{
81 assert(regs);
82 assert(dma8 > 0 && dma8 < 4);
83 sb16_regs_t *registers = regs;
84 memcpy(cmds, irq_cmds, sizeof(irq_cmds));
85 cmds[0].addr = (void*)&registers->dsp_read_status;
86 ranges[0].base = (uintptr_t)registers;
87 ranges[0].size = sizeof(*registers);
88 if (dma16 > 4 && dma16 < 8) {
89 /* Valid dma16 */
90 cmds[1].addr = (void*)&registers->dma16_ack;
91 } else {
92 cmds[1].cmd = CMD_ACCEPT;
93 }
94}
95
96int sb16_init_sb16(sb16_t *sb, void *regs, size_t size,
97 ddf_dev_t *dev, int dma8, int dma16)
98{
99 assert(sb);
100 /* Setup registers */
101 int ret = pio_enable(regs, size, (void**)&sb->regs);
102 if (ret != EOK)
103 return ret;
104 ddf_log_debug("PIO registers at %p accessible.", sb->regs);
105
106 /* Initialize DSP */
107 ddf_fun_t *dsp_fun = ddf_fun_create(dev, fun_exposed, "pcm");
108 if (!dsp_fun) {
109 ddf_log_error("Failed to create dsp function.");
110 return ENOMEM;
111 }
112
113 ret = sb_dsp_init(&sb->dsp, sb->regs, dev, dma8, dma16);
114 if (ret != EOK) {
115 ddf_log_error("Failed to initialize SB DSP: %s.",
116 str_error(ret));
117 ddf_fun_destroy(dsp_fun);
118 return ret;
119 }
120 //TODO remove data implant
121 ddf_fun_data_implant(dsp_fun, &sb->dsp);
122 ddf_fun_set_ops(dsp_fun, &sb_pcm_ops);
123 ddf_log_note("Sound blaster DSP (%x.%x) initialized.",
124 sb->dsp.version.major, sb->dsp.version.minor);
125
126 ret = ddf_fun_bind(dsp_fun);
127 if (ret != EOK) {
128 ddf_log_error(
129 "Failed to bind PCM function: %s.", str_error(ret));
130 // TODO implanted data
131 ddf_fun_destroy(dsp_fun);
132 return ret;
133 }
134
135 ret = ddf_fun_add_to_category(dsp_fun, "audio-pcm");
136 if (ret != EOK) {
137 ddf_log_error("Failed register PCM function in category: %s.",
138 str_error(ret));
139 ddf_fun_unbind(dsp_fun);
140 // TODO implanted data
141 ddf_fun_destroy(dsp_fun);
142 return ret;
143 }
144
145 /* Initialize mixer */
146 const sb_mixer_type_t mixer_type = sb_mixer_type_by_dsp_version(
147 sb->dsp.version.major, sb->dsp.version.minor);
148
149 ddf_fun_t *mixer_fun = ddf_fun_create(dev, fun_exposed, "control");
150 if (!mixer_fun) {
151 ddf_log_error("Failed to create mixer function.");
152 ddf_fun_unbind(dsp_fun);
153 // TODO implanted data
154 ddf_fun_destroy(dsp_fun);
155 return ENOMEM;
156 }
157 ret = sb_mixer_init(&sb->mixer, sb->regs, mixer_type);
158 if (ret != EOK) {
159 ddf_log_error("Failed to initialize SB mixer: %s.",
160 str_error(ret));
161 ddf_fun_unbind(dsp_fun);
162 // TODO implanted data
163 ddf_fun_destroy(dsp_fun);
164 ddf_fun_destroy(mixer_fun);
165 return ret;
166 }
167
168 ddf_log_note("Initialized mixer: %s.",
169 sb_mixer_type_str(sb->mixer.type));
170 ddf_fun_data_implant(mixer_fun, &sb->mixer);
171 ddf_fun_set_ops(mixer_fun, &sb_mixer_ops);
172
173 ret = ddf_fun_bind(mixer_fun);
174 if (ret != EOK) {
175 ddf_log_error(
176 "Failed to bind mixer function: %s.", str_error(ret));
177 // TODO implanted data
178 ddf_fun_destroy(mixer_fun);
179
180 ddf_fun_unbind(dsp_fun);
181 // TODO implanted data
182 ddf_fun_destroy(dsp_fun);
183 return ret;
184 }
185
186 return EOK;
187}
188
189int sb16_init_mpu(sb16_t *sb, void *regs, size_t size)
190{
191 sb->mpu_regs = NULL;
192 return ENOTSUP;
193}
194
195void sb16_interrupt(sb16_t *sb)
196{
197 assert(sb);
198 /* The acknowledgment of interrupts on DSP version 4.xx is different;
199 * It can contain MPU-401 indicator and DMA16 transfers are acked
200 * differently */
201 if (sb->dsp.version.major >= 4) {
202 pio_write_8(&sb->regs->mixer_address, MIXER_IRQ_STATUS_ADDRESS);
203 const uint8_t irq_mask = pio_read_8(&sb->regs->mixer_data);
204 /* Third bit is MPU-401 interrupt */
205 if (irq_mask & 0x4) {
206 return;
207 }
208 } else {
209 ddf_log_debug("SB16 interrupt.");
210 }
211 sb_dsp_interrupt(&sb->dsp);
212}
Note: See TracBrowser for help on using the repository browser.