| 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.h>
|
|---|
| 39 | #include <devman.h>
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "ddf_log.h"
|
|---|
| 46 | #include "sb16.h"
|
|---|
| 47 |
|
|---|
| 48 | #define NAME "sb16"
|
|---|
| 49 |
|
|---|
| 50 | static int sb_add_device(ddf_dev_t *device);
|
|---|
| 51 | static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
|
|---|
| 52 | size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size,
|
|---|
| 53 | int *irq, int *dma8, int *dma16);
|
|---|
| 54 | static int sb_enable_interrupts(ddf_dev_t *device);
|
|---|
| 55 | /*----------------------------------------------------------------------------*/
|
|---|
| 56 | static driver_ops_t sb_driver_ops = {
|
|---|
| 57 | .add_device = sb_add_device,
|
|---|
| 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 | }
|
|---|
| 80 | /*----------------------------------------------------------------------------*/
|
|---|
| 81 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 82 | {
|
|---|
| 83 | assert(dev);
|
|---|
| 84 | assert(dev->driver_data);
|
|---|
| 85 | sb16_interrupt(dev->driver_data);
|
|---|
| 86 | }
|
|---|
| 87 | /*----------------------------------------------------------------------------*/
|
|---|
| 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 | {
|
|---|
| 95 | #define CHECK_RET_RETURN(ret, msg...) \
|
|---|
| 96 | if (ret != EOK) { \
|
|---|
| 97 | ddf_log_error(msg); \
|
|---|
| 98 | return ret; \
|
|---|
| 99 | } else (void)0
|
|---|
| 100 |
|
|---|
| 101 | assert(device);
|
|---|
| 102 |
|
|---|
| 103 | sb16_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_t));
|
|---|
| 104 | int ret = soft_state ? EOK : ENOMEM;
|
|---|
| 105 | CHECK_RET_RETURN(ret, "Failed to allocate sb16 structure.\n");
|
|---|
| 106 |
|
|---|
| 107 | uintptr_t sb_regs = 0, mpu_regs = 0;
|
|---|
| 108 | size_t sb_regs_size = 0, mpu_regs_size = 0;
|
|---|
| 109 | int irq = 0, dma8 = 0, dma16 = 0;
|
|---|
| 110 |
|
|---|
| 111 | ret = sb_get_res(device, &sb_regs, &sb_regs_size, &mpu_regs,
|
|---|
| 112 | &mpu_regs_size, &irq, &dma8, &dma16);
|
|---|
| 113 | CHECK_RET_RETURN(ret,
|
|---|
| 114 | "Failed to get resources: %s.\n", str_error(ret));
|
|---|
| 115 |
|
|---|
| 116 | irq_code_t *irq_code = sb16_irq_code();
|
|---|
| 117 | ret = register_interrupt_handler(device, irq, irq_handler, irq_code);
|
|---|
| 118 | CHECK_RET_RETURN(ret,
|
|---|
| 119 | "Failed to register irq handler: %s.\n", str_error(ret));
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | ddf_fun_t *dsp_fun = NULL;
|
|---|
| 123 | #define CHECK_RET_UNREG_DEST_RETURN(ret, msg...) \
|
|---|
| 124 | if (ret != EOK) { \
|
|---|
| 125 | ddf_log_error(msg); \
|
|---|
| 126 | if (dsp_fun) \
|
|---|
| 127 | ddf_fun_destroy(dsp_fun); \
|
|---|
| 128 | unregister_interrupt_handler(device, irq); \
|
|---|
| 129 | return ret; \
|
|---|
| 130 | } else (void)0
|
|---|
| 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 |
|
|---|
| 136 | dsp_fun = ddf_fun_create(device, fun_exposed, "dsp");
|
|---|
| 137 | ret = dsp_fun ? EOK : ENOMEM;
|
|---|
| 138 | CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to create dsp function.");
|
|---|
| 139 |
|
|---|
| 140 | ret = sb16_init_sb16(
|
|---|
| 141 | soft_state, (void*)sb_regs, sb_regs_size, device, dma8, dma16);
|
|---|
| 142 | CHECK_RET_UNREG_DEST_RETURN(ret,
|
|---|
| 143 | "Failed to init sb16 driver: %s.\n", str_error(ret));
|
|---|
| 144 |
|
|---|
| 145 | ret = ddf_fun_bind(dsp_fun);
|
|---|
| 146 | CHECK_RET_UNREG_DEST_RETURN(ret,
|
|---|
| 147 | "Failed to bind dsp function: %s.\n", str_error(ret));
|
|---|
| 148 |
|
|---|
| 149 | /* Everything's OK assign driver_data. */
|
|---|
| 150 | dsp_fun->driver_data = soft_state;
|
|---|
| 151 |
|
|---|
| 152 | ret = sb16_init_mpu(soft_state, (void*)mpu_regs, mpu_regs_size);
|
|---|
| 153 | if (ret == EOK) {
|
|---|
| 154 | ddf_fun_t *mpu_fun =
|
|---|
| 155 | ddf_fun_create(device, fun_exposed, "midi");
|
|---|
| 156 | if (mpu_fun) {
|
|---|
| 157 | ret = ddf_fun_bind(mpu_fun);
|
|---|
| 158 | if (ret != EOK)
|
|---|
| 159 | ddf_log_error(
|
|---|
| 160 | "Failed to bind midi function: %s.\n",
|
|---|
| 161 | str_error(ret));
|
|---|
| 162 | } else {
|
|---|
| 163 | ddf_log_error("Failed to create midi function.\n");
|
|---|
| 164 | }
|
|---|
| 165 | } else {
|
|---|
| 166 | ddf_log_warning("Failed to init mpu driver: %s.\n", str_error(ret));
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /* MPU state does not matter */
|
|---|
| 170 | return EOK;
|
|---|
| 171 | }
|
|---|
| 172 | /*----------------------------------------------------------------------------*/
|
|---|
| 173 | static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
|
|---|
| 174 | size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size,
|
|---|
| 175 | int *irq, int *dma8, int *dma16)
|
|---|
| 176 | {
|
|---|
| 177 | assert(device);
|
|---|
| 178 | assert(sb_regs);
|
|---|
| 179 | assert(sb_regs_size);
|
|---|
| 180 | assert(mpu_regs);
|
|---|
| 181 | assert(mpu_regs_size);
|
|---|
| 182 | assert(irq);
|
|---|
| 183 | assert(dma8);
|
|---|
| 184 | assert(dma16);
|
|---|
| 185 |
|
|---|
| 186 | async_sess_t *parent_sess =
|
|---|
| 187 | devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
|
|---|
| 188 | IPC_FLAG_BLOCKING);
|
|---|
| 189 | if (!parent_sess)
|
|---|
| 190 | return ENOMEM;
|
|---|
| 191 |
|
|---|
| 192 | hw_resource_list_t hw_resources;
|
|---|
| 193 | const int rc = hw_res_get_resource_list(parent_sess, &hw_resources);
|
|---|
| 194 | if (rc != EOK) {
|
|---|
| 195 | async_hangup(parent_sess);
|
|---|
| 196 | return rc;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | size_t i;
|
|---|
| 200 | for (i = 0; i < hw_resources.count; i++) {
|
|---|
| 201 | const hw_resource_t *res = &hw_resources.resources[i];
|
|---|
| 202 | switch (res->type) {
|
|---|
| 203 | case INTERRUPT:
|
|---|
| 204 | *irq = res->res.interrupt.irq;
|
|---|
| 205 | ddf_log_debug("Found interrupt: %d.\n", *irq);
|
|---|
| 206 | break;
|
|---|
| 207 | case IO_RANGE:
|
|---|
| 208 | ddf_log_debug("Found io: %" PRIx64" %zu.\n",
|
|---|
| 209 | res->res.io_range.address, res->res.io_range.size);
|
|---|
| 210 | if (res->res.io_range.size >= sizeof(sb16_regs_t)) {
|
|---|
| 211 | *sb_regs = res->res.io_range.address;
|
|---|
| 212 | *sb_regs_size = res->res.io_range.size;
|
|---|
| 213 | } else {
|
|---|
| 214 | *mpu_regs = res->res.io_range.address;
|
|---|
| 215 | *mpu_regs_size = res->res.io_range.size;
|
|---|
| 216 | }
|
|---|
| 217 | break;
|
|---|
| 218 | case DMA_CHANNEL_16:
|
|---|
| 219 | *dma16 = res->res.dma_channel.dma16;
|
|---|
| 220 | ddf_log_debug("Found DMA16 channel: %d.\n", *dma16);
|
|---|
| 221 | break;
|
|---|
| 222 | case DMA_CHANNEL_8:
|
|---|
| 223 | *dma8 = res->res.dma_channel.dma8;
|
|---|
| 224 | ddf_log_debug("Found DMA8 channel: %d.\n", *dma8);
|
|---|
| 225 | default:
|
|---|
| 226 | break;
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | async_hangup(parent_sess);
|
|---|
| 231 | free(hw_resources.resources);
|
|---|
| 232 | return EOK;
|
|---|
| 233 | }
|
|---|
| 234 | /*----------------------------------------------------------------------------*/
|
|---|
| 235 | int sb_enable_interrupts(ddf_dev_t *device)
|
|---|
| 236 | {
|
|---|
| 237 | async_sess_t *parent_sess =
|
|---|
| 238 | devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
|
|---|
| 239 | IPC_FLAG_BLOCKING);
|
|---|
| 240 | if (!parent_sess)
|
|---|
| 241 | return ENOMEM;
|
|---|
| 242 |
|
|---|
| 243 | bool enabled = hw_res_enable_interrupt(parent_sess);
|
|---|
| 244 | async_hangup(parent_sess);
|
|---|
| 245 |
|
|---|
| 246 | return enabled ? EOK : EIO;
|
|---|
| 247 | }
|
|---|
| 248 | /**
|
|---|
| 249 | * @}
|
|---|
| 250 | */
|
|---|