xref: /openbmc/linux/sound/synth/emux/emux_hwdep.c (revision 4fc4dca8)
1 /*
2  *  Interface for hwdep device
3  *
4  *  Copyright (C) 2004 Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include <sound/core.h>
23 #include <sound/hwdep.h>
24 #include <linux/uaccess.h>
25 #include <linux/nospec.h>
26 #include "emux_voice.h"
27 
28 #define TMP_CLIENT_ID	0x1001
29 
30 /*
31  * load patch
32  */
33 static int
34 snd_emux_hwdep_load_patch(struct snd_emux *emu, void __user *arg)
35 {
36 	int err;
37 	struct soundfont_patch_info patch;
38 
39 	if (copy_from_user(&patch, arg, sizeof(patch)))
40 		return -EFAULT;
41 
42 	if (patch.key == GUS_PATCH)
43 		return snd_soundfont_load_guspatch(emu->sflist, arg,
44 						   patch.len + sizeof(patch),
45 						   TMP_CLIENT_ID);
46 
47 	if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
48 	    patch.type <= SNDRV_SFNT_PROBE_DATA) {
49 		err = snd_soundfont_load(emu->sflist, arg, patch.len + sizeof(patch), TMP_CLIENT_ID);
50 		if (err < 0)
51 			return err;
52 	} else {
53 		if (emu->ops.load_fx)
54 			return emu->ops.load_fx(emu, patch.type, patch.optarg, arg, patch.len + sizeof(patch));
55 		else
56 			return -EINVAL;
57 	}
58 	return 0;
59 }
60 
61 /*
62  * set misc mode
63  */
64 static int
65 snd_emux_hwdep_misc_mode(struct snd_emux *emu, void __user *arg)
66 {
67 	struct snd_emux_misc_mode info;
68 	int i;
69 
70 	if (copy_from_user(&info, arg, sizeof(info)))
71 		return -EFAULT;
72 	if (info.mode < 0 || info.mode >= EMUX_MD_END)
73 		return -EINVAL;
74 	info.mode = array_index_nospec(info.mode, EMUX_MD_END);
75 
76 	if (info.port < 0) {
77 		for (i = 0; i < emu->num_ports; i++)
78 			emu->portptrs[i]->ctrls[info.mode] = info.value;
79 	} else {
80 		if (info.port < emu->num_ports) {
81 			info.port = array_index_nospec(info.port, emu->num_ports);
82 			emu->portptrs[info.port]->ctrls[info.mode] = info.value;
83 		}
84 	}
85 	return 0;
86 }
87 
88 
89 /*
90  * ioctl
91  */
92 static int
93 snd_emux_hwdep_ioctl(struct snd_hwdep * hw, struct file *file,
94 		     unsigned int cmd, unsigned long arg)
95 {
96 	struct snd_emux *emu = hw->private_data;
97 
98 	switch (cmd) {
99 	case SNDRV_EMUX_IOCTL_VERSION:
100 		return put_user(SNDRV_EMUX_VERSION, (unsigned int __user *)arg);
101 	case SNDRV_EMUX_IOCTL_LOAD_PATCH:
102 		return snd_emux_hwdep_load_patch(emu, (void __user *)arg);
103 	case SNDRV_EMUX_IOCTL_RESET_SAMPLES:
104 		snd_soundfont_remove_samples(emu->sflist);
105 		break;
106 	case SNDRV_EMUX_IOCTL_REMOVE_LAST_SAMPLES:
107 		snd_soundfont_remove_unlocked(emu->sflist);
108 		break;
109 	case SNDRV_EMUX_IOCTL_MEM_AVAIL:
110 		if (emu->memhdr) {
111 			int size = snd_util_mem_avail(emu->memhdr);
112 			return put_user(size, (unsigned int __user *)arg);
113 		}
114 		break;
115 	case SNDRV_EMUX_IOCTL_MISC_MODE:
116 		return snd_emux_hwdep_misc_mode(emu, (void __user *)arg);
117 	}
118 
119 	return 0;
120 }
121 
122 
123 /*
124  * register hwdep device
125  */
126 
127 int
128 snd_emux_init_hwdep(struct snd_emux *emu)
129 {
130 	struct snd_hwdep *hw;
131 	int err;
132 
133 	if ((err = snd_hwdep_new(emu->card, SNDRV_EMUX_HWDEP_NAME, emu->hwdep_idx, &hw)) < 0)
134 		return err;
135 	emu->hwdep = hw;
136 	strcpy(hw->name, SNDRV_EMUX_HWDEP_NAME);
137 	hw->iface = SNDRV_HWDEP_IFACE_EMUX_WAVETABLE;
138 	hw->ops.ioctl = snd_emux_hwdep_ioctl;
139 	/* The ioctl parameter types are compatible between 32- and
140 	 * 64-bit architectures, so use the same function. */
141 	hw->ops.ioctl_compat = snd_emux_hwdep_ioctl;
142 	hw->exclusive = 1;
143 	hw->private_data = emu;
144 	if ((err = snd_card_register(emu->card)) < 0)
145 		return err;
146 
147 	return 0;
148 }
149 
150 
151 /*
152  * unregister
153  */
154 void
155 snd_emux_delete_hwdep(struct snd_emux *emu)
156 {
157 	if (emu->hwdep) {
158 		snd_device_free(emu->card, emu->hwdep);
159 		emu->hwdep = NULL;
160 	}
161 }
162