Skip to content

Commit 32eda57

Browse files
committed
Using prefered language ID when getting strings
1 parent fb469d1 commit 32eda57

File tree

3 files changed

+108
-15
lines changed

3 files changed

+108
-15
lines changed

uspi/include/uspi/usbstring.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ void USBString (TUSBString *pThis, struct TUSBDevice *pDevice);
4444
void USBStringCopy (TUSBString *pThis, TUSBString *pParent);
4545
void _USBString (TUSBString *pThis);
4646

47-
boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID);
47+
boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID, u16 usLanguageID);
4848

4949
const char *USBStringGet (TUSBString *pThis);
5050

51+
u16 USBStringGetLanguageID (TUSBString *pThis);
52+
5153
#ifdef __cplusplus
5254
}
5355
#endif

uspi/lib/usbdevice.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,22 @@ boolean USBDeviceInitialize (TUSBDevice *pThis)
232232

233233
USBDeviceSetAddress (pThis, ucAddress);
234234

235-
if (pThis->m_pDeviceDesc->iManufacturer != 0)
235+
if ( pThis->m_pDeviceDesc->iManufacturer != 0
236+
|| pThis->m_pDeviceDesc->iProduct != 0)
236237
{
237-
USBStringGetFromDescriptor (&pThis->m_ManufacturerString, pThis->m_pDeviceDesc->iManufacturer);
238-
}
238+
u16 usLanguageID = USBStringGetLanguageID (&pThis->m_ManufacturerString);
239239

240-
if (pThis->m_pDeviceDesc->iProduct != 0)
241-
{
242-
USBStringGetFromDescriptor (&pThis->m_ProductString, pThis->m_pDeviceDesc->iProduct);
240+
if (pThis->m_pDeviceDesc->iManufacturer != 0)
241+
{
242+
USBStringGetFromDescriptor (&pThis->m_ManufacturerString,
243+
pThis->m_pDeviceDesc->iManufacturer, usLanguageID);
244+
}
245+
246+
if (pThis->m_pDeviceDesc->iProduct != 0)
247+
{
248+
USBStringGetFromDescriptor (&pThis->m_ProductString,
249+
pThis->m_pDeviceDesc->iProduct, usLanguageID);
250+
}
243251
}
244252

245253
assert (pThis->m_pConfigDesc == 0);

uspi/lib/usbstring.c

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include <uspi/assert.h>
2525
#include <uspios.h>
2626

27-
#define USBSTR_MIN_LENGTH 16
27+
#define USBSTR_MIN_LENGTH 4
28+
29+
#define USBSTR_DEFAULT_LANGID 0x409
2830

2931
void USBString (TUSBString *pThis, struct TUSBDevice *pDevice)
3032
{
@@ -78,7 +80,7 @@ void _USBString (TUSBString *pThis)
7880
pThis->m_pDevice = 0;
7981
}
8082

81-
boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID)
83+
boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID, u16 usLanguageID)
8284
{
8385
assert (pThis != 0);
8486
assert (ucID > 0);
@@ -91,9 +93,11 @@ boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID)
9193
assert (pThis->m_pUSBString != 0);
9294

9395
assert (pThis->m_pDevice != 0);
94-
if (DWHCIDeviceGetDescriptor (USBDeviceGetHost (pThis->m_pDevice), USBDeviceGetEndpoint0 (pThis->m_pDevice),
95-
DESCRIPTOR_STRING, ucID,
96-
pThis->m_pUSBString, USBSTR_MIN_LENGTH, REQUEST_IN) < 0)
96+
if (DWHCIDeviceControlMessage (USBDeviceGetHost (pThis->m_pDevice),
97+
USBDeviceGetEndpoint0 (pThis->m_pDevice),
98+
REQUEST_IN, GET_DESCRIPTOR,
99+
(DESCRIPTOR_STRING << 8) | ucID, usLanguageID,
100+
pThis->m_pUSBString, USBSTR_MIN_LENGTH) < 0)
97101
{
98102
return FALSE;
99103
}
@@ -112,9 +116,11 @@ boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID)
112116
pThis->m_pUSBString = (TUSBStringDescriptor *) malloc (ucLength);
113117
assert (pThis->m_pUSBString != 0);
114118

115-
if (DWHCIDeviceGetDescriptor (USBDeviceGetHost (pThis->m_pDevice), USBDeviceGetEndpoint0 (pThis->m_pDevice),
116-
DESCRIPTOR_STRING, ucID,
117-
pThis->m_pUSBString, ucLength, REQUEST_IN) != (int) ucLength)
119+
if (DWHCIDeviceControlMessage (USBDeviceGetHost (pThis->m_pDevice),
120+
USBDeviceGetEndpoint0 (pThis->m_pDevice),
121+
REQUEST_IN, GET_DESCRIPTOR,
122+
(DESCRIPTOR_STRING << 8) | ucID, usLanguageID,
123+
pThis->m_pUSBString, ucLength) != (int) ucLength)
118124
{
119125
return FALSE;
120126
}
@@ -164,3 +170,80 @@ const char *USBStringGet (TUSBString *pThis)
164170
assert (pThis != 0);
165171
return StringGet (pThis->m_pString);
166172
}
173+
174+
u16 USBStringGetLanguageID (TUSBString *pThis)
175+
{
176+
assert (pThis != 0);
177+
178+
TUSBStringDescriptor *pLanguageIDs = (TUSBStringDescriptor *) malloc (USBSTR_MIN_LENGTH);
179+
assert (pLanguageIDs != 0);
180+
181+
assert (pThis->m_pDevice != 0);
182+
if (DWHCIDeviceGetDescriptor (USBDeviceGetHost (pThis->m_pDevice),
183+
USBDeviceGetEndpoint0 (pThis->m_pDevice),
184+
DESCRIPTOR_STRING, 0,
185+
pLanguageIDs, USBSTR_MIN_LENGTH, REQUEST_IN) < 0)
186+
{
187+
free (pLanguageIDs);
188+
189+
return USBSTR_DEFAULT_LANGID;
190+
}
191+
192+
u8 ucLength = pLanguageIDs->bLength;
193+
if ( ucLength < 4
194+
|| (ucLength & 1) != 0
195+
|| pLanguageIDs->bDescriptorType != DESCRIPTOR_STRING)
196+
{
197+
free (pLanguageIDs);
198+
199+
return USBSTR_DEFAULT_LANGID;
200+
}
201+
202+
if (ucLength > USBSTR_MIN_LENGTH)
203+
{
204+
free (pLanguageIDs);
205+
pLanguageIDs = (TUSBStringDescriptor *) malloc (ucLength);
206+
assert (pLanguageIDs != 0);
207+
208+
if (DWHCIDeviceGetDescriptor (USBDeviceGetHost (pThis->m_pDevice),
209+
USBDeviceGetEndpoint0 (pThis->m_pDevice),
210+
DESCRIPTOR_STRING, 0,
211+
pLanguageIDs, ucLength, REQUEST_IN) != (int) ucLength)
212+
{
213+
free (pLanguageIDs);
214+
215+
return USBSTR_DEFAULT_LANGID;
216+
}
217+
218+
if ( pLanguageIDs->bLength != ucLength
219+
|| (pLanguageIDs->bLength & 1) != 0
220+
|| pLanguageIDs->bDescriptorType != DESCRIPTOR_STRING)
221+
{
222+
free (pLanguageIDs);
223+
224+
return USBSTR_DEFAULT_LANGID;
225+
}
226+
}
227+
228+
assert (pLanguageIDs->bLength >= 4);
229+
assert ((pLanguageIDs->bLength & 1) == 0);
230+
size_t nLength = (pLanguageIDs->bLength-2) / 2;
231+
232+
// search for default language ID
233+
for (unsigned i = 0; i < nLength; i++)
234+
{
235+
if (pLanguageIDs->bString[i] == USBSTR_DEFAULT_LANGID)
236+
{
237+
free (pLanguageIDs);
238+
239+
return USBSTR_DEFAULT_LANGID;
240+
}
241+
}
242+
243+
// default language ID not found, use first ID
244+
u16 usResult = pLanguageIDs->bString[0];
245+
246+
free (pLanguageIDs);
247+
248+
return usResult;
249+
}

0 commit comments

Comments
 (0)