| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2011 Jan Vesely
 | 
|---|
| 3 |  * Copyright (c) 2011 Vojtech Horky
 | 
|---|
| 4 |  * All rights reserved.
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 7 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 8 |  * are met:
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 11 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 12 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 13 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 14 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 15 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 16 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 17 |  *
 | 
|---|
| 18 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 19 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 20 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 21 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 22 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 23 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 24 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 25 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 26 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 27 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #include <usb/dev.h>
 | 
|---|
| 31 | #include <errno.h>
 | 
|---|
| 32 | #include <usb_iface.h>
 | 
|---|
| 33 | #include <str.h>
 | 
|---|
| 34 | #include <stdio.h>
 | 
|---|
| 35 | 
 | 
|---|
| 36 | /** Resolve handle and address of USB device from its path.
 | 
|---|
| 37 |  *
 | 
|---|
| 38 |  * This is a wrapper working on best effort principle.
 | 
|---|
| 39 |  * If the resolving fails, if will not give much details about what
 | 
|---|
| 40 |  * is wrong.
 | 
|---|
| 41 |  * Typically, error from this function would be reported to the user
 | 
|---|
| 42 |  * as "bad device specification" or "device does not exist".
 | 
|---|
| 43 |  *
 | 
|---|
| 44 |  * The path can be specified in following format:
 | 
|---|
| 45 |  *  - devman path (e.g. /hw/pci0/.../usb01_a5
 | 
|---|
| 46 |  *  - bus number and device address (e.g. 5.1)
 | 
|---|
| 47 |  *  - bus number, device address and device function (e.g. 2.1/HID0/keyboard)
 | 
|---|
| 48 |  *
 | 
|---|
| 49 |  * @param[in] dev_path Path to the device.
 | 
|---|
| 50 |  * @param[out] out_hc_handle Where to store handle of a parent host controller.
 | 
|---|
| 51 |  * @param[out] out_dev_addr Where to store device (USB) address.
 | 
|---|
| 52 |  * @param[out] out_dev_handle Where to store device handle.
 | 
|---|
| 53 |  * @return Error code.
 | 
|---|
| 54 |  */
 | 
|---|
| 55 | errno_t usb_resolve_device_handle(const char *dev_path, devman_handle_t *dev_handle)
 | 
|---|
| 56 | {
 | 
|---|
| 57 |         if (dev_path == NULL || dev_handle == NULL) {
 | 
|---|
| 58 |                 return EBADMEM;
 | 
|---|
| 59 |         }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         /* First, try to get the device handle. */
 | 
|---|
| 62 |         errno_t rc = devman_fun_get_handle(dev_path, dev_handle, 0);
 | 
|---|
| 63 | 
 | 
|---|
| 64 |         /* Next, try parsing dev_handle from the provided string */
 | 
|---|
| 65 |         if (rc != EOK) {
 | 
|---|
| 66 |                 *dev_handle = strtoul(dev_path, NULL, 10);
 | 
|---|
| 67 |                 //FIXME: check errno
 | 
|---|
| 68 |                 rc = EOK;
 | 
|---|
| 69 |         }
 | 
|---|
| 70 |         return rc;
 | 
|---|
| 71 | }
 | 
|---|