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 "hc.h"
|
---|
43 |
|
---|
44 | /** Callback to set toggle on ED.
|
---|
45 | *
|
---|
46 | * @param[in] hcd_ep hcd endpoint structure
|
---|
47 | * @param[in] toggle new value of toggle bit
|
---|
48 | */
|
---|
49 | static void ohci_ep_toggle_set(endpoint_t *ep, bool toggle)
|
---|
50 | {
|
---|
51 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
52 | assert(instance);
|
---|
53 | assert(instance->ed);
|
---|
54 | ep->toggle = toggle;
|
---|
55 | ed_toggle_set(instance->ed, toggle);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** Callback to get value of toggle bit.
|
---|
59 | *
|
---|
60 | * @param[in] hcd_ep hcd endpoint structure
|
---|
61 | * @return Current value of toggle bit.
|
---|
62 | */
|
---|
63 | static bool ohci_ep_toggle_get(endpoint_t *ep)
|
---|
64 | {
|
---|
65 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
66 | assert(instance);
|
---|
67 | assert(instance->ed);
|
---|
68 | return ed_toggle_get(instance->ed);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Creates new hcd endpoint representation.
|
---|
72 | */
|
---|
73 | static endpoint_t *ohci_endpoint_create(bus_t *bus)
|
---|
74 | {
|
---|
75 | assert(bus);
|
---|
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, bus);
|
---|
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->link);
|
---|
97 | return &ohci_ep->base;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Disposes hcd endpoint structure
|
---|
101 | *
|
---|
102 | * @param[in] hcd driver using this instance.
|
---|
103 | * @param[in] ep endpoint structure.
|
---|
104 | */
|
---|
105 | static void ohci_endpoint_destroy(endpoint_t *ep)
|
---|
106 | {
|
---|
107 | assert(ep);
|
---|
108 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
109 |
|
---|
110 | free32(instance->ed);
|
---|
111 | free32(instance->td);
|
---|
112 | free(instance);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | static int ohci_register_ep(bus_t *bus_base, endpoint_t *ep)
|
---|
117 | {
|
---|
118 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
119 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
120 |
|
---|
121 | const int err = bus->parent_ops.register_endpoint(bus_base, ep);
|
---|
122 | if (err)
|
---|
123 | return err;
|
---|
124 |
|
---|
125 | ed_init(ohci_ep->ed, ep, ohci_ep->td);
|
---|
126 | hc_enqueue_endpoint(bus->hc, ep);
|
---|
127 |
|
---|
128 | return EOK;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int ohci_release_ep(bus_t *bus_base, endpoint_t *ep)
|
---|
132 | {
|
---|
133 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
134 | assert(bus);
|
---|
135 | assert(ep);
|
---|
136 |
|
---|
137 | const int err = bus->parent_ops.release_endpoint(bus_base, ep);
|
---|
138 | if (err)
|
---|
139 | return err;
|
---|
140 |
|
---|
141 | hc_dequeue_endpoint(bus->hc, ep);
|
---|
142 | return EOK;
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
|
---|
147 | {
|
---|
148 | assert(hc);
|
---|
149 | assert(bus);
|
---|
150 |
|
---|
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 = ohci_endpoint_create;
|
---|
156 | ops->destroy_endpoint = ohci_endpoint_destroy;
|
---|
157 | ops->endpoint_set_toggle = ohci_ep_toggle_set;
|
---|
158 | ops->endpoint_get_toggle = ohci_ep_toggle_get;
|
---|
159 |
|
---|
160 | ops->register_endpoint = ohci_register_ep;
|
---|
161 | ops->release_endpoint = ohci_release_ep;
|
---|
162 |
|
---|
163 | bus->hc = hc;
|
---|
164 |
|
---|
165 | return EOK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * @}
|
---|
170 | */
|
---|