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 
release_interface(libusb_device_t * dev,FILE_OBJECT * file_object,int interface)24 NTSTATUS release_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
25                            int interface)
26 {
27     USBMSG("interface %d\n", interface);
28 
29     if (!dev->config.value)
30     {
31         USBERR0("device is not configured\n");
32         return STATUS_INVALID_DEVICE_STATE;
33     }
34 
35     if (interface >= LIBUSB_MAX_NUMBER_OF_INTERFACES)
36     {
37         USBERR("interface number %d too high\n",
38                     interface);
39         return STATUS_INVALID_PARAMETER;
40     }
41 
42     if (!dev->config.interfaces[interface].valid)
43     {
44         USBERR("invalid interface %02d\n", interface);
45         return STATUS_INVALID_PARAMETER;
46     }
47 
48     if (!dev->config.interfaces[interface].file_object)
49     {
50         USBERR("could not release interface %d, interface is not claimed\n", interface);
51         return STATUS_INVALID_DEVICE_STATE;
52     }
53 
54     if (dev->config.interfaces[interface].file_object != file_object)
55     {
56         USBERR("could not release interface %d, interface is not bound to this file object\n", interface);
57         return STATUS_DEVICE_BUSY;
58     }
59 
60     dev->config.interfaces[interface].file_object = NULL;
61 
62     return STATUS_SUCCESS;
63 }
64 
release_interface_ex(libusb_device_t * dev,FILE_OBJECT * file_object,interface_request_t * interface_request)65 NTSTATUS release_interface_ex(libusb_device_t *dev,
66 							  FILE_OBJECT *file_object,
67 							  interface_request_t* interface_request)
68 {
69  	PUSB_INTERFACE_DESCRIPTOR interface_descriptor;
70 
71 	USBMSG("interface-%s=%d\n",
72 		interface_request->intf_use_index ? "index" : "number",
73 		interface_request->intf_use_index ? interface_request->interface_index : interface_request->interface_number);
74 
75     if (!dev->config.value || !dev->config.descriptor)
76     {
77         USBERR0("device is not configured\n");
78         return STATUS_INVALID_DEVICE_STATE;
79     }
80 
81 	interface_request->altsetting_index=FIND_INTERFACE_INDEX_ANY;
82 	interface_descriptor = find_interface_desc_ex(dev->config.descriptor,dev->config.total_size,interface_request,NULL);
83 	if (!interface_descriptor)
84 	{
85         return STATUS_NO_MORE_ENTRIES;
86 	}
87 	return release_interface(dev, file_object, interface_descriptor->bInterfaceNumber);
88 }
89 
release_all_interfaces(libusb_device_t * dev,FILE_OBJECT * file_object)90 NTSTATUS release_all_interfaces(libusb_device_t *dev, FILE_OBJECT *file_object)
91 {
92     int i;
93 
94     USBMSG("releasing all interfaces bound to file object 0x%x\n", file_object);
95 
96     for (i = 0; i < LIBUSB_MAX_NUMBER_OF_INTERFACES; i++)
97     {
98         if (dev->config.interfaces[i].file_object == file_object)
99         {
100             dev->config.interfaces[i].file_object = NULL;
101         }
102     }
103 
104     return STATUS_SUCCESS;
105 }
106