]> sigrok.org Git - libserialport.git/blob - windows.c
Fix typos.
[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                 /* enumerate 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                         RegCloseKey(device_key);
364                         continue;
365                 }
366                 RegCloseKey(device_key);
367                 value[sizeof(value)-1] = 0;
368                 if (strcmp(value, port->name))
369                         continue;
370
371                 /* check port transport type */
372                 dev_inst = device_info_data.DevInst;
373                 size = sizeof(class);
374                 cr = CR_FAILURE;
375                 while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS &&
376                        (cr = CM_Get_DevNode_Registry_PropertyA(dev_inst,
377                                  CM_DRP_CLASS, 0, class, &size, 0)) != CR_SUCCESS) { }
378                 if (cr == CR_SUCCESS) {
379                         if (!strcmp(class, "USB"))
380                                 port->transport = SP_TRANSPORT_USB;
381                 }
382
383                 /* get port description (friendly name) */
384                 dev_inst = device_info_data.DevInst;
385                 size = sizeof(description);
386                 while ((cr = CM_Get_DevNode_Registry_PropertyA(dev_inst,
387                           CM_DRP_FRIENDLYNAME, 0, description, &size, 0)) != CR_SUCCESS
388                        && CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) { }
389                 if (cr == CR_SUCCESS)
390                         port->description = strdup(description);
391
392                 /* get more informations for USB connected ports */
393                 if (port->transport == SP_TRANSPORT_USB) {
394                         char usb_path[MAX_USB_PATH] = "", tmp[MAX_USB_PATH];
395                         char device_id[MAX_DEVICE_ID_LEN];
396
397                         /* recurse over parents to build the USB device path */
398                         dev_inst = device_info_data.DevInst;
399                         do {
400                                 /* verify that this layer of the tree is USB related */
401                                 if (CM_Get_Device_IDA(dev_inst, device_id,
402                                                       sizeof(device_id), 0) != CR_SUCCESS
403                                     || strncmp(device_id, "USB\\", 4))
404                                         continue;
405
406                                 /* discard one layer for composite devices */
407                                 char compat_ids[512], *p = compat_ids;
408                                 size = sizeof(compat_ids);
409                                 if (CM_Get_DevNode_Registry_PropertyA(dev_inst,
410                                                                       CM_DRP_COMPATIBLEIDS, 0,
411                                                                       &compat_ids,
412                                                                       &size, 0) == CR_SUCCESS) {
413                                         while (*p) {
414                                                 if (!strncmp(p, "USB\\COMPOSITE", 13))
415                                                         break;
416                                                 p += strlen(p) + 1;
417                                         }
418                                         if (*p)
419                                                 continue;
420                                 }
421
422                                 /* stop the recursion when reaching the USB root */
423                                 if (!strncmp(device_id, "USB\\ROOT", 8))
424                                         break;
425
426                                 /* prepend the address of current USB layer to the USB path */
427                                 DWORD address;
428                                 size = sizeof(address);
429                                 if (CM_Get_DevNode_Registry_PropertyA(dev_inst, CM_DRP_ADDRESS,
430                                                         0, &address, &size, 0) == CR_SUCCESS) {
431                                         strcpy(tmp, usb_path);
432                                         snprintf(usb_path, sizeof(usb_path), "%d%s%s",
433                                                  (int)address, *tmp ? "." : "", tmp);
434                                 }
435                         } while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS);
436
437                         port->usb_path = strdup(usb_path);
438
439                         /* wake up the USB device to be able to read string descriptor */
440                         char *escaped_port_name;
441                         HANDLE handle;
442                         if (!(escaped_port_name = malloc(strlen(port->name) + 5)))
443                                 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
444                         sprintf(escaped_port_name, "\\\\.\\%s", port->name);
445                         handle = CreateFile(escaped_port_name, GENERIC_READ, 0, 0,
446                                             OPEN_EXISTING,
447                                             FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
448                         free(escaped_port_name);
449                         CloseHandle(handle);
450
451                         /* retrieve USB device details from the device descriptor */
452                         get_usb_details(port, device_info_data.DevInst);
453                 }
454                 break;
455         }
456
457         SetupDiDestroyDeviceInfoList(device_info);
458
459         RETURN_OK();
460 }
461
462 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
463 {
464         HKEY key;
465         TCHAR *value, *data;
466         DWORD max_value_len, max_data_size, max_data_len;
467         DWORD value_len, data_size, data_len;
468         DWORD type, index = 0;
469         char *name;
470         int name_len;
471         int ret = SP_OK;
472
473         DEBUG("Opening registry key");
474         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
475                         0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
476                 SET_FAIL(ret, "RegOpenKeyEx() failed");
477                 goto out_done;
478         }
479         DEBUG("Querying registry key value and data sizes");
480         if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
481                                 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
482                 SET_FAIL(ret, "RegQueryInfoKey() failed");
483                 goto out_close;
484         }
485         max_data_len = max_data_size / sizeof(TCHAR);
486         if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
487                 SET_ERROR(ret, SP_ERR_MEM, "registry value malloc failed");
488                 goto out_close;
489         }
490         if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
491                 SET_ERROR(ret, SP_ERR_MEM, "registry data malloc failed");
492                 goto out_free_value;
493         }
494         DEBUG("Iterating over values");
495         while (
496                 value_len = max_value_len + 1,
497                 data_size = max_data_size,
498                 RegEnumValue(key, index, value, &value_len,
499                         NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
500         {
501                 if (type == REG_SZ) {
502                         data_len = data_size / sizeof(TCHAR);
503                         data[data_len] = '\0';
504 #ifdef UNICODE
505                         name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL);
506 #else
507                         name_len = data_len + 1;
508 #endif
509                         if (!(name = malloc(name_len))) {
510                                 SET_ERROR(ret, SP_ERR_MEM, "registry port name malloc failed");
511                                 goto out;
512                         }
513 #ifdef UNICODE
514                         WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
515 #else
516                         strcpy(name, data);
517 #endif
518                         DEBUG_FMT("Found port %s", name);
519                         if (!(*list = list_append(*list, name))) {
520                                 SET_ERROR(ret, SP_ERR_MEM, "list append failed");
521                                 free(name);
522                                 goto out;
523                         }
524                         free(name);
525                 }
526                 index++;
527         }
528 out:
529         free(data);
530 out_free_value:
531         free(value);
532 out_close:
533         RegCloseKey(key);
534 out_done:
535
536         return ret;
537 }