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/notifier.h>
13 #include <linux/mutex.h>
16 #include "../core/hcd.h"
18 static void mon_stop(struct mon_bus *mbus);
19 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
20 static void mon_bus_drop(struct kref *r);
21 static void mon_bus_init(struct usb_bus *ubus);
23 DEFINE_MUTEX(mon_lock);
25 struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
26 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
29 * Link a reader into the bus.
31 * This must be called with mon_lock taken because of mbus->ref.
33 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
38 spin_lock_irqsave(&mbus->lock, flags);
39 if (mbus->nreaders == 0) {
40 if (mbus == &mon_bus0) {
41 list_for_each (p, &mon_buses) {
43 m1 = list_entry(p, struct mon_bus, bus_link);
44 m1->u_bus->monitored = 1;
47 mbus->u_bus->monitored = 1;
51 list_add_tail(&r->r_link, &mbus->r_list);
52 spin_unlock_irqrestore(&mbus->lock, flags);
58 * Unlink reader from the bus.
60 * This is called with mon_lock taken, so we can decrement mbus->ref.
62 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
66 spin_lock_irqsave(&mbus->lock, flags);
69 if (mbus->nreaders == 0)
71 spin_unlock_irqrestore(&mbus->lock, flags);
73 kref_put(&mbus->ref, mon_bus_drop);
78 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
81 struct list_head *pos;
84 spin_lock_irqsave(&mbus->lock, flags);
86 list_for_each (pos, &mbus->r_list) {
87 r = list_entry(pos, struct mon_reader, r_link);
88 r->rnf_submit(r->r_data, urb);
90 spin_unlock_irqrestore(&mbus->lock, flags);
94 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
98 if ((mbus = ubus->mon_bus) != NULL)
99 mon_bus_submit(mbus, urb);
100 mon_bus_submit(&mon_bus0, urb);
105 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
108 struct list_head *pos;
109 struct mon_reader *r;
111 spin_lock_irqsave(&mbus->lock, flags);
113 list_for_each (pos, &mbus->r_list) {
114 r = list_entry(pos, struct mon_reader, r_link);
115 r->rnf_error(r->r_data, urb, error);
117 spin_unlock_irqrestore(&mbus->lock, flags);
121 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
123 struct mon_bus *mbus;
125 if ((mbus = ubus->mon_bus) != NULL)
126 mon_bus_submit_error(mbus, urb, error);
127 mon_bus_submit_error(&mon_bus0, urb, error);
132 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
135 struct list_head *pos;
136 struct mon_reader *r;
138 spin_lock_irqsave(&mbus->lock, flags);
140 list_for_each (pos, &mbus->r_list) {
141 r = list_entry(pos, struct mon_reader, r_link);
142 r->rnf_complete(r->r_data, urb);
144 spin_unlock_irqrestore(&mbus->lock, flags);
147 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
149 struct mon_bus *mbus;
151 mbus = ubus->mon_bus;
154 * This should not happen.
155 * At this point we do not even know the bus number...
157 printk(KERN_ERR TAG ": Null mon bus in URB, address %p\n",
162 mon_bus_complete(mbus, urb);
163 mon_bus_complete(&mon_bus0, urb);
166 /* int (*unlink_urb) (struct urb *urb, int status); */
171 static void mon_stop(struct mon_bus *mbus)
173 struct usb_bus *ubus = mbus->u_bus;
176 if (mbus == &mon_bus0) {
177 list_for_each (p, &mon_buses) {
178 mbus = list_entry(p, struct mon_bus, bus_link);
180 * We do not change nreaders here, so rely on mon_lock.
182 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
187 * A stop can be called for a dissolved mon_bus in case of
188 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
190 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
198 * Add a USB bus (usually by a modprobe foo-hcd)
200 * This does not return an error code because the core cannot care less
201 * if monitoring is not established.
203 static void mon_bus_add(struct usb_bus *ubus)
206 mutex_lock(&mon_lock);
207 if (mon_bus0.nreaders != 0)
209 mutex_unlock(&mon_lock);
213 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
215 static void mon_bus_remove(struct usb_bus *ubus)
217 struct mon_bus *mbus = ubus->mon_bus;
219 mutex_lock(&mon_lock);
220 list_del(&mbus->bus_link);
221 if (mbus->text_inited)
223 if (mbus->bin_inited)
226 mon_dissolve(mbus, ubus);
227 kref_put(&mbus->ref, mon_bus_drop);
228 mutex_unlock(&mon_lock);
231 static int mon_notify(struct notifier_block *self, unsigned long action,
244 static struct notifier_block mon_nb = {
245 .notifier_call = mon_notify,
251 static struct usb_mon_operations mon_ops_0 = {
252 .urb_submit = mon_submit,
253 .urb_submit_error = mon_submit_error,
254 .urb_complete = mon_complete,
258 * Tear usb_bus and mon_bus apart.
260 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
263 if (ubus->monitored) {
268 ubus->mon_bus = NULL;
272 /* We want synchronize_irq() here, but that needs an argument. */
277 static void mon_bus_drop(struct kref *r)
279 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
284 * Initialize a bus for us:
286 * - refcount USB bus struct
289 static void mon_bus_init(struct usb_bus *ubus)
291 struct mon_bus *mbus;
293 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
295 kref_init(&mbus->ref);
296 spin_lock_init(&mbus->lock);
297 INIT_LIST_HEAD(&mbus->r_list);
300 * We don't need to take a reference to ubus, because we receive
301 * a notification if the bus is about to be removed.
304 ubus->mon_bus = mbus;
306 mbus->text_inited = mon_text_add(mbus, ubus);
307 mbus->bin_inited = mon_bin_add(mbus, ubus);
309 mutex_lock(&mon_lock);
310 list_add_tail(&mbus->bus_link, &mon_buses);
311 mutex_unlock(&mon_lock);
318 static void mon_bus0_init(void)
320 struct mon_bus *mbus = &mon_bus0;
322 kref_init(&mbus->ref);
323 spin_lock_init(&mbus->lock);
324 INIT_LIST_HEAD(&mbus->r_list);
326 mbus->text_inited = mon_text_add(mbus, NULL);
327 mbus->bin_inited = mon_bin_add(mbus, NULL);
331 * Search a USB bus by number. Notice that USB bus numbers start from one,
332 * which we may later use to identify "all" with zero.
334 * This function must be called with mon_lock held.
336 * This is obviously inefficient and may be revised in the future.
338 struct mon_bus *mon_bus_lookup(unsigned int num)
341 struct mon_bus *mbus;
346 list_for_each (p, &mon_buses) {
347 mbus = list_entry(p, struct mon_bus, bus_link);
348 if (mbus->u_bus->busnum == num) {
355 static int __init mon_init(void)
357 struct usb_bus *ubus;
360 if ((rc = mon_text_init()) != 0)
362 if ((rc = mon_bin_init()) != 0)
367 if (usb_mon_register(&mon_ops_0) != 0) {
368 printk(KERN_NOTICE TAG ": unable to register with the core\n");
372 // MOD_INC_USE_COUNT(which_module?);
374 usb_register_notify(&mon_nb);
376 mutex_lock(&usb_bus_list_lock);
377 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
380 mutex_unlock(&usb_bus_list_lock);
391 static void __exit mon_exit(void)
393 struct mon_bus *mbus;
396 usb_unregister_notify(&mon_nb);
397 usb_mon_deregister();
399 mutex_lock(&mon_lock);
401 while (!list_empty(&mon_buses)) {
403 mbus = list_entry(p, struct mon_bus, bus_link);
406 if (mbus->text_inited)
408 if (mbus->bin_inited)
412 * This never happens, because the open/close paths in
413 * file level maintain module use counters and so rmmod fails
414 * before reaching here. However, better be safe...
416 if (mbus->nreaders) {
418 ": Outstanding opens (%d) on usb%d, leaking...\n",
419 mbus->nreaders, mbus->u_bus->busnum);
420 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
423 mon_dissolve(mbus, mbus->u_bus);
424 kref_put(&mbus->ref, mon_bus_drop);
428 if (mbus->text_inited)
430 if (mbus->bin_inited)
433 mutex_unlock(&mon_lock);
439 module_init(mon_init);
440 module_exit(mon_exit);
442 MODULE_LICENSE("GPL");