1 | /*
|
---|
2 | * Copyright (c) 2017 Petr Manek
|
---|
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 drvusbdiag
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Code for managing debug device structures.
|
---|
35 | */
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usbdiag_iface.h>
|
---|
41 |
|
---|
42 | #include "device.h"
|
---|
43 | #include "tests.h"
|
---|
44 |
|
---|
45 | #define NAME "usbdiag"
|
---|
46 |
|
---|
47 | static usbdiag_iface_t diag_interface = {
|
---|
48 | .test_in = usbdiag_dev_test_in,
|
---|
49 | .test_out = usbdiag_dev_test_out,
|
---|
50 | };
|
---|
51 |
|
---|
52 | static ddf_dev_ops_t diag_ops = {
|
---|
53 | .interfaces[USBDIAG_DEV_IFACE] = &diag_interface
|
---|
54 | };
|
---|
55 |
|
---|
56 | static errno_t device_init(usbdiag_dev_t *dev, const usb_endpoint_description_t **endpoints)
|
---|
57 | {
|
---|
58 | errno_t rc;
|
---|
59 | ddf_fun_t *fun = usb_device_ddf_fun_create(dev->usb_dev, fun_exposed, "tmon");
|
---|
60 | if (!fun) {
|
---|
61 | rc = ENOMEM;
|
---|
62 | goto err;
|
---|
63 | }
|
---|
64 |
|
---|
65 | ddf_fun_set_ops(fun, &diag_ops);
|
---|
66 | dev->fun = fun;
|
---|
67 |
|
---|
68 | #define _MAP_EP(target, ep_no) do {\
|
---|
69 | usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep_desc(dev->usb_dev, endpoints[USBDIAG_EP_##ep_no]);\
|
---|
70 | if (!epm || !epm->present) {\
|
---|
71 | usb_log_error("Failed to map endpoint: " #ep_no ".");\
|
---|
72 | rc = ENOENT;\
|
---|
73 | goto err_fun;\
|
---|
74 | }\
|
---|
75 | target = &epm->pipe;\
|
---|
76 | } while (0);
|
---|
77 |
|
---|
78 | _MAP_EP(dev->burst_intr_in, BURST_INTR_IN);
|
---|
79 | _MAP_EP(dev->burst_intr_out, BURST_INTR_OUT);
|
---|
80 | _MAP_EP(dev->burst_bulk_in, BURST_BULK_IN);
|
---|
81 | _MAP_EP(dev->burst_bulk_out, BURST_BULK_OUT);
|
---|
82 | _MAP_EP(dev->burst_isoch_in, BURST_ISOCH_IN);
|
---|
83 | _MAP_EP(dev->burst_isoch_out, BURST_ISOCH_OUT);
|
---|
84 | _MAP_EP(dev->data_intr_in, DATA_INTR_IN);
|
---|
85 | _MAP_EP(dev->data_intr_out, DATA_INTR_OUT);
|
---|
86 | _MAP_EP(dev->data_bulk_in, DATA_BULK_IN);
|
---|
87 | _MAP_EP(dev->data_bulk_out, DATA_BULK_OUT);
|
---|
88 | _MAP_EP(dev->data_isoch_in, DATA_ISOCH_IN);
|
---|
89 | _MAP_EP(dev->data_isoch_out, DATA_ISOCH_OUT);
|
---|
90 |
|
---|
91 | #undef _MAP_EP
|
---|
92 |
|
---|
93 | return EOK;
|
---|
94 |
|
---|
95 | err_fun:
|
---|
96 | ddf_fun_destroy(fun);
|
---|
97 | err:
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void device_fini(usbdiag_dev_t *dev)
|
---|
102 | {
|
---|
103 | ddf_fun_destroy(dev->fun);
|
---|
104 | }
|
---|
105 |
|
---|
106 | errno_t usbdiag_dev_create(usb_device_t *dev, usbdiag_dev_t **out_diag_dev, const usb_endpoint_description_t **endpoints)
|
---|
107 | {
|
---|
108 | assert(dev);
|
---|
109 | assert(out_diag_dev);
|
---|
110 |
|
---|
111 | usbdiag_dev_t *diag_dev = usb_device_data_alloc(dev, sizeof(usbdiag_dev_t));
|
---|
112 | if (!diag_dev)
|
---|
113 | return ENOMEM;
|
---|
114 |
|
---|
115 | diag_dev->usb_dev = dev;
|
---|
116 |
|
---|
117 | errno_t err;
|
---|
118 | if ((err = device_init(diag_dev, endpoints)))
|
---|
119 | goto err_init;
|
---|
120 |
|
---|
121 | *out_diag_dev = diag_dev;
|
---|
122 | return EOK;
|
---|
123 |
|
---|
124 | err_init:
|
---|
125 | /* There is no usb_device_data_free. */
|
---|
126 | return err;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void usbdiag_dev_destroy(usbdiag_dev_t *dev)
|
---|
130 | {
|
---|
131 | assert(dev);
|
---|
132 |
|
---|
133 | device_fini(dev);
|
---|
134 | /* There is no usb_device_data_free. */
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @}
|
---|
139 | */
|
---|