Skip to contentSkip to conversation

2.4.19 CPUID, MSR: add devfs support

PATCH New David McIlwraith 1 participant 1 message Aug 11, 2002

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 2.4.19 CPUID, MSR: add devfs support

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

arch/i386/kernel/cpuid.c +33 -4
diff -Naur linux/arch/i386/kernel/cpuid.c linux-2.4.19-dm1/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>
@@ -35,12 +37,16 @@
 #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
 
 struct cpuid_command {
@@ -101,7 +107,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 */
@@ -119,7 +125,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)) )
@@ -142,18 +148,41 @@
 
 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);
arch/i386/kernel/msr.c +35 -6

92 lines not on this page · raw message