linux/drivers/staging/comedi/drivers/comedi_isadma.c
<<
>>
Prefs
   1/*
   2 * COMEDI ISA DMA support functions
   3 * Copyright (c) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2 of the License, or
   8 * (at your option) any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/module.h>
  17#include <linux/slab.h>
  18#include <linux/delay.h>
  19#include <linux/dma-mapping.h>
  20#include <asm/dma.h>
  21
  22#include "../comedidev.h"
  23
  24#include "comedi_isadma.h"
  25
  26/**
  27 * comedi_isadma_program - program and enable an ISA DMA transfer
  28 * @desc:       the ISA DMA cookie to program and enable
  29 */
  30void comedi_isadma_program(struct comedi_isadma_desc *desc)
  31{
  32        unsigned long flags;
  33
  34        flags = claim_dma_lock();
  35        clear_dma_ff(desc->chan);
  36        set_dma_mode(desc->chan, desc->mode);
  37        set_dma_addr(desc->chan, desc->hw_addr);
  38        set_dma_count(desc->chan, desc->size);
  39        enable_dma(desc->chan);
  40        release_dma_lock(flags);
  41}
  42EXPORT_SYMBOL_GPL(comedi_isadma_program);
  43
  44/**
  45 * comedi_isadma_disable - disable the ISA DMA channel
  46 * @dma_chan:   the DMA channel to disable
  47 *
  48 * Returns the residue (remaining bytes) left in the DMA transfer.
  49 */
  50unsigned int comedi_isadma_disable(unsigned int dma_chan)
  51{
  52        unsigned long flags;
  53        unsigned int residue;
  54
  55        flags = claim_dma_lock();
  56        disable_dma(dma_chan);
  57        residue = get_dma_residue(dma_chan);
  58        release_dma_lock(flags);
  59
  60        return residue;
  61}
  62EXPORT_SYMBOL_GPL(comedi_isadma_disable);
  63
  64/**
  65 * comedi_isadma_disable_on_sample - disable the ISA DMA channel
  66 * @dma_chan:   the DMA channel to disable
  67 * @size:       the sample size (in bytes)
  68 *
  69 * Returns the residue (remaining bytes) left in the DMA transfer.
  70 */
  71unsigned int comedi_isadma_disable_on_sample(unsigned int dma_chan,
  72                                             unsigned int size)
  73{
  74        int stalled = 0;
  75        unsigned long flags;
  76        unsigned int residue;
  77        unsigned int new_residue;
  78
  79        residue = comedi_isadma_disable(dma_chan);
  80        while (residue % size) {
  81                /* residue is a partial sample, enable DMA to allow more data */
  82                flags = claim_dma_lock();
  83                enable_dma(dma_chan);
  84                release_dma_lock(flags);
  85
  86                udelay(2);
  87                new_residue = comedi_isadma_disable(dma_chan);
  88
  89                /* is DMA stalled? */
  90                if (new_residue == residue) {
  91                        stalled++;
  92                        if (stalled > 10)
  93                                break;
  94                } else {
  95                        residue = new_residue;
  96                        stalled = 0;
  97                }
  98        }
  99        return residue;
 100}
 101EXPORT_SYMBOL_GPL(comedi_isadma_disable_on_sample);
 102
 103/**
 104 * comedi_isadma_poll - poll the current DMA transfer
 105 * @dma:        the ISA DMA to poll
 106 *
 107 * Returns the position (in bytes) of the current DMA transfer.
 108 */
 109unsigned int comedi_isadma_poll(struct comedi_isadma *dma)
 110{
 111        struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
 112        unsigned long flags;
 113        unsigned int result;
 114        unsigned int result1;
 115
 116        flags = claim_dma_lock();
 117        clear_dma_ff(desc->chan);
 118        if (!isa_dma_bridge_buggy)
 119                disable_dma(desc->chan);
 120        result = get_dma_residue(desc->chan);
 121        /*
 122         * Read the counter again and choose higher value in order to
 123         * avoid reading during counter lower byte roll over if the
 124         * isa_dma_bridge_buggy is set.
 125         */
 126        result1 = get_dma_residue(desc->chan);
 127        if (!isa_dma_bridge_buggy)
 128                enable_dma(desc->chan);
 129        release_dma_lock(flags);
 130
 131        if (result < result1)
 132                result = result1;
 133        if (result >= desc->size || result == 0)
 134                return 0;
 135        return desc->size - result;
 136}
 137EXPORT_SYMBOL_GPL(comedi_isadma_poll);
 138
 139/**
 140 * comedi_isadma_set_mode - set the ISA DMA transfer direction
 141 * @desc:       the ISA DMA cookie to set
 142 * @dma_dir:    the DMA direction
 143 */
 144void comedi_isadma_set_mode(struct comedi_isadma_desc *desc, char dma_dir)
 145{
 146        desc->mode = (dma_dir == COMEDI_ISADMA_READ) ? DMA_MODE_READ
 147                                                     : DMA_MODE_WRITE;
 148}
 149EXPORT_SYMBOL_GPL(comedi_isadma_set_mode);
 150
 151/**
 152 * comedi_isadma_alloc - allocate and initialize the ISA DMA
 153 * @dev:        comedi_device struct
 154 * @n_desc:     the number of cookies to allocate
 155 * @dma_chan:   DMA channel for the first cookie
 156 * @dma_chan2:  DMA channel for the second cookie
 157 * @maxsize:    the size of the buffer to allocate for each cookie
 158 * @dma_dir:    the DMA direction
 159 *
 160 * Returns the allocated and initialized ISA DMA or NULL if anything fails.
 161 */
 162struct comedi_isadma *comedi_isadma_alloc(struct comedi_device *dev,
 163                                          int n_desc, unsigned int dma_chan1,
 164                                          unsigned int dma_chan2,
 165                                          unsigned int maxsize, char dma_dir)
 166{
 167        struct comedi_isadma *dma = NULL;
 168        struct comedi_isadma_desc *desc;
 169        unsigned int dma_chans[2];
 170        int i;
 171
 172        if (n_desc < 1 || n_desc > 2)
 173                goto no_dma;
 174
 175        dma = kzalloc(sizeof(*dma), GFP_KERNEL);
 176        if (!dma)
 177                goto no_dma;
 178
 179        desc = kcalloc(n_desc, sizeof(*desc), GFP_KERNEL);
 180        if (!desc)
 181                goto no_dma;
 182        dma->desc = desc;
 183        dma->n_desc = n_desc;
 184
 185        dma_chans[0] = dma_chan1;
 186        if (dma_chan2 == 0 || dma_chan2 == dma_chan1)
 187                dma_chans[1] = dma_chan1;
 188        else
 189                dma_chans[1] = dma_chan2;
 190
 191        if (request_dma(dma_chans[0], dev->board_name))
 192                goto no_dma;
 193        dma->chan = dma_chans[0];
 194        if (dma_chans[1] != dma_chans[0]) {
 195                if (request_dma(dma_chans[1], dev->board_name))
 196                        goto no_dma;
 197        }
 198        dma->chan2 = dma_chans[1];
 199
 200        for (i = 0; i < n_desc; i++) {
 201                desc = &dma->desc[i];
 202                desc->chan = dma_chans[i];
 203                desc->maxsize = maxsize;
 204                desc->virt_addr = dma_alloc_coherent(NULL, desc->maxsize,
 205                                                     &desc->hw_addr,
 206                                                     GFP_KERNEL);
 207                if (!desc->virt_addr)
 208                        goto no_dma;
 209                comedi_isadma_set_mode(desc, dma_dir);
 210        }
 211
 212        return dma;
 213
 214no_dma:
 215        comedi_isadma_free(dma);
 216        return NULL;
 217}
 218EXPORT_SYMBOL_GPL(comedi_isadma_alloc);
 219
 220/**
 221 * comedi_isadma_free - free the ISA DMA
 222 * @dma:        the ISA DMA to free
 223 */
 224void comedi_isadma_free(struct comedi_isadma *dma)
 225{
 226        struct comedi_isadma_desc *desc;
 227        int i;
 228
 229        if (!dma)
 230                return;
 231
 232        if (dma->desc) {
 233                for (i = 0; i < dma->n_desc; i++) {
 234                        desc = &dma->desc[i];
 235                        if (desc->virt_addr)
 236                                dma_free_coherent(NULL, desc->maxsize,
 237                                                  desc->virt_addr,
 238                                                  desc->hw_addr);
 239                }
 240                kfree(dma->desc);
 241        }
 242        if (dma->chan2 && dma->chan2 != dma->chan)
 243                free_dma(dma->chan2);
 244        if (dma->chan)
 245                free_dma(dma->chan);
 246        kfree(dma);
 247}
 248EXPORT_SYMBOL_GPL(comedi_isadma_free);
 249
 250static int __init comedi_isadma_init(void)
 251{
 252        return 0;
 253}
 254module_init(comedi_isadma_init);
 255
 256static void __exit comedi_isadma_exit(void)
 257{
 258}
 259module_exit(comedi_isadma_exit);
 260
 261MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
 262MODULE_DESCRIPTION("Comedi ISA DMA support");
 263MODULE_LICENSE("GPL");
 264