source: mainline/uspace/drv/bus/usb/ohci/ohci_bus.c@ a1ce9bd

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

libusbhost: usb2_bus needs to release address when the device is gone

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
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 drvusbohci
30 * @{
31 */
32/** @file
33 * @brief OHCI driver
34 */
35
36#include <assert.h>
37#include <stdlib.h>
38#include <usb/host/utils/malloc32.h>
39#include <usb/host/bandwidth.h>
40
41#include "ohci_bus.h"
42#include "ohci_batch.h"
43#include "hc.h"
44
45/**
46 * Callback to reset toggle on ED.
47 *
48 * @param[in] hcd_ep hcd endpoint structure
49 * @param[in] toggle new value of toggle bit
50 */
51void ohci_ep_toggle_reset(endpoint_t *ep)
52{
53 ohci_endpoint_t *instance = ohci_endpoint_get(ep);
54 assert(instance);
55 assert(instance->ed);
56 ed_toggle_set(instance->ed, 0);
57}
58
59static int ohci_device_enumerate(device_t *dev)
60{
61 ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
62 return usb2_bus_device_enumerate(&bus->helper, dev);
63}
64
65static void ohci_device_gone(device_t *dev)
66{
67 ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
68 usb2_bus_device_gone(&bus->helper, dev);
69}
70
71/** Creates new hcd endpoint representation.
72 */
73static endpoint_t *ohci_endpoint_create(device_t *dev, const usb_endpoint_descriptors_t *desc)
74{
75 assert(dev);
76
77 ohci_endpoint_t *ohci_ep = malloc(sizeof(ohci_endpoint_t));
78 if (ohci_ep == NULL)
79 return NULL;
80
81 endpoint_init(&ohci_ep->base, dev, desc);
82
83 ohci_ep->ed = malloc32(sizeof(ed_t));
84 if (ohci_ep->ed == NULL) {
85 free(ohci_ep);
86 return NULL;
87 }
88
89 ohci_ep->td = malloc32(sizeof(td_t));
90 if (ohci_ep->td == NULL) {
91 free32(ohci_ep->ed);
92 free(ohci_ep);
93 return NULL;
94 }
95
96 link_initialize(&ohci_ep->eplist_link);
97 link_initialize(&ohci_ep->pending_link);
98 return &ohci_ep->base;
99}
100
101/** Disposes hcd endpoint structure
102 *
103 * @param[in] hcd driver using this instance.
104 * @param[in] ep endpoint structure.
105 */
106static void ohci_endpoint_destroy(endpoint_t *ep)
107{
108 assert(ep);
109 ohci_endpoint_t *instance = ohci_endpoint_get(ep);
110
111 free32(instance->ed);
112 free32(instance->td);
113 free(instance);
114}
115
116
117static int ohci_register_ep(endpoint_t *ep)
118{
119 bus_t *bus_base = endpoint_get_bus(ep);
120 ohci_bus_t *bus = (ohci_bus_t *) bus_base;
121 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
122
123 const int err = usb2_bus_endpoint_register(&bus->helper, ep);
124 if (err)
125 return err;
126
127 ed_init(ohci_ep->ed, ep, ohci_ep->td);
128 hc_enqueue_endpoint(bus->hc, ep);
129 endpoint_set_online(ep, &bus->hc->guard);
130
131 return EOK;
132}
133
134static void ohci_unregister_ep(endpoint_t *ep)
135{
136 ohci_bus_t * const bus = (ohci_bus_t *) endpoint_get_bus(ep);
137 hc_t * const hc = bus->hc;
138 assert(ep);
139
140 usb2_bus_endpoint_unregister(&bus->helper, ep);
141 hc_dequeue_endpoint(bus->hc, ep);
142
143 /*
144 * Now we can be sure the active transfer will not be completed,
145 * as it's out of the schedule, and HC acknowledged it.
146 */
147
148 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
149
150 fibril_mutex_lock(&hc->guard);
151 endpoint_set_offline_locked(ep);
152 list_remove(&ohci_ep->pending_link);
153 usb_transfer_batch_t * const batch = ep->active_batch;
154 endpoint_deactivate_locked(ep);
155 fibril_mutex_unlock(&hc->guard);
156
157 if (batch) {
158 batch->error = EINTR;
159 batch->transferred_size = 0;
160 usb_transfer_batch_finish(batch);
161 }
162}
163
164static usb_transfer_batch_t *ohci_create_batch(endpoint_t *ep)
165{
166 ohci_transfer_batch_t *batch = ohci_transfer_batch_create(ep);
167 return &batch->base;
168}
169
170static void ohci_destroy_batch(usb_transfer_batch_t *batch)
171{
172 ohci_transfer_batch_destroy(ohci_transfer_batch_get(batch));
173}
174
175static const bus_ops_t ohci_bus_ops = {
176 .interrupt = ohci_hc_interrupt,
177 .status = ohci_hc_status,
178
179 .device_enumerate = ohci_device_enumerate,
180 .device_gone = ohci_device_gone,
181
182 .endpoint_destroy = ohci_endpoint_destroy,
183 .endpoint_create = ohci_endpoint_create,
184 .endpoint_register = ohci_register_ep,
185 .endpoint_unregister = ohci_unregister_ep,
186
187 .batch_create = ohci_create_batch,
188 .batch_destroy = ohci_destroy_batch,
189 .batch_schedule = ohci_hc_schedule,
190};
191
192
193int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
194{
195 assert(hc);
196 assert(bus);
197
198 bus_t *bus_base = (bus_t *) bus;
199 bus_init(bus_base, sizeof(device_t));
200 bus_base->ops = &ohci_bus_ops;
201
202 usb2_bus_helper_init(&bus->helper, &bandwidth_accounting_usb11);
203
204 bus->hc = hc;
205
206 return EOK;
207}
208
209/**
210 * @}
211 */
Note: See TracBrowser for help on using the repository browser.