source: mainline/uspace/drv/bus/usb/usbdiag/device.c@ 85bf12ba

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

usbdiag: mapping endpoints for diagnostic devices the right way

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