source: mainline/uspace/lib/drv/generic/remote_usbhc.c@ 50a01a9

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

usb: Don't pass speed info when registering endpoint.

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[91db50ac]1/*
[9753220]2 * Copyright (c) 2010-2011 Vojtech Horky
[91db50ac]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 libdrv
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <errno.h>
[eb1a2f4]37#include <assert.h>
[91db50ac]38
[cb59f787]39#include "usbhc_iface.h"
[eb1a2f4]40#include "ddf/driver.h"
[91db50ac]41
[1b22bd4]42#define USB_MAX_PAYLOAD_SIZE 1020
43
[eb1a2f4]44static void remote_usbhc_request_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
45static void remote_usbhc_bind_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[eb2f7dd]46static void remote_usbhc_find_by_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[eb1a2f4]47static void remote_usbhc_release_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[b7d8fd9]48static void remote_usbhc_register_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
49static void remote_usbhc_unregister_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[bbce2c2]50static void remote_usbhc_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
51static void remote_usbhc_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[eb1a2f4]52//static void remote_usbhc(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[91db50ac]53
[6edd494]54/** Remote USB host controller interface operations. */
[ffe3fe1]55static remote_iface_func_ptr_t remote_usbhc_iface_ops[] = {
56 [IPC_M_USBHC_REQUEST_ADDRESS] = remote_usbhc_request_address,
[27736cf]57 [IPC_M_USBHC_RELEASE_ADDRESS] = remote_usbhc_release_address,
[ffe3fe1]58 [IPC_M_USBHC_BIND_ADDRESS] = remote_usbhc_bind_address,
59 [IPC_M_USBHC_GET_HANDLE_BY_ADDRESS] = remote_usbhc_find_by_address,
[6f04905]60
[1e647c7d]61 [IPC_M_USBHC_REGISTER_ENDPOINT] = remote_usbhc_register_endpoint,
62 [IPC_M_USBHC_UNREGISTER_ENDPOINT] = remote_usbhc_unregister_endpoint,
[0a46c41e]63
[bbce2c2]64 [IPC_M_USBHC_READ] = remote_usbhc_read,
65 [IPC_M_USBHC_WRITE] = remote_usbhc_write,
[91db50ac]66};
67
[6edd494]68/** Remote USB host controller interface structure.
[91db50ac]69 */
[cb59f787]70remote_iface_t remote_usbhc_iface = {
71 .method_count = sizeof(remote_usbhc_iface_ops) /
72 sizeof(remote_usbhc_iface_ops[0]),
73 .methods = remote_usbhc_iface_ops
[91db50ac]74};
75
[1b22bd4]76typedef struct {
77 ipc_callid_t caller;
[0a6fa9f]78 ipc_callid_t data_caller;
[1b22bd4]79 void *buffer;
80 size_t size;
81} async_transaction_t;
[91db50ac]82
[93f8da1]83static void async_transaction_destroy(async_transaction_t *trans)
84{
85 if (trans == NULL) {
86 return;
87 }
88 if (trans->buffer != NULL) {
89 free(trans->buffer);
90 }
91
92 free(trans);
93}
94
95static async_transaction_t *async_transaction_create(ipc_callid_t caller)
96{
97 async_transaction_t *trans = malloc(sizeof(async_transaction_t));
98 if (trans == NULL) {
99 return NULL;
100 }
101
102 trans->caller = caller;
[0a6fa9f]103 trans->data_caller = 0;
[93f8da1]104 trans->buffer = NULL;
105 trans->size = 0;
106
107 return trans;
108}
109
[eb1a2f4]110void remote_usbhc_request_address(ddf_fun_t *fun, void *iface,
[6f04905]111 ipc_callid_t callid, ipc_call_t *call)
112{
113 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
114
115 if (!usb_iface->request_address) {
[17aca1c]116 async_answer_0(callid, ENOTSUP);
[6f04905]117 return;
118 }
[ffe3fe1]119
[67f55e7b]120 usb_address_t address = DEV_IPC_GET_ARG1(*call);
121 const bool strict = DEV_IPC_GET_ARG2(*call);
122 const usb_speed_t speed = DEV_IPC_GET_ARG3(*call);
[6f04905]123
[67f55e7b]124 const int rc = usb_iface->request_address(fun, &address, strict, speed);
[6f04905]125 if (rc != EOK) {
[17aca1c]126 async_answer_0(callid, rc);
[6f04905]127 } else {
[17aca1c]128 async_answer_1(callid, EOK, (sysarg_t) address);
[6f04905]129 }
130}
131
[eb1a2f4]132void remote_usbhc_bind_address(ddf_fun_t *fun, void *iface,
[4689d40]133 ipc_callid_t callid, ipc_call_t *call)
134{
135 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
136
137 if (!usb_iface->bind_address) {
[17aca1c]138 async_answer_0(callid, ENOTSUP);
[4689d40]139 return;
140 }
141
[eac610e]142 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
143 devman_handle_t handle = (devman_handle_t) DEV_IPC_GET_ARG2(*call);
[4689d40]144
[eb1a2f4]145 int rc = usb_iface->bind_address(fun, address, handle);
[4689d40]146
[17aca1c]147 async_answer_0(callid, rc);
[4689d40]148}
149
[eb2f7dd]150void remote_usbhc_find_by_address(ddf_fun_t *fun, void *iface,
151 ipc_callid_t callid, ipc_call_t *call)
152{
153 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
154
155 if (!usb_iface->find_by_address) {
156 async_answer_0(callid, ENOTSUP);
157 return;
158 }
159
160 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
161 devman_handle_t handle;
162 int rc = usb_iface->find_by_address(fun, address, &handle);
163
164 if (rc == EOK) {
165 async_answer_1(callid, EOK, handle);
166 } else {
167 async_answer_0(callid, rc);
168 }
169}
170
[eb1a2f4]171void remote_usbhc_release_address(ddf_fun_t *fun, void *iface,
[6f04905]172 ipc_callid_t callid, ipc_call_t *call)
173{
174 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
175
176 if (!usb_iface->release_address) {
[17aca1c]177 async_answer_0(callid, ENOTSUP);
[6f04905]178 return;
179 }
180
[eac610e]181 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
[6f04905]182
[eb1a2f4]183 int rc = usb_iface->release_address(fun, address);
[6f04905]184
[17aca1c]185 async_answer_0(callid, rc);
[6f04905]186}
187
[1b22bd4]188
[eb1a2f4]189static void callback_out(ddf_fun_t *fun,
[daec5e04]190 int outcome, void *arg)
[1b22bd4]191{
192 async_transaction_t *trans = (async_transaction_t *)arg;
193
[17aca1c]194 async_answer_0(trans->caller, outcome);
[1b22bd4]195
[93f8da1]196 async_transaction_destroy(trans);
[1b22bd4]197}
198
[eb1a2f4]199static void callback_in(ddf_fun_t *fun,
[daec5e04]200 int outcome, size_t actual_size, void *arg)
[1b22bd4]201{
202 async_transaction_t *trans = (async_transaction_t *)arg;
203
[daec5e04]204 if (outcome != EOK) {
[17aca1c]205 async_answer_0(trans->caller, outcome);
[5842493]206 if (trans->data_caller) {
207 async_answer_0(trans->data_caller, EINTR);
208 }
[93f8da1]209 async_transaction_destroy(trans);
210 return;
211 }
[1b22bd4]212
213 trans->size = actual_size;
[0a6fa9f]214
215 if (trans->data_caller) {
216 async_data_read_finalize(trans->data_caller,
217 trans->buffer, actual_size);
218 }
219
[daec5e04]220 async_answer_0(trans->caller, EOK);
[1e64b250]221
222 async_transaction_destroy(trans);
[91db50ac]223}
224
[b7d8fd9]225void remote_usbhc_register_endpoint(ddf_fun_t *fun, void *iface,
226 ipc_callid_t callid, ipc_call_t *call)
227{
228 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
229
230 if (!usb_iface->register_endpoint) {
231 async_answer_0(callid, ENOTSUP);
232 return;
233 }
234
[1998bcd]235#define _INIT_FROM_HIGH_DATA2(type, var, arg_no) \
[27736cf]236 type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) >> 16)
[1998bcd]237#define _INIT_FROM_LOW_DATA2(type, var, arg_no) \
[27736cf]238 type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) & 0xffff)
[1998bcd]239
[365e29e2]240 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[1998bcd]241
[27736cf]242 _INIT_FROM_HIGH_DATA2(usb_transfer_type_t, transfer_type, 2);
243 _INIT_FROM_LOW_DATA2(usb_direction_t, direction, 2);
[1998bcd]244
245 _INIT_FROM_HIGH_DATA2(size_t, max_packet_size, 3);
246 _INIT_FROM_LOW_DATA2(unsigned int, interval, 3);
247
248#undef _INIT_FROM_HIGH_DATA2
249#undef _INIT_FROM_LOW_DATA2
250
[27736cf]251 int rc = usb_iface->register_endpoint(fun, target.address,
[bdd8ad2f]252 target.endpoint, transfer_type, direction, max_packet_size, interval);
[b7d8fd9]253
254 async_answer_0(callid, rc);
255}
256
257void remote_usbhc_unregister_endpoint(ddf_fun_t *fun, void *iface,
258 ipc_callid_t callid, ipc_call_t *call)
259{
260 usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
261
262 if (!usb_iface->unregister_endpoint) {
263 async_answer_0(callid, ENOTSUP);
264 return;
265 }
266
267 usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
268 usb_endpoint_t endpoint = (usb_endpoint_t) DEV_IPC_GET_ARG2(*call);
269 usb_direction_t direction = (usb_direction_t) DEV_IPC_GET_ARG3(*call);
270
271 int rc = usb_iface->unregister_endpoint(fun,
272 address, endpoint, direction);
273
274 async_answer_0(callid, rc);
275}
276
[bbce2c2]277void remote_usbhc_read(
[ffe3fe1]278 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
279{
280 assert(fun);
281 assert(iface);
282 assert(call);
283
284 const usbhc_iface_t *hc_iface = iface;
285
286 if (!hc_iface->read) {
287 async_answer_0(callid, ENOTSUP);
288 return;
289 }
290
[365e29e2]291 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[bdd8ad2f]292 const uint64_t setup =
293 ((uint64_t)DEV_IPC_GET_ARG2(*call)) |
294 (((uint64_t)DEV_IPC_GET_ARG3(*call)) << 32);
[ffe3fe1]295
296 async_transaction_t *trans = async_transaction_create(callid);
297 if (trans == NULL) {
298 async_answer_0(callid, ENOMEM);
299 return;
300 }
301
302 if (!async_data_read_receive(&trans->data_caller, &trans->size)) {
303 async_answer_0(callid, EPARTY);
304 return;
305 }
306
307 trans->buffer = malloc(trans->size);
308 if (trans->buffer == NULL) {
309 async_answer_0(trans->data_caller, ENOMEM);
310 async_answer_0(callid, ENOMEM);
311 async_transaction_destroy(trans);
312 }
313
314 const int rc = hc_iface->read(
[bdd8ad2f]315 fun, target, setup, trans->buffer, trans->size, callback_in, trans);
[ffe3fe1]316
317 if (rc != EOK) {
318 async_answer_0(trans->data_caller, rc);
319 async_answer_0(callid, rc);
320 async_transaction_destroy(trans);
321 }
322}
323
[bbce2c2]324void remote_usbhc_write(
[ffe3fe1]325 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
326{
327 assert(fun);
328 assert(iface);
329 assert(call);
330
331 const usbhc_iface_t *hc_iface = iface;
332
333 if (!hc_iface->write) {
334 async_answer_0(callid, ENOTSUP);
335 return;
336 }
337
[365e29e2]338 const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
[bdd8ad2f]339 const size_t data_buffer_len = DEV_IPC_GET_ARG2(*call);
340 const uint64_t setup =
341 ((uint64_t)DEV_IPC_GET_ARG3(*call)) |
342 (((uint64_t)DEV_IPC_GET_ARG4(*call)) << 32);
[ffe3fe1]343
344 async_transaction_t *trans = async_transaction_create(callid);
345 if (trans == NULL) {
346 async_answer_0(callid, ENOMEM);
347 return;
348 }
349
[bdd8ad2f]350 if (data_buffer_len > 0) {
351 int rc = async_data_write_accept(&trans->buffer, false,
352 1, USB_MAX_PAYLOAD_SIZE,
353 0, &trans->size);
[ffe3fe1]354
[bdd8ad2f]355 if (rc != EOK) {
356 async_answer_0(callid, rc);
357 async_transaction_destroy(trans);
358 return;
359 }
[ffe3fe1]360 }
361
[bdd8ad2f]362 int rc = hc_iface->write(
363 fun, target, setup, trans->buffer, trans->size, callback_out, trans);
[ffe3fe1]364
365 if (rc != EOK) {
366 async_answer_0(callid, rc);
367 async_transaction_destroy(trans);
368 }
369}
370
[1b22bd4]371
[91db50ac]372/**
373 * @}
374 */
Note: See TracBrowser for help on using the repository browser.