RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / mon / mon_main.c
blob812dc288bb8c1a081805313f742110e9ad2c0bc1
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/usb/hcd.h>
13 #include <linux/slab.h>
14 #include <linux/notifier.h>
15 #include <linux/mutex.h>
17 #include "usb_mon.h"
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 usb_bus *ubus);
25 DEFINE_MUTEX(mon_lock);
27 struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
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 list_head *p;
40 spin_lock_irqsave(&mbus->lock, flags);
41 if (mbus->nreaders == 0) {
42 if (mbus == &mon_bus0) {
43 list_for_each (p, &mon_buses) {
44 struct mon_bus *m1;
45 m1 = list_entry(p, struct mon_bus, bus_link);
46 m1->u_bus->monitored = 1;
48 } else {
49 mbus->u_bus->monitored = 1;
52 mbus->nreaders++;
53 list_add_tail(&r->r_link, &mbus->r_list);
54 spin_unlock_irqrestore(&mbus->lock, flags);
56 kref_get(&mbus->ref);
60 * Unlink reader from the bus.
62 * This is called with mon_lock taken, so we can decrement mbus->ref.
64 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
66 unsigned long flags;
68 spin_lock_irqsave(&mbus->lock, flags);
69 list_del(&r->r_link);
70 --mbus->nreaders;
71 if (mbus->nreaders == 0)
72 mon_stop(mbus);
73 spin_unlock_irqrestore(&mbus->lock, flags);
75 kref_put(&mbus->ref, mon_bus_drop);
80 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
82 unsigned long flags;
83 struct list_head *pos;
84 struct mon_reader *r;
86 spin_lock_irqsave(&mbus->lock, flags);
87 mbus->cnt_events++;
88 list_for_each (pos, &mbus->r_list) {
89 r = list_entry(pos, struct mon_reader, r_link);
90 r->rnf_submit(r->r_data, urb);
92 spin_unlock_irqrestore(&mbus->lock, flags);
93 return;
96 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
98 struct mon_bus *mbus;
100 if ((mbus = ubus->mon_bus) != NULL)
101 mon_bus_submit(mbus, urb);
102 mon_bus_submit(&mon_bus0, urb);
107 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
109 unsigned long flags;
110 struct list_head *pos;
111 struct mon_reader *r;
113 spin_lock_irqsave(&mbus->lock, flags);
114 mbus->cnt_events++;
115 list_for_each (pos, &mbus->r_list) {
116 r = list_entry(pos, struct mon_reader, r_link);
117 r->rnf_error(r->r_data, urb, error);
119 spin_unlock_irqrestore(&mbus->lock, flags);
120 return;
123 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
125 struct mon_bus *mbus;
127 if ((mbus = ubus->mon_bus) != NULL)
128 mon_bus_submit_error(mbus, urb, error);
129 mon_bus_submit_error(&mon_bus0, urb, error);
134 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
136 unsigned long flags;
137 struct list_head *pos;
138 struct mon_reader *r;
140 spin_lock_irqsave(&mbus->lock, flags);
141 mbus->cnt_events++;
142 list_for_each (pos, &mbus->r_list) {
143 r = list_entry(pos, struct mon_reader, r_link);
144 r->rnf_complete(r->r_data, urb, status);
146 spin_unlock_irqrestore(&mbus->lock, flags);
149 static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
151 struct mon_bus *mbus;
153 if ((mbus = ubus->mon_bus) != NULL)
154 mon_bus_complete(mbus, urb, status);
155 mon_bus_complete(&mon_bus0, urb, status);
158 /* int (*unlink_urb) (struct urb *urb, int status); */
161 * Stop monitoring.
163 static void mon_stop(struct mon_bus *mbus)
165 struct usb_bus *ubus;
166 struct list_head *p;
168 if (mbus == &mon_bus0) {
169 list_for_each (p, &mon_buses) {
170 mbus = list_entry(p, struct mon_bus, bus_link);
172 * We do not change nreaders here, so rely on mon_lock.
174 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
175 ubus->monitored = 0;
177 } else {
179 * A stop can be called for a dissolved mon_bus in case of
180 * a reader staying across an rmmod foo_hcd, so test ->u_bus.
182 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
183 ubus->monitored = 0;
184 mb();
190 * Add a USB bus (usually by a modprobe foo-hcd)
192 * This does not return an error code because the core cannot care less
193 * if monitoring is not established.
195 static void mon_bus_add(struct usb_bus *ubus)
197 mon_bus_init(ubus);
198 mutex_lock(&mon_lock);
199 if (mon_bus0.nreaders != 0)
200 ubus->monitored = 1;
201 mutex_unlock(&mon_lock);
205 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
207 static void mon_bus_remove(struct usb_bus *ubus)
209 struct mon_bus *mbus = ubus->mon_bus;
211 mutex_lock(&mon_lock);
212 list_del(&mbus->bus_link);
213 if (mbus->text_inited)
214 mon_text_del(mbus);
215 if (mbus->bin_inited)
216 mon_bin_del(mbus);
218 mon_dissolve(mbus, ubus);
219 kref_put(&mbus->ref, mon_bus_drop);
220 mutex_unlock(&mon_lock);
223 static int mon_notify(struct notifier_block *self, unsigned long action,
224 void *dev)
226 switch (action) {
227 case USB_BUS_ADD:
228 mon_bus_add(dev);
229 break;
230 case USB_BUS_REMOVE:
231 mon_bus_remove(dev);
233 return NOTIFY_OK;
236 static struct notifier_block mon_nb = {
237 .notifier_call = mon_notify,
241 * Ops
243 static struct usb_mon_operations mon_ops_0 = {
244 .urb_submit = mon_submit,
245 .urb_submit_error = mon_submit_error,
246 .urb_complete = mon_complete,
250 * Tear usb_bus and mon_bus apart.
252 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
255 if (ubus->monitored) {
256 ubus->monitored = 0;
257 mb();
260 ubus->mon_bus = NULL;
261 mbus->u_bus = NULL;
262 mb();
264 /* We want synchronize_irq() here, but that needs an argument. */
269 static void mon_bus_drop(struct kref *r)
271 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
272 kfree(mbus);
276 * Initialize a bus for us:
277 * - allocate mon_bus
278 * - refcount USB bus struct
279 * - link
281 static void mon_bus_init(struct usb_bus *ubus)
283 struct mon_bus *mbus;
285 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
286 goto err_alloc;
287 kref_init(&mbus->ref);
288 spin_lock_init(&mbus->lock);
289 INIT_LIST_HEAD(&mbus->r_list);
292 * We don't need to take a reference to ubus, because we receive
293 * a notification if the bus is about to be removed.
295 mbus->u_bus = ubus;
296 ubus->mon_bus = mbus;
298 mbus->text_inited = mon_text_add(mbus, ubus);
299 mbus->bin_inited = mon_bin_add(mbus, ubus);
301 mutex_lock(&mon_lock);
302 list_add_tail(&mbus->bus_link, &mon_buses);
303 mutex_unlock(&mon_lock);
304 return;
306 err_alloc:
307 return;
310 static void mon_bus0_init(void)
312 struct mon_bus *mbus = &mon_bus0;
314 kref_init(&mbus->ref);
315 spin_lock_init(&mbus->lock);
316 INIT_LIST_HEAD(&mbus->r_list);
318 mbus->text_inited = mon_text_add(mbus, NULL);
319 mbus->bin_inited = mon_bin_add(mbus, NULL);
323 * Search a USB bus by number. Notice that USB bus numbers start from one,
324 * which we may later use to identify "all" with zero.
326 * This function must be called with mon_lock held.
328 * This is obviously inefficient and may be revised in the future.
330 struct mon_bus *mon_bus_lookup(unsigned int num)
332 struct list_head *p;
333 struct mon_bus *mbus;
335 if (num == 0) {
336 return &mon_bus0;
338 list_for_each (p, &mon_buses) {
339 mbus = list_entry(p, struct mon_bus, bus_link);
340 if (mbus->u_bus->busnum == num) {
341 return mbus;
344 return NULL;
347 static int __init mon_init(void)
349 struct usb_bus *ubus;
350 int rc;
352 if ((rc = mon_text_init()) != 0)
353 goto err_text;
354 if ((rc = mon_bin_init()) != 0)
355 goto err_bin;
357 mon_bus0_init();
359 if (usb_mon_register(&mon_ops_0) != 0) {
360 printk(KERN_NOTICE TAG ": unable to register with the core\n");
361 rc = -ENODEV;
362 goto err_reg;
364 // MOD_INC_USE_COUNT(which_module?);
366 mutex_lock(&usb_bus_list_lock);
367 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
368 mon_bus_init(ubus);
370 usb_register_notify(&mon_nb);
371 mutex_unlock(&usb_bus_list_lock);
372 return 0;
374 err_reg:
375 mon_bin_exit();
376 err_bin:
377 mon_text_exit();
378 err_text:
379 return rc;
382 static void __exit mon_exit(void)
384 struct mon_bus *mbus;
385 struct list_head *p;
387 usb_unregister_notify(&mon_nb);
388 usb_mon_deregister();
390 mutex_lock(&mon_lock);
392 while (!list_empty(&mon_buses)) {
393 p = mon_buses.next;
394 mbus = list_entry(p, struct mon_bus, bus_link);
395 list_del(p);
397 if (mbus->text_inited)
398 mon_text_del(mbus);
399 if (mbus->bin_inited)
400 mon_bin_del(mbus);
403 * This never happens, because the open/close paths in
404 * file level maintain module use counters and so rmmod fails
405 * before reaching here. However, better be safe...
407 if (mbus->nreaders) {
408 printk(KERN_ERR TAG
409 ": Outstanding opens (%d) on usb%d, leaking...\n",
410 mbus->nreaders, mbus->u_bus->busnum);
411 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
414 mon_dissolve(mbus, mbus->u_bus);
415 kref_put(&mbus->ref, mon_bus_drop);
418 mbus = &mon_bus0;
419 if (mbus->text_inited)
420 mon_text_del(mbus);
421 if (mbus->bin_inited)
422 mon_bin_del(mbus);
424 mutex_unlock(&mon_lock);
426 mon_text_exit();
427 mon_bin_exit();
430 module_init(mon_init);
431 module_exit(mon_exit);
433 MODULE_LICENSE("GPL");