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 libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * Standard USB requests (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/dev/request.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <assert.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 |
|
---|
40 | #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
|
---|
41 |
|
---|
42 | /** Generic wrapper for SET requests using standard control request format.
|
---|
43 | *
|
---|
44 | * @see usb_pipe_control_write
|
---|
45 | *
|
---|
46 | * @param pipe Pipe used for the communication.
|
---|
47 | * @param request_type Request type (standard/class/vendor).
|
---|
48 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
49 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
50 | * @param value Value of @c wValue field of setup packet
|
---|
51 | * (must be in USB endianness).
|
---|
52 | * @param index Value of @c wIndex field of setup packet
|
---|
53 | * (must be in USB endianness).
|
---|
54 | * @param data Data to be sent during DATA stage
|
---|
55 | * (expected to be in USB endianness).
|
---|
56 | * @param data_size Size of the @p data buffer (in native endianness).
|
---|
57 | * @return Error code.
|
---|
58 | * @retval EBADMEM @p pipe is NULL.
|
---|
59 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
60 | * @retval ERANGE Data buffer too large.
|
---|
61 | */
|
---|
62 | int usb_control_request_set(usb_pipe_t *pipe,
|
---|
63 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
64 | uint8_t request,
|
---|
65 | uint16_t value, uint16_t index,
|
---|
66 | void *data, size_t data_size)
|
---|
67 | {
|
---|
68 | if (pipe == NULL) {
|
---|
69 | return EBADMEM;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (data_size > MAX_DATA_LENGTH) {
|
---|
73 | return ERANGE;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if ((data_size > 0) && (data == NULL)) {
|
---|
77 | return EBADMEM;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * TODO: check that @p request_type and @p recipient are
|
---|
82 | * within ranges.
|
---|
83 | */
|
---|
84 |
|
---|
85 | usb_device_request_setup_packet_t setup_packet;
|
---|
86 | setup_packet.request_type = (request_type << 5) | recipient;
|
---|
87 | setup_packet.request = request;
|
---|
88 | setup_packet.value = value;
|
---|
89 | setup_packet.index = index;
|
---|
90 | setup_packet.length = (uint16_t) data_size;
|
---|
91 |
|
---|
92 | int rc = usb_pipe_control_write(pipe,
|
---|
93 | &setup_packet, sizeof(setup_packet),
|
---|
94 | data, data_size);
|
---|
95 |
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Generic wrapper for GET requests using standard control request format.
|
---|
100 | *
|
---|
101 | * @see usb_pipe_control_read
|
---|
102 | *
|
---|
103 | * @param pipe Pipe used for the communication.
|
---|
104 | * @param request_type Request type (standard/class/vendor).
|
---|
105 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
106 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
107 | * @param value Value of @c wValue field of setup packet
|
---|
108 | * (must be in USB endianness).
|
---|
109 | * @param index Value of @c wIndex field of setup packet
|
---|
110 | * (must be in USB endianness).
|
---|
111 | * @param data Buffer where to store data accepted during the DATA stage.
|
---|
112 | * (they will come in USB endianness).
|
---|
113 | * @param data_size Size of the @p data buffer
|
---|
114 | * (in native endianness).
|
---|
115 | * @param actual_data_size Actual size of transfered data
|
---|
116 | * (in native endianness).
|
---|
117 | * @return Error code.
|
---|
118 | * @retval EBADMEM @p pipe is NULL.
|
---|
119 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
120 | * @retval ERANGE Data buffer too large.
|
---|
121 | */
|
---|
122 | int usb_control_request_get(usb_pipe_t *pipe,
|
---|
123 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
124 | uint8_t request,
|
---|
125 | uint16_t value, uint16_t index,
|
---|
126 | void *data, size_t data_size, size_t *actual_data_size)
|
---|
127 | {
|
---|
128 | if (pipe == NULL) {
|
---|
129 | return EBADMEM;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (data_size > MAX_DATA_LENGTH) {
|
---|
133 | return ERANGE;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if ((data_size > 0) && (data == NULL)) {
|
---|
137 | return EBADMEM;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * TODO: check that @p request_type and @p recipient are
|
---|
142 | * within ranges.
|
---|
143 | */
|
---|
144 |
|
---|
145 | const usb_device_request_setup_packet_t setup_packet = {
|
---|
146 | .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST
|
---|
147 | | (request_type << 5) | recipient,
|
---|
148 | .request = request,
|
---|
149 | .value = value,
|
---|
150 | .index = index,
|
---|
151 | .length = (uint16_t) data_size,
|
---|
152 | };
|
---|
153 |
|
---|
154 | return usb_pipe_control_read(pipe, &setup_packet, sizeof(setup_packet),
|
---|
155 | data, data_size, actual_data_size);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Retrieve status of a USB device.
|
---|
159 | *
|
---|
160 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
161 | * @param[in] index Recipient index (in native endianness).
|
---|
162 | * @param[in] recipient Recipient of the GET_STATUS request.
|
---|
163 | * @param[out] status Recipient status (in native endianness).
|
---|
164 | * @return Error code.
|
---|
165 | */
|
---|
166 | int usb_request_get_status(usb_pipe_t *pipe,
|
---|
167 | usb_request_recipient_t recipient, uint16_t index,
|
---|
168 | uint16_t *status)
|
---|
169 | {
|
---|
170 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
|
---|
171 | return EINVAL;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (status == NULL) {
|
---|
175 | return EBADMEM;
|
---|
176 | }
|
---|
177 |
|
---|
178 | uint16_t status_usb_endianess;
|
---|
179 | size_t data_transfered_size;
|
---|
180 | int rc = usb_control_request_get(pipe, USB_REQUEST_TYPE_STANDARD,
|
---|
181 | recipient, USB_DEVREQ_GET_STATUS, 0, uint16_host2usb(index),
|
---|
182 | &status_usb_endianess, 2, &data_transfered_size);
|
---|
183 | if (rc != EOK) {
|
---|
184 | return rc;
|
---|
185 | }
|
---|
186 | if (data_transfered_size != 2) {
|
---|
187 | return ELIMIT;
|
---|
188 | }
|
---|
189 |
|
---|
190 | *status = uint16_usb2host(status_usb_endianess);
|
---|
191 |
|
---|
192 | return EOK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Clear or disable specific device feature.
|
---|
196 | *
|
---|
197 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
198 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
199 | * @param[in] recipient Recipient of the CLEAR_FEATURE request.
|
---|
200 | * @param[in] feature_selector Feature selector (in native endianness).
|
---|
201 | * @param[in] index Recipient index (in native endianness).
|
---|
202 | * @return Error code.
|
---|
203 | */
|
---|
204 | int usb_request_clear_feature(usb_pipe_t *pipe,
|
---|
205 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
206 | uint16_t feature_selector, uint16_t index)
|
---|
207 | {
|
---|
208 | if (request_type == USB_REQUEST_TYPE_STANDARD) {
|
---|
209 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
|
---|
210 | && (index != 0)) {
|
---|
211 | return EINVAL;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | int rc = usb_control_request_set(pipe, request_type, recipient,
|
---|
216 | USB_DEVREQ_CLEAR_FEATURE,
|
---|
217 | uint16_host2usb(feature_selector), uint16_host2usb(index),
|
---|
218 | NULL, 0);
|
---|
219 |
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Set or enable specific device feature.
|
---|
224 | *
|
---|
225 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
226 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
227 | * @param[in] recipient Recipient of the SET_FEATURE request.
|
---|
228 | * @param[in] feature_selector Feature selector (in native endianness).
|
---|
229 | * @param[in] index Recipient index (in native endianness).
|
---|
230 | * @return Error code.
|
---|
231 | */
|
---|
232 | int usb_request_set_feature(usb_pipe_t *pipe,
|
---|
233 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
234 | uint16_t feature_selector, uint16_t index)
|
---|
235 | {
|
---|
236 | if (request_type == USB_REQUEST_TYPE_STANDARD) {
|
---|
237 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
|
---|
238 | && (index != 0)) {
|
---|
239 | return EINVAL;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | int rc = usb_control_request_set(pipe, request_type, recipient,
|
---|
244 | USB_DEVREQ_SET_FEATURE,
|
---|
245 | uint16_host2usb(feature_selector), uint16_host2usb(index),
|
---|
246 | NULL, 0);
|
---|
247 |
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Retrieve USB descriptor of a USB device.
|
---|
252 | *
|
---|
253 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
254 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
255 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
256 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
257 | * @param[in] descriptor_index Descriptor index.
|
---|
258 | * @param[in] language Language index.
|
---|
259 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
---|
260 | * @param[in] size Size of the @p buffer.
|
---|
261 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
262 | * @return Error code.
|
---|
263 | */
|
---|
264 | int usb_request_get_descriptor(usb_pipe_t *pipe,
|
---|
265 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
266 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
267 | uint16_t language,
|
---|
268 | void *buffer, size_t size, size_t *actual_size)
|
---|
269 | {
|
---|
270 | if (buffer == NULL) {
|
---|
271 | return EBADMEM;
|
---|
272 | }
|
---|
273 | if (size == 0) {
|
---|
274 | return EINVAL;
|
---|
275 | }
|
---|
276 |
|
---|
277 | const uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
278 |
|
---|
279 | return usb_control_request_get(pipe,
|
---|
280 | request_type, recipient,
|
---|
281 | USB_DEVREQ_GET_DESCRIPTOR,
|
---|
282 | wValue, language,
|
---|
283 | buffer, size, actual_size);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /** Retrieve USB descriptor, allocate space for it.
|
---|
287 | *
|
---|
288 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
289 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
290 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
291 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
292 | * @param[in] descriptor_index Descriptor index.
|
---|
293 | * @param[in] language Language index.
|
---|
294 | * @param[out] buffer_ptr Where to store pointer to allocated buffer.
|
---|
295 | * @param[out] buffer_size Where to store the size of the descriptor.
|
---|
296 | * @return
|
---|
297 | */
|
---|
298 | int usb_request_get_descriptor_alloc(usb_pipe_t * pipe,
|
---|
299 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
300 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
301 | uint16_t language,
|
---|
302 | void **buffer_ptr, size_t *buffer_size)
|
---|
303 | {
|
---|
304 | if (buffer_ptr == NULL) {
|
---|
305 | return EBADMEM;
|
---|
306 | }
|
---|
307 |
|
---|
308 | int rc;
|
---|
309 |
|
---|
310 | /*
|
---|
311 | * Get only first byte to retrieve descriptor length.
|
---|
312 | */
|
---|
313 | uint8_t tmp_buffer[1];
|
---|
314 | size_t bytes_transfered;
|
---|
315 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
316 | descriptor_type, descriptor_index, language,
|
---|
317 | &tmp_buffer, 1, &bytes_transfered);
|
---|
318 | if (rc != EOK) {
|
---|
319 | return rc;
|
---|
320 | }
|
---|
321 | if (bytes_transfered != 1) {
|
---|
322 | /* FIXME: some better error code? */
|
---|
323 | return ESTALL;
|
---|
324 | }
|
---|
325 |
|
---|
326 | size_t size = tmp_buffer[0];
|
---|
327 | if (size == 0) {
|
---|
328 | /* FIXME: some better error code? */
|
---|
329 | return ESTALL;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /*
|
---|
333 | * Allocate buffer and get the descriptor again.
|
---|
334 | */
|
---|
335 | void *buffer = malloc(size);
|
---|
336 | if (buffer == NULL) {
|
---|
337 | return ENOMEM;
|
---|
338 | }
|
---|
339 |
|
---|
340 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
341 | descriptor_type, descriptor_index, language,
|
---|
342 | buffer, size, &bytes_transfered);
|
---|
343 | if (rc != EOK) {
|
---|
344 | free(buffer);
|
---|
345 | return rc;
|
---|
346 | }
|
---|
347 | if (bytes_transfered != size) {
|
---|
348 | free(buffer);
|
---|
349 | /* FIXME: some better error code? */
|
---|
350 | return ESTALL;
|
---|
351 | }
|
---|
352 |
|
---|
353 | *buffer_ptr = buffer;
|
---|
354 | if (buffer_size != NULL) {
|
---|
355 | *buffer_size = size;
|
---|
356 | }
|
---|
357 |
|
---|
358 | return EOK;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** Retrieve standard device descriptor of a USB device.
|
---|
362 | *
|
---|
363 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
364 | * @param[out] descriptor Storage for the device descriptor.
|
---|
365 | * @return Error code.
|
---|
366 | */
|
---|
367 | int usb_request_get_device_descriptor(usb_pipe_t *pipe,
|
---|
368 | usb_standard_device_descriptor_t *descriptor)
|
---|
369 | {
|
---|
370 | if (descriptor == NULL) {
|
---|
371 | return EBADMEM;
|
---|
372 | }
|
---|
373 |
|
---|
374 | size_t actually_transferred = 0;
|
---|
375 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
376 | int rc = usb_request_get_descriptor(pipe,
|
---|
377 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
378 | USB_DESCTYPE_DEVICE, 0, 0,
|
---|
379 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
380 | &actually_transferred);
|
---|
381 |
|
---|
382 | if (rc != EOK) {
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Verify that all data has been transferred. */
|
---|
387 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
388 | return ELIMIT;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /* Everything is okay, copy the descriptor. */
|
---|
392 | memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
|
---|
393 |
|
---|
394 | return EOK;
|
---|
395 | }
|
---|
396 |
|
---|
397 | /** Retrieve configuration descriptor of a USB device.
|
---|
398 | *
|
---|
399 | * The function does not retrieve additional data binded with configuration
|
---|
400 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
401 | * usb_request_get_full_configuration_descriptor() instead.
|
---|
402 | *
|
---|
403 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
404 | * @param[in] index Descriptor index.
|
---|
405 | * @param[out] descriptor Storage for the device descriptor.
|
---|
406 | * @return Error code.
|
---|
407 | */
|
---|
408 | int usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe,
|
---|
409 | int index, usb_standard_configuration_descriptor_t *descriptor)
|
---|
410 | {
|
---|
411 | if (descriptor == NULL) {
|
---|
412 | return EBADMEM;
|
---|
413 | }
|
---|
414 |
|
---|
415 | if ((index < 0) || (index > 0xFF)) {
|
---|
416 | return ERANGE;
|
---|
417 | }
|
---|
418 |
|
---|
419 | size_t actually_transferred = 0;
|
---|
420 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
421 | int rc = usb_request_get_descriptor(pipe,
|
---|
422 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
423 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
424 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
425 | &actually_transferred);
|
---|
426 | if (rc != EOK) {
|
---|
427 | return rc;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /* Verify that all data has been transferred. */
|
---|
431 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
432 | return ELIMIT;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /* Everything is okay, copy the descriptor. */
|
---|
436 | memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
|
---|
437 |
|
---|
438 | return EOK;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /** Retrieve full configuration descriptor of a USB device.
|
---|
442 | *
|
---|
443 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
444 | * even when error occurs.
|
---|
445 | *
|
---|
446 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
447 | * @param[in] index Descriptor index.
|
---|
448 | * @param[out] descriptor Storage for the device descriptor.
|
---|
449 | * @param[in] descriptor_size Size of @p descriptor buffer.
|
---|
450 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
451 | * @return Error code.
|
---|
452 | */
|
---|
453 | int usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe,
|
---|
454 | int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
|
---|
455 | {
|
---|
456 | if ((index < 0) || (index > 0xFF)) {
|
---|
457 | return ERANGE;
|
---|
458 | }
|
---|
459 |
|
---|
460 | return usb_request_get_descriptor(pipe,
|
---|
461 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
462 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
463 | descriptor, descriptor_size, actual_size);
|
---|
464 | }
|
---|
465 |
|
---|
466 | /** Retrieve full configuration descriptor, allocate space for it.
|
---|
467 | *
|
---|
468 | * The function takes care that full configuration descriptor is returned
|
---|
469 | * (i.e. the function will fail when less data then descriptor.totalLength
|
---|
470 | * is returned).
|
---|
471 | *
|
---|
472 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
473 | * @param[in] index Configuration index.
|
---|
474 | * @param[out] descriptor_ptr Where to store pointer to allocated buffer.
|
---|
475 | * @param[out] descriptor_size Where to store the size of the descriptor.
|
---|
476 | * @return Error code.
|
---|
477 | */
|
---|
478 | int usb_request_get_full_configuration_descriptor_alloc(
|
---|
479 | usb_pipe_t *pipe, int index,
|
---|
480 | void **descriptor_ptr, size_t *descriptor_size)
|
---|
481 | {
|
---|
482 | int rc;
|
---|
483 |
|
---|
484 | if (descriptor_ptr == NULL) {
|
---|
485 | return EBADMEM;
|
---|
486 | }
|
---|
487 |
|
---|
488 | usb_standard_configuration_descriptor_t bare_config;
|
---|
489 | rc = usb_request_get_bare_configuration_descriptor(pipe, index,
|
---|
490 | &bare_config);
|
---|
491 | if (rc != EOK) {
|
---|
492 | return rc;
|
---|
493 | }
|
---|
494 | if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
|
---|
495 | return ENOENT;
|
---|
496 | }
|
---|
497 | if (bare_config.total_length < sizeof(bare_config)) {
|
---|
498 | return ELIMIT;
|
---|
499 | }
|
---|
500 |
|
---|
501 | void *buffer = malloc(bare_config.total_length);
|
---|
502 | if (buffer == NULL) {
|
---|
503 | return ENOMEM;
|
---|
504 | }
|
---|
505 |
|
---|
506 | size_t transferred = 0;
|
---|
507 | rc = usb_request_get_full_configuration_descriptor(pipe, index,
|
---|
508 | buffer, bare_config.total_length, &transferred);
|
---|
509 | if (rc != EOK) {
|
---|
510 | free(buffer);
|
---|
511 | return rc;
|
---|
512 | }
|
---|
513 |
|
---|
514 | if (transferred != bare_config.total_length) {
|
---|
515 | free(buffer);
|
---|
516 | return ELIMIT;
|
---|
517 | }
|
---|
518 |
|
---|
519 | /* Everything looks okay, copy the pointers. */
|
---|
520 |
|
---|
521 | *descriptor_ptr = buffer;
|
---|
522 |
|
---|
523 | if (descriptor_size != NULL) {
|
---|
524 | *descriptor_size = bare_config.total_length;
|
---|
525 | }
|
---|
526 |
|
---|
527 | return EOK;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /** Update existing or add new USB descriptor to a USB device.
|
---|
531 | *
|
---|
532 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
533 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
534 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
535 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
536 | * @param[in] descriptor_index Descriptor index.
|
---|
537 | * @param[in] language Language index (in native endianness).
|
---|
538 | * @param[in] buffer Buffer with the new descriptor (in USB endianness).
|
---|
539 | * @param[in] size Size of the @p buffer in bytes (in native endianness).
|
---|
540 | * @return Error code.
|
---|
541 | */
|
---|
542 | int usb_request_set_descriptor(usb_pipe_t *pipe,
|
---|
543 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
544 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
545 | uint16_t language,
|
---|
546 | void *buffer, size_t size)
|
---|
547 | {
|
---|
548 | if (buffer == NULL) {
|
---|
549 | return EBADMEM;
|
---|
550 | }
|
---|
551 | if (size == 0) {
|
---|
552 | return EINVAL;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /* FIXME: proper endianness. */
|
---|
556 | uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
557 |
|
---|
558 | return usb_control_request_set(pipe,
|
---|
559 | request_type, recipient,
|
---|
560 | USB_DEVREQ_SET_DESCRIPTOR,
|
---|
561 | wValue, language,
|
---|
562 | buffer, size);
|
---|
563 | }
|
---|
564 |
|
---|
565 | /** Get current configuration value of USB device.
|
---|
566 | *
|
---|
567 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
568 | * @param[out] configuration_value Current configuration value.
|
---|
569 | * @return Error code.
|
---|
570 | */
|
---|
571 | int usb_request_get_configuration(usb_pipe_t *pipe,
|
---|
572 | uint8_t *configuration_value)
|
---|
573 | {
|
---|
574 | uint8_t value;
|
---|
575 | size_t actual_size;
|
---|
576 |
|
---|
577 | int rc = usb_control_request_get(pipe,
|
---|
578 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
579 | USB_DEVREQ_GET_CONFIGURATION,
|
---|
580 | 0, 0,
|
---|
581 | &value, 1, &actual_size);
|
---|
582 |
|
---|
583 | if (rc != EOK) {
|
---|
584 | return rc;
|
---|
585 | }
|
---|
586 | if (actual_size != 1) {
|
---|
587 | return ELIMIT;
|
---|
588 | }
|
---|
589 |
|
---|
590 | if (configuration_value != NULL) {
|
---|
591 | *configuration_value = value;
|
---|
592 | }
|
---|
593 |
|
---|
594 | return EOK;
|
---|
595 | }
|
---|
596 |
|
---|
597 | /** Set configuration of USB device.
|
---|
598 | *
|
---|
599 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
600 | * @param configuration_value New configuration value.
|
---|
601 | * @return Error code.
|
---|
602 | */
|
---|
603 | int usb_request_set_configuration(usb_pipe_t *pipe,
|
---|
604 | uint8_t configuration_value)
|
---|
605 | {
|
---|
606 | uint16_t config_value
|
---|
607 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
608 |
|
---|
609 | return usb_control_request_set(pipe,
|
---|
610 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
611 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
612 | NULL, 0);
|
---|
613 | }
|
---|
614 |
|
---|
615 | /** Get selected alternate setting for USB interface.
|
---|
616 | *
|
---|
617 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
618 | * @param[in] interface_index Interface index.
|
---|
619 | * @param[out] alternate_setting Alternate setting for the interface.
|
---|
620 | * @return Error code.
|
---|
621 | */
|
---|
622 | int usb_request_get_interface(usb_pipe_t *pipe,
|
---|
623 | uint8_t interface_index, uint8_t *alternate_setting)
|
---|
624 | {
|
---|
625 | uint8_t value;
|
---|
626 | size_t actual_size;
|
---|
627 |
|
---|
628 | int rc = usb_control_request_get(pipe,
|
---|
629 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
630 | USB_DEVREQ_GET_INTERFACE,
|
---|
631 | 0, uint16_host2usb((uint16_t) interface_index),
|
---|
632 | &value, 1, &actual_size);
|
---|
633 |
|
---|
634 | if (rc != EOK) {
|
---|
635 | return rc;
|
---|
636 | }
|
---|
637 | if (actual_size != 1) {
|
---|
638 | return ELIMIT;
|
---|
639 | }
|
---|
640 |
|
---|
641 | if (alternate_setting != NULL) {
|
---|
642 | *alternate_setting = value;
|
---|
643 | }
|
---|
644 |
|
---|
645 | return EOK;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /** Select alternate setting for USB interface.
|
---|
649 | *
|
---|
650 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
651 | * @param[in] interface_index Interface index.
|
---|
652 | * @param[in] alternate_setting Alternate setting to select.
|
---|
653 | * @return Error code.
|
---|
654 | */
|
---|
655 | int usb_request_set_interface(usb_pipe_t *pipe,
|
---|
656 | uint8_t interface_index, uint8_t alternate_setting)
|
---|
657 | {
|
---|
658 | return usb_control_request_set(pipe,
|
---|
659 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
660 | USB_DEVREQ_SET_INTERFACE,
|
---|
661 | uint16_host2usb((uint16_t) alternate_setting),
|
---|
662 | uint16_host2usb((uint16_t) interface_index),
|
---|
663 | NULL, 0);
|
---|
664 | }
|
---|
665 |
|
---|
666 | /** Get list of supported languages by USB device.
|
---|
667 | *
|
---|
668 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
669 | * @param[out] languages_ptr Where to store pointer to allocated array of
|
---|
670 | * supported languages.
|
---|
671 | * @param[out] languages_count Number of supported languages.
|
---|
672 | * @return Error code.
|
---|
673 | */
|
---|
674 | int usb_request_get_supported_languages(usb_pipe_t *pipe,
|
---|
675 | l18_win_locales_t **languages_ptr, size_t *languages_count)
|
---|
676 | {
|
---|
677 | int rc;
|
---|
678 |
|
---|
679 | if (languages_ptr == NULL) {
|
---|
680 | return EBADMEM;
|
---|
681 | }
|
---|
682 | if (languages_count == NULL) {
|
---|
683 | return EBADMEM;
|
---|
684 | }
|
---|
685 |
|
---|
686 | uint8_t *string_descriptor = NULL;
|
---|
687 | size_t string_descriptor_size = 0;
|
---|
688 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
689 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
690 | USB_DESCTYPE_STRING, 0, 0,
|
---|
691 | (void **) &string_descriptor, &string_descriptor_size);
|
---|
692 | if (rc != EOK) {
|
---|
693 | return rc;
|
---|
694 | }
|
---|
695 | if (string_descriptor_size <= 2) {
|
---|
696 | free(string_descriptor);
|
---|
697 | return EEMPTY;
|
---|
698 | }
|
---|
699 | /* Subtract first 2 bytes (length and descriptor type). */
|
---|
700 | string_descriptor_size -= 2;
|
---|
701 |
|
---|
702 | /* Odd number of bytes - descriptor is broken? */
|
---|
703 | if ((string_descriptor_size % 2) != 0) {
|
---|
704 | /* FIXME: shall we return with error or silently ignore? */
|
---|
705 | free(string_descriptor);
|
---|
706 | return ESTALL;
|
---|
707 | }
|
---|
708 |
|
---|
709 | size_t langs_count = string_descriptor_size / 2;
|
---|
710 | l18_win_locales_t *langs
|
---|
711 | = malloc(sizeof(l18_win_locales_t) * langs_count);
|
---|
712 | if (langs == NULL) {
|
---|
713 | free(string_descriptor);
|
---|
714 | return ENOMEM;
|
---|
715 | }
|
---|
716 |
|
---|
717 | size_t i;
|
---|
718 | for (i = 0; i < langs_count; i++) {
|
---|
719 | /* Language code from the descriptor is in USB endianness. */
|
---|
720 | /* FIXME: is this really correct? */
|
---|
721 | uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
|
---|
722 | + string_descriptor[2 + 2 * i];
|
---|
723 | langs[i] = uint16_usb2host(lang_code);
|
---|
724 | }
|
---|
725 |
|
---|
726 | free(string_descriptor);
|
---|
727 |
|
---|
728 | *languages_ptr = langs;
|
---|
729 | *languages_count =langs_count;
|
---|
730 |
|
---|
731 | return EOK;
|
---|
732 | }
|
---|
733 |
|
---|
734 | /** Get string (descriptor) from USB device.
|
---|
735 | *
|
---|
736 | * The string is returned in native encoding of the operating system.
|
---|
737 | * For HelenOS, that is UTF-8.
|
---|
738 | *
|
---|
739 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
740 | * @param[in] index String index (in native endianness),
|
---|
741 | * first index has number 1 (index from descriptors can be used directly).
|
---|
742 | * @param[in] lang String language (in native endianness).
|
---|
743 | * @param[out] string_ptr Where to store allocated string in native encoding.
|
---|
744 | * @return Error code.
|
---|
745 | */
|
---|
746 | int usb_request_get_string(usb_pipe_t *pipe,
|
---|
747 | size_t index, l18_win_locales_t lang, char **string_ptr)
|
---|
748 | {
|
---|
749 | if (string_ptr == NULL) {
|
---|
750 | return EBADMEM;
|
---|
751 | }
|
---|
752 | /*
|
---|
753 | * Index is actually one byte value and zero index is used
|
---|
754 | * to retrieve list of supported languages.
|
---|
755 | */
|
---|
756 | if ((index < 1) || (index > 0xFF)) {
|
---|
757 | return ERANGE;
|
---|
758 | }
|
---|
759 | /* Language is actually two byte value. */
|
---|
760 | if (lang > 0xFFFF) {
|
---|
761 | return ERANGE;
|
---|
762 | }
|
---|
763 |
|
---|
764 | int rc;
|
---|
765 |
|
---|
766 | /* Prepare dynamically allocated variables. */
|
---|
767 | uint8_t *string = NULL;
|
---|
768 | wchar_t *string_chars = NULL;
|
---|
769 |
|
---|
770 | /* Get the actual descriptor. */
|
---|
771 | size_t string_size;
|
---|
772 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
773 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
774 | USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
|
---|
775 | (void **) &string, &string_size);
|
---|
776 | if (rc != EOK) {
|
---|
777 | goto leave;
|
---|
778 | }
|
---|
779 |
|
---|
780 | if (string_size <= 2) {
|
---|
781 | rc = EEMPTY;
|
---|
782 | goto leave;
|
---|
783 | }
|
---|
784 | /* Subtract first 2 bytes (length and descriptor type). */
|
---|
785 | string_size -= 2;
|
---|
786 |
|
---|
787 | /* Odd number of bytes - descriptor is broken? */
|
---|
788 | if ((string_size % 2) != 0) {
|
---|
789 | /* FIXME: shall we return with error or silently ignore? */
|
---|
790 | rc = ESTALL;
|
---|
791 | goto leave;
|
---|
792 | }
|
---|
793 |
|
---|
794 | size_t string_char_count = string_size / 2;
|
---|
795 | string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
---|
796 | if (string_chars == NULL) {
|
---|
797 | rc = ENOMEM;
|
---|
798 | goto leave;
|
---|
799 | }
|
---|
800 |
|
---|
801 | /*
|
---|
802 | * Build a wide string.
|
---|
803 | * And do not forget to set NULL terminator (string descriptors
|
---|
804 | * do not have them).
|
---|
805 | */
|
---|
806 | size_t i;
|
---|
807 | for (i = 0; i < string_char_count; i++) {
|
---|
808 | uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
---|
809 | + string[2 + 2 * i];
|
---|
810 | string_chars[i] = uni_char;
|
---|
811 | }
|
---|
812 | string_chars[string_char_count] = 0;
|
---|
813 |
|
---|
814 |
|
---|
815 | /* Convert to normal string. */
|
---|
816 | char *str = wstr_to_astr(string_chars);
|
---|
817 | if (str == NULL) {
|
---|
818 | rc = ENOMEM;
|
---|
819 | goto leave;
|
---|
820 | }
|
---|
821 |
|
---|
822 | *string_ptr = str;
|
---|
823 | rc = EOK;
|
---|
824 |
|
---|
825 | leave:
|
---|
826 | if (string != NULL) {
|
---|
827 | free(string);
|
---|
828 | }
|
---|
829 | if (string_chars != NULL) {
|
---|
830 | free(string_chars);
|
---|
831 | }
|
---|
832 |
|
---|
833 | return rc;
|
---|
834 | }
|
---|
835 |
|
---|
836 | /** Clear halt bit of an endpoint pipe (after pipe stall).
|
---|
837 | *
|
---|
838 | * @param pipe Control pipe.
|
---|
839 | * @param ep_index Endpoint index (in native endianness).
|
---|
840 | * @return Error code.
|
---|
841 | */
|
---|
842 | int usb_request_clear_endpoint_halt(usb_pipe_t *pipe, uint16_t ep_index)
|
---|
843 | {
|
---|
844 | return usb_request_clear_feature(pipe,
|
---|
845 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
|
---|
846 | uint16_host2usb(USB_FEATURE_SELECTOR_ENDPOINT_HALT),
|
---|
847 | uint16_host2usb(ep_index));
|
---|
848 | }
|
---|
849 |
|
---|
850 | /** Clear halt bit of an endpoint pipe (after pipe stall).
|
---|
851 | *
|
---|
852 | * @param ctrl_pipe Control pipe.
|
---|
853 | * @param target_pipe Which pipe is halted and shall be cleared.
|
---|
854 | * @return Error code.
|
---|
855 | */
|
---|
856 | int usb_pipe_clear_halt(usb_pipe_t *ctrl_pipe, usb_pipe_t *target_pipe)
|
---|
857 | {
|
---|
858 | if ((ctrl_pipe == NULL) || (target_pipe == NULL)) {
|
---|
859 | return EINVAL;
|
---|
860 | }
|
---|
861 | return usb_request_clear_endpoint_halt(ctrl_pipe,
|
---|
862 | target_pipe->endpoint_no);
|
---|
863 | }
|
---|
864 |
|
---|
865 | /** Get endpoint status.
|
---|
866 | *
|
---|
867 | * @param[in] ctrl_pipe Control pipe.
|
---|
868 | * @param[in] pipe Of which pipe the status shall be received.
|
---|
869 | * @param[out] status Where to store pipe status (in native endianness).
|
---|
870 | * @return Error code.
|
---|
871 | */
|
---|
872 | int usb_request_get_endpoint_status(usb_pipe_t *ctrl_pipe, usb_pipe_t *pipe,
|
---|
873 | uint16_t *status)
|
---|
874 | {
|
---|
875 | uint16_t status_tmp;
|
---|
876 | uint16_t pipe_index = (uint16_t) pipe->endpoint_no;
|
---|
877 | int rc = usb_request_get_status(ctrl_pipe,
|
---|
878 | USB_REQUEST_RECIPIENT_ENDPOINT, uint16_host2usb(pipe_index),
|
---|
879 | &status_tmp);
|
---|
880 | if (rc != EOK) {
|
---|
881 | return rc;
|
---|
882 | }
|
---|
883 |
|
---|
884 | if (status != NULL) {
|
---|
885 | *status = uint16_usb2host(status_tmp);
|
---|
886 | }
|
---|
887 |
|
---|
888 | return EOK;
|
---|
889 | }
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * @}
|
---|
893 | */
|
---|