source: mainline/uspace/drv/bus/usb/usbdiag/main.c@ cf5cbac4

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

usbdiag: use dev_removed for data cleanup

  • Property mode set to 100644
File size: 6.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 * Main routines of USB diagnostic device driver.
35 */
36#include <errno.h>
37#include <usb/debug.h>
38#include <usb/classes/classes.h>
39#include <usb/dev/driver.h>
40#include <usbdiag_iface.h>
41#include <str_error.h>
42
43#include "device.h"
44
45#define NAME "usbdiag"
46
47static int device_add(usb_device_t *dev)
48{
49 int rc;
50 usb_log_info("Adding device '%s'", usb_device_get_name(dev));
51
52 usbdiag_dev_t *diag_dev;
53 if ((rc = usbdiag_dev_create(dev, &diag_dev))) {
54 usb_log_error("Failed create device: %s.\n", str_error(rc));
55 goto err;
56 }
57
58 if ((rc = ddf_fun_bind(diag_dev->fun))) {
59 usb_log_error("Failed to bind DDF function: %s.\n", str_error(rc));
60 goto err_create;
61 }
62
63 if ((rc = ddf_fun_add_to_category(diag_dev->fun, USBDIAG_CATEGORY))) {
64 usb_log_error("Failed add DDF to category '"
65 USBDIAG_CATEGORY "': %s.\n", str_error(rc));
66 goto err_bind;
67 }
68
69 return EOK;
70
71err_bind:
72 ddf_fun_unbind(diag_dev->fun);
73err_create:
74 usbdiag_dev_destroy(diag_dev);
75err:
76 return rc;
77}
78
79static int device_remove(usb_device_t *dev)
80{
81 int rc;
82 usb_log_info("Removing device '%s'", usb_device_get_name(dev));
83
84 usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
85
86 /* TODO: Make sure nothing is going on with the device. */
87
88 if ((rc = ddf_fun_unbind(diag_dev->fun))) {
89 usb_log_error("Failed to unbind DDF function: %s\n", str_error(rc));
90 goto err;
91 }
92
93 return EOK;
94
95err:
96 return rc;
97}
98
99static int device_cleanup(usbdiag_dev_t *diag_dev)
100{
101 /* TODO: Join some fibrils? */
102
103 /* Free memory. */
104 usbdiag_dev_destroy(diag_dev);
105 return EOK;
106}
107
108static int device_removed(usb_device_t *dev)
109{
110 usb_log_info("Device '%s' removed.", usb_device_get_name(dev));
111
112 usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
113 return device_cleanup(diag_dev);
114}
115
116static int device_gone(usb_device_t *dev)
117{
118 int rc;
119 usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
120
121 usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
122
123 /* TODO: Make sure nothing is going on with the device. */
124
125 if ((rc = ddf_fun_unbind(diag_dev->fun))) {
126 usb_log_error("Failed to unbind DDF function: %s\n", str_error(rc));
127 goto err;
128 }
129
130 return device_cleanup(diag_dev);
131
132err:
133 return rc;
134}
135
136static int function_online(ddf_fun_t *fun)
137{
138 return ddf_fun_online(fun);
139}
140
141static int function_offline(ddf_fun_t *fun)
142{
143 return ddf_fun_offline(fun);
144}
145
146static const usb_endpoint_description_t intr_in_ep = {
147 .transfer_type = USB_TRANSFER_INTERRUPT,
148 .direction = USB_DIRECTION_IN,
149 .interface_class = USB_CLASS_DIAGNOSTIC,
150 .interface_subclass = 0x00,
151 .interface_protocol = 0x01,
152 .flags = 0
153};
154static const usb_endpoint_description_t intr_out_ep = {
155 .transfer_type = USB_TRANSFER_INTERRUPT,
156 .direction = USB_DIRECTION_OUT,
157 .interface_class = USB_CLASS_DIAGNOSTIC,
158 .interface_subclass = 0x00,
159 .interface_protocol = 0x01,
160 .flags = 0
161};
162static const usb_endpoint_description_t bulk_in_ep = {
163 .transfer_type = USB_TRANSFER_BULK,
164 .direction = USB_DIRECTION_IN,
165 .interface_class = USB_CLASS_DIAGNOSTIC,
166 .interface_subclass = 0x00,
167 .interface_protocol = 0x01,
168 .flags = 0
169};
170static const usb_endpoint_description_t bulk_out_ep = {
171 .transfer_type = USB_TRANSFER_BULK,
172 .direction = USB_DIRECTION_OUT,
173 .interface_class = USB_CLASS_DIAGNOSTIC,
174 .interface_subclass = 0x00,
175 .interface_protocol = 0x01,
176 .flags = 0
177};
178static const usb_endpoint_description_t isoch_in_ep = {
179 .transfer_type = USB_TRANSFER_ISOCHRONOUS,
180 .direction = USB_DIRECTION_IN,
181 .interface_class = USB_CLASS_DIAGNOSTIC,
182 .interface_subclass = 0x00,
183 .interface_protocol = 0x01,
184 .flags = 0
185};
186static const usb_endpoint_description_t isoch_out_ep = {
187 .transfer_type = USB_TRANSFER_ISOCHRONOUS,
188 .direction = USB_DIRECTION_OUT,
189 .interface_class = USB_CLASS_DIAGNOSTIC,
190 .interface_subclass = 0x00,
191 .interface_protocol = 0x01,
192 .flags = 0
193};
194
195static const usb_endpoint_description_t *diag_endpoints[] = {
196 [USBDIAG_EP_INTR_IN] = &intr_in_ep,
197 [USBDIAG_EP_INTR_OUT] = &intr_out_ep,
198 [USBDIAG_EP_BULK_IN] = &bulk_in_ep,
199 [USBDIAG_EP_BULK_OUT] = &bulk_out_ep,
200 [USBDIAG_EP_ISOCH_IN] = &isoch_in_ep,
201 [USBDIAG_EP_ISOCH_OUT] = &isoch_out_ep,
202 NULL
203};
204
205/** USB diagnostic driver ops. */
206static const usb_driver_ops_t diag_driver_ops = {
207 .device_add = device_add,
208 .device_remove = device_remove,
209 .device_removed = device_removed,
210 .device_gone = device_gone,
211 .function_online = function_online,
212 .function_offline = function_offline
213};
214
215/** USB diagnostic driver. */
216static const usb_driver_t diag_driver = {
217 .name = NAME,
218 .ops = &diag_driver_ops,
219 .endpoints = &diag_endpoints[1] /* EPs are indexed from 1. */
220};
221
222int main(int argc, char *argv[])
223{
224 printf(NAME ": USB diagnostic device driver.\n");
225
226 log_init(NAME);
227
228 return usb_driver_main(&diag_driver);
229}
230
231/**
232 * @}
233 */
Note: See TracBrowser for help on using the repository browser.