xref: /linux/drivers/usb/mon/mon_main.c (revision 0c6cae0f56239e3274b30dbe5b362cd297ccacc7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * The USB Monitor, inspired by Dave Harding's USBMon.
4  *
5  * mon_main.c: Main file, module initiation and exit, registrations, etc.
6  *
7  * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
8  */
9 
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>
17 
18 #include "usb_mon.h"
19 
20 
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);
25 
26 DEFINE_MUTEX(mon_lock);
27 
28 struct mon_bus mon_bus0;		/* Pseudo bus meaning "all buses" */
29 static LIST_HEAD(mon_buses);		/* All buses we know: struct mon_bus */
30 
31 /*
32  * Link a reader into the bus.
33  *
34  * This must be called with mon_lock taken because of mbus->ref.
35  */
36 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
37 {
38 	unsigned long flags;
39 	struct list_head *p;
40 
41 	spin_lock_irqsave(&mbus->lock, flags);
42 	if (mbus->nreaders == 0) {
43 		if (mbus == &mon_bus0) {
44 			list_for_each (p, &mon_buses) {
45 				struct mon_bus *m1;
46 				m1 = list_entry(p, struct mon_bus, bus_link);
47 				m1->u_bus->monitored = 1;
48 			}
49 		} else {
50 			mbus->u_bus->monitored = 1;
51 		}
52 	}
53 	mbus->nreaders++;
54 	list_add_tail(&r->r_link, &mbus->r_list);
55 	spin_unlock_irqrestore(&mbus->lock, flags);
56 
57 	kref_get(&mbus->ref);
58 }
59 
60 /*
61  * Unlink reader from the bus.
62  *
63  * This is called with mon_lock taken, so we can decrement mbus->ref.
64  */
65 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
66 {
67 	unsigned long flags;
68 
69 	spin_lock_irqsave(&mbus->lock, flags);
70 	list_del(&r->r_link);
71 	--mbus->nreaders;
72 	if (mbus->nreaders == 0)
73 		mon_stop(mbus);
74 	spin_unlock_irqrestore(&mbus->lock, flags);
75 
76 	kref_put(&mbus->ref, mon_bus_drop);
77 }
78 
79 /*
80  */
81 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
82 {
83 	unsigned long flags;
84 	struct mon_reader *r;
85 
86 	spin_lock_irqsave(&mbus->lock, flags);
87 	mbus->cnt_events++;
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);
91 }
92 
93 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
94 {
95 	struct mon_bus *mbus;
96 
97 	mbus = ubus->mon_bus;
98 	if (mbus != NULL)
99 		mon_bus_submit(mbus, urb);
100 	mon_bus_submit(&mon_bus0, urb);
101 }
102 
103 /*
104  */
105 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
106 {
107 	unsigned long flags;
108 	struct mon_reader *r;
109 
110 	spin_lock_irqsave(&mbus->lock, flags);
111 	mbus->cnt_events++;
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);
115 }
116 
117 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
118 {
119 	struct mon_bus *mbus;
120 
121 	mbus = ubus->mon_bus;
122 	if (mbus != NULL)
123 		mon_bus_submit_error(mbus, urb, error);
124 	mon_bus_submit_error(&mon_bus0, urb, error);
125 }
126 
127 /*
128  */
129 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
130 {
131 	unsigned long flags;
132 	struct mon_reader *r;
133 
134 	spin_lock_irqsave(&mbus->lock, flags);
135 	mbus->cnt_events++;
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);
139 }
140 
141 static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
142 {
143 	struct mon_bus *mbus;
144 
145 	mbus = ubus->mon_bus;
146 	if (mbus != NULL)
147 		mon_bus_complete(mbus, urb, status);
148 	mon_bus_complete(&mon_bus0, urb, status);
149 }
150 
151 /* int (*unlink_urb) (struct urb *urb, int status); */
152 
153 /*
154  * Stop monitoring.
155  */
156 static void mon_stop(struct mon_bus *mbus)
157 {
158 	struct usb_bus *ubus;
159 
160 	if (mbus == &mon_bus0) {
161 		list_for_each_entry(mbus, &mon_buses, bus_link) {
162 			/*
163 			 * We do not change nreaders here, so rely on mon_lock.
164 			 */
165 			if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
166 				ubus->monitored = 0;
167 		}
168 	} else {
169 		/*
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.
172 		 */
173 		if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
174 			ubus->monitored = 0;
175 			mb();
176 		}
177 	}
178 }
179 
180 /*
181  * Add a USB bus (usually by a modprobe foo-hcd)
182  *
183  * This does not return an error code because the core cannot care less
184  * if monitoring is not established.
185  */
186 static void mon_bus_add(struct usb_bus *ubus)
187 {
188 	mon_bus_init(ubus);
189 	mutex_lock(&mon_lock);
190 	if (mon_bus0.nreaders != 0)
191 		ubus->monitored = 1;
192 	mutex_unlock(&mon_lock);
193 }
194 
195 /*
196  * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
197  */
198 static void mon_bus_remove(struct usb_bus *ubus)
199 {
200 	struct mon_bus *mbus = ubus->mon_bus;
201 
202 	if (mbus == NULL)
203 		return;
204 
205 	mutex_lock(&mon_lock);
206 	list_del(&mbus->bus_link);
207 	if (mbus->text_inited)
208 		mon_text_del(mbus);
209 	if (mbus->bin_inited)
210 		mon_bin_del(mbus);
211 
212 	mon_dissolve(mbus, ubus);
213 	kref_put(&mbus->ref, mon_bus_drop);
214 	mutex_unlock(&mon_lock);
215 }
216 
217 static int mon_notify(struct notifier_block *self, unsigned long action,
218 		      void *dev)
219 {
220 	switch (action) {
221 	case USB_BUS_ADD:
222 		mon_bus_add(dev);
223 		break;
224 	case USB_BUS_REMOVE:
225 		mon_bus_remove(dev);
226 	}
227 	return NOTIFY_OK;
228 }
229 
230 static struct notifier_block mon_nb = {
231 	.notifier_call = 	mon_notify,
232 };
233 
234 /*
235  * Ops
236  */
237 static const struct usb_mon_operations mon_ops_0 = {
238 	.urb_submit =	mon_submit,
239 	.urb_submit_error = mon_submit_error,
240 	.urb_complete =	mon_complete,
241 };
242 
243 /*
244  * Tear usb_bus and mon_bus apart.
245  */
246 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
247 {
248 
249 	if (ubus->monitored) {
250 		ubus->monitored = 0;
251 		mb();
252 	}
253 
254 	ubus->mon_bus = NULL;
255 	mbus->u_bus = NULL;
256 	mb();
257 
258 	/* We want synchronize_irq() here, but that needs an argument. */
259 }
260 
261 /*
262  */
263 static void mon_bus_drop(struct kref *r)
264 {
265 	struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
266 	kfree(mbus);
267 }
268 
269 /*
270  * Initialize a bus for us:
271  *  - allocate mon_bus
272  *  - refcount USB bus struct
273  *  - link
274  */
275 static void mon_bus_init(struct usb_bus *ubus)
276 {
277 	struct mon_bus *mbus;
278 
279 	mbus = kzalloc_obj(struct mon_bus);
280 	if (mbus == NULL)
281 		goto err_alloc;
282 	kref_init(&mbus->ref);
283 	spin_lock_init(&mbus->lock);
284 	INIT_LIST_HEAD(&mbus->r_list);
285 
286 	/*
287 	 * We don't need to take a reference to ubus, because we receive
288 	 * a notification if the bus is about to be removed.
289 	 */
290 	mbus->u_bus = ubus;
291 	ubus->mon_bus = mbus;
292 
293 	mbus->text_inited = mon_text_add(mbus, ubus);
294 	mbus->bin_inited = mon_bin_add(mbus, ubus);
295 
296 	mutex_lock(&mon_lock);
297 	list_add_tail(&mbus->bus_link, &mon_buses);
298 	mutex_unlock(&mon_lock);
299 	return;
300 
301 err_alloc:
302 	return;
303 }
304 
305 static void mon_bus0_init(void)
306 {
307 	struct mon_bus *mbus = &mon_bus0;
308 
309 	kref_init(&mbus->ref);
310 	spin_lock_init(&mbus->lock);
311 	INIT_LIST_HEAD(&mbus->r_list);
312 
313 	mbus->text_inited = mon_text_add(mbus, NULL);
314 	mbus->bin_inited = mon_bin_add(mbus, NULL);
315 }
316 
317 /*
318  * Search a USB bus by number. Notice that USB bus numbers start from one,
319  * which we may later use to identify "all" with zero.
320  *
321  * This function must be called with mon_lock held.
322  *
323  * This is obviously inefficient and may be revised in the future.
324  */
325 struct mon_bus *mon_bus_lookup(unsigned int num)
326 {
327 	struct mon_bus *mbus;
328 
329 	if (num == 0) {
330 		return &mon_bus0;
331 	}
332 	list_for_each_entry(mbus, &mon_buses, bus_link) {
333 		if (mbus->u_bus->busnum == num) {
334 			return mbus;
335 		}
336 	}
337 	return NULL;
338 }
339 
340 static int __init mon_init(void)
341 {
342 	struct usb_bus *ubus;
343 	int rc, id;
344 
345 	if ((rc = mon_text_init()) != 0)
346 		goto err_text;
347 	if ((rc = mon_bin_init()) != 0)
348 		goto err_bin;
349 
350 	mon_bus0_init();
351 
352 	if (usb_mon_register(&mon_ops_0) != 0) {
353 		printk(KERN_NOTICE TAG ": unable to register with the core\n");
354 		rc = -ENODEV;
355 		goto err_reg;
356 	}
357 	// MOD_INC_USE_COUNT(which_module?);
358 
359 	mutex_lock(&usb_bus_idr_lock);
360 	idr_for_each_entry(&usb_bus_idr, ubus, id)
361 		mon_bus_init(ubus);
362 	usb_register_notify(&mon_nb);
363 	mutex_unlock(&usb_bus_idr_lock);
364 	return 0;
365 
366 err_reg:
367 	mon_bin_exit();
368 err_bin:
369 	mon_text_exit();
370 err_text:
371 	return rc;
372 }
373 
374 static void __exit mon_exit(void)
375 {
376 	struct mon_bus *mbus;
377 	struct list_head *p;
378 
379 	usb_unregister_notify(&mon_nb);
380 	usb_mon_deregister();
381 
382 	mutex_lock(&mon_lock);
383 
384 	while (!list_empty(&mon_buses)) {
385 		p = mon_buses.next;
386 		mbus = list_entry(p, struct mon_bus, bus_link);
387 		list_del(p);
388 
389 		if (mbus->text_inited)
390 			mon_text_del(mbus);
391 		if (mbus->bin_inited)
392 			mon_bin_del(mbus);
393 
394 		/*
395 		 * This never happens, because the open/close paths in
396 		 * file level maintain module use counters and so rmmod fails
397 		 * before reaching here. However, better be safe...
398 		 */
399 		if (mbus->nreaders) {
400 			printk(KERN_ERR TAG
401 			    ": Outstanding opens (%d) on usb%d, leaking...\n",
402 			    mbus->nreaders, mbus->u_bus->busnum);
403 			kref_get(&mbus->ref); /* Force leak */
404 		}
405 
406 		mon_dissolve(mbus, mbus->u_bus);
407 		kref_put(&mbus->ref, mon_bus_drop);
408 	}
409 
410 	mbus = &mon_bus0;
411 	if (mbus->text_inited)
412 		mon_text_del(mbus);
413 	if (mbus->bin_inited)
414 		mon_bin_del(mbus);
415 
416 	mutex_unlock(&mon_lock);
417 
418 	mon_text_exit();
419 	mon_bin_exit();
420 }
421 
422 module_init(mon_init);
423 module_exit(mon_exit);
424 
425 MODULE_DESCRIPTION("USB Monitor");
426 MODULE_LICENSE("GPL");
427