tkaratapanis commented on code in PR #16734:
URL: https://github.com/apache/nuttx/pull/16734#discussion_r2217183616


##########
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);

Review Comment:
   `optee_close()` does that, through `idr_destroy(priv->shms);`.
   In some RPCs we need to get a pointer to the supplicant's idr, this is why 
we need
   to have also a static reference to that idr and get it through 
`optee_supplicant_get_shm_idr();`.
   
   During `optee_open()` priv->shms, points to the same tree:
   
   ```
   #ifdef CONFIG_DEV_OPTEE_SUPPLICANT
     else if (role == OPTEE_ROLE_SUPPLICANT)
       {
         /* Allow only one process to open the device. */
   
         if (filep->f_inode->i_crefs > 2)
           {
             return -EBUSY;
           }
   
         optee_supplicant_init();
         priv->shms = optee_supplicant_get_shm_idr();
       }
   #endif
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to