1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Standard USB requests (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/request.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <assert.h>
|
---|
38 |
|
---|
39 | #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
|
---|
40 |
|
---|
41 | /** Generic wrapper for SET requests using standard control request format.
|
---|
42 | *
|
---|
43 | * @see usb_endpoint_pipe_control_write
|
---|
44 | *
|
---|
45 | * @param pipe Pipe used for the communication.
|
---|
46 | * @param request_type Request type (standard/class/vendor).
|
---|
47 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
48 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
49 | * @param value Value of @c wValue field of setup packet
|
---|
50 | * (must be in USB endianness).
|
---|
51 | * @param index Value of @c wIndex field of setup packet
|
---|
52 | * (must be in USB endianness).
|
---|
53 | * @param data Data to be sent during DATA stage
|
---|
54 | * (expected to be in USB endianness).
|
---|
55 | * @param data_size Size of the @p data buffer (in native endianness).
|
---|
56 | * @return Error code.
|
---|
57 | * @retval EBADMEM @p pipe is NULL.
|
---|
58 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
59 | * @retval ERANGE Data buffer too large.
|
---|
60 | */
|
---|
61 | int usb_control_request_set(usb_endpoint_pipe_t *pipe,
|
---|
62 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
63 | uint8_t request,
|
---|
64 | uint16_t value, uint16_t index,
|
---|
65 | void *data, size_t data_size)
|
---|
66 | {
|
---|
67 | if (pipe == NULL) {
|
---|
68 | return EBADMEM;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (data_size > MAX_DATA_LENGTH) {
|
---|
72 | return ERANGE;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if ((data_size > 0) && (data == NULL)) {
|
---|
76 | return EBADMEM;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * TODO: check that @p request_type and @p recipient are
|
---|
81 | * within ranges.
|
---|
82 | */
|
---|
83 |
|
---|
84 | usb_device_request_setup_packet_t setup_packet;
|
---|
85 | setup_packet.request_type = (request_type << 5) | recipient;
|
---|
86 | setup_packet.request = request;
|
---|
87 | setup_packet.value = value;
|
---|
88 | setup_packet.index = index;
|
---|
89 | setup_packet.length = (uint16_t) data_size;
|
---|
90 |
|
---|
91 | int rc = usb_endpoint_pipe_control_write(pipe,
|
---|
92 | &setup_packet, sizeof(setup_packet),
|
---|
93 | data, data_size);
|
---|
94 |
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Generic wrapper for GET requests using standard control request format.
|
---|
99 | *
|
---|
100 | * @see usb_endpoint_pipe_control_read
|
---|
101 | *
|
---|
102 | * @param pipe Pipe used for the communication.
|
---|
103 | * @param request_type Request type (standard/class/vendor).
|
---|
104 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
105 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
106 | * @param value Value of @c wValue field of setup packet
|
---|
107 | * (must be in USB endianness).
|
---|
108 | * @param index Value of @c wIndex field of setup packet
|
---|
109 | * (must be in USB endianness).
|
---|
110 | * @param data Buffer where to store data accepted during the DATA stage.
|
---|
111 | * (they will come in USB endianess).
|
---|
112 | * @param data_size Size of the @p data buffer
|
---|
113 | * (in native endianness).
|
---|
114 | * @param actual_data_size Actual size of transfered data
|
---|
115 | * (in native endianness).
|
---|
116 | * @return Error code.
|
---|
117 | * @retval EBADMEM @p pipe is NULL.
|
---|
118 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
119 | * @retval ERANGE Data buffer too large.
|
---|
120 | */
|
---|
121 | int usb_control_request_get(usb_endpoint_pipe_t *pipe,
|
---|
122 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
123 | uint8_t request,
|
---|
124 | uint16_t value, uint16_t index,
|
---|
125 | void *data, size_t data_size, size_t *actual_data_size)
|
---|
126 | {
|
---|
127 | if (pipe == NULL) {
|
---|
128 | return EBADMEM;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (data_size > MAX_DATA_LENGTH) {
|
---|
132 | return ERANGE;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if ((data_size > 0) && (data == NULL)) {
|
---|
136 | return EBADMEM;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * TODO: check that @p request_type and @p recipient are
|
---|
141 | * within ranges.
|
---|
142 | */
|
---|
143 |
|
---|
144 | usb_device_request_setup_packet_t setup_packet;
|
---|
145 | setup_packet.request_type = 128 | (request_type << 5) | recipient;
|
---|
146 | setup_packet.request = request;
|
---|
147 | setup_packet.value = value;
|
---|
148 | setup_packet.index = index;
|
---|
149 | setup_packet.length = (uint16_t) data_size;
|
---|
150 |
|
---|
151 | int rc = usb_endpoint_pipe_control_read(pipe,
|
---|
152 | &setup_packet, sizeof(setup_packet),
|
---|
153 | data, data_size, actual_data_size);
|
---|
154 |
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Change address of connected device.
|
---|
159 | * This function automatically updates the backing connection to point to
|
---|
160 | * the new address.
|
---|
161 | *
|
---|
162 | * @see usb_drv_reserve_default_address
|
---|
163 | * @see usb_drv_release_default_address
|
---|
164 | * @see usb_drv_request_address
|
---|
165 | * @see usb_drv_release_address
|
---|
166 | * @see usb_drv_bind_address
|
---|
167 | *
|
---|
168 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
169 | * @param new_address New USB address to be set (in native endianness).
|
---|
170 | * @return Error code.
|
---|
171 | */
|
---|
172 | int usb_request_set_address(usb_endpoint_pipe_t *pipe,
|
---|
173 | usb_address_t new_address)
|
---|
174 | {
|
---|
175 | if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
|
---|
176 | return EINVAL;
|
---|
177 | }
|
---|
178 |
|
---|
179 | uint16_t addr = uint16_host2usb((uint16_t)new_address);
|
---|
180 |
|
---|
181 | int rc = usb_control_request_set(pipe,
|
---|
182 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
183 | USB_DEVREQ_SET_ADDRESS,
|
---|
184 | addr, 0,
|
---|
185 | NULL, 0);
|
---|
186 |
|
---|
187 | if (rc != EOK) {
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 | assert(pipe->wire != NULL);
|
---|
192 | /* TODO: prevent other from accessing wire now. */
|
---|
193 | pipe->wire->address = new_address;
|
---|
194 |
|
---|
195 | return EOK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Retrieve USB descriptor of a USB device.
|
---|
199 | *
|
---|
200 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
201 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
202 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
203 | * @param[in] descriptor_index Descriptor index.
|
---|
204 | * @param[in] language Language index.
|
---|
205 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
---|
206 | * @param[in] size Size of the @p buffer.
|
---|
207 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
208 | * @return Error code.
|
---|
209 | */
|
---|
210 | int usb_request_get_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
211 | usb_request_type_t request_type,
|
---|
212 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
213 | uint16_t language,
|
---|
214 | void *buffer, size_t size, size_t *actual_size)
|
---|
215 | {
|
---|
216 | if (buffer == NULL) {
|
---|
217 | return EBADMEM;
|
---|
218 | }
|
---|
219 | if (size == 0) {
|
---|
220 | return EINVAL;
|
---|
221 | }
|
---|
222 |
|
---|
223 | uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
224 |
|
---|
225 | return usb_control_request_get(pipe,
|
---|
226 | request_type, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
227 | USB_DEVREQ_GET_DESCRIPTOR,
|
---|
228 | wValue, language,
|
---|
229 | buffer, size, actual_size);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Retrieve USB descriptor, allocate space for it.
|
---|
233 | *
|
---|
234 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
235 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
236 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
237 | * @param[in] descriptor_index Descriptor index.
|
---|
238 | * @param[in] language Language index.
|
---|
239 | * @param[out] buffer_ptr Where to store pointer to allocated buffer.
|
---|
240 | * @param[out] buffer_size Where to store the size of the descriptor.
|
---|
241 | * @return
|
---|
242 | */
|
---|
243 | int usb_request_get_descriptor_alloc(usb_endpoint_pipe_t * pipe,
|
---|
244 | usb_request_type_t request_type,
|
---|
245 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
246 | uint16_t language,
|
---|
247 | void **buffer_ptr, size_t *buffer_size)
|
---|
248 | {
|
---|
249 | if (buffer_ptr == NULL) {
|
---|
250 | return EBADMEM;
|
---|
251 | }
|
---|
252 |
|
---|
253 | int rc;
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Get only first byte to retrieve descriptor length.
|
---|
257 | */
|
---|
258 | uint8_t tmp_buffer[1];
|
---|
259 | size_t bytes_transfered;
|
---|
260 | rc = usb_request_get_descriptor(pipe, request_type,
|
---|
261 | descriptor_type, descriptor_index, language,
|
---|
262 | &tmp_buffer, 1, &bytes_transfered);
|
---|
263 | if (rc != EOK) {
|
---|
264 | return rc;
|
---|
265 | }
|
---|
266 | if (bytes_transfered != 1) {
|
---|
267 | /* FIXME: some better error code? */
|
---|
268 | return ESTALL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | size_t size = tmp_buffer[0];
|
---|
272 | if (size == 0) {
|
---|
273 | /* FIXME: some better error code? */
|
---|
274 | return ESTALL;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * Allocate buffer and get the descriptor again.
|
---|
279 | */
|
---|
280 | void *buffer = malloc(size);
|
---|
281 | if (buffer == NULL) {
|
---|
282 | return ENOMEM;
|
---|
283 | }
|
---|
284 |
|
---|
285 | rc = usb_request_get_descriptor(pipe, request_type,
|
---|
286 | descriptor_type, descriptor_index, language,
|
---|
287 | buffer, size, &bytes_transfered);
|
---|
288 | if (rc != EOK) {
|
---|
289 | free(buffer);
|
---|
290 | return rc;
|
---|
291 | }
|
---|
292 | if (bytes_transfered != size) {
|
---|
293 | free(buffer);
|
---|
294 | /* FIXME: some better error code? */
|
---|
295 | return ESTALL;
|
---|
296 | }
|
---|
297 |
|
---|
298 | *buffer_ptr = buffer;
|
---|
299 | if (buffer_size != NULL) {
|
---|
300 | *buffer_size = size;
|
---|
301 | }
|
---|
302 |
|
---|
303 | return EOK;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /** Retrieve standard device descriptor of a USB device.
|
---|
307 | *
|
---|
308 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
309 | * @param[out] descriptor Storage for the device descriptor.
|
---|
310 | * @return Error code.
|
---|
311 | */
|
---|
312 | int usb_request_get_device_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
313 | usb_standard_device_descriptor_t *descriptor)
|
---|
314 | {
|
---|
315 | if (descriptor == NULL) {
|
---|
316 | return EBADMEM;
|
---|
317 | }
|
---|
318 |
|
---|
319 | size_t actually_transferred = 0;
|
---|
320 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
321 | int rc = usb_request_get_descriptor(pipe,
|
---|
322 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_DEVICE,
|
---|
323 | 0, 0,
|
---|
324 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
325 | &actually_transferred);
|
---|
326 |
|
---|
327 | if (rc != EOK) {
|
---|
328 | return rc;
|
---|
329 | }
|
---|
330 |
|
---|
331 | /* Verify that all data has been transferred. */
|
---|
332 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
333 | return ELIMIT;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* Everything is okay, copy the descriptor. */
|
---|
337 | memcpy(descriptor, &descriptor_tmp,
|
---|
338 | sizeof(descriptor_tmp));
|
---|
339 |
|
---|
340 | return EOK;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** Retrieve configuration descriptor of a USB device.
|
---|
344 | *
|
---|
345 | * The function does not retrieve additional data binded with configuration
|
---|
346 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
347 | * usb_request_get_full_configuration_descriptor() instead.
|
---|
348 | *
|
---|
349 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
350 | * @param[in] index Descriptor index.
|
---|
351 | * @param[out] descriptor Storage for the device descriptor.
|
---|
352 | * @return Error code.
|
---|
353 | */
|
---|
354 | int usb_request_get_bare_configuration_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
355 | int index, usb_standard_configuration_descriptor_t *descriptor)
|
---|
356 | {
|
---|
357 | if (descriptor == NULL) {
|
---|
358 | return EBADMEM;
|
---|
359 | }
|
---|
360 |
|
---|
361 | if ((index < 0) || (index > 0xFF)) {
|
---|
362 | return ERANGE;
|
---|
363 | }
|
---|
364 |
|
---|
365 | size_t actually_transferred = 0;
|
---|
366 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
367 | int rc = usb_request_get_descriptor(pipe,
|
---|
368 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
|
---|
369 | index, 0,
|
---|
370 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
371 | &actually_transferred);
|
---|
372 | if (rc != EOK) {
|
---|
373 | return rc;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* Verify that all data has been transferred. */
|
---|
377 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
378 | return ELIMIT;
|
---|
379 | }
|
---|
380 |
|
---|
381 | /* Everything is okay, copy the descriptor. */
|
---|
382 | memcpy(descriptor, &descriptor_tmp,
|
---|
383 | sizeof(descriptor_tmp));
|
---|
384 |
|
---|
385 | return EOK;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /** Retrieve full configuration descriptor of a USB device.
|
---|
389 | *
|
---|
390 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
391 | * even when error occurs.
|
---|
392 | *
|
---|
393 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
394 | * @param[in] index Descriptor index.
|
---|
395 | * @param[out] descriptor Storage for the device descriptor.
|
---|
396 | * @param[in] descriptor_size Size of @p descriptor buffer.
|
---|
397 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
398 | * @return Error code.
|
---|
399 | */
|
---|
400 | int usb_request_get_full_configuration_descriptor(usb_endpoint_pipe_t *pipe,
|
---|
401 | int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
|
---|
402 | {
|
---|
403 | if ((index < 0) || (index > 0xFF)) {
|
---|
404 | return ERANGE;
|
---|
405 | }
|
---|
406 |
|
---|
407 | return usb_request_get_descriptor(pipe,
|
---|
408 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
|
---|
409 | index, 0,
|
---|
410 | descriptor, descriptor_size, actual_size);
|
---|
411 | }
|
---|
412 |
|
---|
413 | /** Set configuration of USB device.
|
---|
414 | *
|
---|
415 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
416 | * @param configuration_value New configuration value.
|
---|
417 | * @return Error code.
|
---|
418 | */
|
---|
419 | int usb_request_set_configuration(usb_endpoint_pipe_t *pipe,
|
---|
420 | uint8_t configuration_value)
|
---|
421 | {
|
---|
422 | uint16_t config_value
|
---|
423 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
424 |
|
---|
425 | return usb_control_request_set(pipe,
|
---|
426 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
427 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
428 | NULL, 0);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** Get list of supported languages by USB device.
|
---|
432 | *
|
---|
433 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
434 | * @param[out] languages_ptr Where to store pointer to allocated array of
|
---|
435 | * supported languages.
|
---|
436 | * @param[out] languages_count Number of supported languages.
|
---|
437 | * @return Error code.
|
---|
438 | */
|
---|
439 | int usb_request_get_supported_languages(usb_endpoint_pipe_t *pipe,
|
---|
440 | l18_win_locales_t **languages_ptr, size_t *languages_count)
|
---|
441 | {
|
---|
442 | int rc;
|
---|
443 |
|
---|
444 | if (languages_ptr == NULL) {
|
---|
445 | return EBADMEM;
|
---|
446 | }
|
---|
447 | if (languages_count == NULL) {
|
---|
448 | return EBADMEM;
|
---|
449 | }
|
---|
450 |
|
---|
451 | uint8_t *string_descriptor = NULL;
|
---|
452 | size_t string_descriptor_size = 0;
|
---|
453 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
454 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING, 0, 0,
|
---|
455 | (void **) &string_descriptor, &string_descriptor_size);
|
---|
456 | if (rc != EOK) {
|
---|
457 | return rc;
|
---|
458 | }
|
---|
459 | if (string_descriptor_size <= 2) {
|
---|
460 | free(string_descriptor);
|
---|
461 | return EEMPTY;
|
---|
462 | }
|
---|
463 | /* Substract first 2 bytes (length and descriptor type). */
|
---|
464 | string_descriptor_size -= 2;
|
---|
465 |
|
---|
466 | /* Odd number of bytes - descriptor is broken? */
|
---|
467 | if ((string_descriptor_size % 2) != 0) {
|
---|
468 | /* FIXME: shall we return with error or silently ignore? */
|
---|
469 | free(string_descriptor);
|
---|
470 | return ESTALL;
|
---|
471 | }
|
---|
472 |
|
---|
473 | size_t langs_count = string_descriptor_size / 2;
|
---|
474 | l18_win_locales_t *langs
|
---|
475 | = malloc(sizeof(l18_win_locales_t) * langs_count);
|
---|
476 | if (langs == NULL) {
|
---|
477 | free(string_descriptor);
|
---|
478 | return ENOMEM;
|
---|
479 | }
|
---|
480 |
|
---|
481 | size_t i;
|
---|
482 | for (i = 0; i < langs_count; i++) {
|
---|
483 | /* Language code from the descriptor is in USB endianess. */
|
---|
484 | /* FIXME: is this really correct? */
|
---|
485 | uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
|
---|
486 | + string_descriptor[2 + 2 * i];
|
---|
487 | langs[i] = uint16_usb2host(lang_code);
|
---|
488 | }
|
---|
489 |
|
---|
490 | free(string_descriptor);
|
---|
491 |
|
---|
492 | *languages_ptr = langs;
|
---|
493 | *languages_count =langs_count;
|
---|
494 |
|
---|
495 | return EOK;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /** Get string (descriptor) from USB device.
|
---|
499 | *
|
---|
500 | * The string is returned in native encoding of the operating system.
|
---|
501 | * For HelenOS, that is UTF-8.
|
---|
502 | *
|
---|
503 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
504 | * @param[in] index String index (in native endianess).
|
---|
505 | * @param[in] lang String language (in native endianess).
|
---|
506 | * @param[out] string_ptr Where to store allocated string in native encoding.
|
---|
507 | * @return Error code.
|
---|
508 | */
|
---|
509 | int usb_request_get_string(usb_endpoint_pipe_t *pipe,
|
---|
510 | size_t index, l18_win_locales_t lang, char **string_ptr)
|
---|
511 | {
|
---|
512 | if (string_ptr == NULL) {
|
---|
513 | return EBADMEM;
|
---|
514 | }
|
---|
515 | /* Index is actually one byte value. */
|
---|
516 | if (index > 0xFF) {
|
---|
517 | return ERANGE;
|
---|
518 | }
|
---|
519 | /* Language is actually two byte value. */
|
---|
520 | if (lang > 0xFFFF) {
|
---|
521 | return ERANGE;
|
---|
522 | }
|
---|
523 |
|
---|
524 | int rc;
|
---|
525 |
|
---|
526 | /* Prepare dynamically allocated variables. */
|
---|
527 | uint8_t *string = NULL;
|
---|
528 | wchar_t *string_chars = NULL;
|
---|
529 |
|
---|
530 | /* Get the actual descriptor. */
|
---|
531 | size_t string_size;
|
---|
532 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
533 | USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_STRING,
|
---|
534 | index, uint16_host2usb(lang),
|
---|
535 | (void **) &string, &string_size);
|
---|
536 | if (rc != EOK) {
|
---|
537 | goto leave;
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (string_size <= 2) {
|
---|
541 | rc = EEMPTY;
|
---|
542 | goto leave;
|
---|
543 | }
|
---|
544 | /* Substract first 2 bytes (length and descriptor type). */
|
---|
545 | string_size -= 2;
|
---|
546 |
|
---|
547 | /* Odd number of bytes - descriptor is broken? */
|
---|
548 | if ((string_size % 2) != 0) {
|
---|
549 | /* FIXME: shall we return with error or silently ignore? */
|
---|
550 | rc = ESTALL;
|
---|
551 | goto leave;
|
---|
552 | }
|
---|
553 |
|
---|
554 | size_t string_char_count = string_size / 2;
|
---|
555 | string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
---|
556 | if (string_chars == NULL) {
|
---|
557 | rc = ENOMEM;
|
---|
558 | goto leave;
|
---|
559 | }
|
---|
560 |
|
---|
561 | /*
|
---|
562 | * Build a wide string.
|
---|
563 | * And do not forget to set NULL terminator (string descriptors
|
---|
564 | * do not have them).
|
---|
565 | */
|
---|
566 | size_t i;
|
---|
567 | for (i = 0; i < string_char_count; i++) {
|
---|
568 | uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
---|
569 | + string[2 + 2 * i];
|
---|
570 | string_chars[i] = uni_char;
|
---|
571 | }
|
---|
572 | string_chars[string_char_count] = 0;
|
---|
573 |
|
---|
574 |
|
---|
575 | /* Convert to normal string. */
|
---|
576 | char *str = wstr_to_astr(string_chars);
|
---|
577 | if (str == NULL) {
|
---|
578 | rc = ENOMEM;
|
---|
579 | goto leave;
|
---|
580 | }
|
---|
581 |
|
---|
582 | *string_ptr = str;
|
---|
583 | rc = EOK;
|
---|
584 |
|
---|
585 | leave:
|
---|
586 | if (string != NULL) {
|
---|
587 | free(string);
|
---|
588 | }
|
---|
589 | if (string_chars != NULL) {
|
---|
590 | free(string_chars);
|
---|
591 | }
|
---|
592 |
|
---|
593 | return rc;
|
---|
594 | }
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * @}
|
---|
598 | */
|
---|