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/dev/driver.h>
|
---|
39 | #include <usbdiag_iface.h>
|
---|
40 | #include <str_error.h>
|
---|
41 |
|
---|
42 | #include "usbdiag.h"
|
---|
43 | #include "device.h"
|
---|
44 |
|
---|
45 | #define NAME "usbdiag"
|
---|
46 |
|
---|
47 | static 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 | usb_diag_dev_t *diag_dev;
|
---|
53 | if ((rc = usb_diag_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 |
|
---|
71 | err_bind:
|
---|
72 | ddf_fun_unbind(diag_dev->fun);
|
---|
73 | err_create:
|
---|
74 | usb_diag_dev_destroy(diag_dev);
|
---|
75 | err:
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static 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 | usb_diag_dev_t *diag_dev = usb_device_to_usb_diag_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 | usb_diag_dev_destroy(diag_dev);
|
---|
94 |
|
---|
95 | return EOK;
|
---|
96 |
|
---|
97 | err:
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static int device_gone(usb_device_t *dev)
|
---|
102 | {
|
---|
103 | usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
|
---|
104 |
|
---|
105 | usb_diag_dev_t *diag_dev = usb_device_to_usb_diag_dev(dev);
|
---|
106 |
|
---|
107 | /* TODO: Make sure nothing is going on with the device. */
|
---|
108 | /* TODO: Unregister device DDF function. */
|
---|
109 | /* TODO: Remove device from list */
|
---|
110 |
|
---|
111 | usb_diag_dev_destroy(diag_dev);
|
---|
112 |
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static int function_online(ddf_fun_t *fun)
|
---|
117 | {
|
---|
118 | return ddf_fun_online(fun);
|
---|
119 | }
|
---|
120 |
|
---|
121 | static int function_offline(ddf_fun_t *fun)
|
---|
122 | {
|
---|
123 | return ddf_fun_offline(fun);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** USB diagnostic driver ops. */
|
---|
127 | static const usb_driver_ops_t diag_driver_ops = {
|
---|
128 | .device_add = device_add,
|
---|
129 | .device_rem = device_remove,
|
---|
130 | .device_gone = device_gone,
|
---|
131 | .function_online = function_online,
|
---|
132 | .function_offline = function_offline
|
---|
133 | };
|
---|
134 |
|
---|
135 | /** USB diagnostic driver. */
|
---|
136 | static const usb_driver_t diag_driver = {
|
---|
137 | .name = NAME,
|
---|
138 | .ops = &diag_driver_ops,
|
---|
139 | .endpoints = NULL
|
---|
140 | };
|
---|
141 |
|
---|
142 | int main(int argc, char *argv[])
|
---|
143 | {
|
---|
144 | printf(NAME ": USB diagnostic device driver.\n");
|
---|
145 |
|
---|
146 | log_init(NAME);
|
---|
147 |
|
---|
148 | return usb_driver_main(&diag_driver);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * @}
|
---|
153 | */
|
---|