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 int pipe; /* Pipe */
54 unsigned long id; /* From pointer, most of the time */
57 int length; /* Depends on type: xfer length or act length */
64 int numdesc; /* Full number */
65 struct mon_iso_desc isodesc[ISODESC_MAX];
66 unsigned char setup[SETUP_MAX];
67 unsigned char data[DATA_MAX];
70 #define SLAB_NAME_SZ 30
71 struct mon_reader_text {
72 struct kmem_cache *e_slab;
74 struct list_head e_list;
75 struct mon_reader r; /* In C, parent class can be placed anywhere */
77 wait_queue_head_t wait;
80 struct mutex printf_lock;
82 char slab_name[SLAB_NAME_SZ];
85 static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
87 static void mon_text_ctor(void *, struct kmem_cache *, unsigned long);
94 static struct mon_event_text *
95 mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
96 static void mon_text_read_head_t(struct mon_reader_text *rp,
97 struct mon_text_ptr *p, const struct mon_event_text *ep);
98 static void mon_text_read_head_u(struct mon_reader_text *rp,
99 struct mon_text_ptr *p, const struct mon_event_text *ep);
100 static void mon_text_read_statset(struct mon_reader_text *rp,
101 struct mon_text_ptr *p, const struct mon_event_text *ep);
102 static void mon_text_read_intstat(struct mon_reader_text *rp,
103 struct mon_text_ptr *p, const struct mon_event_text *ep);
104 static void mon_text_read_isostat(struct mon_reader_text *rp,
105 struct mon_text_ptr *p, const struct mon_event_text *ep);
106 static void mon_text_read_isodesc(struct mon_reader_text *rp,
107 struct mon_text_ptr *p, const struct mon_event_text *ep);
108 static void mon_text_read_data(struct mon_reader_text *rp,
109 struct mon_text_ptr *p, const struct mon_event_text *ep);
115 * May be called from an interrupt.
117 * This is called with the whole mon_bus locked, so no additional lock.
120 static inline char mon_text_get_setup(struct mon_event_text *ep,
121 struct urb *urb, char ev_type, struct mon_bus *mbus)
124 if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
127 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
128 return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
129 if (urb->setup_packet == NULL)
130 return 'Z'; /* '0' would be not as pretty. */
132 memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
136 static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
137 int len, char ev_type, struct mon_bus *mbus)
139 int pipe = urb->pipe;
146 if (usb_pipein(pipe)) {
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
159 * We do not even try to look at transfer_buffer, because it can
160 * contain non-NULL garbage in case the upper level promised to
161 * set DMA for the HCD.
163 if (mbus->uses_dma && (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
164 return mon_dmapeek(ep->data, urb->transfer_dma, len);
166 if (urb->transfer_buffer == NULL)
167 return 'Z'; /* '0' would be not as pretty. */
169 memcpy(ep->data, urb->transfer_buffer, len);
173 static inline unsigned int mon_get_timestamp(void)
178 do_gettimeofday(&tval);
179 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
180 stamp = stamp * 1000000 + tval.tv_usec;
184 static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
187 struct mon_event_text *ep;
189 struct usb_iso_packet_descriptor *fp;
190 struct mon_iso_desc *dp;
193 stamp = mon_get_timestamp();
195 if (rp->nevents >= EVENT_MAX ||
196 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
197 rp->r.m_bus->cnt_text_lost++;
202 ep->pipe = urb->pipe;
203 ep->id = (unsigned long) urb;
204 ep->busnum = rp->r.m_bus->u_bus->busnum;
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;
211 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;
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)
222 fp = urb->iso_frame_desc;
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;
234 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,
239 list_add_tail(&ep->e_link, &rp->e_list);
243 static void mon_text_submit(void *data, struct urb *urb)
245 struct mon_reader_text *rp = data;
246 mon_text_event(rp, urb, 'S');
249 static void mon_text_complete(void *data, struct urb *urb)
251 struct mon_reader_text *rp = data;
252 mon_text_event(rp, urb, 'C');
255 static void mon_text_error(void *data, struct urb *urb, int error)
257 struct mon_reader_text *rp = data;
258 struct mon_event_text *ep;
260 if (rp->nevents >= EVENT_MAX ||
261 (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
262 rp->r.m_bus->cnt_text_lost++;
267 ep->pipe = urb->pipe;
268 ep->id = (unsigned long) urb;
274 ep->setup_flag = '-';
278 list_add_tail(&ep->e_link, &rp->e_list);
283 * Fetch next event from the circular buffer.
285 static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
286 struct mon_bus *mbus)
291 spin_lock_irqsave(&mbus->lock, flags);
292 if (list_empty(&rp->e_list)) {
293 spin_unlock_irqrestore(&mbus->lock, flags);
299 spin_unlock_irqrestore(&mbus->lock, flags);
300 return list_entry(p, struct mon_event_text, e_link);
305 static int mon_text_open(struct inode *inode, struct file *file)
307 struct mon_bus *mbus;
308 struct usb_bus *ubus;
309 struct mon_reader_text *rp;
312 mutex_lock(&mon_lock);
313 mbus = inode->i_private;
316 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
321 INIT_LIST_HEAD(&rp->e_list);
322 init_waitqueue_head(&rp->wait);
323 mutex_init(&rp->printf_lock);
325 rp->printf_size = PRINTF_DFL;
326 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
327 if (rp->printf_buf == NULL) {
334 rp->r.rnf_submit = mon_text_submit;
335 rp->r.rnf_error = mon_text_error;
336 rp->r.rnf_complete = mon_text_complete;
338 snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
340 rp->e_slab = kmem_cache_create(rp->slab_name,
341 sizeof(struct mon_event_text), sizeof(long), 0,
342 mon_text_ctor, NULL);
343 if (rp->e_slab == NULL) {
348 mon_reader_add(mbus, &rp->r);
350 file->private_data = rp;
351 mutex_unlock(&mon_lock);
355 // kmem_cache_destroy(rp->e_slab);
357 kfree(rp->printf_buf);
361 mutex_unlock(&mon_lock);
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.
371 static ssize_t mon_text_read_t(struct file *file, char __user *buf,
372 size_t nbytes, loff_t *ppos)
374 struct mon_reader_text *rp = file->private_data;
375 struct mon_event_text *ep;
376 struct mon_text_ptr ptr;
378 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
380 mutex_lock(&rp->printf_lock);
382 ptr.pbuf = rp->printf_buf;
383 ptr.limit = rp->printf_size;
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,
389 mon_text_read_data(rp, &ptr, ep);
391 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
393 mutex_unlock(&rp->printf_lock);
394 kmem_cache_free(rp->e_slab, ep);
398 static ssize_t mon_text_read_u(struct file *file, char __user *buf,
399 size_t nbytes, loff_t *ppos)
401 struct mon_reader_text *rp = file->private_data;
402 struct mon_event_text *ep;
403 struct mon_text_ptr ptr;
405 if (IS_ERR(ep = mon_text_read_wait(rp, file)))
407 mutex_lock(&rp->printf_lock);
409 ptr.pbuf = rp->printf_buf;
410 ptr.limit = rp->printf_size;
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);
421 mon_text_read_statset(rp, &ptr, ep);
423 ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
425 mon_text_read_data(rp, &ptr, ep);
427 if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
429 mutex_unlock(&rp->printf_lock);
430 kmem_cache_free(rp->e_slab, ep);
434 static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
437 struct mon_bus *mbus = rp->r.m_bus;
438 DECLARE_WAITQUEUE(waita, current);
439 struct mon_event_text *ep;
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);
447 return ERR_PTR(-EWOULDBLOCK);
450 * We do not count nwaiters, because ->release is supposed
451 * to be called when all openers are gone only.
454 if (signal_pending(current)) {
455 remove_wait_queue(&rp->wait, &waita);
456 return ERR_PTR(-EINTR);
458 set_current_state(TASK_INTERRUPTIBLE);
460 set_current_state(TASK_RUNNING);
461 remove_wait_queue(&rp->wait, &waita);
465 static void mon_text_read_head_t(struct mon_reader_text *rp,
466 struct mon_text_ptr *p, const struct mon_event_text *ep)
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';
477 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
478 "%lx %u %c %c%c:%03u:%02u",
479 ep->id, ep->tstamp, ep->type,
481 usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
484 static void mon_text_read_head_u(struct mon_reader_text *rp,
485 struct mon_text_ptr *p, const struct mon_event_text *ep)
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';
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,
500 ep->busnum, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
503 static void mon_text_read_statset(struct mon_reader_text *rp,
504 struct mon_text_ptr *p, const struct mon_event_text *ep)
507 if (ep->setup_flag == 0) { /* Setup packet is present and captured */
508 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
509 " s %02x %02x %04x %04x %04x",
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 */
516 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
517 " %c __ __ ____ ____ ____", ep->setup_flag);
518 } else { /* No setup for this kind of URB */
519 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
524 static void mon_text_read_intstat(struct mon_reader_text *rp,
525 struct mon_text_ptr *p, const struct mon_event_text *ep)
527 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
528 " %d:%d", ep->status, ep->interval);
531 static void mon_text_read_isostat(struct mon_reader_text *rp,
532 struct mon_text_ptr *p, const struct mon_event_text *ep)
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);
538 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
540 ep->status, ep->interval, ep->start_frame, ep->error_count);
544 static void mon_text_read_isodesc(struct mon_reader_text *rp,
545 struct mon_text_ptr *p, const struct mon_event_text *ep)
547 int ndesc; /* Display this many */
549 const struct mon_iso_desc *dp;
551 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
554 if (ndesc > ISODESC_MAX)
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);
566 static void mon_text_read_data(struct mon_reader_text *rp,
567 struct mon_text_ptr *p, const struct mon_event_text *ep)
571 if ((data_len = ep->length) > 0) {
572 if (ep->data_flag == 0) {
573 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
575 if (data_len >= DATA_MAX)
577 for (i = 0; i < data_len; i++) {
579 p->cnt += snprintf(p->pbuf + p->cnt,
583 p->cnt += snprintf(p->pbuf + p->cnt,
585 "%02x", ep->data[i]);
587 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
590 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
591 " %c\n", ep->data_flag);
594 p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
598 static int mon_text_release(struct inode *inode, struct file *file)
600 struct mon_reader_text *rp = file->private_data;
601 struct mon_bus *mbus;
602 /* unsigned long flags; */
604 struct mon_event_text *ep;
606 mutex_lock(&mon_lock);
607 mbus = inode->i_private;
609 if (mbus->nreaders <= 0) {
610 printk(KERN_ERR TAG ": consistency error on close\n");
611 mutex_unlock(&mon_lock);
614 mon_reader_del(mbus, &rp->r);
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.
623 /* spin_lock_irqsave(&mbus->lock, flags); */
624 while (!list_empty(&rp->e_list)) {
626 ep = list_entry(p, struct mon_event_text, e_link);
629 kmem_cache_free(rp->e_slab, ep);
631 /* spin_unlock_irqrestore(&mbus->lock, flags); */
633 kmem_cache_destroy(rp->e_slab);
634 kfree(rp->printf_buf);
637 mutex_unlock(&mon_lock);
641 static const struct file_operations mon_fops_text_t = {
642 .owner = THIS_MODULE,
643 .open = mon_text_open,
645 .read = mon_text_read_t,
646 .release = mon_text_release,
649 static const struct file_operations mon_fops_text_u = {
650 .owner = THIS_MODULE,
651 .open = mon_text_open,
653 .read = mon_text_read_u,
654 .release = mon_text_release,
657 int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
660 enum { NAMESZ = 10 };
664 rc = snprintf(name, NAMESZ, "%dt", ubus->busnum);
665 if (rc <= 0 || rc >= NAMESZ)
667 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_t);
672 rc = snprintf(name, NAMESZ, "%du", ubus->busnum);
673 if (rc <= 0 || rc >= NAMESZ)
675 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
680 rc = snprintf(name, NAMESZ, "%ds", ubus->busnum);
681 if (rc <= 0 || rc >= NAMESZ)
683 d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
692 debugfs_remove(mbus->dent_u);
696 debugfs_remove(mbus->dent_t);
703 void mon_text_del(struct mon_bus *mbus)
705 debugfs_remove(mbus->dent_u);
706 debugfs_remove(mbus->dent_t);
707 debugfs_remove(mbus->dent_s);
711 * Slab interface: constructor.
713 static void mon_text_ctor(void *mem, struct kmem_cache *slab, unsigned long sflags)
716 * Nothing to initialize. No, really!
717 * So, we fill it with garbage to emulate a reused object.
719 memset(mem, 0xe5, sizeof(struct mon_event_text));
722 int __init mon_text_init(void)
724 struct dentry *mondir;
726 mondir = debugfs_create_dir("usbmon", NULL);
727 if (IS_ERR(mondir)) {
728 printk(KERN_NOTICE TAG ": debugfs is not available\n");
731 if (mondir == NULL) {
732 printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
739 void mon_text_exit(void)
741 debugfs_remove(mon_dir);