Deleted Added
full compact
data.c (113273) data.c (205728)
1/* $NetBSD: data.c,v 1.8 2000/04/02 11:10:53 augustss Exp $ */
2
3/*
4 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 13 unchanged lines hidden (view full) ---

22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
1/* $NetBSD: data.c,v 1.8 2000/04/02 11:10:53 augustss Exp $ */
2
3/*
4 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 13 unchanged lines hidden (view full) ---

22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/lib/libusbhid/data.c 113273 2003-04-09 01:52:49Z mdodd $");
30__FBSDID("$FreeBSD: head/lib/libusbhid/data.c 205728 2010-03-27 08:00:16Z kaiw $");
31
31
32#include <sys/param.h>
32#include <assert.h>
33#include <stdlib.h>
34#include "usbhid.h"
35
36int
37hid_get_data(const void *p, const hid_item_t *h)
38{
33#include <assert.h>
34#include <stdlib.h>
35#include "usbhid.h"
36
37int
38hid_get_data(const void *p, const hid_item_t *h)
39{
39 const unsigned char *buf;
40 unsigned int hpos;
41 unsigned int hsize;
42 int data;
40 const uint8_t *buf;
41 uint32_t hpos;
42 uint32_t hsize;
43 uint32_t data;
43 int i, end, offs;
44
45 buf = p;
44 int i, end, offs;
45
46 buf = p;
47
48 /* Skip report ID byte. */
49 if (h->report_ID > 0)
50 buf++;
51
46 hpos = h->pos; /* bit position of data */
47 hsize = h->report_size; /* bit length of data */
48
52 hpos = h->pos; /* bit position of data */
53 hsize = h->report_size; /* bit length of data */
54
55 /* Range check and limit */
49 if (hsize == 0)
50 return (0);
56 if (hsize == 0)
57 return (0);
58 if (hsize > 32)
59 hsize = 32;
60
51 offs = hpos / 8;
52 end = (hpos + hsize) / 8 - offs;
53 data = 0;
54 for (i = 0; i <= end; i++)
55 data |= buf[offs + i] << (i*8);
56 data >>= hpos % 8;
57 data &= (1 << hsize) - 1;
58 if (h->logical_minimum < 0) {
59 /* Need to sign extend */
60 hsize = sizeof data * 8 - hsize;
61 data = (data << hsize) >> hsize;
62 }
63 return (data);
64}
65
66void
67hid_set_data(void *p, const hid_item_t *h, int data)
68{
61 offs = hpos / 8;
62 end = (hpos + hsize) / 8 - offs;
63 data = 0;
64 for (i = 0; i <= end; i++)
65 data |= buf[offs + i] << (i*8);
66 data >>= hpos % 8;
67 data &= (1 << hsize) - 1;
68 if (h->logical_minimum < 0) {
69 /* Need to sign extend */
70 hsize = sizeof data * 8 - hsize;
71 data = (data << hsize) >> hsize;
72 }
73 return (data);
74}
75
76void
77hid_set_data(void *p, const hid_item_t *h, int data)
78{
69 unsigned char *buf;
70 unsigned int hpos;
71 unsigned int hsize;
79 uint8_t *buf;
80 uint32_t hpos;
81 uint32_t hsize;
72 int i, end, offs, mask;
73
74 buf = p;
82 int i, end, offs, mask;
83
84 buf = p;
85
86 /* Set report ID byte. */
87 if (h->report_ID > 0)
88 *buf++ = h->report_ID & 0xff;
89
75 hpos = h->pos; /* bit position of data */
76 hsize = h->report_size; /* bit length of data */
77
78 if (hsize != 32) {
79 mask = (1 << hsize) - 1;
80 data &= mask;
81 } else
82 mask = ~0;
83
84 data <<= (hpos % 8);
85 mask <<= (hpos % 8);
86 mask = ~mask;
87
88 offs = hpos / 8;
89 end = (hpos + hsize) / 8 - offs;
90
91 for (i = 0; i <= end; i++)
92 buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) |
90 hpos = h->pos; /* bit position of data */
91 hsize = h->report_size; /* bit length of data */
92
93 if (hsize != 32) {
94 mask = (1 << hsize) - 1;
95 data &= mask;
96 } else
97 mask = ~0;
98
99 data <<= (hpos % 8);
100 mask <<= (hpos % 8);
101 mask = ~mask;
102
103 offs = hpos / 8;
104 end = (hpos + hsize) / 8 - offs;
105
106 for (i = 0; i <= end; i++)
107 buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) |
93 ((data >> (i*8)) & 0xff);
108 ((data >> (i*8)) & 0xff);
94}
109}