root/drivers/sound/ad1848.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ad_read
  2. ad_write
  3. wait_for_calibration
  4. ad_mute
  5. ad_unmute
  6. ad_enter_MCE
  7. ad_leave_MCE
  8. ad1848_set_recmask
  9. change_bits
  10. ad1848_mixer_get
  11. ad1848_mixer_set
  12. ad1848_mixer_reset
  13. ad1848_mixer_ioctl
  14. ad1848_open
  15. ad1848_close
  16. set_speed
  17. set_channels
  18. set_format
  19. ad1848_ioctl
  20. ad1848_output_block
  21. ad1848_start_input
  22. ad1848_prepare_for_IO
  23. ad1848_reset
  24. ad1848_halt
  25. ad1848_halt_input
  26. ad1848_halt_output
  27. ad1848_trigger
  28. ad1848_detect
  29. ad1848_init
  30. ad1848_unload
  31. ad1848_interrupt
  32. check_opl3
  33. probe_ms_sound
  34. attach_ms_sound
  35. unload_ms_sound
  36. probe_pnp_ad1848
  37. attach_pnp_ad1848
  38. unload_pnp_ad1848
  39. ad1848_tmr_start
  40. ad1848_tmr_reprogram
  41. ad1848_tmr_disable
  42. ad1848_tmr_restart
  43. ad1848_tmr_install

   1 /*
   2  * sound/ad1848.c
   3  *
   4  * The low level driver for the AD1848/CS4248 codec chip which
   5  * is used for example in the MS Sound System.
   6  *
   7  * The CS4231 which is used in the GUS MAX and some other cards is
   8  * upwards compatible with AD1848 and this driver is able to drive it.
   9  *
  10  * CS4231A and AD1845 are upward compatible with CS4231. However
  11  * the new features of these chips are different.
  12  *
  13  * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
  14  * CS4232A is an improved version of CS4232.
  15  */
  16 
  17 /*
  18  * Copyright by Hannu Savolainen 1993-1996
  19  *
  20  * Redistribution and use in source and binary forms, with or without
  21  * modification, are permitted provided that the following conditions are
  22  * met: 1. Redistributions of source code must retain the above copyright
  23  * notice, this list of conditions and the following disclaimer. 2.
  24  * Redistributions in binary form must reproduce the above copyright notice,
  25  * this list of conditions and the following disclaimer in the documentation
  26  * and/or other materials provided with the distribution.
  27  *
  28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  29  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38  * SUCH DAMAGE.
  39  */
  40 #include <linux/config.h>
  41 
  42 
  43 #define DEB(x)
  44 #define DEB1(x)
  45 #include "sound_config.h"
  46 
  47 #if defined(CONFIG_AD1848)
  48 
  49 #include "ad1848_mixer.h"
  50 
  51 typedef struct
  52   {
  53     int             base;
  54     int             irq;
  55     int             dual_dma;   /* 1, when two DMA channels allocated */
  56     unsigned char   MCE_bit;
  57     unsigned char   saved_regs[16];
  58     int             debug_flag;
  59 
  60     int             speed;
  61     unsigned char   speed_bits;
  62     int             channels;
  63     int             audio_format;
  64     unsigned char   format_bits;
  65 
  66     int             xfer_count;
  67     int             irq_mode;
  68     int             intr_active;
  69     int             opened;
  70     char           *chip_name;
  71     int             mode;
  72 #define MD_1848         1
  73 #define MD_4231         2
  74 #define MD_4231A        3
  75 #define MD_1845         4
  76 #define MD_4232         5
  77 
  78     /* Mixer parameters */
  79     int             recmask;
  80     int             supported_devices;
  81     int             supported_rec_devices;
  82     unsigned short  levels[32];
  83     int             dev_no;
  84     volatile unsigned long timer_ticks;
  85     int             timer_running;
  86     int             irq_ok;
  87     int            *osp;
  88   }
  89 
  90 ad1848_info;
  91 
  92 static int      nr_ad1848_devs = 0;
  93 static volatile char irq2dev[17] =
  94 {-1, -1, -1, -1, -1, -1, -1, -1,
  95  -1, -1, -1, -1, -1, -1, -1, -1, -1};
  96 
  97 static int      timer_installed = -1;
  98 
  99 static char     mixer2codec[MAX_MIXER_DEV] =
 100 {0};
 101 
 102 static int      ad_format_mask[6 /*devc->mode */ ] =
 103 {
 104   0,
 105   AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
 106   AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM,
 107   AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM,
 108   AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,     /* AD1845 */
 109   AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM
 110 };
 111 
 112 static ad1848_info dev_info[MAX_AUDIO_DEV];
 113 
 114 #define io_Index_Addr(d)        ((d)->base)
 115 #define io_Indexed_Data(d)      ((d)->base+1)
 116 #define io_Status(d)            ((d)->base+2)
 117 #define io_Polled_IO(d)         ((d)->base+3)
 118 
 119 static int      ad1848_open (int dev, int mode);
 120 static void     ad1848_close (int dev);
 121 static int      ad1848_ioctl (int dev, unsigned int cmd, caddr_t arg, int local);
 122 static void     ad1848_output_block (int dev, unsigned long buf, int count, int intrflag, int dma_restart);
 123 static void     ad1848_start_input (int dev, unsigned long buf, int count, int intrflag, int dma_restart);
 124 static int      ad1848_prepare_for_IO (int dev, int bsize, int bcount);
 125 static void     ad1848_reset (int dev);
 126 static void     ad1848_halt (int dev);
 127 static void     ad1848_halt_input (int dev);
 128 static void     ad1848_halt_output (int dev);
 129 static void     ad1848_trigger (int dev, int bits);
 130 static int      ad1848_tmr_install (int dev);
 131 static void     ad1848_tmr_reprogram (int dev);
 132 
 133 static int
 134 ad_read (ad1848_info * devc, int reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 135 {
 136   unsigned long   flags;
 137   int             x;
 138   int             timeout = 900000;
 139 
 140   while (timeout > 0 && inb (devc->base) == 0x80)       /*Are we initializing */
 141     timeout--;
 142 
 143   save_flags (flags);
 144   cli ();
 145   outb ((unsigned char) (reg & 0xff) | devc->MCE_bit, io_Index_Addr (devc));
 146   x = inb (io_Indexed_Data (devc));
 147   /*  printk("(%02x<-%02x) ", reg|devc->MCE_bit, x); */
 148   restore_flags (flags);
 149 
 150   return x;
 151 }
 152 
 153 static void
 154 ad_write (ad1848_info * devc, int reg, int data)
     /* [previous][next][first][last][top][bottom][index][help] */
 155 {
 156   unsigned long   flags;
 157   int             timeout = 900000;
 158 
 159   while (timeout > 0 &&
 160          inb (devc->base) == 0x80)      /*Are we initializing */
 161     timeout--;
 162 
 163   save_flags (flags);
 164   cli ();
 165   outb ((unsigned char) (reg & 0xff) | devc->MCE_bit, io_Index_Addr (devc));
 166   outb ((unsigned char) (data & 0xff), io_Indexed_Data (devc));
 167   /* printk("(%02x->%02x) ", reg|devc->MCE_bit, data); */
 168   restore_flags (flags);
 169 }
 170 
 171 static void
 172 wait_for_calibration (ad1848_info * devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 173 {
 174   int             timeout = 0;
 175 
 176   /*
 177      * Wait until the auto calibration process has finished.
 178      *
 179      * 1)       Wait until the chip becomes ready (reads don't return 0x80).
 180      * 2)       Wait until the ACI bit of I11 gets on and then off.
 181    */
 182 
 183   timeout = 100000;
 184   while (timeout > 0 && inb (devc->base) == 0x80)
 185     timeout--;
 186   if (inb (devc->base) & 0x80)
 187     printk ("ad1848: Auto calibration timed out(1).\n");
 188 
 189   timeout = 100;
 190   while (timeout > 0 && !(ad_read (devc, 11) & 0x20))
 191     timeout--;
 192   if (!(ad_read (devc, 11) & 0x20))
 193     return;
 194 
 195   timeout = 80000;
 196   while (timeout > 0 && ad_read (devc, 11) & 0x20)
 197     timeout--;
 198   if (ad_read (devc, 11) & 0x20)
 199     printk ("ad1848: Auto calibration timed out(3).\n");
 200 }
 201 
 202 static void
 203 ad_mute (ad1848_info * devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 204 {
 205   int             i;
 206   unsigned char   prev;
 207 
 208   if (devc->mode != MD_1848)
 209     return;
 210   /*
 211      * Save old register settings and mute output channels
 212    */
 213   for (i = 6; i < 8; i++)
 214     {
 215       prev = devc->saved_regs[i] = ad_read (devc, i);
 216       ad_write (devc, i, prev | 0x80);
 217     }
 218 }
 219 
 220 static void
 221 ad_unmute (ad1848_info * devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223   int             i;
 224 
 225   /*
 226      * Restore back old volume registers (unmute)
 227    */
 228   for (i = 6; i < 8; i++)
 229     {
 230       ad_write (devc, i, devc->saved_regs[i] & ~0x80);
 231     }
 232 }
 233 
 234 static void
 235 ad_enter_MCE (ad1848_info * devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237   unsigned long   flags;
 238   int             timeout = 1000;
 239   unsigned short  prev;
 240 
 241   while (timeout > 0 && inb (devc->base) == 0x80)       /*Are we initializing */
 242     timeout--;
 243 
 244   save_flags (flags);
 245   cli ();
 246 
 247   devc->MCE_bit = 0x40;
 248   prev = inb (io_Index_Addr (devc));
 249   if (prev & 0x40)
 250     {
 251       restore_flags (flags);
 252       return;
 253     }
 254 
 255   outb (devc->MCE_bit, io_Index_Addr (devc));
 256   restore_flags (flags);
 257 }
 258 
 259 static void
 260 ad_leave_MCE (ad1848_info * devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 261 {
 262   unsigned long   flags;
 263   unsigned char   prev;
 264   int             timeout = 1000;
 265 
 266   while (timeout > 0 && inb (devc->base) == 0x80)       /*Are we initializing */
 267     timeout--;
 268 
 269   save_flags (flags);
 270   cli ();
 271 
 272   devc->MCE_bit = 0x00;
 273   prev = inb (io_Index_Addr (devc));
 274   outb (0x00, io_Index_Addr (devc));    /* Clear the MCE bit */
 275 
 276   if ((prev & 0x40) == 0)       /* Not in MCE mode */
 277     {
 278       restore_flags (flags);
 279       return;
 280     }
 281 
 282   outb (0x00, io_Index_Addr (devc));    /* Clear the MCE bit */
 283   wait_for_calibration (devc);
 284   restore_flags (flags);
 285 }
 286 
 287 
 288 static int
 289 ad1848_set_recmask (ad1848_info * devc, int mask)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291   unsigned char   recdev;
 292   int             i, n;
 293 
 294   mask &= devc->supported_rec_devices;
 295 
 296   n = 0;
 297   for (i = 0; i < 32; i++)      /* Count selected device bits */
 298     if (mask & (1 << i))
 299       n++;
 300 
 301   if (n == 0)
 302     mask = SOUND_MASK_MIC;
 303   else if (n != 1)              /* Too many devices selected */
 304     {
 305       mask &= ~devc->recmask;   /* Filter out active settings */
 306 
 307       n = 0;
 308       for (i = 0; i < 32; i++)  /* Count selected device bits */
 309         if (mask & (1 << i))
 310           n++;
 311 
 312       if (n != 1)
 313         mask = SOUND_MASK_MIC;
 314     }
 315 
 316   switch (mask)
 317     {
 318     case SOUND_MASK_MIC:
 319       recdev = 2;
 320       break;
 321 
 322     case SOUND_MASK_LINE:
 323     case SOUND_MASK_LINE3:
 324       recdev = 0;
 325       break;
 326 
 327     case SOUND_MASK_CD:
 328     case SOUND_MASK_LINE1:
 329       recdev = 1;
 330       break;
 331 
 332     case SOUND_MASK_IMIX:
 333       recdev = 3;
 334       break;
 335 
 336     default:
 337       mask = SOUND_MASK_MIC;
 338       recdev = 2;
 339     }
 340 
 341   recdev <<= 6;
 342   ad_write (devc, 0, (ad_read (devc, 0) & 0x3f) | recdev);
 343   ad_write (devc, 1, (ad_read (devc, 1) & 0x3f) | recdev);
 344 
 345   devc->recmask = mask;
 346   return mask;
 347 }
 348 
 349 static void
 350 change_bits (unsigned char *regval, int dev, int chn, int newval)
     /* [previous][next][first][last][top][bottom][index][help] */
 351 {
 352   unsigned char   mask;
 353   int             shift;
 354 
 355   if (mix_devices[dev][chn].polarity == 1)      /* Reverse */
 356     newval = 100 - newval;
 357 
 358   mask = (1 << mix_devices[dev][chn].nbits) - 1;
 359   shift = mix_devices[dev][chn].bitpos;
 360   newval = (int) ((newval * mask) + 50) / 100;  /* Scale it */
 361 
 362   *regval &= ~(mask << shift);  /* Clear bits */
 363   *regval |= (newval & mask) << shift;  /* Set new value */
 364 }
 365 
 366 static int
 367 ad1848_mixer_get (ad1848_info * devc, int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 368 {
 369   if (!((1 << dev) & devc->supported_devices))
 370     return -EINVAL;
 371 
 372   return devc->levels[dev];
 373 }
 374 
 375 static int
 376 ad1848_mixer_set (ad1848_info * devc, int dev, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 377 {
 378   int             left = value & 0x000000ff;
 379   int             right = (value & 0x0000ff00) >> 8;
 380   int             retvol;
 381 
 382   int             regoffs;
 383   unsigned char   val;
 384 
 385   if (left > 100)
 386     left = 100;
 387   if (right > 100)
 388     right = 100;
 389 
 390   if (mix_devices[dev][RIGHT_CHN].nbits == 0)   /* Mono control */
 391     right = left;
 392 
 393   retvol = left | (right << 8);
 394 
 395   /* Scale volumes */
 396   left = mix_cvt[left];
 397   right = mix_cvt[right];
 398 
 399   /* Scale it again */
 400   left = mix_cvt[left];
 401   right = mix_cvt[right];
 402 
 403   if (dev > 31)
 404     return -EINVAL;
 405 
 406   if (!(devc->supported_devices & (1 << dev)))
 407     return -EINVAL;
 408 
 409   if (mix_devices[dev][LEFT_CHN].nbits == 0)
 410     return -EINVAL;
 411 
 412   devc->levels[dev] = retvol;
 413 
 414   /*
 415      * Set the left channel
 416    */
 417 
 418   regoffs = mix_devices[dev][LEFT_CHN].regno;
 419   val = ad_read (devc, regoffs);
 420   change_bits (&val, dev, LEFT_CHN, left);
 421   ad_write (devc, regoffs, val);
 422   devc->saved_regs[regoffs] = val;
 423 
 424   /*
 425      * Set the right channel
 426    */
 427 
 428   if (mix_devices[dev][RIGHT_CHN].nbits == 0)
 429     return retvol;              /* Was just a mono channel */
 430 
 431   regoffs = mix_devices[dev][RIGHT_CHN].regno;
 432   val = ad_read (devc, regoffs);
 433   change_bits (&val, dev, RIGHT_CHN, right);
 434   ad_write (devc, regoffs, val);
 435   devc->saved_regs[regoffs] = val;
 436 
 437   return retvol;
 438 }
 439 
 440 static void
 441 ad1848_mixer_reset (ad1848_info * devc)
     /* [previous][next][first][last][top][bottom][index][help] */
 442 {
 443   int             i;
 444 
 445   switch (devc->mode)
 446     {
 447     case MD_4231:
 448     case MD_4231A:
 449     case MD_1845:
 450       devc->supported_devices = MODE2_MIXER_DEVICES;
 451       break;
 452 
 453     case MD_4232:
 454       devc->supported_devices = MODE3_MIXER_DEVICES;
 455       break;
 456 
 457     default:
 458       devc->supported_devices = MODE1_MIXER_DEVICES;
 459     }
 460 
 461   devc->supported_rec_devices = MODE1_REC_DEVICES;
 462 
 463   for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
 464     if (devc->supported_devices & (1 << i))
 465       ad1848_mixer_set (devc, i, default_mixer_levels[i]);
 466   ad1848_set_recmask (devc, SOUND_MASK_MIC);
 467 }
 468 
 469 static int
 470 ad1848_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 471 {
 472   ad1848_info    *devc;
 473 
 474   int             codec_dev = mixer2codec[dev];
 475 
 476   if (!codec_dev)
 477     return -ENXIO;
 478 
 479   codec_dev--;
 480 
 481   devc = (ad1848_info *) audio_devs[codec_dev]->devc;
 482 
 483   if (((cmd >> 8) & 0xff) == 'M')
 484     {
 485 
 486       if (_IOC_DIR (cmd) & _IOC_WRITE)
 487         switch (cmd & 0xff)
 488           {
 489           case SOUND_MIXER_RECSRC:
 490             return snd_ioctl_return ((int *) arg, ad1848_set_recmask (devc, get_fs_long ((long *) arg)));
 491             break;
 492 
 493           default:
 494             return snd_ioctl_return ((int *) arg, ad1848_mixer_set (devc, cmd & 0xff, get_fs_long ((long *) arg)));
 495           }
 496       else
 497         switch (cmd & 0xff)     /*
 498                                  * Return parameters
 499                                  */
 500           {
 501 
 502           case SOUND_MIXER_RECSRC:
 503             return snd_ioctl_return ((int *) arg, devc->recmask);
 504             break;
 505 
 506           case SOUND_MIXER_DEVMASK:
 507             return snd_ioctl_return ((int *) arg, devc->supported_devices);
 508             break;
 509 
 510           case SOUND_MIXER_STEREODEVS:
 511             return snd_ioctl_return ((int *) arg, devc->supported_devices & ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX));
 512             break;
 513 
 514           case SOUND_MIXER_RECMASK:
 515             return snd_ioctl_return ((int *) arg, devc->supported_rec_devices);
 516             break;
 517 
 518           case SOUND_MIXER_CAPS:
 519             return snd_ioctl_return ((int *) arg, SOUND_CAP_EXCL_INPUT);
 520             break;
 521 
 522           default:
 523             return snd_ioctl_return ((int *) arg, ad1848_mixer_get (devc, cmd & 0xff));
 524           }
 525     }
 526   else
 527     return -EINVAL;
 528 }
 529 
 530 static struct audio_operations ad1848_pcm_operations[MAX_AUDIO_DEV] =
 531 {
 532   {
 533     "Generic AD1848 codec",
 534     DMA_AUTOMODE,
 535     AFMT_U8,                    /* Will be set later */
 536     NULL,
 537     ad1848_open,
 538     ad1848_close,
 539     ad1848_output_block,
 540     ad1848_start_input,
 541     ad1848_ioctl,
 542     ad1848_prepare_for_IO,
 543     ad1848_prepare_for_IO,
 544     ad1848_reset,
 545     ad1848_halt,
 546     NULL,
 547     NULL,
 548     ad1848_halt_input,
 549     ad1848_halt_output,
 550     ad1848_trigger
 551   }};
 552 
 553 static struct mixer_operations ad1848_mixer_operations =
 554 {
 555   "AD1848/CS4248/CS4231",
 556   ad1848_mixer_ioctl
 557 };
 558 
 559 static int
 560 ad1848_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 561 {
 562   ad1848_info    *devc = NULL;
 563   unsigned long   flags;
 564 
 565   if (dev < 0 || dev >= num_audiodevs)
 566     return -ENXIO;
 567 
 568   devc = (ad1848_info *) audio_devs[dev]->devc;
 569 
 570 
 571   save_flags (flags);
 572   cli ();
 573   if (devc->opened)
 574     {
 575       restore_flags (flags);
 576       printk ("ad1848: Already opened\n");
 577       return -EBUSY;
 578     }
 579 
 580 
 581   devc->dual_dma = 0;
 582 
 583   if (audio_devs[dev]->flags & DMA_DUPLEX)
 584     {
 585       devc->dual_dma = 1;
 586     }
 587 
 588   devc->intr_active = 0;
 589   devc->opened = 1;
 590   devc->irq_mode = 0;
 591   ad1848_trigger (dev, 0);
 592   restore_flags (flags);
 593 /*
 594  * Mute output until the playback really starts. This decreases clicking (hope so).
 595  */
 596   ad_mute (devc);
 597 
 598   return 0;
 599 }
 600 
 601 static void
 602 ad1848_close (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 603 {
 604   unsigned long   flags;
 605   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
 606 
 607   DEB (printk ("ad1848_close(void)\n"));
 608 
 609   save_flags (flags);
 610   cli ();
 611 
 612   devc->intr_active = 0;
 613   ad1848_reset (dev);
 614 
 615 
 616   devc->opened = 0;
 617   devc->irq_mode = 0;
 618 
 619   ad_unmute (devc);
 620   restore_flags (flags);
 621 }
 622 
 623 static int
 624 set_speed (ad1848_info * devc, int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 625 {
 626   /*
 627      * The sampling speed is encoded in the least significant nibble of I8. The
 628      * LSB selects the clock source (0=24.576 MHz, 1=16.9344 Mhz) and other
 629      * three bits select the divisor (indirectly):
 630      *
 631      * The available speeds are in the following table. Keep the speeds in
 632      * the increasing order.
 633    */
 634   typedef struct
 635   {
 636     int             speed;
 637     unsigned char   bits;
 638   }
 639   speed_struct;
 640 
 641   static speed_struct speed_table[] =
 642   {
 643     {5510, (0 << 1) | 1},
 644     {5510, (0 << 1) | 1},
 645     {6620, (7 << 1) | 1},
 646     {8000, (0 << 1) | 0},
 647     {9600, (7 << 1) | 0},
 648     {11025, (1 << 1) | 1},
 649     {16000, (1 << 1) | 0},
 650     {18900, (2 << 1) | 1},
 651     {22050, (3 << 1) | 1},
 652     {27420, (2 << 1) | 0},
 653     {32000, (3 << 1) | 0},
 654     {33075, (6 << 1) | 1},
 655     {37800, (4 << 1) | 1},
 656     {44100, (5 << 1) | 1},
 657     {48000, (6 << 1) | 0}
 658   };
 659 
 660   int             i, n, selected = -1;
 661 
 662   n = sizeof (speed_table) / sizeof (speed_struct);
 663 
 664   if (devc->mode == MD_1845)    /* AD1845 has different timer than others */
 665     {
 666       if (arg < 4000)
 667         arg = 4000;
 668       if (arg > 50000)
 669         arg = 50000;
 670 
 671       devc->speed = arg;
 672       devc->speed_bits = speed_table[3].bits;
 673       return devc->speed;
 674     }
 675 
 676   if (arg < speed_table[0].speed)
 677     selected = 0;
 678   if (arg > speed_table[n - 1].speed)
 679     selected = n - 1;
 680 
 681   for (i = 1 /*really */ ; selected == -1 && i < n; i++)
 682     if (speed_table[i].speed == arg)
 683       selected = i;
 684     else if (speed_table[i].speed > arg)
 685       {
 686         int             diff1, diff2;
 687 
 688         diff1 = arg - speed_table[i - 1].speed;
 689         diff2 = speed_table[i].speed - arg;
 690 
 691         if (diff1 < diff2)
 692           selected = i - 1;
 693         else
 694           selected = i;
 695       }
 696 
 697   if (selected == -1)
 698     {
 699       printk ("ad1848: Can't find speed???\n");
 700       selected = 3;
 701     }
 702 
 703   devc->speed = speed_table[selected].speed;
 704   devc->speed_bits = speed_table[selected].bits;
 705   return devc->speed;
 706 }
 707 
 708 static int
 709 set_channels (ad1848_info * devc, int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 710 {
 711   if (arg != 1 && arg != 2)
 712     return devc->channels;
 713 
 714   devc->channels = arg;
 715   return arg;
 716 }
 717 
 718 static int
 719 set_format (ad1848_info * devc, int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 720 {
 721 
 722   static struct format_tbl
 723   {
 724     int             format;
 725     unsigned char   bits;
 726   }
 727   format2bits[] =
 728   {
 729     {
 730       0, 0
 731     }
 732     ,
 733     {
 734       AFMT_MU_LAW, 1
 735     }
 736     ,
 737     {
 738       AFMT_A_LAW, 3
 739     }
 740     ,
 741     {
 742       AFMT_IMA_ADPCM, 5
 743     }
 744     ,
 745     {
 746       AFMT_U8, 0
 747     }
 748     ,
 749     {
 750       AFMT_S16_LE, 2
 751     }
 752     ,
 753     {
 754       AFMT_S16_BE, 6
 755     }
 756     ,
 757     {
 758       AFMT_S8, 0
 759     }
 760     ,
 761     {
 762       AFMT_U16_LE, 0
 763     }
 764     ,
 765     {
 766       AFMT_U16_BE, 0
 767     }
 768   };
 769   int             i, n = sizeof (format2bits) / sizeof (struct format_tbl);
 770 
 771   if (!(arg & ad_format_mask[devc->mode]))
 772     arg = AFMT_U8;
 773 
 774   devc->audio_format = arg;
 775 
 776   for (i = 0; i < n; i++)
 777     if (format2bits[i].format == arg)
 778       {
 779         if ((devc->format_bits = format2bits[i].bits) == 0)
 780           return devc->audio_format = AFMT_U8;  /* Was not supported */
 781 
 782         return arg;
 783       }
 784 
 785   /* Still hanging here. Something must be terribly wrong */
 786   devc->format_bits = 0;
 787   return devc->audio_format = AFMT_U8;
 788 }
 789 
 790 static int
 791 ad1848_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
 792 {
 793   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
 794 
 795   switch (cmd)
 796     {
 797     case SOUND_PCM_WRITE_RATE:
 798       if (local)
 799         return set_speed (devc, (int) arg);
 800       return snd_ioctl_return ((int *) arg, set_speed (devc, get_fs_long ((long *) arg)));
 801 
 802     case SOUND_PCM_READ_RATE:
 803       if (local)
 804         return devc->speed;
 805       return snd_ioctl_return ((int *) arg, devc->speed);
 806 
 807     case SNDCTL_DSP_STEREO:
 808       if (local)
 809         return set_channels (devc, (int) arg + 1) - 1;
 810       return snd_ioctl_return ((int *) arg, set_channels (devc, get_fs_long ((long *) arg) + 1) - 1);
 811 
 812     case SOUND_PCM_WRITE_CHANNELS:
 813       if (local)
 814         return set_channels (devc, (int) arg);
 815       return snd_ioctl_return ((int *) arg, set_channels (devc, get_fs_long ((long *) arg)));
 816 
 817     case SOUND_PCM_READ_CHANNELS:
 818       if (local)
 819         return devc->channels;
 820       return snd_ioctl_return ((int *) arg, devc->channels);
 821 
 822     case SNDCTL_DSP_SAMPLESIZE:
 823       if (local)
 824         return set_format (devc, (int) arg);
 825       return snd_ioctl_return ((int *) arg, set_format (devc, get_fs_long ((long *) arg)));
 826 
 827     case SOUND_PCM_READ_BITS:
 828       if (local)
 829         return devc->audio_format;
 830       return snd_ioctl_return ((int *) arg, devc->audio_format);
 831 
 832     default:;
 833     }
 834   return -EINVAL;
 835 }
 836 
 837 static void
 838 ad1848_output_block (int dev, unsigned long buf, int count, int intrflag, int dma_restart)
     /* [previous][next][first][last][top][bottom][index][help] */
 839 {
 840   unsigned long   flags, cnt;
 841   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
 842 
 843   cnt = count;
 844 
 845   if (devc->audio_format == AFMT_IMA_ADPCM)
 846     {
 847       cnt /= 4;
 848     }
 849   else
 850     {
 851       if (devc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))     /* 16 bit data */
 852         cnt >>= 1;
 853     }
 854   if (devc->channels > 1)
 855     cnt >>= 1;
 856   cnt--;
 857 
 858   if (devc->irq_mode & PCM_ENABLE_OUTPUT && audio_devs[dev]->flags & DMA_AUTOMODE &&
 859       intrflag &&
 860       cnt == devc->xfer_count)
 861     {
 862       devc->irq_mode |= PCM_ENABLE_OUTPUT;
 863       devc->intr_active = 1;
 864       return;                   /*
 865                                  * Auto DMA mode on. No need to react
 866                                  */
 867     }
 868   save_flags (flags);
 869   cli ();
 870 
 871   if (dma_restart)
 872     {
 873       ad_write (devc, 9, ad_read (devc, 9) & ~0x01);    /* Playback disable */
 874       /* ad1848_halt (dev); */
 875       DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE);
 876     }
 877 
 878   ad_write (devc, 15, (unsigned char) (cnt & 0xff));
 879   ad_write (devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
 880 
 881   ad_unmute (devc);
 882 
 883   devc->xfer_count = cnt;
 884   devc->irq_mode |= PCM_ENABLE_OUTPUT;
 885   devc->intr_active = 1;
 886   restore_flags (flags);
 887 }
 888 
 889 static void
 890 ad1848_start_input (int dev, unsigned long buf, int count, int intrflag, int dma_restart)
     /* [previous][next][first][last][top][bottom][index][help] */
 891 {
 892   unsigned long   flags, cnt;
 893   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
 894 
 895   cnt = count;
 896   if (devc->audio_format == AFMT_IMA_ADPCM)
 897     {
 898       cnt /= 4;
 899     }
 900   else
 901     {
 902       if (devc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))     /* 16 bit data */
 903         cnt >>= 1;
 904     }
 905   if (devc->channels > 1)
 906     cnt >>= 1;
 907   cnt--;
 908 
 909   if (devc->irq_mode & PCM_ENABLE_INPUT && audio_devs[dev]->flags & DMA_AUTOMODE &&
 910       intrflag &&
 911       cnt == devc->xfer_count)
 912     {
 913       devc->irq_mode |= PCM_ENABLE_INPUT;
 914       devc->intr_active = 1;
 915       return;                   /*
 916                                  * Auto DMA mode on. No need to react
 917                                  */
 918     }
 919   save_flags (flags);
 920   cli ();
 921 
 922   if (dma_restart)
 923     {
 924       /* ad1848_halt (dev); */
 925       ad_write (devc, 9, ad_read (devc, 9) & ~0x02);    /* Capture disable */
 926       DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
 927     }
 928 
 929   if (devc->mode == MD_1848 || !devc->dual_dma)         /* Single DMA channel mode */
 930     {
 931       ad_write (devc, 15, (unsigned char) (cnt & 0xff));
 932       ad_write (devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
 933     }
 934   else
 935     /* Dual DMA channel mode */
 936     {
 937       ad_write (devc, 31, (unsigned char) (cnt & 0xff));
 938       ad_write (devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
 939     }
 940 
 941   ad_unmute (devc);
 942 
 943   devc->xfer_count = cnt;
 944   devc->irq_mode |= PCM_ENABLE_INPUT;
 945   devc->intr_active = 1;
 946   restore_flags (flags);
 947 }
 948 
 949 static int
 950 ad1848_prepare_for_IO (int dev, int bsize, int bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 951 {
 952   int             timeout;
 953   unsigned char   fs, old_fs, tmp = 0;
 954   unsigned long   flags;
 955   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
 956 
 957   if (devc->irq_mode)
 958     return 0;
 959 
 960   save_flags (flags);
 961   cli ();
 962   fs = devc->speed_bits | (devc->format_bits << 5);
 963 
 964   if (devc->channels > 1)
 965     fs |= 0x10;
 966 
 967   if (devc->mode == MD_1845)    /* Use alternate speed select registers */
 968     {
 969       fs &= 0xf0;               /* Mask off the rate select bits */
 970 
 971       ad_write (devc, 22, (devc->speed >> 8) & 0xff);   /* Speed MSB */
 972       ad_write (devc, 23, devc->speed & 0xff);  /* Speed LSB */
 973     }
 974 
 975   old_fs = ad_read (devc, 8);
 976 
 977   if (devc->mode != MD_4232)
 978     if (fs == old_fs)           /* No change */
 979       {
 980         restore_flags (flags);
 981         devc->xfer_count = 0;
 982         return 0;
 983       }
 984 
 985   ad_enter_MCE (devc);          /* Enables changes to the format select reg */
 986 
 987   if (devc->mode == MD_4232)
 988     {
 989       tmp = ad_read (devc, 16);
 990       ad_write (devc, 16, tmp | 0x30);
 991     }
 992 
 993   ad_write (devc, 8, fs);
 994   /*
 995    * Write to I8 starts resynchronization. Wait until it completes.
 996    */
 997   timeout = 10000;
 998   while (timeout > 0 && inb (devc->base) == 0x80)
 999     timeout--;
1000 
1001   /*
1002      * If mode == 2 (CS4231), set I28 also. It's the capture format register.
1003    */
1004   if (devc->mode != MD_1848)
1005     {
1006       ad_write (devc, 28, fs);
1007 
1008       /*
1009          * Write to I28 starts resynchronization. Wait until it completes.
1010        */
1011       timeout = 10000;
1012       while (timeout > 0 && inb (devc->base) == 0x80)
1013         timeout--;
1014 
1015     }
1016 
1017   if (devc->mode == MD_4232)
1018     ad_write (devc, 16, tmp & ~0x30);
1019 
1020   ad_leave_MCE (devc);          /*
1021                                  * Starts the calibration process.
1022                                  */
1023   restore_flags (flags);
1024   devc->xfer_count = 0;
1025 
1026 #ifdef CONFIG_SEQUENCER
1027   if (dev == timer_installed && devc->timer_running)
1028     if ((fs & 0x01) != (old_fs & 0x01))
1029       {
1030         ad1848_tmr_reprogram (dev);
1031       }
1032 #endif
1033   return 0;
1034 }
1035 
1036 static void
1037 ad1848_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1038 {
1039   ad1848_halt (dev);
1040 }
1041 
1042 static void
1043 ad1848_halt (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1044 {
1045   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1046 
1047   unsigned char   bits = ad_read (devc, 9);
1048 
1049   if (bits & 0x01)
1050     ad1848_halt_output (dev);
1051 
1052   if (bits & 0x02)
1053     ad1848_halt_input (dev);
1054 }
1055 
1056 static void
1057 ad1848_halt_input (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1058 {
1059   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1060   unsigned long   flags;
1061 
1062   save_flags (flags);
1063   cli ();
1064 
1065   ad_mute (devc);
1066 
1067   if (devc->mode == MD_4232)    /* Use applied black magic */
1068     {
1069       int             tmout;
1070 
1071       disable_dma (audio_devs[dev]->dmachan1);
1072 
1073       for (tmout = 0; tmout < 100000; tmout++)
1074         if (ad_read (devc, 11) & 0x10)
1075           break;
1076       ad_write (devc, 9, ad_read (devc, 9) & ~0x01);    /* Stop playback */
1077 
1078       enable_dma (audio_devs[dev]->dmachan1);
1079       restore_flags (flags);
1080       return;
1081     }
1082   ad_write (devc, 9, ad_read (devc, 9) & ~0x02);        /* Stop capture */
1083 
1084 
1085   outb (0, io_Status (devc));   /* Clear interrupt status */
1086   outb (0, io_Status (devc));   /* Clear interrupt status */
1087 
1088   devc->irq_mode &= ~PCM_ENABLE_INPUT;
1089 
1090   restore_flags (flags);
1091 }
1092 
1093 static void
1094 ad1848_halt_output (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1095 {
1096   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1097   unsigned long   flags;
1098 
1099   save_flags (flags);
1100   cli ();
1101 
1102   ad_mute (devc);
1103   if (devc->mode == MD_4232)    /* Use applied black magic */
1104     {
1105       int             tmout;
1106 
1107       disable_dma (audio_devs[dev]->dmachan1);
1108 
1109       for (tmout = 0; tmout < 100000; tmout++)
1110         if (ad_read (devc, 11) & 0x10)
1111           break;
1112       ad_write (devc, 9, ad_read (devc, 9) & ~0x01);    /* Stop playback */
1113 
1114       enable_dma (audio_devs[dev]->dmachan1);
1115       restore_flags (flags);
1116       return;
1117     }
1118   ad_write (devc, 9, ad_read (devc, 9) & ~0x01);        /* Stop playback */
1119 
1120 
1121   outb (0, io_Status (devc));   /* Clear interrupt status */
1122   outb (0, io_Status (devc));   /* Clear interrupt status */
1123 
1124   devc->irq_mode &= ~PCM_ENABLE_OUTPUT;
1125 
1126   restore_flags (flags);
1127 }
1128 
1129 static void
1130 ad1848_trigger (int dev, int state)
     /* [previous][next][first][last][top][bottom][index][help] */
1131 {
1132   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1133   unsigned long   flags;
1134   unsigned char   tmp;
1135 
1136   save_flags (flags);
1137   cli ();
1138   state &= devc->irq_mode;
1139 
1140   tmp = ad_read (devc, 9) & ~0x03;
1141   if (state & PCM_ENABLE_INPUT)
1142     tmp |= 0x02;
1143   if (state & PCM_ENABLE_OUTPUT)
1144     tmp |= 0x01;
1145   ad_write (devc, 9, tmp);
1146 
1147   restore_flags (flags);
1148 }
1149 
1150 int
1151 ad1848_detect (int io_base, int *ad_flags, int *osp)
     /* [previous][next][first][last][top][bottom][index][help] */
1152 {
1153 
1154   unsigned char   tmp;
1155   ad1848_info    *devc = &dev_info[nr_ad1848_devs];
1156   unsigned char   tmp1 = 0xff, tmp2 = 0xff;
1157   int             i;
1158 
1159   DDB (printk ("ad1848_detect(%x)\n", io_base));
1160 
1161   if (ad_flags)
1162     *ad_flags = 0;
1163 
1164   if (nr_ad1848_devs >= MAX_AUDIO_DEV)
1165     {
1166       DDB (printk ("ad1848 detect error - step 0\n"));
1167       return 0;
1168     }
1169   if (check_region (io_base, 4))
1170     {
1171       printk ("\n\nad1848.c: Port %x not free.\n\n", io_base);
1172       return 0;
1173     }
1174 
1175   devc->base = io_base;
1176   devc->irq_ok = 0;
1177   devc->timer_running = 0;
1178   devc->MCE_bit = 0x40;
1179   devc->irq = 0;
1180   devc->opened = 0;
1181   devc->chip_name = "AD1848";
1182   devc->mode = MD_1848;         /* AD1848 or CS4248 */
1183   devc->osp = osp;
1184   devc->debug_flag = 0;
1185 
1186   /*
1187      * Check that the I/O address is in use.
1188      *
1189      * The bit 0x80 of the base I/O port is known to be 0 after the
1190      * chip has performed it's power on initialization. Just assume
1191      * this has happened before the OS is starting.
1192      *
1193      * If the I/O address is unused, it typically returns 0xff.
1194    */
1195 
1196   DDB (printk ("ad1848_detect() - step A\n"));
1197   if ((inb (devc->base) & 0x80) != 0x00)        /* Not a AD1848 */
1198     {
1199       DDB (printk ("ad1848 detect error - step A (%02x)\n",
1200                    inb (devc->base)));
1201       return 0;
1202     }
1203 
1204   DDB (printk ("ad1848: regs: "));
1205   for (i = 0; i < 32; i++)
1206     DDB (printk ("%02x ", ad_read (devc, i)));
1207   DDB (printk ("\n"));
1208 
1209   /*
1210      * Test if it's possible to change contents of the indirect registers.
1211      * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
1212      * so try to avoid using it.
1213    */
1214 
1215   DDB (printk ("ad1848_detect() - step B\n"));
1216   ad_write (devc, 0, 0xaa);
1217   ad_write (devc, 1, 0x45);     /* 0x55 with bit 0x10 clear */
1218 
1219   if ((tmp1 = ad_read (devc, 0)) != 0xaa || (tmp2 = ad_read (devc, 1)) != 0x45)
1220     {
1221       DDB (printk ("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
1222       return 0;
1223     }
1224 
1225   DDB (printk ("ad1848_detect() - step C\n"));
1226   ad_write (devc, 0, 0x45);
1227   ad_write (devc, 1, 0xaa);
1228 
1229   if ((tmp1 = ad_read (devc, 0)) != 0x45 || (tmp2 = ad_read (devc, 1)) != 0xaa)
1230     {
1231       DDB (printk ("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
1232       return 0;
1233     }
1234 
1235   /*
1236      * The indirect register I12 has some read only bits. Lets
1237      * try to change them.
1238    */
1239 
1240   DDB (printk ("ad1848_detect() - step D\n"));
1241   tmp = ad_read (devc, 12);
1242   ad_write (devc, 12, (~tmp) & 0x0f);
1243 
1244   if ((tmp & 0x0f) != ((tmp1 = ad_read (devc, 12)) & 0x0f))
1245     {
1246       DDB (printk ("ad1848 detect error - step D (%x)\n", tmp1));
1247       return 0;
1248     }
1249 
1250   /*
1251      * NOTE! Last 4 bits of the reg I12 tell the chip revision.
1252      *   0x01=RevB and 0x0A=RevC.
1253    */
1254 
1255   /*
1256      * The original AD1848/CS4248 has just 15 indirect registers. This means
1257      * that I0 and I16 should return the same value (etc.).
1258      * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
1259      * with CS4231.
1260    */
1261 
1262 
1263   /*
1264      * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
1265      * The bit 0x80 is always 1 in CS4248 and CS4231.
1266    */
1267 
1268   DDB (printk ("ad1848_detect() - step G\n"));
1269   ad_write (devc, 12, 0x40);    /* Set mode2, clear 0x80 */
1270 
1271   tmp1 = ad_read (devc, 12);
1272   if (tmp1 & 0x80)
1273     {
1274       if (ad_flags)
1275         *ad_flags |= AD_F_CS4248;
1276 
1277       devc->chip_name = "CS4248";       /* Our best knowledge just now */
1278     }
1279 
1280   if ((tmp1 & 0xc0) == (0x80 | 0x40))
1281     {
1282       /*
1283          *      CS4231 detected - is it?
1284          *
1285          *      Verify that setting I0 doesn't change I16.
1286        */
1287       DDB (printk ("ad1848_detect() - step H\n"));
1288       ad_write (devc, 16, 0);   /* Set I16 to known value */
1289 
1290       ad_write (devc, 0, 0x45);
1291       if ((tmp1 = ad_read (devc, 16)) != 0x45)  /* No change -> CS4231? */
1292         {
1293 
1294           ad_write (devc, 0, 0xaa);
1295           if ((tmp1 = ad_read (devc, 16)) == 0xaa)      /* Rotten bits? */
1296             {
1297               DDB (printk ("ad1848 detect error - step H(%x)\n", tmp1));
1298               return 0;
1299             }
1300 
1301           /*
1302              * Verify that some bits of I25 are read only.
1303            */
1304 
1305           DDB (printk ("ad1848_detect() - step I\n"));
1306           tmp1 = ad_read (devc, 25);    /* Original bits */
1307           ad_write (devc, 25, ~tmp1);   /* Invert all bits */
1308           if ((ad_read (devc, 25) & 0xe7) == (tmp1 & 0xe7))
1309             {
1310               int             id;
1311 
1312               /*
1313                *      It's at least CS4231
1314                */
1315               devc->chip_name = "CS4231";
1316 
1317               devc->mode = MD_4231;
1318 
1319               /*
1320                * It could be an AD1845 or CS4231A as well.
1321                * CS4231 and AD1845 report the same revision info in I25
1322                * while the CS4231A reports different.
1323                */
1324 
1325               DDB (printk ("ad1848_detect() - step I\n"));
1326               id = ad_read (devc, 25) & 0xe7;
1327 
1328               switch (id)
1329                 {
1330 
1331                 case 0xa0:
1332                   devc->chip_name = "CS4231A";
1333                   devc->mode = MD_4231A;
1334                   break;
1335 
1336                 case 0xa2:
1337                   devc->chip_name = "CS4232";
1338                   devc->mode = MD_4232;
1339                   break;
1340 
1341                 case 0xb2:
1342                   devc->chip_name = "CS4232A";
1343                   devc->mode = MD_4232;
1344                   break;
1345 
1346                 case 0x80:
1347                   {
1348                     /* 
1349                      * It must be a CS4231 or AD1845. The register I23 of
1350                      * CS4231 is undefined and it appears to be read only.
1351                      * AD1845 uses I23 for setting sample rate. Assume
1352                      * the chip is AD1845 if I23 is changeable.
1353                      */
1354 
1355                     unsigned char   tmp = ad_read (devc, 23);
1356 
1357                     ad_write (devc, 23, ~tmp);
1358                     if (ad_read (devc, 23) != tmp)      /* AD1845 ? */
1359                       {
1360                         devc->chip_name = "AD1845";
1361                         devc->mode = MD_1845;
1362                       }
1363 
1364                     ad_write (devc, 23, tmp);   /* Restore */
1365                   }
1366                   break;
1367 
1368                 default:        /* Assume CS4231 */
1369                   devc->mode = MD_4231;
1370 
1371                 }
1372             }
1373           ad_write (devc, 25, tmp1);    /* Restore bits */
1374 
1375           DDB (printk ("ad1848_detect() - step K\n"));
1376         }
1377     }
1378 
1379   DDB (printk ("ad1848_detect() - step L\n"));
1380   if (ad_flags)
1381     {
1382       if (devc->mode != MD_1848)
1383         *ad_flags |= AD_F_CS4231;
1384     }
1385 
1386   DDB (printk ("ad1848_detect() - Detected OK\n"));
1387   return 1;
1388 }
1389 
1390 void
1391 ad1848_init (char *name, int io_base, int irq, int dma_playback, int dma_capture, int share_dma, int *osp)
     /* [previous][next][first][last][top][bottom][index][help] */
1392 {
1393   /*
1394      * NOTE! If irq < 0, there is another driver which has allocated the IRQ
1395      *   so that this driver doesn't need to allocate/deallocate it.
1396      *   The actually used IRQ is ABS(irq).
1397    */
1398 
1399   /*
1400      * Initial values for the indirect registers of CS4248/AD1848.
1401    */
1402   static int      init_values[] =
1403   {
1404     0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1405     0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
1406 
1407   /* Positions 16 to 31 just for CS4231 */
1408     0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,
1409     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1410   };
1411   int             i, my_dev;
1412 
1413   ad1848_info    *devc = &dev_info[nr_ad1848_devs];
1414 
1415   if (!ad1848_detect (io_base, NULL, osp))
1416     return;
1417 
1418   request_region (devc->base, 4, devc->chip_name);
1419 
1420   devc->irq = (irq > 0) ? irq : 0;
1421   devc->opened = 0;
1422   devc->timer_ticks = 0;
1423   devc->osp = osp;
1424 
1425   if (nr_ad1848_devs != 0)
1426     {
1427       memcpy ((char *) &ad1848_pcm_operations[nr_ad1848_devs],
1428               (char *) &ad1848_pcm_operations[0],
1429               sizeof (struct audio_operations));
1430     }
1431 
1432   for (i = 0; i < 16; i++)
1433     ad_write (devc, i, init_values[i]);
1434 
1435   ad_mute (devc);               /* Initialize some variables */
1436   ad_unmute (devc);             /* Leave it unmuted now */
1437 
1438   if (devc->mode > MD_1848)
1439     {
1440       if (dma_capture == dma_playback || dma_capture == -1 || dma_playback == -1)
1441         {
1442           ad_write (devc, 9, ad_read (devc, 9) | 0x04);         /* Single DMA mode */
1443           ad1848_pcm_operations[nr_ad1848_devs].flags &= ~DMA_DUPLEX;
1444         }
1445       else
1446         {
1447           ad_write (devc, 9, ad_read (devc, 9) & ~0x04);        /* Dual DMA mode */
1448           ad1848_pcm_operations[nr_ad1848_devs].flags |= DMA_DUPLEX;
1449         }
1450 
1451       ad_write (devc, 12, ad_read (devc, 12) | 0x40);   /* Mode2 = enabled */
1452       for (i = 16; i < 32; i++)
1453         ad_write (devc, i, init_values[i]);
1454 
1455 
1456       if (devc->mode == MD_1845)
1457         ad_write (devc, 27, ad_read (devc, 27) | 0x08);         /* Alternate freq select enabled */
1458     }
1459   else
1460     {
1461       ad1848_pcm_operations[nr_ad1848_devs].flags &= ~DMA_DUPLEX;
1462       ad_write (devc, 9, ad_read (devc, 9) | 0x04);     /* Single DMA mode */
1463     }
1464 
1465   outb (0, io_Status (devc));   /* Clear pending interrupts */
1466 
1467   if (name != NULL && name[0] != 0)
1468     sprintf (ad1848_pcm_operations[nr_ad1848_devs].name,
1469              "%s (%s)", name, devc->chip_name);
1470   else
1471     sprintf (ad1848_pcm_operations[nr_ad1848_devs].name,
1472              "Generic audio codec (%s)", devc->chip_name);
1473 
1474   conf_printf2 (ad1848_pcm_operations[nr_ad1848_devs].name,
1475                 devc->base, devc->irq, dma_playback, dma_capture);
1476 
1477   if (num_audiodevs < MAX_AUDIO_DEV)
1478     {
1479       audio_devs[my_dev = num_audiodevs++] = &ad1848_pcm_operations[nr_ad1848_devs];
1480       if (irq > 0)
1481         {
1482           audio_devs[my_dev]->devc = devc;
1483           irq2dev[irq] = devc->dev_no = my_dev;
1484           if (snd_set_irq_handler (devc->irq, ad1848_interrupt,
1485                                    audio_devs[my_dev]->name,
1486                                    devc->osp) < 0)
1487             {
1488               printk ("ad1848: IRQ in use\n");
1489             }
1490 
1491 #ifdef NO_IRQ_TEST
1492           if (devc->mode != MD_1848)
1493             {
1494               int             x;
1495               unsigned char   tmp = ad_read (devc, 16);
1496 
1497               devc->timer_ticks = 0;
1498 
1499               ad_write (devc, 21, 0x00);        /* Timer msb */
1500               ad_write (devc, 20, 0x10);        /* Timer lsb */
1501 
1502               ad_write (devc, 16, tmp | 0x40);  /* Enable timer */
1503               for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
1504               ad_write (devc, 16, tmp & ~0x40);         /* Disable timer */
1505 
1506               if (devc->timer_ticks == 0)
1507                 printk ("[IRQ conflict???]");
1508               else
1509                 devc->irq_ok = 1;
1510 
1511             }
1512           else
1513             devc->irq_ok = 1;   /* Couldn't test. assume it's OK */
1514 #else
1515           devc->irq_ok = 1;
1516 #endif
1517         }
1518       else if (irq < 0)
1519         irq2dev[-irq] = devc->dev_no = my_dev;
1520 
1521       audio_devs[my_dev]->dmachan1 = dma_playback;
1522       audio_devs[my_dev]->dmachan2 = dma_capture;
1523       audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
1524       audio_devs[my_dev]->devc = devc;
1525       audio_devs[my_dev]->format_mask = ad_format_mask[devc->mode];
1526       nr_ad1848_devs++;
1527 
1528 #ifdef CONFIG_SEQUENCER
1529       if (devc->mode != MD_1848 && devc->mode != MD_1845 && devc->irq_ok)
1530         ad1848_tmr_install (my_dev);
1531 #endif
1532 
1533       if (!share_dma)
1534         {
1535           if (sound_alloc_dma (dma_playback, "Sound System"))
1536             printk ("ad1848.c: Can't allocate DMA%d\n", dma_playback);
1537 
1538           if (dma_capture != dma_playback)
1539             if (sound_alloc_dma (dma_capture, "Sound System (capture)"))
1540               printk ("ad1848.c: Can't allocate DMA%d\n", dma_capture);
1541         }
1542 
1543       /*
1544          * Toggle the MCE bit. It completes the initialization phase.
1545        */
1546 
1547       ad_enter_MCE (devc);      /* In case the bit was off */
1548       ad_leave_MCE (devc);
1549 
1550       if (num_mixers < MAX_MIXER_DEV)
1551         {
1552           mixer2codec[num_mixers] = my_dev + 1;
1553           audio_devs[my_dev]->mixer_dev = num_mixers;
1554           mixer_devs[num_mixers++] = &ad1848_mixer_operations;
1555           ad1848_mixer_reset (devc);
1556         }
1557     }
1558   else
1559     printk ("AD1848: Too many PCM devices available\n");
1560 }
1561 
1562 void
1563 ad1848_unload (int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
     /* [previous][next][first][last][top][bottom][index][help] */
1564 {
1565   int             i, dev = 0;
1566   ad1848_info    *devc = NULL;
1567 
1568   for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
1569     if (dev_info[i].base == io_base)
1570       {
1571         devc = &dev_info[i];
1572         dev = devc->dev_no;
1573       }
1574 
1575   if (devc != NULL)
1576     {
1577       release_region (devc->base, 4);
1578 
1579       if (!share_dma)
1580         {
1581           if (irq > 0)
1582             snd_release_irq (devc->irq);
1583 
1584           sound_free_dma (audio_devs[dev]->dmachan1);
1585 
1586           if (audio_devs[dev]->dmachan2 != audio_devs[dev]->dmachan1)
1587             sound_free_dma (audio_devs[dev]->dmachan2);
1588         }
1589     }
1590   else
1591     printk ("ad1848: Can't find device to be unloaded. Base=%x\n",
1592             io_base);
1593 }
1594 
1595 void
1596 ad1848_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
1597 {
1598   unsigned char   status;
1599   ad1848_info    *devc;
1600   int             dev;
1601   int             alt_stat = 0xff;
1602 
1603   if (irq < 0 || irq > 15)
1604     {
1605       dev = -1;
1606     }
1607   else
1608     dev = irq2dev[irq];
1609 
1610   if (dev < 0 || dev >= num_audiodevs)
1611     {
1612       for (irq = 0; irq < 17; irq++)
1613         if (irq2dev[irq] != -1)
1614           break;
1615 
1616       if (irq > 15)
1617         {
1618           /* printk ("ad1848.c: Bogus interrupt %d\n", irq); */
1619           return;
1620         }
1621 
1622       dev = irq2dev[irq];
1623       devc = (ad1848_info *) audio_devs[dev]->devc;
1624     }
1625   else
1626     devc = (ad1848_info *) audio_devs[dev]->devc;
1627 
1628   status = inb (io_Status (devc));
1629 
1630   if (status == 0x80)
1631     printk ("ad1848_interrupt: Why?\n");
1632 
1633   if (status & 0x01)
1634     {
1635 
1636       if (devc->mode != MD_1848)
1637         alt_stat = ad_read (devc, 24);
1638 
1639       if (devc->opened && devc->irq_mode & PCM_ENABLE_INPUT && alt_stat & 0x20)
1640         {
1641           DMAbuf_inputintr (dev);
1642         }
1643 
1644       if (devc->opened && devc->irq_mode & PCM_ENABLE_OUTPUT &&
1645           alt_stat & 0x10)
1646         {
1647           DMAbuf_outputintr (dev, 1);
1648         }
1649 
1650       if (devc->mode != MD_1848 && alt_stat & 0x40)     /* Timer interrupt */
1651         {
1652           devc->timer_ticks++;
1653 #ifdef CONFIG_SEQUENCER
1654           if (timer_installed == dev && devc->timer_running)
1655             sound_timer_interrupt ();
1656 #endif
1657         }
1658     }
1659 
1660   if (devc->mode != MD_1848)
1661     ad_write (devc, 24, ad_read (devc, 24) & ~alt_stat);        /* Selective ack */
1662   else
1663     outb (0, io_Status (devc)); /* Clear interrupt status */
1664 }
1665 
1666 /*
1667  * Some extra code for the MS Sound System
1668  */
1669 
1670 void
1671 check_opl3 (int base, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1672 {
1673 
1674 #ifdef CONFIG_YM3812
1675   if (check_region (base, 4))
1676     {
1677       printk ("\n\nopl3.c: I/O port %x already in use\n\n", base);
1678       return;
1679     }
1680 
1681   if (!opl3_detect (base, hw_config->osp))
1682     return;
1683 
1684   opl3_init (0, base, hw_config->osp);
1685   request_region (base, 4, "OPL3/OPL2");
1686 #endif
1687 }
1688 
1689 int
1690 probe_ms_sound (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1691 {
1692   unsigned char   tmp;
1693 
1694   DDB (printk ("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
1695 
1696   if (check_region (hw_config->io_base, 8))
1697     {
1698       printk ("MSS: I/O port conflict\n");
1699       return 0;
1700     }
1701 
1702   if (hw_config->card_subtype == 1)     /* Has no IRQ/DMA registers */
1703     {
1704       /* check_opl3(0x388, hw_config); */
1705       return ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp);
1706     }
1707 
1708   /*
1709      * Check if the IO port returns valid signature. The original MS Sound
1710      * system returns 0x04 while some cards (AudioTriX Pro for example)
1711      * return 0x00 or 0x0f.
1712    */
1713 
1714   if ((tmp = inb (hw_config->io_base + 3)) == 0xff)     /* Bus float */
1715     {
1716       int             ret;
1717 
1718       DDB (printk ("I/O address is inactive (%x)\n", tmp));
1719       if (!(ret = ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp)))
1720         return 0;
1721       return 1;
1722     }
1723   if ((tmp & 0x3f) != 0x04 &&
1724       (tmp & 0x3f) != 0x0f &&
1725       (tmp & 0x3f) != 0x00)
1726     {
1727       int             ret;
1728 
1729       DDB (printk ("No MSS signature detected on port 0x%x (0x%x)\n",
1730                    hw_config->io_base, inb (hw_config->io_base + 3)));
1731       DDB (printk ("Trying to detect codec anyway but IRQ/DMA may not work\n"));
1732       if (!(ret = ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp)))
1733         return 0;
1734 
1735       return 1;
1736     }
1737 
1738   if (hw_config->irq > 11)
1739     {
1740       printk ("MSS: Bad IRQ %d\n", hw_config->irq);
1741       return 0;
1742     }
1743 
1744   if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
1745     {
1746       printk ("MSS: Bad DMA %d\n", hw_config->dma);
1747       return 0;
1748     }
1749 
1750   /*
1751      * Check that DMA0 is not in use with a 8 bit board.
1752    */
1753 
1754   if (hw_config->dma == 0 && inb (hw_config->io_base + 3) & 0x80)
1755     {
1756       printk ("MSS: Can't use DMA0 with a 8 bit card/slot\n");
1757       return 0;
1758     }
1759 
1760   if (hw_config->irq > 7 && hw_config->irq != 9 && inb (hw_config->io_base + 3) & 0x80)
1761     {
1762       printk ("MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
1763       return 0;
1764     }
1765 
1766   return ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp);
1767 }
1768 
1769 long
1770 attach_ms_sound (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1771 {
1772   static char     interrupt_bits[12] =
1773   {
1774     -1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
1775   };
1776   char            bits;
1777 
1778   static char     dma_bits[4] =
1779   {
1780     1, 2, 0, 3
1781   };
1782 
1783   int             config_port = hw_config->io_base + 0;
1784   int             version_port = hw_config->io_base + 3;
1785 
1786   if (!ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp))
1787     return mem_start;
1788 
1789   if (hw_config->card_subtype == 1)     /* Has no IRQ/DMA registers */
1790     {
1791       ad1848_init ("MS Sound System", hw_config->io_base + 4,
1792                    hw_config->irq,
1793                    hw_config->dma,
1794                    hw_config->dma2, 0, hw_config->osp);
1795       request_region (hw_config->io_base, 4, "WSS config");
1796       return mem_start;
1797     }
1798 
1799   /*
1800      * Set the IRQ and DMA addresses.
1801    */
1802 
1803   bits = interrupt_bits[hw_config->irq];
1804   if (bits == -1)
1805     return mem_start;
1806 
1807   outb (bits | 0x40, config_port);
1808   if ((inb (version_port) & 0x40) == 0)
1809     printk ("[IRQ Conflict?]");
1810 
1811   outb (bits | dma_bits[hw_config->dma], config_port);  /* Write IRQ+DMA setup */
1812 
1813   ad1848_init ("MS Sound System", hw_config->io_base + 4,
1814                hw_config->irq,
1815                hw_config->dma,
1816                hw_config->dma, 0, hw_config->osp);
1817   request_region (hw_config->io_base, 4, "WSS config");
1818   return mem_start;
1819 }
1820 
1821 void
1822 unload_ms_sound (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1823 {
1824   ad1848_unload (hw_config->io_base + 4,
1825                  hw_config->irq,
1826                  hw_config->dma,
1827                  hw_config->dma, 0);
1828   release_region (hw_config->io_base, 4);
1829 }
1830 
1831 /*
1832  * WSS compatible PnP codec support
1833  */
1834 
1835 int
1836 probe_pnp_ad1848 (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1837 {
1838   return ad1848_detect (hw_config->io_base, NULL, hw_config->osp);
1839 }
1840 
1841 long
1842 attach_pnp_ad1848 (long mem_start, struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1843 {
1844 
1845   ad1848_init (hw_config->name, hw_config->io_base,
1846                hw_config->irq,
1847                hw_config->dma,
1848                hw_config->dma2, 0, hw_config->osp);
1849   return mem_start;
1850 }
1851 
1852 void
1853 unload_pnp_ad1848 (struct address_info *hw_config)
     /* [previous][next][first][last][top][bottom][index][help] */
1854 {
1855   ad1848_unload (hw_config->io_base,
1856                  hw_config->irq,
1857                  hw_config->dma,
1858                  hw_config->dma2, 0);
1859   release_region (hw_config->io_base, 4);
1860 }
1861 
1862 #ifdef CONFIG_SEQUENCER
1863 /*
1864  * Timer stuff (for /dev/music).
1865  */
1866 
1867 static unsigned int current_interval = 0;
1868 
1869 static unsigned int
1870 ad1848_tmr_start (int dev, unsigned int usecs)
     /* [previous][next][first][last][top][bottom][index][help] */
1871 {
1872   unsigned long   flags;
1873   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1874   unsigned long   xtal_nsecs;   /* nanoseconds per xtal oscillator tick */
1875   unsigned long   divider;
1876 
1877   save_flags (flags);
1878   cli ();
1879 
1880 /*
1881  * Length of the timer interval (in nanoseconds) depends on the
1882  * selected crystal oscillator. Check this from bit 0x01 of I8.
1883  *
1884  * AD1845 has just one oscillator which has cycle time of 10.050 us
1885  * (when a 24.576 MHz xtal oscillator is used).
1886  *
1887  * Convert requested interval to nanoseconds before computing
1888  * the timer divider.
1889  */
1890 
1891   if (devc->mode == MD_1845)
1892     xtal_nsecs = 10050;
1893   else if (ad_read (devc, 8) & 0x01)
1894     xtal_nsecs = 9920;
1895   else
1896     xtal_nsecs = 9969;
1897 
1898   divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
1899 
1900   if (divider < 100)            /* Don't allow shorter intervals than about 1ms */
1901     divider = 100;
1902 
1903   if (divider > 65535)          /* Overflow check */
1904     divider = 65535;
1905 
1906   ad_write (devc, 21, (divider >> 8) & 0xff);   /* Set upper bits */
1907   ad_write (devc, 20, divider & 0xff);  /* Set lower bits */
1908   ad_write (devc, 16, ad_read (devc, 16) | 0x40);       /* Start the timer */
1909   devc->timer_running = 1;
1910   restore_flags (flags);
1911 
1912   return current_interval = (divider * xtal_nsecs + 500) / 1000;
1913 }
1914 
1915 static void
1916 ad1848_tmr_reprogram (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1917 {
1918 /*
1919  *    Audio driver has changed sampling rate so that a different xtal
1920  *      oscillator was selected. We have to reprogram the timer rate.
1921  */
1922 
1923   ad1848_tmr_start (dev, current_interval);
1924   sound_timer_syncinterval (current_interval);
1925 }
1926 
1927 static void
1928 ad1848_tmr_disable (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1929 {
1930   unsigned long   flags;
1931   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1932 
1933   save_flags (flags);
1934   cli ();
1935   ad_write (devc, 16, ad_read (devc, 16) & ~0x40);
1936   devc->timer_running = 0;
1937   restore_flags (flags);
1938 }
1939 
1940 static void
1941 ad1848_tmr_restart (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1942 {
1943   unsigned long   flags;
1944   ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1945 
1946   if (current_interval == 0)
1947     return;
1948 
1949   save_flags (flags);
1950   cli ();
1951   ad_write (devc, 16, ad_read (devc, 16) | 0x40);
1952   devc->timer_running = 1;
1953   restore_flags (flags);
1954 }
1955 
1956 static struct sound_lowlev_timer ad1848_tmr =
1957 {
1958   0,
1959   ad1848_tmr_start,
1960   ad1848_tmr_disable,
1961   ad1848_tmr_restart
1962 };
1963 
1964 static int
1965 ad1848_tmr_install (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1966 {
1967 
1968   if (timer_installed != -1)
1969     return 0;                   /* Don't install another timer */
1970 
1971   timer_installed = ad1848_tmr.dev = dev;
1972   sound_timer_init (&ad1848_tmr, audio_devs[dev]->name);
1973 
1974   return 1;
1975 }
1976 #endif
1977 #endif

/* [previous][next][first][last][top][bottom][index][help] */