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>
19 #include <asm/uaccess.h>
24 * Defined by USB 2.0 clause 9.3, table 9.2.
29 #define MON_IOC_MAGIC 0x92
31 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
32 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
33 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
34 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
35 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
36 #define MON_IOCX_GET _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
37 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
38 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
40 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
41 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
45 * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
46 * But it's all right. Just use a simple way to make sure the chunk is never
47 * smaller than a page.
49 * N.B. An application does not know our chunk size.
51 * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
52 * page-sized chunks for the time being.
54 #define CHUNK_SIZE PAGE_SIZE
55 #define CHUNK_ALIGN(x) (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
58 * The magic limit was calculated so that it allows the monitoring
59 * application to pick data once in two ticks. This way, another application,
60 * which presumably drives the bus, gets to hog CPU, yet we collect our data.
61 * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
62 * enormous overhead built into the bus protocol, so we need about 1000 KB.
64 * This is still too much for most cases, where we just snoop a few
65 * descriptor fetches for enumeration. So, the default is a "reasonable"
66 * amount for systems with HZ=250 and incomplete bus saturation.
68 * XXX What about multi-megabyte URBs which take minutes to transfer?
70 #define BUFF_MAX CHUNK_ALIGN(1200*1024)
71 #define BUFF_DFL CHUNK_ALIGN(300*1024)
72 #define BUFF_MIN CHUNK_ALIGN(8*1024)
75 * The per-event API header (2 per URB).
77 * This structure is seen in userland as defined by the documentation.
80 u64 id
; /* URB ID - from submission to callback */
81 unsigned char type
; /* Same as in text API; extensible. */
82 unsigned char xfer_type
; /* ISO, Intr, Control, Bulk */
83 unsigned char epnum
; /* Endpoint number and transfer direction */
84 unsigned char devnum
; /* Device address */
85 unsigned short busnum
; /* Bus number */
88 s64 ts_sec
; /* gettimeofday */
89 s32 ts_usec
; /* gettimeofday */
91 unsigned int len_urb
; /* Length of data (submitted or actual) */
92 unsigned int len_cap
; /* Delivered length */
93 unsigned char setup
[SETUP_LEN
]; /* Only for Control S-type */
96 /* per file statistic */
97 struct mon_bin_stats
{
103 struct mon_bin_hdr __user
*hdr
; /* Only 48 bytes, not 64. */
105 size_t alloc
; /* Length of data (can be zero) */
108 struct mon_bin_mfetch
{
109 u32 __user
*offvec
; /* Vector of events fetched */
110 u32 nfetch
; /* Number of events to fetch (out: fetched) */
111 u32 nflush
; /* Number of events to flush */
115 struct mon_bin_get32
{
121 struct mon_bin_mfetch32
{
128 /* Having these two values same prevents wrapping of the mon_bin_hdr */
132 /* max number of USB bus supported */
133 #define MON_BIN_MAX_MINOR 128
136 * The buffer: map of used pages.
140 unsigned char *ptr
; /* XXX just use page_to_virt everywhere? */
144 * This gets associated with an open file struct.
146 struct mon_reader_bin
{
147 /* The buffer: one per open. */
148 spinlock_t b_lock
; /* Protect b_cnt, b_in */
149 unsigned int b_size
; /* Current size of the buffer - bytes */
150 unsigned int b_cnt
; /* Bytes used */
151 unsigned int b_in
, b_out
; /* Offsets into buffer - bytes */
152 unsigned int b_read
; /* Amount of read data in curr. pkt. */
153 struct mon_pgmap
*b_vec
; /* The map array */
154 wait_queue_head_t b_wait
; /* Wait for data here */
156 struct mutex fetch_lock
; /* Protect b_read, b_out */
159 /* A list of these is needed for "bus 0". Some time later. */
163 unsigned int cnt_lost
;
166 static inline struct mon_bin_hdr
*MON_OFF2HDR(const struct mon_reader_bin
*rp
,
169 return (struct mon_bin_hdr
*)
170 (rp
->b_vec
[offset
/ CHUNK_SIZE
].ptr
+ offset
% CHUNK_SIZE
);
173 #define MON_RING_EMPTY(rp) ((rp)->b_cnt == 0)
175 static unsigned char xfer_to_pipe
[4] = {
176 PIPE_CONTROL
, PIPE_ISOCHRONOUS
, PIPE_BULK
, PIPE_INTERRUPT
179 static struct class *mon_bin_class
;
180 static dev_t mon_bin_dev0
;
181 static struct cdev mon_bin_cdev
;
183 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
184 unsigned int offset
, unsigned int size
);
185 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
);
186 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
);
187 static void mon_free_buff(struct mon_pgmap
*map
, int npages
);
190 * This is a "chunked memcpy". It does not manipulate any counters.
191 * But it returns the new offset for repeated application.
193 unsigned int mon_copy_to_buff(const struct mon_reader_bin
*this,
194 unsigned int off
, const unsigned char *from
, unsigned int length
)
196 unsigned int step_len
;
198 unsigned int in_page
;
202 * Determine step_len.
205 in_page
= CHUNK_SIZE
- (off
& (CHUNK_SIZE
-1));
206 if (in_page
< step_len
)
210 * Copy data and advance pointers.
212 buf
= this->b_vec
[off
/ CHUNK_SIZE
].ptr
+ off
% CHUNK_SIZE
;
213 memcpy(buf
, from
, step_len
);
214 if ((off
+= step_len
) >= this->b_size
) off
= 0;
222 * This is a little worse than the above because it's "chunked copy_to_user".
223 * The return value is an error code, not an offset.
225 static int copy_from_buf(const struct mon_reader_bin
*this, unsigned int off
,
226 char __user
*to
, 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 if (copy_to_user(to
, buf
, step_len
))
247 if ((off
+= step_len
) >= this->b_size
) off
= 0;
255 * Allocate an (aligned) area in the buffer.
256 * This is called under b_lock.
257 * Returns ~0 on failure.
259 static unsigned int mon_buff_area_alloc(struct mon_reader_bin
*rp
,
264 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
265 if (rp
->b_cnt
+ size
> rp
->b_size
)
269 if ((rp
->b_in
+= size
) >= rp
->b_size
)
270 rp
->b_in
-= rp
->b_size
;
275 * This is the same thing as mon_buff_area_alloc, only it does not allow
276 * buffers to wrap. This is needed by applications which pass references
277 * into mmap-ed buffers up their stacks (libpcap can do that).
279 * Currently, we always have the header stuck with the data, although
280 * it is not strictly speaking necessary.
282 * When a buffer would wrap, we place a filler packet to mark the space.
284 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin
*rp
,
288 unsigned int fill_size
;
290 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
291 if (rp
->b_cnt
+ size
> rp
->b_size
)
293 if (rp
->b_in
+ size
> rp
->b_size
) {
295 * This would wrap. Find if we still have space after
296 * skipping to the end of the buffer. If we do, place
297 * a filler packet and allocate a new packet.
299 fill_size
= rp
->b_size
- rp
->b_in
;
300 if (rp
->b_cnt
+ size
+ fill_size
> rp
->b_size
)
302 mon_buff_area_fill(rp
, rp
->b_in
, fill_size
);
306 rp
->b_cnt
+= size
+ fill_size
;
307 } else if (rp
->b_in
+ size
== rp
->b_size
) {
320 * Return a few (kilo-)bytes to the head of the buffer.
321 * This is used if a DMA fetch fails.
323 static void mon_buff_area_shrink(struct mon_reader_bin
*rp
, unsigned int size
)
326 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
329 rp
->b_in
+= rp
->b_size
;
334 * This has to be called under both b_lock and fetch_lock, because
335 * it accesses both b_cnt and b_out.
337 static void mon_buff_area_free(struct mon_reader_bin
*rp
, unsigned int size
)
340 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
342 if ((rp
->b_out
+= size
) >= rp
->b_size
)
343 rp
->b_out
-= rp
->b_size
;
346 static void mon_buff_area_fill(const struct mon_reader_bin
*rp
,
347 unsigned int offset
, unsigned int size
)
349 struct mon_bin_hdr
*ep
;
351 ep
= MON_OFF2HDR(rp
, offset
);
352 memset(ep
, 0, PKT_SIZE
);
354 ep
->len_cap
= size
- PKT_SIZE
;
357 static inline char mon_bin_get_setup(unsigned char *setupb
,
358 const struct urb
*urb
, char ev_type
)
361 if (!usb_endpoint_xfer_control(&urb
->ep
->desc
) || ev_type
!= 'S')
364 if (urb
->dev
->bus
->uses_dma
&&
365 (urb
->transfer_flags
& URB_NO_SETUP_DMA_MAP
)) {
366 return mon_dmapeek(setupb
, urb
->setup_dma
, SETUP_LEN
);
368 if (urb
->setup_packet
== NULL
)
371 memcpy(setupb
, urb
->setup_packet
, SETUP_LEN
);
375 static char mon_bin_get_data(const struct mon_reader_bin
*rp
,
376 unsigned int offset
, struct urb
*urb
, unsigned int length
)
379 if (urb
->dev
->bus
->uses_dma
&&
380 (urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)) {
381 mon_dmapeek_vec(rp
, offset
, urb
->transfer_dma
, length
);
385 if (urb
->transfer_buffer
== NULL
)
388 mon_copy_to_buff(rp
, offset
, urb
->transfer_buffer
, length
);
392 static void mon_bin_event(struct mon_reader_bin
*rp
, struct urb
*urb
,
395 const struct usb_endpoint_descriptor
*epd
= &urb
->ep
->desc
;
398 unsigned int urb_length
;
402 struct mon_bin_hdr
*ep
;
405 do_gettimeofday(&ts
);
407 spin_lock_irqsave(&rp
->b_lock
, flags
);
410 * Find the maximum allowable length, then allocate space.
412 urb_length
= (ev_type
== 'S') ?
413 urb
->transfer_buffer_length
: urb
->actual_length
;
416 if (length
>= rp
->b_size
/5)
417 length
= rp
->b_size
/5;
419 if (usb_urb_dir_in(urb
)) {
420 if (ev_type
== 'S') {
424 /* Cannot rely on endpoint number in case of control ep.0 */
427 if (ev_type
== 'C') {
435 offset
= mon_buff_area_alloc_contiguous(rp
, length
+ PKT_SIZE
);
437 offset
= mon_buff_area_alloc(rp
, length
+ PKT_SIZE
);
440 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
444 ep
= MON_OFF2HDR(rp
, offset
);
445 if ((offset
+= PKT_SIZE
) >= rp
->b_size
) offset
= 0;
448 * Fill the allocated area.
450 memset(ep
, 0, PKT_SIZE
);
452 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(epd
)];
453 ep
->epnum
= dir
| usb_endpoint_num(epd
);
454 ep
->devnum
= urb
->dev
->devnum
;
455 ep
->busnum
= urb
->dev
->bus
->busnum
;
456 ep
->id
= (unsigned long) urb
;
457 ep
->ts_sec
= ts
.tv_sec
;
458 ep
->ts_usec
= ts
.tv_usec
;
459 ep
->status
= urb
->status
;
460 ep
->len_urb
= urb_length
;
461 ep
->len_cap
= length
;
463 ep
->flag_setup
= mon_bin_get_setup(ep
->setup
, urb
, ev_type
);
465 ep
->flag_data
= mon_bin_get_data(rp
, offset
, urb
, length
);
466 if (ep
->flag_data
!= 0) { /* Yes, it's 0x00, not '0' */
468 mon_buff_area_shrink(rp
, length
);
471 ep
->flag_data
= data_tag
;
474 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
476 wake_up(&rp
->b_wait
);
479 static void mon_bin_submit(void *data
, struct urb
*urb
)
481 struct mon_reader_bin
*rp
= data
;
482 mon_bin_event(rp
, urb
, 'S');
485 static void mon_bin_complete(void *data
, struct urb
*urb
)
487 struct mon_reader_bin
*rp
= data
;
488 mon_bin_event(rp
, urb
, 'C');
491 static void mon_bin_error(void *data
, struct urb
*urb
, int error
)
493 struct mon_reader_bin
*rp
= data
;
496 struct mon_bin_hdr
*ep
;
498 spin_lock_irqsave(&rp
->b_lock
, flags
);
500 offset
= mon_buff_area_alloc(rp
, PKT_SIZE
);
502 /* Not incrementing cnt_lost. Just because. */
503 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
507 ep
= MON_OFF2HDR(rp
, offset
);
509 memset(ep
, 0, PKT_SIZE
);
511 ep
->xfer_type
= xfer_to_pipe
[usb_endpoint_type(&urb
->ep
->desc
)];
512 ep
->epnum
= usb_urb_dir_in(urb
) ? USB_DIR_IN
: 0;
513 ep
->epnum
|= usb_endpoint_num(&urb
->ep
->desc
);
514 ep
->devnum
= urb
->dev
->devnum
;
515 ep
->busnum
= urb
->dev
->bus
->busnum
;
516 ep
->id
= (unsigned long) urb
;
519 ep
->flag_setup
= '-';
522 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
524 wake_up(&rp
->b_wait
);
527 static int mon_bin_open(struct inode
*inode
, struct file
*file
)
529 struct mon_bus
*mbus
;
530 struct mon_reader_bin
*rp
;
534 mutex_lock(&mon_lock
);
535 if ((mbus
= mon_bus_lookup(iminor(inode
))) == NULL
) {
536 mutex_unlock(&mon_lock
);
539 if (mbus
!= &mon_bus0
&& mbus
->u_bus
== NULL
) {
540 printk(KERN_ERR TAG
": consistency error on open\n");
541 mutex_unlock(&mon_lock
);
545 rp
= kzalloc(sizeof(struct mon_reader_bin
), GFP_KERNEL
);
550 spin_lock_init(&rp
->b_lock
);
551 init_waitqueue_head(&rp
->b_wait
);
552 mutex_init(&rp
->fetch_lock
);
554 rp
->b_size
= BUFF_DFL
;
556 size
= sizeof(struct mon_pgmap
) * (rp
->b_size
/CHUNK_SIZE
);
557 if ((rp
->b_vec
= kzalloc(size
, GFP_KERNEL
)) == NULL
) {
562 if ((rc
= mon_alloc_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
)) < 0)
567 rp
->r
.rnf_submit
= mon_bin_submit
;
568 rp
->r
.rnf_error
= mon_bin_error
;
569 rp
->r
.rnf_complete
= mon_bin_complete
;
571 mon_reader_add(mbus
, &rp
->r
);
573 file
->private_data
= rp
;
574 mutex_unlock(&mon_lock
);
582 mutex_unlock(&mon_lock
);
587 * Extract an event from buffer and copy it to user space.
588 * Wait if there is no event ready.
589 * Returns zero or error.
591 static int mon_bin_get_event(struct file
*file
, struct mon_reader_bin
*rp
,
592 struct mon_bin_hdr __user
*hdr
, void __user
*data
, unsigned int nbytes
)
595 struct mon_bin_hdr
*ep
;
600 mutex_lock(&rp
->fetch_lock
);
602 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
603 mutex_unlock(&rp
->fetch_lock
);
607 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
609 if (copy_to_user(hdr
, ep
, sizeof(struct mon_bin_hdr
))) {
610 mutex_unlock(&rp
->fetch_lock
);
614 step_len
= min(ep
->len_cap
, nbytes
);
615 if ((offset
= rp
->b_out
+ PKT_SIZE
) >= rp
->b_size
) offset
= 0;
617 if (copy_from_buf(rp
, offset
, data
, step_len
)) {
618 mutex_unlock(&rp
->fetch_lock
);
622 spin_lock_irqsave(&rp
->b_lock
, flags
);
623 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
624 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
627 mutex_unlock(&rp
->fetch_lock
);
631 static int mon_bin_release(struct inode
*inode
, struct file
*file
)
633 struct mon_reader_bin
*rp
= file
->private_data
;
634 struct mon_bus
* mbus
= rp
->r
.m_bus
;
636 mutex_lock(&mon_lock
);
638 if (mbus
->nreaders
<= 0) {
639 printk(KERN_ERR TAG
": consistency error on close\n");
640 mutex_unlock(&mon_lock
);
643 mon_reader_del(mbus
, &rp
->r
);
645 mon_free_buff(rp
->b_vec
, rp
->b_size
/CHUNK_SIZE
);
649 mutex_unlock(&mon_lock
);
653 static ssize_t
mon_bin_read(struct file
*file
, char __user
*buf
,
654 size_t nbytes
, loff_t
*ppos
)
656 struct mon_reader_bin
*rp
= file
->private_data
;
658 struct mon_bin_hdr
*ep
;
665 mutex_lock(&rp
->fetch_lock
);
667 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
668 mutex_unlock(&rp
->fetch_lock
);
672 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
674 if (rp
->b_read
< sizeof(struct mon_bin_hdr
)) {
675 step_len
= min(nbytes
, sizeof(struct mon_bin_hdr
) - rp
->b_read
);
676 ptr
= ((char *)ep
) + rp
->b_read
;
677 if (step_len
&& copy_to_user(buf
, ptr
, step_len
)) {
678 mutex_unlock(&rp
->fetch_lock
);
683 rp
->b_read
+= step_len
;
687 if (rp
->b_read
>= sizeof(struct mon_bin_hdr
)) {
688 step_len
= min(nbytes
, (size_t)ep
->len_cap
);
689 offset
= rp
->b_out
+ PKT_SIZE
;
690 offset
+= rp
->b_read
- sizeof(struct mon_bin_hdr
);
691 if (offset
>= rp
->b_size
)
692 offset
-= rp
->b_size
;
693 if (copy_from_buf(rp
, offset
, buf
, step_len
)) {
694 mutex_unlock(&rp
->fetch_lock
);
699 rp
->b_read
+= step_len
;
704 * Check if whole packet was read, and if so, jump to the next one.
706 if (rp
->b_read
>= sizeof(struct mon_bin_hdr
) + ep
->len_cap
) {
707 spin_lock_irqsave(&rp
->b_lock
, flags
);
708 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
709 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
713 mutex_unlock(&rp
->fetch_lock
);
718 * Remove at most nevents from chunked buffer.
719 * Returns the number of removed events.
721 static int mon_bin_flush(struct mon_reader_bin
*rp
, unsigned nevents
)
724 struct mon_bin_hdr
*ep
;
727 mutex_lock(&rp
->fetch_lock
);
728 spin_lock_irqsave(&rp
->b_lock
, flags
);
729 for (i
= 0; i
< nevents
; ++i
) {
730 if (MON_RING_EMPTY(rp
))
733 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
734 mon_buff_area_free(rp
, PKT_SIZE
+ ep
->len_cap
);
736 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
738 mutex_unlock(&rp
->fetch_lock
);
743 * Fetch at most max event offsets into the buffer and put them into vec.
744 * The events are usually freed later with mon_bin_flush.
745 * Return the effective number of events fetched.
747 static int mon_bin_fetch(struct file
*file
, struct mon_reader_bin
*rp
,
748 u32 __user
*vec
, unsigned int max
)
750 unsigned int cur_out
;
751 unsigned int bytes
, avail
;
753 unsigned int nevents
;
754 struct mon_bin_hdr
*ep
;
758 mutex_lock(&rp
->fetch_lock
);
760 if ((rc
= mon_bin_wait_event(file
, rp
)) < 0) {
761 mutex_unlock(&rp
->fetch_lock
);
765 spin_lock_irqsave(&rp
->b_lock
, flags
);
767 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
772 while (bytes
< avail
) {
776 ep
= MON_OFF2HDR(rp
, cur_out
);
777 if (put_user(cur_out
, &vec
[nevents
])) {
778 mutex_unlock(&rp
->fetch_lock
);
783 size
= ep
->len_cap
+ PKT_SIZE
;
784 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
785 if ((cur_out
+= size
) >= rp
->b_size
)
786 cur_out
-= rp
->b_size
;
790 mutex_unlock(&rp
->fetch_lock
);
795 * Count events. This is almost the same as the above mon_bin_fetch,
796 * only we do not store offsets into user vector, and we have no limit.
798 static int mon_bin_queued(struct mon_reader_bin
*rp
)
800 unsigned int cur_out
;
801 unsigned int bytes
, avail
;
803 unsigned int nevents
;
804 struct mon_bin_hdr
*ep
;
807 mutex_lock(&rp
->fetch_lock
);
809 spin_lock_irqsave(&rp
->b_lock
, flags
);
811 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
816 while (bytes
< avail
) {
817 ep
= MON_OFF2HDR(rp
, cur_out
);
820 size
= ep
->len_cap
+ PKT_SIZE
;
821 size
= (size
+ PKT_ALIGN
-1) & ~(PKT_ALIGN
-1);
822 if ((cur_out
+= size
) >= rp
->b_size
)
823 cur_out
-= rp
->b_size
;
827 mutex_unlock(&rp
->fetch_lock
);
833 static int mon_bin_ioctl(struct inode
*inode
, struct file
*file
,
834 unsigned int cmd
, unsigned long arg
)
836 struct mon_reader_bin
*rp
= file
->private_data
;
837 // struct mon_bus* mbus = rp->r.m_bus;
839 struct mon_bin_hdr
*ep
;
844 case MON_IOCQ_URB_LEN
:
846 * N.B. This only returns the size of data, without the header.
848 spin_lock_irqsave(&rp
->b_lock
, flags
);
849 if (!MON_RING_EMPTY(rp
)) {
850 ep
= MON_OFF2HDR(rp
, rp
->b_out
);
853 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
856 case MON_IOCQ_RING_SIZE
:
860 case MON_IOCT_RING_SIZE
:
862 * Changing the buffer size will flush it's contents; the new
863 * buffer is allocated before releasing the old one to be sure
864 * the device will stay functional also in case of memory
869 struct mon_pgmap
*vec
;
871 if (arg
< BUFF_MIN
|| arg
> BUFF_MAX
)
874 size
= CHUNK_ALIGN(arg
);
875 if ((vec
= kzalloc(sizeof(struct mon_pgmap
) * (size
/CHUNK_SIZE
),
876 GFP_KERNEL
)) == NULL
) {
881 ret
= mon_alloc_buff(vec
, size
/CHUNK_SIZE
);
887 mutex_lock(&rp
->fetch_lock
);
888 spin_lock_irqsave(&rp
->b_lock
, flags
);
889 mon_free_buff(rp
->b_vec
, size
/CHUNK_SIZE
);
893 rp
->b_read
= rp
->b_in
= rp
->b_out
= rp
->b_cnt
= 0;
895 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
896 mutex_unlock(&rp
->fetch_lock
);
900 case MON_IOCH_MFLUSH
:
901 ret
= mon_bin_flush(rp
, arg
);
906 struct mon_bin_get getb
;
908 if (copy_from_user(&getb
, (void __user
*)arg
,
909 sizeof(struct mon_bin_get
)))
912 if (getb
.alloc
> 0x10000000) /* Want to cast to u32 */
914 ret
= mon_bin_get_event(file
, rp
,
915 getb
.hdr
, getb
.data
, (unsigned int)getb
.alloc
);
920 case MON_IOCX_GET32
: {
921 struct mon_bin_get32 getb
;
923 if (copy_from_user(&getb
, (void __user
*)arg
,
924 sizeof(struct mon_bin_get32
)))
927 ret
= mon_bin_get_event(file
, rp
,
928 compat_ptr(getb
.hdr32
), compat_ptr(getb
.data32
),
934 case MON_IOCX_MFETCH
:
936 struct mon_bin_mfetch mfetch
;
937 struct mon_bin_mfetch __user
*uptr
;
939 uptr
= (struct mon_bin_mfetch __user
*)arg
;
941 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
945 ret
= mon_bin_flush(rp
, mfetch
.nflush
);
948 if (put_user(ret
, &uptr
->nflush
))
951 ret
= mon_bin_fetch(file
, rp
, mfetch
.offvec
, mfetch
.nfetch
);
954 if (put_user(ret
, &uptr
->nfetch
))
961 case MON_IOCX_MFETCH32
:
963 struct mon_bin_mfetch32 mfetch
;
964 struct mon_bin_mfetch32 __user
*uptr
;
966 uptr
= (struct mon_bin_mfetch32 __user
*) compat_ptr(arg
);
968 if (copy_from_user(&mfetch
, uptr
, sizeof(mfetch
)))
971 if (mfetch
.nflush32
) {
972 ret
= mon_bin_flush(rp
, mfetch
.nflush32
);
975 if (put_user(ret
, &uptr
->nflush32
))
978 ret
= mon_bin_fetch(file
, rp
, compat_ptr(mfetch
.offvec32
),
982 if (put_user(ret
, &uptr
->nfetch32
))
989 case MON_IOCG_STATS
: {
990 struct mon_bin_stats __user
*sp
;
991 unsigned int nevents
;
992 unsigned int ndropped
;
994 spin_lock_irqsave(&rp
->b_lock
, flags
);
995 ndropped
= rp
->cnt_lost
;
997 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
998 nevents
= mon_bin_queued(rp
);
1000 sp
= (struct mon_bin_stats __user
*)arg
;
1001 if (put_user(rp
->cnt_lost
, &sp
->dropped
))
1003 if (put_user(nevents
, &sp
->queued
))
1017 mon_bin_poll(struct file
*file
, struct poll_table_struct
*wait
)
1019 struct mon_reader_bin
*rp
= file
->private_data
;
1020 unsigned int mask
= 0;
1021 unsigned long flags
;
1023 if (file
->f_mode
& FMODE_READ
)
1024 poll_wait(file
, &rp
->b_wait
, wait
);
1026 spin_lock_irqsave(&rp
->b_lock
, flags
);
1027 if (!MON_RING_EMPTY(rp
))
1028 mask
|= POLLIN
| POLLRDNORM
; /* readable */
1029 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1034 * open and close: just keep track of how many times the device is
1035 * mapped, to use the proper memory allocation function.
1037 static void mon_bin_vma_open(struct vm_area_struct
*vma
)
1039 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1043 static void mon_bin_vma_close(struct vm_area_struct
*vma
)
1045 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1050 * Map ring pages to user space.
1052 struct page
*mon_bin_vma_nopage(struct vm_area_struct
*vma
,
1053 unsigned long address
, int *type
)
1055 struct mon_reader_bin
*rp
= vma
->vm_private_data
;
1056 unsigned long offset
, chunk_idx
;
1057 struct page
*pageptr
;
1059 offset
= (address
- vma
->vm_start
) + (vma
->vm_pgoff
<< PAGE_SHIFT
);
1060 if (offset
>= rp
->b_size
)
1061 return NOPAGE_SIGBUS
;
1062 chunk_idx
= offset
/ CHUNK_SIZE
;
1063 pageptr
= rp
->b_vec
[chunk_idx
].pg
;
1066 *type
= VM_FAULT_MINOR
;
1070 struct vm_operations_struct mon_bin_vm_ops
= {
1071 .open
= mon_bin_vma_open
,
1072 .close
= mon_bin_vma_close
,
1073 .nopage
= mon_bin_vma_nopage
,
1076 int mon_bin_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1078 /* don't do anything here: "nopage" will set up page table entries */
1079 vma
->vm_ops
= &mon_bin_vm_ops
;
1080 vma
->vm_flags
|= VM_RESERVED
;
1081 vma
->vm_private_data
= filp
->private_data
;
1082 mon_bin_vma_open(vma
);
1086 struct file_operations mon_fops_binary
= {
1087 .owner
= THIS_MODULE
,
1088 .open
= mon_bin_open
,
1089 .llseek
= no_llseek
,
1090 .read
= mon_bin_read
,
1091 /* .write = mon_text_write, */
1092 .poll
= mon_bin_poll
,
1093 .ioctl
= mon_bin_ioctl
,
1094 .release
= mon_bin_release
,
1097 static int mon_bin_wait_event(struct file
*file
, struct mon_reader_bin
*rp
)
1099 DECLARE_WAITQUEUE(waita
, current
);
1100 unsigned long flags
;
1102 add_wait_queue(&rp
->b_wait
, &waita
);
1103 set_current_state(TASK_INTERRUPTIBLE
);
1105 spin_lock_irqsave(&rp
->b_lock
, flags
);
1106 while (MON_RING_EMPTY(rp
)) {
1107 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1109 if (file
->f_flags
& O_NONBLOCK
) {
1110 set_current_state(TASK_RUNNING
);
1111 remove_wait_queue(&rp
->b_wait
, &waita
);
1112 return -EWOULDBLOCK
; /* Same as EAGAIN in Linux */
1115 if (signal_pending(current
)) {
1116 remove_wait_queue(&rp
->b_wait
, &waita
);
1119 set_current_state(TASK_INTERRUPTIBLE
);
1121 spin_lock_irqsave(&rp
->b_lock
, flags
);
1123 spin_unlock_irqrestore(&rp
->b_lock
, flags
);
1125 set_current_state(TASK_RUNNING
);
1126 remove_wait_queue(&rp
->b_wait
, &waita
);
1130 static int mon_alloc_buff(struct mon_pgmap
*map
, int npages
)
1133 unsigned long vaddr
;
1135 for (n
= 0; n
< npages
; n
++) {
1136 vaddr
= get_zeroed_page(GFP_KERNEL
);
1139 free_page((unsigned long) map
[n
].ptr
);
1142 map
[n
].ptr
= (unsigned char *) vaddr
;
1143 map
[n
].pg
= virt_to_page(vaddr
);
1148 static void mon_free_buff(struct mon_pgmap
*map
, int npages
)
1152 for (n
= 0; n
< npages
; n
++)
1153 free_page((unsigned long) map
[n
].ptr
);
1156 int mon_bin_add(struct mon_bus
*mbus
, const struct usb_bus
*ubus
)
1159 unsigned minor
= ubus
? ubus
->busnum
: 0;
1161 if (minor
>= MON_BIN_MAX_MINOR
)
1164 dev
= device_create(mon_bin_class
, ubus
? ubus
->controller
: NULL
,
1165 MKDEV(MAJOR(mon_bin_dev0
), minor
), "usbmon%d", minor
);
1169 mbus
->classdev
= dev
;
1173 void mon_bin_del(struct mon_bus
*mbus
)
1175 device_destroy(mon_bin_class
, mbus
->classdev
->devt
);
1178 int __init
mon_bin_init(void)
1182 mon_bin_class
= class_create(THIS_MODULE
, "usbmon");
1183 if (IS_ERR(mon_bin_class
)) {
1184 rc
= PTR_ERR(mon_bin_class
);
1188 rc
= alloc_chrdev_region(&mon_bin_dev0
, 0, MON_BIN_MAX_MINOR
, "usbmon");
1192 cdev_init(&mon_bin_cdev
, &mon_fops_binary
);
1193 mon_bin_cdev
.owner
= THIS_MODULE
;
1195 rc
= cdev_add(&mon_bin_cdev
, mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1202 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1204 class_destroy(mon_bin_class
);
1209 void mon_bin_exit(void)
1211 cdev_del(&mon_bin_cdev
);
1212 unregister_chrdev_region(mon_bin_dev0
, MON_BIN_MAX_MINOR
);
1213 class_destroy(mon_bin_class
);