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)
100 list_for_each (pos
, &mbus
->r_list
) {
101 r
= list_entry(pos
, struct mon_reader
, r_link
);
102 r
->rnf_submit(r
->r_data
, urb
);
105 spin_unlock_irqrestore(&mbus
->lock
, flags
);
109 spin_unlock_irqrestore(&mbus
->lock
, flags
);
116 static void mon_submit_error(struct usb_bus
*ubus
, struct urb
*urb
, int err
)
118 struct mon_bus
*mbus
;
120 mbus
= ubus
->mon_bus
;
125 * XXX Capture the error code and the 'E' event.
136 static void mon_complete(struct usb_bus
*ubus
, struct urb
*urb
)
138 struct mon_bus
*mbus
;
140 struct list_head
*pos
;
141 struct mon_reader
*r
;
143 mbus
= ubus
->mon_bus
;
146 * This should not happen.
147 * At this point we do not even know the bus number...
149 printk(KERN_ERR TAG
": Null mon bus in URB, pipe 0x%x\n",
154 spin_lock_irqsave(&mbus
->lock
, flags
);
155 list_for_each (pos
, &mbus
->r_list
) {
156 r
= list_entry(pos
, struct mon_reader
, r_link
);
157 r
->rnf_complete(r
->r_data
, urb
);
159 spin_unlock_irqrestore(&mbus
->lock
, flags
);
162 /* int (*unlink_urb) (struct urb *urb, int status); */
166 * Obviously this must be well locked, so no need to play with mb's.
168 static void mon_stop(struct mon_bus
*mbus
)
170 struct usb_bus
*ubus
= mbus
->u_bus
;
173 * A stop can be called for a dissolved mon_bus in case of
174 * a reader staying across an rmmod foo_hcd.
183 * Add a USB bus (usually by a modprobe foo-hcd)
185 * This does not return an error code because the core cannot care less
186 * if monitoring is not established.
188 static void mon_bus_add(struct usb_bus
*ubus
)
190 mon_bus_init(mon_dir
, ubus
);
194 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
196 static void mon_bus_remove(struct usb_bus
*ubus
)
198 struct mon_bus
*mbus
= ubus
->mon_bus
;
200 mutex_lock(&mon_lock
);
201 list_del(&mbus
->bus_link
);
202 debugfs_remove(mbus
->dent_t
);
203 debugfs_remove(mbus
->dent_s
);
205 mon_dissolve(mbus
, ubus
);
206 kref_put(&mbus
->ref
, mon_bus_drop
);
207 mutex_unlock(&mon_lock
);
210 static int mon_notify(struct notifier_block
*self
, unsigned long action
,
223 static struct notifier_block mon_nb
= {
224 .notifier_call
= mon_notify
,
230 static struct usb_mon_operations mon_ops_0
= {
231 .urb_submit
= mon_submit
,
232 .urb_submit_error
= mon_submit_error
,
233 .urb_complete
= mon_complete
,
237 * Tear usb_bus and mon_bus apart.
239 static void mon_dissolve(struct mon_bus
*mbus
, struct usb_bus
*ubus
)
243 * Never happens, but...
245 if (ubus
->monitored
) {
246 printk(KERN_ERR TAG
": bus %d is dissolved while monitored\n",
252 ubus
->mon_bus
= NULL
;
255 // usb_bus_put(ubus);
260 static void mon_bus_drop(struct kref
*r
)
262 struct mon_bus
*mbus
= container_of(r
, struct mon_bus
, ref
);
267 * Initialize a bus for us:
269 * - refcount USB bus struct
272 static void mon_bus_init(struct dentry
*mondir
, struct usb_bus
*ubus
)
275 struct mon_bus
*mbus
;
276 enum { NAMESZ
= 10 };
280 if ((mbus
= kzalloc(sizeof(struct mon_bus
), GFP_KERNEL
)) == NULL
)
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
);
310 mutex_lock(&mon_lock
);
311 list_add_tail(&mbus
->bus_link
, &mon_buses
);
312 mutex_unlock(&mon_lock
);
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 mutex_lock(&usb_bus_list_lock
);
351 list_for_each_entry (ubus
, &usb_bus_list
, bus_list
) {
352 mon_bus_init(mondir
, ubus
);
354 mutex_unlock(&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();
366 mutex_lock(&mon_lock
);
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
);
390 mutex_unlock(&mon_lock
);
392 debugfs_remove(mon_dir
);
395 module_init(mon_init
);
396 module_exit(mon_exit
);
398 MODULE_LICENSE("GPL");