[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / peripherals / bus / win32 / PeripheralBusUSB.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "PeripheralBusUSB.h"
22 #include "peripherals/Peripherals.h"
23 #include "utils/log.h"
24 #include "utils/StringUtils.h"
25
26 static GUID USB_RAW_GUID =  { 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
27 static GUID USB_HID_GUID =  { 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
28 static GUID USB_DISK_GUID = { 0x53F56307, 0xB6BF, 0x11D0, { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B } };
29 static GUID USB_NIC_GUID =  { 0xAD498944, 0x762F, 0x11D0, { 0x8D, 0xCB, 0x00, 0xC0, 0x4F, 0xC3, 0x35, 0x8C } };
30
31 using namespace PERIPHERALS;
32
33 CPeripheralBusUSB::CPeripheralBusUSB(CPeripherals *manager) :
34     CPeripheralBus(manager, PERIPHERAL_BUS_USB)
35 {
36   /* device removals aren't always triggering OnDeviceRemoved events, so poll for changes every 5 seconds to be sure we don't miss anything */
37   m_iRescanTime = 5000;
38 }
39
40 bool CPeripheralBusUSB::PerformDeviceScan(PeripheralScanResults &results)
41 {
42   /* XXX we'll just scan the RAW guid and find all devices. they'll show up as type 'unknown' in the UI,
43      but the other option is that they're detected more than once, because RAW will return all devices.
44      we have to scan the RAW guid here, because not every device is found by it's GUID correctly, e.g. CDC adapters. */
45   return PerformDeviceScan(&USB_RAW_GUID, PERIPHERAL_UNKNOWN, results);
46 }
47
48 bool CPeripheralBusUSB::PerformDeviceScan(const GUID *guid, const PeripheralType type, PeripheralScanResults &results)
49 {
50   bool     bReturn(false);
51   HDEVINFO hDevHandle;
52   DWORD    required = 0, iMemberIndex = 0;
53   int      nBufferSize = 0;
54
55   SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
56   deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
57
58   SP_DEVINFO_DATA devInfoData;
59   devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
60
61   if ((hDevHandle = SetupDiGetClassDevs(guid, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)) == INVALID_HANDLE_VALUE)
62   {
63     CLog::Log(LOGWARNING, "%s - cannot query USB devices: invalid handle", __FUNCTION__);
64     return bReturn;
65   }
66
67   bReturn = true;
68   BOOL bResult = true;
69   PSP_DEVICE_INTERFACE_DETAIL_DATA devicedetailData = NULL;
70   while(bResult)
71   {
72     bResult = SetupDiEnumDeviceInfo(hDevHandle, iMemberIndex, &devInfoData);
73
74     if (bResult)
75       bResult = SetupDiEnumDeviceInterfaces(hDevHandle, 0, guid, iMemberIndex, &deviceInterfaceData);
76
77     if (bResult)
78     {
79       iMemberIndex++;
80       BOOL bDetailResult = false;
81       {
82         // As per MSDN, Get the required buffer size. Call SetupDiGetDeviceInterfaceDetail with a 
83         // NULL DeviceInterfaceDetailData pointer, a DeviceInterfaceDetailDataSize of zero, 
84         // and a valid RequiredSize variable. In response to such a call, this function returns 
85         // the required buffer size at RequiredSize and fails with GetLastError returning 
86         // ERROR_INSUFFICIENT_BUFFER. 
87         // Allocate an appropriately sized buffer and call the function again to get the interface details. 
88
89         SetupDiGetDeviceInterfaceDetail(hDevHandle, &deviceInterfaceData, NULL, 0, &required, NULL);
90
91         devicedetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(required * sizeof(TCHAR));
92         devicedetailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
93         nBufferSize = required;
94       }
95
96       bDetailResult = SetupDiGetDeviceInterfaceDetail(hDevHandle, &deviceInterfaceData, devicedetailData, nBufferSize , &required, NULL);
97       if (bDetailResult)
98       {
99         CStdString strVendorId(StringUtils::EmptyString);
100         CStdString strProductId(StringUtils::EmptyString);
101         CStdString strTmp(devicedetailData->DevicePath);
102         strVendorId = strTmp.substr(strTmp.Find("vid_") + 4, 4);
103         strProductId = strTmp.substr(strTmp.Find("pid_") + 4, 4);
104
105         if ((strTmp.Find("&mi_") < 0) || (strTmp.Find("&mi_00") >= 0))
106         {
107           PeripheralScanResult prevDevice;
108           if (!results.GetDeviceOnLocation(devicedetailData->DevicePath, &prevDevice))
109           {
110             PeripheralScanResult result;
111             result.m_strLocation  = devicedetailData->DevicePath;
112             result.m_type         = type;
113             result.m_iVendorId    = PeripheralTypeTranslator::HexStringToInt(strVendorId.c_str());
114             result.m_iProductId   = PeripheralTypeTranslator::HexStringToInt(strProductId.c_str());
115
116             if (!results.ContainsResult(result))
117               results.m_results.push_back(result);
118           }
119         }
120       }
121
122       if (devicedetailData)
123       {
124         free(devicedetailData);
125         devicedetailData = NULL;
126       } 
127     }
128     else
129     {
130       SetupDiDestroyDeviceInfoList(hDevHandle);
131       return bReturn;
132     }
133   }
134
135   return bReturn;
136 }