1 /* 2 * Copyright 2008-2009 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Pieter Panman 7 */ 8 9 10 #include "DeviceUSB.h" 11 12 #include <sstream> 13 #include <stdlib.h> 14 15 #include <Catalog.h> 16 17 #undef B_TRANSLATION_CONTEXT 18 #define B_TRANSLATION_CONTEXT "DeviceUSB" 19 20 extern "C" { 21 #include "dm_wrapper.h" 22 #include "usb-utils.h" 23 } 24 25 // from usb_private.h 26 #define USB_DEVICE_CLASS "usb/class" 27 #define USB_DEVICE_SUBCLASS "usb/subclass" 28 #define USB_DEVICE_PROTOCOL "usb/protocol" 29 30 31 32 DeviceUSB::DeviceUSB(Device* parent) 33 : 34 Device(parent), 35 fVendorId(0), 36 fDeviceId(0) 37 { 38 } 39 40 41 DeviceUSB::~DeviceUSB() 42 { 43 } 44 45 46 static BString ToHex(uint16 num) 47 { 48 std::stringstream ss; 49 ss.flags(std::ios::hex | std::ios::showbase); 50 ss << num; 51 return BString(ss.str().c_str()); 52 } 53 54 55 void 56 DeviceUSB::InitFromAttributes() 57 { 58 // Process the attributes 59 fClassBaseId = atoi(fAttributeMap[USB_DEVICE_CLASS].String()); 60 fClassSubId = atoi(fAttributeMap[USB_DEVICE_SUBCLASS].String()); 61 fClassProtoId = atoi(fAttributeMap[USB_DEVICE_PROTOCOL].String()); 62 fVendorId = atoi(fAttributeMap[B_DEVICE_VENDOR_ID].String()); 63 fDeviceId = atoi(fAttributeMap[B_DEVICE_ID].String()); 64 65 // Looks better in Hex, so rewrite 66 fAttributeMap[USB_DEVICE_CLASS] = ToHex(fClassBaseId); 67 fAttributeMap[USB_DEVICE_SUBCLASS] = ToHex(fClassSubId); 68 fAttributeMap[USB_DEVICE_PROTOCOL] = ToHex(fClassProtoId); 69 fAttributeMap[B_DEVICE_VENDOR_ID] = ToHex(fVendorId); 70 fAttributeMap[B_DEVICE_ID] = ToHex(fDeviceId); 71 72 // Fetch ClassInfo 73 char classInfo[128]; 74 usb_get_class_info(fClassBaseId, fClassSubId, fClassProtoId, classInfo, 75 sizeof(classInfo)); 76 77 // Fetch ManufacturerName 78 const char* vendorName = NULL; 79 const char* deviceName = NULL; 80 BString deviceLabel; 81 BString manufacturerLabel; 82 usb_get_vendor_info(fVendorId, &vendorName); 83 usb_get_device_info(fVendorId, fDeviceId, &deviceName); 84 if (vendorName == NULL) { 85 manufacturerLabel << B_TRANSLATE("Unknown"); 86 } else { 87 manufacturerLabel << vendorName; 88 }; 89 90 // Fetch DeviceName 91 if (deviceName == NULL) { 92 deviceLabel << B_TRANSLATE("Unknown"); 93 } else { 94 deviceLabel << deviceName; 95 } 96 97 SetAttribute(B_TRANSLATE("Device name"), deviceLabel); 98 SetAttribute(B_TRANSLATE("Manufacturer"), manufacturerLabel); 99 #if 0 100 // These are a source of confusion for users, leading them to think there 101 // is no driver for their device. Until we can display something useful, 102 // let's not show the lines at all. 103 SetAttribute(B_TRANSLATE("Driver used"), B_TRANSLATE("Not implemented")); 104 SetAttribute(B_TRANSLATE("Device paths"), B_TRANSLATE("Not implemented")); 105 #endif 106 SetAttribute(B_TRANSLATE("Class info"), classInfo); 107 switch (fClassBaseId) { 108 case 0x1: 109 fCategory = CAT_MULTIMEDIA; break; 110 case 0x2: 111 fCategory = CAT_COMM; break; 112 case 0x3: 113 fCategory = CAT_INPUT; break; 114 case 0x6: // Imaging 115 fCategory = CAT_MULTIMEDIA; break; 116 case 0x7: // Printer 117 fCategory = CAT_MULTIMEDIA; break; 118 case 0x8: 119 fCategory = CAT_MASS; break; 120 case 0x9: 121 fCategory = CAT_GENERIC; break; 122 case 0xa: 123 fCategory = CAT_COMM; break; 124 case 0xe: // Webcam 125 fCategory = CAT_MULTIMEDIA; break; 126 case 0xe0: 127 fCategory = CAT_WIRELESS; break; 128 } 129 BString outlineName; 130 outlineName << manufacturerLabel << " " << deviceLabel; 131 SetText(outlineName.String()); 132 } 133 134 135 Attributes 136 DeviceUSB::GetBusAttributes() 137 { 138 Attributes attributes; 139 attributes.push_back(GetAttribute(B_DEVICE_VENDOR_ID)); 140 attributes.push_back(GetAttribute(B_DEVICE_ID)); 141 attributes.push_back(GetAttribute(USB_DEVICE_CLASS)); 142 attributes.push_back(GetAttribute(USB_DEVICE_SUBCLASS)); 143 attributes.push_back(GetAttribute(USB_DEVICE_PROTOCOL)); 144 return attributes; 145 } 146 147 148 BString 149 DeviceUSB::GetBusStrings() 150 { 151 BString str("Class Info:\t\t\t\t: %classInfo%"); 152 str.ReplaceFirst("%classInfo%", fAttributeMap["Class Info"]); 153 return str; 154 } 155 156 157 BString 158 DeviceUSB::GetBusTabName() 159 { 160 return B_TRANSLATE("USB Information"); 161 } 162 163