1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup drvusbohci
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * @brief OHCI driver
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <usb/host/utils/malloc32.h>
|
---|
40 | #include <usb/host/bandwidth.h>
|
---|
41 |
|
---|
42 | #include "ohci_bus.h"
|
---|
43 | #include "ohci_batch.h"
|
---|
44 | #include "hc.h"
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Callback to reset toggle on ED.
|
---|
48 | *
|
---|
49 | * @param[in] hcd_ep hcd endpoint structure
|
---|
50 | * @param[in] toggle new value of toggle bit
|
---|
51 | */
|
---|
52 | void ohci_ep_toggle_reset(endpoint_t *ep)
|
---|
53 | {
|
---|
54 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
55 | assert(instance);
|
---|
56 | assert(instance->ed);
|
---|
57 | ed_toggle_set(instance->ed, 0);
|
---|
58 | }
|
---|
59 |
|
---|
60 | static int ohci_device_enumerate(device_t *dev)
|
---|
61 | {
|
---|
62 | ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
|
---|
63 | return usb2_bus_device_enumerate(&bus->helper, dev);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static void ohci_device_gone(device_t *dev)
|
---|
67 | {
|
---|
68 | ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
|
---|
69 | usb2_bus_device_gone(&bus->helper, dev);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Creates new hcd endpoint representation.
|
---|
73 | */
|
---|
74 | static endpoint_t *ohci_endpoint_create(device_t *dev,
|
---|
75 | const usb_endpoint_descriptors_t *desc)
|
---|
76 | {
|
---|
77 | assert(dev);
|
---|
78 |
|
---|
79 | ohci_endpoint_t *ohci_ep = calloc(1, sizeof(ohci_endpoint_t));
|
---|
80 | if (ohci_ep == NULL)
|
---|
81 | return NULL;
|
---|
82 |
|
---|
83 | endpoint_init(&ohci_ep->base, dev, desc);
|
---|
84 |
|
---|
85 | const errno_t err = dma_buffer_alloc(&ohci_ep->dma_buffer,
|
---|
86 | sizeof(ed_t) + 2 * sizeof(td_t));
|
---|
87 | if (err) {
|
---|
88 | free(ohci_ep);
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | ohci_ep->ed = ohci_ep->dma_buffer.virt;
|
---|
93 |
|
---|
94 | ohci_ep->tds[0] = (td_t *) ohci_ep->ed + 1;
|
---|
95 | ohci_ep->tds[1] = ohci_ep->tds[0] + 1;
|
---|
96 |
|
---|
97 | link_initialize(&ohci_ep->eplist_link);
|
---|
98 | link_initialize(&ohci_ep->pending_link);
|
---|
99 | return &ohci_ep->base;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Disposes hcd endpoint structure
|
---|
103 | *
|
---|
104 | * @param[in] hcd driver using this instance.
|
---|
105 | * @param[in] ep endpoint structure.
|
---|
106 | */
|
---|
107 | static void ohci_endpoint_destroy(endpoint_t *ep)
|
---|
108 | {
|
---|
109 | assert(ep);
|
---|
110 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
111 |
|
---|
112 | dma_buffer_free(&instance->dma_buffer);
|
---|
113 | free(instance);
|
---|
114 | }
|
---|
115 |
|
---|
116 | static int ohci_register_ep(endpoint_t *ep)
|
---|
117 | {
|
---|
118 | bus_t *bus_base = endpoint_get_bus(ep);
|
---|
119 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
120 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
121 |
|
---|
122 | const int err = usb2_bus_endpoint_register(&bus->helper, ep);
|
---|
123 | if (err)
|
---|
124 | return err;
|
---|
125 |
|
---|
126 | ed_init(ohci_ep->ed, ep, ohci_ep->tds[0]);
|
---|
127 | hc_enqueue_endpoint(bus->hc, ep);
|
---|
128 | endpoint_set_online(ep, &bus->hc->guard);
|
---|
129 |
|
---|
130 | return EOK;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void ohci_unregister_ep(endpoint_t *ep)
|
---|
134 | {
|
---|
135 | ohci_bus_t *const bus = (ohci_bus_t *) endpoint_get_bus(ep);
|
---|
136 | hc_t *const hc = bus->hc;
|
---|
137 | assert(ep);
|
---|
138 |
|
---|
139 | usb2_bus_endpoint_unregister(&bus->helper, ep);
|
---|
140 | hc_dequeue_endpoint(bus->hc, ep);
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Now we can be sure the active transfer will not be completed,
|
---|
144 | * as it's out of the schedule, and HC acknowledged it.
|
---|
145 | */
|
---|
146 |
|
---|
147 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
148 |
|
---|
149 | fibril_mutex_lock(&hc->guard);
|
---|
150 | endpoint_set_offline_locked(ep);
|
---|
151 | list_remove(&ohci_ep->pending_link);
|
---|
152 | usb_transfer_batch_t *const batch = ep->active_batch;
|
---|
153 | endpoint_deactivate_locked(ep);
|
---|
154 | fibril_mutex_unlock(&hc->guard);
|
---|
155 |
|
---|
156 | if (batch) {
|
---|
157 | batch->error = EINTR;
|
---|
158 | batch->transferred_size = 0;
|
---|
159 | usb_transfer_batch_finish(batch);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | static usb_transfer_batch_t *ohci_create_batch(endpoint_t *ep)
|
---|
164 | {
|
---|
165 | ohci_transfer_batch_t *batch = ohci_transfer_batch_create(ep);
|
---|
166 | return &batch->base;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void ohci_destroy_batch(usb_transfer_batch_t *batch)
|
---|
170 | {
|
---|
171 | ohci_transfer_batch_destroy(ohci_transfer_batch_get(batch));
|
---|
172 | }
|
---|
173 |
|
---|
174 | static const bus_ops_t ohci_bus_ops = {
|
---|
175 | .interrupt = ohci_hc_interrupt,
|
---|
176 | .status = ohci_hc_status,
|
---|
177 |
|
---|
178 | .device_enumerate = ohci_device_enumerate,
|
---|
179 | .device_gone = ohci_device_gone,
|
---|
180 |
|
---|
181 | .endpoint_destroy = ohci_endpoint_destroy,
|
---|
182 | .endpoint_create = ohci_endpoint_create,
|
---|
183 | .endpoint_register = ohci_register_ep,
|
---|
184 | .endpoint_unregister = ohci_unregister_ep,
|
---|
185 |
|
---|
186 | .batch_create = ohci_create_batch,
|
---|
187 | .batch_destroy = ohci_destroy_batch,
|
---|
188 | .batch_schedule = ohci_hc_schedule,
|
---|
189 | };
|
---|
190 |
|
---|
191 | int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
|
---|
192 | {
|
---|
193 | assert(hc);
|
---|
194 | assert(bus);
|
---|
195 |
|
---|
196 | bus_t *bus_base = (bus_t *) bus;
|
---|
197 | bus_init(bus_base, sizeof(device_t));
|
---|
198 | bus_base->ops = &ohci_bus_ops;
|
---|
199 |
|
---|
200 | usb2_bus_helper_init(&bus->helper, &bandwidth_accounting_usb11);
|
---|
201 |
|
---|
202 | bus->hc = hc;
|
---|
203 |
|
---|
204 | return EOK;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * @}
|
---|
209 | */
|
---|