Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UsbDevice.OS.Specific.cs « LibWinUsb - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f8120d0ff9ecacc9816302c99001aa1933abef1e (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright © 2006-2010 Travis Robinson. All rights reserved.
// 
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail:  libusbdotnet@gmail.com
// 
// 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. or 
// visit www.gnu.org.
// 
// 
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using LibUsbDotNet.Internal.LibUsb;
using LibUsbDotNet.LibUsb;
using LibUsbDotNet.Main;
using LibUsbDotNet.LudnMonoLibUsb;
using LibUsbDotNet.WinUsb;
using MonoLibUsb;

namespace LibUsbDotNet
{
    public abstract partial class UsbDevice
    {
        /// <summary>
        /// 
        /// </summary>
        private static readonly bool ForceLegacyLibUsb = IsLinux;

        /// <summary>
        /// Setting this field to <see langword="true"/> will force <see cref="LibUsbDotNet"/> to use the <a href="http://www.libusb.org/wiki/windows_backend">Libusb-1.0 Windows-backend driver.</a>  For platforms other than windows, this setting has no effect.
        /// </summary>
        /// <remarks>
        /// If this is <see langword="true"/>, <see cref="AllDevices"/> will return only <see cref="MonoUsbDevice"/>s in the list.
        /// </remarks>
        public static bool ForceLibUsbWinBack = false;
        

        /// <summary>
        /// Gets a list of all available WinUSB USB devices.
        /// </summary>
        /// <remarks>
        /// On windows, gets a list of WinUSB devices. On linux always returns null.
        /// <para>
        /// Using the <see cref="AllDevices"/> property instead will ensure your source code is platform-independent.
        /// </para>
        /// </remarks>
        public static UsbRegDeviceList AllWinUsbDevices
        {
            get
            {
                UsbRegDeviceList regDevList = new UsbRegDeviceList();
                if (IsLinux || ForceLibUsbWinBack) return regDevList;

                if (HasWinUsbDriver)
                {
                    List<WinUsbRegistry> winUsbRegistry = WinUsbRegistry.DeviceList;
                    foreach (WinUsbRegistry usbRegistry in winUsbRegistry)
                        regDevList.Add(usbRegistry);
                }

                return regDevList;
            }
        }

        /// <summary>
        /// True if the LibUsb driver is found on the system.
        /// </summary>
        [Obsolete("Always returns true")]
        public static bool HasLibUsbDriver
        {
            get
            {
                return true;
            }
        }
        /*
        public static bool HasLibUsbDriver
        {
            get
            {
                if (mHasLibUsbDriver == null)
                {
                    if (IsLinux)
                    {
                        mHasLibUsbDriver = true;
                    }
                    else
                    {
                        mHasLibUsbDriver = false;
                        string sysDriver = Path.Combine(Environment.SystemDirectory, "drivers");
                        DirectoryInfo diSysDriver = new DirectoryInfo(sysDriver);
                        if (diSysDriver.Exists)
                        {
                            FileInfo[] libUsbSysFileInfo = diSysDriver.GetFiles(LIBUSB_SYS);
                            if (libUsbSysFileInfo.Length > 0)
                                mHasLibUsbDriver = true;
                        }
                    }
                }
                return (bool) mHasLibUsbDriver;
            }
        }
        */

        /// <summary>
        /// True if the WinUSB API is available.
        /// </summary>
        /// <remarks>
        /// </remarks>
        public static bool HasWinUsbDriver
        {
            get
            {
                if (mHasWinUsbDriver == null)
                {
                    if (IsLinux)
                    {
                        mHasWinUsbDriver = false;
                    }
                    else
                    {
                        try
                        {
                            WinUsb.Internal.WinUsbAPI.WinUsb_Free(IntPtr.Zero);
                            mHasWinUsbDriver = true;
                        }
                        catch (Exception)
                        {
                            mHasWinUsbDriver = false;

                        }
                    }
                }
                return (bool) mHasWinUsbDriver;
            }
        }

        /// <summary>
        /// True if the libusb-1.0 API is available.
        /// </summary>
        public static bool HasLibUsbWinBackDriver
        {
            get
            {
                if (mHasLibUsbWinBackDriver == null)
                {
                    if (IsLinux)
                    {
                        mHasLibUsbWinBackDriver = false;
                    }
                    else
                    {
                        try
                        {
                            MonoUsbApi.StrError(MonoUsbError.Success);
                            mHasLibUsbWinBackDriver = true;
                        }
                        catch(Exception)
                        {
                            mHasLibUsbWinBackDriver = false;
                          
                        }
                    }
                }
                return (bool)mHasLibUsbWinBackDriver;
            }
        }
        ///<summary>
        /// Returns true if the system is a linux/unix-like operating system. 
        ///</summary>
        ///<exception cref="NotSupportedException"></exception>
        public static bool IsLinux
        {
            get
            {
                return Helper.IsLinux;

            }
        }

        /// <summary>
        /// Gets the kernel driver type in use by LibUsbDotNet. 
        /// If <see cref="LibUsbKernelType.NativeLibUsb"/> is returned, LibUsbDotNet using using its
        /// native kernel driver.  Basic usb device information is retreived from the windows registry
        /// which reduces USB IO overhead. 
        /// If <see cref="LibUsbKernelType.LegacyLibUsb"/> is returned, LibUsbDotNet is using the orginal kernel
        /// available at the libusb-win32.sourceforge.net page and true windows registry support
        /// is unavailable.
        /// Under linux, <see cref="LibUsbKernelType.MonoLibUsb"/> is always returned.
        /// </summary>
        public static LibUsbKernelType KernelType
        {
            get
            {
                if (mLibUsbKernelType == LibUsbKernelType.Unknown)
                {
                    if (IsLinux)
                    {
                        mLibUsbKernelType = LibUsbKernelType.MonoLibUsb;
                    }
                    else
                    {
                        UsbKernelVersion libUsbVersion = KernelVersion;
                        if (!libUsbVersion.IsEmpty)
                        {
                            mLibUsbKernelType = libUsbVersion.BcdLibUsbDotNetKernelMod != 0
                                                    ? LibUsbKernelType.NativeLibUsb
                                                    : LibUsbKernelType.LegacyLibUsb;
                        }
                    }
                }

                return mLibUsbKernelType;
            }
        }

        /// <summary>
        /// Gets the kernel driver version in use by LibUsbDotNet.
        /// <alert class="note"><para>
        /// if <see cref="UsbKernelVersion.BcdLibUsbDotNetKernelMod"/> is non-zero then the kernel driver is native.
        /// </para></alert>
        /// </summary>
        public static UsbKernelVersion KernelVersion
        {
            get
            {
                if (mUsbKernelVersion.IsEmpty)
                {
                    if (IsLinux)
                    {
                        mUsbKernelVersion = new UsbKernelVersion(1, 0, 0, 0, 0);
                    }
                    else
                    {
                        for (int i = 1; i < UsbConstants.MAX_DEVICES; i++)
                        {
                            LibUsbDevice newLibUsbDevice;
                            string deviceFileName = LibUsbDriverIO.GetDeviceNameString(i);
                            if (!LibUsbDevice.Open(deviceFileName, out newLibUsbDevice)) continue;
                            LibUsbRequest request = new LibUsbRequest();
                            GCHandle gcReq = GCHandle.Alloc(request, GCHandleType.Pinned);

                            int transferred;
                            bool bSuccess = newLibUsbDevice.UsbIoSync(LibUsbIoCtl.GET_VERSION,
                                                                      request,
                                                                      LibUsbRequest.Size,
                                                                      gcReq.AddrOfPinnedObject(),
                                                                      LibUsbRequest.Size,
                                                                      out transferred);

                            gcReq.Free();
                            newLibUsbDevice.Close();
                            if (bSuccess && transferred == LibUsbRequest.Size)
                            {
                                mUsbKernelVersion = request.Version;
                                break;
                            }
                        }
                    }
                }

                return mUsbKernelVersion;
            }
        }


        ///<summary>
        /// Gets a <see cref="System.OperatingSystem"/> object that contains the current platform identifier and version number.
        ///</summary>
        public static OperatingSystem OSVersion
        {
            get
            {
                return Helper.OSVersion;
            }
        }
    }
}