tkaratapanis commented on code in PR #16734: URL: https://github.com/apache/nuttx/pull/16734#discussion_r2219204335
########## drivers/misc/optee_supplicant.c: ########## @@ -0,0 +1,598 @@ +/**************************************************************************** + * drivers/misc/optee_supplicant.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/mutex.h> +#include <nuttx/semaphore.h> +#include <nuttx/kmalloc.h> +#include <nuttx/queue.h> +#include <nuttx/idr.h> +#include <string.h> +#include "optee.h" +#include "optee_supplicant.h" +#include "optee_msg.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* Request structure for RPCs serviced by the supplicant. */ + +struct optee_supplicant_req +{ + sq_entry_t link; + uint32_t func; + uint32_t ret; + size_t num_params; + FAR struct tee_ioctl_param *params; + sem_t c; +}; + +struct optee_supplicant +{ + mutex_t mutex; + int req_id; + struct sq_queue_s reqs; + FAR struct idr_s *idr; + FAR struct idr_s *shm_idr; + bool running; + sem_t reqs_c; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct optee_supplicant supp_s; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pop_entry + * + * Description: + * Pop the first request from the request queue, and create unique id. + * + * Parameters: + * num_params - Number of parameters passed. + * id - Pointer to the unique request id. + * + * Returned Value: + * A pointer to the request on success or NULL. + * + ****************************************************************************/ + +static FAR struct optee_supplicant_req * pop_entry(size_t num_params, + FAR int *id) +{ + FAR struct optee_supplicant_req *req; + + if (supp_s.req_id != -1) + { + /* Mixing sync/async not supported */ + + return NULL; + } + + if (sq_empty(&supp_s.reqs)) + { + return NULL; + } + + req = (struct optee_supplicant_req *)sq_remfirst(&supp_s.reqs); + + /* The request can't fit in the supplicant's supplied parameter buffer. */ + + if (num_params < req->num_params) + { + kmm_free(req); + return NULL; + } + + *id = idr_alloc(supp_s.idr, req, 0, INT32_MAX); + if (*id < 0) + { + kmm_free(req); + return NULL; + } + + return req; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: optee_supplicant_running + * + * Description: + * Returns true if the userspace supplicant is running. + * + * Parameters: + * None + * + * Returned Value: + * True or False. + * + ****************************************************************************/ + +bool optee_supplicant_running(void) +{ + return supp_s.running; +} + +/**************************************************************************** + * Name: optee_supplicant_init + * + * Description: + * Initialize supplicant data. + * + * Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void optee_supplicant_init(void) +{ + memset(&supp_s, 0, sizeof(supp_s)); + nxmutex_init(&supp_s.mutex); + nxsem_init(&supp_s.reqs_c, 0, 0); + sq_init(&supp_s.reqs); + supp_s.idr = idr_init(); + supp_s.shm_idr = idr_init(); + supp_s.req_id = -1; + supp_s.running = true; +} + +/**************************************************************************** + * Name: optee_supplicant_uninit + * + * Description: + * Uninitialize supplicant data. + * + * Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void optee_supplicant_uninit(void) +{ + supp_s.running = false; + nxmutex_destroy(&supp_s.mutex); + nxsem_destroy(&supp_s.reqs_c); + idr_destroy(supp_s.idr); +} + +/**************************************************************************** + * Name: optee_supplicant_request + * + * Description: + * Create a request with the received parameters from the OP-TEE, add it to + * the supplicant's request queue and then wait on the request's semaphore + * until it is serviced. + * + * Parameters: + * func - Requested function for the supplicant to perform + * params - Pointer pointer to parameter data. + * num_params - Number of parameters passed. + * + * Returned Value: + * TEE_SUCCESS on success or a global platform api error code on failure. + * + ****************************************************************************/ + +uint32_t optee_supplicant_request(uint32_t func, size_t num_params, + FAR struct tee_ioctl_param *params) +{ + FAR struct optee_supplicant_req *req; + uint32_t ret; + + if (!optee_supplicant_running()) + { + return TEE_ERROR_COMMUNICATION; + } + + req = (struct optee_supplicant_req *)kmm_zalloc(sizeof(*req)); Review Comment: I changed the code to be stack allocated, and skip the` kmm_free()` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org