source: mainline/uspace/drv/vhc/connhost.c@ 55f690c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 55f690c was 8555112, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Address keeping in addrkeep.h only

Removed symbolic link, the hcd.h file makes no sense.

  • Property mode set to 100644
File size: 12.4 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
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 drvusbvhc
30 * @{
31 */
32/** @file
33 * @brief Connection handling of calls from host (implementation).
34 */
35#include <assert.h>
36#include <errno.h>
37#include <usb/usb.h>
38#include <usb/addrkeep.h>
39
40#include "vhcd.h"
41#include "conn.h"
42#include "hc.h"
43
44
45typedef struct {
46 usb_direction_t direction;
47 usbhc_iface_transfer_out_callback_t out_callback;
48 usbhc_iface_transfer_in_callback_t in_callback;
49 device_t *dev;
50 size_t reported_size;
51 void *arg;
52} transfer_info_t;
53
54typedef struct {
55 usb_direction_t direction;
56 usb_target_t target;
57 usbhc_iface_transfer_out_callback_t out_callback;
58 usbhc_iface_transfer_in_callback_t in_callback;
59 device_t *dev;
60 void *arg;
61 void *data_buffer;
62 size_t data_buffer_size;
63} control_transfer_info_t;
64
65static void universal_callback(void *buffer, size_t size,
66 usb_transaction_outcome_t outcome, void *arg)
67{
68 transfer_info_t *transfer = (transfer_info_t *) arg;
69
70 if (transfer->reported_size != (size_t) -1) {
71 size = transfer->reported_size;
72 }
73
74 switch (transfer->direction) {
75 case USB_DIRECTION_IN:
76 transfer->in_callback(transfer->dev,
77 outcome, size,
78 transfer->arg);
79 break;
80 case USB_DIRECTION_OUT:
81 transfer->out_callback(transfer->dev,
82 outcome,
83 transfer->arg);
84 break;
85 default:
86 assert(false && "unreachable");
87 break;
88 }
89
90 free(transfer);
91}
92
93static transfer_info_t *create_transfer_info(device_t *dev,
94 usb_direction_t direction, void *arg)
95{
96 transfer_info_t *transfer = malloc(sizeof(transfer_info_t));
97
98 transfer->direction = direction;
99 transfer->in_callback = NULL;
100 transfer->out_callback = NULL;
101 transfer->arg = arg;
102 transfer->dev = dev;
103 transfer->reported_size = (size_t) -1;
104
105 return transfer;
106}
107
108static void control_abort_prematurely(control_transfer_info_t *transfer,
109 size_t size, usb_transaction_outcome_t outcome)
110{
111 switch (transfer->direction) {
112 case USB_DIRECTION_IN:
113 transfer->in_callback(transfer->dev,
114 outcome, size,
115 transfer->arg);
116 break;
117 case USB_DIRECTION_OUT:
118 transfer->out_callback(transfer->dev,
119 outcome,
120 transfer->arg);
121 break;
122 default:
123 assert(false && "unreachable");
124 break;
125 }
126}
127
128static void control_callback_two(void *buffer, size_t size,
129 usb_transaction_outcome_t outcome, void *arg)
130{
131 control_transfer_info_t *ctrl_transfer = (control_transfer_info_t *) arg;
132
133 if (outcome != USB_OUTCOME_OK) {
134 control_abort_prematurely(ctrl_transfer, outcome, size);
135 free(ctrl_transfer);
136 return;
137 }
138
139 transfer_info_t *transfer = create_transfer_info(ctrl_transfer->dev,
140 ctrl_transfer->direction, ctrl_transfer->arg);
141 transfer->out_callback = ctrl_transfer->out_callback;
142 transfer->in_callback = ctrl_transfer->in_callback;
143 transfer->reported_size = size;
144
145 switch (ctrl_transfer->direction) {
146 case USB_DIRECTION_IN:
147 hc_add_transaction_to_device(false, ctrl_transfer->target,
148 USB_TRANSFER_CONTROL,
149 NULL, 0,
150 universal_callback, transfer);
151 break;
152 case USB_DIRECTION_OUT:
153 hc_add_transaction_from_device(ctrl_transfer->target,
154 USB_TRANSFER_CONTROL,
155 NULL, 0,
156 universal_callback, transfer);
157 break;
158 default:
159 assert(false && "unreachable");
160 break;
161 }
162
163 free(ctrl_transfer);
164}
165
166static void control_callback_one(void *buffer, size_t size,
167 usb_transaction_outcome_t outcome, void *arg)
168{
169 control_transfer_info_t *transfer = (control_transfer_info_t *) arg;
170
171 if (outcome != USB_OUTCOME_OK) {
172 control_abort_prematurely(transfer, outcome, size);
173 free(transfer);
174 return;
175 }
176
177 switch (transfer->direction) {
178 case USB_DIRECTION_IN:
179 hc_add_transaction_from_device(transfer->target,
180 USB_TRANSFER_CONTROL,
181 transfer->data_buffer, transfer->data_buffer_size,
182 control_callback_two, transfer);
183 break;
184 case USB_DIRECTION_OUT:
185 hc_add_transaction_to_device(false, transfer->target,
186 USB_TRANSFER_CONTROL,
187 transfer->data_buffer, transfer->data_buffer_size,
188 control_callback_two, transfer);
189 break;
190 default:
191 assert(false && "unreachable");
192 break;
193 }
194}
195
196static control_transfer_info_t *create_control_transfer_info(device_t *dev,
197 usb_direction_t direction, usb_target_t target,
198 void *data_buffer, size_t data_buffer_size,
199 void *arg)
200{
201 control_transfer_info_t *transfer
202 = malloc(sizeof(control_transfer_info_t));
203
204 transfer->direction = direction;
205 transfer->target = target;
206 transfer->in_callback = NULL;
207 transfer->out_callback = NULL;
208 transfer->arg = arg;
209 transfer->dev = dev;
210 transfer->data_buffer = data_buffer;
211 transfer->data_buffer_size = data_buffer_size;
212
213 return transfer;
214}
215
216static int enqueue_transfer_out(device_t *dev,
217 usb_target_t target, usb_transfer_type_t transfer_type,
218 void *buffer, size_t size,
219 usbhc_iface_transfer_out_callback_t callback, void *arg)
220{
221 usb_log_debug2("Transfer OUT [%d.%d (%s); %zu].\n",
222 target.address, target.endpoint,
223 usb_str_transfer_type(transfer_type),
224 size);
225
226 transfer_info_t *transfer
227 = create_transfer_info(dev, USB_DIRECTION_OUT, arg);
228 transfer->out_callback = callback;
229
230 hc_add_transaction_to_device(false, target, transfer_type, buffer, size,
231 universal_callback, transfer);
232
233 return EOK;
234}
235
236static int enqueue_transfer_setup(device_t *dev,
237 usb_target_t target, usb_transfer_type_t transfer_type,
238 void *buffer, size_t size,
239 usbhc_iface_transfer_out_callback_t callback, void *arg)
240{
241 usb_log_debug2("Transfer SETUP [%d.%d (%s); %zu].\n",
242 target.address, target.endpoint,
243 usb_str_transfer_type(transfer_type),
244 size);
245
246 transfer_info_t *transfer
247 = create_transfer_info(dev, USB_DIRECTION_OUT, arg);
248 transfer->out_callback = callback;
249
250 hc_add_transaction_to_device(true, target, transfer_type, buffer, size,
251 universal_callback, transfer);
252
253 return EOK;
254}
255
256static int enqueue_transfer_in(device_t *dev,
257 usb_target_t target, usb_transfer_type_t transfer_type,
258 void *buffer, size_t size,
259 usbhc_iface_transfer_in_callback_t callback, void *arg)
260{
261 usb_log_debug2("Transfer IN [%d.%d (%s); %zu].\n",
262 target.address, target.endpoint,
263 usb_str_transfer_type(transfer_type),
264 size);
265
266 transfer_info_t *transfer
267 = create_transfer_info(dev, USB_DIRECTION_IN, arg);
268 transfer->in_callback = callback;
269
270 hc_add_transaction_from_device(target, transfer_type, buffer, size,
271 universal_callback, transfer);
272
273 return EOK;
274}
275
276
277static int interrupt_out(device_t *dev, usb_target_t target,
278 void *data, size_t size,
279 usbhc_iface_transfer_out_callback_t callback, void *arg)
280{
281 return enqueue_transfer_out(dev, target, USB_TRANSFER_INTERRUPT,
282 data, size,
283 callback, arg);
284}
285
286static int interrupt_in(device_t *dev, usb_target_t target,
287 void *data, size_t size,
288 usbhc_iface_transfer_in_callback_t callback, void *arg)
289{
290 return enqueue_transfer_in(dev, target, USB_TRANSFER_INTERRUPT,
291 data, size,
292 callback, arg);
293}
294
295static int control_write_setup(device_t *dev, usb_target_t target,
296 void *data, size_t size,
297 usbhc_iface_transfer_out_callback_t callback, void *arg)
298{
299 return enqueue_transfer_setup(dev, target, USB_TRANSFER_CONTROL,
300 data, size,
301 callback, arg);
302}
303
304static int control_write_data(device_t *dev, usb_target_t target,
305 void *data, size_t size,
306 usbhc_iface_transfer_out_callback_t callback, void *arg)
307{
308 return enqueue_transfer_out(dev, target, USB_TRANSFER_CONTROL,
309 data, size,
310 callback, arg);
311}
312
313static int control_write_status(device_t *dev, usb_target_t target,
314 usbhc_iface_transfer_in_callback_t callback, void *arg)
315{
316 return enqueue_transfer_in(dev, target, USB_TRANSFER_CONTROL,
317 NULL, 0,
318 callback, arg);
319}
320
321static int control_write(device_t *dev, usb_target_t target,
322 void *setup_packet, size_t setup_packet_size,
323 void *data, size_t data_size,
324 usbhc_iface_transfer_out_callback_t callback, void *arg)
325{
326 control_transfer_info_t *transfer
327 = create_control_transfer_info(dev, USB_DIRECTION_OUT, target,
328 data, data_size, arg);
329 transfer->out_callback = callback;
330
331 hc_add_transaction_to_device(true, target, USB_TRANSFER_CONTROL,
332 setup_packet, setup_packet_size,
333 control_callback_one, transfer);
334
335 return EOK;
336}
337
338static int control_read_setup(device_t *dev, usb_target_t target,
339 void *data, size_t size,
340 usbhc_iface_transfer_out_callback_t callback, void *arg)
341{
342 return enqueue_transfer_setup(dev, target, USB_TRANSFER_CONTROL,
343 data, size,
344 callback, arg);
345}
346
347static int control_read_data(device_t *dev, usb_target_t target,
348 void *data, size_t size,
349 usbhc_iface_transfer_in_callback_t callback, void *arg)
350{
351 return enqueue_transfer_in(dev, target, USB_TRANSFER_CONTROL,
352 data, size,
353 callback, arg);
354}
355
356static int control_read_status(device_t *dev, usb_target_t target,
357 usbhc_iface_transfer_out_callback_t callback, void *arg)
358{
359 return enqueue_transfer_out(dev, target, USB_TRANSFER_CONTROL,
360 NULL, 0,
361 callback, arg);
362}
363
364static int control_read(device_t *dev, usb_target_t target,
365 void *setup_packet, size_t setup_packet_size,
366 void *data, size_t data_size,
367 usbhc_iface_transfer_in_callback_t callback, void *arg)
368{
369 control_transfer_info_t *transfer
370 = create_control_transfer_info(dev, USB_DIRECTION_IN, target,
371 data, data_size, arg);
372 transfer->in_callback = callback;
373
374 hc_add_transaction_to_device(true, target, USB_TRANSFER_CONTROL,
375 setup_packet, setup_packet_size,
376 control_callback_one, transfer);
377
378 return EOK;
379}
380
381static usb_address_keeping_t addresses;
382
383
384static int reserve_default_address(device_t *dev)
385{
386 usb_address_keeping_reserve_default(&addresses);
387 return EOK;
388}
389
390static int release_default_address(device_t *dev)
391{
392 usb_address_keeping_release_default(&addresses);
393 return EOK;
394}
395
396static int request_address(device_t *dev, usb_address_t *address)
397{
398 usb_address_t addr = usb_address_keeping_request(&addresses);
399 if (addr < 0) {
400 return (int)addr;
401 }
402
403 *address = addr;
404 return EOK;
405}
406
407static int release_address(device_t *dev, usb_address_t address)
408{
409 return usb_address_keeping_release(&addresses, address);
410}
411
412static int bind_address(device_t *dev, usb_address_t address,
413 devman_handle_t handle)
414{
415 usb_address_keeping_devman_bind(&addresses, address, handle);
416 return EOK;
417}
418
419static int tell_address(device_t *dev, devman_handle_t handle,
420 usb_address_t *address)
421{
422 usb_address_t addr = usb_address_keeping_find(&addresses, handle);
423 if (addr < 0) {
424 return addr;
425 }
426
427 *address = addr;
428 return EOK;
429}
430
431void address_init(void)
432{
433 usb_address_keeping_init(&addresses, 50);
434}
435
436usbhc_iface_t vhc_iface = {
437 .tell_address = tell_address,
438
439 .reserve_default_address = reserve_default_address,
440 .release_default_address = release_default_address,
441 .request_address = request_address,
442 .bind_address = bind_address,
443 .release_address = release_address,
444
445 .interrupt_out = interrupt_out,
446 .interrupt_in = interrupt_in,
447
448 .control_write_setup = control_write_setup,
449 .control_write_data = control_write_data,
450 .control_write_status = control_write_status,
451
452 .control_write = control_write,
453
454 .control_read_setup = control_read_setup,
455 .control_read_data = control_read_data,
456 .control_read_status = control_read_status,
457
458 .control_read = control_read
459};
460
461/**
462 * @}
463 */
Note: See TracBrowser for help on using the repository browser.