]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blkdev: add blkdev_scsi_type_to_name()
authorSami Kerola <kerolasa@iki.fi>
Mon, 11 Jun 2012 19:17:38 +0000 (21:17 +0200)
committerSami Kerola <kerolasa@iki.fi>
Mon, 11 Jun 2012 20:06:06 +0000 (22:06 +0200)
Add a function, and necessary symbols, to convert scsi type id's
to name strings.

Reference: http://permalink.gmane.org/gmane.linux.utilities.util-linux-ng/5994
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/blkdev.h
lib/blkdev.c

index 56dbce75c93fc3068b0e0c572a72ad3c36f4fa52..84c26dd3304ed5cce45f926da02a82b01b2f381a 100644 (file)
 # define CDROM_GET_CAPABILITY 0x5331
 #endif
 
+/* SCSI device types.  Copied almost as-is from kernel header.
+ * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/scsi/scsi.h */
+#define SCSI_TYPE_DISK                 0x00
+#define SCSI_TYPE_TAPE                 0x01
+#define SCSI_TYPE_PRINTER              0x02
+#define SCSI_TYPE_PROCESSOR            0x03    /* HP scanners use this */
+#define SCSI_TYPE_WORM                 0x04    /* Treated as ROM by our system */
+#define SCSI_TYPE_ROM                  0x05
+#define SCSI_TYPE_SCANNER              0x06
+#define SCSI_TYPE_MOD                  0x07    /* Magneto-optical disk - treated as SCSI_TYPE_DISK */
+#define SCSI_TYPE_MEDIUM_CHANGER       0x08
+#define SCSI_TYPE_COMM                 0x09    /* Communications device */
+#define SCSI_TYPE_RAID                 0x0c
+#define SCSI_TYPE_ENCLOSURE            0x0d    /* Enclosure Services Device */
+#define SCSI_TYPE_RBC                  0x0e
+#define SCSI_TYPE_OSD                  0x11
+#define SCSI_TYPE_NO_LUN               0x7f
+
 struct hd_geometry {
        unsigned char heads;
        unsigned char sectors;
@@ -113,4 +131,7 @@ int blkdev_is_cdrom(int fd);
 /* get device's geometry - legacy */
 int blkdev_get_geometry(int fd, unsigned int *h, unsigned int *s);
 
+/* convert scsi type code to name */
+char *blkdev_scsi_type_to_name(int type);
+
 #endif /* BLKDEV_H */
index 9e13e13709d984af950481262b15ac4ac784cc7b..35ec3695454e26648ba8a4c2a55a744d4d7123a1 100644 (file)
@@ -21,8 +21,9 @@
 #endif
 
 #include "blkdev.h"
-#include "linux_version.h"
 #include "c.h"
+#include "linux_version.h"
+#include "xalloc.h"
 
 static long
 blkdev_valid_offset (int fd, off_t offset) {
@@ -288,6 +289,51 @@ int blkdev_get_geometry(int fd, unsigned int *h, unsigned int *s)
        return -1;
 }
 
+/*
+ * Convert scsi type to human readable string.  Return value is
+ * expected to free'd after use.
+ */
+char *blkdev_scsi_type_to_name(int type)
+{
+       char *type_str = NULL;
+
+       switch (type) {
+       case SCSI_TYPE_DISK:
+               return xstrdup("disk");
+       case SCSI_TYPE_TAPE:
+               return xstrdup("tape");
+       case SCSI_TYPE_PRINTER:
+               return xstrdup("printer");
+       case SCSI_TYPE_PROCESSOR:
+               return xstrdup("processor");
+       case SCSI_TYPE_WORM:
+               return xstrdup("worm");
+       case SCSI_TYPE_ROM:
+               return xstrdup("rom");
+       case SCSI_TYPE_SCANNER:
+               return xstrdup("scanner");
+       case SCSI_TYPE_MOD:
+               return xstrdup("mo-disk");
+       case SCSI_TYPE_MEDIUM_CHANGER:
+               return xstrdup("changer");
+       case SCSI_TYPE_COMM:
+               return xstrdup("comm");
+       case SCSI_TYPE_RAID:
+               return xstrdup("raid");
+       case SCSI_TYPE_ENCLOSURE:
+               return xstrdup("enclosure");
+       case SCSI_TYPE_RBC:
+               return xstrdup("rbc");
+       case SCSI_TYPE_OSD:
+               return xstrdup("osd");
+       case SCSI_TYPE_NO_LUN:
+               return xstrdup("no-lun");
+       default:
+               xasprintf(&type_str, "0x%02x", type);
+               return type_str;
+       }
+}
+
 #ifdef TEST_PROGRAM
 #include <stdio.h>
 #include <stdlib.h>