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 exeeding 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 sice with SNDCTL_DSP_SETFRAGMENT or
 139          * the buffer sice 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]->dmap_out->underrun_count = 0;
 364       while (!current_got_fatal_signal ()
 365              && audio_devs[dev]->dmap_out->qlen
 366              && audio_devs[dev]->dmap_out->underrun_count == 0)
 367         {
 368 
 369           {
 370             unsigned long   tl;
 371 
 372             if (HZ)
 373               current_set_timeout (tl = jiffies + (HZ));
 374             else
 375               tl = (unsigned long) -1;
 376             out_sleep_flag[dev].mode = WK_SLEEP;
 377             module_interruptible_sleep_on (&out_sleeper[dev]);
 378             if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
 379               {
 380                 if (jiffies >= tl)
 381                   out_sleep_flag[dev].mode |= WK_TIMEOUT;
 382               }
 383             out_sleep_flag[dev].mode &= ~WK_SLEEP;
 384           };
 385           if ((out_sleep_flag[dev].mode & WK_TIMEOUT))
 386             {
 387               restore_flags (flags);
 388               return audio_devs[dev]->dmap_out->qlen;
 389             }
 390         }
 391       restore_flags (flags);
 392 
 393       /*
 394        * Some devices such as GUS have huge amount of on board RAM for the
 395        * audio data. We have to wait until the device has finished playing.
 396        */
 397 
 398       save_flags (flags);
 399       cli ();
 400       if (audio_devs[dev]->local_qlen)  /* Device has hidden buffers */
 401         {
 402           while (!(current_got_fatal_signal ())
 403                  && audio_devs[dev]->local_qlen (dev))
 404             {
 405 
 406               {
 407                 unsigned long   tl;
 408 
 409                 if (HZ)
 410                   current_set_timeout (tl = jiffies + (HZ));
 411                 else
 412                   tl = (unsigned long) -1;
 413                 out_sleep_flag[dev].mode = WK_SLEEP;
 414                 module_interruptible_sleep_on (&out_sleeper[dev]);
 415                 if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
 416                   {
 417                     if (jiffies >= tl)
 418                       out_sleep_flag[dev].mode |= WK_TIMEOUT;
 419                   }
 420                 out_sleep_flag[dev].mode &= ~WK_SLEEP;
 421               };
 422             }
 423         }
 424       restore_flags (flags);
 425     }
 426   return audio_devs[dev]->dmap_out->qlen;
 427 }
 428 
 429 int
 430 DMAbuf_release (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 431 {
 432   unsigned long   flags;
 433 
 434   audio_devs[dev]->dmap_out->closing = 1;
 435   audio_devs[dev]->dmap_in->closing = 1;
 436 
 437   if (!(current_got_fatal_signal ())
 438       && (audio_devs[dev]->dmap_out->dma_mode == DMODE_OUTPUT))
 439     {
 440       dma_sync (dev);
 441 
 442       memset (audio_devs[dev]->dmap_out->raw_buf,
 443               audio_devs[dev]->dmap_out->neutral_byte,
 444               audio_devs[dev]->dmap_out->bytes_in_use);
 445     }
 446 
 447   save_flags (flags);
 448   cli ();
 449 
 450   audio_devs[dev]->close (dev);
 451 
 452   close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
 453 
 454   if (audio_devs[dev]->flags & DMA_DUPLEX)
 455     close_dmap (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
 456   audio_devs[dev]->open_mode = 0;
 457 
 458   restore_flags (flags);
 459 
 460   return 0;
 461 }
 462 
 463 static int
 464 activate_recording (int dev, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
 465 {
 466   if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT))
 467     return 0;
 468 
 469   if (dmap->flags & DMA_RESTART)
 470     {
 471       dma_reset_input (dev);
 472       dmap->flags &= ~DMA_RESTART;
 473     }
 474 
 475   if (dmap->dma_mode == DMODE_OUTPUT)   /* Direction change */
 476     {
 477       dma_sync (dev);
 478       dma_reset (dev);
 479       dmap->dma_mode = DMODE_NONE;
 480     }
 481 
 482   if (!(dmap->flags & DMA_ALLOC_DONE))
 483     reorganize_buffers (dev, dmap, 1);
 484 
 485   if (!dmap->dma_mode)
 486     {
 487       int             err;
 488 
 489       if ((err = audio_devs[dev]->prepare_for_input (dev,
 490                                      dmap->fragment_size, dmap->nbufs)) < 0)
 491         {
 492           return err;
 493         }
 494       dmap->dma_mode = DMODE_INPUT;
 495     }
 496 
 497   if (!(dmap->flags & DMA_ACTIVE))
 498     {
 499       audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
 500                                     dmap->qtail * dmap->fragment_size,
 501                                     dmap->fragment_size, 0,
 502                                  !(audio_devs[dev]->flags & DMA_AUTOMODE) ||
 503                                     !(dmap->flags & DMA_STARTED));
 504       dmap->flags |= DMA_ACTIVE | DMA_STARTED;
 505       if (audio_devs[dev]->trigger)
 506         audio_devs[dev]->trigger (dev,
 507                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
 508     }
 509   return 0;
 510 }
 511 
 512 int
 513 DMAbuf_getrdbuffer (int dev, char **buf, int *len, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
 514 {
 515   unsigned long   flags;
 516   int             err = EIO;
 517   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
 518 
 519   save_flags (flags);
 520   cli ();
 521   if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 522     {
 523       printk ("Sound: Can't read from mmapped device (1)\n");
 524       return -EINVAL;
 525     }
 526   else if (!dmap->qlen)
 527     {
 528       int             tmout;
 529 
 530       if ((err = activate_recording (dev, dmap)) < 0)
 531         {
 532           restore_flags (flags);
 533           return err;
 534         }
 535 
 536       /* Wait for the next block */
 537 
 538       if (dontblock)
 539         {
 540           restore_flags (flags);
 541           return -EAGAIN;
 542         }
 543 
 544       if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT) &
 545           audio_devs[dev]->go)
 546         {
 547           restore_flags (flags);
 548           return -EAGAIN;
 549         }
 550 
 551       if (!audio_devs[dev]->go)
 552         tmout = 0;
 553       else
 554         tmout = 2 * HZ;
 555 
 556 
 557       {
 558         unsigned long   tl;
 559 
 560         if (tmout)
 561           current_set_timeout (tl = jiffies + (tmout));
 562         else
 563           tl = (unsigned long) -1;
 564         in_sleep_flag[dev].mode = WK_SLEEP;
 565         module_interruptible_sleep_on (&in_sleeper[dev]);
 566         if (!(in_sleep_flag[dev].mode & WK_WAKEUP))
 567           {
 568             if (jiffies >= tl)
 569               in_sleep_flag[dev].mode |= WK_TIMEOUT;
 570           }
 571         in_sleep_flag[dev].mode &= ~WK_SLEEP;
 572       };
 573       if ((in_sleep_flag[dev].mode & WK_TIMEOUT))
 574         {
 575           printk ("Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
 576           err = EIO;
 577           audio_devs[dev]->reset (dev);
 578           ;
 579         }
 580       else
 581         err = EINTR;
 582     }
 583   restore_flags (flags);
 584 
 585   if (!dmap->qlen)
 586     return -err;
 587 
 588   *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
 589   *len = dmap->fragment_size - dmap->counts[dmap->qhead];
 590 
 591   return dmap->qhead;
 592 }
 593 
 594 int
 595 DMAbuf_rmchars (int dev, int buff_no, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
 596 {
 597   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
 598 
 599   int             p = dmap->counts[dmap->qhead] + c;
 600 
 601   if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 602     {
 603       printk ("Sound: Can't read from mmapped device (2)\n");
 604       return -EINVAL;
 605     }
 606   else if (p >= dmap->fragment_size)
 607     {                           /* This buffer is completely empty */
 608       dmap->counts[dmap->qhead] = 0;
 609       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
 610         printk ("\nSound: Audio queue1 corrupted for dev%d (%d/%d)\n",
 611                 dev, dmap->qlen, dmap->nbufs);
 612       dmap->qlen--;
 613       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
 614     }
 615   else
 616     dmap->counts[dmap->qhead] = p;
 617 
 618   return 0;
 619 }
 620 
 621 static int
 622 dma_subdivide (int dev, struct dma_buffparms *dmap, caddr_t arg, int fact)
     /* [previous][next][first][last][top][bottom][index][help] */
 623 {
 624   if (fact == 0)
 625     {
 626       fact = dmap->subdivision;
 627       if (fact == 0)
 628         fact = 1;
 629       return snd_ioctl_return ((int *) arg, fact);
 630     }
 631 
 632   if (dmap->subdivision != 0 ||
 633       dmap->fragment_size)      /* Loo late to change */
 634     return -EINVAL;
 635 
 636   if (fact > MAX_REALTIME_FACTOR)
 637     return -EINVAL;
 638 
 639   if (fact != 1 && fact != 2 && fact != 4 && fact != 8 && fact != 16)
 640     return -EINVAL;
 641 
 642   dmap->subdivision = fact;
 643   return snd_ioctl_return ((int *) arg, fact);
 644 }
 645 
 646 static int
 647 dma_set_fragment (int dev, struct dma_buffparms *dmap, caddr_t arg, int fact)
     /* [previous][next][first][last][top][bottom][index][help] */
 648 {
 649   int             bytes, count;
 650 
 651   if (fact == 0)
 652     return -EIO;
 653 
 654   if (dmap->subdivision != 0 ||
 655       dmap->fragment_size)      /* Loo late to change */
 656     return -EINVAL;
 657 
 658   bytes = fact & 0xffff;
 659   count = (fact >> 16) & 0xffff;
 660 
 661   if (count == 0)
 662     count = MAX_SUB_BUFFERS;
 663 
 664   if (bytes < 4 || bytes > 17)  /* <16 || > 128k */
 665     return -EINVAL;
 666 
 667   if (count < 2)
 668     return -EINVAL;
 669 
 670 #ifdef OS_DMA_MINBITS
 671   if (bytes < OS_DMA_MINBITS)
 672     bytes = OS_DMA_MINBITS;
 673 #endif
 674 
 675   dmap->fragment_size = (1 << bytes);
 676   dmap->max_fragments = count;
 677 
 678   if (dmap->fragment_size > audio_devs[dev]->buffsize)
 679     dmap->fragment_size = audio_devs[dev]->buffsize;
 680 
 681   if (dmap->fragment_size == audio_devs[dev]->buffsize &&
 682       audio_devs[dev]->flags & DMA_AUTOMODE)
 683     dmap->fragment_size /= 2;   /* Needs at least 2 buffers */
 684 
 685   dmap->subdivision = 1;        /* Disable SNDCTL_DSP_SUBDIVIDE */
 686   return snd_ioctl_return ((int *) arg, bytes | (count << 16));
 687 }
 688 
 689 static int
 690 get_buffer_pointer (int dev, int chan, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
 691 {
 692   int             pos;
 693   unsigned long   flags;
 694 
 695   save_flags (flags);
 696   cli ();
 697   if (!(dmap->flags & DMA_ACTIVE))
 698     pos = 0;
 699   else
 700     {
 701       clear_dma_ff (chan);
 702       disable_dma (chan);
 703       pos = get_dma_residue (chan);
 704       enable_dma (chan);
 705     }
 706   restore_flags (flags);
 707   /* printk ("%04x ", pos); */
 708 
 709   if (audio_devs[dev]->flags & DMA_AUTOMODE)
 710     return dmap->bytes_in_use - pos;
 711   else
 712     {
 713       pos = dmap->fragment_size - pos;
 714       if (pos < 0)
 715         return 0;
 716       return pos;
 717     }
 718 }
 719 
 720 
 721 int
 722 DMAbuf_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
 723 {
 724   struct dma_buffparms *dmap_out = audio_devs[dev]->dmap_out;
 725   struct dma_buffparms *dmap_in = audio_devs[dev]->dmap_in;
 726 
 727   switch (cmd)
 728     {
 729     case SNDCTL_DSP_RESET:
 730       dma_reset (dev);
 731       return 0;
 732       break;
 733 
 734     case SNDCTL_DSP_SYNC:
 735       dma_sync (dev);
 736       dma_reset (dev);
 737       return 0;
 738       break;
 739 
 740     case SNDCTL_DSP_GETBLKSIZE:
 741       if (!(dmap_out->flags & DMA_ALLOC_DONE))
 742         {
 743           reorganize_buffers (dev, dmap_out,
 744                               (audio_devs[dev]->open_mode == OPEN_READ));
 745           if (audio_devs[dev]->flags & DMA_DUPLEX)
 746             reorganize_buffers (dev, dmap_in,
 747                                 (audio_devs[dev]->open_mode == OPEN_READ));
 748         }
 749 
 750       return snd_ioctl_return ((int *) arg, dmap_out->fragment_size);
 751       break;
 752 
 753     case SNDCTL_DSP_SUBDIVIDE:
 754       {
 755         int             fact = get_fs_long ((long *) arg);
 756         int             ret;
 757 
 758         ret = dma_subdivide (dev, dmap_out, arg, fact);
 759         if (ret < 0)
 760           return ret;
 761 
 762         if (audio_devs[dev]->flags & DMA_DUPLEX)
 763           ret = dma_subdivide (dev, dmap_in, arg, fact);
 764 
 765         return ret;
 766       }
 767       break;
 768 
 769     case SNDCTL_DSP_SETFRAGMENT:
 770       {
 771         int             fact = get_fs_long ((long *) arg);
 772         int             ret;
 773 
 774         ret = dma_set_fragment (dev, dmap_out, arg, fact);
 775         if (ret < 0)
 776           return ret;
 777 
 778         if (audio_devs[dev]->flags & DMA_DUPLEX)
 779           ret = dma_set_fragment (dev, dmap_in, arg, fact);
 780 
 781         return ret;
 782       }
 783       break;
 784 
 785     case SNDCTL_DSP_GETISPACE:
 786     case SNDCTL_DSP_GETOSPACE:
 787       if (!local)
 788         return -EINVAL;
 789       else
 790         {
 791           struct dma_buffparms *dmap = dmap_out;
 792 
 793           audio_buf_info *info = (audio_buf_info *) arg;
 794 
 795           if (cmd == SNDCTL_DSP_GETISPACE && audio_devs[dev]->flags & DMA_DUPLEX)
 796             dmap = dmap_in;
 797 
 798           if (dmap->mapping_flags & DMA_MAP_MAPPED)
 799             return -EINVAL;
 800 
 801           if (!(dmap->flags & DMA_ALLOC_DONE))
 802             reorganize_buffers (dev, dmap, (cmd == SNDCTL_DSP_GETISPACE));
 803 
 804           info->fragstotal = dmap->nbufs;
 805 
 806           if (cmd == SNDCTL_DSP_GETISPACE)
 807             info->fragments = dmap->qlen;
 808           else
 809             {
 810               if (!space_in_queue (dev))
 811                 info->fragments = 0;
 812               else
 813                 {
 814                   info->fragments = dmap->nbufs - dmap->qlen;
 815                   if (audio_devs[dev]->local_qlen)
 816                     {
 817                       int             tmp = audio_devs[dev]->local_qlen (dev);
 818 
 819                       if (tmp && info->fragments)
 820                         tmp--;  /*
 821                                    * This buffer has been counted twice
 822                                  */
 823                       info->fragments -= tmp;
 824                     }
 825                 }
 826             }
 827 
 828           if (info->fragments < 0)
 829             info->fragments = 0;
 830           else if (info->fragments > dmap->nbufs)
 831             info->fragments = dmap->nbufs;
 832 
 833           info->fragsize = dmap->fragment_size;
 834           info->bytes = info->fragments * dmap->fragment_size;
 835 
 836           if (cmd == SNDCTL_DSP_GETISPACE && dmap->qlen)
 837             info->bytes -= dmap->counts[dmap->qhead];
 838         }
 839       return 0;
 840 
 841     case SNDCTL_DSP_SETTRIGGER:
 842       {
 843         unsigned long   flags;
 844 
 845         int             bits = get_fs_long ((long *) arg) & audio_devs[dev]->open_mode;
 846         int             changed;
 847 
 848         if (audio_devs[dev]->trigger == NULL)
 849           return -EINVAL;
 850 
 851         if (!(audio_devs[dev]->flags & DMA_DUPLEX))
 852           if ((bits & PCM_ENABLE_INPUT) && (bits & PCM_ENABLE_OUTPUT))
 853             {
 854               printk ("Sound: Device doesn't have full duplex capability\n");
 855               return -EINVAL;
 856             }
 857 
 858         save_flags (flags);
 859         cli ();
 860         changed = audio_devs[dev]->enable_bits ^ bits;
 861 
 862         if ((changed & bits) & PCM_ENABLE_INPUT && audio_devs[dev]->go)
 863           {
 864             int             err;
 865 
 866             if (!(dmap_in->flags & DMA_ALLOC_DONE))
 867               {
 868                 reorganize_buffers (dev, dmap_in, 1);
 869               }
 870 
 871             if ((err = audio_devs[dev]->prepare_for_input (dev,
 872                                dmap_in->fragment_size, dmap_in->nbufs)) < 0)
 873               return -err;
 874 
 875             activate_recording (dev, dmap_in);
 876           }
 877 
 878         if ((changed & bits) & PCM_ENABLE_OUTPUT &&
 879             dmap_out->mapping_flags & DMA_MAP_MAPPED &&
 880             audio_devs[dev]->go)
 881           {
 882             int             err;
 883 
 884             if (!(dmap_out->flags & DMA_ALLOC_DONE))
 885               {
 886                 reorganize_buffers (dev, dmap_out, 0);
 887               }
 888 
 889             if ((err = audio_devs[dev]->prepare_for_output (dev,
 890                              dmap_out->fragment_size, dmap_out->nbufs)) < 0)
 891               return -err;
 892 
 893             dmap_out->counts[dmap_out->qhead] = dmap_out->fragment_size;
 894             DMAbuf_start_output (dev, 0, dmap_out->fragment_size);
 895           }
 896 
 897         audio_devs[dev]->enable_bits = bits;
 898         if (changed && audio_devs[dev]->trigger)
 899           audio_devs[dev]->trigger (dev, bits * audio_devs[dev]->go);
 900         restore_flags (flags);
 901       }
 902     case SNDCTL_DSP_GETTRIGGER:
 903       return snd_ioctl_return ((int *) arg, audio_devs[dev]->enable_bits);
 904       break;
 905 
 906     case SNDCTL_DSP_SETSYNCRO:
 907 
 908       if (!audio_devs[dev]->trigger)
 909         return -EINVAL;
 910 
 911       audio_devs[dev]->trigger (dev, 0);
 912       audio_devs[dev]->go = 0;
 913       return 0;
 914       break;
 915 
 916     case SNDCTL_DSP_GETIPTR:
 917       {
 918         count_info      info;
 919         unsigned long   flags;
 920 
 921         save_flags (flags);
 922         cli ();
 923         info.bytes = audio_devs[dev]->dmap_in->byte_counter;
 924         info.ptr = get_buffer_pointer (dev, audio_devs[dev]->dmachan2, audio_devs[dev]->dmap_in);
 925         info.blocks = audio_devs[dev]->dmap_in->qlen;
 926         info.bytes += info.ptr;
 927         memcpy_tofs ((&((char *) arg)[0]), (char *) &info, sizeof (info));
 928 
 929         if (audio_devs[dev]->dmap_in->mapping_flags & DMA_MAP_MAPPED)
 930           audio_devs[dev]->dmap_in->qlen = 0;   /* Acknowledge interrupts */
 931         restore_flags (flags);
 932         return 0;
 933       }
 934       break;
 935 
 936     case SNDCTL_DSP_GETOPTR:
 937       {
 938         count_info      info;
 939         unsigned long   flags;
 940 
 941         save_flags (flags);
 942         cli ();
 943         info.bytes = audio_devs[dev]->dmap_out->byte_counter;
 944         info.ptr = get_buffer_pointer (dev, audio_devs[dev]->dmachan1, audio_devs[dev]->dmap_out);
 945         info.blocks = audio_devs[dev]->dmap_out->qlen;
 946         info.bytes += info.ptr;
 947         memcpy_tofs ((&((char *) arg)[0]), (char *) &info, sizeof (info));
 948 
 949         if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
 950           audio_devs[dev]->dmap_out->qlen = 0;  /* Acknowledge interrupts */
 951         restore_flags (flags);
 952         return 0;
 953       }
 954       break;
 955 
 956 
 957     default:
 958       return audio_devs[dev]->ioctl (dev, cmd, arg, local);
 959     }
 960 
 961 }
 962 
 963 /*
 964  * DMAbuf_start_devices() is called by the /dev/music driver to start
 965  * one or more audio devices at desired moment.
 966  */
 967 
 968 void
 969 DMAbuf_start_devices (unsigned int devmask)
     /* [previous][next][first][last][top][bottom][index][help] */
 970 {
 971   int             dev;
 972 
 973   for (dev = 0; dev < num_audiodevs; dev++)
 974     if (devmask & (1 << dev))
 975       if (audio_devs[dev]->open_mode != 0)
 976         if (!audio_devs[dev]->go)
 977           {
 978             /* OK to start the device */
 979             audio_devs[dev]->go = 1;
 980 
 981             if (audio_devs[dev]->trigger)
 982               audio_devs[dev]->trigger (dev,
 983                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
 984           }
 985 }
 986 
 987 static int
 988 space_in_queue (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 989 {
 990   int             len, max, tmp;
 991   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
 992 
 993   if (dmap->qlen >= dmap->nbufs)        /* No space at all */
 994     return 0;
 995 
 996   /*
 997      * Verify that there are no more pending buffers than the limit
 998      * defined by the process.
 999    */
1000 
1001   max = dmap->max_fragments;
1002   len = dmap->qlen;
1003 
1004   if (audio_devs[dev]->local_qlen)
1005     {
1006       tmp = audio_devs[dev]->local_qlen (dev);
1007       if (tmp && len)
1008         tmp--;                  /*
1009                                    * This buffer has been counted twice
1010                                  */
1011       len += tmp;
1012     }
1013 
1014   if (len >= max)
1015     return 0;
1016   return 1;
1017 }
1018 
1019 int
1020 DMAbuf_getwrbuffer (int dev, char **buf, int *size, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1021 {
1022   unsigned long   flags;
1023   int             abort, err = EIO;
1024   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1025 
1026   if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
1027     {
1028       printk ("Sound: Can't write to mmapped device (3)\n");
1029       return -EINVAL;
1030     }
1031 
1032   if (dmap->dma_mode == DMODE_INPUT)    /* Direction change */
1033     {
1034       dma_reset (dev);
1035       dmap->dma_mode = DMODE_NONE;
1036     }
1037   else if (dmap->flags & DMA_RESTART)   /* Restart buffering */
1038     {
1039       dma_sync (dev);
1040       dma_reset_output (dev);
1041     }
1042 
1043   dmap->flags &= ~(DMA_RESTART | DMA_EMPTY);
1044 
1045   if (!(dmap->flags & DMA_ALLOC_DONE))
1046     reorganize_buffers (dev, dmap, 0);
1047 
1048   if (!dmap->dma_mode)
1049     {
1050       int             err;
1051 
1052       dmap->dma_mode = DMODE_OUTPUT;
1053       if ((err = audio_devs[dev]->prepare_for_output (dev,
1054                                      dmap->fragment_size, dmap->nbufs)) < 0)
1055         return err;
1056     }
1057 
1058   save_flags (flags);
1059   cli ();
1060 
1061   abort = 0;
1062   while (!space_in_queue (dev) &&
1063          !abort)
1064     {
1065       int             tmout;
1066 
1067       if (dontblock)
1068         {
1069           restore_flags (flags);
1070           return -EAGAIN;
1071         }
1072 
1073       if (!(audio_devs[dev]->enable_bits & PCM_ENABLE_OUTPUT) &&
1074           audio_devs[dev]->go)
1075         {
1076           restore_flags (flags);
1077           return -EAGAIN;
1078         }
1079 
1080       /*
1081        * Wait for free space
1082        */
1083       if (!audio_devs[dev]->go)
1084         tmout = 0;
1085       else
1086         tmout = 2 * HZ;
1087 
1088 
1089       {
1090         unsigned long   tl;
1091 
1092         if (tmout)
1093           current_set_timeout (tl = jiffies + (tmout));
1094         else
1095           tl = (unsigned long) -1;
1096         out_sleep_flag[dev].mode = WK_SLEEP;
1097         module_interruptible_sleep_on (&out_sleeper[dev]);
1098         if (!(out_sleep_flag[dev].mode & WK_WAKEUP))
1099           {
1100             if (jiffies >= tl)
1101               out_sleep_flag[dev].mode |= WK_TIMEOUT;
1102           }
1103         out_sleep_flag[dev].mode &= ~WK_SLEEP;
1104       };
1105       if ((out_sleep_flag[dev].mode & WK_TIMEOUT))
1106         {
1107           printk ("Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
1108           err = EIO;
1109           abort = 1;
1110           ;
1111           audio_devs[dev]->reset (dev);
1112         }
1113       else if (current_got_fatal_signal ())
1114         {
1115           err = EINTR;
1116           abort = 1;
1117         }
1118     }
1119   restore_flags (flags);
1120 
1121   if (!space_in_queue (dev))
1122     {
1123       return -err;              /* Caught a signal ? */
1124     }
1125 
1126   *buf = dmap->raw_buf + dmap->qtail * dmap->fragment_size;
1127   *size = dmap->fragment_size;
1128   dmap->counts[dmap->qtail] = 0;
1129 
1130   return dmap->qtail;
1131 }
1132 
1133 int
1134 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] */
1135 {
1136   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1137 
1138   if (dmap->cfrag < 0)
1139     return -1;
1140 
1141   *dma_buf = dmap->raw_buf + dmap->qtail * dmap->fragment_size;
1142   *buf_ptr = dmap->counts[dmap->qtail];
1143   *buf_size = dmap->fragment_size;
1144   return *buf_no = dmap->cfrag;
1145 }
1146 
1147 int
1148 DMAbuf_set_count (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1149 {
1150   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1151 
1152   if (buff_no == dmap->qtail)
1153     {
1154       dmap->cfrag = buff_no;
1155       dmap->counts[buff_no] = l;
1156     }
1157   else
1158     dmap->cfrag = -1;
1159   return 0;
1160 }
1161 
1162 int
1163 DMAbuf_start_output (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1164 {
1165   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1166 
1167   dmap->cfrag = -1;
1168 /*
1169  * Bypass buffering if using mmaped access
1170  */
1171 
1172   if (audio_devs[dev]->dmap_out->mapping_flags & DMA_MAP_MAPPED)
1173     {
1174       l = dmap->fragment_size;
1175       dmap->counts[dmap->qtail] = l;
1176       dmap->flags &= ~DMA_RESTART;
1177       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1178     }
1179   else
1180     {
1181 
1182 
1183       dmap->qlen++;
1184       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1185         printk ("\nSound: Audio queue2 corrupted for dev%d (%d/%d)\n",
1186                 dev, dmap->qlen, dmap->nbufs);
1187 
1188       dmap->counts[dmap->qtail] = l;
1189       if (l < dmap->fragment_size)
1190         {
1191           int             p = dmap->fragment_size * dmap->qtail;
1192 
1193           memset (dmap->raw_buf + p + l,
1194                   dmap->neutral_byte,
1195                   dmap->fragment_size - l);
1196         }
1197 
1198       if ((l != dmap->fragment_size) &&
1199           ((audio_devs[dev]->flags & DMA_AUTOMODE) &&
1200            audio_devs[dev]->flags & NEEDS_RESTART))
1201         dmap->flags |= DMA_RESTART;
1202       else
1203         dmap->flags &= ~DMA_RESTART;
1204 
1205       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1206     }
1207 
1208   if (!(dmap->flags & DMA_ACTIVE))
1209     {
1210       dmap->flags |= DMA_ACTIVE;
1211       audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1212                                      dmap->qhead * dmap->fragment_size,
1213                                      dmap->counts[dmap->qhead], 0,
1214                                  !(audio_devs[dev]->flags & DMA_AUTOMODE) ||
1215                                      !(dmap->flags & DMA_STARTED));
1216       dmap->flags |= DMA_STARTED;
1217       if (audio_devs[dev]->trigger)
1218         audio_devs[dev]->trigger (dev,
1219                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1220     }
1221 
1222   return 0;
1223 }
1224 
1225 int
1226 DMAbuf_start_dma (int dev, unsigned long physaddr, int count, int dma_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1227 {
1228   int             chan;
1229   struct dma_buffparms *dmap;
1230   unsigned long   flags;
1231 
1232   if (dma_mode == DMA_MODE_WRITE)
1233     {
1234       chan = audio_devs[dev]->dmachan1;
1235       dmap = audio_devs[dev]->dmap_out;
1236     }
1237   else
1238     {
1239       chan = audio_devs[dev]->dmachan2;
1240       dmap = audio_devs[dev]->dmap_in;
1241     }
1242 
1243   /*
1244    * The count must be one less than the actual size. This is handled by
1245    * set_dma_addr()
1246    */
1247 
1248   if (audio_devs[dev]->flags & DMA_AUTOMODE)
1249     {                           /*
1250                                  * Auto restart mode. Transfer the whole *
1251                                  * buffer
1252                                  */
1253       save_flags (flags);
1254       cli ();
1255       disable_dma (chan);
1256       clear_dma_ff (chan);
1257       set_dma_mode (chan, dma_mode | DMA_AUTOINIT);
1258       set_dma_addr (chan, dmap->raw_buf_phys);
1259       set_dma_count (chan, dmap->bytes_in_use);
1260       enable_dma (chan);
1261       restore_flags (flags);
1262     }
1263   else
1264     {
1265       save_flags (flags);
1266       cli ();
1267       disable_dma (chan);
1268       clear_dma_ff (chan);
1269       set_dma_mode (chan, dma_mode);
1270       set_dma_addr (chan, physaddr);
1271       set_dma_count (chan, count);
1272       enable_dma (chan);
1273       restore_flags (flags);
1274     }
1275 
1276   return count;
1277 }
1278 
1279 long
1280 DMAbuf_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1281 {
1282   int             dev;
1283 
1284 
1285   /*
1286      * NOTE! This routine could be called several times.
1287    */
1288 
1289   for (dev = 0; dev < num_audiodevs; dev++)
1290     if (audio_devs[dev]->dmap_out == NULL)
1291       {
1292         audio_devs[dev]->dmap_out =
1293           audio_devs[dev]->dmap_in =
1294           &dmaps[ndmaps++];
1295 
1296         if (audio_devs[dev]->flags & DMA_DUPLEX)
1297           audio_devs[dev]->dmap_in =
1298             &dmaps[ndmaps++];
1299       }
1300   return mem_start;
1301 }
1302 
1303 static void
1304 polish_buffers (struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
1305 {
1306   int             i;
1307   int             p, l;
1308 
1309   i = dmap->qhead;
1310 
1311   p = dmap->fragment_size * i;
1312 
1313   if (i == dmap->cfrag)
1314     {
1315       l = dmap->fragment_size - dmap->counts[i];
1316     }
1317   else
1318     l = dmap->fragment_size;
1319 
1320   if (l)
1321     {
1322       memset (dmap->raw_buf + p,
1323               dmap->neutral_byte,
1324               l);
1325     }
1326 }
1327 
1328 static void
1329 force_restart (int dev, struct dma_buffparms *dmap)
     /* [previous][next][first][last][top][bottom][index][help] */
1330 {
1331   if ((audio_devs[dev]->flags & DMA_DUPLEX) &&
1332       audio_devs[dev]->halt_output)
1333     audio_devs[dev]->halt_output (dev);
1334   else
1335     audio_devs[dev]->halt_xfer (dev);
1336 
1337   dmap->flags &= ~DMA_ACTIVE;
1338   if (audio_devs[dev]->flags & DMA_AUTOMODE)
1339     dmap->flags |= DMA_RESTART;
1340   else
1341     dmap->flags &= ~DMA_RESTART;
1342 }
1343 
1344 void
1345 DMAbuf_outputintr (int dev, int event_type)
     /* [previous][next][first][last][top][bottom][index][help] */
1346 {
1347   /*
1348      * Event types:
1349      *  0 = DMA transfer done. Device still has more data in the local
1350      *      buffer.
1351      *  1 = DMA transfer done. Device doesn't have local buffer or it's
1352      *      empty now.
1353      *  2 = No DMA transfer but the device has now more space in it's local
1354      *      buffer.
1355    */
1356 
1357   unsigned long   flags;
1358   struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
1359 
1360   dmap->byte_counter += dmap->counts[dmap->qhead];
1361 
1362 #ifdef OS_DMA_INTR
1363   sound_dma_intr (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1364 #endif
1365 
1366   if (dmap->mapping_flags & DMA_MAP_MAPPED)
1367     {
1368       /* mmapped access */
1369 
1370       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1371       dmap->qlen++;             /* Yes increment it (don't decrement) */
1372       dmap->flags &= ~DMA_ACTIVE;
1373       dmap->counts[dmap->qhead] = dmap->fragment_size;
1374 
1375       if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1376         {
1377           audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1378                                          dmap->qhead * dmap->fragment_size,
1379                                          dmap->counts[dmap->qhead], 1,
1380                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1381           if (audio_devs[dev]->trigger)
1382             audio_devs[dev]->trigger (dev,
1383                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1384         }
1385       dmap->flags |= DMA_ACTIVE;
1386     }
1387   else if (event_type != 2)
1388     {
1389       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1390         {
1391           printk ("\nSound: Audio queue3 corrupted for dev%d (%d/%d)\n",
1392                   dev, dmap->qlen, dmap->nbufs);
1393           return;
1394         }
1395 
1396       dmap->qlen--;
1397       dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1398       dmap->flags &= ~DMA_ACTIVE;
1399 
1400       if (event_type == 1 && dmap->qlen < 1)
1401         {
1402           dmap->underrun_count++;
1403 
1404           if (dmap->underrun_count > 5 || dmap->flags & DMA_EMPTY)
1405             {
1406               dmap->qlen = 0;
1407               force_restart (dev, dmap);
1408             }
1409           else
1410             /* Ignore underrun. Just move the tail pointer forward and go */
1411           if (dmap->closing)
1412             {
1413               polish_buffers (dmap);
1414               audio_devs[dev]->halt_xfer (dev);
1415             }
1416           else
1417             {
1418               dmap->qlen++;
1419               dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1420 
1421               if (!(dmap->flags & DMA_EMPTY))
1422                 polish_buffers (dmap);
1423 
1424               dmap->cfrag = -1;
1425               dmap->flags |= DMA_EMPTY;
1426               dmap->counts[dmap->qtail] = dmap->fragment_size;
1427             }
1428         }
1429 
1430       if (dmap->qlen)
1431         {
1432           if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1433             {
1434 
1435               if (dmap->counts[dmap->qhead] == 0)
1436                 dmap->counts[dmap->qhead] = dmap->fragment_size;
1437 
1438               audio_devs[dev]->output_block (dev, dmap->raw_buf_phys +
1439                                           dmap->qhead * dmap->fragment_size,
1440                                              dmap->counts[dmap->qhead], 1,
1441                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1442               if (audio_devs[dev]->trigger)
1443                 audio_devs[dev]->trigger (dev,
1444                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1445             }
1446           dmap->flags |= DMA_ACTIVE;
1447         }
1448     }                           /* event_type != 2 */
1449 
1450   save_flags (flags);
1451   cli ();
1452   if ((out_sleep_flag[dev].mode & WK_SLEEP))
1453     {
1454       {
1455         out_sleep_flag[dev].mode = WK_WAKEUP;
1456         module_wake_up (&out_sleeper[dev]);
1457       };
1458     }
1459   restore_flags (flags);
1460 }
1461 
1462 void
1463 DMAbuf_inputintr (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1464 {
1465   unsigned long   flags;
1466   struct dma_buffparms *dmap = audio_devs[dev]->dmap_in;
1467 
1468   dmap->byte_counter += dmap->fragment_size;
1469 
1470 #ifdef OS_DMA_INTR
1471   sound_dma_intr (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
1472 #endif
1473 
1474   if (dmap->mapping_flags & DMA_MAP_MAPPED)
1475     {
1476       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1477       dmap->qlen++;
1478 
1479       if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1480         {
1481           audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
1482                                         dmap->qtail * dmap->fragment_size,
1483                                         dmap->fragment_size, 1,
1484                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1485           if (audio_devs[dev]->trigger)
1486             audio_devs[dev]->trigger (dev,
1487                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1488         }
1489 
1490       dmap->flags |= DMA_ACTIVE;
1491     }
1492   else if (dmap->qlen == (dmap->nbufs - 1))
1493     {
1494       printk ("Sound: Recording overrun\n");
1495       dmap->underrun_count++;
1496 
1497       if (audio_devs[dev]->flags & DMA_AUTOMODE)
1498         {
1499           /* Force restart on next read */
1500           if ((audio_devs[dev]->flags & DMA_DUPLEX) &&
1501               audio_devs[dev]->halt_input)
1502             audio_devs[dev]->halt_input (dev);
1503           else
1504             audio_devs[dev]->halt_xfer (dev);
1505 
1506           dmap->flags &= ~DMA_ACTIVE;
1507           if (audio_devs[dev]->flags & DMA_AUTOMODE)
1508             dmap->flags |= DMA_RESTART;
1509           else
1510             dmap->flags &= ~DMA_RESTART;
1511         }
1512     }
1513   else
1514     {
1515       dmap->qlen++;
1516       if (dmap->qlen <= 0 || dmap->qlen > dmap->nbufs)
1517         printk ("\nSound: Audio queue4 corrupted for dev%d (%d/%d)\n",
1518                 dev, dmap->qlen, dmap->nbufs);
1519       dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1520     }
1521 
1522   if (!(audio_devs[dev]->flags & DMA_AUTOMODE))
1523     {
1524       audio_devs[dev]->start_input (dev, dmap->raw_buf_phys +
1525                                     dmap->qtail * dmap->fragment_size,
1526                                     dmap->fragment_size, 1,
1527                                   !(audio_devs[dev]->flags & DMA_AUTOMODE));
1528       if (audio_devs[dev]->trigger)
1529         audio_devs[dev]->trigger (dev,
1530                         audio_devs[dev]->enable_bits * audio_devs[dev]->go);
1531     }
1532 
1533   dmap->flags |= DMA_ACTIVE;
1534 
1535   save_flags (flags);
1536   cli ();
1537   if ((in_sleep_flag[dev].mode & WK_SLEEP))
1538     {
1539       {
1540         in_sleep_flag[dev].mode = WK_WAKEUP;
1541         module_wake_up (&in_sleeper[dev]);
1542       };
1543     }
1544   restore_flags (flags);
1545 }
1546 
1547 int
1548 DMAbuf_open_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1549 {
1550   int             chan = audio_devs[dev]->dmachan1;
1551   int             err;
1552   unsigned long   flags;
1553 
1554   if ((err = open_dmap (dev, OPEN_READWRITE, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1)) < 0)
1555     {
1556       return -EBUSY;
1557     }
1558   dma_init_buffers (dev, audio_devs[dev]->dmap_out);
1559   audio_devs[dev]->dmap_out->flags |= DMA_ALLOC_DONE;
1560   audio_devs[dev]->dmap_out->fragment_size = audio_devs[dev]->buffsize;
1561   /* reorganize_buffers (dev, audio_devs[dev]->dmap_out); */
1562 
1563   if (audio_devs[dev]->flags & DMA_DUPLEX)
1564     {
1565       if ((err = open_dmap (dev, OPEN_READWRITE, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2)) < 0)
1566         {
1567           printk ("Unable to grab DMA%d for the audio driver\n",
1568                   audio_devs[dev]->dmachan2);
1569           close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1570           return -EBUSY;
1571         }
1572       dma_init_buffers (dev, audio_devs[dev]->dmap_in);
1573       audio_devs[dev]->dmap_in->flags |= DMA_ALLOC_DONE;
1574       audio_devs[dev]->dmap_in->fragment_size = audio_devs[dev]->buffsize;
1575       /* reorganize_buffers (dev, audio_devs[dev]->dmap_in); */
1576     }
1577   else
1578     {
1579       audio_devs[dev]->dmap_in = audio_devs[dev]->dmap_out;
1580       audio_devs[dev]->dmachan2 = audio_devs[dev]->dmachan1;
1581     }
1582 
1583 
1584   save_flags (flags);
1585   cli ();
1586   disable_dma (chan);
1587   clear_dma_ff (chan);
1588   restore_flags (flags);
1589 
1590   return 0;
1591 }
1592 
1593 void
1594 DMAbuf_close_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1595 {
1596   DMAbuf_reset_dma (dev);
1597   close_dmap (dev, audio_devs[dev]->dmap_out, audio_devs[dev]->dmachan1);
1598 
1599   if (audio_devs[dev]->flags & DMA_DUPLEX)
1600     close_dmap (dev, audio_devs[dev]->dmap_in, audio_devs[dev]->dmachan2);
1601 
1602 }
1603 
1604 void
1605 DMAbuf_reset_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1606 {
1607 }
1608 
1609 int
1610 DMAbuf_select (int dev, struct fileinfo *file, int sel_type, select_table_handle * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1611 {
1612   struct dma_buffparms *dmap;
1613   unsigned long   flags;
1614 
1615   switch (sel_type)
1616     {
1617     case SEL_IN:
1618       dmap = audio_devs[dev]->dmap_in;
1619 
1620       if (dmap->mapping_flags & DMA_MAP_MAPPED)
1621         {
1622           if (dmap->qlen)
1623             return 1;
1624 
1625           save_flags (flags);
1626           cli ();
1627           in_sleep_flag[dev].mode = WK_SLEEP;
1628           module_select_wait (&in_sleeper[dev], wait);
1629           restore_flags (flags);
1630           return 0;
1631         }
1632 
1633       if (dmap->dma_mode != DMODE_INPUT)
1634         {
1635           if ((audio_devs[dev]->flags & DMA_DUPLEX) && !dmap->qlen &&
1636               audio_devs[dev]->enable_bits & PCM_ENABLE_INPUT &&
1637               audio_devs[dev]->go)
1638             {
1639               unsigned long   flags;
1640 
1641               save_flags (flags);
1642               cli ();
1643               activate_recording (dev, dmap);
1644               restore_flags (flags);
1645             }
1646           return 0;
1647         }
1648 
1649       if (!dmap->qlen)
1650         {
1651           save_flags (flags);
1652           cli ();
1653           in_sleep_flag[dev].mode = WK_SLEEP;
1654           module_select_wait (&in_sleeper[dev], wait);
1655           restore_flags (flags);
1656           return 0;
1657         }
1658       return 1;
1659       break;
1660 
1661     case SEL_OUT:
1662       dmap = audio_devs[dev]->dmap_out;
1663 
1664       if (dmap->mapping_flags & DMA_MAP_MAPPED)
1665         {
1666           if (dmap->qlen)
1667             return 1;
1668 
1669           save_flags (flags);
1670           cli ();
1671           out_sleep_flag[dev].mode = WK_SLEEP;
1672           module_select_wait (&out_sleeper[dev], wait);
1673           restore_flags (flags);
1674           return 0;
1675         }
1676 
1677       if (dmap->dma_mode == DMODE_INPUT)
1678         {
1679           return 0;
1680         }
1681 
1682       if (dmap->dma_mode == DMODE_NONE)
1683         {
1684           return 1;
1685         }
1686 
1687       if (!space_in_queue (dev))
1688         {
1689           save_flags (flags);
1690           cli ();
1691           out_sleep_flag[dev].mode = WK_SLEEP;
1692           module_select_wait (&out_sleeper[dev], wait);
1693           restore_flags (flags);
1694           return 0;
1695         }
1696       return 1;
1697       break;
1698 
1699     case SEL_EX:
1700       return 0;
1701     }
1702 
1703   return 0;
1704 }
1705 
1706 
1707 #else /* CONFIG_AUDIO */
1708 /*
1709  * Stub versions if audio services not included
1710  */
1711 
1712 int
1713 DMAbuf_open (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1714 {
1715   return -ENXIO;
1716 }
1717 
1718 int
1719 DMAbuf_release (int dev, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1720 {
1721   return 0;
1722 }
1723 
1724 int
1725 DMAbuf_getwrbuffer (int dev, char **buf, int *size, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1726 {
1727   return -EIO;
1728 }
1729 
1730 int
1731 DMAbuf_getrdbuffer (int dev, char **buf, int *len, int dontblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1732 {
1733   return -EIO;
1734 }
1735 
1736 int
1737 DMAbuf_rmchars (int dev, int buff_no, int c)
     /* [previous][next][first][last][top][bottom][index][help] */
1738 {
1739   return -EIO;
1740 }
1741 
1742 int
1743 DMAbuf_start_output (int dev, int buff_no, int l)
     /* [previous][next][first][last][top][bottom][index][help] */
1744 {
1745   return -EIO;
1746 }
1747 
1748 int
1749 DMAbuf_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
     /* [previous][next][first][last][top][bottom][index][help] */
1750 {
1751   return -EIO;
1752 }
1753 
1754 long
1755 DMAbuf_init (long mem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1756 {
1757   return mem_start;
1758 }
1759 
1760 int
1761 DMAbuf_start_dma (int dev, unsigned long physaddr, int count, int dma_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1762 {
1763   return -EIO;
1764 }
1765 
1766 int
1767 DMAbuf_open_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1768 {
1769   return -ENXIO;
1770 }
1771 
1772 void
1773 DMAbuf_close_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1774 {
1775   return;
1776 }
1777 
1778 void
1779 DMAbuf_reset_dma (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1780 {
1781   return;
1782 }
1783 
1784 void
1785 DMAbuf_inputintr (int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1786 {
1787   return;
1788 }
1789 
1790 void
1791 DMAbuf_outputintr (int dev, int underrun_flag)
     /* [previous][next][first][last][top][bottom][index][help] */
1792 {
1793   return;
1794 }
1795 #endif

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