2 * The USB Monitor, inspired by Dave Harding's USBMon.
4 * This is a binary format reader.
6 * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
7 * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
10 #include <linux/kernel.h>
11 #include <linux/types.h>
13 #include <linux/cdev.h>
14 #include <linux/usb.h>
15 #include <linux/poll.h>
16 #include <linux/compat.h>
18 #include <linux/scatterlist.h>
19 #include <linux/slab.h>
21 #include <asm/uaccess.h>
26 * Defined by USB 2.0 clause 9.3, table 9.2.
31 #define MON_IOC_MAGIC 0x92
33 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
34 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
35 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
36 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
37 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
38 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
39 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
40 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
41 /* #9 was MON_IOCT_SETAPI */
42 #define MON_IOCX_GETX _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
45 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
46 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
47 #define MON_IOCX_GETX32 _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
51 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
52 * But it's all right. Just use a simple way to make sure the chunk is never
53 * smaller than a page.
55 * N.B. An application does not know our chunk size.
57 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
58 * page-sized chunks for the time being.
60 #define CHUNK_SIZE PAGE_SIZE
61 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
64 * The magic limit was calculated so that it allows the monitoring
65 * application to pick data once in two ticks. This way, another application,
66 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
67 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
68 * enormous overhead built into the bus protocol, so we need about 1000 KB.
70 * This is still too much for most cases, where we just snoop a few
71 * descriptor fetches for enumeration. So, the default is a "reasonable"
72 * amount for systems with HZ=250 and incomplete bus saturation.
74 * XXX What about multi-megabyte URBs which take minutes to transfer?
76 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
77 #define BUFF_DFL CHUNK_ALIGN(300*1024)
78 #define BUFF_MIN CHUNK_ALIGN(8*1024)
81 * The per-event API header (2 per URB).
83 * This structure is seen in userland as defined by the documentation.
86 u64 id
; /* URB ID - from submission to callback */
87 unsigned char type
; /* Same as in text API; extensible. */
88 unsigned char xfer_type
; /* ISO, Intr, Control, Bulk */
89 unsigned char epnum
; /* Endpoint number and transfer direction */
90 unsigned char devnum
; /* Device address */
91 unsigned short busnum
; /* Bus number */
94 s64 ts_sec
; /* gettimeofday */
95 s32 ts_usec
; /* gettimeofday */
97 unsigned int len_urb
; /* Length of data (submitted or actual) */
98 unsigned int len_cap
; /* Delivered length */
100 unsigned char setup
[SETUP_LEN
]; /* Only for Control S-type */
108 unsigned int xfer_flags
;
109 unsigned int ndesc
; /* Actual number of ISO descriptors */
113 * ISO vector, packed into the head of data stream.
114 * This has to take 16 bytes to make sure that the end of buffer
115 * wrap is not happening in the middle of a descriptor.
117 struct mon_bin_isodesc
{
119 unsigned int iso_off
;
120 unsigned int iso_len
;
124 /* per file statistic */
125 struct mon_bin_stats
{
131 struct mon_bin_hdr __user
*hdr
; /* Can be 48 bytes or 64. */
133 size_t alloc
; /* Length of data (can be zero) */
136 struct mon_bin_mfetch
{
137 u32 __user
*offvec
; /* Vector of events fetched */
138 u32 nfetch
; /* Number of events to fetch (out: fetched) */
139 u32 nflush
; /* Number of events to flush */
143 struct mon_bin_get32
{
149 struct mon_bin_mfetch32
{
156 /* Having these two values same prevents wrapping of the mon_bin_hdr */
160 #define PKT_SZ_API0 48 /* API 0 (2.6.20) size */
161 #define PKT_SZ_API1 64 /* API 1 size: extra fields */
163 #define ISODESC_MAX 128 /* Same number as usbfs allows, 2048 bytes. */
165 /* max number of USB bus supported */
166 #define MON_BIN_MAX_MINOR 128
169 * The buffer: map of used pages.
173 unsigned char *ptr
; /* XXX just use page_to_virt everywhere? */
177 * This gets associated with an open file struct.
179 struct mon_reader_bin
{
180 /* The buffer: one per open. */
181 spinlock_t b_lock
; /* Protect b_cnt, b_in */
182 unsigned int b_size
; /* Current size of the buffer - bytes */
183 unsigned int b_cnt
; /* Bytes used */
184 unsigned int b_in
, b_out
; /* Offsets into buffer - bytes */
185 unsigned int b_read
; /* Amount of read data in curr. pkt. */
186 struct mon_pgmap
*b_vec
; /* The map array */
187 wait_queue_head_t b_wait
; /* Wait for data here */
189 struct mutex fetch_lock
; /* Protect b_read, b_out */
192 /* A list of these is needed for "bus 0". Some time later. */
196 unsigned int cnt_lost
;
199 static inline struct mon_bin_hdr
*MON_OFF2HDR(const struct mon_reader_bin
*rp
,
202 return (struct mon_bin_hdr
*)
203 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
206 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
208 static unsigned char xfer_to_pipe
[4] = {
209 PIPE_CONTROL
, PIPE_ISOCHRONOUS
, PIPE_BULK
, PIPE_INTERRUPT
212 static struct class *mon_bin_class
;
213 static dev_t mon_bin_dev0
;
214 static struct cdev mon_bin_cdev
;
216 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
217 unsigned int offset
, unsigned int size
);
218 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
);
219 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
);
220 static void mon_free_buff(struct mon_pgmap
*map
, int npages
);
223 * This is a "chunked memcpy". It does not manipulate any counters.
225 static unsigned int mon_copy_to_buff(const struct mon_reader_bin
*this,
226 unsigned int off
, const unsigned char *from
, unsigned int length
)
228 unsigned int step_len
;
230 unsigned int in_page
;
234 * Determine step_len.
237 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
238 if (in_page
< step_len
)
242 * Copy data and advance pointers.
244 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
245 memcpy(buf
, from
, step_len
);
246 if ((off
+= step_len
) >= this->b_size
) off
= 0;
254 * This is a little worse than the above because it's "chunked copy_to_user".
255 * The return value is an error code, not an offset.
257 static int copy_from_buf(const struct mon_reader_bin
*this, unsigned int off
,
258 char __user
*to
, int length
)
260 unsigned int step_len
;
262 unsigned int in_page
;
266 * Determine step_len.
269 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
270 if (in_page
< step_len
)
274 * Copy data and advance pointers.
276 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
277 if (copy_to_user(to
, buf
, step_len
))
279 if ((off
+= step_len
) >= this->b_size
) off
= 0;
287 * Allocate an (aligned) area in the buffer.
288 * This is called under b_lock.
289 * Returns ~0 on failure.
291 static unsigned int mon_buff_area_alloc(struct mon_reader_bin
*rp
,
296 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
297 if (rp
->b_cnt
+ size
> rp
->b_size
)
301 if ((rp
->b_in
+= size
) >= rp
->b_size
)
302 rp
->b_in
-= rp
->b_size
;
307 * This is the same thing as mon_buff_area_alloc, only it does not allow
308 * buffers to wrap. This is needed by applications which pass references
309 * into mmap-ed buffers up their stacks (libpcap can do that).
311 * Currently, we always have the header stuck with the data, although
312 * it is not strictly speaking necessary.
314 * When a buffer would wrap, we place a filler packet to mark the space.
316 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin
*rp
,
320 unsigned int fill_size
;
322 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
323 if (rp
->b_cnt
+ size
> rp
->b_size
)
325 if (rp
->b_in
+ size
> rp
->b_size
) {
327 * This would wrap. Find if we still have space after
328 * skipping to the end of the buffer. If we do, place
329 * a filler packet and allocate a new packet.
331 fill_size
= rp
->b_size
- rp
->b_in
;
332 if (rp
->b_cnt
+ size
+ fill_size
> rp
->b_size
)
334 mon_buff_area_fill(rp
, rp
->b_in
, fill_size
);
338 rp
->b_cnt
+= size
+ fill_size
;
339 } else if (rp
->b_in
+ size
== rp
->b_size
) {
352 * Return a few (kilo-)bytes to the head of the buffer.
353 * This is used if a data fetch fails.
355 static void mon_buff_area_shrink(struct mon_reader_bin
*rp
, unsigned int size
)
358 /* size &= ~(PKT_ALIGN-1); -- we're called with aligned size */
361 rp
->b_in
+= rp
->b_size
;
366 * This has to be called under both b_lock and fetch_lock, because
367 * it accesses both b_cnt and b_out.
369 static void mon_buff_area_free(struct mon_reader_bin
*rp
, unsigned int size
)
372 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
374 if ((rp
->b_out
+= size
) >= rp
->b_size
)
375 rp
->b_out
-= rp
->b_size
;
378 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
379 unsigned int offset
, unsigned int size
)
381 struct mon_bin_hdr
*ep
;
383 ep
= MON_OFF2HDR(rp
, offset
);
384 memset(ep
, 0, PKT_SIZE
);
386 ep
->len_cap
= size
- PKT_SIZE
;
389 static inline char mon_bin_get_setup(unsigned char *setupb
,
390 const struct urb
*urb
, char ev_type
)
393 if (urb
->setup_packet
== NULL
)
395 memcpy(setupb
, urb
->setup_packet
, SETUP_LEN
);
399 static unsigned int mon_bin_get_data(const struct mon_reader_bin
*rp
,
400 unsigned int offset
, struct urb
*urb
, unsigned int length
,
404 struct scatterlist
*sg
;
405 unsigned int this_len
;
408 if (urb
->num_sgs
== 0) {
409 if (urb
->transfer_buffer
== NULL
) {
413 mon_copy_to_buff(rp
, offset
, urb
->transfer_buffer
, length
);
417 /* If IOMMU coalescing occurred, we cannot trust sg_page */
418 if (urb
->transfer_flags
& URB_DMA_SG_COMBINED
) {
423 /* Copy up to the first non-addressable segment */
424 for_each_sg(urb
->sg
, sg
, urb
->num_sgs
, i
) {
425 if (length
== 0 || PageHighMem(sg_page(sg
)))
427 this_len
= min_t(unsigned int, sg
->length
, length
);
428 offset
= mon_copy_to_buff(rp
, offset
, sg_virt(sg
),
440 * This is the look-ahead pass in case of 'C Zi', when actual_length cannot
441 * be used to determine the length of the whole contiguous buffer.
443 static unsigned int mon_bin_collate_isodesc(const struct mon_reader_bin
*rp
,
444 struct urb
*urb
, unsigned int ndesc
)
446 struct usb_iso_packet_descriptor
*fp
;
450 fp
= urb
->iso_frame_desc
;
451 while (ndesc
-- != 0) {
452 if (fp
->actual_length
!= 0) {
453 if (fp
->offset
+ fp
->actual_length
> length
)
454 length
= fp
->offset
+ fp
->actual_length
;
461 static void mon_bin_get_isodesc(const struct mon_reader_bin
*rp
,
462 unsigned int offset
, struct urb
*urb
, char ev_type
, unsigned int ndesc
)
464 struct mon_bin_isodesc
*dp
;
465 struct usb_iso_packet_descriptor
*fp
;
467 fp
= urb
->iso_frame_desc
;
468 while (ndesc
-- != 0) {
469 dp
= (struct mon_bin_isodesc
*)
470 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
471 dp
->iso_status
= fp
->status
;
472 dp
->iso_off
= fp
->offset
;
473 dp
->iso_len
= (ev_type
== 'S') ? fp
->length
: fp
->actual_length
;
475 if ((offset
+= sizeof(struct mon_bin_isodesc
)) >= rp
->b_size
)
481 static void mon_bin_event(struct mon_reader_bin
*rp
, struct urb
*urb
,
482 char ev_type
, int status
)
484 const struct usb_endpoint_descriptor
*epd
= &urb
->ep
->desc
;
487 unsigned int urb_length
;
491 unsigned int ndesc
, lendesc
;
493 struct mon_bin_hdr
*ep
;
496 do_gettimeofday(&ts
);
498 spin_lock_irqsave(&rp
->b_lock
, flags
);
501 * Find the maximum allowable length, then allocate space.
503 urb_length
= (ev_type
== 'S') ?
504 urb
->transfer_buffer_length
: urb
->actual_length
;
507 if (usb_endpoint_xfer_isoc(epd
)) {
508 if (urb
->number_of_packets
< 0) {
510 } else if (urb
->number_of_packets
>= ISODESC_MAX
) {
513 ndesc
= urb
->number_of_packets
;
515 if (ev_type
== 'C' && usb_urb_dir_in(urb
))
516 length
= mon_bin_collate_isodesc(rp
, urb
, ndesc
);
520 lendesc
= ndesc
*sizeof(struct mon_bin_isodesc
);
522 /* not an issue unless there's a subtle bug in a HCD somewhere */
523 if (length
>= urb
->transfer_buffer_length
)
524 length
= urb
->transfer_buffer_length
;
526 if (length
>= rp
->b_size
/5)
527 length
= rp
->b_size
/5;
529 if (usb_urb_dir_in(urb
)) {
530 if (ev_type
== 'S') {
534 /* Cannot rely on endpoint number in case of control ep.0 */
537 if (ev_type
== 'C') {
544 if (rp
->mmap_active
) {
545 offset
= mon_buff_area_alloc_contiguous(rp
,
546 length
+ PKT_SIZE
+ lendesc
);
548 offset
= mon_buff_area_alloc(rp
, length
+ PKT_SIZE
+ lendesc
);
552 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
556 ep
= MON_OFF2HDR(rp
, offset
);
557 if ((offset
+= PKT_SIZE
) >= rp
->b_size
) offset
= 0;
560 * Fill the allocated area.
562 memset(ep
, 0, PKT_SIZE
);
564 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(epd
)];
565 ep
->epnum
= dir
| usb_endpoint_num(epd
);
566 ep
->devnum
= urb
->dev
->devnum
;
567 ep
->busnum
= urb
->dev
->bus
->busnum
;
568 ep
->id
= (unsigned long) urb
;
569 ep
->ts_sec
= ts
.tv_sec
;
570 ep
->ts_usec
= ts
.tv_usec
;
572 ep
->len_urb
= urb_length
;
573 ep
->len_cap
= length
+ lendesc
;
574 ep
->xfer_flags
= urb
->transfer_flags
;
576 if (usb_endpoint_xfer_int(epd
)) {
577 ep
->interval
= urb
->interval
;
578 } else if (usb_endpoint_xfer_isoc(epd
)) {
579 ep
->interval
= urb
->interval
;
580 ep
->start_frame
= urb
->start_frame
;
581 ep
->s
.iso
.error_count
= urb
->error_count
;
582 ep
->s
.iso
.numdesc
= urb
->number_of_packets
;
585 if (usb_endpoint_xfer_control(epd
) && ev_type
== 'S') {
586 ep
->flag_setup
= mon_bin_get_setup(ep
->s
.setup
, urb
, ev_type
);
588 ep
->flag_setup
= '-';
593 mon_bin_get_isodesc(rp
, offset
, urb
, ev_type
, ndesc
);
594 if ((offset
+= lendesc
) >= rp
->b_size
)
595 offset
-= rp
->b_size
;
599 length
= mon_bin_get_data(rp
, offset
, urb
, length
,
602 delta
= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
603 ep
->len_cap
-= length
;
604 delta
-= (ep
->len_cap
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
605 mon_buff_area_shrink(rp
, delta
);
608 ep
->flag_data
= data_tag
;
611 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
613 wake_up(&rp
->b_wait
);
616 static void mon_bin_submit(void *data
, struct urb
*urb
)
618 struct mon_reader_bin
*rp
= data
;
619 mon_bin_event(rp
, urb
, 'S', -EINPROGRESS
);
622 static void mon_bin_complete(void *data
, struct urb
*urb
, int status
)
624 struct mon_reader_bin
*rp
= data
;
625 mon_bin_event(rp
, urb
, 'C', status
);
628 static void mon_bin_error(void *data
, struct urb
*urb
, int error
)
630 struct mon_reader_bin
*rp
= data
;
634 struct mon_bin_hdr
*ep
;
636 do_gettimeofday(&ts
);
638 spin_lock_irqsave(&rp
->b_lock
, flags
);
640 offset
= mon_buff_area_alloc(rp
, PKT_SIZE
);
642 /* Not incrementing cnt_lost. Just because. */
643 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
647 ep
= MON_OFF2HDR(rp
, offset
);
649 memset(ep
, 0, PKT_SIZE
);
651 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(&urb
->ep
->desc
)];
652 ep
->epnum
= usb_urb_dir_in(urb
) ? USB_DIR_IN
: 0;
653 ep
->epnum
|= usb_endpoint_num(&urb
->ep
->desc
);
654 ep
->devnum
= urb
->dev
->devnum
;
655 ep
->busnum
= urb
->dev
->bus
->busnum
;
656 ep
->id
= (unsigned long) urb
;
657 ep
->ts_sec
= ts
.tv_sec
;
658 ep
->ts_usec
= ts
.tv_usec
;
661 ep
->flag_setup
= '-';
664 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
666 wake_up(&rp
->b_wait
);
669 static int mon_bin_open(struct inode
*inode
, struct file
*file
)
671 struct mon_bus
*mbus
;
672 struct mon_reader_bin
*rp
;
676 mutex_lock(&mon_lock
);
677 if ((mbus
= mon_bus_lookup(iminor(inode
))) == NULL
) {
678 mutex_unlock(&mon_lock
);
681 if (mbus
!= &mon_bus0
&& mbus
->u_bus
== NULL
) {
682 printk(KERN_ERR TAG
": consistency error on open\n");
683 mutex_unlock(&mon_lock
);
687 rp
= kzalloc(sizeof(struct mon_reader_bin
), GFP_KERNEL
);
692 spin_lock_init(&rp
->b_lock
);
693 init_waitqueue_head(&rp
->b_wait
);
694 mutex_init(&rp
->fetch_lock
);
695 rp
->b_size
= BUFF_DFL
;
697 size
= sizeof(struct mon_pgmap
) * (rp
->b_size
/CHUNK_SIZE
);
698 if ((rp
->b_vec
= kzalloc(size
, GFP_KERNEL
)) == NULL
) {
703 if ((rc
= mon_alloc_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
)) < 0)
708 rp
->r
.rnf_submit
= mon_bin_submit
;
709 rp
->r
.rnf_error
= mon_bin_error
;
710 rp
->r
.rnf_complete
= mon_bin_complete
;
712 mon_reader_add(mbus
, &rp
->r
);
714 file
->private_data
= rp
;
715 mutex_unlock(&mon_lock
);
723 mutex_unlock(&mon_lock
);
728 * Extract an event from buffer and copy it to user space.
729 * Wait if there is no event ready.
730 * Returns zero or error.
732 static int mon_bin_get_event(struct file
*file
, struct mon_reader_bin
*rp
,
733 struct mon_bin_hdr __user
*hdr
, unsigned int hdrbytes
,
734 void __user
*data
, unsigned int nbytes
)
737 struct mon_bin_hdr
*ep
;
742 mutex_lock(&rp
->fetch_lock
);
744 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
745 mutex_unlock(&rp
->fetch_lock
);
749 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
751 if (copy_to_user(hdr
, ep
, hdrbytes
)) {
752 mutex_unlock(&rp
->fetch_lock
);
756 step_len
= min(ep
->len_cap
, nbytes
);
757 if ((offset
= rp
->b_out
+ PKT_SIZE
) >= rp
->b_size
) offset
= 0;
759 if (copy_from_buf(rp
, offset
, data
, step_len
)) {
760 mutex_unlock(&rp
->fetch_lock
);
764 spin_lock_irqsave(&rp
->b_lock
, flags
);
765 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
766 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
769 mutex_unlock(&rp
->fetch_lock
);
773 static int mon_bin_release(struct inode
*inode
, struct file
*file
)
775 struct mon_reader_bin
*rp
= file
->private_data
;
776 struct mon_bus
* mbus
= rp
->r
.m_bus
;
778 mutex_lock(&mon_lock
);
780 if (mbus
->nreaders
<= 0) {
781 printk(KERN_ERR TAG
": consistency error on close\n");
782 mutex_unlock(&mon_lock
);
785 mon_reader_del(mbus
, &rp
->r
);
787 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
791 mutex_unlock(&mon_lock
);
795 static ssize_t
mon_bin_read(struct file
*file
, char __user
*buf
,
796 size_t nbytes
, loff_t
*ppos
)
798 struct mon_reader_bin
*rp
= file
->private_data
;
799 unsigned int hdrbytes
= PKT_SZ_API0
;
801 struct mon_bin_hdr
*ep
;
808 mutex_lock(&rp
->fetch_lock
);
810 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
811 mutex_unlock(&rp
->fetch_lock
);
815 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
817 if (rp
->b_read
< hdrbytes
) {
818 step_len
= min(nbytes
, (size_t)(hdrbytes
- rp
->b_read
));
819 ptr
= ((char *)ep
) + rp
->b_read
;
820 if (step_len
&& copy_to_user(buf
, ptr
, step_len
)) {
821 mutex_unlock(&rp
->fetch_lock
);
826 rp
->b_read
+= step_len
;
830 if (rp
->b_read
>= hdrbytes
) {
831 step_len
= ep
->len_cap
;
832 step_len
-= rp
->b_read
- hdrbytes
;
833 if (step_len
> nbytes
)
835 offset
= rp
->b_out
+ PKT_SIZE
;
836 offset
+= rp
->b_read
- hdrbytes
;
837 if (offset
>= rp
->b_size
)
838 offset
-= rp
->b_size
;
839 if (copy_from_buf(rp
, offset
, buf
, step_len
)) {
840 mutex_unlock(&rp
->fetch_lock
);
845 rp
->b_read
+= step_len
;
850 * Check if whole packet was read, and if so, jump to the next one.
852 if (rp
->b_read
>= hdrbytes
+ ep
->len_cap
) {
853 spin_lock_irqsave(&rp
->b_lock
, flags
);
854 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
855 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
859 mutex_unlock(&rp
->fetch_lock
);
864 * Remove at most nevents from chunked buffer.
865 * Returns the number of removed events.
867 static int mon_bin_flush(struct mon_reader_bin
*rp
, unsigned nevents
)
870 struct mon_bin_hdr
*ep
;
873 mutex_lock(&rp
->fetch_lock
);
874 spin_lock_irqsave(&rp
->b_lock
, flags
);
875 for (i
= 0; i
< nevents
; ++i
) {
876 if (MON_RING_EMPTY(rp
))
879 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
880 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
882 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
884 mutex_unlock(&rp
->fetch_lock
);
889 * Fetch at most max event offsets into the buffer and put them into vec.
890 * The events are usually freed later with mon_bin_flush.
891 * Return the effective number of events fetched.
893 static int mon_bin_fetch(struct file
*file
, struct mon_reader_bin
*rp
,
894 u32 __user
*vec
, unsigned int max
)
896 unsigned int cur_out
;
897 unsigned int bytes
, avail
;
899 unsigned int nevents
;
900 struct mon_bin_hdr
*ep
;
904 mutex_lock(&rp
->fetch_lock
);
906 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
907 mutex_unlock(&rp
->fetch_lock
);
911 spin_lock_irqsave(&rp
->b_lock
, flags
);
913 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
918 while (bytes
< avail
) {
922 ep
= MON_OFF2HDR(rp
, cur_out
);
923 if (put_user(cur_out
, &vec
[nevents
])) {
924 mutex_unlock(&rp
->fetch_lock
);
929 size
= ep
->len_cap
+ PKT_SIZE
;
930 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
931 if ((cur_out
+= size
) >= rp
->b_size
)
932 cur_out
-= rp
->b_size
;
936 mutex_unlock(&rp
->fetch_lock
);
941 * Count events. This is almost the same as the above mon_bin_fetch,
942 * only we do not store offsets into user vector, and we have no limit.
944 static int mon_bin_queued(struct mon_reader_bin
*rp
)
946 unsigned int cur_out
;
947 unsigned int bytes
, avail
;
949 unsigned int nevents
;
950 struct mon_bin_hdr
*ep
;
953 mutex_lock(&rp
->fetch_lock
);
955 spin_lock_irqsave(&rp
->b_lock
, flags
);
957 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
962 while (bytes
< avail
) {
963 ep
= MON_OFF2HDR(rp
, cur_out
);
966 size
= ep
->len_cap
+ PKT_SIZE
;
967 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
968 if ((cur_out
+= size
) >= rp
->b_size
)
969 cur_out
-= rp
->b_size
;
973 mutex_unlock(&rp
->fetch_lock
);
979 static long mon_bin_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
981 struct mon_reader_bin
*rp
= file
->private_data
;
982 // struct mon_bus* mbus = rp->r.m_bus;
984 struct mon_bin_hdr
*ep
;
989 case MON_IOCQ_URB_LEN
:
991 * N.B. This only returns the size of data, without the header.
993 spin_lock_irqsave(&rp
->b_lock
, flags
);
994 if (!MON_RING_EMPTY(rp
)) {
995 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
998 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1001 case MON_IOCQ_RING_SIZE
:
1005 case MON_IOCT_RING_SIZE
:
1007 * Changing the buffer size will flush it's contents; the new
1008 * buffer is allocated before releasing the old one to be sure
1009 * the device will stay functional also in case of memory
1014 struct mon_pgmap
*vec
;
1016 if (arg
< BUFF_MIN
|| arg
> BUFF_MAX
)
1019 size
= CHUNK_ALIGN(arg
);
1020 if ((vec
= kzalloc(sizeof(struct mon_pgmap
) * (size
/CHUNK_SIZE
),
1021 GFP_KERNEL
)) == NULL
) {
1026 ret
= mon_alloc_buff(vec
, size
/CHUNK_SIZE
);
1032 mutex_lock(&rp
->fetch_lock
);
1033 spin_lock_irqsave(&rp
->b_lock
, flags
);
1034 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
1038 rp
->b_read
= rp
->b_in
= rp
->b_out
= rp
->b_cnt
= 0;
1040 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1041 mutex_unlock(&rp
->fetch_lock
);
1045 case MON_IOCH_MFLUSH
:
1046 ret
= mon_bin_flush(rp
, arg
);
1052 struct mon_bin_get getb
;
1054 if (copy_from_user(&getb
, (void __user
*)arg
,
1055 sizeof(struct mon_bin_get
)))
1058 if (getb
.alloc
> 0x10000000) /* Want to cast to u32 */
1060 ret
= mon_bin_get_event(file
, rp
, getb
.hdr
,
1061 (cmd
== MON_IOCX_GET
)? PKT_SZ_API0
: PKT_SZ_API1
,
1062 getb
.data
, (unsigned int)getb
.alloc
);
1066 case MON_IOCX_MFETCH
:
1068 struct mon_bin_mfetch mfetch
;
1069 struct mon_bin_mfetch __user
*uptr
;
1071 uptr
= (struct mon_bin_mfetch __user
*)arg
;
1073 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1076 if (mfetch
.nflush
) {
1077 ret
= mon_bin_flush(rp
, mfetch
.nflush
);
1080 if (put_user(ret
, &uptr
->nflush
))
1083 ret
= mon_bin_fetch(file
, rp
, mfetch
.offvec
, mfetch
.nfetch
);
1086 if (put_user(ret
, &uptr
->nfetch
))
1092 case MON_IOCG_STATS
: {
1093 struct mon_bin_stats __user
*sp
;
1094 unsigned int nevents
;
1095 unsigned int ndropped
;
1097 spin_lock_irqsave(&rp
->b_lock
, flags
);
1098 ndropped
= rp
->cnt_lost
;
1100 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1101 nevents
= mon_bin_queued(rp
);
1103 sp
= (struct mon_bin_stats __user
*)arg
;
1104 if (put_user(ndropped
, &sp
->dropped
))
1106 if (put_user(nevents
, &sp
->queued
))
1119 #ifdef CONFIG_COMPAT
1120 static long mon_bin_compat_ioctl(struct file
*file
,
1121 unsigned int cmd
, unsigned long arg
)
1123 struct mon_reader_bin
*rp
= file
->private_data
;
1128 case MON_IOCX_GET32
:
1129 case MON_IOCX_GETX32
:
1131 struct mon_bin_get32 getb
;
1133 if (copy_from_user(&getb
, (void __user
*)arg
,
1134 sizeof(struct mon_bin_get32
)))
1137 ret
= mon_bin_get_event(file
, rp
, compat_ptr(getb
.hdr32
),
1138 (cmd
== MON_IOCX_GET32
)? PKT_SZ_API0
: PKT_SZ_API1
,
1139 compat_ptr(getb
.data32
), getb
.alloc32
);
1145 case MON_IOCX_MFETCH32
:
1147 struct mon_bin_mfetch32 mfetch
;
1148 struct mon_bin_mfetch32 __user
*uptr
;
1150 uptr
= (struct mon_bin_mfetch32 __user
*) compat_ptr(arg
);
1152 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
1155 if (mfetch
.nflush32
) {
1156 ret
= mon_bin_flush(rp
, mfetch
.nflush32
);
1159 if (put_user(ret
, &uptr
->nflush32
))
1162 ret
= mon_bin_fetch(file
, rp
, compat_ptr(mfetch
.offvec32
),
1166 if (put_user(ret
, &uptr
->nfetch32
))
1171 case MON_IOCG_STATS
:
1172 return mon_bin_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
1174 case MON_IOCQ_URB_LEN
:
1175 case MON_IOCQ_RING_SIZE
:
1176 case MON_IOCT_RING_SIZE
:
1177 case MON_IOCH_MFLUSH
:
1178 return mon_bin_ioctl(file
, cmd
, arg
);
1185 #endif /* CONFIG_COMPAT */
1188 mon_bin_poll(struct file
*file
, struct poll_table_struct
*wait
)
1190 struct mon_reader_bin
*rp
= file
->private_data
;
1191 unsigned int mask
= 0;
1192 unsigned long flags
;
1194 if (file
->f_mode
& FMODE_READ
)
1195 poll_wait(file
, &rp
->b_wait
, wait
);
1197 spin_lock_irqsave(&rp
->b_lock
, flags
);
1198 if (!MON_RING_EMPTY(rp
))
1199 mask
|= POLLIN
| POLLRDNORM
; /* readable */
1200 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1205 * open and close: just keep track of how many times the device is
1206 * mapped, to use the proper memory allocation function.
1208 static void mon_bin_vma_open(struct vm_area_struct
*vma
)
1210 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1214 static void mon_bin_vma_close(struct vm_area_struct
*vma
)
1216 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1221 * Map ring pages to user space.
1223 static int mon_bin_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1225 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1226 unsigned long offset
, chunk_idx
;
1227 struct page
*pageptr
;
1229 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1230 if (offset
>= rp
->b_size
)
1231 return VM_FAULT_SIGBUS
;
1232 chunk_idx
= offset
/ CHUNK_SIZE
;
1233 pageptr
= rp
->b_vec
[chunk_idx
].pg
;
1235 vmf
->page
= pageptr
;
1239 static const struct vm_operations_struct mon_bin_vm_ops
= {
1240 .open
= mon_bin_vma_open
,
1241 .close
= mon_bin_vma_close
,
1242 .fault
= mon_bin_vma_fault
,
1245 static int mon_bin_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1247 /* don't do anything here: "fault" will set up page table entries */
1248 vma
->vm_ops
= &mon_bin_vm_ops
;
1249 vma
->vm_flags
|= VM_RESERVED
;
1250 vma
->vm_private_data
= filp
->private_data
;
1251 mon_bin_vma_open(vma
);
1255 static const struct file_operations mon_fops_binary
= {
1256 .owner
= THIS_MODULE
,
1257 .open
= mon_bin_open
,
1258 .llseek
= no_llseek
,
1259 .read
= mon_bin_read
,
1260 /* .write = mon_text_write, */
1261 .poll
= mon_bin_poll
,
1262 .unlocked_ioctl
= mon_bin_ioctl
,
1263 #ifdef CONFIG_COMPAT
1264 .compat_ioctl
= mon_bin_compat_ioctl
,
1266 .release
= mon_bin_release
,
1267 .mmap
= mon_bin_mmap
,
1270 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
)
1272 DECLARE_WAITQUEUE(waita
, current
);
1273 unsigned long flags
;
1275 add_wait_queue(&rp
->b_wait
, &waita
);
1276 set_current_state(TASK_INTERRUPTIBLE
);
1278 spin_lock_irqsave(&rp
->b_lock
, flags
);
1279 while (MON_RING_EMPTY(rp
)) {
1280 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1282 if (file
->f_flags
& O_NONBLOCK
) {
1283 set_current_state(TASK_RUNNING
);
1284 remove_wait_queue(&rp
->b_wait
, &waita
);
1285 return -EWOULDBLOCK
; /* Same as EAGAIN in Linux */
1288 if (signal_pending(current
)) {
1289 remove_wait_queue(&rp
->b_wait
, &waita
);
1292 set_current_state(TASK_INTERRUPTIBLE
);
1294 spin_lock_irqsave(&rp
->b_lock
, flags
);
1296 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1298 set_current_state(TASK_RUNNING
);
1299 remove_wait_queue(&rp
->b_wait
, &waita
);
1303 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
)
1306 unsigned long vaddr
;
1308 for (n
= 0; n
< npages
; n
++) {
1309 vaddr
= get_zeroed_page(GFP_KERNEL
);
1312 free_page((unsigned long) map
[n
].ptr
);
1315 map
[n
].ptr
= (unsigned char *) vaddr
;
1316 map
[n
].pg
= virt_to_page((void *) vaddr
);
1321 static void mon_free_buff(struct mon_pgmap
*map
, int npages
)
1325 for (n
= 0; n
< npages
; n
++)
1326 free_page((unsigned long) map
[n
].ptr
);
1329 int mon_bin_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
1332 unsigned minor
= ubus
? ubus
->busnum
: 0;
1334 if (minor
>= MON_BIN_MAX_MINOR
)
1337 dev
= device_create(mon_bin_class
, ubus
? ubus
->controller
: NULL
,
1338 MKDEV(MAJOR(mon_bin_dev0
), minor
), NULL
,
1343 mbus
->classdev
= dev
;
1347 void mon_bin_del(struct mon_bus
*mbus
)
1349 device_destroy(mon_bin_class
, mbus
->classdev
->devt
);
1352 int __init
mon_bin_init(void)
1356 mon_bin_class
= class_create(THIS_MODULE
, "usbmon");
1357 if (IS_ERR(mon_bin_class
)) {
1358 rc
= PTR_ERR(mon_bin_class
);
1362 rc
= alloc_chrdev_region(&mon_bin_dev0
, 0, MON_BIN_MAX_MINOR
, "usbmon");
1366 cdev_init(&mon_bin_cdev
, &mon_fops_binary
);
1367 mon_bin_cdev
.owner
= THIS_MODULE
;
1369 rc
= cdev_add(&mon_bin_cdev
, mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1376 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1378 class_destroy(mon_bin_class
);
1383 void mon_bin_exit(void)
1385 cdev_del(&mon_bin_cdev
);
1386 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1387 class_destroy(mon_bin_class
);