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 (urb
->dev
->bus
->uses_dma
&&
128 (urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)) {
129 return mon_dmapeek(ep
->setup
, urb
->setup_dma
, SETUP_MAX
);
131 if (urb
->setup_packet
== NULL
)
132 return 'Z'; /* '0' would be not as pretty. */
134 memcpy(ep
->setup
, urb
->setup_packet
, SETUP_MAX
);
138 static inline char mon_text_get_data(struct mon_event_text
*ep
, struct urb
*urb
,
139 int len
, char ev_type
, struct mon_bus
*mbus
)
141 int pipe
= urb
->pipe
;
148 if (usb_pipein(pipe
)) {
157 * The check to see if it's safe to poke at data has an enormous
158 * number of corner cases, but it seems that the following is
161 * We do not even try to look at transfer_buffer, because it can
162 * contain non-NULL garbage in case the upper level promised to
163 * set DMA for the HCD.
165 if (urb
->dev
->bus
->uses_dma
&&
166 (urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)) {
167 return mon_dmapeek(ep
->data
, urb
->transfer_dma
, len
);
170 if (urb
->transfer_buffer
== NULL
)
171 return 'Z'; /* '0' would be not as pretty. */
173 memcpy(ep
->data
, urb
->transfer_buffer
, len
);
177 static inline unsigned int mon_get_timestamp(void)
182 do_gettimeofday(&tval
);
183 stamp
= tval
.tv_sec
& 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
184 stamp
= stamp
* 1000000 + tval
.tv_usec
;
188 static void mon_text_event(struct mon_reader_text
*rp
, struct urb
*urb
,
191 struct mon_event_text
*ep
;
193 struct usb_iso_packet_descriptor
*fp
;
194 struct mon_iso_desc
*dp
;
197 stamp
= mon_get_timestamp();
199 if (rp
->nevents
>= EVENT_MAX
||
200 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
201 rp
->r
.m_bus
->cnt_text_lost
++;
206 ep
->pipe
= urb
->pipe
;
207 ep
->id
= (unsigned long) urb
;
208 ep
->busnum
= urb
->dev
->bus
->busnum
;
210 ep
->length
= (ev_type
== 'S') ?
211 urb
->transfer_buffer_length
: urb
->actual_length
;
212 /* Collecting status makes debugging sense for submits, too */
213 ep
->status
= urb
->status
;
215 if (usb_pipeint(urb
->pipe
)) {
216 ep
->interval
= urb
->interval
;
217 } else if (usb_pipeisoc(urb
->pipe
)) {
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 (usb_pipeisoc(urb
->pipe
) && urb
->number_of_packets
> 0) {
224 if ((ndesc
= urb
->number_of_packets
) > ISODESC_MAX
)
226 fp
= urb
->iso_frame_desc
;
228 for (i
= 0; i
< ndesc
; i
++) {
229 dp
->status
= fp
->status
;
230 dp
->offset
= fp
->offset
;
231 dp
->length
= (ev_type
== 'S') ?
232 fp
->length
: fp
->actual_length
;
238 ep
->setup_flag
= mon_text_get_setup(ep
, urb
, ev_type
, rp
->r
.m_bus
);
239 ep
->data_flag
= mon_text_get_data(ep
, urb
, ep
->length
, ev_type
,
243 list_add_tail(&ep
->e_link
, &rp
->e_list
);
247 static void mon_text_submit(void *data
, struct urb
*urb
)
249 struct mon_reader_text
*rp
= data
;
250 mon_text_event(rp
, urb
, 'S');
253 static void mon_text_complete(void *data
, struct urb
*urb
)
255 struct mon_reader_text
*rp
= data
;
256 mon_text_event(rp
, urb
, 'C');
259 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
261 struct mon_reader_text
*rp
= data
;
262 struct mon_event_text
*ep
;
264 if (rp
->nevents
>= EVENT_MAX
||
265 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
266 rp
->r
.m_bus
->cnt_text_lost
++;
271 ep
->pipe
= urb
->pipe
;
272 ep
->id
= (unsigned long) urb
;
278 ep
->setup_flag
= '-';
282 list_add_tail(&ep
->e_link
, &rp
->e_list
);
287 * Fetch next event from the circular buffer.
289 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
290 struct mon_bus
*mbus
)
295 spin_lock_irqsave(&mbus
->lock
, flags
);
296 if (list_empty(&rp
->e_list
)) {
297 spin_unlock_irqrestore(&mbus
->lock
, flags
);
303 spin_unlock_irqrestore(&mbus
->lock
, flags
);
304 return list_entry(p
, struct mon_event_text
, e_link
);
309 static int mon_text_open(struct inode
*inode
, struct file
*file
)
311 struct mon_bus
*mbus
;
312 struct mon_reader_text
*rp
;
315 mutex_lock(&mon_lock
);
316 mbus
= inode
->i_private
;
318 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
323 INIT_LIST_HEAD(&rp
->e_list
);
324 init_waitqueue_head(&rp
->wait
);
325 mutex_init(&rp
->printf_lock
);
327 rp
->printf_size
= PRINTF_DFL
;
328 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
329 if (rp
->printf_buf
== NULL
) {
336 rp
->r
.rnf_submit
= mon_text_submit
;
337 rp
->r
.rnf_error
= mon_text_error
;
338 rp
->r
.rnf_complete
= mon_text_complete
;
340 snprintf(rp
->slab_name
, SLAB_NAME_SZ
, "mon_text_%p", rp
);
341 rp
->e_slab
= kmem_cache_create(rp
->slab_name
,
342 sizeof(struct mon_event_text
), sizeof(long), 0,
343 mon_text_ctor
, NULL
);
344 if (rp
->e_slab
== NULL
) {
349 mon_reader_add(mbus
, &rp
->r
);
351 file
->private_data
= rp
;
352 mutex_unlock(&mon_lock
);
356 // kmem_cache_destroy(rp->e_slab);
358 kfree(rp
->printf_buf
);
362 mutex_unlock(&mon_lock
);
367 * For simplicity, we read one record in one system call and throw out
368 * what does not fit. This means that the following does not work:
369 * dd if=/dbg/usbmon/0t bs=10
370 * Also, we do not allow seeks and do not bother advancing the offset.
372 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
373 size_t nbytes
, loff_t
*ppos
)
375 struct mon_reader_text
*rp
= file
->private_data
;
376 struct mon_event_text
*ep
;
377 struct mon_text_ptr ptr
;
379 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
381 mutex_lock(&rp
->printf_lock
);
383 ptr
.pbuf
= rp
->printf_buf
;
384 ptr
.limit
= rp
->printf_size
;
386 mon_text_read_head_t(rp
, &ptr
, ep
);
387 mon_text_read_statset(rp
, &ptr
, ep
);
388 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
390 mon_text_read_data(rp
, &ptr
, ep
);
392 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
394 mutex_unlock(&rp
->printf_lock
);
395 kmem_cache_free(rp
->e_slab
, ep
);
399 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
400 size_t nbytes
, loff_t
*ppos
)
402 struct mon_reader_text
*rp
= file
->private_data
;
403 struct mon_event_text
*ep
;
404 struct mon_text_ptr ptr
;
406 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
408 mutex_lock(&rp
->printf_lock
);
410 ptr
.pbuf
= rp
->printf_buf
;
411 ptr
.limit
= rp
->printf_size
;
413 mon_text_read_head_u(rp
, &ptr
, ep
);
414 if (ep
->type
== 'E') {
415 mon_text_read_statset(rp
, &ptr
, ep
);
416 } else if (usb_pipeisoc(ep
->pipe
)) {
417 mon_text_read_isostat(rp
, &ptr
, ep
);
418 mon_text_read_isodesc(rp
, &ptr
, ep
);
419 } else if (usb_pipeint(ep
->pipe
)) {
420 mon_text_read_intstat(rp
, &ptr
, ep
);
422 mon_text_read_statset(rp
, &ptr
, ep
);
424 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
426 mon_text_read_data(rp
, &ptr
, ep
);
428 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
430 mutex_unlock(&rp
->printf_lock
);
431 kmem_cache_free(rp
->e_slab
, ep
);
435 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
438 struct mon_bus
*mbus
= rp
->r
.m_bus
;
439 DECLARE_WAITQUEUE(waita
, current
);
440 struct mon_event_text
*ep
;
442 add_wait_queue(&rp
->wait
, &waita
);
443 set_current_state(TASK_INTERRUPTIBLE
);
444 while ((ep
= mon_text_fetch(rp
, mbus
)) == NULL
) {
445 if (file
->f_flags
& O_NONBLOCK
) {
446 set_current_state(TASK_RUNNING
);
447 remove_wait_queue(&rp
->wait
, &waita
);
448 return ERR_PTR(-EWOULDBLOCK
);
451 * We do not count nwaiters, because ->release is supposed
452 * to be called when all openers are gone only.
455 if (signal_pending(current
)) {
456 remove_wait_queue(&rp
->wait
, &waita
);
457 return ERR_PTR(-EINTR
);
459 set_current_state(TASK_INTERRUPTIBLE
);
461 set_current_state(TASK_RUNNING
);
462 remove_wait_queue(&rp
->wait
, &waita
);
466 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
467 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
471 udir
= usb_pipein(ep
->pipe
) ? 'i' : 'o';
472 switch (usb_pipetype(ep
->pipe
)) {
473 case PIPE_ISOCHRONOUS
: utype
= 'Z'; break;
474 case PIPE_INTERRUPT
: utype
= 'I'; break;
475 case PIPE_CONTROL
: utype
= 'C'; break;
476 default: /* PIPE_BULK */ utype
= 'B';
478 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
479 "%lx %u %c %c%c:%03u:%02u",
480 ep
->id
, ep
->tstamp
, ep
->type
,
482 usb_pipedevice(ep
->pipe
), usb_pipeendpoint(ep
->pipe
));
485 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
486 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
490 udir
= usb_pipein(ep
->pipe
) ? 'i' : 'o';
491 switch (usb_pipetype(ep
->pipe
)) {
492 case PIPE_ISOCHRONOUS
: utype
= 'Z'; break;
493 case PIPE_INTERRUPT
: utype
= 'I'; break;
494 case PIPE_CONTROL
: utype
= 'C'; break;
495 default: /* PIPE_BULK */ utype
= 'B';
497 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
498 "%lx %u %c %c%c:%d:%03u:%u",
499 ep
->id
, ep
->tstamp
, ep
->type
,
501 ep
->busnum
, usb_pipedevice(ep
->pipe
), usb_pipeendpoint(ep
->pipe
));
504 static void mon_text_read_statset(struct mon_reader_text
*rp
,
505 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
508 if (ep
->setup_flag
== 0) { /* Setup packet is present and captured */
509 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
510 " s %02x %02x %04x %04x %04x",
513 (ep
->setup
[3] << 8) | ep
->setup
[2],
514 (ep
->setup
[5] << 8) | ep
->setup
[4],
515 (ep
->setup
[7] << 8) | ep
->setup
[6]);
516 } else if (ep
->setup_flag
!= '-') { /* Unable to capture setup packet */
517 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
518 " %c __ __ ____ ____ ____", ep
->setup_flag
);
519 } else { /* No setup for this kind of URB */
520 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
525 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
526 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
528 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
529 " %d:%d", ep
->status
, ep
->interval
);
532 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
533 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
535 if (ep
->type
== 'S') {
536 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
537 " %d:%d:%d", ep
->status
, ep
->interval
, ep
->start_frame
);
539 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
541 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
545 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
546 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
548 int ndesc
; /* Display this many */
550 const struct mon_iso_desc
*dp
;
552 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
555 if (ndesc
> ISODESC_MAX
)
560 for (i
= 0; i
< ndesc
; i
++) {
561 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
562 " %d:%u:%u", dp
->status
, dp
->offset
, dp
->length
);
567 static void mon_text_read_data(struct mon_reader_text
*rp
,
568 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
572 if ((data_len
= ep
->length
) > 0) {
573 if (ep
->data_flag
== 0) {
574 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
576 if (data_len
>= DATA_MAX
)
578 for (i
= 0; i
< data_len
; i
++) {
580 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
584 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
586 "%02x", ep
->data
[i
]);
588 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
591 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
592 " %c\n", ep
->data_flag
);
595 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
599 static int mon_text_release(struct inode
*inode
, struct file
*file
)
601 struct mon_reader_text
*rp
= file
->private_data
;
602 struct mon_bus
*mbus
;
603 /* unsigned long flags; */
605 struct mon_event_text
*ep
;
607 mutex_lock(&mon_lock
);
608 mbus
= inode
->i_private
;
610 if (mbus
->nreaders
<= 0) {
611 printk(KERN_ERR TAG
": consistency error on close\n");
612 mutex_unlock(&mon_lock
);
615 mon_reader_del(mbus
, &rp
->r
);
618 * In theory, e_list is protected by mbus->lock. However,
619 * after mon_reader_del has finished, the following is the case:
620 * - we are not on reader list anymore, so new events won't be added;
621 * - whole mbus may be dropped if it was orphaned.
622 * So, we better not touch mbus.
624 /* spin_lock_irqsave(&mbus->lock, flags); */
625 while (!list_empty(&rp
->e_list
)) {
627 ep
= list_entry(p
, struct mon_event_text
, e_link
);
630 kmem_cache_free(rp
->e_slab
, ep
);
632 /* spin_unlock_irqrestore(&mbus->lock, flags); */
634 kmem_cache_destroy(rp
->e_slab
);
635 kfree(rp
->printf_buf
);
638 mutex_unlock(&mon_lock
);
642 static const struct file_operations mon_fops_text_t
= {
643 .owner
= THIS_MODULE
,
644 .open
= mon_text_open
,
646 .read
= mon_text_read_t
,
647 .release
= mon_text_release
,
650 static const struct file_operations mon_fops_text_u
= {
651 .owner
= THIS_MODULE
,
652 .open
= mon_text_open
,
654 .read
= mon_text_read_u
,
655 .release
= mon_text_release
,
658 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
661 enum { NAMESZ
= 10 };
663 int busnum
= ubus
? ubus
->busnum
: 0;
667 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
668 if (rc
<= 0 || rc
>= NAMESZ
)
670 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
677 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
678 if (rc
<= 0 || rc
>= NAMESZ
)
680 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
685 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
686 if (rc
<= 0 || rc
>= NAMESZ
)
688 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
697 debugfs_remove(mbus
->dent_u
);
702 debugfs_remove(mbus
->dent_t
);
710 void mon_text_del(struct mon_bus
*mbus
)
712 debugfs_remove(mbus
->dent_u
);
713 if (mbus
->dent_t
!= NULL
)
714 debugfs_remove(mbus
->dent_t
);
715 debugfs_remove(mbus
->dent_s
);
719 * Slab interface: constructor.
721 static void mon_text_ctor(void *mem
, struct kmem_cache
*slab
, unsigned long sflags
)
724 * Nothing to initialize. No, really!
725 * So, we fill it with garbage to emulate a reused object.
727 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
730 int __init
mon_text_init(void)
732 struct dentry
*mondir
;
734 mondir
= debugfs_create_dir("usbmon", NULL
);
735 if (IS_ERR(mondir
)) {
736 printk(KERN_NOTICE TAG
": debugfs is not available\n");
739 if (mondir
== NULL
) {
740 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
747 void mon_text_exit(void)
749 debugfs_remove(mon_dir
);