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

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

WIP usbhost refactoring: ehci completed

vhc to go…

  • Property mode set to 100644
File size: 4.4 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 "hc.h"
44
45/** Callback to set toggle on ED.
46 *
47 * @param[in] hcd_ep hcd endpoint structure
48 * @param[in] toggle new value of toggle bit
49 */
50static void ehci_ep_toggle_set(endpoint_t *ep, bool toggle)
51{
52 ehci_endpoint_t *instance = ehci_endpoint_get(ep);
53 assert(instance);
54 assert(instance->qh);
55 ep->toggle = toggle;
56 if (qh_toggle_from_td(instance->qh))
57 usb_log_warning("EP(%p): Setting toggle bit for transfer "
58 "directed EP", instance);
59 qh_toggle_set(instance->qh, toggle);
60}
61
62/** Callback to get value of toggle bit.
63 *
64 * @param[in] hcd_ep hcd endpoint structure
65 * @return Current value of toggle bit.
66 */
67static bool ehci_ep_toggle_get(endpoint_t *ep)
68{
69 ehci_endpoint_t *instance = ehci_endpoint_get(ep);
70 assert(instance);
71 assert(instance->qh);
72
73 if (qh_toggle_from_td(instance->qh))
74 usb_log_warning("EP(%p): Reading useless toggle bit", instance);
75 return qh_toggle_get(instance->qh);
76}
77
78/** Creates new hcd endpoint representation.
79 */
80static endpoint_t *ehci_endpoint_create(bus_t *bus)
81{
82 assert(bus);
83
84 ehci_endpoint_t *ehci_ep = malloc(sizeof(ehci_endpoint_t));
85 if (ehci_ep == NULL)
86 return NULL;
87
88 endpoint_init(&ehci_ep->base, bus);
89
90 ehci_ep->qh = malloc32(sizeof(qh_t));
91 if (ehci_ep->qh == NULL) {
92 free(ehci_ep);
93 return NULL;
94 }
95
96 link_initialize(&ehci_ep->link);
97 return EOK;
98}
99
100/** Disposes hcd endpoint structure
101 *
102 * @param[in] hcd driver using this instance.
103 * @param[in] ep endpoint structure.
104 */
105static void ehci_endpoint_destroy(endpoint_t *ep)
106{
107 assert(ep);
108 ehci_endpoint_t *instance = ehci_endpoint_get(ep);
109
110 free32(instance->qh);
111 free(instance);
112}
113
114
115static int ehci_register_ep(bus_t *bus_base, endpoint_t *ep)
116{
117 ehci_bus_t *bus = (ehci_bus_t *) bus_base;
118 ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
119
120 const int err = bus->parent_ops.register_endpoint(bus_base, ep);
121 if (err)
122 return err;
123
124 qh_init(ehci_ep->qh, ep);
125 hc_enqueue_endpoint(bus->hc, ep);
126
127 return EOK;
128}
129
130static int ehci_release_ep(bus_t *bus_base, endpoint_t *ep)
131{
132 ehci_bus_t *bus = (ehci_bus_t *) bus_base;
133 assert(bus);
134 assert(ep);
135
136 const int err = bus->parent_ops.release_endpoint(bus_base, ep);
137 if (err)
138 return err;
139
140 hc_dequeue_endpoint(bus->hc, ep);
141 return EOK;
142
143}
144
145int ehci_bus_init(ehci_bus_t *bus, hc_t *hc)
146{
147 assert(hc);
148 assert(bus);
149
150 // FIXME: Implement the USB2 bw counting.
151 usb2_bus_init(&bus->base, BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
152
153 bus_ops_t *ops = &bus->base.base.ops;
154 bus->parent_ops = *ops;
155 ops->create_endpoint = ehci_endpoint_create;
156 ops->destroy_endpoint = ehci_endpoint_destroy;
157 ops->endpoint_set_toggle = ehci_ep_toggle_set;
158 ops->endpoint_get_toggle = ehci_ep_toggle_get;
159
160 ops->register_endpoint = ehci_register_ep;
161 ops->release_endpoint = ehci_release_ep;
162
163 return EOK;
164}
165
166/**
167 * @}
168 */
Note: See TracBrowser for help on using the repository browser.