blob: e4af18b93c7d85b7bc512872d7cb46f88bd07882 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.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 Zaitcev454459b2008-03-19 22:29:51 -0700133static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
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);
Alan Stern9347d512007-08-24 15:41:41 -0400143 r->rnf_complete(r->r_data, urb, status);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700144 }
145 spin_unlock_irqrestore(&mbus->lock, flags);
146}
147
Alan Stern9347d512007-08-24 15:41:41 -0400148static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700149{
150 struct mon_bus *mbus;
151
Pete Zaitcevc36d54a2007-08-14 00:42:53 -0700152 if ((mbus = ubus->mon_bus) != NULL)
Alan Stern9347d512007-08-24 15:41:41 -0400153 mon_bus_complete(mbus, urb, status);
154 mon_bus_complete(&mon_bus0, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/* int (*unlink_urb) (struct urb *urb, int status); */
158
159/*
160 * Stop monitoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
162static void mon_stop(struct mon_bus *mbus)
163{
Pete Zaitcevc36d54a2007-08-14 00:42:53 -0700164 struct usb_bus *ubus;
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700165 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700167 if (mbus == &mon_bus0) {
168 list_for_each (p, &mon_buses) {
169 mbus = list_entry(p, struct mon_bus, bus_link);
170 /*
171 * We do not change nreaders here, so rely on mon_lock.
172 */
173 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
174 ubus->monitored = 0;
175 }
176 } else {
177 /*
178 * A stop can be called for a dissolved mon_bus in case of
179 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
180 */
181 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
182 ubus->monitored = 0;
183 mb();
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186}
187
188/*
189 * Add a USB bus (usually by a modprobe foo-hcd)
190 *
191 * This does not return an error code because the core cannot care less
192 * if monitoring is not established.
193 */
194static void mon_bus_add(struct usb_bus *ubus)
195{
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800196 mon_bus_init(ubus);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700197 mutex_lock(&mon_lock);
198 if (mon_bus0.nreaders != 0)
199 ubus->monitored = 1;
200 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/*
204 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
205 */
206static void mon_bus_remove(struct usb_bus *ubus)
207{
208 struct mon_bus *mbus = ubus->mon_bus;
209
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100210 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 list_del(&mbus->bus_link);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800212 if (mbus->text_inited)
213 mon_text_del(mbus);
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700214 if (mbus->bin_inited)
215 mon_bin_del(mbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 mon_dissolve(mbus, ubus);
218 kref_put(&mbus->ref, mon_bus_drop);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100219 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Greg Kroah-Hartman72adaa92005-06-20 21:15:16 -0700222static int mon_notify(struct notifier_block *self, unsigned long action,
223 void *dev)
224{
225 switch (action) {
226 case USB_BUS_ADD:
227 mon_bus_add(dev);
228 break;
229 case USB_BUS_REMOVE:
230 mon_bus_remove(dev);
231 }
232 return NOTIFY_OK;
233}
234
235static struct notifier_block mon_nb = {
236 .notifier_call = mon_notify,
237};
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Ops
241 */
242static struct usb_mon_operations mon_ops_0 = {
243 .urb_submit = mon_submit,
244 .urb_submit_error = mon_submit_error,
245 .urb_complete = mon_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
248/*
249 * Tear usb_bus and mon_bus apart.
250 */
251static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
252{
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (ubus->monitored) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ubus->monitored = 0;
256 mb();
257 }
258
259 ubus->mon_bus = NULL;
260 mbus->u_bus = NULL;
261 mb();
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700262
263 /* We want synchronize_irq() here, but that needs an argument. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*
267 */
268static void mon_bus_drop(struct kref *r)
269{
270 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
271 kfree(mbus);
272}
273
274/*
275 * Initialize a bus for us:
276 * - allocate mon_bus
277 * - refcount USB bus struct
278 * - link
279 */
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800280static void mon_bus_init(struct usb_bus *ubus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct mon_bus *mbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100284 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kref_init(&mbus->ref);
287 spin_lock_init(&mbus->lock);
288 INIT_LIST_HEAD(&mbus->r_list);
289
290 /*
Alan Stern17200582006-08-30 11:32:52 -0400291 * We don't need to take a reference to ubus, because we receive
292 * a notification if the bus is about to be removed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 mbus->u_bus = ubus;
295 ubus->mon_bus = mbus;
296
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700297 mbus->text_inited = mon_text_add(mbus, ubus);
298 mbus->bin_inited = mon_bin_add(mbus, ubus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100300 mutex_lock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 list_add_tail(&mbus->bus_link, &mon_buses);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100302 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305err_alloc:
306 return;
307}
308
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700309static void mon_bus0_init(void)
310{
311 struct mon_bus *mbus = &mon_bus0;
312
313 kref_init(&mbus->ref);
314 spin_lock_init(&mbus->lock);
315 INIT_LIST_HEAD(&mbus->r_list);
316
Pete Zaitcevce7cd132007-05-03 16:51:16 -0700317 mbus->text_inited = mon_text_add(mbus, NULL);
318 mbus->bin_inited = mon_bin_add(mbus, NULL);
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700319}
320
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800321/*
322 * Search a USB bus by number. Notice that USB bus numbers start from one,
323 * which we may later use to identify "all" with zero.
324 *
325 * This function must be called with mon_lock held.
326 *
327 * This is obviously inefficient and may be revised in the future.
328 */
329struct mon_bus *mon_bus_lookup(unsigned int num)
330{
331 struct list_head *p;
332 struct mon_bus *mbus;
333
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700334 if (num == 0) {
335 return &mon_bus0;
336 }
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800337 list_for_each (p, &mon_buses) {
338 mbus = list_entry(p, struct mon_bus, bus_link);
339 if (mbus->u_bus->busnum == num) {
340 return mbus;
341 }
342 }
343 return NULL;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static int __init mon_init(void)
347{
348 struct usb_bus *ubus;
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800349 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800351 if ((rc = mon_text_init()) != 0)
352 goto err_text;
353 if ((rc = mon_bin_init()) != 0)
354 goto err_bin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Pete Zaitcevecb658d2007-04-11 13:47:26 -0700356 mon_bus0_init();
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (usb_mon_register(&mon_ops_0) != 0) {
359 printk(KERN_NOTICE TAG ": unable to register with the core\n");
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800360 rc = -ENODEV;
361 goto err_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 // MOD_INC_USE_COUNT(which_module?);
364
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 Zaitcevce7cd132007-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 Zaitcevce7cd132007-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");