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

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

sb16: Add interrupt handler.

Set buffer alignment to something that memalign can handle (might be wrong).

  • Property mode set to 100644
File size: 7.7 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);
53static int sb_enable_interrupts(ddf_dev_t *device);
54/*----------------------------------------------------------------------------*/
55static driver_ops_t sb_driver_ops = {
56 .add_device = sb_add_device,
57};
58/*----------------------------------------------------------------------------*/
59static driver_t sb_driver = {
60 .name = NAME,
61 .driver_ops = &sb_driver_ops
62};
63//static ddf_dev_ops_t sb_ops = {};
64/*----------------------------------------------------------------------------*/
65/** Initializes global driver structures (NONE).
66 *
67 * @param[in] argc Nmber of arguments in argv vector (ignored).
68 * @param[in] argv Cmdline argument vector (ignored).
69 * @return Error code.
70 *
71 * Driver debug level is set here.
72 */
73int main(int argc, char *argv[])
74{
75 printf(NAME": HelenOS SB16 audio driver.\n");
76 ddf_log_init(NAME, LVL_DEBUG2);
77 return ddf_driver_main(&sb_driver);
78}
79/*----------------------------------------------------------------------------*/
80static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
81{
82 assert(dev);
83 sb16_drv_t *sb = dev->driver_data;
84 assert(sb);
85 sb16_interrupt(sb);
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 */
93static int sb_add_device(ddf_dev_t *device)
94{
95#define CHECK_RET_RETURN(ret, msg...) \
96if (ret != EOK) { \
97 ddf_log_error(msg); \
98 return ret; \
99} else (void)0
100
101 assert(device);
102
103 sb16_drv_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_drv_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;
110
111 ret = sb_get_res(device, &sb_regs, &sb_regs_size, &mpu_regs,
112 &mpu_regs_size, &irq);
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, *mixer_fun = NULL;
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 if (mixer_fun) \
129 ddf_fun_destroy(mixer_fun); \
130 unregister_interrupt_handler(device, irq); \
131 return ret; \
132} else (void)0
133
134 ret = sb_enable_interrupts(device);
135 CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to enable interrupts: %s.\n",
136 str_error(ret));
137
138 dsp_fun = ddf_fun_create(device, fun_exposed, "dsp");
139 ret = dsp_fun ? EOK : ENOMEM;
140 CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to create dsp function.");
141
142
143 mixer_fun = ddf_fun_create(device, fun_exposed, "mixer");
144 ret = dsp_fun ? EOK : ENOMEM;
145 CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to create mixer function.");
146
147 ret = sb16_init_sb16(soft_state, (void*)sb_regs, sb_regs_size);
148 CHECK_RET_UNREG_DEST_RETURN(ret,
149 "Failed to init sb16 driver: %s.\n", str_error(ret));
150
151 ret = ddf_fun_bind(dsp_fun);
152 CHECK_RET_UNREG_DEST_RETURN(ret,
153 "Failed to bind dsp function: %s.\n", str_error(ret));
154
155 ret = ddf_fun_bind(mixer_fun);
156 CHECK_RET_UNREG_DEST_RETURN(ret,
157 "Failed to bind mixer function: %s.\n", str_error(ret));
158
159 /* Everything's OK assign driver_data. */
160 mixer_fun->driver_data = soft_state;
161 dsp_fun->driver_data = soft_state;
162
163 ret = sb16_init_mpu(soft_state, (void*)mpu_regs, mpu_regs_size);
164 if (ret == EOK) {
165 ddf_fun_t *mpu_fun =
166 ddf_fun_create(device, fun_exposed, "midi");
167 if (mpu_fun) {
168 ret = ddf_fun_bind(mpu_fun);
169 if (ret != EOK)
170 ddf_log_error(
171 "Failed to bind midi function: %s.\n",
172 str_error(ret));
173 } else {
174 ddf_log_error("Failed to create midi function.\n");
175 }
176 } else {
177 ddf_log_warning("Failed to init mpu driver: %s.\n", str_error(ret));
178 }
179
180 /* MPU state does not matter */
181 return EOK;
182}
183/*----------------------------------------------------------------------------*/
184static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
185 size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size, int *irq)
186{
187 assert(device);
188 assert(sb_regs);
189 assert(sb_regs_size);
190 assert(mpu_regs);
191 assert(mpu_regs_size);
192 assert(irq);
193
194 async_sess_t *parent_sess =
195 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
196 IPC_FLAG_BLOCKING);
197 if (!parent_sess)
198 return ENOMEM;
199
200 hw_resource_list_t hw_resources;
201 const int rc = hw_res_get_resource_list(parent_sess, &hw_resources);
202 if (rc != EOK) {
203 async_hangup(parent_sess);
204 return rc;
205 }
206
207 size_t i;
208 for (i = 0; i < hw_resources.count; i++) {
209 const hw_resource_t *res = &hw_resources.resources[i];
210 switch (res->type) {
211 case INTERRUPT:
212 *irq = res->res.interrupt.irq;
213 ddf_log_debug("Found interrupt: %d.\n", *irq);
214 break;
215 case IO_RANGE:
216 ddf_log_debug("Found io: %" PRIx64" %zu.\n",
217 res->res.io_range.address, res->res.io_range.size);
218 if (res->res.io_range.size >= sizeof(sb16_regs_t)) {
219 *sb_regs = res->res.io_range.address;
220 *sb_regs_size = res->res.io_range.size;
221 } else {
222 *mpu_regs = res->res.io_range.address;
223 *mpu_regs_size = res->res.io_range.size;
224 }
225 break;
226 default:
227 break;
228 }
229 }
230
231 async_hangup(parent_sess);
232 free(hw_resources.resources);
233 return EOK;
234}
235/*----------------------------------------------------------------------------*/
236int sb_enable_interrupts(ddf_dev_t *device)
237{
238 async_sess_t *parent_sess =
239 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
240 IPC_FLAG_BLOCKING);
241 if (!parent_sess)
242 return ENOMEM;
243
244 bool enabled = hw_res_enable_interrupt(parent_sess);
245 async_hangup(parent_sess);
246
247 return enabled ? EOK : EIO;
248}
249/**
250 * @}
251 */
Note: See TracBrowser for help on using the repository browser.