[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);
|
---|
[53738d3] | 51 | static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
|
---|
[c885a21] | 52 | size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size,
|
---|
| 53 | int *irq, int *dma8, int *dma16);
|
---|
[e0f9950] | 54 | static int sb_enable_interrupts(ddf_dev_t *device);
|
---|
[2fc487f] | 55 | /*----------------------------------------------------------------------------*/
|
---|
| 56 | static driver_ops_t sb_driver_ops = {
|
---|
[722912e] | 57 | .dev_add = sb_add_device,
|
---|
[2fc487f] | 58 | };
|
---|
| 59 | /*----------------------------------------------------------------------------*/
|
---|
| 60 | static driver_t sb_driver = {
|
---|
| 61 | .name = NAME,
|
---|
| 62 | .driver_ops = &sb_driver_ops
|
---|
| 63 | };
|
---|
| 64 | //static ddf_dev_ops_t sb_ops = {};
|
---|
| 65 | /*----------------------------------------------------------------------------*/
|
---|
| 66 | /** Initializes global driver structures (NONE).
|
---|
| 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 | {
|
---|
| 76 | printf(NAME": HelenOS SB16 audio driver.\n");
|
---|
| 77 | ddf_log_init(NAME, LVL_DEBUG2);
|
---|
| 78 | return ddf_driver_main(&sb_driver);
|
---|
| 79 | }
|
---|
[53738d3] | 80 | /*----------------------------------------------------------------------------*/
|
---|
[7a69340] | 81 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
| 82 | {
|
---|
[f14e6ea] | 83 | assert(dev);
|
---|
[c885a21] | 84 | assert(dev->driver_data);
|
---|
| 85 | sb16_interrupt(dev->driver_data);
|
---|
[7a69340] | 86 | }
|
---|
| 87 | /*----------------------------------------------------------------------------*/
|
---|
[53738d3] | 88 | /** Initializes a new ddf driver instance of SB16.
|
---|
| 89 | *
|
---|
| 90 | * @param[in] device DDF instance of the device to initialize.
|
---|
| 91 | * @return Error code.
|
---|
| 92 | */
|
---|
| 93 | static int sb_add_device(ddf_dev_t *device)
|
---|
| 94 | {
|
---|
[e0f9950] | 95 | #define CHECK_RET_RETURN(ret, msg...) \
|
---|
[7a69340] | 96 | if (ret != EOK) { \
|
---|
| 97 | ddf_log_error(msg); \
|
---|
| 98 | return ret; \
|
---|
| 99 | } else (void)0
|
---|
| 100 |
|
---|
[53738d3] | 101 | assert(device);
|
---|
[7a69340] | 102 |
|
---|
[c885a21] | 103 | sb16_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_t));
|
---|
[7a69340] | 104 | int ret = soft_state ? EOK : ENOMEM;
|
---|
[e0f9950] | 105 | CHECK_RET_RETURN(ret, "Failed to allocate sb16 structure.\n");
|
---|
[7a69340] | 106 |
|
---|
[53738d3] | 107 | uintptr_t sb_regs = 0, mpu_regs = 0;
|
---|
| 108 | size_t sb_regs_size = 0, mpu_regs_size = 0;
|
---|
[c885a21] | 109 | int irq = 0, dma8 = 0, dma16 = 0;
|
---|
[7a69340] | 110 |
|
---|
| 111 | ret = sb_get_res(device, &sb_regs, &sb_regs_size, &mpu_regs,
|
---|
[c885a21] | 112 | &mpu_regs_size, &irq, &dma8, &dma16);
|
---|
[e0f9950] | 113 | CHECK_RET_RETURN(ret,
|
---|
[7a69340] | 114 | "Failed to get resources: %s.\n", str_error(ret));
|
---|
| 115 |
|
---|
[413225d] | 116 | const size_t irq_cmd_count = sb16_irq_code_size();
|
---|
| 117 | irq_cmd_t irq_cmds[irq_cmd_count];
|
---|
| 118 | sb16_irq_code((void*)sb_regs, dma8, dma16, irq_cmds);
|
---|
| 119 | irq_code_t irq_code = { .cmdcount = irq_cmd_count, .cmds = irq_cmds };
|
---|
| 120 |
|
---|
| 121 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
[e0f9950] | 122 | CHECK_RET_RETURN(ret,
|
---|
[7a69340] | 123 | "Failed to register irq handler: %s.\n", str_error(ret));
|
---|
| 124 |
|
---|
| 125 | #define CHECK_RET_UNREG_DEST_RETURN(ret, msg...) \
|
---|
| 126 | if (ret != EOK) { \
|
---|
| 127 | ddf_log_error(msg); \
|
---|
| 128 | unregister_interrupt_handler(device, irq); \
|
---|
| 129 | return ret; \
|
---|
| 130 | } else (void)0
|
---|
[e0f9950] | 131 |
|
---|
| 132 | ret = sb_enable_interrupts(device);
|
---|
| 133 | CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to enable interrupts: %s.\n",
|
---|
| 134 | str_error(ret));
|
---|
| 135 |
|
---|
[c885a21] | 136 | ret = sb16_init_sb16(
|
---|
| 137 | soft_state, (void*)sb_regs, sb_regs_size, device, dma8, dma16);
|
---|
[9dd79bc7] | 138 | CHECK_RET_UNREG_DEST_RETURN(ret,
|
---|
| 139 | "Failed to init sb16 driver: %s.\n", str_error(ret));
|
---|
| 140 |
|
---|
[7a69340] | 141 | ret = sb16_init_mpu(soft_state, (void*)mpu_regs, mpu_regs_size);
|
---|
| 142 | if (ret == EOK) {
|
---|
| 143 | ddf_fun_t *mpu_fun =
|
---|
| 144 | ddf_fun_create(device, fun_exposed, "midi");
|
---|
| 145 | if (mpu_fun) {
|
---|
| 146 | ret = ddf_fun_bind(mpu_fun);
|
---|
| 147 | if (ret != EOK)
|
---|
| 148 | ddf_log_error(
|
---|
| 149 | "Failed to bind midi function: %s.\n",
|
---|
| 150 | str_error(ret));
|
---|
| 151 | } else {
|
---|
| 152 | ddf_log_error("Failed to create midi function.\n");
|
---|
| 153 | }
|
---|
| 154 | } else {
|
---|
| 155 | ddf_log_warning("Failed to init mpu driver: %s.\n", str_error(ret));
|
---|
| 156 | }
|
---|
[53738d3] | 157 |
|
---|
[7a69340] | 158 | /* MPU state does not matter */
|
---|
| 159 | return EOK;
|
---|
[53738d3] | 160 | }
|
---|
| 161 | /*----------------------------------------------------------------------------*/
|
---|
| 162 | static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
|
---|
[c885a21] | 163 | size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size,
|
---|
| 164 | int *irq, int *dma8, int *dma16)
|
---|
[53738d3] | 165 | {
|
---|
| 166 | assert(device);
|
---|
| 167 |
|
---|
| 168 | async_sess_t *parent_sess =
|
---|
| 169 | devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
|
---|
| 170 | IPC_FLAG_BLOCKING);
|
---|
| 171 | if (!parent_sess)
|
---|
| 172 | return ENOMEM;
|
---|
| 173 |
|
---|
[ba72f2b] | 174 | hw_res_list_parsed_t hw_res;
|
---|
| 175 | hw_res_list_parsed_init(&hw_res);
|
---|
| 176 | const int ret = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
|
---|
| 177 | async_hangup(parent_sess);
|
---|
| 178 | if (ret != EOK) {
|
---|
| 179 | return ret;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /* 1x IRQ, 1-2x DMA(8,16), 1-2x IO (MPU is separate). */
|
---|
| 183 | if (hw_res.irqs.count != 1 ||
|
---|
| 184 | (hw_res.io_ranges.count != 1 && hw_res.io_ranges.count != 2) ||
|
---|
| 185 | (hw_res.dma_channels.count != 1 && hw_res.dma_channels.count != 2)) {
|
---|
| 186 | hw_res_list_parsed_clean(&hw_res);
|
---|
| 187 | return EINVAL;
|
---|
[53738d3] | 188 | }
|
---|
| 189 |
|
---|
[ba72f2b] | 190 | if (irq)
|
---|
| 191 | *irq = hw_res.irqs.irqs[0];
|
---|
| 192 |
|
---|
| 193 | if (dma8) {
|
---|
| 194 | if (hw_res.dma_channels.channels[0] < 4) {
|
---|
| 195 | *dma8 = hw_res.dma_channels.channels[0];
|
---|
| 196 | } else {
|
---|
| 197 | if (hw_res.dma_channels.count == 2 &&
|
---|
| 198 | hw_res.dma_channels.channels[1] < 4) {
|
---|
| 199 | *dma8 = hw_res.dma_channels.channels[1];
|
---|
[53738d3] | 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[ba72f2b] | 204 | if (dma16) {
|
---|
| 205 | if (hw_res.dma_channels.channels[0] > 4) {
|
---|
| 206 | *dma16 = 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 | *dma16 = hw_res.dma_channels.channels[1];
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | if (hw_res.io_ranges.count == 1) {
|
---|
| 217 | if (sb_regs)
|
---|
| 218 | *sb_regs = hw_res.io_ranges.ranges[0].address;
|
---|
| 219 | if (sb_regs_size)
|
---|
| 220 | *sb_regs_size = hw_res.io_ranges.ranges[0].size;
|
---|
| 221 | } else {
|
---|
| 222 | const int sb =
|
---|
| 223 | (hw_res.io_ranges.ranges[0].size >= sizeof(sb16_regs_t))
|
---|
| 224 | ? 1 : 0;
|
---|
| 225 | const int mpu = 1 - sb;
|
---|
| 226 | if (sb_regs)
|
---|
| 227 | *sb_regs = hw_res.io_ranges.ranges[sb].address;
|
---|
| 228 | if (sb_regs_size)
|
---|
| 229 | *sb_regs_size = hw_res.io_ranges.ranges[sb].size;
|
---|
| 230 | if (mpu_regs)
|
---|
| 231 | *sb_regs = hw_res.io_ranges.ranges[mpu].address;
|
---|
| 232 | if (mpu_regs_size)
|
---|
| 233 | *sb_regs_size = hw_res.io_ranges.ranges[mpu].size;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[53738d3] | 236 | return EOK;
|
---|
| 237 | }
|
---|
[e0f9950] | 238 | /*----------------------------------------------------------------------------*/
|
---|
| 239 | int sb_enable_interrupts(ddf_dev_t *device)
|
---|
| 240 | {
|
---|
| 241 | async_sess_t *parent_sess =
|
---|
| 242 | devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
|
---|
| 243 | IPC_FLAG_BLOCKING);
|
---|
| 244 | if (!parent_sess)
|
---|
| 245 | return ENOMEM;
|
---|
| 246 |
|
---|
| 247 | bool enabled = hw_res_enable_interrupt(parent_sess);
|
---|
| 248 | async_hangup(parent_sess);
|
---|
| 249 |
|
---|
| 250 | return enabled ? EOK : EIO;
|
---|
| 251 | }
|
---|
[2fc487f] | 252 | /**
|
---|
| 253 | * @}
|
---|
| 254 | */
|
---|