]> git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IOHIDSystem/IOHIDDescriptorParser/HIDGetData.c
xnu-123.5.tar.gz
[apple/xnu.git] / iokit / Families / IOHIDSystem / IOHIDDescriptorParser / HIDGetData.c
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 File: HIDGetData.c
24
25 Contains: xxx put contents here xxx
26
27 Version: xxx put version here xxx
28
29 Copyright: © 1999-2000 by Apple Computer, Inc., all rights reserved.
30
31 File Ownership:
32
33 DRI: xxx put dri here xxx
34
35 Other Contact: xxx put other contact here xxx
36
37 Technology: xxx put technology here xxx
38
39 Writers:
40
41 (KH) Keithen Hayenga
42 (BWS) Brent Schorsch
43
44 Change History (most recent first):
45
46 <USB3> 12/12/00 KH Correct cast of void *
47 <USB2> 3/5/99 BWS [2311353] HIDGetData not masking properly, so not work at all
48 <USB1> 3/5/99 BWS first checked in
49 */
50
51 #include "HIDLib.h"
52
53 /*
54 *------------------------------------------------------------------------------
55 *
56 * HIDGetData - Get a single data item from a report
57 *
58 * Input:
59 * psReport - The report
60 * iReportLength - The length of the report
61 * iStart - Start Bit in report
62 * iSize - Number of Bits
63 * piValue - The place to write the data
64 * bSignExtend - Sign extend?
65 * Output:
66 * piValue - The data
67 * Returns:
68 * kHidP_Success - Success
69 * kHidP_NullPointer - Argument, Pointer was Null
70 *
71 *------------------------------------------------------------------------------
72 */
73 OSStatus HIDGetData(void * report, UInt32 iReportLength,
74 UInt32 iStart, UInt32 iSize, SInt32 *piValue,
75 Boolean bSignExtend)
76 {
77 Byte * psReport = (Byte *)report;
78 unsigned data;
79 unsigned iSignBit;
80 unsigned iExtendMask;
81 unsigned iStartByte = iStart/8;
82 unsigned startBit = iStart&7;
83 unsigned iLastBit = iStart + iSize - 1;
84 unsigned iLastByte = iLastBit/8;
85 int iCurrentByte; // needs to be signed, we terminate loop on -1
86 unsigned iMask;
87
88 // Check the parameters
89 if ((iSize == 0) || (iLastByte >= iReportLength) || (iLastByte < iStartByte))
90 return kHIDBadParameterErr;
91
92 // Pick up the data bytes backwards
93 data = 0;
94 for (iCurrentByte = iLastByte; iCurrentByte >= (int) iStartByte; iCurrentByte--)
95 {
96 data <<= 8;
97
98 iMask = 0xff; // 1111 1111 initial mask
99 // if this is the 'last byte', then we need to mask off the top part of the byte
100 // to find the mask, we: find the position in this byte (lastBit % 8)
101 // then shift one to the left that many times plus one (to get one bit further)
102 // then subtract 1 to get all ones starting from the lastBit to the least signif bit
103 // ex: if iLastBit is 9, or iLastBit is 15, then we get:
104 // 1 7 (x % 8)
105 // 0000 0100 1 0000 0000 (1 << (x + 1))
106 // 0000 0011 0 1111 1111 (x - 1)
107 if (iCurrentByte == iLastByte)
108 iMask = ((1 << (((unsigned) iLastBit % 8) + 1)) - 1);
109
110 data |= (unsigned) psReport[iCurrentByte] & iMask;
111 }
112
113 // Shift to the right to byte align the least significant bit
114 data >>= startBit;
115
116 // Sign extend the report item
117 if (bSignExtend)
118 {
119 iSignBit = 1;
120 if (iSize > 1)
121 iSignBit <<= (iSize-1);
122 iExtendMask = (iSignBit << 1) - 1;
123 if ((data & iSignBit)==0)
124 data &= iExtendMask;
125 else
126 data |= ~iExtendMask;
127 }
128
129 // Return the value
130 *piValue = (SInt32) data;
131
132 return kHIDSuccess;
133 }