• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_USB_INL_H__
18 #define ANDROID_USB_INL_H__
19 /** \file
20   This file consists of inline routines for the driver.
21 */
22 
23 /// Gets control code out of the entire IOCTL code packet
GetCtlCode(ULONG ioctl_code)24 __forceinline ULONG GetCtlCode(ULONG ioctl_code) {
25   return (ioctl_code >> 2) & 0x0FFF;
26 }
27 
28 /** \brief
29   Converts string length from number of wide characters into number of bytes.
30 */
ByteLen(USHORT wchar_len)31 __forceinline USHORT ByteLen(USHORT wchar_len) {
32   return static_cast<USHORT>(wchar_len * sizeof(WCHAR));
33 }
34 
35 /** \brief Gets byte length of a zero-terminated string not including
36   zero terminator. Must be called at low IRQL.
37 */
ByteLen(const WCHAR * str)38 __forceinline USHORT ByteLen(const WCHAR* str) {
39   ASSERT_IRQL_LOW();
40   return (NULL != str) ? ByteLen(static_cast<USHORT>(wcslen(str))) : 0;
41 }
42 
43 /** \brief
44   Converts string length from number of bytes into number of wide characters.
45   Can be called at any IRQL.
46 */
WcharLen(USHORT byte_len)47 __forceinline USHORT WcharLen(USHORT byte_len) {
48   return byte_len / sizeof(WCHAR);
49 }
50 
51 /** \brief Retrieves pointer out of the WDFMEMORY handle
52 */
GetAddress(WDFMEMORY wdf_mem)53 __forceinline void* GetAddress(WDFMEMORY wdf_mem) {
54   ASSERT(NULL != wdf_mem);
55   return (NULL != wdf_mem) ? WdfMemoryGetBuffer(wdf_mem, NULL) : NULL;
56 }
57 
58 /** \brief Retrieves output memory address for WDFREQUEST
59 
60   @param request[in] A handle to KMDF request object
61   @param status[out] Receives status of the call. Can be NULL.
62 */
OutAddress(WDFREQUEST request,NTSTATUS * status)63 __forceinline void* OutAddress(WDFREQUEST request, NTSTATUS* status) {
64   ASSERT(NULL != request);
65   WDFMEMORY wdf_mem = NULL;
66   NTSTATUS stat = WdfRequestRetrieveOutputMemory(request, &wdf_mem);
67   ASSERT((NULL != wdf_mem) || (!NT_SUCCESS(stat)));
68   if (NULL != status)
69     *status = stat;
70   return NT_SUCCESS(stat) ? GetAddress(wdf_mem) : NULL;
71 }
72 
73 /** \brief Retrieves input memory address for WDFREQUEST
74 
75   @param request[in] A handle to KMDF request object
76   @param status[out] Receives status of the call. Can be NULL.
77 */
InAddress(WDFREQUEST request,NTSTATUS * status)78 __forceinline void* InAddress(WDFREQUEST request, NTSTATUS* status) {
79   ASSERT(NULL != request);
80   WDFMEMORY wdf_mem = NULL;
81   NTSTATUS stat = WdfRequestRetrieveInputMemory(request, &wdf_mem);
82   ASSERT((NULL != wdf_mem) || (!NT_SUCCESS(stat)));
83   if (NULL != status)
84     *status = stat;
85   return NT_SUCCESS(stat) ? GetAddress(wdf_mem) : NULL;
86 }
87 
88 #endif  // ANDROID_USB_INL_H__
89