blob: 8a1df2c9c73eb137a4bdbab9dc53770b4da7fbc8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/smp_lock.h>
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -070013#include <linux/notifier.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include "usb_mon.h"
17#include "../core/hcd.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static void mon_stop(struct mon_bus *mbus);
20static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
21static void mon_bus_drop(struct kref *r);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080022static void mon_bus_init(struct usb_bus *ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010024DEFINE_MUTEX(mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Pete Zaitcevecb658d2007-04-11 13:47:26 -070026struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
28
29/*
30 * Link a reader into the bus.
31 *
32 * This must be called with mon_lock taken because of mbus->ref.
33 */
34void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
35{
36 unsigned long flags;
Pete Zaitcevecb658d2007-04-11 13:47:26 -070037 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 spin_lock_irqsave(&mbus->lock, flags);
40 if (mbus->nreaders == 0) {
Pete Zaitcevecb658d2007-04-11 13:47:26 -070041 if (mbus == &mon_bus0) {
42 list_for_each (p, &mon_buses) {
43 struct mon_bus *m1;
44 m1 = list_entry(p, struct mon_bus, bus_link);
45 m1->u_bus->monitored = 1;
46 }
47 } else {
48 mbus->u_bus->monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51 mbus->nreaders++;
52 list_add_tail(&r->r_link, &mbus->r_list);
53 spin_unlock_irqrestore(&mbus->lock, flags);
54
55 kref_get(&mbus->ref);
56}
57
58/*
59 * Unlink reader from the bus.
60 *
61 * This is called with mon_lock taken, so we can decrement mbus->ref.
62 */
63void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
64{
65 unsigned long flags;
66
67 spin_lock_irqsave(&mbus->lock, flags);
68 list_del(&r->r_link);
69 --mbus->nreaders;
70 if (mbus->nreaders == 0)
71 mon_stop(mbus);
72 spin_unlock_irqrestore(&mbus->lock, flags);
73
74 kref_put(&mbus->ref, mon_bus_drop);
75}
76
77/*
78 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -070079static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned long flags;
82 struct list_head *pos;
83 struct mon_reader *r;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 spin_lock_irqsave(&mbus->lock, flags);
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070086 mbus->cnt_events++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 list_for_each (pos, &mbus->r_list) {
88 r = list_entry(pos, struct mon_reader, r_link);
89 r->rnf_submit(r->r_data, urb);
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 spin_unlock_irqrestore(&mbus->lock, flags);
92 return;
Pete Zaitcevecb658d2007-04-11 13:47:26 -070093}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Pete Zaitcevecb658d2007-04-11 13:47:26 -070095static void mon_submit(struct usb_bus *ubus, struct urb *urb)
96{
97 struct mon_bus *mbus;
98
99 if ((mbus = ubus->mon_bus) != NULL)
100 mon_bus_submit(mbus, urb);
101 mon_bus_submit(&mon_bus0, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/*
105 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700106static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700108 unsigned long flags;
109 struct list_head *pos;
110 struct mon_reader *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700112 spin_lock_irqsave(&mbus->lock, flags);
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700113 mbus->cnt_events++;
114 list_for_each (pos, &mbus->r_list) {
115 r = list_entry(pos, struct mon_reader, r_link);
116 r->rnf_error(r->r_data, urb, error);
117 }
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700118 spin_unlock_irqrestore(&mbus->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700122static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
123{
124 struct mon_bus *mbus;
125
126 if ((mbus = ubus->mon_bus) != NULL)
127 mon_bus_submit_error(mbus, urb, error);
128 mon_bus_submit_error(&mon_bus0, urb, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/*
132 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700133static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned long flags;
136 struct list_head *pos;
137 struct mon_reader *r;
138
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700139 spin_lock_irqsave(&mbus->lock, flags);
140 mbus->cnt_events++;
141 list_for_each (pos, &mbus->r_list) {
142 r = list_entry(pos, struct mon_reader, r_link);
143 r->rnf_complete(r->r_data, urb);
144 }
145 spin_unlock_irqrestore(&mbus->lock, flags);
146}
147
148static void mon_complete(struct usb_bus *ubus, struct urb *urb)
149{
150 struct mon_bus *mbus;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 mbus = ubus->mon_bus;
153 if (mbus == NULL) {
154 /*
155 * This should not happen.
156 * At this point we do not even know the bus number...
157 */
158 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
159 urb->pipe);
160 return;
161 }
162
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700163 mon_bus_complete(mbus, urb);
164 mon_bus_complete(&mon_bus0, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/* int (*unlink_urb) (struct urb *urb, int status); */
168
169/*
170 * Stop monitoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172static void mon_stop(struct mon_bus *mbus)
173{
174 struct usb_bus *ubus = mbus->u_bus;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700175 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700177 if (mbus == &mon_bus0) {
178 list_for_each (p, &mon_buses) {
179 mbus = list_entry(p, struct mon_bus, bus_link);
180 /*
181 * We do not change nreaders here, so rely on mon_lock.
182 */
183 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
184 ubus->monitored = 0;
185 }
186 } else {
187 /*
188 * A stop can be called for a dissolved mon_bus in case of
189 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
190 */
191 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
192 ubus->monitored = 0;
193 mb();
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196}
197
198/*
199 * Add a USB bus (usually by a modprobe foo-hcd)
200 *
201 * This does not return an error code because the core cannot care less
202 * if monitoring is not established.
203 */
204static void mon_bus_add(struct usb_bus *ubus)
205{
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800206 mon_bus_init(ubus);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700207 mutex_lock(&mon_lock);
208 if (mon_bus0.nreaders != 0)
209 ubus->monitored = 1;
210 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/*
214 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
215 */
216static void mon_bus_remove(struct usb_bus *ubus)
217{
218 struct mon_bus *mbus = ubus->mon_bus;
219
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100220 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 list_del(&mbus->bus_link);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800222 if (mbus->text_inited)
223 mon_text_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 mon_dissolve(mbus, ubus);
226 kref_put(&mbus->ref, mon_bus_drop);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100227 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700230static int mon_notify(struct notifier_block *self, unsigned long action,
231 void *dev)
232{
233 switch (action) {
234 case USB_BUS_ADD:
235 mon_bus_add(dev);
236 break;
237 case USB_BUS_REMOVE:
238 mon_bus_remove(dev);
239 }
240 return NOTIFY_OK;
241}
242
243static struct notifier_block mon_nb = {
244 .notifier_call = mon_notify,
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
248 * Ops
249 */
250static struct usb_mon_operations mon_ops_0 = {
251 .urb_submit = mon_submit,
252 .urb_submit_error = mon_submit_error,
253 .urb_complete = mon_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
256/*
257 * Tear usb_bus and mon_bus apart.
258 */
259static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
260{
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (ubus->monitored) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ubus->monitored = 0;
264 mb();
265 }
266
267 ubus->mon_bus = NULL;
268 mbus->u_bus = NULL;
269 mb();
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700270
271 /* We want synchronize_irq() here, but that needs an argument. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/*
275 */
276static void mon_bus_drop(struct kref *r)
277{
278 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
279 kfree(mbus);
280}
281
282/*
283 * Initialize a bus for us:
284 * - allocate mon_bus
285 * - refcount USB bus struct
286 * - link
287 */
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800288static void mon_bus_init(struct usb_bus *ubus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100292 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 kref_init(&mbus->ref);
295 spin_lock_init(&mbus->lock);
296 INIT_LIST_HEAD(&mbus->r_list);
297
298 /*
Alan Stern17200582006-08-30 11:32:52 -0400299 * We don't need to take a reference to ubus, because we receive
300 * a notification if the bus is about to be removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mbus->u_bus = ubus;
303 ubus->mon_bus = mbus;
304
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700305 mbus->text_inited = mon_text_add(mbus, ubus->busnum);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800306 // mon_bin_add(...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100308 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 list_add_tail(&mbus->bus_link, &mon_buses);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100310 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313err_alloc:
314 return;
315}
316
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700317static void mon_bus0_init(void)
318{
319 struct mon_bus *mbus = &mon_bus0;
320
321 kref_init(&mbus->ref);
322 spin_lock_init(&mbus->lock);
323 INIT_LIST_HEAD(&mbus->r_list);
324
325 mbus->text_inited = mon_text_add(mbus, 0);
326 // mbus->bin_inited = mon_bin_add(mbus, 0);
327}
328
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800329/*
330 * Search a USB bus by number. Notice that USB bus numbers start from one,
331 * which we may later use to identify "all" with zero.
332 *
333 * This function must be called with mon_lock held.
334 *
335 * This is obviously inefficient and may be revised in the future.
336 */
337struct mon_bus *mon_bus_lookup(unsigned int num)
338{
339 struct list_head *p;
340 struct mon_bus *mbus;
341
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700342 if (num == 0) {
343 return &mon_bus0;
344 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800345 list_for_each (p, &mon_buses) {
346 mbus = list_entry(p, struct mon_bus, bus_link);
347 if (mbus->u_bus->busnum == num) {
348 return mbus;
349 }
350 }
351 return NULL;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static int __init mon_init(void)
355{
356 struct usb_bus *ubus;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800357 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800359 if ((rc = mon_text_init()) != 0)
360 goto err_text;
361 if ((rc = mon_bin_init()) != 0)
362 goto err_bin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700364 mon_bus0_init();
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (usb_mon_register(&mon_ops_0) != 0) {
367 printk(KERN_NOTICE TAG ": unable to register with the core\n");
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800368 rc = -ENODEV;
369 goto err_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 // MOD_INC_USE_COUNT(which_module?);
372
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700373 usb_register_notify(&mon_nb);
374
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100375 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800377 mon_bus_init(ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100379 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800381
382err_reg:
383 mon_bin_exit();
384err_bin:
385 mon_text_exit();
386err_text:
387 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void __exit mon_exit(void)
391{
392 struct mon_bus *mbus;
393 struct list_head *p;
394
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700395 usb_unregister_notify(&mon_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 usb_mon_deregister();
397
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100398 mutex_lock(&mon_lock);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 while (!list_empty(&mon_buses)) {
401 p = mon_buses.next;
402 mbus = list_entry(p, struct mon_bus, bus_link);
403 list_del(p);
404
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800405 if (mbus->text_inited)
406 mon_text_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /*
409 * This never happens, because the open/close paths in
410 * file level maintain module use counters and so rmmod fails
411 * before reaching here. However, better be safe...
412 */
413 if (mbus->nreaders) {
414 printk(KERN_ERR TAG
415 ": Outstanding opens (%d) on usb%d, leaking...\n",
416 mbus->nreaders, mbus->u_bus->busnum);
417 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
418 }
419
420 mon_dissolve(mbus, mbus->u_bus);
421 kref_put(&mbus->ref, mon_bus_drop);
422 }
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700423
424 mbus = &mon_bus0;
425 if (mbus->text_inited)
426 mon_text_del(mbus);
427
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100428 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800430 mon_text_exit();
431 mon_bin_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434module_init(mon_init);
435module_exit(mon_exit);
436
437MODULE_LICENSE("GPL");