source: mainline/uspace/drv/bus/usb/usbdiag/device.c@ 6a1211c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a1211c was 6a1211c, checked in by Petr Mánek <petr.manek@…>, 8 years ago

usbdiag: simple bulk out in test

  • Property mode set to 100644
File size: 4.5 KB
Line 
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
44#define NAME "usbdiag"
45
46static const usb_endpoint_description_t bulk_in_ep = {
47 .transfer_type = USB_TRANSFER_BULK,
48 .direction = USB_DIRECTION_IN,
49 .interface_class = 0xDC,
50 .interface_subclass = 0x00,
51 .interface_protocol = 0x01,
52 .flags = 0
53};
54
55static const usb_endpoint_description_t bulk_out_ep = {
56 .transfer_type = USB_TRANSFER_BULK,
57 .direction = USB_DIRECTION_OUT,
58 .interface_class = 0xDC,
59 .interface_subclass = 0x00,
60 .interface_protocol = 0x01,
61 .flags = 0
62};
63
64static int some_test(ddf_fun_t *fun, int x, int *y)
65{
66 int rc = EOK;
67 usb_diag_dev_t *dev = ddf_fun_to_usb_diag_dev(fun);
68
69 const size_t size = min(dev->bulk_in->desc.max_packet_size, dev->bulk_out->desc.max_packet_size);
70 char *buffer = (char *) malloc(size);
71 memset(buffer, 42, sizeof(buffer));
72
73 // Write buffer to device.
74 if ((rc = usb_pipe_write(dev->bulk_out, buffer, size))) {
75 usb_log_error("Bulk OUT write failed. %s\n", str_error(rc));
76 }
77
78 // Read device's response.
79 size_t remaining = size;
80 size_t transferred;
81 while (remaining > 0) {
82 if ((rc = usb_pipe_read(dev->bulk_in, buffer + size - remaining, remaining, &transferred))) {
83 usb_log_error("Bulk IN read failed. %s\n", str_error(rc));
84 break;
85 }
86
87 if (transferred > remaining) {
88 usb_log_error("Bulk IN read more than expected.\n");
89 rc = EINVAL;
90 break;
91 }
92
93 remaining -= transferred;
94 }
95
96 // TODO: Check output?
97
98 free(buffer);
99
100 *y = x + 42;
101 return rc;
102}
103
104static usbdiag_iface_t diag_interface = {
105 .test = some_test,
106};
107
108static ddf_dev_ops_t diag_ops = {
109 .interfaces[USBDIAG_DEV_IFACE] = &diag_interface
110};
111
112static int device_init(usb_diag_dev_t *dev)
113{
114 int rc;
115 ddf_fun_t *fun = usb_device_ddf_fun_create(dev->usb_dev, fun_exposed, "tmon");
116 if (!fun) {
117 rc = ENOMEM;
118 goto err;
119 }
120
121 ddf_fun_set_ops(fun, &diag_ops);
122 dev->fun = fun;
123
124 usb_endpoint_mapping_t *epm_out = usb_device_get_mapped_ep_desc(dev->usb_dev, &bulk_out_ep);
125 usb_endpoint_mapping_t *epm_in = usb_device_get_mapped_ep_desc(dev->usb_dev, &bulk_in_ep);
126
127 if (!epm_in || !epm_out || !epm_in->present || !epm_out->present) {
128 usb_log_error("Required EPs were not mapped.\n");
129 rc = ENOENT;
130 goto err_fun;
131 }
132
133 dev->bulk_out = &epm_out->pipe;
134 dev->bulk_in = &epm_in->pipe;
135
136 return EOK;
137
138err_fun:
139 ddf_fun_destroy(fun);
140err:
141 return rc;
142}
143
144static void device_fini(usb_diag_dev_t *dev)
145{
146 ddf_fun_destroy(dev->fun);
147}
148
149int usb_diag_dev_create(usb_device_t *dev, usb_diag_dev_t **out_diag_dev)
150{
151 assert(dev);
152 assert(out_diag_dev);
153
154 usb_diag_dev_t *diag_dev = usb_device_data_alloc(dev, sizeof(usb_diag_dev_t));
155 if (!diag_dev)
156 return ENOMEM;
157
158 diag_dev->usb_dev = dev;
159
160 int err;
161 if ((err = device_init(diag_dev)))
162 goto err_init;
163
164 *out_diag_dev = diag_dev;
165 return EOK;
166
167err_init:
168 /* There is no usb_device_data_free. */
169 return err;
170}
171
172void usb_diag_dev_destroy(usb_diag_dev_t *dev)
173{
174 assert(dev);
175
176 device_fini(dev);
177 /* There is no usb_device_data_free. */
178}
179
180/**
181 * @}
182 */
Note: See TracBrowser for help on using the repository browser.