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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41abf3c was a1732929, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usb: unified logging

Use logger instead of printf. Logger adds newlines automatically.

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