2 * COMEDI ISA DMA support functions
3 * Copyright (c) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
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.
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.
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/dma-mapping.h>
22 #include "../comedidev.h"
24 #include "comedi_isadma.h"
27 * comedi_isadma_program - program and enable an ISA DMA transfer
28 * @desc: the ISA DMA cookie to program and enable
30 void comedi_isadma_program(struct comedi_isadma_desc
*desc
)
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
);
42 EXPORT_SYMBOL_GPL(comedi_isadma_program
);
45 * comedi_isadma_disable - disable the ISA DMA channel
46 * @dma_chan: the DMA channel to disable
48 * Returns the residue (remaining bytes) left in the DMA transfer.
50 unsigned int comedi_isadma_disable(unsigned int dma_chan
)
55 flags
= claim_dma_lock();
56 disable_dma(dma_chan
);
57 residue
= get_dma_residue(dma_chan
);
58 release_dma_lock(flags
);
62 EXPORT_SYMBOL_GPL(comedi_isadma_disable
);
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)
69 * Returns the residue (remaining bytes) left in the DMA transfer.
71 unsigned int comedi_isadma_disable_on_sample(unsigned int dma_chan
,
77 unsigned int new_residue
;
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();
84 release_dma_lock(flags
);
87 new_residue
= comedi_isadma_disable(dma_chan
);
90 if (new_residue
== residue
) {
95 residue
= new_residue
;
100 EXPORT_SYMBOL_GPL(comedi_isadma_disable_on_sample
);
103 * comedi_isadma_poll - poll the current DMA transfer
104 * @dma: the ISA DMA to poll
106 * Returns the position (in bytes) of the current DMA transfer.
108 unsigned int comedi_isadma_poll(struct comedi_isadma
*dma
)
110 struct comedi_isadma_desc
*desc
= &dma
->desc
[dma
->cur_dma
];
113 unsigned int result1
;
115 flags
= claim_dma_lock();
116 clear_dma_ff(desc
->chan
);
117 if (!isa_dma_bridge_buggy
)
118 disable_dma(desc
->chan
);
119 result
= get_dma_residue(desc
->chan
);
121 * Read the counter again and choose higher value in order to
122 * avoid reading during counter lower byte roll over if the
123 * isa_dma_bridge_buggy is set.
125 result1
= get_dma_residue(desc
->chan
);
126 if (!isa_dma_bridge_buggy
)
127 enable_dma(desc
->chan
);
128 release_dma_lock(flags
);
130 if (result
< result1
)
132 if (result
>= desc
->size
|| result
== 0)
135 return desc
->size
- result
;
137 EXPORT_SYMBOL_GPL(comedi_isadma_poll
);
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
144 void comedi_isadma_set_mode(struct comedi_isadma_desc
*desc
, char dma_dir
)
146 desc
->mode
= (dma_dir
== COMEDI_ISADMA_READ
) ? DMA_MODE_READ
149 EXPORT_SYMBOL_GPL(comedi_isadma_set_mode
);
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
160 * Returns the allocated and initialized ISA DMA or NULL if anything fails.
162 struct 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
)
167 struct comedi_isadma
*dma
= NULL
;
168 struct comedi_isadma_desc
*desc
;
169 unsigned int dma_chans
[2];
172 if (n_desc
< 1 || n_desc
> 2)
175 dma
= kzalloc(sizeof(*dma
), GFP_KERNEL
);
179 desc
= kcalloc(n_desc
, sizeof(*desc
), GFP_KERNEL
);
183 dma
->n_desc
= n_desc
;
185 dma_chans
[0] = dma_chan1
;
186 if (dma_chan2
== 0 || dma_chan2
== dma_chan1
)
187 dma_chans
[1] = dma_chan1
;
189 dma_chans
[1] = dma_chan2
;
191 if (request_dma(dma_chans
[0], dev
->board_name
))
193 dma
->chan
= dma_chans
[0];
194 if (dma_chans
[1] != dma_chans
[0]) {
195 if (request_dma(dma_chans
[1], dev
->board_name
))
198 dma
->chan2
= dma_chans
[1];
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
,
207 if (!desc
->virt_addr
)
209 comedi_isadma_set_mode(desc
, dma_dir
);
215 comedi_isadma_free(dma
);
218 EXPORT_SYMBOL_GPL(comedi_isadma_alloc
);
221 * comedi_isadma_free - free the ISA DMA
222 * @dma: the ISA DMA to free
224 void comedi_isadma_free(struct comedi_isadma
*dma
)
226 struct comedi_isadma_desc
*desc
;
233 for (i
= 0; i
< dma
->n_desc
; i
++) {
234 desc
= &dma
->desc
[i
];
236 dma_free_coherent(NULL
, desc
->maxsize
,
237 desc
->virt_addr
, desc
->hw_addr
);
241 if (dma
->chan2
&& dma
->chan2
!= dma
->chan
)
242 free_dma(dma
->chan2
);
247 EXPORT_SYMBOL_GPL(comedi_isadma_free
);
249 static int __init
comedi_isadma_init(void)
253 module_init(comedi_isadma_init
);
255 static void __exit
comedi_isadma_exit(void)
258 module_exit(comedi_isadma_exit
);
260 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
261 MODULE_DESCRIPTION("Comedi ISA DMA support");
262 MODULE_LICENSE("GPL");