]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IOHIDSystem/IOHIDDescriptorParser/HIDGetData.c
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
25 Contains: xxx put contents here xxx
27 Version: xxx put version here xxx
29 Copyright: © 1999-2000 by Apple Computer, Inc., all rights reserved.
33 DRI: xxx put dri here xxx
35 Other Contact: xxx put other contact here xxx
37 Technology: xxx put technology here xxx
44 Change History (most recent first):
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
54 *------------------------------------------------------------------------------
56 * HIDGetData - Get a single data item from a report
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?
68 * kHidP_Success - Success
69 * kHidP_NullPointer - Argument, Pointer was Null
71 *------------------------------------------------------------------------------
73 OSStatus
HIDGetData(void * report
, UInt32 iReportLength
,
74 UInt32 iStart
, UInt32 iSize
, SInt32
*piValue
,
77 Byte
* psReport
= (Byte
*)report
;
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
88 // Check the parameters
89 if ((iSize
== 0) || (iLastByte
>= iReportLength
) || (iLastByte
< iStartByte
))
90 return kHIDBadParameterErr
;
92 // Pick up the data bytes backwards
94 for (iCurrentByte
= iLastByte
; iCurrentByte
>= (int) iStartByte
; iCurrentByte
--)
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:
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);
110 data
|= (unsigned) psReport
[iCurrentByte
] & iMask
;
113 // Shift to the right to byte align the least significant bit
116 // Sign extend the report item
121 iSignBit
<<= (iSize
-1);
122 iExtendMask
= (iSignBit
<< 1) - 1;
123 if ((data
& iSignBit
)==0)
126 data
|= ~iExtendMask
;
130 *piValue
= (SInt32
) data
;