module 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright 2003 Tematic Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
19
#include <ctype.h>
20 21 22 23 24 25 26
#include <inttypes.h>
#include "kernel.h"
#include "swis.h"

#include "Global/HALDevice.h"
#include "Global/Services.h"
#include "Interface/MixerDevice.h"
27
#include "Interface/Sound.h"
28 29 30 31 32 33 34
#include "DebugLib/DebugLib.h"

#include "global.h"
#include "mess.h"
#include "modhdr.h"
#include "resmess.h"

35
#define LAST_KNOWN_DEVICE_MAJOR_VERSION 0
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

typedef struct system
{
  struct system *next;
  uint32_t number;
  struct mixer_device *device;
}
system_t;

static _kernel_oserror *GetInitialDevices(void);
static void RemoveAllDevices(void);
static void AddDevice(struct mixer_device *device);
static void RemoveDevice(struct mixer_device *device);
static void MixVolumeCommand(const char *arg_string, int argc);
static _kernel_oserror *ExamineMixerSWI(_kernel_swi_regs *r);
static _kernel_oserror *SetMixSWI(_kernel_swi_regs *r);
static _kernel_oserror *GetMixSWI(_kernel_swi_regs *r);
53 54 55 56
static _kernel_oserror *FindChannel(system_t *system, int32_t category, uint32_t index, uint32_t *channel);
static _kernel_oserror *FindSystemBlockByNumber(uint32_t system_num, system_t **system);
static _kernel_oserror *FindSystemBlockByDevice(struct mixer_device *device, system_t **system);
static _kernel_oserror *FindSystemBlockForSWI(int r0, system_t **system);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

static system_t *static_SystemList = NULL;


_kernel_oserror *module_Init(const char *cmd_tail, int podule_base, void *pw)
{
  _kernel_oserror *e = NULL;
#ifndef ROM_MODULE
  bool FileRegistered = false;
#endif
  bool MessagesOpen = false;
  bool DevicesEnumerated = false;
  IGNORE(cmd_tail);
  IGNORE(podule_base);
  IGNORE(pw);

  debug_initialise("SoundCtrl", "null:", "");
  debug_atexit();
  debug_set_taskname_prefix(false);
  debug_set_area_level_prefix(true);
  debug_set_area_pad_limit(0);
78
  debug_set_device(PRINTF_OUTPUT);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  debug_set_raw_device(NULL_OUTPUT);
  debug_set_trace_device(NULL_OUTPUT);

  if (!e)
  {

#ifndef ROM_MODULE
    e = _swix(ResourceFS_RegisterFiles, _IN(0),
              resmess_ResourcesFiles());
  }
  if (!e)
  {
    FileRegistered = true;
#endif

    e = _swix(MessageTrans_OpenFile, _INR(0,2),
              &global_MessageFD,
              Module_MessagesFile,
              0);
  }
  if (!e)
  {
    MessagesOpen = true;

    e = GetInitialDevices();
  }
  if (!e)
  {
    DevicesEnumerated = true;

    /* Add other initialisation here */
  }

  if (e && DevicesEnumerated) RemoveAllDevices();
  if (e && MessagesOpen) _swix(MessageTrans_CloseFile, _IN(0),
                               &global_MessageFD);
#ifndef ROM_MODULE
  if (e && FileRegistered) _swix(ResourceFS_DeregisterFiles, _IN(0),
                                 resmess_ResourcesFiles());
#endif
  return e;
}


_kernel_oserror *module_Final(int fatal, int podule, void *pw)
{
  _kernel_oserror *e = NULL;
  IGNORE(fatal);
  IGNORE(podule);
  IGNORE(pw);

  if (!e)
  {
    RemoveAllDevices();
    _swix(MessageTrans_CloseFile, _IN(0),
          &global_MessageFD);
#ifndef ROM_MODULE
    _swix(ResourceFS_DeregisterFiles, _IN(0),
          resmess_ResourcesFiles());
#endif
  }
  return e;
}


void module_Service(int service_number, _kernel_swi_regs *r, void *pw)
{
  IGNORE(pw);
  switch (service_number)
  {
    case Service_Hardware:;
      struct mixer_device *device = (struct mixer_device *) r->r[2];
      switch (r->r[0] & 0xFF)
      {
        case 0:
154
          if (!((device->device.version>>16) > LAST_KNOWN_DEVICE_MAJOR_VERSION))
155 156 157 158 159 160
          {
            AddDevice(device);
          }
          break;

        case 1:
161
          if (!((device->device.version>>16) > LAST_KNOWN_DEVICE_MAJOR_VERSION))
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
          {
            RemoveDevice(device);
          }
          break;
      }
      break;
  }
  return;
}


_kernel_oserror *module_Commands(const char *arg_string, int argc, int cmd_no, void *pw)
{
  IGNORE(pw);
  switch(cmd_no)
  {
    case CMD_MixVolume:
      MixVolumeCommand(arg_string, argc);
      break;
  }
  return NULL;
}


_kernel_oserror *module_SWI(int swi_offset, _kernel_swi_regs *r, void *pw)
{
  _kernel_oserror *e;
  IGNORE(pw);
  switch(swi_offset)
  {
    case SoundCtrl_ExamineMixer - SoundCtrl_00:
      e = ExamineMixerSWI(r);
      break;

    case SoundCtrl_SetMix - SoundCtrl_00:
      e = SetMixSWI(r);
      break;

    case SoundCtrl_GetMix - SoundCtrl_00:
      e = GetMixSWI(r);
      break;

    default:
      e = mess_GenerateError("BadSWI", error_SOUNDCTRL_BAD_SWI, 1, "SoundControl");
      break;
  }
  return e;
}


static _kernel_oserror *GetInitialDevices(void)
{
  int32_t r1 = 0;
  system_t *system = NULL;

  do
  {
    _kernel_oserror *e;
    struct mixer_device *device;

    e = _swix(OS_Hardware, _INR(0,1)|_IN(8)|_OUTR(1,2),
223
              (LAST_KNOWN_DEVICE_MAJOR_VERSION << 16) | HALDeviceType_Audio | HALDeviceAudio_Mixer,
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
              r1,
              4,
              &r1,
              &device);
    if (!e && r1 == -1) break;
    if (!e)
    {
      system = malloc(sizeof *system);
      if (system == NULL) e = mess_GenerateError("NoMem", error_SOUNDCTRL_NO_MEM, 0);
    }
    if (e)
    {
      RemoveAllDevices();
      return e;
    }
    if (device->device.Activate(&device->device))
    {
      system->next = static_SystemList;
      system->device = device;
      static_SystemList = system;
    }
    else
    {
      free(system);
    }
  }
  while (true);

  /* Now number the devices in the reverse of enumeration order */
  /* so any motherboard mixer is guaranteed to get number 0 */
  uint32_t counter = 0;
  for (system = static_SystemList; system != NULL; system = system->next)
  {
    system->number = counter++;
  }
  return NULL;
}


static void RemoveAllDevices(void)
{
  system_t *system = static_SystemList;
  system_t *next;
  for (; system != NULL; system = next)
  {
    next = system->next;
    system->device->device.Deactivate(&system->device->device);
    free(system);
  }
  return;
}


static void AddDevice(struct mixer_device *device)
{
  system_t *system = malloc(sizeof *system);
  if (system == NULL) return;

282 283
  system->device = device;

284 285 286 287 288 289 290 291 292 293 294 295 296
  if (device->device.Activate(&device->device))
  {
    /* Seach for a gap in the list of system numbers; if a driver module was */
    /* *RMReInited, this approach should hopefully keep the system number the same */
    system_t *prev = NULL;
    system_t *next = static_SystemList;
    uint32_t expected_number = 0;

    while (next != NULL && next->number == expected_number++)
    {
      prev = next;
      next = next->next;
    }
297
    system->number = expected_number;
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    system->next = next;
    if (prev)
    {
      prev->next = system;
    }
    else
    {
      static_SystemList = system;
    }
  }
  else
  {
    free(system);
  }
  return;
}


static void RemoveDevice(struct mixer_device *device)
{
  system_t *system = static_SystemList;
  system_t *prev = NULL;
  for (; system != NULL; prev = system, system = system->next)
  {
    if (system->device == device)
    {
      if (prev)
      {
        prev->next = system->next;
      }
      else
      {
        static_SystemList = system->next;
      }
      system->device->device.Deactivate(&system->device->device);
      free(system);
      break;
    }
  }
  return;
}


341
static bool SoundDMA_ControllerSelection(void)
342 343 344 345 346
{
  uint32_t ret,flags;
  /* Check if device selection & enumeration functionality is available */
  if (_swix(Sound_ReadSysInfo, _IN(0)|_OUTR(0,1), Sound_RSI_Features, &ret, &flags))
  {
347
    dprintf(("", "ControllerSelection: SWI error\n"));
348 349
    return false;
  }
350
  if (ret || !(flags & Sound_RSI_Feature_ControllerSelection))
351
  {
352
    dprintf(("", "ControllerSelection: no\n"));
353 354
    return false;
  }
355
  dprintf(("", "ControllerSelection: yes\n"));
356 357 358 359
  return true;
}


360 361 362 363 364 365
static void MixVolumeCommand(const char *arg_string, int argc)
{
  int32_t category;
  uint32_t index;
  unsigned char mute;
  int32_t gain;
366
  system_t *system = NULL;
367
  uint32_t channel;
368

369
  if (SoundDMA_ControllerSelection())
370
  {
371
    /* Accept "0" or a controller ID */
372 373
    struct mixer_device *device;
    char temp[128];
374
    const char *controller_id;
Robert Sprowson's avatar
Robert Sprowson committed
375
    if ((arg_string[0] == '0') && isspace(arg_string[1]))
376 377
    {
      /* Get current device ID */
378
      if (_swix(Sound_ReadSysInfo, _INR(0,2), Sound_RSI_DefaultController, temp, sizeof(temp)))
379 380 381
      {
        return;
      }
382
      controller_id = temp;
383 384 385
    }
    else
    {
386
      controller_id = arg_string;
387
    }
388
    dprintf(("", "Looking for mixer for '%s'\n",controller_id));
389
    /* Get mixer from device ID */
390
    if (_swix(Sound_ControllerInfo, _INR(0,3), controller_id, &device, sizeof(device), Sound_CtlrInfo_MixerDevice))
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    {
      return;
    }
    FindSystemBlockByDevice(device, &system);
  }
  else
  {
    /* Old SoundDMA, accept mixer number (using internal numbering scheme) */
    FindSystemBlockByNumber(atoi(arg_string), &system);
  }

  if (!system)
  {
    return;
  }

  while (*arg_string && !isspace(*arg_string))
  {
    arg_string++;
  }

412 413
  if (argc == 4)
  {
414
    if (sscanf(arg_string, "%"SCNd32" %"SCNu32" %c", &category, &index, &mute) == 3)
415 416
    {
      mute -= '0';
417
      if (mute <= 1 && FindChannel(system, category, index, &channel) == 0)
418 419 420 421 422 423 424 425 426
      {
        struct mixer_device_channel_state state = system->device->GetMix(system->device, channel);
        state.mute = mute;
        system->device->SetMix(system->device, channel, state);
      }
    }
  }
  else /* argc == 5 */
  {
427
    if (sscanf(arg_string, "%"SCNd32" %"SCNu32" %c %"SCNd32, &category, &index, &mute, &gain) == 4)
428 429
    {
      mute -= '0';
430
      if (mute <= 1 && FindChannel(system, category, index, &channel) == 0)
431 432 433 434 435 436 437 438 439 440 441 442 443
      {
        struct mixer_device_channel_state state = { mute, gain };
        system->device->SetMix(system->device, channel, state);
      }
    }
  }
  return;
}


static _kernel_oserror *ExamineMixerSWI(_kernel_swi_regs *r)
{
  system_t *system;
444
  _kernel_oserror *e = FindSystemBlockForSWI((uint32_t) r->r[0], &system);
445 446 447
  if (e) return e;

  uint32_t nchannels = system->device->nchannels;
448 449 450 451 452
  uint32_t block_size = sizeof (struct mixer_device_channel_features);
  if (system->device->device.version > 0)
    block_size += sizeof (struct mixer_device_channel_limits);

  uint32_t to_do = r->r[2] / block_size;
453 454
  if (to_do > nchannels) to_do = nchannels;

455 456
  r->r[2] -= nchannels * block_size;
  r->r[3] = block_size;
457 458
  r->r[4] = to_do;

459
  uint8_t *ptr = (uint8_t *) r->r[1];
460 461
  for (uint32_t c = 0; c < to_do; c++)
  {
462 463 464 465 466 467 468
    *((struct mixer_device_channel_features *) ptr) = system->device->GetFeatures(system->device, c);
    ptr += sizeof(struct mixer_device_channel_features);
    if(system->device->device.version > 0)
    {
      system->device->GetMixLimits(system->device, c, (struct mixer_device_channel_limits *) ptr);
      ptr += sizeof(struct mixer_device_channel_limits);
    }
469 470 471 472 473 474 475 476 477 478
  }

  return e;
}


static _kernel_oserror *SetMixSWI(_kernel_swi_regs *r)
{
  system_t *system;
  uint32_t channel;
479 480 481 482
  _kernel_oserror *e = FindSystemBlockForSWI(r->r[0], &system);
  if (e) return e;

  e = FindChannel(system, (int32_t) r->r[1], (uint32_t) r->r[2], &channel);
483 484 485 486 487 488 489 490 491 492 493 494 495
  if (e) return e;

  struct mixer_device_channel_state state = { r->r[3], r->r[4] };
  system->device->SetMix(system->device, channel, state);

  return e;
}


static _kernel_oserror *GetMixSWI(_kernel_swi_regs *r)
{
  system_t *system;
  uint32_t channel;
496 497 498 499
  _kernel_oserror *e = FindSystemBlockForSWI(r->r[0], &system);
  if (e) return e;

  e = FindChannel(system, (int32_t) r->r[1], (uint32_t) r->r[2], &channel);
500 501 502 503 504 505 506 507 508 509
  if (e) return e;

  struct mixer_device_channel_state state = system->device->GetMix(system->device, channel);
  r->r[3] = (int) state.mute;
  r->r[4] = (int) state.gain;

  return e;
}


510
static _kernel_oserror *FindChannel(system_t *system, int32_t category, uint32_t index, uint32_t *channel)
511 512
{
  uint32_t c = 0;
513
  uint32_t nchannels = system->device->nchannels;
514 515 516
  struct mixer_device_channel_features features;
  for (; c < nchannels; c++)
  {
517
    features = system->device->GetFeatures(system->device, c);
518 519 520 521 522 523 524 525 526 527
    if (features.category == category && index-- == 0) /* note second isn't performed if first test fails */
    {
      *channel = c;
      return NULL;
    }
  }
  return mess_MakeError(error_SOUNDCTRL_BAD_CHANNEL, 0);
}


528
static _kernel_oserror *FindSystemBlockByNumber(uint32_t system_num, system_t **system)
529
{
530
  dprintf(("","FindSystemBlockByNumber: %u\n",system_num));
531 532 533 534 535 536 537
  system_t *s = static_SystemList;
  for (; s != NULL; s = s->next)
  {
    if (s->number == system_num) break;
  }
  if (s)
  {
538
    dprintf(("","Found\n"));
539 540 541 542 543 544 545 546
    *system = s;
    return NULL;
  }
  else
  {
    return mess_MakeError(error_SOUNDCTRL_BAD_MIXER, 0);
  }
}
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571


static _kernel_oserror *FindSystemBlockByDevice(struct mixer_device *device, system_t **system)
{
  dprintf(("","FindSystemBlockByDevice: %x\n",device));
  system_t *s = static_SystemList;
  for (; s != NULL; s = s->next)
  {
    if (s->device == device) break;
  }
  if (s)
  {
    dprintf(("","Found\n"));
    *system = s;
    return NULL;
  }
  else
  {
    return mess_MakeError(error_SOUNDCTRL_BAD_MIXER, 0);
  }
}

static _kernel_oserror *FindSystemBlockForSWI(int r0, system_t **system)
{
  _kernel_oserror *e;
572
  if (SoundDMA_ControllerSelection())
573 574 575 576
  {
    /* Accept NULL or a string pointer */
    struct mixer_device *device;
    char temp[128];
577
    char *controller_id;
578 579
    if (!r0)
    {
580 581
      /* Get current controller ID */
      if ((e = _swix(Sound_ReadSysInfo, _INR(0,2), Sound_RSI_DefaultController, temp, sizeof(temp))) != NULL)
582 583 584
      {
        return e;
      }
585
      controller_id = temp;
586 587 588
    }
    else
    {
589
      controller_id = (char *) r0;
590
    }
591 592 593
    dprintf(("", "Looking for mixer for '%s'\n",controller_id));
    /* Get mixer from controller ID */
    if ((e = _swix(Sound_ControllerInfo, _INR(0,3), controller_id, &device, sizeof(device), Sound_CtlrInfo_MixerDevice)) != NULL)
594 595 596 597 598 599 600 601 602 603 604
    {
      return e;
    }
    return FindSystemBlockByDevice(device, system);
  }
  else
  {
    /* Old SoundDMA, accept mixer number (using internal numbering scheme) */
    return FindSystemBlockByNumber(r0, system);
  }
}