^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * PC-Speaker driver for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1993-1997 Michael Beck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1997-2001 David Woodhouse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2001-2008 Stas Sergeev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "pcsp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static bool nforce_wa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) module_param(nforce_wa, bool, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) "(expect bad sound)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define DMIX_WANTS_S16 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Call snd_pcm_period_elapsed in a work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * This avoids spinlock messes and long-running irq contexts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void pcsp_call_pcm_elapsed(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (atomic_read(&pcsp_chip.timer_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct snd_pcm_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) substream = pcsp_chip.playback_substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) snd_pcm_period_elapsed(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static DECLARE_WORK(pcsp_pcm_work, pcsp_call_pcm_elapsed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* write the port and returns the next expire time in ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * called at the trigger-start and in hrtimer callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static u64 pcsp_timer_update(struct snd_pcsp *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned char timer_cnt, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) u64 ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct snd_pcm_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct snd_pcm_runtime *runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (chip->thalf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) outb(chip->val61, 0x61);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) chip->thalf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return chip->ns_rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) substream = chip->playback_substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* assume it is mono! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) val = runtime->dma_area[chip->playback_ptr + chip->fmt_size - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (chip->is_signed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) val ^= 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) timer_cnt = val * CUR_DIV() / 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (timer_cnt && chip->enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) raw_spin_lock_irqsave(&i8253_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!nforce_wa) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) outb_p(chip->val61, 0x61);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) outb_p(timer_cnt, 0x42);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) outb(chip->val61 ^ 1, 0x61);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) outb(chip->val61 ^ 2, 0x61);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) chip->thalf = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) raw_spin_unlock_irqrestore(&i8253_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) chip->ns_rem = PCSP_PERIOD_NS();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) chip->ns_rem -= ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static void pcsp_pointer_update(struct snd_pcsp *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct snd_pcm_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) size_t period_bytes, buffer_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int periods_elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* update the playback position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) substream = chip->playback_substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) period_bytes = snd_pcm_lib_period_bytes(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spin_lock_irqsave(&chip->substream_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) chip->playback_ptr += PCSP_INDEX_INC() * chip->fmt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) periods_elapsed = chip->playback_ptr - chip->period_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (periods_elapsed < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) printk(KERN_INFO "PCSP: buffer_bytes mod period_bytes != 0 ? "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) "(%zi %zi %zi)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) chip->playback_ptr, period_bytes, buffer_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) periods_elapsed += buffer_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) periods_elapsed /= period_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* wrap the pointer _before_ calling snd_pcm_period_elapsed(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * or ALSA will BUG on us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) chip->playback_ptr %= buffer_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (periods_elapsed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) chip->period_ptr += periods_elapsed * period_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) chip->period_ptr %= buffer_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) queue_work(system_highpri_wq, &pcsp_pcm_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) spin_unlock_irqrestore(&chip->substream_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int pointer_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u64 ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!atomic_read(&chip->timer_active) || !chip->playback_substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) pointer_update = !chip->thalf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ns = pcsp_timer_update(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) printk(KERN_WARNING "PCSP: unexpected stop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (pointer_update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) pcsp_pointer_update(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return HRTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int pcsp_start_playing(struct snd_pcsp *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) printk(KERN_INFO "PCSP: start_playing called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (atomic_read(&chip->timer_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) printk(KERN_ERR "PCSP: Timer already active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) raw_spin_lock(&i8253_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) chip->val61 = inb(0x61) | 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) outb_p(0x92, 0x43); /* binary, mode 1, LSB only, ch 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) raw_spin_unlock(&i8253_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) atomic_set(&chip->timer_active, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) chip->thalf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) hrtimer_start(&pcsp_chip.timer, 0, HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void pcsp_stop_playing(struct snd_pcsp *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) printk(KERN_INFO "PCSP: stop_playing called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!atomic_read(&chip->timer_active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) atomic_set(&chip->timer_active, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) raw_spin_lock(&i8253_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* restore the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) outb_p(0xb6, 0x43); /* binary, mode 3, LSB/MSB, ch 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) outb(chip->val61 & 0xFC, 0x61);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) raw_spin_unlock(&i8253_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Force to stop and sync the stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) void pcsp_sync_stop(struct snd_pcsp *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pcsp_stop_playing(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) hrtimer_cancel(&chip->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) cancel_work_sync(&pcsp_pcm_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) printk(KERN_INFO "PCSP: close called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) pcsp_sync_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) chip->playback_substream = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct snd_pcm_hw_params *hw_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) pcsp_sync_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) printk(KERN_INFO "PCSP: hw_free called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pcsp_sync_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int snd_pcsp_playback_prepare(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pcsp_sync_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) chip->playback_ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) chip->period_ptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) chip->fmt_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) snd_pcm_format_physical_width(substream->runtime->format) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) chip->is_signed = snd_pcm_format_signed(substream->runtime->format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) printk(KERN_INFO "PCSP: prepare called, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) "size=%zi psize=%zi f=%zi f1=%i fsize=%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) snd_pcm_lib_buffer_bytes(substream),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) snd_pcm_lib_period_bytes(substream),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) snd_pcm_lib_buffer_bytes(substream) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) snd_pcm_lib_period_bytes(substream),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) substream->runtime->periods,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) chip->fmt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int snd_pcsp_trigger(struct snd_pcm_substream *substream, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) printk(KERN_INFO "PCSP: trigger called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case SNDRV_PCM_TRIGGER_RESUME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return pcsp_start_playing(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case SNDRV_PCM_TRIGGER_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pcsp_stop_playing(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static snd_pcm_uframes_t snd_pcsp_playback_pointer(struct snd_pcm_substream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) unsigned int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) spin_lock(&chip->substream_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) pos = chip->playback_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) spin_unlock(&chip->substream_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return bytes_to_frames(substream->runtime, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static const struct snd_pcm_hardware snd_pcsp_playback = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .info = (SNDRV_PCM_INFO_INTERLEAVED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) SNDRV_PCM_INFO_HALF_DUPLEX |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .formats = (SNDRV_PCM_FMTBIT_U8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #if DMIX_WANTS_S16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) | SNDRV_PCM_FMTBIT_S16_LE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .rates = SNDRV_PCM_RATE_KNOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .rate_min = PCSP_DEFAULT_SRATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .rate_max = PCSP_DEFAULT_SRATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .channels_min = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .channels_max = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .buffer_bytes_max = PCSP_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .period_bytes_min = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .period_bytes_max = PCSP_MAX_PERIOD_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .periods_min = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .periods_max = PCSP_MAX_PERIODS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .fifo_size = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int snd_pcsp_playback_open(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #if PCSP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) printk(KERN_INFO "PCSP: open called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (atomic_read(&chip->timer_active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) printk(KERN_ERR "PCSP: still active!!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) runtime->hw = snd_pcsp_playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) chip->playback_substream = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static const struct snd_pcm_ops snd_pcsp_playback_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .open = snd_pcsp_playback_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) .close = snd_pcsp_playback_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) .hw_params = snd_pcsp_playback_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .hw_free = snd_pcsp_playback_hw_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .prepare = snd_pcsp_playback_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .trigger = snd_pcsp_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .pointer = snd_pcsp_playback_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int snd_pcsp_new_pcm(struct snd_pcsp *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) err = snd_pcm_new(chip->card, "pcspeaker", 0, 1, 0, &chip->pcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) snd_pcm_set_ops(chip->pcm, SNDRV_PCM_STREAM_PLAYBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) &snd_pcsp_playback_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) chip->pcm->private_data = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) chip->pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) strcpy(chip->pcm->name, "pcsp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) snd_pcm_set_managed_buffer_all(chip->pcm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) SNDRV_DMA_TYPE_CONTINUOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) PCSP_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) PCSP_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }