Skip to content

Commit

Permalink
feat(platform/nuttx): add usbd_fs & usbh_serial
Browse files Browse the repository at this point in the history
Signed-off-by: sakumisu <[email protected]>
  • Loading branch information
sakumisu committed Jan 23, 2025
1 parent c399be3 commit b6618b9
Show file tree
Hide file tree
Showing 3 changed files with 372 additions and 40 deletions.
66 changes: 66 additions & 0 deletions platform/nuttx/usbd_fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2025, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <nuttx/fs/fs.h>

#include "usbd_core.h"
#include "usbd_msc.h"

#ifndef CONFIG_USBDEV_MSC_THREAD
#error "CONFIG_USBDEV_MSC_THREAD must be enabled"
#endif

#ifndef CONFIG_USBDEV_MSC_DEVPATH0
#define CONFIG_USBDEV_MSC_DEVPATH0 "/dev/ram1"
#endif

static FAR struct inode *inode;
static struct geometry geo;

void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
{
int ret;

/* Open the block driver */

ret = open_blockdriver(CONFIG_USBDEV_MSC_DEVPATH0, 0, &inode);
if (ret < 0) {
*block_num = 0;
*block_size = 0;
return;
}

/* Get the drive geometry */

if (!inode || !inode->u.i_bops || !inode->u.i_bops->geometry ||
inode->u.i_bops->geometry(inode, &geo) != OK || !geo.geo_available) {
*block_num = 0;
*block_size = 0;
return;
}

*block_num = geo.geo_nsectors;
*block_size = geo.geo_sectorsize;

USB_LOG_INFO("block_num: %ld, block_size: %ld\n", *block_num, *block_size);
}

int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
{
if (inode->u.i_bops->read) {
inode->u.i_bops->read(inode, buffer, sector, length / geo.geo_sectorsize);
}

return 0;
}

int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
{
if (inode->u.i_bops->write) {
inode->u.i_bops->write(inode, buffer, sector, length / geo.geo_sectorsize);
}

return 0;
}
105 changes: 65 additions & 40 deletions platform/nuttx/usbh_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <nuttx/config.h>

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>

#include <nuttx/irq.h>
#include <nuttx/kmalloc.h>
#include <nuttx/signal.h>
#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
#include <nuttx/scsi.h>
#include <nuttx/fs/fs.h>
#include <nuttx/mutex.h>

#include "usbh_core.h"
#include "usbh_msc.h"
Expand All @@ -38,6 +20,57 @@

#define DEV_FORMAT "/dev/sd%c"

static int nuttx_errorcode(int error)
{
int err = 0;

switch (error) {
case -USB_ERR_NOMEM:
err = -EIO;
break;
case -USB_ERR_INVAL:
err = -EINVAL;
break;
case -USB_ERR_NODEV:
err = -ENODEV;
break;
case -USB_ERR_NOTCONN:
err = -ENOTCONN;
break;
case -USB_ERR_NOTSUPP:
err = -EIO;
break;
case -USB_ERR_BUSY:
err = -EBUSY;
break;
case -USB_ERR_RANGE:
err = -ERANGE;
break;
case -USB_ERR_STALL:
err = -EPERM;
break;
case -USB_ERR_NAK:
err = -EAGAIN;
break;
case -USB_ERR_DT:
err = -EIO;
break;
case -USB_ERR_IO:
err = -EIO;
break;
case -USB_ERR_SHUTDOWN:
err = -ESHUTDOWN;
break;
case -USB_ERR_TIMEOUT:
err = -ETIMEDOUT;
break;

default:
break;
}
return err;
}

static int usbhost_open(FAR struct inode *inode);
static int usbhost_close(FAR struct inode *inode);
static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer,
Expand Down Expand Up @@ -90,18 +123,14 @@ static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer,
DEBUGASSERT(inode->i_private);
msc_class = (struct usbh_msc *)inode->i_private;

if (msc_class->hport && msc_class->hport->connected) {
ret = usbh_msc_scsi_read10(msc_class, startsector, (uint8_t *)buffer, nsectors);
if (ret < 0) {
return ret;
} else {
#ifdef CONFIG_ARCH_DCACHE
up_invalidate_dcache((uintptr_t)buffer, (uintptr_t)(buffer + nsectors * msc_class->blocksize));
#endif
return nsectors;
}
ret = usbh_msc_scsi_read10(msc_class, startsector, (uint8_t *)buffer, nsectors);
if (ret < 0) {
return nuttx_errorcode(ret);
} else {
return -ENODEV;
#if defined(CONFIG_ARCH_DCACHE) && !defined(CONFIG_USB_DCACHE_ENABLE)
up_invalidate_dcache((uintptr_t)buffer, (uintptr_t)(buffer + nsectors * msc_class->blocksize));
#endif
return nsectors;
}
}

Expand All @@ -115,18 +144,14 @@ static ssize_t usbhost_write(FAR struct inode *inode,
DEBUGASSERT(inode->i_private);
msc_class = (struct usbh_msc *)inode->i_private;

if (msc_class->hport && msc_class->hport->connected) {
#ifdef CONFIG_ARCH_DCACHE
up_flush_dcache((uintptr_t)buffer, (uintptr_t)(buffer + nsectors * msc_class->blocksize));
#if defined(CONFIG_ARCH_DCACHE) && !defined(CONFIG_USB_DCACHE_ENABLE)
up_clean_dcache((uintptr_t)buffer, (uintptr_t)(buffer + nsectors * msc_class->blocksize));
#endif
ret = usbh_msc_scsi_write10(msc_class, startsector, (uint8_t *)buffer, nsectors);
if (ret < 0) {
return ret;
} else {
return nsectors;
}
ret = usbh_msc_scsi_write10(msc_class, startsector, (uint8_t *)buffer, nsectors);
if (ret < 0) {
return nuttx_errorcode(ret);
} else {
return -ENODEV;
return nsectors;
}
}

Expand All @@ -147,7 +172,7 @@ static int usbhost_geometry(FAR struct inode *inode,
geometry->geo_nsectors = msc_class->blocknum;
geometry->geo_sectorsize = msc_class->blocksize;

uinfo("nsectors: %" PRIdOFF " sectorsize: %" PRIi16 "\n",
USB_LOG_DBG("nsectors: %ld, sectorsize: %ld\n",
geometry->geo_nsectors, geometry->geo_sectorsize);
return OK;
} else {
Expand Down
Loading

0 comments on commit b6618b9

Please sign in to comment.