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