const: mark struct vm_struct_operations
[linux-block.git] / drivers / usb / mon / mon_bin.c
1 /*
2  * The USB Monitor, inspired by Dave Harding's USBMon.
3  *
4  * This is a binary format reader.
5  *
6  * Copyright (C) 2006 Paolo Abeni (paolo.abeni@email.it)
7  * Copyright (C) 2006,2007 Pete Zaitcev (zaitcev@redhat.com)
8  */
9
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
20 #include <asm/uaccess.h>
21
22 #include "usb_mon.h"
23
24 /*
25  * Defined by USB 2.0 clause 9.3, table 9.2.
26  */
27 #define SETUP_LEN  8
28
29 /* ioctl macros */
30 #define MON_IOC_MAGIC 0x92
31
32 #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
33 /* #2 used to be MON_IOCX_URB, removed before it got into Linus tree */
34 #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
35 #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
36 #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
37 #define MON_IOCX_GET   _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
38 #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
39 #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
40 /* #9 was MON_IOCT_SETAPI */
41 #define MON_IOCX_GETX   _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get)
42
43 #ifdef CONFIG_COMPAT
44 #define MON_IOCX_GET32 _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get32)
45 #define MON_IOCX_MFETCH32 _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch32)
46 #define MON_IOCX_GETX32   _IOW(MON_IOC_MAGIC, 10, struct mon_bin_get32)
47 #endif
48
49 /*
50  * Some architectures have enormous basic pages (16KB for ia64, 64KB for ppc).
51  * But it's all right. Just use a simple way to make sure the chunk is never
52  * smaller than a page.
53  *
54  * N.B. An application does not know our chunk size.
55  *
56  * Woops, get_zeroed_page() returns a single page. I guess we're stuck with
57  * page-sized chunks for the time being.
58  */
59 #define CHUNK_SIZE   PAGE_SIZE
60 #define CHUNK_ALIGN(x)   (((x)+CHUNK_SIZE-1) & ~(CHUNK_SIZE-1))
61
62 /*
63  * The magic limit was calculated so that it allows the monitoring
64  * application to pick data once in two ticks. This way, another application,
65  * which presumably drives the bus, gets to hog CPU, yet we collect our data.
66  * If HZ is 100, a 480 mbit/s bus drives 614 KB every jiffy. USB has an
67  * enormous overhead built into the bus protocol, so we need about 1000 KB.
68  *
69  * This is still too much for most cases, where we just snoop a few
70  * descriptor fetches for enumeration. So, the default is a "reasonable"
71  * amount for systems with HZ=250 and incomplete bus saturation.
72  *
73  * XXX What about multi-megabyte URBs which take minutes to transfer?
74  */
75 #define BUFF_MAX  CHUNK_ALIGN(1200*1024)
76 #define BUFF_DFL   CHUNK_ALIGN(300*1024)
77 #define BUFF_MIN     CHUNK_ALIGN(8*1024)
78
79 /*
80  * The per-event API header (2 per URB).
81  *
82  * This structure is seen in userland as defined by the documentation.
83  */
84 struct mon_bin_hdr {
85         u64 id;                 /* URB ID - from submission to callback */
86         unsigned char type;     /* Same as in text API; extensible. */
87         unsigned char xfer_type;        /* ISO, Intr, Control, Bulk */
88         unsigned char epnum;    /* Endpoint number and transfer direction */
89         unsigned char devnum;   /* Device address */
90         unsigned short busnum;  /* Bus number */
91         char flag_setup;
92         char flag_data;
93         s64 ts_sec;             /* gettimeofday */
94         s32 ts_usec;            /* gettimeofday */
95         int status;
96         unsigned int len_urb;   /* Length of data (submitted or actual) */
97         unsigned int len_cap;   /* Delivered length */
98         union {
99                 unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
100                 struct iso_rec {
101                         int error_count;
102                         int numdesc;
103                 } iso;
104         } s;
105         int interval;
106         int start_frame;
107         unsigned int xfer_flags;
108         unsigned int ndesc;     /* Actual number of ISO descriptors */
109 };
110
111 /*
112  * ISO vector, packed into the head of data stream.
113  * This has to take 16 bytes to make sure that the end of buffer
114  * wrap is not happening in the middle of a descriptor.
115  */
116 struct mon_bin_isodesc {
117         int          iso_status;
118         unsigned int iso_off;
119         unsigned int iso_len;
120         u32 _pad;
121 };
122
123 /* per file statistic */
124 struct mon_bin_stats {
125         u32 queued;
126         u32 dropped;
127 };
128
129 struct mon_bin_get {
130         struct mon_bin_hdr __user *hdr; /* Can be 48 bytes or 64. */
131         void __user *data;
132         size_t alloc;           /* Length of data (can be zero) */
133 };
134
135 struct mon_bin_mfetch {
136         u32 __user *offvec;     /* Vector of events fetched */
137         u32 nfetch;             /* Number of events to fetch (out: fetched) */
138         u32 nflush;             /* Number of events to flush */
139 };
140
141 #ifdef CONFIG_COMPAT
142 struct mon_bin_get32 {
143         u32 hdr32;
144         u32 data32;
145         u32 alloc32;
146 };
147
148 struct mon_bin_mfetch32 {
149         u32 offvec32;
150         u32 nfetch32;
151         u32 nflush32;
152 };
153 #endif
154
155 /* Having these two values same prevents wrapping of the mon_bin_hdr */
156 #define PKT_ALIGN   64
157 #define PKT_SIZE    64
158
159 #define PKT_SZ_API0 48  /* API 0 (2.6.20) size */
160 #define PKT_SZ_API1 64  /* API 1 size: extra fields */
161
162 #define ISODESC_MAX   128       /* Same number as usbfs allows, 2048 bytes. */
163
164 /* max number of USB bus supported */
165 #define MON_BIN_MAX_MINOR 128
166
167 /*
168  * The buffer: map of used pages.
169  */
170 struct mon_pgmap {
171         struct page *pg;
172         unsigned char *ptr;     /* XXX just use page_to_virt everywhere? */
173 };
174
175 /*
176  * This gets associated with an open file struct.
177  */
178 struct mon_reader_bin {
179         /* The buffer: one per open. */
180         spinlock_t b_lock;              /* Protect b_cnt, b_in */
181         unsigned int b_size;            /* Current size of the buffer - bytes */
182         unsigned int b_cnt;             /* Bytes used */
183         unsigned int b_in, b_out;       /* Offsets into buffer - bytes */
184         unsigned int b_read;            /* Amount of read data in curr. pkt. */
185         struct mon_pgmap *b_vec;        /* The map array */
186         wait_queue_head_t b_wait;       /* Wait for data here */
187
188         struct mutex fetch_lock;        /* Protect b_read, b_out */
189         int mmap_active;
190
191         /* A list of these is needed for "bus 0". Some time later. */
192         struct mon_reader r;
193
194         /* Stats */
195         unsigned int cnt_lost;
196 };
197
198 static inline struct mon_bin_hdr *MON_OFF2HDR(const struct mon_reader_bin *rp,
199     unsigned int offset)
200 {
201         return (struct mon_bin_hdr *)
202             (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
203 }
204
205 #define MON_RING_EMPTY(rp)      ((rp)->b_cnt == 0)
206
207 static unsigned char xfer_to_pipe[4] = {
208         PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
209 };
210
211 static struct class *mon_bin_class;
212 static dev_t mon_bin_dev0;
213 static struct cdev mon_bin_cdev;
214
215 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
216     unsigned int offset, unsigned int size);
217 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp);
218 static int mon_alloc_buff(struct mon_pgmap *map, int npages);
219 static void mon_free_buff(struct mon_pgmap *map, int npages);
220
221 /*
222  * This is a "chunked memcpy". It does not manipulate any counters.
223  */
224 static void mon_copy_to_buff(const struct mon_reader_bin *this,
225     unsigned int off, const unsigned char *from, unsigned int length)
226 {
227         unsigned int step_len;
228         unsigned char *buf;
229         unsigned int in_page;
230
231         while (length) {
232                 /*
233                  * Determine step_len.
234                  */
235                 step_len = length;
236                 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
237                 if (in_page < step_len)
238                         step_len = in_page;
239
240                 /*
241                  * Copy data and advance pointers.
242                  */
243                 buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
244                 memcpy(buf, from, step_len);
245                 if ((off += step_len) >= this->b_size) off = 0;
246                 from += step_len;
247                 length -= step_len;
248         }
249 }
250
251 /*
252  * This is a little worse than the above because it's "chunked copy_to_user".
253  * The return value is an error code, not an offset.
254  */
255 static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
256     char __user *to, int length)
257 {
258         unsigned int step_len;
259         unsigned char *buf;
260         unsigned int in_page;
261
262         while (length) {
263                 /*
264                  * Determine step_len.
265                  */
266                 step_len = length;
267                 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
268                 if (in_page < step_len)
269                         step_len = in_page;
270
271                 /*
272                  * Copy data and advance pointers.
273                  */
274                 buf = this->b_vec[off / CHUNK_SIZE].ptr + off % CHUNK_SIZE;
275                 if (copy_to_user(to, buf, step_len))
276                         return -EINVAL;
277                 if ((off += step_len) >= this->b_size) off = 0;
278                 to += step_len;
279                 length -= step_len;
280         }
281         return 0;
282 }
283
284 /*
285  * Allocate an (aligned) area in the buffer.
286  * This is called under b_lock.
287  * Returns ~0 on failure.
288  */
289 static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
290     unsigned int size)
291 {
292         unsigned int offset;
293
294         size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
295         if (rp->b_cnt + size > rp->b_size)
296                 return ~0;
297         offset = rp->b_in;
298         rp->b_cnt += size;
299         if ((rp->b_in += size) >= rp->b_size)
300                 rp->b_in -= rp->b_size;
301         return offset;
302 }
303
304 /*
305  * This is the same thing as mon_buff_area_alloc, only it does not allow
306  * buffers to wrap. This is needed by applications which pass references
307  * into mmap-ed buffers up their stacks (libpcap can do that).
308  *
309  * Currently, we always have the header stuck with the data, although
310  * it is not strictly speaking necessary.
311  *
312  * When a buffer would wrap, we place a filler packet to mark the space.
313  */
314 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
315     unsigned int size)
316 {
317         unsigned int offset;
318         unsigned int fill_size;
319
320         size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
321         if (rp->b_cnt + size > rp->b_size)
322                 return ~0;
323         if (rp->b_in + size > rp->b_size) {
324                 /*
325                  * This would wrap. Find if we still have space after
326                  * skipping to the end of the buffer. If we do, place
327                  * a filler packet and allocate a new packet.
328                  */
329                 fill_size = rp->b_size - rp->b_in;
330                 if (rp->b_cnt + size + fill_size > rp->b_size)
331                         return ~0;
332                 mon_buff_area_fill(rp, rp->b_in, fill_size);
333
334                 offset = 0;
335                 rp->b_in = size;
336                 rp->b_cnt += size + fill_size;
337         } else if (rp->b_in + size == rp->b_size) {
338                 offset = rp->b_in;
339                 rp->b_in = 0;
340                 rp->b_cnt += size;
341         } else {
342                 offset = rp->b_in;
343                 rp->b_in += size;
344                 rp->b_cnt += size;
345         }
346         return offset;
347 }
348
349 /*
350  * Return a few (kilo-)bytes to the head of the buffer.
351  * This is used if a DMA fetch fails.
352  */
353 static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
354 {
355
356         size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
357         rp->b_cnt -= size;
358         if (rp->b_in < size)
359                 rp->b_in += rp->b_size;
360         rp->b_in -= size;
361 }
362
363 /*
364  * This has to be called under both b_lock and fetch_lock, because
365  * it accesses both b_cnt and b_out.
366  */
367 static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
368 {
369
370         size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
371         rp->b_cnt -= size;
372         if ((rp->b_out += size) >= rp->b_size)
373                 rp->b_out -= rp->b_size;
374 }
375
376 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
377     unsigned int offset, unsigned int size)
378 {
379         struct mon_bin_hdr *ep;
380
381         ep = MON_OFF2HDR(rp, offset);
382         memset(ep, 0, PKT_SIZE);
383         ep->type = '@';
384         ep->len_cap = size - PKT_SIZE;
385 }
386
387 static inline char mon_bin_get_setup(unsigned char *setupb,
388     const struct urb *urb, char ev_type)
389 {
390
391         if (urb->setup_packet == NULL)
392                 return 'Z';
393         memcpy(setupb, urb->setup_packet, SETUP_LEN);
394         return 0;
395 }
396
397 static char mon_bin_get_data(const struct mon_reader_bin *rp,
398     unsigned int offset, struct urb *urb, unsigned int length)
399 {
400
401         if (urb->transfer_buffer == NULL)
402                 return 'Z';
403         mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
404         return 0;
405 }
406
407 static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
408     unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
409 {
410         struct mon_bin_isodesc *dp;
411         struct usb_iso_packet_descriptor *fp;
412
413         fp = urb->iso_frame_desc;
414         while (ndesc-- != 0) {
415                 dp = (struct mon_bin_isodesc *)
416                     (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
417                 dp->iso_status = fp->status;
418                 dp->iso_off = fp->offset;
419                 dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
420                 dp->_pad = 0;
421                 if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
422                         offset = 0;
423                 fp++;
424         }
425 }
426
427 static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
428     char ev_type, int status)
429 {
430         const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
431         unsigned long flags;
432         struct timeval ts;
433         unsigned int urb_length;
434         unsigned int offset;
435         unsigned int length;
436         unsigned int ndesc, lendesc;
437         unsigned char dir;
438         struct mon_bin_hdr *ep;
439         char data_tag = 0;
440
441         do_gettimeofday(&ts);
442
443         spin_lock_irqsave(&rp->b_lock, flags);
444
445         /*
446          * Find the maximum allowable length, then allocate space.
447          */
448         if (usb_endpoint_xfer_isoc(epd)) {
449                 if (urb->number_of_packets < 0) {
450                         ndesc = 0;
451                 } else if (urb->number_of_packets >= ISODESC_MAX) {
452                         ndesc = ISODESC_MAX;
453                 } else {
454                         ndesc = urb->number_of_packets;
455                 }
456         } else {
457                 ndesc = 0;
458         }
459         lendesc = ndesc*sizeof(struct mon_bin_isodesc);
460
461         urb_length = (ev_type == 'S') ?
462             urb->transfer_buffer_length : urb->actual_length;
463         length = urb_length;
464
465         if (length >= rp->b_size/5)
466                 length = rp->b_size/5;
467
468         if (usb_urb_dir_in(urb)) {
469                 if (ev_type == 'S') {
470                         length = 0;
471                         data_tag = '<';
472                 }
473                 /* Cannot rely on endpoint number in case of control ep.0 */
474                 dir = USB_DIR_IN;
475         } else {
476                 if (ev_type == 'C') {
477                         length = 0;
478                         data_tag = '>';
479                 }
480                 dir = 0;
481         }
482
483         if (rp->mmap_active) {
484                 offset = mon_buff_area_alloc_contiguous(rp,
485                                                  length + PKT_SIZE + lendesc);
486         } else {
487                 offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
488         }
489         if (offset == ~0) {
490                 rp->cnt_lost++;
491                 spin_unlock_irqrestore(&rp->b_lock, flags);
492                 return;
493         }
494
495         ep = MON_OFF2HDR(rp, offset);
496         if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
497
498         /*
499          * Fill the allocated area.
500          */
501         memset(ep, 0, PKT_SIZE);
502         ep->type = ev_type;
503         ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
504         ep->epnum = dir | usb_endpoint_num(epd);
505         ep->devnum = urb->dev->devnum;
506         ep->busnum = urb->dev->bus->busnum;
507         ep->id = (unsigned long) urb;
508         ep->ts_sec = ts.tv_sec;
509         ep->ts_usec = ts.tv_usec;
510         ep->status = status;
511         ep->len_urb = urb_length;
512         ep->len_cap = length + lendesc;
513         ep->xfer_flags = urb->transfer_flags;
514
515         if (usb_endpoint_xfer_int(epd)) {
516                 ep->interval = urb->interval;
517         } else if (usb_endpoint_xfer_isoc(epd)) {
518                 ep->interval = urb->interval;
519                 ep->start_frame = urb->start_frame;
520                 ep->s.iso.error_count = urb->error_count;
521                 ep->s.iso.numdesc = urb->number_of_packets;
522         }
523
524         if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
525                 ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
526         } else {
527                 ep->flag_setup = '-';
528         }
529
530         if (ndesc != 0) {
531                 ep->ndesc = ndesc;
532                 mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
533                 if ((offset += lendesc) >= rp->b_size)
534                         offset -= rp->b_size;
535         }
536
537         if (length != 0) {
538                 ep->flag_data = mon_bin_get_data(rp, offset, urb, length);
539                 if (ep->flag_data != 0) {       /* Yes, it's 0x00, not '0' */
540                         ep->len_cap = 0;
541                         mon_buff_area_shrink(rp, length);
542                 }
543         } else {
544                 ep->flag_data = data_tag;
545         }
546
547         spin_unlock_irqrestore(&rp->b_lock, flags);
548
549         wake_up(&rp->b_wait);
550 }
551
552 static void mon_bin_submit(void *data, struct urb *urb)
553 {
554         struct mon_reader_bin *rp = data;
555         mon_bin_event(rp, urb, 'S', -EINPROGRESS);
556 }
557
558 static void mon_bin_complete(void *data, struct urb *urb, int status)
559 {
560         struct mon_reader_bin *rp = data;
561         mon_bin_event(rp, urb, 'C', status);
562 }
563
564 static void mon_bin_error(void *data, struct urb *urb, int error)
565 {
566         struct mon_reader_bin *rp = data;
567         unsigned long flags;
568         unsigned int offset;
569         struct mon_bin_hdr *ep;
570
571         spin_lock_irqsave(&rp->b_lock, flags);
572
573         offset = mon_buff_area_alloc(rp, PKT_SIZE);
574         if (offset == ~0) {
575                 /* Not incrementing cnt_lost. Just because. */
576                 spin_unlock_irqrestore(&rp->b_lock, flags);
577                 return;
578         }
579
580         ep = MON_OFF2HDR(rp, offset);
581
582         memset(ep, 0, PKT_SIZE);
583         ep->type = 'E';
584         ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
585         ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
586         ep->epnum |= usb_endpoint_num(&urb->ep->desc);
587         ep->devnum = urb->dev->devnum;
588         ep->busnum = urb->dev->bus->busnum;
589         ep->id = (unsigned long) urb;
590         ep->status = error;
591
592         ep->flag_setup = '-';
593         ep->flag_data = 'E';
594
595         spin_unlock_irqrestore(&rp->b_lock, flags);
596
597         wake_up(&rp->b_wait);
598 }
599
600 static int mon_bin_open(struct inode *inode, struct file *file)
601 {
602         struct mon_bus *mbus;
603         struct mon_reader_bin *rp;
604         size_t size;
605         int rc;
606
607         lock_kernel();
608         mutex_lock(&mon_lock);
609         if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) {
610                 mutex_unlock(&mon_lock);
611                 unlock_kernel();
612                 return -ENODEV;
613         }
614         if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
615                 printk(KERN_ERR TAG ": consistency error on open\n");
616                 mutex_unlock(&mon_lock);
617                 unlock_kernel();
618                 return -ENODEV;
619         }
620
621         rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
622         if (rp == NULL) {
623                 rc = -ENOMEM;
624                 goto err_alloc;
625         }
626         spin_lock_init(&rp->b_lock);
627         init_waitqueue_head(&rp->b_wait);
628         mutex_init(&rp->fetch_lock);
629         rp->b_size = BUFF_DFL;
630
631         size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
632         if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
633                 rc = -ENOMEM;
634                 goto err_allocvec;
635         }
636
637         if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
638                 goto err_allocbuff;
639
640         rp->r.m_bus = mbus;
641         rp->r.r_data = rp;
642         rp->r.rnf_submit = mon_bin_submit;
643         rp->r.rnf_error = mon_bin_error;
644         rp->r.rnf_complete = mon_bin_complete;
645
646         mon_reader_add(mbus, &rp->r);
647
648         file->private_data = rp;
649         mutex_unlock(&mon_lock);
650         unlock_kernel();
651         return 0;
652
653 err_allocbuff:
654         kfree(rp->b_vec);
655 err_allocvec:
656         kfree(rp);
657 err_alloc:
658         mutex_unlock(&mon_lock);
659         unlock_kernel();
660         return rc;
661 }
662
663 /*
664  * Extract an event from buffer and copy it to user space.
665  * Wait if there is no event ready.
666  * Returns zero or error.
667  */
668 static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
669     struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
670     void __user *data, unsigned int nbytes)
671 {
672         unsigned long flags;
673         struct mon_bin_hdr *ep;
674         size_t step_len;
675         unsigned int offset;
676         int rc;
677
678         mutex_lock(&rp->fetch_lock);
679
680         if ((rc = mon_bin_wait_event(file, rp)) < 0) {
681                 mutex_unlock(&rp->fetch_lock);
682                 return rc;
683         }
684
685         ep = MON_OFF2HDR(rp, rp->b_out);
686
687         if (copy_to_user(hdr, ep, hdrbytes)) {
688                 mutex_unlock(&rp->fetch_lock);
689                 return -EFAULT;
690         }
691
692         step_len = min(ep->len_cap, nbytes);
693         if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
694
695         if (copy_from_buf(rp, offset, data, step_len)) {
696                 mutex_unlock(&rp->fetch_lock);
697                 return -EFAULT;
698         }
699
700         spin_lock_irqsave(&rp->b_lock, flags);
701         mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
702         spin_unlock_irqrestore(&rp->b_lock, flags);
703         rp->b_read = 0;
704
705         mutex_unlock(&rp->fetch_lock);
706         return 0;
707 }
708
709 static int mon_bin_release(struct inode *inode, struct file *file)
710 {
711         struct mon_reader_bin *rp = file->private_data;
712         struct mon_bus* mbus = rp->r.m_bus;
713
714         mutex_lock(&mon_lock);
715
716         if (mbus->nreaders <= 0) {
717                 printk(KERN_ERR TAG ": consistency error on close\n");
718                 mutex_unlock(&mon_lock);
719                 return 0;
720         }
721         mon_reader_del(mbus, &rp->r);
722
723         mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
724         kfree(rp->b_vec);
725         kfree(rp);
726
727         mutex_unlock(&mon_lock);
728         return 0;
729 }
730
731 static ssize_t mon_bin_read(struct file *file, char __user *buf,
732     size_t nbytes, loff_t *ppos)
733 {
734         struct mon_reader_bin *rp = file->private_data;
735         unsigned int hdrbytes = PKT_SZ_API0;
736         unsigned long flags;
737         struct mon_bin_hdr *ep;
738         unsigned int offset;
739         size_t step_len;
740         char *ptr;
741         ssize_t done = 0;
742         int rc;
743
744         mutex_lock(&rp->fetch_lock);
745
746         if ((rc = mon_bin_wait_event(file, rp)) < 0) {
747                 mutex_unlock(&rp->fetch_lock);
748                 return rc;
749         }
750
751         ep = MON_OFF2HDR(rp, rp->b_out);
752
753         if (rp->b_read < hdrbytes) {
754                 step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
755                 ptr = ((char *)ep) + rp->b_read;
756                 if (step_len && copy_to_user(buf, ptr, step_len)) {
757                         mutex_unlock(&rp->fetch_lock);
758                         return -EFAULT;
759                 }
760                 nbytes -= step_len;
761                 buf += step_len;
762                 rp->b_read += step_len;
763                 done += step_len;
764         }
765
766         if (rp->b_read >= hdrbytes) {
767                 step_len = ep->len_cap;
768                 step_len -= rp->b_read - hdrbytes;
769                 if (step_len > nbytes)
770                         step_len = nbytes;
771                 offset = rp->b_out + PKT_SIZE;
772                 offset += rp->b_read - hdrbytes;
773                 if (offset >= rp->b_size)
774                         offset -= rp->b_size;
775                 if (copy_from_buf(rp, offset, buf, step_len)) {
776                         mutex_unlock(&rp->fetch_lock);
777                         return -EFAULT;
778                 }
779                 nbytes -= step_len;
780                 buf += step_len;
781                 rp->b_read += step_len;
782                 done += step_len;
783         }
784
785         /*
786          * Check if whole packet was read, and if so, jump to the next one.
787          */
788         if (rp->b_read >= hdrbytes + ep->len_cap) {
789                 spin_lock_irqsave(&rp->b_lock, flags);
790                 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
791                 spin_unlock_irqrestore(&rp->b_lock, flags);
792                 rp->b_read = 0;
793         }
794
795         mutex_unlock(&rp->fetch_lock);
796         return done;
797 }
798
799 /*
800  * Remove at most nevents from chunked buffer.
801  * Returns the number of removed events.
802  */
803 static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
804 {
805         unsigned long flags;
806         struct mon_bin_hdr *ep;
807         int i;
808
809         mutex_lock(&rp->fetch_lock);
810         spin_lock_irqsave(&rp->b_lock, flags);
811         for (i = 0; i < nevents; ++i) {
812                 if (MON_RING_EMPTY(rp))
813                         break;
814
815                 ep = MON_OFF2HDR(rp, rp->b_out);
816                 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
817         }
818         spin_unlock_irqrestore(&rp->b_lock, flags);
819         rp->b_read = 0;
820         mutex_unlock(&rp->fetch_lock);
821         return i;
822 }
823
824 /*
825  * Fetch at most max event offsets into the buffer and put them into vec.
826  * The events are usually freed later with mon_bin_flush.
827  * Return the effective number of events fetched.
828  */
829 static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
830     u32 __user *vec, unsigned int max)
831 {
832         unsigned int cur_out;
833         unsigned int bytes, avail;
834         unsigned int size;
835         unsigned int nevents;
836         struct mon_bin_hdr *ep;
837         unsigned long flags;
838         int rc;
839
840         mutex_lock(&rp->fetch_lock);
841
842         if ((rc = mon_bin_wait_event(file, rp)) < 0) {
843                 mutex_unlock(&rp->fetch_lock);
844                 return rc;
845         }
846
847         spin_lock_irqsave(&rp->b_lock, flags);
848         avail = rp->b_cnt;
849         spin_unlock_irqrestore(&rp->b_lock, flags);
850
851         cur_out = rp->b_out;
852         nevents = 0;
853         bytes = 0;
854         while (bytes < avail) {
855                 if (nevents >= max)
856                         break;
857
858                 ep = MON_OFF2HDR(rp, cur_out);
859                 if (put_user(cur_out, &vec[nevents])) {
860                         mutex_unlock(&rp->fetch_lock);
861                         return -EFAULT;
862                 }
863
864                 nevents++;
865                 size = ep->len_cap + PKT_SIZE;
866                 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
867                 if ((cur_out += size) >= rp->b_size)
868                         cur_out -= rp->b_size;
869                 bytes += size;
870         }
871
872         mutex_unlock(&rp->fetch_lock);
873         return nevents;
874 }
875
876 /*
877  * Count events. This is almost the same as the above mon_bin_fetch,
878  * only we do not store offsets into user vector, and we have no limit.
879  */
880 static int mon_bin_queued(struct mon_reader_bin *rp)
881 {
882         unsigned int cur_out;
883         unsigned int bytes, avail;
884         unsigned int size;
885         unsigned int nevents;
886         struct mon_bin_hdr *ep;
887         unsigned long flags;
888
889         mutex_lock(&rp->fetch_lock);
890
891         spin_lock_irqsave(&rp->b_lock, flags);
892         avail = rp->b_cnt;
893         spin_unlock_irqrestore(&rp->b_lock, flags);
894
895         cur_out = rp->b_out;
896         nevents = 0;
897         bytes = 0;
898         while (bytes < avail) {
899                 ep = MON_OFF2HDR(rp, cur_out);
900
901                 nevents++;
902                 size = ep->len_cap + PKT_SIZE;
903                 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
904                 if ((cur_out += size) >= rp->b_size)
905                         cur_out -= rp->b_size;
906                 bytes += size;
907         }
908
909         mutex_unlock(&rp->fetch_lock);
910         return nevents;
911 }
912
913 /*
914  */
915 static int mon_bin_ioctl(struct inode *inode, struct file *file,
916     unsigned int cmd, unsigned long arg)
917 {
918         struct mon_reader_bin *rp = file->private_data;
919         // struct mon_bus* mbus = rp->r.m_bus;
920         int ret = 0;
921         struct mon_bin_hdr *ep;
922         unsigned long flags;
923
924         switch (cmd) {
925
926         case MON_IOCQ_URB_LEN:
927                 /*
928                  * N.B. This only returns the size of data, without the header.
929                  */
930                 spin_lock_irqsave(&rp->b_lock, flags);
931                 if (!MON_RING_EMPTY(rp)) {
932                         ep = MON_OFF2HDR(rp, rp->b_out);
933                         ret = ep->len_cap;
934                 }
935                 spin_unlock_irqrestore(&rp->b_lock, flags);
936                 break;
937
938         case MON_IOCQ_RING_SIZE:
939                 ret = rp->b_size;
940                 break;
941
942         case MON_IOCT_RING_SIZE:
943                 /*
944                  * Changing the buffer size will flush it's contents; the new
945                  * buffer is allocated before releasing the old one to be sure
946                  * the device will stay functional also in case of memory
947                  * pressure.
948                  */
949                 {
950                 int size;
951                 struct mon_pgmap *vec;
952
953                 if (arg < BUFF_MIN || arg > BUFF_MAX)
954                         return -EINVAL;
955
956                 size = CHUNK_ALIGN(arg);
957                 if ((vec = kzalloc(sizeof(struct mon_pgmap) * (size/CHUNK_SIZE),
958                     GFP_KERNEL)) == NULL) {
959                         ret = -ENOMEM;
960                         break;
961                 }
962
963                 ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
964                 if (ret < 0) {
965                         kfree(vec);
966                         break;
967                 }
968
969                 mutex_lock(&rp->fetch_lock);
970                 spin_lock_irqsave(&rp->b_lock, flags);
971                 mon_free_buff(rp->b_vec, size/CHUNK_SIZE);
972                 kfree(rp->b_vec);
973                 rp->b_vec  = vec;
974                 rp->b_size = size;
975                 rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
976                 rp->cnt_lost = 0;
977                 spin_unlock_irqrestore(&rp->b_lock, flags);
978                 mutex_unlock(&rp->fetch_lock);
979                 }
980                 break;
981
982         case MON_IOCH_MFLUSH:
983                 ret = mon_bin_flush(rp, arg);
984                 break;
985
986         case MON_IOCX_GET:
987         case MON_IOCX_GETX:
988                 {
989                 struct mon_bin_get getb;
990
991                 if (copy_from_user(&getb, (void __user *)arg,
992                                             sizeof(struct mon_bin_get)))
993                         return -EFAULT;
994
995                 if (getb.alloc > 0x10000000)    /* Want to cast to u32 */
996                         return -EINVAL;
997                 ret = mon_bin_get_event(file, rp, getb.hdr,
998                     (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
999                     getb.data, (unsigned int)getb.alloc);
1000                 }
1001                 break;
1002
1003         case MON_IOCX_MFETCH:
1004                 {
1005                 struct mon_bin_mfetch mfetch;
1006                 struct mon_bin_mfetch __user *uptr;
1007
1008                 uptr = (struct mon_bin_mfetch __user *)arg;
1009
1010                 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1011                         return -EFAULT;
1012
1013                 if (mfetch.nflush) {
1014                         ret = mon_bin_flush(rp, mfetch.nflush);
1015                         if (ret < 0)
1016                                 return ret;
1017                         if (put_user(ret, &uptr->nflush))
1018                                 return -EFAULT;
1019                 }
1020                 ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1021                 if (ret < 0)
1022                         return ret;
1023                 if (put_user(ret, &uptr->nfetch))
1024                         return -EFAULT;
1025                 ret = 0;
1026                 }
1027                 break;
1028
1029         case MON_IOCG_STATS: {
1030                 struct mon_bin_stats __user *sp;
1031                 unsigned int nevents;
1032                 unsigned int ndropped;
1033
1034                 spin_lock_irqsave(&rp->b_lock, flags);
1035                 ndropped = rp->cnt_lost;
1036                 rp->cnt_lost = 0;
1037                 spin_unlock_irqrestore(&rp->b_lock, flags);
1038                 nevents = mon_bin_queued(rp);
1039
1040                 sp = (struct mon_bin_stats __user *)arg;
1041                 if (put_user(rp->cnt_lost, &sp->dropped))
1042                         return -EFAULT;
1043                 if (put_user(nevents, &sp->queued))
1044                         return -EFAULT;
1045
1046                 }
1047                 break;
1048
1049         default:
1050                 return -ENOTTY;
1051         }
1052
1053         return ret;
1054 }
1055
1056 #ifdef CONFIG_COMPAT
1057 static long mon_bin_compat_ioctl(struct file *file,
1058     unsigned int cmd, unsigned long arg)
1059 {
1060         struct mon_reader_bin *rp = file->private_data;
1061         int ret;
1062
1063         switch (cmd) {
1064
1065         case MON_IOCX_GET32:
1066         case MON_IOCX_GETX32:
1067                 {
1068                 struct mon_bin_get32 getb;
1069
1070                 if (copy_from_user(&getb, (void __user *)arg,
1071                                             sizeof(struct mon_bin_get32)))
1072                         return -EFAULT;
1073
1074                 ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
1075                     (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
1076                     compat_ptr(getb.data32), getb.alloc32);
1077                 if (ret < 0)
1078                         return ret;
1079                 }
1080                 return 0;
1081
1082         case MON_IOCX_MFETCH32:
1083                 {
1084                 struct mon_bin_mfetch32 mfetch;
1085                 struct mon_bin_mfetch32 __user *uptr;
1086
1087                 uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1088
1089                 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1090                         return -EFAULT;
1091
1092                 if (mfetch.nflush32) {
1093                         ret = mon_bin_flush(rp, mfetch.nflush32);
1094                         if (ret < 0)
1095                                 return ret;
1096                         if (put_user(ret, &uptr->nflush32))
1097                                 return -EFAULT;
1098                 }
1099                 ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1100                     mfetch.nfetch32);
1101                 if (ret < 0)
1102                         return ret;
1103                 if (put_user(ret, &uptr->nfetch32))
1104                         return -EFAULT;
1105                 }
1106                 return 0;
1107
1108         case MON_IOCG_STATS:
1109                 return mon_bin_ioctl(NULL, file, cmd,
1110                                             (unsigned long) compat_ptr(arg));
1111
1112         case MON_IOCQ_URB_LEN:
1113         case MON_IOCQ_RING_SIZE:
1114         case MON_IOCT_RING_SIZE:
1115         case MON_IOCH_MFLUSH:
1116                 return mon_bin_ioctl(NULL, file, cmd, arg);
1117
1118         default:
1119                 ;
1120         }
1121         return -ENOTTY;
1122 }
1123 #endif /* CONFIG_COMPAT */
1124
1125 static unsigned int
1126 mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1127 {
1128         struct mon_reader_bin *rp = file->private_data;
1129         unsigned int mask = 0;
1130         unsigned long flags;
1131
1132         if (file->f_mode & FMODE_READ)
1133                 poll_wait(file, &rp->b_wait, wait);
1134
1135         spin_lock_irqsave(&rp->b_lock, flags);
1136         if (!MON_RING_EMPTY(rp))
1137                 mask |= POLLIN | POLLRDNORM;    /* readable */
1138         spin_unlock_irqrestore(&rp->b_lock, flags);
1139         return mask;
1140 }
1141
1142 /*
1143  * open and close: just keep track of how many times the device is
1144  * mapped, to use the proper memory allocation function.
1145  */
1146 static void mon_bin_vma_open(struct vm_area_struct *vma)
1147 {
1148         struct mon_reader_bin *rp = vma->vm_private_data;
1149         rp->mmap_active++;
1150 }
1151
1152 static void mon_bin_vma_close(struct vm_area_struct *vma)
1153 {
1154         struct mon_reader_bin *rp = vma->vm_private_data;
1155         rp->mmap_active--;
1156 }
1157
1158 /*
1159  * Map ring pages to user space.
1160  */
1161 static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1162 {
1163         struct mon_reader_bin *rp = vma->vm_private_data;
1164         unsigned long offset, chunk_idx;
1165         struct page *pageptr;
1166
1167         offset = vmf->pgoff << PAGE_SHIFT;
1168         if (offset >= rp->b_size)
1169                 return VM_FAULT_SIGBUS;
1170         chunk_idx = offset / CHUNK_SIZE;
1171         pageptr = rp->b_vec[chunk_idx].pg;
1172         get_page(pageptr);
1173         vmf->page = pageptr;
1174         return 0;
1175 }
1176
1177 static const struct vm_operations_struct mon_bin_vm_ops = {
1178         .open =     mon_bin_vma_open,
1179         .close =    mon_bin_vma_close,
1180         .fault =    mon_bin_vma_fault,
1181 };
1182
1183 static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1184 {
1185         /* don't do anything here: "fault" will set up page table entries */
1186         vma->vm_ops = &mon_bin_vm_ops;
1187         vma->vm_flags |= VM_RESERVED;
1188         vma->vm_private_data = filp->private_data;
1189         mon_bin_vma_open(vma);
1190         return 0;
1191 }
1192
1193 static const struct file_operations mon_fops_binary = {
1194         .owner =        THIS_MODULE,
1195         .open =         mon_bin_open,
1196         .llseek =       no_llseek,
1197         .read =         mon_bin_read,
1198         /* .write =     mon_text_write, */
1199         .poll =         mon_bin_poll,
1200         .ioctl =        mon_bin_ioctl,
1201 #ifdef CONFIG_COMPAT
1202         .compat_ioctl = mon_bin_compat_ioctl,
1203 #endif
1204         .release =      mon_bin_release,
1205         .mmap =         mon_bin_mmap,
1206 };
1207
1208 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1209 {
1210         DECLARE_WAITQUEUE(waita, current);
1211         unsigned long flags;
1212
1213         add_wait_queue(&rp->b_wait, &waita);
1214         set_current_state(TASK_INTERRUPTIBLE);
1215
1216         spin_lock_irqsave(&rp->b_lock, flags);
1217         while (MON_RING_EMPTY(rp)) {
1218                 spin_unlock_irqrestore(&rp->b_lock, flags);
1219
1220                 if (file->f_flags & O_NONBLOCK) {
1221                         set_current_state(TASK_RUNNING);
1222                         remove_wait_queue(&rp->b_wait, &waita);
1223                         return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1224                 }
1225                 schedule();
1226                 if (signal_pending(current)) {
1227                         remove_wait_queue(&rp->b_wait, &waita);
1228                         return -EINTR;
1229                 }
1230                 set_current_state(TASK_INTERRUPTIBLE);
1231
1232                 spin_lock_irqsave(&rp->b_lock, flags);
1233         }
1234         spin_unlock_irqrestore(&rp->b_lock, flags);
1235
1236         set_current_state(TASK_RUNNING);
1237         remove_wait_queue(&rp->b_wait, &waita);
1238         return 0;
1239 }
1240
1241 static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1242 {
1243         int n;
1244         unsigned long vaddr;
1245
1246         for (n = 0; n < npages; n++) {
1247                 vaddr = get_zeroed_page(GFP_KERNEL);
1248                 if (vaddr == 0) {
1249                         while (n-- != 0)
1250                                 free_page((unsigned long) map[n].ptr);
1251                         return -ENOMEM;
1252                 }
1253                 map[n].ptr = (unsigned char *) vaddr;
1254                 map[n].pg = virt_to_page((void *) vaddr);
1255         }
1256         return 0;
1257 }
1258
1259 static void mon_free_buff(struct mon_pgmap *map, int npages)
1260 {
1261         int n;
1262
1263         for (n = 0; n < npages; n++)
1264                 free_page((unsigned long) map[n].ptr);
1265 }
1266
1267 int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1268 {
1269         struct device *dev;
1270         unsigned minor = ubus? ubus->busnum: 0;
1271
1272         if (minor >= MON_BIN_MAX_MINOR)
1273                 return 0;
1274
1275         dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
1276                             MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1277                             "usbmon%d", minor);
1278         if (IS_ERR(dev))
1279                 return 0;
1280
1281         mbus->classdev = dev;
1282         return 1;
1283 }
1284
1285 void mon_bin_del(struct mon_bus *mbus)
1286 {
1287         device_destroy(mon_bin_class, mbus->classdev->devt);
1288 }
1289
1290 int __init mon_bin_init(void)
1291 {
1292         int rc;
1293
1294         mon_bin_class = class_create(THIS_MODULE, "usbmon");
1295         if (IS_ERR(mon_bin_class)) {
1296                 rc = PTR_ERR(mon_bin_class);
1297                 goto err_class;
1298         }
1299
1300         rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1301         if (rc < 0)
1302                 goto err_dev;
1303
1304         cdev_init(&mon_bin_cdev, &mon_fops_binary);
1305         mon_bin_cdev.owner = THIS_MODULE;
1306
1307         rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1308         if (rc < 0)
1309                 goto err_add;
1310
1311         return 0;
1312
1313 err_add:
1314         unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1315 err_dev:
1316         class_destroy(mon_bin_class);
1317 err_class:
1318         return rc;
1319 }
1320
1321 void mon_bin_exit(void)
1322 {
1323         cdev_del(&mon_bin_cdev);
1324         unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1325         class_destroy(mon_bin_class);
1326 }