usbmid.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 
00036 #include <errno.h>
00037 #include <str_error.h>
00038 #include <stdlib.h>
00039 #include <usb_iface.h>
00040 #include <usb/ddfiface.h>
00041 #include <usb/dev/pipes.h>
00042 #include <usb/classes/classes.h>
00043 #include <usb/dev/recognise.h>
00044 #include "usbmid.h"
00045 
00047 static int usb_iface_get_address_impl(ddf_fun_t *fun, devman_handle_t handle,
00048     usb_address_t *address)
00049 {
00050         return usb_iface_get_address_hub_impl(fun, handle, address);
00051 }
00052 
00054 static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
00055     int *iface_no)
00056 {
00057         assert(fun);
00058 
00059         usbmid_interface_t *iface = fun->driver_data;
00060         assert(iface);
00061 
00062         if (iface_no != NULL) {
00063                 *iface_no = iface->interface_no;
00064         }
00065 
00066         return EOK;
00067 }
00068 
00070 static usb_iface_t child_usb_iface = {
00071         .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
00072         .get_address = usb_iface_get_address_impl,
00073         .get_interface = usb_iface_get_interface_impl
00074 };
00075 
00077 static ddf_dev_ops_t child_device_ops = {
00078         .interfaces[USB_DEV_IFACE] = &child_usb_iface
00079 };
00080 
00081 
00090 int usbmid_spawn_interface_child(usb_device_t *parent,
00091     usbmid_interface_t *iface,
00092     const usb_standard_device_descriptor_t *device_descriptor,
00093     const usb_standard_interface_descriptor_t *interface_descriptor)
00094 {
00095         ddf_fun_t *child = NULL;
00096         char *child_name = NULL;
00097         int rc;
00098 
00099         /*
00100          * Name is class name followed by interface number.
00101          * The interface number shall provide uniqueness while the
00102          * class name something humanly understandable.
00103          */
00104         rc = asprintf(&child_name, "%s%d",
00105             usb_str_class(interface_descriptor->interface_class),
00106             (int) interface_descriptor->interface_number);
00107         if (rc < 0) {
00108                 goto error_leave;
00109         }
00110 
00111         /* Create the device. */
00112         child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
00113         if (child == NULL) {
00114                 rc = ENOMEM;
00115                 goto error_leave;
00116         }
00117 
00118         iface->fun = child;
00119 
00120         child->driver_data = iface;
00121         child->ops = &child_device_ops;
00122 
00123         rc = usb_device_create_match_ids_from_interface(device_descriptor,
00124             interface_descriptor,
00125             &child->match_ids);
00126         if (rc != EOK) {
00127                 goto error_leave;
00128         }
00129 
00130         rc = ddf_fun_bind(child);
00131         if (rc != EOK) {
00132                 goto error_leave;
00133         }
00134 
00135         return EOK;
00136 
00137 error_leave:
00138         if (child != NULL) {
00139                 child->name = NULL;
00140                 /* This takes care of match_id deallocation as well. */
00141                 ddf_fun_destroy(child);
00142         }
00143         if (child_name != NULL) {
00144                 free(child_name);
00145         }
00146 
00147         return rc;
00148 }
00149 

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