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/usb/hcd.h>
13 #include <linux/slab.h>
14 #include <linux/notifier.h>
15 #include <linux/mutex.h>
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 usb_bus
*ubus
);
25 DEFINE_MUTEX(mon_lock
);
27 struct mon_bus mon_bus0
; /* Pseudo bus meaning "all buses" */
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) {
42 if (mbus
== &mon_bus0
) {
43 list_for_each (p
, &mon_buses
) {
45 m1
= list_entry(p
, struct mon_bus
, bus_link
);
46 m1
->u_bus
->monitored
= 1;
49 mbus
->u_bus
->monitored
= 1;
53 list_add_tail(&r
->r_link
, &mbus
->r_list
);
54 spin_unlock_irqrestore(&mbus
->lock
, flags
);
60 * Unlink reader from the bus.
62 * This is called with mon_lock taken, so we can decrement mbus->ref.
64 void mon_reader_del(struct mon_bus
*mbus
, struct mon_reader
*r
)
68 spin_lock_irqsave(&mbus
->lock
, flags
);
71 if (mbus
->nreaders
== 0)
73 spin_unlock_irqrestore(&mbus
->lock
, flags
);
75 kref_put(&mbus
->ref
, mon_bus_drop
);
80 static void mon_bus_submit(struct mon_bus
*mbus
, struct urb
*urb
)
83 struct list_head
*pos
;
86 spin_lock_irqsave(&mbus
->lock
, flags
);
88 list_for_each (pos
, &mbus
->r_list
) {
89 r
= list_entry(pos
, struct mon_reader
, r_link
);
90 r
->rnf_submit(r
->r_data
, urb
);
92 spin_unlock_irqrestore(&mbus
->lock
, flags
);
95 static void mon_submit(struct usb_bus
*ubus
, struct urb
*urb
)
101 mon_bus_submit(mbus
, urb
);
102 mon_bus_submit(&mon_bus0
, urb
);
107 static void mon_bus_submit_error(struct mon_bus
*mbus
, struct urb
*urb
, int error
)
110 struct list_head
*pos
;
111 struct mon_reader
*r
;
113 spin_lock_irqsave(&mbus
->lock
, flags
);
115 list_for_each (pos
, &mbus
->r_list
) {
116 r
= list_entry(pos
, struct mon_reader
, r_link
);
117 r
->rnf_error(r
->r_data
, urb
, error
);
119 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 mbus
= ubus
->mon_bus
;
128 mon_bus_submit_error(mbus
, urb
, error
);
129 mon_bus_submit_error(&mon_bus0
, urb
, error
);
134 static void mon_bus_complete(struct mon_bus
*mbus
, struct urb
*urb
, int status
)
137 struct list_head
*pos
;
138 struct mon_reader
*r
;
140 spin_lock_irqsave(&mbus
->lock
, flags
);
142 list_for_each (pos
, &mbus
->r_list
) {
143 r
= list_entry(pos
, struct mon_reader
, r_link
);
144 r
->rnf_complete(r
->r_data
, urb
, status
);
146 spin_unlock_irqrestore(&mbus
->lock
, flags
);
149 static void mon_complete(struct usb_bus
*ubus
, struct urb
*urb
, int status
)
151 struct mon_bus
*mbus
;
153 mbus
= ubus
->mon_bus
;
155 mon_bus_complete(mbus
, urb
, status
);
156 mon_bus_complete(&mon_bus0
, urb
, status
);
159 /* int (*unlink_urb) (struct urb *urb, int status); */
164 static void mon_stop(struct mon_bus
*mbus
)
166 struct usb_bus
*ubus
;
169 if (mbus
== &mon_bus0
) {
170 list_for_each (p
, &mon_buses
) {
171 mbus
= list_entry(p
, struct mon_bus
, bus_link
);
173 * We do not change nreaders here, so rely on mon_lock.
175 if (mbus
->nreaders
== 0 && (ubus
= mbus
->u_bus
) != NULL
)
180 * A stop can be called for a dissolved mon_bus in case of
181 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
183 if (mon_bus0
.nreaders
== 0 && (ubus
= mbus
->u_bus
) != NULL
) {
191 * Add a USB bus (usually by a modprobe foo-hcd)
193 * This does not return an error code because the core cannot care less
194 * if monitoring is not established.
196 static void mon_bus_add(struct usb_bus
*ubus
)
199 mutex_lock(&mon_lock
);
200 if (mon_bus0
.nreaders
!= 0)
202 mutex_unlock(&mon_lock
);
206 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
208 static void mon_bus_remove(struct usb_bus
*ubus
)
210 struct mon_bus
*mbus
= ubus
->mon_bus
;
212 mutex_lock(&mon_lock
);
213 list_del(&mbus
->bus_link
);
214 if (mbus
->text_inited
)
216 if (mbus
->bin_inited
)
219 mon_dissolve(mbus
, ubus
);
220 kref_put(&mbus
->ref
, mon_bus_drop
);
221 mutex_unlock(&mon_lock
);
224 static int mon_notify(struct notifier_block
*self
, unsigned long action
,
237 static struct notifier_block mon_nb
= {
238 .notifier_call
= mon_notify
,
244 static const struct usb_mon_operations mon_ops_0
= {
245 .urb_submit
= mon_submit
,
246 .urb_submit_error
= mon_submit_error
,
247 .urb_complete
= mon_complete
,
251 * Tear usb_bus and mon_bus apart.
253 static void mon_dissolve(struct mon_bus
*mbus
, struct usb_bus
*ubus
)
256 if (ubus
->monitored
) {
261 ubus
->mon_bus
= NULL
;
265 /* We want synchronize_irq() here, but that needs an argument. */
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 mbus
= kzalloc(sizeof(struct mon_bus
), GFP_KERNEL
);
289 kref_init(&mbus
->ref
);
290 spin_lock_init(&mbus
->lock
);
291 INIT_LIST_HEAD(&mbus
->r_list
);
294 * We don't need to take a reference to ubus, because we receive
295 * a notification if the bus is about to be removed.
298 ubus
->mon_bus
= mbus
;
300 mbus
->text_inited
= mon_text_add(mbus
, ubus
);
301 mbus
->bin_inited
= mon_bin_add(mbus
, ubus
);
303 mutex_lock(&mon_lock
);
304 list_add_tail(&mbus
->bus_link
, &mon_buses
);
305 mutex_unlock(&mon_lock
);
312 static void mon_bus0_init(void)
314 struct mon_bus
*mbus
= &mon_bus0
;
316 kref_init(&mbus
->ref
);
317 spin_lock_init(&mbus
->lock
);
318 INIT_LIST_HEAD(&mbus
->r_list
);
320 mbus
->text_inited
= mon_text_add(mbus
, NULL
);
321 mbus
->bin_inited
= mon_bin_add(mbus
, NULL
);
325 * Search a USB bus by number. Notice that USB bus numbers start from one,
326 * which we may later use to identify "all" with zero.
328 * This function must be called with mon_lock held.
330 * This is obviously inefficient and may be revised in the future.
332 struct mon_bus
*mon_bus_lookup(unsigned int num
)
335 struct mon_bus
*mbus
;
340 list_for_each (p
, &mon_buses
) {
341 mbus
= list_entry(p
, struct mon_bus
, bus_link
);
342 if (mbus
->u_bus
->busnum
== num
) {
349 static int __init
mon_init(void)
351 struct usb_bus
*ubus
;
354 if ((rc
= mon_text_init()) != 0)
356 if ((rc
= mon_bin_init()) != 0)
361 if (usb_mon_register(&mon_ops_0
) != 0) {
362 printk(KERN_NOTICE TAG
": unable to register with the core\n");
366 // MOD_INC_USE_COUNT(which_module?);
368 mutex_lock(&usb_bus_idr_lock
);
369 idr_for_each_entry(&usb_bus_idr
, ubus
, id
)
371 usb_register_notify(&mon_nb
);
372 mutex_unlock(&usb_bus_idr_lock
);
383 static void __exit
mon_exit(void)
385 struct mon_bus
*mbus
;
388 usb_unregister_notify(&mon_nb
);
389 usb_mon_deregister();
391 mutex_lock(&mon_lock
);
393 while (!list_empty(&mon_buses
)) {
395 mbus
= list_entry(p
, struct mon_bus
, bus_link
);
398 if (mbus
->text_inited
)
400 if (mbus
->bin_inited
)
404 * This never happens, because the open/close paths in
405 * file level maintain module use counters and so rmmod fails
406 * before reaching here. However, better be safe...
408 if (mbus
->nreaders
) {
410 ": Outstanding opens (%d) on usb%d, leaking...\n",
411 mbus
->nreaders
, mbus
->u_bus
->busnum
);
412 atomic_set(&mbus
->ref
.refcount
, 2); /* Force leak */
415 mon_dissolve(mbus
, mbus
->u_bus
);
416 kref_put(&mbus
->ref
, mon_bus_drop
);
420 if (mbus
->text_inited
)
422 if (mbus
->bin_inited
)
425 mutex_unlock(&mon_lock
);
431 module_init(mon_init
);
432 module_exit(mon_exit
);
434 MODULE_LICENSE("GPL");