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