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