hlink/tests: Add a trailing '\n' to an ok() call.
[wine] / dlls / usbd.sys / usbd.c
1 /*
2  * Copyright (C) 2010 Damjan Jovanovic
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "ddk/wdm.h"
30 #include "ddk/usb.h"
31 #include "ddk/usbdlib.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(usbd);
35
36 PUSB_INTERFACE_DESCRIPTOR WINAPI USBD_ParseConfigurationDescriptorEx(
37         PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
38         PVOID StartPosition, LONG InterfaceNumber,
39         LONG AlternateSetting, LONG InterfaceClass,
40         LONG InterfaceSubClass, LONG InterfaceProtocol )
41 {
42     /* http://blogs.msdn.com/usbcoreblog/archive/2009/12/12/
43      *        what-is-the-right-way-to-validate-and-parse-configuration-descriptors.aspx
44      */
45
46     PUSB_INTERFACE_DESCRIPTOR interface;
47
48     TRACE( "(%p, %p, %d, %d, %d, %d, %d)\n", ConfigurationDescriptor,
49             StartPosition, InterfaceNumber, AlternateSetting,
50             InterfaceClass, InterfaceSubClass, InterfaceProtocol );
51
52     interface = (PUSB_INTERFACE_DESCRIPTOR) USBD_ParseDescriptors(
53         ConfigurationDescriptor, ConfigurationDescriptor->wTotalLength,
54         StartPosition, USB_INTERFACE_DESCRIPTOR_TYPE );
55     while (interface != NULL)
56     {
57         if ((InterfaceNumber == -1 || interface->bInterfaceNumber == InterfaceNumber) &&
58             (AlternateSetting == -1 || interface->bAlternateSetting == AlternateSetting) &&
59             (InterfaceClass == -1 || interface->bInterfaceClass == InterfaceClass) &&
60             (InterfaceSubClass == -1 || interface->bInterfaceSubClass == InterfaceSubClass) &&
61             (InterfaceProtocol == -1 || interface->bInterfaceProtocol == InterfaceProtocol))
62         {
63             return interface;
64         }
65         interface = (PUSB_INTERFACE_DESCRIPTOR) USBD_ParseDescriptors(
66             ConfigurationDescriptor, ConfigurationDescriptor->wTotalLength,
67             interface + 1, USB_INTERFACE_DESCRIPTOR_TYPE );
68     }
69     return NULL;
70 }
71
72 PUSB_COMMON_DESCRIPTOR WINAPI USBD_ParseDescriptors(
73         PVOID DescriptorBuffer,
74         ULONG TotalLength,
75         PVOID StartPosition,
76         LONG DescriptorType )
77 {
78     PUSB_COMMON_DESCRIPTOR common;
79
80     TRACE( "(%p, %u, %p, %d)\n", DescriptorBuffer, TotalLength, StartPosition, DescriptorType );
81
82     for (common = (PUSB_COMMON_DESCRIPTOR)DescriptorBuffer;
83          ((char*)common) + sizeof(USB_COMMON_DESCRIPTOR) <= ((char*)DescriptorBuffer) + TotalLength;
84          common = (PUSB_COMMON_DESCRIPTOR)(((char*)common) + common->bLength))
85     {
86         if (StartPosition <= (PVOID)common && common->bDescriptorType == DescriptorType)
87             return common;
88     }
89     return NULL;
90 }
91
92 ULONG WINAPI USBD_GetInterfaceLength(
93         PUSB_INTERFACE_DESCRIPTOR InterfaceDescriptor,
94         PUCHAR BufferEnd )
95 {
96     PUSB_COMMON_DESCRIPTOR common;
97     ULONG total = InterfaceDescriptor->bLength;
98
99     TRACE( "(%p, %p)\n", InterfaceDescriptor, BufferEnd );
100
101     for (common = (PUSB_COMMON_DESCRIPTOR)(InterfaceDescriptor + 1);
102          (((PUCHAR)common) + sizeof(USB_COMMON_DESCRIPTOR)) <= BufferEnd &&
103              common->bDescriptorType != USB_INTERFACE_DESCRIPTOR_TYPE;
104          common = (PUSB_COMMON_DESCRIPTOR)(((char*)common) + common->bLength))
105     {
106         total += common->bLength;
107     }
108     return total;
109 }
110
111 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
112 {
113     TRACE( "(%p, %s)\n", driver, debugstr_w(path->Buffer) );
114     return STATUS_SUCCESS;
115 }