ec4e9b4a4b97277c5496cb4ac9760631a3856b45
[vuplus_xbmc] / xbmc / peripherals / bus / linux / PeripheralBusUSBLibUSB.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 "PeripheralBusUSBLibUSB.h"
22 #include "peripherals/Peripherals.h"
23 #include <usb.h>
24 #include "utils/log.h"
25
26 using namespace PERIPHERALS;
27
28 CPeripheralBusUSB::CPeripheralBusUSB(CPeripherals *manager) :
29     CPeripheralBus("PeripBusUSB", manager, PERIPHERAL_BUS_USB)
30 {
31   usb_init();
32   usb_find_busses();
33   m_busses = usb_get_busses();
34   CLog::Log(LOGDEBUG, "%s - using libusb peripheral scanning", __FUNCTION__);
35 }
36
37 bool CPeripheralBusUSB::PerformDeviceScan(PeripheralScanResults &results)
38 {
39   struct usb_bus *bus;
40   usb_find_devices();
41   for (bus = m_busses; bus; bus = bus->next)
42   {
43     struct usb_device *dev;
44     for (dev = bus->devices; dev; dev = dev->next)
45     {
46       PeripheralScanResult result(m_type);
47       result.m_iVendorId  = dev->descriptor.idVendor;
48       result.m_iProductId = dev->descriptor.idProduct;
49       result.m_type       = (dev->descriptor.bDeviceClass == USB_CLASS_PER_INTERFACE && dev->descriptor.bNumConfigurations > 0 &&
50                              dev->config[0].bNumInterfaces > 0 && dev->config[0].interface[0].num_altsetting > 0) ?
51                                  GetType(dev->config[0].interface[0].altsetting[0].bInterfaceClass) :
52                                  GetType(dev->descriptor.bDeviceClass);
53 #ifdef TARGET_FREEBSD
54       result.m_strLocation.Format("%s", dev->filename);
55 #else
56       result.m_strLocation.Format("/bus%s/dev%s", bus->dirname, dev->filename);
57 #endif
58       result.m_iSequence   = GetNumberOfPeripheralsWithId(result.m_iVendorId, result.m_iProductId);
59       if (!results.ContainsResult(result))
60         results.m_results.push_back(result);
61     }
62   }
63
64   return true;
65 }
66
67 const PeripheralType CPeripheralBusUSB::GetType(int iDeviceClass)
68 {
69   switch (iDeviceClass)
70   {
71   case USB_CLASS_HID:
72     return PERIPHERAL_HID;
73   case USB_CLASS_COMM:
74     return PERIPHERAL_NIC;
75   case USB_CLASS_MASS_STORAGE:
76     return PERIPHERAL_DISK;
77   case USB_CLASS_PER_INTERFACE:
78   case USB_CLASS_AUDIO:
79   case USB_CLASS_PRINTER:
80   case USB_CLASS_PTP:
81   case USB_CLASS_HUB:
82   case USB_CLASS_DATA:
83   case USB_CLASS_VENDOR_SPEC:
84   default:
85     return PERIPHERAL_UNKNOWN;
86   }
87 }