c4993b004c420be609e7d95530a48634f17f2ae0
[linux-2.6.git] / sound / drivers / vx / vx_hwdep.c
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(vx_core_t *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(vx_core_t *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(snd_hwdep_t *hw, struct file *file)
110 {
111         return 0;
112 }
113
114 static int vx_hwdep_release(snd_hwdep_t *hw, struct file *file)
115 {
116         return 0;
117 }
118
119 static int vx_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t *info)
120 {
121         static char *type_ids[VX_TYPE_NUMS] = {
122                 [VX_TYPE_BOARD] = "vxboard",
123                 [VX_TYPE_V2] = "vx222",
124                 [VX_TYPE_MIC] = "vx222",
125                 [VX_TYPE_VXPOCKET] = "vxpocket",
126                 [VX_TYPE_VXP440] = "vxp440",
127         };
128         vx_core_t *vx = hw->private_data;
129
130         snd_assert(type_ids[vx->type], return -EINVAL);
131         strcpy(info->id, type_ids[vx->type]);
132         if (vx_is_pcmcia(vx))
133                 info->num_dsps = 4;
134         else
135                 info->num_dsps = 3;
136         if (vx->chip_status & VX_STAT_CHIP_INIT)
137                 info->chip_ready = 1;
138         info->version = VX_DRIVER_VERSION;
139         return 0;
140 }
141
142 static void free_fw(const struct firmware *fw)
143 {
144         if (fw) {
145                 vfree(fw->data);
146                 kfree(fw);
147         }
148 }
149
150 static int vx_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t *dsp)
151 {
152         vx_core_t *vx = hw->private_data;
153         int index, err;
154         struct firmware *fw;
155
156         snd_assert(vx->ops->load_dsp, return -ENXIO);
157
158         fw = kmalloc(sizeof(*fw), GFP_KERNEL);
159         if (! fw) {
160                 snd_printk(KERN_ERR "cannot allocate firmware\n");
161                 return -ENOMEM;
162         }
163         fw->size = dsp->length;
164         fw->data = vmalloc(fw->size);
165         if (! fw->data) {
166                 snd_printk(KERN_ERR "cannot allocate firmware image (length=%d)\n",
167                            (int)fw->size);
168                 kfree(fw);
169                 return -ENOMEM;
170         }
171         if (copy_from_user(fw->data, dsp->image, dsp->length)) {
172                 free_fw(fw);
173                 return -EFAULT;
174         }
175
176         index = dsp->index;
177         if (! vx_is_pcmcia(vx))
178                 index++;
179         err = vx->ops->load_dsp(vx, index, fw);
180         if (err < 0) {
181                 free_fw(fw);
182                 return err;
183         }
184 #ifdef CONFIG_PM
185         vx->firmware[index] = fw;
186 #else
187         free_fw(fw);
188 #endif
189
190         if (index == 1)
191                 vx->chip_status |= VX_STAT_XILINX_LOADED;
192         if (index < 3)
193                 return 0;
194
195         /* ok, we reached to the last one */
196         /* create the devices if not built yet */
197         if (! (vx->chip_status & VX_STAT_DEVICE_INIT)) {
198                 if ((err = snd_vx_pcm_new(vx)) < 0)
199                         return err;
200
201                 if ((err = snd_vx_mixer_new(vx)) < 0)
202                         return err;
203
204                 if (vx->ops->add_controls)
205                         if ((err = vx->ops->add_controls(vx)) < 0)
206                                 return err;
207
208                 if ((err = snd_card_register(vx->card)) < 0)
209                         return err;
210
211                 vx->chip_status |= VX_STAT_DEVICE_INIT;
212         }
213         vx->chip_status |= VX_STAT_CHIP_INIT;
214         return 0;
215 }
216
217
218 /* exported */
219 int snd_vx_setup_firmware(vx_core_t *chip)
220 {
221         int err;
222         snd_hwdep_t *hw;
223
224         if ((err = snd_hwdep_new(chip->card, SND_VX_HWDEP_ID, 0, &hw)) < 0)
225                 return err;
226
227         hw->iface = SNDRV_HWDEP_IFACE_VX;
228         hw->private_data = chip;
229         hw->ops.open = vx_hwdep_open;
230         hw->ops.release = vx_hwdep_release;
231         hw->ops.dsp_status = vx_hwdep_dsp_status;
232         hw->ops.dsp_load = vx_hwdep_dsp_load;
233         hw->exclusive = 1;
234         sprintf(hw->name, "VX Loader (%s)", chip->card->driver);
235         chip->hwdep = hw;
236
237         return snd_card_register(chip->card);
238 }
239
240 /* exported */
241 void snd_vx_free_firmware(vx_core_t *chip)
242 {
243 #ifdef CONFIG_PM
244         int i;
245         for (i = 0; i < 4; i++)
246                 free_fw(chip->firmware[i]);
247 #endif
248 }
249
250 #endif /* SND_VX_FW_LOADER */