1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
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 | /** @addtogroup drvusbohci
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief OHCI driver
|
---|
33 | */
|
---|
34 | #include <assert.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #include <str_error.h>
|
---|
37 |
|
---|
38 | #include <usb/debug.h>
|
---|
39 |
|
---|
40 | #include "root_hub.h"
|
---|
41 | #include <usb/classes/classes.h>
|
---|
42 | #include <usb/classes/hub.h>
|
---|
43 | #include <usb/dev/driver.h>
|
---|
44 | #include "ohci_regs.h"
|
---|
45 |
|
---|
46 | #include <usb/dev/request.h>
|
---|
47 | #include <usb/classes/hub.h>
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * standart device descriptor for ohci root hub
|
---|
51 | */
|
---|
52 | static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
|
---|
53 | .configuration_count = 1,
|
---|
54 | .descriptor_type = USB_DESCTYPE_DEVICE,
|
---|
55 | .device_class = USB_CLASS_HUB,
|
---|
56 | .device_protocol = 0,
|
---|
57 | .device_subclass = 0,
|
---|
58 | .device_version = 0,
|
---|
59 | .length = sizeof (usb_standard_device_descriptor_t),
|
---|
60 | .max_packet_size = 8,
|
---|
61 | .vendor_id = 0x16db,
|
---|
62 | .product_id = 0x0001,
|
---|
63 | .str_serial_number = 0,
|
---|
64 | .usb_spec_version = 0x110,
|
---|
65 | };
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * standart configuration descriptor with filled common values
|
---|
69 | * for ohci root hubs
|
---|
70 | */
|
---|
71 | static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
|
---|
72 | .attributes = 1 << 7,
|
---|
73 | .configuration_number = 1,
|
---|
74 | .descriptor_type = USB_DESCTYPE_CONFIGURATION,
|
---|
75 | .interface_count = 1,
|
---|
76 | .length = sizeof (usb_standard_configuration_descriptor_t),
|
---|
77 | .max_power = 0, /* root hubs don't need no power */
|
---|
78 | .str_configuration = 0,
|
---|
79 | };
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * standart ohci root hub interface descriptor
|
---|
83 | */
|
---|
84 | static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
|
---|
85 | .alternate_setting = 0,
|
---|
86 | .descriptor_type = USB_DESCTYPE_INTERFACE,
|
---|
87 | .endpoint_count = 1,
|
---|
88 | .interface_class = USB_CLASS_HUB,
|
---|
89 | .interface_number = 1,
|
---|
90 | .interface_protocol = 0,
|
---|
91 | .interface_subclass = 0,
|
---|
92 | .length = sizeof (usb_standard_interface_descriptor_t),
|
---|
93 | .str_interface = 0,
|
---|
94 | };
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * standart ohci root hub endpoint descriptor
|
---|
98 | */
|
---|
99 | static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
|
---|
100 | .attributes = USB_TRANSFER_INTERRUPT,
|
---|
101 | .descriptor_type = USB_DESCTYPE_ENDPOINT,
|
---|
102 | .endpoint_address = 1 + (1 << 7),
|
---|
103 | .length = sizeof (usb_standard_endpoint_descriptor_t),
|
---|
104 | .max_packet_size = 8,
|
---|
105 | .poll_interval = 255,
|
---|
106 | };
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * bitmask of hub features that are valid to be set
|
---|
110 | */
|
---|
111 | static const uint32_t hub_set_feature_valid_mask =
|
---|
112 | RHS_LPSC_FLAG |
|
---|
113 | RHS_OCIC_FLAG;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * bitmask of hub features that are set by writing 1 and cleared by writing 0
|
---|
117 | */
|
---|
118 | static const uint32_t hub_set_feature_direct_mask =
|
---|
119 | RHS_SET_PORT_POWER;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * bitmask of port features that are valid to be set
|
---|
123 | */
|
---|
124 | static const uint32_t port_set_feature_valid_mask =
|
---|
125 | RHPS_SET_PORT_ENABLE |
|
---|
126 | RHPS_SET_PORT_SUSPEND |
|
---|
127 | RHPS_SET_PORT_RESET |
|
---|
128 | RHPS_SET_PORT_POWER;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * bitmask of port features that can be cleared
|
---|
132 | */
|
---|
133 | static const uint32_t port_clear_feature_valid_mask =
|
---|
134 | RHPS_CCS_FLAG |
|
---|
135 | RHPS_SET_PORT_SUSPEND |
|
---|
136 | RHPS_POCI_FLAG |
|
---|
137 | RHPS_SET_PORT_POWER |
|
---|
138 | RHPS_CSC_FLAG |
|
---|
139 | RHPS_PESC_FLAG |
|
---|
140 | RHPS_PSSC_FLAG |
|
---|
141 | RHPS_OCIC_FLAG |
|
---|
142 | RHPS_PRSC_FLAG;
|
---|
143 |
|
---|
144 | //note that USB_HUB_FEATURE_PORT_POWER bit is translated into
|
---|
145 | //USB_HUB_FEATURE_PORT_LOW_SPEED for port set feature request
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * bitmask with port status changes
|
---|
149 | */
|
---|
150 | static const uint32_t port_status_change_mask = RHPS_CHANGE_WC_MASK;
|
---|
151 |
|
---|
152 | static void create_serialized_hub_descriptor(rh_t *instance);
|
---|
153 |
|
---|
154 | static void rh_init_descriptors(rh_t *instance);
|
---|
155 |
|
---|
156 | static void create_interrupt_mask_in_instance(rh_t *instance);
|
---|
157 |
|
---|
158 | static int get_status_request(
|
---|
159 | rh_t *instance, usb_transfer_batch_t *request);
|
---|
160 |
|
---|
161 | static int get_descriptor_request(
|
---|
162 | rh_t *instance, usb_transfer_batch_t *request);
|
---|
163 |
|
---|
164 | static int port_feature_set_request(
|
---|
165 | rh_t *instance, uint16_t feature, uint16_t port);
|
---|
166 |
|
---|
167 | static int port_feature_clear_request(
|
---|
168 | rh_t *instance, uint16_t feature, uint16_t port);
|
---|
169 |
|
---|
170 | static int request_with_output(
|
---|
171 | rh_t *instance, usb_transfer_batch_t *request);
|
---|
172 |
|
---|
173 | static int request_without_data(
|
---|
174 | rh_t *instance, usb_transfer_batch_t *request);
|
---|
175 |
|
---|
176 | static int ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
|
---|
177 |
|
---|
178 | static int interrupt_mask_in_instance(
|
---|
179 | rh_t *instance, usb_transfer_batch_t *request);
|
---|
180 |
|
---|
181 | static bool is_zeros(const void *buffer, size_t size);
|
---|
182 |
|
---|
183 |
|
---|
184 | #define TRANSFER_OK(bytes) \
|
---|
185 | do { \
|
---|
186 | request->transfered_size = bytes; \
|
---|
187 | return EOK; \
|
---|
188 | } while (0)
|
---|
189 |
|
---|
190 | /** Root hub initialization
|
---|
191 | * @return Error code.
|
---|
192 | */
|
---|
193 | void rh_init(rh_t *instance, ohci_regs_t *regs)
|
---|
194 | {
|
---|
195 | assert(instance);
|
---|
196 | assert(regs);
|
---|
197 |
|
---|
198 | instance->registers = regs;
|
---|
199 | instance->port_count =
|
---|
200 | (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
|
---|
201 | if (instance->port_count > 15) {
|
---|
202 | usb_log_error("OHCI specification does not allow more than 15"
|
---|
203 | " ports. Max 15 ports will be used");
|
---|
204 | instance->port_count = 15;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* Don't forget the hub status bit and round up */
|
---|
208 | instance->interrupt_mask_size = (instance->port_count + 1 + 8) / 8;
|
---|
209 | instance->interrupt_buffer[0] = 0;
|
---|
210 | instance->interrupt_buffer[1] = 0;
|
---|
211 | instance->unfinished_interrupt_transfer = NULL;
|
---|
212 |
|
---|
213 | /* Set port power mode to no power-switching. (always on) */
|
---|
214 | instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
|
---|
215 |
|
---|
216 | rh_init_descriptors(instance);
|
---|
217 |
|
---|
218 | usb_log_info("Root hub (%zu ports) initialized.\n",
|
---|
219 | instance->port_count);
|
---|
220 | }
|
---|
221 | /*----------------------------------------------------------------------------*/
|
---|
222 | /**
|
---|
223 | * Process root hub request.
|
---|
224 | *
|
---|
225 | * @param instance Root hub instance
|
---|
226 | * @param request Structure containing both request and response information
|
---|
227 | * @return Error code
|
---|
228 | */
|
---|
229 | int rh_request(rh_t *instance, usb_transfer_batch_t *request)
|
---|
230 | {
|
---|
231 | assert(instance);
|
---|
232 | assert(request);
|
---|
233 |
|
---|
234 | int opResult;
|
---|
235 | switch (request->ep->transfer_type)
|
---|
236 | {
|
---|
237 | case USB_TRANSFER_CONTROL:
|
---|
238 | usb_log_debug("Root hub got CONTROL packet\n");
|
---|
239 | opResult = ctrl_request(instance, request);
|
---|
240 | usb_transfer_batch_finish_error(request, opResult);
|
---|
241 | break;
|
---|
242 | case USB_TRANSFER_INTERRUPT:
|
---|
243 | usb_log_debug("Root hub got INTERRUPT packet\n");
|
---|
244 | create_interrupt_mask_in_instance(instance);
|
---|
245 | if (is_zeros(instance->interrupt_buffer,
|
---|
246 | instance->interrupt_mask_size)) {
|
---|
247 | usb_log_debug("No changes..\n");
|
---|
248 | instance->unfinished_interrupt_transfer = request;
|
---|
249 | //will be finished later
|
---|
250 | } else {
|
---|
251 | usb_log_debug("Processing changes..\n");
|
---|
252 | interrupt_mask_in_instance(instance, request);
|
---|
253 | }
|
---|
254 | break;
|
---|
255 | default:
|
---|
256 | usb_log_error("Root hub got unsupported request.\n");
|
---|
257 | usb_transfer_batch_finish_error(request, EINVAL);
|
---|
258 | }
|
---|
259 | return EOK;
|
---|
260 | }
|
---|
261 | /*----------------------------------------------------------------------------*/
|
---|
262 | /**
|
---|
263 | * process interrupt on a hub
|
---|
264 | *
|
---|
265 | * If there is no pending interrupt transfer, nothing happens.
|
---|
266 | * @param instance
|
---|
267 | */
|
---|
268 | void rh_interrupt(rh_t *instance)
|
---|
269 | {
|
---|
270 | if (!instance->unfinished_interrupt_transfer)
|
---|
271 | return;
|
---|
272 |
|
---|
273 | usb_log_debug("Finalizing interrupt transfer\n");
|
---|
274 | create_interrupt_mask_in_instance(instance);
|
---|
275 | interrupt_mask_in_instance(instance,
|
---|
276 | instance->unfinished_interrupt_transfer);
|
---|
277 | }
|
---|
278 | /*----------------------------------------------------------------------------*/
|
---|
279 | /**
|
---|
280 | * Create hub descriptor.
|
---|
281 | *
|
---|
282 | * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
|
---|
283 | *
|
---|
284 | * @param instance Root hub instance
|
---|
285 | * @return Error code
|
---|
286 | */
|
---|
287 | void create_serialized_hub_descriptor(rh_t *instance)
|
---|
288 | {
|
---|
289 | assert(instance);
|
---|
290 |
|
---|
291 | const size_t bit_field_size = (instance->port_count + 1 + 7) / 8;
|
---|
292 | assert(bit_field_size == 2 || bit_field_size == 1);
|
---|
293 | /* 7 bytes + 2 port bit fields (port count + global bit) */
|
---|
294 | const size_t size = 7 + (bit_field_size * 2);
|
---|
295 | assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
|
---|
296 | instance->hub_descriptor_size = size;
|
---|
297 |
|
---|
298 | const uint32_t hub_desc = instance->registers->rh_desc_a;
|
---|
299 | const uint32_t port_desc = instance->registers->rh_desc_b;
|
---|
300 |
|
---|
301 | /* bDescLength */
|
---|
302 | instance->descriptors.hub[0] = size;
|
---|
303 | /* bDescriptorType */
|
---|
304 | instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
|
---|
305 | /* bNmbrPorts */
|
---|
306 | instance->descriptors.hub[2] = instance->port_count;
|
---|
307 | /* wHubCharacteristics */
|
---|
308 | instance->descriptors.hub[3] = 0 |
|
---|
309 | /* The lowest 2 bits indicate power switching mode */
|
---|
310 | (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
|
---|
311 | (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
|
---|
312 | /* Bit 3 indicates device type (compound device) */
|
---|
313 | (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
|
---|
314 | /* Bits 4,5 indicate over-current protection mode */
|
---|
315 | (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
|
---|
316 | (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
|
---|
317 |
|
---|
318 | /* Reserved */
|
---|
319 | instance->descriptors.hub[4] = 0;
|
---|
320 | /* bPwrOn2PwrGood */
|
---|
321 | instance->descriptors.hub[5] =
|
---|
322 | (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
|
---|
323 | /* bHubContrCurrent, root hubs don't need no power. */
|
---|
324 | instance->descriptors.hub[6] = 0;
|
---|
325 |
|
---|
326 | /* Device Removable and some legacy 1.0 stuff*/
|
---|
327 | instance->descriptors.hub[7] =
|
---|
328 | (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
|
---|
329 | instance->descriptors.hub[8] = 0xff;
|
---|
330 | if (bit_field_size == 2) {
|
---|
331 | instance->descriptors.hub[8] =
|
---|
332 | (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
|
---|
333 | instance->descriptors.hub[9] = 0xff;
|
---|
334 | instance->descriptors.hub[10] = 0xff;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | /*----------------------------------------------------------------------------*/
|
---|
338 | /** Initialize hub descriptors.
|
---|
339 | *
|
---|
340 | * Device and full configuration descriptor are created. These need to
|
---|
341 | * be initialized only once per hub.
|
---|
342 | * @param instance Root hub instance
|
---|
343 | * @return Error code
|
---|
344 | */
|
---|
345 | void rh_init_descriptors(rh_t *instance)
|
---|
346 | {
|
---|
347 | assert(instance);
|
---|
348 |
|
---|
349 | instance->descriptors.configuration = ohci_rh_conf_descriptor;
|
---|
350 | instance->descriptors.interface = ohci_rh_iface_descriptor;
|
---|
351 | instance->descriptors.endpoint = ohci_rh_ep_descriptor;
|
---|
352 | create_serialized_hub_descriptor(instance);
|
---|
353 |
|
---|
354 | instance->descriptors.configuration.total_length =
|
---|
355 | sizeof(usb_standard_configuration_descriptor_t) +
|
---|
356 | sizeof(usb_standard_endpoint_descriptor_t) +
|
---|
357 | sizeof(usb_standard_interface_descriptor_t) +
|
---|
358 | instance->hub_descriptor_size;
|
---|
359 | }
|
---|
360 | /*----------------------------------------------------------------------------*/
|
---|
361 | /**
|
---|
362 | * Create answer to status request.
|
---|
363 | *
|
---|
364 | * This might be either hub status or port status request. If neither,
|
---|
365 | * ENOTSUP is returned.
|
---|
366 | * @param instance root hub instance
|
---|
367 | * @param request structure containing both request and response information
|
---|
368 | * @return error code
|
---|
369 | */
|
---|
370 | int get_status_request(rh_t *instance, usb_transfer_batch_t *request)
|
---|
371 | {
|
---|
372 | assert(instance);
|
---|
373 | assert(request);
|
---|
374 |
|
---|
375 | const usb_device_request_setup_packet_t *request_packet =
|
---|
376 | (usb_device_request_setup_packet_t*)request->setup_buffer;
|
---|
377 |
|
---|
378 | if (request->buffer_size < 4) {
|
---|
379 | usb_log_error("Buffer too small for get status request.\n");
|
---|
380 | return EOVERFLOW;
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS) {
|
---|
384 | const uint32_t data = instance->registers->rh_status &
|
---|
385 | (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
|
---|
386 | memcpy(request->data_buffer, &data, 4);
|
---|
387 | TRANSFER_OK(4);
|
---|
388 | }
|
---|
389 |
|
---|
390 | if (request_packet->request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS) {
|
---|
391 | unsigned port = request_packet->index;
|
---|
392 | if (port < 1 || port > instance->port_count)
|
---|
393 | return EINVAL;
|
---|
394 |
|
---|
395 | const uint32_t data =
|
---|
396 | instance->registers->rh_port_status[port - 1];
|
---|
397 | memcpy(request->data_buffer, &data, 4);
|
---|
398 | TRANSFER_OK(4);
|
---|
399 | }
|
---|
400 |
|
---|
401 | return ENOTSUP;
|
---|
402 | }
|
---|
403 | /*----------------------------------------------------------------------------*/
|
---|
404 | /**
|
---|
405 | * Create bitmap of changes to answer status interrupt.
|
---|
406 | *
|
---|
407 | * Result contains bitmap where bit 0 indicates change on hub and
|
---|
408 | * bit i indicates change on i`th port (i>0). For more info see
|
---|
409 | * Hub and Port status bitmap specification in USB specification
|
---|
410 | * (chapter 11.13.4).
|
---|
411 | * Uses instance`s interrupt buffer to store the interrupt information.
|
---|
412 | * @param instance root hub instance
|
---|
413 | */
|
---|
414 | void create_interrupt_mask_in_instance(rh_t *instance)
|
---|
415 | {
|
---|
416 | assert(instance);
|
---|
417 |
|
---|
418 | uint8_t * bitmap = instance->interrupt_buffer;
|
---|
419 | uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
|
---|
420 | | (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
|
---|
421 | bzero(bitmap, instance->interrupt_mask_size);
|
---|
422 | if ((instance->registers->rh_status & mask) != 0) {
|
---|
423 | bitmap[0] = 1;
|
---|
424 | }
|
---|
425 | mask = port_status_change_mask;
|
---|
426 | size_t port = 1;
|
---|
427 | for (; port <= instance->port_count; ++port) {
|
---|
428 | if ((mask & instance->registers->rh_port_status[port - 1]) != 0) {
|
---|
429 |
|
---|
430 | bitmap[(port) / 8] += 1 << (port % 8);
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 | /*----------------------------------------------------------------------------*/
|
---|
435 | /**
|
---|
436 | * Create answer to a descriptor request.
|
---|
437 | *
|
---|
438 | * This might be a request for standard (configuration, device, endpoint or
|
---|
439 | * interface) or device specific (hub) descriptor.
|
---|
440 | * @param instance Root hub instance
|
---|
441 | * @param request Structure containing both request and response information
|
---|
442 | * @return Error code
|
---|
443 | */
|
---|
444 | int get_descriptor_request(
|
---|
445 | rh_t *instance, usb_transfer_batch_t *request)
|
---|
446 | {
|
---|
447 | assert(instance);
|
---|
448 | assert(request);
|
---|
449 |
|
---|
450 | const usb_device_request_setup_packet_t *setup_request =
|
---|
451 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
452 | size_t size;
|
---|
453 | const void * result_descriptor = NULL;
|
---|
454 | const uint16_t setup_request_value = setup_request->value_high;
|
---|
455 | //(setup_request->value_low << 8);
|
---|
456 | switch (setup_request_value)
|
---|
457 | {
|
---|
458 | case USB_DESCTYPE_HUB:
|
---|
459 | usb_log_debug2("USB_DESCTYPE_HUB\n");
|
---|
460 | result_descriptor = instance->descriptors.hub;
|
---|
461 | size = instance->hub_descriptor_size;
|
---|
462 | break;
|
---|
463 |
|
---|
464 | case USB_DESCTYPE_DEVICE:
|
---|
465 | usb_log_debug2("USB_DESCTYPE_DEVICE\n");
|
---|
466 | result_descriptor = &ohci_rh_device_descriptor;
|
---|
467 | size = sizeof(ohci_rh_device_descriptor);
|
---|
468 | break;
|
---|
469 |
|
---|
470 | case USB_DESCTYPE_CONFIGURATION:
|
---|
471 | usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
|
---|
472 | result_descriptor = &instance->descriptors;
|
---|
473 | size = instance->descriptors.configuration.total_length;
|
---|
474 | break;
|
---|
475 |
|
---|
476 | case USB_DESCTYPE_INTERFACE:
|
---|
477 | usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
|
---|
478 | result_descriptor = &ohci_rh_iface_descriptor;
|
---|
479 | size = sizeof(ohci_rh_iface_descriptor);
|
---|
480 | break;
|
---|
481 |
|
---|
482 | case USB_DESCTYPE_ENDPOINT:
|
---|
483 | usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
|
---|
484 | result_descriptor = &ohci_rh_ep_descriptor;
|
---|
485 | size = sizeof(ohci_rh_ep_descriptor);
|
---|
486 | break;
|
---|
487 |
|
---|
488 | default:
|
---|
489 | usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
|
---|
490 | "\ttype %d\n\trequest %d\n\tvalue "
|
---|
491 | "%d\n\tindex %d\n\tlen %d\n ",
|
---|
492 | setup_request->value,
|
---|
493 | setup_request->request_type, setup_request->request,
|
---|
494 | setup_request_value, setup_request->index,
|
---|
495 | setup_request->length);
|
---|
496 | return EINVAL;
|
---|
497 | }
|
---|
498 | if (request->buffer_size < size) {
|
---|
499 | size = request->buffer_size;
|
---|
500 | }
|
---|
501 |
|
---|
502 | memcpy(request->data_buffer, result_descriptor, size);
|
---|
503 | TRANSFER_OK(size);
|
---|
504 | }
|
---|
505 | /*----------------------------------------------------------------------------*/
|
---|
506 | /**
|
---|
507 | * process feature-enabling request on hub
|
---|
508 | *
|
---|
509 | * @param instance root hub instance
|
---|
510 | * @param feature feature selector
|
---|
511 | * @param port port number, counted from 1
|
---|
512 | * @param enable enable or disable the specified feature
|
---|
513 | * @return error code
|
---|
514 | */
|
---|
515 | int port_feature_set_request(rh_t *instance, uint16_t feature, uint16_t port)
|
---|
516 | {
|
---|
517 | assert(instance);
|
---|
518 |
|
---|
519 | if (!((1 << feature) & port_set_feature_valid_mask))
|
---|
520 | return EINVAL;
|
---|
521 | if (port < 1 || port > instance->port_count)
|
---|
522 | return EINVAL;
|
---|
523 |
|
---|
524 | instance->registers->rh_port_status[port - 1] =
|
---|
525 | (instance->registers->rh_port_status[port - 1] | (1 << feature))
|
---|
526 | & (~port_clear_feature_valid_mask);
|
---|
527 | return EOK;
|
---|
528 | }
|
---|
529 | /*----------------------------------------------------------------------------*/
|
---|
530 | /**
|
---|
531 | * process feature-disabling request on hub
|
---|
532 | *
|
---|
533 | * @param instance root hub instance
|
---|
534 | * @param feature feature selector
|
---|
535 | * @param port port number, counted from 1
|
---|
536 | * @param enable enable or disable the specified feature
|
---|
537 | * @return error code
|
---|
538 | */
|
---|
539 | int port_feature_clear_request(
|
---|
540 | rh_t *instance, uint16_t feature, uint16_t port)
|
---|
541 | {
|
---|
542 | assert(instance);
|
---|
543 |
|
---|
544 | if (!((1 << feature) & port_clear_feature_valid_mask))
|
---|
545 | return EINVAL;
|
---|
546 | if (port < 1 || port > instance->port_count)
|
---|
547 | return EINVAL;
|
---|
548 |
|
---|
549 | /* Some weird stuff... */
|
---|
550 | if (feature == USB_HUB_FEATURE_PORT_POWER)
|
---|
551 | feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
|
---|
552 | if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
|
---|
553 | feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
|
---|
554 |
|
---|
555 | instance->registers->rh_port_status[port - 1] =
|
---|
556 | (instance->registers->rh_port_status[port - 1]
|
---|
557 | & (~port_clear_feature_valid_mask))
|
---|
558 | | (1 << feature);
|
---|
559 | return EOK;
|
---|
560 | }
|
---|
561 | /*----------------------------------------------------------------------------*/
|
---|
562 | /**
|
---|
563 | * Process a request that requires output data.
|
---|
564 | *
|
---|
565 | * Request can be one of USB_DEVREQ_GET_STATUS, USB_DEVREQ_GET_DESCRIPTOR or
|
---|
566 | * USB_DEVREQ_GET_CONFIGURATION.
|
---|
567 | * @param instance root hub instance
|
---|
568 | * @param request structure containing both request and response information
|
---|
569 | * @return error code
|
---|
570 | */
|
---|
571 | int request_with_output(rh_t *instance, usb_transfer_batch_t *request)
|
---|
572 | {
|
---|
573 | assert(instance);
|
---|
574 | assert(request);
|
---|
575 |
|
---|
576 | const usb_device_request_setup_packet_t *setup_request =
|
---|
577 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
578 | switch (setup_request->request)
|
---|
579 | {
|
---|
580 | case USB_DEVREQ_GET_STATUS:
|
---|
581 | usb_log_debug("USB_DEVREQ_GET_STATUS\n");
|
---|
582 | return get_status_request(instance, request);
|
---|
583 | case USB_DEVREQ_GET_DESCRIPTOR:
|
---|
584 | usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
|
---|
585 | return get_descriptor_request(instance, request);
|
---|
586 | case USB_DEVREQ_GET_CONFIGURATION:
|
---|
587 | usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
|
---|
588 | if (request->buffer_size != 1)
|
---|
589 | return EINVAL;
|
---|
590 | request->data_buffer[0] = 1;
|
---|
591 | TRANSFER_OK(1);
|
---|
592 | }
|
---|
593 | return ENOTSUP;
|
---|
594 | }
|
---|
595 | /*----------------------------------------------------------------------------*/
|
---|
596 | /**
|
---|
597 | * process one of requests that do not request nor carry additional data
|
---|
598 | *
|
---|
599 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
---|
600 | * USB_DEVREQ_SET_ADDRESS.
|
---|
601 | * @param instance root hub instance
|
---|
602 | * @param request structure containing both request and response information
|
---|
603 | * @return error code
|
---|
604 | */
|
---|
605 | int request_without_data(rh_t *instance, usb_transfer_batch_t *request)
|
---|
606 | {
|
---|
607 | assert(instance);
|
---|
608 | assert(request);
|
---|
609 |
|
---|
610 | const usb_device_request_setup_packet_t *setup_request =
|
---|
611 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
612 | request->transfered_size = 0;
|
---|
613 | const int request_type = setup_request->request_type;
|
---|
614 | switch (setup_request->request)
|
---|
615 | {
|
---|
616 | case USB_DEVREQ_CLEAR_FEATURE:
|
---|
617 | if (request_type == USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE) {
|
---|
618 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
|
---|
619 | return port_feature_clear_request(instance,
|
---|
620 | setup_request->value, setup_request->index);
|
---|
621 | }
|
---|
622 | if (request_type == USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE) {
|
---|
623 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE\n");
|
---|
624 | /*
|
---|
625 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
626 | * C_HUB_OVER_CURRENT are supported. C_HUB_OVER_CURRENT is represented
|
---|
627 | * by OHCI RHS_OCIC_FLAG. C_HUB_LOCAL_POWER is not supported
|
---|
628 | * as root hubs do not support local power status feature.
|
---|
629 | * (OHCI pg. 127) */
|
---|
630 | if (setup_request->value == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
|
---|
631 | instance->registers->rh_status = RHS_OCIC_FLAG;
|
---|
632 | return EOK;
|
---|
633 | }
|
---|
634 | }
|
---|
635 | usb_log_error("Invalid clear feature request type: %d\n",
|
---|
636 | request_type);
|
---|
637 | return EINVAL;
|
---|
638 |
|
---|
639 | case USB_DEVREQ_SET_FEATURE:
|
---|
640 | switch (request_type)
|
---|
641 | {
|
---|
642 | case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
|
---|
643 | usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
|
---|
644 | return port_feature_set_request(instance,
|
---|
645 | setup_request->value, setup_request->index);
|
---|
646 |
|
---|
647 | case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
|
---|
648 | /* Chapter 11.16.2 specifies that hub can be recipient
|
---|
649 | * only for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
|
---|
650 | * features. It makes no sense to SET either. */
|
---|
651 | usb_log_error("Invalid HUB set feature request.\n");
|
---|
652 | return ENOTSUP;
|
---|
653 | default:
|
---|
654 | usb_log_error("Invalid set feature request type: %d\n",
|
---|
655 | request_type);
|
---|
656 | return EINVAL;
|
---|
657 | }
|
---|
658 |
|
---|
659 | case USB_DEVREQ_SET_ADDRESS:
|
---|
660 | usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
|
---|
661 | return ENOTSUP;
|
---|
662 |
|
---|
663 | default:
|
---|
664 | usb_log_error("Invalid HUB request: %d\n",
|
---|
665 | setup_request->request);
|
---|
666 | return ENOTSUP;
|
---|
667 | }
|
---|
668 | }
|
---|
669 | /*----------------------------------------------------------------------------*/
|
---|
670 | /**
|
---|
671 | * Process hub control request.
|
---|
672 | *
|
---|
673 | * If needed, writes answer into the request structure.
|
---|
674 | * Request can be one of
|
---|
675 | * USB_DEVREQ_GET_STATUS,
|
---|
676 | * USB_DEVREQ_GET_DESCRIPTOR,
|
---|
677 | * USB_DEVREQ_GET_CONFIGURATION,
|
---|
678 | * USB_DEVREQ_CLEAR_FEATURE,
|
---|
679 | * USB_DEVREQ_SET_FEATURE,
|
---|
680 | * USB_DEVREQ_SET_ADDRESS,
|
---|
681 | * USB_DEVREQ_SET_DESCRIPTOR or
|
---|
682 | * USB_DEVREQ_SET_CONFIGURATION.
|
---|
683 | *
|
---|
684 | * @param instance root hub instance
|
---|
685 | * @param request structure containing both request and response information
|
---|
686 | * @return error code
|
---|
687 | */
|
---|
688 | int ctrl_request(rh_t *instance, usb_transfer_batch_t *request)
|
---|
689 | {
|
---|
690 | assert(instance);
|
---|
691 | assert(request);
|
---|
692 |
|
---|
693 | if (!request->setup_buffer) {
|
---|
694 | usb_log_error("Root hub received empty transaction!");
|
---|
695 | return EINVAL;
|
---|
696 | }
|
---|
697 | if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
|
---|
698 | usb_log_error("Setup packet too small\n");
|
---|
699 | return EOVERFLOW;
|
---|
700 | }
|
---|
701 | usb_log_debug2("CTRL packet: %s.\n",
|
---|
702 | usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
|
---|
703 | const usb_device_request_setup_packet_t *setup_request =
|
---|
704 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
705 | switch (setup_request->request)
|
---|
706 | {
|
---|
707 | case USB_DEVREQ_GET_STATUS:
|
---|
708 | case USB_DEVREQ_GET_DESCRIPTOR:
|
---|
709 | case USB_DEVREQ_GET_CONFIGURATION:
|
---|
710 | usb_log_debug2("Processing request with output\n");
|
---|
711 | return request_with_output(instance, request);
|
---|
712 | case USB_DEVREQ_CLEAR_FEATURE:
|
---|
713 | case USB_DEVREQ_SET_FEATURE:
|
---|
714 | case USB_DEVREQ_SET_ADDRESS:
|
---|
715 | usb_log_debug2("Processing request without "
|
---|
716 | "additional data\n");
|
---|
717 | return request_without_data(instance, request);
|
---|
718 | case USB_DEVREQ_SET_CONFIGURATION:
|
---|
719 | usb_log_debug2("Processing request with input\n");
|
---|
720 | /* We don't need to do anything */
|
---|
721 | TRANSFER_OK(0);
|
---|
722 | case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
|
---|
723 | default:
|
---|
724 | usb_log_error("Received unsupported request: %d.\n",
|
---|
725 | setup_request->request);
|
---|
726 | return ENOTSUP;
|
---|
727 | }
|
---|
728 | }
|
---|
729 | /*----------------------------------------------------------------------------*/
|
---|
730 | /**
|
---|
731 | * process hanging interrupt request
|
---|
732 | *
|
---|
733 | * If an interrupt transfer has been received and there was no change,
|
---|
734 | * the driver stores the transfer information and waits for change to occur.
|
---|
735 | * This routine is called when that happens and it finalizes the interrupt
|
---|
736 | * transfer.
|
---|
737 | *
|
---|
738 | * @param instance hub instance
|
---|
739 | * @param request batch request to be processed
|
---|
740 | *
|
---|
741 | * @return
|
---|
742 | */
|
---|
743 | int interrupt_mask_in_instance(
|
---|
744 | rh_t *instance, usb_transfer_batch_t *request)
|
---|
745 | {
|
---|
746 | assert(instance);
|
---|
747 | assert(request);
|
---|
748 |
|
---|
749 | memcpy(request->data_buffer, instance->interrupt_buffer,
|
---|
750 | instance->interrupt_mask_size);
|
---|
751 | request->transfered_size = instance->interrupt_mask_size;
|
---|
752 | instance->unfinished_interrupt_transfer = NULL;
|
---|
753 | usb_transfer_batch_finish_error(request, EOK);
|
---|
754 |
|
---|
755 | return EOK;
|
---|
756 | }
|
---|
757 | /*----------------------------------------------------------------------------*/
|
---|
758 | /**
|
---|
759 | * return whether the buffer is full of zeros
|
---|
760 | *
|
---|
761 | * Convenience function.
|
---|
762 | * @param buffer
|
---|
763 | * @param size
|
---|
764 | * @return
|
---|
765 | */
|
---|
766 | bool is_zeros(const void *buffer, size_t size)
|
---|
767 | {
|
---|
768 | if (!buffer) return true;
|
---|
769 | const char * const end = buffer + size;
|
---|
770 | const char *data = buffer;
|
---|
771 | for (; data < end; ++data) {
|
---|
772 | if (*data)
|
---|
773 | return false;
|
---|
774 | }
|
---|
775 | return true;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * @}
|
---|
780 | */
|
---|