hal/hald/linux block_class_device.c,1.46,1.47

David Zeuthen david at freedesktop.org
Sun Aug 1 12:14:05 PDT 2004


Update of /cvs/hal/hal/hald/linux
In directory pdx:/tmp/cvs-serv22767/hald/linux

Modified Files:
	block_class_device.c 
Log Message:
2004-08-01  David Zeuthen  <david at fubar.dk>

	Patch from Kay Sievers <kay.sievers at vrfy.org>

	* hald/Makefile.am:
	* hald/linux/block_class_device.c: (block_class_pre_process),
	(block_class_compute_udi):
	* hald/linux/drive_id/drive_id.c: (dump), (set_str), (scsi_inq),
	(probe_scsi), (probe_ata), (drive_id_probe), (drive_id_open_fd),
	(drive_id_open_node), (drive_id_open_dev_t), (drive_id_close):
	* hald/linux/drive_id/drive_id.h:
	Here is the code, that reads the serial number of a drive,
	directly connected to an ATA or SCSI bus. HAL probes for the
	values and adds: storage.serial, storage.firmware_version,
        storage.revision if available.
	Note: This will only work on native interfaces. For devices behind
	USB, it's not possible to get any of these values. There are some
	bridges out there, which are capable to set the USB-values itself,
	to the values from a ATA Inquiry, but unfortunally, most of the
	bridges out there, don't do it.  "USB storage compliance" mandates
	a unique serial number, but most of the vendors seems not to care
	about it.
	I changed the compute_udi for block devices. If we get a serial
	number or a uuid we use it to compute the udi.



Index: block_class_device.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux/block_class_device.c,v
retrieving revision 1.46
retrieving revision 1.47
diff -u -d -r1.46 -r1.47
--- block_class_device.c	29 Jul 2004 17:07:09 -0000	1.46
+++ block_class_device.c	1 Aug 2004 19:13:56 -0000	1.47
@@ -60,6 +60,7 @@
 #include "common.h"
 
 #include "volume_id/volume_id.h"
+#include "drive_id/drive_id.h"
 #include "linux_dvd_rw_utils.h"
 
 /**
@@ -1272,10 +1273,12 @@
 	HAL_INFO (("Bus type is %s!",
 		   hal_device_property_get_string (parent, "info.bus")));
 
-	if (strcmp (hal_device_property_get_string (parent, "info.bus"), 
+	if (strcmp (hal_device_property_get_string (parent, "info.bus"),
 			    "ide") == 0) {
 		const char *ide_name;
 		char *model;
+		const char *device_file;
+		struct drive_id *did;
 		char *media;
 
 		ide_name = get_last_element (hal_device_property_get_string
@@ -1283,14 +1286,27 @@
 
 		model = read_single_line ("/proc/ide/%s/model", ide_name);
 		if (model != NULL) {
-			hal_device_property_set_string (stordev, 
-							"storage.model", 
+			hal_device_property_set_string (stordev,
+							"storage.model",
 							model);
 			hal_device_property_set_string (d, 
 							"info.product",
 							model);
 		}
 
+		device_file = hal_device_property_get_string (d, "block.device");
+		did = drive_id_open_node(device_file);
+		if (drive_id_probe(did, DID_ATA) == 0) {
+			if (did->serial[0] != '\0')
+				hal_device_property_set_string (stordev, 
+								"storage.serial",
+								did->serial);
+			if (did->firmware[0] != '\0')
+				hal_device_property_set_string (stordev, 
+								"storage.firmware_version",
+								did->firmware);
+		}
+		drive_id_close(did);
 
 		/* According to the function proc_ide_read_media() in 
 		 * drivers/ide/ide-proc.c in the Linux sources, media
@@ -1340,9 +1356,11 @@
 			
 		}
 		
-	} else if (strcmp (hal_device_property_get_string (parent, 
+	} else if (strcmp (hal_device_property_get_string (parent,
 							 "info.bus"),
 			   "scsi_device") == 0) {
+		const char *device_file;
+		struct drive_id *did;
 		const char *sysfs_path;
 		char attr_path[SYSFS_PATH_MAX];
 		struct sysfs_attribute *attr;
@@ -1370,7 +1388,7 @@
 		attr = sysfs_open_attribute (attr_path);
 		if (sysfs_read_attribute (attr) >= 0) {
 			strip_space (attr->value);
-			hal_device_property_set_string (d, 
+			hal_device_property_set_string (d,
 							"info.product",
 							attr->value);
 			hal_device_property_set_string (stordev,
@@ -1378,7 +1396,21 @@
 							attr->value);
 			sysfs_close_attribute (attr);
 		}
-		
+
+		device_file = hal_device_property_get_string (d, "block.device");
+		did = drive_id_open_node(device_file);
+		if (drive_id_probe(did, DID_SCSI) == 0) {
+			if (did->serial[0] != '\0')
+				hal_device_property_set_string (stordev,
+								"storage.serial",
+								did->serial);
+			if (did->revision[0] != '\0')
+				hal_device_property_set_string (stordev, 
+								"storage.revision",
+								did->revision);
+		}
+		drive_id_close(did);
+
 		snprintf (attr_path, SYSFS_PATH_MAX,
 			  "%s/device/type", sysfs_path);
 		attr = sysfs_open_attribute (attr_path);
@@ -1534,19 +1566,52 @@
 static char *
 block_class_compute_udi (HalDevice * d, int append_num)
 {
-        char *format;
-        static char buf[256];
- 
-        if (append_num == -1)
-                format = "/org/freedesktop/Hal/devices/block_%d_%d";
-        else
-                format = "/org/freedesktop/Hal/devices/block_%d_%d-%d";
- 
-        snprintf (buf, 256, format,
-                  hal_device_property_get_int (d, "block.major"),
-                  hal_device_property_get_int (d, "block.minor"), append_num);
- 
-        return buf;
+	char *format;
+	static char buf[256];
+	char id[256] = "";
+
+	if (hal_device_property_get_bool (d, "block.is_volume")) {
+		const char *label =
+			hal_device_property_get_string (d, "volume.label");
+		const char *uuid =
+			hal_device_property_get_string (d, "volume.uuid");
+
+		if (uuid != NULL && uuid[0] != '\0') {
+			strcpy(id, uuid);
+		} else if (label != NULL && label[0] != '\0') {
+			if (strcmp(label, "/") == 0)
+				strcpy(id, "root");
+			else
+				strcpy(id, label);
+		}
+	} else {
+		const char *model =
+			hal_device_property_get_string (d, "storage.model");
+		const char *serial =
+			hal_device_property_get_string (d, "storage.serial");
+
+		if (model != NULL && serial != NULL && serial[0] != '\0') {
+			snprintf (id, 255, "%s-%s", model, serial);
+		}
+	}
+	id[255] = '\0';
+
+	/* FIXME: do something better then fallback to major/minor */
+	if (id[0] == '\0') {
+		int major = hal_device_property_get_int (d, "block.major");
+		int minor = hal_device_property_get_int (d, "block.minor");
+		sprintf (id, "%d_%d", major, minor);
+	}
+
+	if (append_num == -1)
+		format = "/org/freedesktop/Hal/devices/block_%s";
+	else
+		format = "/org/freedesktop/Hal/devices/block_%s-%d";
+
+	snprintf (buf, 255, format, id, append_num);
+	buf[255] = '\0';
+
+	return buf;
 }
 
 




More information about the hal-commit mailing list