Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / usb / mon / mon_main.c
blob6ecc273022117351f9d155e3d16ab6d3a8a7ab34
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>
14 #include <linux/notifier.h>
15 #include <linux/mutex.h>
17 #include "usb_mon.h"
18 #include "../core/hcd.h"
20 static void mon_submit(struct usb_bus *ubus, struct urb *urb);
21 static void mon_complete(struct usb_bus *ubus, struct urb *urb);
22 static void mon_stop(struct mon_bus *mbus);
23 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
24 static void mon_bus_drop(struct kref *r);
25 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
27 DEFINE_MUTEX(mon_lock);
29 static struct dentry *mon_dir; /* /dbg/usbmon */
30 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
33 * Link a reader into the bus.
35 * This must be called with mon_lock taken because of mbus->ref.
37 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
39 unsigned long flags;
40 struct usb_bus *ubus;
42 spin_lock_irqsave(&mbus->lock, flags);
43 if (mbus->nreaders == 0) {
44 ubus = mbus->u_bus;
45 if (ubus->monitored) {
47 * Something is really broken, refuse to go on and
48 * possibly corrupt ops pointers or worse.
50 printk(KERN_ERR TAG ": bus %d is already monitored\n",
51 ubus->busnum);
52 spin_unlock_irqrestore(&mbus->lock, flags);
53 return;
55 ubus->monitored = 1;
57 mbus->nreaders++;
58 list_add_tail(&r->r_link, &mbus->r_list);
59 spin_unlock_irqrestore(&mbus->lock, flags);
61 kref_get(&mbus->ref);
65 * Unlink reader from the bus.
67 * This is called with mon_lock taken, so we can decrement mbus->ref.
69 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
71 unsigned long flags;
73 spin_lock_irqsave(&mbus->lock, flags);
74 list_del(&r->r_link);
75 --mbus->nreaders;
76 if (mbus->nreaders == 0)
77 mon_stop(mbus);
78 spin_unlock_irqrestore(&mbus->lock, flags);
80 kref_put(&mbus->ref, mon_bus_drop);
85 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
87 struct mon_bus *mbus;
88 unsigned long flags;
89 struct list_head *pos;
90 struct mon_reader *r;
92 mbus = ubus->mon_bus;
93 if (mbus == NULL)
94 goto out_unlocked;
96 spin_lock_irqsave(&mbus->lock, flags);
97 if (mbus->nreaders == 0)
98 goto out_locked;
100 list_for_each (pos, &mbus->r_list) {
101 r = list_entry(pos, struct mon_reader, r_link);
102 r->rnf_submit(r->r_data, urb);
105 spin_unlock_irqrestore(&mbus->lock, flags);
106 return;
108 out_locked:
109 spin_unlock_irqrestore(&mbus->lock, flags);
110 out_unlocked:
111 return;
116 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err)
118 struct mon_bus *mbus;
120 mbus = ubus->mon_bus;
121 if (mbus == NULL)
122 goto out_unlocked;
125 * XXX Capture the error code and the 'E' event.
128 return;
130 out_unlocked:
131 return;
136 static void mon_complete(struct usb_bus *ubus, struct urb *urb)
138 struct mon_bus *mbus;
139 unsigned long flags;
140 struct list_head *pos;
141 struct mon_reader *r;
143 mbus = ubus->mon_bus;
144 if (mbus == NULL) {
146 * This should not happen.
147 * At this point we do not even know the bus number...
149 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
150 urb->pipe);
151 return;
154 spin_lock_irqsave(&mbus->lock, flags);
155 list_for_each (pos, &mbus->r_list) {
156 r = list_entry(pos, struct mon_reader, r_link);
157 r->rnf_complete(r->r_data, urb);
159 spin_unlock_irqrestore(&mbus->lock, flags);
162 /* int (*unlink_urb) (struct urb *urb, int status); */
165 * Stop monitoring.
166 * Obviously this must be well locked, so no need to play with mb's.
168 static void mon_stop(struct mon_bus *mbus)
170 struct usb_bus *ubus = mbus->u_bus;
173 * A stop can be called for a dissolved mon_bus in case of
174 * a reader staying across an rmmod foo_hcd.
176 if (ubus != NULL) {
177 ubus->monitored = 0;
178 mb();
183 * Add a USB bus (usually by a modprobe foo-hcd)
185 * This does not return an error code because the core cannot care less
186 * if monitoring is not established.
188 static void mon_bus_add(struct usb_bus *ubus)
190 mon_bus_init(mon_dir, ubus);
194 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
196 static void mon_bus_remove(struct usb_bus *ubus)
198 struct mon_bus *mbus = ubus->mon_bus;
200 mutex_lock(&mon_lock);
201 list_del(&mbus->bus_link);
202 debugfs_remove(mbus->dent_t);
203 debugfs_remove(mbus->dent_s);
205 mon_dissolve(mbus, ubus);
206 kref_put(&mbus->ref, mon_bus_drop);
207 mutex_unlock(&mon_lock);
210 static int mon_notify(struct notifier_block *self, unsigned long action,
211 void *dev)
213 switch (action) {
214 case USB_BUS_ADD:
215 mon_bus_add(dev);
216 break;
217 case USB_BUS_REMOVE:
218 mon_bus_remove(dev);
220 return NOTIFY_OK;
223 static struct notifier_block mon_nb = {
224 .notifier_call = mon_notify,
228 * Ops
230 static struct usb_mon_operations mon_ops_0 = {
231 .urb_submit = mon_submit,
232 .urb_submit_error = mon_submit_error,
233 .urb_complete = mon_complete,
237 * Tear usb_bus and mon_bus apart.
239 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
243 * Never happens, but...
245 if (ubus->monitored) {
246 printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
247 ubus->busnum);
248 ubus->monitored = 0;
249 mb();
252 ubus->mon_bus = NULL;
253 mbus->u_bus = NULL;
254 mb();
255 // usb_bus_put(ubus);
260 static void mon_bus_drop(struct kref *r)
262 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
263 kfree(mbus);
267 * Initialize a bus for us:
268 * - allocate mon_bus
269 * - refcount USB bus struct
270 * - link
272 static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
274 struct dentry *d;
275 struct mon_bus *mbus;
276 enum { NAMESZ = 10 };
277 char name[NAMESZ];
278 int rc;
280 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
281 goto err_alloc;
282 kref_init(&mbus->ref);
283 spin_lock_init(&mbus->lock);
284 INIT_LIST_HEAD(&mbus->r_list);
287 * This usb_bus_get here is superfluous, because we receive
288 * a notification if usb_bus is about to be removed.
290 // usb_bus_get(ubus);
291 mbus->u_bus = ubus;
292 ubus->mon_bus = mbus;
294 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
295 if (rc <= 0 || rc >= NAMESZ)
296 goto err_print_t;
297 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text);
298 if (d == NULL)
299 goto err_create_t;
300 mbus->dent_t = d;
302 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
303 if (rc <= 0 || rc >= NAMESZ)
304 goto err_print_s;
305 d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat);
306 if (d == NULL)
307 goto err_create_s;
308 mbus->dent_s = d;
310 mutex_lock(&mon_lock);
311 list_add_tail(&mbus->bus_link, &mon_buses);
312 mutex_unlock(&mon_lock);
313 return;
315 err_create_s:
316 err_print_s:
317 debugfs_remove(mbus->dent_t);
318 err_create_t:
319 err_print_t:
320 kfree(mbus);
321 err_alloc:
322 return;
325 static int __init mon_init(void)
327 struct usb_bus *ubus;
328 struct dentry *mondir;
330 mondir = debugfs_create_dir("usbmon", NULL);
331 if (IS_ERR(mondir)) {
332 printk(KERN_NOTICE TAG ": debugfs is not available\n");
333 return -ENODEV;
335 if (mondir == NULL) {
336 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
337 return -ENODEV;
339 mon_dir = mondir;
341 if (usb_mon_register(&mon_ops_0) != 0) {
342 printk(KERN_NOTICE TAG ": unable to register with the core\n");
343 debugfs_remove(mondir);
344 return -ENODEV;
346 // MOD_INC_USE_COUNT(which_module?);
348 usb_register_notify(&mon_nb);
350 mutex_lock(&usb_bus_list_lock);
351 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
352 mon_bus_init(mondir, ubus);
354 mutex_unlock(&usb_bus_list_lock);
355 return 0;
358 static void __exit mon_exit(void)
360 struct mon_bus *mbus;
361 struct list_head *p;
363 usb_unregister_notify(&mon_nb);
364 usb_mon_deregister();
366 mutex_lock(&mon_lock);
367 while (!list_empty(&mon_buses)) {
368 p = mon_buses.next;
369 mbus = list_entry(p, struct mon_bus, bus_link);
370 list_del(p);
372 debugfs_remove(mbus->dent_t);
373 debugfs_remove(mbus->dent_s);
376 * This never happens, because the open/close paths in
377 * file level maintain module use counters and so rmmod fails
378 * before reaching here. However, better be safe...
380 if (mbus->nreaders) {
381 printk(KERN_ERR TAG
382 ": Outstanding opens (%d) on usb%d, leaking...\n",
383 mbus->nreaders, mbus->u_bus->busnum);
384 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
387 mon_dissolve(mbus, mbus->u_bus);
388 kref_put(&mbus->ref, mon_bus_drop);
390 mutex_unlock(&mon_lock);
392 debugfs_remove(mon_dir);
395 module_init(mon_init);
396 module_exit(mon_exit);
398 MODULE_LICENSE("GPL");