2.4.19 CPUID, MSR: add devfs support

2.4.19 CPUID, MSR: add devfs support

Post by David McIlwrait » Tue, 13 Aug 2002 06:00:07



This set of patches adds devfs support for both cpuid and msr drivers in kernel 2.4.19.

diff -Naur linux/arch/i386/kernel/cpuid.c linux-2.4.19-dm1/arch/i386/kernel/cpuid.c
--- linux/arch/i386/kernel/cpuid.c      Mon Aug 12 04:32:08 2002

  *
  * 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.
+ *

  */


 #include <linux/poll.h>
 #include <linux/smp.h>
 #include <linux/major.h>
+#include <linux/devfs_fs_kernel.h>
+#include <linux/slab.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


   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 )

 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];


 int __init cpuid_init(void)
 {
-  if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
+  int i;
+  char devname[24];
+
+  if (devfs_register_chrdev(CPUID_MAJOR, "cpu/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(smp_num_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 < smp_num_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;
+
+  devfs_unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
+
+  for (i = 0; i < smp_num_cpus; ++i)
+    devfs_unregister(devfs_handles[i]);
+
+  kfree(devfs_handles);
 }

 module_init(cpuid_init);
diff -Naur linux/arch/i386/kernel/msr.c linux-2.4.19-dm1/arch/i386/kernel/msr.c
--- linux/arch/i386/kernel/msr.c        Mon Aug 12 04:32:13 2002

  *
  * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
  * an SMP box will direct the access to CPU %d.
+ *

  */


 #include <linux/poll.h>
 #include <linux/smp.h>
 #include <linux/major.h>
+#include <linux/devfs_fs_kernel.h>
+#include <linux/slab.h>

 #include <asm/processor.h>
 #include <asm/msr.h>
 #include <asm/uaccess.h>
 #include <asm/system.h>

+static devfs_handle_t *devfs_handles;
+
 /* Note: "err" is handled in a funny way below.  Otherwise one version
    of gcc or another breaks. */

   u32 data[2];
   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);
   int err;


   u32 data[2];
   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);
   int err;


 static int msr_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];


 int __init msr_init(void)
 {
-  if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
+  int i;
+  char devname[24];
+
+  if (devfs_register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
     printk(KERN_ERR "msr: unable to get major %d for msr\n",
           MSR_MAJOR);
     return -EBUSY;
   }
-  
+
+  devfs_handles = (devfs_handle_t *) kmalloc(smp_num_cpus * sizeof(devfs_handle_t), GFP_KERNEL);
+  if (!devfs_handles) {
+    printk(KERN_ERR "msr: unable to allocate memory for devfs\n");
+    return -ENOMEM;
+  }
+
+  for (i = 0; i < smp_num_cpus; ++i)
+  {
+    snprintf(devname, 24, "cpu/%i/msr", i);
+    devfs_handles[i] = devfs_register(NULL, devname, DEVFS_FL_DEFAULT, MSR_MAJOR, i,
+                                     S_IFCHR | S_IRUGO | S_IWUSR, &msr_fops, (void *) i + 1);
+  }
+
   return 0;
 }

 void __exit msr_exit(void)
 {
-  unregister_chrdev(MSR_MAJOR, "cpu/msr");
+  int i;
+
+  devfs_unregister_chrdev(MSR_MAJOR, "cpu/msr");
+
+  for (i = 0; i < smp_num_cpus; ++i)
+    devfs_unregister(devfs_handles[i]);
+
+  kfree(devfs_handles);
 }

 module_init(msr_init);
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in

More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/