1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * Copyright (c) 2011 Vojtech Horky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 | /** @addtogroup drvaudiosb16
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Main routines of Creative Labs SoundBlaster 16 driver
|
---|
34 | */
|
---|
35 | #include <ddf/driver.h>
|
---|
36 | #include <ddf/interrupt.h>
|
---|
37 | #include <ddf/log.h>
|
---|
38 | #include <device/hw_res_parsed.h>
|
---|
39 | #include <assert.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <str_error.h>
|
---|
43 |
|
---|
44 | #include "ddf_log.h"
|
---|
45 | #include "sb16.h"
|
---|
46 |
|
---|
47 | #define NAME "sb16"
|
---|
48 |
|
---|
49 | static int sb_add_device(ddf_dev_t *device);
|
---|
50 | static int sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
|
---|
51 | addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16);
|
---|
52 | static int sb_enable_interrupts(ddf_dev_t *device);
|
---|
53 |
|
---|
54 | static driver_ops_t sb_driver_ops = {
|
---|
55 | .dev_add = sb_add_device,
|
---|
56 | };
|
---|
57 |
|
---|
58 | static driver_t sb_driver = {
|
---|
59 | .name = NAME,
|
---|
60 | .driver_ops = &sb_driver_ops
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Initialize global driver structures (NONE).
|
---|
64 | *
|
---|
65 | * @param[in] argc Nmber of arguments in argv vector (ignored).
|
---|
66 | * @param[in] argv Cmdline argument vector (ignored).
|
---|
67 | * @return Error code.
|
---|
68 | *
|
---|
69 | * Driver debug level is set here.
|
---|
70 | */
|
---|
71 | int main(int argc, char *argv[])
|
---|
72 | {
|
---|
73 | printf(NAME": HelenOS SB16 audio driver.\n");
|
---|
74 | ddf_log_init(NAME);
|
---|
75 | return ddf_driver_main(&sb_driver);
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
---|
79 | {
|
---|
80 | sb16_t *sb16_dev = ddf_dev_data_get(dev);
|
---|
81 | sb16_interrupt(sb16_dev);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Initialize new SB16 driver instance.
|
---|
85 | *
|
---|
86 | * @param[in] device DDF instance of the device to initialize.
|
---|
87 | * @return Error code.
|
---|
88 | */
|
---|
89 | static int sb_add_device(ddf_dev_t *device)
|
---|
90 | {
|
---|
91 | bool handler_regd = false;
|
---|
92 | const size_t irq_cmd_count = sb16_irq_code_size();
|
---|
93 | irq_cmd_t irq_cmds[irq_cmd_count];
|
---|
94 | irq_pio_range_t irq_ranges[1];
|
---|
95 | int irq_cap;
|
---|
96 |
|
---|
97 | sb16_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_t));
|
---|
98 | int rc = soft_state ? EOK : ENOMEM;
|
---|
99 | if (rc != EOK) {
|
---|
100 | ddf_log_error("Failed to allocate sb16 structure.");
|
---|
101 | goto error;
|
---|
102 | }
|
---|
103 |
|
---|
104 | addr_range_t sb_regs;
|
---|
105 | addr_range_t *p_sb_regs = &sb_regs;
|
---|
106 | addr_range_t mpu_regs;
|
---|
107 | addr_range_t *p_mpu_regs = &mpu_regs;
|
---|
108 | int irq = 0, dma8 = 0, dma16 = 0;
|
---|
109 |
|
---|
110 | rc = sb_get_res(device, &p_sb_regs, &p_mpu_regs, &irq, &dma8, &dma16);
|
---|
111 | if (rc != EOK) {
|
---|
112 | ddf_log_error("Failed to get resources: %s.", str_error(rc));
|
---|
113 | goto error;
|
---|
114 | }
|
---|
115 |
|
---|
116 | sb16_irq_code(p_sb_regs, dma8, dma16, irq_cmds, irq_ranges);
|
---|
117 |
|
---|
118 | irq_code_t irq_code = {
|
---|
119 | .cmdcount = irq_cmd_count,
|
---|
120 | .cmds = irq_cmds,
|
---|
121 | .rangecount = 1,
|
---|
122 | .ranges = irq_ranges
|
---|
123 | };
|
---|
124 |
|
---|
125 | irq_cap = register_interrupt_handler(device, irq, irq_handler,
|
---|
126 | &irq_code);
|
---|
127 | if (irq_cap < 0) {
|
---|
128 | rc = irq_cap;
|
---|
129 | ddf_log_error("Failed to register irq handler: %s.",
|
---|
130 | str_error(rc));
|
---|
131 | goto error;
|
---|
132 | }
|
---|
133 |
|
---|
134 | handler_regd = true;
|
---|
135 |
|
---|
136 | rc = sb_enable_interrupts(device);
|
---|
137 | if (rc != EOK) {
|
---|
138 | ddf_log_error("Failed to enable interrupts: %s.",
|
---|
139 | str_error(rc));
|
---|
140 | goto error;
|
---|
141 | }
|
---|
142 |
|
---|
143 | rc = sb16_init_sb16(soft_state, p_sb_regs, device, dma8, dma16);
|
---|
144 | if (rc != EOK) {
|
---|
145 | ddf_log_error("Failed to init sb16 driver: %s.",
|
---|
146 | str_error(rc));
|
---|
147 | goto error;
|
---|
148 | }
|
---|
149 |
|
---|
150 | rc = sb16_init_mpu(soft_state, p_mpu_regs);
|
---|
151 | if (rc == EOK) {
|
---|
152 | ddf_fun_t *mpu_fun =
|
---|
153 | ddf_fun_create(device, fun_exposed, "midi");
|
---|
154 | if (mpu_fun) {
|
---|
155 | rc = ddf_fun_bind(mpu_fun);
|
---|
156 | if (rc != EOK)
|
---|
157 | ddf_log_error(
|
---|
158 | "Failed to bind midi function: %s.",
|
---|
159 | str_error(rc));
|
---|
160 | } else {
|
---|
161 | ddf_log_error("Failed to create midi function.");
|
---|
162 | }
|
---|
163 | } else {
|
---|
164 | ddf_log_warning("Failed to init mpu driver: %s.",
|
---|
165 | str_error(rc));
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* MPU state does not matter */
|
---|
169 | return EOK;
|
---|
170 | error:
|
---|
171 | if (handler_regd)
|
---|
172 | unregister_interrupt_handler(device, irq_cap);
|
---|
173 | return rc;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static int sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
|
---|
177 | addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16)
|
---|
178 | {
|
---|
179 | assert(device);
|
---|
180 |
|
---|
181 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
---|
182 | if (parent_sess == NULL)
|
---|
183 | return ENOMEM;
|
---|
184 |
|
---|
185 | hw_res_list_parsed_t hw_res;
|
---|
186 | hw_res_list_parsed_init(&hw_res);
|
---|
187 | const int ret = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
|
---|
188 | async_hangup(parent_sess);
|
---|
189 | if (ret != EOK) {
|
---|
190 | return ret;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /* 1x IRQ, 1-2x DMA(8,16), 1-2x IO (MPU is separate). */
|
---|
194 | if (hw_res.irqs.count != 1 ||
|
---|
195 | (hw_res.io_ranges.count != 1 && hw_res.io_ranges.count != 2) ||
|
---|
196 | (hw_res.dma_channels.count != 1 && hw_res.dma_channels.count != 2)) {
|
---|
197 | hw_res_list_parsed_clean(&hw_res);
|
---|
198 | return EINVAL;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (irq)
|
---|
202 | *irq = hw_res.irqs.irqs[0];
|
---|
203 |
|
---|
204 | if (dma8) {
|
---|
205 | if (hw_res.dma_channels.channels[0] < 4) {
|
---|
206 | *dma8 = hw_res.dma_channels.channels[0];
|
---|
207 | } else {
|
---|
208 | if (hw_res.dma_channels.count == 2 &&
|
---|
209 | hw_res.dma_channels.channels[1] < 4) {
|
---|
210 | *dma8 = hw_res.dma_channels.channels[1];
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (dma16) {
|
---|
216 | if (hw_res.dma_channels.channels[0] > 4) {
|
---|
217 | *dma16 = hw_res.dma_channels.channels[0];
|
---|
218 | } else {
|
---|
219 | if (hw_res.dma_channels.count == 2 &&
|
---|
220 | hw_res.dma_channels.channels[1] > 4) {
|
---|
221 | *dma16 = hw_res.dma_channels.channels[1];
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (hw_res.io_ranges.count == 1) {
|
---|
227 | if (pp_sb_regs && *pp_sb_regs)
|
---|
228 | **pp_sb_regs = hw_res.io_ranges.ranges[0];
|
---|
229 | if (pp_mpu_regs)
|
---|
230 | *pp_mpu_regs = NULL;
|
---|
231 | } else {
|
---|
232 | const int sb =
|
---|
233 | (hw_res.io_ranges.ranges[0].size >= sizeof(sb16_regs_t))
|
---|
234 | ? 0 : 1;
|
---|
235 | const int mpu = 1 - sb;
|
---|
236 | if (pp_sb_regs && *pp_sb_regs)
|
---|
237 | **pp_sb_regs = hw_res.io_ranges.ranges[sb];
|
---|
238 | if (pp_mpu_regs && *pp_mpu_regs)
|
---|
239 | **pp_mpu_regs = hw_res.io_ranges.ranges[mpu];
|
---|
240 | }
|
---|
241 |
|
---|
242 | return EOK;
|
---|
243 | }
|
---|
244 |
|
---|
245 | int sb_enable_interrupts(ddf_dev_t *device)
|
---|
246 | {
|
---|
247 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
---|
248 | if (parent_sess == NULL)
|
---|
249 | return ENOMEM;
|
---|
250 |
|
---|
251 | bool enabled = hw_res_enable_interrupt(parent_sess);
|
---|
252 | async_hangup(parent_sess);
|
---|
253 |
|
---|
254 | return enabled ? EOK : EIO;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * @}
|
---|
259 | */
|
---|