1 // SPDX-License-Identifier: GPL-2.0
3 * The USB Monitor, inspired by Dave Harding's USBMon.
5 * mon_main.c: Main file, module initiation and exit, registrations, etc.
7 * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/usb.h>
13 #include <linux/usb/hcd.h>
14 #include <linux/slab.h>
15 #include <linux/notifier.h>
16 #include <linux/mutex.h>
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 struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
29 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
32 * Link a reader into the bus.
34 * This must be called with mon_lock taken because of mbus->ref.
36 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
41 spin_lock_irqsave(&mbus->lock, flags);
42 if (mbus->nreaders == 0) {
43 if (mbus == &mon_bus0) {
44 list_for_each (p, &mon_buses) {
46 m1 = list_entry(p, struct mon_bus, bus_link);
47 m1->u_bus->monitored = 1;
50 mbus->u_bus->monitored = 1;
54 list_add_tail(&r->r_link, &mbus->r_list);
55 spin_unlock_irqrestore(&mbus->lock, flags);
61 * Unlink reader from the bus.
63 * This is called with mon_lock taken, so we can decrement mbus->ref.
65 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
69 spin_lock_irqsave(&mbus->lock, flags);
72 if (mbus->nreaders == 0)
74 spin_unlock_irqrestore(&mbus->lock, flags);
76 kref_put(&mbus->ref, mon_bus_drop);
81 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
86 spin_lock_irqsave(&mbus->lock, flags);
88 list_for_each_entry(r, &mbus->r_list, r_link)
89 r->rnf_submit(r->r_data, urb);
90 spin_unlock_irqrestore(&mbus->lock, flags);
93 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
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 mon_reader *r;
110 spin_lock_irqsave(&mbus->lock, flags);
112 list_for_each_entry(r, &mbus->r_list, r_link)
113 r->rnf_error(r->r_data, urb, error);
114 spin_unlock_irqrestore(&mbus->lock, flags);
117 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
119 struct mon_bus *mbus;
121 mbus = ubus->mon_bus;
123 mon_bus_submit_error(mbus, urb, error);
124 mon_bus_submit_error(&mon_bus0, urb, error);
129 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
132 struct mon_reader *r;
134 spin_lock_irqsave(&mbus->lock, flags);
136 list_for_each_entry(r, &mbus->r_list, r_link)
137 r->rnf_complete(r->r_data, urb, status);
138 spin_unlock_irqrestore(&mbus->lock, flags);
141 static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
143 struct mon_bus *mbus;
145 mbus = ubus->mon_bus;
147 mon_bus_complete(mbus, urb, status);
148 mon_bus_complete(&mon_bus0, urb, status);
151 /* int (*unlink_urb) (struct urb *urb, int status); */
156 static void mon_stop(struct mon_bus *mbus)
158 struct usb_bus *ubus;
160 if (mbus == &mon_bus0) {
161 list_for_each_entry(mbus, &mon_buses, bus_link) {
163 * We do not change nreaders here, so rely on mon_lock.
165 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
170 * A stop can be called for a dissolved mon_bus in case of
171 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
173 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
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)
189 mutex_lock(&mon_lock);
190 if (mon_bus0.nreaders != 0)
192 mutex_unlock(&mon_lock);
196 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
198 static void mon_bus_remove(struct usb_bus *ubus)
200 struct mon_bus *mbus = ubus->mon_bus;
202 mutex_lock(&mon_lock);
203 list_del(&mbus->bus_link);
204 if (mbus->text_inited)
206 if (mbus->bin_inited)
209 mon_dissolve(mbus, ubus);
210 kref_put(&mbus->ref, mon_bus_drop);
211 mutex_unlock(&mon_lock);
214 static int mon_notify(struct notifier_block *self, unsigned long action,
227 static struct notifier_block mon_nb = {
228 .notifier_call = mon_notify,
234 static const struct usb_mon_operations mon_ops_0 = {
235 .urb_submit = mon_submit,
236 .urb_submit_error = mon_submit_error,
237 .urb_complete = mon_complete,
241 * Tear usb_bus and mon_bus apart.
243 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
246 if (ubus->monitored) {
251 ubus->mon_bus = NULL;
255 /* We want synchronize_irq() here, but that needs an argument. */
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 usb_bus *ubus)
274 struct mon_bus *mbus;
276 mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL);
279 kref_init(&mbus->ref);
280 spin_lock_init(&mbus->lock);
281 INIT_LIST_HEAD(&mbus->r_list);
284 * We don't need to take a reference to ubus, because we receive
285 * a notification if the bus is about to be removed.
288 ubus->mon_bus = mbus;
290 mbus->text_inited = mon_text_add(mbus, ubus);
291 mbus->bin_inited = mon_bin_add(mbus, ubus);
293 mutex_lock(&mon_lock);
294 list_add_tail(&mbus->bus_link, &mon_buses);
295 mutex_unlock(&mon_lock);
302 static void mon_bus0_init(void)
304 struct mon_bus *mbus = &mon_bus0;
306 kref_init(&mbus->ref);
307 spin_lock_init(&mbus->lock);
308 INIT_LIST_HEAD(&mbus->r_list);
310 mbus->text_inited = mon_text_add(mbus, NULL);
311 mbus->bin_inited = mon_bin_add(mbus, NULL);
315 * Search a USB bus by number. Notice that USB bus numbers start from one,
316 * which we may later use to identify "all" with zero.
318 * This function must be called with mon_lock held.
320 * This is obviously inefficient and may be revised in the future.
322 struct mon_bus *mon_bus_lookup(unsigned int num)
324 struct mon_bus *mbus;
329 list_for_each_entry(mbus, &mon_buses, bus_link) {
330 if (mbus->u_bus->busnum == num) {
337 static int __init mon_init(void)
339 struct usb_bus *ubus;
342 if ((rc = mon_text_init()) != 0)
344 if ((rc = mon_bin_init()) != 0)
349 if (usb_mon_register(&mon_ops_0) != 0) {
350 printk(KERN_NOTICE TAG ": unable to register with the core\n");
354 // MOD_INC_USE_COUNT(which_module?);
356 mutex_lock(&usb_bus_idr_lock);
357 idr_for_each_entry(&usb_bus_idr, ubus, id)
359 usb_register_notify(&mon_nb);
360 mutex_unlock(&usb_bus_idr_lock);
371 static void __exit mon_exit(void)
373 struct mon_bus *mbus;
376 usb_unregister_notify(&mon_nb);
377 usb_mon_deregister();
379 mutex_lock(&mon_lock);
381 while (!list_empty(&mon_buses)) {
383 mbus = list_entry(p, struct mon_bus, bus_link);
386 if (mbus->text_inited)
388 if (mbus->bin_inited)
392 * This never happens, because the open/close paths in
393 * file level maintain module use counters and so rmmod fails
394 * before reaching here. However, better be safe...
396 if (mbus->nreaders) {
398 ": Outstanding opens (%d) on usb%d, leaking...\n",
399 mbus->nreaders, mbus->u_bus->busnum);
400 kref_get(&mbus->ref); /* Force leak */
403 mon_dissolve(mbus, mbus->u_bus);
404 kref_put(&mbus->ref, mon_bus_drop);
408 if (mbus->text_inited)
410 if (mbus->bin_inited)
413 mutex_unlock(&mon_lock);
419 module_init(mon_init);
420 module_exit(mon_exit);
422 MODULE_LICENSE("GPL");