nyx_error_t nyx_device_get_report_rate(nyx_device_handle_t handle, nyx_report_rate_t *rate_out_ptr) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); nyx_get_report_rate_function_t f = LOOKUP_METHOD(d, NYX_GET_REPORT_RATE_MODULE_METHOD); if (f) { return f(d, rate_out_ptr); } else { return NYX_ERROR_NOT_IMPLEMENTED; } }
nyx_error_t nyx_device_set_property(nyx_device_handle_t handle, const char *property_name_in, void *property_value_in_ptr) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); nyx_set_property_function_t f = LOOKUP_METHOD(d, NYX_SET_PROPERTY_MODULE_METHOD); if (f) { return f(d, property_name_in, property_value_in_ptr); } else { return NYX_ERROR_NOT_IMPLEMENTED; } }
nyx_error_t nyx_device_release_event(nyx_device_handle_t handle, nyx_event_handle_t event_handle) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); nyx_release_event_function_t f = LOOKUP_METHOD(d, NYX_RELEASE_EVENT_MODULE_METHOD); if (f) { return f(d, (nyx_event_t *)event_handle); } else { return NYX_ERROR_NOT_IMPLEMENTED; } }
nyx_error_t nyx_device_get_event_source(nyx_device_handle_t handle, int32_t *source_out_ptr) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); nyx_get_event_source_function_t f = LOOKUP_METHOD(d, NYX_GET_EVENT_SOURCE_MODULE_METHOD); if (f) { return f(d, source_out_ptr); } else { return NYX_ERROR_NOT_IMPLEMENTED; } }
nyx_error_t nyx_device_get_operating_mode(nyx_device_handle_t handle, nyx_operating_mode_t *mode_out_ptr) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); nyx_get_operating_mode_function_t f = LOOKUP_METHOD(d, NYX_GET_OPERATING_MODE_MODULE_METHOD); if (f) { return f(d, mode_out_ptr); } else { return NYX_ERROR_NOT_IMPLEMENTED; } }
nyx_error_t nyx_haptics_cancel_all(nyx_device_handle_t handle) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); CHECK_DEVICE_TYPE(d, NYX_DEVICE_HAPTICS); nyx_haptics_cancel_all_function_t f_ptr = LOOKUP_METHOD(d, NYX_HAPTICS_CANCEL_ALL_MODULE_METHOD); if (f_ptr) { return f_ptr(d); } else { return NYX_ERROR_NOT_IMPLEMENTED; } }
nyx_error_t nyx_device_close(nyx_device_handle_t handle) { nyx_device_t* d = (nyx_device_t*)handle; CHECK_DEVICE(d); /* * we need to cache method hash table as it might be * used during the close, we will destroy it after * a call to close. */ GHashTable* method_hash_table = (GHashTable*)d->method_hash_table; char* name = d->name; char* desc = d->description; /* * Don't know if necessary but since we are deallocating * the device structure during the call to close method * let's first cache the pointer to close method. */ void* module_ptr = d->module_ptr; nyx_close_function_t close_ptr = d->close_ptr; nyx_error_t error = close_ptr(d); /* * let's destroy the method hash table. */ if (method_hash_table) g_hash_table_destroy (method_hash_table); if (name) free (name); if (desc) free (desc); /* * We are done so, we can close the module. */ dlclose(module_ptr); return error; }
//------------------------------------------------------------------------------ void MobiCoreDriverDaemon::processMapBulkBuf(Connection *connection) { MC_DRV_CMD_MAP_BULK_BUF_struct cmd; RECV_PAYLOAD_FROM_CLIENT(connection, &cmd); // Device required MobiCoreDevice *device = (MobiCoreDevice *) (connection->connectionData); CHECK_DEVICE(device, connection); if (!device->lockWsmL2(cmd.handle)) { LOG_E("Couldn't lock the buffer!"); writeResult(connection, MC_DRV_ERR_DAEMON_WSM_HANDLE_NOT_FOUND); return; } uint32_t secureVirtualAdr = (uint32_t)NULL; uint64_t pAddrL2 = device->findWsmL2(cmd.handle, connection->socketDescriptor); if (pAddrL2 == 0) { LOG_E("Failed to resolve WSM with handle %u", cmd.handle); writeResult(connection, MC_DRV_ERR_DAEMON_WSM_HANDLE_NOT_FOUND); return; } // Map bulk memory to secure world mcResult_t mcResult = device->mapBulk(connection, cmd.sessionId, cmd.handle, pAddrL2, cmd.offsetPayload, cmd.lenBulkMem, &secureVirtualAdr); if (mcResult != MC_DRV_OK) { writeResult(connection, mcResult); return; } mcDrvRspMapBulkMem_t rsp; rsp.header.responseId = MC_DRV_OK; rsp.payload.sessionId = cmd.sessionId; rsp.payload.secureVirtualAdr = secureVirtualAdr; connection->writeData(&rsp, sizeof(mcDrvRspMapBulkMem_t)); }
nyx_error_t nyx_device_close(nyx_device_handle_t handle) { nyx_device_t *d = (nyx_device_t *)handle; CHECK_DEVICE(d); /* * cleanup device local members after call to close because members * might be used during the close. * * need to setup copy from device because call to close will deallocate * device (d) structure (cant access device members through pointer d) */ nyx_device_t cache; memcpy(&cache, d, sizeof(cache)); nyx_error_t error = cache.close_ptr(d); /* * do rest of the cleanup using cache */ if (NULL != cache.method_hash_table) { g_hash_table_destroy((GHashTable *)cache.method_hash_table); } g_free(cache.name); g_free(cache.description); /* * We are done so, we can close the module. */ dlclose(cache.module_ptr); return error; }
//------------------------------------------------------------------------------ void MobiCoreDriverDaemon::processUnmapBulkBuf(Connection *connection) { MC_DRV_CMD_UNMAP_BULK_BUF_struct cmd; RECV_PAYLOAD_FROM_CLIENT(connection, &cmd) // Device required MobiCoreDevice *device = (MobiCoreDevice *) (connection->connectionData); CHECK_DEVICE(device, connection); // Unmap bulk memory from secure world uint32_t mcResult = device->unmapBulk(connection, cmd.sessionId, cmd.handle, cmd.secureVirtualAdr, cmd.lenBulkMem); if (mcResult != MC_DRV_OK) { LOG_V("MCP UNMAP returned code %d", mcResult); writeResult(connection, mcResult); return; } // TODO-2012-09-06-haenellu: Think about not ignoring the error case. device->unlockWsmL2(cmd.handle); writeResult(connection, MC_DRV_OK); }
//------------------------------------------------------------------------------ void MobiCoreDriverDaemon::processOpenTrustlet(Connection *connection) { MC_DRV_CMD_OPEN_TRUSTLET_struct cmdOpenTrustlet; RECV_PAYLOAD_FROM_CLIENT(connection, &cmdOpenTrustlet); // Device required MobiCoreDevice *device = (MobiCoreDevice *) (connection->connectionData); CHECK_DEVICE(device, connection); uint32_t total_len, rlen, len = cmdOpenTrustlet.trustlet_len; uint8_t *payload = (uint8_t *)malloc(len); uint8_t *p = payload; if (payload == NULL) { LOG_E("failed to allocate payload buffer"); writeResult(connection, MC_DRV_ERR_DAEMON_SOCKET); return; } total_len = 0; while (total_len < len) { rlen = connection->readData(p, len - total_len); if ((int32_t)rlen < 0) { LOG_E("reading from Client failed"); /* it is questionable, if writing to broken socket has any effect here. */ writeResult(connection, MC_DRV_ERR_DAEMON_SOCKET); free(payload); return; } total_len += rlen; p += rlen; } // Get service blob from registry regObject_t *regObj = mcRegistryMemGetServiceBlob(cmdOpenTrustlet.spid, (uint8_t *)payload, len); // Free the payload object no matter what free(payload); if (regObj == NULL) { writeResult(connection, MC_DRV_ERR_TRUSTLET_NOT_FOUND); return; } if (regObj->len == 0) { free(regObj); writeResult(connection, MC_DRV_ERR_TRUSTLET_NOT_FOUND); return; } LOG_I(" Sharing Service loaded at %p with Secure World", (addr_t)(regObj->value)); CWsm_ptr pWsm = device->registerWsmL2((addr_t)(regObj->value), regObj->len, 0); if (pWsm == NULL) { free(regObj); LOG_E("allocating WSM for Trustlet failed"); writeResult(connection, MC_DRV_ERR_DAEMON_KMOD_ERROR); return; } // Initialize information data of open session command loadDataOpenSession_t loadDataOpenSession; loadDataOpenSession.baseAddr = pWsm->physAddr; loadDataOpenSession.offs = ((uintptr_t) regObj->value) & 0xFFF; loadDataOpenSession.len = regObj->len; loadDataOpenSession.tlHeader = (mclfHeader_ptr) (regObj->value + regObj->tlStartOffset); mcDrvRspOpenSession_t rspOpenSession; mcResult_t ret = device->openSession( connection, &loadDataOpenSession, cmdOpenTrustlet.handle, cmdOpenTrustlet.len, cmdOpenTrustlet.tci, &rspOpenSession.payload); // Unregister physical memory from kernel module. LOG_I(" Service buffer was copied to Secure world and processed. Stop sharing of buffer."); // This will also destroy the WSM object. if (!device->unregisterWsmL2(pWsm)) { free(regObj); // TODO-2012-07-02-haenellu: Can this ever happen? And if so, we should assert(), also TL might still be running. writeResult(connection, MC_DRV_ERR_DAEMON_KMOD_ERROR); return; } // Free memory occupied by Trustlet data free(regObj); if (ret != MC_DRV_OK) { LOG_E("Service could not be loaded."); writeResult(connection, ret); } else { rspOpenSession.header.responseId = ret; connection->writeData( &rspOpenSession, sizeof(rspOpenSession)); } }
//------------------------------------------------------------------------------ void MobiCoreDriverDaemon::processOpenSession(Connection *connection, bool isGpUuid) { MC_DRV_CMD_OPEN_SESSION_struct cmdOpenSession; RECV_PAYLOAD_FROM_CLIENT(connection, &cmdOpenSession); // Device required MobiCoreDevice *device = (MobiCoreDevice *) (connection->connectionData); CHECK_DEVICE(device, connection); // Get service blob from registry regObject_t *regObj = mcRegistryGetServiceBlob(&cmdOpenSession.uuid, isGpUuid); if (NULL == regObj) { writeResult(connection, MC_DRV_ERR_TRUSTLET_NOT_FOUND); return; } if (regObj->len == 0) { free(regObj); writeResult(connection, MC_DRV_ERR_TRUSTLET_NOT_FOUND); return; } LOG_I(" Sharing Service loaded at %p with Secure World", (addr_t)(regObj->value)); CWsm_ptr pWsm = device->registerWsmL2((addr_t)(regObj->value), regObj->len, 0); if (pWsm == NULL) { // Free memory occupied by Trustlet data free(regObj); LOG_E("allocating WSM for Trustlet failed"); writeResult(connection, MC_DRV_ERR_DAEMON_KMOD_ERROR); return; } // Initialize information data of open session command loadDataOpenSession_t loadDataOpenSession; loadDataOpenSession.baseAddr = pWsm->physAddr; loadDataOpenSession.offs = ((uintptr_t) regObj->value) & 0xFFF; loadDataOpenSession.len = regObj->len; loadDataOpenSession.tlHeader = (mclfHeader_ptr) (regObj->value + regObj->tlStartOffset); mcDrvRspOpenSession_t rspOpenSession; mcResult_t ret = device->openSession( connection, &loadDataOpenSession, cmdOpenSession.handle, cmdOpenSession.len, cmdOpenSession.tci, &rspOpenSession.payload); // Unregister physical memory from kernel module. LOG_I(" Service buffer was copied to Secure world and processed. Stop sharing of buffer."); // This will also destroy the WSM object. if (!device->unregisterWsmL2(pWsm)) { // TODO-2012-07-02-haenellu: Can this ever happen? And if so, we should assert(), also TL might still be running. free(regObj); writeResult(connection, MC_DRV_ERR_DAEMON_KMOD_ERROR); return; } // Free memory occupied by Trustlet data free(regObj); if (ret != MC_DRV_OK) { LOG_E("Service could not be loaded."); writeResult(connection, ret); } else { rspOpenSession.header.responseId = ret; connection->writeData( &rspOpenSession, sizeof(rspOpenSession)); } }