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