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_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 usb_bus *ubus);
26 DEFINE_MUTEX(mon_lock);
28 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
31 * Link a reader into the bus.
33 * This must be called with mon_lock taken because of mbus->ref.
35 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
40 spin_lock_irqsave(&mbus->lock, flags);
41 if (mbus->nreaders == 0) {
43 if (ubus->monitored) {
45 * Something is really broken, refuse to go on and
46 * possibly corrupt ops pointers or worse.
48 printk(KERN_ERR TAG ": bus %d is already monitored\n",
50 spin_unlock_irqrestore(&mbus->lock, flags);
56 list_add_tail(&r->r_link, &mbus->r_list);
57 spin_unlock_irqrestore(&mbus->lock, flags);
63 * Unlink reader from the bus.
65 * This is called with mon_lock taken, so we can decrement mbus->ref.
67 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
71 spin_lock_irqsave(&mbus->lock, flags);
74 if (mbus->nreaders == 0)
76 spin_unlock_irqrestore(&mbus->lock, flags);
78 kref_put(&mbus->ref, mon_bus_drop);
83 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
87 struct list_head *pos;
94 spin_lock_irqsave(&mbus->lock, flags);
95 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 error)
117 struct mon_bus *mbus;
119 struct list_head *pos;
120 struct mon_reader *r;
122 mbus = ubus->mon_bus;
126 spin_lock_irqsave(&mbus->lock, flags);
127 if (mbus->nreaders == 0)
131 list_for_each (pos, &mbus->r_list) {
132 r = list_entry(pos, struct mon_reader, r_link);
133 r->rnf_error(r->r_data, urb, error);
136 spin_unlock_irqrestore(&mbus->lock, flags);
140 spin_unlock_irqrestore(&mbus->lock, flags);
147 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
149 struct mon_bus *mbus;
151 struct list_head *pos;
152 struct mon_reader *r;
154 mbus = ubus->mon_bus;
157 * This should not happen.
158 * At this point we do not even know the bus number...
160 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
165 spin_lock_irqsave(&mbus->lock, flags);
167 list_for_each (pos, &mbus->r_list) {
168 r = list_entry(pos, struct mon_reader, r_link);
169 r->rnf_complete(r->r_data, urb);
171 spin_unlock_irqrestore(&mbus->lock, flags);
174 /* int (*unlink_urb) (struct urb *urb, int status); */
179 static void mon_stop(struct mon_bus *mbus)
181 struct usb_bus *ubus = mbus->u_bus;
184 * A stop can be called for a dissolved mon_bus in case of
185 * a reader staying across an rmmod foo_hcd.
194 * Add a USB bus (usually by a modprobe foo-hcd)
196 * This does not return an error code because the core cannot care less
197 * if monitoring is not established.
199 static void mon_bus_add(struct usb_bus *ubus)
205 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
207 static void mon_bus_remove(struct usb_bus *ubus)
209 struct mon_bus *mbus = ubus->mon_bus;
211 mutex_lock(&mon_lock);
212 list_del(&mbus->bus_link);
213 if (mbus->text_inited)
216 mon_dissolve(mbus, ubus);
217 kref_put(&mbus->ref, mon_bus_drop);
218 mutex_unlock(&mon_lock);
221 static int mon_notify(struct notifier_block *self, unsigned long action,
234 static struct notifier_block mon_nb = {
235 .notifier_call = mon_notify,
241 static struct usb_mon_operations mon_ops_0 = {
242 .urb_submit = mon_submit,
243 .urb_submit_error = mon_submit_error,
244 .urb_complete = mon_complete,
248 * Tear usb_bus and mon_bus apart.
250 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
254 * Never happens, but...
256 if (ubus->monitored) {
257 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
263 ubus->mon_bus = NULL;
270 static void mon_bus_drop(struct kref *r)
272 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
277 * Initialize a bus for us:
279 * - refcount USB bus struct
282 static void mon_bus_init(struct usb_bus *ubus)
284 struct mon_bus *mbus;
286 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
288 kref_init(&mbus->ref);
289 spin_lock_init(&mbus->lock);
290 INIT_LIST_HEAD(&mbus->r_list);
293 * We don't need to take a reference to ubus, because we receive
294 * a notification if the bus is about to be removed.
297 ubus->mon_bus = mbus;
298 mbus->uses_dma = ubus->uses_dma;
300 mbus->text_inited = mon_text_add(mbus, ubus);
303 mutex_lock(&mon_lock);
304 list_add_tail(&mbus->bus_link, &mon_buses);
305 mutex_unlock(&mon_lock);
313 * Search a USB bus by number. Notice that USB bus numbers start from one,
314 * which we may later use to identify "all" with zero.
316 * This function must be called with mon_lock held.
318 * This is obviously inefficient and may be revised in the future.
320 struct mon_bus *mon_bus_lookup(unsigned int num)
323 struct mon_bus *mbus;
325 list_for_each (p, &mon_buses) {
326 mbus = list_entry(p, struct mon_bus, bus_link);
327 if (mbus->u_bus->busnum == num) {
334 static int __init mon_init(void)
336 struct usb_bus *ubus;
339 if ((rc = mon_text_init()) != 0)
341 if ((rc = mon_bin_init()) != 0)
344 if (usb_mon_register(&mon_ops_0) != 0) {
345 printk(KERN_NOTICE TAG ": unable to register with the core\n");
349 // MOD_INC_USE_COUNT(which_module?);
351 usb_register_notify(&mon_nb);
353 mutex_lock(&usb_bus_list_lock);
354 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
357 mutex_unlock(&usb_bus_list_lock);
368 static void __exit mon_exit(void)
370 struct mon_bus *mbus;
373 usb_unregister_notify(&mon_nb);
374 usb_mon_deregister();
376 mutex_lock(&mon_lock);
377 while (!list_empty(&mon_buses)) {
379 mbus = list_entry(p, struct mon_bus, bus_link);
382 if (mbus->text_inited)
386 * This never happens, because the open/close paths in
387 * file level maintain module use counters and so rmmod fails
388 * before reaching here. However, better be safe...
390 if (mbus->nreaders) {
392 ": Outstanding opens (%d) on usb%d, leaking...\n",
393 mbus->nreaders, mbus->u_bus->busnum);
394 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
397 mon_dissolve(mbus, mbus->u_bus);
398 kref_put(&mbus->ref, mon_bus_drop);
400 mutex_unlock(&mon_lock);
406 module_init(mon_init);
407 module_exit(mon_exit);
409 MODULE_LICENSE("GPL");