vserver 1.9.5.x5
[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/firmware.h>
25 #include <sound/core.h>
26 #include <sound/hwdep.h>
27 #include <sound/vx_core.h>
28
29 #ifdef SND_VX_FW_LOADER
30
31 int snd_vx_setup_firmware(vx_core_t *chip)
32 {
33         static char *fw_files[VX_TYPE_NUMS][4] = {
34                 [VX_TYPE_BOARD] = {
35                         NULL, "x1_1_vx2.xlx", "bd56002.boot", "l_1_vx2.d56",
36                 },
37                 [VX_TYPE_V2] = {
38                         NULL, "x1_2_v22.xlx", "bd563v2.boot", "l_1_v22.d56",
39                 },
40                 [VX_TYPE_MIC] = {
41                         NULL, "x1_2_v22.xlx", "bd563v2.boot", "l_1_v22.d56",
42                 },
43                 [VX_TYPE_VXPOCKET] = {
44                         "bx_1_vxp.b56", "x1_1_vxp.xlx", "bd563s3.boot", "l_1_vxp.d56"
45                 },
46                 [VX_TYPE_VXP440] = {
47                         "bx_1_vp4.b56", "x1_1_vp4.xlx", "bd563s3.boot", "l_1_vp4.d56"
48                 },
49         };
50
51         int i, err;
52
53         for (i = 0; i < 4; i++) {
54                 char path[32];
55                 const struct firmware *fw;
56                 if (! fw_files[chip->type][i])
57                         continue;
58                 sprintf(path, "vx/%s", fw_files[chip->type][i]);
59                 if (request_firmware(&fw, path, chip->dev)) {
60                         snd_printk(KERN_ERR "vx: can't load firmware %s\n", path);
61                         return -ENOENT;
62                 }
63                 err = chip->ops->load_dsp(chip, i, fw);
64                 if (err < 0) {
65                         release_firmware(fw);
66                         return err;
67                 }
68                 if (i == 1)
69                         chip->chip_status |= VX_STAT_XILINX_LOADED;
70 #ifdef CONFIG_PM
71                 chip->firmware[i] = fw;
72 #else
73                 release_firmware(fw);
74 #endif
75         }
76
77         /* ok, we reached to the last one */
78         /* create the devices if not built yet */
79         if ((err = snd_vx_pcm_new(chip)) < 0)
80                 return err;
81
82         if ((err = snd_vx_mixer_new(chip)) < 0)
83                 return err;
84
85         if (chip->ops->add_controls)
86                 if ((err = chip->ops->add_controls(chip)) < 0)
87                         return err;
88
89         chip->chip_status |= VX_STAT_DEVICE_INIT;
90         chip->chip_status |= VX_STAT_CHIP_INIT;
91
92         return snd_card_register(chip->card);
93 }
94
95 /* exported */
96 void snd_vx_free_firmware(vx_core_t *chip)
97 {
98 #ifdef CONFIG_PM
99         int i;
100         for (i = 0; i < 4; i++)
101                 release_firmware(chip->firmware[i]);
102 #endif
103 }
104
105 #else /* old style firmware loading */
106
107 static int vx_hwdep_open(snd_hwdep_t *hw, struct file *file)
108 {
109         return 0;
110 }
111
112 static int vx_hwdep_release(snd_hwdep_t *hw, struct file *file)
113 {
114         return 0;
115 }
116
117 static int vx_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t *info)
118 {
119         static char *type_ids[VX_TYPE_NUMS] = {
120                 [VX_TYPE_BOARD] = "vxboard",
121                 [VX_TYPE_V2] = "vx222",
122                 [VX_TYPE_MIC] = "vx222",
123                 [VX_TYPE_VXPOCKET] = "vxpocket",
124                 [VX_TYPE_VXP440] = "vxp440",
125         };
126         vx_core_t *vx = hw->private_data;
127
128         snd_assert(type_ids[vx->type], return -EINVAL);
129         strcpy(info->id, type_ids[vx->type]);
130         if (vx_is_pcmcia(vx))
131                 info->num_dsps = 4;
132         else
133                 info->num_dsps = 3;
134         if (vx->chip_status & VX_STAT_CHIP_INIT)
135                 info->chip_ready = 1;
136         info->version = VX_DRIVER_VERSION;
137         return 0;
138 }
139
140 static void free_fw(const struct firmware *fw)
141 {
142         if (fw) {
143                 vfree(fw->data);
144                 kfree(fw);
145         }
146 }
147
148 static int vx_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t *dsp)
149 {
150         vx_core_t *vx = hw->private_data;
151         int index, err;
152         struct firmware *fw;
153
154         snd_assert(vx->ops->load_dsp, return -ENXIO);
155
156         fw = kmalloc(sizeof(*fw), GFP_KERNEL);
157         if (! fw) {
158                 snd_printk(KERN_ERR "cannot allocate firmware\n");
159                 return -ENOMEM;
160         }
161         fw->size = dsp->length;
162         fw->data = vmalloc(fw->size);
163         if (! fw->data) {
164                 snd_printk(KERN_ERR "cannot allocate firmware image (length=%d)\n",
165                            (int)fw->size);
166                 kfree(fw);
167                 return -ENOMEM;
168         }
169         if (copy_from_user(fw->data, dsp->image, dsp->length)) {
170                 free_fw(fw);
171                 return -EFAULT;
172         }
173
174         index = dsp->index;
175         if (! vx_is_pcmcia(vx))
176                 index++;
177         err = vx->ops->load_dsp(vx, index, fw);
178         if (err < 0) {
179                 free_fw(fw);
180                 return err;
181         }
182 #ifdef CONFIG_PM
183         vx->firmware[index] = fw;
184 #else
185         free_fw(fw);
186 #endif
187
188         if (index == 1)
189                 vx->chip_status |= VX_STAT_XILINX_LOADED;
190         if (index < 3)
191                 return 0;
192
193         /* ok, we reached to the last one */
194         /* create the devices if not built yet */
195         if (! (vx->chip_status & VX_STAT_DEVICE_INIT)) {
196                 if ((err = snd_vx_pcm_new(vx)) < 0)
197                         return err;
198
199                 if ((err = snd_vx_mixer_new(vx)) < 0)
200                         return err;
201
202                 if (vx->ops->add_controls)
203                         if ((err = vx->ops->add_controls(vx)) < 0)
204                                 return err;
205
206                 if ((err = snd_card_register(vx->card)) < 0)
207                         return err;
208
209                 vx->chip_status |= VX_STAT_DEVICE_INIT;
210         }
211         vx->chip_status |= VX_STAT_CHIP_INIT;
212         return 0;
213 }
214
215
216 /* exported */
217 int snd_vx_setup_firmware(vx_core_t *chip)
218 {
219         int err;
220         snd_hwdep_t *hw;
221
222         if ((err = snd_hwdep_new(chip->card, SND_VX_HWDEP_ID, 0, &hw)) < 0)
223                 return err;
224
225         hw->iface = SNDRV_HWDEP_IFACE_VX;
226         hw->private_data = chip;
227         hw->ops.open = vx_hwdep_open;
228         hw->ops.release = vx_hwdep_release;
229         hw->ops.dsp_status = vx_hwdep_dsp_status;
230         hw->ops.dsp_load = vx_hwdep_dsp_load;
231         hw->exclusive = 1;
232         sprintf(hw->name, "VX Loader (%s)", chip->card->driver);
233         chip->hwdep = hw;
234
235         return snd_card_register(chip->card);
236 }
237
238 /* exported */
239 void snd_vx_free_firmware(vx_core_t *chip)
240 {
241 #ifdef CONFIG_PM
242         int i;
243         for (i = 0; i < 4; i++)
244                 free_fw(chip->firmware[i]);
245 #endif
246 }
247
248 #endif /* SND_VX_FW_LOADER */