Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * GF1 (GUS) Patch - Instrument routines | |
3 | * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | * | |
19 | */ | |
20 | ||
21 | #include <sound/driver.h> | |
22 | #include <linux/init.h> | |
1da177e4 LT |
23 | #include <linux/slab.h> |
24 | #include <sound/core.h> | |
25 | #include <sound/ainstr_gf1.h> | |
26 | #include <sound/initval.h> | |
27 | #include <asm/uaccess.h> | |
28 | ||
29 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); | |
30 | MODULE_DESCRIPTION("Advanced Linux Sound Architecture GF1 (GUS) Patch support."); | |
31 | MODULE_LICENSE("GPL"); | |
32 | ||
33 | static unsigned int snd_seq_gf1_size(unsigned int size, unsigned int format) | |
34 | { | |
35 | unsigned int result = size; | |
36 | ||
37 | if (format & GF1_WAVE_16BIT) | |
38 | result <<= 1; | |
39 | if (format & GF1_WAVE_STEREO) | |
40 | result <<= 1; | |
41 | return format; | |
42 | } | |
43 | ||
19ac31e8 TI |
44 | static int snd_seq_gf1_copy_wave_from_stream(struct snd_gf1_ops *ops, |
45 | struct gf1_instrument *ip, | |
1da177e4 LT |
46 | char __user **data, |
47 | long *len, | |
48 | int atomic) | |
49 | { | |
19ac31e8 TI |
50 | struct gf1_wave *wp, *prev; |
51 | struct gf1_xwave xp; | |
5a0f217d | 52 | int err; |
1ef64e67 | 53 | gfp_t gfp_mask; |
1da177e4 LT |
54 | unsigned int real_size; |
55 | ||
56 | gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL; | |
57 | if (*len < (long)sizeof(xp)) | |
58 | return -EINVAL; | |
59 | if (copy_from_user(&xp, *data, sizeof(xp))) | |
60 | return -EFAULT; | |
61 | *data += sizeof(xp); | |
62 | *len -= sizeof(xp); | |
ecca82b4 | 63 | wp = kzalloc(sizeof(*wp), gfp_mask); |
1da177e4 LT |
64 | if (wp == NULL) |
65 | return -ENOMEM; | |
66 | wp->share_id[0] = le32_to_cpu(xp.share_id[0]); | |
67 | wp->share_id[1] = le32_to_cpu(xp.share_id[1]); | |
68 | wp->share_id[2] = le32_to_cpu(xp.share_id[2]); | |
69 | wp->share_id[3] = le32_to_cpu(xp.share_id[3]); | |
70 | wp->format = le32_to_cpu(xp.format); | |
71 | wp->size = le32_to_cpu(xp.size); | |
72 | wp->start = le32_to_cpu(xp.start); | |
73 | wp->loop_start = le32_to_cpu(xp.loop_start); | |
74 | wp->loop_end = le32_to_cpu(xp.loop_end); | |
75 | wp->loop_repeat = le16_to_cpu(xp.loop_repeat); | |
76 | wp->flags = xp.flags; | |
77 | wp->sample_rate = le32_to_cpu(xp.sample_rate); | |
78 | wp->low_frequency = le32_to_cpu(xp.low_frequency); | |
79 | wp->high_frequency = le32_to_cpu(xp.high_frequency); | |
80 | wp->root_frequency = le32_to_cpu(xp.root_frequency); | |
81 | wp->tune = le16_to_cpu(xp.tune); | |
82 | wp->balance = xp.balance; | |
83 | memcpy(wp->envelope_rate, xp.envelope_rate, 6); | |
84 | memcpy(wp->envelope_offset, xp.envelope_offset, 6); | |
85 | wp->tremolo_sweep = xp.tremolo_sweep; | |
86 | wp->tremolo_rate = xp.tremolo_rate; | |
87 | wp->tremolo_depth = xp.tremolo_depth; | |
88 | wp->vibrato_sweep = xp.vibrato_sweep; | |
89 | wp->vibrato_rate = xp.vibrato_rate; | |
90 | wp->vibrato_depth = xp.vibrato_depth; | |
91 | wp->scale_frequency = le16_to_cpu(xp.scale_frequency); | |
92 | wp->scale_factor = le16_to_cpu(xp.scale_factor); | |
93 | real_size = snd_seq_gf1_size(wp->size, wp->format); | |
94 | if ((long)real_size > *len) { | |
95 | kfree(wp); | |
96 | return -ENOMEM; | |
97 | } | |
98 | if (ops->put_sample) { | |
99 | err = ops->put_sample(ops->private_data, wp, | |
100 | *data, real_size, atomic); | |
101 | if (err < 0) { | |
102 | kfree(wp); | |
103 | return err; | |
104 | } | |
105 | } | |
106 | *data += real_size; | |
107 | *len -= real_size; | |
108 | prev = ip->wave; | |
109 | if (prev) { | |
110 | while (prev->next) prev = prev->next; | |
111 | prev->next = wp; | |
112 | } else { | |
113 | ip->wave = wp; | |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
19ac31e8 TI |
118 | static void snd_seq_gf1_wave_free(struct snd_gf1_ops *ops, |
119 | struct gf1_wave *wave, | |
1da177e4 LT |
120 | int atomic) |
121 | { | |
122 | if (ops->remove_sample) | |
123 | ops->remove_sample(ops->private_data, wave, atomic); | |
124 | kfree(wave); | |
125 | } | |
126 | ||
19ac31e8 TI |
127 | static void snd_seq_gf1_instr_free(struct snd_gf1_ops *ops, |
128 | struct gf1_instrument *ip, | |
1da177e4 LT |
129 | int atomic) |
130 | { | |
19ac31e8 | 131 | struct gf1_wave *wave; |
1da177e4 LT |
132 | |
133 | while ((wave = ip->wave) != NULL) { | |
134 | ip->wave = wave->next; | |
135 | snd_seq_gf1_wave_free(ops, wave, atomic); | |
136 | } | |
137 | } | |
138 | ||
19ac31e8 | 139 | static int snd_seq_gf1_put(void *private_data, struct snd_seq_kinstr *instr, |
1da177e4 LT |
140 | char __user *instr_data, long len, int atomic, |
141 | int cmd) | |
142 | { | |
19ac31e8 TI |
143 | struct snd_gf1_ops *ops = private_data; |
144 | struct gf1_instrument *ip; | |
145 | struct gf1_xinstrument ix; | |
1ef64e67 AV |
146 | int err; |
147 | gfp_t gfp_mask; | |
1da177e4 LT |
148 | |
149 | if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE) | |
150 | return -EINVAL; | |
151 | gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL; | |
152 | /* copy instrument data */ | |
153 | if (len < (long)sizeof(ix)) | |
154 | return -EINVAL; | |
155 | if (copy_from_user(&ix, instr_data, sizeof(ix))) | |
156 | return -EFAULT; | |
157 | if (ix.stype != GF1_STRU_INSTR) | |
158 | return -EINVAL; | |
159 | instr_data += sizeof(ix); | |
160 | len -= sizeof(ix); | |
19ac31e8 | 161 | ip = (struct gf1_instrument *)KINSTR_DATA(instr); |
1da177e4 LT |
162 | ip->exclusion = le16_to_cpu(ix.exclusion); |
163 | ip->exclusion_group = le16_to_cpu(ix.exclusion_group); | |
164 | ip->effect1 = ix.effect1; | |
165 | ip->effect1_depth = ix.effect1_depth; | |
166 | ip->effect2 = ix.effect2; | |
167 | ip->effect2_depth = ix.effect2_depth; | |
168 | /* copy layers */ | |
169 | while (len > (long)sizeof(__u32)) { | |
170 | __u32 stype; | |
171 | ||
172 | if (copy_from_user(&stype, instr_data, sizeof(stype))) | |
173 | return -EFAULT; | |
174 | if (stype != GF1_STRU_WAVE) { | |
175 | snd_seq_gf1_instr_free(ops, ip, atomic); | |
176 | return -EINVAL; | |
177 | } | |
178 | err = snd_seq_gf1_copy_wave_from_stream(ops, | |
179 | ip, | |
180 | &instr_data, | |
181 | &len, | |
182 | atomic); | |
183 | if (err < 0) { | |
184 | snd_seq_gf1_instr_free(ops, ip, atomic); | |
185 | return err; | |
186 | } | |
187 | } | |
188 | return 0; | |
189 | } | |
190 | ||
19ac31e8 TI |
191 | static int snd_seq_gf1_copy_wave_to_stream(struct snd_gf1_ops *ops, |
192 | struct gf1_instrument *ip, | |
1da177e4 LT |
193 | char __user **data, |
194 | long *len, | |
195 | int atomic) | |
196 | { | |
19ac31e8 TI |
197 | struct gf1_wave *wp; |
198 | struct gf1_xwave xp; | |
1da177e4 LT |
199 | int err; |
200 | unsigned int real_size; | |
201 | ||
202 | for (wp = ip->wave; wp; wp = wp->next) { | |
203 | if (*len < (long)sizeof(xp)) | |
204 | return -ENOMEM; | |
205 | memset(&xp, 0, sizeof(xp)); | |
206 | xp.stype = GF1_STRU_WAVE; | |
207 | xp.share_id[0] = cpu_to_le32(wp->share_id[0]); | |
208 | xp.share_id[1] = cpu_to_le32(wp->share_id[1]); | |
209 | xp.share_id[2] = cpu_to_le32(wp->share_id[2]); | |
210 | xp.share_id[3] = cpu_to_le32(wp->share_id[3]); | |
211 | xp.format = cpu_to_le32(wp->format); | |
212 | xp.size = cpu_to_le32(wp->size); | |
213 | xp.start = cpu_to_le32(wp->start); | |
214 | xp.loop_start = cpu_to_le32(wp->loop_start); | |
215 | xp.loop_end = cpu_to_le32(wp->loop_end); | |
216 | xp.loop_repeat = cpu_to_le32(wp->loop_repeat); | |
217 | xp.flags = wp->flags; | |
218 | xp.sample_rate = cpu_to_le32(wp->sample_rate); | |
219 | xp.low_frequency = cpu_to_le32(wp->low_frequency); | |
220 | xp.high_frequency = cpu_to_le32(wp->high_frequency); | |
221 | xp.root_frequency = cpu_to_le32(wp->root_frequency); | |
222 | xp.tune = cpu_to_le16(wp->tune); | |
223 | xp.balance = wp->balance; | |
224 | memcpy(xp.envelope_rate, wp->envelope_rate, 6); | |
225 | memcpy(xp.envelope_offset, wp->envelope_offset, 6); | |
226 | xp.tremolo_sweep = wp->tremolo_sweep; | |
227 | xp.tremolo_rate = wp->tremolo_rate; | |
228 | xp.tremolo_depth = wp->tremolo_depth; | |
229 | xp.vibrato_sweep = wp->vibrato_sweep; | |
230 | xp.vibrato_rate = wp->vibrato_rate; | |
231 | xp.vibrato_depth = wp->vibrato_depth; | |
232 | xp.scale_frequency = cpu_to_le16(wp->scale_frequency); | |
233 | xp.scale_factor = cpu_to_le16(wp->scale_factor); | |
234 | if (copy_to_user(*data, &xp, sizeof(xp))) | |
235 | return -EFAULT; | |
236 | *data += sizeof(xp); | |
237 | *len -= sizeof(xp); | |
238 | real_size = snd_seq_gf1_size(wp->size, wp->format); | |
239 | if (*len < (long)real_size) | |
240 | return -ENOMEM; | |
241 | if (ops->get_sample) { | |
242 | err = ops->get_sample(ops->private_data, wp, | |
243 | *data, real_size, atomic); | |
244 | if (err < 0) | |
245 | return err; | |
246 | } | |
247 | *data += wp->size; | |
248 | *len -= wp->size; | |
249 | } | |
250 | return 0; | |
251 | } | |
252 | ||
19ac31e8 | 253 | static int snd_seq_gf1_get(void *private_data, struct snd_seq_kinstr *instr, |
1da177e4 LT |
254 | char __user *instr_data, long len, int atomic, |
255 | int cmd) | |
256 | { | |
19ac31e8 TI |
257 | struct snd_gf1_ops *ops = private_data; |
258 | struct gf1_instrument *ip; | |
259 | struct gf1_xinstrument ix; | |
1da177e4 LT |
260 | |
261 | if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL) | |
262 | return -EINVAL; | |
263 | if (len < (long)sizeof(ix)) | |
264 | return -ENOMEM; | |
265 | memset(&ix, 0, sizeof(ix)); | |
19ac31e8 | 266 | ip = (struct gf1_instrument *)KINSTR_DATA(instr); |
1da177e4 LT |
267 | ix.stype = GF1_STRU_INSTR; |
268 | ix.exclusion = cpu_to_le16(ip->exclusion); | |
269 | ix.exclusion_group = cpu_to_le16(ip->exclusion_group); | |
270 | ix.effect1 = cpu_to_le16(ip->effect1); | |
271 | ix.effect1_depth = cpu_to_le16(ip->effect1_depth); | |
272 | ix.effect2 = ip->effect2; | |
273 | ix.effect2_depth = ip->effect2_depth; | |
274 | if (copy_to_user(instr_data, &ix, sizeof(ix))) | |
275 | return -EFAULT; | |
276 | instr_data += sizeof(ix); | |
277 | len -= sizeof(ix); | |
278 | return snd_seq_gf1_copy_wave_to_stream(ops, | |
279 | ip, | |
280 | &instr_data, | |
281 | &len, | |
282 | atomic); | |
283 | } | |
284 | ||
19ac31e8 | 285 | static int snd_seq_gf1_get_size(void *private_data, struct snd_seq_kinstr *instr, |
1da177e4 LT |
286 | long *size) |
287 | { | |
288 | long result; | |
19ac31e8 TI |
289 | struct gf1_instrument *ip; |
290 | struct gf1_wave *wp; | |
1da177e4 LT |
291 | |
292 | *size = 0; | |
19ac31e8 TI |
293 | ip = (struct gf1_instrument *)KINSTR_DATA(instr); |
294 | result = sizeof(struct gf1_xinstrument); | |
1da177e4 | 295 | for (wp = ip->wave; wp; wp = wp->next) { |
19ac31e8 | 296 | result += sizeof(struct gf1_xwave); |
1da177e4 LT |
297 | result += wp->size; |
298 | } | |
299 | *size = result; | |
300 | return 0; | |
301 | } | |
302 | ||
303 | static int snd_seq_gf1_remove(void *private_data, | |
19ac31e8 | 304 | struct snd_seq_kinstr *instr, |
1da177e4 LT |
305 | int atomic) |
306 | { | |
19ac31e8 TI |
307 | struct snd_gf1_ops *ops = private_data; |
308 | struct gf1_instrument *ip; | |
1da177e4 | 309 | |
19ac31e8 | 310 | ip = (struct gf1_instrument *)KINSTR_DATA(instr); |
1da177e4 LT |
311 | snd_seq_gf1_instr_free(ops, ip, atomic); |
312 | return 0; | |
313 | } | |
314 | ||
315 | static void snd_seq_gf1_notify(void *private_data, | |
19ac31e8 | 316 | struct snd_seq_kinstr *instr, |
1da177e4 LT |
317 | int what) |
318 | { | |
19ac31e8 | 319 | struct snd_gf1_ops *ops = private_data; |
1da177e4 LT |
320 | |
321 | if (ops->notify) | |
322 | ops->notify(ops->private_data, instr, what); | |
323 | } | |
324 | ||
19ac31e8 | 325 | int snd_seq_gf1_init(struct snd_gf1_ops *ops, |
1da177e4 | 326 | void *private_data, |
19ac31e8 | 327 | struct snd_seq_kinstr_ops *next) |
1da177e4 LT |
328 | { |
329 | memset(ops, 0, sizeof(*ops)); | |
330 | ops->private_data = private_data; | |
331 | ops->kops.private_data = ops; | |
19ac31e8 | 332 | ops->kops.add_len = sizeof(struct gf1_instrument); |
1da177e4 LT |
333 | ops->kops.instr_type = SNDRV_SEQ_INSTR_ID_GUS_PATCH; |
334 | ops->kops.put = snd_seq_gf1_put; | |
335 | ops->kops.get = snd_seq_gf1_get; | |
336 | ops->kops.get_size = snd_seq_gf1_get_size; | |
337 | ops->kops.remove = snd_seq_gf1_remove; | |
338 | ops->kops.notify = snd_seq_gf1_notify; | |
339 | ops->kops.next = next; | |
340 | return 0; | |
341 | } | |
342 | ||
343 | /* | |
344 | * Init part | |
345 | */ | |
346 | ||
347 | static int __init alsa_ainstr_gf1_init(void) | |
348 | { | |
349 | return 0; | |
350 | } | |
351 | ||
352 | static void __exit alsa_ainstr_gf1_exit(void) | |
353 | { | |
354 | } | |
355 | ||
356 | module_init(alsa_ainstr_gf1_init) | |
357 | module_exit(alsa_ainstr_gf1_exit) | |
358 | ||
359 | EXPORT_SYMBOL(snd_seq_gf1_init); |