mon_main.c 8.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * The USB Monitor, inspired by Dave Harding's USBMon.
 *
 * mon_main.c: Main file, module initiation and exit, registrations, etc.
5 6
 *
 * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com)
L
Linus Torvalds 已提交
7 8 9 10 11 12
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/smp_lock.h>
13
#include <linux/notifier.h>
14
#include <linux/mutex.h>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23

#include "usb_mon.h"
#include "../core/hcd.h"

static void mon_submit(struct usb_bus *ubus, struct urb *urb);
static void mon_complete(struct usb_bus *ubus, struct urb *urb);
static void mon_stop(struct mon_bus *mbus);
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
static void mon_bus_drop(struct kref *r);
P
Pete Zaitcev 已提交
24
static void mon_bus_init(struct usb_bus *ubus);
L
Linus Torvalds 已提交
25

26
DEFINE_MUTEX(mon_lock);
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

static LIST_HEAD(mon_buses);		/* All buses we know: struct mon_bus */

/*
 * Link a reader into the bus.
 *
 * This must be called with mon_lock taken because of mbus->ref.
 */
void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r)
{
	unsigned long flags;
	struct usb_bus *ubus;

	spin_lock_irqsave(&mbus->lock, flags);
	if (mbus->nreaders == 0) {
		ubus = mbus->u_bus;
		if (ubus->monitored) {
			/*
			 * Something is really broken, refuse to go on and
			 * possibly corrupt ops pointers or worse.
			 */
			printk(KERN_ERR TAG ": bus %d is already monitored\n",
			    ubus->busnum);
			spin_unlock_irqrestore(&mbus->lock, flags);
			return;
		}
		ubus->monitored = 1;
	}
	mbus->nreaders++;
	list_add_tail(&r->r_link, &mbus->r_list);
	spin_unlock_irqrestore(&mbus->lock, flags);

	kref_get(&mbus->ref);
}

/*
 * Unlink reader from the bus.
 *
 * This is called with mon_lock taken, so we can decrement mbus->ref.
 */
void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r)
{
	unsigned long flags;

	spin_lock_irqsave(&mbus->lock, flags);
	list_del(&r->r_link);
	--mbus->nreaders;
	if (mbus->nreaders == 0)
		mon_stop(mbus);
	spin_unlock_irqrestore(&mbus->lock, flags);

	kref_put(&mbus->ref, mon_bus_drop);
}

/*
 */
static void mon_submit(struct usb_bus *ubus, struct urb *urb)
{
	struct mon_bus *mbus;
	unsigned long flags;
	struct list_head *pos;
	struct mon_reader *r;

	mbus = ubus->mon_bus;
	if (mbus == NULL)
		goto out_unlocked;

	spin_lock_irqsave(&mbus->lock, flags);
	if (mbus->nreaders == 0)
		goto out_locked;

98
	mbus->cnt_events++;
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	list_for_each (pos, &mbus->r_list) {
		r = list_entry(pos, struct mon_reader, r_link);
		r->rnf_submit(r->r_data, urb);
	}

	spin_unlock_irqrestore(&mbus->lock, flags);
	return;

out_locked:
	spin_unlock_irqrestore(&mbus->lock, flags);
out_unlocked:
	return;
}

/*
 */
115
static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error)
L
Linus Torvalds 已提交
116 117
{
	struct mon_bus *mbus;
118 119 120
	unsigned long flags;
	struct list_head *pos;
	struct mon_reader *r;
L
Linus Torvalds 已提交
121 122 123 124 125

	mbus = ubus->mon_bus;
	if (mbus == NULL)
		goto out_unlocked;

126 127 128 129 130 131 132 133 134
	spin_lock_irqsave(&mbus->lock, flags);
	if (mbus->nreaders == 0)
		goto out_locked;

	mbus->cnt_events++;
	list_for_each (pos, &mbus->r_list) {
		r = list_entry(pos, struct mon_reader, r_link);
		r->rnf_error(r->r_data, urb, error);
	}
L
Linus Torvalds 已提交
135

136
	spin_unlock_irqrestore(&mbus->lock, flags);
L
Linus Torvalds 已提交
137 138
	return;

139 140
out_locked:
	spin_unlock_irqrestore(&mbus->lock, flags);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
out_unlocked:
	return;
}

/*
 */
static void mon_complete(struct usb_bus *ubus, struct urb *urb)
{
	struct mon_bus *mbus;
	unsigned long flags;
	struct list_head *pos;
	struct mon_reader *r;

	mbus = ubus->mon_bus;
	if (mbus == NULL) {
		/*
		 * This should not happen.
		 * At this point we do not even know the bus number...
		 */
		printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n",
		    urb->pipe);
		return;
	}

	spin_lock_irqsave(&mbus->lock, flags);
166
	mbus->cnt_events++;
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	list_for_each (pos, &mbus->r_list) {
		r = list_entry(pos, struct mon_reader, r_link);
		r->rnf_complete(r->r_data, urb);
	}
	spin_unlock_irqrestore(&mbus->lock, flags);
}

/* int (*unlink_urb) (struct urb *urb, int status); */

/*
 * Stop monitoring.
 */
static void mon_stop(struct mon_bus *mbus)
{
	struct usb_bus *ubus = mbus->u_bus;

	/*
	 * A stop can be called for a dissolved mon_bus in case of
	 * a reader staying across an rmmod foo_hcd.
	 */
	if (ubus != NULL) {
		ubus->monitored = 0;
		mb();
	}
}

/*
 * Add a USB bus (usually by a modprobe foo-hcd)
 *
 * This does not return an error code because the core cannot care less
 * if monitoring is not established.
 */
static void mon_bus_add(struct usb_bus *ubus)
{
P
Pete Zaitcev 已提交
201
	mon_bus_init(ubus);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210
}

/*
 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event).
 */
static void mon_bus_remove(struct usb_bus *ubus)
{
	struct mon_bus *mbus = ubus->mon_bus;

211
	mutex_lock(&mon_lock);
L
Linus Torvalds 已提交
212
	list_del(&mbus->bus_link);
P
Pete Zaitcev 已提交
213 214
	if (mbus->text_inited)
		mon_text_del(mbus);
L
Linus Torvalds 已提交
215 216 217

	mon_dissolve(mbus, ubus);
	kref_put(&mbus->ref, mon_bus_drop);
218
	mutex_unlock(&mon_lock);
L
Linus Torvalds 已提交
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static int mon_notify(struct notifier_block *self, unsigned long action,
		      void *dev)
{
	switch (action) {
	case USB_BUS_ADD:
		mon_bus_add(dev);
		break;
	case USB_BUS_REMOVE:
		mon_bus_remove(dev);
	}
	return NOTIFY_OK;
}

static struct notifier_block mon_nb = {
	.notifier_call = 	mon_notify,
};

L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/*
 * Ops
 */
static struct usb_mon_operations mon_ops_0 = {
	.urb_submit =	mon_submit,
	.urb_submit_error = mon_submit_error,
	.urb_complete =	mon_complete,
};

/*
 * Tear usb_bus and mon_bus apart.
 */
static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus)
{

	/*
	 * Never happens, but...
	 */
	if (ubus->monitored) {
		printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n",
		    ubus->busnum);
		ubus->monitored = 0;
		mb();
	}

	ubus->mon_bus = NULL;
	mbus->u_bus = NULL;
	mb();
}

/*
 */
static void mon_bus_drop(struct kref *r)
{
	struct mon_bus *mbus = container_of(r, struct mon_bus, ref);
	kfree(mbus);
}

/*
 * Initialize a bus for us:
 *  - allocate mon_bus
 *  - refcount USB bus struct
 *  - link
 */
P
Pete Zaitcev 已提交
282
static void mon_bus_init(struct usb_bus *ubus)
L
Linus Torvalds 已提交
283 284 285
{
	struct mon_bus *mbus;

286
	if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
287 288 289 290 291 292
		goto err_alloc;
	kref_init(&mbus->ref);
	spin_lock_init(&mbus->lock);
	INIT_LIST_HEAD(&mbus->r_list);

	/*
293 294
	 * We don't need to take a reference to ubus, because we receive
	 * a notification if the bus is about to be removed.
L
Linus Torvalds 已提交
295 296 297
	 */
	mbus->u_bus = ubus;
	ubus->mon_bus = mbus;
298
	mbus->uses_dma = ubus->uses_dma;
L
Linus Torvalds 已提交
299

P
Pete Zaitcev 已提交
300 301
	mbus->text_inited = mon_text_add(mbus, ubus);
	// mon_bin_add(...)
L
Linus Torvalds 已提交
302

303
	mutex_lock(&mon_lock);
L
Linus Torvalds 已提交
304
	list_add_tail(&mbus->bus_link, &mon_buses);
305
	mutex_unlock(&mon_lock);
L
Linus Torvalds 已提交
306 307 308 309 310 311
	return;

err_alloc:
	return;
}

P
Pete Zaitcev 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/*
 * Search a USB bus by number. Notice that USB bus numbers start from one,
 * which we may later use to identify "all" with zero.
 *
 * This function must be called with mon_lock held.
 *
 * This is obviously inefficient and may be revised in the future.
 */
struct mon_bus *mon_bus_lookup(unsigned int num)
{
	struct list_head *p;
	struct mon_bus *mbus;

	list_for_each (p, &mon_buses) {
		mbus = list_entry(p, struct mon_bus, bus_link);
		if (mbus->u_bus->busnum == num) {
			return mbus;
		}
	}
	return NULL;
}

L
Linus Torvalds 已提交
334 335 336
static int __init mon_init(void)
{
	struct usb_bus *ubus;
P
Pete Zaitcev 已提交
337
	int rc;
L
Linus Torvalds 已提交
338

P
Pete Zaitcev 已提交
339 340 341 342
	if ((rc = mon_text_init()) != 0)
		goto err_text;
	if ((rc = mon_bin_init()) != 0)
		goto err_bin;
L
Linus Torvalds 已提交
343 344 345

	if (usb_mon_register(&mon_ops_0) != 0) {
		printk(KERN_NOTICE TAG ": unable to register with the core\n");
P
Pete Zaitcev 已提交
346 347
		rc = -ENODEV;
		goto err_reg;
L
Linus Torvalds 已提交
348 349 350
	}
	// MOD_INC_USE_COUNT(which_module?);

351 352
	usb_register_notify(&mon_nb);

353
	mutex_lock(&usb_bus_list_lock);
L
Linus Torvalds 已提交
354
	list_for_each_entry (ubus, &usb_bus_list, bus_list) {
P
Pete Zaitcev 已提交
355
		mon_bus_init(ubus);
L
Linus Torvalds 已提交
356
	}
357
	mutex_unlock(&usb_bus_list_lock);
L
Linus Torvalds 已提交
358
	return 0;
P
Pete Zaitcev 已提交
359 360 361 362 363 364 365

err_reg:
	mon_bin_exit();
err_bin:
	mon_text_exit();
err_text:
	return rc;
L
Linus Torvalds 已提交
366 367 368 369 370 371 372
}

static void __exit mon_exit(void)
{
	struct mon_bus *mbus;
	struct list_head *p;

373
	usb_unregister_notify(&mon_nb);
L
Linus Torvalds 已提交
374 375
	usb_mon_deregister();

376
	mutex_lock(&mon_lock);
L
Linus Torvalds 已提交
377 378 379 380 381
	while (!list_empty(&mon_buses)) {
		p = mon_buses.next;
		mbus = list_entry(p, struct mon_bus, bus_link);
		list_del(p);

P
Pete Zaitcev 已提交
382 383
		if (mbus->text_inited)
			mon_text_del(mbus);
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

		/*
		 * This never happens, because the open/close paths in
		 * file level maintain module use counters and so rmmod fails
		 * before reaching here. However, better be safe...
		 */
		if (mbus->nreaders) {
			printk(KERN_ERR TAG
			    ": Outstanding opens (%d) on usb%d, leaking...\n",
			    mbus->nreaders, mbus->u_bus->busnum);
			atomic_set(&mbus->ref.refcount, 2);	/* Force leak */
		}

		mon_dissolve(mbus, mbus->u_bus);
		kref_put(&mbus->ref, mon_bus_drop);
	}
400
	mutex_unlock(&mon_lock);
L
Linus Torvalds 已提交
401

P
Pete Zaitcev 已提交
402 403
	mon_text_exit();
	mon_bin_exit();
L
Linus Torvalds 已提交
404 405 406 407 408 409
}

module_init(mon_init);
module_exit(mon_exit);

MODULE_LICENSE("GPL");