xref: /openbmc/qemu/hw/audio/es1370.c (revision a68694cd)
1 /*
2  * QEMU ES1370 emulation
3  *
4  * Copyright (c) 2005 Vassili Karpov (malc)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /* #define DEBUG_ES1370 */
26 /* #define VERBOSE_ES1370 */
27 #define SILENT_ES1370
28 
29 #include "qemu/osdep.h"
30 #include "hw/audio/soundhw.h"
31 #include "audio/audio.h"
32 #include "hw/pci/pci.h"
33 #include "migration/vmstate.h"
34 #include "qemu/module.h"
35 #include "sysemu/dma.h"
36 #include "qom/object.h"
37 
38 /* Missing stuff:
39    SCTRL_P[12](END|ST)INC
40    SCTRL_P1SCTRLD
41    SCTRL_P2DACSEN
42    CTRL_DAC_SYNC
43    MIDI
44    non looped mode
45    surely more
46 */
47 
48 /*
49   Following macros and samplerate array were copied verbatim from
50   Linux kernel 2.4.30: drivers/sound/es1370.c
51 
52   Copyright (C) 1998-2001, 2003 Thomas Sailer (t.sailer@alumni.ethz.ch)
53 */
54 
55 /* Start blatant GPL violation */
56 
57 #define ES1370_REG_CONTROL        0x00
58 #define ES1370_REG_STATUS         0x04
59 #define ES1370_REG_UART_DATA      0x08
60 #define ES1370_REG_UART_STATUS    0x09
61 #define ES1370_REG_UART_CONTROL   0x09
62 #define ES1370_REG_UART_TEST      0x0a
63 #define ES1370_REG_MEMPAGE        0x0c
64 #define ES1370_REG_CODEC          0x10
65 #define ES1370_REG_SERIAL_CONTROL 0x20
66 #define ES1370_REG_DAC1_SCOUNT    0x24
67 #define ES1370_REG_DAC2_SCOUNT    0x28
68 #define ES1370_REG_ADC_SCOUNT     0x2c
69 
70 #define ES1370_REG_DAC1_FRAMEADR    0xc30
71 #define ES1370_REG_DAC1_FRAMECNT    0xc34
72 #define ES1370_REG_DAC2_FRAMEADR    0xc38
73 #define ES1370_REG_DAC2_FRAMECNT    0xc3c
74 #define ES1370_REG_ADC_FRAMEADR     0xd30
75 #define ES1370_REG_ADC_FRAMECNT     0xd34
76 #define ES1370_REG_PHANTOM_FRAMEADR 0xd38
77 #define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
78 
79 static const unsigned dac1_samplerate[] = { 5512, 11025, 22050, 44100 };
80 
81 #define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
82 #define DAC2_DIVTOSR(x) (1411200/((x)+2))
83 
84 #define CTRL_ADC_STOP   0x80000000  /* 1 = ADC stopped */
85 #define CTRL_XCTL1      0x40000000  /* electret mic bias */
86 #define CTRL_OPEN       0x20000000  /* no function, can be read and written */
87 #define CTRL_PCLKDIV    0x1fff0000  /* ADC/DAC2 clock divider */
88 #define CTRL_SH_PCLKDIV 16
89 #define CTRL_MSFMTSEL   0x00008000  /* MPEG serial data fmt: 0 = Sony, 1 = I2S */
90 #define CTRL_M_SBB      0x00004000  /* DAC2 clock: 0 = PCLKDIV, 1 = MPEG */
91 #define CTRL_WTSRSEL    0x00003000  /* DAC1 clock freq: 0=5512, 1=11025, 2=22050, 3=44100 */
92 #define CTRL_SH_WTSRSEL 12
93 #define CTRL_DAC_SYNC   0x00000800  /* 1 = DAC2 runs off DAC1 clock */
94 #define CTRL_CCB_INTRM  0x00000400  /* 1 = CCB "voice" ints enabled */
95 #define CTRL_M_CB       0x00000200  /* recording source: 0 = ADC, 1 = MPEG */
96 #define CTRL_XCTL0      0x00000100  /* 0 = Line in, 1 = Line out */
97 #define CTRL_BREQ       0x00000080  /* 1 = test mode (internal mem test) */
98 #define CTRL_DAC1_EN    0x00000040  /* enable DAC1 */
99 #define CTRL_DAC2_EN    0x00000020  /* enable DAC2 */
100 #define CTRL_ADC_EN     0x00000010  /* enable ADC */
101 #define CTRL_UART_EN    0x00000008  /* enable MIDI uart */
102 #define CTRL_JYSTK_EN   0x00000004  /* enable Joystick port (presumably at address 0x200) */
103 #define CTRL_CDC_EN     0x00000002  /* enable serial (CODEC) interface */
104 #define CTRL_SERR_DIS   0x00000001  /* 1 = disable PCI SERR signal */
105 
106 #define STAT_INTR       0x80000000  /* wired or of all interrupt bits */
107 #define STAT_CSTAT      0x00000400  /* 1 = codec busy or codec write in progress */
108 #define STAT_CBUSY      0x00000200  /* 1 = codec busy */
109 #define STAT_CWRIP      0x00000100  /* 1 = codec write in progress */
110 #define STAT_VC         0x00000060  /* CCB int source, 0=DAC1, 1=DAC2, 2=ADC, 3=undef */
111 #define STAT_SH_VC      5
112 #define STAT_MCCB       0x00000010  /* CCB int pending */
113 #define STAT_UART       0x00000008  /* UART int pending */
114 #define STAT_DAC1       0x00000004  /* DAC1 int pending */
115 #define STAT_DAC2       0x00000002  /* DAC2 int pending */
116 #define STAT_ADC        0x00000001  /* ADC int pending */
117 
118 #define USTAT_RXINT     0x80        /* UART rx int pending */
119 #define USTAT_TXINT     0x04        /* UART tx int pending */
120 #define USTAT_TXRDY     0x02        /* UART tx ready */
121 #define USTAT_RXRDY     0x01        /* UART rx ready */
122 
123 #define UCTRL_RXINTEN   0x80        /* 1 = enable RX ints */
124 #define UCTRL_TXINTEN   0x60        /* TX int enable field mask */
125 #define UCTRL_ENA_TXINT 0x20        /* enable TX int */
126 #define UCTRL_CNTRL     0x03        /* control field */
127 #define UCTRL_CNTRL_SWR 0x03        /* software reset command */
128 
129 #define SCTRL_P2ENDINC    0x00380000  /*  */
130 #define SCTRL_SH_P2ENDINC 19
131 #define SCTRL_P2STINC     0x00070000  /*  */
132 #define SCTRL_SH_P2STINC  16
133 #define SCTRL_R1LOOPSEL   0x00008000  /* 0 = loop mode */
134 #define SCTRL_P2LOOPSEL   0x00004000  /* 0 = loop mode */
135 #define SCTRL_P1LOOPSEL   0x00002000  /* 0 = loop mode */
136 #define SCTRL_P2PAUSE     0x00001000  /* 1 = pause mode */
137 #define SCTRL_P1PAUSE     0x00000800  /* 1 = pause mode */
138 #define SCTRL_R1INTEN     0x00000400  /* enable interrupt */
139 #define SCTRL_P2INTEN     0x00000200  /* enable interrupt */
140 #define SCTRL_P1INTEN     0x00000100  /* enable interrupt */
141 #define SCTRL_P1SCTRLD    0x00000080  /* reload sample count register for DAC1 */
142 #define SCTRL_P2DACSEN    0x00000040  /* 1 = DAC2 play back last sample when disabled */
143 #define SCTRL_R1SEB       0x00000020  /* 1 = 16bit */
144 #define SCTRL_R1SMB       0x00000010  /* 1 = stereo */
145 #define SCTRL_R1FMT       0x00000030  /* format mask */
146 #define SCTRL_SH_R1FMT    4
147 #define SCTRL_P2SEB       0x00000008  /* 1 = 16bit */
148 #define SCTRL_P2SMB       0x00000004  /* 1 = stereo */
149 #define SCTRL_P2FMT       0x0000000c  /* format mask */
150 #define SCTRL_SH_P2FMT    2
151 #define SCTRL_P1SEB       0x00000002  /* 1 = 16bit */
152 #define SCTRL_P1SMB       0x00000001  /* 1 = stereo */
153 #define SCTRL_P1FMT       0x00000003  /* format mask */
154 #define SCTRL_SH_P1FMT    0
155 
156 /* End blatant GPL violation */
157 
158 #define NB_CHANNELS 3
159 #define DAC1_CHANNEL 0
160 #define DAC2_CHANNEL 1
161 #define ADC_CHANNEL 2
162 
163 static void es1370_dac1_callback (void *opaque, int free);
164 static void es1370_dac2_callback (void *opaque, int free);
165 static void es1370_adc_callback (void *opaque, int avail);
166 
167 #ifdef DEBUG_ES1370
168 
169 #define ldebug(...) AUD_log ("es1370", __VA_ARGS__)
170 
171 static void print_ctl (uint32_t val)
172 {
173     char buf[1024];
174 
175     buf[0] = '\0';
176 #define a(n) if (val & CTRL_##n) strcat (buf, " "#n)
177     a (ADC_STOP);
178     a (XCTL1);
179     a (OPEN);
180     a (MSFMTSEL);
181     a (M_SBB);
182     a (DAC_SYNC);
183     a (CCB_INTRM);
184     a (M_CB);
185     a (XCTL0);
186     a (BREQ);
187     a (DAC1_EN);
188     a (DAC2_EN);
189     a (ADC_EN);
190     a (UART_EN);
191     a (JYSTK_EN);
192     a (CDC_EN);
193     a (SERR_DIS);
194 #undef a
195     AUD_log ("es1370", "ctl - PCLKDIV %d(DAC2 freq %d), freq %d,%s\n",
196              (val & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV,
197              DAC2_DIVTOSR ((val & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV),
198              dac1_samplerate[(val & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL],
199              buf);
200 }
201 
202 static void print_sctl (uint32_t val)
203 {
204     static const char *fmt_names[] = {"8M", "8S", "16M", "16S"};
205     char buf[1024];
206 
207     buf[0] = '\0';
208 
209 #define a(n) if (val & SCTRL_##n) strcat (buf, " "#n)
210 #define b(n) if (!(val & SCTRL_##n)) strcat (buf, " "#n)
211     b (R1LOOPSEL);
212     b (P2LOOPSEL);
213     b (P1LOOPSEL);
214     a (P2PAUSE);
215     a (P1PAUSE);
216     a (R1INTEN);
217     a (P2INTEN);
218     a (P1INTEN);
219     a (P1SCTRLD);
220     a (P2DACSEN);
221     if (buf[0]) {
222         strcat (buf, "\n        ");
223     }
224     else {
225         buf[0] = ' ';
226         buf[1] = '\0';
227     }
228 #undef b
229 #undef a
230     AUD_log ("es1370",
231              "%s"
232              "p2_end_inc %d, p2_st_inc %d, r1_fmt %s, p2_fmt %s, p1_fmt %s\n",
233              buf,
234              (val & SCTRL_P2ENDINC) >> SCTRL_SH_P2ENDINC,
235              (val & SCTRL_P2STINC) >> SCTRL_SH_P2STINC,
236              fmt_names [(val >> SCTRL_SH_R1FMT) & 3],
237              fmt_names [(val >> SCTRL_SH_P2FMT) & 3],
238              fmt_names [(val >> SCTRL_SH_P1FMT) & 3]
239         );
240 }
241 #else
242 #define ldebug(...)
243 #define print_ctl(...)
244 #define print_sctl(...)
245 #endif
246 
247 #ifdef VERBOSE_ES1370
248 #define dolog(...) AUD_log ("es1370", __VA_ARGS__)
249 #else
250 #define dolog(...)
251 #endif
252 
253 #ifndef SILENT_ES1370
254 #define lwarn(...) AUD_log ("es1370: warning", __VA_ARGS__)
255 #else
256 #define lwarn(...)
257 #endif
258 
259 struct chan {
260     uint32_t shift;
261     uint32_t leftover;
262     uint32_t scount;
263     uint32_t frame_addr;
264     uint32_t frame_cnt;
265 };
266 
267 struct ES1370State {
268     PCIDevice dev;
269     QEMUSoundCard card;
270     MemoryRegion io;
271     struct chan chan[NB_CHANNELS];
272     SWVoiceOut *dac_voice[2];
273     SWVoiceIn *adc_voice;
274 
275     uint32_t ctl;
276     uint32_t status;
277     uint32_t mempage;
278     uint32_t codec;
279     uint32_t sctl;
280 };
281 typedef struct ES1370State ES1370State;
282 
283 struct chan_bits {
284     uint32_t ctl_en;
285     uint32_t stat_int;
286     uint32_t sctl_pause;
287     uint32_t sctl_inten;
288     uint32_t sctl_fmt;
289     uint32_t sctl_sh_fmt;
290     uint32_t sctl_loopsel;
291     void (*calc_freq) (ES1370State *s, uint32_t ctl,
292                        uint32_t *old_freq, uint32_t *new_freq);
293 };
294 
295 #define TYPE_ES1370 "ES1370"
296 DECLARE_INSTANCE_CHECKER(ES1370State, ES1370,
297                          TYPE_ES1370)
298 
299 static void es1370_dac1_calc_freq (ES1370State *s, uint32_t ctl,
300                                    uint32_t *old_freq, uint32_t *new_freq);
301 static void es1370_dac2_and_adc_calc_freq (ES1370State *s, uint32_t ctl,
302                                            uint32_t *old_freq,
303                                            uint32_t *new_freq);
304 
305 static const struct chan_bits es1370_chan_bits[] = {
306     {CTRL_DAC1_EN, STAT_DAC1, SCTRL_P1PAUSE, SCTRL_P1INTEN,
307      SCTRL_P1FMT, SCTRL_SH_P1FMT, SCTRL_P1LOOPSEL,
308      es1370_dac1_calc_freq},
309 
310     {CTRL_DAC2_EN, STAT_DAC2, SCTRL_P2PAUSE, SCTRL_P2INTEN,
311      SCTRL_P2FMT, SCTRL_SH_P2FMT, SCTRL_P2LOOPSEL,
312      es1370_dac2_and_adc_calc_freq},
313 
314     {CTRL_ADC_EN, STAT_ADC, 0, SCTRL_R1INTEN,
315      SCTRL_R1FMT, SCTRL_SH_R1FMT, SCTRL_R1LOOPSEL,
316      es1370_dac2_and_adc_calc_freq}
317 };
318 
319 static void es1370_update_status (ES1370State *s, uint32_t new_status)
320 {
321     uint32_t level = new_status & (STAT_DAC1 | STAT_DAC2 | STAT_ADC);
322 
323     if (level) {
324         s->status = new_status | STAT_INTR;
325     }
326     else {
327         s->status = new_status & ~STAT_INTR;
328     }
329     pci_set_irq(&s->dev, !!level);
330 }
331 
332 static void es1370_reset (ES1370State *s)
333 {
334     size_t i;
335 
336     s->ctl = 1;
337     s->status = 0x60;
338     s->mempage = 0;
339     s->codec = 0;
340     s->sctl = 0;
341 
342     for (i = 0; i < NB_CHANNELS; ++i) {
343         struct chan *d = &s->chan[i];
344         d->scount = 0;
345         d->leftover = 0;
346         if (i == ADC_CHANNEL) {
347             AUD_close_in (&s->card, s->adc_voice);
348             s->adc_voice = NULL;
349         }
350         else {
351             AUD_close_out (&s->card, s->dac_voice[i]);
352             s->dac_voice[i] = NULL;
353         }
354     }
355     pci_irq_deassert(&s->dev);
356 }
357 
358 static void es1370_maybe_lower_irq (ES1370State *s, uint32_t sctl)
359 {
360     uint32_t new_status = s->status;
361 
362     if (!(sctl & SCTRL_P1INTEN) && (s->sctl & SCTRL_P1INTEN)) {
363         new_status &= ~STAT_DAC1;
364     }
365 
366     if (!(sctl & SCTRL_P2INTEN) && (s->sctl & SCTRL_P2INTEN)) {
367         new_status &= ~STAT_DAC2;
368     }
369 
370     if (!(sctl & SCTRL_R1INTEN) && (s->sctl & SCTRL_R1INTEN)) {
371         new_status &= ~STAT_ADC;
372     }
373 
374     if (new_status != s->status) {
375         es1370_update_status (s, new_status);
376     }
377 }
378 
379 static void es1370_dac1_calc_freq (ES1370State *s, uint32_t ctl,
380                                    uint32_t *old_freq, uint32_t *new_freq)
381 
382 {
383     *old_freq = dac1_samplerate[(s->ctl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL];
384     *new_freq = dac1_samplerate[(ctl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL];
385 }
386 
387 static void es1370_dac2_and_adc_calc_freq (ES1370State *s, uint32_t ctl,
388                                            uint32_t *old_freq,
389                                            uint32_t *new_freq)
390 
391 {
392     uint32_t old_pclkdiv, new_pclkdiv;
393 
394     new_pclkdiv = (ctl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV;
395     old_pclkdiv = (s->ctl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV;
396     *new_freq = DAC2_DIVTOSR (new_pclkdiv);
397     *old_freq = DAC2_DIVTOSR (old_pclkdiv);
398 }
399 
400 static void es1370_update_voices (ES1370State *s, uint32_t ctl, uint32_t sctl)
401 {
402     size_t i;
403     uint32_t old_freq, new_freq, old_fmt, new_fmt;
404 
405     for (i = 0; i < NB_CHANNELS; ++i) {
406         struct chan *d = &s->chan[i];
407         const struct chan_bits *b = &es1370_chan_bits[i];
408 
409         new_fmt = (sctl & b->sctl_fmt) >> b->sctl_sh_fmt;
410         old_fmt = (s->sctl & b->sctl_fmt) >> b->sctl_sh_fmt;
411 
412         b->calc_freq (s, ctl, &old_freq, &new_freq);
413 
414         if ((old_fmt != new_fmt) || (old_freq != new_freq)) {
415             d->shift = (new_fmt & 1) + (new_fmt >> 1);
416             ldebug ("channel %zu, freq = %d, nchannels %d, fmt %d, shift %d\n",
417                     i,
418                     new_freq,
419                     1 << (new_fmt & 1),
420                     (new_fmt & 2) ? AUDIO_FORMAT_S16 : AUDIO_FORMAT_U8,
421                     d->shift);
422             if (new_freq) {
423                 struct audsettings as;
424 
425                 as.freq = new_freq;
426                 as.nchannels = 1 << (new_fmt & 1);
427                 as.fmt = (new_fmt & 2) ? AUDIO_FORMAT_S16 : AUDIO_FORMAT_U8;
428                 as.endianness = 0;
429 
430                 if (i == ADC_CHANNEL) {
431                     s->adc_voice =
432                         AUD_open_in (
433                             &s->card,
434                             s->adc_voice,
435                             "es1370.adc",
436                             s,
437                             es1370_adc_callback,
438                             &as
439                             );
440                 }
441                 else {
442                     s->dac_voice[i] =
443                         AUD_open_out (
444                             &s->card,
445                             s->dac_voice[i],
446                             i ? "es1370.dac2" : "es1370.dac1",
447                             s,
448                             i ? es1370_dac2_callback : es1370_dac1_callback,
449                             &as
450                             );
451                 }
452             }
453         }
454 
455         if (((ctl ^ s->ctl) & b->ctl_en)
456             || ((sctl ^ s->sctl) & b->sctl_pause)) {
457             int on = (ctl & b->ctl_en) && !(sctl & b->sctl_pause);
458 
459             if (i == ADC_CHANNEL) {
460                 AUD_set_active_in (s->adc_voice, on);
461             }
462             else {
463                 AUD_set_active_out (s->dac_voice[i], on);
464             }
465         }
466     }
467 
468     s->ctl = ctl;
469     s->sctl = sctl;
470 }
471 
472 static inline uint32_t es1370_fixup (ES1370State *s, uint32_t addr)
473 {
474     addr &= 0xff;
475     if (addr >= 0x30 && addr <= 0x3f)
476         addr |= s->mempage << 8;
477     return addr;
478 }
479 
480 static void es1370_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
481 {
482     ES1370State *s = opaque;
483     struct chan *d = &s->chan[0];
484 
485     addr = es1370_fixup (s, addr);
486 
487     switch (addr) {
488     case ES1370_REG_CONTROL:
489         es1370_update_voices (s, val, s->sctl);
490         print_ctl (val);
491         break;
492 
493     case ES1370_REG_MEMPAGE:
494         s->mempage = val & 0xf;
495         break;
496 
497     case ES1370_REG_SERIAL_CONTROL:
498         es1370_maybe_lower_irq (s, val);
499         es1370_update_voices (s, s->ctl, val);
500         print_sctl (val);
501         break;
502 
503     case ES1370_REG_DAC1_SCOUNT:
504     case ES1370_REG_DAC2_SCOUNT:
505     case ES1370_REG_ADC_SCOUNT:
506         d += (addr - ES1370_REG_DAC1_SCOUNT) >> 2;
507         d->scount = (val & 0xffff) | (d->scount & ~0xffff);
508         ldebug ("chan %td CURR_SAMP_CT %d, SAMP_CT %d\n",
509                 d - &s->chan[0], val >> 16, (val & 0xffff));
510         break;
511 
512     case ES1370_REG_ADC_FRAMEADR:
513         d += 2;
514         goto frameadr;
515     case ES1370_REG_DAC1_FRAMEADR:
516     case ES1370_REG_DAC2_FRAMEADR:
517         d += (addr - ES1370_REG_DAC1_FRAMEADR) >> 3;
518     frameadr:
519         d->frame_addr = val;
520         ldebug ("chan %td frame address %#x\n", d - &s->chan[0], val);
521         break;
522 
523     case ES1370_REG_PHANTOM_FRAMECNT:
524         lwarn ("writing to phantom frame count %#x\n", val);
525         break;
526     case ES1370_REG_PHANTOM_FRAMEADR:
527         lwarn ("writing to phantom frame address %#x\n", val);
528         break;
529 
530     case ES1370_REG_ADC_FRAMECNT:
531         d += 2;
532         goto framecnt;
533     case ES1370_REG_DAC1_FRAMECNT:
534     case ES1370_REG_DAC2_FRAMECNT:
535         d += (addr - ES1370_REG_DAC1_FRAMECNT) >> 3;
536     framecnt:
537         d->frame_cnt = val;
538         d->leftover = 0;
539         ldebug ("chan %td frame count %d, buffer size %d\n",
540                 d - &s->chan[0], val >> 16, val & 0xffff);
541         break;
542 
543     default:
544         lwarn ("writel %#x <- %#x\n", addr, val);
545         break;
546     }
547 }
548 
549 static uint64_t es1370_read(void *opaque, hwaddr addr, unsigned size)
550 {
551     ES1370State *s = opaque;
552     uint32_t val;
553     struct chan *d = &s->chan[0];
554 
555     addr = es1370_fixup (s, addr);
556 
557     switch (addr) {
558     case ES1370_REG_CONTROL:
559         val = s->ctl;
560         break;
561     case ES1370_REG_STATUS:
562         val = s->status;
563         break;
564     case ES1370_REG_MEMPAGE:
565         val = s->mempage;
566         break;
567     case ES1370_REG_CODEC:
568         val = s->codec;
569         break;
570     case ES1370_REG_SERIAL_CONTROL:
571         val = s->sctl;
572         break;
573 
574     case ES1370_REG_DAC1_SCOUNT:
575     case ES1370_REG_DAC2_SCOUNT:
576     case ES1370_REG_ADC_SCOUNT:
577         d += (addr - ES1370_REG_DAC1_SCOUNT) >> 2;
578         val = d->scount;
579 #ifdef DEBUG_ES1370
580         {
581             uint32_t curr_count = d->scount >> 16;
582             uint32_t count = d->scount & 0xffff;
583 
584             curr_count <<= d->shift;
585             count <<= d->shift;
586             dolog ("read scount curr %d, total %d\n", curr_count, count);
587         }
588 #endif
589         break;
590 
591     case ES1370_REG_ADC_FRAMECNT:
592         d += 2;
593         goto framecnt;
594     case ES1370_REG_DAC1_FRAMECNT:
595     case ES1370_REG_DAC2_FRAMECNT:
596         d += (addr - ES1370_REG_DAC1_FRAMECNT) >> 3;
597     framecnt:
598         val = d->frame_cnt;
599 #ifdef DEBUG_ES1370
600         {
601             uint32_t size = ((d->frame_cnt & 0xffff) + 1) << 2;
602             uint32_t curr = ((d->frame_cnt >> 16) + 1) << 2;
603             if (curr > size) {
604                 dolog ("read framecnt curr %d, size %d %d\n", curr, size,
605                        curr > size);
606             }
607         }
608 #endif
609         break;
610 
611     case ES1370_REG_ADC_FRAMEADR:
612         d += 2;
613         goto frameadr;
614     case ES1370_REG_DAC1_FRAMEADR:
615     case ES1370_REG_DAC2_FRAMEADR:
616         d += (addr - ES1370_REG_DAC1_FRAMEADR) >> 3;
617     frameadr:
618         val = d->frame_addr;
619         break;
620 
621     case ES1370_REG_PHANTOM_FRAMECNT:
622         val = ~0U;
623         lwarn ("reading from phantom frame count\n");
624         break;
625     case ES1370_REG_PHANTOM_FRAMEADR:
626         val = ~0U;
627         lwarn ("reading from phantom frame address\n");
628         break;
629 
630     default:
631         val = ~0U;
632         lwarn ("readl %#x -> %#x\n", addr, val);
633         break;
634     }
635     return val;
636 }
637 
638 static void es1370_transfer_audio (ES1370State *s, struct chan *d, int loop_sel,
639                                    int max, int *irq)
640 {
641     uint8_t tmpbuf[4096];
642     uint32_t addr = d->frame_addr;
643     int sc = d->scount & 0xffff;
644     int csc = d->scount >> 16;
645     int csc_bytes = (csc + 1) << d->shift;
646     int cnt = d->frame_cnt >> 16;
647     int size = d->frame_cnt & 0xffff;
648     if (size < cnt) {
649         return;
650     }
651     int left = ((size - cnt + 1) << 2) + d->leftover;
652     int transferred = 0;
653     int temp = MIN (max, MIN (left, csc_bytes));
654     int index = d - &s->chan[0];
655 
656     addr += (cnt << 2) + d->leftover;
657 
658     if (index == ADC_CHANNEL) {
659         while (temp > 0) {
660             int acquired, to_copy;
661 
662             to_copy = MIN ((size_t) temp, sizeof (tmpbuf));
663             acquired = AUD_read (s->adc_voice, tmpbuf, to_copy);
664             if (!acquired)
665                 break;
666 
667             pci_dma_write (&s->dev, addr, tmpbuf, acquired);
668 
669             temp -= acquired;
670             addr += acquired;
671             transferred += acquired;
672         }
673     }
674     else {
675         SWVoiceOut *voice = s->dac_voice[index];
676 
677         while (temp > 0) {
678             int copied, to_copy;
679 
680             to_copy = MIN ((size_t) temp, sizeof (tmpbuf));
681             pci_dma_read (&s->dev, addr, tmpbuf, to_copy);
682             copied = AUD_write (voice, tmpbuf, to_copy);
683             if (!copied)
684                 break;
685             temp -= copied;
686             addr += copied;
687             transferred += copied;
688         }
689     }
690 
691     if (csc_bytes == transferred) {
692         *irq = 1;
693         d->scount = sc | (sc << 16);
694         ldebug ("sc = %d, rate = %f\n",
695                 (sc + 1) << d->shift,
696                 (sc + 1) / (double) 44100);
697     }
698     else {
699         *irq = 0;
700         d->scount = sc | (((csc_bytes - transferred - 1) >> d->shift) << 16);
701     }
702 
703     cnt += (transferred + d->leftover) >> 2;
704 
705     if (s->sctl & loop_sel) {
706         /* Bah, how stupid is that having a 0 represent true value?
707            i just spent few hours on this shit */
708         AUD_log ("es1370: warning", "non looping mode\n");
709     }
710     else {
711         d->frame_cnt = size;
712 
713         if ((uint32_t) cnt <= d->frame_cnt)
714             d->frame_cnt |= cnt << 16;
715     }
716 
717     d->leftover = (transferred + d->leftover) & 3;
718 }
719 
720 static void es1370_run_channel (ES1370State *s, size_t chan, int free_or_avail)
721 {
722     uint32_t new_status = s->status;
723     int max_bytes, irq;
724     struct chan *d = &s->chan[chan];
725     const struct chan_bits *b = &es1370_chan_bits[chan];
726 
727     if (!(s->ctl & b->ctl_en) || (s->sctl & b->sctl_pause)) {
728         return;
729     }
730 
731     max_bytes = free_or_avail;
732     max_bytes &= ~((1 << d->shift) - 1);
733     if (!max_bytes) {
734         return;
735     }
736 
737     es1370_transfer_audio (s, d, b->sctl_loopsel, max_bytes, &irq);
738 
739     if (irq) {
740         if (s->sctl & b->sctl_inten) {
741             new_status |= b->stat_int;
742         }
743     }
744 
745     if (new_status != s->status) {
746         es1370_update_status (s, new_status);
747     }
748 }
749 
750 static void es1370_dac1_callback (void *opaque, int free)
751 {
752     ES1370State *s = opaque;
753 
754     es1370_run_channel (s, DAC1_CHANNEL, free);
755 }
756 
757 static void es1370_dac2_callback (void *opaque, int free)
758 {
759     ES1370State *s = opaque;
760 
761     es1370_run_channel (s, DAC2_CHANNEL, free);
762 }
763 
764 static void es1370_adc_callback (void *opaque, int avail)
765 {
766     ES1370State *s = opaque;
767 
768     es1370_run_channel (s, ADC_CHANNEL, avail);
769 }
770 
771 static const MemoryRegionOps es1370_io_ops = {
772     .read = es1370_read,
773     .write = es1370_write,
774     .valid = {
775         .min_access_size = 1,
776         .max_access_size = 4,
777     },
778     .impl = {
779         .min_access_size = 4,
780         .max_access_size = 4,
781     },
782     .endianness = DEVICE_LITTLE_ENDIAN,
783 };
784 
785 static const VMStateDescription vmstate_es1370_channel = {
786     .name = "es1370_channel",
787     .version_id = 2,
788     .minimum_version_id = 2,
789     .fields = (VMStateField[]) {
790         VMSTATE_UINT32 (shift, struct chan),
791         VMSTATE_UINT32 (leftover, struct chan),
792         VMSTATE_UINT32 (scount, struct chan),
793         VMSTATE_UINT32 (frame_addr, struct chan),
794         VMSTATE_UINT32 (frame_cnt, struct chan),
795         VMSTATE_END_OF_LIST ()
796     }
797 };
798 
799 static int es1370_post_load (void *opaque, int version_id)
800 {
801     uint32_t ctl, sctl;
802     ES1370State *s = opaque;
803     size_t i;
804 
805     for (i = 0; i < NB_CHANNELS; ++i) {
806         if (i == ADC_CHANNEL) {
807             if (s->adc_voice) {
808                 AUD_close_in (&s->card, s->adc_voice);
809                 s->adc_voice = NULL;
810             }
811         }
812         else {
813             if (s->dac_voice[i]) {
814                 AUD_close_out (&s->card, s->dac_voice[i]);
815                 s->dac_voice[i] = NULL;
816             }
817         }
818     }
819 
820     ctl = s->ctl;
821     sctl = s->sctl;
822     s->ctl = 0;
823     s->sctl = 0;
824     es1370_update_voices (s, ctl, sctl);
825     return 0;
826 }
827 
828 static const VMStateDescription vmstate_es1370 = {
829     .name = "es1370",
830     .version_id = 2,
831     .minimum_version_id = 2,
832     .post_load = es1370_post_load,
833     .fields = (VMStateField[]) {
834         VMSTATE_PCI_DEVICE (dev, ES1370State),
835         VMSTATE_STRUCT_ARRAY (chan, ES1370State, NB_CHANNELS, 2,
836                               vmstate_es1370_channel, struct chan),
837         VMSTATE_UINT32 (ctl, ES1370State),
838         VMSTATE_UINT32 (status, ES1370State),
839         VMSTATE_UINT32 (mempage, ES1370State),
840         VMSTATE_UINT32 (codec, ES1370State),
841         VMSTATE_UINT32 (sctl, ES1370State),
842         VMSTATE_END_OF_LIST ()
843     }
844 };
845 
846 static void es1370_on_reset(DeviceState *dev)
847 {
848     ES1370State *s = container_of(dev, ES1370State, dev.qdev);
849     es1370_reset (s);
850 }
851 
852 static void es1370_realize(PCIDevice *dev, Error **errp)
853 {
854     ES1370State *s = ES1370(dev);
855     uint8_t *c = s->dev.config;
856 
857     c[PCI_STATUS + 1] = PCI_STATUS_DEVSEL_SLOW >> 8;
858 
859 #if 0
860     c[PCI_CAPABILITY_LIST] = 0xdc;
861     c[PCI_INTERRUPT_LINE] = 10;
862     c[0xdc] = 0x00;
863 #endif
864 
865     c[PCI_INTERRUPT_PIN] = 1;
866     c[PCI_MIN_GNT] = 0x0c;
867     c[PCI_MAX_LAT] = 0x80;
868 
869     memory_region_init_io (&s->io, OBJECT(s), &es1370_io_ops, s, "es1370", 256);
870     pci_register_bar (&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
871 
872     AUD_register_card ("es1370", &s->card);
873     es1370_reset (s);
874 }
875 
876 static void es1370_exit(PCIDevice *dev)
877 {
878     ES1370State *s = ES1370(dev);
879     int i;
880 
881     for (i = 0; i < 2; ++i) {
882         AUD_close_out(&s->card, s->dac_voice[i]);
883     }
884 
885     AUD_close_in(&s->card, s->adc_voice);
886     AUD_remove_card(&s->card);
887 }
888 
889 static Property es1370_properties[] = {
890     DEFINE_AUDIO_PROPERTIES(ES1370State, card),
891     DEFINE_PROP_END_OF_LIST(),
892 };
893 
894 static void es1370_class_init (ObjectClass *klass, void *data)
895 {
896     DeviceClass *dc = DEVICE_CLASS (klass);
897     PCIDeviceClass *k = PCI_DEVICE_CLASS (klass);
898 
899     k->realize = es1370_realize;
900     k->exit = es1370_exit;
901     k->vendor_id = PCI_VENDOR_ID_ENSONIQ;
902     k->device_id = PCI_DEVICE_ID_ENSONIQ_ES1370;
903     k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO;
904     k->subsystem_vendor_id = 0x4942;
905     k->subsystem_id = 0x4c4c;
906     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
907     dc->desc = "ENSONIQ AudioPCI ES1370";
908     dc->vmsd = &vmstate_es1370;
909     dc->reset = es1370_on_reset;
910     device_class_set_props(dc, es1370_properties);
911 }
912 
913 static const TypeInfo es1370_info = {
914     .name          = TYPE_ES1370,
915     .parent        = TYPE_PCI_DEVICE,
916     .instance_size = sizeof (ES1370State),
917     .class_init    = es1370_class_init,
918     .interfaces = (InterfaceInfo[]) {
919         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
920         { },
921     },
922 };
923 
924 static void es1370_register_types (void)
925 {
926     type_register_static (&es1370_info);
927     deprecated_register_soundhw("es1370", "ENSONIQ AudioPCI ES1370",
928                                 0, TYPE_ES1370);
929 }
930 
931 type_init (es1370_register_types)
932