pcxhr_hwdep.c 12 KB

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