pcxhr_hwdep.c 13 KB

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