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