00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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