echoaudio_dsp.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /****************************************************************************
  2. Copyright Echo Digital Audio Corporation (c) 1998 - 2004
  3. All rights reserved
  4. www.echoaudio.com
  5. This file is part of Echo Digital Audio's generic driver library.
  6. Echo Digital Audio's generic driver library is free software;
  7. you can redistribute it and/or modify it under the terms of
  8. the GNU General Public License as published by the Free Software
  9. Foundation.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. MA 02111-1307, USA.
  18. *************************************************************************
  19. Translation from C++ and adaptation for use in ALSA-Driver
  20. were made by Giuliano Pochini <pochini@shiny.it>
  21. ****************************************************************************/
  22. #if PAGE_SIZE < 4096
  23. #error PAGE_SIZE is < 4k
  24. #endif
  25. static int restore_dsp_rettings(struct echoaudio *chip);
  26. /* Some vector commands involve the DSP reading or writing data to and from the
  27. comm page; if you send one of these commands to the DSP, it will complete the
  28. command and then write a non-zero value to the Handshake field in the
  29. comm page. This function waits for the handshake to show up. */
  30. static int wait_handshake(struct echoaudio *chip)
  31. {
  32. int i;
  33. /* Wait up to 10ms for the handshake from the DSP */
  34. for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
  35. /* Look for the handshake value */
  36. if (chip->comm_page->handshake) {
  37. /*if (i) DE_ACT(("Handshake time: %d\n", i));*/
  38. return 0;
  39. }
  40. udelay(1);
  41. }
  42. snd_printk(KERN_ERR "wait_handshake(): Timeout waiting for DSP\n");
  43. return -EBUSY;
  44. }
  45. /* Much of the interaction between the DSP and the driver is done via vector
  46. commands; send_vector writes a vector command to the DSP. Typically, this
  47. causes the DSP to read or write fields in the comm page.
  48. PCI posting is not required thanks to the handshake logic. */
  49. static int send_vector(struct echoaudio *chip, u32 command)
  50. {
  51. int i;
  52. wmb(); /* Flush all pending writes before sending the command */
  53. /* Wait up to 100ms for the "vector busy" bit to be off */
  54. for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
  55. if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
  56. CHI32_VECTOR_BUSY)) {
  57. set_dsp_register(chip, CHI32_VECTOR_REG, command);
  58. /*if (i) DE_ACT(("send_vector time: %d\n", i));*/
  59. return 0;
  60. }
  61. udelay(1);
  62. }
  63. DE_ACT((KERN_ERR "timeout on send_vector\n"));
  64. return -EBUSY;
  65. }
  66. /* write_dsp writes a 32-bit value to the DSP; this is used almost
  67. exclusively for loading the DSP. */
  68. static int write_dsp(struct echoaudio *chip, u32 data)
  69. {
  70. u32 status, i;
  71. for (i = 0; i < 10000000; i++) { /* timeout = 10s */
  72. status = get_dsp_register(chip, CHI32_STATUS_REG);
  73. if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
  74. set_dsp_register(chip, CHI32_DATA_REG, data);
  75. wmb(); /* write it immediately */
  76. return 0;
  77. }
  78. udelay(1);
  79. cond_resched();
  80. }
  81. chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */
  82. DE_ACT((KERN_ERR "write_dsp: Set bad_board to TRUE\n"));
  83. return -EIO;
  84. }
  85. /* read_dsp reads a 32-bit value from the DSP; this is used almost
  86. exclusively for loading the DSP and checking the status of the ASIC. */
  87. static int read_dsp(struct echoaudio *chip, u32 *data)
  88. {
  89. u32 status, i;
  90. for (i = 0; i < READ_DSP_TIMEOUT; i++) {
  91. status = get_dsp_register(chip, CHI32_STATUS_REG);
  92. if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
  93. *data = get_dsp_register(chip, CHI32_DATA_REG);
  94. return 0;
  95. }
  96. udelay(1);
  97. cond_resched();
  98. }
  99. chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */
  100. DE_INIT((KERN_ERR "read_dsp: Set bad_board to TRUE\n"));
  101. return -EIO;
  102. }
  103. /****************************************************************************
  104. Firmware loading functions
  105. ****************************************************************************/
  106. /* This function is used to read back the serial number from the DSP;
  107. this is triggered by the SET_COMMPAGE_ADDR command.
  108. Only some early Echogals products have serial numbers in the ROM;
  109. the serial number is not used, but you still need to do this as
  110. part of the DSP load process. */
  111. static int read_sn(struct echoaudio *chip)
  112. {
  113. int i;
  114. u32 sn[6];
  115. for (i = 0; i < 5; i++) {
  116. if (read_dsp(chip, &sn[i])) {
  117. snd_printk(KERN_ERR "Failed to read serial number\n");
  118. return -EIO;
  119. }
  120. }
  121. DE_INIT(("Read serial number %08x %08x %08x %08x %08x\n",
  122. sn[0], sn[1], sn[2], sn[3], sn[4]));
  123. return 0;
  124. }
  125. #ifndef ECHOCARD_HAS_ASIC
  126. /* This card has no ASIC, just return ok */
  127. static inline int check_asic_status(struct echoaudio *chip)
  128. {
  129. chip->asic_loaded = TRUE;
  130. return 0;
  131. }
  132. #endif /* !ECHOCARD_HAS_ASIC */
  133. #ifdef ECHOCARD_HAS_ASIC
  134. /* Load ASIC code - done after the DSP is loaded */
  135. static int load_asic_generic(struct echoaudio *chip, u32 cmd,
  136. const struct firmware *asic)
  137. {
  138. const struct firmware *fw;
  139. int err;
  140. u32 i, size;
  141. u8 *code;
  142. if ((err = get_firmware(&fw, asic, chip)) < 0) {
  143. snd_printk(KERN_WARNING "Firmware not found !\n");
  144. return err;
  145. }
  146. code = (u8 *)fw->data;
  147. size = fw->size;
  148. /* Send the "Here comes the ASIC" command */
  149. if (write_dsp(chip, cmd) < 0)
  150. goto la_error;
  151. /* Write length of ASIC file in bytes */
  152. if (write_dsp(chip, size) < 0)
  153. goto la_error;
  154. for (i = 0; i < size; i++) {
  155. if (write_dsp(chip, code[i]) < 0)
  156. goto la_error;
  157. }
  158. DE_INIT(("ASIC loaded\n"));
  159. free_firmware(fw);
  160. return 0;
  161. la_error:
  162. DE_INIT(("failed on write_dsp\n"));
  163. free_firmware(fw);
  164. return -EIO;
  165. }
  166. #endif /* ECHOCARD_HAS_ASIC */
  167. #ifdef DSP_56361
  168. /* Install the resident loader for 56361 DSPs; The resident loader is on
  169. the EPROM on the board for 56301 DSP. The resident loader is a tiny little
  170. program that is used to load the real DSP code. */
  171. static int install_resident_loader(struct echoaudio *chip)
  172. {
  173. u32 address;
  174. int index, words, i;
  175. u16 *code;
  176. u32 status;
  177. const struct firmware *fw;
  178. /* 56361 cards only! This check is required by the old 56301-based
  179. Mona and Gina24 */
  180. if (chip->device_id != DEVICE_ID_56361)
  181. return 0;
  182. /* Look to see if the resident loader is present. If the resident
  183. loader is already installed, host flag 5 will be on. */
  184. status = get_dsp_register(chip, CHI32_STATUS_REG);
  185. if (status & CHI32_STATUS_REG_HF5) {
  186. DE_INIT(("Resident loader already installed; status is 0x%x\n",
  187. status));
  188. return 0;
  189. }
  190. if ((i = get_firmware(&fw, &card_fw[FW_361_LOADER], chip)) < 0) {
  191. snd_printk(KERN_WARNING "Firmware not found !\n");
  192. return i;
  193. }
  194. /* The DSP code is an array of 16 bit words. The array is divided up
  195. into sections. The first word of each section is the size in words,
  196. followed by the section type.
  197. Since DSP addresses and data are 24 bits wide, they each take up two
  198. 16 bit words in the array.
  199. This is a lot like the other loader loop, but it's not a loop, you
  200. don't write the memory type, and you don't write a zero at the end. */
  201. /* Set DSP format bits for 24 bit mode */
  202. set_dsp_register(chip, CHI32_CONTROL_REG,
  203. get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
  204. code = (u16 *)fw->data;
  205. /* Skip the header section; the first word in the array is the size
  206. of the first section, so the first real section of code is pointed
  207. to by Code[0]. */
  208. index = code[0];
  209. /* Skip the section size, LRS block type, and DSP memory type */
  210. index += 3;
  211. /* Get the number of DSP words to write */
  212. words = code[index++];
  213. /* Get the DSP address for this block; 24 bits, so build from two words */
  214. address = ((u32)code[index] << 16) + code[index + 1];
  215. index += 2;
  216. /* Write the count to the DSP */
  217. if (write_dsp(chip, words)) {
  218. DE_INIT(("install_resident_loader: Failed to write word count!\n"));
  219. goto irl_error;
  220. }
  221. /* Write the DSP address */
  222. if (write_dsp(chip, address)) {
  223. DE_INIT(("install_resident_loader: Failed to write DSP address!\n"));
  224. goto irl_error;
  225. }
  226. /* Write out this block of code to the DSP */
  227. for (i = 0; i < words; i++) {
  228. u32 data;
  229. data = ((u32)code[index] << 16) + code[index + 1];
  230. if (write_dsp(chip, data)) {
  231. DE_INIT(("install_resident_loader: Failed to write DSP code\n"));
  232. goto irl_error;
  233. }
  234. index += 2;
  235. }
  236. /* Wait for flag 5 to come up */
  237. for (i = 0; i < 200; i++) { /* Timeout is 50us * 200 = 10ms */
  238. udelay(50);
  239. status = get_dsp_register(chip, CHI32_STATUS_REG);
  240. if (status & CHI32_STATUS_REG_HF5)
  241. break;
  242. }
  243. if (i == 200) {
  244. DE_INIT(("Resident loader failed to set HF5\n"));
  245. goto irl_error;
  246. }
  247. DE_INIT(("Resident loader successfully installed\n"));
  248. free_firmware(fw);
  249. return 0;
  250. irl_error:
  251. free_firmware(fw);
  252. return -EIO;
  253. }
  254. #endif /* DSP_56361 */
  255. static int load_dsp(struct echoaudio *chip, u16 *code)
  256. {
  257. u32 address, data;
  258. int index, words, i;
  259. if (chip->dsp_code == code) {
  260. DE_INIT(("DSP is already loaded!\n"));
  261. return 0;
  262. }
  263. chip->bad_board = TRUE; /* Set TRUE until DSP loaded */
  264. chip->dsp_code = NULL; /* Current DSP code not loaded */
  265. chip->asic_loaded = FALSE; /* Loading the DSP code will reset the ASIC */
  266. DE_INIT(("load_dsp: Set bad_board to TRUE\n"));
  267. /* If this board requires a resident loader, install it. */
  268. #ifdef DSP_56361
  269. if ((i = install_resident_loader(chip)) < 0)
  270. return i;
  271. #endif
  272. /* Send software reset command */
  273. if (send_vector(chip, DSP_VC_RESET) < 0) {
  274. DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n"));
  275. return -EIO;
  276. }
  277. /* Delay 10us */
  278. udelay(10);
  279. /* Wait 10ms for HF3 to indicate that software reset is complete */
  280. for (i = 0; i < 1000; i++) { /* Timeout is 10us * 1000 = 10ms */
  281. if (get_dsp_register(chip, CHI32_STATUS_REG) &
  282. CHI32_STATUS_REG_HF3)
  283. break;
  284. udelay(10);
  285. }
  286. if (i == 1000) {
  287. DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n"));
  288. return -EIO;
  289. }
  290. /* Set DSP format bits for 24 bit mode now that soft reset is done */
  291. set_dsp_register(chip, CHI32_CONTROL_REG,
  292. get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
  293. /* Main loader loop */
  294. index = code[0];
  295. for (;;) {
  296. int block_type, mem_type;
  297. /* Total Block Size */
  298. index++;
  299. /* Block Type */
  300. block_type = code[index];
  301. if (block_type == 4) /* We're finished */
  302. break;
  303. index++;
  304. /* Memory Type P=0,X=1,Y=2 */
  305. mem_type = code[index++];
  306. /* Block Code Size */
  307. words = code[index++];
  308. if (words == 0) /* We're finished */
  309. break;
  310. /* Start Address */
  311. address = ((u32)code[index] << 16) + code[index + 1];
  312. index += 2;
  313. if (write_dsp(chip, words) < 0) {
  314. DE_INIT(("load_dsp: failed to write number of DSP words\n"));
  315. return -EIO;
  316. }
  317. if (write_dsp(chip, address) < 0) {
  318. DE_INIT(("load_dsp: failed to write DSP address\n"));
  319. return -EIO;
  320. }
  321. if (write_dsp(chip, mem_type) < 0) {
  322. DE_INIT(("load_dsp: failed to write DSP memory type\n"));
  323. return -EIO;
  324. }
  325. /* Code */
  326. for (i = 0; i < words; i++, index+=2) {
  327. data = ((u32)code[index] << 16) + code[index + 1];
  328. if (write_dsp(chip, data) < 0) {
  329. DE_INIT(("load_dsp: failed to write DSP data\n"));
  330. return -EIO;
  331. }
  332. }
  333. }
  334. if (write_dsp(chip, 0) < 0) { /* We're done!!! */
  335. DE_INIT(("load_dsp: Failed to write final zero\n"));
  336. return -EIO;
  337. }
  338. udelay(10);
  339. for (i = 0; i < 5000; i++) { /* Timeout is 100us * 5000 = 500ms */
  340. /* Wait for flag 4 - indicates that the DSP loaded OK */
  341. if (get_dsp_register(chip, CHI32_STATUS_REG) &
  342. CHI32_STATUS_REG_HF4) {
  343. set_dsp_register(chip, CHI32_CONTROL_REG,
  344. get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
  345. if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
  346. DE_INIT(("load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n"));
  347. return -EIO;
  348. }
  349. if (write_dsp(chip, chip->comm_page_phys) < 0) {
  350. DE_INIT(("load_dsp: Failed to write comm page address\n"));
  351. return -EIO;
  352. }
  353. /* Get the serial number via slave mode.
  354. This is triggered by the SET_COMMPAGE_ADDR command.
  355. We don't actually use the serial number but we have to
  356. get it as part of the DSP init voodoo. */
  357. if (read_sn(chip) < 0) {
  358. DE_INIT(("load_dsp: Failed to read serial number\n"));
  359. return -EIO;
  360. }
  361. chip->dsp_code = code; /* Show which DSP code loaded */
  362. chip->bad_board = FALSE; /* DSP OK */
  363. DE_INIT(("load_dsp: OK!\n"));
  364. return 0;
  365. }
  366. udelay(100);
  367. }
  368. DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n"));
  369. return -EIO;
  370. }
  371. /* load_firmware takes care of loading the DSP and any ASIC code. */
  372. static int load_firmware(struct echoaudio *chip)
  373. {
  374. const struct firmware *fw;
  375. int box_type, err;
  376. snd_assert(chip->dsp_code_to_load && chip->comm_page, return -EPERM);
  377. /* See if the ASIC is present and working - only if the DSP is already loaded */
  378. if (chip->dsp_code) {
  379. if ((box_type = check_asic_status(chip)) >= 0)
  380. return box_type;
  381. /* ASIC check failed; force the DSP to reload */
  382. chip->dsp_code = NULL;
  383. }
  384. if ((err = get_firmware(&fw, chip->dsp_code_to_load, chip)) < 0)
  385. return err;
  386. err = load_dsp(chip, (u16 *)fw->data);
  387. free_firmware(fw);
  388. if (err < 0)
  389. return err;
  390. if ((box_type = load_asic(chip)) < 0)
  391. return box_type; /* error */
  392. if ((err = restore_dsp_rettings(chip)) < 0)
  393. return err;
  394. return box_type;
  395. }
  396. /****************************************************************************
  397. Mixer functions
  398. ****************************************************************************/
  399. #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
  400. defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
  401. /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
  402. static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
  403. {
  404. snd_assert(index < num_busses_out(chip) + num_busses_in(chip),
  405. return -EINVAL);
  406. /* Wait for the handshake (OK even if ASIC is not loaded) */
  407. if (wait_handshake(chip))
  408. return -EIO;
  409. chip->nominal_level[index] = consumer;
  410. if (consumer)
  411. chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
  412. else
  413. chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
  414. return 0;
  415. }
  416. #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
  417. /* Set the gain for a single physical output channel (dB). */
  418. static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
  419. {
  420. snd_assert(channel < num_busses_out(chip), return -EINVAL);
  421. if (wait_handshake(chip))
  422. return -EIO;
  423. /* Save the new value */
  424. chip->output_gain[channel] = gain;
  425. chip->comm_page->line_out_level[channel] = gain;
  426. return 0;
  427. }
  428. #ifdef ECHOCARD_HAS_MONITOR
  429. /* Set the monitor level from an input bus to an output bus. */
  430. static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
  431. s8 gain)
  432. {
  433. snd_assert(output < num_busses_out(chip) &&
  434. input < num_busses_in(chip), return -EINVAL);
  435. if (wait_handshake(chip))
  436. return -EIO;
  437. chip->monitor_gain[output][input] = gain;
  438. chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
  439. return 0;
  440. }
  441. #endif /* ECHOCARD_HAS_MONITOR */
  442. /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
  443. static int update_output_line_level(struct echoaudio *chip)
  444. {
  445. if (wait_handshake(chip))
  446. return -EIO;
  447. clear_handshake(chip);
  448. return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
  449. }
  450. /* Tell the DSP to read and update input levels in comm page */
  451. static int update_input_line_level(struct echoaudio *chip)
  452. {
  453. if (wait_handshake(chip))
  454. return -EIO;
  455. clear_handshake(chip);
  456. return send_vector(chip, DSP_VC_UPDATE_INGAIN);
  457. }
  458. /* set_meters_on turns the meters on or off. If meters are turned on, the DSP
  459. will write the meter and clock detect values to the comm page at about 30Hz */
  460. static void set_meters_on(struct echoaudio *chip, char on)
  461. {
  462. if (on && !chip->meters_enabled) {
  463. send_vector(chip, DSP_VC_METERS_ON);
  464. chip->meters_enabled = 1;
  465. } else if (!on && chip->meters_enabled) {
  466. send_vector(chip, DSP_VC_METERS_OFF);
  467. chip->meters_enabled = 0;
  468. memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
  469. DSP_MAXPIPES);
  470. memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
  471. DSP_MAXPIPES);
  472. }
  473. }
  474. /* Fill out an the given array using the current values in the comm page.
  475. Meters are written in the comm page by the DSP in this order:
  476. Output busses
  477. Input busses
  478. Output pipes (vmixer cards only)
  479. This function assumes there are no more than 16 in/out busses or pipes
  480. Meters is an array [3][16][2] of long. */
  481. static void get_audio_meters(struct echoaudio *chip, long *meters)
  482. {
  483. int i, m, n;
  484. m = 0;
  485. n = 0;
  486. for (i = 0; i < num_busses_out(chip); i++, m++) {
  487. meters[n++] = chip->comm_page->vu_meter[m];
  488. meters[n++] = chip->comm_page->peak_meter[m];
  489. }
  490. for (; n < 32; n++)
  491. meters[n] = 0;
  492. #ifdef ECHOCARD_ECHO3G
  493. m = E3G_MAX_OUTPUTS; /* Skip unused meters */
  494. #endif
  495. for (i = 0; i < num_busses_in(chip); i++, m++) {
  496. meters[n++] = chip->comm_page->vu_meter[m];
  497. meters[n++] = chip->comm_page->peak_meter[m];
  498. }
  499. for (; n < 64; n++)
  500. meters[n] = 0;
  501. #ifdef ECHOCARD_HAS_VMIXER
  502. for (i = 0; i < num_pipes_out(chip); i++, m++) {
  503. meters[n++] = chip->comm_page->vu_meter[m];
  504. meters[n++] = chip->comm_page->peak_meter[m];
  505. }
  506. #endif
  507. for (; n < 96; n++)
  508. meters[n] = 0;
  509. }
  510. static int restore_dsp_rettings(struct echoaudio *chip)
  511. {
  512. int err;
  513. DE_INIT(("restore_dsp_settings\n"));
  514. if ((err = check_asic_status(chip)) < 0)
  515. return err;
  516. /* @ Gina20/Darla20 only. Should be harmless for other cards. */
  517. chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
  518. chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
  519. chip->comm_page->handshake = 0xffffffff;
  520. if ((err = set_sample_rate(chip, chip->sample_rate)) < 0)
  521. return err;
  522. if (chip->meters_enabled)
  523. if (send_vector(chip, DSP_VC_METERS_ON) < 0)
  524. return -EIO;
  525. #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
  526. if (set_input_clock(chip, chip->input_clock) < 0)
  527. return -EIO;
  528. #endif
  529. #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
  530. if (set_output_clock(chip, chip->output_clock) < 0)
  531. return -EIO;
  532. #endif
  533. if (update_output_line_level(chip) < 0)
  534. return -EIO;
  535. if (update_input_line_level(chip) < 0)
  536. return -EIO;
  537. #ifdef ECHOCARD_HAS_VMIXER
  538. if (update_vmixer_level(chip) < 0)
  539. return -EIO;
  540. #endif
  541. if (wait_handshake(chip) < 0)
  542. return -EIO;
  543. clear_handshake(chip);
  544. DE_INIT(("restore_dsp_rettings done\n"));
  545. return send_vector(chip, DSP_VC_UPDATE_FLAGS);
  546. }
  547. /****************************************************************************
  548. Transport functions
  549. ****************************************************************************/
  550. /* set_audio_format() sets the format of the audio data in host memory for
  551. this pipe. Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
  552. but they are here because they are just mono while capturing */
  553. static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
  554. const struct audioformat *format)
  555. {
  556. u16 dsp_format;
  557. dsp_format = DSP_AUDIOFORM_SS_16LE;
  558. /* Look for super-interleave (no big-endian and 8 bits) */
  559. if (format->interleave > 2) {
  560. switch (format->bits_per_sample) {
  561. case 16:
  562. dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
  563. break;
  564. case 24:
  565. dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
  566. break;
  567. case 32:
  568. dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
  569. break;
  570. }
  571. dsp_format |= format->interleave;
  572. } else if (format->data_are_bigendian) {
  573. /* For big-endian data, only 32 bit samples are supported */
  574. switch (format->interleave) {
  575. case 1:
  576. dsp_format = DSP_AUDIOFORM_MM_32BE;
  577. break;
  578. #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
  579. case 2:
  580. dsp_format = DSP_AUDIOFORM_SS_32BE;
  581. break;
  582. #endif
  583. }
  584. } else if (format->interleave == 1 &&
  585. format->bits_per_sample == 32 && !format->mono_to_stereo) {
  586. /* 32 bit little-endian mono->mono case */
  587. dsp_format = DSP_AUDIOFORM_MM_32LE;
  588. } else {
  589. /* Handle the other little-endian formats */
  590. switch (format->bits_per_sample) {
  591. case 8:
  592. if (format->interleave == 2)
  593. dsp_format = DSP_AUDIOFORM_SS_8;
  594. else
  595. dsp_format = DSP_AUDIOFORM_MS_8;
  596. break;
  597. default:
  598. case 16:
  599. if (format->interleave == 2)
  600. dsp_format = DSP_AUDIOFORM_SS_16LE;
  601. else
  602. dsp_format = DSP_AUDIOFORM_MS_16LE;
  603. break;
  604. case 24:
  605. if (format->interleave == 2)
  606. dsp_format = DSP_AUDIOFORM_SS_24LE;
  607. else
  608. dsp_format = DSP_AUDIOFORM_MS_24LE;
  609. break;
  610. case 32:
  611. if (format->interleave == 2)
  612. dsp_format = DSP_AUDIOFORM_SS_32LE;
  613. else
  614. dsp_format = DSP_AUDIOFORM_MS_32LE;
  615. break;
  616. }
  617. }
  618. DE_ACT(("set_audio_format[%d] = %x\n", pipe_index, dsp_format));
  619. chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
  620. }
  621. /* start_transport starts transport for a set of pipes.
  622. The bits 1 in channel_mask specify what pipes to start. Only the bit of the
  623. first channel must be set, regardless its interleave.
  624. Same thing for pause_ and stop_ -trasport below. */
  625. static int start_transport(struct echoaudio *chip, u32 channel_mask,
  626. u32 cyclic_mask)
  627. {
  628. DE_ACT(("start_transport %x\n", channel_mask));
  629. if (wait_handshake(chip))
  630. return -EIO;
  631. chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
  632. if (chip->comm_page->cmd_start) {
  633. clear_handshake(chip);
  634. send_vector(chip, DSP_VC_START_TRANSFER);
  635. if (wait_handshake(chip))
  636. return -EIO;
  637. /* Keep track of which pipes are transporting */
  638. chip->active_mask |= channel_mask;
  639. chip->comm_page->cmd_start = 0;
  640. return 0;
  641. }
  642. DE_ACT(("start_transport: No pipes to start!\n"));
  643. return -EINVAL;
  644. }
  645. static int pause_transport(struct echoaudio *chip, u32 channel_mask)
  646. {
  647. DE_ACT(("pause_transport %x\n", channel_mask));
  648. if (wait_handshake(chip))
  649. return -EIO;
  650. chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
  651. chip->comm_page->cmd_reset = 0;
  652. if (chip->comm_page->cmd_stop) {
  653. clear_handshake(chip);
  654. send_vector(chip, DSP_VC_STOP_TRANSFER);
  655. if (wait_handshake(chip))
  656. return -EIO;
  657. /* Keep track of which pipes are transporting */
  658. chip->active_mask &= ~channel_mask;
  659. chip->comm_page->cmd_stop = 0;
  660. chip->comm_page->cmd_reset = 0;
  661. return 0;
  662. }
  663. DE_ACT(("pause_transport: No pipes to stop!\n"));
  664. return 0;
  665. }
  666. static int stop_transport(struct echoaudio *chip, u32 channel_mask)
  667. {
  668. DE_ACT(("stop_transport %x\n", channel_mask));
  669. if (wait_handshake(chip))
  670. return -EIO;
  671. chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
  672. chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
  673. if (chip->comm_page->cmd_reset) {
  674. clear_handshake(chip);
  675. send_vector(chip, DSP_VC_STOP_TRANSFER);
  676. if (wait_handshake(chip))
  677. return -EIO;
  678. /* Keep track of which pipes are transporting */
  679. chip->active_mask &= ~channel_mask;
  680. chip->comm_page->cmd_stop = 0;
  681. chip->comm_page->cmd_reset = 0;
  682. return 0;
  683. }
  684. DE_ACT(("stop_transport: No pipes to stop!\n"));
  685. return 0;
  686. }
  687. static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
  688. {
  689. return (chip->pipe_alloc_mask & (1 << pipe_index));
  690. }
  691. /* Stops everything and turns off the DSP. All pipes should be already
  692. stopped and unallocated. */
  693. static int rest_in_peace(struct echoaudio *chip)
  694. {
  695. DE_ACT(("rest_in_peace() open=%x\n", chip->pipe_alloc_mask));
  696. /* Stops all active pipes (just to be sure) */
  697. stop_transport(chip, chip->active_mask);
  698. set_meters_on(chip, FALSE);
  699. #ifdef ECHOCARD_HAS_MIDI
  700. enable_midi_input(chip, FALSE);
  701. #endif
  702. /* Go to sleep */
  703. if (chip->dsp_code) {
  704. /* Make load_firmware do a complete reload */
  705. chip->dsp_code = NULL;
  706. /* Put the DSP to sleep */
  707. return send_vector(chip, DSP_VC_GO_COMATOSE);
  708. }
  709. return 0;
  710. }
  711. /* Fills the comm page with default values */
  712. static int init_dsp_comm_page(struct echoaudio *chip)
  713. {
  714. /* Check if the compiler added extra padding inside the structure */
  715. if (offsetof(struct comm_page, midi_output) != 0xbe0) {
  716. DE_INIT(("init_dsp_comm_page() - Invalid struct comm_page structure\n"));
  717. return -EPERM;
  718. }
  719. /* Init all the basic stuff */
  720. chip->card_name = ECHOCARD_NAME;
  721. chip->bad_board = TRUE; /* Set TRUE until DSP loaded */
  722. chip->dsp_code = NULL; /* Current DSP code not loaded */
  723. chip->digital_mode = DIGITAL_MODE_NONE;
  724. chip->input_clock = ECHO_CLOCK_INTERNAL;
  725. chip->output_clock = ECHO_CLOCK_WORD;
  726. chip->asic_loaded = FALSE;
  727. memset(chip->comm_page, 0, sizeof(struct comm_page));
  728. /* Init the comm page */
  729. chip->comm_page->comm_size =
  730. __constant_cpu_to_le32(sizeof(struct comm_page));
  731. chip->comm_page->handshake = 0xffffffff;
  732. chip->comm_page->midi_out_free_count =
  733. __constant_cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
  734. chip->comm_page->sample_rate = __constant_cpu_to_le32(44100);
  735. chip->sample_rate = 44100;
  736. /* Set line levels so we don't blast any inputs on startup */
  737. memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
  738. memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
  739. return 0;
  740. }
  741. /* This function initializes the several volume controls for busses and pipes.
  742. This MUST be called after the DSP is up and running ! */
  743. static int init_line_levels(struct echoaudio *chip)
  744. {
  745. int st, i, o;
  746. DE_INIT(("init_line_levels\n"));
  747. /* Mute output busses */
  748. for (i = 0; i < num_busses_out(chip); i++)
  749. if ((st = set_output_gain(chip, i, ECHOGAIN_MUTED)))
  750. return st;
  751. if ((st = update_output_line_level(chip)))
  752. return st;
  753. #ifdef ECHOCARD_HAS_VMIXER
  754. /* Mute the Vmixer */
  755. for (i = 0; i < num_pipes_out(chip); i++)
  756. for (o = 0; o < num_busses_out(chip); o++)
  757. if ((st = set_vmixer_gain(chip, o, i, ECHOGAIN_MUTED)))
  758. return st;
  759. if ((st = update_vmixer_level(chip)))
  760. return st;
  761. #endif /* ECHOCARD_HAS_VMIXER */
  762. #ifdef ECHOCARD_HAS_MONITOR
  763. /* Mute the monitor mixer */
  764. for (o = 0; o < num_busses_out(chip); o++)
  765. for (i = 0; i < num_busses_in(chip); i++)
  766. if ((st = set_monitor_gain(chip, o, i, ECHOGAIN_MUTED)))
  767. return st;
  768. if ((st = update_output_line_level(chip)))
  769. return st;
  770. #endif /* ECHOCARD_HAS_MONITOR */
  771. #ifdef ECHOCARD_HAS_INPUT_GAIN
  772. for (i = 0; i < num_busses_in(chip); i++)
  773. if ((st = set_input_gain(chip, i, ECHOGAIN_MUTED)))
  774. return st;
  775. if ((st = update_input_line_level(chip)))
  776. return st;
  777. #endif /* ECHOCARD_HAS_INPUT_GAIN */
  778. return 0;
  779. }
  780. /* This is low level part of the interrupt handler.
  781. It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
  782. of midi data in the input queue. */
  783. static int service_irq(struct echoaudio *chip)
  784. {
  785. int st;
  786. /* Read the DSP status register and see if this DSP generated this interrupt */
  787. if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
  788. st = 0;
  789. #ifdef ECHOCARD_HAS_MIDI
  790. /* Get and parse midi data if present */
  791. if (chip->comm_page->midi_input[0]) /* The count is at index 0 */
  792. st = midi_service_irq(chip); /* Returns how many midi bytes we received */
  793. #endif
  794. /* Clear the hardware interrupt */
  795. chip->comm_page->midi_input[0] = 0;
  796. send_vector(chip, DSP_VC_ACK_INT);
  797. return st;
  798. }
  799. return -1;
  800. }
  801. /******************************************************************************
  802. Functions for opening and closing pipes
  803. ******************************************************************************/
  804. /* allocate_pipes is used to reserve audio pipes for your exclusive use.
  805. The call will fail if some pipes are already allocated. */
  806. static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
  807. int pipe_index, int interleave)
  808. {
  809. int i;
  810. u32 channel_mask;
  811. char is_cyclic;
  812. DE_ACT(("allocate_pipes: ch=%d int=%d\n", pipe_index, interleave));
  813. if (chip->bad_board)
  814. return -EIO;
  815. is_cyclic = 1; /* This driver uses cyclic buffers only */
  816. for (channel_mask = i = 0; i < interleave; i++)
  817. channel_mask |= 1 << (pipe_index + i);
  818. if (chip->pipe_alloc_mask & channel_mask) {
  819. DE_ACT(("allocate_pipes: channel already open\n"));
  820. return -EAGAIN;
  821. }
  822. chip->comm_page->position[pipe_index] = 0;
  823. chip->pipe_alloc_mask |= channel_mask;
  824. if (is_cyclic)
  825. chip->pipe_cyclic_mask |= channel_mask;
  826. pipe->index = pipe_index;
  827. pipe->interleave = interleave;
  828. pipe->state = PIPE_STATE_STOPPED;
  829. /* The counter register is where the DSP writes the 32 bit DMA
  830. position for a pipe. The DSP is constantly updating this value as
  831. it moves data. The DMA counter is in units of bytes, not samples. */
  832. pipe->dma_counter = &chip->comm_page->position[pipe_index];
  833. *pipe->dma_counter = 0;
  834. DE_ACT(("allocate_pipes: ok\n"));
  835. return pipe_index;
  836. }
  837. static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
  838. {
  839. u32 channel_mask;
  840. int i;
  841. DE_ACT(("free_pipes: Pipe %d\n", pipe->index));
  842. snd_assert(is_pipe_allocated(chip, pipe->index), return -EINVAL);
  843. snd_assert(pipe->state == PIPE_STATE_STOPPED, return -EINVAL);
  844. for (channel_mask = i = 0; i < pipe->interleave; i++)
  845. channel_mask |= 1 << (pipe->index + i);
  846. chip->pipe_alloc_mask &= ~channel_mask;
  847. chip->pipe_cyclic_mask &= ~channel_mask;
  848. return 0;
  849. }
  850. /******************************************************************************
  851. Functions for managing the scatter-gather list
  852. ******************************************************************************/
  853. static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
  854. {
  855. pipe->sglist_head = 0;
  856. memset(pipe->sgpage.area, 0, PAGE_SIZE);
  857. chip->comm_page->sglist_addr[pipe->index].addr =
  858. cpu_to_le32(pipe->sgpage.addr);
  859. return 0;
  860. }
  861. static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
  862. dma_addr_t address, size_t length)
  863. {
  864. int head = pipe->sglist_head;
  865. struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
  866. if (head < MAX_SGLIST_ENTRIES - 1) {
  867. list[head].addr = cpu_to_le32(address);
  868. list[head].size = cpu_to_le32(length);
  869. pipe->sglist_head++;
  870. } else {
  871. DE_ACT(("SGlist: too many fragments\n"));
  872. return -ENOMEM;
  873. }
  874. return 0;
  875. }
  876. static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
  877. {
  878. return sglist_add_mapping(chip, pipe, 0, 0);
  879. }
  880. static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
  881. {
  882. return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
  883. }