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