xref: /linux/sound/drivers/vx/vx_hwdep.c (revision 0a1340c185734a57fbf4775927966ad4a1347b02)
1 /*
2  * Driver for Digigram VX soundcards
3  *
4  * DSP firmware management
5  *
6  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 
23 #include <sound/driver.h>
24 #include <linux/device.h>
25 #include <linux/firmware.h>
26 #include <linux/vmalloc.h>
27 #include <sound/core.h>
28 #include <sound/hwdep.h>
29 #include <sound/vx_core.h>
30 
31 #ifdef SND_VX_FW_LOADER
32 
33 int snd_vx_setup_firmware(struct vx_core *chip)
34 {
35 	static char *fw_files[VX_TYPE_NUMS][4] = {
36 		[VX_TYPE_BOARD] = {
37 			NULL, "x1_1_vx2.xlx", "bd56002.boot", "l_1_vx2.d56",
38 		},
39 		[VX_TYPE_V2] = {
40 			NULL, "x1_2_v22.xlx", "bd563v2.boot", "l_1_v22.d56",
41 		},
42 		[VX_TYPE_MIC] = {
43 			NULL, "x1_2_v22.xlx", "bd563v2.boot", "l_1_v22.d56",
44 		},
45 		[VX_TYPE_VXPOCKET] = {
46 			"bx_1_vxp.b56", "x1_1_vxp.xlx", "bd563s3.boot", "l_1_vxp.d56"
47 		},
48 		[VX_TYPE_VXP440] = {
49 			"bx_1_vp4.b56", "x1_1_vp4.xlx", "bd563s3.boot", "l_1_vp4.d56"
50 		},
51 	};
52 
53 	int i, err;
54 
55 	for (i = 0; i < 4; i++) {
56 		char path[32];
57 		const struct firmware *fw;
58 		if (! fw_files[chip->type][i])
59 			continue;
60 		sprintf(path, "vx/%s", fw_files[chip->type][i]);
61 		if (request_firmware(&fw, path, chip->dev)) {
62 			snd_printk(KERN_ERR "vx: can't load firmware %s\n", path);
63 			return -ENOENT;
64 		}
65 		err = chip->ops->load_dsp(chip, i, fw);
66 		if (err < 0) {
67 			release_firmware(fw);
68 			return err;
69 		}
70 		if (i == 1)
71 			chip->chip_status |= VX_STAT_XILINX_LOADED;
72 #ifdef CONFIG_PM
73 		chip->firmware[i] = fw;
74 #else
75 		release_firmware(fw);
76 #endif
77 	}
78 
79 	/* ok, we reached to the last one */
80 	/* create the devices if not built yet */
81 	if ((err = snd_vx_pcm_new(chip)) < 0)
82 		return err;
83 
84 	if ((err = snd_vx_mixer_new(chip)) < 0)
85 		return err;
86 
87 	if (chip->ops->add_controls)
88 		if ((err = chip->ops->add_controls(chip)) < 0)
89 			return err;
90 
91 	chip->chip_status |= VX_STAT_DEVICE_INIT;
92 	chip->chip_status |= VX_STAT_CHIP_INIT;
93 
94 	return snd_card_register(chip->card);
95 }
96 
97 /* exported */
98 void snd_vx_free_firmware(struct vx_core *chip)
99 {
100 #ifdef CONFIG_PM
101 	int i;
102 	for (i = 0; i < 4; i++)
103 		release_firmware(chip->firmware[i]);
104 #endif
105 }
106 
107 #else /* old style firmware loading */
108 
109 static int vx_hwdep_open(struct snd_hwdep *hw, struct file *file)
110 {
111 	return 0;
112 }
113 
114 static int vx_hwdep_release(struct snd_hwdep *hw, struct file *file)
115 {
116 	return 0;
117 }
118 
119 static int vx_hwdep_dsp_status(struct snd_hwdep *hw,
120 			       struct snd_hwdep_dsp_status *info)
121 {
122 	static char *type_ids[VX_TYPE_NUMS] = {
123 		[VX_TYPE_BOARD] = "vxboard",
124 		[VX_TYPE_V2] = "vx222",
125 		[VX_TYPE_MIC] = "vx222",
126 		[VX_TYPE_VXPOCKET] = "vxpocket",
127 		[VX_TYPE_VXP440] = "vxp440",
128 	};
129 	struct vx_core *vx = hw->private_data;
130 
131 	snd_assert(type_ids[vx->type], return -EINVAL);
132 	strcpy(info->id, type_ids[vx->type]);
133 	if (vx_is_pcmcia(vx))
134 		info->num_dsps = 4;
135 	else
136 		info->num_dsps = 3;
137 	if (vx->chip_status & VX_STAT_CHIP_INIT)
138 		info->chip_ready = 1;
139 	info->version = VX_DRIVER_VERSION;
140 	return 0;
141 }
142 
143 static void free_fw(const struct firmware *fw)
144 {
145 	if (fw) {
146 		vfree(fw->data);
147 		kfree(fw);
148 	}
149 }
150 
151 static int vx_hwdep_dsp_load(struct snd_hwdep *hw,
152 			     struct snd_hwdep_dsp_image *dsp)
153 {
154 	struct vx_core *vx = hw->private_data;
155 	int index, err;
156 	struct firmware *fw;
157 
158 	snd_assert(vx->ops->load_dsp, return -ENXIO);
159 
160 	fw = kmalloc(sizeof(*fw), GFP_KERNEL);
161 	if (! fw) {
162 		snd_printk(KERN_ERR "cannot allocate firmware\n");
163 		return -ENOMEM;
164 	}
165 	fw->size = dsp->length;
166 	fw->data = vmalloc(fw->size);
167 	if (! fw->data) {
168 		snd_printk(KERN_ERR "cannot allocate firmware image (length=%d)\n",
169 			   (int)fw->size);
170 		kfree(fw);
171 		return -ENOMEM;
172 	}
173 	if (copy_from_user(fw->data, dsp->image, dsp->length)) {
174 		free_fw(fw);
175 		return -EFAULT;
176 	}
177 
178 	index = dsp->index;
179 	if (! vx_is_pcmcia(vx))
180 		index++;
181 	err = vx->ops->load_dsp(vx, index, fw);
182 	if (err < 0) {
183 		free_fw(fw);
184 		return err;
185 	}
186 #ifdef CONFIG_PM
187 	vx->firmware[index] = fw;
188 #else
189 	free_fw(fw);
190 #endif
191 
192 	if (index == 1)
193 		vx->chip_status |= VX_STAT_XILINX_LOADED;
194 	if (index < 3)
195 		return 0;
196 
197 	/* ok, we reached to the last one */
198 	/* create the devices if not built yet */
199 	if (! (vx->chip_status & VX_STAT_DEVICE_INIT)) {
200 		if ((err = snd_vx_pcm_new(vx)) < 0)
201 			return err;
202 
203 		if ((err = snd_vx_mixer_new(vx)) < 0)
204 			return err;
205 
206 		if (vx->ops->add_controls)
207 			if ((err = vx->ops->add_controls(vx)) < 0)
208 				return err;
209 
210 		if ((err = snd_card_register(vx->card)) < 0)
211 			return err;
212 
213 		vx->chip_status |= VX_STAT_DEVICE_INIT;
214 	}
215 	vx->chip_status |= VX_STAT_CHIP_INIT;
216 	return 0;
217 }
218 
219 
220 /* exported */
221 int snd_vx_setup_firmware(struct vx_core *chip)
222 {
223 	int err;
224 	struct snd_hwdep *hw;
225 
226 	if ((err = snd_hwdep_new(chip->card, SND_VX_HWDEP_ID, 0, &hw)) < 0)
227 		return err;
228 
229 	hw->iface = SNDRV_HWDEP_IFACE_VX;
230 	hw->private_data = chip;
231 	hw->ops.open = vx_hwdep_open;
232 	hw->ops.release = vx_hwdep_release;
233 	hw->ops.dsp_status = vx_hwdep_dsp_status;
234 	hw->ops.dsp_load = vx_hwdep_dsp_load;
235 	hw->exclusive = 1;
236 	sprintf(hw->name, "VX Loader (%s)", chip->card->driver);
237 	chip->hwdep = hw;
238 
239 	return snd_card_register(chip->card);
240 }
241 
242 /* exported */
243 void snd_vx_free_firmware(struct vx_core *chip)
244 {
245 #ifdef CONFIG_PM
246 	int i;
247 	for (i = 0; i < 4; i++)
248 		free_fw(chip->firmware[i]);
249 #endif
250 }
251 
252 #endif /* SND_VX_FW_LOADER */
253 
254 EXPORT_SYMBOL(snd_vx_setup_firmware);
255 EXPORT_SYMBOL(snd_vx_free_firmware);
256