source: mainline/uspace/drv/uhci/callback.c@ 44d8853

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

Fixed: Do not crash on zero buffers

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#include <errno.h>
2#include <mem.h>
3
4#include "callback.h"
5int callback_init(callback_t *instance, device_t *dev,
6 void *buffer, size_t size, usbhc_iface_transfer_in_callback_t func_in,
7 usbhc_iface_transfer_out_callback_t func_out, void *arg)
8{
9 assert(instance);
10 assert(func_in == NULL || func_out == NULL);
11 if (size > 0) {
12 instance->new_buffer = malloc32(size);
13 if (!instance->new_buffer) {
14 uhci_print_error("Failed to allocate device acessible buffer.\n");
15 return ENOMEM;
16 }
17 if (func_out)
18 memcpy(instance->new_buffer, buffer, size);
19 } else {
20 instance->new_buffer = NULL;
21 }
22
23
24 instance->callback_out = func_out;
25 instance->callback_in = func_in;
26 instance->old_buffer = buffer;
27 instance->buffer_size = size;
28 instance->dev = dev;
29 instance->arg = arg;
30 return EOK;
31}
32/*----------------------------------------------------------------------------*/
33void callback_run(
34callback_t *instance, usb_transaction_outcome_t outcome, size_t act_size)
35{
36 assert(instance);
37
38 /* update the old buffer */
39 if (instance->new_buffer) {
40 memcpy(instance->new_buffer, instance->old_buffer, instance->buffer_size);
41 free32(instance->new_buffer);
42 instance->new_buffer = NULL;
43 }
44
45 if (instance->callback_in) {
46 assert(instance->callback_out == NULL);
47 uhci_print_verbose("Callback in: %p %x %d.\n",
48 instance->callback_in, outcome, act_size);
49 instance->callback_in(
50 instance->dev, act_size, outcome, instance->arg);
51 } else {
52 assert(instance->callback_out);
53 assert(instance->callback_in == NULL);
54 uhci_print_verbose("Callback out: %p %p %x %p .\n",
55 instance->callback_out, instance->dev, outcome, instance->arg);
56 instance->callback_out(
57 instance->dev, outcome, instance->arg);
58 }
59}
Note: See TracBrowser for help on using the repository browser.