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