source: mainline/uspace/drv/audio/hdaudio/hdaudio.c@ 7cd7a8d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7cd7a8d was 3fafe5e0, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix incorrectly indented double-slash comments.

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[b229062]1/*
2 * Copyright (c) 2014 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup hdaudio
30 * @{
31 */
32/** @file High Definition Audio driver
33 */
34
35#include <assert.h>
[a333b7f]36#include <bitops.h>
[b229062]37#include <ddi.h>
[d51838f]38#include <device/hw_res.h>
[b229062]39#include <device/hw_res_parsed.h>
40#include <stdio.h>
41#include <errno.h>
42#include <str_error.h>
43#include <ddf/driver.h>
[a333b7f]44#include <ddf/interrupt.h>
[b229062]45#include <ddf/log.h>
46
47#include "hdactl.h"
48#include "hdaudio.h"
[93c3163]49#include "pcm_iface.h"
[7978d1e7]50#include "spec/regs.h"
[b229062]51
52#define NAME "hdaudio"
53
[b7fd2a0]54static errno_t hda_dev_add(ddf_dev_t *dev);
55static errno_t hda_dev_remove(ddf_dev_t *dev);
56static errno_t hda_dev_gone(ddf_dev_t *dev);
57static errno_t hda_fun_online(ddf_fun_t *fun);
58static errno_t hda_fun_offline(ddf_fun_t *fun);
[b229062]59
[01c3bb4]60static void hdaudio_interrupt(ipc_call_t *, ddf_dev_t *);
[a333b7f]61
[b229062]62static driver_ops_t driver_ops = {
63 .dev_add = &hda_dev_add,
64 .dev_remove = &hda_dev_remove,
65 .dev_gone = &hda_dev_gone,
66 .fun_online = &hda_fun_online,
67 .fun_offline = &hda_fun_offline
68};
69
70static driver_t hda_driver = {
71 .name = NAME,
72 .driver_ops = &driver_ops
73};
74
[93c3163]75ddf_dev_ops_t hda_pcm_ops = {
76 .interfaces[AUDIO_PCM_BUFFER_IFACE] = &hda_pcm_iface
77};
78
[a333b7f]79irq_pio_range_t hdaudio_irq_pio_ranges[] = {
80 {
81 .base = 0,
82 .size = 8192
83 }
84};
85
86irq_cmd_t hdaudio_irq_commands[] = {
[31ccd42a]87 /* 0 */
[a333b7f]88 {
89 .cmd = CMD_PIO_READ_8,
[903eff5]90 .addr = NULL, /* rirbsts */
[a333b7f]91 .dstarg = 2
92 },
[31ccd42a]93 /* 1 */
[a333b7f]94 {
95 .cmd = CMD_AND,
96 .value = BIT_V(uint8_t, rirbsts_intfl),
97 .srcarg = 2,
98 .dstarg = 3
99 },
[31ccd42a]100 /* 2 */
[a333b7f]101 {
102 .cmd = CMD_PREDICATE,
103 .value = 2,
104 .srcarg = 3
105 },
[31ccd42a]106 /* 3 */
[a333b7f]107 {
108 .cmd = CMD_PIO_WRITE_8,
[903eff5]109 .addr = NULL, /* rirbsts */
[31ccd42a]110 .value = BIT_V(uint8_t, rirbsts_intfl)
[a333b7f]111 },
[31ccd42a]112 /* 4 */
[a333b7f]113 {
114 .cmd = CMD_ACCEPT
115 }
116};
117
[31ccd42a]118irq_cmd_t hdaudio_irq_commands_sdesc[] = {
119 /* 0 */
120 {
121 .cmd = CMD_PIO_READ_32,
122 .addr = NULL, /* intsts */
123 .dstarg = 2
124 },
125 /* 1 */
126 {
127 .cmd = CMD_AND,
128 .value = 0, /* 1 << idx */
129 .srcarg = 2,
130 .dstarg = 3,
131 },
132 /* 2 */
133 {
134 .cmd = CMD_PREDICATE,
135 .value = 2,
136 .srcarg = 3
137 },
138 /* 3 */
139 {
140 .cmd = CMD_PIO_WRITE_8,
141 .addr = NULL, /* sdesc[x].sts */
142 .value = 0x4 /* XXX sdesc.sts.BCIS */
143 },
144 /* 4 */
145 {
146 .cmd = CMD_ACCEPT
147 }
[a333b7f]148};
149
[b7fd2a0]150static errno_t hda_dev_add(ddf_dev_t *dev)
[b229062]151{
[de16f89]152 ddf_fun_t *fun_pcm = NULL;
[b229062]153 hda_t *hda = NULL;
154 hw_res_list_parsed_t res;
[31ccd42a]155 irq_code_t irq_code;
[de16f89]156 irq_cmd_t *cmds = NULL;
[31ccd42a]157 size_t ncmds_base;
158 size_t ncmds_sdesc;
159 size_t ncmds;
160 int i;
[de16f89]161 void *regs = NULL;
[b7fd2a0]162 errno_t rc;
[b229062]163
164 ddf_msg(LVL_NOTE, "hda_dev_add()");
[de16f89]165 hw_res_list_parsed_init(&res);
[b229062]166
167 hda = ddf_dev_data_alloc(dev, sizeof(hda_t));
168 if (hda == NULL) {
169 ddf_msg(LVL_ERROR, "Failed allocating soft state.\n");
170 rc = ENOMEM;
171 goto error;
172 }
173
[57a2208]174 fibril_mutex_initialize(&hda->lock);
175
[b229062]176 ddf_msg(LVL_NOTE, "create parent sess");
[2fd26bb]177 hda->parent_sess = ddf_dev_parent_sess_get(dev);
[b229062]178 if (hda->parent_sess == NULL) {
179 ddf_msg(LVL_ERROR, "Failed connecting parent driver.\n");
180 rc = ENOMEM;
181 goto error;
182 }
183
184 ddf_msg(LVL_NOTE, "get HW res list");
185 rc = hw_res_get_list_parsed(hda->parent_sess, &res, 0);
186 if (rc != EOK) {
187 ddf_msg(LVL_ERROR, "Failed getting resource list.\n");
188 goto error;
189 }
190
191 if (res.mem_ranges.count != 1) {
192 ddf_msg(LVL_ERROR, "Expected exactly one memory range.\n");
193 rc = EINVAL;
194 goto error;
195 }
196
197 hda->rwbase = RNGABS(res.mem_ranges.ranges[0]);
198 hda->rwsize = RNGSZ(res.mem_ranges.ranges[0]);
199
[a333b7f]200 ddf_msg(LVL_NOTE, "hda reg base: %" PRIx64,
[3bacee1]201 RNGABS(res.mem_ranges.ranges[0]));
[a333b7f]202
[b229062]203 if (hda->rwsize < sizeof(hda_regs_t)) {
204 ddf_msg(LVL_ERROR, "Memory range is too small.");
205 rc = EINVAL;
206 goto error;
207 }
208
209 ddf_msg(LVL_NOTE, "enable PIO");
210 rc = pio_enable((void *)(uintptr_t)hda->rwbase, hda->rwsize, &regs);
211 if (rc != EOK) {
212 ddf_msg(LVL_ERROR, "Error enabling PIO range.");
213 goto error;
214 }
215
216 hda->regs = (hda_regs_t *)regs;
217
[149dd52d]218 ddf_msg(LVL_NOTE, "IRQs: %zu", res.irqs.count);
[a333b7f]219 if (res.irqs.count != 1) {
[149dd52d]220 ddf_msg(LVL_ERROR, "Unexpected IRQ count %zu (!= 1)",
[a333b7f]221 res.irqs.count);
222 goto error;
223 }
224 ddf_msg(LVL_NOTE, "interrupt no: %d", res.irqs.irqs[0]);
225
[31ccd42a]226 ncmds_base = sizeof(hdaudio_irq_commands) / sizeof(irq_cmd_t);
227 ncmds_sdesc = sizeof(hdaudio_irq_commands_sdesc) / sizeof(irq_cmd_t);
228 ncmds = ncmds_base + 30 * ncmds_sdesc;
229
230 cmds = calloc(ncmds, sizeof(irq_cmd_t));
231 if (cmds == NULL) {
232 ddf_msg(LVL_ERROR, "Out of memory");
233 goto error;
234 }
235
236 irq_code.rangecount = sizeof(hdaudio_irq_pio_ranges) /
237 sizeof(irq_pio_range_t);
238 irq_code.ranges = hdaudio_irq_pio_ranges;
239 irq_code.cmdcount = ncmds;
240 irq_code.cmds = cmds;
241
[a333b7f]242 hda_regs_t *rphys = (hda_regs_t *)(uintptr_t)hda->rwbase;
243 hdaudio_irq_pio_ranges[0].base = (uintptr_t)hda->rwbase;
[31ccd42a]244
245 memcpy(cmds, hdaudio_irq_commands, sizeof(hdaudio_irq_commands));
246 cmds[0].addr = (void *)&rphys->rirbsts;
247 cmds[3].addr = (void *)&rphys->rirbsts;
248
249 for (i = 0; i < 30; i++) {
250 memcpy(&cmds[ncmds_base + i * ncmds_sdesc],
251 hdaudio_irq_commands_sdesc, sizeof(hdaudio_irq_commands_sdesc));
252 cmds[ncmds_base + i * ncmds_sdesc + 0].addr = (void *)&rphys->intsts;
253 cmds[ncmds_base + i * ncmds_sdesc + 1].value = BIT_V(uint32_t, i);
254 cmds[ncmds_base + i * ncmds_sdesc + 3].addr = (void *)&rphys->sdesc[i].sts;
255 }
256
[149dd52d]257 ddf_msg(LVL_NOTE, "range0.base=%zu", hdaudio_irq_pio_ranges[0].base);
[a333b7f]258
[d51838f]259 rc = hw_res_enable_interrupt(hda->parent_sess, res.irqs.irqs[0]);
[1e92bc3]260 if (rc != EOK) {
[c1694b6b]261 ddf_msg(LVL_ERROR, "Failed enabling interrupt.: %s", str_error(rc));
[1e92bc3]262 goto error;
263 }
264
[eadaeae8]265 cap_irq_handle_t irq_cap;
[071a1ddb]266 rc = register_interrupt_handler(dev, res.irqs.irqs[0],
267 hdaudio_interrupt, &irq_code, &irq_cap);
268 if (rc != EOK) {
[dd8ab1c]269 ddf_msg(LVL_ERROR, "Failed registering interrupt handler: %s",
270 str_error_name(rc));
[a333b7f]271 goto error;
272 }
273
[de16f89]274 free(cmds);
275 cmds = NULL;
276
[7978d1e7]277 if (hda_ctl_init(hda) == NULL) {
[b229062]278 rc = EIO;
279 goto error;
280 }
281
282 ddf_msg(LVL_NOTE, "create function");
[93c3163]283 fun_pcm = ddf_fun_create(dev, fun_exposed, "pcm");
284 if (fun_pcm == NULL) {
285 ddf_msg(LVL_ERROR, "Failed creating function 'pcm'.");
[b229062]286 rc = ENOMEM;
287 goto error;
288 }
289
[93c3163]290 hda->fun_pcm = fun_pcm;
291
292 ddf_fun_set_ops(fun_pcm, &hda_pcm_ops);
[b229062]293
[93c3163]294 rc = ddf_fun_bind(fun_pcm);
[b229062]295 if (rc != EOK) {
[93c3163]296 ddf_msg(LVL_ERROR, "Failed binding function 'pcm'.");
297 ddf_fun_destroy(fun_pcm);
[b229062]298 goto error;
299 }
300
[93c3163]301 ddf_fun_add_to_category(fun_pcm, "audio-pcm");
[de16f89]302
303 hw_res_list_parsed_clean(&res);
[b229062]304 return EOK;
305error:
[de16f89]306 if (fun_pcm != NULL)
307 ddf_fun_destroy(fun_pcm);
[b229062]308 if (hda != NULL) {
309 if (hda->ctl != NULL)
310 hda_ctl_fini(hda->ctl);
311 }
[de16f89]312 free(cmds);
313 // pio_disable(regs);
314 hw_res_list_parsed_clean(&res);
[b229062]315
[dd8ab1c]316 ddf_msg(LVL_NOTE, "Failing hda_dev_add() -> %s", str_error_name(rc));
[b229062]317 return rc;
318}
319
[b7fd2a0]320static errno_t hda_dev_remove(ddf_dev_t *dev)
[b229062]321{
322 hda_t *hda = (hda_t *)ddf_dev_data_get(dev);
[b7fd2a0]323 errno_t rc;
[b229062]324
325 ddf_msg(LVL_DEBUG, "hda_dev_remove(%p)", dev);
326
[93c3163]327 if (hda->fun_pcm != NULL) {
328 rc = ddf_fun_offline(hda->fun_pcm);
[b229062]329 if (rc != EOK)
330 return rc;
331
[93c3163]332 rc = ddf_fun_unbind(hda->fun_pcm);
[b229062]333 if (rc != EOK)
334 return rc;
335 }
336
[de16f89]337 hda_ctl_fini(hda->ctl);
338 // pio_disable(regs);
[b229062]339 return EOK;
340}
341
[b7fd2a0]342static errno_t hda_dev_gone(ddf_dev_t *dev)
[b229062]343{
344 hda_t *hda = (hda_t *)ddf_dev_data_get(dev);
[b7fd2a0]345 errno_t rc;
[b229062]346
347 ddf_msg(LVL_DEBUG, "hda_dev_remove(%p)", dev);
348
[93c3163]349 if (hda->fun_pcm != NULL) {
350 rc = ddf_fun_unbind(hda->fun_pcm);
[b229062]351 if (rc != EOK)
352 return rc;
353 }
354
355 return EOK;
356}
357
[b7fd2a0]358static errno_t hda_fun_online(ddf_fun_t *fun)
[b229062]359{
360 ddf_msg(LVL_DEBUG, "hda_fun_online()");
361 return ddf_fun_online(fun);
362}
363
[b7fd2a0]364static errno_t hda_fun_offline(ddf_fun_t *fun)
[b229062]365{
366 ddf_msg(LVL_DEBUG, "hda_fun_offline()");
367 return ddf_fun_offline(fun);
368}
369
[01c3bb4]370static void hdaudio_interrupt(ipc_call_t *icall, ddf_dev_t *dev)
[a333b7f]371{
372 hda_t *hda = (hda_t *)ddf_dev_data_get(dev);
373
[3bacee1]374 if (0)
375 ddf_msg(LVL_NOTE, "## interrupt ##");
[57a2208]376 hda_ctl_interrupt(hda->ctl);
377
[159c722d]378 if (IPC_GET_ARG3(*icall) != 0) {
[57a2208]379 /* Buffer completed */
380 hda_lock(hda);
381 if (hda->playing) {
382 hda_pcm_event(hda, PCM_EVENT_FRAMES_PLAYED);
383 hda_pcm_event(hda, PCM_EVENT_FRAMES_PLAYED);
384 hda_pcm_event(hda, PCM_EVENT_FRAMES_PLAYED);
385 hda_pcm_event(hda, PCM_EVENT_FRAMES_PLAYED);
[0e4c5f0]386 } else if (hda->capturing) {
387 hda_pcm_event(hda, PCM_EVENT_FRAMES_CAPTURED);
388 hda_pcm_event(hda, PCM_EVENT_FRAMES_CAPTURED);
389 hda_pcm_event(hda, PCM_EVENT_FRAMES_CAPTURED);
390 hda_pcm_event(hda, PCM_EVENT_FRAMES_CAPTURED);
[57a2208]391 }
[0e4c5f0]392
[57a2208]393 hda_unlock(hda);
[159c722d]394 }
[57a2208]395}
396
397void hda_lock(hda_t *hda)
398{
399 fibril_mutex_lock(&hda->lock);
400}
401
402void hda_unlock(hda_t *hda)
403{
404 fibril_mutex_unlock(&hda->lock);
[a333b7f]405}
406
[b229062]407int main(int argc, char *argv[])
408{
409 printf(NAME ": High Definition Audio driver\n");
410 ddf_log_init(NAME);
411 return ddf_driver_main(&hda_driver);
412}
413
414/** @}
415 */
Note: See TracBrowser for help on using the repository browser.