source: mainline/uspace/drv/uhci-hcd/iface.c@ 4708657

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4708657 was 563ead9, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Further cleanup, includes, const, …

  • Property mode set to 100644
File size: 13.2 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky, 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/** @addtogroup drvusbuhcihc
29 * @{
30 */
31/** @file
32 * @brief UHCI driver hc interface implementation
33 */
34#include <ddf/driver.h>
35#include <errno.h>
36
37#include <usb/debug.h>
38#include <usb/host/endpoint.h>
39
40#include "iface.h"
41#include "batch.h"
42#include "hc.h"
43
44static inline int setup_batch(
45 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
46 void *data, size_t size, void * setup_data, size_t setup_size,
47 usbhc_iface_transfer_in_callback_t in,
48 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name,
49 hc_t **hc, usb_transfer_batch_t **batch)
50{
51 assert(hc);
52 assert(batch);
53 assert(fun);
54 *hc = fun_to_hc(fun);
55 assert(*hc);
56
57 size_t res_bw;
58 endpoint_t *ep = usb_endpoint_manager_get_ep(&(*hc)->ep_manager,
59 target.address, target.endpoint, direction, &res_bw);
60 if (ep == NULL) {
61 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
62 target.address, target.endpoint, name);
63 return ENOENT;
64 }
65
66 usb_log_debug("%s %d:%d %zu(%zu).\n",
67 name, target.address, target.endpoint, size, ep->max_packet_size);
68
69 const size_t bw = bandwidth_count_usb11(
70 ep->speed, ep->transfer_type, size, ep->max_packet_size);
71 if (res_bw < bw) {
72 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
73 "but only %zu is reserved.\n",
74 target.address, target.endpoint, name, bw, res_bw);
75 return ENOSPC;
76 }
77
78 *batch = batch_get(
79 fun, ep, data, size, setup_data, setup_size, in, out, arg);
80 if (!*batch)
81 return ENOMEM;
82 return EOK;
83}
84/*----------------------------------------------------------------------------*/
85/** Request address interface function
86 *
87 * @param[in] fun DDF function that was called.
88 * @param[in] speed Speed to associate with the new default address.
89 * @param[out] address Place to write a new address.
90 * @return Error code.
91 */
92static int request_address(
93 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
94{
95 assert(fun);
96 hc_t *hc = fun_to_hc(fun);
97 assert(hc);
98 assert(address);
99
100 usb_log_debug("Address request with speed %d.\n", speed);
101 *address = device_keeper_get_free_address(&hc->manager, speed);
102 usb_log_debug("Address request with result: %d.\n", *address);
103 if (*address <= 0)
104 return *address;
105 return EOK;
106}
107/*----------------------------------------------------------------------------*/
108/** Bind address interface function
109 *
110 * @param[in] fun DDF function that was called.
111 * @param[in] address Address of the device
112 * @param[in] handle Devman handle of the device driver.
113 * @return Error code.
114 */
115static int bind_address(
116 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
117{
118 assert(fun);
119 hc_t *hc = fun_to_hc(fun);
120 assert(hc);
121 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
122 usb_device_keeper_bind(&hc->manager, address, handle);
123 return EOK;
124}
125/*----------------------------------------------------------------------------*/
126/** Find device handle by address interface function.
127 *
128 * @param[in] fun DDF function that was called.
129 * @param[in] address Address in question.
130 * @param[out] handle Where to store device handle if found.
131 * @return Error code.
132 */
133static int find_by_address(ddf_fun_t *fun, usb_address_t address,
134 devman_handle_t *handle)
135{
136 assert(fun);
137 hc_t *hc = fun_to_hc(fun);
138 assert(hc);
139 const bool found =
140 usb_device_keeper_find_by_address(&hc->manager, address, handle);
141 return found ? EOK : ENOENT;
142}
143/*----------------------------------------------------------------------------*/
144/** Release address interface function
145 *
146 * @param[in] fun DDF function that was called.
147 * @param[in] address USB address to be released.
148 * @return Error code.
149 */
150static int release_address(ddf_fun_t *fun, usb_address_t address)
151{
152 assert(fun);
153 hc_t *hc = fun_to_hc(fun);
154 assert(hc);
155 usb_log_debug("Address release %d.\n", address);
156 usb_device_keeper_release(&hc->manager, address);
157 return EOK;
158}
159/*----------------------------------------------------------------------------*/
160static int register_endpoint(
161 ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
162 usb_endpoint_t endpoint,
163 usb_transfer_type_t transfer_type, usb_direction_t direction,
164 size_t max_packet_size, unsigned int interval)
165{
166 assert(fun);
167 hc_t *hc = fun_to_hc(fun);
168 assert(hc);
169 const size_t size = max_packet_size;
170 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
171 if (speed >= USB_SPEED_MAX) {
172 speed = ep_speed;
173 }
174 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
175 address, endpoint, usb_str_transfer_type(transfer_type),
176 usb_str_speed(speed), direction, size, max_packet_size, interval);
177
178 return usb_endpoint_manager_add_ep(&hc->ep_manager, address, endpoint,
179 direction, transfer_type, speed, max_packet_size, size);
180}
181/*----------------------------------------------------------------------------*/
182static int unregister_endpoint(
183 ddf_fun_t *fun, usb_address_t address,
184 usb_endpoint_t endpoint, usb_direction_t direction)
185{
186 assert(fun);
187 hc_t *hc = fun_to_hc(fun);
188 assert(hc);
189 usb_log_debug("Unregister endpoint %d:%d %d.\n",
190 address, endpoint, direction);
191 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
192 endpoint, direction);
193}
194/*----------------------------------------------------------------------------*/
195/** Interrupt out transaction interface function
196 *
197 * @param[in] fun DDF function that was called.
198 * @param[in] target USB device to write to.
199 * @param[in] data Source of data.
200 * @param[in] size Size of data source.
201 * @param[in] callback Function to call on transaction completion
202 * @param[in] arg Additional for callback function.
203 * @return Error code.
204 */
205static int interrupt_out(
206 ddf_fun_t *fun, usb_target_t target, void *data,
207 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
208{
209 usb_transfer_batch_t *batch = NULL;
210 hc_t *hc = NULL;
211 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
212 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
213 if (ret != EOK)
214 return ret;
215 assert(batch);
216 assert(hc);
217 batch_interrupt_out(batch);
218 ret = hc_schedule(hc, batch);
219 if (ret != EOK) {
220 usb_transfer_batch_dispose(batch);
221 }
222 return ret;
223}
224/*----------------------------------------------------------------------------*/
225/** Interrupt in transaction interface function
226 *
227 * @param[in] fun DDF function that was called.
228 * @param[in] target USB device to write to.
229 * @param[out] data Data destination.
230 * @param[in] size Size of data source.
231 * @param[in] callback Function to call on transaction completion
232 * @param[in] arg Additional for callback function.
233 * @return Error code.
234 */
235static int interrupt_in(
236 ddf_fun_t *fun, usb_target_t target, void *data,
237 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
238{
239 usb_transfer_batch_t *batch = NULL;
240 hc_t *hc = NULL;
241 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
242 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
243 if (ret != EOK)
244 return ret;
245 assert(batch);
246 assert(hc);
247 batch_interrupt_in(batch);
248 ret = hc_schedule(hc, batch);
249 if (ret != EOK) {
250 usb_transfer_batch_dispose(batch);
251 }
252 return ret;
253}
254/*----------------------------------------------------------------------------*/
255/** Bulk out transaction interface function
256 *
257 * @param[in] fun DDF function that was called.
258 * @param[in] target USB device to write to.
259 * @param[in] data Source of data.
260 * @param[in] size Size of data source.
261 * @param[in] callback Function to call on transaction completion
262 * @param[in] arg Additional for callback function.
263 * @return Error code.
264 */
265static int bulk_out(
266 ddf_fun_t *fun, usb_target_t target, void *data,
267 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
268{
269 usb_transfer_batch_t *batch = NULL;
270 hc_t *hc = NULL;
271 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
272 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
273 if (ret != EOK)
274 return ret;
275 assert(batch);
276 assert(hc);
277 batch_bulk_out(batch);
278 ret = hc_schedule(hc, batch);
279 if (ret != EOK) {
280 usb_transfer_batch_dispose(batch);
281 }
282 return ret;
283}
284/*----------------------------------------------------------------------------*/
285/** Bulk in transaction interface function
286 *
287 * @param[in] fun DDF function that was called.
288 * @param[in] target USB device to write to.
289 * @param[out] data Data destination.
290 * @param[in] size Size of data source.
291 * @param[in] callback Function to call on transaction completion
292 * @param[in] arg Additional for callback function.
293 * @return Error code.
294 */
295static int bulk_in(
296 ddf_fun_t *fun, usb_target_t target, void *data,
297 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
298{
299 usb_transfer_batch_t *batch = NULL;
300 hc_t *hc = NULL;
301 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
302 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
303 if (ret != EOK)
304 return ret;
305 assert(batch);
306 assert(hc);
307 batch_bulk_in(batch);
308 ret = hc_schedule(hc, batch);
309 if (ret != EOK) {
310 usb_transfer_batch_dispose(batch);
311 }
312 return ret;
313}
314/*----------------------------------------------------------------------------*/
315/** Control write transaction interface function
316 *
317 * @param[in] fun DDF function that was called.
318 * @param[in] target USB device to write to.
319 * @param[in] setup_data Data to send with SETUP transfer.
320 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
321 * @param[in] data Source of data.
322 * @param[in] size Size of data source.
323 * @param[in] callback Function to call on transaction completion.
324 * @param[in] arg Additional for callback function.
325 * @return Error code.
326 */
327static int control_write(
328 ddf_fun_t *fun, usb_target_t target,
329 void *setup_data, size_t setup_size, void *data, size_t size,
330 usbhc_iface_transfer_out_callback_t callback, void *arg)
331{
332 usb_transfer_batch_t *batch = NULL;
333 hc_t *hc = NULL;
334 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
335 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
336 &hc, &batch);
337 if (ret != EOK)
338 return ret;
339 assert(batch);
340 assert(hc);
341 usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
342 batch_control_write(batch);
343 ret = hc_schedule(hc, batch);
344 if (ret != EOK) {
345 usb_transfer_batch_dispose(batch);
346 }
347 return ret;
348}
349/*----------------------------------------------------------------------------*/
350/** Control read transaction interface function
351 *
352 * @param[in] fun DDF function that was called.
353 * @param[in] target USB device to write to.
354 * @param[in] setup_data Data to send with SETUP packet.
355 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
356 * @param[out] data Source of data.
357 * @param[in] size Size of data source.
358 * @param[in] callback Function to call on transaction completion.
359 * @param[in] arg Additional for callback function.
360 * @return Error code.
361 */
362static int control_read(
363 ddf_fun_t *fun, usb_target_t target,
364 void *setup_data, size_t setup_size, void *data, size_t size,
365 usbhc_iface_transfer_in_callback_t callback, void *arg)
366{
367 usb_transfer_batch_t *batch = NULL;
368 hc_t *hc = NULL;
369 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
370 setup_data, setup_size, callback, NULL, arg, "Control READ",
371 &hc, &batch);
372 if (ret != EOK)
373 return ret;
374 assert(batch);
375 assert(hc);
376 batch_control_read(batch);
377 ret = hc_schedule(hc, batch);
378 if (ret != EOK) {
379 usb_transfer_batch_dispose(batch);
380 }
381 return ret;
382}
383/*----------------------------------------------------------------------------*/
384usbhc_iface_t hc_iface = {
385 .request_address = request_address,
386 .bind_address = bind_address,
387 .find_by_address = find_by_address,
388 .release_address = release_address,
389
390 .register_endpoint = register_endpoint,
391 .unregister_endpoint = unregister_endpoint,
392
393 .interrupt_out = interrupt_out,
394 .interrupt_in = interrupt_in,
395
396 .bulk_out = bulk_out,
397 .bulk_in = bulk_in,
398
399 .control_write = control_write,
400 .control_read = control_read,
401};
402/**
403 * @}
404 */
Note: See TracBrowser for help on using the repository browser.