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