1 /* libusb-win32, Generic Windows USB Library
2  * Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 
20 #include "libusb_driver.h"
21 
22 
23 
get_interface(libusb_device_t * dev,int interface_number,unsigned char * altsetting,int timeout)24 NTSTATUS get_interface(libusb_device_t *dev,
25 					   int interface_number,
26 					   unsigned char *altsetting,
27 					   int timeout)
28 {
29     NTSTATUS status = STATUS_SUCCESS;
30     URB urb;
31 	PUSB_INTERFACE_DESCRIPTOR interface_descriptor;
32 
33 	USBMSG("interface: %d timeout: %d\n", interface_number, timeout);
34 
35     if (!dev->config.value)
36     {
37         USBERR0("invalid configuration 0\n");
38         return STATUS_INVALID_DEVICE_STATE;
39     }
40 
41     memset(&urb, 0, sizeof(URB));
42 
43     urb.UrbHeader.Function = URB_FUNCTION_GET_INTERFACE;
44     urb.UrbHeader.Length = sizeof(struct _URB_CONTROL_GET_INTERFACE_REQUEST);
45     urb.UrbControlGetInterfaceRequest.TransferBufferLength = 1;
46     urb.UrbControlGetInterfaceRequest.TransferBuffer = altsetting;
47     urb.UrbControlGetInterfaceRequest.Interface = (USHORT)interface_number;
48 
49     status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout);
50 
51     if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status))
52     {
53         USBERR("getting interface failed: status: 0x%x, urb-status: 0x%x\n",
54                     status, urb.UrbHeader.Status);
55     }
56     else
57     {
58         USBMSG("current altsetting is %d\n", *altsetting);
59     }
60 
61     return status;
62 }
63 
get_interface_ex(libusb_device_t * dev,interface_request_t * interface_request,int timeout)64 NTSTATUS get_interface_ex(libusb_device_t *dev,
65 					   interface_request_t* interface_request,
66                        int timeout)
67 {
68     unsigned char alt_number;
69 	NTSTATUS status = STATUS_SUCCESS;
70 
71     USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL;
72 
73 	USBMSG("interface-%s=%d timeout=%d\n",
74 		interface_request->intf_use_index ? "index" : "number",
75 		interface_request->intf_use_index ? interface_request->interface_index : interface_request->interface_number,
76 		timeout);
77 
78     if (!dev->config.value || !dev->config.descriptor)
79     {
80         USBERR0("device is not configured\n");
81         return STATUS_INVALID_DEVICE_STATE;
82     }
83 
84 	interface_request->altsetting_index=FIND_INTERFACE_INDEX_ANY;
85 	interface_descriptor = find_interface_desc_ex(dev->config.descriptor,dev->config.total_size,interface_request,NULL);
86 	if (!interface_descriptor)
87 	{
88         return STATUS_NO_MORE_ENTRIES;
89 	}
90 
91 	status = get_interface(dev,interface_descriptor->bInterfaceNumber, &alt_number, timeout);
92 	if (!NT_SUCCESS(status))
93 	{
94         return STATUS_NO_MORE_ENTRIES;
95 	}
96 
97 	interface_request->interface_number=interface_descriptor->bInterfaceNumber;
98 	interface_request->altsetting_number=alt_number;
99 	interface_request->altf_use_index=0;
100 	interface_request->intf_use_index=0;
101 
102 	interface_descriptor = find_interface_desc_ex(dev->config.descriptor,dev->config.total_size,interface_request,NULL);
103 	if (!interface_descriptor)
104 	{
105         return STATUS_NO_MORE_ENTRIES;
106 	}
107 
108 	return status;
109 }
110