2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * mon_main.c: Main file, module initiation and exit, registrations, etc.
6 * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/debugfs.h>
13 #include <linux/smp_lock.h>
14 #include <linux/notifier.h>
15 #include <linux/mutex.h>
18 #include "../core/hcd.h"
20 static void mon_submit(struct usb_bus *ubus, struct urb *urb);
21 static void mon_complete(struct usb_bus *ubus, struct urb *urb);
22 static void mon_stop(struct mon_bus *mbus);
23 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
24 static void mon_bus_drop(struct kref *r);
25 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
27 DEFINE_MUTEX(mon_lock);
29 static struct dentry *mon_dir; /* /dbg/usbmon */
30 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
33 * Link a reader into the bus.
35 * This must be called with mon_lock taken because of mbus->ref.
37 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
42 spin_lock_irqsave(&mbus->lock, flags);
43 if (mbus->nreaders == 0) {
45 if (ubus->monitored) {
47 * Something is really broken, refuse to go on and
48 * possibly corrupt ops pointers or worse.
50 printk(KERN_ERR TAG ": bus %d is already monitored\n",
52 spin_unlock_irqrestore(&mbus->lock, flags);
58 list_add_tail(&r->r_link, &mbus->r_list);
59 spin_unlock_irqrestore(&mbus->lock, flags);
65 * Unlink reader from the bus.
67 * This is called with mon_lock taken, so we can decrement mbus->ref.
69 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
73 spin_lock_irqsave(&mbus->lock, flags);
76 if (mbus->nreaders == 0)
78 spin_unlock_irqrestore(&mbus->lock, flags);
80 kref_put(&mbus->ref, mon_bus_drop);
85 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
89 struct list_head *pos;
96 spin_lock_irqsave(&mbus->lock, flags);
97 if (mbus->nreaders == 0)
101 list_for_each (pos, &mbus->r_list) {
102 r = list_entry(pos, struct mon_reader, r_link);
103 r->rnf_submit(r->r_data, urb);
106 spin_unlock_irqrestore(&mbus->lock, flags);
110 spin_unlock_irqrestore(&mbus->lock, flags);
117 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
119 struct mon_bus *mbus;
121 mbus = ubus->mon_bus;
126 * XXX Capture the error code and the 'E' event.
137 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
139 struct mon_bus *mbus;
141 struct list_head *pos;
142 struct mon_reader *r;
144 mbus = ubus->mon_bus;
147 * This should not happen.
148 * At this point we do not even know the bus number...
150 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
155 spin_lock_irqsave(&mbus->lock, flags);
157 list_for_each (pos, &mbus->r_list) {
158 r = list_entry(pos, struct mon_reader, r_link);
159 r->rnf_complete(r->r_data, urb);
161 spin_unlock_irqrestore(&mbus->lock, flags);
164 /* int (*unlink_urb) (struct urb *urb, int status); */
169 static void mon_stop(struct mon_bus *mbus)
171 struct usb_bus *ubus = mbus->u_bus;
174 * A stop can be called for a dissolved mon_bus in case of
175 * a reader staying across an rmmod foo_hcd.
184 * Add a USB bus (usually by a modprobe foo-hcd)
186 * This does not return an error code because the core cannot care less
187 * if monitoring is not established.
189 static void mon_bus_add(struct usb_bus *ubus)
191 mon_bus_init(mon_dir, ubus);
195 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
197 static void mon_bus_remove(struct usb_bus *ubus)
199 struct mon_bus *mbus = ubus->mon_bus;
201 mutex_lock(&mon_lock);
202 list_del(&mbus->bus_link);
203 debugfs_remove(mbus->dent_t);
204 debugfs_remove(mbus->dent_s);
206 mon_dissolve(mbus, ubus);
207 kref_put(&mbus->ref, mon_bus_drop);
208 mutex_unlock(&mon_lock);
211 static int mon_notify(struct notifier_block *self, unsigned long action,
224 static struct notifier_block mon_nb = {
225 .notifier_call = mon_notify,
231 static struct usb_mon_operations mon_ops_0 = {
232 .urb_submit = mon_submit,
233 .urb_submit_error = mon_submit_error,
234 .urb_complete = mon_complete,
238 * Tear usb_bus and mon_bus apart.
240 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
244 * Never happens, but...
246 if (ubus->monitored) {
247 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
253 ubus->mon_bus = NULL;
256 // usb_bus_put(ubus);
261 static void mon_bus_drop(struct kref *r)
263 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
268 * Initialize a bus for us:
270 * - refcount USB bus struct
273 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
276 struct mon_bus *mbus;
277 enum { NAMESZ = 10 };
281 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
283 kref_init(&mbus->ref);
284 spin_lock_init(&mbus->lock);
285 INIT_LIST_HEAD(&mbus->r_list);
288 * This usb_bus_get here is superfluous, because we receive
289 * a notification if usb_bus is about to be removed.
291 // usb_bus_get(ubus);
293 ubus->mon_bus = mbus;
295 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
296 if (rc <= 0 || rc >= NAMESZ)
298 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
303 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
304 if (rc <= 0 || rc >= NAMESZ)
306 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
311 mutex_lock(&mon_lock);
312 list_add_tail(&mbus->bus_link, &mon_buses);
313 mutex_unlock(&mon_lock);
318 debugfs_remove(mbus->dent_t);
326 static int __init mon_init(void)
328 struct usb_bus *ubus;
329 struct dentry *mondir;
331 mondir = debugfs_create_dir("usbmon", NULL);
332 if (IS_ERR(mondir)) {
333 printk(KERN_NOTICE TAG ": debugfs is not available\n");
336 if (mondir == NULL) {
337 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
342 if (usb_mon_register(&mon_ops_0) != 0) {
343 printk(KERN_NOTICE TAG ": unable to register with the core\n");
344 debugfs_remove(mondir);
347 // MOD_INC_USE_COUNT(which_module?);
349 usb_register_notify(&mon_nb);
351 mutex_lock(&usb_bus_list_lock);
352 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
353 mon_bus_init(mondir, ubus);
355 mutex_unlock(&usb_bus_list_lock);
359 static void __exit mon_exit(void)
361 struct mon_bus *mbus;
364 usb_unregister_notify(&mon_nb);
365 usb_mon_deregister();
367 mutex_lock(&mon_lock);
368 while (!list_empty(&mon_buses)) {
370 mbus = list_entry(p, struct mon_bus, bus_link);
373 debugfs_remove(mbus->dent_t);
374 debugfs_remove(mbus->dent_s);
377 * This never happens, because the open/close paths in
378 * file level maintain module use counters and so rmmod fails
379 * before reaching here. However, better be safe...
381 if (mbus->nreaders) {
383 ": Outstanding opens (%d) on usb%d, leaking...\n",
384 mbus->nreaders, mbus->u_bus->busnum);
385 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
388 mon_dissolve(mbus, mbus->u_bus);
389 kref_put(&mbus->ref, mon_bus_drop);
391 mutex_unlock(&mon_lock);
393 debugfs_remove(mon_dir);
396 module_init(mon_init);
397 module_exit(mon_exit);
399 MODULE_LICENSE("GPL");