pcxhr_hwdep.c 11 KB

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