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> | |
12 | #include <linux/debugfs.h> | |
13 | #include <linux/smp_lock.h> | |
72adaa96 | 14 | #include <linux/notifier.h> |
4186ecf8 | 15 | #include <linux/mutex.h> |
1da177e4 LT |
16 | |
17 | #include "usb_mon.h" | |
18 | #include "../core/hcd.h" | |
19 | ||
20 | static void mon_submit(struct usb_bus *ubus, struct urb *urb); | |
21 | static void mon_complete(struct usb_bus *ubus, struct urb *urb); | |
22 | static void mon_stop(struct mon_bus *mbus); | |
23 | static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus); | |
24 | static void mon_bus_drop(struct kref *r); | |
25 | static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus); | |
26 | ||
4186ecf8 | 27 | DEFINE_MUTEX(mon_lock); |
1da177e4 LT |
28 | |
29 | static struct dentry *mon_dir; /* /dbg/usbmon */ | |
30 | static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */ | |
31 | ||
32 | /* | |
33 | * Link a reader into the bus. | |
34 | * | |
35 | * This must be called with mon_lock taken because of mbus->ref. | |
36 | */ | |
37 | void mon_reader_add(struct mon_bus *mbus, struct mon_reader *r) | |
38 | { | |
39 | unsigned long flags; | |
40 | struct usb_bus *ubus; | |
41 | ||
42 | spin_lock_irqsave(&mbus->lock, flags); | |
43 | if (mbus->nreaders == 0) { | |
44 | ubus = mbus->u_bus; | |
45 | if (ubus->monitored) { | |
46 | /* | |
47 | * Something is really broken, refuse to go on and | |
48 | * possibly corrupt ops pointers or worse. | |
49 | */ | |
50 | printk(KERN_ERR TAG ": bus %d is already monitored\n", | |
51 | ubus->busnum); | |
52 | spin_unlock_irqrestore(&mbus->lock, flags); | |
53 | return; | |
54 | } | |
55 | ubus->monitored = 1; | |
56 | } | |
57 | mbus->nreaders++; | |
58 | list_add_tail(&r->r_link, &mbus->r_list); | |
59 | spin_unlock_irqrestore(&mbus->lock, flags); | |
60 | ||
61 | kref_get(&mbus->ref); | |
62 | } | |
63 | ||
64 | /* | |
65 | * Unlink reader from the bus. | |
66 | * | |
67 | * This is called with mon_lock taken, so we can decrement mbus->ref. | |
68 | */ | |
69 | void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r) | |
70 | { | |
71 | unsigned long flags; | |
72 | ||
73 | spin_lock_irqsave(&mbus->lock, flags); | |
74 | list_del(&r->r_link); | |
75 | --mbus->nreaders; | |
76 | if (mbus->nreaders == 0) | |
77 | mon_stop(mbus); | |
78 | spin_unlock_irqrestore(&mbus->lock, flags); | |
79 | ||
80 | kref_put(&mbus->ref, mon_bus_drop); | |
81 | } | |
82 | ||
83 | /* | |
84 | */ | |
85 | static void mon_submit(struct usb_bus *ubus, struct urb *urb) | |
86 | { | |
87 | struct mon_bus *mbus; | |
88 | unsigned long flags; | |
89 | struct list_head *pos; | |
90 | struct mon_reader *r; | |
91 | ||
92 | mbus = ubus->mon_bus; | |
93 | if (mbus == NULL) | |
94 | goto out_unlocked; | |
95 | ||
96 | spin_lock_irqsave(&mbus->lock, flags); | |
97 | if (mbus->nreaders == 0) | |
98 | goto out_locked; | |
99 | ||
100 | list_for_each (pos, &mbus->r_list) { | |
101 | r = list_entry(pos, struct mon_reader, r_link); | |
102 | r->rnf_submit(r->r_data, urb); | |
103 | } | |
104 | ||
105 | spin_unlock_irqrestore(&mbus->lock, flags); | |
106 | return; | |
107 | ||
108 | out_locked: | |
109 | spin_unlock_irqrestore(&mbus->lock, flags); | |
110 | out_unlocked: | |
111 | return; | |
112 | } | |
113 | ||
114 | /* | |
115 | */ | |
116 | static void mon_submit_error(struct usb_bus *ubus, struct urb *urb, int err) | |
117 | { | |
118 | struct mon_bus *mbus; | |
119 | ||
120 | mbus = ubus->mon_bus; | |
121 | if (mbus == NULL) | |
122 | goto out_unlocked; | |
123 | ||
124 | /* | |
125 | * XXX Capture the error code and the 'E' event. | |
126 | */ | |
127 | ||
128 | return; | |
129 | ||
130 | out_unlocked: | |
131 | return; | |
132 | } | |
133 | ||
134 | /* | |
135 | */ | |
136 | static void mon_complete(struct usb_bus *ubus, struct urb *urb) | |
137 | { | |
138 | struct mon_bus *mbus; | |
139 | unsigned long flags; | |
140 | struct list_head *pos; | |
141 | struct mon_reader *r; | |
142 | ||
143 | mbus = ubus->mon_bus; | |
144 | if (mbus == NULL) { | |
145 | /* | |
146 | * This should not happen. | |
147 | * At this point we do not even know the bus number... | |
148 | */ | |
149 | printk(KERN_ERR TAG ": Null mon bus in URB, pipe 0x%x\n", | |
150 | urb->pipe); | |
151 | return; | |
152 | } | |
153 | ||
154 | spin_lock_irqsave(&mbus->lock, flags); | |
155 | list_for_each (pos, &mbus->r_list) { | |
156 | r = list_entry(pos, struct mon_reader, r_link); | |
157 | r->rnf_complete(r->r_data, urb); | |
158 | } | |
159 | spin_unlock_irqrestore(&mbus->lock, flags); | |
160 | } | |
161 | ||
162 | /* int (*unlink_urb) (struct urb *urb, int status); */ | |
163 | ||
164 | /* | |
165 | * Stop monitoring. | |
166 | * Obviously this must be well locked, so no need to play with mb's. | |
167 | */ | |
168 | static void mon_stop(struct mon_bus *mbus) | |
169 | { | |
170 | struct usb_bus *ubus = mbus->u_bus; | |
171 | ||
172 | /* | |
173 | * A stop can be called for a dissolved mon_bus in case of | |
174 | * a reader staying across an rmmod foo_hcd. | |
175 | */ | |
176 | if (ubus != NULL) { | |
177 | ubus->monitored = 0; | |
178 | mb(); | |
179 | } | |
180 | } | |
181 | ||
182 | /* | |
183 | * Add a USB bus (usually by a modprobe foo-hcd) | |
184 | * | |
185 | * This does not return an error code because the core cannot care less | |
186 | * if monitoring is not established. | |
187 | */ | |
188 | static void mon_bus_add(struct usb_bus *ubus) | |
189 | { | |
190 | mon_bus_init(mon_dir, ubus); | |
191 | } | |
192 | ||
193 | /* | |
194 | * Remove a USB bus (either from rmmod foo-hcd or from a hot-remove event). | |
195 | */ | |
196 | static void mon_bus_remove(struct usb_bus *ubus) | |
197 | { | |
198 | struct mon_bus *mbus = ubus->mon_bus; | |
199 | ||
4186ecf8 | 200 | mutex_lock(&mon_lock); |
1da177e4 LT |
201 | list_del(&mbus->bus_link); |
202 | debugfs_remove(mbus->dent_t); | |
203 | debugfs_remove(mbus->dent_s); | |
204 | ||
205 | mon_dissolve(mbus, ubus); | |
206 | kref_put(&mbus->ref, mon_bus_drop); | |
4186ecf8 | 207 | mutex_unlock(&mon_lock); |
1da177e4 LT |
208 | } |
209 | ||
72adaa96 GKH |
210 | static int mon_notify(struct notifier_block *self, unsigned long action, |
211 | void *dev) | |
212 | { | |
213 | switch (action) { | |
214 | case USB_BUS_ADD: | |
215 | mon_bus_add(dev); | |
216 | break; | |
217 | case USB_BUS_REMOVE: | |
218 | mon_bus_remove(dev); | |
219 | } | |
220 | return NOTIFY_OK; | |
221 | } | |
222 | ||
223 | static struct notifier_block mon_nb = { | |
224 | .notifier_call = mon_notify, | |
225 | }; | |
226 | ||
1da177e4 LT |
227 | /* |
228 | * Ops | |
229 | */ | |
230 | static struct usb_mon_operations mon_ops_0 = { | |
231 | .urb_submit = mon_submit, | |
232 | .urb_submit_error = mon_submit_error, | |
233 | .urb_complete = mon_complete, | |
1da177e4 LT |
234 | }; |
235 | ||
236 | /* | |
237 | * Tear usb_bus and mon_bus apart. | |
238 | */ | |
239 | static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus) | |
240 | { | |
241 | ||
242 | /* | |
243 | * Never happens, but... | |
244 | */ | |
245 | if (ubus->monitored) { | |
246 | printk(KERN_ERR TAG ": bus %d is dissolved while monitored\n", | |
247 | ubus->busnum); | |
248 | ubus->monitored = 0; | |
249 | mb(); | |
250 | } | |
251 | ||
252 | ubus->mon_bus = NULL; | |
253 | mbus->u_bus = NULL; | |
254 | mb(); | |
255 | // usb_bus_put(ubus); | |
256 | } | |
257 | ||
258 | /* | |
259 | */ | |
260 | static void mon_bus_drop(struct kref *r) | |
261 | { | |
262 | struct mon_bus *mbus = container_of(r, struct mon_bus, ref); | |
263 | kfree(mbus); | |
264 | } | |
265 | ||
266 | /* | |
267 | * Initialize a bus for us: | |
268 | * - allocate mon_bus | |
269 | * - refcount USB bus struct | |
270 | * - link | |
271 | */ | |
272 | static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus) | |
273 | { | |
274 | struct dentry *d; | |
275 | struct mon_bus *mbus; | |
276 | enum { NAMESZ = 10 }; | |
277 | char name[NAMESZ]; | |
278 | int rc; | |
279 | ||
80b6ca48 | 280 | if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL) |
1da177e4 | 281 | goto err_alloc; |
1da177e4 LT |
282 | kref_init(&mbus->ref); |
283 | spin_lock_init(&mbus->lock); | |
284 | INIT_LIST_HEAD(&mbus->r_list); | |
285 | ||
286 | /* | |
287 | * This usb_bus_get here is superfluous, because we receive | |
288 | * a notification if usb_bus is about to be removed. | |
289 | */ | |
290 | // usb_bus_get(ubus); | |
291 | mbus->u_bus = ubus; | |
292 | ubus->mon_bus = mbus; | |
293 | ||
294 | rc = snprintf(name, NAMESZ, "%dt", ubus->busnum); | |
295 | if (rc <= 0 || rc >= NAMESZ) | |
296 | goto err_print_t; | |
297 | d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_text); | |
298 | if (d == NULL) | |
299 | goto err_create_t; | |
300 | mbus->dent_t = d; | |
301 | ||
302 | rc = snprintf(name, NAMESZ, "%ds", ubus->busnum); | |
303 | if (rc <= 0 || rc >= NAMESZ) | |
304 | goto err_print_s; | |
305 | d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_stat); | |
306 | if (d == NULL) | |
307 | goto err_create_s; | |
308 | mbus->dent_s = d; | |
309 | ||
4186ecf8 | 310 | mutex_lock(&mon_lock); |
1da177e4 | 311 | list_add_tail(&mbus->bus_link, &mon_buses); |
4186ecf8 | 312 | mutex_unlock(&mon_lock); |
1da177e4 LT |
313 | return; |
314 | ||
315 | err_create_s: | |
316 | err_print_s: | |
317 | debugfs_remove(mbus->dent_t); | |
318 | err_create_t: | |
319 | err_print_t: | |
320 | kfree(mbus); | |
321 | err_alloc: | |
322 | return; | |
323 | } | |
324 | ||
325 | static int __init mon_init(void) | |
326 | { | |
327 | struct usb_bus *ubus; | |
328 | struct dentry *mondir; | |
329 | ||
330 | mondir = debugfs_create_dir("usbmon", NULL); | |
331 | if (IS_ERR(mondir)) { | |
da5ca008 | 332 | printk(KERN_NOTICE TAG ": debugfs is not available\n"); |
1da177e4 LT |
333 | return -ENODEV; |
334 | } | |
335 | if (mondir == NULL) { | |
336 | printk(KERN_NOTICE TAG ": unable to create usbmon directory\n"); | |
337 | return -ENODEV; | |
338 | } | |
339 | mon_dir = mondir; | |
340 | ||
341 | if (usb_mon_register(&mon_ops_0) != 0) { | |
342 | printk(KERN_NOTICE TAG ": unable to register with the core\n"); | |
343 | debugfs_remove(mondir); | |
344 | return -ENODEV; | |
345 | } | |
346 | // MOD_INC_USE_COUNT(which_module?); | |
347 | ||
72adaa96 GKH |
348 | usb_register_notify(&mon_nb); |
349 | ||
4186ecf8 | 350 | mutex_lock(&usb_bus_list_lock); |
1da177e4 LT |
351 | list_for_each_entry (ubus, &usb_bus_list, bus_list) { |
352 | mon_bus_init(mondir, ubus); | |
353 | } | |
4186ecf8 | 354 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 LT |
355 | return 0; |
356 | } | |
357 | ||
358 | static void __exit mon_exit(void) | |
359 | { | |
360 | struct mon_bus *mbus; | |
361 | struct list_head *p; | |
362 | ||
72adaa96 | 363 | usb_unregister_notify(&mon_nb); |
1da177e4 LT |
364 | usb_mon_deregister(); |
365 | ||
4186ecf8 | 366 | mutex_lock(&mon_lock); |
1da177e4 LT |
367 | while (!list_empty(&mon_buses)) { |
368 | p = mon_buses.next; | |
369 | mbus = list_entry(p, struct mon_bus, bus_link); | |
370 | list_del(p); | |
371 | ||
372 | debugfs_remove(mbus->dent_t); | |
373 | debugfs_remove(mbus->dent_s); | |
374 | ||
375 | /* | |
376 | * This never happens, because the open/close paths in | |
377 | * file level maintain module use counters and so rmmod fails | |
378 | * before reaching here. However, better be safe... | |
379 | */ | |
380 | if (mbus->nreaders) { | |
381 | printk(KERN_ERR TAG | |
382 | ": Outstanding opens (%d) on usb%d, leaking...\n", | |
383 | mbus->nreaders, mbus->u_bus->busnum); | |
384 | atomic_set(&mbus->ref.refcount, 2); /* Force leak */ | |
385 | } | |
386 | ||
387 | mon_dissolve(mbus, mbus->u_bus); | |
388 | kref_put(&mbus->ref, mon_bus_drop); | |
389 | } | |
4186ecf8 | 390 | mutex_unlock(&mon_lock); |
1da177e4 LT |
391 | |
392 | debugfs_remove(mon_dir); | |
393 | } | |
394 | ||
395 | module_init(mon_init); | |
396 | module_exit(mon_exit); | |
397 | ||
398 | MODULE_LICENSE("GPL"); |