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>
16 #include "../core/hcd.h"
18 static void mon_submit(struct usb_bus
*ubus
, struct urb
*urb
);
19 static void mon_complete(struct usb_bus
*ubus
, struct urb
*urb
);
20 static void mon_stop(struct mon_bus
*mbus
);
21 static void mon_dissolve(struct mon_bus
*mbus
, struct usb_bus
*ubus
);
22 static void mon_bus_drop(struct kref
*r
);
23 static void mon_bus_init(struct dentry
*mondir
, struct usb_bus
*ubus
);
25 DECLARE_MUTEX(mon_lock
);
27 static struct dentry
*mon_dir
; /* /dbg/usbmon */
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)
98 list_for_each (pos
, &mbus
->r_list
) {
99 r
= list_entry(pos
, struct mon_reader
, r_link
);
100 r
->rnf_submit(r
->r_data
, urb
);
103 spin_unlock_irqrestore(&mbus
->lock
, flags
);
107 spin_unlock_irqrestore(&mbus
->lock
, flags
);
114 static void mon_submit_error(struct usb_bus
*ubus
, struct urb
*urb
, int err
)
116 struct mon_bus
*mbus
;
118 mbus
= ubus
->mon_bus
;
123 * XXX Capture the error code and the 'E' event.
134 static void mon_complete(struct usb_bus
*ubus
, struct urb
*urb
)
136 struct mon_bus
*mbus
;
138 struct list_head
*pos
;
139 struct mon_reader
*r
;
141 mbus
= ubus
->mon_bus
;
144 * This should not happen.
145 * At this point we do not even know the bus number...
147 printk(KERN_ERR TAG
": Null mon bus in URB, pipe 0x%x\n",
152 spin_lock_irqsave(&mbus
->lock
, flags
);
153 list_for_each (pos
, &mbus
->r_list
) {
154 r
= list_entry(pos
, struct mon_reader
, r_link
);
155 r
->rnf_complete(r
->r_data
, urb
);
157 spin_unlock_irqrestore(&mbus
->lock
, flags
);
160 /* int (*unlink_urb) (struct urb *urb, int status); */
164 * Obviously this must be well locked, so no need to play with mb's.
166 static void mon_stop(struct mon_bus
*mbus
)
168 struct usb_bus
*ubus
= mbus
->u_bus
;
171 * A stop can be called for a dissolved mon_bus in case of
172 * a reader staying across an rmmod foo_hcd.
181 * Add a USB bus (usually by a modprobe foo-hcd)
183 * This does not return an error code because the core cannot care less
184 * if monitoring is not established.
186 static void mon_bus_add(struct usb_bus
*ubus
)
188 mon_bus_init(mon_dir
, ubus
);
192 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
194 static void mon_bus_remove(struct usb_bus
*ubus
)
196 struct mon_bus
*mbus
= ubus
->mon_bus
;
199 list_del(&mbus
->bus_link
);
200 debugfs_remove(mbus
->dent_t
);
201 debugfs_remove(mbus
->dent_s
);
203 mon_dissolve(mbus
, ubus
);
204 kref_put(&mbus
->ref
, mon_bus_drop
);
211 static struct usb_mon_operations mon_ops_0
= {
212 .urb_submit
= mon_submit
,
213 .urb_submit_error
= mon_submit_error
,
214 .urb_complete
= mon_complete
,
215 .bus_add
= mon_bus_add
,
216 .bus_remove
= mon_bus_remove
,
220 * Tear usb_bus and mon_bus apart.
222 static void mon_dissolve(struct mon_bus
*mbus
, struct usb_bus
*ubus
)
226 * Never happens, but...
228 if (ubus
->monitored
) {
229 printk(KERN_ERR TAG
": bus %d is dissolved while monitored\n",
235 ubus
->mon_bus
= NULL
;
238 // usb_bus_put(ubus);
243 static void mon_bus_drop(struct kref
*r
)
245 struct mon_bus
*mbus
= container_of(r
, struct mon_bus
, ref
);
250 * Initialize a bus for us:
252 * - refcount USB bus struct
255 static void mon_bus_init(struct dentry
*mondir
, struct usb_bus
*ubus
)
258 struct mon_bus
*mbus
;
259 enum { NAMESZ
= 10 };
263 if ((mbus
= kmalloc(sizeof(struct mon_bus
), GFP_KERNEL
)) == NULL
)
265 memset(mbus
, 0, sizeof(struct mon_bus
));
266 kref_init(&mbus
->ref
);
267 spin_lock_init(&mbus
->lock
);
268 INIT_LIST_HEAD(&mbus
->r_list
);
271 * This usb_bus_get here is superfluous, because we receive
272 * a notification if usb_bus is about to be removed.
274 // usb_bus_get(ubus);
276 ubus
->mon_bus
= mbus
;
278 rc
= snprintf(name
, NAMESZ
, "%dt", ubus
->busnum
);
279 if (rc
<= 0 || rc
>= NAMESZ
)
281 d
= debugfs_create_file(name
, 0600, mondir
, mbus
, &mon_fops_text
);
286 rc
= snprintf(name
, NAMESZ
, "%ds", ubus
->busnum
);
287 if (rc
<= 0 || rc
>= NAMESZ
)
289 d
= debugfs_create_file(name
, 0600, mondir
, mbus
, &mon_fops_stat
);
295 list_add_tail(&mbus
->bus_link
, &mon_buses
);
301 debugfs_remove(mbus
->dent_t
);
309 static int __init
mon_init(void)
311 struct usb_bus
*ubus
;
312 struct dentry
*mondir
;
314 mondir
= debugfs_create_dir("usbmon", NULL
);
315 if (IS_ERR(mondir
)) {
316 printk(KERN_NOTICE TAG
": debugfs is not available\n");
319 if (mondir
== NULL
) {
320 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
325 if (usb_mon_register(&mon_ops_0
) != 0) {
326 printk(KERN_NOTICE TAG
": unable to register with the core\n");
327 debugfs_remove(mondir
);
330 // MOD_INC_USE_COUNT(which_module?);
332 down(&usb_bus_list_lock
);
333 list_for_each_entry (ubus
, &usb_bus_list
, bus_list
) {
334 mon_bus_init(mondir
, ubus
);
336 up(&usb_bus_list_lock
);
340 static void __exit
mon_exit(void)
342 struct mon_bus
*mbus
;
345 usb_mon_deregister();
348 while (!list_empty(&mon_buses
)) {
350 mbus
= list_entry(p
, struct mon_bus
, bus_link
);
353 debugfs_remove(mbus
->dent_t
);
354 debugfs_remove(mbus
->dent_s
);
357 * This never happens, because the open/close paths in
358 * file level maintain module use counters and so rmmod fails
359 * before reaching here. However, better be safe...
361 if (mbus
->nreaders
) {
363 ": Outstanding opens (%d) on usb%d, leaking...\n",
364 mbus
->nreaders
, mbus
->u_bus
->busnum
);
365 atomic_set(&mbus
->ref
.refcount
, 2); /* Force leak */
368 mon_dissolve(mbus
, mbus
->u_bus
);
369 kref_put(&mbus
->ref
, mon_bus_drop
);
373 debugfs_remove(mon_dir
);
376 module_init(mon_init
);
377 module_exit(mon_exit
);
379 MODULE_LICENSE("GPL");