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

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

sb16: Add hw DSP initialization.

  • Property mode set to 100644
File size: 7.1 KB
Line 
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
50static int sb_add_device(ddf_dev_t *device);
51static 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, int *irq);
53/*----------------------------------------------------------------------------*/
54static driver_ops_t sb_driver_ops = {
55 .add_device = sb_add_device,
56};
57/*----------------------------------------------------------------------------*/
58static driver_t sb_driver = {
59 .name = NAME,
60 .driver_ops = &sb_driver_ops
61};
62//static ddf_dev_ops_t sb_ops = {};
63/*----------------------------------------------------------------------------*/
64/** Initializes global driver structures (NONE).
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 */
72int main(int argc, char *argv[])
73{
74 printf(NAME": HelenOS SB16 audio driver.\n");
75 ddf_log_init(NAME, LVL_DEBUG2);
76 return ddf_driver_main(&sb_driver);
77}
78/*----------------------------------------------------------------------------*/
79static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
80{
81 ddf_log_note("SB16 IRQ handler.\n");
82}
83/*----------------------------------------------------------------------------*/
84/** Initializes a new ddf driver instance of SB16.
85 *
86 * @param[in] device DDF instance of the device to initialize.
87 * @return Error code.
88 */
89static int sb_add_device(ddf_dev_t *device)
90{
91#define CHECK_RET_FREE_RETURN(ret, msg...) \
92if (ret != EOK) { \
93 free(soft_state); \
94 ddf_log_error(msg); \
95 return ret; \
96} else (void)0
97
98 assert(device);
99
100 sb16_drv_t *soft_state = malloc(sizeof(sb16_drv_t));
101 int ret = soft_state ? EOK : ENOMEM;
102 CHECK_RET_FREE_RETURN(ret, "Failed to allocate sb16 structure.\n");
103
104 uintptr_t sb_regs = 0, mpu_regs = 0;
105 size_t sb_regs_size = 0, mpu_regs_size = 0;
106 int irq = 0;
107
108 ret = sb_get_res(device, &sb_regs, &sb_regs_size, &mpu_regs,
109 &mpu_regs_size, &irq);
110 CHECK_RET_FREE_RETURN(ret,
111 "Failed to get resources: %s.\n", str_error(ret));
112
113 irq_code_t *irq_code = sb16_irq_code();
114 ret = register_interrupt_handler(device, irq, irq_handler, irq_code);
115 CHECK_RET_FREE_RETURN(ret,
116 "Failed to register irq handler: %s.\n", str_error(ret));
117
118 ddf_fun_t *dsp_fun = NULL, *mixer_fun = NULL;
119#define CHECK_RET_UNREG_DEST_RETURN(ret, msg...) \
120if (ret != EOK) { \
121 ddf_log_error(msg); \
122 if (dsp_fun) \
123 ddf_fun_destroy(dsp_fun); \
124 if (mixer_fun) \
125 ddf_fun_destroy(mixer_fun); \
126 free(soft_state); \
127 unregister_interrupt_handler(device, irq); \
128 return ret; \
129} else (void)0
130 dsp_fun = ddf_fun_create(device, fun_exposed, "dsp");
131 ret = dsp_fun ? EOK : ENOMEM;
132 CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to create dsp function.");
133
134
135 mixer_fun = ddf_fun_create(device, fun_exposed, "mixer");
136 ret = dsp_fun ? EOK : ENOMEM;
137 CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to create mixer function.");
138
139 ret = sb16_init_sb16(soft_state, (void*)sb_regs, sb_regs_size);
140 CHECK_RET_UNREG_DEST_RETURN(ret,
141 "Failed to init sb16 driver: %s.\n", str_error(ret));
142
143 ret = ddf_fun_bind(dsp_fun);
144 CHECK_RET_UNREG_DEST_RETURN(ret,
145 "Failed to bind dsp function: %s.\n", str_error(ret));
146 dsp_fun->driver_data = soft_state;
147
148 ret = ddf_fun_bind(mixer_fun);
149 CHECK_RET_UNREG_DEST_RETURN(ret,
150 "Failed to bind mixer function: %s.\n", str_error(ret));
151 mixer_fun->driver_data = soft_state;
152
153
154 ret = sb16_init_mpu(soft_state, (void*)mpu_regs, mpu_regs_size);
155 if (ret == EOK) {
156 ddf_fun_t *mpu_fun =
157 ddf_fun_create(device, fun_exposed, "midi");
158 if (mpu_fun) {
159 ret = ddf_fun_bind(mpu_fun);
160 if (ret != EOK)
161 ddf_log_error(
162 "Failed to bind midi function: %s.\n",
163 str_error(ret));
164 } else {
165 ddf_log_error("Failed to create midi function.\n");
166 }
167 } else {
168 ddf_log_warning("Failed to init mpu driver: %s.\n", str_error(ret));
169 }
170
171 /* MPU state does not matter */
172 return EOK;
173}
174/*----------------------------------------------------------------------------*/
175static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
176 size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size, int *irq)
177{
178 assert(device);
179 assert(sb_regs);
180 assert(sb_regs_size);
181 assert(mpu_regs);
182 assert(mpu_regs_size);
183 assert(irq);
184
185 async_sess_t *parent_sess =
186 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
187 IPC_FLAG_BLOCKING);
188 if (!parent_sess)
189 return ENOMEM;
190
191 hw_resource_list_t hw_resources;
192 const int rc = hw_res_get_resource_list(parent_sess, &hw_resources);
193 if (rc != EOK) {
194 async_hangup(parent_sess);
195 return rc;
196 }
197
198 size_t i;
199 for (i = 0; i < hw_resources.count; i++) {
200 const hw_resource_t *res = &hw_resources.resources[i];
201 switch (res->type) {
202 case INTERRUPT:
203 *irq = res->res.interrupt.irq;
204 ddf_log_debug("Found interrupt: %d.\n", *irq);
205 break;
206 case IO_RANGE:
207 ddf_log_debug("Found io: %" PRIx64" %zu.\n",
208 res->res.io_range.address, res->res.io_range.size);
209 if (res->res.io_range.size >= sizeof(sb16_regs_t)) {
210 *sb_regs = res->res.io_range.address;
211 *sb_regs_size = res->res.io_range.size;
212 } else {
213 *mpu_regs = res->res.io_range.address;
214 *mpu_regs_size = res->res.io_range.size;
215 }
216 break;
217 default:
218 break;
219 }
220 }
221
222 async_hangup(parent_sess);
223 free(hw_resources.resources);
224 return EOK;
225}
226/**
227 * @}
228 */
229
Note: See TracBrowser for help on using the repository browser.