Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/usbinfo/main.c

    re8f826b r7d521e24  
    4343#include <devman.h>
    4444#include <devmap.h>
    45 #include <usb/hc.h>
     45#include <usb/dev/hc.h>
    4646#include <usb/dev/pipes.h>
     47#include <usb/host.h>
     48#include <usb/driver.h>
    4749#include "usbinfo.h"
     50
     51static bool try_parse_class_and_address(const char *path,
     52    devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
     53{
     54        size_t class_index;
     55        size_t address;
     56        int rc;
     57        char *ptr;
     58
     59        rc = str_size_t(path, &ptr, 10, false, &class_index);
     60        if (rc != EOK) {
     61                return false;
     62        }
     63        if ((*ptr == ':') || (*ptr == '.')) {
     64                ptr++;
     65        } else {
     66                return false;
     67        }
     68        rc = str_size_t(ptr, NULL, 10, true, &address);
     69        if (rc != EOK) {
     70                return false;
     71        }
     72        rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle);
     73        if (rc != EOK) {
     74                return false;
     75        }
     76        if (out_device_address != NULL) {
     77                *out_device_address = (usb_address_t) address;
     78        }
     79        return true;
     80}
     81
     82static bool resolve_hc_handle_and_dev_addr(const char *devpath,
     83    devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
     84{
     85        int rc;
     86
     87        /* Hack for QEMU to save-up on typing ;-). */
     88        if (str_cmp(devpath, "qemu") == 0) {
     89                devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
     90        }
     91
     92        /* Hack for virtual keyboard. */
     93        if (str_cmp(devpath, "virt") == 0) {
     94                devpath = "/virt/usbhc/usb00_a1/usb00_a2";
     95        }
     96
     97        if (try_parse_class_and_address(devpath,
     98            out_hc_handle, out_device_address)) {
     99                return true;
     100        }
     101
     102        char *path = str_dup(devpath);
     103        if (path == NULL) {
     104                return ENOMEM;
     105        }
     106
     107        devman_handle_t hc = 0;
     108        bool hc_found = false;
     109        usb_address_t addr = 0;
     110        bool addr_found = false;
     111
     112        /* Remove suffixes and hope that we will encounter device node. */
     113        while (str_length(path) > 0) {
     114                /* Get device handle first. */
     115                devman_handle_t dev_handle;
     116                rc = devman_device_get_handle(path, &dev_handle, 0);
     117                if (rc != EOK) {
     118                        free(path);
     119                        return false;
     120                }
     121
     122                /* Try to find its host controller. */
     123                if (!hc_found) {
     124                        rc = usb_hc_find(dev_handle, &hc);
     125                        if (rc == EOK) {
     126                                hc_found = true;
     127                        }
     128                }
     129                /* Try to get its address. */
     130                if (!addr_found) {
     131                        addr = usb_device_get_assigned_address(dev_handle);
     132                        if (addr >= 0) {
     133                                addr_found = true;
     134                        }
     135                }
     136
     137                /* Speed-up. */
     138                if (hc_found && addr_found) {
     139                        break;
     140                }
     141
     142                /* Remove the last suffix. */
     143                char *slash_pos = str_rchr(path, '/');
     144                if (slash_pos != NULL) {
     145                        *slash_pos = 0;
     146                }
     147        }
     148
     149        free(path);
     150
     151        if (hc_found && addr_found) {
     152                if (out_hc_handle != NULL) {
     153                        *out_hc_handle = hc;
     154                }
     155                if (out_device_address != NULL) {
     156                        *out_device_address = addr;
     157                }
     158                return true;
     159        } else {
     160                return false;
     161        }
     162}
    48163
    49164static void print_usage(char *app_name)
     
    185300                devman_handle_t hc_handle = 0;
    186301                usb_address_t dev_addr = 0;
    187                 int rc = usb_resolve_device_handle(devpath,
    188                     &hc_handle, &dev_addr, NULL);
    189                 if (rc != EOK) {
     302                bool found = resolve_hc_handle_and_dev_addr(devpath,
     303                    &hc_handle, &dev_addr);
     304                if (!found) {
    190305                        fprintf(stderr, NAME ": device `%s' not found "
    191306                            "or not of USB kind, skipping.\n",
Note: See TracChangeset for help on using the changeset viewer.