usbmon: Add class for the binary API

Pete Zaitcev <[email protected]>
Newsgroups gmane.linux.usb.devel
Organization Red Hat, Inc.
Message-ID <[email protected]>
This patch adds a class which allows for an easier integration with udev.

Signed-off-by: Paolo Abeni <[email protected]>
Signed-off-by: Pete Zaitcev <[email protected]>

---

Paolo, I touched up two places:
 - class_create returns ERR_PTR on error, not NULL
 - I prefer a LIFO order of construction/destruction in case of
   lifetime issues

Otherwise, it's your patch. I verified with double-diffing.
But make sure to re-apply and re-test, please.

diff -urp -X dontdiff linux-2.6.21-rc1-gregkh-mon/drivers/usb/mon/mon_bin.c linux-2.6.20-mon/drivers/usb/mon/mon_bin.c
--- linux-2.6.21-rc1-gregkh-mon/drivers/usb/mon/mon_bin.c	2007-02-24 18:00:29.000000000 -0800
+++ linux-2.6.20-mon/drivers/usb/mon/mon_bin.c	2007-02-24 18:56:56.000000000 -0800
@@ -172,6 +201,7 @@ static inline struct mon_bin_hdr *MON_OF
 
 #define MON_RING_EMPTY(rp)	((rp)->b_cnt == 0)
 
+static struct class *mon_bin_class;
 static dev_t mon_bin_dev0;
 static struct cdev mon_bin_cdev;
 
@@ -1142,10 +1243,35 @@ static void mon_free_buff(struct mon_pgm
 		free_page((unsigned long) map[n].ptr);
 }
 
+int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
+{
+	struct device *dev;
+	unsigned minor = ubus->busnum;
+
+	if (minor >= MON_BIN_MAX_MINOR)
+		return 0;
+
+	dev = device_create(mon_bin_class, NULL,
+			MKDEV(MAJOR(mon_bin_dev0), minor), "usbmon%d", minor);
+	return !IS_ERR(dev);
+}
+
+void mon_bin_del(struct mon_bus *mbus)
+{
+	unsigned minor = mbus->u_bus->busnum;
+	device_destroy(mon_bin_class, MKDEV(MAJOR(mon_bin_dev0), minor));
+}
+
 int __init mon_bin_init(void)
 {
 	int rc;
 
+	mon_bin_class = class_create(THIS_MODULE, "usbmon");
+	if (IS_ERR(mon_bin_class)) {
+		rc = PTR_ERR(mon_bin_class);
+		goto err_class;
+	}
+
 	rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
 	if (rc < 0)
 		goto err_dev;
@@ -1162,6 +1288,8 @@ int __init mon_bin_init(void)
 err_add:
 	unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
 err_dev:
+	class_destroy(mon_bin_class);
+err_class:
 	return rc;
 }
 
@@ -1169,4 +1297,5 @@ void mon_bin_exit(void)
 {
 	cdev_del(&mon_bin_cdev);
 	unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
+	class_destroy(mon_bin_class);
 }
diff -urp -X dontdiff linux-2.6.21-rc1-gregkh-mon/drivers/usb/mon/mon_main.c linux-2.6.20-mon/drivers/usb/mon/mon_main.c
--- linux-2.6.21-rc1-gregkh-mon/drivers/usb/mon/mon_main.c	2007-02-24 17:31:38.000000000 -0800
+++ linux-2.6.20-mon/drivers/usb/mon/mon_main.c	2007-02-23 17:07:29.000000000 -0800
@@ -212,6 +212,8 @@ static void mon_bus_remove(struct usb_bu
 	list_del(&mbus->bus_link);
 	if (mbus->text_inited)
 		mon_text_del(mbus);
+	if (mbus->bin_inited)
+		mon_bin_del(mbus);
 
 	mon_dissolve(mbus, ubus);
 	kref_put(&mbus->ref, mon_bus_drop);
@@ -298,7 +300,7 @@ static void mon_bus_init(struct usb_bus 
 	mbus->uses_dma = ubus->uses_dma;
 
 	mbus->text_inited = mon_text_add(mbus, ubus);
-	// mon_bin_add(...)
+	mbus->bin_inited = mon_bin_add(mbus, ubus);
 
 	mutex_lock(&mon_lock);
 	list_add_tail(&mbus->bus_link, &mon_buses);
@@ -381,6 +383,8 @@ static void __exit mon_exit(void)
 
 		if (mbus->text_inited)
 			mon_text_del(mbus);
+		if (mbus->bin_inited)
+			mon_bin_del(mbus);
 
 		/*
 		 * This never happens, because the open/close paths in
diff -urp -X dontdiff linux-2.6.21-rc1-gregkh-mon/drivers/usb/mon/usb_mon.h linux-2.6.20-mon/drivers/usb/mon/usb_mon.h
--- linux-2.6.21-rc1-gregkh-mon/drivers/usb/mon/usb_mon.h	2007-02-24 18:00:30.000000000 -0800
+++ linux-2.6.20-mon/drivers/usb/mon/usb_mon.h	2007-02-23 17:07:29.000000000 -0800
@@ -20,6 +20,7 @@ struct mon_bus {
 	struct usb_bus *u_bus;
 
 	int text_inited;
+	int bin_inited;
 	struct dentry *dent_s;		/* Debugging file */
 	struct dentry *dent_t;		/* Text interface file */
 	int uses_dma;
@@ -54,7 +56,8 @@ struct mon_bus *mon_bus_lookup(unsigned 
 
 int /*bool*/ mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus);
 void mon_text_del(struct mon_bus *mbus);
-// void mon_bin_add(struct mon_bus *);
+int /*bool*/ mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus);
+void mon_bin_del(struct mon_bus *mbus);
 
 int __init mon_text_init(void);
 void mon_text_exit(void);

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.