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#include <bus/USB.h>
17
18#undef B_TRANSLATION_CONTEXT
19#define B_TRANSLATION_CONTEXT "DeviceUSB"
20
21extern "C" {
22#include "dm_wrapper.h"
23#include "usb-utils.h"
24}
25
26
27DeviceUSB::DeviceUSB(Device* parent)
28	:
29	Device(parent),
30	fVendorId(0),
31	fDeviceId(0)
32{
33}
34
35
36DeviceUSB::~DeviceUSB()
37{
38}
39
40
41static BString ToHex(uint16 num)
42{
43	std::stringstream ss;
44	ss.flags(std::ios::hex | std::ios::showbase);
45	ss << num;
46	return BString(ss.str().c_str());
47}
48
49
50void
51DeviceUSB::InitFromAttributes()
52{
53	// Process the attributes
54	fClassBaseId = atoi(fAttributeMap[USB_DEVICE_CLASS].String());
55	fClassSubId = atoi(fAttributeMap[USB_DEVICE_SUBCLASS].String());
56	fClassProtoId = atoi(fAttributeMap[USB_DEVICE_PROTOCOL].String());
57	fVendorId = atoi(fAttributeMap[B_DEVICE_VENDOR_ID].String());
58	fDeviceId = atoi(fAttributeMap[B_DEVICE_ID].String());
59
60	// Looks better in Hex, so rewrite
61	fAttributeMap[USB_DEVICE_CLASS] = ToHex(fClassBaseId);
62	fAttributeMap[USB_DEVICE_SUBCLASS] = ToHex(fClassSubId);
63	fAttributeMap[USB_DEVICE_PROTOCOL] = ToHex(fClassProtoId);
64	fAttributeMap[B_DEVICE_VENDOR_ID] = ToHex(fVendorId);
65	fAttributeMap[B_DEVICE_ID] = ToHex(fDeviceId);
66
67	// Fetch ClassInfo
68	char classInfo[128];
69	usb_get_class_info(fClassBaseId, fClassSubId, fClassProtoId, classInfo,
70		sizeof(classInfo));
71
72	// Fetch ManufacturerName
73	const char* vendorName = NULL;
74	const char* deviceName = NULL;
75	BString deviceLabel;
76	BString manufacturerLabel;
77	usb_get_vendor_info(fVendorId, &vendorName);
78	usb_get_device_info(fVendorId, fDeviceId, &deviceName);
79	if (vendorName == NULL) {
80		manufacturerLabel << B_TRANSLATE("Unknown");
81	} else {
82		manufacturerLabel << vendorName;
83	};
84
85	// Fetch DeviceName
86	if (deviceName == NULL) {
87		deviceLabel << B_TRANSLATE("Unknown");
88	} else {
89		deviceLabel << deviceName;
90	}
91
92	SetAttribute(B_TRANSLATE("Device name"), deviceLabel);
93	SetAttribute(B_TRANSLATE("Manufacturer"), manufacturerLabel);
94#if 0
95	// These are a source of confusion for users, leading them to think there
96	// is no driver for their device. Until we can display something useful,
97	// let's not show the lines at all.
98	SetAttribute(B_TRANSLATE("Driver used"), B_TRANSLATE("Not implemented"));
99	SetAttribute(B_TRANSLATE("Device paths"), B_TRANSLATE("Not implemented"));
100#endif
101	SetAttribute(B_TRANSLATE("Class info"), classInfo);
102	switch (fClassBaseId) {
103		case 0x1:
104			fCategory = CAT_MULTIMEDIA; break;
105		case 0x2:
106			fCategory = CAT_COMM; break;
107		case 0x3:
108			fCategory = CAT_INPUT; break;
109		case 0x6:	// Imaging
110			fCategory = CAT_MULTIMEDIA; break;
111		case 0x7:	// Printer
112			fCategory = CAT_MULTIMEDIA; break;
113		case 0x8:
114			fCategory = CAT_MASS; break;
115		case 0x9:
116			fCategory = CAT_GENERIC; break;
117		case 0xa:
118			fCategory = CAT_COMM; break;
119		case 0xe:	// Webcam
120			fCategory = CAT_MULTIMEDIA; break;
121		case 0xe0:
122			fCategory = CAT_WIRELESS; break;
123	}
124	BString outlineName;
125	outlineName << manufacturerLabel << " " << deviceLabel;
126	SetText(outlineName.String());
127}
128