Linux v2.6.13
[linux-2.6/next.git] / drivers / usb / mon / mon_main.c
blob508a21028db420c41a3d188155d3cfb0410146e3
1 /*
2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * mon_main.c: Main file, module initiation and exit, registrations, etc.
6 * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/debugfs.h>
13 #include <linux/smp_lock.h>
15 #include "usb_mon.h"
16 #include "../core/hcd.h"
18 static void mon_submit(struct usb_bus *ubus, struct urb *urb);
19 static void mon_complete(struct usb_bus *ubus, struct urb *urb);
20 static void mon_stop(struct mon_bus *mbus);
21 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
22 static void mon_bus_drop(struct kref *r);
23 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
25 DECLARE_MUTEX(mon_lock);
27 static struct dentry *mon_dir; /* /dbg/usbmon */
28 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
31 * Link a reader into the bus.
33 * This must be called with mon_lock taken because of mbus->ref.
35 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
37 unsigned long flags;
38 struct usb_bus *ubus;
40 spin_lock_irqsave(&mbus->lock, flags);
41 if (mbus->nreaders == 0) {
42 ubus = mbus->u_bus;
43 if (ubus->monitored) {
45 * Something is really broken, refuse to go on and
46 * possibly corrupt ops pointers or worse.
48 printk(KERN_ERR TAG ": bus %d is already monitored\n",
49 ubus->busnum);
50 spin_unlock_irqrestore(&mbus->lock, flags);
51 return;
53 ubus->monitored = 1;
55 mbus->nreaders++;
56 list_add_tail(&r->r_link, &mbus->r_list);
57 spin_unlock_irqrestore(&mbus->lock, flags);
59 kref_get(&mbus->ref);
63 * Unlink reader from the bus.
65 * This is called with mon_lock taken, so we can decrement mbus->ref.
67 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
69 unsigned long flags;
71 spin_lock_irqsave(&mbus->lock, flags);
72 list_del(&r->r_link);
73 --mbus->nreaders;
74 if (mbus->nreaders == 0)
75 mon_stop(mbus);
76 spin_unlock_irqrestore(&mbus->lock, flags);
78 kref_put(&mbus->ref, mon_bus_drop);
83 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
85 struct mon_bus *mbus;
86 unsigned long flags;
87 struct list_head *pos;
88 struct mon_reader *r;
90 mbus = ubus->mon_bus;
91 if (mbus == NULL)
92 goto out_unlocked;
94 spin_lock_irqsave(&mbus->lock, flags);
95 if (mbus->nreaders == 0)
96 goto out_locked;
98 list_for_each (pos, &mbus->r_list) {
99 r = list_entry(pos, struct mon_reader, r_link);
100 r->rnf_submit(r->r_data, urb);
103 spin_unlock_irqrestore(&mbus->lock, flags);
104 return;
106 out_locked:
107 spin_unlock_irqrestore(&mbus->lock, flags);
108 out_unlocked:
109 return;
114 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
116 struct mon_bus *mbus;
118 mbus = ubus->mon_bus;
119 if (mbus == NULL)
120 goto out_unlocked;
123 * XXX Capture the error code and the 'E' event.
126 return;
128 out_unlocked:
129 return;
134 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
136 struct mon_bus *mbus;
137 unsigned long flags;
138 struct list_head *pos;
139 struct mon_reader *r;
141 mbus = ubus->mon_bus;
142 if (mbus == NULL) {
144 * This should not happen.
145 * At this point we do not even know the bus number...
147 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
148 urb->pipe);
149 return;
152 spin_lock_irqsave(&mbus->lock, flags);
153 list_for_each (pos, &mbus->r_list) {
154 r = list_entry(pos, struct mon_reader, r_link);
155 r->rnf_complete(r->r_data, urb);
157 spin_unlock_irqrestore(&mbus->lock, flags);
160 /* int (*unlink_urb) (struct urb *urb, int status); */
163 * Stop monitoring.
164 * Obviously this must be well locked, so no need to play with mb's.
166 static void mon_stop(struct mon_bus *mbus)
168 struct usb_bus *ubus = mbus->u_bus;
171 * A stop can be called for a dissolved mon_bus in case of
172 * a reader staying across an rmmod foo_hcd.
174 if (ubus != NULL) {
175 ubus->monitored = 0;
176 mb();
181 * Add a USB bus (usually by a modprobe foo-hcd)
183 * This does not return an error code because the core cannot care less
184 * if monitoring is not established.
186 static void mon_bus_add(struct usb_bus *ubus)
188 mon_bus_init(mon_dir, ubus);
192 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
194 static void mon_bus_remove(struct usb_bus *ubus)
196 struct mon_bus *mbus = ubus->mon_bus;
198 down(&mon_lock);
199 list_del(&mbus->bus_link);
200 debugfs_remove(mbus->dent_t);
201 debugfs_remove(mbus->dent_s);
203 mon_dissolve(mbus, ubus);
204 kref_put(&mbus->ref, mon_bus_drop);
205 up(&mon_lock);
209 * Ops
211 static struct usb_mon_operations mon_ops_0 = {
212 .urb_submit = mon_submit,
213 .urb_submit_error = mon_submit_error,
214 .urb_complete = mon_complete,
215 .bus_add = mon_bus_add,
216 .bus_remove = mon_bus_remove,
220 * Tear usb_bus and mon_bus apart.
222 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
226 * Never happens, but...
228 if (ubus->monitored) {
229 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
230 ubus->busnum);
231 ubus->monitored = 0;
232 mb();
235 ubus->mon_bus = NULL;
236 mbus->u_bus = NULL;
237 mb();
238 // usb_bus_put(ubus);
243 static void mon_bus_drop(struct kref *r)
245 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
246 kfree(mbus);
250 * Initialize a bus for us:
251 * - allocate mon_bus
252 * - refcount USB bus struct
253 * - link
255 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
257 struct dentry *d;
258 struct mon_bus *mbus;
259 enum { NAMESZ = 10 };
260 char name[NAMESZ];
261 int rc;
263 if ((mbus = kmalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
264 goto err_alloc;
265 memset(mbus, 0, sizeof(struct mon_bus));
266 kref_init(&mbus->ref);
267 spin_lock_init(&mbus->lock);
268 INIT_LIST_HEAD(&mbus->r_list);
271 * This usb_bus_get here is superfluous, because we receive
272 * a notification if usb_bus is about to be removed.
274 // usb_bus_get(ubus);
275 mbus->u_bus = ubus;
276 ubus->mon_bus = mbus;
278 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
279 if (rc <= 0 || rc >= NAMESZ)
280 goto err_print_t;
281 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
282 if (d == NULL)
283 goto err_create_t;
284 mbus->dent_t = d;
286 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
287 if (rc <= 0 || rc >= NAMESZ)
288 goto err_print_s;
289 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
290 if (d == NULL)
291 goto err_create_s;
292 mbus->dent_s = d;
294 down(&mon_lock);
295 list_add_tail(&mbus->bus_link, &mon_buses);
296 up(&mon_lock);
297 return;
299 err_create_s:
300 err_print_s:
301 debugfs_remove(mbus->dent_t);
302 err_create_t:
303 err_print_t:
304 kfree(mbus);
305 err_alloc:
306 return;
309 static int __init mon_init(void)
311 struct usb_bus *ubus;
312 struct dentry *mondir;
314 mondir = debugfs_create_dir("usbmon", NULL);
315 if (IS_ERR(mondir)) {
316 printk(KERN_NOTICE TAG ": debugfs is not available\n");
317 return -ENODEV;
319 if (mondir == NULL) {
320 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
321 return -ENODEV;
323 mon_dir = mondir;
325 if (usb_mon_register(&mon_ops_0) != 0) {
326 printk(KERN_NOTICE TAG ": unable to register with the core\n");
327 debugfs_remove(mondir);
328 return -ENODEV;
330 // MOD_INC_USE_COUNT(which_module?);
332 down(&usb_bus_list_lock);
333 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
334 mon_bus_init(mondir, ubus);
336 up(&usb_bus_list_lock);
337 return 0;
340 static void __exit mon_exit(void)
342 struct mon_bus *mbus;
343 struct list_head *p;
345 usb_mon_deregister();
347 down(&mon_lock);
348 while (!list_empty(&mon_buses)) {
349 p = mon_buses.next;
350 mbus = list_entry(p, struct mon_bus, bus_link);
351 list_del(p);
353 debugfs_remove(mbus->dent_t);
354 debugfs_remove(mbus->dent_s);
357 * This never happens, because the open/close paths in
358 * file level maintain module use counters and so rmmod fails
359 * before reaching here. However, better be safe...
361 if (mbus->nreaders) {
362 printk(KERN_ERR TAG
363 ": Outstanding opens (%d) on usb%d, leaking...\n",
364 mbus->nreaders, mbus->u_bus->busnum);
365 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
368 mon_dissolve(mbus, mbus->u_bus);
369 kref_put(&mbus->ref, mon_bus_drop);
371 up(&mon_lock);
373 debugfs_remove(mon_dir);
376 module_init(mon_init);
377 module_exit(mon_exit);
379 MODULE_LICENSE("GPL");