source: mainline/uspace/drv/audio/sb16/sb16.c@ 7813516

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7813516 was 7de1988c, checked in by Jakub Jermar <jakub@…>, 12 years ago

Adapt drivers using parsed HW resources to use the new interface.

  • 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(addr_range_t *regs, int dma8, int dma16, irq_cmd_t cmds[],
80 irq_pio_range_t ranges[])
81{
82 assert(regs);
83 assert(dma8 > 0 && dma8 < 4);
84
85 sb16_regs_t *registers = RNGABSPTR(*regs);
86 memcpy(cmds, irq_cmds, sizeof(irq_cmds));
87 cmds[0].addr = (void *) &registers->dsp_read_status;
88 ranges[0].base = (uintptr_t) registers;
89 ranges[0].size = sizeof(*registers);
90 if (dma16 > 4 && dma16 < 8) {
91 /* Valid dma16 */
92 cmds[1].addr = (void *) &registers->dma16_ack;
93 } else {
94 cmds[1].cmd = CMD_ACCEPT;
95 }
96}
97
98int sb16_init_sb16(sb16_t *sb, addr_range_t *regs, ddf_dev_t *dev, int dma8,
99 int dma16)
100{
101 assert(sb);
102
103 /* Setup registers */
104 int ret = pio_enable_range(regs, (void **) &sb->regs);
105 if (ret != EOK)
106 return ret;
107 ddf_log_note("PIO registers at %p accessible.", sb->regs);
108
109 /* Initialize DSP */
110 ddf_fun_t *dsp_fun = ddf_fun_create(dev, fun_exposed, "pcm");
111 if (!dsp_fun) {
112 ddf_log_error("Failed to create dsp function.");
113 return ENOMEM;
114 }
115
116 ret = sb_dsp_init(&sb->dsp, sb->regs, dev, dma8, dma16);
117 if (ret != EOK) {
118 ddf_log_error("Failed to initialize SB DSP: %s.",
119 str_error(ret));
120 ddf_fun_destroy(dsp_fun);
121 return ret;
122 }
123 //TODO remove data implant
124 ddf_fun_data_implant(dsp_fun, &sb->dsp);
125 ddf_fun_set_ops(dsp_fun, &sb_pcm_ops);
126 ddf_log_note("Sound blaster DSP (%x.%x) initialized.",
127 sb->dsp.version.major, sb->dsp.version.minor);
128
129 ret = ddf_fun_bind(dsp_fun);
130 if (ret != EOK) {
131 ddf_log_error(
132 "Failed to bind PCM function: %s.", str_error(ret));
133 // TODO implanted data
134 ddf_fun_destroy(dsp_fun);
135 return ret;
136 }
137
138 ret = ddf_fun_add_to_category(dsp_fun, "audio-pcm");
139 if (ret != EOK) {
140 ddf_log_error("Failed register PCM function in category: %s.",
141 str_error(ret));
142 ddf_fun_unbind(dsp_fun);
143 // TODO implanted data
144 ddf_fun_destroy(dsp_fun);
145 return ret;
146 }
147
148 /* Initialize mixer */
149 const sb_mixer_type_t mixer_type = sb_mixer_type_by_dsp_version(
150 sb->dsp.version.major, sb->dsp.version.minor);
151
152 ddf_fun_t *mixer_fun = ddf_fun_create(dev, fun_exposed, "control");
153 if (!mixer_fun) {
154 ddf_log_error("Failed to create mixer function.");
155 ddf_fun_unbind(dsp_fun);
156 // TODO implanted data
157 ddf_fun_destroy(dsp_fun);
158 return ENOMEM;
159 }
160 ret = sb_mixer_init(&sb->mixer, sb->regs, mixer_type);
161 if (ret != EOK) {
162 ddf_log_error("Failed to initialize SB mixer: %s.",
163 str_error(ret));
164 ddf_fun_unbind(dsp_fun);
165 // TODO implanted data
166 ddf_fun_destroy(dsp_fun);
167 ddf_fun_destroy(mixer_fun);
168 return ret;
169 }
170
171 ddf_log_note("Initialized mixer: %s.",
172 sb_mixer_type_str(sb->mixer.type));
173 ddf_fun_data_implant(mixer_fun, &sb->mixer);
174 ddf_fun_set_ops(mixer_fun, &sb_mixer_ops);
175
176 ret = ddf_fun_bind(mixer_fun);
177 if (ret != EOK) {
178 ddf_log_error(
179 "Failed to bind mixer function: %s.", str_error(ret));
180 // TODO implanted data
181 ddf_fun_destroy(mixer_fun);
182
183 ddf_fun_unbind(dsp_fun);
184 // TODO implanted data
185 ddf_fun_destroy(dsp_fun);
186 return ret;
187 }
188
189 return EOK;
190}
191
192int sb16_init_mpu(sb16_t *sb, addr_range_t *regs)
193{
194 sb->mpu_regs = NULL;
195 return ENOTSUP;
196}
197
198void sb16_interrupt(sb16_t *sb)
199{
200 assert(sb);
201 /* The acknowledgment of interrupts on DSP version 4.xx is different;
202 * It can contain MPU-401 indicator and DMA16 transfers are acked
203 * differently */
204 if (sb->dsp.version.major >= 4) {
205 pio_write_8(&sb->regs->mixer_address, MIXER_IRQ_STATUS_ADDRESS);
206 const uint8_t irq_mask = pio_read_8(&sb->regs->mixer_data);
207 /* Third bit is MPU-401 interrupt */
208 if (irq_mask & 0x4) {
209 return;
210 }
211 } else {
212 ddf_log_debug("SB16 interrupt.");
213 }
214 sb_dsp_interrupt(&sb->dsp);
215}
Note: See TracBrowser for help on using the repository browser.