Skip to content

Commit 5356b17

Browse files
svenpeter42marcan
authored andcommitted
nvmem: allow bit offset > 8
Signed-off-by: Sven Peter <[email protected]>
1 parent ffc2532 commit 5356b17

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

drivers/nvmem/core.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,15 +1555,23 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
15551555
static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
15561556
{
15571557
u8 *p, *b;
1558-
int i, extra, bit_offset = cell->bit_offset;
1558+
int i, padding, extra, bit_offset = cell->bit_offset;
1559+
int bytes = cell->bytes;
15591560

15601561
p = b = buf;
15611562
if (bit_offset) {
1563+
padding = bit_offset/8;
1564+
if (padding) {
1565+
memmove(buf, buf + padding, bytes - padding);
1566+
bit_offset -= BITS_PER_BYTE * padding;
1567+
bytes -= padding;
1568+
}
1569+
15621570
/* First shift */
15631571
*b++ >>= bit_offset;
15641572

15651573
/* setup rest of the bytes if any */
1566-
for (i = 1; i < cell->bytes; i++) {
1574+
for (i = 1; i < bytes; i++) {
15671575
/* Get bits from next byte and shift them towards msb */
15681576
*p |= *b << (BITS_PER_BYTE - bit_offset);
15691577

@@ -1576,7 +1584,7 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
15761584
}
15771585

15781586
/* result fits in less bytes */
1579-
extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1587+
extra = bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
15801588
while (--extra >= 0)
15811589
*p-- = 0;
15821590

0 commit comments

Comments
 (0)