00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00035 #include <usbvirt/device.h>
00036 #include <usb/debug.h>
00037 #include <errno.h>
00038 #include <assert.h>
00039 #include "private.h"
00040
00052 static int usbvirt_control_transfer(usbvirt_device_t *dev,
00053 void *setup, size_t setup_size,
00054 void *data, size_t data_size, size_t *data_size_sent)
00055 {
00056 assert(dev);
00057 assert(dev->ops);
00058
00059 if (setup_size != sizeof(usb_device_request_setup_packet_t)) {
00060 return ESTALL;
00061 }
00062 usb_device_request_setup_packet_t *setup_packet = setup;
00063 if (data_size != setup_packet->length) {
00064 return ESTALL;
00065 }
00066
00067 int rc;
00068
00069
00070 rc = process_control_transfer(dev, dev->ops->control,
00071 setup_packet, data, data_size_sent);
00072 if (rc != EFORWARD) {
00073 return rc;
00074 }
00075
00076
00077 rc = process_control_transfer(dev, library_handlers,
00078 setup_packet, data, data_size_sent);
00079
00080 if (rc == EFORWARD) {
00081 usb_log_warning("Control transfer {%s (%s)} not handled.\n",
00082 usb_debug_str_buffer(setup, setup_size, 10),
00083 setup_packet->request_type & 0x80
00084 ? "IN" : usb_debug_str_buffer(data, data_size, 10));
00085 rc = EBADCHECKSUM;
00086 }
00087
00088 return rc;
00089 }
00090
00102 int usbvirt_control_write(usbvirt_device_t *dev, void *setup, size_t setup_size,
00103 void *data, size_t data_size)
00104 {
00105 return usbvirt_control_transfer(dev, setup, setup_size,
00106 data, data_size, NULL);
00107 }
00108
00121 int usbvirt_control_read(usbvirt_device_t *dev, void *setup, size_t setup_size,
00122 void *data, size_t data_size, size_t *data_size_sent)
00123 {
00124 return usbvirt_control_transfer(dev, setup, setup_size,
00125 data, data_size, data_size_sent);
00126 }
00127
00137 int usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
00138 usb_endpoint_t endpoint, void *data, size_t data_size)
00139 {
00140 if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
00141 return ERANGE;
00142 }
00143 if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
00144 return ENOTSUP;
00145 }
00146
00147 int rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
00148 data, data_size);
00149
00150 return rc;
00151 }
00152
00163 int usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
00164 usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
00165 {
00166 if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
00167 return ERANGE;
00168 }
00169 if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
00170 return ENOTSUP;
00171 }
00172
00173 size_t data_size_sent_tmp;
00174 int rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
00175 data, data_size, &data_size_sent_tmp);
00176
00177 if (rc != EOK) {
00178 return rc;
00179 }
00180
00181 if (data_size_sent != NULL) {
00182 *data_size_sent = data_size_sent_tmp;
00183 }
00184
00185 return EOK;
00186 }
00187