Full support for Ginger Console
[linux-ginger.git] / drivers / usb / mon / mon_main.c
blobe0c2db3b767b7cb91539e1ed1fe8f386beebc268
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/notifier.h>
13 #include <linux/mutex.h>
15 #include "usb_mon.h"
16 #include "../core/hcd.h"
18 static void mon_stop(struct mon_bus *mbus);
19 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
20 static void mon_bus_drop(struct kref *r);
21 static void mon_bus_init(struct usb_bus *ubus);
23 DEFINE_MUTEX(mon_lock);
25 struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */
26 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
29 * Link a reader into the bus.
31 * This must be called with mon_lock taken because of mbus->ref.
33 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
35 unsigned long flags;
36 struct list_head *p;
38 spin_lock_irqsave(&mbus->lock, flags);
39 if (mbus->nreaders == 0) {
40 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;
46 } else {
47 mbus->u_bus->monitored = 1;
50 mbus->nreaders++;
51 list_add_tail(&r->r_link, &mbus->r_list);
52 spin_unlock_irqrestore(&mbus->lock, flags);
54 kref_get(&mbus->ref);
58 * Unlink reader from the bus.
60 * This is called with mon_lock taken, so we can decrement mbus->ref.
62 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
64 unsigned long flags;
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);
73 kref_put(&mbus->ref, mon_bus_drop);
78 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb)
80 unsigned long flags;
81 struct list_head *pos;
82 struct mon_reader *r;
84 spin_lock_irqsave(&mbus->lock, flags);
85 mbus->cnt_events++;
86 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);
90 spin_unlock_irqrestore(&mbus->lock, flags);
91 return;
94 static void mon_submit(struct usb_bus *ubus, struct urb *urb)
96 struct mon_bus *mbus;
98 if ((mbus = ubus->mon_bus) != NULL)
99 mon_bus_submit(mbus, urb);
100 mon_bus_submit(&mon_bus0, urb);
105 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error)
107 unsigned long flags;
108 struct list_head *pos;
109 struct mon_reader *r;
111 spin_lock_irqsave(&mbus->lock, flags);
112 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);
117 spin_unlock_irqrestore(&mbus->lock, flags);
118 return;
121 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
123 struct mon_bus *mbus;
125 if ((mbus = ubus->mon_bus) != NULL)
126 mon_bus_submit_error(mbus, urb, error);
127 mon_bus_submit_error(&mon_bus0, urb, error);
132 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
134 unsigned long flags;
135 struct list_head *pos;
136 struct mon_reader *r;
138 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);
142 r->rnf_complete(r->r_data, urb, status);
144 spin_unlock_irqrestore(&mbus->lock, flags);
147 static void mon_complete(struct usb_bus *ubus, struct urb *urb, int status)
149 struct mon_bus *mbus;
151 if ((mbus = ubus->mon_bus) != NULL)
152 mon_bus_complete(mbus, urb, status);
153 mon_bus_complete(&mon_bus0, urb, status);
156 /* int (*unlink_urb) (struct urb *urb, int status); */
159 * Stop monitoring.
161 static void mon_stop(struct mon_bus *mbus)
163 struct usb_bus *ubus;
164 struct list_head *p;
166 if (mbus == &mon_bus0) {
167 list_for_each (p, &mon_buses) {
168 mbus = list_entry(p, struct mon_bus, bus_link);
170 * We do not change nreaders here, so rely on mon_lock.
172 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL)
173 ubus->monitored = 0;
175 } else {
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.
180 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) {
181 ubus->monitored = 0;
182 mb();
188 * Add a USB bus (usually by a modprobe foo-hcd)
190 * This does not return an error code because the core cannot care less
191 * if monitoring is not established.
193 static void mon_bus_add(struct usb_bus *ubus)
195 mon_bus_init(ubus);
196 mutex_lock(&mon_lock);
197 if (mon_bus0.nreaders != 0)
198 ubus->monitored = 1;
199 mutex_unlock(&mon_lock);
203 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
205 static void mon_bus_remove(struct usb_bus *ubus)
207 struct mon_bus *mbus = ubus->mon_bus;
209 mutex_lock(&mon_lock);
210 list_del(&mbus->bus_link);
211 if (mbus->text_inited)
212 mon_text_del(mbus);
213 if (mbus->bin_inited)
214 mon_bin_del(mbus);
216 mon_dissolve(mbus, ubus);
217 kref_put(&mbus->ref, mon_bus_drop);
218 mutex_unlock(&mon_lock);
221 static int mon_notify(struct notifier_block *self, unsigned long action,
222 void *dev)
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);
231 return NOTIFY_OK;
234 static struct notifier_block mon_nb = {
235 .notifier_call = mon_notify,
239 * Ops
241 static struct usb_mon_operations mon_ops_0 = {
242 .urb_submit = mon_submit,
243 .urb_submit_error = mon_submit_error,
244 .urb_complete = mon_complete,
248 * Tear usb_bus and mon_bus apart.
250 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
253 if (ubus->monitored) {
254 ubus->monitored = 0;
255 mb();
258 ubus->mon_bus = NULL;
259 mbus->u_bus = NULL;
260 mb();
262 /* We want synchronize_irq() here, but that needs an argument. */
267 static void mon_bus_drop(struct kref *r)
269 struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
270 kfree(mbus);
274 * Initialize a bus for us:
275 * - allocate mon_bus
276 * - refcount USB bus struct
277 * - link
279 static void mon_bus_init(struct usb_bus *ubus)
281 struct mon_bus *mbus;
283 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
284 goto err_alloc;
285 kref_init(&mbus->ref);
286 spin_lock_init(&mbus->lock);
287 INIT_LIST_HEAD(&mbus->r_list);
290 * We don't need to take a reference to ubus, because we receive
291 * a notification if the bus is about to be removed.
293 mbus->u_bus = ubus;
294 ubus->mon_bus = mbus;
296 mbus->text_inited = mon_text_add(mbus, ubus);
297 mbus->bin_inited = mon_bin_add(mbus, ubus);
299 mutex_lock(&mon_lock);
300 list_add_tail(&mbus->bus_link, &mon_buses);
301 mutex_unlock(&mon_lock);
302 return;
304 err_alloc:
305 return;
308 static void mon_bus0_init(void)
310 struct mon_bus *mbus = &mon_bus0;
312 kref_init(&mbus->ref);
313 spin_lock_init(&mbus->lock);
314 INIT_LIST_HEAD(&mbus->r_list);
316 mbus->text_inited = mon_text_add(mbus, NULL);
317 mbus->bin_inited = mon_bin_add(mbus, NULL);
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.
324 * This function must be called with mon_lock held.
326 * This is obviously inefficient and may be revised in the future.
328 struct mon_bus *mon_bus_lookup(unsigned int num)
330 struct list_head *p;
331 struct mon_bus *mbus;
333 if (num == 0) {
334 return &mon_bus0;
336 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;
342 return NULL;
345 static int __init mon_init(void)
347 struct usb_bus *ubus;
348 int rc;
350 if ((rc = mon_text_init()) != 0)
351 goto err_text;
352 if ((rc = mon_bin_init()) != 0)
353 goto err_bin;
355 mon_bus0_init();
357 if (usb_mon_register(&mon_ops_0) != 0) {
358 printk(KERN_NOTICE TAG ": unable to register with the core\n");
359 rc = -ENODEV;
360 goto err_reg;
362 // MOD_INC_USE_COUNT(which_module?);
364 mutex_lock(&usb_bus_list_lock);
365 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
366 mon_bus_init(ubus);
368 usb_register_notify(&mon_nb);
369 mutex_unlock(&usb_bus_list_lock);
370 return 0;
372 err_reg:
373 mon_bin_exit();
374 err_bin:
375 mon_text_exit();
376 err_text:
377 return rc;
380 static void __exit mon_exit(void)
382 struct mon_bus *mbus;
383 struct list_head *p;
385 usb_unregister_notify(&mon_nb);
386 usb_mon_deregister();
388 mutex_lock(&mon_lock);
390 while (!list_empty(&mon_buses)) {
391 p = mon_buses.next;
392 mbus = list_entry(p, struct mon_bus, bus_link);
393 list_del(p);
395 if (mbus->text_inited)
396 mon_text_del(mbus);
397 if (mbus->bin_inited)
398 mon_bin_del(mbus);
401 * This never happens, because the open/close paths in
402 * file level maintain module use counters and so rmmod fails
403 * before reaching here. However, better be safe...
405 if (mbus->nreaders) {
406 printk(KERN_ERR TAG
407 ": Outstanding opens (%d) on usb%d, leaking...\n",
408 mbus->nreaders, mbus->u_bus->busnum);
409 atomic_set(&mbus->ref.refcount, 2); /* Force leak */
412 mon_dissolve(mbus, mbus->u_bus);
413 kref_put(&mbus->ref, mon_bus_drop);
416 mbus = &mon_bus0;
417 if (mbus->text_inited)
418 mon_text_del(mbus);
419 if (mbus->bin_inited)
420 mon_bin_del(mbus);
422 mutex_unlock(&mon_lock);
424 mon_text_exit();
425 mon_bin_exit();
428 module_init(mon_init);
429 module_exit(mon_exit);
431 MODULE_LICENSE("GPL");