blob: bbf54396e5f2d0e3a1fcef2f7eb24fa3016a15b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The USB Monitor, inspired by Dave Harding's USBMon.
3 *
4 * This is a text format reader.
5 */
6
7#include <linux/kernel.h>
8#include <linux/list.h>
9#include <linux/usb.h>
10#include <linux/time.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010011#include <linux/mutex.h>
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080012#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/uaccess.h>
14
15#include "usb_mon.h"
16
17/*
18 * No, we do not want arbitrarily long data strings.
19 * Use the binary interface if you want to capture bulk data!
20 */
21#define DATA_MAX 32
22
23/*
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070024 * Defined by USB 2.0 clause 9.3, table 9.2.
25 */
26#define SETUP_MAX 8
27
28/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * This limit exists to prevent OOMs when the user process stops reading.
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070030 * If usbmon were available to unprivileged processes, it might be open
31 * to a local DoS. But we have to keep to root in order to prevent
32 * password sniffing from HID devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080034#define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080036/*
37 * Potentially unlimited number; we limit it for similar allocations.
38 * The usbfs limits this to 128, but we're not quite as generous.
39 */
40#define ISODESC_MAX 5
41
42#define PRINTF_DFL 250 /* with 5 ISOs segs */
43
44struct mon_iso_desc {
45 int status;
46 unsigned int offset;
47 unsigned int length; /* Unsigned here, signed in URB. Historic. */
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50struct mon_event_text {
51 struct list_head e_link;
52 int type; /* submit, complete, etc. */
53 unsigned int pipe; /* Pipe */
54 unsigned long id; /* From pointer, most of the time */
55 unsigned int tstamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080056 int busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int length; /* Depends on type: xfer length or act length */
58 int status;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080059 int interval;
60 int start_frame;
61 int error_count;
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070062 char setup_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 char data_flag;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080064 int numdesc; /* Full number */
65 struct mon_iso_desc isodesc[ISODESC_MAX];
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -070066 unsigned char setup[SETUP_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned char data[DATA_MAX];
68};
69
70#define SLAB_NAME_SZ 30
71struct mon_reader_text {
Christoph Lametere18b8902006-12-06 20:33:20 -080072 struct kmem_cache *e_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 int nevents;
74 struct list_head e_list;
75 struct mon_reader r; /* In C, parent class can be placed anywhere */
76
77 wait_queue_head_t wait;
78 int printf_size;
79 char *printf_buf;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010080 struct mutex printf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 char slab_name[SLAB_NAME_SZ];
83};
84
Pete Zaitcev6f23ee12006-12-30 22:43:10 -080085static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
86
Christoph Lametere18b8902006-12-06 20:33:20 -080087static void mon_text_ctor(void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Pete Zaitcevf1c9e302007-02-24 19:27:33 -080089struct mon_text_ptr {
90 int cnt, limit;
91 char *pbuf;
92};
93
94static struct mon_event_text *
95 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
96static void mon_text_read_head_t(struct mon_reader_text *rp,
97 struct mon_text_ptr *p, const struct mon_event_text *ep);
98static void mon_text_read_head_u(struct mon_reader_text *rp,
99 struct mon_text_ptr *p, const struct mon_event_text *ep);
100static void mon_text_read_statset(struct mon_reader_text *rp,
101 struct mon_text_ptr *p, const struct mon_event_text *ep);
102static void mon_text_read_intstat(struct mon_reader_text *rp,
103 struct mon_text_ptr *p, const struct mon_event_text *ep);
104static void mon_text_read_isostat(struct mon_reader_text *rp,
105 struct mon_text_ptr *p, const struct mon_event_text *ep);
106static void mon_text_read_isodesc(struct mon_reader_text *rp,
107 struct mon_text_ptr *p, const struct mon_event_text *ep);
108static void mon_text_read_data(struct mon_reader_text *rp,
109 struct mon_text_ptr *p, const struct mon_event_text *ep);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * mon_text_submit
113 * mon_text_complete
114 *
115 * May be called from an interrupt.
116 *
117 * This is called with the whole mon_bus locked, so no additional lock.
118 */
119
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700120static inline char mon_text_get_setup(struct mon_event_text *ep,
Alan Stern4d6cd482006-08-30 11:35:21 -0400121 struct urb *urb, char ev_type, struct mon_bus *mbus)
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700122{
123
124 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
125 return '-';
126
Alan Stern4d6cd482006-08-30 11:35:21 -0400127 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
Pete Zaitcevbc506512005-09-01 14:35:05 -0700128 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700129 if (urb->setup_packet == NULL)
130 return 'Z'; /* '0' would be not as pretty. */
131
132 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
133 return 0;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
Alan Stern4d6cd482006-08-30 11:35:21 -0400137 int len, char ev_type, struct mon_bus *mbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (len <= 0)
142 return 'L';
Pete Zaitcev02568392005-08-15 16:53:57 -0700143 if (len >= DATA_MAX)
144 len = DATA_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800146 if (usb_pipein(pipe)) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800147 if (ev_type != 'C')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800148 return '<';
149 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800150 if (ev_type != 'S')
Pete Zaitcevb9b09422005-12-03 21:52:10 -0800151 return '>';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Pete Zaitcev02568392005-08-15 16:53:57 -0700154 /*
155 * The check to see if it's safe to poke at data has an enormous
156 * number of corner cases, but it seems that the following is
157 * more or less safe.
158 *
Pete Zaitcev5b1c6742006-06-09 20:10:10 -0700159 * We do not even try to look at transfer_buffer, because it can
Pete Zaitcev02568392005-08-15 16:53:57 -0700160 * contain non-NULL garbage in case the upper level promised to
161 * set DMA for the HCD.
162 */
Alan Stern4d6cd482006-08-30 11:35:21 -0400163 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
Pete Zaitcev02568392005-08-15 16:53:57 -0700164 return mon_dmapeek(ep->data, urb->transfer_dma, len);
165
166 if (urb->transfer_buffer == NULL)
167 return 'Z'; /* '0' would be not as pretty. */
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 memcpy(ep->data, urb->transfer_buffer, len);
170 return 0;
171}
172
173static inline unsigned int mon_get_timestamp(void)
174{
175 struct timeval tval;
176 unsigned int stamp;
177
178 do_gettimeofday(&tval);
179 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
180 stamp = stamp * 1000000 + tval.tv_usec;
181 return stamp;
182}
183
184static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
185 char ev_type)
186{
187 struct mon_event_text *ep;
188 unsigned int stamp;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800189 struct usb_iso_packet_descriptor *fp;
190 struct mon_iso_desc *dp;
191 int i, ndesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 stamp = mon_get_timestamp();
194
195 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800196 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 rp->r.m_bus->cnt_text_lost++;
198 return;
199 }
200
201 ep->type = ev_type;
202 ep->pipe = urb->pipe;
203 ep->id = (unsigned long) urb;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800204 ep->busnum = rp->r.m_bus->u_bus->busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ep->tstamp = stamp;
206 ep->length = (ev_type == 'S') ?
207 urb->transfer_buffer_length : urb->actual_length;
208 /* Collecting status makes debugging sense for submits, too */
209 ep->status = urb->status;
210
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800211 if (usb_pipeint(urb->pipe)) {
212 ep->interval = urb->interval;
213 } else if (usb_pipeisoc(urb->pipe)) {
214 ep->interval = urb->interval;
215 ep->start_frame = urb->start_frame;
216 ep->error_count = urb->error_count;
217 }
218 ep->numdesc = urb->number_of_packets;
219 if (usb_pipeisoc(urb->pipe) && urb->number_of_packets > 0) {
220 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
221 ndesc = ISODESC_MAX;
222 fp = urb->iso_frame_desc;
223 dp = ep->isodesc;
224 for (i = 0; i < ndesc; i++) {
225 dp->status = fp->status;
226 dp->offset = fp->offset;
227 dp->length = (ev_type == 'S') ?
228 fp->length : fp->actual_length;
229 fp++;
230 dp++;
231 }
232 }
233
Alan Stern4d6cd482006-08-30 11:35:21 -0400234 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
235 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
236 rp->r.m_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 rp->nevents++;
239 list_add_tail(&ep->e_link, &rp->e_list);
240 wake_up(&rp->wait);
241}
242
243static void mon_text_submit(void *data, struct urb *urb)
244{
245 struct mon_reader_text *rp = data;
246 mon_text_event(rp, urb, 'S');
247}
248
249static void mon_text_complete(void *data, struct urb *urb)
250{
251 struct mon_reader_text *rp = data;
252 mon_text_event(rp, urb, 'C');
253}
254
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700255static void mon_text_error(void *data, struct urb *urb, int error)
256{
257 struct mon_reader_text *rp = data;
258 struct mon_event_text *ep;
259
260 if (rp->nevents >= EVENT_MAX ||
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800261 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700262 rp->r.m_bus->cnt_text_lost++;
263 return;
264 }
265
266 ep->type = 'E';
267 ep->pipe = urb->pipe;
268 ep->id = (unsigned long) urb;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800269 ep->busnum = 0;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700270 ep->tstamp = 0;
271 ep->length = 0;
272 ep->status = error;
273
274 ep->setup_flag = '-';
275 ep->data_flag = 'E';
276
277 rp->nevents++;
278 list_add_tail(&ep->e_link, &rp->e_list);
279 wake_up(&rp->wait);
280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*
283 * Fetch next event from the circular buffer.
284 */
285static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
286 struct mon_bus *mbus)
287{
288 struct list_head *p;
289 unsigned long flags;
290
291 spin_lock_irqsave(&mbus->lock, flags);
292 if (list_empty(&rp->e_list)) {
293 spin_unlock_irqrestore(&mbus->lock, flags);
294 return NULL;
295 }
296 p = rp->e_list.next;
297 list_del(p);
298 --rp->nevents;
299 spin_unlock_irqrestore(&mbus->lock, flags);
300 return list_entry(p, struct mon_event_text, e_link);
301}
302
303/*
304 */
305static int mon_text_open(struct inode *inode, struct file *file)
306{
307 struct mon_bus *mbus;
308 struct usb_bus *ubus;
309 struct mon_reader_text *rp;
310 int rc;
311
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100312 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700313 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ubus = mbus->u_bus;
315
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100316 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (rp == NULL) {
318 rc = -ENOMEM;
319 goto err_alloc;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 INIT_LIST_HEAD(&rp->e_list);
322 init_waitqueue_head(&rp->wait);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100323 mutex_init(&rp->printf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 rp->printf_size = PRINTF_DFL;
326 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
327 if (rp->printf_buf == NULL) {
328 rc = -ENOMEM;
329 goto err_alloc_pr;
330 }
331
332 rp->r.m_bus = mbus;
333 rp->r.r_data = rp;
334 rp->r.rnf_submit = mon_text_submit;
Pete Zaitcev12e72fe2006-06-09 22:03:32 -0700335 rp->r.rnf_error = mon_text_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 rp->r.rnf_complete = mon_text_complete;
337
338 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
339 (long)rp);
340 rp->e_slab = kmem_cache_create(rp->slab_name,
341 sizeof(struct mon_event_text), sizeof(long), 0,
Christoph Lameter028d2a32006-06-30 02:34:47 -0700342 mon_text_ctor, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (rp->e_slab == NULL) {
344 rc = -ENOMEM;
345 goto err_slab;
346 }
347
348 mon_reader_add(mbus, &rp->r);
349
350 file->private_data = rp;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100351 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353
354// err_busy:
355// kmem_cache_destroy(rp->e_slab);
356err_slab:
357 kfree(rp->printf_buf);
358err_alloc_pr:
359 kfree(rp);
360err_alloc:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100361 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return rc;
363}
364
365/*
366 * For simplicity, we read one record in one system call and throw out
367 * what does not fit. This means that the following does not work:
368 * dd if=/dbg/usbmon/0t bs=10
369 * Also, we do not allow seeks and do not bother advancing the offset.
370 */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800371static ssize_t mon_text_read_t(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 size_t nbytes, loff_t *ppos)
373{
374 struct mon_reader_text *rp = file->private_data;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800375 struct mon_event_text *ep;
376 struct mon_text_ptr ptr;
377
378 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
379 return PTR_ERR(ep);
380 mutex_lock(&rp->printf_lock);
381 ptr.cnt = 0;
382 ptr.pbuf = rp->printf_buf;
383 ptr.limit = rp->printf_size;
384
385 mon_text_read_head_t(rp, &ptr, ep);
386 mon_text_read_statset(rp, &ptr, ep);
387 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
388 " %d", ep->length);
389 mon_text_read_data(rp, &ptr, ep);
390
391 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
392 ptr.cnt = -EFAULT;
393 mutex_unlock(&rp->printf_lock);
394 kmem_cache_free(rp->e_slab, ep);
395 return ptr.cnt;
396}
397
398static ssize_t mon_text_read_u(struct file *file, char __user *buf,
399 size_t nbytes, loff_t *ppos)
400{
401 struct mon_reader_text *rp = file->private_data;
402 struct mon_event_text *ep;
403 struct mon_text_ptr ptr;
404
405 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
406 return PTR_ERR(ep);
407 mutex_lock(&rp->printf_lock);
408 ptr.cnt = 0;
409 ptr.pbuf = rp->printf_buf;
410 ptr.limit = rp->printf_size;
411
412 mon_text_read_head_u(rp, &ptr, ep);
413 if (ep->type == 'E') {
414 mon_text_read_statset(rp, &ptr, ep);
415 } else if (usb_pipeisoc(ep->pipe)) {
416 mon_text_read_isostat(rp, &ptr, ep);
417 mon_text_read_isodesc(rp, &ptr, ep);
418 } else if (usb_pipeint(ep->pipe)) {
419 mon_text_read_intstat(rp, &ptr, ep);
420 } else {
421 mon_text_read_statset(rp, &ptr, ep);
422 }
423 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
424 " %d", ep->length);
425 mon_text_read_data(rp, &ptr, ep);
426
427 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
428 ptr.cnt = -EFAULT;
429 mutex_unlock(&rp->printf_lock);
430 kmem_cache_free(rp->e_slab, ep);
431 return ptr.cnt;
432}
433
434static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
435 struct file *file)
436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct mon_bus *mbus = rp->r.m_bus;
438 DECLARE_WAITQUEUE(waita, current);
439 struct mon_event_text *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 add_wait_queue(&rp->wait, &waita);
442 set_current_state(TASK_INTERRUPTIBLE);
443 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
444 if (file->f_flags & O_NONBLOCK) {
445 set_current_state(TASK_RUNNING);
446 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800447 return ERR_PTR(-EWOULDBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 /*
450 * We do not count nwaiters, because ->release is supposed
451 * to be called when all openers are gone only.
452 */
453 schedule();
454 if (signal_pending(current)) {
455 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800456 return ERR_PTR(-EINTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 set_current_state(TASK_INTERRUPTIBLE);
459 }
460 set_current_state(TASK_RUNNING);
461 remove_wait_queue(&rp->wait, &waita);
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800462 return ep;
463}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800465static void mon_text_read_head_t(struct mon_reader_text *rp,
466 struct mon_text_ptr *p, const struct mon_event_text *ep)
467{
468 char udir, utype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
471 switch (usb_pipetype(ep->pipe)) {
472 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
473 case PIPE_INTERRUPT: utype = 'I'; break;
474 case PIPE_CONTROL: utype = 'C'; break;
475 default: /* PIPE_BULK */ utype = 'B';
476 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800477 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700478 "%lx %u %c %c%c:%03u:%02u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ep->id, ep->tstamp, ep->type,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800480 utype, udir,
481 usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
482}
483
484static void mon_text_read_head_u(struct mon_reader_text *rp,
485 struct mon_text_ptr *p, const struct mon_event_text *ep)
486{
487 char udir, utype;
488
489 udir = usb_pipein(ep->pipe) ? 'i' : 'o';
490 switch (usb_pipetype(ep->pipe)) {
491 case PIPE_ISOCHRONOUS: utype = 'Z'; break;
492 case PIPE_INTERRUPT: utype = 'I'; break;
493 case PIPE_CONTROL: utype = 'C'; break;
494 default: /* PIPE_BULK */ utype = 'B';
495 }
496 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
497 "%lx %u %c %c%c:%d:%03u:%u",
498 ep->id, ep->tstamp, ep->type,
499 utype, udir,
500 ep->busnum, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
501}
502
503static void mon_text_read_statset(struct mon_reader_text *rp,
504 struct mon_text_ptr *p, const struct mon_event_text *ep)
505{
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700506
507 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800508 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700509 " s %02x %02x %04x %04x %04x",
510 ep->setup[0],
511 ep->setup[1],
512 (ep->setup[3] << 8) | ep->setup[2],
513 (ep->setup[5] << 8) | ep->setup[4],
514 (ep->setup[7] << 8) | ep->setup[6]);
515 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800516 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700517 " %c __ __ ____ ____ ____", ep->setup_flag);
518 } else { /* No setup for this kind of URB */
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800519 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
520 " %d", ep->status);
Pete Zaitcevae0d6cc2005-06-25 14:32:59 -0700521 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800522}
523
524static void mon_text_read_intstat(struct mon_reader_text *rp,
525 struct mon_text_ptr *p, const struct mon_event_text *ep)
526{
527 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
528 " %d:%d", ep->status, ep->interval);
529}
530
531static void mon_text_read_isostat(struct mon_reader_text *rp,
532 struct mon_text_ptr *p, const struct mon_event_text *ep)
533{
534 if (ep->type == 'S') {
535 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
536 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
537 } else {
538 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
539 " %d:%d:%d:%d",
540 ep->status, ep->interval, ep->start_frame, ep->error_count);
541 }
542}
543
544static void mon_text_read_isodesc(struct mon_reader_text *rp,
545 struct mon_text_ptr *p, const struct mon_event_text *ep)
546{
547 int ndesc; /* Display this many */
548 int i;
549 const struct mon_iso_desc *dp;
550
551 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
552 " %d", ep->numdesc);
553 ndesc = ep->numdesc;
554 if (ndesc > ISODESC_MAX)
555 ndesc = ISODESC_MAX;
556 if (ndesc < 0)
557 ndesc = 0;
558 dp = ep->isodesc;
559 for (i = 0; i < ndesc; i++) {
560 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
561 " %d:%u:%u", dp->status, dp->offset, dp->length);
562 dp++;
563 }
564}
565
566static void mon_text_read_data(struct mon_reader_text *rp,
567 struct mon_text_ptr *p, const struct mon_event_text *ep)
568{
569 int data_len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if ((data_len = ep->length) > 0) {
572 if (ep->data_flag == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800573 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
574 " =");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (data_len >= DATA_MAX)
576 data_len = DATA_MAX;
577 for (i = 0; i < data_len; i++) {
578 if (i % 4 == 0) {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800579 p->cnt += snprintf(p->pbuf + p->cnt,
580 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 " ");
582 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800583 p->cnt += snprintf(p->pbuf + p->cnt,
584 p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 "%02x", ep->data[i]);
586 }
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800587 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
588 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800590 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 " %c\n", ep->data_flag);
592 }
593 } else {
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800594 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static int mon_text_release(struct inode *inode, struct file *file)
599{
600 struct mon_reader_text *rp = file->private_data;
601 struct mon_bus *mbus;
602 /* unsigned long flags; */
603 struct list_head *p;
604 struct mon_event_text *ep;
605
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100606 mutex_lock(&mon_lock);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700607 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (mbus->nreaders <= 0) {
610 printk(KERN_ERR TAG ": consistency error on close\n");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100611 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return 0;
613 }
614 mon_reader_del(mbus, &rp->r);
615
616 /*
617 * In theory, e_list is protected by mbus->lock. However,
618 * after mon_reader_del has finished, the following is the case:
619 * - we are not on reader list anymore, so new events won't be added;
620 * - whole mbus may be dropped if it was orphaned.
621 * So, we better not touch mbus.
622 */
623 /* spin_lock_irqsave(&mbus->lock, flags); */
624 while (!list_empty(&rp->e_list)) {
625 p = rp->e_list.next;
626 ep = list_entry(p, struct mon_event_text, e_link);
627 list_del(p);
628 --rp->nevents;
629 kmem_cache_free(rp->e_slab, ep);
630 }
631 /* spin_unlock_irqrestore(&mbus->lock, flags); */
632
633 kmem_cache_destroy(rp->e_slab);
634 kfree(rp->printf_buf);
635 kfree(rp);
636
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100637 mutex_unlock(&mon_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 0;
639}
640
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800641static const struct file_operations mon_fops_text_t = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 .owner = THIS_MODULE,
643 .open = mon_text_open,
644 .llseek = no_llseek,
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800645 .read = mon_text_read_t,
646 .release = mon_text_release,
647};
648
649static const struct file_operations mon_fops_text_u = {
650 .owner = THIS_MODULE,
651 .open = mon_text_open,
652 .llseek = no_llseek,
653 .read = mon_text_read_u,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 .release = mon_text_release,
655};
656
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800657int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
658{
659 struct dentry *d;
660 enum { NAMESZ = 10 };
661 char name[NAMESZ];
662 int rc;
663
664 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
665 if (rc <= 0 || rc >= NAMESZ)
666 goto err_print_t;
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800667 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800668 if (d == NULL)
669 goto err_create_t;
670 mbus->dent_t = d;
671
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800672 rc = snprintf(name, NAMESZ, "%du", ubus->busnum);
673 if (rc <= 0 || rc >= NAMESZ)
674 goto err_print_u;
675 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
676 if (d == NULL)
677 goto err_create_u;
678 mbus->dent_u = d;
679
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800680 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
681 if (rc <= 0 || rc >= NAMESZ)
682 goto err_print_s;
683 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
684 if (d == NULL)
685 goto err_create_s;
686 mbus->dent_s = d;
687
688 return 1;
689
690err_create_s:
691err_print_s:
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800692 debugfs_remove(mbus->dent_u);
693 mbus->dent_u = NULL;
694err_create_u:
695err_print_u:
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800696 debugfs_remove(mbus->dent_t);
697 mbus->dent_t = NULL;
698err_create_t:
699err_print_t:
700 return 0;
701}
702
703void mon_text_del(struct mon_bus *mbus)
704{
Pete Zaitcevf1c9e302007-02-24 19:27:33 -0800705 debugfs_remove(mbus->dent_u);
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800706 debugfs_remove(mbus->dent_t);
707 debugfs_remove(mbus->dent_s);
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * Slab interface: constructor.
712 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800713static void mon_text_ctor(void *mem, struct kmem_cache *slab, unsigned long sflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 /*
716 * Nothing to initialize. No, really!
717 * So, we fill it with garbage to emulate a reused object.
718 */
719 memset(mem, 0xe5, sizeof(struct mon_event_text));
720}
721
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800722int __init mon_text_init(void)
723{
724 struct dentry *mondir;
725
726 mondir = debugfs_create_dir("usbmon", NULL);
727 if (IS_ERR(mondir)) {
728 printk(KERN_NOTICE TAG ": debugfs is not available\n");
729 return -ENODEV;
730 }
731 if (mondir == NULL) {
732 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
733 return -ENODEV;
734 }
735 mon_dir = mondir;
736 return 0;
737}
738
Pete Zaitcev21641e32007-02-20 10:37:52 -0800739void mon_text_exit(void)
Pete Zaitcev6f23ee12006-12-30 22:43:10 -0800740{
741 debugfs_remove(mon_dir);
742}