1 /*
2  * Driver for Digigram VX soundcards
3  *
4  * Hardware core part
5  *
6  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
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 
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/device.h>
28 #include <linux/firmware.h>
29 #include <linux/module.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/asoundef.h>
33 #include <sound/info.h>
34 #include <asm/io.h>
35 #include <sound/vx_core.h>
36 #include "vx_cmd.h"
37 
38 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
39 MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
40 MODULE_LICENSE("GPL");
41 
42 
43 /*
44  * vx_check_reg_bit - wait for the specified bit is set/reset on a register
45  * @reg: register to check
46  * @mask: bit mask
47  * @bit: resultant bit to be checked
48  * @time: time-out of loop in msec
49  *
50  * returns zero if a bit matches, or a negative error code.
51  */
snd_vx_check_reg_bit(struct vx_core * chip,int reg,int mask,int bit,int time)52 int snd_vx_check_reg_bit(struct vx_core *chip, int reg, int mask, int bit, int time)
53 {
54 	unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
55 #ifdef CONFIG_SND_DEBUG
56 	static char *reg_names[VX_REG_MAX] = {
57 		"ICR", "CVR", "ISR", "IVR", "RXH", "RXM", "RXL",
58 		"DMA", "CDSP", "RFREQ", "RUER/V2", "DATA", "MEMIRQ",
59 		"ACQ", "BIT0", "BIT1", "MIC0", "MIC1", "MIC2",
60 		"MIC3", "INTCSR", "CNTRL", "GPIOC",
61 		"LOFREQ", "HIFREQ", "CSUER", "RUER"
62 	};
63 #endif
64 	do {
65 		if ((snd_vx_inb(chip, reg) & mask) == bit)
66 			return 0;
67 		//msleep(10);
68 	} while (time_after_eq(end_time, jiffies));
69 	snd_printd(KERN_DEBUG "vx_check_reg_bit: timeout, reg=%s, mask=0x%x, val=0x%x\n", reg_names[reg], mask, snd_vx_inb(chip, reg));
70 	return -EIO;
71 }
72 
73 EXPORT_SYMBOL(snd_vx_check_reg_bit);
74 
75 /*
76  * vx_send_irq_dsp - set command irq bit
77  * @num: the requested IRQ type, IRQ_XXX
78  *
79  * this triggers the specified IRQ request
80  * returns 0 if successful, or a negative error code.
81  *
82  */
vx_send_irq_dsp(struct vx_core * chip,int num)83 static int vx_send_irq_dsp(struct vx_core *chip, int num)
84 {
85 	int nirq;
86 
87 	/* wait for Hc = 0 */
88 	if (snd_vx_check_reg_bit(chip, VX_CVR, CVR_HC, 0, 200) < 0)
89 		return -EIO;
90 
91 	nirq = num;
92 	if (vx_has_new_dsp(chip))
93 		nirq += VXP_IRQ_OFFSET;
94 	vx_outb(chip, CVR, (nirq >> 1) | CVR_HC);
95 	return 0;
96 }
97 
98 
99 /*
100  * vx_reset_chk - reset CHK bit on ISR
101  *
102  * returns 0 if successful, or a negative error code.
103  */
vx_reset_chk(struct vx_core * chip)104 static int vx_reset_chk(struct vx_core *chip)
105 {
106 	/* Reset irq CHK */
107 	if (vx_send_irq_dsp(chip, IRQ_RESET_CHK) < 0)
108 		return -EIO;
109 	/* Wait until CHK = 0 */
110 	if (vx_check_isr(chip, ISR_CHK, 0, 200) < 0)
111 		return -EIO;
112 	return 0;
113 }
114 
115 /*
116  * vx_transfer_end - terminate message transfer
117  * @cmd: IRQ message to send (IRQ_MESS_XXX_END)
118  *
119  * returns 0 if successful, or a negative error code.
120  * the error code can be VX-specific, retrieved via vx_get_error().
121  * NB: call with spinlock held!
122  */
vx_transfer_end(struct vx_core * chip,int cmd)123 static int vx_transfer_end(struct vx_core *chip, int cmd)
124 {
125 	int err;
126 
127 	if ((err = vx_reset_chk(chip)) < 0)
128 		return err;
129 
130 	/* irq MESS_READ/WRITE_END */
131 	if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
132 		return err;
133 
134 	/* Wait CHK = 1 */
135 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
136 		return err;
137 
138 	/* If error, Read RX */
139 	if ((err = vx_inb(chip, ISR)) & ISR_ERR) {
140 		if ((err = vx_wait_for_rx_full(chip)) < 0) {
141 			snd_printd(KERN_DEBUG "transfer_end: error in rx_full\n");
142 			return err;
143 		}
144 		err = vx_inb(chip, RXH) << 16;
145 		err |= vx_inb(chip, RXM) << 8;
146 		err |= vx_inb(chip, RXL);
147 		snd_printd(KERN_DEBUG "transfer_end: error = 0x%x\n", err);
148 		return -(VX_ERR_MASK | err);
149 	}
150 	return 0;
151 }
152 
153 /*
154  * vx_read_status - return the status rmh
155  * @rmh: rmh record to store the status
156  *
157  * returns 0 if successful, or a negative error code.
158  * the error code can be VX-specific, retrieved via vx_get_error().
159  * NB: call with spinlock held!
160  */
vx_read_status(struct vx_core * chip,struct vx_rmh * rmh)161 static int vx_read_status(struct vx_core *chip, struct vx_rmh *rmh)
162 {
163 	int i, err, val, size;
164 
165 	/* no read necessary? */
166 	if (rmh->DspStat == RMH_SSIZE_FIXED && rmh->LgStat == 0)
167 		return 0;
168 
169 	/* Wait for RX full (with timeout protection)
170 	 * The first word of status is in RX
171 	 */
172 	err = vx_wait_for_rx_full(chip);
173 	if (err < 0)
174 		return err;
175 
176 	/* Read RX */
177 	val = vx_inb(chip, RXH) << 16;
178 	val |= vx_inb(chip, RXM) << 8;
179 	val |= vx_inb(chip, RXL);
180 
181 	/* If status given by DSP, let's decode its size */
182 	switch (rmh->DspStat) {
183 	case RMH_SSIZE_ARG:
184 		size = val & 0xff;
185 		rmh->Stat[0] = val & 0xffff00;
186 		rmh->LgStat = size + 1;
187 		break;
188 	case RMH_SSIZE_MASK:
189 		/* Let's count the arg numbers from a mask */
190 		rmh->Stat[0] = val;
191 		size = 0;
192 		while (val) {
193 			if (val & 0x01)
194 				size++;
195 			val >>= 1;
196 		}
197 		rmh->LgStat = size + 1;
198 		break;
199 	default:
200 		/* else retrieve the status length given by the driver */
201 		size = rmh->LgStat;
202 		rmh->Stat[0] = val;  /* Val is the status 1st word */
203 		size--;              /* hence adjust remaining length */
204 		break;
205         }
206 
207 	if (size < 1)
208 		return 0;
209 	if (snd_BUG_ON(size > SIZE_MAX_STATUS))
210 		return -EINVAL;
211 
212 	for (i = 1; i <= size; i++) {
213 		/* trigger an irq MESS_WRITE_NEXT */
214 		err = vx_send_irq_dsp(chip, IRQ_MESS_WRITE_NEXT);
215 		if (err < 0)
216 			return err;
217 		/* Wait for RX full (with timeout protection) */
218 		err = vx_wait_for_rx_full(chip);
219 		if (err < 0)
220 			return err;
221 		rmh->Stat[i] = vx_inb(chip, RXH) << 16;
222 		rmh->Stat[i] |= vx_inb(chip, RXM) <<  8;
223 		rmh->Stat[i] |= vx_inb(chip, RXL);
224 	}
225 
226 	return vx_transfer_end(chip, IRQ_MESS_WRITE_END);
227 }
228 
229 
230 #define MASK_MORE_THAN_1_WORD_COMMAND   0x00008000
231 #define MASK_1_WORD_COMMAND             0x00ff7fff
232 
233 /*
234  * vx_send_msg_nolock - send a DSP message and read back the status
235  * @rmh: the rmh record to send and receive
236  *
237  * returns 0 if successful, or a negative error code.
238  * the error code can be VX-specific, retrieved via vx_get_error().
239  *
240  * this function doesn't call spinlock at all.
241  */
vx_send_msg_nolock(struct vx_core * chip,struct vx_rmh * rmh)242 int vx_send_msg_nolock(struct vx_core *chip, struct vx_rmh *rmh)
243 {
244 	int i, err;
245 
246 	if (chip->chip_status & VX_STAT_IS_STALE)
247 		return -EBUSY;
248 
249 	if ((err = vx_reset_chk(chip)) < 0) {
250 		snd_printd(KERN_DEBUG "vx_send_msg: vx_reset_chk error\n");
251 		return err;
252 	}
253 
254 #if 0
255 	printk(KERN_DEBUG "rmh: cmd = 0x%06x, length = %d, stype = %d\n",
256 	       rmh->Cmd[0], rmh->LgCmd, rmh->DspStat);
257 	if (rmh->LgCmd > 1) {
258 		printk(KERN_DEBUG "  ");
259 		for (i = 1; i < rmh->LgCmd; i++)
260 			printk("0x%06x ", rmh->Cmd[i]);
261 		printk("\n");
262 	}
263 #endif
264 	/* Check bit M is set according to length of the command */
265 	if (rmh->LgCmd > 1)
266 		rmh->Cmd[0] |= MASK_MORE_THAN_1_WORD_COMMAND;
267 	else
268 		rmh->Cmd[0] &= MASK_1_WORD_COMMAND;
269 
270 	/* Wait for TX empty */
271 	if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
272 		snd_printd(KERN_DEBUG "vx_send_msg: wait tx empty error\n");
273 		return err;
274 	}
275 
276 	/* Write Cmd[0] */
277 	vx_outb(chip, TXH, (rmh->Cmd[0] >> 16) & 0xff);
278 	vx_outb(chip, TXM, (rmh->Cmd[0] >> 8) & 0xff);
279 	vx_outb(chip, TXL, rmh->Cmd[0] & 0xff);
280 
281 	/* Trigger irq MESSAGE */
282 	if ((err = vx_send_irq_dsp(chip, IRQ_MESSAGE)) < 0) {
283 		snd_printd(KERN_DEBUG "vx_send_msg: send IRQ_MESSAGE error\n");
284 		return err;
285 	}
286 
287 	/* Wait for CHK = 1 */
288 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
289 		return err;
290 
291 	/* If error, get error value from RX */
292 	if (vx_inb(chip, ISR) & ISR_ERR) {
293 		if ((err = vx_wait_for_rx_full(chip)) < 0) {
294 			snd_printd(KERN_DEBUG "vx_send_msg: rx_full read error\n");
295 			return err;
296 		}
297 		err = vx_inb(chip, RXH) << 16;
298 		err |= vx_inb(chip, RXM) << 8;
299 		err |= vx_inb(chip, RXL);
300 		snd_printd(KERN_DEBUG "msg got error = 0x%x at cmd[0]\n", err);
301 		err = -(VX_ERR_MASK | err);
302 		return err;
303 	}
304 
305 	/* Send the other words */
306 	if (rmh->LgCmd > 1) {
307 		for (i = 1; i < rmh->LgCmd; i++) {
308 			/* Wait for TX ready */
309 			if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
310 				snd_printd(KERN_DEBUG "vx_send_msg: tx_ready error\n");
311 				return err;
312 			}
313 
314 			/* Write Cmd[i] */
315 			vx_outb(chip, TXH, (rmh->Cmd[i] >> 16) & 0xff);
316 			vx_outb(chip, TXM, (rmh->Cmd[i] >> 8) & 0xff);
317 			vx_outb(chip, TXL, rmh->Cmd[i] & 0xff);
318 
319 			/* Trigger irq MESS_READ_NEXT */
320 			if ((err = vx_send_irq_dsp(chip, IRQ_MESS_READ_NEXT)) < 0) {
321 				snd_printd(KERN_DEBUG "vx_send_msg: IRQ_READ_NEXT error\n");
322 				return err;
323 			}
324 		}
325 		/* Wait for TX empty */
326 		if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
327 			snd_printd(KERN_DEBUG "vx_send_msg: TX_READY error\n");
328 			return err;
329 		}
330 		/* End of transfer */
331 		err = vx_transfer_end(chip, IRQ_MESS_READ_END);
332 		if (err < 0)
333 			return err;
334 	}
335 
336 	return vx_read_status(chip, rmh);
337 }
338 
339 
340 /*
341  * vx_send_msg - send a DSP message with spinlock
342  * @rmh: the rmh record to send and receive
343  *
344  * returns 0 if successful, or a negative error code.
345  * see vx_send_msg_nolock().
346  */
vx_send_msg(struct vx_core * chip,struct vx_rmh * rmh)347 int vx_send_msg(struct vx_core *chip, struct vx_rmh *rmh)
348 {
349 	unsigned long flags;
350 	int err;
351 
352 	spin_lock_irqsave(&chip->lock, flags);
353 	err = vx_send_msg_nolock(chip, rmh);
354 	spin_unlock_irqrestore(&chip->lock, flags);
355 	return err;
356 }
357 
358 
359 /*
360  * vx_send_rih_nolock - send an RIH to xilinx
361  * @cmd: the command to send
362  *
363  * returns 0 if successful, or a negative error code.
364  * the error code can be VX-specific, retrieved via vx_get_error().
365  *
366  * this function doesn't call spinlock at all.
367  *
368  * unlike RMH, no command is sent to DSP.
369  */
vx_send_rih_nolock(struct vx_core * chip,int cmd)370 int vx_send_rih_nolock(struct vx_core *chip, int cmd)
371 {
372 	int err;
373 
374 	if (chip->chip_status & VX_STAT_IS_STALE)
375 		return -EBUSY;
376 
377 #if 0
378 	printk(KERN_DEBUG "send_rih: cmd = 0x%x\n", cmd);
379 #endif
380 	if ((err = vx_reset_chk(chip)) < 0)
381 		return err;
382 	/* send the IRQ */
383 	if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
384 		return err;
385 	/* Wait CHK = 1 */
386 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
387 		return err;
388 	/* If error, read RX */
389 	if (vx_inb(chip, ISR) & ISR_ERR) {
390 		if ((err = vx_wait_for_rx_full(chip)) < 0)
391 			return err;
392 		err = vx_inb(chip, RXH) << 16;
393 		err |= vx_inb(chip, RXM) << 8;
394 		err |= vx_inb(chip, RXL);
395 		return -(VX_ERR_MASK | err);
396 	}
397 	return 0;
398 }
399 
400 
401 /*
402  * vx_send_rih - send an RIH with spinlock
403  * @cmd: the command to send
404  *
405  * see vx_send_rih_nolock().
406  */
vx_send_rih(struct vx_core * chip,int cmd)407 int vx_send_rih(struct vx_core *chip, int cmd)
408 {
409 	unsigned long flags;
410 	int err;
411 
412 	spin_lock_irqsave(&chip->lock, flags);
413 	err = vx_send_rih_nolock(chip, cmd);
414 	spin_unlock_irqrestore(&chip->lock, flags);
415 	return err;
416 }
417 
418 #define END_OF_RESET_WAIT_TIME		500	/* us */
419 
420 /**
421  * snd_vx_boot_xilinx - boot up the xilinx interface
422  * @boot: the boot record to load
423  */
snd_vx_load_boot_image(struct vx_core * chip,const struct firmware * boot)424 int snd_vx_load_boot_image(struct vx_core *chip, const struct firmware *boot)
425 {
426 	unsigned int i;
427 	int no_fillup = vx_has_new_dsp(chip);
428 
429 	/* check the length of boot image */
430 	if (boot->size <= 0)
431 		return -EINVAL;
432 	if (boot->size % 3)
433 		return -EINVAL;
434 #if 0
435 	{
436 		/* more strict check */
437 		unsigned int c = ((u32)boot->data[0] << 16) | ((u32)boot->data[1] << 8) | boot->data[2];
438 		if (boot->size != (c + 2) * 3)
439 			return -EINVAL;
440 	}
441 #endif
442 
443 	/* reset dsp */
444 	vx_reset_dsp(chip);
445 
446 	udelay(END_OF_RESET_WAIT_TIME); /* another wait? */
447 
448 	/* download boot strap */
449 	for (i = 0; i < 0x600; i += 3) {
450 		if (i >= boot->size) {
451 			if (no_fillup)
452 				break;
453 			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
454 				snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
455 				return -EIO;
456 			}
457 			vx_outb(chip, TXH, 0);
458 			vx_outb(chip, TXM, 0);
459 			vx_outb(chip, TXL, 0);
460 		} else {
461 			const unsigned char *image = boot->data + i;
462 			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
463 				snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
464 				return -EIO;
465 			}
466 			vx_outb(chip, TXH, image[0]);
467 			vx_outb(chip, TXM, image[1]);
468 			vx_outb(chip, TXL, image[2]);
469 		}
470 	}
471 	return 0;
472 }
473 
474 EXPORT_SYMBOL(snd_vx_load_boot_image);
475 
476 /*
477  * vx_test_irq_src - query the source of interrupts
478  *
479  * called from irq handler only
480  */
vx_test_irq_src(struct vx_core * chip,unsigned int * ret)481 static int vx_test_irq_src(struct vx_core *chip, unsigned int *ret)
482 {
483 	int err;
484 
485 	vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
486 	spin_lock(&chip->lock);
487 	err = vx_send_msg_nolock(chip, &chip->irq_rmh);
488 	if (err < 0)
489 		*ret = 0;
490 	else
491 		*ret = chip->irq_rmh.Stat[0];
492 	spin_unlock(&chip->lock);
493 	return err;
494 }
495 
496 
497 /*
498  * vx_interrupt - soft irq handler
499  */
vx_interrupt(unsigned long private_data)500 static void vx_interrupt(unsigned long private_data)
501 {
502 	struct vx_core *chip = (struct vx_core *) private_data;
503 	unsigned int events;
504 
505 	if (chip->chip_status & VX_STAT_IS_STALE)
506 		return;
507 
508 	if (vx_test_irq_src(chip, &events) < 0)
509 		return;
510 
511 #if 0
512 	if (events & 0x000800)
513 		printk(KERN_ERR "DSP Stream underrun ! IRQ events = 0x%x\n", events);
514 #endif
515 	// printk(KERN_DEBUG "IRQ events = 0x%x\n", events);
516 
517 	/* We must prevent any application using this DSP
518 	 * and block any further request until the application
519 	 * either unregisters or reloads the DSP
520 	 */
521 	if (events & FATAL_DSP_ERROR) {
522 		snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
523 		return;
524 	}
525 
526 	/* The start on time code conditions are filled (ie the time code
527 	 * received by the board is equal to one of those given to it).
528 	 */
529 	if (events & TIME_CODE_EVENT_PENDING)
530 		; /* so far, nothing to do yet */
531 
532 	/* The frequency has changed on the board (UER mode). */
533 	if (events & FREQUENCY_CHANGE_EVENT_PENDING)
534 		vx_change_frequency(chip);
535 
536 	/* update the pcm streams */
537 	vx_pcm_update_intr(chip, events);
538 }
539 
540 
541 /**
542  * snd_vx_irq_handler - interrupt handler
543  */
snd_vx_irq_handler(int irq,void * dev)544 irqreturn_t snd_vx_irq_handler(int irq, void *dev)
545 {
546 	struct vx_core *chip = dev;
547 
548 	if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
549 	    (chip->chip_status & VX_STAT_IS_STALE))
550 		return IRQ_NONE;
551 	if (! vx_test_and_ack(chip))
552 		tasklet_schedule(&chip->tq);
553 	return IRQ_HANDLED;
554 }
555 
556 EXPORT_SYMBOL(snd_vx_irq_handler);
557 
558 /*
559  */
vx_reset_board(struct vx_core * chip,int cold_reset)560 static void vx_reset_board(struct vx_core *chip, int cold_reset)
561 {
562 	if (snd_BUG_ON(!chip->ops->reset_board))
563 		return;
564 
565 	/* current source, later sync'ed with target */
566 	chip->audio_source = VX_AUDIO_SRC_LINE;
567 	if (cold_reset) {
568 		chip->audio_source_target = chip->audio_source;
569 		chip->clock_source = INTERNAL_QUARTZ;
570 		chip->clock_mode = VX_CLOCK_MODE_AUTO;
571 		chip->freq = 48000;
572 		chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
573 		chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
574 	}
575 
576 	chip->ops->reset_board(chip, cold_reset);
577 
578 	vx_reset_codec(chip, cold_reset);
579 
580 	vx_set_internal_clock(chip, chip->freq);
581 
582 	/* Reset the DSP */
583 	vx_reset_dsp(chip);
584 
585 	if (vx_is_pcmcia(chip)) {
586 		/* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
587 		vx_test_and_ack(chip);
588 		vx_validate_irq(chip, 1);
589 	}
590 
591 	/* init CBits */
592 	vx_set_iec958_status(chip, chip->uer_bits);
593 }
594 
595 
596 /*
597  * proc interface
598  */
599 
vx_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)600 static void vx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
601 {
602 	struct vx_core *chip = entry->private_data;
603 	static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
604 	static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
605 	static char *clock_mode[] = { "Auto", "Internal", "External" };
606 	static char *clock_src[] = { "Internal", "External" };
607 	static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
608 
609 	snd_iprintf(buffer, "%s\n", chip->card->longname);
610 	snd_iprintf(buffer, "Xilinx Firmware: %s\n",
611 		    chip->chip_status & VX_STAT_XILINX_LOADED ? "Loaded" : "No");
612 	snd_iprintf(buffer, "Device Initialized: %s\n",
613 		    chip->chip_status & VX_STAT_DEVICE_INIT ? "Yes" : "No");
614 	snd_iprintf(buffer, "DSP audio info:");
615 	if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
616 		snd_iprintf(buffer, " realtime");
617 	if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
618 		snd_iprintf(buffer, " offline");
619 	if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
620 		snd_iprintf(buffer, " mpeg1");
621 	if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
622 		snd_iprintf(buffer, " mpeg2");
623 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
624 		snd_iprintf(buffer, " linear8");
625 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
626 		snd_iprintf(buffer, " linear16");
627 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
628 		snd_iprintf(buffer, " linear24");
629 	snd_iprintf(buffer, "\n");
630 	snd_iprintf(buffer, "Input Source: %s\n", vx_is_pcmcia(chip) ?
631 		    audio_src_vxp[chip->audio_source] :
632 		    audio_src_vx2[chip->audio_source]);
633 	snd_iprintf(buffer, "Clock Mode: %s\n", clock_mode[chip->clock_mode]);
634 	snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
635 	snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
636 	snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
637 	snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
638 	snd_iprintf(buffer, "Min/Max/Cur IBL: %d/%d/%d (granularity=%d)\n",
639 		    chip->ibl.min_size, chip->ibl.max_size, chip->ibl.size,
640 		    chip->ibl.granularity);
641 }
642 
vx_proc_init(struct vx_core * chip)643 static void vx_proc_init(struct vx_core *chip)
644 {
645 	struct snd_info_entry *entry;
646 
647 	if (! snd_card_proc_new(chip->card, "vx-status", &entry))
648 		snd_info_set_text_ops(entry, chip, vx_proc_read);
649 }
650 
651 
652 /**
653  * snd_vx_dsp_boot - load the DSP boot
654  */
snd_vx_dsp_boot(struct vx_core * chip,const struct firmware * boot)655 int snd_vx_dsp_boot(struct vx_core *chip, const struct firmware *boot)
656 {
657 	int err;
658 	int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);
659 
660 	vx_reset_board(chip, cold_reset);
661 	vx_validate_irq(chip, 0);
662 
663 	if ((err = snd_vx_load_boot_image(chip, boot)) < 0)
664 		return err;
665 	msleep(10);
666 
667 	return 0;
668 }
669 
670 EXPORT_SYMBOL(snd_vx_dsp_boot);
671 
672 /**
673  * snd_vx_dsp_load - load the DSP image
674  */
snd_vx_dsp_load(struct vx_core * chip,const struct firmware * dsp)675 int snd_vx_dsp_load(struct vx_core *chip, const struct firmware *dsp)
676 {
677 	unsigned int i;
678 	int err;
679 	unsigned int csum = 0;
680 	const unsigned char *image, *cptr;
681 
682 	if (dsp->size % 3)
683 		return -EINVAL;
684 
685 	vx_toggle_dac_mute(chip, 1);
686 
687 	/* Transfert data buffer from PC to DSP */
688 	for (i = 0; i < dsp->size; i += 3) {
689 		image = dsp->data + i;
690 		/* Wait DSP ready for a new read */
691 		if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
692 			printk(KERN_ERR
693 			       "dsp loading error at position %d\n", i);
694 			return err;
695 		}
696 		cptr = image;
697 		csum ^= *cptr;
698 		csum = (csum >> 24) | (csum << 8);
699 		vx_outb(chip, TXH, *cptr++);
700 		csum ^= *cptr;
701 		csum = (csum >> 24) | (csum << 8);
702 		vx_outb(chip, TXM, *cptr++);
703 		csum ^= *cptr;
704 		csum = (csum >> 24) | (csum << 8);
705 		vx_outb(chip, TXL, *cptr++);
706 	}
707 	snd_printdd(KERN_DEBUG "checksum = 0x%08x\n", csum);
708 
709 	msleep(200);
710 
711 	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
712 		return err;
713 
714 	vx_toggle_dac_mute(chip, 0);
715 
716 	vx_test_and_ack(chip);
717 	vx_validate_irq(chip, 1);
718 
719 	return 0;
720 }
721 
722 EXPORT_SYMBOL(snd_vx_dsp_load);
723 
724 #ifdef CONFIG_PM
725 /*
726  * suspend
727  */
snd_vx_suspend(struct vx_core * chip,pm_message_t state)728 int snd_vx_suspend(struct vx_core *chip, pm_message_t state)
729 {
730 	unsigned int i;
731 
732 	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
733 	chip->chip_status |= VX_STAT_IN_SUSPEND;
734 	for (i = 0; i < chip->hw->num_codecs; i++)
735 		snd_pcm_suspend_all(chip->pcm[i]);
736 
737 	return 0;
738 }
739 
740 EXPORT_SYMBOL(snd_vx_suspend);
741 
742 /*
743  * resume
744  */
snd_vx_resume(struct vx_core * chip)745 int snd_vx_resume(struct vx_core *chip)
746 {
747 	int i, err;
748 
749 	chip->chip_status &= ~VX_STAT_CHIP_INIT;
750 
751 	for (i = 0; i < 4; i++) {
752 		if (! chip->firmware[i])
753 			continue;
754 		err = chip->ops->load_dsp(chip, i, chip->firmware[i]);
755 		if (err < 0) {
756 			snd_printk(KERN_ERR "vx: firmware resume error at DSP %d\n", i);
757 			return -EIO;
758 		}
759 	}
760 
761 	chip->chip_status |= VX_STAT_CHIP_INIT;
762 	chip->chip_status &= ~VX_STAT_IN_SUSPEND;
763 
764 	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
765 	return 0;
766 }
767 
768 EXPORT_SYMBOL(snd_vx_resume);
769 #endif
770 
771 /**
772  * snd_vx_create - constructor for struct vx_core
773  * @hw: hardware specific record
774  *
775  * this function allocates the instance and prepare for the hardware
776  * initialization.
777  *
778  * return the instance pointer if successful, NULL in error.
779  */
snd_vx_create(struct snd_card * card,struct snd_vx_hardware * hw,struct snd_vx_ops * ops,int extra_size)780 struct vx_core *snd_vx_create(struct snd_card *card, struct snd_vx_hardware *hw,
781 			      struct snd_vx_ops *ops,
782 			      int extra_size)
783 {
784 	struct vx_core *chip;
785 
786 	if (snd_BUG_ON(!card || !hw || !ops))
787 		return NULL;
788 
789 	chip = kzalloc(sizeof(*chip) + extra_size, GFP_KERNEL);
790 	if (! chip) {
791 		snd_printk(KERN_ERR "vx_core: no memory\n");
792 		return NULL;
793 	}
794 	spin_lock_init(&chip->lock);
795 	spin_lock_init(&chip->irq_lock);
796 	chip->irq = -1;
797 	chip->hw = hw;
798 	chip->type = hw->type;
799 	chip->ops = ops;
800 	tasklet_init(&chip->tq, vx_interrupt, (unsigned long)chip);
801 	mutex_init(&chip->mixer_mutex);
802 
803 	chip->card = card;
804 	card->private_data = chip;
805 	strcpy(card->driver, hw->name);
806 	sprintf(card->shortname, "Digigram %s", hw->name);
807 
808 	vx_proc_init(chip);
809 
810 	return chip;
811 }
812 
813 EXPORT_SYMBOL(snd_vx_create);
814 
815 /*
816  * module entries
817  */
alsa_vx_core_init(void)818 static int __init alsa_vx_core_init(void)
819 {
820 	return 0;
821 }
822 
alsa_vx_core_exit(void)823 static void __exit alsa_vx_core_exit(void)
824 {
825 }
826 
827 module_init(alsa_vx_core_init)
828 module_exit(alsa_vx_core_exit)
829