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