iwlwifi: work around bogus active chains detection
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / mon / mon_bin.c
blob385ec052016705386e4f10fa8bffc51a44dec9e9
1 /*
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)
8 */
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/fs.h>
13 #include <linux/cdev.h>
14 #include <linux/usb.h>
15 #include <linux/poll.h>
16 #include <linux/compat.h>
17 #include <linux/mm.h>
18 #include <linux/smp_lock.h>
19 #include <linux/scatterlist.h>
21 #include <asm/uaccess.h>
23 #include "usb_mon.h"
26 * Defined by USB 2.0 clause 9.3, table 9.2.
28 #define SETUP_LEN 8
30 /* ioctl macros */
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)
44 #ifdef CONFIG_COMPAT
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)
48 #endif
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.
85 struct mon_bin_hdr {
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 */
92 char flag_setup;
93 char flag_data;
94 s64 ts_sec; /* gettimeofday */
95 s32 ts_usec; /* gettimeofday */
96 int status;
97 unsigned int len_urb; /* Length of data (submitted or actual) */
98 unsigned int len_cap; /* Delivered length */
99 union {
100 unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
101 struct iso_rec {
102 int error_count;
103 int numdesc;
104 } iso;
105 } s;
106 int interval;
107 int start_frame;
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 {
118 int iso_status;
119 unsigned int iso_off;
120 unsigned int iso_len;
121 u32 _pad;
124 /* per file statistic */
125 struct mon_bin_stats {
126 u32 queued;
127 u32 dropped;
130 struct mon_bin_get {
131 struct mon_bin_hdr __user *hdr; /* Can be 48 bytes or 64. */
132 void __user *data;
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 */
142 #ifdef CONFIG_COMPAT
143 struct mon_bin_get32 {
144 u32 hdr32;
145 u32 data32;
146 u32 alloc32;
149 struct mon_bin_mfetch32 {
150 u32 offvec32;
151 u32 nfetch32;
152 u32 nflush32;
154 #endif
156 /* Having these two values same prevents wrapping of the mon_bin_hdr */
157 #define PKT_ALIGN 64
158 #define PKT_SIZE 64
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.
171 struct mon_pgmap {
172 struct page *pg;
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 */
190 int mmap_active;
192 /* A list of these is needed for "bus 0". Some time later. */
193 struct mon_reader r;
195 /* Stats */
196 unsigned int cnt_lost;
199 static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
200 unsigned int offset)
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;
229 unsigned char *buf;
230 unsigned int in_page;
232 while (length) {
234 * Determine step_len.
236 step_len = length;
237 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
238 if (in_page < step_len)
239 step_len = in_page;
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;
247 from += step_len;
248 length -= step_len;
250 return off;
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;
261 unsigned char *buf;
262 unsigned int in_page;
264 while (length) {
266 * Determine step_len.
268 step_len = length;
269 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
270 if (in_page < step_len)
271 step_len = in_page;
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))
278 return -EINVAL;
279 if ((off += step_len) >= this->b_size) off = 0;
280 to += step_len;
281 length -= step_len;
283 return 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,
292 unsigned int size)
294 unsigned int offset;
296 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
297 if (rp->b_cnt + size > rp->b_size)
298 return ~0;
299 offset = rp->b_in;
300 rp->b_cnt += size;
301 if ((rp->b_in += size) >= rp->b_size)
302 rp->b_in -= rp->b_size;
303 return offset;
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,
317 unsigned int size)
319 unsigned int offset;
320 unsigned int fill_size;
322 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
323 if (rp->b_cnt + size > rp->b_size)
324 return ~0;
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)
333 return ~0;
334 mon_buff_area_fill(rp, rp->b_in, fill_size);
336 offset = 0;
337 rp->b_in = size;
338 rp->b_cnt += size + fill_size;
339 } else if (rp->b_in + size == rp->b_size) {
340 offset = rp->b_in;
341 rp->b_in = 0;
342 rp->b_cnt += size;
343 } else {
344 offset = rp->b_in;
345 rp->b_in += size;
346 rp->b_cnt += size;
348 return offset;
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 */
359 rp->b_cnt -= size;
360 if (rp->b_in < size)
361 rp->b_in += rp->b_size;
362 rp->b_in -= 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);
373 rp->b_cnt -= size;
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);
385 ep->type = '@';
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)
394 return 'Z';
395 memcpy(setupb, urb->setup_packet, SETUP_LEN);
396 return 0;
399 static unsigned int mon_bin_get_data(const struct mon_reader_bin *rp,
400 unsigned int offset, struct urb *urb, unsigned int length,
401 char *flag)
403 int i;
404 struct scatterlist *sg;
405 unsigned int this_len;
407 *flag = 0;
408 if (urb->num_sgs == 0) {
409 if (urb->transfer_buffer == NULL) {
410 *flag = 'Z';
411 return length;
413 mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
414 length = 0;
416 } else {
417 /* If IOMMU coalescing occurred, we cannot trust sg_page */
418 if (urb->sg->nents != urb->num_sgs) {
419 *flag = 'D';
420 return length;
423 /* Copy up to the first non-addressable segment */
424 for_each_sg(urb->sg->sg, sg, urb->num_sgs, i) {
425 if (length == 0 || PageHighMem(sg_page(sg)))
426 break;
427 this_len = min_t(unsigned int, sg->length, length);
428 offset = mon_copy_to_buff(rp, offset, sg_virt(sg),
429 this_len);
430 length -= this_len;
432 if (i == 0)
433 *flag = 'D';
436 return length;
439 static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
440 unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
442 struct mon_bin_isodesc *dp;
443 struct usb_iso_packet_descriptor *fp;
445 fp = urb->iso_frame_desc;
446 while (ndesc-- != 0) {
447 dp = (struct mon_bin_isodesc *)
448 (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
449 dp->iso_status = fp->status;
450 dp->iso_off = fp->offset;
451 dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
452 dp->_pad = 0;
453 if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
454 offset = 0;
455 fp++;
459 static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
460 char ev_type, int status)
462 const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
463 unsigned long flags;
464 struct timeval ts;
465 unsigned int urb_length;
466 unsigned int offset;
467 unsigned int length;
468 unsigned int delta;
469 unsigned int ndesc, lendesc;
470 unsigned char dir;
471 struct mon_bin_hdr *ep;
472 char data_tag = 0;
474 do_gettimeofday(&ts);
476 spin_lock_irqsave(&rp->b_lock, flags);
479 * Find the maximum allowable length, then allocate space.
481 if (usb_endpoint_xfer_isoc(epd)) {
482 if (urb->number_of_packets < 0) {
483 ndesc = 0;
484 } else if (urb->number_of_packets >= ISODESC_MAX) {
485 ndesc = ISODESC_MAX;
486 } else {
487 ndesc = urb->number_of_packets;
489 } else {
490 ndesc = 0;
492 lendesc = ndesc*sizeof(struct mon_bin_isodesc);
494 urb_length = (ev_type == 'S') ?
495 urb->transfer_buffer_length : urb->actual_length;
496 length = urb_length;
498 if (length >= rp->b_size/5)
499 length = rp->b_size/5;
501 if (usb_urb_dir_in(urb)) {
502 if (ev_type == 'S') {
503 length = 0;
504 data_tag = '<';
506 /* Cannot rely on endpoint number in case of control ep.0 */
507 dir = USB_DIR_IN;
508 } else {
509 if (ev_type == 'C') {
510 length = 0;
511 data_tag = '>';
513 dir = 0;
516 if (rp->mmap_active) {
517 offset = mon_buff_area_alloc_contiguous(rp,
518 length + PKT_SIZE + lendesc);
519 } else {
520 offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
522 if (offset == ~0) {
523 rp->cnt_lost++;
524 spin_unlock_irqrestore(&rp->b_lock, flags);
525 return;
528 ep = MON_OFF2HDR(rp, offset);
529 if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
532 * Fill the allocated area.
534 memset(ep, 0, PKT_SIZE);
535 ep->type = ev_type;
536 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
537 ep->epnum = dir | usb_endpoint_num(epd);
538 ep->devnum = urb->dev->devnum;
539 ep->busnum = urb->dev->bus->busnum;
540 ep->id = (unsigned long) urb;
541 ep->ts_sec = ts.tv_sec;
542 ep->ts_usec = ts.tv_usec;
543 ep->status = status;
544 ep->len_urb = urb_length;
545 ep->len_cap = length + lendesc;
546 ep->xfer_flags = urb->transfer_flags;
548 if (usb_endpoint_xfer_int(epd)) {
549 ep->interval = urb->interval;
550 } else if (usb_endpoint_xfer_isoc(epd)) {
551 ep->interval = urb->interval;
552 ep->start_frame = urb->start_frame;
553 ep->s.iso.error_count = urb->error_count;
554 ep->s.iso.numdesc = urb->number_of_packets;
557 if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
558 ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
559 } else {
560 ep->flag_setup = '-';
563 if (ndesc != 0) {
564 ep->ndesc = ndesc;
565 mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
566 if ((offset += lendesc) >= rp->b_size)
567 offset -= rp->b_size;
570 if (length != 0) {
571 length = mon_bin_get_data(rp, offset, urb, length,
572 &ep->flag_data);
573 if (length > 0) {
574 delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
575 ep->len_cap -= length;
576 delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
577 mon_buff_area_shrink(rp, delta);
579 } else {
580 ep->flag_data = data_tag;
583 spin_unlock_irqrestore(&rp->b_lock, flags);
585 wake_up(&rp->b_wait);
588 static void mon_bin_submit(void *data, struct urb *urb)
590 struct mon_reader_bin *rp = data;
591 mon_bin_event(rp, urb, 'S', -EINPROGRESS);
594 static void mon_bin_complete(void *data, struct urb *urb, int status)
596 struct mon_reader_bin *rp = data;
597 mon_bin_event(rp, urb, 'C', status);
600 static void mon_bin_error(void *data, struct urb *urb, int error)
602 struct mon_reader_bin *rp = data;
603 unsigned long flags;
604 unsigned int offset;
605 struct mon_bin_hdr *ep;
607 spin_lock_irqsave(&rp->b_lock, flags);
609 offset = mon_buff_area_alloc(rp, PKT_SIZE);
610 if (offset == ~0) {
611 /* Not incrementing cnt_lost. Just because. */
612 spin_unlock_irqrestore(&rp->b_lock, flags);
613 return;
616 ep = MON_OFF2HDR(rp, offset);
618 memset(ep, 0, PKT_SIZE);
619 ep->type = 'E';
620 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
621 ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
622 ep->epnum |= usb_endpoint_num(&urb->ep->desc);
623 ep->devnum = urb->dev->devnum;
624 ep->busnum = urb->dev->bus->busnum;
625 ep->id = (unsigned long) urb;
626 ep->status = error;
628 ep->flag_setup = '-';
629 ep->flag_data = 'E';
631 spin_unlock_irqrestore(&rp->b_lock, flags);
633 wake_up(&rp->b_wait);
636 static int mon_bin_open(struct inode *inode, struct file *file)
638 struct mon_bus *mbus;
639 struct mon_reader_bin *rp;
640 size_t size;
641 int rc;
643 lock_kernel();
644 mutex_lock(&mon_lock);
645 if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) {
646 mutex_unlock(&mon_lock);
647 unlock_kernel();
648 return -ENODEV;
650 if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
651 printk(KERN_ERR TAG ": consistency error on open\n");
652 mutex_unlock(&mon_lock);
653 unlock_kernel();
654 return -ENODEV;
657 rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
658 if (rp == NULL) {
659 rc = -ENOMEM;
660 goto err_alloc;
662 spin_lock_init(&rp->b_lock);
663 init_waitqueue_head(&rp->b_wait);
664 mutex_init(&rp->fetch_lock);
665 rp->b_size = BUFF_DFL;
667 size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
668 if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
669 rc = -ENOMEM;
670 goto err_allocvec;
673 if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
674 goto err_allocbuff;
676 rp->r.m_bus = mbus;
677 rp->r.r_data = rp;
678 rp->r.rnf_submit = mon_bin_submit;
679 rp->r.rnf_error = mon_bin_error;
680 rp->r.rnf_complete = mon_bin_complete;
682 mon_reader_add(mbus, &rp->r);
684 file->private_data = rp;
685 mutex_unlock(&mon_lock);
686 unlock_kernel();
687 return 0;
689 err_allocbuff:
690 kfree(rp->b_vec);
691 err_allocvec:
692 kfree(rp);
693 err_alloc:
694 mutex_unlock(&mon_lock);
695 unlock_kernel();
696 return rc;
700 * Extract an event from buffer and copy it to user space.
701 * Wait if there is no event ready.
702 * Returns zero or error.
704 static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
705 struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
706 void __user *data, unsigned int nbytes)
708 unsigned long flags;
709 struct mon_bin_hdr *ep;
710 size_t step_len;
711 unsigned int offset;
712 int rc;
714 mutex_lock(&rp->fetch_lock);
716 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
717 mutex_unlock(&rp->fetch_lock);
718 return rc;
721 ep = MON_OFF2HDR(rp, rp->b_out);
723 if (copy_to_user(hdr, ep, hdrbytes)) {
724 mutex_unlock(&rp->fetch_lock);
725 return -EFAULT;
728 step_len = min(ep->len_cap, nbytes);
729 if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
731 if (copy_from_buf(rp, offset, data, step_len)) {
732 mutex_unlock(&rp->fetch_lock);
733 return -EFAULT;
736 spin_lock_irqsave(&rp->b_lock, flags);
737 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
738 spin_unlock_irqrestore(&rp->b_lock, flags);
739 rp->b_read = 0;
741 mutex_unlock(&rp->fetch_lock);
742 return 0;
745 static int mon_bin_release(struct inode *inode, struct file *file)
747 struct mon_reader_bin *rp = file->private_data;
748 struct mon_bus* mbus = rp->r.m_bus;
750 mutex_lock(&mon_lock);
752 if (mbus->nreaders <= 0) {
753 printk(KERN_ERR TAG ": consistency error on close\n");
754 mutex_unlock(&mon_lock);
755 return 0;
757 mon_reader_del(mbus, &rp->r);
759 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
760 kfree(rp->b_vec);
761 kfree(rp);
763 mutex_unlock(&mon_lock);
764 return 0;
767 static ssize_t mon_bin_read(struct file *file, char __user *buf,
768 size_t nbytes, loff_t *ppos)
770 struct mon_reader_bin *rp = file->private_data;
771 unsigned int hdrbytes = PKT_SZ_API0;
772 unsigned long flags;
773 struct mon_bin_hdr *ep;
774 unsigned int offset;
775 size_t step_len;
776 char *ptr;
777 ssize_t done = 0;
778 int rc;
780 mutex_lock(&rp->fetch_lock);
782 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
783 mutex_unlock(&rp->fetch_lock);
784 return rc;
787 ep = MON_OFF2HDR(rp, rp->b_out);
789 if (rp->b_read < hdrbytes) {
790 step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
791 ptr = ((char *)ep) + rp->b_read;
792 if (step_len && copy_to_user(buf, ptr, step_len)) {
793 mutex_unlock(&rp->fetch_lock);
794 return -EFAULT;
796 nbytes -= step_len;
797 buf += step_len;
798 rp->b_read += step_len;
799 done += step_len;
802 if (rp->b_read >= hdrbytes) {
803 step_len = ep->len_cap;
804 step_len -= rp->b_read - hdrbytes;
805 if (step_len > nbytes)
806 step_len = nbytes;
807 offset = rp->b_out + PKT_SIZE;
808 offset += rp->b_read - hdrbytes;
809 if (offset >= rp->b_size)
810 offset -= rp->b_size;
811 if (copy_from_buf(rp, offset, buf, step_len)) {
812 mutex_unlock(&rp->fetch_lock);
813 return -EFAULT;
815 nbytes -= step_len;
816 buf += step_len;
817 rp->b_read += step_len;
818 done += step_len;
822 * Check if whole packet was read, and if so, jump to the next one.
824 if (rp->b_read >= hdrbytes + ep->len_cap) {
825 spin_lock_irqsave(&rp->b_lock, flags);
826 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
827 spin_unlock_irqrestore(&rp->b_lock, flags);
828 rp->b_read = 0;
831 mutex_unlock(&rp->fetch_lock);
832 return done;
836 * Remove at most nevents from chunked buffer.
837 * Returns the number of removed events.
839 static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
841 unsigned long flags;
842 struct mon_bin_hdr *ep;
843 int i;
845 mutex_lock(&rp->fetch_lock);
846 spin_lock_irqsave(&rp->b_lock, flags);
847 for (i = 0; i < nevents; ++i) {
848 if (MON_RING_EMPTY(rp))
849 break;
851 ep = MON_OFF2HDR(rp, rp->b_out);
852 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
854 spin_unlock_irqrestore(&rp->b_lock, flags);
855 rp->b_read = 0;
856 mutex_unlock(&rp->fetch_lock);
857 return i;
861 * Fetch at most max event offsets into the buffer and put them into vec.
862 * The events are usually freed later with mon_bin_flush.
863 * Return the effective number of events fetched.
865 static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
866 u32 __user *vec, unsigned int max)
868 unsigned int cur_out;
869 unsigned int bytes, avail;
870 unsigned int size;
871 unsigned int nevents;
872 struct mon_bin_hdr *ep;
873 unsigned long flags;
874 int rc;
876 mutex_lock(&rp->fetch_lock);
878 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
879 mutex_unlock(&rp->fetch_lock);
880 return rc;
883 spin_lock_irqsave(&rp->b_lock, flags);
884 avail = rp->b_cnt;
885 spin_unlock_irqrestore(&rp->b_lock, flags);
887 cur_out = rp->b_out;
888 nevents = 0;
889 bytes = 0;
890 while (bytes < avail) {
891 if (nevents >= max)
892 break;
894 ep = MON_OFF2HDR(rp, cur_out);
895 if (put_user(cur_out, &vec[nevents])) {
896 mutex_unlock(&rp->fetch_lock);
897 return -EFAULT;
900 nevents++;
901 size = ep->len_cap + PKT_SIZE;
902 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
903 if ((cur_out += size) >= rp->b_size)
904 cur_out -= rp->b_size;
905 bytes += size;
908 mutex_unlock(&rp->fetch_lock);
909 return nevents;
913 * Count events. This is almost the same as the above mon_bin_fetch,
914 * only we do not store offsets into user vector, and we have no limit.
916 static int mon_bin_queued(struct mon_reader_bin *rp)
918 unsigned int cur_out;
919 unsigned int bytes, avail;
920 unsigned int size;
921 unsigned int nevents;
922 struct mon_bin_hdr *ep;
923 unsigned long flags;
925 mutex_lock(&rp->fetch_lock);
927 spin_lock_irqsave(&rp->b_lock, flags);
928 avail = rp->b_cnt;
929 spin_unlock_irqrestore(&rp->b_lock, flags);
931 cur_out = rp->b_out;
932 nevents = 0;
933 bytes = 0;
934 while (bytes < avail) {
935 ep = MON_OFF2HDR(rp, cur_out);
937 nevents++;
938 size = ep->len_cap + PKT_SIZE;
939 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
940 if ((cur_out += size) >= rp->b_size)
941 cur_out -= rp->b_size;
942 bytes += size;
945 mutex_unlock(&rp->fetch_lock);
946 return nevents;
951 static int mon_bin_ioctl(struct inode *inode, struct file *file,
952 unsigned int cmd, unsigned long arg)
954 struct mon_reader_bin *rp = file->private_data;
955 // struct mon_bus* mbus = rp->r.m_bus;
956 int ret = 0;
957 struct mon_bin_hdr *ep;
958 unsigned long flags;
960 switch (cmd) {
962 case MON_IOCQ_URB_LEN:
964 * N.B. This only returns the size of data, without the header.
966 spin_lock_irqsave(&rp->b_lock, flags);
967 if (!MON_RING_EMPTY(rp)) {
968 ep = MON_OFF2HDR(rp, rp->b_out);
969 ret = ep->len_cap;
971 spin_unlock_irqrestore(&rp->b_lock, flags);
972 break;
974 case MON_IOCQ_RING_SIZE:
975 ret = rp->b_size;
976 break;
978 case MON_IOCT_RING_SIZE:
980 * Changing the buffer size will flush it's contents; the new
981 * buffer is allocated before releasing the old one to be sure
982 * the device will stay functional also in case of memory
983 * pressure.
986 int size;
987 struct mon_pgmap *vec;
989 if (arg < BUFF_MIN || arg > BUFF_MAX)
990 return -EINVAL;
992 size = CHUNK_ALIGN(arg);
993 if ((vec = kzalloc(sizeof(struct mon_pgmap) * (size/CHUNK_SIZE),
994 GFP_KERNEL)) == NULL) {
995 ret = -ENOMEM;
996 break;
999 ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
1000 if (ret < 0) {
1001 kfree(vec);
1002 break;
1005 mutex_lock(&rp->fetch_lock);
1006 spin_lock_irqsave(&rp->b_lock, flags);
1007 mon_free_buff(rp->b_vec, size/CHUNK_SIZE);
1008 kfree(rp->b_vec);
1009 rp->b_vec = vec;
1010 rp->b_size = size;
1011 rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
1012 rp->cnt_lost = 0;
1013 spin_unlock_irqrestore(&rp->b_lock, flags);
1014 mutex_unlock(&rp->fetch_lock);
1016 break;
1018 case MON_IOCH_MFLUSH:
1019 ret = mon_bin_flush(rp, arg);
1020 break;
1022 case MON_IOCX_GET:
1023 case MON_IOCX_GETX:
1025 struct mon_bin_get getb;
1027 if (copy_from_user(&getb, (void __user *)arg,
1028 sizeof(struct mon_bin_get)))
1029 return -EFAULT;
1031 if (getb.alloc > 0x10000000) /* Want to cast to u32 */
1032 return -EINVAL;
1033 ret = mon_bin_get_event(file, rp, getb.hdr,
1034 (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
1035 getb.data, (unsigned int)getb.alloc);
1037 break;
1039 case MON_IOCX_MFETCH:
1041 struct mon_bin_mfetch mfetch;
1042 struct mon_bin_mfetch __user *uptr;
1044 uptr = (struct mon_bin_mfetch __user *)arg;
1046 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1047 return -EFAULT;
1049 if (mfetch.nflush) {
1050 ret = mon_bin_flush(rp, mfetch.nflush);
1051 if (ret < 0)
1052 return ret;
1053 if (put_user(ret, &uptr->nflush))
1054 return -EFAULT;
1056 ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1057 if (ret < 0)
1058 return ret;
1059 if (put_user(ret, &uptr->nfetch))
1060 return -EFAULT;
1061 ret = 0;
1063 break;
1065 case MON_IOCG_STATS: {
1066 struct mon_bin_stats __user *sp;
1067 unsigned int nevents;
1068 unsigned int ndropped;
1070 spin_lock_irqsave(&rp->b_lock, flags);
1071 ndropped = rp->cnt_lost;
1072 rp->cnt_lost = 0;
1073 spin_unlock_irqrestore(&rp->b_lock, flags);
1074 nevents = mon_bin_queued(rp);
1076 sp = (struct mon_bin_stats __user *)arg;
1077 if (put_user(rp->cnt_lost, &sp->dropped))
1078 return -EFAULT;
1079 if (put_user(nevents, &sp->queued))
1080 return -EFAULT;
1083 break;
1085 default:
1086 return -ENOTTY;
1089 return ret;
1092 #ifdef CONFIG_COMPAT
1093 static long mon_bin_compat_ioctl(struct file *file,
1094 unsigned int cmd, unsigned long arg)
1096 struct mon_reader_bin *rp = file->private_data;
1097 int ret;
1099 switch (cmd) {
1101 case MON_IOCX_GET32:
1102 case MON_IOCX_GETX32:
1104 struct mon_bin_get32 getb;
1106 if (copy_from_user(&getb, (void __user *)arg,
1107 sizeof(struct mon_bin_get32)))
1108 return -EFAULT;
1110 ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
1111 (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
1112 compat_ptr(getb.data32), getb.alloc32);
1113 if (ret < 0)
1114 return ret;
1116 return 0;
1118 case MON_IOCX_MFETCH32:
1120 struct mon_bin_mfetch32 mfetch;
1121 struct mon_bin_mfetch32 __user *uptr;
1123 uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1125 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1126 return -EFAULT;
1128 if (mfetch.nflush32) {
1129 ret = mon_bin_flush(rp, mfetch.nflush32);
1130 if (ret < 0)
1131 return ret;
1132 if (put_user(ret, &uptr->nflush32))
1133 return -EFAULT;
1135 ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1136 mfetch.nfetch32);
1137 if (ret < 0)
1138 return ret;
1139 if (put_user(ret, &uptr->nfetch32))
1140 return -EFAULT;
1142 return 0;
1144 case MON_IOCG_STATS:
1145 return mon_bin_ioctl(NULL, file, cmd,
1146 (unsigned long) compat_ptr(arg));
1148 case MON_IOCQ_URB_LEN:
1149 case MON_IOCQ_RING_SIZE:
1150 case MON_IOCT_RING_SIZE:
1151 case MON_IOCH_MFLUSH:
1152 return mon_bin_ioctl(NULL, file, cmd, arg);
1154 default:
1157 return -ENOTTY;
1159 #endif /* CONFIG_COMPAT */
1161 static unsigned int
1162 mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1164 struct mon_reader_bin *rp = file->private_data;
1165 unsigned int mask = 0;
1166 unsigned long flags;
1168 if (file->f_mode & FMODE_READ)
1169 poll_wait(file, &rp->b_wait, wait);
1171 spin_lock_irqsave(&rp->b_lock, flags);
1172 if (!MON_RING_EMPTY(rp))
1173 mask |= POLLIN | POLLRDNORM; /* readable */
1174 spin_unlock_irqrestore(&rp->b_lock, flags);
1175 return mask;
1179 * open and close: just keep track of how many times the device is
1180 * mapped, to use the proper memory allocation function.
1182 static void mon_bin_vma_open(struct vm_area_struct *vma)
1184 struct mon_reader_bin *rp = vma->vm_private_data;
1185 rp->mmap_active++;
1188 static void mon_bin_vma_close(struct vm_area_struct *vma)
1190 struct mon_reader_bin *rp = vma->vm_private_data;
1191 rp->mmap_active--;
1195 * Map ring pages to user space.
1197 static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1199 struct mon_reader_bin *rp = vma->vm_private_data;
1200 unsigned long offset, chunk_idx;
1201 struct page *pageptr;
1203 offset = vmf->pgoff << PAGE_SHIFT;
1204 if (offset >= rp->b_size)
1205 return VM_FAULT_SIGBUS;
1206 chunk_idx = offset / CHUNK_SIZE;
1207 pageptr = rp->b_vec[chunk_idx].pg;
1208 get_page(pageptr);
1209 vmf->page = pageptr;
1210 return 0;
1213 static const struct vm_operations_struct mon_bin_vm_ops = {
1214 .open = mon_bin_vma_open,
1215 .close = mon_bin_vma_close,
1216 .fault = mon_bin_vma_fault,
1219 static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1221 /* don't do anything here: "fault" will set up page table entries */
1222 vma->vm_ops = &mon_bin_vm_ops;
1223 vma->vm_flags |= VM_RESERVED;
1224 vma->vm_private_data = filp->private_data;
1225 mon_bin_vma_open(vma);
1226 return 0;
1229 static const struct file_operations mon_fops_binary = {
1230 .owner = THIS_MODULE,
1231 .open = mon_bin_open,
1232 .llseek = no_llseek,
1233 .read = mon_bin_read,
1234 /* .write = mon_text_write, */
1235 .poll = mon_bin_poll,
1236 .ioctl = mon_bin_ioctl,
1237 #ifdef CONFIG_COMPAT
1238 .compat_ioctl = mon_bin_compat_ioctl,
1239 #endif
1240 .release = mon_bin_release,
1241 .mmap = mon_bin_mmap,
1244 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1246 DECLARE_WAITQUEUE(waita, current);
1247 unsigned long flags;
1249 add_wait_queue(&rp->b_wait, &waita);
1250 set_current_state(TASK_INTERRUPTIBLE);
1252 spin_lock_irqsave(&rp->b_lock, flags);
1253 while (MON_RING_EMPTY(rp)) {
1254 spin_unlock_irqrestore(&rp->b_lock, flags);
1256 if (file->f_flags & O_NONBLOCK) {
1257 set_current_state(TASK_RUNNING);
1258 remove_wait_queue(&rp->b_wait, &waita);
1259 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1261 schedule();
1262 if (signal_pending(current)) {
1263 remove_wait_queue(&rp->b_wait, &waita);
1264 return -EINTR;
1266 set_current_state(TASK_INTERRUPTIBLE);
1268 spin_lock_irqsave(&rp->b_lock, flags);
1270 spin_unlock_irqrestore(&rp->b_lock, flags);
1272 set_current_state(TASK_RUNNING);
1273 remove_wait_queue(&rp->b_wait, &waita);
1274 return 0;
1277 static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1279 int n;
1280 unsigned long vaddr;
1282 for (n = 0; n < npages; n++) {
1283 vaddr = get_zeroed_page(GFP_KERNEL);
1284 if (vaddr == 0) {
1285 while (n-- != 0)
1286 free_page((unsigned long) map[n].ptr);
1287 return -ENOMEM;
1289 map[n].ptr = (unsigned char *) vaddr;
1290 map[n].pg = virt_to_page((void *) vaddr);
1292 return 0;
1295 static void mon_free_buff(struct mon_pgmap *map, int npages)
1297 int n;
1299 for (n = 0; n < npages; n++)
1300 free_page((unsigned long) map[n].ptr);
1303 int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1305 struct device *dev;
1306 unsigned minor = ubus? ubus->busnum: 0;
1308 if (minor >= MON_BIN_MAX_MINOR)
1309 return 0;
1311 dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
1312 MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1313 "usbmon%d", minor);
1314 if (IS_ERR(dev))
1315 return 0;
1317 mbus->classdev = dev;
1318 return 1;
1321 void mon_bin_del(struct mon_bus *mbus)
1323 device_destroy(mon_bin_class, mbus->classdev->devt);
1326 int __init mon_bin_init(void)
1328 int rc;
1330 mon_bin_class = class_create(THIS_MODULE, "usbmon");
1331 if (IS_ERR(mon_bin_class)) {
1332 rc = PTR_ERR(mon_bin_class);
1333 goto err_class;
1336 rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1337 if (rc < 0)
1338 goto err_dev;
1340 cdev_init(&mon_bin_cdev, &mon_fops_binary);
1341 mon_bin_cdev.owner = THIS_MODULE;
1343 rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1344 if (rc < 0)
1345 goto err_add;
1347 return 0;
1349 err_add:
1350 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1351 err_dev:
1352 class_destroy(mon_bin_class);
1353 err_class:
1354 return rc;
1357 void mon_bin_exit(void)
1359 cdev_del(&mon_bin_cdev);
1360 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1361 class_destroy(mon_bin_class);