blob: 812dc288bb8c1a081805313f742110e9ad2c0bc1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The USB Monitor, inspired by Dave Harding's USBMon.
3 *
4 * mon_main.c: Main file, module initiation and exit, registrations, etc.
Pete Zaitcevda5ca002005-08-16 15:16:46 -07005 *
6 * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020012#include <linux/usb/hcd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -070014#include <linux/notifier.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010015#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include "usb_mon.h"
Eric Lescouet27729aa2010-04-24 23:21:52 +020018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static void mon_stop(struct mon_bus *mbus);
21static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
22static void mon_bus_drop(struct kref *r);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080023static void mon_bus_init(struct usb_bus *ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010025DEFINE_MUTEX(mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Pete Zaitcevecb658d2007-04-11 13:47:26 -070027struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
29
30/*
31 * Link a reader into the bus.
32 *
33 * This must be called with mon_lock taken because of mbus->ref.
34 */
35void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
36{
37 unsigned long flags;
Pete Zaitcevecb658d2007-04-11 13:47:26 -070038 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 spin_lock_irqsave(&mbus->lock, flags);
41 if (mbus->nreaders == 0) {
Pete Zaitcevecb658d2007-04-11 13:47:26 -070042 if (mbus == &mon_bus0) {
43 list_for_each (p, &mon_buses) {
44 struct mon_bus *m1;
45 m1 = list_entry(p, struct mon_bus, bus_link);
46 m1->u_bus->monitored = 1;
47 }
48 } else {
49 mbus->u_bus->monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52 mbus->nreaders++;
53 list_add_tail(&r->r_link, &mbus->r_list);
54 spin_unlock_irqrestore(&mbus->lock, flags);
55
56 kref_get(&mbus->ref);
57}
58
59/*
60 * Unlink reader from the bus.
61 *
62 * This is called with mon_lock taken, so we can decrement mbus->ref.
63 */
64void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
65{
66 unsigned long flags;
67
68 spin_lock_irqsave(&mbus->lock, flags);
69 list_del(&r->r_link);
70 --mbus->nreaders;
71 if (mbus->nreaders == 0)
72 mon_stop(mbus);
73 spin_unlock_irqrestore(&mbus->lock, flags);
74
75 kref_put(&mbus->ref, mon_bus_drop);
76}
77
78/*
79 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -070080static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned long flags;
83 struct list_head *pos;
84 struct mon_reader *r;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 spin_lock_irqsave(&mbus->lock, flags);
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070087 mbus->cnt_events++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 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);
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 spin_unlock_irqrestore(&mbus->lock, flags);
93 return;
Pete Zaitcevecb658d2007-04-11 13:47:26 -070094}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Pete Zaitcevecb658d2007-04-11 13:47:26 -070096static void mon_submit(struct usb_bus *ubus, struct urb *urb)
97{
98 struct mon_bus *mbus;
99
100 if ((mbus = ubus->mon_bus) != NULL)
101 mon_bus_submit(mbus, urb);
102 mon_bus_submit(&mon_bus0, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700107static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700109 unsigned long flags;
110 struct list_head *pos;
111 struct mon_reader *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700113 spin_lock_irqsave(&mbus->lock, flags);
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700114 mbus->cnt_events++;
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);
118 }
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700119 spin_unlock_irqrestore(&mbus->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700123static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
124{
125 struct mon_bus *mbus;
126
127 if ((mbus = ubus->mon_bus) != NULL)
128 mon_bus_submit_error(mbus, urb, error);
129 mon_bus_submit_error(&mon_bus0, urb, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/*
133 */
Pete Zaitcev454459b2008-03-19 22:29:51 -0700134static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long flags;
137 struct list_head *pos;
138 struct mon_reader *r;
139
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700140 spin_lock_irqsave(&mbus->lock, flags);
141 mbus->cnt_events++;
142 list_for_each (pos, &mbus->r_list) {
143 r = list_entry(pos, struct mon_reader, r_link);
Alan Stern9347d512007-08-24 15:41:41 -0400144 r->rnf_complete(r->r_data, urb, status);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700145 }
146 spin_unlock_irqrestore(&mbus->lock, flags);
147}
148
Alan Stern9347d512007-08-24 15:41:41 -0400149static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700150{
151 struct mon_bus *mbus;
152
Pete Zaitcevc36d54a2007-08-14 00:42:53 -0700153 if ((mbus = ubus->mon_bus) != NULL)
Alan Stern9347d512007-08-24 15:41:41 -0400154 mon_bus_complete(mbus, urb, status);
155 mon_bus_complete(&mon_bus0, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/* int (*unlink_urb) (struct urb *urb, int status); */
159
160/*
161 * Stop monitoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
163static void mon_stop(struct mon_bus *mbus)
164{
Pete Zaitcevc36d54a2007-08-14 00:42:53 -0700165 struct usb_bus *ubus;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700166 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700168 if (mbus == &mon_bus0) {
169 list_for_each (p, &mon_buses) {
170 mbus = list_entry(p, struct mon_bus, bus_link);
171 /*
172 * We do not change nreaders here, so rely on mon_lock.
173 */
174 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
175 ubus->monitored = 0;
176 }
177 } else {
178 /*
179 * A stop can be called for a dissolved mon_bus in case of
180 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
181 */
182 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
183 ubus->monitored = 0;
184 mb();
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187}
188
189/*
190 * Add a USB bus (usually by a modprobe foo-hcd)
191 *
192 * This does not return an error code because the core cannot care less
193 * if monitoring is not established.
194 */
195static void mon_bus_add(struct usb_bus *ubus)
196{
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800197 mon_bus_init(ubus);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700198 mutex_lock(&mon_lock);
199 if (mon_bus0.nreaders != 0)
200 ubus->monitored = 1;
201 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
205 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
206 */
207static void mon_bus_remove(struct usb_bus *ubus)
208{
209 struct mon_bus *mbus = ubus->mon_bus;
210
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100211 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 list_del(&mbus->bus_link);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800213 if (mbus->text_inited)
214 mon_text_del(mbus);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700215 if (mbus->bin_inited)
216 mon_bin_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 mon_dissolve(mbus, ubus);
219 kref_put(&mbus->ref, mon_bus_drop);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100220 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700223static int mon_notify(struct notifier_block *self, unsigned long action,
224 void *dev)
225{
226 switch (action) {
227 case USB_BUS_ADD:
228 mon_bus_add(dev);
229 break;
230 case USB_BUS_REMOVE:
231 mon_bus_remove(dev);
232 }
233 return NOTIFY_OK;
234}
235
236static struct notifier_block mon_nb = {
237 .notifier_call = mon_notify,
238};
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Ops
242 */
243static struct usb_mon_operations mon_ops_0 = {
244 .urb_submit = mon_submit,
245 .urb_submit_error = mon_submit_error,
246 .urb_complete = mon_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
249/*
250 * Tear usb_bus and mon_bus apart.
251 */
252static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
253{
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ubus->monitored) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 ubus->monitored = 0;
257 mb();
258 }
259
260 ubus->mon_bus = NULL;
261 mbus->u_bus = NULL;
262 mb();
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700263
264 /* We want synchronize_irq() here, but that needs an argument. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 */
269static void mon_bus_drop(struct kref *r)
270{
271 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
272 kfree(mbus);
273}
274
275/*
276 * Initialize a bus for us:
277 * - allocate mon_bus
278 * - refcount USB bus struct
279 * - link
280 */
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800281static void mon_bus_init(struct usb_bus *ubus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100285 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 kref_init(&mbus->ref);
288 spin_lock_init(&mbus->lock);
289 INIT_LIST_HEAD(&mbus->r_list);
290
291 /*
Alan Stern17200582006-08-30 11:32:52 -0400292 * We don't need to take a reference to ubus, because we receive
293 * a notification if the bus is about to be removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 mbus->u_bus = ubus;
296 ubus->mon_bus = mbus;
297
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700298 mbus->text_inited = mon_text_add(mbus, ubus);
299 mbus->bin_inited = mon_bin_add(mbus, ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100301 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 list_add_tail(&mbus->bus_link, &mon_buses);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100303 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306err_alloc:
307 return;
308}
309
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700310static void mon_bus0_init(void)
311{
312 struct mon_bus *mbus = &mon_bus0;
313
314 kref_init(&mbus->ref);
315 spin_lock_init(&mbus->lock);
316 INIT_LIST_HEAD(&mbus->r_list);
317
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700318 mbus->text_inited = mon_text_add(mbus, NULL);
319 mbus->bin_inited = mon_bin_add(mbus, NULL);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700320}
321
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800322/*
323 * Search a USB bus by number. Notice that USB bus numbers start from one,
324 * which we may later use to identify "all" with zero.
325 *
326 * This function must be called with mon_lock held.
327 *
328 * This is obviously inefficient and may be revised in the future.
329 */
330struct mon_bus *mon_bus_lookup(unsigned int num)
331{
332 struct list_head *p;
333 struct mon_bus *mbus;
334
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700335 if (num == 0) {
336 return &mon_bus0;
337 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800338 list_for_each (p, &mon_buses) {
339 mbus = list_entry(p, struct mon_bus, bus_link);
340 if (mbus->u_bus->busnum == num) {
341 return mbus;
342 }
343 }
344 return NULL;
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static int __init mon_init(void)
348{
349 struct usb_bus *ubus;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800350 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800352 if ((rc = mon_text_init()) != 0)
353 goto err_text;
354 if ((rc = mon_bin_init()) != 0)
355 goto err_bin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700357 mon_bus0_init();
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (usb_mon_register(&mon_ops_0) != 0) {
360 printk(KERN_NOTICE TAG ": unable to register with the core\n");
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800361 rc = -ENODEV;
362 goto err_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 // MOD_INC_USE_COUNT(which_module?);
365
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100366 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800368 mon_bus_init(ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Arjan van de Venbb4e3b52008-09-22 15:00:10 -0700370 usb_register_notify(&mon_nb);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100371 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800373
374err_reg:
375 mon_bin_exit();
376err_bin:
377 mon_text_exit();
378err_text:
379 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382static void __exit mon_exit(void)
383{
384 struct mon_bus *mbus;
385 struct list_head *p;
386
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700387 usb_unregister_notify(&mon_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 usb_mon_deregister();
389
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100390 mutex_lock(&mon_lock);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 while (!list_empty(&mon_buses)) {
393 p = mon_buses.next;
394 mbus = list_entry(p, struct mon_bus, bus_link);
395 list_del(p);
396
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800397 if (mbus->text_inited)
398 mon_text_del(mbus);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700399 if (mbus->bin_inited)
400 mon_bin_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /*
403 * This never happens, because the open/close paths in
404 * file level maintain module use counters and so rmmod fails
405 * before reaching here. However, better be safe...
406 */
407 if (mbus->nreaders) {
408 printk(KERN_ERR TAG
409 ": Outstanding opens (%d) on usb%d, leaking...\n",
410 mbus->nreaders, mbus->u_bus->busnum);
411 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
412 }
413
414 mon_dissolve(mbus, mbus->u_bus);
415 kref_put(&mbus->ref, mon_bus_drop);
416 }
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700417
418 mbus = &mon_bus0;
419 if (mbus->text_inited)
420 mon_text_del(mbus);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700421 if (mbus->bin_inited)
422 mon_bin_del(mbus);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700423
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100424 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800426 mon_text_exit();
427 mon_bin_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430module_init(mon_init);
431module_exit(mon_exit);
432
433MODULE_LICENSE("GPL");