dev.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 <usb/dev/pipes.h>
00037 #include <errno.h>
00038 #include <str_error.h>
00039 #include <usb/dev/request.h>
00040 #include "usbinfo.h"
00041 
00042 usbinfo_device_t *prepare_device(const char *name,
00043     devman_handle_t hc_handle, usb_address_t dev_addr)
00044 {
00045         usbinfo_device_t *dev = malloc(sizeof(usbinfo_device_t));
00046         if (dev == NULL) {
00047                 fprintf(stderr, NAME ": memory allocation failed.\n");
00048                 return NULL;
00049         }
00050 
00051         int rc;
00052         bool transfer_started = false;
00053 
00054         rc = usb_device_connection_initialize(&dev->wire, hc_handle, dev_addr);
00055         if (rc != EOK) {
00056                 fprintf(stderr,
00057                     NAME ": failed to create connection to device %s: %s.\n",
00058                     name, str_error(rc));
00059                 goto leave;
00060         }
00061 
00062         rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
00063             &dev->wire);
00064         if (rc != EOK) {
00065                 fprintf(stderr,
00066                     NAME ": failed to create default control pipe to %s: %s.\n",
00067                     name, str_error(rc));
00068                 goto leave;
00069         }
00070 
00071         rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
00072         if (rc != EOK) {
00073                 if (rc == ENOENT) {
00074                         fprintf(stderr, NAME ": " \
00075                             "device %s not present or malfunctioning.\n",
00076                             name);
00077                 } else {
00078                         fprintf(stderr, NAME ": " \
00079                             "probing default control pipe of %s failed: %s.\n",
00080                             name, str_error(rc));
00081                 }
00082                 goto leave;
00083         }
00084 
00085         usb_pipe_start_long_transfer(&dev->ctrl_pipe);
00086         transfer_started = true;
00087 
00088         rc = usb_request_get_device_descriptor(&dev->ctrl_pipe,
00089             &dev->device_descriptor);
00090         if (rc != EOK) {
00091                 fprintf(stderr,
00092                     NAME ": failed to retrieve device descriptor of %s: %s.\n",
00093                     name, str_error(rc));
00094                 goto leave;
00095         }
00096 
00097         rc = usb_request_get_full_configuration_descriptor_alloc(
00098             &dev->ctrl_pipe, 0, (void **)&dev->full_configuration_descriptor,
00099             &dev->full_configuration_descriptor_size);
00100         if (rc != EOK) {
00101                 fprintf(stderr, NAME ": " \
00102                     "failed to retrieve configuration descriptor of %s: %s.\n",
00103                     name, str_error(rc));
00104                 goto leave;
00105         }
00106 
00107         return dev;
00108 
00109 
00110 leave:
00111         if (transfer_started) {
00112                 usb_pipe_end_long_transfer(&dev->ctrl_pipe);
00113         }
00114 
00115         free(dev);
00116 
00117         return NULL;
00118 }
00119 
00120 void destroy_device(usbinfo_device_t *dev)
00121 {
00122         usb_pipe_end_long_transfer(&dev->ctrl_pipe);
00123         free(dev);
00124 }
00125 

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