| 1 |
|
|---|
| 2 | #include <errno.h>
|
|---|
| 3 | #include <str_error.h>
|
|---|
| 4 | //#include <usb/devreq.h> /* for usb_device_request_setup_packet_t */
|
|---|
| 5 | #include <usb/usb.h>
|
|---|
| 6 | #include <usb/usbdrv.h>
|
|---|
| 7 |
|
|---|
| 8 | #include "debug.h"
|
|---|
| 9 | #include "uhci.h"
|
|---|
| 10 | #include "port.h"
|
|---|
| 11 | #include "port_status.h"
|
|---|
| 12 |
|
|---|
| 13 | static int uhci_port_new_device(uhci_port_t *port);
|
|---|
| 14 | static int uhci_port_remove_device(uhci_port_t *port);
|
|---|
| 15 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
|
|---|
| 16 |
|
|---|
| 17 | /*----------------------------------------------------------------------------*/
|
|---|
| 18 | int uhci_port_check(void *port)
|
|---|
| 19 | {
|
|---|
| 20 | uhci_port_t *port_instance = port;
|
|---|
| 21 | assert(port_instance);
|
|---|
| 22 | port_instance->hc_phone = devman_device_connect(port_instance->hc->handle, 0);
|
|---|
| 23 |
|
|---|
| 24 | while (1) {
|
|---|
| 25 | /* Read port status. */
|
|---|
| 26 | port_status_t port_status =
|
|---|
| 27 | port_status_read(port_instance->address);
|
|---|
| 28 |
|
|---|
| 29 | uhci_print_info("Port %d: %04X (@ 0x%x) = " \
|
|---|
| 30 | "%s,%s,%s,%s,[%s,%s],%s,%s,%s,%s\n",
|
|---|
| 31 | port_instance->number, port_status, port_instance->address,
|
|---|
| 32 | UHCI_GET_STR_FLAG(port_status, STATUS_SUSPEND, "suspend", "up"),
|
|---|
| 33 | UHCI_GET_STR_FLAG(port_status, STATUS_IN_RESET, "in reset", "-"),
|
|---|
| 34 | UHCI_GET_STR_FLAG(port_status, STATUS_LOW_SPEED, "lowsp", "fullsp"),
|
|---|
| 35 | UHCI_GET_STR_FLAG(port_status, STATUS_RESUME, "resume", "k-state"),
|
|---|
| 36 | UHCI_GET_STR_FLAG(port_status, STATUS_LINE_D_MINUS, "D- on", "D- off"),
|
|---|
| 37 | UHCI_GET_STR_FLAG(port_status, STATUS_LINE_D_PLUS, "D+ on", "D+ off"),
|
|---|
| 38 | UHCI_GET_STR_FLAG(port_status, STATUS_ENABLED_CHANGED, "enblchg", "-"),
|
|---|
| 39 | UHCI_GET_STR_FLAG(port_status, STATUS_ENABLED, "enabled", "disabled"),
|
|---|
| 40 | UHCI_GET_STR_FLAG(port_status, STATUS_CONNECTED_CHANGED, "connchg", "-"),
|
|---|
| 41 | UHCI_GET_STR_FLAG(port_status, STATUS_CONNECTED, "hasdev", "nodev"));
|
|---|
| 42 |
|
|---|
| 43 | if (port_status & STATUS_CONNECTED_CHANGED) {
|
|---|
| 44 | if (port_status & STATUS_CONNECTED) {
|
|---|
| 45 | /* new device */
|
|---|
| 46 | port_status |= STATUS_IN_RESET;
|
|---|
| 47 | port_status_write(port_instance->address, port_status);
|
|---|
| 48 | async_usleep(1000);
|
|---|
| 49 | port_status =
|
|---|
| 50 | port_status_read(port_instance->address);
|
|---|
| 51 | port_status &= ~STATUS_IN_RESET;
|
|---|
| 52 | port_status_write(port_instance->address, port_status);
|
|---|
| 53 | uhci_port_new_device(port_instance);
|
|---|
| 54 | } else {
|
|---|
| 55 | uhci_port_remove_device(port_instance);
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | async_usleep(port_instance->wait_period_usec);
|
|---|
| 59 | }
|
|---|
| 60 | return EOK;
|
|---|
| 61 | }
|
|---|
| 62 | /*----------------------------------------------------------------------------*/
|
|---|
| 63 | static int uhci_port_new_device(uhci_port_t *port)
|
|---|
| 64 | {
|
|---|
| 65 | assert(port);
|
|---|
| 66 | assert(port->hc);
|
|---|
| 67 |
|
|---|
| 68 | uhci_print_info("Adding new device on port %d.\n", port->number);
|
|---|
| 69 |
|
|---|
| 70 | uhci_t *uhci_instance = (uhci_t*)(port->hc->driver_data);
|
|---|
| 71 |
|
|---|
| 72 | /* get default address */
|
|---|
| 73 | usb_address_keeping_reserve_default(&uhci_instance->address_manager);
|
|---|
| 74 |
|
|---|
| 75 | const usb_address_t usb_address =
|
|---|
| 76 | usb_address_keeping_request(&uhci_instance->address_manager);
|
|---|
| 77 |
|
|---|
| 78 | if (usb_address <= 0) {
|
|---|
| 79 | return usb_address;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /* enable port */
|
|---|
| 83 | uhci_port_set_enabled(port, true);
|
|---|
| 84 |
|
|---|
| 85 | /* assign address to device */
|
|---|
| 86 | int ret = usb_drv_req_set_address(port->hc_phone, 0, usb_address);
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | if (ret != EOK) { /* address assigning went wrong */
|
|---|
| 90 | uhci_print_error("Failed to assign address (port %d): %s.\n",
|
|---|
| 91 | port->number, str_error(ret));
|
|---|
| 92 | uhci_port_set_enabled(port, false);
|
|---|
| 93 | usb_address_keeping_release_default(&uhci_instance->address_manager);
|
|---|
| 94 | return ENOMEM;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /* release default address */
|
|---|
| 98 | usb_address_keeping_release_default(&uhci_instance->address_manager);
|
|---|
| 99 |
|
|---|
| 100 | /* communicate and possibly report to devman */
|
|---|
| 101 | assert(port->attached_device == 0);
|
|---|
| 102 |
|
|---|
| 103 | ret = usb_drv_register_child_in_devman(port->hc_phone, port->hc, usb_address,
|
|---|
| 104 | &port->attached_device);
|
|---|
| 105 |
|
|---|
| 106 | if (ret != EOK) { /* something went wrong */
|
|---|
| 107 | uhci_print_error("Failed(%d) in usb_drv_register_child.\n", ret);
|
|---|
| 108 | uhci_port_set_enabled(port, false);
|
|---|
| 109 | return ENOMEM;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | return EOK;
|
|---|
| 113 | }
|
|---|
| 114 | /*----------------------------------------------------------------------------*/
|
|---|
| 115 | static int uhci_port_remove_device(uhci_port_t *port)
|
|---|
| 116 | {
|
|---|
| 117 | uhci_print_error("Don't know how to remove device %#x.\n",
|
|---|
| 118 | port->attached_device);
|
|---|
| 119 | uhci_port_set_enabled(port, false);
|
|---|
| 120 | return EOK;
|
|---|
| 121 | }
|
|---|
| 122 | /*----------------------------------------------------------------------------*/
|
|---|
| 123 | static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
|
|---|
| 124 | {
|
|---|
| 125 | assert(port);
|
|---|
| 126 |
|
|---|
| 127 | /* read register value */
|
|---|
| 128 | port_status_t port_status
|
|---|
| 129 | = port_status_read(port->address);
|
|---|
| 130 |
|
|---|
| 131 | /* enable port: register write */
|
|---|
| 132 | if (enabled) {
|
|---|
| 133 | port_status |= STATUS_ENABLED;
|
|---|
| 134 | } else {
|
|---|
| 135 | port_status &= ~STATUS_ENABLED;
|
|---|
| 136 | }
|
|---|
| 137 | port_status_write(port->address, port_status);
|
|---|
| 138 |
|
|---|
| 139 | uhci_print_info("%s port %d.\n",
|
|---|
| 140 | enabled ? "Enabled" : "Disabled", port->number);
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 | /*----------------------------------------------------------------------------*/
|
|---|