Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * The USB Monitor, inspired by Dave Harding's USBMon. |
| 3 | * |
| 4 | * mon_main.c: Main file, module initiation and exit, registrations, etc. |
Pete Zaitcev | da5ca00 | 2005-08-16 15:16:46 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2005 Pete Zaitcev (zaitcev@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/usb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/smp_lock.h> |
Greg Kroah-Hartman | 72adaa9 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 13 | #include <linux/notifier.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include "usb_mon.h" |
| 17 | #include "../core/hcd.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 22 | static void mon_bus_init(struct usb_bus *ubus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 24 | DEFINE_MUTEX(mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 26 | struct mon_bus mon_bus0; /* Pseudo bus meaning "all buses" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 37 | struct list_head *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | spin_lock_irqsave(&mbus->lock, flags); |
| 40 | if (mbus->nreaders == 0) { |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 79 | static void mon_bus_submit(struct mon_bus *mbus, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | unsigned long flags; |
| 82 | struct list_head *pos; |
| 83 | struct mon_reader *r; |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | spin_lock_irqsave(&mbus->lock, flags); |
Pete Zaitcev | 5b1c674 | 2006-06-09 20:10:10 -0700 | [diff] [blame] | 86 | mbus->cnt_events++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | spin_unlock_irqrestore(&mbus->lock, flags); |
| 92 | return; |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
| 105 | */ |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 106 | static void mon_bus_submit_error(struct mon_bus *mbus, struct urb *urb, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 108 | unsigned long flags; |
| 109 | struct list_head *pos; |
| 110 | struct mon_reader *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 112 | spin_lock_irqsave(&mbus->lock, flags); |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 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 | } |
Pete Zaitcev | 12e72fe | 2006-06-09 22:03:32 -0700 | [diff] [blame] | 118 | spin_unlock_irqrestore(&mbus->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return; |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | */ |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 133 | static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | unsigned long flags; |
| 136 | struct list_head *pos; |
| 137 | struct mon_reader *r; |
| 138 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 163 | mon_bus_complete(mbus, urb); |
| 164 | mon_bus_complete(&mon_bus0, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* int (*unlink_urb) (struct urb *urb, int status); */ |
| 168 | |
| 169 | /* |
| 170 | * Stop monitoring. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | */ |
| 172 | static void mon_stop(struct mon_bus *mbus) |
| 173 | { |
| 174 | struct usb_bus *ubus = mbus->u_bus; |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 175 | struct list_head *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 206 | mon_bus_init(ubus); |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 207 | mutex_lock(&mon_lock); |
| 208 | if (mon_bus0.nreaders != 0) |
| 209 | ubus->monitored = 1; |
| 210 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 220 | mutex_lock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | list_del(&mbus->bus_link); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 222 | if (mbus->text_inited) |
| 223 | mon_text_del(mbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | mon_dissolve(mbus, ubus); |
| 226 | kref_put(&mbus->ref, mon_bus_drop); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 227 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Greg Kroah-Hartman | 72adaa9 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (ubus->monitored) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | ubus->monitored = 0; |
| 264 | mb(); |
| 265 | } |
| 266 | |
| 267 | ubus->mon_bus = NULL; |
| 268 | mbus->u_bus = NULL; |
| 269 | mb(); |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 270 | |
| 271 | /* We want synchronize_irq() here, but that needs an argument. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 288 | static void mon_bus_init(struct usb_bus *ubus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | struct mon_bus *mbus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 292 | if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | goto err_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | kref_init(&mbus->ref); |
| 295 | spin_lock_init(&mbus->lock); |
| 296 | INIT_LIST_HEAD(&mbus->r_list); |
| 297 | |
| 298 | /* |
Alan Stern | 1720058 | 2006-08-30 11:32:52 -0400 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | mbus->u_bus = ubus; |
| 303 | ubus->mon_bus = mbus; |
| 304 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 305 | mbus->text_inited = mon_text_add(mbus, ubus->busnum); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 306 | // mon_bin_add(...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 308 | mutex_lock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | list_add_tail(&mbus->bus_link, &mon_buses); |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 310 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return; |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | err_alloc: |
| 314 | return; |
| 315 | } |
| 316 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 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 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 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 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 342 | if (num == 0) { |
| 343 | return &mon_bus0; |
| 344 | } |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | static int __init mon_init(void) |
| 355 | { |
| 356 | struct usb_bus *ubus; |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 357 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 359 | if ((rc = mon_text_init()) != 0) |
| 360 | goto err_text; |
| 361 | if ((rc = mon_bin_init()) != 0) |
| 362 | goto err_bin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 364 | mon_bus0_init(); |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (usb_mon_register(&mon_ops_0) != 0) { |
| 367 | printk(KERN_NOTICE TAG ": unable to register with the core\n"); |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 368 | rc = -ENODEV; |
| 369 | goto err_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | // MOD_INC_USE_COUNT(which_module?); |
| 372 | |
Greg Kroah-Hartman | 72adaa9 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 373 | usb_register_notify(&mon_nb); |
| 374 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 375 | mutex_lock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | list_for_each_entry (ubus, &usb_bus_list, bus_list) { |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 377 | mon_bus_init(ubus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 379 | mutex_unlock(&usb_bus_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return 0; |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 381 | |
| 382 | err_reg: |
| 383 | mon_bin_exit(); |
| 384 | err_bin: |
| 385 | mon_text_exit(); |
| 386 | err_text: |
| 387 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void __exit mon_exit(void) |
| 391 | { |
| 392 | struct mon_bus *mbus; |
| 393 | struct list_head *p; |
| 394 | |
Greg Kroah-Hartman | 72adaa9 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 395 | usb_unregister_notify(&mon_nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | usb_mon_deregister(); |
| 397 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 398 | mutex_lock(&mon_lock); |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 405 | if (mbus->text_inited) |
| 406 | mon_text_del(mbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Pete Zaitcev | ecb658d | 2007-04-11 13:47:26 -0700 | [diff] [blame^] | 423 | |
| 424 | mbus = &mon_bus0; |
| 425 | if (mbus->text_inited) |
| 426 | mon_text_del(mbus); |
| 427 | |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 428 | mutex_unlock(&mon_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 430 | mon_text_exit(); |
| 431 | mon_bin_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | module_init(mon_init); |
| 435 | module_exit(mon_exit); |
| 436 | |
| 437 | MODULE_LICENSE("GPL"); |