hub.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <usb/classes/classes.h>
00036 #include <usbvirt/device.h>
00037 #include <errno.h>
00038 #include <str_error.h>
00039 #include <stdlib.h>
00040 #include <ddf/driver.h>
00041 #include <devman.h>
00042 #include <usb/dev/hub.h>
00043 #include <usb/dev/recognise.h>
00044 
00045 #include "hub.h"
00046 //#include "hub/virthub.h"
00047 #include "vhcd.h"
00048 #include "conn.h"
00049 
00050 usbvirt_device_t virtual_hub_device = {
00051         .name = "root hub",
00052         .ops = &hub_ops,
00053         .address = 0
00054 };
00055 
00056 static ddf_dev_ops_t rh_ops = {
00057         .interfaces[USB_DEV_IFACE] = &rh_usb_iface,
00058 };
00059 
00060 static int hub_register_in_devman_fibril(void *arg);
00061 
00062 void virtual_hub_device_init(ddf_fun_t *hc_dev)
00063 {
00064         virthub_init(&virtual_hub_device);
00065 
00066         /*
00067          * We need to register the root hub.
00068          * This must be done in separate fibril because the device
00069          * we are connecting to are ourselves and we cannot connect
00070          * before leaving the add_device() function.
00071          */
00072         fid_t root_hub_registration
00073             = fibril_create(hub_register_in_devman_fibril, hc_dev);
00074         if (root_hub_registration == 0) {
00075                 usb_log_fatal("Failed to create hub registration fibril.\n");
00076                 return;
00077         }
00078 
00079         fibril_add_ready(root_hub_registration);
00080 }
00081 
00082 static int pretend_port_rest(int unused, void *unused2)
00083 {
00084         return EOK;
00085 }
00086 
00092 int hub_register_in_devman_fibril(void *arg)
00093 {
00094         ddf_fun_t *hc_dev = (ddf_fun_t *) arg;
00095 
00096         /*
00097          * Wait until parent device is properly initialized.
00098          */
00099         int phone;
00100         do {
00101                 phone = devman_device_connect(hc_dev->handle, 0);
00102         } while (phone < 0);
00103         async_hangup(phone);
00104 
00105         int rc;
00106 
00107         usb_hc_connection_t hc_conn;
00108         rc = usb_hc_connection_initialize(&hc_conn, hc_dev->handle);
00109         assert(rc == EOK);
00110 
00111         rc = usb_hc_connection_open(&hc_conn);
00112         assert(rc == EOK);
00113 
00114         ddf_fun_t *hub_dev;
00115         rc = usb_hc_new_device_wrapper(hc_dev->dev, &hc_conn,
00116             USB_SPEED_FULL,
00117             pretend_port_rest, 0, NULL,
00118             NULL, NULL, &rh_ops, hc_dev, &hub_dev);
00119         if (rc != EOK) {
00120                 usb_log_fatal("Failed to create root hub: %s.\n",
00121                     str_error(rc));
00122         }
00123 
00124         usb_hc_connection_close(&hc_conn);
00125 
00126         usb_log_info("Created root hub function (handle %zu).\n",
00127             (size_t) hub_dev->handle);
00128 
00129         return 0;
00130 }
00131         
00132 

Generated on Thu Jun 2 07:45:44 2011 for HelenOS/USB by  doxygen 1.4.7