source: mainline/uspace/drv/bus/usb/ehci/ehci_bus.c@ bd05140

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

libusbhost bus: endpoint→device is now managed by bus implementation

That allows xhci to better isolate responsibilities.

  • Property mode set to 100644
File size: 4.9 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 drvusbehci
30 * @{
31 */
32/** @file
33 * @brief EHCI 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#include <usb/debug.h>
41
42#include "ehci_bus.h"
43#include "ehci_batch.h"
44#include "hc.h"
45
46/** Callback to set toggle on ED.
47 *
48 * @param[in] hcd_ep hcd endpoint structure
49 * @param[in] toggle new value of toggle bit
50 */
51static void ehci_ep_toggle_set(endpoint_t *ep, bool toggle)
52{
53 ehci_endpoint_t *instance = ehci_endpoint_get(ep);
54 assert(instance);
55 assert(instance->qh);
56 ep->toggle = toggle;
57 if (qh_toggle_from_td(instance->qh))
58 usb_log_warning("EP(%p): Setting toggle bit for transfer "
59 "directed EP", instance);
60 qh_toggle_set(instance->qh, toggle);
61}
62
63/** Callback to get value of toggle bit.
64 *
65 * @param[in] hcd_ep hcd endpoint structure
66 * @return Current value of toggle bit.
67 */
68static bool ehci_ep_toggle_get(endpoint_t *ep)
69{
70 ehci_endpoint_t *instance = ehci_endpoint_get(ep);
71 assert(instance);
72 assert(instance->qh);
73
74 if (qh_toggle_from_td(instance->qh))
75 usb_log_warning("EP(%p): Reading useless toggle bit", instance);
76 return qh_toggle_get(instance->qh);
77}
78
79/** Creates new hcd endpoint representation.
80 */
81static endpoint_t *ehci_endpoint_create(bus_t *bus)
82{
83 assert(bus);
84
85 ehci_endpoint_t *ehci_ep = malloc(sizeof(ehci_endpoint_t));
86 if (ehci_ep == NULL)
87 return NULL;
88
89 endpoint_init(&ehci_ep->base, bus);
90
91 ehci_ep->qh = malloc32(sizeof(qh_t));
92 if (ehci_ep->qh == NULL) {
93 free(ehci_ep);
94 return NULL;
95 }
96
97 link_initialize(&ehci_ep->link);
98 return &ehci_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 ehci_endpoint_destroy(endpoint_t *ep)
107{
108 assert(ep);
109 ehci_endpoint_t *instance = ehci_endpoint_get(ep);
110
111 free32(instance->qh);
112 free(instance);
113}
114
115
116static int ehci_register_ep(bus_t *bus_base, device_t *dev, endpoint_t *ep, const usb_endpoint_desc_t *desc)
117{
118 ehci_bus_t *bus = (ehci_bus_t *) bus_base;
119 ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
120
121 // TODO utilize desc->usb2
122
123 const int err = bus->parent_ops.register_endpoint(bus_base, dev, ep, desc);
124 if (err)
125 return err;
126
127 qh_init(ehci_ep->qh, ep);
128 hc_enqueue_endpoint(bus->hc, ep);
129
130 return EOK;
131}
132
133static int ehci_unregister_ep(bus_t *bus_base, endpoint_t *ep)
134{
135 ehci_bus_t *bus = (ehci_bus_t *) bus_base;
136 assert(bus);
137 assert(ep);
138
139 const int err = bus->parent_ops.unregister_endpoint(bus_base, ep);
140 if (err)
141 return err;
142
143 hc_dequeue_endpoint(bus->hc, ep);
144 return EOK;
145}
146
147static usb_transfer_batch_t *ehci_bus_create_batch(bus_t *bus, endpoint_t *ep)
148{
149 ehci_transfer_batch_t *batch = ehci_transfer_batch_create(ep);
150 return &batch->base;
151}
152
153static void ehci_bus_destroy_batch(usb_transfer_batch_t *batch)
154{
155 ehci_transfer_batch_destroy(ehci_transfer_batch_get(batch));
156}
157
158int ehci_bus_init(ehci_bus_t *bus, hc_t *hc)
159{
160 assert(hc);
161 assert(bus);
162
163 // FIXME: Implement the USB2 bw counting.
164 usb2_bus_init(&bus->base, BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
165
166 bus_ops_t *ops = &bus->base.base.ops;
167 bus->parent_ops = *ops;
168 ops->create_endpoint = ehci_endpoint_create;
169 ops->destroy_endpoint = ehci_endpoint_destroy;
170 ops->endpoint_set_toggle = ehci_ep_toggle_set;
171 ops->endpoint_get_toggle = ehci_ep_toggle_get;
172
173 ops->register_endpoint = ehci_register_ep;
174 ops->unregister_endpoint = ehci_unregister_ep;
175
176 ops->create_batch = ehci_bus_create_batch;
177 ops->destroy_batch = ehci_bus_destroy_batch;
178
179 bus->hc = hc;
180
181 return EOK;
182}
183
184/**
185 * @}
186 */
Note: See TracBrowser for help on using the repository browser.