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/smp_lock.h>
13 #include <linux/notifier.h>
14 #include <linux/mutex.h>
17 #include "../core/hcd.h"
19 static void mon_stop(struct mon_bus *mbus);
20 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
21 static void mon_bus_drop(struct kref *r);
22 static void mon_bus_init(struct usb_bus *ubus);
24 DEFINE_MUTEX(mon_lock);
26 struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
27 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
30 * Link a reader into the bus.
32 * This must be called with mon_lock taken because of mbus->ref.
34 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
39 spin_lock_irqsave(&mbus->lock, flags);
40 if (mbus->nreaders == 0) {
41 if (mbus == &mon_bus0) {
42 list_for_each (p, &mon_buses) {
44 m1 = list_entry(p, struct mon_bus, bus_link);
45 m1->u_bus->monitored = 1;
48 mbus->u_bus->monitored = 1;
52 list_add_tail(&r->r_link, &mbus->r_list);
53 spin_unlock_irqrestore(&mbus->lock, flags);
59 * Unlink reader from the bus.
61 * This is called with mon_lock taken, so we can decrement mbus->ref.
63 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
67 spin_lock_irqsave(&mbus->lock, flags);
70 if (mbus->nreaders == 0)
72 spin_unlock_irqrestore(&mbus->lock, flags);
74 kref_put(&mbus->ref, mon_bus_drop);
79 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
82 struct list_head *pos;
85 spin_lock_irqsave(&mbus->lock, flags);
87 list_for_each (pos, &mbus->r_list) {
88 r = list_entry(pos, struct mon_reader, r_link);
89 r->rnf_submit(r->r_data, urb);
91 spin_unlock_irqrestore(&mbus->lock, flags);
95 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
99 if ((mbus = ubus->mon_bus) != NULL)
100 mon_bus_submit(mbus, urb);
101 mon_bus_submit(&mon_bus0, urb);
106 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
109 struct list_head *pos;
110 struct mon_reader *r;
112 spin_lock_irqsave(&mbus->lock, flags);
114 list_for_each (pos, &mbus->r_list) {
115 r = list_entry(pos, struct mon_reader, r_link);
116 r->rnf_error(r->r_data, urb, error);
118 spin_unlock_irqrestore(&mbus->lock, flags);
122 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
124 struct mon_bus *mbus;
126 if ((mbus = ubus->mon_bus) != NULL)
127 mon_bus_submit_error(mbus, urb, error);
128 mon_bus_submit_error(&mon_bus0, urb, error);
133 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
136 struct list_head *pos;
137 struct mon_reader *r;
139 spin_lock_irqsave(&mbus->lock, flags);
141 list_for_each (pos, &mbus->r_list) {
142 r = list_entry(pos, struct mon_reader, r_link);
143 r->rnf_complete(r->r_data, urb);
145 spin_unlock_irqrestore(&mbus->lock, flags);
148 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
150 struct mon_bus *mbus;
152 mbus = ubus->mon_bus;
155 * This should not happen.
156 * At this point we do not even know the bus number...
158 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
163 mon_bus_complete(mbus, urb);
164 mon_bus_complete(&mon_bus0, urb);
167 /* int (*unlink_urb) (struct urb *urb, int status); */
172 static void mon_stop(struct mon_bus *mbus)
174 struct usb_bus *ubus = mbus->u_bus;
177 if (mbus == &mon_bus0) {
178 list_for_each (p, &mon_buses) {
179 mbus = list_entry(p, struct mon_bus, bus_link);
181 * We do not change nreaders here, so rely on mon_lock.
183 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
188 * A stop can be called for a dissolved mon_bus in case of
189 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
191 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
199 * Add a USB bus (usually by a modprobe foo-hcd)
201 * This does not return an error code because the core cannot care less
202 * if monitoring is not established.
204 static void mon_bus_add(struct usb_bus *ubus)
207 mutex_lock(&mon_lock);
208 if (mon_bus0.nreaders != 0)
210 mutex_unlock(&mon_lock);
214 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
216 static void mon_bus_remove(struct usb_bus *ubus)
218 struct mon_bus *mbus = ubus->mon_bus;
220 mutex_lock(&mon_lock);
221 list_del(&mbus->bus_link);
222 if (mbus->text_inited)
225 mon_dissolve(mbus, ubus);
226 kref_put(&mbus->ref, mon_bus_drop);
227 mutex_unlock(&mon_lock);
230 static int mon_notify(struct notifier_block *self, unsigned long action,
243 static struct notifier_block mon_nb = {
244 .notifier_call = mon_notify,
250 static struct usb_mon_operations mon_ops_0 = {
251 .urb_submit = mon_submit,
252 .urb_submit_error = mon_submit_error,
253 .urb_complete = mon_complete,
257 * Tear usb_bus and mon_bus apart.
259 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
262 if (ubus->monitored) {
267 ubus->mon_bus = NULL;
271 /* We want synchronize_irq() here, but that needs an argument. */
276 static void mon_bus_drop(struct kref *r)
278 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
283 * Initialize a bus for us:
285 * - refcount USB bus struct
288 static void mon_bus_init(struct usb_bus *ubus)
290 struct mon_bus *mbus;
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;
305 mbus->text_inited = mon_text_add(mbus, ubus->busnum);
308 mutex_lock(&mon_lock);
309 list_add_tail(&mbus->bus_link, &mon_buses);
310 mutex_unlock(&mon_lock);
317 static void mon_bus0_init(void)
319 struct mon_bus *mbus = &mon_bus0;
321 kref_init(&mbus->ref);
322 spin_lock_init(&mbus->lock);
323 INIT_LIST_HEAD(&mbus->r_list);
325 mbus->text_inited = mon_text_add(mbus, 0);
326 // mbus->bin_inited = mon_bin_add(mbus, 0);
330 * Search a USB bus by number. Notice that USB bus numbers start from one,
331 * which we may later use to identify "all" with zero.
333 * This function must be called with mon_lock held.
335 * This is obviously inefficient and may be revised in the future.
337 struct mon_bus *mon_bus_lookup(unsigned int num)
340 struct mon_bus *mbus;
345 list_for_each (p, &mon_buses) {
346 mbus = list_entry(p, struct mon_bus, bus_link);
347 if (mbus->u_bus->busnum == num) {
354 static int __init mon_init(void)
356 struct usb_bus *ubus;
359 if ((rc = mon_text_init()) != 0)
361 if ((rc = mon_bin_init()) != 0)
366 if (usb_mon_register(&mon_ops_0) != 0) {
367 printk(KERN_NOTICE TAG ": unable to register with the core\n");
371 // MOD_INC_USE_COUNT(which_module?);
373 usb_register_notify(&mon_nb);
375 mutex_lock(&usb_bus_list_lock);
376 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
379 mutex_unlock(&usb_bus_list_lock);
390 static void __exit mon_exit(void)
392 struct mon_bus *mbus;
395 usb_unregister_notify(&mon_nb);
396 usb_mon_deregister();
398 mutex_lock(&mon_lock);
400 while (!list_empty(&mon_buses)) {
402 mbus = list_entry(p, struct mon_bus, bus_link);
405 if (mbus->text_inited)
409 * This never happens, because the open/close paths in
410 * file level maintain module use counters and so rmmod fails
411 * before reaching here. However, better be safe...
413 if (mbus->nreaders) {
415 ": Outstanding opens (%d) on usb%d, leaking...\n",
416 mbus->nreaders, mbus->u_bus->busnum);
417 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
420 mon_dissolve(mbus, mbus->u_bus);
421 kref_put(&mbus->ref, mon_bus_drop);
425 if (mbus->text_inited)
428 mutex_unlock(&mon_lock);
434 module_init(mon_init);
435 module_exit(mon_exit);
437 MODULE_LICENSE("GPL");