hidiface.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 <dev_iface.h>
00036 #include <usbhid_iface.h>
00037 #include <usb/hid/iface.h>
00038 #include <errno.h>
00039 #include <str_error.h>
00040 #include <async.h>
00041 #include <assert.h>
00042 
00048 int usbhid_dev_get_event_length(int dev_phone, size_t *size)
00049 {
00050         if (dev_phone < 0) {
00051                 return EINVAL;
00052         }
00053 
00054         sysarg_t len;
00055         int rc = async_req_1_1(dev_phone, DEV_IFACE_ID(USBHID_DEV_IFACE),
00056             IPC_M_USBHID_GET_EVENT_LENGTH, &len);
00057         if (rc == EOK) {
00058                 if (size != NULL) {
00059                         *size = (size_t) len;
00060                 }
00061         }
00062         
00063         return rc;
00064 }
00065 
00078 int usbhid_dev_get_event(int dev_phone, uint8_t *buf, 
00079     size_t size, size_t *actual_size, int *event_nr, unsigned int flags)
00080 {
00081         if (dev_phone < 0) {
00082                 return EINVAL;
00083         }
00084         if ((buf == NULL)) {
00085                 return ENOMEM;
00086         }
00087         if (size == 0) {
00088                 return EINVAL;
00089         }
00090         
00091 //      if (size == 0) {
00092 //              return EOK;
00093 //      }
00094 
00095         size_t buffer_size =  size;
00096         uint8_t *buffer = malloc(buffer_size);
00097         if (buffer == NULL) {
00098                 return ENOMEM;
00099         }
00100 
00101         ipc_call_t opening_request_call;
00102         aid_t opening_request = async_send_2(dev_phone,
00103             DEV_IFACE_ID(USBHID_DEV_IFACE), IPC_M_USBHID_GET_EVENT,
00104             flags, &opening_request_call);
00105         if (opening_request == 0) {
00106                 free(buffer);
00107                 return ENOMEM;
00108         }
00109 
00110         ipc_call_t data_request_call;
00111         aid_t data_request = async_data_read(dev_phone, buffer, buffer_size,
00112             &data_request_call);
00113         if (data_request == 0) {
00114                 async_wait_for(opening_request, NULL);
00115                 free(buffer);
00116                 return ENOMEM;
00117         }
00118 
00119         sysarg_t data_request_rc;
00120         sysarg_t opening_request_rc;
00121         async_wait_for(data_request, &data_request_rc);
00122         async_wait_for(opening_request, &opening_request_rc);
00123 
00124         if (data_request_rc != EOK) {
00125                 /* Prefer return code of the opening request. */
00126                 if (opening_request_rc != EOK) {
00127                         return (int) opening_request_rc;
00128                 } else {
00129                         return (int) data_request_rc;
00130                 }
00131         }
00132 
00133         if (opening_request_rc != EOK) {
00134                 return (int) opening_request_rc;
00135         }
00136 
00137         size_t act_size = IPC_GET_ARG2(data_request_call);
00138 
00139         /* Copy the individual items. */
00140         memcpy(buf, buffer, act_size);
00141 //      memcpy(usages, buffer + items, items * sizeof(int32_t));
00142 
00143         if (actual_size != NULL) {
00144                 *actual_size = act_size;
00145         }
00146         
00147         if (event_nr != NULL) {
00148                 *event_nr = IPC_GET_ARG1(opening_request_call);
00149         }
00150 
00151         return EOK;
00152 }
00153 
00154 
00155 int usbhid_dev_get_report_descriptor_length(int dev_phone, size_t *size)
00156 {
00157         if (dev_phone < 0) {
00158                 return EINVAL;
00159         }
00160 
00161         sysarg_t arg_size;
00162         int rc = async_req_1_1(dev_phone, DEV_IFACE_ID(USBHID_DEV_IFACE),
00163             IPC_M_USBHID_GET_REPORT_DESCRIPTOR_LENGTH, &arg_size);
00164         if (rc == EOK) {
00165                 if (size != NULL) {
00166                         *size = (size_t) arg_size;
00167                 }
00168         }
00169         return rc;
00170 }
00171 
00172 int usbhid_dev_get_report_descriptor(int dev_phone, uint8_t *buf, size_t size, 
00173     size_t *actual_size)
00174 {
00175         if (dev_phone < 0) {
00176                 return EINVAL;
00177         }
00178         if ((buf == NULL)) {
00179                 return ENOMEM;
00180         }
00181         if (size == 0) {
00182                 return EINVAL;
00183         }
00184 
00185         aid_t opening_request = async_send_1(dev_phone,
00186             DEV_IFACE_ID(USBHID_DEV_IFACE), IPC_M_USBHID_GET_REPORT_DESCRIPTOR,
00187             NULL);
00188         if (opening_request == 0) {
00189                 return ENOMEM;
00190         }
00191 
00192         ipc_call_t data_request_call;
00193         aid_t data_request = async_data_read(dev_phone, buf, size,
00194             &data_request_call);
00195         if (data_request == 0) {
00196                 async_wait_for(opening_request, NULL);
00197                 return ENOMEM;
00198         }
00199 
00200         sysarg_t data_request_rc;
00201         sysarg_t opening_request_rc;
00202         async_wait_for(data_request, &data_request_rc);
00203         async_wait_for(opening_request, &opening_request_rc);
00204 
00205         if (data_request_rc != EOK) {
00206                 /* Prefer return code of the opening request. */
00207                 if (opening_request_rc != EOK) {
00208                         return (int) opening_request_rc;
00209                 } else {
00210                         return (int) data_request_rc;
00211                 }
00212         }
00213 
00214         if (opening_request_rc != EOK) {
00215                 return (int) opening_request_rc;
00216         }
00217 
00218         size_t act_size = IPC_GET_ARG2(data_request_call);
00219 
00220         if (actual_size != NULL) {
00221                 *actual_size = act_size;
00222         }
00223 
00224         return EOK;
00225 }
00226 

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