main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 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 
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include <str_error.h>
00042 
00043 #include <usb/usb.h>
00044 #include <usb/descriptor.h>
00045 #include <usb/debug.h>
00046 #include <usb/classes/classes.h>
00047 #include <usb/hid/hid.h>
00048 #include <usbvirt/device.h>
00049 
00050 #include "virthid.h"
00051 #include "ifaces.h"
00052 #include "stdreq.h"
00053 
00054 static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
00055         {
00056                 .req_direction = USB_DIRECTION_IN,
00057                 .req_type = USB_REQUEST_TYPE_STANDARD,
00058                 .req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
00059                 .request = USB_DEVREQ_GET_DESCRIPTOR,
00060                 .name = "Get_Descriptor",
00061                 .callback = req_get_descriptor
00062         },
00063         {
00064                 .req_direction = USB_DIRECTION_OUT,
00065                 .req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
00066                 .req_type = USB_REQUEST_TYPE_CLASS,
00067                 .request = USB_HIDREQ_SET_PROTOCOL,
00068                 .name = "Set_Protocol",
00069                 .callback = req_set_protocol
00070         },
00071         {
00072                 .req_direction = USB_DIRECTION_OUT,
00073                 .req_recipient = USB_REQUEST_RECIPIENT_INTERFACE,
00074                 .req_type = USB_REQUEST_TYPE_CLASS,
00075                 .request = USB_HIDREQ_SET_REPORT,
00076                 .name = "Set_Report",
00077                 .callback = req_set_report
00078         },
00079         {
00080                 .callback = NULL
00081         }
00082 };
00083 
00087 static usbvirt_device_ops_t hid_ops = {
00088         .control = endpoint_zero_handlers,
00089 };
00090 
00092 static usb_standard_configuration_descriptor_t std_configuration_descriptor = {
00093         .length = sizeof(usb_standard_configuration_descriptor_t),
00094         .descriptor_type = USB_DESCTYPE_CONFIGURATION,
00095         /* Will be patched at runtime. */
00096         .total_length = sizeof(usb_standard_configuration_descriptor_t),
00097         .interface_count = 0,
00098         .configuration_number = 1,
00099         .str_configuration = 0,
00100         .attributes = 128, /* denotes bus-powered device */
00101         .max_power = 50
00102 };
00103 
00105 static usb_standard_device_descriptor_t std_device_descriptor = {
00106         .length = sizeof(usb_standard_device_descriptor_t),
00107         .descriptor_type = USB_DESCTYPE_DEVICE,
00108         .usb_spec_version = 0x110,
00109         .device_class = USB_CLASS_USE_INTERFACE,
00110         .device_subclass = 0,
00111         .device_protocol = 0,
00112         .max_packet_size = 64,
00113         .configuration_count = 1
00114 };
00115 
00117 usbvirt_device_configuration_t configuration = {
00118         .descriptor = &std_configuration_descriptor,
00119         .extra = NULL,
00120         .extra_count = 0
00121 };
00122 
00124 usbvirt_descriptors_t descriptors = {
00125         .device = &std_device_descriptor,
00126         .configuration = &configuration,
00127         .configuration_count = 1,
00128 };
00129 
00130 static vuhid_data_t vuhid_data = {
00131         .in_endpoints_mapping = { NULL },
00132         .in_endpoint_first_free = 1,
00133         .out_endpoints_mapping = { NULL },
00134         .out_endpoint_first_free = 1,
00135 
00136         .iface_count = 0,
00137         .iface_died_count = 0
00138         // mutex and CV must be initialized elsewhere
00139 };
00140 
00141 
00145 static usbvirt_device_t hid_dev = {
00146         .ops = &hid_ops,
00147         .descriptors = &descriptors,
00148         .name = "HID",
00149         .device_data = &vuhid_data
00150 };
00151 
00152 
00153 int main(int argc, char * argv[])
00154 {
00155         int rc;
00156 
00157         usb_log_enable(USB_LOG_LEVEL_DEBUG2, "vusbhid");
00158 
00159         fibril_mutex_initialize(&vuhid_data.iface_count_mutex);
00160         fibril_condvar_initialize(&vuhid_data.iface_count_cv);
00161 
00162         /* Determine which interfaces to initialize. */
00163         int i;
00164         for (i = 1; i < argc; i++) {
00165                 rc = add_interface_by_id(available_hid_interfaces, argv[i],
00166                     &hid_dev);
00167                 if (rc != EOK) {
00168                         fprintf(stderr, "Failed to add device `%s': %s.\n",
00169                             argv[i], str_error(rc));
00170                 } else {
00171                         printf("Added device `%s'.\n", argv[i]);
00172                 }
00173         }
00174 
00175         for (i = 0; i < (int) hid_dev.descriptors->configuration->extra_count; i++) {
00176                 usb_log_debug("Found extra descriptor: %s.\n",
00177                     usb_debug_str_buffer(
00178                         hid_dev.descriptors->configuration->extra[i].data,
00179                         hid_dev.descriptors->configuration->extra[i].length,
00180                         0));
00181         }
00182 
00183         rc = usbvirt_device_plug(&hid_dev, "/virt/usbhc/hc");
00184         if (rc != EOK) {
00185                 printf("Unable to start communication with VHCD: %s.\n",
00186                     str_error(rc));
00187                 return rc;
00188         }
00189         
00190         printf("Connected to VHCD...\n");
00191 
00192         wait_for_interfaces_death(&hid_dev);
00193         
00194         printf("Terminating...\n");
00195         
00196         usbvirt_device_unplug(&hid_dev);
00197 
00198         return 0;
00199 }
00200 
00201 

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