Linux Audio

Check our new training course

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