pcxhr_hwdep.c 13 KB

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