summaryrefslogtreecommitdiff
path: root/libusb/os/driver/driver_registry.c
blob: aa4b5d22db5b1e631c29520e8756d91542be371e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* LIBUSB-WIN32, Generic Windows USB Library
 * Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "libusb_driver.h"

/* missing in mingw's ddk headers */
#ifndef PLUGPLAY_REGKEY_DEVICE
#define PLUGPLAY_REGKEY_DEVICE  1
#endif

#define LIBUSB_REG_SURPRISE_REMOVAL_OK L"SurpriseRemovalOK"


static bool_t reg_get_property(DEVICE_OBJECT *physical_device_object, 
                               int property, char *data, int size);

static bool_t reg_get_property(DEVICE_OBJECT *physical_device_object,
                               int property, char *data, int size)
{
  WCHAR tmp[512];
  ULONG ret;
  ULONG i;

  if(!physical_device_object || !data || !size)
    {
      return FALSE;
    }

  memset(data, 0, size);
  memset(tmp, 0, sizeof(tmp));

  if(NT_SUCCESS(IoGetDeviceProperty(physical_device_object,
                                    property,
                                    sizeof(tmp) - 2,
                                    tmp,
                                    &ret)) && ret)
    {
      /* convert unicode string to normal character string */
      for(i = 0; (i < ret/2) && (i < ((ULONG)size - 1)); i++)
        {
          data[i] = (char)tmp[i];
        }
      
      _strlwr(data);

      return TRUE;
    }

  return FALSE;
}


bool_t reg_get_properties(libusb_device_t *dev)
{
  HANDLE key = NULL;
  NTSTATUS status;
  UNICODE_STRING name;
  KEY_VALUE_FULL_INFORMATION *info;
  ULONG length;

  if(!dev->physical_device_object)
    {
      return FALSE;
    }

  /* default settings */
  dev->surprise_removal_ok = FALSE;
  dev->is_filter = TRUE;

  status = IoOpenDeviceRegistryKey(dev->physical_device_object,
                                   PLUGPLAY_REGKEY_DEVICE,
                                   STANDARD_RIGHTS_ALL,
                                   &key);
  if(NT_SUCCESS(status)) 
    {
      RtlInitUnicodeString(&name, LIBUSB_REG_SURPRISE_REMOVAL_OK);
      
      length = sizeof(KEY_VALUE_FULL_INFORMATION) + name.MaximumLength
        + sizeof(ULONG);

      info = ExAllocatePool(NonPagedPool, length);
      
      if(info) 
        {
          memset(info, 0, length);

          status = ZwQueryValueKey(key, &name, KeyValueFullInformation,
                                   info, length, &length);
          
          if(NT_SUCCESS(status) && (info->Type == REG_DWORD))
            {
              ULONG val = *((ULONG *)(((char *)info) + info->DataOffset));

              dev->surprise_removal_ok = val ? TRUE : FALSE;
              dev->is_filter = FALSE;
            }
          
          ExFreePool(info);
        }
      
      ZwClose(key);
    }

  return TRUE;
}

bool_t reg_get_hardware_id(DEVICE_OBJECT *physical_device_object, 
                           char *data, int size)
{
  if(!physical_device_object || !data || !size)
    {
      return FALSE;
    }

  return reg_get_property(physical_device_object, DevicePropertyHardwareID, 
                          data, size);
}