pcxhr_hwdep.c 12 KB

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