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/slab.h>
11 #include <linux/time.h>
12 #include <linux/export.h>
13 #include <linux/mutex.h>
14 #include <linux/debugfs.h>
15 #include <linux/scatterlist.h>
16 #include <asm/uaccess.h>
21 * No, we do not want arbitrarily long data strings.
22 * Use the binary interface if you want to capture bulk data!
27 * Defined by USB 2.0 clause 9.3, table 9.2.
32 * This limit exists to prevent OOMs when the user process stops reading.
33 * If usbmon were available to unprivileged processes, it might be open
34 * to a local DoS. But we have to keep to root in order to prevent
35 * password sniffing from HID devices.
37 #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
40 * Potentially unlimited number; we limit it for similar allocations.
41 * The usbfs limits this to 128, but we're not quite as generous.
45 #define PRINTF_DFL 250 /* with 5 ISOs segs */
50 unsigned int length
; /* Unsigned here, signed in URB. Historic. */
53 struct mon_event_text
{
54 struct list_head e_link
;
55 int type
; /* submit, complete, etc. */
56 unsigned long id
; /* From pointer, most of the time */
63 int length
; /* Depends on type: xfer length or act length */
70 int numdesc
; /* Full number */
71 struct mon_iso_desc isodesc
[ISODESC_MAX
];
72 unsigned char setup
[SETUP_MAX
];
73 unsigned char data
[DATA_MAX
];
76 #define SLAB_NAME_SZ 30
77 struct mon_reader_text
{
78 struct kmem_cache
*e_slab
;
80 struct list_head e_list
;
81 struct mon_reader r
; /* In C, parent class can be placed anywhere */
83 wait_queue_head_t wait
;
86 struct mutex printf_lock
;
88 char slab_name
[SLAB_NAME_SZ
];
91 static struct dentry
*mon_dir
; /* Usually /sys/kernel/debug/usbmon */
93 static void mon_text_ctor(void *);
100 static struct mon_event_text
*
101 mon_text_read_wait(struct mon_reader_text
*rp
, struct file
*file
);
102 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
103 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
104 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
105 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
106 static void mon_text_read_statset(struct mon_reader_text
*rp
,
107 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
108 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
109 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
110 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
111 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
112 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
113 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
114 static void mon_text_read_data(struct mon_reader_text
*rp
,
115 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
);
121 * May be called from an interrupt.
123 * This is called with the whole mon_bus locked, so no additional lock.
126 static inline char mon_text_get_setup(struct mon_event_text
*ep
,
127 struct urb
*urb
, char ev_type
, struct mon_bus
*mbus
)
130 if (ep
->xfertype
!= USB_ENDPOINT_XFER_CONTROL
|| ev_type
!= 'S')
133 if (urb
->setup_packet
== NULL
)
134 return 'Z'; /* '0' would be not as pretty. */
136 memcpy(ep
->setup
, urb
->setup_packet
, SETUP_MAX
);
140 static inline char mon_text_get_data(struct mon_event_text
*ep
, struct urb
*urb
,
141 int len
, char ev_type
, struct mon_bus
*mbus
)
158 if (urb
->num_sgs
== 0) {
159 src
= urb
->transfer_buffer
;
161 return 'Z'; /* '0' would be not as pretty. */
163 struct scatterlist
*sg
= urb
->sg
;
165 if (PageHighMem(sg_page(sg
)))
168 /* For the text interface we copy only the first sg buffer */
169 len
= min_t(int, sg
->length
, len
);
173 memcpy(ep
->data
, src
, len
);
177 static inline unsigned int mon_get_timestamp(void)
182 do_gettimeofday(&tval
);
183 stamp
= tval
.tv_sec
& 0xFFF; /* 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
,
189 char ev_type
, int status
)
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
->id
= (unsigned long) urb
;
207 ep
->busnum
= urb
->dev
->bus
->busnum
;
208 ep
->devnum
= urb
->dev
->devnum
;
209 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
210 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
211 ep
->is_in
= usb_urb_dir_in(urb
);
213 ep
->length
= (ev_type
== 'S') ?
214 urb
->transfer_buffer_length
: urb
->actual_length
;
215 /* Collecting status makes debugging sense for submits, too */
218 if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
219 ep
->interval
= urb
->interval
;
220 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
221 ep
->interval
= urb
->interval
;
222 ep
->start_frame
= urb
->start_frame
;
223 ep
->error_count
= urb
->error_count
;
225 ep
->numdesc
= urb
->number_of_packets
;
226 if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
&&
227 urb
->number_of_packets
> 0) {
228 if ((ndesc
= urb
->number_of_packets
) > ISODESC_MAX
)
230 fp
= urb
->iso_frame_desc
;
232 for (i
= 0; i
< ndesc
; i
++) {
233 dp
->status
= fp
->status
;
234 dp
->offset
= fp
->offset
;
235 dp
->length
= (ev_type
== 'S') ?
236 fp
->length
: fp
->actual_length
;
240 /* Wasteful, but simple to understand: ISO 'C' is sparse. */
242 ep
->length
= urb
->transfer_buffer_length
;
245 ep
->setup_flag
= mon_text_get_setup(ep
, urb
, ev_type
, rp
->r
.m_bus
);
246 ep
->data_flag
= mon_text_get_data(ep
, urb
, ep
->length
, ev_type
,
250 list_add_tail(&ep
->e_link
, &rp
->e_list
);
254 static void mon_text_submit(void *data
, struct urb
*urb
)
256 struct mon_reader_text
*rp
= data
;
257 mon_text_event(rp
, urb
, 'S', -EINPROGRESS
);
260 static void mon_text_complete(void *data
, struct urb
*urb
, int status
)
262 struct mon_reader_text
*rp
= data
;
263 mon_text_event(rp
, urb
, 'C', status
);
266 static void mon_text_error(void *data
, struct urb
*urb
, int error
)
268 struct mon_reader_text
*rp
= data
;
269 struct mon_event_text
*ep
;
271 if (rp
->nevents
>= EVENT_MAX
||
272 (ep
= kmem_cache_alloc(rp
->e_slab
, GFP_ATOMIC
)) == NULL
) {
273 rp
->r
.m_bus
->cnt_text_lost
++;
278 ep
->id
= (unsigned long) urb
;
279 ep
->busnum
= urb
->dev
->bus
->busnum
;
280 ep
->devnum
= urb
->dev
->devnum
;
281 ep
->epnum
= usb_endpoint_num(&urb
->ep
->desc
);
282 ep
->xfertype
= usb_endpoint_type(&urb
->ep
->desc
);
283 ep
->is_in
= usb_urb_dir_in(urb
);
284 ep
->tstamp
= mon_get_timestamp();
288 ep
->setup_flag
= '-';
292 list_add_tail(&ep
->e_link
, &rp
->e_list
);
297 * Fetch next event from the circular buffer.
299 static struct mon_event_text
*mon_text_fetch(struct mon_reader_text
*rp
,
300 struct mon_bus
*mbus
)
305 spin_lock_irqsave(&mbus
->lock
, flags
);
306 if (list_empty(&rp
->e_list
)) {
307 spin_unlock_irqrestore(&mbus
->lock
, flags
);
313 spin_unlock_irqrestore(&mbus
->lock
, flags
);
314 return list_entry(p
, struct mon_event_text
, e_link
);
319 static int mon_text_open(struct inode
*inode
, struct file
*file
)
321 struct mon_bus
*mbus
;
322 struct mon_reader_text
*rp
;
325 mutex_lock(&mon_lock
);
326 mbus
= inode
->i_private
;
328 rp
= kzalloc(sizeof(struct mon_reader_text
), GFP_KERNEL
);
333 INIT_LIST_HEAD(&rp
->e_list
);
334 init_waitqueue_head(&rp
->wait
);
335 mutex_init(&rp
->printf_lock
);
337 rp
->printf_size
= PRINTF_DFL
;
338 rp
->printf_buf
= kmalloc(rp
->printf_size
, GFP_KERNEL
);
339 if (rp
->printf_buf
== NULL
) {
346 rp
->r
.rnf_submit
= mon_text_submit
;
347 rp
->r
.rnf_error
= mon_text_error
;
348 rp
->r
.rnf_complete
= mon_text_complete
;
350 snprintf(rp
->slab_name
, SLAB_NAME_SZ
, "mon_text_%p", rp
);
351 rp
->e_slab
= kmem_cache_create(rp
->slab_name
,
352 sizeof(struct mon_event_text
), sizeof(long), 0,
354 if (rp
->e_slab
== NULL
) {
359 mon_reader_add(mbus
, &rp
->r
);
361 file
->private_data
= rp
;
362 mutex_unlock(&mon_lock
);
366 // kmem_cache_destroy(rp->e_slab);
368 kfree(rp
->printf_buf
);
372 mutex_unlock(&mon_lock
);
377 * For simplicity, we read one record in one system call and throw out
378 * what does not fit. This means that the following does not work:
379 * dd if=/dbg/usbmon/0t bs=10
380 * Also, we do not allow seeks and do not bother advancing the offset.
382 static ssize_t
mon_text_read_t(struct file
*file
, char __user
*buf
,
383 size_t nbytes
, loff_t
*ppos
)
385 struct mon_reader_text
*rp
= file
->private_data
;
386 struct mon_event_text
*ep
;
387 struct mon_text_ptr ptr
;
389 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
391 mutex_lock(&rp
->printf_lock
);
393 ptr
.pbuf
= rp
->printf_buf
;
394 ptr
.limit
= rp
->printf_size
;
396 mon_text_read_head_t(rp
, &ptr
, ep
);
397 mon_text_read_statset(rp
, &ptr
, ep
);
398 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
400 mon_text_read_data(rp
, &ptr
, ep
);
402 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
404 mutex_unlock(&rp
->printf_lock
);
405 kmem_cache_free(rp
->e_slab
, ep
);
409 static ssize_t
mon_text_read_u(struct file
*file
, char __user
*buf
,
410 size_t nbytes
, loff_t
*ppos
)
412 struct mon_reader_text
*rp
= file
->private_data
;
413 struct mon_event_text
*ep
;
414 struct mon_text_ptr ptr
;
416 if (IS_ERR(ep
= mon_text_read_wait(rp
, file
)))
418 mutex_lock(&rp
->printf_lock
);
420 ptr
.pbuf
= rp
->printf_buf
;
421 ptr
.limit
= rp
->printf_size
;
423 mon_text_read_head_u(rp
, &ptr
, ep
);
424 if (ep
->type
== 'E') {
425 mon_text_read_statset(rp
, &ptr
, ep
);
426 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_ISOC
) {
427 mon_text_read_isostat(rp
, &ptr
, ep
);
428 mon_text_read_isodesc(rp
, &ptr
, ep
);
429 } else if (ep
->xfertype
== USB_ENDPOINT_XFER_INT
) {
430 mon_text_read_intstat(rp
, &ptr
, ep
);
432 mon_text_read_statset(rp
, &ptr
, ep
);
434 ptr
.cnt
+= snprintf(ptr
.pbuf
+ ptr
.cnt
, ptr
.limit
- ptr
.cnt
,
436 mon_text_read_data(rp
, &ptr
, ep
);
438 if (copy_to_user(buf
, rp
->printf_buf
, ptr
.cnt
))
440 mutex_unlock(&rp
->printf_lock
);
441 kmem_cache_free(rp
->e_slab
, ep
);
445 static struct mon_event_text
*mon_text_read_wait(struct mon_reader_text
*rp
,
448 struct mon_bus
*mbus
= rp
->r
.m_bus
;
449 DECLARE_WAITQUEUE(waita
, current
);
450 struct mon_event_text
*ep
;
452 add_wait_queue(&rp
->wait
, &waita
);
453 set_current_state(TASK_INTERRUPTIBLE
);
454 while ((ep
= mon_text_fetch(rp
, mbus
)) == NULL
) {
455 if (file
->f_flags
& O_NONBLOCK
) {
456 set_current_state(TASK_RUNNING
);
457 remove_wait_queue(&rp
->wait
, &waita
);
458 return ERR_PTR(-EWOULDBLOCK
);
461 * We do not count nwaiters, because ->release is supposed
462 * to be called when all openers are gone only.
465 if (signal_pending(current
)) {
466 remove_wait_queue(&rp
->wait
, &waita
);
467 return ERR_PTR(-EINTR
);
469 set_current_state(TASK_INTERRUPTIBLE
);
471 set_current_state(TASK_RUNNING
);
472 remove_wait_queue(&rp
->wait
, &waita
);
476 static void mon_text_read_head_t(struct mon_reader_text
*rp
,
477 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
481 udir
= (ep
->is_in
? 'i' : 'o');
482 switch (ep
->xfertype
) {
483 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
484 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
485 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
486 default: /* PIPE_BULK */ utype
= 'B';
488 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
489 "%lx %u %c %c%c:%03u:%02u",
490 ep
->id
, ep
->tstamp
, ep
->type
,
491 utype
, udir
, ep
->devnum
, ep
->epnum
);
494 static void mon_text_read_head_u(struct mon_reader_text
*rp
,
495 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
499 udir
= (ep
->is_in
? 'i' : 'o');
500 switch (ep
->xfertype
) {
501 case USB_ENDPOINT_XFER_ISOC
: utype
= 'Z'; break;
502 case USB_ENDPOINT_XFER_INT
: utype
= 'I'; break;
503 case USB_ENDPOINT_XFER_CONTROL
: utype
= 'C'; break;
504 default: /* PIPE_BULK */ utype
= 'B';
506 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
507 "%lx %u %c %c%c:%d:%03u:%u",
508 ep
->id
, ep
->tstamp
, ep
->type
,
509 utype
, udir
, ep
->busnum
, ep
->devnum
, ep
->epnum
);
512 static void mon_text_read_statset(struct mon_reader_text
*rp
,
513 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
516 if (ep
->setup_flag
== 0) { /* Setup packet is present and captured */
517 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
518 " s %02x %02x %04x %04x %04x",
521 (ep
->setup
[3] << 8) | ep
->setup
[2],
522 (ep
->setup
[5] << 8) | ep
->setup
[4],
523 (ep
->setup
[7] << 8) | ep
->setup
[6]);
524 } else if (ep
->setup_flag
!= '-') { /* Unable to capture setup packet */
525 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
526 " %c __ __ ____ ____ ____", ep
->setup_flag
);
527 } else { /* No setup for this kind of URB */
528 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
533 static void mon_text_read_intstat(struct mon_reader_text
*rp
,
534 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
536 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
537 " %d:%d", ep
->status
, ep
->interval
);
540 static void mon_text_read_isostat(struct mon_reader_text
*rp
,
541 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
543 if (ep
->type
== 'S') {
544 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
545 " %d:%d:%d", ep
->status
, ep
->interval
, ep
->start_frame
);
547 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
549 ep
->status
, ep
->interval
, ep
->start_frame
, ep
->error_count
);
553 static void mon_text_read_isodesc(struct mon_reader_text
*rp
,
554 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
556 int ndesc
; /* Display this many */
558 const struct mon_iso_desc
*dp
;
560 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
563 if (ndesc
> ISODESC_MAX
)
568 for (i
= 0; i
< ndesc
; i
++) {
569 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
570 " %d:%u:%u", dp
->status
, dp
->offset
, dp
->length
);
575 static void mon_text_read_data(struct mon_reader_text
*rp
,
576 struct mon_text_ptr
*p
, const struct mon_event_text
*ep
)
580 if ((data_len
= ep
->length
) > 0) {
581 if (ep
->data_flag
== 0) {
582 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
584 if (data_len
>= DATA_MAX
)
586 for (i
= 0; i
< data_len
; i
++) {
588 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
592 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
,
594 "%02x", ep
->data
[i
]);
596 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
599 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
,
600 " %c\n", ep
->data_flag
);
603 p
->cnt
+= snprintf(p
->pbuf
+ p
->cnt
, p
->limit
- p
->cnt
, "\n");
607 static int mon_text_release(struct inode
*inode
, struct file
*file
)
609 struct mon_reader_text
*rp
= file
->private_data
;
610 struct mon_bus
*mbus
;
611 /* unsigned long flags; */
613 struct mon_event_text
*ep
;
615 mutex_lock(&mon_lock
);
616 mbus
= inode
->i_private
;
618 if (mbus
->nreaders
<= 0) {
619 printk(KERN_ERR TAG
": consistency error on close\n");
620 mutex_unlock(&mon_lock
);
623 mon_reader_del(mbus
, &rp
->r
);
626 * In theory, e_list is protected by mbus->lock. However,
627 * after mon_reader_del has finished, the following is the case:
628 * - we are not on reader list anymore, so new events won't be added;
629 * - whole mbus may be dropped if it was orphaned.
630 * So, we better not touch mbus.
632 /* spin_lock_irqsave(&mbus->lock, flags); */
633 while (!list_empty(&rp
->e_list
)) {
635 ep
= list_entry(p
, struct mon_event_text
, e_link
);
638 kmem_cache_free(rp
->e_slab
, ep
);
640 /* spin_unlock_irqrestore(&mbus->lock, flags); */
642 kmem_cache_destroy(rp
->e_slab
);
643 kfree(rp
->printf_buf
);
646 mutex_unlock(&mon_lock
);
650 static const struct file_operations mon_fops_text_t
= {
651 .owner
= THIS_MODULE
,
652 .open
= mon_text_open
,
654 .read
= mon_text_read_t
,
655 .release
= mon_text_release
,
658 static const struct file_operations mon_fops_text_u
= {
659 .owner
= THIS_MODULE
,
660 .open
= mon_text_open
,
662 .read
= mon_text_read_u
,
663 .release
= mon_text_release
,
666 int mon_text_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
669 enum { NAMESZ
= 10 };
671 int busnum
= ubus
? ubus
->busnum
: 0;
678 rc
= snprintf(name
, NAMESZ
, "%dt", busnum
);
679 if (rc
<= 0 || rc
>= NAMESZ
)
681 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
,
688 rc
= snprintf(name
, NAMESZ
, "%du", busnum
);
689 if (rc
<= 0 || rc
>= NAMESZ
)
691 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_text_u
);
696 rc
= snprintf(name
, NAMESZ
, "%ds", busnum
);
697 if (rc
<= 0 || rc
>= NAMESZ
)
699 d
= debugfs_create_file(name
, 0600, mon_dir
, mbus
, &mon_fops_stat
);
708 debugfs_remove(mbus
->dent_u
);
713 debugfs_remove(mbus
->dent_t
);
721 void mon_text_del(struct mon_bus
*mbus
)
723 debugfs_remove(mbus
->dent_u
);
724 if (mbus
->dent_t
!= NULL
)
725 debugfs_remove(mbus
->dent_t
);
726 debugfs_remove(mbus
->dent_s
);
730 * Slab interface: constructor.
732 static void mon_text_ctor(void *mem
)
735 * Nothing to initialize. No, really!
736 * So, we fill it with garbage to emulate a reused object.
738 memset(mem
, 0xe5, sizeof(struct mon_event_text
));
741 int __init
mon_text_init(void)
743 struct dentry
*mondir
;
745 mondir
= debugfs_create_dir("usbmon", usb_debug_root
);
746 if (IS_ERR(mondir
)) {
747 /* debugfs not available, but we can use usbmon without it */
750 if (mondir
== NULL
) {
751 printk(KERN_NOTICE TAG
": unable to create usbmon directory\n");
758 void mon_text_exit(void)
760 debugfs_remove(mon_dir
);