root/drivers/sound/dmabuf.c

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

DEFINITIONS

This source file includes following definitions.
  1. reorganize_buffers
  2. dma_init_buffers
  3. open_dmap
  4. close_dmap
  5. DMAbuf_open
  6. dma_reset
  7. dma_reset_output
  8. dma_reset_input
  9. dma_sync
  10. DMAbuf_release
  11. activate_recording
  12. DMAbuf_getrdbuffer
  13. DMAbuf_rmchars
  14. dma_subdivide
  15. dma_set_fragment
  16. get_buffer_pointer
  17. DMAbuf_ioctl
  18. DMAbuf_start_devices
  19. space_in_queue
  20. DMAbuf_getwrbuffer
  21. DMAbuf_get_curr_buffer
  22. DMAbuf_set_count
  23. DMAbuf_start_output
  24. DMAbuf_start_dma
  25. DMAbuf_init
  26. polish_buffers
  27. force_restart
  28. DMAbuf_outputintr
  29. DMAbuf_inputintr
  30. DMAbuf_open_dma
  31. DMAbuf_close_dma
  32. DMAbuf_reset_dma
  33. DMAbuf_select
  34. DMAbuf_open
  35. DMAbuf_release
  36. DMAbuf_getwrbuffer
  37. DMAbuf_getrdbuffer
  38. DMAbuf_rmchars
  39. DMAbuf_start_output
  40. DMAbuf_ioctl
  41. DMAbuf_init
  42. DMAbuf_start_dma
  43. DMAbuf_open_dma
  44. DMAbuf_close_dma
  45. DMAbuf_reset_dma
  46. DMAbuf_inputintr
  47. DMAbuf_outputintr

   1 /*
   2  * sound/dmabuf.c
   3  *
   4  * The DMA buffer manager for digitized voice applications
   5  */
   6 /*
   7  * Copyright by Hannu Savolainen 1993-1996
   8  *
   9  * Redistribution and use in source and binary forms, with or without
  10  * modification, are permitted provided that the following conditions are
  11  * met: 1. Redistributions of source code must retain the above copyright
  12  * notice, this list of conditions and the following disclaimer. 2.
  13  * Redistributions in binary form must reproduce the above copyright notice,
  14  * this list of conditions and the following disclaimer in the documentation
  15  * and/or other materials provided with the distribution.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 #include <linux/config.h>
  30 
  31 
  32 #include "sound_config.h"
  33 
  34 #if defined(CONFIG_AUDIO) || defined(CONFIG_GUS)
  35 
  36 static wait_handle *in_sleeper[MAX_AUDIO_DEV] =
  37 {NULL};
  38 static volatile struct snd_wait in_sleep_flag[MAX_AUDIO_DEV] =
  39 {
  40   {0}};
  41 static wait_handle *out_sleeper[MAX_AUDIO_DEV] =
  42 {NULL};
  43 static volatile struct snd_wait out_sleep_flag[MAX_AUDIO_DEV] =
  44 {
  45   {0}};
  46 
  47 #define NEUTRAL8        0x80
  48 #define NEUTRAL16       0x00
  49 
  50 static int      ndmaps = 0;
  51 
  52 #define MAX_DMAP (MAX_AUDIO_DEV*2)
  53 
  54 static struct dma_buffparms dmaps[MAX_DMAP] =
  55 {
  56   {0}};
  57 
  58 static int      space_in_queue (int dev);
  59 
  60 static void     dma_reset_output (int dev);
  61 static void     dma_reset_input (int dev);
  62 
  63 static void
  64 reorganize_buffers (int dev, struct dma_buffparms *dmap, int recording)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66   /*
  67    * This routine breaks the physical device buffers to logical ones.
  68    */
  69 
  70   struct audio_operations *dsp_dev = audio_devs[dev];
  71 
  72   unsigned        i, n;
  73   unsigned        sr, nc, sz, bsz;
  74 
  75   if (dmap->fragment_size == 0)
  76     {                           /* Compute the fragment size using the default algorithm */
  77 
  78       sr = dsp_dev->ioctl (dev, SOUND_PCM_READ_RATE, 0, 1);
  79       nc = dsp_dev->ioctl (dev, SOUND_PCM_READ_CHANNELS, 0, 1);
  80       sz = dsp_dev->ioctl (dev, SOUND_PCM_READ_BITS, 0, 1);
  81 
  82       if (sz == 8)
  83         dmap->neutral_byte = NEUTRAL8;
  84       else
  85         dmap->neutral_byte = NEUTRAL16;
  86 
  87       if (sr < 1 || nc < 1 || sz < 1)
  88         {
  89           printk ("Warning: Invalid PCM parameters[%d] sr=%d, nc=%d, sz=%d\n",
  90                   dev, sr, nc, sz);
  91           sr = DSP_DEFAULT_SPEED;
  92           nc = 1;
  93           sz = 8;
  94         }
  95 
  96       sz = sr * nc * sz;
  97 
  98       sz /= 8;                  /* #bits -> #bytes */
  99 
 100       /*
 101          * Compute a buffer size for time not exceeding 1 second.
 102          * Usually this algorithm gives a buffer size for 0.5 to 1.0 seconds
 103          * of sound (using the current speed, sample size and #channels).
 104        */
 105 
 106       bsz = dsp_dev->buffsize;
 107       while (bsz > sz)
 108         bsz /= 2;
 109 
 110       if (bsz == dsp_dev->buffsize)
 111         bsz /= 2;               /* Needs at least 2 buffers */
 112 
 113 /*
 114  *    Split the computed fragment to smaller parts. After 3.5a9
 115  *      the default subdivision is 4 which should give better
 116  *      results when recording.
 117  */
 118 
 119       if (dmap->subdivision == 0)       /* Not already set */
 120         {
 121           dmap->subdivision = 1;        /* Init to the default value */
 122 #ifndef V35A9_COMPATIBLE
 123           if (recording)
 124             dmap->subdivision = 4;      /* Use shorter fragments when recording */
 125 #endif
 126         }
 127 
 128       bsz /= dmap->subdivision;
 129 
 130       if (bsz < 16)
 131         bsz = 16;               /* Just a sanity check */
 132 
 133       dmap->fragment_size = bsz;
 134     }
 135   else
 136     {
 137       /*
 138          * The process has specified the buffer size with SNDCTL_DSP_SETFRAGMENT or
 139          * the buffer size computation has already been done.
 140        */
 141       if (dmap->fragment_size > (audio_devs[dev]->buffsize / 2))
 142         dmap->fragment_size = (audio_devs[dev]->buffsize / 2);
 143       bsz = dmap->fragment_size;
 144     }
 145 
 146   bsz &= ~0x03;                 /* Force size which is multiple of 4 bytes */
 147 #ifdef OS_DMA_ALIGN_CHECK
 148   OS_DMA_ALIGN_CHECK (bsz);
 149 #endif
 150 
 151   n = dsp_dev->buffsize / bsz;
 152   if (n > MAX_SUB_BUFFERS)
 153     n = MAX_SUB_BUFFERS;
 154   if (n > dmap->max_fragments)
 155     n = dmap->max_fragments;
 156   dmap->nbufs = n;
 157   dmap->bytes_in_use = n * bsz;
 158 
 159   memset (dmap->raw_buf,
 160           dmap->neutral_byte,
 161           dmap->bytes_in_use);
 162 
 163   for (i = 0; i < dmap->nbufs; i++)
 164     {
 165       dmap->counts[i] = 0;
 166     }
 167 
 168   dmap->flags |= DMA_ALLOC_DONE | DMA_EMPTY;
 169 }
 170 
 171 static void
 172 dma_init_buffers (int dev, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
 173 {
 174   if (dmap == audio_devs[dev]->dmap_out)
 175     {
 176       out_sleep_flag[dev].mode = WK_NONE;
 177     }
 178   else
 179     {
 180       in_sleep_flag[dev].mode = WK_NONE;
 181     }
 182 
 183   dmap->flags = DMA_BUSY;       /* Other flags off */
 184   dmap->qlen = dmap->qhead = dmap->qtail = 0;
 185   dmap->nbufs = 1;
 186   dmap->bytes_in_use = audio_devs[dev]->buffsize;
 187 
 188   dmap->dma_mode = DMODE_NONE;
 189   dmap->mapping_flags = 0;
 190   dmap->neutral_byte = NEUTRAL8;
 191   dmap->cfrag = -1;
 192   dmap->closing = 0;
 193 }
 194 
 195 static int
 196 open_dmap (int dev, int mode, struct dma_buffparms *dmap, int chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198   if (dmap->flags & DMA_BUSY)
 199     return -EBUSY;
 200 
 201   {
 202     int             err;
 203 
 204     if ((err = sound_alloc_dmap (dev, dmap, chan)) < 0)
 205       return err;
 206   }
 207 
 208   if (dmap->raw_buf == NULL)
 209     return -ENOSPC;             /* Memory allocation failed during boot */
 210 
 211   if (sound_open_dma (chan, audio_devs[dev]->name))
 212     {
 213       printk ("Unable to grab(2) DMA%d for the audio driver\n", chan);
 214       return -EBUSY;
 215     }
 216 
 217   dmap->open_mode = mode;
 218   dmap->subdivision = dmap->underrun_count = 0;
 219   dmap->fragment_size = 0;
 220   dmap->max_fragments = 65536;  /* Just a large value */
 221   dmap->byte_counter = 0;
 222 
 223   dma_init_buffers (dev, dmap);
 224 
 225   return 0;
 226 }
 227 
 228 static void
 229 close_dmap (int dev, struct dma_buffparms *dmap, int chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 230 {
 231   sound_close_dma (chan);
 232 
 233   if (dmap->flags & DMA_BUSY)
 234     dmap->dma_mode = DMODE_NONE;
 235   dmap->flags &= ~DMA_BUSY;
 236 
 237   disable_dma (chan);
 238   sound_free_dmap (dev, dmap);
 239 }
 240 
 241 int
 242 DMAbuf_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244   int             retval;
 245   struct dma_buffparms *dmap_in = NULL;
 246   struct dma_buffparms *dmap_out = NULL;
 247 
 248   if (dev >= num_audiodevs)
 249     {
 250       printk ("PCM device %d not installed.\n", dev);
 251       return -ENXIO;
 252     }
 253 
 254   if (!audio_devs[dev])
 255     {
 256       printk ("PCM device %d not initialized\n", dev);
 257       return -ENXIO;
 258     }
 259 
 260   if (!(audio_devs[dev]->flags & DMA_DUPLEX))
 261     {
 262       audio_devs[dev]->dmap_in = audio_devs[dev]->dmap_out;
 263       audio_devs[dev]->dmachan2 = audio_devs[dev]->dmachan1;
 264     }
 265 
 266   if ((retval = audio_devs[dev]->open (dev, mode)) < 0)
 267     return retval;
 268 
 269   dmap_out = audio_devs[dev]->dmap_out;
 270   dmap_in = audio_devs[dev]->dmap_in;
 271 
 272   if ((retval = open_dmap (dev, mode, dmap_out, audio_devs[dev]->dmachan1)) < 0)
 273     {
 274       audio_devs[dev]->close (dev);
 275       return retval;
 276     }
 277 
 278   audio_devs[dev]->enable_bits = mode;
 279   if (audio_devs[dev]->flags & DMA_DUPLEX && dmap_out != dmap_in)
 280     if ((retval = open_dmap (dev, mode, dmap_in, audio_devs[dev]->dmachan2)) < 0)
 281       {
 282         audio_devs[dev]->close (dev);
 283         close_dmap (dev, dmap_out, audio_devs[dev]->dmachan1);
 284         return retval;
 285       }
 286   audio_devs[dev]->open_mode = mode;
 287   audio_devs[dev]->go = 1;
 288   in_sleep_flag[dev].mode = WK_NONE;
 289   out_sleep_flag[dev].mode = WK_NONE;
 290 
 291   audio_devs[dev]->ioctl (dev, SOUND_PCM_WRITE_BITS, (caddr_t) 8, 1);
 292   audio_devs[dev]->ioctl (dev, SOUND_PCM_WRITE_CHANNELS, (caddr_t) 1, 1);
 293   audio_devs[dev]->ioctl (dev, SOUND_PCM_WRITE_RATE, (caddr_t) DSP_DEFAULT_SPEED, 1);
 294 
 295   return 0;
 296 }
 297 
 298 static void
 299 dma_reset (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301   unsigned long   flags;
 302 
 303   save_flags (flags);
 304   cli ();
 305   audio_devs[dev]->reset (dev);
 306   restore_flags (flags);
 307 
 308   dma_reset_output (dev);
 309 
 310   if (audio_devs[dev]->flags & DMA_DUPLEX)
 311     dma_reset_input (dev);
 312 }
 313 
 314 static void
 315 dma_reset_output (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 316 {
 317   unsigned long   flags;
 318 
 319   save_flags (flags);
 320   cli ();
 321   if (!(audio_devs[dev]->flags & DMA_DUPLEX) ||
 322       !audio_devs[dev]->halt_output)
 323     audio_devs[dev]->reset (dev);
 324   else
 325     audio_devs[dev]->halt_output (dev);
 326   restore_flags (flags);
 327 
 328   dma_init_buffers (dev, audio_devs[dev]->dmap_out);
 329   reorganize_buffers (dev, audio_devs[dev]->dmap_out, 0);
 330 }
 331 
 332 static void
 333 dma_reset_input (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 334 {
 335   unsigned long   flags;
 336 
 337   save_flags (flags);
 338   cli ();
 339   if (!(audio_devs[dev]->flags & DMA_DUPLEX) ||
 340       !audio_devs[dev]->halt_input)
 341     audio_devs[dev]->reset (dev);
 342   else
 343     audio_devs[dev]->halt_input (dev);
 344   restore_flags (flags);
 345 
 346   dma_init_buffers (dev, audio_devs[dev]->dmap_in);
 347   reorganize_buffers (dev, audio_devs[dev]->dmap_in, 1);
 348 }
 349 
 350 static int
 351 dma_sync (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 352 {
 353   unsigned long   flags;
 354 
 355   if (!audio_devs[dev]->go && (!audio_devs[dev]->enable_bits & PCM_ENABLE_OUTPUT))
 356     return 0;
 357 
 358   if (audio_devs[dev]->dmap_out->dma_mode == DMODE_OUTPUT)
 359     {
 360       save_flags (flags);
 361       cli ();
 362 
 363       audio_devs[dev]->flags |= DMA_SYNCING;
 364 
 365       audio_devs[dev]->dmap_out->underrun_count = 0;
 366       while (!current_got_fatal_signal ()
 367              && audio_devs[dev]->dmap_out->qlen
 368              && audio_devs[dev]->dmap_out->underrun_count == 0)
 369         {
 370 
 371           {
 372             unsigned long   tl;
 373 
 374             if (HZ)
 375               current_set_timeout (tl = jiffies + (HZ));
 376             else
 377               tl = (unsigned long) -1;
 378             out_sleep_flag[dev].mode = WK_SLEEP;
 379             module_interruptible_sleep_on (&out_sleeper[dev]);
 380             if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
 381               {
 382                 if (jiffies >= tl)
 383                   out_sleep_flag[dev].mode |= WK_TIMEOUT;
 384               }
 385             out_sleep_flag[dev].mode &= ~WK_SLEEP;
 386           };
 387           if ((out_sleep_flag[dev].mode & WK_TIMEOUT))
 388             {
 389               audio_devs[dev]->flags &= ~DMA_SYNCING;
 390               restore_flags (flags);
 391               return audio_devs[dev]->dmap_out->qlen;
 392             }
 393         }
 394       audio_devs[dev]->flags &= ~DMA_SYNCING;
 395       restore_flags (flags);
 396 
 397       /*
 398        * Some devices such as GUS have huge amount of on board RAM for the
 399        * audio data. We have to wait until the device has finished playing.
 400        */
 401 
 402       save_flags (flags);
 403       cli ();
 404       if (audio_devs[dev]->local_qlen)  /* Device has hidden buffers */
 405         {
 406           while (!(current_got_fatal_signal ())
 407                  && audio_devs[dev]->local_qlen (dev))
 408             {
 409 
 410               {
 411                 unsigned long   tl;
 412 
 413                 if (HZ)
 414                   current_set_timeout (tl = jiffies + (HZ));
 415                 else
 416                   tl = (unsigned long) -1;
 417                 out_sleep_flag[dev].mode = WK_SLEEP;
 418                 module_interruptible_sleep_on (&out_sleeper[dev]);
 419                 if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
 420                   {
 421                     if (jiffies >= tl)
 422                       out_sleep_flag[dev].mode |= WK_TIMEOUT;
 423                   }
 424                 out_sleep_flag[dev].mode &= ~WK_SLEEP;
 425               };
 426             }
 427         }
 428       restore_flags (flags);
 429     }
 430   return audio_devs[dev]->dmap_out->qlen;
 431 }
 432 
 433 int
 434 DMAbuf_release (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 435 {
 436   unsigned long   flags;
 437 
 438   audio_devs[dev]->dmap_out->closing = 1;
 439   audio_devs[dev]->dmap_in->closing = 1;
 440 
 441   if (!(current_got_fatal_signal ())
 442       && (audio_devs[dev]->dmap_out->dma_mode == DMODE_OUTPUT))
 443     {
 444       dma_sync (dev);
 445 
 446       memset (audio_devs[dev]->dmap_out->raw_buf,
 447               audio_devs[dev]->dmap_out->neutral_byte,
 448               audio_devs[dev]->dmap_out->bytes_in_use);
 449     }
 450 
 451   save_flags (flags);
 452   cli ();
 453 
 454   audio_devs[dev]->close (dev);
 455 
 456   close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
 457 
 458   if (audio_devs[dev]->flags & DMA_DUPLEX)
 459     close_dmap (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
 460   audio_devs[dev]->open_mode = 0;
 461 
 462   restore_flags (flags);
 463 
 464   return 0;
 465 }
 466 
 467 static int
 468 activate_recording (int dev, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
 469 {
 470   if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT))
 471     return 0;
 472 
 473   if (dmap->flags & DMA_RESTART)
 474     {
 475       dma_reset_input (dev);
 476       dmap->flags &= ~DMA_RESTART;
 477     }
 478 
 479   if (dmap->dma_mode == DMODE_OUTPUT)   /* Direction change */
 480     {
 481       dma_sync (dev);
 482       dma_reset (dev);
 483       dmap->dma_mode = DMODE_NONE;
 484     }
 485 
 486   if (!(dmap->flags & DMA_ALLOC_DONE))
 487     reorganize_buffers (dev, dmap, 1);
 488 
 489   if (!dmap->dma_mode)
 490     {
 491       int             err;
 492 
 493       if ((err = audio_devs[dev]->prepare_for_input (dev,
 494                                      dmap->fragment_size, dmap->nbufs)) < 0)
 495         {
 496           return err;
 497         }
 498       dmap->dma_mode = DMODE_INPUT;
 499     }
 500 
 501   if (!(dmap->flags & DMA_ACTIVE))
 502     {
 503       audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
 504                                     dmap->qtail * dmap->fragment_size,
 505                                     dmap->fragment_size, 0,
 506                                  !(audio_devs[dev]->flags & DMA_AUTOMODE) ||
 507                                     !(dmap->flags & DMA_STARTED));
 508       dmap->flags |= DMA_ACTIVE | DMA_STARTED;
 509       if (audio_devs[dev]->trigger)
 510         audio_devs[dev]->trigger (dev,
 511                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
 512     }
 513   return 0;
 514 }
 515 
 516 int
 517 DMAbuf_getrdbuffer (int dev, char **buf, int *len, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519   unsigned long   flags;
 520   int             err = EIO;
 521   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
 522 
 523   save_flags (flags);
 524   cli ();
 525   if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 526     {
 527       printk ("Sound: Can't read from mmapped device (1)\n");
 528       return -EINVAL;
 529     }
 530   else if (!dmap->qlen)
 531     {
 532       int             tmout;
 533 
 534       if ((err = activate_recording (dev, dmap)) < 0)
 535         {
 536           restore_flags (flags);
 537           return err;
 538         }
 539 
 540       /* Wait for the next block */
 541 
 542       if (dontblock)
 543         {
 544           restore_flags (flags);
 545           return -EAGAIN;
 546         }
 547 
 548       if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT) &
 549           audio_devs[dev]->go)
 550         {
 551           restore_flags (flags);
 552           return -EAGAIN;
 553         }
 554 
 555       if (!audio_devs[dev]->go)
 556         tmout = 0;
 557       else
 558         tmout = 2 * HZ;
 559 
 560 
 561       {
 562         unsigned long   tl;
 563 
 564         if (tmout)
 565           current_set_timeout (tl = jiffies + (tmout));
 566         else
 567           tl = (unsigned long) -1;
 568         in_sleep_flag[dev].mode = WK_SLEEP;
 569         module_interruptible_sleep_on (&in_sleeper[dev]);
 570         if (!(in_sleep_flag[dev].mode & WK_WAKEUP))
 571           {
 572             if (jiffies >= tl)
 573               in_sleep_flag[dev].mode |= WK_TIMEOUT;
 574           }
 575         in_sleep_flag[dev].mode &= ~WK_SLEEP;
 576       };
 577       if ((in_sleep_flag[dev].mode & WK_TIMEOUT))
 578         {
 579           printk ("Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
 580           err = EIO;
 581           audio_devs[dev]->reset (dev);
 582           ;
 583         }
 584       else
 585         err = EINTR;
 586     }
 587   restore_flags (flags);
 588 
 589   if (!dmap->qlen)
 590     return -err;
 591 
 592   *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
 593   *len = dmap->fragment_size - dmap->counts[dmap->qhead];
 594 
 595   return dmap->qhead;
 596 }
 597 
 598 int
 599 DMAbuf_rmchars (int dev, int buff_no, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
 600 {
 601   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
 602 
 603   int             p = dmap->counts[dmap->qhead] + c;
 604 
 605   if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 606     {
 607       printk ("Sound: Can't read from mmapped device (2)\n");
 608       return -EINVAL;
 609     }
 610   else if (p >= dmap->fragment_size)
 611     {                           /* This buffer is completely empty */
 612       dmap->counts[dmap->qhead] = 0;
 613       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
 614         printk ("\nSound: Audio queue1 corrupted for dev%d (%d/%d)\n",
 615                 dev, dmap->qlen, dmap->nbufs);
 616       dmap->qlen--;
 617       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
 618     }
 619   else
 620     dmap->counts[dmap->qhead] = p;
 621 
 622   return 0;
 623 }
 624 
 625 static int
 626 dma_subdivide (int dev, struct dma_buffparms *dmap, caddr_t arg, int fact)
     /* [previous][next][first][last][top][bottom][index][help] */
 627 {
 628   if (fact == 0)
 629     {
 630       fact = dmap->subdivision;
 631       if (fact == 0)
 632         fact = 1;
 633       return snd_ioctl_return ((int *) arg, fact);
 634     }
 635 
 636   if (dmap->subdivision != 0 ||
 637       dmap->fragment_size)      /* Too late to change */
 638     return -EINVAL;
 639 
 640   if (fact > MAX_REALTIME_FACTOR)
 641     return -EINVAL;
 642 
 643   if (fact != 1 && fact != 2 && fact != 4 && fact != 8 && fact != 16)
 644     return -EINVAL;
 645 
 646   dmap->subdivision = fact;
 647   return snd_ioctl_return ((int *) arg, fact);
 648 }
 649 
 650 static int
 651 dma_set_fragment (int dev, struct dma_buffparms *dmap, caddr_t arg, int fact)
     /* [previous][next][first][last][top][bottom][index][help] */
 652 {
 653   int             bytes, count;
 654 
 655   if (fact == 0)
 656     return -EIO;
 657 
 658   if (dmap->subdivision != 0 ||
 659       dmap->fragment_size)      /* Too late to change */
 660     return -EINVAL;
 661 
 662   bytes = fact & 0xffff;
 663   count = (fact >> 16) & 0xffff;
 664 
 665   if (count == 0)
 666     count = MAX_SUB_BUFFERS;
 667 
 668   if (bytes < 4 || bytes > 17)  /* <16 || > 128k */
 669     return -EINVAL;
 670 
 671   if (count < 2)
 672     return -EINVAL;
 673 
 674 #ifdef OS_DMA_MINBITS
 675   if (bytes < OS_DMA_MINBITS)
 676     bytes = OS_DMA_MINBITS;
 677 #endif
 678 
 679   dmap->fragment_size = (1 << bytes);
 680   dmap->max_fragments = count;
 681 
 682   if (dmap->fragment_size > audio_devs[dev]->buffsize)
 683     dmap->fragment_size = audio_devs[dev]->buffsize;
 684 
 685   if (dmap->fragment_size == audio_devs[dev]->buffsize &&
 686       audio_devs[dev]->flags & DMA_AUTOMODE)
 687     dmap->fragment_size /= 2;   /* Needs at least 2 buffers */
 688 
 689   dmap->subdivision = 1;        /* Disable SNDCTL_DSP_SUBDIVIDE */
 690   return snd_ioctl_return ((int *) arg, bytes | (count << 16));
 691 }
 692 
 693 static int
 694 get_buffer_pointer (int dev, int chan, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
 695 {
 696   int             pos;
 697   unsigned long   flags;
 698 
 699   save_flags (flags);
 700   cli ();
 701   if (!(dmap->flags & DMA_ACTIVE))
 702     pos = 0;
 703   else
 704     {
 705       clear_dma_ff (chan);
 706       disable_dma (chan);
 707       pos = get_dma_residue (chan);
 708       enable_dma (chan);
 709     }
 710   restore_flags (flags);
 711   /* printk ("%04x ", pos); */
 712 
 713   if (audio_devs[dev]->flags & DMA_AUTOMODE)
 714     return dmap->bytes_in_use - pos;
 715   else
 716     {
 717       pos = dmap->fragment_size - pos;
 718       if (pos < 0)
 719         return 0;
 720       return pos;
 721     }
 722 }
 723 
 724 
 725 int
 726 DMAbuf_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
 727 {
 728   struct dma_buffparms *dmap_out = audio_devs[dev]->dmap_out;
 729   struct dma_buffparms *dmap_in = audio_devs[dev]->dmap_in;
 730 
 731   switch (cmd)
 732     {
 733     case SNDCTL_DSP_RESET:
 734       dma_reset (dev);
 735       return 0;
 736       break;
 737 
 738     case SNDCTL_DSP_SYNC:
 739       dma_sync (dev);
 740       dma_reset (dev);
 741       return 0;
 742       break;
 743 
 744     case SNDCTL_DSP_GETBLKSIZE:
 745       if (!(dmap_out->flags & DMA_ALLOC_DONE))
 746         {
 747           reorganize_buffers (dev, dmap_out,
 748                               (audio_devs[dev]->open_mode == OPEN_READ));
 749           if (audio_devs[dev]->flags & DMA_DUPLEX)
 750             reorganize_buffers (dev, dmap_in,
 751                                 (audio_devs[dev]->open_mode == OPEN_READ));
 752         }
 753 
 754       return snd_ioctl_return ((int *) arg, dmap_out->fragment_size);
 755       break;
 756 
 757     case SNDCTL_DSP_SUBDIVIDE:
 758       {
 759         int             fact = get_fs_long ((long *) arg);
 760         int             ret;
 761 
 762         ret = dma_subdivide (dev, dmap_out, arg, fact);
 763         if (ret < 0)
 764           return ret;
 765 
 766         if (audio_devs[dev]->flags & DMA_DUPLEX)
 767           ret = dma_subdivide (dev, dmap_in, arg, fact);
 768 
 769         return ret;
 770       }
 771       break;
 772 
 773     case SNDCTL_DSP_SETFRAGMENT:
 774       {
 775         int             fact = get_fs_long ((long *) arg);
 776         int             ret;
 777 
 778         ret = dma_set_fragment (dev, dmap_out, arg, fact);
 779         if (ret < 0)
 780           return ret;
 781 
 782         if (audio_devs[dev]->flags & DMA_DUPLEX)
 783           ret = dma_set_fragment (dev, dmap_in, arg, fact);
 784 
 785         return ret;
 786       }
 787       break;
 788 
 789     case SNDCTL_DSP_GETISPACE:
 790     case SNDCTL_DSP_GETOSPACE:
 791       if (!local)
 792         return -EINVAL;
 793       else
 794         {
 795           struct dma_buffparms *dmap = dmap_out;
 796 
 797           audio_buf_info *info = (audio_buf_info *) arg;
 798 
 799           if (cmd == SNDCTL_DSP_GETISPACE && audio_devs[dev]->flags & DMA_DUPLEX)
 800             dmap = dmap_in;
 801 
 802           if (dmap->mapping_flags & DMA_MAP_MAPPED)
 803             return -EINVAL;
 804 
 805           if (!(dmap->flags & DMA_ALLOC_DONE))
 806             reorganize_buffers (dev, dmap, (cmd == SNDCTL_DSP_GETISPACE));
 807 
 808           info->fragstotal = dmap->nbufs;
 809 
 810           if (cmd == SNDCTL_DSP_GETISPACE)
 811             info->fragments = dmap->qlen;
 812           else
 813             {
 814               if (!space_in_queue (dev))
 815                 info->fragments = 0;
 816               else
 817                 {
 818                   info->fragments = dmap->nbufs - dmap->qlen;
 819                   if (audio_devs[dev]->local_qlen)
 820                     {
 821                       int             tmp = audio_devs[dev]->local_qlen (dev);
 822 
 823                       if (tmp && info->fragments)
 824                         tmp--;  /*
 825                                    * This buffer has been counted twice
 826                                  */
 827                       info->fragments -= tmp;
 828                     }
 829                 }
 830             }
 831 
 832           if (info->fragments < 0)
 833             info->fragments = 0;
 834           else if (info->fragments > dmap->nbufs)
 835             info->fragments = dmap->nbufs;
 836 
 837           info->fragsize = dmap->fragment_size;
 838           info->bytes = info->fragments * dmap->fragment_size;
 839 
 840           if (cmd == SNDCTL_DSP_GETISPACE && dmap->qlen)
 841             info->bytes -= dmap->counts[dmap->qhead];
 842         }
 843       return 0;
 844 
 845     case SNDCTL_DSP_SETTRIGGER:
 846       {
 847         unsigned long   flags;
 848 
 849         int             bits = get_fs_long ((long *) arg) & audio_devs[dev]->open_mode;
 850         int             changed;
 851 
 852         if (audio_devs[dev]->trigger == NULL)
 853           return -EINVAL;
 854 
 855         if (!(audio_devs[dev]->flags & DMA_DUPLEX))
 856           if ((bits & PCM_ENABLE_INPUT) && (bits & PCM_ENABLE_OUTPUT))
 857             {
 858               printk ("Sound: Device doesn't have full duplex capability\n");
 859               return -EINVAL;
 860             }
 861 
 862         save_flags (flags);
 863         cli ();
 864         changed = audio_devs[dev]->enable_bits ^ bits;
 865 
 866         if ((changed & bits) & PCM_ENABLE_INPUT && audio_devs[dev]->go)
 867           {
 868             int             err;
 869 
 870             if (!(dmap_in->flags & DMA_ALLOC_DONE))
 871               {
 872                 reorganize_buffers (dev, dmap_in, 1);
 873               }
 874 
 875             if ((err = audio_devs[dev]->prepare_for_input (dev,
 876                                dmap_in->fragment_size, dmap_in->nbufs)) < 0)
 877               return -err;
 878 
 879             activate_recording (dev, dmap_in);
 880           }
 881 
 882         if ((changed & bits) & PCM_ENABLE_OUTPUT &&
 883             dmap_out->mapping_flags & DMA_MAP_MAPPED &&
 884             audio_devs[dev]->go)
 885           {
 886             int             err;
 887 
 888             if (!(dmap_out->flags & DMA_ALLOC_DONE))
 889               {
 890                 reorganize_buffers (dev, dmap_out, 0);
 891               }
 892 
 893             if ((err = audio_devs[dev]->prepare_for_output (dev,
 894                              dmap_out->fragment_size, dmap_out->nbufs)) < 0)
 895               return -err;
 896 
 897             dmap_out->counts[dmap_out->qhead] = dmap_out->fragment_size;
 898             DMAbuf_start_output (dev, 0, dmap_out->fragment_size);
 899           }
 900 
 901         audio_devs[dev]->enable_bits = bits;
 902         if (changed && audio_devs[dev]->trigger)
 903           audio_devs[dev]->trigger (dev, bits * audio_devs[dev]->go);
 904         restore_flags (flags);
 905       }
 906     case SNDCTL_DSP_GETTRIGGER:
 907       return snd_ioctl_return ((int *) arg, audio_devs[dev]->enable_bits);
 908       break;
 909 
 910     case SNDCTL_DSP_SETSYNCRO:
 911 
 912       if (!audio_devs[dev]->trigger)
 913         return -EINVAL;
 914 
 915       audio_devs[dev]->trigger (dev, 0);
 916       audio_devs[dev]->go = 0;
 917       return 0;
 918       break;
 919 
 920     case SNDCTL_DSP_GETIPTR:
 921       {
 922         count_info      info;
 923         unsigned long   flags;
 924 
 925         save_flags (flags);
 926         cli ();
 927         info.bytes = audio_devs[dev]->dmap_in->byte_counter;
 928         info.ptr = get_buffer_pointer (dev, audio_devs[dev]->dmachan2, audio_devs[dev]->dmap_in);
 929         info.blocks = audio_devs[dev]->dmap_in->qlen;
 930         info.bytes += info.ptr;
 931         memcpy_tofs ((&((char *) arg)[0]), (char *) &info, sizeof (info));
 932 
 933         if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 934           audio_devs[dev]->dmap_in->qlen = 0;   /* Acknowledge interrupts */
 935         restore_flags (flags);
 936         return 0;
 937       }
 938       break;
 939 
 940     case SNDCTL_DSP_GETOPTR:
 941       {
 942         count_info      info;
 943         unsigned long   flags;
 944 
 945         save_flags (flags);
 946         cli ();
 947         info.bytes = audio_devs[dev]->dmap_out->byte_counter;
 948         info.ptr = get_buffer_pointer (dev, audio_devs[dev]->dmachan1, audio_devs[dev]->dmap_out);
 949         info.blocks = audio_devs[dev]->dmap_out->qlen;
 950         info.bytes += info.ptr;
 951         memcpy_tofs ((&((char *) arg)[0]), (char *) &info, sizeof (info));
 952 
 953         if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
 954           audio_devs[dev]->dmap_out->qlen = 0;  /* Acknowledge interrupts */
 955         restore_flags (flags);
 956         return 0;
 957       }
 958       break;
 959 
 960 
 961     default:
 962       return audio_devs[dev]->ioctl (dev, cmd, arg, local);
 963     }
 964 
 965 }
 966 
 967 /*
 968  * DMAbuf_start_devices() is called by the /dev/music driver to start
 969  * one or more audio devices at desired moment.
 970  */
 971 
 972 void
 973 DMAbuf_start_devices (unsigned int devmask)
     /* [previous][next][first][last][top][bottom][index][help] */
 974 {
 975   int             dev;
 976 
 977   for (dev = 0; dev < num_audiodevs; dev++)
 978     if (devmask & (1 << dev))
 979       if (audio_devs[dev]->open_mode != 0)
 980         if (!audio_devs[dev]->go)
 981           {
 982             /* OK to start the device */
 983             audio_devs[dev]->go = 1;
 984 
 985             if (audio_devs[dev]->trigger)
 986               audio_devs[dev]->trigger (dev,
 987                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
 988           }
 989 }
 990 
 991 static int
 992 space_in_queue (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 993 {
 994   int             len, max, tmp;
 995   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
 996 
 997   if (dmap->qlen >= dmap->nbufs)        /* No space at all */
 998     return 0;
 999 
1000   /*
1001      * Verify that there are no more pending buffers than the limit
1002      * defined by the process.
1003    */
1004 
1005   max = dmap->max_fragments;
1006   len = dmap->qlen;
1007 
1008   if (audio_devs[dev]->local_qlen)
1009     {
1010       tmp = audio_devs[dev]->local_qlen (dev);
1011       if (tmp && len)
1012         tmp--;                  /*
1013                                    * This buffer has been counted twice
1014                                  */
1015       len += tmp;
1016     }
1017 
1018   if (len >= max)
1019     return 0;
1020   return 1;
1021 }
1022 
1023 int
1024 DMAbuf_getwrbuffer (int dev, char **buf, int *size, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1025 {
1026   unsigned long   flags;
1027   int             abort, err = EIO;
1028   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1029 
1030   dmap->flags &= ~DMA_CLEAN;
1031 
1032   if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
1033     {
1034       printk ("Sound: Can't write to mmapped device (3)\n");
1035       return -EINVAL;
1036     }
1037 
1038   if (dmap->dma_mode == DMODE_INPUT)    /* Direction change */
1039     {
1040       dma_reset (dev);
1041       dmap->dma_mode = DMODE_NONE;
1042     }
1043   else if (dmap->flags & DMA_RESTART)   /* Restart buffering */
1044     {
1045       dma_sync (dev);
1046       dma_reset_output (dev);
1047     }
1048 
1049   dmap->flags &= ~(DMA_RESTART | DMA_EMPTY);
1050 
1051   if (!(dmap->flags & DMA_ALLOC_DONE))
1052     reorganize_buffers (dev, dmap, 0);
1053 
1054   if (!dmap->dma_mode)
1055     {
1056       int             err;
1057 
1058       dmap->dma_mode = DMODE_OUTPUT;
1059       if ((err = audio_devs[dev]->prepare_for_output (dev,
1060                                      dmap->fragment_size, dmap->nbufs)) < 0)
1061         return err;
1062     }
1063 
1064   save_flags (flags);
1065   cli ();
1066 
1067   abort = 0;
1068   while (!space_in_queue (dev) &&
1069          !abort)
1070     {
1071       int             tmout;
1072 
1073       if (dontblock)
1074         {
1075           restore_flags (flags);
1076           return -EAGAIN;
1077         }
1078 
1079       if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_OUTPUT) &&
1080           audio_devs[dev]->go)
1081         {
1082           restore_flags (flags);
1083           return -EAGAIN;
1084         }
1085 
1086       /*
1087        * Wait for free space
1088        */
1089       if (!audio_devs[dev]->go)
1090         tmout = 0;
1091       else
1092         tmout = 2 * HZ;
1093 
1094 
1095       {
1096         unsigned long   tl;
1097 
1098         if (tmout)
1099           current_set_timeout (tl = jiffies + (tmout));
1100         else
1101           tl = (unsigned long) -1;
1102         out_sleep_flag[dev].mode = WK_SLEEP;
1103         module_interruptible_sleep_on (&out_sleeper[dev]);
1104         if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
1105           {
1106             if (jiffies >= tl)
1107               out_sleep_flag[dev].mode |= WK_TIMEOUT;
1108           }
1109         out_sleep_flag[dev].mode &= ~WK_SLEEP;
1110       };
1111       if ((out_sleep_flag[dev].mode & WK_TIMEOUT))
1112         {
1113           printk ("Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
1114           err = EIO;
1115           abort = 1;
1116           ;
1117           audio_devs[dev]->reset (dev);
1118         }
1119       else if (current_got_fatal_signal ())
1120         {
1121           err = EINTR;
1122           abort = 1;
1123         }
1124     }
1125   restore_flags (flags);
1126 
1127   if (!space_in_queue (dev))
1128     {
1129       return -err;              /* Caught a signal ? */
1130     }
1131 
1132   *buf = dmap->raw_buf + dmap->qtail * dmap->fragment_size;
1133   *size = dmap->fragment_size;
1134   dmap->counts[dmap->qtail] = 0;
1135 
1136   return dmap->qtail;
1137 }
1138 
1139 int
1140 DMAbuf_get_curr_buffer (int dev, int *buf_no, char **dma_buf, int *buf_ptr, int *buf_size)
     /* [previous][next][first][last][top][bottom][index][help] */
1141 {
1142   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1143 
1144   if (dmap->cfrag < 0)
1145     return -1;
1146 
1147   *dma_buf = dmap->raw_buf + dmap->qtail * dmap->fragment_size;
1148   *buf_ptr = dmap->counts[dmap->qtail];
1149   *buf_size = dmap->fragment_size;
1150   return *buf_no = dmap->cfrag;
1151 }
1152 
1153 int
1154 DMAbuf_set_count (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1155 {
1156   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1157 
1158   if (buff_no == dmap->qtail)
1159     {
1160       dmap->cfrag = buff_no;
1161       dmap->counts[buff_no] = l;
1162     }
1163   else
1164     dmap->cfrag = -1;
1165   return 0;
1166 }
1167 
1168 int
1169 DMAbuf_start_output (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1170 {
1171   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1172 
1173   dmap->cfrag = -1;
1174 
1175 /*
1176  * Bypass buffering if using mmapped access
1177  */
1178 
1179   if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
1180     {
1181       l = dmap->fragment_size;
1182       dmap->counts[dmap->qtail] = l;
1183       dmap->flags &= ~DMA_RESTART;
1184       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1185     }
1186   else
1187     {
1188 
1189 
1190       dmap->qlen++;
1191       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1192         printk ("\nSound: Audio queue2 corrupted for dev%d (%d/%d)\n",
1193                 dev, dmap->qlen, dmap->nbufs);
1194 
1195       dmap->counts[dmap->qtail] = l;
1196       if (l < dmap->fragment_size)
1197         {
1198           int             p = dmap->fragment_size * dmap->qtail;
1199 
1200           memset (dmap->raw_buf + p + l,
1201                   dmap->neutral_byte,
1202                   dmap->fragment_size - l);
1203         }
1204 
1205       if ((l != dmap->fragment_size) &&
1206           ((audio_devs[dev]->flags & DMA_AUTOMODE) &&
1207            audio_devs[dev]->flags & NEEDS_RESTART))
1208         dmap->flags |= DMA_RESTART;
1209       else
1210         dmap->flags &= ~DMA_RESTART;
1211 
1212       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1213     }
1214 
1215   if (!(dmap->flags & DMA_ACTIVE))
1216     {
1217       dmap->flags |= DMA_ACTIVE;
1218       audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1219                                      dmap->qhead * dmap->fragment_size,
1220                                      dmap->counts[dmap->qhead], 0,
1221                                  !(audio_devs[dev]->flags & DMA_AUTOMODE) ||
1222                                      !(dmap->flags & DMA_STARTED));
1223       dmap->flags |= DMA_STARTED;
1224       if (audio_devs[dev]->trigger)
1225         audio_devs[dev]->trigger (dev,
1226                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1227     }
1228 
1229   return 0;
1230 }
1231 
1232 int
1233 DMAbuf_start_dma (int dev, unsigned long physaddr, int count, int dma_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1234 {
1235   int             chan;
1236   struct dma_buffparms *dmap;
1237   unsigned long   flags;
1238 
1239   if (dma_mode == DMA_MODE_WRITE)
1240     {
1241       chan = audio_devs[dev]->dmachan1;
1242       dmap = audio_devs[dev]->dmap_out;
1243     }
1244   else
1245     {
1246       chan = audio_devs[dev]->dmachan2;
1247       dmap = audio_devs[dev]->dmap_in;
1248     }
1249 
1250   /*
1251    * The count must be one less than the actual size. This is handled by
1252    * set_dma_addr()
1253    */
1254 
1255   if (audio_devs[dev]->flags & DMA_AUTOMODE)
1256     {                           /*
1257                                  * Auto restart mode. Transfer the whole *
1258                                  * buffer
1259                                  */
1260       save_flags (flags);
1261       cli ();
1262       disable_dma (chan);
1263       clear_dma_ff (chan);
1264       set_dma_mode (chan, dma_mode | DMA_AUTOINIT);
1265       set_dma_addr (chan, dmap->raw_buf_phys);
1266       set_dma_count (chan, dmap->bytes_in_use);
1267       enable_dma (chan);
1268       restore_flags (flags);
1269     }
1270   else
1271     {
1272       save_flags (flags);
1273       cli ();
1274       disable_dma (chan);
1275       clear_dma_ff (chan);
1276       set_dma_mode (chan, dma_mode);
1277       set_dma_addr (chan, physaddr);
1278       set_dma_count (chan, count);
1279       enable_dma (chan);
1280       restore_flags (flags);
1281     }
1282 
1283   return count;
1284 }
1285 
1286 long
1287 DMAbuf_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1288 {
1289   int             dev;
1290 
1291 
1292   /*
1293      * NOTE! This routine could be called several times.
1294    */
1295 
1296   for (dev = 0; dev < num_audiodevs; dev++)
1297     if (audio_devs[dev]->dmap_out == NULL)
1298       {
1299         audio_devs[dev]->dmap_out =
1300           audio_devs[dev]->dmap_in =
1301           &dmaps[ndmaps++];
1302 
1303         if (audio_devs[dev]->flags & DMA_DUPLEX)
1304           audio_devs[dev]->dmap_in =
1305             &dmaps[ndmaps++];
1306       }
1307   return mem_start;
1308 }
1309 
1310 static void
1311 polish_buffers (struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
1312 {
1313   int             i;
1314   int             p, l;
1315 
1316   i = dmap->qhead;
1317 
1318   p = dmap->fragment_size * i;
1319 
1320   if (i == dmap->cfrag)
1321     {
1322       l = dmap->fragment_size - dmap->counts[i];
1323     }
1324   else
1325     l = dmap->fragment_size;
1326 
1327   if (l)
1328     {
1329       memset (dmap->raw_buf + p,
1330               dmap->neutral_byte,
1331               l);
1332     }
1333 }
1334 
1335 static void
1336 force_restart (int dev, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
1337 {
1338   if ((audio_devs[dev]->flags & DMA_DUPLEX) &&
1339       audio_devs[dev]->halt_output)
1340     audio_devs[dev]->halt_output (dev);
1341   else
1342     audio_devs[dev]->halt_xfer (dev);
1343 
1344   dmap->flags &= ~DMA_ACTIVE;
1345   if (audio_devs[dev]->flags & DMA_AUTOMODE)
1346     dmap->flags |= DMA_RESTART;
1347   else
1348     dmap->flags &= ~DMA_RESTART;
1349 }
1350 
1351 void
1352 DMAbuf_outputintr (int dev, int event_type)
     /* [previous][next][first][last][top][bottom][index][help] */
1353 {
1354   /*
1355      * Event types:
1356      *  0 = DMA transfer done. Device still has more data in the local
1357      *      buffer.
1358      *  1 = DMA transfer done. Device doesn't have local buffer or it's
1359      *      empty now.
1360      *  2 = No DMA transfer but the device has now more space in it's local
1361      *      buffer.
1362    */
1363 
1364   unsigned long   flags;
1365   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1366 
1367   dmap->byte_counter += dmap->counts[dmap->qhead];
1368 
1369 #ifdef OS_DMA_INTR
1370   sound_dma_intr (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1371 #endif
1372 
1373   if (dmap->mapping_flags & DMA_MAP_MAPPED)
1374     {
1375       /* mmapped access */
1376 
1377       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1378       dmap->qlen++;             /* Yes increment it (don't decrement) */
1379       dmap->flags &= ~DMA_ACTIVE;
1380       dmap->counts[dmap->qhead] = dmap->fragment_size;
1381 
1382       if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1383         {
1384           audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1385                                          dmap->qhead * dmap->fragment_size,
1386                                          dmap->counts[dmap->qhead], 1,
1387                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1388           if (audio_devs[dev]->trigger)
1389             audio_devs[dev]->trigger (dev,
1390                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1391         }
1392       dmap->flags |= DMA_ACTIVE;
1393     }
1394   else if (event_type != 2)
1395     {
1396       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1397         {
1398           printk ("\nSound: Audio queue3 corrupted for dev%d (%d/%d)\n",
1399                   dev, dmap->qlen, dmap->nbufs);
1400           return;
1401         }
1402 
1403       dmap->qlen--;
1404       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1405       dmap->flags &= ~DMA_ACTIVE;
1406 
1407       if (event_type == 1 && dmap->qlen < 1)
1408         {
1409           dmap->underrun_count++;
1410 
1411           if (!(dmap->flags & DMA_CLEAN) &&
1412               (audio_devs[dev]->flags & DMA_SYNCING || dmap->underrun_count > 5 || dmap->flags & DMA_EMPTY))
1413             {
1414               dmap->qlen = 0;
1415               force_restart (dev, dmap);
1416             }
1417           else
1418             /* Ignore underrun. Just move the tail pointer forward and go */
1419           if (dmap->closing)
1420             {
1421               polish_buffers (dmap);
1422               audio_devs[dev]->halt_xfer (dev);
1423             }
1424           else
1425             {
1426               dmap->qlen++;
1427               dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1428 
1429               if (!(dmap->flags & DMA_EMPTY))
1430                 polish_buffers (dmap);
1431 
1432               dmap->cfrag = -1;
1433               dmap->flags |= DMA_EMPTY;
1434               dmap->counts[dmap->qtail] = dmap->fragment_size;
1435             }
1436         }
1437 
1438       if (dmap->qlen)
1439         {
1440           if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1441             {
1442 
1443               if (dmap->counts[dmap->qhead] == 0)
1444                 dmap->counts[dmap->qhead] = dmap->fragment_size;
1445 
1446               audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1447                                           dmap->qhead * dmap->fragment_size,
1448                                              dmap->counts[dmap->qhead], 1,
1449                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1450               if (audio_devs[dev]->trigger)
1451                 audio_devs[dev]->trigger (dev,
1452                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1453             }
1454           dmap->flags |= DMA_ACTIVE;
1455         }
1456     }                           /* event_type != 2 */
1457 
1458   save_flags (flags);
1459   cli ();
1460   if ((out_sleep_flag[dev].mode & WK_SLEEP))
1461     {
1462       {
1463         out_sleep_flag[dev].mode = WK_WAKEUP;
1464         module_wake_up (&out_sleeper[dev]);
1465       };
1466     }
1467   restore_flags (flags);
1468 }
1469 
1470 void
1471 DMAbuf_inputintr (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1472 {
1473   unsigned long   flags;
1474   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
1475 
1476   dmap->byte_counter += dmap->fragment_size;
1477 
1478 #ifdef OS_DMA_INTR
1479   sound_dma_intr (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
1480 #endif
1481 
1482   if (dmap->mapping_flags & DMA_MAP_MAPPED)
1483     {
1484       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1485       dmap->qlen++;
1486 
1487       if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1488         {
1489           audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
1490                                         dmap->qtail * dmap->fragment_size,
1491                                         dmap->fragment_size, 1,
1492                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1493           if (audio_devs[dev]->trigger)
1494             audio_devs[dev]->trigger (dev,
1495                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1496         }
1497 
1498       dmap->flags |= DMA_ACTIVE;
1499     }
1500   else if (dmap->qlen == (dmap->nbufs - 1))
1501     {
1502       printk ("Sound: Recording overrun\n");
1503       dmap->underrun_count++;
1504 
1505       if (audio_devs[dev]->flags & DMA_AUTOMODE)
1506         {
1507           /* Force restart on next read */
1508           if ((audio_devs[dev]->flags & DMA_DUPLEX) &&
1509               audio_devs[dev]->halt_input)
1510             audio_devs[dev]->halt_input (dev);
1511           else
1512             audio_devs[dev]->halt_xfer (dev);
1513 
1514           dmap->flags &= ~DMA_ACTIVE;
1515           if (audio_devs[dev]->flags & DMA_AUTOMODE)
1516             dmap->flags |= DMA_RESTART;
1517           else
1518             dmap->flags &= ~DMA_RESTART;
1519         }
1520     }
1521   else
1522     {
1523       dmap->qlen++;
1524       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1525         printk ("\nSound: Audio queue4 corrupted for dev%d (%d/%d)\n",
1526                 dev, dmap->qlen, dmap->nbufs);
1527       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1528     }
1529 
1530   if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1531     {
1532       audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
1533                                     dmap->qtail * dmap->fragment_size,
1534                                     dmap->fragment_size, 1,
1535                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1536       if (audio_devs[dev]->trigger)
1537         audio_devs[dev]->trigger (dev,
1538                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1539     }
1540 
1541   dmap->flags |= DMA_ACTIVE;
1542 
1543   save_flags (flags);
1544   cli ();
1545   if ((in_sleep_flag[dev].mode & WK_SLEEP))
1546     {
1547       {
1548         in_sleep_flag[dev].mode = WK_WAKEUP;
1549         module_wake_up (&in_sleeper[dev]);
1550       };
1551     }
1552   restore_flags (flags);
1553 }
1554 
1555 int
1556 DMAbuf_open_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1557 {
1558   int             chan = audio_devs[dev]->dmachan1;
1559   int             err;
1560   unsigned long   flags;
1561 
1562   if ((err = open_dmap (dev, OPEN_READWRITE, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1)) < 0)
1563     {
1564       return -EBUSY;
1565     }
1566   dma_init_buffers (dev, audio_devs[dev]->dmap_out);
1567   audio_devs[dev]->dmap_out->flags |= DMA_ALLOC_DONE;
1568   audio_devs[dev]->dmap_out->fragment_size = audio_devs[dev]->buffsize;
1569   /* reorganize_buffers (dev, audio_devs[dev]->dmap_out); */
1570 
1571   if (audio_devs[dev]->flags & DMA_DUPLEX)
1572     {
1573       if ((err = open_dmap (dev, OPEN_READWRITE, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2)) < 0)
1574         {
1575           printk ("Unable to grab DMA%d for the audio driver\n",
1576                   audio_devs[dev]->dmachan2);
1577           close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1578           return -EBUSY;
1579         }
1580       dma_init_buffers (dev, audio_devs[dev]->dmap_in);
1581       audio_devs[dev]->dmap_in->flags |= DMA_ALLOC_DONE;
1582       audio_devs[dev]->dmap_in->fragment_size = audio_devs[dev]->buffsize;
1583       /* reorganize_buffers (dev, audio_devs[dev]->dmap_in); */
1584     }
1585   else
1586     {
1587       audio_devs[dev]->dmap_in = audio_devs[dev]->dmap_out;
1588       audio_devs[dev]->dmachan2 = audio_devs[dev]->dmachan1;
1589     }
1590 
1591 
1592   save_flags (flags);
1593   cli ();
1594   disable_dma (chan);
1595   clear_dma_ff (chan);
1596   restore_flags (flags);
1597 
1598   return 0;
1599 }
1600 
1601 void
1602 DMAbuf_close_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1603 {
1604   DMAbuf_reset_dma (dev);
1605   close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1606 
1607   if (audio_devs[dev]->flags & DMA_DUPLEX)
1608     close_dmap (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
1609 
1610 }
1611 
1612 void
1613 DMAbuf_reset_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1614 {
1615 }
1616 
1617 int
1618 DMAbuf_select (int dev, struct fileinfo *file, int sel_type, select_table_handle * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1619 {
1620   struct dma_buffparms *dmap;
1621   unsigned long   flags;
1622 
1623   switch (sel_type)
1624     {
1625     case SEL_IN:
1626       dmap = audio_devs[dev]->dmap_in;
1627 
1628       if (dmap->mapping_flags & DMA_MAP_MAPPED)
1629         {
1630           if (dmap->qlen)
1631             return 1;
1632 
1633           save_flags (flags);
1634           cli ();
1635 
1636           in_sleep_flag[dev].mode = WK_SLEEP;
1637           module_select_wait (&in_sleeper[dev], wait);
1638           restore_flags (flags);
1639           return 0;
1640         }
1641 
1642       if (dmap->dma_mode != DMODE_INPUT)
1643         {
1644           if ((audio_devs[dev]->flags & DMA_DUPLEX) && !dmap->qlen &&
1645               audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT &&
1646               audio_devs[dev]->go)
1647             {
1648               unsigned long   flags;
1649 
1650               save_flags (flags);
1651               cli ();
1652               activate_recording (dev, dmap);
1653               restore_flags (flags);
1654             }
1655           return 0;
1656         }
1657 
1658       if (!dmap->qlen)
1659         {
1660           save_flags (flags);
1661           cli ();
1662 
1663           in_sleep_flag[dev].mode = WK_SLEEP;
1664           module_select_wait (&in_sleeper[dev], wait);
1665           restore_flags (flags);
1666           return 0;
1667         }
1668       return 1;
1669       break;
1670 
1671     case SEL_OUT:
1672       dmap = audio_devs[dev]->dmap_out;
1673 
1674       if (dmap->mapping_flags & DMA_MAP_MAPPED)
1675         {
1676           if (dmap->qlen)
1677             return 1;
1678 
1679           save_flags (flags);
1680           cli ();
1681 
1682           out_sleep_flag[dev].mode = WK_SLEEP;
1683           module_select_wait (&out_sleeper[dev], wait);
1684           restore_flags (flags);
1685           return 0;
1686         }
1687 
1688       if (dmap->dma_mode == DMODE_INPUT)
1689         {
1690           return 0;
1691         }
1692 
1693       if (dmap->dma_mode == DMODE_NONE)
1694         {
1695           return 1;
1696         }
1697 
1698       if (!space_in_queue (dev))
1699         {
1700           save_flags (flags);
1701           cli ();
1702 
1703           out_sleep_flag[dev].mode = WK_SLEEP;
1704           module_select_wait (&out_sleeper[dev], wait);
1705           restore_flags (flags);
1706           return 0;
1707         }
1708       return 1;
1709       break;
1710 
1711     case SEL_EX:
1712       return 0;
1713     }
1714 
1715   return 0;
1716 }
1717 
1718 
1719 #else /* CONFIG_AUDIO */
1720 /*
1721  * Stub versions if audio services not included
1722  */
1723 
1724 int
1725 DMAbuf_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1726 {
1727   return -ENXIO;
1728 }
1729 
1730 int
1731 DMAbuf_release (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1732 {
1733   return 0;
1734 }
1735 
1736 int
1737 DMAbuf_getwrbuffer (int dev, char **buf, int *size, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1738 {
1739   return -EIO;
1740 }
1741 
1742 int
1743 DMAbuf_getrdbuffer (int dev, char **buf, int *len, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1744 {
1745   return -EIO;
1746 }
1747 
1748 int
1749 DMAbuf_rmchars (int dev, int buff_no, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
1750 {
1751   return -EIO;
1752 }
1753 
1754 int
1755 DMAbuf_start_output (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1756 {
1757   return -EIO;
1758 }
1759 
1760 int
1761 DMAbuf_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
1762 {
1763   return -EIO;
1764 }
1765 
1766 long
1767 DMAbuf_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1768 {
1769   return mem_start;
1770 }
1771 
1772 int
1773 DMAbuf_start_dma (int dev, unsigned long physaddr, int count, int dma_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1774 {
1775   return -EIO;
1776 }
1777 
1778 int
1779 DMAbuf_open_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1780 {
1781   return -ENXIO;
1782 }
1783 
1784 void
1785 DMAbuf_close_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1786 {
1787   return;
1788 }
1789 
1790 void
1791 DMAbuf_reset_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1792 {
1793   return;
1794 }
1795 
1796 void
1797 DMAbuf_inputintr (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1798 {
1799   return;
1800 }
1801 
1802 void
1803 DMAbuf_outputintr (int dev, int underrun_flag)
     /* [previous][next][first][last][top][bottom][index][help] */
1804 {
1805   return;
1806 }
1807 #endif

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