1 /* 2 * The USB Monitor, inspired by Dave Harding's USBMon. 3 * 4 * mon_main.c: Main file, module initiation and exit, registrations, etc. 5 * 6 * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com) 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/usb.h> 12 #include <linux/smp_lock.h> 13 #include <linux/notifier.h> 14 #include <linux/mutex.h> 15 16 #include "usb_mon.h" 17 #include "../core/hcd.h" 18 19 static void mon_stop(struct mon_bus *mbus); 20 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus); 21 static void mon_bus_drop(struct kref *r); 22 static void mon_bus_init(struct usb_bus *ubus); 23 24 DEFINE_MUTEX(mon_lock); 25 26 struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */ 27 static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */ 28 29 /* 30 * Link a reader into the bus. 31 * 32 * This must be called with mon_lock taken because of mbus->ref. 33 */ 34 void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r) 35 { 36 unsigned long flags; 37 struct list_head *p; 38 39 spin_lock_irqsave(&mbus->lock, flags); 40 if (mbus->nreaders == 0) { 41 if (mbus == &mon_bus0) { 42 list_for_each (p, &mon_buses) { 43 struct mon_bus *m1; 44 m1 = list_entry(p, struct mon_bus, bus_link); 45 m1->u_bus->monitored = 1; 46 } 47 } else { 48 mbus->u_bus->monitored = 1; 49 } 50 } 51 mbus->nreaders++; 52 list_add_tail(&r->r_link, &mbus->r_list); 53 spin_unlock_irqrestore(&mbus->lock, flags); 54 55 kref_get(&mbus->ref); 56 } 57 58 /* 59 * Unlink reader from the bus. 60 * 61 * This is called with mon_lock taken, so we can decrement mbus->ref. 62 */ 63 void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r) 64 { 65 unsigned long flags; 66 67 spin_lock_irqsave(&mbus->lock, flags); 68 list_del(&r->r_link); 69 --mbus->nreaders; 70 if (mbus->nreaders == 0) 71 mon_stop(mbus); 72 spin_unlock_irqrestore(&mbus->lock, flags); 73 74 kref_put(&mbus->ref, mon_bus_drop); 75 } 76 77 /* 78 */ 79 static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb) 80 { 81 unsigned long flags; 82 struct list_head *pos; 83 struct mon_reader *r; 84 85 spin_lock_irqsave(&mbus->lock, flags); 86 mbus->cnt_events++; 87 list_for_each (pos, &mbus->r_list) { 88 r = list_entry(pos, struct mon_reader, r_link); 89 r->rnf_submit(r->r_data, urb); 90 } 91 spin_unlock_irqrestore(&mbus->lock, flags); 92 return; 93 } 94 95 static void mon_submit(struct usb_bus *ubus, struct urb *urb) 96 { 97 struct mon_bus *mbus; 98 99 if ((mbus = ubus->mon_bus) != NULL) 100 mon_bus_submit(mbus, urb); 101 mon_bus_submit(&mon_bus0, urb); 102 } 103 104 /* 105 */ 106 static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error) 107 { 108 unsigned long flags; 109 struct list_head *pos; 110 struct mon_reader *r; 111 112 spin_lock_irqsave(&mbus->lock, flags); 113 mbus->cnt_events++; 114 list_for_each (pos, &mbus->r_list) { 115 r = list_entry(pos, struct mon_reader, r_link); 116 r->rnf_error(r->r_data, urb, error); 117 } 118 spin_unlock_irqrestore(&mbus->lock, flags); 119 return; 120 } 121 122 static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int error) 123 { 124 struct mon_bus *mbus; 125 126 if ((mbus = ubus->mon_bus) != NULL) 127 mon_bus_submit_error(mbus, urb, error); 128 mon_bus_submit_error(&mon_bus0, urb, error); 129 } 130 131 /* 132 */ 133 static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb) 134 { 135 unsigned long flags; 136 struct list_head *pos; 137 struct mon_reader *r; 138 139 spin_lock_irqsave(&mbus->lock, flags); 140 mbus->cnt_events++; 141 list_for_each (pos, &mbus->r_list) { 142 r = list_entry(pos, struct mon_reader, r_link); 143 r->rnf_complete(r->r_data, urb); 144 } 145 spin_unlock_irqrestore(&mbus->lock, flags); 146 } 147 148 static void mon_complete(struct usb_bus *ubus, struct urb *urb) 149 { 150 struct mon_bus *mbus; 151 152 mbus = ubus->mon_bus; 153 if (mbus == NULL) { 154 /* 155 * This should not happen. 156 * At this point we do not even know the bus number... 157 */ 158 printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n", 159 urb->pipe); 160 return; 161 } 162 163 mon_bus_complete(mbus, urb); 164 mon_bus_complete(&mon_bus0, urb); 165 } 166 167 /* int (*unlink_urb) (struct urb *urb, int status); */ 168 169 /* 170 * Stop monitoring. 171 */ 172 static void mon_stop(struct mon_bus *mbus) 173 { 174 struct usb_bus *ubus = mbus->u_bus; 175 struct list_head *p; 176 177 if (mbus == &mon_bus0) { 178 list_for_each (p, &mon_buses) { 179 mbus = list_entry(p, struct mon_bus, bus_link); 180 /* 181 * We do not change nreaders here, so rely on mon_lock. 182 */ 183 if (mbus->nreaders == 0 && (ubus = mbus->u_bus) != NULL) 184 ubus->monitored = 0; 185 } 186 } else { 187 /* 188 * A stop can be called for a dissolved mon_bus in case of 189 * a reader staying across an rmmod foo_hcd, so test ->u_bus. 190 */ 191 if (mon_bus0.nreaders == 0 && (ubus = mbus->u_bus) != NULL) { 192 ubus->monitored = 0; 193 mb(); 194 } 195 } 196 } 197 198 /* 199 * Add a USB bus (usually by a modprobe foo-hcd) 200 * 201 * This does not return an error code because the core cannot care less 202 * if monitoring is not established. 203 */ 204 static void mon_bus_add(struct usb_bus *ubus) 205 { 206 mon_bus_init(ubus); 207 mutex_lock(&mon_lock); 208 if (mon_bus0.nreaders != 0) 209 ubus->monitored = 1; 210 mutex_unlock(&mon_lock); 211 } 212 213 /* 214 * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event). 215 */ 216 static void mon_bus_remove(struct usb_bus *ubus) 217 { 218 struct mon_bus *mbus = ubus->mon_bus; 219 220 mutex_lock(&mon_lock); 221 list_del(&mbus->bus_link); 222 if (mbus->text_inited) 223 mon_text_del(mbus); 224 225 mon_dissolve(mbus, ubus); 226 kref_put(&mbus->ref, mon_bus_drop); 227 mutex_unlock(&mon_lock); 228 } 229 230 static int mon_notify(struct notifier_block *self, unsigned long action, 231 void *dev) 232 { 233 switch (action) { 234 case USB_BUS_ADD: 235 mon_bus_add(dev); 236 break; 237 case USB_BUS_REMOVE: 238 mon_bus_remove(dev); 239 } 240 return NOTIFY_OK; 241 } 242 243 static struct notifier_block mon_nb = { 244 .notifier_call = mon_notify, 245 }; 246 247 /* 248 * Ops 249 */ 250 static struct usb_mon_operations mon_ops_0 = { 251 .urb_submit = mon_submit, 252 .urb_submit_error = mon_submit_error, 253 .urb_complete = mon_complete, 254 }; 255 256 /* 257 * Tear usb_bus and mon_bus apart. 258 */ 259 static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus) 260 { 261 262 if (ubus->monitored) { 263 ubus->monitored = 0; 264 mb(); 265 } 266 267 ubus->mon_bus = NULL; 268 mbus->u_bus = NULL; 269 mb(); 270 271 /* We want synchronize_irq() here, but that needs an argument. */ 272 } 273 274 /* 275 */ 276 static void mon_bus_drop(struct kref *r) 277 { 278 struct mon_bus *mbus = container_of(r, struct mon_bus, ref); 279 kfree(mbus); 280 } 281 282 /* 283 * Initialize a bus for us: 284 * - allocate mon_bus 285 * - refcount USB bus struct 286 * - link 287 */ 288 static void mon_bus_init(struct usb_bus *ubus) 289 { 290 struct mon_bus *mbus; 291 292 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL) 293 goto err_alloc; 294 kref_init(&mbus->ref); 295 spin_lock_init(&mbus->lock); 296 INIT_LIST_HEAD(&mbus->r_list); 297 298 /* 299 * We don't need to take a reference to ubus, because we receive 300 * a notification if the bus is about to be removed. 301 */ 302 mbus->u_bus = ubus; 303 ubus->mon_bus = mbus; 304 305 mbus->text_inited = mon_text_add(mbus, ubus->busnum); 306 // mon_bin_add(...) 307 308 mutex_lock(&mon_lock); 309 list_add_tail(&mbus->bus_link, &mon_buses); 310 mutex_unlock(&mon_lock); 311 return; 312 313 err_alloc: 314 return; 315 } 316 317 static void mon_bus0_init(void) 318 { 319 struct mon_bus *mbus = &mon_bus0; 320 321 kref_init(&mbus->ref); 322 spin_lock_init(&mbus->lock); 323 INIT_LIST_HEAD(&mbus->r_list); 324 325 mbus->text_inited = mon_text_add(mbus, 0); 326 // mbus->bin_inited = mon_bin_add(mbus, 0); 327 } 328 329 /* 330 * Search a USB bus by number. Notice that USB bus numbers start from one, 331 * which we may later use to identify "all" with zero. 332 * 333 * This function must be called with mon_lock held. 334 * 335 * This is obviously inefficient and may be revised in the future. 336 */ 337 struct mon_bus *mon_bus_lookup(unsigned int num) 338 { 339 struct list_head *p; 340 struct mon_bus *mbus; 341 342 if (num == 0) { 343 return &mon_bus0; 344 } 345 list_for_each (p, &mon_buses) { 346 mbus = list_entry(p, struct mon_bus, bus_link); 347 if (mbus->u_bus->busnum == num) { 348 return mbus; 349 } 350 } 351 return NULL; 352 } 353 354 static int __init mon_init(void) 355 { 356 struct usb_bus *ubus; 357 int rc; 358 359 if ((rc = mon_text_init()) != 0) 360 goto err_text; 361 if ((rc = mon_bin_init()) != 0) 362 goto err_bin; 363 364 mon_bus0_init(); 365 366 if (usb_mon_register(&mon_ops_0) != 0) { 367 printk(KERN_NOTICE TAG ": unable to register with the core\n"); 368 rc = -ENODEV; 369 goto err_reg; 370 } 371 // MOD_INC_USE_COUNT(which_module?); 372 373 usb_register_notify(&mon_nb); 374 375 mutex_lock(&usb_bus_list_lock); 376 list_for_each_entry (ubus, &usb_bus_list, bus_list) { 377 mon_bus_init(ubus); 378 } 379 mutex_unlock(&usb_bus_list_lock); 380 return 0; 381 382 err_reg: 383 mon_bin_exit(); 384 err_bin: 385 mon_text_exit(); 386 err_text: 387 return rc; 388 } 389 390 static void __exit mon_exit(void) 391 { 392 struct mon_bus *mbus; 393 struct list_head *p; 394 395 usb_unregister_notify(&mon_nb); 396 usb_mon_deregister(); 397 398 mutex_lock(&mon_lock); 399 400 while (!list_empty(&mon_buses)) { 401 p = mon_buses.next; 402 mbus = list_entry(p, struct mon_bus, bus_link); 403 list_del(p); 404 405 if (mbus->text_inited) 406 mon_text_del(mbus); 407 408 /* 409 * This never happens, because the open/close paths in 410 * file level maintain module use counters and so rmmod fails 411 * before reaching here. However, better be safe... 412 */ 413 if (mbus->nreaders) { 414 printk(KERN_ERR TAG 415 ": Outstanding opens (%d) on usb%d, leaking...\n", 416 mbus->nreaders, mbus->u_bus->busnum); 417 atomic_set(&mbus->ref.refcount, 2); /* Force leak */ 418 } 419 420 mon_dissolve(mbus, mbus->u_bus); 421 kref_put(&mbus->ref, mon_bus_drop); 422 } 423 424 mbus = &mon_bus0; 425 if (mbus->text_inited) 426 mon_text_del(mbus); 427 428 mutex_unlock(&mon_lock); 429 430 mon_text_exit(); 431 mon_bin_exit(); 432 } 433 434 module_init(mon_init); 435 module_exit(mon_exit); 436 437 MODULE_LICENSE("GPL"); 438