24
24
#include <uspi/assert.h>
25
25
#include <uspios.h>
26
26
27
- #define USBSTR_MIN_LENGTH 16
27
+ #define USBSTR_MIN_LENGTH 4
28
+
29
+ #define USBSTR_DEFAULT_LANGID 0x409
28
30
29
31
void USBString (TUSBString * pThis , struct TUSBDevice * pDevice )
30
32
{
@@ -78,7 +80,7 @@ void _USBString (TUSBString *pThis)
78
80
pThis -> m_pDevice = 0 ;
79
81
}
80
82
81
- boolean USBStringGetFromDescriptor (TUSBString * pThis , u8 ucID )
83
+ boolean USBStringGetFromDescriptor (TUSBString * pThis , u8 ucID , u16 usLanguageID )
82
84
{
83
85
assert (pThis != 0 );
84
86
assert (ucID > 0 );
@@ -91,9 +93,11 @@ boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID)
91
93
assert (pThis -> m_pUSBString != 0 );
92
94
93
95
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 )
97
101
{
98
102
return FALSE;
99
103
}
@@ -112,9 +116,11 @@ boolean USBStringGetFromDescriptor (TUSBString *pThis, u8 ucID)
112
116
pThis -> m_pUSBString = (TUSBStringDescriptor * ) malloc (ucLength );
113
117
assert (pThis -> m_pUSBString != 0 );
114
118
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 )
118
124
{
119
125
return FALSE;
120
126
}
@@ -164,3 +170,80 @@ const char *USBStringGet (TUSBString *pThis)
164
170
assert (pThis != 0 );
165
171
return StringGet (pThis -> m_pString );
166
172
}
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