transfer.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         /* Run user handler first. */
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         /* Run the library handlers afterwards. */
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 

Generated on Thu Jun 2 07:45:45 2011 for HelenOS/USB by  doxygen 1.4.7