]>
Commit | Line | Data |
---|---|---|
e33dcf90 ML |
1 | /* |
2 | * This file is part of the libserialport project. | |
3 | * | |
48a4076f | 4 | * Copyright (C) 2013-2014 Martin Ling <martin-libserialport@earth.li> |
e33dcf90 ML |
5 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Lesser General Public License as | |
9 | * published by the Free Software Foundation, either version 3 of the | |
10 | * License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
1a584c45 | 21 | #include <config.h> |
e33dcf90 ML |
22 | #include "libserialport.h" |
23 | #include "libserialport_internal.h" | |
24 | ||
dc422c04 UH |
25 | /* USB path is a string of at most 8 decimal numbers < 128 separated by dots. */ |
26 | #define MAX_USB_PATH ((8 * 3) + (7 * 1) + 1) | |
e33dcf90 | 27 | |
eb50b1ac | 28 | static void enumerate_hub(struct sp_port *port, const char *hub_name, |
02c8a142 | 29 | const char *parent_path, DEVINST dev_inst); |
e33dcf90 ML |
30 | |
31 | static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size) | |
32 | { | |
38b71192 MJ |
33 | ULONG wc_length = size / sizeof(WCHAR); |
34 | WCHAR wc_str[wc_length + 1]; | |
e33dcf90 ML |
35 | char *utf8_str; |
36 | ||
dc422c04 | 37 | /* Zero-terminate the wide char string. */ |
e33dcf90 | 38 | memcpy(wc_str, wc_buffer, size); |
38b71192 | 39 | wc_str[wc_length] = 0; |
e33dcf90 | 40 | |
dc422c04 | 41 | /* Compute the size of the UTF-8 converted string. */ |
e33dcf90 ML |
42 | if (!(size = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1, |
43 | NULL, 0, NULL, NULL))) | |
44 | return NULL; | |
45 | ||
dc422c04 | 46 | /* Allocate UTF-8 output buffer. */ |
e33dcf90 ML |
47 | if (!(utf8_str = malloc(size))) |
48 | return NULL; | |
49 | ||
dc422c04 | 50 | /* Actually converted to UTF-8. */ |
e33dcf90 ML |
51 | if (!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1, |
52 | utf8_str, size, NULL, NULL)) { | |
53 | free(utf8_str); | |
54 | return NULL; | |
55 | } | |
56 | ||
57 | return utf8_str; | |
58 | } | |
59 | ||
60 | static char *get_root_hub_name(HANDLE host_controller) | |
61 | { | |
dc422c04 | 62 | USB_ROOT_HUB_NAME root_hub_name; |
e33dcf90 ML |
63 | PUSB_ROOT_HUB_NAME root_hub_name_wc; |
64 | char *root_hub_name_utf8; | |
65 | ULONG size = 0; | |
66 | ||
dc422c04 | 67 | /* Compute the size of the root hub name string. */ |
e33dcf90 ML |
68 | if (!DeviceIoControl(host_controller, IOCTL_USB_GET_ROOT_HUB_NAME, 0, 0, |
69 | &root_hub_name, sizeof(root_hub_name), &size, NULL)) | |
70 | return NULL; | |
71 | ||
dc422c04 | 72 | /* Allocate wide char root hub name string. */ |
e33dcf90 ML |
73 | size = root_hub_name.ActualLength; |
74 | if (!(root_hub_name_wc = malloc(size))) | |
75 | return NULL; | |
76 | ||
dc422c04 | 77 | /* Actually get the root hub name string. */ |
e33dcf90 ML |
78 | if (!DeviceIoControl(host_controller, IOCTL_USB_GET_ROOT_HUB_NAME, |
79 | NULL, 0, root_hub_name_wc, size, &size, NULL)) { | |
80 | free(root_hub_name_wc); | |
81 | return NULL; | |
82 | } | |
83 | ||
dc422c04 | 84 | /* Convert the root hub name string to UTF-8. */ |
2e0437c2 | 85 | root_hub_name_utf8 = wc_to_utf8(root_hub_name_wc->RootHubName, size - offsetof(USB_ROOT_HUB_NAME, RootHubName)); |
e33dcf90 ML |
86 | free(root_hub_name_wc); |
87 | return root_hub_name_utf8; | |
88 | } | |
89 | ||
90 | static char *get_external_hub_name(HANDLE hub, ULONG connection_index) | |
91 | { | |
dc422c04 | 92 | USB_NODE_CONNECTION_NAME ext_hub_name; |
e33dcf90 ML |
93 | PUSB_NODE_CONNECTION_NAME ext_hub_name_wc; |
94 | char *ext_hub_name_utf8; | |
95 | ULONG size; | |
96 | ||
dc422c04 | 97 | /* Compute the size of the external hub name string. */ |
e33dcf90 ML |
98 | ext_hub_name.ConnectionIndex = connection_index; |
99 | if (!DeviceIoControl(hub, IOCTL_USB_GET_NODE_CONNECTION_NAME, | |
100 | &ext_hub_name, sizeof(ext_hub_name), | |
101 | &ext_hub_name, sizeof(ext_hub_name), &size, NULL)) | |
102 | return NULL; | |
103 | ||
dc422c04 | 104 | /* Allocate wide char external hub name string. */ |
e33dcf90 ML |
105 | size = ext_hub_name.ActualLength; |
106 | if (size <= sizeof(ext_hub_name) | |
107 | || !(ext_hub_name_wc = malloc(size))) | |
108 | return NULL; | |
109 | ||
dc422c04 | 110 | /* Get the name of the external hub attached to the specified port. */ |
e33dcf90 ML |
111 | ext_hub_name_wc->ConnectionIndex = connection_index; |
112 | if (!DeviceIoControl(hub, IOCTL_USB_GET_NODE_CONNECTION_NAME, | |
113 | ext_hub_name_wc, size, | |
114 | ext_hub_name_wc, size, &size, NULL)) { | |
115 | free(ext_hub_name_wc); | |
116 | return NULL; | |
117 | } | |
118 | ||
dc422c04 | 119 | /* Convert the external hub name string to UTF-8. */ |
2e0437c2 | 120 | ext_hub_name_utf8 = wc_to_utf8(ext_hub_name_wc->NodeName, size - offsetof(USB_NODE_CONNECTION_NAME, NodeName)); |
e33dcf90 ML |
121 | free(ext_hub_name_wc); |
122 | return ext_hub_name_utf8; | |
123 | } | |
124 | ||
125 | static char *get_string_descriptor(HANDLE hub_device, ULONG connection_index, | |
126 | UCHAR descriptor_index) | |
127 | { | |
128 | char desc_req_buf[sizeof(USB_DESCRIPTOR_REQUEST) + | |
129 | MAXIMUM_USB_STRING_LENGTH] = { 0 }; | |
dc422c04 UH |
130 | PUSB_DESCRIPTOR_REQUEST desc_req = (void *)desc_req_buf; |
131 | PUSB_STRING_DESCRIPTOR desc = (void *)(desc_req + 1); | |
e33dcf90 ML |
132 | ULONG size = sizeof(desc_req_buf); |
133 | ||
dc422c04 UH |
134 | desc_req->ConnectionIndex = connection_index; |
135 | desc_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | |
136 | | descriptor_index; | |
137 | desc_req->SetupPacket.wIndex = 0; | |
e33dcf90 ML |
138 | desc_req->SetupPacket.wLength = size - sizeof(*desc_req); |
139 | ||
140 | if (!DeviceIoControl(hub_device, | |
141 | IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, | |
142 | desc_req, size, desc_req, size, &size, NULL) | |
143 | || size < 2 | |
144 | || desc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE | |
145 | || desc->bLength != size - sizeof(*desc_req) | |
146 | || desc->bLength % 2) | |
147 | return NULL; | |
148 | ||
2e0437c2 | 149 | return wc_to_utf8(desc->bString, desc->bLength - offsetof(USB_STRING_DESCRIPTOR, bString)); |
e33dcf90 ML |
150 | } |
151 | ||
152 | static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device, | |
02c8a142 | 153 | ULONG nb_ports, const char *parent_path, DEVINST dev_inst) |
e33dcf90 ML |
154 | { |
155 | char path[MAX_USB_PATH]; | |
156 | ULONG index = 0; | |
157 | ||
158 | for (index = 1; index <= nb_ports; index++) { | |
159 | PUSB_NODE_CONNECTION_INFORMATION_EX connection_info_ex; | |
dc422c04 | 160 | ULONG size = sizeof(*connection_info_ex) + (30 * sizeof(USB_PIPE_INFO)); |
e33dcf90 ML |
161 | |
162 | if (!(connection_info_ex = malloc(size))) | |
163 | break; | |
164 | ||
165 | connection_info_ex->ConnectionIndex = index; | |
166 | if (!DeviceIoControl(hub_device, | |
167 | IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, | |
168 | connection_info_ex, size, | |
169 | connection_info_ex, size, &size, NULL)) { | |
dc422c04 UH |
170 | /* |
171 | * Try to get CONNECTION_INFORMATION if | |
172 | * CONNECTION_INFORMATION_EX did not work. | |
173 | */ | |
e33dcf90 ML |
174 | PUSB_NODE_CONNECTION_INFORMATION connection_info; |
175 | ||
dc422c04 | 176 | size = sizeof(*connection_info) + (30 * sizeof(USB_PIPE_INFO)); |
e33dcf90 ML |
177 | if (!(connection_info = malloc(size))) { |
178 | free(connection_info_ex); | |
179 | continue; | |
180 | } | |
181 | connection_info->ConnectionIndex = index; | |
182 | if (!DeviceIoControl(hub_device, | |
183 | IOCTL_USB_GET_NODE_CONNECTION_INFORMATION, | |
184 | connection_info, size, | |
185 | connection_info, size, &size, NULL)) { | |
186 | free(connection_info); | |
187 | free(connection_info_ex); | |
188 | continue; | |
189 | } | |
190 | ||
191 | connection_info_ex->ConnectionIndex = connection_info->ConnectionIndex; | |
192 | connection_info_ex->DeviceDescriptor = connection_info->DeviceDescriptor; | |
193 | connection_info_ex->DeviceIsHub = connection_info->DeviceIsHub; | |
194 | connection_info_ex->DeviceAddress = connection_info->DeviceAddress; | |
195 | free(connection_info); | |
196 | } | |
197 | ||
198 | if (connection_info_ex->DeviceIsHub) { | |
dc422c04 | 199 | /* Recursively enumerate external hub. */ |
e33dcf90 ML |
200 | PCHAR ext_hub_name; |
201 | if ((ext_hub_name = get_external_hub_name(hub_device, index))) { | |
202 | snprintf(path, sizeof(path), "%s%ld.", | |
203 | parent_path, connection_info_ex->ConnectionIndex); | |
02c8a142 | 204 | enumerate_hub(port, ext_hub_name, path, dev_inst); |
e33dcf90 ML |
205 | } |
206 | free(connection_info_ex); | |
207 | } else { | |
208 | snprintf(path, sizeof(path), "%s%ld", | |
209 | parent_path, connection_info_ex->ConnectionIndex); | |
210 | ||
dc422c04 | 211 | /* Check if this device is the one we search for. */ |
e33dcf90 ML |
212 | if (strcmp(path, port->usb_path)) { |
213 | free(connection_info_ex); | |
214 | continue; | |
215 | } | |
216 | ||
dc422c04 | 217 | /* Finally grab detailed information regarding the device. */ |
e33dcf90 ML |
218 | port->usb_address = connection_info_ex->DeviceAddress + 1; |
219 | port->usb_vid = connection_info_ex->DeviceDescriptor.idVendor; | |
220 | port->usb_pid = connection_info_ex->DeviceDescriptor.idProduct; | |
221 | ||
222 | if (connection_info_ex->DeviceDescriptor.iManufacturer) | |
81243567 | 223 | port->usb_manufacturer = get_string_descriptor(hub_device, index, |
e33dcf90 ML |
224 | connection_info_ex->DeviceDescriptor.iManufacturer); |
225 | if (connection_info_ex->DeviceDescriptor.iProduct) | |
226 | port->usb_product = get_string_descriptor(hub_device, index, | |
227 | connection_info_ex->DeviceDescriptor.iProduct); | |
02c8a142 | 228 | if (connection_info_ex->DeviceDescriptor.iSerialNumber) { |
e33dcf90 ML |
229 | port->usb_serial = get_string_descriptor(hub_device, index, |
230 | connection_info_ex->DeviceDescriptor.iSerialNumber); | |
02c8a142 MF |
231 | if (port->usb_serial == NULL) { |
232 | //composite device, get the parent's serial number | |
233 | char device_id[MAX_DEVICE_ID_LEN]; | |
234 | if (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) { | |
235 | if (CM_Get_Device_IDA(dev_inst, device_id, sizeof(device_id), 0) == CR_SUCCESS) | |
236 | port->usb_serial = strdup(strrchr(device_id, '\\')+1); | |
237 | } | |
238 | } | |
239 | } | |
e33dcf90 ML |
240 | |
241 | free(connection_info_ex); | |
242 | break; | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
eb50b1ac | 247 | static void enumerate_hub(struct sp_port *port, const char *hub_name, |
02c8a142 | 248 | const char *parent_path, DEVINST dev_inst) |
e33dcf90 ML |
249 | { |
250 | USB_NODE_INFORMATION hub_info; | |
251 | HANDLE hub_device; | |
252 | ULONG size = sizeof(hub_info); | |
253 | char *device_name; | |
254 | ||
dc422c04 | 255 | /* Open the hub with its full name. */ |
e33dcf90 ML |
256 | if (!(device_name = malloc(strlen("\\\\.\\") + strlen(hub_name) + 1))) |
257 | return; | |
258 | strcpy(device_name, "\\\\.\\"); | |
259 | strcat(device_name, hub_name); | |
260 | hub_device = CreateFile(device_name, GENERIC_WRITE, FILE_SHARE_WRITE, | |
261 | NULL, OPEN_EXISTING, 0, NULL); | |
262 | free(device_name); | |
263 | if (hub_device == INVALID_HANDLE_VALUE) | |
264 | return; | |
265 | ||
dc422c04 | 266 | /* Get the number of ports of the hub. */ |
e33dcf90 ML |
267 | if (DeviceIoControl(hub_device, IOCTL_USB_GET_NODE_INFORMATION, |
268 | &hub_info, size, &hub_info, size, &size, NULL)) | |
dc422c04 | 269 | /* Enumerate the ports of the hub. */ |
e33dcf90 | 270 | enumerate_hub_ports(port, hub_device, |
02c8a142 | 271 | hub_info.u.HubInformation.HubDescriptor.bNumberOfPorts, parent_path, dev_inst); |
e33dcf90 ML |
272 | |
273 | CloseHandle(hub_device); | |
274 | } | |
275 | ||
276 | static void enumerate_host_controller(struct sp_port *port, | |
02c8a142 MF |
277 | HANDLE host_controller_device, |
278 | DEVINST dev_inst) | |
e33dcf90 ML |
279 | { |
280 | char *root_hub_name; | |
281 | ||
282 | if ((root_hub_name = get_root_hub_name(host_controller_device))) { | |
02c8a142 | 283 | enumerate_hub(port, root_hub_name, "", dev_inst); |
e33dcf90 ML |
284 | free(root_hub_name); |
285 | } | |
286 | } | |
287 | ||
288 | static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match) | |
289 | { | |
290 | HDEVINFO device_info; | |
291 | SP_DEVINFO_DATA device_info_data; | |
292 | ULONG i, size = 0; | |
293 | ||
dc422c04 | 294 | device_info = SetupDiGetClassDevs(&GUID_CLASS_USB_HOST_CONTROLLER, NULL, NULL, |
e33dcf90 ML |
295 | DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); |
296 | device_info_data.cbSize = sizeof(device_info_data); | |
297 | ||
dc422c04 | 298 | for (i = 0; SetupDiEnumDeviceInfo(device_info, i, &device_info_data); i++) { |
e33dcf90 ML |
299 | SP_DEVICE_INTERFACE_DATA device_interface_data; |
300 | PSP_DEVICE_INTERFACE_DETAIL_DATA device_detail_data; | |
301 | DEVINST dev_inst = dev_inst_match; | |
302 | HANDLE host_controller_device; | |
303 | ||
304 | device_interface_data.cbSize = sizeof(device_interface_data); | |
305 | if (!SetupDiEnumDeviceInterfaces(device_info, 0, | |
306 | &GUID_CLASS_USB_HOST_CONTROLLER, | |
307 | i, &device_interface_data)) | |
308 | continue; | |
309 | ||
310 | if (!SetupDiGetDeviceInterfaceDetail(device_info,&device_interface_data, | |
311 | NULL, 0, &size, NULL) | |
312 | && GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
313 | continue; | |
314 | ||
315 | if (!(device_detail_data = malloc(size))) | |
316 | continue; | |
317 | device_detail_data->cbSize = sizeof(*device_detail_data); | |
318 | if (!SetupDiGetDeviceInterfaceDetail(device_info,&device_interface_data, | |
319 | device_detail_data, size, &size, | |
320 | NULL)) { | |
321 | free(device_detail_data); | |
322 | continue; | |
323 | } | |
324 | ||
325 | while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS | |
326 | && dev_inst != device_info_data.DevInst) { } | |
327 | if (dev_inst != device_info_data.DevInst) { | |
328 | free(device_detail_data); | |
329 | continue; | |
330 | } | |
331 | ||
332 | port->usb_bus = i + 1; | |
333 | ||
334 | host_controller_device = CreateFile(device_detail_data->DevicePath, | |
335 | GENERIC_WRITE, FILE_SHARE_WRITE, | |
336 | NULL, OPEN_EXISTING, 0, NULL); | |
337 | if (host_controller_device != INVALID_HANDLE_VALUE) { | |
02c8a142 | 338 | enumerate_host_controller(port, host_controller_device, dev_inst_match); |
e33dcf90 ML |
339 | CloseHandle(host_controller_device); |
340 | } | |
341 | free(device_detail_data); | |
342 | } | |
343 | ||
344 | SetupDiDestroyDeviceInfoList(device_info); | |
345 | return; | |
346 | } | |
347 | ||
970f279a | 348 | SP_PRIV enum sp_return get_port_details(struct sp_port *port) |
e33dcf90 | 349 | { |
dc422c04 UH |
350 | /* |
351 | * Description limited to 127 char, anything longer | |
352 | * would not be user friendly anyway. | |
353 | */ | |
e33dcf90 ML |
354 | char description[128]; |
355 | SP_DEVINFO_DATA device_info_data = { .cbSize = sizeof(device_info_data) }; | |
356 | HDEVINFO device_info; | |
357 | int i; | |
358 | ||
359 | device_info = SetupDiGetClassDevs(NULL, 0, 0, | |
360 | DIGCF_PRESENT | DIGCF_ALLCLASSES); | |
361 | if (device_info == INVALID_HANDLE_VALUE) | |
362 | RETURN_FAIL("SetupDiGetClassDevs() failed"); | |
363 | ||
dc422c04 | 364 | for (i = 0; SetupDiEnumDeviceInfo(device_info, i, &device_info_data); i++) { |
e33dcf90 ML |
365 | HKEY device_key; |
366 | DEVINST dev_inst; | |
367 | char value[8], class[16]; | |
368 | DWORD size, type; | |
369 | CONFIGRET cr; | |
370 | ||
dc422c04 | 371 | /* Check if this is the device we are looking for. */ |
b328a48b AJ |
372 | device_key = SetupDiOpenDevRegKey(device_info, &device_info_data, |
373 | DICS_FLAG_GLOBAL, 0, | |
374 | DIREG_DEV, KEY_QUERY_VALUE); | |
375 | if (device_key == INVALID_HANDLE_VALUE) | |
e33dcf90 ML |
376 | continue; |
377 | size = sizeof(value); | |
378 | if (RegQueryValueExA(device_key, "PortName", NULL, &type, (LPBYTE)value, | |
7aeb6736 AJ |
379 | &size) != ERROR_SUCCESS || type != REG_SZ) { |
380 | RegCloseKey(device_key); | |
e33dcf90 | 381 | continue; |
7aeb6736 | 382 | } |
e33dcf90 | 383 | RegCloseKey(device_key); |
81243567 | 384 | value[sizeof(value) - 1] = 0; |
e33dcf90 ML |
385 | if (strcmp(value, port->name)) |
386 | continue; | |
387 | ||
dc422c04 | 388 | /* Check port transport type. */ |
e33dcf90 ML |
389 | dev_inst = device_info_data.DevInst; |
390 | size = sizeof(class); | |
391 | cr = CR_FAILURE; | |
392 | while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS && | |
393 | (cr = CM_Get_DevNode_Registry_PropertyA(dev_inst, | |
394 | CM_DRP_CLASS, 0, class, &size, 0)) != CR_SUCCESS) { } | |
395 | if (cr == CR_SUCCESS) { | |
396 | if (!strcmp(class, "USB")) | |
397 | port->transport = SP_TRANSPORT_USB; | |
398 | } | |
399 | ||
dc422c04 | 400 | /* Get port description (friendly name). */ |
e33dcf90 ML |
401 | dev_inst = device_info_data.DevInst; |
402 | size = sizeof(description); | |
403 | while ((cr = CM_Get_DevNode_Registry_PropertyA(dev_inst, | |
404 | CM_DRP_FRIENDLYNAME, 0, description, &size, 0)) != CR_SUCCESS | |
405 | && CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) { } | |
406 | if (cr == CR_SUCCESS) | |
407 | port->description = strdup(description); | |
408 | ||
dc422c04 | 409 | /* Get more informations for USB connected ports. */ |
e33dcf90 ML |
410 | if (port->transport == SP_TRANSPORT_USB) { |
411 | char usb_path[MAX_USB_PATH] = "", tmp[MAX_USB_PATH]; | |
412 | char device_id[MAX_DEVICE_ID_LEN]; | |
413 | ||
dc422c04 | 414 | /* Recurse over parents to build the USB device path. */ |
e33dcf90 ML |
415 | dev_inst = device_info_data.DevInst; |
416 | do { | |
dc422c04 | 417 | /* Verify that this layer of the tree is USB related. */ |
e33dcf90 ML |
418 | if (CM_Get_Device_IDA(dev_inst, device_id, |
419 | sizeof(device_id), 0) != CR_SUCCESS | |
420 | || strncmp(device_id, "USB\\", 4)) | |
421 | continue; | |
422 | ||
dc422c04 | 423 | /* Discard one layer for composite devices. */ |
e33dcf90 ML |
424 | char compat_ids[512], *p = compat_ids; |
425 | size = sizeof(compat_ids); | |
426 | if (CM_Get_DevNode_Registry_PropertyA(dev_inst, | |
427 | CM_DRP_COMPATIBLEIDS, 0, | |
428 | &compat_ids, | |
429 | &size, 0) == CR_SUCCESS) { | |
430 | while (*p) { | |
431 | if (!strncmp(p, "USB\\COMPOSITE", 13)) | |
432 | break; | |
433 | p += strlen(p) + 1; | |
434 | } | |
435 | if (*p) | |
436 | continue; | |
437 | } | |
438 | ||
dc422c04 | 439 | /* Stop the recursion when reaching the USB root. */ |
e33dcf90 ML |
440 | if (!strncmp(device_id, "USB\\ROOT", 8)) |
441 | break; | |
442 | ||
dc422c04 | 443 | /* Prepend the address of current USB layer to the USB path. */ |
e33dcf90 ML |
444 | DWORD address; |
445 | size = sizeof(address); | |
446 | if (CM_Get_DevNode_Registry_PropertyA(dev_inst, CM_DRP_ADDRESS, | |
447 | 0, &address, &size, 0) == CR_SUCCESS) { | |
448 | strcpy(tmp, usb_path); | |
449 | snprintf(usb_path, sizeof(usb_path), "%d%s%s", | |
450 | (int)address, *tmp ? "." : "", tmp); | |
451 | } | |
452 | } while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS); | |
453 | ||
454 | port->usb_path = strdup(usb_path); | |
455 | ||
dc422c04 | 456 | /* Wake up the USB device to be able to read string descriptor. */ |
e33dcf90 ML |
457 | char *escaped_port_name; |
458 | HANDLE handle; | |
459 | if (!(escaped_port_name = malloc(strlen(port->name) + 5))) | |
460 | RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed"); | |
461 | sprintf(escaped_port_name, "\\\\.\\%s", port->name); | |
462 | handle = CreateFile(escaped_port_name, GENERIC_READ, 0, 0, | |
463 | OPEN_EXISTING, | |
464 | FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0); | |
465 | free(escaped_port_name); | |
466 | CloseHandle(handle); | |
467 | ||
dc422c04 | 468 | /* Retrieve USB device details from the device descriptor. */ |
e33dcf90 ML |
469 | get_usb_details(port, device_info_data.DevInst); |
470 | } | |
471 | break; | |
472 | } | |
473 | ||
7aeb6736 AJ |
474 | SetupDiDestroyDeviceInfoList(device_info); |
475 | ||
e33dcf90 ML |
476 | RETURN_OK(); |
477 | } | |
478 | ||
970f279a | 479 | SP_PRIV enum sp_return list_ports(struct sp_port ***list) |
48a4076f AJ |
480 | { |
481 | HKEY key; | |
482 | TCHAR *value, *data; | |
483 | DWORD max_value_len, max_data_size, max_data_len; | |
484 | DWORD value_len, data_size, data_len; | |
485 | DWORD type, index = 0; | |
486 | char *name; | |
487 | int name_len; | |
488 | int ret = SP_OK; | |
489 | ||
490 | DEBUG("Opening registry key"); | |
491 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"), | |
492 | 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) { | |
493 | SET_FAIL(ret, "RegOpenKeyEx() failed"); | |
494 | goto out_done; | |
495 | } | |
496 | DEBUG("Querying registry key value and data sizes"); | |
497 | if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
498 | &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) { | |
499 | SET_FAIL(ret, "RegQueryInfoKey() failed"); | |
500 | goto out_close; | |
501 | } | |
502 | max_data_len = max_data_size / sizeof(TCHAR); | |
503 | if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) { | |
dc422c04 | 504 | SET_ERROR(ret, SP_ERR_MEM, "Registry value malloc failed"); |
48a4076f AJ |
505 | goto out_close; |
506 | } | |
507 | if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) { | |
dc422c04 | 508 | SET_ERROR(ret, SP_ERR_MEM, "Registry data malloc failed"); |
48a4076f AJ |
509 | goto out_free_value; |
510 | } | |
511 | DEBUG("Iterating over values"); | |
512 | while ( | |
513 | value_len = max_value_len + 1, | |
514 | data_size = max_data_size, | |
515 | RegEnumValue(key, index, value, &value_len, | |
516 | NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS) | |
517 | { | |
7aeb6736 | 518 | if (type == REG_SZ) { |
6c444ade AJ |
519 | data_len = data_size / sizeof(TCHAR); |
520 | data[data_len] = '\0'; | |
48a4076f | 521 | #ifdef UNICODE |
6c444ade | 522 | name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL); |
48a4076f | 523 | #else |
6c444ade | 524 | name_len = data_len + 1; |
48a4076f | 525 | #endif |
6c444ade | 526 | if (!(name = malloc(name_len))) { |
dc422c04 | 527 | SET_ERROR(ret, SP_ERR_MEM, "Registry port name malloc failed"); |
6c444ade AJ |
528 | goto out; |
529 | } | |
48a4076f | 530 | #ifdef UNICODE |
6c444ade | 531 | WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL); |
48a4076f | 532 | #else |
6c444ade | 533 | strcpy(name, data); |
48a4076f | 534 | #endif |
d38c7d2c | 535 | DEBUG_FMT("Found port %s", name); |
48a4076f | 536 | if (!(*list = list_append(*list, name))) { |
dc422c04 | 537 | SET_ERROR(ret, SP_ERR_MEM, "List append failed"); |
7aeb6736 | 538 | free(name); |
48a4076f AJ |
539 | goto out; |
540 | } | |
7aeb6736 | 541 | free(name); |
48a4076f AJ |
542 | } |
543 | index++; | |
544 | } | |
545 | out: | |
546 | free(data); | |
547 | out_free_value: | |
548 | free(value); | |
549 | out_close: | |
550 | RegCloseKey(key); | |
551 | out_done: | |
552 | ||
553 | return ret; | |
554 | } |