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
;
101 EXPORT_SYMBOL_GPL(comedi_isadma_disable_on_sample
);
104 * comedi_isadma_poll - poll the current DMA transfer
105 * @dma: the ISA DMA to poll
107 * Returns the position (in bytes) of the current DMA transfer.
109 unsigned int comedi_isadma_poll(struct comedi_isadma
*dma
)
111 struct comedi_isadma_desc
*desc
= &dma
->desc
[dma
->cur_dma
];
114 unsigned int result1
;
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
);
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.
126 result1
= get_dma_residue(desc
->chan
);
127 if (!isa_dma_bridge_buggy
)
128 enable_dma(desc
->chan
);
129 release_dma_lock(flags
);
131 if (result
< result1
)
133 if (result
>= desc
->size
|| result
== 0)
136 return desc
->size
- result
;
138 EXPORT_SYMBOL_GPL(comedi_isadma_poll
);
141 * comedi_isadma_set_mode - set the ISA DMA transfer direction
142 * @desc: the ISA DMA cookie to set
143 * @dma_dir: the DMA direction
145 void comedi_isadma_set_mode(struct comedi_isadma_desc
*desc
, char dma_dir
)
147 desc
->mode
= (dma_dir
== COMEDI_ISADMA_READ
) ? DMA_MODE_READ
150 EXPORT_SYMBOL_GPL(comedi_isadma_set_mode
);
153 * comedi_isadma_alloc - allocate and initialize the ISA DMA
154 * @dev: comedi_device struct
155 * @n_desc: the number of cookies to allocate
156 * @dma_chan: DMA channel for the first cookie
157 * @dma_chan2: DMA channel for the second cookie
158 * @maxsize: the size of the buffer to allocate for each cookie
159 * @dma_dir: the DMA direction
161 * Returns the allocated and initialized ISA DMA or NULL if anything fails.
163 struct comedi_isadma
*comedi_isadma_alloc(struct comedi_device
*dev
,
164 int n_desc
, unsigned int dma_chan1
,
165 unsigned int dma_chan2
,
166 unsigned int maxsize
, char dma_dir
)
168 struct comedi_isadma
*dma
= NULL
;
169 struct comedi_isadma_desc
*desc
;
170 unsigned int dma_chans
[2];
173 if (n_desc
< 1 || n_desc
> 2)
176 dma
= kzalloc(sizeof(*dma
), GFP_KERNEL
);
180 desc
= kcalloc(n_desc
, sizeof(*desc
), GFP_KERNEL
);
184 dma
->n_desc
= n_desc
;
186 dma_chans
[0] = dma_chan1
;
187 if (dma_chan2
== 0 || dma_chan2
== dma_chan1
)
188 dma_chans
[1] = dma_chan1
;
190 dma_chans
[1] = dma_chan2
;
192 if (request_dma(dma_chans
[0], dev
->board_name
))
194 dma
->chan
= dma_chans
[0];
195 if (dma_chans
[1] != dma_chans
[0]) {
196 if (request_dma(dma_chans
[1], dev
->board_name
))
199 dma
->chan2
= dma_chans
[1];
201 for (i
= 0; i
< n_desc
; i
++) {
202 desc
= &dma
->desc
[i
];
203 desc
->chan
= dma_chans
[i
];
204 desc
->maxsize
= maxsize
;
205 desc
->virt_addr
= dma_alloc_coherent(NULL
, desc
->maxsize
,
208 if (!desc
->virt_addr
)
210 comedi_isadma_set_mode(desc
, dma_dir
);
216 comedi_isadma_free(dma
);
219 EXPORT_SYMBOL_GPL(comedi_isadma_alloc
);
222 * comedi_isadma_free - free the ISA DMA
223 * @dma: the ISA DMA to free
225 void comedi_isadma_free(struct comedi_isadma
*dma
)
227 struct comedi_isadma_desc
*desc
;
234 for (i
= 0; i
< dma
->n_desc
; i
++) {
235 desc
= &dma
->desc
[i
];
237 dma_free_coherent(NULL
, desc
->maxsize
,
243 if (dma
->chan2
&& dma
->chan2
!= dma
->chan
)
244 free_dma(dma
->chan2
);
249 EXPORT_SYMBOL_GPL(comedi_isadma_free
);
251 static int __init
comedi_isadma_init(void)
255 module_init(comedi_isadma_init
);
257 static void __exit
comedi_isadma_exit(void)
260 module_exit(comedi_isadma_exit
);
262 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
263 MODULE_DESCRIPTION("Comedi ISA DMA support");
264 MODULE_LICENSE("GPL");