xref: /linux/sound/drivers/vx/vx_core.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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 
25 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
26 MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
27 MODULE_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  */
39 int 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 
61 EXPORT_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  */
71 static 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  */
92 static 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  */
111 static 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  */
155 static 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  */
236 int 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  */
345 int vx_send_msg(struct vx_core *chip, struct vx_rmh *rmh)
346 {
347 	guard(mutex)(&chip->lock);
348 	return vx_send_msg_nolock(chip, rmh);
349 }
350 
351 
352 /*
353  * vx_send_rih_nolock - send an RIH to xilinx
354  * @cmd: the command to send
355  *
356  * returns 0 if successful, or a negative error code.
357  * the error code can be VX-specific, retrieved via vx_get_error().
358  *
359  * this function doesn't call mutex at all.
360  *
361  * unlike RMH, no command is sent to DSP.
362  */
363 int vx_send_rih_nolock(struct vx_core *chip, int cmd)
364 {
365 	int err;
366 
367 	if (chip->chip_status & VX_STAT_IS_STALE)
368 		return -EBUSY;
369 
370 	err = vx_reset_chk(chip);
371 	if (err < 0)
372 		return err;
373 	/* send the IRQ */
374 	err = vx_send_irq_dsp(chip, cmd);
375 	if (err < 0)
376 		return err;
377 	/* Wait CHK = 1 */
378 	err = vx_wait_isr_bit(chip, ISR_CHK);
379 	if (err < 0)
380 		return err;
381 	/* If error, read RX */
382 	if (vx_inb(chip, ISR) & ISR_ERR) {
383 		err = vx_wait_for_rx_full(chip);
384 		if (err < 0)
385 			return err;
386 		err = vx_inb(chip, RXH) << 16;
387 		err |= vx_inb(chip, RXM) << 8;
388 		err |= vx_inb(chip, RXL);
389 		return -(VX_ERR_MASK | err);
390 	}
391 	return 0;
392 }
393 
394 
395 /*
396  * vx_send_rih - send an RIH with mutex
397  * @cmd: the command to send
398  *
399  * see vx_send_rih_nolock().
400  */
401 int vx_send_rih(struct vx_core *chip, int cmd)
402 {
403 	guard(mutex)(&chip->lock);
404 	return vx_send_rih_nolock(chip, cmd);
405 }
406 
407 #define END_OF_RESET_WAIT_TIME		500	/* us */
408 
409 /**
410  * snd_vx_load_boot_image - boot up the xilinx interface
411  * @chip: VX core instance
412  * @boot: the boot record to load
413  */
414 int snd_vx_load_boot_image(struct vx_core *chip, const struct firmware *boot)
415 {
416 	unsigned int i;
417 	int no_fillup = vx_has_new_dsp(chip);
418 
419 	/* check the length of boot image */
420 	if (boot->size <= 0)
421 		return -EINVAL;
422 	if (boot->size % 3)
423 		return -EINVAL;
424 #if 0
425 	{
426 		/* more strict check */
427 		unsigned int c = ((u32)boot->data[0] << 16) | ((u32)boot->data[1] << 8) | boot->data[2];
428 		if (boot->size != (c + 2) * 3)
429 			return -EINVAL;
430 	}
431 #endif
432 
433 	/* reset dsp */
434 	vx_reset_dsp(chip);
435 
436 	udelay(END_OF_RESET_WAIT_TIME); /* another wait? */
437 
438 	/* download boot strap */
439 	for (i = 0; i < 0x600; i += 3) {
440 		if (i >= boot->size) {
441 			if (no_fillup)
442 				break;
443 			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
444 				dev_err(chip->card->dev, "dsp boot failed at %d\n", i);
445 				return -EIO;
446 			}
447 			vx_outb(chip, TXH, 0);
448 			vx_outb(chip, TXM, 0);
449 			vx_outb(chip, TXL, 0);
450 		} else {
451 			const unsigned char *image = boot->data + i;
452 			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
453 				dev_err(chip->card->dev, "dsp boot failed at %d\n", i);
454 				return -EIO;
455 			}
456 			vx_outb(chip, TXH, image[0]);
457 			vx_outb(chip, TXM, image[1]);
458 			vx_outb(chip, TXL, image[2]);
459 		}
460 	}
461 	return 0;
462 }
463 
464 EXPORT_SYMBOL(snd_vx_load_boot_image);
465 
466 /*
467  * vx_test_irq_src - query the source of interrupts
468  *
469  * called from irq handler only
470  */
471 static int vx_test_irq_src(struct vx_core *chip, unsigned int *ret)
472 {
473 	int err;
474 
475 	vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
476 	guard(mutex)(&chip->lock);
477 	err = vx_send_msg_nolock(chip, &chip->irq_rmh);
478 	if (err < 0)
479 		*ret = 0;
480 	else
481 		*ret = chip->irq_rmh.Stat[0];
482 	return err;
483 }
484 
485 
486 /*
487  * snd_vx_threaded_irq_handler - threaded irq handler
488  */
489 irqreturn_t snd_vx_threaded_irq_handler(int irq, void *dev)
490 {
491 	struct vx_core *chip = dev;
492 	unsigned int events;
493 
494 	if (chip->chip_status & VX_STAT_IS_STALE)
495 		return IRQ_HANDLED;
496 
497 	if (vx_test_irq_src(chip, &events) < 0)
498 		return IRQ_HANDLED;
499 
500 	/* We must prevent any application using this DSP
501 	 * and block any further request until the application
502 	 * either unregisters or reloads the DSP
503 	 */
504 	if (events & FATAL_DSP_ERROR) {
505 		dev_err(chip->card->dev, "vx_core: fatal DSP error!!\n");
506 		return IRQ_HANDLED;
507 	}
508 
509 	/* The start on time code conditions are filled (ie the time code
510 	 * received by the board is equal to one of those given to it).
511 	 */
512 	if (events & TIME_CODE_EVENT_PENDING) {
513 		; /* so far, nothing to do yet */
514 	}
515 
516 	/* The frequency has changed on the board (UER mode). */
517 	if (events & FREQUENCY_CHANGE_EVENT_PENDING)
518 		vx_change_frequency(chip);
519 
520 	/* update the pcm streams */
521 	vx_pcm_update_intr(chip, events);
522 	return IRQ_HANDLED;
523 }
524 EXPORT_SYMBOL(snd_vx_threaded_irq_handler);
525 
526 /**
527  * snd_vx_irq_handler - interrupt handler
528  * @irq: irq number
529  * @dev: VX core instance
530  */
531 irqreturn_t snd_vx_irq_handler(int irq, void *dev)
532 {
533 	struct vx_core *chip = dev;
534 
535 	if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
536 	    (chip->chip_status & VX_STAT_IS_STALE))
537 		return IRQ_NONE;
538 	if (! vx_test_and_ack(chip))
539 		return IRQ_WAKE_THREAD;
540 	return IRQ_NONE;
541 }
542 
543 EXPORT_SYMBOL(snd_vx_irq_handler);
544 
545 /*
546  */
547 static void vx_reset_board(struct vx_core *chip, int cold_reset)
548 {
549 	if (snd_BUG_ON(!chip->ops->reset_board))
550 		return;
551 
552 	/* current source, later sync'ed with target */
553 	chip->audio_source = VX_AUDIO_SRC_LINE;
554 	if (cold_reset) {
555 		chip->audio_source_target = chip->audio_source;
556 		chip->clock_source = INTERNAL_QUARTZ;
557 		chip->clock_mode = VX_CLOCK_MODE_AUTO;
558 		chip->freq = 48000;
559 		chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
560 		chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
561 	}
562 
563 	chip->ops->reset_board(chip, cold_reset);
564 
565 	vx_reset_codec(chip, cold_reset);
566 
567 	vx_set_internal_clock(chip, chip->freq);
568 
569 	/* Reset the DSP */
570 	vx_reset_dsp(chip);
571 
572 	if (vx_is_pcmcia(chip)) {
573 		/* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
574 		vx_test_and_ack(chip);
575 		vx_validate_irq(chip, 1);
576 	}
577 
578 	/* init CBits */
579 	vx_set_iec958_status(chip, chip->uer_bits);
580 }
581 
582 
583 /*
584  * proc interface
585  */
586 
587 static void vx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
588 {
589 	struct vx_core *chip = entry->private_data;
590 	static const char * const audio_src_vxp[] = { "Line", "Mic", "Digital" };
591 	static const char * const audio_src_vx2[] = { "Analog", "Analog", "Digital" };
592 	static const char * const clock_mode[] = { "Auto", "Internal", "External" };
593 	static const char * const clock_src[] = { "Internal", "External" };
594 	static const char * const uer_type[] = { "Consumer", "Professional", "Not Present" };
595 
596 	snd_iprintf(buffer, "%s\n", chip->card->longname);
597 	snd_iprintf(buffer, "Xilinx Firmware: %s\n",
598 		    (chip->chip_status & VX_STAT_XILINX_LOADED) ? "Loaded" : "No");
599 	snd_iprintf(buffer, "Device Initialized: %s\n",
600 		    (chip->chip_status & VX_STAT_DEVICE_INIT) ? "Yes" : "No");
601 	snd_iprintf(buffer, "DSP audio info:");
602 	if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
603 		snd_iprintf(buffer, " realtime");
604 	if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
605 		snd_iprintf(buffer, " offline");
606 	if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
607 		snd_iprintf(buffer, " mpeg1");
608 	if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
609 		snd_iprintf(buffer, " mpeg2");
610 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
611 		snd_iprintf(buffer, " linear8");
612 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
613 		snd_iprintf(buffer, " linear16");
614 	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
615 		snd_iprintf(buffer, " linear24");
616 	snd_iprintf(buffer, "\n");
617 	snd_iprintf(buffer, "Input Source: %s\n", vx_is_pcmcia(chip) ?
618 		    audio_src_vxp[chip->audio_source] :
619 		    audio_src_vx2[chip->audio_source]);
620 	snd_iprintf(buffer, "Clock Mode: %s\n", clock_mode[chip->clock_mode]);
621 	snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
622 	snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
623 	snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
624 	snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
625 	snd_iprintf(buffer, "Min/Max/Cur IBL: %d/%d/%d (granularity=%d)\n",
626 		    chip->ibl.min_size, chip->ibl.max_size, chip->ibl.size,
627 		    chip->ibl.granularity);
628 }
629 
630 static void vx_proc_init(struct vx_core *chip)
631 {
632 	snd_card_ro_proc_new(chip->card, "vx-status", chip, vx_proc_read);
633 }
634 
635 
636 /**
637  * snd_vx_dsp_boot - load the DSP boot
638  * @chip: VX core instance
639  * @boot: firmware data
640  */
641 int snd_vx_dsp_boot(struct vx_core *chip, const struct firmware *boot)
642 {
643 	int err;
644 	int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);
645 
646 	vx_reset_board(chip, cold_reset);
647 	vx_validate_irq(chip, 0);
648 
649 	err = snd_vx_load_boot_image(chip, boot);
650 	if (err < 0)
651 		return err;
652 	msleep(10);
653 
654 	return 0;
655 }
656 
657 EXPORT_SYMBOL(snd_vx_dsp_boot);
658 
659 /**
660  * snd_vx_dsp_load - load the DSP image
661  * @chip: VX core instance
662  * @dsp: firmware data
663  */
664 int snd_vx_dsp_load(struct vx_core *chip, const struct firmware *dsp)
665 {
666 	unsigned int i;
667 	int err;
668 	unsigned int csum = 0;
669 	const unsigned char *image, *cptr;
670 
671 	if (dsp->size % 3)
672 		return -EINVAL;
673 
674 	vx_toggle_dac_mute(chip, 1);
675 
676 	/* Transfert data buffer from PC to DSP */
677 	for (i = 0; i < dsp->size; i += 3) {
678 		image = dsp->data + i;
679 		/* Wait DSP ready for a new read */
680 		err = vx_wait_isr_bit(chip, ISR_TX_EMPTY);
681 		if (err < 0) {
682 			dev_err(chip->card->dev,
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 
698 	msleep(200);
699 
700 	err = vx_wait_isr_bit(chip, ISR_CHK);
701 	if (err < 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 
712 EXPORT_SYMBOL(snd_vx_dsp_load);
713 
714 #ifdef CONFIG_PM
715 /*
716  * suspend
717  */
718 int 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 
726 EXPORT_SYMBOL(snd_vx_suspend);
727 
728 /*
729  * resume
730  */
731 int 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 			dev_err(chip->card->dev,
743 				"vx: firmware resume error at DSP %d\n", i);
744 			return -EIO;
745 		}
746 	}
747 
748 	chip->chip_status |= VX_STAT_CHIP_INIT;
749 	chip->chip_status &= ~VX_STAT_IN_SUSPEND;
750 
751 	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
752 	return 0;
753 }
754 
755 EXPORT_SYMBOL(snd_vx_resume);
756 #endif
757 
758 static void snd_vx_release(struct device *dev, void *data)
759 {
760 	snd_vx_free_firmware(data);
761 }
762 
763 /**
764  * snd_vx_create - constructor for struct vx_core
765  * @card: card instance
766  * @hw: hardware specific record
767  * @ops: VX ops pointer
768  * @extra_size: extra byte size to allocate appending to chip
769  *
770  * this function allocates the instance and prepare for the hardware
771  * initialization.
772  *
773  * The object is managed via devres, and will be automatically released.
774  *
775  * return the instance pointer if successful, NULL in error.
776  */
777 struct vx_core *snd_vx_create(struct snd_card *card,
778 			      const struct snd_vx_hardware *hw,
779 			      const struct snd_vx_ops *ops,
780 			      int extra_size)
781 {
782 	struct vx_core *chip;
783 
784 	if (snd_BUG_ON(!card || !hw || !ops))
785 		return NULL;
786 
787 	chip = devres_alloc(snd_vx_release, sizeof(*chip) + extra_size,
788 			    GFP_KERNEL);
789 	if (!chip)
790 		return NULL;
791 	mutex_init(&chip->lock);
792 	chip->irq = -1;
793 	chip->hw = hw;
794 	chip->type = hw->type;
795 	chip->ops = ops;
796 	mutex_init(&chip->mixer_mutex);
797 
798 	chip->card = card;
799 	card->private_data = chip;
800 	strscpy(card->driver, hw->name);
801 	sprintf(card->shortname, "Digigram %s", hw->name);
802 
803 	vx_proc_init(chip);
804 
805 	return chip;
806 }
807 
808 EXPORT_SYMBOL(snd_vx_create);
809