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

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

audio/sb16: Remove optical separators.

  • Property mode set to 100644
File size: 7.5 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_parsed.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,
53 int *irq, int *dma8, int *dma16);
54static int sb_enable_interrupts(ddf_dev_t *device);
55/*----------------------------------------------------------------------------*/
56static driver_ops_t sb_driver_ops = {
57 .dev_add = 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}
80
81static 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 */
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_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.");
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, "Failed to get resources: %s.", str_error(ret));
114
115 const size_t irq_cmd_count = sb16_irq_code_size();
116 irq_cmd_t irq_cmds[irq_cmd_count];
117 irq_pio_range_t irq_ranges[1];
118 sb16_irq_code((void*)sb_regs, dma8, dma16, irq_cmds, irq_ranges);
119
120 irq_code_t irq_code = {
121 .cmdcount = irq_cmd_count,
122 .cmds = irq_cmds,
123 .rangecount = 1,
124 .ranges = irq_ranges
125 };
126
127 ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
128 CHECK_RET_RETURN(ret,
129 "Failed to register irq handler: %s.", str_error(ret));
130
131#define CHECK_RET_UNREG_DEST_RETURN(ret, msg...) \
132if (ret != EOK) { \
133 ddf_log_error(msg); \
134 unregister_interrupt_handler(device, irq); \
135 return ret; \
136} else (void)0
137
138 ret = sb_enable_interrupts(device);
139 CHECK_RET_UNREG_DEST_RETURN(ret, "Failed to enable interrupts: %s.",
140 str_error(ret));
141
142 ret = sb16_init_sb16(
143 soft_state, (void*)sb_regs, sb_regs_size, device, dma8, dma16);
144 CHECK_RET_UNREG_DEST_RETURN(ret,
145 "Failed to init sb16 driver: %s.", str_error(ret));
146
147 ret = sb16_init_mpu(soft_state, (void*)mpu_regs, mpu_regs_size);
148 if (ret == EOK) {
149 ddf_fun_t *mpu_fun =
150 ddf_fun_create(device, fun_exposed, "midi");
151 if (mpu_fun) {
152 ret = ddf_fun_bind(mpu_fun);
153 if (ret != EOK)
154 ddf_log_error(
155 "Failed to bind midi function: %s.",
156 str_error(ret));
157 } else {
158 ddf_log_error("Failed to create midi function.");
159 }
160 } else {
161 ddf_log_warning("Failed to init mpu driver: %s.", str_error(ret));
162 }
163
164 /* MPU state does not matter */
165 return EOK;
166}
167
168static int sb_get_res(const ddf_dev_t *device, uintptr_t *sb_regs,
169 size_t *sb_regs_size, uintptr_t *mpu_regs, size_t *mpu_regs_size,
170 int *irq, int *dma8, int *dma16)
171{
172 assert(device);
173
174 async_sess_t *parent_sess =
175 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
176 IPC_FLAG_BLOCKING);
177 if (!parent_sess)
178 return ENOMEM;
179
180 hw_res_list_parsed_t hw_res;
181 hw_res_list_parsed_init(&hw_res);
182 const int ret = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
183 async_hangup(parent_sess);
184 if (ret != EOK) {
185 return ret;
186 }
187
188 /* 1x IRQ, 1-2x DMA(8,16), 1-2x IO (MPU is separate). */
189 if (hw_res.irqs.count != 1 ||
190 (hw_res.io_ranges.count != 1 && hw_res.io_ranges.count != 2) ||
191 (hw_res.dma_channels.count != 1 && hw_res.dma_channels.count != 2)) {
192 hw_res_list_parsed_clean(&hw_res);
193 return EINVAL;
194 }
195
196 if (irq)
197 *irq = hw_res.irqs.irqs[0];
198
199 if (dma8) {
200 if (hw_res.dma_channels.channels[0] < 4) {
201 *dma8 = hw_res.dma_channels.channels[0];
202 } else {
203 if (hw_res.dma_channels.count == 2 &&
204 hw_res.dma_channels.channels[1] < 4) {
205 *dma8 = hw_res.dma_channels.channels[1];
206 }
207 }
208 }
209
210 if (dma16) {
211 if (hw_res.dma_channels.channels[0] > 4) {
212 *dma16 = hw_res.dma_channels.channels[0];
213 } else {
214 if (hw_res.dma_channels.count == 2 &&
215 hw_res.dma_channels.channels[1] > 4) {
216 *dma16 = hw_res.dma_channels.channels[1];
217 }
218 }
219 }
220
221
222 if (hw_res.io_ranges.count == 1) {
223 if (sb_regs)
224 *sb_regs = hw_res.io_ranges.ranges[0].address;
225 if (sb_regs_size)
226 *sb_regs_size = hw_res.io_ranges.ranges[0].size;
227 } else {
228 const int sb =
229 (hw_res.io_ranges.ranges[0].size >= sizeof(sb16_regs_t))
230 ? 1 : 0;
231 const int mpu = 1 - sb;
232 if (sb_regs)
233 *sb_regs = hw_res.io_ranges.ranges[sb].address;
234 if (sb_regs_size)
235 *sb_regs_size = hw_res.io_ranges.ranges[sb].size;
236 if (mpu_regs)
237 *sb_regs = hw_res.io_ranges.ranges[mpu].address;
238 if (mpu_regs_size)
239 *sb_regs_size = hw_res.io_ranges.ranges[mpu].size;
240 }
241
242 return EOK;
243}
244
245int sb_enable_interrupts(ddf_dev_t *device)
246{
247 async_sess_t *parent_sess =
248 devman_parent_device_connect(EXCHANGE_SERIALIZE, device->handle,
249 IPC_FLAG_BLOCKING);
250 if (!parent_sess)
251 return ENOMEM;
252
253 bool enabled = hw_res_enable_interrupt(parent_sess);
254 async_hangup(parent_sess);
255
256 return enabled ? EOK : EIO;
257}
258/**
259 * @}
260 */
Note: See TracBrowser for help on using the repository browser.