1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "emu8000_local.h"
22
23#include <linux/sched/signal.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <sound/initval.h>
27#include <sound/pcm.h>
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52struct snd_emu8k_pcm {
53 struct snd_emu8000 *emu;
54 struct snd_pcm_substream *substream;
55
56 unsigned int allocated_bytes;
57 struct snd_util_memblk *block;
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
79
80static int
81emu8k_open_dram_for_pcm(struct snd_emu8000 *emu, int channels)
82{
83 int i;
84
85
86 snd_emux_lock_voice(emu->emu, 0);
87 if (channels > 1)
88 snd_emux_lock_voice(emu->emu, 1);
89
90
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
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
116static void
117snd_emu8000_write_wait(struct snd_emu8000 *emu, int can_schedule)
118{
119 while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
120 if (can_schedule) {
121 schedule_timeout_interruptible(1);
122 if (signal_pending(current))
123 break;
124 }
125 }
126}
127
128
129
130
131static void
132emu8k_close_dram(struct snd_emu8000 *emu)
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
146
147
148#define OFFSET_SAMPLERATE 1011119
149#define SAMPLERATE_RATIO 4096
150
151static 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
160static const struct snd_pcm_hardware emu8k_pcm_hw = {
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
183
184static inline int emu8k_get_curpos(struct snd_emu8k_pcm *rec, int ch)
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
194
195
196static void emu8k_pcm_timer_func(struct timer_list *t)
197{
198 struct snd_emu8k_pcm *rec = from_timer(rec, t, timer);
199 int ptr, delta;
200
201 spin_lock(&rec->timer_lock);
202
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
212 mod_timer(&rec->timer, jiffies + 1);
213
214
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
227
228
229static int emu8k_pcm_open(struct snd_pcm_substream *subs)
230{
231 struct snd_emu8000 *emu = snd_pcm_substream_chip(subs);
232 struct snd_emu8k_pcm *rec;
233 struct snd_pcm_runtime *runtime = subs->runtime;
234
235 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
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);
244 timer_setup(&rec->timer, emu8k_pcm_timer_func, 0);
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
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
257static int emu8k_pcm_close(struct snd_pcm_substream *subs)
258{
259 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
260 kfree(rec);
261 subs->runtime->private_data = NULL;
262 return 0;
263}
264
265
266
267
268static 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
281
282static void setup_voice(struct snd_emu8k_pcm *rec, int ch)
283{
284 struct snd_emu8000 *hw = rec->emu;
285 unsigned int temp;
286
287
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
295 EMU8000_IP_WRITE(hw, ch, rec->pitch);
296
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
303
304
305 EMU8000_PEFE_WRITE(hw, ch, 0x0);
306
307 EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
308 EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
309
310 EMU8000_FMMOD_WRITE(hw, ch, 0);
311
312 EMU8000_TREMFRQ_WRITE(hw, ch, 0);
313
314 EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
315
316 temp = rec->panning[ch];
317 temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
318 EMU8000_PSST_WRITE(hw, ch, temp);
319
320 temp = 0;
321 temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
322 EMU8000_CSL_WRITE(hw, ch, temp);
323
324 temp = 0;
325 temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
326 EMU8000_CCCA_WRITE(hw, ch, temp);
327
328 EMU8000_00A0_WRITE(hw, ch, 0);
329 EMU8000_0080_WRITE(hw, ch, 0);
330}
331
332
333
334
335static void start_voice(struct snd_emu8k_pcm *rec, int ch)
336{
337 unsigned long flags;
338 struct snd_emu8000 *hw = rec->emu;
339 unsigned int temp, aux;
340 int pt = calc_pitch_target(rec->pitch);
341
342
343 EMU8000_IFATN_WRITE(hw, ch, 0xff00);
344 EMU8000_VTFT_WRITE(hw, ch, 0xffff);
345 EMU8000_CVCF_WRITE(hw, ch, 0xffff);
346
347 EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
348
349 temp = 0;
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
359 spin_lock_irqsave(&rec->timer_lock, flags);
360 if (! rec->timer_running) {
361 mod_timer(&rec->timer, jiffies + 1);
362 rec->timer_running = 1;
363 }
364 spin_unlock_irqrestore(&rec->timer_lock, flags);
365}
366
367
368
369
370static void stop_voice(struct snd_emu8k_pcm *rec, int ch)
371{
372 unsigned long flags;
373 struct snd_emu8000 *hw = rec->emu;
374
375 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
376
377
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
386static int emu8k_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
387{
388 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
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
411
412
413
414
415
416
417
418#define CHECK_SCHEDULER() \
419do { \
420 cond_resched();\
421 if (signal_pending(current))\
422 return -EAGAIN;\
423} while (0)
424
425enum {
426 COPY_USER, COPY_KERNEL, FILL_SILENCE,
427};
428
429#define GET_VAL(sval, buf, mode) \
430 do { \
431 switch (mode) { \
432 case FILL_SILENCE: \
433 sval = 0; \
434 break; \
435 case COPY_KERNEL: \
436 sval = *buf++; \
437 break; \
438 default: \
439 if (get_user(sval, (unsigned short __user *)buf)) \
440 return -EFAULT; \
441 buf++; \
442 break; \
443 } \
444 } while (0)
445
446#ifdef USE_NONINTERLEAVE
447
448#define LOOP_WRITE(rec, offset, _buf, count, mode) \
449 do { \
450 struct snd_emu8000 *emu = (rec)->emu; \
451 unsigned short *buf = (unsigned short *)(_buf); \
452 snd_emu8000_write_wait(emu, 1); \
453 EMU8000_SMALW_WRITE(emu, offset); \
454 while (count > 0) { \
455 unsigned short sval; \
456 CHECK_SCHEDULER(); \
457 GET_VAL(sval, buf, mode); \
458 EMU8000_SMLD_WRITE(emu, sval); \
459 count--; \
460 } \
461 } while (0)
462
463
464static int emu8k_pcm_copy(struct snd_pcm_substream *subs,
465 int voice, unsigned long pos,
466 void __user *src, unsigned long count)
467{
468 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
469
470
471 pos = (pos << 1) + rec->loop_start[voice];
472 count <<= 1;
473 LOOP_WRITE(rec, pos, src, count, COPY_USER);
474 return 0;
475}
476
477static int emu8k_pcm_copy_kernel(struct snd_pcm_substream *subs,
478 int voice, unsigned long pos,
479 void *src, unsigned long count)
480{
481 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
482
483
484 pos = (pos << 1) + rec->loop_start[voice];
485 count <<= 1;
486 LOOP_WRITE(rec, pos, src, count, COPY_KERNEL);
487 return 0;
488}
489
490
491static int emu8k_pcm_silence(struct snd_pcm_substream *subs,
492 int voice, unsigned long pos, unsigned long count)
493{
494 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
495
496
497 pos = (pos << 1) + rec->loop_start[voice];
498 count <<= 1;
499 LOOP_WRITE(rec, pos, NULL, count, FILL_SILENCE);
500 return 0;
501}
502
503#else
504
505#define LOOP_WRITE(rec, pos, _buf, count, mode) \
506 do { \
507 struct snd_emu8000 *emu = rec->emu; \
508 unsigned short *buf = (unsigned short *)(_buf); \
509 snd_emu8000_write_wait(emu, 1); \
510 EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]); \
511 if (rec->voices > 1) \
512 EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]); \
513 while (count > 0) { \
514 unsigned short sval; \
515 CHECK_SCHEDULER(); \
516 GET_VAL(sval, buf, mode); \
517 EMU8000_SMLD_WRITE(emu, sval); \
518 if (rec->voices > 1) { \
519 CHECK_SCHEDULER(); \
520 GET_VAL(sval, buf, mode); \
521 EMU8000_SMRD_WRITE(emu, sval); \
522 } \
523 count--; \
524 } \
525 } while (0)
526
527
528
529
530
531
532static int emu8k_pcm_copy(struct snd_pcm_substream *subs,
533 int voice, unsigned long pos,
534 void __user *src, unsigned long count)
535{
536 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
537
538
539 pos = bytes_to_frames(subs->runtime, pos);
540 count = bytes_to_frames(subs->runtime, count);
541 LOOP_WRITE(rec, pos, src, count, COPY_USER);
542 return 0;
543}
544
545static int emu8k_pcm_copy_kernel(struct snd_pcm_substream *subs,
546 int voice, unsigned long pos,
547 void *src, unsigned long count)
548{
549 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
550
551
552 pos = bytes_to_frames(subs->runtime, pos);
553 count = bytes_to_frames(subs->runtime, count);
554 LOOP_WRITE(rec, pos, src, count, COPY_KERNEL);
555 return 0;
556}
557
558static int emu8k_pcm_silence(struct snd_pcm_substream *subs,
559 int voice, unsigned long pos, unsigned long count)
560{
561 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
562
563
564 pos = bytes_to_frames(subs->runtime, pos);
565 count = bytes_to_frames(subs->runtime, count);
566 LOOP_WRITE(rec, pos, NULL, count, FILL_SILENCE);
567 return 0;
568}
569#endif
570
571
572
573
574
575static int emu8k_pcm_hw_params(struct snd_pcm_substream *subs,
576 struct snd_pcm_hw_params *hw_params)
577{
578 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
579
580 if (rec->block) {
581
582 snd_util_mem_free(rec->emu->memhdr, rec->block);
583 rec->block = NULL;
584 }
585
586 rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
587 rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
588 if (! rec->block)
589 return -ENOMEM;
590 rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1);
591
592 subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
593
594 return 0;
595}
596
597
598
599
600static int emu8k_pcm_hw_free(struct snd_pcm_substream *subs)
601{
602 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
603
604 if (rec->block) {
605 int ch;
606 for (ch = 0; ch < rec->voices; ch++)
607 stop_voice(rec, ch);
608 if (rec->dram_opened)
609 emu8k_close_dram(rec->emu);
610 snd_util_mem_free(rec->emu->memhdr, rec->block);
611 rec->block = NULL;
612 }
613 return 0;
614}
615
616
617
618static int emu8k_pcm_prepare(struct snd_pcm_substream *subs)
619{
620 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
621
622 rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
623 rec->last_ptr = 0;
624 rec->period_pos = 0;
625
626 rec->buf_size = subs->runtime->buffer_size;
627 rec->period_size = subs->runtime->period_size;
628 rec->voices = subs->runtime->channels;
629 rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
630 if (rec->voices > 1)
631 rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
632 if (rec->voices > 1) {
633 rec->panning[0] = 0xff;
634 rec->panning[1] = 0x00;
635 } else
636 rec->panning[0] = 0x80;
637
638 if (! rec->dram_opened) {
639 int err, i, ch;
640
641 snd_emux_terminate_all(rec->emu->emu);
642 if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
643 return err;
644 rec->dram_opened = 1;
645
646
647 snd_emu8000_write_wait(rec->emu, 0);
648 EMU8000_SMALW_WRITE(rec->emu, rec->offset);
649 for (i = 0; i < LOOP_BLANK_SIZE; i++)
650 EMU8000_SMLD_WRITE(rec->emu, 0);
651 for (ch = 0; ch < rec->voices; ch++) {
652 EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
653 for (i = 0; i < LOOP_BLANK_SIZE; i++)
654 EMU8000_SMLD_WRITE(rec->emu, 0);
655 }
656 }
657
658 setup_voice(rec, 0);
659 if (rec->voices > 1)
660 setup_voice(rec, 1);
661 return 0;
662}
663
664static snd_pcm_uframes_t emu8k_pcm_pointer(struct snd_pcm_substream *subs)
665{
666 struct snd_emu8k_pcm *rec = subs->runtime->private_data;
667 if (rec->running)
668 return emu8k_get_curpos(rec, 0);
669 return 0;
670}
671
672
673static const struct snd_pcm_ops emu8k_pcm_ops = {
674 .open = emu8k_pcm_open,
675 .close = emu8k_pcm_close,
676 .ioctl = snd_pcm_lib_ioctl,
677 .hw_params = emu8k_pcm_hw_params,
678 .hw_free = emu8k_pcm_hw_free,
679 .prepare = emu8k_pcm_prepare,
680 .trigger = emu8k_pcm_trigger,
681 .pointer = emu8k_pcm_pointer,
682 .copy_user = emu8k_pcm_copy,
683 .copy_kernel = emu8k_pcm_copy_kernel,
684 .fill_silence = emu8k_pcm_silence,
685};
686
687
688static void snd_emu8000_pcm_free(struct snd_pcm *pcm)
689{
690 struct snd_emu8000 *emu = pcm->private_data;
691 emu->pcm = NULL;
692}
693
694int snd_emu8000_pcm_new(struct snd_card *card, struct snd_emu8000 *emu, int index)
695{
696 struct snd_pcm *pcm;
697 int err;
698
699 if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
700 return err;
701 pcm->private_data = emu;
702 pcm->private_free = snd_emu8000_pcm_free;
703 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
704 emu->pcm = pcm;
705
706 snd_device_register(card, pcm);
707
708 return 0;
709}
710