source: mainline/uspace/app/usbinfo/dev.c@ 96f2aa6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96f2aa6 was 7d521e24, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

libusbdev uses include usb/dev

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[3100ebe]1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup usbinfo
30 * @{
31 */
32/**
33 * @file
34 * Representation of queried device.
35 */
[7d521e24]36#include <usb/dev/pipes.h>
[3100ebe]37#include <errno.h>
38#include <str_error.h>
[7d521e24]39#include <usb/dev/request.h>
[3100ebe]40#include "usbinfo.h"
41
[faf498d]42usbinfo_device_t *prepare_device(const char *name,
43 devman_handle_t hc_handle, usb_address_t dev_addr)
[3100ebe]44{
45 usbinfo_device_t *dev = malloc(sizeof(usbinfo_device_t));
46 if (dev == NULL) {
47 fprintf(stderr, NAME ": memory allocation failed.\n");
48 return NULL;
49 }
50
51 int rc;
[8efafda]52 bool transfer_started = false;
[3100ebe]53
54 rc = usb_device_connection_initialize(&dev->wire, hc_handle, dev_addr);
55 if (rc != EOK) {
56 fprintf(stderr,
[faf498d]57 NAME ": failed to create connection to device %s: %s.\n",
58 name, str_error(rc));
[3100ebe]59 goto leave;
60 }
61
[3954a63b]62 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
[3100ebe]63 &dev->wire);
64 if (rc != EOK) {
65 fprintf(stderr,
[faf498d]66 NAME ": failed to create default control pipe to %s: %s.\n",
67 name, str_error(rc));
[3100ebe]68 goto leave;
69 }
70
[3954a63b]71 rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
[3100ebe]72 if (rc != EOK) {
[faf498d]73 if (rc == ENOENT) {
74 fprintf(stderr, NAME ": " \
75 "device %s not present or malfunctioning.\n",
76 name);
77 } else {
78 fprintf(stderr, NAME ": " \
79 "probing default control pipe of %s failed: %s.\n",
80 name, str_error(rc));
81 }
[3100ebe]82 goto leave;
83 }
84
[2c2cbcf]85 usb_pipe_start_long_transfer(&dev->ctrl_pipe);
[8efafda]86 transfer_started = true;
[3100ebe]87
88 rc = usb_request_get_device_descriptor(&dev->ctrl_pipe,
89 &dev->device_descriptor);
90 if (rc != EOK) {
91 fprintf(stderr,
[faf498d]92 NAME ": failed to retrieve device descriptor of %s: %s.\n",
93 name, str_error(rc));
[3100ebe]94 goto leave;
95 }
96
[e160da4d]97 rc = usb_request_get_full_configuration_descriptor_alloc(
98 &dev->ctrl_pipe, 0, (void **)&dev->full_configuration_descriptor,
99 &dev->full_configuration_descriptor_size);
100 if (rc != EOK) {
[faf498d]101 fprintf(stderr, NAME ": " \
102 "failed to retrieve configuration descriptor of %s: %s.\n",
103 name, str_error(rc));
[e160da4d]104 goto leave;
105 }
106
[3100ebe]107 return dev;
108
109
110leave:
[8efafda]111 if (transfer_started) {
112 usb_pipe_end_long_transfer(&dev->ctrl_pipe);
[3100ebe]113 }
114
115 free(dev);
116
117 return NULL;
118}
119
120void destroy_device(usbinfo_device_t *dev)
121{
[8efafda]122 usb_pipe_end_long_transfer(&dev->ctrl_pipe);
[3100ebe]123 free(dev);
124}
125
126/** @}
127 */
Note: See TracBrowser for help on using the repository browser.