conndev.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 
00036 #include <assert.h>
00037 #include <errno.h>
00038 #include <ddf/driver.h>
00039 #include <usbvirt/ipc.h>
00040 #include "conn.h"
00041 
00042 static fibril_local uintptr_t plugged_device_handle = 0;
00043 #define PLUGGED_DEVICE_NAME_MAXLEN 256
00044 static fibril_local char plugged_device_name[PLUGGED_DEVICE_NAME_MAXLEN + 1] = "<unknown>";
00045 
00052 static void receive_device_name(int phone)
00053 {
00054         aid_t opening_request = async_send_0(phone, IPC_M_USBVIRT_GET_NAME, NULL);
00055         if (opening_request == 0) {
00056                 return;
00057         }
00058 
00059 
00060         ipc_call_t data_request_call;
00061         aid_t data_request = async_data_read(phone,
00062              plugged_device_name, PLUGGED_DEVICE_NAME_MAXLEN,
00063              &data_request_call);
00064 
00065         if (data_request == 0) {
00066                 async_wait_for(opening_request, NULL);
00067                 return;
00068         }
00069 
00070         sysarg_t data_request_rc;
00071         sysarg_t opening_request_rc;
00072         async_wait_for(data_request, &data_request_rc);
00073         async_wait_for(opening_request, &opening_request_rc);
00074 
00075         if ((data_request_rc != EOK) || (opening_request_rc != EOK)) {
00076                 return;
00077         }
00078 
00079         size_t len = IPC_GET_ARG2(data_request_call);
00080         plugged_device_name[len] = 0;
00081 }
00082 
00089 void default_connection_handler(ddf_fun_t *fun,
00090     ipc_callid_t icallid, ipc_call_t *icall)
00091 {
00092         vhc_data_t *vhc = fun->dev->driver_data;
00093         sysarg_t method = IPC_GET_IMETHOD(*icall);
00094 
00095         if (method == IPC_M_CONNECT_TO_ME) {
00096                 int callback = IPC_GET_ARG5(*icall);
00097                 int rc = vhc_virtdev_plug(vhc, callback,
00098                     &plugged_device_handle);
00099                 if (rc != EOK) {
00100                         async_answer_0(icallid, rc);
00101                         async_hangup(callback);
00102                         return;
00103                 }
00104 
00105                 async_answer_0(icallid, EOK);
00106 
00107                 receive_device_name(callback);
00108 
00109                 usb_log_info("New virtual device `%s' (id: %" PRIxn ").\n",
00110                     plugged_device_name, plugged_device_handle);
00111 
00112                 return;
00113         }
00114 
00115         async_answer_0(icallid, EINVAL);
00116 }
00117 
00124 void on_client_close(ddf_fun_t *fun)
00125 {
00126         vhc_data_t *vhc = fun->dev->driver_data;
00127 
00128         if (plugged_device_handle != 0) {
00129                 usb_log_info("Virtual device `%s' disconnected (id: %" PRIxn ").\n",
00130                     plugged_device_name, plugged_device_handle);
00131                 vhc_virtdev_unplug(vhc, plugged_device_handle);
00132         }
00133 }
00134 
00135 

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