[2fc487f] | 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>
|
---|
[ba72f2b] | 38 | #include <device/hw_res_parsed.h>
|
---|
[53738d3] | 39 | #include <devman.h>
|
---|
| 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 |
|
---|
| 50 | static int sb_add_device(ddf_dev_t *device);
|
---|
[7de1988c] | 51 | static int sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
|
---|
| 52 | addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16);
|
---|
[e0f9950] | 53 | static int sb_enable_interrupts(ddf_dev_t *device);
|
---|
[5b1adf5] | 54 |
|
---|
[2fc487f] | 55 | static driver_ops_t sb_driver_ops = {
|
---|
[722912e] | 56 | .dev_add = sb_add_device,
|
---|
[2fc487f] | 57 | };
|
---|
[5b1adf5] | 58 |
|
---|
[2fc487f] | 59 | static driver_t sb_driver = {
|
---|
| 60 | .name = NAME,
|
---|
| 61 | .driver_ops = &sb_driver_ops
|
---|
| 62 | };
|
---|
[5b1adf5] | 63 |
|
---|
| 64 | /** Initialize global driver structures (NONE).
|
---|
[2fc487f] | 65 | *
|
---|
| 66 | * @param[in] argc Nmber of arguments in argv vector (ignored).
|
---|
| 67 | * @param[in] argv Cmdline argument vector (ignored).
|
---|
| 68 | * @return Error code.
|
---|
| 69 | *
|
---|
| 70 | * Driver debug level is set here.
|
---|
| 71 | */
|
---|
| 72 | int main(int argc, char *argv[])
|
---|
| 73 | {
|
---|
| 74 | printf(NAME": HelenOS SB16 audio driver.\n");
|
---|
[03362fbd] | 75 | ddf_log_init(NAME);
|
---|
[2fc487f] | 76 | return ddf_driver_main(&sb_driver);
|
---|
| 77 | }
|
---|
[e941bf8] | 78 |
|
---|
[8820544] | 79 | static void irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
---|
[7a69340] | 80 | {
|
---|
[03362fbd] | 81 | sb16_t *sb16_dev = ddf_dev_data_get(dev);
|
---|
| 82 | sb16_interrupt(sb16_dev);
|
---|
[7a69340] | 83 | }
|
---|
[e941bf8] | 84 |
|
---|
[5b1adf5] | 85 | /** Initialize new SB16 driver instance.
|
---|
[53738d3] | 86 | *
|
---|
| 87 | * @param[in] device DDF instance of the device to initialize.
|
---|
| 88 | * @return Error code.
|
---|
| 89 | */
|
---|
| 90 | static int sb_add_device(ddf_dev_t *device)
|
---|
| 91 | {
|
---|
[5b1adf5] | 92 | bool handler_regd = false;
|
---|
| 93 | const size_t irq_cmd_count = sb16_irq_code_size();
|
---|
| 94 | irq_cmd_t irq_cmds[irq_cmd_count];
|
---|
| 95 | irq_pio_range_t irq_ranges[1];
|
---|
[7a69340] | 96 |
|
---|
[c885a21] | 97 | sb16_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_t));
|
---|
[5b1adf5] | 98 | int rc = soft_state ? EOK : ENOMEM;
|
---|
| 99 | if (rc != EOK) {
|
---|
| 100 | ddf_log_error("Failed to allocate sb16 structure.");
|
---|
| 101 | goto error;
|
---|
| 102 | }
|
---|
[7a69340] | 103 |
|
---|
[7de1988c] | 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;
|
---|
[c885a21] | 108 | int irq = 0, dma8 = 0, dma16 = 0;
|
---|
[7a69340] | 109 |
|
---|
[7de1988c] | 110 | rc = sb_get_res(device, &p_sb_regs, &p_mpu_regs, &irq, &dma8, &dma16);
|
---|
[5b1adf5] | 111 | if (rc != EOK) {
|
---|
| 112 | ddf_log_error("Failed to get resources: %s.", str_error(rc));
|
---|
| 113 | goto error;
|
---|
| 114 | }
|
---|
[7a69340] | 115 |
|
---|
[7de1988c] | 116 | sb16_irq_code(p_sb_regs, dma8, dma16, irq_cmds, irq_ranges);
|
---|
[ea150dc6] | 117 |
|
---|
| 118 | irq_code_t irq_code = {
|
---|
| 119 | .cmdcount = irq_cmd_count,
|
---|
| 120 | .cmds = irq_cmds,
|
---|
| 121 | .rangecount = 1,
|
---|
| 122 | .ranges = irq_ranges
|
---|
| 123 | };
|
---|
[413225d] | 124 |
|
---|
[5b1adf5] | 125 | rc = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
| 126 | if (rc != EOK) {
|
---|
| 127 | ddf_log_error("Failed to register irq handler: %s.",
|
---|
| 128 | str_error(rc));
|
---|
| 129 | goto error;
|
---|
| 130 | }
|
---|
[7a69340] | 131 |
|
---|
[5b1adf5] | 132 | handler_regd = true;
|
---|
[e0f9950] | 133 |
|
---|
[5b1adf5] | 134 | rc = sb_enable_interrupts(device);
|
---|
| 135 | if (rc != EOK) {
|
---|
| 136 | ddf_log_error("Failed to enable interrupts: %s.",
|
---|
| 137 | str_error(rc));
|
---|
| 138 | goto error;
|
---|
| 139 | }
|
---|
[e0f9950] | 140 |
|
---|
[7de1988c] | 141 | rc = sb16_init_sb16(soft_state, p_sb_regs, device, dma8, dma16);
|
---|
[5b1adf5] | 142 | if (rc != EOK) {
|
---|
| 143 | ddf_log_error("Failed to init sb16 driver: %s.",
|
---|
| 144 | str_error(rc));
|
---|
| 145 | goto error;
|
---|
| 146 | }
|
---|
[9dd79bc7] | 147 |
|
---|
[7de1988c] | 148 | rc = sb16_init_mpu(soft_state, p_mpu_regs);
|
---|
[5b1adf5] | 149 | if (rc == EOK) {
|
---|
[7a69340] | 150 | ddf_fun_t *mpu_fun =
|
---|
| 151 | ddf_fun_create(device, fun_exposed, "midi");
|
---|
| 152 | if (mpu_fun) {
|
---|
[5b1adf5] | 153 | rc = ddf_fun_bind(mpu_fun);
|
---|
| 154 | if (rc != EOK)
|
---|
[7a69340] | 155 | ddf_log_error(
|
---|
[124f9bd] | 156 | "Failed to bind midi function: %s.",
|
---|
[5b1adf5] | 157 | str_error(rc));
|
---|
[7a69340] | 158 | } else {
|
---|
[124f9bd] | 159 | ddf_log_error("Failed to create midi function.");
|
---|
[7a69340] | 160 | }
|
---|
| 161 | } else {
|
---|
[5b1adf5] | 162 | ddf_log_warning("Failed to init mpu driver: %s.",
|
---|
| 163 | str_error(rc));
|
---|
[7a69340] | 164 | }
|
---|
[53738d3] | 165 |
|
---|
[7a69340] | 166 | /* MPU state does not matter */
|
---|
| 167 | return EOK;
|
---|
[5b1adf5] | 168 | error:
|
---|
| 169 | if (handler_regd)
|
---|
| 170 | unregister_interrupt_handler(device, irq);
|
---|
| 171 | return rc;
|
---|
[53738d3] | 172 | }
|
---|
[e941bf8] | 173 |
|
---|
[7de1988c] | 174 | static int sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
|
---|
| 175 | addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16)
|
---|
[53738d3] | 176 | {
|
---|
| 177 | assert(device);
|
---|
| 178 |
|
---|
[03362fbd] | 179 | async_sess_t *parent_sess = devman_parent_device_connect(
|
---|
[f9b2cb4c] | 180 | ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
---|
[53738d3] | 181 | if (!parent_sess)
|
---|
| 182 | return ENOMEM;
|
---|
| 183 |
|
---|
[ba72f2b] | 184 | hw_res_list_parsed_t hw_res;
|
---|
| 185 | hw_res_list_parsed_init(&hw_res);
|
---|
| 186 | const int ret = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
|
---|
| 187 | async_hangup(parent_sess);
|
---|
| 188 | if (ret != EOK) {
|
---|
| 189 | return ret;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /* 1x IRQ, 1-2x DMA(8,16), 1-2x IO (MPU is separate). */
|
---|
| 193 | if (hw_res.irqs.count != 1 ||
|
---|
| 194 | (hw_res.io_ranges.count != 1 && hw_res.io_ranges.count != 2) ||
|
---|
| 195 | (hw_res.dma_channels.count != 1 && hw_res.dma_channels.count != 2)) {
|
---|
| 196 | hw_res_list_parsed_clean(&hw_res);
|
---|
| 197 | return EINVAL;
|
---|
[53738d3] | 198 | }
|
---|
| 199 |
|
---|
[ba72f2b] | 200 | if (irq)
|
---|
| 201 | *irq = hw_res.irqs.irqs[0];
|
---|
| 202 |
|
---|
| 203 | if (dma8) {
|
---|
| 204 | if (hw_res.dma_channels.channels[0] < 4) {
|
---|
| 205 | *dma8 = hw_res.dma_channels.channels[0];
|
---|
| 206 | } else {
|
---|
| 207 | if (hw_res.dma_channels.count == 2 &&
|
---|
| 208 | hw_res.dma_channels.channels[1] < 4) {
|
---|
| 209 | *dma8 = hw_res.dma_channels.channels[1];
|
---|
[53738d3] | 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[ba72f2b] | 214 | if (dma16) {
|
---|
| 215 | if (hw_res.dma_channels.channels[0] > 4) {
|
---|
| 216 | *dma16 = hw_res.dma_channels.channels[0];
|
---|
| 217 | } else {
|
---|
| 218 | if (hw_res.dma_channels.count == 2 &&
|
---|
| 219 | hw_res.dma_channels.channels[1] > 4) {
|
---|
| 220 | *dma16 = hw_res.dma_channels.channels[1];
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | if (hw_res.io_ranges.count == 1) {
|
---|
[7de1988c] | 226 | if (pp_sb_regs && *pp_sb_regs)
|
---|
| 227 | **pp_sb_regs = hw_res.io_ranges.ranges[0];
|
---|
| 228 | if (pp_mpu_regs)
|
---|
| 229 | *pp_mpu_regs = NULL;
|
---|
[ba72f2b] | 230 | } else {
|
---|
| 231 | const int sb =
|
---|
| 232 | (hw_res.io_ranges.ranges[0].size >= sizeof(sb16_regs_t))
|
---|
[7de1988c] | 233 | ? 0 : 1;
|
---|
[ba72f2b] | 234 | const int mpu = 1 - sb;
|
---|
[7de1988c] | 235 | if (pp_sb_regs && *pp_sb_regs)
|
---|
| 236 | **pp_sb_regs = hw_res.io_ranges.ranges[sb];
|
---|
| 237 | if (pp_mpu_regs && *pp_mpu_regs)
|
---|
| 238 | **pp_mpu_regs = hw_res.io_ranges.ranges[mpu];
|
---|
[ba72f2b] | 239 | }
|
---|
| 240 |
|
---|
[53738d3] | 241 | return EOK;
|
---|
| 242 | }
|
---|
[e941bf8] | 243 |
|
---|
[e0f9950] | 244 | int sb_enable_interrupts(ddf_dev_t *device)
|
---|
| 245 | {
|
---|
[03362fbd] | 246 | async_sess_t *parent_sess = devman_parent_device_connect(
|
---|
[f9b2cb4c] | 247 | ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
---|
[e0f9950] | 248 | if (!parent_sess)
|
---|
| 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 | }
|
---|
[7de1988c] | 256 |
|
---|
[2fc487f] | 257 | /**
|
---|
| 258 | * @}
|
---|
| 259 | */
|
---|