vserver 1.9.3
[linux-2.6.git] / sound / core / seq / instr / ainstr_gf1.c
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>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/ainstr_gf1.h>
27 #include <sound/initval.h>
28 #include <asm/uaccess.h>
29
30 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
31 MODULE_DESCRIPTION("Advanced Linux Sound Architecture GF1 (GUS) Patch support.");
32 MODULE_LICENSE("GPL");
33
34 char *snd_seq_gf1_id = SNDRV_SEQ_INSTR_ID_GUS_PATCH;
35
36 static unsigned int snd_seq_gf1_size(unsigned int size, unsigned int format)
37 {
38         unsigned int result = size;
39         
40         if (format & GF1_WAVE_16BIT)
41                 result <<= 1;
42         if (format & GF1_WAVE_STEREO)
43                 result <<= 1;
44         return format;
45 }
46
47 static int snd_seq_gf1_copy_wave_from_stream(snd_gf1_ops_t *ops,
48                                              gf1_instrument_t *ip,
49                                              char __user **data,
50                                              long *len,
51                                              int atomic)
52 {
53         gf1_wave_t *wp, *prev;
54         gf1_xwave_t xp;
55         int err, gfp_mask;
56         unsigned int real_size;
57         
58         gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
59         if (*len < (long)sizeof(xp))
60                 return -EINVAL;
61         if (copy_from_user(&xp, *data, sizeof(xp)))
62                 return -EFAULT;
63         *data += sizeof(xp);
64         *len -= sizeof(xp);
65         wp = kcalloc(1, sizeof(*wp), gfp_mask);
66         if (wp == NULL)
67                 return -ENOMEM;
68         wp->share_id[0] = le32_to_cpu(xp.share_id[0]);
69         wp->share_id[1] = le32_to_cpu(xp.share_id[1]);
70         wp->share_id[2] = le32_to_cpu(xp.share_id[2]);
71         wp->share_id[3] = le32_to_cpu(xp.share_id[3]);
72         wp->format = le32_to_cpu(xp.format);
73         wp->size = le32_to_cpu(xp.size);
74         wp->start = le32_to_cpu(xp.start);
75         wp->loop_start = le32_to_cpu(xp.loop_start);
76         wp->loop_end = le32_to_cpu(xp.loop_end);
77         wp->loop_repeat = le16_to_cpu(xp.loop_repeat);
78         wp->flags = xp.flags;
79         wp->sample_rate = le32_to_cpu(xp.sample_rate);
80         wp->low_frequency = le32_to_cpu(xp.low_frequency);
81         wp->high_frequency = le32_to_cpu(xp.high_frequency);
82         wp->root_frequency = le32_to_cpu(xp.root_frequency);
83         wp->tune = le16_to_cpu(xp.tune);
84         wp->balance = xp.balance;
85         memcpy(wp->envelope_rate, xp.envelope_rate, 6);
86         memcpy(wp->envelope_offset, xp.envelope_offset, 6);
87         wp->tremolo_sweep = xp.tremolo_sweep;
88         wp->tremolo_rate = xp.tremolo_rate;
89         wp->tremolo_depth = xp.tremolo_depth;
90         wp->vibrato_sweep = xp.vibrato_sweep;
91         wp->vibrato_rate = xp.vibrato_rate;
92         wp->vibrato_depth = xp.vibrato_depth;
93         wp->scale_frequency = le16_to_cpu(xp.scale_frequency);
94         wp->scale_factor = le16_to_cpu(xp.scale_factor);
95         real_size = snd_seq_gf1_size(wp->size, wp->format);
96         if ((long)real_size > *len) {
97                 kfree(wp);
98                 return -ENOMEM;
99         }
100         if (ops->put_sample) {
101                 err = ops->put_sample(ops->private_data, wp,
102                                       *data, real_size, atomic);
103                 if (err < 0) {
104                         kfree(wp);
105                         return err;
106                 }
107         }
108         *data += real_size;
109         *len -= real_size;
110         prev = ip->wave;
111         if (prev) {
112                 while (prev->next) prev = prev->next;
113                 prev->next = wp;
114         } else {
115                 ip->wave = wp;
116         }
117         return 0;
118 }
119
120 static void snd_seq_gf1_wave_free(snd_gf1_ops_t *ops,
121                                   gf1_wave_t *wave,
122                                   int atomic)
123 {
124         if (ops->remove_sample)
125                 ops->remove_sample(ops->private_data, wave, atomic);
126         kfree(wave);
127 }
128
129 static void snd_seq_gf1_instr_free(snd_gf1_ops_t *ops,
130                                    gf1_instrument_t *ip,
131                                    int atomic)
132 {
133         gf1_wave_t *wave;
134         
135         while ((wave = ip->wave) != NULL) {
136                 ip->wave = wave->next;
137                 snd_seq_gf1_wave_free(ops, wave, atomic);
138         }
139 }
140
141 static int snd_seq_gf1_put(void *private_data, snd_seq_kinstr_t *instr,
142                            char __user *instr_data, long len, int atomic,
143                            int cmd)
144 {
145         snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
146         gf1_instrument_t *ip;
147         gf1_xinstrument_t ix;
148         int err, gfp_mask;
149
150         if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE)
151                 return -EINVAL;
152         gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
153         /* copy instrument data */
154         if (len < (long)sizeof(ix))
155                 return -EINVAL;
156         if (copy_from_user(&ix, instr_data, sizeof(ix)))
157                 return -EFAULT;
158         if (ix.stype != GF1_STRU_INSTR)
159                 return -EINVAL;
160         instr_data += sizeof(ix);
161         len -= sizeof(ix);
162         ip = (gf1_instrument_t *)KINSTR_DATA(instr);
163         ip->exclusion = le16_to_cpu(ix.exclusion);
164         ip->exclusion_group = le16_to_cpu(ix.exclusion_group);
165         ip->effect1 = ix.effect1;
166         ip->effect1_depth = ix.effect1_depth;
167         ip->effect2 = ix.effect2;
168         ip->effect2_depth = ix.effect2_depth;
169         /* copy layers */
170         while (len > (long)sizeof(__u32)) {
171                 __u32 stype;
172
173                 if (copy_from_user(&stype, instr_data, sizeof(stype)))
174                         return -EFAULT;
175                 if (stype != GF1_STRU_WAVE) {
176                         snd_seq_gf1_instr_free(ops, ip, atomic);
177                         return -EINVAL;
178                 }
179                 err = snd_seq_gf1_copy_wave_from_stream(ops,
180                                                         ip,
181                                                         &instr_data,
182                                                         &len,
183                                                         atomic);
184                 if (err < 0) {
185                         snd_seq_gf1_instr_free(ops, ip, atomic);
186                         return err;
187                 }
188         }
189         return 0;
190 }
191
192 static int snd_seq_gf1_copy_wave_to_stream(snd_gf1_ops_t *ops,
193                                            gf1_instrument_t *ip,
194                                            char __user **data,
195                                            long *len,
196                                            int atomic)
197 {
198         gf1_wave_t *wp;
199         gf1_xwave_t xp;
200         int err;
201         unsigned int real_size;
202         
203         for (wp = ip->wave; wp; wp = wp->next) {
204                 if (*len < (long)sizeof(xp))
205                         return -ENOMEM;
206                 memset(&xp, 0, sizeof(xp));
207                 xp.stype = GF1_STRU_WAVE;
208                 xp.share_id[0] = cpu_to_le32(wp->share_id[0]);
209                 xp.share_id[1] = cpu_to_le32(wp->share_id[1]);
210                 xp.share_id[2] = cpu_to_le32(wp->share_id[2]);
211                 xp.share_id[3] = cpu_to_le32(wp->share_id[3]);
212                 xp.format = cpu_to_le32(wp->format);
213                 xp.size = cpu_to_le32(wp->size);
214                 xp.start = cpu_to_le32(wp->start);
215                 xp.loop_start = cpu_to_le32(wp->loop_start);
216                 xp.loop_end = cpu_to_le32(wp->loop_end);
217                 xp.loop_repeat = cpu_to_le32(wp->loop_repeat);
218                 xp.flags = wp->flags;
219                 xp.sample_rate = cpu_to_le32(wp->sample_rate);
220                 xp.low_frequency = cpu_to_le32(wp->low_frequency);
221                 xp.high_frequency = cpu_to_le32(wp->high_frequency);
222                 xp.root_frequency = cpu_to_le32(wp->root_frequency);
223                 xp.tune = cpu_to_le16(wp->tune);
224                 xp.balance = wp->balance;
225                 memcpy(xp.envelope_rate, wp->envelope_rate, 6);
226                 memcpy(xp.envelope_offset, wp->envelope_offset, 6);
227                 xp.tremolo_sweep = wp->tremolo_sweep;
228                 xp.tremolo_rate = wp->tremolo_rate;
229                 xp.tremolo_depth = wp->tremolo_depth;
230                 xp.vibrato_sweep = wp->vibrato_sweep;
231                 xp.vibrato_rate = wp->vibrato_rate;
232                 xp.vibrato_depth = wp->vibrato_depth;
233                 xp.scale_frequency = cpu_to_le16(wp->scale_frequency);
234                 xp.scale_factor = cpu_to_le16(wp->scale_factor);
235                 if (copy_to_user(*data, &xp, sizeof(xp)))
236                         return -EFAULT;
237                 *data += sizeof(xp);
238                 *len -= sizeof(xp);
239                 real_size = snd_seq_gf1_size(wp->size, wp->format);
240                 if (*len < (long)real_size)
241                         return -ENOMEM;
242                 if (ops->get_sample) {
243                         err = ops->get_sample(ops->private_data, wp,
244                                               *data, real_size, atomic);
245                         if (err < 0)
246                                 return err;
247                 }
248                 *data += wp->size;
249                 *len -= wp->size;
250         }
251         return 0;
252 }
253
254 static int snd_seq_gf1_get(void *private_data, snd_seq_kinstr_t *instr,
255                            char __user *instr_data, long len, int atomic,
256                            int cmd)
257 {
258         snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
259         gf1_instrument_t *ip;
260         gf1_xinstrument_t ix;
261         
262         if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL)
263                 return -EINVAL;
264         if (len < (long)sizeof(ix))
265                 return -ENOMEM;
266         memset(&ix, 0, sizeof(ix));
267         ip = (gf1_instrument_t *)KINSTR_DATA(instr);
268         ix.stype = GF1_STRU_INSTR;
269         ix.exclusion = cpu_to_le16(ip->exclusion);
270         ix.exclusion_group = cpu_to_le16(ip->exclusion_group);
271         ix.effect1 = cpu_to_le16(ip->effect1);
272         ix.effect1_depth = cpu_to_le16(ip->effect1_depth);
273         ix.effect2 = ip->effect2;
274         ix.effect2_depth = ip->effect2_depth;
275         if (copy_to_user(instr_data, &ix, sizeof(ix)))
276                 return -EFAULT;
277         instr_data += sizeof(ix);
278         len -= sizeof(ix);
279         return snd_seq_gf1_copy_wave_to_stream(ops,
280                                                ip,
281                                                &instr_data,
282                                                &len,
283                                                atomic);
284 }
285
286 static int snd_seq_gf1_get_size(void *private_data, snd_seq_kinstr_t *instr,
287                                 long *size)
288 {
289         long result;
290         gf1_instrument_t *ip;
291         gf1_wave_t *wp;
292
293         *size = 0;
294         ip = (gf1_instrument_t *)KINSTR_DATA(instr);
295         result = sizeof(gf1_xinstrument_t);
296         for (wp = ip->wave; wp; wp = wp->next) {
297                 result += sizeof(gf1_xwave_t);
298                 result += wp->size;
299         }
300         *size = result;
301         return 0;
302 }
303
304 static int snd_seq_gf1_remove(void *private_data,
305                               snd_seq_kinstr_t *instr,
306                               int atomic)
307 {
308         snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
309         gf1_instrument_t *ip;
310
311         ip = (gf1_instrument_t *)KINSTR_DATA(instr);
312         snd_seq_gf1_instr_free(ops, ip, atomic);
313         return 0;
314 }
315
316 static void snd_seq_gf1_notify(void *private_data,
317                                snd_seq_kinstr_t *instr,
318                                int what)
319 {
320         snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
321
322         if (ops->notify)
323                 ops->notify(ops->private_data, instr, what);
324 }
325
326 int snd_seq_gf1_init(snd_gf1_ops_t *ops,
327                      void *private_data,
328                      snd_seq_kinstr_ops_t *next)
329 {
330         memset(ops, 0, sizeof(*ops));
331         ops->private_data = private_data;
332         ops->kops.private_data = ops;
333         ops->kops.add_len = sizeof(gf1_instrument_t);
334         ops->kops.instr_type = snd_seq_gf1_id;
335         ops->kops.put = snd_seq_gf1_put;
336         ops->kops.get = snd_seq_gf1_get;
337         ops->kops.get_size = snd_seq_gf1_get_size;
338         ops->kops.remove = snd_seq_gf1_remove;
339         ops->kops.notify = snd_seq_gf1_notify;
340         ops->kops.next = next;
341         return 0;
342 }
343
344 /*
345  *  Init part
346  */
347
348 static int __init alsa_ainstr_gf1_init(void)
349 {
350         return 0;
351 }
352
353 static void __exit alsa_ainstr_gf1_exit(void)
354 {
355 }
356
357 module_init(alsa_ainstr_gf1_init)
358 module_exit(alsa_ainstr_gf1_exit)
359
360 EXPORT_SYMBOL(snd_seq_gf1_id);
361 EXPORT_SYMBOL(snd_seq_gf1_init);