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

Last change on this file was a64970e1, checked in by Jiri Svoboda <jiri@…>, 5 months ago

Implement quiesce in HD Audio and SB16 drivers.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2011 Jan Vesely
4 * Copyright (c) 2011 Vojtech Horky
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/** @addtogroup drvaudiosb16
31 * @{
32 */
33/** @file
34 * Main routines of Creative Labs SoundBlaster 16 driver
35 */
36#include <ddf/driver.h>
37#include <ddf/interrupt.h>
38#include <ddf/log.h>
39#include <device/hw_res_parsed.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 errno_t sb_add_device(ddf_dev_t *device);
51static errno_t sb_dev_quiesce(ddf_dev_t *device);
52static errno_t sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
53 addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16);
54static errno_t sb_enable_interrupt(ddf_dev_t *device, int irq);
55
56static driver_ops_t sb_driver_ops = {
57 .dev_add = sb_add_device,
58 .dev_quiesce = sb_dev_quiesce
59};
60
61static driver_t sb_driver = {
62 .name = NAME,
63 .driver_ops = &sb_driver_ops
64};
65
66/** Initialize 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);
78 return ddf_driver_main(&sb_driver);
79}
80
81/** SB16 IRQ handler.
82 *
83 * @param call IRQ event notification
84 * @param arg Argument (sb16_t *)
85 */
86static void irq_handler(ipc_call_t *call, void *arg)
87{
88 sb16_t *sb16_dev = (sb16_t *)arg;
89 sb16_interrupt(sb16_dev);
90}
91
92/** Initialize new SB16 driver instance.
93 *
94 * @param[in] device DDF instance of the device to initialize.
95 * @return Error code.
96 */
97static errno_t sb_add_device(ddf_dev_t *device)
98{
99 bool handler_regd = false;
100 const size_t irq_cmd_count = sb16_irq_code_size();
101 irq_cmd_t irq_cmds[irq_cmd_count];
102 irq_pio_range_t irq_ranges[1];
103 cap_irq_handle_t irq_cap;
104
105 sb16_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_t));
106 errno_t rc = soft_state ? EOK : ENOMEM;
107 if (rc != EOK) {
108 ddf_log_error("Failed to allocate sb16 structure.");
109 goto error;
110 }
111
112 addr_range_t sb_regs;
113 addr_range_t *p_sb_regs = &sb_regs;
114 addr_range_t mpu_regs;
115 addr_range_t *p_mpu_regs = &mpu_regs;
116 int irq = 0, dma8 = 0, dma16 = 0;
117
118 rc = sb_get_res(device, &p_sb_regs, &p_mpu_regs, &irq, &dma8, &dma16);
119 if (rc != EOK) {
120 ddf_log_error("Failed to get resources: %s.", str_error(rc));
121 goto error;
122 }
123
124 sb16_irq_code(p_sb_regs, dma8, dma16, irq_cmds, irq_ranges);
125
126 irq_code_t irq_code = {
127 .cmdcount = irq_cmd_count,
128 .cmds = irq_cmds,
129 .rangecount = 1,
130 .ranges = irq_ranges
131 };
132
133 rc = register_interrupt_handler(device, irq, irq_handler,
134 (void *)soft_state, &irq_code, &irq_cap);
135 if (rc != EOK) {
136 ddf_log_error("Failed to register irq handler: %s.",
137 str_error(rc));
138 goto error;
139 }
140
141 handler_regd = true;
142
143 rc = sb_enable_interrupt(device, irq);
144 if (rc != EOK) {
145 ddf_log_error("Failed to enable interrupts: %s.",
146 str_error(rc));
147 goto error;
148 }
149
150 rc = sb16_init_sb16(soft_state, p_sb_regs, device, dma8, dma16);
151 if (rc != EOK) {
152 ddf_log_error("Failed to init sb16 driver: %s.",
153 str_error(rc));
154 goto error;
155 }
156
157 rc = sb16_init_mpu(soft_state, p_mpu_regs);
158 if (rc == EOK) {
159 ddf_fun_t *mpu_fun =
160 ddf_fun_create(device, fun_exposed, "midi");
161 if (mpu_fun) {
162 rc = ddf_fun_bind(mpu_fun);
163 if (rc != EOK)
164 ddf_log_error(
165 "Failed to bind midi function: %s.",
166 str_error(rc));
167 } else {
168 ddf_log_error("Failed to create midi function.");
169 }
170 } else {
171 ddf_log_warning("Failed to init mpu driver: %s.",
172 str_error(rc));
173 }
174
175 /* MPU state does not matter */
176 return EOK;
177error:
178 if (handler_regd)
179 unregister_interrupt_handler(device, irq_cap);
180 return rc;
181}
182
183/** Initialize new SB16 driver instance.
184 *
185 * @param[in] device DDF instance of the device to initialize.
186 * @return Error code.
187 */
188static errno_t sb_dev_quiesce(ddf_dev_t *device)
189{
190 sb16_t *soft_state = (sb16_t *)ddf_dev_data_get(device);
191
192 return sb16_quiesce(soft_state);
193}
194
195static errno_t sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
196 addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16)
197{
198 assert(device);
199
200 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
201 if (parent_sess == NULL)
202 return ENOMEM;
203
204 hw_res_list_parsed_t hw_res;
205 hw_res_list_parsed_init(&hw_res);
206 const errno_t ret = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
207 if (ret != EOK) {
208 return ret;
209 }
210
211 /* 1x IRQ, 1-2x DMA(8,16), 1-2x IO (MPU is separate). */
212 if (hw_res.irqs.count != 1 ||
213 (hw_res.io_ranges.count != 1 && hw_res.io_ranges.count != 2) ||
214 (hw_res.dma_channels.count != 1 && hw_res.dma_channels.count != 2)) {
215 hw_res_list_parsed_clean(&hw_res);
216 return EINVAL;
217 }
218
219 if (irq)
220 *irq = hw_res.irqs.irqs[0];
221
222 if (dma8) {
223 if (hw_res.dma_channels.channels[0] < 4) {
224 *dma8 = hw_res.dma_channels.channels[0];
225 } else {
226 if (hw_res.dma_channels.count == 2 &&
227 hw_res.dma_channels.channels[1] < 4) {
228 *dma8 = hw_res.dma_channels.channels[1];
229 }
230 }
231 }
232
233 if (dma16) {
234 if (hw_res.dma_channels.channels[0] > 4) {
235 *dma16 = hw_res.dma_channels.channels[0];
236 } else {
237 if (hw_res.dma_channels.count == 2 &&
238 hw_res.dma_channels.channels[1] > 4) {
239 *dma16 = hw_res.dma_channels.channels[1];
240 }
241 }
242 }
243
244 if (hw_res.io_ranges.count == 1) {
245 if (pp_sb_regs && *pp_sb_regs)
246 **pp_sb_regs = hw_res.io_ranges.ranges[0];
247 if (pp_mpu_regs)
248 *pp_mpu_regs = NULL;
249 } else {
250 const int sb =
251 (hw_res.io_ranges.ranges[0].size >= sizeof(sb16_regs_t)) ?
252 0 : 1;
253 const int mpu = 1 - sb;
254 if (pp_sb_regs && *pp_sb_regs)
255 **pp_sb_regs = hw_res.io_ranges.ranges[sb];
256 if (pp_mpu_regs && *pp_mpu_regs)
257 **pp_mpu_regs = hw_res.io_ranges.ranges[mpu];
258 }
259
260 return EOK;
261}
262
263static errno_t sb_enable_interrupt(ddf_dev_t *device, int irq)
264{
265 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
266 if (parent_sess == NULL)
267 return ENOMEM;
268
269 return hw_res_enable_interrupt(parent_sess, irq);
270}
271
272/**
273 * @}
274 */
Note: See TracBrowser for help on using the repository browser.