[355f7c2] | 1 | /*
|
---|
[6cb58e6] | 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[355f7c2] | 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 |
|
---|
[bd8c753d] | 29 | /** @addtogroup drvusbvhc
|
---|
[355f7c2] | 30 | * @{
|
---|
[79ae36dd] | 31 | */
|
---|
[355f7c2] | 32 | /** @file
|
---|
[6cb58e6] | 33 | * Virtual host controller.
|
---|
[355f7c2] | 34 | */
|
---|
[63b4f90] | 35 |
|
---|
[15f3c3f] | 36 | #include <loc.h>
|
---|
[63b4f90] | 37 | #include <async.h>
|
---|
| 38 | #include <unistd.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include <sysinfo.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <str_error.h>
|
---|
[eb1a2f4] | 44 | #include <ddf/driver.h>
|
---|
[355f7c2] | 45 |
|
---|
[4b4c797] | 46 | #include <usb/usb.h>
|
---|
[357a302] | 47 | #include <usb/ddfiface.h>
|
---|
[71ed4849] | 48 | #include <usb_iface.h>
|
---|
[355f7c2] | 49 | #include "vhcd.h"
|
---|
[63b4f90] | 50 | #include "hub.h"
|
---|
| 51 | #include "conn.h"
|
---|
[355f7c2] | 52 |
|
---|
[eb1a2f4] | 53 | static ddf_dev_ops_t vhc_ops = {
|
---|
[4317827] | 54 | .interfaces[USBHC_DEV_IFACE] = &vhc_iface,
|
---|
[357a302] | 55 | .interfaces[USB_DEV_IFACE] = &vhc_usb_iface,
|
---|
[6967c14] | 56 | .close = on_client_close,
|
---|
[4317827] | 57 | .default_handler = default_connection_handler
|
---|
| 58 | };
|
---|
[e27595b] | 59 |
|
---|
[eb1a2f4] | 60 | static int vhc_add_device(ddf_dev_t *dev)
|
---|
[355f7c2] | 61 | {
|
---|
[eb1a2f4] | 62 | static int vhc_count = 0;
|
---|
| 63 | int rc;
|
---|
| 64 |
|
---|
[63b4f90] | 65 | if (vhc_count > 0) {
|
---|
| 66 | return ELIMIT;
|
---|
[355f7c2] | 67 | }
|
---|
[63b4f90] | 68 |
|
---|
[6cb58e6] | 69 | vhc_data_t *data = malloc(sizeof(vhc_data_t));
|
---|
| 70 | if (data == NULL) {
|
---|
| 71 | usb_log_fatal("Failed to allocate memory.\n");
|
---|
| 72 | return ENOMEM;
|
---|
| 73 | }
|
---|
| 74 | data->magic = 0xDEADBEEF;
|
---|
[933b0d7] | 75 | rc = usb_endpoint_manager_init(&data->ep_manager, (size_t) -1,
|
---|
| 76 | bandwidth_count_usb11);
|
---|
[6cb58e6] | 77 | if (rc != EOK) {
|
---|
| 78 | usb_log_fatal("Failed to initialize endpoint manager.\n");
|
---|
| 79 | free(data);
|
---|
| 80 | return rc;
|
---|
| 81 | }
|
---|
[8b54fe6] | 82 | usb_device_manager_init(&data->dev_manager);
|
---|
[6cb58e6] | 83 |
|
---|
[eb1a2f4] | 84 | ddf_fun_t *hc = ddf_fun_create(dev, fun_exposed, "hc");
|
---|
| 85 | if (hc == NULL) {
|
---|
| 86 | usb_log_fatal("Failed to create device function.\n");
|
---|
[6cb58e6] | 87 | free(data);
|
---|
[eb1a2f4] | 88 | return ENOMEM;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | hc->ops = &vhc_ops;
|
---|
[6cb58e6] | 92 | list_initialize(&data->devices);
|
---|
| 93 | fibril_mutex_initialize(&data->guard);
|
---|
| 94 | data->hub = &virtual_hub_device;
|
---|
| 95 | data->hc_fun = hc;
|
---|
| 96 |
|
---|
| 97 | dev->driver_data = data;
|
---|
[63b4f90] | 98 |
|
---|
[eb1a2f4] | 99 | rc = ddf_fun_bind(hc);
|
---|
| 100 | if (rc != EOK) {
|
---|
| 101 | usb_log_fatal("Failed to bind HC function: %s.\n",
|
---|
| 102 | str_error(rc));
|
---|
[6cb58e6] | 103 | free(data);
|
---|
[eb1a2f4] | 104 | return rc;
|
---|
| 105 | }
|
---|
[7034be15] | 106 |
|
---|
[1dc4a5e] | 107 | rc = ddf_fun_add_to_category(hc, USB_HC_CATEGORY);
|
---|
[28216a0] | 108 | if (rc != EOK) {
|
---|
| 109 | usb_log_fatal("Failed to add function to HC class: %s.\n",
|
---|
| 110 | str_error(rc));
|
---|
| 111 | free(data);
|
---|
| 112 | return rc;
|
---|
| 113 | }
|
---|
[70a9092] | 114 |
|
---|
[eb1a2f4] | 115 | virtual_hub_device_init(hc);
|
---|
[7034be15] | 116 |
|
---|
[eb1a2f4] | 117 | usb_log_info("Virtual USB host controller ready (dev %zu, hc %zu).\n",
|
---|
| 118 | (size_t) dev->handle, (size_t) hc->handle);
|
---|
[e27595b] | 119 |
|
---|
[6cb58e6] | 120 |
|
---|
| 121 |
|
---|
| 122 | rc = vhc_virtdev_plug_hub(data, data->hub, NULL);
|
---|
| 123 | if (rc != EOK) {
|
---|
| 124 | usb_log_fatal("Failed to plug root hub: %s.\n", str_error(rc));
|
---|
| 125 | free(data);
|
---|
| 126 | return rc;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[7034be15] | 129 | return EOK;
|
---|
[355f7c2] | 130 | }
|
---|
| 131 |
|
---|
[4317827] | 132 | static driver_ops_t vhc_driver_ops = {
|
---|
| 133 | .add_device = vhc_add_device,
|
---|
| 134 | };
|
---|
| 135 |
|
---|
| 136 | static driver_t vhc_driver = {
|
---|
[63b4f90] | 137 | .name = NAME,
|
---|
[4317827] | 138 | .driver_ops = &vhc_driver_ops
|
---|
[63b4f90] | 139 | };
|
---|
| 140 |
|
---|
[c4ba29c7] | 141 |
|
---|
[63b4f90] | 142 | int main(int argc, char * argv[])
|
---|
| 143 | {
|
---|
[215b001] | 144 | usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
|
---|
[c4ba29c7] | 145 |
|
---|
[e63a4e1] | 146 | printf(NAME ": virtual USB host controller driver.\n");
|
---|
[e27595b] | 147 |
|
---|
[eb1a2f4] | 148 | return ddf_driver_main(&vhc_driver);
|
---|
[355f7c2] | 149 | }
|
---|
| 150 |
|
---|
[63b4f90] | 151 |
|
---|
[355f7c2] | 152 | /**
|
---|
| 153 | * @}
|
---|
| 154 | */
|
---|