blob: 5e0ab4201c00894def56d3c182461173c89344c7 [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>
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -070012#include <linux/notifier.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include "usb_mon.h"
16#include "../core/hcd.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static void mon_stop(struct mon_bus *mbus);
19static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
20static void mon_bus_drop(struct kref *r);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080021static void mon_bus_init(struct usb_bus *ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010023DEFINE_MUTEX(mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Pete Zaitcevecb658d2007-04-11 13:47:26 -070025struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
27
28/*
29 * Link a reader into the bus.
30 *
31 * This must be called with mon_lock taken because of mbus->ref.
32 */
33void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
34{
35 unsigned long flags;
Pete Zaitcevecb658d2007-04-11 13:47:26 -070036 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 spin_lock_irqsave(&mbus->lock, flags);
39 if (mbus->nreaders == 0) {
Pete Zaitcevecb658d2007-04-11 13:47:26 -070040 if (mbus == &mon_bus0) {
41 list_for_each (p, &mon_buses) {
42 struct mon_bus *m1;
43 m1 = list_entry(p, struct mon_bus, bus_link);
44 m1->u_bus->monitored = 1;
45 }
46 } else {
47 mbus->u_bus->monitored = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50 mbus->nreaders++;
51 list_add_tail(&r->r_link, &mbus->r_list);
52 spin_unlock_irqrestore(&mbus->lock, flags);
53
54 kref_get(&mbus->ref);
55}
56
57/*
58 * Unlink reader from the bus.
59 *
60 * This is called with mon_lock taken, so we can decrement mbus->ref.
61 */
62void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
63{
64 unsigned long flags;
65
66 spin_lock_irqsave(&mbus->lock, flags);
67 list_del(&r->r_link);
68 --mbus->nreaders;
69 if (mbus->nreaders == 0)
70 mon_stop(mbus);
71 spin_unlock_irqrestore(&mbus->lock, flags);
72
73 kref_put(&mbus->ref, mon_bus_drop);
74}
75
76/*
77 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -070078static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned long flags;
81 struct list_head *pos;
82 struct mon_reader *r;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 spin_lock_irqsave(&mbus->lock, flags);
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070085 mbus->cnt_events++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 list_for_each (pos, &mbus->r_list) {
87 r = list_entry(pos, struct mon_reader, r_link);
88 r->rnf_submit(r->r_data, urb);
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock_irqrestore(&mbus->lock, flags);
91 return;
Pete Zaitcevecb658d2007-04-11 13:47:26 -070092}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Pete Zaitcevecb658d2007-04-11 13:47:26 -070094static void mon_submit(struct usb_bus *ubus, struct urb *urb)
95{
96 struct mon_bus *mbus;
97
98 if ((mbus = ubus->mon_bus) != NULL)
99 mon_bus_submit(mbus, urb);
100 mon_bus_submit(&mon_bus0, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/*
104 */
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700105static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700107 unsigned long flags;
108 struct list_head *pos;
109 struct mon_reader *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700111 spin_lock_irqsave(&mbus->lock, flags);
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700112 mbus->cnt_events++;
113 list_for_each (pos, &mbus->r_list) {
114 r = list_entry(pos, struct mon_reader, r_link);
115 r->rnf_error(r->r_data, urb, error);
116 }
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700117 spin_unlock_irqrestore(&mbus->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700121static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
122{
123 struct mon_bus *mbus;
124
125 if ((mbus = ubus->mon_bus) != NULL)
126 mon_bus_submit_error(mbus, urb, error);
127 mon_bus_submit_error(&mon_bus0, urb, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
131 */
Pete Zaitcev454459b2008-03-19 22:29:51 -0700132static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned long flags;
135 struct list_head *pos;
136 struct mon_reader *r;
137
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700138 spin_lock_irqsave(&mbus->lock, flags);
139 mbus->cnt_events++;
140 list_for_each (pos, &mbus->r_list) {
141 r = list_entry(pos, struct mon_reader, r_link);
Alan Stern9347d512007-08-24 15:41:41 -0400142 r->rnf_complete(r->r_data, urb, status);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700143 }
144 spin_unlock_irqrestore(&mbus->lock, flags);
145}
146
Alan Stern9347d512007-08-24 15:41:41 -0400147static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700148{
149 struct mon_bus *mbus;
150
Pete Zaitcevc36d54a2007-08-14 00:42:53 -0700151 if ((mbus = ubus->mon_bus) != NULL)
Alan Stern9347d512007-08-24 15:41:41 -0400152 mon_bus_complete(mbus, urb, status);
153 mon_bus_complete(&mon_bus0, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/* int (*unlink_urb) (struct urb *urb, int status); */
157
158/*
159 * Stop monitoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
161static void mon_stop(struct mon_bus *mbus)
162{
Pete Zaitcevc36d54a2007-08-14 00:42:53 -0700163 struct usb_bus *ubus;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700164 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700166 if (mbus == &mon_bus0) {
167 list_for_each (p, &mon_buses) {
168 mbus = list_entry(p, struct mon_bus, bus_link);
169 /*
170 * We do not change nreaders here, so rely on mon_lock.
171 */
172 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
173 ubus->monitored = 0;
174 }
175 } else {
176 /*
177 * A stop can be called for a dissolved mon_bus in case of
178 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
179 */
180 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
181 ubus->monitored = 0;
182 mb();
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185}
186
187/*
188 * Add a USB bus (usually by a modprobe foo-hcd)
189 *
190 * This does not return an error code because the core cannot care less
191 * if monitoring is not established.
192 */
193static void mon_bus_add(struct usb_bus *ubus)
194{
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800195 mon_bus_init(ubus);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700196 mutex_lock(&mon_lock);
197 if (mon_bus0.nreaders != 0)
198 ubus->monitored = 1;
199 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/*
203 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
204 */
205static void mon_bus_remove(struct usb_bus *ubus)
206{
207 struct mon_bus *mbus = ubus->mon_bus;
208
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100209 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 list_del(&mbus->bus_link);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800211 if (mbus->text_inited)
212 mon_text_del(mbus);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700213 if (mbus->bin_inited)
214 mon_bin_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 mon_dissolve(mbus, ubus);
217 kref_put(&mbus->ref, mon_bus_drop);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100218 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700221static int mon_notify(struct notifier_block *self, unsigned long action,
222 void *dev)
223{
224 switch (action) {
225 case USB_BUS_ADD:
226 mon_bus_add(dev);
227 break;
228 case USB_BUS_REMOVE:
229 mon_bus_remove(dev);
230 }
231 return NOTIFY_OK;
232}
233
234static struct notifier_block mon_nb = {
235 .notifier_call = mon_notify,
236};
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Ops
240 */
241static struct usb_mon_operations mon_ops_0 = {
242 .urb_submit = mon_submit,
243 .urb_submit_error = mon_submit_error,
244 .urb_complete = mon_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
247/*
248 * Tear usb_bus and mon_bus apart.
249 */
250static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
251{
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (ubus->monitored) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ubus->monitored = 0;
255 mb();
256 }
257
258 ubus->mon_bus = NULL;
259 mbus->u_bus = NULL;
260 mb();
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700261
262 /* We want synchronize_irq() here, but that needs an argument. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/*
266 */
267static void mon_bus_drop(struct kref *r)
268{
269 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
270 kfree(mbus);
271}
272
273/*
274 * Initialize a bus for us:
275 * - allocate mon_bus
276 * - refcount USB bus struct
277 * - link
278 */
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800279static void mon_bus_init(struct usb_bus *ubus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100283 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 kref_init(&mbus->ref);
286 spin_lock_init(&mbus->lock);
287 INIT_LIST_HEAD(&mbus->r_list);
288
289 /*
Alan Stern17200582006-08-30 11:32:52 -0400290 * We don't need to take a reference to ubus, because we receive
291 * a notification if the bus is about to be removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 mbus->u_bus = ubus;
294 ubus->mon_bus = mbus;
295
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700296 mbus->text_inited = mon_text_add(mbus, ubus);
297 mbus->bin_inited = mon_bin_add(mbus, ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100299 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 list_add_tail(&mbus->bus_link, &mon_buses);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100301 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304err_alloc:
305 return;
306}
307
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700308static void mon_bus0_init(void)
309{
310 struct mon_bus *mbus = &mon_bus0;
311
312 kref_init(&mbus->ref);
313 spin_lock_init(&mbus->lock);
314 INIT_LIST_HEAD(&mbus->r_list);
315
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700316 mbus->text_inited = mon_text_add(mbus, NULL);
317 mbus->bin_inited = mon_bin_add(mbus, NULL);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700318}
319
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800320/*
321 * Search a USB bus by number. Notice that USB bus numbers start from one,
322 * which we may later use to identify "all" with zero.
323 *
324 * This function must be called with mon_lock held.
325 *
326 * This is obviously inefficient and may be revised in the future.
327 */
328struct mon_bus *mon_bus_lookup(unsigned int num)
329{
330 struct list_head *p;
331 struct mon_bus *mbus;
332
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700333 if (num == 0) {
334 return &mon_bus0;
335 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800336 list_for_each (p, &mon_buses) {
337 mbus = list_entry(p, struct mon_bus, bus_link);
338 if (mbus->u_bus->busnum == num) {
339 return mbus;
340 }
341 }
342 return NULL;
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int __init mon_init(void)
346{
347 struct usb_bus *ubus;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800348 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800350 if ((rc = mon_text_init()) != 0)
351 goto err_text;
352 if ((rc = mon_bin_init()) != 0)
353 goto err_bin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700355 mon_bus0_init();
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (usb_mon_register(&mon_ops_0) != 0) {
358 printk(KERN_NOTICE TAG ": unable to register with the core\n");
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800359 rc = -ENODEV;
360 goto err_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 // MOD_INC_USE_COUNT(which_module?);
363
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700364
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100365 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800367 mon_bus_init(ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Arjan van de Venbb4e3b52008-09-22 15:00:10 -0700369 usb_register_notify(&mon_nb);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100370 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800372
373err_reg:
374 mon_bin_exit();
375err_bin:
376 mon_text_exit();
377err_text:
378 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381static void __exit mon_exit(void)
382{
383 struct mon_bus *mbus;
384 struct list_head *p;
385
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700386 usb_unregister_notify(&mon_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 usb_mon_deregister();
388
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100389 mutex_lock(&mon_lock);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 while (!list_empty(&mon_buses)) {
392 p = mon_buses.next;
393 mbus = list_entry(p, struct mon_bus, bus_link);
394 list_del(p);
395
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800396 if (mbus->text_inited)
397 mon_text_del(mbus);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700398 if (mbus->bin_inited)
399 mon_bin_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * This never happens, because the open/close paths in
403 * file level maintain module use counters and so rmmod fails
404 * before reaching here. However, better be safe...
405 */
406 if (mbus->nreaders) {
407 printk(KERN_ERR TAG
408 ": Outstanding opens (%d) on usb%d, leaking...\n",
409 mbus->nreaders, mbus->u_bus->busnum);
410 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
411 }
412
413 mon_dissolve(mbus, mbus->u_bus);
414 kref_put(&mbus->ref, mon_bus_drop);
415 }
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700416
417 mbus = &mon_bus0;
418 if (mbus->text_inited)
419 mon_text_del(mbus);
Pete Zaitcevce7cd137f2007-05-03 16:51:16 -0700420 if (mbus->bin_inited)
421 mon_bin_del(mbus);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700422
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100423 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800425 mon_text_exit();
426 mon_bin_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429module_init(mon_init);
430module_exit(mon_exit);
431
432MODULE_LICENSE("GPL");