Skip to content

Commit 645576d

Browse files
svenpeter42marcan
authored andcommitted
nvmem: allow bit offset > 8
Signed-off-by: Sven Peter <[email protected]>
1 parent 6995e2d commit 645576d

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
@@ -1526,15 +1526,23 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
15261526
static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
15271527
{
15281528
u8 *p, *b;
1529-
int i, extra, bit_offset = cell->bit_offset;
1529+
int i, padding, extra, bit_offset = cell->bit_offset;
1530+
int bytes = cell->bytes;
15301531

15311532
p = b = buf;
15321533
if (bit_offset) {
1534+
padding = bit_offset/8;
1535+
if (padding) {
1536+
memmove(buf, buf + padding, bytes - padding);
1537+
bit_offset -= BITS_PER_BYTE * padding;
1538+
bytes -= padding;
1539+
}
1540+
15331541
/* First shift */
15341542
*b++ >>= bit_offset;
15351543

15361544
/* setup rest of the bytes if any */
1537-
for (i = 1; i < cell->bytes; i++) {
1545+
for (i = 1; i < bytes; i++) {
15381546
/* Get bits from next byte and shift them towards msb */
15391547
*p |= *b << (BITS_PER_BYTE - bit_offset);
15401548

@@ -1547,7 +1555,7 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
15471555
}
15481556

15491557
/* result fits in less bytes */
1550-
extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1558+
extra = bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
15511559
while (--extra >= 0)
15521560
*p-- = 0;
15531561

0 commit comments

Comments
 (0)