summaryrefslogtreecommitdiffstats
path: root/sound/pci/emu10k1/emu10k1_patch.c
blob: dbfa89435ac2a4363f19b543302e859f4d5572a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *  Patch transfer callback for Emu10k1
 *
 *  Copyright (C) 2000 Takashi iwai <tiwai@suse.de>
 */
/*
 * All the code for loading in a patch.  There is very little that is
 * chip specific here.  Just the actual writing to the board.
 */

#include "emu10k1_synth_local.h"

/*
 */
#define BLANK_LOOP_START	4
#define BLANK_LOOP_END		8
#define BLANK_LOOP_SIZE		12
#define BLANK_HEAD_SIZE		3

/*
 * allocate a sample block and copy data from userspace
 */
int
snd_emu10k1_sample_new(struct snd_emux *rec, struct snd_sf_sample *sp,
		       struct snd_util_memhdr *hdr,
		       const void __user *data, long count)
{
	u8 fill;
	u32 xor;
	int shift;
	int offset;
	int truesize, size, blocksize;
	int loop_start, loop_end, loop_size, data_end, unroll;
	struct snd_emu10k1 *emu;

	emu = rec->hw;
	if (snd_BUG_ON(!sp || !hdr))
		return -EINVAL;

	if (sp->v.mode_flags & (SNDRV_SFNT_SAMPLE_BIDIR_LOOP | SNDRV_SFNT_SAMPLE_REVERSE_LOOP)) {
		/* should instead return -ENOTSUPP; but compatibility */
		printk(KERN_WARNING "Emu10k1 wavetable patch %d with unsupported loop feature\n",
		       sp->v.sample);
	}

	if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_8BITS) {
		shift = 0;
		fill = 0x80;
		xor = (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_UNSIGNED) ? 0 : 0x80808080;
	} else {
		shift = 1;
		fill = 0;
		xor = (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_UNSIGNED) ? 0x80008000 : 0;
	}

	/* compute true data size to be loaded */
	truesize = sp->v.size + BLANK_HEAD_SIZE;
	if (sp->v.mode_flags & SNDRV_SFNT_SAMPLE_NO_BLANK) {
		truesize += BLANK_LOOP_SIZE;
		/* if no blank loop is attached in the sample, add it */
		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;
		}
	}

	loop_start = sp->v.loopstart;
	loop_end = sp->v.loopend;
	loop_size = loop_end - loop_start;
	if (!loop_size)
		return -EINVAL;
	data_end = sp->v.end;

	/* recalculate offset */
	sp->v.start += BLANK_HEAD_SIZE;
	sp->v.end += BLANK_HEAD_SIZE;
	sp->v.loopstart += BLANK_HEAD_SIZE;
	sp->v.loopend += BLANK_HEAD_SIZE;

	// Automatic pre-filling of the cache does not work in the presence
	// of loops (*), and we don't want to fill it manually, as that is
	// fiddly and slow. So we unroll the loop until the loop end is
	// beyond the cache size.
	// (*) Strictly speaking, a single iteration is supported (that's
	// how it works when the playback engine runs), but handling this
	// special case is not worth it.
	unroll = 0;
	while (sp->v.loopend < 64) {
		truesize += loop_size;
		sp->v.loopstart += loop_size;
		sp->v.loopend += loop_size;
		sp->v.end += loop_size;
		unroll++;
	}

	/* try to allocate a memory block */
	blocksize = truesize << shift;
	sp->block = snd_emu10k1_synth_alloc(emu, blocksize);
	if (sp->block == NULL) {
		dev_dbg(emu->card->dev,
			"synth malloc failed (size=%d)\n", blocksize);
		/* not ENOMEM (for compatibility with OSS) */
		return -ENOSPC;
	}
	/* set the total size */
	sp->v.truesize = blocksize;

	/* write blank samples at head */
	offset = 0;
	size = BLANK_HEAD_SIZE << shift;
	snd_emu10k1_synth_memset(emu, sp->block, offset, size, fill);
	offset += size;

	/* copy provided samples */
	if (unroll && loop_end <= data_end) {
		size = loop_end << shift;
		if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size, xor))
			goto faulty;
		offset += size;

		data += loop_start << shift;
		while (--unroll > 0) {
			size = loop_size << shift;
			if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size, xor))
				goto faulty;
			offset += size;
		}

		size = (data_end - loop_start) << shift;
	} else {
		size = data_end << shift;
	}
	if (snd_emu10k1_synth_copy_from_user(emu, sp->block, offset, data, size, xor))
		goto faulty;
	offset += size;

	/* clear rest of samples (if any) */
	if (offset < blocksize)
		snd_emu10k1_synth_memset(emu, sp->block, offset, blocksize - offset, fill);

	return 0;

faulty:
	snd_emu10k1_synth_free(emu, sp->block);
	sp->block = NULL;
	return -EFAULT;
}

/*
 * free a sample block
 */
int
snd_emu10k1_sample_free(struct snd_emux *rec, struct snd_sf_sample *sp,
			struct snd_util_memhdr *hdr)
{
	struct snd_emu10k1 *emu;

	emu = rec->hw;
	if (snd_BUG_ON(!sp || !hdr))
		return -EINVAL;

	if (sp->block) {
		snd_emu10k1_synth_free(emu, sp->block);
		sp->block = NULL;
	}
	return 0;
}