2004-04-15 21:11:32

by Hanna Linder

[permalink] [raw]
Subject: [PATCH 2.6.5] Add class support to drivers/mtd/mtdchar.c


This patch add sysfs class support to the MTD char device driver.
I have verified it compiles and works. Please review or test with an
actual device if possible.

Thanks.

Hanna Linder
IBM Linux Technology Center
----
diff -Nrup -Xdontdiff linux-2.6.5/drivers/mtd/mtdchar.c linux-2.6.5p/drivers/mtd/mtdchar.c
--- linux-2.6.5/drivers/mtd/mtdchar.c 2004-04-03 19:37:37.000000000 -0800
+++ linux-2.6.5p/drivers/mtd/mtdchar.c 2004-04-15 13:48:44.000000000 -0700
@@ -12,6 +12,7 @@
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
+#include <linux/device.h>
#include <asm/uaccess.h>

#ifdef CONFIG_DEVFS_FS
@@ -26,6 +27,10 @@ static struct mtd_notifier notifier = {

#endif

+/* For class support */
+static struct class_simple *mtd_class;
+static int mtd_minor;
+
static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
{
struct mtd_info *mtd=(struct mtd_info *)file->private_data;
@@ -473,18 +478,43 @@ static struct file_operations mtd_fops =

static void mtd_notify_add(struct mtd_info* mtd)
{
+ int err = 0;
if (!mtd)
return;
- devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
+ class_simple_device_add(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
+ NULL, "mtd%d", mtd->index);
+ err = devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
- devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
+ if (err) {
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
+ goto out_class;
+ }
+ class_simple_device_add(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
+ NULL, "mtd%dro", mtd->index);
+ err = devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
+ if (err)
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
+ else
+ goto out;
+out_class:
+ class_simple_destroy(mtd_class);
+ unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
+out:
+ /* Need the minor number to be global so the module_exit function can see it*/
+ mtd_minor = mtd->index;
+ return err;
+
}

static void mtd_notify_remove(struct mtd_info* mtd)
{
if (!mtd)
return;
+
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
+ class_simple_destroy(mtd_class);
devfs_remove("mtd/%d", mtd->index);
devfs_remove("mtd/%dro", mtd->index);
}
@@ -492,22 +522,36 @@ static void mtd_notify_remove(struct mtd

static int __init init_mtdchar(void)
{
+ int err = 0;
+
if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
MTD_CHAR_MAJOR);
- return -EAGAIN;
+ err = -EAGAIN;
+ }
+ mtd_class = class_simple_create(THIS_MODULE, "mtd");
+ if (IS_ERR(mtd_class)) {
+ err = PTR_ERR(mtd_class);
+ goto out_chrdev;
}
-
#ifdef CONFIG_DEVFS_FS
devfs_mk_dir("mtd");

register_mtd_user(&notifier);
#endif
- return 0;
+ goto out;
+
+out_chrdev:
+ unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
+out:
+ return err;
}

static void __exit cleanup_mtdchar(void)
{
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd_minor*2));
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd_minor*2+1));
+ class_simple_destroy(mtd_class);
#ifdef CONFIG_DEVFS_FS
unregister_mtd_user(&notifier);
devfs_remove("mtd");


2004-04-15 21:17:17

by David Woodhouse

[permalink] [raw]
Subject: Re: [PATCH 2.6.5] Add class support to drivers/mtd/mtdchar.c

On Thu, 2004-04-15 at 14:10 -0700, Hanna Linder wrote:
> This patch add sysfs class support to the MTD char device driver.
> I have verified it compiles and works. Please review or test with an
> actual device if possible.

CONFIG_MTD_MTDRAM would let you test -- otherwise I'll look at it when I
get back to the office in a couple of weeks. Thanks.

--
dwmw2

2004-04-16 22:42:08

by Hanna Linder

[permalink] [raw]
Subject: Re: [PATCH 2.6.5] Add class support to drivers/mtd/mtdchar.c

--On Thursday, April 15, 2004 05:14:59 PM -0400 David Woodhouse <[email protected]> wrote:

> CONFIG_MTD_MTDRAM would let you test -- otherwise I'll look at it when I
> get back to the office in a couple of weeks. Thanks.
>
> --
> dwmw2

Thanks David. I tried the MTDRAM and it showed up in the class tree but
no dev file was made. I suspect it needs a device attached. Anyway, I found
a small bug by code inspection so here is the new patch.

Thanks.

Hanna

-----
diff -Nrup -Xdontdiff linux-2.6.5/drivers/mtd/mtdchar.c linux-2.6.5mtd/drivers/mtd/mtdchar.c
--- linux-2.6.5/drivers/mtd/mtdchar.c 2004-04-03 19:37:37.000000000 -0800
+++ linux-2.6.5mtd/drivers/mtd/mtdchar.c 2004-04-16 15:18:33.000000000 -0700
@@ -12,6 +12,7 @@
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
+#include <linux/device.h>
#include <asm/uaccess.h>

#ifdef CONFIG_DEVFS_FS
@@ -26,6 +27,10 @@ static struct mtd_notifier notifier = {

#endif

+/* For class support */
+static struct class_simple *mtd_class;
+static int mtd_minor;
+
static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
{
struct mtd_info *mtd=(struct mtd_info *)file->private_data;
@@ -473,18 +478,43 @@ static struct file_operations mtd_fops =

static void mtd_notify_add(struct mtd_info* mtd)
{
+ int err = 0;
if (!mtd)
return;
- devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
+ class_simple_device_add(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
+ NULL, "mtd%d", mtd->index);
+ err = devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
- devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
+ if (err) {
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
+ goto out_class;
+ }
+ class_simple_device_add(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
+ NULL, "mtd%dro", mtd->index);
+ err = devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
+ if (err)
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
+ else
+ goto out;
+out_class:
+ class_simple_destroy(mtd_class);
+ unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
+out:
+ /* Need the minor number to be global so the module_exit function can see it*/
+ mtd_minor = mtd->index;
+ return err;
+
}

static void mtd_notify_remove(struct mtd_info* mtd)
{
if (!mtd)
return;
+
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
+ class_simple_destroy(mtd_class);
devfs_remove("mtd/%d", mtd->index);
devfs_remove("mtd/%dro", mtd->index);
}
@@ -492,22 +522,37 @@ static void mtd_notify_remove(struct mtd

static int __init init_mtdchar(void)
{
+ int err = 0;
+
if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
MTD_CHAR_MAJOR);
- return -EAGAIN;
+ err = -EAGAIN;
+ goto out;
+ }
+ mtd_class = class_simple_create(THIS_MODULE, "mtd");
+ if (IS_ERR(mtd_class)) {
+ err = PTR_ERR(mtd_class);
+ goto out_chrdev;
}
-
#ifdef CONFIG_DEVFS_FS
devfs_mk_dir("mtd");

register_mtd_user(&notifier);
#endif
- return 0;
+ goto out;
+
+out_chrdev:
+ unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
+out:
+ return err;
}

static void __exit cleanup_mtdchar(void)
{
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd_minor*2));
+ class_simple_device_remove(MKDEV(MTD_CHAR_MAJOR, mtd_minor*2+1));
+ class_simple_destroy(mtd_class);
#ifdef CONFIG_DEVFS_FS
unregister_mtd_user(&notifier);
devfs_remove("mtd");

2004-04-17 00:09:58

by David Woodhouse

[permalink] [raw]
Subject: Re: [PATCH 2.6.5] Add class support to drivers/mtd/mtdchar.c

On Fri, 2004-04-16 at 15:35 -0700, Hanna Linder wrote:
> Thanks David. I tried the MTDRAM and it showed up in the class tree but
> no dev file was made. I suspect it needs a device attached. Anyway, I found
> a small bug by code inspection so here is the new patch.

MTDRAM is a fake MTD device, using backing store provided by vmalloc().
There is no hardware. If you have mtdram.ko and mtdchar.ko both loaded,
you should have been able to access /dev/mtd0 and you should have seen
it in /proc/mtd

--
dwmw2

2004-04-20 00:22:49

by Hanna Linder

[permalink] [raw]
Subject: Re: [PATCH 2.6.5] Add class support to drivers/mtd/mtdchar.c

--On Friday, April 16, 2004 08:09:58 PM -0400 David Woodhouse <[email protected]> wrote:
>
> MTDRAM is a fake MTD device, using backing store provided by vmalloc().
> There is no hardware. If you have mtdram.ko and mtdchar.ko both loaded,
> you should have been able to access /dev/mtd0 and you should have seen
> it in /proc/mtd

Thanks. I can see /proc/mtd but there is no /dev/mtd0 (or anything mtd in /dev).
And still all I see in sysfs is the mtd class directory no dev file.

Module Size Used by
mtdram 2884 0
mtdchar 5000 0
mtdcore 5760 3 mtdram,mtdchar

I just tried it on a clean 2.6.6-rc1 kernel and the same thing happened. Created
a /proc/mtd but no /dev/mtd*

So either Im doing something wrong or there is a bug of some sort...

Thanks a lot.

Hanna