source: mainline/uspace/drv/audio/sb16/main.c@ 25c98a8e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25c98a8e was 25c98a8e, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

sb16: Implement audio mixer interface.

Move mixer fun creation and initialization to sb_init.

  • Property mode set to 100644
File size: 7.7 KB
RevLine 
[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>
[53738d3]38#include <device/hw_res.h>
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
50static int sb_add_device(ddf_dev_t *device);
[53738d3]51static 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]54static int sb_enable_interrupts(ddf_dev_t *device);
[2fc487f]55/*----------------------------------------------------------------------------*/
56static driver_ops_t sb_driver_ops = {
57 .add_device = sb_add_device,
58};
59/*----------------------------------------------------------------------------*/
60static 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 */
74int 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]81static 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 */
93static int sb_add_device(ddf_dev_t *device)
94{
[e0f9950]95#define CHECK_RET_RETURN(ret, msg...) \
[7a69340]96if (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
116 irq_code_t *irq_code = sb16_irq_code();
117 ret = register_interrupt_handler(device, irq, irq_handler, irq_code);
[e0f9950]118 CHECK_RET_RETURN(ret,
[7a69340]119 "Failed to register irq handler: %s.\n", str_error(ret));
120
[e0f9950]121
[25c98a8e]122 ddf_fun_t *dsp_fun = NULL;
[7a69340]123#define CHECK_RET_UNREG_DEST_RETURN(ret, msg...) \
124if (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
[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
[7a69340]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
[c885a21]140 ret = sb16_init_sb16(
141 soft_state, (void*)sb_regs, sb_regs_size, device, dma8, dma16);
[9dd79bc7]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
[e0f9950]149 /* Everything's OK assign driver_data. */
150 dsp_fun->driver_data = soft_state;
[7a69340]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 }
[53738d3]168
[7a69340]169 /* MPU state does not matter */
170 return EOK;
[53738d3]171}
172/*----------------------------------------------------------------------------*/
173static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
[c885a21]174 size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size,
175 int *irq, int *dma8, int *dma16)
[53738d3]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);
[c885a21]183 assert(dma8);
184 assert(dma16);
[53738d3]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;
[c885a21]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);
[53738d3]225 default:
226 break;
227 }
228 }
229
230 async_hangup(parent_sess);
231 free(hw_resources.resources);
232 return EOK;
233}
[e0f9950]234/*----------------------------------------------------------------------------*/
235int 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}
[2fc487f]248/**
249 * @}
250 */
Note: See TracBrowser for help on using the repository browser.