Skip to content

Commit 0a7718f

Browse files
svenpeter42jannau
authored andcommitted
nvmem: allow bit offset > 8
Signed-off-by: Sven Peter <sven@svenpeter.dev>
1 parent 5e1d90b commit 0a7718f

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
@@ -1622,15 +1622,23 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
16221622
static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
16231623
{
16241624
u8 *p, *b;
1625-
int i, extra, bit_offset = cell->bit_offset;
1625+
int i, padding, extra, bit_offset = cell->bit_offset;
1626+
int bytes = cell->bytes;
16261627

16271628
p = b = buf;
16281629
if (bit_offset) {
1630+
padding = bit_offset/8;
1631+
if (padding) {
1632+
memmove(buf, buf + padding, bytes - padding);
1633+
bit_offset -= BITS_PER_BYTE * padding;
1634+
bytes -= padding;
1635+
}
1636+
16291637
/* First shift */
16301638
*b++ >>= bit_offset;
16311639

16321640
/* setup rest of the bytes if any */
1633-
for (i = 1; i < cell->bytes; i++) {
1641+
for (i = 1; i < bytes; i++) {
16341642
/* Get bits from next byte and shift them towards msb */
16351643
*p |= *b << (BITS_PER_BYTE - bit_offset);
16361644

@@ -1643,7 +1651,7 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
16431651
}
16441652

16451653
/* result fits in less bytes */
1646-
extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1654+
extra = bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
16471655
while (--extra >= 0)
16481656
*p-- = 0;
16491657

0 commit comments

Comments
 (0)