linux/sound/oss/dmabuf.c
<<
>>
Prefs
   1/*
   2 * sound/oss/dmabuf.c
   3 *
   4 * The DMA buffer manager for digitized voice applications
   5 */
   6/*
   7 * Copyright (C) by Hannu Savolainen 1993-1997
   8 *
   9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11 * for more info.
  12 *
  13 * Thomas Sailer   : moved several static variables into struct audio_operations
  14 *                   (which is grossly misnamed btw.) because they have the same
  15 *                   lifetime as the rest in there and dynamic allocation saves
  16 *                   12k or so
  17 * Thomas Sailer   : remove {in,out}_sleep_flag. It was used for the sleeper to
  18 *                   determine if it was woken up by the expiring timeout or by
  19 *                   an explicit wake_up. The return value from schedule_timeout
  20 *                   can be used instead; if 0, the wakeup was due to the timeout.
  21 *
  22 * Rob Riggs            Added persistent DMA buffers (1998/10/17)
  23 */
  24
  25#define BE_CONSERVATIVE
  26#define SAMPLE_ROUNDUP 0
  27
  28#include <linux/mm.h>
  29#include "sound_config.h"
  30
  31#define DMAP_FREE_ON_CLOSE      0
  32#define DMAP_KEEP_ON_CLOSE      1
  33extern int sound_dmap_flag;
  34
  35static void dma_reset_output(int dev);
  36static void dma_reset_input(int dev);
  37static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
  38
  39
  40
  41static int debugmem;            /* switched off by default */
  42static int dma_buffsize = DSP_BUFFSIZE;
  43
  44static long dmabuf_timeout(struct dma_buffparms *dmap)
  45{
  46        long tmout;
  47
  48        tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
  49        tmout += HZ / 5;        /* Some safety distance */
  50        if (tmout < (HZ / 2))
  51                tmout = HZ / 2;
  52        if (tmout > 20 * HZ)
  53                tmout = 20 * HZ;
  54        return tmout;
  55}
  56
  57static int sound_alloc_dmap(struct dma_buffparms *dmap)
  58{
  59        char *start_addr, *end_addr;
  60        int dma_pagesize;
  61        int sz, size;
  62        struct page *page;
  63
  64        dmap->mapping_flags &= ~DMA_MAP_MAPPED;
  65
  66        if (dmap->raw_buf != NULL)
  67                return 0;       /* Already done */
  68        if (dma_buffsize < 4096)
  69                dma_buffsize = 4096;
  70        dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
  71        
  72        /*
  73         *      Now check for the Cyrix problem.
  74         */
  75         
  76        if(isa_dma_bridge_buggy==2)
  77                dma_pagesize=32768;
  78         
  79        dmap->raw_buf = NULL;
  80        dmap->buffsize = dma_buffsize;
  81        if (dmap->buffsize > dma_pagesize)
  82                dmap->buffsize = dma_pagesize;
  83        start_addr = NULL;
  84        /*
  85         * Now loop until we get a free buffer. Try to get smaller buffer if
  86         * it fails. Don't accept smaller than 8k buffer for performance
  87         * reasons.
  88         */
  89        while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
  90                for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
  91                dmap->buffsize = PAGE_SIZE * (1 << sz);
  92                start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
  93                if (start_addr == NULL)
  94                        dmap->buffsize /= 2;
  95        }
  96
  97        if (start_addr == NULL) {
  98                printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
  99                return -ENOMEM;
 100        } else {
 101                /* make some checks */
 102                end_addr = start_addr + dmap->buffsize - 1;
 103
 104                if (debugmem)
 105                        printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
 106                
 107                /* now check if it fits into the same dma-pagesize */
 108
 109                if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
 110                    || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
 111                        printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
 112                        return -EFAULT;
 113                }
 114        }
 115        dmap->raw_buf = start_addr;
 116        dmap->raw_buf_phys = virt_to_bus(start_addr);
 117
 118        for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
 119                SetPageReserved(page);
 120        return 0;
 121}
 122
 123static void sound_free_dmap(struct dma_buffparms *dmap)
 124{
 125        int sz, size;
 126        struct page *page;
 127        unsigned long start_addr, end_addr;
 128
 129        if (dmap->raw_buf == NULL)
 130                return;
 131        if (dmap->mapping_flags & DMA_MAP_MAPPED)
 132                return;         /* Don't free mmapped buffer. Will use it next time */
 133        for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
 134
 135        start_addr = (unsigned long) dmap->raw_buf;
 136        end_addr = start_addr + dmap->buffsize;
 137
 138        for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
 139                ClearPageReserved(page);
 140
 141        free_pages((unsigned long) dmap->raw_buf, sz);
 142        dmap->raw_buf = NULL;
 143}
 144
 145
 146/* Intel version !!!!!!!!! */
 147
 148static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
 149{
 150        unsigned long flags;
 151        int chan = dmap->dma;
 152
 153        /* printk( "Start DMA%d %d, %d\n",  chan,  (int)(physaddr-dmap->raw_buf_phys),  count); */
 154
 155        flags = claim_dma_lock();
 156        disable_dma(chan);
 157        clear_dma_ff(chan);
 158        set_dma_mode(chan, dma_mode);
 159        set_dma_addr(chan, physaddr);
 160        set_dma_count(chan, count);
 161        enable_dma(chan);
 162        release_dma_lock(flags);
 163
 164        return 0;
 165}
 166
 167static void dma_init_buffers(struct dma_buffparms *dmap)
 168{
 169        dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
 170        dmap->byte_counter = 0;
 171        dmap->max_byte_counter = 8000 * 60 * 60;
 172        dmap->bytes_in_use = dmap->buffsize;
 173
 174        dmap->dma_mode = DMODE_NONE;
 175        dmap->mapping_flags = 0;
 176        dmap->neutral_byte = 0x80;
 177        dmap->data_rate = 8000;
 178        dmap->cfrag = -1;
 179        dmap->closing = 0;
 180        dmap->nbufs = 1;
 181        dmap->flags = DMA_BUSY; /* Other flags off */
 182}
 183
 184static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
 185{
 186        int err;
 187        
 188        if (dmap->flags & DMA_BUSY)
 189                return -EBUSY;
 190        if ((err = sound_alloc_dmap(dmap)) < 0)
 191                return err;
 192
 193        if (dmap->raw_buf == NULL) {
 194                printk(KERN_WARNING "Sound: DMA buffers not available\n");
 195                return -ENOSPC; /* Memory allocation failed during boot */
 196        }
 197        if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
 198                printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
 199                return -EBUSY;
 200        }
 201        dma_init_buffers(dmap);
 202        spin_lock_init(&dmap->lock);
 203        dmap->open_mode = mode;
 204        dmap->subdivision = dmap->underrun_count = 0;
 205        dmap->fragment_size = 0;
 206        dmap->max_fragments = 65536;    /* Just a large value */
 207        dmap->byte_counter = 0;
 208        dmap->max_byte_counter = 8000 * 60 * 60;
 209        dmap->applic_profile = APF_NORMAL;
 210        dmap->needs_reorg = 1;
 211        dmap->audio_callback = NULL;
 212        dmap->callback_parm = 0;
 213        return 0;
 214}
 215
 216static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
 217{
 218        unsigned long flags;
 219        
 220        if (dmap->dma >= 0) {
 221                sound_close_dma(dmap->dma);
 222                flags=claim_dma_lock();
 223                disable_dma(dmap->dma);
 224                release_dma_lock(flags);
 225        }
 226        if (dmap->flags & DMA_BUSY)
 227                dmap->dma_mode = DMODE_NONE;
 228        dmap->flags &= ~DMA_BUSY;
 229        
 230        if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
 231                sound_free_dmap(dmap);
 232}
 233
 234
 235static unsigned int default_set_bits(int dev, unsigned int bits)
 236{
 237        mm_segment_t fs = get_fs();
 238
 239        set_fs(get_ds());
 240        audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
 241        set_fs(fs);
 242        return bits;
 243}
 244
 245static int default_set_speed(int dev, int speed)
 246{
 247        mm_segment_t fs = get_fs();
 248
 249        set_fs(get_ds());
 250        audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
 251        set_fs(fs);
 252        return speed;
 253}
 254
 255static short default_set_channels(int dev, short channels)
 256{
 257        int c = channels;
 258        mm_segment_t fs = get_fs();
 259
 260        set_fs(get_ds());
 261        audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
 262        set_fs(fs);
 263        return c;
 264}
 265
 266static void check_driver(struct audio_driver *d)
 267{
 268        if (d->set_speed == NULL)
 269                d->set_speed = default_set_speed;
 270        if (d->set_bits == NULL)
 271                d->set_bits = default_set_bits;
 272        if (d->set_channels == NULL)
 273                d->set_channels = default_set_channels;
 274}
 275
 276int DMAbuf_open(int dev, int mode)
 277{
 278        struct audio_operations *adev = audio_devs[dev];
 279        int retval;
 280        struct dma_buffparms *dmap_in = NULL;
 281        struct dma_buffparms *dmap_out = NULL;
 282
 283        if (!adev)
 284                  return -ENXIO;
 285        if (!(adev->flags & DMA_DUPLEX))
 286                adev->dmap_in = adev->dmap_out;
 287        check_driver(adev->d);
 288
 289        if ((retval = adev->d->open(dev, mode)) < 0)
 290                return retval;
 291        dmap_out = adev->dmap_out;
 292        dmap_in = adev->dmap_in;
 293        if (dmap_in == dmap_out)
 294                adev->flags &= ~DMA_DUPLEX;
 295
 296        if (mode & OPEN_WRITE) {
 297                if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
 298                        adev->d->close(dev);
 299                        return retval;
 300                }
 301        }
 302        adev->enable_bits = mode;
 303
 304        if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
 305                if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
 306                        adev->d->close(dev);
 307                        if (mode & OPEN_WRITE)
 308                                close_dmap(adev, dmap_out);
 309                        return retval;
 310                }
 311        }
 312        adev->open_mode = mode;
 313        adev->go = 1;
 314
 315        adev->d->set_bits(dev, 8);
 316        adev->d->set_channels(dev, 1);
 317        adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
 318        if (adev->dmap_out->dma_mode == DMODE_OUTPUT) 
 319                memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
 320                       adev->dmap_out->bytes_in_use);
 321        return 0;
 322}
 323/* MUST not hold the spinlock */
 324void DMAbuf_reset(int dev)
 325{
 326        if (audio_devs[dev]->open_mode & OPEN_WRITE)
 327                dma_reset_output(dev);
 328
 329        if (audio_devs[dev]->open_mode & OPEN_READ)
 330                dma_reset_input(dev);
 331}
 332
 333static void dma_reset_output(int dev)
 334{
 335        struct audio_operations *adev = audio_devs[dev];
 336        unsigned long flags,f ;
 337        struct dma_buffparms *dmap = adev->dmap_out;
 338
 339        if (!(dmap->flags & DMA_STARTED))       /* DMA is not active */
 340                return;
 341
 342        /*
 343         *      First wait until the current fragment has been played completely
 344         */
 345        spin_lock_irqsave(&dmap->lock,flags);
 346        adev->dmap_out->flags |= DMA_SYNCING;
 347
 348        adev->dmap_out->underrun_count = 0;
 349        if (!signal_pending(current) && adev->dmap_out->qlen && 
 350            adev->dmap_out->underrun_count == 0){
 351                spin_unlock_irqrestore(&dmap->lock,flags);
 352                interruptible_sleep_on_timeout(&adev->out_sleeper,
 353                                               dmabuf_timeout(dmap));
 354                spin_lock_irqsave(&dmap->lock,flags);
 355        }
 356        adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
 357
 358        /*
 359         *      Finally shut the device off
 360         */
 361        if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
 362                adev->d->halt_io(dev);
 363        else
 364                adev->d->halt_output(dev);
 365        adev->dmap_out->flags &= ~DMA_STARTED;
 366        
 367        f=claim_dma_lock();
 368        clear_dma_ff(dmap->dma);
 369        disable_dma(dmap->dma);
 370        release_dma_lock(f);
 371        
 372        dmap->byte_counter = 0;
 373        reorganize_buffers(dev, adev->dmap_out, 0);
 374        dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
 375        spin_unlock_irqrestore(&dmap->lock,flags);
 376}
 377
 378static void dma_reset_input(int dev)
 379{
 380        struct audio_operations *adev = audio_devs[dev];
 381        unsigned long flags;
 382        struct dma_buffparms *dmap = adev->dmap_in;
 383
 384        spin_lock_irqsave(&dmap->lock,flags);
 385        if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
 386                adev->d->halt_io(dev);
 387        else
 388                adev->d->halt_input(dev);
 389        adev->dmap_in->flags &= ~DMA_STARTED;
 390
 391        dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
 392        dmap->byte_counter = 0;
 393        reorganize_buffers(dev, adev->dmap_in, 1);
 394        spin_unlock_irqrestore(&dmap->lock,flags);
 395}
 396/* MUST be called with holding the dmap->lock */
 397void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
 398{
 399        struct audio_operations *adev = audio_devs[dev];
 400
 401        if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
 402                return;         /* Don't start DMA yet */
 403        dmap->dma_mode = DMODE_OUTPUT;
 404
 405        if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
 406                if (!(dmap->flags & DMA_STARTED)) {
 407                        reorganize_buffers(dev, dmap, 0);
 408                        if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
 409                                return;
 410                        if (!(dmap->flags & DMA_NODMA))
 411                                local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
 412                        dmap->flags |= DMA_STARTED;
 413                }
 414                if (dmap->counts[dmap->qhead] == 0)
 415                        dmap->counts[dmap->qhead] = dmap->fragment_size;
 416                dmap->dma_mode = DMODE_OUTPUT;
 417                adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
 418                                      dmap->counts[dmap->qhead], 1);
 419                if (adev->d->trigger)
 420                        adev->d->trigger(dev,adev->enable_bits * adev->go);
 421        }
 422        dmap->flags |= DMA_ACTIVE;
 423}
 424
 425int DMAbuf_sync(int dev)
 426{
 427        struct audio_operations *adev = audio_devs[dev];
 428        unsigned long flags;
 429        int n = 0;
 430        struct dma_buffparms *dmap;
 431
 432        if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
 433                return 0;
 434
 435        if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
 436                dmap = adev->dmap_out;
 437                spin_lock_irqsave(&dmap->lock,flags);
 438                if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
 439                        DMAbuf_launch_output(dev, dmap);
 440                adev->dmap_out->flags |= DMA_SYNCING;
 441                adev->dmap_out->underrun_count = 0;
 442                while (!signal_pending(current) && n++ <= adev->dmap_out->nbufs && 
 443                       adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
 444                        long t = dmabuf_timeout(dmap);
 445                        spin_unlock_irqrestore(&dmap->lock,flags);
 446                        /* FIXME: not safe may miss events */
 447                        t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
 448                        spin_lock_irqsave(&dmap->lock,flags);
 449                        if (!t) {
 450                                adev->dmap_out->flags &= ~DMA_SYNCING;
 451                                spin_unlock_irqrestore(&dmap->lock,flags);
 452                                return adev->dmap_out->qlen;
 453                        }
 454                }
 455                adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
 456                
 457                /*
 458                 * Some devices such as GUS have huge amount of on board RAM for the
 459                 * audio data. We have to wait until the device has finished playing.
 460                 */
 461
 462                /* still holding the lock */
 463                if (adev->d->local_qlen) {   /* Device has hidden buffers */
 464                        while (!signal_pending(current) &&
 465                               adev->d->local_qlen(dev)){
 466                                spin_unlock_irqrestore(&dmap->lock,flags);
 467                                interruptible_sleep_on_timeout(&adev->out_sleeper,
 468                                                               dmabuf_timeout(dmap));
 469                                spin_lock_irqsave(&dmap->lock,flags);
 470                        }
 471                }
 472                spin_unlock_irqrestore(&dmap->lock,flags);
 473        }
 474        adev->dmap_out->dma_mode = DMODE_NONE;
 475        return adev->dmap_out->qlen;
 476}
 477
 478int DMAbuf_release(int dev, int mode)
 479{
 480        struct audio_operations *adev = audio_devs[dev];
 481        struct dma_buffparms *dmap;
 482        unsigned long flags;
 483
 484        dmap = adev->dmap_out;
 485        if (adev->open_mode & OPEN_WRITE)
 486                adev->dmap_out->closing = 1;
 487
 488        if (adev->open_mode & OPEN_READ){
 489                adev->dmap_in->closing = 1;
 490                dmap = adev->dmap_in;
 491        }
 492        if (adev->open_mode & OPEN_WRITE)
 493                if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
 494                        if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
 495                                DMAbuf_sync(dev);
 496        if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
 497                memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
 498
 499        DMAbuf_reset(dev);
 500        spin_lock_irqsave(&dmap->lock,flags);
 501        adev->d->close(dev);
 502
 503        if (adev->open_mode & OPEN_WRITE)
 504                close_dmap(adev, adev->dmap_out);
 505
 506        if (adev->open_mode == OPEN_READ ||
 507            (adev->open_mode != OPEN_WRITE &&
 508             (adev->flags & DMA_DUPLEX)))
 509                close_dmap(adev, adev->dmap_in);
 510        adev->open_mode = 0;
 511        spin_unlock_irqrestore(&dmap->lock,flags);
 512        return 0;
 513}
 514/* called with dmap->lock dold */
 515int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
 516{
 517        struct audio_operations *adev = audio_devs[dev];
 518        int  err;
 519
 520        if (!(adev->open_mode & OPEN_READ))
 521                return 0;
 522        if (!(adev->enable_bits & PCM_ENABLE_INPUT))
 523                return 0;
 524        if (dmap->dma_mode == DMODE_OUTPUT) {   /* Direction change */
 525                /* release lock - it's not recursive */
 526                spin_unlock_irq(&dmap->lock);
 527                DMAbuf_sync(dev);
 528                DMAbuf_reset(dev);
 529                spin_lock_irq(&dmap->lock);
 530                dmap->dma_mode = DMODE_NONE;
 531        }
 532        if (!dmap->dma_mode) {
 533                reorganize_buffers(dev, dmap, 1);
 534                if ((err = adev->d->prepare_for_input(dev,
 535                                dmap->fragment_size, dmap->nbufs)) < 0)
 536                        return err;
 537                dmap->dma_mode = DMODE_INPUT;
 538        }
 539        if (!(dmap->flags & DMA_ACTIVE)) {
 540                if (dmap->needs_reorg)
 541                        reorganize_buffers(dev, dmap, 0);
 542                local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
 543                adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
 544                                     dmap->fragment_size, 0);
 545                dmap->flags |= DMA_ACTIVE;
 546                if (adev->d->trigger)
 547                        adev->d->trigger(dev, adev->enable_bits * adev->go);
 548        }
 549        return 0;
 550}
 551/* acquires lock */
 552int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
 553{
 554        struct audio_operations *adev = audio_devs[dev];
 555        unsigned long flags;
 556        int err = 0, n = 0;
 557        struct dma_buffparms *dmap = adev->dmap_in;
 558        int go;
 559
 560        if (!(adev->open_mode & OPEN_READ))
 561                return -EIO;
 562        spin_lock_irqsave(&dmap->lock,flags);
 563        if (dmap->needs_reorg)
 564                reorganize_buffers(dev, dmap, 0);
 565        if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
 566/*                printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
 567                  spin_unlock_irqrestore(&dmap->lock,flags);
 568                  return -EINVAL;
 569        } else while (dmap->qlen <= 0 && n++ < 10) {
 570                long timeout = MAX_SCHEDULE_TIMEOUT;
 571                if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
 572                        spin_unlock_irqrestore(&dmap->lock,flags);
 573                        return -EAGAIN;
 574                }
 575                if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
 576                        spin_unlock_irqrestore(&dmap->lock,flags);
 577                        return err;
 578                }
 579                /* Wait for the next block */
 580
 581                if (dontblock) {
 582                        spin_unlock_irqrestore(&dmap->lock,flags);
 583                        return -EAGAIN;
 584                }
 585                if ((go = adev->go))
 586                        timeout = dmabuf_timeout(dmap);
 587
 588                spin_unlock_irqrestore(&dmap->lock,flags);
 589                timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
 590                                                         timeout);
 591                if (!timeout) {
 592                        /* FIXME: include device name */
 593                        err = -EIO;
 594                        printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
 595                        dma_reset_input(dev);
 596                } else
 597                        err = -EINTR;
 598                spin_lock_irqsave(&dmap->lock,flags);
 599        }
 600        spin_unlock_irqrestore(&dmap->lock,flags);
 601
 602        if (dmap->qlen <= 0)
 603                return err ? err : -EINTR;
 604        *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
 605        *len = dmap->fragment_size - dmap->counts[dmap->qhead];
 606
 607        return dmap->qhead;
 608}
 609
 610int DMAbuf_rmchars(int dev, int buff_no, int c)
 611{
 612        struct audio_operations *adev = audio_devs[dev];
 613        struct dma_buffparms *dmap = adev->dmap_in;
 614        int p = dmap->counts[dmap->qhead] + c;
 615
 616        if (dmap->mapping_flags & DMA_MAP_MAPPED)
 617        {
 618/*                printk("Sound: Can't read from mmapped device (2)\n");*/
 619                return -EINVAL;
 620        }
 621        else if (dmap->qlen <= 0)
 622                return -EIO;
 623        else if (p >= dmap->fragment_size) {  /* This buffer is completely empty */
 624                dmap->counts[dmap->qhead] = 0;
 625                dmap->qlen--;
 626                dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
 627        }
 628        else dmap->counts[dmap->qhead] = p;
 629
 630        return 0;
 631}
 632/* MUST be called with dmap->lock hold */
 633int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
 634{
 635        /*
 636         *      Try to approximate the active byte position of the DMA pointer within the
 637         *      buffer area as well as possible.
 638         */
 639
 640        int pos;
 641        unsigned long f;
 642
 643        if (!(dmap->flags & DMA_ACTIVE))
 644                pos = 0;
 645        else {
 646                int chan = dmap->dma;
 647                
 648                f=claim_dma_lock();
 649                clear_dma_ff(chan);
 650                
 651                if(!isa_dma_bridge_buggy)
 652                        disable_dma(dmap->dma);
 653                
 654                pos = get_dma_residue(chan);
 655                
 656                pos = dmap->bytes_in_use - pos;
 657
 658                if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
 659                        if (direction == DMODE_OUTPUT) {
 660                                if (dmap->qhead == 0)
 661                                        if (pos > dmap->fragment_size)
 662                                                pos = 0;
 663                        } else {
 664                                if (dmap->qtail == 0)
 665                                        if (pos > dmap->fragment_size)
 666                                                pos = 0;
 667                        }
 668                }
 669                if (pos < 0)
 670                        pos = 0;
 671                if (pos >= dmap->bytes_in_use)
 672                        pos = 0;
 673                
 674                if(!isa_dma_bridge_buggy)
 675                        enable_dma(dmap->dma);
 676                        
 677                release_dma_lock(f);
 678        }
 679        /* printk( "%04x ",  pos); */
 680
 681        return pos;
 682}
 683
 684/*
 685 *      DMAbuf_start_devices() is called by the /dev/music driver to start
 686 *      one or more audio devices at desired moment.
 687 */
 688
 689void DMAbuf_start_devices(unsigned int devmask)
 690{
 691        struct audio_operations *adev;
 692        int dev;
 693
 694        for (dev = 0; dev < num_audiodevs; dev++) {
 695                if (!(devmask & (1 << dev)))
 696                        continue;
 697                if (!(adev = audio_devs[dev]))
 698                        continue;
 699                if (adev->open_mode == 0)
 700                        continue;
 701                if (adev->go)
 702                        continue;
 703                /* OK to start the device */
 704                adev->go = 1;
 705                if (adev->d->trigger)
 706                        adev->d->trigger(dev,adev->enable_bits * adev->go);
 707        }
 708}
 709/* via poll called without a lock ?*/
 710int DMAbuf_space_in_queue(int dev)
 711{
 712        struct audio_operations *adev = audio_devs[dev];
 713        int len, max, tmp;
 714        struct dma_buffparms *dmap = adev->dmap_out;
 715        int lim = dmap->nbufs;
 716
 717        if (lim < 2)
 718                lim = 2;
 719
 720        if (dmap->qlen >= lim)  /* No space at all */
 721                return 0;
 722
 723        /*
 724         *      Verify that there are no more pending buffers than the limit
 725         *      defined by the process.
 726         */
 727
 728        max = dmap->max_fragments;
 729        if (max > lim)
 730                max = lim;
 731        len = dmap->qlen;
 732
 733        if (adev->d->local_qlen) {
 734                tmp = adev->d->local_qlen(dev);
 735                if (tmp && len)
 736                        tmp--;  /* This buffer has been counted twice */
 737                len += tmp;
 738        }
 739        if (dmap->byte_counter % dmap->fragment_size)   /* There is a partial fragment */
 740                len = len + 1;
 741
 742        if (len >= max)
 743                return 0;
 744        return max - len;
 745}
 746/* MUST not hold the spinlock  - this function may sleep */
 747static int output_sleep(int dev, int dontblock)
 748{
 749        struct audio_operations *adev = audio_devs[dev];
 750        int err = 0;
 751        struct dma_buffparms *dmap = adev->dmap_out;
 752        long timeout;
 753        long timeout_value;
 754
 755        if (dontblock)
 756                return -EAGAIN;
 757        if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
 758                return -EAGAIN;
 759
 760        /*
 761         * Wait for free space
 762         */
 763        if (signal_pending(current))
 764                return -EINTR;
 765        timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
 766        if (timeout) 
 767                timeout_value = dmabuf_timeout(dmap);
 768        else
 769                timeout_value = MAX_SCHEDULE_TIMEOUT;
 770        timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
 771                                                       timeout_value);
 772        if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
 773                printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
 774                dma_reset_output(dev);
 775        } else {
 776                if (signal_pending(current))
 777                        err = -EINTR;
 778        }
 779        return err;
 780}
 781/* called with the lock held */
 782static int find_output_space(int dev, char **buf, int *size)
 783{
 784        struct audio_operations *adev = audio_devs[dev];
 785        struct dma_buffparms *dmap = adev->dmap_out;
 786        unsigned long active_offs;
 787        long len, offs;
 788        int maxfrags;
 789        int occupied_bytes = (dmap->user_counter % dmap->fragment_size);
 790
 791        *buf = dmap->raw_buf;
 792        if (!(maxfrags = DMAbuf_space_in_queue(dev)) && !occupied_bytes)
 793                return 0;
 794
 795#ifdef BE_CONSERVATIVE
 796        active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
 797#else
 798        active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
 799        /* Check for pointer wrapping situation */
 800        if (active_offs >= dmap->bytes_in_use)
 801                active_offs = 0;
 802        active_offs += dmap->byte_counter;
 803#endif
 804
 805        offs = (dmap->user_counter % dmap->bytes_in_use) & ~SAMPLE_ROUNDUP;
 806        if (offs < 0 || offs >= dmap->bytes_in_use) {
 807                printk(KERN_ERR "Sound: Got unexpected offs %ld. Giving up.\n", offs);
 808                printk("Counter = %ld, bytes=%d\n", dmap->user_counter, dmap->bytes_in_use);
 809                return 0;
 810        }
 811        *buf = dmap->raw_buf + offs;
 812
 813        len = active_offs + dmap->bytes_in_use - dmap->user_counter;    /* Number of unused bytes in buffer */
 814
 815        if ((offs + len) > dmap->bytes_in_use)
 816                len = dmap->bytes_in_use - offs;
 817        if (len < 0) {
 818                return 0;
 819        }
 820        if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
 821                len = (maxfrags * dmap->fragment_size) - occupied_bytes;
 822        *size = len & ~SAMPLE_ROUNDUP;
 823        return (*size > 0);
 824}
 825/* acquires lock  */
 826int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
 827{
 828        struct audio_operations *adev = audio_devs[dev];
 829        unsigned long flags;
 830        int err = -EIO;
 831        struct dma_buffparms *dmap = adev->dmap_out;
 832
 833        if (dmap->mapping_flags & DMA_MAP_MAPPED) {
 834/*              printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
 835                return -EINVAL;
 836        }
 837        spin_lock_irqsave(&dmap->lock,flags);
 838        if (dmap->needs_reorg)
 839                reorganize_buffers(dev, dmap, 0);
 840
 841        if (dmap->dma_mode == DMODE_INPUT) {    /* Direction change */
 842                spin_unlock_irqrestore(&dmap->lock,flags);
 843                DMAbuf_reset(dev);
 844                spin_lock_irqsave(&dmap->lock,flags);
 845        }
 846        dmap->dma_mode = DMODE_OUTPUT;
 847
 848        while (find_output_space(dev, buf, size) <= 0) {
 849                spin_unlock_irqrestore(&dmap->lock,flags);
 850                if ((err = output_sleep(dev, dontblock)) < 0) {
 851                        return err;
 852                }
 853                spin_lock_irqsave(&dmap->lock,flags);
 854        }
 855
 856        spin_unlock_irqrestore(&dmap->lock,flags);
 857        return 0;
 858}
 859/* has to acquire dmap->lock */
 860int DMAbuf_move_wrpointer(int dev, int l)
 861{
 862        struct audio_operations *adev = audio_devs[dev];
 863        struct dma_buffparms *dmap = adev->dmap_out;
 864        unsigned long ptr;
 865        unsigned long end_ptr, p;
 866        int post;
 867        unsigned long flags;
 868
 869        spin_lock_irqsave(&dmap->lock,flags);
 870        post= (dmap->flags & DMA_POST);
 871        ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
 872
 873        dmap->flags &= ~DMA_POST;
 874        dmap->cfrag = -1;
 875        dmap->user_counter += l;
 876        dmap->flags |= DMA_DIRTY;
 877
 878        if (dmap->byte_counter >= dmap->max_byte_counter) {
 879                /* Wrap the byte counters */
 880                long decr = dmap->byte_counter;
 881                dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
 882                decr -= dmap->byte_counter;
 883                dmap->user_counter -= decr;
 884        }
 885        end_ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
 886
 887        p = (dmap->user_counter - 1) % dmap->bytes_in_use;
 888        dmap->neutral_byte = dmap->raw_buf[p];
 889
 890        /* Update the fragment based bookkeeping too */
 891        while (ptr < end_ptr) {
 892                dmap->counts[dmap->qtail] = dmap->fragment_size;
 893                dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
 894                dmap->qlen++;
 895                ptr += dmap->fragment_size;
 896        }
 897
 898        dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
 899
 900        /*
 901         *      Let the low level driver perform some postprocessing to
 902         *      the written data.
 903         */
 904        if (adev->d->postprocess_write)
 905                adev->d->postprocess_write(dev);
 906
 907        if (!(dmap->flags & DMA_ACTIVE))
 908                if (dmap->qlen > 1 || (dmap->qlen > 0 && (post || dmap->qlen >= dmap->nbufs - 1)))
 909                        DMAbuf_launch_output(dev, dmap);
 910
 911        spin_unlock_irqrestore(&dmap->lock,flags);
 912        return 0;
 913}
 914
 915int DMAbuf_start_dma(int dev, unsigned long physaddr, int count, int dma_mode)
 916{
 917        struct audio_operations *adev = audio_devs[dev];
 918        struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
 919
 920        if (dmap->raw_buf == NULL) {
 921                printk(KERN_ERR "sound: DMA buffer(1) == NULL\n");
 922                printk("Device %d, chn=%s\n", dev, (dmap == adev->dmap_out) ? "out" : "in");
 923                return 0;
 924        }
 925        if (dmap->dma < 0)
 926                return 0;
 927        sound_start_dma(dmap, physaddr, count, dma_mode);
 928        return count;
 929}
 930EXPORT_SYMBOL(DMAbuf_start_dma);
 931
 932static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
 933{
 934        struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
 935
 936        if (dmap->raw_buf == NULL) {
 937                printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
 938                printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
 939                return 0;
 940        }
 941        if (dmap->flags & DMA_NODMA)
 942                return 1;
 943        if (dmap->dma < 0)
 944                return 0;
 945        sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
 946        dmap->flags |= DMA_STARTED;
 947        return count;
 948}
 949
 950static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
 951{
 952        struct audio_operations *adev = audio_devs[dev];
 953
 954        if (dmap->audio_callback != NULL)
 955                dmap->audio_callback(dev, dmap->callback_parm);
 956        wake_up(&adev->out_sleeper);
 957        wake_up(&adev->poll_sleeper);
 958}
 959/* called with dmap->lock held in irq context*/
 960static void do_outputintr(int dev, int dummy)
 961{
 962        struct audio_operations *adev = audio_devs[dev];
 963        struct dma_buffparms *dmap = adev->dmap_out;
 964        int this_fragment;
 965
 966        if (dmap->raw_buf == NULL) {
 967                printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
 968                return;
 969        }
 970        if (dmap->mapping_flags & DMA_MAP_MAPPED) {     /* Virtual memory mapped access */
 971                /* mmapped access */
 972                dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
 973                if (dmap->qhead == 0) {     /* Wrapped */
 974                        dmap->byte_counter += dmap->bytes_in_use;
 975                        if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
 976                                long decr = dmap->byte_counter;
 977                                dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
 978                                decr -= dmap->byte_counter;
 979                                dmap->user_counter -= decr;
 980                        }
 981                }
 982                dmap->qlen++;   /* Yes increment it (don't decrement) */
 983                if (!(adev->flags & DMA_AUTOMODE))
 984                        dmap->flags &= ~DMA_ACTIVE;
 985                dmap->counts[dmap->qhead] = dmap->fragment_size;
 986                DMAbuf_launch_output(dev, dmap);
 987                finish_output_interrupt(dev, dmap);
 988                return;
 989        }
 990
 991        dmap->qlen--;
 992        this_fragment = dmap->qhead;
 993        dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
 994
 995        if (dmap->qhead == 0) { /* Wrapped */
 996                dmap->byte_counter += dmap->bytes_in_use;
 997                if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
 998                        long decr = dmap->byte_counter;
 999                        dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
1000                        decr -= dmap->byte_counter;
1001                        dmap->user_counter -= decr;
1002                }
1003        }
1004        if (!(adev->flags & DMA_AUTOMODE))
1005                dmap->flags &= ~DMA_ACTIVE;
1006                
1007        /*
1008         *      This is  dmap->qlen <= 0 except when closing when
1009         *      dmap->qlen < 0
1010         */
1011         
1012        while (dmap->qlen <= -dmap->closing) {
1013                dmap->underrun_count++;
1014                dmap->qlen++;
1015                if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
1016                        dmap->flags &= ~DMA_DIRTY;
1017                        memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
1018                               adev->dmap_out->buffsize);
1019                }
1020                dmap->user_counter += dmap->fragment_size;
1021                dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1022        }
1023        if (dmap->qlen > 0)
1024                DMAbuf_launch_output(dev, dmap);
1025        finish_output_interrupt(dev, dmap);
1026}
1027/* called in irq context */
1028void DMAbuf_outputintr(int dev, int notify_only)
1029{
1030        struct audio_operations *adev = audio_devs[dev];
1031        unsigned long flags;
1032        struct dma_buffparms *dmap = adev->dmap_out;
1033
1034        spin_lock_irqsave(&dmap->lock,flags);
1035        if (!(dmap->flags & DMA_NODMA)) {
1036                int chan = dmap->dma, pos, n;
1037                unsigned long f;
1038                
1039                f=claim_dma_lock();
1040                
1041                if(!isa_dma_bridge_buggy)
1042                        disable_dma(dmap->dma);
1043                clear_dma_ff(chan);
1044                pos = dmap->bytes_in_use - get_dma_residue(chan);
1045                if(!isa_dma_bridge_buggy)
1046                        enable_dma(dmap->dma);
1047                release_dma_lock(f);
1048                
1049                pos = pos / dmap->fragment_size;        /* Actual qhead */
1050                if (pos < 0 || pos >= dmap->nbufs)
1051                        pos = 0;
1052                n = 0;
1053                while (dmap->qhead != pos && n++ < dmap->nbufs)
1054                        do_outputintr(dev, notify_only);
1055        }
1056        else
1057                do_outputintr(dev, notify_only);
1058        spin_unlock_irqrestore(&dmap->lock,flags);
1059}
1060EXPORT_SYMBOL(DMAbuf_outputintr);
1061
1062/* called with dmap->lock held in irq context */
1063static void do_inputintr(int dev)
1064{
1065        struct audio_operations *adev = audio_devs[dev];
1066        struct dma_buffparms *dmap = adev->dmap_in;
1067
1068        if (dmap->raw_buf == NULL) {
1069                printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
1070                return;
1071        }
1072        if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1073                dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1074                if (dmap->qtail == 0) {         /* Wrapped */
1075                        dmap->byte_counter += dmap->bytes_in_use;
1076                        if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
1077                                long decr = dmap->byte_counter;
1078                                dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1079                                decr -= dmap->byte_counter;
1080                                dmap->user_counter -= decr;
1081                        }
1082                }
1083                dmap->qlen++;
1084
1085                if (!(adev->flags & DMA_AUTOMODE)) {
1086                        if (dmap->needs_reorg)
1087                                reorganize_buffers(dev, dmap, 0);
1088                        local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
1089                        adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
1090                                             dmap->fragment_size, 1);
1091                        if (adev->d->trigger)
1092                                adev->d->trigger(dev, adev->enable_bits * adev->go);
1093                }
1094                dmap->flags |= DMA_ACTIVE;
1095        } else if (dmap->qlen >= (dmap->nbufs - 1)) {
1096                printk(KERN_WARNING "Sound: Recording overrun\n");
1097                dmap->underrun_count++;
1098
1099                /* Just throw away the oldest fragment but keep the engine running */
1100                dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1101                dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1102        } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
1103                dmap->qlen++;
1104                dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1105                if (dmap->qtail == 0) {         /* Wrapped */
1106                        dmap->byte_counter += dmap->bytes_in_use;
1107                        if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
1108                                long decr = dmap->byte_counter;
1109                                dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1110                                decr -= dmap->byte_counter;
1111                                dmap->user_counter -= decr;
1112                        }
1113                }
1114        }
1115        if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
1116                local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
1117                adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
1118                if (adev->d->trigger)
1119                        adev->d->trigger(dev,adev->enable_bits * adev->go);
1120        }
1121        dmap->flags |= DMA_ACTIVE;
1122        if (dmap->qlen > 0)
1123        {
1124                wake_up(&adev->in_sleeper);
1125                wake_up(&adev->poll_sleeper);
1126        }
1127}
1128/* called in irq context */
1129void DMAbuf_inputintr(int dev)
1130{
1131        struct audio_operations *adev = audio_devs[dev];
1132        struct dma_buffparms *dmap = adev->dmap_in;
1133        unsigned long flags;
1134
1135        spin_lock_irqsave(&dmap->lock,flags);
1136
1137        if (!(dmap->flags & DMA_NODMA)) {
1138                int chan = dmap->dma, pos, n;
1139                unsigned long f;
1140                
1141                f=claim_dma_lock();
1142                if(!isa_dma_bridge_buggy)
1143                        disable_dma(dmap->dma);
1144                clear_dma_ff(chan);
1145                pos = dmap->bytes_in_use - get_dma_residue(chan);
1146                if(!isa_dma_bridge_buggy)
1147                        enable_dma(dmap->dma);
1148                release_dma_lock(f);
1149
1150                pos = pos / dmap->fragment_size;        /* Actual qhead */
1151                if (pos < 0 || pos >= dmap->nbufs)
1152                        pos = 0;
1153
1154                n = 0;
1155                while (dmap->qtail != pos && ++n < dmap->nbufs)
1156                        do_inputintr(dev);
1157        } else
1158                do_inputintr(dev);
1159        spin_unlock_irqrestore(&dmap->lock,flags);
1160}
1161EXPORT_SYMBOL(DMAbuf_inputintr);
1162
1163void DMAbuf_init(int dev, int dma1, int dma2)
1164{
1165        struct audio_operations *adev = audio_devs[dev];
1166        /*
1167         * NOTE! This routine could be called several times.
1168         */
1169
1170        if (adev && adev->dmap_out == NULL) {
1171                if (adev->d == NULL)
1172                        panic("OSS: audio_devs[%d]->d == NULL\n", dev);
1173
1174                if (adev->parent_dev) {  /* Use DMA map of the parent dev */
1175                        int parent = adev->parent_dev - 1;
1176                        adev->dmap_out = audio_devs[parent]->dmap_out;
1177                        adev->dmap_in = audio_devs[parent]->dmap_in;
1178                } else {
1179                        adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
1180                        adev->dmap_out->dma = dma1;
1181                        if (adev->flags & DMA_DUPLEX) {
1182                                adev->dmap_in = &adev->dmaps[1];
1183                                adev->dmap_in->dma = dma2;
1184                        }
1185                }
1186                /* Persistent DMA buffers allocated here */
1187                if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1188                        if (adev->dmap_in->raw_buf == NULL)
1189                                sound_alloc_dmap(adev->dmap_in);
1190                        if (adev->dmap_out->raw_buf == NULL)
1191                                sound_alloc_dmap(adev->dmap_out);
1192                }
1193        }
1194}
1195
1196/* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
1197static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
1198{
1199        struct audio_operations *adev = audio_devs[dev];
1200        struct dma_buffparms *dmap = adev->dmap_in;
1201
1202        if (!(adev->open_mode & OPEN_READ))
1203                return 0;
1204        if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1205                if (dmap->qlen)
1206                        return POLLIN | POLLRDNORM;
1207                return 0;
1208        }
1209        if (dmap->dma_mode != DMODE_INPUT) {
1210                if (dmap->dma_mode == DMODE_NONE &&
1211                    adev->enable_bits & PCM_ENABLE_INPUT &&
1212                    !dmap->qlen && adev->go) {
1213                        unsigned long flags;
1214                        
1215                        spin_lock_irqsave(&dmap->lock,flags);
1216                        DMAbuf_activate_recording(dev, dmap);
1217                        spin_unlock_irqrestore(&dmap->lock,flags);
1218                }
1219                return 0;
1220        }
1221        if (!dmap->qlen)
1222                return 0;
1223        return POLLIN | POLLRDNORM;
1224}
1225
1226static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
1227{
1228        struct audio_operations *adev = audio_devs[dev];
1229        struct dma_buffparms *dmap = adev->dmap_out;
1230        
1231        if (!(adev->open_mode & OPEN_WRITE))
1232                return 0;
1233        if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1234                if (dmap->qlen)
1235                        return POLLOUT | POLLWRNORM;
1236                return 0;
1237        }
1238        if (dmap->dma_mode == DMODE_INPUT)
1239                return 0;
1240        if (dmap->dma_mode == DMODE_NONE)
1241                return POLLOUT | POLLWRNORM;
1242        if (!DMAbuf_space_in_queue(dev))
1243                return 0;
1244        return POLLOUT | POLLWRNORM;
1245}
1246
1247unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
1248{
1249        struct audio_operations *adev = audio_devs[dev];
1250        poll_wait(file, &adev->poll_sleeper, wait);
1251        return poll_input(file, dev, wait) | poll_output(file, dev, wait);
1252}
1253
1254void DMAbuf_deinit(int dev)
1255{
1256        struct audio_operations *adev = audio_devs[dev];
1257        /* This routine is called when driver is being unloaded */
1258        if (!adev)
1259                return;
1260
1261        /* Persistent DMA buffers deallocated here */
1262        if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1263                sound_free_dmap(adev->dmap_out);
1264                if (adev->flags & DMA_DUPLEX)
1265                        sound_free_dmap(adev->dmap_in);
1266        }
1267}
1268
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.