pkarashchenko commented on code in PR #7762: URL: https://github.com/apache/nuttx/pull/7762#discussion_r1041597564
########## arch/xtensa/src/esp32/esp32_himem_chardev.c: ########## @@ -0,0 +1,409 @@ +/**************************************************************************** + * arch/xtensa/src/esp32/esp32_himem_chardev.c + * + * 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 <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <debug.h> +#include <sys/types.h> +#include <nuttx/list.h> +#include <nuttx/himem/himem.h> +#include <nuttx/semaphore.h> +#include <nuttx/fs/fs.h> +#include <nuttx/drivers/himem_chardev.h> +#include "esp32_himem.h" + +#define HIMEM_UNMAPPED (-1) Review Comment: ```suggestion /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ #define HIMEM_UNMAPPED (-1) ``` ########## arch/xtensa/src/esp32/esp32_himem_chardev.c: ########## @@ -0,0 +1,409 @@ +/**************************************************************************** + * arch/xtensa/src/esp32/esp32_himem_chardev.c + * + * 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 <stdlib.h> Review Comment: ```suggestion #include <stdlib.h> ``` ########## arch/xtensa/src/esp32/esp32_himem_chardev.c: ########## @@ -0,0 +1,409 @@ +/**************************************************************************** + * arch/xtensa/src/esp32/esp32_himem_chardev.c + * + * 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 <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <debug.h> +#include <sys/types.h> +#include <nuttx/list.h> +#include <nuttx/himem/himem.h> +#include <nuttx/semaphore.h> +#include <nuttx/fs/fs.h> +#include <nuttx/drivers/himem_chardev.h> +#include "esp32_himem.h" + +#define HIMEM_UNMAPPED (-1) + +struct himem_chardev +{ + struct list_node node; + size_t size; + esp_himem_handle_t mem_handle; + char name[32]; +}; + +struct himem_chardev_priv +{ + off_t offset; /* himem offset[byte]. update by seek(), read(), write(). */ Review Comment: 2 spaces indentation ########## arch/xtensa/src/esp32/esp32_himem_chardev.c: ########## @@ -0,0 +1,409 @@ +/**************************************************************************** + * arch/xtensa/src/esp32/esp32_himem_chardev.c + * + * 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 <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <debug.h> +#include <sys/types.h> +#include <nuttx/list.h> +#include <nuttx/himem/himem.h> +#include <nuttx/semaphore.h> +#include <nuttx/fs/fs.h> +#include <nuttx/drivers/himem_chardev.h> +#include "esp32_himem.h" + +#define HIMEM_UNMAPPED (-1) + +struct himem_chardev +{ + struct list_node node; + size_t size; + esp_himem_handle_t mem_handle; + char name[32]; +}; + +struct himem_chardev_priv +{ + off_t offset; /* himem offset[byte]. update by seek(), read(), write(). */ +}; + +static struct list_node s_himem_chardev_list + = LIST_INITIAL_VALUE(s_himem_chardev_list); +static esp_himem_rangehandle_t s_range_handle; +static sem_t s_sem; +static size_t s_ram_offset; /* used by himem map/unmap */ + +static void *s_himem_ptr; /* mapped himem pointer */ + +static struct file *s_mapped_filep; /* for multi device */ + +/**************************************************************************** + * Priavte Functions + ****************************************************************************/ + +static int himem_chardev_open(FAR struct file *filep) +{ + struct himem_chardev_priv *priv; + + priv = (struct himem_chardev_priv *)malloc( + sizeof(struct himem_chardev_priv)); + if (priv == NULL) + { + _err("Failed to malloc.\n"); + return -1; + } + priv->offset = 0; + filep->f_priv = priv; + + return 0; +} + +static int himem_chardev_close(FAR struct file *filep) +{ + struct himem_chardev_priv *priv = filep->f_priv; + free(priv); + return 0; +} + +static int himem_chardev_read_write(int is_write, + FAR struct file *filep, + FAR char *buffer, + size_t length) +{ + struct himem_chardev *dev + = (struct himem_chardev *)filep->f_inode->i_private; + struct himem_chardev_priv *priv = filep->f_priv; + size_t mmap_offset = 0; + int ret = 0; + size_t i = 0; + size_t copy_size = 0; + void *himem_ptr; + size_t copy_range = 0; + + ret = nxsem_wait(&s_sem); + if (ret != 0) + { + _err("Failed to lock semaphore.\n"); + return -1; + } + + while (i < length) + { + mmap_offset = priv->offset / ESP_HIMEM_BLKSZ; + if (mmap_offset > (dev->size / ESP_HIMEM_BLKSZ)) + { + _err("copy size(%d) over himem phy mem size(%d).\n", + length, dev->size); + goto err; + } + if ((mmap_offset != s_ram_offset) || (s_mapped_filep != filep)) + { + if (s_ram_offset != HIMEM_UNMAPPED) + { + /* already mapped another page */ + + ret = esp_himem_unmap(s_range_handle, + s_himem_ptr, ESP_HIMEM_BLKSZ); + if (ret != 0) + { + _err("Failed to unmap himem.\n"); + goto err; + } + s_ram_offset = HIMEM_UNMAPPED; + s_mapped_filep = NULL; + } + ret = esp_himem_map(dev->mem_handle, s_range_handle, + mmap_offset * ESP_HIMEM_BLKSZ, + 0, ESP_HIMEM_BLKSZ, 0, &s_himem_ptr); + if (ret != 0) + { + _err("Failed to map himem.\n"); + goto err; + } + s_ram_offset = mmap_offset; + s_mapped_filep = filep; + } + himem_ptr = s_himem_ptr + priv->offset % ESP_HIMEM_BLKSZ; + copy_range = ESP_HIMEM_BLKSZ - priv->offset % ESP_HIMEM_BLKSZ; + + /* The case of crossing a phy mem page boundary */ + + if (copy_range < (length - i)) + { + if (is_write) + { + memcpy(himem_ptr, buffer + i, copy_range); + } + else + { + memcpy(buffer + i, himem_ptr, copy_range); + } + priv->offset += copy_range; + copy_size += copy_range; + i += copy_range; + } + else + { + if (is_write) + { + memcpy(himem_ptr, buffer + i, length - i); + } + else + { + memcpy(buffer + i, himem_ptr, length - i); + } + priv->offset += length - i; + copy_size += length - i; + i += length - i; + } + } + nxsem_post(&s_sem); + return (int)(copy_size & INT_MAX); +err: + nxsem_post(&s_sem); + return copy_size > 0 ? (int)(copy_size & INT_MAX) : -1; +} + +static int himem_chardev_read(FAR struct file *filep, + FAR char *dst, size_t length) +{ + return himem_chardev_read_write(0, filep, dst, length); +} + +static int himem_chardev_write(FAR struct file *filep, + FAR const char *src, size_t length) +{ + return himem_chardev_read_write(1, filep, (FAR char *)src, length); +} + +static off_t himem_chardev_seek(FAR struct file *filep, + off_t offset, int whence) +{ + struct himem_chardev_priv *priv = filep->f_priv; + struct himem_chardev *dev = filep->f_inode->i_private; + nxsem_wait(&s_sem); + switch (whence) + { + case SEEK_SET: + priv->offset = offset; + break; + case SEEK_CUR: + priv->offset += offset; + break; + case SEEK_END: + priv->offset = dev->size + offset; + break; + default: + _err("invalid parameter: whence:%d\n", whence); + nxsem_post(&s_sem); + return -1; + } + filep->f_pos = priv->offset; + nxsem_post(&s_sem); + return priv->offset; +} + +static int himem_chardev_ioctl(FAR struct file *filep, + int cmd, unsigned long arg) +{ + return 0; +} + +static const struct file_operations fops = +{ + .open = himem_chardev_open, + .close = himem_chardev_close, + .read = himem_chardev_read, + .write = himem_chardev_write, + .seek = himem_chardev_seek, + .ioctl = himem_chardev_ioctl, Review Comment: 2 spaces indentation ########## arch/xtensa/src/esp32/esp32_himem_chardev.c: ########## @@ -0,0 +1,409 @@ +/**************************************************************************** + * arch/xtensa/src/esp32/esp32_himem_chardev.c + * + * 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 <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <debug.h> +#include <sys/types.h> +#include <nuttx/list.h> +#include <nuttx/himem/himem.h> +#include <nuttx/semaphore.h> +#include <nuttx/fs/fs.h> +#include <nuttx/drivers/himem_chardev.h> +#include "esp32_himem.h" + +#define HIMEM_UNMAPPED (-1) + +struct himem_chardev +{ + struct list_node node; + size_t size; + esp_himem_handle_t mem_handle; + char name[32]; +}; + +struct himem_chardev_priv +{ + off_t offset; /* himem offset[byte]. update by seek(), read(), write(). */ +}; + +static struct list_node s_himem_chardev_list + = LIST_INITIAL_VALUE(s_himem_chardev_list); +static esp_himem_rangehandle_t s_range_handle; +static sem_t s_sem; +static size_t s_ram_offset; /* used by himem map/unmap */ + +static void *s_himem_ptr; /* mapped himem pointer */ + +static struct file *s_mapped_filep; /* for multi device */ + +/**************************************************************************** + * Priavte Functions + ****************************************************************************/ + +static int himem_chardev_open(FAR struct file *filep) +{ + struct himem_chardev_priv *priv; + + priv = (struct himem_chardev_priv *)malloc( + sizeof(struct himem_chardev_priv)); + if (priv == NULL) + { + _err("Failed to malloc.\n"); + return -1; + } + priv->offset = 0; + filep->f_priv = priv; + + return 0; +} + +static int himem_chardev_close(FAR struct file *filep) +{ + struct himem_chardev_priv *priv = filep->f_priv; + free(priv); + return 0; +} + +static int himem_chardev_read_write(int is_write, + FAR struct file *filep, + FAR char *buffer, + size_t length) +{ + struct himem_chardev *dev + = (struct himem_chardev *)filep->f_inode->i_private; + struct himem_chardev_priv *priv = filep->f_priv; + size_t mmap_offset = 0; + int ret = 0; + size_t i = 0; + size_t copy_size = 0; + void *himem_ptr; + size_t copy_range = 0; + + ret = nxsem_wait(&s_sem); + if (ret != 0) + { + _err("Failed to lock semaphore.\n"); + return -1; + } + + while (i < length) + { + mmap_offset = priv->offset / ESP_HIMEM_BLKSZ; + if (mmap_offset > (dev->size / ESP_HIMEM_BLKSZ)) + { + _err("copy size(%d) over himem phy mem size(%d).\n", + length, dev->size); + goto err; + } + if ((mmap_offset != s_ram_offset) || (s_mapped_filep != filep)) + { + if (s_ram_offset != HIMEM_UNMAPPED) + { + /* already mapped another page */ + + ret = esp_himem_unmap(s_range_handle, + s_himem_ptr, ESP_HIMEM_BLKSZ); + if (ret != 0) + { + _err("Failed to unmap himem.\n"); + goto err; + } + s_ram_offset = HIMEM_UNMAPPED; + s_mapped_filep = NULL; + } + ret = esp_himem_map(dev->mem_handle, s_range_handle, + mmap_offset * ESP_HIMEM_BLKSZ, + 0, ESP_HIMEM_BLKSZ, 0, &s_himem_ptr); + if (ret != 0) + { + _err("Failed to map himem.\n"); + goto err; + } + s_ram_offset = mmap_offset; + s_mapped_filep = filep; + } + himem_ptr = s_himem_ptr + priv->offset % ESP_HIMEM_BLKSZ; + copy_range = ESP_HIMEM_BLKSZ - priv->offset % ESP_HIMEM_BLKSZ; + + /* The case of crossing a phy mem page boundary */ + + if (copy_range < (length - i)) + { + if (is_write) + { + memcpy(himem_ptr, buffer + i, copy_range); + } + else + { + memcpy(buffer + i, himem_ptr, copy_range); + } + priv->offset += copy_range; + copy_size += copy_range; + i += copy_range; + } + else + { + if (is_write) + { + memcpy(himem_ptr, buffer + i, length - i); + } + else + { + memcpy(buffer + i, himem_ptr, length - i); + } + priv->offset += length - i; + copy_size += length - i; + i += length - i; + } + } + nxsem_post(&s_sem); + return (int)(copy_size & INT_MAX); +err: + nxsem_post(&s_sem); + return copy_size > 0 ? (int)(copy_size & INT_MAX) : -1; +} + +static int himem_chardev_read(FAR struct file *filep, + FAR char *dst, size_t length) +{ + return himem_chardev_read_write(0, filep, dst, length); +} + +static int himem_chardev_write(FAR struct file *filep, + FAR const char *src, size_t length) +{ + return himem_chardev_read_write(1, filep, (FAR char *)src, length); +} + +static off_t himem_chardev_seek(FAR struct file *filep, + off_t offset, int whence) +{ + struct himem_chardev_priv *priv = filep->f_priv; + struct himem_chardev *dev = filep->f_inode->i_private; + nxsem_wait(&s_sem); + switch (whence) + { + case SEEK_SET: + priv->offset = offset; + break; + case SEEK_CUR: + priv->offset += offset; + break; + case SEEK_END: + priv->offset = dev->size + offset; + break; + default: + _err("invalid parameter: whence:%d\n", whence); + nxsem_post(&s_sem); + return -1; + } + filep->f_pos = priv->offset; + nxsem_post(&s_sem); + return priv->offset; +} + +static int himem_chardev_ioctl(FAR struct file *filep, Review Comment: remove `FAR` for xtensa -- 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