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_SETDUPLEX:
 774       if (audio_devs[dev]->flags & DMA_DUPLEX)
 775          return 0;
 776       else
 777          return -EIO;
 778       break;
 779 
 780     case SNDCTL_DSP_SETFRAGMENT:
 781       {
 782         int             fact = get_fs_long ((long *) arg);
 783         int             ret;
 784 
 785         ret = dma_set_fragment (dev, dmap_out, arg, fact);
 786         if (ret < 0)
 787           return ret;
 788 
 789         if (audio_devs[dev]->flags & DMA_DUPLEX)
 790           ret = dma_set_fragment (dev, dmap_in, arg, fact);
 791 
 792         return ret;
 793       }
 794       break;
 795 
 796     case SNDCTL_DSP_GETISPACE:
 797     case SNDCTL_DSP_GETOSPACE:
 798       if (!local)
 799         return -EINVAL;
 800       else
 801         {
 802           struct dma_buffparms *dmap = dmap_out;
 803 
 804           audio_buf_info *info = (audio_buf_info *) arg;
 805 
 806           if (cmd == SNDCTL_DSP_GETISPACE && audio_devs[dev]->flags & DMA_DUPLEX)
 807             dmap = dmap_in;
 808 
 809           if (dmap->mapping_flags & DMA_MAP_MAPPED)
 810             return -EINVAL;
 811 
 812           if (!(dmap->flags & DMA_ALLOC_DONE))
 813             reorganize_buffers (dev, dmap, (cmd == SNDCTL_DSP_GETISPACE));
 814 
 815           info->fragstotal = dmap->nbufs;
 816 
 817           if (cmd == SNDCTL_DSP_GETISPACE)
 818             info->fragments = dmap->qlen;
 819           else
 820             {
 821               if (!space_in_queue (dev))
 822                 info->fragments = 0;
 823               else
 824                 {
 825                   info->fragments = dmap->nbufs - dmap->qlen;
 826                   if (audio_devs[dev]->local_qlen)
 827                     {
 828                       int             tmp = audio_devs[dev]->local_qlen (dev);
 829 
 830                       if (tmp && info->fragments)
 831                         tmp--;  /*
 832                                    * This buffer has been counted twice
 833                                  */
 834                       info->fragments -= tmp;
 835                     }
 836                 }
 837             }
 838 
 839           if (info->fragments < 0)
 840             info->fragments = 0;
 841           else if (info->fragments > dmap->nbufs)
 842             info->fragments = dmap->nbufs;
 843 
 844           info->fragsize = dmap->fragment_size;
 845           info->bytes = info->fragments * dmap->fragment_size;
 846 
 847           if (cmd == SNDCTL_DSP_GETISPACE && dmap->qlen)
 848             info->bytes -= dmap->counts[dmap->qhead];
 849         }
 850       return 0;
 851 
 852     case SNDCTL_DSP_SETTRIGGER:
 853       {
 854         unsigned long   flags;
 855 
 856         int             bits = get_fs_long ((long *) arg) & audio_devs[dev]->open_mode;
 857         int             changed;
 858 
 859         if (audio_devs[dev]->trigger == NULL)
 860           return -EINVAL;
 861 
 862         if (!(audio_devs[dev]->flags & DMA_DUPLEX))
 863           if ((bits & PCM_ENABLE_INPUT) && (bits & PCM_ENABLE_OUTPUT))
 864             {
 865               printk ("Sound: Device doesn't have full duplex capability\n");
 866               return -EINVAL;
 867             }
 868 
 869         save_flags (flags);
 870         cli ();
 871         changed = audio_devs[dev]->enable_bits ^ bits;
 872 
 873         if ((changed & bits) & PCM_ENABLE_INPUT && audio_devs[dev]->go)
 874           {
 875             int             err;
 876 
 877             if (!(dmap_in->flags & DMA_ALLOC_DONE))
 878               {
 879                 reorganize_buffers (dev, dmap_in, 1);
 880               }
 881 
 882             if ((err = audio_devs[dev]->prepare_for_input (dev,
 883                                dmap_in->fragment_size, dmap_in->nbufs)) < 0)
 884               return -err;
 885 
 886             activate_recording (dev, dmap_in);
 887           }
 888 
 889         if ((changed & bits) & PCM_ENABLE_OUTPUT &&
 890             dmap_out->mapping_flags & DMA_MAP_MAPPED &&
 891             audio_devs[dev]->go)
 892           {
 893             int             err;
 894 
 895             if (!(dmap_out->flags & DMA_ALLOC_DONE))
 896               {
 897                 reorganize_buffers (dev, dmap_out, 0);
 898               }
 899 
 900             if ((err = audio_devs[dev]->prepare_for_output (dev,
 901                              dmap_out->fragment_size, dmap_out->nbufs)) < 0)
 902               return -err;
 903 
 904             dmap_out->counts[dmap_out->qhead] = dmap_out->fragment_size;
 905             DMAbuf_start_output (dev, 0, dmap_out->fragment_size);
 906           }
 907 
 908         audio_devs[dev]->enable_bits = bits;
 909         if (changed && audio_devs[dev]->trigger)
 910           audio_devs[dev]->trigger (dev, bits * audio_devs[dev]->go);
 911         restore_flags (flags);
 912       }
 913     case SNDCTL_DSP_GETTRIGGER:
 914       return snd_ioctl_return ((int *) arg, audio_devs[dev]->enable_bits);
 915       break;
 916 
 917     case SNDCTL_DSP_SETSYNCRO:
 918 
 919       if (!audio_devs[dev]->trigger)
 920         return -EINVAL;
 921 
 922       audio_devs[dev]->trigger (dev, 0);
 923       audio_devs[dev]->go = 0;
 924       return 0;
 925       break;
 926 
 927     case SNDCTL_DSP_GETIPTR:
 928       {
 929         count_info      info;
 930         unsigned long   flags;
 931 
 932         save_flags (flags);
 933         cli ();
 934         info.bytes = audio_devs[dev]->dmap_in->byte_counter;
 935         info.ptr = get_buffer_pointer (dev, audio_devs[dev]->dmachan2, audio_devs[dev]->dmap_in);
 936         info.blocks = audio_devs[dev]->dmap_in->qlen;
 937         info.bytes += info.ptr;
 938         memcpy_tofs ((&((char *) arg)[0]), (char *) &info, sizeof (info));
 939 
 940         if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 941           audio_devs[dev]->dmap_in->qlen = 0;   /* Acknowledge interrupts */
 942         restore_flags (flags);
 943         return 0;
 944       }
 945       break;
 946 
 947     case SNDCTL_DSP_GETOPTR:
 948       {
 949         count_info      info;
 950         unsigned long   flags;
 951 
 952         save_flags (flags);
 953         cli ();
 954         info.bytes = audio_devs[dev]->dmap_out->byte_counter;
 955         info.ptr = get_buffer_pointer (dev, audio_devs[dev]->dmachan1, audio_devs[dev]->dmap_out);
 956         info.blocks = audio_devs[dev]->dmap_out->qlen;
 957         info.bytes += info.ptr;
 958         memcpy_tofs ((&((char *) arg)[0]), (char *) &info, sizeof (info));
 959 
 960         if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
 961           audio_devs[dev]->dmap_out->qlen = 0;  /* Acknowledge interrupts */
 962         restore_flags (flags);
 963         return 0;
 964       }
 965       break;
 966 
 967 
 968     default:
 969       return audio_devs[dev]->ioctl (dev, cmd, arg, local);
 970     }
 971 
 972 }
 973 
 974 /*
 975  * DMAbuf_start_devices() is called by the /dev/music driver to start
 976  * one or more audio devices at desired moment.
 977  */
 978 
 979 void
 980 DMAbuf_start_devices (unsigned int devmask)
     /* [previous][next][first][last][top][bottom][index][help] */
 981 {
 982   int             dev;
 983 
 984   for (dev = 0; dev < num_audiodevs; dev++)
 985     if (devmask & (1 << dev))
 986       if (audio_devs[dev]->open_mode != 0)
 987         if (!audio_devs[dev]->go)
 988           {
 989             /* OK to start the device */
 990             audio_devs[dev]->go = 1;
 991 
 992             if (audio_devs[dev]->trigger)
 993               audio_devs[dev]->trigger (dev,
 994                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
 995           }
 996 }
 997 
 998 static int
 999 space_in_queue (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1000 {
1001   int             len, max, tmp;
1002   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1003 
1004   if (dmap->qlen >= dmap->nbufs)        /* No space at all */
1005     return 0;
1006 
1007   /*
1008      * Verify that there are no more pending buffers than the limit
1009      * defined by the process.
1010    */
1011 
1012   max = dmap->max_fragments;
1013   len = dmap->qlen;
1014 
1015   if (audio_devs[dev]->local_qlen)
1016     {
1017       tmp = audio_devs[dev]->local_qlen (dev);
1018       if (tmp && len)
1019         tmp--;                  /*
1020                                    * This buffer has been counted twice
1021                                  */
1022       len += tmp;
1023     }
1024 
1025   if (len >= max)
1026     return 0;
1027   return 1;
1028 }
1029 
1030 int
1031 DMAbuf_getwrbuffer (int dev, char **buf, int *size, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1032 {
1033   unsigned long   flags;
1034   int             abort, err = EIO;
1035   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1036 
1037   dmap->flags &= ~DMA_CLEAN;
1038 
1039   if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
1040     {
1041       printk ("Sound: Can't write to mmapped device (3)\n");
1042       return -EINVAL;
1043     }
1044 
1045   if (dmap->dma_mode == DMODE_INPUT)    /* Direction change */
1046     {
1047       dma_reset (dev);
1048       dmap->dma_mode = DMODE_NONE;
1049     }
1050   else if (dmap->flags & DMA_RESTART)   /* Restart buffering */
1051     {
1052       dma_sync (dev);
1053       dma_reset_output (dev);
1054     }
1055 
1056   dmap->flags &= ~(DMA_RESTART | DMA_EMPTY);
1057 
1058   if (!(dmap->flags & DMA_ALLOC_DONE))
1059     reorganize_buffers (dev, dmap, 0);
1060 
1061   if (!dmap->dma_mode)
1062     {
1063       int             err;
1064 
1065       dmap->dma_mode = DMODE_OUTPUT;
1066       if ((err = audio_devs[dev]->prepare_for_output (dev,
1067                                      dmap->fragment_size, dmap->nbufs)) < 0)
1068         return err;
1069     }
1070 
1071   save_flags (flags);
1072   cli ();
1073 
1074   abort = 0;
1075   while (!space_in_queue (dev) &&
1076          !abort)
1077     {
1078       int             tmout;
1079 
1080       if (dontblock)
1081         {
1082           restore_flags (flags);
1083           return -EAGAIN;
1084         }
1085 
1086       if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_OUTPUT) &&
1087           audio_devs[dev]->go)
1088         {
1089           restore_flags (flags);
1090           return -EAGAIN;
1091         }
1092 
1093       /*
1094        * Wait for free space
1095        */
1096       if (!audio_devs[dev]->go)
1097         tmout = 0;
1098       else
1099         tmout = 2 * HZ;
1100 
1101 
1102       {
1103         unsigned long   tl;
1104 
1105         if (tmout)
1106           current_set_timeout (tl = jiffies + (tmout));
1107         else
1108           tl = (unsigned long) -1;
1109         out_sleep_flag[dev].mode = WK_SLEEP;
1110         module_interruptible_sleep_on (&out_sleeper[dev]);
1111         if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
1112           {
1113             if (jiffies >= tl)
1114               out_sleep_flag[dev].mode |= WK_TIMEOUT;
1115           }
1116         out_sleep_flag[dev].mode &= ~WK_SLEEP;
1117       };
1118       if ((out_sleep_flag[dev].mode & WK_TIMEOUT))
1119         {
1120           printk ("Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
1121           err = EIO;
1122           abort = 1;
1123           ;
1124           audio_devs[dev]->reset (dev);
1125         }
1126       else if (current_got_fatal_signal ())
1127         {
1128           err = EINTR;
1129           abort = 1;
1130         }
1131     }
1132   restore_flags (flags);
1133 
1134   if (!space_in_queue (dev))
1135     {
1136       return -err;              /* Caught a signal ? */
1137     }
1138 
1139   *buf = dmap->raw_buf + dmap->qtail * dmap->fragment_size;
1140   *size = dmap->fragment_size;
1141   dmap->counts[dmap->qtail] = 0;
1142 
1143   return dmap->qtail;
1144 }
1145 
1146 int
1147 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] */
1148 {
1149   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1150 
1151   if (dmap->cfrag < 0)
1152     return -1;
1153 
1154   *dma_buf = dmap->raw_buf + dmap->qtail * dmap->fragment_size;
1155   *buf_ptr = dmap->counts[dmap->qtail];
1156   *buf_size = dmap->fragment_size;
1157   return *buf_no = dmap->cfrag;
1158 }
1159 
1160 int
1161 DMAbuf_set_count (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1162 {
1163   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1164 
1165   if (buff_no == dmap->qtail)
1166     {
1167       dmap->cfrag = buff_no;
1168       dmap->counts[buff_no] = l;
1169     }
1170   else
1171     dmap->cfrag = -1;
1172   return 0;
1173 }
1174 
1175 int
1176 DMAbuf_start_output (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1177 {
1178   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1179 
1180   dmap->cfrag = -1;
1181 
1182 /*
1183  * Bypass buffering if using mmapped access
1184  */
1185 
1186   if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
1187     {
1188       l = dmap->fragment_size;
1189       dmap->counts[dmap->qtail] = l;
1190       dmap->flags &= ~DMA_RESTART;
1191       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1192     }
1193   else
1194     {
1195 
1196 
1197       dmap->qlen++;
1198       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1199         printk ("\nSound: Audio queue2 corrupted for dev%d (%d/%d)\n",
1200                 dev, dmap->qlen, dmap->nbufs);
1201 
1202       dmap->counts[dmap->qtail] = l;
1203       if (l < dmap->fragment_size)
1204         {
1205           int             p = dmap->fragment_size * dmap->qtail;
1206 
1207           memset (dmap->raw_buf + p + l,
1208                   dmap->neutral_byte,
1209                   dmap->fragment_size - l);
1210         }
1211 
1212       if ((l != dmap->fragment_size) &&
1213           ((audio_devs[dev]->flags & DMA_AUTOMODE) &&
1214            audio_devs[dev]->flags & NEEDS_RESTART))
1215         dmap->flags |= DMA_RESTART;
1216       else
1217         dmap->flags &= ~DMA_RESTART;
1218 
1219       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1220     }
1221 
1222   if (!(dmap->flags & DMA_ACTIVE))
1223     {
1224       dmap->flags |= DMA_ACTIVE;
1225       audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1226                                      dmap->qhead * dmap->fragment_size,
1227                                      dmap->counts[dmap->qhead], 0,
1228                                  !(audio_devs[dev]->flags & DMA_AUTOMODE) ||
1229                                      !(dmap->flags & DMA_STARTED));
1230       dmap->flags |= DMA_STARTED;
1231       if (audio_devs[dev]->trigger)
1232         audio_devs[dev]->trigger (dev,
1233                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1234     }
1235 
1236   return 0;
1237 }
1238 
1239 int
1240 DMAbuf_start_dma (int dev, unsigned long physaddr, int count, int dma_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1241 {
1242   int             chan;
1243   struct dma_buffparms *dmap;
1244   unsigned long   flags;
1245 
1246   if (dma_mode == DMA_MODE_WRITE)
1247     {
1248       chan = audio_devs[dev]->dmachan1;
1249       dmap = audio_devs[dev]->dmap_out;
1250     }
1251   else
1252     {
1253       chan = audio_devs[dev]->dmachan2;
1254       dmap = audio_devs[dev]->dmap_in;
1255     }
1256 
1257   /*
1258    * The count must be one less than the actual size. This is handled by
1259    * set_dma_addr()
1260    */
1261 
1262   if (audio_devs[dev]->flags & DMA_AUTOMODE)
1263     {                           /*
1264                                  * Auto restart mode. Transfer the whole *
1265                                  * buffer
1266                                  */
1267       save_flags (flags);
1268       cli ();
1269       disable_dma (chan);
1270       clear_dma_ff (chan);
1271       set_dma_mode (chan, dma_mode | DMA_AUTOINIT);
1272       set_dma_addr (chan, dmap->raw_buf_phys);
1273       set_dma_count (chan, dmap->bytes_in_use);
1274       enable_dma (chan);
1275       restore_flags (flags);
1276     }
1277   else
1278     {
1279       save_flags (flags);
1280       cli ();
1281       disable_dma (chan);
1282       clear_dma_ff (chan);
1283       set_dma_mode (chan, dma_mode);
1284       set_dma_addr (chan, physaddr);
1285       set_dma_count (chan, count);
1286       enable_dma (chan);
1287       restore_flags (flags);
1288     }
1289 
1290   return count;
1291 }
1292 
1293 long
1294 DMAbuf_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1295 {
1296   int             dev;
1297 
1298 
1299   /*
1300      * NOTE! This routine could be called several times.
1301    */
1302 
1303   for (dev = 0; dev < num_audiodevs; dev++)
1304     if (audio_devs[dev]->dmap_out == NULL)
1305       {
1306         audio_devs[dev]->dmap_out =
1307           audio_devs[dev]->dmap_in =
1308           &dmaps[ndmaps++];
1309 
1310         if (audio_devs[dev]->flags & DMA_DUPLEX)
1311           audio_devs[dev]->dmap_in =
1312             &dmaps[ndmaps++];
1313       }
1314   return mem_start;
1315 }
1316 
1317 static void
1318 polish_buffers (struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
1319 {
1320   int             i;
1321   int             p, l;
1322 
1323   i = dmap->qhead;
1324 
1325   p = dmap->fragment_size * i;
1326 
1327   if (i == dmap->cfrag)
1328     {
1329       l = dmap->fragment_size - dmap->counts[i];
1330     }
1331   else
1332     l = dmap->fragment_size;
1333 
1334   if (l)
1335     {
1336       memset (dmap->raw_buf + p,
1337               dmap->neutral_byte,
1338               l);
1339     }
1340 }
1341 
1342 static void
1343 force_restart (int dev, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
1344 {
1345   if ((audio_devs[dev]->flags & DMA_DUPLEX) &&
1346       audio_devs[dev]->halt_output)
1347     audio_devs[dev]->halt_output (dev);
1348   else
1349     audio_devs[dev]->halt_xfer (dev);
1350 
1351   dmap->flags &= ~DMA_ACTIVE;
1352   if (audio_devs[dev]->flags & DMA_AUTOMODE)
1353     dmap->flags |= DMA_RESTART;
1354   else
1355     dmap->flags &= ~DMA_RESTART;
1356 }
1357 
1358 void
1359 DMAbuf_outputintr (int dev, int event_type)
     /* [previous][next][first][last][top][bottom][index][help] */
1360 {
1361   /*
1362      * Event types:
1363      *  0 = DMA transfer done. Device still has more data in the local
1364      *      buffer.
1365      *  1 = DMA transfer done. Device doesn't have local buffer or it's
1366      *      empty now.
1367      *  2 = No DMA transfer but the device has now more space in it's local
1368      *      buffer.
1369    */
1370 
1371   unsigned long   flags;
1372   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1373 
1374   dmap->byte_counter += dmap->counts[dmap->qhead];
1375 
1376 #ifdef OS_DMA_INTR
1377   sound_dma_intr (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1378 #endif
1379 
1380   if (dmap->mapping_flags & DMA_MAP_MAPPED)
1381     {
1382       /* mmapped access */
1383 
1384       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1385       dmap->qlen++;             /* Yes increment it (don't decrement) */
1386       dmap->flags &= ~DMA_ACTIVE;
1387       dmap->counts[dmap->qhead] = dmap->fragment_size;
1388 
1389       if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1390         {
1391           audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1392                                          dmap->qhead * dmap->fragment_size,
1393                                          dmap->counts[dmap->qhead], 1,
1394                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1395           if (audio_devs[dev]->trigger)
1396             audio_devs[dev]->trigger (dev,
1397                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1398         }
1399       dmap->flags |= DMA_ACTIVE;
1400     }
1401   else if (event_type != 2)
1402     {
1403       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1404         {
1405           printk ("\nSound: Audio queue3 corrupted for dev%d (%d/%d)\n",
1406                   dev, dmap->qlen, dmap->nbufs);
1407           return;
1408         }
1409 
1410       dmap->qlen--;
1411       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1412       dmap->flags &= ~DMA_ACTIVE;
1413 
1414       if (event_type == 1 && dmap->qlen < 1)
1415         {
1416           dmap->underrun_count++;
1417 
1418           if (!(dmap->flags & DMA_CLEAN) &&
1419               (audio_devs[dev]->flags & DMA_SYNCING || dmap->underrun_count > 5 || dmap->flags & DMA_EMPTY))
1420             {
1421               dmap->qlen = 0;
1422               force_restart (dev, dmap);
1423             }
1424           else
1425             /* Ignore underrun. Just move the tail pointer forward and go */
1426           if (dmap->closing)
1427             {
1428               polish_buffers (dmap);
1429               audio_devs[dev]->halt_xfer (dev);
1430             }
1431           else
1432             {
1433               dmap->qlen++;
1434               dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1435 
1436               if (!(dmap->flags & DMA_EMPTY))
1437                 polish_buffers (dmap);
1438 
1439               dmap->cfrag = -1;
1440               dmap->flags |= DMA_EMPTY;
1441               dmap->counts[dmap->qtail] = dmap->fragment_size;
1442             }
1443         }
1444 
1445       if (dmap->qlen)
1446         {
1447           if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1448             {
1449 
1450               if (dmap->counts[dmap->qhead] == 0)
1451                 dmap->counts[dmap->qhead] = dmap->fragment_size;
1452 
1453               audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1454                                           dmap->qhead * dmap->fragment_size,
1455                                              dmap->counts[dmap->qhead], 1,
1456                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1457               if (audio_devs[dev]->trigger)
1458                 audio_devs[dev]->trigger (dev,
1459                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1460             }
1461           dmap->flags |= DMA_ACTIVE;
1462         }
1463     }                           /* event_type != 2 */
1464 
1465   save_flags (flags);
1466   cli ();
1467   if ((out_sleep_flag[dev].mode & WK_SLEEP))
1468     {
1469       {
1470         out_sleep_flag[dev].mode = WK_WAKEUP;
1471         module_wake_up (&out_sleeper[dev]);
1472       };
1473     }
1474   restore_flags (flags);
1475 }
1476 
1477 void
1478 DMAbuf_inputintr (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1479 {
1480   unsigned long   flags;
1481   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
1482 
1483   dmap->byte_counter += dmap->fragment_size;
1484 
1485 #ifdef OS_DMA_INTR
1486   sound_dma_intr (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
1487 #endif
1488 
1489   if (dmap->mapping_flags & DMA_MAP_MAPPED)
1490     {
1491       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1492       dmap->qlen++;
1493 
1494       if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1495         {
1496           audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
1497                                         dmap->qtail * dmap->fragment_size,
1498                                         dmap->fragment_size, 1,
1499                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1500           if (audio_devs[dev]->trigger)
1501             audio_devs[dev]->trigger (dev,
1502                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1503         }
1504 
1505       dmap->flags |= DMA_ACTIVE;
1506     }
1507   else if (dmap->qlen == (dmap->nbufs - 1))
1508     {
1509       printk ("Sound: Recording overrun\n");
1510       dmap->underrun_count++;
1511 
1512       if (audio_devs[dev]->flags & DMA_AUTOMODE)
1513         {
1514           /* Force restart on next read */
1515           if ((audio_devs[dev]->flags & DMA_DUPLEX) &&
1516               audio_devs[dev]->halt_input)
1517             audio_devs[dev]->halt_input (dev);
1518           else
1519             audio_devs[dev]->halt_xfer (dev);
1520 
1521           dmap->flags &= ~DMA_ACTIVE;
1522           if (audio_devs[dev]->flags & DMA_AUTOMODE)
1523             dmap->flags |= DMA_RESTART;
1524           else
1525             dmap->flags &= ~DMA_RESTART;
1526         }
1527     }
1528   else
1529     {
1530       dmap->qlen++;
1531       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1532         printk ("\nSound: Audio queue4 corrupted for dev%d (%d/%d)\n",
1533                 dev, dmap->qlen, dmap->nbufs);
1534       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1535     }
1536 
1537   if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1538     {
1539       audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
1540                                     dmap->qtail * dmap->fragment_size,
1541                                     dmap->fragment_size, 1,
1542                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1543       if (audio_devs[dev]->trigger)
1544         audio_devs[dev]->trigger (dev,
1545                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1546     }
1547 
1548   dmap->flags |= DMA_ACTIVE;
1549 
1550   save_flags (flags);
1551   cli ();
1552   if ((in_sleep_flag[dev].mode & WK_SLEEP))
1553     {
1554       {
1555         in_sleep_flag[dev].mode = WK_WAKEUP;
1556         module_wake_up (&in_sleeper[dev]);
1557       };
1558     }
1559   restore_flags (flags);
1560 }
1561 
1562 int
1563 DMAbuf_open_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1564 {
1565   int             chan = audio_devs[dev]->dmachan1;
1566   int             err;
1567   unsigned long   flags;
1568 
1569   if ((err = open_dmap (dev, OPEN_READWRITE, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1)) < 0)
1570     {
1571       return -EBUSY;
1572     }
1573   dma_init_buffers (dev, audio_devs[dev]->dmap_out);
1574   audio_devs[dev]->dmap_out->flags |= DMA_ALLOC_DONE;
1575   audio_devs[dev]->dmap_out->fragment_size = audio_devs[dev]->buffsize;
1576   /* reorganize_buffers (dev, audio_devs[dev]->dmap_out); */
1577 
1578   if (audio_devs[dev]->flags & DMA_DUPLEX)
1579     {
1580       if ((err = open_dmap (dev, OPEN_READWRITE, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2)) < 0)
1581         {
1582           printk ("Unable to grab DMA%d for the audio driver\n",
1583                   audio_devs[dev]->dmachan2);
1584           close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1585           return -EBUSY;
1586         }
1587       dma_init_buffers (dev, audio_devs[dev]->dmap_in);
1588       audio_devs[dev]->dmap_in->flags |= DMA_ALLOC_DONE;
1589       audio_devs[dev]->dmap_in->fragment_size = audio_devs[dev]->buffsize;
1590       /* reorganize_buffers (dev, audio_devs[dev]->dmap_in); */
1591     }
1592   else
1593     {
1594       audio_devs[dev]->dmap_in = audio_devs[dev]->dmap_out;
1595       audio_devs[dev]->dmachan2 = audio_devs[dev]->dmachan1;
1596     }
1597 
1598 
1599   save_flags (flags);
1600   cli ();
1601   disable_dma (chan);
1602   clear_dma_ff (chan);
1603   restore_flags (flags);
1604 
1605   return 0;
1606 }
1607 
1608 void
1609 DMAbuf_close_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1610 {
1611   DMAbuf_reset_dma (dev);
1612   close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1613 
1614   if (audio_devs[dev]->flags & DMA_DUPLEX)
1615     close_dmap (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
1616 
1617 }
1618 
1619 void
1620 DMAbuf_reset_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1621 {
1622 }
1623 
1624 int
1625 DMAbuf_select (int dev, struct fileinfo *file, int sel_type, select_table_handle * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1626 {
1627   struct dma_buffparms *dmap;
1628   unsigned long   flags;
1629 
1630   switch (sel_type)
1631     {
1632     case SEL_IN:
1633       dmap = audio_devs[dev]->dmap_in;
1634 
1635       if (dmap->mapping_flags & DMA_MAP_MAPPED)
1636         {
1637           if (dmap->qlen)
1638             return 1;
1639 
1640           save_flags (flags);
1641           cli ();
1642 
1643           in_sleep_flag[dev].mode = WK_SLEEP;
1644           module_select_wait (&in_sleeper[dev], wait);
1645           restore_flags (flags);
1646           return 0;
1647         }
1648 
1649       if (dmap->dma_mode != DMODE_INPUT)
1650         {
1651           if ((audio_devs[dev]->flags & DMA_DUPLEX) && !dmap->qlen &&
1652               audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT &&
1653               audio_devs[dev]->go)
1654             {
1655               unsigned long   flags;
1656 
1657               save_flags (flags);
1658               cli ();
1659               activate_recording (dev, dmap);
1660               restore_flags (flags);
1661             }
1662           return 0;
1663         }
1664 
1665       if (!dmap->qlen)
1666         {
1667           save_flags (flags);
1668           cli ();
1669 
1670           in_sleep_flag[dev].mode = WK_SLEEP;
1671           module_select_wait (&in_sleeper[dev], wait);
1672           restore_flags (flags);
1673           return 0;
1674         }
1675       return 1;
1676       break;
1677 
1678     case SEL_OUT:
1679       dmap = audio_devs[dev]->dmap_out;
1680 
1681       if (dmap->mapping_flags & DMA_MAP_MAPPED)
1682         {
1683           if (dmap->qlen)
1684             return 1;
1685 
1686           save_flags (flags);
1687           cli ();
1688 
1689           out_sleep_flag[dev].mode = WK_SLEEP;
1690           module_select_wait (&out_sleeper[dev], wait);
1691           restore_flags (flags);
1692           return 0;
1693         }
1694 
1695       if (dmap->dma_mode == DMODE_INPUT)
1696         {
1697           return 0;
1698         }
1699 
1700       if (dmap->dma_mode == DMODE_NONE)
1701         {
1702           return 1;
1703         }
1704 
1705       if (!space_in_queue (dev))
1706         {
1707           save_flags (flags);
1708           cli ();
1709 
1710           out_sleep_flag[dev].mode = WK_SLEEP;
1711           module_select_wait (&out_sleeper[dev], wait);
1712           restore_flags (flags);
1713           return 0;
1714         }
1715       return 1;
1716       break;
1717 
1718     case SEL_EX:
1719       return 0;
1720     }
1721 
1722   return 0;
1723 }
1724 
1725 
1726 #else /* CONFIG_AUDIO */
1727 /*
1728  * Stub versions if audio services not included
1729  */
1730 
1731 int
1732 DMAbuf_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1733 {
1734   return -ENXIO;
1735 }
1736 
1737 int
1738 DMAbuf_release (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1739 {
1740   return 0;
1741 }
1742 
1743 int
1744 DMAbuf_getwrbuffer (int dev, char **buf, int *size, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1745 {
1746   return -EIO;
1747 }
1748 
1749 int
1750 DMAbuf_getrdbuffer (int dev, char **buf, int *len, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1751 {
1752   return -EIO;
1753 }
1754 
1755 int
1756 DMAbuf_rmchars (int dev, int buff_no, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
1757 {
1758   return -EIO;
1759 }
1760 
1761 int
1762 DMAbuf_start_output (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1763 {
1764   return -EIO;
1765 }
1766 
1767 int
1768 DMAbuf_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
1769 {
1770   return -EIO;
1771 }
1772 
1773 long
1774 DMAbuf_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1775 {
1776   return mem_start;
1777 }
1778 
1779 int
1780 DMAbuf_start_dma (int dev, unsigned long physaddr, int count, int dma_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1781 {
1782   return -EIO;
1783 }
1784 
1785 int
1786 DMAbuf_open_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1787 {
1788   return -ENXIO;
1789 }
1790 
1791 void
1792 DMAbuf_close_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1793 {
1794   return;
1795 }
1796 
1797 void
1798 DMAbuf_reset_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1799 {
1800   return;
1801 }
1802 
1803 void
1804 DMAbuf_inputintr (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1805 {
1806   return;
1807 }
1808 
1809 void
1810 DMAbuf_outputintr (int dev, int underrun_flag)
     /* [previous][next][first][last][top][bottom][index][help] */
1811 {
1812   return;
1813 }
1814 #endif

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