1/* 2 * Driver for Digigram pcxhr compatible soundcards 3 * 4 * hwdep device manager 5 * 6 * Copyright (c) 2004 by Digigram <alsa@digigram.com> 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 <linux/interrupt.h> 24#include <linux/vmalloc.h> 25#include <linux/firmware.h> 26#include <linux/pci.h> 27#include <asm/io.h> 28#include <sound/core.h> 29#include <sound/hwdep.h> 30#include "pcxhr.h" 31#include "pcxhr_mixer.h" 32#include "pcxhr_hwdep.h" 33#include "pcxhr_core.h" 34#include "pcxhr_mix22.h" 35 36 37#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE) 38#if !defined(CONFIG_USE_PCXHRLOADER) && !defined(CONFIG_SND_PCXHR) /* built-in \ 39 kernel */ 40#define SND_PCXHR_FW_LOADER /* use the standard firmware loader */ 41#endif 42#endif 43 44 45static int pcxhr_sub_init(struct pcxhr_mgr *mgr); 46/* 47 * get basic information and init pcxhr card 48 */ 49static int pcxhr_init_board(struct pcxhr_mgr *mgr) 50{ 51 int err; 52 struct pcxhr_rmh rmh; 53 int card_streams; 54 55 /* calc the number of all streams used */ 56 if (mgr->mono_capture) 57 card_streams = mgr->capture_chips * 2; 58 else 59 card_streams = mgr->capture_chips; 60 card_streams += mgr->playback_chips * PCXHR_PLAYBACK_STREAMS; 61 62 /* enable interrupts */ 63 pcxhr_enable_dsp(mgr); 64 65 pcxhr_init_rmh(&rmh, CMD_SUPPORTED); 66 err = pcxhr_send_msg(mgr, &rmh); 67 if (err) 68 return err; 69 /* test 8 or 12 phys out */ 70 if ((rmh.stat[0] & MASK_FIRST_FIELD) != mgr->playback_chips * 2) 71 return -EINVAL; 72 /* test 8 or 2 phys in */ 73 if (((rmh.stat[0] >> (2 * FIELD_SIZE)) & MASK_FIRST_FIELD) < 74 mgr->capture_chips * 2) 75 return -EINVAL; 76 /* test max nb substream per board */ 77 if ((rmh.stat[1] & 0x5F) < card_streams) 78 return -EINVAL; 79 /* test max nb substream per pipe */ 80 if (((rmh.stat[1] >> 7) & 0x5F) < PCXHR_PLAYBACK_STREAMS) 81 return -EINVAL; 82 snd_printdd("supported formats : playback=%x capture=%x\n", 83 rmh.stat[2], rmh.stat[3]); 84 85 pcxhr_init_rmh(&rmh, CMD_VERSION); 86 /* firmware num for DSP */ 87 rmh.cmd[0] |= mgr->firmware_num; 88 /* transfer granularity in samples (should be multiple of 48) */ 89 rmh.cmd[1] = (1<<23) + mgr->granularity; 90 rmh.cmd_len = 2; 91 err = pcxhr_send_msg(mgr, &rmh); 92 if (err) 93 return err; 94 snd_printdd("PCXHR DSP version is %d.%d.%d\n", (rmh.stat[0]>>16)&0xff, 95 (rmh.stat[0]>>8)&0xff, rmh.stat[0]&0xff); 96 mgr->dsp_version = rmh.stat[0]; 97 98 if (mgr->is_hr_stereo) 99 err = hr222_sub_init(mgr); 100 else 101 err = pcxhr_sub_init(mgr); 102 return err; 103} 104 105static int pcxhr_sub_init(struct pcxhr_mgr *mgr) 106{ 107 int err; 108 struct pcxhr_rmh rmh; 109 110 /* get options */ 111 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ); 112 rmh.cmd[0] |= IO_NUM_REG_STATUS; 113 rmh.cmd[1] = REG_STATUS_OPTIONS; 114 rmh.cmd_len = 2; 115 err = pcxhr_send_msg(mgr, &rmh); 116 if (err) 117 return err; 118 119 if ((rmh.stat[1] & REG_STATUS_OPT_DAUGHTER_MASK) == 120 REG_STATUS_OPT_ANALOG_BOARD) 121 mgr->board_has_analog = 1; /* analog addon board found */ 122 123 /* unmute inputs */ 124 err = pcxhr_write_io_num_reg_cont(mgr, REG_CONT_UNMUTE_INPUTS, 125 REG_CONT_UNMUTE_INPUTS, NULL); 126 if (err) 127 return err; 128 /* unmute outputs (a write to IO_NUM_REG_MUTE_OUT mutes!) */ 129 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ); 130 rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT; 131 if (DSP_EXT_CMD_SET(mgr)) { 132 rmh.cmd[1] = 1; /* unmute digital plugs */ 133 rmh.cmd_len = 2; 134 } 135 err = pcxhr_send_msg(mgr, &rmh); 136 return err; 137} 138 139void pcxhr_reset_board(struct pcxhr_mgr *mgr) 140{ 141 struct pcxhr_rmh rmh; 142 143 if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX)) { 144 /* mute outputs */ 145 if (!mgr->is_hr_stereo) { 146 /* a read to IO_NUM_REG_MUTE_OUT register unmutes! */ 147 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE); 148 rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT; 149 pcxhr_send_msg(mgr, &rmh); 150 /* mute inputs */ 151 pcxhr_write_io_num_reg_cont(mgr, REG_CONT_UNMUTE_INPUTS, 152 0, NULL); 153 } 154 /* stereo cards mute with reset of dsp */ 155 } 156 /* reset pcxhr dsp */ 157 if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_EPRM_INDEX)) 158 pcxhr_reset_dsp(mgr); 159 /* reset second xilinx */ 160 if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_XLX_COM_INDEX)) { 161 pcxhr_reset_xilinx_com(mgr); 162 mgr->dsp_loaded = 1; 163 } 164 return; 165} 166 167 168/* 169 * allocate a playback/capture pipe (pcmp0/pcmc0) 170 */ 171static int pcxhr_dsp_allocate_pipe(struct pcxhr_mgr *mgr, 172 struct pcxhr_pipe *pipe, 173 int is_capture, int pin) 174{ 175 int stream_count, audio_count; 176 int err; 177 struct pcxhr_rmh rmh; 178 179 if (is_capture) { 180 stream_count = 1; 181 if (mgr->mono_capture) 182 audio_count = 1; 183 else 184 audio_count = 2; 185 } else { 186 stream_count = PCXHR_PLAYBACK_STREAMS; 187 audio_count = 2; /* always stereo */ 188 } 189 snd_printdd("snd_add_ref_pipe pin(%d) pcm%c0\n", 190 pin, is_capture ? 'c' : 'p'); 191 pipe->is_capture = is_capture; 192 pipe->first_audio = pin; 193 /* define pipe (P_PCM_ONLY_MASK (0x020000) is not necessary) */ 194 pcxhr_init_rmh(&rmh, CMD_RES_PIPE); 195 pcxhr_set_pipe_cmd_params(&rmh, is_capture, pin, 196 audio_count, stream_count); 197 rmh.cmd[1] |= 0x020000; /* add P_PCM_ONLY_MASK */ 198 if (DSP_EXT_CMD_SET(mgr)) { 199 /* add channel mask to command */ 200 rmh.cmd[rmh.cmd_len++] = (audio_count == 1) ? 0x01 : 0x03; 201 } 202 err = pcxhr_send_msg(mgr, &rmh); 203 if (err < 0) { 204 snd_printk(KERN_ERR "error pipe allocation " 205 "(CMD_RES_PIPE) err=%x!\n", err); 206 return err; 207 } 208 pipe->status = PCXHR_PIPE_DEFINED; 209 210 return 0; 211} 212 213/* 214 * free playback/capture pipe (pcmp0/pcmc0) 215 */ 216 217 218static int pcxhr_config_pipes(struct pcxhr_mgr *mgr) 219{ 220 int err, i, j; 221 struct snd_pcxhr *chip; 222 struct pcxhr_pipe *pipe; 223 224 /* allocate the pipes on the dsp */ 225 for (i = 0; i < mgr->num_cards; i++) { 226 chip = mgr->chip[i]; 227 if (chip->nb_streams_play) { 228 pipe = &chip->playback_pipe; 229 err = pcxhr_dsp_allocate_pipe( mgr, pipe, 0, i*2); 230 if (err) 231 return err; 232 for(j = 0; j < chip->nb_streams_play; j++) 233 chip->playback_stream[j].pipe = pipe; 234 } 235 for (j = 0; j < chip->nb_streams_capt; j++) { 236 pipe = &chip->capture_pipe[j]; 237 err = pcxhr_dsp_allocate_pipe(mgr, pipe, 1, i*2 + j); 238 if (err) 239 return err; 240 chip->capture_stream[j].pipe = pipe; 241 } 242 } 243 return 0; 244} 245 246static int pcxhr_start_pipes(struct pcxhr_mgr *mgr) 247{ 248 int i, j; 249 struct snd_pcxhr *chip; 250 int playback_mask = 0; 251 int capture_mask = 0; 252 253 /* start all the pipes on the dsp */ 254 for (i = 0; i < mgr->num_cards; i++) { 255 chip = mgr->chip[i]; 256 if (chip->nb_streams_play) 257 playback_mask |= 1 << chip->playback_pipe.first_audio; 258 for (j = 0; j < chip->nb_streams_capt; j++) 259 capture_mask |= 1 << chip->capture_pipe[j].first_audio; 260 } 261 return pcxhr_set_pipe_state(mgr, playback_mask, capture_mask, 1); 262} 263 264 265static int pcxhr_dsp_load(struct pcxhr_mgr *mgr, int index, 266 const struct firmware *dsp) 267{ 268 int err, card_index; 269 270 snd_printdd("loading dsp [%d] size = %Zd\n", index, dsp->size); 271 272 switch (index) { 273 case PCXHR_FIRMWARE_XLX_INT_INDEX: 274 pcxhr_reset_xilinx_com(mgr); 275 return pcxhr_load_xilinx_binary(mgr, dsp, 0); 276 277 case PCXHR_FIRMWARE_XLX_COM_INDEX: 278 pcxhr_reset_xilinx_com(mgr); 279 return pcxhr_load_xilinx_binary(mgr, dsp, 1); 280 281 case PCXHR_FIRMWARE_DSP_EPRM_INDEX: 282 pcxhr_reset_dsp(mgr); 283 return pcxhr_load_eeprom_binary(mgr, dsp); 284 285 case PCXHR_FIRMWARE_DSP_BOOT_INDEX: 286 return pcxhr_load_boot_binary(mgr, dsp); 287 288 case PCXHR_FIRMWARE_DSP_MAIN_INDEX: 289 err = pcxhr_load_dsp_binary(mgr, dsp); 290 if (err) 291 return err; 292 break; /* continue with first init */ 293 default: 294 snd_printk(KERN_ERR "wrong file index\n"); 295 return -EFAULT; 296 } /* end of switch file index*/ 297 298 /* first communication with embedded */ 299 err = pcxhr_init_board(mgr); 300 if (err < 0) { 301 snd_printk(KERN_ERR "pcxhr could not be set up\n"); 302 return err; 303 } 304 err = pcxhr_config_pipes(mgr); 305 if (err < 0) { 306 snd_printk(KERN_ERR "pcxhr pipes could not be set up\n"); 307 return err; 308 } 309 /* create devices and mixer in accordance with HW options*/ 310 for (card_index = 0; card_index < mgr->num_cards; card_index++) { 311 struct snd_pcxhr *chip = mgr->chip[card_index]; 312 313 if ((err = pcxhr_create_pcm(chip)) < 0) 314 return err; 315 316 if (card_index == 0) { 317 if ((err = pcxhr_create_mixer(chip->mgr)) < 0) 318 return err; 319 } 320 if ((err = snd_card_register(chip->card)) < 0) 321 return err; 322 } 323 err = pcxhr_start_pipes(mgr); 324 if (err < 0) { 325 snd_printk(KERN_ERR "pcxhr pipes could not be started\n"); 326 return err; 327 } 328 snd_printdd("pcxhr firmware downloaded and successfully set up\n"); 329 330 return 0; 331} 332 333/* 334 * fw loader entry 335 */ 336#ifdef SND_PCXHR_FW_LOADER 337 338int pcxhr_setup_firmware(struct pcxhr_mgr *mgr) 339{ 340 static char *fw_files[][5] = { 341 [0] = { "xlxint.dat", "xlxc882hr.dat", 342 "dspe882.e56", "dspb882hr.b56", "dspd882.d56" }, 343 [1] = { "xlxint.dat", "xlxc882e.dat", 344 "dspe882.e56", "dspb882e.b56", "dspd882.d56" }, 345 [2] = { "xlxint.dat", "xlxc1222hr.dat", 346 "dspe882.e56", "dspb1222hr.b56", "dspd1222.d56" }, 347 [3] = { "xlxint.dat", "xlxc1222e.dat", 348 "dspe882.e56", "dspb1222e.b56", "dspd1222.d56" }, 349 [4] = { NULL, "xlxc222.dat", 350 "dspe924.e56", "dspb924.b56", "dspd222.d56" }, 351 [5] = { NULL, "xlxc924.dat", 352 "dspe924.e56", "dspb924.b56", "dspd222.d56" }, 353 }; 354 char path[32]; 355 356 const struct firmware *fw_entry; 357 int i, err; 358 int fw_set = mgr->fw_file_set; 359 360 for (i = 0; i < 5; i++) { 361 if (!fw_files[fw_set][i]) 362 continue; 363 sprintf(path, "pcxhr/%s", fw_files[fw_set][i]); 364 if (request_firmware(&fw_entry, path, &mgr->pci->dev)) { 365 snd_printk(KERN_ERR "pcxhr: can't load firmware %s\n", 366 path); 367 return -ENOENT; 368 } 369 /* fake hwdep dsp record */ 370 err = pcxhr_dsp_load(mgr, i, fw_entry); 371 release_firmware(fw_entry); 372 if (err < 0) 373 return err; 374 mgr->dsp_loaded |= 1 << i; 375 } 376 return 0; 377} 378 379MODULE_FIRMWARE("pcxhr/xlxint.dat"); 380MODULE_FIRMWARE("pcxhr/xlxc882hr.dat"); 381MODULE_FIRMWARE("pcxhr/xlxc882e.dat"); 382MODULE_FIRMWARE("pcxhr/dspe882.e56"); 383MODULE_FIRMWARE("pcxhr/dspb882hr.b56"); 384MODULE_FIRMWARE("pcxhr/dspb882e.b56"); 385MODULE_FIRMWARE("pcxhr/dspd882.d56"); 386 387MODULE_FIRMWARE("pcxhr/xlxc1222hr.dat"); 388MODULE_FIRMWARE("pcxhr/xlxc1222e.dat"); 389MODULE_FIRMWARE("pcxhr/dspb1222hr.b56"); 390MODULE_FIRMWARE("pcxhr/dspb1222e.b56"); 391MODULE_FIRMWARE("pcxhr/dspd1222.d56"); 392 393MODULE_FIRMWARE("pcxhr/xlxc222.dat"); 394MODULE_FIRMWARE("pcxhr/xlxc924.dat"); 395MODULE_FIRMWARE("pcxhr/dspe924.e56"); 396MODULE_FIRMWARE("pcxhr/dspb924.b56"); 397MODULE_FIRMWARE("pcxhr/dspd222.d56"); 398 399 400#else /* old style firmware loading */ 401 402/* pcxhr hwdep interface id string */ 403#define PCXHR_HWDEP_ID "pcxhr loader" 404 405 406static int pcxhr_hwdep_dsp_status(struct snd_hwdep *hw, 407 struct snd_hwdep_dsp_status *info) 408{ 409 struct pcxhr_mgr *mgr = hw->private_data; 410 sprintf(info->id, "pcxhr%d", mgr->fw_file_set); 411 info->num_dsps = PCXHR_FIRMWARE_FILES_MAX_INDEX; 412 413 if (hw->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX)) 414 info->chip_ready = 1; 415 416 info->version = PCXHR_DRIVER_VERSION; 417 return 0; 418} 419 420static int pcxhr_hwdep_dsp_load(struct snd_hwdep *hw, 421 struct snd_hwdep_dsp_image *dsp) 422{ 423 struct pcxhr_mgr *mgr = hw->private_data; 424 int err; 425 struct firmware fw; 426 427 fw.size = dsp->length; 428 fw.data = vmalloc(fw.size); 429 if (! fw.data) { 430 snd_printk(KERN_ERR "pcxhr: cannot allocate dsp image " 431 "(%lu bytes)\n", (unsigned long)fw.size); 432 return -ENOMEM; 433 } 434 if (copy_from_user((void *)fw.data, dsp->image, dsp->length)) { 435 vfree(fw.data); 436 return -EFAULT; 437 } 438 err = pcxhr_dsp_load(mgr, dsp->index, &fw); 439 vfree(fw.data); 440 if (err < 0) 441 return err; 442 mgr->dsp_loaded |= 1 << dsp->index; 443 return 0; 444} 445 446int pcxhr_setup_firmware(struct pcxhr_mgr *mgr) 447{ 448 int err; 449 struct snd_hwdep *hw; 450 451 /* only create hwdep interface for first cardX 452 * (see "index" module parameter) 453 */ 454 err = snd_hwdep_new(mgr->chip[0]->card, PCXHR_HWDEP_ID, 0, &hw); 455 if (err < 0) 456 return err; 457 458 hw->iface = SNDRV_HWDEP_IFACE_PCXHR; 459 hw->private_data = mgr; 460 hw->ops.dsp_status = pcxhr_hwdep_dsp_status; 461 hw->ops.dsp_load = pcxhr_hwdep_dsp_load; 462 hw->exclusive = 1; 463 /* stereo cards don't need fw_file_0 -> dsp_loaded = 1 */ 464 hw->dsp_loaded = mgr->is_hr_stereo ? 1 : 0; 465 mgr->dsp_loaded = 0; 466 sprintf(hw->name, PCXHR_HWDEP_ID); 467 468 err = snd_card_register(mgr->chip[0]->card); 469 if (err < 0) 470 return err; 471 return 0; 472} 473 474#endif /* SND_PCXHR_FW_LOADER */ 475