[PATCH 02/21] nvdisk: Use rtems_blkdev_create()

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Aug 6 07:14:34 UTC 2018


Update #3358.
---
 cpukit/libblock/src/nvdisk.c     | 70 +++++++++++-----------------------------
 testsuites/samples/fileio/init.c | 61 ++--------------------------------
 2 files changed, 22 insertions(+), 109 deletions(-)

diff --git a/cpukit/libblock/src/nvdisk.c b/cpukit/libblock/src/nvdisk.c
index bb1fc5dbe9..693c0b13a0 100644
--- a/cpukit/libblock/src/nvdisk.c
+++ b/cpukit/libblock/src/nvdisk.c
@@ -26,7 +26,6 @@
 #include <inttypes.h>
 
 #include <rtems/blkdev.h>
-#include <rtems/diskdevs.h>
 #include <rtems/nvdisk.h>
 #include <rtems/thread.h>
 
@@ -93,10 +92,8 @@ typedef struct rtems_nvdisk_device_ctl
  * The NV disk control structure for a single disk. There is one
  * for each minor disk in the system.
  */
-typedef struct rtems_mvdisk
+typedef struct
 {
-  rtems_device_major_number major;        /**< The driver's major number. */
-  rtems_device_minor_number minor;        /**< The driver's minor number. */
   uint32_t                  flags;        /**< configuration flags. */
   uint32_t                  block_size;   /**< The block size for this disk. */
   uint32_t                  block_count;  /**< The number of available blocks. */
@@ -108,16 +105,6 @@ typedef struct rtems_mvdisk
 } rtems_nvdisk;
 
 /**
- * The array of NV disks we support.
- */
-static rtems_nvdisk* rtems_nvdisks;
-
-/**
- * The number of NV disks we have.
- */
-static uint32_t rtems_nvdisk_count;
-
-/**
  * The CRC16 factor table. Created during initialisation.
  */
 static uint16_t* rtems_nvdisk_crc16_factor;
@@ -688,17 +675,10 @@ rtems_nvdisk_erase_disk (rtems_nvdisk* nvd)
 static int
 rtems_nvdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
 {
-  dev_t                     dev = rtems_disk_get_device_identifier (dd);
-  rtems_device_minor_number minor = rtems_filesystem_dev_minor_t (dev);
-  rtems_blkdev_request*     r = argp;
-
-  if (minor >= rtems_nvdisk_count)
-  {
-    errno = ENODEV;
-    return -1;
-  }
+  rtems_nvdisk*         nvd = rtems_disk_get_driver_data (dd);
+  rtems_blkdev_request* r = argp;
 
-  if (rtems_nvdisks[minor].device_count == 0)
+  if (nvd->device_count == 0)
   {
     errno = ENODEV;
     return -1;
@@ -706,7 +686,7 @@ rtems_nvdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
 
   errno = 0;
 
-  rtems_mutex_lock (&rtems_nvdisks[minor].lock);
+  rtems_mutex_lock (&nvd->lock);
 
   switch (req)
   {
@@ -714,11 +694,11 @@ rtems_nvdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
       switch (r->req)
       {
         case RTEMS_BLKDEV_REQ_READ:
-          errno = rtems_nvdisk_read (&rtems_nvdisks[minor], r);
+          errno = rtems_nvdisk_read (nvd, r);
           break;
 
         case RTEMS_BLKDEV_REQ_WRITE:
-          errno = rtems_nvdisk_write (&rtems_nvdisks[minor], r);
+          errno = rtems_nvdisk_write (nvd, r);
           break;
 
         default:
@@ -728,11 +708,11 @@ rtems_nvdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
       break;
 
     case RTEMS_NVDISK_IOCTL_ERASE_DISK:
-      errno = rtems_nvdisk_erase_disk (&rtems_nvdisks[minor]);
+      errno = rtems_nvdisk_erase_disk (nvd);
       break;
 
     case RTEMS_NVDISK_IOCTL_INFO_LEVEL:
-      rtems_nvdisks[minor].info_level = (uintptr_t) argp;
+      nvd->info_level = (uintptr_t) argp;
       break;
 
     default:
@@ -740,7 +720,7 @@ rtems_nvdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
       break;
   }
 
-  rtems_mutex_unlock (&rtems_nvdisks[minor].lock);
+  rtems_mutex_unlock (&nvd->lock);
 
   return errno == 0 ? 0 : -1;
 }
@@ -755,41 +735,31 @@ rtems_nvdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
  * @param arg Initialization argument, not applicable.
  */
 rtems_device_driver
-rtems_nvdisk_initialize (rtems_device_major_number major,
-                        rtems_device_minor_number minor,
+rtems_nvdisk_initialize (rtems_device_major_number major RTEMS_UNUSED,
+                        rtems_device_minor_number minor RTEMS_UNUSED,
                         void*                     arg RTEMS_UNUSED)
 {
   const rtems_nvdisk_config* c = rtems_nvdisk_configuration;
   rtems_nvdisk*              nvd;
   rtems_status_code          sc;
-
-  sc = rtems_disk_io_initialize ();
-  if (sc != RTEMS_SUCCESSFUL)
-    return sc;
+  uint32_t                   i;
 
   sc = rtems_nvdisk_crc16_gen_factors (0x8408);
   if (sc != RTEMS_SUCCESSFUL)
       return sc;
 
-  rtems_nvdisks = calloc (rtems_nvdisk_configuration_size,
-                          sizeof (rtems_nvdisk));
-
-  if (!rtems_nvdisks)
+  nvd = calloc (rtems_nvdisk_configuration_size, sizeof (*nvd));
+  if (!nvd)
     return RTEMS_NO_MEMORY;
 
-  for (minor = 0; minor < rtems_nvdisk_configuration_size; minor++, c++)
+  for (i = 0; i < rtems_nvdisk_configuration_size; i++, c++, nvd++)
   {
     char     name[] = RTEMS_NVDISK_DEVICE_BASE_NAME "a";
-    dev_t    dev = rtems_filesystem_make_dev_t (major, minor);
     uint32_t device;
     uint32_t blocks = 0;
 
-    nvd = &rtems_nvdisks[minor];
+    name [sizeof(RTEMS_NVDISK_DEVICE_BASE_NAME)] += i;
 
-    name [sizeof(RTEMS_NVDISK_DEVICE_BASE_NAME)] += minor;
-
-    nvd->major        = major;
-    nvd->minor        = minor;
     nvd->flags        = c->flags;
     nvd->block_size   = c->block_size;
     nvd->info_level   = c->info_level;
@@ -815,8 +785,8 @@ rtems_nvdisk_initialize (rtems_device_major_number major,
     nvd->block_count  = blocks;
     nvd->device_count = c->device_count;
 
-    sc = rtems_disk_create_phys(dev, c->block_size, blocks,
-                                rtems_nvdisk_ioctl, NULL, name);
+    sc = rtems_blkdev_create(name, c->block_size, blocks,
+                             rtems_nvdisk_ioctl, nvd);
     if (sc != RTEMS_SUCCESSFUL)
     {
       rtems_nvdisk_error ("disk create phy failed");
@@ -826,7 +796,5 @@ rtems_nvdisk_initialize (rtems_device_major_number major,
     rtems_mutex_init (&nvd->lock, "NV Disk");
   }
 
-  rtems_nvdisk_count = rtems_nvdisk_configuration_size;
-
   return RTEMS_SUCCESSFUL;
 }
diff --git a/testsuites/samples/fileio/init.c b/testsuites/samples/fileio/init.c
index 974535b83c..6d30c41db0 100644
--- a/testsuites/samples/fileio/init.c
+++ b/testsuites/samples/fileio/init.c
@@ -75,58 +75,6 @@ const rtems_nvdisk_config rtems_nvdisk_configuration[] =
  */
 uint32_t rtems_nvdisk_configuration_size = 1;
 
-/**
- * Create the NV Disk Driver entry.
- */
-rtems_driver_address_table rtems_nvdisk_io_ops = {
-  initialization_entry: rtems_nvdisk_initialize,
-  open_entry:           rtems_blkdev_generic_open,
-  close_entry:          rtems_blkdev_generic_close,
-  read_entry:           rtems_blkdev_generic_read,
-  write_entry:          rtems_blkdev_generic_write,
-  control_entry:        rtems_blkdev_generic_ioctl
-};
-
-#if 0
-int
-setup_nvdisk (const char* mntpath)
-{
-  rtems_device_major_number major;
-  rtems_status_code         sc;
-
-  /*
-   * For our test we do not have any static RAM or EEPROM devices so
-   * we allocate the memory from the heap.
-   */
-  rtems_nv_heap_device_descriptor[0].base =
-    malloc (rtems_nv_heap_device_descriptor[0].size);
-
-  if (!rtems_nv_heap_device_descriptor[0].base)
-  {
-    printf ("error: no memory for NV disk\n");
-    return 1;
-  }
-  
-  /*
-   * Register the NV Disk driver.
-   */
-  printf ("Register NV Disk Driver: ");
-  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
-                                 &rtems_nvdisk_io_ops,
-                                 &major);
-  if (sc != RTEMS_SUCCESSFUL)
-  {
-    printf ("error: nvdisk driver not initialised: %s\n",
-            rtems_status_text (sc));
-    return 1;
-  }
-  
-  printf ("successful\n");
-
-  return 0;
-}
-#endif
-
 /*
  * Table of FAT file systems that will be mounted
  * with the "fsmount" function.
@@ -473,7 +421,6 @@ create_ramdisk (int argc, char *argv[])
 static int
 create_nvdisk (int argc, char *argv[])
 {
-  rtems_device_major_number major;
   rtems_status_code         sc;
   int                       arg;
   uint32_t                  size = 0;
@@ -485,7 +432,7 @@ create_nvdisk (int argc, char *argv[])
   {
     if (argv[arg][0] == '-')
     {
-      switch (argv[arg][0])
+      switch (argv[arg][1])
       {
         case 's':
           ++arg;
@@ -545,10 +492,8 @@ create_nvdisk (int argc, char *argv[])
           " block-size=%" PRIu32"]:",
           rtems_nv_heap_device_descriptor[0].size,
           rtems_nvdisk_configuration[0].block_size);
-  
-  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
-                                 &rtems_nvdisk_io_ops,
-                                 &major);
+
+  sc = rtems_nvdisk_initialize (0, 0, NULL);
   if (sc != RTEMS_SUCCESSFUL)
   {
     printf ("error: nvdisk driver not initialised: %s\n",
-- 
2.13.7




More information about the devel mailing list