/*
  * Scatter-Gather generic device pages
  */
+void *snd_malloc_sgbuf_pages(struct device *device,
+                            size_t size, struct snd_dma_buffer *dmab,
+                            size_t *res_size);
+int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
+
 struct snd_sg_page {
        void *buf;
        dma_addr_t addr;
        return sgbuf->table[offset >> PAGE_SHIFT].addr + offset % PAGE_SIZE;
 }
 
+/*
+ * return the virtual address at the corresponding offset
+ */
+static inline void *snd_sgbuf_get_ptr(struct snd_sg_buf *sgbuf, size_t offset)
+{
+       return sgbuf->table[offset >> PAGE_SHIFT].buf + offset % PAGE_SIZE;
+}
 
 /* allocate/release a buffer */
 int snd_dma_alloc_pages(int type, struct device *dev, size_t size,
 
 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
 
-#define snd_pcm_substream_sgbuf(substream) ((substream)->runtime->dma_buffer_p->private_data)
-#define snd_pcm_sgbuf_pages(size) snd_sgbuf_aligned_pages(size)
-#define snd_pcm_sgbuf_get_addr(sgbuf,ofs) snd_sgbuf_get_addr(sgbuf,ofs)
-struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset);
+/*
+ * SG-buffer handling
+ */
+#define snd_pcm_substream_sgbuf(substream) \
+       ((substream)->runtime->dma_buffer_p->private_data)
+
+static inline dma_addr_t
+snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
+{
+       struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
+       return snd_sgbuf_get_addr(sg, ofs);
+}
+
+static inline void *
+snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
+{
+       struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
+       return snd_sgbuf_get_ptr(sg, ofs);
+}
+
+struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
+                                   unsigned long offset);
+
 
 /* handle mmap counter - PCM mmap callback should handle this counter properly */
 static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)
 
 MODULE_LICENSE("GPL");
 
 
-/*
- */
-
-void *snd_malloc_sgbuf_pages(struct device *device,
-                             size_t size, struct snd_dma_buffer *dmab,
-                            size_t *res_size);
-int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
-
 /*
  */
 
 
        /* Virtual page extender stuff */
        int nr_periods;
        int period_bytes;
-       struct snd_sg_buf *sgbuf;       /* DMA Scatter Gather struct */
        int period_real;
        int period_virt;
 
 
 /* DMA Engines. */
 static void vortex_adbdma_setbuffers(vortex_t * vortex, int adbdma,
-                                    struct snd_sg_buf * sgbuf, int size,
-                                    int count);
+                                    int size, int count);
 static void vortex_adbdma_setmode(vortex_t * vortex, int adbdma, int ie,
                                  int dir, int fmt, int d,
                                  u32 offset);
 static void vortex_adbdma_setstartbuffer(vortex_t * vortex, int adbdma, int sb);
 #ifndef CHIP_AU8810
 static void vortex_wtdma_setbuffers(vortex_t * vortex, int wtdma,
-                                   struct snd_sg_buf * sgbuf, int size,
-                                   int count);
+                                   int size, int count);
 static void vortex_wtdma_setmode(vortex_t * vortex, int wtdma, int ie, int fmt, int d, /*int e, */
                                 u32 offset);
 static void vortex_wtdma_setstartbuffer(vortex_t * vortex, int wtdma, int sb);
 
 
 static void
 vortex_adbdma_setbuffers(vortex_t * vortex, int adbdma,
-                        struct snd_sg_buf * sgbuf, int psize, int count)
+                        int psize, int count)
 {
        stream_t *dma = &vortex->dma_adb[adbdma];
 
-       if (sgbuf == NULL) {
-               printk(KERN_INFO "vortex: FATAL: sgbuf is NULL!\n");
-               return;
-       }
-       //printk(KERN_INFO "vortex: page count = %d, tblcount = %d\n", count, sgbuf->tblsize);
-
        dma->period_bytes = psize;
        dma->nr_periods = count;
-       dma->sgbuf = sgbuf;
 
        dma->cfg0 = 0;
        dma->cfg1 = 0;
                dma->cfg1 |= 0x88000000 | 0x44000000 | 0x30000000 | (psize - 1);
                hwwrite(vortex->mmio,
                        VORTEX_ADBDMA_BUFBASE + (adbdma << 4) + 0xc,
-                       snd_sgbuf_get_addr(sgbuf, psize * 3));
+                       snd_pcm_sgbuf_get_addr(dma->substream, psize * 3));
                /* 3 pages */
        case 3:
                dma->cfg0 |= 0x12000000;
                dma->cfg1 |= 0x80000000 | 0x40000000 | ((psize - 1) << 0xc);
                hwwrite(vortex->mmio,
                        VORTEX_ADBDMA_BUFBASE + (adbdma << 4) + 0x8,
-                       snd_sgbuf_get_addr(sgbuf, psize * 2));
+                       snd_pcm_sgbuf_get_addr(dma->substream, psize * 2));
                /* 2 pages */
        case 2:
                dma->cfg0 |= 0x88000000 | 0x44000000 | 0x10000000 | (psize - 1);
                hwwrite(vortex->mmio,
                        VORTEX_ADBDMA_BUFBASE + (adbdma << 4) + 0x4,
-                       snd_sgbuf_get_addr(sgbuf, psize));
+                       snd_pcm_sgbuf_get_addr(dma->substream, psize));
                /* 1 page */
        case 1:
                dma->cfg0 |= 0x80000000 | 0x40000000 | ((psize - 1) << 0xc);
                hwwrite(vortex->mmio,
                        VORTEX_ADBDMA_BUFBASE + (adbdma << 4),
-                       snd_sgbuf_get_addr(sgbuf, 0));
+                       snd_pcm_sgbuf_get_addr(dma->substream, 0));
                break;
        }
        //printk("vortex: cfg0 = 0x%x\nvortex: cfg1=0x%x\n", dma->cfg0, dma->cfg1);
                        //hwwrite(vortex->mmio, VORTEX_ADBDMA_BUFBASE+(((adbdma << 2)+pp) << 2), dma->table[p].addr);
                        hwwrite(vortex->mmio,
                                VORTEX_ADBDMA_BUFBASE + (((adbdma << 2) + pp) << 2),
-                               snd_sgbuf_get_addr(dma->sgbuf,
+                               snd_pcm_sgbuf_get_addr(dma->substream,
                                dma->period_bytes * p));
                        /* Force write thru cache. */
                        hwread(vortex->mmio, VORTEX_ADBDMA_BUFBASE +
                        if (pp >= 4)
                                pp -= 4;
                }
-               hwwrite(vortex->mmio, VORTEX_ADBDMA_BUFBASE+(((adbdma << 2)+pp) << 2), snd_sgbuf_get_addr(dma->sgbuf, dma->period_bytes * p));
+               hwwrite(vortex->mmio,
+                       VORTEX_ADBDMA_BUFBASE + (((adbdma << 2) + pp) << 2),
+                       snd_pcm_sgbuf_get_addr(dma->substream,
+                                              dma->period_bytes * p));
                /* Force write thru cache. */
                hwread(vortex->mmio, VORTEX_ADBDMA_BUFBASE + (((adbdma << 2)+pp) << 2));
        }
 
 static void
 vortex_wtdma_setbuffers(vortex_t * vortex, int wtdma,
-                       struct snd_sg_buf * sgbuf, int psize, int count)
+                       int psize, int count)
 {
        stream_t *dma = &vortex->dma_wt[wtdma];
 
        dma->period_bytes = psize;
        dma->nr_periods = count;
-       dma->sgbuf = sgbuf;
 
        dma->cfg0 = 0;
        dma->cfg1 = 0;
        case 4:
                dma->cfg1 |= 0x88000000 | 0x44000000 | 0x30000000 | (psize-1);
                hwwrite(vortex->mmio, VORTEX_WTDMA_BUFBASE + (wtdma << 4) + 0xc,
-                       snd_sgbuf_get_addr(sgbuf, psize * 3));
+                       snd_pcm_sgbuf_get_addr(dma->substream, psize * 3));
                /* 3 pages */
        case 3:
                dma->cfg0 |= 0x12000000;
                dma->cfg1 |= 0x80000000 | 0x40000000 | ((psize-1) << 0xc);
                hwwrite(vortex->mmio, VORTEX_WTDMA_BUFBASE + (wtdma << 4)  + 0x8,
-                       snd_sgbuf_get_addr(sgbuf, psize * 2));
+                       snd_pcm_sgbuf_get_addr(dma->substream, psize * 2));
                /* 2 pages */
        case 2:
                dma->cfg0 |= 0x88000000 | 0x44000000 | 0x10000000 | (psize-1);
                hwwrite(vortex->mmio, VORTEX_WTDMA_BUFBASE + (wtdma << 4) + 0x4,
-                       snd_sgbuf_get_addr(sgbuf, psize));
+                       snd_pcm_sgbuf_get_addr(dma->substream, psize));
                /* 1 page */
        case 1:
                dma->cfg0 |= 0x80000000 | 0x40000000 | ((psize-1) << 0xc);
                hwwrite(vortex->mmio, VORTEX_WTDMA_BUFBASE + (wtdma << 4),
-                       snd_sgbuf_get_addr(sgbuf, 0));
+                       snd_pcm_sgbuf_get_addr(dma->substream, 0));
                break;
        }
        hwwrite(vortex->mmio, VORTEX_WTDMA_BUFCFG0 + (wtdma << 3), dma->cfg0);
                        hwwrite(vortex->mmio,
                                VORTEX_WTDMA_BUFBASE +
                                (((wtdma << 2) + pp) << 2),
-                               snd_sgbuf_get_addr(dma->sgbuf, dma->period_bytes * p));
+                               snd_pcm_sgbuf_get_addr(dma->substream,
+                                                      dma->period_bytes * p));
                        /* Force write thru cache. */
                        hwread(vortex->mmio, VORTEX_WTDMA_BUFBASE +
                               (((wtdma << 2) + pp) << 2));
 
 {
        vortex_t *chip = snd_pcm_substream_chip(substream);
        stream_t *stream = (stream_t *) (substream->runtime->private_data);
-       struct snd_sg_buf *sgbuf;
        int err;
 
        // Alloc buffer memory.
                printk(KERN_ERR "Vortex: pcm page alloc failed!\n");
                return err;
        }
-       //sgbuf = (struct snd_sg_buf *) substream->runtime->dma_private;
-       sgbuf = snd_pcm_substream_sgbuf(substream);
        /*
           printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
           params_period_bytes(hw_params), params_channels(hw_params));
                stream = substream->runtime->private_data = &chip->dma_adb[dma];
                stream->substream = substream;
                /* Setup Buffers. */
-               vortex_adbdma_setbuffers(chip, dma, sgbuf,
+               vortex_adbdma_setbuffers(chip, dma,
                                         params_period_bytes(hw_params),
                                         params_periods(hw_params));
        }
                    &chip->dma_wt[substream->number];
                stream->dma = substream->number;
                stream->substream = substream;
-               vortex_wtdma_setbuffers(chip, substream->number, sgbuf,
+               vortex_wtdma_setbuffers(chip, substream->number,
                                        params_period_bytes(hw_params),
                                        params_periods(hw_params));
        }
        return (bytes_to_frames(substream->runtime, current_ptr));
 }
 
-/* Page callback. */
-/*
-static struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset) {
-       
-       
-}
-*/
 /* operators */
 static struct snd_pcm_ops snd_vortex_playback_ops = {
        .open = snd_vortex_pcm_open,
 
 static int snd_bt87x_create_risc(struct snd_bt87x *chip, struct snd_pcm_substream *substream,
                                 unsigned int periods, unsigned int period_bytes)
 {
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
        unsigned int i, offset;
        u32 *risc;
 
                rest = period_bytes;
                do {
                        u32 cmd, len;
+                       unsigned int addr;
 
                        len = PAGE_SIZE - (offset % PAGE_SIZE);
                        if (len > rest)
                        if (len == rest)
                                cmd |= RISC_EOL | RISC_IRQ;
                        *risc++ = cpu_to_le32(cmd);
-                       *risc++ = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, offset));
+                       addr = snd_pcm_sgbuf_get_addr(substream, offset);
+                       *risc++ = cpu_to_le32(addr);
                        offset += len;
                        rest -= len;
                } while (rest > 0);
 
 {
        struct echoaudio *chip;
        int err, per, rest, page, edge, offs;
-       struct snd_sg_buf *sgbuf;
        struct audiopipe *pipe;
 
        chip = snd_pcm_substream_chip(substream);
                return err;
        }
 
-       sgbuf = snd_pcm_substream_sgbuf(substream);
-
-       DE_HWP(("pcm_hw_params table size=%d pages=%d\n",
-               sgbuf->size, sgbuf->pages));
        sglist_init(chip, pipe);
        edge = PAGE_SIZE;
        for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
                if (offs + rest > params_buffer_bytes(hw_params))
                        rest = params_buffer_bytes(hw_params) - offs;
                while (rest) {
+                       dma_addr_t addr;
+                       addr = snd_pcm_sgbuf_get_addr(substream, offs);
                        if (rest <= edge - offs) {
-                               sglist_add_mapping(chip, pipe,
-                                                  snd_sgbuf_get_addr(sgbuf, offs),
-                                                  rest);
+                               sglist_add_mapping(chip, pipe, addr, rest);
                                sglist_add_irq(chip, pipe);
                                offs += rest;
                                rest = 0;
                        } else {
-                               sglist_add_mapping(chip, pipe,
-                                                  snd_sgbuf_get_addr(sgbuf, offs),
+                               sglist_add_mapping(chip, pipe, addr,
                                                   edge - offs);
                                rest -= edge - offs;
                                offs = edge;
 
 snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
        struct snd_util_memhdr *hdr;
        struct snd_emu10k1_memblk *blk;
        int page, err, idx;
         */
        idx = 0;
        for (page = blk->first_page; page <= blk->last_page; page++, idx++) {
+               unsigned long ofs = idx << PAGE_SHIFT;
                dma_addr_t addr;
-#ifdef CONFIG_SND_DEBUG
-               if (idx >= sgbuf->pages) {
-                       printk(KERN_ERR "emu: pages overflow! (%d-%d) for %d\n",
-                              blk->first_page, blk->last_page, sgbuf->pages);
-                       mutex_unlock(&hdr->block_mutex);
-                       return NULL;
-               }
-#endif
-               addr = sgbuf->table[idx].addr;
+               addr = snd_pcm_sgbuf_get_addr(substream, ofs);
                if (! is_valid_page(emu, addr)) {
                        printk(KERN_ERR "emu: failure page = %d\n", idx);
                        mutex_unlock(&hdr->block_mutex);
 
                      struct azx_dev *azx_dev, u32 **bdlp,
                      int ofs, int size, int with_ioc)
 {
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
        u32 *bdl = *bdlp;
 
        while (size > 0) {
                if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
                        return -EINVAL;
 
-               addr = snd_pcm_sgbuf_get_addr(sgbuf, ofs);
+               addr = snd_pcm_sgbuf_get_addr(substream, ofs);
                /* program the address field of the BDL entry */
                bdl[0] = cpu_to_le32((u32)addr);
                bdl[1] = cpu_to_le32(upper_32_bits(addr));
 
 {
        struct snd_riptide *chip = snd_pcm_substream_chip(substream);
        struct snd_pcm_runtime *runtime = substream->runtime;
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
        struct pcmhw *data = get_pcmhwdev(substream);
        struct cmdif *cif = chip->cif;
        unsigned char *lbuspath = NULL;
                        lbuspath = data->paths.stereo;
                break;
        }
-       snd_printdd("use sgdlist at 0x%p and buffer at 0x%p\n",
-                   data->sgdlist.area, sgbuf);
-       if (data->sgdlist.area && sgbuf) {
+       snd_printdd("use sgdlist at 0x%p\n",
+                   data->sgdlist.area);
+       if (data->sgdlist.area) {
                unsigned int i, j, size, pages, f, pt, period;
                struct sgd *c, *p = NULL;
 
                pt = 0;
                j = 0;
                for (i = 0; i < pages; i++) {
+                       unsigned int ofs, addr;
                        c = &data->sgdbuf[i];
                        if (p)
                                p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
                                                             sizeof(struct
                                                                    sgd)));
                        c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
-                       c->dwSegPtrPhys =
-                           cpu_to_le32(sgbuf->table[j].addr + pt);
+                       ofs = j << PAGE_SHIFT;
+                       addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
+                       c->dwSegPtrPhys = cpu_to_le32(addr);
                        pt = (pt + f) % PAGE_SIZE;
                        if (pt == 0)
                                j++;
 
 static int hdspm_update_simple_mixer_controls(struct hdspm * hdspm);
 static int hdspm_autosync_ref(struct hdspm * hdspm);
 static int snd_hdspm_set_defaults(struct hdspm * hdspm);
-static void hdspm_set_sgbuf(struct hdspm * hdspm, struct snd_sg_buf *sgbuf,
+static void hdspm_set_sgbuf(struct hdspm * hdspm,
+                           struct snd_pcm_substream *substream,
                             unsigned int reg, int channels);
 
 static inline int HDSPM_bit2freq(int n)
        int i;
        pid_t this_pid;
        pid_t other_pid;
-       struct snd_sg_buf *sgbuf;
-
 
        spin_lock_irq(&hdspm->lock);
 
        if (err < 0)
                return err;
 
-       sgbuf = snd_pcm_substream_sgbuf(substream);
-
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 
-               hdspm_set_sgbuf(hdspm, sgbuf, HDSPM_pageAddressBufferOut,
+               hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferOut,
                                params_channels(params));
 
                for (i = 0; i < params_channels(params); ++i)
                snd_printdd("Allocated sample buffer for playback at %p\n",
                                hdspm->playback_buffer);
        } else {
-               hdspm_set_sgbuf(hdspm, sgbuf, HDSPM_pageAddressBufferIn,
+               hdspm_set_sgbuf(hdspm, substream, HDSPM_pageAddressBufferIn,
                                params_channels(params));
 
                for (i = 0; i < params_channels(params); ++i)
           snd_printdd("Allocated sample buffer for %s at 0x%08X\n",
           substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
           "playback" : "capture",
-          snd_pcm_sgbuf_get_addr(sgbuf, 0));
+          snd_pcm_sgbuf_get_addr(substream, 0));
         */
        /*
        snd_printdd("set_hwparams: %s %d Hz, %d channels, bs = %d\n",
        return 0;
 }
 
-static void hdspm_set_sgbuf(struct hdspm * hdspm, struct snd_sg_buf *sgbuf,
+static void hdspm_set_sgbuf(struct hdspm * hdspm,
+                           struct snd_pcm_substream *substream,
                             unsigned int reg, int channels)
 {
        int i;
        for (i = 0; i < (channels * 16); i++)
                hdspm_write(hdspm, reg + 4 * i,
-                           snd_pcm_sgbuf_get_addr(sgbuf, (size_t) 4096 * i));
+                           snd_pcm_sgbuf_get_addr(substream, 4096 * i));
 }
 
 /* ------------- ALSA Devices ---------------------------- */
 
        struct snd_util_memblk *blk;
        struct snd_pcm_runtime *runtime = substream->runtime;
        int idx, page;
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
 
        if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
                       runtime->dma_bytes > SNDRV_TRIDENT_MAX_PAGES *
                mutex_unlock(&hdr->block_mutex);
                return NULL;
        }
-       if (lastpg(blk) - firstpg(blk) >= sgbuf->pages) {
-               snd_printk(KERN_ERR "page calculation doesn't match: allocated pages = %d, trident = %d/%d\n", sgbuf->pages, firstpg(blk), lastpg(blk));
-               __snd_util_mem_free(hdr, blk);
-               mutex_unlock(&hdr->block_mutex);
-               return NULL;
-       }
                           
        /* set TLB entries */
        idx = 0;
        for (page = firstpg(blk); page <= lastpg(blk); page++, idx++) {
-               dma_addr_t addr = sgbuf->table[idx].addr;
-               unsigned long ptr = (unsigned long)sgbuf->table[idx].buf;
+               unsigned long ofs = idx << PAGE_SHIFT;
+               dma_addr_t addr = snd_pcm_sgbuf_get_addr(substream, ofs);
+               unsigned long ptr = (unsigned long)
+                       snd_pcm_sgbuf_get_ptr(substream, ofs);
                if (! is_valid_page(addr)) {
                        __snd_util_mem_free(hdr, blk);
                        mutex_unlock(&hdr->block_mutex);
 
 {
        unsigned int i, idx, ofs, rest;
        struct via82xx *chip = snd_pcm_substream_chip(substream);
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
 
        if (dev->table.area == NULL) {
                /* the start of each lists must be aligned to 8 bytes,
                do {
                        unsigned int r;
                        unsigned int flag;
+                       unsigned int addr;
 
                        if (idx >= VIA_TABLE_SIZE) {
                                snd_printk(KERN_ERR "via82xx: too much table size!\n");
                                return -EINVAL;
                        }
-                       ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
+                       addr = snd_pcm_sgbuf_get_addr(substream, ofs);
+                       ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
                        r = PAGE_SIZE - (ofs % PAGE_SIZE);
                        if (rest < r)
                                r = rest;
 
 {
        unsigned int i, idx, ofs, rest;
        struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
-       struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
 
        if (dev->table.area == NULL) {
                /* the start of each lists must be aligned to 8 bytes,
                do {
                        unsigned int r;
                        unsigned int flag;
+                       unsigned int addr;
 
                        if (idx >= VIA_TABLE_SIZE) {
                                snd_printk(KERN_ERR "via82xx: too much table size!\n");
                                return -EINVAL;
                        }
-                       ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
+                       addr = snd_pcm_sgbuf_get_addr(substream, ofs);
+                       ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32(addr);
                        r = PAGE_SIZE - (ofs % PAGE_SIZE);
                        if (rest < r)
                                r = rest;