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

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

sb16: Add pio ranges to irq code.

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