Linux 4.1.16
[linux/fpc-iii.git] / drivers / misc / cb710 / sgbuf2.c
blob2a40d0efdff5d33115ac61a0fc97287638ae18ca
1 /*
2 * cb710/sgbuf2.c
4 * Copyright by Michał Mirosław, 2008-2009
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/cb710.h>
14 static bool sg_dwiter_next(struct sg_mapping_iter *miter)
16 if (sg_miter_next(miter)) {
17 miter->consumed = 0;
18 return true;
19 } else
20 return false;
23 static bool sg_dwiter_is_at_end(struct sg_mapping_iter *miter)
25 return miter->length == miter->consumed && !sg_dwiter_next(miter);
28 static uint32_t sg_dwiter_read_buffer(struct sg_mapping_iter *miter)
30 size_t len, left = 4;
31 uint32_t data;
32 void *addr = &data;
34 do {
35 len = min(miter->length - miter->consumed, left);
36 memcpy(addr, miter->addr + miter->consumed, len);
37 miter->consumed += len;
38 left -= len;
39 if (!left)
40 return data;
41 addr += len;
42 } while (sg_dwiter_next(miter));
44 memset(addr, 0, left);
45 return data;
48 static inline bool needs_unaligned_copy(const void *ptr)
50 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
51 return false;
52 #else
53 return ((ptr - NULL) & 3) != 0;
54 #endif
57 static bool sg_dwiter_get_next_block(struct sg_mapping_iter *miter, uint32_t **ptr)
59 size_t len;
61 if (sg_dwiter_is_at_end(miter))
62 return true;
64 len = miter->length - miter->consumed;
66 if (likely(len >= 4 && !needs_unaligned_copy(
67 miter->addr + miter->consumed))) {
68 *ptr = miter->addr + miter->consumed;
69 miter->consumed += 4;
70 return true;
73 return false;
76 /**
77 * cb710_sg_dwiter_read_next_block() - get next 32-bit word from sg buffer
78 * @miter: sg mapping iterator used for reading
80 * Description:
81 * Returns 32-bit word starting at byte pointed to by @miter@
82 * handling any alignment issues. Bytes past the buffer's end
83 * are not accessed (read) but are returned as zeroes. @miter@
84 * is advanced by 4 bytes or to the end of buffer whichever is
85 * closer.
87 * Context:
88 * Same requirements as in sg_miter_next().
90 * Returns:
91 * 32-bit word just read.
93 uint32_t cb710_sg_dwiter_read_next_block(struct sg_mapping_iter *miter)
95 uint32_t *ptr = NULL;
97 if (likely(sg_dwiter_get_next_block(miter, &ptr)))
98 return ptr ? *ptr : 0;
100 return sg_dwiter_read_buffer(miter);
102 EXPORT_SYMBOL_GPL(cb710_sg_dwiter_read_next_block);
104 static void sg_dwiter_write_slow(struct sg_mapping_iter *miter, uint32_t data)
106 size_t len, left = 4;
107 void *addr = &data;
109 do {
110 len = min(miter->length - miter->consumed, left);
111 memcpy(miter->addr, addr, len);
112 miter->consumed += len;
113 left -= len;
114 if (!left)
115 return;
116 addr += len;
117 } while (sg_dwiter_next(miter));
121 * cb710_sg_dwiter_write_next_block() - write next 32-bit word to sg buffer
122 * @miter: sg mapping iterator used for writing
124 * Description:
125 * Writes 32-bit word starting at byte pointed to by @miter@
126 * handling any alignment issues. Bytes which would be written
127 * past the buffer's end are silently discarded. @miter@ is
128 * advanced by 4 bytes or to the end of buffer whichever is closer.
130 * Context:
131 * Same requirements as in sg_miter_next().
133 void cb710_sg_dwiter_write_next_block(struct sg_mapping_iter *miter, uint32_t data)
135 uint32_t *ptr = NULL;
137 if (likely(sg_dwiter_get_next_block(miter, &ptr))) {
138 if (ptr)
139 *ptr = data;
140 else
141 return;
142 } else
143 sg_dwiter_write_slow(miter, data);
145 EXPORT_SYMBOL_GPL(cb710_sg_dwiter_write_next_block);