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

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

Implement hc bandwidth reservation

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