| [01282fc] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 |
|
|---|
| [0687e1b] | 29 | #include <bool.h>
|
|---|
| [01282fc] | 30 | #include <errno.h>
|
|---|
| [0687e1b] | 31 | #include <libarch/ddi.h>
|
|---|
| [01282fc] | 32 |
|
|---|
| [a4a4e47] | 33 | #include "ddf_log.h"
|
|---|
| [01282fc] | 34 | #include "mixer.h"
|
|---|
| 35 |
|
|---|
| [ce1e5ea] | 36 | typedef struct channel {
|
|---|
| [3df0f75] | 37 | const char *name;
|
|---|
| [0687e1b] | 38 | uint8_t address;
|
|---|
| 39 | unsigned shift;
|
|---|
| [ce1e5ea] | 40 | unsigned volume_levels;
|
|---|
| 41 | bool preserve_bits;
|
|---|
| 42 | } channel_t;
|
|---|
| 43 |
|
|---|
| 44 | typedef struct volume_item {
|
|---|
| 45 | const char *description;
|
|---|
| 46 | uint8_t channels;
|
|---|
| 47 | const channel_t *channel_table;
|
|---|
| [0687e1b] | 48 | } volume_item_t;
|
|---|
| 49 |
|
|---|
| [ce1e5ea] | 50 | /* CT1335 channels */
|
|---|
| 51 | static const channel_t channels_table_ct1335[] = {
|
|---|
| [3df0f75] | 52 | { "", 0x02, 1, 8, false }, /* Master, Mono, 3bit volume level */
|
|---|
| 53 | { "", 0x06, 1, 8, false }, /* Midi, Mono, 3bit volume level */
|
|---|
| 54 | { "", 0x08, 1, 8, false }, /* CD, Mono, 3bit volume level */
|
|---|
| 55 | { "", 0x0a, 1, 4, false }, /* Voice, Mono, 2bit volume level */
|
|---|
| [ce1e5ea] | 56 | };
|
|---|
| 57 |
|
|---|
| 58 | /* CT1345 channels */
|
|---|
| 59 | static const channel_t channels_table_ct1345[] = {
|
|---|
| [3df0f75] | 60 | { "Left", 0x22, 5, 8, true }, /* Master, Left, 3bit volume level */
|
|---|
| 61 | { "Right", 0x22, 1, 8, true }, /* Master, Right, 3bit volume level */
|
|---|
| 62 | { "Left", 0x26, 5, 8, true }, /* Midi, Left, 3bit volume level */
|
|---|
| 63 | { "Right", 0x26, 1, 8, true }, /* Midi, Right, 3bit volume level */
|
|---|
| 64 | { "Left", 0x28, 5, 8, true }, /* CD, Left, 3bit volume level */
|
|---|
| 65 | { "Right", 0x28, 1, 8, true }, /* CD, Right, 3bit volume level */
|
|---|
| 66 | { "Left", 0x2e, 5, 8, true }, /* Line, Left, 3bit volume level */
|
|---|
| 67 | { "Right", 0x2e, 1, 8, true }, /* Line, Right, 3bit volume level */
|
|---|
| 68 | { "Left", 0x04, 5, 8, true }, /* Voice, Left, 3bit volume level */
|
|---|
| 69 | { "Right", 0x04, 1, 8, true }, /* Voice, Right, 3bit volume level */
|
|---|
| 70 | { "", 0x0a, 1, 4, false }, /* Mic, Mono, 2bit volume level */
|
|---|
| [ce1e5ea] | 71 | };
|
|---|
| 72 |
|
|---|
| 73 | /* CT1745 channels */
|
|---|
| 74 | static const channel_t channels_table_ct1745[] = {
|
|---|
| [3df0f75] | 75 | { "Left", 0x30, 3, 32, false }, /* Master, Left, 5bit volume level */
|
|---|
| 76 | { "Right", 0x31, 3, 32, false }, /* Master, Right, 5bit volume level */
|
|---|
| 77 | { "Left", 0x32, 3, 32, false }, /* Voice, Left, 5bit volume level */
|
|---|
| 78 | { "Right", 0x33, 3, 32, false }, /* Voice, Right, 5bit volume level */
|
|---|
| 79 | { "Left", 0x34, 3, 32, false }, /* MIDI, Left, 5bit volume level */
|
|---|
| 80 | { "Right", 0x35, 3, 32, false }, /* MIDI, Right, 5bit volume level */
|
|---|
| 81 | { "Left", 0x36, 3, 32, false }, /* CD, Left, 5bit volume level */
|
|---|
| 82 | { "Right", 0x37, 3, 32, false }, /* CD, Right, 5bit volume level */
|
|---|
| 83 | { "Left", 0x38, 3, 32, false }, /* Line, Left, 5bit volume level */
|
|---|
| 84 | { "Right", 0x39, 3, 32, false }, /* Line, Right, 5bit volume level */
|
|---|
| 85 | { "", 0x3a, 3, 32, false }, /* Mic, Mono, 5bit volume level */
|
|---|
| 86 | { "", 0x3b, 6, 4, false }, /* PC speaker, Mono, 2bit level */
|
|---|
| 87 | { "Left", 0x3f, 6, 4, false }, /* Input Gain, Left, 2bit level */
|
|---|
| 88 | { "Right", 0x40, 6, 4, false }, /* Input Gain, Right, 2bit level */
|
|---|
| 89 | { "Left", 0x41, 6, 4, false }, /* Output Gain, Left, 2bit level */
|
|---|
| 90 | { "Right", 0x42, 6, 4, false }, /* Output Gain, Right, 2bit level */
|
|---|
| 91 | { "Left", 0x44, 4, 16, false }, /* Treble, Left, 4bit volume level */
|
|---|
| 92 | { "Right", 0x45, 4, 16, false }, /* Treble, Right, 4bit volume level */
|
|---|
| 93 | { "Left", 0x46, 4, 16, false }, /* Bass, Left, 4bit volume level */
|
|---|
| 94 | { "Right", 0x47, 4, 16, false }, /* Bass, Right, 4bit volume level */
|
|---|
| [ce1e5ea] | 95 | };
|
|---|
| 96 |
|
|---|
| [0687e1b] | 97 | static const volume_item_t volume_ct1335[] = {
|
|---|
| [ce1e5ea] | 98 | { "Master", 1, &channels_table_ct1335[0] },
|
|---|
| 99 | { "MIDI", 1, &channels_table_ct1335[1] },
|
|---|
| 100 | { "CD", 1, &channels_table_ct1335[2] },
|
|---|
| 101 | { "Voice", 1, &channels_table_ct1335[3] },
|
|---|
| [0687e1b] | 102 | };
|
|---|
| 103 |
|
|---|
| 104 | static const volume_item_t volume_ct1345[] = {
|
|---|
| [ce1e5ea] | 105 | { "Master", 2, &channels_table_ct1345[0] },
|
|---|
| 106 | { "Voice", 2, &channels_table_ct1345[8] },
|
|---|
| 107 | { "Mic", 1, &channels_table_ct1345[10] },
|
|---|
| 108 | { "MIDI", 2, &channels_table_ct1345[2] },
|
|---|
| 109 | { "CD", 2, &channels_table_ct1345[4] },
|
|---|
| 110 | { "Line", 2, &channels_table_ct1345[6] },
|
|---|
| [0687e1b] | 111 | };
|
|---|
| 112 |
|
|---|
| 113 | static const volume_item_t volume_ct1745[] = {
|
|---|
| [ce1e5ea] | 114 | { "Master", 2, &channels_table_ct1745[0] },
|
|---|
| 115 | { "Voice", 2, &channels_table_ct1745[2] },
|
|---|
| 116 | { "MIDI", 2, &channels_table_ct1745[4] },
|
|---|
| 117 | { "CD", 2, &channels_table_ct1745[6] },
|
|---|
| 118 | { "Line", 2, &channels_table_ct1745[8] },
|
|---|
| 119 | { "Mic", 1, &channels_table_ct1745[10] },
|
|---|
| 120 | { "PC Speaker", 1, &channels_table_ct1745[11] },
|
|---|
| 121 | { "Input Gain", 2, &channels_table_ct1745[12] },
|
|---|
| 122 | { "Output Gain", 2, &channels_table_ct1745[14] },
|
|---|
| 123 | { "Treble", 2, &channels_table_ct1745[16] },
|
|---|
| [427069d] | 124 | { "Bass", 2, &channels_table_ct1745[18] },
|
|---|
| [0687e1b] | 125 | };
|
|---|
| 126 |
|
|---|
| 127 | static const struct {
|
|---|
| 128 | size_t count;
|
|---|
| 129 | const volume_item_t *table;
|
|---|
| 130 | } volume_table[] = {
|
|---|
| 131 | [SB_MIXER_NONE] = { 0, NULL },
|
|---|
| 132 | [SB_MIXER_UNKNOWN] = { 0, NULL },
|
|---|
| 133 | [SB_MIXER_CT1335] = {
|
|---|
| 134 | sizeof(volume_ct1335) / sizeof(volume_item_t), volume_ct1335 },
|
|---|
| 135 | [SB_MIXER_CT1345] = {
|
|---|
| 136 | sizeof(volume_ct1345) / sizeof(volume_item_t), volume_ct1345 },
|
|---|
| 137 | [SB_MIXER_CT1745] = {
|
|---|
| 138 | sizeof(volume_ct1745) / sizeof(volume_item_t), volume_ct1745 },
|
|---|
| 139 | };
|
|---|
| [01282fc] | 140 | /*----------------------------------------------------------------------------*/
|
|---|
| [bde691c] | 141 | const char * sb_mixer_type_str(sb_mixer_type_t type)
|
|---|
| [01282fc] | 142 | {
|
|---|
| [050d4fa] | 143 | static const char * names[] = {
|
|---|
| 144 | [SB_MIXER_CT1335] = "CT 1335",
|
|---|
| 145 | [SB_MIXER_CT1345] = "CT 1345",
|
|---|
| 146 | [SB_MIXER_CT1745] = "CT 1745",
|
|---|
| 147 | [SB_MIXER_UNKNOWN] = "Unknown mixer",
|
|---|
| 148 | };
|
|---|
| 149 | return names[type];
|
|---|
| 150 | }
|
|---|
| 151 | /*----------------------------------------------------------------------------*/
|
|---|
| [bde691c] | 152 | int sb_mixer_init(sb_mixer_t *mixer, sb16_regs_t *regs, sb_mixer_type_t type)
|
|---|
| [050d4fa] | 153 | {
|
|---|
| 154 | assert(mixer);
|
|---|
| 155 | mixer->regs = regs;
|
|---|
| 156 | mixer->type = type;
|
|---|
| 157 | if (type == SB_MIXER_UNKNOWN)
|
|---|
| 158 | return ENOTSUP;
|
|---|
| 159 |
|
|---|
| 160 | if (type != SB_MIXER_NONE) {
|
|---|
| [b130d0e] | 161 | pio_write_8(®s->mixer_address, MIXER_RESET_ADDRESS);
|
|---|
| [050d4fa] | 162 | pio_write_8(®s->mixer_data, 1);
|
|---|
| 163 | }
|
|---|
| [b130d0e] | 164 | pio_write_8(®s->mixer_address, MIXER_PNP_IRQ_ADDRESS);
|
|---|
| 165 | const uint8_t irq = pio_read_8(®s->mixer_data);
|
|---|
| 166 | pio_write_8(®s->mixer_address, MIXER_PNP_DMA_ADDRESS);
|
|---|
| 167 | const uint8_t dma = pio_read_8(®s->mixer_data);
|
|---|
| 168 | ddf_log_debug("SB16 setup with IRQ 0x%hhx and DMA 0x%hhx.\n", irq, dma);
|
|---|
| [050d4fa] | 169 | return EOK;
|
|---|
| [01282fc] | 170 | }
|
|---|
| 171 | /*----------------------------------------------------------------------------*/
|
|---|
| [bde691c] | 172 | int sb_mixer_get_control_item_count(const sb_mixer_t *mixer)
|
|---|
| [01282fc] | 173 | {
|
|---|
| [050d4fa] | 174 | assert(mixer);
|
|---|
| 175 | return volume_table[mixer->type].count;
|
|---|
| [01282fc] | 176 | }
|
|---|
| 177 | /*----------------------------------------------------------------------------*/
|
|---|
| [bde691c] | 178 | int sb_mixer_get_control_item_info(const sb_mixer_t *mixer, unsigned index,
|
|---|
| [3df0f75] | 179 | const char** name, unsigned *channels)
|
|---|
| [01282fc] | 180 | {
|
|---|
| [050d4fa] | 181 | assert(mixer);
|
|---|
| 182 | if (index > volume_table[mixer->type].count)
|
|---|
| [0687e1b] | 183 | return ENOENT;
|
|---|
| 184 |
|
|---|
| [3df0f75] | 185 | const volume_item_t *item = &volume_table[mixer->type].table[index];
|
|---|
| [0687e1b] | 186 | if (name)
|
|---|
| [3df0f75] | 187 | *name = item->description;
|
|---|
| [0687e1b] | 188 | if (channels)
|
|---|
| [3df0f75] | 189 | *channels = item->channels;
|
|---|
| 190 | return EOK;
|
|---|
| 191 | }
|
|---|
| 192 | /*----------------------------------------------------------------------------*/
|
|---|
| 193 | int sb_mixer_get_channel_info(const sb_mixer_t *mixer, unsigned index,
|
|---|
| 194 | unsigned channel, const char **name, unsigned *levels)
|
|---|
| 195 | {
|
|---|
| 196 | assert(mixer);
|
|---|
| 197 | if (index > volume_table[mixer->type].count)
|
|---|
| 198 | return ENOENT;
|
|---|
| 199 |
|
|---|
| 200 | const volume_item_t *item = &volume_table[mixer->type].table[index];
|
|---|
| 201 | if (channel > item->channels)
|
|---|
| 202 | return ENOENT;
|
|---|
| 203 |
|
|---|
| 204 | const channel_t *chan = &item->channel_table[channel];
|
|---|
| 205 | if (name)
|
|---|
| 206 | *name = chan->name;
|
|---|
| [b1926d0a] | 207 | if (levels)
|
|---|
| [3df0f75] | 208 | *levels = chan->volume_levels;
|
|---|
| [0687e1b] | 209 | return EOK;
|
|---|
| [01282fc] | 210 | }
|
|---|
| 211 | /*----------------------------------------------------------------------------*/
|
|---|
| [bde691c] | 212 | int sb_mixer_set_volume_level(const sb_mixer_t *mixer,
|
|---|
| [b3f36d2] | 213 | unsigned index, unsigned channel, unsigned level)
|
|---|
| [01282fc] | 214 | {
|
|---|
| [050d4fa] | 215 | if (mixer->type == SB_MIXER_UNKNOWN || mixer->type == SB_MIXER_NONE)
|
|---|
| [b3f36d2] | 216 | return ENOTSUP;
|
|---|
| [050d4fa] | 217 | if (index >= volume_table[mixer->type].count)
|
|---|
| [b3f36d2] | 218 | return ENOENT;
|
|---|
| [050d4fa] | 219 | if (channel >= volume_table[mixer->type].table[index].channels)
|
|---|
| [b3f36d2] | 220 | return ENOENT;
|
|---|
| 221 |
|
|---|
| [3df0f75] | 222 | const channel_t *chan =
|
|---|
| 223 | &volume_table[mixer->type].table[index].channel_table[channel];
|
|---|
| [ce1e5ea] | 224 |
|
|---|
| [80b9ab3] | 225 | if (level >= chan->volume_levels)
|
|---|
| 226 | level = chan->volume_levels - 1;
|
|---|
| [3df0f75] | 227 |
|
|---|
| 228 | pio_write_8(&mixer->regs->mixer_address, chan->address);
|
|---|
| [ce1e5ea] | 229 |
|
|---|
| [3df0f75] | 230 | uint8_t value = 0;
|
|---|
| 231 | if (chan->preserve_bits) {
|
|---|
| [ce1e5ea] | 232 | value = pio_read_8(&mixer->regs->mixer_data);
|
|---|
| [3df0f75] | 233 | value &= ~(uint8_t)((chan->volume_levels - 1) << chan->shift);
|
|---|
| [b3f36d2] | 234 | }
|
|---|
| [ce1e5ea] | 235 |
|
|---|
| [3df0f75] | 236 | value |= level << chan->shift;
|
|---|
| [ce1e5ea] | 237 | pio_write_8(&mixer->regs->mixer_data, value);
|
|---|
| [485a496] | 238 | ddf_log_note("Channel %s %s volume set to: %u.\n",
|
|---|
| 239 | volume_table[mixer->type].table[index].description,
|
|---|
| 240 | chan->name, level);
|
|---|
| [b3f36d2] | 241 | return EOK;
|
|---|
| [01282fc] | 242 | }
|
|---|
| 243 | /*----------------------------------------------------------------------------*/
|
|---|
| [bde691c] | 244 | unsigned sb_mixer_get_volume_level(const sb_mixer_t *mixer, unsigned index,
|
|---|
| [050d4fa] | 245 | unsigned channel)
|
|---|
| [01282fc] | 246 | {
|
|---|
| [050d4fa] | 247 | assert(mixer);
|
|---|
| 248 | if (mixer->type == SB_MIXER_UNKNOWN
|
|---|
| 249 | || mixer->type == SB_MIXER_NONE
|
|---|
| 250 | || (index >= volume_table[mixer->type].count)
|
|---|
| 251 | || (channel >= volume_table[mixer->type].table[index].channels))
|
|---|
| [b3f36d2] | 252 | return 0;
|
|---|
| 253 |
|
|---|
| [3df0f75] | 254 | const channel_t *chan =
|
|---|
| 255 | &volume_table[mixer->type].table[index].channel_table[channel];
|
|---|
| 256 |
|
|---|
| 257 | pio_write_8(&mixer->regs->mixer_address, chan->address);
|
|---|
| 258 | return (pio_read_8(&mixer->regs->mixer_data) >> chan->shift)
|
|---|
| 259 | & (chan->volume_levels - 1);
|
|---|
| [01282fc] | 260 | }
|
|---|