2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a text format reader.
7 #include <linux/kernel.h>
8 #include <linux/list.h>
10 #include <linux/time.h>
11 #include <linux/mutex.h>
12 #include <linux/debugfs.h>
13 #include <asm/uaccess.h>
18 * No, we do not want arbitrarily long data strings.
19 * Use the binary interface if you want to capture bulk data!
24 * Defined by USB 2.0 clause 9.3, table 9.2.
29 * This limit exists to prevent OOMs when the user process stops reading.
30 * 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.
34 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
37 * Potentially unlimited number; we limit it for similar allocations.
38 * The usbfs limits this to 128, but we're not quite as generous.
42 #define PRINTF_DFL 250 /* with 5 ISOs segs */
47 unsigned int length; /* Unsigned here, signed in URB. Historic. */
50 struct mon_event_text {
51 struct list_head e_link;
52 int type; /* submit, complete, etc. */
53 unsigned long id; /* From pointer, most of the time */
60 int length; /* Depends on type: xfer length or act length */
67 int numdesc; /* Full number */
68 struct mon_iso_desc isodesc[ISODESC_MAX];
69 unsigned char setup[SETUP_MAX];
70 unsigned char data[DATA_MAX];
73 #define SLAB_NAME_SZ 30
74 struct mon_reader_text {
75 struct kmem_cache *e_slab;
77 struct list_head e_list;
78 struct mon_reader r; /* In C, parent class can be placed anywhere */
80 wait_queue_head_t wait;
83 struct mutex printf_lock;
85 char slab_name[SLAB_NAME_SZ];
88 static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
90 static void mon_text_ctor(void *);
97 static struct mon_event_text *
98 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
99 static void mon_text_read_head_t(struct mon_reader_text *rp,
100 struct mon_text_ptr *p, const struct mon_event_text *ep);
101 static void mon_text_read_head_u(struct mon_reader_text *rp,
102 struct mon_text_ptr *p, const struct mon_event_text *ep);
103 static void mon_text_read_statset(struct mon_reader_text *rp,
104 struct mon_text_ptr *p, const struct mon_event_text *ep);
105 static void mon_text_read_intstat(struct mon_reader_text *rp,
106 struct mon_text_ptr *p, const struct mon_event_text *ep);
107 static void mon_text_read_isostat(struct mon_reader_text *rp,
108 struct mon_text_ptr *p, const struct mon_event_text *ep);
109 static void mon_text_read_isodesc(struct mon_reader_text *rp,
110 struct mon_text_ptr *p, const struct mon_event_text *ep);
111 static void mon_text_read_data(struct mon_reader_text *rp,
112 struct mon_text_ptr *p, const struct mon_event_text *ep);
118 * May be called from an interrupt.
120 * This is called with the whole mon_bus locked, so no additional lock.
123 static inline char mon_text_get_setup(struct mon_event_text *ep,
124 struct urb *urb, char ev_type, struct mon_bus *mbus)
127 if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
130 if (urb->setup_packet == NULL)
131 return 'Z'; /* '0' would be not as pretty. */
133 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
137 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
138 int len, char ev_type, struct mon_bus *mbus)
154 * The check to see if it's safe to poke at data has an enormous
155 * number of corner cases, but it seems that the following is
158 * We do not even try to look at transfer_buffer, because it can
159 * contain non-NULL garbage in case the upper level promised to
160 * set DMA for the HCD.
162 if (urb->dev->bus->uses_dma &&
163 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
164 return mon_dmapeek(ep->data, urb->transfer_dma, len);
167 if (urb->transfer_buffer == NULL)
168 return 'Z'; /* '0' would be not as pretty. */
170 memcpy(ep->data, urb->transfer_buffer, len);
174 static inline unsigned int mon_get_timestamp(void)
179 do_gettimeofday(&tval);
180 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
181 stamp = stamp * 1000000 + tval.tv_usec;
185 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
186 char ev_type, int status)
188 struct mon_event_text *ep;
190 struct usb_iso_packet_descriptor *fp;
191 struct mon_iso_desc *dp;
194 stamp = mon_get_timestamp();
196 if (rp->nevents >= EVENT_MAX ||
197 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
198 rp->r.m_bus->cnt_text_lost++;
203 ep->id = (unsigned long) urb;
204 ep->busnum = urb->dev->bus->busnum;
205 ep->devnum = urb->dev->devnum;
206 ep->epnum = usb_endpoint_num(&urb->ep->desc);
207 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
208 ep->is_in = usb_urb_dir_in(urb);
210 ep->length = (ev_type == 'S') ?
211 urb->transfer_buffer_length : urb->actual_length;
212 /* Collecting status makes debugging sense for submits, too */
215 if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
216 ep->interval = urb->interval;
217 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
218 ep->interval = urb->interval;
219 ep->start_frame = urb->start_frame;
220 ep->error_count = urb->error_count;
222 ep->numdesc = urb->number_of_packets;
223 if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
224 urb->number_of_packets > 0) {
225 if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
227 fp = urb->iso_frame_desc;
229 for (i = 0; i < ndesc; i++) {
230 dp->status = fp->status;
231 dp->offset = fp->offset;
232 dp->length = (ev_type == 'S') ?
233 fp->length : fp->actual_length;
239 ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
240 ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
244 list_add_tail(&ep->e_link, &rp->e_list);
248 static void mon_text_submit(void *data, struct urb *urb)
250 struct mon_reader_text *rp = data;
251 mon_text_event(rp, urb, 'S', -EINPROGRESS);
254 static void mon_text_complete(void *data, struct urb *urb, int status)
256 struct mon_reader_text *rp = data;
257 mon_text_event(rp, urb, 'C', status);
260 static void mon_text_error(void *data, struct urb *urb, int error)
262 struct mon_reader_text *rp = data;
263 struct mon_event_text *ep;
265 if (rp->nevents >= EVENT_MAX ||
266 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
267 rp->r.m_bus->cnt_text_lost++;
272 ep->id = (unsigned long) urb;
274 ep->devnum = urb->dev->devnum;
275 ep->epnum = usb_endpoint_num(&urb->ep->desc);
276 ep->xfertype = usb_endpoint_type(&urb->ep->desc);
277 ep->is_in = usb_urb_dir_in(urb);
282 ep->setup_flag = '-';
286 list_add_tail(&ep->e_link, &rp->e_list);
291 * Fetch next event from the circular buffer.
293 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
294 struct mon_bus *mbus)
299 spin_lock_irqsave(&mbus->lock, flags);
300 if (list_empty(&rp->e_list)) {
301 spin_unlock_irqrestore(&mbus->lock, flags);
307 spin_unlock_irqrestore(&mbus->lock, flags);
308 return list_entry(p, struct mon_event_text, e_link);
313 static int mon_text_open(struct inode *inode, struct file *file)
315 struct mon_bus *mbus;
316 struct mon_reader_text *rp;
319 mutex_lock(&mon_lock);
320 mbus = inode->i_private;
322 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
327 INIT_LIST_HEAD(&rp->e_list);
328 init_waitqueue_head(&rp->wait);
329 mutex_init(&rp->printf_lock);
331 rp->printf_size = PRINTF_DFL;
332 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
333 if (rp->printf_buf == NULL) {
340 rp->r.rnf_submit = mon_text_submit;
341 rp->r.rnf_error = mon_text_error;
342 rp->r.rnf_complete = mon_text_complete;
344 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
345 rp->e_slab = kmem_cache_create(rp->slab_name,
346 sizeof(struct mon_event_text), sizeof(long), 0,
348 if (rp->e_slab == NULL) {
353 mon_reader_add(mbus, &rp->r);
355 file->private_data = rp;
356 mutex_unlock(&mon_lock);
360 // kmem_cache_destroy(rp->e_slab);
362 kfree(rp->printf_buf);
366 mutex_unlock(&mon_lock);
371 * For simplicity, we read one record in one system call and throw out
372 * what does not fit. This means that the following does not work:
373 * dd if=/dbg/usbmon/0t bs=10
374 * Also, we do not allow seeks and do not bother advancing the offset.
376 static ssize_t mon_text_read_t(struct file *file, char __user *buf,
377 size_t nbytes, loff_t *ppos)
379 struct mon_reader_text *rp = file->private_data;
380 struct mon_event_text *ep;
381 struct mon_text_ptr ptr;
383 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
385 mutex_lock(&rp->printf_lock);
387 ptr.pbuf = rp->printf_buf;
388 ptr.limit = rp->printf_size;
390 mon_text_read_head_t(rp, &ptr, ep);
391 mon_text_read_statset(rp, &ptr, ep);
392 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
394 mon_text_read_data(rp, &ptr, ep);
396 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
398 mutex_unlock(&rp->printf_lock);
399 kmem_cache_free(rp->e_slab, ep);
403 static ssize_t mon_text_read_u(struct file *file, char __user *buf,
404 size_t nbytes, loff_t *ppos)
406 struct mon_reader_text *rp = file->private_data;
407 struct mon_event_text *ep;
408 struct mon_text_ptr ptr;
410 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
412 mutex_lock(&rp->printf_lock);
414 ptr.pbuf = rp->printf_buf;
415 ptr.limit = rp->printf_size;
417 mon_text_read_head_u(rp, &ptr, ep);
418 if (ep->type == 'E') {
419 mon_text_read_statset(rp, &ptr, ep);
420 } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
421 mon_text_read_isostat(rp, &ptr, ep);
422 mon_text_read_isodesc(rp, &ptr, ep);
423 } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
424 mon_text_read_intstat(rp, &ptr, ep);
426 mon_text_read_statset(rp, &ptr, ep);
428 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
430 mon_text_read_data(rp, &ptr, ep);
432 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
434 mutex_unlock(&rp->printf_lock);
435 kmem_cache_free(rp->e_slab, ep);
439 static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
442 struct mon_bus *mbus = rp->r.m_bus;
443 DECLARE_WAITQUEUE(waita, current);
444 struct mon_event_text *ep;
446 add_wait_queue(&rp->wait, &waita);
447 set_current_state(TASK_INTERRUPTIBLE);
448 while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
449 if (file->f_flags & O_NONBLOCK) {
450 set_current_state(TASK_RUNNING);
451 remove_wait_queue(&rp->wait, &waita);
452 return ERR_PTR(-EWOULDBLOCK);
455 * We do not count nwaiters, because ->release is supposed
456 * to be called when all openers are gone only.
459 if (signal_pending(current)) {
460 remove_wait_queue(&rp->wait, &waita);
461 return ERR_PTR(-EINTR);
463 set_current_state(TASK_INTERRUPTIBLE);
465 set_current_state(TASK_RUNNING);
466 remove_wait_queue(&rp->wait, &waita);
470 static void mon_text_read_head_t(struct mon_reader_text *rp,
471 struct mon_text_ptr *p, const struct mon_event_text *ep)
475 udir = (ep->is_in ? 'i' : 'o');
476 switch (ep->xfertype) {
477 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
478 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
479 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
480 default: /* PIPE_BULK */ utype = 'B';
482 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
483 "%lx %u %c %c%c:%03u:%02u",
484 ep->id, ep->tstamp, ep->type,
485 utype, udir, ep->devnum, ep->epnum);
488 static void mon_text_read_head_u(struct mon_reader_text *rp,
489 struct mon_text_ptr *p, const struct mon_event_text *ep)
493 udir = (ep->is_in ? 'i' : 'o');
494 switch (ep->xfertype) {
495 case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
496 case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
497 case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
498 default: /* PIPE_BULK */ utype = 'B';
500 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
501 "%lx %u %c %c%c:%d:%03u:%u",
502 ep->id, ep->tstamp, ep->type,
503 utype, udir, ep->busnum, ep->devnum, ep->epnum);
506 static void mon_text_read_statset(struct mon_reader_text *rp,
507 struct mon_text_ptr *p, const struct mon_event_text *ep)
510 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
511 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
512 " s %02x %02x %04x %04x %04x",
515 (ep->setup[3] << 8) | ep->setup[2],
516 (ep->setup[5] << 8) | ep->setup[4],
517 (ep->setup[7] << 8) | ep->setup[6]);
518 } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
519 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
520 " %c __ __ ____ ____ ____", ep->setup_flag);
521 } else { /* No setup for this kind of URB */
522 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
527 static void mon_text_read_intstat(struct mon_reader_text *rp,
528 struct mon_text_ptr *p, const struct mon_event_text *ep)
530 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
531 " %d:%d", ep->status, ep->interval);
534 static void mon_text_read_isostat(struct mon_reader_text *rp,
535 struct mon_text_ptr *p, const struct mon_event_text *ep)
537 if (ep->type == 'S') {
538 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
539 " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
541 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
543 ep->status, ep->interval, ep->start_frame, ep->error_count);
547 static void mon_text_read_isodesc(struct mon_reader_text *rp,
548 struct mon_text_ptr *p, const struct mon_event_text *ep)
550 int ndesc; /* Display this many */
552 const struct mon_iso_desc *dp;
554 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
557 if (ndesc > ISODESC_MAX)
562 for (i = 0; i < ndesc; i++) {
563 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
564 " %d:%u:%u", dp->status, dp->offset, dp->length);
569 static void mon_text_read_data(struct mon_reader_text *rp,
570 struct mon_text_ptr *p, const struct mon_event_text *ep)
574 if ((data_len = ep->length) > 0) {
575 if (ep->data_flag == 0) {
576 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
578 if (data_len >= DATA_MAX)
580 for (i = 0; i < data_len; i++) {
582 p->cnt += snprintf(p->pbuf + p->cnt,
586 p->cnt += snprintf(p->pbuf + p->cnt,
588 "%02x", ep->data[i]);
590 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
593 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
594 " %c\n", ep->data_flag);
597 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
601 static int mon_text_release(struct inode *inode, struct file *file)
603 struct mon_reader_text *rp = file->private_data;
604 struct mon_bus *mbus;
605 /* unsigned long flags; */
607 struct mon_event_text *ep;
609 mutex_lock(&mon_lock);
610 mbus = inode->i_private;
612 if (mbus->nreaders <= 0) {
613 printk(KERN_ERR TAG ": consistency error on close\n");
614 mutex_unlock(&mon_lock);
617 mon_reader_del(mbus, &rp->r);
620 * In theory, e_list is protected by mbus->lock. However,
621 * after mon_reader_del has finished, the following is the case:
622 * - we are not on reader list anymore, so new events won't be added;
623 * - whole mbus may be dropped if it was orphaned.
624 * So, we better not touch mbus.
626 /* spin_lock_irqsave(&mbus->lock, flags); */
627 while (!list_empty(&rp->e_list)) {
629 ep = list_entry(p, struct mon_event_text, e_link);
632 kmem_cache_free(rp->e_slab, ep);
634 /* spin_unlock_irqrestore(&mbus->lock, flags); */
636 kmem_cache_destroy(rp->e_slab);
637 kfree(rp->printf_buf);
640 mutex_unlock(&mon_lock);
644 static const struct file_operations mon_fops_text_t = {
645 .owner = THIS_MODULE,
646 .open = mon_text_open,
648 .read = mon_text_read_t,
649 .release = mon_text_release,
652 static const struct file_operations mon_fops_text_u = {
653 .owner = THIS_MODULE,
654 .open = mon_text_open,
656 .read = mon_text_read_u,
657 .release = mon_text_release,
660 int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
663 enum { NAMESZ = 10 };
665 int busnum = ubus? ubus->busnum: 0;
669 rc = snprintf(name, NAMESZ, "%dt", busnum);
670 if (rc <= 0 || rc >= NAMESZ)
672 d = debugfs_create_file(name, 0600, mon_dir, mbus,
679 rc = snprintf(name, NAMESZ, "%du", busnum);
680 if (rc <= 0 || rc >= NAMESZ)
682 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
687 rc = snprintf(name, NAMESZ, "%ds", busnum);
688 if (rc <= 0 || rc >= NAMESZ)
690 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
699 debugfs_remove(mbus->dent_u);
704 debugfs_remove(mbus->dent_t);
712 void mon_text_del(struct mon_bus *mbus)
714 debugfs_remove(mbus->dent_u);
715 if (mbus->dent_t != NULL)
716 debugfs_remove(mbus->dent_t);
717 debugfs_remove(mbus->dent_s);
721 * Slab interface: constructor.
723 static void mon_text_ctor(void *mem)
726 * Nothing to initialize. No, really!
727 * So, we fill it with garbage to emulate a reused object.
729 memset(mem, 0xe5, sizeof(struct mon_event_text));
732 int __init mon_text_init(void)
734 struct dentry *mondir;
736 mondir = debugfs_create_dir("usbmon", NULL);
737 if (IS_ERR(mondir)) {
738 printk(KERN_NOTICE TAG ": debugfs is not available\n");
741 if (mondir == NULL) {
742 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
749 void mon_text_exit(void)
751 debugfs_remove(mon_dir);