pipes.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 <usb/usb.h>
00036 #include <usb/dev/pipes.h>
00037 #include <usb/debug.h>
00038 #include <usb/hc.h>
00039 #include <usbhc_iface.h>
00040 #include <usb_iface.h>
00041 #include <devman.h>
00042 #include <errno.h>
00043 #include <assert.h>
00044 #include "pipepriv.h"
00045 
00046 #define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
00047 
00054 static usb_address_t get_my_address(int phone, ddf_dev_t *dev)
00055 {
00056         sysarg_t address;
00057 
00058         /*
00059          * We are sending special value as a handle - zero - to get
00060          * handle of the parent function (that handle was used
00061          * when registering our device @p dev.
00062          */
00063         int rc = async_req_2_1(phone, DEV_IFACE_ID(USB_DEV_IFACE),
00064             IPC_M_USB_GET_ADDRESS,
00065             0, &address);
00066 
00067         if (rc != EOK) {
00068                 return rc;
00069         }
00070 
00071         return (usb_address_t) address;
00072 }
00073 
00079 int usb_device_get_assigned_interface(ddf_dev_t *device)
00080 {
00081         int parent_phone = devman_parent_device_connect(device->handle,
00082             IPC_FLAG_BLOCKING);
00083         if (parent_phone < 0) {
00084                 return -1;
00085         }
00086 
00087         sysarg_t iface_no;
00088         int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
00089             IPC_M_USB_GET_INTERFACE,
00090             device->handle, &iface_no);
00091 
00092         async_hangup(parent_phone);
00093 
00094         if (rc != EOK) {
00095                 return -1;
00096         }
00097 
00098         return (int) iface_no;
00099 }
00100 
00107 int usb_device_connection_initialize_from_device(
00108     usb_device_connection_t *connection, ddf_dev_t *dev)
00109 {
00110         assert(connection);
00111         assert(dev);
00112 
00113         int rc;
00114         devman_handle_t hc_handle;
00115         usb_address_t my_address;
00116 
00117         rc = usb_hc_find(dev->handle, &hc_handle);
00118         if (rc != EOK) {
00119                 return rc;
00120         }
00121 
00122         int parent_phone = devman_parent_device_connect(dev->handle,
00123             IPC_FLAG_BLOCKING);
00124         if (parent_phone < 0) {
00125                 return parent_phone;
00126         }
00127 
00128         /*
00129          * Asking for "my" address may require several attempts.
00130          * That is because following scenario may happen:
00131          *  - parent driver (i.e. driver of parent device) announces new device
00132          *    and devman launches current driver
00133          *  - parent driver is preempted and thus does not send address-handle
00134          *    binding to HC driver
00135          *  - this driver gets here and wants the binding
00136          *  - the HC does not know the binding yet and thus it answers ENOENT
00137          *  So, we need to wait for the HC to learn the binding.
00138          */
00139         do {
00140                 my_address = get_my_address(parent_phone, dev);
00141 
00142                 if (my_address == ENOENT) {
00143                         /* Be nice, let other fibrils run and try again. */
00144                         async_usleep(IPC_AGAIN_DELAY);
00145                 } else if (my_address < 0) {
00146                         /* Some other problem, no sense trying again. */
00147                         rc = my_address;
00148                         goto leave;
00149                 }
00150 
00151         } while (my_address < 0);
00152 
00153         rc = usb_device_connection_initialize(connection,
00154             hc_handle, my_address);
00155 
00156 leave:
00157         async_hangup(parent_phone);
00158         return rc;
00159 }
00160 
00169 int usb_device_connection_initialize(usb_device_connection_t *connection,
00170     devman_handle_t host_controller_handle, usb_address_t device_address)
00171 {
00172         assert(connection);
00173 
00174         if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
00175                 return EINVAL;
00176         }
00177 
00178         connection->hc_handle = host_controller_handle;
00179         connection->address = device_address;
00180 
00181         return EOK;
00182 }
00183 
00190 int usb_device_connection_initialize_on_default_address(
00191     usb_device_connection_t *dev_connection,
00192     usb_hc_connection_t *hc_connection)
00193 {
00194         assert(dev_connection);
00195 
00196         if (hc_connection == NULL) {
00197                 return EBADMEM;
00198         }
00199 
00200         return usb_device_connection_initialize(dev_connection,
00201             hc_connection->hc_handle, (usb_address_t) 0);
00202 }
00203 
00214 void usb_pipe_start_long_transfer(usb_pipe_t *pipe)
00215 {
00216         (void) pipe_add_ref(pipe, true);
00217 }
00218 
00225 void usb_pipe_end_long_transfer(usb_pipe_t *pipe)
00226 {
00227         pipe_drop_ref(pipe);
00228 }
00229 

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