Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usbmon: Add class for binary interface

Add a class which allows for an easier integration with udev.

This code was originally written by Paolo Abeni, and arrived to my tree
as a part of big patch to add binary API on December 18. As I understand,
Paolo always meant the class to be a part of the whole thing. This is his
udev rule to go along with the patch:

KERNEL=="usbmon[0-9]*", NAME="usbmon%n", MODE="0440",OWNER="root",GROUP="bin"

Signed-off-by: Pete Zaitcev <zaitcev@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>


authored by

Pete Zaitcev and committed by
Greg Kroah-Hartman
ce7cd137 49cdee0e

+66 -18
+33 -1
drivers/usb/mon/mon_bin.c
··· 4 4 * This is a binary format reader. 5 5 * 6 6 * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it) 7 - * Copyright (C) 2006 Pete Zaitcev (zaitcev@redhat.com) 7 + * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com) 8 8 */ 9 9 10 10 #include <linux/kernel.h> ··· 172 172 173 173 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0) 174 174 175 + static struct class *mon_bin_class; 175 176 static dev_t mon_bin_dev0; 176 177 static struct cdev mon_bin_cdev; 177 178 ··· 1145 1144 free_page((unsigned long) map[n].ptr); 1146 1145 } 1147 1146 1147 + int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus) 1148 + { 1149 + struct device *dev; 1150 + unsigned minor = ubus? ubus->busnum: 0; 1151 + 1152 + if (minor >= MON_BIN_MAX_MINOR) 1153 + return 0; 1154 + 1155 + dev = device_create(mon_bin_class, ubus? ubus->controller: NULL, 1156 + MKDEV(MAJOR(mon_bin_dev0), minor), "usbmon%d", minor); 1157 + if (IS_ERR(dev)) 1158 + return 0; 1159 + 1160 + mbus->classdev = dev; 1161 + return 1; 1162 + } 1163 + 1164 + void mon_bin_del(struct mon_bus *mbus) 1165 + { 1166 + device_destroy(mon_bin_class, mbus->classdev->devt); 1167 + } 1168 + 1148 1169 int __init mon_bin_init(void) 1149 1170 { 1150 1171 int rc; 1172 + 1173 + mon_bin_class = class_create(THIS_MODULE, "usbmon"); 1174 + if (IS_ERR(mon_bin_class)) { 1175 + rc = PTR_ERR(mon_bin_class); 1176 + goto err_class; 1177 + } 1151 1178 1152 1179 rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon"); 1153 1180 if (rc < 0) ··· 1193 1164 err_add: 1194 1165 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR); 1195 1166 err_dev: 1167 + class_destroy(mon_bin_class); 1168 + err_class: 1196 1169 return rc; 1197 1170 } 1198 1171 ··· 1202 1171 { 1203 1172 cdev_del(&mon_bin_cdev); 1204 1173 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR); 1174 + class_destroy(mon_bin_class); 1205 1175 }
+10 -4
drivers/usb/mon/mon_main.c
··· 220 220 list_del(&mbus->bus_link); 221 221 if (mbus->text_inited) 222 222 mon_text_del(mbus); 223 + if (mbus->bin_inited) 224 + mon_bin_del(mbus); 223 225 224 226 mon_dissolve(mbus, ubus); 225 227 kref_put(&mbus->ref, mon_bus_drop); ··· 303 301 mbus->u_bus = ubus; 304 302 ubus->mon_bus = mbus; 305 303 306 - mbus->text_inited = mon_text_add(mbus, ubus->busnum); 307 - // mon_bin_add(...) 304 + mbus->text_inited = mon_text_add(mbus, ubus); 305 + mbus->bin_inited = mon_bin_add(mbus, ubus); 308 306 309 307 mutex_lock(&mon_lock); 310 308 list_add_tail(&mbus->bus_link, &mon_buses); ··· 323 321 spin_lock_init(&mbus->lock); 324 322 INIT_LIST_HEAD(&mbus->r_list); 325 323 326 - mbus->text_inited = mon_text_add(mbus, 0); 327 - // mbus->bin_inited = mon_bin_add(mbus, 0); 324 + mbus->text_inited = mon_text_add(mbus, NULL); 325 + mbus->bin_inited = mon_bin_add(mbus, NULL); 328 326 } 329 327 330 328 /* ··· 405 403 406 404 if (mbus->text_inited) 407 405 mon_text_del(mbus); 406 + if (mbus->bin_inited) 407 + mon_bin_del(mbus); 408 408 409 409 /* 410 410 * This never happens, because the open/close paths in ··· 427 423 mbus = &mon_bus0; 428 424 if (mbus->text_inited) 429 425 mon_text_del(mbus); 426 + if (mbus->bin_inited) 427 + mon_bin_del(mbus); 430 428 431 429 mutex_unlock(&mon_lock); 432 430
+18 -11
drivers/usb/mon/mon_text.c
··· 655 655 .release = mon_text_release, 656 656 }; 657 657 658 - int mon_text_add(struct mon_bus *mbus, int busnum) 658 + int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus) 659 659 { 660 660 struct dentry *d; 661 661 enum { NAMESZ = 10 }; 662 662 char name[NAMESZ]; 663 + int busnum = ubus? ubus->busnum: 0; 663 664 int rc; 664 665 665 - rc = snprintf(name, NAMESZ, "%dt", busnum); 666 - if (rc <= 0 || rc >= NAMESZ) 667 - goto err_print_t; 668 - d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t); 669 - if (d == NULL) 670 - goto err_create_t; 671 - mbus->dent_t = d; 666 + if (ubus != NULL) { 667 + rc = snprintf(name, NAMESZ, "%dt", busnum); 668 + if (rc <= 0 || rc >= NAMESZ) 669 + goto err_print_t; 670 + d = debugfs_create_file(name, 0600, mon_dir, mbus, 671 + &mon_fops_text_t); 672 + if (d == NULL) 673 + goto err_create_t; 674 + mbus->dent_t = d; 675 + } 672 676 673 677 rc = snprintf(name, NAMESZ, "%du", busnum); 674 678 if (rc <= 0 || rc >= NAMESZ) ··· 698 694 mbus->dent_u = NULL; 699 695 err_create_u: 700 696 err_print_u: 701 - debugfs_remove(mbus->dent_t); 702 - mbus->dent_t = NULL; 697 + if (ubus != NULL) { 698 + debugfs_remove(mbus->dent_t); 699 + mbus->dent_t = NULL; 700 + } 703 701 err_create_t: 704 702 err_print_t: 705 703 return 0; ··· 710 704 void mon_text_del(struct mon_bus *mbus) 711 705 { 712 706 debugfs_remove(mbus->dent_u); 713 - debugfs_remove(mbus->dent_t); 707 + if (mbus->dent_t != NULL) 708 + debugfs_remove(mbus->dent_t); 714 709 debugfs_remove(mbus->dent_s); 715 710 } 716 711
+5 -2
drivers/usb/mon/usb_mon.h
··· 20 20 struct usb_bus *u_bus; 21 21 22 22 int text_inited; 23 + int bin_inited; 23 24 struct dentry *dent_s; /* Debugging file */ 24 25 struct dentry *dent_t; /* Text interface file */ 25 26 struct dentry *dent_u; /* Second text interface file */ 27 + struct device *classdev; /* Device in usbmon class */ 26 28 27 29 /* Ref */ 28 30 int nreaders; /* Under mon_lock AND mbus->lock */ ··· 54 52 55 53 struct mon_bus *mon_bus_lookup(unsigned int num); 56 54 57 - int /*bool*/ mon_text_add(struct mon_bus *mbus, int busnum); 55 + int /*bool*/ mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus); 58 56 void mon_text_del(struct mon_bus *mbus); 59 - // void mon_bin_add(struct mon_bus *); 57 + int /*bool*/ mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus); 58 + void mon_bin_del(struct mon_bus *mbus); 60 59 61 60 int __init mon_text_init(void); 62 61 void mon_text_exit(void);