python_code
stringlengths
0
1.8M
repo_name
stringclasses
7 values
file_path
stringlengths
5
99
// SPDX-License-Identifier: GPL-2.0-or-later /* * Digital Audio (PCM) abstract layer * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/io.h> #include <linux/time.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/moduleparam.h> #include <linux/vmalloc.h> #include <linux/export.h> #include <sound/core.h> #include <sound/pcm.h> #include <sound/info.h> #include <sound/initval.h> #include "pcm_local.h" static int preallocate_dma = 1; module_param(preallocate_dma, int, 0444); MODULE_PARM_DESC(preallocate_dma, "Preallocate DMA memory when the PCM devices are initialized."); static int maximum_substreams = 4; module_param(maximum_substreams, int, 0444); MODULE_PARM_DESC(maximum_substreams, "Maximum substreams with preallocated DMA memory."); static const size_t snd_minimum_buffer = 16384; static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL; module_param(max_alloc_per_card, ulong, 0644); MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card."); static void __update_allocated_size(struct snd_card *card, ssize_t bytes) { card->total_pcm_alloc_bytes += bytes; } static void update_allocated_size(struct snd_card *card, ssize_t bytes) { mutex_lock(&card->memory_mutex); __update_allocated_size(card, bytes); mutex_unlock(&card->memory_mutex); } static void decrease_allocated_size(struct snd_card *card, size_t bytes) { mutex_lock(&card->memory_mutex); WARN_ON(card->total_pcm_alloc_bytes < bytes); __update_allocated_size(card, -(ssize_t)bytes); mutex_unlock(&card->memory_mutex); } static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, int str, size_t size, struct snd_dma_buffer *dmab) { enum dma_data_direction dir; int err; /* check and reserve the requested size */ mutex_lock(&card->memory_mutex); if (max_alloc_per_card && card->total_pcm_alloc_bytes + size > max_alloc_per_card) { mutex_unlock(&card->memory_mutex); return -ENOMEM; } __update_allocated_size(card, size); mutex_unlock(&card->memory_mutex); if (str == SNDRV_PCM_STREAM_PLAYBACK) dir = DMA_TO_DEVICE; else dir = DMA_FROM_DEVICE; err = snd_dma_alloc_dir_pages(type, dev, dir, size, dmab); if (!err) { /* the actual allocation size might be bigger than requested, * and we need to correct the account */ if (dmab->bytes != size) update_allocated_size(card, dmab->bytes - size); } else { /* take back on allocation failure */ decrease_allocated_size(card, size); } return err; } static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab) { if (!dmab->area) return; decrease_allocated_size(card, dmab->bytes); snd_dma_free_pages(dmab); dmab->area = NULL; } /* * try to allocate as the large pages as possible. * stores the resultant memory size in *res_size. * * the minimum size is snd_minimum_buffer. it should be power of 2. */ static int preallocate_pcm_pages(struct snd_pcm_substream *substream, size_t size, bool no_fallback) { struct snd_dma_buffer *dmab = &substream->dma_buffer; struct snd_card *card = substream->pcm->card; size_t orig_size = size; int err; do { err = do_alloc_pages(card, dmab->dev.type, dmab->dev.dev, substream->stream, size, dmab); if (err != -ENOMEM) return err; if (no_fallback) break; size >>= 1; } while (size >= snd_minimum_buffer); dmab->bytes = 0; /* tell error */ pr_warn("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n", substream->pcm->card->number, substream->pcm->device, substream->stream ? 'c' : 'p', substream->number, substream->pcm->name, orig_size); return -ENOMEM; } /** * snd_pcm_lib_preallocate_free - release the preallocated buffer of the specified substream. * @substream: the pcm substream instance * * Releases the pre-allocated buffer of the given substream. */ void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream) { do_free_pages(substream->pcm->card, &substream->dma_buffer); } /** * snd_pcm_lib_preallocate_free_for_all - release all pre-allocated buffers on the pcm * @pcm: the pcm instance * * Releases all the pre-allocated buffers on the given pcm. */ void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm) { struct snd_pcm_substream *substream; int stream; for_each_pcm_substream(pcm, stream, substream) snd_pcm_lib_preallocate_free(substream); } EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all); #ifdef CONFIG_SND_VERBOSE_PROCFS /* * read callback for prealloc proc file * * prints the current allocated size in kB. */ static void snd_pcm_lib_preallocate_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_pcm_substream *substream = entry->private_data; snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_buffer.bytes / 1024); } /* * read callback for prealloc_max proc file * * prints the maximum allowed size in kB. */ static void snd_pcm_lib_preallocate_max_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_pcm_substream *substream = entry->private_data; snd_iprintf(buffer, "%lu\n", (unsigned long) substream->dma_max / 1024); } /* * write callback for prealloc proc file * * accepts the preallocation size in kB. */ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_pcm_substream *substream = entry->private_data; struct snd_card *card = substream->pcm->card; char line[64], str[64]; size_t size; struct snd_dma_buffer new_dmab; mutex_lock(&substream->pcm->open_mutex); if (substream->runtime) { buffer->error = -EBUSY; goto unlock; } if (!snd_info_get_line(buffer, line, sizeof(line))) { snd_info_get_str(str, line, sizeof(str)); size = simple_strtoul(str, NULL, 10) * 1024; if ((size != 0 && size < 8192) || size > substream->dma_max) { buffer->error = -EINVAL; goto unlock; } if (substream->dma_buffer.bytes == size) goto unlock; memset(&new_dmab, 0, sizeof(new_dmab)); new_dmab.dev = substream->dma_buffer.dev; if (size > 0) { if (do_alloc_pages(card, substream->dma_buffer.dev.type, substream->dma_buffer.dev.dev, substream->stream, size, &new_dmab) < 0) { buffer->error = -ENOMEM; pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n", substream->pcm->card->number, substream->pcm->device, substream->stream ? 'c' : 'p', substream->number, substream->pcm->name, size); goto unlock; } substream->buffer_bytes_max = size; } else { substream->buffer_bytes_max = UINT_MAX; } if (substream->dma_buffer.area) do_free_pages(card, &substream->dma_buffer); substream->dma_buffer = new_dmab; } else { buffer->error = -EINVAL; } unlock: mutex_unlock(&substream->pcm->open_mutex); } static inline void preallocate_info_init(struct snd_pcm_substream *substream) { struct snd_info_entry *entry; entry = snd_info_create_card_entry(substream->pcm->card, "prealloc", substream->proc_root); if (entry) { snd_info_set_text_ops(entry, substream, snd_pcm_lib_preallocate_proc_read); entry->c.text.write = snd_pcm_lib_preallocate_proc_write; entry->mode |= 0200; } entry = snd_info_create_card_entry(substream->pcm->card, "prealloc_max", substream->proc_root); if (entry) snd_info_set_text_ops(entry, substream, snd_pcm_lib_preallocate_max_proc_read); } #else /* !CONFIG_SND_VERBOSE_PROCFS */ static inline void preallocate_info_init(struct snd_pcm_substream *substream) { } #endif /* CONFIG_SND_VERBOSE_PROCFS */ /* * pre-allocate the buffer and create a proc file for the substream */ static int preallocate_pages(struct snd_pcm_substream *substream, int type, struct device *data, size_t size, size_t max, bool managed) { int err; if (snd_BUG_ON(substream->dma_buffer.dev.type)) return -EINVAL; substream->dma_buffer.dev.type = type; substream->dma_buffer.dev.dev = data; if (size > 0) { if (!max) { /* no fallback, only also inform -ENOMEM */ err = preallocate_pcm_pages(substream, size, true); if (err < 0) return err; } else if (preallocate_dma && substream->number < maximum_substreams) { err = preallocate_pcm_pages(substream, size, false); if (err < 0 && err != -ENOMEM) return err; } } if (substream->dma_buffer.bytes > 0) substream->buffer_bytes_max = substream->dma_buffer.bytes; substream->dma_max = max; if (max > 0) preallocate_info_init(substream); if (managed) substream->managed_buffer_alloc = 1; return 0; } static int preallocate_pages_for_all(struct snd_pcm *pcm, int type, void *data, size_t size, size_t max, bool managed) { struct snd_pcm_substream *substream; int stream, err; for_each_pcm_substream(pcm, stream, substream) { err = preallocate_pages(substream, type, data, size, max, managed); if (err < 0) return err; } return 0; } /** * snd_pcm_lib_preallocate_pages - pre-allocation for the given DMA type * @substream: the pcm substream instance * @type: DMA type (SNDRV_DMA_TYPE_*) * @data: DMA type dependent data * @size: the requested pre-allocation size in bytes * @max: the max. allowed pre-allocation size * * Do pre-allocation for the given DMA buffer type. */ void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream, int type, struct device *data, size_t size, size_t max) { preallocate_pages(substream, type, data, size, max, false); } EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages); /** * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continuous memory type (all substreams) * @pcm: the pcm instance * @type: DMA type (SNDRV_DMA_TYPE_*) * @data: DMA type dependent data * @size: the requested pre-allocation size in bytes * @max: the max. allowed pre-allocation size * * Do pre-allocation to all substreams of the given pcm for the * specified DMA type. */ void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm, int type, void *data, size_t size, size_t max) { preallocate_pages_for_all(pcm, type, data, size, max, false); } EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all); /** * snd_pcm_set_managed_buffer - set up buffer management for a substream * @substream: the pcm substream instance * @type: DMA type (SNDRV_DMA_TYPE_*) * @data: DMA type dependent data * @size: the requested pre-allocation size in bytes * @max: the max. allowed pre-allocation size * * Do pre-allocation for the given DMA buffer type, and set the managed * buffer allocation mode to the given substream. * In this mode, PCM core will allocate a buffer automatically before PCM * hw_params ops call, and release the buffer after PCM hw_free ops call * as well, so that the driver doesn't need to invoke the allocation and * the release explicitly in its callback. * When a buffer is actually allocated before the PCM hw_params call, it * turns on the runtime buffer_changed flag for drivers changing their h/w * parameters accordingly. * * When @size is non-zero and @max is zero, this tries to allocate for only * the exact buffer size without fallback, and may return -ENOMEM. * Otherwise, the function tries to allocate smaller chunks if the allocation * fails. This is the behavior of snd_pcm_set_fixed_buffer(). * * When both @size and @max are zero, the function only sets up the buffer * for later dynamic allocations. It's used typically for buffers with * SNDRV_DMA_TYPE_VMALLOC type. * * Upon successful buffer allocation and setup, the function returns 0. * * Return: zero if successful, or a negative error code */ int snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type, struct device *data, size_t size, size_t max) { return preallocate_pages(substream, type, data, size, max, true); } EXPORT_SYMBOL(snd_pcm_set_managed_buffer); /** * snd_pcm_set_managed_buffer_all - set up buffer management for all substreams * for all substreams * @pcm: the pcm instance * @type: DMA type (SNDRV_DMA_TYPE_*) * @data: DMA type dependent data * @size: the requested pre-allocation size in bytes * @max: the max. allowed pre-allocation size * * Do pre-allocation to all substreams of the given pcm for the specified DMA * type and size, and set the managed_buffer_alloc flag to each substream. * * Return: zero if successful, or a negative error code */ int snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type, struct device *data, size_t size, size_t max) { return preallocate_pages_for_all(pcm, type, data, size, max, true); } EXPORT_SYMBOL(snd_pcm_set_managed_buffer_all); /** * snd_pcm_lib_malloc_pages - allocate the DMA buffer * @substream: the substream to allocate the DMA buffer to * @size: the requested buffer size in bytes * * Allocates the DMA buffer on the BUS type given earlier to * snd_pcm_lib_preallocate_xxx_pages(). * * Return: 1 if the buffer is changed, 0 if not changed, or a negative * code on failure. */ int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size) { struct snd_card *card; struct snd_pcm_runtime *runtime; struct snd_dma_buffer *dmab = NULL; if (PCM_RUNTIME_CHECK(substream)) return -EINVAL; if (snd_BUG_ON(substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_UNKNOWN)) return -EINVAL; runtime = substream->runtime; card = substream->pcm->card; if (runtime->dma_buffer_p) { /* perphaps, we might free the large DMA memory region to save some space here, but the actual solution costs us less time */ if (runtime->dma_buffer_p->bytes >= size) { runtime->dma_bytes = size; return 0; /* ok, do not change */ } snd_pcm_lib_free_pages(substream); } if (substream->dma_buffer.area != NULL && substream->dma_buffer.bytes >= size) { dmab = &substream->dma_buffer; /* use the pre-allocated buffer */ } else { /* dma_max=0 means the fixed size preallocation */ if (substream->dma_buffer.area && !substream->dma_max) return -ENOMEM; dmab = kzalloc(sizeof(*dmab), GFP_KERNEL); if (! dmab) return -ENOMEM; dmab->dev = substream->dma_buffer.dev; if (do_alloc_pages(card, substream->dma_buffer.dev.type, substream->dma_buffer.dev.dev, substream->stream, size, dmab) < 0) { kfree(dmab); pr_debug("ALSA pcmC%dD%d%c,%d:%s: cannot preallocate for size %zu\n", substream->pcm->card->number, substream->pcm->device, substream->stream ? 'c' : 'p', substream->number, substream->pcm->name, size); return -ENOMEM; } } snd_pcm_set_runtime_buffer(substream, dmab); runtime->dma_bytes = size; return 1; /* area was changed */ } EXPORT_SYMBOL(snd_pcm_lib_malloc_pages); /** * snd_pcm_lib_free_pages - release the allocated DMA buffer. * @substream: the substream to release the DMA buffer * * Releases the DMA buffer allocated via snd_pcm_lib_malloc_pages(). * * Return: Zero if successful, or a negative error code on failure. */ int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime; if (PCM_RUNTIME_CHECK(substream)) return -EINVAL; runtime = substream->runtime; if (runtime->dma_area == NULL) return 0; if (runtime->dma_buffer_p != &substream->dma_buffer) { struct snd_card *card = substream->pcm->card; /* it's a newly allocated buffer. release it now. */ do_free_pages(card, runtime->dma_buffer_p); kfree(runtime->dma_buffer_p); } snd_pcm_set_runtime_buffer(substream, NULL); return 0; } EXPORT_SYMBOL(snd_pcm_lib_free_pages); int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream, size_t size, gfp_t gfp_flags) { struct snd_pcm_runtime *runtime; if (PCM_RUNTIME_CHECK(substream)) return -EINVAL; runtime = substream->runtime; if (runtime->dma_area) { if (runtime->dma_bytes >= size) return 0; /* already large enough */ vfree(runtime->dma_area); } runtime->dma_area = __vmalloc(size, gfp_flags); if (!runtime->dma_area) return -ENOMEM; runtime->dma_bytes = size; return 1; } EXPORT_SYMBOL(_snd_pcm_lib_alloc_vmalloc_buffer); /** * snd_pcm_lib_free_vmalloc_buffer - free vmalloc buffer * @substream: the substream with a buffer allocated by * snd_pcm_lib_alloc_vmalloc_buffer() * * Return: Zero if successful, or a negative error code on failure. */ int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime; if (PCM_RUNTIME_CHECK(substream)) return -EINVAL; runtime = substream->runtime; vfree(runtime->dma_area); runtime->dma_area = NULL; return 0; } EXPORT_SYMBOL(snd_pcm_lib_free_vmalloc_buffer); /** * snd_pcm_lib_get_vmalloc_page - map vmalloc buffer offset to page struct * @substream: the substream with a buffer allocated by * snd_pcm_lib_alloc_vmalloc_buffer() * @offset: offset in the buffer * * This function is to be used as the page callback in the PCM ops. * * Return: The page struct, or %NULL on failure. */ struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream, unsigned long offset) { return vmalloc_to_page(substream->runtime->dma_area + offset); } EXPORT_SYMBOL(snd_pcm_lib_get_vmalloc_page);
linux-master
sound/core/pcm_memory.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer FIFO * Copyright (c) 1998 by Frank van de Pol <[email protected]> */ #include <sound/core.h> #include <linux/slab.h> #include <linux/sched/signal.h> #include "seq_fifo.h" #include "seq_lock.h" /* FIFO */ /* create new fifo */ struct snd_seq_fifo *snd_seq_fifo_new(int poolsize) { struct snd_seq_fifo *f; f = kzalloc(sizeof(*f), GFP_KERNEL); if (!f) return NULL; f->pool = snd_seq_pool_new(poolsize); if (f->pool == NULL) { kfree(f); return NULL; } if (snd_seq_pool_init(f->pool) < 0) { snd_seq_pool_delete(&f->pool); kfree(f); return NULL; } spin_lock_init(&f->lock); snd_use_lock_init(&f->use_lock); init_waitqueue_head(&f->input_sleep); atomic_set(&f->overflow, 0); f->head = NULL; f->tail = NULL; f->cells = 0; return f; } void snd_seq_fifo_delete(struct snd_seq_fifo **fifo) { struct snd_seq_fifo *f; if (snd_BUG_ON(!fifo)) return; f = *fifo; if (snd_BUG_ON(!f)) return; *fifo = NULL; if (f->pool) snd_seq_pool_mark_closing(f->pool); snd_seq_fifo_clear(f); /* wake up clients if any */ if (waitqueue_active(&f->input_sleep)) wake_up(&f->input_sleep); /* release resources...*/ /*....................*/ if (f->pool) { snd_seq_pool_done(f->pool); snd_seq_pool_delete(&f->pool); } kfree(f); } static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f); /* clear queue */ void snd_seq_fifo_clear(struct snd_seq_fifo *f) { struct snd_seq_event_cell *cell; /* clear overflow flag */ atomic_set(&f->overflow, 0); snd_use_lock_sync(&f->use_lock); spin_lock_irq(&f->lock); /* drain the fifo */ while ((cell = fifo_cell_out(f)) != NULL) { snd_seq_cell_free(cell); } spin_unlock_irq(&f->lock); } /* enqueue event to fifo */ int snd_seq_fifo_event_in(struct snd_seq_fifo *f, struct snd_seq_event *event) { struct snd_seq_event_cell *cell; unsigned long flags; int err; if (snd_BUG_ON(!f)) return -EINVAL; snd_use_lock_use(&f->use_lock); err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL, NULL); /* always non-blocking */ if (err < 0) { if ((err == -ENOMEM) || (err == -EAGAIN)) atomic_inc(&f->overflow); snd_use_lock_free(&f->use_lock); return err; } /* append new cells to fifo */ spin_lock_irqsave(&f->lock, flags); if (f->tail != NULL) f->tail->next = cell; f->tail = cell; if (f->head == NULL) f->head = cell; cell->next = NULL; f->cells++; spin_unlock_irqrestore(&f->lock, flags); /* wakeup client */ if (waitqueue_active(&f->input_sleep)) wake_up(&f->input_sleep); snd_use_lock_free(&f->use_lock); return 0; /* success */ } /* dequeue cell from fifo */ static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f) { struct snd_seq_event_cell *cell; cell = f->head; if (cell) { f->head = cell->next; /* reset tail if this was the last element */ if (f->tail == cell) f->tail = NULL; cell->next = NULL; f->cells--; } return cell; } /* dequeue cell from fifo and copy on user space */ int snd_seq_fifo_cell_out(struct snd_seq_fifo *f, struct snd_seq_event_cell **cellp, int nonblock) { struct snd_seq_event_cell *cell; unsigned long flags; wait_queue_entry_t wait; if (snd_BUG_ON(!f)) return -EINVAL; *cellp = NULL; init_waitqueue_entry(&wait, current); spin_lock_irqsave(&f->lock, flags); while ((cell = fifo_cell_out(f)) == NULL) { if (nonblock) { /* non-blocking - return immediately */ spin_unlock_irqrestore(&f->lock, flags); return -EAGAIN; } set_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&f->input_sleep, &wait); spin_unlock_irqrestore(&f->lock, flags); schedule(); spin_lock_irqsave(&f->lock, flags); remove_wait_queue(&f->input_sleep, &wait); if (signal_pending(current)) { spin_unlock_irqrestore(&f->lock, flags); return -ERESTARTSYS; } } spin_unlock_irqrestore(&f->lock, flags); *cellp = cell; return 0; } void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f, struct snd_seq_event_cell *cell) { unsigned long flags; if (cell) { spin_lock_irqsave(&f->lock, flags); cell->next = f->head; f->head = cell; if (!f->tail) f->tail = cell; f->cells++; spin_unlock_irqrestore(&f->lock, flags); } } /* polling; return non-zero if queue is available */ int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file, poll_table *wait) { poll_wait(file, &f->input_sleep, wait); return (f->cells > 0); } /* change the size of pool; all old events are removed */ int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize) { struct snd_seq_pool *newpool, *oldpool; struct snd_seq_event_cell *cell, *next, *oldhead; if (snd_BUG_ON(!f || !f->pool)) return -EINVAL; /* allocate new pool */ newpool = snd_seq_pool_new(poolsize); if (newpool == NULL) return -ENOMEM; if (snd_seq_pool_init(newpool) < 0) { snd_seq_pool_delete(&newpool); return -ENOMEM; } spin_lock_irq(&f->lock); /* remember old pool */ oldpool = f->pool; oldhead = f->head; /* exchange pools */ f->pool = newpool; f->head = NULL; f->tail = NULL; f->cells = 0; /* NOTE: overflow flag is not cleared */ spin_unlock_irq(&f->lock); /* close the old pool and wait until all users are gone */ snd_seq_pool_mark_closing(oldpool); snd_use_lock_sync(&f->use_lock); /* release cells in old pool */ for (cell = oldhead; cell; cell = next) { next = cell->next; snd_seq_cell_free(cell); } snd_seq_pool_delete(&oldpool); return 0; } /* get the number of unused cells safely */ int snd_seq_fifo_unused_cells(struct snd_seq_fifo *f) { unsigned long flags; int cells; if (!f) return 0; snd_use_lock_use(&f->use_lock); spin_lock_irqsave(&f->lock, flags); cells = snd_seq_unused_cells(f->pool); spin_unlock_irqrestore(&f->lock, flags); snd_use_lock_free(&f->use_lock); return cells; }
linux-master
sound/core/seq/seq_fifo.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer Timer * Copyright (c) 1998-1999 by Frank van de Pol <[email protected]> * Jaroslav Kysela <[email protected]> */ #include <sound/core.h> #include <linux/slab.h> #include "seq_timer.h" #include "seq_queue.h" #include "seq_info.h" /* allowed sequencer timer frequencies, in Hz */ #define MIN_FREQUENCY 10 #define MAX_FREQUENCY 6250 #define DEFAULT_FREQUENCY 1000 #define SKEW_BASE 0x10000 /* 16bit shift */ static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr) { if (tmr->tempo < 1000000) tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq; else { /* might overflow.. */ unsigned int s; s = tmr->tempo % tmr->ppq; s = (s * 1000) / tmr->ppq; tmr->tick.resolution = (tmr->tempo / tmr->ppq) * 1000; tmr->tick.resolution += s; } if (tmr->tick.resolution <= 0) tmr->tick.resolution = 1; snd_seq_timer_update_tick(&tmr->tick, 0); } /* create new timer (constructor) */ struct snd_seq_timer *snd_seq_timer_new(void) { struct snd_seq_timer *tmr; tmr = kzalloc(sizeof(*tmr), GFP_KERNEL); if (!tmr) return NULL; spin_lock_init(&tmr->lock); /* reset setup to defaults */ snd_seq_timer_defaults(tmr); /* reset time */ snd_seq_timer_reset(tmr); return tmr; } /* delete timer (destructor) */ void snd_seq_timer_delete(struct snd_seq_timer **tmr) { struct snd_seq_timer *t = *tmr; *tmr = NULL; if (t == NULL) { pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n"); return; } t->running = 0; /* reset time */ snd_seq_timer_stop(t); snd_seq_timer_reset(t); kfree(t); } void snd_seq_timer_defaults(struct snd_seq_timer * tmr) { unsigned long flags; spin_lock_irqsave(&tmr->lock, flags); /* setup defaults */ tmr->ppq = 96; /* 96 PPQ */ tmr->tempo = 500000; /* 120 BPM */ snd_seq_timer_set_tick_resolution(tmr); tmr->running = 0; tmr->type = SNDRV_SEQ_TIMER_ALSA; tmr->alsa_id.dev_class = seq_default_timer_class; tmr->alsa_id.dev_sclass = seq_default_timer_sclass; tmr->alsa_id.card = seq_default_timer_card; tmr->alsa_id.device = seq_default_timer_device; tmr->alsa_id.subdevice = seq_default_timer_subdevice; tmr->preferred_resolution = seq_default_timer_resolution; tmr->skew = tmr->skew_base = SKEW_BASE; spin_unlock_irqrestore(&tmr->lock, flags); } static void seq_timer_reset(struct snd_seq_timer *tmr) { /* reset time & songposition */ tmr->cur_time.tv_sec = 0; tmr->cur_time.tv_nsec = 0; tmr->tick.cur_tick = 0; tmr->tick.fraction = 0; } void snd_seq_timer_reset(struct snd_seq_timer *tmr) { unsigned long flags; spin_lock_irqsave(&tmr->lock, flags); seq_timer_reset(tmr); spin_unlock_irqrestore(&tmr->lock, flags); } /* called by timer interrupt routine. the period time since previous invocation is passed */ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri, unsigned long resolution, unsigned long ticks) { unsigned long flags; struct snd_seq_queue *q = timeri->callback_data; struct snd_seq_timer *tmr; if (q == NULL) return; tmr = q->timer; if (tmr == NULL) return; spin_lock_irqsave(&tmr->lock, flags); if (!tmr->running) { spin_unlock_irqrestore(&tmr->lock, flags); return; } resolution *= ticks; if (tmr->skew != tmr->skew_base) { /* FIXME: assuming skew_base = 0x10000 */ resolution = (resolution >> 16) * tmr->skew + (((resolution & 0xffff) * tmr->skew) >> 16); } /* update timer */ snd_seq_inc_time_nsec(&tmr->cur_time, resolution); /* calculate current tick */ snd_seq_timer_update_tick(&tmr->tick, resolution); /* register actual time of this timer update */ ktime_get_ts64(&tmr->last_update); spin_unlock_irqrestore(&tmr->lock, flags); /* check queues and dispatch events */ snd_seq_check_queue(q, 1, 0); } /* set current tempo */ int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo) { unsigned long flags; if (snd_BUG_ON(!tmr)) return -EINVAL; if (tempo <= 0) return -EINVAL; spin_lock_irqsave(&tmr->lock, flags); if ((unsigned int)tempo != tmr->tempo) { tmr->tempo = tempo; snd_seq_timer_set_tick_resolution(tmr); } spin_unlock_irqrestore(&tmr->lock, flags); return 0; } /* set current tempo and ppq in a shot */ int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq) { int changed; unsigned long flags; if (snd_BUG_ON(!tmr)) return -EINVAL; if (tempo <= 0 || ppq <= 0) return -EINVAL; spin_lock_irqsave(&tmr->lock, flags); if (tmr->running && (ppq != tmr->ppq)) { /* refuse to change ppq on running timers */ /* because it will upset the song position (ticks) */ spin_unlock_irqrestore(&tmr->lock, flags); pr_debug("ALSA: seq: cannot change ppq of a running timer\n"); return -EBUSY; } changed = (tempo != tmr->tempo) || (ppq != tmr->ppq); tmr->tempo = tempo; tmr->ppq = ppq; if (changed) snd_seq_timer_set_tick_resolution(tmr); spin_unlock_irqrestore(&tmr->lock, flags); return 0; } /* set current tick position */ int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, snd_seq_tick_time_t position) { unsigned long flags; if (snd_BUG_ON(!tmr)) return -EINVAL; spin_lock_irqsave(&tmr->lock, flags); tmr->tick.cur_tick = position; tmr->tick.fraction = 0; spin_unlock_irqrestore(&tmr->lock, flags); return 0; } /* set current real-time position */ int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, snd_seq_real_time_t position) { unsigned long flags; if (snd_BUG_ON(!tmr)) return -EINVAL; snd_seq_sanity_real_time(&position); spin_lock_irqsave(&tmr->lock, flags); tmr->cur_time = position; spin_unlock_irqrestore(&tmr->lock, flags); return 0; } /* set timer skew */ int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, unsigned int base) { unsigned long flags; if (snd_BUG_ON(!tmr)) return -EINVAL; /* FIXME */ if (base != SKEW_BASE) { pr_debug("ALSA: seq: invalid skew base 0x%x\n", base); return -EINVAL; } spin_lock_irqsave(&tmr->lock, flags); tmr->skew = skew; spin_unlock_irqrestore(&tmr->lock, flags); return 0; } int snd_seq_timer_open(struct snd_seq_queue *q) { struct snd_timer_instance *t; struct snd_seq_timer *tmr; char str[32]; int err; tmr = q->timer; if (snd_BUG_ON(!tmr)) return -EINVAL; if (tmr->timeri) return -EBUSY; sprintf(str, "sequencer queue %i", q->queue); if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */ return -EINVAL; if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; t = snd_timer_instance_new(str); if (!t) return -ENOMEM; t->callback = snd_seq_timer_interrupt; t->callback_data = q; t->flags |= SNDRV_TIMER_IFLG_AUTO; err = snd_timer_open(t, &tmr->alsa_id, q->queue); if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) { if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL || tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) { struct snd_timer_id tid; memset(&tid, 0, sizeof(tid)); tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER; tid.card = -1; tid.device = SNDRV_TIMER_GLOBAL_SYSTEM; err = snd_timer_open(t, &tid, q->queue); } } if (err < 0) { pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err); snd_timer_instance_free(t); return err; } spin_lock_irq(&tmr->lock); if (tmr->timeri) err = -EBUSY; else tmr->timeri = t; spin_unlock_irq(&tmr->lock); if (err < 0) { snd_timer_close(t); snd_timer_instance_free(t); return err; } return 0; } int snd_seq_timer_close(struct snd_seq_queue *q) { struct snd_seq_timer *tmr; struct snd_timer_instance *t; tmr = q->timer; if (snd_BUG_ON(!tmr)) return -EINVAL; spin_lock_irq(&tmr->lock); t = tmr->timeri; tmr->timeri = NULL; spin_unlock_irq(&tmr->lock); if (t) { snd_timer_close(t); snd_timer_instance_free(t); } return 0; } static int seq_timer_stop(struct snd_seq_timer *tmr) { if (! tmr->timeri) return -EINVAL; if (!tmr->running) return 0; tmr->running = 0; snd_timer_pause(tmr->timeri); return 0; } int snd_seq_timer_stop(struct snd_seq_timer *tmr) { unsigned long flags; int err; spin_lock_irqsave(&tmr->lock, flags); err = seq_timer_stop(tmr); spin_unlock_irqrestore(&tmr->lock, flags); return err; } static int initialize_timer(struct snd_seq_timer *tmr) { struct snd_timer *t; unsigned long freq; t = tmr->timeri->timer; if (!t) return -EINVAL; freq = tmr->preferred_resolution; if (!freq) freq = DEFAULT_FREQUENCY; else if (freq < MIN_FREQUENCY) freq = MIN_FREQUENCY; else if (freq > MAX_FREQUENCY) freq = MAX_FREQUENCY; tmr->ticks = 1; if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { unsigned long r = snd_timer_resolution(tmr->timeri); if (r) { tmr->ticks = (unsigned int)(1000000000uL / (r * freq)); if (! tmr->ticks) tmr->ticks = 1; } } tmr->initialized = 1; return 0; } static int seq_timer_start(struct snd_seq_timer *tmr) { if (! tmr->timeri) return -EINVAL; if (tmr->running) seq_timer_stop(tmr); seq_timer_reset(tmr); if (initialize_timer(tmr) < 0) return -EINVAL; snd_timer_start(tmr->timeri, tmr->ticks); tmr->running = 1; ktime_get_ts64(&tmr->last_update); return 0; } int snd_seq_timer_start(struct snd_seq_timer *tmr) { unsigned long flags; int err; spin_lock_irqsave(&tmr->lock, flags); err = seq_timer_start(tmr); spin_unlock_irqrestore(&tmr->lock, flags); return err; } static int seq_timer_continue(struct snd_seq_timer *tmr) { if (! tmr->timeri) return -EINVAL; if (tmr->running) return -EBUSY; if (! tmr->initialized) { seq_timer_reset(tmr); if (initialize_timer(tmr) < 0) return -EINVAL; } snd_timer_start(tmr->timeri, tmr->ticks); tmr->running = 1; ktime_get_ts64(&tmr->last_update); return 0; } int snd_seq_timer_continue(struct snd_seq_timer *tmr) { unsigned long flags; int err; spin_lock_irqsave(&tmr->lock, flags); err = seq_timer_continue(tmr); spin_unlock_irqrestore(&tmr->lock, flags); return err; } /* return current 'real' time. use timeofday() to get better granularity. */ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr, bool adjust_ktime) { snd_seq_real_time_t cur_time; unsigned long flags; spin_lock_irqsave(&tmr->lock, flags); cur_time = tmr->cur_time; if (adjust_ktime && tmr->running) { struct timespec64 tm; ktime_get_ts64(&tm); tm = timespec64_sub(tm, tmr->last_update); cur_time.tv_nsec += tm.tv_nsec; cur_time.tv_sec += tm.tv_sec; snd_seq_sanity_real_time(&cur_time); } spin_unlock_irqrestore(&tmr->lock, flags); return cur_time; } /* TODO: use interpolation on tick queue (will only be useful for very high PPQ values) */ snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr) { snd_seq_tick_time_t cur_tick; unsigned long flags; spin_lock_irqsave(&tmr->lock, flags); cur_tick = tmr->tick.cur_tick; spin_unlock_irqrestore(&tmr->lock, flags); return cur_tick; } #ifdef CONFIG_SND_PROC_FS /* exported to seq_info.c */ void snd_seq_info_timer_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { int idx; struct snd_seq_queue *q; struct snd_seq_timer *tmr; struct snd_timer_instance *ti; unsigned long resolution; for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) { q = queueptr(idx); if (q == NULL) continue; mutex_lock(&q->timer_mutex); tmr = q->timer; if (!tmr) goto unlock; ti = tmr->timeri; if (!ti) goto unlock; snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name); resolution = snd_timer_resolution(ti) * tmr->ticks; snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000); snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base); unlock: mutex_unlock(&q->timer_mutex); queuefree(q); } } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/seq_timer.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Virtual Raw MIDI client on Sequencer * * Copyright (c) 2000 by Takashi Iwai <[email protected]>, * Jaroslav Kysela <[email protected]> */ /* * Virtual Raw MIDI client * * The virtual rawmidi client is a sequencer client which associate * a rawmidi device file. The created rawmidi device file can be * accessed as a normal raw midi, but its MIDI source and destination * are arbitrary. For example, a user-client software synth connected * to this port can be used as a normal midi device as well. * * The virtual rawmidi device accepts also multiple opens. Each file * has its own input buffer, so that no conflict would occur. The drain * of input/output buffer acts only to the local buffer. * */ #include <linux/init.h> #include <linux/wait.h> #include <linux/module.h> #include <linux/slab.h> #include <sound/core.h> #include <sound/rawmidi.h> #include <sound/info.h> #include <sound/control.h> #include <sound/minors.h> #include <sound/seq_kernel.h> #include <sound/seq_midi_event.h> #include <sound/seq_virmidi.h> MODULE_AUTHOR("Takashi Iwai <[email protected]>"); MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer"); MODULE_LICENSE("GPL"); /* * initialize an event record */ static void snd_virmidi_init_event(struct snd_virmidi *vmidi, struct snd_seq_event *ev) { memset(ev, 0, sizeof(*ev)); ev->source.port = vmidi->port; switch (vmidi->seq_mode) { case SNDRV_VIRMIDI_SEQ_DISPATCH: ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; break; case SNDRV_VIRMIDI_SEQ_ATTACH: /* FIXME: source and destination are same - not good.. */ ev->dest.client = vmidi->client; ev->dest.port = vmidi->port; break; } ev->type = SNDRV_SEQ_EVENT_NONE; } /* * decode input event and put to read buffer of each opened file */ static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev, struct snd_seq_event *ev, bool atomic) { struct snd_virmidi *vmidi; unsigned char msg[4]; int len; if (atomic) read_lock(&rdev->filelist_lock); else down_read(&rdev->filelist_sem); list_for_each_entry(vmidi, &rdev->filelist, list) { if (!READ_ONCE(vmidi->trigger)) continue; if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) continue; snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream); snd_midi_event_reset_decode(vmidi->parser); } else { len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev); if (len > 0) snd_rawmidi_receive(vmidi->substream, msg, len); } } if (atomic) read_unlock(&rdev->filelist_lock); else up_read(&rdev->filelist_sem); return 0; } /* * event handler of virmidi port */ static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct, void *private_data, int atomic, int hop) { struct snd_virmidi_dev *rdev; rdev = private_data; if (!(rdev->flags & SNDRV_VIRMIDI_USE)) return 0; /* ignored */ return snd_virmidi_dev_receive_event(rdev, ev, atomic); } /* * trigger rawmidi stream for input */ static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) { struct snd_virmidi *vmidi = substream->runtime->private_data; WRITE_ONCE(vmidi->trigger, !!up); } /* process rawmidi bytes and send events; * we need no lock here for vmidi->event since it's handled only in this work */ static void snd_vmidi_output_work(struct work_struct *work) { struct snd_virmidi *vmidi; struct snd_rawmidi_substream *substream; unsigned char input; int ret; vmidi = container_of(work, struct snd_virmidi, output_work); substream = vmidi->substream; /* discard the outputs in dispatch mode unless subscribed */ if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH && !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) { snd_rawmidi_proceed(substream); return; } while (READ_ONCE(vmidi->trigger)) { if (snd_rawmidi_transmit(substream, &input, 1) != 1) break; if (!snd_midi_event_encode_byte(vmidi->parser, input, &vmidi->event)) continue; if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) { ret = snd_seq_kernel_client_dispatch(vmidi->client, &vmidi->event, false, 0); vmidi->event.type = SNDRV_SEQ_EVENT_NONE; if (ret < 0) break; } /* rawmidi input might be huge, allow to have a break */ cond_resched(); } } /* * trigger rawmidi stream for output */ static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) { struct snd_virmidi *vmidi = substream->runtime->private_data; WRITE_ONCE(vmidi->trigger, !!up); if (up) queue_work(system_highpri_wq, &vmidi->output_work); } /* * open rawmidi handle for input */ static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream) { struct snd_virmidi_dev *rdev = substream->rmidi->private_data; struct snd_rawmidi_runtime *runtime = substream->runtime; struct snd_virmidi *vmidi; vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL); if (vmidi == NULL) return -ENOMEM; vmidi->substream = substream; if (snd_midi_event_new(0, &vmidi->parser) < 0) { kfree(vmidi); return -ENOMEM; } vmidi->seq_mode = rdev->seq_mode; vmidi->client = rdev->client; vmidi->port = rdev->port; runtime->private_data = vmidi; down_write(&rdev->filelist_sem); write_lock_irq(&rdev->filelist_lock); list_add_tail(&vmidi->list, &rdev->filelist); write_unlock_irq(&rdev->filelist_lock); up_write(&rdev->filelist_sem); vmidi->rdev = rdev; return 0; } /* * open rawmidi handle for output */ static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream) { struct snd_virmidi_dev *rdev = substream->rmidi->private_data; struct snd_rawmidi_runtime *runtime = substream->runtime; struct snd_virmidi *vmidi; vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL); if (vmidi == NULL) return -ENOMEM; vmidi->substream = substream; if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) { kfree(vmidi); return -ENOMEM; } vmidi->seq_mode = rdev->seq_mode; vmidi->client = rdev->client; vmidi->port = rdev->port; snd_virmidi_init_event(vmidi, &vmidi->event); vmidi->rdev = rdev; INIT_WORK(&vmidi->output_work, snd_vmidi_output_work); runtime->private_data = vmidi; return 0; } /* * close rawmidi handle for input */ static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream) { struct snd_virmidi_dev *rdev = substream->rmidi->private_data; struct snd_virmidi *vmidi = substream->runtime->private_data; down_write(&rdev->filelist_sem); write_lock_irq(&rdev->filelist_lock); list_del(&vmidi->list); write_unlock_irq(&rdev->filelist_lock); up_write(&rdev->filelist_sem); snd_midi_event_free(vmidi->parser); substream->runtime->private_data = NULL; kfree(vmidi); return 0; } /* * close rawmidi handle for output */ static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream) { struct snd_virmidi *vmidi = substream->runtime->private_data; WRITE_ONCE(vmidi->trigger, false); /* to be sure */ cancel_work_sync(&vmidi->output_work); snd_midi_event_free(vmidi->parser); substream->runtime->private_data = NULL; kfree(vmidi); return 0; } /* * drain output work queue */ static void snd_virmidi_output_drain(struct snd_rawmidi_substream *substream) { struct snd_virmidi *vmidi = substream->runtime->private_data; flush_work(&vmidi->output_work); } /* * subscribe callback - allow output to rawmidi device */ static int snd_virmidi_subscribe(void *private_data, struct snd_seq_port_subscribe *info) { struct snd_virmidi_dev *rdev; rdev = private_data; if (!try_module_get(rdev->card->module)) return -EFAULT; rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE; return 0; } /* * unsubscribe callback - disallow output to rawmidi device */ static int snd_virmidi_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info) { struct snd_virmidi_dev *rdev; rdev = private_data; rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE; module_put(rdev->card->module); return 0; } /* * use callback - allow input to rawmidi device */ static int snd_virmidi_use(void *private_data, struct snd_seq_port_subscribe *info) { struct snd_virmidi_dev *rdev; rdev = private_data; if (!try_module_get(rdev->card->module)) return -EFAULT; rdev->flags |= SNDRV_VIRMIDI_USE; return 0; } /* * unuse callback - disallow input to rawmidi device */ static int snd_virmidi_unuse(void *private_data, struct snd_seq_port_subscribe *info) { struct snd_virmidi_dev *rdev; rdev = private_data; rdev->flags &= ~SNDRV_VIRMIDI_USE; module_put(rdev->card->module); return 0; } /* * Register functions */ static const struct snd_rawmidi_ops snd_virmidi_input_ops = { .open = snd_virmidi_input_open, .close = snd_virmidi_input_close, .trigger = snd_virmidi_input_trigger, }; static const struct snd_rawmidi_ops snd_virmidi_output_ops = { .open = snd_virmidi_output_open, .close = snd_virmidi_output_close, .trigger = snd_virmidi_output_trigger, .drain = snd_virmidi_output_drain, }; /* * create a sequencer client and a port */ static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev) { int client; struct snd_seq_port_callback pcallbacks; struct snd_seq_port_info *pinfo; int err; if (rdev->client >= 0) return 0; pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL); if (!pinfo) { err = -ENOMEM; goto __error; } client = snd_seq_create_kernel_client(rdev->card, rdev->device, "%s %d-%d", rdev->rmidi->name, rdev->card->number, rdev->device); if (client < 0) { err = client; goto __error; } rdev->client = client; /* create a port */ pinfo->addr.client = client; sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device); /* set all capabilities */ pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; pinfo->direction = SNDRV_SEQ_PORT_DIR_BIDIRECTION; pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | SNDRV_SEQ_PORT_TYPE_SOFTWARE | SNDRV_SEQ_PORT_TYPE_PORT; pinfo->midi_channels = 16; memset(&pcallbacks, 0, sizeof(pcallbacks)); pcallbacks.owner = THIS_MODULE; pcallbacks.private_data = rdev; pcallbacks.subscribe = snd_virmidi_subscribe; pcallbacks.unsubscribe = snd_virmidi_unsubscribe; pcallbacks.use = snd_virmidi_use; pcallbacks.unuse = snd_virmidi_unuse; pcallbacks.event_input = snd_virmidi_event_input; pinfo->kernel = &pcallbacks; err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo); if (err < 0) { snd_seq_delete_kernel_client(client); rdev->client = -1; goto __error; } rdev->port = pinfo->addr.port; err = 0; /* success */ __error: kfree(pinfo); return err; } /* * release the sequencer client */ static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev) { if (rdev->client >= 0) { snd_seq_delete_kernel_client(rdev->client); rdev->client = -1; } } /* * register the device */ static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi) { struct snd_virmidi_dev *rdev = rmidi->private_data; int err; switch (rdev->seq_mode) { case SNDRV_VIRMIDI_SEQ_DISPATCH: err = snd_virmidi_dev_attach_seq(rdev); if (err < 0) return err; break; case SNDRV_VIRMIDI_SEQ_ATTACH: if (rdev->client == 0) return -EINVAL; /* should check presence of port more strictly.. */ break; default: pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode); return -EINVAL; } return 0; } /* * unregister the device */ static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi) { struct snd_virmidi_dev *rdev = rmidi->private_data; if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH) snd_virmidi_dev_detach_seq(rdev); return 0; } /* * */ static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = { .dev_register = snd_virmidi_dev_register, .dev_unregister = snd_virmidi_dev_unregister, }; /* * free device */ static void snd_virmidi_free(struct snd_rawmidi *rmidi) { struct snd_virmidi_dev *rdev = rmidi->private_data; kfree(rdev); } /* * create a new device * */ /* exported */ int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi) { struct snd_rawmidi *rmidi; struct snd_virmidi_dev *rdev; int err; *rrmidi = NULL; err = snd_rawmidi_new(card, "VirMidi", device, 16, /* may be configurable */ 16, /* may be configurable */ &rmidi); if (err < 0) return err; strcpy(rmidi->name, rmidi->id); rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); if (rdev == NULL) { snd_device_free(card, rmidi); return -ENOMEM; } rdev->card = card; rdev->rmidi = rmidi; rdev->device = device; rdev->client = -1; init_rwsem(&rdev->filelist_sem); rwlock_init(&rdev->filelist_lock); INIT_LIST_HEAD(&rdev->filelist); rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH; rmidi->private_data = rdev; rmidi->private_free = snd_virmidi_free; rmidi->ops = &snd_virmidi_global_ops; snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops); rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_DUPLEX; *rrmidi = rmidi; return 0; } EXPORT_SYMBOL(snd_virmidi_new);
linux-master
sound/core/seq/seq_virmidi.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Generic MIDI synth driver for ALSA sequencer * Copyright (c) 1998 by Frank van de Pol <[email protected]> * Jaroslav Kysela <[email protected]> */ /* Possible options for midisynth module: - automatic opening of midi ports on first received event or subscription (close will be performed when client leaves) */ #include <linux/init.h> #include <linux/slab.h> #include <linux/errno.h> #include <linux/string.h> #include <linux/module.h> #include <linux/mutex.h> #include <sound/core.h> #include <sound/rawmidi.h> #include <sound/seq_kernel.h> #include <sound/seq_device.h> #include <sound/seq_midi_event.h> #include <sound/initval.h> MODULE_AUTHOR("Frank van de Pol <[email protected]>, Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth."); MODULE_LICENSE("GPL"); static int output_buffer_size = PAGE_SIZE; module_param(output_buffer_size, int, 0644); MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes."); static int input_buffer_size = PAGE_SIZE; module_param(input_buffer_size, int, 0644); MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes."); /* data for this midi synth driver */ struct seq_midisynth { struct snd_card *card; struct snd_rawmidi *rmidi; int device; int subdevice; struct snd_rawmidi_file input_rfile; struct snd_rawmidi_file output_rfile; int seq_client; int seq_port; struct snd_midi_event *parser; }; struct seq_midisynth_client { int seq_client; int num_ports; int ports_per_device[SNDRV_RAWMIDI_DEVICES]; struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES]; }; static struct seq_midisynth_client *synths[SNDRV_CARDS]; static DEFINE_MUTEX(register_mutex); /* handle rawmidi input event (MIDI v1.0 stream) */ static void snd_midi_input_event(struct snd_rawmidi_substream *substream) { struct snd_rawmidi_runtime *runtime; struct seq_midisynth *msynth; struct snd_seq_event ev; char buf[16], *pbuf; long res; if (substream == NULL) return; runtime = substream->runtime; msynth = runtime->private_data; if (msynth == NULL) return; memset(&ev, 0, sizeof(ev)); while (runtime->avail > 0) { res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf)); if (res <= 0) continue; if (msynth->parser == NULL) continue; pbuf = buf; while (res-- > 0) { if (!snd_midi_event_encode_byte(msynth->parser, *pbuf++, &ev)) continue; ev.source.port = msynth->seq_port; ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0); /* clear event and reset header */ memset(&ev, 0, sizeof(ev)); } } } static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count) { struct snd_rawmidi_runtime *runtime; int tmp; if (snd_BUG_ON(!substream || !buf)) return -EINVAL; runtime = substream->runtime; tmp = runtime->avail; if (tmp < count) { if (printk_ratelimit()) pr_err("ALSA: seq_midi: MIDI output buffer overrun\n"); return -ENOMEM; } if (snd_rawmidi_kernel_write(substream, buf, count) < count) return -EINVAL; return 0; } static int event_process_midi(struct snd_seq_event *ev, int direct, void *private_data, int atomic, int hop) { struct seq_midisynth *msynth = private_data; unsigned char msg[10]; /* buffer for constructing midi messages */ struct snd_rawmidi_substream *substream; int len; if (snd_BUG_ON(!msynth)) return -EINVAL; substream = msynth->output_rfile.output; if (substream == NULL) return -ENODEV; if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */ if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) { /* invalid event */ pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags); return 0; } snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream); snd_midi_event_reset_decode(msynth->parser); } else { if (msynth->parser == NULL) return -EIO; len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev); if (len < 0) return 0; if (dump_midi(substream, msg, len) < 0) snd_midi_event_reset_decode(msynth->parser); } return 0; } static int snd_seq_midisynth_new(struct seq_midisynth *msynth, struct snd_card *card, int device, int subdevice) { if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0) return -ENOMEM; msynth->card = card; msynth->device = device; msynth->subdevice = subdevice; return 0; } /* open associated midi device for input */ static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info) { int err; struct seq_midisynth *msynth = private_data; struct snd_rawmidi_runtime *runtime; struct snd_rawmidi_params params; /* open midi port */ err = snd_rawmidi_kernel_open(msynth->rmidi, msynth->subdevice, SNDRV_RAWMIDI_LFLG_INPUT, &msynth->input_rfile); if (err < 0) { pr_debug("ALSA: seq_midi: midi input open failed!!!\n"); return err; } runtime = msynth->input_rfile.input->runtime; memset(&params, 0, sizeof(params)); params.avail_min = 1; params.buffer_size = input_buffer_size; err = snd_rawmidi_input_params(msynth->input_rfile.input, &params); if (err < 0) { snd_rawmidi_kernel_release(&msynth->input_rfile); return err; } snd_midi_event_reset_encode(msynth->parser); runtime->event = snd_midi_input_event; runtime->private_data = msynth; snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0); return 0; } /* close associated midi device for input */ static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info) { int err; struct seq_midisynth *msynth = private_data; if (snd_BUG_ON(!msynth->input_rfile.input)) return -EINVAL; err = snd_rawmidi_kernel_release(&msynth->input_rfile); return err; } /* open associated midi device for output */ static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info) { int err; struct seq_midisynth *msynth = private_data; struct snd_rawmidi_params params; /* open midi port */ err = snd_rawmidi_kernel_open(msynth->rmidi, msynth->subdevice, SNDRV_RAWMIDI_LFLG_OUTPUT, &msynth->output_rfile); if (err < 0) { pr_debug("ALSA: seq_midi: midi output open failed!!!\n"); return err; } memset(&params, 0, sizeof(params)); params.avail_min = 1; params.buffer_size = output_buffer_size; params.no_active_sensing = 1; err = snd_rawmidi_output_params(msynth->output_rfile.output, &params); if (err < 0) { snd_rawmidi_kernel_release(&msynth->output_rfile); return err; } snd_midi_event_reset_decode(msynth->parser); return 0; } /* close associated midi device for output */ static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info) { struct seq_midisynth *msynth = private_data; if (snd_BUG_ON(!msynth->output_rfile.output)) return -EINVAL; snd_rawmidi_drain_output(msynth->output_rfile.output); return snd_rawmidi_kernel_release(&msynth->output_rfile); } /* delete given midi synth port */ static void snd_seq_midisynth_delete(struct seq_midisynth *msynth) { if (msynth == NULL) return; if (msynth->seq_client > 0) { /* delete port */ snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port); } snd_midi_event_free(msynth->parser); } /* register new midi synth port */ static int snd_seq_midisynth_probe(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); struct seq_midisynth_client *client; struct seq_midisynth *msynth, *ms; struct snd_seq_port_info *port; struct snd_rawmidi_info *info; struct snd_rawmidi *rmidi = dev->private_data; int newclient = 0; unsigned int p, ports; struct snd_seq_port_callback pcallbacks; struct snd_card *card = dev->card; int device = dev->device; unsigned int input_count = 0, output_count = 0; if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES)) return -EINVAL; info = kmalloc(sizeof(*info), GFP_KERNEL); if (! info) return -ENOMEM; info->device = device; info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; info->subdevice = 0; if (snd_rawmidi_info_select(card, info) >= 0) output_count = info->subdevices_count; info->stream = SNDRV_RAWMIDI_STREAM_INPUT; if (snd_rawmidi_info_select(card, info) >= 0) { input_count = info->subdevices_count; } ports = output_count; if (ports < input_count) ports = input_count; if (ports == 0) { kfree(info); return -ENODEV; } if (ports > (256 / SNDRV_RAWMIDI_DEVICES)) ports = 256 / SNDRV_RAWMIDI_DEVICES; mutex_lock(&register_mutex); client = synths[card->number]; if (client == NULL) { newclient = 1; client = kzalloc(sizeof(*client), GFP_KERNEL); if (client == NULL) { mutex_unlock(&register_mutex); kfree(info); return -ENOMEM; } client->seq_client = snd_seq_create_kernel_client( card, 0, "%s", card->shortname[0] ? (const char *)card->shortname : "External MIDI"); if (client->seq_client < 0) { kfree(client); mutex_unlock(&register_mutex); kfree(info); return -ENOMEM; } } msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL); port = kmalloc(sizeof(*port), GFP_KERNEL); if (msynth == NULL || port == NULL) goto __nomem; for (p = 0; p < ports; p++) { ms = &msynth[p]; ms->rmidi = rmidi; if (snd_seq_midisynth_new(ms, card, device, p) < 0) goto __nomem; /* declare port */ memset(port, 0, sizeof(*port)); port->addr.client = client->seq_client; port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p; port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; memset(info, 0, sizeof(*info)); info->device = device; if (p < output_count) info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; else info->stream = SNDRV_RAWMIDI_STREAM_INPUT; info->subdevice = p; if (snd_rawmidi_info_select(card, info) >= 0) strcpy(port->name, info->subname); if (! port->name[0]) { if (info->name[0]) { if (ports > 1) scnprintf(port->name, sizeof(port->name), "%s-%u", info->name, p); else scnprintf(port->name, sizeof(port->name), "%s", info->name); } else { /* last resort */ if (ports > 1) sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p); else sprintf(port->name, "MIDI %d-%d", card->number, device); } } if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count) port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count) port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) && info->flags & SNDRV_RAWMIDI_INFO_DUPLEX) port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; if (port->capability & SNDRV_SEQ_PORT_CAP_READ) port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; if (port->capability & SNDRV_SEQ_PORT_CAP_WRITE) port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | SNDRV_SEQ_PORT_TYPE_HARDWARE | SNDRV_SEQ_PORT_TYPE_PORT; port->midi_channels = 16; memset(&pcallbacks, 0, sizeof(pcallbacks)); pcallbacks.owner = THIS_MODULE; pcallbacks.private_data = ms; pcallbacks.subscribe = midisynth_subscribe; pcallbacks.unsubscribe = midisynth_unsubscribe; pcallbacks.use = midisynth_use; pcallbacks.unuse = midisynth_unuse; pcallbacks.event_input = event_process_midi; port->kernel = &pcallbacks; if (rmidi->ops && rmidi->ops->get_port_info) rmidi->ops->get_port_info(rmidi, p, port); if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0) goto __nomem; ms->seq_client = client->seq_client; ms->seq_port = port->addr.port; } client->ports_per_device[device] = ports; client->ports[device] = msynth; client->num_ports++; if (newclient) synths[card->number] = client; mutex_unlock(&register_mutex); kfree(info); kfree(port); return 0; /* success */ __nomem: if (msynth != NULL) { for (p = 0; p < ports; p++) snd_seq_midisynth_delete(&msynth[p]); kfree(msynth); } if (newclient) { snd_seq_delete_kernel_client(client->seq_client); kfree(client); } kfree(info); kfree(port); mutex_unlock(&register_mutex); return -ENOMEM; } /* release midi synth port */ static int snd_seq_midisynth_remove(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); struct seq_midisynth_client *client; struct seq_midisynth *msynth; struct snd_card *card = dev->card; int device = dev->device, p, ports; mutex_lock(&register_mutex); client = synths[card->number]; if (client == NULL || client->ports[device] == NULL) { mutex_unlock(&register_mutex); return -ENODEV; } ports = client->ports_per_device[device]; client->ports_per_device[device] = 0; msynth = client->ports[device]; client->ports[device] = NULL; for (p = 0; p < ports; p++) snd_seq_midisynth_delete(&msynth[p]); kfree(msynth); client->num_ports--; if (client->num_ports <= 0) { snd_seq_delete_kernel_client(client->seq_client); synths[card->number] = NULL; kfree(client); } mutex_unlock(&register_mutex); return 0; } static struct snd_seq_driver seq_midisynth_driver = { .driver = { .name = KBUILD_MODNAME, .probe = snd_seq_midisynth_probe, .remove = snd_seq_midisynth_remove, }, .id = SNDRV_SEQ_DEV_ID_MIDISYNTH, .argsize = 0, }; module_snd_seq_driver(seq_midisynth_driver);
linux-master
sound/core/seq/seq_midi.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer MIDI-through client * Copyright (c) 1999-2000 by Takashi Iwai <[email protected]> */ #include <linux/init.h> #include <linux/slab.h> #include <linux/module.h> #include <sound/core.h> #include "seq_clientmgr.h" #include <sound/initval.h> #include <sound/asoundef.h> /* Sequencer MIDI-through client This gives a simple midi-through client. All the normal input events are redirected to output port immediately. The routing can be done via aconnect program in alsa-utils. Each client has a static client number 14 (= SNDRV_SEQ_CLIENT_DUMMY). If you want to auto-load this module, you may add the following alias in your /etc/conf.modules file. alias snd-seq-client-14 snd-seq-dummy The module is loaded on demand for client 14, or /proc/asound/seq/ is accessed. If you don't need this module to be loaded, alias snd-seq-client-14 as "off". This will help modprobe. The number of ports to be created can be specified via the module parameter "ports". For example, to create four ports, add the following option in a configuration file under /etc/modprobe.d/: option snd-seq-dummy ports=4 The model option "duplex=1" enables duplex operation to the port. In duplex mode, a pair of ports are created instead of single port, and events are tunneled between pair-ports. For example, input to port A is sent to output port of another port B and vice versa. In duplex mode, each port has DUPLEX capability. */ MODULE_AUTHOR("Takashi Iwai <[email protected]>"); MODULE_DESCRIPTION("ALSA sequencer MIDI-through client"); MODULE_LICENSE("GPL"); MODULE_ALIAS("snd-seq-client-" __stringify(SNDRV_SEQ_CLIENT_DUMMY)); static int ports = 1; static bool duplex; module_param(ports, int, 0444); MODULE_PARM_DESC(ports, "number of ports to be created"); module_param(duplex, bool, 0444); MODULE_PARM_DESC(duplex, "create DUPLEX ports"); struct snd_seq_dummy_port { int client; int port; int duplex; int connect; }; static int my_client = -1; /* * event input callback - just redirect events to subscribers */ static int dummy_input(struct snd_seq_event *ev, int direct, void *private_data, int atomic, int hop) { struct snd_seq_dummy_port *p; struct snd_seq_event tmpev; p = private_data; if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM || ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR) return 0; /* ignore system messages */ tmpev = *ev; if (p->duplex) tmpev.source.port = p->connect; else tmpev.source.port = p->port; tmpev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; return snd_seq_kernel_client_dispatch(p->client, &tmpev, atomic, hop); } /* * free_private callback */ static void dummy_free(void *private_data) { kfree(private_data); } /* * create a port */ static struct snd_seq_dummy_port __init * create_port(int idx, int type) { struct snd_seq_port_info pinfo; struct snd_seq_port_callback pcb; struct snd_seq_dummy_port *rec; rec = kzalloc(sizeof(*rec), GFP_KERNEL); if (!rec) return NULL; rec->client = my_client; rec->duplex = duplex; rec->connect = 0; memset(&pinfo, 0, sizeof(pinfo)); pinfo.addr.client = my_client; if (duplex) sprintf(pinfo.name, "Midi Through Port-%d:%c", idx, (type ? 'B' : 'A')); else sprintf(pinfo.name, "Midi Through Port-%d", idx); pinfo.capability = SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; pinfo.capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; if (duplex) pinfo.capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; pinfo.direction = SNDRV_SEQ_PORT_DIR_BIDIRECTION; pinfo.type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | SNDRV_SEQ_PORT_TYPE_SOFTWARE | SNDRV_SEQ_PORT_TYPE_PORT; memset(&pcb, 0, sizeof(pcb)); pcb.owner = THIS_MODULE; pcb.event_input = dummy_input; pcb.private_free = dummy_free; pcb.private_data = rec; pinfo.kernel = &pcb; if (snd_seq_kernel_client_ctl(my_client, SNDRV_SEQ_IOCTL_CREATE_PORT, &pinfo) < 0) { kfree(rec); return NULL; } rec->port = pinfo.addr.port; return rec; } /* * register client and create ports */ static int __init register_client(void) { struct snd_seq_dummy_port *rec1, *rec2; struct snd_seq_client *client; int i; if (ports < 1) { pr_err("ALSA: seq_dummy: invalid number of ports %d\n", ports); return -EINVAL; } /* create client */ my_client = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_DUMMY, "Midi Through"); if (my_client < 0) return my_client; /* don't convert events but just pass-through */ client = snd_seq_kernel_client_get(my_client); if (!client) return -EINVAL; client->filter = SNDRV_SEQ_FILTER_NO_CONVERT; snd_seq_kernel_client_put(client); /* create ports */ for (i = 0; i < ports; i++) { rec1 = create_port(i, 0); if (rec1 == NULL) { snd_seq_delete_kernel_client(my_client); return -ENOMEM; } if (duplex) { rec2 = create_port(i, 1); if (rec2 == NULL) { snd_seq_delete_kernel_client(my_client); return -ENOMEM; } rec1->connect = rec2->port; rec2->connect = rec1->port; } } return 0; } /* * delete client if exists */ static void __exit delete_client(void) { if (my_client >= 0) snd_seq_delete_kernel_client(my_client); } /* * Init part */ static int __init alsa_seq_dummy_init(void) { return register_client(); } static void __exit alsa_seq_dummy_exit(void) { delete_client(); } module_init(alsa_seq_dummy_init) module_exit(alsa_seq_dummy_exit)
linux-master
sound/core/seq/seq_dummy.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer event conversion between UMP and legacy clients */ #include <linux/init.h> #include <linux/errno.h> #include <linux/string.h> #include <sound/core.h> #include <sound/ump.h> #include <sound/ump_msg.h> #include "seq_ump_convert.h" /* * Upgrade / downgrade value bits */ static u8 downscale_32_to_7bit(u32 src) { return src >> 25; } static u16 downscale_32_to_14bit(u32 src) { return src >> 18; } static u8 downscale_16_to_7bit(u16 src) { return src >> 9; } static u16 upscale_7_to_16bit(u8 src) { u16 val, repeat; val = (u16)src << 9; if (src <= 0x40) return val; repeat = src & 0x3f; return val | (repeat << 3) | (repeat >> 3); } static u32 upscale_7_to_32bit(u8 src) { u32 val, repeat; val = src << 25; if (src <= 0x40) return val; repeat = src & 0x3f; return val | (repeat << 19) | (repeat << 13) | (repeat << 7) | (repeat << 1) | (repeat >> 5); } static u32 upscale_14_to_32bit(u16 src) { u32 val, repeat; val = src << 18; if (src <= 0x2000) return val; repeat = src & 0x1fff; return val | (repeat << 5) | (repeat >> 8); } static unsigned char get_ump_group(struct snd_seq_client_port *port) { return port->ump_group ? (port->ump_group - 1) : 0; } /* create a UMP header */ #define make_raw_ump(port, type) \ ump_compose(type, get_ump_group(port), 0, 0) /* * UMP -> MIDI1 sequencer event */ /* MIDI 1.0 CVM */ /* encode note event */ static void ump_midi1_to_note_ev(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { ev->data.note.channel = val->note.channel; ev->data.note.note = val->note.note; ev->data.note.velocity = val->note.velocity; } /* encode one parameter controls */ static void ump_midi1_to_ctrl_ev(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->caf.channel; ev->data.control.value = val->caf.data; } /* encode pitch wheel change */ static void ump_midi1_to_pitchbend_ev(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->pb.channel; ev->data.control.value = (val->pb.data_msb << 7) | val->pb.data_lsb; ev->data.control.value -= 8192; } /* encode midi control change */ static void ump_midi1_to_cc_ev(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->cc.channel; ev->data.control.param = val->cc.index; ev->data.control.value = val->cc.data; } /* Encoding MIDI 1.0 UMP packet */ struct seq_ump_midi1_to_ev { int seq_type; void (*encode)(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev); }; /* Encoders for MIDI1 status 0x80-0xe0 */ static struct seq_ump_midi1_to_ev midi1_msg_encoders[] = { {SNDRV_SEQ_EVENT_NOTEOFF, ump_midi1_to_note_ev}, /* 0x80 */ {SNDRV_SEQ_EVENT_NOTEON, ump_midi1_to_note_ev}, /* 0x90 */ {SNDRV_SEQ_EVENT_KEYPRESS, ump_midi1_to_note_ev}, /* 0xa0 */ {SNDRV_SEQ_EVENT_CONTROLLER, ump_midi1_to_cc_ev}, /* 0xb0 */ {SNDRV_SEQ_EVENT_PGMCHANGE, ump_midi1_to_ctrl_ev}, /* 0xc0 */ {SNDRV_SEQ_EVENT_CHANPRESS, ump_midi1_to_ctrl_ev}, /* 0xd0 */ {SNDRV_SEQ_EVENT_PITCHBEND, ump_midi1_to_pitchbend_ev}, /* 0xe0 */ }; static int cvt_ump_midi1_to_event(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { unsigned char status = val->note.status; if (status < 0x8 || status > 0xe) return 0; /* invalid - skip */ status -= 8; ev->type = midi1_msg_encoders[status].seq_type; ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; midi1_msg_encoders[status].encode(val, ev); return 1; } /* MIDI System message */ /* encode one parameter value*/ static void ump_system_to_one_param_ev(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { ev->data.control.value = val->system.parm1; } /* encode song position */ static void ump_system_to_songpos_ev(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { ev->data.control.value = (val->system.parm1 << 7) | val->system.parm2; } /* Encoders for 0xf0 - 0xff */ static struct seq_ump_midi1_to_ev system_msg_encoders[] = { {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xf0 */ {SNDRV_SEQ_EVENT_QFRAME, ump_system_to_one_param_ev}, /* 0xf1 */ {SNDRV_SEQ_EVENT_SONGPOS, ump_system_to_songpos_ev}, /* 0xf2 */ {SNDRV_SEQ_EVENT_SONGSEL, ump_system_to_one_param_ev}, /* 0xf3 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xf4 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xf5 */ {SNDRV_SEQ_EVENT_TUNE_REQUEST, NULL}, /* 0xf6 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xf7 */ {SNDRV_SEQ_EVENT_CLOCK, NULL}, /* 0xf8 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xf9 */ {SNDRV_SEQ_EVENT_START, NULL}, /* 0xfa */ {SNDRV_SEQ_EVENT_CONTINUE, NULL}, /* 0xfb */ {SNDRV_SEQ_EVENT_STOP, NULL}, /* 0xfc */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xfd */ {SNDRV_SEQ_EVENT_SENSING, NULL}, /* 0xfe */ {SNDRV_SEQ_EVENT_RESET, NULL}, /* 0xff */ }; static int cvt_ump_system_to_event(const union snd_ump_midi1_msg *val, struct snd_seq_event *ev) { unsigned char status = val->system.status; if ((status & 0xf0) != UMP_MIDI1_MSG_REALTIME) return 0; /* invalid status - skip */ status &= 0x0f; ev->type = system_msg_encoders[status].seq_type; ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; if (ev->type == SNDRV_SEQ_EVENT_NONE) return 0; if (system_msg_encoders[status].encode) system_msg_encoders[status].encode(val, ev); return 1; } /* MIDI 2.0 CVM */ /* encode note event */ static int ump_midi2_to_note_ev(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { ev->data.note.channel = val->note.channel; ev->data.note.note = val->note.note; ev->data.note.velocity = downscale_16_to_7bit(val->note.velocity); /* correct note-on velocity 0 to 1; * it's no longer equivalent as not-off for MIDI 2.0 */ if (ev->type == SNDRV_SEQ_EVENT_NOTEON && !ev->data.note.velocity) ev->data.note.velocity = 1; return 1; } /* encode pitch wheel change */ static int ump_midi2_to_pitchbend_ev(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->pb.channel; ev->data.control.value = downscale_32_to_14bit(val->pb.data); ev->data.control.value -= 8192; return 1; } /* encode midi control change */ static int ump_midi2_to_cc_ev(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->cc.channel; ev->data.control.param = val->cc.index; ev->data.control.value = downscale_32_to_7bit(val->cc.data); return 1; } /* encode midi program change */ static int ump_midi2_to_pgm_ev(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { int size = 1; ev->data.control.channel = val->pg.channel; if (val->pg.bank_valid) { ev->type = SNDRV_SEQ_EVENT_CONTROL14; ev->data.control.param = UMP_CC_BANK_SELECT; ev->data.control.value = (val->pg.bank_msb << 7) | val->pg.bank_lsb; ev[1] = ev[0]; ev++; ev->type = SNDRV_SEQ_EVENT_PGMCHANGE; size = 2; } ev->data.control.value = val->pg.program; return size; } /* encode one parameter controls */ static int ump_midi2_to_ctrl_ev(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->caf.channel; ev->data.control.value = downscale_32_to_7bit(val->caf.data); return 1; } /* encode RPN/NRPN */ static int ump_midi2_to_rpn_ev(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { ev->data.control.channel = val->rpn.channel; ev->data.control.param = (val->rpn.bank << 7) | val->rpn.index; ev->data.control.value = downscale_32_to_14bit(val->rpn.data); return 1; } /* Encoding MIDI 2.0 UMP Packet */ struct seq_ump_midi2_to_ev { int seq_type; int (*encode)(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev); }; /* Encoders for MIDI2 status 0x00-0xf0 */ static struct seq_ump_midi2_to_ev midi2_msg_encoders[] = { {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0x00 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0x10 */ {SNDRV_SEQ_EVENT_REGPARAM, ump_midi2_to_rpn_ev}, /* 0x20 */ {SNDRV_SEQ_EVENT_NONREGPARAM, ump_midi2_to_rpn_ev}, /* 0x30 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0x40 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0x50 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0x60 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0x70 */ {SNDRV_SEQ_EVENT_NOTEOFF, ump_midi2_to_note_ev}, /* 0x80 */ {SNDRV_SEQ_EVENT_NOTEON, ump_midi2_to_note_ev}, /* 0x90 */ {SNDRV_SEQ_EVENT_KEYPRESS, ump_midi2_to_note_ev}, /* 0xa0 */ {SNDRV_SEQ_EVENT_CONTROLLER, ump_midi2_to_cc_ev}, /* 0xb0 */ {SNDRV_SEQ_EVENT_PGMCHANGE, ump_midi2_to_pgm_ev}, /* 0xc0 */ {SNDRV_SEQ_EVENT_CHANPRESS, ump_midi2_to_ctrl_ev}, /* 0xd0 */ {SNDRV_SEQ_EVENT_PITCHBEND, ump_midi2_to_pitchbend_ev}, /* 0xe0 */ {SNDRV_SEQ_EVENT_NONE, NULL}, /* 0xf0 */ }; static int cvt_ump_midi2_to_event(const union snd_ump_midi2_msg *val, struct snd_seq_event *ev) { unsigned char status = val->note.status; ev->type = midi2_msg_encoders[status].seq_type; if (ev->type == SNDRV_SEQ_EVENT_NONE) return 0; /* skip */ ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; return midi2_msg_encoders[status].encode(val, ev); } /* parse and compose for a sysex var-length event */ static int cvt_ump_sysex7_to_event(const u32 *data, unsigned char *buf, struct snd_seq_event *ev) { unsigned char status; unsigned char bytes; u32 val; int size = 0; val = data[0]; status = ump_sysex_message_status(val); bytes = ump_sysex_message_length(val); if (bytes > 6) return 0; // skip if (status == UMP_SYSEX_STATUS_SINGLE || status == UMP_SYSEX_STATUS_START) { buf[0] = UMP_MIDI1_MSG_SYSEX_START; size = 1; } if (bytes > 0) buf[size++] = (val >> 8) & 0x7f; if (bytes > 1) buf[size++] = val & 0x7f; val = data[1]; if (bytes > 2) buf[size++] = (val >> 24) & 0x7f; if (bytes > 3) buf[size++] = (val >> 16) & 0x7f; if (bytes > 4) buf[size++] = (val >> 8) & 0x7f; if (bytes > 5) buf[size++] = val & 0x7f; if (status == UMP_SYSEX_STATUS_SINGLE || status == UMP_SYSEX_STATUS_END) buf[size++] = UMP_MIDI1_MSG_SYSEX_END; ev->type = SNDRV_SEQ_EVENT_SYSEX; ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE; ev->data.ext.len = size; ev->data.ext.ptr = buf; return 1; } /* convert UMP packet from MIDI 1.0 to MIDI 2.0 and deliver it */ static int cvt_ump_midi1_to_midi2(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *__event, int atomic, int hop) { struct snd_seq_ump_event *event = (struct snd_seq_ump_event *)__event; struct snd_seq_ump_event ev_cvt; const union snd_ump_midi1_msg *midi1 = (const union snd_ump_midi1_msg *)event->ump; union snd_ump_midi2_msg *midi2 = (union snd_ump_midi2_msg *)ev_cvt.ump; ev_cvt = *event; memset(&ev_cvt.ump, 0, sizeof(ev_cvt.ump)); midi2->note.type = UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE; midi2->note.group = midi1->note.group; midi2->note.status = midi1->note.status; midi2->note.channel = midi1->note.channel; switch (midi1->note.status) { case UMP_MSG_STATUS_NOTE_ON: case UMP_MSG_STATUS_NOTE_OFF: midi2->note.note = midi1->note.note; midi2->note.velocity = upscale_7_to_16bit(midi1->note.velocity); break; case UMP_MSG_STATUS_POLY_PRESSURE: midi2->paf.note = midi1->paf.note; midi2->paf.data = upscale_7_to_32bit(midi1->paf.data); break; case UMP_MSG_STATUS_CC: midi2->cc.index = midi1->cc.index; midi2->cc.data = upscale_7_to_32bit(midi1->cc.data); break; case UMP_MSG_STATUS_PROGRAM: midi2->pg.program = midi1->pg.program; break; case UMP_MSG_STATUS_CHANNEL_PRESSURE: midi2->caf.data = upscale_7_to_32bit(midi1->caf.data); break; case UMP_MSG_STATUS_PITCH_BEND: midi2->pb.data = upscale_14_to_32bit((midi1->pb.data_msb << 7) | midi1->pb.data_lsb); break; default: return 0; } return __snd_seq_deliver_single_event(dest, dest_port, (struct snd_seq_event *)&ev_cvt, atomic, hop); } /* convert UMP packet from MIDI 2.0 to MIDI 1.0 and deliver it */ static int cvt_ump_midi2_to_midi1(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *__event, int atomic, int hop) { struct snd_seq_ump_event *event = (struct snd_seq_ump_event *)__event; struct snd_seq_ump_event ev_cvt; union snd_ump_midi1_msg *midi1 = (union snd_ump_midi1_msg *)ev_cvt.ump; const union snd_ump_midi2_msg *midi2 = (const union snd_ump_midi2_msg *)event->ump; u16 v; ev_cvt = *event; memset(&ev_cvt.ump, 0, sizeof(ev_cvt.ump)); midi1->note.type = UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE; midi1->note.group = midi2->note.group; midi1->note.status = midi2->note.status; midi1->note.channel = midi2->note.channel; switch (midi2->note.status << 4) { case UMP_MSG_STATUS_NOTE_ON: case UMP_MSG_STATUS_NOTE_OFF: midi1->note.note = midi2->note.note; midi1->note.velocity = downscale_16_to_7bit(midi2->note.velocity); break; case UMP_MSG_STATUS_POLY_PRESSURE: midi1->paf.note = midi2->paf.note; midi1->paf.data = downscale_32_to_7bit(midi2->paf.data); break; case UMP_MSG_STATUS_CC: midi1->cc.index = midi2->cc.index; midi1->cc.data = downscale_32_to_7bit(midi2->cc.data); break; case UMP_MSG_STATUS_PROGRAM: midi1->pg.program = midi2->pg.program; break; case UMP_MSG_STATUS_CHANNEL_PRESSURE: midi1->caf.data = downscale_32_to_7bit(midi2->caf.data); break; case UMP_MSG_STATUS_PITCH_BEND: v = downscale_32_to_14bit(midi2->pb.data); midi1->pb.data_msb = v >> 7; midi1->pb.data_lsb = v & 0x7f; break; default: return 0; } return __snd_seq_deliver_single_event(dest, dest_port, (struct snd_seq_event *)&ev_cvt, atomic, hop); } /* convert UMP to a legacy ALSA seq event and deliver it */ static int cvt_ump_to_any(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, unsigned char type, int atomic, int hop) { struct snd_seq_event ev_cvt[2]; /* up to two events */ struct snd_seq_ump_event *ump_ev = (struct snd_seq_ump_event *)event; /* use the second event as a temp buffer for saving stack usage */ unsigned char *sysex_buf = (unsigned char *)(ev_cvt + 1); unsigned char flags = event->flags & ~SNDRV_SEQ_EVENT_UMP; int i, len, err; ev_cvt[0] = ev_cvt[1] = *event; ev_cvt[0].flags = flags; ev_cvt[1].flags = flags; switch (type) { case UMP_MSG_TYPE_SYSTEM: len = cvt_ump_system_to_event((union snd_ump_midi1_msg *)ump_ev->ump, ev_cvt); break; case UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE: len = cvt_ump_midi1_to_event((union snd_ump_midi1_msg *)ump_ev->ump, ev_cvt); break; case UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE: len = cvt_ump_midi2_to_event((union snd_ump_midi2_msg *)ump_ev->ump, ev_cvt); break; case UMP_MSG_TYPE_DATA: len = cvt_ump_sysex7_to_event(ump_ev->ump, sysex_buf, ev_cvt); break; default: return 0; } for (i = 0; i < len; i++) { err = __snd_seq_deliver_single_event(dest, dest_port, &ev_cvt[i], atomic, hop); if (err < 0) return err; } return 0; } /* Replace UMP group field with the destination and deliver */ static int deliver_with_group_convert(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_ump_event *ump_ev, int atomic, int hop) { struct snd_seq_ump_event ev = *ump_ev; /* rewrite the group to the destination port */ ev.ump[0] &= ~(0xfU << 24); /* fill with the new group; the dest_port->ump_group field is 1-based */ ev.ump[0] |= ((dest_port->ump_group - 1) << 24); return __snd_seq_deliver_single_event(dest, dest_port, (struct snd_seq_event *)&ev, atomic, hop); } /* apply the UMP event filter; return true to skip the event */ static bool ump_event_filtered(struct snd_seq_client *dest, const struct snd_seq_ump_event *ev) { unsigned char group; group = ump_message_group(ev->ump[0]); if (ump_is_groupless_msg(ump_message_type(ev->ump[0]))) return dest->group_filter & (1U << 0); /* check the bitmap for 1-based group number */ return dest->group_filter & (1U << (group + 1)); } /* Convert from UMP packet and deliver */ int snd_seq_deliver_from_ump(struct snd_seq_client *source, struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, int atomic, int hop) { struct snd_seq_ump_event *ump_ev = (struct snd_seq_ump_event *)event; unsigned char type; if (snd_seq_ev_is_variable(event)) return 0; // skip, no variable event for UMP, so far if (ump_event_filtered(dest, ump_ev)) return 0; // skip if group filter is set and matching type = ump_message_type(ump_ev->ump[0]); if (snd_seq_client_is_ump(dest)) { if (snd_seq_client_is_midi2(dest) && type == UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE) return cvt_ump_midi1_to_midi2(dest, dest_port, event, atomic, hop); else if (!snd_seq_client_is_midi2(dest) && type == UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE) return cvt_ump_midi2_to_midi1(dest, dest_port, event, atomic, hop); /* non-EP port and different group is set? */ if (dest_port->ump_group && !ump_is_groupless_msg(type) && ump_message_group(*ump_ev->ump) + 1 != dest_port->ump_group) return deliver_with_group_convert(dest, dest_port, ump_ev, atomic, hop); /* copy as-is */ return __snd_seq_deliver_single_event(dest, dest_port, event, atomic, hop); } return cvt_ump_to_any(dest, dest_port, event, type, atomic, hop); } /* * MIDI1 sequencer event -> UMP conversion */ /* Conversion to UMP MIDI 1.0 */ /* convert note on/off event to MIDI 1.0 UMP */ static int note_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { if (!event->data.note.velocity) status = UMP_MSG_STATUS_NOTE_OFF; data->note.status = status; data->note.channel = event->data.note.channel & 0x0f; data->note.velocity = event->data.note.velocity & 0x7f; data->note.note = event->data.note.note & 0x7f; return 1; } /* convert CC event to MIDI 1.0 UMP */ static int cc_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { data->cc.status = status; data->cc.channel = event->data.control.channel & 0x0f; data->cc.index = event->data.control.param; data->cc.data = event->data.control.value; return 1; } /* convert one-parameter control event to MIDI 1.0 UMP */ static int ctrl_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { data->caf.status = status; data->caf.channel = event->data.control.channel & 0x0f; data->caf.data = event->data.control.value & 0x7f; return 1; } /* convert pitchbend event to MIDI 1.0 UMP */ static int pitchbend_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { int val = event->data.control.value + 8192; val = clamp(val, 0, 0x3fff); data->pb.status = status; data->pb.channel = event->data.control.channel & 0x0f; data->pb.data_msb = (val >> 7) & 0x7f; data->pb.data_lsb = val & 0x7f; return 1; } /* convert 14bit control event to MIDI 1.0 UMP; split to two events */ static int ctrl14_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { data->cc.status = UMP_MSG_STATUS_CC; data->cc.channel = event->data.control.channel & 0x0f; data->cc.index = event->data.control.param & 0x7f; if (event->data.control.param < 0x20) { data->cc.data = (event->data.control.value >> 7) & 0x7f; data[1] = data[0]; data[1].cc.index = event->data.control.param | 0x20; data[1].cc.data = event->data.control.value & 0x7f; return 2; } data->cc.data = event->data.control.value & 0x7f; return 1; } /* convert RPN/NRPN event to MIDI 1.0 UMP; split to four events */ static int rpn_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { bool is_rpn = (status == UMP_MSG_STATUS_RPN); data->cc.status = UMP_MSG_STATUS_CC; data->cc.channel = event->data.control.channel & 0x0f; data[1] = data[2] = data[3] = data[0]; data[0].cc.index = is_rpn ? UMP_CC_RPN_MSB : UMP_CC_NRPN_MSB; data[0].cc.data = (event->data.control.param >> 7) & 0x7f; data[1].cc.index = is_rpn ? UMP_CC_RPN_LSB : UMP_CC_NRPN_LSB; data[1].cc.data = event->data.control.param & 0x7f; data[2].cc.index = UMP_CC_DATA; data[2].cc.data = (event->data.control.value >> 7) & 0x7f; data[3].cc.index = UMP_CC_DATA_LSB; data[3].cc.data = event->data.control.value & 0x7f; return 4; } /* convert system / RT message to UMP */ static int system_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { data->system.status = status; return 1; } /* convert system / RT message with 1 parameter to UMP */ static int system_1p_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { data->system.status = status; data->system.parm1 = event->data.control.value & 0x7f; return 1; } /* convert system / RT message with two parameters to UMP */ static int system_2p_ev_to_ump_midi1(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status) { data->system.status = status; data->system.parm1 = (event->data.control.value >> 7) & 0x7f; data->system.parm2 = event->data.control.value & 0x7f; return 1; } /* Conversion to UMP MIDI 2.0 */ /* convert note on/off event to MIDI 2.0 UMP */ static int note_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { if (!event->data.note.velocity) status = UMP_MSG_STATUS_NOTE_OFF; data->note.status = status; data->note.channel = event->data.note.channel & 0x0f; data->note.note = event->data.note.note & 0x7f; data->note.velocity = upscale_7_to_16bit(event->data.note.velocity & 0x7f); return 1; } /* convert PAF event to MIDI 2.0 UMP */ static int paf_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { data->paf.status = status; data->paf.channel = event->data.note.channel & 0x0f; data->paf.note = event->data.note.note & 0x7f; data->paf.data = upscale_7_to_32bit(event->data.note.velocity & 0x7f); return 1; } /* set up the MIDI2 RPN/NRPN packet data from the parsed info */ static void fill_rpn(struct snd_seq_ump_midi2_bank *cc, union snd_ump_midi2_msg *data) { if (cc->rpn_set) { data->rpn.status = UMP_MSG_STATUS_RPN; data->rpn.bank = cc->cc_rpn_msb; data->rpn.index = cc->cc_rpn_lsb; cc->rpn_set = 0; cc->cc_rpn_msb = cc->cc_rpn_lsb = 0; } else { data->rpn.status = UMP_MSG_STATUS_NRPN; data->rpn.bank = cc->cc_nrpn_msb; data->rpn.index = cc->cc_nrpn_lsb; cc->nrpn_set = 0; cc->cc_nrpn_msb = cc->cc_nrpn_lsb = 0; } data->rpn.data = upscale_14_to_32bit((cc->cc_data_msb << 7) | cc->cc_data_lsb); cc->cc_data_msb = cc->cc_data_lsb = 0; } /* convert CC event to MIDI 2.0 UMP */ static int cc_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { unsigned char channel = event->data.control.channel & 0x0f; unsigned char index = event->data.control.param & 0x7f; unsigned char val = event->data.control.value & 0x7f; struct snd_seq_ump_midi2_bank *cc = &dest_port->midi2_bank[channel]; /* process special CC's (bank/rpn/nrpn) */ switch (index) { case UMP_CC_RPN_MSB: cc->rpn_set = 1; cc->cc_rpn_msb = val; return 0; // skip case UMP_CC_RPN_LSB: cc->rpn_set = 1; cc->cc_rpn_lsb = val; return 0; // skip case UMP_CC_NRPN_MSB: cc->nrpn_set = 1; cc->cc_nrpn_msb = val; return 0; // skip case UMP_CC_NRPN_LSB: cc->nrpn_set = 1; cc->cc_nrpn_lsb = val; return 0; // skip case UMP_CC_DATA: cc->cc_data_msb = val; return 0; // skip case UMP_CC_BANK_SELECT: cc->bank_set = 1; cc->cc_bank_msb = val; return 0; // skip case UMP_CC_BANK_SELECT_LSB: cc->bank_set = 1; cc->cc_bank_lsb = val; return 0; // skip case UMP_CC_DATA_LSB: cc->cc_data_lsb = val; if (!(cc->rpn_set || cc->nrpn_set)) return 0; // skip fill_rpn(cc, data); return 1; } data->cc.status = status; data->cc.channel = channel; data->cc.index = index; data->cc.data = upscale_7_to_32bit(event->data.control.value & 0x7f); return 1; } /* convert one-parameter control event to MIDI 2.0 UMP */ static int ctrl_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { data->caf.status = status; data->caf.channel = event->data.control.channel & 0x0f; data->caf.data = upscale_7_to_32bit(event->data.control.value & 0x7f); return 1; } /* convert program change event to MIDI 2.0 UMP */ static int pgm_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { unsigned char channel = event->data.control.channel & 0x0f; struct snd_seq_ump_midi2_bank *cc = &dest_port->midi2_bank[channel]; data->pg.status = status; data->pg.channel = channel; data->pg.program = event->data.control.value & 0x7f; if (cc->bank_set) { data->pg.bank_valid = 1; data->pg.bank_msb = cc->cc_bank_msb; data->pg.bank_lsb = cc->cc_bank_lsb; cc->bank_set = 0; cc->cc_bank_msb = cc->cc_bank_lsb = 0; } return 1; } /* convert pitchbend event to MIDI 2.0 UMP */ static int pitchbend_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { int val = event->data.control.value + 8192; val = clamp(val, 0, 0x3fff); data->pb.status = status; data->pb.channel = event->data.control.channel & 0x0f; data->pb.data = upscale_14_to_32bit(val); return 1; } /* convert 14bit control event to MIDI 2.0 UMP; split to two events */ static int ctrl14_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { unsigned char channel = event->data.control.channel & 0x0f; unsigned char index = event->data.control.param & 0x7f; struct snd_seq_ump_midi2_bank *cc = &dest_port->midi2_bank[channel]; unsigned char msb, lsb; msb = (event->data.control.value >> 7) & 0x7f; lsb = event->data.control.value & 0x7f; /* process special CC's (bank/rpn/nrpn) */ switch (index) { case UMP_CC_BANK_SELECT: cc->cc_bank_msb = msb; fallthrough; case UMP_CC_BANK_SELECT_LSB: cc->bank_set = 1; cc->cc_bank_lsb = lsb; return 0; // skip case UMP_CC_RPN_MSB: cc->cc_rpn_msb = msb; fallthrough; case UMP_CC_RPN_LSB: cc->rpn_set = 1; cc->cc_rpn_lsb = lsb; return 0; // skip case UMP_CC_NRPN_MSB: cc->cc_nrpn_msb = msb; fallthrough; case UMP_CC_NRPN_LSB: cc->nrpn_set = 1; cc->cc_nrpn_lsb = lsb; return 0; // skip case UMP_CC_DATA: cc->cc_data_msb = msb; fallthrough; case UMP_CC_DATA_LSB: cc->cc_data_lsb = lsb; if (!(cc->rpn_set || cc->nrpn_set)) return 0; // skip fill_rpn(cc, data); return 1; } data->cc.status = UMP_MSG_STATUS_CC; data->cc.channel = channel; data->cc.index = index; if (event->data.control.param < 0x20) { data->cc.data = upscale_7_to_32bit(msb); data[1] = data[0]; data[1].cc.index = event->data.control.param | 0x20; data[1].cc.data = upscale_7_to_32bit(lsb); return 2; } data->cc.data = upscale_7_to_32bit(lsb); return 1; } /* convert RPN/NRPN event to MIDI 2.0 UMP */ static int rpn_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { data->rpn.status = status; data->rpn.channel = event->data.control.channel; data->rpn.bank = (event->data.control.param >> 7) & 0x7f; data->rpn.index = event->data.control.param & 0x7f; data->rpn.data = upscale_14_to_32bit(event->data.control.value & 0x3fff); return 1; } /* convert system / RT message to UMP */ static int system_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { return system_ev_to_ump_midi1(event, dest_port, (union snd_ump_midi1_msg *)data, status); } /* convert system / RT message with 1 parameter to UMP */ static int system_1p_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { return system_1p_ev_to_ump_midi1(event, dest_port, (union snd_ump_midi1_msg *)data, status); } /* convert system / RT message with two parameters to UMP */ static int system_2p_ev_to_ump_midi2(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status) { return system_1p_ev_to_ump_midi1(event, dest_port, (union snd_ump_midi1_msg *)data, status); } struct seq_ev_to_ump { int seq_type; unsigned char status; int (*midi1_encode)(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi1_msg *data, unsigned char status); int (*midi2_encode)(const struct snd_seq_event *event, struct snd_seq_client_port *dest_port, union snd_ump_midi2_msg *data, unsigned char status); }; static const struct seq_ev_to_ump seq_ev_ump_encoders[] = { { SNDRV_SEQ_EVENT_NOTEON, UMP_MSG_STATUS_NOTE_ON, note_ev_to_ump_midi1, note_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_NOTEOFF, UMP_MSG_STATUS_NOTE_OFF, note_ev_to_ump_midi1, note_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_KEYPRESS, UMP_MSG_STATUS_POLY_PRESSURE, note_ev_to_ump_midi1, paf_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_CONTROLLER, UMP_MSG_STATUS_CC, cc_ev_to_ump_midi1, cc_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_PGMCHANGE, UMP_MSG_STATUS_PROGRAM, ctrl_ev_to_ump_midi1, pgm_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_CHANPRESS, UMP_MSG_STATUS_CHANNEL_PRESSURE, ctrl_ev_to_ump_midi1, ctrl_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_PITCHBEND, UMP_MSG_STATUS_PITCH_BEND, pitchbend_ev_to_ump_midi1, pitchbend_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_CONTROL14, 0, ctrl14_ev_to_ump_midi1, ctrl14_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_NONREGPARAM, UMP_MSG_STATUS_NRPN, rpn_ev_to_ump_midi1, rpn_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_REGPARAM, UMP_MSG_STATUS_RPN, rpn_ev_to_ump_midi1, rpn_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_QFRAME, UMP_SYSTEM_STATUS_MIDI_TIME_CODE, system_1p_ev_to_ump_midi1, system_1p_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_SONGPOS, UMP_SYSTEM_STATUS_SONG_POSITION, system_2p_ev_to_ump_midi1, system_2p_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_SONGSEL, UMP_SYSTEM_STATUS_SONG_SELECT, system_1p_ev_to_ump_midi1, system_1p_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_TUNE_REQUEST, UMP_SYSTEM_STATUS_TUNE_REQUEST, system_ev_to_ump_midi1, system_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_CLOCK, UMP_SYSTEM_STATUS_TIMING_CLOCK, system_ev_to_ump_midi1, system_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_START, UMP_SYSTEM_STATUS_START, system_ev_to_ump_midi1, system_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_CONTINUE, UMP_SYSTEM_STATUS_CONTINUE, system_ev_to_ump_midi1, system_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_STOP, UMP_SYSTEM_STATUS_STOP, system_ev_to_ump_midi1, system_ev_to_ump_midi2 }, { SNDRV_SEQ_EVENT_SENSING, UMP_SYSTEM_STATUS_ACTIVE_SENSING, system_ev_to_ump_midi1, system_ev_to_ump_midi2 }, }; static const struct seq_ev_to_ump *find_ump_encoder(int type) { int i; for (i = 0; i < ARRAY_SIZE(seq_ev_ump_encoders); i++) if (seq_ev_ump_encoders[i].seq_type == type) return &seq_ev_ump_encoders[i]; return NULL; } static void setup_ump_event(struct snd_seq_ump_event *dest, const struct snd_seq_event *src) { memcpy(dest, src, sizeof(*src)); dest->type = 0; dest->flags |= SNDRV_SEQ_EVENT_UMP; dest->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; memset(dest->ump, 0, sizeof(dest->ump)); } /* Convert ALSA seq event to UMP MIDI 1.0 and deliver it */ static int cvt_to_ump_midi1(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, int atomic, int hop) { const struct seq_ev_to_ump *encoder; struct snd_seq_ump_event ev_cvt; union snd_ump_midi1_msg data[4]; int i, n, err; encoder = find_ump_encoder(event->type); if (!encoder) return __snd_seq_deliver_single_event(dest, dest_port, event, atomic, hop); data->raw = make_raw_ump(dest_port, UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE); n = encoder->midi1_encode(event, dest_port, data, encoder->status); if (!n) return 0; setup_ump_event(&ev_cvt, event); for (i = 0; i < n; i++) { ev_cvt.ump[0] = data[i].raw; err = __snd_seq_deliver_single_event(dest, dest_port, (struct snd_seq_event *)&ev_cvt, atomic, hop); if (err < 0) return err; } return 0; } /* Convert ALSA seq event to UMP MIDI 2.0 and deliver it */ static int cvt_to_ump_midi2(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, int atomic, int hop) { const struct seq_ev_to_ump *encoder; struct snd_seq_ump_event ev_cvt; union snd_ump_midi2_msg data[2]; int i, n, err; encoder = find_ump_encoder(event->type); if (!encoder) return __snd_seq_deliver_single_event(dest, dest_port, event, atomic, hop); data->raw[0] = make_raw_ump(dest_port, UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE); data->raw[1] = 0; n = encoder->midi2_encode(event, dest_port, data, encoder->status); if (!n) return 0; setup_ump_event(&ev_cvt, event); for (i = 0; i < n; i++) { memcpy(ev_cvt.ump, &data[i], sizeof(data[i])); err = __snd_seq_deliver_single_event(dest, dest_port, (struct snd_seq_event *)&ev_cvt, atomic, hop); if (err < 0) return err; } return 0; } /* Fill up a sysex7 UMP from the byte stream */ static void fill_sysex7_ump(struct snd_seq_client_port *dest_port, u32 *val, u8 status, u8 *buf, int len) { memset(val, 0, 8); memcpy((u8 *)val + 2, buf, len); #ifdef __LITTLE_ENDIAN swab32_array(val, 2); #endif val[0] |= ump_compose(UMP_MSG_TYPE_DATA, get_ump_group(dest_port), status, len); } /* Convert sysex var event to UMP sysex7 packets and deliver them */ static int cvt_sysex_to_ump(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, int atomic, int hop) { struct snd_seq_ump_event ev_cvt; unsigned char status; u8 buf[6], *xbuf; int offset = 0; int len, err; if (!snd_seq_ev_is_variable(event)) return 0; setup_ump_event(&ev_cvt, event); for (;;) { len = snd_seq_expand_var_event_at(event, sizeof(buf), buf, offset); if (len <= 0) break; if (WARN_ON(len > 6)) break; offset += len; xbuf = buf; if (*xbuf == UMP_MIDI1_MSG_SYSEX_START) { status = UMP_SYSEX_STATUS_START; xbuf++; len--; if (len > 0 && xbuf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) { status = UMP_SYSEX_STATUS_SINGLE; len--; } } else { if (xbuf[len - 1] == UMP_MIDI1_MSG_SYSEX_END) { status = UMP_SYSEX_STATUS_END; len--; } else { status = UMP_SYSEX_STATUS_CONTINUE; } } fill_sysex7_ump(dest_port, ev_cvt.ump, status, xbuf, len); err = __snd_seq_deliver_single_event(dest, dest_port, (struct snd_seq_event *)&ev_cvt, atomic, hop); if (err < 0) return err; } return 0; } /* Convert to UMP packet and deliver */ int snd_seq_deliver_to_ump(struct snd_seq_client *source, struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, int atomic, int hop) { if (dest->group_filter & (1U << dest_port->ump_group)) return 0; /* group filtered - skip the event */ if (event->type == SNDRV_SEQ_EVENT_SYSEX) return cvt_sysex_to_ump(dest, dest_port, event, atomic, hop); else if (snd_seq_client_is_midi2(dest)) return cvt_to_ump_midi2(dest, dest_port, event, atomic, hop); else return cvt_to_ump_midi1(dest, dest_port, event, atomic, hop); }
linux-master
sound/core/seq/seq_ump_convert.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer Timing queue handling * Copyright (c) 1998-1999 by Frank van de Pol <[email protected]> * * MAJOR CHANGES * Nov. 13, 1999 Takashi Iwai <[email protected]> * - Queues are allocated dynamically via ioctl. * - When owner client is deleted, all owned queues are deleted, too. * - Owner of unlocked queue is kept unmodified even if it is * manipulated by other clients. * - Owner field in SET_QUEUE_OWNER ioctl must be identical with the * caller client. i.e. Changing owner to a third client is not * allowed. * * Aug. 30, 2000 Takashi Iwai * - Queues are managed in static array again, but with better way. * The API itself is identical. * - The queue is locked when struct snd_seq_queue pointer is returned via * queueptr(). This pointer *MUST* be released afterward by * queuefree(ptr). * - Addition of experimental sync support. */ #include <linux/init.h> #include <linux/slab.h> #include <sound/core.h> #include "seq_memory.h" #include "seq_queue.h" #include "seq_clientmgr.h" #include "seq_fifo.h" #include "seq_timer.h" #include "seq_info.h" /* list of allocated queues */ static struct snd_seq_queue *queue_list[SNDRV_SEQ_MAX_QUEUES]; static DEFINE_SPINLOCK(queue_list_lock); /* number of queues allocated */ static int num_queues; int snd_seq_queue_get_cur_queues(void) { return num_queues; } /*----------------------------------------------------------------*/ /* assign queue id and insert to list */ static int queue_list_add(struct snd_seq_queue *q) { int i; unsigned long flags; spin_lock_irqsave(&queue_list_lock, flags); for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { if (! queue_list[i]) { queue_list[i] = q; q->queue = i; num_queues++; spin_unlock_irqrestore(&queue_list_lock, flags); return i; } } spin_unlock_irqrestore(&queue_list_lock, flags); return -1; } static struct snd_seq_queue *queue_list_remove(int id, int client) { struct snd_seq_queue *q; unsigned long flags; spin_lock_irqsave(&queue_list_lock, flags); q = queue_list[id]; if (q) { spin_lock(&q->owner_lock); if (q->owner == client) { /* found */ q->klocked = 1; spin_unlock(&q->owner_lock); queue_list[id] = NULL; num_queues--; spin_unlock_irqrestore(&queue_list_lock, flags); return q; } spin_unlock(&q->owner_lock); } spin_unlock_irqrestore(&queue_list_lock, flags); return NULL; } /*----------------------------------------------------------------*/ /* create new queue (constructor) */ static struct snd_seq_queue *queue_new(int owner, int locked) { struct snd_seq_queue *q; q = kzalloc(sizeof(*q), GFP_KERNEL); if (!q) return NULL; spin_lock_init(&q->owner_lock); spin_lock_init(&q->check_lock); mutex_init(&q->timer_mutex); snd_use_lock_init(&q->use_lock); q->queue = -1; q->tickq = snd_seq_prioq_new(); q->timeq = snd_seq_prioq_new(); q->timer = snd_seq_timer_new(); if (q->tickq == NULL || q->timeq == NULL || q->timer == NULL) { snd_seq_prioq_delete(&q->tickq); snd_seq_prioq_delete(&q->timeq); snd_seq_timer_delete(&q->timer); kfree(q); return NULL; } q->owner = owner; q->locked = locked; q->klocked = 0; return q; } /* delete queue (destructor) */ static void queue_delete(struct snd_seq_queue *q) { /* stop and release the timer */ mutex_lock(&q->timer_mutex); snd_seq_timer_stop(q->timer); snd_seq_timer_close(q); mutex_unlock(&q->timer_mutex); /* wait until access free */ snd_use_lock_sync(&q->use_lock); /* release resources... */ snd_seq_prioq_delete(&q->tickq); snd_seq_prioq_delete(&q->timeq); snd_seq_timer_delete(&q->timer); kfree(q); } /*----------------------------------------------------------------*/ /* delete all existing queues */ void snd_seq_queues_delete(void) { int i; /* clear list */ for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { if (queue_list[i]) queue_delete(queue_list[i]); } } static void queue_use(struct snd_seq_queue *queue, int client, int use); /* allocate a new queue - * return pointer to new queue or ERR_PTR(-errno) for error * The new queue's use_lock is set to 1. It is the caller's responsibility to * call snd_use_lock_free(&q->use_lock). */ struct snd_seq_queue *snd_seq_queue_alloc(int client, int locked, unsigned int info_flags) { struct snd_seq_queue *q; q = queue_new(client, locked); if (q == NULL) return ERR_PTR(-ENOMEM); q->info_flags = info_flags; queue_use(q, client, 1); snd_use_lock_use(&q->use_lock); if (queue_list_add(q) < 0) { snd_use_lock_free(&q->use_lock); queue_delete(q); return ERR_PTR(-ENOMEM); } return q; } /* delete a queue - queue must be owned by the client */ int snd_seq_queue_delete(int client, int queueid) { struct snd_seq_queue *q; if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES) return -EINVAL; q = queue_list_remove(queueid, client); if (q == NULL) return -EINVAL; queue_delete(q); return 0; } /* return pointer to queue structure for specified id */ struct snd_seq_queue *queueptr(int queueid) { struct snd_seq_queue *q; unsigned long flags; if (queueid < 0 || queueid >= SNDRV_SEQ_MAX_QUEUES) return NULL; spin_lock_irqsave(&queue_list_lock, flags); q = queue_list[queueid]; if (q) snd_use_lock_use(&q->use_lock); spin_unlock_irqrestore(&queue_list_lock, flags); return q; } /* return the (first) queue matching with the specified name */ struct snd_seq_queue *snd_seq_queue_find_name(char *name) { int i; struct snd_seq_queue *q; for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { q = queueptr(i); if (q) { if (strncmp(q->name, name, sizeof(q->name)) == 0) return q; queuefree(q); } } return NULL; } /* -------------------------------------------------------- */ #define MAX_CELL_PROCESSES_IN_QUEUE 1000 void snd_seq_check_queue(struct snd_seq_queue *q, int atomic, int hop) { unsigned long flags; struct snd_seq_event_cell *cell; snd_seq_tick_time_t cur_tick; snd_seq_real_time_t cur_time; int processed = 0; if (q == NULL) return; /* make this function non-reentrant */ spin_lock_irqsave(&q->check_lock, flags); if (q->check_blocked) { q->check_again = 1; spin_unlock_irqrestore(&q->check_lock, flags); return; /* other thread is already checking queues */ } q->check_blocked = 1; spin_unlock_irqrestore(&q->check_lock, flags); __again: /* Process tick queue... */ cur_tick = snd_seq_timer_get_cur_tick(q->timer); for (;;) { cell = snd_seq_prioq_cell_out(q->tickq, &cur_tick); if (!cell) break; snd_seq_dispatch_event(cell, atomic, hop); if (++processed >= MAX_CELL_PROCESSES_IN_QUEUE) goto out; /* the rest processed at the next batch */ } /* Process time queue... */ cur_time = snd_seq_timer_get_cur_time(q->timer, false); for (;;) { cell = snd_seq_prioq_cell_out(q->timeq, &cur_time); if (!cell) break; snd_seq_dispatch_event(cell, atomic, hop); if (++processed >= MAX_CELL_PROCESSES_IN_QUEUE) goto out; /* the rest processed at the next batch */ } out: /* free lock */ spin_lock_irqsave(&q->check_lock, flags); if (q->check_again) { q->check_again = 0; if (processed < MAX_CELL_PROCESSES_IN_QUEUE) { spin_unlock_irqrestore(&q->check_lock, flags); goto __again; } } q->check_blocked = 0; spin_unlock_irqrestore(&q->check_lock, flags); } /* enqueue a event to singe queue */ int snd_seq_enqueue_event(struct snd_seq_event_cell *cell, int atomic, int hop) { int dest, err; struct snd_seq_queue *q; if (snd_BUG_ON(!cell)) return -EINVAL; dest = cell->event.queue; /* destination queue */ q = queueptr(dest); if (q == NULL) return -EINVAL; /* handle relative time stamps, convert them into absolute */ if ((cell->event.flags & SNDRV_SEQ_TIME_MODE_MASK) == SNDRV_SEQ_TIME_MODE_REL) { switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) { case SNDRV_SEQ_TIME_STAMP_TICK: cell->event.time.tick += q->timer->tick.cur_tick; break; case SNDRV_SEQ_TIME_STAMP_REAL: snd_seq_inc_real_time(&cell->event.time.time, &q->timer->cur_time); break; } cell->event.flags &= ~SNDRV_SEQ_TIME_MODE_MASK; cell->event.flags |= SNDRV_SEQ_TIME_MODE_ABS; } /* enqueue event in the real-time or midi queue */ switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) { case SNDRV_SEQ_TIME_STAMP_TICK: err = snd_seq_prioq_cell_in(q->tickq, cell); break; case SNDRV_SEQ_TIME_STAMP_REAL: default: err = snd_seq_prioq_cell_in(q->timeq, cell); break; } if (err < 0) { queuefree(q); /* unlock */ return err; } /* trigger dispatching */ snd_seq_check_queue(q, atomic, hop); queuefree(q); /* unlock */ return 0; } /*----------------------------------------------------------------*/ static inline int check_access(struct snd_seq_queue *q, int client) { return (q->owner == client) || (!q->locked && !q->klocked); } /* check if the client has permission to modify queue parameters. * if it does, lock the queue */ static int queue_access_lock(struct snd_seq_queue *q, int client) { unsigned long flags; int access_ok; spin_lock_irqsave(&q->owner_lock, flags); access_ok = check_access(q, client); if (access_ok) q->klocked = 1; spin_unlock_irqrestore(&q->owner_lock, flags); return access_ok; } /* unlock the queue */ static inline void queue_access_unlock(struct snd_seq_queue *q) { unsigned long flags; spin_lock_irqsave(&q->owner_lock, flags); q->klocked = 0; spin_unlock_irqrestore(&q->owner_lock, flags); } /* exported - only checking permission */ int snd_seq_queue_check_access(int queueid, int client) { struct snd_seq_queue *q = queueptr(queueid); int access_ok; unsigned long flags; if (! q) return 0; spin_lock_irqsave(&q->owner_lock, flags); access_ok = check_access(q, client); spin_unlock_irqrestore(&q->owner_lock, flags); queuefree(q); return access_ok; } /*----------------------------------------------------------------*/ /* * change queue's owner and permission */ int snd_seq_queue_set_owner(int queueid, int client, int locked) { struct snd_seq_queue *q = queueptr(queueid); unsigned long flags; if (q == NULL) return -EINVAL; if (! queue_access_lock(q, client)) { queuefree(q); return -EPERM; } spin_lock_irqsave(&q->owner_lock, flags); q->locked = locked ? 1 : 0; q->owner = client; spin_unlock_irqrestore(&q->owner_lock, flags); queue_access_unlock(q); queuefree(q); return 0; } /*----------------------------------------------------------------*/ /* open timer - * q->use mutex should be down before calling this function to avoid * confliction with snd_seq_queue_use() */ int snd_seq_queue_timer_open(int queueid) { int result = 0; struct snd_seq_queue *queue; struct snd_seq_timer *tmr; queue = queueptr(queueid); if (queue == NULL) return -EINVAL; tmr = queue->timer; result = snd_seq_timer_open(queue); if (result < 0) { snd_seq_timer_defaults(tmr); result = snd_seq_timer_open(queue); } queuefree(queue); return result; } /* close timer - * q->use mutex should be down before calling this function */ int snd_seq_queue_timer_close(int queueid) { struct snd_seq_queue *queue; int result = 0; queue = queueptr(queueid); if (queue == NULL) return -EINVAL; snd_seq_timer_close(queue); queuefree(queue); return result; } /* change queue tempo and ppq */ int snd_seq_queue_timer_set_tempo(int queueid, int client, struct snd_seq_queue_tempo *info) { struct snd_seq_queue *q = queueptr(queueid); int result; if (q == NULL) return -EINVAL; if (! queue_access_lock(q, client)) { queuefree(q); return -EPERM; } result = snd_seq_timer_set_tempo_ppq(q->timer, info->tempo, info->ppq); if (result >= 0 && info->skew_base > 0) result = snd_seq_timer_set_skew(q->timer, info->skew_value, info->skew_base); queue_access_unlock(q); queuefree(q); return result; } /* use or unuse this queue */ static void queue_use(struct snd_seq_queue *queue, int client, int use) { if (use) { if (!test_and_set_bit(client, queue->clients_bitmap)) queue->clients++; } else { if (test_and_clear_bit(client, queue->clients_bitmap)) queue->clients--; } if (queue->clients) { if (use && queue->clients == 1) snd_seq_timer_defaults(queue->timer); snd_seq_timer_open(queue); } else { snd_seq_timer_close(queue); } } /* use or unuse this queue - * if it is the first client, starts the timer. * if it is not longer used by any clients, stop the timer. */ int snd_seq_queue_use(int queueid, int client, int use) { struct snd_seq_queue *queue; queue = queueptr(queueid); if (queue == NULL) return -EINVAL; mutex_lock(&queue->timer_mutex); queue_use(queue, client, use); mutex_unlock(&queue->timer_mutex); queuefree(queue); return 0; } /* * check if queue is used by the client * return negative value if the queue is invalid. * return 0 if not used, 1 if used. */ int snd_seq_queue_is_used(int queueid, int client) { struct snd_seq_queue *q; int result; q = queueptr(queueid); if (q == NULL) return -EINVAL; /* invalid queue */ result = test_bit(client, q->clients_bitmap) ? 1 : 0; queuefree(q); return result; } /*----------------------------------------------------------------*/ /* final stage notification - * remove cells for no longer exist client (for non-owned queue) * or delete this queue (for owned queue) */ void snd_seq_queue_client_leave(int client) { int i; struct snd_seq_queue *q; /* delete own queues from queue list */ for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { q = queue_list_remove(i, client); if (q) queue_delete(q); } /* remove cells from existing queues - * they are not owned by this client */ for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { q = queueptr(i); if (!q) continue; if (test_bit(client, q->clients_bitmap)) { snd_seq_prioq_leave(q->tickq, client, 0); snd_seq_prioq_leave(q->timeq, client, 0); snd_seq_queue_use(q->queue, client, 0); } queuefree(q); } } /*----------------------------------------------------------------*/ /* remove cells from all queues */ void snd_seq_queue_client_leave_cells(int client) { int i; struct snd_seq_queue *q; for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { q = queueptr(i); if (!q) continue; snd_seq_prioq_leave(q->tickq, client, 0); snd_seq_prioq_leave(q->timeq, client, 0); queuefree(q); } } /* remove cells based on flush criteria */ void snd_seq_queue_remove_cells(int client, struct snd_seq_remove_events *info) { int i; struct snd_seq_queue *q; for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { q = queueptr(i); if (!q) continue; if (test_bit(client, q->clients_bitmap) && (! (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) || q->queue == info->queue)) { snd_seq_prioq_remove_events(q->tickq, client, info); snd_seq_prioq_remove_events(q->timeq, client, info); } queuefree(q); } } /*----------------------------------------------------------------*/ /* * send events to all subscribed ports */ static void queue_broadcast_event(struct snd_seq_queue *q, struct snd_seq_event *ev, int atomic, int hop) { struct snd_seq_event sev; sev = *ev; sev.flags = SNDRV_SEQ_TIME_STAMP_TICK|SNDRV_SEQ_TIME_MODE_ABS; sev.time.tick = q->timer->tick.cur_tick; sev.queue = q->queue; sev.data.queue.queue = q->queue; /* broadcast events from Timer port */ sev.source.client = SNDRV_SEQ_CLIENT_SYSTEM; sev.source.port = SNDRV_SEQ_PORT_SYSTEM_TIMER; sev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; snd_seq_kernel_client_dispatch(SNDRV_SEQ_CLIENT_SYSTEM, &sev, atomic, hop); } /* * process a received queue-control event. * this function is exported for seq_sync.c. */ static void snd_seq_queue_process_event(struct snd_seq_queue *q, struct snd_seq_event *ev, int atomic, int hop) { switch (ev->type) { case SNDRV_SEQ_EVENT_START: snd_seq_prioq_leave(q->tickq, ev->source.client, 1); snd_seq_prioq_leave(q->timeq, ev->source.client, 1); if (! snd_seq_timer_start(q->timer)) queue_broadcast_event(q, ev, atomic, hop); break; case SNDRV_SEQ_EVENT_CONTINUE: if (! snd_seq_timer_continue(q->timer)) queue_broadcast_event(q, ev, atomic, hop); break; case SNDRV_SEQ_EVENT_STOP: snd_seq_timer_stop(q->timer); queue_broadcast_event(q, ev, atomic, hop); break; case SNDRV_SEQ_EVENT_TEMPO: snd_seq_timer_set_tempo(q->timer, ev->data.queue.param.value); queue_broadcast_event(q, ev, atomic, hop); break; case SNDRV_SEQ_EVENT_SETPOS_TICK: if (snd_seq_timer_set_position_tick(q->timer, ev->data.queue.param.time.tick) == 0) { queue_broadcast_event(q, ev, atomic, hop); } break; case SNDRV_SEQ_EVENT_SETPOS_TIME: if (snd_seq_timer_set_position_time(q->timer, ev->data.queue.param.time.time) == 0) { queue_broadcast_event(q, ev, atomic, hop); } break; case SNDRV_SEQ_EVENT_QUEUE_SKEW: if (snd_seq_timer_set_skew(q->timer, ev->data.queue.param.skew.value, ev->data.queue.param.skew.base) == 0) { queue_broadcast_event(q, ev, atomic, hop); } break; } } /* * Queue control via timer control port: * this function is exported as a callback of timer port. */ int snd_seq_control_queue(struct snd_seq_event *ev, int atomic, int hop) { struct snd_seq_queue *q; if (snd_BUG_ON(!ev)) return -EINVAL; q = queueptr(ev->data.queue.queue); if (q == NULL) return -EINVAL; if (! queue_access_lock(q, ev->source.client)) { queuefree(q); return -EPERM; } snd_seq_queue_process_event(q, ev, atomic, hop); queue_access_unlock(q); queuefree(q); return 0; } /*----------------------------------------------------------------*/ #ifdef CONFIG_SND_PROC_FS /* exported to seq_info.c */ void snd_seq_info_queues_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { int i, bpm; struct snd_seq_queue *q; struct snd_seq_timer *tmr; bool locked; int owner; for (i = 0; i < SNDRV_SEQ_MAX_QUEUES; i++) { q = queueptr(i); if (!q) continue; tmr = q->timer; if (tmr->tempo) bpm = 60000000 / tmr->tempo; else bpm = 0; spin_lock_irq(&q->owner_lock); locked = q->locked; owner = q->owner; spin_unlock_irq(&q->owner_lock); snd_iprintf(buffer, "queue %d: [%s]\n", q->queue, q->name); snd_iprintf(buffer, "owned by client : %d\n", owner); snd_iprintf(buffer, "lock status : %s\n", locked ? "Locked" : "Free"); snd_iprintf(buffer, "queued time events : %d\n", snd_seq_prioq_avail(q->timeq)); snd_iprintf(buffer, "queued tick events : %d\n", snd_seq_prioq_avail(q->tickq)); snd_iprintf(buffer, "timer state : %s\n", tmr->running ? "Running" : "Stopped"); snd_iprintf(buffer, "timer PPQ : %d\n", tmr->ppq); snd_iprintf(buffer, "current tempo : %d\n", tmr->tempo); snd_iprintf(buffer, "current BPM : %d\n", bpm); snd_iprintf(buffer, "current time : %d.%09d s\n", tmr->cur_time.tv_sec, tmr->cur_time.tv_nsec); snd_iprintf(buffer, "current tick : %d\n", tmr->tick.cur_tick); snd_iprintf(buffer, "\n"); queuefree(q); } } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/seq_queue.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Do sleep inside a spin-lock * Copyright (c) 1999 by Takashi Iwai <[email protected]> */ #include <linux/export.h> #include <sound/core.h> #include "seq_lock.h" /* wait until all locks are released */ void snd_use_lock_sync_helper(snd_use_lock_t *lockp, const char *file, int line) { int warn_count = 5 * HZ; if (atomic_read(lockp) < 0) { pr_warn("ALSA: seq_lock: lock trouble [counter = %d] in %s:%d\n", atomic_read(lockp), file, line); return; } while (atomic_read(lockp) > 0) { if (warn_count-- == 0) pr_warn("ALSA: seq_lock: waiting [%d left] in %s:%d\n", atomic_read(lockp), file, line); schedule_timeout_uninterruptible(1); } } EXPORT_SYMBOL(snd_use_lock_sync_helper);
linux-master
sound/core/seq/seq_lock.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer Client Manager * Copyright (c) 1998-2001 by Frank van de Pol <[email protected]> * Jaroslav Kysela <[email protected]> * Takashi Iwai <[email protected]> */ #include <linux/init.h> #include <linux/export.h> #include <linux/slab.h> #include <sound/core.h> #include <sound/minors.h> #include <linux/kmod.h> #include <sound/seq_kernel.h> #include <sound/ump.h> #include "seq_clientmgr.h" #include "seq_memory.h" #include "seq_queue.h" #include "seq_timer.h" #include "seq_info.h" #include "seq_system.h" #include "seq_ump_convert.h" #include <sound/seq_device.h> #ifdef CONFIG_COMPAT #include <linux/compat.h> #endif /* Client Manager * this module handles the connections of userland and kernel clients * */ /* * There are four ranges of client numbers (last two shared): * 0..15: global clients * 16..127: statically allocated client numbers for cards 0..27 * 128..191: dynamically allocated client numbers for cards 28..31 * 128..191: dynamically allocated client numbers for applications */ /* number of kernel non-card clients */ #define SNDRV_SEQ_GLOBAL_CLIENTS 16 /* clients per cards, for static clients */ #define SNDRV_SEQ_CLIENTS_PER_CARD 4 /* dynamically allocated client numbers (both kernel drivers and user space) */ #define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 128 #define SNDRV_SEQ_LFLG_INPUT 0x0001 #define SNDRV_SEQ_LFLG_OUTPUT 0x0002 #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT) static DEFINE_SPINLOCK(clients_lock); static DEFINE_MUTEX(register_mutex); /* * client table */ static char clienttablock[SNDRV_SEQ_MAX_CLIENTS]; static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS]; static struct snd_seq_usage client_usage; /* * prototypes */ static int bounce_error_event(struct snd_seq_client *client, struct snd_seq_event *event, int err, int atomic, int hop); static int snd_seq_deliver_single_event(struct snd_seq_client *client, struct snd_seq_event *event, int filter, int atomic, int hop); #if IS_ENABLED(CONFIG_SND_SEQ_UMP) static void free_ump_info(struct snd_seq_client *client); #endif /* */ static inline unsigned short snd_seq_file_flags(struct file *file) { switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) { case FMODE_WRITE: return SNDRV_SEQ_LFLG_OUTPUT; case FMODE_READ: return SNDRV_SEQ_LFLG_INPUT; default: return SNDRV_SEQ_LFLG_OPEN; } } static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client) { return snd_seq_total_cells(client->pool) > 0; } /* return pointer to client structure for specified id */ static struct snd_seq_client *clientptr(int clientid) { if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) { pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n", clientid); return NULL; } return clienttab[clientid]; } struct snd_seq_client *snd_seq_client_use_ptr(int clientid) { unsigned long flags; struct snd_seq_client *client; if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) { pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n", clientid); return NULL; } spin_lock_irqsave(&clients_lock, flags); client = clientptr(clientid); if (client) goto __lock; if (clienttablock[clientid]) { spin_unlock_irqrestore(&clients_lock, flags); return NULL; } spin_unlock_irqrestore(&clients_lock, flags); #ifdef CONFIG_MODULES if (!in_interrupt()) { static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); static DECLARE_BITMAP(card_requested, SNDRV_CARDS); if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) { int idx; if (!test_and_set_bit(clientid, client_requested)) { for (idx = 0; idx < 15; idx++) { if (seq_client_load[idx] < 0) break; if (seq_client_load[idx] == clientid) { request_module("snd-seq-client-%i", clientid); break; } } } } else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) { int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) / SNDRV_SEQ_CLIENTS_PER_CARD; if (card < snd_ecards_limit) { if (!test_and_set_bit(card, card_requested)) snd_request_card(card); snd_seq_device_load_drivers(); } } spin_lock_irqsave(&clients_lock, flags); client = clientptr(clientid); if (client) goto __lock; spin_unlock_irqrestore(&clients_lock, flags); } #endif return NULL; __lock: snd_use_lock_use(&client->use_lock); spin_unlock_irqrestore(&clients_lock, flags); return client; } /* Take refcount and perform ioctl_mutex lock on the given client; * used only for OSS sequencer * Unlock via snd_seq_client_ioctl_unlock() below */ bool snd_seq_client_ioctl_lock(int clientid) { struct snd_seq_client *client; client = snd_seq_client_use_ptr(clientid); if (!client) return false; mutex_lock(&client->ioctl_mutex); /* The client isn't unrefed here; see snd_seq_client_ioctl_unlock() */ return true; } EXPORT_SYMBOL_GPL(snd_seq_client_ioctl_lock); /* Unlock and unref the given client; for OSS sequencer use only */ void snd_seq_client_ioctl_unlock(int clientid) { struct snd_seq_client *client; client = snd_seq_client_use_ptr(clientid); if (WARN_ON(!client)) return; mutex_unlock(&client->ioctl_mutex); /* The doubly unrefs below are intentional; the first one releases the * leftover from snd_seq_client_ioctl_lock() above, and the second one * is for releasing snd_seq_client_use_ptr() in this function */ snd_seq_client_unlock(client); snd_seq_client_unlock(client); } EXPORT_SYMBOL_GPL(snd_seq_client_ioctl_unlock); static void usage_alloc(struct snd_seq_usage *res, int num) { res->cur += num; if (res->cur > res->peak) res->peak = res->cur; } static void usage_free(struct snd_seq_usage *res, int num) { res->cur -= num; } /* initialise data structures */ int __init client_init_data(void) { /* zap out the client table */ memset(&clienttablock, 0, sizeof(clienttablock)); memset(&clienttab, 0, sizeof(clienttab)); return 0; } static struct snd_seq_client *seq_create_client1(int client_index, int poolsize) { int c; struct snd_seq_client *client; /* init client data */ client = kzalloc(sizeof(*client), GFP_KERNEL); if (client == NULL) return NULL; client->pool = snd_seq_pool_new(poolsize); if (client->pool == NULL) { kfree(client); return NULL; } client->type = NO_CLIENT; snd_use_lock_init(&client->use_lock); rwlock_init(&client->ports_lock); mutex_init(&client->ports_mutex); INIT_LIST_HEAD(&client->ports_list_head); mutex_init(&client->ioctl_mutex); client->ump_endpoint_port = -1; /* find free slot in the client table */ spin_lock_irq(&clients_lock); if (client_index < 0) { for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN; c < SNDRV_SEQ_MAX_CLIENTS; c++) { if (clienttab[c] || clienttablock[c]) continue; clienttab[client->number = c] = client; spin_unlock_irq(&clients_lock); return client; } } else { if (clienttab[client_index] == NULL && !clienttablock[client_index]) { clienttab[client->number = client_index] = client; spin_unlock_irq(&clients_lock); return client; } } spin_unlock_irq(&clients_lock); snd_seq_pool_delete(&client->pool); kfree(client); return NULL; /* no free slot found or busy, return failure code */ } static int seq_free_client1(struct snd_seq_client *client) { if (!client) return 0; spin_lock_irq(&clients_lock); clienttablock[client->number] = 1; clienttab[client->number] = NULL; spin_unlock_irq(&clients_lock); snd_seq_delete_all_ports(client); snd_seq_queue_client_leave(client->number); snd_use_lock_sync(&client->use_lock); if (client->pool) snd_seq_pool_delete(&client->pool); spin_lock_irq(&clients_lock); clienttablock[client->number] = 0; spin_unlock_irq(&clients_lock); return 0; } static void seq_free_client(struct snd_seq_client * client) { mutex_lock(&register_mutex); switch (client->type) { case NO_CLIENT: pr_warn("ALSA: seq: Trying to free unused client %d\n", client->number); break; case USER_CLIENT: case KERNEL_CLIENT: seq_free_client1(client); usage_free(&client_usage, 1); break; default: pr_err("ALSA: seq: Trying to free client %d with undefined type = %d\n", client->number, client->type); } mutex_unlock(&register_mutex); snd_seq_system_client_ev_client_exit(client->number); } /* -------------------------------------------------------- */ /* create a user client */ static int snd_seq_open(struct inode *inode, struct file *file) { int c, mode; /* client id */ struct snd_seq_client *client; struct snd_seq_user_client *user; int err; err = stream_open(inode, file); if (err < 0) return err; mutex_lock(&register_mutex); client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS); if (!client) { mutex_unlock(&register_mutex); return -ENOMEM; /* failure code */ } mode = snd_seq_file_flags(file); if (mode & SNDRV_SEQ_LFLG_INPUT) client->accept_input = 1; if (mode & SNDRV_SEQ_LFLG_OUTPUT) client->accept_output = 1; user = &client->data.user; user->fifo = NULL; user->fifo_pool_size = 0; if (mode & SNDRV_SEQ_LFLG_INPUT) { user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS; user->fifo = snd_seq_fifo_new(user->fifo_pool_size); if (user->fifo == NULL) { seq_free_client1(client); kfree(client); mutex_unlock(&register_mutex); return -ENOMEM; } } usage_alloc(&client_usage, 1); client->type = USER_CLIENT; mutex_unlock(&register_mutex); c = client->number; file->private_data = client; /* fill client data */ user->file = file; sprintf(client->name, "Client-%d", c); client->data.user.owner = get_pid(task_pid(current)); /* make others aware this new client */ snd_seq_system_client_ev_client_start(c); return 0; } /* delete a user client */ static int snd_seq_release(struct inode *inode, struct file *file) { struct snd_seq_client *client = file->private_data; if (client) { seq_free_client(client); if (client->data.user.fifo) snd_seq_fifo_delete(&client->data.user.fifo); #if IS_ENABLED(CONFIG_SND_SEQ_UMP) free_ump_info(client); #endif put_pid(client->data.user.owner); kfree(client); } return 0; } static bool event_is_compatible(const struct snd_seq_client *client, const struct snd_seq_event *ev) { if (snd_seq_ev_is_ump(ev) && !client->midi_version) return false; if (snd_seq_ev_is_ump(ev) && snd_seq_ev_is_variable(ev)) return false; return true; } /* handle client read() */ /* possible error values: * -ENXIO invalid client or file open mode * -ENOSPC FIFO overflow (the flag is cleared after this error report) * -EINVAL no enough user-space buffer to write the whole event * -EFAULT seg. fault during copy to user space */ static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count, loff_t *offset) { struct snd_seq_client *client = file->private_data; struct snd_seq_fifo *fifo; size_t aligned_size; int err; long result = 0; struct snd_seq_event_cell *cell; if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT)) return -ENXIO; if (!access_ok(buf, count)) return -EFAULT; /* check client structures are in place */ if (snd_BUG_ON(!client)) return -ENXIO; if (!client->accept_input) return -ENXIO; fifo = client->data.user.fifo; if (!fifo) return -ENXIO; if (atomic_read(&fifo->overflow) > 0) { /* buffer overflow is detected */ snd_seq_fifo_clear(fifo); /* return error code */ return -ENOSPC; } cell = NULL; err = 0; snd_seq_fifo_lock(fifo); if (IS_ENABLED(CONFIG_SND_SEQ_UMP) && client->midi_version > 0) aligned_size = sizeof(struct snd_seq_ump_event); else aligned_size = sizeof(struct snd_seq_event); /* while data available in queue */ while (count >= aligned_size) { int nonblock; nonblock = (file->f_flags & O_NONBLOCK) || result > 0; err = snd_seq_fifo_cell_out(fifo, &cell, nonblock); if (err < 0) break; if (!event_is_compatible(client, &cell->event)) { snd_seq_cell_free(cell); cell = NULL; continue; } if (snd_seq_ev_is_variable(&cell->event)) { struct snd_seq_ump_event tmpev; memcpy(&tmpev, &cell->event, aligned_size); tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK; if (copy_to_user(buf, &tmpev, aligned_size)) { err = -EFAULT; break; } count -= aligned_size; buf += aligned_size; err = snd_seq_expand_var_event(&cell->event, count, (char __force *)buf, 0, aligned_size); if (err < 0) break; result += err; count -= err; buf += err; } else { if (copy_to_user(buf, &cell->event, aligned_size)) { err = -EFAULT; break; } count -= aligned_size; buf += aligned_size; } snd_seq_cell_free(cell); cell = NULL; /* to be sure */ result += aligned_size; } if (err < 0) { if (cell) snd_seq_fifo_cell_putback(fifo, cell); if (err == -EAGAIN && result > 0) err = 0; } snd_seq_fifo_unlock(fifo); return (err < 0) ? err : result; } /* * check access permission to the port */ static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags) { if ((port->capability & flags) != flags) return 0; return flags; } /* * check if the destination client is available, and return the pointer * if filter is non-zero, client filter bitmap is tested. */ static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event, int filter) { struct snd_seq_client *dest; dest = snd_seq_client_use_ptr(event->dest.client); if (dest == NULL) return NULL; if (! dest->accept_input) goto __not_avail; if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) && ! test_bit(event->type, dest->event_filter)) goto __not_avail; if (filter && !(dest->filter & filter)) goto __not_avail; return dest; /* ok - accessible */ __not_avail: snd_seq_client_unlock(dest); return NULL; } /* * Return the error event. * * If the receiver client is a user client, the original event is * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If * the original event is also variable length, the external data is * copied after the event record. * If the receiver client is a kernel client, the original event is * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra * kmalloc. */ static int bounce_error_event(struct snd_seq_client *client, struct snd_seq_event *event, int err, int atomic, int hop) { struct snd_seq_event bounce_ev; int result; if (client == NULL || ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) || ! client->accept_input) return 0; /* ignored */ /* set up quoted error */ memset(&bounce_ev, 0, sizeof(bounce_ev)); bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR; bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT; bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM; bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE; bounce_ev.dest.client = client->number; bounce_ev.dest.port = event->source.port; bounce_ev.data.quote.origin = event->dest; bounce_ev.data.quote.event = event; bounce_ev.data.quote.value = -err; /* use positive value */ result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1); if (result < 0) { client->event_lost++; return result; } return result; } /* * rewrite the time-stamp of the event record with the curren time * of the given queue. * return non-zero if updated. */ static int update_timestamp_of_queue(struct snd_seq_event *event, int queue, int real_time) { struct snd_seq_queue *q; q = queueptr(queue); if (! q) return 0; event->queue = queue; event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK; if (real_time) { event->time.time = snd_seq_timer_get_cur_time(q->timer, true); event->flags |= SNDRV_SEQ_TIME_STAMP_REAL; } else { event->time.tick = snd_seq_timer_get_cur_tick(q->timer); event->flags |= SNDRV_SEQ_TIME_STAMP_TICK; } queuefree(q); return 1; } /* deliver a single event; called from below and UMP converter */ int __snd_seq_deliver_single_event(struct snd_seq_client *dest, struct snd_seq_client_port *dest_port, struct snd_seq_event *event, int atomic, int hop) { switch (dest->type) { case USER_CLIENT: if (!dest->data.user.fifo) return 0; return snd_seq_fifo_event_in(dest->data.user.fifo, event); case KERNEL_CLIENT: if (!dest_port->event_input) return 0; return dest_port->event_input(event, snd_seq_ev_is_direct(event), dest_port->private_data, atomic, hop); } return 0; } /* * deliver an event to the specified destination. * if filter is non-zero, client filter bitmap is tested. * * RETURN VALUE: 0 : if succeeded * <0 : error */ static int snd_seq_deliver_single_event(struct snd_seq_client *client, struct snd_seq_event *event, int filter, int atomic, int hop) { struct snd_seq_client *dest = NULL; struct snd_seq_client_port *dest_port = NULL; int result = -ENOENT; int direct; direct = snd_seq_ev_is_direct(event); dest = get_event_dest_client(event, filter); if (dest == NULL) goto __skip; dest_port = snd_seq_port_use_ptr(dest, event->dest.port); if (dest_port == NULL) goto __skip; /* check permission */ if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) { result = -EPERM; goto __skip; } if (dest_port->timestamping) update_timestamp_of_queue(event, dest_port->time_queue, dest_port->time_real); #if IS_ENABLED(CONFIG_SND_SEQ_UMP) if (!(dest->filter & SNDRV_SEQ_FILTER_NO_CONVERT)) { if (snd_seq_ev_is_ump(event)) { result = snd_seq_deliver_from_ump(client, dest, dest_port, event, atomic, hop); goto __skip; } else if (snd_seq_client_is_ump(dest)) { result = snd_seq_deliver_to_ump(client, dest, dest_port, event, atomic, hop); goto __skip; } } #endif /* CONFIG_SND_SEQ_UMP */ result = __snd_seq_deliver_single_event(dest, dest_port, event, atomic, hop); __skip: if (dest_port) snd_seq_port_unlock(dest_port); if (dest) snd_seq_client_unlock(dest); if (result < 0 && !direct) { result = bounce_error_event(client, event, result, atomic, hop); } return result; } /* * send the event to all subscribers: */ static int __deliver_to_subscribers(struct snd_seq_client *client, struct snd_seq_event *event, struct snd_seq_client_port *src_port, int atomic, int hop) { struct snd_seq_subscribers *subs; int err, result = 0, num_ev = 0; union __snd_seq_event event_saved; size_t saved_size; struct snd_seq_port_subs_info *grp; /* save original event record */ saved_size = snd_seq_event_packet_size(event); memcpy(&event_saved, event, saved_size); grp = &src_port->c_src; /* lock list */ if (atomic) read_lock(&grp->list_lock); else down_read_nested(&grp->list_mutex, hop); list_for_each_entry(subs, &grp->list_head, src_list) { /* both ports ready? */ if (atomic_read(&subs->ref_count) != 2) continue; event->dest = subs->info.dest; if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) /* convert time according to flag with subscription */ update_timestamp_of_queue(event, subs->info.queue, subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL); err = snd_seq_deliver_single_event(client, event, 0, atomic, hop); if (err < 0) { /* save first error that occurs and continue */ if (!result) result = err; continue; } num_ev++; /* restore original event record */ memcpy(event, &event_saved, saved_size); } if (atomic) read_unlock(&grp->list_lock); else up_read(&grp->list_mutex); memcpy(event, &event_saved, saved_size); return (result < 0) ? result : num_ev; } static int deliver_to_subscribers(struct snd_seq_client *client, struct snd_seq_event *event, int atomic, int hop) { struct snd_seq_client_port *src_port; int ret = 0, ret2; src_port = snd_seq_port_use_ptr(client, event->source.port); if (src_port) { ret = __deliver_to_subscribers(client, event, src_port, atomic, hop); snd_seq_port_unlock(src_port); } if (client->ump_endpoint_port < 0 || event->source.port == client->ump_endpoint_port) return ret; src_port = snd_seq_port_use_ptr(client, client->ump_endpoint_port); if (!src_port) return ret; ret2 = __deliver_to_subscribers(client, event, src_port, atomic, hop); snd_seq_port_unlock(src_port); return ret2 < 0 ? ret2 : ret; } /* deliver an event to the destination port(s). * if the event is to subscribers or broadcast, the event is dispatched * to multiple targets. * * RETURN VALUE: n > 0 : the number of delivered events. * n == 0 : the event was not passed to any client. * n < 0 : error - event was not processed. */ static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event, int atomic, int hop) { int result; hop++; if (hop >= SNDRV_SEQ_MAX_HOPS) { pr_debug("ALSA: seq: too long delivery path (%d:%d->%d:%d)\n", event->source.client, event->source.port, event->dest.client, event->dest.port); return -EMLINK; } if (snd_seq_ev_is_variable(event) && snd_BUG_ON(atomic && (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR))) return -EINVAL; if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS || event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) result = deliver_to_subscribers(client, event, atomic, hop); else result = snd_seq_deliver_single_event(client, event, 0, atomic, hop); return result; } /* * dispatch an event cell: * This function is called only from queue check routines in timer * interrupts or after enqueued. * The event cell shall be released or re-queued in this function. * * RETURN VALUE: n > 0 : the number of delivered events. * n == 0 : the event was not passed to any client. * n < 0 : error - event was not processed. */ int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop) { struct snd_seq_client *client; int result; if (snd_BUG_ON(!cell)) return -EINVAL; client = snd_seq_client_use_ptr(cell->event.source.client); if (client == NULL) { snd_seq_cell_free(cell); /* release this cell */ return -EINVAL; } if (!snd_seq_ev_is_ump(&cell->event) && cell->event.type == SNDRV_SEQ_EVENT_NOTE) { /* NOTE event: * the event cell is re-used as a NOTE-OFF event and * enqueued again. */ struct snd_seq_event tmpev, *ev; /* reserve this event to enqueue note-off later */ tmpev = cell->event; tmpev.type = SNDRV_SEQ_EVENT_NOTEON; result = snd_seq_deliver_event(client, &tmpev, atomic, hop); /* * This was originally a note event. We now re-use the * cell for the note-off event. */ ev = &cell->event; ev->type = SNDRV_SEQ_EVENT_NOTEOFF; ev->flags |= SNDRV_SEQ_PRIORITY_HIGH; /* add the duration time */ switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) { case SNDRV_SEQ_TIME_STAMP_TICK: cell->event.time.tick += ev->data.note.duration; break; case SNDRV_SEQ_TIME_STAMP_REAL: /* unit for duration is ms */ ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000); ev->time.time.tv_sec += ev->data.note.duration / 1000 + ev->time.time.tv_nsec / 1000000000; ev->time.time.tv_nsec %= 1000000000; break; } ev->data.note.velocity = ev->data.note.off_velocity; /* Now queue this cell as the note off event */ if (snd_seq_enqueue_event(cell, atomic, hop) < 0) snd_seq_cell_free(cell); /* release this cell */ } else { /* Normal events: * event cell is freed after processing the event */ result = snd_seq_deliver_event(client, &cell->event, atomic, hop); snd_seq_cell_free(cell); } snd_seq_client_unlock(client); return result; } /* Allocate a cell from client pool and enqueue it to queue: * if pool is empty and blocking is TRUE, sleep until a new cell is * available. */ static int snd_seq_client_enqueue_event(struct snd_seq_client *client, struct snd_seq_event *event, struct file *file, int blocking, int atomic, int hop, struct mutex *mutexp) { struct snd_seq_event_cell *cell; int err; /* special queue values - force direct passing */ if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) { event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; event->queue = SNDRV_SEQ_QUEUE_DIRECT; } else if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) { /* check presence of source port */ struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port); if (src_port == NULL) return -EINVAL; snd_seq_port_unlock(src_port); } /* direct event processing without enqueued */ if (snd_seq_ev_is_direct(event)) { if (!snd_seq_ev_is_ump(event) && event->type == SNDRV_SEQ_EVENT_NOTE) return -EINVAL; /* this event must be enqueued! */ return snd_seq_deliver_event(client, event, atomic, hop); } /* Not direct, normal queuing */ if (snd_seq_queue_is_used(event->queue, client->number) <= 0) return -EINVAL; /* invalid queue */ if (! snd_seq_write_pool_allocated(client)) return -ENXIO; /* queue is not allocated */ /* allocate an event cell */ err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file, mutexp); if (err < 0) return err; /* we got a cell. enqueue it. */ err = snd_seq_enqueue_event(cell, atomic, hop); if (err < 0) { snd_seq_cell_free(cell); return err; } return 0; } /* * check validity of event type and data length. * return non-zero if invalid. */ static int check_event_type_and_length(struct snd_seq_event *ev) { switch (snd_seq_ev_length_type(ev)) { case SNDRV_SEQ_EVENT_LENGTH_FIXED: if (snd_seq_ev_is_variable_type(ev)) return -EINVAL; break; case SNDRV_SEQ_EVENT_LENGTH_VARIABLE: if (! snd_seq_ev_is_variable_type(ev) || (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN) return -EINVAL; break; case SNDRV_SEQ_EVENT_LENGTH_VARUSR: if (! snd_seq_ev_is_direct(ev)) return -EINVAL; break; } return 0; } /* handle write() */ /* possible error values: * -ENXIO invalid client or file open mode * -ENOMEM malloc failed * -EFAULT seg. fault during copy from user space * -EINVAL invalid event * -EAGAIN no space in output pool * -EINTR interrupts while sleep * -EMLINK too many hops * others depends on return value from driver callback */ static ssize_t snd_seq_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { struct snd_seq_client *client = file->private_data; int written = 0, len; int err, handled; union __snd_seq_event __event; struct snd_seq_event *ev = &__event.legacy; if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT)) return -ENXIO; /* check client structures are in place */ if (snd_BUG_ON(!client)) return -ENXIO; if (!client->accept_output || client->pool == NULL) return -ENXIO; repeat: handled = 0; /* allocate the pool now if the pool is not allocated yet */ mutex_lock(&client->ioctl_mutex); if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) { err = snd_seq_pool_init(client->pool); if (err < 0) goto out; } /* only process whole events */ err = -EINVAL; while (count >= sizeof(struct snd_seq_event)) { /* Read in the event header from the user */ len = sizeof(struct snd_seq_event); if (copy_from_user(ev, buf, len)) { err = -EFAULT; break; } /* read in the rest bytes for UMP events */ if (snd_seq_ev_is_ump(ev)) { if (count < sizeof(struct snd_seq_ump_event)) break; if (copy_from_user((char *)ev + len, buf + len, sizeof(struct snd_seq_ump_event) - len)) { err = -EFAULT; break; } len = sizeof(struct snd_seq_ump_event); } ev->source.client = client->number; /* fill in client number */ /* Check for extension data length */ if (check_event_type_and_length(ev)) { err = -EINVAL; break; } if (!event_is_compatible(client, ev)) { err = -EINVAL; break; } /* check for special events */ if (!snd_seq_ev_is_ump(ev)) { if (ev->type == SNDRV_SEQ_EVENT_NONE) goto __skip_event; else if (snd_seq_ev_is_reserved(ev)) { err = -EINVAL; break; } } if (snd_seq_ev_is_variable(ev)) { int extlen = ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK; if ((size_t)(extlen + len) > count) { /* back out, will get an error this time or next */ err = -EINVAL; break; } /* set user space pointer */ ev->data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR; ev->data.ext.ptr = (char __force *)buf + len; len += extlen; /* increment data length */ } else { #ifdef CONFIG_COMPAT if (client->convert32 && snd_seq_ev_is_varusr(ev)) ev->data.ext.ptr = (void __force *)compat_ptr(ev->data.raw32.d[1]); #endif } /* ok, enqueue it */ err = snd_seq_client_enqueue_event(client, ev, file, !(file->f_flags & O_NONBLOCK), 0, 0, &client->ioctl_mutex); if (err < 0) break; handled++; __skip_event: /* Update pointers and counts */ count -= len; buf += len; written += len; /* let's have a coffee break if too many events are queued */ if (++handled >= 200) { mutex_unlock(&client->ioctl_mutex); goto repeat; } } out: mutex_unlock(&client->ioctl_mutex); return written ? written : err; } /* * handle polling */ static __poll_t snd_seq_poll(struct file *file, poll_table * wait) { struct snd_seq_client *client = file->private_data; __poll_t mask = 0; /* check client structures are in place */ if (snd_BUG_ON(!client)) return EPOLLERR; if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) && client->data.user.fifo) { /* check if data is available in the outqueue */ if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait)) mask |= EPOLLIN | EPOLLRDNORM; } if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) { /* check if data is available in the pool */ if (!snd_seq_write_pool_allocated(client) || snd_seq_pool_poll_wait(client->pool, file, wait)) mask |= EPOLLOUT | EPOLLWRNORM; } return mask; } /*-----------------------------------------------------*/ static int snd_seq_ioctl_pversion(struct snd_seq_client *client, void *arg) { int *pversion = arg; *pversion = SNDRV_SEQ_VERSION; return 0; } static int snd_seq_ioctl_user_pversion(struct snd_seq_client *client, void *arg) { client->user_pversion = *(unsigned int *)arg; return 0; } static int snd_seq_ioctl_client_id(struct snd_seq_client *client, void *arg) { int *client_id = arg; *client_id = client->number; return 0; } /* SYSTEM_INFO ioctl() */ static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void *arg) { struct snd_seq_system_info *info = arg; memset(info, 0, sizeof(*info)); /* fill the info fields */ info->queues = SNDRV_SEQ_MAX_QUEUES; info->clients = SNDRV_SEQ_MAX_CLIENTS; info->ports = SNDRV_SEQ_MAX_PORTS; info->channels = 256; /* fixed limit */ info->cur_clients = client_usage.cur; info->cur_queues = snd_seq_queue_get_cur_queues(); return 0; } /* RUNNING_MODE ioctl() */ static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void *arg) { struct snd_seq_running_info *info = arg; struct snd_seq_client *cptr; int err = 0; /* requested client number */ cptr = snd_seq_client_use_ptr(info->client); if (cptr == NULL) return -ENOENT; /* don't change !!! */ #ifdef SNDRV_BIG_ENDIAN if (!info->big_endian) { err = -EINVAL; goto __err; } #else if (info->big_endian) { err = -EINVAL; goto __err; } #endif if (info->cpu_mode > sizeof(long)) { err = -EINVAL; goto __err; } cptr->convert32 = (info->cpu_mode < sizeof(long)); __err: snd_seq_client_unlock(cptr); return err; } /* CLIENT_INFO ioctl() */ static void get_client_info(struct snd_seq_client *cptr, struct snd_seq_client_info *info) { info->client = cptr->number; /* fill the info fields */ info->type = cptr->type; strcpy(info->name, cptr->name); info->filter = cptr->filter; info->event_lost = cptr->event_lost; memcpy(info->event_filter, cptr->event_filter, 32); info->group_filter = cptr->group_filter; info->num_ports = cptr->num_ports; if (cptr->type == USER_CLIENT) info->pid = pid_vnr(cptr->data.user.owner); else info->pid = -1; if (cptr->type == KERNEL_CLIENT) info->card = cptr->data.kernel.card ? cptr->data.kernel.card->number : -1; else info->card = -1; info->midi_version = cptr->midi_version; memset(info->reserved, 0, sizeof(info->reserved)); } static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client, void *arg) { struct snd_seq_client_info *client_info = arg; struct snd_seq_client *cptr; /* requested client number */ cptr = snd_seq_client_use_ptr(client_info->client); if (cptr == NULL) return -ENOENT; /* don't change !!! */ get_client_info(cptr, client_info); snd_seq_client_unlock(cptr); return 0; } /* CLIENT_INFO ioctl() */ static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client, void *arg) { struct snd_seq_client_info *client_info = arg; /* it is not allowed to set the info fields for an another client */ if (client->number != client_info->client) return -EPERM; /* also client type must be set now */ if (client->type != client_info->type) return -EINVAL; /* check validity of midi_version field */ if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3) && client_info->midi_version > SNDRV_SEQ_CLIENT_UMP_MIDI_2_0) return -EINVAL; /* fill the info fields */ if (client_info->name[0]) strscpy(client->name, client_info->name, sizeof(client->name)); client->filter = client_info->filter; client->event_lost = client_info->event_lost; if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3)) client->midi_version = client_info->midi_version; memcpy(client->event_filter, client_info->event_filter, 32); client->group_filter = client_info->group_filter; return 0; } /* * CREATE PORT ioctl() */ static int snd_seq_ioctl_create_port(struct snd_seq_client *client, void *arg) { struct snd_seq_port_info *info = arg; struct snd_seq_client_port *port; struct snd_seq_port_callback *callback; int port_idx, err; /* it is not allowed to create the port for an another client */ if (info->addr.client != client->number) return -EPERM; if (client->type == USER_CLIENT && info->kernel) return -EINVAL; if ((info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT) && client->ump_endpoint_port >= 0) return -EBUSY; if (info->flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) port_idx = info->addr.port; else port_idx = -1; if (port_idx >= SNDRV_SEQ_ADDRESS_UNKNOWN) return -EINVAL; err = snd_seq_create_port(client, port_idx, &port); if (err < 0) return err; if (client->type == KERNEL_CLIENT) { callback = info->kernel; if (callback) { if (callback->owner) port->owner = callback->owner; port->private_data = callback->private_data; port->private_free = callback->private_free; port->event_input = callback->event_input; port->c_src.open = callback->subscribe; port->c_src.close = callback->unsubscribe; port->c_dest.open = callback->use; port->c_dest.close = callback->unuse; } } info->addr = port->addr; snd_seq_set_port_info(port, info); if (info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT) client->ump_endpoint_port = port->addr.port; snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port); snd_seq_port_unlock(port); return 0; } /* * DELETE PORT ioctl() */ static int snd_seq_ioctl_delete_port(struct snd_seq_client *client, void *arg) { struct snd_seq_port_info *info = arg; int err; /* it is not allowed to remove the port for an another client */ if (info->addr.client != client->number) return -EPERM; err = snd_seq_delete_port(client, info->addr.port); if (err >= 0) { if (client->ump_endpoint_port == info->addr.port) client->ump_endpoint_port = -1; snd_seq_system_client_ev_port_exit(client->number, info->addr.port); } return err; } /* * GET_PORT_INFO ioctl() (on any client) */ static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client, void *arg) { struct snd_seq_port_info *info = arg; struct snd_seq_client *cptr; struct snd_seq_client_port *port; cptr = snd_seq_client_use_ptr(info->addr.client); if (cptr == NULL) return -ENXIO; port = snd_seq_port_use_ptr(cptr, info->addr.port); if (port == NULL) { snd_seq_client_unlock(cptr); return -ENOENT; /* don't change */ } /* get port info */ snd_seq_get_port_info(port, info); snd_seq_port_unlock(port); snd_seq_client_unlock(cptr); return 0; } /* * SET_PORT_INFO ioctl() (only ports on this/own client) */ static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client, void *arg) { struct snd_seq_port_info *info = arg; struct snd_seq_client_port *port; if (info->addr.client != client->number) /* only set our own ports ! */ return -EPERM; port = snd_seq_port_use_ptr(client, info->addr.port); if (port) { snd_seq_set_port_info(port, info); snd_seq_port_unlock(port); } return 0; } /* * port subscription (connection) */ #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ) #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE) static int check_subscription_permission(struct snd_seq_client *client, struct snd_seq_client_port *sport, struct snd_seq_client_port *dport, struct snd_seq_port_subscribe *subs) { if (client->number != subs->sender.client && client->number != subs->dest.client) { /* connection by third client - check export permission */ if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT)) return -EPERM; if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT)) return -EPERM; } /* check read permission */ /* if sender or receiver is the subscribing client itself, * no permission check is necessary */ if (client->number != subs->sender.client) { if (! check_port_perm(sport, PERM_RD)) return -EPERM; } /* check write permission */ if (client->number != subs->dest.client) { if (! check_port_perm(dport, PERM_WR)) return -EPERM; } return 0; } /* * send an subscription notify event to user client: * client must be user client. */ int snd_seq_client_notify_subscription(int client, int port, struct snd_seq_port_subscribe *info, int evtype) { struct snd_seq_event event; memset(&event, 0, sizeof(event)); event.type = evtype; event.data.connect.dest = info->dest; event.data.connect.sender = info->sender; return snd_seq_system_notify(client, port, &event); /* non-atomic */ } /* * add to port's subscription list IOCTL interface */ static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client, void *arg) { struct snd_seq_port_subscribe *subs = arg; int result = -EINVAL; struct snd_seq_client *receiver = NULL, *sender = NULL; struct snd_seq_client_port *sport = NULL, *dport = NULL; receiver = snd_seq_client_use_ptr(subs->dest.client); if (!receiver) goto __end; sender = snd_seq_client_use_ptr(subs->sender.client); if (!sender) goto __end; sport = snd_seq_port_use_ptr(sender, subs->sender.port); if (!sport) goto __end; dport = snd_seq_port_use_ptr(receiver, subs->dest.port); if (!dport) goto __end; result = check_subscription_permission(client, sport, dport, subs); if (result < 0) goto __end; /* connect them */ result = snd_seq_port_connect(client, sender, sport, receiver, dport, subs); if (! result) /* broadcast announce */ snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0, subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); __end: if (sport) snd_seq_port_unlock(sport); if (dport) snd_seq_port_unlock(dport); if (sender) snd_seq_client_unlock(sender); if (receiver) snd_seq_client_unlock(receiver); return result; } /* * remove from port's subscription list */ static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client, void *arg) { struct snd_seq_port_subscribe *subs = arg; int result = -ENXIO; struct snd_seq_client *receiver = NULL, *sender = NULL; struct snd_seq_client_port *sport = NULL, *dport = NULL; receiver = snd_seq_client_use_ptr(subs->dest.client); if (!receiver) goto __end; sender = snd_seq_client_use_ptr(subs->sender.client); if (!sender) goto __end; sport = snd_seq_port_use_ptr(sender, subs->sender.port); if (!sport) goto __end; dport = snd_seq_port_use_ptr(receiver, subs->dest.port); if (!dport) goto __end; result = check_subscription_permission(client, sport, dport, subs); if (result < 0) goto __end; result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, subs); if (! result) /* broadcast announce */ snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0, subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); __end: if (sport) snd_seq_port_unlock(sport); if (dport) snd_seq_port_unlock(dport); if (sender) snd_seq_client_unlock(sender); if (receiver) snd_seq_client_unlock(receiver); return result; } /* CREATE_QUEUE ioctl() */ static int snd_seq_ioctl_create_queue(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_info *info = arg; struct snd_seq_queue *q; q = snd_seq_queue_alloc(client->number, info->locked, info->flags); if (IS_ERR(q)) return PTR_ERR(q); info->queue = q->queue; info->locked = q->locked; info->owner = q->owner; /* set queue name */ if (!info->name[0]) snprintf(info->name, sizeof(info->name), "Queue-%d", q->queue); strscpy(q->name, info->name, sizeof(q->name)); snd_use_lock_free(&q->use_lock); return 0; } /* DELETE_QUEUE ioctl() */ static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_info *info = arg; return snd_seq_queue_delete(client->number, info->queue); } /* GET_QUEUE_INFO ioctl() */ static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_info *info = arg; struct snd_seq_queue *q; q = queueptr(info->queue); if (q == NULL) return -EINVAL; memset(info, 0, sizeof(*info)); info->queue = q->queue; info->owner = q->owner; info->locked = q->locked; strscpy(info->name, q->name, sizeof(info->name)); queuefree(q); return 0; } /* SET_QUEUE_INFO ioctl() */ static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_info *info = arg; struct snd_seq_queue *q; if (info->owner != client->number) return -EINVAL; /* change owner/locked permission */ if (snd_seq_queue_check_access(info->queue, client->number)) { if (snd_seq_queue_set_owner(info->queue, client->number, info->locked) < 0) return -EPERM; if (info->locked) snd_seq_queue_use(info->queue, client->number, 1); } else { return -EPERM; } q = queueptr(info->queue); if (! q) return -EINVAL; if (q->owner != client->number) { queuefree(q); return -EPERM; } strscpy(q->name, info->name, sizeof(q->name)); queuefree(q); return 0; } /* GET_NAMED_QUEUE ioctl() */ static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_info *info = arg; struct snd_seq_queue *q; q = snd_seq_queue_find_name(info->name); if (q == NULL) return -EINVAL; info->queue = q->queue; info->owner = q->owner; info->locked = q->locked; queuefree(q); return 0; } /* GET_QUEUE_STATUS ioctl() */ static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_status *status = arg; struct snd_seq_queue *queue; struct snd_seq_timer *tmr; queue = queueptr(status->queue); if (queue == NULL) return -EINVAL; memset(status, 0, sizeof(*status)); status->queue = queue->queue; tmr = queue->timer; status->events = queue->tickq->cells + queue->timeq->cells; status->time = snd_seq_timer_get_cur_time(tmr, true); status->tick = snd_seq_timer_get_cur_tick(tmr); status->running = tmr->running; status->flags = queue->flags; queuefree(queue); return 0; } /* GET_QUEUE_TEMPO ioctl() */ static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_tempo *tempo = arg; struct snd_seq_queue *queue; struct snd_seq_timer *tmr; queue = queueptr(tempo->queue); if (queue == NULL) return -EINVAL; memset(tempo, 0, sizeof(*tempo)); tempo->queue = queue->queue; tmr = queue->timer; tempo->tempo = tmr->tempo; tempo->ppq = tmr->ppq; tempo->skew_value = tmr->skew; tempo->skew_base = tmr->skew_base; queuefree(queue); return 0; } /* SET_QUEUE_TEMPO ioctl() */ int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo) { if (!snd_seq_queue_check_access(tempo->queue, client)) return -EPERM; return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo); } EXPORT_SYMBOL(snd_seq_set_queue_tempo); static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_tempo *tempo = arg; int result; result = snd_seq_set_queue_tempo(client->number, tempo); return result < 0 ? result : 0; } /* GET_QUEUE_TIMER ioctl() */ static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_timer *timer = arg; struct snd_seq_queue *queue; struct snd_seq_timer *tmr; queue = queueptr(timer->queue); if (queue == NULL) return -EINVAL; mutex_lock(&queue->timer_mutex); tmr = queue->timer; memset(timer, 0, sizeof(*timer)); timer->queue = queue->queue; timer->type = tmr->type; if (tmr->type == SNDRV_SEQ_TIMER_ALSA) { timer->u.alsa.id = tmr->alsa_id; timer->u.alsa.resolution = tmr->preferred_resolution; } mutex_unlock(&queue->timer_mutex); queuefree(queue); return 0; } /* SET_QUEUE_TIMER ioctl() */ static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_timer *timer = arg; int result = 0; if (timer->type != SNDRV_SEQ_TIMER_ALSA) return -EINVAL; if (snd_seq_queue_check_access(timer->queue, client->number)) { struct snd_seq_queue *q; struct snd_seq_timer *tmr; q = queueptr(timer->queue); if (q == NULL) return -ENXIO; mutex_lock(&q->timer_mutex); tmr = q->timer; snd_seq_queue_timer_close(timer->queue); tmr->type = timer->type; if (tmr->type == SNDRV_SEQ_TIMER_ALSA) { tmr->alsa_id = timer->u.alsa.id; tmr->preferred_resolution = timer->u.alsa.resolution; } result = snd_seq_queue_timer_open(timer->queue); mutex_unlock(&q->timer_mutex); queuefree(q); } else { return -EPERM; } return result; } /* GET_QUEUE_CLIENT ioctl() */ static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_client *info = arg; int used; used = snd_seq_queue_is_used(info->queue, client->number); if (used < 0) return -EINVAL; info->used = used; info->client = client->number; return 0; } /* SET_QUEUE_CLIENT ioctl() */ static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client, void *arg) { struct snd_seq_queue_client *info = arg; int err; if (info->used >= 0) { err = snd_seq_queue_use(info->queue, client->number, info->used); if (err < 0) return err; } return snd_seq_ioctl_get_queue_client(client, arg); } /* GET_CLIENT_POOL ioctl() */ static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client, void *arg) { struct snd_seq_client_pool *info = arg; struct snd_seq_client *cptr; cptr = snd_seq_client_use_ptr(info->client); if (cptr == NULL) return -ENOENT; memset(info, 0, sizeof(*info)); info->client = cptr->number; info->output_pool = cptr->pool->size; info->output_room = cptr->pool->room; info->output_free = info->output_pool; info->output_free = snd_seq_unused_cells(cptr->pool); if (cptr->type == USER_CLIENT) { info->input_pool = cptr->data.user.fifo_pool_size; info->input_free = info->input_pool; info->input_free = snd_seq_fifo_unused_cells(cptr->data.user.fifo); } else { info->input_pool = 0; info->input_free = 0; } snd_seq_client_unlock(cptr); return 0; } /* SET_CLIENT_POOL ioctl() */ static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client, void *arg) { struct snd_seq_client_pool *info = arg; int rc; if (client->number != info->client) return -EINVAL; /* can't change other clients */ if (info->output_pool >= 1 && info->output_pool <= SNDRV_SEQ_MAX_EVENTS && (! snd_seq_write_pool_allocated(client) || info->output_pool != client->pool->size)) { if (snd_seq_write_pool_allocated(client)) { /* is the pool in use? */ if (atomic_read(&client->pool->counter)) return -EBUSY; /* remove all existing cells */ snd_seq_pool_mark_closing(client->pool); snd_seq_pool_done(client->pool); } client->pool->size = info->output_pool; rc = snd_seq_pool_init(client->pool); if (rc < 0) return rc; } if (client->type == USER_CLIENT && client->data.user.fifo != NULL && info->input_pool >= 1 && info->input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS && info->input_pool != client->data.user.fifo_pool_size) { /* change pool size */ rc = snd_seq_fifo_resize(client->data.user.fifo, info->input_pool); if (rc < 0) return rc; client->data.user.fifo_pool_size = info->input_pool; } if (info->output_room >= 1 && info->output_room <= client->pool->size) { client->pool->room = info->output_room; } return snd_seq_ioctl_get_client_pool(client, arg); } /* REMOVE_EVENTS ioctl() */ static int snd_seq_ioctl_remove_events(struct snd_seq_client *client, void *arg) { struct snd_seq_remove_events *info = arg; /* * Input mostly not implemented XXX. */ if (info->remove_mode & SNDRV_SEQ_REMOVE_INPUT) { /* * No restrictions so for a user client we can clear * the whole fifo */ if (client->type == USER_CLIENT && client->data.user.fifo) snd_seq_fifo_clear(client->data.user.fifo); } if (info->remove_mode & SNDRV_SEQ_REMOVE_OUTPUT) snd_seq_queue_remove_cells(client->number, info); return 0; } /* * get subscription info */ static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client, void *arg) { struct snd_seq_port_subscribe *subs = arg; int result; struct snd_seq_client *sender = NULL; struct snd_seq_client_port *sport = NULL; result = -EINVAL; sender = snd_seq_client_use_ptr(subs->sender.client); if (!sender) goto __end; sport = snd_seq_port_use_ptr(sender, subs->sender.port); if (!sport) goto __end; result = snd_seq_port_get_subscription(&sport->c_src, &subs->dest, subs); __end: if (sport) snd_seq_port_unlock(sport); if (sender) snd_seq_client_unlock(sender); return result; } /* * get subscription info - check only its presence */ static int snd_seq_ioctl_query_subs(struct snd_seq_client *client, void *arg) { struct snd_seq_query_subs *subs = arg; int result = -ENXIO; struct snd_seq_client *cptr = NULL; struct snd_seq_client_port *port = NULL; struct snd_seq_port_subs_info *group; struct list_head *p; int i; cptr = snd_seq_client_use_ptr(subs->root.client); if (!cptr) goto __end; port = snd_seq_port_use_ptr(cptr, subs->root.port); if (!port) goto __end; switch (subs->type) { case SNDRV_SEQ_QUERY_SUBS_READ: group = &port->c_src; break; case SNDRV_SEQ_QUERY_SUBS_WRITE: group = &port->c_dest; break; default: goto __end; } down_read(&group->list_mutex); /* search for the subscriber */ subs->num_subs = group->count; i = 0; result = -ENOENT; list_for_each(p, &group->list_head) { if (i++ == subs->index) { /* found! */ struct snd_seq_subscribers *s; if (subs->type == SNDRV_SEQ_QUERY_SUBS_READ) { s = list_entry(p, struct snd_seq_subscribers, src_list); subs->addr = s->info.dest; } else { s = list_entry(p, struct snd_seq_subscribers, dest_list); subs->addr = s->info.sender; } subs->flags = s->info.flags; subs->queue = s->info.queue; result = 0; break; } } up_read(&group->list_mutex); __end: if (port) snd_seq_port_unlock(port); if (cptr) snd_seq_client_unlock(cptr); return result; } /* * query next client */ static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client, void *arg) { struct snd_seq_client_info *info = arg; struct snd_seq_client *cptr = NULL; /* search for next client */ if (info->client < INT_MAX) info->client++; if (info->client < 0) info->client = 0; for (; info->client < SNDRV_SEQ_MAX_CLIENTS; info->client++) { cptr = snd_seq_client_use_ptr(info->client); if (cptr) break; /* found */ } if (cptr == NULL) return -ENOENT; get_client_info(cptr, info); snd_seq_client_unlock(cptr); return 0; } /* * query next port */ static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client, void *arg) { struct snd_seq_port_info *info = arg; struct snd_seq_client *cptr; struct snd_seq_client_port *port = NULL; cptr = snd_seq_client_use_ptr(info->addr.client); if (cptr == NULL) return -ENXIO; /* search for next port */ info->addr.port++; port = snd_seq_port_query_nearest(cptr, info); if (port == NULL) { snd_seq_client_unlock(cptr); return -ENOENT; } /* get port info */ info->addr = port->addr; snd_seq_get_port_info(port, info); snd_seq_port_unlock(port); snd_seq_client_unlock(cptr); return 0; } #if IS_ENABLED(CONFIG_SND_SEQ_UMP) #define NUM_UMP_INFOS (SNDRV_UMP_MAX_BLOCKS + 1) static void free_ump_info(struct snd_seq_client *client) { int i; if (!client->ump_info) return; for (i = 0; i < NUM_UMP_INFOS; i++) kfree(client->ump_info[i]); kfree(client->ump_info); client->ump_info = NULL; } static void terminate_ump_info_strings(void *p, int type) { if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) { struct snd_ump_endpoint_info *ep = p; ep->name[sizeof(ep->name) - 1] = 0; } else { struct snd_ump_block_info *bp = p; bp->name[sizeof(bp->name) - 1] = 0; } } #ifdef CONFIG_SND_PROC_FS static void dump_ump_info(struct snd_info_buffer *buffer, struct snd_seq_client *client) { struct snd_ump_endpoint_info *ep; struct snd_ump_block_info *bp; int i; if (!client->ump_info) return; ep = client->ump_info[SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT]; if (ep && *ep->name) snd_iprintf(buffer, " UMP Endpoint: \"%s\"\n", ep->name); for (i = 0; i < SNDRV_UMP_MAX_BLOCKS; i++) { bp = client->ump_info[i + 1]; if (bp && *bp->name) { snd_iprintf(buffer, " UMP Block %d: \"%s\" [%s]\n", i, bp->name, bp->active ? "Active" : "Inactive"); snd_iprintf(buffer, " Groups: %d-%d\n", bp->first_group + 1, bp->first_group + bp->num_groups); } } } #endif /* UMP-specific ioctls -- called directly without data copy */ static int snd_seq_ioctl_client_ump_info(struct snd_seq_client *caller, unsigned int cmd, unsigned long arg) { struct snd_seq_client_ump_info __user *argp = (struct snd_seq_client_ump_info __user *)arg; struct snd_seq_client *cptr; int client, type, err = 0; size_t size; void *p; if (get_user(client, &argp->client) || get_user(type, &argp->type)) return -EFAULT; if (cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO && caller->number != client) return -EPERM; if (type < 0 || type >= NUM_UMP_INFOS) return -EINVAL; if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) size = sizeof(struct snd_ump_endpoint_info); else size = sizeof(struct snd_ump_block_info); cptr = snd_seq_client_use_ptr(client); if (!cptr) return -ENOENT; mutex_lock(&cptr->ioctl_mutex); if (!cptr->midi_version) { err = -EBADFD; goto error; } if (cmd == SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO) { if (!cptr->ump_info) p = NULL; else p = cptr->ump_info[type]; if (!p) { err = -ENODEV; goto error; } if (copy_to_user(argp->info, p, size)) { err = -EFAULT; goto error; } } else { if (cptr->type != USER_CLIENT) { err = -EBADFD; goto error; } if (!cptr->ump_info) { cptr->ump_info = kcalloc(NUM_UMP_INFOS, sizeof(void *), GFP_KERNEL); if (!cptr->ump_info) { err = -ENOMEM; goto error; } } p = memdup_user(argp->info, size); if (IS_ERR(p)) { err = PTR_ERR(p); goto error; } kfree(cptr->ump_info[type]); terminate_ump_info_strings(p, type); cptr->ump_info[type] = p; } error: mutex_unlock(&cptr->ioctl_mutex); snd_seq_client_unlock(cptr); return err; } #endif /* -------------------------------------------------------- */ static const struct ioctl_handler { unsigned int cmd; int (*func)(struct snd_seq_client *client, void *arg); } ioctl_handlers[] = { { SNDRV_SEQ_IOCTL_PVERSION, snd_seq_ioctl_pversion }, { SNDRV_SEQ_IOCTL_USER_PVERSION, snd_seq_ioctl_user_pversion }, { SNDRV_SEQ_IOCTL_CLIENT_ID, snd_seq_ioctl_client_id }, { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info }, { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode }, { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info }, { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info }, { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port }, { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port }, { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info }, { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info }, { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port }, { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port }, { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue }, { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue }, { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info }, { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info }, { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue }, { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status }, { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo }, { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo }, { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer }, { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer }, { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client }, { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client }, { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool }, { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool }, { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription }, { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client }, { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port }, { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events }, { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs }, { 0, NULL }, }; static long snd_seq_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct snd_seq_client *client = file->private_data; /* To use kernel stack for ioctl data. */ union { int pversion; int client_id; struct snd_seq_system_info system_info; struct snd_seq_running_info running_info; struct snd_seq_client_info client_info; struct snd_seq_port_info port_info; struct snd_seq_port_subscribe port_subscribe; struct snd_seq_queue_info queue_info; struct snd_seq_queue_status queue_status; struct snd_seq_queue_tempo tempo; struct snd_seq_queue_timer queue_timer; struct snd_seq_queue_client queue_client; struct snd_seq_client_pool client_pool; struct snd_seq_remove_events remove_events; struct snd_seq_query_subs query_subs; } buf; const struct ioctl_handler *handler; unsigned long size; int err; if (snd_BUG_ON(!client)) return -ENXIO; #if IS_ENABLED(CONFIG_SND_SEQ_UMP) /* exception - handling large data */ switch (cmd) { case SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO: case SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO: return snd_seq_ioctl_client_ump_info(client, cmd, arg); } #endif for (handler = ioctl_handlers; handler->cmd > 0; ++handler) { if (handler->cmd == cmd) break; } if (handler->cmd == 0) return -ENOTTY; memset(&buf, 0, sizeof(buf)); /* * All of ioctl commands for ALSA sequencer get an argument of size * within 13 bits. We can safely pick up the size from the command. */ size = _IOC_SIZE(handler->cmd); if (handler->cmd & IOC_IN) { if (copy_from_user(&buf, (const void __user *)arg, size)) return -EFAULT; } mutex_lock(&client->ioctl_mutex); err = handler->func(client, &buf); mutex_unlock(&client->ioctl_mutex); if (err >= 0) { /* Some commands includes a bug in 'dir' field. */ if (handler->cmd == SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT || handler->cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_POOL || (handler->cmd & IOC_OUT)) if (copy_to_user((void __user *)arg, &buf, size)) return -EFAULT; } return err; } #ifdef CONFIG_COMPAT #include "seq_compat.c" #else #define snd_seq_ioctl_compat NULL #endif /* -------------------------------------------------------- */ /* exported to kernel modules */ int snd_seq_create_kernel_client(struct snd_card *card, int client_index, const char *name_fmt, ...) { struct snd_seq_client *client; va_list args; if (snd_BUG_ON(in_interrupt())) return -EBUSY; if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD) return -EINVAL; if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS) return -EINVAL; mutex_lock(&register_mutex); if (card) { client_index += SNDRV_SEQ_GLOBAL_CLIENTS + card->number * SNDRV_SEQ_CLIENTS_PER_CARD; if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) client_index = -1; } /* empty write queue as default */ client = seq_create_client1(client_index, 0); if (client == NULL) { mutex_unlock(&register_mutex); return -EBUSY; /* failure code */ } usage_alloc(&client_usage, 1); client->accept_input = 1; client->accept_output = 1; client->data.kernel.card = card; client->user_pversion = SNDRV_SEQ_VERSION; va_start(args, name_fmt); vsnprintf(client->name, sizeof(client->name), name_fmt, args); va_end(args); client->type = KERNEL_CLIENT; mutex_unlock(&register_mutex); /* make others aware this new client */ snd_seq_system_client_ev_client_start(client->number); /* return client number to caller */ return client->number; } EXPORT_SYMBOL(snd_seq_create_kernel_client); /* exported to kernel modules */ int snd_seq_delete_kernel_client(int client) { struct snd_seq_client *ptr; if (snd_BUG_ON(in_interrupt())) return -EBUSY; ptr = clientptr(client); if (ptr == NULL) return -EINVAL; seq_free_client(ptr); kfree(ptr); return 0; } EXPORT_SYMBOL(snd_seq_delete_kernel_client); /* * exported, called by kernel clients to enqueue events (w/o blocking) * * RETURN VALUE: zero if succeed, negative if error */ int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event *ev, struct file *file, bool blocking) { struct snd_seq_client *cptr; int result; if (snd_BUG_ON(!ev)) return -EINVAL; if (!snd_seq_ev_is_ump(ev)) { if (ev->type == SNDRV_SEQ_EVENT_NONE) return 0; /* ignore this */ if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR) return -EINVAL; /* quoted events can't be enqueued */ } /* fill in client number */ ev->source.client = client; if (check_event_type_and_length(ev)) return -EINVAL; cptr = snd_seq_client_use_ptr(client); if (cptr == NULL) return -EINVAL; if (!cptr->accept_output) { result = -EPERM; } else { /* send it */ mutex_lock(&cptr->ioctl_mutex); result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, false, 0, &cptr->ioctl_mutex); mutex_unlock(&cptr->ioctl_mutex); } snd_seq_client_unlock(cptr); return result; } EXPORT_SYMBOL(snd_seq_kernel_client_enqueue); /* * exported, called by kernel clients to dispatch events directly to other * clients, bypassing the queues. Event time-stamp will be updated. * * RETURN VALUE: negative = delivery failed, * zero, or positive: the number of delivered events */ int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev, int atomic, int hop) { struct snd_seq_client *cptr; int result; if (snd_BUG_ON(!ev)) return -EINVAL; /* fill in client number */ ev->queue = SNDRV_SEQ_QUEUE_DIRECT; ev->source.client = client; if (check_event_type_and_length(ev)) return -EINVAL; cptr = snd_seq_client_use_ptr(client); if (cptr == NULL) return -EINVAL; if (!cptr->accept_output) result = -EPERM; else result = snd_seq_deliver_event(cptr, ev, atomic, hop); snd_seq_client_unlock(cptr); return result; } EXPORT_SYMBOL(snd_seq_kernel_client_dispatch); /** * snd_seq_kernel_client_ctl - operate a command for a client with data in * kernel space. * @clientid: A numerical ID for a client. * @cmd: An ioctl(2) command for ALSA sequencer operation. * @arg: A pointer to data in kernel space. * * Against its name, both kernel/application client can be handled by this * kernel API. A pointer of 'arg' argument should be in kernel space. * * Return: 0 at success. Negative error code at failure. */ int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg) { const struct ioctl_handler *handler; struct snd_seq_client *client; client = clientptr(clientid); if (client == NULL) return -ENXIO; for (handler = ioctl_handlers; handler->cmd > 0; ++handler) { if (handler->cmd == cmd) return handler->func(client, arg); } pr_debug("ALSA: seq unknown ioctl() 0x%x (type='%c', number=0x%02x)\n", cmd, _IOC_TYPE(cmd), _IOC_NR(cmd)); return -ENOTTY; } EXPORT_SYMBOL(snd_seq_kernel_client_ctl); /* exported (for OSS emulator) */ int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait) { struct snd_seq_client *client; client = clientptr(clientid); if (client == NULL) return -ENXIO; if (! snd_seq_write_pool_allocated(client)) return 1; if (snd_seq_pool_poll_wait(client->pool, file, wait)) return 1; return 0; } EXPORT_SYMBOL(snd_seq_kernel_client_write_poll); /* get a sequencer client object; for internal use from a kernel client */ struct snd_seq_client *snd_seq_kernel_client_get(int id) { return snd_seq_client_use_ptr(id); } EXPORT_SYMBOL_GPL(snd_seq_kernel_client_get); /* put a sequencer client object; for internal use from a kernel client */ void snd_seq_kernel_client_put(struct snd_seq_client *cptr) { if (cptr) snd_seq_client_unlock(cptr); } EXPORT_SYMBOL_GPL(snd_seq_kernel_client_put); /*---------------------------------------------------------------------------*/ #ifdef CONFIG_SND_PROC_FS /* * /proc interface */ static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer, struct snd_seq_port_subs_info *group, int is_src, char *msg) { struct list_head *p; struct snd_seq_subscribers *s; int count = 0; down_read(&group->list_mutex); if (list_empty(&group->list_head)) { up_read(&group->list_mutex); return; } snd_iprintf(buffer, msg); list_for_each(p, &group->list_head) { if (is_src) s = list_entry(p, struct snd_seq_subscribers, src_list); else s = list_entry(p, struct snd_seq_subscribers, dest_list); if (count++) snd_iprintf(buffer, ", "); snd_iprintf(buffer, "%d:%d", is_src ? s->info.dest.client : s->info.sender.client, is_src ? s->info.dest.port : s->info.sender.port); if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP) snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue); if (group->exclusive) snd_iprintf(buffer, "[ex]"); } up_read(&group->list_mutex); snd_iprintf(buffer, "\n"); } #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-') #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-') #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e') #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-') static const char *port_direction_name(unsigned char dir) { static const char *names[4] = { "-", "In", "Out", "In/Out" }; if (dir > SNDRV_SEQ_PORT_DIR_BIDIRECTION) return "Invalid"; return names[dir]; } static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer, struct snd_seq_client *client) { struct snd_seq_client_port *p; mutex_lock(&client->ports_mutex); list_for_each_entry(p, &client->ports_list_head, list) { if (p->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) continue; snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c) [%s]\n", p->addr.port, p->name, FLAG_PERM_RD(p->capability), FLAG_PERM_WR(p->capability), FLAG_PERM_EX(p->capability), FLAG_PERM_DUPLEX(p->capability), port_direction_name(p->direction)); snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: "); snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: "); } mutex_unlock(&client->ports_mutex); } static const char *midi_version_string(unsigned int version) { switch (version) { case SNDRV_SEQ_CLIENT_LEGACY_MIDI: return "Legacy"; case SNDRV_SEQ_CLIENT_UMP_MIDI_1_0: return "UMP MIDI1"; case SNDRV_SEQ_CLIENT_UMP_MIDI_2_0: return "UMP MIDI2"; default: return "Unknown"; } } /* exported to seq_info.c */ void snd_seq_info_clients_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { int c; struct snd_seq_client *client; snd_iprintf(buffer, "Client info\n"); snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur); snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak); snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS); snd_iprintf(buffer, "\n"); /* list the client table */ for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) { client = snd_seq_client_use_ptr(c); if (client == NULL) continue; if (client->type == NO_CLIENT) { snd_seq_client_unlock(client); continue; } snd_iprintf(buffer, "Client %3d : \"%s\" [%s %s]\n", c, client->name, client->type == USER_CLIENT ? "User" : "Kernel", midi_version_string(client->midi_version)); #if IS_ENABLED(CONFIG_SND_SEQ_UMP) dump_ump_info(buffer, client); #endif snd_seq_info_dump_ports(buffer, client); if (snd_seq_write_pool_allocated(client)) { snd_iprintf(buffer, " Output pool :\n"); snd_seq_info_pool(buffer, client->pool, " "); } if (client->type == USER_CLIENT && client->data.user.fifo && client->data.user.fifo->pool) { snd_iprintf(buffer, " Input pool :\n"); snd_seq_info_pool(buffer, client->data.user.fifo->pool, " "); } snd_seq_client_unlock(client); } } #endif /* CONFIG_SND_PROC_FS */ /*---------------------------------------------------------------------------*/ /* * REGISTRATION PART */ static const struct file_operations snd_seq_f_ops = { .owner = THIS_MODULE, .read = snd_seq_read, .write = snd_seq_write, .open = snd_seq_open, .release = snd_seq_release, .llseek = no_llseek, .poll = snd_seq_poll, .unlocked_ioctl = snd_seq_ioctl, .compat_ioctl = snd_seq_ioctl_compat, }; static struct device *seq_dev; /* * register sequencer device */ int __init snd_sequencer_device_init(void) { int err; err = snd_device_alloc(&seq_dev, NULL); if (err < 0) return err; dev_set_name(seq_dev, "seq"); mutex_lock(&register_mutex); err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0, &snd_seq_f_ops, NULL, seq_dev); mutex_unlock(&register_mutex); if (err < 0) { put_device(seq_dev); return err; } return 0; } /* * unregister sequencer device */ void snd_sequencer_device_done(void) { snd_unregister_device(seq_dev); put_device(seq_dev); }
linux-master
sound/core/seq/seq_clientmgr.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer Memory Manager * Copyright (c) 1998 by Frank van de Pol <[email protected]> * Jaroslav Kysela <[email protected]> * 2000 by Takashi Iwai <[email protected]> */ #include <linux/init.h> #include <linux/export.h> #include <linux/slab.h> #include <linux/sched/signal.h> #include <linux/mm.h> #include <sound/core.h> #include <sound/seq_kernel.h> #include "seq_memory.h" #include "seq_queue.h" #include "seq_info.h" #include "seq_lock.h" static inline int snd_seq_pool_available(struct snd_seq_pool *pool) { return pool->total_elements - atomic_read(&pool->counter); } static inline int snd_seq_output_ok(struct snd_seq_pool *pool) { return snd_seq_pool_available(pool) >= pool->room; } /* * Variable length event: * The event like sysex uses variable length type. * The external data may be stored in three different formats. * 1) kernel space * This is the normal case. * ext.data.len = length * ext.data.ptr = buffer pointer * 2) user space * When an event is generated via read(), the external data is * kept in user space until expanded. * ext.data.len = length | SNDRV_SEQ_EXT_USRPTR * ext.data.ptr = userspace pointer * 3) chained cells * When the variable length event is enqueued (in prioq or fifo), * the external data is decomposed to several cells. * ext.data.len = length | SNDRV_SEQ_EXT_CHAINED * ext.data.ptr = the additiona cell head * -> cell.next -> cell.next -> .. */ /* * exported: * call dump function to expand external data. */ static int get_var_len(const struct snd_seq_event *event) { if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) return -EINVAL; return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK; } static int dump_var_event(const struct snd_seq_event *event, snd_seq_dump_func_t func, void *private_data, int offset, int maxlen) { int len, err; struct snd_seq_event_cell *cell; len = get_var_len(event); if (len <= 0) return len; if (len <= offset) return 0; if (maxlen && len > offset + maxlen) len = offset + maxlen; if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) { char buf[32]; char __user *curptr = (char __force __user *)event->data.ext.ptr; curptr += offset; len -= offset; while (len > 0) { int size = sizeof(buf); if (len < size) size = len; if (copy_from_user(buf, curptr, size)) return -EFAULT; err = func(private_data, buf, size); if (err < 0) return err; curptr += size; len -= size; } return 0; } if (!(event->data.ext.len & SNDRV_SEQ_EXT_CHAINED)) return func(private_data, event->data.ext.ptr + offset, len - offset); cell = (struct snd_seq_event_cell *)event->data.ext.ptr; for (; len > 0 && cell; cell = cell->next) { int size = sizeof(struct snd_seq_event); char *curptr = (char *)&cell->event; if (offset >= size) { offset -= size; len -= size; continue; } if (len < size) size = len; err = func(private_data, curptr + offset, size - offset); if (err < 0) return err; offset = 0; len -= size; } return 0; } int snd_seq_dump_var_event(const struct snd_seq_event *event, snd_seq_dump_func_t func, void *private_data) { return dump_var_event(event, func, private_data, 0, 0); } EXPORT_SYMBOL(snd_seq_dump_var_event); /* * exported: * expand the variable length event to linear buffer space. */ static int seq_copy_in_kernel(void *ptr, void *src, int size) { char **bufptr = ptr; memcpy(*bufptr, src, size); *bufptr += size; return 0; } static int seq_copy_in_user(void *ptr, void *src, int size) { char __user **bufptr = ptr; if (copy_to_user(*bufptr, src, size)) return -EFAULT; *bufptr += size; return 0; } static int expand_var_event(const struct snd_seq_event *event, int offset, int size, char *buf, bool in_kernel) { if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) { if (! in_kernel) return -EINVAL; if (copy_from_user(buf, (char __force __user *)event->data.ext.ptr + offset, size)) return -EFAULT; return 0; } return dump_var_event(event, in_kernel ? seq_copy_in_kernel : seq_copy_in_user, &buf, offset, size); } int snd_seq_expand_var_event(const struct snd_seq_event *event, int count, char *buf, int in_kernel, int size_aligned) { int len, newlen, err; len = get_var_len(event); if (len < 0) return len; newlen = len; if (size_aligned > 0) newlen = roundup(len, size_aligned); if (count < newlen) return -EAGAIN; err = expand_var_event(event, 0, len, buf, in_kernel); if (err < 0) return err; if (len != newlen) { if (in_kernel) memset(buf + len, 0, newlen - len); else if (clear_user((__force void __user *)buf + len, newlen - len)) return -EFAULT; } return newlen; } EXPORT_SYMBOL(snd_seq_expand_var_event); int snd_seq_expand_var_event_at(const struct snd_seq_event *event, int count, char *buf, int offset) { int len, err; len = get_var_len(event); if (len < 0) return len; if (len <= offset) return 0; len -= offset; if (len > count) len = count; err = expand_var_event(event, offset, count, buf, true); if (err < 0) return err; return len; } EXPORT_SYMBOL_GPL(snd_seq_expand_var_event_at); /* * release this cell, free extended data if available */ static inline void free_cell(struct snd_seq_pool *pool, struct snd_seq_event_cell *cell) { cell->next = pool->free; pool->free = cell; atomic_dec(&pool->counter); } void snd_seq_cell_free(struct snd_seq_event_cell * cell) { unsigned long flags; struct snd_seq_pool *pool; if (snd_BUG_ON(!cell)) return; pool = cell->pool; if (snd_BUG_ON(!pool)) return; spin_lock_irqsave(&pool->lock, flags); free_cell(pool, cell); if (snd_seq_ev_is_variable(&cell->event)) { if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) { struct snd_seq_event_cell *curp, *nextptr; curp = cell->event.data.ext.ptr; for (; curp; curp = nextptr) { nextptr = curp->next; curp->next = pool->free; free_cell(pool, curp); } } } if (waitqueue_active(&pool->output_sleep)) { /* has enough space now? */ if (snd_seq_output_ok(pool)) wake_up(&pool->output_sleep); } spin_unlock_irqrestore(&pool->lock, flags); } /* * allocate an event cell. */ static int snd_seq_cell_alloc(struct snd_seq_pool *pool, struct snd_seq_event_cell **cellp, int nonblock, struct file *file, struct mutex *mutexp) { struct snd_seq_event_cell *cell; unsigned long flags; int err = -EAGAIN; wait_queue_entry_t wait; if (pool == NULL) return -EINVAL; *cellp = NULL; init_waitqueue_entry(&wait, current); spin_lock_irqsave(&pool->lock, flags); if (pool->ptr == NULL) { /* not initialized */ pr_debug("ALSA: seq: pool is not initialized\n"); err = -EINVAL; goto __error; } while (pool->free == NULL && ! nonblock && ! pool->closing) { set_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&pool->output_sleep, &wait); spin_unlock_irqrestore(&pool->lock, flags); if (mutexp) mutex_unlock(mutexp); schedule(); if (mutexp) mutex_lock(mutexp); spin_lock_irqsave(&pool->lock, flags); remove_wait_queue(&pool->output_sleep, &wait); /* interrupted? */ if (signal_pending(current)) { err = -ERESTARTSYS; goto __error; } } if (pool->closing) { /* closing.. */ err = -ENOMEM; goto __error; } cell = pool->free; if (cell) { int used; pool->free = cell->next; atomic_inc(&pool->counter); used = atomic_read(&pool->counter); if (pool->max_used < used) pool->max_used = used; pool->event_alloc_success++; /* clear cell pointers */ cell->next = NULL; err = 0; } else pool->event_alloc_failures++; *cellp = cell; __error: spin_unlock_irqrestore(&pool->lock, flags); return err; } /* * duplicate the event to a cell. * if the event has external data, the data is decomposed to additional * cells. */ int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event, struct snd_seq_event_cell **cellp, int nonblock, struct file *file, struct mutex *mutexp) { int ncells, err; unsigned int extlen; struct snd_seq_event_cell *cell; int size; *cellp = NULL; ncells = 0; extlen = 0; if (snd_seq_ev_is_variable(event)) { extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK; ncells = DIV_ROUND_UP(extlen, sizeof(struct snd_seq_event)); } if (ncells >= pool->total_elements) return -ENOMEM; err = snd_seq_cell_alloc(pool, &cell, nonblock, file, mutexp); if (err < 0) return err; /* copy the event */ size = snd_seq_event_packet_size(event); memcpy(&cell->ump, event, size); #if IS_ENABLED(CONFIG_SND_SEQ_UMP) if (size < sizeof(cell->event)) cell->ump.raw.extra = 0; #endif /* decompose */ if (snd_seq_ev_is_variable(event)) { int len = extlen; int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED; int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR; struct snd_seq_event_cell *src, *tmp, *tail; char *buf; cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED; cell->event.data.ext.ptr = NULL; src = (struct snd_seq_event_cell *)event->data.ext.ptr; buf = (char *)event->data.ext.ptr; tail = NULL; while (ncells-- > 0) { size = sizeof(struct snd_seq_event); if (len < size) size = len; err = snd_seq_cell_alloc(pool, &tmp, nonblock, file, mutexp); if (err < 0) goto __error; if (cell->event.data.ext.ptr == NULL) cell->event.data.ext.ptr = tmp; if (tail) tail->next = tmp; tail = tmp; /* copy chunk */ if (is_chained && src) { tmp->event = src->event; src = src->next; } else if (is_usrptr) { if (copy_from_user(&tmp->event, (char __force __user *)buf, size)) { err = -EFAULT; goto __error; } } else { memcpy(&tmp->event, buf, size); } buf += size; len -= size; } } *cellp = cell; return 0; __error: snd_seq_cell_free(cell); return err; } /* poll wait */ int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file, poll_table *wait) { poll_wait(file, &pool->output_sleep, wait); return snd_seq_output_ok(pool); } /* allocate room specified number of events */ int snd_seq_pool_init(struct snd_seq_pool *pool) { int cell; struct snd_seq_event_cell *cellptr; if (snd_BUG_ON(!pool)) return -EINVAL; cellptr = kvmalloc_array(sizeof(struct snd_seq_event_cell), pool->size, GFP_KERNEL); if (!cellptr) return -ENOMEM; /* add new cells to the free cell list */ spin_lock_irq(&pool->lock); if (pool->ptr) { spin_unlock_irq(&pool->lock); kvfree(cellptr); return 0; } pool->ptr = cellptr; pool->free = NULL; for (cell = 0; cell < pool->size; cell++) { cellptr = pool->ptr + cell; cellptr->pool = pool; cellptr->next = pool->free; pool->free = cellptr; } pool->room = (pool->size + 1) / 2; /* init statistics */ pool->max_used = 0; pool->total_elements = pool->size; spin_unlock_irq(&pool->lock); return 0; } /* refuse the further insertion to the pool */ void snd_seq_pool_mark_closing(struct snd_seq_pool *pool) { unsigned long flags; if (snd_BUG_ON(!pool)) return; spin_lock_irqsave(&pool->lock, flags); pool->closing = 1; spin_unlock_irqrestore(&pool->lock, flags); } /* remove events */ int snd_seq_pool_done(struct snd_seq_pool *pool) { struct snd_seq_event_cell *ptr; if (snd_BUG_ON(!pool)) return -EINVAL; /* wait for closing all threads */ if (waitqueue_active(&pool->output_sleep)) wake_up(&pool->output_sleep); while (atomic_read(&pool->counter) > 0) schedule_timeout_uninterruptible(1); /* release all resources */ spin_lock_irq(&pool->lock); ptr = pool->ptr; pool->ptr = NULL; pool->free = NULL; pool->total_elements = 0; spin_unlock_irq(&pool->lock); kvfree(ptr); spin_lock_irq(&pool->lock); pool->closing = 0; spin_unlock_irq(&pool->lock); return 0; } /* init new memory pool */ struct snd_seq_pool *snd_seq_pool_new(int poolsize) { struct snd_seq_pool *pool; /* create pool block */ pool = kzalloc(sizeof(*pool), GFP_KERNEL); if (!pool) return NULL; spin_lock_init(&pool->lock); pool->ptr = NULL; pool->free = NULL; pool->total_elements = 0; atomic_set(&pool->counter, 0); pool->closing = 0; init_waitqueue_head(&pool->output_sleep); pool->size = poolsize; /* init statistics */ pool->max_used = 0; return pool; } /* remove memory pool */ int snd_seq_pool_delete(struct snd_seq_pool **ppool) { struct snd_seq_pool *pool = *ppool; *ppool = NULL; if (pool == NULL) return 0; snd_seq_pool_mark_closing(pool); snd_seq_pool_done(pool); kfree(pool); return 0; } /* exported to seq_clientmgr.c */ void snd_seq_info_pool(struct snd_info_buffer *buffer, struct snd_seq_pool *pool, char *space) { if (pool == NULL) return; snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements); snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter)); snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used); snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success); snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures); }
linux-master
sound/core/seq/seq_memory.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer main module * Copyright (c) 1998-1999 by Frank van de Pol <[email protected]> */ #include <linux/init.h> #include <linux/module.h> #include <linux/device.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/seq_kernel.h> #include "seq_clientmgr.h" #include "seq_memory.h" #include "seq_queue.h" #include "seq_lock.h" #include "seq_timer.h" #include "seq_system.h" #include "seq_info.h" #include <sound/minors.h> #include <sound/seq_device.h> #if defined(CONFIG_SND_SEQ_DUMMY_MODULE) int seq_client_load[15] = {[0] = SNDRV_SEQ_CLIENT_DUMMY, [1 ... 14] = -1}; #else int seq_client_load[15] = {[0 ... 14] = -1}; #endif int seq_default_timer_class = SNDRV_TIMER_CLASS_GLOBAL; int seq_default_timer_sclass = SNDRV_TIMER_SCLASS_NONE; int seq_default_timer_card = -1; int seq_default_timer_device = #ifdef CONFIG_SND_SEQ_HRTIMER_DEFAULT SNDRV_TIMER_GLOBAL_HRTIMER #else SNDRV_TIMER_GLOBAL_SYSTEM #endif ; int seq_default_timer_subdevice = 0; int seq_default_timer_resolution = 0; /* Hz */ MODULE_AUTHOR("Frank van de Pol <[email protected]>, Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer."); MODULE_LICENSE("GPL"); module_param_array(seq_client_load, int, NULL, 0444); MODULE_PARM_DESC(seq_client_load, "The numbers of global (system) clients to load through kmod."); module_param(seq_default_timer_class, int, 0644); MODULE_PARM_DESC(seq_default_timer_class, "The default timer class."); module_param(seq_default_timer_sclass, int, 0644); MODULE_PARM_DESC(seq_default_timer_sclass, "The default timer slave class."); module_param(seq_default_timer_card, int, 0644); MODULE_PARM_DESC(seq_default_timer_card, "The default timer card number."); module_param(seq_default_timer_device, int, 0644); MODULE_PARM_DESC(seq_default_timer_device, "The default timer device number."); module_param(seq_default_timer_subdevice, int, 0644); MODULE_PARM_DESC(seq_default_timer_subdevice, "The default timer subdevice number."); module_param(seq_default_timer_resolution, int, 0644); MODULE_PARM_DESC(seq_default_timer_resolution, "The default timer resolution in Hz."); MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_SEQUENCER); MODULE_ALIAS("devname:snd/seq"); /* * INIT PART */ static int __init alsa_seq_init(void) { int err; err = client_init_data(); if (err < 0) goto error; /* register sequencer device */ err = snd_sequencer_device_init(); if (err < 0) goto error; /* register proc interface */ err = snd_seq_info_init(); if (err < 0) goto error_device; /* register our internal client */ err = snd_seq_system_client_init(); if (err < 0) goto error_info; snd_seq_autoload_init(); return 0; error_info: snd_seq_info_done(); error_device: snd_sequencer_device_done(); error: return err; } static void __exit alsa_seq_exit(void) { /* unregister our internal client */ snd_seq_system_client_done(); /* unregister proc interface */ snd_seq_info_done(); /* delete timing queues */ snd_seq_queues_delete(); /* unregister sequencer device */ snd_sequencer_device_done(); snd_seq_autoload_exit(); } module_init(alsa_seq_init) module_exit(alsa_seq_exit)
linux-master
sound/core/seq/seq.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * 32bit -> 64bit ioctl wrapper for sequencer API * Copyright (c) by Takashi Iwai <[email protected]> */ /* This file included from seq.c */ #include <linux/compat.h> #include <linux/slab.h> struct snd_seq_port_info32 { struct snd_seq_addr addr; /* client/port numbers */ char name[64]; /* port name */ u32 capability; /* port capability bits */ u32 type; /* port type bits */ s32 midi_channels; /* channels per MIDI port */ s32 midi_voices; /* voices per MIDI port */ s32 synth_voices; /* voices per SYNTH port */ s32 read_use; /* R/O: subscribers for output (from this port) */ s32 write_use; /* R/O: subscribers for input (to this port) */ u32 kernel; /* reserved for kernel use (must be NULL) */ u32 flags; /* misc. conditioning */ unsigned char time_queue; /* queue # for timestamping */ char reserved[59]; /* for future use */ }; static int snd_seq_call_port_info_ioctl(struct snd_seq_client *client, unsigned int cmd, struct snd_seq_port_info32 __user *data32) { int err = -EFAULT; struct snd_seq_port_info *data; data = kmalloc(sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM; if (copy_from_user(data, data32, sizeof(*data32)) || get_user(data->flags, &data32->flags) || get_user(data->time_queue, &data32->time_queue)) goto error; data->kernel = NULL; err = snd_seq_kernel_client_ctl(client->number, cmd, data); if (err < 0) goto error; if (copy_to_user(data32, data, sizeof(*data32)) || put_user(data->flags, &data32->flags) || put_user(data->time_queue, &data32->time_queue)) err = -EFAULT; error: kfree(data); return err; } /* */ enum { SNDRV_SEQ_IOCTL_CREATE_PORT32 = _IOWR('S', 0x20, struct snd_seq_port_info32), SNDRV_SEQ_IOCTL_DELETE_PORT32 = _IOW ('S', 0x21, struct snd_seq_port_info32), SNDRV_SEQ_IOCTL_GET_PORT_INFO32 = _IOWR('S', 0x22, struct snd_seq_port_info32), SNDRV_SEQ_IOCTL_SET_PORT_INFO32 = _IOW ('S', 0x23, struct snd_seq_port_info32), SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT32 = _IOWR('S', 0x52, struct snd_seq_port_info32), }; static long snd_seq_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) { struct snd_seq_client *client = file->private_data; void __user *argp = compat_ptr(arg); if (snd_BUG_ON(!client)) return -ENXIO; switch (cmd) { case SNDRV_SEQ_IOCTL_PVERSION: case SNDRV_SEQ_IOCTL_USER_PVERSION: case SNDRV_SEQ_IOCTL_CLIENT_ID: case SNDRV_SEQ_IOCTL_SYSTEM_INFO: case SNDRV_SEQ_IOCTL_GET_CLIENT_INFO: case SNDRV_SEQ_IOCTL_SET_CLIENT_INFO: case SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO: case SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO: case SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT: case SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT: case SNDRV_SEQ_IOCTL_CREATE_QUEUE: case SNDRV_SEQ_IOCTL_DELETE_QUEUE: case SNDRV_SEQ_IOCTL_GET_QUEUE_INFO: case SNDRV_SEQ_IOCTL_SET_QUEUE_INFO: case SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE: case SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS: case SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO: case SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO: case SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER: case SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER: case SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT: case SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT: case SNDRV_SEQ_IOCTL_GET_CLIENT_POOL: case SNDRV_SEQ_IOCTL_SET_CLIENT_POOL: case SNDRV_SEQ_IOCTL_REMOVE_EVENTS: case SNDRV_SEQ_IOCTL_QUERY_SUBS: case SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION: case SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT: case SNDRV_SEQ_IOCTL_RUNNING_MODE: return snd_seq_ioctl(file, cmd, arg); case SNDRV_SEQ_IOCTL_CREATE_PORT32: return snd_seq_call_port_info_ioctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, argp); case SNDRV_SEQ_IOCTL_DELETE_PORT32: return snd_seq_call_port_info_ioctl(client, SNDRV_SEQ_IOCTL_DELETE_PORT, argp); case SNDRV_SEQ_IOCTL_GET_PORT_INFO32: return snd_seq_call_port_info_ioctl(client, SNDRV_SEQ_IOCTL_GET_PORT_INFO, argp); case SNDRV_SEQ_IOCTL_SET_PORT_INFO32: return snd_seq_call_port_info_ioctl(client, SNDRV_SEQ_IOCTL_SET_PORT_INFO, argp); case SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT32: return snd_seq_call_port_info_ioctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, argp); } return -ENOIOCTLCMD; }
linux-master
sound/core/seq/seq_compat.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer System services Client * Copyright (c) 1998-1999 by Frank van de Pol <[email protected]> */ #include <linux/init.h> #include <linux/export.h> #include <linux/slab.h> #include <sound/core.h> #include "seq_system.h" #include "seq_timer.h" #include "seq_queue.h" /* internal client that provide system services, access to timer etc. */ /* * Port "Timer" * - send tempo /start/stop etc. events to this port to manipulate the * queue's timer. The queue address is specified in * data.queue.queue. * - this port supports subscription. The received timer events are * broadcasted to all subscribed clients. The modified tempo * value is stored on data.queue.value. * The modifier client/port is not send. * * Port "Announce" * - does not receive message * - supports supscription. For each client or port attaching to or * detaching from the system an announcement is send to the subscribed * clients. * * Idea: the subscription mechanism might also work handy for distributing * synchronisation and timing information. In this case we would ideally have * a list of subscribers for each type of sync (time, tick), for each timing * queue. * * NOTE: the queue to be started, stopped, etc. must be specified * in data.queue.addr.queue field. queue is used only for * scheduling, and no longer referred as affected queue. * They are used only for timer broadcast (see above). * -- iwai */ /* client id of our system client */ static int sysclient = -1; /* port id numbers for this client */ static int announce_port = -1; /* fill standard header data, source port & channel are filled in */ static int setheader(struct snd_seq_event * ev, int client, int port) { if (announce_port < 0) return -ENODEV; memset(ev, 0, sizeof(struct snd_seq_event)); ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED; ev->source.client = sysclient; ev->source.port = announce_port; ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; /* fill data */ /*ev->data.addr.queue = SNDRV_SEQ_ADDRESS_UNKNOWN;*/ ev->data.addr.client = client; ev->data.addr.port = port; return 0; } /* entry points for broadcasting system events */ void snd_seq_system_broadcast(int client, int port, int type) { struct snd_seq_event ev; if (setheader(&ev, client, port) < 0) return; ev.type = type; snd_seq_kernel_client_dispatch(sysclient, &ev, 0, 0); } EXPORT_SYMBOL_GPL(snd_seq_system_broadcast); /* entry points for broadcasting system events */ int snd_seq_system_notify(int client, int port, struct snd_seq_event *ev) { ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; ev->source.client = sysclient; ev->source.port = announce_port; ev->dest.client = client; ev->dest.port = port; return snd_seq_kernel_client_dispatch(sysclient, ev, 0, 0); } /* call-back handler for timer events */ static int event_input_timer(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop) { return snd_seq_control_queue(ev, atomic, hop); } /* register our internal client */ int __init snd_seq_system_client_init(void) { struct snd_seq_port_callback pcallbacks; struct snd_seq_port_info *port; int err; port = kzalloc(sizeof(*port), GFP_KERNEL); if (!port) return -ENOMEM; memset(&pcallbacks, 0, sizeof(pcallbacks)); pcallbacks.owner = THIS_MODULE; pcallbacks.event_input = event_input_timer; /* register client */ sysclient = snd_seq_create_kernel_client(NULL, 0, "System"); if (sysclient < 0) { kfree(port); return sysclient; } /* register timer */ strcpy(port->name, "Timer"); port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */ port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */ port->kernel = &pcallbacks; port->type = 0; port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; port->addr.client = sysclient; port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER; err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port); if (err < 0) goto error_port; /* register announcement port */ strcpy(port->name, "Announce"); port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */ port->kernel = NULL; port->type = 0; port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; port->addr.client = sysclient; port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE; err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port); if (err < 0) goto error_port; announce_port = port->addr.port; kfree(port); return 0; error_port: snd_seq_system_client_done(); kfree(port); return err; } /* unregister our internal client */ void snd_seq_system_client_done(void) { int oldsysclient = sysclient; if (oldsysclient >= 0) { sysclient = -1; announce_port = -1; snd_seq_delete_kernel_client(oldsysclient); } }
linux-master
sound/core/seq/seq_system.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer /proc interface * Copyright (c) 1998 by Frank van de Pol <[email protected]> */ #include <linux/init.h> #include <linux/export.h> #include <sound/core.h> #include "seq_info.h" #include "seq_clientmgr.h" #include "seq_timer.h" static struct snd_info_entry *queues_entry; static struct snd_info_entry *clients_entry; static struct snd_info_entry *timer_entry; static struct snd_info_entry * __init create_info_entry(char *name, void (*read)(struct snd_info_entry *, struct snd_info_buffer *)) { struct snd_info_entry *entry; entry = snd_info_create_module_entry(THIS_MODULE, name, snd_seq_root); if (entry == NULL) return NULL; entry->content = SNDRV_INFO_CONTENT_TEXT; entry->c.text.read = read; if (snd_info_register(entry) < 0) { snd_info_free_entry(entry); return NULL; } return entry; } void snd_seq_info_done(void) { snd_info_free_entry(queues_entry); snd_info_free_entry(clients_entry); snd_info_free_entry(timer_entry); } /* create all our /proc entries */ int __init snd_seq_info_init(void) { queues_entry = create_info_entry("queues", snd_seq_info_queues_read); clients_entry = create_info_entry("clients", snd_seq_info_clients_read); timer_entry = create_info_entry("timer", snd_seq_info_timer_read); if (!queues_entry || !clients_entry || !timer_entry) goto error; return 0; error: snd_seq_info_done(); return -ENOMEM; }
linux-master
sound/core/seq/seq_info.c
// SPDX-License-Identifier: GPL-2.0-or-later /* ALSA sequencer binding for UMP device */ #include <linux/init.h> #include <linux/slab.h> #include <linux/errno.h> #include <linux/mutex.h> #include <linux/string.h> #include <linux/module.h> #include <asm/byteorder.h> #include <sound/core.h> #include <sound/ump.h> #include <sound/seq_kernel.h> #include <sound/seq_device.h> #include "seq_clientmgr.h" #include "seq_system.h" struct seq_ump_client; struct seq_ump_group; enum { STR_IN = SNDRV_RAWMIDI_STREAM_INPUT, STR_OUT = SNDRV_RAWMIDI_STREAM_OUTPUT }; /* object per UMP group; corresponding to a sequencer port */ struct seq_ump_group { int group; /* group index (0-based) */ unsigned int dir_bits; /* directions */ bool active; /* activeness */ char name[64]; /* seq port name */ }; /* context for UMP input parsing, per EP */ struct seq_ump_input_buffer { unsigned char len; /* total length in words */ unsigned char pending; /* pending words */ unsigned char type; /* parsed UMP packet type */ unsigned char group; /* parsed UMP packet group */ u32 buf[4]; /* incoming UMP packet */ }; /* sequencer client, per UMP EP (rawmidi) */ struct seq_ump_client { struct snd_ump_endpoint *ump; /* assigned endpoint */ int seq_client; /* sequencer client id */ int opened[2]; /* current opens for each direction */ struct snd_rawmidi_file out_rfile; /* rawmidi for output */ struct seq_ump_input_buffer input; /* input parser context */ struct seq_ump_group groups[SNDRV_UMP_MAX_GROUPS]; /* table of groups */ void *ump_info[SNDRV_UMP_MAX_BLOCKS + 1]; /* shadow of seq client ump_info */ struct work_struct group_notify_work; /* FB change notification */ }; /* number of 32bit words for each UMP message type */ static unsigned char ump_packet_words[0x10] = { 1, 1, 1, 2, 2, 4, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4 }; /* conversion between UMP group and seq port; * assume the port number is equal with UMP group number (1-based) */ static unsigned char ump_group_to_seq_port(unsigned char group) { return group + 1; } /* process the incoming rawmidi stream */ static void seq_ump_input_receive(struct snd_ump_endpoint *ump, const u32 *val, int words) { struct seq_ump_client *client = ump->seq_client; struct snd_seq_ump_event ev = {}; if (!client->opened[STR_IN]) return; if (ump_is_groupless_msg(ump_message_type(*val))) ev.source.port = 0; /* UMP EP port */ else ev.source.port = ump_group_to_seq_port(ump_message_group(*val)); ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; ev.flags = SNDRV_SEQ_EVENT_UMP; memcpy(ev.ump, val, words << 2); snd_seq_kernel_client_dispatch(client->seq_client, (struct snd_seq_event *)&ev, true, 0); } /* process an input sequencer event; only deal with UMP types */ static int seq_ump_process_event(struct snd_seq_event *ev, int direct, void *private_data, int atomic, int hop) { struct seq_ump_client *client = private_data; struct snd_rawmidi_substream *substream; struct snd_seq_ump_event *ump_ev; unsigned char type; int len; substream = client->out_rfile.output; if (!substream) return -ENODEV; if (!snd_seq_ev_is_ump(ev)) return 0; /* invalid event, skip */ ump_ev = (struct snd_seq_ump_event *)ev; type = ump_message_type(ump_ev->ump[0]); len = ump_packet_words[type]; if (len > 4) return 0; // invalid - skip snd_rawmidi_kernel_write(substream, ev->data.raw8.d, len << 2); return 0; } /* open the rawmidi */ static int seq_ump_client_open(struct seq_ump_client *client, int dir) { struct snd_ump_endpoint *ump = client->ump; int err = 0; mutex_lock(&ump->open_mutex); if (dir == STR_OUT && !client->opened[dir]) { err = snd_rawmidi_kernel_open(&ump->core, 0, SNDRV_RAWMIDI_LFLG_OUTPUT | SNDRV_RAWMIDI_LFLG_APPEND, &client->out_rfile); if (err < 0) goto unlock; } client->opened[dir]++; unlock: mutex_unlock(&ump->open_mutex); return err; } /* close the rawmidi */ static int seq_ump_client_close(struct seq_ump_client *client, int dir) { struct snd_ump_endpoint *ump = client->ump; mutex_lock(&ump->open_mutex); if (!--client->opened[dir]) if (dir == STR_OUT) snd_rawmidi_kernel_release(&client->out_rfile); mutex_unlock(&ump->open_mutex); return 0; } /* sequencer subscription ops for each client */ static int seq_ump_subscribe(void *pdata, struct snd_seq_port_subscribe *info) { struct seq_ump_client *client = pdata; return seq_ump_client_open(client, STR_IN); } static int seq_ump_unsubscribe(void *pdata, struct snd_seq_port_subscribe *info) { struct seq_ump_client *client = pdata; return seq_ump_client_close(client, STR_IN); } static int seq_ump_use(void *pdata, struct snd_seq_port_subscribe *info) { struct seq_ump_client *client = pdata; return seq_ump_client_open(client, STR_OUT); } static int seq_ump_unuse(void *pdata, struct snd_seq_port_subscribe *info) { struct seq_ump_client *client = pdata; return seq_ump_client_close(client, STR_OUT); } /* fill port_info from the given UMP EP and group info */ static void fill_port_info(struct snd_seq_port_info *port, struct seq_ump_client *client, struct seq_ump_group *group) { unsigned int rawmidi_info = client->ump->core.info_flags; port->addr.client = client->seq_client; port->addr.port = ump_group_to_seq_port(group->group); port->capability = 0; if (rawmidi_info & SNDRV_RAWMIDI_INFO_OUTPUT) port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; if (rawmidi_info & SNDRV_RAWMIDI_INFO_INPUT) port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; if (rawmidi_info & SNDRV_RAWMIDI_INFO_DUPLEX) port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; if (group->dir_bits & (1 << STR_IN)) port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; if (group->dir_bits & (1 << STR_OUT)) port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; port->ump_group = group->group + 1; if (!group->active) port->capability |= SNDRV_SEQ_PORT_CAP_INACTIVE; port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | SNDRV_SEQ_PORT_TYPE_MIDI_UMP | SNDRV_SEQ_PORT_TYPE_HARDWARE | SNDRV_SEQ_PORT_TYPE_PORT; port->midi_channels = 16; if (*group->name) snprintf(port->name, sizeof(port->name), "Group %d (%.53s)", group->group + 1, group->name); else sprintf(port->name, "Group %d", group->group + 1); } /* create a new sequencer port per UMP group */ static int seq_ump_group_init(struct seq_ump_client *client, int group_index) { struct seq_ump_group *group = &client->groups[group_index]; struct snd_seq_port_info *port; struct snd_seq_port_callback pcallbacks; int err; port = kzalloc(sizeof(*port), GFP_KERNEL); if (!port) { err = -ENOMEM; goto error; } fill_port_info(port, client, group); port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; memset(&pcallbacks, 0, sizeof(pcallbacks)); pcallbacks.owner = THIS_MODULE; pcallbacks.private_data = client; pcallbacks.subscribe = seq_ump_subscribe; pcallbacks.unsubscribe = seq_ump_unsubscribe; pcallbacks.use = seq_ump_use; pcallbacks.unuse = seq_ump_unuse; pcallbacks.event_input = seq_ump_process_event; port->kernel = &pcallbacks; err = snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port); error: kfree(port); return err; } /* update the sequencer ports; called from notify_fb_change callback */ static void update_port_infos(struct seq_ump_client *client) { struct snd_seq_port_info *old, *new; int i, err; old = kzalloc(sizeof(*old), GFP_KERNEL); new = kzalloc(sizeof(*new), GFP_KERNEL); if (!old || !new) goto error; for (i = 0; i < SNDRV_UMP_MAX_GROUPS; i++) { old->addr.client = client->seq_client; old->addr.port = i; err = snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_GET_PORT_INFO, old); if (err < 0) goto error; fill_port_info(new, client, &client->groups[i]); if (old->capability == new->capability && !strcmp(old->name, new->name)) continue; err = snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_SET_PORT_INFO, new); if (err < 0) goto error; /* notify to system port */ snd_seq_system_client_ev_port_change(client->seq_client, i); } error: kfree(new); kfree(old); } /* update dir_bits and active flag for all groups in the client */ static void update_group_attrs(struct seq_ump_client *client) { struct snd_ump_block *fb; struct seq_ump_group *group; int i; for (i = 0; i < SNDRV_UMP_MAX_GROUPS; i++) { group = &client->groups[i]; *group->name = 0; group->dir_bits = 0; group->active = 0; group->group = i; } list_for_each_entry(fb, &client->ump->block_list, list) { if (fb->info.first_group + fb->info.num_groups > SNDRV_UMP_MAX_GROUPS) break; group = &client->groups[fb->info.first_group]; for (i = 0; i < fb->info.num_groups; i++, group++) { if (fb->info.active) group->active = 1; switch (fb->info.direction) { case SNDRV_UMP_DIR_INPUT: group->dir_bits |= (1 << STR_IN); break; case SNDRV_UMP_DIR_OUTPUT: group->dir_bits |= (1 << STR_OUT); break; case SNDRV_UMP_DIR_BIDIRECTION: group->dir_bits |= (1 << STR_OUT) | (1 << STR_IN); break; } if (!*fb->info.name) continue; if (!*group->name) { /* store the first matching name */ strscpy(group->name, fb->info.name, sizeof(group->name)); } else { /* when overlapping, concat names */ strlcat(group->name, ", ", sizeof(group->name)); strlcat(group->name, fb->info.name, sizeof(group->name)); } } } } /* create a UMP Endpoint port */ static int create_ump_endpoint_port(struct seq_ump_client *client) { struct snd_seq_port_info *port; struct snd_seq_port_callback pcallbacks; unsigned int rawmidi_info = client->ump->core.info_flags; int err; port = kzalloc(sizeof(*port), GFP_KERNEL); if (!port) return -ENOMEM; port->addr.client = client->seq_client; port->addr.port = 0; /* fixed */ port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; port->capability = SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT; if (rawmidi_info & SNDRV_RAWMIDI_INFO_INPUT) { port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; } if (rawmidi_info & SNDRV_RAWMIDI_INFO_OUTPUT) { port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; } if (rawmidi_info & SNDRV_RAWMIDI_INFO_DUPLEX) port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; port->ump_group = 0; /* no associated group, no conversion */ port->type = SNDRV_SEQ_PORT_TYPE_MIDI_UMP | SNDRV_SEQ_PORT_TYPE_HARDWARE | SNDRV_SEQ_PORT_TYPE_PORT; port->midi_channels = 16; strcpy(port->name, "MIDI 2.0"); memset(&pcallbacks, 0, sizeof(pcallbacks)); pcallbacks.owner = THIS_MODULE; pcallbacks.private_data = client; if (rawmidi_info & SNDRV_RAWMIDI_INFO_INPUT) { pcallbacks.subscribe = seq_ump_subscribe; pcallbacks.unsubscribe = seq_ump_unsubscribe; } if (rawmidi_info & SNDRV_RAWMIDI_INFO_OUTPUT) { pcallbacks.use = seq_ump_use; pcallbacks.unuse = seq_ump_unuse; pcallbacks.event_input = seq_ump_process_event; } port->kernel = &pcallbacks; err = snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port); kfree(port); return err; } /* release the client resources */ static void seq_ump_client_free(struct seq_ump_client *client) { cancel_work_sync(&client->group_notify_work); if (client->seq_client >= 0) snd_seq_delete_kernel_client(client->seq_client); client->ump->seq_ops = NULL; client->ump->seq_client = NULL; kfree(client); } /* update the MIDI version for the given client */ static void setup_client_midi_version(struct seq_ump_client *client) { struct snd_seq_client *cptr; cptr = snd_seq_kernel_client_get(client->seq_client); if (!cptr) return; if (client->ump->info.protocol & SNDRV_UMP_EP_INFO_PROTO_MIDI2) cptr->midi_version = SNDRV_SEQ_CLIENT_UMP_MIDI_2_0; else cptr->midi_version = SNDRV_SEQ_CLIENT_UMP_MIDI_1_0; snd_seq_kernel_client_put(cptr); } /* set up client's group_filter bitmap */ static void setup_client_group_filter(struct seq_ump_client *client) { struct snd_seq_client *cptr; unsigned int filter; int p; cptr = snd_seq_kernel_client_get(client->seq_client); if (!cptr) return; filter = ~(1U << 0); /* always allow groupless messages */ for (p = 0; p < SNDRV_UMP_MAX_GROUPS; p++) { if (client->groups[p].active) filter &= ~(1U << (p + 1)); } cptr->group_filter = filter; snd_seq_kernel_client_put(cptr); } /* UMP group change notification */ static void handle_group_notify(struct work_struct *work) { struct seq_ump_client *client = container_of(work, struct seq_ump_client, group_notify_work); update_group_attrs(client); update_port_infos(client); setup_client_group_filter(client); } /* UMP FB change notification */ static int seq_ump_notify_fb_change(struct snd_ump_endpoint *ump, struct snd_ump_block *fb) { struct seq_ump_client *client = ump->seq_client; if (!client) return -ENODEV; schedule_work(&client->group_notify_work); return 0; } /* UMP protocol change notification; just update the midi_version field */ static int seq_ump_switch_protocol(struct snd_ump_endpoint *ump) { if (!ump->seq_client) return -ENODEV; setup_client_midi_version(ump->seq_client); return 0; } static const struct snd_seq_ump_ops seq_ump_ops = { .input_receive = seq_ump_input_receive, .notify_fb_change = seq_ump_notify_fb_change, .switch_protocol = seq_ump_switch_protocol, }; /* create a sequencer client and ports for the given UMP endpoint */ static int snd_seq_ump_probe(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); struct snd_ump_endpoint *ump = dev->private_data; struct snd_card *card = dev->card; struct seq_ump_client *client; struct snd_ump_block *fb; struct snd_seq_client *cptr; int p, err; client = kzalloc(sizeof(*client), GFP_KERNEL); if (!client) return -ENOMEM; INIT_WORK(&client->group_notify_work, handle_group_notify); client->ump = ump; client->seq_client = snd_seq_create_kernel_client(card, ump->core.device, ump->core.name); if (client->seq_client < 0) { err = client->seq_client; goto error; } client->ump_info[0] = &ump->info; list_for_each_entry(fb, &ump->block_list, list) client->ump_info[fb->info.block_id + 1] = &fb->info; setup_client_midi_version(client); update_group_attrs(client); for (p = 0; p < SNDRV_UMP_MAX_GROUPS; p++) { err = seq_ump_group_init(client, p); if (err < 0) goto error; } setup_client_group_filter(client); err = create_ump_endpoint_port(client); if (err < 0) goto error; cptr = snd_seq_kernel_client_get(client->seq_client); if (!cptr) { err = -EINVAL; goto error; } cptr->ump_info = client->ump_info; snd_seq_kernel_client_put(cptr); ump->seq_client = client; ump->seq_ops = &seq_ump_ops; return 0; error: seq_ump_client_free(client); return err; } /* remove a sequencer client */ static int snd_seq_ump_remove(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); struct snd_ump_endpoint *ump = dev->private_data; if (ump->seq_client) seq_ump_client_free(ump->seq_client); return 0; } static struct snd_seq_driver seq_ump_driver = { .driver = { .name = KBUILD_MODNAME, .probe = snd_seq_ump_probe, .remove = snd_seq_ump_remove, }, .id = SNDRV_SEQ_DEV_ID_UMP, .argsize = 0, }; module_snd_seq_driver(seq_ump_driver); MODULE_DESCRIPTION("ALSA sequencer client for UMP rawmidi"); MODULE_LICENSE("GPL");
linux-master
sound/core/seq/seq_ump_client.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * GM/GS/XG midi module. * * Copyright (C) 1999 Steve Ratcliffe * * Based on awe_wave.c by Takashi Iwai */ /* * This module is used to keep track of the current midi state. * It can be used for drivers that are required to emulate midi when * the hardware doesn't. * * It was written for a AWE64 driver, but there should be no AWE specific * code in here. If there is it should be reported as a bug. */ #include <linux/init.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/module.h> #include <sound/core.h> #include <sound/seq_kernel.h> #include <sound/seq_midi_emul.h> #include <sound/initval.h> #include <sound/asoundef.h> MODULE_AUTHOR("Takashi Iwai / Steve Ratcliffe"); MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI emulation."); MODULE_LICENSE("GPL"); /* Prototypes for static functions */ static void note_off(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan, int note, int vel); static void do_control(const struct snd_midi_op *ops, void *private, struct snd_midi_channel_set *chset, struct snd_midi_channel *chan, int control, int value); static void rpn(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan, struct snd_midi_channel_set *chset); static void nrpn(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan, struct snd_midi_channel_set *chset); static void sysex(const struct snd_midi_op *ops, void *private, unsigned char *sysex, int len, struct snd_midi_channel_set *chset); static void all_sounds_off(const struct snd_midi_op *ops, void *private, struct snd_midi_channel *chan); static void all_notes_off(const struct snd_midi_op *ops, void *private, struct snd_midi_channel *chan); static void snd_midi_reset_controllers(struct snd_midi_channel *chan); static void reset_all_channels(struct snd_midi_channel_set *chset); /* * Process an event in a driver independent way. This means dealing * with RPN, NRPN, SysEx etc that are defined for common midi applications * such as GM, GS and XG. * There modes that this module will run in are: * Generic MIDI - no interpretation at all, it will just save current values * of controllers etc. * GM - You can use all gm_ prefixed elements of chan. Controls, RPN, NRPN, * SysEx will be interpreded as defined in General Midi. * GS - You can use all gs_ prefixed elements of chan. Codes for GS will be * interpreted. * XG - You can use all xg_ prefixed elements of chan. Codes for XG will * be interpreted. */ void snd_midi_process_event(const struct snd_midi_op *ops, struct snd_seq_event *ev, struct snd_midi_channel_set *chanset) { struct snd_midi_channel *chan; void *drv; int dest_channel = 0; if (ev == NULL || chanset == NULL) { pr_debug("ALSA: seq_midi_emul: ev or chanbase NULL (snd_midi_process_event)\n"); return; } if (chanset->channels == NULL) return; if (snd_seq_ev_is_channel_type(ev)) { dest_channel = ev->data.note.channel; if (dest_channel >= chanset->max_channels) { pr_debug("ALSA: seq_midi_emul: dest channel is %d, max is %d\n", dest_channel, chanset->max_channels); return; } } chan = chanset->channels + dest_channel; drv = chanset->private_data; /* EVENT_NOTE should be processed before queued */ if (ev->type == SNDRV_SEQ_EVENT_NOTE) return; /* Make sure that we don't have a note on that should really be * a note off */ if (ev->type == SNDRV_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0) ev->type = SNDRV_SEQ_EVENT_NOTEOFF; /* Make sure the note is within array range */ if (ev->type == SNDRV_SEQ_EVENT_NOTEON || ev->type == SNDRV_SEQ_EVENT_NOTEOFF || ev->type == SNDRV_SEQ_EVENT_KEYPRESS) { if (ev->data.note.note >= 128) return; } switch (ev->type) { case SNDRV_SEQ_EVENT_NOTEON: if (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON) { if (ops->note_off) ops->note_off(drv, ev->data.note.note, 0, chan); } chan->note[ev->data.note.note] = SNDRV_MIDI_NOTE_ON; if (ops->note_on) ops->note_on(drv, ev->data.note.note, ev->data.note.velocity, chan); break; case SNDRV_SEQ_EVENT_NOTEOFF: if (! (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON)) break; if (ops->note_off) note_off(ops, drv, chan, ev->data.note.note, ev->data.note.velocity); break; case SNDRV_SEQ_EVENT_KEYPRESS: if (ops->key_press) ops->key_press(drv, ev->data.note.note, ev->data.note.velocity, chan); break; case SNDRV_SEQ_EVENT_CONTROLLER: do_control(ops, drv, chanset, chan, ev->data.control.param, ev->data.control.value); break; case SNDRV_SEQ_EVENT_PGMCHANGE: chan->midi_program = ev->data.control.value; break; case SNDRV_SEQ_EVENT_PITCHBEND: chan->midi_pitchbend = ev->data.control.value; if (ops->control) ops->control(drv, MIDI_CTL_PITCHBEND, chan); break; case SNDRV_SEQ_EVENT_CHANPRESS: chan->midi_pressure = ev->data.control.value; if (ops->control) ops->control(drv, MIDI_CTL_CHAN_PRESSURE, chan); break; case SNDRV_SEQ_EVENT_CONTROL14: /* Best guess is that this is any of the 14 bit controller values */ if (ev->data.control.param < 32) { /* set low part first */ chan->control[ev->data.control.param + 32] = ev->data.control.value & 0x7f; do_control(ops, drv, chanset, chan, ev->data.control.param, ((ev->data.control.value>>7) & 0x7f)); } else do_control(ops, drv, chanset, chan, ev->data.control.param, ev->data.control.value); break; case SNDRV_SEQ_EVENT_NONREGPARAM: /* Break it back into its controller values */ chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED; chan->control[MIDI_CTL_MSB_DATA_ENTRY] = (ev->data.control.value >> 7) & 0x7f; chan->control[MIDI_CTL_LSB_DATA_ENTRY] = ev->data.control.value & 0x7f; chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB] = (ev->data.control.param >> 7) & 0x7f; chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB] = ev->data.control.param & 0x7f; nrpn(ops, drv, chan, chanset); break; case SNDRV_SEQ_EVENT_REGPARAM: /* Break it back into its controller values */ chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED; chan->control[MIDI_CTL_MSB_DATA_ENTRY] = (ev->data.control.value >> 7) & 0x7f; chan->control[MIDI_CTL_LSB_DATA_ENTRY] = ev->data.control.value & 0x7f; chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] = (ev->data.control.param >> 7) & 0x7f; chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB] = ev->data.control.param & 0x7f; rpn(ops, drv, chan, chanset); break; case SNDRV_SEQ_EVENT_SYSEX: if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE) { unsigned char sysexbuf[64]; int len; len = snd_seq_expand_var_event(ev, sizeof(sysexbuf), sysexbuf, 1, 0); if (len > 0) sysex(ops, drv, sysexbuf, len, chanset); } break; case SNDRV_SEQ_EVENT_SONGPOS: case SNDRV_SEQ_EVENT_SONGSEL: case SNDRV_SEQ_EVENT_CLOCK: case SNDRV_SEQ_EVENT_START: case SNDRV_SEQ_EVENT_CONTINUE: case SNDRV_SEQ_EVENT_STOP: case SNDRV_SEQ_EVENT_QFRAME: case SNDRV_SEQ_EVENT_TEMPO: case SNDRV_SEQ_EVENT_TIMESIGN: case SNDRV_SEQ_EVENT_KEYSIGN: goto not_yet; case SNDRV_SEQ_EVENT_SENSING: break; case SNDRV_SEQ_EVENT_CLIENT_START: case SNDRV_SEQ_EVENT_CLIENT_EXIT: case SNDRV_SEQ_EVENT_CLIENT_CHANGE: case SNDRV_SEQ_EVENT_PORT_START: case SNDRV_SEQ_EVENT_PORT_EXIT: case SNDRV_SEQ_EVENT_PORT_CHANGE: case SNDRV_SEQ_EVENT_ECHO: not_yet: default: /*pr_debug("ALSA: seq_midi_emul: Unimplemented event %d\n", ev->type);*/ break; } } EXPORT_SYMBOL(snd_midi_process_event); /* * release note */ static void note_off(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan, int note, int vel) { if (chan->gm_hold) { /* Hold this note until pedal is turned off */ chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED; } else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) { /* Mark this note as release; it will be turned off when sostenuto * is turned off */ chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED; } else { chan->note[note] = 0; if (ops->note_off) ops->note_off(drv, note, vel, chan); } } /* * Do all driver independent operations for this controller and pass * events that need to take place immediately to the driver. */ static void do_control(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel_set *chset, struct snd_midi_channel *chan, int control, int value) { int i; if (control >= ARRAY_SIZE(chan->control)) return; /* Switches */ if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) { /* These are all switches; either off or on so set to 0 or 127 */ value = (value >= 64)? 127: 0; } chan->control[control] = value; switch (control) { case MIDI_CTL_SUSTAIN: if (value == 0) { /* Sustain has been released, turn off held notes */ for (i = 0; i < 128; i++) { if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) { chan->note[i] = SNDRV_MIDI_NOTE_OFF; if (ops->note_off) ops->note_off(drv, i, 0, chan); } } } break; case MIDI_CTL_PORTAMENTO: break; case MIDI_CTL_SOSTENUTO: if (value) { /* Mark each note that is currently held down */ for (i = 0; i < 128; i++) { if (chan->note[i] & SNDRV_MIDI_NOTE_ON) chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO; } } else { /* release all notes that were held */ for (i = 0; i < 128; i++) { if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) { chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO; if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) { chan->note[i] = SNDRV_MIDI_NOTE_OFF; if (ops->note_off) ops->note_off(drv, i, 0, chan); } } } } break; case MIDI_CTL_MSB_DATA_ENTRY: chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0; fallthrough; case MIDI_CTL_LSB_DATA_ENTRY: if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED) rpn(ops, drv, chan, chset); else nrpn(ops, drv, chan, chset); break; case MIDI_CTL_REGIST_PARM_NUM_LSB: case MIDI_CTL_REGIST_PARM_NUM_MSB: chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED; break; case MIDI_CTL_NONREG_PARM_NUM_LSB: case MIDI_CTL_NONREG_PARM_NUM_MSB: chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED; break; case MIDI_CTL_ALL_SOUNDS_OFF: all_sounds_off(ops, drv, chan); break; case MIDI_CTL_ALL_NOTES_OFF: all_notes_off(ops, drv, chan); break; case MIDI_CTL_MSB_BANK: if (chset->midi_mode == SNDRV_MIDI_MODE_XG) { if (value == 127) chan->drum_channel = 1; else chan->drum_channel = 0; } break; case MIDI_CTL_LSB_BANK: break; case MIDI_CTL_RESET_CONTROLLERS: snd_midi_reset_controllers(chan); break; case MIDI_CTL_SOFT_PEDAL: case MIDI_CTL_LEGATO_FOOTSWITCH: case MIDI_CTL_HOLD2: case MIDI_CTL_SC1_SOUND_VARIATION: case MIDI_CTL_SC2_TIMBRE: case MIDI_CTL_SC3_RELEASE_TIME: case MIDI_CTL_SC4_ATTACK_TIME: case MIDI_CTL_SC5_BRIGHTNESS: case MIDI_CTL_E1_REVERB_DEPTH: case MIDI_CTL_E2_TREMOLO_DEPTH: case MIDI_CTL_E3_CHORUS_DEPTH: case MIDI_CTL_E4_DETUNE_DEPTH: case MIDI_CTL_E5_PHASER_DEPTH: goto notyet; notyet: default: if (ops->control) ops->control(drv, control, chan); break; } } /* * initialize the MIDI status */ void snd_midi_channel_set_clear(struct snd_midi_channel_set *chset) { int i; chset->midi_mode = SNDRV_MIDI_MODE_GM; chset->gs_master_volume = 127; for (i = 0; i < chset->max_channels; i++) { struct snd_midi_channel *chan = chset->channels + i; memset(chan->note, 0, sizeof(chan->note)); chan->midi_aftertouch = 0; chan->midi_pressure = 0; chan->midi_program = 0; chan->midi_pitchbend = 0; snd_midi_reset_controllers(chan); chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */ chan->gm_rpn_fine_tuning = 0; chan->gm_rpn_coarse_tuning = 0; if (i == 9) chan->drum_channel = 1; else chan->drum_channel = 0; } } EXPORT_SYMBOL(snd_midi_channel_set_clear); /* * Process a rpn message. */ static void rpn(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan, struct snd_midi_channel_set *chset) { int type; int val; if (chset->midi_mode != SNDRV_MIDI_MODE_NONE) { type = (chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] << 8) | chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB]; val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) | chan->control[MIDI_CTL_LSB_DATA_ENTRY]; switch (type) { case 0x0000: /* Pitch bend sensitivity */ /* MSB only / 1 semitone per 128 */ chan->gm_rpn_pitch_bend_range = val; break; case 0x0001: /* fine tuning: */ /* MSB/LSB, 8192=center, 100/8192 cent step */ chan->gm_rpn_fine_tuning = val - 8192; break; case 0x0002: /* coarse tuning */ /* MSB only / 8192=center, 1 semitone per 128 */ chan->gm_rpn_coarse_tuning = val - 8192; break; case 0x7F7F: /* "lock-in" RPN */ /* ignored */ break; } } /* should call nrpn or rpn callback here.. */ } /* * Process an nrpn message. */ static void nrpn(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan, struct snd_midi_channel_set *chset) { /* parse XG NRPNs here if possible */ if (ops->nrpn) ops->nrpn(drv, chan, chset); } /* * convert channel parameter in GS sysex */ static int get_channel(unsigned char cmd) { int p = cmd & 0x0f; if (p == 0) p = 9; else if (p < 10) p--; return p; } /* * Process a sysex message. */ static void sysex(const struct snd_midi_op *ops, void *private, unsigned char *buf, int len, struct snd_midi_channel_set *chset) { /* GM on */ static const unsigned char gm_on_macro[] = { 0x7e,0x7f,0x09,0x01, }; /* XG on */ static const unsigned char xg_on_macro[] = { 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00, }; /* GS prefix * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off * reverb mode: XX=0x01, YY=0x30, ZZ=0-7 * chorus mode: XX=0x01, YY=0x38, ZZ=0-7 * master vol: XX=0x00, YY=0x04, ZZ=0-127 */ static const unsigned char gs_pfx_macro[] = { 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/ }; int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED; if (len <= 0 || buf[0] != 0xf0) return; /* skip first byte */ buf++; len--; /* GM on */ if (len >= (int)sizeof(gm_on_macro) && memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) { if (chset->midi_mode != SNDRV_MIDI_MODE_GS && chset->midi_mode != SNDRV_MIDI_MODE_XG) { chset->midi_mode = SNDRV_MIDI_MODE_GM; reset_all_channels(chset); parsed = SNDRV_MIDI_SYSEX_GM_ON; } } /* GS macros */ else if (len >= 8 && memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) { if (chset->midi_mode != SNDRV_MIDI_MODE_GS && chset->midi_mode != SNDRV_MIDI_MODE_XG) chset->midi_mode = SNDRV_MIDI_MODE_GS; if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) { /* GS reset */ parsed = SNDRV_MIDI_SYSEX_GS_RESET; reset_all_channels(chset); } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) { /* drum pattern */ int p = get_channel(buf[5]); if (p < chset->max_channels) { parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL; if (buf[7]) chset->channels[p].drum_channel = 1; else chset->channels[p].drum_channel = 0; } } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) { /* program */ int p = get_channel(buf[5]); if (p < chset->max_channels && ! chset->channels[p].drum_channel) { parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL; chset->channels[p].midi_program = buf[7]; } } else if (buf[5] == 0x01 && buf[6] == 0x30) { /* reverb mode */ parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE; chset->gs_reverb_mode = buf[7]; } else if (buf[5] == 0x01 && buf[6] == 0x38) { /* chorus mode */ parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE; chset->gs_chorus_mode = buf[7]; } else if (buf[5] == 0x00 && buf[6] == 0x04) { /* master volume */ parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME; chset->gs_master_volume = buf[7]; } } /* XG on */ else if (len >= (int)sizeof(xg_on_macro) && memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) { int i; chset->midi_mode = SNDRV_MIDI_MODE_XG; parsed = SNDRV_MIDI_SYSEX_XG_ON; /* reset CC#0 for drums */ for (i = 0; i < chset->max_channels; i++) { if (chset->channels[i].drum_channel) chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127; else chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0; } } if (ops->sysex) ops->sysex(private, buf - 1, len + 1, parsed, chset); } /* * all sound off */ static void all_sounds_off(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan) { int n; if (! ops->note_terminate) return; for (n = 0; n < 128; n++) { if (chan->note[n]) { ops->note_terminate(drv, n, chan); chan->note[n] = 0; } } } /* * all notes off */ static void all_notes_off(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan) { int n; if (! ops->note_off) return; for (n = 0; n < 128; n++) { if (chan->note[n] == SNDRV_MIDI_NOTE_ON) note_off(ops, drv, chan, n, 0); } } /* * Initialise a single midi channel control block. */ static void snd_midi_channel_init(struct snd_midi_channel *p, int n) { if (p == NULL) return; memset(p, 0, sizeof(struct snd_midi_channel)); p->private = NULL; p->number = n; snd_midi_reset_controllers(p); p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */ p->gm_rpn_fine_tuning = 0; p->gm_rpn_coarse_tuning = 0; if (n == 9) p->drum_channel = 1; /* Default ch 10 as drums */ } /* * Allocate and initialise a set of midi channel control blocks. */ static struct snd_midi_channel *snd_midi_channel_init_set(int n) { struct snd_midi_channel *chan; int i; chan = kmalloc_array(n, sizeof(struct snd_midi_channel), GFP_KERNEL); if (chan) { for (i = 0; i < n; i++) snd_midi_channel_init(chan+i, i); } return chan; } /* * reset all midi channels */ static void reset_all_channels(struct snd_midi_channel_set *chset) { int ch; for (ch = 0; ch < chset->max_channels; ch++) { struct snd_midi_channel *chan = chset->channels + ch; snd_midi_reset_controllers(chan); chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */ chan->gm_rpn_fine_tuning = 0; chan->gm_rpn_coarse_tuning = 0; if (ch == 9) chan->drum_channel = 1; else chan->drum_channel = 0; } } /* * Allocate and initialise a midi channel set. */ struct snd_midi_channel_set *snd_midi_channel_alloc_set(int n) { struct snd_midi_channel_set *chset; chset = kmalloc(sizeof(*chset), GFP_KERNEL); if (chset) { chset->channels = snd_midi_channel_init_set(n); chset->private_data = NULL; chset->max_channels = n; } return chset; } EXPORT_SYMBOL(snd_midi_channel_alloc_set); /* * Reset the midi controllers on a particular channel to default values. */ static void snd_midi_reset_controllers(struct snd_midi_channel *chan) { memset(chan->control, 0, sizeof(chan->control)); chan->gm_volume = 127; chan->gm_expression = 127; chan->gm_pan = 64; } /* * Free a midi channel set. */ void snd_midi_channel_free_set(struct snd_midi_channel_set *chset) { if (chset == NULL) return; kfree(chset->channels); kfree(chset); } EXPORT_SYMBOL(snd_midi_channel_free_set);
linux-master
sound/core/seq/seq_midi_emul.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer Ports * Copyright (c) 1998 by Frank van de Pol <[email protected]> * Jaroslav Kysela <[email protected]> */ #include <sound/core.h> #include <linux/slab.h> #include <linux/module.h> #include "seq_system.h" #include "seq_ports.h" #include "seq_clientmgr.h" /* registration of client ports */ /* NOTE: the current implementation of the port structure as a linked list is not optimal for clients that have many ports. For sending messages to all subscribers of a port we first need to find the address of the port structure, which means we have to traverse the list. A direct access table (array) would be better, but big preallocated arrays waste memory. Possible actions: 1) leave it this way, a client does normaly does not have more than a few ports 2) replace the linked list of ports by a array of pointers which is dynamicly kmalloced. When a port is added or deleted we can simply allocate a new array, copy the corresponding pointers, and delete the old one. We then only need a pointer to this array, and an integer that tells us how much elements are in array. */ /* return pointer to port structure - port is locked if found */ struct snd_seq_client_port *snd_seq_port_use_ptr(struct snd_seq_client *client, int num) { struct snd_seq_client_port *port; if (client == NULL) return NULL; read_lock(&client->ports_lock); list_for_each_entry(port, &client->ports_list_head, list) { if (port->addr.port == num) { if (port->closing) break; /* deleting now */ snd_use_lock_use(&port->use_lock); read_unlock(&client->ports_lock); return port; } } read_unlock(&client->ports_lock); return NULL; /* not found */ } /* search for the next port - port is locked if found */ struct snd_seq_client_port *snd_seq_port_query_nearest(struct snd_seq_client *client, struct snd_seq_port_info *pinfo) { int num; struct snd_seq_client_port *port, *found; bool check_inactive = (pinfo->capability & SNDRV_SEQ_PORT_CAP_INACTIVE); num = pinfo->addr.port; found = NULL; read_lock(&client->ports_lock); list_for_each_entry(port, &client->ports_list_head, list) { if ((port->capability & SNDRV_SEQ_PORT_CAP_INACTIVE) && !check_inactive) continue; /* skip inactive ports */ if (port->addr.port < num) continue; if (port->addr.port == num) { found = port; break; } if (found == NULL || port->addr.port < found->addr.port) found = port; } if (found) { if (found->closing) found = NULL; else snd_use_lock_use(&found->use_lock); } read_unlock(&client->ports_lock); return found; } /* initialize snd_seq_port_subs_info */ static void port_subs_info_init(struct snd_seq_port_subs_info *grp) { INIT_LIST_HEAD(&grp->list_head); grp->count = 0; grp->exclusive = 0; rwlock_init(&grp->list_lock); init_rwsem(&grp->list_mutex); grp->open = NULL; grp->close = NULL; } /* create a port, port number or a negative error code is returned * the caller needs to unref the port via snd_seq_port_unlock() appropriately */ int snd_seq_create_port(struct snd_seq_client *client, int port, struct snd_seq_client_port **port_ret) { struct snd_seq_client_port *new_port, *p; int num; *port_ret = NULL; /* sanity check */ if (snd_BUG_ON(!client)) return -EINVAL; if (client->num_ports >= SNDRV_SEQ_MAX_PORTS) { pr_warn("ALSA: seq: too many ports for client %d\n", client->number); return -EINVAL; } /* create a new port */ new_port = kzalloc(sizeof(*new_port), GFP_KERNEL); if (!new_port) return -ENOMEM; /* failure, out of memory */ /* init port data */ new_port->addr.client = client->number; new_port->addr.port = -1; new_port->owner = THIS_MODULE; snd_use_lock_init(&new_port->use_lock); port_subs_info_init(&new_port->c_src); port_subs_info_init(&new_port->c_dest); snd_use_lock_use(&new_port->use_lock); num = max(port, 0); mutex_lock(&client->ports_mutex); write_lock_irq(&client->ports_lock); list_for_each_entry(p, &client->ports_list_head, list) { if (p->addr.port == port) { kfree(new_port); num = -EBUSY; goto unlock; } if (p->addr.port > num) break; if (port < 0) /* auto-probe mode */ num = p->addr.port + 1; } /* insert the new port */ list_add_tail(&new_port->list, &p->list); client->num_ports++; new_port->addr.port = num; /* store the port number in the port */ sprintf(new_port->name, "port-%d", num); *port_ret = new_port; unlock: write_unlock_irq(&client->ports_lock); mutex_unlock(&client->ports_mutex); return num; } /* */ static int subscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_port_subs_info *grp, struct snd_seq_port_subscribe *info, int send_ack); static int unsubscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_port_subs_info *grp, struct snd_seq_port_subscribe *info, int send_ack); static struct snd_seq_client_port *get_client_port(struct snd_seq_addr *addr, struct snd_seq_client **cp) { struct snd_seq_client_port *p; *cp = snd_seq_client_use_ptr(addr->client); if (*cp) { p = snd_seq_port_use_ptr(*cp, addr->port); if (! p) { snd_seq_client_unlock(*cp); *cp = NULL; } return p; } return NULL; } static void delete_and_unsubscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_subscribers *subs, bool is_src, bool ack); static inline struct snd_seq_subscribers * get_subscriber(struct list_head *p, bool is_src) { if (is_src) return list_entry(p, struct snd_seq_subscribers, src_list); else return list_entry(p, struct snd_seq_subscribers, dest_list); } /* * remove all subscribers on the list * this is called from port_delete, for each src and dest list. */ static void clear_subscriber_list(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_port_subs_info *grp, int is_src) { struct list_head *p, *n; list_for_each_safe(p, n, &grp->list_head) { struct snd_seq_subscribers *subs; struct snd_seq_client *c; struct snd_seq_client_port *aport; subs = get_subscriber(p, is_src); if (is_src) aport = get_client_port(&subs->info.dest, &c); else aport = get_client_port(&subs->info.sender, &c); delete_and_unsubscribe_port(client, port, subs, is_src, false); if (!aport) { /* looks like the connected port is being deleted. * we decrease the counter, and when both ports are deleted * remove the subscriber info */ if (atomic_dec_and_test(&subs->ref_count)) kfree(subs); continue; } /* ok we got the connected port */ delete_and_unsubscribe_port(c, aport, subs, !is_src, true); kfree(subs); snd_seq_port_unlock(aport); snd_seq_client_unlock(c); } } /* delete port data */ static int port_delete(struct snd_seq_client *client, struct snd_seq_client_port *port) { /* set closing flag and wait for all port access are gone */ port->closing = 1; snd_use_lock_sync(&port->use_lock); /* clear subscribers info */ clear_subscriber_list(client, port, &port->c_src, true); clear_subscriber_list(client, port, &port->c_dest, false); if (port->private_free) port->private_free(port->private_data); snd_BUG_ON(port->c_src.count != 0); snd_BUG_ON(port->c_dest.count != 0); kfree(port); return 0; } /* delete a port with the given port id */ int snd_seq_delete_port(struct snd_seq_client *client, int port) { struct snd_seq_client_port *found = NULL, *p; mutex_lock(&client->ports_mutex); write_lock_irq(&client->ports_lock); list_for_each_entry(p, &client->ports_list_head, list) { if (p->addr.port == port) { /* ok found. delete from the list at first */ list_del(&p->list); client->num_ports--; found = p; break; } } write_unlock_irq(&client->ports_lock); mutex_unlock(&client->ports_mutex); if (found) return port_delete(client, found); else return -ENOENT; } /* delete the all ports belonging to the given client */ int snd_seq_delete_all_ports(struct snd_seq_client *client) { struct list_head deleted_list; struct snd_seq_client_port *port, *tmp; /* move the port list to deleted_list, and * clear the port list in the client data. */ mutex_lock(&client->ports_mutex); write_lock_irq(&client->ports_lock); if (! list_empty(&client->ports_list_head)) { list_add(&deleted_list, &client->ports_list_head); list_del_init(&client->ports_list_head); } else { INIT_LIST_HEAD(&deleted_list); } client->num_ports = 0; write_unlock_irq(&client->ports_lock); /* remove each port in deleted_list */ list_for_each_entry_safe(port, tmp, &deleted_list, list) { list_del(&port->list); snd_seq_system_client_ev_port_exit(port->addr.client, port->addr.port); port_delete(client, port); } mutex_unlock(&client->ports_mutex); return 0; } /* set port info fields */ int snd_seq_set_port_info(struct snd_seq_client_port * port, struct snd_seq_port_info * info) { if (snd_BUG_ON(!port || !info)) return -EINVAL; /* set port name */ if (info->name[0]) strscpy(port->name, info->name, sizeof(port->name)); /* set capabilities */ port->capability = info->capability; /* get port type */ port->type = info->type; /* information about supported channels/voices */ port->midi_channels = info->midi_channels; port->midi_voices = info->midi_voices; port->synth_voices = info->synth_voices; /* timestamping */ port->timestamping = (info->flags & SNDRV_SEQ_PORT_FLG_TIMESTAMP) ? 1 : 0; port->time_real = (info->flags & SNDRV_SEQ_PORT_FLG_TIME_REAL) ? 1 : 0; port->time_queue = info->time_queue; /* UMP direction and group */ port->direction = info->direction; port->ump_group = info->ump_group; if (port->ump_group > SNDRV_UMP_MAX_GROUPS) port->ump_group = 0; /* fill default port direction */ if (!port->direction) { if (info->capability & SNDRV_SEQ_PORT_CAP_READ) port->direction |= SNDRV_SEQ_PORT_DIR_INPUT; if (info->capability & SNDRV_SEQ_PORT_CAP_WRITE) port->direction |= SNDRV_SEQ_PORT_DIR_OUTPUT; } return 0; } /* get port info fields */ int snd_seq_get_port_info(struct snd_seq_client_port * port, struct snd_seq_port_info * info) { if (snd_BUG_ON(!port || !info)) return -EINVAL; /* get port name */ strscpy(info->name, port->name, sizeof(info->name)); /* get capabilities */ info->capability = port->capability; /* get port type */ info->type = port->type; /* information about supported channels/voices */ info->midi_channels = port->midi_channels; info->midi_voices = port->midi_voices; info->synth_voices = port->synth_voices; /* get subscriber counts */ info->read_use = port->c_src.count; info->write_use = port->c_dest.count; /* timestamping */ info->flags = 0; if (port->timestamping) { info->flags |= SNDRV_SEQ_PORT_FLG_TIMESTAMP; if (port->time_real) info->flags |= SNDRV_SEQ_PORT_FLG_TIME_REAL; info->time_queue = port->time_queue; } /* UMP direction and group */ info->direction = port->direction; info->ump_group = port->ump_group; return 0; } /* * call callback functions (if any): * the callbacks are invoked only when the first (for connection) or * the last subscription (for disconnection) is done. Second or later * subscription results in increment of counter, but no callback is * invoked. * This feature is useful if these callbacks are associated with * initialization or termination of devices (see seq_midi.c). */ static int subscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_port_subs_info *grp, struct snd_seq_port_subscribe *info, int send_ack) { int err = 0; if (!try_module_get(port->owner)) return -EFAULT; grp->count++; if (grp->open && grp->count == 1) { err = grp->open(port->private_data, info); if (err < 0) { module_put(port->owner); grp->count--; } } if (err >= 0 && send_ack && client->type == USER_CLIENT) snd_seq_client_notify_subscription(port->addr.client, port->addr.port, info, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED); return err; } static int unsubscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_port_subs_info *grp, struct snd_seq_port_subscribe *info, int send_ack) { int err = 0; if (! grp->count) return -EINVAL; grp->count--; if (grp->close && grp->count == 0) err = grp->close(port->private_data, info); if (send_ack && client->type == USER_CLIENT) snd_seq_client_notify_subscription(port->addr.client, port->addr.port, info, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED); module_put(port->owner); return err; } /* check if both addresses are identical */ static inline int addr_match(struct snd_seq_addr *r, struct snd_seq_addr *s) { return (r->client == s->client) && (r->port == s->port); } /* check the two subscribe info match */ /* if flags is zero, checks only sender and destination addresses */ static int match_subs_info(struct snd_seq_port_subscribe *r, struct snd_seq_port_subscribe *s) { if (addr_match(&r->sender, &s->sender) && addr_match(&r->dest, &s->dest)) { if (r->flags && r->flags == s->flags) return r->queue == s->queue; else if (! r->flags) return 1; } return 0; } static int check_and_subscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_subscribers *subs, bool is_src, bool exclusive, bool ack) { struct snd_seq_port_subs_info *grp; struct list_head *p; struct snd_seq_subscribers *s; int err; grp = is_src ? &port->c_src : &port->c_dest; err = -EBUSY; down_write(&grp->list_mutex); if (exclusive) { if (!list_empty(&grp->list_head)) goto __error; } else { if (grp->exclusive) goto __error; /* check whether already exists */ list_for_each(p, &grp->list_head) { s = get_subscriber(p, is_src); if (match_subs_info(&subs->info, &s->info)) goto __error; } } err = subscribe_port(client, port, grp, &subs->info, ack); if (err < 0) { grp->exclusive = 0; goto __error; } /* add to list */ write_lock_irq(&grp->list_lock); if (is_src) list_add_tail(&subs->src_list, &grp->list_head); else list_add_tail(&subs->dest_list, &grp->list_head); grp->exclusive = exclusive; atomic_inc(&subs->ref_count); write_unlock_irq(&grp->list_lock); err = 0; __error: up_write(&grp->list_mutex); return err; } /* called with grp->list_mutex held */ static void __delete_and_unsubscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_subscribers *subs, bool is_src, bool ack) { struct snd_seq_port_subs_info *grp; struct list_head *list; bool empty; grp = is_src ? &port->c_src : &port->c_dest; list = is_src ? &subs->src_list : &subs->dest_list; write_lock_irq(&grp->list_lock); empty = list_empty(list); if (!empty) list_del_init(list); grp->exclusive = 0; write_unlock_irq(&grp->list_lock); if (!empty) unsubscribe_port(client, port, grp, &subs->info, ack); } static void delete_and_unsubscribe_port(struct snd_seq_client *client, struct snd_seq_client_port *port, struct snd_seq_subscribers *subs, bool is_src, bool ack) { struct snd_seq_port_subs_info *grp; grp = is_src ? &port->c_src : &port->c_dest; down_write(&grp->list_mutex); __delete_and_unsubscribe_port(client, port, subs, is_src, ack); up_write(&grp->list_mutex); } /* connect two ports */ int snd_seq_port_connect(struct snd_seq_client *connector, struct snd_seq_client *src_client, struct snd_seq_client_port *src_port, struct snd_seq_client *dest_client, struct snd_seq_client_port *dest_port, struct snd_seq_port_subscribe *info) { struct snd_seq_subscribers *subs; bool exclusive; int err; subs = kzalloc(sizeof(*subs), GFP_KERNEL); if (!subs) return -ENOMEM; subs->info = *info; atomic_set(&subs->ref_count, 0); INIT_LIST_HEAD(&subs->src_list); INIT_LIST_HEAD(&subs->dest_list); exclusive = !!(info->flags & SNDRV_SEQ_PORT_SUBS_EXCLUSIVE); err = check_and_subscribe_port(src_client, src_port, subs, true, exclusive, connector->number != src_client->number); if (err < 0) goto error; err = check_and_subscribe_port(dest_client, dest_port, subs, false, exclusive, connector->number != dest_client->number); if (err < 0) goto error_dest; return 0; error_dest: delete_and_unsubscribe_port(src_client, src_port, subs, true, connector->number != src_client->number); error: kfree(subs); return err; } /* remove the connection */ int snd_seq_port_disconnect(struct snd_seq_client *connector, struct snd_seq_client *src_client, struct snd_seq_client_port *src_port, struct snd_seq_client *dest_client, struct snd_seq_client_port *dest_port, struct snd_seq_port_subscribe *info) { struct snd_seq_port_subs_info *dest = &dest_port->c_dest; struct snd_seq_subscribers *subs; int err = -ENOENT; /* always start from deleting the dest port for avoiding concurrent * deletions */ down_write(&dest->list_mutex); /* look for the connection */ list_for_each_entry(subs, &dest->list_head, dest_list) { if (match_subs_info(info, &subs->info)) { __delete_and_unsubscribe_port(dest_client, dest_port, subs, false, connector->number != dest_client->number); err = 0; break; } } up_write(&dest->list_mutex); if (err < 0) return err; delete_and_unsubscribe_port(src_client, src_port, subs, true, connector->number != src_client->number); kfree(subs); return 0; } /* get matched subscriber */ int snd_seq_port_get_subscription(struct snd_seq_port_subs_info *src_grp, struct snd_seq_addr *dest_addr, struct snd_seq_port_subscribe *subs) { struct snd_seq_subscribers *s; int err = -ENOENT; down_read(&src_grp->list_mutex); list_for_each_entry(s, &src_grp->list_head, src_list) { if (addr_match(dest_addr, &s->info.dest)) { *subs = s->info; err = 0; break; } } up_read(&src_grp->list_mutex); return err; } /* * Attach a device driver that wants to receive events from the * sequencer. Returns the new port number on success. * A driver that wants to receive the events converted to midi, will * use snd_seq_midisynth_register_port(). */ /* exported */ int snd_seq_event_port_attach(int client, struct snd_seq_port_callback *pcbp, int cap, int type, int midi_channels, int midi_voices, char *portname) { struct snd_seq_port_info portinfo; int ret; /* Set up the port */ memset(&portinfo, 0, sizeof(portinfo)); portinfo.addr.client = client; strscpy(portinfo.name, portname ? portname : "Unnamed port", sizeof(portinfo.name)); portinfo.capability = cap; portinfo.type = type; portinfo.kernel = pcbp; portinfo.midi_channels = midi_channels; portinfo.midi_voices = midi_voices; /* Create it */ ret = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, &portinfo); if (ret >= 0) ret = portinfo.addr.port; return ret; } EXPORT_SYMBOL(snd_seq_event_port_attach); /* * Detach the driver from a port. */ /* exported */ int snd_seq_event_port_detach(int client, int port) { struct snd_seq_port_info portinfo; int err; memset(&portinfo, 0, sizeof(portinfo)); portinfo.addr.client = client; portinfo.addr.port = port; err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_DELETE_PORT, &portinfo); return err; } EXPORT_SYMBOL(snd_seq_event_port_detach);
linux-master
sound/core/seq/seq_ports.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA sequencer Priority Queue * Copyright (c) 1998-1999 by Frank van de Pol <[email protected]> */ #include <linux/time.h> #include <linux/slab.h> #include <sound/core.h> #include "seq_timer.h" #include "seq_prioq.h" /* Implementation is a simple linked list for now... This priority queue orders the events on timestamp. For events with an equeal timestamp the queue behaves as a FIFO. * * +-------+ * Head --> | first | * +-------+ * |next * +-----v-+ * | | * +-------+ * | * +-----v-+ * | | * +-------+ * | * +-----v-+ * Tail --> | last | * +-------+ * */ /* create new prioq (constructor) */ struct snd_seq_prioq *snd_seq_prioq_new(void) { struct snd_seq_prioq *f; f = kzalloc(sizeof(*f), GFP_KERNEL); if (!f) return NULL; spin_lock_init(&f->lock); f->head = NULL; f->tail = NULL; f->cells = 0; return f; } /* delete prioq (destructor) */ void snd_seq_prioq_delete(struct snd_seq_prioq **fifo) { struct snd_seq_prioq *f = *fifo; *fifo = NULL; if (f == NULL) { pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n"); return; } /* release resources...*/ /*....................*/ if (f->cells > 0) { /* drain prioQ */ while (f->cells > 0) snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL)); } kfree(f); } /* compare timestamp between events */ /* return 1 if a >= b; 0 */ static inline int compare_timestamp(struct snd_seq_event *a, struct snd_seq_event *b) { if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) { /* compare ticks */ return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick)); } else { /* compare real time */ return (snd_seq_compare_real_time(&a->time.time, &b->time.time)); } } /* compare timestamp between events */ /* return negative if a < b; * zero if a = b; * positive if a > b; */ static inline int compare_timestamp_rel(struct snd_seq_event *a, struct snd_seq_event *b) { if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) { /* compare ticks */ if (a->time.tick > b->time.tick) return 1; else if (a->time.tick == b->time.tick) return 0; else return -1; } else { /* compare real time */ if (a->time.time.tv_sec > b->time.time.tv_sec) return 1; else if (a->time.time.tv_sec == b->time.time.tv_sec) { if (a->time.time.tv_nsec > b->time.time.tv_nsec) return 1; else if (a->time.time.tv_nsec == b->time.time.tv_nsec) return 0; else return -1; } else return -1; } } /* enqueue cell to prioq */ int snd_seq_prioq_cell_in(struct snd_seq_prioq * f, struct snd_seq_event_cell * cell) { struct snd_seq_event_cell *cur, *prev; unsigned long flags; int count; int prior; if (snd_BUG_ON(!f || !cell)) return -EINVAL; /* check flags */ prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK); spin_lock_irqsave(&f->lock, flags); /* check if this element needs to inserted at the end (ie. ordered data is inserted) This will be very likeley if a sequencer application or midi file player is feeding us (sequential) data */ if (f->tail && !prior) { if (compare_timestamp(&cell->event, &f->tail->event)) { /* add new cell to tail of the fifo */ f->tail->next = cell; f->tail = cell; cell->next = NULL; f->cells++; spin_unlock_irqrestore(&f->lock, flags); return 0; } } /* traverse list of elements to find the place where the new cell is to be inserted... Note that this is a order n process ! */ prev = NULL; /* previous cell */ cur = f->head; /* cursor */ count = 10000; /* FIXME: enough big, isn't it? */ while (cur != NULL) { /* compare timestamps */ int rel = compare_timestamp_rel(&cell->event, &cur->event); if (rel < 0) /* new cell has earlier schedule time, */ break; else if (rel == 0 && prior) /* equal schedule time and prior to others */ break; /* new cell has equal or larger schedule time, */ /* move cursor to next cell */ prev = cur; cur = cur->next; if (! --count) { spin_unlock_irqrestore(&f->lock, flags); pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n"); return -EINVAL; } } /* insert it before cursor */ if (prev != NULL) prev->next = cell; cell->next = cur; if (f->head == cur) /* this is the first cell, set head to it */ f->head = cell; if (cur == NULL) /* reached end of the list */ f->tail = cell; f->cells++; spin_unlock_irqrestore(&f->lock, flags); return 0; } /* return 1 if the current time >= event timestamp */ static int event_is_ready(struct snd_seq_event *ev, void *current_time) { if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) return snd_seq_compare_tick_time(current_time, &ev->time.tick); else return snd_seq_compare_real_time(current_time, &ev->time.time); } /* dequeue cell from prioq */ struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f, void *current_time) { struct snd_seq_event_cell *cell; unsigned long flags; if (f == NULL) { pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n"); return NULL; } spin_lock_irqsave(&f->lock, flags); cell = f->head; if (cell && current_time && !event_is_ready(&cell->event, current_time)) cell = NULL; if (cell) { f->head = cell->next; /* reset tail if this was the last element */ if (f->tail == cell) f->tail = NULL; cell->next = NULL; f->cells--; } spin_unlock_irqrestore(&f->lock, flags); return cell; } /* return number of events available in prioq */ int snd_seq_prioq_avail(struct snd_seq_prioq * f) { if (f == NULL) { pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n"); return 0; } return f->cells; } static inline int prioq_match(struct snd_seq_event_cell *cell, int client, int timestamp) { if (cell->event.source.client == client || cell->event.dest.client == client) return 1; if (!timestamp) return 0; switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) { case SNDRV_SEQ_TIME_STAMP_TICK: if (cell->event.time.tick) return 1; break; case SNDRV_SEQ_TIME_STAMP_REAL: if (cell->event.time.time.tv_sec || cell->event.time.time.tv_nsec) return 1; break; } return 0; } /* remove cells for left client */ void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp) { register struct snd_seq_event_cell *cell, *next; unsigned long flags; struct snd_seq_event_cell *prev = NULL; struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext; /* collect all removed cells */ spin_lock_irqsave(&f->lock, flags); cell = f->head; while (cell) { next = cell->next; if (prioq_match(cell, client, timestamp)) { /* remove cell from prioq */ if (cell == f->head) { f->head = cell->next; } else { prev->next = cell->next; } if (cell == f->tail) f->tail = cell->next; f->cells--; /* add cell to free list */ cell->next = NULL; if (freefirst == NULL) { freefirst = cell; } else { freeprev->next = cell; } freeprev = cell; } else { #if 0 pr_debug("ALSA: seq: type = %i, source = %i, dest = %i, " "client = %i\n", cell->event.type, cell->event.source.client, cell->event.dest.client, client); #endif prev = cell; } cell = next; } spin_unlock_irqrestore(&f->lock, flags); /* remove selected cells */ while (freefirst) { freenext = freefirst->next; snd_seq_cell_free(freefirst); freefirst = freenext; } } static int prioq_remove_match(struct snd_seq_remove_events *info, struct snd_seq_event *ev) { int res; if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) { if (ev->dest.client != info->dest.client || ev->dest.port != info->dest.port) return 0; } if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) { if (! snd_seq_ev_is_channel_type(ev)) return 0; /* data.note.channel and data.control.channel are identical */ if (ev->data.note.channel != info->channel) return 0; } if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) { if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK) res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick); else res = snd_seq_compare_real_time(&ev->time.time, &info->time.time); if (!res) return 0; } if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) { if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK) res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick); else res = snd_seq_compare_real_time(&ev->time.time, &info->time.time); if (res) return 0; } if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) { if (ev->type != info->type) return 0; } if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) { /* Do not remove off events */ switch (ev->type) { case SNDRV_SEQ_EVENT_NOTEOFF: /* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */ return 0; default: break; } } if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) { if (info->tag != ev->tag) return 0; } return 1; } /* remove cells matching remove criteria */ void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client, struct snd_seq_remove_events *info) { struct snd_seq_event_cell *cell, *next; unsigned long flags; struct snd_seq_event_cell *prev = NULL; struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext; /* collect all removed cells */ spin_lock_irqsave(&f->lock, flags); cell = f->head; while (cell) { next = cell->next; if (cell->event.source.client == client && prioq_remove_match(info, &cell->event)) { /* remove cell from prioq */ if (cell == f->head) { f->head = cell->next; } else { prev->next = cell->next; } if (cell == f->tail) f->tail = cell->next; f->cells--; /* add cell to free list */ cell->next = NULL; if (freefirst == NULL) { freefirst = cell; } else { freeprev->next = cell; } freeprev = cell; } else { prev = cell; } cell = next; } spin_unlock_irqrestore(&f->lock, flags); /* remove selected cells */ while (freefirst) { freenext = freefirst->next; snd_seq_cell_free(freefirst); freefirst = freenext; } }
linux-master
sound/core/seq/seq_prioq.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * MIDI byte <-> sequencer event coder * * Copyright (C) 1998,99 Takashi Iwai <[email protected]>, * Jaroslav Kysela <[email protected]> */ #include <linux/slab.h> #include <linux/errno.h> #include <linux/string.h> #include <linux/module.h> #include <sound/core.h> #include <sound/seq_kernel.h> #include <sound/seq_midi_event.h> #include <sound/asoundef.h> MODULE_AUTHOR("Takashi Iwai <[email protected]>, Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("MIDI byte <-> sequencer event coder"); MODULE_LICENSE("GPL"); /* event type, index into status_event[] */ /* from 0 to 6 are normal commands (note off, on, etc.) for 0x9?-0xe? */ #define ST_INVALID 7 #define ST_SPECIAL 8 #define ST_SYSEX ST_SPECIAL /* from 8 to 15 are events for 0xf0-0xf7 */ /* * prototypes */ static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev); static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev); static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev); static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev); static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev); static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev); static void note_decode(struct snd_seq_event *ev, unsigned char *buf); static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf); static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf); static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf); static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf); /* * event list */ static struct status_event_list { int event; int qlen; void (*encode)(struct snd_midi_event *dev, struct snd_seq_event *ev); void (*decode)(struct snd_seq_event *ev, unsigned char *buf); } status_event[] = { /* 0x80 - 0xef */ {SNDRV_SEQ_EVENT_NOTEOFF, 2, note_event, note_decode}, {SNDRV_SEQ_EVENT_NOTEON, 2, note_event, note_decode}, {SNDRV_SEQ_EVENT_KEYPRESS, 2, note_event, note_decode}, {SNDRV_SEQ_EVENT_CONTROLLER, 2, two_param_ctrl_event, two_param_decode}, {SNDRV_SEQ_EVENT_PGMCHANGE, 1, one_param_ctrl_event, one_param_decode}, {SNDRV_SEQ_EVENT_CHANPRESS, 1, one_param_ctrl_event, one_param_decode}, {SNDRV_SEQ_EVENT_PITCHBEND, 2, pitchbend_ctrl_event, pitchbend_decode}, /* invalid */ {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf0 - 0xff */ {SNDRV_SEQ_EVENT_SYSEX, 1, NULL, NULL}, /* sysex: 0xf0 */ {SNDRV_SEQ_EVENT_QFRAME, 1, one_param_event, one_param_decode}, /* 0xf1 */ {SNDRV_SEQ_EVENT_SONGPOS, 2, songpos_event, songpos_decode}, /* 0xf2 */ {SNDRV_SEQ_EVENT_SONGSEL, 1, one_param_event, one_param_decode}, /* 0xf3 */ {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf4 */ {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf5 */ {SNDRV_SEQ_EVENT_TUNE_REQUEST, 0, NULL, NULL}, /* 0xf6 */ {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf7 */ {SNDRV_SEQ_EVENT_CLOCK, 0, NULL, NULL}, /* 0xf8 */ {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xf9 */ {SNDRV_SEQ_EVENT_START, 0, NULL, NULL}, /* 0xfa */ {SNDRV_SEQ_EVENT_CONTINUE, 0, NULL, NULL}, /* 0xfb */ {SNDRV_SEQ_EVENT_STOP, 0, NULL, NULL}, /* 0xfc */ {SNDRV_SEQ_EVENT_NONE, -1, NULL, NULL}, /* 0xfd */ {SNDRV_SEQ_EVENT_SENSING, 0, NULL, NULL}, /* 0xfe */ {SNDRV_SEQ_EVENT_RESET, 0, NULL, NULL}, /* 0xff */ }; static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf, int len, struct snd_seq_event *ev); static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf, int count, struct snd_seq_event *ev); static struct extra_event_list { int event; int (*decode)(struct snd_midi_event *dev, unsigned char *buf, int len, struct snd_seq_event *ev); } extra_event[] = { {SNDRV_SEQ_EVENT_CONTROL14, extra_decode_ctrl14}, {SNDRV_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn}, {SNDRV_SEQ_EVENT_REGPARAM, extra_decode_xrpn}, }; /* * new/delete record */ int snd_midi_event_new(int bufsize, struct snd_midi_event **rdev) { struct snd_midi_event *dev; *rdev = NULL; dev = kzalloc(sizeof(*dev), GFP_KERNEL); if (dev == NULL) return -ENOMEM; if (bufsize > 0) { dev->buf = kmalloc(bufsize, GFP_KERNEL); if (dev->buf == NULL) { kfree(dev); return -ENOMEM; } } dev->bufsize = bufsize; dev->lastcmd = 0xff; dev->type = ST_INVALID; spin_lock_init(&dev->lock); *rdev = dev; return 0; } EXPORT_SYMBOL(snd_midi_event_new); void snd_midi_event_free(struct snd_midi_event *dev) { if (dev != NULL) { kfree(dev->buf); kfree(dev); } } EXPORT_SYMBOL(snd_midi_event_free); /* * initialize record */ static inline void reset_encode(struct snd_midi_event *dev) { dev->read = 0; dev->qlen = 0; dev->type = ST_INVALID; } void snd_midi_event_reset_encode(struct snd_midi_event *dev) { unsigned long flags; spin_lock_irqsave(&dev->lock, flags); reset_encode(dev); spin_unlock_irqrestore(&dev->lock, flags); } EXPORT_SYMBOL(snd_midi_event_reset_encode); void snd_midi_event_reset_decode(struct snd_midi_event *dev) { unsigned long flags; spin_lock_irqsave(&dev->lock, flags); dev->lastcmd = 0xff; spin_unlock_irqrestore(&dev->lock, flags); } EXPORT_SYMBOL(snd_midi_event_reset_decode); void snd_midi_event_no_status(struct snd_midi_event *dev, int on) { dev->nostat = on ? 1 : 0; } EXPORT_SYMBOL(snd_midi_event_no_status); /* * read one byte and encode to sequencer event: * return true if MIDI bytes are encoded to an event * false data is not finished */ bool snd_midi_event_encode_byte(struct snd_midi_event *dev, unsigned char c, struct snd_seq_event *ev) { bool rc = false; unsigned long flags; if (c >= MIDI_CMD_COMMON_CLOCK) { /* real-time event */ ev->type = status_event[ST_SPECIAL + c - 0xf0].event; ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED; return ev->type != SNDRV_SEQ_EVENT_NONE; } spin_lock_irqsave(&dev->lock, flags); if ((c & 0x80) && (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) { /* new command */ dev->buf[0] = c; if ((c & 0xf0) == 0xf0) /* system messages */ dev->type = (c & 0x0f) + ST_SPECIAL; else dev->type = (c >> 4) & 0x07; dev->read = 1; dev->qlen = status_event[dev->type].qlen; } else { if (dev->qlen > 0) { /* rest of command */ dev->buf[dev->read++] = c; if (dev->type != ST_SYSEX) dev->qlen--; } else { /* running status */ dev->buf[1] = c; dev->qlen = status_event[dev->type].qlen - 1; dev->read = 2; } } if (dev->qlen == 0) { ev->type = status_event[dev->type].event; ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED; if (status_event[dev->type].encode) /* set data values */ status_event[dev->type].encode(dev, ev); if (dev->type >= ST_SPECIAL) dev->type = ST_INVALID; rc = true; } else if (dev->type == ST_SYSEX) { if (c == MIDI_CMD_COMMON_SYSEX_END || dev->read >= dev->bufsize) { ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; ev->flags |= SNDRV_SEQ_EVENT_LENGTH_VARIABLE; ev->type = SNDRV_SEQ_EVENT_SYSEX; ev->data.ext.len = dev->read; ev->data.ext.ptr = dev->buf; if (c != MIDI_CMD_COMMON_SYSEX_END) dev->read = 0; /* continue to parse */ else reset_encode(dev); /* all parsed */ rc = true; } } spin_unlock_irqrestore(&dev->lock, flags); return rc; } EXPORT_SYMBOL(snd_midi_event_encode_byte); /* encode note event */ static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev) { ev->data.note.channel = dev->buf[0] & 0x0f; ev->data.note.note = dev->buf[1]; ev->data.note.velocity = dev->buf[2]; } /* encode one parameter controls */ static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev) { ev->data.control.channel = dev->buf[0] & 0x0f; ev->data.control.value = dev->buf[1]; } /* encode pitch wheel change */ static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev) { ev->data.control.channel = dev->buf[0] & 0x0f; ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192; } /* encode midi control change */ static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev) { ev->data.control.channel = dev->buf[0] & 0x0f; ev->data.control.param = dev->buf[1]; ev->data.control.value = dev->buf[2]; } /* encode one parameter value*/ static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev) { ev->data.control.value = dev->buf[1]; } /* encode song position */ static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev) { ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1]; } /* * decode from a sequencer event to midi bytes * return the size of decoded midi events */ long snd_midi_event_decode(struct snd_midi_event *dev, unsigned char *buf, long count, struct snd_seq_event *ev) { unsigned int cmd, type; if (ev->type == SNDRV_SEQ_EVENT_NONE) return -ENOENT; for (type = 0; type < ARRAY_SIZE(status_event); type++) { if (ev->type == status_event[type].event) goto __found; } for (type = 0; type < ARRAY_SIZE(extra_event); type++) { if (ev->type == extra_event[type].event) return extra_event[type].decode(dev, buf, count, ev); } return -ENOENT; __found: if (type >= ST_SPECIAL) cmd = 0xf0 + (type - ST_SPECIAL); else /* data.note.channel and data.control.channel is identical */ cmd = 0x80 | (type << 4) | (ev->data.note.channel & 0x0f); if (cmd == MIDI_CMD_COMMON_SYSEX) { snd_midi_event_reset_decode(dev); return snd_seq_expand_var_event(ev, count, buf, 1, 0); } else { int qlen; unsigned char xbuf[4]; unsigned long flags; spin_lock_irqsave(&dev->lock, flags); if ((cmd & 0xf0) == 0xf0 || dev->lastcmd != cmd || dev->nostat) { dev->lastcmd = cmd; spin_unlock_irqrestore(&dev->lock, flags); xbuf[0] = cmd; if (status_event[type].decode) status_event[type].decode(ev, xbuf + 1); qlen = status_event[type].qlen + 1; } else { spin_unlock_irqrestore(&dev->lock, flags); if (status_event[type].decode) status_event[type].decode(ev, xbuf + 0); qlen = status_event[type].qlen; } if (count < qlen) return -ENOMEM; memcpy(buf, xbuf, qlen); return qlen; } } EXPORT_SYMBOL(snd_midi_event_decode); /* decode note event */ static void note_decode(struct snd_seq_event *ev, unsigned char *buf) { buf[0] = ev->data.note.note & 0x7f; buf[1] = ev->data.note.velocity & 0x7f; } /* decode one parameter controls */ static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf) { buf[0] = ev->data.control.value & 0x7f; } /* decode pitch wheel change */ static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf) { int value = ev->data.control.value + 8192; buf[0] = value & 0x7f; buf[1] = (value >> 7) & 0x7f; } /* decode midi control change */ static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf) { buf[0] = ev->data.control.param & 0x7f; buf[1] = ev->data.control.value & 0x7f; } /* decode song position */ static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf) { buf[0] = ev->data.control.value & 0x7f; buf[1] = (ev->data.control.value >> 7) & 0x7f; } /* decode 14bit control */ static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf, int count, struct snd_seq_event *ev) { unsigned char cmd; int idx = 0; cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f); if (ev->data.control.param < 0x20) { if (count < 4) return -ENOMEM; if (dev->nostat && count < 6) return -ENOMEM; if (cmd != dev->lastcmd || dev->nostat) { if (count < 5) return -ENOMEM; buf[idx++] = dev->lastcmd = cmd; } buf[idx++] = ev->data.control.param; buf[idx++] = (ev->data.control.value >> 7) & 0x7f; if (dev->nostat) buf[idx++] = cmd; buf[idx++] = ev->data.control.param + 0x20; buf[idx++] = ev->data.control.value & 0x7f; } else { if (count < 2) return -ENOMEM; if (cmd != dev->lastcmd || dev->nostat) { if (count < 3) return -ENOMEM; buf[idx++] = dev->lastcmd = cmd; } buf[idx++] = ev->data.control.param & 0x7f; buf[idx++] = ev->data.control.value & 0x7f; } return idx; } /* decode reg/nonreg param */ static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf, int count, struct snd_seq_event *ev) { unsigned char cmd; const char *cbytes; static const char cbytes_nrpn[4] = { MIDI_CTL_NONREG_PARM_NUM_MSB, MIDI_CTL_NONREG_PARM_NUM_LSB, MIDI_CTL_MSB_DATA_ENTRY, MIDI_CTL_LSB_DATA_ENTRY }; static const char cbytes_rpn[4] = { MIDI_CTL_REGIST_PARM_NUM_MSB, MIDI_CTL_REGIST_PARM_NUM_LSB, MIDI_CTL_MSB_DATA_ENTRY, MIDI_CTL_LSB_DATA_ENTRY }; unsigned char bytes[4]; int idx = 0, i; if (count < 8) return -ENOMEM; if (dev->nostat && count < 12) return -ENOMEM; cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f); bytes[0] = (ev->data.control.param & 0x3f80) >> 7; bytes[1] = ev->data.control.param & 0x007f; bytes[2] = (ev->data.control.value & 0x3f80) >> 7; bytes[3] = ev->data.control.value & 0x007f; if (cmd != dev->lastcmd && !dev->nostat) { if (count < 9) return -ENOMEM; buf[idx++] = dev->lastcmd = cmd; } cbytes = ev->type == SNDRV_SEQ_EVENT_NONREGPARAM ? cbytes_nrpn : cbytes_rpn; for (i = 0; i < 4; i++) { if (dev->nostat) buf[idx++] = dev->lastcmd = cmd; buf[idx++] = cbytes[i]; buf[idx++] = bytes[i]; } return idx; }
linux-master
sound/core/seq/seq_midi_event.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * MIDI device handlers * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include <sound/asoundef.h> #include "seq_oss_midi.h" #include "seq_oss_readq.h" #include "seq_oss_timer.h" #include "seq_oss_event.h" #include <sound/seq_midi_event.h> #include "../seq_lock.h" #include <linux/init.h> #include <linux/slab.h> #include <linux/nospec.h> /* * constants */ #define SNDRV_SEQ_OSS_MAX_MIDI_NAME 30 /* * definition of midi device record */ struct seq_oss_midi { int seq_device; /* device number */ int client; /* sequencer client number */ int port; /* sequencer port number */ unsigned int flags; /* port capability */ int opened; /* flag for opening */ unsigned char name[SNDRV_SEQ_OSS_MAX_MIDI_NAME]; struct snd_midi_event *coder; /* MIDI event coder */ struct seq_oss_devinfo *devinfo; /* assigned OSSseq device */ snd_use_lock_t use_lock; struct mutex open_mutex; }; /* * midi device table */ static int max_midi_devs; static struct seq_oss_midi *midi_devs[SNDRV_SEQ_OSS_MAX_MIDI_DEVS]; static DEFINE_SPINLOCK(register_lock); /* * prototypes */ static struct seq_oss_midi *get_mdev(int dev); static struct seq_oss_midi *get_mididev(struct seq_oss_devinfo *dp, int dev); static int send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev); static int send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev); /* * look up the existing ports * this looks a very exhausting job. */ int snd_seq_oss_midi_lookup_ports(int client) { struct snd_seq_client_info *clinfo; struct snd_seq_port_info *pinfo; clinfo = kzalloc(sizeof(*clinfo), GFP_KERNEL); pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL); if (! clinfo || ! pinfo) { kfree(clinfo); kfree(pinfo); return -ENOMEM; } clinfo->client = -1; while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, clinfo) == 0) { if (clinfo->client == client) continue; /* ignore myself */ pinfo->addr.client = clinfo->client; pinfo->addr.port = -1; while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, pinfo) == 0) snd_seq_oss_midi_check_new_port(pinfo); } kfree(clinfo); kfree(pinfo); return 0; } /* */ static struct seq_oss_midi * get_mdev(int dev) { struct seq_oss_midi *mdev; unsigned long flags; spin_lock_irqsave(&register_lock, flags); mdev = midi_devs[dev]; if (mdev) snd_use_lock_use(&mdev->use_lock); spin_unlock_irqrestore(&register_lock, flags); return mdev; } /* * look for the identical slot */ static struct seq_oss_midi * find_slot(int client, int port) { int i; struct seq_oss_midi *mdev; unsigned long flags; spin_lock_irqsave(&register_lock, flags); for (i = 0; i < max_midi_devs; i++) { mdev = midi_devs[i]; if (mdev && mdev->client == client && mdev->port == port) { /* found! */ snd_use_lock_use(&mdev->use_lock); spin_unlock_irqrestore(&register_lock, flags); return mdev; } } spin_unlock_irqrestore(&register_lock, flags); return NULL; } #define PERM_WRITE (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE) #define PERM_READ (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ) /* * register a new port if it doesn't exist yet */ int snd_seq_oss_midi_check_new_port(struct snd_seq_port_info *pinfo) { int i; struct seq_oss_midi *mdev; unsigned long flags; /* the port must include generic midi */ if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC)) return 0; /* either read or write subscribable */ if ((pinfo->capability & PERM_WRITE) != PERM_WRITE && (pinfo->capability & PERM_READ) != PERM_READ) return 0; /* * look for the identical slot */ mdev = find_slot(pinfo->addr.client, pinfo->addr.port); if (mdev) { /* already exists */ snd_use_lock_free(&mdev->use_lock); return 0; } /* * allocate midi info record */ mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); if (!mdev) return -ENOMEM; /* copy the port information */ mdev->client = pinfo->addr.client; mdev->port = pinfo->addr.port; mdev->flags = pinfo->capability; mdev->opened = 0; snd_use_lock_init(&mdev->use_lock); mutex_init(&mdev->open_mutex); /* copy and truncate the name of synth device */ strscpy(mdev->name, pinfo->name, sizeof(mdev->name)); /* create MIDI coder */ if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) { pr_err("ALSA: seq_oss: can't malloc midi coder\n"); kfree(mdev); return -ENOMEM; } /* OSS sequencer adds running status to all sequences */ snd_midi_event_no_status(mdev->coder, 1); /* * look for en empty slot */ spin_lock_irqsave(&register_lock, flags); for (i = 0; i < max_midi_devs; i++) { if (midi_devs[i] == NULL) break; } if (i >= max_midi_devs) { if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) { spin_unlock_irqrestore(&register_lock, flags); snd_midi_event_free(mdev->coder); kfree(mdev); return -ENOMEM; } max_midi_devs++; } mdev->seq_device = i; midi_devs[mdev->seq_device] = mdev; spin_unlock_irqrestore(&register_lock, flags); return 0; } /* * release the midi device if it was registered */ int snd_seq_oss_midi_check_exit_port(int client, int port) { struct seq_oss_midi *mdev; unsigned long flags; int index; mdev = find_slot(client, port); if (mdev) { spin_lock_irqsave(&register_lock, flags); midi_devs[mdev->seq_device] = NULL; spin_unlock_irqrestore(&register_lock, flags); snd_use_lock_free(&mdev->use_lock); snd_use_lock_sync(&mdev->use_lock); snd_midi_event_free(mdev->coder); kfree(mdev); } spin_lock_irqsave(&register_lock, flags); for (index = max_midi_devs - 1; index >= 0; index--) { if (midi_devs[index]) break; } max_midi_devs = index + 1; spin_unlock_irqrestore(&register_lock, flags); return 0; } /* * release the midi device if it was registered */ void snd_seq_oss_midi_clear_all(void) { int i; struct seq_oss_midi *mdev; unsigned long flags; spin_lock_irqsave(&register_lock, flags); for (i = 0; i < max_midi_devs; i++) { mdev = midi_devs[i]; if (mdev) { snd_midi_event_free(mdev->coder); kfree(mdev); midi_devs[i] = NULL; } } max_midi_devs = 0; spin_unlock_irqrestore(&register_lock, flags); } /* * set up midi tables */ void snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp) { spin_lock_irq(&register_lock); dp->max_mididev = max_midi_devs; spin_unlock_irq(&register_lock); } /* * clean up midi tables */ void snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp) { int i; for (i = 0; i < dp->max_mididev; i++) snd_seq_oss_midi_close(dp, i); dp->max_mididev = 0; } /* * open all midi devices. ignore errors. */ void snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode) { int i; for (i = 0; i < dp->max_mididev; i++) snd_seq_oss_midi_open(dp, i, file_mode); } /* * get the midi device information */ static struct seq_oss_midi * get_mididev(struct seq_oss_devinfo *dp, int dev) { if (dev < 0 || dev >= dp->max_mididev) return NULL; dev = array_index_nospec(dev, dp->max_mididev); return get_mdev(dev); } /* * open the midi device if not opened yet */ int snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode) { int perm; struct seq_oss_midi *mdev; struct snd_seq_port_subscribe subs; int err; mdev = get_mididev(dp, dev); if (!mdev) return -ENODEV; mutex_lock(&mdev->open_mutex); /* already used? */ if (mdev->opened && mdev->devinfo != dp) { err = -EBUSY; goto unlock; } perm = 0; if (is_write_mode(fmode)) perm |= PERM_WRITE; if (is_read_mode(fmode)) perm |= PERM_READ; perm &= mdev->flags; if (perm == 0) { err = -ENXIO; goto unlock; } /* already opened? */ if ((mdev->opened & perm) == perm) { err = 0; goto unlock; } perm &= ~mdev->opened; memset(&subs, 0, sizeof(subs)); if (perm & PERM_WRITE) { subs.sender = dp->addr; subs.dest.client = mdev->client; subs.dest.port = mdev->port; if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0) mdev->opened |= PERM_WRITE; } if (perm & PERM_READ) { subs.sender.client = mdev->client; subs.sender.port = mdev->port; subs.dest = dp->addr; subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP; subs.queue = dp->queue; /* queue for timestamps */ if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0) mdev->opened |= PERM_READ; } if (! mdev->opened) { err = -ENXIO; goto unlock; } mdev->devinfo = dp; err = 0; unlock: mutex_unlock(&mdev->open_mutex); snd_use_lock_free(&mdev->use_lock); return err; } /* * close the midi device if already opened */ int snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev) { struct seq_oss_midi *mdev; struct snd_seq_port_subscribe subs; mdev = get_mididev(dp, dev); if (!mdev) return -ENODEV; mutex_lock(&mdev->open_mutex); if (!mdev->opened || mdev->devinfo != dp) goto unlock; memset(&subs, 0, sizeof(subs)); if (mdev->opened & PERM_WRITE) { subs.sender = dp->addr; subs.dest.client = mdev->client; subs.dest.port = mdev->port; snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs); } if (mdev->opened & PERM_READ) { subs.sender.client = mdev->client; subs.sender.port = mdev->port; subs.dest = dp->addr; snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs); } mdev->opened = 0; mdev->devinfo = NULL; unlock: mutex_unlock(&mdev->open_mutex); snd_use_lock_free(&mdev->use_lock); return 0; } /* * change seq capability flags to file mode flags */ int snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev) { struct seq_oss_midi *mdev; int mode; mdev = get_mididev(dp, dev); if (!mdev) return 0; mode = 0; if (mdev->opened & PERM_WRITE) mode |= SNDRV_SEQ_OSS_FILE_WRITE; if (mdev->opened & PERM_READ) mode |= SNDRV_SEQ_OSS_FILE_READ; snd_use_lock_free(&mdev->use_lock); return mode; } /* * reset the midi device and close it: * so far, only close the device. */ void snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev) { struct seq_oss_midi *mdev; mdev = get_mididev(dp, dev); if (!mdev) return; if (! mdev->opened) { snd_use_lock_free(&mdev->use_lock); return; } if (mdev->opened & PERM_WRITE) { struct snd_seq_event ev; int c; memset(&ev, 0, sizeof(ev)); ev.dest.client = mdev->client; ev.dest.port = mdev->port; ev.queue = dp->queue; ev.source.port = dp->port; if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) { ev.type = SNDRV_SEQ_EVENT_SENSING; snd_seq_oss_dispatch(dp, &ev, 0, 0); } for (c = 0; c < 16; c++) { ev.type = SNDRV_SEQ_EVENT_CONTROLLER; ev.data.control.channel = c; ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF; snd_seq_oss_dispatch(dp, &ev, 0, 0); if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) { ev.data.control.param = MIDI_CTL_RESET_CONTROLLERS; snd_seq_oss_dispatch(dp, &ev, 0, 0); ev.type = SNDRV_SEQ_EVENT_PITCHBEND; ev.data.control.value = 0; snd_seq_oss_dispatch(dp, &ev, 0, 0); } } } // snd_seq_oss_midi_close(dp, dev); snd_use_lock_free(&mdev->use_lock); } /* * get client/port of the specified MIDI device */ void snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr) { struct seq_oss_midi *mdev; mdev = get_mididev(dp, dev); if (!mdev) return; addr->client = mdev->client; addr->port = mdev->port; snd_use_lock_free(&mdev->use_lock); } /* * input callback - this can be atomic */ int snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data) { struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data; struct seq_oss_midi *mdev; int rc; if (dp->readq == NULL) return 0; mdev = find_slot(ev->source.client, ev->source.port); if (!mdev) return 0; if (! (mdev->opened & PERM_READ)) { snd_use_lock_free(&mdev->use_lock); return 0; } if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) rc = send_synth_event(dp, ev, mdev->seq_device); else rc = send_midi_event(dp, ev, mdev); snd_use_lock_free(&mdev->use_lock); return rc; } /* * convert ALSA sequencer event to OSS synth event */ static int send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev) { union evrec ossev; memset(&ossev, 0, sizeof(ossev)); switch (ev->type) { case SNDRV_SEQ_EVENT_NOTEON: ossev.v.cmd = MIDI_NOTEON; break; case SNDRV_SEQ_EVENT_NOTEOFF: ossev.v.cmd = MIDI_NOTEOFF; break; case SNDRV_SEQ_EVENT_KEYPRESS: ossev.v.cmd = MIDI_KEY_PRESSURE; break; case SNDRV_SEQ_EVENT_CONTROLLER: ossev.l.cmd = MIDI_CTL_CHANGE; break; case SNDRV_SEQ_EVENT_PGMCHANGE: ossev.l.cmd = MIDI_PGM_CHANGE; break; case SNDRV_SEQ_EVENT_CHANPRESS: ossev.l.cmd = MIDI_CHN_PRESSURE; break; case SNDRV_SEQ_EVENT_PITCHBEND: ossev.l.cmd = MIDI_PITCH_BEND; break; default: return 0; /* not supported */ } ossev.v.dev = dev; switch (ev->type) { case SNDRV_SEQ_EVENT_NOTEON: case SNDRV_SEQ_EVENT_NOTEOFF: case SNDRV_SEQ_EVENT_KEYPRESS: ossev.v.code = EV_CHN_VOICE; ossev.v.note = ev->data.note.note; ossev.v.parm = ev->data.note.velocity; ossev.v.chn = ev->data.note.channel; break; case SNDRV_SEQ_EVENT_CONTROLLER: case SNDRV_SEQ_EVENT_PGMCHANGE: case SNDRV_SEQ_EVENT_CHANPRESS: ossev.l.code = EV_CHN_COMMON; ossev.l.p1 = ev->data.control.param; ossev.l.val = ev->data.control.value; ossev.l.chn = ev->data.control.channel; break; case SNDRV_SEQ_EVENT_PITCHBEND: ossev.l.code = EV_CHN_COMMON; ossev.l.val = ev->data.control.value + 8192; ossev.l.chn = ev->data.control.channel; break; } snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode); snd_seq_oss_readq_put_event(dp->readq, &ossev); return 0; } /* * decode event and send MIDI bytes to read queue */ static int send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev) { char msg[32]; int len; snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode); if (!dp->timer->running) len = snd_seq_oss_timer_start(dp->timer); if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { snd_seq_oss_readq_sysex(dp->readq, mdev->seq_device, ev); snd_midi_event_reset_decode(mdev->coder); } else { len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev); if (len > 0) snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len); } return 0; } /* * dump midi data * return 0 : enqueued * non-zero : invalid - ignored */ int snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev) { struct seq_oss_midi *mdev; mdev = get_mididev(dp, dev); if (!mdev) return -ENODEV; if (snd_midi_event_encode_byte(mdev->coder, c, ev)) { snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port); snd_use_lock_free(&mdev->use_lock); return 0; } snd_use_lock_free(&mdev->use_lock); return -EINVAL; } /* * create OSS compatible midi_info record */ int snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf) { struct seq_oss_midi *mdev; mdev = get_mididev(dp, dev); if (!mdev) return -ENXIO; inf->device = dev; inf->dev_type = 0; /* FIXME: ?? */ inf->capabilities = 0; /* FIXME: ?? */ strscpy(inf->name, mdev->name, sizeof(inf->name)); snd_use_lock_free(&mdev->use_lock); return 0; } #ifdef CONFIG_SND_PROC_FS /* * proc interface */ static char * capmode_str(int val) { val &= PERM_READ|PERM_WRITE; if (val == (PERM_READ|PERM_WRITE)) return "read/write"; else if (val == PERM_READ) return "read"; else if (val == PERM_WRITE) return "write"; else return "none"; } void snd_seq_oss_midi_info_read(struct snd_info_buffer *buf) { int i; struct seq_oss_midi *mdev; snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs); for (i = 0; i < max_midi_devs; i++) { snd_iprintf(buf, "\nmidi %d: ", i); mdev = get_mdev(i); if (mdev == NULL) { snd_iprintf(buf, "*empty*\n"); continue; } snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name, mdev->client, mdev->port); snd_iprintf(buf, " capability %s / opened %s\n", capmode_str(mdev->flags), capmode_str(mdev->opened)); snd_use_lock_free(&mdev->use_lock); } } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/oss/seq_oss_midi.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * read/write/select interface to device file * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_device.h" #include "seq_oss_readq.h" #include "seq_oss_writeq.h" #include "seq_oss_synth.h" #include <sound/seq_oss_legacy.h> #include "seq_oss_event.h" #include "seq_oss_timer.h" #include "../seq_clientmgr.h" /* * protoypes */ static int insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt); /* * read interface */ int snd_seq_oss_read(struct seq_oss_devinfo *dp, char __user *buf, int count) { struct seq_oss_readq *readq = dp->readq; int result = 0, err = 0; int ev_len; union evrec rec; unsigned long flags; if (readq == NULL || ! is_read_mode(dp->file_mode)) return -ENXIO; while (count >= SHORT_EVENT_SIZE) { snd_seq_oss_readq_lock(readq, flags); err = snd_seq_oss_readq_pick(readq, &rec); if (err == -EAGAIN && !is_nonblock_mode(dp->file_mode) && result == 0) { snd_seq_oss_readq_unlock(readq, flags); snd_seq_oss_readq_wait(readq); snd_seq_oss_readq_lock(readq, flags); if (signal_pending(current)) err = -ERESTARTSYS; else err = snd_seq_oss_readq_pick(readq, &rec); } if (err < 0) { snd_seq_oss_readq_unlock(readq, flags); break; } ev_len = ev_length(&rec); if (ev_len < count) { snd_seq_oss_readq_unlock(readq, flags); break; } snd_seq_oss_readq_free(readq); snd_seq_oss_readq_unlock(readq, flags); if (copy_to_user(buf, &rec, ev_len)) { err = -EFAULT; break; } result += ev_len; buf += ev_len; count -= ev_len; } return result > 0 ? result : err; } /* * write interface */ int snd_seq_oss_write(struct seq_oss_devinfo *dp, const char __user *buf, int count, struct file *opt) { int result = 0, err = 0; int ev_size, fmt; union evrec rec; if (! is_write_mode(dp->file_mode) || dp->writeq == NULL) return -ENXIO; while (count >= SHORT_EVENT_SIZE) { if (copy_from_user(&rec, buf, SHORT_EVENT_SIZE)) { err = -EFAULT; break; } if (rec.s.code == SEQ_FULLSIZE) { /* load patch */ if (result > 0) { err = -EINVAL; break; } fmt = (*(unsigned short *)rec.c) & 0xffff; /* FIXME the return value isn't correct */ return snd_seq_oss_synth_load_patch(dp, rec.s.dev, fmt, buf, 0, count); } if (ev_is_long(&rec)) { /* extended code */ if (rec.s.code == SEQ_EXTENDED && dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) { err = -EINVAL; break; } ev_size = LONG_EVENT_SIZE; if (count < ev_size) break; /* copy the reset 4 bytes */ if (copy_from_user(rec.c + SHORT_EVENT_SIZE, buf + SHORT_EVENT_SIZE, LONG_EVENT_SIZE - SHORT_EVENT_SIZE)) { err = -EFAULT; break; } } else { /* old-type code */ if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) { err = -EINVAL; break; } ev_size = SHORT_EVENT_SIZE; } /* insert queue */ err = insert_queue(dp, &rec, opt); if (err < 0) break; result += ev_size; buf += ev_size; count -= ev_size; } return result > 0 ? result : err; } /* * insert event record to write queue * return: 0 = OK, non-zero = NG */ static int insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) { int rc = 0; struct snd_seq_event event; /* if this is a timing event, process the current time */ if (snd_seq_oss_process_timer_event(dp->timer, rec)) return 0; /* no need to insert queue */ /* parse this event */ memset(&event, 0, sizeof(event)); /* set dummy -- to be sure */ event.type = SNDRV_SEQ_EVENT_NOTEOFF; snd_seq_oss_fill_addr(dp, &event, dp->addr.client, dp->addr.port); if (snd_seq_oss_process_event(dp, rec, &event)) return 0; /* invalid event - no need to insert queue */ event.time.tick = snd_seq_oss_timer_cur_tick(dp->timer); if (dp->timer->realtime || !dp->timer->running) snd_seq_oss_dispatch(dp, &event, 0, 0); else rc = snd_seq_kernel_client_enqueue(dp->cseq, &event, opt, !is_nonblock_mode(dp->file_mode)); return rc; } /* * select / poll */ __poll_t snd_seq_oss_poll(struct seq_oss_devinfo *dp, struct file *file, poll_table * wait) { __poll_t mask = 0; /* input */ if (dp->readq && is_read_mode(dp->file_mode)) { if (snd_seq_oss_readq_poll(dp->readq, file, wait)) mask |= EPOLLIN | EPOLLRDNORM; } /* output */ if (dp->writeq && is_write_mode(dp->file_mode)) { if (snd_seq_kernel_client_write_poll(dp->cseq, file, wait)) mask |= EPOLLOUT | EPOLLWRNORM; } return mask; }
linux-master
sound/core/seq/oss/seq_oss_rw.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * registration of device and proc * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include <linux/init.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/compat.h> #include <sound/core.h> #include <sound/minors.h> #include <sound/initval.h> #include "seq_oss_device.h" #include "seq_oss_synth.h" /* * module option */ MODULE_AUTHOR("Takashi Iwai <[email protected]>"); MODULE_DESCRIPTION("OSS-compatible sequencer module"); MODULE_LICENSE("GPL"); /* Takashi says this is really only for sound-service-0-, but this is OK. */ MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_SEQUENCER); MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MUSIC); /* * prototypes */ static int register_device(void); static void unregister_device(void); #ifdef CONFIG_SND_PROC_FS static int register_proc(void); static void unregister_proc(void); #else static inline int register_proc(void) { return 0; } static inline void unregister_proc(void) {} #endif static int odev_open(struct inode *inode, struct file *file); static int odev_release(struct inode *inode, struct file *file); static ssize_t odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset); static ssize_t odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset); static long odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg); static __poll_t odev_poll(struct file *file, poll_table * wait); /* * module interface */ static struct snd_seq_driver seq_oss_synth_driver = { .driver = { .name = KBUILD_MODNAME, .probe = snd_seq_oss_synth_probe, .remove = snd_seq_oss_synth_remove, }, .id = SNDRV_SEQ_DEV_ID_OSS, .argsize = sizeof(struct snd_seq_oss_reg), }; static int __init alsa_seq_oss_init(void) { int rc; rc = register_device(); if (rc < 0) goto error; rc = register_proc(); if (rc < 0) { unregister_device(); goto error; } rc = snd_seq_oss_create_client(); if (rc < 0) { unregister_proc(); unregister_device(); goto error; } rc = snd_seq_driver_register(&seq_oss_synth_driver); if (rc < 0) { snd_seq_oss_delete_client(); unregister_proc(); unregister_device(); goto error; } /* success */ snd_seq_oss_synth_init(); error: return rc; } static void __exit alsa_seq_oss_exit(void) { snd_seq_driver_unregister(&seq_oss_synth_driver); snd_seq_oss_delete_client(); unregister_proc(); unregister_device(); } module_init(alsa_seq_oss_init) module_exit(alsa_seq_oss_exit) /* * ALSA minor device interface */ static DEFINE_MUTEX(register_mutex); static int odev_open(struct inode *inode, struct file *file) { int level, rc; if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC) level = SNDRV_SEQ_OSS_MODE_MUSIC; else level = SNDRV_SEQ_OSS_MODE_SYNTH; mutex_lock(&register_mutex); rc = snd_seq_oss_open(file, level); mutex_unlock(&register_mutex); return rc; } static int odev_release(struct inode *inode, struct file *file) { struct seq_oss_devinfo *dp; dp = file->private_data; if (!dp) return 0; mutex_lock(&register_mutex); snd_seq_oss_release(dp); mutex_unlock(&register_mutex); return 0; } static ssize_t odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset) { struct seq_oss_devinfo *dp; dp = file->private_data; if (snd_BUG_ON(!dp)) return -ENXIO; return snd_seq_oss_read(dp, buf, count); } static ssize_t odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { struct seq_oss_devinfo *dp; dp = file->private_data; if (snd_BUG_ON(!dp)) return -ENXIO; return snd_seq_oss_write(dp, buf, count, file); } static long odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct seq_oss_devinfo *dp; long rc; dp = file->private_data; if (snd_BUG_ON(!dp)) return -ENXIO; if (cmd != SNDCTL_SEQ_SYNC && mutex_lock_interruptible(&register_mutex)) return -ERESTARTSYS; rc = snd_seq_oss_ioctl(dp, cmd, arg); if (cmd != SNDCTL_SEQ_SYNC) mutex_unlock(&register_mutex); return rc; } #ifdef CONFIG_COMPAT static long odev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) { return odev_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); } #else #define odev_ioctl_compat NULL #endif static __poll_t odev_poll(struct file *file, poll_table * wait) { struct seq_oss_devinfo *dp; dp = file->private_data; if (snd_BUG_ON(!dp)) return EPOLLERR; return snd_seq_oss_poll(dp, file, wait); } /* * registration of sequencer minor device */ static const struct file_operations seq_oss_f_ops = { .owner = THIS_MODULE, .read = odev_read, .write = odev_write, .open = odev_open, .release = odev_release, .poll = odev_poll, .unlocked_ioctl = odev_ioctl, .compat_ioctl = odev_ioctl_compat, .llseek = noop_llseek, }; static int __init register_device(void) { int rc; mutex_lock(&register_mutex); rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0, &seq_oss_f_ops, NULL); if (rc < 0) { pr_err("ALSA: seq_oss: can't register device seq\n"); mutex_unlock(&register_mutex); return rc; } rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0, &seq_oss_f_ops, NULL); if (rc < 0) { pr_err("ALSA: seq_oss: can't register device music\n"); snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0); mutex_unlock(&register_mutex); return rc; } mutex_unlock(&register_mutex); return 0; } static void unregister_device(void) { mutex_lock(&register_mutex); if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0) pr_err("ALSA: seq_oss: error unregister device music\n"); if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0) pr_err("ALSA: seq_oss: error unregister device seq\n"); mutex_unlock(&register_mutex); } /* * /proc interface */ #ifdef CONFIG_SND_PROC_FS static struct snd_info_entry *info_entry; static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buf) { mutex_lock(&register_mutex); snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR); snd_seq_oss_system_info_read(buf); snd_seq_oss_synth_info_read(buf); snd_seq_oss_midi_info_read(buf); mutex_unlock(&register_mutex); } static int __init register_proc(void) { struct snd_info_entry *entry; entry = snd_info_create_module_entry(THIS_MODULE, SNDRV_SEQ_OSS_PROCNAME, snd_seq_root); if (entry == NULL) return -ENOMEM; entry->content = SNDRV_INFO_CONTENT_TEXT; entry->private_data = NULL; entry->c.text.read = info_read; if (snd_info_register(entry) < 0) { snd_info_free_entry(entry); return -ENOMEM; } info_entry = entry; return 0; } static void unregister_proc(void) { snd_info_free_entry(info_entry); info_entry = NULL; } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/oss/seq_oss.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * seq_oss_writeq.c - write queue and sync * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_writeq.h" #include "seq_oss_event.h" #include "seq_oss_timer.h" #include <sound/seq_oss_legacy.h> #include "../seq_lock.h" #include "../seq_clientmgr.h" #include <linux/wait.h> #include <linux/slab.h> #include <linux/sched/signal.h> /* * create a write queue record */ struct seq_oss_writeq * snd_seq_oss_writeq_new(struct seq_oss_devinfo *dp, int maxlen) { struct seq_oss_writeq *q; struct snd_seq_client_pool pool; q = kzalloc(sizeof(*q), GFP_KERNEL); if (!q) return NULL; q->dp = dp; q->maxlen = maxlen; spin_lock_init(&q->sync_lock); q->sync_event_put = 0; q->sync_time = 0; init_waitqueue_head(&q->sync_sleep); memset(&pool, 0, sizeof(pool)); pool.client = dp->cseq; pool.output_pool = maxlen; pool.output_room = maxlen / 2; snd_seq_oss_control(dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool); return q; } /* * delete the write queue */ void snd_seq_oss_writeq_delete(struct seq_oss_writeq *q) { if (q) { snd_seq_oss_writeq_clear(q); /* to be sure */ kfree(q); } } /* * reset the write queue */ void snd_seq_oss_writeq_clear(struct seq_oss_writeq *q) { struct snd_seq_remove_events reset; memset(&reset, 0, sizeof(reset)); reset.remove_mode = SNDRV_SEQ_REMOVE_OUTPUT; /* remove all */ snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_REMOVE_EVENTS, &reset); /* wake up sleepers if any */ snd_seq_oss_writeq_wakeup(q, 0); } /* * wait until the write buffer has enough room */ int snd_seq_oss_writeq_sync(struct seq_oss_writeq *q) { struct seq_oss_devinfo *dp = q->dp; abstime_t time; time = snd_seq_oss_timer_cur_tick(dp->timer); if (q->sync_time >= time) return 0; /* already finished */ if (! q->sync_event_put) { struct snd_seq_event ev; union evrec *rec; /* put echoback event */ memset(&ev, 0, sizeof(ev)); ev.flags = 0; ev.type = SNDRV_SEQ_EVENT_ECHO; ev.time.tick = time; /* echo back to itself */ snd_seq_oss_fill_addr(dp, &ev, dp->addr.client, dp->addr.port); rec = (union evrec *)&ev.data; rec->t.code = SEQ_SYNCTIMER; rec->t.time = time; q->sync_event_put = 1; snd_seq_kernel_client_enqueue(dp->cseq, &ev, NULL, true); } wait_event_interruptible_timeout(q->sync_sleep, ! q->sync_event_put, HZ); if (signal_pending(current)) /* interrupted - return 0 to finish sync */ q->sync_event_put = 0; if (! q->sync_event_put || q->sync_time >= time) return 0; return 1; } /* * wake up sync - echo event was catched */ void snd_seq_oss_writeq_wakeup(struct seq_oss_writeq *q, abstime_t time) { unsigned long flags; spin_lock_irqsave(&q->sync_lock, flags); q->sync_time = time; q->sync_event_put = 0; wake_up(&q->sync_sleep); spin_unlock_irqrestore(&q->sync_lock, flags); } /* * return the unused pool size */ int snd_seq_oss_writeq_get_free_size(struct seq_oss_writeq *q) { struct snd_seq_client_pool pool; pool.client = q->dp->cseq; snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool); return pool.output_free; } /* * set output threshold size from ioctl */ void snd_seq_oss_writeq_set_output(struct seq_oss_writeq *q, int val) { struct snd_seq_client_pool pool; pool.client = q->dp->cseq; snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool); pool.output_room = val; snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool); }
linux-master
sound/core/seq/oss/seq_oss_writeq.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * Timer control routines * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_timer.h" #include "seq_oss_event.h" #include <sound/seq_oss_legacy.h> #include <linux/slab.h> /* */ #define MIN_OSS_TEMPO 8 #define MAX_OSS_TEMPO 360 #define MIN_OSS_TIMEBASE 1 #define MAX_OSS_TIMEBASE 1000 /* */ static void calc_alsa_tempo(struct seq_oss_timer *timer); static int send_timer_event(struct seq_oss_devinfo *dp, int type, int value); /* * create and register a new timer. * if queue is not started yet, start it. */ struct seq_oss_timer * snd_seq_oss_timer_new(struct seq_oss_devinfo *dp) { struct seq_oss_timer *rec; rec = kzalloc(sizeof(*rec), GFP_KERNEL); if (rec == NULL) return NULL; rec->dp = dp; rec->cur_tick = 0; rec->realtime = 0; rec->running = 0; rec->oss_tempo = 60; rec->oss_timebase = 100; calc_alsa_tempo(rec); return rec; } /* * delete timer. * if no more timer exists, stop the queue. */ void snd_seq_oss_timer_delete(struct seq_oss_timer *rec) { if (rec) { snd_seq_oss_timer_stop(rec); kfree(rec); } } /* * process one timing event * return 1 : event proceseed -- skip this event * 0 : not a timer event -- enqueue this event */ int snd_seq_oss_process_timer_event(struct seq_oss_timer *rec, union evrec *ev) { abstime_t parm = ev->t.time; if (ev->t.code == EV_TIMING) { switch (ev->t.cmd) { case TMR_WAIT_REL: parm += rec->cur_tick; rec->realtime = 0; fallthrough; case TMR_WAIT_ABS: if (parm == 0) { rec->realtime = 1; } else if (parm >= rec->cur_tick) { rec->realtime = 0; rec->cur_tick = parm; } return 1; /* skip this event */ case TMR_START: snd_seq_oss_timer_start(rec); return 1; } } else if (ev->s.code == SEQ_WAIT) { /* time = from 1 to 3 bytes */ parm = (ev->echo >> 8) & 0xffffff; if (parm > rec->cur_tick) { /* set next event time */ rec->cur_tick = parm; rec->realtime = 0; } return 1; } return 0; } /* * convert tempo units */ static void calc_alsa_tempo(struct seq_oss_timer *timer) { timer->tempo = (60 * 1000000) / timer->oss_tempo; timer->ppq = timer->oss_timebase; } /* * dispatch a timer event */ static int send_timer_event(struct seq_oss_devinfo *dp, int type, int value) { struct snd_seq_event ev; memset(&ev, 0, sizeof(ev)); ev.type = type; ev.source.client = dp->cseq; ev.source.port = 0; ev.dest.client = SNDRV_SEQ_CLIENT_SYSTEM; ev.dest.port = SNDRV_SEQ_PORT_SYSTEM_TIMER; ev.queue = dp->queue; ev.data.queue.queue = dp->queue; ev.data.queue.param.value = value; return snd_seq_kernel_client_dispatch(dp->cseq, &ev, 1, 0); } /* * set queue tempo and start queue */ int snd_seq_oss_timer_start(struct seq_oss_timer *timer) { struct seq_oss_devinfo *dp = timer->dp; struct snd_seq_queue_tempo tmprec; if (timer->running) snd_seq_oss_timer_stop(timer); memset(&tmprec, 0, sizeof(tmprec)); tmprec.queue = dp->queue; tmprec.ppq = timer->ppq; tmprec.tempo = timer->tempo; snd_seq_set_queue_tempo(dp->cseq, &tmprec); send_timer_event(dp, SNDRV_SEQ_EVENT_START, 0); timer->running = 1; timer->cur_tick = 0; return 0; } /* * stop queue */ int snd_seq_oss_timer_stop(struct seq_oss_timer *timer) { if (! timer->running) return 0; send_timer_event(timer->dp, SNDRV_SEQ_EVENT_STOP, 0); timer->running = 0; return 0; } /* * continue queue */ int snd_seq_oss_timer_continue(struct seq_oss_timer *timer) { if (timer->running) return 0; send_timer_event(timer->dp, SNDRV_SEQ_EVENT_CONTINUE, 0); timer->running = 1; return 0; } /* * change queue tempo */ int snd_seq_oss_timer_tempo(struct seq_oss_timer *timer, int value) { if (value < MIN_OSS_TEMPO) value = MIN_OSS_TEMPO; else if (value > MAX_OSS_TEMPO) value = MAX_OSS_TEMPO; timer->oss_tempo = value; calc_alsa_tempo(timer); if (timer->running) send_timer_event(timer->dp, SNDRV_SEQ_EVENT_TEMPO, timer->tempo); return 0; } /* * ioctls */ int snd_seq_oss_timer_ioctl(struct seq_oss_timer *timer, unsigned int cmd, int __user *arg) { int value; if (cmd == SNDCTL_SEQ_CTRLRATE) { /* if *arg == 0, just return the current rate */ if (get_user(value, arg)) return -EFAULT; if (value) return -EINVAL; value = ((timer->oss_tempo * timer->oss_timebase) + 30) / 60; return put_user(value, arg) ? -EFAULT : 0; } if (timer->dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) return 0; switch (cmd) { case SNDCTL_TMR_START: return snd_seq_oss_timer_start(timer); case SNDCTL_TMR_STOP: return snd_seq_oss_timer_stop(timer); case SNDCTL_TMR_CONTINUE: return snd_seq_oss_timer_continue(timer); case SNDCTL_TMR_TEMPO: if (get_user(value, arg)) return -EFAULT; return snd_seq_oss_timer_tempo(timer, value); case SNDCTL_TMR_TIMEBASE: if (get_user(value, arg)) return -EFAULT; if (value < MIN_OSS_TIMEBASE) value = MIN_OSS_TIMEBASE; else if (value > MAX_OSS_TIMEBASE) value = MAX_OSS_TIMEBASE; timer->oss_timebase = value; calc_alsa_tempo(timer); return 0; case SNDCTL_TMR_METRONOME: case SNDCTL_TMR_SELECT: case SNDCTL_TMR_SOURCE: /* not supported */ return 0; } return 0; }
linux-master
sound/core/seq/oss/seq_oss_timer.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * seq_oss_readq.c - MIDI input queue * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_readq.h" #include "seq_oss_event.h" #include <sound/seq_oss_legacy.h> #include "../seq_lock.h" #include <linux/wait.h> #include <linux/slab.h> /* * constants */ //#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1) #define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600) /* * prototypes */ /* * create a read queue */ struct seq_oss_readq * snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen) { struct seq_oss_readq *q; q = kzalloc(sizeof(*q), GFP_KERNEL); if (!q) return NULL; q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL); if (!q->q) { kfree(q); return NULL; } q->maxlen = maxlen; q->qlen = 0; q->head = q->tail = 0; init_waitqueue_head(&q->midi_sleep); spin_lock_init(&q->lock); q->pre_event_timeout = SNDRV_SEQ_OSS_MAX_TIMEOUT; q->input_time = (unsigned long)-1; return q; } /* * delete the read queue */ void snd_seq_oss_readq_delete(struct seq_oss_readq *q) { if (q) { kfree(q->q); kfree(q); } } /* * reset the read queue */ void snd_seq_oss_readq_clear(struct seq_oss_readq *q) { if (q->qlen) { q->qlen = 0; q->head = q->tail = 0; } /* if someone sleeping, wake'em up */ wake_up(&q->midi_sleep); q->input_time = (unsigned long)-1; } /* * put a midi byte */ int snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len) { union evrec rec; int result; memset(&rec, 0, sizeof(rec)); rec.c[0] = SEQ_MIDIPUTC; rec.c[2] = dev; while (len-- > 0) { rec.c[1] = *data++; result = snd_seq_oss_readq_put_event(q, &rec); if (result < 0) return result; } return 0; } /* * put MIDI sysex bytes; the event buffer may be chained, thus it has * to be expanded via snd_seq_dump_var_event(). */ struct readq_sysex_ctx { struct seq_oss_readq *readq; int dev; }; static int readq_dump_sysex(void *ptr, void *buf, int count) { struct readq_sysex_ctx *ctx = ptr; return snd_seq_oss_readq_puts(ctx->readq, ctx->dev, buf, count); } int snd_seq_oss_readq_sysex(struct seq_oss_readq *q, int dev, struct snd_seq_event *ev) { struct readq_sysex_ctx ctx = { .readq = q, .dev = dev }; if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) return 0; return snd_seq_dump_var_event(ev, readq_dump_sysex, &ctx); } /* * copy an event to input queue: * return zero if enqueued */ int snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev) { unsigned long flags; spin_lock_irqsave(&q->lock, flags); if (q->qlen >= q->maxlen - 1) { spin_unlock_irqrestore(&q->lock, flags); return -ENOMEM; } memcpy(&q->q[q->tail], ev, sizeof(*ev)); q->tail = (q->tail + 1) % q->maxlen; q->qlen++; /* wake up sleeper */ wake_up(&q->midi_sleep); spin_unlock_irqrestore(&q->lock, flags); return 0; } /* * pop queue * caller must hold lock */ int snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec) { if (q->qlen == 0) return -EAGAIN; memcpy(rec, &q->q[q->head], sizeof(*rec)); return 0; } /* * sleep until ready */ void snd_seq_oss_readq_wait(struct seq_oss_readq *q) { wait_event_interruptible_timeout(q->midi_sleep, (q->qlen > 0 || q->head == q->tail), q->pre_event_timeout); } /* * drain one record * caller must hold lock */ void snd_seq_oss_readq_free(struct seq_oss_readq *q) { if (q->qlen > 0) { q->head = (q->head + 1) % q->maxlen; q->qlen--; } } /* * polling/select: * return non-zero if readq is not empty. */ unsigned int snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait) { poll_wait(file, &q->midi_sleep, wait); return q->qlen; } /* * put a timestamp */ int snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode) { if (curt != q->input_time) { union evrec rec; memset(&rec, 0, sizeof(rec)); switch (seq_mode) { case SNDRV_SEQ_OSS_MODE_SYNTH: rec.echo = (curt << 8) | SEQ_WAIT; snd_seq_oss_readq_put_event(q, &rec); break; case SNDRV_SEQ_OSS_MODE_MUSIC: rec.t.code = EV_TIMING; rec.t.cmd = TMR_WAIT_ABS; rec.t.time = curt; snd_seq_oss_readq_put_event(q, &rec); break; } q->input_time = curt; } return 0; } #ifdef CONFIG_SND_PROC_FS /* * proc interface */ void snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf) { snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n", (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"), q->qlen, q->input_time); } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/oss/seq_oss_readq.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_device.h" #include "seq_oss_synth.h" #include "seq_oss_midi.h" #include "seq_oss_event.h" #include "seq_oss_timer.h" #include <sound/seq_oss_legacy.h> #include "seq_oss_readq.h" #include "seq_oss_writeq.h" #include <linux/nospec.h> /* * prototypes */ static int extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev); static int chn_voice_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev); static int chn_common_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev); static int timing_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev); static int local_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev); static int old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev); static int note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev); static int note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev); static int set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev); static int set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev); static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev); /* * convert an OSS event to ALSA event * return 0 : enqueued * non-zero : invalid - ignored */ int snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { switch (q->s.code) { case SEQ_EXTENDED: return extended_event(dp, q, ev); case EV_CHN_VOICE: return chn_voice_event(dp, q, ev); case EV_CHN_COMMON: return chn_common_event(dp, q, ev); case EV_TIMING: return timing_event(dp, q, ev); case EV_SEQ_LOCAL: return local_event(dp, q, ev); case EV_SYSEX: return snd_seq_oss_synth_sysex(dp, q->x.dev, q->x.buf, ev); case SEQ_MIDIPUTC: if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return -EINVAL; /* put a midi byte */ if (! is_write_mode(dp->file_mode)) break; if (snd_seq_oss_midi_open(dp, q->s.dev, SNDRV_SEQ_OSS_FILE_WRITE)) break; if (snd_seq_oss_midi_filemode(dp, q->s.dev) & SNDRV_SEQ_OSS_FILE_WRITE) return snd_seq_oss_midi_putc(dp, q->s.dev, q->s.parm1, ev); break; case SEQ_ECHO: if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return -EINVAL; return set_echo_event(dp, q, ev); case SEQ_PRIVATE: if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return -EINVAL; return snd_seq_oss_synth_raw_event(dp, q->c[1], q->c, ev); default: if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return -EINVAL; return old_event(dp, q, ev); } return -EINVAL; } /* old type events: mode1 only */ static int old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { switch (q->s.code) { case SEQ_NOTEOFF: return note_off_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev); case SEQ_NOTEON: return note_on_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev); case SEQ_WAIT: /* skip */ break; case SEQ_PGMCHANGE: return set_control_event(dp, 0, SNDRV_SEQ_EVENT_PGMCHANGE, q->n.chn, 0, q->n.note, ev); case SEQ_SYNCTIMER: return snd_seq_oss_timer_reset(dp->timer); } return -EINVAL; } /* 8bytes extended event: mode1 only */ static int extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { int val; switch (q->e.cmd) { case SEQ_NOTEOFF: return note_off_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev); case SEQ_NOTEON: return note_on_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev); case SEQ_PGMCHANGE: return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_PGMCHANGE, q->e.chn, 0, q->e.p1, ev); case SEQ_AFTERTOUCH: return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CHANPRESS, q->e.chn, 0, q->e.p1, ev); case SEQ_BALANCE: /* convert -128:127 to 0:127 */ val = (char)q->e.p1; val = (val + 128) / 2; return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CONTROLLER, q->e.chn, CTL_PAN, val, ev); case SEQ_CONTROLLER: val = ((short)q->e.p3 << 8) | (short)q->e.p2; switch (q->e.p1) { case CTRL_PITCH_BENDER: /* SEQ1 V2 control */ /* -0x2000:0x1fff */ return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_PITCHBEND, q->e.chn, 0, val, ev); case CTRL_PITCH_BENDER_RANGE: /* conversion: 100/semitone -> 128/semitone */ return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_REGPARAM, q->e.chn, 0, val*128/100, ev); default: return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CONTROL14, q->e.chn, q->e.p1, val, ev); } case SEQ_VOLMODE: return snd_seq_oss_synth_raw_event(dp, q->e.dev, q->c, ev); } return -EINVAL; } /* channel voice events: mode1 and 2 */ static int chn_voice_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { if (q->v.chn >= 32) return -EINVAL; switch (q->v.cmd) { case MIDI_NOTEON: return note_on_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev); case MIDI_NOTEOFF: return note_off_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev); case MIDI_KEY_PRESSURE: return set_note_event(dp, q->v.dev, SNDRV_SEQ_EVENT_KEYPRESS, q->v.chn, q->v.note, q->v.parm, ev); } return -EINVAL; } /* channel common events: mode1 and 2 */ static int chn_common_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { if (q->l.chn >= 32) return -EINVAL; switch (q->l.cmd) { case MIDI_PGM_CHANGE: return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PGMCHANGE, q->l.chn, 0, q->l.p1, ev); case MIDI_CTL_CHANGE: return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CONTROLLER, q->l.chn, q->l.p1, q->l.val, ev); case MIDI_PITCH_BEND: /* conversion: 0:0x3fff -> -0x2000:0x1fff */ return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PITCHBEND, q->l.chn, 0, q->l.val - 8192, ev); case MIDI_CHN_PRESSURE: return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CHANPRESS, q->l.chn, 0, q->l.val, ev); } return -EINVAL; } /* timer events: mode1 and mode2 */ static int timing_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { switch (q->t.cmd) { case TMR_ECHO: if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) return set_echo_event(dp, q, ev); else { union evrec tmp; memset(&tmp, 0, sizeof(tmp)); /* XXX: only for little-endian! */ tmp.echo = (q->t.time << 8) | SEQ_ECHO; return set_echo_event(dp, &tmp, ev); } case TMR_STOP: if (dp->seq_mode) return snd_seq_oss_timer_stop(dp->timer); return 0; case TMR_CONTINUE: if (dp->seq_mode) return snd_seq_oss_timer_continue(dp->timer); return 0; case TMR_TEMPO: if (dp->seq_mode) return snd_seq_oss_timer_tempo(dp->timer, q->t.time); return 0; } return -EINVAL; } /* local events: mode1 and 2 */ static int local_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev) { return -EINVAL; } /* * process note-on event for OSS synth * three different modes are available: * - SNDRV_SEQ_OSS_PROCESS_EVENTS (for one-voice per channel mode) * Accept note 255 as volume change. * - SNDRV_SEQ_OSS_PASS_EVENTS * Pass all events to lowlevel driver anyway * - SNDRV_SEQ_OSS_PROCESS_KEYPRESS (mostly for Emu8000) * Use key-pressure if note >= 128 */ static int note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev) { struct seq_oss_synthinfo *info; info = snd_seq_oss_synth_info(dp, dev); if (!info) return -ENXIO; switch (info->arg.event_passing) { case SNDRV_SEQ_OSS_PROCESS_EVENTS: if (! info->ch || ch < 0 || ch >= info->nr_voices) { /* pass directly */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev); } ch = array_index_nospec(ch, info->nr_voices); if (note == 255 && info->ch[ch].note >= 0) { /* volume control */ int type; //if (! vel) /* set volume to zero -- note off */ // type = SNDRV_SEQ_EVENT_NOTEOFF; //else if (info->ch[ch].vel) /* sample already started -- volume change */ type = SNDRV_SEQ_EVENT_KEYPRESS; else /* sample not started -- start now */ type = SNDRV_SEQ_EVENT_NOTEON; info->ch[ch].vel = vel; return set_note_event(dp, dev, type, ch, info->ch[ch].note, vel, ev); } else if (note >= 128) return -EINVAL; /* invalid */ if (note != info->ch[ch].note && info->ch[ch].note >= 0) /* note changed - note off at beginning */ set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, info->ch[ch].note, 0, ev); /* set current status */ info->ch[ch].note = note; info->ch[ch].vel = vel; if (vel) /* non-zero velocity - start the note now */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev); return -EINVAL; case SNDRV_SEQ_OSS_PASS_EVENTS: /* pass the event anyway */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev); case SNDRV_SEQ_OSS_PROCESS_KEYPRESS: if (note >= 128) /* key pressure: shifted by 128 */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_KEYPRESS, ch, note - 128, vel, ev); else /* normal note-on event */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev); } return -EINVAL; } /* * process note-off event for OSS synth */ static int note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev) { struct seq_oss_synthinfo *info; info = snd_seq_oss_synth_info(dp, dev); if (!info) return -ENXIO; switch (info->arg.event_passing) { case SNDRV_SEQ_OSS_PROCESS_EVENTS: if (! info->ch || ch < 0 || ch >= info->nr_voices) { /* pass directly */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev); } ch = array_index_nospec(ch, info->nr_voices); if (info->ch[ch].note >= 0) { note = info->ch[ch].note; info->ch[ch].vel = 0; info->ch[ch].note = -1; return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev); } return -EINVAL; /* invalid */ case SNDRV_SEQ_OSS_PASS_EVENTS: case SNDRV_SEQ_OSS_PROCESS_KEYPRESS: /* pass the event anyway */ return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev); } return -EINVAL; } /* * create a note event */ static int set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev) { if (!snd_seq_oss_synth_info(dp, dev)) return -ENXIO; ev->type = type; snd_seq_oss_synth_addr(dp, dev, ev); ev->data.note.channel = ch; ev->data.note.note = note; ev->data.note.velocity = vel; return 0; } /* * create a control event */ static int set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev) { if (!snd_seq_oss_synth_info(dp, dev)) return -ENXIO; ev->type = type; snd_seq_oss_synth_addr(dp, dev, ev); ev->data.control.channel = ch; ev->data.control.param = param; ev->data.control.value = val; return 0; } /* * create an echo event */ static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev) { ev->type = SNDRV_SEQ_EVENT_ECHO; /* echo back to itself */ snd_seq_oss_fill_addr(dp, ev, dp->addr.client, dp->addr.port); memcpy(&ev->data, rec, LONG_EVENT_SIZE); return 0; } /* * event input callback from ALSA sequencer: * the echo event is processed here. */ int snd_seq_oss_event_input(struct snd_seq_event *ev, int direct, void *private_data, int atomic, int hop) { struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data; union evrec *rec; if (ev->type != SNDRV_SEQ_EVENT_ECHO) return snd_seq_oss_midi_input(ev, direct, private_data); if (ev->source.client != dp->cseq) return 0; /* ignored */ rec = (union evrec*)&ev->data; if (rec->s.code == SEQ_SYNCTIMER) { /* sync echo back */ snd_seq_oss_writeq_wakeup(dp->writeq, rec->t.time); } else { /* echo back event */ if (dp->readq == NULL) return 0; snd_seq_oss_readq_put_event(dp->readq, rec); } return 0; }
linux-master
sound/core/seq/oss/seq_oss_event.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * OSS compatible i/o control * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_device.h" #include "seq_oss_readq.h" #include "seq_oss_writeq.h" #include "seq_oss_timer.h" #include "seq_oss_synth.h" #include "seq_oss_midi.h" #include "seq_oss_event.h" static int snd_seq_oss_synth_info_user(struct seq_oss_devinfo *dp, void __user *arg) { struct synth_info info; if (copy_from_user(&info, arg, sizeof(info))) return -EFAULT; if (snd_seq_oss_synth_make_info(dp, info.device, &info) < 0) return -EINVAL; if (copy_to_user(arg, &info, sizeof(info))) return -EFAULT; return 0; } static int snd_seq_oss_midi_info_user(struct seq_oss_devinfo *dp, void __user *arg) { struct midi_info info; if (copy_from_user(&info, arg, sizeof(info))) return -EFAULT; if (snd_seq_oss_midi_make_info(dp, info.device, &info) < 0) return -EINVAL; if (copy_to_user(arg, &info, sizeof(info))) return -EFAULT; return 0; } static int snd_seq_oss_oob_user(struct seq_oss_devinfo *dp, void __user *arg) { unsigned char ev[8]; struct snd_seq_event tmpev; if (copy_from_user(ev, arg, 8)) return -EFAULT; memset(&tmpev, 0, sizeof(tmpev)); snd_seq_oss_fill_addr(dp, &tmpev, dp->addr.client, dp->addr.port); tmpev.time.tick = 0; if (! snd_seq_oss_process_event(dp, (union evrec *)ev, &tmpev)) { snd_seq_oss_dispatch(dp, &tmpev, 0, 0); } return 0; } int snd_seq_oss_ioctl(struct seq_oss_devinfo *dp, unsigned int cmd, unsigned long carg) { int dev, val; void __user *arg = (void __user *)carg; int __user *p = arg; switch (cmd) { case SNDCTL_TMR_TIMEBASE: case SNDCTL_TMR_TEMPO: case SNDCTL_TMR_START: case SNDCTL_TMR_STOP: case SNDCTL_TMR_CONTINUE: case SNDCTL_TMR_METRONOME: case SNDCTL_TMR_SOURCE: case SNDCTL_TMR_SELECT: case SNDCTL_SEQ_CTRLRATE: return snd_seq_oss_timer_ioctl(dp->timer, cmd, arg); case SNDCTL_SEQ_PANIC: snd_seq_oss_reset(dp); return -EINVAL; case SNDCTL_SEQ_SYNC: if (! is_write_mode(dp->file_mode) || dp->writeq == NULL) return 0; while (snd_seq_oss_writeq_sync(dp->writeq)) ; if (signal_pending(current)) return -ERESTARTSYS; return 0; case SNDCTL_SEQ_RESET: snd_seq_oss_reset(dp); return 0; case SNDCTL_SEQ_TESTMIDI: if (get_user(dev, p)) return -EFAULT; return snd_seq_oss_midi_open(dp, dev, dp->file_mode); case SNDCTL_SEQ_GETINCOUNT: if (dp->readq == NULL || ! is_read_mode(dp->file_mode)) return 0; return put_user(dp->readq->qlen, p) ? -EFAULT : 0; case SNDCTL_SEQ_GETOUTCOUNT: if (! is_write_mode(dp->file_mode) || dp->writeq == NULL) return 0; return put_user(snd_seq_oss_writeq_get_free_size(dp->writeq), p) ? -EFAULT : 0; case SNDCTL_SEQ_GETTIME: return put_user(snd_seq_oss_timer_cur_tick(dp->timer), p) ? -EFAULT : 0; case SNDCTL_SEQ_RESETSAMPLES: if (get_user(dev, p)) return -EFAULT; return snd_seq_oss_synth_ioctl(dp, dev, cmd, carg); case SNDCTL_SEQ_NRSYNTHS: return put_user(dp->max_synthdev, p) ? -EFAULT : 0; case SNDCTL_SEQ_NRMIDIS: return put_user(dp->max_mididev, p) ? -EFAULT : 0; case SNDCTL_SYNTH_MEMAVL: if (get_user(dev, p)) return -EFAULT; val = snd_seq_oss_synth_ioctl(dp, dev, cmd, carg); return put_user(val, p) ? -EFAULT : 0; case SNDCTL_FM_4OP_ENABLE: if (get_user(dev, p)) return -EFAULT; snd_seq_oss_synth_ioctl(dp, dev, cmd, carg); return 0; case SNDCTL_SYNTH_INFO: case SNDCTL_SYNTH_ID: return snd_seq_oss_synth_info_user(dp, arg); case SNDCTL_SEQ_OUTOFBAND: return snd_seq_oss_oob_user(dp, arg); case SNDCTL_MIDI_INFO: return snd_seq_oss_midi_info_user(dp, arg); case SNDCTL_SEQ_THRESHOLD: if (! is_write_mode(dp->file_mode)) return 0; if (get_user(val, p)) return -EFAULT; if (val < 1) val = 1; if (val >= dp->writeq->maxlen) val = dp->writeq->maxlen - 1; snd_seq_oss_writeq_set_output(dp->writeq, val); return 0; case SNDCTL_MIDI_PRETIME: if (dp->readq == NULL || !is_read_mode(dp->file_mode)) return 0; if (get_user(val, p)) return -EFAULT; if (val <= 0) val = -1; else val = (HZ * val) / 10; dp->readq->pre_event_timeout = val; return put_user(val, p) ? -EFAULT : 0; default: if (! is_write_mode(dp->file_mode)) return -EIO; return snd_seq_oss_synth_ioctl(dp, 0, cmd, carg); } return 0; }
linux-master
sound/core/seq/oss/seq_oss_ioctl.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * open/close and reset interface * * Copyright (C) 1998-1999 Takashi Iwai <[email protected]> */ #include "seq_oss_device.h" #include "seq_oss_synth.h" #include "seq_oss_midi.h" #include "seq_oss_writeq.h" #include "seq_oss_readq.h" #include "seq_oss_timer.h" #include "seq_oss_event.h" #include <linux/init.h> #include <linux/export.h> #include <linux/moduleparam.h> #include <linux/slab.h> #include <linux/workqueue.h> /* * common variables */ static int maxqlen = SNDRV_SEQ_OSS_MAX_QLEN; module_param(maxqlen, int, 0444); MODULE_PARM_DESC(maxqlen, "maximum queue length"); static int system_client = -1; /* ALSA sequencer client number */ static int system_port = -1; static int num_clients; static struct seq_oss_devinfo *client_table[SNDRV_SEQ_OSS_MAX_CLIENTS]; /* * prototypes */ static int receive_announce(struct snd_seq_event *ev, int direct, void *private, int atomic, int hop); static int translate_mode(struct file *file); static int create_port(struct seq_oss_devinfo *dp); static int delete_port(struct seq_oss_devinfo *dp); static int alloc_seq_queue(struct seq_oss_devinfo *dp); static int delete_seq_queue(int queue); static void free_devinfo(void *private); #define call_ctl(type,rec) snd_seq_kernel_client_ctl(system_client, type, rec) /* call snd_seq_oss_midi_lookup_ports() asynchronously */ static void async_call_lookup_ports(struct work_struct *work) { snd_seq_oss_midi_lookup_ports(system_client); } static DECLARE_WORK(async_lookup_work, async_call_lookup_ports); /* * create sequencer client for OSS sequencer */ int __init snd_seq_oss_create_client(void) { int rc; struct snd_seq_port_info *port; struct snd_seq_port_callback port_callback; port = kzalloc(sizeof(*port), GFP_KERNEL); if (!port) { rc = -ENOMEM; goto __error; } /* create ALSA client */ rc = snd_seq_create_kernel_client(NULL, SNDRV_SEQ_CLIENT_OSS, "OSS sequencer"); if (rc < 0) goto __error; system_client = rc; /* create announcement receiver port */ strcpy(port->name, "Receiver"); port->addr.client = system_client; port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* receive only */ port->type = 0; memset(&port_callback, 0, sizeof(port_callback)); /* don't set port_callback.owner here. otherwise the module counter * is incremented and we can no longer release the module.. */ port_callback.event_input = receive_announce; port->kernel = &port_callback; if (call_ctl(SNDRV_SEQ_IOCTL_CREATE_PORT, port) >= 0) { struct snd_seq_port_subscribe subs; system_port = port->addr.port; memset(&subs, 0, sizeof(subs)); subs.sender.client = SNDRV_SEQ_CLIENT_SYSTEM; subs.sender.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE; subs.dest.client = system_client; subs.dest.port = system_port; call_ctl(SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs); } rc = 0; /* look up midi devices */ schedule_work(&async_lookup_work); __error: kfree(port); return rc; } /* * receive annoucement from system port, and check the midi device */ static int receive_announce(struct snd_seq_event *ev, int direct, void *private, int atomic, int hop) { struct snd_seq_port_info pinfo; if (atomic) return 0; /* it must not happen */ switch (ev->type) { case SNDRV_SEQ_EVENT_PORT_START: case SNDRV_SEQ_EVENT_PORT_CHANGE: if (ev->data.addr.client == system_client) break; /* ignore myself */ memset(&pinfo, 0, sizeof(pinfo)); pinfo.addr = ev->data.addr; if (call_ctl(SNDRV_SEQ_IOCTL_GET_PORT_INFO, &pinfo) >= 0) snd_seq_oss_midi_check_new_port(&pinfo); break; case SNDRV_SEQ_EVENT_PORT_EXIT: if (ev->data.addr.client == system_client) break; /* ignore myself */ snd_seq_oss_midi_check_exit_port(ev->data.addr.client, ev->data.addr.port); break; } return 0; } /* * delete OSS sequencer client */ int snd_seq_oss_delete_client(void) { cancel_work_sync(&async_lookup_work); if (system_client >= 0) snd_seq_delete_kernel_client(system_client); snd_seq_oss_midi_clear_all(); return 0; } /* * open sequencer device */ int snd_seq_oss_open(struct file *file, int level) { int i, rc; struct seq_oss_devinfo *dp; dp = kzalloc(sizeof(*dp), GFP_KERNEL); if (!dp) return -ENOMEM; dp->cseq = system_client; dp->port = -1; dp->queue = -1; for (i = 0; i < SNDRV_SEQ_OSS_MAX_CLIENTS; i++) { if (client_table[i] == NULL) break; } dp->index = i; if (i >= SNDRV_SEQ_OSS_MAX_CLIENTS) { pr_debug("ALSA: seq_oss: too many applications\n"); rc = -ENOMEM; goto _error; } /* look up synth and midi devices */ snd_seq_oss_synth_setup(dp); snd_seq_oss_midi_setup(dp); if (dp->synth_opened == 0 && dp->max_mididev == 0) { /* pr_err("ALSA: seq_oss: no device found\n"); */ rc = -ENODEV; goto _error; } /* create port */ rc = create_port(dp); if (rc < 0) { pr_err("ALSA: seq_oss: can't create port\n"); goto _error; } /* allocate queue */ rc = alloc_seq_queue(dp); if (rc < 0) goto _error; /* set address */ dp->addr.client = dp->cseq; dp->addr.port = dp->port; /*dp->addr.queue = dp->queue;*/ /*dp->addr.channel = 0;*/ dp->seq_mode = level; /* set up file mode */ dp->file_mode = translate_mode(file); /* initialize read queue */ if (is_read_mode(dp->file_mode)) { dp->readq = snd_seq_oss_readq_new(dp, maxqlen); if (!dp->readq) { rc = -ENOMEM; goto _error; } } /* initialize write queue */ if (is_write_mode(dp->file_mode)) { dp->writeq = snd_seq_oss_writeq_new(dp, maxqlen); if (!dp->writeq) { rc = -ENOMEM; goto _error; } } /* initialize timer */ dp->timer = snd_seq_oss_timer_new(dp); if (!dp->timer) { pr_err("ALSA: seq_oss: can't alloc timer\n"); rc = -ENOMEM; goto _error; } /* set private data pointer */ file->private_data = dp; /* set up for mode2 */ if (level == SNDRV_SEQ_OSS_MODE_MUSIC) snd_seq_oss_synth_setup_midi(dp); else if (is_read_mode(dp->file_mode)) snd_seq_oss_midi_open_all(dp, SNDRV_SEQ_OSS_FILE_READ); client_table[dp->index] = dp; num_clients++; return 0; _error: snd_seq_oss_synth_cleanup(dp); snd_seq_oss_midi_cleanup(dp); delete_seq_queue(dp->queue); delete_port(dp); return rc; } /* * translate file flags to private mode */ static int translate_mode(struct file *file) { int file_mode = 0; if ((file->f_flags & O_ACCMODE) != O_RDONLY) file_mode |= SNDRV_SEQ_OSS_FILE_WRITE; if ((file->f_flags & O_ACCMODE) != O_WRONLY) file_mode |= SNDRV_SEQ_OSS_FILE_READ; if (file->f_flags & O_NONBLOCK) file_mode |= SNDRV_SEQ_OSS_FILE_NONBLOCK; return file_mode; } /* * create sequencer port */ static int create_port(struct seq_oss_devinfo *dp) { int rc; struct snd_seq_port_info port; struct snd_seq_port_callback callback; memset(&port, 0, sizeof(port)); port.addr.client = dp->cseq; sprintf(port.name, "Sequencer-%d", dp->index); port.capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_WRITE; /* no subscription */ port.type = SNDRV_SEQ_PORT_TYPE_SPECIFIC; port.midi_channels = 128; port.synth_voices = 128; memset(&callback, 0, sizeof(callback)); callback.owner = THIS_MODULE; callback.private_data = dp; callback.event_input = snd_seq_oss_event_input; callback.private_free = free_devinfo; port.kernel = &callback; rc = call_ctl(SNDRV_SEQ_IOCTL_CREATE_PORT, &port); if (rc < 0) return rc; dp->port = port.addr.port; return 0; } /* * delete ALSA port */ static int delete_port(struct seq_oss_devinfo *dp) { if (dp->port < 0) { kfree(dp); return 0; } return snd_seq_event_port_detach(dp->cseq, dp->port); } /* * allocate a queue */ static int alloc_seq_queue(struct seq_oss_devinfo *dp) { struct snd_seq_queue_info qinfo; int rc; memset(&qinfo, 0, sizeof(qinfo)); qinfo.owner = system_client; qinfo.locked = 1; strcpy(qinfo.name, "OSS Sequencer Emulation"); rc = call_ctl(SNDRV_SEQ_IOCTL_CREATE_QUEUE, &qinfo); if (rc < 0) return rc; dp->queue = qinfo.queue; return 0; } /* * release queue */ static int delete_seq_queue(int queue) { struct snd_seq_queue_info qinfo; int rc; if (queue < 0) return 0; memset(&qinfo, 0, sizeof(qinfo)); qinfo.queue = queue; rc = call_ctl(SNDRV_SEQ_IOCTL_DELETE_QUEUE, &qinfo); if (rc < 0) pr_err("ALSA: seq_oss: unable to delete queue %d (%d)\n", queue, rc); return rc; } /* * free device informations - private_free callback of port */ static void free_devinfo(void *private) { struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private; snd_seq_oss_timer_delete(dp->timer); snd_seq_oss_writeq_delete(dp->writeq); snd_seq_oss_readq_delete(dp->readq); kfree(dp); } /* * close sequencer device */ void snd_seq_oss_release(struct seq_oss_devinfo *dp) { int queue; client_table[dp->index] = NULL; num_clients--; snd_seq_oss_reset(dp); snd_seq_oss_synth_cleanup(dp); snd_seq_oss_midi_cleanup(dp); /* clear slot */ queue = dp->queue; if (dp->port >= 0) delete_port(dp); delete_seq_queue(queue); } /* * reset sequencer devices */ void snd_seq_oss_reset(struct seq_oss_devinfo *dp) { int i; /* reset all synth devices */ for (i = 0; i < dp->max_synthdev; i++) snd_seq_oss_synth_reset(dp, i); /* reset all midi devices */ if (dp->seq_mode != SNDRV_SEQ_OSS_MODE_MUSIC) { for (i = 0; i < dp->max_mididev; i++) snd_seq_oss_midi_reset(dp, i); } /* remove queues */ if (dp->readq) snd_seq_oss_readq_clear(dp->readq); if (dp->writeq) snd_seq_oss_writeq_clear(dp->writeq); /* reset timer */ snd_seq_oss_timer_stop(dp->timer); } #ifdef CONFIG_SND_PROC_FS /* * misc. functions for proc interface */ char * enabled_str(int bool) { return bool ? "enabled" : "disabled"; } static const char * filemode_str(int val) { static const char * const str[] = { "none", "read", "write", "read/write", }; return str[val & SNDRV_SEQ_OSS_FILE_ACMODE]; } /* * proc interface */ void snd_seq_oss_system_info_read(struct snd_info_buffer *buf) { int i; struct seq_oss_devinfo *dp; snd_iprintf(buf, "ALSA client number %d\n", system_client); snd_iprintf(buf, "ALSA receiver port %d\n", system_port); snd_iprintf(buf, "\nNumber of applications: %d\n", num_clients); for (i = 0; i < num_clients; i++) { snd_iprintf(buf, "\nApplication %d: ", i); dp = client_table[i]; if (!dp) { snd_iprintf(buf, "*empty*\n"); continue; } snd_iprintf(buf, "port %d : queue %d\n", dp->port, dp->queue); snd_iprintf(buf, " sequencer mode = %s : file open mode = %s\n", (dp->seq_mode ? "music" : "synth"), filemode_str(dp->file_mode)); if (dp->seq_mode) snd_iprintf(buf, " timer tempo = %d, timebase = %d\n", dp->timer->oss_tempo, dp->timer->oss_timebase); snd_iprintf(buf, " max queue length %d\n", maxqlen); if (is_read_mode(dp->file_mode) && dp->readq) snd_seq_oss_readq_info_read(dp->readq, buf); } } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/oss/seq_oss_init.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS compatible sequencer driver * * synth device handlers * * Copyright (C) 1998,99 Takashi Iwai <[email protected]> */ #include "seq_oss_synth.h" #include "seq_oss_midi.h" #include "../seq_lock.h" #include <linux/init.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/nospec.h> /* * constants */ #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30 #define MAX_SYSEX_BUFLEN 128 /* * definition of synth info records */ /* sysex buffer */ struct seq_oss_synth_sysex { int len; int skip; unsigned char buf[MAX_SYSEX_BUFLEN]; }; /* synth info */ struct seq_oss_synth { int seq_device; /* for synth_info */ int synth_type; int synth_subtype; int nr_voices; char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME]; struct snd_seq_oss_callback oper; int opened; void *private_data; snd_use_lock_t use_lock; }; /* * device table */ static int max_synth_devs; static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS]; static struct seq_oss_synth midi_synth_dev = { .seq_device = -1, .synth_type = SYNTH_TYPE_MIDI, .synth_subtype = 0, .nr_voices = 16, .name = "MIDI", }; static DEFINE_SPINLOCK(register_lock); /* * prototypes */ static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev); static void reset_channels(struct seq_oss_synthinfo *info); /* * global initialization */ void __init snd_seq_oss_synth_init(void) { snd_use_lock_init(&midi_synth_dev.use_lock); } /* * registration of the synth device */ int snd_seq_oss_synth_probe(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); int i; struct seq_oss_synth *rec; struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev); unsigned long flags; rec = kzalloc(sizeof(*rec), GFP_KERNEL); if (!rec) return -ENOMEM; rec->seq_device = -1; rec->synth_type = reg->type; rec->synth_subtype = reg->subtype; rec->nr_voices = reg->nvoices; rec->oper = reg->oper; rec->private_data = reg->private_data; rec->opened = 0; snd_use_lock_init(&rec->use_lock); /* copy and truncate the name of synth device */ strscpy(rec->name, dev->name, sizeof(rec->name)); /* registration */ spin_lock_irqsave(&register_lock, flags); for (i = 0; i < max_synth_devs; i++) { if (synth_devs[i] == NULL) break; } if (i >= max_synth_devs) { if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) { spin_unlock_irqrestore(&register_lock, flags); pr_err("ALSA: seq_oss: no more synth slot\n"); kfree(rec); return -ENOMEM; } max_synth_devs++; } rec->seq_device = i; synth_devs[i] = rec; spin_unlock_irqrestore(&register_lock, flags); dev->driver_data = rec; #ifdef SNDRV_OSS_INFO_DEV_SYNTH if (i < SNDRV_CARDS) snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name); #endif return 0; } int snd_seq_oss_synth_remove(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); int index; struct seq_oss_synth *rec = dev->driver_data; unsigned long flags; spin_lock_irqsave(&register_lock, flags); for (index = 0; index < max_synth_devs; index++) { if (synth_devs[index] == rec) break; } if (index >= max_synth_devs) { spin_unlock_irqrestore(&register_lock, flags); pr_err("ALSA: seq_oss: can't unregister synth\n"); return -EINVAL; } synth_devs[index] = NULL; if (index == max_synth_devs - 1) { for (index--; index >= 0; index--) { if (synth_devs[index]) break; } max_synth_devs = index + 1; } spin_unlock_irqrestore(&register_lock, flags); #ifdef SNDRV_OSS_INFO_DEV_SYNTH if (rec->seq_device < SNDRV_CARDS) snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device); #endif snd_use_lock_sync(&rec->use_lock); kfree(rec); return 0; } /* */ static struct seq_oss_synth * get_sdev(int dev) { struct seq_oss_synth *rec; unsigned long flags; spin_lock_irqsave(&register_lock, flags); rec = synth_devs[dev]; if (rec) snd_use_lock_use(&rec->use_lock); spin_unlock_irqrestore(&register_lock, flags); return rec; } /* * set up synth tables */ void snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp) { int i; struct seq_oss_synth *rec; struct seq_oss_synthinfo *info; dp->max_synthdev = max_synth_devs; dp->synth_opened = 0; memset(dp->synths, 0, sizeof(dp->synths)); for (i = 0; i < dp->max_synthdev; i++) { rec = get_sdev(i); if (rec == NULL) continue; if (rec->oper.open == NULL || rec->oper.close == NULL) { snd_use_lock_free(&rec->use_lock); continue; } info = &dp->synths[i]; info->arg.app_index = dp->port; info->arg.file_mode = dp->file_mode; info->arg.seq_mode = dp->seq_mode; if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS; else info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS; info->opened = 0; if (!try_module_get(rec->oper.owner)) { snd_use_lock_free(&rec->use_lock); continue; } if (rec->oper.open(&info->arg, rec->private_data) < 0) { module_put(rec->oper.owner); snd_use_lock_free(&rec->use_lock); continue; } info->nr_voices = rec->nr_voices; if (info->nr_voices > 0) { info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL); if (!info->ch) { rec->oper.close(&info->arg); module_put(rec->oper.owner); snd_use_lock_free(&rec->use_lock); continue; } reset_channels(info); } info->opened++; rec->opened++; dp->synth_opened++; snd_use_lock_free(&rec->use_lock); } } /* * set up synth tables for MIDI emulation - /dev/music mode only */ void snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp) { int i; if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) return; for (i = 0; i < dp->max_mididev; i++) { struct seq_oss_synthinfo *info; info = &dp->synths[dp->max_synthdev]; if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0) continue; info->arg.app_index = dp->port; info->arg.file_mode = dp->file_mode; info->arg.seq_mode = dp->seq_mode; info->arg.private_data = info; info->is_midi = 1; info->midi_mapped = i; info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS; snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr); info->opened = 1; midi_synth_dev.opened++; dp->max_synthdev++; if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) break; } } /* * clean up synth tables */ void snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp) { int i; struct seq_oss_synth *rec; struct seq_oss_synthinfo *info; if (snd_BUG_ON(dp->max_synthdev > SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)) return; for (i = 0; i < dp->max_synthdev; i++) { info = &dp->synths[i]; if (! info->opened) continue; if (info->is_midi) { if (midi_synth_dev.opened > 0) { snd_seq_oss_midi_close(dp, info->midi_mapped); midi_synth_dev.opened--; } } else { rec = get_sdev(i); if (rec == NULL) continue; if (rec->opened > 0) { rec->oper.close(&info->arg); module_put(rec->oper.owner); rec->opened = 0; } snd_use_lock_free(&rec->use_lock); } kfree(info->sysex); info->sysex = NULL; kfree(info->ch); info->ch = NULL; } dp->synth_opened = 0; dp->max_synthdev = 0; } static struct seq_oss_synthinfo * get_synthinfo_nospec(struct seq_oss_devinfo *dp, int dev) { if (dev < 0 || dev >= dp->max_synthdev) return NULL; dev = array_index_nospec(dev, SNDRV_SEQ_OSS_MAX_SYNTH_DEVS); return &dp->synths[dev]; } /* * return synth device information pointer */ static struct seq_oss_synth * get_synthdev(struct seq_oss_devinfo *dp, int dev) { struct seq_oss_synth *rec; struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev); if (!info) return NULL; if (!info->opened) return NULL; if (info->is_midi) { rec = &midi_synth_dev; snd_use_lock_use(&rec->use_lock); } else { rec = get_sdev(dev); if (!rec) return NULL; } if (! rec->opened) { snd_use_lock_free(&rec->use_lock); return NULL; } return rec; } /* * reset note and velocity on each channel. */ static void reset_channels(struct seq_oss_synthinfo *info) { int i; if (info->ch == NULL || ! info->nr_voices) return; for (i = 0; i < info->nr_voices; i++) { info->ch[i].note = -1; info->ch[i].vel = 0; } } /* * reset synth device: * call reset callback. if no callback is defined, send a heartbeat * event to the corresponding port. */ void snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev) { struct seq_oss_synth *rec; struct seq_oss_synthinfo *info; info = get_synthinfo_nospec(dp, dev); if (!info || !info->opened) return; if (info->sysex) info->sysex->len = 0; /* reset sysex */ reset_channels(info); if (info->is_midi) { if (midi_synth_dev.opened <= 0) return; snd_seq_oss_midi_reset(dp, info->midi_mapped); /* reopen the device */ snd_seq_oss_midi_close(dp, dev); if (snd_seq_oss_midi_open(dp, info->midi_mapped, dp->file_mode) < 0) { midi_synth_dev.opened--; info->opened = 0; kfree(info->sysex); info->sysex = NULL; kfree(info->ch); info->ch = NULL; } return; } rec = get_sdev(dev); if (rec == NULL) return; if (rec->oper.reset) { rec->oper.reset(&info->arg); } else { struct snd_seq_event ev; memset(&ev, 0, sizeof(ev)); snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client, info->arg.addr.port); ev.type = SNDRV_SEQ_EVENT_RESET; snd_seq_oss_dispatch(dp, &ev, 0, 0); } snd_use_lock_free(&rec->use_lock); } /* * load a patch record: * call load_patch callback function */ int snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt, const char __user *buf, int p, int c) { struct seq_oss_synth *rec; struct seq_oss_synthinfo *info; int rc; info = get_synthinfo_nospec(dp, dev); if (!info) return -ENXIO; if (info->is_midi) return 0; rec = get_synthdev(dp, dev); if (!rec) return -ENXIO; if (rec->oper.load_patch == NULL) rc = -ENXIO; else rc = rec->oper.load_patch(&info->arg, fmt, buf, p, c); snd_use_lock_free(&rec->use_lock); return rc; } /* * check if the device is valid synth device and return the synth info */ struct seq_oss_synthinfo * snd_seq_oss_synth_info(struct seq_oss_devinfo *dp, int dev) { struct seq_oss_synth *rec; rec = get_synthdev(dp, dev); if (rec) { snd_use_lock_free(&rec->use_lock); return get_synthinfo_nospec(dp, dev); } return NULL; } /* * receive OSS 6 byte sysex packet: * the full sysex message will be sent if it reaches to the end of data * (0xff). */ int snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev) { int i, send; unsigned char *dest; struct seq_oss_synth_sysex *sysex; struct seq_oss_synthinfo *info; info = snd_seq_oss_synth_info(dp, dev); if (!info) return -ENXIO; sysex = info->sysex; if (sysex == NULL) { sysex = kzalloc(sizeof(*sysex), GFP_KERNEL); if (sysex == NULL) return -ENOMEM; info->sysex = sysex; } send = 0; dest = sysex->buf + sysex->len; /* copy 6 byte packet to the buffer */ for (i = 0; i < 6; i++) { if (buf[i] == 0xff) { send = 1; break; } dest[i] = buf[i]; sysex->len++; if (sysex->len >= MAX_SYSEX_BUFLEN) { sysex->len = 0; sysex->skip = 1; break; } } if (sysex->len && send) { if (sysex->skip) { sysex->skip = 0; sysex->len = 0; return -EINVAL; /* skip */ } /* copy the data to event record and send it */ ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE; if (snd_seq_oss_synth_addr(dp, dev, ev)) return -EINVAL; ev->data.ext.len = sysex->len; ev->data.ext.ptr = sysex->buf; sysex->len = 0; return 0; } return -EINVAL; /* skip */ } /* * fill the event source/destination addresses */ int snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev) { struct seq_oss_synthinfo *info = snd_seq_oss_synth_info(dp, dev); if (!info) return -EINVAL; snd_seq_oss_fill_addr(dp, ev, info->arg.addr.client, info->arg.addr.port); return 0; } /* * OSS compatible ioctl */ int snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr) { struct seq_oss_synth *rec; struct seq_oss_synthinfo *info; int rc; info = get_synthinfo_nospec(dp, dev); if (!info || info->is_midi) return -ENXIO; rec = get_synthdev(dp, dev); if (!rec) return -ENXIO; if (rec->oper.ioctl == NULL) rc = -ENXIO; else rc = rec->oper.ioctl(&info->arg, cmd, addr); snd_use_lock_free(&rec->use_lock); return rc; } /* * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME */ int snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev) { struct seq_oss_synthinfo *info; info = snd_seq_oss_synth_info(dp, dev); if (!info || info->is_midi) return -ENXIO; ev->type = SNDRV_SEQ_EVENT_OSS; memcpy(ev->data.raw8.d, data, 8); return snd_seq_oss_synth_addr(dp, dev, ev); } /* * create OSS compatible synth_info record */ int snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf) { struct seq_oss_synth *rec; struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev); if (!info) return -ENXIO; if (info->is_midi) { struct midi_info minf; if (snd_seq_oss_midi_make_info(dp, info->midi_mapped, &minf)) return -ENXIO; inf->synth_type = SYNTH_TYPE_MIDI; inf->synth_subtype = 0; inf->nr_voices = 16; inf->device = dev; strscpy(inf->name, minf.name, sizeof(inf->name)); } else { rec = get_synthdev(dp, dev); if (!rec) return -ENXIO; inf->synth_type = rec->synth_type; inf->synth_subtype = rec->synth_subtype; inf->nr_voices = rec->nr_voices; inf->device = dev; strscpy(inf->name, rec->name, sizeof(inf->name)); snd_use_lock_free(&rec->use_lock); } return 0; } #ifdef CONFIG_SND_PROC_FS /* * proc interface */ void snd_seq_oss_synth_info_read(struct snd_info_buffer *buf) { int i; struct seq_oss_synth *rec; snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs); for (i = 0; i < max_synth_devs; i++) { snd_iprintf(buf, "\nsynth %d: ", i); rec = get_sdev(i); if (rec == NULL) { snd_iprintf(buf, "*empty*\n"); continue; } snd_iprintf(buf, "[%s]\n", rec->name); snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n", rec->synth_type, rec->synth_subtype, rec->nr_voices); snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n", enabled_str((long)rec->oper.ioctl), enabled_str((long)rec->oper.load_patch)); snd_use_lock_free(&rec->use_lock); } } #endif /* CONFIG_SND_PROC_FS */
linux-master
sound/core/seq/oss/seq_oss_synth.c
/* * PCM I/O Plug-In Interface * Copyright (c) 1999 by Jaroslav Kysela <[email protected]> * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include <linux/time.h> #include <sound/core.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include "pcm_plugin.h" #define pcm_write(plug,buf,count) snd_pcm_oss_write3(plug,buf,count,1) #define pcm_writev(plug,vec,count) snd_pcm_oss_writev3(plug,vec,count) #define pcm_read(plug,buf,count) snd_pcm_oss_read3(plug,buf,count,1) #define pcm_readv(plug,vec,count) snd_pcm_oss_readv3(plug,vec,count) /* * Basic io plugin */ static snd_pcm_sframes_t io_playback_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { if (snd_BUG_ON(!plugin)) return -ENXIO; if (snd_BUG_ON(!src_channels)) return -ENXIO; if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) { return pcm_write(plugin->plug, src_channels->area.addr, frames); } else { int channel, channels = plugin->dst_format.channels; void **bufs = (void**)plugin->extra_data; if (snd_BUG_ON(!bufs)) return -ENXIO; for (channel = 0; channel < channels; channel++) { if (src_channels[channel].enabled) bufs[channel] = src_channels[channel].area.addr; else bufs[channel] = NULL; } return pcm_writev(plugin->plug, bufs, frames); } } static snd_pcm_sframes_t io_capture_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { if (snd_BUG_ON(!plugin)) return -ENXIO; if (snd_BUG_ON(!dst_channels)) return -ENXIO; if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) { return pcm_read(plugin->plug, dst_channels->area.addr, frames); } else { int channel, channels = plugin->dst_format.channels; void **bufs = (void**)plugin->extra_data; if (snd_BUG_ON(!bufs)) return -ENXIO; for (channel = 0; channel < channels; channel++) { if (dst_channels[channel].enabled) bufs[channel] = dst_channels[channel].area.addr; else bufs[channel] = NULL; } return pcm_readv(plugin->plug, bufs, frames); } return 0; } static snd_pcm_sframes_t io_src_channels(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames, struct snd_pcm_plugin_channel **channels) { int err; unsigned int channel; struct snd_pcm_plugin_channel *v; err = snd_pcm_plugin_client_channels(plugin, frames, &v); if (err < 0) return err; *channels = v; if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) { for (channel = 0; channel < plugin->src_format.channels; ++channel, ++v) v->wanted = 1; } return frames; } int snd_pcm_plugin_build_io(struct snd_pcm_substream *plug, struct snd_pcm_hw_params *params, struct snd_pcm_plugin **r_plugin) { int err; struct snd_pcm_plugin_format format; struct snd_pcm_plugin *plugin; if (snd_BUG_ON(!r_plugin)) return -ENXIO; *r_plugin = NULL; if (snd_BUG_ON(!plug || !params)) return -ENXIO; format.format = params_format(params); format.rate = params_rate(params); format.channels = params_channels(params); err = snd_pcm_plugin_build(plug, "I/O io", &format, &format, sizeof(void *) * format.channels, &plugin); if (err < 0) return err; plugin->access = params_access(params); if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) { plugin->transfer = io_playback_transfer; if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) plugin->client_channels = io_src_channels; } else { plugin->transfer = io_capture_transfer; } *r_plugin = plugin; return 0; }
linux-master
sound/core/oss/io.c
/* * Rate conversion Plug-In * Copyright (c) 1999 by Jaroslav Kysela <[email protected]> * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include <linux/time.h> #include <sound/core.h> #include <sound/pcm.h> #include "pcm_plugin.h" #define SHIFT 11 #define BITS (1<<SHIFT) #define R_MASK (BITS-1) /* * Basic rate conversion plugin */ struct rate_channel { signed short last_S1; signed short last_S2; }; typedef void (*rate_f)(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, int src_frames, int dst_frames); struct rate_priv { unsigned int pitch; unsigned int pos; rate_f func; snd_pcm_sframes_t old_src_frames, old_dst_frames; struct rate_channel channels[]; }; static void rate_init(struct snd_pcm_plugin *plugin) { unsigned int channel; struct rate_priv *data = (struct rate_priv *)plugin->extra_data; data->pos = 0; for (channel = 0; channel < plugin->src_format.channels; channel++) { data->channels[channel].last_S1 = 0; data->channels[channel].last_S2 = 0; } } static void resample_expand(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, int src_frames, int dst_frames) { unsigned int pos = 0; signed int val; signed short S1, S2; signed short *src, *dst; unsigned int channel; int src_step, dst_step; int src_frames1, dst_frames1; struct rate_priv *data = (struct rate_priv *)plugin->extra_data; struct rate_channel *rchannels = data->channels; for (channel = 0; channel < plugin->src_format.channels; channel++) { pos = data->pos; S1 = rchannels->last_S1; S2 = rchannels->last_S2; if (!src_channels[channel].enabled) { if (dst_channels[channel].wanted) snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format); dst_channels[channel].enabled = 0; continue; } dst_channels[channel].enabled = 1; src = (signed short *)src_channels[channel].area.addr + src_channels[channel].area.first / 8 / 2; dst = (signed short *)dst_channels[channel].area.addr + dst_channels[channel].area.first / 8 / 2; src_step = src_channels[channel].area.step / 8 / 2; dst_step = dst_channels[channel].area.step / 8 / 2; src_frames1 = src_frames; dst_frames1 = dst_frames; while (dst_frames1-- > 0) { if (pos & ~R_MASK) { pos &= R_MASK; S1 = S2; if (src_frames1-- > 0) { S2 = *src; src += src_step; } } val = S1 + ((S2 - S1) * (signed int)pos) / BITS; if (val < -32768) val = -32768; else if (val > 32767) val = 32767; *dst = val; dst += dst_step; pos += data->pitch; } rchannels->last_S1 = S1; rchannels->last_S2 = S2; rchannels++; } data->pos = pos; } static void resample_shrink(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, int src_frames, int dst_frames) { unsigned int pos = 0; signed int val; signed short S1, S2; signed short *src, *dst; unsigned int channel; int src_step, dst_step; int src_frames1, dst_frames1; struct rate_priv *data = (struct rate_priv *)plugin->extra_data; struct rate_channel *rchannels = data->channels; for (channel = 0; channel < plugin->src_format.channels; ++channel) { pos = data->pos; S1 = rchannels->last_S1; S2 = rchannels->last_S2; if (!src_channels[channel].enabled) { if (dst_channels[channel].wanted) snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format); dst_channels[channel].enabled = 0; continue; } dst_channels[channel].enabled = 1; src = (signed short *)src_channels[channel].area.addr + src_channels[channel].area.first / 8 / 2; dst = (signed short *)dst_channels[channel].area.addr + dst_channels[channel].area.first / 8 / 2; src_step = src_channels[channel].area.step / 8 / 2; dst_step = dst_channels[channel].area.step / 8 / 2; src_frames1 = src_frames; dst_frames1 = dst_frames; while (dst_frames1 > 0) { S1 = S2; if (src_frames1-- > 0) { S2 = *src; src += src_step; } if (pos & ~R_MASK) { pos &= R_MASK; val = S1 + ((S2 - S1) * (signed int)pos) / BITS; if (val < -32768) val = -32768; else if (val > 32767) val = 32767; *dst = val; dst += dst_step; dst_frames1--; } pos += data->pitch; } rchannels->last_S1 = S1; rchannels->last_S2 = S2; rchannels++; } data->pos = pos; } static snd_pcm_sframes_t rate_src_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames) { struct rate_priv *data; snd_pcm_sframes_t res; if (snd_BUG_ON(!plugin)) return -ENXIO; if (frames == 0) return 0; data = (struct rate_priv *)plugin->extra_data; if (plugin->src_format.rate < plugin->dst_format.rate) { res = (((frames * data->pitch) + (BITS/2)) >> SHIFT); } else { res = DIV_ROUND_CLOSEST(frames << SHIFT, data->pitch); } if (data->old_src_frames > 0) { snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames; while (data->old_src_frames < frames1) { frames1 >>= 1; res1 <<= 1; } while (data->old_src_frames > frames1) { frames1 <<= 1; res1 >>= 1; } if (data->old_src_frames == frames1) return res1; } data->old_src_frames = frames; data->old_dst_frames = res; return res; } static snd_pcm_sframes_t rate_dst_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames) { struct rate_priv *data; snd_pcm_sframes_t res; if (snd_BUG_ON(!plugin)) return -ENXIO; if (frames == 0) return 0; data = (struct rate_priv *)plugin->extra_data; if (plugin->src_format.rate < plugin->dst_format.rate) { res = DIV_ROUND_CLOSEST(frames << SHIFT, data->pitch); } else { res = (((frames * data->pitch) + (BITS/2)) >> SHIFT); } if (data->old_dst_frames > 0) { snd_pcm_sframes_t frames1 = frames, res1 = data->old_src_frames; while (data->old_dst_frames < frames1) { frames1 >>= 1; res1 <<= 1; } while (data->old_dst_frames > frames1) { frames1 <<= 1; res1 >>= 1; } if (data->old_dst_frames == frames1) return res1; } data->old_dst_frames = frames; data->old_src_frames = res; return res; } static snd_pcm_sframes_t rate_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { snd_pcm_uframes_t dst_frames; struct rate_priv *data; if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) return -ENXIO; if (frames == 0) return 0; #ifdef CONFIG_SND_DEBUG { unsigned int channel; for (channel = 0; channel < plugin->src_format.channels; channel++) { if (snd_BUG_ON(src_channels[channel].area.first % 8 || src_channels[channel].area.step % 8)) return -ENXIO; if (snd_BUG_ON(dst_channels[channel].area.first % 8 || dst_channels[channel].area.step % 8)) return -ENXIO; } } #endif dst_frames = rate_dst_frames(plugin, frames); if (dst_frames > dst_channels[0].frames) dst_frames = dst_channels[0].frames; data = (struct rate_priv *)plugin->extra_data; data->func(plugin, src_channels, dst_channels, frames, dst_frames); return dst_frames; } static int rate_action(struct snd_pcm_plugin *plugin, enum snd_pcm_plugin_action action, unsigned long udata) { if (snd_BUG_ON(!plugin)) return -ENXIO; switch (action) { case INIT: case PREPARE: rate_init(plugin); break; default: break; } return 0; /* silenty ignore other actions */ } int snd_pcm_plugin_build_rate(struct snd_pcm_substream *plug, struct snd_pcm_plugin_format *src_format, struct snd_pcm_plugin_format *dst_format, struct snd_pcm_plugin **r_plugin) { int err; struct rate_priv *data; struct snd_pcm_plugin *plugin; if (snd_BUG_ON(!r_plugin)) return -ENXIO; *r_plugin = NULL; if (snd_BUG_ON(src_format->channels != dst_format->channels)) return -ENXIO; if (snd_BUG_ON(src_format->channels <= 0)) return -ENXIO; if (snd_BUG_ON(src_format->format != SNDRV_PCM_FORMAT_S16)) return -ENXIO; if (snd_BUG_ON(dst_format->format != SNDRV_PCM_FORMAT_S16)) return -ENXIO; if (snd_BUG_ON(src_format->rate == dst_format->rate)) return -ENXIO; err = snd_pcm_plugin_build(plug, "rate conversion", src_format, dst_format, struct_size(data, channels, src_format->channels), &plugin); if (err < 0) return err; data = (struct rate_priv *)plugin->extra_data; if (src_format->rate < dst_format->rate) { data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate; data->func = resample_expand; } else { data->pitch = ((dst_format->rate << SHIFT) + (src_format->rate >> 1)) / src_format->rate; data->func = resample_shrink; } data->pos = 0; rate_init(plugin); data->old_src_frames = data->old_dst_frames = 0; plugin->transfer = rate_transfer; plugin->src_frames = rate_src_frames; plugin->dst_frames = rate_dst_frames; plugin->action = rate_action; *r_plugin = plugin; return 0; }
linux-master
sound/core/oss/rate.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Digital Audio (PCM) abstract layer / OSS compatible * Copyright (c) by Jaroslav Kysela <[email protected]> */ #if 0 #define PLUGIN_DEBUG #endif #if 0 #define OSS_DEBUG #endif #include <linux/init.h> #include <linux/slab.h> #include <linux/sched/signal.h> #include <linux/time.h> #include <linux/vmalloc.h> #include <linux/module.h> #include <linux/math64.h> #include <linux/string.h> #include <linux/compat.h> #include <sound/core.h> #include <sound/minors.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include "pcm_plugin.h" #include <sound/info.h> #include <linux/soundcard.h> #include <sound/initval.h> #include <sound/mixer_oss.h> #define OSS_ALSAEMULVER _SIOR ('M', 249, int) static int dsp_map[SNDRV_CARDS]; static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1}; static bool nonblock_open = 1; MODULE_AUTHOR("Jaroslav Kysela <[email protected]>, Abramo Bagnara <[email protected]>"); MODULE_DESCRIPTION("PCM OSS emulation for ALSA."); MODULE_LICENSE("GPL"); module_param_array(dsp_map, int, NULL, 0444); MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device."); module_param_array(adsp_map, int, NULL, 0444); MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device."); module_param(nonblock_open, bool, 0644); MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices."); MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM); MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1); static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file); static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file); static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file); /* * helper functions to process hw_params */ static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin) { int changed = 0; if (i->min < min) { i->min = min; i->openmin = openmin; changed = 1; } else if (i->min == min && !i->openmin && openmin) { i->openmin = 1; changed = 1; } if (i->integer) { if (i->openmin) { i->min++; i->openmin = 0; } } if (snd_interval_checkempty(i)) { snd_interval_none(i); return -EINVAL; } return changed; } static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax) { int changed = 0; if (i->max > max) { i->max = max; i->openmax = openmax; changed = 1; } else if (i->max == max && !i->openmax && openmax) { i->openmax = 1; changed = 1; } if (i->integer) { if (i->openmax) { i->max--; i->openmax = 0; } } if (snd_interval_checkempty(i)) { snd_interval_none(i); return -EINVAL; } return changed; } static int snd_interval_refine_set(struct snd_interval *i, unsigned int val) { struct snd_interval t; t.empty = 0; t.min = t.max = val; t.openmin = t.openmax = 0; t.integer = 1; return snd_interval_refine(i, &t); } /** * snd_pcm_hw_param_value_min * @params: the hw_params instance * @var: parameter to retrieve * @dir: pointer to the direction (-1,0,1) or NULL * * Return the minimum value for field PAR. */ static unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, int *dir) { if (hw_is_mask(var)) { if (dir) *dir = 0; return snd_mask_min(hw_param_mask_c(params, var)); } if (hw_is_interval(var)) { const struct snd_interval *i = hw_param_interval_c(params, var); if (dir) *dir = i->openmin; return snd_interval_min(i); } return -EINVAL; } /** * snd_pcm_hw_param_value_max * @params: the hw_params instance * @var: parameter to retrieve * @dir: pointer to the direction (-1,0,1) or NULL * * Return the maximum value for field PAR. */ static int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, int *dir) { if (hw_is_mask(var)) { if (dir) *dir = 0; return snd_mask_max(hw_param_mask_c(params, var)); } if (hw_is_interval(var)) { const struct snd_interval *i = hw_param_interval_c(params, var); if (dir) *dir = - (int) i->openmax; return snd_interval_max(i); } return -EINVAL; } static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, const struct snd_mask *val) { int changed; changed = snd_mask_refine(hw_param_mask(params, var), val); if (changed > 0) { params->cmask |= 1 << var; params->rmask |= 1 << var; } return changed; } static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, const struct snd_mask *val) { int changed = _snd_pcm_hw_param_mask(params, var, val); if (changed < 0) return changed; if (params->rmask) { int err = snd_pcm_hw_refine(pcm, params); if (err < 0) return err; } return 0; } static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int val, int dir) { int changed; int open = 0; if (dir) { if (dir > 0) { open = 1; } else if (dir < 0) { if (val > 0) { open = 1; val--; } } } if (hw_is_mask(var)) changed = snd_mask_refine_min(hw_param_mask(params, var), val + !!open); else if (hw_is_interval(var)) changed = snd_interval_refine_min(hw_param_interval(params, var), val, open); else return -EINVAL; if (changed > 0) { params->cmask |= 1 << var; params->rmask |= 1 << var; } return changed; } /** * snd_pcm_hw_param_min * @pcm: PCM instance * @params: the hw_params instance * @var: parameter to retrieve * @val: minimal value * @dir: pointer to the direction (-1,0,1) or NULL * * Inside configuration space defined by PARAMS remove from PAR all * values < VAL. Reduce configuration space accordingly. * Return new minimum or -EINVAL if the configuration space is empty */ static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int val, int *dir) { int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0); if (changed < 0) return changed; if (params->rmask) { int err = snd_pcm_hw_refine(pcm, params); if (err < 0) return err; } return snd_pcm_hw_param_value_min(params, var, dir); } static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int val, int dir) { int changed; int open = 0; if (dir) { if (dir < 0) { open = 1; } else if (dir > 0) { open = 1; val++; } } if (hw_is_mask(var)) { if (val == 0 && open) { snd_mask_none(hw_param_mask(params, var)); changed = -EINVAL; } else changed = snd_mask_refine_max(hw_param_mask(params, var), val - !!open); } else if (hw_is_interval(var)) changed = snd_interval_refine_max(hw_param_interval(params, var), val, open); else return -EINVAL; if (changed > 0) { params->cmask |= 1 << var; params->rmask |= 1 << var; } return changed; } /** * snd_pcm_hw_param_max * @pcm: PCM instance * @params: the hw_params instance * @var: parameter to retrieve * @val: maximal value * @dir: pointer to the direction (-1,0,1) or NULL * * Inside configuration space defined by PARAMS remove from PAR all * values >= VAL + 1. Reduce configuration space accordingly. * Return new maximum or -EINVAL if the configuration space is empty */ static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int val, int *dir) { int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0); if (changed < 0) return changed; if (params->rmask) { int err = snd_pcm_hw_refine(pcm, params); if (err < 0) return err; } return snd_pcm_hw_param_value_max(params, var, dir); } static int boundary_sub(int a, int adir, int b, int bdir, int *c, int *cdir) { adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0); bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0); *c = a - b; *cdir = adir - bdir; if (*cdir == -2) { (*c)--; } else if (*cdir == 2) { (*c)++; } return 0; } static int boundary_lt(unsigned int a, int adir, unsigned int b, int bdir) { if (adir < 0) { a--; adir = 1; } else if (adir > 0) adir = 1; if (bdir < 0) { b--; bdir = 1; } else if (bdir > 0) bdir = 1; return a < b || (a == b && adir < bdir); } /* Return 1 if min is nearer to best than max */ static int boundary_nearer(int min, int mindir, int best, int bestdir, int max, int maxdir) { int dmin, dmindir; int dmax, dmaxdir; boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir); boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir); return boundary_lt(dmin, dmindir, dmax, dmaxdir); } /** * snd_pcm_hw_param_near * @pcm: PCM instance * @params: the hw_params instance * @var: parameter to retrieve * @best: value to set * @dir: pointer to the direction (-1,0,1) or NULL * * Inside configuration space defined by PARAMS set PAR to the available value * nearest to VAL. Reduce configuration space accordingly. * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS, * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT. * Return the value found. */ static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int best, int *dir) { struct snd_pcm_hw_params *save = NULL; int v; unsigned int saved_min; int last = 0; int min, max; int mindir, maxdir; int valdir = dir ? *dir : 0; /* FIXME */ if (best > INT_MAX) best = INT_MAX; min = max = best; mindir = maxdir = valdir; if (maxdir > 0) maxdir = 0; else if (maxdir == 0) maxdir = -1; else { maxdir = 1; max--; } save = kmalloc(sizeof(*save), GFP_KERNEL); if (save == NULL) return -ENOMEM; *save = *params; saved_min = min; min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir); if (min >= 0) { struct snd_pcm_hw_params *params1; if (max < 0) goto _end; if ((unsigned int)min == saved_min && mindir == valdir) goto _end; params1 = kmalloc(sizeof(*params1), GFP_KERNEL); if (params1 == NULL) { kfree(save); return -ENOMEM; } *params1 = *save; max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir); if (max < 0) { kfree(params1); goto _end; } if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) { *params = *params1; last = 1; } kfree(params1); } else { *params = *save; max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir); if (max < 0) { kfree(save); return max; } last = 1; } _end: kfree(save); if (last) v = snd_pcm_hw_param_last(pcm, params, var, dir); else v = snd_pcm_hw_param_first(pcm, params, var, dir); return v; } static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int val, int dir) { int changed; if (hw_is_mask(var)) { struct snd_mask *m = hw_param_mask(params, var); if (val == 0 && dir < 0) { changed = -EINVAL; snd_mask_none(m); } else { if (dir > 0) val++; else if (dir < 0) val--; changed = snd_mask_refine_set(hw_param_mask(params, var), val); } } else if (hw_is_interval(var)) { struct snd_interval *i = hw_param_interval(params, var); if (val == 0 && dir < 0) { changed = -EINVAL; snd_interval_none(i); } else if (dir == 0) changed = snd_interval_refine_set(i, val); else { struct snd_interval t; t.openmin = 1; t.openmax = 1; t.empty = 0; t.integer = 0; if (dir < 0) { t.min = val - 1; t.max = val; } else { t.min = val; t.max = val+1; } changed = snd_interval_refine(i, &t); } } else return -EINVAL; if (changed > 0) { params->cmask |= 1 << var; params->rmask |= 1 << var; } return changed; } /** * snd_pcm_hw_param_set * @pcm: PCM instance * @params: the hw_params instance * @var: parameter to retrieve * @val: value to set * @dir: pointer to the direction (-1,0,1) or NULL * * Inside configuration space defined by PARAMS remove from PAR all * values != VAL. Reduce configuration space accordingly. * Return VAL or -EINVAL if the configuration space is empty */ static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var, unsigned int val, int dir) { int changed = _snd_pcm_hw_param_set(params, var, val, dir); if (changed < 0) return changed; if (params->rmask) { int err = snd_pcm_hw_refine(pcm, params); if (err < 0) return err; } return snd_pcm_hw_param_value(params, var, NULL); } static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var) { int changed; changed = snd_interval_setinteger(hw_param_interval(params, var)); if (changed > 0) { params->cmask |= 1 << var; params->rmask |= 1 << var; } return changed; } /* * plugin */ #ifdef CONFIG_SND_PCM_OSS_PLUGINS static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_pcm_plugin *plugin, *next; plugin = runtime->oss.plugin_first; while (plugin) { next = plugin->next; snd_pcm_plugin_free(plugin); plugin = next; } runtime->oss.plugin_first = runtime->oss.plugin_last = NULL; return 0; } static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin) { struct snd_pcm_runtime *runtime = plugin->plug->runtime; plugin->next = runtime->oss.plugin_first; plugin->prev = NULL; if (runtime->oss.plugin_first) { runtime->oss.plugin_first->prev = plugin; runtime->oss.plugin_first = plugin; } else { runtime->oss.plugin_last = runtime->oss.plugin_first = plugin; } return 0; } int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin) { struct snd_pcm_runtime *runtime = plugin->plug->runtime; plugin->next = NULL; plugin->prev = runtime->oss.plugin_last; if (runtime->oss.plugin_last) { runtime->oss.plugin_last->next = plugin; runtime->oss.plugin_last = plugin; } else { runtime->oss.plugin_last = runtime->oss.plugin_first = plugin; } return 0; } #endif /* CONFIG_SND_PCM_OSS_PLUGINS */ static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames) { struct snd_pcm_runtime *runtime = substream->runtime; long buffer_size = snd_pcm_lib_buffer_bytes(substream); long bytes = frames_to_bytes(runtime, frames); if (buffer_size == runtime->oss.buffer_bytes) return bytes; #if BITS_PER_LONG >= 64 return runtime->oss.buffer_bytes * bytes / buffer_size; #else { u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes; return div_u64(bsize, buffer_size); } #endif } static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes) { struct snd_pcm_runtime *runtime = substream->runtime; long buffer_size = snd_pcm_lib_buffer_bytes(substream); if (buffer_size == runtime->oss.buffer_bytes) return bytes_to_frames(runtime, bytes); return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes); } static inline snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime) { return runtime->hw_ptr_interrupt; } /* define extended formats in the recent OSS versions (if any) */ /* linear formats */ #define AFMT_S32_LE 0x00001000 #define AFMT_S32_BE 0x00002000 #define AFMT_S24_LE 0x00008000 #define AFMT_S24_BE 0x00010000 #define AFMT_S24_PACKED 0x00040000 /* other supported formats */ #define AFMT_FLOAT 0x00004000 #define AFMT_SPDIF_RAW 0x00020000 /* unsupported formats */ #define AFMT_AC3 0x00000400 #define AFMT_VORBIS 0x00000800 static snd_pcm_format_t snd_pcm_oss_format_from(int format) { switch (format) { case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW; case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW; case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM; case AFMT_U8: return SNDRV_PCM_FORMAT_U8; case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE; case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE; case AFMT_S8: return SNDRV_PCM_FORMAT_S8; case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE; case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE; case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG; case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE; case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE; case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE; case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE; case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE; case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT; case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME; default: return SNDRV_PCM_FORMAT_U8; } } static int snd_pcm_oss_format_to(snd_pcm_format_t format) { switch (format) { case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW; case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW; case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM; case SNDRV_PCM_FORMAT_U8: return AFMT_U8; case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE; case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE; case SNDRV_PCM_FORMAT_S8: return AFMT_S8; case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE; case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE; case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG; case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE; case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE; case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE; case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE; case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED; case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT; case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW; default: return -EINVAL; } } static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *oss_params, struct snd_pcm_hw_params *slave_params) { ssize_t s; ssize_t oss_buffer_size; ssize_t oss_period_size, oss_periods; ssize_t min_period_size, max_period_size; struct snd_pcm_runtime *runtime = substream->runtime; size_t oss_frame_size; oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8; oss_buffer_size = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL); if (oss_buffer_size <= 0) return -EINVAL; oss_buffer_size = snd_pcm_plug_client_size(substream, oss_buffer_size * oss_frame_size); if (oss_buffer_size <= 0) return -EINVAL; oss_buffer_size = rounddown_pow_of_two(oss_buffer_size); if (atomic_read(&substream->mmap_count)) { if (oss_buffer_size > runtime->oss.mmap_bytes) oss_buffer_size = runtime->oss.mmap_bytes; } if (substream->oss.setup.period_size > 16) oss_period_size = substream->oss.setup.period_size; else if (runtime->oss.fragshift) { oss_period_size = 1 << runtime->oss.fragshift; if (oss_period_size > oss_buffer_size / 2) oss_period_size = oss_buffer_size / 2; } else { int sd; size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8; oss_period_size = oss_buffer_size; do { oss_period_size /= 2; } while (oss_period_size > bytes_per_sec); if (runtime->oss.subdivision == 0) { sd = 4; if (oss_period_size / sd > 4096) sd *= 2; if (oss_period_size / sd < 4096) sd = 1; } else sd = runtime->oss.subdivision; oss_period_size /= sd; if (oss_period_size < 16) oss_period_size = 16; } min_period_size = snd_pcm_plug_client_size(substream, snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL)); if (min_period_size > 0) { min_period_size *= oss_frame_size; min_period_size = roundup_pow_of_two(min_period_size); if (oss_period_size < min_period_size) oss_period_size = min_period_size; } max_period_size = snd_pcm_plug_client_size(substream, snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL)); if (max_period_size > 0) { max_period_size *= oss_frame_size; max_period_size = rounddown_pow_of_two(max_period_size); if (oss_period_size > max_period_size) oss_period_size = max_period_size; } oss_periods = oss_buffer_size / oss_period_size; if (substream->oss.setup.periods > 1) oss_periods = substream->oss.setup.periods; s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL); if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags) s = runtime->oss.maxfrags; if (oss_periods > s) oss_periods = s; s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL); if (s < 2) s = 2; if (oss_periods < s) oss_periods = s; while (oss_period_size * oss_periods > oss_buffer_size) oss_period_size /= 2; if (oss_period_size < 16) return -EINVAL; /* don't allocate too large period; 1MB period must be enough */ if (oss_period_size > 1024 * 1024) return -ENOMEM; runtime->oss.period_bytes = oss_period_size; runtime->oss.period_frames = 1; runtime->oss.periods = oss_periods; return 0; } static int choose_rate(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, unsigned int best_rate) { const struct snd_interval *it; struct snd_pcm_hw_params *save; unsigned int rate, prev; save = kmalloc(sizeof(*save), GFP_KERNEL); if (save == NULL) return -ENOMEM; *save = *params; it = hw_param_interval_c(save, SNDRV_PCM_HW_PARAM_RATE); /* try multiples of the best rate */ rate = best_rate; for (;;) { if (it->max < rate || (it->max == rate && it->openmax)) break; if (it->min < rate || (it->min == rate && !it->openmin)) { int ret; ret = snd_pcm_hw_param_set(substream, params, SNDRV_PCM_HW_PARAM_RATE, rate, 0); if (ret == (int)rate) { kfree(save); return rate; } *params = *save; } prev = rate; rate += best_rate; if (rate <= prev) break; } /* not found, use the nearest rate */ kfree(save); return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL); } /* parameter locking: returns immediately if tried during streaming */ static int lock_params(struct snd_pcm_runtime *runtime) { if (mutex_lock_interruptible(&runtime->oss.params_lock)) return -ERESTARTSYS; if (atomic_read(&runtime->oss.rw_ref)) { mutex_unlock(&runtime->oss.params_lock); return -EBUSY; } return 0; } static void unlock_params(struct snd_pcm_runtime *runtime) { mutex_unlock(&runtime->oss.params_lock); } static void snd_pcm_oss_release_buffers(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; kvfree(runtime->oss.buffer); runtime->oss.buffer = NULL; #ifdef CONFIG_SND_PCM_OSS_PLUGINS snd_pcm_oss_plugin_clear(substream); #endif } /* call with params_lock held */ static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_pcm_hw_params *params, *sparams; struct snd_pcm_sw_params *sw_params; ssize_t oss_buffer_size, oss_period_size; size_t oss_frame_size; int err; int direct; snd_pcm_format_t format, sformat; int n; const struct snd_mask *sformat_mask; struct snd_mask mask; if (!runtime->oss.params) return 0; sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL); params = kmalloc(sizeof(*params), GFP_KERNEL); sparams = kmalloc(sizeof(*sparams), GFP_KERNEL); if (!sw_params || !params || !sparams) { err = -ENOMEM; goto failure; } if (atomic_read(&substream->mmap_count)) direct = 1; else direct = substream->oss.setup.direct; _snd_pcm_hw_params_any(sparams); _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS); _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0); snd_mask_none(&mask); if (atomic_read(&substream->mmap_count)) snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); else { snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED); if (!direct) snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); } err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask); if (err < 0) { pcm_dbg(substream->pcm, "No usable accesses\n"); err = -EINVAL; goto failure; } err = choose_rate(substream, sparams, runtime->oss.rate); if (err < 0) goto failure; err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL); if (err < 0) goto failure; format = snd_pcm_oss_format_from(runtime->oss.format); sformat_mask = hw_param_mask_c(sparams, SNDRV_PCM_HW_PARAM_FORMAT); if (direct) sformat = format; else sformat = snd_pcm_plug_slave_format(format, sformat_mask); if ((__force int)sformat < 0 || !snd_mask_test_format(sformat_mask, sformat)) { pcm_for_each_format(sformat) { if (snd_mask_test_format(sformat_mask, sformat) && snd_pcm_oss_format_to(sformat) >= 0) goto format_found; } pcm_dbg(substream->pcm, "Cannot find a format!!!\n"); err = -EINVAL; goto failure; } format_found: err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0); if (err < 0) goto failure; if (direct) { memcpy(params, sparams, sizeof(*params)); } else { _snd_pcm_hw_params_any(params); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, 0); _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE, runtime->oss.rate, 0); pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n", params_access(params), params_format(params), params_channels(params), params_rate(params)); } pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n", params_access(sparams), params_format(sparams), params_channels(sparams), params_rate(sparams)); oss_frame_size = snd_pcm_format_physical_width(params_format(params)) * params_channels(params) / 8; err = snd_pcm_oss_period_size(substream, params, sparams); if (err < 0) goto failure; n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size); err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL); if (err < 0) goto failure; err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS, runtime->oss.periods, NULL); if (err < 0) goto failure; snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams); if (err < 0) { pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err); goto failure; } #ifdef CONFIG_SND_PCM_OSS_PLUGINS snd_pcm_oss_plugin_clear(substream); if (!direct) { /* add necessary plugins */ err = snd_pcm_plug_format_plugins(substream, params, sparams); if (err < 0) { pcm_dbg(substream->pcm, "snd_pcm_plug_format_plugins failed: %i\n", err); goto failure; } if (runtime->oss.plugin_first) { struct snd_pcm_plugin *plugin; err = snd_pcm_plugin_build_io(substream, sparams, &plugin); if (err < 0) { pcm_dbg(substream->pcm, "snd_pcm_plugin_build_io failed: %i\n", err); goto failure; } if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { err = snd_pcm_plugin_append(plugin); } else { err = snd_pcm_plugin_insert(plugin); } if (err < 0) goto failure; } } #endif if (runtime->oss.trigger) { sw_params->start_threshold = 1; } else { sw_params->start_threshold = runtime->boundary; } if (atomic_read(&substream->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE) sw_params->stop_threshold = runtime->boundary; else sw_params->stop_threshold = runtime->buffer_size; sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; sw_params->period_step = 1; sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : runtime->period_size; if (atomic_read(&substream->mmap_count) || substream->oss.setup.nosilence) { sw_params->silence_threshold = 0; sw_params->silence_size = 0; } else { snd_pcm_uframes_t frames; frames = runtime->period_size + 16; if (frames > runtime->buffer_size) frames = runtime->buffer_size; sw_params->silence_threshold = frames; sw_params->silence_size = frames; } err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params); if (err < 0) { pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err); goto failure; } runtime->oss.periods = params_periods(sparams); oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams)); if (oss_period_size < 0) { err = -EINVAL; goto failure; } #ifdef CONFIG_SND_PCM_OSS_PLUGINS if (runtime->oss.plugin_first) { err = snd_pcm_plug_alloc(substream, oss_period_size); if (err < 0) goto failure; } #endif oss_period_size = array_size(oss_period_size, oss_frame_size); oss_buffer_size = array_size(oss_period_size, runtime->oss.periods); if (oss_buffer_size <= 0) { err = -EINVAL; goto failure; } runtime->oss.period_bytes = oss_period_size; runtime->oss.buffer_bytes = oss_buffer_size; pdprintf("oss: period bytes = %i, buffer bytes = %i\n", runtime->oss.period_bytes, runtime->oss.buffer_bytes); pdprintf("slave: period_size = %i, buffer_size = %i\n", params_period_size(sparams), params_buffer_size(sparams)); runtime->oss.format = snd_pcm_oss_format_to(params_format(params)); runtime->oss.channels = params_channels(params); runtime->oss.rate = params_rate(params); kvfree(runtime->oss.buffer); runtime->oss.buffer = kvzalloc(runtime->oss.period_bytes, GFP_KERNEL); if (!runtime->oss.buffer) { err = -ENOMEM; goto failure; } runtime->oss.params = 0; runtime->oss.prepare = 1; runtime->oss.buffer_used = 0; if (runtime->dma_area) snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes)); runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size); err = 0; failure: if (err) snd_pcm_oss_release_buffers(substream); kfree(sw_params); kfree(params); kfree(sparams); return err; } /* this one takes the lock by itself */ static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream, bool trylock) { struct snd_pcm_runtime *runtime = substream->runtime; int err; if (trylock) { if (!(mutex_trylock(&runtime->oss.params_lock))) return -EAGAIN; } else if (mutex_lock_interruptible(&runtime->oss.params_lock)) return -ERESTARTSYS; err = snd_pcm_oss_change_params_locked(substream); mutex_unlock(&runtime->oss.params_lock); return err; } static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream) { int idx, err; struct snd_pcm_substream *asubstream = NULL, *substream; for (idx = 0; idx < 2; idx++) { substream = pcm_oss_file->streams[idx]; if (substream == NULL) continue; if (asubstream == NULL) asubstream = substream; if (substream->runtime->oss.params) { err = snd_pcm_oss_change_params(substream, false); if (err < 0) return err; } } if (!asubstream) return -EIO; if (r_substream) *r_substream = asubstream; return 0; } /* call with params_lock held */ /* NOTE: this always call PREPARE unconditionally no matter whether * runtime->oss.prepare is set or not */ static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream) { int err; struct snd_pcm_runtime *runtime = substream->runtime; err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); if (err < 0) { pcm_dbg(substream->pcm, "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n"); return err; } runtime->oss.prepare = 0; runtime->oss.prev_hw_ptr_period = 0; runtime->oss.period_ptr = 0; runtime->oss.buffer_used = 0; return 0; } static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime; int err; runtime = substream->runtime; if (runtime->oss.params) { err = snd_pcm_oss_change_params(substream, false); if (err < 0) return err; } if (runtime->oss.prepare) { if (mutex_lock_interruptible(&runtime->oss.params_lock)) return -ERESTARTSYS; err = snd_pcm_oss_prepare(substream); mutex_unlock(&runtime->oss.params_lock); if (err < 0) return err; } return 0; } /* call with params_lock held */ static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime; int err; runtime = substream->runtime; if (runtime->oss.params) { err = snd_pcm_oss_change_params_locked(substream); if (err < 0) return err; } if (runtime->oss.prepare) { err = snd_pcm_oss_prepare(substream); if (err < 0) return err; } return 0; } static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay) { struct snd_pcm_runtime *runtime; snd_pcm_uframes_t frames; int err = 0; while (1) { err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay); if (err < 0) break; runtime = substream->runtime; if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size) break; /* in case of overrun, skip whole periods like OSS/Linux driver does */ /* until avail(delay) <= buffer_size */ frames = (*delay - runtime->buffer_size) + runtime->period_size - 1; frames /= runtime->period_size; frames *= runtime->period_size; err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames); if (err < 0) break; } return err; } snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel) { struct snd_pcm_runtime *runtime = substream->runtime; int ret; while (1) { if (runtime->state == SNDRV_PCM_STATE_XRUN || runtime->state == SNDRV_PCM_STATE_SUSPENDED) { #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "pcm_oss: write: recovering from %s\n", runtime->state == SNDRV_PCM_STATE_XRUN ? "XRUN" : "SUSPEND"); #endif ret = snd_pcm_oss_prepare(substream); if (ret < 0) break; } mutex_unlock(&runtime->oss.params_lock); ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true, frames, in_kernel); mutex_lock(&runtime->oss.params_lock); if (ret != -EPIPE && ret != -ESTRPIPE) break; /* test, if we can't store new data, because the stream */ /* has not been started */ if (runtime->state == SNDRV_PCM_STATE_PREPARED) return -EAGAIN; } return ret; } snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel) { struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_sframes_t delay; int ret; while (1) { if (runtime->state == SNDRV_PCM_STATE_XRUN || runtime->state == SNDRV_PCM_STATE_SUSPENDED) { #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "pcm_oss: read: recovering from %s\n", runtime->state == SNDRV_PCM_STATE_XRUN ? "XRUN" : "SUSPEND"); #endif ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL); if (ret < 0) break; } else if (runtime->state == SNDRV_PCM_STATE_SETUP) { ret = snd_pcm_oss_prepare(substream); if (ret < 0) break; } ret = snd_pcm_oss_capture_position_fixup(substream, &delay); if (ret < 0) break; mutex_unlock(&runtime->oss.params_lock); ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true, frames, in_kernel); mutex_lock(&runtime->oss.params_lock); if (ret == -EPIPE) { if (runtime->state == SNDRV_PCM_STATE_DRAINING) { ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); if (ret < 0) break; } continue; } if (ret != -ESTRPIPE) break; } return ret; } #ifdef CONFIG_SND_PCM_OSS_PLUGINS snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames) { struct snd_pcm_runtime *runtime = substream->runtime; int ret; while (1) { if (runtime->state == SNDRV_PCM_STATE_XRUN || runtime->state == SNDRV_PCM_STATE_SUSPENDED) { #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "pcm_oss: writev: recovering from %s\n", runtime->state == SNDRV_PCM_STATE_XRUN ? "XRUN" : "SUSPEND"); #endif ret = snd_pcm_oss_prepare(substream); if (ret < 0) break; } ret = snd_pcm_kernel_writev(substream, bufs, frames); if (ret != -EPIPE && ret != -ESTRPIPE) break; /* test, if we can't store new data, because the stream */ /* has not been started */ if (runtime->state == SNDRV_PCM_STATE_PREPARED) return -EAGAIN; } return ret; } snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames) { struct snd_pcm_runtime *runtime = substream->runtime; int ret; while (1) { if (runtime->state == SNDRV_PCM_STATE_XRUN || runtime->state == SNDRV_PCM_STATE_SUSPENDED) { #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "pcm_oss: readv: recovering from %s\n", runtime->state == SNDRV_PCM_STATE_XRUN ? "XRUN" : "SUSPEND"); #endif ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL); if (ret < 0) break; } else if (runtime->state == SNDRV_PCM_STATE_SETUP) { ret = snd_pcm_oss_prepare(substream); if (ret < 0) break; } ret = snd_pcm_kernel_readv(substream, bufs, frames); if (ret != -EPIPE && ret != -ESTRPIPE) break; } return ret; } #endif /* CONFIG_SND_PCM_OSS_PLUGINS */ static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel) { struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_sframes_t frames, frames1; #ifdef CONFIG_SND_PCM_OSS_PLUGINS if (runtime->oss.plugin_first) { struct snd_pcm_plugin_channel *channels; size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8; if (!in_kernel) { if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes)) return -EFAULT; buf = runtime->oss.buffer; } frames = bytes / oss_frame_bytes; frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels); if (frames1 < 0) return frames1; frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1); if (frames1 <= 0) return frames1; bytes = frames1 * oss_frame_bytes; } else #endif { frames = bytes_to_frames(runtime, bytes); frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel); if (frames1 <= 0) return frames1; bytes = frames_to_bytes(runtime, frames1); } return bytes; } static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes) { size_t xfer = 0; ssize_t tmp = 0; struct snd_pcm_runtime *runtime = substream->runtime; if (atomic_read(&substream->mmap_count)) return -ENXIO; atomic_inc(&runtime->oss.rw_ref); while (bytes > 0) { if (mutex_lock_interruptible(&runtime->oss.params_lock)) { tmp = -ERESTARTSYS; break; } tmp = snd_pcm_oss_make_ready_locked(substream); if (tmp < 0) goto err; if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) { tmp = bytes; if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes) tmp = runtime->oss.period_bytes - runtime->oss.buffer_used; if (tmp > 0) { if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) { tmp = -EFAULT; goto err; } } runtime->oss.buffer_used += tmp; buf += tmp; bytes -= tmp; xfer += tmp; if (substream->oss.setup.partialfrag || runtime->oss.buffer_used == runtime->oss.period_bytes) { tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr, runtime->oss.buffer_used - runtime->oss.period_ptr, 1); if (tmp <= 0) goto err; runtime->oss.bytes += tmp; runtime->oss.period_ptr += tmp; runtime->oss.period_ptr %= runtime->oss.period_bytes; if (runtime->oss.period_ptr == 0 || runtime->oss.period_ptr == runtime->oss.buffer_used) runtime->oss.buffer_used = 0; else if ((substream->f_flags & O_NONBLOCK) != 0) { tmp = -EAGAIN; goto err; } } } else { tmp = snd_pcm_oss_write2(substream, (const char __force *)buf, runtime->oss.period_bytes, 0); if (tmp <= 0) goto err; runtime->oss.bytes += tmp; buf += tmp; bytes -= tmp; xfer += tmp; if ((substream->f_flags & O_NONBLOCK) != 0 && tmp != runtime->oss.period_bytes) tmp = -EAGAIN; } err: mutex_unlock(&runtime->oss.params_lock); if (tmp < 0) break; if (signal_pending(current)) { tmp = -ERESTARTSYS; break; } tmp = 0; } atomic_dec(&runtime->oss.rw_ref); return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp; } static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel) { struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_sframes_t frames, frames1; #ifdef CONFIG_SND_PCM_OSS_PLUGINS char __user *final_dst = (char __force __user *)buf; if (runtime->oss.plugin_first) { struct snd_pcm_plugin_channel *channels; size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8; if (!in_kernel) buf = runtime->oss.buffer; frames = bytes / oss_frame_bytes; frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels); if (frames1 < 0) return frames1; frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1); if (frames1 <= 0) return frames1; bytes = frames1 * oss_frame_bytes; if (!in_kernel && copy_to_user(final_dst, buf, bytes)) return -EFAULT; } else #endif { frames = bytes_to_frames(runtime, bytes); frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel); if (frames1 <= 0) return frames1; bytes = frames_to_bytes(runtime, frames1); } return bytes; } static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes) { size_t xfer = 0; ssize_t tmp = 0; struct snd_pcm_runtime *runtime = substream->runtime; if (atomic_read(&substream->mmap_count)) return -ENXIO; atomic_inc(&runtime->oss.rw_ref); while (bytes > 0) { if (mutex_lock_interruptible(&runtime->oss.params_lock)) { tmp = -ERESTARTSYS; break; } tmp = snd_pcm_oss_make_ready_locked(substream); if (tmp < 0) goto err; if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) { if (runtime->oss.buffer_used == 0) { tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1); if (tmp <= 0) goto err; runtime->oss.bytes += tmp; runtime->oss.period_ptr = tmp; runtime->oss.buffer_used = tmp; } tmp = bytes; if ((size_t) tmp > runtime->oss.buffer_used) tmp = runtime->oss.buffer_used; if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) { tmp = -EFAULT; goto err; } buf += tmp; bytes -= tmp; xfer += tmp; runtime->oss.buffer_used -= tmp; } else { tmp = snd_pcm_oss_read2(substream, (char __force *)buf, runtime->oss.period_bytes, 0); if (tmp <= 0) goto err; runtime->oss.bytes += tmp; buf += tmp; bytes -= tmp; xfer += tmp; } err: mutex_unlock(&runtime->oss.params_lock); if (tmp < 0) break; if (signal_pending(current)) { tmp = -ERESTARTSYS; break; } tmp = 0; } atomic_dec(&runtime->oss.rw_ref); return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp; } static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; struct snd_pcm_runtime *runtime; int i; for (i = 0; i < 2; i++) { substream = pcm_oss_file->streams[i]; if (!substream) continue; runtime = substream->runtime; snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); mutex_lock(&runtime->oss.params_lock); runtime->oss.prepare = 1; runtime->oss.buffer_used = 0; runtime->oss.prev_hw_ptr_period = 0; runtime->oss.period_ptr = 0; mutex_unlock(&runtime->oss.params_lock); } return 0; } static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; int err; substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; if (substream != NULL) { err = snd_pcm_oss_make_ready(substream); if (err < 0) return err; snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL); } /* note: all errors from the start action are ignored */ /* OSS apps do not know, how to handle them */ return 0; } static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size) { struct snd_pcm_runtime *runtime; ssize_t result = 0; snd_pcm_state_t state; long res; wait_queue_entry_t wait; runtime = substream->runtime; init_waitqueue_entry(&wait, current); add_wait_queue(&runtime->sleep, &wait); #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "sync1: size = %li\n", size); #endif while (1) { result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1); if (result > 0) { runtime->oss.buffer_used = 0; result = 0; break; } if (result != 0 && result != -EAGAIN) break; result = 0; set_current_state(TASK_INTERRUPTIBLE); snd_pcm_stream_lock_irq(substream); state = runtime->state; snd_pcm_stream_unlock_irq(substream); if (state != SNDRV_PCM_STATE_RUNNING) { set_current_state(TASK_RUNNING); break; } res = schedule_timeout(10 * HZ); if (signal_pending(current)) { result = -ERESTARTSYS; break; } if (res == 0) { pcm_err(substream->pcm, "OSS sync error - DMA timeout\n"); result = -EIO; break; } } remove_wait_queue(&runtime->sleep, &wait); return result; } static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file) { int err = 0; unsigned int saved_f_flags; struct snd_pcm_substream *substream; struct snd_pcm_runtime *runtime; snd_pcm_format_t format; unsigned long width; size_t size; substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; if (substream != NULL) { runtime = substream->runtime; if (atomic_read(&substream->mmap_count)) goto __direct; atomic_inc(&runtime->oss.rw_ref); if (mutex_lock_interruptible(&runtime->oss.params_lock)) { atomic_dec(&runtime->oss.rw_ref); return -ERESTARTSYS; } err = snd_pcm_oss_make_ready_locked(substream); if (err < 0) goto unlock; format = snd_pcm_oss_format_from(runtime->oss.format); width = snd_pcm_format_physical_width(format); if (runtime->oss.buffer_used > 0) { #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "sync: buffer_used\n"); #endif size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width; snd_pcm_format_set_silence(format, runtime->oss.buffer + runtime->oss.buffer_used, size); err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes); if (err < 0) goto unlock; } else if (runtime->oss.period_ptr > 0) { #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "sync: period_ptr\n"); #endif size = runtime->oss.period_bytes - runtime->oss.period_ptr; snd_pcm_format_set_silence(format, runtime->oss.buffer, size * 8 / width); err = snd_pcm_oss_sync1(substream, size); if (err < 0) goto unlock; } /* * The ALSA's period might be a bit large than OSS one. * Fill the remain portion of ALSA period with zeros. */ size = runtime->control->appl_ptr % runtime->period_size; if (size > 0) { size = runtime->period_size - size; if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) snd_pcm_lib_write(substream, NULL, size); else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) snd_pcm_lib_writev(substream, NULL, size); } unlock: mutex_unlock(&runtime->oss.params_lock); atomic_dec(&runtime->oss.rw_ref); if (err < 0) return err; /* * finish sync: drain the buffer */ __direct: saved_f_flags = substream->f_flags; substream->f_flags &= ~O_NONBLOCK; err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL); substream->f_flags = saved_f_flags; if (err < 0) return err; mutex_lock(&runtime->oss.params_lock); runtime->oss.prepare = 1; mutex_unlock(&runtime->oss.params_lock); } substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; if (substream != NULL) { err = snd_pcm_oss_make_ready(substream); if (err < 0) return err; runtime = substream->runtime; err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); if (err < 0) return err; mutex_lock(&runtime->oss.params_lock); runtime->oss.buffer_used = 0; runtime->oss.prepare = 1; mutex_unlock(&runtime->oss.params_lock); } return 0; } static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate) { int idx; for (idx = 1; idx >= 0; --idx) { struct snd_pcm_substream *substream = pcm_oss_file->streams[idx]; struct snd_pcm_runtime *runtime; int err; if (substream == NULL) continue; runtime = substream->runtime; if (rate < 1000) rate = 1000; else if (rate > 192000) rate = 192000; err = lock_params(runtime); if (err < 0) return err; if (runtime->oss.rate != rate) { runtime->oss.params = 1; runtime->oss.rate = rate; } unlock_params(runtime); } return snd_pcm_oss_get_rate(pcm_oss_file); } static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; int err; err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream); if (err < 0) return err; return substream->runtime->oss.rate; } static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels) { int idx; if (channels < 1) channels = 1; if (channels > 128) return -EINVAL; for (idx = 1; idx >= 0; --idx) { struct snd_pcm_substream *substream = pcm_oss_file->streams[idx]; struct snd_pcm_runtime *runtime; int err; if (substream == NULL) continue; runtime = substream->runtime; err = lock_params(runtime); if (err < 0) return err; if (runtime->oss.channels != channels) { runtime->oss.params = 1; runtime->oss.channels = channels; } unlock_params(runtime); } return snd_pcm_oss_get_channels(pcm_oss_file); } static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; int err; err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream); if (err < 0) return err; return substream->runtime->oss.channels; } static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; int err; err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream); if (err < 0) return err; return substream->runtime->oss.period_bytes; } static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; int err; int direct; struct snd_pcm_hw_params *params; unsigned int formats = 0; const struct snd_mask *format_mask; int fmt; err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream); if (err < 0) return err; if (atomic_read(&substream->mmap_count)) direct = 1; else direct = substream->oss.setup.direct; if (!direct) return AFMT_MU_LAW | AFMT_U8 | AFMT_S16_LE | AFMT_S16_BE | AFMT_S8 | AFMT_U16_LE | AFMT_U16_BE | AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | AFMT_S24_PACKED; params = kmalloc(sizeof(*params), GFP_KERNEL); if (!params) return -ENOMEM; _snd_pcm_hw_params_any(params); err = snd_pcm_hw_refine(substream, params); if (err < 0) goto error; format_mask = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT); for (fmt = 0; fmt < 32; ++fmt) { if (snd_mask_test(format_mask, fmt)) { int f = snd_pcm_oss_format_to((__force snd_pcm_format_t)fmt); if (f >= 0) formats |= f; } } error: kfree(params); return err < 0 ? err : formats; } static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format) { int formats, idx; int err; if (format != AFMT_QUERY) { formats = snd_pcm_oss_get_formats(pcm_oss_file); if (formats < 0) return formats; if (!(formats & format)) format = AFMT_U8; for (idx = 1; idx >= 0; --idx) { struct snd_pcm_substream *substream = pcm_oss_file->streams[idx]; struct snd_pcm_runtime *runtime; if (substream == NULL) continue; runtime = substream->runtime; err = lock_params(runtime); if (err < 0) return err; if (runtime->oss.format != format) { runtime->oss.params = 1; runtime->oss.format = format; } unlock_params(runtime); } } return snd_pcm_oss_get_format(pcm_oss_file); } static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; int err; err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream); if (err < 0) return err; return substream->runtime->oss.format; } static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide) { struct snd_pcm_runtime *runtime; runtime = substream->runtime; if (subdivide == 0) { subdivide = runtime->oss.subdivision; if (subdivide == 0) subdivide = 1; return subdivide; } if (runtime->oss.subdivision || runtime->oss.fragshift) return -EINVAL; if (subdivide != 1 && subdivide != 2 && subdivide != 4 && subdivide != 8 && subdivide != 16) return -EINVAL; runtime->oss.subdivision = subdivide; runtime->oss.params = 1; return subdivide; } static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide) { int err = -EINVAL, idx; for (idx = 1; idx >= 0; --idx) { struct snd_pcm_substream *substream = pcm_oss_file->streams[idx]; struct snd_pcm_runtime *runtime; if (substream == NULL) continue; runtime = substream->runtime; err = lock_params(runtime); if (err < 0) return err; err = snd_pcm_oss_set_subdivide1(substream, subdivide); unlock_params(runtime); if (err < 0) return err; } return err; } static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val) { struct snd_pcm_runtime *runtime; int fragshift; runtime = substream->runtime; if (runtime->oss.subdivision || runtime->oss.fragshift) return -EINVAL; fragshift = val & 0xffff; if (fragshift >= 25) /* should be large enough */ return -EINVAL; runtime->oss.fragshift = fragshift; runtime->oss.maxfrags = (val >> 16) & 0xffff; if (runtime->oss.fragshift < 4) /* < 16 */ runtime->oss.fragshift = 4; if (runtime->oss.maxfrags < 2) runtime->oss.maxfrags = 2; runtime->oss.params = 1; return 0; } static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val) { int err = -EINVAL, idx; for (idx = 1; idx >= 0; --idx) { struct snd_pcm_substream *substream = pcm_oss_file->streams[idx]; struct snd_pcm_runtime *runtime; if (substream == NULL) continue; runtime = substream->runtime; err = lock_params(runtime); if (err < 0) return err; err = snd_pcm_oss_set_fragment1(substream, val); unlock_params(runtime); if (err < 0) return err; } return err; } static int snd_pcm_oss_nonblock(struct file * file) { spin_lock(&file->f_lock); file->f_flags |= O_NONBLOCK; spin_unlock(&file->f_lock); return 0; } static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res) { if (substream == NULL) { res &= ~DSP_CAP_DUPLEX; return res; } #ifdef DSP_CAP_MULTI if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) if (substream->pstr->substream_count > 1) res |= DSP_CAP_MULTI; #endif /* DSP_CAP_REALTIME is set all times: */ /* all ALSA drivers can return actual pointer in ring buffer */ #if defined(DSP_CAP_REALTIME) && 0 { struct snd_pcm_runtime *runtime = substream->runtime; if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH)) res &= ~DSP_CAP_REALTIME; } #endif return res; } static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file) { int result, idx; result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME; for (idx = 0; idx < 2; idx++) { struct snd_pcm_substream *substream = pcm_oss_file->streams[idx]; result = snd_pcm_oss_get_caps1(substream, result); } result |= 0x0001; /* revision - same as SB AWE 64 */ return result; } static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr) { struct snd_pcm_runtime *runtime = substream->runtime; snd_pcm_uframes_t appl_ptr; appl_ptr = hw_ptr + runtime->buffer_size; appl_ptr %= runtime->boundary; runtime->control->appl_ptr = appl_ptr; } static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger) { struct snd_pcm_runtime *runtime; struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL; int err, cmd; #ifdef OSS_DEBUG pr_debug("pcm_oss: trigger = 0x%x\n", trigger); #endif psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; if (psubstream) { err = snd_pcm_oss_make_ready(psubstream); if (err < 0) return err; } if (csubstream) { err = snd_pcm_oss_make_ready(csubstream); if (err < 0) return err; } if (psubstream) { runtime = psubstream->runtime; cmd = 0; if (mutex_lock_interruptible(&runtime->oss.params_lock)) return -ERESTARTSYS; if (trigger & PCM_ENABLE_OUTPUT) { if (runtime->oss.trigger) goto _skip1; if (atomic_read(&psubstream->mmap_count)) snd_pcm_oss_simulate_fill(psubstream, get_hw_ptr_period(runtime)); runtime->oss.trigger = 1; runtime->start_threshold = 1; cmd = SNDRV_PCM_IOCTL_START; } else { if (!runtime->oss.trigger) goto _skip1; runtime->oss.trigger = 0; runtime->start_threshold = runtime->boundary; cmd = SNDRV_PCM_IOCTL_DROP; runtime->oss.prepare = 1; } _skip1: mutex_unlock(&runtime->oss.params_lock); if (cmd) { err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL); if (err < 0) return err; } } if (csubstream) { runtime = csubstream->runtime; cmd = 0; if (mutex_lock_interruptible(&runtime->oss.params_lock)) return -ERESTARTSYS; if (trigger & PCM_ENABLE_INPUT) { if (runtime->oss.trigger) goto _skip2; runtime->oss.trigger = 1; runtime->start_threshold = 1; cmd = SNDRV_PCM_IOCTL_START; } else { if (!runtime->oss.trigger) goto _skip2; runtime->oss.trigger = 0; runtime->start_threshold = runtime->boundary; cmd = SNDRV_PCM_IOCTL_DROP; runtime->oss.prepare = 1; } _skip2: mutex_unlock(&runtime->oss.params_lock); if (cmd) { err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL); if (err < 0) return err; } } return 0; } static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL; int result = 0; psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger) result |= PCM_ENABLE_OUTPUT; if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger) result |= PCM_ENABLE_INPUT; return result; } static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file) { struct snd_pcm_substream *substream; struct snd_pcm_runtime *runtime; snd_pcm_sframes_t delay; int err; substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; if (substream == NULL) return -EINVAL; err = snd_pcm_oss_make_ready(substream); if (err < 0) return err; runtime = substream->runtime; if (runtime->oss.params || runtime->oss.prepare) return 0; err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay); if (err == -EPIPE) delay = 0; /* hack for broken OSS applications */ else if (err < 0) return err; return snd_pcm_oss_bytes(substream, delay); } static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info) { struct snd_pcm_substream *substream; struct snd_pcm_runtime *runtime; snd_pcm_sframes_t delay; int fixup; struct count_info info; int err; if (_info == NULL) return -EFAULT; substream = pcm_oss_file->streams[stream]; if (substream == NULL) return -EINVAL; err = snd_pcm_oss_make_ready(substream); if (err < 0) return err; runtime = substream->runtime; if (runtime->oss.params || runtime->oss.prepare) { memset(&info, 0, sizeof(info)); if (copy_to_user(_info, &info, sizeof(info))) return -EFAULT; return 0; } if (stream == SNDRV_PCM_STREAM_PLAYBACK) { err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay); if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) { err = 0; delay = 0; fixup = 0; } else { fixup = runtime->oss.buffer_used; } } else { err = snd_pcm_oss_capture_position_fixup(substream, &delay); fixup = -runtime->oss.buffer_used; } if (err < 0) return err; info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size); if (atomic_read(&substream->mmap_count)) { snd_pcm_sframes_t n; delay = get_hw_ptr_period(runtime); n = delay - runtime->oss.prev_hw_ptr_period; if (n < 0) n += runtime->boundary; info.blocks = n / runtime->period_size; runtime->oss.prev_hw_ptr_period = delay; if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) snd_pcm_oss_simulate_fill(substream, delay); info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX; } else { delay = snd_pcm_oss_bytes(substream, delay); if (stream == SNDRV_PCM_STREAM_PLAYBACK) { if (substream->oss.setup.buggyptr) info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes; else info.blocks = (delay + fixup) / runtime->oss.period_bytes; info.bytes = (runtime->oss.bytes - delay) & INT_MAX; } else { delay += fixup; info.blocks = delay / runtime->oss.period_bytes; info.bytes = (runtime->oss.bytes + delay) & INT_MAX; } } if (copy_to_user(_info, &info, sizeof(info))) return -EFAULT; return 0; } static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info) { struct snd_pcm_substream *substream; struct snd_pcm_runtime *runtime; snd_pcm_sframes_t avail; int fixup; struct audio_buf_info info; int err; if (_info == NULL) return -EFAULT; substream = pcm_oss_file->streams[stream]; if (substream == NULL) return -EINVAL; runtime = substream->runtime; if (runtime->oss.params) { err = snd_pcm_oss_change_params(substream, false); if (err < 0) return err; } info.fragsize = runtime->oss.period_bytes; info.fragstotal = runtime->periods; if (runtime->oss.prepare) { if (stream == SNDRV_PCM_STREAM_PLAYBACK) { info.bytes = runtime->oss.period_bytes * runtime->oss.periods; info.fragments = runtime->oss.periods; } else { info.bytes = 0; info.fragments = 0; } } else { if (stream == SNDRV_PCM_STREAM_PLAYBACK) { err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail); if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) { avail = runtime->buffer_size; err = 0; fixup = 0; } else { avail = runtime->buffer_size - avail; fixup = -runtime->oss.buffer_used; } } else { err = snd_pcm_oss_capture_position_fixup(substream, &avail); fixup = runtime->oss.buffer_used; } if (err < 0) return err; info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup; info.fragments = info.bytes / runtime->oss.period_bytes; } #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize); #endif if (copy_to_user(_info, &info, sizeof(info))) return -EFAULT; return 0; } static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info) { // it won't be probably implemented // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n"); return -EINVAL; } static const char *strip_task_path(const char *path) { const char *ptr, *ptrl = NULL; for (ptr = path; *ptr; ptr++) { if (*ptr == '/') ptrl = ptr + 1; } return ptrl; } static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream, const char *task_name, struct snd_pcm_oss_setup *rsetup) { struct snd_pcm_oss_setup *setup; mutex_lock(&pcm->streams[stream].oss.setup_mutex); do { for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) { if (!strcmp(setup->task_name, task_name)) goto out; } } while ((task_name = strip_task_path(task_name)) != NULL); out: if (setup) *rsetup = *setup; mutex_unlock(&pcm->streams[stream].oss.setup_mutex); } static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream) { snd_pcm_oss_release_buffers(substream); substream->oss.oss = 0; } static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream, struct snd_pcm_oss_setup *setup, int minor) { struct snd_pcm_runtime *runtime; substream->oss.oss = 1; substream->oss.setup = *setup; if (setup->nonblock) substream->f_flags |= O_NONBLOCK; else if (setup->block) substream->f_flags &= ~O_NONBLOCK; runtime = substream->runtime; runtime->oss.params = 1; runtime->oss.trigger = 1; runtime->oss.rate = 8000; mutex_init(&runtime->oss.params_lock); switch (SNDRV_MINOR_OSS_DEVICE(minor)) { case SNDRV_MINOR_OSS_PCM_8: runtime->oss.format = AFMT_U8; break; case SNDRV_MINOR_OSS_PCM_16: runtime->oss.format = AFMT_S16_LE; break; default: runtime->oss.format = AFMT_MU_LAW; } runtime->oss.channels = 1; runtime->oss.fragshift = 0; runtime->oss.maxfrags = 0; runtime->oss.subdivision = 0; substream->pcm_release = snd_pcm_oss_release_substream; atomic_set(&runtime->oss.rw_ref, 0); } static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file) { int cidx; if (!pcm_oss_file) return 0; for (cidx = 0; cidx < 2; ++cidx) { struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx]; if (substream) snd_pcm_release_substream(substream); } kfree(pcm_oss_file); return 0; } static int snd_pcm_oss_open_file(struct file *file, struct snd_pcm *pcm, struct snd_pcm_oss_file **rpcm_oss_file, int minor, struct snd_pcm_oss_setup *setup) { int idx, err; struct snd_pcm_oss_file *pcm_oss_file; struct snd_pcm_substream *substream; fmode_t f_mode = file->f_mode; if (rpcm_oss_file) *rpcm_oss_file = NULL; pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL); if (pcm_oss_file == NULL) return -ENOMEM; if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) && (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX)) f_mode = FMODE_WRITE; file->f_flags &= ~O_APPEND; for (idx = 0; idx < 2; idx++) { if (setup[idx].disable) continue; if (! pcm->streams[idx].substream_count) continue; /* no matching substream */ if (idx == SNDRV_PCM_STREAM_PLAYBACK) { if (! (f_mode & FMODE_WRITE)) continue; } else { if (! (f_mode & FMODE_READ)) continue; } err = snd_pcm_open_substream(pcm, idx, file, &substream); if (err < 0) { snd_pcm_oss_release_file(pcm_oss_file); return err; } pcm_oss_file->streams[idx] = substream; snd_pcm_oss_init_substream(substream, &setup[idx], minor); } if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) { snd_pcm_oss_release_file(pcm_oss_file); return -EINVAL; } file->private_data = pcm_oss_file; if (rpcm_oss_file) *rpcm_oss_file = pcm_oss_file; return 0; } static int snd_task_name(struct task_struct *task, char *name, size_t size) { unsigned int idx; if (snd_BUG_ON(!task || !name || size < 2)) return -EINVAL; for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++) name[idx] = task->comm[idx]; name[idx] = '\0'; return 0; } static int snd_pcm_oss_open(struct inode *inode, struct file *file) { int err; char task_name[32]; struct snd_pcm *pcm; struct snd_pcm_oss_file *pcm_oss_file; struct snd_pcm_oss_setup setup[2]; int nonblock; wait_queue_entry_t wait; err = nonseekable_open(inode, file); if (err < 0) return err; pcm = snd_lookup_oss_minor_data(iminor(inode), SNDRV_OSS_DEVICE_TYPE_PCM); if (pcm == NULL) { err = -ENODEV; goto __error1; } err = snd_card_file_add(pcm->card, file); if (err < 0) goto __error1; if (!try_module_get(pcm->card->module)) { err = -EFAULT; goto __error2; } if (snd_task_name(current, task_name, sizeof(task_name)) < 0) { err = -EFAULT; goto __error; } memset(setup, 0, sizeof(setup)); if (file->f_mode & FMODE_WRITE) snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name, &setup[0]); if (file->f_mode & FMODE_READ) snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name, &setup[1]); nonblock = !!(file->f_flags & O_NONBLOCK); if (!nonblock) nonblock = nonblock_open; init_waitqueue_entry(&wait, current); add_wait_queue(&pcm->open_wait, &wait); mutex_lock(&pcm->open_mutex); while (1) { err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file, iminor(inode), setup); if (err >= 0) break; if (err == -EAGAIN) { if (nonblock) { err = -EBUSY; break; } } else break; set_current_state(TASK_INTERRUPTIBLE); mutex_unlock(&pcm->open_mutex); schedule(); mutex_lock(&pcm->open_mutex); if (pcm->card->shutdown) { err = -ENODEV; break; } if (signal_pending(current)) { err = -ERESTARTSYS; break; } } remove_wait_queue(&pcm->open_wait, &wait); mutex_unlock(&pcm->open_mutex); if (err < 0) goto __error; snd_card_unref(pcm->card); return err; __error: module_put(pcm->card->module); __error2: snd_card_file_remove(pcm->card, file); __error1: if (pcm) snd_card_unref(pcm->card); return err; } static int snd_pcm_oss_release(struct inode *inode, struct file *file) { struct snd_pcm *pcm; struct snd_pcm_substream *substream; struct snd_pcm_oss_file *pcm_oss_file; pcm_oss_file = file->private_data; substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; if (substream == NULL) substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; if (snd_BUG_ON(!substream)) return -ENXIO; pcm = substream->pcm; if (!pcm->card->shutdown) snd_pcm_oss_sync(pcm_oss_file); mutex_lock(&pcm->open_mutex); snd_pcm_oss_release_file(pcm_oss_file); mutex_unlock(&pcm->open_mutex); wake_up(&pcm->open_wait); module_put(pcm->card->module); snd_card_file_remove(pcm->card, file); return 0; } static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct snd_pcm_oss_file *pcm_oss_file; int __user *p = (int __user *)arg; int res; pcm_oss_file = file->private_data; if (cmd == OSS_GETVERSION) return put_user(SNDRV_OSS_VERSION, p); if (cmd == OSS_ALSAEMULVER) return put_user(1, p); #if IS_REACHABLE(CONFIG_SND_MIXER_OSS) if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */ struct snd_pcm_substream *substream; int idx; for (idx = 0; idx < 2; ++idx) { substream = pcm_oss_file->streams[idx]; if (substream != NULL) break; } if (snd_BUG_ON(idx >= 2)) return -ENXIO; return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg); } #endif if (((cmd >> 8) & 0xff) != 'P') return -EINVAL; #ifdef OSS_DEBUG pr_debug("pcm_oss: ioctl = 0x%x\n", cmd); #endif switch (cmd) { case SNDCTL_DSP_RESET: return snd_pcm_oss_reset(pcm_oss_file); case SNDCTL_DSP_SYNC: return snd_pcm_oss_sync(pcm_oss_file); case SNDCTL_DSP_SPEED: if (get_user(res, p)) return -EFAULT; res = snd_pcm_oss_set_rate(pcm_oss_file, res); if (res < 0) return res; return put_user(res, p); case SOUND_PCM_READ_RATE: res = snd_pcm_oss_get_rate(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_STEREO: if (get_user(res, p)) return -EFAULT; res = res > 0 ? 2 : 1; res = snd_pcm_oss_set_channels(pcm_oss_file, res); if (res < 0) return res; return put_user(--res, p); case SNDCTL_DSP_GETBLKSIZE: res = snd_pcm_oss_get_block_size(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_SETFMT: if (get_user(res, p)) return -EFAULT; res = snd_pcm_oss_set_format(pcm_oss_file, res); if (res < 0) return res; return put_user(res, p); case SOUND_PCM_READ_BITS: res = snd_pcm_oss_get_format(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_CHANNELS: if (get_user(res, p)) return -EFAULT; res = snd_pcm_oss_set_channels(pcm_oss_file, res); if (res < 0) return res; return put_user(res, p); case SOUND_PCM_READ_CHANNELS: res = snd_pcm_oss_get_channels(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SOUND_PCM_WRITE_FILTER: case SOUND_PCM_READ_FILTER: return -EIO; case SNDCTL_DSP_POST: return snd_pcm_oss_post(pcm_oss_file); case SNDCTL_DSP_SUBDIVIDE: if (get_user(res, p)) return -EFAULT; res = snd_pcm_oss_set_subdivide(pcm_oss_file, res); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_SETFRAGMENT: if (get_user(res, p)) return -EFAULT; return snd_pcm_oss_set_fragment(pcm_oss_file, res); case SNDCTL_DSP_GETFMTS: res = snd_pcm_oss_get_formats(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_GETOSPACE: case SNDCTL_DSP_GETISPACE: return snd_pcm_oss_get_space(pcm_oss_file, cmd == SNDCTL_DSP_GETISPACE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, (struct audio_buf_info __user *) arg); case SNDCTL_DSP_NONBLOCK: return snd_pcm_oss_nonblock(file); case SNDCTL_DSP_GETCAPS: res = snd_pcm_oss_get_caps(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_GETTRIGGER: res = snd_pcm_oss_get_trigger(pcm_oss_file); if (res < 0) return res; return put_user(res, p); case SNDCTL_DSP_SETTRIGGER: if (get_user(res, p)) return -EFAULT; return snd_pcm_oss_set_trigger(pcm_oss_file, res); case SNDCTL_DSP_GETIPTR: case SNDCTL_DSP_GETOPTR: return snd_pcm_oss_get_ptr(pcm_oss_file, cmd == SNDCTL_DSP_GETIPTR ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, (struct count_info __user *) arg); case SNDCTL_DSP_MAPINBUF: case SNDCTL_DSP_MAPOUTBUF: return snd_pcm_oss_get_mapbuf(pcm_oss_file, cmd == SNDCTL_DSP_MAPINBUF ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, (struct buffmem_desc __user *) arg); case SNDCTL_DSP_SETSYNCRO: /* stop DMA now.. */ return 0; case SNDCTL_DSP_SETDUPLEX: if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX) return 0; return -EIO; case SNDCTL_DSP_GETODELAY: res = snd_pcm_oss_get_odelay(pcm_oss_file); if (res < 0) { /* it's for sure, some broken apps don't check for error codes */ put_user(0, p); return res; } return put_user(res, p); case SNDCTL_DSP_PROFILE: return 0; /* silently ignore */ default: pr_debug("pcm_oss: unknown command = 0x%x\n", cmd); } return -EINVAL; } #ifdef CONFIG_COMPAT /* all compatible */ static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) { /* * Everything is compatbile except SNDCTL_DSP_MAPINBUF/SNDCTL_DSP_MAPOUTBUF, * which are not implemented for the native case either */ return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); } #else #define snd_pcm_oss_ioctl_compat NULL #endif static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset) { struct snd_pcm_oss_file *pcm_oss_file; struct snd_pcm_substream *substream; pcm_oss_file = file->private_data; substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; if (substream == NULL) return -ENXIO; substream->f_flags = file->f_flags & O_NONBLOCK; #ifndef OSS_DEBUG return snd_pcm_oss_read1(substream, buf, count); #else { ssize_t res = snd_pcm_oss_read1(substream, buf, count); pcm_dbg(substream->pcm, "pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res); return res; } #endif } static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { struct snd_pcm_oss_file *pcm_oss_file; struct snd_pcm_substream *substream; long result; pcm_oss_file = file->private_data; substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; if (substream == NULL) return -ENXIO; substream->f_flags = file->f_flags & O_NONBLOCK; result = snd_pcm_oss_write1(substream, buf, count); #ifdef OSS_DEBUG pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result); #endif return result; } static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; if (atomic_read(&substream->mmap_count)) return runtime->oss.prev_hw_ptr_period != get_hw_ptr_period(runtime); else return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames; } static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; if (atomic_read(&substream->mmap_count)) return runtime->oss.prev_hw_ptr_period != get_hw_ptr_period(runtime); else return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames; } static __poll_t snd_pcm_oss_poll(struct file *file, poll_table * wait) { struct snd_pcm_oss_file *pcm_oss_file; __poll_t mask; struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL; pcm_oss_file = file->private_data; psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; mask = 0; if (psubstream != NULL) { struct snd_pcm_runtime *runtime = psubstream->runtime; poll_wait(file, &runtime->sleep, wait); snd_pcm_stream_lock_irq(psubstream); if (runtime->state != SNDRV_PCM_STATE_DRAINING && (runtime->state != SNDRV_PCM_STATE_RUNNING || snd_pcm_oss_playback_ready(psubstream))) mask |= EPOLLOUT | EPOLLWRNORM; snd_pcm_stream_unlock_irq(psubstream); } if (csubstream != NULL) { struct snd_pcm_runtime *runtime = csubstream->runtime; snd_pcm_state_t ostate; poll_wait(file, &runtime->sleep, wait); snd_pcm_stream_lock_irq(csubstream); ostate = runtime->state; if (ostate != SNDRV_PCM_STATE_RUNNING || snd_pcm_oss_capture_ready(csubstream)) mask |= EPOLLIN | EPOLLRDNORM; snd_pcm_stream_unlock_irq(csubstream); if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) { struct snd_pcm_oss_file ofile; memset(&ofile, 0, sizeof(ofile)); ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; runtime->oss.trigger = 0; snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT); } } return mask; } static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area) { struct snd_pcm_oss_file *pcm_oss_file; struct snd_pcm_substream *substream = NULL; struct snd_pcm_runtime *runtime; int err; #ifdef OSS_DEBUG pr_debug("pcm_oss: mmap begin\n"); #endif pcm_oss_file = file->private_data; switch ((area->vm_flags & (VM_READ | VM_WRITE))) { case VM_READ | VM_WRITE: substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; if (substream) break; fallthrough; case VM_READ: substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE]; break; case VM_WRITE: substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK]; break; default: return -EINVAL; } /* set VM_READ access as well to fix memset() routines that do reads before writes (to improve performance) */ vm_flags_set(area, VM_READ); if (substream == NULL) return -ENXIO; runtime = substream->runtime; if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID)) return -EIO; if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED) runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED; else return -EIO; if (runtime->oss.params) { /* use mutex_trylock() for params_lock for avoiding a deadlock * between mmap_lock and params_lock taken by * copy_from/to_user() in snd_pcm_oss_write/read() */ err = snd_pcm_oss_change_params(substream, true); if (err < 0) return err; } #ifdef CONFIG_SND_PCM_OSS_PLUGINS if (runtime->oss.plugin_first != NULL) return -EIO; #endif if (area->vm_pgoff != 0) return -EINVAL; err = snd_pcm_mmap_data(substream, file, area); if (err < 0) return err; runtime->oss.mmap_bytes = area->vm_end - area->vm_start; runtime->silence_threshold = 0; runtime->silence_size = 0; #ifdef OSS_DEBUG pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes); #endif /* In mmap mode we never stop */ runtime->stop_threshold = runtime->boundary; return 0; } #ifdef CONFIG_SND_VERBOSE_PROCFS /* * /proc interface */ static void snd_pcm_oss_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_pcm_str *pstr = entry->private_data; struct snd_pcm_oss_setup *setup = pstr->oss.setup_list; mutex_lock(&pstr->oss.setup_mutex); while (setup) { snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n", setup->task_name, setup->periods, setup->period_size, setup->disable ? " disable" : "", setup->direct ? " direct" : "", setup->block ? " block" : "", setup->nonblock ? " non-block" : "", setup->partialfrag ? " partial-frag" : "", setup->nosilence ? " no-silence" : ""); setup = setup->next; } mutex_unlock(&pstr->oss.setup_mutex); } static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr) { struct snd_pcm_oss_setup *setup, *setupn; for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL; setup; setup = setupn) { setupn = setup->next; kfree(setup->task_name); kfree(setup); } pstr->oss.setup_list = NULL; } static void snd_pcm_oss_proc_write(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_pcm_str *pstr = entry->private_data; char line[128], str[32], task_name[32]; const char *ptr; int idx1; struct snd_pcm_oss_setup *setup, *setup1, template; while (!snd_info_get_line(buffer, line, sizeof(line))) { mutex_lock(&pstr->oss.setup_mutex); memset(&template, 0, sizeof(template)); ptr = snd_info_get_str(task_name, line, sizeof(task_name)); if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) { snd_pcm_oss_proc_free_setup_list(pstr); mutex_unlock(&pstr->oss.setup_mutex); continue; } for (setup = pstr->oss.setup_list; setup; setup = setup->next) { if (!strcmp(setup->task_name, task_name)) { template = *setup; break; } } ptr = snd_info_get_str(str, ptr, sizeof(str)); template.periods = simple_strtoul(str, NULL, 10); ptr = snd_info_get_str(str, ptr, sizeof(str)); template.period_size = simple_strtoul(str, NULL, 10); for (idx1 = 31; idx1 >= 0; idx1--) if (template.period_size & (1 << idx1)) break; for (idx1--; idx1 >= 0; idx1--) template.period_size &= ~(1 << idx1); do { ptr = snd_info_get_str(str, ptr, sizeof(str)); if (!strcmp(str, "disable")) { template.disable = 1; } else if (!strcmp(str, "direct")) { template.direct = 1; } else if (!strcmp(str, "block")) { template.block = 1; } else if (!strcmp(str, "non-block")) { template.nonblock = 1; } else if (!strcmp(str, "partial-frag")) { template.partialfrag = 1; } else if (!strcmp(str, "no-silence")) { template.nosilence = 1; } else if (!strcmp(str, "buggy-ptr")) { template.buggyptr = 1; } } while (*str); if (setup == NULL) { setup = kmalloc(sizeof(*setup), GFP_KERNEL); if (! setup) { buffer->error = -ENOMEM; mutex_unlock(&pstr->oss.setup_mutex); return; } if (pstr->oss.setup_list == NULL) pstr->oss.setup_list = setup; else { for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next); setup1->next = setup; } template.task_name = kstrdup(task_name, GFP_KERNEL); if (! template.task_name) { kfree(setup); buffer->error = -ENOMEM; mutex_unlock(&pstr->oss.setup_mutex); return; } } *setup = template; mutex_unlock(&pstr->oss.setup_mutex); } } static void snd_pcm_oss_proc_init(struct snd_pcm *pcm) { int stream; for (stream = 0; stream < 2; ++stream) { struct snd_info_entry *entry; struct snd_pcm_str *pstr = &pcm->streams[stream]; if (pstr->substream_count == 0) continue; entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root); if (entry) { entry->content = SNDRV_INFO_CONTENT_TEXT; entry->mode = S_IFREG | 0644; entry->c.text.read = snd_pcm_oss_proc_read; entry->c.text.write = snd_pcm_oss_proc_write; entry->private_data = pstr; if (snd_info_register(entry) < 0) { snd_info_free_entry(entry); entry = NULL; } } pstr->oss.proc_entry = entry; } } static void snd_pcm_oss_proc_done(struct snd_pcm *pcm) { int stream; for (stream = 0; stream < 2; ++stream) { struct snd_pcm_str *pstr = &pcm->streams[stream]; snd_info_free_entry(pstr->oss.proc_entry); pstr->oss.proc_entry = NULL; snd_pcm_oss_proc_free_setup_list(pstr); } } #else /* !CONFIG_SND_VERBOSE_PROCFS */ static inline void snd_pcm_oss_proc_init(struct snd_pcm *pcm) { } static inline void snd_pcm_oss_proc_done(struct snd_pcm *pcm) { } #endif /* CONFIG_SND_VERBOSE_PROCFS */ /* * ENTRY functions */ static const struct file_operations snd_pcm_oss_f_reg = { .owner = THIS_MODULE, .read = snd_pcm_oss_read, .write = snd_pcm_oss_write, .open = snd_pcm_oss_open, .release = snd_pcm_oss_release, .llseek = no_llseek, .poll = snd_pcm_oss_poll, .unlocked_ioctl = snd_pcm_oss_ioctl, .compat_ioctl = snd_pcm_oss_ioctl_compat, .mmap = snd_pcm_oss_mmap, }; static void register_oss_dsp(struct snd_pcm *pcm, int index) { if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM, pcm->card, index, &snd_pcm_oss_f_reg, pcm) < 0) { pcm_err(pcm, "unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device); } } static int snd_pcm_oss_register_minor(struct snd_pcm *pcm) { pcm->oss.reg = 0; if (dsp_map[pcm->card->number] == (int)pcm->device) { char name[128]; int duplex; register_oss_dsp(pcm, 0); duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX)); sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : ""); #ifdef SNDRV_OSS_INFO_DEV_AUDIO snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number, name); #endif pcm->oss.reg++; pcm->oss.reg_mask |= 1; } if (adsp_map[pcm->card->number] == (int)pcm->device) { register_oss_dsp(pcm, 1); pcm->oss.reg++; pcm->oss.reg_mask |= 2; } if (pcm->oss.reg) snd_pcm_oss_proc_init(pcm); return 0; } static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm) { if (pcm->oss.reg) { if (pcm->oss.reg_mask & 1) { pcm->oss.reg_mask &= ~1; snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM, pcm->card, 0); } if (pcm->oss.reg_mask & 2) { pcm->oss.reg_mask &= ~2; snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM, pcm->card, 1); } if (dsp_map[pcm->card->number] == (int)pcm->device) { #ifdef SNDRV_OSS_INFO_DEV_AUDIO snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number); #endif } pcm->oss.reg = 0; } return 0; } static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm) { snd_pcm_oss_disconnect_minor(pcm); snd_pcm_oss_proc_done(pcm); return 0; } static struct snd_pcm_notify snd_pcm_oss_notify = { .n_register = snd_pcm_oss_register_minor, .n_disconnect = snd_pcm_oss_disconnect_minor, .n_unregister = snd_pcm_oss_unregister_minor, }; static int __init alsa_pcm_oss_init(void) { int i; int err; /* check device map table */ for (i = 0; i < SNDRV_CARDS; i++) { if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) { pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n", i, dsp_map[i]); dsp_map[i] = 0; } if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) { pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n", i, adsp_map[i]); adsp_map[i] = 1; } } err = snd_pcm_notify(&snd_pcm_oss_notify, 0); if (err < 0) return err; return 0; } static void __exit alsa_pcm_oss_exit(void) { snd_pcm_notify(&snd_pcm_oss_notify, 1); } module_init(alsa_pcm_oss_init) module_exit(alsa_pcm_oss_exit)
linux-master
sound/core/oss/pcm_oss.c
/* * Linear conversion Plug-In * Copyright (c) 2000 by Abramo Bagnara <[email protected]> * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include <linux/time.h> #include <sound/core.h> #include <sound/pcm.h> #include "pcm_plugin.h" static snd_pcm_sframes_t copy_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { unsigned int channel; unsigned int nchannels; if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) return -ENXIO; if (frames == 0) return 0; nchannels = plugin->src_format.channels; for (channel = 0; channel < nchannels; channel++) { if (snd_BUG_ON(src_channels->area.first % 8 || src_channels->area.step % 8)) return -ENXIO; if (snd_BUG_ON(dst_channels->area.first % 8 || dst_channels->area.step % 8)) return -ENXIO; if (!src_channels->enabled) { if (dst_channels->wanted) snd_pcm_area_silence(&dst_channels->area, 0, frames, plugin->dst_format.format); dst_channels->enabled = 0; continue; } dst_channels->enabled = 1; snd_pcm_area_copy(&src_channels->area, 0, &dst_channels->area, 0, frames, plugin->src_format.format); src_channels++; dst_channels++; } return frames; } int snd_pcm_plugin_build_copy(struct snd_pcm_substream *plug, struct snd_pcm_plugin_format *src_format, struct snd_pcm_plugin_format *dst_format, struct snd_pcm_plugin **r_plugin) { int err; struct snd_pcm_plugin *plugin; int width; if (snd_BUG_ON(!r_plugin)) return -ENXIO; *r_plugin = NULL; if (snd_BUG_ON(src_format->format != dst_format->format)) return -ENXIO; if (snd_BUG_ON(src_format->rate != dst_format->rate)) return -ENXIO; if (snd_BUG_ON(src_format->channels != dst_format->channels)) return -ENXIO; width = snd_pcm_format_physical_width(src_format->format); if (snd_BUG_ON(width <= 0)) return -ENXIO; err = snd_pcm_plugin_build(plug, "copy", src_format, dst_format, 0, &plugin); if (err < 0) return err; plugin->transfer = copy_transfer; *r_plugin = plugin; return 0; }
linux-master
sound/core/oss/copy.c
/* * PCM Plug-In shared (kernel/library) code * Copyright (c) 1999 by Jaroslav Kysela <[email protected]> * Copyright (c) 2000 by Abramo Bagnara <[email protected]> * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #if 0 #define PLUGIN_DEBUG #endif #include <linux/slab.h> #include <linux/time.h> #include <linux/vmalloc.h> #include <sound/core.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include "pcm_plugin.h" #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first) #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last) /* * because some cards might have rates "very close", we ignore * all "resampling" requests within +-5% */ static int rate_match(unsigned int src_rate, unsigned int dst_rate) { unsigned int low = (src_rate * 95) / 100; unsigned int high = (src_rate * 105) / 100; return dst_rate >= low && dst_rate <= high; } static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames) { struct snd_pcm_plugin_format *format; ssize_t width; size_t size; unsigned int channel; struct snd_pcm_plugin_channel *c; if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) { format = &plugin->src_format; } else { format = &plugin->dst_format; } width = snd_pcm_format_physical_width(format->format); if (width < 0) return width; size = array3_size(frames, format->channels, width); /* check for too large period size once again */ if (size > 1024 * 1024) return -ENOMEM; if (snd_BUG_ON(size % 8)) return -ENXIO; size /= 8; if (plugin->buf_frames < frames) { kvfree(plugin->buf); plugin->buf = kvzalloc(size, GFP_KERNEL); plugin->buf_frames = frames; } if (!plugin->buf) { plugin->buf_frames = 0; return -ENOMEM; } c = plugin->buf_channels; if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) { for (channel = 0; channel < format->channels; channel++, c++) { c->frames = frames; c->enabled = 1; c->wanted = 0; c->area.addr = plugin->buf; c->area.first = channel * width; c->area.step = format->channels * width; } } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) { if (snd_BUG_ON(size % format->channels)) return -EINVAL; size /= format->channels; for (channel = 0; channel < format->channels; channel++, c++) { c->frames = frames; c->enabled = 1; c->wanted = 0; c->area.addr = plugin->buf + (channel * size); c->area.first = 0; c->area.step = width; } } else return -EINVAL; return 0; } int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames) { int err; if (snd_BUG_ON(!snd_pcm_plug_first(plug))) return -ENXIO; if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) { struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug); while (plugin->next) { if (plugin->dst_frames) frames = plugin->dst_frames(plugin, frames); if ((snd_pcm_sframes_t)frames <= 0) return -ENXIO; plugin = plugin->next; err = snd_pcm_plugin_alloc(plugin, frames); if (err < 0) return err; } } else { struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug); while (plugin->prev) { if (plugin->src_frames) frames = plugin->src_frames(plugin, frames); if ((snd_pcm_sframes_t)frames <= 0) return -ENXIO; plugin = plugin->prev; err = snd_pcm_plugin_alloc(plugin, frames); if (err < 0) return err; } } return 0; } snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames, struct snd_pcm_plugin_channel **channels) { *channels = plugin->buf_channels; return frames; } int snd_pcm_plugin_build(struct snd_pcm_substream *plug, const char *name, struct snd_pcm_plugin_format *src_format, struct snd_pcm_plugin_format *dst_format, size_t extra, struct snd_pcm_plugin **ret) { struct snd_pcm_plugin *plugin; unsigned int channels; if (snd_BUG_ON(!plug)) return -ENXIO; if (snd_BUG_ON(!src_format || !dst_format)) return -ENXIO; plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL); if (plugin == NULL) return -ENOMEM; plugin->name = name; plugin->plug = plug; plugin->stream = snd_pcm_plug_stream(plug); plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; plugin->src_format = *src_format; plugin->src_width = snd_pcm_format_physical_width(src_format->format); snd_BUG_ON(plugin->src_width <= 0); plugin->dst_format = *dst_format; plugin->dst_width = snd_pcm_format_physical_width(dst_format->format); snd_BUG_ON(plugin->dst_width <= 0); if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) channels = src_format->channels; else channels = dst_format->channels; plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL); if (plugin->buf_channels == NULL) { snd_pcm_plugin_free(plugin); return -ENOMEM; } plugin->client_channels = snd_pcm_plugin_client_channels; *ret = plugin; return 0; } int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin) { if (! plugin) return 0; if (plugin->private_free) plugin->private_free(plugin); kfree(plugin->buf_channels); kvfree(plugin->buf); kfree(plugin); return 0; } static snd_pcm_sframes_t calc_dst_frames(struct snd_pcm_substream *plug, snd_pcm_sframes_t frames, bool check_size) { struct snd_pcm_plugin *plugin, *plugin_next; plugin = snd_pcm_plug_first(plug); while (plugin && frames > 0) { plugin_next = plugin->next; if (check_size && plugin->buf_frames && frames > plugin->buf_frames) frames = plugin->buf_frames; if (plugin->dst_frames) { frames = plugin->dst_frames(plugin, frames); if (frames < 0) return frames; } plugin = plugin_next; } return frames; } static snd_pcm_sframes_t calc_src_frames(struct snd_pcm_substream *plug, snd_pcm_sframes_t frames, bool check_size) { struct snd_pcm_plugin *plugin, *plugin_prev; plugin = snd_pcm_plug_last(plug); while (plugin && frames > 0) { plugin_prev = plugin->prev; if (plugin->src_frames) { frames = plugin->src_frames(plugin, frames); if (frames < 0) return frames; } if (check_size && plugin->buf_frames && frames > plugin->buf_frames) frames = plugin->buf_frames; plugin = plugin_prev; } return frames; } snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames) { if (snd_BUG_ON(!plug)) return -ENXIO; switch (snd_pcm_plug_stream(plug)) { case SNDRV_PCM_STREAM_PLAYBACK: return calc_src_frames(plug, drv_frames, false); case SNDRV_PCM_STREAM_CAPTURE: return calc_dst_frames(plug, drv_frames, false); default: snd_BUG(); return -EINVAL; } } snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames) { if (snd_BUG_ON(!plug)) return -ENXIO; switch (snd_pcm_plug_stream(plug)) { case SNDRV_PCM_STREAM_PLAYBACK: return calc_dst_frames(plug, clt_frames, false); case SNDRV_PCM_STREAM_CAPTURE: return calc_src_frames(plug, clt_frames, false); default: snd_BUG(); return -EINVAL; } } static int snd_pcm_plug_formats(const struct snd_mask *mask, snd_pcm_format_t format) { struct snd_mask formats = *mask; u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE); snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW); if (formats.bits[0] & lower_32_bits(linfmts)) formats.bits[0] |= lower_32_bits(linfmts); if (formats.bits[1] & upper_32_bits(linfmts)) formats.bits[1] |= upper_32_bits(linfmts); return snd_mask_test(&formats, (__force int)format); } static const snd_pcm_format_t preferred_formats[] = { SNDRV_PCM_FORMAT_S16_LE, SNDRV_PCM_FORMAT_S16_BE, SNDRV_PCM_FORMAT_U16_LE, SNDRV_PCM_FORMAT_U16_BE, SNDRV_PCM_FORMAT_S24_3LE, SNDRV_PCM_FORMAT_S24_3BE, SNDRV_PCM_FORMAT_U24_3LE, SNDRV_PCM_FORMAT_U24_3BE, SNDRV_PCM_FORMAT_S24_LE, SNDRV_PCM_FORMAT_S24_BE, SNDRV_PCM_FORMAT_U24_LE, SNDRV_PCM_FORMAT_U24_BE, SNDRV_PCM_FORMAT_S32_LE, SNDRV_PCM_FORMAT_S32_BE, SNDRV_PCM_FORMAT_U32_LE, SNDRV_PCM_FORMAT_U32_BE, SNDRV_PCM_FORMAT_S8, SNDRV_PCM_FORMAT_U8 }; snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format, const struct snd_mask *format_mask) { int i; if (snd_mask_test(format_mask, (__force int)format)) return format; if (!snd_pcm_plug_formats(format_mask, format)) return (__force snd_pcm_format_t)-EINVAL; if (snd_pcm_format_linear(format)) { unsigned int width = snd_pcm_format_width(format); int unsignd = snd_pcm_format_unsigned(format) > 0; int big = snd_pcm_format_big_endian(format) > 0; unsigned int badness, best = -1; snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1; for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) { snd_pcm_format_t f = preferred_formats[i]; unsigned int w; if (!snd_mask_test(format_mask, (__force int)f)) continue; w = snd_pcm_format_width(f); if (w >= width) badness = w - width; else badness = width - w + 32; badness += snd_pcm_format_unsigned(f) != unsignd; badness += snd_pcm_format_big_endian(f) != big; if (badness < best) { best_format = f; best = badness; } } if ((__force int)best_format >= 0) return best_format; else return (__force snd_pcm_format_t)-EINVAL; } else { switch (format) { case SNDRV_PCM_FORMAT_MU_LAW: for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) { snd_pcm_format_t format1 = preferred_formats[i]; if (snd_mask_test(format_mask, (__force int)format1)) return format1; } fallthrough; default: return (__force snd_pcm_format_t)-EINVAL; } } } int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug, struct snd_pcm_hw_params *params, struct snd_pcm_hw_params *slave_params) { struct snd_pcm_plugin_format tmpformat; struct snd_pcm_plugin_format dstformat; struct snd_pcm_plugin_format srcformat; snd_pcm_access_t src_access, dst_access; struct snd_pcm_plugin *plugin = NULL; int err; int stream = snd_pcm_plug_stream(plug); int slave_interleaved = (params_channels(slave_params) == 1 || params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED); switch (stream) { case SNDRV_PCM_STREAM_PLAYBACK: dstformat.format = params_format(slave_params); dstformat.rate = params_rate(slave_params); dstformat.channels = params_channels(slave_params); srcformat.format = params_format(params); srcformat.rate = params_rate(params); srcformat.channels = params_channels(params); src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED : SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); break; case SNDRV_PCM_STREAM_CAPTURE: dstformat.format = params_format(params); dstformat.rate = params_rate(params); dstformat.channels = params_channels(params); srcformat.format = params_format(slave_params); srcformat.rate = params_rate(slave_params); srcformat.channels = params_channels(slave_params); src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED : SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; break; default: snd_BUG(); return -EINVAL; } tmpformat = srcformat; pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", srcformat.format, srcformat.rate, srcformat.channels); pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", dstformat.format, dstformat.rate, dstformat.channels); /* Format change (linearization) */ if (! rate_match(srcformat.rate, dstformat.rate) && ! snd_pcm_format_linear(srcformat.format)) { if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW) return -EINVAL; tmpformat.format = SNDRV_PCM_FORMAT_S16; err = snd_pcm_plugin_build_mulaw(plug, &srcformat, &tmpformat, &plugin); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } srcformat = tmpformat; src_access = dst_access; } /* channels reduction */ if (srcformat.channels > dstformat.channels) { tmpformat.channels = dstformat.channels; err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin); pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } srcformat = tmpformat; src_access = dst_access; } /* rate resampling */ if (!rate_match(srcformat.rate, dstformat.rate)) { if (srcformat.format != SNDRV_PCM_FORMAT_S16) { /* convert to S16 for resampling */ tmpformat.format = SNDRV_PCM_FORMAT_S16; err = snd_pcm_plugin_build_linear(plug, &srcformat, &tmpformat, &plugin); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } srcformat = tmpformat; src_access = dst_access; } tmpformat.rate = dstformat.rate; err = snd_pcm_plugin_build_rate(plug, &srcformat, &tmpformat, &plugin); pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } srcformat = tmpformat; src_access = dst_access; } /* format change */ if (srcformat.format != dstformat.format) { tmpformat.format = dstformat.format; if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW || tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) { err = snd_pcm_plugin_build_mulaw(plug, &srcformat, &tmpformat, &plugin); } else if (snd_pcm_format_linear(srcformat.format) && snd_pcm_format_linear(tmpformat.format)) { err = snd_pcm_plugin_build_linear(plug, &srcformat, &tmpformat, &plugin); } else return -EINVAL; pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } srcformat = tmpformat; src_access = dst_access; } /* channels extension */ if (srcformat.channels < dstformat.channels) { tmpformat.channels = dstformat.channels; err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin); pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } srcformat = tmpformat; src_access = dst_access; } /* de-interleave */ if (src_access != dst_access) { err = snd_pcm_plugin_build_copy(plug, &srcformat, &tmpformat, &plugin); pdprintf("interleave change (copy: returns %i)\n", err); if (err < 0) return err; err = snd_pcm_plugin_append(plugin); if (err < 0) { snd_pcm_plugin_free(plugin); return err; } } return 0; } snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug, char *buf, snd_pcm_uframes_t count, struct snd_pcm_plugin_channel **channels) { struct snd_pcm_plugin *plugin; struct snd_pcm_plugin_channel *v; struct snd_pcm_plugin_format *format; int width, nchannels, channel; int stream = snd_pcm_plug_stream(plug); if (snd_BUG_ON(!buf)) return -ENXIO; if (stream == SNDRV_PCM_STREAM_PLAYBACK) { plugin = snd_pcm_plug_first(plug); format = &plugin->src_format; } else { plugin = snd_pcm_plug_last(plug); format = &plugin->dst_format; } v = plugin->buf_channels; *channels = v; width = snd_pcm_format_physical_width(format->format); if (width < 0) return width; nchannels = format->channels; if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && format->channels > 1)) return -ENXIO; for (channel = 0; channel < nchannels; channel++, v++) { v->frames = count; v->enabled = 1; v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE); v->area.addr = buf; v->area.first = channel * width; v->area.step = nchannels * width; } return count; } snd_pcm_sframes_t snd_pcm_plug_write_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *src_channels, snd_pcm_uframes_t size) { struct snd_pcm_plugin *plugin, *next; struct snd_pcm_plugin_channel *dst_channels; int err; snd_pcm_sframes_t frames = size; plugin = snd_pcm_plug_first(plug); while (plugin) { if (frames <= 0) return frames; next = plugin->next; if (next) { snd_pcm_sframes_t frames1 = frames; if (plugin->dst_frames) { frames1 = plugin->dst_frames(plugin, frames); if (frames1 <= 0) return frames1; } err = next->client_channels(next, frames1, &dst_channels); if (err < 0) return err; if (err != frames1) { frames = err; if (plugin->src_frames) { frames = plugin->src_frames(plugin, frames1); if (frames <= 0) return frames; } } } else dst_channels = NULL; pdprintf("write plugin: %s, %li\n", plugin->name, frames); frames = plugin->transfer(plugin, src_channels, dst_channels, frames); if (frames < 0) return frames; src_channels = dst_channels; plugin = next; } return calc_src_frames(plug, frames, true); } snd_pcm_sframes_t snd_pcm_plug_read_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *dst_channels_final, snd_pcm_uframes_t size) { struct snd_pcm_plugin *plugin, *next; struct snd_pcm_plugin_channel *src_channels, *dst_channels; snd_pcm_sframes_t frames = size; int err; frames = calc_src_frames(plug, frames, true); if (frames < 0) return frames; src_channels = NULL; plugin = snd_pcm_plug_first(plug); while (plugin && frames > 0) { next = plugin->next; if (next) { err = plugin->client_channels(plugin, frames, &dst_channels); if (err < 0) return err; frames = err; } else { dst_channels = dst_channels_final; } pdprintf("read plugin: %s, %li\n", plugin->name, frames); frames = plugin->transfer(plugin, src_channels, dst_channels, frames); if (frames < 0) return frames; plugin = next; src_channels = dst_channels; } return frames; } int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset, size_t samples, snd_pcm_format_t format) { /* FIXME: sub byte resolution and odd dst_offset */ unsigned char *dst; unsigned int dst_step; int width; const unsigned char *silence; if (!dst_area->addr) return 0; dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8; width = snd_pcm_format_physical_width(format); if (width <= 0) return -EINVAL; if (dst_area->step == (unsigned int) width && width >= 8) return snd_pcm_format_set_silence(format, dst, samples); silence = snd_pcm_format_silence_64(format); if (! silence) return -EINVAL; dst_step = dst_area->step / 8; if (width == 4) { /* Ima ADPCM */ int dstbit = dst_area->first % 8; int dstbit_step = dst_area->step % 8; while (samples-- > 0) { if (dstbit) *dst &= 0xf0; else *dst &= 0x0f; dst += dst_step; dstbit += dstbit_step; if (dstbit == 8) { dst++; dstbit = 0; } } } else { width /= 8; while (samples-- > 0) { memcpy(dst, silence, width); dst += dst_step; } } return 0; } int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset, const struct snd_pcm_channel_area *dst_area, size_t dst_offset, size_t samples, snd_pcm_format_t format) { /* FIXME: sub byte resolution and odd dst_offset */ char *src, *dst; int width; int src_step, dst_step; src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8; if (!src_area->addr) return snd_pcm_area_silence(dst_area, dst_offset, samples, format); dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8; if (!dst_area->addr) return 0; width = snd_pcm_format_physical_width(format); if (width <= 0) return -EINVAL; if (src_area->step == (unsigned int) width && dst_area->step == (unsigned int) width && width >= 8) { size_t bytes = samples * width / 8; memcpy(dst, src, bytes); return 0; } src_step = src_area->step / 8; dst_step = dst_area->step / 8; if (width == 4) { /* Ima ADPCM */ int srcbit = src_area->first % 8; int srcbit_step = src_area->step % 8; int dstbit = dst_area->first % 8; int dstbit_step = dst_area->step % 8; while (samples-- > 0) { unsigned char srcval; if (srcbit) srcval = *src & 0x0f; else srcval = (*src & 0xf0) >> 4; if (dstbit) *dst = (*dst & 0xf0) | srcval; else *dst = (*dst & 0x0f) | (srcval << 4); src += src_step; srcbit += srcbit_step; if (srcbit == 8) { src++; srcbit = 0; } dst += dst_step; dstbit += dstbit_step; if (dstbit == 8) { dst++; dstbit = 0; } } } else { width /= 8; while (samples-- > 0) { memcpy(dst, src, width); src += src_step; dst += dst_step; } } return 0; }
linux-master
sound/core/oss/pcm_plugin.c
/* * Mu-Law conversion Plug-In Interface * Copyright (c) 1999 by Jaroslav Kysela <[email protected]> * Uros Bizjak <[email protected]> * * Based on reference implementation by Sun Microsystems, Inc. * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include <linux/time.h> #include <sound/core.h> #include <sound/pcm.h> #include "pcm_plugin.h" #define SIGN_BIT (0x80) /* Sign bit for a u-law byte. */ #define QUANT_MASK (0xf) /* Quantization field mask. */ #define NSEGS (8) /* Number of u-law segments. */ #define SEG_SHIFT (4) /* Left shift for segment number. */ #define SEG_MASK (0x70) /* Segment field mask. */ static inline int val_seg(int val) { int r = 0; val >>= 7; if (val & 0xf0) { val >>= 4; r += 4; } if (val & 0x0c) { val >>= 2; r += 2; } if (val & 0x02) r += 1; return r; } #define BIAS (0x84) /* Bias for linear code. */ /* * linear2ulaw() - Convert a linear PCM value to u-law * * In order to simplify the encoding process, the original linear magnitude * is biased by adding 33 which shifts the encoding range from (0 - 8158) to * (33 - 8191). The result can be seen in the following encoding table: * * Biased Linear Input Code Compressed Code * ------------------------ --------------- * 00000001wxyza 000wxyz * 0000001wxyzab 001wxyz * 000001wxyzabc 010wxyz * 00001wxyzabcd 011wxyz * 0001wxyzabcde 100wxyz * 001wxyzabcdef 101wxyz * 01wxyzabcdefg 110wxyz * 1wxyzabcdefgh 111wxyz * * Each biased linear code has a leading 1 which identifies the segment * number. The value of the segment number is equal to 7 minus the number * of leading 0's. The quantization interval is directly available as the * four bits wxyz. * The trailing bits (a - h) are ignored. * * Ordinarily the complement of the resulting code word is used for * transmission, and so the code word is complemented before it is returned. * * For further information see John C. Bellamy's Digital Telephony, 1982, * John Wiley & Sons, pps 98-111 and 472-476. */ static unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */ { int mask; int seg; unsigned char uval; /* Get the sign and the magnitude of the value. */ if (pcm_val < 0) { pcm_val = BIAS - pcm_val; mask = 0x7F; } else { pcm_val += BIAS; mask = 0xFF; } if (pcm_val > 0x7FFF) pcm_val = 0x7FFF; /* Convert the scaled magnitude to segment number. */ seg = val_seg(pcm_val); /* * Combine the sign, segment, quantization bits; * and complement the code word. */ uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF); return uval ^ mask; } /* * ulaw2linear() - Convert a u-law value to 16-bit linear PCM * * First, a biased linear code is derived from the code word. An unbiased * output can then be obtained by subtracting 33 from the biased code. * * Note that this function expects to be passed the complement of the * original code word. This is in keeping with ISDN conventions. */ static int ulaw2linear(unsigned char u_val) { int t; /* Complement to obtain normal u-law value. */ u_val = ~u_val; /* * Extract and bias the quantization bits. Then * shift up by the segment number and subtract out the bias. */ t = ((u_val & QUANT_MASK) << 3) + BIAS; t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); } /* * Basic Mu-Law plugin */ typedef void (*mulaw_f)(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames); struct mulaw_priv { mulaw_f func; int cvt_endian; /* need endian conversion? */ unsigned int native_ofs; /* byte offset in native format */ unsigned int copy_ofs; /* byte offset in s16 format */ unsigned int native_bytes; /* byte size of the native format */ unsigned int copy_bytes; /* bytes to copy per conversion */ u16 flip; /* MSB flip for signedness, done after endian conversion */ }; static inline void cvt_s16_to_native(struct mulaw_priv *data, unsigned char *dst, u16 sample) { sample ^= data->flip; if (data->cvt_endian) sample = swab16(sample); if (data->native_bytes > data->copy_bytes) memset(dst, 0, data->native_bytes); memcpy(dst + data->native_ofs, (char *)&sample + data->copy_ofs, data->copy_bytes); } static void mulaw_decode(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; int channel; int nchannels = plugin->src_format.channels; for (channel = 0; channel < nchannels; ++channel) { char *src; char *dst; int src_step, dst_step; snd_pcm_uframes_t frames1; if (!src_channels[channel].enabled) { if (dst_channels[channel].wanted) snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format); dst_channels[channel].enabled = 0; continue; } dst_channels[channel].enabled = 1; src = src_channels[channel].area.addr + src_channels[channel].area.first / 8; dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8; src_step = src_channels[channel].area.step / 8; dst_step = dst_channels[channel].area.step / 8; frames1 = frames; while (frames1-- > 0) { signed short sample = ulaw2linear(*src); cvt_s16_to_native(data, dst, sample); src += src_step; dst += dst_step; } } } static inline signed short cvt_native_to_s16(struct mulaw_priv *data, unsigned char *src) { u16 sample = 0; memcpy((char *)&sample + data->copy_ofs, src + data->native_ofs, data->copy_bytes); if (data->cvt_endian) sample = swab16(sample); sample ^= data->flip; return (signed short)sample; } static void mulaw_encode(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; int channel; int nchannels = plugin->src_format.channels; for (channel = 0; channel < nchannels; ++channel) { char *src; char *dst; int src_step, dst_step; snd_pcm_uframes_t frames1; if (!src_channels[channel].enabled) { if (dst_channels[channel].wanted) snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format); dst_channels[channel].enabled = 0; continue; } dst_channels[channel].enabled = 1; src = src_channels[channel].area.addr + src_channels[channel].area.first / 8; dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8; src_step = src_channels[channel].area.step / 8; dst_step = dst_channels[channel].area.step / 8; frames1 = frames; while (frames1-- > 0) { signed short sample = cvt_native_to_s16(data, src); *dst = linear2ulaw(sample); src += src_step; dst += dst_step; } } } static snd_pcm_sframes_t mulaw_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { struct mulaw_priv *data; if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) return -ENXIO; if (frames == 0) return 0; #ifdef CONFIG_SND_DEBUG { unsigned int channel; for (channel = 0; channel < plugin->src_format.channels; channel++) { if (snd_BUG_ON(src_channels[channel].area.first % 8 || src_channels[channel].area.step % 8)) return -ENXIO; if (snd_BUG_ON(dst_channels[channel].area.first % 8 || dst_channels[channel].area.step % 8)) return -ENXIO; } } #endif if (frames > dst_channels[0].frames) frames = dst_channels[0].frames; data = (struct mulaw_priv *)plugin->extra_data; data->func(plugin, src_channels, dst_channels, frames); return frames; } static void init_data(struct mulaw_priv *data, snd_pcm_format_t format) { #ifdef SNDRV_LITTLE_ENDIAN data->cvt_endian = snd_pcm_format_big_endian(format) > 0; #else data->cvt_endian = snd_pcm_format_little_endian(format) > 0; #endif if (!snd_pcm_format_signed(format)) data->flip = 0x8000; data->native_bytes = snd_pcm_format_physical_width(format) / 8; data->copy_bytes = data->native_bytes < 2 ? 1 : 2; if (snd_pcm_format_little_endian(format)) { data->native_ofs = data->native_bytes - data->copy_bytes; data->copy_ofs = 2 - data->copy_bytes; } else { /* S24 in 4bytes need an 1 byte offset */ data->native_ofs = data->native_bytes - snd_pcm_format_width(format) / 8; } } int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug, struct snd_pcm_plugin_format *src_format, struct snd_pcm_plugin_format *dst_format, struct snd_pcm_plugin **r_plugin) { int err; struct mulaw_priv *data; struct snd_pcm_plugin *plugin; struct snd_pcm_plugin_format *format; mulaw_f func; if (snd_BUG_ON(!r_plugin)) return -ENXIO; *r_plugin = NULL; if (snd_BUG_ON(src_format->rate != dst_format->rate)) return -ENXIO; if (snd_BUG_ON(src_format->channels != dst_format->channels)) return -ENXIO; if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) { format = src_format; func = mulaw_encode; } else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) { format = dst_format; func = mulaw_decode; } else { snd_BUG(); return -EINVAL; } if (!snd_pcm_format_linear(format->format)) return -EINVAL; err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion", src_format, dst_format, sizeof(struct mulaw_priv), &plugin); if (err < 0) return err; data = (struct mulaw_priv *)plugin->extra_data; data->func = func; init_data(data, format->format); plugin->transfer = mulaw_transfer; *r_plugin = plugin; return 0; }
linux-master
sound/core/oss/mulaw.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * OSS emulation layer for the mixer interface * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/slab.h> #include <linux/time.h> #include <linux/string.h> #include <linux/module.h> #include <linux/compat.h> #include <sound/core.h> #include <sound/minors.h> #include <sound/control.h> #include <sound/info.h> #include <sound/mixer_oss.h> #include <linux/soundcard.h> #define OSS_ALSAEMULVER _SIOR ('M', 249, int) MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Mixer OSS emulation for ALSA."); MODULE_LICENSE("GPL"); MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MIXER); static int snd_mixer_oss_open(struct inode *inode, struct file *file) { struct snd_card *card; struct snd_mixer_oss_file *fmixer; int err; err = nonseekable_open(inode, file); if (err < 0) return err; card = snd_lookup_oss_minor_data(iminor(inode), SNDRV_OSS_DEVICE_TYPE_MIXER); if (card == NULL) return -ENODEV; if (card->mixer_oss == NULL) { snd_card_unref(card); return -ENODEV; } err = snd_card_file_add(card, file); if (err < 0) { snd_card_unref(card); return err; } fmixer = kzalloc(sizeof(*fmixer), GFP_KERNEL); if (fmixer == NULL) { snd_card_file_remove(card, file); snd_card_unref(card); return -ENOMEM; } fmixer->card = card; fmixer->mixer = card->mixer_oss; file->private_data = fmixer; if (!try_module_get(card->module)) { kfree(fmixer); snd_card_file_remove(card, file); snd_card_unref(card); return -EFAULT; } snd_card_unref(card); return 0; } static int snd_mixer_oss_release(struct inode *inode, struct file *file) { struct snd_mixer_oss_file *fmixer; if (file->private_data) { fmixer = file->private_data; module_put(fmixer->card->module); snd_card_file_remove(fmixer->card, file); kfree(fmixer); } return 0; } static int snd_mixer_oss_info(struct snd_mixer_oss_file *fmixer, mixer_info __user *_info) { struct snd_card *card = fmixer->card; struct snd_mixer_oss *mixer = fmixer->mixer; struct mixer_info info; memset(&info, 0, sizeof(info)); strscpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id)); strscpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name)); info.modify_counter = card->mixer_oss_change_count; if (copy_to_user(_info, &info, sizeof(info))) return -EFAULT; return 0; } static int snd_mixer_oss_info_obsolete(struct snd_mixer_oss_file *fmixer, _old_mixer_info __user *_info) { struct snd_card *card = fmixer->card; struct snd_mixer_oss *mixer = fmixer->mixer; _old_mixer_info info; memset(&info, 0, sizeof(info)); strscpy(info.id, mixer && mixer->id[0] ? mixer->id : card->driver, sizeof(info.id)); strscpy(info.name, mixer && mixer->name[0] ? mixer->name : card->mixername, sizeof(info.name)); if (copy_to_user(_info, &info, sizeof(info))) return -EFAULT; return 0; } static int snd_mixer_oss_caps(struct snd_mixer_oss_file *fmixer) { struct snd_mixer_oss *mixer = fmixer->mixer; int result = 0; if (mixer == NULL) return -EIO; if (mixer->get_recsrc && mixer->put_recsrc) result |= SOUND_CAP_EXCL_INPUT; return result; } static int snd_mixer_oss_devmask(struct snd_mixer_oss_file *fmixer) { struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_mixer_oss_slot *pslot; int result = 0, chn; if (mixer == NULL) return -EIO; mutex_lock(&mixer->reg_mutex); for (chn = 0; chn < 31; chn++) { pslot = &mixer->slots[chn]; if (pslot->put_volume || pslot->put_recsrc) result |= 1 << chn; } mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_stereodevs(struct snd_mixer_oss_file *fmixer) { struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_mixer_oss_slot *pslot; int result = 0, chn; if (mixer == NULL) return -EIO; mutex_lock(&mixer->reg_mutex); for (chn = 0; chn < 31; chn++) { pslot = &mixer->slots[chn]; if (pslot->put_volume && pslot->stereo) result |= 1 << chn; } mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_recmask(struct snd_mixer_oss_file *fmixer) { struct snd_mixer_oss *mixer = fmixer->mixer; int result = 0; if (mixer == NULL) return -EIO; mutex_lock(&mixer->reg_mutex); if (mixer->put_recsrc && mixer->get_recsrc) { /* exclusive */ result = mixer->mask_recsrc; } else { struct snd_mixer_oss_slot *pslot; int chn; for (chn = 0; chn < 31; chn++) { pslot = &mixer->slots[chn]; if (pslot->put_recsrc) result |= 1 << chn; } } mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_get_recsrc(struct snd_mixer_oss_file *fmixer) { struct snd_mixer_oss *mixer = fmixer->mixer; int result = 0; if (mixer == NULL) return -EIO; mutex_lock(&mixer->reg_mutex); if (mixer->put_recsrc && mixer->get_recsrc) { /* exclusive */ unsigned int index; result = mixer->get_recsrc(fmixer, &index); if (result < 0) goto unlock; result = 1 << index; } else { struct snd_mixer_oss_slot *pslot; int chn; for (chn = 0; chn < 31; chn++) { pslot = &mixer->slots[chn]; if (pslot->get_recsrc) { int active = 0; pslot->get_recsrc(fmixer, pslot, &active); if (active) result |= 1 << chn; } } } mixer->oss_recsrc = result; unlock: mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_set_recsrc(struct snd_mixer_oss_file *fmixer, int recsrc) { struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_mixer_oss_slot *pslot; int chn, active; unsigned int index; int result = 0; if (mixer == NULL) return -EIO; mutex_lock(&mixer->reg_mutex); if (mixer->get_recsrc && mixer->put_recsrc) { /* exclusive input */ if (recsrc & ~mixer->oss_recsrc) recsrc &= ~mixer->oss_recsrc; mixer->put_recsrc(fmixer, ffz(~recsrc)); mixer->get_recsrc(fmixer, &index); result = 1 << index; } for (chn = 0; chn < 31; chn++) { pslot = &mixer->slots[chn]; if (pslot->put_recsrc) { active = (recsrc & (1 << chn)) ? 1 : 0; pslot->put_recsrc(fmixer, pslot, active); } } if (! result) { for (chn = 0; chn < 31; chn++) { pslot = &mixer->slots[chn]; if (pslot->get_recsrc) { active = 0; pslot->get_recsrc(fmixer, pslot, &active); if (active) result |= 1 << chn; } } } mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_get_volume(struct snd_mixer_oss_file *fmixer, int slot) { struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_mixer_oss_slot *pslot; int result = 0, left, right; if (mixer == NULL || slot > 30) return -EIO; mutex_lock(&mixer->reg_mutex); pslot = &mixer->slots[slot]; left = pslot->volume[0]; right = pslot->volume[1]; if (pslot->get_volume) result = pslot->get_volume(fmixer, pslot, &left, &right); if (!pslot->stereo) right = left; if (snd_BUG_ON(left < 0 || left > 100)) { result = -EIO; goto unlock; } if (snd_BUG_ON(right < 0 || right > 100)) { result = -EIO; goto unlock; } if (result >= 0) { pslot->volume[0] = left; pslot->volume[1] = right; result = (left & 0xff) | ((right & 0xff) << 8); } unlock: mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_set_volume(struct snd_mixer_oss_file *fmixer, int slot, int volume) { struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_mixer_oss_slot *pslot; int result = 0, left = volume & 0xff, right = (volume >> 8) & 0xff; if (mixer == NULL || slot > 30) return -EIO; mutex_lock(&mixer->reg_mutex); pslot = &mixer->slots[slot]; if (left > 100) left = 100; if (right > 100) right = 100; if (!pslot->stereo) right = left; if (pslot->put_volume) result = pslot->put_volume(fmixer, pslot, left, right); if (result < 0) goto unlock; pslot->volume[0] = left; pslot->volume[1] = right; result = (left & 0xff) | ((right & 0xff) << 8); unlock: mutex_unlock(&mixer->reg_mutex); return result; } static int snd_mixer_oss_ioctl1(struct snd_mixer_oss_file *fmixer, unsigned int cmd, unsigned long arg) { void __user *argp = (void __user *)arg; int __user *p = argp; int tmp; if (snd_BUG_ON(!fmixer)) return -ENXIO; if (((cmd >> 8) & 0xff) == 'M') { switch (cmd) { case SOUND_MIXER_INFO: return snd_mixer_oss_info(fmixer, argp); case SOUND_OLD_MIXER_INFO: return snd_mixer_oss_info_obsolete(fmixer, argp); case SOUND_MIXER_WRITE_RECSRC: if (get_user(tmp, p)) return -EFAULT; tmp = snd_mixer_oss_set_recsrc(fmixer, tmp); if (tmp < 0) return tmp; return put_user(tmp, p); case OSS_GETVERSION: return put_user(SNDRV_OSS_VERSION, p); case OSS_ALSAEMULVER: return put_user(1, p); case SOUND_MIXER_READ_DEVMASK: tmp = snd_mixer_oss_devmask(fmixer); if (tmp < 0) return tmp; return put_user(tmp, p); case SOUND_MIXER_READ_STEREODEVS: tmp = snd_mixer_oss_stereodevs(fmixer); if (tmp < 0) return tmp; return put_user(tmp, p); case SOUND_MIXER_READ_RECMASK: tmp = snd_mixer_oss_recmask(fmixer); if (tmp < 0) return tmp; return put_user(tmp, p); case SOUND_MIXER_READ_CAPS: tmp = snd_mixer_oss_caps(fmixer); if (tmp < 0) return tmp; return put_user(tmp, p); case SOUND_MIXER_READ_RECSRC: tmp = snd_mixer_oss_get_recsrc(fmixer); if (tmp < 0) return tmp; return put_user(tmp, p); } } if (cmd & SIOC_IN) { if (get_user(tmp, p)) return -EFAULT; tmp = snd_mixer_oss_set_volume(fmixer, cmd & 0xff, tmp); if (tmp < 0) return tmp; return put_user(tmp, p); } else if (cmd & SIOC_OUT) { tmp = snd_mixer_oss_get_volume(fmixer, cmd & 0xff); if (tmp < 0) return tmp; return put_user(tmp, p); } return -ENXIO; } static long snd_mixer_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { return snd_mixer_oss_ioctl1(file->private_data, cmd, arg); } int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg) { struct snd_mixer_oss_file fmixer; if (snd_BUG_ON(!card)) return -ENXIO; if (card->mixer_oss == NULL) return -ENXIO; memset(&fmixer, 0, sizeof(fmixer)); fmixer.card = card; fmixer.mixer = card->mixer_oss; return snd_mixer_oss_ioctl1(&fmixer, cmd, arg); } EXPORT_SYMBOL(snd_mixer_oss_ioctl_card); #ifdef CONFIG_COMPAT /* all compatible */ static long snd_mixer_oss_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) { return snd_mixer_oss_ioctl1(file->private_data, cmd, (unsigned long)compat_ptr(arg)); } #else #define snd_mixer_oss_ioctl_compat NULL #endif /* * REGISTRATION PART */ static const struct file_operations snd_mixer_oss_f_ops = { .owner = THIS_MODULE, .open = snd_mixer_oss_open, .release = snd_mixer_oss_release, .llseek = no_llseek, .unlocked_ioctl = snd_mixer_oss_ioctl, .compat_ioctl = snd_mixer_oss_ioctl_compat, }; /* * utilities */ static long snd_mixer_oss_conv(long val, long omin, long omax, long nmin, long nmax) { long orange = omax - omin, nrange = nmax - nmin; if (orange == 0) return 0; return DIV_ROUND_CLOSEST(nrange * (val - omin), orange) + nmin; } /* convert from alsa native to oss values (0-100) */ static long snd_mixer_oss_conv1(long val, long min, long max, int *old) { if (val == snd_mixer_oss_conv(*old, 0, 100, min, max)) return *old; return snd_mixer_oss_conv(val, min, max, 0, 100); } /* convert from oss to alsa native values */ static long snd_mixer_oss_conv2(long val, long min, long max) { return snd_mixer_oss_conv(val, 0, 100, min, max); } #if 0 static void snd_mixer_oss_recsrce_set(struct snd_card *card, int slot) { struct snd_mixer_oss *mixer = card->mixer_oss; if (mixer) mixer->mask_recsrc |= 1 << slot; } static int snd_mixer_oss_recsrce_get(struct snd_card *card, int slot) { struct snd_mixer_oss *mixer = card->mixer_oss; if (mixer && (mixer->mask_recsrc & (1 << slot))) return 1; return 0; } #endif #define SNDRV_MIXER_OSS_SIGNATURE 0x65999250 #define SNDRV_MIXER_OSS_ITEM_GLOBAL 0 #define SNDRV_MIXER_OSS_ITEM_GSWITCH 1 #define SNDRV_MIXER_OSS_ITEM_GROUTE 2 #define SNDRV_MIXER_OSS_ITEM_GVOLUME 3 #define SNDRV_MIXER_OSS_ITEM_PSWITCH 4 #define SNDRV_MIXER_OSS_ITEM_PROUTE 5 #define SNDRV_MIXER_OSS_ITEM_PVOLUME 6 #define SNDRV_MIXER_OSS_ITEM_CSWITCH 7 #define SNDRV_MIXER_OSS_ITEM_CROUTE 8 #define SNDRV_MIXER_OSS_ITEM_CVOLUME 9 #define SNDRV_MIXER_OSS_ITEM_CAPTURE 10 #define SNDRV_MIXER_OSS_ITEM_COUNT 11 #define SNDRV_MIXER_OSS_PRESENT_GLOBAL (1<<0) #define SNDRV_MIXER_OSS_PRESENT_GSWITCH (1<<1) #define SNDRV_MIXER_OSS_PRESENT_GROUTE (1<<2) #define SNDRV_MIXER_OSS_PRESENT_GVOLUME (1<<3) #define SNDRV_MIXER_OSS_PRESENT_PSWITCH (1<<4) #define SNDRV_MIXER_OSS_PRESENT_PROUTE (1<<5) #define SNDRV_MIXER_OSS_PRESENT_PVOLUME (1<<6) #define SNDRV_MIXER_OSS_PRESENT_CSWITCH (1<<7) #define SNDRV_MIXER_OSS_PRESENT_CROUTE (1<<8) #define SNDRV_MIXER_OSS_PRESENT_CVOLUME (1<<9) #define SNDRV_MIXER_OSS_PRESENT_CAPTURE (1<<10) struct slot { unsigned int signature; unsigned int present; unsigned int channels; unsigned int numid[SNDRV_MIXER_OSS_ITEM_COUNT]; unsigned int capture_item; const struct snd_mixer_oss_assign_table *assigned; unsigned int allocated: 1; }; #define ID_UNKNOWN ((unsigned int)-1) static struct snd_kcontrol *snd_mixer_oss_test_id(struct snd_mixer_oss *mixer, const char *name, int index) { struct snd_card *card = mixer->card; struct snd_ctl_elem_id id; memset(&id, 0, sizeof(id)); id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; strscpy(id.name, name, sizeof(id.name)); id.index = index; return snd_ctl_find_id_locked(card, &id); } static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, unsigned int numid, int *left, int *right) { struct snd_ctl_elem_info *uinfo; struct snd_ctl_elem_value *uctl; struct snd_kcontrol *kctl; struct snd_card *card = fmixer->card; if (numid == ID_UNKNOWN) return; down_read(&card->controls_rwsem); kctl = snd_ctl_find_numid_locked(card, numid); if (!kctl) { up_read(&card->controls_rwsem); return; } uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); uctl = kzalloc(sizeof(*uctl), GFP_KERNEL); if (uinfo == NULL || uctl == NULL) goto __unalloc; if (kctl->info(kctl, uinfo)) goto __unalloc; if (kctl->get(kctl, uctl)) goto __unalloc; if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN && uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1) goto __unalloc; *left = snd_mixer_oss_conv1(uctl->value.integer.value[0], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[0]); if (uinfo->count > 1) *right = snd_mixer_oss_conv1(uctl->value.integer.value[1], uinfo->value.integer.min, uinfo->value.integer.max, &pslot->volume[1]); __unalloc: up_read(&card->controls_rwsem); kfree(uctl); kfree(uinfo); } static void snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, unsigned int numid, int *left, int *right, int route) { struct snd_ctl_elem_info *uinfo; struct snd_ctl_elem_value *uctl; struct snd_kcontrol *kctl; struct snd_card *card = fmixer->card; if (numid == ID_UNKNOWN) return; down_read(&card->controls_rwsem); kctl = snd_ctl_find_numid_locked(card, numid); if (!kctl) { up_read(&card->controls_rwsem); return; } uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); uctl = kzalloc(sizeof(*uctl), GFP_KERNEL); if (uinfo == NULL || uctl == NULL) goto __unalloc; if (kctl->info(kctl, uinfo)) goto __unalloc; if (kctl->get(kctl, uctl)) goto __unalloc; if (!uctl->value.integer.value[0]) { *left = 0; if (uinfo->count == 1) *right = 0; } if (uinfo->count > 1 && !uctl->value.integer.value[route ? 3 : 1]) *right = 0; __unalloc: up_read(&card->controls_rwsem); kfree(uctl); kfree(uinfo); } static int snd_mixer_oss_get_volume1(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, int *left, int *right) { struct slot *slot = pslot->private_data; *left = *right = 100; if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) { snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) { snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) { snd_mixer_oss_get_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right); } if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) { snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) { snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) { snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) { snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1); } return 0; } static void snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, unsigned int numid, int left, int right) { struct snd_ctl_elem_info *uinfo; struct snd_ctl_elem_value *uctl; struct snd_kcontrol *kctl; struct snd_card *card = fmixer->card; int res; if (numid == ID_UNKNOWN) return; down_read(&card->controls_rwsem); kctl = snd_ctl_find_numid_locked(card, numid); if (!kctl) { up_read(&card->controls_rwsem); return; } uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); uctl = kzalloc(sizeof(*uctl), GFP_KERNEL); if (uinfo == NULL || uctl == NULL) goto __unalloc; if (kctl->info(kctl, uinfo)) goto __unalloc; if (uinfo->type == SNDRV_CTL_ELEM_TYPE_BOOLEAN && uinfo->value.integer.min == 0 && uinfo->value.integer.max == 1) goto __unalloc; uctl->value.integer.value[0] = snd_mixer_oss_conv2(left, uinfo->value.integer.min, uinfo->value.integer.max); if (uinfo->count > 1) uctl->value.integer.value[1] = snd_mixer_oss_conv2(right, uinfo->value.integer.min, uinfo->value.integer.max); res = kctl->put(kctl, uctl); if (res < 0) goto __unalloc; if (res > 0) snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); __unalloc: up_read(&card->controls_rwsem); kfree(uctl); kfree(uinfo); } static void snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, unsigned int numid, int left, int right, int route) { struct snd_ctl_elem_info *uinfo; struct snd_ctl_elem_value *uctl; struct snd_kcontrol *kctl; struct snd_card *card = fmixer->card; int res; if (numid == ID_UNKNOWN) return; down_read(&card->controls_rwsem); kctl = snd_ctl_find_numid_locked(card, numid); if (!kctl) { up_read(&card->controls_rwsem); return; } uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); uctl = kzalloc(sizeof(*uctl), GFP_KERNEL); if (uinfo == NULL || uctl == NULL) goto __unalloc; if (kctl->info(kctl, uinfo)) goto __unalloc; if (uinfo->count > 1) { uctl->value.integer.value[0] = left > 0 ? 1 : 0; uctl->value.integer.value[route ? 3 : 1] = right > 0 ? 1 : 0; if (route) { uctl->value.integer.value[1] = uctl->value.integer.value[2] = 0; } } else { uctl->value.integer.value[0] = (left > 0 || right > 0) ? 1 : 0; } res = kctl->put(kctl, uctl); if (res < 0) goto __unalloc; if (res > 0) snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); __unalloc: up_read(&card->controls_rwsem); kfree(uctl); kfree(uinfo); } static int snd_mixer_oss_put_volume1(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, int left, int right) { struct slot *slot = pslot->private_data; if (slot->present & SNDRV_MIXER_OSS_PRESENT_PVOLUME) { snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PVOLUME], left, right); if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME) snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CVOLUME) { snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CVOLUME], left, right); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GVOLUME) { snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GVOLUME], left, right); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GLOBAL) { snd_mixer_oss_put_volume1_vol(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GLOBAL], left, right); } if (left || right) { if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0); if (slot->present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], left, right, 0); if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0); if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1); if (slot->present & SNDRV_MIXER_OSS_PRESENT_CROUTE) snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], left, right, 1); if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1); } else { if (slot->present & SNDRV_MIXER_OSS_PRESENT_PSWITCH) { snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PSWITCH], left, right, 0); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) { snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], left, right, 0); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GSWITCH) { snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GSWITCH], left, right, 0); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_PROUTE) { snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_PROUTE], left, right, 1); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_CROUTE) { snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], left, right, 1); } else if (slot->present & SNDRV_MIXER_OSS_PRESENT_GROUTE) { snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_GROUTE], left, right, 1); } } return 0; } static int snd_mixer_oss_get_recsrc1_sw(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, int *active) { struct slot *slot = pslot->private_data; int left, right; left = right = 1; snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], &left, &right, 0); *active = (left || right) ? 1 : 0; return 0; } static int snd_mixer_oss_get_recsrc1_route(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, int *active) { struct slot *slot = pslot->private_data; int left, right; left = right = 1; snd_mixer_oss_get_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], &left, &right, 1); *active = (left || right) ? 1 : 0; return 0; } static int snd_mixer_oss_put_recsrc1_sw(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, int active) { struct slot *slot = pslot->private_data; snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CSWITCH], active, active, 0); return 0; } static int snd_mixer_oss_put_recsrc1_route(struct snd_mixer_oss_file *fmixer, struct snd_mixer_oss_slot *pslot, int active) { struct slot *slot = pslot->private_data; snd_mixer_oss_put_volume1_sw(fmixer, pslot, slot->numid[SNDRV_MIXER_OSS_ITEM_CROUTE], active, active, 1); return 0; } static int snd_mixer_oss_get_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int *active_index) { struct snd_card *card = fmixer->card; struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_kcontrol *kctl; struct snd_mixer_oss_slot *pslot; struct slot *slot; struct snd_ctl_elem_info *uinfo; struct snd_ctl_elem_value *uctl; int err, idx; uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); uctl = kzalloc(sizeof(*uctl), GFP_KERNEL); if (uinfo == NULL || uctl == NULL) { err = -ENOMEM; goto __free_only; } down_read(&card->controls_rwsem); kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0); if (! kctl) { err = -ENOENT; goto __unlock; } err = kctl->info(kctl, uinfo); if (err < 0) goto __unlock; err = kctl->get(kctl, uctl); if (err < 0) goto __unlock; for (idx = 0; idx < 32; idx++) { if (!(mixer->mask_recsrc & (1 << idx))) continue; pslot = &mixer->slots[idx]; slot = pslot->private_data; if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE) continue; if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE)) continue; if (slot->capture_item == uctl->value.enumerated.item[0]) { *active_index = idx; break; } } err = 0; __unlock: up_read(&card->controls_rwsem); __free_only: kfree(uctl); kfree(uinfo); return err; } static int snd_mixer_oss_put_recsrc2(struct snd_mixer_oss_file *fmixer, unsigned int active_index) { struct snd_card *card = fmixer->card; struct snd_mixer_oss *mixer = fmixer->mixer; struct snd_kcontrol *kctl; struct snd_mixer_oss_slot *pslot; struct slot *slot = NULL; struct snd_ctl_elem_info *uinfo; struct snd_ctl_elem_value *uctl; int err; unsigned int idx; uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); uctl = kzalloc(sizeof(*uctl), GFP_KERNEL); if (uinfo == NULL || uctl == NULL) { err = -ENOMEM; goto __free_only; } down_read(&card->controls_rwsem); kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0); if (! kctl) { err = -ENOENT; goto __unlock; } err = kctl->info(kctl, uinfo); if (err < 0) goto __unlock; for (idx = 0; idx < 32; idx++) { if (!(mixer->mask_recsrc & (1 << idx))) continue; pslot = &mixer->slots[idx]; slot = pslot->private_data; if (slot->signature != SNDRV_MIXER_OSS_SIGNATURE) continue; if (!(slot->present & SNDRV_MIXER_OSS_PRESENT_CAPTURE)) continue; if (idx == active_index) break; slot = NULL; } if (! slot) goto __unlock; for (idx = 0; idx < uinfo->count; idx++) uctl->value.enumerated.item[idx] = slot->capture_item; err = kctl->put(kctl, uctl); if (err > 0) snd_ctl_notify(fmixer->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); err = 0; __unlock: up_read(&card->controls_rwsem); __free_only: kfree(uctl); kfree(uinfo); return err; } struct snd_mixer_oss_assign_table { int oss_id; const char *name; int index; }; static int snd_mixer_oss_build_test(struct snd_mixer_oss *mixer, struct slot *slot, const char *name, int index, int item) { struct snd_ctl_elem_info *info; struct snd_kcontrol *kcontrol; struct snd_card *card = mixer->card; int err; down_read(&card->controls_rwsem); kcontrol = snd_mixer_oss_test_id(mixer, name, index); if (kcontrol == NULL) { up_read(&card->controls_rwsem); return 0; } info = kmalloc(sizeof(*info), GFP_KERNEL); if (! info) { up_read(&card->controls_rwsem); return -ENOMEM; } err = kcontrol->info(kcontrol, info); if (err < 0) { up_read(&card->controls_rwsem); kfree(info); return err; } slot->numid[item] = kcontrol->id.numid; up_read(&card->controls_rwsem); if (info->count > slot->channels) slot->channels = info->count; slot->present |= 1 << item; kfree(info); return 0; } static void snd_mixer_oss_slot_free(struct snd_mixer_oss_slot *chn) { struct slot *p = chn->private_data; if (p) { if (p->allocated && p->assigned) { kfree_const(p->assigned->name); kfree_const(p->assigned); } kfree(p); } } static void mixer_slot_clear(struct snd_mixer_oss_slot *rslot) { int idx = rslot->number; /* remember this */ if (rslot->private_free) rslot->private_free(rslot); memset(rslot, 0, sizeof(*rslot)); rslot->number = idx; } /* In a separate function to keep gcc 3.2 happy - do NOT merge this in snd_mixer_oss_build_input! */ static int snd_mixer_oss_build_test_all(struct snd_mixer_oss *mixer, const struct snd_mixer_oss_assign_table *ptr, struct slot *slot) { char str[64]; int err; err = snd_mixer_oss_build_test(mixer, slot, ptr->name, ptr->index, SNDRV_MIXER_OSS_ITEM_GLOBAL); if (err) return err; sprintf(str, "%s Switch", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_GSWITCH); if (err) return err; sprintf(str, "%s Route", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_GROUTE); if (err) return err; sprintf(str, "%s Volume", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_GVOLUME); if (err) return err; sprintf(str, "%s Playback Switch", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_PSWITCH); if (err) return err; sprintf(str, "%s Playback Route", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_PROUTE); if (err) return err; sprintf(str, "%s Playback Volume", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_PVOLUME); if (err) return err; sprintf(str, "%s Capture Switch", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_CSWITCH); if (err) return err; sprintf(str, "%s Capture Route", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_CROUTE); if (err) return err; sprintf(str, "%s Capture Volume", ptr->name); err = snd_mixer_oss_build_test(mixer, slot, str, ptr->index, SNDRV_MIXER_OSS_ITEM_CVOLUME); if (err) return err; return 0; } /* * build an OSS mixer element. * ptr_allocated means the entry is dynamically allocated (change via proc file). * when replace_old = 1, the old entry is replaced with the new one. */ static int snd_mixer_oss_build_input(struct snd_mixer_oss *mixer, const struct snd_mixer_oss_assign_table *ptr, int ptr_allocated, int replace_old) { struct slot slot; struct slot *pslot; struct snd_kcontrol *kctl; struct snd_mixer_oss_slot *rslot; char str[64]; /* check if already assigned */ if (mixer->slots[ptr->oss_id].get_volume && ! replace_old) return 0; memset(&slot, 0, sizeof(slot)); memset(slot.numid, 0xff, sizeof(slot.numid)); /* ID_UNKNOWN */ if (snd_mixer_oss_build_test_all(mixer, ptr, &slot)) return 0; down_read(&mixer->card->controls_rwsem); kctl = NULL; if (!ptr->index) kctl = snd_mixer_oss_test_id(mixer, "Capture Source", 0); if (kctl) { struct snd_ctl_elem_info *uinfo; uinfo = kzalloc(sizeof(*uinfo), GFP_KERNEL); if (! uinfo) { up_read(&mixer->card->controls_rwsem); return -ENOMEM; } if (kctl->info(kctl, uinfo)) { up_read(&mixer->card->controls_rwsem); kfree(uinfo); return 0; } strcpy(str, ptr->name); if (!strcmp(str, "Master")) strcpy(str, "Mix"); if (!strcmp(str, "Master Mono")) strcpy(str, "Mix Mono"); slot.capture_item = 0; if (!strcmp(uinfo->value.enumerated.name, str)) { slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE; } else { for (slot.capture_item = 1; slot.capture_item < uinfo->value.enumerated.items; slot.capture_item++) { uinfo->value.enumerated.item = slot.capture_item; if (kctl->info(kctl, uinfo)) { up_read(&mixer->card->controls_rwsem); kfree(uinfo); return 0; } if (!strcmp(uinfo->value.enumerated.name, str)) { slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE; break; } } } kfree(uinfo); } up_read(&mixer->card->controls_rwsem); if (slot.present != 0) { pslot = kmalloc(sizeof(slot), GFP_KERNEL); if (! pslot) return -ENOMEM; *pslot = slot; pslot->signature = SNDRV_MIXER_OSS_SIGNATURE; pslot->assigned = ptr; pslot->allocated = ptr_allocated; rslot = &mixer->slots[ptr->oss_id]; mixer_slot_clear(rslot); rslot->stereo = slot.channels > 1 ? 1 : 0; rslot->get_volume = snd_mixer_oss_get_volume1; rslot->put_volume = snd_mixer_oss_put_volume1; /* note: ES18xx have both Capture Source and XX Capture Volume !!! */ if (slot.present & SNDRV_MIXER_OSS_PRESENT_CSWITCH) { rslot->get_recsrc = snd_mixer_oss_get_recsrc1_sw; rslot->put_recsrc = snd_mixer_oss_put_recsrc1_sw; } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CROUTE) { rslot->get_recsrc = snd_mixer_oss_get_recsrc1_route; rslot->put_recsrc = snd_mixer_oss_put_recsrc1_route; } else if (slot.present & SNDRV_MIXER_OSS_PRESENT_CAPTURE) { mixer->mask_recsrc |= 1 << ptr->oss_id; } rslot->private_data = pslot; rslot->private_free = snd_mixer_oss_slot_free; return 1; } return 0; } #ifdef CONFIG_SND_PROC_FS /* */ #define MIXER_VOL(name) [SOUND_MIXER_##name] = #name static const char * const oss_mixer_names[SNDRV_OSS_MAX_MIXERS] = { MIXER_VOL(VOLUME), MIXER_VOL(BASS), MIXER_VOL(TREBLE), MIXER_VOL(SYNTH), MIXER_VOL(PCM), MIXER_VOL(SPEAKER), MIXER_VOL(LINE), MIXER_VOL(MIC), MIXER_VOL(CD), MIXER_VOL(IMIX), MIXER_VOL(ALTPCM), MIXER_VOL(RECLEV), MIXER_VOL(IGAIN), MIXER_VOL(OGAIN), MIXER_VOL(LINE1), MIXER_VOL(LINE2), MIXER_VOL(LINE3), MIXER_VOL(DIGITAL1), MIXER_VOL(DIGITAL2), MIXER_VOL(DIGITAL3), MIXER_VOL(PHONEIN), MIXER_VOL(PHONEOUT), MIXER_VOL(VIDEO), MIXER_VOL(RADIO), MIXER_VOL(MONITOR), }; /* * /proc interface */ static void snd_mixer_oss_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_mixer_oss *mixer = entry->private_data; int i; mutex_lock(&mixer->reg_mutex); for (i = 0; i < SNDRV_OSS_MAX_MIXERS; i++) { struct slot *p; if (! oss_mixer_names[i]) continue; p = (struct slot *)mixer->slots[i].private_data; snd_iprintf(buffer, "%s ", oss_mixer_names[i]); if (p && p->assigned) snd_iprintf(buffer, "\"%s\" %d\n", p->assigned->name, p->assigned->index); else snd_iprintf(buffer, "\"\" 0\n"); } mutex_unlock(&mixer->reg_mutex); } static void snd_mixer_oss_proc_write(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_mixer_oss *mixer = entry->private_data; char line[128], str[32], idxstr[16]; const char *cptr; unsigned int idx; int ch; struct snd_mixer_oss_assign_table *tbl; struct slot *slot; while (!snd_info_get_line(buffer, line, sizeof(line))) { cptr = snd_info_get_str(str, line, sizeof(str)); for (ch = 0; ch < SNDRV_OSS_MAX_MIXERS; ch++) if (oss_mixer_names[ch] && strcmp(oss_mixer_names[ch], str) == 0) break; if (ch >= SNDRV_OSS_MAX_MIXERS) { pr_err("ALSA: mixer_oss: invalid OSS volume '%s'\n", str); continue; } cptr = snd_info_get_str(str, cptr, sizeof(str)); if (! *str) { /* remove the entry */ mutex_lock(&mixer->reg_mutex); mixer_slot_clear(&mixer->slots[ch]); mutex_unlock(&mixer->reg_mutex); continue; } snd_info_get_str(idxstr, cptr, sizeof(idxstr)); idx = simple_strtoul(idxstr, NULL, 10); if (idx >= 0x4000) { /* too big */ pr_err("ALSA: mixer_oss: invalid index %d\n", idx); continue; } mutex_lock(&mixer->reg_mutex); slot = (struct slot *)mixer->slots[ch].private_data; if (slot && slot->assigned && slot->assigned->index == idx && ! strcmp(slot->assigned->name, str)) /* not changed */ goto __unlock; tbl = kmalloc(sizeof(*tbl), GFP_KERNEL); if (!tbl) goto __unlock; tbl->oss_id = ch; tbl->name = kstrdup(str, GFP_KERNEL); if (! tbl->name) { kfree(tbl); goto __unlock; } tbl->index = idx; if (snd_mixer_oss_build_input(mixer, tbl, 1, 1) <= 0) { kfree(tbl->name); kfree(tbl); } __unlock: mutex_unlock(&mixer->reg_mutex); } } static void snd_mixer_oss_proc_init(struct snd_mixer_oss *mixer) { struct snd_info_entry *entry; entry = snd_info_create_card_entry(mixer->card, "oss_mixer", mixer->card->proc_root); if (! entry) return; entry->content = SNDRV_INFO_CONTENT_TEXT; entry->mode = S_IFREG | 0644; entry->c.text.read = snd_mixer_oss_proc_read; entry->c.text.write = snd_mixer_oss_proc_write; entry->private_data = mixer; if (snd_info_register(entry) < 0) { snd_info_free_entry(entry); entry = NULL; } mixer->proc_entry = entry; } static void snd_mixer_oss_proc_done(struct snd_mixer_oss *mixer) { snd_info_free_entry(mixer->proc_entry); mixer->proc_entry = NULL; } #else /* !CONFIG_SND_PROC_FS */ #define snd_mixer_oss_proc_init(mix) #define snd_mixer_oss_proc_done(mix) #endif /* CONFIG_SND_PROC_FS */ static void snd_mixer_oss_build(struct snd_mixer_oss *mixer) { static const struct snd_mixer_oss_assign_table table[] = { { SOUND_MIXER_VOLUME, "Master", 0 }, { SOUND_MIXER_VOLUME, "Front", 0 }, /* fallback */ { SOUND_MIXER_BASS, "Tone Control - Bass", 0 }, { SOUND_MIXER_TREBLE, "Tone Control - Treble", 0 }, { SOUND_MIXER_SYNTH, "Synth", 0 }, { SOUND_MIXER_SYNTH, "FM", 0 }, /* fallback */ { SOUND_MIXER_SYNTH, "Music", 0 }, /* fallback */ { SOUND_MIXER_PCM, "PCM", 0 }, { SOUND_MIXER_SPEAKER, "Beep", 0 }, { SOUND_MIXER_SPEAKER, "PC Speaker", 0 }, /* fallback */ { SOUND_MIXER_SPEAKER, "Speaker", 0 }, /* fallback */ { SOUND_MIXER_LINE, "Line", 0 }, { SOUND_MIXER_MIC, "Mic", 0 }, { SOUND_MIXER_CD, "CD", 0 }, { SOUND_MIXER_IMIX, "Monitor Mix", 0 }, { SOUND_MIXER_ALTPCM, "PCM", 1 }, { SOUND_MIXER_ALTPCM, "Headphone", 0 }, /* fallback */ { SOUND_MIXER_ALTPCM, "Wave", 0 }, /* fallback */ { SOUND_MIXER_RECLEV, "-- nothing --", 0 }, { SOUND_MIXER_IGAIN, "Capture", 0 }, { SOUND_MIXER_OGAIN, "Playback", 0 }, { SOUND_MIXER_LINE1, "Aux", 0 }, { SOUND_MIXER_LINE2, "Aux", 1 }, { SOUND_MIXER_LINE3, "Aux", 2 }, { SOUND_MIXER_DIGITAL1, "Digital", 0 }, { SOUND_MIXER_DIGITAL1, "IEC958", 0 }, /* fallback */ { SOUND_MIXER_DIGITAL1, "IEC958 Optical", 0 }, /* fallback */ { SOUND_MIXER_DIGITAL1, "IEC958 Coaxial", 0 }, /* fallback */ { SOUND_MIXER_DIGITAL2, "Digital", 1 }, { SOUND_MIXER_DIGITAL3, "Digital", 2 }, { SOUND_MIXER_PHONEIN, "Phone", 0 }, { SOUND_MIXER_PHONEOUT, "Master Mono", 0 }, { SOUND_MIXER_PHONEOUT, "Speaker", 0 }, /*fallback*/ { SOUND_MIXER_PHONEOUT, "Mono", 0 }, /*fallback*/ { SOUND_MIXER_PHONEOUT, "Phone", 0 }, /* fallback */ { SOUND_MIXER_VIDEO, "Video", 0 }, { SOUND_MIXER_RADIO, "Radio", 0 }, { SOUND_MIXER_MONITOR, "Monitor", 0 } }; unsigned int idx; for (idx = 0; idx < ARRAY_SIZE(table); idx++) snd_mixer_oss_build_input(mixer, &table[idx], 0, 0); if (mixer->mask_recsrc) { mixer->get_recsrc = snd_mixer_oss_get_recsrc2; mixer->put_recsrc = snd_mixer_oss_put_recsrc2; } } /* * */ static int snd_mixer_oss_free1(void *private) { struct snd_mixer_oss *mixer = private; struct snd_card *card; int idx; if (!mixer) return 0; card = mixer->card; if (snd_BUG_ON(mixer != card->mixer_oss)) return -ENXIO; card->mixer_oss = NULL; for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++) { struct snd_mixer_oss_slot *chn = &mixer->slots[idx]; if (chn->private_free) chn->private_free(chn); } kfree(mixer); return 0; } static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd) { struct snd_mixer_oss *mixer; if (cmd == SND_MIXER_OSS_NOTIFY_REGISTER) { int idx, err; mixer = kcalloc(2, sizeof(*mixer), GFP_KERNEL); if (mixer == NULL) return -ENOMEM; mutex_init(&mixer->reg_mutex); err = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, card, 0, &snd_mixer_oss_f_ops, card); if (err < 0) { dev_err(card->dev, "unable to register OSS mixer device %i:%i\n", card->number, 0); kfree(mixer); return err; } mixer->oss_dev_alloc = 1; mixer->card = card; if (*card->mixername) strscpy(mixer->name, card->mixername, sizeof(mixer->name)); else snprintf(mixer->name, sizeof(mixer->name), "mixer%i", card->number); #ifdef SNDRV_OSS_INFO_DEV_MIXERS snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIXERS, card->number, mixer->name); #endif for (idx = 0; idx < SNDRV_OSS_MAX_MIXERS; idx++) mixer->slots[idx].number = idx; card->mixer_oss = mixer; snd_mixer_oss_build(mixer); snd_mixer_oss_proc_init(mixer); } else { mixer = card->mixer_oss; if (mixer == NULL) return 0; if (mixer->oss_dev_alloc) { #ifdef SNDRV_OSS_INFO_DEV_MIXERS snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number); #endif snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0); mixer->oss_dev_alloc = 0; } if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT) return 0; snd_mixer_oss_proc_done(mixer); return snd_mixer_oss_free1(mixer); } return 0; } static int __init alsa_mixer_oss_init(void) { struct snd_card *card; int idx; snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler; for (idx = 0; idx < SNDRV_CARDS; idx++) { card = snd_card_ref(idx); if (card) { snd_mixer_oss_notify_handler(card, SND_MIXER_OSS_NOTIFY_REGISTER); snd_card_unref(card); } } return 0; } static void __exit alsa_mixer_oss_exit(void) { struct snd_card *card; int idx; snd_mixer_oss_notify_callback = NULL; for (idx = 0; idx < SNDRV_CARDS; idx++) { card = snd_card_ref(idx); if (card) { snd_mixer_oss_notify_handler(card, SND_MIXER_OSS_NOTIFY_FREE); snd_card_unref(card); } } } module_init(alsa_mixer_oss_init) module_exit(alsa_mixer_oss_exit)
linux-master
sound/core/oss/mixer_oss.c
/* * Route Plug-In * Copyright (c) 2000 by Abramo Bagnara <[email protected]> * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include <linux/time.h> #include <sound/core.h> #include <sound/pcm.h> #include "pcm_plugin.h" static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts, snd_pcm_uframes_t frames, snd_pcm_format_t format) { int dst = 0; for (; dst < ndsts; ++dst) { if (dvp->wanted) snd_pcm_area_silence(&dvp->area, 0, frames, format); dvp->enabled = 0; dvp++; } } static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel, struct snd_pcm_plugin_channel *dst_channel, snd_pcm_uframes_t frames, snd_pcm_format_t format) { dst_channel->enabled = 1; snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format); } static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { int nsrcs, ndsts, dst; struct snd_pcm_plugin_channel *dvp; snd_pcm_format_t format; if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) return -ENXIO; if (frames == 0) return 0; if (frames > dst_channels[0].frames) frames = dst_channels[0].frames; nsrcs = plugin->src_format.channels; ndsts = plugin->dst_format.channels; format = plugin->dst_format.format; dvp = dst_channels; if (nsrcs <= 1) { /* expand to all channels */ for (dst = 0; dst < ndsts; ++dst) { copy_area(src_channels, dvp, frames, format); dvp++; } return frames; } for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) { copy_area(src_channels, dvp, frames, format); dvp++; src_channels++; } if (dst < ndsts) zero_areas(dvp, ndsts - dst, frames, format); return frames; } int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug, struct snd_pcm_plugin_format *src_format, struct snd_pcm_plugin_format *dst_format, struct snd_pcm_plugin **r_plugin) { struct snd_pcm_plugin *plugin; int err; if (snd_BUG_ON(!r_plugin)) return -ENXIO; *r_plugin = NULL; if (snd_BUG_ON(src_format->rate != dst_format->rate)) return -ENXIO; if (snd_BUG_ON(src_format->format != dst_format->format)) return -ENXIO; err = snd_pcm_plugin_build(plug, "route conversion", src_format, dst_format, 0, &plugin); if (err < 0) return err; plugin->transfer = route_transfer; *r_plugin = plugin; return 0; }
linux-master
sound/core/oss/route.c
/* * Linear conversion Plug-In * Copyright (c) 1999 by Jaroslav Kysela <[email protected]>, * Abramo Bagnara <[email protected]> * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include <linux/time.h> #include <sound/core.h> #include <sound/pcm.h> #include "pcm_plugin.h" /* * Basic linear conversion plugin */ struct linear_priv { int cvt_endian; /* need endian conversion? */ unsigned int src_ofs; /* byte offset in source format */ unsigned int dst_ofs; /* byte soffset in destination format */ unsigned int copy_ofs; /* byte offset in temporary u32 data */ unsigned int dst_bytes; /* byte size of destination format */ unsigned int copy_bytes; /* bytes to copy per conversion */ unsigned int flip; /* MSB flip for signeness, done after endian conv */ }; static inline void do_convert(struct linear_priv *data, unsigned char *dst, unsigned char *src) { unsigned int tmp = 0; unsigned char *p = (unsigned char *)&tmp; memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes); if (data->cvt_endian) tmp = swab32(tmp); tmp ^= data->flip; memcpy(dst, p + data->dst_ofs, data->dst_bytes); } static void convert(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { struct linear_priv *data = (struct linear_priv *)plugin->extra_data; int channel; int nchannels = plugin->src_format.channels; for (channel = 0; channel < nchannels; ++channel) { char *src; char *dst; int src_step, dst_step; snd_pcm_uframes_t frames1; if (!src_channels[channel].enabled) { if (dst_channels[channel].wanted) snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format); dst_channels[channel].enabled = 0; continue; } dst_channels[channel].enabled = 1; src = src_channels[channel].area.addr + src_channels[channel].area.first / 8; dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8; src_step = src_channels[channel].area.step / 8; dst_step = dst_channels[channel].area.step / 8; frames1 = frames; while (frames1-- > 0) { do_convert(data, dst, src); src += src_step; dst += dst_step; } } } static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin, const struct snd_pcm_plugin_channel *src_channels, struct snd_pcm_plugin_channel *dst_channels, snd_pcm_uframes_t frames) { if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) return -ENXIO; if (frames == 0) return 0; #ifdef CONFIG_SND_DEBUG { unsigned int channel; for (channel = 0; channel < plugin->src_format.channels; channel++) { if (snd_BUG_ON(src_channels[channel].area.first % 8 || src_channels[channel].area.step % 8)) return -ENXIO; if (snd_BUG_ON(dst_channels[channel].area.first % 8 || dst_channels[channel].area.step % 8)) return -ENXIO; } } #endif if (frames > dst_channels[0].frames) frames = dst_channels[0].frames; convert(plugin, src_channels, dst_channels, frames); return frames; } static void init_data(struct linear_priv *data, snd_pcm_format_t src_format, snd_pcm_format_t dst_format) { int src_le, dst_le, src_bytes, dst_bytes; src_bytes = snd_pcm_format_width(src_format) / 8; dst_bytes = snd_pcm_format_width(dst_format) / 8; src_le = snd_pcm_format_little_endian(src_format) > 0; dst_le = snd_pcm_format_little_endian(dst_format) > 0; data->dst_bytes = dst_bytes; data->cvt_endian = src_le != dst_le; data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes; if (src_le) { data->copy_ofs = 4 - data->copy_bytes; data->src_ofs = src_bytes - data->copy_bytes; } else data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 - src_bytes; if (dst_le) data->dst_ofs = 4 - data->dst_bytes; else data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 - dst_bytes; if (snd_pcm_format_signed(src_format) != snd_pcm_format_signed(dst_format)) { if (dst_le) data->flip = (__force u32)cpu_to_le32(0x80000000); else data->flip = (__force u32)cpu_to_be32(0x80000000); } } int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug, struct snd_pcm_plugin_format *src_format, struct snd_pcm_plugin_format *dst_format, struct snd_pcm_plugin **r_plugin) { int err; struct linear_priv *data; struct snd_pcm_plugin *plugin; if (snd_BUG_ON(!r_plugin)) return -ENXIO; *r_plugin = NULL; if (snd_BUG_ON(src_format->rate != dst_format->rate)) return -ENXIO; if (snd_BUG_ON(src_format->channels != dst_format->channels)) return -ENXIO; if (snd_BUG_ON(!snd_pcm_format_linear(src_format->format) || !snd_pcm_format_linear(dst_format->format))) return -ENXIO; err = snd_pcm_plugin_build(plug, "linear format conversion", src_format, dst_format, sizeof(struct linear_priv), &plugin); if (err < 0) return err; data = (struct linear_priv *)plugin->extra_data; init_data(data, src_format->format, dst_format->format); plugin->transfer = linear_transfer; *r_plugin = plugin; return 0; }
linux-master
sound/core/oss/linear.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for generic ESS AudioDrive ES18xx soundcards * Copyright (c) by Christian Fischbach <[email protected]> * Copyright (c) by Abramo Bagnara <[email protected]> */ /* GENERAL NOTES: * * BUGS: * - There are pops (we can't delay in trigger function, cause midlevel * often need to trigger down and then up very quickly). * Any ideas? * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it. */ /* * ES1868 NOTES: * - The chip has one half duplex pcm (with very limited full duplex support). * * - Duplex stereophonic sound is impossible. * - Record and playback must share the same frequency rate. * * - The driver use dma2 for playback and dma1 for capture. */ /* * ES1869 NOTES: * * - there are a first full duplex pcm and a second playback only pcm * (incompatible with first pcm capture) * * - there is support for the capture volume and ESS Spatializer 3D effect. * * - contrarily to some pages in DS_1869.PDF the rates can be set * independently. * * - Zoom Video is implemented by sharing the FM DAC, thus the user can * have either FM playback or Video playback but not both simultaneously. * The Video Playback Switch mixer control toggles this choice. * * BUGS: * * - There is a major trouble I noted: * * using both channel for playback stereo 16 bit samples at 44100 Hz * the second pcm (Audio1) DMA slows down irregularly and sound is garbled. * * The same happens using Audio1 for captureing. * * The Windows driver does not suffer of this (although it use Audio1 * only for captureing). I'm unable to discover why. * */ /* * ES1879 NOTES: * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback * seems to be effected (speaker_test plays a lower frequency). Can't find * anything in the datasheet to account for this, so a Video Playback Switch * control has been included to allow ZV to be enabled only when necessary. * Then again on at least one test system the 0x71 bit 6 enable bit is not * needed for ZV, so maybe the datasheet is entirely wrong here. */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/pnp.h> #include <linux/isapnp.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/io.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/control.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include <sound/mpu401.h> #include <sound/opl3.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #define PFX "es18xx: " struct snd_es18xx { unsigned long port; /* port of ESS chip */ unsigned long ctrl_port; /* Control port of ESS chip */ int irq; /* IRQ number of ESS chip */ int dma1; /* DMA1 */ int dma2; /* DMA2 */ unsigned short version; /* version of ESS chip */ int caps; /* Chip capabilities */ unsigned short audio2_vol; /* volume level of audio2 */ unsigned short active; /* active channel mask */ unsigned int dma1_shift; unsigned int dma2_shift; struct snd_pcm *pcm; struct snd_pcm_substream *playback_a_substream; struct snd_pcm_substream *capture_a_substream; struct snd_pcm_substream *playback_b_substream; struct snd_rawmidi *rmidi; struct snd_kcontrol *hw_volume; struct snd_kcontrol *hw_switch; struct snd_kcontrol *master_volume; struct snd_kcontrol *master_switch; spinlock_t reg_lock; spinlock_t mixer_lock; #ifdef CONFIG_PM unsigned char pm_reg; #endif #ifdef CONFIG_PNP struct pnp_dev *dev; struct pnp_dev *devc; #endif }; #define AUDIO1_IRQ 0x01 #define AUDIO2_IRQ 0x02 #define HWV_IRQ 0x04 #define MPU_IRQ 0x08 #define ES18XX_PCM2 0x0001 /* Has two useable PCM */ #define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */ #define ES18XX_RECMIX 0x0004 /* Has record mixer */ #define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */ #define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */ #define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */ #define ES18XX_AUXB 0x0040 /* AuxB mixer control */ #define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/ #define ES18XX_MONO 0x0100 /* Mono_in mixer control */ #define ES18XX_I2S 0x0200 /* I2S mixer control */ #define ES18XX_MUTEREC 0x0400 /* Record source can be muted */ #define ES18XX_CONTROL 0x0800 /* Has control ports */ #define ES18XX_GPO_2BIT 0x1000 /* GPO0,1 controlled by PM port */ /* Power Management */ #define ES18XX_PM 0x07 #define ES18XX_PM_GPO0 0x01 #define ES18XX_PM_GPO1 0x02 #define ES18XX_PM_PDR 0x04 #define ES18XX_PM_ANA 0x08 #define ES18XX_PM_FM 0x020 #define ES18XX_PM_SUS 0x080 /* Lowlevel */ #define DAC1 0x01 #define ADC1 0x02 #define DAC2 0x04 #define MILLISECOND 10000 static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val) { int i; for(i = MILLISECOND; i; i--) if ((inb(chip->port + 0x0C) & 0x80) == 0) { outb(val, chip->port + 0x0C); return 0; } snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val); return -EINVAL; } static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip) { int i; for(i = MILLISECOND/10; i; i--) if (inb(chip->port + 0x0C) & 0x40) return inb(chip->port + 0x0A); snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n", chip->port + 0x0A, inb(chip->port + 0x0A)); return -ENODEV; } #undef REG_DEBUG static int snd_es18xx_write(struct snd_es18xx *chip, unsigned char reg, unsigned char data) { unsigned long flags; int ret; spin_lock_irqsave(&chip->reg_lock, flags); ret = snd_es18xx_dsp_command(chip, reg); if (ret < 0) goto end; ret = snd_es18xx_dsp_command(chip, data); end: spin_unlock_irqrestore(&chip->reg_lock, flags); #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data); #endif return ret; } static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg) { unsigned long flags; int ret, data; spin_lock_irqsave(&chip->reg_lock, flags); ret = snd_es18xx_dsp_command(chip, 0xC0); if (ret < 0) goto end; ret = snd_es18xx_dsp_command(chip, reg); if (ret < 0) goto end; data = snd_es18xx_dsp_get_byte(chip); ret = data; #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret); #endif end: spin_unlock_irqrestore(&chip->reg_lock, flags); return ret; } /* Return old value */ static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg, unsigned char mask, unsigned char val) { int ret; unsigned char old, new, oval; unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); ret = snd_es18xx_dsp_command(chip, 0xC0); if (ret < 0) goto end; ret = snd_es18xx_dsp_command(chip, reg); if (ret < 0) goto end; ret = snd_es18xx_dsp_get_byte(chip); if (ret < 0) { goto end; } old = ret; oval = old & mask; if (val != oval) { ret = snd_es18xx_dsp_command(chip, reg); if (ret < 0) goto end; new = (old & ~mask) | (val & mask); ret = snd_es18xx_dsp_command(chip, new); if (ret < 0) goto end; #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n", reg, old, new, ret); #endif } ret = oval; end: spin_unlock_irqrestore(&chip->reg_lock, flags); return ret; } static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip, unsigned char reg, unsigned char data) { unsigned long flags; spin_lock_irqsave(&chip->mixer_lock, flags); outb(reg, chip->port + 0x04); outb(data, chip->port + 0x05); spin_unlock_irqrestore(&chip->mixer_lock, flags); #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data); #endif } static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg) { unsigned long flags; int data; spin_lock_irqsave(&chip->mixer_lock, flags); outb(reg, chip->port + 0x04); data = inb(chip->port + 0x05); spin_unlock_irqrestore(&chip->mixer_lock, flags); #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data); #endif return data; } /* Return old value */ static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg, unsigned char mask, unsigned char val) { unsigned char old, new, oval; unsigned long flags; spin_lock_irqsave(&chip->mixer_lock, flags); outb(reg, chip->port + 0x04); old = inb(chip->port + 0x05); oval = old & mask; if (val != oval) { new = (old & ~mask) | (val & mask); outb(new, chip->port + 0x05); #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n", reg, old, new); #endif } spin_unlock_irqrestore(&chip->mixer_lock, flags); return oval; } static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg, unsigned char mask) { int old, expected, new; unsigned long flags; spin_lock_irqsave(&chip->mixer_lock, flags); outb(reg, chip->port + 0x04); old = inb(chip->port + 0x05); expected = old ^ mask; outb(expected, chip->port + 0x05); new = inb(chip->port + 0x05); spin_unlock_irqrestore(&chip->mixer_lock, flags); #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n", reg, old, expected, new); #endif return expected == new; } static int snd_es18xx_reset(struct snd_es18xx *chip) { int i; outb(0x03, chip->port + 0x06); inb(chip->port + 0x06); outb(0x00, chip->port + 0x06); for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++); if (inb(chip->port + 0x0A) != 0xAA) return -1; return 0; } static int snd_es18xx_reset_fifo(struct snd_es18xx *chip) { outb(0x02, chip->port + 0x06); inb(chip->port + 0x06); outb(0x00, chip->port + 0x06); return 0; } static const struct snd_ratnum new_clocks[2] = { { .num = 793800, .den_min = 1, .den_max = 128, .den_step = 1, }, { .num = 768000, .den_min = 1, .den_max = 128, .den_step = 1, } }; static const struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = { .nrats = 2, .rats = new_clocks, }; static const struct snd_ratnum old_clocks[2] = { { .num = 795444, .den_min = 1, .den_max = 128, .den_step = 1, }, { .num = 397722, .den_min = 1, .den_max = 128, .den_step = 1, } }; static const struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = { .nrats = 2, .rats = old_clocks, }; static void snd_es18xx_rate_set(struct snd_es18xx *chip, struct snd_pcm_substream *substream, int mode) { unsigned int bits, div0; struct snd_pcm_runtime *runtime = substream->runtime; if (chip->caps & ES18XX_NEW_RATE) { if (runtime->rate_num == new_clocks[0].num) bits = 128 - runtime->rate_den; else bits = 256 - runtime->rate_den; } else { if (runtime->rate_num == old_clocks[0].num) bits = 256 - runtime->rate_den; else bits = 128 - runtime->rate_den; } /* set filter register */ div0 = 256 - 7160000*20/(8*82*runtime->rate); if ((chip->caps & ES18XX_PCM2) && mode == DAC2) { snd_es18xx_mixer_write(chip, 0x70, bits); /* * Comment from kernel oss driver: * FKS: fascinating: 0x72 doesn't seem to work. */ snd_es18xx_write(chip, 0xA2, div0); snd_es18xx_mixer_write(chip, 0x72, div0); } else { snd_es18xx_write(chip, 0xA1, bits); snd_es18xx_write(chip, 0xA2, div0); } } static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); int shift; shift = 0; if (params_channels(hw_params) == 2) shift++; if (snd_pcm_format_width(params_format(hw_params)) == 16) shift++; if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) { if ((chip->caps & ES18XX_DUPLEX_MONO) && (chip->capture_a_substream) && params_channels(hw_params) != 1) { _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS); return -EBUSY; } chip->dma2_shift = shift; } else { chip->dma1_shift = shift; } return 0; } static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip, struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); snd_es18xx_rate_set(chip, substream, DAC2); /* Transfer Count Reload */ count = 0x10000 - count; snd_es18xx_mixer_write(chip, 0x74, count & 0xff); snd_es18xx_mixer_write(chip, 0x76, count >> 8); /* Set format */ snd_es18xx_mixer_bits(chip, 0x7A, 0x07, ((runtime->channels == 1) ? 0x00 : 0x02) | (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) | (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04)); /* Set DMA controller */ snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); return 0; } static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip, struct snd_pcm_substream *substream, int cmd) { switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: if (chip->active & DAC2) return 0; chip->active |= DAC2; /* Start DMA */ if (chip->dma2 >= 4) snd_es18xx_mixer_write(chip, 0x78, 0xb3); else snd_es18xx_mixer_write(chip, 0x78, 0x93); #ifdef AVOID_POPS /* Avoid pops */ mdelay(100); if (chip->caps & ES18XX_PCM2) /* Restore Audio 2 volume */ snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol); else /* Enable PCM output */ snd_es18xx_dsp_command(chip, 0xD1); #endif break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: if (!(chip->active & DAC2)) return 0; chip->active &= ~DAC2; /* Stop DMA */ snd_es18xx_mixer_write(chip, 0x78, 0x00); #ifdef AVOID_POPS mdelay(25); if (chip->caps & ES18XX_PCM2) /* Set Audio 2 volume to 0 */ snd_es18xx_mixer_write(chip, 0x7C, 0); else /* Disable PCM output */ snd_es18xx_dsp_command(chip, 0xD3); #endif break; default: return -EINVAL; } return 0; } static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); int shift; shift = 0; if ((chip->caps & ES18XX_DUPLEX_MONO) && chip->playback_a_substream && params_channels(hw_params) != 1) { _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS); return -EBUSY; } if (params_channels(hw_params) == 2) shift++; if (snd_pcm_format_width(params_format(hw_params)) == 16) shift++; chip->dma1_shift = shift; return 0; } static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); snd_es18xx_reset_fifo(chip); /* Set stereo/mono */ snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01); snd_es18xx_rate_set(chip, substream, ADC1); /* Transfer Count Reload */ count = 0x10000 - count; snd_es18xx_write(chip, 0xA4, count & 0xff); snd_es18xx_write(chip, 0xA5, count >> 8); #ifdef AVOID_POPS mdelay(100); #endif /* Set format */ snd_es18xx_write(chip, 0xB7, snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71); snd_es18xx_write(chip, 0xB7, 0x90 | ((runtime->channels == 1) ? 0x40 : 0x08) | (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) | (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20)); /* Set DMA controller */ snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); return 0; } static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: if (chip->active & ADC1) return 0; chip->active |= ADC1; /* Start DMA */ snd_es18xx_write(chip, 0xB8, 0x0f); break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: if (!(chip->active & ADC1)) return 0; chip->active &= ~ADC1; /* Stop DMA */ snd_es18xx_write(chip, 0xB8, 0x00); break; default: return -EINVAL; } return 0; } static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip, struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); snd_es18xx_reset_fifo(chip); /* Set stereo/mono */ snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01); snd_es18xx_rate_set(chip, substream, DAC1); /* Transfer Count Reload */ count = 0x10000 - count; snd_es18xx_write(chip, 0xA4, count & 0xff); snd_es18xx_write(chip, 0xA5, count >> 8); /* Set format */ snd_es18xx_write(chip, 0xB6, snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00); snd_es18xx_write(chip, 0xB7, snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71); snd_es18xx_write(chip, 0xB7, 0x90 | (runtime->channels == 1 ? 0x40 : 0x08) | (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) | (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20)); /* Set DMA controller */ snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); return 0; } static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip, struct snd_pcm_substream *substream, int cmd) { switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: if (chip->active & DAC1) return 0; chip->active |= DAC1; /* Start DMA */ snd_es18xx_write(chip, 0xB8, 0x05); #ifdef AVOID_POPS /* Avoid pops */ mdelay(100); /* Enable Audio 1 */ snd_es18xx_dsp_command(chip, 0xD1); #endif break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: if (!(chip->active & DAC1)) return 0; chip->active &= ~DAC1; /* Stop DMA */ snd_es18xx_write(chip, 0xB8, 0x00); #ifdef AVOID_POPS /* Avoid pops */ mdelay(25); /* Disable Audio 1 */ snd_es18xx_dsp_command(chip, 0xD3); #endif break; default: return -EINVAL; } return 0; } static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) return snd_es18xx_playback1_prepare(chip, substream); else return snd_es18xx_playback2_prepare(chip, substream); } static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) return snd_es18xx_playback1_trigger(chip, substream, cmd); else return snd_es18xx_playback2_trigger(chip, substream, cmd); } static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id) { struct snd_card *card = dev_id; struct snd_es18xx *chip = card->private_data; unsigned char status; if (chip->caps & ES18XX_CONTROL) { /* Read Interrupt status */ status = inb(chip->ctrl_port + 6); } else { /* Read Interrupt status */ status = snd_es18xx_mixer_read(chip, 0x7f) >> 4; } #if 0 else { status = 0; if (inb(chip->port + 0x0C) & 0x01) status |= AUDIO1_IRQ; if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80) status |= AUDIO2_IRQ; if ((chip->caps & ES18XX_HWV) && snd_es18xx_mixer_read(chip, 0x64) & 0x10) status |= HWV_IRQ; } #endif /* Audio 1 & Audio 2 */ if (status & AUDIO2_IRQ) { if (chip->active & DAC2) snd_pcm_period_elapsed(chip->playback_a_substream); /* ack interrupt */ snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00); } if (status & AUDIO1_IRQ) { /* ok.. capture is active */ if (chip->active & ADC1) snd_pcm_period_elapsed(chip->capture_a_substream); /* ok.. playback2 is active */ else if (chip->active & DAC1) snd_pcm_period_elapsed(chip->playback_b_substream); /* ack interrupt */ inb(chip->port + 0x0E); } /* MPU */ if ((status & MPU_IRQ) && chip->rmidi) snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data); /* Hardware volume */ if (status & HWV_IRQ) { int split = 0; if (chip->caps & ES18XX_HWV) { split = snd_es18xx_mixer_read(chip, 0x64) & 0x80; snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id); snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id); } if (!split) { snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id); snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id); } /* ack interrupt */ snd_es18xx_mixer_write(chip, 0x66, 0x00); } return IRQ_HANDLED; } static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); unsigned int size = snd_pcm_lib_buffer_bytes(substream); int pos; if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) { if (!(chip->active & DAC2)) return 0; pos = snd_dma_pointer(chip->dma2, size); return pos >> chip->dma2_shift; } else { if (!(chip->active & DAC1)) return 0; pos = snd_dma_pointer(chip->dma1, size); return pos >> chip->dma1_shift; } } static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); unsigned int size = snd_pcm_lib_buffer_bytes(substream); int pos; if (!(chip->active & ADC1)) return 0; pos = snd_dma_pointer(chip->dma1, size); return pos >> chip->dma1_shift; } static const struct snd_pcm_hardware snd_es18xx_playback = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID), .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE), .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 65536, .period_bytes_min = 64, .period_bytes_max = 65536, .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_es18xx_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID), .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE), .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 65536, .period_bytes_min = 64, .period_bytes_max = 65536, .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static int snd_es18xx_playback_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_es18xx *chip = snd_pcm_substream_chip(substream); if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) { if ((chip->caps & ES18XX_DUPLEX_MONO) && chip->capture_a_substream && chip->capture_a_substream->runtime->channels != 1) return -EAGAIN; chip->playback_a_substream = substream; } else if (substream->number <= 1) { if (chip->capture_a_substream) return -EAGAIN; chip->playback_b_substream = substream; } else { snd_BUG(); return -EINVAL; } substream->runtime->hw = snd_es18xx_playback; snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks); return 0; } static int snd_es18xx_capture_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_es18xx *chip = snd_pcm_substream_chip(substream); if (chip->playback_b_substream) return -EAGAIN; if ((chip->caps & ES18XX_DUPLEX_MONO) && chip->playback_a_substream && chip->playback_a_substream->runtime->channels != 1) return -EAGAIN; chip->capture_a_substream = substream; substream->runtime->hw = snd_es18xx_capture; snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks); return 0; } static int snd_es18xx_playback_close(struct snd_pcm_substream *substream) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) chip->playback_a_substream = NULL; else chip->playback_b_substream = NULL; return 0; } static int snd_es18xx_capture_close(struct snd_pcm_substream *substream) { struct snd_es18xx *chip = snd_pcm_substream_chip(substream); chip->capture_a_substream = NULL; return 0; } /* * MIXER part */ /* Record source mux routines: * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs. * bit table for the 4/5 source mux: * reg 1C: * b2 b1 b0 muxSource * x 0 x microphone * 0 1 x CD * 1 1 0 line * 1 1 1 mixer * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines * either the play mixer or the capture mixer. * * "map4Source" translates from source number to reg bit pattern * "invMap4Source" translates from reg bit pattern to source number */ static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts5Source[5] = { "Mic", "CD", "Line", "Master", "Mix" }; static const char * const texts8Source[8] = { "Mic", "Mic Master", "CD", "AOUT", "Mic1", "Mix", "Line", "Master" }; struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); switch (chip->version) { case 0x1868: case 0x1878: return snd_ctl_enum_info(uinfo, 1, 4, texts5Source); case 0x1887: case 0x1888: return snd_ctl_enum_info(uinfo, 1, 5, texts5Source); case 0x1869: /* DS somewhat contradictory for 1869: could be 5 or 8 */ case 0x1879: return snd_ctl_enum_info(uinfo, 1, 8, texts8Source); default: return -EINVAL; } } static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { static const unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3}; struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07; if (!(chip->version == 0x1869 || chip->version == 0x1879)) { muxSource = invMap4Source[muxSource]; if (muxSource==3 && (chip->version == 0x1887 || chip->version == 0x1888) && (snd_es18xx_mixer_read(chip, 0x7a) & 0x08) ) muxSource = 4; } ucontrol->value.enumerated.item[0] = muxSource; return 0; } static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { static const unsigned char map4Source[4] = {0, 2, 6, 7}; struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); unsigned char val = ucontrol->value.enumerated.item[0]; unsigned char retVal = 0; switch (chip->version) { /* 5 source chips */ case 0x1887: case 0x1888: if (val > 4) return -EINVAL; if (val == 4) { retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08; val = 3; } else retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00; fallthrough; /* 4 source chips */ case 0x1868: case 0x1878: if (val > 3) return -EINVAL; val = map4Source[val]; break; /* 8 source chips */ case 0x1869: case 0x1879: if (val > 7) return -EINVAL; break; default: return -EINVAL; } return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal; } #define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); unsigned char val = snd_es18xx_mixer_read(chip, 0x50); ucontrol->value.integer.value[0] = !!(val & 8); return 0; } static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); unsigned char oval, nval; int change; nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04; oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c; change = nval != oval; if (change) { snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04); snd_es18xx_mixer_write(chip, 0x50, nval); } return change; } static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = 63; return 0; } static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f; ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f; return 0; } #define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40); ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40); return 0; } static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); chip->master_volume = NULL; chip->master_switch = NULL; chip->hw_volume = NULL; chip->hw_switch = NULL; } static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg, unsigned char mask, unsigned char val) { if (reg < 0xa0) return snd_es18xx_mixer_bits(chip, reg, mask, val); else return snd_es18xx_bits(chip, reg, mask, val); } static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg) { if (reg < 0xa0) return snd_es18xx_mixer_read(chip, reg); else return snd_es18xx_read(chip, reg); } #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, flags) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_es18xx_info_single, \ .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (flags << 24) } #define ES18XX_FL_INVERT (1 << 0) #define ES18XX_FL_PMPORT (1 << 1) static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 16) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT; int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT; int val; if (pm_port) val = inb(chip->port + ES18XX_PM); else val = snd_es18xx_reg_read(chip, reg); ucontrol->value.integer.value[0] = (val >> shift) & mask; if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT; int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT; unsigned char val; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; mask <<= shift; val <<= shift; if (pm_port) { unsigned char cur = inb(chip->port + ES18XX_PM); if ((cur & mask) == val) return 0; outb((cur & ~mask) | val, chip->port + ES18XX_PM); return 1; } return snd_es18xx_reg_bits(chip, reg, mask, val) != val; } #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_es18xx_info_double, \ .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) } static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; unsigned char left, right; left = snd_es18xx_reg_read(chip, left_reg); if (left_reg != right_reg) right = snd_es18xx_reg_read(chip, right_reg); else right = left; ucontrol->value.integer.value[0] = (left >> shift_left) & mask; ucontrol->value.integer.value[1] = (right >> shift_right) & mask; if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol); int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change; unsigned char val1, val2, mask1, mask2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; mask1 = mask << shift_left; mask2 = mask << shift_right; if (left_reg != right_reg) { change = 0; if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1) change = 1; if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2) change = 1; } else { change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2, val1 | val2) != (val1 | val2)); } return change; } /* Mixer controls * These arrays contain setup data for mixer controls. * * The controls that are universal to all chipsets are fully initialized * here. */ static const struct snd_kcontrol_new snd_es18xx_base_controls[] = { ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0), ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1), ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0), ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0), ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0), ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0), ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0), ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0), ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0), { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Capture Source", .info = snd_es18xx_info_mux, .get = snd_es18xx_get_mux, .put = snd_es18xx_put_mux, } }; static const struct snd_kcontrol_new snd_es18xx_recmix_controls[] = { ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0), ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0), ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0), ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0), ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0), ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0) }; /* * The chipset specific mixer controls */ static const struct snd_kcontrol_new snd_es18xx_opt_speaker = ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0); static const struct snd_kcontrol_new snd_es18xx_opt_1869[] = { ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, ES18XX_FL_INVERT), ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0), ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0), ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0) }; static const struct snd_kcontrol_new snd_es18xx_opt_1878 = ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0); static const struct snd_kcontrol_new snd_es18xx_opt_1879[] = { ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0), ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0), ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0) }; static const struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = { ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0), }; static const struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = { ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0), ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0) }; static const struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = { ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0), { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "3D Control - Switch", .info = snd_es18xx_info_spatializer_enable, .get = snd_es18xx_get_spatializer_enable, .put = snd_es18xx_put_spatializer_enable, } }; static const struct snd_kcontrol_new snd_es18xx_micpre1_control = ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0); static const struct snd_kcontrol_new snd_es18xx_micpre2_control = ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0); static const struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = { { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Hardware Master Playback Volume", .access = SNDRV_CTL_ELEM_ACCESS_READ, .info = snd_es18xx_info_hw_volume, .get = snd_es18xx_get_hw_volume, }, { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Hardware Master Playback Switch", .access = SNDRV_CTL_ELEM_ACCESS_READ, .info = snd_es18xx_info_hw_switch, .get = snd_es18xx_get_hw_switch, }, ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0), }; static const struct snd_kcontrol_new snd_es18xx_opt_gpo_2bit[] = { ES18XX_SINGLE("GPO0 Switch", 0, ES18XX_PM, 0, 1, ES18XX_FL_PMPORT), ES18XX_SINGLE("GPO1 Switch", 0, ES18XX_PM, 1, 1, ES18XX_FL_PMPORT), }; static int snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg) { outb(reg, chip->ctrl_port); return inb(chip->ctrl_port + 1); } static void snd_es18xx_config_write(struct snd_es18xx *chip, unsigned char reg, unsigned char data) { /* No need for spinlocks, this function is used only in otherwise protected init code */ outb(reg, chip->ctrl_port); outb(data, chip->ctrl_port + 1); #ifdef REG_DEBUG snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data); #endif } static int snd_es18xx_initialize(struct snd_es18xx *chip, unsigned long mpu_port, unsigned long fm_port) { int mask = 0; /* enable extended mode */ snd_es18xx_dsp_command(chip, 0xC6); /* Reset mixer registers */ snd_es18xx_mixer_write(chip, 0x00, 0x00); /* Audio 1 DMA demand mode (4 bytes/request) */ snd_es18xx_write(chip, 0xB9, 2); if (chip->caps & ES18XX_CONTROL) { /* Hardware volume IRQ */ snd_es18xx_config_write(chip, 0x27, chip->irq); if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) { /* FM I/O */ snd_es18xx_config_write(chip, 0x62, fm_port >> 8); snd_es18xx_config_write(chip, 0x63, fm_port & 0xff); } if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) { /* MPU-401 I/O */ snd_es18xx_config_write(chip, 0x64, mpu_port >> 8); snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff); /* MPU-401 IRQ */ snd_es18xx_config_write(chip, 0x28, chip->irq); } /* Audio1 IRQ */ snd_es18xx_config_write(chip, 0x70, chip->irq); /* Audio2 IRQ */ snd_es18xx_config_write(chip, 0x72, chip->irq); /* Audio1 DMA */ snd_es18xx_config_write(chip, 0x74, chip->dma1); /* Audio2 DMA */ snd_es18xx_config_write(chip, 0x75, chip->dma2); /* Enable Audio 1 IRQ */ snd_es18xx_write(chip, 0xB1, 0x50); /* Enable Audio 2 IRQ */ snd_es18xx_mixer_write(chip, 0x7A, 0x40); /* Enable Audio 1 DMA */ snd_es18xx_write(chip, 0xB2, 0x50); /* Enable MPU and hardware volume interrupt */ snd_es18xx_mixer_write(chip, 0x64, 0x42); /* Enable ESS wavetable input */ snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10); } else { int irqmask, dma1mask, dma2mask; switch (chip->irq) { case 2: case 9: irqmask = 0; break; case 5: irqmask = 1; break; case 7: irqmask = 2; break; case 10: irqmask = 3; break; default: snd_printk(KERN_ERR "invalid irq %d\n", chip->irq); return -ENODEV; } switch (chip->dma1) { case 0: dma1mask = 1; break; case 1: dma1mask = 2; break; case 3: dma1mask = 3; break; default: snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1); return -ENODEV; } switch (chip->dma2) { case 0: dma2mask = 0; break; case 1: dma2mask = 1; break; case 3: dma2mask = 2; break; case 5: dma2mask = 3; break; default: snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2); return -ENODEV; } /* Enable and set Audio 1 IRQ */ snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2)); /* Enable and set Audio 1 DMA */ snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2)); /* Set Audio 2 DMA */ snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask); /* Enable Audio 2 IRQ and DMA Set capture mixer input */ snd_es18xx_mixer_write(chip, 0x7A, 0x68); /* Enable and set hardware volume interrupt */ snd_es18xx_mixer_write(chip, 0x64, 0x06); if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) { /* MPU401 share irq with audio Joystick enabled FM enabled */ snd_es18xx_mixer_write(chip, 0x40, 0x43 | (mpu_port & 0xf0) >> 1); } snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01); } if (chip->caps & ES18XX_NEW_RATE) { /* Change behaviour of register A1 4x oversampling 2nd channel DAC asynchronous */ snd_es18xx_mixer_write(chip, 0x71, 0x32); } if (!(chip->caps & ES18XX_PCM2)) { /* Enable DMA FIFO */ snd_es18xx_write(chip, 0xB7, 0x80); } if (chip->caps & ES18XX_SPATIALIZER) { /* Set spatializer parameters to recommended values */ snd_es18xx_mixer_write(chip, 0x54, 0x8f); snd_es18xx_mixer_write(chip, 0x56, 0x95); snd_es18xx_mixer_write(chip, 0x58, 0x94); snd_es18xx_mixer_write(chip, 0x5a, 0x80); } /* Flip the "enable I2S" bits for those chipsets that need it */ switch (chip->version) { case 0x1879: //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow) //so a Switch control has been added to toggle this 0x71 bit on/off: //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40); /* Note: we fall through on purpose here. */ case 0x1878: snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40); break; } /* Mute input source */ if (chip->caps & ES18XX_MUTEREC) mask = 0x10; if (chip->caps & ES18XX_RECMIX) snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask); else { snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask); snd_es18xx_write(chip, 0xb4, 0x00); } #ifndef AVOID_POPS /* Enable PCM output */ snd_es18xx_dsp_command(chip, 0xD1); #endif return 0; } static int snd_es18xx_identify(struct snd_card *card, struct snd_es18xx *chip) { int hi,lo; /* reset */ if (snd_es18xx_reset(chip) < 0) { snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port); return -ENODEV; } snd_es18xx_dsp_command(chip, 0xe7); hi = snd_es18xx_dsp_get_byte(chip); if (hi < 0) { return hi; } lo = snd_es18xx_dsp_get_byte(chip); if ((lo & 0xf0) != 0x80) { return -ENODEV; } if (hi == 0x48) { chip->version = 0x488; return 0; } if (hi != 0x68) { return -ENODEV; } if ((lo & 0x0f) < 8) { chip->version = 0x688; return 0; } outb(0x40, chip->port + 0x04); udelay(10); hi = inb(chip->port + 0x05); udelay(10); lo = inb(chip->port + 0x05); if (hi != lo) { chip->version = hi << 8 | lo; chip->ctrl_port = inb(chip->port + 0x05) << 8; udelay(10); chip->ctrl_port += inb(chip->port + 0x05); if (!devm_request_region(card->dev, chip->ctrl_port, 8, "ES18xx - CTRL")) { snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port); return -EBUSY; } return 0; } /* If has Hardware volume */ if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) { /* If has Audio2 */ if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) { /* If has volume count */ if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) { chip->version = 0x1887; } else { chip->version = 0x1888; } } else { chip->version = 0x1788; } } else chip->version = 0x1688; return 0; } static int snd_es18xx_probe(struct snd_card *card, struct snd_es18xx *chip, unsigned long mpu_port, unsigned long fm_port) { if (snd_es18xx_identify(card, chip) < 0) { snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port); return -ENODEV; } switch (chip->version) { case 0x1868: chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT; break; case 0x1869: chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV | ES18XX_GPO_2BIT; break; case 0x1878: chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL; break; case 0x1879: chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV; break; case 0x1887: case 0x1888: chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT; break; default: snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n", chip->port, chip->version); return -ENODEV; } snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version); if (chip->dma1 == chip->dma2) chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME); return snd_es18xx_initialize(chip, mpu_port, fm_port); } static const struct snd_pcm_ops snd_es18xx_playback_ops = { .open = snd_es18xx_playback_open, .close = snd_es18xx_playback_close, .hw_params = snd_es18xx_playback_hw_params, .prepare = snd_es18xx_playback_prepare, .trigger = snd_es18xx_playback_trigger, .pointer = snd_es18xx_playback_pointer, }; static const struct snd_pcm_ops snd_es18xx_capture_ops = { .open = snd_es18xx_capture_open, .close = snd_es18xx_capture_close, .hw_params = snd_es18xx_capture_hw_params, .prepare = snd_es18xx_capture_prepare, .trigger = snd_es18xx_capture_trigger, .pointer = snd_es18xx_capture_pointer, }; static int snd_es18xx_pcm(struct snd_card *card, int device) { struct snd_es18xx *chip = card->private_data; struct snd_pcm *pcm; char str[16]; int err; sprintf(str, "ES%x", chip->version); if (chip->caps & ES18XX_PCM2) err = snd_pcm_new(card, str, device, 2, 1, &pcm); else err = snd_pcm_new(card, str, device, 1, 1, &pcm); if (err < 0) return err; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops); /* global setup */ pcm->private_data = chip; pcm->info_flags = 0; if (chip->caps & ES18XX_DUPLEX_SAME) pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX; if (! (chip->caps & ES18XX_PCM2)) pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX; sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version); chip->pcm = pcm; snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024); return 0; } /* Power Management support functions */ #ifdef CONFIG_PM static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state) { struct snd_es18xx *chip = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); /* power down */ chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM); chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS); snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg); snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS); return 0; } static int snd_es18xx_resume(struct snd_card *card) { struct snd_es18xx *chip = card->private_data; /* restore PM register, we won't wake till (not 0x07) i/o activity though */ snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif /* CONFIG_PM */ static int snd_es18xx_new_device(struct snd_card *card, unsigned long port, unsigned long mpu_port, unsigned long fm_port, int irq, int dma1, int dma2) { struct snd_es18xx *chip = card->private_data; spin_lock_init(&chip->reg_lock); spin_lock_init(&chip->mixer_lock); chip->port = port; chip->irq = -1; chip->dma1 = -1; chip->dma2 = -1; chip->audio2_vol = 0x00; chip->active = 0; if (!devm_request_region(card->dev, port, 16, "ES18xx")) { snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1); return -EBUSY; } if (devm_request_irq(card->dev, irq, snd_es18xx_interrupt, 0, "ES18xx", (void *) card)) { snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq); return -EBUSY; } chip->irq = irq; card->sync_irq = chip->irq; if (snd_devm_request_dma(card->dev, dma1, "ES18xx DMA 1")) { snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1); return -EBUSY; } chip->dma1 = dma1; if (dma2 != dma1 && snd_devm_request_dma(card->dev, dma2, "ES18xx DMA 2")) { snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2); return -EBUSY; } chip->dma2 = dma2; if (snd_es18xx_probe(card, chip, mpu_port, fm_port) < 0) return -ENODEV; return 0; } static int snd_es18xx_mixer(struct snd_card *card) { struct snd_es18xx *chip = card->private_data; int err; unsigned int idx; strcpy(card->mixername, chip->pcm->name); for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) { struct snd_kcontrol *kctl; kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip); if (chip->caps & ES18XX_HWV) { switch (idx) { case 0: chip->master_volume = kctl; kctl->private_free = snd_es18xx_hwv_free; break; case 1: chip->master_switch = kctl; kctl->private_free = snd_es18xx_hwv_free; break; } } err = snd_ctl_add(card, kctl); if (err < 0) return err; } if (chip->caps & ES18XX_PCM2) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip)); if (err < 0) return err; } } else { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip)); if (err < 0) return err; } } if (chip->caps & ES18XX_RECMIX) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip)); if (err < 0) return err; } } switch (chip->version) { default: err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip)); if (err < 0) return err; break; case 0x1869: case 0x1879: err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip)); if (err < 0) return err; break; } if (chip->caps & ES18XX_SPATIALIZER) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip)); if (err < 0) return err; } } if (chip->caps & ES18XX_HWV) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) { struct snd_kcontrol *kctl; kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip); if (idx == 0) chip->hw_volume = kctl; else chip->hw_switch = kctl; kctl->private_free = snd_es18xx_hwv_free; err = snd_ctl_add(card, kctl); if (err < 0) return err; } } /* finish initializing other chipset specific controls */ if (chip->version != 0x1868) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker, chip)); if (err < 0) return err; } if (chip->version == 0x1869) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1869[idx], chip)); if (err < 0) return err; } } else if (chip->version == 0x1878) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878, chip)); if (err < 0) return err; } else if (chip->version == 0x1879) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1879[idx], chip)); if (err < 0) return err; } } if (chip->caps & ES18XX_GPO_2BIT) { for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx], chip)); if (err < 0) return err; } } return 0; } /* Card level */ MODULE_AUTHOR("Christian Fischbach <[email protected]>, Abramo Bagnara <[email protected]>"); MODULE_DESCRIPTION("ESS ES18xx AudioDrive"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; #endif static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */ #ifndef CONFIG_PNP static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1}; #else static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; #endif static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for ES18xx soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for ES18xx soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable ES18xx soundcard."); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard."); #endif module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for ES18xx driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver."); module_param_hw_array(fm_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver."); #ifdef CONFIG_PNP static int isa_registered; static int pnp_registered; static int pnpc_registered; static const struct pnp_device_id snd_audiodrive_pnpbiosids[] = { { .id = "ESS1869" }, { .id = "ESS1879" }, { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids); /* PnP main device initialization */ static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev) { if (pnp_activate_dev(pdev) < 0) { snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n"); return -EBUSY; } /* ok. hack using Vendor-Defined Card-Level registers */ /* skip csn and logdev initialization - already done in isapnp_configure */ if (pnp_device_is_isapnp(pdev)) { isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev)); isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */ if (mpu_port[dev] != SNDRV_AUTO_PORT) isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */ isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */ isapnp_cfg_end(); } port[dev] = pnp_port_start(pdev, 0); fm_port[dev] = pnp_port_start(pdev, 1); mpu_port[dev] = pnp_port_start(pdev, 2); dma1[dev] = pnp_dma(pdev, 0); dma2[dev] = pnp_dma(pdev, 1); irq[dev] = pnp_irq(pdev, 0); snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]); snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]); return 0; } static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip, struct pnp_dev *pdev) { chip->dev = pdev; if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0) return -EBUSY; return 0; } static const struct pnp_card_device_id snd_audiodrive_pnpids[] = { /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */ { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } }, /* ESS 1868 (integrated on Maxisound Cards) */ { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } }, /* ESS 1868 (integrated on Maxisound Cards) */ { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } }, /* ESS ES1869 Plug and Play AudioDrive */ { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } }, /* ESS 1869 */ { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } }, /* ESS 1878 */ { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } }, /* ESS 1879 */ { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } }, /* --- */ { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids); static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip, struct pnp_card_link *card, const struct pnp_card_device_id *id) { chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL); if (chip->dev == NULL) return -EBUSY; chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL); if (chip->devc == NULL) return -EBUSY; /* Control port initialization */ if (pnp_activate_dev(chip->devc) < 0) { snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n"); return -EAGAIN; } snd_printdd("pnp: port=0x%llx\n", (unsigned long long)pnp_port_start(chip->devc, 0)); if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0) return -EBUSY; return 0; } #endif /* CONFIG_PNP */ #ifdef CONFIG_PNP #define is_isapnp_selected(dev) isapnp[dev] #else #define is_isapnp_selected(dev) 0 #endif static int snd_es18xx_card_new(struct device *pdev, int dev, struct snd_card **cardp) { return snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_es18xx), cardp); } static int snd_audiodrive_probe(struct snd_card *card, int dev) { struct snd_es18xx *chip = card->private_data; struct snd_opl3 *opl3; int err; err = snd_es18xx_new_device(card, port[dev], mpu_port[dev], fm_port[dev], irq[dev], dma1[dev], dma2[dev]); if (err < 0) return err; sprintf(card->driver, "ES%x", chip->version); sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version); if (dma1[dev] != dma2[dev]) sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d", card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]); else sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d", card->shortname, chip->port, irq[dev], dma1[dev]); err = snd_es18xx_pcm(card, 0); if (err < 0) return err; err = snd_es18xx_mixer(card); if (err < 0) return err; if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) { if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_OPL3, 0, &opl3) < 0) { snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", fm_port[dev]); } else { err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } } if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) { err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX, mpu_port[dev], MPU401_INFO_IRQ_HOOK, -1, &chip->rmidi); if (err < 0) return err; } return snd_card_register(card); } static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev) { return enable[dev] && !is_isapnp_selected(dev); } static int snd_es18xx_isa_probe1(int dev, struct device *devptr) { struct snd_card *card; int err; err = snd_es18xx_card_new(devptr, dev, &card); if (err < 0) return err; err = snd_audiodrive_probe(card, dev); if (err < 0) return err; dev_set_drvdata(devptr, card); return 0; } static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev) { int err; static const int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1}; static const int possible_dmas[] = {1, 0, 3, 5, -1}; if (irq[dev] == SNDRV_AUTO_IRQ) { irq[dev] = snd_legacy_find_free_irq(possible_irqs); if (irq[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); return -EBUSY; } } if (dma1[dev] == SNDRV_AUTO_DMA) { dma1[dev] = snd_legacy_find_free_dma(possible_dmas); if (dma1[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA1\n"); return -EBUSY; } } if (dma2[dev] == SNDRV_AUTO_DMA) { dma2[dev] = snd_legacy_find_free_dma(possible_dmas); if (dma2[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA2\n"); return -EBUSY; } } if (port[dev] != SNDRV_AUTO_PORT) { return snd_es18xx_isa_probe1(dev, pdev); } else { static const unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280}; int i; for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { port[dev] = possible_ports[i]; err = snd_es18xx_isa_probe1(dev, pdev); if (! err) return 0; } return err; } } #ifdef CONFIG_PM static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n, pm_message_t state) { return snd_es18xx_suspend(dev_get_drvdata(dev), state); } static int snd_es18xx_isa_resume(struct device *dev, unsigned int n) { return snd_es18xx_resume(dev_get_drvdata(dev)); } #endif #define DEV_NAME "es18xx" static struct isa_driver snd_es18xx_isa_driver = { .match = snd_es18xx_isa_match, .probe = snd_es18xx_isa_probe, #ifdef CONFIG_PM .suspend = snd_es18xx_isa_suspend, .resume = snd_es18xx_isa_resume, #endif .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev, const struct pnp_device_id *id) { static int dev; int err; struct snd_card *card; if (pnp_device_is_isapnp(pdev)) return -ENOENT; /* we have another procedure - card */ for (; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; err = snd_es18xx_card_new(&pdev->dev, dev, &card); if (err < 0) return err; err = snd_audiodrive_pnp(dev, card->private_data, pdev); if (err < 0) return err; err = snd_audiodrive_probe(card, dev); if (err < 0) return err; pnp_set_drvdata(pdev, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state) { return snd_es18xx_suspend(pnp_get_drvdata(pdev), state); } static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev) { return snd_es18xx_resume(pnp_get_drvdata(pdev)); } #endif static struct pnp_driver es18xx_pnp_driver = { .name = "es18xx-pnpbios", .id_table = snd_audiodrive_pnpbiosids, .probe = snd_audiodrive_pnp_detect, #ifdef CONFIG_PM .suspend = snd_audiodrive_pnp_suspend, .resume = snd_audiodrive_pnp_resume, #endif }; static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int dev; struct snd_card *card; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; res = snd_es18xx_card_new(&pcard->card->dev, dev, &card); if (res < 0) return res; res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid); if (res < 0) return res; res = snd_audiodrive_probe(card, dev); if (res < 0) return res; pnp_set_card_drvdata(pcard, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state) { return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state); } static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard) { return snd_es18xx_resume(pnp_get_card_drvdata(pcard)); } #endif static struct pnp_card_driver es18xx_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "es18xx", .id_table = snd_audiodrive_pnpids, .probe = snd_audiodrive_pnpc_detect, #ifdef CONFIG_PM .suspend = snd_audiodrive_pnpc_suspend, .resume = snd_audiodrive_pnpc_resume, #endif }; #endif /* CONFIG_PNP */ static int __init alsa_card_es18xx_init(void) { int err; err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_driver(&es18xx_pnp_driver); if (!err) pnp_registered = 1; err = pnp_register_card_driver(&es18xx_pnpc_driver); if (!err) pnpc_registered = 1; if (isa_registered || pnp_registered) err = 0; #endif return err; } static void __exit alsa_card_es18xx_exit(void) { #ifdef CONFIG_PNP if (pnpc_registered) pnp_unregister_card_driver(&es18xx_pnpc_driver); if (pnp_registered) pnp_unregister_driver(&es18xx_pnp_driver); if (isa_registered) #endif isa_unregister_driver(&snd_es18xx_isa_driver); } module_init(alsa_card_es18xx_init) module_exit(alsa_card_es18xx_exit)
linux-master
sound/isa/es18xx.c
// 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. */ #include <linux/init.h> #include <linux/err.h> #include <linux/io.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/firmware.h> #include <linux/pnp.h> #include <linux/spinlock.h> #include <linux/module.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/initval.h> MODULE_AUTHOR("Chris Rankin"); MODULE_DESCRIPTION("ENSONIQ SoundScape driver"); MODULE_LICENSE("GPL"); MODULE_FIRMWARE("sndscape.co0"); MODULE_FIRMWARE("sndscape.co1"); MODULE_FIRMWARE("sndscape.co2"); MODULE_FIRMWARE("sndscape.co3"); MODULE_FIRMWARE("sndscape.co4"); MODULE_FIRMWARE("scope.cod"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int dma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static bool joystick[SNDRV_CARDS]; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index number for SoundScape soundcard"); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "Description for SoundScape card"); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for SoundScape driver."); module_param_hw_array(wss_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver."); module_param_hw_array(dma, int, dma, NULL, 0444); MODULE_PARM_DESC(dma, "DMA # for SoundScape driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver."); module_param_array(joystick, bool, NULL, 0444); MODULE_PARM_DESC(joystick, "Enable gameport."); #ifdef CONFIG_PNP static int isa_registered; static int pnp_registered; static const struct pnp_card_device_id sscape_pnpids[] = { { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */ { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */ { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids); #endif #define HOST_CTRL_IO(i) ((i) + 2) #define HOST_DATA_IO(i) ((i) + 3) #define ODIE_ADDR_IO(i) ((i) + 4) #define ODIE_DATA_IO(i) ((i) + 5) #define CODEC_IO(i) ((i) + 8) #define IC_ODIE 1 #define IC_OPUS 2 #define RX_READY 0x01 #define TX_READY 0x02 #define CMD_ACK 0x80 #define CMD_SET_MIDI_VOL 0x84 #define CMD_GET_MIDI_VOL 0x85 #define CMD_XXX_MIDI_VOL 0x86 #define CMD_SET_EXTMIDI 0x8a #define CMD_GET_EXTMIDI 0x8b #define CMD_SET_MT32 0x8c #define CMD_GET_MT32 0x8d enum GA_REG { GA_INTSTAT_REG = 0, GA_INTENA_REG, GA_DMAA_REG, GA_DMAB_REG, GA_INTCFG_REG, GA_DMACFG_REG, GA_CDCFG_REG, GA_SMCFGA_REG, GA_SMCFGB_REG, GA_HMCTL_REG }; #define DMA_8BIT 0x80 enum card_type { MEDIA_FX, /* Sequoia S-1000 */ SSCAPE, /* Sequoia S-2000 */ SSCAPE_PNP, SSCAPE_VIVO, }; struct soundscape { spinlock_t lock; unsigned io_base; int ic_type; enum card_type type; struct resource *io_res; struct resource *wss_res; struct snd_wss *chip; unsigned char midi_vol; }; #define INVALID_IRQ ((unsigned)-1) static inline struct soundscape *get_card_soundscape(struct snd_card *c) { return (struct soundscape *) (c->private_data); } /* * 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. */ static struct snd_dma_buffer *get_dmabuf(struct soundscape *s, struct snd_dma_buffer *buf, unsigned long size) { if (buf) { if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, s->chip->card->dev, size, buf) < 0) { snd_printk(KERN_ERR "sscape: Failed to allocate " "%lu bytes for DMA\n", size); return NULL; } } return buf; } /* * Release the DMA-able kernel memory ... */ static void 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" ... */ static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char 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 ... */ static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val) { unsigned long flags; spin_lock_irqsave(&s->lock, flags); sscape_write_unsafe(s->io_base, reg, val); spin_unlock_irqrestore(&s->lock, flags); } /* * Read from the SoundScape's control registers, but leave any * locking to the caller. This is why the function is "unsafe" ... */ static inline unsigned char 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 */ static inline void 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 */ static inline void 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 ... */ static inline int 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 ... */ static int 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 ... */ static inline int host_write_unsafe(unsigned io_base, unsigned char 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 ... */ static int host_write_ctrl_unsafe(unsigned io_base, unsigned char 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. */ static inline int verify_mpu401(const struct snd_mpu401 *mpu) { return ((inb(MPU401C(mpu)) & 0xc0) == 0x80); } /* * This is apparently the standard way to initialise an MPU-401 */ static inline void initialise_mpu401(const struct 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 ... */ static void activate_ad1845_unsafe(unsigned io_base) { unsigned char 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. */ static void 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. */ static int 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. */ static int obp_startup_ack(struct soundscape *s, unsigned timeout) { unsigned long end_time = jiffies + msecs_to_jiffies(timeout); do { unsigned long 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. */ static int host_startup_ack(struct soundscape *s, unsigned timeout) { unsigned long end_time = jiffies + msecs_to_jiffies(timeout); do { unsigned long 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. */ static int upload_dma_data(struct soundscape *s, const unsigned char *data, size_t size) { unsigned long flags; struct snd_dma_buffer dma; int ret; unsigned char 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) { unsigned long 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); snd_printk(KERN_ERR "sscape: DMA upload has timed out\n"); ret = -EAGAIN; goto _release_dma; } } /* while */ set_host_mode_unsafe(s->io_base); outb(0x0, s->io_base); /* * 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)) { snd_printk(KERN_ERR "sscape: No response " "from on-board processor after upload\n"); ret = -EAGAIN; } else if (!host_startup_ack(s, 5000)) { snd_printk(KERN_ERR "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. */ static int sscape_upload_bootblock(struct snd_card *card) { struct soundscape *sscape = get_card_soundscape(card); unsigned long flags; const struct firmware *init_fw = NULL; int data = 0; int ret; ret = request_firmware(&init_fw, "scope.cod", card->dev); if (ret < 0) { snd_printk(KERN_ERR "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) { snd_printk(KERN_ERR "sscape: timeout reading firmware version\n"); ret = -EAGAIN; } return (ret == 0) ? data : ret; } /* * Upload the microcode into the SoundScape. */ static int sscape_upload_microcode(struct snd_card *card, int version) { struct soundscape *sscape = get_card_soundscape(card); const struct firmware *init_fw = NULL; char name[14]; int err; scnprintf(name, sizeof(name), "sndscape.co%d", version); err = request_firmware(&init_fw, name, card->dev); if (err < 0) { snd_printk(KERN_ERR "sscape: Error loading sndscape.co%d", version); return err; } err = upload_dma_data(sscape, init_fw->data, init_fw->size); if (err == 0) snd_printk(KERN_INFO "sscape: MIDI firmware loaded %zu KBs\n", init_fw->size >> 10); release_firmware(init_fw); return err; } /* * Mixer control for the SoundScape's MIDI device. */ static int sscape_midi_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 127; return 0; } static int sscape_midi_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *uctl) { struct snd_wss *chip = snd_kcontrol_chip(kctl); struct snd_card *card = chip->card; register struct soundscape *s = get_card_soundscape(card); unsigned long flags; spin_lock_irqsave(&s->lock, flags); uctl->value.integer.value[0] = s->midi_vol; spin_unlock_irqrestore(&s->lock, flags); return 0; } static int sscape_midi_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *uctl) { struct snd_wss *chip = snd_kcontrol_chip(kctl); struct snd_card *card = chip->card; struct soundscape *s = get_card_soundscape(card); unsigned long flags; int change; unsigned char new_val; spin_lock_irqsave(&s->lock, flags); 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); spin_unlock_irqrestore(&s->lock, flags); return change; } static const struct snd_kcontrol_new midi_mixer_ctl = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "MIDI", .info = sscape_midi_info, .get = sscape_midi_get, .put = sscape_midi_put }; /* * 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. */ static unsigned get_irq_config(int sscape_type, int irq) { static const int valid_irq[] = { 9, 5, 7, 10 }; static const int 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. */ static int detect_sscape(struct soundscape *s, long wss_io) { unsigned long 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; if (d == 0) s->ic_type = IC_ODIE; else if ((d & 0x60) != 0) s->ic_type = IC_OPUS; else goto _done; outb(0xfa, ODIE_ADDR_IO(s->io_base)); if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a) goto _done; outb(0xfe, ODIE_ADDR_IO(s->io_base)); if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e) goto _done; outb(0xfe, ODIE_ADDR_IO(s->io_base)); d = inb(ODIE_DATA_IO(s->io_base)); if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e) goto _done; if (s->ic_type == IC_OPUS) activate_ad1845_unsafe(s->io_base); if (s->type == SSCAPE_VIVO) wss_io += 4; d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0); /* wait for WSS codec */ for (d = 0; d < 500; d++) { if ((inb(wss_io) & 0x80) == 0) break; spin_unlock_irqrestore(&s->lock, flags); msleep(1); spin_lock_irqsave(&s->lock, flags); } if ((inb(wss_io) & 0x80) != 0) goto _done; if (inb(wss_io + 2) == 0xff) goto _done; d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f; sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d); if ((inb(wss_io) & 0x80) != 0) s->type = MEDIA_FX; d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0); /* wait for WSS codec */ for (d = 0; d < 500; d++) { if ((inb(wss_io) & 0x80) == 0) break; spin_unlock_irqrestore(&s->lock, flags); msleep(1); spin_lock_irqsave(&s->lock, flags); } /* * SoundScape successfully detected! */ retval = 1; _done: spin_unlock_irqrestore(&s->lock, flags); return retval; } /* * 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. */ static int mpu401_open(struct snd_mpu401 *mpu) { if (!verify_mpu401(mpu)) { snd_printk(KERN_ERR "sscape: MIDI disabled, " "please load firmware\n"); return -ENODEV; } return 0; } /* * Initialise an MPU-401 subdevice for MIDI support on the SoundScape. */ static int create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq) { struct soundscape *sscape = get_card_soundscape(card); struct snd_rawmidi *rawmidi; int err; err = snd_mpu401_uart_new(card, devnum, MPU401_HW_MPU401, port, MPU401_INFO_INTEGRATED, irq, &rawmidi); if (err == 0) { struct snd_mpu401 *mpu = rawmidi->private_data; mpu->open_input = mpu401_open; mpu->open_output = mpu401_open; mpu->private_data = sscape; initialise_mpu401(mpu); } return 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. */ static int create_ad1845(struct snd_card *card, unsigned port, int irq, int dma1, int dma2) { register struct 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; } err = snd_wss_create(card, port, -1, irq, dma1, dma2, codec_type, WSS_HWSHARE_DMA1, &chip); if (!err) { unsigned long flags; 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) { snd_printk(KERN_ERR "sscape: No PCM device " "for AD1845 chip\n"); goto _error; } err = snd_wss_mixer(chip); if (err < 0) { snd_printk(KERN_ERR "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) { snd_printk(KERN_ERR "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) { snd_printk(KERN_ERR "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. */ static int 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; unsigned long flags; int err; int val; const char *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) { snd_printk(KERN_ERR "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) { snd_printk(KERN_ERR "sscape: can't grab port 0x%lx\n", wss_port[dev]); return -EBUSY; } } /* * Grab one DMA channel ... */ err = snd_devm_request_dma(card->dev, dma[dev], "SoundScape"); if (err < 0) { snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]); return err; } spin_lock_init(&sscape->lock); sscape->io_res = io_res; sscape->wss_res = wss_res; sscape->io_base = port[dev]; if (!detect_sscape(sscape, wss_port[dev])) { printk(KERN_ERR "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; } printk(KERN_INFO "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) { snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]); return -ENXIO; } mpu_irq_cfg = get_irq_config(sscape->type, mpu_irq[dev]); if (mpu_irq_cfg == INVALID_IRQ) { snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_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); sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e); sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00); /* * Enable and configure the DMA channels ... */ sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50); dma_cfg = (sscape->ic_type == IC_OPUS ? 0x40 : 0x70); sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg); sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20); mpu_irq_cfg |= mpu_irq_cfg << 2; val = sscape_read_unsafe(sscape->io_base, GA_HMCTL_REG) & 0xF7; if (joystick[dev]) val |= 8; sscape_write_unsafe(sscape->io_base, GA_HMCTL_REG, val | 0x10); sscape_write_unsafe(sscape->io_base, GA_INTCFG_REG, 0xf0 | mpu_irq_cfg); sscape_write_unsafe(sscape->io_base, GA_CDCFG_REG, 0x09 | DMA_8BIT | (dma[dev] << 4) | (irq_cfg << 1)); /* * Enable the master IRQ ... */ sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x80); spin_unlock_irqrestore(&sscape->lock, flags); /* * We have now enabled the codec chip, and so we should * detect the AD1845 device ... */ err = create_ad1845(card, wss_port[dev], irq[dev], dma[dev], dma2[dev]); if (err < 0) { snd_printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n", wss_port[dev], irq[dev]); return err; } strcpy(card->driver, "SoundScape"); strcpy(card->shortname, name); snprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n", name, sscape->chip->port, sscape->chip->irq, sscape->chip->dma1, sscape->chip->dma2); #define MIDI_DEVNUM 0 if (sscape->type != SSCAPE_VIVO) { err = sscape_upload_bootblock(card); if (err >= 0) err = sscape_upload_microcode(card, err); if (err == 0) { err = create_mpu401(card, MIDI_DEVNUM, port[dev], mpu_irq[dev]); if (err < 0) { snd_printk(KERN_ERR "sscape: Failed to create " "MPU-401 device at 0x%lx\n", port[dev]); return err; } /* * Initialize mixer */ spin_lock_irqsave(&sscape->lock, flags); sscape->midi_vol = 0; host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100); host_write_ctrl_unsafe(sscape->io_base, sscape->midi_vol, 100); host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100); host_write_ctrl_unsafe(sscape->io_base, sscape->midi_vol, 100); host_write_ctrl_unsafe(sscape->io_base, CMD_SET_EXTMIDI, 100); host_write_ctrl_unsafe(sscape->io_base, 0, 100); host_write_ctrl_unsafe(sscape->io_base, CMD_ACK, 100); set_midi_mode_unsafe(sscape->io_base); spin_unlock_irqrestore(&sscape->lock, flags); } } return 0; } static int snd_sscape_match(struct device *pdev, unsigned int 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) { printk(KERN_INFO "sscape: insufficient parameters, " "need IO, IRQ, MPU-IRQ and DMA\n"); return 0; } return 1; } static int snd_sscape_probe(struct device *pdev, unsigned int dev) { struct snd_card *card; struct soundscape *sscape; int ret; ret = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct soundscape), &card); if (ret < 0) return ret; sscape = get_card_soundscape(card); sscape->type = SSCAPE; dma[dev] &= 0x03; ret = create_sscape(dev, card); if (ret < 0) return ret; ret = snd_card_register(card); if (ret < 0) { snd_printk(KERN_ERR "sscape: Failed to register sound card\n"); return ret; } dev_set_drvdata(pdev, card); return 0; } #define DEV_NAME "sscape" static struct isa_driver snd_sscape_driver = { .match = snd_sscape_match, .probe = snd_sscape_probe, /* FIXME: suspend/resume */ .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static inline int get_next_autoindex(int i) { while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT) ++i; return i; } static int sscape_pnp_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int idx = 0; struct pnp_dev *dev; struct snd_card *card; struct soundscape *sscape; int ret; /* * 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) { snd_printk(KERN_INFO "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; sscape = get_card_soundscape(card); /* * Identify card model ... */ if (!strncmp("ENS4081", pid->id, 7)) sscape->type = SSCAPE_VIVO; else sscape->type = SSCAPE_PNP; /* * Read the correct parameters off the ISA PnP bus ... */ port[idx] = pnp_port_start(dev, 0); irq[idx] = pnp_irq(dev, 0); mpu_irq[idx] = pnp_irq(dev, 1); dma[idx] = pnp_dma(dev, 0) & 0x03; if (sscape->type == SSCAPE_PNP) { dma2[idx] = dma[idx]; wss_port[idx] = CODEC_IO(port[idx]); } else { wss_port[idx] = pnp_port_start(dev, 1); dma2[idx] = pnp_dma(dev, 1); } ret = create_sscape(idx, card); if (ret < 0) return ret; ret = snd_card_register(card); if (ret < 0) { snd_printk(KERN_ERR "sscape: Failed to register sound card\n"); return ret; } pnp_set_card_drvdata(pcard, card); ++idx; return 0; } static struct pnp_card_driver sscape_pnpc_driver = { .flags = PNP_DRIVER_RES_DO_NOT_CHANGE, .name = "sscape", .id_table = sscape_pnpids, .probe = sscape_pnp_detect, }; #endif /* CONFIG_PNP */ static int __init sscape_init(void) { int err; err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_card_driver(&sscape_pnpc_driver); if (!err) pnp_registered = 1; if (isa_registered) err = 0; #endif return err; } static void __exit sscape_exit(void) { #ifdef CONFIG_PNP if (pnp_registered) pnp_unregister_card_driver(&sscape_pnpc_driver); if (isa_registered) #endif isa_unregister_driver(&snd_sscape_driver); } module_init(sscape_init); module_exit(sscape_exit);
linux-master
sound/isa/sscape.c
// SPDX-License-Identifier: GPL-2.0-or-later /* card-azt2320.c - driver for Aztech Systems AZT2320 based soundcards. Copyright (C) 1999-2000 by Massimo Piccioni <[email protected]> */ /* This driver should provide support for most Aztech AZT2320 based cards. Several AZT2316 chips are also supported/tested, but autoprobe doesn't work: all module option have to be set. No docs available for us at Aztech headquarters !!! Unbelievable ... No other help obtained. Thanks to Rainer Wiesner <[email protected]> for the WSS activation method (full-duplex audio!). */ #include <linux/io.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/time.h> #include <linux/wait.h> #include <linux/pnp.h> #include <linux/module.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/opl3.h> #define PFX "azt2320: " MODULE_AUTHOR("Massimo Piccioni <[email protected]>"); MODULE_DESCRIPTION("Aztech Systems AZT2320"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* Pnp setup */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* Pnp setup */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for azt2320 based soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for azt2320 based soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable azt2320 based soundcard."); struct snd_card_azt2320 { int dev_no; struct pnp_dev *dev; struct pnp_dev *devmpu; struct snd_wss *chip; }; static const struct pnp_card_device_id snd_azt2320_pnpids[] = { /* PRO16V */ { .id = "AZT1008", .devs = { { "AZT1008" }, { "AZT2001" }, } }, /* Aztech Sound Galaxy 16 */ { .id = "AZT2320", .devs = { { "AZT0001" }, { "AZT0002" }, } }, /* Packard Bell Sound III 336 AM/SP */ { .id = "AZT3000", .devs = { { "AZT1003" }, { "AZT2001" }, } }, /* AT3300 */ { .id = "AZT3002", .devs = { { "AZT1004" }, { "AZT2001" }, } }, /* --- */ { .id = "AZT3005", .devs = { { "AZT1003" }, { "AZT2001" }, } }, /* --- */ { .id = "AZT3011", .devs = { { "AZT1003" }, { "AZT2001" }, } }, { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, snd_azt2320_pnpids); #define DRIVER_NAME "snd-card-azt2320" static int snd_card_azt2320_pnp(int dev, struct snd_card_azt2320 *acard, struct pnp_card_link *card, const struct pnp_card_device_id *id) { struct pnp_dev *pdev; int err; acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL); if (acard->dev == NULL) return -ENODEV; acard->devmpu = pnp_request_card_device(card, id->devs[1].id, NULL); pdev = acard->dev; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n"); return err; } port[dev] = pnp_port_start(pdev, 0); fm_port[dev] = pnp_port_start(pdev, 1); wss_port[dev] = pnp_port_start(pdev, 2); dma1[dev] = pnp_dma(pdev, 0); dma2[dev] = pnp_dma(pdev, 1); irq[dev] = pnp_irq(pdev, 0); pdev = acard->devmpu; if (pdev != NULL) { err = pnp_activate_dev(pdev); if (err < 0) goto __mpu_error; mpu_port[dev] = pnp_port_start(pdev, 0); mpu_irq[dev] = pnp_irq(pdev, 0); } else { __mpu_error: if (pdev) { pnp_release_card_device(pdev); snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n"); } acard->devmpu = NULL; mpu_port[dev] = -1; } return 0; } /* same of snd_sbdsp_command by Jaroslav Kysela */ static int snd_card_azt2320_command(unsigned long port, unsigned char val) { int i; unsigned long limit; limit = jiffies + HZ / 10; for (i = 50000; i && time_after(limit, jiffies); i--) if (!(inb(port + 0x0c) & 0x80)) { outb(val, port + 0x0c); return 0; } return -EBUSY; } static int snd_card_azt2320_enable_wss(unsigned long port) { int error; error = snd_card_azt2320_command(port, 0x09); if (error) return error; error = snd_card_azt2320_command(port, 0x00); if (error) return error; mdelay(5); return 0; } static int snd_card_azt2320_probe(int dev, struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { int error; struct snd_card *card; struct snd_card_azt2320 *acard; struct snd_wss *chip; struct snd_opl3 *opl3; error = snd_devm_card_new(&pcard->card->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_card_azt2320), &card); if (error < 0) return error; acard = card->private_data; error = snd_card_azt2320_pnp(dev, acard, pcard, pid); if (error) return error; error = snd_card_azt2320_enable_wss(port[dev]); if (error) return error; error = snd_wss_create(card, wss_port[dev], -1, irq[dev], dma1[dev], dma2[dev], WSS_HW_DETECT, 0, &chip); if (error < 0) return error; strcpy(card->driver, "AZT2320"); strcpy(card->shortname, "Aztech AZT2320"); sprintf(card->longname, "%s, WSS at 0x%lx, irq %i, dma %i&%i", card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]); error = snd_wss_pcm(chip, 0); if (error < 0) return error; error = snd_wss_mixer(chip); if (error < 0) return error; error = snd_wss_timer(chip, 0); if (error < 0) return error; if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) { if (snd_mpu401_uart_new(card, 0, MPU401_HW_AZT2320, mpu_port[dev], 0, mpu_irq[dev], NULL) < 0) snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]); } if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) { if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_AUTO, 0, &opl3) < 0) { snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n", fm_port[dev], fm_port[dev] + 2); } else { error = snd_opl3_timer_new(opl3, 1, 2); if (error < 0) return error; error = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (error < 0) return error; } } error = snd_card_register(card); if (error < 0) return error; pnp_set_card_drvdata(pcard, card); return 0; } static unsigned int azt2320_devices; static int snd_azt2320_pnp_detect(struct pnp_card_link *card, const struct pnp_card_device_id *id) { static int dev; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (!enable[dev]) continue; res = snd_card_azt2320_probe(dev, card, id); if (res < 0) return res; dev++; azt2320_devices++; return 0; } return -ENODEV; } #ifdef CONFIG_PM static int snd_azt2320_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { struct snd_card *card = pnp_get_card_drvdata(pcard); struct snd_card_azt2320 *acard = card->private_data; struct snd_wss *chip = acard->chip; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); chip->suspend(chip); return 0; } static int snd_azt2320_pnp_resume(struct pnp_card_link *pcard) { struct snd_card *card = pnp_get_card_drvdata(pcard); struct snd_card_azt2320 *acard = card->private_data; struct snd_wss *chip = acard->chip; chip->resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct pnp_card_driver azt2320_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "azt2320", .id_table = snd_azt2320_pnpids, .probe = snd_azt2320_pnp_detect, #ifdef CONFIG_PM .suspend = snd_azt2320_pnp_suspend, .resume = snd_azt2320_pnp_resume, #endif }; static int __init alsa_card_azt2320_init(void) { int err; err = pnp_register_card_driver(&azt2320_pnpc_driver); if (err) return err; if (!azt2320_devices) { pnp_unregister_card_driver(&azt2320_pnpc_driver); #ifdef MODULE snd_printk(KERN_ERR "no AZT2320 based soundcards found\n"); #endif return -ENODEV; } return 0; } static void __exit alsa_card_azt2320_exit(void) { pnp_unregister_card_driver(&azt2320_pnpc_driver); } module_init(alsa_card_azt2320_init) module_exit(alsa_card_azt2320_exit)
linux-master
sound/isa/azt2320.c
// SPDX-License-Identifier: GPL-2.0-or-later /* card-als100.c - driver for Avance Logic ALS100 based soundcards. Copyright (C) 1999-2000 by Massimo Piccioni <[email protected]> Copyright (C) 1999-2002 by Massimo Piccioni <[email protected]> Thanks to Pierfrancesco 'qM2' Passerini. Generalised for soundcards based on DT-0196 and ALS-007 chips by Jonathan Woithe <[email protected]>: June 2002. */ #include <linux/init.h> #include <linux/wait.h> #include <linux/time.h> #include <linux/pnp.h> #include <linux/module.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/mpu401.h> #include <sound/opl3.h> #include <sound/sb.h> #define PFX "als100: " MODULE_DESCRIPTION("Avance Logic ALS007/ALS1X0"); MODULE_AUTHOR("Massimo Piccioni <[email protected]>"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* PnP setup */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* PnP setup */ static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for Avance Logic based soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for Avance Logic based soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable Avance Logic based soundcard."); MODULE_ALIAS("snd-dt019x"); struct snd_card_als100 { struct pnp_dev *dev; struct pnp_dev *devmpu; struct pnp_dev *devopl; struct snd_sb *chip; }; static const struct pnp_card_device_id snd_als100_pnpids[] = { /* DT197A30 */ { .id = "RWB1688", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } }, .driver_data = SB_HW_DT019X }, /* DT0196 / ALS-007 */ { .id = "ALS0007", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } }, .driver_data = SB_HW_DT019X }, /* ALS100 - PRO16PNP */ { .id = "ALS0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } }, .driver_data = SB_HW_ALS100 }, /* ALS110 - MF1000 - Digimate 3D Sound */ { .id = "ALS0110", .devs = { { "@@@1001" }, { "@X@1001" }, { "@H@1001" } }, .driver_data = SB_HW_ALS100 }, /* ALS120 */ { .id = "ALS0120", .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } }, .driver_data = SB_HW_ALS100 }, /* ALS200 */ { .id = "ALS0200", .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0001" } }, .driver_data = SB_HW_ALS100 }, /* ALS200 OEM */ { .id = "ALS0200", .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0020" } }, .driver_data = SB_HW_ALS100 }, /* RTL3000 */ { .id = "RTL3000", .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } }, .driver_data = SB_HW_ALS100 }, { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, snd_als100_pnpids); static int snd_card_als100_pnp(int dev, struct snd_card_als100 *acard, struct pnp_card_link *card, const struct pnp_card_device_id *id) { struct pnp_dev *pdev; int err; acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL); if (acard->dev == NULL) return -ENODEV; acard->devmpu = pnp_request_card_device(card, id->devs[1].id, acard->dev); acard->devopl = pnp_request_card_device(card, id->devs[2].id, acard->dev); pdev = acard->dev; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n"); return err; } port[dev] = pnp_port_start(pdev, 0); if (id->driver_data == SB_HW_DT019X) dma8[dev] = pnp_dma(pdev, 0); else { dma8[dev] = pnp_dma(pdev, 1); dma16[dev] = pnp_dma(pdev, 0); } irq[dev] = pnp_irq(pdev, 0); pdev = acard->devmpu; if (pdev != NULL) { err = pnp_activate_dev(pdev); if (err < 0) goto __mpu_error; mpu_port[dev] = pnp_port_start(pdev, 0); mpu_irq[dev] = pnp_irq(pdev, 0); } else { __mpu_error: if (pdev) { pnp_release_card_device(pdev); snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n"); } acard->devmpu = NULL; mpu_port[dev] = -1; } pdev = acard->devopl; if (pdev != NULL) { err = pnp_activate_dev(pdev); if (err < 0) goto __fm_error; fm_port[dev] = pnp_port_start(pdev, 0); } else { __fm_error: if (pdev) { pnp_release_card_device(pdev); snd_printk(KERN_ERR PFX "OPL3 pnp configure failure, skipping\n"); } acard->devopl = NULL; fm_port[dev] = -1; } return 0; } static int snd_card_als100_probe(int dev, struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { int error; struct snd_sb *chip; struct snd_card *card; struct snd_card_als100 *acard; struct snd_opl3 *opl3; error = snd_devm_card_new(&pcard->card->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_card_als100), &card); if (error < 0) return error; acard = card->private_data; error = snd_card_als100_pnp(dev, acard, pcard, pid); if (error) return error; if (pid->driver_data == SB_HW_DT019X) dma16[dev] = -1; error = snd_sbdsp_create(card, port[dev], irq[dev], snd_sb16dsp_interrupt, dma8[dev], dma16[dev], pid->driver_data, &chip); if (error < 0) return error; acard->chip = chip; if (pid->driver_data == SB_HW_DT019X) { strcpy(card->driver, "DT-019X"); strcpy(card->shortname, "Diamond Tech. DT-019X"); snprintf(card->longname, sizeof(card->longname), "Diamond Tech. DT-019X, %s at 0x%lx, irq %d, dma %d", chip->name, chip->port, irq[dev], dma8[dev]); } else { strcpy(card->driver, "ALS100"); strcpy(card->shortname, "Avance Logic ALS100"); snprintf(card->longname, sizeof(card->longname), "Avance Logic ALS100, %s at 0x%lx, irq %d, dma %d&%d", chip->name, chip->port, irq[dev], dma8[dev], dma16[dev]); } error = snd_sb16dsp_pcm(chip, 0); if (error < 0) return error; error = snd_sbmixer_new(chip); if (error < 0) return error; if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) { int mpu_type = MPU401_HW_ALS100; if (mpu_irq[dev] == SNDRV_AUTO_IRQ) mpu_irq[dev] = -1; if (pid->driver_data == SB_HW_DT019X) mpu_type = MPU401_HW_MPU401; if (snd_mpu401_uart_new(card, 0, mpu_type, mpu_port[dev], 0, mpu_irq[dev], NULL) < 0) snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]); } if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) { if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_AUTO, 0, &opl3) < 0) { snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n", fm_port[dev], fm_port[dev] + 2); } else { error = snd_opl3_timer_new(opl3, 0, 1); if (error < 0) return error; error = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (error < 0) return error; } } error = snd_card_register(card); if (error < 0) return error; pnp_set_card_drvdata(pcard, card); return 0; } static unsigned int als100_devices; static int snd_als100_pnp_detect(struct pnp_card_link *card, const struct pnp_card_device_id *id) { static int dev; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (!enable[dev]) continue; res = snd_card_als100_probe(dev, card, id); if (res < 0) return res; dev++; als100_devices++; return 0; } return -ENODEV; } #ifdef CONFIG_PM static int snd_als100_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { struct snd_card *card = pnp_get_card_drvdata(pcard); struct snd_card_als100 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); snd_sbmixer_suspend(chip); return 0; } static int snd_als100_pnp_resume(struct pnp_card_link *pcard) { struct snd_card *card = pnp_get_card_drvdata(pcard); struct snd_card_als100 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_sbdsp_reset(chip); snd_sbmixer_resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct pnp_card_driver als100_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "als100", .id_table = snd_als100_pnpids, .probe = snd_als100_pnp_detect, #ifdef CONFIG_PM .suspend = snd_als100_pnp_suspend, .resume = snd_als100_pnp_resume, #endif }; static int __init alsa_card_als100_init(void) { int err; err = pnp_register_card_driver(&als100_pnpc_driver); if (err) return err; if (!als100_devices) { pnp_unregister_card_driver(&als100_pnpc_driver); #ifdef MODULE snd_printk(KERN_ERR "no Avance Logic based soundcards found\n"); #endif return -ENODEV; } return 0; } static void __exit alsa_card_als100_exit(void) { pnp_unregister_card_driver(&als100_pnpc_driver); } module_init(alsa_card_als100_init) module_exit(alsa_card_als100_exit)
linux-master
sound/isa/als100.c
// SPDX-License-Identifier: GPL-2.0-only /* * Driver for C-Media CMI8328-based soundcards, such as AudioExcel AV500 * Copyright (c) 2012 Ondrej Zary * * AudioExcel AV500 card consists of: * - CMI8328 - main chip (SB Pro emulation, gameport, OPL3, MPU401, CD-ROM) * - CS4231A - WSS codec * - Dream SAM9233+GMS950400+RAM+ROM: Wavetable MIDI, connected to MPU401 */ #include <linux/init.h> #include <linux/isa.h> #include <linux/module.h> #include <linux/gameport.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/opl3.h> #include <sound/mpu401.h> #define SNDRV_LEGACY_FIND_FREE_IOPORT #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> MODULE_AUTHOR("Ondrej Zary <[email protected]>"); MODULE_DESCRIPTION("C-Media CMI8328"); MODULE_LICENSE("GPL"); #if IS_ENABLED(CONFIG_GAMEPORT) #define SUPPORT_JOYSTICK 1 #endif /* I/O port is configured by jumpers on the card to one of these */ static const int cmi8328_ports[] = { 0x530, 0xe80, 0xf40, 0x604 }; #define CMI8328_MAX ARRAY_SIZE(cmi8328_ports) static int index[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = -1}; static char *id[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = NULL}; static long port[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT}; static int irq[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ}; static int dma1[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA}; static int dma2[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_DMA}; static long mpuport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_PORT}; static int mpuirq[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = SNDRV_AUTO_IRQ}; #ifdef SUPPORT_JOYSTICK static bool gameport[CMI8328_MAX] = {[0 ... (CMI8328_MAX-1)] = true}; #endif module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for CMI8328 soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for CMI8328 soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for CMI8328 driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for CMI8328 driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 for CMI8328 driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 for CMI8328 driver."); module_param_hw_array(mpuport, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8328 driver."); module_param_hw_array(mpuirq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8328 MPU-401 port."); #ifdef SUPPORT_JOYSTICK module_param_array(gameport, bool, NULL, 0444); MODULE_PARM_DESC(gameport, "Enable gameport."); #endif struct snd_cmi8328 { u16 port; u8 cfg[3]; u8 wss_cfg; struct snd_card *card; struct snd_wss *wss; #ifdef SUPPORT_JOYSTICK struct gameport *gameport; #endif }; /* CMI8328 configuration registers */ #define CFG1 0x61 #define CFG1_SB_DISABLE (1 << 0) #define CFG1_GAMEPORT (1 << 1) /* * bit 0: SB: 0=enabled, 1=disabled * bit 1: gameport: 0=disabled, 1=enabled * bits 2-4: SB IRQ: 001=3, 010=5, 011=7, 100=9, 101=10, 110=11 * bits 5-6: SB DMA: 00=disabled (when SB disabled), 01=DMA0, 10=DMA1, 11=DMA3 * bit 7: SB port: 0=0x220, 1=0x240 */ #define CFG2 0x62 #define CFG2_MPU_ENABLE (1 << 2) /* * bits 0-1: CD-ROM mode: 00=disabled, 01=Panasonic, 10=Sony/Mitsumi/Wearnes, 11=IDE * bit 2: MPU401: 0=disabled, 1=enabled * bits 3-4: MPU401 IRQ: 00=3, 01=5, 10=7, 11=9, * bits 5-7: MPU401 port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x332, 101=0x334, 110=0x336 */ #define CFG3 0x63 /* * bits 0-2: CD-ROM IRQ: 000=disabled, 001=3, 010=5, 011=7, 100=9, 101=10, 110=11 * bits 3-4: CD-ROM DMA: 00=disabled, 01=DMA0, 10=DMA1, 11=DMA3 * bits 5-7: CD-ROM port: 000=0x300, 001=0x310, 010=0x320, 011=0x330, 100=0x340, 101=0x350, 110=0x360, 111=0x370 */ static u8 snd_cmi8328_cfg_read(u16 port, u8 reg) { outb(0x43, port + 3); outb(0x21, port + 3); outb(reg, port + 3); return inb(port); } static void snd_cmi8328_cfg_write(u16 port, u8 reg, u8 val) { outb(0x43, port + 3); outb(0x21, port + 3); outb(reg, port + 3); outb(val, port + 3); /* yes, value goes to the same port as index */ } #ifdef CONFIG_PM static void snd_cmi8328_cfg_save(u16 port, u8 cfg[]) { cfg[0] = snd_cmi8328_cfg_read(port, CFG1); cfg[1] = snd_cmi8328_cfg_read(port, CFG2); cfg[2] = snd_cmi8328_cfg_read(port, CFG3); } static void snd_cmi8328_cfg_restore(u16 port, u8 cfg[]) { snd_cmi8328_cfg_write(port, CFG1, cfg[0]); snd_cmi8328_cfg_write(port, CFG2, cfg[1]); snd_cmi8328_cfg_write(port, CFG3, cfg[2]); } #endif /* CONFIG_PM */ static int snd_cmi8328_mixer(struct snd_wss *chip) { struct snd_card *card; struct snd_ctl_elem_id id1, id2; int err; card = chip->card; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; /* rename AUX0 switch to CD */ strcpy(id1.name, "Aux Playback Switch"); strcpy(id2.name, "CD Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "error renaming control\n"); return err; } /* rename AUX0 volume to CD */ strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "CD Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "error renaming control\n"); return err; } /* rename AUX1 switch to Synth */ strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; strcpy(id2.name, "Synth Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "error renaming control\n"); return err; } /* rename AUX1 volume to Synth */ strcpy(id1.name, "Aux Playback Volume"); id1.index = 1; strcpy(id2.name, "Synth Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "error renaming control\n"); return err; } return 0; } /* find index of an item in "-1"-ended array */ static int array_find(const int array[], int item) { int i; for (i = 0; array[i] != -1; i++) if (array[i] == item) return i; return -1; } /* the same for long */ static int array_find_l(const long array[], long item) { int i; for (i = 0; array[i] != -1; i++) if (array[i] == item) return i; return -1; } static int snd_cmi8328_probe(struct device *pdev, unsigned int ndev) { struct snd_card *card; struct snd_opl3 *opl3; struct snd_cmi8328 *cmi; #ifdef SUPPORT_JOYSTICK struct resource *res; #endif int err, pos; static const long mpu_ports[] = { 0x330, 0x300, 0x310, 0x320, 0x332, 0x334, 0x336, -1 }; static const u8 mpu_port_bits[] = { 3, 0, 1, 2, 4, 5, 6 }; static const int mpu_irqs[] = { 9, 7, 5, 3, -1 }; static const u8 mpu_irq_bits[] = { 3, 2, 1, 0 }; static const int irqs[] = { 9, 10, 11, 7, -1 }; static const u8 irq_bits[] = { 2, 3, 4, 1 }; static const int dma1s[] = { 3, 1, 0, -1 }; static const u8 dma_bits[] = { 3, 2, 1 }; static const int dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} }; u16 port = cmi8328_ports[ndev]; u8 val; /* 0xff is invalid configuration (but settable - hope it isn't set) */ if (snd_cmi8328_cfg_read(port, CFG1) == 0xff) return -ENODEV; /* the SB disable bit must NEVER EVER be cleared or the WSS dies */ snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE); if (snd_cmi8328_cfg_read(port, CFG1) != CFG1_SB_DISABLE) return -ENODEV; /* disable everything first */ snd_cmi8328_cfg_write(port, CFG2, 0); /* disable CDROM and MPU401 */ snd_cmi8328_cfg_write(port, CFG3, 0); /* disable CDROM IRQ and DMA */ if (irq[ndev] == SNDRV_AUTO_IRQ) { irq[ndev] = snd_legacy_find_free_irq(irqs); if (irq[ndev] < 0) { snd_printk(KERN_ERR "unable to find a free IRQ\n"); return -EBUSY; } } if (dma1[ndev] == SNDRV_AUTO_DMA) { dma1[ndev] = snd_legacy_find_free_dma(dma1s); if (dma1[ndev] < 0) { snd_printk(KERN_ERR "unable to find a free DMA1\n"); return -EBUSY; } } if (dma2[ndev] == SNDRV_AUTO_DMA) { dma2[ndev] = snd_legacy_find_free_dma(dma2s[dma1[ndev] % 4]); if (dma2[ndev] < 0) { snd_printk(KERN_WARNING "unable to find a free DMA2, full-duplex will not work\n"); dma2[ndev] = -1; } } /* configure WSS IRQ... */ pos = array_find(irqs, irq[ndev]); if (pos < 0) { snd_printk(KERN_ERR "invalid IRQ %d\n", irq[ndev]); return -EINVAL; } val = irq_bits[pos] << 3; /* ...and DMA... */ pos = array_find(dma1s, dma1[ndev]); if (pos < 0) { snd_printk(KERN_ERR "invalid DMA1 %d\n", dma1[ndev]); return -EINVAL; } val |= dma_bits[pos]; /* ...and DMA2 */ if (dma2[ndev] >= 0 && dma1[ndev] != dma2[ndev]) { pos = array_find(dma2s[dma1[ndev]], dma2[ndev]); if (pos < 0) { snd_printk(KERN_ERR "invalid DMA2 %d\n", dma2[ndev]); return -EINVAL; } val |= 0x04; /* enable separate capture DMA */ } outb(val, port); err = snd_devm_card_new(pdev, index[ndev], id[ndev], THIS_MODULE, sizeof(struct snd_cmi8328), &card); if (err < 0) return err; cmi = card->private_data; cmi->card = card; cmi->port = port; cmi->wss_cfg = val; err = snd_wss_create(card, port + 4, -1, irq[ndev], dma1[ndev], dma2[ndev], WSS_HW_DETECT, 0, &cmi->wss); if (err < 0) return err; err = snd_wss_pcm(cmi->wss, 0); if (err < 0) return err; err = snd_wss_mixer(cmi->wss); if (err < 0) return err; err = snd_cmi8328_mixer(cmi->wss); if (err < 0) return err; if (snd_wss_timer(cmi->wss, 0) < 0) snd_printk(KERN_WARNING "error initializing WSS timer\n"); if (mpuport[ndev] == SNDRV_AUTO_PORT) { mpuport[ndev] = snd_legacy_find_free_ioport(mpu_ports, 2); if (mpuport[ndev] < 0) snd_printk(KERN_ERR "unable to find a free MPU401 port\n"); } if (mpuirq[ndev] == SNDRV_AUTO_IRQ) { mpuirq[ndev] = snd_legacy_find_free_irq(mpu_irqs); if (mpuirq[ndev] < 0) snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n"); } /* enable and configure MPU401 */ if (mpuport[ndev] > 0 && mpuirq[ndev] > 0) { val = CFG2_MPU_ENABLE; pos = array_find_l(mpu_ports, mpuport[ndev]); if (pos < 0) snd_printk(KERN_WARNING "invalid MPU401 port 0x%lx\n", mpuport[ndev]); else { val |= mpu_port_bits[pos] << 5; pos = array_find(mpu_irqs, mpuirq[ndev]); if (pos < 0) snd_printk(KERN_WARNING "invalid MPU401 IRQ %d\n", mpuirq[ndev]); else { val |= mpu_irq_bits[pos] << 3; snd_cmi8328_cfg_write(port, CFG2, val); if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpuport[ndev], 0, mpuirq[ndev], NULL) < 0) snd_printk(KERN_ERR "error initializing MPU401\n"); } } } /* OPL3 is hardwired to 0x388 and cannot be disabled */ if (snd_opl3_create(card, 0x388, 0x38a, OPL3_HW_AUTO, 0, &opl3) < 0) snd_printk(KERN_ERR "error initializing OPL3\n"); else if (snd_opl3_hwdep_new(opl3, 0, 1, NULL) < 0) snd_printk(KERN_WARNING "error initializing OPL3 hwdep\n"); strcpy(card->driver, "CMI8328"); strcpy(card->shortname, "C-Media CMI8328"); sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d,%d", card->shortname, cmi->wss->port, irq[ndev], dma1[ndev], (dma2[ndev] >= 0) ? dma2[ndev] : dma1[ndev]); dev_set_drvdata(pdev, card); err = snd_card_register(card); if (err < 0) return err; #ifdef SUPPORT_JOYSTICK if (!gameport[ndev]) return 0; /* gameport is hardwired to 0x200 */ res = devm_request_region(pdev, 0x200, 8, "CMI8328 gameport"); if (!res) snd_printk(KERN_WARNING "unable to allocate gameport I/O port\n"); else { struct gameport *gp = cmi->gameport = gameport_allocate_port(); if (cmi->gameport) { gameport_set_name(gp, "CMI8328 Gameport"); gameport_set_phys(gp, "%s/gameport0", dev_name(pdev)); gameport_set_dev_parent(gp, pdev); gp->io = 0x200; /* Enable gameport */ snd_cmi8328_cfg_write(port, CFG1, CFG1_SB_DISABLE | CFG1_GAMEPORT); gameport_register_port(gp); } } #endif return 0; } static void snd_cmi8328_remove(struct device *pdev, unsigned int dev) { struct snd_card *card = dev_get_drvdata(pdev); struct snd_cmi8328 *cmi = card->private_data; #ifdef SUPPORT_JOYSTICK if (cmi->gameport) gameport_unregister_port(cmi->gameport); #endif /* disable everything */ snd_cmi8328_cfg_write(cmi->port, CFG1, CFG1_SB_DISABLE); snd_cmi8328_cfg_write(cmi->port, CFG2, 0); snd_cmi8328_cfg_write(cmi->port, CFG3, 0); } #ifdef CONFIG_PM static int snd_cmi8328_suspend(struct device *pdev, unsigned int n, pm_message_t state) { struct snd_card *card = dev_get_drvdata(pdev); struct snd_cmi8328 *cmi; if (!card) /* ignore absent devices */ return 0; cmi = card->private_data; snd_cmi8328_cfg_save(cmi->port, cmi->cfg); snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); cmi->wss->suspend(cmi->wss); return 0; } static int snd_cmi8328_resume(struct device *pdev, unsigned int n) { struct snd_card *card = dev_get_drvdata(pdev); struct snd_cmi8328 *cmi; if (!card) /* ignore absent devices */ return 0; cmi = card->private_data; snd_cmi8328_cfg_restore(cmi->port, cmi->cfg); outb(cmi->wss_cfg, cmi->port); cmi->wss->resume(cmi->wss); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct isa_driver snd_cmi8328_driver = { .probe = snd_cmi8328_probe, .remove = snd_cmi8328_remove, #ifdef CONFIG_PM .suspend = snd_cmi8328_suspend, .resume = snd_cmi8328_resume, #endif .driver = { .name = "cmi8328" }, }; module_isa_driver(snd_cmi8328_driver, CMI8328_MAX);
linux-master
sound/isa/cmi8328.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for Yamaha OPL3-SA[2,3] soundcards * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/interrupt.h> #include <linux/pm.h> #include <linux/pnp.h> #include <linux/module.h> #include <linux/io.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/opl3.h> #include <sound/initval.h> #include <sound/tlv.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Yamaha OPL3SA2+"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; #endif static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0xf86,0x370,0x100 */ static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */ static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x530,0xe80,0xf40,0x604 */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x388 */ static long midi_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x330,0x300 */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 0,1,3,5,9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ static int opl3sa3_ymode[SNDRV_CARDS]; /* 0,1,2,3 */ /*SL Added*/ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for OPL3-SA soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for OPL3-SA soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable OPL3-SA soundcard."); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard."); #endif module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for OPL3-SA driver."); module_param_hw_array(sb_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(sb_port, "SB port # for OPL3-SA driver."); module_param_hw_array(wss_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(wss_port, "WSS port # for OPL3-SA driver."); module_param_hw_array(fm_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(fm_port, "FM port # for OPL3-SA driver."); module_param_hw_array(midi_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(midi_port, "MIDI port # for OPL3-SA driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for OPL3-SA driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for OPL3-SA driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for OPL3-SA driver."); module_param_array(opl3sa3_ymode, int, NULL, 0444); MODULE_PARM_DESC(opl3sa3_ymode, "Speaker size selection for 3D Enhancement mode: Desktop/Large Notebook/Small Notebook/HiFi."); #ifdef CONFIG_PNP static int isa_registered; static int pnp_registered; static int pnpc_registered; #endif /* control ports */ #define OPL3SA2_PM_CTRL 0x01 #define OPL3SA2_SYS_CTRL 0x02 #define OPL3SA2_IRQ_CONFIG 0x03 #define OPL3SA2_IRQ_STATUS 0x04 #define OPL3SA2_DMA_CONFIG 0x06 #define OPL3SA2_MASTER_LEFT 0x07 #define OPL3SA2_MASTER_RIGHT 0x08 #define OPL3SA2_MIC 0x09 #define OPL3SA2_MISC 0x0A /* opl3sa3 only */ #define OPL3SA3_DGTL_DOWN 0x12 #define OPL3SA3_ANLG_DOWN 0x13 #define OPL3SA3_WIDE 0x14 #define OPL3SA3_BASS 0x15 #define OPL3SA3_TREBLE 0x16 /* power management bits */ #define OPL3SA2_PM_ADOWN 0x20 #define OPL3SA2_PM_PSV 0x04 #define OPL3SA2_PM_PDN 0x02 #define OPL3SA2_PM_PDX 0x01 #define OPL3SA2_PM_D0 0x00 #define OPL3SA2_PM_D3 (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX) struct snd_opl3sa2 { int version; /* 2 or 3 */ unsigned long port; /* control port */ struct resource *res_port; /* control port resource */ int irq; int single_dma; spinlock_t reg_lock; struct snd_hwdep *synth; struct snd_rawmidi *rmidi; struct snd_wss *wss; unsigned char ctlregs[0x20]; int ymode; /* SL added */ struct snd_kcontrol *master_switch; struct snd_kcontrol *master_volume; }; #define PFX "opl3sa2: " #ifdef CONFIG_PNP static const struct pnp_device_id snd_opl3sa2_pnpbiosids[] = { { .id = "YMH0021" }, { .id = "NMX2210" }, /* Gateway Solo 2500 */ { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids); static const struct pnp_card_device_id snd_opl3sa2_pnpids[] = { /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */ { .id = "YMH0020", .devs = { { "YMH0021" } } }, /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */ { .id = "YMH0030", .devs = { { "YMH0021" } } }, /* Yamaha OPL3-SA2 */ { .id = "YMH0800", .devs = { { "YMH0021" } } }, /* Yamaha OPL3-SA2 */ { .id = "YMH0801", .devs = { { "YMH0021" } } }, /* NeoMagic MagicWave 3DX */ { .id = "NMX2200", .devs = { { "YMH2210" } } }, /* NeoMagic MagicWave 3D */ { .id = "NMX2200", .devs = { { "NMX2210" } } }, /* --- */ { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids); #endif /* CONFIG_PNP */ /* read control port (w/o spinlock) */ static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg) { unsigned char result; #if 0 outb(0x1d, port); /* password */ printk(KERN_DEBUG "read [0x%lx] = 0x%x\n", port, inb(port)); #endif outb(reg, chip->port); /* register */ result = inb(chip->port + 1); #if 0 printk(KERN_DEBUG "read [0x%lx] = 0x%x [0x%x]\n", port, result, inb(port)); #endif return result; } /* read control port (with spinlock) */ static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg) { unsigned long flags; unsigned char result; spin_lock_irqsave(&chip->reg_lock, flags); result = __snd_opl3sa2_read(chip, reg); spin_unlock_irqrestore(&chip->reg_lock, flags); return result; } /* write control port (w/o spinlock) */ static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value) { #if 0 outb(0x1d, port); /* password */ #endif outb(reg, chip->port); /* register */ outb(value, chip->port + 1); chip->ctlregs[reg] = value; } /* write control port (with spinlock) */ static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value) { unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); __snd_opl3sa2_write(chip, reg, value); spin_unlock_irqrestore(&chip->reg_lock, flags); } static int snd_opl3sa2_detect(struct snd_card *card) { struct snd_opl3sa2 *chip = card->private_data; unsigned long port; unsigned char tmp, tmp1; char str[2]; port = chip->port; chip->res_port = devm_request_region(card->dev, port, 2, "OPL3-SA control"); if (!chip->res_port) { snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port); return -EBUSY; } /* snd_printk(KERN_DEBUG "REG 0A = 0x%x\n", snd_opl3sa2_read(chip, 0x0a)); */ chip->version = 0; tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC); if (tmp == 0xff) { snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp); return -ENODEV; } switch (tmp & 0x07) { case 0x01: chip->version = 2; /* YMF711 */ break; default: chip->version = 3; /* 0x02 - standard */ /* 0x03 - YM715B */ /* 0x04 - YM719 - OPL-SA4? */ /* 0x05 - OPL3-SA3 - Libretto 100 */ /* 0x07 - unknown - Neomagic MagicWave 3D */ break; } str[0] = chip->version + '0'; str[1] = 0; strcat(card->shortname, str); snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7); tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC); if (tmp1 != tmp) { snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1); return -ENODEV; } /* try if the MIC register is accessible */ tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC); snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a); tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC); if ((tmp1 & 0x9f) != 0x8a) { snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1); return -ENODEV; } snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f); /* initialization */ /* Power Management - full on */ snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0); if (chip->version > 2) { /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */ snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4)); } else { /* default for opl3sa2 versions */ snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00); } snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d); /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */ if (chip->single_dma) { snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03); /* DMA Configuration - DMA A = WSS-R + WSS-P */ } else { snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21); /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */ } snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7)); /* Miscellaneous - default */ if (chip->version > 2) { snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00); /* Digital Block Partial Power Down - default */ snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00); /* Analog Block Partial Power Down - default */ } return 0; } static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id) { unsigned short status; struct snd_card *card = dev_id; struct snd_opl3sa2 *chip; int handled = 0; if (card == NULL) return IRQ_NONE; chip = card->private_data; status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS); if (status & 0x20) { handled = 1; snd_opl3_interrupt(chip->synth); } if ((status & 0x10) && chip->rmidi != NULL) { handled = 1; snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data); } if (status & 0x07) { /* TI,CI,PI */ handled = 1; snd_wss_interrupt(irq, chip->wss); } if (status & 0x40) { /* hardware volume change */ handled = 1; /* reading from Master Lch register at 0x07 clears this bit */ snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT); snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT); if (chip->master_switch && chip->master_volume) { snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id); snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id); } } return IRQ_RETVAL(handled); } #define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_wss_info_single, \ .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) } #define OPL3SA2_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .name = xname, .index = xindex, \ .info = snd_wss_info_single, \ .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \ .tlv = { .p = (xtlv) } } static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned short val, oval; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; val <<= shift; spin_lock_irqsave(&chip->reg_lock, flags); oval = chip->ctlregs[reg]; val = (oval & ~(mask << shift)) | val; change = val != oval; __snd_opl3sa2_write(chip, reg, val); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_wss_info_double, \ .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) } #define OPL3SA2_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .name = xname, .index = xindex, \ .info = snd_wss_info_double, \ .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \ .tlv = { .p = (xtlv) } } static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask; ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change; unsigned short val1, val2, oval1, oval2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; spin_lock_irqsave(&chip->reg_lock, flags); if (left_reg != right_reg) { oval1 = chip->ctlregs[left_reg]; oval2 = chip->ctlregs[right_reg]; val1 = (oval1 & ~(mask << shift_left)) | val1; val2 = (oval2 & ~(mask << shift_right)) | val2; change = val1 != oval1 || val2 != oval2; __snd_opl3sa2_write(chip, left_reg, val1); __snd_opl3sa2_write(chip, right_reg, val2); } else { oval1 = chip->ctlregs[left_reg]; val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; change = val1 != oval1; __snd_opl3sa2_write(chip, left_reg, val1); } spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } static const DECLARE_TLV_DB_SCALE(db_scale_master, -3000, 200, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0); static const struct snd_kcontrol_new snd_opl3sa2_controls[] = { OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1), OPL3SA2_DOUBLE_TLV("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1, db_scale_master), OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1), OPL3SA2_SINGLE_TLV("Mic Playback Volume", 0, 0x09, 0, 31, 1, db_scale_5bit_12db_max), OPL3SA2_SINGLE("ZV Port Switch", 0, 0x02, 0, 1, 0), }; static const struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = { OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0), OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0), OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0) }; static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol) { struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol); chip->master_switch = NULL; chip->master_volume = NULL; } static int snd_opl3sa2_mixer(struct snd_card *card) { struct snd_opl3sa2 *chip = card->private_data; struct snd_ctl_elem_id id1, id2; struct snd_kcontrol *kctl; unsigned int idx; int err; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; /* reassign AUX0 to CD */ strcpy(id1.name, "Aux Playback Switch"); strcpy(id2.name, "CD Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n"); return err; } strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "CD Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n"); return err; } /* reassign AUX1 to FM */ strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; strcpy(id2.name, "FM Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n"); return err; } strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "FM Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n"); return err; } /* add OPL3SA2 controls */ for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) { kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip); err = snd_ctl_add(card, kctl); if (err < 0) return err; switch (idx) { case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break; case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break; } } if (chip->version > 2) { for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip)); if (err < 0) return err; } } return 0; } /* Power Management support functions */ #ifdef CONFIG_PM static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state) { if (card) { struct snd_opl3sa2 *chip = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); chip->wss->suspend(chip->wss); /* power down */ snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3); } return 0; } static int snd_opl3sa2_resume(struct snd_card *card) { struct snd_opl3sa2 *chip; int i; if (!card) return 0; chip = card->private_data; /* power up */ snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0); /* restore registers */ for (i = 2; i <= 0x0a; i++) { if (i != OPL3SA2_IRQ_STATUS) snd_opl3sa2_write(chip, i, chip->ctlregs[i]); } if (chip->version > 2) { for (i = 0x12; i <= 0x16; i++) snd_opl3sa2_write(chip, i, chip->ctlregs[i]); } /* restore wss */ chip->wss->resume(chip->wss); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif /* CONFIG_PM */ #ifdef CONFIG_PNP static int snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip, struct pnp_dev *pdev) { if (pnp_activate_dev(pdev) < 0) { snd_printk(KERN_ERR "PnP configure failure (out of resources?)\n"); return -EBUSY; } sb_port[dev] = pnp_port_start(pdev, 0); wss_port[dev] = pnp_port_start(pdev, 1); fm_port[dev] = pnp_port_start(pdev, 2); midi_port[dev] = pnp_port_start(pdev, 3); port[dev] = pnp_port_start(pdev, 4); dma1[dev] = pnp_dma(pdev, 0); dma2[dev] = pnp_dma(pdev, 1); irq[dev] = pnp_irq(pdev, 0); snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n", pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]); snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n", pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]); return 0; } #endif /* CONFIG_PNP */ static int snd_opl3sa2_card_new(struct device *pdev, int dev, struct snd_card **cardp) { struct snd_card *card; struct snd_opl3sa2 *chip; int err; err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_opl3sa2), &card); if (err < 0) return err; strcpy(card->driver, "OPL3SA2"); strcpy(card->shortname, "Yamaha OPL3-SA"); chip = card->private_data; spin_lock_init(&chip->reg_lock); chip->irq = -1; *cardp = card; return 0; } static int snd_opl3sa2_probe(struct snd_card *card, int dev) { int xirq, xdma1, xdma2; struct snd_opl3sa2 *chip; struct snd_wss *wss; struct snd_opl3 *opl3; int err; /* initialise this card from supplied (or default) parameter*/ chip = card->private_data; chip->ymode = opl3sa3_ymode[dev] & 0x03 ; chip->port = port[dev]; xirq = irq[dev]; xdma1 = dma1[dev]; xdma2 = dma2[dev]; if (xdma2 < 0) chip->single_dma = 1; err = snd_opl3sa2_detect(card); if (err < 0) return err; err = devm_request_irq(card->dev, xirq, snd_opl3sa2_interrupt, 0, "OPL3-SA2", card); if (err) { snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq); return -ENODEV; } chip->irq = xirq; card->sync_irq = chip->irq; err = snd_wss_create(card, wss_port[dev] + 4, -1, xirq, xdma1, xdma2, WSS_HW_OPL3SA2, WSS_HWSHARE_IRQ, &wss); if (err < 0) { snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4); return err; } chip->wss = wss; err = snd_wss_pcm(wss, 0); if (err < 0) return err; err = snd_wss_mixer(wss); if (err < 0) return err; err = snd_opl3sa2_mixer(card); if (err < 0) return err; err = snd_wss_timer(wss, 0); if (err < 0) return err; if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) { err = snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_OPL3, 0, &opl3); if (err < 0) return err; err = snd_opl3_timer_new(opl3, 1, 2); if (err < 0) return err; err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth); if (err < 0) return err; } if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) { err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2, midi_port[dev], MPU401_INFO_IRQ_HOOK, -1, &chip->rmidi); if (err < 0) return err; } sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d", card->shortname, chip->port, xirq, xdma1); if (xdma2 >= 0) sprintf(card->longname + strlen(card->longname), "&%d", xdma2); return snd_card_register(card); } #ifdef CONFIG_PNP static int snd_opl3sa2_pnp_detect(struct pnp_dev *pdev, const struct pnp_device_id *id) { static int dev; int err; struct snd_card *card; if (pnp_device_is_isapnp(pdev)) return -ENOENT; /* we have another procedure - card */ for (; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; err = snd_opl3sa2_card_new(&pdev->dev, dev, &card); if (err < 0) return err; err = snd_opl3sa2_pnp(dev, card->private_data, pdev); if (err < 0) return err; err = snd_opl3sa2_probe(card, dev); if (err < 0) return err; pnp_set_drvdata(pdev, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state) { return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state); } static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev) { return snd_opl3sa2_resume(pnp_get_drvdata(pdev)); } #endif static struct pnp_driver opl3sa2_pnp_driver = { .name = "snd-opl3sa2-pnpbios", .id_table = snd_opl3sa2_pnpbiosids, .probe = snd_opl3sa2_pnp_detect, #ifdef CONFIG_PM .suspend = snd_opl3sa2_pnp_suspend, .resume = snd_opl3sa2_pnp_resume, #endif }; static int snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard, const struct pnp_card_device_id *id) { static int dev; struct pnp_dev *pdev; int err; struct snd_card *card; pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL); if (pdev == NULL) { snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n", id->devs[0].id); return -EBUSY; } for (; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; err = snd_opl3sa2_card_new(&pdev->dev, dev, &card); if (err < 0) return err; err = snd_opl3sa2_pnp(dev, card->private_data, pdev); if (err < 0) return err; err = snd_opl3sa2_probe(card, dev); if (err < 0) return err; pnp_set_card_drvdata(pcard, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state) { return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state); } static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard) { return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard)); } #endif static struct pnp_card_driver opl3sa2_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "snd-opl3sa2-cpnp", .id_table = snd_opl3sa2_pnpids, .probe = snd_opl3sa2_pnp_cdetect, #ifdef CONFIG_PM .suspend = snd_opl3sa2_pnp_csuspend, .resume = snd_opl3sa2_pnp_cresume, #endif }; #endif /* CONFIG_PNP */ static int snd_opl3sa2_isa_match(struct device *pdev, unsigned int dev) { if (!enable[dev]) return 0; #ifdef CONFIG_PNP if (isapnp[dev]) return 0; #endif if (port[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR PFX "specify port\n"); return 0; } if (wss_port[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR PFX "specify wss_port\n"); return 0; } if (fm_port[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR PFX "specify fm_port\n"); return 0; } if (midi_port[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR PFX "specify midi_port\n"); return 0; } return 1; } static int snd_opl3sa2_isa_probe(struct device *pdev, unsigned int dev) { struct snd_card *card; int err; err = snd_opl3sa2_card_new(pdev, dev, &card); if (err < 0) return err; err = snd_opl3sa2_probe(card, dev); if (err < 0) return err; dev_set_drvdata(pdev, card); return 0; } #ifdef CONFIG_PM static int snd_opl3sa2_isa_suspend(struct device *dev, unsigned int n, pm_message_t state) { return snd_opl3sa2_suspend(dev_get_drvdata(dev), state); } static int snd_opl3sa2_isa_resume(struct device *dev, unsigned int n) { return snd_opl3sa2_resume(dev_get_drvdata(dev)); } #endif #define DEV_NAME "opl3sa2" static struct isa_driver snd_opl3sa2_isa_driver = { .match = snd_opl3sa2_isa_match, .probe = snd_opl3sa2_isa_probe, #ifdef CONFIG_PM .suspend = snd_opl3sa2_isa_suspend, .resume = snd_opl3sa2_isa_resume, #endif .driver = { .name = DEV_NAME }, }; static int __init alsa_card_opl3sa2_init(void) { int err; err = isa_register_driver(&snd_opl3sa2_isa_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_driver(&opl3sa2_pnp_driver); if (!err) pnp_registered = 1; err = pnp_register_card_driver(&opl3sa2_pnpc_driver); if (!err) pnpc_registered = 1; if (isa_registered || pnp_registered) err = 0; #endif return err; } static void __exit alsa_card_opl3sa2_exit(void) { #ifdef CONFIG_PNP if (pnpc_registered) pnp_unregister_card_driver(&opl3sa2_pnpc_driver); if (pnp_registered) pnp_unregister_driver(&opl3sa2_pnp_driver); if (isa_registered) #endif isa_unregister_driver(&snd_opl3sa2_isa_driver); } module_init(alsa_card_opl3sa2_init) module_exit(alsa_card_opl3sa2_exit)
linux-master
sound/isa/opl3sa2.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for C-Media's CMI8330 and CMI8329 soundcards. * Copyright (c) by George Talusan <[email protected]> * http://www.undergrad.math.uwaterloo.ca/~gstalusa */ /* * NOTES * * The extended registers contain mixer settings which are largely * untapped for the time being. * * MPU401 and SPDIF are not supported yet. I don't have the hardware * to aid in coding and testing, so I won't bother. * * To quickly load the module, * * modprobe -a snd-cmi8330 sbport=0x220 sbirq=5 sbdma8=1 * sbdma16=5 wssport=0x530 wssirq=11 wssdma=0 fmport=0x388 * * This card has two mixers and two PCM devices. I've cheesed it such * that recording and playback can be done through the same device. * The driver "magically" routes the capturing to the AD1848 codec, * and playback to the SB16 codec. This allows for full-duplex mode * to some extent. * The utilities in alsa-utils are aware of both devices, so passing * the appropriate parameters to amixer and alsactl will give you * full control over both mixers. */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/pnp.h> #include <linux/module.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/opl3.h> #include <sound/mpu401.h> #include <sound/sb.h> #include <sound/initval.h> /* */ /* #define ENABLE_SB_MIXER */ #define PLAYBACK_ON_SB /* */ MODULE_AUTHOR("George Talusan <[email protected]>"); MODULE_DESCRIPTION("C-Media CMI8330/CMI8329"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; #endif static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static long fmport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static long mpuport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int mpuirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for CMI8330/CMI8329 soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for CMI8330/CMI8329 soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable CMI8330/CMI8329 soundcard."); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard."); #endif module_param_hw_array(sbport, long, ioport, NULL, 0444); MODULE_PARM_DESC(sbport, "Port # for CMI8330/CMI8329 SB driver."); module_param_hw_array(sbirq, int, irq, NULL, 0444); MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330/CMI8329 SB driver."); module_param_hw_array(sbdma8, int, dma, NULL, 0444); MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330/CMI8329 SB driver."); module_param_hw_array(sbdma16, int, dma, NULL, 0444); MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330/CMI8329 SB driver."); module_param_hw_array(wssport, long, ioport, NULL, 0444); MODULE_PARM_DESC(wssport, "Port # for CMI8330/CMI8329 WSS driver."); module_param_hw_array(wssirq, int, irq, NULL, 0444); MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330/CMI8329 WSS driver."); module_param_hw_array(wssdma, int, dma, NULL, 0444); MODULE_PARM_DESC(wssdma, "DMA for CMI8330/CMI8329 WSS driver."); module_param_hw_array(fmport, long, ioport, NULL, 0444); MODULE_PARM_DESC(fmport, "FM port # for CMI8330/CMI8329 driver."); module_param_hw_array(mpuport, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpuport, "MPU-401 port # for CMI8330/CMI8329 driver."); module_param_hw_array(mpuirq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpuirq, "IRQ # for CMI8330/CMI8329 MPU-401 port."); #ifdef CONFIG_PNP static int isa_registered; static int pnp_registered; #endif #define CMI8330_RMUX3D 16 #define CMI8330_MUTEMUX 17 #define CMI8330_OUTPUTVOL 18 #define CMI8330_MASTVOL 19 #define CMI8330_LINVOL 20 #define CMI8330_CDINVOL 21 #define CMI8330_WAVVOL 22 #define CMI8330_RECMUX 23 #define CMI8330_WAVGAIN 24 #define CMI8330_LINGAIN 25 #define CMI8330_CDINGAIN 26 static const unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] = { 0x40, /* 16 - recording mux (SB-mixer-enabled) */ #ifdef ENABLE_SB_MIXER 0x40, /* 17 - mute mux (Mode2) */ #else 0x0, /* 17 - mute mux */ #endif 0x0, /* 18 - vol */ 0x0, /* 19 - master volume */ 0x0, /* 20 - line-in volume */ 0x0, /* 21 - cd-in volume */ 0x0, /* 22 - wave volume */ 0x0, /* 23 - mute/rec mux */ 0x0, /* 24 - wave rec gain */ 0x0, /* 25 - line-in rec gain */ 0x0 /* 26 - cd-in rec gain */ }; typedef int (*snd_pcm_open_callback_t)(struct snd_pcm_substream *); enum card_type { CMI8330, CMI8329 }; struct snd_cmi8330 { #ifdef CONFIG_PNP struct pnp_dev *cap; struct pnp_dev *play; struct pnp_dev *mpu; #endif struct snd_card *card; struct snd_wss *wss; struct snd_sb *sb; struct snd_pcm *pcm; struct snd_cmi8330_stream { struct snd_pcm_ops ops; snd_pcm_open_callback_t open; void *private_data; /* sb or wss */ } streams[2]; enum card_type type; }; #ifdef CONFIG_PNP static const struct pnp_card_device_id snd_cmi8330_pnpids[] = { { .id = "CMI0001", .devs = { { "@X@0001" }, { "@@@0001" }, { "@H@0001" }, { "A@@0001" } } }, { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } } }, { .id = "" } }; MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids); #endif static const struct snd_kcontrol_new snd_cmi8330_controls[] = { WSS_DOUBLE("Master Playback Volume", 0, CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0), WSS_SINGLE("Loud Playback Switch", 0, CMI8330_MUTEMUX, 6, 1, 1), WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1), WSS_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1), WSS_DOUBLE("Line Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0), WSS_DOUBLE("Line Playback Volume", 0, CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0), WSS_DOUBLE("Line Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0), WSS_DOUBLE("Line Capture Volume", 0, CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0), WSS_DOUBLE("CD Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0), WSS_DOUBLE("CD Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0), WSS_DOUBLE("CD Playback Volume", 0, CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0), WSS_DOUBLE("CD Capture Volume", 0, CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0), WSS_SINGLE("Mic Playback Switch", 0, CMI8330_MUTEMUX, 0, 1, 0), WSS_SINGLE("Mic Playback Volume", 0, CMI8330_OUTPUTVOL, 0, 7, 0), WSS_SINGLE("Mic Capture Switch", 0, CMI8330_RMUX3D, 0, 1, 0), WSS_SINGLE("Mic Capture Volume", 0, CMI8330_OUTPUTVOL, 5, 7, 0), WSS_DOUBLE("Wavetable Playback Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0), WSS_DOUBLE("Wavetable Playback Volume", 0, CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0), WSS_DOUBLE("Wavetable Capture Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0), WSS_DOUBLE("Wavetable Capture Volume", 0, CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0), WSS_SINGLE("3D Control - Switch", 0, CMI8330_RMUX3D, 5, 1, 1), WSS_SINGLE("Beep Playback Volume", 0, CMI8330_OUTPUTVOL, 3, 3, 0), WSS_DOUBLE("FM Playback Switch", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE("FM Playback Volume", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1), WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", CAPTURE, SWITCH), 0, CMI8330_RMUX3D, 7, 1, 1), WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", PLAYBACK, SWITCH), 0, CMI8330_MUTEMUX, 7, 1, 1), }; #ifdef ENABLE_SB_MIXER static const struct sbmix_elem cmi8330_sb_mixers[] = { SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31), SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15), SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15), SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31), SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31), SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1), SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31), SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1), SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31), SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1), SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31), SB_SINGLE("SB Beep Volume", SB_DSP4_SPEAKER_DEV, 6, 3), SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3), SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3), SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1), }; static const unsigned char cmi8330_sb_init_values[][2] = { { SB_DSP4_MASTER_DEV + 0, 0 }, { SB_DSP4_MASTER_DEV + 1, 0 }, { SB_DSP4_PCM_DEV + 0, 0 }, { SB_DSP4_PCM_DEV + 1, 0 }, { SB_DSP4_SYNTH_DEV + 0, 0 }, { SB_DSP4_SYNTH_DEV + 1, 0 }, { SB_DSP4_INPUT_LEFT, 0 }, { SB_DSP4_INPUT_RIGHT, 0 }, { SB_DSP4_OUTPUT_SW, 0 }, { SB_DSP4_SPEAKER_DEV, 0 }, }; static int cmi8330_add_sb_mixers(struct snd_sb *chip) { int idx, err; unsigned long flags; spin_lock_irqsave(&chip->mixer_lock, flags); snd_sbmixer_write(chip, 0x00, 0x00); /* mixer reset */ spin_unlock_irqrestore(&chip->mixer_lock, flags); /* mute and zero volume channels */ for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) { spin_lock_irqsave(&chip->mixer_lock, flags); snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0], cmi8330_sb_init_values[idx][1]); spin_unlock_irqrestore(&chip->mixer_lock, flags); } for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) { err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx]); if (err < 0) return err; } return 0; } #endif static int snd_cmi8330_mixer(struct snd_card *card, struct snd_cmi8330 *acard) { unsigned int idx; int err; strcpy(card->mixername, (acard->type == CMI8329) ? "CMI8329" : "CMI8330/C3D"); for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_cmi8330_controls[idx], acard->wss)); if (err < 0) return err; } #ifdef ENABLE_SB_MIXER err = cmi8330_add_sb_mixers(acard->sb); if (err < 0) return err; #endif return 0; } #ifdef CONFIG_PNP static int snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard, struct pnp_card_link *card, const struct pnp_card_device_id *id) { struct pnp_dev *pdev; int err; /* CMI8329 has a device with ID A@@0001, CMI8330 does not */ acard->type = (id->devs[3].id[0]) ? CMI8329 : CMI8330; acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL); if (acard->cap == NULL) return -EBUSY; acard->play = pnp_request_card_device(card, id->devs[1].id, NULL); if (acard->play == NULL) return -EBUSY; acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL); if (acard->mpu == NULL) return -EBUSY; pdev = acard->cap; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR "AD1848 PnP configure failure\n"); return -EBUSY; } wssport[dev] = pnp_port_start(pdev, 0); wssdma[dev] = pnp_dma(pdev, 0); wssirq[dev] = pnp_irq(pdev, 0); if (pnp_port_start(pdev, 1)) fmport[dev] = pnp_port_start(pdev, 1); /* allocate SB16 resources */ pdev = acard->play; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR "SB16 PnP configure failure\n"); return -EBUSY; } sbport[dev] = pnp_port_start(pdev, 0); sbdma8[dev] = pnp_dma(pdev, 0); sbdma16[dev] = pnp_dma(pdev, 1); sbirq[dev] = pnp_irq(pdev, 0); /* On CMI8239, the OPL3 port might be present in SB16 PnP resources */ if (fmport[dev] == SNDRV_AUTO_PORT) { if (pnp_port_start(pdev, 1)) fmport[dev] = pnp_port_start(pdev, 1); else fmport[dev] = 0x388; /* Or hardwired */ } /* allocate MPU-401 resources */ pdev = acard->mpu; err = pnp_activate_dev(pdev); if (err < 0) snd_printk(KERN_ERR "MPU-401 PnP configure failure: will be disabled\n"); else { mpuport[dev] = pnp_port_start(pdev, 0); mpuirq[dev] = pnp_irq(pdev, 0); } return 0; } #endif /* * PCM interface * * since we call the different chip interfaces for playback and capture * directions, we need a trick. * * - copy the ops for each direction into a local record. * - replace the open callback with the new one, which replaces the * substream->private_data with the corresponding chip instance * and calls again the original open callback of the chip. * */ #ifdef PLAYBACK_ON_SB #define CMI_SB_STREAM SNDRV_PCM_STREAM_PLAYBACK #define CMI_AD_STREAM SNDRV_PCM_STREAM_CAPTURE #else #define CMI_SB_STREAM SNDRV_PCM_STREAM_CAPTURE #define CMI_AD_STREAM SNDRV_PCM_STREAM_PLAYBACK #endif static int snd_cmi8330_playback_open(struct snd_pcm_substream *substream) { struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream); /* replace the private_data and call the original open callback */ substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data; return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream); } static int snd_cmi8330_capture_open(struct snd_pcm_substream *substream) { struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream); /* replace the private_data and call the original open callback */ substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data; return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream); } static int snd_cmi8330_pcm(struct snd_card *card, struct snd_cmi8330 *chip) { struct snd_pcm *pcm; const struct snd_pcm_ops *ops; int err; static const snd_pcm_open_callback_t cmi_open_callbacks[2] = { snd_cmi8330_playback_open, snd_cmi8330_capture_open }; err = snd_pcm_new(card, (chip->type == CMI8329) ? "CMI8329" : "CMI8330", 0, 1, 1, &pcm); if (err < 0) return err; strcpy(pcm->name, (chip->type == CMI8329) ? "CMI8329" : "CMI8330"); pcm->private_data = chip; /* SB16 */ ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM); chip->streams[CMI_SB_STREAM].ops = *ops; chip->streams[CMI_SB_STREAM].open = ops->open; chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM]; chip->streams[CMI_SB_STREAM].private_data = chip->sb; /* AD1848 */ ops = snd_wss_get_pcm_ops(CMI_AD_STREAM); chip->streams[CMI_AD_STREAM].ops = *ops; chip->streams[CMI_AD_STREAM].open = ops->open; chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM]; chip->streams[CMI_AD_STREAM].private_data = chip->wss; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops); snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, 128*1024); chip->pcm = pcm; return 0; } #ifdef CONFIG_PM static int snd_cmi8330_suspend(struct snd_card *card) { struct snd_cmi8330 *acard = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); acard->wss->suspend(acard->wss); snd_sbmixer_suspend(acard->sb); return 0; } static int snd_cmi8330_resume(struct snd_card *card) { struct snd_cmi8330 *acard = card->private_data; snd_sbdsp_reset(acard->sb); snd_sbmixer_suspend(acard->sb); acard->wss->resume(acard->wss); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif /* */ #ifdef CONFIG_PNP #define is_isapnp_selected(dev) isapnp[dev] #else #define is_isapnp_selected(dev) 0 #endif #define PFX "cmi8330: " static int snd_cmi8330_card_new(struct device *pdev, int dev, struct snd_card **cardp) { struct snd_card *card; struct snd_cmi8330 *acard; int err; err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_cmi8330), &card); if (err < 0) { snd_printk(KERN_ERR PFX "could not get a new card\n"); return err; } acard = card->private_data; acard->card = card; *cardp = card; return 0; } static int snd_cmi8330_probe(struct snd_card *card, int dev) { struct snd_cmi8330 *acard; int i, err; struct snd_opl3 *opl3; acard = card->private_data; err = snd_wss_create(card, wssport[dev] + 4, -1, wssirq[dev], wssdma[dev], -1, WSS_HW_DETECT, 0, &acard->wss); if (err < 0) { snd_printk(KERN_ERR PFX "AD1848 device busy??\n"); return err; } if (acard->wss->hardware != WSS_HW_CMI8330) { snd_printk(KERN_ERR PFX "AD1848 not found during probe\n"); return -ENODEV; } err = snd_sbdsp_create(card, sbport[dev], sbirq[dev], snd_sb16dsp_interrupt, sbdma8[dev], sbdma16[dev], SB_HW_AUTO, &acard->sb); if (err < 0) { snd_printk(KERN_ERR PFX "SB16 device busy??\n"); return err; } if (acard->sb->hardware != SB_HW_16) { snd_printk(KERN_ERR PFX "SB16 not found during probe\n"); return -ENODEV; } snd_wss_out(acard->wss, CS4231_MISC_INFO, 0x40); /* switch on MODE2 */ for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++) snd_wss_out(acard->wss, i, snd_cmi8330_image[i - CMI8330_RMUX3D]); err = snd_cmi8330_mixer(card, acard); if (err < 0) { snd_printk(KERN_ERR PFX "failed to create mixers\n"); return err; } err = snd_cmi8330_pcm(card, acard); if (err < 0) { snd_printk(KERN_ERR PFX "failed to create pcms\n"); return err; } if (fmport[dev] != SNDRV_AUTO_PORT) { if (snd_opl3_create(card, fmport[dev], fmport[dev] + 2, OPL3_HW_AUTO, 0, &opl3) < 0) { snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx ?\n", fmport[dev], fmport[dev] + 2); } else { err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } } if (mpuport[dev] != SNDRV_AUTO_PORT) { if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpuport[dev], 0, mpuirq[dev], NULL) < 0) printk(KERN_ERR PFX "no MPU-401 device at 0x%lx.\n", mpuport[dev]); } strcpy(card->driver, (acard->type == CMI8329) ? "CMI8329" : "CMI8330/C3D"); strcpy(card->shortname, (acard->type == CMI8329) ? "C-Media CMI8329" : "C-Media CMI8330/C3D"); sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d", card->shortname, acard->wss->port, wssirq[dev], wssdma[dev]); return snd_card_register(card); } static int snd_cmi8330_isa_match(struct device *pdev, unsigned int dev) { if (!enable[dev] || is_isapnp_selected(dev)) return 0; if (wssport[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR PFX "specify wssport\n"); return 0; } if (sbport[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR PFX "specify sbport\n"); return 0; } return 1; } static int snd_cmi8330_isa_probe(struct device *pdev, unsigned int dev) { struct snd_card *card; int err; err = snd_cmi8330_card_new(pdev, dev, &card); if (err < 0) return err; err = snd_cmi8330_probe(card, dev); if (err < 0) return err; dev_set_drvdata(pdev, card); return 0; } #ifdef CONFIG_PM static int snd_cmi8330_isa_suspend(struct device *dev, unsigned int n, pm_message_t state) { return snd_cmi8330_suspend(dev_get_drvdata(dev)); } static int snd_cmi8330_isa_resume(struct device *dev, unsigned int n) { return snd_cmi8330_resume(dev_get_drvdata(dev)); } #endif #define DEV_NAME "cmi8330" static struct isa_driver snd_cmi8330_driver = { .match = snd_cmi8330_isa_match, .probe = snd_cmi8330_isa_probe, #ifdef CONFIG_PM .suspend = snd_cmi8330_isa_suspend, .resume = snd_cmi8330_isa_resume, #endif .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_cmi8330_pnp_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int dev; struct snd_card *card; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; res = snd_cmi8330_card_new(&pcard->card->dev, dev, &card); if (res < 0) return res; res = snd_cmi8330_pnp(dev, card->private_data, pcard, pid); if (res < 0) { snd_printk(KERN_ERR PFX "PnP detection failed\n"); return res; } res = snd_cmi8330_probe(card, dev); if (res < 0) return res; pnp_set_card_drvdata(pcard, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_cmi8330_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { return snd_cmi8330_suspend(pnp_get_card_drvdata(pcard)); } static int snd_cmi8330_pnp_resume(struct pnp_card_link *pcard) { return snd_cmi8330_resume(pnp_get_card_drvdata(pcard)); } #endif static struct pnp_card_driver cmi8330_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "cmi8330", .id_table = snd_cmi8330_pnpids, .probe = snd_cmi8330_pnp_detect, #ifdef CONFIG_PM .suspend = snd_cmi8330_pnp_suspend, .resume = snd_cmi8330_pnp_resume, #endif }; #endif /* CONFIG_PNP */ static int __init alsa_card_cmi8330_init(void) { int err; err = isa_register_driver(&snd_cmi8330_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_card_driver(&cmi8330_pnpc_driver); if (!err) pnp_registered = 1; if (isa_registered) err = 0; #endif return err; } static void __exit alsa_card_cmi8330_exit(void) { #ifdef CONFIG_PNP if (pnp_registered) pnp_unregister_card_driver(&cmi8330_pnpc_driver); if (isa_registered) #endif isa_unregister_driver(&snd_cmi8330_driver); } module_init(alsa_card_cmi8330_init) module_exit(alsa_card_cmi8330_exit)
linux-master
sound/isa/cmi8330.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for Gallant SC-6000 soundcard. This card is also known as * Audio Excel DSP 16 or Zoltrix AV302. * These cards use CompuMedia ASC-9308 chip + AD1848 codec. * SC-6600 and SC-7000 cards are also supported. They are based on * CompuMedia ASC-9408 chip and CS4231 codec. * * Copyright (C) 2007 Krzysztof Helt <[email protected]> * * I don't have documentation for this card. I used the driver * for OSS/Free included in the kernel source as reference. */ #include <linux/module.h> #include <linux/delay.h> #include <linux/isa.h> #include <linux/io.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/opl3.h> #include <sound/mpu401.h> #include <sound/control.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> MODULE_AUTHOR("Krzysztof Helt"); MODULE_DESCRIPTION("Gallant SC-6000"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220, 0x240 */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5, 7, 9, 10, 11 */ static long mss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x530, 0xe80 */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x300, 0x310, 0x320, 0x330 */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5, 7, 9, 10, 0 */ static int dma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0, 1, 3 */ static bool joystick[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = false }; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for sc-6000 based soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for sc-6000 based soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable sc-6000 based soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for sc-6000 driver."); module_param_hw_array(mss_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mss_port, "MSS Port # for sc-6000 driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for sc-6000 driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for sc-6000 driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for sc-6000 driver."); module_param_hw_array(dma, int, dma, NULL, 0444); MODULE_PARM_DESC(dma, "DMA # for sc-6000 driver."); module_param_array(joystick, bool, NULL, 0444); MODULE_PARM_DESC(joystick, "Enable gameport."); /* * Commands of SC6000's DSP (SBPRO+special). * Some of them are COMMAND_xx, in the future they may change. */ #define WRITE_MDIRQ_CFG 0x50 /* Set M&I&DRQ mask (the real config) */ #define COMMAND_52 0x52 /* */ #define READ_HARD_CFG 0x58 /* Read Hardware Config (I/O base etc) */ #define COMMAND_5C 0x5c /* */ #define COMMAND_60 0x60 /* */ #define COMMAND_66 0x66 /* */ #define COMMAND_6C 0x6c /* */ #define COMMAND_6E 0x6e /* */ #define COMMAND_88 0x88 /* Unknown command */ #define DSP_INIT_MSS 0x8c /* Enable Microsoft Sound System mode */ #define COMMAND_C5 0xc5 /* */ #define GET_DSP_VERSION 0xe1 /* Get DSP Version */ #define GET_DSP_COPYRIGHT 0xe3 /* Get DSP Copyright */ /* * Offsets of SC6000 DSP I/O ports. The offset is added to base I/O port * to have the actual I/O port. * Register permissions are: * (wo) == Write Only * (ro) == Read Only * (w-) == Write * (r-) == Read */ #define DSP_RESET 0x06 /* offset of DSP RESET (wo) */ #define DSP_READ 0x0a /* offset of DSP READ (ro) */ #define DSP_WRITE 0x0c /* offset of DSP WRITE (w-) */ #define DSP_COMMAND 0x0c /* offset of DSP COMMAND (w-) */ #define DSP_STATUS 0x0c /* offset of DSP STATUS (r-) */ #define DSP_DATAVAIL 0x0e /* offset of DSP DATA AVAILABLE (ro) */ #define PFX "sc6000: " #define DRV_NAME "SC-6000" /* hardware dependent functions */ /* * sc6000_irq_to_softcfg - Decode irq number into cfg code. */ static unsigned char sc6000_irq_to_softcfg(int irq) { unsigned char val = 0; switch (irq) { case 5: val = 0x28; break; case 7: val = 0x8; break; case 9: val = 0x10; break; case 10: val = 0x18; break; case 11: val = 0x20; break; default: break; } return val; } /* * sc6000_dma_to_softcfg - Decode dma number into cfg code. */ static unsigned char sc6000_dma_to_softcfg(int dma) { unsigned char val = 0; switch (dma) { case 0: val = 1; break; case 1: val = 2; break; case 3: val = 3; break; default: break; } return val; } /* * sc6000_mpu_irq_to_softcfg - Decode MPU-401 irq number into cfg code. */ static unsigned char sc6000_mpu_irq_to_softcfg(int mpu_irq) { unsigned char val = 0; switch (mpu_irq) { case 5: val = 4; break; case 7: val = 0x44; break; case 9: val = 0x84; break; case 10: val = 0xc4; break; default: break; } return val; } static int sc6000_wait_data(char __iomem *vport) { int loop = 1000; unsigned char val = 0; do { val = ioread8(vport + DSP_DATAVAIL); if (val & 0x80) return 0; cpu_relax(); } while (loop--); return -EAGAIN; } static int sc6000_read(char __iomem *vport) { if (sc6000_wait_data(vport)) return -EBUSY; return ioread8(vport + DSP_READ); } static int sc6000_write(char __iomem *vport, int cmd) { unsigned char val; int loop = 500000; do { val = ioread8(vport + DSP_STATUS); /* * DSP ready to receive data if bit 7 of val == 0 */ if (!(val & 0x80)) { iowrite8(cmd, vport + DSP_COMMAND); return 0; } cpu_relax(); } while (loop--); snd_printk(KERN_ERR "DSP Command (0x%x) timeout.\n", cmd); return -EIO; } static int sc6000_dsp_get_answer(char __iomem *vport, int command, char *data, int data_len) { int len = 0; if (sc6000_write(vport, command)) { snd_printk(KERN_ERR "CMD 0x%x: failed!\n", command); return -EIO; } do { int val = sc6000_read(vport); if (val < 0) break; data[len++] = val; } while (len < data_len); /* * If no more data available, return to the caller, no error if len>0. * We have no other way to know when the string is finished. */ return len ? len : -EIO; } static int sc6000_dsp_reset(char __iomem *vport) { iowrite8(1, vport + DSP_RESET); udelay(10); iowrite8(0, vport + DSP_RESET); udelay(20); if (sc6000_read(vport) == 0xaa) return 0; return -ENODEV; } /* detection and initialization */ static int sc6000_hw_cfg_write(char __iomem *vport, const int *cfg) { if (sc6000_write(vport, COMMAND_6C) < 0) { snd_printk(KERN_WARNING "CMD 0x%x: failed!\n", COMMAND_6C); return -EIO; } if (sc6000_write(vport, COMMAND_5C) < 0) { snd_printk(KERN_ERR "CMD 0x%x: failed!\n", COMMAND_5C); return -EIO; } if (sc6000_write(vport, cfg[0]) < 0) { snd_printk(KERN_ERR "DATA 0x%x: failed!\n", cfg[0]); return -EIO; } if (sc6000_write(vport, cfg[1]) < 0) { snd_printk(KERN_ERR "DATA 0x%x: failed!\n", cfg[1]); return -EIO; } if (sc6000_write(vport, COMMAND_C5) < 0) { snd_printk(KERN_ERR "CMD 0x%x: failed!\n", COMMAND_C5); return -EIO; } return 0; } static int sc6000_cfg_write(char __iomem *vport, unsigned char softcfg) { if (sc6000_write(vport, WRITE_MDIRQ_CFG)) { snd_printk(KERN_ERR "CMD 0x%x: failed!\n", WRITE_MDIRQ_CFG); return -EIO; } if (sc6000_write(vport, softcfg)) { snd_printk(KERN_ERR "sc6000_cfg_write: failed!\n"); return -EIO; } return 0; } static int sc6000_setup_board(char __iomem *vport, int config) { int loop = 10; do { if (sc6000_write(vport, COMMAND_88)) { snd_printk(KERN_ERR "CMD 0x%x: failed!\n", COMMAND_88); return -EIO; } } while ((sc6000_wait_data(vport) < 0) && loop--); if (sc6000_read(vport) < 0) { snd_printk(KERN_ERR "sc6000_read after CMD 0x%x: failed\n", COMMAND_88); return -EIO; } if (sc6000_cfg_write(vport, config)) return -ENODEV; return 0; } static int sc6000_init_mss(char __iomem *vport, int config, char __iomem *vmss_port, int mss_config) { if (sc6000_write(vport, DSP_INIT_MSS)) { snd_printk(KERN_ERR "sc6000_init_mss [0x%x]: failed!\n", DSP_INIT_MSS); return -EIO; } msleep(10); if (sc6000_cfg_write(vport, config)) return -EIO; iowrite8(mss_config, vmss_port); return 0; } static void sc6000_hw_cfg_encode(char __iomem *vport, int *cfg, long xport, long xmpu, long xmss_port, int joystick) { cfg[0] = 0; cfg[1] = 0; if (xport == 0x240) cfg[0] |= 1; if (xmpu != SNDRV_AUTO_PORT) { cfg[0] |= (xmpu & 0x30) >> 2; cfg[1] |= 0x20; } if (xmss_port == 0xe80) cfg[0] |= 0x10; cfg[0] |= 0x40; /* always set */ if (!joystick) cfg[0] |= 0x02; cfg[1] |= 0x80; /* enable WSS system */ cfg[1] &= ~0x40; /* disable IDE */ snd_printd("hw cfg %x, %x\n", cfg[0], cfg[1]); } static int sc6000_init_board(char __iomem *vport, char __iomem *vmss_port, int dev) { char answer[15]; char version[2]; int mss_config = sc6000_irq_to_softcfg(irq[dev]) | sc6000_dma_to_softcfg(dma[dev]); int config = mss_config | sc6000_mpu_irq_to_softcfg(mpu_irq[dev]); int err; int old = 0; err = sc6000_dsp_reset(vport); if (err < 0) { snd_printk(KERN_ERR "sc6000_dsp_reset: failed!\n"); return err; } memset(answer, 0, sizeof(answer)); err = sc6000_dsp_get_answer(vport, GET_DSP_COPYRIGHT, answer, 15); if (err <= 0) { snd_printk(KERN_ERR "sc6000_dsp_copyright: failed!\n"); return -ENODEV; } /* * My SC-6000 card return "SC-6000" in DSPCopyright, so * if we have something different, we have to be warned. */ if (strncmp("SC-6000", answer, 7)) snd_printk(KERN_WARNING "Warning: non SC-6000 audio card!\n"); if (sc6000_dsp_get_answer(vport, GET_DSP_VERSION, version, 2) < 2) { snd_printk(KERN_ERR "sc6000_dsp_version: failed!\n"); return -ENODEV; } printk(KERN_INFO PFX "Detected model: %s, DSP version %d.%d\n", answer, version[0], version[1]); /* set configuration */ sc6000_write(vport, COMMAND_5C); if (sc6000_read(vport) < 0) old = 1; if (!old) { int cfg[2]; sc6000_hw_cfg_encode(vport, &cfg[0], port[dev], mpu_port[dev], mss_port[dev], joystick[dev]); if (sc6000_hw_cfg_write(vport, cfg) < 0) { snd_printk(KERN_ERR "sc6000_hw_cfg_write: failed!\n"); return -EIO; } } err = sc6000_setup_board(vport, config); if (err < 0) { snd_printk(KERN_ERR "sc6000_setup_board: failed!\n"); return -ENODEV; } sc6000_dsp_reset(vport); if (!old) { sc6000_write(vport, COMMAND_60); sc6000_write(vport, 0x02); sc6000_dsp_reset(vport); } err = sc6000_setup_board(vport, config); if (err < 0) { snd_printk(KERN_ERR "sc6000_setup_board: failed!\n"); return -ENODEV; } err = sc6000_init_mss(vport, config, vmss_port, mss_config); if (err < 0) { snd_printk(KERN_ERR "Cannot initialize " "Microsoft Sound System mode.\n"); return -ENODEV; } return 0; } static int snd_sc6000_mixer(struct snd_wss *chip) { struct snd_card *card = chip->card; struct snd_ctl_elem_id id1, id2; int err; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = SNDRV_CTL_ELEM_IFACE_MIXER; id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; /* reassign AUX0 to FM */ strcpy(id1.name, "Aux Playback Switch"); strcpy(id2.name, "FM Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "FM Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; /* reassign AUX1 to CD */ strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; strcpy(id2.name, "CD Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "CD Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; return 0; } static int snd_sc6000_match(struct device *devptr, unsigned int dev) { if (!enable[dev]) return 0; if (port[dev] == SNDRV_AUTO_PORT) { printk(KERN_ERR PFX "specify IO port\n"); return 0; } if (mss_port[dev] == SNDRV_AUTO_PORT) { printk(KERN_ERR PFX "specify MSS port\n"); return 0; } if (port[dev] != 0x220 && port[dev] != 0x240) { printk(KERN_ERR PFX "Port must be 0x220 or 0x240\n"); return 0; } if (mss_port[dev] != 0x530 && mss_port[dev] != 0xe80) { printk(KERN_ERR PFX "MSS port must be 0x530 or 0xe80\n"); return 0; } if (irq[dev] != SNDRV_AUTO_IRQ && !sc6000_irq_to_softcfg(irq[dev])) { printk(KERN_ERR PFX "invalid IRQ %d\n", irq[dev]); return 0; } if (dma[dev] != SNDRV_AUTO_DMA && !sc6000_dma_to_softcfg(dma[dev])) { printk(KERN_ERR PFX "invalid DMA %d\n", dma[dev]); return 0; } if (mpu_port[dev] != SNDRV_AUTO_PORT && (mpu_port[dev] & ~0x30L) != 0x300) { printk(KERN_ERR PFX "invalid MPU-401 port %lx\n", mpu_port[dev]); return 0; } if (mpu_port[dev] != SNDRV_AUTO_PORT && mpu_irq[dev] != SNDRV_AUTO_IRQ && mpu_irq[dev] != 0 && !sc6000_mpu_irq_to_softcfg(mpu_irq[dev])) { printk(KERN_ERR PFX "invalid MPU-401 IRQ %d\n", mpu_irq[dev]); return 0; } return 1; } static void snd_sc6000_free(struct snd_card *card) { char __iomem *vport = (char __force __iomem *)card->private_data; if (vport) sc6000_setup_board(vport, 0); } static int __snd_sc6000_probe(struct device *devptr, unsigned int dev) { static const int possible_irqs[] = { 5, 7, 9, 10, 11, -1 }; static const int possible_dmas[] = { 1, 3, 0, -1 }; int err; int xirq = irq[dev]; int xdma = dma[dev]; struct snd_card *card; struct snd_wss *chip; struct snd_opl3 *opl3; char __iomem *vport; char __iomem *vmss_port; err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE, 0, &card); if (err < 0) return err; if (xirq == SNDRV_AUTO_IRQ) { xirq = snd_legacy_find_free_irq(possible_irqs); if (xirq < 0) { snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); return -EBUSY; } } if (xdma == SNDRV_AUTO_DMA) { xdma = snd_legacy_find_free_dma(possible_dmas); if (xdma < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA\n"); return -EBUSY; } } if (!devm_request_region(devptr, port[dev], 0x10, DRV_NAME)) { snd_printk(KERN_ERR PFX "I/O port region is already in use.\n"); return -EBUSY; } vport = devm_ioport_map(devptr, port[dev], 0x10); if (!vport) { snd_printk(KERN_ERR PFX "I/O port cannot be iomapped.\n"); return -EBUSY; } card->private_data = (void __force *)vport; /* to make it marked as used */ if (!devm_request_region(devptr, mss_port[dev], 4, DRV_NAME)) { snd_printk(KERN_ERR PFX "SC-6000 port I/O port region is already in use.\n"); return -EBUSY; } vmss_port = devm_ioport_map(devptr, mss_port[dev], 4); if (!vmss_port) { snd_printk(KERN_ERR PFX "MSS port I/O cannot be iomapped.\n"); return -EBUSY; } snd_printd("Initializing BASE[0x%lx] IRQ[%d] DMA[%d] MIRQ[%d]\n", port[dev], xirq, xdma, mpu_irq[dev] == SNDRV_AUTO_IRQ ? 0 : mpu_irq[dev]); err = sc6000_init_board(vport, vmss_port, dev); if (err < 0) return err; card->private_free = snd_sc6000_free; err = snd_wss_create(card, mss_port[dev] + 4, -1, xirq, xdma, -1, WSS_HW_DETECT, 0, &chip); if (err < 0) return err; err = snd_wss_pcm(chip, 0); if (err < 0) { snd_printk(KERN_ERR PFX "error creating new WSS PCM device\n"); return err; } err = snd_wss_mixer(chip); if (err < 0) { snd_printk(KERN_ERR PFX "error creating new WSS mixer\n"); return err; } err = snd_sc6000_mixer(chip); if (err < 0) { snd_printk(KERN_ERR PFX "the mixer rewrite failed\n"); return err; } if (snd_opl3_create(card, 0x388, 0x388 + 2, OPL3_HW_AUTO, 0, &opl3) < 0) { snd_printk(KERN_ERR PFX "no OPL device at 0x%x-0x%x ?\n", 0x388, 0x388 + 2); } else { err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } if (mpu_port[dev] != SNDRV_AUTO_PORT) { if (mpu_irq[dev] == SNDRV_AUTO_IRQ) mpu_irq[dev] = -1; if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_port[dev], 0, mpu_irq[dev], NULL) < 0) snd_printk(KERN_ERR "no MPU-401 device at 0x%lx ?\n", mpu_port[dev]); } strcpy(card->driver, DRV_NAME); strcpy(card->shortname, "SC-6000"); sprintf(card->longname, "Gallant SC-6000 at 0x%lx, irq %d, dma %d", mss_port[dev], xirq, xdma); err = snd_card_register(card); if (err < 0) return err; dev_set_drvdata(devptr, card); return 0; } static int snd_sc6000_probe(struct device *devptr, unsigned int dev) { return snd_card_free_on_error(devptr, __snd_sc6000_probe(devptr, dev)); } static struct isa_driver snd_sc6000_driver = { .match = snd_sc6000_match, .probe = snd_sc6000_probe, /* FIXME: suspend/resume */ .driver = { .name = DRV_NAME, }, }; module_isa_driver(snd_sc6000_driver, SNDRV_CARDS);
linux-master
sound/isa/sc6000.c
// SPDX-License-Identifier: GPL-2.0-only /* * AdLib FM card driver. */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/isa.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/opl3.h> #define CRD_NAME "AdLib FM" #define DEV_NAME "adlib" MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Rene Herman"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); static int snd_adlib_match(struct device *dev, unsigned int n) { if (!enable[n]) return 0; if (port[n] == SNDRV_AUTO_PORT) { dev_err(dev, "please specify port\n"); return 0; } return 1; } static int snd_adlib_probe(struct device *dev, unsigned int n) { struct snd_card *card; struct snd_opl3 *opl3; int error; error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, 0, &card); if (error < 0) { dev_err(dev, "could not create card\n"); return error; } card->private_data = devm_request_region(dev, port[n], 4, CRD_NAME); if (!card->private_data) { dev_err(dev, "could not grab ports\n"); return -EBUSY; } strcpy(card->driver, DEV_NAME); strcpy(card->shortname, CRD_NAME); sprintf(card->longname, CRD_NAME " at %#lx", port[n]); error = snd_opl3_create(card, port[n], port[n] + 2, OPL3_HW_AUTO, 1, &opl3); if (error < 0) { dev_err(dev, "could not create OPL\n"); return error; } error = snd_opl3_hwdep_new(opl3, 0, 0, NULL); if (error < 0) { dev_err(dev, "could not create FM\n"); return error; } error = snd_card_register(card); if (error < 0) { dev_err(dev, "could not register card\n"); return error; } dev_set_drvdata(dev, card); return 0; } static struct isa_driver snd_adlib_driver = { .match = snd_adlib_match, .probe = snd_adlib_probe, .driver = { .name = DEV_NAME } }; module_isa_driver(snd_adlib_driver, SNDRV_CARDS);
linux-master
sound/isa/adlib.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * and (c) 1999 Steve Ratcliffe <[email protected]> * Copyright (C) 1999-2000 Takashi Iwai <[email protected]> * * Emu8000 synth plug-in routine */ #include "emu8000_local.h" #include <linux/init.h> #include <linux/module.h> #include <sound/initval.h> MODULE_AUTHOR("Takashi Iwai, Steve Ratcliffe"); MODULE_DESCRIPTION("Emu8000 synth plug-in routine"); MODULE_LICENSE("GPL"); /*----------------------------------------------------------------*/ /* * create a new hardware dependent device for Emu8000 */ static int snd_emu8000_probe(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); struct snd_emu8000 *hw; struct snd_emux *emu; hw = *(struct snd_emu8000**)SNDRV_SEQ_DEVICE_ARGPTR(dev); if (hw == NULL) return -EINVAL; if (hw->emu) return -EBUSY; /* already exists..? */ if (snd_emux_new(&emu) < 0) return -ENOMEM; hw->emu = emu; snd_emu8000_ops_setup(hw); emu->hw = hw; emu->max_voices = EMU8000_DRAM_VOICES; emu->num_ports = hw->seq_ports; if (hw->memhdr) { snd_printk(KERN_ERR "memhdr is already initialized!?\n"); snd_util_memhdr_free(hw->memhdr); } hw->memhdr = snd_util_memhdr_new(hw->mem_size); if (hw->memhdr == NULL) { snd_emux_free(emu); hw->emu = NULL; return -ENOMEM; } emu->memhdr = hw->memhdr; emu->midi_ports = hw->seq_ports < 2 ? hw->seq_ports : 2; /* number of virmidi ports */ emu->midi_devidx = 1; emu->linear_panning = 1; emu->hwdep_idx = 2; /* FIXED */ if (snd_emux_register(emu, dev->card, hw->index, "Emu8000") < 0) { snd_emux_free(emu); snd_util_memhdr_free(hw->memhdr); hw->emu = NULL; hw->memhdr = NULL; return -ENOMEM; } if (hw->mem_size > 0) snd_emu8000_pcm_new(dev->card, hw, 1); dev->driver_data = hw; return 0; } /* * free all resources */ static int snd_emu8000_remove(struct device *_dev) { struct snd_seq_device *dev = to_seq_dev(_dev); struct snd_emu8000 *hw; if (dev->driver_data == NULL) return 0; /* no synth was allocated actually */ hw = dev->driver_data; if (hw->pcm) snd_device_free(dev->card, hw->pcm); snd_emux_free(hw->emu); snd_util_memhdr_free(hw->memhdr); hw->emu = NULL; hw->memhdr = NULL; return 0; } /* * INIT part */ static struct snd_seq_driver emu8000_driver = { .driver = { .name = KBUILD_MODNAME, .probe = snd_emu8000_probe, .remove = snd_emu8000_remove, }, .id = SNDRV_SEQ_DEV_ID_EMU8000, .argsize = sizeof(struct snd_emu8000 *), }; module_snd_seq_driver(emu8000_driver);
linux-master
sound/isa/sb/emu8000_synth.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of SoundBlaster cards - MIDI interface * * -- * * Sun May 9 22:54:38 BST 1999 George David Morrison <[email protected]> * Fixed typo in snd_sb8dsp_midi_new_device which prevented midi from * working. * * Sun May 11 12:34:56 UTC 2003 Clemens Ladisch <[email protected]> * Added full duplex UART mode for DSP version 2.0 and later. */ #include <linux/io.h> #include <linux/time.h> #include <sound/core.h> #include <sound/sb.h> irqreturn_t snd_sb8dsp_midi_interrupt(struct snd_sb *chip) { struct snd_rawmidi *rmidi; int max = 64; char byte; if (!chip) return IRQ_NONE; rmidi = chip->rmidi; if (!rmidi) { inb(SBP(chip, DATA_AVAIL)); /* ack interrupt */ return IRQ_NONE; } spin_lock(&chip->midi_input_lock); while (max-- > 0) { if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { byte = inb(SBP(chip, READ)); if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) { snd_rawmidi_receive(chip->midi_substream_input, &byte, 1); } } } spin_unlock(&chip->midi_input_lock); return IRQ_HANDLED; } static int snd_sb8dsp_midi_input_open(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_sb *chip; unsigned int valid_open_flags; chip = substream->rmidi->private_data; valid_open_flags = chip->hardware >= SB_HW_20 ? SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER : 0; spin_lock_irqsave(&chip->open_lock, flags); if (chip->open & ~valid_open_flags) { spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; } chip->open |= SB_OPEN_MIDI_INPUT; chip->midi_substream_input = substream; if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) { spin_unlock_irqrestore(&chip->open_lock, flags); snd_sbdsp_reset(chip); /* reset DSP */ if (chip->hardware >= SB_HW_20) snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ); } else { spin_unlock_irqrestore(&chip->open_lock, flags); } return 0; } static int snd_sb8dsp_midi_output_open(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_sb *chip; unsigned int valid_open_flags; chip = substream->rmidi->private_data; valid_open_flags = chip->hardware >= SB_HW_20 ? SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER : 0; spin_lock_irqsave(&chip->open_lock, flags); if (chip->open & ~valid_open_flags) { spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; } chip->open |= SB_OPEN_MIDI_OUTPUT; chip->midi_substream_output = substream; if (!(chip->open & SB_OPEN_MIDI_INPUT)) { spin_unlock_irqrestore(&chip->open_lock, flags); snd_sbdsp_reset(chip); /* reset DSP */ if (chip->hardware >= SB_HW_20) snd_sbdsp_command(chip, SB_DSP_MIDI_UART_IRQ); } else { spin_unlock_irqrestore(&chip->open_lock, flags); } return 0; } static int snd_sb8dsp_midi_input_close(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_sb *chip; chip = substream->rmidi->private_data; spin_lock_irqsave(&chip->open_lock, flags); chip->open &= ~(SB_OPEN_MIDI_INPUT | SB_OPEN_MIDI_INPUT_TRIGGER); chip->midi_substream_input = NULL; if (!(chip->open & SB_OPEN_MIDI_OUTPUT)) { spin_unlock_irqrestore(&chip->open_lock, flags); snd_sbdsp_reset(chip); /* reset DSP */ } else { spin_unlock_irqrestore(&chip->open_lock, flags); } return 0; } static int snd_sb8dsp_midi_output_close(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_sb *chip; chip = substream->rmidi->private_data; del_timer_sync(&chip->midi_timer); spin_lock_irqsave(&chip->open_lock, flags); chip->open &= ~(SB_OPEN_MIDI_OUTPUT | SB_OPEN_MIDI_OUTPUT_TRIGGER); chip->midi_substream_output = NULL; if (!(chip->open & SB_OPEN_MIDI_INPUT)) { spin_unlock_irqrestore(&chip->open_lock, flags); snd_sbdsp_reset(chip); /* reset DSP */ } else { spin_unlock_irqrestore(&chip->open_lock, flags); } return 0; } static void snd_sb8dsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up) { unsigned long flags; struct snd_sb *chip; chip = substream->rmidi->private_data; spin_lock_irqsave(&chip->open_lock, flags); if (up) { if (!(chip->open & SB_OPEN_MIDI_INPUT_TRIGGER)) { if (chip->hardware < SB_HW_20) snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ); chip->open |= SB_OPEN_MIDI_INPUT_TRIGGER; } } else { if (chip->open & SB_OPEN_MIDI_INPUT_TRIGGER) { if (chip->hardware < SB_HW_20) snd_sbdsp_command(chip, SB_DSP_MIDI_INPUT_IRQ); chip->open &= ~SB_OPEN_MIDI_INPUT_TRIGGER; } } spin_unlock_irqrestore(&chip->open_lock, flags); } static void snd_sb8dsp_midi_output_write(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_sb *chip; char byte; int max = 32; /* how big is Tx FIFO? */ chip = substream->rmidi->private_data; while (max-- > 0) { spin_lock_irqsave(&chip->open_lock, flags); if (snd_rawmidi_transmit_peek(substream, &byte, 1) != 1) { chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER; del_timer(&chip->midi_timer); spin_unlock_irqrestore(&chip->open_lock, flags); break; } if (chip->hardware >= SB_HW_20) { int timeout = 8; while ((inb(SBP(chip, STATUS)) & 0x80) != 0 && --timeout > 0) ; if (timeout == 0) { /* Tx FIFO full - try again later */ spin_unlock_irqrestore(&chip->open_lock, flags); break; } outb(byte, SBP(chip, WRITE)); } else { snd_sbdsp_command(chip, SB_DSP_MIDI_OUTPUT); snd_sbdsp_command(chip, byte); } snd_rawmidi_transmit_ack(substream, 1); spin_unlock_irqrestore(&chip->open_lock, flags); } } static void snd_sb8dsp_midi_output_timer(struct timer_list *t) { struct snd_sb *chip = from_timer(chip, t, midi_timer); struct snd_rawmidi_substream *substream = chip->midi_substream_output; unsigned long flags; spin_lock_irqsave(&chip->open_lock, flags); mod_timer(&chip->midi_timer, 1 + jiffies); spin_unlock_irqrestore(&chip->open_lock, flags); snd_sb8dsp_midi_output_write(substream); } static void snd_sb8dsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up) { unsigned long flags; struct snd_sb *chip; chip = substream->rmidi->private_data; spin_lock_irqsave(&chip->open_lock, flags); if (up) { if (!(chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER)) { mod_timer(&chip->midi_timer, 1 + jiffies); chip->open |= SB_OPEN_MIDI_OUTPUT_TRIGGER; } } else { if (chip->open & SB_OPEN_MIDI_OUTPUT_TRIGGER) { chip->open &= ~SB_OPEN_MIDI_OUTPUT_TRIGGER; } } spin_unlock_irqrestore(&chip->open_lock, flags); if (up) snd_sb8dsp_midi_output_write(substream); } static const struct snd_rawmidi_ops snd_sb8dsp_midi_output = { .open = snd_sb8dsp_midi_output_open, .close = snd_sb8dsp_midi_output_close, .trigger = snd_sb8dsp_midi_output_trigger, }; static const struct snd_rawmidi_ops snd_sb8dsp_midi_input = { .open = snd_sb8dsp_midi_input_open, .close = snd_sb8dsp_midi_input_close, .trigger = snd_sb8dsp_midi_input_trigger, }; int snd_sb8dsp_midi(struct snd_sb *chip, int device) { struct snd_rawmidi *rmidi; int err; err = snd_rawmidi_new(chip->card, "SB8 MIDI", device, 1, 1, &rmidi); if (err < 0) return err; strcpy(rmidi->name, "SB8 MIDI"); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_sb8dsp_midi_output); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_sb8dsp_midi_input); rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT; if (chip->hardware >= SB_HW_20) rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX; rmidi->private_data = chip; timer_setup(&chip->midi_timer, snd_sb8dsp_midi_output_timer, 0); chip->rmidi = rmidi; return 0; }
linux-master
sound/isa/sb/sb8_midi.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for SoundBlaster 16/AWE32/AWE64 soundcards * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <asm/dma.h> #include <linux/init.h> #include <linux/pnp.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/module.h> #include <sound/core.h> #include <sound/sb.h> #include <sound/sb16_csp.h> #include <sound/mpu401.h> #include <sound/opl3.h> #include <sound/emu8000.h> #include <sound/seq_device.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #ifdef SNDRV_SBAWE #define PFX "sbawe: " #else #define PFX "sb16: " #endif MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); #ifndef SNDRV_SBAWE MODULE_DESCRIPTION("Sound Blaster 16"); #else MODULE_DESCRIPTION("Sound Blaster AWE"); #endif #if 0 #define SNDRV_DEBUG_IRQ #endif #if defined(SNDRV_SBAWE) && IS_ENABLED(CONFIG_SND_SEQUENCER) #define SNDRV_SBAWE_EMU8000 #endif static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; #endif static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x330,0x300 */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; #ifdef SNDRV_SBAWE_EMU8000 static long awe_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; #endif static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */ static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 5,6,7 */ static int mic_agc[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; #ifdef CONFIG_SND_SB16_CSP static int csp[SNDRV_CARDS]; #endif #ifdef SNDRV_SBAWE_EMU8000 static int seq_ports[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4}; #endif module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for SoundBlaster 16 soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for SoundBlaster 16 soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable SoundBlaster 16 soundcard."); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard."); #endif module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for SB16 driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for SB16 driver."); module_param_hw_array(fm_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(fm_port, "FM port # for SB16 PnP driver."); #ifdef SNDRV_SBAWE_EMU8000 module_param_hw_array(awe_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(awe_port, "AWE port # for SB16 PnP driver."); #endif module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for SB16 driver."); module_param_hw_array(dma8, int, dma, NULL, 0444); MODULE_PARM_DESC(dma8, "8-bit DMA # for SB16 driver."); module_param_hw_array(dma16, int, dma, NULL, 0444); MODULE_PARM_DESC(dma16, "16-bit DMA # for SB16 driver."); module_param_array(mic_agc, int, NULL, 0444); MODULE_PARM_DESC(mic_agc, "Mic Auto-Gain-Control switch."); #ifdef CONFIG_SND_SB16_CSP module_param_array(csp, int, NULL, 0444); MODULE_PARM_DESC(csp, "ASP/CSP chip support."); #endif #ifdef SNDRV_SBAWE_EMU8000 module_param_array(seq_ports, int, NULL, 0444); MODULE_PARM_DESC(seq_ports, "Number of sequencer ports for WaveTable synth."); #endif #ifdef CONFIG_PNP static int isa_registered; static int pnp_registered; #endif struct snd_card_sb16 { struct resource *fm_res; /* used to block FM i/o region for legacy cards */ struct snd_sb *chip; #ifdef CONFIG_PNP int dev_no; struct pnp_dev *dev; #ifdef SNDRV_SBAWE_EMU8000 struct pnp_dev *devwt; #endif #endif }; #ifdef CONFIG_PNP static const struct pnp_card_device_id snd_sb16_pnpids[] = { #ifndef SNDRV_SBAWE /* Sound Blaster 16 PnP */ { .id = "CTL0024", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL0025", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL0026", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL0027", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL0028", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL0029", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL002a", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ /* Note: This card has also a CTL0051:StereoEnhance device!!! */ { .id = "CTL002b", .devs = { { "CTL0031" } } }, /* Sound Blaster 16 PnP */ { .id = "CTL002c", .devs = { { "CTL0031" } } }, /* Sound Blaster Vibra16S */ { .id = "CTL0051", .devs = { { "CTL0001" } } }, /* Sound Blaster Vibra16C */ { .id = "CTL0070", .devs = { { "CTL0001" } } }, /* Sound Blaster Vibra16CL - added by [email protected] */ { .id = "CTL0080", .devs = { { "CTL0041" } } }, /* Sound Blaster 16 'value' PnP. It says model ct4130 on the pcb, */ /* but ct4131 on a sticker on the board.. */ { .id = "CTL0086", .devs = { { "CTL0041" } } }, /* Sound Blaster Vibra16X */ { .id = "CTL00f0", .devs = { { "CTL0043" } } }, /* Sound Blaster 16 (Virtual PC 2004) */ { .id = "tBA03b0", .devs = { {.id="PNPb003" } } }, #else /* SNDRV_SBAWE defined */ /* Sound Blaster AWE 32 PnP */ { .id = "CTL0035", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0039", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0042", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0043", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ /* Note: This card has also a CTL0051:StereoEnhance device!!! */ { .id = "CTL0044", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ /* Note: This card has also a CTL0051:StereoEnhance device!!! */ { .id = "CTL0045", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0046", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0047", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0048", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL0054", .devs = { { "CTL0031" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL009a", .devs = { { "CTL0041" }, { "CTL0021" } } }, /* Sound Blaster AWE 32 PnP */ { .id = "CTL009c", .devs = { { "CTL0041" }, { "CTL0021" } } }, /* Sound Blaster 32 PnP */ { .id = "CTL009f", .devs = { { "CTL0041" }, { "CTL0021" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL009d", .devs = { { "CTL0042" }, { "CTL0022" } } }, /* Sound Blaster AWE 64 PnP Gold */ { .id = "CTL009e", .devs = { { "CTL0044" }, { "CTL0023" } } }, /* Sound Blaster AWE 64 PnP Gold */ { .id = "CTL00b2", .devs = { { "CTL0044" }, { "CTL0023" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL00c1", .devs = { { "CTL0042" }, { "CTL0022" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL00c3", .devs = { { "CTL0045" }, { "CTL0022" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL00c5", .devs = { { "CTL0045" }, { "CTL0022" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL00c7", .devs = { { "CTL0045" }, { "CTL0022" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL00e4", .devs = { { "CTL0045" }, { "CTL0022" } } }, /* Sound Blaster AWE 64 PnP */ { .id = "CTL00e9", .devs = { { "CTL0045" }, { "CTL0022" } } }, /* Sound Blaster 16 PnP (AWE) */ { .id = "CTL00ed", .devs = { { "CTL0041" }, { "CTL0070" } } }, /* Generic entries */ { .id = "CTLXXXX" , .devs = { { "CTL0031" }, { "CTL0021" } } }, { .id = "CTLXXXX" , .devs = { { "CTL0041" }, { "CTL0021" } } }, { .id = "CTLXXXX" , .devs = { { "CTL0042" }, { "CTL0022" } } }, { .id = "CTLXXXX" , .devs = { { "CTL0044" }, { "CTL0023" } } }, { .id = "CTLXXXX" , .devs = { { "CTL0045" }, { "CTL0022" } } }, #endif /* SNDRV_SBAWE */ { .id = "", } }; MODULE_DEVICE_TABLE(pnp_card, snd_sb16_pnpids); #endif /* CONFIG_PNP */ #ifdef SNDRV_SBAWE_EMU8000 #define DRIVER_NAME "snd-card-sbawe" #else #define DRIVER_NAME "snd-card-sb16" #endif #ifdef CONFIG_PNP static int snd_card_sb16_pnp(int dev, struct snd_card_sb16 *acard, struct pnp_card_link *card, const struct pnp_card_device_id *id) { struct pnp_dev *pdev; int err; acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL); if (acard->dev == NULL) return -ENODEV; #ifdef SNDRV_SBAWE_EMU8000 acard->devwt = pnp_request_card_device(card, id->devs[1].id, acard->dev); #endif /* Audio initialization */ pdev = acard->dev; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n"); return err; } port[dev] = pnp_port_start(pdev, 0); mpu_port[dev] = pnp_port_start(pdev, 1); fm_port[dev] = pnp_port_start(pdev, 2); dma8[dev] = pnp_dma(pdev, 0); dma16[dev] = pnp_dma(pdev, 1); irq[dev] = pnp_irq(pdev, 0); snd_printdd("pnp SB16: port=0x%lx, mpu port=0x%lx, fm port=0x%lx\n", port[dev], mpu_port[dev], fm_port[dev]); snd_printdd("pnp SB16: dma1=%i, dma2=%i, irq=%i\n", dma8[dev], dma16[dev], irq[dev]); #ifdef SNDRV_SBAWE_EMU8000 /* WaveTable initialization */ pdev = acard->devwt; if (pdev != NULL) { err = pnp_activate_dev(pdev); if (err < 0) { goto __wt_error; } awe_port[dev] = pnp_port_start(pdev, 0); snd_printdd("pnp SB16: wavetable port=0x%llx\n", (unsigned long long)pnp_port_start(pdev, 0)); } else { __wt_error: if (pdev) { pnp_release_card_device(pdev); snd_printk(KERN_ERR PFX "WaveTable pnp configure failure\n"); } acard->devwt = NULL; awe_port[dev] = -1; } #endif return 0; } #endif /* CONFIG_PNP */ #ifdef CONFIG_PNP #define is_isapnp_selected(dev) isapnp[dev] #else #define is_isapnp_selected(dev) 0 #endif static int snd_sb16_card_new(struct device *devptr, int dev, struct snd_card **cardp) { struct snd_card *card; int err; err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_card_sb16), &card); if (err < 0) return err; *cardp = card; return 0; } static int snd_sb16_probe(struct snd_card *card, int dev) { int xirq, xdma8, xdma16; struct snd_sb *chip; struct snd_card_sb16 *acard = card->private_data; struct snd_opl3 *opl3; struct snd_hwdep *synth = NULL; #ifdef CONFIG_SND_SB16_CSP struct snd_hwdep *xcsp = NULL; #endif unsigned long flags; int err; xirq = irq[dev]; xdma8 = dma8[dev]; xdma16 = dma16[dev]; err = snd_sbdsp_create(card, port[dev], xirq, snd_sb16dsp_interrupt, xdma8, xdma16, SB_HW_AUTO, &chip); if (err < 0) return err; acard->chip = chip; if (chip->hardware != SB_HW_16) { snd_printk(KERN_ERR PFX "SB 16 chip was not detected at 0x%lx\n", port[dev]); return -ENODEV; } chip->mpu_port = mpu_port[dev]; if (!is_isapnp_selected(dev)) { err = snd_sb16dsp_configure(chip); if (err < 0) return err; } err = snd_sb16dsp_pcm(chip, 0); if (err < 0) return err; strcpy(card->driver, #ifdef SNDRV_SBAWE_EMU8000 awe_port[dev] > 0 ? "SB AWE" : #endif "SB16"); strcpy(card->shortname, chip->name); sprintf(card->longname, "%s at 0x%lx, irq %i, dma ", chip->name, chip->port, xirq); if (xdma8 >= 0) sprintf(card->longname + strlen(card->longname), "%d", xdma8); if (xdma16 >= 0) sprintf(card->longname + strlen(card->longname), "%s%d", xdma8 >= 0 ? "&" : "", xdma16); if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) { err = snd_mpu401_uart_new(card, 0, MPU401_HW_SB, chip->mpu_port, MPU401_INFO_IRQ_HOOK, -1, &chip->rmidi); if (err < 0) return err; chip->rmidi_callback = snd_mpu401_uart_interrupt; } #ifdef SNDRV_SBAWE_EMU8000 if (awe_port[dev] == SNDRV_AUTO_PORT) awe_port[dev] = 0; /* disable */ #endif if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) { if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_OPL3, acard->fm_res != NULL || fm_port[dev] == port[dev], &opl3) < 0) { snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n", fm_port[dev], fm_port[dev] + 2); } else { #ifdef SNDRV_SBAWE_EMU8000 int seqdev = awe_port[dev] > 0 ? 2 : 1; #else int seqdev = 1; #endif err = snd_opl3_hwdep_new(opl3, 0, seqdev, &synth); if (err < 0) return err; } } err = snd_sbmixer_new(chip); if (err < 0) return err; #ifdef CONFIG_SND_SB16_CSP /* CSP chip on SB16ASP/AWE32 */ if ((chip->hardware == SB_HW_16) && csp[dev]) { snd_sb_csp_new(chip, synth != NULL ? 1 : 0, &xcsp); if (xcsp) { chip->csp = xcsp->private_data; chip->hardware = SB_HW_16CSP; } else { snd_printk(KERN_INFO PFX "warning - CSP chip not detected on soundcard #%i\n", dev + 1); } } #endif #ifdef SNDRV_SBAWE_EMU8000 if (awe_port[dev] > 0) { err = snd_emu8000_new(card, 1, awe_port[dev], seq_ports[dev], NULL); if (err < 0) { snd_printk(KERN_ERR PFX "fatal error - EMU-8000 synthesizer not detected at 0x%lx\n", awe_port[dev]); return err; } } #endif /* setup Mic AGC */ spin_lock_irqsave(&chip->mixer_lock, flags); snd_sbmixer_write(chip, SB_DSP4_MIC_AGC, (snd_sbmixer_read(chip, SB_DSP4_MIC_AGC) & 0x01) | (mic_agc[dev] ? 0x00 : 0x01)); spin_unlock_irqrestore(&chip->mixer_lock, flags); err = snd_card_register(card); if (err < 0) return err; return 0; } #ifdef CONFIG_PM static int snd_sb16_suspend(struct snd_card *card, pm_message_t state) { struct snd_card_sb16 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); snd_sbmixer_suspend(chip); return 0; } static int snd_sb16_resume(struct snd_card *card) { struct snd_card_sb16 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_sbdsp_reset(chip); snd_sbmixer_resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static int snd_sb16_isa_probe1(int dev, struct device *pdev) { struct snd_card_sb16 *acard; struct snd_card *card; int err; err = snd_sb16_card_new(pdev, dev, &card); if (err < 0) return err; acard = card->private_data; /* non-PnP FM port address is hardwired with base port address */ fm_port[dev] = port[dev]; /* block the 0x388 port to avoid PnP conflicts */ acard->fm_res = devm_request_region(card->dev, 0x388, 4, "SoundBlaster FM"); #ifdef SNDRV_SBAWE_EMU8000 /* non-PnP AWE port address is hardwired with base port address */ awe_port[dev] = port[dev] + 0x400; #endif err = snd_sb16_probe(card, dev); if (err < 0) return err; dev_set_drvdata(pdev, card); return 0; } static int snd_sb16_isa_match(struct device *pdev, unsigned int dev) { return enable[dev] && !is_isapnp_selected(dev); } static int snd_sb16_isa_probe(struct device *pdev, unsigned int dev) { int err; static const int possible_irqs[] = {5, 9, 10, 7, -1}; static const int possible_dmas8[] = {1, 3, 0, -1}; static const int possible_dmas16[] = {5, 6, 7, -1}; if (irq[dev] == SNDRV_AUTO_IRQ) { irq[dev] = snd_legacy_find_free_irq(possible_irqs); if (irq[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); return -EBUSY; } } if (dma8[dev] == SNDRV_AUTO_DMA) { dma8[dev] = snd_legacy_find_free_dma(possible_dmas8); if (dma8[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free 8-bit DMA\n"); return -EBUSY; } } if (dma16[dev] == SNDRV_AUTO_DMA) { dma16[dev] = snd_legacy_find_free_dma(possible_dmas16); if (dma16[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free 16-bit DMA\n"); return -EBUSY; } } if (port[dev] != SNDRV_AUTO_PORT) return snd_sb16_isa_probe1(dev, pdev); else { static const int possible_ports[] = {0x220, 0x240, 0x260, 0x280}; int i; for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { port[dev] = possible_ports[i]; err = snd_sb16_isa_probe1(dev, pdev); if (! err) return 0; } return err; } } #ifdef CONFIG_PM static int snd_sb16_isa_suspend(struct device *dev, unsigned int n, pm_message_t state) { return snd_sb16_suspend(dev_get_drvdata(dev), state); } static int snd_sb16_isa_resume(struct device *dev, unsigned int n) { return snd_sb16_resume(dev_get_drvdata(dev)); } #endif #ifdef SNDRV_SBAWE #define DEV_NAME "sbawe" #else #define DEV_NAME "sb16" #endif static struct isa_driver snd_sb16_isa_driver = { .match = snd_sb16_isa_match, .probe = snd_sb16_isa_probe, #ifdef CONFIG_PM .suspend = snd_sb16_isa_suspend, .resume = snd_sb16_isa_resume, #endif .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_sb16_pnp_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int dev; struct snd_card *card; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (!enable[dev] || !isapnp[dev]) continue; res = snd_sb16_card_new(&pcard->card->dev, dev, &card); if (res < 0) return res; res = snd_card_sb16_pnp(dev, card->private_data, pcard, pid); if (res < 0) return res; res = snd_sb16_probe(card, dev); if (res < 0) return res; pnp_set_card_drvdata(pcard, card); dev++; return 0; } return -ENODEV; } #ifdef CONFIG_PM static int snd_sb16_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { return snd_sb16_suspend(pnp_get_card_drvdata(pcard), state); } static int snd_sb16_pnp_resume(struct pnp_card_link *pcard) { return snd_sb16_resume(pnp_get_card_drvdata(pcard)); } #endif static struct pnp_card_driver sb16_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, #ifdef SNDRV_SBAWE .name = "sbawe", #else .name = "sb16", #endif .id_table = snd_sb16_pnpids, .probe = snd_sb16_pnp_detect, #ifdef CONFIG_PM .suspend = snd_sb16_pnp_suspend, .resume = snd_sb16_pnp_resume, #endif }; #endif /* CONFIG_PNP */ static int __init alsa_card_sb16_init(void) { int err; err = isa_register_driver(&snd_sb16_isa_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_card_driver(&sb16_pnpc_driver); if (!err) pnp_registered = 1; if (isa_registered) err = 0; #endif return err; } static void __exit alsa_card_sb16_exit(void) { #ifdef CONFIG_PNP if (pnp_registered) pnp_unregister_card_driver(&sb16_pnpc_driver); if (isa_registered) #endif isa_unregister_driver(&snd_sb16_isa_driver); } module_init(alsa_card_sb16_init) module_exit(alsa_card_sb16_exit)
linux-master
sound/isa/sb/sb16.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Uros Bizjak <[email protected]> * * Routines for control of 8-bit SoundBlaster cards and clones * Please note: I don't have access to old SB8 soundcards. * * -- * * Thu Apr 29 20:36:17 BST 1999 George David Morrison <[email protected]> * DSP can't respond to commands whilst in "high speed" mode. Caused * glitching during playback. Fixed. * * Wed Jul 12 22:02:55 CEST 2000 Uros Bizjak <[email protected]> * Cleaned up and rewrote lowlevel routines. */ #include <linux/io.h> #include <asm/dma.h> #include <linux/init.h> #include <linux/time.h> #include <linux/module.h> #include <sound/core.h> #include <sound/sb.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>, Uros Bizjak <[email protected]>"); MODULE_DESCRIPTION("Routines for control of 8-bit SoundBlaster cards and clones"); MODULE_LICENSE("GPL"); #define SB8_CLOCK 1000000 #define SB8_DEN(v) ((SB8_CLOCK + (v) / 2) / (v)) #define SB8_RATE(v) (SB8_CLOCK / SB8_DEN(v)) static const struct snd_ratnum clock = { .num = SB8_CLOCK, .den_min = 1, .den_max = 256, .den_step = 1, }; static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clock = { .nrats = 1, .rats = &clock, }; static const struct snd_ratnum stereo_clocks[] = { { .num = SB8_CLOCK, .den_min = SB8_DEN(22050), .den_max = SB8_DEN(22050), .den_step = 1, }, { .num = SB8_CLOCK, .den_min = SB8_DEN(11025), .den_max = SB8_DEN(11025), .den_step = 1, } }; static int snd_sb8_hw_constraint_rate_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); if (c->min > 1) { unsigned int num = 0, den = 0; int err = snd_interval_ratnum(hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE), 2, stereo_clocks, &num, &den); if (err >= 0 && den) { params->rate_num = num; params->rate_den = den; } return err; } return 0; } static int snd_sb8_hw_constraint_channels_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); if (r->min > SB8_RATE(22050) || r->max <= SB8_RATE(11025)) { struct snd_interval t = { .min = 1, .max = 1 }; return snd_interval_refine(hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS), &t); } return 0; } static int snd_sb8_playback_prepare(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned int mixreg, rate, size, count; unsigned char format; unsigned char stereo = runtime->channels > 1; int dma; rate = runtime->rate; switch (chip->hardware) { case SB_HW_JAZZ16: if (runtime->format == SNDRV_PCM_FORMAT_S16_LE) { if (chip->mode & SB_MODE_CAPTURE_16) return -EBUSY; else chip->mode |= SB_MODE_PLAYBACK_16; } chip->playback_format = SB_DSP_LO_OUTPUT_AUTO; break; case SB_HW_PRO: if (runtime->channels > 1) { if (snd_BUG_ON(rate != SB8_RATE(11025) && rate != SB8_RATE(22050))) return -EINVAL; chip->playback_format = SB_DSP_HI_OUTPUT_AUTO; break; } fallthrough; case SB_HW_201: if (rate > 23000) { chip->playback_format = SB_DSP_HI_OUTPUT_AUTO; break; } fallthrough; case SB_HW_20: chip->playback_format = SB_DSP_LO_OUTPUT_AUTO; break; case SB_HW_10: chip->playback_format = SB_DSP_OUTPUT; break; default: return -EINVAL; } if (chip->mode & SB_MODE_PLAYBACK_16) { format = stereo ? SB_DSP_STEREO_16BIT : SB_DSP_MONO_16BIT; dma = chip->dma16; } else { format = stereo ? SB_DSP_STEREO_8BIT : SB_DSP_MONO_8BIT; chip->mode |= SB_MODE_PLAYBACK_8; dma = chip->dma8; } size = chip->p_dma_size = snd_pcm_lib_buffer_bytes(substream); count = chip->p_period_size = snd_pcm_lib_period_bytes(substream); spin_lock_irqsave(&chip->reg_lock, flags); snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); if (chip->hardware == SB_HW_JAZZ16) snd_sbdsp_command(chip, format); else if (stereo) { /* set playback stereo mode */ spin_lock(&chip->mixer_lock); mixreg = snd_sbmixer_read(chip, SB_DSP_STEREO_SW); snd_sbmixer_write(chip, SB_DSP_STEREO_SW, mixreg | 0x02); spin_unlock(&chip->mixer_lock); /* Soundblaster hardware programming reference guide, 3-23 */ snd_sbdsp_command(chip, SB_DSP_DMA8_EXIT); runtime->dma_area[0] = 0x80; snd_dma_program(dma, runtime->dma_addr, 1, DMA_MODE_WRITE); /* force interrupt */ snd_sbdsp_command(chip, SB_DSP_OUTPUT); snd_sbdsp_command(chip, 0); snd_sbdsp_command(chip, 0); } snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE); if (stereo) { snd_sbdsp_command(chip, 256 - runtime->rate_den / 2); spin_lock(&chip->mixer_lock); /* save output filter status and turn it off */ mixreg = snd_sbmixer_read(chip, SB_DSP_PLAYBACK_FILT); snd_sbmixer_write(chip, SB_DSP_PLAYBACK_FILT, mixreg | 0x20); spin_unlock(&chip->mixer_lock); /* just use force_mode16 for temporary storate... */ chip->force_mode16 = mixreg; } else { snd_sbdsp_command(chip, 256 - runtime->rate_den); } if (chip->playback_format != SB_DSP_OUTPUT) { if (chip->mode & SB_MODE_PLAYBACK_16) count /= 2; count--; snd_sbdsp_command(chip, SB_DSP_BLOCK_SIZE); snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); } spin_unlock_irqrestore(&chip->reg_lock, flags); snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); return 0; } static int snd_sb8_playback_trigger(struct snd_pcm_substream *substream, int cmd) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); unsigned int count; spin_lock_irqsave(&chip->reg_lock, flags); switch (cmd) { case SNDRV_PCM_TRIGGER_START: snd_sbdsp_command(chip, chip->playback_format); if (chip->playback_format == SB_DSP_OUTPUT) { count = chip->p_period_size - 1; snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); } break; case SNDRV_PCM_TRIGGER_STOP: if (chip->playback_format == SB_DSP_HI_OUTPUT_AUTO) { struct snd_pcm_runtime *runtime = substream->runtime; snd_sbdsp_reset(chip); if (runtime->channels > 1) { spin_lock(&chip->mixer_lock); /* restore output filter and set hardware to mono mode */ snd_sbmixer_write(chip, SB_DSP_STEREO_SW, chip->force_mode16 & ~0x02); spin_unlock(&chip->mixer_lock); } } else { snd_sbdsp_command(chip, SB_DSP_DMA8_OFF); } snd_sbdsp_command(chip, SB_DSP_SPEAKER_OFF); } spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_sb8_capture_prepare(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned int mixreg, rate, size, count; unsigned char format; unsigned char stereo = runtime->channels > 1; int dma; rate = runtime->rate; switch (chip->hardware) { case SB_HW_JAZZ16: if (runtime->format == SNDRV_PCM_FORMAT_S16_LE) { if (chip->mode & SB_MODE_PLAYBACK_16) return -EBUSY; else chip->mode |= SB_MODE_CAPTURE_16; } chip->capture_format = SB_DSP_LO_INPUT_AUTO; break; case SB_HW_PRO: if (runtime->channels > 1) { if (snd_BUG_ON(rate != SB8_RATE(11025) && rate != SB8_RATE(22050))) return -EINVAL; chip->capture_format = SB_DSP_HI_INPUT_AUTO; break; } chip->capture_format = (rate > 23000) ? SB_DSP_HI_INPUT_AUTO : SB_DSP_LO_INPUT_AUTO; break; case SB_HW_201: if (rate > 13000) { chip->capture_format = SB_DSP_HI_INPUT_AUTO; break; } fallthrough; case SB_HW_20: chip->capture_format = SB_DSP_LO_INPUT_AUTO; break; case SB_HW_10: chip->capture_format = SB_DSP_INPUT; break; default: return -EINVAL; } if (chip->mode & SB_MODE_CAPTURE_16) { format = stereo ? SB_DSP_STEREO_16BIT : SB_DSP_MONO_16BIT; dma = chip->dma16; } else { format = stereo ? SB_DSP_STEREO_8BIT : SB_DSP_MONO_8BIT; chip->mode |= SB_MODE_CAPTURE_8; dma = chip->dma8; } size = chip->c_dma_size = snd_pcm_lib_buffer_bytes(substream); count = chip->c_period_size = snd_pcm_lib_period_bytes(substream); spin_lock_irqsave(&chip->reg_lock, flags); snd_sbdsp_command(chip, SB_DSP_SPEAKER_OFF); if (chip->hardware == SB_HW_JAZZ16) snd_sbdsp_command(chip, format); else if (stereo) snd_sbdsp_command(chip, SB_DSP_STEREO_8BIT); snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE); if (stereo) { snd_sbdsp_command(chip, 256 - runtime->rate_den / 2); spin_lock(&chip->mixer_lock); /* save input filter status and turn it off */ mixreg = snd_sbmixer_read(chip, SB_DSP_CAPTURE_FILT); snd_sbmixer_write(chip, SB_DSP_CAPTURE_FILT, mixreg | 0x20); spin_unlock(&chip->mixer_lock); /* just use force_mode16 for temporary storate... */ chip->force_mode16 = mixreg; } else { snd_sbdsp_command(chip, 256 - runtime->rate_den); } if (chip->capture_format != SB_DSP_INPUT) { if (chip->mode & SB_MODE_PLAYBACK_16) count /= 2; count--; snd_sbdsp_command(chip, SB_DSP_BLOCK_SIZE); snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); } spin_unlock_irqrestore(&chip->reg_lock, flags); snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); return 0; } static int snd_sb8_capture_trigger(struct snd_pcm_substream *substream, int cmd) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); unsigned int count; spin_lock_irqsave(&chip->reg_lock, flags); switch (cmd) { case SNDRV_PCM_TRIGGER_START: snd_sbdsp_command(chip, chip->capture_format); if (chip->capture_format == SB_DSP_INPUT) { count = chip->c_period_size - 1; snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); } break; case SNDRV_PCM_TRIGGER_STOP: if (chip->capture_format == SB_DSP_HI_INPUT_AUTO) { struct snd_pcm_runtime *runtime = substream->runtime; snd_sbdsp_reset(chip); if (runtime->channels > 1) { /* restore input filter status */ spin_lock(&chip->mixer_lock); snd_sbmixer_write(chip, SB_DSP_CAPTURE_FILT, chip->force_mode16); spin_unlock(&chip->mixer_lock); /* set hardware to mono mode */ snd_sbdsp_command(chip, SB_DSP_MONO_8BIT); } } else { snd_sbdsp_command(chip, SB_DSP_DMA8_OFF); } snd_sbdsp_command(chip, SB_DSP_SPEAKER_OFF); } spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } irqreturn_t snd_sb8dsp_interrupt(struct snd_sb *chip) { struct snd_pcm_substream *substream; snd_sb_ack_8bit(chip); switch (chip->mode) { case SB_MODE_PLAYBACK_16: /* ok.. playback is active */ if (chip->hardware != SB_HW_JAZZ16) break; fallthrough; case SB_MODE_PLAYBACK_8: substream = chip->playback_substream; if (chip->playback_format == SB_DSP_OUTPUT) snd_sb8_playback_trigger(substream, SNDRV_PCM_TRIGGER_START); snd_pcm_period_elapsed(substream); break; case SB_MODE_CAPTURE_16: if (chip->hardware != SB_HW_JAZZ16) break; fallthrough; case SB_MODE_CAPTURE_8: substream = chip->capture_substream; if (chip->capture_format == SB_DSP_INPUT) snd_sb8_capture_trigger(substream, SNDRV_PCM_TRIGGER_START); snd_pcm_period_elapsed(substream); break; } return IRQ_HANDLED; } static snd_pcm_uframes_t snd_sb8_playback_pointer(struct snd_pcm_substream *substream) { struct snd_sb *chip = snd_pcm_substream_chip(substream); size_t ptr; int dma; if (chip->mode & SB_MODE_PLAYBACK_8) dma = chip->dma8; else if (chip->mode & SB_MODE_PLAYBACK_16) dma = chip->dma16; else return 0; ptr = snd_dma_pointer(dma, chip->p_dma_size); return bytes_to_frames(substream->runtime, ptr); } static snd_pcm_uframes_t snd_sb8_capture_pointer(struct snd_pcm_substream *substream) { struct snd_sb *chip = snd_pcm_substream_chip(substream); size_t ptr; int dma; if (chip->mode & SB_MODE_CAPTURE_8) dma = chip->dma8; else if (chip->mode & SB_MODE_CAPTURE_16) dma = chip->dma16; else return 0; ptr = snd_dma_pointer(dma, chip->c_dma_size); return bytes_to_frames(substream->runtime, ptr); } /* */ static const struct snd_pcm_hardware snd_sb8_playback = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_U8, .rates = (SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050), .rate_min = 4000, .rate_max = 23000, .channels_min = 1, .channels_max = 1, .buffer_bytes_max = 65536, .period_bytes_min = 64, .period_bytes_max = 65536, .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_sb8_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_U8, .rates = (SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025), .rate_min = 4000, .rate_max = 13000, .channels_min = 1, .channels_max = 1, .buffer_bytes_max = 65536, .period_bytes_min = 64, .period_bytes_max = 65536, .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; /* * */ static int snd_sb8_open(struct snd_pcm_substream *substream) { struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned long flags; spin_lock_irqsave(&chip->open_lock, flags); if (chip->open) { spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; } chip->open |= SB_OPEN_PCM; spin_unlock_irqrestore(&chip->open_lock, flags); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { chip->playback_substream = substream; runtime->hw = snd_sb8_playback; } else { chip->capture_substream = substream; runtime->hw = snd_sb8_capture; } switch (chip->hardware) { case SB_HW_JAZZ16: if (chip->dma16 == 5 || chip->dma16 == 7) runtime->hw.formats |= SNDRV_PCM_FMTBIT_S16_LE; runtime->hw.rates |= SNDRV_PCM_RATE_8000_48000; runtime->hw.rate_min = 4000; runtime->hw.rate_max = 50000; runtime->hw.channels_max = 2; break; case SB_HW_PRO: runtime->hw.rate_max = 44100; runtime->hw.channels_max = 2; snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, snd_sb8_hw_constraint_rate_channels, NULL, SNDRV_PCM_HW_PARAM_CHANNELS, SNDRV_PCM_HW_PARAM_RATE, -1); snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, snd_sb8_hw_constraint_channels_rate, NULL, SNDRV_PCM_HW_PARAM_RATE, -1); break; case SB_HW_201: if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { runtime->hw.rate_max = 44100; } else { runtime->hw.rate_max = 15000; } break; default: break; } snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_clock); if (chip->dma8 > 3 || chip->dma16 >= 0) { snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 2); snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 2); runtime->hw.buffer_bytes_max = 128 * 1024 * 1024; runtime->hw.period_bytes_max = 128 * 1024 * 1024; } return 0; } static int snd_sb8_close(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); chip->playback_substream = NULL; chip->capture_substream = NULL; spin_lock_irqsave(&chip->open_lock, flags); chip->open &= ~SB_OPEN_PCM; if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) chip->mode &= ~SB_MODE_PLAYBACK; else chip->mode &= ~SB_MODE_CAPTURE; spin_unlock_irqrestore(&chip->open_lock, flags); return 0; } /* * Initialization part */ static const struct snd_pcm_ops snd_sb8_playback_ops = { .open = snd_sb8_open, .close = snd_sb8_close, .prepare = snd_sb8_playback_prepare, .trigger = snd_sb8_playback_trigger, .pointer = snd_sb8_playback_pointer, }; static const struct snd_pcm_ops snd_sb8_capture_ops = { .open = snd_sb8_open, .close = snd_sb8_close, .prepare = snd_sb8_capture_prepare, .trigger = snd_sb8_capture_trigger, .pointer = snd_sb8_capture_pointer, }; int snd_sb8dsp_pcm(struct snd_sb *chip, int device) { struct snd_card *card = chip->card; struct snd_pcm *pcm; int err; size_t max_prealloc = 64 * 1024; err = snd_pcm_new(card, "SB8 DSP", device, 1, 1, &pcm); if (err < 0) return err; sprintf(pcm->name, "DSP v%i.%i", chip->version >> 8, chip->version & 0xff); pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX; pcm->private_data = chip; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sb8_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sb8_capture_ops); if (chip->dma8 > 3 || chip->dma16 >= 0) max_prealloc = 128 * 1024; snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, max_prealloc); return 0; } EXPORT_SYMBOL(snd_sb8dsp_pcm); EXPORT_SYMBOL(snd_sb8dsp_interrupt); /* sb8_midi.c */ EXPORT_SYMBOL(snd_sb8dsp_midi_interrupt); EXPORT_SYMBOL(snd_sb8dsp_midi);
linux-master
sound/isa/sb/sb8_main.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of 16-bit SoundBlaster cards and clones * Note: This is very ugly hardware which uses one 8-bit DMA channel and * second 16-bit DMA channel. Unfortunately 8-bit DMA channel can't * transfer 16-bit samples and 16-bit DMA channels can't transfer * 8-bit samples. This make full duplex more complicated than * can be... People, don't buy these soundcards for full 16-bit * duplex!!! * Note: 16-bit wide is assigned to first direction which made request. * With full duplex - playback is preferred with abstract layer. * * Note: Some chip revisions have hardware bug. Changing capture * channel from full-duplex 8bit DMA to 16bit DMA will block * 16bit DMA transfers from DSP chip (capture) until 8bit transfer * to DSP chip (playback) starts. This bug can be avoided with * "16bit DMA Allocation" setting set to Playback or Capture. */ #include <linux/io.h> #include <asm/dma.h> #include <linux/init.h> #include <linux/time.h> #include <linux/module.h> #include <sound/core.h> #include <sound/sb.h> #include <sound/sb16_csp.h> #include <sound/mpu401.h> #include <sound/control.h> #include <sound/info.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Routines for control of 16-bit SoundBlaster cards and clones"); MODULE_LICENSE("GPL"); #define runtime_format_bits(runtime) \ ((unsigned int)pcm_format_to_bits((runtime)->format)) #ifdef CONFIG_SND_SB16_CSP static void snd_sb16_csp_playback_prepare(struct snd_sb *chip, struct snd_pcm_runtime *runtime) { if (chip->hardware == SB_HW_16CSP) { struct snd_sb_csp *csp = chip->csp; if (csp->running & SNDRV_SB_CSP_ST_LOADED) { /* manually loaded codec */ if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) && (runtime_format_bits(runtime) == csp->acc_format)) { /* Supported runtime PCM format for playback */ if (csp->ops.csp_use(csp) == 0) { /* If CSP was successfully acquired */ goto __start_CSP; } } else if ((csp->mode & SNDRV_SB_CSP_MODE_QSOUND) && (csp->q_enabled)) { /* QSound decoder is loaded and enabled */ if (runtime_format_bits(runtime) & (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE)) { /* Only for simple PCM formats */ if (csp->ops.csp_use(csp) == 0) { /* If CSP was successfully acquired */ goto __start_CSP; } } } } else if (csp->ops.csp_use(csp) == 0) { /* Acquire CSP and try to autoload hardware codec */ if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_WRITE)) { /* Unsupported format, release CSP */ csp->ops.csp_unuse(csp); } else { __start_CSP: /* Try to start CSP */ if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_PLAYBACK_16) ? SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT, (runtime->channels > 1) ? SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) { /* Failed, release CSP */ csp->ops.csp_unuse(csp); } else { /* Success, CSP acquired and running */ chip->open = SNDRV_SB_CSP_MODE_DSP_WRITE; } } } } } static void snd_sb16_csp_capture_prepare(struct snd_sb *chip, struct snd_pcm_runtime *runtime) { if (chip->hardware == SB_HW_16CSP) { struct snd_sb_csp *csp = chip->csp; if (csp->running & SNDRV_SB_CSP_ST_LOADED) { /* manually loaded codec */ if ((csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) && (runtime_format_bits(runtime) == csp->acc_format)) { /* Supported runtime PCM format for capture */ if (csp->ops.csp_use(csp) == 0) { /* If CSP was successfully acquired */ goto __start_CSP; } } } else if (csp->ops.csp_use(csp) == 0) { /* Acquire CSP and try to autoload hardware codec */ if (csp->ops.csp_autoload(csp, runtime->format, SNDRV_SB_CSP_MODE_DSP_READ)) { /* Unsupported format, release CSP */ csp->ops.csp_unuse(csp); } else { __start_CSP: /* Try to start CSP */ if (csp->ops.csp_start(csp, (chip->mode & SB_MODE_CAPTURE_16) ? SNDRV_SB_CSP_SAMPLE_16BIT : SNDRV_SB_CSP_SAMPLE_8BIT, (runtime->channels > 1) ? SNDRV_SB_CSP_STEREO : SNDRV_SB_CSP_MONO)) { /* Failed, release CSP */ csp->ops.csp_unuse(csp); } else { /* Success, CSP acquired and running */ chip->open = SNDRV_SB_CSP_MODE_DSP_READ; } } } } } static void snd_sb16_csp_update(struct snd_sb *chip) { if (chip->hardware == SB_HW_16CSP) { struct snd_sb_csp *csp = chip->csp; if (csp->qpos_changed) { spin_lock(&chip->reg_lock); csp->ops.csp_qsound_transfer (csp); spin_unlock(&chip->reg_lock); } } } static void snd_sb16_csp_playback_open(struct snd_sb *chip, struct snd_pcm_runtime *runtime) { /* CSP decoders (QSound excluded) support only 16bit transfers */ if (chip->hardware == SB_HW_16CSP) { struct snd_sb_csp *csp = chip->csp; if (csp->running & SNDRV_SB_CSP_ST_LOADED) { /* manually loaded codec */ if (csp->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) { runtime->hw.formats |= csp->acc_format; } } else { /* autoloaded codecs */ runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM; } } } static void snd_sb16_csp_playback_close(struct snd_sb *chip) { if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_WRITE)) { struct snd_sb_csp *csp = chip->csp; if (csp->ops.csp_stop(csp) == 0) { csp->ops.csp_unuse(csp); chip->open = 0; } } } static void snd_sb16_csp_capture_open(struct snd_sb *chip, struct snd_pcm_runtime *runtime) { /* CSP coders support only 16bit transfers */ if (chip->hardware == SB_HW_16CSP) { struct snd_sb_csp *csp = chip->csp; if (csp->running & SNDRV_SB_CSP_ST_LOADED) { /* manually loaded codec */ if (csp->mode & SNDRV_SB_CSP_MODE_DSP_READ) { runtime->hw.formats |= csp->acc_format; } } else { /* autoloaded codecs */ runtime->hw.formats |= SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM; } } } static void snd_sb16_csp_capture_close(struct snd_sb *chip) { if ((chip->hardware == SB_HW_16CSP) && (chip->open == SNDRV_SB_CSP_MODE_DSP_READ)) { struct snd_sb_csp *csp = chip->csp; if (csp->ops.csp_stop(csp) == 0) { csp->ops.csp_unuse(csp); chip->open = 0; } } } #else #define snd_sb16_csp_playback_prepare(chip, runtime) /*nop*/ #define snd_sb16_csp_capture_prepare(chip, runtime) /*nop*/ #define snd_sb16_csp_update(chip) /*nop*/ #define snd_sb16_csp_playback_open(chip, runtime) /*nop*/ #define snd_sb16_csp_playback_close(chip) /*nop*/ #define snd_sb16_csp_capture_open(chip, runtime) /*nop*/ #define snd_sb16_csp_capture_close(chip) /*nop*/ #endif static void snd_sb16_setup_rate(struct snd_sb *chip, unsigned short rate, int channel) { unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); if (chip->mode & (channel == SNDRV_PCM_STREAM_PLAYBACK ? SB_MODE_PLAYBACK_16 : SB_MODE_CAPTURE_16)) snd_sb_ack_16bit(chip); else snd_sb_ack_8bit(chip); if (!(chip->mode & SB_RATE_LOCK)) { chip->locked_rate = rate; snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_IN); snd_sbdsp_command(chip, rate >> 8); snd_sbdsp_command(chip, rate & 0xff); snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT); snd_sbdsp_command(chip, rate >> 8); snd_sbdsp_command(chip, rate & 0xff); } spin_unlock_irqrestore(&chip->reg_lock, flags); } static int snd_sb16_playback_prepare(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned char format; unsigned int size, count, dma; snd_sb16_csp_playback_prepare(chip, runtime); if (snd_pcm_format_unsigned(runtime->format) > 0) { format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO; } else { format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO; } snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_PLAYBACK); size = chip->p_dma_size = snd_pcm_lib_buffer_bytes(substream); dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16; snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); count = snd_pcm_lib_period_bytes(substream); spin_lock_irqsave(&chip->reg_lock, flags); if (chip->mode & SB_MODE_PLAYBACK_16) { count >>= 1; count--; snd_sbdsp_command(chip, SB_DSP4_OUT16_AI); snd_sbdsp_command(chip, format); snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); snd_sbdsp_command(chip, SB_DSP_DMA16_OFF); } else { count--; snd_sbdsp_command(chip, SB_DSP4_OUT8_AI); snd_sbdsp_command(chip, format); snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); snd_sbdsp_command(chip, SB_DSP_DMA8_OFF); } spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_sb16_playback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_sb *chip = snd_pcm_substream_chip(substream); int result = 0; spin_lock(&chip->reg_lock); switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: chip->mode |= SB_RATE_LOCK_PLAYBACK; snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON); break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF); /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */ if (chip->mode & SB_RATE_LOCK_CAPTURE) snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON); chip->mode &= ~SB_RATE_LOCK_PLAYBACK; break; default: result = -EINVAL; } spin_unlock(&chip->reg_lock); return result; } static int snd_sb16_capture_prepare(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned char format; unsigned int size, count, dma; snd_sb16_csp_capture_prepare(chip, runtime); if (snd_pcm_format_unsigned(runtime->format) > 0) { format = runtime->channels > 1 ? SB_DSP4_MODE_UNS_STEREO : SB_DSP4_MODE_UNS_MONO; } else { format = runtime->channels > 1 ? SB_DSP4_MODE_SIGN_STEREO : SB_DSP4_MODE_SIGN_MONO; } snd_sb16_setup_rate(chip, runtime->rate, SNDRV_PCM_STREAM_CAPTURE); size = chip->c_dma_size = snd_pcm_lib_buffer_bytes(substream); dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16; snd_dma_program(dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); count = snd_pcm_lib_period_bytes(substream); spin_lock_irqsave(&chip->reg_lock, flags); if (chip->mode & SB_MODE_CAPTURE_16) { count >>= 1; count--; snd_sbdsp_command(chip, SB_DSP4_IN16_AI); snd_sbdsp_command(chip, format); snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); snd_sbdsp_command(chip, SB_DSP_DMA16_OFF); } else { count--; snd_sbdsp_command(chip, SB_DSP4_IN8_AI); snd_sbdsp_command(chip, format); snd_sbdsp_command(chip, count & 0xff); snd_sbdsp_command(chip, count >> 8); snd_sbdsp_command(chip, SB_DSP_DMA8_OFF); } spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_sb16_capture_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_sb *chip = snd_pcm_substream_chip(substream); int result = 0; spin_lock(&chip->reg_lock); switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: chip->mode |= SB_RATE_LOCK_CAPTURE; snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON); break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: snd_sbdsp_command(chip, chip->mode & SB_MODE_CAPTURE_16 ? SB_DSP_DMA16_OFF : SB_DSP_DMA8_OFF); /* next two lines are needed for some types of DSP4 (SB AWE 32 - 4.13) */ if (chip->mode & SB_RATE_LOCK_PLAYBACK) snd_sbdsp_command(chip, chip->mode & SB_MODE_PLAYBACK_16 ? SB_DSP_DMA16_ON : SB_DSP_DMA8_ON); chip->mode &= ~SB_RATE_LOCK_CAPTURE; break; default: result = -EINVAL; } spin_unlock(&chip->reg_lock); return result; } irqreturn_t snd_sb16dsp_interrupt(int irq, void *dev_id) { struct snd_sb *chip = dev_id; unsigned char status; int ok; spin_lock(&chip->mixer_lock); status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS); spin_unlock(&chip->mixer_lock); if ((status & SB_IRQTYPE_MPUIN) && chip->rmidi_callback) chip->rmidi_callback(irq, chip->rmidi->private_data); if (status & SB_IRQTYPE_8BIT) { ok = 0; if (chip->mode & SB_MODE_PLAYBACK_8) { snd_pcm_period_elapsed(chip->playback_substream); snd_sb16_csp_update(chip); ok++; } if (chip->mode & SB_MODE_CAPTURE_8) { snd_pcm_period_elapsed(chip->capture_substream); ok++; } spin_lock(&chip->reg_lock); if (!ok) snd_sbdsp_command(chip, SB_DSP_DMA8_OFF); snd_sb_ack_8bit(chip); spin_unlock(&chip->reg_lock); } if (status & SB_IRQTYPE_16BIT) { ok = 0; if (chip->mode & SB_MODE_PLAYBACK_16) { snd_pcm_period_elapsed(chip->playback_substream); snd_sb16_csp_update(chip); ok++; } if (chip->mode & SB_MODE_CAPTURE_16) { snd_pcm_period_elapsed(chip->capture_substream); ok++; } spin_lock(&chip->reg_lock); if (!ok) snd_sbdsp_command(chip, SB_DSP_DMA16_OFF); snd_sb_ack_16bit(chip); spin_unlock(&chip->reg_lock); } return IRQ_HANDLED; } /* */ static snd_pcm_uframes_t snd_sb16_playback_pointer(struct snd_pcm_substream *substream) { struct snd_sb *chip = snd_pcm_substream_chip(substream); unsigned int dma; size_t ptr; dma = (chip->mode & SB_MODE_PLAYBACK_8) ? chip->dma8 : chip->dma16; ptr = snd_dma_pointer(dma, chip->p_dma_size); return bytes_to_frames(substream->runtime, ptr); } static snd_pcm_uframes_t snd_sb16_capture_pointer(struct snd_pcm_substream *substream) { struct snd_sb *chip = snd_pcm_substream_chip(substream); unsigned int dma; size_t ptr; dma = (chip->mode & SB_MODE_CAPTURE_8) ? chip->dma8 : chip->dma16; ptr = snd_dma_pointer(dma, chip->c_dma_size); return bytes_to_frames(substream->runtime, ptr); } /* */ static const struct snd_pcm_hardware snd_sb16_playback = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = 0, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100, .rate_min = 4000, .rate_max = 44100, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_sb16_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = 0, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100, .rate_min = 4000, .rate_max = 44100, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; /* * open/close */ static int snd_sb16_playback_open(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; spin_lock_irqsave(&chip->open_lock, flags); if (chip->mode & SB_MODE_PLAYBACK) { spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; } runtime->hw = snd_sb16_playback; /* skip if 16 bit DMA was reserved for capture */ if (chip->force_mode16 & SB_MODE_CAPTURE_16) goto __skip_16bit; if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_CAPTURE_16)) { chip->mode |= SB_MODE_PLAYBACK_16; runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE; /* Vibra16X hack */ if (chip->dma16 <= 3) { runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max = 64 * 1024; } else { snd_sb16_csp_playback_open(chip, runtime); } goto __open_ok; } __skip_16bit: if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_CAPTURE_8)) { chip->mode |= SB_MODE_PLAYBACK_8; /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */ if (chip->dma16 < 0) { runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE; chip->mode |= SB_MODE_PLAYBACK_16; } else { runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8; } runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max = 64 * 1024; goto __open_ok; } spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; __open_ok: if (chip->hardware == SB_HW_ALS100) runtime->hw.rate_max = 48000; if (chip->hardware == SB_HW_CS5530) { runtime->hw.buffer_bytes_max = 32 * 1024; runtime->hw.periods_min = 2; runtime->hw.rate_min = 44100; } if (chip->mode & SB_RATE_LOCK) runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate; chip->playback_substream = substream; spin_unlock_irqrestore(&chip->open_lock, flags); return 0; } static int snd_sb16_playback_close(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); snd_sb16_csp_playback_close(chip); spin_lock_irqsave(&chip->open_lock, flags); chip->playback_substream = NULL; chip->mode &= ~SB_MODE_PLAYBACK; spin_unlock_irqrestore(&chip->open_lock, flags); return 0; } static int snd_sb16_capture_open(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; spin_lock_irqsave(&chip->open_lock, flags); if (chip->mode & SB_MODE_CAPTURE) { spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; } runtime->hw = snd_sb16_capture; /* skip if 16 bit DMA was reserved for playback */ if (chip->force_mode16 & SB_MODE_PLAYBACK_16) goto __skip_16bit; if (chip->dma16 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_16)) { chip->mode |= SB_MODE_CAPTURE_16; runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE; /* Vibra16X hack */ if (chip->dma16 <= 3) { runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max = 64 * 1024; } else { snd_sb16_csp_capture_open(chip, runtime); } goto __open_ok; } __skip_16bit: if (chip->dma8 >= 0 && !(chip->mode & SB_MODE_PLAYBACK_8)) { chip->mode |= SB_MODE_CAPTURE_8; /* DSP v 4.xx can transfer 16bit data through 8bit DMA channel, SBHWPG 2-7 */ if (chip->dma16 < 0) { runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE; chip->mode |= SB_MODE_CAPTURE_16; } else { runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8; } runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max = 64 * 1024; goto __open_ok; } spin_unlock_irqrestore(&chip->open_lock, flags); return -EAGAIN; __open_ok: if (chip->hardware == SB_HW_ALS100) runtime->hw.rate_max = 48000; if (chip->hardware == SB_HW_CS5530) { runtime->hw.buffer_bytes_max = 32 * 1024; runtime->hw.periods_min = 2; runtime->hw.rate_min = 44100; } if (chip->mode & SB_RATE_LOCK) runtime->hw.rate_min = runtime->hw.rate_max = chip->locked_rate; chip->capture_substream = substream; spin_unlock_irqrestore(&chip->open_lock, flags); return 0; } static int snd_sb16_capture_close(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_sb *chip = snd_pcm_substream_chip(substream); snd_sb16_csp_capture_close(chip); spin_lock_irqsave(&chip->open_lock, flags); chip->capture_substream = NULL; chip->mode &= ~SB_MODE_CAPTURE; spin_unlock_irqrestore(&chip->open_lock, flags); return 0; } /* * DMA control interface */ static int snd_sb16_set_dma_mode(struct snd_sb *chip, int what) { if (chip->dma8 < 0 || chip->dma16 < 0) { if (snd_BUG_ON(what)) return -EINVAL; return 0; } if (what == 0) { chip->force_mode16 = 0; } else if (what == 1) { chip->force_mode16 = SB_MODE_PLAYBACK_16; } else if (what == 2) { chip->force_mode16 = SB_MODE_CAPTURE_16; } else { return -EINVAL; } return 0; } static int snd_sb16_get_dma_mode(struct snd_sb *chip) { if (chip->dma8 < 0 || chip->dma16 < 0) return 0; switch (chip->force_mode16) { case SB_MODE_PLAYBACK_16: return 1; case SB_MODE_CAPTURE_16: return 2; default: return 0; } } static int snd_sb16_dma_control_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[3] = { "Auto", "Playback", "Capture" }; return snd_ctl_enum_info(uinfo, 1, 3, texts); } static int snd_sb16_dma_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.enumerated.item[0] = snd_sb16_get_dma_mode(chip); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_sb16_dma_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned char nval, oval; int change; nval = ucontrol->value.enumerated.item[0]; if (nval > 2) return -EINVAL; spin_lock_irqsave(&chip->reg_lock, flags); oval = snd_sb16_get_dma_mode(chip); change = nval != oval; snd_sb16_set_dma_mode(chip, nval); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } static const struct snd_kcontrol_new snd_sb16_dma_control = { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = "16-bit DMA Allocation", .info = snd_sb16_dma_control_info, .get = snd_sb16_dma_control_get, .put = snd_sb16_dma_control_put }; /* * Initialization part */ int snd_sb16dsp_configure(struct snd_sb * chip) { unsigned long flags; unsigned char irqreg = 0, dmareg = 0, mpureg; unsigned char realirq, realdma, realmpureg; /* note: mpu register should be present only on SB16 Vibra soundcards */ // printk(KERN_DEBUG "codec->irq=%i, codec->dma8=%i, codec->dma16=%i\n", chip->irq, chip->dma8, chip->dma16); spin_lock_irqsave(&chip->mixer_lock, flags); mpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP) & ~0x06; spin_unlock_irqrestore(&chip->mixer_lock, flags); switch (chip->irq) { case 2: case 9: irqreg |= SB_IRQSETUP_IRQ9; break; case 5: irqreg |= SB_IRQSETUP_IRQ5; break; case 7: irqreg |= SB_IRQSETUP_IRQ7; break; case 10: irqreg |= SB_IRQSETUP_IRQ10; break; default: return -EINVAL; } if (chip->dma8 >= 0) { switch (chip->dma8) { case 0: dmareg |= SB_DMASETUP_DMA0; break; case 1: dmareg |= SB_DMASETUP_DMA1; break; case 3: dmareg |= SB_DMASETUP_DMA3; break; default: return -EINVAL; } } if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) { switch (chip->dma16) { case 5: dmareg |= SB_DMASETUP_DMA5; break; case 6: dmareg |= SB_DMASETUP_DMA6; break; case 7: dmareg |= SB_DMASETUP_DMA7; break; default: return -EINVAL; } } switch (chip->mpu_port) { case 0x300: mpureg |= 0x04; break; case 0x330: mpureg |= 0x00; break; default: mpureg |= 0x02; /* disable MPU */ } spin_lock_irqsave(&chip->mixer_lock, flags); snd_sbmixer_write(chip, SB_DSP4_IRQSETUP, irqreg); realirq = snd_sbmixer_read(chip, SB_DSP4_IRQSETUP); snd_sbmixer_write(chip, SB_DSP4_DMASETUP, dmareg); realdma = snd_sbmixer_read(chip, SB_DSP4_DMASETUP); snd_sbmixer_write(chip, SB_DSP4_MPUSETUP, mpureg); realmpureg = snd_sbmixer_read(chip, SB_DSP4_MPUSETUP); spin_unlock_irqrestore(&chip->mixer_lock, flags); if ((~realirq) & irqreg || (~realdma) & dmareg) { snd_printk(KERN_ERR "SB16 [0x%lx]: unable to set DMA & IRQ (PnP device?)\n", chip->port); snd_printk(KERN_ERR "SB16 [0x%lx]: wanted: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, realirq, realdma, realmpureg); snd_printk(KERN_ERR "SB16 [0x%lx]: got: irqreg=0x%x, dmareg=0x%x, mpureg = 0x%x\n", chip->port, irqreg, dmareg, mpureg); return -ENODEV; } return 0; } static const struct snd_pcm_ops snd_sb16_playback_ops = { .open = snd_sb16_playback_open, .close = snd_sb16_playback_close, .prepare = snd_sb16_playback_prepare, .trigger = snd_sb16_playback_trigger, .pointer = snd_sb16_playback_pointer, }; static const struct snd_pcm_ops snd_sb16_capture_ops = { .open = snd_sb16_capture_open, .close = snd_sb16_capture_close, .prepare = snd_sb16_capture_prepare, .trigger = snd_sb16_capture_trigger, .pointer = snd_sb16_capture_pointer, }; int snd_sb16dsp_pcm(struct snd_sb *chip, int device) { struct snd_card *card = chip->card; struct snd_pcm *pcm; int err; err = snd_pcm_new(card, "SB16 DSP", device, 1, 1, &pcm); if (err < 0) return err; sprintf(pcm->name, "DSP v%i.%i", chip->version >> 8, chip->version & 0xff); pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX; pcm->private_data = chip; chip->pcm = pcm; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sb16_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sb16_capture_ops); if (chip->dma16 >= 0 && chip->dma8 != chip->dma16) snd_ctl_add(card, snd_ctl_new1(&snd_sb16_dma_control, chip)); else pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX; snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, 128*1024); return 0; } const struct snd_pcm_ops *snd_sb16dsp_get_pcm_ops(int direction) { return direction == SNDRV_PCM_STREAM_PLAYBACK ? &snd_sb16_playback_ops : &snd_sb16_capture_ops; } EXPORT_SYMBOL(snd_sb16dsp_pcm); EXPORT_SYMBOL(snd_sb16dsp_get_pcm_ops); EXPORT_SYMBOL(snd_sb16dsp_configure); EXPORT_SYMBOL(snd_sb16dsp_interrupt);
linux-master
sound/isa/sb/sb16_main.c
#define SNDRV_SBAWE #include "sb16.c"
linux-master
sound/isa/sb/sbawe.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * synth callback routines for the emu8000 (AWE32/64) * * Copyright (C) 1999 Steve Ratcliffe * Copyright (C) 1999-2000 Takashi Iwai <[email protected]> */ #include "emu8000_local.h" #include <linux/export.h> #include <sound/asoundef.h> /* * prototypes */ static struct snd_emux_voice *get_voice(struct snd_emux *emu, struct snd_emux_port *port); static int start_voice(struct snd_emux_voice *vp); static void trigger_voice(struct snd_emux_voice *vp); static void release_voice(struct snd_emux_voice *vp); static void update_voice(struct snd_emux_voice *vp, int update); static void reset_voice(struct snd_emux *emu, int ch); static void terminate_voice(struct snd_emux_voice *vp); static void sysex(struct snd_emux *emu, char *buf, int len, int parsed, struct snd_midi_channel_set *chset); #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS) static int oss_ioctl(struct snd_emux *emu, int cmd, int p1, int p2); #endif static int load_fx(struct snd_emux *emu, int type, int mode, const void __user *buf, long len); static void set_pitch(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void set_volume(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void set_pan(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void set_fmmod(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void set_tremfreq(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void set_fm2frq2(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void set_filterQ(struct snd_emu8000 *hw, struct snd_emux_voice *vp); static void snd_emu8000_tweak_voice(struct snd_emu8000 *emu, int ch); /* * Ensure a value is between two points * macro evaluates its args more than once, so changed to upper-case. */ #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0) #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0) /* * set up operators */ static const struct snd_emux_operators emu8000_ops = { .owner = THIS_MODULE, .get_voice = get_voice, .prepare = start_voice, .trigger = trigger_voice, .release = release_voice, .update = update_voice, .terminate = terminate_voice, .reset = reset_voice, .sample_new = snd_emu8000_sample_new, .sample_free = snd_emu8000_sample_free, .sample_reset = snd_emu8000_sample_reset, .load_fx = load_fx, .sysex = sysex, #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS) .oss_ioctl = oss_ioctl, #endif }; void snd_emu8000_ops_setup(struct snd_emu8000 *hw) { hw->emu->ops = emu8000_ops; } /* * Terminate a voice */ static void release_voice(struct snd_emux_voice *vp) { int dcysusv; struct snd_emu8000 *hw; hw = vp->hw; dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease; EMU8000_DCYSUS_WRITE(hw, vp->ch, dcysusv); dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease; EMU8000_DCYSUSV_WRITE(hw, vp->ch, dcysusv); } /* */ static void terminate_voice(struct snd_emux_voice *vp) { struct snd_emu8000 *hw; hw = vp->hw; EMU8000_DCYSUSV_WRITE(hw, vp->ch, 0x807F); } /* */ static void update_voice(struct snd_emux_voice *vp, int update) { struct snd_emu8000 *hw; hw = vp->hw; if (update & SNDRV_EMUX_UPDATE_VOLUME) set_volume(hw, vp); if (update & SNDRV_EMUX_UPDATE_PITCH) set_pitch(hw, vp); if ((update & SNDRV_EMUX_UPDATE_PAN) && vp->port->ctrls[EMUX_MD_REALTIME_PAN]) set_pan(hw, vp); if (update & SNDRV_EMUX_UPDATE_FMMOD) set_fmmod(hw, vp); if (update & SNDRV_EMUX_UPDATE_TREMFREQ) set_tremfreq(hw, vp); if (update & SNDRV_EMUX_UPDATE_FM2FRQ2) set_fm2frq2(hw, vp); if (update & SNDRV_EMUX_UPDATE_Q) set_filterQ(hw, vp); } /* * Find a channel (voice) within the EMU that is not in use or at least * less in use than other channels. Always returns a valid pointer * no matter what. If there is a real shortage of voices then one * will be cut. Such is life. * * The channel index (vp->ch) must be initialized in this routine. * In Emu8k, it is identical with the array index. */ static struct snd_emux_voice * get_voice(struct snd_emux *emu, struct snd_emux_port *port) { int i; struct snd_emux_voice *vp; struct snd_emu8000 *hw; /* what we are looking for, in order of preference */ enum { OFF=0, RELEASED, PLAYING, END }; /* Keeps track of what we are finding */ struct best { unsigned int time; int voice; } best[END]; struct best *bp; hw = emu->hw; for (i = 0; i < END; i++) { best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */ best[i].voice = -1; } /* * Go through them all and get a best one to use. */ for (i = 0; i < emu->max_voices; i++) { int state, val; vp = &emu->voices[i]; state = vp->state; if (state == SNDRV_EMUX_ST_OFF) bp = best + OFF; else if (state == SNDRV_EMUX_ST_RELEASED || state == SNDRV_EMUX_ST_PENDING) { bp = best + RELEASED; val = (EMU8000_CVCF_READ(hw, vp->ch) >> 16) & 0xffff; if (! val) bp = best + OFF; } else if (state & SNDRV_EMUX_ST_ON) bp = best + PLAYING; else continue; /* check if sample is finished playing (non-looping only) */ if (state != SNDRV_EMUX_ST_OFF && (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) { val = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff; if (val >= vp->reg.loopstart) bp = best + OFF; } if (vp->time < bp->time) { bp->time = vp->time; bp->voice = i; } } for (i = 0; i < END; i++) { if (best[i].voice >= 0) { vp = &emu->voices[best[i].voice]; vp->ch = best[i].voice; return vp; } } /* not found */ return NULL; } /* */ static int start_voice(struct snd_emux_voice *vp) { unsigned int temp; int ch; int addr; struct snd_midi_channel *chan; struct snd_emu8000 *hw; hw = vp->hw; ch = vp->ch; chan = vp->chan; /* channel to be silent and idle */ EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080); EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF); EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF); EMU8000_PTRX_WRITE(hw, ch, 0); EMU8000_CPF_WRITE(hw, ch, 0); /* set pitch offset */ set_pitch(hw, vp); /* set envelope parameters */ EMU8000_ENVVAL_WRITE(hw, ch, vp->reg.parm.moddelay); EMU8000_ATKHLD_WRITE(hw, ch, vp->reg.parm.modatkhld); EMU8000_DCYSUS_WRITE(hw, ch, vp->reg.parm.moddcysus); EMU8000_ENVVOL_WRITE(hw, ch, vp->reg.parm.voldelay); EMU8000_ATKHLDV_WRITE(hw, ch, vp->reg.parm.volatkhld); /* decay/sustain parameter for volume envelope is used for triggerg the voice */ /* cutoff and volume */ set_volume(hw, vp); /* modulation envelope heights */ EMU8000_PEFE_WRITE(hw, ch, vp->reg.parm.pefe); /* lfo1/2 delay */ EMU8000_LFO1VAL_WRITE(hw, ch, vp->reg.parm.lfo1delay); EMU8000_LFO2VAL_WRITE(hw, ch, vp->reg.parm.lfo2delay); /* lfo1 pitch & cutoff shift */ set_fmmod(hw, vp); /* lfo1 volume & freq */ set_tremfreq(hw, vp); /* lfo2 pitch & freq */ set_fm2frq2(hw, vp); /* pan & loop start */ set_pan(hw, vp); /* chorus & loop end (chorus 8bit, MSB) */ addr = vp->reg.loopend - 1; temp = vp->reg.parm.chorus; temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10; LIMITMAX(temp, 255); temp = (temp <<24) | (unsigned int)addr; EMU8000_CSL_WRITE(hw, ch, temp); /* Q & current address (Q 4bit value, MSB) */ addr = vp->reg.start - 1; temp = vp->reg.parm.filterQ; temp = (temp<<28) | (unsigned int)addr; EMU8000_CCCA_WRITE(hw, ch, temp); /* clear unknown registers */ EMU8000_00A0_WRITE(hw, ch, 0); EMU8000_0080_WRITE(hw, ch, 0); /* reset volume */ temp = vp->vtarget << 16; EMU8000_VTFT_WRITE(hw, ch, temp | vp->ftarget); EMU8000_CVCF_WRITE(hw, ch, temp | 0xff00); return 0; } /* * Start envelope */ static void trigger_voice(struct snd_emux_voice *vp) { int ch = vp->ch; unsigned int temp; struct snd_emu8000 *hw; hw = vp->hw; /* set reverb and pitch target */ temp = vp->reg.parm.reverb; temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10; LIMITMAX(temp, 255); temp = (temp << 8) | (vp->ptarget << 16) | vp->aaux; EMU8000_PTRX_WRITE(hw, ch, temp); EMU8000_CPF_WRITE(hw, ch, vp->ptarget << 16); EMU8000_DCYSUSV_WRITE(hw, ch, vp->reg.parm.voldcysus); } /* * reset voice parameters */ static void reset_voice(struct snd_emux *emu, int ch) { struct snd_emu8000 *hw; hw = emu->hw; EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F); snd_emu8000_tweak_voice(hw, ch); } /* * Set the pitch of a possibly playing note. */ static void set_pitch(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { EMU8000_IP_WRITE(hw, vp->ch, vp->apitch); } /* * Set the volume of a possibly already playing note */ static void set_volume(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { int ifatn; ifatn = (unsigned char)vp->acutoff; ifatn = (ifatn << 8); ifatn |= (unsigned char)vp->avol; EMU8000_IFATN_WRITE(hw, vp->ch, ifatn); } /* * Set pan and loop start address. */ static void set_pan(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { unsigned int temp; temp = ((unsigned int)vp->apan<<24) | ((unsigned int)vp->reg.loopstart - 1); EMU8000_PSST_WRITE(hw, vp->ch, temp); } #define MOD_SENSE 18 static void set_fmmod(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { unsigned short fmmod; short pitch; unsigned char cutoff; int modulation; pitch = (char)(vp->reg.parm.fmmod>>8); cutoff = (vp->reg.parm.fmmod & 0xff); modulation = vp->chan->gm_modulation + vp->chan->midi_pressure; pitch += (MOD_SENSE * modulation) / 1200; LIMITVALUE(pitch, -128, 127); fmmod = ((unsigned char)pitch<<8) | cutoff; EMU8000_FMMOD_WRITE(hw, vp->ch, fmmod); } /* set tremolo (lfo1) volume & frequency */ static void set_tremfreq(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { EMU8000_TREMFRQ_WRITE(hw, vp->ch, vp->reg.parm.tremfrq); } /* set lfo2 pitch & frequency */ static void set_fm2frq2(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { unsigned short fm2frq2; short pitch; unsigned char freq; int modulation; pitch = (char)(vp->reg.parm.fm2frq2>>8); freq = vp->reg.parm.fm2frq2 & 0xff; modulation = vp->chan->gm_modulation + vp->chan->midi_pressure; pitch += (MOD_SENSE * modulation) / 1200; LIMITVALUE(pitch, -128, 127); fm2frq2 = ((unsigned char)pitch<<8) | freq; EMU8000_FM2FRQ2_WRITE(hw, vp->ch, fm2frq2); } /* set filterQ */ static void set_filterQ(struct snd_emu8000 *hw, struct snd_emux_voice *vp) { unsigned int addr; addr = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff; addr |= (vp->reg.parm.filterQ << 28); EMU8000_CCCA_WRITE(hw, vp->ch, addr); } /* * set the envelope & LFO parameters to the default values */ static void snd_emu8000_tweak_voice(struct snd_emu8000 *emu, int i) { /* set all mod/vol envelope shape to minimum */ EMU8000_ENVVOL_WRITE(emu, i, 0x8000); EMU8000_ENVVAL_WRITE(emu, i, 0x8000); EMU8000_DCYSUS_WRITE(emu, i, 0x7F7F); EMU8000_ATKHLDV_WRITE(emu, i, 0x7F7F); EMU8000_ATKHLD_WRITE(emu, i, 0x7F7F); EMU8000_PEFE_WRITE(emu, i, 0); /* mod envelope height to zero */ EMU8000_LFO1VAL_WRITE(emu, i, 0x8000); /* no delay for LFO1 */ EMU8000_LFO2VAL_WRITE(emu, i, 0x8000); EMU8000_IP_WRITE(emu, i, 0xE000); /* no pitch shift */ EMU8000_IFATN_WRITE(emu, i, 0xFF00); /* volume to minimum */ EMU8000_FMMOD_WRITE(emu, i, 0); EMU8000_TREMFRQ_WRITE(emu, i, 0); EMU8000_FM2FRQ2_WRITE(emu, i, 0); } /* * sysex callback */ static void sysex(struct snd_emux *emu, char *buf, int len, int parsed, struct snd_midi_channel_set *chset) { struct snd_emu8000 *hw; hw = emu->hw; switch (parsed) { case SNDRV_MIDI_SYSEX_GS_CHORUS_MODE: hw->chorus_mode = chset->gs_chorus_mode; snd_emu8000_update_chorus_mode(hw); break; case SNDRV_MIDI_SYSEX_GS_REVERB_MODE: hw->reverb_mode = chset->gs_reverb_mode; snd_emu8000_update_reverb_mode(hw); break; } } #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS) /* * OSS ioctl callback */ static int oss_ioctl(struct snd_emux *emu, int cmd, int p1, int p2) { struct snd_emu8000 *hw; hw = emu->hw; switch (cmd) { case _EMUX_OSS_REVERB_MODE: hw->reverb_mode = p1; snd_emu8000_update_reverb_mode(hw); break; case _EMUX_OSS_CHORUS_MODE: hw->chorus_mode = p1; snd_emu8000_update_chorus_mode(hw); break; case _EMUX_OSS_INITIALIZE_CHIP: /* snd_emu8000_init(hw); */ /*ignored*/ break; case _EMUX_OSS_EQUALIZER: hw->bass_level = p1; hw->treble_level = p2; snd_emu8000_update_equalizer(hw); break; } return 0; } #endif /* * additional patch keys */ #define SNDRV_EMU8000_LOAD_CHORUS_FX 0x10 /* optarg=mode */ #define SNDRV_EMU8000_LOAD_REVERB_FX 0x11 /* optarg=mode */ /* * callback routine */ static int load_fx(struct snd_emux *emu, int type, int mode, const void __user *buf, long len) { struct snd_emu8000 *hw; hw = emu->hw; /* skip header */ buf += 16; len -= 16; switch (type) { case SNDRV_EMU8000_LOAD_CHORUS_FX: return snd_emu8000_load_chorus_fx(hw, mode, buf, len); case SNDRV_EMU8000_LOAD_REVERB_FX: return snd_emu8000_load_reverb_fx(hw, mode, buf, len); } return -EINVAL; }
linux-master
sound/isa/sb/emu8000_callback.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 1999 by Uros Bizjak <[email protected]> * Takashi Iwai <[email protected]> * * SB16ASP/AWE32 CSP control * * CSP microcode loader: * alsa-tools/sb16_csp/ */ #include <linux/delay.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/module.h> #include <sound/core.h> #include <sound/control.h> #include <sound/info.h> #include <sound/sb16_csp.h> #include <sound/initval.h> MODULE_AUTHOR("Uros Bizjak <[email protected]>"); MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor"); MODULE_LICENSE("GPL"); MODULE_FIRMWARE("sb16/mulaw_main.csp"); MODULE_FIRMWARE("sb16/alaw_main.csp"); MODULE_FIRMWARE("sb16/ima_adpcm_init.csp"); MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp"); MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp"); #ifdef SNDRV_LITTLE_ENDIAN #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24)) #else #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24)) #endif #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F') #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ') #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T') #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c') #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e') #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't') #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n') /* * RIFF data format */ struct riff_header { __le32 name; __le32 len; }; struct desc_header { struct riff_header info; __le16 func_nr; __le16 VOC_type; __le16 flags_play_rec; __le16 flags_16bit_8bit; __le16 flags_stereo_mono; __le16 flags_rates; }; /* * prototypes */ static void snd_sb_csp_free(struct snd_hwdep *hw); static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file); static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg); static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file); static int csp_detect(struct snd_sb *chip, int *version); static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val); static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val); static int read_register(struct snd_sb *chip, unsigned char reg); static int set_mode_register(struct snd_sb *chip, unsigned char mode); static int get_version(struct snd_sb *chip); static int snd_sb_csp_riff_load(struct snd_sb_csp * p, struct snd_sb_csp_microcode __user * code); static int snd_sb_csp_unload(struct snd_sb_csp * p); static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags); static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode); static int snd_sb_csp_check_version(struct snd_sb_csp * p); static int snd_sb_csp_use(struct snd_sb_csp * p); static int snd_sb_csp_unuse(struct snd_sb_csp * p); static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels); static int snd_sb_csp_stop(struct snd_sb_csp * p); static int snd_sb_csp_pause(struct snd_sb_csp * p); static int snd_sb_csp_restart(struct snd_sb_csp * p); static int snd_sb_qsound_build(struct snd_sb_csp * p); static void snd_sb_qsound_destroy(struct snd_sb_csp * p); static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p); static int init_proc_entry(struct snd_sb_csp * p, int device); static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer); /* * Detect CSP chip and create a new instance */ int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep) { struct snd_sb_csp *p; int version; int err; struct snd_hwdep *hw; if (rhwdep) *rhwdep = NULL; if (csp_detect(chip, &version)) return -ENODEV; err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw); if (err < 0) return err; p = kzalloc(sizeof(*p), GFP_KERNEL); if (!p) { snd_device_free(chip->card, hw); return -ENOMEM; } p->chip = chip; p->version = version; /* CSP operators */ p->ops.csp_use = snd_sb_csp_use; p->ops.csp_unuse = snd_sb_csp_unuse; p->ops.csp_autoload = snd_sb_csp_autoload; p->ops.csp_start = snd_sb_csp_start; p->ops.csp_stop = snd_sb_csp_stop; p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer; mutex_init(&p->access_mutex); sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f)); hw->iface = SNDRV_HWDEP_IFACE_SB16CSP; hw->private_data = p; hw->private_free = snd_sb_csp_free; /* operators - only write/ioctl */ hw->ops.open = snd_sb_csp_open; hw->ops.ioctl = snd_sb_csp_ioctl; hw->ops.release = snd_sb_csp_release; /* create a proc entry */ init_proc_entry(p, device); if (rhwdep) *rhwdep = hw; return 0; } /* * free_private for hwdep instance */ static void snd_sb_csp_free(struct snd_hwdep *hwdep) { int i; struct snd_sb_csp *p = hwdep->private_data; if (p) { if (p->running & SNDRV_SB_CSP_ST_RUNNING) snd_sb_csp_stop(p); for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i) release_firmware(p->csp_programs[i]); kfree(p); } } /* ------------------------------ */ /* * open the device exclusively */ static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file) { struct snd_sb_csp *p = hw->private_data; return (snd_sb_csp_use(p)); } /* * ioctl for hwdep device: */ static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg) { struct snd_sb_csp *p = hw->private_data; struct snd_sb_csp_info info; struct snd_sb_csp_start start_info; int err; if (snd_BUG_ON(!p)) return -EINVAL; if (snd_sb_csp_check_version(p)) return -ENODEV; switch (cmd) { /* get information */ case SNDRV_SB_CSP_IOCTL_INFO: memset(&info, 0, sizeof(info)); *info.codec_name = *p->codec_name; info.func_nr = p->func_nr; info.acc_format = p->acc_format; info.acc_channels = p->acc_channels; info.acc_width = p->acc_width; info.acc_rates = p->acc_rates; info.csp_mode = p->mode; info.run_channels = p->run_channels; info.run_width = p->run_width; info.version = p->version; info.state = p->running; if (copy_to_user((void __user *)arg, &info, sizeof(info))) err = -EFAULT; else err = 0; break; /* load CSP microcode */ case SNDRV_SB_CSP_IOCTL_LOAD_CODE: err = (p->running & SNDRV_SB_CSP_ST_RUNNING ? -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg)); break; case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE: err = (p->running & SNDRV_SB_CSP_ST_RUNNING ? -EBUSY : snd_sb_csp_unload(p)); break; /* change CSP running state */ case SNDRV_SB_CSP_IOCTL_START: if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info))) err = -EFAULT; else err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels); break; case SNDRV_SB_CSP_IOCTL_STOP: err = snd_sb_csp_stop(p); break; case SNDRV_SB_CSP_IOCTL_PAUSE: err = snd_sb_csp_pause(p); break; case SNDRV_SB_CSP_IOCTL_RESTART: err = snd_sb_csp_restart(p); break; default: err = -ENOTTY; break; } return err; } /* * close the device */ static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file) { struct snd_sb_csp *p = hw->private_data; return (snd_sb_csp_unuse(p)); } /* ------------------------------ */ /* * acquire device */ static int snd_sb_csp_use(struct snd_sb_csp * p) { mutex_lock(&p->access_mutex); if (p->used) { mutex_unlock(&p->access_mutex); return -EAGAIN; } p->used++; mutex_unlock(&p->access_mutex); return 0; } /* * release device */ static int snd_sb_csp_unuse(struct snd_sb_csp * p) { mutex_lock(&p->access_mutex); p->used--; mutex_unlock(&p->access_mutex); return 0; } /* * load microcode via ioctl: * code is user-space pointer */ static int snd_sb_csp_riff_load(struct snd_sb_csp * p, struct snd_sb_csp_microcode __user * mcode) { struct snd_sb_csp_mc_header info; unsigned char __user *data_ptr; unsigned char __user *data_end; unsigned short func_nr = 0; struct riff_header file_h, item_h, code_h; __le32 item_type; struct desc_header funcdesc_h; unsigned long flags; int err; if (copy_from_user(&info, mcode, sizeof(info))) return -EFAULT; data_ptr = mcode->data; if (copy_from_user(&file_h, data_ptr, sizeof(file_h))) return -EFAULT; if ((le32_to_cpu(file_h.name) != RIFF_HEADER) || (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) { snd_printd("%s: Invalid RIFF header\n", __func__); return -EINVAL; } data_ptr += sizeof(file_h); data_end = data_ptr + le32_to_cpu(file_h.len); if (copy_from_user(&item_type, data_ptr, sizeof(item_type))) return -EFAULT; if (le32_to_cpu(item_type) != CSP__HEADER) { snd_printd("%s: Invalid RIFF file type\n", __func__); return -EINVAL; } data_ptr += sizeof (item_type); for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) { if (copy_from_user(&item_h, data_ptr, sizeof(item_h))) return -EFAULT; data_ptr += sizeof(item_h); if (le32_to_cpu(item_h.name) != LIST_HEADER) continue; if (copy_from_user(&item_type, data_ptr, sizeof(item_type))) return -EFAULT; switch (le32_to_cpu(item_type)) { case FUNC_HEADER: if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h))) return -EFAULT; func_nr = le16_to_cpu(funcdesc_h.func_nr); break; case CODE_HEADER: if (func_nr != info.func_req) break; /* not required function, try next */ data_ptr += sizeof(item_type); /* destroy QSound mixer element */ if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) { snd_sb_qsound_destroy(p); } /* Clear all flags */ p->running = 0; p->mode = 0; /* load microcode blocks */ for (;;) { if (data_ptr >= data_end) return -EINVAL; if (copy_from_user(&code_h, data_ptr, sizeof(code_h))) return -EFAULT; /* init microcode blocks */ if (le32_to_cpu(code_h.name) != INIT_HEADER) break; data_ptr += sizeof(code_h); err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len), SNDRV_SB_CSP_LOAD_INITBLOCK); if (err) return err; data_ptr += le32_to_cpu(code_h.len); } /* main microcode block */ if (copy_from_user(&code_h, data_ptr, sizeof(code_h))) return -EFAULT; if (le32_to_cpu(code_h.name) != MAIN_HEADER) { snd_printd("%s: Missing 'main' microcode\n", __func__); return -EINVAL; } data_ptr += sizeof(code_h); err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len), 0); if (err) return err; /* fill in codec header */ strscpy(p->codec_name, info.codec_name, sizeof(p->codec_name)); p->func_nr = func_nr; p->mode = le16_to_cpu(funcdesc_h.flags_play_rec); switch (le16_to_cpu(funcdesc_h.VOC_type)) { case 0x0001: /* QSound decoder */ if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) { if (snd_sb_qsound_build(p) == 0) /* set QSound flag and clear all other mode flags */ p->mode = SNDRV_SB_CSP_MODE_QSOUND; } p->acc_format = 0; break; case 0x0006: /* A Law codec */ p->acc_format = SNDRV_PCM_FMTBIT_A_LAW; break; case 0x0007: /* Mu Law codec */ p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW; break; case 0x0011: /* what Creative thinks is IMA ADPCM codec */ case 0x0200: /* Creative ADPCM codec */ p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM; break; case 201: /* Text 2 Speech decoder */ /* TODO: Text2Speech handling routines */ p->acc_format = 0; break; case 0x0202: /* Fast Speech 8 codec */ case 0x0203: /* Fast Speech 10 codec */ p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL; break; default: /* other codecs are unsupported */ p->acc_format = p->acc_width = p->acc_rates = 0; p->mode = 0; snd_printd("%s: Unsupported CSP codec type: 0x%04x\n", __func__, le16_to_cpu(funcdesc_h.VOC_type)); return -EINVAL; } p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono); p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit); p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates); /* Decouple CSP from IRQ and DMAREQ lines */ spin_lock_irqsave(&p->chip->reg_lock, flags); set_mode_register(p->chip, 0xfc); set_mode_register(p->chip, 0x00); spin_unlock_irqrestore(&p->chip->reg_lock, flags); /* finished loading successfully */ p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */ return 0; } } snd_printd("%s: Function #%d not found\n", __func__, info.func_req); return -EINVAL; } /* * unload CSP microcode */ static int snd_sb_csp_unload(struct snd_sb_csp * p) { if (p->running & SNDRV_SB_CSP_ST_RUNNING) return -EBUSY; if (!(p->running & SNDRV_SB_CSP_ST_LOADED)) return -ENXIO; /* clear supported formats */ p->acc_format = 0; p->acc_channels = p->acc_width = p->acc_rates = 0; /* destroy QSound mixer element */ if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) { snd_sb_qsound_destroy(p); } /* clear all flags */ p->running = 0; p->mode = 0; return 0; } /* * send command sequence to DSP */ static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size) { int i; for (i = 0; i < size; i++) { if (!snd_sbdsp_command(chip, seq[i])) return -EIO; } return 0; } /* * set CSP codec parameter */ static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val) { unsigned char dsp_cmd[3]; dsp_cmd[0] = 0x05; /* CSP set codec parameter */ dsp_cmd[1] = val; /* Parameter value */ dsp_cmd[2] = par; /* Parameter */ command_seq(chip, dsp_cmd, 3); snd_sbdsp_command(chip, 0x03); /* DSP read? */ if (snd_sbdsp_get_byte(chip) != par) return -EIO; return 0; } /* * set CSP register */ static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val) { unsigned char dsp_cmd[3]; dsp_cmd[0] = 0x0e; /* CSP set register */ dsp_cmd[1] = reg; /* CSP Register */ dsp_cmd[2] = val; /* value */ return command_seq(chip, dsp_cmd, 3); } /* * read CSP register * return < 0 -> error */ static int read_register(struct snd_sb *chip, unsigned char reg) { unsigned char dsp_cmd[2]; dsp_cmd[0] = 0x0f; /* CSP read register */ dsp_cmd[1] = reg; /* CSP Register */ command_seq(chip, dsp_cmd, 2); return snd_sbdsp_get_byte(chip); /* Read DSP value */ } /* * set CSP mode register */ static int set_mode_register(struct snd_sb *chip, unsigned char mode) { unsigned char dsp_cmd[2]; dsp_cmd[0] = 0x04; /* CSP set mode register */ dsp_cmd[1] = mode; /* mode */ return command_seq(chip, dsp_cmd, 2); } /* * Detect CSP * return 0 if CSP exists. */ static int csp_detect(struct snd_sb *chip, int *version) { unsigned char csp_test1, csp_test2; unsigned long flags; int result = -ENODEV; spin_lock_irqsave(&chip->reg_lock, flags); set_codec_parameter(chip, 0x00, 0x00); set_mode_register(chip, 0xfc); /* 0xfc = ?? */ csp_test1 = read_register(chip, 0x83); set_register(chip, 0x83, ~csp_test1); csp_test2 = read_register(chip, 0x83); if (csp_test2 != (csp_test1 ^ 0xff)) goto __fail; set_register(chip, 0x83, csp_test1); csp_test2 = read_register(chip, 0x83); if (csp_test2 != csp_test1) goto __fail; set_mode_register(chip, 0x00); /* 0x00 = ? */ *version = get_version(chip); snd_sbdsp_reset(chip); /* reset DSP after getversion! */ if (*version >= 0x10 && *version <= 0x1f) result = 0; /* valid version id */ __fail: spin_unlock_irqrestore(&chip->reg_lock, flags); return result; } /* * get CSP version number */ static int get_version(struct snd_sb *chip) { unsigned char dsp_cmd[2]; dsp_cmd[0] = 0x08; /* SB_DSP_!something! */ dsp_cmd[1] = 0x03; /* get chip version id? */ command_seq(chip, dsp_cmd, 2); return (snd_sbdsp_get_byte(chip)); } /* * check if the CSP version is valid */ static int snd_sb_csp_check_version(struct snd_sb_csp * p) { if (p->version < 0x10 || p->version > 0x1f) { snd_printd("%s: Invalid CSP version: 0x%x\n", __func__, p->version); return 1; } return 0; } /* * download microcode to CSP (microcode should have one "main" block). */ static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags) { int status, i; int err; int result = -EIO; unsigned long flags; spin_lock_irqsave(&p->chip->reg_lock, flags); snd_sbdsp_command(p->chip, 0x01); /* CSP download command */ if (snd_sbdsp_get_byte(p->chip)) { snd_printd("%s: Download command failed\n", __func__); goto __fail; } /* Send CSP low byte (size - 1) */ snd_sbdsp_command(p->chip, (unsigned char)(size - 1)); /* Send high byte */ snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8)); /* send microcode sequence */ /* load from kernel space */ while (size--) { if (!snd_sbdsp_command(p->chip, *buf++)) goto __fail; } if (snd_sbdsp_get_byte(p->chip)) goto __fail; if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) { i = 0; /* some codecs (FastSpeech) take some time to initialize */ while (1) { snd_sbdsp_command(p->chip, 0x03); status = snd_sbdsp_get_byte(p->chip); if (status == 0x55 || ++i >= 10) break; udelay (10); } if (status != 0x55) { snd_printd("%s: Microcode initialization failed\n", __func__); goto __fail; } } else { /* * Read mixer register SB_DSP4_DMASETUP after loading 'main' code. * Start CSP chip if no 16bit DMA channel is set - some kind * of autorun or perhaps a bugfix? */ spin_lock(&p->chip->mixer_lock); status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP); spin_unlock(&p->chip->mixer_lock); if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) { err = (set_codec_parameter(p->chip, 0xaa, 0x00) || set_codec_parameter(p->chip, 0xff, 0x00)); snd_sbdsp_reset(p->chip); /* really! */ if (err) goto __fail; set_mode_register(p->chip, 0xc0); /* c0 = STOP */ set_mode_register(p->chip, 0x70); /* 70 = RUN */ } } result = 0; __fail: spin_unlock_irqrestore(&p->chip->reg_lock, flags); return result; } static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags) { int err; unsigned char *kbuf; kbuf = memdup_user(buf, size); if (IS_ERR(kbuf)) return PTR_ERR(kbuf); err = snd_sb_csp_load(p, kbuf, size, load_flags); kfree(kbuf); return err; } static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags) { static const char *const names[] = { "sb16/mulaw_main.csp", "sb16/alaw_main.csp", "sb16/ima_adpcm_init.csp", "sb16/ima_adpcm_playback.csp", "sb16/ima_adpcm_capture.csp", }; const struct firmware *program; BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT); program = p->csp_programs[index]; if (!program) { int err = request_firmware(&program, names[index], p->chip->card->dev); if (err < 0) return err; p->csp_programs[index] = program; } return snd_sb_csp_load(p, program->data, program->size, flags); } /* * autoload hardware codec if necessary * return 0 if CSP is loaded and ready to run (p->running != 0) */ static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode) { unsigned long flags; int err = 0; /* if CSP is running or manually loaded then exit */ if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED)) return -EBUSY; /* autoload microcode only if requested hardware codec is not already loaded */ if (((1U << (__force int)pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) { p->running = SNDRV_SB_CSP_ST_AUTO; } else { switch (pcm_sfmt) { case SNDRV_PCM_FORMAT_MU_LAW: err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0); p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW; p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE; break; case SNDRV_PCM_FORMAT_A_LAW: err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0); p->acc_format = SNDRV_PCM_FMTBIT_A_LAW; p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE; break; case SNDRV_PCM_FORMAT_IMA_ADPCM: err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT, SNDRV_SB_CSP_LOAD_INITBLOCK); if (err) break; if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) { err = snd_sb_csp_firmware_load (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0); p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE; } else { err = snd_sb_csp_firmware_load (p, CSP_PROGRAM_ADPCM_CAPTURE, 0); p->mode = SNDRV_SB_CSP_MODE_DSP_READ; } p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM; break; default: /* Decouple CSP from IRQ and DMAREQ lines */ if (p->running & SNDRV_SB_CSP_ST_AUTO) { spin_lock_irqsave(&p->chip->reg_lock, flags); set_mode_register(p->chip, 0xfc); set_mode_register(p->chip, 0x00); spin_unlock_irqrestore(&p->chip->reg_lock, flags); p->running = 0; /* clear autoloaded flag */ } return -EINVAL; } if (err) { p->acc_format = 0; p->acc_channels = p->acc_width = p->acc_rates = 0; p->running = 0; /* clear autoloaded flag */ p->mode = 0; return (err); } else { p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */ p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */ p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO; p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */ } } return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO; } /* * start CSP */ static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels) { unsigned char s_type; /* sample type */ unsigned char mixL, mixR; int result = -EIO; unsigned long flags; if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) { snd_printd("%s: Microcode not loaded\n", __func__); return -ENXIO; } if (p->running & SNDRV_SB_CSP_ST_RUNNING) { snd_printd("%s: CSP already running\n", __func__); return -EBUSY; } if (!(sample_width & p->acc_width)) { snd_printd("%s: Unsupported PCM sample width\n", __func__); return -EINVAL; } if (!(channels & p->acc_channels)) { snd_printd("%s: Invalid number of channels\n", __func__); return -EINVAL; } /* Mute PCM volume */ spin_lock_irqsave(&p->chip->mixer_lock, flags); mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV); mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7); spin_unlock_irqrestore(&p->chip->mixer_lock, flags); spin_lock(&p->chip->reg_lock); set_mode_register(p->chip, 0xc0); /* c0 = STOP */ set_mode_register(p->chip, 0x70); /* 70 = RUN */ s_type = 0x00; if (channels == SNDRV_SB_CSP_MONO) s_type = 0x11; /* 000n 000n (n = 1 if mono) */ if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT) s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */ if (set_codec_parameter(p->chip, 0x81, s_type)) { snd_printd("%s: Set sample type command failed\n", __func__); goto __fail; } if (set_codec_parameter(p->chip, 0x80, 0x00)) { snd_printd("%s: Codec start command failed\n", __func__); goto __fail; } p->run_width = sample_width; p->run_channels = channels; p->running |= SNDRV_SB_CSP_ST_RUNNING; if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) { set_codec_parameter(p->chip, 0xe0, 0x01); /* enable QSound decoder */ set_codec_parameter(p->chip, 0x00, 0xff); set_codec_parameter(p->chip, 0x01, 0xff); p->running |= SNDRV_SB_CSP_ST_QSOUND; /* set QSound startup value */ snd_sb_csp_qsound_transfer(p); } result = 0; __fail: spin_unlock(&p->chip->reg_lock); /* restore PCM volume */ spin_lock_irqsave(&p->chip->mixer_lock, flags); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR); spin_unlock_irqrestore(&p->chip->mixer_lock, flags); return result; } /* * stop CSP */ static int snd_sb_csp_stop(struct snd_sb_csp * p) { int result; unsigned char mixL, mixR; unsigned long flags; if (!(p->running & SNDRV_SB_CSP_ST_RUNNING)) return 0; /* Mute PCM volume */ spin_lock_irqsave(&p->chip->mixer_lock, flags); mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV); mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7); spin_unlock_irqrestore(&p->chip->mixer_lock, flags); spin_lock(&p->chip->reg_lock); if (p->running & SNDRV_SB_CSP_ST_QSOUND) { set_codec_parameter(p->chip, 0xe0, 0x01); /* disable QSound decoder */ set_codec_parameter(p->chip, 0x00, 0x00); set_codec_parameter(p->chip, 0x01, 0x00); p->running &= ~SNDRV_SB_CSP_ST_QSOUND; } result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */ spin_unlock(&p->chip->reg_lock); /* restore PCM volume */ spin_lock_irqsave(&p->chip->mixer_lock, flags); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL); snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR); spin_unlock_irqrestore(&p->chip->mixer_lock, flags); if (!(result)) p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING); return result; } /* * pause CSP codec and hold DMA transfer */ static int snd_sb_csp_pause(struct snd_sb_csp * p) { int result; unsigned long flags; if (!(p->running & SNDRV_SB_CSP_ST_RUNNING)) return -EBUSY; spin_lock_irqsave(&p->chip->reg_lock, flags); result = set_codec_parameter(p->chip, 0x80, 0xff); spin_unlock_irqrestore(&p->chip->reg_lock, flags); if (!(result)) p->running |= SNDRV_SB_CSP_ST_PAUSED; return result; } /* * restart CSP codec and resume DMA transfer */ static int snd_sb_csp_restart(struct snd_sb_csp * p) { int result; unsigned long flags; if (!(p->running & SNDRV_SB_CSP_ST_PAUSED)) return -EBUSY; spin_lock_irqsave(&p->chip->reg_lock, flags); result = set_codec_parameter(p->chip, 0x80, 0x00); spin_unlock_irqrestore(&p->chip->reg_lock, flags); if (!(result)) p->running &= ~SNDRV_SB_CSP_ST_PAUSED; return result; } /* ------------------------------ */ /* * QSound mixer control for PCM */ #define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0; return 0; } static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned char nval; nval = ucontrol->value.integer.value[0] & 0x01; spin_lock_irqsave(&p->q_lock, flags); change = p->q_enabled != nval; p->q_enabled = nval; spin_unlock_irqrestore(&p->q_lock, flags); return change; } static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT; return 0; } static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&p->q_lock, flags); ucontrol->value.integer.value[0] = p->qpos_left; ucontrol->value.integer.value[1] = p->qpos_right; spin_unlock_irqrestore(&p->q_lock, flags); return 0; } static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned char nval1, nval2; nval1 = ucontrol->value.integer.value[0]; if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT) nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT; nval2 = ucontrol->value.integer.value[1]; if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT) nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT; spin_lock_irqsave(&p->q_lock, flags); change = p->qpos_left != nval1 || p->qpos_right != nval2; p->qpos_left = nval1; p->qpos_right = nval2; p->qpos_changed = change; spin_unlock_irqrestore(&p->q_lock, flags); return change; } static const struct snd_kcontrol_new snd_sb_qsound_switch = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "3D Control - Switch", .info = snd_sb_qsound_switch_info, .get = snd_sb_qsound_switch_get, .put = snd_sb_qsound_switch_put }; static const struct snd_kcontrol_new snd_sb_qsound_space = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "3D Control - Space", .info = snd_sb_qsound_space_info, .get = snd_sb_qsound_space_get, .put = snd_sb_qsound_space_put }; static int snd_sb_qsound_build(struct snd_sb_csp * p) { struct snd_card *card; struct snd_kcontrol *kctl; int err; if (snd_BUG_ON(!p)) return -EINVAL; card = p->chip->card; p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2; p->qpos_changed = 0; spin_lock_init(&p->q_lock); kctl = snd_ctl_new1(&snd_sb_qsound_switch, p); err = snd_ctl_add(card, kctl); if (err < 0) goto __error; p->qsound_switch = kctl; kctl = snd_ctl_new1(&snd_sb_qsound_space, p); err = snd_ctl_add(card, kctl); if (err < 0) goto __error; p->qsound_space = kctl; return 0; __error: snd_sb_qsound_destroy(p); return err; } static void snd_sb_qsound_destroy(struct snd_sb_csp * p) { struct snd_card *card; unsigned long flags; if (snd_BUG_ON(!p)) return; card = p->chip->card; if (p->qsound_switch) { snd_ctl_remove(card, p->qsound_switch); p->qsound_switch = NULL; } if (p->qsound_space) { snd_ctl_remove(card, p->qsound_space); p->qsound_space = NULL; } /* cancel pending transfer of QSound parameters */ spin_lock_irqsave (&p->q_lock, flags); p->qpos_changed = 0; spin_unlock_irqrestore (&p->q_lock, flags); } /* * Transfer qsound parameters to CSP, * function should be called from interrupt routine */ static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p) { int err = -ENXIO; spin_lock(&p->q_lock); if (p->running & SNDRV_SB_CSP_ST_QSOUND) { set_codec_parameter(p->chip, 0xe0, 0x01); /* left channel */ set_codec_parameter(p->chip, 0x00, p->qpos_left); set_codec_parameter(p->chip, 0x02, 0x00); /* right channel */ set_codec_parameter(p->chip, 0x00, p->qpos_right); set_codec_parameter(p->chip, 0x03, 0x00); err = 0; } p->qpos_changed = 0; spin_unlock(&p->q_lock); return err; } /* ------------------------------ */ /* * proc interface */ static int init_proc_entry(struct snd_sb_csp * p, int device) { char name[16]; sprintf(name, "cspD%d", device); snd_card_ro_proc_new(p->chip->card, name, p, info_read); return 0; } static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_sb_csp *p = entry->private_data; snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f)); snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'), ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'), ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'), ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-')); if (p->running & SNDRV_SB_CSP_ST_LOADED) { snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr); snd_iprintf(buffer, "Sample rates: "); if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) { snd_iprintf(buffer, "All\n"); } else { snd_iprintf(buffer, "%s%s%s%s\n", ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""), ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""), ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""), ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : "")); } if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) { snd_iprintf(buffer, "QSound decoder %sabled\n", p->q_enabled ? "en" : "dis"); } else { snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n", p->acc_format, ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"), ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"), ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"), ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"), ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"), ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-")); } } if (p->running & SNDRV_SB_CSP_ST_AUTO) { snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n"); } if (p->running & SNDRV_SB_CSP_ST_RUNNING) { snd_iprintf(buffer, "Processing %dbit %s PCM samples\n", ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8), ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo")); } if (p->running & SNDRV_SB_CSP_ST_QSOUND) { snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n", p->qpos_left, p->qpos_right); } } /* */ EXPORT_SYMBOL(snd_sb_csp_new);
linux-master
sound/isa/sb/sb16_csp.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for Sound Blaster mixer control */ #include <linux/io.h> #include <linux/delay.h> #include <linux/time.h> #include <sound/core.h> #include <sound/sb.h> #include <sound/control.h> #undef IO_DEBUG void snd_sbmixer_write(struct snd_sb *chip, unsigned char reg, unsigned char data) { outb(reg, SBP(chip, MIXER_ADDR)); udelay(10); outb(data, SBP(chip, MIXER_DATA)); udelay(10); #ifdef IO_DEBUG snd_printk(KERN_DEBUG "mixer_write 0x%x 0x%x\n", reg, data); #endif } unsigned char snd_sbmixer_read(struct snd_sb *chip, unsigned char reg) { unsigned char result; outb(reg, SBP(chip, MIXER_ADDR)); udelay(10); result = inb(SBP(chip, MIXER_DATA)); udelay(10); #ifdef IO_DEBUG snd_printk(KERN_DEBUG "mixer_read 0x%x 0x%x\n", reg, result); #endif return result; } /* * Single channel mixer element */ static int snd_sbmixer_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 16) & 0xff; int mask = (kcontrol->private_value >> 24) & 0xff; unsigned char val; spin_lock_irqsave(&sb->mixer_lock, flags); val = (snd_sbmixer_read(sb, reg) >> shift) & mask; spin_unlock_irqrestore(&sb->mixer_lock, flags); ucontrol->value.integer.value[0] = val; return 0; } static int snd_sbmixer_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 16) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int change; unsigned char val, oval; val = (ucontrol->value.integer.value[0] & mask) << shift; spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, reg); val = (oval & ~(mask << shift)) | val; change = val != oval; if (change) snd_sbmixer_write(sb, reg, val); spin_unlock_irqrestore(&sb->mixer_lock, flags); return change; } /* * Double channel mixer element */ static int snd_sbmixer_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_sbmixer_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int left_shift = (kcontrol->private_value >> 16) & 0x07; int right_shift = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; unsigned char left, right; spin_lock_irqsave(&sb->mixer_lock, flags); left = (snd_sbmixer_read(sb, left_reg) >> left_shift) & mask; right = (snd_sbmixer_read(sb, right_reg) >> right_shift) & mask; spin_unlock_irqrestore(&sb->mixer_lock, flags); ucontrol->value.integer.value[0] = left; ucontrol->value.integer.value[1] = right; return 0; } static int snd_sbmixer_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int left_shift = (kcontrol->private_value >> 16) & 0x07; int right_shift = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int change; unsigned char left, right, oleft, oright; left = (ucontrol->value.integer.value[0] & mask) << left_shift; right = (ucontrol->value.integer.value[1] & mask) << right_shift; spin_lock_irqsave(&sb->mixer_lock, flags); if (left_reg == right_reg) { oleft = snd_sbmixer_read(sb, left_reg); left = (oleft & ~((mask << left_shift) | (mask << right_shift))) | left | right; change = left != oleft; if (change) snd_sbmixer_write(sb, left_reg, left); } else { oleft = snd_sbmixer_read(sb, left_reg); oright = snd_sbmixer_read(sb, right_reg); left = (oleft & ~(mask << left_shift)) | left; right = (oright & ~(mask << right_shift)) | right; change = left != oleft || right != oright; if (change) { snd_sbmixer_write(sb, left_reg, left); snd_sbmixer_write(sb, right_reg, right); } } spin_unlock_irqrestore(&sb->mixer_lock, flags); return change; } /* * DT-019x / ALS-007 capture/input switch */ static int snd_dt019x_input_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[5] = { "CD", "Mic", "Line", "Synth", "Master" }; return snd_ctl_enum_info(uinfo, 1, 5, texts); } static int snd_dt019x_input_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned char oval; spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, SB_DT019X_CAPTURE_SW); spin_unlock_irqrestore(&sb->mixer_lock, flags); switch (oval & 0x07) { case SB_DT019X_CAP_CD: ucontrol->value.enumerated.item[0] = 0; break; case SB_DT019X_CAP_MIC: ucontrol->value.enumerated.item[0] = 1; break; case SB_DT019X_CAP_LINE: ucontrol->value.enumerated.item[0] = 2; break; case SB_DT019X_CAP_MAIN: ucontrol->value.enumerated.item[0] = 4; break; /* To record the synth on these cards you must record the main. */ /* Thus SB_DT019X_CAP_SYNTH == SB_DT019X_CAP_MAIN and would cause */ /* duplicate case labels if left uncommented. */ /* case SB_DT019X_CAP_SYNTH: * ucontrol->value.enumerated.item[0] = 3; * break; */ default: ucontrol->value.enumerated.item[0] = 4; break; } return 0; } static int snd_dt019x_input_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned char nval, oval; if (ucontrol->value.enumerated.item[0] > 4) return -EINVAL; switch (ucontrol->value.enumerated.item[0]) { case 0: nval = SB_DT019X_CAP_CD; break; case 1: nval = SB_DT019X_CAP_MIC; break; case 2: nval = SB_DT019X_CAP_LINE; break; case 3: nval = SB_DT019X_CAP_SYNTH; break; case 4: nval = SB_DT019X_CAP_MAIN; break; default: nval = SB_DT019X_CAP_MAIN; } spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, SB_DT019X_CAPTURE_SW); change = nval != oval; if (change) snd_sbmixer_write(sb, SB_DT019X_CAPTURE_SW, nval); spin_unlock_irqrestore(&sb->mixer_lock, flags); return change; } /* * ALS4000 mono recording control switch */ static int snd_als4k_mono_capture_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[3] = { "L chan only", "R chan only", "L ch/2 + R ch/2" }; return snd_ctl_enum_info(uinfo, 1, 3, texts); } static int snd_als4k_mono_capture_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned char oval; spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, SB_ALS4000_MONO_IO_CTRL); spin_unlock_irqrestore(&sb->mixer_lock, flags); oval >>= 6; if (oval > 2) oval = 2; ucontrol->value.enumerated.item[0] = oval; return 0; } static int snd_als4k_mono_capture_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned char nval, oval; if (ucontrol->value.enumerated.item[0] > 2) return -EINVAL; spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, SB_ALS4000_MONO_IO_CTRL); nval = (oval & ~(3 << 6)) | (ucontrol->value.enumerated.item[0] << 6); change = nval != oval; if (change) snd_sbmixer_write(sb, SB_ALS4000_MONO_IO_CTRL, nval); spin_unlock_irqrestore(&sb->mixer_lock, flags); return change; } /* * SBPRO input multiplexer */ static int snd_sb8mixer_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[3] = { "Mic", "CD", "Line" }; return snd_ctl_enum_info(uinfo, 1, 3, texts); } static int snd_sb8mixer_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned char oval; spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, SB_DSP_CAPTURE_SOURCE); spin_unlock_irqrestore(&sb->mixer_lock, flags); switch ((oval >> 0x01) & 0x03) { case SB_DSP_MIXS_CD: ucontrol->value.enumerated.item[0] = 1; break; case SB_DSP_MIXS_LINE: ucontrol->value.enumerated.item[0] = 2; break; default: ucontrol->value.enumerated.item[0] = 0; break; } return 0; } static int snd_sb8mixer_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned char nval, oval; if (ucontrol->value.enumerated.item[0] > 2) return -EINVAL; switch (ucontrol->value.enumerated.item[0]) { case 1: nval = SB_DSP_MIXS_CD; break; case 2: nval = SB_DSP_MIXS_LINE; break; default: nval = SB_DSP_MIXS_MIC; } nval <<= 1; spin_lock_irqsave(&sb->mixer_lock, flags); oval = snd_sbmixer_read(sb, SB_DSP_CAPTURE_SOURCE); nval |= oval & ~0x06; change = nval != oval; if (change) snd_sbmixer_write(sb, SB_DSP_CAPTURE_SOURCE, nval); spin_unlock_irqrestore(&sb->mixer_lock, flags); return change; } /* * SB16 input switch */ static int snd_sb16mixer_info_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; uinfo->count = 4; uinfo->value.integer.min = 0; uinfo->value.integer.max = 1; return 0; } static int snd_sb16mixer_get_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg1 = kcontrol->private_value & 0xff; int reg2 = (kcontrol->private_value >> 8) & 0xff; int left_shift = (kcontrol->private_value >> 16) & 0x0f; int right_shift = (kcontrol->private_value >> 24) & 0x0f; unsigned char val1, val2; spin_lock_irqsave(&sb->mixer_lock, flags); val1 = snd_sbmixer_read(sb, reg1); val2 = snd_sbmixer_read(sb, reg2); spin_unlock_irqrestore(&sb->mixer_lock, flags); ucontrol->value.integer.value[0] = (val1 >> left_shift) & 0x01; ucontrol->value.integer.value[1] = (val2 >> left_shift) & 0x01; ucontrol->value.integer.value[2] = (val1 >> right_shift) & 0x01; ucontrol->value.integer.value[3] = (val2 >> right_shift) & 0x01; return 0; } static int snd_sb16mixer_put_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_sb *sb = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg1 = kcontrol->private_value & 0xff; int reg2 = (kcontrol->private_value >> 8) & 0xff; int left_shift = (kcontrol->private_value >> 16) & 0x0f; int right_shift = (kcontrol->private_value >> 24) & 0x0f; int change; unsigned char val1, val2, oval1, oval2; spin_lock_irqsave(&sb->mixer_lock, flags); oval1 = snd_sbmixer_read(sb, reg1); oval2 = snd_sbmixer_read(sb, reg2); val1 = oval1 & ~((1 << left_shift) | (1 << right_shift)); val2 = oval2 & ~((1 << left_shift) | (1 << right_shift)); val1 |= (ucontrol->value.integer.value[0] & 1) << left_shift; val2 |= (ucontrol->value.integer.value[1] & 1) << left_shift; val1 |= (ucontrol->value.integer.value[2] & 1) << right_shift; val2 |= (ucontrol->value.integer.value[3] & 1) << right_shift; change = val1 != oval1 || val2 != oval2; if (change) { snd_sbmixer_write(sb, reg1, val1); snd_sbmixer_write(sb, reg2, val2); } spin_unlock_irqrestore(&sb->mixer_lock, flags); return change; } /* */ /* */ int snd_sbmixer_add_ctl(struct snd_sb *chip, const char *name, int index, int type, unsigned long value) { static const struct snd_kcontrol_new newctls[] = { [SB_MIX_SINGLE] = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .info = snd_sbmixer_info_single, .get = snd_sbmixer_get_single, .put = snd_sbmixer_put_single, }, [SB_MIX_DOUBLE] = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .info = snd_sbmixer_info_double, .get = snd_sbmixer_get_double, .put = snd_sbmixer_put_double, }, [SB_MIX_INPUT_SW] = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .info = snd_sb16mixer_info_input_sw, .get = snd_sb16mixer_get_input_sw, .put = snd_sb16mixer_put_input_sw, }, [SB_MIX_CAPTURE_PRO] = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .info = snd_sb8mixer_info_mux, .get = snd_sb8mixer_get_mux, .put = snd_sb8mixer_put_mux, }, [SB_MIX_CAPTURE_DT019X] = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .info = snd_dt019x_input_sw_info, .get = snd_dt019x_input_sw_get, .put = snd_dt019x_input_sw_put, }, [SB_MIX_MONO_CAPTURE_ALS4K] = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .info = snd_als4k_mono_capture_route_info, .get = snd_als4k_mono_capture_route_get, .put = snd_als4k_mono_capture_route_put, }, }; struct snd_kcontrol *ctl; int err; ctl = snd_ctl_new1(&newctls[type], chip); if (! ctl) return -ENOMEM; strscpy(ctl->id.name, name, sizeof(ctl->id.name)); ctl->id.index = index; ctl->private_value = value; err = snd_ctl_add(chip->card, ctl); if (err < 0) return err; return 0; } /* * SB 2.0 specific mixer elements */ static const struct sbmix_elem snd_sb20_controls[] = { SB_SINGLE("Master Playback Volume", SB_DSP20_MASTER_DEV, 1, 7), SB_SINGLE("PCM Playback Volume", SB_DSP20_PCM_DEV, 1, 3), SB_SINGLE("Synth Playback Volume", SB_DSP20_FM_DEV, 1, 7), SB_SINGLE("CD Playback Volume", SB_DSP20_CD_DEV, 1, 7) }; static const unsigned char snd_sb20_init_values[][2] = { { SB_DSP20_MASTER_DEV, 0 }, { SB_DSP20_FM_DEV, 0 }, }; /* * SB Pro specific mixer elements */ static const struct sbmix_elem snd_sbpro_controls[] = { SB_DOUBLE("Master Playback Volume", SB_DSP_MASTER_DEV, SB_DSP_MASTER_DEV, 5, 1, 7), SB_DOUBLE("PCM Playback Volume", SB_DSP_PCM_DEV, SB_DSP_PCM_DEV, 5, 1, 7), SB_SINGLE("PCM Playback Filter", SB_DSP_PLAYBACK_FILT, 5, 1), SB_DOUBLE("Synth Playback Volume", SB_DSP_FM_DEV, SB_DSP_FM_DEV, 5, 1, 7), SB_DOUBLE("CD Playback Volume", SB_DSP_CD_DEV, SB_DSP_CD_DEV, 5, 1, 7), SB_DOUBLE("Line Playback Volume", SB_DSP_LINE_DEV, SB_DSP_LINE_DEV, 5, 1, 7), SB_SINGLE("Mic Playback Volume", SB_DSP_MIC_DEV, 1, 3), { .name = "Capture Source", .type = SB_MIX_CAPTURE_PRO }, SB_SINGLE("Capture Filter", SB_DSP_CAPTURE_FILT, 5, 1), SB_SINGLE("Capture Low-Pass Filter", SB_DSP_CAPTURE_FILT, 3, 1) }; static const unsigned char snd_sbpro_init_values[][2] = { { SB_DSP_MASTER_DEV, 0 }, { SB_DSP_PCM_DEV, 0 }, { SB_DSP_FM_DEV, 0 }, }; /* * SB16 specific mixer elements */ static const struct sbmix_elem snd_sb16_controls[] = { SB_DOUBLE("Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31), SB_DOUBLE("PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31), SB16_INPUT_SW("Synth Capture Route", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 6, 5), SB_DOUBLE("Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31), SB16_INPUT_SW("CD Capture Route", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 2, 1), SB_DOUBLE("CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1), SB_DOUBLE("CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31), SB16_INPUT_SW("Mic Capture Route", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0), SB_SINGLE("Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1), SB_SINGLE("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31), SB_SINGLE("Beep Volume", SB_DSP4_SPEAKER_DEV, 6, 3), SB_DOUBLE("Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3), SB_DOUBLE("Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3), SB16_INPUT_SW("Line Capture Route", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 4, 3), SB_DOUBLE("Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1), SB_DOUBLE("Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31), SB_SINGLE("Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1), SB_SINGLE("3D Enhancement Switch", SB_DSP4_3DSE, 0, 1), SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15), SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15) }; static const unsigned char snd_sb16_init_values[][2] = { { SB_DSP4_MASTER_DEV + 0, 0 }, { SB_DSP4_MASTER_DEV + 1, 0 }, { SB_DSP4_PCM_DEV + 0, 0 }, { SB_DSP4_PCM_DEV + 1, 0 }, { SB_DSP4_SYNTH_DEV + 0, 0 }, { SB_DSP4_SYNTH_DEV + 1, 0 }, { SB_DSP4_INPUT_LEFT, 0 }, { SB_DSP4_INPUT_RIGHT, 0 }, { SB_DSP4_OUTPUT_SW, 0 }, { SB_DSP4_SPEAKER_DEV, 0 }, }; /* * DT019x specific mixer elements */ static const struct sbmix_elem snd_dt019x_controls[] = { /* ALS4000 below has some parts which we might be lacking, * e.g. snd_als4000_ctl_mono_playback_switch - check it! */ SB_DOUBLE("Master Playback Volume", SB_DT019X_MASTER_DEV, SB_DT019X_MASTER_DEV, 4, 0, 15), SB_DOUBLE("PCM Playback Switch", SB_DT019X_OUTPUT_SW2, SB_DT019X_OUTPUT_SW2, 2, 1, 1), SB_DOUBLE("PCM Playback Volume", SB_DT019X_PCM_DEV, SB_DT019X_PCM_DEV, 4, 0, 15), SB_DOUBLE("Synth Playback Switch", SB_DT019X_OUTPUT_SW2, SB_DT019X_OUTPUT_SW2, 4, 3, 1), SB_DOUBLE("Synth Playback Volume", SB_DT019X_SYNTH_DEV, SB_DT019X_SYNTH_DEV, 4, 0, 15), SB_DOUBLE("CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1), SB_DOUBLE("CD Playback Volume", SB_DT019X_CD_DEV, SB_DT019X_CD_DEV, 4, 0, 15), SB_SINGLE("Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1), SB_SINGLE("Mic Playback Volume", SB_DT019X_MIC_DEV, 4, 7), SB_SINGLE("Beep Volume", SB_DT019X_SPKR_DEV, 0, 7), SB_DOUBLE("Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1), SB_DOUBLE("Line Playback Volume", SB_DT019X_LINE_DEV, SB_DT019X_LINE_DEV, 4, 0, 15), { .name = "Capture Source", .type = SB_MIX_CAPTURE_DT019X } }; static const unsigned char snd_dt019x_init_values[][2] = { { SB_DT019X_MASTER_DEV, 0 }, { SB_DT019X_PCM_DEV, 0 }, { SB_DT019X_SYNTH_DEV, 0 }, { SB_DT019X_CD_DEV, 0 }, { SB_DT019X_MIC_DEV, 0 }, /* Includes PC-speaker in high nibble */ { SB_DT019X_LINE_DEV, 0 }, { SB_DSP4_OUTPUT_SW, 0 }, { SB_DT019X_OUTPUT_SW2, 0 }, { SB_DT019X_CAPTURE_SW, 0x06 }, }; /* * ALS4000 specific mixer elements */ static const struct sbmix_elem snd_als4000_controls[] = { SB_DOUBLE("PCM Playback Switch", SB_DT019X_OUTPUT_SW2, SB_DT019X_OUTPUT_SW2, 2, 1, 1), SB_DOUBLE("Synth Playback Switch", SB_DT019X_OUTPUT_SW2, SB_DT019X_OUTPUT_SW2, 4, 3, 1), SB_SINGLE("Mic Boost (+20dB)", SB_ALS4000_MIC_IN_GAIN, 0, 0x03), SB_SINGLE("Master Mono Playback Switch", SB_ALS4000_MONO_IO_CTRL, 5, 1), { .name = "Master Mono Capture Route", .type = SB_MIX_MONO_CAPTURE_ALS4K }, SB_SINGLE("Mono Playback Switch", SB_DT019X_OUTPUT_SW2, 0, 1), SB_SINGLE("Analog Loopback Switch", SB_ALS4000_MIC_IN_GAIN, 7, 0x01), SB_SINGLE("3D Control - Switch", SB_ALS4000_3D_SND_FX, 6, 0x01), SB_SINGLE("Digital Loopback Switch", SB_ALS4000_CR3_CONFIGURATION, 7, 0x01), /* FIXME: functionality of 3D controls might be swapped, I didn't find * a description of how to identify what is supposed to be what */ SB_SINGLE("3D Control - Level", SB_ALS4000_3D_SND_FX, 0, 0x07), /* FIXME: maybe there's actually some standard 3D ctrl name for it?? */ SB_SINGLE("3D Control - Freq", SB_ALS4000_3D_SND_FX, 4, 0x03), /* FIXME: ALS4000a.pdf mentions BBD (Bucket Brigade Device) time delay, * but what ALSA 3D attribute is that actually? "Center", "Depth", * "Wide" or "Space" or even "Level"? Assuming "Wide" for now... */ SB_SINGLE("3D Control - Wide", SB_ALS4000_3D_TIME_DELAY, 0, 0x0f), SB_SINGLE("3D PowerOff Switch", SB_ALS4000_3D_TIME_DELAY, 4, 0x01), SB_SINGLE("Master Playback 8kHz / 20kHz LPF Switch", SB_ALS4000_FMDAC, 5, 0x01), #ifdef NOT_AVAILABLE SB_SINGLE("FMDAC Switch (Option ?)", SB_ALS4000_FMDAC, 0, 0x01), SB_SINGLE("QSound Mode", SB_ALS4000_QSOUND, 1, 0x1f), #endif }; static const unsigned char snd_als4000_init_values[][2] = { { SB_DSP4_MASTER_DEV + 0, 0 }, { SB_DSP4_MASTER_DEV + 1, 0 }, { SB_DSP4_PCM_DEV + 0, 0 }, { SB_DSP4_PCM_DEV + 1, 0 }, { SB_DSP4_SYNTH_DEV + 0, 0 }, { SB_DSP4_SYNTH_DEV + 1, 0 }, { SB_DSP4_SPEAKER_DEV, 0 }, { SB_DSP4_OUTPUT_SW, 0 }, { SB_DSP4_INPUT_LEFT, 0 }, { SB_DSP4_INPUT_RIGHT, 0 }, { SB_DT019X_OUTPUT_SW2, 0 }, { SB_ALS4000_MIC_IN_GAIN, 0 }, }; /* */ static int snd_sbmixer_init(struct snd_sb *chip, const struct sbmix_elem *controls, int controls_count, const unsigned char map[][2], int map_count, char *name) { unsigned long flags; struct snd_card *card = chip->card; int idx, err; /* mixer reset */ spin_lock_irqsave(&chip->mixer_lock, flags); snd_sbmixer_write(chip, 0x00, 0x00); spin_unlock_irqrestore(&chip->mixer_lock, flags); /* mute and zero volume channels */ for (idx = 0; idx < map_count; idx++) { spin_lock_irqsave(&chip->mixer_lock, flags); snd_sbmixer_write(chip, map[idx][0], map[idx][1]); spin_unlock_irqrestore(&chip->mixer_lock, flags); } for (idx = 0; idx < controls_count; idx++) { err = snd_sbmixer_add_ctl_elem(chip, &controls[idx]); if (err < 0) return err; } snd_component_add(card, name); strcpy(card->mixername, name); return 0; } int snd_sbmixer_new(struct snd_sb *chip) { struct snd_card *card; int err; if (snd_BUG_ON(!chip || !chip->card)) return -EINVAL; card = chip->card; switch (chip->hardware) { case SB_HW_10: return 0; /* no mixer chip on SB1.x */ case SB_HW_20: case SB_HW_201: err = snd_sbmixer_init(chip, snd_sb20_controls, ARRAY_SIZE(snd_sb20_controls), snd_sb20_init_values, ARRAY_SIZE(snd_sb20_init_values), "CTL1335"); if (err < 0) return err; break; case SB_HW_PRO: case SB_HW_JAZZ16: err = snd_sbmixer_init(chip, snd_sbpro_controls, ARRAY_SIZE(snd_sbpro_controls), snd_sbpro_init_values, ARRAY_SIZE(snd_sbpro_init_values), "CTL1345"); if (err < 0) return err; break; case SB_HW_16: case SB_HW_ALS100: case SB_HW_CS5530: err = snd_sbmixer_init(chip, snd_sb16_controls, ARRAY_SIZE(snd_sb16_controls), snd_sb16_init_values, ARRAY_SIZE(snd_sb16_init_values), "CTL1745"); if (err < 0) return err; break; case SB_HW_ALS4000: /* use only the first 16 controls from SB16 */ err = snd_sbmixer_init(chip, snd_sb16_controls, 16, snd_sb16_init_values, ARRAY_SIZE(snd_sb16_init_values), "ALS4000"); if (err < 0) return err; err = snd_sbmixer_init(chip, snd_als4000_controls, ARRAY_SIZE(snd_als4000_controls), snd_als4000_init_values, ARRAY_SIZE(snd_als4000_init_values), "ALS4000"); if (err < 0) return err; break; case SB_HW_DT019X: err = snd_sbmixer_init(chip, snd_dt019x_controls, ARRAY_SIZE(snd_dt019x_controls), snd_dt019x_init_values, ARRAY_SIZE(snd_dt019x_init_values), "DT019X"); if (err < 0) return err; break; default: strcpy(card->mixername, "???"); } return 0; } #ifdef CONFIG_PM static const unsigned char sb20_saved_regs[] = { SB_DSP20_MASTER_DEV, SB_DSP20_PCM_DEV, SB_DSP20_FM_DEV, SB_DSP20_CD_DEV, }; static const unsigned char sbpro_saved_regs[] = { SB_DSP_MASTER_DEV, SB_DSP_PCM_DEV, SB_DSP_PLAYBACK_FILT, SB_DSP_FM_DEV, SB_DSP_CD_DEV, SB_DSP_LINE_DEV, SB_DSP_MIC_DEV, SB_DSP_CAPTURE_SOURCE, SB_DSP_CAPTURE_FILT, }; static const unsigned char sb16_saved_regs[] = { SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1, SB_DSP4_3DSE, SB_DSP4_BASS_DEV, SB_DSP4_BASS_DEV + 1, SB_DSP4_TREBLE_DEV, SB_DSP4_TREBLE_DEV + 1, SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1, SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1, SB_DSP4_OUTPUT_SW, SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1, SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1, SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV, SB_DSP4_IGAIN_DEV, SB_DSP4_IGAIN_DEV + 1, SB_DSP4_OGAIN_DEV, SB_DSP4_OGAIN_DEV + 1, SB_DSP4_MIC_AGC }; static const unsigned char dt019x_saved_regs[] = { SB_DT019X_MASTER_DEV, SB_DT019X_PCM_DEV, SB_DT019X_SYNTH_DEV, SB_DT019X_CD_DEV, SB_DT019X_MIC_DEV, SB_DT019X_SPKR_DEV, SB_DT019X_LINE_DEV, SB_DSP4_OUTPUT_SW, SB_DT019X_OUTPUT_SW2, SB_DT019X_CAPTURE_SW, }; static const unsigned char als4000_saved_regs[] = { /* please verify in dsheet whether regs to be added are actually real H/W or just dummy */ SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1, SB_DSP4_OUTPUT_SW, SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1, SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1, SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1, SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV, SB_DSP4_IGAIN_DEV, SB_DSP4_IGAIN_DEV + 1, SB_DSP4_OGAIN_DEV, SB_DSP4_OGAIN_DEV + 1, SB_DT019X_OUTPUT_SW2, SB_ALS4000_MONO_IO_CTRL, SB_ALS4000_MIC_IN_GAIN, SB_ALS4000_FMDAC, SB_ALS4000_3D_SND_FX, SB_ALS4000_3D_TIME_DELAY, SB_ALS4000_CR3_CONFIGURATION, }; static void save_mixer(struct snd_sb *chip, const unsigned char *regs, int num_regs) { unsigned char *val = chip->saved_regs; if (snd_BUG_ON(num_regs > ARRAY_SIZE(chip->saved_regs))) return; for (; num_regs; num_regs--) *val++ = snd_sbmixer_read(chip, *regs++); } static void restore_mixer(struct snd_sb *chip, const unsigned char *regs, int num_regs) { unsigned char *val = chip->saved_regs; if (snd_BUG_ON(num_regs > ARRAY_SIZE(chip->saved_regs))) return; for (; num_regs; num_regs--) snd_sbmixer_write(chip, *regs++, *val++); } void snd_sbmixer_suspend(struct snd_sb *chip) { switch (chip->hardware) { case SB_HW_20: case SB_HW_201: save_mixer(chip, sb20_saved_regs, ARRAY_SIZE(sb20_saved_regs)); break; case SB_HW_PRO: case SB_HW_JAZZ16: save_mixer(chip, sbpro_saved_regs, ARRAY_SIZE(sbpro_saved_regs)); break; case SB_HW_16: case SB_HW_ALS100: case SB_HW_CS5530: save_mixer(chip, sb16_saved_regs, ARRAY_SIZE(sb16_saved_regs)); break; case SB_HW_ALS4000: save_mixer(chip, als4000_saved_regs, ARRAY_SIZE(als4000_saved_regs)); break; case SB_HW_DT019X: save_mixer(chip, dt019x_saved_regs, ARRAY_SIZE(dt019x_saved_regs)); break; default: break; } } void snd_sbmixer_resume(struct snd_sb *chip) { switch (chip->hardware) { case SB_HW_20: case SB_HW_201: restore_mixer(chip, sb20_saved_regs, ARRAY_SIZE(sb20_saved_regs)); break; case SB_HW_PRO: case SB_HW_JAZZ16: restore_mixer(chip, sbpro_saved_regs, ARRAY_SIZE(sbpro_saved_regs)); break; case SB_HW_16: case SB_HW_ALS100: case SB_HW_CS5530: restore_mixer(chip, sb16_saved_regs, ARRAY_SIZE(sb16_saved_regs)); break; case SB_HW_ALS4000: restore_mixer(chip, als4000_saved_regs, ARRAY_SIZE(als4000_saved_regs)); break; case SB_HW_DT019X: restore_mixer(chip, dt019x_saved_regs, ARRAY_SIZE(dt019x_saved_regs)); break; default: break; } } #endif
linux-master
sound/isa/sb/sb_mixer.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * pcm emulation on emu8000 wavetable * * Copyright (C) 2002 Takashi Iwai <[email protected]> */ #include "emu8000_local.h" #include <linux/sched/signal.h> #include <linux/init.h> #include <linux/slab.h> #include <sound/initval.h> #include <sound/pcm.h> /* * define the following if you want to use this pcm with non-interleaved mode */ /* #define USE_NONINTERLEAVE */ /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set * mmap_emulation flag to 1 in your .asoundrc, such like * * pcm.emu8k { * type plug * slave.pcm { * type hw * card 0 * device 1 * mmap_emulation 1 * } * } * * besides, for the time being, the non-interleaved mode doesn't work well on * alsa-lib... */ struct snd_emu8k_pcm { struct snd_emu8000 *emu; struct snd_pcm_substream *substream; unsigned int allocated_bytes; struct snd_util_memblk *block; unsigned int offset; unsigned int buf_size; unsigned int period_size; unsigned int loop_start[2]; unsigned int pitch; int panning[2]; int last_ptr; int period_pos; int voices; unsigned int dram_opened: 1; unsigned int running: 1; unsigned int timer_running: 1; struct timer_list timer; spinlock_t timer_lock; }; #define LOOP_BLANK_SIZE 8 /* * open up channels for the simultaneous data transfer and playback */ static int emu8k_open_dram_for_pcm(struct snd_emu8000 *emu, int channels) { int i; /* reserve up to 2 voices for playback */ snd_emux_lock_voice(emu->emu, 0); if (channels > 1) snd_emux_lock_voice(emu->emu, 1); /* reserve 28 voices for loading */ for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) { unsigned int mode = EMU8000_RAM_WRITE; snd_emux_lock_voice(emu->emu, i); #ifndef USE_NONINTERLEAVE if (channels > 1 && (i & 1) != 0) mode |= EMU8000_RAM_RIGHT; #endif snd_emu8000_dma_chan(emu, i, mode); } /* assign voice 31 and 32 to ROM */ EMU8000_VTFT_WRITE(emu, 30, 0); EMU8000_PSST_WRITE(emu, 30, 0x1d8); EMU8000_CSL_WRITE(emu, 30, 0x1e0); EMU8000_CCCA_WRITE(emu, 30, 0x1d8); EMU8000_VTFT_WRITE(emu, 31, 0); EMU8000_PSST_WRITE(emu, 31, 0x1d8); EMU8000_CSL_WRITE(emu, 31, 0x1e0); EMU8000_CCCA_WRITE(emu, 31, 0x1d8); return 0; } /* */ static void snd_emu8000_write_wait(struct snd_emu8000 *emu, int can_schedule) { while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) { if (can_schedule) { schedule_timeout_interruptible(1); if (signal_pending(current)) break; } } } /* * close all channels */ static void emu8k_close_dram(struct snd_emu8000 *emu) { int i; for (i = 0; i < 2; i++) snd_emux_unlock_voice(emu->emu, i); for (; i < EMU8000_DRAM_VOICES; i++) { snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE); snd_emux_unlock_voice(emu->emu, i); } } /* * convert Hz to AWE32 rate offset (see emux/soundfont.c) */ #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */ #define SAMPLERATE_RATIO 4096 static int calc_rate_offset(int hz) { return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO); } /* */ static const struct snd_pcm_hardware emu8k_pcm_hw = { #ifdef USE_NONINTERLEAVE .info = SNDRV_PCM_INFO_NONINTERLEAVED, #else .info = SNDRV_PCM_INFO_INTERLEAVED, #endif .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 1024, .period_bytes_max = (128*1024), .periods_min = 2, .periods_max = 1024, .fifo_size = 0, }; /* * get the current position at the given channel from CCCA register */ static inline int emu8k_get_curpos(struct snd_emu8k_pcm *rec, int ch) { int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff; val -= rec->loop_start[ch] - 1; return val; } /* * timer interrupt handler * check the current position and update the period if necessary. */ static void emu8k_pcm_timer_func(struct timer_list *t) { struct snd_emu8k_pcm *rec = from_timer(rec, t, timer); int ptr, delta; spin_lock(&rec->timer_lock); /* update the current pointer */ ptr = emu8k_get_curpos(rec, 0); if (ptr < rec->last_ptr) delta = ptr + rec->buf_size - rec->last_ptr; else delta = ptr - rec->last_ptr; rec->period_pos += delta; rec->last_ptr = ptr; /* reprogram timer */ mod_timer(&rec->timer, jiffies + 1); /* update period */ if (rec->period_pos >= (int)rec->period_size) { rec->period_pos %= rec->period_size; spin_unlock(&rec->timer_lock); snd_pcm_period_elapsed(rec->substream); return; } spin_unlock(&rec->timer_lock); } /* * open pcm * creating an instance here */ static int emu8k_pcm_open(struct snd_pcm_substream *subs) { struct snd_emu8000 *emu = snd_pcm_substream_chip(subs); struct snd_emu8k_pcm *rec; struct snd_pcm_runtime *runtime = subs->runtime; rec = kzalloc(sizeof(*rec), GFP_KERNEL); if (! rec) return -ENOMEM; rec->emu = emu; rec->substream = subs; runtime->private_data = rec; spin_lock_init(&rec->timer_lock); timer_setup(&rec->timer, emu8k_pcm_timer_func, 0); runtime->hw = emu8k_pcm_hw; runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3; runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2; /* use timer to update periods.. (specified in msec) */ snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, DIV_ROUND_UP(1000000, HZ), UINT_MAX); return 0; } static int emu8k_pcm_close(struct snd_pcm_substream *subs) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; kfree(rec); subs->runtime->private_data = NULL; return 0; } /* * calculate pitch target */ static int calc_pitch_target(int pitch) { int ptarget = 1 << (pitch >> 12); if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710; if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710; if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710; ptarget += (ptarget >> 1); if (ptarget > 0xffff) ptarget = 0xffff; return ptarget; } /* * set up the voice */ static void setup_voice(struct snd_emu8k_pcm *rec, int ch) { struct snd_emu8000 *hw = rec->emu; unsigned int temp; /* channel to be silent and idle */ EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080); EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF); EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF); EMU8000_PTRX_WRITE(hw, ch, 0); EMU8000_CPF_WRITE(hw, ch, 0); /* pitch offset */ EMU8000_IP_WRITE(hw, ch, rec->pitch); /* set envelope parameters */ EMU8000_ENVVAL_WRITE(hw, ch, 0x8000); EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f); EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f); EMU8000_ENVVOL_WRITE(hw, ch, 0x8000); EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f); /* decay/sustain parameter for volume envelope is used for triggerg the voice */ /* modulation envelope heights */ EMU8000_PEFE_WRITE(hw, ch, 0x0); /* lfo1/2 delay */ EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000); EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000); /* lfo1 pitch & cutoff shift */ EMU8000_FMMOD_WRITE(hw, ch, 0); /* lfo1 volume & freq */ EMU8000_TREMFRQ_WRITE(hw, ch, 0); /* lfo2 pitch & freq */ EMU8000_FM2FRQ2_WRITE(hw, ch, 0); /* pan & loop start */ temp = rec->panning[ch]; temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1); EMU8000_PSST_WRITE(hw, ch, temp); /* chorus & loop end (chorus 8bit, MSB) */ temp = 0; // chorus temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1); EMU8000_CSL_WRITE(hw, ch, temp); /* Q & current address (Q 4bit value, MSB) */ temp = 0; // filterQ temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1); EMU8000_CCCA_WRITE(hw, ch, temp); /* clear unknown registers */ EMU8000_00A0_WRITE(hw, ch, 0); EMU8000_0080_WRITE(hw, ch, 0); } /* * trigger the voice */ static void start_voice(struct snd_emu8k_pcm *rec, int ch) { unsigned long flags; struct snd_emu8000 *hw = rec->emu; unsigned int temp, aux; int pt = calc_pitch_target(rec->pitch); /* cutoff and volume */ EMU8000_IFATN_WRITE(hw, ch, 0xff00); EMU8000_VTFT_WRITE(hw, ch, 0xffff); EMU8000_CVCF_WRITE(hw, ch, 0xffff); /* trigger envelope */ EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f); /* set reverb and pitch target */ temp = 0; // reverb if (rec->panning[ch] == 0) aux = 0xff; else aux = (-rec->panning[ch]) & 0xff; temp = (temp << 8) | (pt << 16) | aux; EMU8000_PTRX_WRITE(hw, ch, temp); EMU8000_CPF_WRITE(hw, ch, pt << 16); /* start timer */ spin_lock_irqsave(&rec->timer_lock, flags); if (! rec->timer_running) { mod_timer(&rec->timer, jiffies + 1); rec->timer_running = 1; } spin_unlock_irqrestore(&rec->timer_lock, flags); } /* * stop the voice immediately */ static void stop_voice(struct snd_emu8k_pcm *rec, int ch) { unsigned long flags; struct snd_emu8000 *hw = rec->emu; EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F); /* stop timer */ spin_lock_irqsave(&rec->timer_lock, flags); if (rec->timer_running) { del_timer(&rec->timer); rec->timer_running = 0; } spin_unlock_irqrestore(&rec->timer_lock, flags); } static int emu8k_pcm_trigger(struct snd_pcm_substream *subs, int cmd) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; int ch; switch (cmd) { case SNDRV_PCM_TRIGGER_START: for (ch = 0; ch < rec->voices; ch++) start_voice(rec, ch); rec->running = 1; break; case SNDRV_PCM_TRIGGER_STOP: rec->running = 0; for (ch = 0; ch < rec->voices; ch++) stop_voice(rec, ch); break; default: return -EINVAL; } return 0; } /* * copy / silence ops */ /* * this macro should be inserted in the copy/silence loops * to reduce the latency. without this, the system will hang up * during the whole loop. */ #define CHECK_SCHEDULER() \ do { \ cond_resched();\ if (signal_pending(current))\ return -EAGAIN;\ } while (0) #define GET_VAL(sval, iter) \ do { \ if (!iter) \ sval = 0; \ else if (copy_from_iter(&sval, 2, iter) != 2) \ return -EFAULT; \ } while (0) #ifdef USE_NONINTERLEAVE #define LOOP_WRITE(rec, offset, iter, count) \ do { \ struct snd_emu8000 *emu = (rec)->emu; \ snd_emu8000_write_wait(emu, 1); \ EMU8000_SMALW_WRITE(emu, offset); \ while (count > 0) { \ unsigned short sval; \ CHECK_SCHEDULER(); \ GET_VAL(sval, iter); \ EMU8000_SMLD_WRITE(emu, sval); \ count--; \ } \ } while (0) /* copy one channel block */ static int emu8k_pcm_copy(struct snd_pcm_substream *subs, int voice, unsigned long pos, struct iov_iter *src, unsigned long count) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; /* convert to word unit */ pos = (pos << 1) + rec->loop_start[voice]; count <<= 1; LOOP_WRITE(rec, pos, src, count); return 0; } /* make a channel block silence */ static int emu8k_pcm_silence(struct snd_pcm_substream *subs, int voice, unsigned long pos, unsigned long count) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; /* convert to word unit */ pos = (pos << 1) + rec->loop_start[voice]; count <<= 1; LOOP_WRITE(rec, pos, NULL, count); return 0; } #else /* interleave */ #define LOOP_WRITE(rec, pos, iter, count) \ do { \ struct snd_emu8000 *emu = rec->emu; \ snd_emu8000_write_wait(emu, 1); \ EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]); \ if (rec->voices > 1) \ EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]); \ while (count > 0) { \ unsigned short sval; \ CHECK_SCHEDULER(); \ GET_VAL(sval, iter); \ EMU8000_SMLD_WRITE(emu, sval); \ if (rec->voices > 1) { \ CHECK_SCHEDULER(); \ GET_VAL(sval, iter); \ EMU8000_SMRD_WRITE(emu, sval); \ } \ count--; \ } \ } while (0) /* * copy the interleaved data can be done easily by using * DMA "left" and "right" channels on emu8k engine. */ static int emu8k_pcm_copy(struct snd_pcm_substream *subs, int voice, unsigned long pos, struct iov_iter *src, unsigned long count) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; /* convert to frames */ pos = bytes_to_frames(subs->runtime, pos); count = bytes_to_frames(subs->runtime, count); LOOP_WRITE(rec, pos, src, count); return 0; } static int emu8k_pcm_silence(struct snd_pcm_substream *subs, int voice, unsigned long pos, unsigned long count) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; /* convert to frames */ pos = bytes_to_frames(subs->runtime, pos); count = bytes_to_frames(subs->runtime, count); LOOP_WRITE(rec, pos, NULL, count); return 0; } #endif /* * allocate a memory block */ static int emu8k_pcm_hw_params(struct snd_pcm_substream *subs, struct snd_pcm_hw_params *hw_params) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; if (rec->block) { /* reallocation - release the old block */ snd_util_mem_free(rec->emu->memhdr, rec->block); rec->block = NULL; } rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4; rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes); if (! rec->block) return -ENOMEM; rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */ /* at least dma_bytes must be set for non-interleaved mode */ subs->dma_buffer.bytes = params_buffer_bytes(hw_params); return 0; } /* * free the memory block */ static int emu8k_pcm_hw_free(struct snd_pcm_substream *subs) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; if (rec->block) { int ch; for (ch = 0; ch < rec->voices; ch++) stop_voice(rec, ch); // to be sure if (rec->dram_opened) emu8k_close_dram(rec->emu); snd_util_mem_free(rec->emu->memhdr, rec->block); rec->block = NULL; } return 0; } /* */ static int emu8k_pcm_prepare(struct snd_pcm_substream *subs) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate); rec->last_ptr = 0; rec->period_pos = 0; rec->buf_size = subs->runtime->buffer_size; rec->period_size = subs->runtime->period_size; rec->voices = subs->runtime->channels; rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE; if (rec->voices > 1) rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE; if (rec->voices > 1) { rec->panning[0] = 0xff; rec->panning[1] = 0x00; } else rec->panning[0] = 0x80; if (! rec->dram_opened) { int err, i, ch; snd_emux_terminate_all(rec->emu->emu); err = emu8k_open_dram_for_pcm(rec->emu, rec->voices); if (err) return err; rec->dram_opened = 1; /* clear loop blanks */ snd_emu8000_write_wait(rec->emu, 0); EMU8000_SMALW_WRITE(rec->emu, rec->offset); for (i = 0; i < LOOP_BLANK_SIZE; i++) EMU8000_SMLD_WRITE(rec->emu, 0); for (ch = 0; ch < rec->voices; ch++) { EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size); for (i = 0; i < LOOP_BLANK_SIZE; i++) EMU8000_SMLD_WRITE(rec->emu, 0); } } setup_voice(rec, 0); if (rec->voices > 1) setup_voice(rec, 1); return 0; } static snd_pcm_uframes_t emu8k_pcm_pointer(struct snd_pcm_substream *subs) { struct snd_emu8k_pcm *rec = subs->runtime->private_data; if (rec->running) return emu8k_get_curpos(rec, 0); return 0; } static const struct snd_pcm_ops emu8k_pcm_ops = { .open = emu8k_pcm_open, .close = emu8k_pcm_close, .hw_params = emu8k_pcm_hw_params, .hw_free = emu8k_pcm_hw_free, .prepare = emu8k_pcm_prepare, .trigger = emu8k_pcm_trigger, .pointer = emu8k_pcm_pointer, .copy = emu8k_pcm_copy, .fill_silence = emu8k_pcm_silence, }; static void snd_emu8000_pcm_free(struct snd_pcm *pcm) { struct snd_emu8000 *emu = pcm->private_data; emu->pcm = NULL; } int snd_emu8000_pcm_new(struct snd_card *card, struct snd_emu8000 *emu, int index) { struct snd_pcm *pcm; int err; err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm); if (err < 0) return err; pcm->private_data = emu; pcm->private_free = snd_emu8000_pcm_free; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops); emu->pcm = pcm; snd_device_register(card, pcm); return 0; }
linux-master
sound/isa/sb/emu8000_pcm.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for SoundBlaster 1.0/2.0/Pro soundcards and compatible * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/ioport.h> #include <linux/module.h> #include <sound/core.h> #include <sound/sb.h> #include <sound/opl3.h> #include <sound/initval.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Sound Blaster 1.0/2.0/Pro"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3 */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for Sound Blaster soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for Sound Blaster soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable Sound Blaster soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for SB8 driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for SB8 driver."); module_param_hw_array(dma8, int, dma, NULL, 0444); MODULE_PARM_DESC(dma8, "8-bit DMA # for SB8 driver."); struct snd_sb8 { struct resource *fm_res; /* used to block FM i/o region for legacy cards */ struct snd_sb *chip; }; static irqreturn_t snd_sb8_interrupt(int irq, void *dev_id) { struct snd_sb *chip = dev_id; if (chip->open & SB_OPEN_PCM) { return snd_sb8dsp_interrupt(chip); } else { return snd_sb8dsp_midi_interrupt(chip); } } static int snd_sb8_match(struct device *pdev, unsigned int dev) { if (!enable[dev]) return 0; if (irq[dev] == SNDRV_AUTO_IRQ) { dev_err(pdev, "please specify irq\n"); return 0; } if (dma8[dev] == SNDRV_AUTO_DMA) { dev_err(pdev, "please specify dma8\n"); return 0; } return 1; } static int snd_sb8_probe(struct device *pdev, unsigned int dev) { struct snd_sb *chip; struct snd_card *card; struct snd_sb8 *acard; struct snd_opl3 *opl3; int err; err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_sb8), &card); if (err < 0) return err; acard = card->private_data; /* * Block the 0x388 port to avoid PnP conflicts. * No need to check this value after request_region, * as we never do anything with it. */ acard->fm_res = devm_request_region(card->dev, 0x388, 4, "SoundBlaster FM"); if (port[dev] != SNDRV_AUTO_PORT) { err = snd_sbdsp_create(card, port[dev], irq[dev], snd_sb8_interrupt, dma8[dev], -1, SB_HW_AUTO, &chip); if (err < 0) return err; } else { /* auto-probe legacy ports */ static const unsigned long possible_ports[] = { 0x220, 0x240, 0x260, }; int i; for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { err = snd_sbdsp_create(card, possible_ports[i], irq[dev], snd_sb8_interrupt, dma8[dev], -1, SB_HW_AUTO, &chip); if (err >= 0) { port[dev] = possible_ports[i]; break; } } if (i >= ARRAY_SIZE(possible_ports)) return -EINVAL; } acard->chip = chip; if (chip->hardware >= SB_HW_16) { if (chip->hardware == SB_HW_ALS100) snd_printk(KERN_WARNING "ALS100 chip detected at 0x%lx, try snd-als100 module\n", port[dev]); else snd_printk(KERN_WARNING "SB 16 chip detected at 0x%lx, try snd-sb16 module\n", port[dev]); return -ENODEV; } err = snd_sb8dsp_pcm(chip, 0); if (err < 0) return err; err = snd_sbmixer_new(chip); if (err < 0) return err; if (chip->hardware == SB_HW_10 || chip->hardware == SB_HW_20) { err = snd_opl3_create(card, chip->port + 8, 0, OPL3_HW_AUTO, 1, &opl3); if (err < 0) snd_printk(KERN_WARNING "sb8: no OPL device at 0x%lx\n", chip->port + 8); } else { err = snd_opl3_create(card, chip->port, chip->port + 2, OPL3_HW_AUTO, 1, &opl3); if (err < 0) { snd_printk(KERN_WARNING "sb8: no OPL device at 0x%lx-0x%lx\n", chip->port, chip->port + 2); } } if (err >= 0) { err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } err = snd_sb8dsp_midi(chip, 0); if (err < 0) return err; strcpy(card->driver, chip->hardware == SB_HW_PRO ? "SB Pro" : "SB8"); strcpy(card->shortname, chip->name); sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d", chip->name, chip->port, irq[dev], dma8[dev]); err = snd_card_register(card); if (err < 0) return err; dev_set_drvdata(pdev, card); return 0; } #ifdef CONFIG_PM static int snd_sb8_suspend(struct device *dev, unsigned int n, pm_message_t state) { struct snd_card *card = dev_get_drvdata(dev); struct snd_sb8 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); snd_sbmixer_suspend(chip); return 0; } static int snd_sb8_resume(struct device *dev, unsigned int n) { struct snd_card *card = dev_get_drvdata(dev); struct snd_sb8 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_sbdsp_reset(chip); snd_sbmixer_resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif #define DEV_NAME "sb8" static struct isa_driver snd_sb8_driver = { .match = snd_sb8_match, .probe = snd_sb8_probe, #ifdef CONFIG_PM .suspend = snd_sb8_suspend, .resume = snd_sb8_resume, #endif .driver = { .name = DEV_NAME }, }; module_isa_driver(snd_sb8_driver, SNDRV_CARDS);
linux-master
sound/isa/sb/sb8.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Uros Bizjak <[email protected]> * * Lowlevel routines for control of Sound Blaster cards */ #include <linux/delay.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/slab.h> #include <linux/ioport.h> #include <linux/module.h> #include <linux/io.h> #include <sound/core.h> #include <sound/sb.h> #include <sound/initval.h> #include <asm/dma.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards"); MODULE_LICENSE("GPL"); #define BUSY_LOOPS 100000 #undef IO_DEBUG int snd_sbdsp_command(struct snd_sb *chip, unsigned char val) { int i; #ifdef IO_DEBUG snd_printk(KERN_DEBUG "command 0x%x\n", val); #endif for (i = BUSY_LOOPS; i; i--) if ((inb(SBP(chip, STATUS)) & 0x80) == 0) { outb(val, SBP(chip, COMMAND)); return 1; } snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val); return 0; } int snd_sbdsp_get_byte(struct snd_sb *chip) { int val; int i; for (i = BUSY_LOOPS; i; i--) { if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { val = inb(SBP(chip, READ)); #ifdef IO_DEBUG snd_printk(KERN_DEBUG "get_byte 0x%x\n", val); #endif return val; } } snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port); return -ENODEV; } int snd_sbdsp_reset(struct snd_sb *chip) { int i; outb(1, SBP(chip, RESET)); udelay(10); outb(0, SBP(chip, RESET)); udelay(30); for (i = BUSY_LOOPS; i; i--) if (inb(SBP(chip, DATA_AVAIL)) & 0x80) { if (inb(SBP(chip, READ)) == 0xaa) return 0; else break; } snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port); return -ENODEV; } static int snd_sbdsp_version(struct snd_sb * chip) { unsigned int result; snd_sbdsp_command(chip, SB_DSP_GET_VERSION); result = (short) snd_sbdsp_get_byte(chip) << 8; result |= (short) snd_sbdsp_get_byte(chip); return result; } static int snd_sbdsp_probe(struct snd_sb * chip) { int version; int major, minor; char *str; unsigned long flags; /* * initialization sequence */ spin_lock_irqsave(&chip->reg_lock, flags); if (snd_sbdsp_reset(chip) < 0) { spin_unlock_irqrestore(&chip->reg_lock, flags); return -ENODEV; } version = snd_sbdsp_version(chip); if (version < 0) { spin_unlock_irqrestore(&chip->reg_lock, flags); return -ENODEV; } spin_unlock_irqrestore(&chip->reg_lock, flags); major = version >> 8; minor = version & 0xff; snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n", chip->port, major, minor); switch (chip->hardware) { case SB_HW_AUTO: switch (major) { case 1: chip->hardware = SB_HW_10; str = "1.0"; break; case 2: if (minor) { chip->hardware = SB_HW_201; str = "2.01+"; } else { chip->hardware = SB_HW_20; str = "2.0"; } break; case 3: chip->hardware = SB_HW_PRO; str = "Pro"; break; case 4: chip->hardware = SB_HW_16; str = "16"; break; default: snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n", chip->port, major, minor); return -ENODEV; } break; case SB_HW_ALS100: str = "16 (ALS-100)"; break; case SB_HW_ALS4000: str = "16 (ALS-4000)"; break; case SB_HW_DT019X: str = "(DT019X/ALS007)"; break; case SB_HW_CS5530: str = "16 (CS5530)"; break; case SB_HW_JAZZ16: str = "Pro (Jazz16)"; break; default: return -ENODEV; } sprintf(chip->name, "Sound Blaster %s", str); chip->version = (major << 8) | minor; return 0; } int snd_sbdsp_create(struct snd_card *card, unsigned long port, int irq, irq_handler_t irq_handler, int dma8, int dma16, unsigned short hardware, struct snd_sb **r_chip) { struct snd_sb *chip; int err; if (snd_BUG_ON(!r_chip)) return -EINVAL; *r_chip = NULL; chip = devm_kzalloc(card->dev, sizeof(*chip), GFP_KERNEL); if (!chip) return -ENOMEM; spin_lock_init(&chip->reg_lock); spin_lock_init(&chip->open_lock); spin_lock_init(&chip->midi_input_lock); spin_lock_init(&chip->mixer_lock); chip->irq = -1; chip->dma8 = -1; chip->dma16 = -1; chip->port = port; if (devm_request_irq(card->dev, irq, irq_handler, (hardware == SB_HW_ALS4000 || hardware == SB_HW_CS5530) ? IRQF_SHARED : 0, "SoundBlaster", (void *) chip)) { snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq); return -EBUSY; } chip->irq = irq; card->sync_irq = chip->irq; if (hardware == SB_HW_ALS4000) goto __skip_allocation; chip->res_port = devm_request_region(card->dev, port, 16, "SoundBlaster"); if (!chip->res_port) { snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port); return -EBUSY; } #ifdef CONFIG_ISA if (dma8 >= 0 && snd_devm_request_dma(card->dev, dma8, "SoundBlaster - 8bit")) { snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8); return -EBUSY; } chip->dma8 = dma8; if (dma16 >= 0) { if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) { /* no duplex */ dma16 = -1; } else if (snd_devm_request_dma(card->dev, dma16, "SoundBlaster - 16bit")) { snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16); return -EBUSY; } } chip->dma16 = dma16; #endif __skip_allocation: chip->card = card; chip->hardware = hardware; err = snd_sbdsp_probe(chip); if (err < 0) return err; *r_chip = chip; return 0; } EXPORT_SYMBOL(snd_sbdsp_command); EXPORT_SYMBOL(snd_sbdsp_get_byte); EXPORT_SYMBOL(snd_sbdsp_reset); EXPORT_SYMBOL(snd_sbdsp_create); /* sb_mixer.c */ EXPORT_SYMBOL(snd_sbmixer_write); EXPORT_SYMBOL(snd_sbmixer_read); EXPORT_SYMBOL(snd_sbmixer_new); EXPORT_SYMBOL(snd_sbmixer_add_ctl); #ifdef CONFIG_PM EXPORT_SYMBOL(snd_sbmixer_suspend); EXPORT_SYMBOL(snd_sbmixer_resume); #endif
linux-master
sound/isa/sb/sb_common.c
/* * jazz16.c - driver for Media Vision Jazz16 based soundcards. * Copyright (C) 2009 Krzysztof Helt <[email protected]> * Based on patches posted by Rask Ingemann Lambertsen and Rene Herman. * Based on OSS Sound Blaster driver. * * This file is subject to the terms and conditions of the GNU General Public * License. See the file COPYING in the main directory of this archive for * more details. * */ #include <linux/init.h> #include <linux/module.h> #include <linux/io.h> #include <linux/delay.h> #include <asm/dma.h> #include <linux/isa.h> #include <sound/core.h> #include <sound/mpu401.h> #include <sound/opl3.h> #include <sound/sb.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #define PFX "jazz16: " MODULE_DESCRIPTION("Media Vision Jazz16"); MODULE_AUTHOR("Krzysztof Helt <[email protected]>"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static unsigned long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static unsigned long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for Media Vision Jazz16 based soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for Media Vision Jazz16 based soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable Media Vision Jazz16 based soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for jazz16 driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for jazz16 driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for jazz16 driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for jazz16 driver."); module_param_hw_array(dma8, int, dma, NULL, 0444); MODULE_PARM_DESC(dma8, "DMA8 # for jazz16 driver."); module_param_hw_array(dma16, int, dma, NULL, 0444); MODULE_PARM_DESC(dma16, "DMA16 # for jazz16 driver."); #define SB_JAZZ16_WAKEUP 0xaf #define SB_JAZZ16_SET_PORTS 0x50 #define SB_DSP_GET_JAZZ_BRD_REV 0xfa #define SB_JAZZ16_SET_DMAINTR 0xfb #define SB_DSP_GET_JAZZ_MODEL 0xfe struct snd_card_jazz16 { struct snd_sb *chip; }; static irqreturn_t jazz16_interrupt(int irq, void *chip) { return snd_sb8dsp_interrupt(chip); } static int jazz16_configure_ports(unsigned long port, unsigned long mpu_port, int idx) { unsigned char val; if (!request_region(0x201, 1, "jazz16 config")) { snd_printk(KERN_ERR "config port region is already in use.\n"); return -EBUSY; } outb(SB_JAZZ16_WAKEUP - idx, 0x201); udelay(100); outb(SB_JAZZ16_SET_PORTS + idx, 0x201); udelay(100); val = port & 0x70; val |= (mpu_port & 0x30) >> 4; outb(val, 0x201); release_region(0x201, 1); return 0; } static int jazz16_detect_board(unsigned long port, unsigned long mpu_port) { int err; int val; struct snd_sb chip; if (!request_region(port, 0x10, "jazz16")) { snd_printk(KERN_ERR "I/O port region is already in use.\n"); return -EBUSY; } /* just to call snd_sbdsp_command/reset/get_byte() */ chip.port = port; err = snd_sbdsp_reset(&chip); if (err < 0) for (val = 0; val < 4; val++) { err = jazz16_configure_ports(port, mpu_port, val); if (err < 0) break; err = snd_sbdsp_reset(&chip); if (!err) break; } if (err < 0) { err = -ENODEV; goto err_unmap; } if (!snd_sbdsp_command(&chip, SB_DSP_GET_JAZZ_BRD_REV)) { err = -EBUSY; goto err_unmap; } val = snd_sbdsp_get_byte(&chip); if (val >= 0x30) snd_sbdsp_get_byte(&chip); if ((val & 0xf0) != 0x10) { err = -ENODEV; goto err_unmap; } if (!snd_sbdsp_command(&chip, SB_DSP_GET_JAZZ_MODEL)) { err = -EBUSY; goto err_unmap; } snd_sbdsp_get_byte(&chip); err = snd_sbdsp_get_byte(&chip); snd_printd("Media Vision Jazz16 board detected: rev 0x%x, model 0x%x\n", val, err); err = 0; err_unmap: release_region(port, 0x10); return err; } static int jazz16_configure_board(struct snd_sb *chip, int mpu_irq) { static const unsigned char jazz_irq_bits[] = { 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6 }; static const unsigned char jazz_dma_bits[] = { 0, 1, 0, 2, 0, 3, 0, 4 }; if (jazz_dma_bits[chip->dma8] == 0 || jazz_dma_bits[chip->dma16] == 0 || jazz_irq_bits[chip->irq] == 0) return -EINVAL; if (!snd_sbdsp_command(chip, SB_JAZZ16_SET_DMAINTR)) return -EBUSY; if (!snd_sbdsp_command(chip, jazz_dma_bits[chip->dma8] | (jazz_dma_bits[chip->dma16] << 4))) return -EBUSY; if (!snd_sbdsp_command(chip, jazz_irq_bits[chip->irq] | (jazz_irq_bits[mpu_irq] << 4))) return -EBUSY; return 0; } static int snd_jazz16_match(struct device *devptr, unsigned int dev) { if (!enable[dev]) return 0; if (port[dev] == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR "please specify port\n"); return 0; } else if (port[dev] == 0x200 || (port[dev] & ~0x270)) { snd_printk(KERN_ERR "incorrect port specified\n"); return 0; } if (dma8[dev] != SNDRV_AUTO_DMA && dma8[dev] != 1 && dma8[dev] != 3) { snd_printk(KERN_ERR "dma8 must be 1 or 3\n"); return 0; } if (dma16[dev] != SNDRV_AUTO_DMA && dma16[dev] != 5 && dma16[dev] != 7) { snd_printk(KERN_ERR "dma16 must be 5 or 7\n"); return 0; } if (mpu_port[dev] != SNDRV_AUTO_PORT && (mpu_port[dev] & ~0x030) != 0x300) { snd_printk(KERN_ERR "incorrect mpu_port specified\n"); return 0; } if (mpu_irq[dev] != SNDRV_AUTO_DMA && mpu_irq[dev] != 2 && mpu_irq[dev] != 3 && mpu_irq[dev] != 5 && mpu_irq[dev] != 7) { snd_printk(KERN_ERR "mpu_irq must be 2, 3, 5 or 7\n"); return 0; } return 1; } static int snd_jazz16_probe(struct device *devptr, unsigned int dev) { struct snd_card *card; struct snd_card_jazz16 *jazz16; struct snd_sb *chip; struct snd_opl3 *opl3; static const int possible_irqs[] = {2, 3, 5, 7, 9, 10, 15, -1}; static const int possible_dmas8[] = {1, 3, -1}; static const int possible_dmas16[] = {5, 7, -1}; int err, xirq, xdma8, xdma16, xmpu_port, xmpu_irq; err = snd_devm_card_new(devptr, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_card_jazz16), &card); if (err < 0) return err; jazz16 = card->private_data; xirq = irq[dev]; if (xirq == SNDRV_AUTO_IRQ) { xirq = snd_legacy_find_free_irq(possible_irqs); if (xirq < 0) { snd_printk(KERN_ERR "unable to find a free IRQ\n"); return -EBUSY; } } xdma8 = dma8[dev]; if (xdma8 == SNDRV_AUTO_DMA) { xdma8 = snd_legacy_find_free_dma(possible_dmas8); if (xdma8 < 0) { snd_printk(KERN_ERR "unable to find a free DMA8\n"); return -EBUSY; } } xdma16 = dma16[dev]; if (xdma16 == SNDRV_AUTO_DMA) { xdma16 = snd_legacy_find_free_dma(possible_dmas16); if (xdma16 < 0) { snd_printk(KERN_ERR "unable to find a free DMA16\n"); return -EBUSY; } } xmpu_port = mpu_port[dev]; if (xmpu_port == SNDRV_AUTO_PORT) xmpu_port = 0; err = jazz16_detect_board(port[dev], xmpu_port); if (err < 0) { printk(KERN_ERR "Media Vision Jazz16 board not detected\n"); return err; } err = snd_sbdsp_create(card, port[dev], irq[dev], jazz16_interrupt, dma8[dev], dma16[dev], SB_HW_JAZZ16, &chip); if (err < 0) return err; xmpu_irq = mpu_irq[dev]; if (xmpu_irq == SNDRV_AUTO_IRQ || mpu_port[dev] == SNDRV_AUTO_PORT) xmpu_irq = 0; err = jazz16_configure_board(chip, xmpu_irq); if (err < 0) { printk(KERN_ERR "Media Vision Jazz16 configuration failed\n"); return err; } jazz16->chip = chip; strcpy(card->driver, "jazz16"); strcpy(card->shortname, "Media Vision Jazz16"); sprintf(card->longname, "Media Vision Jazz16 at 0x%lx, irq %d, dma8 %d, dma16 %d", port[dev], xirq, xdma8, xdma16); err = snd_sb8dsp_pcm(chip, 0); if (err < 0) return err; err = snd_sbmixer_new(chip); if (err < 0) return err; err = snd_opl3_create(card, chip->port, chip->port + 2, OPL3_HW_AUTO, 1, &opl3); if (err < 0) snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n", chip->port, chip->port + 2); else { err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) { if (mpu_irq[dev] == SNDRV_AUTO_IRQ) mpu_irq[dev] = -1; if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_port[dev], 0, mpu_irq[dev], NULL) < 0) snd_printk(KERN_ERR "no MPU-401 device at 0x%lx\n", mpu_port[dev]); } err = snd_card_register(card); if (err < 0) return err; dev_set_drvdata(devptr, card); return 0; } #ifdef CONFIG_PM static int snd_jazz16_suspend(struct device *pdev, unsigned int n, pm_message_t state) { struct snd_card *card = dev_get_drvdata(pdev); struct snd_card_jazz16 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); snd_sbmixer_suspend(chip); return 0; } static int snd_jazz16_resume(struct device *pdev, unsigned int n) { struct snd_card *card = dev_get_drvdata(pdev); struct snd_card_jazz16 *acard = card->private_data; struct snd_sb *chip = acard->chip; snd_sbdsp_reset(chip); snd_sbmixer_resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct isa_driver snd_jazz16_driver = { .match = snd_jazz16_match, .probe = snd_jazz16_probe, #ifdef CONFIG_PM .suspend = snd_jazz16_suspend, .resume = snd_jazz16_resume, #endif .driver = { .name = "jazz16" }, }; module_isa_driver(snd_jazz16_driver, SNDRV_CARDS);
linux-master
sound/isa/sb/jazz16.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Patch routines for the emu8000 (AWE32/64) * * Copyright (C) 1999 Steve Ratcliffe * Copyright (C) 1999-2000 Takashi Iwai <[email protected]> */ #include "emu8000_local.h" #include <linux/sched/signal.h> #include <linux/uaccess.h> #include <linux/moduleparam.h> static int emu8000_reset_addr; module_param(emu8000_reset_addr, int, 0444); MODULE_PARM_DESC(emu8000_reset_addr, "reset write address at each time (makes slowdown)"); /* * Open up channels. */ static int snd_emu8000_open_dma(struct snd_emu8000 *emu, int write) { int i; /* reserve all 30 voices for loading */ for (i = 0; i < EMU8000_DRAM_VOICES; i++) { snd_emux_lock_voice(emu->emu, i); snd_emu8000_dma_chan(emu, i, write); } /* assign voice 31 and 32 to ROM */ EMU8000_VTFT_WRITE(emu, 30, 0); EMU8000_PSST_WRITE(emu, 30, 0x1d8); EMU8000_CSL_WRITE(emu, 30, 0x1e0); EMU8000_CCCA_WRITE(emu, 30, 0x1d8); EMU8000_VTFT_WRITE(emu, 31, 0); EMU8000_PSST_WRITE(emu, 31, 0x1d8); EMU8000_CSL_WRITE(emu, 31, 0x1e0); EMU8000_CCCA_WRITE(emu, 31, 0x1d8); return 0; } /* * Close all dram channels. */ static void snd_emu8000_close_dma(struct snd_emu8000 *emu) { int i; for (i = 0; i < EMU8000_DRAM_VOICES; i++) { snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE); snd_emux_unlock_voice(emu->emu, i); } } /* */ #define BLANK_LOOP_START 4 #define BLANK_LOOP_END 8 #define BLANK_LOOP_SIZE 12 #define BLANK_HEAD_SIZE 48 /* * Read a word from userland, taking care of conversions from * 8bit samples etc. */ static unsigned short read_word(const void __user *buf, int offset, int mode) { unsigned short c; if (mode & SNDRV_SFNT_SAMPLE_8BITS) { unsigned char cc; get_user(cc, (unsigned char __user *)buf + offset); c = cc << 8; /* convert 8bit -> 16bit */ } else { #ifdef SNDRV_LITTLE_ENDIAN get_user(c, (unsigned short __user *)buf + offset); #else unsigned short cc; get_user(cc, (unsigned short __user *)buf + offset); c = swab16(cc); #endif } if (mode & SNDRV_SFNT_SAMPLE_UNSIGNED) c ^= 0x8000; /* unsigned -> signed */ return c; } /* */ static void snd_emu8000_write_wait(struct snd_emu8000 *emu) { while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) { schedule_timeout_interruptible(1); if (signal_pending(current)) break; } } /* * write sample word data * * You should not have to keep resetting the address each time * as the chip is supposed to step on the next address automatically. * It mostly does, but during writes of some samples at random it * completely loses words (every one in 16 roughly but with no * obvious pattern). * * This is therefore much slower than need be, but is at least * working. */ static inline void write_word(struct snd_emu8000 *emu, int *offset, unsigned short data) { if (emu8000_reset_addr) { if (emu8000_reset_addr > 1) snd_emu8000_write_wait(emu); EMU8000_SMALW_WRITE(emu, *offset); } EMU8000_SMLD_WRITE(emu, data); *offset += 1; } /* * Write the sample to EMU800 memory. This routine is invoked out of * the generic soundfont routines as a callback. */ int snd_emu8000_sample_new(struct snd_emux *rec, struct snd_sf_sample *sp, struct snd_util_memhdr *hdr, const void __user *data, long count) { int i; int rc; int offset; int truesize; int dram_offset, dram_start; struct snd_emu8000 *emu; emu = rec->hw; if (snd_BUG_ON(!sp)) return -EINVAL; if (sp->v.size == 0) return 0; /* be sure loop points start < end */ if (sp->v.loopstart > sp->v.loopend) swap(sp->v.loopstart, sp->v.loopend); /* compute true data size to be loaded */ truesize = sp->v.size; if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP)) truesize += sp->v.loopend - sp->v.loopstart; if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) truesize += BLANK_LOOP_SIZE; sp->block = snd_util_mem_alloc(hdr, truesize * 2); if (sp->block == NULL) { /*snd_printd("EMU8000: out of memory\n");*/ /* not ENOMEM (for compatibility) */ return -ENOSPC; } if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS) { if (!access_ok(data, sp->v.size)) return -EFAULT; } else { if (!access_ok(data, sp->v.size * 2)) return -EFAULT; } /* recalculate address offset */ sp->v.end -= sp->v.start; sp->v.loopstart -= sp->v.start; sp->v.loopend -= sp->v.start; sp->v.start = 0; /* dram position (in word) -- mem_offset is byte */ dram_offset = EMU8000_DRAM_OFFSET + (sp->block->offset >> 1); dram_start = dram_offset; /* set the total size (store onto obsolete checksum value) */ sp->v.truesize = truesize * 2; /* in bytes */ snd_emux_terminate_all(emu->emu); rc = snd_emu8000_open_dma(emu, EMU8000_RAM_WRITE); if (rc) return rc; /* Set the address to start writing at */ snd_emu8000_write_wait(emu); EMU8000_SMALW_WRITE(emu, dram_offset); /*snd_emu8000_init_fm(emu);*/ #if 0 /* first block - write 48 samples for silence */ if (! sp->block->offset) { for (i = 0; i < BLANK_HEAD_SIZE; i++) { write_word(emu, &dram_offset, 0); } } #endif offset = 0; for (i = 0; i < sp->v.size; i++) { unsigned short s; s = read_word(data, offset, sp->v.mode_flags); offset++; write_word(emu, &dram_offset, s); /* we may take too long time in this loop. * so give controls back to kernel if needed. */ cond_resched(); if (i == sp->v.loopend && (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP|SNDRV_SFNT_SAMPLE_REVERSE_LOOP))) { int looplen = sp->v.loopend - sp->v.loopstart; int k; /* copy reverse loop */ for (k = 1; k <= looplen; k++) { s = read_word(data, offset - k, sp->v.mode_flags); write_word(emu, &dram_offset, s); } if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_BIDIR_LOOP) { sp->v.loopend += looplen; } else { sp->v.loopstart += looplen; sp->v.loopend += looplen; } sp->v.end += looplen; } } /* if no blank loop is attached in the sample, add it */ if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) { for (i = 0; i < BLANK_LOOP_SIZE; i++) { write_word(emu, &dram_offset, 0); } if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_SINGLESHOT) { sp->v.loopstart = sp->v.end + BLANK_LOOP_START; sp->v.loopend = sp->v.end + BLANK_LOOP_END; } } /* add dram offset */ sp->v.start += dram_start; sp->v.end += dram_start; sp->v.loopstart += dram_start; sp->v.loopend += dram_start; snd_emu8000_close_dma(emu); snd_emu8000_init_fm(emu); return 0; } /* * free a sample block */ int snd_emu8000_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp, struct snd_util_memhdr *hdr) { if (sp->block) { snd_util_mem_free(hdr, sp->block); sp->block = NULL; } return 0; } /* * sample_reset callback - terminate voices */ void snd_emu8000_sample_reset(struct snd_emux *rec) { snd_emux_terminate_all(rec); }
linux-master
sound/isa/sb/emu8000_patch.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * and (c) 1999 Steve Ratcliffe <[email protected]> * Copyright (C) 1999-2000 Takashi Iwai <[email protected]> * * Routines for control of EMU8000 chip */ #include <linux/wait.h> #include <linux/sched/signal.h> #include <linux/slab.h> #include <linux/ioport.h> #include <linux/export.h> #include <linux/delay.h> #include <linux/io.h> #include <sound/core.h> #include <sound/emu8000.h> #include <sound/emu8000_reg.h> #include <linux/uaccess.h> #include <linux/init.h> #include <sound/control.h> #include <sound/initval.h> /* * emu8000 register controls */ /* * The following routines read and write registers on the emu8000. They * should always be called via the EMU8000*READ/WRITE macros and never * directly. The macros handle the port number and command word. */ /* Write a word */ void snd_emu8000_poke(struct snd_emu8000 *emu, unsigned int port, unsigned int reg, unsigned int val) { unsigned long flags; spin_lock_irqsave(&emu->reg_lock, flags); if (reg != emu->last_reg) { outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */ emu->last_reg = reg; } outw((unsigned short)val, port); /* Send data */ spin_unlock_irqrestore(&emu->reg_lock, flags); } /* Read a word */ unsigned short snd_emu8000_peek(struct snd_emu8000 *emu, unsigned int port, unsigned int reg) { unsigned short res; unsigned long flags; spin_lock_irqsave(&emu->reg_lock, flags); if (reg != emu->last_reg) { outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */ emu->last_reg = reg; } res = inw(port); /* Read data */ spin_unlock_irqrestore(&emu->reg_lock, flags); return res; } /* Write a double word */ void snd_emu8000_poke_dw(struct snd_emu8000 *emu, unsigned int port, unsigned int reg, unsigned int val) { unsigned long flags; spin_lock_irqsave(&emu->reg_lock, flags); if (reg != emu->last_reg) { outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */ emu->last_reg = reg; } outw((unsigned short)val, port); /* Send low word of data */ outw((unsigned short)(val>>16), port+2); /* Send high word of data */ spin_unlock_irqrestore(&emu->reg_lock, flags); } /* Read a double word */ unsigned int snd_emu8000_peek_dw(struct snd_emu8000 *emu, unsigned int port, unsigned int reg) { unsigned short low; unsigned int res; unsigned long flags; spin_lock_irqsave(&emu->reg_lock, flags); if (reg != emu->last_reg) { outw((unsigned short)reg, EMU8000_PTR(emu)); /* Set register */ emu->last_reg = reg; } low = inw(port); /* Read low word of data */ res = low + (inw(port+2) << 16); spin_unlock_irqrestore(&emu->reg_lock, flags); return res; } /* * Set up / close a channel to be used for DMA. */ /*exported*/ void snd_emu8000_dma_chan(struct snd_emu8000 *emu, int ch, int mode) { unsigned right_bit = (mode & EMU8000_RAM_RIGHT) ? 0x01000000 : 0; mode &= EMU8000_RAM_MODE_MASK; if (mode == EMU8000_RAM_CLOSE) { EMU8000_CCCA_WRITE(emu, ch, 0); EMU8000_DCYSUSV_WRITE(emu, ch, 0x807F); return; } EMU8000_DCYSUSV_WRITE(emu, ch, 0x80); EMU8000_VTFT_WRITE(emu, ch, 0); EMU8000_CVCF_WRITE(emu, ch, 0); EMU8000_PTRX_WRITE(emu, ch, 0x40000000); EMU8000_CPF_WRITE(emu, ch, 0x40000000); EMU8000_PSST_WRITE(emu, ch, 0); EMU8000_CSL_WRITE(emu, ch, 0); if (mode == EMU8000_RAM_WRITE) /* DMA write */ EMU8000_CCCA_WRITE(emu, ch, 0x06000000 | right_bit); else /* DMA read */ EMU8000_CCCA_WRITE(emu, ch, 0x04000000 | right_bit); } /* */ static void snd_emu8000_read_wait(struct snd_emu8000 *emu) { while ((EMU8000_SMALR_READ(emu) & 0x80000000) != 0) { schedule_timeout_interruptible(1); if (signal_pending(current)) break; } } /* */ static void snd_emu8000_write_wait(struct snd_emu8000 *emu) { while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) { schedule_timeout_interruptible(1); if (signal_pending(current)) break; } } /* * detect a card at the given port */ static int snd_emu8000_detect(struct snd_emu8000 *emu) { /* Initialise */ EMU8000_HWCF1_WRITE(emu, 0x0059); EMU8000_HWCF2_WRITE(emu, 0x0020); EMU8000_HWCF3_WRITE(emu, 0x0000); /* Check for a recognisable emu8000 */ /* if ((EMU8000_U1_READ(emu) & 0x000f) != 0x000c) return -ENODEV; */ if ((EMU8000_HWCF1_READ(emu) & 0x007e) != 0x0058) return -ENODEV; if ((EMU8000_HWCF2_READ(emu) & 0x0003) != 0x0003) return -ENODEV; snd_printdd("EMU8000 [0x%lx]: Synth chip found\n", emu->port1); return 0; } /* * intiailize audio channels */ static void init_audio(struct snd_emu8000 *emu) { int ch; /* turn off envelope engines */ for (ch = 0; ch < EMU8000_CHANNELS; ch++) EMU8000_DCYSUSV_WRITE(emu, ch, 0x80); /* reset all other parameters to zero */ for (ch = 0; ch < EMU8000_CHANNELS; ch++) { EMU8000_ENVVOL_WRITE(emu, ch, 0); EMU8000_ENVVAL_WRITE(emu, ch, 0); EMU8000_DCYSUS_WRITE(emu, ch, 0); EMU8000_ATKHLDV_WRITE(emu, ch, 0); EMU8000_LFO1VAL_WRITE(emu, ch, 0); EMU8000_ATKHLD_WRITE(emu, ch, 0); EMU8000_LFO2VAL_WRITE(emu, ch, 0); EMU8000_IP_WRITE(emu, ch, 0); EMU8000_IFATN_WRITE(emu, ch, 0); EMU8000_PEFE_WRITE(emu, ch, 0); EMU8000_FMMOD_WRITE(emu, ch, 0); EMU8000_TREMFRQ_WRITE(emu, ch, 0); EMU8000_FM2FRQ2_WRITE(emu, ch, 0); EMU8000_PTRX_WRITE(emu, ch, 0); EMU8000_VTFT_WRITE(emu, ch, 0); EMU8000_PSST_WRITE(emu, ch, 0); EMU8000_CSL_WRITE(emu, ch, 0); EMU8000_CCCA_WRITE(emu, ch, 0); } for (ch = 0; ch < EMU8000_CHANNELS; ch++) { EMU8000_CPF_WRITE(emu, ch, 0); EMU8000_CVCF_WRITE(emu, ch, 0); } } /* * initialize DMA address */ static void init_dma(struct snd_emu8000 *emu) { EMU8000_SMALR_WRITE(emu, 0); EMU8000_SMARR_WRITE(emu, 0); EMU8000_SMALW_WRITE(emu, 0); EMU8000_SMARW_WRITE(emu, 0); } /* * initialization arrays; from ADIP */ static const unsigned short init1[128] = { 0x03ff, 0x0030, 0x07ff, 0x0130, 0x0bff, 0x0230, 0x0fff, 0x0330, 0x13ff, 0x0430, 0x17ff, 0x0530, 0x1bff, 0x0630, 0x1fff, 0x0730, 0x23ff, 0x0830, 0x27ff, 0x0930, 0x2bff, 0x0a30, 0x2fff, 0x0b30, 0x33ff, 0x0c30, 0x37ff, 0x0d30, 0x3bff, 0x0e30, 0x3fff, 0x0f30, 0x43ff, 0x0030, 0x47ff, 0x0130, 0x4bff, 0x0230, 0x4fff, 0x0330, 0x53ff, 0x0430, 0x57ff, 0x0530, 0x5bff, 0x0630, 0x5fff, 0x0730, 0x63ff, 0x0830, 0x67ff, 0x0930, 0x6bff, 0x0a30, 0x6fff, 0x0b30, 0x73ff, 0x0c30, 0x77ff, 0x0d30, 0x7bff, 0x0e30, 0x7fff, 0x0f30, 0x83ff, 0x0030, 0x87ff, 0x0130, 0x8bff, 0x0230, 0x8fff, 0x0330, 0x93ff, 0x0430, 0x97ff, 0x0530, 0x9bff, 0x0630, 0x9fff, 0x0730, 0xa3ff, 0x0830, 0xa7ff, 0x0930, 0xabff, 0x0a30, 0xafff, 0x0b30, 0xb3ff, 0x0c30, 0xb7ff, 0x0d30, 0xbbff, 0x0e30, 0xbfff, 0x0f30, 0xc3ff, 0x0030, 0xc7ff, 0x0130, 0xcbff, 0x0230, 0xcfff, 0x0330, 0xd3ff, 0x0430, 0xd7ff, 0x0530, 0xdbff, 0x0630, 0xdfff, 0x0730, 0xe3ff, 0x0830, 0xe7ff, 0x0930, 0xebff, 0x0a30, 0xefff, 0x0b30, 0xf3ff, 0x0c30, 0xf7ff, 0x0d30, 0xfbff, 0x0e30, 0xffff, 0x0f30, }; static const unsigned short init2[128] = { 0x03ff, 0x8030, 0x07ff, 0x8130, 0x0bff, 0x8230, 0x0fff, 0x8330, 0x13ff, 0x8430, 0x17ff, 0x8530, 0x1bff, 0x8630, 0x1fff, 0x8730, 0x23ff, 0x8830, 0x27ff, 0x8930, 0x2bff, 0x8a30, 0x2fff, 0x8b30, 0x33ff, 0x8c30, 0x37ff, 0x8d30, 0x3bff, 0x8e30, 0x3fff, 0x8f30, 0x43ff, 0x8030, 0x47ff, 0x8130, 0x4bff, 0x8230, 0x4fff, 0x8330, 0x53ff, 0x8430, 0x57ff, 0x8530, 0x5bff, 0x8630, 0x5fff, 0x8730, 0x63ff, 0x8830, 0x67ff, 0x8930, 0x6bff, 0x8a30, 0x6fff, 0x8b30, 0x73ff, 0x8c30, 0x77ff, 0x8d30, 0x7bff, 0x8e30, 0x7fff, 0x8f30, 0x83ff, 0x8030, 0x87ff, 0x8130, 0x8bff, 0x8230, 0x8fff, 0x8330, 0x93ff, 0x8430, 0x97ff, 0x8530, 0x9bff, 0x8630, 0x9fff, 0x8730, 0xa3ff, 0x8830, 0xa7ff, 0x8930, 0xabff, 0x8a30, 0xafff, 0x8b30, 0xb3ff, 0x8c30, 0xb7ff, 0x8d30, 0xbbff, 0x8e30, 0xbfff, 0x8f30, 0xc3ff, 0x8030, 0xc7ff, 0x8130, 0xcbff, 0x8230, 0xcfff, 0x8330, 0xd3ff, 0x8430, 0xd7ff, 0x8530, 0xdbff, 0x8630, 0xdfff, 0x8730, 0xe3ff, 0x8830, 0xe7ff, 0x8930, 0xebff, 0x8a30, 0xefff, 0x8b30, 0xf3ff, 0x8c30, 0xf7ff, 0x8d30, 0xfbff, 0x8e30, 0xffff, 0x8f30, }; static const unsigned short init3[128] = { 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5, 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x8F7C, 0x167E, 0xF254, 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x8BAA, 0x1B6D, 0xF234, 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x86E7, 0x229E, 0xF224, 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x87F6, 0x2C28, 0xF254, 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x8F02, 0x1341, 0xF264, 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x8FA9, 0x3EB5, 0xF294, 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0xC4C3, 0x3EBB, 0xC5C3, 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x8671, 0x14FD, 0x8287, 0x3EBC, 0xE610, 0x3EC8, 0x8C7B, 0x031A, 0x87E6, 0x3EC8, 0x86F7, 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x821F, 0x3ECA, 0x8386, 0x3EC1, 0x8C03, 0x3EC9, 0x831E, 0x3ECA, 0x8C4C, 0x3EBF, 0x8C55, 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x8EAD, 0x3EC8, 0xD308, 0x3EC2, 0x8F7E, 0x3ECB, 0x8219, 0x3ECB, 0xD26E, 0x3EC5, 0x831F, 0x3EC6, 0xC308, 0x3EC3, 0xB2FF, 0x3EC9, 0x8265, 0x3EC9, 0x8319, 0x1342, 0xD36E, 0x3EC7, 0xB3FF, 0x0000, 0x8365, 0x1420, 0x9570, }; static const unsigned short init4[128] = { 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5, 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x0F7C, 0x167E, 0x7254, 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x0BAA, 0x1B6D, 0x7234, 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x06E7, 0x229E, 0x7224, 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x07F6, 0x2C28, 0x7254, 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x0F02, 0x1341, 0x7264, 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x0FA9, 0x3EB5, 0x7294, 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0x44C3, 0x3EBB, 0x45C3, 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x0671, 0x14FD, 0x0287, 0x3EBC, 0xE610, 0x3EC8, 0x0C7B, 0x031A, 0x07E6, 0x3EC8, 0x86F7, 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x021F, 0x3ECA, 0x0386, 0x3EC1, 0x0C03, 0x3EC9, 0x031E, 0x3ECA, 0x8C4C, 0x3EBF, 0x0C55, 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x0EAD, 0x3EC8, 0xD308, 0x3EC2, 0x8F7E, 0x3ECB, 0x0219, 0x3ECB, 0xD26E, 0x3EC5, 0x031F, 0x3EC6, 0xC308, 0x3EC3, 0x32FF, 0x3EC9, 0x0265, 0x3EC9, 0x8319, 0x1342, 0xD36E, 0x3EC7, 0x33FF, 0x0000, 0x8365, 0x1420, 0x9570, }; /* send an initialization array * Taken from the oss driver, not obvious from the doc how this * is meant to work */ static void send_array(struct snd_emu8000 *emu, const unsigned short *data, int size) { int i; const unsigned short *p; p = data; for (i = 0; i < size; i++, p++) EMU8000_INIT1_WRITE(emu, i, *p); for (i = 0; i < size; i++, p++) EMU8000_INIT2_WRITE(emu, i, *p); for (i = 0; i < size; i++, p++) EMU8000_INIT3_WRITE(emu, i, *p); for (i = 0; i < size; i++, p++) EMU8000_INIT4_WRITE(emu, i, *p); } /* * Send initialization arrays to start up, this just follows the * initialisation sequence in the adip. */ static void init_arrays(struct snd_emu8000 *emu) { send_array(emu, init1, ARRAY_SIZE(init1)/4); msleep((1024 * 1000) / 44100); /* wait for 1024 clocks */ send_array(emu, init2, ARRAY_SIZE(init2)/4); send_array(emu, init3, ARRAY_SIZE(init3)/4); EMU8000_HWCF4_WRITE(emu, 0); EMU8000_HWCF5_WRITE(emu, 0x83); EMU8000_HWCF6_WRITE(emu, 0x8000); send_array(emu, init4, ARRAY_SIZE(init4)/4); } #define UNIQUE_ID1 0xa5b9 #define UNIQUE_ID2 0x9d53 /* * Size the onboard memory. * This is written so as not to need arbitrary delays after the write. It * seems that the only way to do this is to use the one channel and keep * reallocating between read and write. */ static void size_dram(struct snd_emu8000 *emu) { int i, size; if (emu->dram_checked) return; size = 0; /* write out a magic number */ snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_WRITE); snd_emu8000_dma_chan(emu, 1, EMU8000_RAM_READ); EMU8000_SMALW_WRITE(emu, EMU8000_DRAM_OFFSET); EMU8000_SMLD_WRITE(emu, UNIQUE_ID1); snd_emu8000_init_fm(emu); /* This must really be here and not 2 lines back even */ snd_emu8000_write_wait(emu); /* * Detect first 512 KiB. If a write succeeds at the beginning of a * 512 KiB page we assume that the whole page is there. */ EMU8000_SMALR_WRITE(emu, EMU8000_DRAM_OFFSET); EMU8000_SMLD_READ(emu); /* discard stale data */ if (EMU8000_SMLD_READ(emu) != UNIQUE_ID1) goto skip_detect; /* No RAM */ snd_emu8000_read_wait(emu); for (size = 512 * 1024; size < EMU8000_MAX_DRAM; size += 512 * 1024) { /* Write a unique data on the test address. * if the address is out of range, the data is written on * 0x200000(=EMU8000_DRAM_OFFSET). Then the id word is * changed by this data. */ /*snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_WRITE);*/ EMU8000_SMALW_WRITE(emu, EMU8000_DRAM_OFFSET + (size>>1)); EMU8000_SMLD_WRITE(emu, UNIQUE_ID2); snd_emu8000_write_wait(emu); /* * read the data on the just written DRAM address * if not the same then we have reached the end of ram. */ /*snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_READ);*/ EMU8000_SMALR_WRITE(emu, EMU8000_DRAM_OFFSET + (size>>1)); /*snd_emu8000_read_wait(emu);*/ EMU8000_SMLD_READ(emu); /* discard stale data */ if (EMU8000_SMLD_READ(emu) != UNIQUE_ID2) break; /* no memory at this address */ snd_emu8000_read_wait(emu); /* * If it is the same it could be that the address just * wraps back to the beginning; so check to see if the * initial value has been overwritten. */ EMU8000_SMALR_WRITE(emu, EMU8000_DRAM_OFFSET); EMU8000_SMLD_READ(emu); /* discard stale data */ if (EMU8000_SMLD_READ(emu) != UNIQUE_ID1) break; /* we must have wrapped around */ snd_emu8000_read_wait(emu); /* Otherwise, it's valid memory. */ } skip_detect: /* wait until FULL bit in SMAxW register is false */ for (i = 0; i < 10000; i++) { if ((EMU8000_SMALW_READ(emu) & 0x80000000) == 0) break; schedule_timeout_interruptible(1); if (signal_pending(current)) break; } snd_emu8000_dma_chan(emu, 0, EMU8000_RAM_CLOSE); snd_emu8000_dma_chan(emu, 1, EMU8000_RAM_CLOSE); pr_info("EMU8000 [0x%lx]: %d KiB on-board DRAM detected\n", emu->port1, size/1024); emu->mem_size = size; emu->dram_checked = 1; } /* * Initiailise the FM section. You have to do this to use sample RAM * and therefore lose 2 voices. */ /*exported*/ void snd_emu8000_init_fm(struct snd_emu8000 *emu) { unsigned long flags; /* Initialize the last two channels for DRAM refresh and producing the reverb and chorus effects for Yamaha OPL-3 synthesizer */ /* 31: FM left channel, 0xffffe0-0xffffe8 */ EMU8000_DCYSUSV_WRITE(emu, 30, 0x80); EMU8000_PSST_WRITE(emu, 30, 0xFFFFFFE0); /* full left */ EMU8000_CSL_WRITE(emu, 30, 0x00FFFFE8 | (emu->fm_chorus_depth << 24)); EMU8000_PTRX_WRITE(emu, 30, (emu->fm_reverb_depth << 8)); EMU8000_CPF_WRITE(emu, 30, 0); EMU8000_CCCA_WRITE(emu, 30, 0x00FFFFE3); /* 32: FM right channel, 0xfffff0-0xfffff8 */ EMU8000_DCYSUSV_WRITE(emu, 31, 0x80); EMU8000_PSST_WRITE(emu, 31, 0x00FFFFF0); /* full right */ EMU8000_CSL_WRITE(emu, 31, 0x00FFFFF8 | (emu->fm_chorus_depth << 24)); EMU8000_PTRX_WRITE(emu, 31, (emu->fm_reverb_depth << 8)); EMU8000_CPF_WRITE(emu, 31, 0x8000); EMU8000_CCCA_WRITE(emu, 31, 0x00FFFFF3); snd_emu8000_poke((emu), EMU8000_DATA0(emu), EMU8000_CMD(1, (30)), 0); spin_lock_irqsave(&emu->reg_lock, flags); while (!(inw(EMU8000_PTR(emu)) & 0x1000)) ; while ((inw(EMU8000_PTR(emu)) & 0x1000)) ; spin_unlock_irqrestore(&emu->reg_lock, flags); snd_emu8000_poke((emu), EMU8000_DATA0(emu), EMU8000_CMD(1, (30)), 0x4828); /* this is really odd part.. */ outb(0x3C, EMU8000_PTR(emu)); outb(0, EMU8000_DATA1(emu)); /* skew volume & cutoff */ EMU8000_VTFT_WRITE(emu, 30, 0x8000FFFF); EMU8000_VTFT_WRITE(emu, 31, 0x8000FFFF); } /* * The main initialization routine. */ static void snd_emu8000_init_hw(struct snd_emu8000 *emu) { int i; emu->last_reg = 0xffff; /* reset the last register index */ /* initialize hardware configuration */ EMU8000_HWCF1_WRITE(emu, 0x0059); EMU8000_HWCF2_WRITE(emu, 0x0020); /* disable audio; this seems to reduce a clicking noise a bit.. */ EMU8000_HWCF3_WRITE(emu, 0); /* initialize audio channels */ init_audio(emu); /* initialize DMA */ init_dma(emu); /* initialize init arrays */ init_arrays(emu); /* * Initialize the FM section of the AWE32, this is needed * for DRAM refresh as well */ snd_emu8000_init_fm(emu); /* terminate all voices */ for (i = 0; i < EMU8000_DRAM_VOICES; i++) EMU8000_DCYSUSV_WRITE(emu, 0, 0x807F); /* check DRAM memory size */ size_dram(emu); /* enable audio */ EMU8000_HWCF3_WRITE(emu, 0x4); /* set equzlier, chorus and reverb modes */ snd_emu8000_update_equalizer(emu); snd_emu8000_update_chorus_mode(emu); snd_emu8000_update_reverb_mode(emu); } /*---------------------------------------------------------------- * Bass/Treble Equalizer *----------------------------------------------------------------*/ static const unsigned short bass_parm[12][3] = { {0xD26A, 0xD36A, 0x0000}, /* -12 dB */ {0xD25B, 0xD35B, 0x0000}, /* -8 */ {0xD24C, 0xD34C, 0x0000}, /* -6 */ {0xD23D, 0xD33D, 0x0000}, /* -4 */ {0xD21F, 0xD31F, 0x0000}, /* -2 */ {0xC208, 0xC308, 0x0001}, /* 0 (HW default) */ {0xC219, 0xC319, 0x0001}, /* +2 */ {0xC22A, 0xC32A, 0x0001}, /* +4 */ {0xC24C, 0xC34C, 0x0001}, /* +6 */ {0xC26E, 0xC36E, 0x0001}, /* +8 */ {0xC248, 0xC384, 0x0002}, /* +10 */ {0xC26A, 0xC36A, 0x0002}, /* +12 dB */ }; static const unsigned short treble_parm[12][9] = { {0x821E, 0xC26A, 0x031E, 0xC36A, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, /* -12 dB */ {0x821E, 0xC25B, 0x031E, 0xC35B, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, {0x821E, 0xC24C, 0x031E, 0xC34C, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, {0x821E, 0xC23D, 0x031E, 0xC33D, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, {0x821E, 0xC21F, 0x031E, 0xC31F, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, {0x821E, 0xD208, 0x031E, 0xD308, 0x021E, 0xD208, 0x831E, 0xD308, 0x0002}, {0x821E, 0xD208, 0x031E, 0xD308, 0x021D, 0xD219, 0x831D, 0xD319, 0x0002}, {0x821E, 0xD208, 0x031E, 0xD308, 0x021C, 0xD22A, 0x831C, 0xD32A, 0x0002}, {0x821E, 0xD208, 0x031E, 0xD308, 0x021A, 0xD24C, 0x831A, 0xD34C, 0x0002}, {0x821E, 0xD208, 0x031E, 0xD308, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +8 (HW default) */ {0x821D, 0xD219, 0x031D, 0xD319, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, {0x821C, 0xD22A, 0x031C, 0xD32A, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002} /* +12 dB */ }; /* * set Emu8000 digital equalizer; from 0 to 11 [-12dB - 12dB] */ /*exported*/ void snd_emu8000_update_equalizer(struct snd_emu8000 *emu) { unsigned short w; int bass = emu->bass_level; int treble = emu->treble_level; if (bass < 0 || bass > 11 || treble < 0 || treble > 11) return; EMU8000_INIT4_WRITE(emu, 0x01, bass_parm[bass][0]); EMU8000_INIT4_WRITE(emu, 0x11, bass_parm[bass][1]); EMU8000_INIT3_WRITE(emu, 0x11, treble_parm[treble][0]); EMU8000_INIT3_WRITE(emu, 0x13, treble_parm[treble][1]); EMU8000_INIT3_WRITE(emu, 0x1b, treble_parm[treble][2]); EMU8000_INIT4_WRITE(emu, 0x07, treble_parm[treble][3]); EMU8000_INIT4_WRITE(emu, 0x0b, treble_parm[treble][4]); EMU8000_INIT4_WRITE(emu, 0x0d, treble_parm[treble][5]); EMU8000_INIT4_WRITE(emu, 0x17, treble_parm[treble][6]); EMU8000_INIT4_WRITE(emu, 0x19, treble_parm[treble][7]); w = bass_parm[bass][2] + treble_parm[treble][8]; EMU8000_INIT4_WRITE(emu, 0x15, (unsigned short)(w + 0x0262)); EMU8000_INIT4_WRITE(emu, 0x1d, (unsigned short)(w + 0x8362)); } /*---------------------------------------------------------------- * Chorus mode control *----------------------------------------------------------------*/ /* * chorus mode parameters */ #define SNDRV_EMU8000_CHORUS_1 0 #define SNDRV_EMU8000_CHORUS_2 1 #define SNDRV_EMU8000_CHORUS_3 2 #define SNDRV_EMU8000_CHORUS_4 3 #define SNDRV_EMU8000_CHORUS_FEEDBACK 4 #define SNDRV_EMU8000_CHORUS_FLANGER 5 #define SNDRV_EMU8000_CHORUS_SHORTDELAY 6 #define SNDRV_EMU8000_CHORUS_SHORTDELAY2 7 #define SNDRV_EMU8000_CHORUS_PREDEFINED 8 /* user can define chorus modes up to 32 */ #define SNDRV_EMU8000_CHORUS_NUMBERS 32 struct soundfont_chorus_fx { unsigned short feedback; /* feedback level (0xE600-0xE6FF) */ unsigned short delay_offset; /* delay (0-0x0DA3) [1/44100 sec] */ unsigned short lfo_depth; /* LFO depth (0xBC00-0xBCFF) */ unsigned int delay; /* right delay (0-0xFFFFFFFF) [1/256/44100 sec] */ unsigned int lfo_freq; /* LFO freq LFO freq (0-0xFFFFFFFF) */ }; /* 5 parameters for each chorus mode; 3 x 16bit, 2 x 32bit */ static char chorus_defined[SNDRV_EMU8000_CHORUS_NUMBERS]; static struct soundfont_chorus_fx chorus_parm[SNDRV_EMU8000_CHORUS_NUMBERS] = { {0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */ {0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */ {0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */ {0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */ {0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */ {0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */ {0xE600, 0x0B06, 0xBC00, 0x0006E000, 0x00000083}, /* short delay */ {0xE6C0, 0x0B06, 0xBC00, 0x0006E000, 0x00000083}, /* short delay + feedback */ }; /*exported*/ int snd_emu8000_load_chorus_fx(struct snd_emu8000 *emu, int mode, const void __user *buf, long len) { struct soundfont_chorus_fx rec; if (mode < SNDRV_EMU8000_CHORUS_PREDEFINED || mode >= SNDRV_EMU8000_CHORUS_NUMBERS) { snd_printk(KERN_WARNING "invalid chorus mode %d for uploading\n", mode); return -EINVAL; } if (len < (long)sizeof(rec) || copy_from_user(&rec, buf, sizeof(rec))) return -EFAULT; chorus_parm[mode] = rec; chorus_defined[mode] = 1; return 0; } /*exported*/ void snd_emu8000_update_chorus_mode(struct snd_emu8000 *emu) { int effect = emu->chorus_mode; if (effect < 0 || effect >= SNDRV_EMU8000_CHORUS_NUMBERS || (effect >= SNDRV_EMU8000_CHORUS_PREDEFINED && !chorus_defined[effect])) return; EMU8000_INIT3_WRITE(emu, 0x09, chorus_parm[effect].feedback); EMU8000_INIT3_WRITE(emu, 0x0c, chorus_parm[effect].delay_offset); EMU8000_INIT4_WRITE(emu, 0x03, chorus_parm[effect].lfo_depth); EMU8000_HWCF4_WRITE(emu, chorus_parm[effect].delay); EMU8000_HWCF5_WRITE(emu, chorus_parm[effect].lfo_freq); EMU8000_HWCF6_WRITE(emu, 0x8000); EMU8000_HWCF7_WRITE(emu, 0x0000); } /*---------------------------------------------------------------- * Reverb mode control *----------------------------------------------------------------*/ /* * reverb mode parameters */ #define SNDRV_EMU8000_REVERB_ROOM1 0 #define SNDRV_EMU8000_REVERB_ROOM2 1 #define SNDRV_EMU8000_REVERB_ROOM3 2 #define SNDRV_EMU8000_REVERB_HALL1 3 #define SNDRV_EMU8000_REVERB_HALL2 4 #define SNDRV_EMU8000_REVERB_PLATE 5 #define SNDRV_EMU8000_REVERB_DELAY 6 #define SNDRV_EMU8000_REVERB_PANNINGDELAY 7 #define SNDRV_EMU8000_REVERB_PREDEFINED 8 /* user can define reverb modes up to 32 */ #define SNDRV_EMU8000_REVERB_NUMBERS 32 struct soundfont_reverb_fx { unsigned short parms[28]; }; /* reverb mode settings; write the following 28 data of 16 bit length * on the corresponding ports in the reverb_cmds array */ static char reverb_defined[SNDRV_EMU8000_CHORUS_NUMBERS]; static struct soundfont_reverb_fx reverb_parm[SNDRV_EMU8000_REVERB_NUMBERS] = { {{ /* room 1 */ 0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4, 0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516, 0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528, }}, {{ /* room 2 */ 0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284, 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548, 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528, }}, {{ /* room 3 */ 0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284, 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516, 0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A, }}, {{ /* hall 1 */ 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284, 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548, 0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529, }}, {{ /* hall 2 */ 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254, 0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3, 0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528, }}, {{ /* plate */ 0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548, 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528, }}, {{ /* delay */ 0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204, 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500, 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, }}, {{ /* panning delay */ 0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204, 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500, 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, }}, }; enum { DATA1, DATA2 }; #define AWE_INIT1(c) EMU8000_CMD(2,c), DATA1 #define AWE_INIT2(c) EMU8000_CMD(2,c), DATA2 #define AWE_INIT3(c) EMU8000_CMD(3,c), DATA1 #define AWE_INIT4(c) EMU8000_CMD(3,c), DATA2 static struct reverb_cmd_pair { unsigned short cmd, port; } reverb_cmds[28] = { {AWE_INIT1(0x03)}, {AWE_INIT1(0x05)}, {AWE_INIT4(0x1F)}, {AWE_INIT1(0x07)}, {AWE_INIT2(0x14)}, {AWE_INIT2(0x16)}, {AWE_INIT1(0x0F)}, {AWE_INIT1(0x17)}, {AWE_INIT1(0x1F)}, {AWE_INIT2(0x07)}, {AWE_INIT2(0x0F)}, {AWE_INIT2(0x17)}, {AWE_INIT2(0x1D)}, {AWE_INIT2(0x1F)}, {AWE_INIT3(0x01)}, {AWE_INIT3(0x03)}, {AWE_INIT1(0x09)}, {AWE_INIT1(0x0B)}, {AWE_INIT1(0x11)}, {AWE_INIT1(0x13)}, {AWE_INIT1(0x19)}, {AWE_INIT1(0x1B)}, {AWE_INIT2(0x01)}, {AWE_INIT2(0x03)}, {AWE_INIT2(0x09)}, {AWE_INIT2(0x0B)}, {AWE_INIT2(0x11)}, {AWE_INIT2(0x13)}, }; /*exported*/ int snd_emu8000_load_reverb_fx(struct snd_emu8000 *emu, int mode, const void __user *buf, long len) { struct soundfont_reverb_fx rec; if (mode < SNDRV_EMU8000_REVERB_PREDEFINED || mode >= SNDRV_EMU8000_REVERB_NUMBERS) { snd_printk(KERN_WARNING "invalid reverb mode %d for uploading\n", mode); return -EINVAL; } if (len < (long)sizeof(rec) || copy_from_user(&rec, buf, sizeof(rec))) return -EFAULT; reverb_parm[mode] = rec; reverb_defined[mode] = 1; return 0; } /*exported*/ void snd_emu8000_update_reverb_mode(struct snd_emu8000 *emu) { int effect = emu->reverb_mode; int i; if (effect < 0 || effect >= SNDRV_EMU8000_REVERB_NUMBERS || (effect >= SNDRV_EMU8000_REVERB_PREDEFINED && !reverb_defined[effect])) return; for (i = 0; i < 28; i++) { int port; if (reverb_cmds[i].port == DATA1) port = EMU8000_DATA1(emu); else port = EMU8000_DATA2(emu); snd_emu8000_poke(emu, port, reverb_cmds[i].cmd, reverb_parm[effect].parms[i]); } } /*---------------------------------------------------------------- * mixer interface *----------------------------------------------------------------*/ /* * bass/treble */ static int mixer_bass_treble_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 11; return 0; } static int mixer_bass_treble_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = kcontrol->private_value ? emu->treble_level : emu->bass_level; return 0; } static int mixer_bass_treble_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned short val1; val1 = ucontrol->value.integer.value[0] % 12; spin_lock_irqsave(&emu->control_lock, flags); if (kcontrol->private_value) { change = val1 != emu->treble_level; emu->treble_level = val1; } else { change = val1 != emu->bass_level; emu->bass_level = val1; } spin_unlock_irqrestore(&emu->control_lock, flags); snd_emu8000_update_equalizer(emu); return change; } static const struct snd_kcontrol_new mixer_bass_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Synth Tone Control - Bass", .info = mixer_bass_treble_info, .get = mixer_bass_treble_get, .put = mixer_bass_treble_put, .private_value = 0, }; static const struct snd_kcontrol_new mixer_treble_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Synth Tone Control - Treble", .info = mixer_bass_treble_info, .get = mixer_bass_treble_get, .put = mixer_bass_treble_put, .private_value = 1, }; /* * chorus/reverb mode */ static int mixer_chorus_reverb_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = kcontrol->private_value ? (SNDRV_EMU8000_CHORUS_NUMBERS-1) : (SNDRV_EMU8000_REVERB_NUMBERS-1); return 0; } static int mixer_chorus_reverb_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = kcontrol->private_value ? emu->chorus_mode : emu->reverb_mode; return 0; } static int mixer_chorus_reverb_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned short val1; spin_lock_irqsave(&emu->control_lock, flags); if (kcontrol->private_value) { val1 = ucontrol->value.integer.value[0] % SNDRV_EMU8000_CHORUS_NUMBERS; change = val1 != emu->chorus_mode; emu->chorus_mode = val1; } else { val1 = ucontrol->value.integer.value[0] % SNDRV_EMU8000_REVERB_NUMBERS; change = val1 != emu->reverb_mode; emu->reverb_mode = val1; } spin_unlock_irqrestore(&emu->control_lock, flags); if (change) { if (kcontrol->private_value) snd_emu8000_update_chorus_mode(emu); else snd_emu8000_update_reverb_mode(emu); } return change; } static const struct snd_kcontrol_new mixer_chorus_mode_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Chorus Mode", .info = mixer_chorus_reverb_info, .get = mixer_chorus_reverb_get, .put = mixer_chorus_reverb_put, .private_value = 1, }; static const struct snd_kcontrol_new mixer_reverb_mode_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Reverb Mode", .info = mixer_chorus_reverb_info, .get = mixer_chorus_reverb_get, .put = mixer_chorus_reverb_put, .private_value = 0, }; /* * FM OPL3 chorus/reverb depth */ static int mixer_fm_depth_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 255; return 0; } static int mixer_fm_depth_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = kcontrol->private_value ? emu->fm_chorus_depth : emu->fm_reverb_depth; return 0; } static int mixer_fm_depth_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_emu8000 *emu = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned short val1; val1 = ucontrol->value.integer.value[0] % 256; spin_lock_irqsave(&emu->control_lock, flags); if (kcontrol->private_value) { change = val1 != emu->fm_chorus_depth; emu->fm_chorus_depth = val1; } else { change = val1 != emu->fm_reverb_depth; emu->fm_reverb_depth = val1; } spin_unlock_irqrestore(&emu->control_lock, flags); if (change) snd_emu8000_init_fm(emu); return change; } static const struct snd_kcontrol_new mixer_fm_chorus_depth_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "FM Chorus Depth", .info = mixer_fm_depth_info, .get = mixer_fm_depth_get, .put = mixer_fm_depth_put, .private_value = 1, }; static const struct snd_kcontrol_new mixer_fm_reverb_depth_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "FM Reverb Depth", .info = mixer_fm_depth_info, .get = mixer_fm_depth_get, .put = mixer_fm_depth_put, .private_value = 0, }; static const struct snd_kcontrol_new *mixer_defs[EMU8000_NUM_CONTROLS] = { &mixer_bass_control, &mixer_treble_control, &mixer_chorus_mode_control, &mixer_reverb_mode_control, &mixer_fm_chorus_depth_control, &mixer_fm_reverb_depth_control, }; /* * create and attach mixer elements for WaveTable treble/bass controls */ static int snd_emu8000_create_mixer(struct snd_card *card, struct snd_emu8000 *emu) { struct snd_kcontrol *kctl; int i, err = 0; if (snd_BUG_ON(!emu || !card)) return -EINVAL; spin_lock_init(&emu->control_lock); memset(emu->controls, 0, sizeof(emu->controls)); for (i = 0; i < EMU8000_NUM_CONTROLS; i++) { kctl = snd_ctl_new1(mixer_defs[i], emu); err = snd_ctl_add(card, kctl); if (err < 0) goto __error; emu->controls[i] = kctl; } return 0; __error: for (i = 0; i < EMU8000_NUM_CONTROLS; i++) { if (emu->controls[i]) snd_ctl_remove(card, emu->controls[i]); } return err; } /* * initialize and register emu8000 synth device. */ int snd_emu8000_new(struct snd_card *card, int index, long port, int seq_ports, struct snd_seq_device **awe_ret) { struct snd_seq_device *awe; struct snd_emu8000 *hw; int err; if (awe_ret) *awe_ret = NULL; if (seq_ports <= 0) return 0; hw = devm_kzalloc(card->dev, sizeof(*hw), GFP_KERNEL); if (hw == NULL) return -ENOMEM; spin_lock_init(&hw->reg_lock); hw->index = index; hw->port1 = port; hw->port2 = port + 0x400; hw->port3 = port + 0x800; if (!devm_request_region(card->dev, hw->port1, 4, "Emu8000-1") || !devm_request_region(card->dev, hw->port2, 4, "Emu8000-2") || !devm_request_region(card->dev, hw->port3, 4, "Emu8000-3")) { snd_printk(KERN_ERR "sbawe: can't grab ports 0x%lx, 0x%lx, 0x%lx\n", hw->port1, hw->port2, hw->port3); return -EBUSY; } hw->mem_size = 0; hw->card = card; hw->seq_ports = seq_ports; hw->bass_level = 5; hw->treble_level = 9; hw->chorus_mode = 2; hw->reverb_mode = 4; hw->fm_chorus_depth = 0; hw->fm_reverb_depth = 0; if (snd_emu8000_detect(hw) < 0) return -ENODEV; snd_emu8000_init_hw(hw); err = snd_emu8000_create_mixer(card, hw); if (err < 0) return err; #if IS_ENABLED(CONFIG_SND_SEQUENCER) if (snd_seq_device_new(card, index, SNDRV_SEQ_DEV_ID_EMU8000, sizeof(struct snd_emu8000*), &awe) >= 0) { strcpy(awe->name, "EMU-8000"); *(struct snd_emu8000 **)SNDRV_SEQ_DEVICE_ARGPTR(awe) = hw; } #else awe = NULL; #endif if (awe_ret) *awe_ret = awe; return 0; } /* * exported stuff */ EXPORT_SYMBOL(snd_emu8000_poke); EXPORT_SYMBOL(snd_emu8000_peek); EXPORT_SYMBOL(snd_emu8000_poke_dw); EXPORT_SYMBOL(snd_emu8000_peek_dw); EXPORT_SYMBOL(snd_emu8000_dma_chan); EXPORT_SYMBOL(snd_emu8000_init_fm); EXPORT_SYMBOL(snd_emu8000_load_chorus_fx); EXPORT_SYMBOL(snd_emu8000_load_reverb_fx); EXPORT_SYMBOL(snd_emu8000_update_chorus_mode); EXPORT_SYMBOL(snd_emu8000_update_reverb_mode); EXPORT_SYMBOL(snd_emu8000_update_equalizer);
linux-master
sound/isa/sb/emu8000.c
/* The work is in msnd_pinnacle.c, just define MSND_CLASSIC before it. */ #define MSND_CLASSIC #include "msnd_pinnacle.c"
linux-master
sound/isa/msnd/msnd_classic.c
// SPDX-License-Identifier: GPL-2.0-or-later /********************************************************************* * * 2002/06/30 Karsten Wiese: * removed kernel-version dependencies. * ripped from linux kernel 2.4.18 (OSS Implementation) by me. * In the OSS Version, this file is compiled to a separate MODULE, * that is used by the pinnacle and the classic driver. * since there is no classic driver for alsa yet (i dont have a classic * & writing one blindfold is difficult) this file's object is statically * linked into the pinnacle-driver-module for now. look for the string * "uncomment this to make this a module again" * to do guess what. * * the following is a copy of the 2.4.18 OSS FREE file-heading comment: * * msnd.c - Driver Base * * Turtle Beach MultiSound Sound Card Driver for Linux * * Copyright (C) 1998 Andrew Veliath * ********************************************************************/ #include <linux/kernel.h> #include <linux/sched/signal.h> #include <linux/types.h> #include <linux/interrupt.h> #include <linux/io.h> #include <linux/fs.h> #include <linux/delay.h> #include <linux/module.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/pcm.h> #include <sound/pcm_params.h> #include "msnd.h" #define LOGNAME "msnd" void snd_msnd_init_queue(void __iomem *base, int start, int size) { writew(PCTODSP_BASED(start), base + JQS_wStart); writew(PCTODSP_OFFSET(size) - 1, base + JQS_wSize); writew(0, base + JQS_wHead); writew(0, base + JQS_wTail); } EXPORT_SYMBOL(snd_msnd_init_queue); static int snd_msnd_wait_TXDE(struct snd_msnd *dev) { unsigned int io = dev->io; int timeout = 1000; while (timeout-- > 0) if (inb(io + HP_ISR) & HPISR_TXDE) return 0; return -EIO; } static int snd_msnd_wait_HC0(struct snd_msnd *dev) { unsigned int io = dev->io; int timeout = 1000; while (timeout-- > 0) if (!(inb(io + HP_CVR) & HPCVR_HC)) return 0; return -EIO; } int snd_msnd_send_dsp_cmd(struct snd_msnd *dev, u8 cmd) { unsigned long flags; spin_lock_irqsave(&dev->lock, flags); if (snd_msnd_wait_HC0(dev) == 0) { outb(cmd, dev->io + HP_CVR); spin_unlock_irqrestore(&dev->lock, flags); return 0; } spin_unlock_irqrestore(&dev->lock, flags); snd_printd(KERN_ERR LOGNAME ": Send DSP command timeout\n"); return -EIO; } EXPORT_SYMBOL(snd_msnd_send_dsp_cmd); int snd_msnd_send_word(struct snd_msnd *dev, unsigned char high, unsigned char mid, unsigned char low) { unsigned int io = dev->io; if (snd_msnd_wait_TXDE(dev) == 0) { outb(high, io + HP_TXH); outb(mid, io + HP_TXM); outb(low, io + HP_TXL); return 0; } snd_printd(KERN_ERR LOGNAME ": Send host word timeout\n"); return -EIO; } EXPORT_SYMBOL(snd_msnd_send_word); int snd_msnd_upload_host(struct snd_msnd *dev, const u8 *bin, int len) { int i; if (len % 3 != 0) { snd_printk(KERN_ERR LOGNAME ": Upload host data not multiple of 3!\n"); return -EINVAL; } for (i = 0; i < len; i += 3) if (snd_msnd_send_word(dev, bin[i], bin[i + 1], bin[i + 2])) return -EIO; inb(dev->io + HP_RXL); inb(dev->io + HP_CVR); return 0; } EXPORT_SYMBOL(snd_msnd_upload_host); int snd_msnd_enable_irq(struct snd_msnd *dev) { unsigned long flags; if (dev->irq_ref++) return 0; snd_printdd(LOGNAME ": Enabling IRQ\n"); spin_lock_irqsave(&dev->lock, flags); if (snd_msnd_wait_TXDE(dev) == 0) { outb(inb(dev->io + HP_ICR) | HPICR_TREQ, dev->io + HP_ICR); if (dev->type == msndClassic) outb(dev->irqid, dev->io + HP_IRQM); outb(inb(dev->io + HP_ICR) & ~HPICR_TREQ, dev->io + HP_ICR); outb(inb(dev->io + HP_ICR) | HPICR_RREQ, dev->io + HP_ICR); enable_irq(dev->irq); snd_msnd_init_queue(dev->DSPQ, dev->dspq_data_buff, dev->dspq_buff_size); spin_unlock_irqrestore(&dev->lock, flags); return 0; } spin_unlock_irqrestore(&dev->lock, flags); snd_printd(KERN_ERR LOGNAME ": Enable IRQ failed\n"); return -EIO; } EXPORT_SYMBOL(snd_msnd_enable_irq); int snd_msnd_disable_irq(struct snd_msnd *dev) { unsigned long flags; if (--dev->irq_ref > 0) return 0; if (dev->irq_ref < 0) snd_printd(KERN_WARNING LOGNAME ": IRQ ref count is %d\n", dev->irq_ref); snd_printdd(LOGNAME ": Disabling IRQ\n"); spin_lock_irqsave(&dev->lock, flags); if (snd_msnd_wait_TXDE(dev) == 0) { outb(inb(dev->io + HP_ICR) & ~HPICR_RREQ, dev->io + HP_ICR); if (dev->type == msndClassic) outb(HPIRQ_NONE, dev->io + HP_IRQM); disable_irq(dev->irq); spin_unlock_irqrestore(&dev->lock, flags); return 0; } spin_unlock_irqrestore(&dev->lock, flags); snd_printd(KERN_ERR LOGNAME ": Disable IRQ failed\n"); return -EIO; } EXPORT_SYMBOL(snd_msnd_disable_irq); static inline long get_play_delay_jiffies(struct snd_msnd *chip, long size) { long tmp = (size * HZ * chip->play_sample_size) / 8; return tmp / (chip->play_sample_rate * chip->play_channels); } static void snd_msnd_dsp_write_flush(struct snd_msnd *chip) { if (!(chip->mode & FMODE_WRITE) || !test_bit(F_WRITING, &chip->flags)) return; set_bit(F_WRITEFLUSH, &chip->flags); /* interruptible_sleep_on_timeout( &chip->writeflush, get_play_delay_jiffies(&chip, chip->DAPF.len));*/ clear_bit(F_WRITEFLUSH, &chip->flags); if (!signal_pending(current)) schedule_timeout_interruptible( get_play_delay_jiffies(chip, chip->play_period_bytes)); clear_bit(F_WRITING, &chip->flags); } void snd_msnd_dsp_halt(struct snd_msnd *chip, struct file *file) { if ((file ? file->f_mode : chip->mode) & FMODE_READ) { clear_bit(F_READING, &chip->flags); snd_msnd_send_dsp_cmd(chip, HDEX_RECORD_STOP); snd_msnd_disable_irq(chip); if (file) { snd_printd(KERN_INFO LOGNAME ": Stopping read for %p\n", file); chip->mode &= ~FMODE_READ; } clear_bit(F_AUDIO_READ_INUSE, &chip->flags); } if ((file ? file->f_mode : chip->mode) & FMODE_WRITE) { if (test_bit(F_WRITING, &chip->flags)) { snd_msnd_dsp_write_flush(chip); snd_msnd_send_dsp_cmd(chip, HDEX_PLAY_STOP); } snd_msnd_disable_irq(chip); if (file) { snd_printd(KERN_INFO LOGNAME ": Stopping write for %p\n", file); chip->mode &= ~FMODE_WRITE; } clear_bit(F_AUDIO_WRITE_INUSE, &chip->flags); } } EXPORT_SYMBOL(snd_msnd_dsp_halt); int snd_msnd_DARQ(struct snd_msnd *chip, int bank) { int /*size, n,*/ timeout = 3; u16 wTmp; /* void *DAQD; */ /* Increment the tail and check for queue wrap */ wTmp = readw(chip->DARQ + JQS_wTail) + PCTODSP_OFFSET(DAQDS__size); if (wTmp > readw(chip->DARQ + JQS_wSize)) wTmp = 0; while (wTmp == readw(chip->DARQ + JQS_wHead) && timeout--) udelay(1); if (chip->capturePeriods == 2) { void __iomem *pDAQ = chip->mappedbase + DARQ_DATA_BUFF + bank * DAQDS__size + DAQDS_wStart; unsigned short offset = 0x3000 + chip->capturePeriodBytes; if (readw(pDAQ) != PCTODSP_BASED(0x3000)) offset = 0x3000; writew(PCTODSP_BASED(offset), pDAQ); } writew(wTmp, chip->DARQ + JQS_wTail); #if 0 /* Get our digital audio queue struct */ DAQD = bank * DAQDS__size + chip->mappedbase + DARQ_DATA_BUFF; /* Get length of data */ size = readw(DAQD + DAQDS_wSize); /* Read data from the head (unprotected bank 1 access okay since this is only called inside an interrupt) */ outb(HPBLKSEL_1, chip->io + HP_BLKS); n = msnd_fifo_write(&chip->DARF, (char *)(chip->base + bank * DAR_BUFF_SIZE), size, 0); if (n <= 0) { outb(HPBLKSEL_0, chip->io + HP_BLKS); return n; } outb(HPBLKSEL_0, chip->io + HP_BLKS); #endif return 1; } EXPORT_SYMBOL(snd_msnd_DARQ); int snd_msnd_DAPQ(struct snd_msnd *chip, int start) { u16 DAPQ_tail; int protect = start, nbanks = 0; void __iomem *DAQD; static int play_banks_submitted; /* unsigned long flags; spin_lock_irqsave(&chip->lock, flags); not necessary */ DAPQ_tail = readw(chip->DAPQ + JQS_wTail); while (DAPQ_tail != readw(chip->DAPQ + JQS_wHead) || start) { int bank_num = DAPQ_tail / PCTODSP_OFFSET(DAQDS__size); if (start) { start = 0; play_banks_submitted = 0; } /* Get our digital audio queue struct */ DAQD = bank_num * DAQDS__size + chip->mappedbase + DAPQ_DATA_BUFF; /* Write size of this bank */ writew(chip->play_period_bytes, DAQD + DAQDS_wSize); if (play_banks_submitted < 3) ++play_banks_submitted; else if (chip->playPeriods == 2) { unsigned short offset = chip->play_period_bytes; if (readw(DAQD + DAQDS_wStart) != PCTODSP_BASED(0x0)) offset = 0; writew(PCTODSP_BASED(offset), DAQD + DAQDS_wStart); } ++nbanks; /* Then advance the tail */ /* if (protect) snd_printd(KERN_INFO "B %X %lX\n", bank_num, xtime.tv_usec); */ DAPQ_tail = (++bank_num % 3) * PCTODSP_OFFSET(DAQDS__size); writew(DAPQ_tail, chip->DAPQ + JQS_wTail); /* Tell the DSP to play the bank */ snd_msnd_send_dsp_cmd(chip, HDEX_PLAY_START); if (protect) if (2 == bank_num) break; } /* if (protect) snd_printd(KERN_INFO "%lX\n", xtime.tv_usec); */ /* spin_unlock_irqrestore(&chip->lock, flags); not necessary */ return nbanks; } EXPORT_SYMBOL(snd_msnd_DAPQ); static void snd_msnd_play_reset_queue(struct snd_msnd *chip, unsigned int pcm_periods, unsigned int pcm_count) { int n; void __iomem *pDAQ = chip->mappedbase + DAPQ_DATA_BUFF; chip->last_playbank = -1; chip->playLimit = pcm_count * (pcm_periods - 1); chip->playPeriods = pcm_periods; writew(PCTODSP_OFFSET(0 * DAQDS__size), chip->DAPQ + JQS_wHead); writew(PCTODSP_OFFSET(0 * DAQDS__size), chip->DAPQ + JQS_wTail); chip->play_period_bytes = pcm_count; for (n = 0; n < pcm_periods; ++n, pDAQ += DAQDS__size) { writew(PCTODSP_BASED((u32)(pcm_count * n)), pDAQ + DAQDS_wStart); writew(0, pDAQ + DAQDS_wSize); writew(1, pDAQ + DAQDS_wFormat); writew(chip->play_sample_size, pDAQ + DAQDS_wSampleSize); writew(chip->play_channels, pDAQ + DAQDS_wChannels); writew(chip->play_sample_rate, pDAQ + DAQDS_wSampleRate); writew(HIMT_PLAY_DONE * 0x100 + n, pDAQ + DAQDS_wIntMsg); writew(n, pDAQ + DAQDS_wFlags); } } static void snd_msnd_capture_reset_queue(struct snd_msnd *chip, unsigned int pcm_periods, unsigned int pcm_count) { int n; void __iomem *pDAQ; /* unsigned long flags; */ /* snd_msnd_init_queue(chip->DARQ, DARQ_DATA_BUFF, DARQ_BUFF_SIZE); */ chip->last_recbank = 2; chip->captureLimit = pcm_count * (pcm_periods - 1); chip->capturePeriods = pcm_periods; writew(PCTODSP_OFFSET(0 * DAQDS__size), chip->DARQ + JQS_wHead); writew(PCTODSP_OFFSET(chip->last_recbank * DAQDS__size), chip->DARQ + JQS_wTail); #if 0 /* Critical section: bank 1 access. this is how the OSS driver does it:*/ spin_lock_irqsave(&chip->lock, flags); outb(HPBLKSEL_1, chip->io + HP_BLKS); memset_io(chip->mappedbase, 0, DAR_BUFF_SIZE * 3); outb(HPBLKSEL_0, chip->io + HP_BLKS); spin_unlock_irqrestore(&chip->lock, flags); #endif chip->capturePeriodBytes = pcm_count; snd_printdd("snd_msnd_capture_reset_queue() %i\n", pcm_count); pDAQ = chip->mappedbase + DARQ_DATA_BUFF; for (n = 0; n < pcm_periods; ++n, pDAQ += DAQDS__size) { u32 tmp = pcm_count * n; writew(PCTODSP_BASED(tmp + 0x3000), pDAQ + DAQDS_wStart); writew(pcm_count, pDAQ + DAQDS_wSize); writew(1, pDAQ + DAQDS_wFormat); writew(chip->capture_sample_size, pDAQ + DAQDS_wSampleSize); writew(chip->capture_channels, pDAQ + DAQDS_wChannels); writew(chip->capture_sample_rate, pDAQ + DAQDS_wSampleRate); writew(HIMT_RECORD_DONE * 0x100 + n, pDAQ + DAQDS_wIntMsg); writew(n, pDAQ + DAQDS_wFlags); } } static const struct snd_pcm_hardware snd_msnd_playback = { .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH, .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 0x3000, .period_bytes_min = 0x40, .period_bytes_max = 0x1800, .periods_min = 2, .periods_max = 3, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_msnd_capture = { .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH, .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 0x3000, .period_bytes_min = 0x40, .period_bytes_max = 0x1800, .periods_min = 2, .periods_max = 3, .fifo_size = 0, }; static int snd_msnd_playback_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_msnd *chip = snd_pcm_substream_chip(substream); set_bit(F_AUDIO_WRITE_INUSE, &chip->flags); clear_bit(F_WRITING, &chip->flags); snd_msnd_enable_irq(chip); runtime->dma_area = (__force void *)chip->mappedbase; runtime->dma_addr = chip->base; runtime->dma_bytes = 0x3000; chip->playback_substream = substream; runtime->hw = snd_msnd_playback; return 0; } static int snd_msnd_playback_close(struct snd_pcm_substream *substream) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); snd_msnd_disable_irq(chip); clear_bit(F_AUDIO_WRITE_INUSE, &chip->flags); return 0; } static int snd_msnd_playback_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) { int i; struct snd_msnd *chip = snd_pcm_substream_chip(substream); void __iomem *pDAQ = chip->mappedbase + DAPQ_DATA_BUFF; chip->play_sample_size = snd_pcm_format_width(params_format(params)); chip->play_channels = params_channels(params); chip->play_sample_rate = params_rate(params); for (i = 0; i < 3; ++i, pDAQ += DAQDS__size) { writew(chip->play_sample_size, pDAQ + DAQDS_wSampleSize); writew(chip->play_channels, pDAQ + DAQDS_wChannels); writew(chip->play_sample_rate, pDAQ + DAQDS_wSampleRate); } /* dont do this here: * snd_msnd_calibrate_adc(chip->play_sample_rate); */ return 0; } static int snd_msnd_playback_prepare(struct snd_pcm_substream *substream) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); unsigned int pcm_size = snd_pcm_lib_buffer_bytes(substream); unsigned int pcm_count = snd_pcm_lib_period_bytes(substream); unsigned int pcm_periods = pcm_size / pcm_count; snd_msnd_play_reset_queue(chip, pcm_periods, pcm_count); chip->playDMAPos = 0; return 0; } static int snd_msnd_playback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); int result = 0; if (cmd == SNDRV_PCM_TRIGGER_START) { snd_printdd("snd_msnd_playback_trigger(START)\n"); chip->banksPlayed = 0; set_bit(F_WRITING, &chip->flags); snd_msnd_DAPQ(chip, 1); } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { snd_printdd("snd_msnd_playback_trigger(STop)\n"); /* interrupt diagnostic, comment this out later */ clear_bit(F_WRITING, &chip->flags); snd_msnd_send_dsp_cmd(chip, HDEX_PLAY_STOP); } else { snd_printd(KERN_ERR "snd_msnd_playback_trigger(?????)\n"); result = -EINVAL; } snd_printdd("snd_msnd_playback_trigger() ENDE\n"); return result; } static snd_pcm_uframes_t snd_msnd_playback_pointer(struct snd_pcm_substream *substream) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); return bytes_to_frames(substream->runtime, chip->playDMAPos); } static const struct snd_pcm_ops snd_msnd_playback_ops = { .open = snd_msnd_playback_open, .close = snd_msnd_playback_close, .hw_params = snd_msnd_playback_hw_params, .prepare = snd_msnd_playback_prepare, .trigger = snd_msnd_playback_trigger, .pointer = snd_msnd_playback_pointer, .mmap = snd_pcm_lib_mmap_iomem, }; static int snd_msnd_capture_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_msnd *chip = snd_pcm_substream_chip(substream); set_bit(F_AUDIO_READ_INUSE, &chip->flags); snd_msnd_enable_irq(chip); runtime->dma_area = (__force void *)chip->mappedbase + 0x3000; runtime->dma_addr = chip->base + 0x3000; runtime->dma_bytes = 0x3000; memset(runtime->dma_area, 0, runtime->dma_bytes); chip->capture_substream = substream; runtime->hw = snd_msnd_capture; return 0; } static int snd_msnd_capture_close(struct snd_pcm_substream *substream) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); snd_msnd_disable_irq(chip); clear_bit(F_AUDIO_READ_INUSE, &chip->flags); return 0; } static int snd_msnd_capture_prepare(struct snd_pcm_substream *substream) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); unsigned int pcm_size = snd_pcm_lib_buffer_bytes(substream); unsigned int pcm_count = snd_pcm_lib_period_bytes(substream); unsigned int pcm_periods = pcm_size / pcm_count; snd_msnd_capture_reset_queue(chip, pcm_periods, pcm_count); chip->captureDMAPos = 0; return 0; } static int snd_msnd_capture_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_msnd *chip = snd_pcm_substream_chip(substream); if (cmd == SNDRV_PCM_TRIGGER_START) { chip->last_recbank = -1; set_bit(F_READING, &chip->flags); if (snd_msnd_send_dsp_cmd(chip, HDEX_RECORD_START) == 0) return 0; clear_bit(F_READING, &chip->flags); } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { clear_bit(F_READING, &chip->flags); snd_msnd_send_dsp_cmd(chip, HDEX_RECORD_STOP); return 0; } return -EINVAL; } static snd_pcm_uframes_t snd_msnd_capture_pointer(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_msnd *chip = snd_pcm_substream_chip(substream); return bytes_to_frames(runtime, chip->captureDMAPos); } static int snd_msnd_capture_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) { int i; struct snd_msnd *chip = snd_pcm_substream_chip(substream); void __iomem *pDAQ = chip->mappedbase + DARQ_DATA_BUFF; chip->capture_sample_size = snd_pcm_format_width(params_format(params)); chip->capture_channels = params_channels(params); chip->capture_sample_rate = params_rate(params); for (i = 0; i < 3; ++i, pDAQ += DAQDS__size) { writew(chip->capture_sample_size, pDAQ + DAQDS_wSampleSize); writew(chip->capture_channels, pDAQ + DAQDS_wChannels); writew(chip->capture_sample_rate, pDAQ + DAQDS_wSampleRate); } return 0; } static const struct snd_pcm_ops snd_msnd_capture_ops = { .open = snd_msnd_capture_open, .close = snd_msnd_capture_close, .hw_params = snd_msnd_capture_hw_params, .prepare = snd_msnd_capture_prepare, .trigger = snd_msnd_capture_trigger, .pointer = snd_msnd_capture_pointer, .mmap = snd_pcm_lib_mmap_iomem, }; int snd_msnd_pcm(struct snd_card *card, int device) { struct snd_msnd *chip = card->private_data; struct snd_pcm *pcm; int err; err = snd_pcm_new(card, "MSNDPINNACLE", device, 1, 1, &pcm); if (err < 0) return err; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_msnd_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_msnd_capture_ops); pcm->private_data = chip; strcpy(pcm->name, "Hurricane"); return 0; } EXPORT_SYMBOL(snd_msnd_pcm); MODULE_DESCRIPTION("Common routines for Turtle Beach Multisound drivers"); MODULE_LICENSE("GPL");
linux-master
sound/isa/msnd/msnd.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Copyright (c) 2009 by Krzysztof Helt * Routines for control of MPU-401 in UART mode * * MPU-401 supports UART mode which is not capable generate transmit * interrupts thus output is done via polling. Also, if irq < 0, then * input is done also via polling. Do not expect good performance. */ #include <linux/io.h> #include <linux/slab.h> #include <linux/delay.h> #include <linux/ioport.h> #include <linux/errno.h> #include <linux/export.h> #include <sound/core.h> #include <sound/rawmidi.h> #include "msnd.h" #define MSNDMIDI_MODE_BIT_INPUT 0 #define MSNDMIDI_MODE_BIT_OUTPUT 1 #define MSNDMIDI_MODE_BIT_INPUT_TRIGGER 2 #define MSNDMIDI_MODE_BIT_OUTPUT_TRIGGER 3 struct snd_msndmidi { struct snd_msnd *dev; unsigned long mode; /* MSNDMIDI_MODE_XXXX */ struct snd_rawmidi_substream *substream_input; spinlock_t input_lock; }; /* * input/output open/close - protected by open_mutex in rawmidi.c */ static int snd_msndmidi_input_open(struct snd_rawmidi_substream *substream) { struct snd_msndmidi *mpu; snd_printdd("snd_msndmidi_input_open()\n"); mpu = substream->rmidi->private_data; mpu->substream_input = substream; snd_msnd_enable_irq(mpu->dev); snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_START); set_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode); return 0; } static int snd_msndmidi_input_close(struct snd_rawmidi_substream *substream) { struct snd_msndmidi *mpu; mpu = substream->rmidi->private_data; snd_msnd_send_dsp_cmd(mpu->dev, HDEX_MIDI_IN_STOP); clear_bit(MSNDMIDI_MODE_BIT_INPUT, &mpu->mode); mpu->substream_input = NULL; snd_msnd_disable_irq(mpu->dev); return 0; } static void snd_msndmidi_input_drop(struct snd_msndmidi *mpu) { u16 tail; tail = readw(mpu->dev->MIDQ + JQS_wTail); writew(tail, mpu->dev->MIDQ + JQS_wHead); } /* * trigger input */ static void snd_msndmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) { unsigned long flags; struct snd_msndmidi *mpu; snd_printdd("snd_msndmidi_input_trigger(, %i)\n", up); mpu = substream->rmidi->private_data; spin_lock_irqsave(&mpu->input_lock, flags); if (up) { if (!test_and_set_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode)) snd_msndmidi_input_drop(mpu); } else { clear_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode); } spin_unlock_irqrestore(&mpu->input_lock, flags); if (up) snd_msndmidi_input_read(mpu); } void snd_msndmidi_input_read(void *mpuv) { unsigned long flags; struct snd_msndmidi *mpu = mpuv; void __iomem *pwMIDQData = mpu->dev->mappedbase + MIDQ_DATA_BUFF; u16 head, tail, size; spin_lock_irqsave(&mpu->input_lock, flags); head = readw(mpu->dev->MIDQ + JQS_wHead); tail = readw(mpu->dev->MIDQ + JQS_wTail); size = readw(mpu->dev->MIDQ + JQS_wSize); if (head > size || tail > size) goto out; while (head != tail) { unsigned char val = readw(pwMIDQData + 2 * head); if (test_bit(MSNDMIDI_MODE_BIT_INPUT_TRIGGER, &mpu->mode)) snd_rawmidi_receive(mpu->substream_input, &val, 1); if (++head > size) head = 0; writew(head, mpu->dev->MIDQ + JQS_wHead); } out: spin_unlock_irqrestore(&mpu->input_lock, flags); } EXPORT_SYMBOL(snd_msndmidi_input_read); static const struct snd_rawmidi_ops snd_msndmidi_input = { .open = snd_msndmidi_input_open, .close = snd_msndmidi_input_close, .trigger = snd_msndmidi_input_trigger, }; static void snd_msndmidi_free(struct snd_rawmidi *rmidi) { struct snd_msndmidi *mpu = rmidi->private_data; kfree(mpu); } int snd_msndmidi_new(struct snd_card *card, int device) { struct snd_msnd *chip = card->private_data; struct snd_msndmidi *mpu; struct snd_rawmidi *rmidi; int err; err = snd_rawmidi_new(card, "MSND-MIDI", device, 1, 1, &rmidi); if (err < 0) return err; mpu = kzalloc(sizeof(*mpu), GFP_KERNEL); if (mpu == NULL) { snd_device_free(card, rmidi); return -ENOMEM; } mpu->dev = chip; chip->msndmidi_mpu = mpu; rmidi->private_data = mpu; rmidi->private_free = snd_msndmidi_free; spin_lock_init(&mpu->input_lock); strcpy(rmidi->name, "MSND MIDI"); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_msndmidi_input); rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; return 0; }
linux-master
sound/isa/msnd/msnd_midi.c
// SPDX-License-Identifier: GPL-2.0-or-later /*************************************************************************** msnd_pinnacle_mixer.c - description ------------------- begin : Fre Jun 7 2002 copyright : (C) 2002 by karsten wiese email : [email protected] ***************************************************************************/ /*************************************************************************** * * * * ***************************************************************************/ #include <linux/io.h> #include <linux/export.h> #include <sound/core.h> #include <sound/control.h> #include "msnd.h" #include "msnd_pinnacle.h" #define MSND_MIXER_VOLUME 0 #define MSND_MIXER_PCM 1 #define MSND_MIXER_AUX 2 /* Input source 1 (aux1) */ #define MSND_MIXER_IMIX 3 /* Recording monitor */ #define MSND_MIXER_SYNTH 4 #define MSND_MIXER_SPEAKER 5 #define MSND_MIXER_LINE 6 #define MSND_MIXER_MIC 7 #define MSND_MIXER_RECLEV 11 /* Recording level */ #define MSND_MIXER_IGAIN 12 /* Input gain */ #define MSND_MIXER_OGAIN 13 /* Output gain */ #define MSND_MIXER_DIGITAL 17 /* Digital (input) 1 */ /* Device mask bits */ #define MSND_MASK_VOLUME (1 << MSND_MIXER_VOLUME) #define MSND_MASK_SYNTH (1 << MSND_MIXER_SYNTH) #define MSND_MASK_PCM (1 << MSND_MIXER_PCM) #define MSND_MASK_SPEAKER (1 << MSND_MIXER_SPEAKER) #define MSND_MASK_LINE (1 << MSND_MIXER_LINE) #define MSND_MASK_MIC (1 << MSND_MIXER_MIC) #define MSND_MASK_IMIX (1 << MSND_MIXER_IMIX) #define MSND_MASK_RECLEV (1 << MSND_MIXER_RECLEV) #define MSND_MASK_IGAIN (1 << MSND_MIXER_IGAIN) #define MSND_MASK_OGAIN (1 << MSND_MIXER_OGAIN) #define MSND_MASK_AUX (1 << MSND_MIXER_AUX) #define MSND_MASK_DIGITAL (1 << MSND_MIXER_DIGITAL) static int snd_msndmix_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[3] = { "Analog", "MASS", "SPDIF", }; struct snd_msnd *chip = snd_kcontrol_chip(kcontrol); unsigned items = test_bit(F_HAVEDIGITAL, &chip->flags) ? 3 : 2; return snd_ctl_enum_info(uinfo, 1, items, texts); } static int snd_msndmix_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_msnd *chip = snd_kcontrol_chip(kcontrol); /* MSND_MASK_IMIX is the default */ ucontrol->value.enumerated.item[0] = 0; if (chip->recsrc & MSND_MASK_SYNTH) { ucontrol->value.enumerated.item[0] = 1; } else if ((chip->recsrc & MSND_MASK_DIGITAL) && test_bit(F_HAVEDIGITAL, &chip->flags)) { ucontrol->value.enumerated.item[0] = 2; } return 0; } static int snd_msndmix_set_mux(struct snd_msnd *chip, int val) { unsigned newrecsrc; int change; unsigned char msndbyte; switch (val) { case 0: newrecsrc = MSND_MASK_IMIX; msndbyte = HDEXAR_SET_ANA_IN; break; case 1: newrecsrc = MSND_MASK_SYNTH; msndbyte = HDEXAR_SET_SYNTH_IN; break; case 2: newrecsrc = MSND_MASK_DIGITAL; msndbyte = HDEXAR_SET_DAT_IN; break; default: return -EINVAL; } change = newrecsrc != chip->recsrc; if (change) { change = 0; if (!snd_msnd_send_word(chip, 0, 0, msndbyte)) if (!snd_msnd_send_dsp_cmd(chip, HDEX_AUX_REQ)) { chip->recsrc = newrecsrc; change = 1; } } return change; } static int snd_msndmix_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol); return snd_msndmix_set_mux(msnd, ucontrol->value.enumerated.item[0]); } static int snd_msndmix_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = 100; return 0; } static int snd_msndmix_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol); int addr = kcontrol->private_value; unsigned long flags; spin_lock_irqsave(&msnd->mixer_lock, flags); ucontrol->value.integer.value[0] = msnd->left_levels[addr] * 100; ucontrol->value.integer.value[0] /= 0xFFFF; ucontrol->value.integer.value[1] = msnd->right_levels[addr] * 100; ucontrol->value.integer.value[1] /= 0xFFFF; spin_unlock_irqrestore(&msnd->mixer_lock, flags); return 0; } #define update_volm(a, b) \ do { \ writew((dev->left_levels[a] >> 1) * \ readw(dev->SMA + SMA_wCurrMastVolLeft) / 0xffff, \ dev->SMA + SMA_##b##Left); \ writew((dev->right_levels[a] >> 1) * \ readw(dev->SMA + SMA_wCurrMastVolRight) / 0xffff, \ dev->SMA + SMA_##b##Right); \ } while (0); #define update_potm(d, s, ar) \ do { \ writeb((dev->left_levels[d] >> 8) * \ readw(dev->SMA + SMA_wCurrMastVolLeft) / 0xffff, \ dev->SMA + SMA_##s##Left); \ writeb((dev->right_levels[d] >> 8) * \ readw(dev->SMA + SMA_wCurrMastVolRight) / 0xffff, \ dev->SMA + SMA_##s##Right); \ if (snd_msnd_send_word(dev, 0, 0, ar) == 0) \ snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); \ } while (0); #define update_pot(d, s, ar) \ do { \ writeb(dev->left_levels[d] >> 8, \ dev->SMA + SMA_##s##Left); \ writeb(dev->right_levels[d] >> 8, \ dev->SMA + SMA_##s##Right); \ if (snd_msnd_send_word(dev, 0, 0, ar) == 0) \ snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); \ } while (0); static int snd_msndmix_set(struct snd_msnd *dev, int d, int left, int right) { int bLeft, bRight; int wLeft, wRight; int updatemaster = 0; if (d >= LEVEL_ENTRIES) return -EINVAL; bLeft = left * 0xff / 100; wLeft = left * 0xffff / 100; bRight = right * 0xff / 100; wRight = right * 0xffff / 100; dev->left_levels[d] = wLeft; dev->right_levels[d] = wRight; switch (d) { /* master volume unscaled controls */ case MSND_MIXER_LINE: /* line pot control */ /* scaled by IMIX in digital mix */ writeb(bLeft, dev->SMA + SMA_bInPotPosLeft); writeb(bRight, dev->SMA + SMA_bInPotPosRight); if (snd_msnd_send_word(dev, 0, 0, HDEXAR_IN_SET_POTS) == 0) snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); break; case MSND_MIXER_MIC: /* mic pot control */ if (dev->type == msndClassic) return -EINVAL; /* scaled by IMIX in digital mix */ writeb(bLeft, dev->SMA + SMA_bMicPotPosLeft); writeb(bRight, dev->SMA + SMA_bMicPotPosRight); if (snd_msnd_send_word(dev, 0, 0, HDEXAR_MIC_SET_POTS) == 0) snd_msnd_send_dsp_cmd(dev, HDEX_AUX_REQ); break; case MSND_MIXER_VOLUME: /* master volume */ writew(wLeft, dev->SMA + SMA_wCurrMastVolLeft); writew(wRight, dev->SMA + SMA_wCurrMastVolRight); fallthrough; case MSND_MIXER_AUX: /* aux pot control */ /* scaled by master volume */ /* digital controls */ case MSND_MIXER_SYNTH: /* synth vol (dsp mix) */ case MSND_MIXER_PCM: /* pcm vol (dsp mix) */ case MSND_MIXER_IMIX: /* input monitor (dsp mix) */ /* scaled by master volume */ updatemaster = 1; break; default: return -EINVAL; } if (updatemaster) { /* update master volume scaled controls */ update_volm(MSND_MIXER_PCM, wCurrPlayVol); update_volm(MSND_MIXER_IMIX, wCurrInVol); if (dev->type == msndPinnacle) update_volm(MSND_MIXER_SYNTH, wCurrMHdrVol); update_potm(MSND_MIXER_AUX, bAuxPotPos, HDEXAR_AUX_SET_POTS); } return 0; } static int snd_msndmix_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_msnd *msnd = snd_kcontrol_chip(kcontrol); int change, addr = kcontrol->private_value; int left, right; unsigned long flags; left = ucontrol->value.integer.value[0] % 101; right = ucontrol->value.integer.value[1] % 101; spin_lock_irqsave(&msnd->mixer_lock, flags); change = msnd->left_levels[addr] != left || msnd->right_levels[addr] != right; snd_msndmix_set(msnd, addr, left, right); spin_unlock_irqrestore(&msnd->mixer_lock, flags); return change; } #define DUMMY_VOLUME(xname, xindex, addr) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_msndmix_volume_info, \ .get = snd_msndmix_volume_get, .put = snd_msndmix_volume_put, \ .private_value = addr } static const struct snd_kcontrol_new snd_msnd_controls[] = { DUMMY_VOLUME("Master Volume", 0, MSND_MIXER_VOLUME), DUMMY_VOLUME("PCM Volume", 0, MSND_MIXER_PCM), DUMMY_VOLUME("Aux Volume", 0, MSND_MIXER_AUX), DUMMY_VOLUME("Line Volume", 0, MSND_MIXER_LINE), DUMMY_VOLUME("Mic Volume", 0, MSND_MIXER_MIC), DUMMY_VOLUME("Monitor", 0, MSND_MIXER_IMIX), { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Capture Source", .info = snd_msndmix_info_mux, .get = snd_msndmix_get_mux, .put = snd_msndmix_put_mux, } }; int snd_msndmix_new(struct snd_card *card) { struct snd_msnd *chip = card->private_data; unsigned int idx; int err; if (snd_BUG_ON(!chip)) return -EINVAL; spin_lock_init(&chip->mixer_lock); strcpy(card->mixername, "MSND Pinnacle Mixer"); for (idx = 0; idx < ARRAY_SIZE(snd_msnd_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(snd_msnd_controls + idx, chip)); if (err < 0) return err; } return 0; } EXPORT_SYMBOL(snd_msndmix_new); void snd_msndmix_setup(struct snd_msnd *dev) { update_pot(MSND_MIXER_LINE, bInPotPos, HDEXAR_IN_SET_POTS); update_potm(MSND_MIXER_AUX, bAuxPotPos, HDEXAR_AUX_SET_POTS); update_volm(MSND_MIXER_PCM, wCurrPlayVol); update_volm(MSND_MIXER_IMIX, wCurrInVol); if (dev->type == msndPinnacle) { update_pot(MSND_MIXER_MIC, bMicPotPos, HDEXAR_MIC_SET_POTS); update_volm(MSND_MIXER_SYNTH, wCurrMHdrVol); } } EXPORT_SYMBOL(snd_msndmix_setup); int snd_msndmix_force_recsrc(struct snd_msnd *dev, int recsrc) { dev->recsrc = -1; return snd_msndmix_set_mux(dev, recsrc); } EXPORT_SYMBOL(snd_msndmix_force_recsrc);
linux-master
sound/isa/msnd/msnd_pinnacle_mixer.c
// SPDX-License-Identifier: GPL-2.0-or-later /********************************************************************* * * Linux multisound pinnacle/fiji driver for ALSA. * * 2002/06/30 Karsten Wiese: * for now this is only used to build a pinnacle / fiji driver. * the OSS parent of this code is designed to also support * the multisound classic via the file msnd_classic.c. * to make it easier for some brave heart to implemt classic * support in alsa, i left all the MSND_CLASSIC tokens in this file. * but for now this untested & undone. * * ripped from linux kernel 2.4.18 by Karsten Wiese. * * the following is a copy of the 2.4.18 OSS FREE file-heading comment: * * Turtle Beach MultiSound Sound Card Driver for Linux * msnd_pinnacle.c / msnd_classic.c * * -- If MSND_CLASSIC is defined: * * -> driver for Turtle Beach Classic/Monterey/Tahiti * * -- Else * * -> driver for Turtle Beach Pinnacle/Fiji * * 12-3-2000 Modified IO port validation Steve Sycamore * * Copyright (C) 1998 Andrew Veliath * ********************************************************************/ #include <linux/kernel.h> #include <linux/module.h> #include <linux/interrupt.h> #include <linux/types.h> #include <linux/delay.h> #include <linux/ioport.h> #include <linux/firmware.h> #include <linux/isa.h> #include <linux/isapnp.h> #include <linux/irq.h> #include <linux/io.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/asound.h> #include <sound/pcm.h> #include <sound/mpu401.h> #ifdef MSND_CLASSIC # ifndef __alpha__ # define SLOWIO # endif #endif #include "msnd.h" #ifdef MSND_CLASSIC # include "msnd_classic.h" # define LOGNAME "msnd_classic" # define DEV_NAME "msnd-classic" #else # include "msnd_pinnacle.h" # define LOGNAME "snd_msnd_pinnacle" # define DEV_NAME "msnd-pinnacle" #endif static void set_default_audio_parameters(struct snd_msnd *chip) { chip->play_sample_size = snd_pcm_format_width(DEFSAMPLESIZE); chip->play_sample_rate = DEFSAMPLERATE; chip->play_channels = DEFCHANNELS; chip->capture_sample_size = snd_pcm_format_width(DEFSAMPLESIZE); chip->capture_sample_rate = DEFSAMPLERATE; chip->capture_channels = DEFCHANNELS; } static void snd_msnd_eval_dsp_msg(struct snd_msnd *chip, u16 wMessage) { switch (HIBYTE(wMessage)) { case HIMT_PLAY_DONE: { if (chip->banksPlayed < 3) snd_printdd("%08X: HIMT_PLAY_DONE: %i\n", (unsigned)jiffies, LOBYTE(wMessage)); if (chip->last_playbank == LOBYTE(wMessage)) { snd_printdd("chip.last_playbank == LOBYTE(wMessage)\n"); break; } chip->banksPlayed++; if (test_bit(F_WRITING, &chip->flags)) snd_msnd_DAPQ(chip, 0); chip->last_playbank = LOBYTE(wMessage); chip->playDMAPos += chip->play_period_bytes; if (chip->playDMAPos > chip->playLimit) chip->playDMAPos = 0; snd_pcm_period_elapsed(chip->playback_substream); break; } case HIMT_RECORD_DONE: if (chip->last_recbank == LOBYTE(wMessage)) break; chip->last_recbank = LOBYTE(wMessage); chip->captureDMAPos += chip->capturePeriodBytes; if (chip->captureDMAPos > (chip->captureLimit)) chip->captureDMAPos = 0; if (test_bit(F_READING, &chip->flags)) snd_msnd_DARQ(chip, chip->last_recbank); snd_pcm_period_elapsed(chip->capture_substream); break; case HIMT_DSP: switch (LOBYTE(wMessage)) { #ifndef MSND_CLASSIC case HIDSP_PLAY_UNDER: #endif case HIDSP_INT_PLAY_UNDER: snd_printd(KERN_WARNING LOGNAME ": Play underflow %i\n", chip->banksPlayed); if (chip->banksPlayed > 2) clear_bit(F_WRITING, &chip->flags); break; case HIDSP_INT_RECORD_OVER: snd_printd(KERN_WARNING LOGNAME ": Record overflow\n"); clear_bit(F_READING, &chip->flags); break; default: snd_printd(KERN_WARNING LOGNAME ": DSP message %d 0x%02x\n", LOBYTE(wMessage), LOBYTE(wMessage)); break; } break; case HIMT_MIDI_IN_UCHAR: if (chip->msndmidi_mpu) snd_msndmidi_input_read(chip->msndmidi_mpu); break; default: snd_printd(KERN_WARNING LOGNAME ": HIMT message %d 0x%02x\n", HIBYTE(wMessage), HIBYTE(wMessage)); break; } } static irqreturn_t snd_msnd_interrupt(int irq, void *dev_id) { struct snd_msnd *chip = dev_id; void __iomem *pwDSPQData = chip->mappedbase + DSPQ_DATA_BUFF; u16 head, tail, size; /* Send ack to DSP */ /* inb(chip->io + HP_RXL); */ /* Evaluate queued DSP messages */ head = readw(chip->DSPQ + JQS_wHead); tail = readw(chip->DSPQ + JQS_wTail); size = readw(chip->DSPQ + JQS_wSize); if (head > size || tail > size) goto out; while (head != tail) { snd_msnd_eval_dsp_msg(chip, readw(pwDSPQData + 2 * head)); if (++head > size) head = 0; writew(head, chip->DSPQ + JQS_wHead); } out: /* Send ack to DSP */ inb(chip->io + HP_RXL); return IRQ_HANDLED; } static int snd_msnd_reset_dsp(long io, unsigned char *info) { int timeout = 100; outb(HPDSPRESET_ON, io + HP_DSPR); msleep(1); #ifndef MSND_CLASSIC if (info) *info = inb(io + HP_INFO); #endif outb(HPDSPRESET_OFF, io + HP_DSPR); msleep(1); while (timeout-- > 0) { if (inb(io + HP_CVR) == HP_CVR_DEF) return 0; msleep(1); } snd_printk(KERN_ERR LOGNAME ": Cannot reset DSP\n"); return -EIO; } static int snd_msnd_probe(struct snd_card *card) { struct snd_msnd *chip = card->private_data; unsigned char info; #ifndef MSND_CLASSIC char *xv, *rev = NULL; char *pin = "TB Pinnacle", *fiji = "TB Fiji"; char *pinfiji = "TB Pinnacle/Fiji"; #endif if (!request_region(chip->io, DSP_NUMIO, "probing")) { snd_printk(KERN_ERR LOGNAME ": I/O port conflict\n"); return -ENODEV; } if (snd_msnd_reset_dsp(chip->io, &info) < 0) { release_region(chip->io, DSP_NUMIO); return -ENODEV; } #ifdef MSND_CLASSIC strcpy(card->shortname, "Classic/Tahiti/Monterey"); strcpy(card->longname, "Turtle Beach Multisound"); printk(KERN_INFO LOGNAME ": %s, " "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n", card->shortname, chip->io, chip->io + DSP_NUMIO - 1, chip->irq, chip->base, chip->base + 0x7fff); #else switch (info >> 4) { case 0xf: xv = "<= 1.15"; break; case 0x1: xv = "1.18/1.2"; break; case 0x2: xv = "1.3"; break; case 0x3: xv = "1.4"; break; default: xv = "unknown"; break; } switch (info & 0x7) { case 0x0: rev = "I"; strcpy(card->shortname, pin); break; case 0x1: rev = "F"; strcpy(card->shortname, pin); break; case 0x2: rev = "G"; strcpy(card->shortname, pin); break; case 0x3: rev = "H"; strcpy(card->shortname, pin); break; case 0x4: rev = "E"; strcpy(card->shortname, fiji); break; case 0x5: rev = "C"; strcpy(card->shortname, fiji); break; case 0x6: rev = "D"; strcpy(card->shortname, fiji); break; case 0x7: rev = "A-B (Fiji) or A-E (Pinnacle)"; strcpy(card->shortname, pinfiji); break; } strcpy(card->longname, "Turtle Beach Multisound Pinnacle"); printk(KERN_INFO LOGNAME ": %s revision %s, Xilinx version %s, " "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n", card->shortname, rev, xv, chip->io, chip->io + DSP_NUMIO - 1, chip->irq, chip->base, chip->base + 0x7fff); #endif release_region(chip->io, DSP_NUMIO); return 0; } static int snd_msnd_init_sma(struct snd_msnd *chip) { static int initted; u16 mastVolLeft, mastVolRight; unsigned long flags; #ifdef MSND_CLASSIC outb(chip->memid, chip->io + HP_MEMM); #endif outb(HPBLKSEL_0, chip->io + HP_BLKS); /* Motorola 56k shared memory base */ chip->SMA = chip->mappedbase + SMA_STRUCT_START; if (initted) { mastVolLeft = readw(chip->SMA + SMA_wCurrMastVolLeft); mastVolRight = readw(chip->SMA + SMA_wCurrMastVolRight); } else mastVolLeft = mastVolRight = 0; memset_io(chip->mappedbase, 0, 0x8000); /* Critical section: bank 1 access */ spin_lock_irqsave(&chip->lock, flags); outb(HPBLKSEL_1, chip->io + HP_BLKS); memset_io(chip->mappedbase, 0, 0x8000); outb(HPBLKSEL_0, chip->io + HP_BLKS); spin_unlock_irqrestore(&chip->lock, flags); /* Digital audio play queue */ chip->DAPQ = chip->mappedbase + DAPQ_OFFSET; snd_msnd_init_queue(chip->DAPQ, DAPQ_DATA_BUFF, DAPQ_BUFF_SIZE); /* Digital audio record queue */ chip->DARQ = chip->mappedbase + DARQ_OFFSET; snd_msnd_init_queue(chip->DARQ, DARQ_DATA_BUFF, DARQ_BUFF_SIZE); /* MIDI out queue */ chip->MODQ = chip->mappedbase + MODQ_OFFSET; snd_msnd_init_queue(chip->MODQ, MODQ_DATA_BUFF, MODQ_BUFF_SIZE); /* MIDI in queue */ chip->MIDQ = chip->mappedbase + MIDQ_OFFSET; snd_msnd_init_queue(chip->MIDQ, MIDQ_DATA_BUFF, MIDQ_BUFF_SIZE); /* DSP -> host message queue */ chip->DSPQ = chip->mappedbase + DSPQ_OFFSET; snd_msnd_init_queue(chip->DSPQ, DSPQ_DATA_BUFF, DSPQ_BUFF_SIZE); /* Setup some DSP values */ #ifndef MSND_CLASSIC writew(1, chip->SMA + SMA_wCurrPlayFormat); writew(chip->play_sample_size, chip->SMA + SMA_wCurrPlaySampleSize); writew(chip->play_channels, chip->SMA + SMA_wCurrPlayChannels); writew(chip->play_sample_rate, chip->SMA + SMA_wCurrPlaySampleRate); #endif writew(chip->play_sample_rate, chip->SMA + SMA_wCalFreqAtoD); writew(mastVolLeft, chip->SMA + SMA_wCurrMastVolLeft); writew(mastVolRight, chip->SMA + SMA_wCurrMastVolRight); #ifndef MSND_CLASSIC writel(0x00010000, chip->SMA + SMA_dwCurrPlayPitch); writel(0x00000001, chip->SMA + SMA_dwCurrPlayRate); #endif writew(0x303, chip->SMA + SMA_wCurrInputTagBits); initted = 1; return 0; } static int upload_dsp_code(struct snd_card *card) { struct snd_msnd *chip = card->private_data; const struct firmware *init_fw = NULL, *perm_fw = NULL; int err; outb(HPBLKSEL_0, chip->io + HP_BLKS); err = request_firmware(&init_fw, INITCODEFILE, card->dev); if (err < 0) { printk(KERN_ERR LOGNAME ": Error loading " INITCODEFILE); goto cleanup1; } err = request_firmware(&perm_fw, PERMCODEFILE, card->dev); if (err < 0) { printk(KERN_ERR LOGNAME ": Error loading " PERMCODEFILE); goto cleanup; } memcpy_toio(chip->mappedbase, perm_fw->data, perm_fw->size); if (snd_msnd_upload_host(chip, init_fw->data, init_fw->size) < 0) { printk(KERN_WARNING LOGNAME ": Error uploading to DSP\n"); err = -ENODEV; goto cleanup; } printk(KERN_INFO LOGNAME ": DSP firmware uploaded\n"); err = 0; cleanup: release_firmware(perm_fw); cleanup1: release_firmware(init_fw); return err; } #ifdef MSND_CLASSIC static void reset_proteus(struct snd_msnd *chip) { outb(HPPRORESET_ON, chip->io + HP_PROR); msleep(TIME_PRO_RESET); outb(HPPRORESET_OFF, chip->io + HP_PROR); msleep(TIME_PRO_RESET_DONE); } #endif static int snd_msnd_initialize(struct snd_card *card) { struct snd_msnd *chip = card->private_data; int err, timeout; #ifdef MSND_CLASSIC outb(HPWAITSTATE_0, chip->io + HP_WAIT); outb(HPBITMODE_16, chip->io + HP_BITM); reset_proteus(chip); #endif err = snd_msnd_init_sma(chip); if (err < 0) { printk(KERN_WARNING LOGNAME ": Cannot initialize SMA\n"); return err; } err = snd_msnd_reset_dsp(chip->io, NULL); if (err < 0) return err; err = upload_dsp_code(card); if (err < 0) { printk(KERN_WARNING LOGNAME ": Cannot upload DSP code\n"); return err; } timeout = 200; while (readw(chip->mappedbase)) { msleep(1); if (!timeout--) { snd_printd(KERN_ERR LOGNAME ": DSP reset timeout\n"); return -EIO; } } snd_msndmix_setup(chip); return 0; } static int snd_msnd_dsp_full_reset(struct snd_card *card) { struct snd_msnd *chip = card->private_data; int rv; if (test_bit(F_RESETTING, &chip->flags) || ++chip->nresets > 10) return 0; set_bit(F_RESETTING, &chip->flags); snd_msnd_dsp_halt(chip, NULL); /* Unconditionally halt */ rv = snd_msnd_initialize(card); if (rv) printk(KERN_WARNING LOGNAME ": DSP reset failed\n"); snd_msndmix_force_recsrc(chip, 0); clear_bit(F_RESETTING, &chip->flags); return rv; } static int snd_msnd_send_dsp_cmd_chk(struct snd_msnd *chip, u8 cmd) { if (snd_msnd_send_dsp_cmd(chip, cmd) == 0) return 0; snd_msnd_dsp_full_reset(chip->card); return snd_msnd_send_dsp_cmd(chip, cmd); } static int snd_msnd_calibrate_adc(struct snd_msnd *chip, u16 srate) { snd_printdd("snd_msnd_calibrate_adc(%i)\n", srate); writew(srate, chip->SMA + SMA_wCalFreqAtoD); if (chip->calibrate_signal == 0) writew(readw(chip->SMA + SMA_wCurrHostStatusFlags) | 0x0001, chip->SMA + SMA_wCurrHostStatusFlags); else writew(readw(chip->SMA + SMA_wCurrHostStatusFlags) & ~0x0001, chip->SMA + SMA_wCurrHostStatusFlags); if (snd_msnd_send_word(chip, 0, 0, HDEXAR_CAL_A_TO_D) == 0 && snd_msnd_send_dsp_cmd_chk(chip, HDEX_AUX_REQ) == 0) { schedule_timeout_interruptible(msecs_to_jiffies(333)); return 0; } printk(KERN_WARNING LOGNAME ": ADC calibration failed\n"); return -EIO; } /* * ALSA callback function, called when attempting to open the MIDI device. */ static int snd_msnd_mpu401_open(struct snd_mpu401 *mpu) { snd_msnd_enable_irq(mpu->private_data); snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_START); return 0; } static void snd_msnd_mpu401_close(struct snd_mpu401 *mpu) { snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_STOP); snd_msnd_disable_irq(mpu->private_data); } static long mpu_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int snd_msnd_attach(struct snd_card *card) { struct snd_msnd *chip = card->private_data; int err; err = devm_request_irq(card->dev, chip->irq, snd_msnd_interrupt, 0, card->shortname, chip); if (err < 0) { printk(KERN_ERR LOGNAME ": Couldn't grab IRQ %d\n", chip->irq); return err; } card->sync_irq = chip->irq; if (!devm_request_region(card->dev, chip->io, DSP_NUMIO, card->shortname)) return -EBUSY; if (!devm_request_mem_region(card->dev, chip->base, BUFFSIZE, card->shortname)) { printk(KERN_ERR LOGNAME ": unable to grab memory region 0x%lx-0x%lx\n", chip->base, chip->base + BUFFSIZE - 1); return -EBUSY; } chip->mappedbase = devm_ioremap(card->dev, chip->base, 0x8000); if (!chip->mappedbase) { printk(KERN_ERR LOGNAME ": unable to map memory region 0x%lx-0x%lx\n", chip->base, chip->base + BUFFSIZE - 1); return -EIO; } err = snd_msnd_dsp_full_reset(card); if (err < 0) return err; err = snd_msnd_pcm(card, 0); if (err < 0) { printk(KERN_ERR LOGNAME ": error creating new PCM device\n"); return err; } err = snd_msndmix_new(card); if (err < 0) { printk(KERN_ERR LOGNAME ": error creating new Mixer device\n"); return err; } if (mpu_io[0] != SNDRV_AUTO_PORT) { struct snd_mpu401 *mpu; err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_io[0], MPU401_MODE_INPUT | MPU401_MODE_OUTPUT, mpu_irq[0], &chip->rmidi); if (err < 0) { printk(KERN_ERR LOGNAME ": error creating new Midi device\n"); return err; } mpu = chip->rmidi->private_data; mpu->open_input = snd_msnd_mpu401_open; mpu->close_input = snd_msnd_mpu401_close; mpu->private_data = chip; } disable_irq(chip->irq); snd_msnd_calibrate_adc(chip, chip->play_sample_rate); snd_msndmix_force_recsrc(chip, 0); err = snd_card_register(card); if (err < 0) return err; return 0; } #ifndef MSND_CLASSIC /* Pinnacle/Fiji Logical Device Configuration */ static int snd_msnd_write_cfg(int cfg, int reg, int value) { outb(reg, cfg); outb(value, cfg + 1); if (value != inb(cfg + 1)) { printk(KERN_ERR LOGNAME ": snd_msnd_write_cfg: I/O error\n"); return -EIO; } return 0; } static int snd_msnd_write_cfg_io0(int cfg, int num, u16 io) { if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num)) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_IO0_BASEHI, HIBYTE(io))) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_IO0_BASELO, LOBYTE(io))) return -EIO; return 0; } static int snd_msnd_write_cfg_io1(int cfg, int num, u16 io) { if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num)) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_IO1_BASEHI, HIBYTE(io))) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_IO1_BASELO, LOBYTE(io))) return -EIO; return 0; } static int snd_msnd_write_cfg_irq(int cfg, int num, u16 irq) { if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num)) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_IRQ_NUMBER, LOBYTE(irq))) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_IRQ_TYPE, IRQTYPE_EDGE)) return -EIO; return 0; } static int snd_msnd_write_cfg_mem(int cfg, int num, int mem) { u16 wmem; mem >>= 8; wmem = (u16)(mem & 0xfff); if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num)) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_MEMBASEHI, HIBYTE(wmem))) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_MEMBASELO, LOBYTE(wmem))) return -EIO; if (wmem && snd_msnd_write_cfg(cfg, IREG_MEMCONTROL, MEMTYPE_HIADDR | MEMTYPE_16BIT)) return -EIO; return 0; } static int snd_msnd_activate_logical(int cfg, int num) { if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num)) return -EIO; if (snd_msnd_write_cfg(cfg, IREG_ACTIVATE, LD_ACTIVATE)) return -EIO; return 0; } static int snd_msnd_write_cfg_logical(int cfg, int num, u16 io0, u16 io1, u16 irq, int mem) { if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num)) return -EIO; if (snd_msnd_write_cfg_io0(cfg, num, io0)) return -EIO; if (snd_msnd_write_cfg_io1(cfg, num, io1)) return -EIO; if (snd_msnd_write_cfg_irq(cfg, num, irq)) return -EIO; if (snd_msnd_write_cfg_mem(cfg, num, mem)) return -EIO; if (snd_msnd_activate_logical(cfg, num)) return -EIO; return 0; } static int snd_msnd_pinnacle_cfg_reset(int cfg) { int i; /* Reset devices if told to */ printk(KERN_INFO LOGNAME ": Resetting all devices\n"); for (i = 0; i < 4; ++i) if (snd_msnd_write_cfg_logical(cfg, i, 0, 0, 0, 0)) return -EIO; return 0; } #endif static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for msnd_pinnacle soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for msnd_pinnacle soundcard."); static long io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static long mem[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; #ifndef MSND_CLASSIC static long cfg[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* Extra Peripheral Configuration (Default: Disable) */ static long ide_io0[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static long ide_io1[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int ide_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static long joystick_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* If we have the digital daugherboard... */ static int digital[SNDRV_CARDS]; /* Extra Peripheral Configuration */ static int reset[SNDRV_CARDS]; #endif static int write_ndelay[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 }; static int calibrate_signal; #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard."); #define has_isapnp(x) isapnp[x] #else #define has_isapnp(x) 0 #endif MODULE_AUTHOR("Karsten Wiese <[email protected]>"); MODULE_DESCRIPTION("Turtle Beach " LONGNAME " Linux Driver"); MODULE_LICENSE("GPL"); MODULE_FIRMWARE(INITCODEFILE); MODULE_FIRMWARE(PERMCODEFILE); module_param_hw_array(io, long, ioport, NULL, 0444); MODULE_PARM_DESC(io, "IO port #"); module_param_hw_array(irq, int, irq, NULL, 0444); module_param_hw_array(mem, long, iomem, NULL, 0444); module_param_array(write_ndelay, int, NULL, 0444); module_param(calibrate_signal, int, 0444); #ifndef MSND_CLASSIC module_param_array(digital, int, NULL, 0444); module_param_hw_array(cfg, long, ioport, NULL, 0444); module_param_array(reset, int, NULL, 0444); module_param_hw_array(mpu_io, long, ioport, NULL, 0444); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); module_param_hw_array(ide_io0, long, ioport, NULL, 0444); module_param_hw_array(ide_io1, long, ioport, NULL, 0444); module_param_hw_array(ide_irq, int, irq, NULL, 0444); module_param_hw_array(joystick_io, long, ioport, NULL, 0444); #endif static int snd_msnd_isa_match(struct device *pdev, unsigned int i) { if (io[i] == SNDRV_AUTO_PORT) return 0; if (irq[i] == SNDRV_AUTO_PORT || mem[i] == SNDRV_AUTO_PORT) { printk(KERN_WARNING LOGNAME ": io, irq and mem must be set\n"); return 0; } #ifdef MSND_CLASSIC if (!(io[i] == 0x290 || io[i] == 0x260 || io[i] == 0x250 || io[i] == 0x240 || io[i] == 0x230 || io[i] == 0x220 || io[i] == 0x210 || io[i] == 0x3e0)) { printk(KERN_ERR LOGNAME ": \"io\" - DSP I/O base must be set " " to 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x290, " "or 0x3E0\n"); return 0; } #else if (io[i] < 0x100 || io[i] > 0x3e0 || (io[i] % 0x10) != 0) { printk(KERN_ERR LOGNAME ": \"io\" - DSP I/O base must within the range 0x100 " "to 0x3E0 and must be evenly divisible by 0x10\n"); return 0; } #endif /* MSND_CLASSIC */ if (!(irq[i] == 5 || irq[i] == 7 || irq[i] == 9 || irq[i] == 10 || irq[i] == 11 || irq[i] == 12)) { printk(KERN_ERR LOGNAME ": \"irq\" - must be set to 5, 7, 9, 10, 11 or 12\n"); return 0; } if (!(mem[i] == 0xb0000 || mem[i] == 0xc8000 || mem[i] == 0xd0000 || mem[i] == 0xd8000 || mem[i] == 0xe0000 || mem[i] == 0xe8000)) { printk(KERN_ERR LOGNAME ": \"mem\" - must be set to " "0xb0000, 0xc8000, 0xd0000, 0xd8000, 0xe0000 or " "0xe8000\n"); return 0; } #ifndef MSND_CLASSIC if (cfg[i] == SNDRV_AUTO_PORT) { printk(KERN_INFO LOGNAME ": Assuming PnP mode\n"); } else if (cfg[i] != 0x250 && cfg[i] != 0x260 && cfg[i] != 0x270) { printk(KERN_INFO LOGNAME ": Config port must be 0x250, 0x260 or 0x270 " "(or unspecified for PnP mode)\n"); return 0; } #endif /* MSND_CLASSIC */ return 1; } static int snd_msnd_isa_probe(struct device *pdev, unsigned int idx) { int err; struct snd_card *card; struct snd_msnd *chip; if (has_isapnp(idx) #ifndef MSND_CLASSIC || cfg[idx] == SNDRV_AUTO_PORT #endif ) { printk(KERN_INFO LOGNAME ": Assuming PnP mode\n"); return -ENODEV; } err = snd_devm_card_new(pdev, index[idx], id[idx], THIS_MODULE, sizeof(struct snd_msnd), &card); if (err < 0) return err; chip = card->private_data; chip->card = card; #ifdef MSND_CLASSIC switch (irq[idx]) { case 5: chip->irqid = HPIRQ_5; break; case 7: chip->irqid = HPIRQ_7; break; case 9: chip->irqid = HPIRQ_9; break; case 10: chip->irqid = HPIRQ_10; break; case 11: chip->irqid = HPIRQ_11; break; case 12: chip->irqid = HPIRQ_12; break; } switch (mem[idx]) { case 0xb0000: chip->memid = HPMEM_B000; break; case 0xc8000: chip->memid = HPMEM_C800; break; case 0xd0000: chip->memid = HPMEM_D000; break; case 0xd8000: chip->memid = HPMEM_D800; break; case 0xe0000: chip->memid = HPMEM_E000; break; case 0xe8000: chip->memid = HPMEM_E800; break; } #else printk(KERN_INFO LOGNAME ": Non-PnP mode: configuring at port 0x%lx\n", cfg[idx]); if (!devm_request_region(card->dev, cfg[idx], 2, "Pinnacle/Fiji Config")) { printk(KERN_ERR LOGNAME ": Config port 0x%lx conflict\n", cfg[idx]); return -EIO; } if (reset[idx]) if (snd_msnd_pinnacle_cfg_reset(cfg[idx])) return -EIO; /* DSP */ err = snd_msnd_write_cfg_logical(cfg[idx], 0, io[idx], 0, irq[idx], mem[idx]); if (err) return err; /* The following are Pinnacle specific */ /* MPU */ if (mpu_io[idx] != SNDRV_AUTO_PORT && mpu_irq[idx] != SNDRV_AUTO_IRQ) { printk(KERN_INFO LOGNAME ": Configuring MPU to I/O 0x%lx IRQ %d\n", mpu_io[idx], mpu_irq[idx]); err = snd_msnd_write_cfg_logical(cfg[idx], 1, mpu_io[idx], 0, mpu_irq[idx], 0); if (err) return err; } /* IDE */ if (ide_io0[idx] != SNDRV_AUTO_PORT && ide_io1[idx] != SNDRV_AUTO_PORT && ide_irq[idx] != SNDRV_AUTO_IRQ) { printk(KERN_INFO LOGNAME ": Configuring IDE to I/O 0x%lx, 0x%lx IRQ %d\n", ide_io0[idx], ide_io1[idx], ide_irq[idx]); err = snd_msnd_write_cfg_logical(cfg[idx], 2, ide_io0[idx], ide_io1[idx], ide_irq[idx], 0); if (err) return err; } /* Joystick */ if (joystick_io[idx] != SNDRV_AUTO_PORT) { printk(KERN_INFO LOGNAME ": Configuring joystick to I/O 0x%lx\n", joystick_io[idx]); err = snd_msnd_write_cfg_logical(cfg[idx], 3, joystick_io[idx], 0, 0, 0); if (err) return err; } #endif /* MSND_CLASSIC */ set_default_audio_parameters(chip); #ifdef MSND_CLASSIC chip->type = msndClassic; #else chip->type = msndPinnacle; #endif chip->io = io[idx]; chip->irq = irq[idx]; chip->base = mem[idx]; chip->calibrate_signal = calibrate_signal ? 1 : 0; chip->recsrc = 0; chip->dspq_data_buff = DSPQ_DATA_BUFF; chip->dspq_buff_size = DSPQ_BUFF_SIZE; if (write_ndelay[idx]) clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags); else set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags); #ifndef MSND_CLASSIC if (digital[idx]) set_bit(F_HAVEDIGITAL, &chip->flags); #endif spin_lock_init(&chip->lock); err = snd_msnd_probe(card); if (err < 0) { printk(KERN_ERR LOGNAME ": Probe failed\n"); return err; } err = snd_msnd_attach(card); if (err < 0) { printk(KERN_ERR LOGNAME ": Attach failed\n"); return err; } dev_set_drvdata(pdev, card); return 0; } static struct isa_driver snd_msnd_driver = { .match = snd_msnd_isa_match, .probe = snd_msnd_isa_probe, /* FIXME: suspend, resume */ .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_msnd_pnp_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int idx; struct pnp_dev *pnp_dev; struct pnp_dev *mpu_dev; struct snd_card *card; struct snd_msnd *chip; int ret; for ( ; idx < SNDRV_CARDS; idx++) { if (has_isapnp(idx)) break; } if (idx >= SNDRV_CARDS) return -ENODEV; /* * Check that we still have room for another sound card ... */ pnp_dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL); if (!pnp_dev) return -ENODEV; mpu_dev = pnp_request_card_device(pcard, pid->devs[1].id, NULL); if (!mpu_dev) return -ENODEV; if (!pnp_is_active(pnp_dev) && pnp_activate_dev(pnp_dev) < 0) { printk(KERN_INFO "msnd_pinnacle: device is inactive\n"); return -EBUSY; } if (!pnp_is_active(mpu_dev) && pnp_activate_dev(mpu_dev) < 0) { printk(KERN_INFO "msnd_pinnacle: MPU 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 snd_msnd), &card); if (ret < 0) return ret; chip = card->private_data; chip->card = card; /* * Read the correct parameters off the ISA PnP bus ... */ io[idx] = pnp_port_start(pnp_dev, 0); irq[idx] = pnp_irq(pnp_dev, 0); mem[idx] = pnp_mem_start(pnp_dev, 0); mpu_io[idx] = pnp_port_start(mpu_dev, 0); mpu_irq[idx] = pnp_irq(mpu_dev, 0); set_default_audio_parameters(chip); #ifdef MSND_CLASSIC chip->type = msndClassic; #else chip->type = msndPinnacle; #endif chip->io = io[idx]; chip->irq = irq[idx]; chip->base = mem[idx]; chip->calibrate_signal = calibrate_signal ? 1 : 0; chip->recsrc = 0; chip->dspq_data_buff = DSPQ_DATA_BUFF; chip->dspq_buff_size = DSPQ_BUFF_SIZE; if (write_ndelay[idx]) clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags); else set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags); #ifndef MSND_CLASSIC if (digital[idx]) set_bit(F_HAVEDIGITAL, &chip->flags); #endif spin_lock_init(&chip->lock); ret = snd_msnd_probe(card); if (ret < 0) { printk(KERN_ERR LOGNAME ": Probe failed\n"); return ret; } ret = snd_msnd_attach(card); if (ret < 0) { printk(KERN_ERR LOGNAME ": Attach failed\n"); return ret; } pnp_set_card_drvdata(pcard, card); ++idx; return 0; } static int isa_registered; static int pnp_registered; static const struct pnp_card_device_id msnd_pnpids[] = { /* Pinnacle PnP */ { .id = "BVJ0440", .devs = { { "TBS0000" }, { "TBS0001" } } }, { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, msnd_pnpids); static struct pnp_card_driver msnd_pnpc_driver = { .flags = PNP_DRIVER_RES_DO_NOT_CHANGE, .name = "msnd_pinnacle", .id_table = msnd_pnpids, .probe = snd_msnd_pnp_detect, }; #endif /* CONFIG_PNP */ static int __init snd_msnd_init(void) { int err; err = isa_register_driver(&snd_msnd_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_card_driver(&msnd_pnpc_driver); if (!err) pnp_registered = 1; if (isa_registered) err = 0; #endif return err; } static void __exit snd_msnd_exit(void) { #ifdef CONFIG_PNP if (pnp_registered) pnp_unregister_card_driver(&msnd_pnpc_driver); if (isa_registered) #endif isa_unregister_driver(&snd_msnd_driver); } module_init(snd_msnd_init); module_exit(snd_msnd_exit);
linux-master
sound/isa/msnd/msnd_pinnacle.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for generic ESS AudioDrive ESx688 soundcards * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/isapnp.h> #include <linux/time.h> #include <linux/wait.h> #include <linux/module.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/es1688.h> #include <sound/mpu401.h> #include <sound/opl3.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #define CRD_NAME "Generic ESS ES1688/ES688 AudioDrive" #define DEV_NAME "es1688" MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); MODULE_ALIAS("snd_es968"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; #endif static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* Usually 0x388 */ static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1}; static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard."); #endif MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver."); module_param_hw_array(irq, int, irq, NULL, 0444); module_param_hw_array(fm_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(fm_port, "FM port # for ES1688 driver."); MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver."); module_param_hw_array(dma8, int, dma, NULL, 0444); MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver."); #ifdef CONFIG_PNP #define is_isapnp_selected(dev) isapnp[dev] #else #define is_isapnp_selected(dev) 0 #endif static int snd_es1688_match(struct device *dev, unsigned int n) { return enable[n] && !is_isapnp_selected(n); } static int snd_es1688_legacy_create(struct snd_card *card, struct device *dev, unsigned int n) { struct snd_es1688 *chip = card->private_data; static const long possible_ports[] = {0x220, 0x240, 0x260}; static const int possible_irqs[] = {5, 9, 10, 7, -1}; static const int possible_dmas[] = {1, 3, 0, -1}; int i, error; if (irq[n] == SNDRV_AUTO_IRQ) { irq[n] = snd_legacy_find_free_irq(possible_irqs); if (irq[n] < 0) { dev_err(dev, "unable to find a free IRQ\n"); return -EBUSY; } } if (dma8[n] == SNDRV_AUTO_DMA) { dma8[n] = snd_legacy_find_free_dma(possible_dmas); if (dma8[n] < 0) { dev_err(dev, "unable to find a free DMA\n"); return -EBUSY; } } if (port[n] != SNDRV_AUTO_PORT) return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO); i = 0; do { port[n] = possible_ports[i]; error = snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO); } while (error < 0 && ++i < ARRAY_SIZE(possible_ports)); return error; } static int snd_es1688_probe(struct snd_card *card, unsigned int n) { struct snd_es1688 *chip = card->private_data; struct snd_opl3 *opl3; int error; error = snd_es1688_pcm(card, chip, 0); if (error < 0) return error; error = snd_es1688_mixer(card, chip); if (error < 0) return error; strscpy(card->driver, "ES1688", sizeof(card->driver)); strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname)); scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port, chip->irq, chip->dma8); if (fm_port[n] == SNDRV_AUTO_PORT) fm_port[n] = port[n]; /* share the same port */ if (fm_port[n] > 0) { if (snd_opl3_create(card, fm_port[n], fm_port[n] + 2, OPL3_HW_OPL3, 0, &opl3) < 0) dev_warn(card->dev, "opl3 not detected at 0x%lx\n", fm_port[n]); else { error = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (error < 0) return error; } } if (mpu_irq[n] >= 0 && mpu_irq[n] != SNDRV_AUTO_IRQ && chip->mpu_port > 0) { error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688, chip->mpu_port, 0, mpu_irq[n], NULL); if (error < 0) return error; } return snd_card_register(card); } static int snd_es1688_isa_probe(struct device *dev, unsigned int n) { struct snd_card *card; int error; error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, sizeof(struct snd_es1688), &card); if (error < 0) return error; error = snd_es1688_legacy_create(card, dev, n); if (error < 0) return error; error = snd_es1688_probe(card, n); if (error < 0) return error; dev_set_drvdata(dev, card); return 0; } static struct isa_driver snd_es1688_driver = { .match = snd_es1688_match, .probe = snd_es1688_isa_probe, #if 0 /* FIXME */ .suspend = snd_es1688_suspend, .resume = snd_es1688_resume, #endif .driver = { .name = DEV_NAME } }; static int snd_es968_pnp_is_probed; #ifdef CONFIG_PNP static int snd_card_es968_pnp(struct snd_card *card, unsigned int n, struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { struct snd_es1688 *chip = card->private_data; struct pnp_dev *pdev; int error; pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL); if (pdev == NULL) return -ENODEV; error = pnp_activate_dev(pdev); if (error < 0) { snd_printk(KERN_ERR "ES968 pnp configure failure\n"); return error; } port[n] = pnp_port_start(pdev, 0); dma8[n] = pnp_dma(pdev, 0); irq[n] = pnp_irq(pdev, 0); return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO); } static int snd_es968_pnp_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { struct snd_card *card; static unsigned int dev; int error; if (snd_es968_pnp_is_probed) return -EBUSY; for ( ; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev == SNDRV_CARDS) return -ENODEV; error = snd_devm_card_new(&pcard->card->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_es1688), &card); if (error < 0) return error; error = snd_card_es968_pnp(card, dev, pcard, pid); if (error < 0) return error; error = snd_es1688_probe(card, dev); if (error < 0) return error; pnp_set_card_drvdata(pcard, card); snd_es968_pnp_is_probed = 1; return 0; } static void snd_es968_pnp_remove(struct pnp_card_link *pcard) { snd_es968_pnp_is_probed = 0; } #ifdef CONFIG_PM static int snd_es968_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { struct snd_card *card = pnp_get_card_drvdata(pcard); snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); return 0; } static int snd_es968_pnp_resume(struct pnp_card_link *pcard) { struct snd_card *card = pnp_get_card_drvdata(pcard); struct snd_es1688 *chip = card->private_data; snd_es1688_reset(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static const struct pnp_card_device_id snd_es968_pnpids[] = { { .id = "ESS0968", .devs = { { "@@@0968" }, } }, { .id = "ESS0968", .devs = { { "ESS0968" }, } }, { .id = "", } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids); static struct pnp_card_driver es968_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = DEV_NAME " PnP", .id_table = snd_es968_pnpids, .probe = snd_es968_pnp_detect, .remove = snd_es968_pnp_remove, #ifdef CONFIG_PM .suspend = snd_es968_pnp_suspend, .resume = snd_es968_pnp_resume, #endif }; #endif static int __init alsa_card_es1688_init(void) { #ifdef CONFIG_PNP pnp_register_card_driver(&es968_pnpc_driver); if (snd_es968_pnp_is_probed) return 0; pnp_unregister_card_driver(&es968_pnpc_driver); #endif return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS); } static void __exit alsa_card_es1688_exit(void) { if (!snd_es968_pnp_is_probed) { isa_unregister_driver(&snd_es1688_driver); return; } #ifdef CONFIG_PNP pnp_unregister_card_driver(&es968_pnpc_driver); #endif } module_init(alsa_card_es1688_init); module_exit(alsa_card_es1688_exit);
linux-master
sound/isa/es1688/es1688.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of ESS ES1688/688/488 chip */ #include <linux/init.h> #include <linux/interrupt.h> #include <linux/delay.h> #include <linux/slab.h> #include <linux/ioport.h> #include <linux/module.h> #include <linux/io.h> #include <sound/core.h> #include <sound/es1688.h> #include <sound/initval.h> #include <asm/dma.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("ESS ESx688 lowlevel module"); MODULE_LICENSE("GPL"); static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val) { int i; for (i = 10000; i; i--) if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) { outb(val, ES1688P(chip, COMMAND)); return 1; } #ifdef CONFIG_SND_DEBUG printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val); #endif return 0; } static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip) { int i; for (i = 1000; i; i--) if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) return inb(ES1688P(chip, READ)); snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL))); return -ENODEV; } static int snd_es1688_write(struct snd_es1688 *chip, unsigned char reg, unsigned char data) { if (!snd_es1688_dsp_command(chip, reg)) return 0; return snd_es1688_dsp_command(chip, data); } static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg) { /* Read a byte from an extended mode register of ES1688 */ if (!snd_es1688_dsp_command(chip, 0xc0)) return -1; if (!snd_es1688_dsp_command(chip, reg)) return -1; return snd_es1688_dsp_get_byte(chip); } void snd_es1688_mixer_write(struct snd_es1688 *chip, unsigned char reg, unsigned char data) { outb(reg, ES1688P(chip, MIXER_ADDR)); udelay(10); outb(data, ES1688P(chip, MIXER_DATA)); udelay(10); } static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg) { unsigned char result; outb(reg, ES1688P(chip, MIXER_ADDR)); udelay(10); result = inb(ES1688P(chip, MIXER_DATA)); udelay(10); return result; } int snd_es1688_reset(struct snd_es1688 *chip) { int i; outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */ udelay(10); outb(0, ES1688P(chip, RESET)); udelay(30); for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++); if (inb(ES1688P(chip, READ)) != 0xaa) { snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port); return -ENODEV; } snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */ return 0; } EXPORT_SYMBOL(snd_es1688_reset); static int snd_es1688_probe(struct snd_es1688 *chip) { unsigned long flags; unsigned short major, minor; int i; /* * initialization sequence */ spin_lock_irqsave(&chip->reg_lock, flags); /* Some ESS1688 cards need this */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */ inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */ inb(ES1688P(chip, ENABLE0)); /* ENABLE0 */ if (snd_es1688_reset(chip) < 0) { snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ))); spin_unlock_irqrestore(&chip->reg_lock, flags); return -ENODEV; } snd_es1688_dsp_command(chip, 0xe7); /* return identification */ for (i = 1000, major = minor = 0; i; i--) { if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) { if (major == 0) { major = inb(ES1688P(chip, READ)); } else { minor = inb(ES1688P(chip, READ)); } } } spin_unlock_irqrestore(&chip->reg_lock, flags); snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor); chip->version = (major << 8) | minor; if (!chip->version) return -ENODEV; /* probably SB */ switch (chip->version & 0xfff0) { case 0x4880: snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, " "but driver is in another place\n", chip->port); return -ENODEV; case 0x6880: break; default: snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip " "with version 0x%x (Jazz16 soundcard?)\n", chip->port, chip->version); return -ENODEV; } spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */ snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */ spin_unlock_irqrestore(&chip->reg_lock, flags); /* enable joystick, but disable OPL3 */ spin_lock_irqsave(&chip->mixer_lock, flags); snd_es1688_mixer_write(chip, 0x40, 0x01); spin_unlock_irqrestore(&chip->mixer_lock, flags); return 0; } static int snd_es1688_init(struct snd_es1688 * chip, int enable) { static const int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1}; unsigned long flags; int cfg, irq_bits, dma, dma_bits, tmp, tmp1; /* ok.. setup MPU-401 port and joystick and OPL3 */ cfg = 0x01; /* enable joystick, but disable OPL3 */ if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) { tmp = (chip->mpu_port & 0x0f0) >> 4; if (tmp <= 3) { switch (chip->mpu_irq) { case 9: tmp1 = 4; break; case 5: tmp1 = 5; break; case 7: tmp1 = 6; break; case 10: tmp1 = 7; break; default: tmp1 = 0; } if (tmp1) { cfg |= (tmp << 3) | (tmp1 << 5); } } } #if 0 snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg); #endif spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_mixer_write(chip, 0x40, cfg); spin_unlock_irqrestore(&chip->reg_lock, flags); /* --- */ spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_read(chip, 0xb1); snd_es1688_read(chip, 0xb2); spin_unlock_irqrestore(&chip->reg_lock, flags); if (enable) { cfg = 0xf0; /* enable only DMA counter interrupt */ irq_bits = irqs[chip->irq & 0x0f]; if (irq_bits < 0) { snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d " "for ES1688 chip!!\n", chip->port, chip->irq); #if 0 irq_bits = 0; cfg = 0x10; #endif return -EINVAL; } spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2)); spin_unlock_irqrestore(&chip->reg_lock, flags); cfg = 0xf0; /* extended mode DMA enable */ dma = chip->dma8; if (dma > 3 || dma == 2) { snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d " "for ES1688 chip!!\n", chip->port, dma); #if 0 dma_bits = 0; cfg = 0x00; /* disable all DMA */ #endif return -EINVAL; } else { dma_bits = dma; if (dma != 3) dma_bits++; } spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2)); spin_unlock_irqrestore(&chip->reg_lock, flags); } else { spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */ snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */ spin_unlock_irqrestore(&chip->reg_lock, flags); } spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_read(chip, 0xb1); snd_es1688_read(chip, 0xb2); snd_es1688_reset(chip); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } /* */ static const struct snd_ratnum clocks[2] = { { .num = 795444, .den_min = 1, .den_max = 128, .den_step = 1, }, { .num = 397722, .den_min = 1, .den_max = 128, .den_step = 1, } }; static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = { .nrats = 2, .rats = clocks, }; static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; unsigned int bits, divider; if (runtime->rate_num == clocks[0].num) bits = 256 - runtime->rate_den; else bits = 128 - runtime->rate_den; /* set filter register */ divider = 256 - 7160000*20/(8*82*runtime->rate); /* write result to hardware */ snd_es1688_write(chip, 0xa1, bits); snd_es1688_write(chip, 0xa2, divider); } static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value) { int val; if (cmd == SNDRV_PCM_TRIGGER_STOP) { value = 0x00; } else if (cmd != SNDRV_PCM_TRIGGER_START) { return -EINVAL; } spin_lock(&chip->reg_lock); chip->trigger_value = value; val = snd_es1688_read(chip, 0xb8); if ((val < 0) || (val & 0x0f) == value) { spin_unlock(&chip->reg_lock); return -EINVAL; /* something is wrong */ } #if 0 printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value); printk(KERN_DEBUG "trigger: pointer = 0x%x\n", snd_dma_pointer(chip->dma8, chip->dma_size)); #endif snd_es1688_write(chip, 0xb8, (val & 0xf0) | value); spin_unlock(&chip->reg_lock); return 0; } static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_es1688 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); chip->dma_size = size; spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_reset(chip); snd_es1688_set_rate(chip, substream); snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */ snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels)); snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */ if (runtime->channels == 1) { if (snd_pcm_format_width(runtime->format) == 8) { /* 8. bit mono */ snd_es1688_write(chip, 0xb6, 0x80); snd_es1688_write(chip, 0xb7, 0x51); snd_es1688_write(chip, 0xb7, 0xd0); } else { /* 16. bit mono */ snd_es1688_write(chip, 0xb6, 0x00); snd_es1688_write(chip, 0xb7, 0x71); snd_es1688_write(chip, 0xb7, 0xf4); } } else { if (snd_pcm_format_width(runtime->format) == 8) { /* 8. bit stereo */ snd_es1688_write(chip, 0xb6, 0x80); snd_es1688_write(chip, 0xb7, 0x51); snd_es1688_write(chip, 0xb7, 0x98); } else { /* 16. bit stereo */ snd_es1688_write(chip, 0xb6, 0x00); snd_es1688_write(chip, 0xb7, 0x71); snd_es1688_write(chip, 0xb7, 0xbc); } } snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50); snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50); snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON); spin_unlock_irqrestore(&chip->reg_lock, flags); /* --- */ count = -count; snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_write(chip, 0xa4, (unsigned char) count); snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8)); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); return snd_es1688_trigger(chip, cmd, 0x05); } static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream) { unsigned long flags; struct snd_es1688 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); chip->dma_size = size; spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_reset(chip); snd_es1688_set_rate(chip, substream); snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF); snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */ snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels)); snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */ if (runtime->channels == 1) { if (snd_pcm_format_width(runtime->format) == 8) { /* 8. bit mono */ snd_es1688_write(chip, 0xb7, 0x51); snd_es1688_write(chip, 0xb7, 0xd0); } else { /* 16. bit mono */ snd_es1688_write(chip, 0xb7, 0x71); snd_es1688_write(chip, 0xb7, 0xf4); } } else { if (snd_pcm_format_width(runtime->format) == 8) { /* 8. bit stereo */ snd_es1688_write(chip, 0xb7, 0x51); snd_es1688_write(chip, 0xb7, 0x98); } else { /* 16. bit stereo */ snd_es1688_write(chip, 0xb7, 0x71); snd_es1688_write(chip, 0xb7, 0xbc); } } snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50); snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50); spin_unlock_irqrestore(&chip->reg_lock, flags); /* --- */ count = -count; snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); spin_lock_irqsave(&chip->reg_lock, flags); snd_es1688_write(chip, 0xa4, (unsigned char) count); snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8)); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); return snd_es1688_trigger(chip, cmd, 0x0f); } static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id) { struct snd_es1688 *chip = dev_id; if (chip->trigger_value == 0x05) /* ok.. playback is active */ snd_pcm_period_elapsed(chip->playback_substream); if (chip->trigger_value == 0x0f) /* ok.. capture is active */ snd_pcm_period_elapsed(chip->capture_substream); inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */ return IRQ_HANDLED; } static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); size_t ptr; if (chip->trigger_value != 0x05) return 0; ptr = snd_dma_pointer(chip->dma8, chip->dma_size); return bytes_to_frames(substream->runtime, ptr); } static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); size_t ptr; if (chip->trigger_value != 0x0f) return 0; ptr = snd_dma_pointer(chip->dma8, chip->dma_size); return bytes_to_frames(substream->runtime, ptr); } /* */ static const struct snd_pcm_hardware snd_es1688_playback = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 65536, .period_bytes_min = 64, .period_bytes_max = 65536, .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_es1688_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = 65536, .period_bytes_min = 64, .period_bytes_max = 65536, .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; /* */ static int snd_es1688_playback_open(struct snd_pcm_substream *substream) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; if (chip->capture_substream != NULL) return -EAGAIN; chip->playback_substream = substream; runtime->hw = snd_es1688_playback; snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_clocks); return 0; } static int snd_es1688_capture_open(struct snd_pcm_substream *substream) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; if (chip->playback_substream != NULL) return -EAGAIN; chip->capture_substream = substream; runtime->hw = snd_es1688_capture; snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_clocks); return 0; } static int snd_es1688_playback_close(struct snd_pcm_substream *substream) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); chip->playback_substream = NULL; return 0; } static int snd_es1688_capture_close(struct snd_pcm_substream *substream) { struct snd_es1688 *chip = snd_pcm_substream_chip(substream); chip->capture_substream = NULL; return 0; } static int snd_es1688_free(struct snd_es1688 *chip) { if (chip->hardware != ES1688_HW_UNDEF) snd_es1688_init(chip, 0); release_and_free_resource(chip->res_port); if (chip->irq >= 0) free_irq(chip->irq, (void *) chip); if (chip->dma8 >= 0) { disable_dma(chip->dma8); free_dma(chip->dma8); } return 0; } static int snd_es1688_dev_free(struct snd_device *device) { struct snd_es1688 *chip = device->device_data; return snd_es1688_free(chip); } static const char *snd_es1688_chip_id(struct snd_es1688 *chip) { static char tmp[16]; sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f); return tmp; } int snd_es1688_create(struct snd_card *card, struct snd_es1688 *chip, unsigned long port, unsigned long mpu_port, int irq, int mpu_irq, int dma8, unsigned short hardware) { static const struct snd_device_ops ops = { .dev_free = snd_es1688_dev_free, }; int err; if (chip == NULL) return -ENOMEM; chip->irq = -1; chip->dma8 = -1; chip->hardware = ES1688_HW_UNDEF; chip->res_port = request_region(port + 4, 12, "ES1688"); if (chip->res_port == NULL) { snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4); err = -EBUSY; goto exit; } err = request_irq(irq, snd_es1688_interrupt, 0, "ES1688", (void *) chip); if (err < 0) { snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq); goto exit; } chip->irq = irq; card->sync_irq = chip->irq; err = request_dma(dma8, "ES1688"); if (err < 0) { snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8); goto exit; } chip->dma8 = dma8; spin_lock_init(&chip->reg_lock); spin_lock_init(&chip->mixer_lock); chip->port = port; mpu_port &= ~0x000f; if (mpu_port < 0x300 || mpu_port > 0x330) mpu_port = 0; chip->mpu_port = mpu_port; chip->mpu_irq = mpu_irq; chip->hardware = hardware; err = snd_es1688_probe(chip); if (err < 0) goto exit; err = snd_es1688_init(chip, 1); if (err < 0) goto exit; /* Register device */ err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); exit: if (err) snd_es1688_free(chip); return err; } static const struct snd_pcm_ops snd_es1688_playback_ops = { .open = snd_es1688_playback_open, .close = snd_es1688_playback_close, .prepare = snd_es1688_playback_prepare, .trigger = snd_es1688_playback_trigger, .pointer = snd_es1688_playback_pointer, }; static const struct snd_pcm_ops snd_es1688_capture_ops = { .open = snd_es1688_capture_open, .close = snd_es1688_capture_close, .prepare = snd_es1688_capture_prepare, .trigger = snd_es1688_capture_trigger, .pointer = snd_es1688_capture_pointer, }; int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip, int device) { struct snd_pcm *pcm; int err; err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm); if (err < 0) return err; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops); pcm->private_data = chip; pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX; strcpy(pcm->name, snd_es1688_chip_id(chip)); chip->pcm = pcm; snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, 64*1024); return 0; } /* * MIXER part */ static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[8] = { "Mic", "Mic Master", "CD", "AOUT", "Mic1", "Mix", "Line", "Master" }; return snd_ctl_enum_info(uinfo, 1, 8, texts); } static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7; return 0; } static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned char oval, nval; int change; if (ucontrol->value.enumerated.item[0] > 8) return -EINVAL; spin_lock_irqsave(&chip->reg_lock, flags); oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV); nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15); change = nval != oval; if (change) snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_es1688_info_single, \ .get = snd_es1688_get_single, .put = snd_es1688_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) } static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 16) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned char oval, nval; nval = (ucontrol->value.integer.value[0] & mask); if (invert) nval = mask - nval; nval <<= shift; spin_lock_irqsave(&chip->reg_lock, flags); oval = snd_es1688_mixer_read(chip, reg); nval = (oval & ~(mask << shift)) | nval; change = nval != oval; if (change) snd_es1688_mixer_write(chip, reg, nval); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_es1688_info_double, \ .get = snd_es1688_get_double, .put = snd_es1688_put_double, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) } static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; unsigned char left, right; spin_lock_irqsave(&chip->reg_lock, flags); if (left_reg < 0xa0) left = snd_es1688_mixer_read(chip, left_reg); else left = snd_es1688_read(chip, left_reg); if (left_reg != right_reg) { if (right_reg < 0xa0) right = snd_es1688_mixer_read(chip, right_reg); else right = snd_es1688_read(chip, right_reg); } else right = left; spin_unlock_irqrestore(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (left >> shift_left) & mask; ucontrol->value.integer.value[1] = (right >> shift_right) & mask; if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change; unsigned char val1, val2, oval1, oval2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; spin_lock_irqsave(&chip->reg_lock, flags); if (left_reg != right_reg) { if (left_reg < 0xa0) oval1 = snd_es1688_mixer_read(chip, left_reg); else oval1 = snd_es1688_read(chip, left_reg); if (right_reg < 0xa0) oval2 = snd_es1688_mixer_read(chip, right_reg); else oval2 = snd_es1688_read(chip, right_reg); val1 = (oval1 & ~(mask << shift_left)) | val1; val2 = (oval2 & ~(mask << shift_right)) | val2; change = val1 != oval1 || val2 != oval2; if (change) { if (left_reg < 0xa0) snd_es1688_mixer_write(chip, left_reg, val1); else snd_es1688_write(chip, left_reg, val1); if (right_reg < 0xa0) snd_es1688_mixer_write(chip, right_reg, val1); else snd_es1688_write(chip, right_reg, val1); } } else { if (left_reg < 0xa0) oval1 = snd_es1688_mixer_read(chip, left_reg); else oval1 = snd_es1688_read(chip, left_reg); val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; change = val1 != oval1; if (change) { if (left_reg < 0xa0) snd_es1688_mixer_write(chip, left_reg, val1); else snd_es1688_write(chip, left_reg, val1); } } spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } static const struct snd_kcontrol_new snd_es1688_controls[] = { ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0), ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0), ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0), ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0), ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0), ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0), ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0), ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0), ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0), ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1), { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Capture Source", .info = snd_es1688_info_mux, .get = snd_es1688_get_mux, .put = snd_es1688_put_mux, }, }; #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2) static const unsigned char snd_es1688_init_table[][2] = { { ES1688_MASTER_DEV, 0 }, { ES1688_PCM_DEV, 0 }, { ES1688_LINE_DEV, 0 }, { ES1688_CD_DEV, 0 }, { ES1688_FM_DEV, 0 }, { ES1688_MIC_DEV, 0 }, { ES1688_AUX_DEV, 0 }, { ES1688_SPEAKER_DEV, 0 }, { ES1688_RECLEV_DEV, 0 }, { ES1688_REC_DEV, 0x17 } }; int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip) { unsigned int idx; int err; unsigned char reg, val; if (snd_BUG_ON(!chip || !card)) return -EINVAL; strcpy(card->mixername, snd_es1688_chip_id(chip)); for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip)); if (err < 0) return err; } for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) { reg = snd_es1688_init_table[idx][0]; val = snd_es1688_init_table[idx][1]; if (reg < 0xa0) snd_es1688_mixer_write(chip, reg, val); else snd_es1688_write(chip, reg, val); } return 0; } EXPORT_SYMBOL(snd_es1688_mixer_write); EXPORT_SYMBOL(snd_es1688_create); EXPORT_SYMBOL(snd_es1688_pcm); EXPORT_SYMBOL(snd_es1688_mixer);
linux-master
sound/isa/es1688/es1688_lib.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of CS4235/4236B/4237B/4238B/4239 chips * * Note: * ----- * * Bugs: * ----- */ /* * Indirect control registers (CS4236B+) * * C0 * D8: WSS reset (all chips) * * C1 (all chips except CS4236) * D7-D5: version * D4-D0: chip id * 11101 - CS4235 * 01011 - CS4236B * 01000 - CS4237B * 01001 - CS4238B * 11110 - CS4239 * * C2 * D7-D4: 3D Space (CS4235,CS4237B,CS4238B,CS4239) * D3-D0: 3D Center (CS4237B); 3D Volume (CS4238B) * * C3 * D7: 3D Enable (CS4237B) * D6: 3D Mono Enable (CS4237B) * D5: 3D Serial Output (CS4237B,CS4238B) * D4: 3D Enable (CS4235,CS4238B,CS4239) * * C4 * D7: consumer serial port enable (CS4237B,CS4238B) * D6: channels status block reset (CS4237B,CS4238B) * D5: user bit in sub-frame of digital audio data (CS4237B,CS4238B) * D4: validity bit in sub-frame of digital audio data (CS4237B,CS4238B) * * C5 lower channel status (digital serial data description) (CS4237B,CS4238B) * D7-D6: first two bits of category code * D5: lock * D4-D3: pre-emphasis (0 = none, 1 = 50/15us) * D2: copy/copyright (0 = copy inhibited) * D1: 0 = digital audio / 1 = non-digital audio * * C6 upper channel status (digital serial data description) (CS4237B,CS4238B) * D7-D6: sample frequency (0 = 44.1kHz) * D5: generation status (0 = no indication, 1 = original/commercially precaptureed data) * D4-D0: category code (upper bits) * * C7 reserved (must write 0) * * C8 wavetable control * D7: volume control interrupt enable (CS4235,CS4239) * D6: hardware volume control format (CS4235,CS4239) * D3: wavetable serial port enable (all chips) * D2: DSP serial port switch (all chips) * D1: disable MCLK (all chips) * D0: force BRESET low (all chips) * */ #include <linux/io.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/time.h> #include <linux/wait.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/asoundef.h> #include <sound/initval.h> #include <sound/tlv.h> /* * */ static const unsigned char snd_cs4236_ext_map[18] = { /* CS4236_LEFT_LINE */ 0xff, /* CS4236_RIGHT_LINE */ 0xff, /* CS4236_LEFT_MIC */ 0xdf, /* CS4236_RIGHT_MIC */ 0xdf, /* CS4236_LEFT_MIX_CTRL */ 0xe0 | 0x18, /* CS4236_RIGHT_MIX_CTRL */ 0xe0, /* CS4236_LEFT_FM */ 0xbf, /* CS4236_RIGHT_FM */ 0xbf, /* CS4236_LEFT_DSP */ 0xbf, /* CS4236_RIGHT_DSP */ 0xbf, /* CS4236_RIGHT_LOOPBACK */ 0xbf, /* CS4236_DAC_MUTE */ 0xe0, /* CS4236_ADC_RATE */ 0x01, /* 48kHz */ /* CS4236_DAC_RATE */ 0x01, /* 48kHz */ /* CS4236_LEFT_MASTER */ 0xbf, /* CS4236_RIGHT_MASTER */ 0xbf, /* CS4236_LEFT_WAVE */ 0xbf, /* CS4236_RIGHT_WAVE */ 0xbf }; /* * */ static void snd_cs4236_ctrl_out(struct snd_wss *chip, unsigned char reg, unsigned char val) { outb(reg, chip->cport + 3); outb(chip->cimage[reg] = val, chip->cport + 4); } static unsigned char snd_cs4236_ctrl_in(struct snd_wss *chip, unsigned char reg) { outb(reg, chip->cport + 3); return inb(chip->cport + 4); } /* * PCM */ #define CLOCKS 8 static const struct snd_ratnum clocks[CLOCKS] = { { .num = 16934400, .den_min = 353, .den_max = 353, .den_step = 1 }, { .num = 16934400, .den_min = 529, .den_max = 529, .den_step = 1 }, { .num = 16934400, .den_min = 617, .den_max = 617, .den_step = 1 }, { .num = 16934400, .den_min = 1058, .den_max = 1058, .den_step = 1 }, { .num = 16934400, .den_min = 1764, .den_max = 1764, .den_step = 1 }, { .num = 16934400, .den_min = 2117, .den_max = 2117, .den_step = 1 }, { .num = 16934400, .den_min = 2558, .den_max = 2558, .den_step = 1 }, { .num = 16934400/16, .den_min = 21, .den_max = 192, .den_step = 1 } }; static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = { .nrats = CLOCKS, .rats = clocks, }; static int snd_cs4236_xrate(struct snd_pcm_runtime *runtime) { return snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_clocks); } static unsigned char divisor_to_rate_register(unsigned int divisor) { switch (divisor) { case 353: return 1; case 529: return 2; case 617: return 3; case 1058: return 4; case 1764: return 5; case 2117: return 6; case 2558: return 7; default: if (divisor < 21 || divisor > 192) { snd_BUG(); return 192; } return divisor; } } static void snd_cs4236_playback_format(struct snd_wss *chip, struct snd_pcm_hw_params *params, unsigned char pdfr) { unsigned long flags; unsigned char rate = divisor_to_rate_register(params->rate_den); spin_lock_irqsave(&chip->reg_lock, flags); /* set fast playback format change and clean playback FIFO */ snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10); snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr & 0xf0); snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] & ~0x10); snd_cs4236_ext_out(chip, CS4236_DAC_RATE, rate); spin_unlock_irqrestore(&chip->reg_lock, flags); } static void snd_cs4236_capture_format(struct snd_wss *chip, struct snd_pcm_hw_params *params, unsigned char cdfr) { unsigned long flags; unsigned char rate = divisor_to_rate_register(params->rate_den); spin_lock_irqsave(&chip->reg_lock, flags); /* set fast capture format change and clean capture FIFO */ snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20); snd_wss_out(chip, CS4231_REC_FORMAT, cdfr & 0xf0); snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] & ~0x20); snd_cs4236_ext_out(chip, CS4236_ADC_RATE, rate); spin_unlock_irqrestore(&chip->reg_lock, flags); } #ifdef CONFIG_PM static void snd_cs4236_suspend(struct snd_wss *chip) { int reg; unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); for (reg = 0; reg < 32; reg++) chip->image[reg] = snd_wss_in(chip, reg); for (reg = 0; reg < 18; reg++) chip->eimage[reg] = snd_cs4236_ext_in(chip, CS4236_I23VAL(reg)); for (reg = 2; reg < 9; reg++) chip->cimage[reg] = snd_cs4236_ctrl_in(chip, reg); spin_unlock_irqrestore(&chip->reg_lock, flags); } static void snd_cs4236_resume(struct snd_wss *chip) { int reg; unsigned long flags; snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); for (reg = 0; reg < 32; reg++) { switch (reg) { case CS4236_EXT_REG: case CS4231_VERSION: case 27: /* why? CS4235 - master left */ case 29: /* why? CS4235 - master right */ break; default: snd_wss_out(chip, reg, chip->image[reg]); break; } } for (reg = 0; reg < 18; reg++) snd_cs4236_ext_out(chip, CS4236_I23VAL(reg), chip->eimage[reg]); for (reg = 2; reg < 9; reg++) { switch (reg) { case 7: break; default: snd_cs4236_ctrl_out(chip, reg, chip->cimage[reg]); } } spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); } #endif /* CONFIG_PM */ /* * This function does no fail if the chip is not CS4236B or compatible. * It just an equivalent to the snd_wss_create() then. */ int snd_cs4236_create(struct snd_card *card, unsigned long port, unsigned long cport, int irq, int dma1, int dma2, unsigned short hardware, unsigned short hwshare, struct snd_wss **rchip) { struct snd_wss *chip; unsigned char ver1, ver2; unsigned int reg; int err; *rchip = NULL; if (hardware == WSS_HW_DETECT) hardware = WSS_HW_DETECT3; err = snd_wss_create(card, port, cport, irq, dma1, dma2, hardware, hwshare, &chip); if (err < 0) return err; if ((chip->hardware & WSS_HW_CS4236B_MASK) == 0) { snd_printd("chip is not CS4236+, hardware=0x%x\n", chip->hardware); *rchip = chip; return 0; } #if 0 { int idx; for (idx = 0; idx < 8; idx++) snd_printk(KERN_DEBUG "CD%i = 0x%x\n", idx, inb(chip->cport + idx)); for (idx = 0; idx < 9; idx++) snd_printk(KERN_DEBUG "C%i = 0x%x\n", idx, snd_cs4236_ctrl_in(chip, idx)); } #endif if (cport < 0x100 || cport == SNDRV_AUTO_PORT) { snd_printk(KERN_ERR "please, specify control port " "for CS4236+ chips\n"); return -ENODEV; } ver1 = snd_cs4236_ctrl_in(chip, 1); ver2 = snd_cs4236_ext_in(chip, CS4236_VERSION); snd_printdd("CS4236: [0x%lx] C1 (version) = 0x%x, ext = 0x%x\n", cport, ver1, ver2); if (ver1 != ver2) { snd_printk(KERN_ERR "CS4236+ chip detected, but " "control port 0x%lx is not valid\n", cport); return -ENODEV; } snd_cs4236_ctrl_out(chip, 0, 0x00); snd_cs4236_ctrl_out(chip, 2, 0xff); snd_cs4236_ctrl_out(chip, 3, 0x00); snd_cs4236_ctrl_out(chip, 4, 0x80); reg = ((IEC958_AES1_CON_PCM_CODER & 3) << 6) | IEC958_AES0_CON_EMPHASIS_NONE; snd_cs4236_ctrl_out(chip, 5, reg); snd_cs4236_ctrl_out(chip, 6, IEC958_AES1_CON_PCM_CODER >> 2); snd_cs4236_ctrl_out(chip, 7, 0x00); /* * 0x8c for C8 is valid for Turtle Beach Malibu - the IEC-958 * output is working with this setup, other hardware should * have different signal paths and this value should be * selectable in the future */ snd_cs4236_ctrl_out(chip, 8, 0x8c); chip->rate_constraint = snd_cs4236_xrate; chip->set_playback_format = snd_cs4236_playback_format; chip->set_capture_format = snd_cs4236_capture_format; #ifdef CONFIG_PM chip->suspend = snd_cs4236_suspend; chip->resume = snd_cs4236_resume; #endif /* initialize extended registers */ for (reg = 0; reg < sizeof(snd_cs4236_ext_map); reg++) snd_cs4236_ext_out(chip, CS4236_I23VAL(reg), snd_cs4236_ext_map[reg]); /* initialize compatible but more featured registers */ snd_wss_out(chip, CS4231_LEFT_INPUT, 0x40); snd_wss_out(chip, CS4231_RIGHT_INPUT, 0x40); snd_wss_out(chip, CS4231_AUX1_LEFT_INPUT, 0xff); snd_wss_out(chip, CS4231_AUX1_RIGHT_INPUT, 0xff); snd_wss_out(chip, CS4231_AUX2_LEFT_INPUT, 0xdf); snd_wss_out(chip, CS4231_AUX2_RIGHT_INPUT, 0xdf); snd_wss_out(chip, CS4231_RIGHT_LINE_IN, 0xff); snd_wss_out(chip, CS4231_LEFT_LINE_IN, 0xff); snd_wss_out(chip, CS4231_RIGHT_LINE_IN, 0xff); switch (chip->hardware) { case WSS_HW_CS4235: case WSS_HW_CS4239: snd_wss_out(chip, CS4235_LEFT_MASTER, 0xff); snd_wss_out(chip, CS4235_RIGHT_MASTER, 0xff); break; } *rchip = chip; return 0; } int snd_cs4236_pcm(struct snd_wss *chip, int device) { int err; err = snd_wss_pcm(chip, device); if (err < 0) return err; chip->pcm->info_flags &= ~SNDRV_PCM_INFO_JOINT_DUPLEX; return 0; } /* * MIXER */ #define CS4236_SINGLE(xname, xindex, reg, shift, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_cs4236_info_single, \ .get = snd_cs4236_get_single, .put = snd_cs4236_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) } #define CS4236_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .info = snd_cs4236_info_single, \ .get = snd_cs4236_get_single, .put = snd_cs4236_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \ .tlv = { .p = (xtlv) } } static int snd_cs4236_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 16) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_cs4236_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->eimage[CS4236_REG(reg)] >> shift) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } static int snd_cs4236_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned short val; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; val <<= shift; spin_lock_irqsave(&chip->reg_lock, flags); val = (chip->eimage[CS4236_REG(reg)] & ~(mask << shift)) | val; change = val != chip->eimage[CS4236_REG(reg)]; snd_cs4236_ext_out(chip, reg, val); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define CS4236_SINGLEC(xname, xindex, reg, shift, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_cs4236_info_single, \ .get = snd_cs4236_get_singlec, .put = snd_cs4236_put_singlec, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) } static int snd_cs4236_get_singlec(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->cimage[reg] >> shift) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } static int snd_cs4236_put_singlec(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned short val; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; val <<= shift; spin_lock_irqsave(&chip->reg_lock, flags); val = (chip->cimage[reg] & ~(mask << shift)) | val; change = val != chip->cimage[reg]; snd_cs4236_ctrl_out(chip, reg, val); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define CS4236_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_cs4236_info_double, \ .get = snd_cs4236_get_double, .put = snd_cs4236_put_double, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) } #define CS4236_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, \ shift_right, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .info = snd_cs4236_info_double, \ .get = snd_cs4236_get_double, .put = snd_cs4236_put_double, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | \ (shift_right << 19) | (mask << 24) | (invert << 22), \ .tlv = { .p = (xtlv) } } static int snd_cs4236_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_cs4236_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->eimage[CS4236_REG(left_reg)] >> shift_left) & mask; ucontrol->value.integer.value[1] = (chip->eimage[CS4236_REG(right_reg)] >> shift_right) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } static int snd_cs4236_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change; unsigned short val1, val2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; spin_lock_irqsave(&chip->reg_lock, flags); if (left_reg != right_reg) { val1 = (chip->eimage[CS4236_REG(left_reg)] & ~(mask << shift_left)) | val1; val2 = (chip->eimage[CS4236_REG(right_reg)] & ~(mask << shift_right)) | val2; change = val1 != chip->eimage[CS4236_REG(left_reg)] || val2 != chip->eimage[CS4236_REG(right_reg)]; snd_cs4236_ext_out(chip, left_reg, val1); snd_cs4236_ext_out(chip, right_reg, val2); } else { val1 = (chip->eimage[CS4236_REG(left_reg)] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; change = val1 != chip->eimage[CS4236_REG(left_reg)]; snd_cs4236_ext_out(chip, left_reg, val1); } spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define CS4236_DOUBLE1(xname, xindex, left_reg, right_reg, shift_left, \ shift_right, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_cs4236_info_double, \ .get = snd_cs4236_get_double1, .put = snd_cs4236_put_double1, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) } #define CS4236_DOUBLE1_TLV(xname, xindex, left_reg, right_reg, shift_left, \ shift_right, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .info = snd_cs4236_info_double, \ .get = snd_cs4236_get_double1, .put = snd_cs4236_put_double1, \ .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | \ (shift_right << 19) | (mask << 24) | (invert << 22), \ .tlv = { .p = (xtlv) } } static int snd_cs4236_get_double1(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask; ucontrol->value.integer.value[1] = (chip->eimage[CS4236_REG(right_reg)] >> shift_right) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } static int snd_cs4236_put_double1(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change; unsigned short val1, val2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; spin_lock_irqsave(&chip->reg_lock, flags); val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1; val2 = (chip->eimage[CS4236_REG(right_reg)] & ~(mask << shift_right)) | val2; change = val1 != chip->image[left_reg] || val2 != chip->eimage[CS4236_REG(right_reg)]; snd_wss_out(chip, left_reg, val1); snd_cs4236_ext_out(chip, right_reg, val2); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define CS4236_MASTER_DIGITAL(xname, xindex, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .info = snd_cs4236_info_double, \ .get = snd_cs4236_get_master_digital, .put = snd_cs4236_put_master_digital, \ .private_value = 71 << 24, \ .tlv = { .p = (xtlv) } } static inline int snd_cs4236_mixer_master_digital_invert_volume(int vol) { return (vol < 64) ? 63 - vol : 64 + (71 - vol); } static int snd_cs4236_get_master_digital(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = snd_cs4236_mixer_master_digital_invert_volume(chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] & 0x7f); ucontrol->value.integer.value[1] = snd_cs4236_mixer_master_digital_invert_volume(chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)] & 0x7f); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_cs4236_put_master_digital(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned short val1, val2; val1 = snd_cs4236_mixer_master_digital_invert_volume(ucontrol->value.integer.value[0] & 0x7f); val2 = snd_cs4236_mixer_master_digital_invert_volume(ucontrol->value.integer.value[1] & 0x7f); spin_lock_irqsave(&chip->reg_lock, flags); val1 = (chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] & ~0x7f) | val1; val2 = (chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)] & ~0x7f) | val2; change = val1 != chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] || val2 != chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)]; snd_cs4236_ext_out(chip, CS4236_LEFT_MASTER, val1); snd_cs4236_ext_out(chip, CS4236_RIGHT_MASTER, val2); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } #define CS4235_OUTPUT_ACCU(xname, xindex, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .info = snd_cs4236_info_double, \ .get = snd_cs4235_get_output_accu, .put = snd_cs4235_put_output_accu, \ .private_value = 3 << 24, \ .tlv = { .p = (xtlv) } } static inline int snd_cs4235_mixer_output_accu_get_volume(int vol) { switch ((vol >> 5) & 3) { case 0: return 1; case 1: return 3; case 2: return 2; case 3: return 0; } return 3; } static inline int snd_cs4235_mixer_output_accu_set_volume(int vol) { switch (vol & 3) { case 0: return 3 << 5; case 1: return 0 << 5; case 2: return 2 << 5; case 3: return 1 << 5; } return 1 << 5; } static int snd_cs4235_get_output_accu(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = snd_cs4235_mixer_output_accu_get_volume(chip->image[CS4235_LEFT_MASTER]); ucontrol->value.integer.value[1] = snd_cs4235_mixer_output_accu_get_volume(chip->image[CS4235_RIGHT_MASTER]); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_cs4235_put_output_accu(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned short val1, val2; val1 = snd_cs4235_mixer_output_accu_set_volume(ucontrol->value.integer.value[0]); val2 = snd_cs4235_mixer_output_accu_set_volume(ucontrol->value.integer.value[1]); spin_lock_irqsave(&chip->reg_lock, flags); val1 = (chip->image[CS4235_LEFT_MASTER] & ~(3 << 5)) | val1; val2 = (chip->image[CS4235_RIGHT_MASTER] & ~(3 << 5)) | val2; change = val1 != chip->image[CS4235_LEFT_MASTER] || val2 != chip->image[CS4235_RIGHT_MASTER]; snd_wss_out(chip, CS4235_LEFT_MASTER, val1); snd_wss_out(chip, CS4235_RIGHT_MASTER, val2); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } static const DECLARE_TLV_DB_SCALE(db_scale_7bit, -9450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_6bit_12db_max, -8250, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit_22db_max, -2400, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0); static const DECLARE_TLV_DB_SCALE(db_scale_2bit, -1800, 600, 0); static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0); static const struct snd_kcontrol_new snd_cs4236_controls[] = { CS4236_DOUBLE("Master Digital Playback Switch", 0, CS4236_LEFT_MASTER, CS4236_RIGHT_MASTER, 7, 7, 1, 1), CS4236_DOUBLE("Master Digital Capture Switch", 0, CS4236_DAC_MUTE, CS4236_DAC_MUTE, 7, 6, 1, 1), CS4236_MASTER_DIGITAL("Master Digital Volume", 0, db_scale_7bit), CS4236_DOUBLE_TLV("Capture Boost Volume", 0, CS4236_LEFT_MIX_CTRL, CS4236_RIGHT_MIX_CTRL, 5, 5, 3, 1, db_scale_2bit), WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1, db_scale_6bit), CS4236_DOUBLE("DSP Playback Switch", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 7, 7, 1, 1), CS4236_DOUBLE_TLV("DSP Playback Volume", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 0, 0, 63, 1, db_scale_6bit), CS4236_DOUBLE("FM Playback Switch", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 7, 7, 1, 1), CS4236_DOUBLE_TLV("FM Playback Volume", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 0, 0, 63, 1, db_scale_6bit), CS4236_DOUBLE("Wavetable Playback Switch", 0, CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 7, 7, 1, 1), CS4236_DOUBLE_TLV("Wavetable Playback Volume", 0, CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 0, 0, 63, 1, db_scale_6bit_12db_max), WSS_DOUBLE("Synth Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1), WSS_DOUBLE_TLV("Synth Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_DOUBLE("Synth Capture Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 6, 6, 1, 1), WSS_DOUBLE("Synth Capture Bypass", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 5, 5, 1, 1), CS4236_DOUBLE("Mic Playback Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 6, 6, 1, 1), CS4236_DOUBLE("Mic Capture Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 7, 7, 1, 1), CS4236_DOUBLE_TLV("Mic Volume", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 0, 0, 31, 1, db_scale_5bit_22db_max), CS4236_DOUBLE("Mic Playback Boost (+20dB)", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 5, 5, 1, 0), WSS_DOUBLE("Line Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("Line Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_DOUBLE("Line Capture Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 6, 6, 1, 1), WSS_DOUBLE("Line Capture Bypass", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 5, 5, 1, 1), WSS_DOUBLE("CD Playback Switch", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("CD Volume", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_DOUBLE("CD Capture Switch", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 6, 6, 1, 1), CS4236_DOUBLE1("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, CS4236_RIGHT_MIX_CTRL, 6, 7, 1, 1), CS4236_DOUBLE1("Beep Playback Switch", 0, CS4231_MONO_CTRL, CS4236_LEFT_MIX_CTRL, 7, 7, 1, 1), WSS_SINGLE_TLV("Beep Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1, db_scale_4bit), WSS_SINGLE("Beep Bypass Playback Switch", 0, CS4231_MONO_CTRL, 5, 1, 0), WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0, db_scale_rec_gain), WSS_DOUBLE("Analog Loopback Capture Switch", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 7, 7, 1, 0), WSS_SINGLE("Loopback Digital Playback Switch", 0, CS4231_LOOPBACK, 0, 1, 0), CS4236_DOUBLE1_TLV("Loopback Digital Playback Volume", 0, CS4231_LOOPBACK, CS4236_RIGHT_LOOPBACK, 2, 0, 63, 1, db_scale_6bit), }; static const DECLARE_TLV_DB_SCALE(db_scale_5bit_6db_max, -5600, 200, 0); static const DECLARE_TLV_DB_SCALE(db_scale_2bit_16db_max, -2400, 800, 0); static const struct snd_kcontrol_new snd_cs4235_controls[] = { WSS_DOUBLE("Master Playback Switch", 0, CS4235_LEFT_MASTER, CS4235_RIGHT_MASTER, 7, 7, 1, 1), WSS_DOUBLE_TLV("Master Playback Volume", 0, CS4235_LEFT_MASTER, CS4235_RIGHT_MASTER, 0, 0, 31, 1, db_scale_5bit_6db_max), CS4235_OUTPUT_ACCU("Playback Volume", 0, db_scale_2bit_16db_max), WSS_DOUBLE("Synth Playback Switch", 1, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1), WSS_DOUBLE("Synth Capture Switch", 1, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 6, 6, 1, 1), WSS_DOUBLE_TLV("Synth Volume", 1, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1, db_scale_5bit_12db_max), CS4236_DOUBLE_TLV("Capture Volume", 0, CS4236_LEFT_MIX_CTRL, CS4236_RIGHT_MIX_CTRL, 5, 5, 3, 1, db_scale_2bit), WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1), WSS_DOUBLE("PCM Capture Switch", 0, CS4236_DAC_MUTE, CS4236_DAC_MUTE, 7, 6, 1, 1), WSS_DOUBLE_TLV("PCM Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1, db_scale_6bit), CS4236_DOUBLE("DSP Switch", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 7, 7, 1, 1), CS4236_DOUBLE("FM Switch", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 7, 7, 1, 1), CS4236_DOUBLE("Wavetable Switch", 0, CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 7, 7, 1, 1), CS4236_DOUBLE("Mic Capture Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 7, 7, 1, 1), CS4236_DOUBLE("Mic Playback Switch", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 6, 6, 1, 1), CS4236_SINGLE_TLV("Mic Volume", 0, CS4236_LEFT_MIC, 0, 31, 1, db_scale_5bit_22db_max), CS4236_SINGLE("Mic Boost (+20dB)", 0, CS4236_LEFT_MIC, 5, 1, 0), WSS_DOUBLE("Line Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE("Line Capture Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 6, 6, 1, 1), WSS_DOUBLE_TLV("Line Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_DOUBLE("CD Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE("CD Capture Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 6, 6, 1, 1), WSS_DOUBLE_TLV("CD Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1, db_scale_5bit_12db_max), CS4236_DOUBLE1("Beep Playback Switch", 0, CS4231_MONO_CTRL, CS4236_LEFT_MIX_CTRL, 7, 7, 1, 1), WSS_SINGLE("Beep Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1), WSS_DOUBLE("Analog Loopback Switch", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 7, 7, 1, 0), }; #define CS4236_IEC958_ENABLE(xname, xindex) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_cs4236_info_single, \ .get = snd_cs4236_get_iec958_switch, .put = snd_cs4236_put_iec958_switch, \ .private_value = 1 << 16 } static int snd_cs4236_get_iec958_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = chip->image[CS4231_ALT_FEATURE_1] & 0x02 ? 1 : 0; #if 0 printk(KERN_DEBUG "get valid: ALT = 0x%x, C3 = 0x%x, C4 = 0x%x, " "C5 = 0x%x, C6 = 0x%x, C8 = 0x%x\n", snd_wss_in(chip, CS4231_ALT_FEATURE_1), snd_cs4236_ctrl_in(chip, 3), snd_cs4236_ctrl_in(chip, 4), snd_cs4236_ctrl_in(chip, 5), snd_cs4236_ctrl_in(chip, 6), snd_cs4236_ctrl_in(chip, 8)); #endif spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_cs4236_put_iec958_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned short enable, val; enable = ucontrol->value.integer.value[0] & 1; mutex_lock(&chip->mce_mutex); snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); val = (chip->image[CS4231_ALT_FEATURE_1] & ~0x0e) | (0<<2) | (enable << 1); change = val != chip->image[CS4231_ALT_FEATURE_1]; snd_wss_out(chip, CS4231_ALT_FEATURE_1, val); val = snd_cs4236_ctrl_in(chip, 4) | 0xc0; snd_cs4236_ctrl_out(chip, 4, val); udelay(100); val &= ~0x40; snd_cs4236_ctrl_out(chip, 4, val); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); mutex_unlock(&chip->mce_mutex); #if 0 printk(KERN_DEBUG "set valid: ALT = 0x%x, C3 = 0x%x, C4 = 0x%x, " "C5 = 0x%x, C6 = 0x%x, C8 = 0x%x\n", snd_wss_in(chip, CS4231_ALT_FEATURE_1), snd_cs4236_ctrl_in(chip, 3), snd_cs4236_ctrl_in(chip, 4), snd_cs4236_ctrl_in(chip, 5), snd_cs4236_ctrl_in(chip, 6), snd_cs4236_ctrl_in(chip, 8)); #endif return change; } static const struct snd_kcontrol_new snd_cs4236_iec958_controls[] = { CS4236_IEC958_ENABLE("IEC958 Output Enable", 0), CS4236_SINGLEC("IEC958 Output Validity", 0, 4, 4, 1, 0), CS4236_SINGLEC("IEC958 Output User", 0, 4, 5, 1, 0), CS4236_SINGLEC("IEC958 Output CSBR", 0, 4, 6, 1, 0), CS4236_SINGLEC("IEC958 Output Channel Status Low", 0, 5, 1, 127, 0), CS4236_SINGLEC("IEC958 Output Channel Status High", 0, 6, 0, 255, 0) }; static const struct snd_kcontrol_new snd_cs4236_3d_controls_cs4235[] = { CS4236_SINGLEC("3D Control - Switch", 0, 3, 4, 1, 0), CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1) }; static const struct snd_kcontrol_new snd_cs4236_3d_controls_cs4237[] = { CS4236_SINGLEC("3D Control - Switch", 0, 3, 7, 1, 0), CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1), CS4236_SINGLEC("3D Control - Center", 0, 2, 0, 15, 1), CS4236_SINGLEC("3D Control - Mono", 0, 3, 6, 1, 0), CS4236_SINGLEC("3D Control - IEC958", 0, 3, 5, 1, 0) }; static const struct snd_kcontrol_new snd_cs4236_3d_controls_cs4238[] = { CS4236_SINGLEC("3D Control - Switch", 0, 3, 4, 1, 0), CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1), CS4236_SINGLEC("3D Control - Volume", 0, 2, 0, 15, 1), CS4236_SINGLEC("3D Control - IEC958", 0, 3, 5, 1, 0) }; int snd_cs4236_mixer(struct snd_wss *chip) { struct snd_card *card; unsigned int idx, count; int err; const struct snd_kcontrol_new *kcontrol; if (snd_BUG_ON(!chip || !chip->card)) return -EINVAL; card = chip->card; strcpy(card->mixername, snd_wss_chip_id(chip)); if (chip->hardware == WSS_HW_CS4235 || chip->hardware == WSS_HW_CS4239) { for (idx = 0; idx < ARRAY_SIZE(snd_cs4235_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4235_controls[idx], chip)); if (err < 0) return err; } } else { for (idx = 0; idx < ARRAY_SIZE(snd_cs4236_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4236_controls[idx], chip)); if (err < 0) return err; } } switch (chip->hardware) { case WSS_HW_CS4235: case WSS_HW_CS4239: count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4235); kcontrol = snd_cs4236_3d_controls_cs4235; break; case WSS_HW_CS4237B: count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4237); kcontrol = snd_cs4236_3d_controls_cs4237; break; case WSS_HW_CS4238B: count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4238); kcontrol = snd_cs4236_3d_controls_cs4238; break; default: count = 0; kcontrol = NULL; } for (idx = 0; idx < count; idx++, kcontrol++) { err = snd_ctl_add(card, snd_ctl_new1(kcontrol, chip)); if (err < 0) return err; } if (chip->hardware == WSS_HW_CS4237B || chip->hardware == WSS_HW_CS4238B) { for (idx = 0; idx < ARRAY_SIZE(snd_cs4236_iec958_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4236_iec958_controls[idx], chip)); if (err < 0) return err; } } return 0; }
linux-master
sound/isa/cs423x/cs4236_lib.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for generic CS4232/CS4235/CS4236/CS4236B/CS4237B/CS4238B/CS4239 chips * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/pnp.h> #include <linux/module.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/opl3.h> #include <sound/initval.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Cirrus Logic CS4232-9"); MODULE_ALIAS("snd_cs4232"); #define IDENT "CS4232+" #define DEV_NAME "cs4232+" static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; #endif static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long cport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* PnP setup */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,11,12,15 */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " IDENT " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " IDENT " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " IDENT " soundcard."); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard."); #endif module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " IDENT " driver."); module_param_hw_array(cport, long, ioport, NULL, 0444); MODULE_PARM_DESC(cport, "Control port # for " IDENT " driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " IDENT " driver."); module_param_hw_array(fm_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(fm_port, "FM port # for " IDENT " driver."); module_param_hw_array(sb_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(sb_port, "SB port # for " IDENT " driver (optional)."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for " IDENT " driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " IDENT " driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for " IDENT " driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for " IDENT " driver."); #ifdef CONFIG_PNP static int isa_registered; static int pnpc_registered; static int pnp_registered; #endif /* CONFIG_PNP */ struct snd_card_cs4236 { struct snd_wss *chip; #ifdef CONFIG_PNP struct pnp_dev *wss; struct pnp_dev *ctrl; struct pnp_dev *mpu; #endif }; #ifdef CONFIG_PNP /* * PNP BIOS */ static const struct pnp_device_id snd_cs423x_pnpbiosids[] = { { .id = "CSC0100" }, { .id = "CSC0000" }, /* Guillemot Turtlebeach something appears to be cs4232 compatible * (untested) */ { .id = "GIM0100" }, { .id = "" } }; MODULE_DEVICE_TABLE(pnp, snd_cs423x_pnpbiosids); #define CS423X_ISAPNP_DRIVER "cs4232_isapnp" static const struct pnp_card_device_id snd_cs423x_pnpids[] = { /* Philips PCA70PS */ { .id = "CSC0d32", .devs = { { "CSC0000" }, { "CSC0010" }, { "PNPb006" } } }, /* TerraTec Maestro 32/96 (CS4232) */ { .id = "CSC1a32", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* HP Omnibook 5500 onboard */ { .id = "CSC4232", .devs = { { "CSC0000" }, { "CSC0002" }, { "CSC0003" } } }, /* Unnamed CS4236 card (Made in Taiwan) */ { .id = "CSC4236", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Turtle Beach TBS-2000 (CS4232) */ { .id = "CSC7532", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSCb006" } } }, /* Turtle Beach Tropez Plus (CS4232) */ { .id = "CSC7632", .devs = { { "CSC0000" }, { "CSC0010" }, { "PNPb006" } } }, /* SIC CrystalWave 32 (CS4232) */ { .id = "CSCf032", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Netfinity 3000 on-board soundcard */ { .id = "CSCe825", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC010f" } } }, /* Intel Marlin Spike Motherboard - CS4235 */ { .id = "CSC0225", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Intel Marlin Spike Motherboard (#2) - CS4235 */ { .id = "CSC0225", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } }, /* Unknown Intel mainboard - CS4235 */ { .id = "CSC0225", .devs = { { "CSC0100" }, { "CSC0110" } } }, /* Genius Sound Maker 3DJ - CS4237B */ { .id = "CSC0437", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Digital PC 5000 Onboard - CS4236B */ { .id = "CSC0735", .devs = { { "CSC0000" }, { "CSC0010" } } }, /* some unknown CS4236B */ { .id = "CSC0b35", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Intel PR440FX Onboard sound */ { .id = "CSC0b36", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* CS4235 on mainboard without MPU */ { .id = "CSC1425", .devs = { { "CSC0100" }, { "CSC0110" } } }, /* Gateway E1000 Onboard CS4236B */ { .id = "CSC1335", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* HP 6330 Onboard sound */ { .id = "CSC1525", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } }, /* Crystal Computer TidalWave128 */ { .id = "CSC1e37", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* ACER AW37 - CS4235 */ { .id = "CSC4236", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* build-in soundcard in EliteGroup P5TX-LA motherboard - CS4237B */ { .id = "CSC4237", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Crystal 3D - CS4237B */ { .id = "CSC4336", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Typhoon Soundsystem PnP - CS4236B */ { .id = "CSC4536", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Crystal CX4235-XQ3 EP - CS4235 */ { .id = "CSC4625", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } }, /* Crystal Semiconductors CS4237B */ { .id = "CSC4637", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* NewClear 3D - CX4237B-XQ3 */ { .id = "CSC4837", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Dell Optiplex GX1 - CS4236B */ { .id = "CSC6835", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Dell P410 motherboard - CS4236B */ { .id = "CSC6835", .devs = { { "CSC0000" }, { "CSC0010" } } }, /* Dell Workstation 400 Onboard - CS4236B */ { .id = "CSC6836", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Turtle Beach Malibu - CS4237B */ { .id = "CSC7537", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* CS4235 - onboard */ { .id = "CSC8025", .devs = { { "CSC0100" }, { "CSC0110" }, { "CSC0103" } } }, /* IBM Aptiva 2137 E24 Onboard - CS4237B */ { .id = "CSC8037", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* IBM IntelliStation M Pro motherboard */ { .id = "CSCc835", .devs = { { "CSC0000" }, { "CSC0010" } } }, /* Guillemot MaxiSound 16 PnP - CS4236B */ { .id = "CSC9836", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Gallant SC-70P */ { .id = "CSC9837", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* Techmakers MF-4236PW */ { .id = "CSCa736", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* TerraTec AudioSystem EWS64XL - CS4236B */ { .id = "CSCa836", .devs = { { "CSCa800" }, { "CSCa810" }, { "CSCa803" } } }, /* TerraTec AudioSystem EWS64XL - CS4236B */ { .id = "CSCa836", .devs = { { "CSCa800" }, { "CSCa810" } } }, /* ACER AW37/Pro - CS4235 */ { .id = "CSCd925", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* ACER AW35/Pro - CS4237B */ { .id = "CSCd937", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* CS4235 without MPU401 */ { .id = "CSCe825", .devs = { { "CSC0100" }, { "CSC0110" } } }, /* Unknown SiS530 - CS4235 */ { .id = "CSC4825", .devs = { { "CSC0100" }, { "CSC0110" } } }, /* IBM IntelliStation M Pro 6898 11U - CS4236B */ { .id = "CSCe835", .devs = { { "CSC0000" }, { "CSC0010" } } }, /* IBM PC 300PL Onboard - CS4236B */ { .id = "CSCe836", .devs = { { "CSC0000" }, { "CSC0010" } } }, /* Some noname CS4236 based card */ { .id = "CSCe936", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* CS4236B */ { .id = "CSCf235", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* CS4236B */ { .id = "CSCf238", .devs = { { "CSC0000" }, { "CSC0010" }, { "CSC0003" } } }, /* --- */ { .id = "" } /* end */ }; MODULE_DEVICE_TABLE(pnp_card, snd_cs423x_pnpids); /* WSS initialization */ static int snd_cs423x_pnp_init_wss(int dev, struct pnp_dev *pdev) { if (pnp_activate_dev(pdev) < 0) { printk(KERN_ERR IDENT " WSS PnP configure failed for WSS (out of resources?)\n"); return -EBUSY; } port[dev] = pnp_port_start(pdev, 0); if (fm_port[dev] > 0) fm_port[dev] = pnp_port_start(pdev, 1); sb_port[dev] = pnp_port_start(pdev, 2); irq[dev] = pnp_irq(pdev, 0); dma1[dev] = pnp_dma(pdev, 0); dma2[dev] = pnp_dma(pdev, 1) == 4 ? -1 : (int)pnp_dma(pdev, 1); snd_printdd("isapnp WSS: wss port=0x%lx, fm port=0x%lx, sb port=0x%lx\n", port[dev], fm_port[dev], sb_port[dev]); snd_printdd("isapnp WSS: irq=%i, dma1=%i, dma2=%i\n", irq[dev], dma1[dev], dma2[dev]); return 0; } /* CTRL initialization */ static int snd_cs423x_pnp_init_ctrl(int dev, struct pnp_dev *pdev) { if (pnp_activate_dev(pdev) < 0) { printk(KERN_ERR IDENT " CTRL PnP configure failed for WSS (out of resources?)\n"); return -EBUSY; } cport[dev] = pnp_port_start(pdev, 0); snd_printdd("isapnp CTRL: control port=0x%lx\n", cport[dev]); return 0; } /* MPU initialization */ static int snd_cs423x_pnp_init_mpu(int dev, struct pnp_dev *pdev) { if (pnp_activate_dev(pdev) < 0) { printk(KERN_ERR IDENT " MPU401 PnP configure failed for WSS (out of resources?)\n"); mpu_port[dev] = SNDRV_AUTO_PORT; mpu_irq[dev] = SNDRV_AUTO_IRQ; } else { mpu_port[dev] = pnp_port_start(pdev, 0); if (mpu_irq[dev] >= 0 && pnp_irq_valid(pdev, 0) && pnp_irq(pdev, 0) != (resource_size_t)-1) { mpu_irq[dev] = pnp_irq(pdev, 0); } else { mpu_irq[dev] = -1; /* disable interrupt */ } } snd_printdd("isapnp MPU: port=0x%lx, irq=%i\n", mpu_port[dev], mpu_irq[dev]); return 0; } static int snd_card_cs423x_pnp(int dev, struct snd_card_cs4236 *acard, struct pnp_dev *pdev, struct pnp_dev *cdev) { acard->wss = pdev; if (snd_cs423x_pnp_init_wss(dev, acard->wss) < 0) return -EBUSY; if (cdev) cport[dev] = pnp_port_start(cdev, 0); else cport[dev] = -1; return 0; } static int snd_card_cs423x_pnpc(int dev, struct snd_card_cs4236 *acard, struct pnp_card_link *card, const struct pnp_card_device_id *id) { acard->wss = pnp_request_card_device(card, id->devs[0].id, NULL); if (acard->wss == NULL) return -EBUSY; acard->ctrl = pnp_request_card_device(card, id->devs[1].id, NULL); if (acard->ctrl == NULL) return -EBUSY; if (id->devs[2].id[0]) { acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL); if (acard->mpu == NULL) return -EBUSY; } /* WSS initialization */ if (snd_cs423x_pnp_init_wss(dev, acard->wss) < 0) return -EBUSY; /* CTRL initialization */ if (acard->ctrl && cport[dev] > 0) { if (snd_cs423x_pnp_init_ctrl(dev, acard->ctrl) < 0) return -EBUSY; } /* MPU initialization */ if (acard->mpu && mpu_port[dev] > 0) { if (snd_cs423x_pnp_init_mpu(dev, acard->mpu) < 0) return -EBUSY; } return 0; } #endif /* CONFIG_PNP */ #ifdef CONFIG_PNP #define is_isapnp_selected(dev) isapnp[dev] #else #define is_isapnp_selected(dev) 0 #endif static int snd_cs423x_card_new(struct device *pdev, int dev, struct snd_card **cardp) { struct snd_card *card; int err; err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_card_cs4236), &card); if (err < 0) return err; *cardp = card; return 0; } static int snd_cs423x_probe(struct snd_card *card, int dev) { struct snd_card_cs4236 *acard; struct snd_wss *chip; struct snd_opl3 *opl3; int err; acard = card->private_data; if (sb_port[dev] > 0 && sb_port[dev] != SNDRV_AUTO_PORT) { if (!devm_request_region(card->dev, sb_port[dev], 16, IDENT " SB")) { printk(KERN_ERR IDENT ": unable to register SB port at 0x%lx\n", sb_port[dev]); return -EBUSY; } } err = snd_cs4236_create(card, port[dev], cport[dev], irq[dev], dma1[dev], dma2[dev], WSS_HW_DETECT3, 0, &chip); if (err < 0) return err; acard->chip = chip; if (chip->hardware & WSS_HW_CS4236B_MASK) { err = snd_cs4236_pcm(chip, 0); if (err < 0) return err; err = snd_cs4236_mixer(chip); if (err < 0) return err; } else { err = snd_wss_pcm(chip, 0); if (err < 0) return err; err = snd_wss_mixer(chip); if (err < 0) return err; } strscpy(card->driver, chip->pcm->name, sizeof(card->driver)); strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname)); if (dma2[dev] < 0) scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port, irq[dev], dma1[dev]); else scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %i, dma %i&%d", chip->pcm->name, chip->port, irq[dev], dma1[dev], dma2[dev]); err = snd_wss_timer(chip, 0); if (err < 0) return err; if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) { if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_OPL3_CS, 0, &opl3) < 0) { printk(KERN_WARNING IDENT ": OPL3 not detected\n"); } else { err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } } if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) { if (mpu_irq[dev] == SNDRV_AUTO_IRQ) mpu_irq[dev] = -1; if (snd_mpu401_uart_new(card, 0, MPU401_HW_CS4232, mpu_port[dev], 0, mpu_irq[dev], NULL) < 0) printk(KERN_WARNING IDENT ": MPU401 not detected\n"); } return snd_card_register(card); } static int snd_cs423x_isa_match(struct device *pdev, unsigned int dev) { if (!enable[dev] || is_isapnp_selected(dev)) return 0; if (port[dev] == SNDRV_AUTO_PORT) { dev_err(pdev, "please specify port\n"); return 0; } if (cport[dev] == SNDRV_AUTO_PORT) { dev_err(pdev, "please specify cport\n"); return 0; } if (irq[dev] == SNDRV_AUTO_IRQ) { dev_err(pdev, "please specify irq\n"); return 0; } if (dma1[dev] == SNDRV_AUTO_DMA) { dev_err(pdev, "please specify dma1\n"); return 0; } return 1; } static int snd_cs423x_isa_probe(struct device *pdev, unsigned int dev) { struct snd_card *card; int err; err = snd_cs423x_card_new(pdev, dev, &card); if (err < 0) return err; err = snd_cs423x_probe(card, dev); if (err < 0) return err; dev_set_drvdata(pdev, card); return 0; } #ifdef CONFIG_PM static int snd_cs423x_suspend(struct snd_card *card) { struct snd_card_cs4236 *acard = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); acard->chip->suspend(acard->chip); return 0; } static int snd_cs423x_resume(struct snd_card *card) { struct snd_card_cs4236 *acard = card->private_data; acard->chip->resume(acard->chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } static int snd_cs423x_isa_suspend(struct device *dev, unsigned int n, pm_message_t state) { return snd_cs423x_suspend(dev_get_drvdata(dev)); } static int snd_cs423x_isa_resume(struct device *dev, unsigned int n) { return snd_cs423x_resume(dev_get_drvdata(dev)); } #endif static struct isa_driver cs423x_isa_driver = { .match = snd_cs423x_isa_match, .probe = snd_cs423x_isa_probe, #ifdef CONFIG_PM .suspend = snd_cs423x_isa_suspend, .resume = snd_cs423x_isa_resume, #endif .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_cs423x_pnpbios_detect(struct pnp_dev *pdev, const struct pnp_device_id *id) { static int dev; int err; struct snd_card *card; struct pnp_dev *cdev, *iter; char cid[PNP_ID_LEN]; if (pnp_device_is_isapnp(pdev)) return -ENOENT; /* we have another procedure - card */ for (; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; /* prepare second id */ strcpy(cid, pdev->id[0].id); cid[5] = '1'; cdev = NULL; list_for_each_entry(iter, &(pdev->protocol->devices), protocol_list) { if (!strcmp(iter->id[0].id, cid)) { cdev = iter; break; } } err = snd_cs423x_card_new(&pdev->dev, dev, &card); if (err < 0) return err; err = snd_card_cs423x_pnp(dev, card->private_data, pdev, cdev); if (err < 0) { printk(KERN_ERR "PnP BIOS detection failed for " IDENT "\n"); return err; } err = snd_cs423x_probe(card, dev); if (err < 0) return err; pnp_set_drvdata(pdev, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_cs423x_pnp_suspend(struct pnp_dev *pdev, pm_message_t state) { return snd_cs423x_suspend(pnp_get_drvdata(pdev)); } static int snd_cs423x_pnp_resume(struct pnp_dev *pdev) { return snd_cs423x_resume(pnp_get_drvdata(pdev)); } #endif static struct pnp_driver cs423x_pnp_driver = { .name = "cs423x-pnpbios", .id_table = snd_cs423x_pnpbiosids, .probe = snd_cs423x_pnpbios_detect, #ifdef CONFIG_PM .suspend = snd_cs423x_pnp_suspend, .resume = snd_cs423x_pnp_resume, #endif }; static int snd_cs423x_pnpc_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int dev; struct snd_card *card; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; res = snd_cs423x_card_new(&pcard->card->dev, dev, &card); if (res < 0) return res; res = snd_card_cs423x_pnpc(dev, card->private_data, pcard, pid); if (res < 0) { printk(KERN_ERR "isapnp detection failed and probing for " IDENT " is not supported\n"); return res; } res = snd_cs423x_probe(card, dev); if (res < 0) return res; pnp_set_card_drvdata(pcard, card); dev++; return 0; } #ifdef CONFIG_PM static int snd_cs423x_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state) { return snd_cs423x_suspend(pnp_get_card_drvdata(pcard)); } static int snd_cs423x_pnpc_resume(struct pnp_card_link *pcard) { return snd_cs423x_resume(pnp_get_card_drvdata(pcard)); } #endif static struct pnp_card_driver cs423x_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = CS423X_ISAPNP_DRIVER, .id_table = snd_cs423x_pnpids, .probe = snd_cs423x_pnpc_detect, #ifdef CONFIG_PM .suspend = snd_cs423x_pnpc_suspend, .resume = snd_cs423x_pnpc_resume, #endif }; #endif /* CONFIG_PNP */ static int __init alsa_card_cs423x_init(void) { int err; err = isa_register_driver(&cs423x_isa_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_driver(&cs423x_pnp_driver); if (!err) pnp_registered = 1; err = pnp_register_card_driver(&cs423x_pnpc_driver); if (!err) pnpc_registered = 1; if (pnp_registered) err = 0; if (isa_registered) err = 0; #endif return err; } static void __exit alsa_card_cs423x_exit(void) { #ifdef CONFIG_PNP if (pnpc_registered) pnp_unregister_card_driver(&cs423x_pnpc_driver); if (pnp_registered) pnp_unregister_driver(&cs423x_pnp_driver); if (isa_registered) #endif isa_unregister_driver(&cs423x_isa_driver); } module_init(alsa_card_cs423x_init) module_exit(alsa_card_cs423x_exit)
linux-master
sound/isa/cs423x/cs4236.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Generic driver for CS4231 chips * Copyright (c) by Jaroslav Kysela <[email protected]> * Originally the CS4232/CS4232A driver, modified for use on CS4231 by * Tugrul Galatali <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/time.h> #include <linux/wait.h> #include <linux/module.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/initval.h> #define CRD_NAME "Generic CS4231" #define DEV_NAME "cs4231" MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,11,12,15 */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for " CRD_NAME " driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for " CRD_NAME " driver."); static int snd_cs4231_match(struct device *dev, unsigned int n) { if (!enable[n]) return 0; if (port[n] == SNDRV_AUTO_PORT) { dev_err(dev, "please specify port\n"); return 0; } if (irq[n] == SNDRV_AUTO_IRQ) { dev_err(dev, "please specify irq\n"); return 0; } if (dma1[n] == SNDRV_AUTO_DMA) { dev_err(dev, "please specify dma1\n"); return 0; } return 1; } static int snd_cs4231_probe(struct device *dev, unsigned int n) { struct snd_card *card; struct snd_wss *chip; int error; error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, 0, &card); if (error < 0) return error; error = snd_wss_create(card, port[n], -1, irq[n], dma1[n], dma2[n], WSS_HW_DETECT, 0, &chip); if (error < 0) return error; card->private_data = chip; error = snd_wss_pcm(chip, 0); if (error < 0) return error; strscpy(card->driver, "CS4231", sizeof(card->driver)); strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname)); if (dma2[n] < 0) scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %d, dma %d", chip->pcm->name, chip->port, irq[n], dma1[n]); else scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %d, dma %d&%d", chip->pcm->name, chip->port, irq[n], dma1[n], dma2[n]); error = snd_wss_mixer(chip); if (error < 0) return error; error = snd_wss_timer(chip, 0); if (error < 0) return error; if (mpu_port[n] > 0 && mpu_port[n] != SNDRV_AUTO_PORT) { if (mpu_irq[n] == SNDRV_AUTO_IRQ) mpu_irq[n] = -1; if (snd_mpu401_uart_new(card, 0, MPU401_HW_CS4232, mpu_port[n], 0, mpu_irq[n], NULL) < 0) dev_warn(dev, "MPU401 not detected\n"); } error = snd_card_register(card); if (error < 0) return error; dev_set_drvdata(dev, card); return 0; } #ifdef CONFIG_PM static int snd_cs4231_suspend(struct device *dev, unsigned int n, pm_message_t state) { struct snd_card *card = dev_get_drvdata(dev); struct snd_wss *chip = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); chip->suspend(chip); return 0; } static int snd_cs4231_resume(struct device *dev, unsigned int n) { struct snd_card *card = dev_get_drvdata(dev); struct snd_wss *chip = card->private_data; chip->resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct isa_driver snd_cs4231_driver = { .match = snd_cs4231_match, .probe = snd_cs4231_probe, #ifdef CONFIG_PM .suspend = snd_cs4231_suspend, .resume = snd_cs4231_resume, #endif .driver = { .name = DEV_NAME } }; module_isa_driver(snd_cs4231_driver, SNDRV_CARDS);
linux-master
sound/isa/cs423x/cs4231.c
// SPDX-License-Identifier: GPL-2.0-or-later /* ad1816a.c - lowlevel code for Analog Devices AD1816A chip. Copyright (C) 1999-2000 by Massimo Piccioni <[email protected]> */ #include <linux/delay.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/slab.h> #include <linux/ioport.h> #include <linux/io.h> #include <sound/core.h> #include <sound/tlv.h> #include <sound/ad1816a.h> #include <asm/dma.h> static inline int snd_ad1816a_busy_wait(struct snd_ad1816a *chip) { int timeout; for (timeout = 1000; timeout-- > 0; udelay(10)) if (inb(AD1816A_REG(AD1816A_CHIP_STATUS)) & AD1816A_READY) return 0; snd_printk(KERN_WARNING "chip busy.\n"); return -EBUSY; } static inline unsigned char snd_ad1816a_in(struct snd_ad1816a *chip, unsigned char reg) { snd_ad1816a_busy_wait(chip); return inb(AD1816A_REG(reg)); } static inline void snd_ad1816a_out(struct snd_ad1816a *chip, unsigned char reg, unsigned char value) { snd_ad1816a_busy_wait(chip); outb(value, AD1816A_REG(reg)); } static inline void snd_ad1816a_out_mask(struct snd_ad1816a *chip, unsigned char reg, unsigned char mask, unsigned char value) { snd_ad1816a_out(chip, reg, (value & mask) | (snd_ad1816a_in(chip, reg) & ~mask)); } static unsigned short snd_ad1816a_read(struct snd_ad1816a *chip, unsigned char reg) { snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f); return snd_ad1816a_in(chip, AD1816A_INDIR_DATA_LOW) | (snd_ad1816a_in(chip, AD1816A_INDIR_DATA_HIGH) << 8); } static void snd_ad1816a_write(struct snd_ad1816a *chip, unsigned char reg, unsigned short value) { snd_ad1816a_out(chip, AD1816A_INDIR_ADDR, reg & 0x3f); snd_ad1816a_out(chip, AD1816A_INDIR_DATA_LOW, value & 0xff); snd_ad1816a_out(chip, AD1816A_INDIR_DATA_HIGH, (value >> 8) & 0xff); } static void snd_ad1816a_write_mask(struct snd_ad1816a *chip, unsigned char reg, unsigned short mask, unsigned short value) { snd_ad1816a_write(chip, reg, (value & mask) | (snd_ad1816a_read(chip, reg) & ~mask)); } static unsigned char snd_ad1816a_get_format(struct snd_ad1816a *chip, snd_pcm_format_t format, int channels) { unsigned char retval = AD1816A_FMT_LINEAR_8; switch (format) { case SNDRV_PCM_FORMAT_MU_LAW: retval = AD1816A_FMT_ULAW_8; break; case SNDRV_PCM_FORMAT_A_LAW: retval = AD1816A_FMT_ALAW_8; break; case SNDRV_PCM_FORMAT_S16_LE: retval = AD1816A_FMT_LINEAR_16_LIT; break; case SNDRV_PCM_FORMAT_S16_BE: retval = AD1816A_FMT_LINEAR_16_BIG; } return (channels > 1) ? (retval | AD1816A_FMT_STEREO) : retval; } static int snd_ad1816a_open(struct snd_ad1816a *chip, unsigned int mode) { unsigned long flags; spin_lock_irqsave(&chip->lock, flags); if (chip->mode & mode) { spin_unlock_irqrestore(&chip->lock, flags); return -EAGAIN; } switch ((mode &= AD1816A_MODE_OPEN)) { case AD1816A_MODE_PLAYBACK: snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS, AD1816A_PLAYBACK_IRQ_PENDING, 0x00); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_PLAYBACK_IRQ_ENABLE, 0xffff); break; case AD1816A_MODE_CAPTURE: snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS, AD1816A_CAPTURE_IRQ_PENDING, 0x00); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_CAPTURE_IRQ_ENABLE, 0xffff); break; case AD1816A_MODE_TIMER: snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS, AD1816A_TIMER_IRQ_PENDING, 0x00); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_TIMER_IRQ_ENABLE, 0xffff); } chip->mode |= mode; spin_unlock_irqrestore(&chip->lock, flags); return 0; } static void snd_ad1816a_close(struct snd_ad1816a *chip, unsigned int mode) { unsigned long flags; spin_lock_irqsave(&chip->lock, flags); switch ((mode &= AD1816A_MODE_OPEN)) { case AD1816A_MODE_PLAYBACK: snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS, AD1816A_PLAYBACK_IRQ_PENDING, 0x00); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_PLAYBACK_IRQ_ENABLE, 0x0000); break; case AD1816A_MODE_CAPTURE: snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS, AD1816A_CAPTURE_IRQ_PENDING, 0x00); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_CAPTURE_IRQ_ENABLE, 0x0000); break; case AD1816A_MODE_TIMER: snd_ad1816a_out_mask(chip, AD1816A_INTERRUPT_STATUS, AD1816A_TIMER_IRQ_PENDING, 0x00); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_TIMER_IRQ_ENABLE, 0x0000); } chip->mode &= ~mode; if (!(chip->mode & AD1816A_MODE_OPEN)) chip->mode = 0; spin_unlock_irqrestore(&chip->lock, flags); } static int snd_ad1816a_trigger(struct snd_ad1816a *chip, unsigned char what, int channel, int cmd, int iscapture) { int error = 0; switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_STOP: spin_lock(&chip->lock); cmd = (cmd == SNDRV_PCM_TRIGGER_START) ? 0xff: 0x00; /* if (what & AD1816A_PLAYBACK_ENABLE) */ /* That is not valid, because playback and capture enable * are the same bit pattern, just to different addresses */ if (! iscapture) snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG, AD1816A_PLAYBACK_ENABLE, cmd); else snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG, AD1816A_CAPTURE_ENABLE, cmd); spin_unlock(&chip->lock); break; default: snd_printk(KERN_WARNING "invalid trigger mode 0x%x.\n", what); error = -EINVAL; } return error; } static int snd_ad1816a_playback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); return snd_ad1816a_trigger(chip, AD1816A_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd, 0); } static int snd_ad1816a_capture_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); return snd_ad1816a_trigger(chip, AD1816A_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd, 1); } static int snd_ad1816a_playback_prepare(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); unsigned long flags; struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size, rate; spin_lock_irqsave(&chip->lock, flags); chip->p_dma_size = size = snd_pcm_lib_buffer_bytes(substream); snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG, AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00); snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); rate = runtime->rate; if (chip->clock_freq) rate = (rate * 33000) / chip->clock_freq; snd_ad1816a_write(chip, AD1816A_PLAYBACK_SAMPLE_RATE, rate); snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG, AD1816A_FMT_ALL | AD1816A_FMT_STEREO, snd_ad1816a_get_format(chip, runtime->format, runtime->channels)); snd_ad1816a_write(chip, AD1816A_PLAYBACK_BASE_COUNT, snd_pcm_lib_period_bytes(substream) / 4 - 1); spin_unlock_irqrestore(&chip->lock, flags); return 0; } static int snd_ad1816a_capture_prepare(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); unsigned long flags; struct snd_pcm_runtime *runtime = substream->runtime; unsigned int size, rate; spin_lock_irqsave(&chip->lock, flags); chip->c_dma_size = size = snd_pcm_lib_buffer_bytes(substream); snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG, AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00); snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); rate = runtime->rate; if (chip->clock_freq) rate = (rate * 33000) / chip->clock_freq; snd_ad1816a_write(chip, AD1816A_CAPTURE_SAMPLE_RATE, rate); snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG, AD1816A_FMT_ALL | AD1816A_FMT_STEREO, snd_ad1816a_get_format(chip, runtime->format, runtime->channels)); snd_ad1816a_write(chip, AD1816A_CAPTURE_BASE_COUNT, snd_pcm_lib_period_bytes(substream) / 4 - 1); spin_unlock_irqrestore(&chip->lock, flags); return 0; } static snd_pcm_uframes_t snd_ad1816a_playback_pointer(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); size_t ptr; if (!(chip->mode & AD1816A_MODE_PLAYBACK)) return 0; ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size); return bytes_to_frames(substream->runtime, ptr); } static snd_pcm_uframes_t snd_ad1816a_capture_pointer(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); size_t ptr; if (!(chip->mode & AD1816A_MODE_CAPTURE)) return 0; ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size); return bytes_to_frames(substream->runtime, ptr); } static irqreturn_t snd_ad1816a_interrupt(int irq, void *dev_id) { struct snd_ad1816a *chip = dev_id; unsigned char status; spin_lock(&chip->lock); status = snd_ad1816a_in(chip, AD1816A_INTERRUPT_STATUS); spin_unlock(&chip->lock); if ((status & AD1816A_PLAYBACK_IRQ_PENDING) && chip->playback_substream) snd_pcm_period_elapsed(chip->playback_substream); if ((status & AD1816A_CAPTURE_IRQ_PENDING) && chip->capture_substream) snd_pcm_period_elapsed(chip->capture_substream); if ((status & AD1816A_TIMER_IRQ_PENDING) && chip->timer) snd_timer_interrupt(chip->timer, chip->timer->sticks); spin_lock(&chip->lock); snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00); spin_unlock(&chip->lock); return IRQ_HANDLED; } static const struct snd_pcm_hardware snd_ad1816a_playback = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE), .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 55200, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_ad1816a_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE), .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 4000, .rate_max = 55200, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static int snd_ad1816a_timer_close(struct snd_timer *timer) { struct snd_ad1816a *chip = snd_timer_chip(timer); snd_ad1816a_close(chip, AD1816A_MODE_TIMER); return 0; } static int snd_ad1816a_timer_open(struct snd_timer *timer) { struct snd_ad1816a *chip = snd_timer_chip(timer); snd_ad1816a_open(chip, AD1816A_MODE_TIMER); return 0; } static unsigned long snd_ad1816a_timer_resolution(struct snd_timer *timer) { if (snd_BUG_ON(!timer)) return 0; return 10000; } static int snd_ad1816a_timer_start(struct snd_timer *timer) { unsigned short bits; unsigned long flags; struct snd_ad1816a *chip = snd_timer_chip(timer); spin_lock_irqsave(&chip->lock, flags); bits = snd_ad1816a_read(chip, AD1816A_INTERRUPT_ENABLE); if (!(bits & AD1816A_TIMER_ENABLE)) { snd_ad1816a_write(chip, AD1816A_TIMER_BASE_COUNT, timer->sticks & 0xffff); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_TIMER_ENABLE, 0xffff); } spin_unlock_irqrestore(&chip->lock, flags); return 0; } static int snd_ad1816a_timer_stop(struct snd_timer *timer) { unsigned long flags; struct snd_ad1816a *chip = snd_timer_chip(timer); spin_lock_irqsave(&chip->lock, flags); snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE, AD1816A_TIMER_ENABLE, 0x0000); spin_unlock_irqrestore(&chip->lock, flags); return 0; } static const struct snd_timer_hardware snd_ad1816a_timer_table = { .flags = SNDRV_TIMER_HW_AUTO, .resolution = 10000, .ticks = 65535, .open = snd_ad1816a_timer_open, .close = snd_ad1816a_timer_close, .c_resolution = snd_ad1816a_timer_resolution, .start = snd_ad1816a_timer_start, .stop = snd_ad1816a_timer_stop, }; static int snd_ad1816a_playback_open(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int error; error = snd_ad1816a_open(chip, AD1816A_MODE_PLAYBACK); if (error < 0) return error; runtime->hw = snd_ad1816a_playback; snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max); snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max); chip->playback_substream = substream; return 0; } static int snd_ad1816a_capture_open(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int error; error = snd_ad1816a_open(chip, AD1816A_MODE_CAPTURE); if (error < 0) return error; runtime->hw = snd_ad1816a_capture; snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max); snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max); chip->capture_substream = substream; return 0; } static int snd_ad1816a_playback_close(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); chip->playback_substream = NULL; snd_ad1816a_close(chip, AD1816A_MODE_PLAYBACK); return 0; } static int snd_ad1816a_capture_close(struct snd_pcm_substream *substream) { struct snd_ad1816a *chip = snd_pcm_substream_chip(substream); chip->capture_substream = NULL; snd_ad1816a_close(chip, AD1816A_MODE_CAPTURE); return 0; } static void snd_ad1816a_init(struct snd_ad1816a *chip) { unsigned long flags; spin_lock_irqsave(&chip->lock, flags); snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00); snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG, AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00); snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG, AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00); snd_ad1816a_write(chip, AD1816A_INTERRUPT_ENABLE, 0x0000); snd_ad1816a_write_mask(chip, AD1816A_CHIP_CONFIG, AD1816A_CAPTURE_NOT_EQUAL | AD1816A_WSS_ENABLE, 0xffff); snd_ad1816a_write(chip, AD1816A_DSP_CONFIG, 0x0000); snd_ad1816a_write(chip, AD1816A_POWERDOWN_CTRL, 0x0000); spin_unlock_irqrestore(&chip->lock, flags); } #ifdef CONFIG_PM void snd_ad1816a_suspend(struct snd_ad1816a *chip) { int reg; unsigned long flags; spin_lock_irqsave(&chip->lock, flags); for (reg = 0; reg < 48; reg++) chip->image[reg] = snd_ad1816a_read(chip, reg); spin_unlock_irqrestore(&chip->lock, flags); } void snd_ad1816a_resume(struct snd_ad1816a *chip) { int reg; unsigned long flags; snd_ad1816a_init(chip); spin_lock_irqsave(&chip->lock, flags); for (reg = 0; reg < 48; reg++) snd_ad1816a_write(chip, reg, chip->image[reg]); spin_unlock_irqrestore(&chip->lock, flags); } #endif static int snd_ad1816a_probe(struct snd_ad1816a *chip) { unsigned long flags; spin_lock_irqsave(&chip->lock, flags); switch (chip->version = snd_ad1816a_read(chip, AD1816A_VERSION_ID)) { case 0: chip->hardware = AD1816A_HW_AD1815; break; case 1: chip->hardware = AD1816A_HW_AD18MAX10; break; case 3: chip->hardware = AD1816A_HW_AD1816A; break; default: chip->hardware = AD1816A_HW_AUTO; } spin_unlock_irqrestore(&chip->lock, flags); return 0; } static const char *snd_ad1816a_chip_id(struct snd_ad1816a *chip) { switch (chip->hardware) { case AD1816A_HW_AD1816A: return "AD1816A"; case AD1816A_HW_AD1815: return "AD1815"; case AD1816A_HW_AD18MAX10: return "AD18max10"; default: snd_printk(KERN_WARNING "Unknown chip version %d:%d.\n", chip->version, chip->hardware); return "AD1816A - unknown"; } } int snd_ad1816a_create(struct snd_card *card, unsigned long port, int irq, int dma1, int dma2, struct snd_ad1816a *chip) { int error; chip->irq = -1; chip->dma1 = -1; chip->dma2 = -1; chip->res_port = devm_request_region(card->dev, port, 16, "AD1816A"); if (!chip->res_port) { snd_printk(KERN_ERR "ad1816a: can't grab port 0x%lx\n", port); return -EBUSY; } if (devm_request_irq(card->dev, irq, snd_ad1816a_interrupt, 0, "AD1816A", (void *) chip)) { snd_printk(KERN_ERR "ad1816a: can't grab IRQ %d\n", irq); return -EBUSY; } chip->irq = irq; card->sync_irq = chip->irq; if (snd_devm_request_dma(card->dev, dma1, "AD1816A - 1")) { snd_printk(KERN_ERR "ad1816a: can't grab DMA1 %d\n", dma1); return -EBUSY; } chip->dma1 = dma1; if (snd_devm_request_dma(card->dev, dma2, "AD1816A - 2")) { snd_printk(KERN_ERR "ad1816a: can't grab DMA2 %d\n", dma2); return -EBUSY; } chip->dma2 = dma2; chip->card = card; chip->port = port; spin_lock_init(&chip->lock); error = snd_ad1816a_probe(chip); if (error) return error; snd_ad1816a_init(chip); return 0; } static const struct snd_pcm_ops snd_ad1816a_playback_ops = { .open = snd_ad1816a_playback_open, .close = snd_ad1816a_playback_close, .prepare = snd_ad1816a_playback_prepare, .trigger = snd_ad1816a_playback_trigger, .pointer = snd_ad1816a_playback_pointer, }; static const struct snd_pcm_ops snd_ad1816a_capture_ops = { .open = snd_ad1816a_capture_open, .close = snd_ad1816a_capture_close, .prepare = snd_ad1816a_capture_prepare, .trigger = snd_ad1816a_capture_trigger, .pointer = snd_ad1816a_capture_pointer, }; int snd_ad1816a_pcm(struct snd_ad1816a *chip, int device) { int error; struct snd_pcm *pcm; error = snd_pcm_new(chip->card, "AD1816A", device, 1, 1, &pcm); if (error) return error; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1816a_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1816a_capture_ops); pcm->private_data = chip; pcm->info_flags = (chip->dma1 == chip->dma2 ) ? SNDRV_PCM_INFO_JOINT_DUPLEX : 0; strcpy(pcm->name, snd_ad1816a_chip_id(chip)); snd_ad1816a_init(chip); snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, chip->card->dev, 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024); chip->pcm = pcm; return 0; } int snd_ad1816a_timer(struct snd_ad1816a *chip, int device) { struct snd_timer *timer; struct snd_timer_id tid; int error; tid.dev_class = SNDRV_TIMER_CLASS_CARD; tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; tid.card = chip->card->number; tid.device = device; tid.subdevice = 0; error = snd_timer_new(chip->card, "AD1816A", &tid, &timer); if (error < 0) return error; strcpy(timer->name, snd_ad1816a_chip_id(chip)); timer->private_data = chip; chip->timer = timer; timer->hw = snd_ad1816a_timer_table; return 0; } /* * */ static int snd_ad1816a_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[8] = { "Line", "Mix", "CD", "Synth", "Video", "Mic", "Phone", }; return snd_ctl_enum_info(uinfo, 2, 7, texts); } static int snd_ad1816a_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned short val; spin_lock_irqsave(&chip->lock, flags); val = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL); spin_unlock_irqrestore(&chip->lock, flags); ucontrol->value.enumerated.item[0] = (val >> 12) & 7; ucontrol->value.enumerated.item[1] = (val >> 4) & 7; return 0; } static int snd_ad1816a_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned short val; int change; if (ucontrol->value.enumerated.item[0] > 6 || ucontrol->value.enumerated.item[1] > 6) return -EINVAL; val = (ucontrol->value.enumerated.item[0] << 12) | (ucontrol->value.enumerated.item[1] << 4); spin_lock_irqsave(&chip->lock, flags); change = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL) != val; snd_ad1816a_write(chip, AD1816A_ADC_SOURCE_SEL, val); spin_unlock_irqrestore(&chip->lock, flags); return change; } #define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .name = xname, .info = snd_ad1816a_info_single, \ .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \ .tlv = { .p = (xtlv) } } #define AD1816A_SINGLE(xname, reg, shift, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \ .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \ .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) } static int snd_ad1816a_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 16) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_ad1816a_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; spin_lock_irqsave(&chip->lock, flags); ucontrol->value.integer.value[0] = (snd_ad1816a_read(chip, reg) >> shift) & mask; spin_unlock_irqrestore(&chip->lock, flags); if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } static int snd_ad1816a_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned short old_val, val; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; val <<= shift; spin_lock_irqsave(&chip->lock, flags); old_val = snd_ad1816a_read(chip, reg); val = (old_val & ~(mask << shift)) | val; change = val != old_val; snd_ad1816a_write(chip, reg, val); spin_unlock_irqrestore(&chip->lock, flags); return change; } #define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ .name = xname, .info = snd_ad1816a_info_double, \ .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \ .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \ .tlv = { .p = (xtlv) } } #define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \ .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \ .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) } static int snd_ad1816a_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 16) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } static int snd_ad1816a_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift_left = (kcontrol->private_value >> 8) & 0x0f; int shift_right = (kcontrol->private_value >> 12) & 0x0f; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; unsigned short val; spin_lock_irqsave(&chip->lock, flags); val = snd_ad1816a_read(chip, reg); ucontrol->value.integer.value[0] = (val >> shift_left) & mask; ucontrol->value.integer.value[1] = (val >> shift_right) & mask; spin_unlock_irqrestore(&chip->lock, flags); if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } static int snd_ad1816a_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift_left = (kcontrol->private_value >> 8) & 0x0f; int shift_right = (kcontrol->private_value >> 12) & 0x0f; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned short old_val, val1, val2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; spin_lock_irqsave(&chip->lock, flags); old_val = snd_ad1816a_read(chip, reg); val1 = (old_val & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; change = val1 != old_val; snd_ad1816a_write(chip, reg, val1); spin_unlock_irqrestore(&chip->lock, flags); return change; } static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0); static const struct snd_kcontrol_new snd_ad1816a_controls[] = { AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT, 8, 0, 31, 1, db_scale_5bit), AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT, 8, 0, 63, 1, db_scale_6bit), AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT, 8, 0, 31, 1, db_scale_5bit_12db_max), AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT, 8, 0, 31, 1, db_scale_5bit_12db_max), AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT, 8, 0, 31, 1, db_scale_5bit_12db_max), AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT, 8, 0, 63, 1, db_scale_6bit), AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT, 15, 1, 1), AD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT, 8, 31, 1, db_scale_5bit_12db_max), AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT, 14, 1, 0), AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT, 8, 0, 31, 1, db_scale_5bit_12db_max), AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT, 15, 1, 1), AD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT, 0, 15, 1, db_scale_4bit), AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT, 7, 1, 1), AD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT, 0, 31, 1, db_scale_5bit), { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Capture Source", .info = snd_ad1816a_info_mux, .get = snd_ad1816a_get_mux, .put = snd_ad1816a_put_mux, }, AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA, 15, 7, 1, 1), AD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA, 8, 0, 15, 0, db_scale_rec_gain), AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL, 15, 1, 1), AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL, 0, 15, 0), }; int snd_ad1816a_mixer(struct snd_ad1816a *chip) { struct snd_card *card; unsigned int idx; int err; if (snd_BUG_ON(!chip || !chip->card)) return -EINVAL; card = chip->card; strcpy(card->mixername, snd_ad1816a_chip_id(chip)); for (idx = 0; idx < ARRAY_SIZE(snd_ad1816a_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_ad1816a_controls[idx], chip)); if (err < 0) return err; } return 0; }
linux-master
sound/isa/ad1816a/ad1816a_lib.c
// SPDX-License-Identifier: GPL-2.0-or-later /* card-ad1816a.c - driver for ADI SoundPort AD1816A based soundcards. Copyright (C) 2000 by Massimo Piccioni <[email protected]> */ #include <linux/init.h> #include <linux/time.h> #include <linux/wait.h> #include <linux/pnp.h> #include <linux/module.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/ad1816a.h> #include <sound/mpu401.h> #include <sound/opl3.h> #define PFX "ad1816a: " MODULE_AUTHOR("Massimo Piccioni <[email protected]>"); MODULE_DESCRIPTION("AD1816A, AD1815"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* Pnp setup */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* Pnp setup */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */ static int clockfreq[SNDRV_CARDS]; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for ad1816a based soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for ad1816a based soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable ad1816a based soundcard."); module_param_array(clockfreq, int, NULL, 0444); MODULE_PARM_DESC(clockfreq, "Clock frequency for ad1816a driver (default = 0)."); static const struct pnp_card_device_id snd_ad1816a_pnpids[] = { /* Analog Devices AD1815 */ { .id = "ADS7150", .devs = { { .id = "ADS7150" }, { .id = "ADS7151" } } }, /* Analog Devices AD1816? */ { .id = "ADS7180", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* Analog Devices AD1816A - added by Kenneth Platz <[email protected]> */ { .id = "ADS7181", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* Analog Devices AD1816A - Aztech/Newcom SC-16 3D */ { .id = "AZT1022", .devs = { { .id = "AZT1018" }, { .id = "AZT2002" } } }, /* Highscreen Sound-Boostar 16 3D - added by Stefan Behnel */ { .id = "LWC1061", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* Highscreen Sound-Boostar 16 3D */ { .id = "MDK1605", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* Shark Predator ISA - added by Ken Arromdee */ { .id = "SMM7180", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* Analog Devices AD1816A - Terratec AudioSystem EWS64 S */ { .id = "TER1112", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* Analog Devices AD1816A - Terratec AudioSystem EWS64 S */ { .id = "TER1112", .devs = { { .id = "TER1100" }, { .id = "TER1101" } } }, /* Analog Devices AD1816A - Terratec Base 64 */ { .id = "TER1411", .devs = { { .id = "ADS7180" }, { .id = "ADS7181" } } }, /* end */ { .id = "" } }; MODULE_DEVICE_TABLE(pnp_card, snd_ad1816a_pnpids); #define DRIVER_NAME "snd-card-ad1816a" static int snd_card_ad1816a_pnp(int dev, struct pnp_card_link *card, const struct pnp_card_device_id *id) { struct pnp_dev *pdev; int err; pdev = pnp_request_card_device(card, id->devs[0].id, NULL); if (pdev == NULL) return -EBUSY; err = pnp_activate_dev(pdev); if (err < 0) { printk(KERN_ERR PFX "AUDIO PnP configure failure\n"); return -EBUSY; } port[dev] = pnp_port_start(pdev, 2); fm_port[dev] = pnp_port_start(pdev, 1); dma1[dev] = pnp_dma(pdev, 0); dma2[dev] = pnp_dma(pdev, 1); irq[dev] = pnp_irq(pdev, 0); pdev = pnp_request_card_device(card, id->devs[1].id, NULL); if (pdev == NULL) { mpu_port[dev] = -1; snd_printk(KERN_WARNING PFX "MPU401 device busy, skipping.\n"); return 0; } err = pnp_activate_dev(pdev); if (err < 0) { printk(KERN_ERR PFX "MPU401 PnP configure failure\n"); mpu_port[dev] = -1; } else { mpu_port[dev] = pnp_port_start(pdev, 0); mpu_irq[dev] = pnp_irq(pdev, 0); } return 0; } static int snd_card_ad1816a_probe(int dev, struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { int error; struct snd_card *card; struct snd_ad1816a *chip; struct snd_opl3 *opl3; error = snd_devm_card_new(&pcard->card->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_ad1816a), &card); if (error < 0) return error; chip = card->private_data; error = snd_card_ad1816a_pnp(dev, pcard, pid); if (error) return error; error = snd_ad1816a_create(card, port[dev], irq[dev], dma1[dev], dma2[dev], chip); if (error) return error; if (clockfreq[dev] >= 5000 && clockfreq[dev] <= 100000) chip->clock_freq = clockfreq[dev]; strcpy(card->driver, "AD1816A"); strcpy(card->shortname, "ADI SoundPort AD1816A"); sprintf(card->longname, "%s, SS at 0x%lx, irq %d, dma %d&%d", card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]); error = snd_ad1816a_pcm(chip, 0); if (error < 0) return error; error = snd_ad1816a_mixer(chip); if (error < 0) return error; error = snd_ad1816a_timer(chip, 0); if (error < 0) return error; if (mpu_port[dev] > 0) { if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_port[dev], 0, mpu_irq[dev], NULL) < 0) printk(KERN_ERR PFX "no MPU-401 device at 0x%lx.\n", mpu_port[dev]); } if (fm_port[dev] > 0) { if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2, OPL3_HW_AUTO, 0, &opl3) < 0) { printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx.\n", fm_port[dev], fm_port[dev] + 2); } else { error = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (error < 0) return error; } } error = snd_card_register(card); if (error < 0) return error; pnp_set_card_drvdata(pcard, card); return 0; } static unsigned int ad1816a_devices; static int snd_ad1816a_pnp_detect(struct pnp_card_link *card, const struct pnp_card_device_id *id) { static int dev; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (!enable[dev]) continue; res = snd_card_ad1816a_probe(dev, card, id); if (res < 0) return res; dev++; ad1816a_devices++; return 0; } return -ENODEV; } #ifdef CONFIG_PM static int snd_ad1816a_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { struct snd_card *card = pnp_get_card_drvdata(pcard); snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); snd_ad1816a_suspend(card->private_data); return 0; } static int snd_ad1816a_pnp_resume(struct pnp_card_link *pcard) { struct snd_card *card = pnp_get_card_drvdata(pcard); snd_ad1816a_resume(card->private_data); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct pnp_card_driver ad1816a_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "ad1816a", .id_table = snd_ad1816a_pnpids, .probe = snd_ad1816a_pnp_detect, #ifdef CONFIG_PM .suspend = snd_ad1816a_pnp_suspend, .resume = snd_ad1816a_pnp_resume, #endif }; static int __init alsa_card_ad1816a_init(void) { int err; err = pnp_register_card_driver(&ad1816a_pnpc_driver); if (err) return err; if (!ad1816a_devices) { pnp_unregister_card_driver(&ad1816a_pnpc_driver); #ifdef MODULE printk(KERN_ERR "no AD1816A based soundcards found.\n"); #endif /* MODULE */ return -ENODEV; } return 0; } static void __exit alsa_card_ad1816a_exit(void) { pnp_unregister_card_driver(&ad1816a_pnpc_driver); } module_init(alsa_card_ad1816a_init) module_exit(alsa_card_ad1816a_exit)
linux-master
sound/isa/ad1816a/ad1816a.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Generic driver for AD1848/AD1847/CS4248 chips (0.1 Alpha) * Copyright (c) by Tugrul Galatali <[email protected]>, * Jaroslav Kysela <[email protected]> * Based on card-4232.c by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/time.h> #include <linux/wait.h> #include <linux/module.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/initval.h> #define CRD_NAME "Generic AD1848/AD1847/CS4248" #define DEV_NAME "ad1848" MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Tugrul Galatali <[email protected]>, Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ static bool thinkpad[SNDRV_CARDS]; /* Thinkpad special case */ module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for " CRD_NAME " driver."); module_param_array(thinkpad, bool, NULL, 0444); MODULE_PARM_DESC(thinkpad, "Enable only for the onboard CS4248 of IBM Thinkpad 360/750/755 series."); static int snd_ad1848_match(struct device *dev, unsigned int n) { if (!enable[n]) return 0; if (port[n] == SNDRV_AUTO_PORT) { dev_err(dev, "please specify port\n"); return 0; } if (irq[n] == SNDRV_AUTO_IRQ) { dev_err(dev, "please specify irq\n"); return 0; } if (dma1[n] == SNDRV_AUTO_DMA) { dev_err(dev, "please specify dma1\n"); return 0; } return 1; } static int snd_ad1848_probe(struct device *dev, unsigned int n) { struct snd_card *card; struct snd_wss *chip; int error; error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, 0, &card); if (error < 0) return error; error = snd_wss_create(card, port[n], -1, irq[n], dma1[n], -1, thinkpad[n] ? WSS_HW_THINKPAD : WSS_HW_DETECT, 0, &chip); if (error < 0) return error; card->private_data = chip; error = snd_wss_pcm(chip, 0); if (error < 0) return error; error = snd_wss_mixer(chip); if (error < 0) return error; strscpy(card->driver, "AD1848", sizeof(card->driver)); strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname)); if (!thinkpad[n]) scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %d, dma %d", chip->pcm->name, chip->port, irq[n], dma1[n]); else scnprintf(card->longname, sizeof(card->longname), "%s at 0x%lx, irq %d, dma %d [Thinkpad]", chip->pcm->name, chip->port, irq[n], dma1[n]); error = snd_card_register(card); if (error < 0) return error; dev_set_drvdata(dev, card); return 0; } #ifdef CONFIG_PM static int snd_ad1848_suspend(struct device *dev, unsigned int n, pm_message_t state) { struct snd_card *card = dev_get_drvdata(dev); struct snd_wss *chip = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); chip->suspend(chip); return 0; } static int snd_ad1848_resume(struct device *dev, unsigned int n) { struct snd_card *card = dev_get_drvdata(dev); struct snd_wss *chip = card->private_data; chip->resume(chip); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } #endif static struct isa_driver snd_ad1848_driver = { .match = snd_ad1848_match, .probe = snd_ad1848_probe, #ifdef CONFIG_PM .suspend = snd_ad1848_suspend, .resume = snd_ad1848_resume, #endif .driver = { .name = DEV_NAME } }; module_isa_driver(snd_ad1848_driver, SNDRV_CARDS);
linux-master
sound/isa/ad1848/ad1848.c
// SPDX-License-Identifier: GPL-2.0-or-later /* card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards. Copyright (C) 1998-2000 by Massimo Piccioni <[email protected]> Part of this code was developed at the Italian Ministry of Air Defence, Sixth Division (oh, che pace ...), Rome. Thanks to Maria Grazia Pollarini, Salvatore Vassallo. */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/pnp.h> #include <linux/module.h> #include <linux/io.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/tlv.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/opl3.h> #ifndef OPTi93X #include <sound/opl4.h> #endif #define SNDRV_LEGACY_FIND_FREE_IOPORT #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> MODULE_AUTHOR("Massimo Piccioni <[email protected]>"); MODULE_LICENSE("GPL"); #ifdef OPTi93X MODULE_DESCRIPTION("OPTi93X"); #else /* OPTi93X */ #ifdef CS4231 MODULE_DESCRIPTION("OPTi92X - CS4231"); #else /* CS4231 */ MODULE_DESCRIPTION("OPTi92X - AD1848"); #endif /* CS4231 */ #endif /* OPTi93X */ static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ //static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ #ifdef CONFIG_PNP static bool isapnp = true; /* Enable ISA PnP detection */ #endif static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */ static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */ static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */ static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */ static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */ static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ #if defined(CS4231) || defined(OPTi93X) static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ #endif /* CS4231 || OPTi93X */ module_param(index, int, 0444); MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard."); module_param(id, charp, 0444); MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard."); //module_param(enable, bool, 0444); //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard."); #ifdef CONFIG_PNP module_param(isapnp, bool, 0444); MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard."); #endif module_param_hw(port, long, ioport, 0444); MODULE_PARM_DESC(port, "WSS port # for opti9xx driver."); module_param_hw(mpu_port, long, ioport, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver."); module_param_hw(fm_port, long, ioport, 0444); MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver."); module_param_hw(irq, int, irq, 0444); MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver."); module_param_hw(mpu_irq, int, irq, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver."); module_param_hw(dma1, int, dma, 0444); MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver."); #if defined(CS4231) || defined(OPTi93X) module_param_hw(dma2, int, dma, 0444); MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver."); #endif /* CS4231 || OPTi93X */ #define OPTi9XX_HW_82C928 1 #define OPTi9XX_HW_82C929 2 #define OPTi9XX_HW_82C924 3 #define OPTi9XX_HW_82C925 4 #define OPTi9XX_HW_82C930 5 #define OPTi9XX_HW_82C931 6 #define OPTi9XX_HW_82C933 7 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933 #define OPTi9XX_MC_REG(n) n #ifdef OPTi93X #define OPTi93X_STATUS 0x02 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r) #define OPTi93X_IRQ_PLAYBACK 0x04 #define OPTi93X_IRQ_CAPTURE 0x08 #endif /* OPTi93X */ struct snd_opti9xx { unsigned short hardware; unsigned char password; char name[7]; unsigned long mc_base; struct resource *res_mc_base; unsigned long mc_base_size; #ifdef OPTi93X unsigned long mc_indir_index; struct resource *res_mc_indir; #endif /* OPTi93X */ struct snd_wss *codec; unsigned long pwd_reg; spinlock_t lock; long wss_base; int irq; }; static int snd_opti9xx_pnp_is_probed; #ifdef CONFIG_PNP static const struct pnp_card_device_id snd_opti9xx_pnpids[] = { #ifndef OPTi93X /* OPTi 82C924 */ { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } }, .driver_data = 0x0924 }, /* OPTi 82C925 */ { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } }, .driver_data = 0x0925 }, #else /* OPTi 82C931/3 */ { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 }, #endif /* OPTi93X */ { .id = "" } }; MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids); #endif /* CONFIG_PNP */ #define DEV_NAME KBUILD_MODNAME static const char * const snd_opti9xx_names[] = { "unknown", "82C928", "82C929", "82C924", "82C925", "82C930", "82C931", "82C933" }; static int snd_opti9xx_init(struct snd_opti9xx *chip, unsigned short hardware) { static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2}; chip->hardware = hardware; strcpy(chip->name, snd_opti9xx_names[hardware]); spin_lock_init(&chip->lock); chip->irq = -1; #ifndef OPTi93X #ifdef CONFIG_PNP if (isapnp && chip->mc_base) /* PnP resource gives the least 10 bits */ chip->mc_base |= 0xc00; else #endif /* CONFIG_PNP */ { chip->mc_base = 0xf8c; chip->mc_base_size = opti9xx_mc_size[hardware]; } #else chip->mc_base_size = opti9xx_mc_size[hardware]; #endif switch (hardware) { #ifndef OPTi93X case OPTi9XX_HW_82C928: case OPTi9XX_HW_82C929: chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3; chip->pwd_reg = 3; break; case OPTi9XX_HW_82C924: case OPTi9XX_HW_82C925: chip->password = 0xe5; chip->pwd_reg = 3; break; #else /* OPTi93X */ case OPTi9XX_HW_82C930: case OPTi9XX_HW_82C931: case OPTi9XX_HW_82C933: chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d; if (!chip->mc_indir_index) chip->mc_indir_index = 0xe0e; chip->password = 0xe4; chip->pwd_reg = 0; break; #endif /* OPTi93X */ default: snd_printk(KERN_ERR "chip %d not supported\n", hardware); return -ENODEV; } return 0; } static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip, unsigned char reg) { unsigned long flags; unsigned char retval = 0xff; spin_lock_irqsave(&chip->lock, flags); outb(chip->password, chip->mc_base + chip->pwd_reg); switch (chip->hardware) { #ifndef OPTi93X case OPTi9XX_HW_82C924: case OPTi9XX_HW_82C925: if (reg > 7) { outb(reg, chip->mc_base + 8); outb(chip->password, chip->mc_base + chip->pwd_reg); retval = inb(chip->mc_base + 9); break; } fallthrough; case OPTi9XX_HW_82C928: case OPTi9XX_HW_82C929: retval = inb(chip->mc_base + reg); break; #else /* OPTi93X */ case OPTi9XX_HW_82C930: case OPTi9XX_HW_82C931: case OPTi9XX_HW_82C933: outb(reg, chip->mc_indir_index); outb(chip->password, chip->mc_base + chip->pwd_reg); retval = inb(chip->mc_indir_index + 1); break; #endif /* OPTi93X */ default: snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); } spin_unlock_irqrestore(&chip->lock, flags); return retval; } static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg, unsigned char value) { unsigned long flags; spin_lock_irqsave(&chip->lock, flags); outb(chip->password, chip->mc_base + chip->pwd_reg); switch (chip->hardware) { #ifndef OPTi93X case OPTi9XX_HW_82C924: case OPTi9XX_HW_82C925: if (reg > 7) { outb(reg, chip->mc_base + 8); outb(chip->password, chip->mc_base + chip->pwd_reg); outb(value, chip->mc_base + 9); break; } fallthrough; case OPTi9XX_HW_82C928: case OPTi9XX_HW_82C929: outb(value, chip->mc_base + reg); break; #else /* OPTi93X */ case OPTi9XX_HW_82C930: case OPTi9XX_HW_82C931: case OPTi9XX_HW_82C933: outb(reg, chip->mc_indir_index); outb(chip->password, chip->mc_base + chip->pwd_reg); outb(value, chip->mc_indir_index + 1); break; #endif /* OPTi93X */ default: snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); } spin_unlock_irqrestore(&chip->lock, flags); } static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip, unsigned char reg, unsigned char value, unsigned char mask) { unsigned char oldval = snd_opti9xx_read(chip, reg); snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask)); } static int snd_opti9xx_configure(struct snd_opti9xx *chip, long port, int irq, int dma1, int dma2, long mpu_port, int mpu_irq) { unsigned char wss_base_bits; unsigned char irq_bits; unsigned char dma_bits; unsigned char mpu_port_bits = 0; unsigned char mpu_irq_bits; switch (chip->hardware) { #ifndef OPTi93X case OPTi9XX_HW_82C924: /* opti 929 mode (?), OPL3 clock output, audio enable */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc); /* enable wave audio */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02); fallthrough; case OPTi9XX_HW_82C925: /* enable WSS mode */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); /* OPL3 FM synthesis */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); /* disable Sound Blaster IRQ and DMA */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff); #ifdef CS4231 /* cs4231/4248 fix enabled */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); #else /* cs4231/4248 fix disabled */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); #endif /* CS4231 */ break; case OPTi9XX_HW_82C928: case OPTi9XX_HW_82C929: snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); /* snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae); */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c); #ifdef CS4231 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); #else snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); #endif /* CS4231 */ break; #else /* OPTi93X */ case OPTi9XX_HW_82C931: /* disable 3D sound (set GPIO1 as output, low) */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c); fallthrough; case OPTi9XX_HW_82C933: /* * The BTC 1817DW has QS1000 wavetable which is connected * to the serial digital input of the OPTI931. */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff); /* * This bit sets OPTI931 to automaticaly select FM * or digital input signal. */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01); fallthrough; case OPTi9XX_HW_82C930: snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03); snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff); snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 | (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04), 0x34); snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf); break; #endif /* OPTi93X */ default: snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); return -EINVAL; } /* PnP resource says it decodes only 10 bits of address */ switch (port & 0x3ff) { case 0x130: chip->wss_base = 0x530; wss_base_bits = 0x00; break; case 0x204: chip->wss_base = 0x604; wss_base_bits = 0x03; break; case 0x280: chip->wss_base = 0xe80; wss_base_bits = 0x01; break; case 0x340: chip->wss_base = 0xf40; wss_base_bits = 0x02; break; default: snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port); goto __skip_base; } snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30); __skip_base: switch (irq) { //#ifdef OPTi93X case 5: irq_bits = 0x05; break; //#endif /* OPTi93X */ case 7: irq_bits = 0x01; break; case 9: irq_bits = 0x02; break; case 10: irq_bits = 0x03; break; case 11: irq_bits = 0x04; break; default: snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq); goto __skip_resources; } switch (dma1) { case 0: dma_bits = 0x01; break; case 1: dma_bits = 0x02; break; case 3: dma_bits = 0x03; break; default: snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1); goto __skip_resources; } #if defined(CS4231) || defined(OPTi93X) if (dma1 == dma2) { snd_printk(KERN_ERR "don't want to share dmas\n"); return -EBUSY; } switch (dma2) { case 0: case 1: break; default: snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2); goto __skip_resources; } dma_bits |= 0x04; #endif /* CS4231 || OPTi93X */ #ifndef OPTi93X outb(irq_bits << 3 | dma_bits, chip->wss_base); #else /* OPTi93X */ snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits)); #endif /* OPTi93X */ __skip_resources: if (chip->hardware > OPTi9XX_HW_82C928) { switch (mpu_port) { case 0: case -1: break; case 0x300: mpu_port_bits = 0x03; break; case 0x310: mpu_port_bits = 0x02; break; case 0x320: mpu_port_bits = 0x01; break; case 0x330: mpu_port_bits = 0x00; break; default: snd_printk(KERN_WARNING "MPU-401 port 0x%lx not valid\n", mpu_port); goto __skip_mpu; } switch (mpu_irq) { case 5: mpu_irq_bits = 0x02; break; case 7: mpu_irq_bits = 0x03; break; case 9: mpu_irq_bits = 0x00; break; case 10: mpu_irq_bits = 0x01; break; default: snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n", mpu_irq); goto __skip_mpu; } snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), (mpu_port <= 0) ? 0x00 : 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3, 0xf8); } __skip_mpu: return 0; } #ifdef OPTi93X static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0); static const struct snd_kcontrol_new snd_opti93x_controls[] = { WSS_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1), WSS_DOUBLE_TLV("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1, db_scale_5bit_3db_step), WSS_DOUBLE_TLV("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1, db_scale_5bit), WSS_DOUBLE_TLV("FM Playback Volume", 0, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1, db_scale_4bit_12db_max), WSS_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1), WSS_DOUBLE_TLV("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1, db_scale_4bit_12db_max), WSS_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1, db_scale_4bit_12db_max), WSS_DOUBLE_TLV("CD Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1, db_scale_4bit_12db_max), WSS_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1, db_scale_4bit_12db_max), }; static int snd_opti93x_mixer(struct snd_wss *chip) { struct snd_card *card; unsigned int idx; struct snd_ctl_elem_id id1, id2; int err; if (snd_BUG_ON(!chip || !chip->pcm)) return -EINVAL; card = chip->card; strcpy(card->mixername, chip->pcm->name); memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; /* reassign AUX0 switch to CD */ strcpy(id1.name, "Aux Playback Switch"); strcpy(id2.name, "CD Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "Cannot rename opti93x control\n"); return err; } /* reassign AUX1 switch to FM */ strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; strcpy(id2.name, "FM Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) { snd_printk(KERN_ERR "Cannot rename opti93x control\n"); return err; } /* remove AUX1 volume */ strcpy(id1.name, "Aux Playback Volume"); id1.index = 1; snd_ctl_remove_id(card, &id1); /* Replace WSS volume controls with OPTi93x volume controls */ id1.index = 0; for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) { strcpy(id1.name, snd_opti93x_controls[idx].name); snd_ctl_remove_id(card, &id1); err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip)); if (err < 0) return err; } return 0; } static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id) { struct snd_opti9xx *chip = dev_id; struct snd_wss *codec = chip->codec; unsigned char status; if (!codec) return IRQ_HANDLED; status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11)); if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream) snd_pcm_period_elapsed(codec->playback_substream); if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) { snd_wss_overrange(codec); snd_pcm_period_elapsed(codec->capture_substream); } outb(0x00, OPTi93X_PORT(codec, STATUS)); return IRQ_HANDLED; } #endif /* OPTi93X */ static int snd_opti9xx_read_check(struct snd_card *card, struct snd_opti9xx *chip) { unsigned char value; #ifdef OPTi93X unsigned long flags; #endif chip->res_mc_base = devm_request_region(card->dev, chip->mc_base, chip->mc_base_size, "OPTi9xx MC"); if (!chip->res_mc_base) return -EBUSY; #ifndef OPTi93X value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)); if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1))) if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1))) return 0; #else /* OPTi93X */ chip->res_mc_indir = devm_request_region(card->dev, chip->mc_indir_index, 2, "OPTi93x MC"); if (!chip->res_mc_indir) return -EBUSY; spin_lock_irqsave(&chip->lock, flags); outb(chip->password, chip->mc_base + chip->pwd_reg); outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base); spin_unlock_irqrestore(&chip->lock, flags); value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)); snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value); if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value) return 0; devm_release_resource(card->dev, chip->res_mc_indir); chip->res_mc_indir = NULL; #endif /* OPTi93X */ devm_release_resource(card->dev, chip->res_mc_base); chip->res_mc_base = NULL; return -ENODEV; } static int snd_card_opti9xx_detect(struct snd_card *card, struct snd_opti9xx *chip) { int i, err; #ifndef OPTi93X for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) { #else for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) { #endif err = snd_opti9xx_init(chip, i); if (err < 0) return err; err = snd_opti9xx_read_check(card, chip); if (err == 0) return 1; #ifdef OPTi93X chip->mc_indir_index = 0; #endif } return -ENODEV; } #ifdef CONFIG_PNP static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip, struct pnp_card_link *card, const struct pnp_card_device_id *pid) { struct pnp_dev *pdev; int err; struct pnp_dev *devmpu; #ifndef OPTi93X struct pnp_dev *devmc; #endif pdev = pnp_request_card_device(card, pid->devs[0].id, NULL); if (pdev == NULL) return -EBUSY; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err); return err; } #ifdef OPTi93X port = pnp_port_start(pdev, 0) - 4; fm_port = pnp_port_start(pdev, 1) + 8; /* adjust mc_indir_index - some cards report it at 0xe?d, other at 0xe?c but it really is always at 0xe?e */ chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe; #else devmc = pnp_request_card_device(card, pid->devs[2].id, NULL); if (devmc == NULL) return -EBUSY; err = pnp_activate_dev(devmc); if (err < 0) { snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err); return err; } port = pnp_port_start(pdev, 1); fm_port = pnp_port_start(pdev, 2) + 8; /* * The MC(0) is never accessed and card does not * include it in the PnP resource range. OPTI93x include it. */ chip->mc_base = pnp_port_start(devmc, 0) - 1; chip->mc_base_size = pnp_port_len(devmc, 0) + 1; #endif /* OPTi93X */ irq = pnp_irq(pdev, 0); dma1 = pnp_dma(pdev, 0); #if defined(CS4231) || defined(OPTi93X) dma2 = pnp_dma(pdev, 1); #endif /* CS4231 || OPTi93X */ devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL); if (devmpu && mpu_port > 0) { err = pnp_activate_dev(devmpu); if (err < 0) { snd_printk(KERN_ERR "MPU401 pnp configure failure\n"); mpu_port = -1; } else { mpu_port = pnp_port_start(devmpu, 0); mpu_irq = pnp_irq(devmpu, 0); } } return pid->driver_data; } #endif /* CONFIG_PNP */ static int snd_opti9xx_probe(struct snd_card *card) { static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1}; int error; int xdma2; struct snd_opti9xx *chip = card->private_data; struct snd_wss *codec; struct snd_rawmidi *rmidi; struct snd_hwdep *synth; #if defined(CS4231) || defined(OPTi93X) xdma2 = dma2; #else xdma2 = -1; #endif if (port == SNDRV_AUTO_PORT) { port = snd_legacy_find_free_ioport(possible_ports, 4); if (port < 0) { snd_printk(KERN_ERR "unable to find a free WSS port\n"); return -EBUSY; } } error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, mpu_port, mpu_irq); if (error) return error; error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2, #ifdef OPTi93X WSS_HW_OPTI93X, WSS_HWSHARE_IRQ, #else WSS_HW_DETECT, 0, #endif &codec); if (error < 0) return error; chip->codec = codec; error = snd_wss_pcm(codec, 0); if (error < 0) return error; error = snd_wss_mixer(codec); if (error < 0) return error; #ifdef OPTi93X error = snd_opti93x_mixer(codec); if (error < 0) return error; #endif #ifdef CS4231 error = snd_wss_timer(codec, 0); if (error < 0) return error; #endif #ifdef OPTi93X error = devm_request_irq(card->dev, irq, snd_opti93x_interrupt, 0, DEV_NAME" - WSS", chip); if (error < 0) { snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq); return error; } #endif chip->irq = irq; card->sync_irq = chip->irq; strcpy(card->driver, chip->name); sprintf(card->shortname, "OPTi %s", card->driver); #if defined(CS4231) || defined(OPTi93X) scnprintf(card->longname, sizeof(card->longname), "%s, %s at 0x%lx, irq %d, dma %d&%d", card->shortname, codec->pcm->name, chip->wss_base + 4, irq, dma1, xdma2); #else scnprintf(card->longname, sizeof(card->longname), "%s, %s at 0x%lx, irq %d, dma %d", card->shortname, codec->pcm->name, chip->wss_base + 4, irq, dma1); #endif /* CS4231 || OPTi93X */ if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT) rmidi = NULL; else { error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_port, 0, mpu_irq, &rmidi); if (error) snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n", mpu_port); } if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) { struct snd_opl3 *opl3 = NULL; #ifndef OPTi93X if (chip->hardware == OPTi9XX_HW_82C928 || chip->hardware == OPTi9XX_HW_82C929 || chip->hardware == OPTi9XX_HW_82C924) { struct snd_opl4 *opl4; /* assume we have an OPL4 */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); if (snd_opl4_create(card, fm_port, fm_port - 8, 2, &opl3, &opl4) < 0) { /* no luck, use OPL3 instead */ snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); } } #endif /* !OPTi93X */ if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2, OPL3_HW_AUTO, 0, &opl3) < 0) { snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n", fm_port, fm_port + 4 - 1); } if (opl3) { error = snd_opl3_hwdep_new(opl3, 0, 1, &synth); if (error < 0) return error; } } return snd_card_register(card); } static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp) { struct snd_card *card; int err; err = snd_devm_card_new(pdev, index, id, THIS_MODULE, sizeof(struct snd_opti9xx), &card); if (err < 0) return err; *cardp = card; return 0; } static int snd_opti9xx_isa_match(struct device *devptr, unsigned int dev) { #ifdef CONFIG_PNP if (snd_opti9xx_pnp_is_probed) return 0; if (isapnp) return 0; #endif return 1; } static int snd_opti9xx_isa_probe(struct device *devptr, unsigned int dev) { struct snd_card *card; int error; static const long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1}; #ifdef OPTi93X static const int possible_irqs[] = {5, 9, 10, 11, 7, -1}; #else static const int possible_irqs[] = {9, 10, 11, 7, -1}; #endif /* OPTi93X */ static const int possible_mpu_irqs[] = {5, 9, 10, 7, -1}; static const int possible_dma1s[] = {3, 1, 0, -1}; #if defined(CS4231) || defined(OPTi93X) static const int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}}; #endif /* CS4231 || OPTi93X */ if (mpu_port == SNDRV_AUTO_PORT) { mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2); if (mpu_port < 0) { snd_printk(KERN_ERR "unable to find a free MPU401 port\n"); return -EBUSY; } } if (irq == SNDRV_AUTO_IRQ) { irq = snd_legacy_find_free_irq(possible_irqs); if (irq < 0) { snd_printk(KERN_ERR "unable to find a free IRQ\n"); return -EBUSY; } } if (mpu_irq == SNDRV_AUTO_IRQ) { mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs); if (mpu_irq < 0) { snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n"); return -EBUSY; } } if (dma1 == SNDRV_AUTO_DMA) { dma1 = snd_legacy_find_free_dma(possible_dma1s); if (dma1 < 0) { snd_printk(KERN_ERR "unable to find a free DMA1\n"); return -EBUSY; } } #if defined(CS4231) || defined(OPTi93X) if (dma2 == SNDRV_AUTO_DMA) { dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]); if (dma2 < 0) { snd_printk(KERN_ERR "unable to find a free DMA2\n"); return -EBUSY; } } #endif error = snd_opti9xx_card_new(devptr, &card); if (error < 0) return error; error = snd_card_opti9xx_detect(card, card->private_data); if (error < 0) return error; error = snd_opti9xx_probe(card); if (error < 0) return error; dev_set_drvdata(devptr, card); return 0; } #ifdef CONFIG_PM static int snd_opti9xx_suspend(struct snd_card *card) { struct snd_opti9xx *chip = card->private_data; snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); chip->codec->suspend(chip->codec); return 0; } static int snd_opti9xx_resume(struct snd_card *card) { struct snd_opti9xx *chip = card->private_data; int error, xdma2; #if defined(CS4231) || defined(OPTi93X) xdma2 = dma2; #else xdma2 = -1; #endif error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, mpu_port, mpu_irq); if (error) return error; chip->codec->resume(chip->codec); snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; } static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n, pm_message_t state) { return snd_opti9xx_suspend(dev_get_drvdata(dev)); } static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n) { return snd_opti9xx_resume(dev_get_drvdata(dev)); } #endif static struct isa_driver snd_opti9xx_driver = { .match = snd_opti9xx_isa_match, .probe = snd_opti9xx_isa_probe, #ifdef CONFIG_PM .suspend = snd_opti9xx_isa_suspend, .resume = snd_opti9xx_isa_resume, #endif .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { struct snd_card *card; int error, hw; struct snd_opti9xx *chip; if (snd_opti9xx_pnp_is_probed) return -EBUSY; if (! isapnp) return -ENODEV; error = snd_opti9xx_card_new(&pcard->card->dev, &card); if (error < 0) return error; chip = card->private_data; hw = snd_card_opti9xx_pnp(chip, pcard, pid); switch (hw) { case 0x0924: hw = OPTi9XX_HW_82C924; break; case 0x0925: hw = OPTi9XX_HW_82C925; break; case 0x0931: hw = OPTi9XX_HW_82C931; break; default: return -ENODEV; } error = snd_opti9xx_init(chip, hw); if (error) return error; error = snd_opti9xx_read_check(card, chip); if (error) { snd_printk(KERN_ERR "OPTI chip not found\n"); return error; } error = snd_opti9xx_probe(card); if (error < 0) return error; pnp_set_card_drvdata(pcard, card); snd_opti9xx_pnp_is_probed = 1; return 0; } static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard) { snd_opti9xx_pnp_is_probed = 0; } #ifdef CONFIG_PM static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) { return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard)); } static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard) { return snd_opti9xx_resume(pnp_get_card_drvdata(pcard)); } #endif static struct pnp_card_driver opti9xx_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = DEV_NAME, .id_table = snd_opti9xx_pnpids, .probe = snd_opti9xx_pnp_probe, .remove = snd_opti9xx_pnp_remove, #ifdef CONFIG_PM .suspend = snd_opti9xx_pnp_suspend, .resume = snd_opti9xx_pnp_resume, #endif }; #endif #ifdef OPTi93X #define CHIP_NAME "82C93x" #else #define CHIP_NAME "82C92x" #endif static int __init alsa_card_opti9xx_init(void) { #ifdef CONFIG_PNP pnp_register_card_driver(&opti9xx_pnpc_driver); if (snd_opti9xx_pnp_is_probed) return 0; pnp_unregister_card_driver(&opti9xx_pnpc_driver); #endif return isa_register_driver(&snd_opti9xx_driver, 1); } static void __exit alsa_card_opti9xx_exit(void) { if (!snd_opti9xx_pnp_is_probed) { isa_unregister_driver(&snd_opti9xx_driver); return; } #ifdef CONFIG_PNP pnp_unregister_card_driver(&opti9xx_pnpc_driver); #endif } module_init(alsa_card_opti9xx_init) module_exit(alsa_card_opti9xx_exit)
linux-master
sound/isa/opti9xx/opti92x-ad1848.c
#define CS4231 #include "opti92x-ad1848.c"
linux-master
sound/isa/opti9xx/opti92x-cs4231.c
#define OPTi93X #include "opti92x-ad1848.c"
linux-master
sound/isa/opti9xx/opti93x.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * ALSA soundcard driver for Miro miroSOUND PCM1 pro * miroSOUND PCM12 * miroSOUND PCM20 Radio * * Copyright (C) 2004-2005 Martin Langer <[email protected]> * * Based on OSS ACI and ALSA OPTi9xx drivers */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/pnp.h> #include <linux/delay.h> #include <linux/ioport.h> #include <linux/module.h> #include <linux/io.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/opl4.h> #include <sound/control.h> #include <sound/info.h> #define SNDRV_LEGACY_FIND_FREE_IOPORT #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #include <sound/aci.h> MODULE_AUTHOR("Martin Langer <[email protected]>"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio"); static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */ static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */ static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */ static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */ static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */ static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ static int wss; static int ide; #ifdef CONFIG_PNP static bool isapnp = 1; /* Enable ISA PnP detection */ #endif module_param(index, int, 0444); MODULE_PARM_DESC(index, "Index value for miro soundcard."); module_param(id, charp, 0444); MODULE_PARM_DESC(id, "ID string for miro soundcard."); module_param_hw(port, long, ioport, 0444); MODULE_PARM_DESC(port, "WSS port # for miro driver."); module_param_hw(mpu_port, long, ioport, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver."); module_param_hw(fm_port, long, ioport, 0444); MODULE_PARM_DESC(fm_port, "FM Port # for miro driver."); module_param_hw(irq, int, irq, 0444); MODULE_PARM_DESC(irq, "WSS irq # for miro driver."); module_param_hw(mpu_irq, int, irq, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver."); module_param_hw(dma1, int, dma, 0444); MODULE_PARM_DESC(dma1, "1st dma # for miro driver."); module_param_hw(dma2, int, dma, 0444); MODULE_PARM_DESC(dma2, "2nd dma # for miro driver."); module_param(wss, int, 0444); MODULE_PARM_DESC(wss, "wss mode"); module_param(ide, int, 0444); MODULE_PARM_DESC(ide, "enable ide port"); #ifdef CONFIG_PNP module_param(isapnp, bool, 0444); MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard."); #endif #define OPTi9XX_HW_DETECT 0 #define OPTi9XX_HW_82C928 1 #define OPTi9XX_HW_82C929 2 #define OPTi9XX_HW_82C924 3 #define OPTi9XX_HW_82C925 4 #define OPTi9XX_HW_82C930 5 #define OPTi9XX_HW_82C931 6 #define OPTi9XX_HW_82C933 7 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933 #define OPTi9XX_MC_REG(n) n struct snd_miro { unsigned short hardware; unsigned char password; char name[7]; struct resource *res_mc_base; struct resource *res_aci_port; unsigned long mc_base; unsigned long mc_base_size; unsigned long pwd_reg; spinlock_t lock; struct snd_pcm *pcm; long wss_base; int irq; int dma1; int dma2; long mpu_port; int mpu_irq; struct snd_miro_aci *aci; }; static struct snd_miro_aci aci_device; static const char * const snd_opti9xx_names[] = { "unknown", "82C928", "82C929", "82C924", "82C925", "82C930", "82C931", "82C933" }; static int snd_miro_pnp_is_probed; #ifdef CONFIG_PNP static const struct pnp_card_device_id snd_miro_pnpids[] = { /* PCM20 and PCM12 in PnP mode */ { .id = "MIR0924", .devs = { { "MIR0000" }, { "MIR0002" }, { "MIR0005" } }, }, { .id = "" } }; MODULE_DEVICE_TABLE(pnp_card, snd_miro_pnpids); #endif /* CONFIG_PNP */ /* * ACI control */ static int aci_busy_wait(struct snd_miro_aci *aci) { long timeout; unsigned char byte; for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) { byte = inb(aci->aci_port + ACI_REG_BUSY); if ((byte & 1) == 0) { if (timeout >= ACI_MINTIME) snd_printd("aci ready in round %ld.\n", timeout-ACI_MINTIME); return byte; } if (timeout >= ACI_MINTIME) { long out=10*HZ; switch (timeout-ACI_MINTIME) { case 0 ... 9: out /= 10; fallthrough; case 10 ... 19: out /= 10; fallthrough; case 20 ... 30: out /= 10; fallthrough; default: set_current_state(TASK_UNINTERRUPTIBLE); schedule_timeout(out); break; } } } snd_printk(KERN_ERR "aci_busy_wait() time out\n"); return -EBUSY; } static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte) { if (aci_busy_wait(aci) >= 0) { outb(byte, aci->aci_port + ACI_REG_COMMAND); return 0; } else { snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte); return -EBUSY; } } static inline int aci_read(struct snd_miro_aci *aci) { unsigned char byte; if (aci_busy_wait(aci) >= 0) { byte = inb(aci->aci_port + ACI_REG_STATUS); return byte; } else { snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n"); return -EBUSY; } } int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3) { int write[] = {write1, write2, write3}; int value, i; if (mutex_lock_interruptible(&aci->aci_mutex)) return -EINTR; for (i=0; i<3; i++) { if (write[i]< 0 || write[i] > 255) break; else { value = aci_write(aci, write[i]); if (value < 0) goto out; } } value = aci_read(aci); out: mutex_unlock(&aci->aci_mutex); return value; } EXPORT_SYMBOL(snd_aci_cmd); static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index) { return snd_aci_cmd(aci, ACI_STATUS, index, -1); } static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index, int value) { return snd_aci_cmd(aci, index, value, -1); } struct snd_miro_aci *snd_aci_get_aci(void) { if (aci_device.aci_port == 0) return NULL; return &aci_device; } EXPORT_SYMBOL(snd_aci_get_aci); /* * MIXER part */ #define snd_miro_info_capture snd_ctl_boolean_mono_info static int snd_miro_get_capture(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); int value; value = aci_getvalue(miro->aci, ACI_S_GENERAL); if (value < 0) { snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n", value); return value; } ucontrol->value.integer.value[0] = value & 0x20; return 0; } static int snd_miro_put_capture(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); int change, value, error; value = !(ucontrol->value.integer.value[0]); error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value); if (error < 0) { snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n", error); return error; } change = (value != miro->aci->aci_solomode); miro->aci->aci_solomode = value; return change; } static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 3; return 0; } static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); int value; if (miro->aci->aci_version <= 176) { /* OSS says it's not readable with versions < 176. But it doesn't work on my card, which is a PCM12 with aci_version = 176. */ ucontrol->value.integer.value[0] = miro->aci->aci_preamp; return 0; } value = aci_getvalue(miro->aci, ACI_GET_PREAMP); if (value < 0) { snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n", value); return value; } ucontrol->value.integer.value[0] = value; return 0; } static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); int error, value, change; value = ucontrol->value.integer.value[0]; error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value); if (error < 0) { snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n", error); return error; } change = (value != miro->aci->aci_preamp); miro->aci->aci_preamp = value; return change; } #define snd_miro_info_amp snd_ctl_boolean_mono_info static int snd_miro_get_amp(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = miro->aci->aci_amp; return 0; } static int snd_miro_put_amp(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); int error, value, change; value = ucontrol->value.integer.value[0]; error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value); if (error < 0) { snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error); return error; } change = (value != miro->aci->aci_amp); miro->aci->aci_amp = value; return change; } #define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ .name = ctl_name, \ .index = ctl_index, \ .info = snd_miro_info_double, \ .get = snd_miro_get_double, \ .put = snd_miro_put_double, \ .private_value = get_right_reg | (set_right_reg << 8) \ } static int snd_miro_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int reg = kcontrol->private_value & 0xff; uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) { /* equalizer elements */ uinfo->value.integer.min = - 0x7f; uinfo->value.integer.max = 0x7f; } else { /* non-equalizer elements */ uinfo->value.integer.min = 0; uinfo->value.integer.max = 0x20; } return 0; } static int snd_miro_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *uinfo) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); int left_val, right_val; int right_reg = kcontrol->private_value & 0xff; int left_reg = right_reg + 1; right_val = aci_getvalue(miro->aci, right_reg); if (right_val < 0) { snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val); return right_val; } left_val = aci_getvalue(miro->aci, left_reg); if (left_val < 0) { snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val); return left_val; } if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) { /* equalizer elements */ if (left_val < 0x80) { uinfo->value.integer.value[0] = left_val; } else { uinfo->value.integer.value[0] = 0x80 - left_val; } if (right_val < 0x80) { uinfo->value.integer.value[1] = right_val; } else { uinfo->value.integer.value[1] = 0x80 - right_val; } } else { /* non-equalizer elements */ uinfo->value.integer.value[0] = 0x20 - left_val; uinfo->value.integer.value[1] = 0x20 - right_val; } return 0; } static int snd_miro_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_miro *miro = snd_kcontrol_chip(kcontrol); struct snd_miro_aci *aci = miro->aci; int left, right, left_old, right_old; int setreg_left, setreg_right, getreg_left, getreg_right; int change, error; left = ucontrol->value.integer.value[0]; right = ucontrol->value.integer.value[1]; setreg_right = (kcontrol->private_value >> 8) & 0xff; setreg_left = setreg_right + 8; if (setreg_right == ACI_SET_MASTER) setreg_left -= 7; getreg_right = kcontrol->private_value & 0xff; getreg_left = getreg_right + 1; left_old = aci_getvalue(aci, getreg_left); if (left_old < 0) { snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old); return left_old; } right_old = aci_getvalue(aci, getreg_right); if (right_old < 0) { snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old); return right_old; } if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) { /* equalizer elements */ if (left < -0x7f || left > 0x7f || right < -0x7f || right > 0x7f) return -EINVAL; if (left_old > 0x80) left_old = 0x80 - left_old; if (right_old > 0x80) right_old = 0x80 - right_old; if (left >= 0) { error = aci_setvalue(aci, setreg_left, left); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", left, error); return error; } } else { error = aci_setvalue(aci, setreg_left, 0x80 - left); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 0x80 - left, error); return error; } } if (right >= 0) { error = aci_setvalue(aci, setreg_right, right); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", right, error); return error; } } else { error = aci_setvalue(aci, setreg_right, 0x80 - right); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 0x80 - right, error); return error; } } } else { /* non-equalizer elements */ if (left < 0 || left > 0x20 || right < 0 || right > 0x20) return -EINVAL; left_old = 0x20 - left_old; right_old = 0x20 - right_old; error = aci_setvalue(aci, setreg_left, 0x20 - left); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 0x20 - left, error); return error; } error = aci_setvalue(aci, setreg_right, 0x20 - right); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 0x20 - right, error); return error; } } change = (left != left_old) || (right != right_old); return change; } static const struct snd_kcontrol_new snd_miro_controls[] = { MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER), MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC), MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE), MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD), MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH), MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM), MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2), }; /* Equalizer with seven bands (only PCM20) from -12dB up to +12dB on each band */ static const struct snd_kcontrol_new snd_miro_eq_controls[] = { MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1), MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2), MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3), MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4), MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5), MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6), MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7), }; static const struct snd_kcontrol_new snd_miro_radio_control[] = { MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1), }; static const struct snd_kcontrol_new snd_miro_line_control[] = { MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1), }; static const struct snd_kcontrol_new snd_miro_preamp_control[] = { { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Mic Boost", .index = 1, .info = snd_miro_info_preamp, .get = snd_miro_get_preamp, .put = snd_miro_put_preamp, }}; static const struct snd_kcontrol_new snd_miro_amp_control[] = { { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Line Boost", .index = 0, .info = snd_miro_info_amp, .get = snd_miro_get_amp, .put = snd_miro_put_amp, }}; static const struct snd_kcontrol_new snd_miro_capture_control[] = { { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "PCM Capture Switch", .index = 0, .info = snd_miro_info_capture, .get = snd_miro_get_capture, .put = snd_miro_put_capture, }}; static const unsigned char aci_init_values[][2] = { { ACI_SET_MUTE, 0x00 }, { ACI_SET_POWERAMP, 0x00 }, { ACI_SET_PREAMP, 0x00 }, { ACI_SET_SOLOMODE, 0x00 }, { ACI_SET_MIC + 0, 0x20 }, { ACI_SET_MIC + 8, 0x20 }, { ACI_SET_LINE + 0, 0x20 }, { ACI_SET_LINE + 8, 0x20 }, { ACI_SET_CD + 0, 0x20 }, { ACI_SET_CD + 8, 0x20 }, { ACI_SET_PCM + 0, 0x20 }, { ACI_SET_PCM + 8, 0x20 }, { ACI_SET_LINE1 + 0, 0x20 }, { ACI_SET_LINE1 + 8, 0x20 }, { ACI_SET_LINE2 + 0, 0x20 }, { ACI_SET_LINE2 + 8, 0x20 }, { ACI_SET_SYNTH + 0, 0x20 }, { ACI_SET_SYNTH + 8, 0x20 }, { ACI_SET_MASTER + 0, 0x20 }, { ACI_SET_MASTER + 1, 0x20 }, }; static int snd_set_aci_init_values(struct snd_miro *miro) { int idx, error; struct snd_miro_aci *aci = miro->aci; /* enable WSS on PCM1 */ if ((aci->aci_product == 'A') && wss) { error = aci_setvalue(aci, ACI_SET_WSS, wss); if (error < 0) { snd_printk(KERN_ERR "enabling WSS mode failed\n"); return error; } } /* enable IDE port */ if (ide) { error = aci_setvalue(aci, ACI_SET_IDE, ide); if (error < 0) { snd_printk(KERN_ERR "enabling IDE port failed\n"); return error; } } /* set common aci values */ for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) { error = aci_setvalue(aci, aci_init_values[idx][0], aci_init_values[idx][1]); if (error < 0) { snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", aci_init_values[idx][0], error); return error; } } aci->aci_amp = 0; aci->aci_preamp = 0; aci->aci_solomode = 1; return 0; } static int snd_miro_mixer(struct snd_card *card, struct snd_miro *miro) { unsigned int idx; int err; if (snd_BUG_ON(!miro || !card)) return -EINVAL; switch (miro->hardware) { case OPTi9XX_HW_82C924: strcpy(card->mixername, "ACI & OPTi924"); break; case OPTi9XX_HW_82C929: strcpy(card->mixername, "ACI & OPTi929"); break; default: snd_BUG(); break; } for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro)); if (err < 0) return err; } if ((miro->aci->aci_product == 'A') || (miro->aci->aci_product == 'B')) { /* PCM1/PCM12 with power-amp and Line 2 */ err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro)); if (err < 0) return err; err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro)); if (err < 0) return err; } if ((miro->aci->aci_product == 'B') || (miro->aci->aci_product == 'C')) { /* PCM12/PCM20 with mic-preamp */ err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro)); if (err < 0) return err; if (miro->aci->aci_version >= 176) { err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro)); if (err < 0) return err; } } if (miro->aci->aci_product == 'C') { /* PCM20 with radio and 7 band equalizer */ err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro)); if (err < 0) return err; for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro)); if (err < 0) return err; } } return 0; } static int snd_miro_init(struct snd_miro *chip, unsigned short hardware) { static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2}; chip->hardware = hardware; strcpy(chip->name, snd_opti9xx_names[hardware]); chip->mc_base_size = opti9xx_mc_size[hardware]; spin_lock_init(&chip->lock); chip->wss_base = -1; chip->irq = -1; chip->dma1 = -1; chip->dma2 = -1; chip->mpu_port = -1; chip->mpu_irq = -1; chip->pwd_reg = 3; #ifdef CONFIG_PNP if (isapnp && chip->mc_base) /* PnP resource gives the least 10 bits */ chip->mc_base |= 0xc00; else #endif chip->mc_base = 0xf8c; switch (hardware) { case OPTi9XX_HW_82C929: chip->password = 0xe3; break; case OPTi9XX_HW_82C924: chip->password = 0xe5; break; default: snd_printk(KERN_ERR "sorry, no support for %d\n", hardware); return -ENODEV; } return 0; } static unsigned char snd_miro_read(struct snd_miro *chip, unsigned char reg) { unsigned long flags; unsigned char retval = 0xff; spin_lock_irqsave(&chip->lock, flags); outb(chip->password, chip->mc_base + chip->pwd_reg); switch (chip->hardware) { case OPTi9XX_HW_82C924: if (reg > 7) { outb(reg, chip->mc_base + 8); outb(chip->password, chip->mc_base + chip->pwd_reg); retval = inb(chip->mc_base + 9); break; } fallthrough; case OPTi9XX_HW_82C929: retval = inb(chip->mc_base + reg); break; default: snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware); } spin_unlock_irqrestore(&chip->lock, flags); return retval; } static void snd_miro_write(struct snd_miro *chip, unsigned char reg, unsigned char value) { unsigned long flags; spin_lock_irqsave(&chip->lock, flags); outb(chip->password, chip->mc_base + chip->pwd_reg); switch (chip->hardware) { case OPTi9XX_HW_82C924: if (reg > 7) { outb(reg, chip->mc_base + 8); outb(chip->password, chip->mc_base + chip->pwd_reg); outb(value, chip->mc_base + 9); break; } fallthrough; case OPTi9XX_HW_82C929: outb(value, chip->mc_base + reg); break; default: snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware); } spin_unlock_irqrestore(&chip->lock, flags); } static inline void snd_miro_write_mask(struct snd_miro *chip, unsigned char reg, unsigned char value, unsigned char mask) { unsigned char oldval = snd_miro_read(chip, reg); snd_miro_write(chip, reg, (oldval & ~mask) | (value & mask)); } /* * Proc Interface */ static void snd_miro_proc_read(struct snd_info_entry * entry, struct snd_info_buffer *buffer) { struct snd_miro *miro = (struct snd_miro *) entry->private_data; struct snd_miro_aci *aci = miro->aci; char* model = "unknown"; /* miroSOUND PCM1 pro, early PCM12 */ if ((miro->hardware == OPTi9XX_HW_82C929) && (aci->aci_vendor == 'm') && (aci->aci_product == 'A')) { switch (aci->aci_version) { case 3: model = "miroSOUND PCM1 pro"; break; default: model = "miroSOUND PCM1 pro / (early) PCM12"; break; } } /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */ if ((miro->hardware == OPTi9XX_HW_82C924) && (aci->aci_vendor == 'm') && (aci->aci_product == 'B')) { switch (aci->aci_version) { case 4: model = "miroSOUND PCM12"; break; case 176: model = "miroSOUND PCM12 (Rev. E)"; break; default: model = "miroSOUND PCM12 / PCM12 pnp"; break; } } /* miroSOUND PCM20 radio */ if ((miro->hardware == OPTi9XX_HW_82C924) && (aci->aci_vendor == 'm') && (aci->aci_product == 'C')) { switch (aci->aci_version) { case 7: model = "miroSOUND PCM20 radio (Rev. E)"; break; default: model = "miroSOUND PCM20 radio"; break; } } snd_iprintf(buffer, "\nGeneral information:\n"); snd_iprintf(buffer, " model : %s\n", model); snd_iprintf(buffer, " opti : %s\n", miro->name); snd_iprintf(buffer, " codec : %s\n", miro->pcm->name); snd_iprintf(buffer, " port : 0x%lx\n", miro->wss_base); snd_iprintf(buffer, " irq : %d\n", miro->irq); snd_iprintf(buffer, " dma : %d,%d\n\n", miro->dma1, miro->dma2); snd_iprintf(buffer, "MPU-401:\n"); snd_iprintf(buffer, " port : 0x%lx\n", miro->mpu_port); snd_iprintf(buffer, " irq : %d\n\n", miro->mpu_irq); snd_iprintf(buffer, "ACI information:\n"); snd_iprintf(buffer, " vendor : "); switch (aci->aci_vendor) { case 'm': snd_iprintf(buffer, "Miro\n"); break; default: snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor); break; } snd_iprintf(buffer, " product : "); switch (aci->aci_product) { case 'A': snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n"); break; case 'B': snd_iprintf(buffer, "miroSOUND PCM12\n"); break; case 'C': snd_iprintf(buffer, "miroSOUND PCM20 radio\n"); break; default: snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product); break; } snd_iprintf(buffer, " firmware: %d (0x%x)\n", aci->aci_version, aci->aci_version); snd_iprintf(buffer, " port : 0x%lx-0x%lx\n", aci->aci_port, aci->aci_port+2); snd_iprintf(buffer, " wss : 0x%x\n", wss); snd_iprintf(buffer, " ide : 0x%x\n", ide); snd_iprintf(buffer, " solomode: 0x%x\n", aci->aci_solomode); snd_iprintf(buffer, " amp : 0x%x\n", aci->aci_amp); snd_iprintf(buffer, " preamp : 0x%x\n", aci->aci_preamp); } static void snd_miro_proc_init(struct snd_card *card, struct snd_miro *miro) { snd_card_ro_proc_new(card, "miro", miro, snd_miro_proc_read); } /* * Init */ static int snd_miro_configure(struct snd_miro *chip) { unsigned char wss_base_bits; unsigned char irq_bits; unsigned char dma_bits; unsigned char mpu_port_bits = 0; unsigned char mpu_irq_bits; unsigned long flags; snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */ snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); switch (chip->hardware) { case OPTi9XX_HW_82C924: snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02); snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff); break; case OPTi9XX_HW_82C929: /* untested init commands for OPTi929 */ snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c); break; default: snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); return -EINVAL; } /* PnP resource says it decodes only 10 bits of address */ switch (chip->wss_base & 0x3ff) { case 0x130: chip->wss_base = 0x530; wss_base_bits = 0x00; break; case 0x204: chip->wss_base = 0x604; wss_base_bits = 0x03; break; case 0x280: chip->wss_base = 0xe80; wss_base_bits = 0x01; break; case 0x340: chip->wss_base = 0xf40; wss_base_bits = 0x02; break; default: snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base); goto __skip_base; } snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30); __skip_base: switch (chip->irq) { case 5: irq_bits = 0x05; break; case 7: irq_bits = 0x01; break; case 9: irq_bits = 0x02; break; case 10: irq_bits = 0x03; break; case 11: irq_bits = 0x04; break; default: snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq); goto __skip_resources; } switch (chip->dma1) { case 0: dma_bits = 0x01; break; case 1: dma_bits = 0x02; break; case 3: dma_bits = 0x03; break; default: snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1); goto __skip_resources; } if (chip->dma1 == chip->dma2) { snd_printk(KERN_ERR "don't want to share dmas\n"); return -EBUSY; } switch (chip->dma2) { case 0: case 1: break; default: snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2); goto __skip_resources; } dma_bits |= 0x04; spin_lock_irqsave(&chip->lock, flags); outb(irq_bits << 3 | dma_bits, chip->wss_base); spin_unlock_irqrestore(&chip->lock, flags); __skip_resources: if (chip->hardware > OPTi9XX_HW_82C928) { switch (chip->mpu_port) { case 0: case -1: break; case 0x300: mpu_port_bits = 0x03; break; case 0x310: mpu_port_bits = 0x02; break; case 0x320: mpu_port_bits = 0x01; break; case 0x330: mpu_port_bits = 0x00; break; default: snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n", chip->mpu_port); goto __skip_mpu; } switch (chip->mpu_irq) { case 5: mpu_irq_bits = 0x02; break; case 7: mpu_irq_bits = 0x03; break; case 9: mpu_irq_bits = 0x00; break; case 10: mpu_irq_bits = 0x01; break; default: snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n", chip->mpu_irq); goto __skip_mpu; } snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), (chip->mpu_port <= 0) ? 0x00 : 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3, 0xf8); } __skip_mpu: return 0; } static int snd_miro_opti_check(struct snd_card *card, struct snd_miro *chip) { unsigned char value; chip->res_mc_base = devm_request_region(card->dev, chip->mc_base, chip->mc_base_size, "OPTi9xx MC"); if (chip->res_mc_base == NULL) return -ENOMEM; value = snd_miro_read(chip, OPTi9XX_MC_REG(1)); if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1))) if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1))) return 0; devm_release_resource(card->dev, chip->res_mc_base); chip->res_mc_base = NULL; return -ENODEV; } static int snd_card_miro_detect(struct snd_card *card, struct snd_miro *chip) { int i, err; for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) { err = snd_miro_init(chip, i); if (err < 0) return err; err = snd_miro_opti_check(card, chip); if (err == 0) return 1; } return -ENODEV; } static int snd_card_miro_aci_detect(struct snd_card *card, struct snd_miro *miro) { unsigned char regval; int i; struct snd_miro_aci *aci = &aci_device; miro->aci = aci; mutex_init(&aci->aci_mutex); /* get ACI port from OPTi9xx MC 4 */ regval=inb(miro->mc_base + 4); aci->aci_port = (regval & 0x10) ? 0x344 : 0x354; miro->res_aci_port = devm_request_region(card->dev, aci->aci_port, 3, "miro aci"); if (miro->res_aci_port == NULL) { snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n", aci->aci_port, aci->aci_port+2); return -ENOMEM; } /* force ACI into a known state */ for (i = 0; i < 3; i++) if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) { snd_printk(KERN_ERR "can't force aci into known state.\n"); return -ENXIO; } aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1); aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1); if (aci->aci_vendor < 0 || aci->aci_product < 0) { snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n", aci->aci_port); return -ENXIO; } aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1); if (aci->aci_version < 0) { snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n", aci->aci_port); return -ENXIO; } if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 || snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 || snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) { snd_printk(KERN_ERR "can't initialize aci.\n"); return -ENXIO; } return 0; } static int snd_miro_probe(struct snd_card *card) { int error; struct snd_miro *miro = card->private_data; struct snd_wss *codec; struct snd_rawmidi *rmidi; if (!miro->res_mc_base) { miro->res_mc_base = devm_request_region(card->dev, miro->mc_base, miro->mc_base_size, "miro (OPTi9xx MC)"); if (miro->res_mc_base == NULL) { snd_printk(KERN_ERR "request for OPTI9xx MC failed\n"); return -ENOMEM; } } error = snd_card_miro_aci_detect(card, miro); if (error < 0) { snd_printk(KERN_ERR "unable to detect aci chip\n"); return -ENODEV; } miro->wss_base = port; miro->mpu_port = mpu_port; miro->irq = irq; miro->mpu_irq = mpu_irq; miro->dma1 = dma1; miro->dma2 = dma2; /* init proc interface */ snd_miro_proc_init(card, miro); error = snd_miro_configure(miro); if (error) return error; error = snd_wss_create(card, miro->wss_base + 4, -1, miro->irq, miro->dma1, miro->dma2, WSS_HW_DETECT, 0, &codec); if (error < 0) return error; error = snd_wss_pcm(codec, 0); if (error < 0) return error; error = snd_wss_mixer(codec); if (error < 0) return error; error = snd_wss_timer(codec, 0); if (error < 0) return error; miro->pcm = codec->pcm; error = snd_miro_mixer(card, miro); if (error < 0) return error; if (miro->aci->aci_vendor == 'm') { /* It looks like a miro sound card. */ switch (miro->aci->aci_product) { case 'A': sprintf(card->shortname, "miroSOUND PCM1 pro / PCM12"); break; case 'B': sprintf(card->shortname, "miroSOUND PCM12"); break; case 'C': sprintf(card->shortname, "miroSOUND PCM20 radio"); break; default: sprintf(card->shortname, "unknown miro"); snd_printk(KERN_INFO "unknown miro aci id\n"); break; } } else { snd_printk(KERN_INFO "found unsupported aci card\n"); sprintf(card->shortname, "unknown Cardinal Technologies"); } strcpy(card->driver, "miro"); scnprintf(card->longname, sizeof(card->longname), "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d", card->shortname, miro->name, codec->pcm->name, miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2); if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT) rmidi = NULL; else { error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_port, 0, miro->mpu_irq, &rmidi); if (error < 0) snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n", mpu_port); } if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) { struct snd_opl3 *opl3 = NULL; struct snd_opl4 *opl4; if (snd_opl4_create(card, fm_port, fm_port - 8, 2, &opl3, &opl4) < 0) snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n", fm_port); } error = snd_set_aci_init_values(miro); if (error < 0) return error; return snd_card_register(card); } static int snd_miro_isa_match(struct device *devptr, unsigned int n) { #ifdef CONFIG_PNP if (snd_miro_pnp_is_probed) return 0; if (isapnp) return 0; #endif return 1; } static int snd_miro_isa_probe(struct device *devptr, unsigned int n) { static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1}; static const long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1}; static const int possible_irqs[] = {11, 9, 10, 7, -1}; static const int possible_mpu_irqs[] = {10, 5, 9, 7, -1}; static const int possible_dma1s[] = {3, 1, 0, -1}; static const int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1}, {0, -1} }; int error; struct snd_miro *miro; struct snd_card *card; error = snd_devm_card_new(devptr, index, id, THIS_MODULE, sizeof(struct snd_miro), &card); if (error < 0) return error; miro = card->private_data; error = snd_card_miro_detect(card, miro); if (error < 0) { snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n"); return -ENODEV; } if (port == SNDRV_AUTO_PORT) { port = snd_legacy_find_free_ioport(possible_ports, 4); if (port < 0) { snd_printk(KERN_ERR "unable to find a free WSS port\n"); return -EBUSY; } } if (mpu_port == SNDRV_AUTO_PORT) { mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2); if (mpu_port < 0) { snd_printk(KERN_ERR "unable to find a free MPU401 port\n"); return -EBUSY; } } if (irq == SNDRV_AUTO_IRQ) { irq = snd_legacy_find_free_irq(possible_irqs); if (irq < 0) { snd_printk(KERN_ERR "unable to find a free IRQ\n"); return -EBUSY; } } if (mpu_irq == SNDRV_AUTO_IRQ) { mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs); if (mpu_irq < 0) { snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n"); return -EBUSY; } } if (dma1 == SNDRV_AUTO_DMA) { dma1 = snd_legacy_find_free_dma(possible_dma1s); if (dma1 < 0) { snd_printk(KERN_ERR "unable to find a free DMA1\n"); return -EBUSY; } } if (dma2 == SNDRV_AUTO_DMA) { dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]); if (dma2 < 0) { snd_printk(KERN_ERR "unable to find a free DMA2\n"); return -EBUSY; } } error = snd_miro_probe(card); if (error < 0) return error; dev_set_drvdata(devptr, card); return 0; } #define DEV_NAME "miro" static struct isa_driver snd_miro_driver = { .match = snd_miro_isa_match, .probe = snd_miro_isa_probe, /* FIXME: suspend/resume */ .driver = { .name = DEV_NAME }, }; #ifdef CONFIG_PNP static int snd_card_miro_pnp(struct snd_miro *chip, struct pnp_card_link *card, const struct pnp_card_device_id *pid) { struct pnp_dev *pdev; int err; struct pnp_dev *devmpu; struct pnp_dev *devmc; pdev = pnp_request_card_device(card, pid->devs[0].id, NULL); if (pdev == NULL) return -EBUSY; devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL); if (devmpu == NULL) return -EBUSY; devmc = pnp_request_card_device(card, pid->devs[2].id, NULL); if (devmc == NULL) return -EBUSY; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err); return err; } err = pnp_activate_dev(devmc); if (err < 0) { snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err); return err; } port = pnp_port_start(pdev, 1); fm_port = pnp_port_start(pdev, 2) + 8; /* * The MC(0) is never accessed and the miroSOUND PCM20 card does not * include it in the PnP resource range. OPTI93x include it. */ chip->mc_base = pnp_port_start(devmc, 0) - 1; chip->mc_base_size = pnp_port_len(devmc, 0) + 1; irq = pnp_irq(pdev, 0); dma1 = pnp_dma(pdev, 0); dma2 = pnp_dma(pdev, 1); if (mpu_port > 0) { err = pnp_activate_dev(devmpu); if (err < 0) { snd_printk(KERN_ERR "MPU401 pnp configure failure\n"); mpu_port = -1; return err; } mpu_port = pnp_port_start(devmpu, 0); mpu_irq = pnp_irq(devmpu, 0); } return 0; } static int snd_miro_pnp_probe(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { struct snd_card *card; int err; struct snd_miro *miro; if (snd_miro_pnp_is_probed) return -EBUSY; if (!isapnp) return -ENODEV; err = snd_devm_card_new(&pcard->card->dev, index, id, THIS_MODULE, sizeof(struct snd_miro), &card); if (err < 0) return err; miro = card->private_data; err = snd_card_miro_pnp(miro, pcard, pid); if (err) return err; /* only miroSOUND PCM20 and PCM12 == OPTi924 */ err = snd_miro_init(miro, OPTi9XX_HW_82C924); if (err) return err; err = snd_miro_opti_check(card, miro); if (err) { snd_printk(KERN_ERR "OPTI chip not found\n"); return err; } err = snd_miro_probe(card); if (err < 0) return err; pnp_set_card_drvdata(pcard, card); snd_miro_pnp_is_probed = 1; return 0; } static void snd_miro_pnp_remove(struct pnp_card_link *pcard) { snd_miro_pnp_is_probed = 0; } static struct pnp_card_driver miro_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = "miro", .id_table = snd_miro_pnpids, .probe = snd_miro_pnp_probe, .remove = snd_miro_pnp_remove, }; #endif static int __init alsa_card_miro_init(void) { #ifdef CONFIG_PNP pnp_register_card_driver(&miro_pnpc_driver); if (snd_miro_pnp_is_probed) return 0; pnp_unregister_card_driver(&miro_pnpc_driver); #endif return isa_register_driver(&snd_miro_driver, 1); } static void __exit alsa_card_miro_exit(void) { if (!snd_miro_pnp_is_probed) { isa_unregister_driver(&snd_miro_driver); return; } #ifdef CONFIG_PNP pnp_unregister_card_driver(&miro_pnpc_driver); #endif } module_init(alsa_card_miro_init) module_exit(alsa_card_miro_exit)
linux-master
sound/isa/opti9xx/miro.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Aztech AZT2316 Driver * Copyright (C) 2007,2010 Rene Herman */ #define AZT2316 #define CRD_NAME "Aztech AZT2316" #define DRV_NAME "AZT2316" #define DEV_NAME "azt2316" #define GALAXY_DSP_MAJOR 3 #define GALAXY_DSP_MINOR 1 #define GALAXY_CONFIG_SIZE 4 /* * 32-bit config register */ #define GALAXY_CONFIG_SBA_220 (0 << 0) #define GALAXY_CONFIG_SBA_240 (1 << 0) #define GALAXY_CONFIG_SBA_260 (2 << 0) #define GALAXY_CONFIG_SBA_280 (3 << 0) #define GALAXY_CONFIG_SBA_MASK GALAXY_CONFIG_SBA_280 #define GALAXY_CONFIG_SBIRQ_2 (1 << 2) #define GALAXY_CONFIG_SBIRQ_5 (1 << 3) #define GALAXY_CONFIG_SBIRQ_7 (1 << 4) #define GALAXY_CONFIG_SBIRQ_10 (1 << 5) #define GALAXY_CONFIG_SBDMA_DISABLE (0 << 6) #define GALAXY_CONFIG_SBDMA_0 (1 << 6) #define GALAXY_CONFIG_SBDMA_1 (2 << 6) #define GALAXY_CONFIG_SBDMA_3 (3 << 6) #define GALAXY_CONFIG_WSSA_530 (0 << 8) #define GALAXY_CONFIG_WSSA_604 (1 << 8) #define GALAXY_CONFIG_WSSA_E80 (2 << 8) #define GALAXY_CONFIG_WSSA_F40 (3 << 8) #define GALAXY_CONFIG_WSS_ENABLE (1 << 10) #define GALAXY_CONFIG_GAME_ENABLE (1 << 11) #define GALAXY_CONFIG_MPUA_300 (0 << 12) #define GALAXY_CONFIG_MPUA_330 (1 << 12) #define GALAXY_CONFIG_MPU_ENABLE (1 << 13) #define GALAXY_CONFIG_CDA_310 (0 << 14) #define GALAXY_CONFIG_CDA_320 (1 << 14) #define GALAXY_CONFIG_CDA_340 (2 << 14) #define GALAXY_CONFIG_CDA_350 (3 << 14) #define GALAXY_CONFIG_CDA_MASK GALAXY_CONFIG_CDA_350 #define GALAXY_CONFIG_CD_DISABLE (0 << 16) #define GALAXY_CONFIG_CD_PANASONIC (1 << 16) #define GALAXY_CONFIG_CD_SONY (2 << 16) #define GALAXY_CONFIG_CD_MITSUMI (3 << 16) #define GALAXY_CONFIG_CD_AZTECH (4 << 16) #define GALAXY_CONFIG_CD_UNUSED_5 (5 << 16) #define GALAXY_CONFIG_CD_UNUSED_6 (6 << 16) #define GALAXY_CONFIG_CD_UNUSED_7 (7 << 16) #define GALAXY_CONFIG_CD_MASK GALAXY_CONFIG_CD_UNUSED_7 #define GALAXY_CONFIG_CDDMA8_DISABLE (0 << 20) #define GALAXY_CONFIG_CDDMA8_0 (1 << 20) #define GALAXY_CONFIG_CDDMA8_1 (2 << 20) #define GALAXY_CONFIG_CDDMA8_3 (3 << 20) #define GALAXY_CONFIG_CDDMA8_MASK GALAXY_CONFIG_CDDMA8_3 #define GALAXY_CONFIG_CDDMA16_DISABLE (0 << 22) #define GALAXY_CONFIG_CDDMA16_5 (1 << 22) #define GALAXY_CONFIG_CDDMA16_6 (2 << 22) #define GALAXY_CONFIG_CDDMA16_7 (3 << 22) #define GALAXY_CONFIG_CDDMA16_MASK GALAXY_CONFIG_CDDMA16_7 #define GALAXY_CONFIG_MPUIRQ_2 (1 << 24) #define GALAXY_CONFIG_MPUIRQ_5 (1 << 25) #define GALAXY_CONFIG_MPUIRQ_7 (1 << 26) #define GALAXY_CONFIG_MPUIRQ_10 (1 << 27) #define GALAXY_CONFIG_CDIRQ_5 (1 << 28) #define GALAXY_CONFIG_CDIRQ_11 (1 << 29) #define GALAXY_CONFIG_CDIRQ_12 (1 << 30) #define GALAXY_CONFIG_CDIRQ_15 (1 << 31) #define GALAXY_CONFIG_CDIRQ_MASK (\ GALAXY_CONFIG_CDIRQ_5 | GALAXY_CONFIG_CDIRQ_11 |\ GALAXY_CONFIG_CDIRQ_12 | GALAXY_CONFIG_CDIRQ_15) #define GALAXY_CONFIG_MASK (\ GALAXY_CONFIG_SBA_MASK | GALAXY_CONFIG_CDA_MASK |\ GALAXY_CONFIG_CD_MASK | GALAXY_CONFIG_CDDMA16_MASK |\ GALAXY_CONFIG_CDDMA8_MASK | GALAXY_CONFIG_CDIRQ_MASK) #include "galaxy.c"
linux-master
sound/isa/galaxy/azt2316.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Aztech AZT1605 Driver * Copyright (C) 2007,2010 Rene Herman */ #define AZT1605 #define CRD_NAME "Aztech AZT1605" #define DRV_NAME "AZT1605" #define DEV_NAME "azt1605" #define GALAXY_DSP_MAJOR 2 #define GALAXY_DSP_MINOR 1 #define GALAXY_CONFIG_SIZE 3 /* * 24-bit config register */ #define GALAXY_CONFIG_SBA_220 (0 << 0) #define GALAXY_CONFIG_SBA_240 (1 << 0) #define GALAXY_CONFIG_SBA_260 (2 << 0) #define GALAXY_CONFIG_SBA_280 (3 << 0) #define GALAXY_CONFIG_SBA_MASK GALAXY_CONFIG_SBA_280 #define GALAXY_CONFIG_MPUA_300 (0 << 2) #define GALAXY_CONFIG_MPUA_330 (1 << 2) #define GALAXY_CONFIG_MPU_ENABLE (1 << 3) #define GALAXY_CONFIG_GAME_ENABLE (1 << 4) #define GALAXY_CONFIG_CD_PANASONIC (1 << 5) #define GALAXY_CONFIG_CD_MITSUMI (1 << 6) #define GALAXY_CONFIG_CD_MASK (\ GALAXY_CONFIG_CD_PANASONIC | GALAXY_CONFIG_CD_MITSUMI) #define GALAXY_CONFIG_UNUSED (1 << 7) #define GALAXY_CONFIG_UNUSED_MASK GALAXY_CONFIG_UNUSED #define GALAXY_CONFIG_SBIRQ_2 (1 << 8) #define GALAXY_CONFIG_SBIRQ_3 (1 << 9) #define GALAXY_CONFIG_SBIRQ_5 (1 << 10) #define GALAXY_CONFIG_SBIRQ_7 (1 << 11) #define GALAXY_CONFIG_MPUIRQ_2 (1 << 12) #define GALAXY_CONFIG_MPUIRQ_3 (1 << 13) #define GALAXY_CONFIG_MPUIRQ_5 (1 << 14) #define GALAXY_CONFIG_MPUIRQ_7 (1 << 15) #define GALAXY_CONFIG_WSSA_530 (0 << 16) #define GALAXY_CONFIG_WSSA_604 (1 << 16) #define GALAXY_CONFIG_WSSA_E80 (2 << 16) #define GALAXY_CONFIG_WSSA_F40 (3 << 16) #define GALAXY_CONFIG_WSS_ENABLE (1 << 18) #define GALAXY_CONFIG_CDIRQ_11 (1 << 19) #define GALAXY_CONFIG_CDIRQ_12 (1 << 20) #define GALAXY_CONFIG_CDIRQ_15 (1 << 21) #define GALAXY_CONFIG_CDIRQ_MASK (\ GALAXY_CONFIG_CDIRQ_11 | GALAXY_CONFIG_CDIRQ_12 |\ GALAXY_CONFIG_CDIRQ_15) #define GALAXY_CONFIG_CDDMA_DISABLE (0 << 22) #define GALAXY_CONFIG_CDDMA_0 (1 << 22) #define GALAXY_CONFIG_CDDMA_1 (2 << 22) #define GALAXY_CONFIG_CDDMA_3 (3 << 22) #define GALAXY_CONFIG_CDDMA_MASK GALAXY_CONFIG_CDDMA_3 #define GALAXY_CONFIG_MASK (\ GALAXY_CONFIG_SBA_MASK | GALAXY_CONFIG_CD_MASK |\ GALAXY_CONFIG_UNUSED_MASK | GALAXY_CONFIG_CDIRQ_MASK |\ GALAXY_CONFIG_CDDMA_MASK) #include "galaxy.c"
linux-master
sound/isa/galaxy/azt1605.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Aztech AZT1605/AZT2316 Driver * Copyright (C) 2007,2010 Rene Herman */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/io.h> #include <asm/processor.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/wss.h> #include <sound/mpu401.h> #include <sound/opl3.h> MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Rene Herman"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); module_param_hw_array(wss_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(wss_port, "WSS port # for " CRD_NAME " driver."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver."); module_param_hw_array(fm_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(fm_port, "FM port # for " CRD_NAME " driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "Playback DMA # for " CRD_NAME " driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "Capture DMA # for " CRD_NAME " driver."); /* * Generic SB DSP support routines */ #define DSP_PORT_RESET 0x6 #define DSP_PORT_READ 0xa #define DSP_PORT_COMMAND 0xc #define DSP_PORT_STATUS 0xc #define DSP_PORT_DATA_AVAIL 0xe #define DSP_SIGNATURE 0xaa #define DSP_COMMAND_GET_VERSION 0xe1 static int dsp_get_byte(void __iomem *port, u8 *val) { int loops = 1000; while (!(ioread8(port + DSP_PORT_DATA_AVAIL) & 0x80)) { if (!loops--) return -EIO; cpu_relax(); } *val = ioread8(port + DSP_PORT_READ); return 0; } static int dsp_reset(void __iomem *port) { u8 val; iowrite8(1, port + DSP_PORT_RESET); udelay(10); iowrite8(0, port + DSP_PORT_RESET); if (dsp_get_byte(port, &val) < 0 || val != DSP_SIGNATURE) return -ENODEV; return 0; } static int dsp_command(void __iomem *port, u8 cmd) { int loops = 1000; while (ioread8(port + DSP_PORT_STATUS) & 0x80) { if (!loops--) return -EIO; cpu_relax(); } iowrite8(cmd, port + DSP_PORT_COMMAND); return 0; } static int dsp_get_version(void __iomem *port, u8 *major, u8 *minor) { int err; err = dsp_command(port, DSP_COMMAND_GET_VERSION); if (err < 0) return err; err = dsp_get_byte(port, major); if (err < 0) return err; err = dsp_get_byte(port, minor); if (err < 0) return err; return 0; } /* * Generic WSS support routines */ #define WSS_CONFIG_DMA_0 (1 << 0) #define WSS_CONFIG_DMA_1 (2 << 0) #define WSS_CONFIG_DMA_3 (3 << 0) #define WSS_CONFIG_DUPLEX (1 << 2) #define WSS_CONFIG_IRQ_7 (1 << 3) #define WSS_CONFIG_IRQ_9 (2 << 3) #define WSS_CONFIG_IRQ_10 (3 << 3) #define WSS_CONFIG_IRQ_11 (4 << 3) #define WSS_PORT_CONFIG 0 #define WSS_PORT_SIGNATURE 3 #define WSS_SIGNATURE 4 static int wss_detect(void __iomem *wss_port) { if ((ioread8(wss_port + WSS_PORT_SIGNATURE) & 0x3f) != WSS_SIGNATURE) return -ENODEV; return 0; } static void wss_set_config(void __iomem *wss_port, u8 wss_config) { iowrite8(wss_config, wss_port + WSS_PORT_CONFIG); } /* * Aztech Sound Galaxy specifics */ #define GALAXY_PORT_CONFIG 1024 #define CONFIG_PORT_SET 4 #define DSP_COMMAND_GALAXY_8 8 #define GALAXY_COMMAND_GET_TYPE 5 #define DSP_COMMAND_GALAXY_9 9 #define GALAXY_COMMAND_WSSMODE 0 #define GALAXY_COMMAND_SB8MODE 1 #define GALAXY_MODE_WSS GALAXY_COMMAND_WSSMODE #define GALAXY_MODE_SB8 GALAXY_COMMAND_SB8MODE struct snd_galaxy { void __iomem *port; void __iomem *config_port; void __iomem *wss_port; u32 config; struct resource *res_port; struct resource *res_config_port; struct resource *res_wss_port; }; static u32 config[SNDRV_CARDS]; static u8 wss_config[SNDRV_CARDS]; static int snd_galaxy_match(struct device *dev, unsigned int n) { if (!enable[n]) return 0; switch (port[n]) { case SNDRV_AUTO_PORT: dev_err(dev, "please specify port\n"); return 0; case 0x220: config[n] |= GALAXY_CONFIG_SBA_220; break; case 0x240: config[n] |= GALAXY_CONFIG_SBA_240; break; case 0x260: config[n] |= GALAXY_CONFIG_SBA_260; break; case 0x280: config[n] |= GALAXY_CONFIG_SBA_280; break; default: dev_err(dev, "invalid port %#lx\n", port[n]); return 0; } switch (wss_port[n]) { case SNDRV_AUTO_PORT: dev_err(dev, "please specify wss_port\n"); return 0; case 0x530: config[n] |= GALAXY_CONFIG_WSS_ENABLE | GALAXY_CONFIG_WSSA_530; break; case 0x604: config[n] |= GALAXY_CONFIG_WSS_ENABLE | GALAXY_CONFIG_WSSA_604; break; case 0xe80: config[n] |= GALAXY_CONFIG_WSS_ENABLE | GALAXY_CONFIG_WSSA_E80; break; case 0xf40: config[n] |= GALAXY_CONFIG_WSS_ENABLE | GALAXY_CONFIG_WSSA_F40; break; default: dev_err(dev, "invalid WSS port %#lx\n", wss_port[n]); return 0; } switch (irq[n]) { case SNDRV_AUTO_IRQ: dev_err(dev, "please specify irq\n"); return 0; case 7: wss_config[n] |= WSS_CONFIG_IRQ_7; break; case 2: irq[n] = 9; fallthrough; case 9: wss_config[n] |= WSS_CONFIG_IRQ_9; break; case 10: wss_config[n] |= WSS_CONFIG_IRQ_10; break; case 11: wss_config[n] |= WSS_CONFIG_IRQ_11; break; default: dev_err(dev, "invalid IRQ %d\n", irq[n]); return 0; } switch (dma1[n]) { case SNDRV_AUTO_DMA: dev_err(dev, "please specify dma1\n"); return 0; case 0: wss_config[n] |= WSS_CONFIG_DMA_0; break; case 1: wss_config[n] |= WSS_CONFIG_DMA_1; break; case 3: wss_config[n] |= WSS_CONFIG_DMA_3; break; default: dev_err(dev, "invalid playback DMA %d\n", dma1[n]); return 0; } if (dma2[n] == SNDRV_AUTO_DMA || dma2[n] == dma1[n]) { dma2[n] = -1; goto mpu; } wss_config[n] |= WSS_CONFIG_DUPLEX; switch (dma2[n]) { case 0: break; case 1: if (dma1[n] == 0) break; fallthrough; default: dev_err(dev, "invalid capture DMA %d\n", dma2[n]); return 0; } mpu: switch (mpu_port[n]) { case SNDRV_AUTO_PORT: dev_warn(dev, "mpu_port not specified; not using MPU-401\n"); mpu_port[n] = -1; goto fm; case 0x300: config[n] |= GALAXY_CONFIG_MPU_ENABLE | GALAXY_CONFIG_MPUA_300; break; case 0x330: config[n] |= GALAXY_CONFIG_MPU_ENABLE | GALAXY_CONFIG_MPUA_330; break; default: dev_err(dev, "invalid MPU port %#lx\n", mpu_port[n]); return 0; } switch (mpu_irq[n]) { case SNDRV_AUTO_IRQ: dev_warn(dev, "mpu_irq not specified: using polling mode\n"); mpu_irq[n] = -1; break; case 2: mpu_irq[n] = 9; fallthrough; case 9: config[n] |= GALAXY_CONFIG_MPUIRQ_2; break; #ifdef AZT1605 case 3: config[n] |= GALAXY_CONFIG_MPUIRQ_3; break; #endif case 5: config[n] |= GALAXY_CONFIG_MPUIRQ_5; break; case 7: config[n] |= GALAXY_CONFIG_MPUIRQ_7; break; #ifdef AZT2316 case 10: config[n] |= GALAXY_CONFIG_MPUIRQ_10; break; #endif default: dev_err(dev, "invalid MPU IRQ %d\n", mpu_irq[n]); return 0; } if (mpu_irq[n] == irq[n]) { dev_err(dev, "cannot share IRQ between WSS and MPU-401\n"); return 0; } fm: switch (fm_port[n]) { case SNDRV_AUTO_PORT: dev_warn(dev, "fm_port not specified: not using OPL3\n"); fm_port[n] = -1; break; case 0x388: break; default: dev_err(dev, "illegal FM port %#lx\n", fm_port[n]); return 0; } config[n] |= GALAXY_CONFIG_GAME_ENABLE; return 1; } static int galaxy_init(struct snd_galaxy *galaxy, u8 *type) { u8 major; u8 minor; int err; err = dsp_reset(galaxy->port); if (err < 0) return err; err = dsp_get_version(galaxy->port, &major, &minor); if (err < 0) return err; if (major != GALAXY_DSP_MAJOR || minor != GALAXY_DSP_MINOR) return -ENODEV; err = dsp_command(galaxy->port, DSP_COMMAND_GALAXY_8); if (err < 0) return err; err = dsp_command(galaxy->port, GALAXY_COMMAND_GET_TYPE); if (err < 0) return err; err = dsp_get_byte(galaxy->port, type); if (err < 0) return err; return 0; } static int galaxy_set_mode(struct snd_galaxy *galaxy, u8 mode) { int err; err = dsp_command(galaxy->port, DSP_COMMAND_GALAXY_9); if (err < 0) return err; err = dsp_command(galaxy->port, mode); if (err < 0) return err; #ifdef AZT1605 /* * Needed for MPU IRQ on AZT1605, but AZT2316 loses WSS again */ err = dsp_reset(galaxy->port); if (err < 0) return err; #endif return 0; } static void galaxy_set_config(struct snd_galaxy *galaxy, u32 config) { u8 tmp = ioread8(galaxy->config_port + CONFIG_PORT_SET); int i; iowrite8(tmp | 0x80, galaxy->config_port + CONFIG_PORT_SET); for (i = 0; i < GALAXY_CONFIG_SIZE; i++) { iowrite8(config, galaxy->config_port + i); config >>= 8; } iowrite8(tmp & 0x7f, galaxy->config_port + CONFIG_PORT_SET); msleep(10); } static void galaxy_config(struct snd_galaxy *galaxy, u32 config) { int i; for (i = GALAXY_CONFIG_SIZE; i; i--) { u8 tmp = ioread8(galaxy->config_port + i - 1); galaxy->config = (galaxy->config << 8) | tmp; } config |= galaxy->config & GALAXY_CONFIG_MASK; galaxy_set_config(galaxy, config); } static int galaxy_wss_config(struct snd_galaxy *galaxy, u8 wss_config) { int err; err = wss_detect(galaxy->wss_port); if (err < 0) return err; wss_set_config(galaxy->wss_port, wss_config); err = galaxy_set_mode(galaxy, GALAXY_MODE_WSS); if (err < 0) return err; return 0; } static void snd_galaxy_free(struct snd_card *card) { struct snd_galaxy *galaxy = card->private_data; if (galaxy->wss_port) wss_set_config(galaxy->wss_port, 0); if (galaxy->config_port) galaxy_set_config(galaxy, galaxy->config); } static int __snd_galaxy_probe(struct device *dev, unsigned int n) { struct snd_galaxy *galaxy; struct snd_wss *chip; struct snd_card *card; u8 type; int err; err = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, sizeof(*galaxy), &card); if (err < 0) return err; card->private_free = snd_galaxy_free; galaxy = card->private_data; galaxy->res_port = devm_request_region(dev, port[n], 16, DRV_NAME); if (!galaxy->res_port) { dev_err(dev, "could not grab ports %#lx-%#lx\n", port[n], port[n] + 15); return -EBUSY; } galaxy->port = devm_ioport_map(dev, port[n], 16); if (!galaxy->port) return -ENOMEM; err = galaxy_init(galaxy, &type); if (err < 0) { dev_err(dev, "did not find a Sound Galaxy at %#lx\n", port[n]); return err; } dev_info(dev, "Sound Galaxy (type %d) found at %#lx\n", type, port[n]); galaxy->res_config_port = devm_request_region(dev, port[n] + GALAXY_PORT_CONFIG, 16, DRV_NAME); if (!galaxy->res_config_port) { dev_err(dev, "could not grab ports %#lx-%#lx\n", port[n] + GALAXY_PORT_CONFIG, port[n] + GALAXY_PORT_CONFIG + 15); return -EBUSY; } galaxy->config_port = devm_ioport_map(dev, port[n] + GALAXY_PORT_CONFIG, 16); if (!galaxy->config_port) return -ENOMEM; galaxy_config(galaxy, config[n]); galaxy->res_wss_port = devm_request_region(dev, wss_port[n], 4, DRV_NAME); if (!galaxy->res_wss_port) { dev_err(dev, "could not grab ports %#lx-%#lx\n", wss_port[n], wss_port[n] + 3); return -EBUSY; } galaxy->wss_port = devm_ioport_map(dev, wss_port[n], 4); if (!galaxy->wss_port) return -ENOMEM; err = galaxy_wss_config(galaxy, wss_config[n]); if (err < 0) { dev_err(dev, "could not configure WSS\n"); return err; } strcpy(card->driver, DRV_NAME); strcpy(card->shortname, DRV_NAME); sprintf(card->longname, "%s at %#lx/%#lx, irq %d, dma %d/%d", card->shortname, port[n], wss_port[n], irq[n], dma1[n], dma2[n]); err = snd_wss_create(card, wss_port[n] + 4, -1, irq[n], dma1[n], dma2[n], WSS_HW_DETECT, 0, &chip); if (err < 0) return err; err = snd_wss_pcm(chip, 0); if (err < 0) return err; err = snd_wss_mixer(chip); if (err < 0) return err; err = snd_wss_timer(chip, 0); if (err < 0) return err; if (mpu_port[n] >= 0) { err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, mpu_port[n], 0, mpu_irq[n], NULL); if (err < 0) return err; } if (fm_port[n] >= 0) { struct snd_opl3 *opl3; err = snd_opl3_create(card, fm_port[n], fm_port[n] + 2, OPL3_HW_AUTO, 0, &opl3); if (err < 0) { dev_err(dev, "no OPL device at %#lx\n", fm_port[n]); return err; } err = snd_opl3_timer_new(opl3, 1, 2); if (err < 0) return err; err = snd_opl3_hwdep_new(opl3, 0, 1, NULL); if (err < 0) return err; } err = snd_card_register(card); if (err < 0) return err; dev_set_drvdata(dev, card); return 0; } static int snd_galaxy_probe(struct device *dev, unsigned int n) { return snd_card_free_on_error(dev, __snd_galaxy_probe(dev, n)); } static struct isa_driver snd_galaxy_driver = { .match = snd_galaxy_match, .probe = snd_galaxy_probe, .driver = { .name = DEV_NAME } }; module_isa_driver(snd_galaxy_driver, SNDRV_CARDS);
linux-master
sound/isa/galaxy/galaxy.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of CS4231(A)/CS4232/InterWave & compatible chips * * Bugs: * - sometimes record brokes playback with WSS portion of * Yamaha OPL3-SA3 chip * - CS4231 (GUS MAX) - still trouble with occasional noises * - broken initialization? */ #include <linux/delay.h> #include <linux/pm.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/slab.h> #include <linux/ioport.h> #include <linux/module.h> #include <linux/io.h> #include <sound/core.h> #include <sound/wss.h> #include <sound/pcm_params.h> #include <sound/tlv.h> #include <asm/dma.h> #include <asm/irq.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips"); MODULE_LICENSE("GPL"); #if 0 #define SNDRV_DEBUG_MCE #endif /* * Some variables */ static const unsigned char freq_bits[14] = { /* 5510 */ 0x00 | CS4231_XTAL2, /* 6620 */ 0x0E | CS4231_XTAL2, /* 8000 */ 0x00 | CS4231_XTAL1, /* 9600 */ 0x0E | CS4231_XTAL1, /* 11025 */ 0x02 | CS4231_XTAL2, /* 16000 */ 0x02 | CS4231_XTAL1, /* 18900 */ 0x04 | CS4231_XTAL2, /* 22050 */ 0x06 | CS4231_XTAL2, /* 27042 */ 0x04 | CS4231_XTAL1, /* 32000 */ 0x06 | CS4231_XTAL1, /* 33075 */ 0x0C | CS4231_XTAL2, /* 37800 */ 0x08 | CS4231_XTAL2, /* 44100 */ 0x0A | CS4231_XTAL2, /* 48000 */ 0x0C | CS4231_XTAL1 }; static const unsigned int rates[14] = { 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050, 27042, 32000, 33075, 37800, 44100, 48000 }; static const struct snd_pcm_hw_constraint_list hw_constraints_rates = { .count = ARRAY_SIZE(rates), .list = rates, .mask = 0, }; static int snd_wss_xrate(struct snd_pcm_runtime *runtime) { return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates); } static const unsigned char snd_wss_original_image[32] = { 0x00, /* 00/00 - lic */ 0x00, /* 01/01 - ric */ 0x9f, /* 02/02 - la1ic */ 0x9f, /* 03/03 - ra1ic */ 0x9f, /* 04/04 - la2ic */ 0x9f, /* 05/05 - ra2ic */ 0xbf, /* 06/06 - loc */ 0xbf, /* 07/07 - roc */ 0x20, /* 08/08 - pdfr */ CS4231_AUTOCALIB, /* 09/09 - ic */ 0x00, /* 0a/10 - pc */ 0x00, /* 0b/11 - ti */ CS4231_MODE2, /* 0c/12 - mi */ 0xfc, /* 0d/13 - lbc */ 0x00, /* 0e/14 - pbru */ 0x00, /* 0f/15 - pbrl */ 0x80, /* 10/16 - afei */ 0x01, /* 11/17 - afeii */ 0x9f, /* 12/18 - llic */ 0x9f, /* 13/19 - rlic */ 0x00, /* 14/20 - tlb */ 0x00, /* 15/21 - thb */ 0x00, /* 16/22 - la3mic/reserved */ 0x00, /* 17/23 - ra3mic/reserved */ 0x00, /* 18/24 - afs */ 0x00, /* 19/25 - lamoc/version */ 0xcf, /* 1a/26 - mioc */ 0x00, /* 1b/27 - ramoc/reserved */ 0x20, /* 1c/28 - cdfr */ 0x00, /* 1d/29 - res4 */ 0x00, /* 1e/30 - cbru */ 0x00, /* 1f/31 - cbrl */ }; static const unsigned char snd_opti93x_original_image[32] = { 0x00, /* 00/00 - l_mixout_outctrl */ 0x00, /* 01/01 - r_mixout_outctrl */ 0x88, /* 02/02 - l_cd_inctrl */ 0x88, /* 03/03 - r_cd_inctrl */ 0x88, /* 04/04 - l_a1/fm_inctrl */ 0x88, /* 05/05 - r_a1/fm_inctrl */ 0x80, /* 06/06 - l_dac_inctrl */ 0x80, /* 07/07 - r_dac_inctrl */ 0x00, /* 08/08 - ply_dataform_reg */ 0x00, /* 09/09 - if_conf */ 0x00, /* 0a/10 - pin_ctrl */ 0x00, /* 0b/11 - err_init_reg */ 0x0a, /* 0c/12 - id_reg */ 0x00, /* 0d/13 - reserved */ 0x00, /* 0e/14 - ply_upcount_reg */ 0x00, /* 0f/15 - ply_lowcount_reg */ 0x88, /* 10/16 - reserved/l_a1_inctrl */ 0x88, /* 11/17 - reserved/r_a1_inctrl */ 0x88, /* 12/18 - l_line_inctrl */ 0x88, /* 13/19 - r_line_inctrl */ 0x88, /* 14/20 - l_mic_inctrl */ 0x88, /* 15/21 - r_mic_inctrl */ 0x80, /* 16/22 - l_out_outctrl */ 0x80, /* 17/23 - r_out_outctrl */ 0x00, /* 18/24 - reserved */ 0x00, /* 19/25 - reserved */ 0x00, /* 1a/26 - reserved */ 0x00, /* 1b/27 - reserved */ 0x00, /* 1c/28 - cap_dataform_reg */ 0x00, /* 1d/29 - reserved */ 0x00, /* 1e/30 - cap_upcount_reg */ 0x00 /* 1f/31 - cap_lowcount_reg */ }; /* * Basic I/O functions */ static inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val) { outb(val, chip->port + offset); } static inline u8 wss_inb(struct snd_wss *chip, u8 offset) { return inb(chip->port + offset); } static void snd_wss_wait(struct snd_wss *chip) { int timeout; for (timeout = 250; timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT); timeout--) udelay(100); } static void snd_wss_dout(struct snd_wss *chip, unsigned char reg, unsigned char value) { int timeout; for (timeout = 250; timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT); timeout--) udelay(10); wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg); wss_outb(chip, CS4231P(REG), value); mb(); } void snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value) { snd_wss_wait(chip); #ifdef CONFIG_SND_DEBUG if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) snd_printk(KERN_DEBUG "out: auto calibration time out " "- reg = 0x%x, value = 0x%x\n", reg, value); #endif wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg); wss_outb(chip, CS4231P(REG), value); chip->image[reg] = value; mb(); snd_printdd("codec out - reg 0x%x = 0x%x\n", chip->mce_bit | reg, value); } EXPORT_SYMBOL(snd_wss_out); unsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg) { snd_wss_wait(chip); #ifdef CONFIG_SND_DEBUG if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) snd_printk(KERN_DEBUG "in: auto calibration time out " "- reg = 0x%x\n", reg); #endif wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg); mb(); return wss_inb(chip, CS4231P(REG)); } EXPORT_SYMBOL(snd_wss_in); void snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg, unsigned char val) { wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17); wss_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01)); wss_outb(chip, CS4231P(REG), val); chip->eimage[CS4236_REG(reg)] = val; #if 0 printk(KERN_DEBUG "ext out : reg = 0x%x, val = 0x%x\n", reg, val); #endif } EXPORT_SYMBOL(snd_cs4236_ext_out); unsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg) { wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17); wss_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01)); #if 1 return wss_inb(chip, CS4231P(REG)); #else { unsigned char res; res = wss_inb(chip, CS4231P(REG)); printk(KERN_DEBUG "ext in : reg = 0x%x, val = 0x%x\n", reg, res); return res; } #endif } EXPORT_SYMBOL(snd_cs4236_ext_in); #if 0 static void snd_wss_debug(struct snd_wss *chip) { printk(KERN_DEBUG "CS4231 REGS: INDEX = 0x%02x " " STATUS = 0x%02x\n", wss_inb(chip, CS4231P(REGSEL)), wss_inb(chip, CS4231P(STATUS))); printk(KERN_DEBUG " 0x00: left input = 0x%02x " " 0x10: alt 1 (CFIG 2) = 0x%02x\n", snd_wss_in(chip, 0x00), snd_wss_in(chip, 0x10)); printk(KERN_DEBUG " 0x01: right input = 0x%02x " " 0x11: alt 2 (CFIG 3) = 0x%02x\n", snd_wss_in(chip, 0x01), snd_wss_in(chip, 0x11)); printk(KERN_DEBUG " 0x02: GF1 left input = 0x%02x " " 0x12: left line in = 0x%02x\n", snd_wss_in(chip, 0x02), snd_wss_in(chip, 0x12)); printk(KERN_DEBUG " 0x03: GF1 right input = 0x%02x " " 0x13: right line in = 0x%02x\n", snd_wss_in(chip, 0x03), snd_wss_in(chip, 0x13)); printk(KERN_DEBUG " 0x04: CD left input = 0x%02x " " 0x14: timer low = 0x%02x\n", snd_wss_in(chip, 0x04), snd_wss_in(chip, 0x14)); printk(KERN_DEBUG " 0x05: CD right input = 0x%02x " " 0x15: timer high = 0x%02x\n", snd_wss_in(chip, 0x05), snd_wss_in(chip, 0x15)); printk(KERN_DEBUG " 0x06: left output = 0x%02x " " 0x16: left MIC (PnP) = 0x%02x\n", snd_wss_in(chip, 0x06), snd_wss_in(chip, 0x16)); printk(KERN_DEBUG " 0x07: right output = 0x%02x " " 0x17: right MIC (PnP) = 0x%02x\n", snd_wss_in(chip, 0x07), snd_wss_in(chip, 0x17)); printk(KERN_DEBUG " 0x08: playback format = 0x%02x " " 0x18: IRQ status = 0x%02x\n", snd_wss_in(chip, 0x08), snd_wss_in(chip, 0x18)); printk(KERN_DEBUG " 0x09: iface (CFIG 1) = 0x%02x " " 0x19: left line out = 0x%02x\n", snd_wss_in(chip, 0x09), snd_wss_in(chip, 0x19)); printk(KERN_DEBUG " 0x0a: pin control = 0x%02x " " 0x1a: mono control = 0x%02x\n", snd_wss_in(chip, 0x0a), snd_wss_in(chip, 0x1a)); printk(KERN_DEBUG " 0x0b: init & status = 0x%02x " " 0x1b: right line out = 0x%02x\n", snd_wss_in(chip, 0x0b), snd_wss_in(chip, 0x1b)); printk(KERN_DEBUG " 0x0c: revision & mode = 0x%02x " " 0x1c: record format = 0x%02x\n", snd_wss_in(chip, 0x0c), snd_wss_in(chip, 0x1c)); printk(KERN_DEBUG " 0x0d: loopback = 0x%02x " " 0x1d: var freq (PnP) = 0x%02x\n", snd_wss_in(chip, 0x0d), snd_wss_in(chip, 0x1d)); printk(KERN_DEBUG " 0x0e: ply upr count = 0x%02x " " 0x1e: ply lwr count = 0x%02x\n", snd_wss_in(chip, 0x0e), snd_wss_in(chip, 0x1e)); printk(KERN_DEBUG " 0x0f: rec upr count = 0x%02x " " 0x1f: rec lwr count = 0x%02x\n", snd_wss_in(chip, 0x0f), snd_wss_in(chip, 0x1f)); } #endif /* * CS4231 detection / MCE routines */ static void snd_wss_busy_wait(struct snd_wss *chip) { int timeout; /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */ for (timeout = 5; timeout > 0; timeout--) wss_inb(chip, CS4231P(REGSEL)); /* end of cleanup sequence */ for (timeout = 25000; timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT); timeout--) udelay(10); } void snd_wss_mce_up(struct snd_wss *chip) { unsigned long flags; int timeout; snd_wss_wait(chip); #ifdef CONFIG_SND_DEBUG if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) snd_printk(KERN_DEBUG "mce_up - auto calibration time out (0)\n"); #endif spin_lock_irqsave(&chip->reg_lock, flags); chip->mce_bit |= CS4231_MCE; timeout = wss_inb(chip, CS4231P(REGSEL)); if (timeout == 0x80) snd_printk(KERN_DEBUG "mce_up [0x%lx]: " "serious init problem - codec still busy\n", chip->port); if (!(timeout & CS4231_MCE)) wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f)); spin_unlock_irqrestore(&chip->reg_lock, flags); } EXPORT_SYMBOL(snd_wss_mce_up); void snd_wss_mce_down(struct snd_wss *chip) { unsigned long flags; unsigned long end_time; int timeout; int hw_mask = WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK | WSS_HW_AD1848; snd_wss_busy_wait(chip); #ifdef CONFIG_SND_DEBUG if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) snd_printk(KERN_DEBUG "mce_down [0x%lx] - " "auto calibration time out (0)\n", (long)CS4231P(REGSEL)); #endif spin_lock_irqsave(&chip->reg_lock, flags); chip->mce_bit &= ~CS4231_MCE; timeout = wss_inb(chip, CS4231P(REGSEL)); wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f)); spin_unlock_irqrestore(&chip->reg_lock, flags); if (timeout == 0x80) snd_printk(KERN_DEBUG "mce_down [0x%lx]: " "serious init problem - codec still busy\n", chip->port); if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & hw_mask)) return; /* * Wait for (possible -- during init auto-calibration may not be set) * calibration process to start. Needs up to 5 sample periods on AD1848 * which at the slowest possible rate of 5.5125 kHz means 907 us. */ msleep(1); snd_printdd("(1) jiffies = %lu\n", jiffies); /* check condition up to 250 ms */ end_time = jiffies + msecs_to_jiffies(250); while (snd_wss_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) { if (time_after(jiffies, end_time)) { snd_printk(KERN_ERR "mce_down - " "auto calibration time out (2)\n"); return; } msleep(1); } snd_printdd("(2) jiffies = %lu\n", jiffies); /* check condition up to 100 ms */ end_time = jiffies + msecs_to_jiffies(100); while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) { if (time_after(jiffies, end_time)) { snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n"); return; } msleep(1); } snd_printdd("(3) jiffies = %lu\n", jiffies); snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL))); } EXPORT_SYMBOL(snd_wss_mce_down); static unsigned int snd_wss_get_count(unsigned char format, unsigned int size) { switch (format & 0xe0) { case CS4231_LINEAR_16: case CS4231_LINEAR_16_BIG: size >>= 1; break; case CS4231_ADPCM_16: return size >> 2; } if (format & CS4231_STEREO) size >>= 1; return size; } static int snd_wss_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_wss *chip = snd_pcm_substream_chip(substream); int result = 0; unsigned int what; struct snd_pcm_substream *s; int do_start; switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: do_start = 1; break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: do_start = 0; break; default: return -EINVAL; } what = 0; snd_pcm_group_for_each_entry(s, substream) { if (s == chip->playback_substream) { what |= CS4231_PLAYBACK_ENABLE; snd_pcm_trigger_done(s, substream); } else if (s == chip->capture_substream) { what |= CS4231_RECORD_ENABLE; snd_pcm_trigger_done(s, substream); } } spin_lock(&chip->reg_lock); if (do_start) { chip->image[CS4231_IFACE_CTRL] |= what; if (chip->trigger) chip->trigger(chip, what, 1); } else { chip->image[CS4231_IFACE_CTRL] &= ~what; if (chip->trigger) chip->trigger(chip, what, 0); } snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]); spin_unlock(&chip->reg_lock); #if 0 snd_wss_debug(chip); #endif return result; } /* * CODEC I/O */ static unsigned char snd_wss_get_rate(unsigned int rate) { int i; for (i = 0; i < ARRAY_SIZE(rates); i++) if (rate == rates[i]) return freq_bits[i]; // snd_BUG(); return freq_bits[ARRAY_SIZE(rates) - 1]; } static unsigned char snd_wss_get_format(struct snd_wss *chip, snd_pcm_format_t format, int channels) { unsigned char rformat; rformat = CS4231_LINEAR_8; switch (format) { case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break; case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break; case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break; case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break; case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break; } if (channels > 1) rformat |= CS4231_STEREO; #if 0 snd_printk(KERN_DEBUG "get_format: 0x%x (mode=0x%x)\n", format, mode); #endif return rformat; } static void snd_wss_calibrate_mute(struct snd_wss *chip, int mute) { unsigned long flags; mute = mute ? 0x80 : 0; spin_lock_irqsave(&chip->reg_lock, flags); if (chip->calibrate_mute == mute) { spin_unlock_irqrestore(&chip->reg_lock, flags); return; } if (!mute) { snd_wss_dout(chip, CS4231_LEFT_INPUT, chip->image[CS4231_LEFT_INPUT]); snd_wss_dout(chip, CS4231_RIGHT_INPUT, chip->image[CS4231_RIGHT_INPUT]); snd_wss_dout(chip, CS4231_LOOPBACK, chip->image[CS4231_LOOPBACK]); } else { snd_wss_dout(chip, CS4231_LEFT_INPUT, 0); snd_wss_dout(chip, CS4231_RIGHT_INPUT, 0); snd_wss_dout(chip, CS4231_LOOPBACK, 0xfd); } snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT, mute | chip->image[CS4231_AUX1_LEFT_INPUT]); snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT, mute | chip->image[CS4231_AUX1_RIGHT_INPUT]); snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT, mute | chip->image[CS4231_AUX2_LEFT_INPUT]); snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT, mute | chip->image[CS4231_AUX2_RIGHT_INPUT]); snd_wss_dout(chip, CS4231_LEFT_OUTPUT, mute | chip->image[CS4231_LEFT_OUTPUT]); snd_wss_dout(chip, CS4231_RIGHT_OUTPUT, mute | chip->image[CS4231_RIGHT_OUTPUT]); if (!(chip->hardware & WSS_HW_AD1848_MASK)) { snd_wss_dout(chip, CS4231_LEFT_LINE_IN, mute | chip->image[CS4231_LEFT_LINE_IN]); snd_wss_dout(chip, CS4231_RIGHT_LINE_IN, mute | chip->image[CS4231_RIGHT_LINE_IN]); snd_wss_dout(chip, CS4231_MONO_CTRL, mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]); } if (chip->hardware == WSS_HW_INTERWAVE) { snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT, mute | chip->image[CS4231_LEFT_MIC_INPUT]); snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT, mute | chip->image[CS4231_RIGHT_MIC_INPUT]); snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT, mute | chip->image[CS4231_LINE_LEFT_OUTPUT]); snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT, mute | chip->image[CS4231_LINE_RIGHT_OUTPUT]); } chip->calibrate_mute = mute; spin_unlock_irqrestore(&chip->reg_lock, flags); } static void snd_wss_playback_format(struct snd_wss *chip, struct snd_pcm_hw_params *params, unsigned char pdfr) { unsigned long flags; int full_calib = 1; mutex_lock(&chip->mce_mutex); if (chip->hardware == WSS_HW_CS4231A || (chip->hardware & WSS_HW_CS4232_MASK)) { spin_lock_irqsave(&chip->reg_lock, flags); if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */ snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10); chip->image[CS4231_PLAYBK_FORMAT] = pdfr; snd_wss_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]); snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x10); udelay(100); /* Fixes audible clicks at least on GUS MAX */ full_calib = 0; } spin_unlock_irqrestore(&chip->reg_lock, flags); } else if (chip->hardware == WSS_HW_AD1845) { unsigned rate = params_rate(params); /* * Program the AD1845 correctly for the playback stream. * Note that we do NOT need to toggle the MCE bit because * the PLAYBACK_ENABLE bit of the Interface Configuration * register is set. * * NOTE: We seem to need to write to the MSB before the LSB * to get the correct sample frequency. */ spin_lock_irqsave(&chip->reg_lock, flags); snd_wss_out(chip, CS4231_PLAYBK_FORMAT, (pdfr & 0xf0)); snd_wss_out(chip, AD1845_UPR_FREQ_SEL, (rate >> 8) & 0xff); snd_wss_out(chip, AD1845_LWR_FREQ_SEL, rate & 0xff); full_calib = 0; spin_unlock_irqrestore(&chip->reg_lock, flags); } if (full_calib) { snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) { if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) pdfr = (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f); } else { chip->image[CS4231_PLAYBK_FORMAT] = pdfr; } snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr); spin_unlock_irqrestore(&chip->reg_lock, flags); if (chip->hardware == WSS_HW_OPL3SA2) udelay(100); /* this seems to help */ snd_wss_mce_down(chip); } mutex_unlock(&chip->mce_mutex); } static void snd_wss_capture_format(struct snd_wss *chip, struct snd_pcm_hw_params *params, unsigned char cdfr) { unsigned long flags; int full_calib = 1; mutex_lock(&chip->mce_mutex); if (chip->hardware == WSS_HW_CS4231A || (chip->hardware & WSS_HW_CS4232_MASK)) { spin_lock_irqsave(&chip->reg_lock, flags); if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */ (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) { snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20); snd_wss_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT] = cdfr); snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x20); full_calib = 0; } spin_unlock_irqrestore(&chip->reg_lock, flags); } else if (chip->hardware == WSS_HW_AD1845) { unsigned rate = params_rate(params); /* * Program the AD1845 correctly for the capture stream. * Note that we do NOT need to toggle the MCE bit because * the PLAYBACK_ENABLE bit of the Interface Configuration * register is set. * * NOTE: We seem to need to write to the MSB before the LSB * to get the correct sample frequency. */ spin_lock_irqsave(&chip->reg_lock, flags); snd_wss_out(chip, CS4231_REC_FORMAT, (cdfr & 0xf0)); snd_wss_out(chip, AD1845_UPR_FREQ_SEL, (rate >> 8) & 0xff); snd_wss_out(chip, AD1845_LWR_FREQ_SEL, rate & 0xff); full_calib = 0; spin_unlock_irqrestore(&chip->reg_lock, flags); } if (full_calib) { snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); if (chip->hardware != WSS_HW_INTERWAVE && !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) { if (chip->single_dma) snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr); else snd_wss_out(chip, CS4231_PLAYBK_FORMAT, (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) | (cdfr & 0x0f)); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); } if (chip->hardware & WSS_HW_AD1848_MASK) snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr); else snd_wss_out(chip, CS4231_REC_FORMAT, cdfr); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); } mutex_unlock(&chip->mce_mutex); } /* * Timer interface */ static unsigned long snd_wss_timer_resolution(struct snd_timer *timer) { struct snd_wss *chip = snd_timer_chip(timer); if (chip->hardware & WSS_HW_CS4236B_MASK) return 14467; else return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920; } static int snd_wss_timer_start(struct snd_timer *timer) { unsigned long flags; unsigned int ticks; struct snd_wss *chip = snd_timer_chip(timer); spin_lock_irqsave(&chip->reg_lock, flags); ticks = timer->sticks; if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 || (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] || (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) { chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8); snd_wss_out(chip, CS4231_TIMER_HIGH, chip->image[CS4231_TIMER_HIGH]); chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks; snd_wss_out(chip, CS4231_TIMER_LOW, chip->image[CS4231_TIMER_LOW]); snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE); } spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_wss_timer_stop(struct snd_timer *timer) { unsigned long flags; struct snd_wss *chip = snd_timer_chip(timer); spin_lock_irqsave(&chip->reg_lock, flags); chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE; snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]); spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static void snd_wss_init(struct snd_wss *chip) { unsigned long flags; snd_wss_calibrate_mute(chip, 1); snd_wss_mce_down(chip); #ifdef SNDRV_DEBUG_MCE snd_printk(KERN_DEBUG "init: (1)\n"); #endif snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO | CS4231_RECORD_ENABLE | CS4231_RECORD_PIO | CS4231_CALIB_MODE); chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB; snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); #ifdef SNDRV_DEBUG_MCE snd_printk(KERN_DEBUG "init: (2)\n"); #endif snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); chip->image[CS4231_IFACE_CTRL] &= ~CS4231_AUTOCALIB; snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]); snd_wss_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); #ifdef SNDRV_DEBUG_MCE snd_printk(KERN_DEBUG "init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]); #endif spin_lock_irqsave(&chip->reg_lock, flags); snd_wss_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); snd_wss_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); #ifdef SNDRV_DEBUG_MCE snd_printk(KERN_DEBUG "init: (4)\n"); #endif snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); if (!(chip->hardware & WSS_HW_AD1848_MASK)) snd_wss_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); snd_wss_calibrate_mute(chip, 0); #ifdef SNDRV_DEBUG_MCE snd_printk(KERN_DEBUG "init: (5)\n"); #endif } static int snd_wss_open(struct snd_wss *chip, unsigned int mode) { unsigned long flags; mutex_lock(&chip->open_mutex); if ((chip->mode & mode) || ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) { mutex_unlock(&chip->open_mutex); return -EAGAIN; } if (chip->mode & WSS_MODE_OPEN) { chip->mode |= mode; mutex_unlock(&chip->open_mutex); return 0; } /* ok. now enable and ack CODEC IRQ */ spin_lock_irqsave(&chip->reg_lock, flags); if (!(chip->hardware & WSS_HW_AD1848_MASK)) { snd_wss_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ | CS4231_RECORD_IRQ | CS4231_TIMER_IRQ); snd_wss_out(chip, CS4231_IRQ_STATUS, 0); } wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */ wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */ chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE; snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]); if (!(chip->hardware & WSS_HW_AD1848_MASK)) { snd_wss_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ | CS4231_RECORD_IRQ | CS4231_TIMER_IRQ); snd_wss_out(chip, CS4231_IRQ_STATUS, 0); } spin_unlock_irqrestore(&chip->reg_lock, flags); chip->mode = mode; mutex_unlock(&chip->open_mutex); return 0; } static void snd_wss_close(struct snd_wss *chip, unsigned int mode) { unsigned long flags; mutex_lock(&chip->open_mutex); chip->mode &= ~mode; if (chip->mode & WSS_MODE_OPEN) { mutex_unlock(&chip->open_mutex); return; } /* disable IRQ */ spin_lock_irqsave(&chip->reg_lock, flags); if (!(chip->hardware & WSS_HW_AD1848_MASK)) snd_wss_out(chip, CS4231_IRQ_STATUS, 0); wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */ wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */ chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE; snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]); /* now disable record & playback */ if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO | CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) { spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO | CS4231_RECORD_ENABLE | CS4231_RECORD_PIO); snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_down(chip); spin_lock_irqsave(&chip->reg_lock, flags); } /* clear IRQ again */ if (!(chip->hardware & WSS_HW_AD1848_MASK)) snd_wss_out(chip, CS4231_IRQ_STATUS, 0); wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */ wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */ spin_unlock_irqrestore(&chip->reg_lock, flags); chip->mode = 0; mutex_unlock(&chip->open_mutex); } /* * timer open/close */ static int snd_wss_timer_open(struct snd_timer *timer) { struct snd_wss *chip = snd_timer_chip(timer); snd_wss_open(chip, WSS_MODE_TIMER); return 0; } static int snd_wss_timer_close(struct snd_timer *timer) { struct snd_wss *chip = snd_timer_chip(timer); snd_wss_close(chip, WSS_MODE_TIMER); return 0; } static const struct snd_timer_hardware snd_wss_timer_table = { .flags = SNDRV_TIMER_HW_AUTO, .resolution = 9945, .ticks = 65535, .open = snd_wss_timer_open, .close = snd_wss_timer_close, .c_resolution = snd_wss_timer_resolution, .start = snd_wss_timer_start, .stop = snd_wss_timer_stop, }; /* * ok.. exported functions.. */ static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_wss *chip = snd_pcm_substream_chip(substream); unsigned char new_pdfr; new_pdfr = snd_wss_get_format(chip, params_format(hw_params), params_channels(hw_params)) | snd_wss_get_rate(params_rate(hw_params)); chip->set_playback_format(chip, hw_params, new_pdfr); return 0; } static int snd_wss_playback_prepare(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned long flags; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); spin_lock_irqsave(&chip->reg_lock, flags); chip->p_dma_size = size; chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO); snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1; snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count); snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8)); spin_unlock_irqrestore(&chip->reg_lock, flags); #if 0 snd_wss_debug(chip); #endif return 0; } static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_wss *chip = snd_pcm_substream_chip(substream); unsigned char new_cdfr; new_cdfr = snd_wss_get_format(chip, params_format(hw_params), params_channels(hw_params)) | snd_wss_get_rate(params_rate(hw_params)); chip->set_capture_format(chip, hw_params, new_cdfr); return 0; } static int snd_wss_capture_prepare(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; unsigned long flags; unsigned int size = snd_pcm_lib_buffer_bytes(substream); unsigned int count = snd_pcm_lib_period_bytes(substream); spin_lock_irqsave(&chip->reg_lock, flags); chip->c_dma_size = size; chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO); snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); if (chip->hardware & WSS_HW_AD1848_MASK) count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count); else count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT], count); count--; if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) { snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count); snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8)); } else { snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count); snd_wss_out(chip, CS4231_REC_UPR_CNT, (unsigned char) (count >> 8)); } spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } void snd_wss_overrange(struct snd_wss *chip) { unsigned long flags; unsigned char res; spin_lock_irqsave(&chip->reg_lock, flags); res = snd_wss_in(chip, CS4231_TEST_INIT); spin_unlock_irqrestore(&chip->reg_lock, flags); if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */ chip->capture_substream->runtime->overrange++; } EXPORT_SYMBOL(snd_wss_overrange); irqreturn_t snd_wss_interrupt(int irq, void *dev_id) { struct snd_wss *chip = dev_id; unsigned char status; if (chip->hardware & WSS_HW_AD1848_MASK) /* pretend it was the only possible irq for AD1848 */ status = CS4231_PLAYBACK_IRQ; else status = snd_wss_in(chip, CS4231_IRQ_STATUS); if (status & CS4231_TIMER_IRQ) { if (chip->timer) snd_timer_interrupt(chip->timer, chip->timer->sticks); } if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) { if (status & CS4231_PLAYBACK_IRQ) { if (chip->mode & WSS_MODE_PLAY) { if (chip->playback_substream) snd_pcm_period_elapsed(chip->playback_substream); } if (chip->mode & WSS_MODE_RECORD) { if (chip->capture_substream) { snd_wss_overrange(chip); snd_pcm_period_elapsed(chip->capture_substream); } } } } else { if (status & CS4231_PLAYBACK_IRQ) { if (chip->playback_substream) snd_pcm_period_elapsed(chip->playback_substream); } if (status & CS4231_RECORD_IRQ) { if (chip->capture_substream) { snd_wss_overrange(chip); snd_pcm_period_elapsed(chip->capture_substream); } } } spin_lock(&chip->reg_lock); status = ~CS4231_ALL_IRQS | ~status; if (chip->hardware & WSS_HW_AD1848_MASK) wss_outb(chip, CS4231P(STATUS), 0); else snd_wss_out(chip, CS4231_IRQ_STATUS, status); spin_unlock(&chip->reg_lock); return IRQ_HANDLED; } EXPORT_SYMBOL(snd_wss_interrupt); static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); size_t ptr; if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) return 0; ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size); return bytes_to_frames(substream->runtime, ptr); } static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); size_t ptr; if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)) return 0; ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size); return bytes_to_frames(substream->runtime, ptr); } /* */ static int snd_ad1848_probe(struct snd_wss *chip) { unsigned long timeout = jiffies + msecs_to_jiffies(1000); unsigned long flags; unsigned char r; unsigned short hardware = 0; int err = 0; int i; while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) { if (time_after(jiffies, timeout)) return -ENODEV; cond_resched(); } spin_lock_irqsave(&chip->reg_lock, flags); /* set CS423x MODE 1 */ snd_wss_dout(chip, CS4231_MISC_INFO, 0); snd_wss_dout(chip, CS4231_RIGHT_INPUT, 0x45); /* 0x55 & ~0x10 */ r = snd_wss_in(chip, CS4231_RIGHT_INPUT); if (r != 0x45) { /* RMGE always high on AD1847 */ if ((r & ~CS4231_ENABLE_MIC_GAIN) != 0x45) { err = -ENODEV; goto out; } hardware = WSS_HW_AD1847; } else { snd_wss_dout(chip, CS4231_LEFT_INPUT, 0xaa); r = snd_wss_in(chip, CS4231_LEFT_INPUT); /* L/RMGE always low on AT2320 */ if ((r | CS4231_ENABLE_MIC_GAIN) != 0xaa) { err = -ENODEV; goto out; } } /* clear pending IRQ */ wss_inb(chip, CS4231P(STATUS)); wss_outb(chip, CS4231P(STATUS), 0); mb(); if ((chip->hardware & WSS_HW_TYPE_MASK) != WSS_HW_DETECT) goto out; if (hardware) { chip->hardware = hardware; goto out; } r = snd_wss_in(chip, CS4231_MISC_INFO); /* set CS423x MODE 2 */ snd_wss_dout(chip, CS4231_MISC_INFO, CS4231_MODE2); for (i = 0; i < 16; i++) { if (snd_wss_in(chip, i) != snd_wss_in(chip, 16 + i)) { /* we have more than 16 registers: check ID */ if ((r & 0xf) != 0xa) goto out_mode; /* * on CMI8330, CS4231_VERSION is volume control and * can be set to 0 */ snd_wss_dout(chip, CS4231_VERSION, 0); r = snd_wss_in(chip, CS4231_VERSION) & 0xe7; if (!r) chip->hardware = WSS_HW_CMI8330; goto out_mode; } } if (r & 0x80) chip->hardware = WSS_HW_CS4248; else chip->hardware = WSS_HW_AD1848; out_mode: snd_wss_dout(chip, CS4231_MISC_INFO, 0); out: spin_unlock_irqrestore(&chip->reg_lock, flags); return err; } static int snd_wss_probe(struct snd_wss *chip) { unsigned long flags; int i, id, rev, regnum; unsigned char *ptr; unsigned int hw; id = snd_ad1848_probe(chip); if (id < 0) return id; hw = chip->hardware; if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) { for (i = 0; i < 50; i++) { mb(); if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) msleep(2); else { spin_lock_irqsave(&chip->reg_lock, flags); snd_wss_out(chip, CS4231_MISC_INFO, CS4231_MODE2); id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f; spin_unlock_irqrestore(&chip->reg_lock, flags); if (id == 0x0a) break; /* this is valid value */ } } snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id); if (id != 0x0a) return -ENODEV; /* no valid device found */ rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7; snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev); if (rev == 0x80) { unsigned char tmp = snd_wss_in(chip, 23); snd_wss_out(chip, 23, ~tmp); if (snd_wss_in(chip, 23) != tmp) chip->hardware = WSS_HW_AD1845; else chip->hardware = WSS_HW_CS4231; } else if (rev == 0xa0) { chip->hardware = WSS_HW_CS4231A; } else if (rev == 0xa2) { chip->hardware = WSS_HW_CS4232; } else if (rev == 0xb2) { chip->hardware = WSS_HW_CS4232A; } else if (rev == 0x83) { chip->hardware = WSS_HW_CS4236; } else if (rev == 0x03) { chip->hardware = WSS_HW_CS4236B; } else { snd_printk(KERN_ERR "unknown CS chip with version 0x%x\n", rev); return -ENODEV; /* unknown CS4231 chip? */ } } spin_lock_irqsave(&chip->reg_lock, flags); wss_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */ wss_outb(chip, CS4231P(STATUS), 0); mb(); spin_unlock_irqrestore(&chip->reg_lock, flags); if (!(chip->hardware & WSS_HW_AD1848_MASK)) chip->image[CS4231_MISC_INFO] = CS4231_MODE2; switch (chip->hardware) { case WSS_HW_INTERWAVE: chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3; break; case WSS_HW_CS4235: case WSS_HW_CS4236B: case WSS_HW_CS4237B: case WSS_HW_CS4238B: case WSS_HW_CS4239: if (hw == WSS_HW_DETECT3) chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3; else chip->hardware = WSS_HW_CS4236; break; } chip->image[CS4231_IFACE_CTRL] = (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) | (chip->single_dma ? CS4231_SINGLE_DMA : 0); if (chip->hardware != WSS_HW_OPTI93X) { chip->image[CS4231_ALT_FEATURE_1] = 0x80; chip->image[CS4231_ALT_FEATURE_2] = chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01; } /* enable fine grained frequency selection */ if (chip->hardware == WSS_HW_AD1845) chip->image[AD1845_PWR_DOWN] = 8; ptr = (unsigned char *) &chip->image; regnum = (chip->hardware & WSS_HW_AD1848_MASK) ? 16 : 32; snd_wss_mce_down(chip); spin_lock_irqsave(&chip->reg_lock, flags); for (i = 0; i < regnum; i++) /* ok.. fill all registers */ snd_wss_out(chip, i, *ptr++); spin_unlock_irqrestore(&chip->reg_lock, flags); snd_wss_mce_up(chip); snd_wss_mce_down(chip); mdelay(2); /* ok.. try check hardware version for CS4236+ chips */ if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) { if (chip->hardware == WSS_HW_CS4236B) { rev = snd_cs4236_ext_in(chip, CS4236_VERSION); snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff); id = snd_cs4236_ext_in(chip, CS4236_VERSION); snd_cs4236_ext_out(chip, CS4236_VERSION, rev); snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id); if ((id & 0x1f) == 0x1d) { /* CS4235 */ chip->hardware = WSS_HW_CS4235; switch (id >> 5) { case 4: case 5: case 6: break; default: snd_printk(KERN_WARNING "unknown CS4235 chip " "(enhanced version = 0x%x)\n", id); } } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */ switch (id >> 5) { case 4: case 5: case 6: case 7: chip->hardware = WSS_HW_CS4236B; break; default: snd_printk(KERN_WARNING "unknown CS4236 chip " "(enhanced version = 0x%x)\n", id); } } else if ((id & 0x1f) == 0x08) { /* CS4237B */ chip->hardware = WSS_HW_CS4237B; switch (id >> 5) { case 4: case 5: case 6: case 7: break; default: snd_printk(KERN_WARNING "unknown CS4237B chip " "(enhanced version = 0x%x)\n", id); } } else if ((id & 0x1f) == 0x09) { /* CS4238B */ chip->hardware = WSS_HW_CS4238B; switch (id >> 5) { case 5: case 6: case 7: break; default: snd_printk(KERN_WARNING "unknown CS4238B chip " "(enhanced version = 0x%x)\n", id); } } else if ((id & 0x1f) == 0x1e) { /* CS4239 */ chip->hardware = WSS_HW_CS4239; switch (id >> 5) { case 4: case 5: case 6: break; default: snd_printk(KERN_WARNING "unknown CS4239 chip " "(enhanced version = 0x%x)\n", id); } } else { snd_printk(KERN_WARNING "unknown CS4236/CS423xB chip " "(enhanced version = 0x%x)\n", id); } } } return 0; /* all things are ok.. */ } /* */ static const struct snd_pcm_hardware snd_wss_playback = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START), .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE), .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000, .rate_min = 5510, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_wss_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_SYNC_START), .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE), .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000, .rate_min = 5510, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; /* */ static int snd_wss_playback_open(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int err; runtime->hw = snd_wss_playback; /* hardware limitation of older chipsets */ if (chip->hardware & WSS_HW_AD1848_MASK) runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM | SNDRV_PCM_FMTBIT_S16_BE); /* hardware bug in InterWave chipset */ if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3) runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW; /* hardware limitation of cheap chips */ if (chip->hardware == WSS_HW_CS4235 || chip->hardware == WSS_HW_CS4239) runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE; snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max); snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max); if (chip->claim_dma) { err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1); if (err < 0) return err; } err = snd_wss_open(chip, WSS_MODE_PLAY); if (err < 0) { if (chip->release_dma) chip->release_dma(chip, chip->dma_private_data, chip->dma1); return err; } chip->playback_substream = substream; snd_pcm_set_sync(substream); chip->rate_constraint(runtime); return 0; } static int snd_wss_capture_open(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int err; runtime->hw = snd_wss_capture; /* hardware limitation of older chipsets */ if (chip->hardware & WSS_HW_AD1848_MASK) runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM | SNDRV_PCM_FMTBIT_S16_BE); /* hardware limitation of cheap chips */ if (chip->hardware == WSS_HW_CS4235 || chip->hardware == WSS_HW_CS4239 || chip->hardware == WSS_HW_OPTI93X) runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE; snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max); snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max); if (chip->claim_dma) { err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2); if (err < 0) return err; } err = snd_wss_open(chip, WSS_MODE_RECORD); if (err < 0) { if (chip->release_dma) chip->release_dma(chip, chip->dma_private_data, chip->dma2); return err; } chip->capture_substream = substream; snd_pcm_set_sync(substream); chip->rate_constraint(runtime); return 0; } static int snd_wss_playback_close(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); chip->playback_substream = NULL; snd_wss_close(chip, WSS_MODE_PLAY); return 0; } static int snd_wss_capture_close(struct snd_pcm_substream *substream) { struct snd_wss *chip = snd_pcm_substream_chip(substream); chip->capture_substream = NULL; snd_wss_close(chip, WSS_MODE_RECORD); return 0; } static void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on) { int tmp; if (!chip->thinkpad_flag) return; outb(0x1c, AD1848_THINKPAD_CTL_PORT1); tmp = inb(AD1848_THINKPAD_CTL_PORT2); if (on) /* turn it on */ tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT; else /* turn it off */ tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT; outb(tmp, AD1848_THINKPAD_CTL_PORT2); } #ifdef CONFIG_PM /* lowlevel suspend callback for CS4231 */ static void snd_wss_suspend(struct snd_wss *chip) { int reg; unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); for (reg = 0; reg < 32; reg++) chip->image[reg] = snd_wss_in(chip, reg); spin_unlock_irqrestore(&chip->reg_lock, flags); if (chip->thinkpad_flag) snd_wss_thinkpad_twiddle(chip, 0); } /* lowlevel resume callback for CS4231 */ static void snd_wss_resume(struct snd_wss *chip) { int reg; unsigned long flags; /* int timeout; */ if (chip->thinkpad_flag) snd_wss_thinkpad_twiddle(chip, 1); snd_wss_mce_up(chip); spin_lock_irqsave(&chip->reg_lock, flags); for (reg = 0; reg < 32; reg++) { switch (reg) { case CS4231_VERSION: break; default: snd_wss_out(chip, reg, chip->image[reg]); break; } } /* Yamaha needs this to resume properly */ if (chip->hardware == WSS_HW_OPL3SA2) snd_wss_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]); spin_unlock_irqrestore(&chip->reg_lock, flags); #if 1 snd_wss_mce_down(chip); #else /* The following is a workaround to avoid freeze after resume on TP600E. This is the first half of copy of snd_wss_mce_down(), but doesn't include rescheduling. -- iwai */ snd_wss_busy_wait(chip); spin_lock_irqsave(&chip->reg_lock, flags); chip->mce_bit &= ~CS4231_MCE; timeout = wss_inb(chip, CS4231P(REGSEL)); wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f)); spin_unlock_irqrestore(&chip->reg_lock, flags); if (timeout == 0x80) snd_printk(KERN_ERR "down [0x%lx]: serious init problem " "- codec still busy\n", chip->port); if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) { return; } snd_wss_busy_wait(chip); #endif } #endif /* CONFIG_PM */ const char *snd_wss_chip_id(struct snd_wss *chip) { switch (chip->hardware) { case WSS_HW_CS4231: return "CS4231"; case WSS_HW_CS4231A: return "CS4231A"; case WSS_HW_CS4232: return "CS4232"; case WSS_HW_CS4232A: return "CS4232A"; case WSS_HW_CS4235: return "CS4235"; case WSS_HW_CS4236: return "CS4236"; case WSS_HW_CS4236B: return "CS4236B"; case WSS_HW_CS4237B: return "CS4237B"; case WSS_HW_CS4238B: return "CS4238B"; case WSS_HW_CS4239: return "CS4239"; case WSS_HW_INTERWAVE: return "AMD InterWave"; case WSS_HW_OPL3SA2: return chip->card->shortname; case WSS_HW_AD1845: return "AD1845"; case WSS_HW_OPTI93X: return "OPTi 93x"; case WSS_HW_AD1847: return "AD1847"; case WSS_HW_AD1848: return "AD1848"; case WSS_HW_CS4248: return "CS4248"; case WSS_HW_CMI8330: return "CMI8330/C3D"; default: return "???"; } } EXPORT_SYMBOL(snd_wss_chip_id); static int snd_wss_new(struct snd_card *card, unsigned short hardware, unsigned short hwshare, struct snd_wss **rchip) { struct snd_wss *chip; *rchip = NULL; chip = devm_kzalloc(card->dev, sizeof(*chip), GFP_KERNEL); if (chip == NULL) return -ENOMEM; chip->hardware = hardware; chip->hwshare = hwshare; spin_lock_init(&chip->reg_lock); mutex_init(&chip->mce_mutex); mutex_init(&chip->open_mutex); chip->card = card; chip->rate_constraint = snd_wss_xrate; chip->set_playback_format = snd_wss_playback_format; chip->set_capture_format = snd_wss_capture_format; if (chip->hardware == WSS_HW_OPTI93X) memcpy(&chip->image, &snd_opti93x_original_image, sizeof(snd_opti93x_original_image)); else memcpy(&chip->image, &snd_wss_original_image, sizeof(snd_wss_original_image)); if (chip->hardware & WSS_HW_AD1848_MASK) { chip->image[CS4231_PIN_CTRL] = 0; chip->image[CS4231_TEST_INIT] = 0; } *rchip = chip; return 0; } int snd_wss_create(struct snd_card *card, unsigned long port, unsigned long cport, int irq, int dma1, int dma2, unsigned short hardware, unsigned short hwshare, struct snd_wss **rchip) { struct snd_wss *chip; int err; err = snd_wss_new(card, hardware, hwshare, &chip); if (err < 0) return err; chip->irq = -1; chip->dma1 = -1; chip->dma2 = -1; chip->res_port = devm_request_region(card->dev, port, 4, "WSS"); if (!chip->res_port) { snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port); return -EBUSY; } chip->port = port; if ((long)cport >= 0) { chip->res_cport = devm_request_region(card->dev, cport, 8, "CS4232 Control"); if (!chip->res_cport) { snd_printk(KERN_ERR "wss: can't grab control port 0x%lx\n", cport); return -ENODEV; } } chip->cport = cport; if (!(hwshare & WSS_HWSHARE_IRQ)) if (devm_request_irq(card->dev, irq, snd_wss_interrupt, 0, "WSS", (void *) chip)) { snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq); return -EBUSY; } chip->irq = irq; card->sync_irq = chip->irq; if (!(hwshare & WSS_HWSHARE_DMA1) && snd_devm_request_dma(card->dev, dma1, "WSS - 1")) { snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1); return -EBUSY; } chip->dma1 = dma1; if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 && dma2 >= 0 && snd_devm_request_dma(card->dev, dma2, "WSS - 2")) { snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2); return -EBUSY; } if (dma1 == dma2 || dma2 < 0) { chip->single_dma = 1; chip->dma2 = chip->dma1; } else chip->dma2 = dma2; if (hardware == WSS_HW_THINKPAD) { chip->thinkpad_flag = 1; chip->hardware = WSS_HW_DETECT; /* reset */ snd_wss_thinkpad_twiddle(chip, 1); } /* global setup */ if (snd_wss_probe(chip) < 0) return -ENODEV; snd_wss_init(chip); #if 0 if (chip->hardware & WSS_HW_CS4232_MASK) { if (chip->res_cport == NULL) snd_printk(KERN_ERR "CS4232 control port features are " "not accessible\n"); } #endif #ifdef CONFIG_PM /* Power Management */ chip->suspend = snd_wss_suspend; chip->resume = snd_wss_resume; #endif *rchip = chip; return 0; } EXPORT_SYMBOL(snd_wss_create); static const struct snd_pcm_ops snd_wss_playback_ops = { .open = snd_wss_playback_open, .close = snd_wss_playback_close, .hw_params = snd_wss_playback_hw_params, .prepare = snd_wss_playback_prepare, .trigger = snd_wss_trigger, .pointer = snd_wss_playback_pointer, }; static const struct snd_pcm_ops snd_wss_capture_ops = { .open = snd_wss_capture_open, .close = snd_wss_capture_close, .hw_params = snd_wss_capture_hw_params, .prepare = snd_wss_capture_prepare, .trigger = snd_wss_trigger, .pointer = snd_wss_capture_pointer, }; int snd_wss_pcm(struct snd_wss *chip, int device) { struct snd_pcm *pcm; int err; err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm); if (err < 0) return err; snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops); /* global setup */ pcm->private_data = chip; pcm->info_flags = 0; if (chip->single_dma) pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX; if (chip->hardware != WSS_HW_INTERWAVE) pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX; strcpy(pcm->name, snd_wss_chip_id(chip)); snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, chip->card->dev, 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024); chip->pcm = pcm; return 0; } EXPORT_SYMBOL(snd_wss_pcm); static void snd_wss_timer_free(struct snd_timer *timer) { struct snd_wss *chip = timer->private_data; chip->timer = NULL; } int snd_wss_timer(struct snd_wss *chip, int device) { struct snd_timer *timer; struct snd_timer_id tid; int err; /* Timer initialization */ tid.dev_class = SNDRV_TIMER_CLASS_CARD; tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; tid.card = chip->card->number; tid.device = device; tid.subdevice = 0; err = snd_timer_new(chip->card, "CS4231", &tid, &timer); if (err < 0) return err; strcpy(timer->name, snd_wss_chip_id(chip)); timer->private_data = chip; timer->private_free = snd_wss_timer_free; timer->hw = snd_wss_timer_table; chip->timer = timer; return 0; } EXPORT_SYMBOL(snd_wss_timer); /* * MIXER part */ static int snd_wss_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static const char * const texts[4] = { "Line", "Aux", "Mic", "Mix" }; static const char * const opl3sa_texts[4] = { "Line", "CD", "Mic", "Mix" }; static const char * const gusmax_texts[4] = { "Line", "Synth", "Mic", "Mix" }; const char * const *ptexts = texts; struct snd_wss *chip = snd_kcontrol_chip(kcontrol); if (snd_BUG_ON(!chip->card)) return -EINVAL; if (!strcmp(chip->card->driver, "GUS MAX")) ptexts = gusmax_texts; switch (chip->hardware) { case WSS_HW_INTERWAVE: ptexts = gusmax_texts; break; case WSS_HW_OPTI93X: case WSS_HW_OPL3SA2: ptexts = opl3sa_texts; break; } return snd_ctl_enum_info(uinfo, 2, 4, ptexts); } static int snd_wss_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6; ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6; spin_unlock_irqrestore(&chip->reg_lock, flags); return 0; } static int snd_wss_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; unsigned short left, right; int change; if (ucontrol->value.enumerated.item[0] > 3 || ucontrol->value.enumerated.item[1] > 3) return -EINVAL; left = ucontrol->value.enumerated.item[0] << 6; right = ucontrol->value.enumerated.item[1] << 6; spin_lock_irqsave(&chip->reg_lock, flags); left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left; right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right; change = left != chip->image[CS4231_LEFT_INPUT] || right != chip->image[CS4231_RIGHT_INPUT]; snd_wss_out(chip, CS4231_LEFT_INPUT, left); snd_wss_out(chip, CS4231_RIGHT_INPUT, right); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } int snd_wss_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 16) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } EXPORT_SYMBOL(snd_wss_info_single); int snd_wss_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; return 0; } EXPORT_SYMBOL(snd_wss_get_single); int snd_wss_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 8) & 0xff; int mask = (kcontrol->private_value >> 16) & 0xff; int invert = (kcontrol->private_value >> 24) & 0xff; int change; unsigned short val; val = (ucontrol->value.integer.value[0] & mask); if (invert) val = mask - val; val <<= shift; spin_lock_irqsave(&chip->reg_lock, flags); val = (chip->image[reg] & ~(mask << shift)) | val; change = val != chip->image[reg]; snd_wss_out(chip, reg, val); spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } EXPORT_SYMBOL(snd_wss_put_single); int snd_wss_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { int mask = (kcontrol->private_value >> 24) & 0xff; uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = mask; return 0; } EXPORT_SYMBOL(snd_wss_info_double); int snd_wss_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; spin_lock_irqsave(&chip->reg_lock, flags); ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask; ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask; spin_unlock_irqrestore(&chip->reg_lock, flags); if (invert) { ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; } return 0; } EXPORT_SYMBOL(snd_wss_get_double); int snd_wss_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_wss *chip = snd_kcontrol_chip(kcontrol); unsigned long flags; int left_reg = kcontrol->private_value & 0xff; int right_reg = (kcontrol->private_value >> 8) & 0xff; int shift_left = (kcontrol->private_value >> 16) & 0x07; int shift_right = (kcontrol->private_value >> 19) & 0x07; int mask = (kcontrol->private_value >> 24) & 0xff; int invert = (kcontrol->private_value >> 22) & 1; int change; unsigned short val1, val2; val1 = ucontrol->value.integer.value[0] & mask; val2 = ucontrol->value.integer.value[1] & mask; if (invert) { val1 = mask - val1; val2 = mask - val2; } val1 <<= shift_left; val2 <<= shift_right; spin_lock_irqsave(&chip->reg_lock, flags); if (left_reg != right_reg) { val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1; val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2; change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg]; snd_wss_out(chip, left_reg, val1); snd_wss_out(chip, right_reg, val2); } else { mask = (mask << shift_left) | (mask << shift_right); val1 = (chip->image[left_reg] & ~mask) | val1 | val2; change = val1 != chip->image[left_reg]; snd_wss_out(chip, left_reg, val1); } spin_unlock_irqrestore(&chip->reg_lock, flags); return change; } EXPORT_SYMBOL(snd_wss_put_double); static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0); static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0); static const struct snd_kcontrol_new snd_wss_controls[] = { WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1, db_scale_6bit), WSS_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1), WSS_DOUBLE_TLV("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0, db_scale_rec_gain), { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Capture Source", .info = snd_wss_info_mux, .get = snd_wss_get_mux, .put = snd_wss_put_mux, }, WSS_DOUBLE("Mic Boost (+20dB)", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0), WSS_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0), WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1, db_scale_6bit), WSS_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1), WSS_DOUBLE_TLV("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1, db_scale_5bit_12db_max), WSS_SINGLE("Beep Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1), WSS_SINGLE_TLV("Beep Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1, db_scale_4bit), WSS_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1), WSS_SINGLE("Beep Bypass Playback Switch", 0, CS4231_MONO_CTRL, 5, 1, 0), }; int snd_wss_mixer(struct snd_wss *chip) { struct snd_card *card; unsigned int idx; int err; int count = ARRAY_SIZE(snd_wss_controls); if (snd_BUG_ON(!chip || !chip->pcm)) return -EINVAL; card = chip->card; strcpy(card->mixername, chip->pcm->name); /* Use only the first 11 entries on AD1848 */ if (chip->hardware & WSS_HW_AD1848_MASK) count = 11; /* There is no loopback on OPTI93X */ else if (chip->hardware == WSS_HW_OPTI93X) count = 9; for (idx = 0; idx < count; idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_wss_controls[idx], chip)); if (err < 0) return err; } return 0; } EXPORT_SYMBOL(snd_wss_mixer); const struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction) { return direction == SNDRV_PCM_STREAM_PLAYBACK ? &snd_wss_playback_ops : &snd_wss_capture_ops; } EXPORT_SYMBOL(snd_wss_get_pcm_ops);
linux-master
sound/isa/wss/wss_lib.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for Gravis UltraSound Classic soundcard * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/time.h> #include <linux/module.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/gus.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #define CRD_NAME "Gravis UltraSound Classic" #define DEV_NAME "gusclassic" MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,5,9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for " CRD_NAME " driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for " CRD_NAME " driver."); module_param_array(joystick_dac, int, NULL, 0444); MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver."); module_param_array(channels, int, NULL, 0444); MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver."); module_param_array(pcm_channels, int, NULL, 0444); MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver."); static int snd_gusclassic_match(struct device *dev, unsigned int n) { return enable[n]; } static int snd_gusclassic_create(struct snd_card *card, struct device *dev, unsigned int n, struct snd_gus_card **rgus) { static const long possible_ports[] = {0x220, 0x230, 0x240, 0x250, 0x260}; static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, 4, -1}; static const int possible_dmas[] = {5, 6, 7, 1, 3, -1}; int i, error; if (irq[n] == SNDRV_AUTO_IRQ) { irq[n] = snd_legacy_find_free_irq(possible_irqs); if (irq[n] < 0) { dev_err(dev, "unable to find a free IRQ\n"); return -EBUSY; } } if (dma1[n] == SNDRV_AUTO_DMA) { dma1[n] = snd_legacy_find_free_dma(possible_dmas); if (dma1[n] < 0) { dev_err(dev, "unable to find a free DMA1\n"); return -EBUSY; } } if (dma2[n] == SNDRV_AUTO_DMA) { dma2[n] = snd_legacy_find_free_dma(possible_dmas); if (dma2[n] < 0) { dev_err(dev, "unable to find a free DMA2\n"); return -EBUSY; } } if (port[n] != SNDRV_AUTO_PORT) return snd_gus_create(card, port[n], irq[n], dma1[n], dma2[n], 0, channels[n], pcm_channels[n], 0, rgus); i = 0; do { port[n] = possible_ports[i]; error = snd_gus_create(card, port[n], irq[n], dma1[n], dma2[n], 0, channels[n], pcm_channels[n], 0, rgus); } while (error < 0 && ++i < ARRAY_SIZE(possible_ports)); return error; } static int snd_gusclassic_detect(struct snd_gus_card *gus) { unsigned char d; snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 0) { snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); return -ENODEV; } udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ udelay(160); d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 1) { snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); return -ENODEV; } return 0; } static int snd_gusclassic_probe(struct device *dev, unsigned int n) { struct snd_card *card; struct snd_gus_card *gus; int error; error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, 0, &card); if (error < 0) return error; if (pcm_channels[n] < 2) pcm_channels[n] = 2; error = snd_gusclassic_create(card, dev, n, &gus); if (error < 0) return error; error = snd_gusclassic_detect(gus); if (error < 0) return error; gus->joystick_dac = joystick_dac[n]; error = snd_gus_initialize(gus); if (error < 0) return error; error = -ENODEV; if (gus->max_flag || gus->ess_flag) { dev_err(dev, "GUS Classic or ACE soundcard was " "not detected at 0x%lx\n", gus->gf1.port); return error; } error = snd_gf1_new_mixer(gus); if (error < 0) return error; error = snd_gf1_pcm_new(gus, 0, 0); if (error < 0) return error; if (!gus->ace_flag) { error = snd_gf1_rawmidi_new(gus, 0); if (error < 0) return error; } sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %d, dma %d", gus->gf1.port, gus->gf1.irq, gus->gf1.dma1); if (gus->gf1.dma2 >= 0) sprintf(card->longname + strlen(card->longname), "&%d", gus->gf1.dma2); error = snd_card_register(card); if (error < 0) return error; dev_set_drvdata(dev, card); return 0; } static struct isa_driver snd_gusclassic_driver = { .match = snd_gusclassic_match, .probe = snd_gusclassic_probe, #if 0 /* FIXME */ .suspend = snd_gusclassic_suspend, #endif .driver = { .name = DEV_NAME } }; module_isa_driver(snd_gusclassic_driver, SNDRV_CARDS);
linux-master
sound/isa/gus/gusclassic.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for Gravis UltraSound MAX soundcard * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/time.h> #include <linux/module.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/gus.h> #include <sound/wss.h> #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_DESCRIPTION("Gravis UltraSound MAX"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x230,0x240,0x250,0x260 */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */ static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for GUS MAX soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for GUS MAX soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable GUS MAX soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for GUS MAX driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for GUS MAX driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for GUS MAX driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for GUS MAX driver."); module_param_array(joystick_dac, int, NULL, 0444); MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for GUS MAX driver."); module_param_array(channels, int, NULL, 0444); MODULE_PARM_DESC(channels, "Used GF1 channels for GUS MAX driver."); module_param_array(pcm_channels, int, NULL, 0444); MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for GUS MAX driver."); struct snd_gusmax { int irq; struct snd_card *card; struct snd_gus_card *gus; struct snd_wss *wss; unsigned short gus_status_reg; unsigned short pcm_status_reg; }; #define PFX "gusmax: " static int snd_gusmax_detect(struct snd_gus_card *gus) { unsigned char d; snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 0) { snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); return -ENODEV; } udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ udelay(160); d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 1) { snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); return -ENODEV; } return 0; } static irqreturn_t snd_gusmax_interrupt(int irq, void *dev_id) { struct snd_gusmax *maxcard = dev_id; int loop, max = 5; int handled = 0; do { loop = 0; if (inb(maxcard->gus_status_reg)) { handled = 1; snd_gus_interrupt(irq, maxcard->gus); loop++; } if (inb(maxcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */ handled = 1; snd_wss_interrupt(irq, maxcard->wss); loop++; } } while (loop && --max > 0); return IRQ_RETVAL(handled); } static void snd_gusmax_init(int dev, struct snd_card *card, struct snd_gus_card *gus) { gus->equal_irq = 1; gus->codec_flag = 1; gus->joystick_dac = joystick_dac[dev]; /* init control register */ gus->max_cntrl_val = (gus->gf1.port >> 4) & 0x0f; if (gus->gf1.dma1 > 3) gus->max_cntrl_val |= 0x10; if (gus->gf1.dma2 > 3) gus->max_cntrl_val |= 0x20; gus->max_cntrl_val |= 0x40; outb(gus->max_cntrl_val, GUSP(gus, MAXCNTRLPORT)); } static int snd_gusmax_mixer(struct snd_wss *chip) { struct snd_card *card = chip->card; struct snd_ctl_elem_id id1, id2; int err; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; /* reassign AUXA to SYNTHESIZER */ strcpy(id1.name, "Aux Playback Switch"); strcpy(id2.name, "Synth Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "Synth Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; /* reassign AUXB to CD */ strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; strcpy(id2.name, "CD Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "CD Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; #if 0 /* reassign Mono Input to MIC */ if (snd_mixer_group_rename(mixer, SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_IN_MIC, 0) < 0) goto __error; if (snd_mixer_elem_rename(mixer, SNDRV_MIXER_IN_MONO, 0, SNDRV_MIXER_ETYPE_INPUT, SNDRV_MIXER_IN_MIC, 0) < 0) goto __error; if (snd_mixer_elem_rename(mixer, "Mono Capture Volume", 0, SNDRV_MIXER_ETYPE_VOLUME1, "Mic Capture Volume", 0) < 0) goto __error; if (snd_mixer_elem_rename(mixer, "Mono Capture Switch", 0, SNDRV_MIXER_ETYPE_SWITCH1, "Mic Capture Switch", 0) < 0) goto __error; #endif return 0; } static int snd_gusmax_match(struct device *pdev, unsigned int dev) { return enable[dev]; } static int snd_gusmax_probe(struct device *pdev, unsigned int dev) { static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1}; static const int possible_dmas[] = {5, 6, 7, 1, 3, -1}; int xirq, xdma1, xdma2, err; struct snd_card *card; struct snd_gus_card *gus = NULL; struct snd_wss *wss; struct snd_gusmax *maxcard; err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_gusmax), &card); if (err < 0) return err; maxcard = card->private_data; maxcard->card = card; maxcard->irq = -1; xirq = irq[dev]; if (xirq == SNDRV_AUTO_IRQ) { xirq = snd_legacy_find_free_irq(possible_irqs); if (xirq < 0) { snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); return -EBUSY; } } xdma1 = dma1[dev]; if (xdma1 == SNDRV_AUTO_DMA) { xdma1 = snd_legacy_find_free_dma(possible_dmas); if (xdma1 < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA1\n"); return -EBUSY; } } xdma2 = dma2[dev]; if (xdma2 == SNDRV_AUTO_DMA) { xdma2 = snd_legacy_find_free_dma(possible_dmas); if (xdma2 < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA2\n"); return -EBUSY; } } if (port[dev] != SNDRV_AUTO_PORT) { err = snd_gus_create(card, port[dev], -xirq, xdma1, xdma2, 0, channels[dev], pcm_channels[dev], 0, &gus); } else { static const unsigned long possible_ports[] = { 0x220, 0x230, 0x240, 0x250, 0x260 }; int i; for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { err = snd_gus_create(card, possible_ports[i], -xirq, xdma1, xdma2, 0, channels[dev], pcm_channels[dev], 0, &gus); if (err >= 0) { port[dev] = possible_ports[i]; break; } } } if (err < 0) return err; err = snd_gusmax_detect(gus); if (err < 0) return err; maxcard->gus_status_reg = gus->gf1.reg_irqstat; maxcard->pcm_status_reg = gus->gf1.port + 0x10c + 2; snd_gusmax_init(dev, card, gus); err = snd_gus_initialize(gus); if (err < 0) return err; if (!gus->max_flag) { snd_printk(KERN_ERR PFX "GUS MAX soundcard was not detected at 0x%lx\n", gus->gf1.port); return -ENODEV; } if (devm_request_irq(card->dev, xirq, snd_gusmax_interrupt, 0, "GUS MAX", (void *)maxcard)) { snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq); return -EBUSY; } maxcard->irq = xirq; card->sync_irq = maxcard->irq; err = snd_wss_create(card, gus->gf1.port + 0x10c, -1, xirq, xdma2 < 0 ? xdma1 : xdma2, xdma1, WSS_HW_DETECT, WSS_HWSHARE_IRQ | WSS_HWSHARE_DMA1 | WSS_HWSHARE_DMA2, &wss); if (err < 0) return err; err = snd_wss_pcm(wss, 0); if (err < 0) return err; err = snd_wss_mixer(wss); if (err < 0) return err; err = snd_wss_timer(wss, 2); if (err < 0) return err; if (pcm_channels[dev] > 0) { err = snd_gf1_pcm_new(gus, 1, 1); if (err < 0) return err; } err = snd_gusmax_mixer(wss); if (err < 0) return err; err = snd_gf1_rawmidi_new(gus, 0); if (err < 0) return err; sprintf(card->longname + strlen(card->longname), " at 0x%lx, irq %i, dma %i", gus->gf1.port, xirq, xdma1); if (xdma2 >= 0) sprintf(card->longname + strlen(card->longname), "&%i", xdma2); err = snd_card_register(card); if (err < 0) return err; maxcard->gus = gus; maxcard->wss = wss; dev_set_drvdata(pdev, card); return 0; } #define DEV_NAME "gusmax" static struct isa_driver snd_gusmax_driver = { .match = snd_gusmax_match, .probe = snd_gusmax_probe, /* FIXME: suspend/resume */ .driver = { .name = DEV_NAME }, }; module_isa_driver(snd_gusmax_driver, SNDRV_CARDS);
linux-master
sound/isa/gus/gusmax.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * I/O routines for GF1/InterWave synthesizer chips */ #include <linux/delay.h> #include <linux/time.h> #include <sound/core.h> #include <sound/gus.h> void snd_gf1_delay(struct snd_gus_card * gus) { int i; for (i = 0; i < 6; i++) { mb(); inb(GUSP(gus, DRAM)); } } /* * ======================================================================= */ /* * ok.. stop of control registers (wave & ramp) need some special things.. * big UltraClick (tm) elimination... */ static inline void __snd_gf1_ctrl_stop(struct snd_gus_card * gus, unsigned char reg) { unsigned char value; outb(reg | 0x80, gus->gf1.reg_regsel); mb(); value = inb(gus->gf1.reg_data8); mb(); outb(reg, gus->gf1.reg_regsel); mb(); outb((value | 0x03) & ~(0x80 | 0x20), gus->gf1.reg_data8); mb(); } static inline void __snd_gf1_write8(struct snd_gus_card * gus, unsigned char reg, unsigned char data) { outb(reg, gus->gf1.reg_regsel); mb(); outb(data, gus->gf1.reg_data8); mb(); } static inline unsigned char __snd_gf1_look8(struct snd_gus_card * gus, unsigned char reg) { outb(reg, gus->gf1.reg_regsel); mb(); return inb(gus->gf1.reg_data8); } static inline void __snd_gf1_write16(struct snd_gus_card * gus, unsigned char reg, unsigned int data) { outb(reg, gus->gf1.reg_regsel); mb(); outw((unsigned short) data, gus->gf1.reg_data16); mb(); } static inline unsigned short __snd_gf1_look16(struct snd_gus_card * gus, unsigned char reg) { outb(reg, gus->gf1.reg_regsel); mb(); return inw(gus->gf1.reg_data16); } static inline void __snd_gf1_adlib_write(struct snd_gus_card * gus, unsigned char reg, unsigned char data) { outb(reg, gus->gf1.reg_timerctrl); inb(gus->gf1.reg_timerctrl); inb(gus->gf1.reg_timerctrl); outb(data, gus->gf1.reg_timerdata); inb(gus->gf1.reg_timerctrl); inb(gus->gf1.reg_timerctrl); } static inline void __snd_gf1_write_addr(struct snd_gus_card * gus, unsigned char reg, unsigned int addr, int w_16bit) { if (gus->gf1.enh_mode) { if (w_16bit) addr = ((addr >> 1) & ~0x0000000f) | (addr & 0x0000000f); __snd_gf1_write8(gus, SNDRV_GF1_VB_UPPER_ADDRESS, (unsigned char) ((addr >> 26) & 0x03)); } else if (w_16bit) addr = (addr & 0x00c0000f) | ((addr & 0x003ffff0) >> 1); __snd_gf1_write16(gus, reg, (unsigned short) (addr >> 11)); __snd_gf1_write16(gus, reg + 1, (unsigned short) (addr << 5)); } static inline unsigned int __snd_gf1_read_addr(struct snd_gus_card * gus, unsigned char reg, short w_16bit) { unsigned int res; res = ((unsigned int) __snd_gf1_look16(gus, reg | 0x80) << 11) & 0xfff800; res |= ((unsigned int) __snd_gf1_look16(gus, (reg + 1) | 0x80) >> 5) & 0x0007ff; if (gus->gf1.enh_mode) { res |= (unsigned int) __snd_gf1_look8(gus, SNDRV_GF1_VB_UPPER_ADDRESS | 0x80) << 26; if (w_16bit) res = ((res << 1) & 0xffffffe0) | (res & 0x0000000f); } else if (w_16bit) res = ((res & 0x001ffff0) << 1) | (res & 0x00c0000f); return res; } /* * ======================================================================= */ void snd_gf1_ctrl_stop(struct snd_gus_card * gus, unsigned char reg) { __snd_gf1_ctrl_stop(gus, reg); } void snd_gf1_write8(struct snd_gus_card * gus, unsigned char reg, unsigned char data) { __snd_gf1_write8(gus, reg, data); } unsigned char snd_gf1_look8(struct snd_gus_card * gus, unsigned char reg) { return __snd_gf1_look8(gus, reg); } void snd_gf1_write16(struct snd_gus_card * gus, unsigned char reg, unsigned int data) { __snd_gf1_write16(gus, reg, data); } unsigned short snd_gf1_look16(struct snd_gus_card * gus, unsigned char reg) { return __snd_gf1_look16(gus, reg); } void snd_gf1_adlib_write(struct snd_gus_card * gus, unsigned char reg, unsigned char data) { __snd_gf1_adlib_write(gus, reg, data); } void snd_gf1_write_addr(struct snd_gus_card * gus, unsigned char reg, unsigned int addr, short w_16bit) { __snd_gf1_write_addr(gus, reg, addr, w_16bit); } unsigned int snd_gf1_read_addr(struct snd_gus_card * gus, unsigned char reg, short w_16bit) { return __snd_gf1_read_addr(gus, reg, w_16bit); } /* */ void snd_gf1_i_ctrl_stop(struct snd_gus_card * gus, unsigned char reg) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); __snd_gf1_ctrl_stop(gus, reg); spin_unlock_irqrestore(&gus->reg_lock, flags); } void snd_gf1_i_write8(struct snd_gus_card * gus, unsigned char reg, unsigned char data) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); __snd_gf1_write8(gus, reg, data); spin_unlock_irqrestore(&gus->reg_lock, flags); } unsigned char snd_gf1_i_look8(struct snd_gus_card * gus, unsigned char reg) { unsigned long flags; unsigned char res; spin_lock_irqsave(&gus->reg_lock, flags); res = __snd_gf1_look8(gus, reg); spin_unlock_irqrestore(&gus->reg_lock, flags); return res; } void snd_gf1_i_write16(struct snd_gus_card * gus, unsigned char reg, unsigned int data) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); __snd_gf1_write16(gus, reg, data); spin_unlock_irqrestore(&gus->reg_lock, flags); } unsigned short snd_gf1_i_look16(struct snd_gus_card * gus, unsigned char reg) { unsigned long flags; unsigned short res; spin_lock_irqsave(&gus->reg_lock, flags); res = __snd_gf1_look16(gus, reg); spin_unlock_irqrestore(&gus->reg_lock, flags); return res; } #if 0 void snd_gf1_i_adlib_write(struct snd_gus_card * gus, unsigned char reg, unsigned char data) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); __snd_gf1_adlib_write(gus, reg, data); spin_unlock_irqrestore(&gus->reg_lock, flags); } void snd_gf1_i_write_addr(struct snd_gus_card * gus, unsigned char reg, unsigned int addr, short w_16bit) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); __snd_gf1_write_addr(gus, reg, addr, w_16bit); spin_unlock_irqrestore(&gus->reg_lock, flags); } #endif /* 0 */ #ifdef CONFIG_SND_DEBUG static unsigned int snd_gf1_i_read_addr(struct snd_gus_card * gus, unsigned char reg, short w_16bit) { unsigned int res; unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); res = __snd_gf1_read_addr(gus, reg, w_16bit); spin_unlock_irqrestore(&gus->reg_lock, flags); return res; } #endif /* */ void snd_gf1_dram_addr(struct snd_gus_card * gus, unsigned int addr) { outb(0x43, gus->gf1.reg_regsel); mb(); outw((unsigned short) addr, gus->gf1.reg_data16); mb(); outb(0x44, gus->gf1.reg_regsel); mb(); outb((unsigned char) (addr >> 16), gus->gf1.reg_data8); mb(); } void snd_gf1_poke(struct snd_gus_card * gus, unsigned int addr, unsigned char data) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); outb(SNDRV_GF1_GW_DRAM_IO_LOW, gus->gf1.reg_regsel); mb(); outw((unsigned short) addr, gus->gf1.reg_data16); mb(); outb(SNDRV_GF1_GB_DRAM_IO_HIGH, gus->gf1.reg_regsel); mb(); outb((unsigned char) (addr >> 16), gus->gf1.reg_data8); mb(); outb(data, gus->gf1.reg_dram); spin_unlock_irqrestore(&gus->reg_lock, flags); } unsigned char snd_gf1_peek(struct snd_gus_card * gus, unsigned int addr) { unsigned long flags; unsigned char res; spin_lock_irqsave(&gus->reg_lock, flags); outb(SNDRV_GF1_GW_DRAM_IO_LOW, gus->gf1.reg_regsel); mb(); outw((unsigned short) addr, gus->gf1.reg_data16); mb(); outb(SNDRV_GF1_GB_DRAM_IO_HIGH, gus->gf1.reg_regsel); mb(); outb((unsigned char) (addr >> 16), gus->gf1.reg_data8); mb(); res = inb(gus->gf1.reg_dram); spin_unlock_irqrestore(&gus->reg_lock, flags); return res; } #if 0 void snd_gf1_pokew(struct snd_gus_card * gus, unsigned int addr, unsigned short data) { unsigned long flags; #ifdef CONFIG_SND_DEBUG if (!gus->interwave) snd_printk(KERN_DEBUG "snd_gf1_pokew - GF1!!!\n"); #endif spin_lock_irqsave(&gus->reg_lock, flags); outb(SNDRV_GF1_GW_DRAM_IO_LOW, gus->gf1.reg_regsel); mb(); outw((unsigned short) addr, gus->gf1.reg_data16); mb(); outb(SNDRV_GF1_GB_DRAM_IO_HIGH, gus->gf1.reg_regsel); mb(); outb((unsigned char) (addr >> 16), gus->gf1.reg_data8); mb(); outb(SNDRV_GF1_GW_DRAM_IO16, gus->gf1.reg_regsel); mb(); outw(data, gus->gf1.reg_data16); spin_unlock_irqrestore(&gus->reg_lock, flags); } unsigned short snd_gf1_peekw(struct snd_gus_card * gus, unsigned int addr) { unsigned long flags; unsigned short res; #ifdef CONFIG_SND_DEBUG if (!gus->interwave) snd_printk(KERN_DEBUG "snd_gf1_peekw - GF1!!!\n"); #endif spin_lock_irqsave(&gus->reg_lock, flags); outb(SNDRV_GF1_GW_DRAM_IO_LOW, gus->gf1.reg_regsel); mb(); outw((unsigned short) addr, gus->gf1.reg_data16); mb(); outb(SNDRV_GF1_GB_DRAM_IO_HIGH, gus->gf1.reg_regsel); mb(); outb((unsigned char) (addr >> 16), gus->gf1.reg_data8); mb(); outb(SNDRV_GF1_GW_DRAM_IO16, gus->gf1.reg_regsel); mb(); res = inw(gus->gf1.reg_data16); spin_unlock_irqrestore(&gus->reg_lock, flags); return res; } void snd_gf1_dram_setmem(struct snd_gus_card * gus, unsigned int addr, unsigned short value, unsigned int count) { unsigned long port; unsigned long flags; #ifdef CONFIG_SND_DEBUG if (!gus->interwave) snd_printk(KERN_DEBUG "snd_gf1_dram_setmem - GF1!!!\n"); #endif addr &= ~1; count >>= 1; port = GUSP(gus, GF1DATALOW); spin_lock_irqsave(&gus->reg_lock, flags); outb(SNDRV_GF1_GW_DRAM_IO_LOW, gus->gf1.reg_regsel); mb(); outw((unsigned short) addr, gus->gf1.reg_data16); mb(); outb(SNDRV_GF1_GB_DRAM_IO_HIGH, gus->gf1.reg_regsel); mb(); outb((unsigned char) (addr >> 16), gus->gf1.reg_data8); mb(); outb(SNDRV_GF1_GW_DRAM_IO16, gus->gf1.reg_regsel); while (count--) outw(value, port); spin_unlock_irqrestore(&gus->reg_lock, flags); } #endif /* 0 */ void snd_gf1_select_active_voices(struct snd_gus_card * gus) { unsigned short voices; static const unsigned short voices_tbl[32 - 14 + 1] = { 44100, 41160, 38587, 36317, 34300, 32494, 30870, 29400, 28063, 26843, 25725, 24696, 23746, 22866, 22050, 21289, 20580, 19916, 19293 }; voices = gus->gf1.active_voices; if (voices > 32) voices = 32; if (voices < 14) voices = 14; if (gus->gf1.enh_mode) voices = 32; gus->gf1.active_voices = voices; gus->gf1.playback_freq = gus->gf1.enh_mode ? 44100 : voices_tbl[voices - 14]; if (!gus->gf1.enh_mode) { snd_gf1_i_write8(gus, SNDRV_GF1_GB_ACTIVE_VOICES, 0xc0 | (voices - 1)); udelay(100); } } #ifdef CONFIG_SND_DEBUG void snd_gf1_print_voice_registers(struct snd_gus_card * gus) { unsigned char mode; int voice, ctrl; voice = gus->gf1.active_voice; printk(KERN_INFO " -%i- GF1 voice ctrl, ramp ctrl = 0x%x, 0x%x\n", voice, ctrl = snd_gf1_i_read8(gus, 0), snd_gf1_i_read8(gus, 0x0d)); printk(KERN_INFO " -%i- GF1 frequency = 0x%x\n", voice, snd_gf1_i_read16(gus, 1)); printk(KERN_INFO " -%i- GF1 loop start, end = 0x%x (0x%x), 0x%x (0x%x)\n", voice, snd_gf1_i_read_addr(gus, 2, ctrl & 4), snd_gf1_i_read_addr(gus, 2, (ctrl & 4) ^ 4), snd_gf1_i_read_addr(gus, 4, ctrl & 4), snd_gf1_i_read_addr(gus, 4, (ctrl & 4) ^ 4)); printk(KERN_INFO " -%i- GF1 ramp start, end, rate = 0x%x, 0x%x, 0x%x\n", voice, snd_gf1_i_read8(gus, 7), snd_gf1_i_read8(gus, 8), snd_gf1_i_read8(gus, 6)); printk(KERN_INFO" -%i- GF1 volume = 0x%x\n", voice, snd_gf1_i_read16(gus, 9)); printk(KERN_INFO " -%i- GF1 position = 0x%x (0x%x)\n", voice, snd_gf1_i_read_addr(gus, 0x0a, ctrl & 4), snd_gf1_i_read_addr(gus, 0x0a, (ctrl & 4) ^ 4)); if (gus->interwave && snd_gf1_i_read8(gus, 0x19) & 0x01) { /* enhanced mode */ mode = snd_gf1_i_read8(gus, 0x15); printk(KERN_INFO " -%i- GFA1 mode = 0x%x\n", voice, mode); if (mode & 0x01) { /* Effect processor */ printk(KERN_INFO " -%i- GFA1 effect address = 0x%x\n", voice, snd_gf1_i_read_addr(gus, 0x11, ctrl & 4)); printk(KERN_INFO " -%i- GFA1 effect volume = 0x%x\n", voice, snd_gf1_i_read16(gus, 0x16)); printk(KERN_INFO " -%i- GFA1 effect volume final = 0x%x\n", voice, snd_gf1_i_read16(gus, 0x1d)); printk(KERN_INFO " -%i- GFA1 effect accumulator = 0x%x\n", voice, snd_gf1_i_read8(gus, 0x14)); } if (mode & 0x20) { printk(KERN_INFO " -%i- GFA1 left offset = 0x%x (%i)\n", voice, snd_gf1_i_read16(gus, 0x13), snd_gf1_i_read16(gus, 0x13) >> 4); printk(KERN_INFO " -%i- GFA1 left offset final = 0x%x (%i)\n", voice, snd_gf1_i_read16(gus, 0x1c), snd_gf1_i_read16(gus, 0x1c) >> 4); printk(KERN_INFO " -%i- GFA1 right offset = 0x%x (%i)\n", voice, snd_gf1_i_read16(gus, 0x0c), snd_gf1_i_read16(gus, 0x0c) >> 4); printk(KERN_INFO " -%i- GFA1 right offset final = 0x%x (%i)\n", voice, snd_gf1_i_read16(gus, 0x1b), snd_gf1_i_read16(gus, 0x1b) >> 4); } else printk(KERN_INFO " -%i- GF1 pan = 0x%x\n", voice, snd_gf1_i_read8(gus, 0x0c)); } else printk(KERN_INFO " -%i- GF1 pan = 0x%x\n", voice, snd_gf1_i_read8(gus, 0x0c)); } #if 0 void snd_gf1_print_global_registers(struct snd_gus_card * gus) { unsigned char global_mode = 0x00; printk(KERN_INFO " -G- GF1 active voices = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_ACTIVE_VOICES)); if (gus->interwave) { global_mode = snd_gf1_i_read8(gus, SNDRV_GF1_GB_GLOBAL_MODE); printk(KERN_INFO " -G- GF1 global mode = 0x%x\n", global_mode); } if (global_mode & 0x02) /* LFO enabled? */ printk(KERN_INFO " -G- GF1 LFO base = 0x%x\n", snd_gf1_i_look16(gus, SNDRV_GF1_GW_LFO_BASE)); printk(KERN_INFO " -G- GF1 voices IRQ read = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_VOICES_IRQ_READ)); printk(KERN_INFO " -G- GF1 DRAM DMA control = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL)); printk(KERN_INFO " -G- GF1 DRAM DMA high/low = 0x%x/0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_DRAM_DMA_HIGH), snd_gf1_i_read16(gus, SNDRV_GF1_GW_DRAM_DMA_LOW)); printk(KERN_INFO " -G- GF1 DRAM IO high/low = 0x%x/0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_DRAM_IO_HIGH), snd_gf1_i_read16(gus, SNDRV_GF1_GW_DRAM_IO_LOW)); if (!gus->interwave) printk(KERN_INFO " -G- GF1 record DMA control = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL)); printk(KERN_INFO " -G- GF1 DRAM IO 16 = 0x%x\n", snd_gf1_i_look16(gus, SNDRV_GF1_GW_DRAM_IO16)); if (gus->gf1.enh_mode) { printk(KERN_INFO " -G- GFA1 memory config = 0x%x\n", snd_gf1_i_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG)); printk(KERN_INFO " -G- GFA1 memory control = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_MEMORY_CONTROL)); printk(KERN_INFO " -G- GFA1 FIFO record base = 0x%x\n", snd_gf1_i_look16(gus, SNDRV_GF1_GW_FIFO_RECORD_BASE_ADDR)); printk(KERN_INFO " -G- GFA1 FIFO playback base = 0x%x\n", snd_gf1_i_look16(gus, SNDRV_GF1_GW_FIFO_PLAY_BASE_ADDR)); printk(KERN_INFO " -G- GFA1 interleave control = 0x%x\n", snd_gf1_i_look16(gus, SNDRV_GF1_GW_INTERLEAVE)); } } void snd_gf1_print_setup_registers(struct snd_gus_card * gus) { printk(KERN_INFO " -S- mix control = 0x%x\n", inb(GUSP(gus, MIXCNTRLREG))); printk(KERN_INFO " -S- IRQ status = 0x%x\n", inb(GUSP(gus, IRQSTAT))); printk(KERN_INFO " -S- timer control = 0x%x\n", inb(GUSP(gus, TIMERCNTRL))); printk(KERN_INFO " -S- timer data = 0x%x\n", inb(GUSP(gus, TIMERDATA))); printk(KERN_INFO " -S- status read = 0x%x\n", inb(GUSP(gus, REGCNTRLS))); printk(KERN_INFO " -S- Sound Blaster control = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL)); printk(KERN_INFO " -S- AdLib timer 1/2 = 0x%x/0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1), snd_gf1_i_look8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2)); printk(KERN_INFO " -S- reset = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET)); if (gus->interwave) { printk(KERN_INFO " -S- compatibility = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_COMPATIBILITY)); printk(KERN_INFO " -S- decode control = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_DECODE_CONTROL)); printk(KERN_INFO " -S- version number = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER)); printk(KERN_INFO " -S- MPU-401 emul. control A/B = 0x%x/0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_MPU401_CONTROL_A), snd_gf1_i_look8(gus, SNDRV_GF1_GB_MPU401_CONTROL_B)); printk(KERN_INFO " -S- emulation IRQ = 0x%x\n", snd_gf1_i_look8(gus, SNDRV_GF1_GB_EMULATION_IRQ)); } } void snd_gf1_peek_print_block(struct snd_gus_card * gus, unsigned int addr, int count, int w_16bit) { if (!w_16bit) { while (count-- > 0) printk(count > 0 ? "%02x:" : "%02x", snd_gf1_peek(gus, addr++)); } else { while (count-- > 0) { printk(count > 0 ? "%04x:" : "%04x", snd_gf1_peek(gus, addr) | (snd_gf1_peek(gus, addr + 1) << 8)); addr += 2; } } } #endif /* 0 */ #endif
linux-master
sound/isa/gus/gus_io.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for AMD InterWave soundcard * Copyright (c) by Jaroslav Kysela <[email protected]> * * 1999/07/22 Erik Inge Bolso <[email protected]> * * mixer group handlers */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/pnp.h> #include <linux/module.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/gus.h> #include <sound/wss.h> #ifdef SNDRV_STB #include <sound/tea6330t.h> #endif #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); #ifndef SNDRV_STB MODULE_DESCRIPTION("AMD InterWave"); #else MODULE_DESCRIPTION("AMD InterWave STB with TEA6330T"); #endif static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */ #ifdef CONFIG_PNP static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; #endif static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x210,0x220,0x230,0x240,0x250,0x260 */ #ifdef SNDRV_STB static long port_tc[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x350,0x360,0x370,0x380 */ #endif static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */ static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ static int midi[SNDRV_CARDS]; static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; static int effect[SNDRV_CARDS]; #ifdef SNDRV_STB #define PFX "interwave-stb: " #define INTERWAVE_DRIVER "snd_interwave_stb" #define INTERWAVE_PNP_DRIVER "interwave-stb" #else #define PFX "interwave: " #define INTERWAVE_DRIVER "snd_interwave" #define INTERWAVE_PNP_DRIVER "interwave" #endif module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for InterWave soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for InterWave soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable InterWave soundcard."); #ifdef CONFIG_PNP module_param_array(isapnp, bool, NULL, 0444); MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard."); #endif module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for InterWave driver."); #ifdef SNDRV_STB module_param_hw_array(port_tc, long, ioport, NULL, 0444); MODULE_PARM_DESC(port_tc, "Tone control (TEA6330T - i2c bus) port # for InterWave driver."); #endif module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for InterWave driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "DMA1 # for InterWave driver."); module_param_hw_array(dma2, int, dma, NULL, 0444); MODULE_PARM_DESC(dma2, "DMA2 # for InterWave driver."); module_param_array(joystick_dac, int, NULL, 0444); MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for InterWave driver."); module_param_array(midi, int, NULL, 0444); MODULE_PARM_DESC(midi, "MIDI UART enable for InterWave driver."); module_param_array(pcm_channels, int, NULL, 0444); MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for InterWave driver."); module_param_array(effect, int, NULL, 0444); MODULE_PARM_DESC(effect, "Effects enable for InterWave driver."); struct snd_interwave { int irq; struct snd_card *card; struct snd_gus_card *gus; struct snd_wss *wss; #ifdef SNDRV_STB struct resource *i2c_res; #endif unsigned short gus_status_reg; unsigned short pcm_status_reg; #ifdef CONFIG_PNP struct pnp_dev *dev; #ifdef SNDRV_STB struct pnp_dev *devtc; #endif #endif }; #ifdef CONFIG_PNP static int isa_registered; static int pnp_registered; static const struct pnp_card_device_id snd_interwave_pnpids[] = { #ifndef SNDRV_STB /* Gravis UltraSound Plug & Play */ { .id = "GRV0001", .devs = { { .id = "GRV0000" } } }, /* STB SoundRage32 */ { .id = "STB011a", .devs = { { .id = "STB0010" } } }, /* MED3210 */ { .id = "DXP3201", .devs = { { .id = "DXP0010" } } }, /* Dynasonic Pro */ /* This device also have CDC1117:DynaSonix Pro Audio Effects Processor */ { .id = "CDC1111", .devs = { { .id = "CDC1112" } } }, /* Panasonic PCA761AW Audio Card */ { .id = "ADV55ff", .devs = { { .id = "ADV0010" } } }, /* InterWave STB without TEA6330T */ { .id = "ADV550a", .devs = { { .id = "ADV0010" } } }, #else /* InterWave STB with TEA6330T */ { .id = "ADV550a", .devs = { { .id = "ADV0010" }, { .id = "ADV0015" } } }, #endif { .id = "" } }; MODULE_DEVICE_TABLE(pnp_card, snd_interwave_pnpids); #endif /* CONFIG_PNP */ #ifdef SNDRV_STB static void snd_interwave_i2c_setlines(struct snd_i2c_bus *bus, int ctrl, int data) { unsigned long port = bus->private_value; #if 0 printk(KERN_DEBUG "i2c_setlines - 0x%lx <- %i,%i\n", port, ctrl, data); #endif outb((data << 1) | ctrl, port); udelay(10); } static int snd_interwave_i2c_getclockline(struct snd_i2c_bus *bus) { unsigned long port = bus->private_value; unsigned char res; res = inb(port) & 1; #if 0 printk(KERN_DEBUG "i2c_getclockline - 0x%lx -> %i\n", port, res); #endif return res; } static int snd_interwave_i2c_getdataline(struct snd_i2c_bus *bus, int ack) { unsigned long port = bus->private_value; unsigned char res; if (ack) udelay(10); res = (inb(port) & 2) >> 1; #if 0 printk(KERN_DEBUG "i2c_getdataline - 0x%lx -> %i\n", port, res); #endif return res; } static struct snd_i2c_bit_ops snd_interwave_i2c_bit_ops = { .setlines = snd_interwave_i2c_setlines, .getclock = snd_interwave_i2c_getclockline, .getdata = snd_interwave_i2c_getdataline, }; static int snd_interwave_detect_stb(struct snd_interwave *iwcard, struct snd_gus_card *gus, int dev, struct snd_i2c_bus **rbus) { unsigned long port; struct snd_i2c_bus *bus; struct snd_card *card = iwcard->card; char name[32]; int err; *rbus = NULL; port = port_tc[dev]; if (port == SNDRV_AUTO_PORT) { port = 0x350; if (gus->gf1.port == 0x250) { port = 0x360; } while (port <= 0x380) { iwcard->i2c_res = devm_request_region(card->dev, port, 1, "InterWave (I2C bus)"); if (iwcard->i2c_res) break; port += 0x10; } } else { iwcard->i2c_res = devm_request_region(card->dev, port, 1, "InterWave (I2C bus)"); } if (iwcard->i2c_res == NULL) { snd_printk(KERN_ERR "interwave: can't grab i2c bus port\n"); return -ENODEV; } sprintf(name, "InterWave-%i", card->number); err = snd_i2c_bus_create(card, name, NULL, &bus); if (err < 0) return err; bus->private_value = port; bus->hw_ops.bit = &snd_interwave_i2c_bit_ops; err = snd_tea6330t_detect(bus, 0); if (err < 0) return err; *rbus = bus; return 0; } #endif static int snd_interwave_detect(struct snd_interwave *iwcard, struct snd_gus_card *gus, int dev #ifdef SNDRV_STB , struct snd_i2c_bus **rbus #endif ) { unsigned long flags; unsigned char rev1, rev2; int d; snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 0) { snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); return -ENODEV; } udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ udelay(160); d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 1) { snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); return -ENODEV; } spin_lock_irqsave(&gus->reg_lock, flags); rev1 = snd_gf1_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER); snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, ~rev1); rev2 = snd_gf1_look8(gus, SNDRV_GF1_GB_VERSION_NUMBER); snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, rev1); spin_unlock_irqrestore(&gus->reg_lock, flags); snd_printdd("[0x%lx] InterWave check - rev1=0x%x, rev2=0x%x\n", gus->gf1.port, rev1, rev2); if ((rev1 & 0xf0) == (rev2 & 0xf0) && (rev1 & 0x0f) != (rev2 & 0x0f)) { snd_printdd("[0x%lx] InterWave check - passed\n", gus->gf1.port); gus->interwave = 1; strcpy(gus->card->shortname, "AMD InterWave"); gus->revision = rev1 >> 4; #ifndef SNDRV_STB return 0; /* ok.. We have an InterWave board */ #else return snd_interwave_detect_stb(iwcard, gus, dev, rbus); #endif } snd_printdd("[0x%lx] InterWave check - failed\n", gus->gf1.port); return -ENODEV; } static irqreturn_t snd_interwave_interrupt(int irq, void *dev_id) { struct snd_interwave *iwcard = dev_id; int loop, max = 5; int handled = 0; do { loop = 0; if (inb(iwcard->gus_status_reg)) { handled = 1; snd_gus_interrupt(irq, iwcard->gus); loop++; } if (inb(iwcard->pcm_status_reg) & 0x01) { /* IRQ bit is set? */ handled = 1; snd_wss_interrupt(irq, iwcard->wss); loop++; } } while (loop && --max > 0); return IRQ_RETVAL(handled); } static void snd_interwave_reset(struct snd_gus_card *gus) { snd_gf1_write8(gus, SNDRV_GF1_GB_RESET, 0x00); udelay(160); snd_gf1_write8(gus, SNDRV_GF1_GB_RESET, 0x01); udelay(160); } static void snd_interwave_bank_sizes(struct snd_gus_card *gus, int *sizes) { unsigned int idx; unsigned int local; unsigned char d; for (idx = 0; idx < 4; idx++) { sizes[idx] = 0; d = 0x55; for (local = idx << 22; local < (idx << 22) + 0x400000; local += 0x40000, d++) { snd_gf1_poke(gus, local, d); snd_gf1_poke(gus, local + 1, d + 1); #if 0 printk(KERN_DEBUG "d = 0x%x, local = 0x%x, " "local + 1 = 0x%x, idx << 22 = 0x%x\n", d, snd_gf1_peek(gus, local), snd_gf1_peek(gus, local + 1), snd_gf1_peek(gus, idx << 22)); #endif if (snd_gf1_peek(gus, local) != d || snd_gf1_peek(gus, local + 1) != d + 1 || snd_gf1_peek(gus, idx << 22) != 0x55) break; sizes[idx]++; } } #if 0 printk(KERN_DEBUG "sizes: %i %i %i %i\n", sizes[0], sizes[1], sizes[2], sizes[3]); #endif } struct rom_hdr { /* 000 */ unsigned char iwave[8]; /* 008 */ unsigned char rom_hdr_revision; /* 009 */ unsigned char series_number; /* 010 */ unsigned char series_name[16]; /* 026 */ unsigned char date[10]; /* 036 */ unsigned short vendor_revision_major; /* 038 */ unsigned short vendor_revision_minor; /* 040 */ unsigned int rom_size; /* 044 */ unsigned char copyright[128]; /* 172 */ unsigned char vendor_name[64]; /* 236 */ unsigned char rom_description[128]; /* 364 */ unsigned char pad[147]; /* 511 */ unsigned char csum; }; static void snd_interwave_detect_memory(struct snd_gus_card *gus) { static const unsigned int lmc[13] = { 0x00000001, 0x00000101, 0x01010101, 0x00000401, 0x04040401, 0x00040101, 0x04040101, 0x00000004, 0x00000404, 0x04040404, 0x00000010, 0x00001010, 0x10101010 }; int bank_pos, pages; unsigned int i, lmct; int psizes[4]; unsigned char iwave[8]; unsigned char csum; snd_interwave_reset(gus); snd_gf1_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_read8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01); /* enhanced mode */ snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01); /* DRAM I/O cycles selected */ snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xff10) | 0x004c); /* ok.. simple test of memory size */ pages = 0; snd_gf1_poke(gus, 0, 0x55); snd_gf1_poke(gus, 1, 0xaa); #if 1 if (snd_gf1_peek(gus, 0) == 0x55 && snd_gf1_peek(gus, 1) == 0xaa) #else if (0) /* ok.. for testing of 0k RAM */ #endif { snd_interwave_bank_sizes(gus, psizes); lmct = (psizes[3] << 24) | (psizes[2] << 16) | (psizes[1] << 8) | psizes[0]; #if 0 printk(KERN_DEBUG "lmct = 0x%08x\n", lmct); #endif for (i = 0; i < ARRAY_SIZE(lmc); i++) if (lmct == lmc[i]) { #if 0 printk(KERN_DEBUG "found !!! %i\n", i); #endif snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xfff0) | i); snd_interwave_bank_sizes(gus, psizes); break; } if (i >= ARRAY_SIZE(lmc) && !gus->gf1.enh_mode) snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xfff0) | 2); for (i = 0; i < 4; i++) { gus->gf1.mem_alloc.banks_8[i].address = gus->gf1.mem_alloc.banks_16[i].address = i << 22; gus->gf1.mem_alloc.banks_8[i].size = gus->gf1.mem_alloc.banks_16[i].size = psizes[i] << 18; pages += psizes[i]; } } pages <<= 18; gus->gf1.memory = pages; snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x03); /* select ROM */ snd_gf1_write16(gus, SNDRV_GF1_GW_MEMORY_CONFIG, (snd_gf1_look16(gus, SNDRV_GF1_GW_MEMORY_CONFIG) & 0xff1f) | (4 << 5)); gus->gf1.rom_banks = 0; gus->gf1.rom_memory = 0; for (bank_pos = 0; bank_pos < 16L * 1024L * 1024L; bank_pos += 4L * 1024L * 1024L) { for (i = 0; i < 8; ++i) iwave[i] = snd_gf1_peek(gus, bank_pos + i); if (strncmp(iwave, "INTRWAVE", 8)) continue; /* first check */ csum = 0; for (i = 0; i < sizeof(struct rom_hdr); i++) csum += snd_gf1_peek(gus, bank_pos + i); if (csum != 0) continue; /* not valid rom */ gus->gf1.rom_banks++; gus->gf1.rom_present |= 1 << (bank_pos >> 22); gus->gf1.rom_memory = snd_gf1_peek(gus, bank_pos + 40) | (snd_gf1_peek(gus, bank_pos + 41) << 8) | (snd_gf1_peek(gus, bank_pos + 42) << 16) | (snd_gf1_peek(gus, bank_pos + 43) << 24); } #if 0 if (gus->gf1.rom_memory > 0) { if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8) gus->card->type = SNDRV_CARD_TYPE_IW_DYNASONIC; } #endif snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x00); /* select RAM */ if (!gus->gf1.enh_mode) snd_interwave_reset(gus); } static void snd_interwave_init(int dev, struct snd_gus_card *gus) { unsigned long flags; /* ok.. some InterWave specific initialization */ spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0x00); snd_gf1_write8(gus, SNDRV_GF1_GB_COMPATIBILITY, 0x1f); snd_gf1_write8(gus, SNDRV_GF1_GB_DECODE_CONTROL, 0x49); snd_gf1_write8(gus, SNDRV_GF1_GB_VERSION_NUMBER, 0x11); snd_gf1_write8(gus, SNDRV_GF1_GB_MPU401_CONTROL_A, 0x00); snd_gf1_write8(gus, SNDRV_GF1_GB_MPU401_CONTROL_B, 0x30); snd_gf1_write8(gus, SNDRV_GF1_GB_EMULATION_IRQ, 0x00); spin_unlock_irqrestore(&gus->reg_lock, flags); gus->equal_irq = 1; gus->codec_flag = 1; gus->interwave = 1; gus->max_flag = 1; gus->joystick_dac = joystick_dac[dev]; } static const struct snd_kcontrol_new snd_interwave_controls[] = { WSS_DOUBLE("Master Playback Switch", 0, CS4231_LINE_LEFT_OUTPUT, CS4231_LINE_RIGHT_OUTPUT, 7, 7, 1, 1), WSS_DOUBLE("Master Playback Volume", 0, CS4231_LINE_LEFT_OUTPUT, CS4231_LINE_RIGHT_OUTPUT, 0, 0, 31, 1), WSS_DOUBLE("Mic Playback Switch", 0, CS4231_LEFT_MIC_INPUT, CS4231_RIGHT_MIC_INPUT, 7, 7, 1, 1), WSS_DOUBLE("Mic Playback Volume", 0, CS4231_LEFT_MIC_INPUT, CS4231_RIGHT_MIC_INPUT, 0, 0, 31, 1) }; static int snd_interwave_mixer(struct snd_wss *chip) { struct snd_card *card = chip->card; struct snd_ctl_elem_id id1, id2; unsigned int idx; int err; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; #if 0 /* remove mono microphone controls */ strcpy(id1.name, "Mic Playback Switch"); err = snd_ctl_remove_id(card, &id1); if (err < 0) return err; strcpy(id1.name, "Mic Playback Volume"); err = snd_ctl_remove_id(card, &id1); if (err < 0) return err; #endif /* add new master and mic controls */ for (idx = 0; idx < ARRAY_SIZE(snd_interwave_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_interwave_controls[idx], chip)); if (err < 0) return err; } snd_wss_out(chip, CS4231_LINE_LEFT_OUTPUT, 0x9f); snd_wss_out(chip, CS4231_LINE_RIGHT_OUTPUT, 0x9f); snd_wss_out(chip, CS4231_LEFT_MIC_INPUT, 0x9f); snd_wss_out(chip, CS4231_RIGHT_MIC_INPUT, 0x9f); /* reassign AUXA to SYNTHESIZER */ strcpy(id1.name, "Aux Playback Switch"); strcpy(id2.name, "Synth Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "Synth Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; /* reassign AUXB to CD */ strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; strcpy(id2.name, "CD Playback Switch"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "CD Playback Volume"); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; return 0; } #ifdef CONFIG_PNP static int snd_interwave_pnp(int dev, struct snd_interwave *iwcard, struct pnp_card_link *card, const struct pnp_card_device_id *id) { struct pnp_dev *pdev; int err; iwcard->dev = pnp_request_card_device(card, id->devs[0].id, NULL); if (iwcard->dev == NULL) return -EBUSY; #ifdef SNDRV_STB iwcard->devtc = pnp_request_card_device(card, id->devs[1].id, NULL); if (iwcard->devtc == NULL) return -EBUSY; #endif /* Synth & Codec initialization */ pdev = iwcard->dev; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR "InterWave PnP configure failure (out of resources?)\n"); return err; } if (pnp_port_start(pdev, 0) + 0x100 != pnp_port_start(pdev, 1) || pnp_port_start(pdev, 0) + 0x10c != pnp_port_start(pdev, 2)) { snd_printk(KERN_ERR "PnP configure failure (wrong ports)\n"); return -ENOENT; } port[dev] = pnp_port_start(pdev, 0); dma1[dev] = pnp_dma(pdev, 0); if (dma2[dev] >= 0) dma2[dev] = pnp_dma(pdev, 1); irq[dev] = pnp_irq(pdev, 0); snd_printdd("isapnp IW: sb port=0x%llx, gf1 port=0x%llx, codec port=0x%llx\n", (unsigned long long)pnp_port_start(pdev, 0), (unsigned long long)pnp_port_start(pdev, 1), (unsigned long long)pnp_port_start(pdev, 2)); snd_printdd("isapnp IW: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]); #ifdef SNDRV_STB /* Tone Control initialization */ pdev = iwcard->devtc; err = pnp_activate_dev(pdev); if (err < 0) { snd_printk(KERN_ERR "InterWave ToneControl PnP configure failure (out of resources?)\n"); return err; } port_tc[dev] = pnp_port_start(pdev, 0); snd_printdd("isapnp IW: tone control port=0x%lx\n", port_tc[dev]); #endif return 0; } #endif /* CONFIG_PNP */ static int snd_interwave_card_new(struct device *pdev, int dev, struct snd_card **cardp) { struct snd_card *card; struct snd_interwave *iwcard; int err; err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, sizeof(struct snd_interwave), &card); if (err < 0) return err; iwcard = card->private_data; iwcard->card = card; iwcard->irq = -1; *cardp = card; return 0; } static int snd_interwave_probe_gus(struct snd_card *card, int dev, struct snd_gus_card **gusp) { return snd_gus_create(card, port[dev], -irq[dev], dma1[dev], dma2[dev], 0, 32, pcm_channels[dev], effect[dev], gusp); } static int snd_interwave_probe(struct snd_card *card, int dev, struct snd_gus_card *gus) { int xirq, xdma1, xdma2; struct snd_interwave *iwcard = card->private_data; struct snd_wss *wss; #ifdef SNDRV_STB struct snd_i2c_bus *i2c_bus; #endif char *str; int err; xirq = irq[dev]; xdma1 = dma1[dev]; xdma2 = dma2[dev]; err = snd_interwave_detect(iwcard, gus, dev #ifdef SNDRV_STB , &i2c_bus #endif ); if (err < 0) return err; iwcard->gus_status_reg = gus->gf1.reg_irqstat; iwcard->pcm_status_reg = gus->gf1.port + 0x10c + 2; snd_interwave_init(dev, gus); snd_interwave_detect_memory(gus); err = snd_gus_initialize(gus); if (err < 0) return err; if (devm_request_irq(card->dev, xirq, snd_interwave_interrupt, 0, "InterWave", iwcard)) { snd_printk(KERN_ERR PFX "unable to grab IRQ %d\n", xirq); return -EBUSY; } iwcard->irq = xirq; card->sync_irq = iwcard->irq; err = snd_wss_create(card, gus->gf1.port + 0x10c, -1, xirq, xdma2 < 0 ? xdma1 : xdma2, xdma1, WSS_HW_INTERWAVE, WSS_HWSHARE_IRQ | WSS_HWSHARE_DMA1 | WSS_HWSHARE_DMA2, &wss); if (err < 0) return err; err = snd_wss_pcm(wss, 0); if (err < 0) return err; sprintf(wss->pcm->name + strlen(wss->pcm->name), " rev %c", gus->revision + 'A'); strcat(wss->pcm->name, " (codec)"); err = snd_wss_timer(wss, 2); if (err < 0) return err; err = snd_wss_mixer(wss); if (err < 0) return err; if (pcm_channels[dev] > 0) { err = snd_gf1_pcm_new(gus, 1, 1); if (err < 0) return err; } err = snd_interwave_mixer(wss); if (err < 0) return err; #ifdef SNDRV_STB { struct snd_ctl_elem_id id1, id2; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; strcpy(id1.name, "Master Playback Switch"); strcpy(id2.name, id1.name); id2.index = 1; err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; strcpy(id1.name, "Master Playback Volume"); strcpy(id2.name, id1.name); err = snd_ctl_rename_id(card, &id1, &id2); if (err < 0) return err; err = snd_tea6330t_update_mixer(card, i2c_bus, 0, 1); if (err < 0) return err; } #endif gus->uart_enable = midi[dev]; err = snd_gf1_rawmidi_new(gus, 0); if (err < 0) return err; #ifndef SNDRV_STB str = "AMD InterWave"; if (gus->gf1.rom_banks == 1 && gus->gf1.rom_present == 8) str = "Dynasonic 3-D"; #else str = "InterWave STB"; #endif strcpy(card->driver, str); strcpy(card->shortname, str); sprintf(card->longname, "%s at 0x%lx, irq %i, dma %d", str, gus->gf1.port, xirq, xdma1); if (xdma2 >= 0) sprintf(card->longname + strlen(card->longname), "&%d", xdma2); err = snd_card_register(card); if (err < 0) return err; iwcard->wss = wss; iwcard->gus = gus; return 0; } static int snd_interwave_isa_match(struct device *pdev, unsigned int dev) { if (!enable[dev]) return 0; #ifdef CONFIG_PNP if (isapnp[dev]) return 0; #endif return 1; } static int snd_interwave_isa_probe(struct device *pdev, unsigned int dev) { struct snd_card *card; struct snd_gus_card *gus; int err; static const int possible_irqs[] = {5, 11, 12, 9, 7, 15, 3, -1}; static const int possible_dmas[] = {0, 1, 3, 5, 6, 7, -1}; if (irq[dev] == SNDRV_AUTO_IRQ) { irq[dev] = snd_legacy_find_free_irq(possible_irqs); if (irq[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free IRQ\n"); return -EBUSY; } } if (dma1[dev] == SNDRV_AUTO_DMA) { dma1[dev] = snd_legacy_find_free_dma(possible_dmas); if (dma1[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA1\n"); return -EBUSY; } } if (dma2[dev] == SNDRV_AUTO_DMA) { dma2[dev] = snd_legacy_find_free_dma(possible_dmas); if (dma2[dev] < 0) { snd_printk(KERN_ERR PFX "unable to find a free DMA2\n"); return -EBUSY; } } err = snd_interwave_card_new(pdev, dev, &card); if (err < 0) return err; if (port[dev] != SNDRV_AUTO_PORT) err = snd_interwave_probe_gus(card, dev, &gus); else { static const long possible_ports[] = {0x210, 0x220, 0x230, 0x240, 0x250, 0x260}; int i; for (i = 0; i < ARRAY_SIZE(possible_ports); i++) { port[dev] = possible_ports[i]; err = snd_interwave_probe_gus(card, dev, &gus); if (! err) return 0; } } if (err < 0) return err; err = snd_interwave_probe(card, dev, gus); if (err < 0) return err; dev_set_drvdata(pdev, card); return 0; } static struct isa_driver snd_interwave_driver = { .match = snd_interwave_isa_match, .probe = snd_interwave_isa_probe, /* FIXME: suspend,resume */ .driver = { .name = INTERWAVE_DRIVER }, }; #ifdef CONFIG_PNP static int snd_interwave_pnp_detect(struct pnp_card_link *pcard, const struct pnp_card_device_id *pid) { static int dev; struct snd_card *card; struct snd_gus_card *gus; int res; for ( ; dev < SNDRV_CARDS; dev++) { if (enable[dev] && isapnp[dev]) break; } if (dev >= SNDRV_CARDS) return -ENODEV; res = snd_interwave_card_new(&pcard->card->dev, dev, &card); if (res < 0) return res; res = snd_interwave_pnp(dev, card->private_data, pcard, pid); if (res < 0) return res; res = snd_interwave_probe_gus(card, dev, &gus); if (res < 0) return res; res = snd_interwave_probe(card, dev, gus); if (res < 0) return res; pnp_set_card_drvdata(pcard, card); dev++; return 0; } static struct pnp_card_driver interwave_pnpc_driver = { .flags = PNP_DRIVER_RES_DISABLE, .name = INTERWAVE_PNP_DRIVER, .id_table = snd_interwave_pnpids, .probe = snd_interwave_pnp_detect, /* FIXME: suspend,resume */ }; #endif /* CONFIG_PNP */ static int __init alsa_card_interwave_init(void) { int err; err = isa_register_driver(&snd_interwave_driver, SNDRV_CARDS); #ifdef CONFIG_PNP if (!err) isa_registered = 1; err = pnp_register_card_driver(&interwave_pnpc_driver); if (!err) pnp_registered = 1; if (isa_registered) err = 0; #endif return err; } static void __exit alsa_card_interwave_exit(void) { #ifdef CONFIG_PNP if (pnp_registered) pnp_unregister_card_driver(&interwave_pnpc_driver); if (isa_registered) #endif isa_unregister_driver(&snd_interwave_driver); } module_init(alsa_card_interwave_init) module_exit(alsa_card_interwave_exit)
linux-master
sound/isa/gus/interwave.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Routines for GF1 DMA control * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <asm/dma.h> #include <linux/slab.h> #include <sound/core.h> #include <sound/gus.h> static void snd_gf1_dma_ack(struct snd_gus_card * gus) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_write8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL, 0x00); snd_gf1_look8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL); spin_unlock_irqrestore(&gus->reg_lock, flags); } static void snd_gf1_dma_program(struct snd_gus_card * gus, unsigned int addr, unsigned long buf_addr, unsigned int count, unsigned int cmd) { unsigned long flags; unsigned int address; unsigned char dma_cmd; unsigned int address_high; snd_printdd("dma_transfer: addr=0x%x, buf=0x%lx, count=0x%x\n", addr, buf_addr, count); if (gus->gf1.dma1 > 3) { if (gus->gf1.enh_mode) { address = addr >> 1; } else { if (addr & 0x1f) { snd_printd("snd_gf1_dma_transfer: unaligned address (0x%x)?\n", addr); return; } address = (addr & 0x000c0000) | ((addr & 0x0003ffff) >> 1); } } else { address = addr; } dma_cmd = SNDRV_GF1_DMA_ENABLE | (unsigned short) cmd; #if 0 dma_cmd |= 0x08; #endif if (dma_cmd & SNDRV_GF1_DMA_16BIT) { count++; count &= ~1; /* align */ } if (gus->gf1.dma1 > 3) { dma_cmd |= SNDRV_GF1_DMA_WIDTH16; count++; count &= ~1; /* align */ } snd_gf1_dma_ack(gus); snd_dma_program(gus->gf1.dma1, buf_addr, count, dma_cmd & SNDRV_GF1_DMA_READ ? DMA_MODE_READ : DMA_MODE_WRITE); #if 0 snd_printk(KERN_DEBUG "address = 0x%x, count = 0x%x, dma_cmd = 0x%x\n", address << 1, count, dma_cmd); #endif spin_lock_irqsave(&gus->reg_lock, flags); if (gus->gf1.enh_mode) { address_high = ((address >> 16) & 0x000000f0) | (address & 0x0000000f); snd_gf1_write16(gus, SNDRV_GF1_GW_DRAM_DMA_LOW, (unsigned short) (address >> 4)); snd_gf1_write8(gus, SNDRV_GF1_GB_DRAM_DMA_HIGH, (unsigned char) address_high); } else snd_gf1_write16(gus, SNDRV_GF1_GW_DRAM_DMA_LOW, (unsigned short) (address >> 4)); snd_gf1_write8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL, dma_cmd); spin_unlock_irqrestore(&gus->reg_lock, flags); } static struct snd_gf1_dma_block *snd_gf1_dma_next_block(struct snd_gus_card * gus) { struct snd_gf1_dma_block *block; /* PCM block have bigger priority than synthesizer one */ if (gus->gf1.dma_data_pcm) { block = gus->gf1.dma_data_pcm; if (gus->gf1.dma_data_pcm_last == block) { gus->gf1.dma_data_pcm = gus->gf1.dma_data_pcm_last = NULL; } else { gus->gf1.dma_data_pcm = block->next; } } else if (gus->gf1.dma_data_synth) { block = gus->gf1.dma_data_synth; if (gus->gf1.dma_data_synth_last == block) { gus->gf1.dma_data_synth = gus->gf1.dma_data_synth_last = NULL; } else { gus->gf1.dma_data_synth = block->next; } } else { block = NULL; } if (block) { gus->gf1.dma_ack = block->ack; gus->gf1.dma_private_data = block->private_data; } return block; } static void snd_gf1_dma_interrupt(struct snd_gus_card * gus) { struct snd_gf1_dma_block *block; snd_gf1_dma_ack(gus); if (gus->gf1.dma_ack) gus->gf1.dma_ack(gus, gus->gf1.dma_private_data); spin_lock(&gus->dma_lock); if (gus->gf1.dma_data_pcm == NULL && gus->gf1.dma_data_synth == NULL) { gus->gf1.dma_ack = NULL; gus->gf1.dma_flags &= ~SNDRV_GF1_DMA_TRIGGER; spin_unlock(&gus->dma_lock); return; } block = snd_gf1_dma_next_block(gus); spin_unlock(&gus->dma_lock); if (!block) return; snd_gf1_dma_program(gus, block->addr, block->buf_addr, block->count, (unsigned short) block->cmd); kfree(block); #if 0 snd_printd(KERN_DEBUG "program dma (IRQ) - " "addr = 0x%x, buffer = 0x%lx, count = 0x%x, cmd = 0x%x\n", block->addr, block->buf_addr, block->count, block->cmd); #endif } int snd_gf1_dma_init(struct snd_gus_card * gus) { mutex_lock(&gus->dma_mutex); gus->gf1.dma_shared++; if (gus->gf1.dma_shared > 1) { mutex_unlock(&gus->dma_mutex); return 0; } gus->gf1.interrupt_handler_dma_write = snd_gf1_dma_interrupt; gus->gf1.dma_data_pcm = gus->gf1.dma_data_pcm_last = gus->gf1.dma_data_synth = gus->gf1.dma_data_synth_last = NULL; mutex_unlock(&gus->dma_mutex); return 0; } int snd_gf1_dma_done(struct snd_gus_card * gus) { struct snd_gf1_dma_block *block; mutex_lock(&gus->dma_mutex); gus->gf1.dma_shared--; if (!gus->gf1.dma_shared) { snd_dma_disable(gus->gf1.dma1); snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_WRITE); snd_gf1_dma_ack(gus); while ((block = gus->gf1.dma_data_pcm)) { gus->gf1.dma_data_pcm = block->next; kfree(block); } while ((block = gus->gf1.dma_data_synth)) { gus->gf1.dma_data_synth = block->next; kfree(block); } gus->gf1.dma_data_pcm_last = gus->gf1.dma_data_synth_last = NULL; } mutex_unlock(&gus->dma_mutex); return 0; } int snd_gf1_dma_transfer_block(struct snd_gus_card * gus, struct snd_gf1_dma_block * __block, int atomic, int synth) { unsigned long flags; struct snd_gf1_dma_block *block; block = kmalloc(sizeof(*block), atomic ? GFP_ATOMIC : GFP_KERNEL); if (!block) return -ENOMEM; *block = *__block; block->next = NULL; snd_printdd("addr = 0x%x, buffer = 0x%lx, count = 0x%x, cmd = 0x%x\n", block->addr, (long) block->buffer, block->count, block->cmd); snd_printdd("gus->gf1.dma_data_pcm_last = 0x%lx\n", (long)gus->gf1.dma_data_pcm_last); snd_printdd("gus->gf1.dma_data_pcm = 0x%lx\n", (long)gus->gf1.dma_data_pcm); spin_lock_irqsave(&gus->dma_lock, flags); if (synth) { if (gus->gf1.dma_data_synth_last) { gus->gf1.dma_data_synth_last->next = block; gus->gf1.dma_data_synth_last = block; } else { gus->gf1.dma_data_synth = gus->gf1.dma_data_synth_last = block; } } else { if (gus->gf1.dma_data_pcm_last) { gus->gf1.dma_data_pcm_last->next = block; gus->gf1.dma_data_pcm_last = block; } else { gus->gf1.dma_data_pcm = gus->gf1.dma_data_pcm_last = block; } } if (!(gus->gf1.dma_flags & SNDRV_GF1_DMA_TRIGGER)) { gus->gf1.dma_flags |= SNDRV_GF1_DMA_TRIGGER; block = snd_gf1_dma_next_block(gus); spin_unlock_irqrestore(&gus->dma_lock, flags); if (block == NULL) return 0; snd_gf1_dma_program(gus, block->addr, block->buf_addr, block->count, (unsigned short) block->cmd); kfree(block); return 0; } spin_unlock_irqrestore(&gus->dma_lock, flags); return 0; }
linux-master
sound/isa/gus/gus_dma.c
#define SNDRV_STB #include "interwave.c"
linux-master
sound/isa/gus/interwave-stb.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * GUS's memory allocation routines / bottom layer */ #include <linux/slab.h> #include <linux/string.h> #include <sound/core.h> #include <sound/gus.h> #include <sound/info.h> #ifdef CONFIG_SND_DEBUG static void snd_gf1_mem_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer); #endif void snd_gf1_mem_lock(struct snd_gf1_mem * alloc, int xup) { if (!xup) { mutex_lock(&alloc->memory_mutex); } else { mutex_unlock(&alloc->memory_mutex); } } static struct snd_gf1_mem_block * snd_gf1_mem_xalloc(struct snd_gf1_mem *alloc, struct snd_gf1_mem_block *block, const char *name) { struct snd_gf1_mem_block *pblock, *nblock; nblock = kmalloc(sizeof(struct snd_gf1_mem_block), GFP_KERNEL); if (nblock == NULL) return NULL; *nblock = *block; nblock->name = kstrdup(name, GFP_KERNEL); if (!nblock->name) { kfree(nblock); return NULL; } pblock = alloc->first; while (pblock) { if (pblock->ptr > nblock->ptr) { nblock->prev = pblock->prev; nblock->next = pblock; pblock->prev = nblock; if (pblock == alloc->first) alloc->first = nblock; else nblock->prev->next = nblock; mutex_unlock(&alloc->memory_mutex); return nblock; } pblock = pblock->next; } nblock->next = NULL; if (alloc->last == NULL) { nblock->prev = NULL; alloc->first = alloc->last = nblock; } else { nblock->prev = alloc->last; alloc->last->next = nblock; alloc->last = nblock; } return nblock; } int snd_gf1_mem_xfree(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block) { if (block->share) { /* ok.. shared block */ block->share--; mutex_unlock(&alloc->memory_mutex); return 0; } if (alloc->first == block) { alloc->first = block->next; if (block->next) block->next->prev = NULL; } else { block->prev->next = block->next; if (block->next) block->next->prev = block->prev; } if (alloc->last == block) { alloc->last = block->prev; if (block->prev) block->prev->next = NULL; } else { block->next->prev = block->prev; if (block->prev) block->prev->next = block->next; } kfree(block->name); kfree(block); return 0; } static struct snd_gf1_mem_block *snd_gf1_mem_look(struct snd_gf1_mem * alloc, unsigned int address) { struct snd_gf1_mem_block *block; for (block = alloc->first; block; block = block->next) { if (block->ptr == address) { return block; } } return NULL; } static struct snd_gf1_mem_block *snd_gf1_mem_share(struct snd_gf1_mem * alloc, unsigned int *share_id) { struct snd_gf1_mem_block *block; if (!share_id[0] && !share_id[1] && !share_id[2] && !share_id[3]) return NULL; for (block = alloc->first; block; block = block->next) if (!memcmp(share_id, block->share_id, sizeof(block->share_id))) return block; return NULL; } static int snd_gf1_mem_find(struct snd_gf1_mem * alloc, struct snd_gf1_mem_block * block, unsigned int size, int w_16, int align) { struct snd_gf1_bank_info *info = w_16 ? alloc->banks_16 : alloc->banks_8; unsigned int idx, boundary; int size1; struct snd_gf1_mem_block *pblock; unsigned int ptr1, ptr2; if (w_16 && align < 2) align = 2; block->flags = w_16 ? SNDRV_GF1_MEM_BLOCK_16BIT : 0; block->owner = SNDRV_GF1_MEM_OWNER_DRIVER; block->share = 0; block->share_id[0] = block->share_id[1] = block->share_id[2] = block->share_id[3] = 0; block->name = NULL; block->prev = block->next = NULL; for (pblock = alloc->first, idx = 0; pblock; pblock = pblock->next) { while (pblock->ptr >= (boundary = info[idx].address + info[idx].size)) idx++; while (pblock->ptr + pblock->size >= (boundary = info[idx].address + info[idx].size)) idx++; ptr2 = boundary; if (pblock->next) { if (pblock->ptr + pblock->size == pblock->next->ptr) continue; if (pblock->next->ptr < boundary) ptr2 = pblock->next->ptr; } ptr1 = ALIGN(pblock->ptr + pblock->size, align); if (ptr1 >= ptr2) continue; size1 = ptr2 - ptr1; if ((int)size <= size1) { block->ptr = ptr1; block->size = size; return 0; } } while (++idx < 4) { if (size <= info[idx].size) { /* I assume that bank address is already aligned.. */ block->ptr = info[idx].address; block->size = size; return 0; } } return -ENOMEM; } struct snd_gf1_mem_block *snd_gf1_mem_alloc(struct snd_gf1_mem * alloc, int owner, char *name, int size, int w_16, int align, unsigned int *share_id) { struct snd_gf1_mem_block block, *nblock; snd_gf1_mem_lock(alloc, 0); if (share_id != NULL) { nblock = snd_gf1_mem_share(alloc, share_id); if (nblock != NULL) { if (size != (int)nblock->size) { /* TODO: remove in the future */ snd_printk(KERN_ERR "snd_gf1_mem_alloc - share: sizes differ\n"); goto __std; } nblock->share++; snd_gf1_mem_lock(alloc, 1); return NULL; } } __std: if (snd_gf1_mem_find(alloc, &block, size, w_16, align) < 0) { snd_gf1_mem_lock(alloc, 1); return NULL; } if (share_id != NULL) memcpy(&block.share_id, share_id, sizeof(block.share_id)); block.owner = owner; nblock = snd_gf1_mem_xalloc(alloc, &block, name); snd_gf1_mem_lock(alloc, 1); return nblock; } int snd_gf1_mem_free(struct snd_gf1_mem * alloc, unsigned int address) { int result; struct snd_gf1_mem_block *block; snd_gf1_mem_lock(alloc, 0); block = snd_gf1_mem_look(alloc, address); if (block) { result = snd_gf1_mem_xfree(alloc, block); snd_gf1_mem_lock(alloc, 1); return result; } snd_gf1_mem_lock(alloc, 1); return -EINVAL; } int snd_gf1_mem_init(struct snd_gus_card * gus) { struct snd_gf1_mem *alloc; struct snd_gf1_mem_block block; alloc = &gus->gf1.mem_alloc; mutex_init(&alloc->memory_mutex); alloc->first = alloc->last = NULL; if (!gus->gf1.memory) return 0; memset(&block, 0, sizeof(block)); block.owner = SNDRV_GF1_MEM_OWNER_DRIVER; if (gus->gf1.enh_mode) { block.ptr = 0; block.size = 1024; if (!snd_gf1_mem_xalloc(alloc, &block, "InterWave LFOs")) return -ENOMEM; } block.ptr = gus->gf1.default_voice_address; block.size = 4; if (!snd_gf1_mem_xalloc(alloc, &block, "Voice default (NULL's)")) return -ENOMEM; #ifdef CONFIG_SND_DEBUG snd_card_ro_proc_new(gus->card, "gusmem", gus, snd_gf1_mem_info_read); #endif return 0; } int snd_gf1_mem_done(struct snd_gus_card * gus) { struct snd_gf1_mem *alloc; struct snd_gf1_mem_block *block, *nblock; alloc = &gus->gf1.mem_alloc; block = alloc->first; while (block) { nblock = block->next; snd_gf1_mem_xfree(alloc, block); block = nblock; } return 0; } #ifdef CONFIG_SND_DEBUG static void snd_gf1_mem_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_gus_card *gus; struct snd_gf1_mem *alloc; struct snd_gf1_mem_block *block; unsigned int total, used; int i; gus = entry->private_data; alloc = &gus->gf1.mem_alloc; mutex_lock(&alloc->memory_mutex); snd_iprintf(buffer, "8-bit banks : \n "); for (i = 0; i < 4; i++) snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_8[i].address, alloc->banks_8[i].size >> 10, i + 1 < 4 ? "," : ""); snd_iprintf(buffer, "\n" "16-bit banks : \n "); for (i = total = 0; i < 4; i++) { snd_iprintf(buffer, "0x%06x (%04ik)%s", alloc->banks_16[i].address, alloc->banks_16[i].size >> 10, i + 1 < 4 ? "," : ""); total += alloc->banks_16[i].size; } snd_iprintf(buffer, "\n"); used = 0; for (block = alloc->first, i = 0; block; block = block->next, i++) { used += block->size; snd_iprintf(buffer, "Block %i onboard 0x%x size %i (0x%x):\n", i, block->ptr, block->size, block->size); if (block->share || block->share_id[0] || block->share_id[1] || block->share_id[2] || block->share_id[3]) snd_iprintf(buffer, " Share : %i [id0 0x%x] [id1 0x%x] [id2 0x%x] [id3 0x%x]\n", block->share, block->share_id[0], block->share_id[1], block->share_id[2], block->share_id[3]); snd_iprintf(buffer, " Flags :%s\n", block->flags & SNDRV_GF1_MEM_BLOCK_16BIT ? " 16-bit" : ""); snd_iprintf(buffer, " Owner : "); switch (block->owner) { case SNDRV_GF1_MEM_OWNER_DRIVER: snd_iprintf(buffer, "driver - %s\n", block->name); break; case SNDRV_GF1_MEM_OWNER_WAVE_SIMPLE: snd_iprintf(buffer, "SIMPLE wave\n"); break; case SNDRV_GF1_MEM_OWNER_WAVE_GF1: snd_iprintf(buffer, "GF1 wave\n"); break; case SNDRV_GF1_MEM_OWNER_WAVE_IWFFFF: snd_iprintf(buffer, "IWFFFF wave\n"); break; default: snd_iprintf(buffer, "unknown\n"); } } snd_iprintf(buffer, " Total: memory = %i, used = %i, free = %i\n", total, used, total - used); mutex_unlock(&alloc->memory_mutex); #if 0 ultra_iprintf(buffer, " Verify: free = %i, max 8-bit block = %i, max 16-bit block = %i\n", ultra_memory_free_size(card, &card->gf1.mem_alloc), ultra_memory_free_block(card, &card->gf1.mem_alloc, 0), ultra_memory_free_block(card, &card->gf1.mem_alloc, 1)); #endif } #endif
linux-master
sound/isa/gus/gus_mem.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/delay.h> #include <linux/interrupt.h> #include <linux/time.h> #include <sound/core.h> #include <sound/gus.h> extern int snd_gf1_synth_init(struct snd_gus_card * gus); extern void snd_gf1_synth_done(struct snd_gus_card * gus); /* * ok.. default interrupt handlers... */ static void snd_gf1_default_interrupt_handler_midi_out(struct snd_gus_card * gus) { snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x20); } static void snd_gf1_default_interrupt_handler_midi_in(struct snd_gus_card * gus) { snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x80); } static void snd_gf1_default_interrupt_handler_timer1(struct snd_gus_card * gus) { snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~4); } static void snd_gf1_default_interrupt_handler_timer2(struct snd_gus_card * gus) { snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~8); } static void snd_gf1_default_interrupt_handler_wave_and_volume(struct snd_gus_card * gus, struct snd_gus_voice * voice) { snd_gf1_i_ctrl_stop(gus, 0x00); snd_gf1_i_ctrl_stop(gus, 0x0d); } static void snd_gf1_default_interrupt_handler_dma_write(struct snd_gus_card * gus) { snd_gf1_i_write8(gus, 0x41, 0x00); } static void snd_gf1_default_interrupt_handler_dma_read(struct snd_gus_card * gus) { snd_gf1_i_write8(gus, 0x49, 0x00); } void snd_gf1_set_default_handlers(struct snd_gus_card * gus, unsigned int what) { if (what & SNDRV_GF1_HANDLER_MIDI_OUT) gus->gf1.interrupt_handler_midi_out = snd_gf1_default_interrupt_handler_midi_out; if (what & SNDRV_GF1_HANDLER_MIDI_IN) gus->gf1.interrupt_handler_midi_in = snd_gf1_default_interrupt_handler_midi_in; if (what & SNDRV_GF1_HANDLER_TIMER1) gus->gf1.interrupt_handler_timer1 = snd_gf1_default_interrupt_handler_timer1; if (what & SNDRV_GF1_HANDLER_TIMER2) gus->gf1.interrupt_handler_timer2 = snd_gf1_default_interrupt_handler_timer2; if (what & SNDRV_GF1_HANDLER_VOICE) { struct snd_gus_voice *voice; voice = &gus->gf1.voices[what & 0xffff]; voice->handler_wave = voice->handler_volume = snd_gf1_default_interrupt_handler_wave_and_volume; voice->handler_effect = NULL; voice->volume_change = NULL; } if (what & SNDRV_GF1_HANDLER_DMA_WRITE) gus->gf1.interrupt_handler_dma_write = snd_gf1_default_interrupt_handler_dma_write; if (what & SNDRV_GF1_HANDLER_DMA_READ) gus->gf1.interrupt_handler_dma_read = snd_gf1_default_interrupt_handler_dma_read; } /* */ static void snd_gf1_clear_regs(struct snd_gus_card * gus) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); inb(GUSP(gus, IRQSTAT)); snd_gf1_write8(gus, 0x41, 0); /* DRAM DMA Control Register */ snd_gf1_write8(gus, 0x45, 0); /* Timer Control */ snd_gf1_write8(gus, 0x49, 0); /* Sampling Control Register */ spin_unlock_irqrestore(&gus->reg_lock, flags); } static void snd_gf1_look_regs(struct snd_gus_card * gus) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_look8(gus, 0x41); /* DRAM DMA Control Register */ snd_gf1_look8(gus, 0x49); /* Sampling Control Register */ inb(GUSP(gus, IRQSTAT)); snd_gf1_read8(gus, 0x0f); /* IRQ Source Register */ spin_unlock_irqrestore(&gus->reg_lock, flags); } /* * put selected GF1 voices to initial stage... */ void snd_gf1_smart_stop_voice(struct snd_gus_card * gus, unsigned short voice) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_select_voice(gus, voice); #if 0 printk(KERN_DEBUG " -%i- smart stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME)); #endif snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL); snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); spin_unlock_irqrestore(&gus->reg_lock, flags); } void snd_gf1_stop_voice(struct snd_gus_card * gus, unsigned short voice) { unsigned long flags; spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_select_voice(gus, voice); #if 0 printk(KERN_DEBUG " -%i- stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME)); #endif snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL); snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); if (gus->gf1.enh_mode) snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0); spin_unlock_irqrestore(&gus->reg_lock, flags); #if 0 snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_VIBRATO); snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_TREMOLO); #endif } static void snd_gf1_clear_voices(struct snd_gus_card * gus, unsigned short v_min, unsigned short v_max) { unsigned long flags; unsigned int daddr; unsigned short i, w_16; daddr = gus->gf1.default_voice_address << 4; for (i = v_min; i <= v_max; i++) { #if 0 if (gus->gf1.syn_voices) gus->gf1.syn_voices[i].flags = ~VFLG_DYNAMIC; #endif spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_select_voice(gus, i); snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL); /* Voice Control Register = voice stop */ snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); /* Volume Ramp Control Register = ramp off */ if (gus->gf1.enh_mode) snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, gus->gf1.memory ? 0x02 : 0x82); /* Deactivate voice */ w_16 = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & 0x04; snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, 0x400); snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, daddr, w_16); snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, daddr, w_16); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, 0); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, 0); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0); snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, 0); snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, daddr, w_16); snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, 7); if (gus->gf1.enh_mode) { snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0); snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME, 0); snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME_FINAL, 0); } spin_unlock_irqrestore(&gus->reg_lock, flags); #if 0 snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_VIBRATO); snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_TREMOLO); #endif } } void snd_gf1_stop_voices(struct snd_gus_card * gus, unsigned short v_min, unsigned short v_max) { unsigned long flags; short i, ramp_ok; unsigned short ramp_end; if (!in_interrupt()) { /* this can't be done in interrupt */ for (i = v_min, ramp_ok = 0; i <= v_max; i++) { spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_select_voice(gus, i); ramp_end = snd_gf1_read16(gus, 9) >> 8; if (ramp_end > SNDRV_GF1_MIN_OFFSET) { ramp_ok++; snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 20); /* ramp rate */ snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET); /* ramp start */ snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, ramp_end); /* ramp end */ snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40); /* ramp down */ if (gus->gf1.enh_mode) { snd_gf1_delay(gus); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40); } } spin_unlock_irqrestore(&gus->reg_lock, flags); } msleep_interruptible(50); } snd_gf1_clear_voices(gus, v_min, v_max); } static void snd_gf1_alloc_voice_use(struct snd_gus_card * gus, struct snd_gus_voice * pvoice, int type, int client, int port) { pvoice->use = 1; switch (type) { case SNDRV_GF1_VOICE_TYPE_PCM: gus->gf1.pcm_alloc_voices++; pvoice->pcm = 1; break; case SNDRV_GF1_VOICE_TYPE_SYNTH: pvoice->synth = 1; pvoice->client = client; pvoice->port = port; break; case SNDRV_GF1_VOICE_TYPE_MIDI: pvoice->midi = 1; pvoice->client = client; pvoice->port = port; break; } } struct snd_gus_voice *snd_gf1_alloc_voice(struct snd_gus_card * gus, int type, int client, int port) { struct snd_gus_voice *pvoice; unsigned long flags; int idx; spin_lock_irqsave(&gus->voice_alloc, flags); if (type == SNDRV_GF1_VOICE_TYPE_PCM) { if (gus->gf1.pcm_alloc_voices >= gus->gf1.pcm_channels) { spin_unlock_irqrestore(&gus->voice_alloc, flags); return NULL; } } for (idx = 0; idx < 32; idx++) { pvoice = &gus->gf1.voices[idx]; if (!pvoice->use) { snd_gf1_alloc_voice_use(gus, pvoice, type, client, port); spin_unlock_irqrestore(&gus->voice_alloc, flags); return pvoice; } } for (idx = 0; idx < 32; idx++) { pvoice = &gus->gf1.voices[idx]; if (pvoice->midi && !pvoice->client) { snd_gf1_clear_voices(gus, pvoice->number, pvoice->number); snd_gf1_alloc_voice_use(gus, pvoice, type, client, port); spin_unlock_irqrestore(&gus->voice_alloc, flags); return pvoice; } } spin_unlock_irqrestore(&gus->voice_alloc, flags); return NULL; } void snd_gf1_free_voice(struct snd_gus_card * gus, struct snd_gus_voice *voice) { unsigned long flags; void (*private_free)(struct snd_gus_voice *voice); if (voice == NULL || !voice->use) return; snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | voice->number); snd_gf1_clear_voices(gus, voice->number, voice->number); spin_lock_irqsave(&gus->voice_alloc, flags); private_free = voice->private_free; voice->private_free = NULL; voice->private_data = NULL; if (voice->pcm) gus->gf1.pcm_alloc_voices--; voice->use = voice->pcm = 0; voice->sample_ops = NULL; spin_unlock_irqrestore(&gus->voice_alloc, flags); if (private_free) private_free(voice); } /* * call this function only by start of driver */ int snd_gf1_start(struct snd_gus_card * gus) { unsigned long flags; unsigned int i; snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* disable IRQ & DAC */ udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac); snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_ALL); for (i = 0; i < 32; i++) { gus->gf1.voices[i].number = i; snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | i); } snd_gf1_uart_cmd(gus, 0x03); /* huh.. this cleanup took me some time... */ if (gus->gf1.enh_mode) { /* enhanced mode !!!! */ snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01); snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01); } snd_gf1_clear_regs(gus); snd_gf1_select_active_voices(gus); snd_gf1_delay(gus); gus->gf1.default_voice_address = gus->gf1.memory > 0 ? 0 : 512 - 8; /* initialize LFOs & clear LFOs memory */ if (gus->gf1.enh_mode && gus->gf1.memory) { gus->gf1.hw_lfo = 1; gus->gf1.default_voice_address += 1024; } else { gus->gf1.sw_lfo = 1; } #if 0 snd_gf1_lfo_init(gus); #endif if (gus->gf1.memory > 0) for (i = 0; i < 4; i++) snd_gf1_poke(gus, gus->gf1.default_voice_address + i, 0); snd_gf1_clear_regs(gus); snd_gf1_clear_voices(gus, 0, 31); snd_gf1_look_regs(gus); udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7); /* Reset Register = IRQ enable, DAC enable */ udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7); /* Reset Register = IRQ enable, DAC enable */ if (gus->gf1.enh_mode) { /* enhanced mode !!!! */ snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01); snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01); } while ((snd_gf1_i_read8(gus, SNDRV_GF1_GB_VOICES_IRQ) & 0xc0) != 0xc0); spin_lock_irqsave(&gus->reg_lock, flags); outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE)); outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG)); spin_unlock_irqrestore(&gus->reg_lock, flags); snd_gf1_timers_init(gus); snd_gf1_look_regs(gus); snd_gf1_mem_init(gus); snd_gf1_mem_proc_init(gus); #ifdef CONFIG_SND_DEBUG snd_gus_irq_profile_init(gus); #endif #if 0 if (gus->pnp_flag) { if (gus->chip.playback_fifo_size > 0) snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_RECORD_BASE_ADDR, gus->chip.playback_fifo_block->ptr >> 8); if (gus->chip.record_fifo_size > 0) snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_PLAY_BASE_ADDR, gus->chip.record_fifo_block->ptr >> 8); snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_SIZE, gus->chip.interwave_fifo_reg); } #endif return 0; } /* * call this function only by shutdown of driver */ int snd_gf1_stop(struct snd_gus_card * gus) { snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0); /* stop all timers */ snd_gf1_stop_voices(gus, 0, 31); /* stop all voices */ snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* disable IRQ & DAC */ snd_gf1_timers_done(gus); snd_gf1_mem_done(gus); #if 0 snd_gf1_lfo_done(gus); #endif return 0; }
linux-master
sound/isa/gus/gus_reset.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of GF1 chip (PCM things) * * InterWave chips supports interleaved DMA, but this feature isn't used in * this code. * * This code emulates autoinit DMA transfer for playback, recording by GF1 * chip doesn't support autoinit DMA. */ #include <asm/dma.h> #include <linux/slab.h> #include <linux/sched/signal.h> #include <sound/core.h> #include <sound/control.h> #include <sound/gus.h> #include <sound/pcm_params.h> #include "gus_tables.h" /* maximum rate */ #define SNDRV_GF1_PCM_RATE 48000 #define SNDRV_GF1_PCM_PFLG_NONE 0 #define SNDRV_GF1_PCM_PFLG_ACTIVE (1<<0) #define SNDRV_GF1_PCM_PFLG_NEUTRAL (2<<0) struct gus_pcm_private { struct snd_gus_card * gus; struct snd_pcm_substream *substream; spinlock_t lock; unsigned int voices; struct snd_gus_voice *pvoices[2]; unsigned int memory; unsigned short flags; unsigned char voice_ctrl, ramp_ctrl; unsigned int bpos; unsigned int blocks; unsigned int block_size; unsigned int dma_size; wait_queue_head_t sleep; atomic_t dma_count; int final_volume; }; static void snd_gf1_pcm_block_change_ack(struct snd_gus_card * gus, void *private_data) { struct gus_pcm_private *pcmp = private_data; if (pcmp) { atomic_dec(&pcmp->dma_count); wake_up(&pcmp->sleep); } } static int snd_gf1_pcm_block_change(struct snd_pcm_substream *substream, unsigned int offset, unsigned int addr, unsigned int count) { struct snd_gf1_dma_block block; struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; count += offset & 31; offset &= ~31; /* snd_printk(KERN_DEBUG "block change - offset = 0x%x, count = 0x%x\n", offset, count); */ memset(&block, 0, sizeof(block)); block.cmd = SNDRV_GF1_DMA_IRQ; if (snd_pcm_format_unsigned(runtime->format)) block.cmd |= SNDRV_GF1_DMA_UNSIGNED; if (snd_pcm_format_width(runtime->format) == 16) block.cmd |= SNDRV_GF1_DMA_16BIT; block.addr = addr & ~31; block.buffer = runtime->dma_area + offset; block.buf_addr = runtime->dma_addr + offset; block.count = count; block.private_data = pcmp; block.ack = snd_gf1_pcm_block_change_ack; if (!snd_gf1_dma_transfer_block(pcmp->gus, &block, 0, 0)) atomic_inc(&pcmp->dma_count); return 0; } static void snd_gf1_pcm_trigger_up(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; struct snd_gus_card * gus = pcmp->gus; unsigned long flags; unsigned char voice_ctrl, ramp_ctrl; unsigned short rate; unsigned int curr, begin, end; unsigned short vol; unsigned char pan; unsigned int voice; spin_lock_irqsave(&pcmp->lock, flags); if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) { spin_unlock_irqrestore(&pcmp->lock, flags); return; } pcmp->flags |= SNDRV_GF1_PCM_PFLG_ACTIVE; pcmp->final_volume = 0; spin_unlock_irqrestore(&pcmp->lock, flags); rate = snd_gf1_translate_freq(gus, runtime->rate << 4); /* enable WAVE IRQ */ voice_ctrl = snd_pcm_format_width(runtime->format) == 16 ? 0x24 : 0x20; /* enable RAMP IRQ + rollover */ ramp_ctrl = 0x24; if (pcmp->blocks == 1) { voice_ctrl |= 0x08; /* loop enable */ ramp_ctrl &= ~0x04; /* disable rollover */ } for (voice = 0; voice < pcmp->voices; voice++) { begin = pcmp->memory + voice * (pcmp->dma_size / runtime->channels); curr = begin + (pcmp->bpos * pcmp->block_size) / runtime->channels; end = curr + (pcmp->block_size / runtime->channels); end -= snd_pcm_format_width(runtime->format) == 16 ? 2 : 1; /* snd_printk(KERN_DEBUG "init: curr=0x%x, begin=0x%x, end=0x%x, " "ctrl=0x%x, ramp=0x%x, rate=0x%x\n", curr, begin, end, voice_ctrl, ramp_ctrl, rate); */ pan = runtime->channels == 2 ? (!voice ? 1 : 14) : 8; vol = !voice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right; spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number); snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, pan); snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, rate); snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, begin << 4, voice_ctrl & 4); snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4); snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, curr << 4, voice_ctrl & 4); snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, SNDRV_GF1_MIN_VOLUME << 4); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0x2f); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, vol >> 8); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl); if (!gus->gf1.enh_mode) { snd_gf1_delay(gus); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl); } spin_unlock_irqrestore(&gus->reg_lock, flags); } spin_lock_irqsave(&gus->reg_lock, flags); for (voice = 0; voice < pcmp->voices; voice++) { snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number); if (gus->gf1.enh_mode) snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, 0x00); /* deactivate voice */ snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl); voice_ctrl &= ~0x20; } voice_ctrl |= 0x20; if (!gus->gf1.enh_mode) { snd_gf1_delay(gus); for (voice = 0; voice < pcmp->voices; voice++) { snd_gf1_select_voice(gus, pcmp->pvoices[voice]->number); snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl); voice_ctrl &= ~0x20; /* disable IRQ for next voice */ } } spin_unlock_irqrestore(&gus->reg_lock, flags); } static void snd_gf1_pcm_interrupt_wave(struct snd_gus_card * gus, struct snd_gus_voice *pvoice) { struct gus_pcm_private * pcmp; struct snd_pcm_runtime *runtime; unsigned char voice_ctrl, ramp_ctrl; unsigned int idx; unsigned int end, step; if (!pvoice->private_data) { snd_printd("snd_gf1_pcm: unknown wave irq?\n"); snd_gf1_smart_stop_voice(gus, pvoice->number); return; } pcmp = pvoice->private_data; if (pcmp == NULL) { snd_printd("snd_gf1_pcm: unknown wave irq?\n"); snd_gf1_smart_stop_voice(gus, pvoice->number); return; } gus = pcmp->gus; runtime = pcmp->substream->runtime; spin_lock(&gus->reg_lock); snd_gf1_select_voice(gus, pvoice->number); voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & ~0x8b; ramp_ctrl = (snd_gf1_read8(gus, SNDRV_GF1_VB_VOLUME_CONTROL) & ~0xa4) | 0x03; #if 0 snd_gf1_select_voice(gus, pvoice->number); printk(KERN_DEBUG "position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4)); snd_gf1_select_voice(gus, pcmp->pvoices[1]->number); printk(KERN_DEBUG "position = 0x%x\n", (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4)); snd_gf1_select_voice(gus, pvoice->number); #endif pcmp->bpos++; pcmp->bpos %= pcmp->blocks; if (pcmp->bpos + 1 >= pcmp->blocks) { /* last block? */ voice_ctrl |= 0x08; /* enable loop */ } else { ramp_ctrl |= 0x04; /* enable rollover */ } end = pcmp->memory + (((pcmp->bpos + 1) * pcmp->block_size) / runtime->channels); end -= voice_ctrl & 4 ? 2 : 1; step = pcmp->dma_size / runtime->channels; voice_ctrl |= 0x20; if (!pcmp->final_volume) { ramp_ctrl |= 0x20; ramp_ctrl &= ~0x03; } for (idx = 0; idx < pcmp->voices; idx++, end += step) { snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number); snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, end << 4, voice_ctrl & 4); snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl); voice_ctrl &= ~0x20; } if (!gus->gf1.enh_mode) { snd_gf1_delay(gus); voice_ctrl |= 0x20; for (idx = 0; idx < pcmp->voices; idx++) { snd_gf1_select_voice(gus, pcmp->pvoices[idx]->number); snd_gf1_write8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL, voice_ctrl); snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, ramp_ctrl); voice_ctrl &= ~0x20; } } spin_unlock(&gus->reg_lock); snd_pcm_period_elapsed(pcmp->substream); #if 0 if ((runtime->flags & SNDRV_PCM_FLG_MMAP) && *runtime->state == SNDRV_PCM_STATE_RUNNING) { end = pcmp->bpos * pcmp->block_size; if (runtime->channels > 1) { snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + (end / 2), pcmp->block_size / 2); snd_gf1_pcm_block_change(pcmp->substream, end + (pcmp->block_size / 2), pcmp->memory + (pcmp->dma_size / 2) + (end / 2), pcmp->block_size / 2); } else { snd_gf1_pcm_block_change(pcmp->substream, end, pcmp->memory + end, pcmp->block_size); } } #endif } static void snd_gf1_pcm_interrupt_volume(struct snd_gus_card * gus, struct snd_gus_voice * pvoice) { unsigned short vol; int cvoice; struct gus_pcm_private *pcmp = pvoice->private_data; /* stop ramp, but leave rollover bit untouched */ spin_lock(&gus->reg_lock); snd_gf1_select_voice(gus, pvoice->number); snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); spin_unlock(&gus->reg_lock); if (pcmp == NULL) return; /* are we active? */ if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE)) return; /* load real volume - better precision */ cvoice = pcmp->pvoices[0] == pvoice ? 0 : 1; if (pcmp->substream == NULL) return; vol = !cvoice ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right; spin_lock(&gus->reg_lock); snd_gf1_select_voice(gus, pvoice->number); snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol); pcmp->final_volume = 1; spin_unlock(&gus->reg_lock); } static void snd_gf1_pcm_volume_change(struct snd_gus_card * gus) { } static int snd_gf1_pcm_poke_block(struct snd_gus_card *gus, unsigned char *buf, unsigned int pos, unsigned int count, int w16, int invert) { unsigned int len; unsigned long flags; /* printk(KERN_DEBUG "poke block; buf = 0x%x, pos = %i, count = %i, port = 0x%x\n", (int)buf, pos, count, gus->gf1.port); */ while (count > 0) { len = count; if (len > 512) /* limit, to allow IRQ */ len = 512; count -= len; if (gus->interwave) { spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01 | (invert ? 0x08 : 0x00)); snd_gf1_dram_addr(gus, pos); if (w16) { outb(SNDRV_GF1_GW_DRAM_IO16, GUSP(gus, GF1REGSEL)); outsw(GUSP(gus, GF1DATALOW), buf, len >> 1); } else { outsb(GUSP(gus, DRAM), buf, len); } spin_unlock_irqrestore(&gus->reg_lock, flags); buf += 512; pos += 512; } else { invert = invert ? 0x80 : 0x00; if (w16) { len >>= 1; while (len--) { snd_gf1_poke(gus, pos++, *buf++); snd_gf1_poke(gus, pos++, *buf++ ^ invert); } } else { while (len--) snd_gf1_poke(gus, pos++, *buf++ ^ invert); } } if (count > 0 && !in_interrupt()) { schedule_timeout_interruptible(1); if (signal_pending(current)) return -EAGAIN; } } return 0; } static int get_bpos(struct gus_pcm_private *pcmp, int voice, unsigned int pos, unsigned int len) { unsigned int bpos = pos + (voice * (pcmp->dma_size / 2)); if (snd_BUG_ON(bpos > pcmp->dma_size)) return -EIO; if (snd_BUG_ON(bpos + len > pcmp->dma_size)) return -EIO; return bpos; } static int playback_copy_ack(struct snd_pcm_substream *substream, unsigned int bpos, unsigned int len) { struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; struct snd_gus_card *gus = pcmp->gus; int w16, invert; if (len > 32) return snd_gf1_pcm_block_change(substream, bpos, pcmp->memory + bpos, len); w16 = (snd_pcm_format_width(runtime->format) == 16); invert = snd_pcm_format_unsigned(runtime->format); return snd_gf1_pcm_poke_block(gus, runtime->dma_area + bpos, pcmp->memory + bpos, len, w16, invert); } static int snd_gf1_pcm_playback_copy(struct snd_pcm_substream *substream, int voice, unsigned long pos, struct iov_iter *src, unsigned long count) { struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; unsigned int len = count; int bpos; bpos = get_bpos(pcmp, voice, pos, len); if (bpos < 0) return pos; if (copy_from_iter(runtime->dma_area + bpos, len, src) != len) return -EFAULT; return playback_copy_ack(substream, bpos, len); } static int snd_gf1_pcm_playback_silence(struct snd_pcm_substream *substream, int voice, unsigned long pos, unsigned long count) { struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; unsigned int len = count; int bpos; bpos = get_bpos(pcmp, voice, pos, len); if (bpos < 0) return pos; snd_pcm_format_set_silence(runtime->format, runtime->dma_area + bpos, bytes_to_samples(runtime, count)); return playback_copy_ack(substream, bpos, len); } static int snd_gf1_pcm_playback_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; if (runtime->buffer_changed) { struct snd_gf1_mem_block *block; if (pcmp->memory > 0) { snd_gf1_mem_free(&gus->gf1.mem_alloc, pcmp->memory); pcmp->memory = 0; } block = snd_gf1_mem_alloc(&gus->gf1.mem_alloc, SNDRV_GF1_MEM_OWNER_DRIVER, "GF1 PCM", runtime->dma_bytes, 1, 32, NULL); if (!block) return -ENOMEM; pcmp->memory = block->ptr; } pcmp->voices = params_channels(hw_params); if (pcmp->pvoices[0] == NULL) { pcmp->pvoices[0] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0); if (!pcmp->pvoices[0]) return -ENOMEM; pcmp->pvoices[0]->handler_wave = snd_gf1_pcm_interrupt_wave; pcmp->pvoices[0]->handler_volume = snd_gf1_pcm_interrupt_volume; pcmp->pvoices[0]->volume_change = snd_gf1_pcm_volume_change; pcmp->pvoices[0]->private_data = pcmp; } if (pcmp->voices > 1 && pcmp->pvoices[1] == NULL) { pcmp->pvoices[1] = snd_gf1_alloc_voice(pcmp->gus, SNDRV_GF1_VOICE_TYPE_PCM, 0, 0); if (!pcmp->pvoices[1]) return -ENOMEM; pcmp->pvoices[1]->handler_wave = snd_gf1_pcm_interrupt_wave; pcmp->pvoices[1]->handler_volume = snd_gf1_pcm_interrupt_volume; pcmp->pvoices[1]->volume_change = snd_gf1_pcm_volume_change; pcmp->pvoices[1]->private_data = pcmp; } else if (pcmp->voices == 1) { if (pcmp->pvoices[1]) { snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]); pcmp->pvoices[1] = NULL; } } return 0; } static int snd_gf1_pcm_playback_hw_free(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; if (pcmp->pvoices[0]) { snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[0]); pcmp->pvoices[0] = NULL; } if (pcmp->pvoices[1]) { snd_gf1_free_voice(pcmp->gus, pcmp->pvoices[1]); pcmp->pvoices[1] = NULL; } if (pcmp->memory > 0) { snd_gf1_mem_free(&pcmp->gus->gf1.mem_alloc, pcmp->memory); pcmp->memory = 0; } return 0; } static int snd_gf1_pcm_playback_prepare(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; pcmp->bpos = 0; pcmp->dma_size = snd_pcm_lib_buffer_bytes(substream); pcmp->block_size = snd_pcm_lib_period_bytes(substream); pcmp->blocks = pcmp->dma_size / pcmp->block_size; return 0; } static int snd_gf1_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; int voice; if (cmd == SNDRV_PCM_TRIGGER_START) { snd_gf1_pcm_trigger_up(substream); } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { spin_lock(&pcmp->lock); pcmp->flags &= ~SNDRV_GF1_PCM_PFLG_ACTIVE; spin_unlock(&pcmp->lock); voice = pcmp->pvoices[0]->number; snd_gf1_stop_voices(gus, voice, voice); if (pcmp->pvoices[1]) { voice = pcmp->pvoices[1]->number; snd_gf1_stop_voices(gus, voice, voice); } } else { return -EINVAL; } return 0; } static snd_pcm_uframes_t snd_gf1_pcm_playback_pointer(struct snd_pcm_substream *substream) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; unsigned int pos; unsigned char voice_ctrl; pos = 0; spin_lock(&gus->reg_lock); if (pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE) { snd_gf1_select_voice(gus, pcmp->pvoices[0]->number); voice_ctrl = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL); pos = (snd_gf1_read_addr(gus, SNDRV_GF1_VA_CURRENT, voice_ctrl & 4) >> 4) - pcmp->memory; if (substream->runtime->channels > 1) pos <<= 1; pos = bytes_to_frames(runtime, pos); } spin_unlock(&gus->reg_lock); return pos; } static const struct snd_ratnum clock = { .num = 9878400/16, .den_min = 2, .den_max = 257, .den_step = 1, }; static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = { .nrats = 1, .rats = &clock, }; static int snd_gf1_pcm_capture_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); gus->c_dma_size = params_buffer_bytes(hw_params); gus->c_period_size = params_period_bytes(hw_params); gus->c_pos = 0; gus->gf1.pcm_rcntrl_reg = 0x21; /* IRQ at end, enable & start */ if (params_channels(hw_params) > 1) gus->gf1.pcm_rcntrl_reg |= 2; if (gus->gf1.dma2 > 3) gus->gf1.pcm_rcntrl_reg |= 4; if (snd_pcm_format_unsigned(params_format(hw_params))) gus->gf1.pcm_rcntrl_reg |= 0x80; return 0; } static int snd_gf1_pcm_capture_prepare(struct snd_pcm_substream *substream) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; snd_gf1_i_write8(gus, SNDRV_GF1_GB_RECORD_RATE, runtime->rate_den - 2); snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */ snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */ snd_dma_program(gus->gf1.dma2, runtime->dma_addr, gus->c_period_size, DMA_MODE_READ); return 0; } static int snd_gf1_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); int val; if (cmd == SNDRV_PCM_TRIGGER_START) { val = gus->gf1.pcm_rcntrl_reg; } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { val = 0; } else { return -EINVAL; } spin_lock(&gus->reg_lock); snd_gf1_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, val); snd_gf1_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); spin_unlock(&gus->reg_lock); return 0; } static snd_pcm_uframes_t snd_gf1_pcm_capture_pointer(struct snd_pcm_substream *substream) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); int pos = snd_dma_pointer(gus->gf1.dma2, gus->c_period_size); pos = bytes_to_frames(substream->runtime, (gus->c_pos + pos) % gus->c_dma_size); return pos; } static void snd_gf1_pcm_interrupt_dma_read(struct snd_gus_card * gus) { snd_gf1_i_write8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL, 0); /* disable sampling */ snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL); /* Sampling Control Register */ if (gus->pcm_cap_substream != NULL) { snd_gf1_pcm_capture_prepare(gus->pcm_cap_substream); snd_gf1_pcm_capture_trigger(gus->pcm_cap_substream, SNDRV_PCM_TRIGGER_START); gus->c_pos += gus->c_period_size; snd_pcm_period_elapsed(gus->pcm_cap_substream); } } static const struct snd_pcm_hardware snd_gf1_pcm_playback = { .info = SNDRV_PCM_INFO_NONINTERLEAVED, .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE), .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 5510, .rate_max = 48000, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static const struct snd_pcm_hardware snd_gf1_pcm_capture = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_44100, .rate_min = 5510, .rate_max = 44100, .channels_min = 1, .channels_max = 2, .buffer_bytes_max = (128*1024), .period_bytes_min = 64, .period_bytes_max = (128*1024), .periods_min = 1, .periods_max = 1024, .fifo_size = 0, }; static void snd_gf1_pcm_playback_free(struct snd_pcm_runtime *runtime) { kfree(runtime->private_data); } static int snd_gf1_pcm_playback_open(struct snd_pcm_substream *substream) { struct gus_pcm_private *pcmp; struct snd_gus_card *gus = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int err; pcmp = kzalloc(sizeof(*pcmp), GFP_KERNEL); if (pcmp == NULL) return -ENOMEM; pcmp->gus = gus; spin_lock_init(&pcmp->lock); init_waitqueue_head(&pcmp->sleep); atomic_set(&pcmp->dma_count, 0); runtime->private_data = pcmp; runtime->private_free = snd_gf1_pcm_playback_free; #if 0 printk(KERN_DEBUG "playback.buffer = 0x%lx, gf1.pcm_buffer = 0x%lx\n", (long) pcm->playback.buffer, (long) gus->gf1.pcm_buffer); #endif err = snd_gf1_dma_init(gus); if (err < 0) return err; pcmp->flags = SNDRV_GF1_PCM_PFLG_NONE; pcmp->substream = substream; runtime->hw = snd_gf1_pcm_playback; snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.buffer_bytes_max); snd_pcm_limit_isa_dma_size(gus->gf1.dma1, &runtime->hw.period_bytes_max); snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64); return 0; } static int snd_gf1_pcm_playback_close(struct snd_pcm_substream *substream) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; struct gus_pcm_private *pcmp = runtime->private_data; if (!wait_event_timeout(pcmp->sleep, (atomic_read(&pcmp->dma_count) <= 0), 2*HZ)) snd_printk(KERN_ERR "gf1 pcm - serious DMA problem\n"); snd_gf1_dma_done(gus); return 0; } static int snd_gf1_pcm_capture_open(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct snd_gus_card *gus = snd_pcm_substream_chip(substream); gus->gf1.interrupt_handler_dma_read = snd_gf1_pcm_interrupt_dma_read; gus->pcm_cap_substream = substream; substream->runtime->hw = snd_gf1_pcm_capture; snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.buffer_bytes_max); snd_pcm_limit_isa_dma_size(gus->gf1.dma2, &runtime->hw.period_bytes_max); snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_clocks); return 0; } static int snd_gf1_pcm_capture_close(struct snd_pcm_substream *substream) { struct snd_gus_card *gus = snd_pcm_substream_chip(substream); gus->pcm_cap_substream = NULL; snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_READ); return 0; } static int snd_gf1_pcm_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = 127; return 0; } static int snd_gf1_pcm_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); unsigned long flags; spin_lock_irqsave(&gus->pcm_volume_level_lock, flags); ucontrol->value.integer.value[0] = gus->gf1.pcm_volume_level_left1; ucontrol->value.integer.value[1] = gus->gf1.pcm_volume_level_right1; spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags); return 0; } static int snd_gf1_pcm_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); unsigned long flags; int change; unsigned int idx; unsigned short val1, val2, vol; struct gus_pcm_private *pcmp; struct snd_gus_voice *pvoice; val1 = ucontrol->value.integer.value[0] & 127; val2 = ucontrol->value.integer.value[1] & 127; spin_lock_irqsave(&gus->pcm_volume_level_lock, flags); change = val1 != gus->gf1.pcm_volume_level_left1 || val2 != gus->gf1.pcm_volume_level_right1; gus->gf1.pcm_volume_level_left1 = val1; gus->gf1.pcm_volume_level_right1 = val2; gus->gf1.pcm_volume_level_left = snd_gf1_lvol_to_gvol_raw(val1 << 9) << 4; gus->gf1.pcm_volume_level_right = snd_gf1_lvol_to_gvol_raw(val2 << 9) << 4; spin_unlock_irqrestore(&gus->pcm_volume_level_lock, flags); /* are we active? */ spin_lock_irqsave(&gus->voice_alloc, flags); for (idx = 0; idx < 32; idx++) { pvoice = &gus->gf1.voices[idx]; if (!pvoice->pcm) continue; pcmp = pvoice->private_data; if (!(pcmp->flags & SNDRV_GF1_PCM_PFLG_ACTIVE)) continue; /* load real volume - better precision */ spin_lock(&gus->reg_lock); snd_gf1_select_voice(gus, pvoice->number); snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); vol = pvoice == pcmp->pvoices[0] ? gus->gf1.pcm_volume_level_left : gus->gf1.pcm_volume_level_right; snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, vol); pcmp->final_volume = 1; spin_unlock(&gus->reg_lock); } spin_unlock_irqrestore(&gus->voice_alloc, flags); return change; } static const struct snd_kcontrol_new snd_gf1_pcm_volume_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "PCM Playback Volume", .info = snd_gf1_pcm_volume_info, .get = snd_gf1_pcm_volume_get, .put = snd_gf1_pcm_volume_put }; static const struct snd_kcontrol_new snd_gf1_pcm_volume_control1 = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "GPCM Playback Volume", .info = snd_gf1_pcm_volume_info, .get = snd_gf1_pcm_volume_get, .put = snd_gf1_pcm_volume_put }; static const struct snd_pcm_ops snd_gf1_pcm_playback_ops = { .open = snd_gf1_pcm_playback_open, .close = snd_gf1_pcm_playback_close, .hw_params = snd_gf1_pcm_playback_hw_params, .hw_free = snd_gf1_pcm_playback_hw_free, .prepare = snd_gf1_pcm_playback_prepare, .trigger = snd_gf1_pcm_playback_trigger, .pointer = snd_gf1_pcm_playback_pointer, .copy = snd_gf1_pcm_playback_copy, .fill_silence = snd_gf1_pcm_playback_silence, }; static const struct snd_pcm_ops snd_gf1_pcm_capture_ops = { .open = snd_gf1_pcm_capture_open, .close = snd_gf1_pcm_capture_close, .hw_params = snd_gf1_pcm_capture_hw_params, .prepare = snd_gf1_pcm_capture_prepare, .trigger = snd_gf1_pcm_capture_trigger, .pointer = snd_gf1_pcm_capture_pointer, }; int snd_gf1_pcm_new(struct snd_gus_card *gus, int pcm_dev, int control_index) { struct snd_card *card; struct snd_kcontrol *kctl; struct snd_pcm *pcm; struct snd_pcm_substream *substream; int capture, err; card = gus->card; capture = !gus->interwave && !gus->ess_flag && !gus->ace_flag ? 1 : 0; err = snd_pcm_new(card, gus->interwave ? "AMD InterWave" : "GF1", pcm_dev, gus->gf1.pcm_channels / 2, capture, &pcm); if (err < 0) return err; pcm->private_data = gus; /* playback setup */ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_gf1_pcm_playback_ops); for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next) snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, gus->gf1.dma1 > 3 ? 128*1024 : 64*1024); pcm->info_flags = 0; pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; if (capture) { snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_gf1_pcm_capture_ops); if (gus->gf1.dma2 == gus->gf1.dma1) pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX; snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, SNDRV_DMA_TYPE_DEV, card->dev, 64*1024, gus->gf1.dma2 > 3 ? 128*1024 : 64*1024); } strcpy(pcm->name, pcm->id); if (gus->interwave) { sprintf(pcm->name + strlen(pcm->name), " rev %c", gus->revision + 'A'); } strcat(pcm->name, " (synth)"); gus->pcm = pcm; if (gus->codec_flag) kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus); else kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus); kctl->id.index = control_index; err = snd_ctl_add(card, kctl); if (err < 0) return err; return 0; }
linux-master
sound/isa/gus/gus_pcm.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for the GF1 MIDI interface - like UART 6850 */ #include <linux/delay.h> #include <linux/interrupt.h> #include <linux/time.h> #include <sound/core.h> #include <sound/gus.h> static void snd_gf1_interrupt_midi_in(struct snd_gus_card * gus) { int count; unsigned char stat, byte; __always_unused unsigned char data; unsigned long flags; count = 10; while (count) { spin_lock_irqsave(&gus->uart_cmd_lock, flags); stat = snd_gf1_uart_stat(gus); if (!(stat & 0x01)) { /* data in Rx FIFO? */ spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); count--; continue; } count = 100; /* arm counter to new value */ data = snd_gf1_uart_get(gus); if (!(gus->gf1.uart_cmd & 0x80)) { spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); continue; } if (stat & 0x10) { /* framing error */ gus->gf1.uart_framing++; spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); continue; } byte = snd_gf1_uart_get(gus); spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); snd_rawmidi_receive(gus->midi_substream_input, &byte, 1); if (stat & 0x20) { gus->gf1.uart_overrun++; } } } static void snd_gf1_interrupt_midi_out(struct snd_gus_card * gus) { char byte; unsigned long flags; /* try unlock output */ if (snd_gf1_uart_stat(gus) & 0x01) snd_gf1_interrupt_midi_in(gus); spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (snd_gf1_uart_stat(gus) & 0x02) { /* Tx FIFO free? */ if (snd_rawmidi_transmit(gus->midi_substream_output, &byte, 1) != 1) { /* no other bytes or error */ snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd & ~0x20); /* disable Tx interrupt */ } else { snd_gf1_uart_put(gus, byte); } } spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); } static void snd_gf1_uart_reset(struct snd_gus_card * gus, int close) { snd_gf1_uart_cmd(gus, 0x03); /* reset */ if (!close && gus->uart_enable) { udelay(160); snd_gf1_uart_cmd(gus, 0x00); /* normal operations */ } } static int snd_gf1_uart_output_open(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_gus_card *gus; gus = substream->rmidi->private_data; spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (!(gus->gf1.uart_cmd & 0x80)) { /* input active? */ snd_gf1_uart_reset(gus, 0); } gus->gf1.interrupt_handler_midi_out = snd_gf1_interrupt_midi_out; gus->midi_substream_output = substream; spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); #if 0 snd_printk(KERN_DEBUG "write init - cmd = 0x%x, stat = 0x%x\n", gus->gf1.uart_cmd, snd_gf1_uart_stat(gus)); #endif return 0; } static int snd_gf1_uart_input_open(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_gus_card *gus; int i; gus = substream->rmidi->private_data; spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (gus->gf1.interrupt_handler_midi_out != snd_gf1_interrupt_midi_out) { snd_gf1_uart_reset(gus, 0); } gus->gf1.interrupt_handler_midi_in = snd_gf1_interrupt_midi_in; gus->midi_substream_input = substream; if (gus->uart_enable) { for (i = 0; i < 1000 && (snd_gf1_uart_stat(gus) & 0x01); i++) snd_gf1_uart_get(gus); /* clean Rx */ if (i >= 1000) snd_printk(KERN_ERR "gus midi uart init read - cleanup error\n"); } spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); #if 0 snd_printk(KERN_DEBUG "read init - enable = %i, cmd = 0x%x, stat = 0x%x\n", gus->uart_enable, gus->gf1.uart_cmd, snd_gf1_uart_stat(gus)); snd_printk(KERN_DEBUG "[0x%x] reg (ctrl/status) = 0x%x, reg (data) = 0x%x " "(page = 0x%x)\n", gus->gf1.port + 0x100, inb(gus->gf1.port + 0x100), inb(gus->gf1.port + 0x101), inb(gus->gf1.port + 0x102)); #endif return 0; } static int snd_gf1_uart_output_close(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_gus_card *gus; gus = substream->rmidi->private_data; spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (gus->gf1.interrupt_handler_midi_in != snd_gf1_interrupt_midi_in) snd_gf1_uart_reset(gus, 1); snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_MIDI_OUT); gus->midi_substream_output = NULL; spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); return 0; } static int snd_gf1_uart_input_close(struct snd_rawmidi_substream *substream) { unsigned long flags; struct snd_gus_card *gus; gus = substream->rmidi->private_data; spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (gus->gf1.interrupt_handler_midi_out != snd_gf1_interrupt_midi_out) snd_gf1_uart_reset(gus, 1); snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_MIDI_IN); gus->midi_substream_input = NULL; spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); return 0; } static void snd_gf1_uart_input_trigger(struct snd_rawmidi_substream *substream, int up) { struct snd_gus_card *gus; unsigned long flags; gus = substream->rmidi->private_data; spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (up) { if ((gus->gf1.uart_cmd & 0x80) == 0) snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd | 0x80); /* enable Rx interrupts */ } else { if (gus->gf1.uart_cmd & 0x80) snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd & ~0x80); /* disable Rx interrupts */ } spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); } static void snd_gf1_uart_output_trigger(struct snd_rawmidi_substream *substream, int up) { unsigned long flags; struct snd_gus_card *gus; char byte; int timeout; gus = substream->rmidi->private_data; spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (up) { if ((gus->gf1.uart_cmd & 0x20) == 0) { spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); /* wait for empty Rx - Tx is probably unlocked */ timeout = 10000; while (timeout-- > 0 && snd_gf1_uart_stat(gus) & 0x01); /* Tx FIFO free? */ spin_lock_irqsave(&gus->uart_cmd_lock, flags); if (gus->gf1.uart_cmd & 0x20) { spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); return; } if (snd_gf1_uart_stat(gus) & 0x02) { if (snd_rawmidi_transmit(substream, &byte, 1) != 1) { spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); return; } snd_gf1_uart_put(gus, byte); } snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd | 0x20); /* enable Tx interrupt */ } } else { if (gus->gf1.uart_cmd & 0x20) snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd & ~0x20); } spin_unlock_irqrestore(&gus->uart_cmd_lock, flags); } static const struct snd_rawmidi_ops snd_gf1_uart_output = { .open = snd_gf1_uart_output_open, .close = snd_gf1_uart_output_close, .trigger = snd_gf1_uart_output_trigger, }; static const struct snd_rawmidi_ops snd_gf1_uart_input = { .open = snd_gf1_uart_input_open, .close = snd_gf1_uart_input_close, .trigger = snd_gf1_uart_input_trigger, }; int snd_gf1_rawmidi_new(struct snd_gus_card *gus, int device) { struct snd_rawmidi *rmidi; int err; err = snd_rawmidi_new(gus->card, "GF1", device, 1, 1, &rmidi); if (err < 0) return err; strcpy(rmidi->name, gus->interwave ? "AMD InterWave" : "GF1"); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_gf1_uart_output); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_gf1_uart_input); rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX; rmidi->private_data = gus; gus->midi_uart = rmidi; return err; }
linux-master
sound/isa/gus/gus_uart.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * DRAM access routines */ #include <linux/time.h> #include <sound/core.h> #include <sound/gus.h> #include <sound/info.h> static int snd_gus_dram_poke(struct snd_gus_card *gus, char __user *_buffer, unsigned int address, unsigned int size) { unsigned long flags; unsigned int size1, size2; char buffer[256], *pbuffer; while (size > 0) { size1 = size > sizeof(buffer) ? sizeof(buffer) : size; if (copy_from_user(buffer, _buffer, size1)) return -EFAULT; if (gus->interwave) { spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01); snd_gf1_dram_addr(gus, address); outsb(GUSP(gus, DRAM), buffer, size1); spin_unlock_irqrestore(&gus->reg_lock, flags); address += size1; } else { pbuffer = buffer; size2 = size1; while (size2--) snd_gf1_poke(gus, address++, *pbuffer++); } size -= size1; _buffer += size1; } return 0; } int snd_gus_dram_write(struct snd_gus_card *gus, char __user *buffer, unsigned int address, unsigned int size) { return snd_gus_dram_poke(gus, buffer, address, size); } static int snd_gus_dram_peek(struct snd_gus_card *gus, char __user *_buffer, unsigned int address, unsigned int size, int rom) { unsigned long flags; unsigned int size1, size2; char buffer[256], *pbuffer; while (size > 0) { size1 = size > sizeof(buffer) ? sizeof(buffer) : size; if (gus->interwave) { spin_lock_irqsave(&gus->reg_lock, flags); snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, rom ? 0x03 : 0x01); snd_gf1_dram_addr(gus, address); insb(GUSP(gus, DRAM), buffer, size1); snd_gf1_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01); spin_unlock_irqrestore(&gus->reg_lock, flags); address += size1; } else { pbuffer = buffer; size2 = size1; while (size2--) *pbuffer++ = snd_gf1_peek(gus, address++); } if (copy_to_user(_buffer, buffer, size1)) return -EFAULT; size -= size1; _buffer += size1; } return 0; } int snd_gus_dram_read(struct snd_gus_card *gus, char __user *buffer, unsigned int address, unsigned int size, int rom) { return snd_gus_dram_peek(gus, buffer, address, size, rom); }
linux-master
sound/isa/gus/gus_dram.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for Gravis UltraSound Extreme soundcards * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <linux/init.h> #include <linux/err.h> #include <linux/isa.h> #include <linux/delay.h> #include <linux/time.h> #include <linux/module.h> #include <asm/dma.h> #include <sound/core.h> #include <sound/gus.h> #include <sound/es1688.h> #include <sound/mpu401.h> #include <sound/opl3.h> #define SNDRV_LEGACY_AUTO_PROBE #define SNDRV_LEGACY_FIND_FREE_IRQ #define SNDRV_LEGACY_FIND_FREE_DMA #include <sound/initval.h> #define CRD_NAME "Gravis UltraSound Extreme" #define DEV_NAME "gusextreme" MODULE_DESCRIPTION(CRD_NAME); MODULE_AUTHOR("Jaroslav Kysela <[email protected]>"); MODULE_LICENSE("GPL"); static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */ static long gf1_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x210,0x220,0x230,0x240,0x250,0x260,0x270 */ static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS) - 1] = -1}; /* 0x300,0x310,0x320 */ static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */ static int gf1_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,3,5,9,11,12,15 */ static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */ static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; static int joystick_dac[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 29}; /* 0 to 31, (0.59V-4.52V or 0.389V-2.98V) */ static int channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 24}; static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard."); module_param_hw_array(port, long, ioport, NULL, 0444); MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver."); module_param_hw_array(gf1_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(gf1_port, "GF1 port # for " CRD_NAME " driver (optional)."); module_param_hw_array(mpu_port, long, ioport, NULL, 0444); MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver."); module_param_hw_array(irq, int, irq, NULL, 0444); MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver."); module_param_hw_array(mpu_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver."); module_param_hw_array(gf1_irq, int, irq, NULL, 0444); MODULE_PARM_DESC(gf1_irq, "GF1 IRQ # for " CRD_NAME " driver."); module_param_hw_array(dma8, int, dma, NULL, 0444); MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver."); module_param_hw_array(dma1, int, dma, NULL, 0444); MODULE_PARM_DESC(dma1, "GF1 DMA # for " CRD_NAME " driver."); module_param_array(joystick_dac, int, NULL, 0444); MODULE_PARM_DESC(joystick_dac, "Joystick DAC level 0.59V-4.52V or 0.389V-2.98V for " CRD_NAME " driver."); module_param_array(channels, int, NULL, 0444); MODULE_PARM_DESC(channels, "GF1 channels for " CRD_NAME " driver."); module_param_array(pcm_channels, int, NULL, 0444); MODULE_PARM_DESC(pcm_channels, "Reserved PCM channels for " CRD_NAME " driver."); static int snd_gusextreme_match(struct device *dev, unsigned int n) { return enable[n]; } static int snd_gusextreme_es1688_create(struct snd_card *card, struct snd_es1688 *chip, struct device *dev, unsigned int n) { static const long possible_ports[] = {0x220, 0x240, 0x260}; static const int possible_irqs[] = {5, 9, 10, 7, -1}; static const int possible_dmas[] = {1, 3, 0, -1}; int i, error; if (irq[n] == SNDRV_AUTO_IRQ) { irq[n] = snd_legacy_find_free_irq(possible_irqs); if (irq[n] < 0) { dev_err(dev, "unable to find a free IRQ for ES1688\n"); return -EBUSY; } } if (dma8[n] == SNDRV_AUTO_DMA) { dma8[n] = snd_legacy_find_free_dma(possible_dmas); if (dma8[n] < 0) { dev_err(dev, "unable to find a free DMA for ES1688\n"); return -EBUSY; } } if (port[n] != SNDRV_AUTO_PORT) return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688); i = 0; do { port[n] = possible_ports[i]; error = snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n], mpu_irq[n], dma8[n], ES1688_HW_1688); } while (error < 0 && ++i < ARRAY_SIZE(possible_ports)); return error; } static int snd_gusextreme_gus_card_create(struct snd_card *card, struct device *dev, unsigned int n, struct snd_gus_card **rgus) { static const int possible_irqs[] = {11, 12, 15, 9, 5, 7, 3, -1}; static const int possible_dmas[] = {5, 6, 7, 3, 1, -1}; if (gf1_irq[n] == SNDRV_AUTO_IRQ) { gf1_irq[n] = snd_legacy_find_free_irq(possible_irqs); if (gf1_irq[n] < 0) { dev_err(dev, "unable to find a free IRQ for GF1\n"); return -EBUSY; } } if (dma1[n] == SNDRV_AUTO_DMA) { dma1[n] = snd_legacy_find_free_dma(possible_dmas); if (dma1[n] < 0) { dev_err(dev, "unable to find a free DMA for GF1\n"); return -EBUSY; } } return snd_gus_create(card, gf1_port[n], gf1_irq[n], dma1[n], -1, 0, channels[n], pcm_channels[n], 0, rgus); } static int snd_gusextreme_detect(struct snd_gus_card *gus, struct snd_es1688 *es1688) { unsigned long flags; unsigned char d; /* * This is main stuff - enable access to GF1 chip... * I'm not sure, if this will work for card which have * ES1688 chip in another place than 0x220. * * I used reverse-engineering in DOSEMU. [--jk] * * ULTRINIT.EXE: * 0x230 = 0,2,3 * 0x240 = 2,0,1 * 0x250 = 2,0,3 * 0x260 = 2,2,1 */ spin_lock_irqsave(&es1688->mixer_lock, flags); snd_es1688_mixer_write(es1688, 0x40, 0x0b); /* don't change!!! */ spin_unlock_irqrestore(&es1688->mixer_lock, flags); spin_lock_irqsave(&es1688->reg_lock, flags); outb(gus->gf1.port & 0x040 ? 2 : 0, ES1688P(es1688, INIT1)); outb(0, 0x201); outb(gus->gf1.port & 0x020 ? 2 : 0, ES1688P(es1688, INIT1)); outb(0, 0x201); outb(gus->gf1.port & 0x010 ? 3 : 1, ES1688P(es1688, INIT1)); spin_unlock_irqrestore(&es1688->reg_lock, flags); udelay(100); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0); /* reset GF1 */ d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 0) { snd_printdd("[0x%lx] check 1 failed - 0x%x\n", gus->gf1.port, d); return -EIO; } udelay(160); snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1); /* release reset */ udelay(160); d = snd_gf1_i_look8(gus, SNDRV_GF1_GB_RESET); if ((d & 0x07) != 1) { snd_printdd("[0x%lx] check 2 failed - 0x%x\n", gus->gf1.port, d); return -EIO; } return 0; } static int snd_gusextreme_mixer(struct snd_card *card) { struct snd_ctl_elem_id id1, id2; int error; memset(&id1, 0, sizeof(id1)); memset(&id2, 0, sizeof(id2)); id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; /* reassign AUX to SYNTHESIZER */ strcpy(id1.name, "Aux Playback Volume"); strcpy(id2.name, "Synth Playback Volume"); error = snd_ctl_rename_id(card, &id1, &id2); if (error < 0) return error; /* reassign Master Playback Switch to Synth Playback Switch */ strcpy(id1.name, "Master Playback Switch"); strcpy(id2.name, "Synth Playback Switch"); error = snd_ctl_rename_id(card, &id1, &id2); if (error < 0) return error; return 0; } static int snd_gusextreme_probe(struct device *dev, unsigned int n) { struct snd_card *card; struct snd_gus_card *gus; struct snd_es1688 *es1688; struct snd_opl3 *opl3; int error; error = snd_devm_card_new(dev, index[n], id[n], THIS_MODULE, sizeof(struct snd_es1688), &card); if (error < 0) return error; es1688 = card->private_data; if (mpu_port[n] == SNDRV_AUTO_PORT) mpu_port[n] = 0; if (mpu_irq[n] > 15) mpu_irq[n] = -1; error = snd_gusextreme_es1688_create(card, es1688, dev, n); if (error < 0) return error; if (gf1_port[n] < 0) gf1_port[n] = es1688->port + 0x20; error = snd_gusextreme_gus_card_create(card, dev, n, &gus); if (error < 0) return error; error = snd_gusextreme_detect(gus, es1688); if (error < 0) return error; gus->joystick_dac = joystick_dac[n]; error = snd_gus_initialize(gus); if (error < 0) return error; error = -ENODEV; if (!gus->ess_flag) { dev_err(dev, "GUS Extreme soundcard was not " "detected at 0x%lx\n", gus->gf1.port); return error; } gus->codec_flag = 1; error = snd_es1688_pcm(card, es1688, 0); if (error < 0) return error; error = snd_es1688_mixer(card, es1688); if (error < 0) return error; snd_component_add(card, "ES1688"); if (pcm_channels[n] > 0) { error = snd_gf1_pcm_new(gus, 1, 1); if (error < 0) return error; } error = snd_gf1_new_mixer(gus); if (error < 0) return error; error = snd_gusextreme_mixer(card); if (error < 0) return error; if (snd_opl3_create(card, es1688->port, es1688->port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) dev_warn(dev, "opl3 not detected at 0x%lx\n", es1688->port); else { error = snd_opl3_hwdep_new(opl3, 0, 2, NULL); if (error < 0) return error; } if (es1688->mpu_port >= 0x300) { error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688, es1688->mpu_port, 0, mpu_irq[n], NULL); if (error < 0) return error; } sprintf(card->longname, "Gravis UltraSound Extreme at 0x%lx, " "irq %i&%i, dma %i&%i", es1688->port, gus->gf1.irq, es1688->irq, gus->gf1.dma1, es1688->dma8); error = snd_card_register(card); if (error < 0) return error; dev_set_drvdata(dev, card); return 0; } static struct isa_driver snd_gusextreme_driver = { .match = snd_gusextreme_match, .probe = snd_gusextreme_probe, #if 0 /* FIXME */ .suspend = snd_gusextreme_suspend, .resume = snd_gusextreme_resume, #endif .driver = { .name = DEV_NAME } }; module_isa_driver(snd_gusextreme_driver, SNDRV_CARDS);
linux-master
sound/isa/gus/gusextreme.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * GUS's memory access via proc filesystem */ #include <linux/slab.h> #include <sound/core.h> #include <sound/gus.h> #include <sound/info.h> struct gus_proc_private { int rom; /* data are in ROM */ unsigned int address; unsigned int size; struct snd_gus_card * gus; }; static ssize_t snd_gf1_mem_proc_dump(struct snd_info_entry *entry, void *file_private_data, struct file *file, char __user *buf, size_t count, loff_t pos) { struct gus_proc_private *priv = entry->private_data; struct snd_gus_card *gus = priv->gus; int err; err = snd_gus_dram_read(gus, buf, pos, count, priv->rom); if (err < 0) return err; return count; } static void snd_gf1_mem_proc_free(struct snd_info_entry *entry) { struct gus_proc_private *priv = entry->private_data; kfree(priv); } static const struct snd_info_entry_ops snd_gf1_mem_proc_ops = { .read = snd_gf1_mem_proc_dump, }; int snd_gf1_mem_proc_init(struct snd_gus_card * gus) { int idx; char name[16]; struct gus_proc_private *priv; struct snd_info_entry *entry; for (idx = 0; idx < 4; idx++) { if (gus->gf1.mem_alloc.banks_8[idx].size > 0) { priv = kzalloc(sizeof(*priv), GFP_KERNEL); if (priv == NULL) return -ENOMEM; priv->gus = gus; sprintf(name, "gus-ram-%i", idx); if (! snd_card_proc_new(gus->card, name, &entry)) { entry->content = SNDRV_INFO_CONTENT_DATA; entry->private_data = priv; entry->private_free = snd_gf1_mem_proc_free; entry->c.ops = &snd_gf1_mem_proc_ops; priv->address = gus->gf1.mem_alloc.banks_8[idx].address; priv->size = entry->size = gus->gf1.mem_alloc.banks_8[idx].size; } } } for (idx = 0; idx < 4; idx++) { if (gus->gf1.rom_present & (1 << idx)) { priv = kzalloc(sizeof(*priv), GFP_KERNEL); if (priv == NULL) return -ENOMEM; priv->rom = 1; priv->gus = gus; sprintf(name, "gus-rom-%i", idx); if (! snd_card_proc_new(gus->card, name, &entry)) { entry->content = SNDRV_INFO_CONTENT_DATA; entry->private_data = priv; entry->private_free = snd_gf1_mem_proc_free; entry->c.ops = &snd_gf1_mem_proc_ops; priv->address = idx * 4096 * 1024; priv->size = entry->size = gus->gf1.rom_memory; } } } return 0; }
linux-master
sound/isa/gus/gus_mem_proc.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Routine for IRQ handling from GF1/InterWave chip * Copyright (c) by Jaroslav Kysela <[email protected]> */ #include <sound/core.h> #include <sound/info.h> #include <sound/gus.h> #ifdef CONFIG_SND_DEBUG #define STAT_ADD(x) ((x)++) #else #define STAT_ADD(x) while (0) { ; } #endif irqreturn_t snd_gus_interrupt(int irq, void *dev_id) { struct snd_gus_card * gus = dev_id; unsigned char status; int loop = 100; int handled = 0; __again: status = inb(gus->gf1.reg_irqstat); if (status == 0) return IRQ_RETVAL(handled); handled = 1; /* snd_printk(KERN_DEBUG "IRQ: status = 0x%x\n", status); */ if (status & 0x02) { STAT_ADD(gus->gf1.interrupt_stat_midi_in); if (gus->gf1.interrupt_handler_midi_in) gus->gf1.interrupt_handler_midi_in(gus); } if (status & 0x01) { STAT_ADD(gus->gf1.interrupt_stat_midi_out); if (gus->gf1.interrupt_handler_midi_out) gus->gf1.interrupt_handler_midi_out(gus); } if (status & (0x20 | 0x40)) { unsigned int already, _current_; unsigned char voice_status, voice; struct snd_gus_voice *pvoice; already = 0; while (((voice_status = snd_gf1_i_read8(gus, SNDRV_GF1_GB_VOICES_IRQ)) & 0xc0) != 0xc0) { voice = voice_status & 0x1f; _current_ = 1 << voice; if (already & _current_) continue; /* multi request */ already |= _current_; /* mark request */ #if 0 printk(KERN_DEBUG "voice = %i, voice_status = 0x%x, " "voice_verify = %i\n", voice, voice_status, inb(GUSP(gus, GF1PAGE))); #endif pvoice = &gus->gf1.voices[voice]; if (pvoice->use) { if (!(voice_status & 0x80)) { /* voice position IRQ */ STAT_ADD(pvoice->interrupt_stat_wave); pvoice->handler_wave(gus, pvoice); } if (!(voice_status & 0x40)) { /* volume ramp IRQ */ STAT_ADD(pvoice->interrupt_stat_volume); pvoice->handler_volume(gus, pvoice); } } else { STAT_ADD(gus->gf1.interrupt_stat_voice_lost); snd_gf1_i_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL); snd_gf1_i_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL); } } } if (status & 0x04) { STAT_ADD(gus->gf1.interrupt_stat_timer1); if (gus->gf1.interrupt_handler_timer1) gus->gf1.interrupt_handler_timer1(gus); } if (status & 0x08) { STAT_ADD(gus->gf1.interrupt_stat_timer2); if (gus->gf1.interrupt_handler_timer2) gus->gf1.interrupt_handler_timer2(gus); } if (status & 0x80) { if (snd_gf1_i_look8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL) & 0x40) { STAT_ADD(gus->gf1.interrupt_stat_dma_write); if (gus->gf1.interrupt_handler_dma_write) gus->gf1.interrupt_handler_dma_write(gus); } if (snd_gf1_i_look8(gus, SNDRV_GF1_GB_REC_DMA_CONTROL) & 0x40) { STAT_ADD(gus->gf1.interrupt_stat_dma_read); if (gus->gf1.interrupt_handler_dma_read) gus->gf1.interrupt_handler_dma_read(gus); } } if (--loop > 0) goto __again; return IRQ_NONE; } #ifdef CONFIG_SND_DEBUG static void snd_gus_irq_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct snd_gus_card *gus; struct snd_gus_voice *pvoice; int idx; gus = entry->private_data; snd_iprintf(buffer, "midi out = %u\n", gus->gf1.interrupt_stat_midi_out); snd_iprintf(buffer, "midi in = %u\n", gus->gf1.interrupt_stat_midi_in); snd_iprintf(buffer, "timer1 = %u\n", gus->gf1.interrupt_stat_timer1); snd_iprintf(buffer, "timer2 = %u\n", gus->gf1.interrupt_stat_timer2); snd_iprintf(buffer, "dma write = %u\n", gus->gf1.interrupt_stat_dma_write); snd_iprintf(buffer, "dma read = %u\n", gus->gf1.interrupt_stat_dma_read); snd_iprintf(buffer, "voice lost = %u\n", gus->gf1.interrupt_stat_voice_lost); for (idx = 0; idx < 32; idx++) { pvoice = &gus->gf1.voices[idx]; snd_iprintf(buffer, "voice %i: wave = %u, volume = %u\n", idx, pvoice->interrupt_stat_wave, pvoice->interrupt_stat_volume); } } void snd_gus_irq_profile_init(struct snd_gus_card *gus) { snd_card_ro_proc_new(gus->card, "gusirq", gus, snd_gus_irq_info_read); } #endif
linux-master
sound/isa/gus/gus_irq.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) by Jaroslav Kysela <[email protected]> * Routines for control of ICS 2101 chip and "mixer" in GF1 chip */ #include <linux/time.h> #include <linux/wait.h> #include <sound/core.h> #include <sound/control.h> #include <sound/gus.h> /* * */ #define GF1_SINGLE(xname, xindex, shift, invert) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_gf1_info_single, \ .get = snd_gf1_get_single, .put = snd_gf1_put_single, \ .private_value = shift | (invert << 8) } #define snd_gf1_info_single snd_ctl_boolean_mono_info static int snd_gf1_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); int shift = kcontrol->private_value & 0xff; int invert = (kcontrol->private_value >> 8) & 1; ucontrol->value.integer.value[0] = (gus->mix_cntrl_reg >> shift) & 1; if (invert) ucontrol->value.integer.value[0] ^= 1; return 0; } static int snd_gf1_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); unsigned long flags; int shift = kcontrol->private_value & 0xff; int invert = (kcontrol->private_value >> 8) & 1; int change; unsigned char oval, nval; nval = ucontrol->value.integer.value[0] & 1; if (invert) nval ^= 1; nval <<= shift; spin_lock_irqsave(&gus->reg_lock, flags); oval = gus->mix_cntrl_reg; nval = (oval & ~(1 << shift)) | nval; change = nval != oval; outb(gus->mix_cntrl_reg = nval, GUSP(gus, MIXCNTRLREG)); outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE)); spin_unlock_irqrestore(&gus->reg_lock, flags); return change; } #define ICS_DOUBLE(xname, xindex, addr) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ .info = snd_ics_info_double, \ .get = snd_ics_get_double, .put = snd_ics_put_double, \ .private_value = addr } static int snd_ics_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 2; uinfo->value.integer.min = 0; uinfo->value.integer.max = 127; return 0; } static int snd_ics_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); unsigned long flags; int addr = kcontrol->private_value & 0xff; unsigned char left, right; spin_lock_irqsave(&gus->reg_lock, flags); left = gus->gf1.ics_regs[addr][0]; right = gus->gf1.ics_regs[addr][1]; spin_unlock_irqrestore(&gus->reg_lock, flags); ucontrol->value.integer.value[0] = left & 127; ucontrol->value.integer.value[1] = right & 127; return 0; } static int snd_ics_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); unsigned long flags; int addr = kcontrol->private_value & 0xff; int change; unsigned char val1, val2, oval1, oval2; val1 = ucontrol->value.integer.value[0] & 127; val2 = ucontrol->value.integer.value[1] & 127; spin_lock_irqsave(&gus->reg_lock, flags); oval1 = gus->gf1.ics_regs[addr][0]; oval2 = gus->gf1.ics_regs[addr][1]; change = val1 != oval1 || val2 != oval2; gus->gf1.ics_regs[addr][0] = val1; gus->gf1.ics_regs[addr][1] = val2; if (gus->ics_flag && gus->ics_flipped && (addr == SNDRV_ICS_GF1_DEV || addr == SNDRV_ICS_MASTER_DEV)) swap(val1, val2); addr <<= 3; outb(addr | 0, GUSP(gus, MIXCNTRLPORT)); outb(1, GUSP(gus, MIXDATAPORT)); outb(addr | 2, GUSP(gus, MIXCNTRLPORT)); outb((unsigned char) val1, GUSP(gus, MIXDATAPORT)); outb(addr | 1, GUSP(gus, MIXCNTRLPORT)); outb(2, GUSP(gus, MIXDATAPORT)); outb(addr | 3, GUSP(gus, MIXCNTRLPORT)); outb((unsigned char) val2, GUSP(gus, MIXDATAPORT)); spin_unlock_irqrestore(&gus->reg_lock, flags); return change; } static const struct snd_kcontrol_new snd_gf1_controls[] = { GF1_SINGLE("Master Playback Switch", 0, 1, 1), GF1_SINGLE("Line Switch", 0, 0, 1), GF1_SINGLE("Mic Switch", 0, 2, 0) }; static const struct snd_kcontrol_new snd_ics_controls[] = { GF1_SINGLE("Master Playback Switch", 0, 1, 1), ICS_DOUBLE("Master Playback Volume", 0, SNDRV_ICS_MASTER_DEV), ICS_DOUBLE("Synth Playback Volume", 0, SNDRV_ICS_GF1_DEV), GF1_SINGLE("Line Switch", 0, 0, 1), ICS_DOUBLE("Line Playback Volume", 0, SNDRV_ICS_LINE_DEV), GF1_SINGLE("Mic Switch", 0, 2, 0), ICS_DOUBLE("Mic Playback Volume", 0, SNDRV_ICS_MIC_DEV), ICS_DOUBLE("CD Playback Volume", 0, SNDRV_ICS_CD_DEV) }; int snd_gf1_new_mixer(struct snd_gus_card * gus) { struct snd_card *card; unsigned int idx, max; int err; if (snd_BUG_ON(!gus)) return -EINVAL; card = gus->card; if (snd_BUG_ON(!card)) return -EINVAL; if (gus->ics_flag) snd_component_add(card, "ICS2101"); if (card->mixername[0] == '\0') { strcpy(card->mixername, gus->ics_flag ? "GF1,ICS2101" : "GF1"); } else { if (gus->ics_flag) strcat(card->mixername, ",ICS2101"); strcat(card->mixername, ",GF1"); } if (!gus->ics_flag) { max = gus->ess_flag ? 1 : ARRAY_SIZE(snd_gf1_controls); for (idx = 0; idx < max; idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_gf1_controls[idx], gus)); if (err < 0) return err; } } else { for (idx = 0; idx < ARRAY_SIZE(snd_ics_controls); idx++) { err = snd_ctl_add(card, snd_ctl_new1(&snd_ics_controls[idx], gus)); if (err < 0) return err; } } return 0; }
linux-master
sound/isa/gus/gus_mixer.c
// SPDX-License-Identifier: GPL-2.0-or-later /* * Routines for Gravis UltraSound soundcards - Timers * Copyright (c) by Jaroslav Kysela <[email protected]> * * GUS have similar timers as AdLib (OPL2/OPL3 chips). */ #include <linux/time.h> #include <sound/core.h> #include <sound/gus.h> /* * Timer 1 - 80us */ static int snd_gf1_timer1_start(struct snd_timer * timer) { unsigned long flags; unsigned char tmp; unsigned int ticks; struct snd_gus_card *gus; gus = snd_timer_chip(timer); spin_lock_irqsave(&gus->reg_lock, flags); ticks = timer->sticks; tmp = (gus->gf1.timer_enabled |= 4); snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_1, 256 - ticks); /* timer 1 count */ snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 1 IRQ */ snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */ spin_unlock_irqrestore(&gus->reg_lock, flags); return 0; } static int snd_gf1_timer1_stop(struct snd_timer * timer) { unsigned long flags; unsigned char tmp; struct snd_gus_card *gus; gus = snd_timer_chip(timer); spin_lock_irqsave(&gus->reg_lock, flags); tmp = (gus->gf1.timer_enabled &= ~4); snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */ spin_unlock_irqrestore(&gus->reg_lock, flags); return 0; } /* * Timer 2 - 320us */ static int snd_gf1_timer2_start(struct snd_timer * timer) { unsigned long flags; unsigned char tmp; unsigned int ticks; struct snd_gus_card *gus; gus = snd_timer_chip(timer); spin_lock_irqsave(&gus->reg_lock, flags); ticks = timer->sticks; tmp = (gus->gf1.timer_enabled |= 8); snd_gf1_write8(gus, SNDRV_GF1_GB_ADLIB_TIMER_2, 256 - ticks); /* timer 2 count */ snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* enable timer 2 IRQ */ snd_gf1_adlib_write(gus, 0x04, tmp >> 2); /* timer 2 start */ spin_unlock_irqrestore(&gus->reg_lock, flags); return 0; } static int snd_gf1_timer2_stop(struct snd_timer * timer) { unsigned long flags; unsigned char tmp; struct snd_gus_card *gus; gus = snd_timer_chip(timer); spin_lock_irqsave(&gus->reg_lock, flags); tmp = (gus->gf1.timer_enabled &= ~8); snd_gf1_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, tmp); /* disable timer #1 */ spin_unlock_irqrestore(&gus->reg_lock, flags); return 0; } /* */ static void snd_gf1_interrupt_timer1(struct snd_gus_card * gus) { struct snd_timer *timer = gus->gf1.timer1; if (timer == NULL) return; snd_timer_interrupt(timer, timer->sticks); } static void snd_gf1_interrupt_timer2(struct snd_gus_card * gus) { struct snd_timer *timer = gus->gf1.timer2; if (timer == NULL) return; snd_timer_interrupt(timer, timer->sticks); } /* */ static const struct snd_timer_hardware snd_gf1_timer1 = { .flags = SNDRV_TIMER_HW_STOP, .resolution = 80000, .ticks = 256, .start = snd_gf1_timer1_start, .stop = snd_gf1_timer1_stop, }; static const struct snd_timer_hardware snd_gf1_timer2 = { .flags = SNDRV_TIMER_HW_STOP, .resolution = 320000, .ticks = 256, .start = snd_gf1_timer2_start, .stop = snd_gf1_timer2_stop, }; static void snd_gf1_timer1_free(struct snd_timer *timer) { struct snd_gus_card *gus = timer->private_data; gus->gf1.timer1 = NULL; } static void snd_gf1_timer2_free(struct snd_timer *timer) { struct snd_gus_card *gus = timer->private_data; gus->gf1.timer2 = NULL; } void snd_gf1_timers_init(struct snd_gus_card * gus) { struct snd_timer *timer; struct snd_timer_id tid; if (gus->gf1.timer1 != NULL || gus->gf1.timer2 != NULL) return; gus->gf1.interrupt_handler_timer1 = snd_gf1_interrupt_timer1; gus->gf1.interrupt_handler_timer2 = snd_gf1_interrupt_timer2; tid.dev_class = SNDRV_TIMER_CLASS_CARD; tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; tid.card = gus->card->number; tid.device = gus->timer_dev; tid.subdevice = 0; if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) { strcpy(timer->name, "GF1 timer #1"); timer->private_data = gus; timer->private_free = snd_gf1_timer1_free; timer->hw = snd_gf1_timer1; } gus->gf1.timer1 = timer; tid.device++; if (snd_timer_new(gus->card, "GF1 timer", &tid, &timer) >= 0) { strcpy(timer->name, "GF1 timer #2"); timer->private_data = gus; timer->private_free = snd_gf1_timer2_free; timer->hw = snd_gf1_timer2; } gus->gf1.timer2 = timer; } void snd_gf1_timers_done(struct snd_gus_card * gus) { snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_TIMER1 | SNDRV_GF1_HANDLER_TIMER2); if (gus->gf1.timer1) { snd_device_free(gus->card, gus->gf1.timer1); gus->gf1.timer1 = NULL; } if (gus->gf1.timer2) { snd_device_free(gus->card, gus->gf1.timer2); gus->gf1.timer2 = NULL; } }
linux-master
sound/isa/gus/gus_timer.c