OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [sound/] [dmabuf.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.