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 error)
119 struct mon_bus *mbus;
121 struct list_head *pos;
122 struct mon_reader *r;
124 mbus = ubus->mon_bus;
128 spin_lock_irqsave(&mbus->lock, flags);
129 if (mbus->nreaders == 0)
133 list_for_each (pos, &mbus->r_list) {
134 r = list_entry(pos, struct mon_reader, r_link);
135 r->rnf_error(r->r_data, urb, error);
138 spin_unlock_irqrestore(&mbus->lock, flags);
142 spin_unlock_irqrestore(&mbus->lock, flags);
149 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
151 struct mon_bus *mbus;
153 struct list_head *pos;
154 struct mon_reader *r;
156 mbus = ubus->mon_bus;
159 * This should not happen.
160 * At this point we do not even know the bus number...
162 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
167 spin_lock_irqsave(&mbus->lock, flags);
169 list_for_each (pos, &mbus->r_list) {
170 r = list_entry(pos, struct mon_reader, r_link);
171 r->rnf_complete(r->r_data, urb);
173 spin_unlock_irqrestore(&mbus->lock, flags);
176 /* int (*unlink_urb) (struct urb *urb, int status); */
181 static void mon_stop(struct mon_bus *mbus)
183 struct usb_bus *ubus = mbus->u_bus;
186 * A stop can be called for a dissolved mon_bus in case of
187 * a reader staying across an rmmod foo_hcd.
196 * Add a USB bus (usually by a modprobe foo-hcd)
198 * This does not return an error code because the core cannot care less
199 * if monitoring is not established.
201 static void mon_bus_add(struct usb_bus *ubus)
203 mon_bus_init(mon_dir, ubus);
207 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
209 static void mon_bus_remove(struct usb_bus *ubus)
211 struct mon_bus *mbus = ubus->mon_bus;
213 mutex_lock(&mon_lock);
214 list_del(&mbus->bus_link);
215 debugfs_remove(mbus->dent_t);
216 debugfs_remove(mbus->dent_s);
218 mon_dissolve(mbus, ubus);
219 kref_put(&mbus->ref, mon_bus_drop);
220 mutex_unlock(&mon_lock);
223 static int mon_notify(struct notifier_block *self, unsigned long action,
236 static struct notifier_block mon_nb = {
237 .notifier_call = mon_notify,
243 static struct usb_mon_operations mon_ops_0 = {
244 .urb_submit = mon_submit,
245 .urb_submit_error = mon_submit_error,
246 .urb_complete = mon_complete,
250 * Tear usb_bus and mon_bus apart.
252 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
256 * Never happens, but...
258 if (ubus->monitored) {
259 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
265 ubus->mon_bus = NULL;
272 static void mon_bus_drop(struct kref *r)
274 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
279 * Initialize a bus for us:
281 * - refcount USB bus struct
284 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
287 struct mon_bus *mbus;
288 enum { NAMESZ = 10 };
292 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
294 kref_init(&mbus->ref);
295 spin_lock_init(&mbus->lock);
296 INIT_LIST_HEAD(&mbus->r_list);
299 * We don't need to take a reference to ubus, because we receive
300 * a notification if the bus is about to be removed.
303 ubus->mon_bus = mbus;
304 mbus->uses_dma = ubus->uses_dma;
306 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
307 if (rc <= 0 || rc >= NAMESZ)
309 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
314 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
315 if (rc <= 0 || rc >= NAMESZ)
317 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
322 mutex_lock(&mon_lock);
323 list_add_tail(&mbus->bus_link, &mon_buses);
324 mutex_unlock(&mon_lock);
329 debugfs_remove(mbus->dent_t);
337 static int __init mon_init(void)
339 struct usb_bus *ubus;
340 struct dentry *mondir;
342 mondir = debugfs_create_dir("usbmon", NULL);
343 if (IS_ERR(mondir)) {
344 printk(KERN_NOTICE TAG ": debugfs is not available\n");
347 if (mondir == NULL) {
348 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
353 if (usb_mon_register(&mon_ops_0) != 0) {
354 printk(KERN_NOTICE TAG ": unable to register with the core\n");
355 debugfs_remove(mondir);
358 // MOD_INC_USE_COUNT(which_module?);
360 usb_register_notify(&mon_nb);
362 mutex_lock(&usb_bus_list_lock);
363 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
364 mon_bus_init(mondir, ubus);
366 mutex_unlock(&usb_bus_list_lock);
370 static void __exit mon_exit(void)
372 struct mon_bus *mbus;
375 usb_unregister_notify(&mon_nb);
376 usb_mon_deregister();
378 mutex_lock(&mon_lock);
379 while (!list_empty(&mon_buses)) {
381 mbus = list_entry(p, struct mon_bus, bus_link);
384 debugfs_remove(mbus->dent_t);
385 debugfs_remove(mbus->dent_s);
388 * This never happens, because the open/close paths in
389 * file level maintain module use counters and so rmmod fails
390 * before reaching here. However, better be safe...
392 if (mbus->nreaders) {
394 ": Outstanding opens (%d) on usb%d, leaking...\n",
395 mbus->nreaders, mbus->u_bus->busnum);
396 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
399 mon_dissolve(mbus, mbus->u_bus);
400 kref_put(&mbus->ref, mon_bus_drop);
402 mutex_unlock(&mon_lock);
404 debugfs_remove(mon_dir);
407 module_init(mon_init);
408 module_exit(mon_exit);
410 MODULE_LICENSE("GPL");