Skip to contentSkip to conversation

add devfs support to cpuid and msr

PATCH New Jordan Breeding 1 participant 1 message Oct 4, 2002

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add devfs support to cpuid and msr

Hello,

This patch was originally floating around linux-kernel a while ago
for 2.4.x, all I have done is update it to work with 2.5.x. It will only create directories and device files for cpus present at boot, there was a seperate version of a patch similar to this which implemented devfs_per_cpu_{un,}register functions but the devfs code it was based is very different than todays devfs in 2.5.x. This patch works well enough for me to simply gain access to /dev/cpu/%d/{cpuid,msr}. Thanks.

Jordan

arch/i386/kernel/cpuid.c +33 -5
diff -urN linux-2.5.36/arch/i386/kernel/cpuid.c linux-2.5.36-bk/arch/i386/kernel/cpuid.c
@@ -23,6 +23,8 @@
  *
  * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
  * an SMP box will direct the access to CPU %d.
+ *
+ * 20020812 - David McIlwraith ([email protected]) - Added devfs support
  */
 
 #include <linux/module.h>
@@ -36,14 +38,17 @@
 #include <linux/smp.h>
 #include <linux/major.h>
 #include <linux/fs.h>
+#include <linux/devfs_fs_kernel.h>
+#include <linux/slab.h>
 #include <linux/smp_lock.h>
-#include <linux/fs.h>
 
 #include <asm/processor.h>
 #include <asm/msr.h>
 #include <asm/uaccess.h>
 #include <asm/system.h>
 
+static devfs_handle_t *devfs_handles;
+
 #ifdef CONFIG_SMP
 
 struct cpuid_command {
@@ -113,7 +118,7 @@
   u32 data[4];
   size_t rv;
   u32 reg = *ppos;
-  int cpu = minor(file->f_dentry->d_inode->i_rdev);
+  int cpu = file->private_data ? (int) file->private_data - 1 : minor(file->f_dentry->d_inode->i_rdev);
   
   if ( count % 16 )
     return -EINVAL; /* Invalid chunk size */
@@ -131,7 +136,7 @@
 
 static int cpuid_open(struct inode *inode, struct file *file)
 {
-  int cpu = minor(file->f_dentry->d_inode->i_rdev);
+  int cpu = file->private_data ? (int) file->private_data - 1 : minor(file->f_dentry->d_inode->i_rdev);
   struct cpuinfo_x86 *c = &(cpu_data)[cpu];
 
   if ( !(cpu_online_map & (1UL << cpu)) )
@@ -154,18 +159,41 @@
 
 int __init cpuid_init(void)
 {
-  if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
+  int i;
+  char devname[24];
+
+  if (register_chrdev(CPUID_MAJOR, "cpu/%d/cpuid", &cpuid_fops)) {
     printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
 	   CPUID_MAJOR);
     return -EBUSY;
   }
 
+  devfs_handles = (devfs_handle_t *) kmalloc(num_online_cpus() * sizeof(devfs_handle_t), GFP_KERNEL);
+  if (!devfs_handles) {
+    printk(KERN_ERR "cpuid: unable to allocate memory for devfs\n");
+    return -ENOMEM;
+  }
+
+  for (i = 0; i < num_online_cpus(); ++i)
+  {
+    snprintf(devname, 24, "cpu/%i/cpuid", i);
+    devfs_handles[i] = devfs_register(NULL, devname, DEVFS_FL_DEFAULT, CPUID_MAJOR, i,
+ 				      S_IFCHR | S_IRUGO, &cpuid_fops, (void *) i + 1);
+  }
+
   return 0;
 }
 
 void __exit cpuid_exit(void)
 {
-  unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
+  int i;
+
+  unregister_chrdev(CPUID_MAJOR, "cpu/%d/cpuid");
+
+  for (i = 0; i < num_online_cpus(); ++i)
+    devfs_unregister(devfs_handles[i]);
+
+  kfree(devfs_handles);
 }
 
 module_init(cpuid_init);
arch/i386/kernel/msr.c +35 -6

92 lines not on this page · raw message