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 | #include <fibril_synch.h>
|
---|
38 |
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usb/dev/request.h>
|
---|
41 | #include <usb/classes/hub.h>
|
---|
42 |
|
---|
43 | #include "root_hub.h"
|
---|
44 | #include <usb/classes/classes.h>
|
---|
45 | #include <usb/classes/hub.h>
|
---|
46 | #include <usb/dev/driver.h>
|
---|
47 | #include "ohci_regs.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 = 64,
|
---|
61 | .vendor_id = 0x16db, /* HelenOS does not have USB vendor ID assigned.*/
|
---|
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 = 2,
|
---|
105 | .poll_interval = 255,
|
---|
106 | };
|
---|
107 |
|
---|
108 | static void create_serialized_hub_descriptor(rh_t *instance);
|
---|
109 | static void rh_init_descriptors(rh_t *instance);
|
---|
110 | static uint16_t create_interrupt_mask(const rh_t *instance);
|
---|
111 | static void get_status(const rh_t *instance, usb_transfer_batch_t *request);
|
---|
112 | static void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request);
|
---|
113 | static void set_feature(const rh_t *instance, usb_transfer_batch_t *request);
|
---|
114 | static void clear_feature(const rh_t *instance, usb_transfer_batch_t *request);
|
---|
115 | static int set_feature_port(
|
---|
116 | const rh_t *instance, uint16_t feature, uint16_t port);
|
---|
117 | static int clear_feature_port(
|
---|
118 | const rh_t *instance, uint16_t feature, uint16_t port);
|
---|
119 | static void control_request(rh_t *instance, usb_transfer_batch_t *request);
|
---|
120 | static inline void interrupt_request(
|
---|
121 | usb_transfer_batch_t *request, uint16_t mask, size_t size)
|
---|
122 | {
|
---|
123 | assert(request);
|
---|
124 | usb_transfer_batch_finish_error(request, &mask, size, EOK);
|
---|
125 | usb_transfer_batch_destroy(request);
|
---|
126 | }
|
---|
127 |
|
---|
128 | #define TRANSFER_END_DATA(request, data, bytes) \
|
---|
129 | do { \
|
---|
130 | usb_transfer_batch_finish_error(request, data, bytes, EOK); \
|
---|
131 | usb_transfer_batch_destroy(request); \
|
---|
132 | return; \
|
---|
133 | } while (0)
|
---|
134 |
|
---|
135 | #define TRANSFER_END(request, error) \
|
---|
136 | do { \
|
---|
137 | usb_transfer_batch_finish_error(request, NULL, 0, error); \
|
---|
138 | usb_transfer_batch_destroy(request); \
|
---|
139 | return; \
|
---|
140 | } while (0)
|
---|
141 |
|
---|
142 | /** Root Hub driver structure initialization.
|
---|
143 | *
|
---|
144 | * Reads info registers and prepares descriptors. Sets power mode.
|
---|
145 | */
|
---|
146 | void rh_init(rh_t *instance, ohci_regs_t *regs)
|
---|
147 | {
|
---|
148 | assert(instance);
|
---|
149 | assert(regs);
|
---|
150 |
|
---|
151 | instance->registers = regs;
|
---|
152 | instance->port_count =
|
---|
153 | (instance->registers->rh_desc_a >> RHDA_NDS_SHIFT) & RHDA_NDS_MASK;
|
---|
154 | if (instance->port_count > 15) {
|
---|
155 | usb_log_warning("OHCI specification does not allow more than 15"
|
---|
156 | " ports. Max 15 ports will be used");
|
---|
157 | instance->port_count = 15;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* Don't forget the hub status bit and round up */
|
---|
161 | instance->interrupt_mask_size = 1 + (instance->port_count / 8);
|
---|
162 | instance->unfinished_interrupt_transfer = NULL;
|
---|
163 |
|
---|
164 | #if defined OHCI_POWER_SWITCH_no
|
---|
165 | /* Set port power mode to no power-switching. (always on) */
|
---|
166 | instance->registers->rh_desc_a |= RHDA_NPS_FLAG;
|
---|
167 |
|
---|
168 | /* Set to no over-current reporting */
|
---|
169 | instance->registers->rh_desc_a |= RHDA_NOCP_FLAG;
|
---|
170 |
|
---|
171 | #elif defined OHCI_POWER_SWITCH_ganged
|
---|
172 | /* Set port power mode to no ganged power-switching. */
|
---|
173 | instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
|
---|
174 | instance->registers->rh_desc_a &= ~RHDA_PSM_FLAG;
|
---|
175 | instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
|
---|
176 |
|
---|
177 | /* Set to global over-current */
|
---|
178 | instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
|
---|
179 | instance->registers->rh_desc_a &= ~RHDA_OCPM_FLAG;
|
---|
180 | #else
|
---|
181 | /* Set port power mode to no per port power-switching. */
|
---|
182 | instance->registers->rh_desc_a &= ~RHDA_NPS_FLAG;
|
---|
183 | instance->registers->rh_desc_a |= RHDA_PSM_FLAG;
|
---|
184 |
|
---|
185 | /* Control all ports by global switch and turn them off */
|
---|
186 | instance->registers->rh_desc_b &= (RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
---|
187 | instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
|
---|
188 |
|
---|
189 | /* Return control to per port state */
|
---|
190 | instance->registers->rh_desc_b |=
|
---|
191 | ((1 << (instance->port_count + 1)) - 1) << RHDB_PCC_SHIFT;
|
---|
192 |
|
---|
193 | /* Set per port over-current */
|
---|
194 | instance->registers->rh_desc_a &= ~RHDA_NOCP_FLAG;
|
---|
195 | instance->registers->rh_desc_a |= RHDA_OCPM_FLAG;
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | fibril_mutex_initialize(&instance->guard);
|
---|
199 | rh_init_descriptors(instance);
|
---|
200 |
|
---|
201 | usb_log_info("Root hub (%zu ports) initialized.\n",
|
---|
202 | instance->port_count);
|
---|
203 | }
|
---|
204 | /*----------------------------------------------------------------------------*/
|
---|
205 | /**
|
---|
206 | * Process root hub request.
|
---|
207 | *
|
---|
208 | * @param instance Root hub instance
|
---|
209 | * @param request Structure containing both request and response information
|
---|
210 | * @return Error code
|
---|
211 | */
|
---|
212 | void rh_request(rh_t *instance, usb_transfer_batch_t *request)
|
---|
213 | {
|
---|
214 | assert(instance);
|
---|
215 | assert(request);
|
---|
216 |
|
---|
217 | switch (request->ep->transfer_type)
|
---|
218 | {
|
---|
219 | case USB_TRANSFER_CONTROL:
|
---|
220 | usb_log_debug("Root hub got CONTROL packet\n");
|
---|
221 | control_request(instance, request);
|
---|
222 | break;
|
---|
223 |
|
---|
224 | case USB_TRANSFER_INTERRUPT:
|
---|
225 | usb_log_debug("Root hub got INTERRUPT packet\n");
|
---|
226 | fibril_mutex_lock(&instance->guard);
|
---|
227 | assert(instance->unfinished_interrupt_transfer == NULL);
|
---|
228 | const uint16_t mask = create_interrupt_mask(instance);
|
---|
229 | if (mask == 0) {
|
---|
230 | usb_log_debug("No changes...\n");
|
---|
231 | instance->unfinished_interrupt_transfer = request;
|
---|
232 | } else {
|
---|
233 | usb_log_debug("Processing changes...\n");
|
---|
234 | interrupt_request(
|
---|
235 | request, mask, instance->interrupt_mask_size);
|
---|
236 | }
|
---|
237 | fibril_mutex_unlock(&instance->guard);
|
---|
238 | break;
|
---|
239 |
|
---|
240 | default:
|
---|
241 | usb_log_error("Root hub got unsupported request.\n");
|
---|
242 | TRANSFER_END(request, ENOTSUP);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | /*----------------------------------------------------------------------------*/
|
---|
246 | /**
|
---|
247 | * Process interrupt on a hub device.
|
---|
248 | *
|
---|
249 | * If there is no pending interrupt transfer, nothing happens.
|
---|
250 | * @param instance
|
---|
251 | */
|
---|
252 | void rh_interrupt(rh_t *instance)
|
---|
253 | {
|
---|
254 | assert(instance);
|
---|
255 |
|
---|
256 | fibril_mutex_lock(&instance->guard);
|
---|
257 | if (instance->unfinished_interrupt_transfer) {
|
---|
258 | usb_log_debug("Finalizing interrupt transfer\n");
|
---|
259 | const uint16_t mask = create_interrupt_mask(instance);
|
---|
260 | interrupt_request(instance->unfinished_interrupt_transfer,
|
---|
261 | mask, instance->interrupt_mask_size);
|
---|
262 | instance->unfinished_interrupt_transfer = NULL;
|
---|
263 | }
|
---|
264 | fibril_mutex_unlock(&instance->guard);
|
---|
265 | }
|
---|
266 | /*----------------------------------------------------------------------------*/
|
---|
267 | /**
|
---|
268 | * Create hub descriptor.
|
---|
269 | *
|
---|
270 | * For descriptor format see USB hub specification (chapter 11.15.2.1, pg. 263)
|
---|
271 | *
|
---|
272 | * @param instance Root hub instance
|
---|
273 | * @return Error code
|
---|
274 | */
|
---|
275 | void create_serialized_hub_descriptor(rh_t *instance)
|
---|
276 | {
|
---|
277 | assert(instance);
|
---|
278 |
|
---|
279 | /* 7 bytes + 2 port bit fields (port count + global bit) */
|
---|
280 | const size_t size = 7 + (instance->interrupt_mask_size * 2);
|
---|
281 | assert(size <= HUB_DESCRIPTOR_MAX_SIZE);
|
---|
282 | instance->hub_descriptor_size = size;
|
---|
283 |
|
---|
284 | const uint32_t hub_desc = instance->registers->rh_desc_a;
|
---|
285 | const uint32_t port_desc = instance->registers->rh_desc_b;
|
---|
286 |
|
---|
287 | /* bDescLength */
|
---|
288 | instance->descriptors.hub[0] = size;
|
---|
289 | /* bDescriptorType */
|
---|
290 | instance->descriptors.hub[1] = USB_DESCTYPE_HUB;
|
---|
291 | /* bNmbrPorts */
|
---|
292 | instance->descriptors.hub[2] = instance->port_count;
|
---|
293 | /* wHubCharacteristics */
|
---|
294 | instance->descriptors.hub[3] = 0 |
|
---|
295 | /* The lowest 2 bits indicate power switching mode */
|
---|
296 | (((hub_desc & RHDA_PSM_FLAG) ? 1 : 0) << 0) |
|
---|
297 | (((hub_desc & RHDA_NPS_FLAG) ? 1 : 0) << 1) |
|
---|
298 | /* Bit 3 indicates device type (compound device) */
|
---|
299 | (((hub_desc & RHDA_DT_FLAG) ? 1 : 0) << 2) |
|
---|
300 | /* Bits 4,5 indicate over-current protection mode */
|
---|
301 | (((hub_desc & RHDA_OCPM_FLAG) ? 1 : 0) << 3) |
|
---|
302 | (((hub_desc & RHDA_NOCP_FLAG) ? 1 : 0) << 4);
|
---|
303 |
|
---|
304 | /* Reserved */
|
---|
305 | instance->descriptors.hub[4] = 0;
|
---|
306 | /* bPwrOn2PwrGood */
|
---|
307 | instance->descriptors.hub[5] =
|
---|
308 | (hub_desc >> RHDA_POTPGT_SHIFT) & RHDA_POTPGT_MASK;
|
---|
309 | /* bHubContrCurrent, root hubs don't need no power. */
|
---|
310 | instance->descriptors.hub[6] = 0;
|
---|
311 |
|
---|
312 | /* Device Removable and some legacy 1.0 stuff*/
|
---|
313 | instance->descriptors.hub[7] =
|
---|
314 | (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK & 0xff;
|
---|
315 | instance->descriptors.hub[8] = 0xff;
|
---|
316 | if (instance->interrupt_mask_size == 2) {
|
---|
317 | instance->descriptors.hub[8] =
|
---|
318 | (port_desc >> RHDB_DR_SHIFT) & RHDB_DR_MASK >> 8;
|
---|
319 | instance->descriptors.hub[9] = 0xff;
|
---|
320 | instance->descriptors.hub[10] = 0xff;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | /*----------------------------------------------------------------------------*/
|
---|
324 | /** Initialize hub descriptors.
|
---|
325 | *
|
---|
326 | * A full configuration descriptor is assembled. The configuration and endpoint
|
---|
327 | * descriptors have local modifications.
|
---|
328 | * @param instance Root hub instance
|
---|
329 | * @return Error code
|
---|
330 | */
|
---|
331 | void rh_init_descriptors(rh_t *instance)
|
---|
332 | {
|
---|
333 | assert(instance);
|
---|
334 |
|
---|
335 | instance->descriptors.configuration = ohci_rh_conf_descriptor;
|
---|
336 | instance->descriptors.interface = ohci_rh_iface_descriptor;
|
---|
337 | instance->descriptors.endpoint = ohci_rh_ep_descriptor;
|
---|
338 | create_serialized_hub_descriptor(instance);
|
---|
339 |
|
---|
340 | instance->descriptors.endpoint.max_packet_size =
|
---|
341 | instance->interrupt_mask_size;
|
---|
342 |
|
---|
343 | instance->descriptors.configuration.total_length =
|
---|
344 | sizeof(usb_standard_configuration_descriptor_t) +
|
---|
345 | sizeof(usb_standard_endpoint_descriptor_t) +
|
---|
346 | sizeof(usb_standard_interface_descriptor_t) +
|
---|
347 | instance->hub_descriptor_size;
|
---|
348 | }
|
---|
349 | /*----------------------------------------------------------------------------*/
|
---|
350 | /**
|
---|
351 | * Create bitmap of changes to answer status interrupt.
|
---|
352 | *
|
---|
353 | * Result contains bitmap where bit 0 indicates change on hub and
|
---|
354 | * bit i indicates change on i`th port (i>0). For more info see
|
---|
355 | * Hub and Port status bitmap specification in USB specification
|
---|
356 | * (chapter 11.13.4).
|
---|
357 | * @param instance root hub instance
|
---|
358 | * @return Mask of changes.
|
---|
359 | */
|
---|
360 | uint16_t create_interrupt_mask(const rh_t *instance)
|
---|
361 | {
|
---|
362 | assert(instance);
|
---|
363 | uint16_t mask = 0;
|
---|
364 |
|
---|
365 | /* Only local power source change and over-current change can happen */
|
---|
366 | if (instance->registers->rh_status & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
|
---|
367 | mask |= 1;
|
---|
368 | }
|
---|
369 | for (size_t port = 1; port <= instance->port_count; ++port) {
|
---|
370 | /* Write-clean bits are those that indicate change */
|
---|
371 | if (RHPS_CHANGE_WC_MASK
|
---|
372 | & instance->registers->rh_port_status[port - 1]) {
|
---|
373 |
|
---|
374 | mask |= (1 << port);
|
---|
375 | }
|
---|
376 | }
|
---|
377 | /* USB is little endian */
|
---|
378 | return host2uint32_t_le(mask);
|
---|
379 | }
|
---|
380 | /*----------------------------------------------------------------------------*/
|
---|
381 | /**
|
---|
382 | * Create answer to status request.
|
---|
383 | *
|
---|
384 | * This might be either hub status or port status request. If neither,
|
---|
385 | * ENOTSUP is returned.
|
---|
386 | * @param instance root hub instance
|
---|
387 | * @param request structure containing both request and response information
|
---|
388 | * @return error code
|
---|
389 | */
|
---|
390 | void get_status(const rh_t *instance, usb_transfer_batch_t *request)
|
---|
391 | {
|
---|
392 | assert(instance);
|
---|
393 | assert(request);
|
---|
394 |
|
---|
395 |
|
---|
396 | const usb_device_request_setup_packet_t *request_packet =
|
---|
397 | (usb_device_request_setup_packet_t*)request->setup_buffer;
|
---|
398 |
|
---|
399 | switch (request_packet->request_type)
|
---|
400 | {
|
---|
401 | case USB_HUB_REQ_TYPE_GET_HUB_STATUS:
|
---|
402 | /* Hub status: just filter relevant info from rh_status reg */
|
---|
403 | if (request->buffer_size < 4) {
|
---|
404 | usb_log_error("Buffer(%zu) too small for hub get "
|
---|
405 | "status request.\n", request->buffer_size);
|
---|
406 | TRANSFER_END(request, EOVERFLOW);
|
---|
407 | } else {
|
---|
408 | const uint32_t data = instance->registers->rh_status &
|
---|
409 | (RHS_LPS_FLAG | RHS_LPSC_FLAG
|
---|
410 | | RHS_OCI_FLAG | RHS_OCIC_FLAG);
|
---|
411 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
---|
412 | }
|
---|
413 |
|
---|
414 | /* Copy appropriate rh_port_status register, OHCI designers were
|
---|
415 | * kind enough to make those bit values match USB specification */
|
---|
416 | case USB_HUB_REQ_TYPE_GET_PORT_STATUS:
|
---|
417 | if (request->buffer_size < 4) {
|
---|
418 | usb_log_error("Buffer(%zu) too small for hub get "
|
---|
419 | "status request.\n", request->buffer_size);
|
---|
420 | TRANSFER_END(request, EOVERFLOW);
|
---|
421 | } else {
|
---|
422 | const unsigned port = request_packet->index;
|
---|
423 | if (port < 1 || port > instance->port_count)
|
---|
424 | TRANSFER_END(request, EINVAL);
|
---|
425 |
|
---|
426 | const uint32_t data =
|
---|
427 | instance->registers->rh_port_status[port - 1];
|
---|
428 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
---|
429 | }
|
---|
430 | case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE):
|
---|
431 | if (request->buffer_size < 2) {
|
---|
432 | usb_log_error("Buffer(%zu) too small for hub generic "
|
---|
433 | "get status request.\n", request->buffer_size);
|
---|
434 | TRANSFER_END(request, EOVERFLOW);
|
---|
435 | } else {
|
---|
436 | static const uint16_t data =
|
---|
437 | uint16_host2usb(USB_DEVICE_STATUS_SELF_POWERED);
|
---|
438 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
---|
439 | }
|
---|
440 |
|
---|
441 | case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE):
|
---|
442 | /* Hubs are allowed to have only one interface */
|
---|
443 | if (request_packet->index != 0)
|
---|
444 | TRANSFER_END(request, EINVAL);
|
---|
445 | /* Fall through, as the answer will be the same: 0x0000 */
|
---|
446 | case SETUP_REQUEST_TO_HOST(USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT):
|
---|
447 | /* Endpoint 0 (default control) and 1 (interrupt) */
|
---|
448 | if (request_packet->index >= 2)
|
---|
449 | TRANSFER_END(request, EINVAL);
|
---|
450 |
|
---|
451 | if (request->buffer_size < 2) {
|
---|
452 | usb_log_error("Buffer(%zu) too small for hub generic "
|
---|
453 | "get status request.\n", request->buffer_size);
|
---|
454 | TRANSFER_END(request, EOVERFLOW);
|
---|
455 | } else {
|
---|
456 | /* Endpoints are OK. (We don't halt) */
|
---|
457 | static const uint16_t data = 0;
|
---|
458 | TRANSFER_END_DATA(request, &data, sizeof(data));
|
---|
459 | }
|
---|
460 |
|
---|
461 | default:
|
---|
462 | usb_log_error("Unsupported GET_STATUS request.\n");
|
---|
463 | TRANSFER_END(request, ENOTSUP);
|
---|
464 | }
|
---|
465 |
|
---|
466 | }
|
---|
467 | /*----------------------------------------------------------------------------*/
|
---|
468 | /**
|
---|
469 | * Create answer to a descriptor request.
|
---|
470 | *
|
---|
471 | * This might be a request for standard (configuration, device, endpoint or
|
---|
472 | * interface) or device specific (hub) descriptor.
|
---|
473 | * @param instance Root hub instance
|
---|
474 | * @param request Structure containing both request and response information
|
---|
475 | * @return Error code
|
---|
476 | */
|
---|
477 | void get_descriptor(const rh_t *instance, usb_transfer_batch_t *request)
|
---|
478 | {
|
---|
479 | assert(instance);
|
---|
480 | assert(request);
|
---|
481 |
|
---|
482 | const usb_device_request_setup_packet_t *setup_request =
|
---|
483 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
484 | const uint16_t setup_request_value = setup_request->value_high;
|
---|
485 | switch (setup_request_value)
|
---|
486 | {
|
---|
487 | case USB_DESCTYPE_HUB:
|
---|
488 | usb_log_debug2("USB_DESCTYPE_HUB\n");
|
---|
489 | /* Hub descriptor was generated locally */
|
---|
490 | TRANSFER_END_DATA(request, instance->descriptors.hub,
|
---|
491 | instance->hub_descriptor_size);
|
---|
492 |
|
---|
493 | case USB_DESCTYPE_DEVICE:
|
---|
494 | usb_log_debug2("USB_DESCTYPE_DEVICE\n");
|
---|
495 | /* Device descriptor is shared (No one should ask for it) */
|
---|
496 | TRANSFER_END_DATA(request, &ohci_rh_device_descriptor,
|
---|
497 | sizeof(ohci_rh_device_descriptor));
|
---|
498 |
|
---|
499 | case USB_DESCTYPE_CONFIGURATION:
|
---|
500 | usb_log_debug2("USB_DESCTYPE_CONFIGURATION\n");
|
---|
501 | /* Start with configuration and add others depending on
|
---|
502 | * request size */
|
---|
503 | TRANSFER_END_DATA(request, &instance->descriptors,
|
---|
504 | instance->descriptors.configuration.total_length);
|
---|
505 |
|
---|
506 | case USB_DESCTYPE_INTERFACE:
|
---|
507 | usb_log_debug2("USB_DESCTYPE_INTERFACE\n");
|
---|
508 | /* Use local interface descriptor. There is one and it
|
---|
509 | * might be modified */
|
---|
510 | TRANSFER_END_DATA(request, &instance->descriptors.interface,
|
---|
511 | sizeof(instance->descriptors.interface));
|
---|
512 |
|
---|
513 | case USB_DESCTYPE_ENDPOINT:
|
---|
514 | /* Use local endpoint descriptor. There is one
|
---|
515 | * it might have max_packet_size field modified*/
|
---|
516 | usb_log_debug2("USB_DESCTYPE_ENDPOINT\n");
|
---|
517 | TRANSFER_END_DATA(request, &instance->descriptors.endpoint,
|
---|
518 | sizeof(instance->descriptors.endpoint));
|
---|
519 |
|
---|
520 | default:
|
---|
521 | usb_log_debug2("USB_DESCTYPE_EINVAL %d \n"
|
---|
522 | "\ttype %d\n\trequest %d\n\tvalue "
|
---|
523 | "%d\n\tindex %d\n\tlen %d\n ",
|
---|
524 | setup_request->value,
|
---|
525 | setup_request->request_type, setup_request->request,
|
---|
526 | setup_request_value, setup_request->index,
|
---|
527 | setup_request->length);
|
---|
528 | TRANSFER_END(request, EINVAL);
|
---|
529 | }
|
---|
530 |
|
---|
531 | TRANSFER_END(request, ENOTSUP);
|
---|
532 | }
|
---|
533 | /*----------------------------------------------------------------------------*/
|
---|
534 | /**
|
---|
535 | * process feature-enabling request on hub
|
---|
536 | *
|
---|
537 | * @param instance root hub instance
|
---|
538 | * @param feature feature selector
|
---|
539 | * @param port port number, counted from 1
|
---|
540 | * @param enable enable or disable the specified feature
|
---|
541 | * @return error code
|
---|
542 | */
|
---|
543 | int set_feature_port(const rh_t *instance, uint16_t feature, uint16_t port)
|
---|
544 | {
|
---|
545 | assert(instance);
|
---|
546 |
|
---|
547 | if (port < 1 || port > instance->port_count)
|
---|
548 | return EINVAL;
|
---|
549 |
|
---|
550 | switch (feature)
|
---|
551 | {
|
---|
552 | case USB_HUB_FEATURE_PORT_POWER: //8
|
---|
553 | /* No power switching */
|
---|
554 | if (instance->registers->rh_desc_a & RHDA_NPS_FLAG)
|
---|
555 | return EOK;
|
---|
556 | /* Ganged power switching */
|
---|
557 | if (!(instance->registers->rh_desc_a & RHDA_PSM_FLAG)) {
|
---|
558 | instance->registers->rh_status = RHS_SET_GLOBAL_POWER;
|
---|
559 | return EOK;
|
---|
560 | }
|
---|
561 | case USB_HUB_FEATURE_PORT_ENABLE: //1
|
---|
562 | case USB_HUB_FEATURE_PORT_SUSPEND: //2
|
---|
563 | case USB_HUB_FEATURE_PORT_RESET: //4
|
---|
564 | /* Nice thing is that these shifts correspond to the position
|
---|
565 | * of control bits in register */
|
---|
566 | instance->registers->rh_port_status[port - 1] = (1 << feature);
|
---|
567 | return EOK;
|
---|
568 | default:
|
---|
569 | return ENOTSUP;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | /*----------------------------------------------------------------------------*/
|
---|
573 | /**
|
---|
574 | * Process feature clear request.
|
---|
575 | *
|
---|
576 | * @param instance root hub instance
|
---|
577 | * @param feature feature selector
|
---|
578 | * @param port port number, counted from 1
|
---|
579 | * @param enable enable or disable the specified feature
|
---|
580 | * @return error code
|
---|
581 | */
|
---|
582 | int clear_feature_port(const rh_t *instance, uint16_t feature, uint16_t port)
|
---|
583 | {
|
---|
584 | assert(instance);
|
---|
585 |
|
---|
586 | if (port < 1 || port > instance->port_count)
|
---|
587 | return EINVAL;
|
---|
588 |
|
---|
589 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
590 | switch (feature)
|
---|
591 | {
|
---|
592 | case USB_HUB_FEATURE_PORT_POWER: //8
|
---|
593 | /* No power switching */
|
---|
594 | if (instance->registers->rh_desc_a & RHDA_NPS_FLAG)
|
---|
595 | return ENOTSUP;
|
---|
596 | /* Ganged power switching */
|
---|
597 | if (!(instance->registers->rh_desc_a & RHDA_PSM_FLAG)) {
|
---|
598 | instance->registers->rh_status = RHS_CLEAR_GLOBAL_POWER;
|
---|
599 | return EOK;
|
---|
600 | }
|
---|
601 | instance->registers->rh_port_status[port - 1] =
|
---|
602 | RHPS_CLEAR_PORT_POWER;
|
---|
603 | return EOK;
|
---|
604 |
|
---|
605 | case USB_HUB_FEATURE_PORT_ENABLE: //1
|
---|
606 | instance->registers->rh_port_status[port - 1] =
|
---|
607 | RHPS_CLEAR_PORT_ENABLE;
|
---|
608 | return EOK;
|
---|
609 |
|
---|
610 | case USB_HUB_FEATURE_PORT_SUSPEND: //2
|
---|
611 | instance->registers->rh_port_status[port - 1] =
|
---|
612 | RHPS_CLEAR_PORT_SUSPEND;
|
---|
613 | return EOK;
|
---|
614 |
|
---|
615 | case USB_HUB_FEATURE_C_PORT_CONNECTION: //16
|
---|
616 | case USB_HUB_FEATURE_C_PORT_ENABLE: //17
|
---|
617 | case USB_HUB_FEATURE_C_PORT_SUSPEND: //18
|
---|
618 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: //19
|
---|
619 | case USB_HUB_FEATURE_C_PORT_RESET: //20
|
---|
620 | /* Nice thing is that these shifts correspond to the position
|
---|
621 | * of control bits in register */
|
---|
622 | instance->registers->rh_port_status[port - 1] = (1 << feature);
|
---|
623 | return EOK;
|
---|
624 |
|
---|
625 | default:
|
---|
626 | return ENOTSUP;
|
---|
627 | }
|
---|
628 | }
|
---|
629 | /*----------------------------------------------------------------------------*/
|
---|
630 | /**
|
---|
631 | * process one of requests that do not request nor carry additional data
|
---|
632 | *
|
---|
633 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
---|
634 | * USB_DEVREQ_SET_ADDRESS.
|
---|
635 | * @param instance root hub instance
|
---|
636 | * @param request structure containing both request and response information
|
---|
637 | * @return error code
|
---|
638 | */
|
---|
639 | void set_feature(const rh_t *instance, usb_transfer_batch_t *request)
|
---|
640 | {
|
---|
641 | assert(instance);
|
---|
642 | assert(request);
|
---|
643 |
|
---|
644 | const usb_device_request_setup_packet_t *setup_request =
|
---|
645 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
646 | switch (setup_request->request_type)
|
---|
647 | {
|
---|
648 | case USB_HUB_REQ_TYPE_SET_PORT_FEATURE:
|
---|
649 | usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
|
---|
650 | const int ret = set_feature_port(instance,
|
---|
651 | setup_request->value, setup_request->index);
|
---|
652 | TRANSFER_END(request, ret);
|
---|
653 |
|
---|
654 | case USB_HUB_REQ_TYPE_SET_HUB_FEATURE:
|
---|
655 | /* Chapter 11.16.2 specifies that hub can be recipient
|
---|
656 | * only for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
|
---|
657 | * features. It makes no sense to SET either. */
|
---|
658 | usb_log_error("Invalid HUB set feature request.\n");
|
---|
659 | TRANSFER_END(request, ENOTSUP);
|
---|
660 | default:
|
---|
661 | usb_log_error("Invalid set feature request type: %d\n",
|
---|
662 | setup_request->request_type);
|
---|
663 | TRANSFER_END(request, EINVAL);
|
---|
664 | }
|
---|
665 | }
|
---|
666 | /*----------------------------------------------------------------------------*/
|
---|
667 | /**
|
---|
668 | * process one of requests that do not request nor carry additional data
|
---|
669 | *
|
---|
670 | * Request can be one of USB_DEVREQ_CLEAR_FEATURE, USB_DEVREQ_SET_FEATURE or
|
---|
671 | * USB_DEVREQ_SET_ADDRESS.
|
---|
672 | * @param instance root hub instance
|
---|
673 | * @param request structure containing both request and response information
|
---|
674 | * @return error code
|
---|
675 | */
|
---|
676 | void clear_feature(const rh_t *instance, usb_transfer_batch_t *request)
|
---|
677 | {
|
---|
678 | assert(instance);
|
---|
679 | assert(request);
|
---|
680 |
|
---|
681 | const usb_device_request_setup_packet_t *setup_request =
|
---|
682 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
683 |
|
---|
684 | switch (setup_request->request_type)
|
---|
685 | {
|
---|
686 | case USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE:
|
---|
687 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE\n");
|
---|
688 | const int ret = clear_feature_port(instance,
|
---|
689 | setup_request->value, setup_request->index);
|
---|
690 | TRANSFER_END(request, ret);
|
---|
691 |
|
---|
692 | case USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE:
|
---|
693 | usb_log_debug("USB_HUB_REQ_TYPE_CLEAR_HUB_FEATURE\n");
|
---|
694 | /*
|
---|
695 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
696 | * C_HUB_OVER_CURRENT are supported.
|
---|
697 | * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
|
---|
698 | * C_HUB_LOCAL_POWER is not supported
|
---|
699 | * as root hubs do not support local power status feature.
|
---|
700 | * (OHCI pg. 127) */
|
---|
701 | if (setup_request->value == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
|
---|
702 | instance->registers->rh_status = RHS_OCIC_FLAG;
|
---|
703 | TRANSFER_END(request, EOK);
|
---|
704 | }
|
---|
705 | default:
|
---|
706 | usb_log_error("Invalid clear feature request type: %d\n",
|
---|
707 | setup_request->request_type);
|
---|
708 | TRANSFER_END(request, EINVAL);
|
---|
709 | }
|
---|
710 | }
|
---|
711 | /*----------------------------------------------------------------------------*/
|
---|
712 | /**
|
---|
713 | * Process hub control request.
|
---|
714 | *
|
---|
715 | * If needed, writes answer into the request structure.
|
---|
716 | * Request can be one of
|
---|
717 | * USB_DEVREQ_GET_STATUS,
|
---|
718 | * USB_DEVREQ_GET_DESCRIPTOR,
|
---|
719 | * USB_DEVREQ_GET_CONFIGURATION,
|
---|
720 | * USB_DEVREQ_CLEAR_FEATURE,
|
---|
721 | * USB_DEVREQ_SET_FEATURE,
|
---|
722 | * USB_DEVREQ_SET_ADDRESS,
|
---|
723 | * USB_DEVREQ_SET_DESCRIPTOR or
|
---|
724 | * USB_DEVREQ_SET_CONFIGURATION.
|
---|
725 | *
|
---|
726 | * @param instance root hub instance
|
---|
727 | * @param request structure containing both request and response information
|
---|
728 | * @return error code
|
---|
729 | */
|
---|
730 | void control_request(rh_t *instance, usb_transfer_batch_t *request)
|
---|
731 | {
|
---|
732 | assert(instance);
|
---|
733 | assert(request);
|
---|
734 |
|
---|
735 | if (!request->setup_buffer) {
|
---|
736 | usb_log_error("Root hub received empty transaction!");
|
---|
737 | TRANSFER_END(request, EBADMEM);
|
---|
738 | }
|
---|
739 |
|
---|
740 | if (sizeof(usb_device_request_setup_packet_t) > request->setup_size) {
|
---|
741 | usb_log_error("Setup packet too small\n");
|
---|
742 | TRANSFER_END(request, EOVERFLOW);
|
---|
743 | }
|
---|
744 |
|
---|
745 | usb_log_debug2("CTRL packet: %s.\n",
|
---|
746 | usb_debug_str_buffer((uint8_t *) request->setup_buffer, 8, 8));
|
---|
747 | const usb_device_request_setup_packet_t *setup_request =
|
---|
748 | (usb_device_request_setup_packet_t *) request->setup_buffer;
|
---|
749 | switch (setup_request->request)
|
---|
750 | {
|
---|
751 | case USB_DEVREQ_GET_STATUS:
|
---|
752 | usb_log_debug("USB_DEVREQ_GET_STATUS\n");
|
---|
753 | get_status(instance, request);
|
---|
754 | break;
|
---|
755 |
|
---|
756 | case USB_DEVREQ_GET_DESCRIPTOR:
|
---|
757 | usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
|
---|
758 | get_descriptor(instance, request);
|
---|
759 | break;
|
---|
760 |
|
---|
761 | case USB_DEVREQ_GET_CONFIGURATION:
|
---|
762 | usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
|
---|
763 | if (request->buffer_size == 0)
|
---|
764 | TRANSFER_END(request, EOVERFLOW);
|
---|
765 | static const uint8_t config = 1;
|
---|
766 | TRANSFER_END_DATA(request, &config, sizeof(config));
|
---|
767 |
|
---|
768 | case USB_DEVREQ_CLEAR_FEATURE:
|
---|
769 | usb_log_debug2("USB_DEVREQ_CLEAR_FEATURE\n");
|
---|
770 | clear_feature(instance, request);
|
---|
771 | break;
|
---|
772 |
|
---|
773 | case USB_DEVREQ_SET_FEATURE:
|
---|
774 | usb_log_debug2("USB_DEVREQ_SET_FEATURE\n");
|
---|
775 | set_feature(instance, request);
|
---|
776 | break;
|
---|
777 |
|
---|
778 | case USB_DEVREQ_SET_ADDRESS:
|
---|
779 | usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
|
---|
780 | instance->address = setup_request->value;
|
---|
781 | TRANSFER_END(request, EOK);
|
---|
782 |
|
---|
783 | case USB_DEVREQ_SET_CONFIGURATION:
|
---|
784 | usb_log_debug("USB_DEVREQ_SET_CONFIGURATION\n");
|
---|
785 | /* We don't need to do anything */
|
---|
786 | TRANSFER_END(request, EOK);
|
---|
787 |
|
---|
788 | case USB_DEVREQ_SET_DESCRIPTOR: /* Not supported by OHCI RH */
|
---|
789 | default:
|
---|
790 | usb_log_error("Received unsupported request: %d.\n",
|
---|
791 | setup_request->request);
|
---|
792 | TRANSFER_END(request, ENOTSUP);
|
---|
793 | }
|
---|
794 | }
|
---|
795 | /**
|
---|
796 | * @}
|
---|
797 | */
|
---|