-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdma_sg.c
More file actions
77 lines (62 loc) · 1.45 KB
/
dma_sg.c
File metadata and controls
77 lines (62 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (C) 2016 University of Granada
* Miguel Jimenez Lopez <klyone@ugr.es>
*
* DMA Scatter-Gather functions (implementation).
*
* This source code is licensed under the GNU General Public License,
* Version 2. See the file COPYING for more details.
*/
#include <linux/slab.h>
#include <linux/mm.h>
#include <asm/page.h>
#include "dma_sg.h"
struct dma_sg * dma_sg_offset_create(struct dma_block * block, \
size_t offset, gfp_t gfp)
{
struct dma_sg * sg = NULL;
sg = kzalloc(sizeof(*sg),gfp);
if(sg != NULL) {
sg->block = block;
sg->offset = offset;
}
return sg;
}
struct dma_sg * dma_sg_create(struct dma_block * block, \
gfp_t gfp)
{
return dma_sg_offset_create(block,0,gfp);
}
/* This function is inspired by zio_calculate_nents (dma.c) of
* the ZIO project (http://www.ohwr.org/projects/zio).
*/
int dma_sg_get_pages(struct dma_sg * sg)
{
void * bufp;
int bytesleft;
int mapbytes;
int nents = 0;
struct dma_block * blk;
if(sg == NULL)
return -1;
blk = sg->block;
bufp = dma_block_get_buffer(blk)+sg->offset;
bytesleft = dma_block_get_size(blk)-sg->offset;
while(bytesleft) {
nents++;
if(bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
mapbytes = bytesleft;
else
mapbytes = PAGE_SIZE - offset_in_page(bufp);
bufp += mapbytes;
bytesleft -= mapbytes;
}
return nents;
}
void dma_sg_free(struct dma_sg * sg)
{
if(sg != NULL) {
/* FIXME: Maybe, Free the block as well? */
kfree(sg);
}
}