ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / seq / instr / ainstr_simple.c
1 /*
2  *   Simple (MOD player) - 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_simple.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 Simple Instrument support.");
32 MODULE_LICENSE("GPL");
33 MODULE_CLASSES("{sound}");
34 MODULE_SUPPORTED_DEVICE("sound");
35
36 char *snd_seq_simple_id = SNDRV_SEQ_INSTR_ID_SIMPLE;
37
38 static unsigned int snd_seq_simple_size(unsigned int size, unsigned int format)
39 {
40         unsigned int result = size;
41         
42         if (format & SIMPLE_WAVE_16BIT)
43                 result <<= 1;
44         if (format & SIMPLE_WAVE_STEREO)
45                 result <<= 1;
46         return result;
47 }
48
49 static void snd_seq_simple_instr_free(snd_simple_ops_t *ops,
50                                       simple_instrument_t *ip,
51                                       int atomic)
52 {
53         if (ops->remove_sample)
54                 ops->remove_sample(ops->private_data, ip, atomic);
55 }
56
57 static int snd_seq_simple_put(void *private_data, snd_seq_kinstr_t *instr,
58                               char *instr_data, long len, int atomic, int cmd)
59 {
60         snd_simple_ops_t *ops = (snd_simple_ops_t *)private_data;
61         simple_instrument_t *ip;
62         simple_xinstrument_t ix;
63         int err, gfp_mask;
64         unsigned int real_size;
65
66         if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE)
67                 return -EINVAL;
68         gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
69         /* copy instrument data */
70         if (len < (long)sizeof(ix))
71                 return -EINVAL;
72         if (copy_from_user(&ix, instr_data, sizeof(ix)))
73                 return -EFAULT;
74         if (ix.stype != SIMPLE_STRU_INSTR)
75                 return -EINVAL;
76         instr_data += sizeof(ix);
77         len -= sizeof(ix);
78         ip = (simple_instrument_t *)KINSTR_DATA(instr);
79         ip->share_id[0] = le32_to_cpu(ix.share_id[0]);
80         ip->share_id[1] = le32_to_cpu(ix.share_id[1]);
81         ip->share_id[2] = le32_to_cpu(ix.share_id[2]);
82         ip->share_id[3] = le32_to_cpu(ix.share_id[3]);
83         ip->format = le32_to_cpu(ix.format);
84         ip->size = le32_to_cpu(ix.size);
85         ip->start = le32_to_cpu(ix.start);
86         ip->loop_start = le32_to_cpu(ix.loop_start);
87         ip->loop_end = le32_to_cpu(ix.loop_end);
88         ip->loop_repeat = le16_to_cpu(ix.loop_repeat);
89         ip->effect1 = ix.effect1;
90         ip->effect1_depth = ix.effect1_depth;
91         ip->effect2 = ix.effect2;
92         ip->effect2_depth = ix.effect2_depth;
93         real_size = snd_seq_simple_size(ip->size, ip->format);
94         if (len < (long)real_size)
95                 return -EINVAL;
96         if (ops->put_sample) {
97                 err = ops->put_sample(ops->private_data, ip,
98                                       instr_data, real_size, atomic);
99                 if (err < 0)
100                         return err;
101         }
102         return 0;
103 }
104
105 static int snd_seq_simple_get(void *private_data, snd_seq_kinstr_t *instr,
106                               char *instr_data, long len, int atomic, int cmd)
107 {
108         snd_simple_ops_t *ops = (snd_simple_ops_t *)private_data;
109         simple_instrument_t *ip;
110         simple_xinstrument_t ix;
111         int err;
112         unsigned int real_size;
113         
114         if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL)
115                 return -EINVAL;
116         if (len < (long)sizeof(ix))
117                 return -ENOMEM;
118         memset(&ix, 0, sizeof(ix));
119         ip = (simple_instrument_t *)KINSTR_DATA(instr);
120         ix.stype = SIMPLE_STRU_INSTR;
121         ix.share_id[0] = cpu_to_le32(ip->share_id[0]);
122         ix.share_id[1] = cpu_to_le32(ip->share_id[1]);
123         ix.share_id[2] = cpu_to_le32(ip->share_id[2]);
124         ix.share_id[3] = cpu_to_le32(ip->share_id[3]);
125         ix.format = cpu_to_le32(ip->format);
126         ix.size = cpu_to_le32(ip->size);
127         ix.start = cpu_to_le32(ip->start);
128         ix.loop_start = cpu_to_le32(ip->loop_start);
129         ix.loop_end = cpu_to_le32(ip->loop_end);
130         ix.loop_repeat = cpu_to_le32(ip->loop_repeat);
131         ix.effect1 = cpu_to_le16(ip->effect1);
132         ix.effect1_depth = cpu_to_le16(ip->effect1_depth);
133         ix.effect2 = ip->effect2;
134         ix.effect2_depth = ip->effect2_depth;
135         if (copy_to_user(instr_data, &ix, sizeof(ix)))
136                 return -EFAULT;
137         instr_data += sizeof(ix);
138         len -= sizeof(ix);
139         real_size = snd_seq_simple_size(ip->size, ip->format);
140         if (len < (long)real_size)
141                 return -ENOMEM;
142         if (ops->get_sample) {
143                 err = ops->get_sample(ops->private_data, ip,
144                                       instr_data, real_size, atomic);
145                 if (err < 0)
146                         return err;
147         }
148         return 0;
149 }
150
151 static int snd_seq_simple_get_size(void *private_data, snd_seq_kinstr_t *instr,
152                                    long *size)
153 {
154         simple_instrument_t *ip;
155
156         ip = (simple_instrument_t *)KINSTR_DATA(instr);
157         *size = sizeof(simple_xinstrument_t) + snd_seq_simple_size(ip->size, ip->format);
158         return 0;
159 }
160
161 static int snd_seq_simple_remove(void *private_data,
162                                  snd_seq_kinstr_t *instr,
163                                  int atomic)
164 {
165         snd_simple_ops_t *ops = (snd_simple_ops_t *)private_data;
166         simple_instrument_t *ip;
167
168         ip = (simple_instrument_t *)KINSTR_DATA(instr);
169         snd_seq_simple_instr_free(ops, ip, atomic);
170         return 0;
171 }
172
173 static void snd_seq_simple_notify(void *private_data,
174                                   snd_seq_kinstr_t *instr,
175                                   int what)
176 {
177         snd_simple_ops_t *ops = (snd_simple_ops_t *)private_data;
178
179         if (ops->notify)
180                 ops->notify(ops->private_data, instr, what);
181 }
182
183 int snd_seq_simple_init(snd_simple_ops_t *ops,
184                         void *private_data,
185                         snd_seq_kinstr_ops_t *next)
186 {
187         memset(ops, 0, sizeof(*ops));
188         ops->private_data = private_data;
189         ops->kops.private_data = ops;
190         ops->kops.add_len = sizeof(simple_instrument_t);
191         ops->kops.instr_type = snd_seq_simple_id;
192         ops->kops.put = snd_seq_simple_put;
193         ops->kops.get = snd_seq_simple_get;
194         ops->kops.get_size = snd_seq_simple_get_size;
195         ops->kops.remove = snd_seq_simple_remove;
196         ops->kops.notify = snd_seq_simple_notify;
197         ops->kops.next = next;
198         return 0;
199 }
200
201 /*
202  *  Init part
203  */
204
205 static int __init alsa_ainstr_simple_init(void)
206 {
207         return 0;
208 }
209
210 static void __exit alsa_ainstr_simple_exit(void)
211 {
212 }
213
214 module_init(alsa_ainstr_simple_init)
215 module_exit(alsa_ainstr_simple_exit)
216
217 EXPORT_SYMBOL(snd_seq_simple_id);
218 EXPORT_SYMBOL(snd_seq_simple_init);