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 | */
|
---|
36 | #include <usb/dev.h>
|
---|
37 | #include <usb/hc.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <usb/dev/request.h>
|
---|
41 | #include "usbinfo.h"
|
---|
42 |
|
---|
43 | usbinfo_device_t *prepare_device(const char *name,
|
---|
44 | devman_handle_t hc_handle, usb_address_t dev_addr)
|
---|
45 | {
|
---|
46 | usbinfo_device_t *dev = malloc(sizeof(usbinfo_device_t));
|
---|
47 | if (dev == NULL) {
|
---|
48 | fprintf(stderr, NAME ": memory allocation failed.\n");
|
---|
49 | return NULL;
|
---|
50 | }
|
---|
51 |
|
---|
52 | int rc;
|
---|
53 | bool transfer_started = false;
|
---|
54 |
|
---|
55 | usb_hc_connection_initialize(&dev->hc_conn, hc_handle);
|
---|
56 |
|
---|
57 | rc = usb_device_connection_initialize(
|
---|
58 | &dev->wire, &dev->hc_conn, dev_addr);
|
---|
59 | if (rc != EOK) {
|
---|
60 | fprintf(stderr,
|
---|
61 | NAME ": failed to create connection to device %s: %s.\n",
|
---|
62 | name, str_error(rc));
|
---|
63 | goto leave;
|
---|
64 | }
|
---|
65 |
|
---|
66 | rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
|
---|
67 | &dev->wire);
|
---|
68 | if (rc != EOK) {
|
---|
69 | fprintf(stderr,
|
---|
70 | NAME ": failed to create default control pipe to %s: %s.\n",
|
---|
71 | name, str_error(rc));
|
---|
72 | goto leave;
|
---|
73 | }
|
---|
74 |
|
---|
75 | rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
|
---|
76 | if (rc != EOK) {
|
---|
77 | if (rc == ENOENT) {
|
---|
78 | fprintf(stderr, NAME ": " \
|
---|
79 | "device %s not present or malfunctioning.\n",
|
---|
80 | name);
|
---|
81 | } else {
|
---|
82 | fprintf(stderr, NAME ": " \
|
---|
83 | "probing default control pipe of %s failed: %s.\n",
|
---|
84 | name, str_error(rc));
|
---|
85 | }
|
---|
86 | goto leave;
|
---|
87 | }
|
---|
88 |
|
---|
89 | usb_pipe_start_long_transfer(&dev->ctrl_pipe);
|
---|
90 | transfer_started = true;
|
---|
91 |
|
---|
92 | rc = usb_request_get_device_descriptor(&dev->ctrl_pipe,
|
---|
93 | &dev->device_descriptor);
|
---|
94 | if (rc != EOK) {
|
---|
95 | fprintf(stderr,
|
---|
96 | NAME ": failed to retrieve device descriptor of %s: %s.\n",
|
---|
97 | name, str_error(rc));
|
---|
98 | goto leave;
|
---|
99 | }
|
---|
100 |
|
---|
101 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
102 | &dev->ctrl_pipe, 0, (void **)&dev->full_configuration_descriptor,
|
---|
103 | &dev->full_configuration_descriptor_size);
|
---|
104 | if (rc != EOK) {
|
---|
105 | fprintf(stderr, NAME ": " \
|
---|
106 | "failed to retrieve configuration descriptor of %s: %s.\n",
|
---|
107 | name, str_error(rc));
|
---|
108 | goto leave;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return dev;
|
---|
112 |
|
---|
113 |
|
---|
114 | leave:
|
---|
115 | if (transfer_started) {
|
---|
116 | usb_pipe_end_long_transfer(&dev->ctrl_pipe);
|
---|
117 | }
|
---|
118 |
|
---|
119 | free(dev);
|
---|
120 |
|
---|
121 | return NULL;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void destroy_device(usbinfo_device_t *dev)
|
---|
125 | {
|
---|
126 | usb_pipe_end_long_transfer(&dev->ctrl_pipe);
|
---|
127 | free(dev);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** @}
|
---|
131 | */
|
---|