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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [arch/] [armnommu/] [drivers/] [sound/] [dmabuf.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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