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>
17 #include "../core/hcd.h"
19 static void mon_submit(struct usb_bus *ubus, struct urb *urb);
20 static void mon_complete(struct usb_bus *ubus, struct urb *urb);
21 static void mon_stop(struct mon_bus *mbus);
22 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
23 static void mon_bus_drop(struct kref *r);
24 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
26 DECLARE_MUTEX(mon_lock);
28 static struct dentry *mon_dir; /* /dbg/usbmon */
29 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
32 * Link a reader into the bus.
34 * This must be called with mon_lock taken because of mbus->ref.
36 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
41 spin_lock_irqsave(&mbus->lock, flags);
42 if (mbus->nreaders == 0) {
44 if (ubus->monitored) {
46 * Something is really broken, refuse to go on and
47 * possibly corrupt ops pointers or worse.
49 printk(KERN_ERR TAG ": bus %d is already monitored\n",
51 spin_unlock_irqrestore(&mbus->lock, flags);
57 list_add_tail(&r->r_link, &mbus->r_list);
58 spin_unlock_irqrestore(&mbus->lock, flags);
64 * Unlink reader from the bus.
66 * This is called with mon_lock taken, so we can decrement mbus->ref.
68 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
72 spin_lock_irqsave(&mbus->lock, flags);
75 if (mbus->nreaders == 0)
77 spin_unlock_irqrestore(&mbus->lock, flags);
79 kref_put(&mbus->ref, mon_bus_drop);
84 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
88 struct list_head *pos;
95 spin_lock_irqsave(&mbus->lock, flags);
96 if (mbus->nreaders == 0)
99 list_for_each (pos, &mbus->r_list) {
100 r = list_entry(pos, struct mon_reader, r_link);
101 r->rnf_submit(r->r_data, urb);
104 spin_unlock_irqrestore(&mbus->lock, flags);
108 spin_unlock_irqrestore(&mbus->lock, flags);
115 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
117 struct mon_bus *mbus;
119 mbus = ubus->mon_bus;
124 * XXX Capture the error code and the 'E' event.
135 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
137 struct mon_bus *mbus;
139 struct list_head *pos;
140 struct mon_reader *r;
142 mbus = ubus->mon_bus;
145 * This should not happen.
146 * At this point we do not even know the bus number...
148 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
153 spin_lock_irqsave(&mbus->lock, flags);
154 list_for_each (pos, &mbus->r_list) {
155 r = list_entry(pos, struct mon_reader, r_link);
156 r->rnf_complete(r->r_data, urb);
158 spin_unlock_irqrestore(&mbus->lock, flags);
161 /* int (*unlink_urb) (struct urb *urb, int status); */
165 * Obviously this must be well locked, so no need to play with mb's.
167 static void mon_stop(struct mon_bus *mbus)
169 struct usb_bus *ubus = mbus->u_bus;
172 * A stop can be called for a dissolved mon_bus in case of
173 * a reader staying across an rmmod foo_hcd.
182 * Add a USB bus (usually by a modprobe foo-hcd)
184 * This does not return an error code because the core cannot care less
185 * if monitoring is not established.
187 static void mon_bus_add(struct usb_bus *ubus)
189 mon_bus_init(mon_dir, ubus);
193 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
195 static void mon_bus_remove(struct usb_bus *ubus)
197 struct mon_bus *mbus = ubus->mon_bus;
200 list_del(&mbus->bus_link);
201 debugfs_remove(mbus->dent_t);
202 debugfs_remove(mbus->dent_s);
204 mon_dissolve(mbus, ubus);
205 kref_put(&mbus->ref, mon_bus_drop);
209 static int mon_notify(struct notifier_block *self, unsigned long action,
222 static struct notifier_block mon_nb = {
223 .notifier_call = mon_notify,
229 static struct usb_mon_operations mon_ops_0 = {
230 .urb_submit = mon_submit,
231 .urb_submit_error = mon_submit_error,
232 .urb_complete = mon_complete,
236 * Tear usb_bus and mon_bus apart.
238 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
242 * Never happens, but...
244 if (ubus->monitored) {
245 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
251 ubus->mon_bus = NULL;
254 // usb_bus_put(ubus);
259 static void mon_bus_drop(struct kref *r)
261 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
266 * Initialize a bus for us:
268 * - refcount USB bus struct
271 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
274 struct mon_bus *mbus;
275 enum { NAMESZ = 10 };
279 if ((mbus = kmalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
281 memset(mbus, 0, sizeof(struct mon_bus));
282 kref_init(&mbus->ref);
283 spin_lock_init(&mbus->lock);
284 INIT_LIST_HEAD(&mbus->r_list);
287 * This usb_bus_get here is superfluous, because we receive
288 * a notification if usb_bus is about to be removed.
290 // usb_bus_get(ubus);
292 ubus->mon_bus = mbus;
294 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
295 if (rc <= 0 || rc >= NAMESZ)
297 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
302 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
303 if (rc <= 0 || rc >= NAMESZ)
305 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
311 list_add_tail(&mbus->bus_link, &mon_buses);
317 debugfs_remove(mbus->dent_t);
325 static int __init mon_init(void)
327 struct usb_bus *ubus;
328 struct dentry *mondir;
330 mondir = debugfs_create_dir("usbmon", NULL);
331 if (IS_ERR(mondir)) {
332 printk(KERN_NOTICE TAG ": debugfs is not available\n");
335 if (mondir == NULL) {
336 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
341 if (usb_mon_register(&mon_ops_0) != 0) {
342 printk(KERN_NOTICE TAG ": unable to register with the core\n");
343 debugfs_remove(mondir);
346 // MOD_INC_USE_COUNT(which_module?);
348 usb_register_notify(&mon_nb);
350 down(&usb_bus_list_lock);
351 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
352 mon_bus_init(mondir, ubus);
354 up(&usb_bus_list_lock);
358 static void __exit mon_exit(void)
360 struct mon_bus *mbus;
363 usb_unregister_notify(&mon_nb);
364 usb_mon_deregister();
367 while (!list_empty(&mon_buses)) {
369 mbus = list_entry(p, struct mon_bus, bus_link);
372 debugfs_remove(mbus->dent_t);
373 debugfs_remove(mbus->dent_s);
376 * This never happens, because the open/close paths in
377 * file level maintain module use counters and so rmmod fails
378 * before reaching here. However, better be safe...
380 if (mbus->nreaders) {
382 ": Outstanding opens (%d) on usb%d, leaking...\n",
383 mbus->nreaders, mbus->u_bus->busnum);
384 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
387 mon_dissolve(mbus, mbus->u_bus);
388 kref_put(&mbus->ref, mon_bus_drop);
392 debugfs_remove(mon_dir);
395 module_init(mon_init);
396 module_exit(mon_exit);
398 MODULE_LICENSE("GPL");