Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pcm emulation on emu8000 wavetable |
| 3 | * |
| 4 | * Copyright (C) 2002 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include "emu8000_local.h" |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 22 | |
| 23 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <sound/initval.h> |
| 27 | #include <sound/pcm.h> |
| 28 | |
| 29 | /* |
| 30 | * define the following if you want to use this pcm with non-interleaved mode |
| 31 | */ |
| 32 | /* #define USE_NONINTERLEAVE */ |
| 33 | |
| 34 | /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set |
| 35 | * mmap_emulation flag to 1 in your .asoundrc, such like |
| 36 | * |
| 37 | * pcm.emu8k { |
| 38 | * type plug |
| 39 | * slave.pcm { |
| 40 | * type hw |
| 41 | * card 0 |
| 42 | * device 1 |
| 43 | * mmap_emulation 1 |
| 44 | * } |
| 45 | * } |
| 46 | * |
| 47 | * besides, for the time being, the non-interleaved mode doesn't work well on |
| 48 | * alsa-lib... |
| 49 | */ |
| 50 | |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct snd_emu8k_pcm { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 53 | struct snd_emu8000 *emu; |
| 54 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | unsigned int allocated_bytes; |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 57 | struct snd_util_memblk *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | unsigned int offset; |
| 59 | unsigned int buf_size; |
| 60 | unsigned int period_size; |
| 61 | unsigned int loop_start[2]; |
| 62 | unsigned int pitch; |
| 63 | int panning[2]; |
| 64 | int last_ptr; |
| 65 | int period_pos; |
| 66 | int voices; |
| 67 | unsigned int dram_opened: 1; |
| 68 | unsigned int running: 1; |
| 69 | unsigned int timer_running: 1; |
| 70 | struct timer_list timer; |
| 71 | spinlock_t timer_lock; |
| 72 | }; |
| 73 | |
| 74 | #define LOOP_BLANK_SIZE 8 |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * open up channels for the simultaneous data transfer and playback |
| 79 | */ |
| 80 | static int |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 81 | emu8k_open_dram_for_pcm(struct snd_emu8000 *emu, int channels) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | int i; |
| 84 | |
| 85 | /* reserve up to 2 voices for playback */ |
| 86 | snd_emux_lock_voice(emu->emu, 0); |
| 87 | if (channels > 1) |
| 88 | snd_emux_lock_voice(emu->emu, 1); |
| 89 | |
| 90 | /* reserve 28 voices for loading */ |
| 91 | for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) { |
| 92 | unsigned int mode = EMU8000_RAM_WRITE; |
| 93 | snd_emux_lock_voice(emu->emu, i); |
| 94 | #ifndef USE_NONINTERLEAVE |
| 95 | if (channels > 1 && (i & 1) != 0) |
| 96 | mode |= EMU8000_RAM_RIGHT; |
| 97 | #endif |
| 98 | snd_emu8000_dma_chan(emu, i, mode); |
| 99 | } |
| 100 | |
| 101 | /* assign voice 31 and 32 to ROM */ |
| 102 | EMU8000_VTFT_WRITE(emu, 30, 0); |
| 103 | EMU8000_PSST_WRITE(emu, 30, 0x1d8); |
| 104 | EMU8000_CSL_WRITE(emu, 30, 0x1e0); |
| 105 | EMU8000_CCCA_WRITE(emu, 30, 0x1d8); |
| 106 | EMU8000_VTFT_WRITE(emu, 31, 0); |
| 107 | EMU8000_PSST_WRITE(emu, 31, 0x1d8); |
| 108 | EMU8000_CSL_WRITE(emu, 31, 0x1e0); |
| 109 | EMU8000_CCCA_WRITE(emu, 31, 0x1d8); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | */ |
| 116 | static void |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 117 | snd_emu8000_write_wait(struct snd_emu8000 *emu, int can_schedule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) { |
| 120 | if (can_schedule) { |
Nishanth Aravamudan | 8433a50 | 2005-10-24 15:02:37 +0200 | [diff] [blame] | 121 | schedule_timeout_interruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (signal_pending(current)) |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * close all channels |
| 130 | */ |
| 131 | static void |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 132 | emu8k_close_dram(struct snd_emu8000 *emu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | int i; |
| 135 | |
| 136 | for (i = 0; i < 2; i++) |
| 137 | snd_emux_unlock_voice(emu->emu, i); |
| 138 | for (; i < EMU8000_DRAM_VOICES; i++) { |
| 139 | snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE); |
| 140 | snd_emux_unlock_voice(emu->emu, i); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * convert Hz to AWE32 rate offset (see emux/soundfont.c) |
| 146 | */ |
| 147 | |
| 148 | #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */ |
| 149 | #define SAMPLERATE_RATIO 4096 |
| 150 | |
| 151 | static int calc_rate_offset(int hz) |
| 152 | { |
| 153 | return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* |
| 158 | */ |
| 159 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 160 | static struct snd_pcm_hardware emu8k_pcm_hw = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | #ifdef USE_NONINTERLEAVE |
| 162 | .info = SNDRV_PCM_INFO_NONINTERLEAVED, |
| 163 | #else |
| 164 | .info = SNDRV_PCM_INFO_INTERLEAVED, |
| 165 | #endif |
| 166 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 167 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, |
| 168 | .rate_min = 4000, |
| 169 | .rate_max = 48000, |
| 170 | .channels_min = 1, |
| 171 | .channels_max = 2, |
| 172 | .buffer_bytes_max = (128*1024), |
| 173 | .period_bytes_min = 1024, |
| 174 | .period_bytes_max = (128*1024), |
| 175 | .periods_min = 2, |
| 176 | .periods_max = 1024, |
| 177 | .fifo_size = 0, |
| 178 | |
| 179 | }; |
| 180 | |
| 181 | /* |
| 182 | * get the current position at the given channel from CCCA register |
| 183 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 184 | static inline int emu8k_get_curpos(struct snd_emu8k_pcm *rec, int ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff; |
| 187 | val -= rec->loop_start[ch] - 1; |
| 188 | return val; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | * timer interrupt handler |
| 194 | * check the current position and update the period if necessary. |
| 195 | */ |
| 196 | static void emu8k_pcm_timer_func(unsigned long data) |
| 197 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 198 | struct snd_emu8k_pcm *rec = (struct snd_emu8k_pcm *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | int ptr, delta; |
| 200 | |
| 201 | spin_lock(&rec->timer_lock); |
| 202 | /* update the current pointer */ |
| 203 | ptr = emu8k_get_curpos(rec, 0); |
| 204 | if (ptr < rec->last_ptr) |
| 205 | delta = ptr + rec->buf_size - rec->last_ptr; |
| 206 | else |
| 207 | delta = ptr - rec->last_ptr; |
| 208 | rec->period_pos += delta; |
| 209 | rec->last_ptr = ptr; |
| 210 | |
| 211 | /* reprogram timer */ |
Takashi Iwai | f05b412 | 2015-01-19 11:29:42 +0100 | [diff] [blame] | 212 | mod_timer(&rec->timer, jiffies + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | /* update period */ |
| 215 | if (rec->period_pos >= (int)rec->period_size) { |
| 216 | rec->period_pos %= rec->period_size; |
| 217 | spin_unlock(&rec->timer_lock); |
| 218 | snd_pcm_period_elapsed(rec->substream); |
| 219 | return; |
| 220 | } |
| 221 | spin_unlock(&rec->timer_lock); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* |
| 226 | * open pcm |
| 227 | * creating an instance here |
| 228 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 229 | static int emu8k_pcm_open(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 231 | struct snd_emu8000 *emu = snd_pcm_substream_chip(subs); |
| 232 | struct snd_emu8k_pcm *rec; |
| 233 | struct snd_pcm_runtime *runtime = subs->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Takashi Iwai | 9e76a76 | 2005-09-09 14:21:17 +0200 | [diff] [blame] | 235 | rec = kzalloc(sizeof(*rec), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | if (! rec) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | rec->emu = emu; |
| 240 | rec->substream = subs; |
| 241 | runtime->private_data = rec; |
| 242 | |
| 243 | spin_lock_init(&rec->timer_lock); |
Takashi Iwai | f05b412 | 2015-01-19 11:29:42 +0100 | [diff] [blame] | 244 | setup_timer(&rec->timer, emu8k_pcm_timer_func, (unsigned long)rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | runtime->hw = emu8k_pcm_hw; |
| 247 | runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3; |
| 248 | runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2; |
| 249 | |
| 250 | /* use timer to update periods.. (specified in msec) */ |
| 251 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 252 | (1000000 + HZ - 1) / HZ, UINT_MAX); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 257 | static int emu8k_pcm_close(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 259 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | kfree(rec); |
| 261 | subs->runtime->private_data = NULL; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * calculate pitch target |
| 267 | */ |
| 268 | static int calc_pitch_target(int pitch) |
| 269 | { |
| 270 | int ptarget = 1 << (pitch >> 12); |
| 271 | if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710; |
| 272 | if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710; |
| 273 | if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710; |
| 274 | ptarget += (ptarget >> 1); |
| 275 | if (ptarget > 0xffff) ptarget = 0xffff; |
| 276 | return ptarget; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * set up the voice |
| 281 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 282 | static void setup_voice(struct snd_emu8k_pcm *rec, int ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 284 | struct snd_emu8000 *hw = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | unsigned int temp; |
| 286 | |
| 287 | /* channel to be silent and idle */ |
| 288 | EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080); |
| 289 | EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF); |
| 290 | EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF); |
| 291 | EMU8000_PTRX_WRITE(hw, ch, 0); |
| 292 | EMU8000_CPF_WRITE(hw, ch, 0); |
| 293 | |
| 294 | /* pitch offset */ |
| 295 | EMU8000_IP_WRITE(hw, ch, rec->pitch); |
| 296 | /* set envelope parameters */ |
| 297 | EMU8000_ENVVAL_WRITE(hw, ch, 0x8000); |
| 298 | EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f); |
| 299 | EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f); |
| 300 | EMU8000_ENVVOL_WRITE(hw, ch, 0x8000); |
| 301 | EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f); |
| 302 | /* decay/sustain parameter for volume envelope is used |
| 303 | for triggerg the voice */ |
| 304 | /* modulation envelope heights */ |
| 305 | EMU8000_PEFE_WRITE(hw, ch, 0x0); |
| 306 | /* lfo1/2 delay */ |
| 307 | EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000); |
| 308 | EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000); |
| 309 | /* lfo1 pitch & cutoff shift */ |
| 310 | EMU8000_FMMOD_WRITE(hw, ch, 0); |
| 311 | /* lfo1 volume & freq */ |
| 312 | EMU8000_TREMFRQ_WRITE(hw, ch, 0); |
| 313 | /* lfo2 pitch & freq */ |
| 314 | EMU8000_FM2FRQ2_WRITE(hw, ch, 0); |
| 315 | /* pan & loop start */ |
| 316 | temp = rec->panning[ch]; |
| 317 | temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1); |
| 318 | EMU8000_PSST_WRITE(hw, ch, temp); |
| 319 | /* chorus & loop end (chorus 8bit, MSB) */ |
| 320 | temp = 0; // chorus |
| 321 | temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1); |
| 322 | EMU8000_CSL_WRITE(hw, ch, temp); |
| 323 | /* Q & current address (Q 4bit value, MSB) */ |
| 324 | temp = 0; // filterQ |
| 325 | temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1); |
| 326 | EMU8000_CCCA_WRITE(hw, ch, temp); |
| 327 | /* clear unknown registers */ |
| 328 | EMU8000_00A0_WRITE(hw, ch, 0); |
| 329 | EMU8000_0080_WRITE(hw, ch, 0); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * trigger the voice |
| 334 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 335 | static void start_voice(struct snd_emu8k_pcm *rec, int ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
| 337 | unsigned long flags; |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 338 | struct snd_emu8000 *hw = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | unsigned int temp, aux; |
| 340 | int pt = calc_pitch_target(rec->pitch); |
| 341 | |
| 342 | /* cutoff and volume */ |
| 343 | EMU8000_IFATN_WRITE(hw, ch, 0xff00); |
| 344 | EMU8000_VTFT_WRITE(hw, ch, 0xffff); |
| 345 | EMU8000_CVCF_WRITE(hw, ch, 0xffff); |
| 346 | /* trigger envelope */ |
| 347 | EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f); |
| 348 | /* set reverb and pitch target */ |
| 349 | temp = 0; // reverb |
| 350 | if (rec->panning[ch] == 0) |
| 351 | aux = 0xff; |
| 352 | else |
| 353 | aux = (-rec->panning[ch]) & 0xff; |
| 354 | temp = (temp << 8) | (pt << 16) | aux; |
| 355 | EMU8000_PTRX_WRITE(hw, ch, temp); |
| 356 | EMU8000_CPF_WRITE(hw, ch, pt << 16); |
| 357 | |
| 358 | /* start timer */ |
| 359 | spin_lock_irqsave(&rec->timer_lock, flags); |
| 360 | if (! rec->timer_running) { |
Takashi Iwai | f05b412 | 2015-01-19 11:29:42 +0100 | [diff] [blame] | 361 | mod_timer(&rec->timer, jiffies + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | rec->timer_running = 1; |
| 363 | } |
| 364 | spin_unlock_irqrestore(&rec->timer_lock, flags); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * stop the voice immediately |
| 369 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 370 | static void stop_voice(struct snd_emu8k_pcm *rec, int ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
| 372 | unsigned long flags; |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 373 | struct snd_emu8000 *hw = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F); |
| 376 | |
| 377 | /* stop timer */ |
| 378 | spin_lock_irqsave(&rec->timer_lock, flags); |
| 379 | if (rec->timer_running) { |
| 380 | del_timer(&rec->timer); |
| 381 | rec->timer_running = 0; |
| 382 | } |
| 383 | spin_unlock_irqrestore(&rec->timer_lock, flags); |
| 384 | } |
| 385 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 386 | static int emu8k_pcm_trigger(struct snd_pcm_substream *subs, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 388 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | int ch; |
| 390 | |
| 391 | switch (cmd) { |
| 392 | case SNDRV_PCM_TRIGGER_START: |
| 393 | for (ch = 0; ch < rec->voices; ch++) |
| 394 | start_voice(rec, ch); |
| 395 | rec->running = 1; |
| 396 | break; |
| 397 | case SNDRV_PCM_TRIGGER_STOP: |
| 398 | rec->running = 0; |
| 399 | for (ch = 0; ch < rec->voices; ch++) |
| 400 | stop_voice(rec, ch); |
| 401 | break; |
| 402 | default: |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | /* |
| 410 | * copy / silence ops |
| 411 | */ |
| 412 | |
| 413 | /* |
| 414 | * this macro should be inserted in the copy/silence loops |
| 415 | * to reduce the latency. without this, the system will hang up |
| 416 | * during the whole loop. |
| 417 | */ |
| 418 | #define CHECK_SCHEDULER() \ |
| 419 | do { \ |
| 420 | cond_resched();\ |
| 421 | if (signal_pending(current))\ |
| 422 | return -EAGAIN;\ |
| 423 | } while (0) |
| 424 | |
| 425 | |
| 426 | #ifdef USE_NONINTERLEAVE |
| 427 | /* copy one channel block */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 428 | static int emu8k_transfer_block(struct snd_emu8000 *emu, int offset, unsigned short *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | EMU8000_SMALW_WRITE(emu, offset); |
| 431 | while (count > 0) { |
| 432 | unsigned short sval; |
| 433 | CHECK_SCHEDULER(); |
Kulikov Vasiliy | 5157cc8 | 2010-07-28 20:40:51 +0400 | [diff] [blame] | 434 | if (get_user(sval, buf)) |
| 435 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | EMU8000_SMLD_WRITE(emu, sval); |
| 437 | buf++; |
| 438 | count--; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 443 | static int emu8k_pcm_copy(struct snd_pcm_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | int voice, |
| 445 | snd_pcm_uframes_t pos, |
| 446 | void *src, |
| 447 | snd_pcm_uframes_t count) |
| 448 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 449 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
| 450 | struct snd_emu8000 *emu = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | snd_emu8000_write_wait(emu, 1); |
| 453 | if (voice == -1) { |
| 454 | unsigned short *buf = src; |
| 455 | int i, err; |
| 456 | count /= rec->voices; |
| 457 | for (i = 0; i < rec->voices; i++) { |
| 458 | err = emu8k_transfer_block(emu, pos + rec->loop_start[i], buf, count); |
| 459 | if (err < 0) |
| 460 | return err; |
| 461 | buf += count; |
| 462 | } |
| 463 | return 0; |
| 464 | } else { |
| 465 | return emu8k_transfer_block(emu, pos + rec->loop_start[voice], src, count); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* make a channel block silence */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 470 | static int emu8k_silence_block(struct snd_emu8000 *emu, int offset, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | EMU8000_SMALW_WRITE(emu, offset); |
| 473 | while (count > 0) { |
| 474 | CHECK_SCHEDULER(); |
| 475 | EMU8000_SMLD_WRITE(emu, 0); |
| 476 | count--; |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 481 | static int emu8k_pcm_silence(struct snd_pcm_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | int voice, |
| 483 | snd_pcm_uframes_t pos, |
| 484 | snd_pcm_uframes_t count) |
| 485 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 486 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
| 487 | struct snd_emu8000 *emu = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | snd_emu8000_write_wait(emu, 1); |
| 490 | if (voice == -1 && rec->voices == 1) |
| 491 | voice = 0; |
| 492 | if (voice == -1) { |
| 493 | int err; |
| 494 | err = emu8k_silence_block(emu, pos + rec->loop_start[0], count / 2); |
| 495 | if (err < 0) |
| 496 | return err; |
| 497 | return emu8k_silence_block(emu, pos + rec->loop_start[1], count / 2); |
| 498 | } else { |
| 499 | return emu8k_silence_block(emu, pos + rec->loop_start[voice], count); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | #else /* interleave */ |
| 504 | |
| 505 | /* |
| 506 | * copy the interleaved data can be done easily by using |
| 507 | * DMA "left" and "right" channels on emu8k engine. |
| 508 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 509 | static int emu8k_pcm_copy(struct snd_pcm_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | int voice, |
| 511 | snd_pcm_uframes_t pos, |
| 512 | void __user *src, |
| 513 | snd_pcm_uframes_t count) |
| 514 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 515 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
| 516 | struct snd_emu8000 *emu = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | unsigned short __user *buf = src; |
| 518 | |
| 519 | snd_emu8000_write_wait(emu, 1); |
| 520 | EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]); |
| 521 | if (rec->voices > 1) |
| 522 | EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]); |
| 523 | |
| 524 | while (count-- > 0) { |
| 525 | unsigned short sval; |
| 526 | CHECK_SCHEDULER(); |
Kulikov Vasiliy | 5157cc8 | 2010-07-28 20:40:51 +0400 | [diff] [blame] | 527 | if (get_user(sval, buf)) |
| 528 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | EMU8000_SMLD_WRITE(emu, sval); |
| 530 | buf++; |
| 531 | if (rec->voices > 1) { |
| 532 | CHECK_SCHEDULER(); |
Kulikov Vasiliy | 5157cc8 | 2010-07-28 20:40:51 +0400 | [diff] [blame] | 533 | if (get_user(sval, buf)) |
| 534 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | EMU8000_SMRD_WRITE(emu, sval); |
| 536 | buf++; |
| 537 | } |
| 538 | } |
| 539 | return 0; |
| 540 | } |
| 541 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 542 | static int emu8k_pcm_silence(struct snd_pcm_substream *subs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | int voice, |
| 544 | snd_pcm_uframes_t pos, |
| 545 | snd_pcm_uframes_t count) |
| 546 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 547 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
| 548 | struct snd_emu8000 *emu = rec->emu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | snd_emu8000_write_wait(emu, 1); |
| 551 | EMU8000_SMALW_WRITE(emu, rec->loop_start[0] + pos); |
| 552 | if (rec->voices > 1) |
| 553 | EMU8000_SMARW_WRITE(emu, rec->loop_start[1] + pos); |
| 554 | while (count-- > 0) { |
| 555 | CHECK_SCHEDULER(); |
| 556 | EMU8000_SMLD_WRITE(emu, 0); |
| 557 | if (rec->voices > 1) { |
| 558 | CHECK_SCHEDULER(); |
| 559 | EMU8000_SMRD_WRITE(emu, 0); |
| 560 | } |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | |
| 567 | /* |
| 568 | * allocate a memory block |
| 569 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 570 | static int emu8k_pcm_hw_params(struct snd_pcm_substream *subs, |
| 571 | struct snd_pcm_hw_params *hw_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 573 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | if (rec->block) { |
| 576 | /* reallocation - release the old block */ |
| 577 | snd_util_mem_free(rec->emu->memhdr, rec->block); |
| 578 | rec->block = NULL; |
| 579 | } |
| 580 | |
| 581 | rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4; |
| 582 | rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes); |
| 583 | if (! rec->block) |
| 584 | return -ENOMEM; |
| 585 | rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */ |
| 586 | /* at least dma_bytes must be set for non-interleaved mode */ |
| 587 | subs->dma_buffer.bytes = params_buffer_bytes(hw_params); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * free the memory block |
| 594 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 595 | static int emu8k_pcm_hw_free(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 597 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | if (rec->block) { |
| 600 | int ch; |
| 601 | for (ch = 0; ch < rec->voices; ch++) |
| 602 | stop_voice(rec, ch); // to be sure |
| 603 | if (rec->dram_opened) |
| 604 | emu8k_close_dram(rec->emu); |
| 605 | snd_util_mem_free(rec->emu->memhdr, rec->block); |
| 606 | rec->block = NULL; |
| 607 | } |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | */ |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 613 | static int emu8k_pcm_prepare(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 615 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate); |
| 618 | rec->last_ptr = 0; |
| 619 | rec->period_pos = 0; |
| 620 | |
| 621 | rec->buf_size = subs->runtime->buffer_size; |
| 622 | rec->period_size = subs->runtime->period_size; |
| 623 | rec->voices = subs->runtime->channels; |
| 624 | rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE; |
| 625 | if (rec->voices > 1) |
| 626 | rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE; |
| 627 | if (rec->voices > 1) { |
| 628 | rec->panning[0] = 0xff; |
| 629 | rec->panning[1] = 0x00; |
| 630 | } else |
| 631 | rec->panning[0] = 0x80; |
| 632 | |
| 633 | if (! rec->dram_opened) { |
| 634 | int err, i, ch; |
| 635 | |
| 636 | snd_emux_terminate_all(rec->emu->emu); |
| 637 | if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0) |
| 638 | return err; |
| 639 | rec->dram_opened = 1; |
| 640 | |
| 641 | /* clear loop blanks */ |
| 642 | snd_emu8000_write_wait(rec->emu, 0); |
| 643 | EMU8000_SMALW_WRITE(rec->emu, rec->offset); |
| 644 | for (i = 0; i < LOOP_BLANK_SIZE; i++) |
| 645 | EMU8000_SMLD_WRITE(rec->emu, 0); |
| 646 | for (ch = 0; ch < rec->voices; ch++) { |
| 647 | EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size); |
| 648 | for (i = 0; i < LOOP_BLANK_SIZE; i++) |
| 649 | EMU8000_SMLD_WRITE(rec->emu, 0); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | setup_voice(rec, 0); |
| 654 | if (rec->voices > 1) |
| 655 | setup_voice(rec, 1); |
| 656 | return 0; |
| 657 | } |
| 658 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 659 | static snd_pcm_uframes_t emu8k_pcm_pointer(struct snd_pcm_substream *subs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 661 | struct snd_emu8k_pcm *rec = subs->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (rec->running) |
| 663 | return emu8k_get_curpos(rec, 0); |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 668 | static struct snd_pcm_ops emu8k_pcm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | .open = emu8k_pcm_open, |
| 670 | .close = emu8k_pcm_close, |
| 671 | .ioctl = snd_pcm_lib_ioctl, |
| 672 | .hw_params = emu8k_pcm_hw_params, |
| 673 | .hw_free = emu8k_pcm_hw_free, |
| 674 | .prepare = emu8k_pcm_prepare, |
| 675 | .trigger = emu8k_pcm_trigger, |
| 676 | .pointer = emu8k_pcm_pointer, |
| 677 | .copy = emu8k_pcm_copy, |
| 678 | .silence = emu8k_pcm_silence, |
| 679 | }; |
| 680 | |
| 681 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 682 | static void snd_emu8000_pcm_free(struct snd_pcm *pcm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 684 | struct snd_emu8000 *emu = pcm->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | emu->pcm = NULL; |
| 686 | } |
| 687 | |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 688 | int snd_emu8000_pcm_new(struct snd_card *card, struct snd_emu8000 *emu, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Takashi Iwai | 029d64b | 2005-11-17 14:34:36 +0100 | [diff] [blame] | 690 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | int err; |
| 692 | |
| 693 | if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0) |
| 694 | return err; |
| 695 | pcm->private_data = emu; |
| 696 | pcm->private_free = snd_emu8000_pcm_free; |
| 697 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops); |
| 698 | emu->pcm = pcm; |
| 699 | |
| 700 | snd_device_register(card, pcm); |
| 701 | |
| 702 | return 0; |
| 703 | } |