2 * IWFFFF - AMD InterWave (tm) - Instrument routines
3 * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
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.
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.
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
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_iw.h>
27 #include <sound/initval.h>
28 #include <asm/uaccess.h>
30 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
31 MODULE_DESCRIPTION("Advanced Linux Sound Architecture IWFFFF support.");
32 MODULE_LICENSE("GPL");
33 MODULE_CLASSES("{sound}");
34 MODULE_SUPPORTED_DEVICE("sound");
36 char *snd_seq_iwffff_id = SNDRV_SEQ_INSTR_ID_INTERWAVE;
38 static unsigned int snd_seq_iwffff_size(unsigned int size, unsigned int format)
40 unsigned int result = size;
42 if (format & IWFFFF_WAVE_16BIT)
44 if (format & IWFFFF_WAVE_STEREO)
49 static void snd_seq_iwffff_copy_lfo_from_stream(iwffff_lfo_t *fp,
52 fp->freq = le16_to_cpu(fx->freq);
53 fp->depth = le16_to_cpu(fx->depth);
54 fp->sweep = le16_to_cpu(fx->sweep);
55 fp->shape = fx->shape;
56 fp->delay = fx->delay;
59 static int snd_seq_iwffff_copy_env_from_stream(__u32 req_stype,
68 iwffff_env_record_t *rp, *rp_last;
69 iwffff_xenv_record_t rx;
70 iwffff_env_point_t *pp;
71 iwffff_xenv_point_t px;
74 ep->flags = ex->flags;
76 ep->index = ex->index;
79 if (*len < (long)sizeof(__u32))
81 if (copy_from_user(&stype, *data, sizeof(stype)))
83 if (stype == IWFFFF_STRU_WAVE)
85 if (req_stype != stype) {
86 if (stype == IWFFFF_STRU_ENV_RECP ||
87 stype == IWFFFF_STRU_ENV_RECV)
90 if (*len < (long)sizeof(rx))
92 if (copy_from_user(&rx, *data, sizeof(rx)))
96 points_size = (le16_to_cpu(rx.nattack) + le16_to_cpu(rx.nrelease)) * 2 * sizeof(__u16);
97 if (points_size > *len)
99 rp = (iwffff_env_record_t *)snd_kcalloc(sizeof(*rp) + points_size, gfp_mask);
102 rp->nattack = le16_to_cpu(rx.nattack);
103 rp->nrelease = le16_to_cpu(rx.nrelease);
104 rp->sustain_offset = le16_to_cpu(rx.sustain_offset);
105 rp->sustain_rate = le16_to_cpu(rx.sustain_rate);
106 rp->release_rate = le16_to_cpu(rx.release_rate);
107 rp->hirange = rx.hirange;
108 pp = (iwffff_env_point_t *)(rp + 1);
109 for (idx = 0; idx < rp->nattack + rp->nrelease; idx++) {
110 if (copy_from_user(&px, *data, sizeof(px)))
114 pp->offset = le16_to_cpu(px.offset);
115 pp->rate = le16_to_cpu(px.rate);
117 if (ep->record == NULL) {
127 static int snd_seq_iwffff_copy_wave_from_stream(snd_iwffff_ops_t *ops,
133 iwffff_wave_t *wp, *prev;
136 unsigned int real_size;
138 gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
139 if (*len < (long)sizeof(xp))
141 if (copy_from_user(&xp, *data, sizeof(xp)))
145 wp = (iwffff_wave_t *)snd_kcalloc(sizeof(*wp), gfp_mask);
148 wp->share_id[0] = le32_to_cpu(xp.share_id[0]);
149 wp->share_id[1] = le32_to_cpu(xp.share_id[1]);
150 wp->share_id[2] = le32_to_cpu(xp.share_id[2]);
151 wp->share_id[3] = le32_to_cpu(xp.share_id[3]);
152 wp->format = le32_to_cpu(xp.format);
153 wp->address.memory = le32_to_cpu(xp.offset);
154 wp->size = le32_to_cpu(xp.size);
155 wp->start = le32_to_cpu(xp.start);
156 wp->loop_start = le32_to_cpu(xp.loop_start);
157 wp->loop_end = le32_to_cpu(xp.loop_end);
158 wp->loop_repeat = le16_to_cpu(xp.loop_repeat);
159 wp->sample_ratio = le32_to_cpu(xp.sample_ratio);
160 wp->attenuation = xp.attenuation;
161 wp->low_note = xp.low_note;
162 wp->high_note = xp.high_note;
163 real_size = snd_seq_iwffff_size(wp->size, wp->format);
164 if (!(wp->format & IWFFFF_WAVE_ROM)) {
165 if ((long)real_size > *len) {
170 if (ops->put_sample) {
171 err = ops->put_sample(ops->private_data, wp,
172 *data, real_size, atomic);
178 if (!(wp->format & IWFFFF_WAVE_ROM)) {
184 while (prev->next) prev = prev->next;
192 static void snd_seq_iwffff_env_free(snd_iwffff_ops_t *ops,
196 iwffff_env_record_t *rec;
198 while ((rec = env->record) != NULL) {
199 env->record = rec->next;
204 static void snd_seq_iwffff_wave_free(snd_iwffff_ops_t *ops,
208 if (ops->remove_sample)
209 ops->remove_sample(ops->private_data, wave, atomic);
213 static void snd_seq_iwffff_instr_free(snd_iwffff_ops_t *ops,
214 iwffff_instrument_t *ip,
217 iwffff_layer_t *layer;
220 while ((layer = ip->layer) != NULL) {
221 ip->layer = layer->next;
222 snd_seq_iwffff_env_free(ops, &layer->penv, atomic);
223 snd_seq_iwffff_env_free(ops, &layer->venv, atomic);
224 while ((wave = layer->wave) != NULL) {
225 layer->wave = wave->next;
226 snd_seq_iwffff_wave_free(ops, wave, atomic);
232 static int snd_seq_iwffff_put(void *private_data, snd_seq_kinstr_t *instr,
233 char *instr_data, long len, int atomic, int cmd)
235 snd_iwffff_ops_t *ops = (snd_iwffff_ops_t *)private_data;
236 iwffff_instrument_t *ip;
237 iwffff_xinstrument_t ix;
238 iwffff_layer_t *lp, *prev_lp;
242 if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE)
244 gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
245 /* copy instrument data */
246 if (len < (long)sizeof(ix))
248 if (copy_from_user(&ix, instr_data, sizeof(ix)))
250 if (ix.stype != IWFFFF_STRU_INSTR)
252 instr_data += sizeof(ix);
254 ip = (iwffff_instrument_t *)KINSTR_DATA(instr);
255 ip->exclusion = le16_to_cpu(ix.exclusion);
256 ip->layer_type = le16_to_cpu(ix.layer_type);
257 ip->exclusion_group = le16_to_cpu(ix.exclusion_group);
258 ip->effect1 = ix.effect1;
259 ip->effect1_depth = ix.effect1_depth;
260 ip->effect2 = ix.effect2;
261 ip->effect2_depth = ix.effect2_depth;
265 if (len < (long)sizeof(iwffff_xlayer_t)) {
266 snd_seq_iwffff_instr_free(ops, ip, atomic);
269 if (copy_from_user(&lx, instr_data, sizeof(lx)))
271 instr_data += sizeof(lx);
273 if (lx.stype != IWFFFF_STRU_LAYER) {
274 snd_seq_iwffff_instr_free(ops, ip, atomic);
277 lp = (iwffff_layer_t *)snd_kcalloc(sizeof(*lp), gfp_mask);
279 snd_seq_iwffff_instr_free(ops, ip, atomic);
288 lp->flags = lx.flags;
289 lp->velocity_mode = lx.velocity_mode;
290 lp->layer_event = lx.layer_event;
291 lp->low_range = lx.low_range;
292 lp->high_range = lx.high_range;
294 lp->pan_freq_scale = lx.pan_freq_scale;
295 lp->attenuation = lx.attenuation;
296 snd_seq_iwffff_copy_lfo_from_stream(&lp->tremolo, &lx.tremolo);
297 snd_seq_iwffff_copy_lfo_from_stream(&lp->vibrato, &lx.vibrato);
298 lp->freq_scale = le16_to_cpu(lx.freq_scale);
299 lp->freq_center = lx.freq_center;
300 err = snd_seq_iwffff_copy_env_from_stream(IWFFFF_STRU_ENV_RECP,
306 snd_seq_iwffff_instr_free(ops, ip, atomic);
309 err = snd_seq_iwffff_copy_env_from_stream(IWFFFF_STRU_ENV_RECV,
315 snd_seq_iwffff_instr_free(ops, ip, atomic);
318 while (len > (long)sizeof(__u32)) {
321 if (copy_from_user(&stype, instr_data, sizeof(stype)))
323 if (stype != IWFFFF_STRU_WAVE)
325 err = snd_seq_iwffff_copy_wave_from_stream(ops,
331 snd_seq_iwffff_instr_free(ops, ip, atomic);
339 static void snd_seq_iwffff_copy_lfo_to_stream(iwffff_xlfo_t *fx,
342 fx->freq = cpu_to_le16(fp->freq);
343 fx->depth = cpu_to_le16(fp->depth);
344 fx->sweep = cpu_to_le16(fp->sweep);
345 fp->shape = fx->shape;
346 fp->delay = fx->delay;
349 static int snd_seq_iwffff_copy_env_to_stream(__u32 req_stype,
356 iwffff_env_record_t *rp;
357 iwffff_xenv_record_t rx;
358 iwffff_env_point_t *pp;
359 iwffff_xenv_point_t px;
360 int points_size, idx;
362 ex->flags = ep->flags;
364 ex->index = ep->index;
365 for (rp = ep->record; rp; rp = rp->next) {
366 if (*len < (long)sizeof(rx))
368 memset(&rx, 0, sizeof(rx));
369 rx.stype = req_stype;
370 rx.nattack = cpu_to_le16(rp->nattack);
371 rx.nrelease = cpu_to_le16(rp->nrelease);
372 rx.sustain_offset = cpu_to_le16(rp->sustain_offset);
373 rx.sustain_rate = cpu_to_le16(rp->sustain_rate);
374 rx.release_rate = cpu_to_le16(rp->release_rate);
375 rx.hirange = cpu_to_le16(rp->hirange);
376 if (copy_to_user(*data, &rx, sizeof(rx)))
380 points_size = (rp->nattack + rp->nrelease) * 2 * sizeof(__u16);
381 if (*len < points_size)
383 pp = (iwffff_env_point_t *)(rp + 1);
384 for (idx = 0; idx < rp->nattack + rp->nrelease; idx++) {
385 px.offset = cpu_to_le16(pp->offset);
386 px.rate = cpu_to_le16(pp->rate);
387 if (copy_to_user(*data, &px, sizeof(px)))
396 static int snd_seq_iwffff_copy_wave_to_stream(snd_iwffff_ops_t *ops,
405 unsigned int real_size;
407 for (wp = lp->wave; wp; wp = wp->next) {
408 if (*len < (long)sizeof(xp))
410 memset(&xp, 0, sizeof(xp));
411 xp.stype = IWFFFF_STRU_WAVE;
412 xp.share_id[0] = cpu_to_le32(wp->share_id[0]);
413 xp.share_id[1] = cpu_to_le32(wp->share_id[1]);
414 xp.share_id[2] = cpu_to_le32(wp->share_id[2]);
415 xp.share_id[3] = cpu_to_le32(wp->share_id[3]);
416 xp.format = cpu_to_le32(wp->format);
417 if (wp->format & IWFFFF_WAVE_ROM)
418 xp.offset = cpu_to_le32(wp->address.memory);
419 xp.size = cpu_to_le32(wp->size);
420 xp.start = cpu_to_le32(wp->start);
421 xp.loop_start = cpu_to_le32(wp->loop_start);
422 xp.loop_end = cpu_to_le32(wp->loop_end);
423 xp.loop_repeat = cpu_to_le32(wp->loop_repeat);
424 xp.sample_ratio = cpu_to_le32(wp->sample_ratio);
425 xp.attenuation = wp->attenuation;
426 xp.low_note = wp->low_note;
427 xp.high_note = wp->high_note;
428 if (copy_to_user(*data, &xp, sizeof(xp)))
432 real_size = snd_seq_iwffff_size(wp->size, wp->format);
433 if (!(wp->format & IWFFFF_WAVE_ROM)) {
434 if (*len < (long)real_size)
437 if (ops->get_sample) {
438 err = ops->get_sample(ops->private_data, wp,
439 *data, real_size, atomic);
443 if (!(wp->format & IWFFFF_WAVE_ROM)) {
451 static int snd_seq_iwffff_get(void *private_data, snd_seq_kinstr_t *instr,
452 char *instr_data, long len, int atomic, int cmd)
454 snd_iwffff_ops_t *ops = (snd_iwffff_ops_t *)private_data;
455 iwffff_instrument_t *ip;
456 iwffff_xinstrument_t ix;
459 char *layer_instr_data;
462 if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL)
464 if (len < (long)sizeof(ix))
466 memset(&ix, 0, sizeof(ix));
467 ip = (iwffff_instrument_t *)KINSTR_DATA(instr);
468 ix.stype = IWFFFF_STRU_INSTR;
469 ix.exclusion = cpu_to_le16(ip->exclusion);
470 ix.layer_type = cpu_to_le16(ip->layer_type);
471 ix.exclusion_group = cpu_to_le16(ip->exclusion_group);
472 ix.effect1 = cpu_to_le16(ip->effect1);
473 ix.effect1_depth = cpu_to_le16(ip->effect1_depth);
474 ix.effect2 = ip->effect2;
475 ix.effect2_depth = ip->effect2_depth;
476 if (copy_to_user(instr_data, &ix, sizeof(ix)))
478 instr_data += sizeof(ix);
480 for (lp = ip->layer; lp; lp = lp->next) {
481 if (len < (long)sizeof(lx))
483 memset(&lx, 0, sizeof(lx));
484 lx.stype = IWFFFF_STRU_LAYER;
485 lx.flags = lp->flags;
486 lx.velocity_mode = lp->velocity_mode;
487 lx.layer_event = lp->layer_event;
488 lx.low_range = lp->low_range;
489 lx.high_range = lp->high_range;
491 lx.pan_freq_scale = lp->pan_freq_scale;
492 lx.attenuation = lp->attenuation;
493 snd_seq_iwffff_copy_lfo_to_stream(&lx.tremolo, &lp->tremolo);
494 snd_seq_iwffff_copy_lfo_to_stream(&lx.vibrato, &lp->vibrato);
495 layer_instr_data = instr_data;
496 instr_data += sizeof(lx);
498 err = snd_seq_iwffff_copy_env_to_stream(IWFFFF_STRU_ENV_RECP,
504 err = snd_seq_iwffff_copy_env_to_stream(IWFFFF_STRU_ENV_RECV,
510 /* layer structure updating is now finished */
511 if (copy_to_user(layer_instr_data, &lx, sizeof(lx)))
513 err = snd_seq_iwffff_copy_wave_to_stream(ops,
524 static long snd_seq_iwffff_env_size_in_stream(iwffff_env_t *ep)
527 iwffff_env_record_t *rp;
529 for (rp = ep->record; rp; rp = rp->next) {
530 result += sizeof(iwffff_xenv_record_t);
531 result += (rp->nattack + rp->nrelease) * 2 * sizeof(__u16);
536 static long snd_seq_iwffff_wave_size_in_stream(iwffff_layer_t *lp)
541 for (wp = lp->wave; wp; wp = wp->next) {
542 result += sizeof(iwffff_xwave_t);
543 if (!(wp->format & IWFFFF_WAVE_ROM))
549 static int snd_seq_iwffff_get_size(void *private_data, snd_seq_kinstr_t *instr,
553 iwffff_instrument_t *ip;
557 ip = (iwffff_instrument_t *)KINSTR_DATA(instr);
558 result = sizeof(iwffff_xinstrument_t);
559 for (lp = ip->layer; lp; lp = lp->next) {
560 result += sizeof(iwffff_xlayer_t);
561 result += snd_seq_iwffff_env_size_in_stream(&lp->penv);
562 result += snd_seq_iwffff_env_size_in_stream(&lp->venv);
563 result += snd_seq_iwffff_wave_size_in_stream(lp);
569 static int snd_seq_iwffff_remove(void *private_data,
570 snd_seq_kinstr_t *instr,
573 snd_iwffff_ops_t *ops = (snd_iwffff_ops_t *)private_data;
574 iwffff_instrument_t *ip;
576 ip = (iwffff_instrument_t *)KINSTR_DATA(instr);
577 snd_seq_iwffff_instr_free(ops, ip, atomic);
581 static void snd_seq_iwffff_notify(void *private_data,
582 snd_seq_kinstr_t *instr,
585 snd_iwffff_ops_t *ops = (snd_iwffff_ops_t *)private_data;
588 ops->notify(ops->private_data, instr, what);
591 int snd_seq_iwffff_init(snd_iwffff_ops_t *ops,
593 snd_seq_kinstr_ops_t *next)
595 memset(ops, 0, sizeof(*ops));
596 ops->private_data = private_data;
597 ops->kops.private_data = ops;
598 ops->kops.add_len = sizeof(iwffff_instrument_t);
599 ops->kops.instr_type = snd_seq_iwffff_id;
600 ops->kops.put = snd_seq_iwffff_put;
601 ops->kops.get = snd_seq_iwffff_get;
602 ops->kops.get_size = snd_seq_iwffff_get_size;
603 ops->kops.remove = snd_seq_iwffff_remove;
604 ops->kops.notify = snd_seq_iwffff_notify;
605 ops->kops.next = next;
613 static int __init alsa_ainstr_iw_init(void)
618 static void __exit alsa_ainstr_iw_exit(void)
622 module_init(alsa_ainstr_iw_init)
623 module_exit(alsa_ainstr_iw_exit)
625 EXPORT_SYMBOL(snd_seq_iwffff_id);
626 EXPORT_SYMBOL(snd_seq_iwffff_init);