// SPDX-License-Identifier: GPL-2.0-or-later /* * Low-level ALSA driver for the ENSONIQ SoundScape * Copyright (c) by Chris Rankin * * This driver was written in part using information obtained from * the OSS/Free SoundScape driver, written by Hannu Savolainen.
*/
/* * Allocates some kernel memory that we can use for DMA. * I think this means that the memory has to map to * contiguous pages of physical memory.
*/ staticstruct snd_dma_buffer *get_dmabuf(struct soundscape *s, struct snd_dma_buffer *buf, unsignedlong size)
{ if (buf) { if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
s->chip->card->dev,
size, buf) < 0) {
dev_err(s->dev, "sscape: Failed to allocate %lu bytes for DMA\n",
size); return NULL;
}
}
return buf;
}
/* * Release the DMA-able kernel memory ...
*/ staticvoid free_dmabuf(struct snd_dma_buffer *buf)
{ if (buf && buf->area)
snd_dma_free_pages(buf);
}
/* * This function writes to the SoundScape's control registers, * but doesn't do any locking. It's up to the caller to do that. * This is why this function is "unsafe" ...
*/ staticinlinevoid sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsignedchar val)
{
outb(reg, ODIE_ADDR_IO(io_base));
outb(val, ODIE_DATA_IO(io_base));
}
/* * Write to the SoundScape's control registers, and do the * necessary locking ...
*/ staticvoid sscape_write(struct soundscape *s, enum GA_REG reg, unsignedchar val)
{ unsignedlong flags;
/* * Read from the SoundScape's control registers, but leave any * locking to the caller. This is why the function is "unsafe" ...
*/ staticinlineunsignedchar sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
{
outb(reg, ODIE_ADDR_IO(io_base)); return inb(ODIE_DATA_IO(io_base));
}
/* * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
*/ staticinlinevoid set_host_mode_unsafe(unsigned io_base)
{
outb(0x0, HOST_CTRL_IO(io_base));
}
/* * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
*/ staticinlinevoid set_midi_mode_unsafe(unsigned io_base)
{
outb(0x3, HOST_CTRL_IO(io_base));
}
/* * Read the SoundScape's host-mode control register, but leave * any locking issues to the caller ...
*/ staticinlineint host_read_unsafe(unsigned io_base)
{ int data = -1; if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0)
data = inb(HOST_DATA_IO(io_base));
return data;
}
/* * Read the SoundScape's host-mode control register, performing * a limited amount of busy-waiting if the register isn't ready. * Also leaves all locking-issues to the caller ...
*/ staticint host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
{ int data;
while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
udelay(100);
--timeout;
} /* while */
return data;
}
/* * Write to the SoundScape's host-mode control registers, but * leave any locking issues to the caller ...
*/ staticinlineint host_write_unsafe(unsigned io_base, unsignedchar data)
{ if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
outb(data, HOST_DATA_IO(io_base)); return 1;
}
return 0;
}
/* * Write to the SoundScape's host-mode control registers, performing * a limited amount of busy-waiting if the register isn't ready. * Also leaves all locking-issues to the caller ...
*/ staticint host_write_ctrl_unsafe(unsigned io_base, unsignedchar data, unsigned timeout)
{ int err;
while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
udelay(100);
--timeout;
} /* while */
return err;
}
/* * Check that the MIDI subsystem is operational. If it isn't, * then we will hang the computer if we try to use it ... * * NOTE: This check is based upon observation, not documentation.
*/ staticinlineint verify_mpu401(conststruct snd_mpu401 *mpu)
{ return ((inb(MPU401C(mpu)) & 0xc0) == 0x80);
}
/* * This is apparently the standard way to initialise an MPU-401
*/ staticinlinevoid initialise_mpu401(conststruct snd_mpu401 *mpu)
{
outb(0, MPU401D(mpu));
}
/* * Tell the SoundScape to activate the AD1845 chip (I think). * The AD1845 detection fails if we *don't* do this, so I * think that this is a good idea ...
*/ staticvoid activate_ad1845_unsafe(unsigned io_base)
{ unsignedchar val = sscape_read_unsafe(io_base, GA_HMCTL_REG);
sscape_write_unsafe(io_base, GA_HMCTL_REG, (val & 0xcf) | 0x10);
sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
}
/* * Tell the SoundScape to begin a DMA transfer using the given channel. * All locking issues are left to the caller.
*/ staticvoid sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
{
sscape_write_unsafe(io_base, reg,
sscape_read_unsafe(io_base, reg) | 0x01);
sscape_write_unsafe(io_base, reg,
sscape_read_unsafe(io_base, reg) & 0xfe);
}
/* * Wait for a DMA transfer to complete. This is a "limited busy-wait", * and all locking issues are left to the caller.
*/ staticint sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
{ while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
udelay(100);
--timeout;
} /* while */
return sscape_read_unsafe(io_base, reg) & 0x01;
}
/* * Wait for the On-Board Processor to return its start-up * acknowledgement sequence. This wait is too long for * us to perform "busy-waiting", and so we must sleep. * This in turn means that we must not be holding any * spinlocks when we call this function.
*/ staticint obp_startup_ack(struct soundscape *s, unsigned timeout)
{ unsignedlong end_time = jiffies + msecs_to_jiffies(timeout);
do { unsignedlong flags; int x;
spin_lock_irqsave(&s->lock, flags);
x = host_read_unsafe(s->io_base);
spin_unlock_irqrestore(&s->lock, flags); if (x == 0xfe || x == 0xff) return 1;
msleep(10);
} while (time_before(jiffies, end_time));
return 0;
}
/* * Wait for the host to return its start-up acknowledgement * sequence. This wait is too long for us to perform * "busy-waiting", and so we must sleep. This in turn means * that we must not be holding any spinlocks when we call * this function.
*/ staticint host_startup_ack(struct soundscape *s, unsigned timeout)
{ unsignedlong end_time = jiffies + msecs_to_jiffies(timeout);
do { unsignedlong flags; int x;
spin_lock_irqsave(&s->lock, flags);
x = host_read_unsafe(s->io_base);
spin_unlock_irqrestore(&s->lock, flags); if (x == 0xfe) return 1;
msleep(10);
} while (time_before(jiffies, end_time));
return 0;
}
/* * Upload a byte-stream into the SoundScape using DMA channel A.
*/ staticint upload_dma_data(struct soundscape *s, constunsignedchar *data,
size_t size)
{ unsignedlong flags; struct snd_dma_buffer dma; int ret; unsignedchar val;
if (!get_dmabuf(s, &dma, PAGE_ALIGN(32 * 1024))) return -ENOMEM;
spin_lock_irqsave(&s->lock, flags);
/* * Reset the board ...
*/
val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val & 0x3f);
/* * Enable the DMA channels and configure them ...
*/
val = (s->chip->dma1 << 4) | DMA_8BIT;
sscape_write_unsafe(s->io_base, GA_DMAA_REG, val);
sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
/* * Take the board out of reset ...
*/
val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val | 0x80);
/* * Upload the firmware to the SoundScape * board through the DMA channel ...
*/ while (size != 0) { unsignedlong len;
len = min(size, dma.bytes);
memcpy(dma.area, data, len);
data += len;
size -= len;
snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG); if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) { /* * Don't forget to release this spinlock we're holding
*/
spin_unlock_irqrestore(&s->lock, flags);
dev_err(s->dev, "sscape: DMA upload has timed out\n");
ret = -EAGAIN; goto _release_dma;
}
} /* while */
/* * Boot the board ... (I think)
*/
val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val | 0x40);
spin_unlock_irqrestore(&s->lock, flags);
/* * If all has gone well, then the board should acknowledge * the new upload and tell us that it has rebooted OK. We * give it 5 seconds (max) ...
*/
ret = 0; if (!obp_startup_ack(s, 5000)) {
dev_err(s->dev, "sscape: No response from on-board processor after upload\n");
ret = -EAGAIN;
} elseif (!host_startup_ack(s, 5000)) {
dev_err(s->dev, "sscape: SoundScape failed to initialise\n");
ret = -EAGAIN;
}
_release_dma: /* * NOTE!!! We are NOT holding any spinlocks at this point !!!
*/
sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_OPUS ? 0x40 : 0x70));
free_dmabuf(&dma);
return ret;
}
/* * Upload the bootblock(?) into the SoundScape. The only * purpose of this block of code seems to be to tell * us which version of the microcode we should be using.
*/ staticint sscape_upload_bootblock(struct snd_card *card)
{ struct soundscape *sscape = get_card_soundscape(card); unsignedlong flags; conststruct firmware *init_fw = NULL; int data = 0; int ret;
ret = request_firmware(&init_fw, "scope.cod", card->dev); if (ret < 0) {
dev_err(card->dev, "sscape: Error loading scope.cod"); return ret;
}
ret = upload_dma_data(sscape, init_fw->data, init_fw->size);
release_firmware(init_fw);
spin_lock_irqsave(&sscape->lock, flags); if (ret == 0)
data = host_read_ctrl_unsafe(sscape->io_base, 100);
if (data & 0x10)
sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2f);
spin_unlock_irqrestore(&sscape->lock, flags);
data &= 0xf; if (ret == 0 && data > 7) {
dev_err(card->dev, "sscape: timeout reading firmware version\n");
ret = -EAGAIN;
}
return (ret == 0) ? data : ret;
}
/* * Upload the microcode into the SoundScape.
*/ staticint sscape_upload_microcode(struct snd_card *card, int version)
{ struct soundscape *sscape = get_card_soundscape(card); conststruct firmware *init_fw = NULL; char name[14]; int err;
new_val = uctl->value.integer.value[0] & 127; /* * We need to put the board into HOST mode before we * can send any volume-changing HOST commands ...
*/
set_host_mode_unsafe(s->io_base);
/* * To successfully change the MIDI volume setting, you seem to * have to write a volume command, write the new volume value, * and then perform another volume-related command. Perhaps the * first command is an "open" and the second command is a "close"?
*/ if (s->midi_vol == new_val) {
change = 0; goto __skip_change;
}
change = host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
&& host_write_ctrl_unsafe(s->io_base, new_val, 100)
&& host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100)
&& host_write_ctrl_unsafe(s->io_base, new_val, 100);
s->midi_vol = new_val;
__skip_change:
/* * Take the board out of HOST mode and back into MIDI mode ...
*/
set_midi_mode_unsafe(s->io_base);
/* * The SoundScape can use two IRQs from a possible set of four. * These IRQs are encoded as bit patterns so that they can be * written to the control registers.
*/ staticunsigned get_irq_config(int sscape_type, int irq)
{ staticconstint valid_irq[] = { 9, 5, 7, 10 }; staticconstint old_irq[] = { 9, 7, 5, 15 }; unsigned cfg;
if (sscape_type == MEDIA_FX) { for (cfg = 0; cfg < ARRAY_SIZE(old_irq); ++cfg) if (irq == old_irq[cfg]) return cfg;
} else { for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) if (irq == valid_irq[cfg]) return cfg;
}
return INVALID_IRQ;
}
/* * Perform certain arcane port-checks to see whether there * is a SoundScape board lurking behind the given ports.
*/ staticint detect_sscape(struct soundscape *s, long wss_io)
{ unsignedlong flags; unsigned d; int retval = 0;
spin_lock_irqsave(&s->lock, flags);
/* * The following code is lifted from the original OSS driver, * and as I don't have a datasheet I cannot really comment * on what it is doing...
*/ if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0) goto _done;
d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0; if ((d & 0x80) != 0) goto _done;
/* * ALSA callback function, called when attempting to open the MIDI device. * Check that the MIDI firmware has been loaded, because we don't want * to crash the machine. Also check that someone isn't using the hardware * IOCTL device.
*/ staticint mpu401_open(struct snd_mpu401 *mpu)
{ if (!verify_mpu401(mpu)) {
dev_err(mpu->rmidi->card->dev, "sscape: MIDI disabled, please load firmware\n"); return -ENODEV;
}
return 0;
}
/* * Initialise an MPU-401 subdevice for MIDI support on the SoundScape.
*/ staticint create_mpu401(struct snd_card *card, int devnum, unsignedlong port, int irq)
{ struct soundscape *sscape = get_card_soundscape(card); struct snd_rawmidi *rawmidi; int err;
/* * Create an AD1845 PCM subdevice on the SoundScape. The AD1845 * is very much like a CS4231, with a few extra bits. We will * try to support at least some of the extra bits by overriding * some of the CS4231 callback.
*/ staticint create_ad1845(struct snd_card *card, unsigned port, int irq, int dma1, int dma2)
{ registerstruct soundscape *sscape = get_card_soundscape(card); struct snd_wss *chip; int err; int codec_type = WSS_HW_DETECT;
switch (sscape->type) { case MEDIA_FX: case SSCAPE: /* * There are some freak examples of early Soundscape cards * with CS4231 instead of AD1848/CS4248. Unfortunately, the * CS4231 works only in CS4248 compatibility mode on * these cards so force it.
*/ if (sscape->ic_type != IC_OPUS)
codec_type = WSS_HW_AD1848; break;
case SSCAPE_VIVO:
port += 4; break; default: break;
}
if (sscape->type != SSCAPE_VIVO) { /* * The input clock frequency on the SoundScape must * be 14.31818 MHz, because we must set this register * to get the playback to sound correct ...
*/
snd_wss_mce_up(chip);
spin_lock_irqsave(&chip->reg_lock, flags);
snd_wss_out(chip, AD1845_CLOCK, 0x20);
spin_unlock_irqrestore(&chip->reg_lock, flags);
snd_wss_mce_down(chip);
}
err = snd_wss_pcm(chip, 0); if (err < 0) {
dev_err(card->dev, "sscape: No PCM device for AD1845 chip\n"); goto _error;
}
err = snd_wss_mixer(chip); if (err < 0) {
dev_err(card->dev, "sscape: No mixer device for AD1845 chip\n"); goto _error;
} if (chip->hardware != WSS_HW_AD1848) {
err = snd_wss_timer(chip, 0); if (err < 0) {
dev_err(card->dev, "sscape: No timer device for AD1845 chip\n"); goto _error;
}
}
if (sscape->type != SSCAPE_VIVO) {
err = snd_ctl_add(card,
snd_ctl_new1(&midi_mixer_ctl, chip)); if (err < 0) {
dev_err(card->dev, "sscape: Could not create MIDI mixer control\n"); goto _error;
}
}
sscape->chip = chip;
}
_error: return err;
}
/* * Create an ALSA soundcard entry for the SoundScape, using * the given list of port, IRQ and DMA resources.
*/ staticint create_sscape(int dev, struct snd_card *card)
{ struct soundscape *sscape = get_card_soundscape(card); unsigned dma_cfg; unsigned irq_cfg; unsigned mpu_irq_cfg; struct resource *io_res; struct resource *wss_res; unsignedlong flags; int err; int val; constchar *name;
/* * Grab IO ports that we will need to probe so that we * can detect and control this hardware ...
*/
io_res = devm_request_region(card->dev, port[dev], 8, "SoundScape"); if (!io_res) {
dev_err(card->dev, "sscape: can't grab port 0x%lx\n", port[dev]); return -EBUSY;
}
wss_res = NULL; if (sscape->type == SSCAPE_VIVO) {
wss_res = devm_request_region(card->dev, wss_port[dev], 4, "SoundScape"); if (!wss_res) {
dev_err(card->dev, "sscape: can't grab port 0x%lx\n",
wss_port[dev]); return -EBUSY;
}
}
if (!detect_sscape(sscape, wss_port[dev])) {
dev_err(card->dev, "sscape: hardware not detected at 0x%x\n",
sscape->io_base); return -ENODEV;
}
switch (sscape->type) { case MEDIA_FX:
name = "MediaFX/SoundFX"; break; case SSCAPE:
name = "Soundscape"; break; case SSCAPE_PNP:
name = "Soundscape PnP"; break; case SSCAPE_VIVO:
name = "Soundscape VIVO"; break; default:
name = "unknown Soundscape"; break;
}
dev_info(card->dev, "sscape: %s card detected at 0x%x, using IRQ %d, DMA %d\n",
name, sscape->io_base, irq[dev], dma[dev]);
/* * Check that the user didn't pass us garbage data ...
*/
irq_cfg = get_irq_config(sscape->type, irq[dev]); if (irq_cfg == INVALID_IRQ) {
dev_err(card->dev, "sscape: Invalid IRQ %d\n", irq[dev]); return -ENXIO;
}
/* * Tell the on-board devices where their resources are (I think - * I can't be sure without a datasheet ... So many magic values!)
*/
spin_lock_irqsave(&sscape->lock, flags);
staticint snd_sscape_match(struct device *pdev, unsignedint i)
{ /* * Make sure we were given ALL of the other parameters.
*/ if (port[i] == SNDRV_AUTO_PORT) return 0;
if (irq[i] == SNDRV_AUTO_IRQ ||
mpu_irq[i] == SNDRV_AUTO_IRQ ||
dma[i] == SNDRV_AUTO_DMA) {
dev_info(pdev, "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n"); return 0;
}
/* * Allow this function to fail *quietly* if all the ISA PnP * devices were configured using module parameters instead.
*/
idx = get_next_autoindex(idx); if (idx >= SNDRV_CARDS) return -ENOSPC;
/* * Check that we still have room for another sound card ...
*/
dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL); if (!dev) return -ENODEV;
if (!pnp_is_active(dev)) { if (pnp_activate_dev(dev) < 0) {
dev_info(&dev->dev, "sscape: device is inactive\n"); return -EBUSY;
}
}
/* * Create a new ALSA sound card entry, in anticipation * of detecting our hardware ...
*/
ret = snd_devm_card_new(&pcard->card->dev,
index[idx], id[idx], THIS_MODULE, sizeof(struct soundscape), &card); if (ret < 0) return ret;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.