xref: /haiku/src/kits/device/USBConfiguration.cpp (revision 4b3b81da9e459443d75329cfd08bc9a57ad02653)
1 /*
2  * Copyright 2007, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Lotz <mmlr@mlotz.ch>
7  */
8 
9 #include <USBKit.h>
10 #include <usb_raw.h>
11 #include <unistd.h>
12 #include <string.h>
13 
14 
15 BUSBConfiguration::BUSBConfiguration(BUSBDevice *device, uint32 index, int rawFD)
16 	:	fDevice(device),
17 		fIndex(index),
18 		fRawFD(rawFD),
19 		fInterfaces(NULL),
20 		fConfigurationString(NULL)
21 {
22 	usb_raw_command command;
23 	command.config.descriptor = &fDescriptor;
24 	command.config.config_index = fIndex;
25 	if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR, &command,
26 		sizeof(command)) || command.config.status != B_USB_RAW_STATUS_SUCCESS)
27 		memset(&fDescriptor, 0, sizeof(fDescriptor));
28 
29 	fInterfaces = new BUSBInterface *[fDescriptor.number_interfaces];
30 	for (uint32 i = 0; i < fDescriptor.number_interfaces; i++)
31 		fInterfaces[i] = new BUSBInterface(this, i, fRawFD);
32 }
33 
34 
35 BUSBConfiguration::~BUSBConfiguration()
36 {
37 	delete[] fConfigurationString;
38 	for (int32 i = 0; i < fDescriptor.number_interfaces; i++)
39 		delete fInterfaces[i];
40 	delete[] fInterfaces;
41 }
42 
43 
44 uint32
45 BUSBConfiguration::Index() const
46 {
47 	return fIndex;
48 }
49 
50 
51 const BUSBDevice *
52 BUSBConfiguration::Device() const
53 {
54 	return fDevice;
55 }
56 
57 
58 const char *
59 BUSBConfiguration::ConfigurationString() const
60 {
61 	if (fDescriptor.configuration == 0)
62 		return "";
63 
64 	if (fConfigurationString)
65 		return fConfigurationString;
66 
67 	fConfigurationString = Device()->DecodeStringDescriptor(fDescriptor.configuration);
68 	if (!fConfigurationString) {
69 		fConfigurationString = new char[1];
70 		fConfigurationString[0] = 0;
71 	}
72 
73 	return fConfigurationString;
74 }
75 
76 
77 const usb_configuration_descriptor *
78 BUSBConfiguration::Descriptor() const
79 {
80 	return &fDescriptor;
81 }
82 
83 
84 uint32
85 BUSBConfiguration::CountInterfaces() const
86 {
87 	return fDescriptor.number_interfaces;
88 }
89 
90 
91 const BUSBInterface *
92 BUSBConfiguration::InterfaceAt(uint32 index) const
93 {
94 	if (index >= fDescriptor.number_interfaces)
95 		return NULL;
96 
97 	return fInterfaces[index];
98 }
99