Linux Audio

Check our new training course

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