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

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

ohci: implement transfer abort on endpoint unregister

  • 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/** Callback to reset toggle on ED.
46 *
47 * @param[in] hcd_ep hcd endpoint structure
48 * @param[in] toggle new value of toggle bit
49 */
50static void ohci_ep_toggle_reset(endpoint_t *ep)
51{
52 ohci_endpoint_t *instance = ohci_endpoint_get(ep);
53 assert(instance);
54 assert(instance->ed);
55 ep->toggle = 0;
56 ed_toggle_set(instance->ed, 0);
57}
58
59/** Creates new hcd endpoint representation.
60 */
61static endpoint_t *ohci_endpoint_create(device_t *dev, const usb_endpoint_descriptors_t *desc)
62{
63 assert(dev);
64
65 ohci_endpoint_t *ohci_ep = malloc(sizeof(ohci_endpoint_t));
66 if (ohci_ep == NULL)
67 return NULL;
68
69 endpoint_init(&ohci_ep->base, dev, desc);
70
71 ohci_ep->ed = malloc32(sizeof(ed_t));
72 if (ohci_ep->ed == NULL) {
73 free(ohci_ep);
74 return NULL;
75 }
76
77 ohci_ep->td = malloc32(sizeof(td_t));
78 if (ohci_ep->td == NULL) {
79 free32(ohci_ep->ed);
80 free(ohci_ep);
81 return NULL;
82 }
83
84 link_initialize(&ohci_ep->eplist_link);
85 link_initialize(&ohci_ep->pending_link);
86 return &ohci_ep->base;
87}
88
89/** Disposes hcd endpoint structure
90 *
91 * @param[in] hcd driver using this instance.
92 * @param[in] ep endpoint structure.
93 */
94static void ohci_endpoint_destroy(endpoint_t *ep)
95{
96 assert(ep);
97 ohci_endpoint_t *instance = ohci_endpoint_get(ep);
98
99 free32(instance->ed);
100 free32(instance->td);
101 free(instance);
102}
103
104
105static int ohci_register_ep(endpoint_t *ep)
106{
107 bus_t *bus_base = endpoint_get_bus(ep);
108 ohci_bus_t *bus = (ohci_bus_t *) bus_base;
109 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
110
111 const int err = usb2_bus_ops.endpoint_register(ep);
112 if (err)
113 return err;
114
115 ed_init(ohci_ep->ed, ep, ohci_ep->td);
116 hc_enqueue_endpoint(bus->hc, ep);
117
118 return EOK;
119}
120
121static void ohci_unregister_ep(endpoint_t *ep)
122{
123 ohci_bus_t * const bus = (ohci_bus_t *) endpoint_get_bus(ep);
124 hc_t * const hc = bus->hc;
125 assert(ep);
126
127 usb2_bus_ops.endpoint_unregister(ep);
128 hc_dequeue_endpoint(bus->hc, ep);
129
130 ohci_endpoint_t * const ohci_ep = ohci_endpoint_get(ep);
131
132 /*
133 * Now we can be sure the active transfer will not be completed. But first,
134 * make sure that the handling fibril won't use its link in pending list.
135 */
136 fibril_mutex_lock(&hc->guard);
137 if (link_in_use(&ohci_ep->pending_link))
138 /* pending list reference */
139 endpoint_del_ref(ep);
140 list_remove(&ohci_ep->pending_link);
141 fibril_mutex_unlock(&hc->guard);
142
143 /*
144 * Finally, the endpoint shall not be used anywhere else. Finish the
145 * pending batch.
146 */
147 fibril_mutex_lock(&ep->guard);
148 usb_transfer_batch_t * const batch = ep->active_batch;
149 endpoint_deactivate_locked(ep);
150 fibril_mutex_unlock(&ep->guard);
151
152 if (batch) {
153 batch->error = EINTR;
154 batch->transfered_size = 0;
155 usb_transfer_batch_finish(batch);
156 }
157}
158
159static usb_transfer_batch_t *ohci_create_batch(endpoint_t *ep)
160{
161 ohci_transfer_batch_t *batch = ohci_transfer_batch_create(ep);
162 return &batch->base;
163}
164
165static void ohci_destroy_batch(usb_transfer_batch_t *batch)
166{
167 ohci_transfer_batch_destroy(ohci_transfer_batch_get(batch));
168}
169
170static const bus_ops_t ohci_bus_ops = {
171 .parent = &usb2_bus_ops,
172
173 .interrupt = ohci_hc_interrupt,
174 .status = ohci_hc_status,
175
176 .endpoint_destroy = ohci_endpoint_destroy,
177 .endpoint_create = ohci_endpoint_create,
178 .endpoint_register = ohci_register_ep,
179 .endpoint_unregister = ohci_unregister_ep,
180 .endpoint_count_bw = bandwidth_count_usb11,
181 .endpoint_toggle_reset = ohci_ep_toggle_reset,
182 .batch_create = ohci_create_batch,
183 .batch_destroy = ohci_destroy_batch,
184 .batch_schedule = ohci_hc_schedule,
185};
186
187
188int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
189{
190 assert(hc);
191 assert(bus);
192
193 usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
194 bus_t *bus_base = (bus_t *) bus;
195
196 usb2_bus_init(usb2_bus, BANDWIDTH_AVAILABLE_USB11);
197 bus_base->ops = &ohci_bus_ops;
198
199 bus->hc = hc;
200
201 return EOK;
202}
203
204/**
205 * @}
206 */
Note: See TracBrowser for help on using the repository browser.