source: mainline/uspace/drv/bus/usb/usbdiag/device.c@ 83fb72e

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

usbdiag: addressing endpoints by their numbers

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