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