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 * But it returns the new offset for repeated application.
224 */
225 unsigned int mon_copy_to_buff(const struct mon_reader_bin *this,
226 unsigned int off, const unsigned char *from, unsigned int length)
227 {
228 unsigned int step_len;
229 unsigned char *buf;
230 unsigned int in_page;
231
232 while (length) {
233 /*
234 * Determine step_len.
235 */
236 step_len = length;
237 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
238 if (in_page < step_len)
239 step_len = in_page;
240
241 /*
242 * Copy data and advance pointers.
243 */
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;
249 }
250 return off;
251 }
252
253 /*
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.
256 */
257 static int copy_from_buf(const struct mon_reader_bin *this, unsigned int off,
258 char __user *to, int length)
259 {
260 unsigned int step_len;
261 unsigned char *buf;
262 unsigned int in_page;
263
264 while (length) {
265 /*
266 * Determine step_len.
267 */
268 step_len = length;
269 in_page = CHUNK_SIZE - (off & (CHUNK_SIZE-1));
270 if (in_page < step_len)
271 step_len = in_page;
272
273 /*
274 * Copy data and advance pointers.
275 */
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;
282 }
283 return 0;
284 }
285
286 /*
287 * Allocate an (aligned) area in the buffer.
288 * This is called under b_lock.
289 * Returns ~0 on failure.
290 */
291 static unsigned int mon_buff_area_alloc(struct mon_reader_bin *rp,
292 unsigned int size)
293 {
294 unsigned int offset;
295
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;
304 }
305
306 /*
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).
310 *
311 * Currently, we always have the header stuck with the data, although
312 * it is not strictly speaking necessary.
313 *
314 * When a buffer would wrap, we place a filler packet to mark the space.
315 */
316 static unsigned int mon_buff_area_alloc_contiguous(struct mon_reader_bin *rp,
317 unsigned int size)
318 {
319 unsigned int offset;
320 unsigned int fill_size;
321
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) {
326 /*
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.
330 */
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);
335
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;
347 }
348 return offset;
349 }
350
351 /*
352 * Return a few (kilo-)bytes to the head of the buffer.
353 * This is used if a data fetch fails.
354 */
355 static void mon_buff_area_shrink(struct mon_reader_bin *rp, unsigned int size)
356 {
357
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;
363 }
364
365 /*
366 * This has to be called under both b_lock and fetch_lock, because
367 * it accesses both b_cnt and b_out.
368 */
369 static void mon_buff_area_free(struct mon_reader_bin *rp, unsigned int size)
370 {
371
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;
376 }
377
378 static void mon_buff_area_fill(const struct mon_reader_bin *rp,
379 unsigned int offset, unsigned int size)
380 {
381 struct mon_bin_hdr *ep;
382
383 ep = MON_OFF2HDR(rp, offset);
384 memset(ep, 0, PKT_SIZE);
385 ep->type = '@';
386 ep->len_cap = size - PKT_SIZE;
387 }
388
389 static inline char mon_bin_get_setup(unsigned char *setupb,
390 const struct urb *urb, char ev_type)
391 {
392
393 if (urb->setup_packet == NULL)
394 return 'Z';
395 memcpy(setupb, urb->setup_packet, SETUP_LEN);
396 return 0;
397 }
398
399 static char mon_bin_get_data(const struct mon_reader_bin *rp,
400 unsigned int offset, struct urb *urb, unsigned int length)
401 {
402
403 if (urb->dev->bus->uses_dma &&
404 (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
405 mon_dmapeek_vec(rp, offset, urb->transfer_dma, length);
406 return 0;
407 }
408
409 if (urb->transfer_buffer == NULL)
410 return 'Z';
411
412 mon_copy_to_buff(rp, offset, urb->transfer_buffer, length);
413 return 0;
414 }
415
416 static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
417 unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
418 {
419 struct mon_bin_isodesc *dp;
420 struct usb_iso_packet_descriptor *fp;
421
422 fp = urb->iso_frame_desc;
423 while (ndesc-- != 0) {
424 dp = (struct mon_bin_isodesc *)
425 (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
426 dp->iso_status = fp->status;
427 dp->iso_off = fp->offset;
428 dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
429 dp->_pad = 0;
430 if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
431 offset = 0;
432 fp++;
433 }
434 }
435
436 static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
437 char ev_type, int status)
438 {
439 const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
440 unsigned long flags;
441 struct timeval ts;
442 unsigned int urb_length;
443 unsigned int offset;
444 unsigned int length;
445 unsigned int delta;
446 unsigned int ndesc, lendesc;
447 unsigned char dir;
448 struct mon_bin_hdr *ep;
449 char data_tag = 0;
450
451 do_gettimeofday(&ts);
452
453 spin_lock_irqsave(&rp->b_lock, flags);
454
455 /*
456 * Find the maximum allowable length, then allocate space.
457 */
458 if (usb_endpoint_xfer_isoc(epd)) {
459 if (urb->number_of_packets < 0) {
460 ndesc = 0;
461 } else if (urb->number_of_packets >= ISODESC_MAX) {
462 ndesc = ISODESC_MAX;
463 } else {
464 ndesc = urb->number_of_packets;
465 }
466 } else {
467 ndesc = 0;
468 }
469 lendesc = ndesc*sizeof(struct mon_bin_isodesc);
470
471 urb_length = (ev_type == 'S') ?
472 urb->transfer_buffer_length : urb->actual_length;
473 length = urb_length;
474
475 if (length >= rp->b_size/5)
476 length = rp->b_size/5;
477
478 if (usb_urb_dir_in(urb)) {
479 if (ev_type == 'S') {
480 length = 0;
481 data_tag = '<';
482 }
483 /* Cannot rely on endpoint number in case of control ep.0 */
484 dir = USB_DIR_IN;
485 } else {
486 if (ev_type == 'C') {
487 length = 0;
488 data_tag = '>';
489 }
490 dir = 0;
491 }
492
493 if (rp->mmap_active) {
494 offset = mon_buff_area_alloc_contiguous(rp,
495 length + PKT_SIZE + lendesc);
496 } else {
497 offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
498 }
499 if (offset == ~0) {
500 rp->cnt_lost++;
501 spin_unlock_irqrestore(&rp->b_lock, flags);
502 return;
503 }
504
505 ep = MON_OFF2HDR(rp, offset);
506 if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
507
508 /*
509 * Fill the allocated area.
510 */
511 memset(ep, 0, PKT_SIZE);
512 ep->type = ev_type;
513 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
514 ep->epnum = dir | usb_endpoint_num(epd);
515 ep->devnum = urb->dev->devnum;
516 ep->busnum = urb->dev->bus->busnum;
517 ep->id = (unsigned long) urb;
518 ep->ts_sec = ts.tv_sec;
519 ep->ts_usec = ts.tv_usec;
520 ep->status = status;
521 ep->len_urb = urb_length;
522 ep->len_cap = length + lendesc;
523 ep->xfer_flags = urb->transfer_flags;
524
525 if (usb_endpoint_xfer_int(epd)) {
526 ep->interval = urb->interval;
527 } else if (usb_endpoint_xfer_isoc(epd)) {
528 ep->interval = urb->interval;
529 ep->start_frame = urb->start_frame;
530 ep->s.iso.error_count = urb->error_count;
531 ep->s.iso.numdesc = urb->number_of_packets;
532 }
533
534 if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
535 ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
536 } else {
537 ep->flag_setup = '-';
538 }
539
540 if (ndesc != 0) {
541 ep->ndesc = ndesc;
542 mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
543 if ((offset += lendesc) >= rp->b_size)
544 offset -= rp->b_size;
545 }
546
547 if (length != 0) {
548 ep->flag_data = mon_bin_get_data(rp, offset, urb, length);
549 if (ep->flag_data != 0) { /* Yes, it's 0x00, not '' */
550 delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
551 ep->len_cap -= length;
552 delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
553 mon_buff_area_shrink(rp, delta);
554 }
555 } else {
556 ep->flag_data = data_tag;
557 }
558
559 spin_unlock_irqrestore(&rp->b_lock, flags);
560
561 wake_up(&rp->b_wait);
562 }
563
564 static void mon_bin_submit(void *data, struct urb *urb)
565 {
566 struct mon_reader_bin *rp = data;
567 mon_bin_event(rp, urb, 'S', -EINPROGRESS);
568 }
569
570 static void mon_bin_complete(void *data, struct urb *urb, int status)
571 {
572 struct mon_reader_bin *rp = data;
573 mon_bin_event(rp, urb, 'C', status);
574 }
575
576 static void mon_bin_error(void *data, struct urb *urb, int error)
577 {
578 struct mon_reader_bin *rp = data;
579 unsigned long flags;
580 unsigned int offset;
581 struct mon_bin_hdr *ep;
582
583 spin_lock_irqsave(&rp->b_lock, flags);
584
585 offset = mon_buff_area_alloc(rp, PKT_SIZE);
586 if (offset == ~0) {
587 /* Not incrementing cnt_lost. Just because. */
588 spin_unlock_irqrestore(&rp->b_lock, flags);
589 return;
590 }
591
592 ep = MON_OFF2HDR(rp, offset);
593
594 memset(ep, 0, PKT_SIZE);
595 ep->type = 'E';
596 ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
597 ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
598 ep->epnum |= usb_endpoint_num(&urb->ep->desc);
599 ep->devnum = urb->dev->devnum;
600 ep->busnum = urb->dev->bus->busnum;
601 ep->id = (unsigned long) urb;
602 ep->status = error;
603
604 ep->flag_setup = '-';
605 ep->flag_data = 'E';
606
607 spin_unlock_irqrestore(&rp->b_lock, flags);
608
609 wake_up(&rp->b_wait);
610 }
611
612 static int mon_bin_open(struct inode *inode, struct file *file)
613 {
614 struct mon_bus *mbus;
615 struct mon_reader_bin *rp;
616 size_t size;
617 int rc;
618
619 lock_kernel();
620 mutex_lock(&mon_lock);
621 if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) {
622 mutex_unlock(&mon_lock);
623 unlock_kernel();
624 return -ENODEV;
625 }
626 if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
627 printk(KERN_ERR TAG ": consistency error on open\n");
628 mutex_unlock(&mon_lock);
629 unlock_kernel();
630 return -ENODEV;
631 }
632
633 rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
634 if (rp == NULL) {
635 rc = -ENOMEM;
636 goto err_alloc;
637 }
638 spin_lock_init(&rp->b_lock);
639 init_waitqueue_head(&rp->b_wait);
640 mutex_init(&rp->fetch_lock);
641
642 rp->b_size = BUFF_DFL;
643
644 size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
645 if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
646 rc = -ENOMEM;
647 goto err_allocvec;
648 }
649
650 if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
651 goto err_allocbuff;
652
653 rp->r.m_bus = mbus;
654 rp->r.r_data = rp;
655 rp->r.rnf_submit = mon_bin_submit;
656 rp->r.rnf_error = mon_bin_error;
657 rp->r.rnf_complete = mon_bin_complete;
658
659 mon_reader_add(mbus, &rp->r);
660
661 file->private_data = rp;
662 mutex_unlock(&mon_lock);
663 unlock_kernel();
664 return 0;
665
666 err_allocbuff:
667 kfree(rp->b_vec);
668 err_allocvec:
669 kfree(rp);
670 err_alloc:
671 mutex_unlock(&mon_lock);
672 unlock_kernel();
673 return rc;
674 }
675
676 /*
677 * Extract an event from buffer and copy it to user space.
678 * Wait if there is no event ready.
679 * Returns zero or error.
680 */
681 static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
682 struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
683 void __user *data, unsigned int nbytes)
684 {
685 unsigned long flags;
686 struct mon_bin_hdr *ep;
687 size_t step_len;
688 unsigned int offset;
689 int rc;
690
691 mutex_lock(&rp->fetch_lock);
692
693 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
694 mutex_unlock(&rp->fetch_lock);
695 return rc;
696 }
697
698 ep = MON_OFF2HDR(rp, rp->b_out);
699
700 if (copy_to_user(hdr, ep, hdrbytes)) {
701 mutex_unlock(&rp->fetch_lock);
702 return -EFAULT;
703 }
704
705 step_len = min(ep->len_cap, nbytes);
706 if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
707
708 if (copy_from_buf(rp, offset, data, step_len)) {
709 mutex_unlock(&rp->fetch_lock);
710 return -EFAULT;
711 }
712
713 spin_lock_irqsave(&rp->b_lock, flags);
714 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
715 spin_unlock_irqrestore(&rp->b_lock, flags);
716 rp->b_read = 0;
717
718 mutex_unlock(&rp->fetch_lock);
719 return 0;
720 }
721
722 static int mon_bin_release(struct inode *inode, struct file *file)
723 {
724 struct mon_reader_bin *rp = file->private_data;
725 struct mon_bus* mbus = rp->r.m_bus;
726
727 mutex_lock(&mon_lock);
728
729 if (mbus->nreaders <= 0) {
730 printk(KERN_ERR TAG ": consistency error on close\n");
731 mutex_unlock(&mon_lock);
732 return 0;
733 }
734 mon_reader_del(mbus, &rp->r);
735
736 mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
737 kfree(rp->b_vec);
738 kfree(rp);
739
740 mutex_unlock(&mon_lock);
741 return 0;
742 }
743
744 static ssize_t mon_bin_read(struct file *file, char __user *buf,
745 size_t nbytes, loff_t *ppos)
746 {
747 struct mon_reader_bin *rp = file->private_data;
748 unsigned int hdrbytes = PKT_SZ_API0;
749 unsigned long flags;
750 struct mon_bin_hdr *ep;
751 unsigned int offset;
752 size_t step_len;
753 char *ptr;
754 ssize_t done = 0;
755 int rc;
756
757 mutex_lock(&rp->fetch_lock);
758
759 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
760 mutex_unlock(&rp->fetch_lock);
761 return rc;
762 }
763
764 ep = MON_OFF2HDR(rp, rp->b_out);
765
766 if (rp->b_read < hdrbytes) {
767 step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
768 ptr = ((char *)ep) + rp->b_read;
769 if (step_len && copy_to_user(buf, ptr, step_len)) {
770 mutex_unlock(&rp->fetch_lock);
771 return -EFAULT;
772 }
773 nbytes -= step_len;
774 buf += step_len;
775 rp->b_read += step_len;
776 done += step_len;
777 }
778
779 if (rp->b_read >= hdrbytes) {
780 step_len = ep->len_cap;
781 step_len -= rp->b_read - hdrbytes;
782 if (step_len > nbytes)
783 step_len = nbytes;
784 offset = rp->b_out + PKT_SIZE;
785 offset += rp->b_read - hdrbytes;
786 if (offset >= rp->b_size)
787 offset -= rp->b_size;
788 if (copy_from_buf(rp, offset, buf, step_len)) {
789 mutex_unlock(&rp->fetch_lock);
790 return -EFAULT;
791 }
792 nbytes -= step_len;
793 buf += step_len;
794 rp->b_read += step_len;
795 done += step_len;
796 }
797
798 /*
799 * Check if whole packet was read, and if so, jump to the next one.
800 */
801 if (rp->b_read >= hdrbytes + ep->len_cap) {
802 spin_lock_irqsave(&rp->b_lock, flags);
803 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
804 spin_unlock_irqrestore(&rp->b_lock, flags);
805 rp->b_read = 0;
806 }
807
808 mutex_unlock(&rp->fetch_lock);
809 return done;
810 }
811
812 /*
813 * Remove at most nevents from chunked buffer.
814 * Returns the number of removed events.
815 */
816 static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
817 {
818 unsigned long flags;
819 struct mon_bin_hdr *ep;
820 int i;
821
822 mutex_lock(&rp->fetch_lock);
823 spin_lock_irqsave(&rp->b_lock, flags);
824 for (i = 0; i < nevents; ++i) {
825 if (MON_RING_EMPTY(rp))
826 break;
827
828 ep = MON_OFF2HDR(rp, rp->b_out);
829 mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
830 }
831 spin_unlock_irqrestore(&rp->b_lock, flags);
832 rp->b_read = 0;
833 mutex_unlock(&rp->fetch_lock);
834 return i;
835 }
836
837 /*
838 * Fetch at most max event offsets into the buffer and put them into vec.
839 * The events are usually freed later with mon_bin_flush.
840 * Return the effective number of events fetched.
841 */
842 static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
843 u32 __user *vec, unsigned int max)
844 {
845 unsigned int cur_out;
846 unsigned int bytes, avail;
847 unsigned int size;
848 unsigned int nevents;
849 struct mon_bin_hdr *ep;
850 unsigned long flags;
851 int rc;
852
853 mutex_lock(&rp->fetch_lock);
854
855 if ((rc = mon_bin_wait_event(file, rp)) < 0) {
856 mutex_unlock(&rp->fetch_lock);
857 return rc;
858 }
859
860 spin_lock_irqsave(&rp->b_lock, flags);
861 avail = rp->b_cnt;
862 spin_unlock_irqrestore(&rp->b_lock, flags);
863
864 cur_out = rp->b_out;
865 nevents = 0;
866 bytes = 0;
867 while (bytes < avail) {
868 if (nevents >= max)
869 break;
870
871 ep = MON_OFF2HDR(rp, cur_out);
872 if (put_user(cur_out, &vec[nevents])) {
873 mutex_unlock(&rp->fetch_lock);
874 return -EFAULT;
875 }
876
877 nevents++;
878 size = ep->len_cap + PKT_SIZE;
879 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
880 if ((cur_out += size) >= rp->b_size)
881 cur_out -= rp->b_size;
882 bytes += size;
883 }
884
885 mutex_unlock(&rp->fetch_lock);
886 return nevents;
887 }
888
889 /*
890 * Count events. This is almost the same as the above mon_bin_fetch,
891 * only we do not store offsets into user vector, and we have no limit.
892 */
893 static int mon_bin_queued(struct mon_reader_bin *rp)
894 {
895 unsigned int cur_out;
896 unsigned int bytes, avail;
897 unsigned int size;
898 unsigned int nevents;
899 struct mon_bin_hdr *ep;
900 unsigned long flags;
901
902 mutex_lock(&rp->fetch_lock);
903
904 spin_lock_irqsave(&rp->b_lock, flags);
905 avail = rp->b_cnt;
906 spin_unlock_irqrestore(&rp->b_lock, flags);
907
908 cur_out = rp->b_out;
909 nevents = 0;
910 bytes = 0;
911 while (bytes < avail) {
912 ep = MON_OFF2HDR(rp, cur_out);
913
914 nevents++;
915 size = ep->len_cap + PKT_SIZE;
916 size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
917 if ((cur_out += size) >= rp->b_size)
918 cur_out -= rp->b_size;
919 bytes += size;
920 }
921
922 mutex_unlock(&rp->fetch_lock);
923 return nevents;
924 }
925
926 /*
927 */
928 static int mon_bin_ioctl(struct inode *inode, struct file *file,
929 unsigned int cmd, unsigned long arg)
930 {
931 struct mon_reader_bin *rp = file->private_data;
932 // struct mon_bus* mbus = rp->r.m_bus;
933 int ret = 0;
934 struct mon_bin_hdr *ep;
935 unsigned long flags;
936
937 switch (cmd) {
938
939 case MON_IOCQ_URB_LEN:
940 /*
941 * N.B. This only returns the size of data, without the header.
942 */
943 spin_lock_irqsave(&rp->b_lock, flags);
944 if (!MON_RING_EMPTY(rp)) {
945 ep = MON_OFF2HDR(rp, rp->b_out);
946 ret = ep->len_cap;
947 }
948 spin_unlock_irqrestore(&rp->b_lock, flags);
949 break;
950
951 case MON_IOCQ_RING_SIZE:
952 ret = rp->b_size;
953 break;
954
955 case MON_IOCT_RING_SIZE:
956 /*
957 * Changing the buffer size will flush it's contents; the new
958 * buffer is allocated before releasing the old one to be sure
959 * the device will stay functional also in case of memory
960 * pressure.
961 */
962 {
963 int size;
964 struct mon_pgmap *vec;
965
966 if (arg < BUFF_MIN || arg > BUFF_MAX)
967 return -EINVAL;
968
969 size = CHUNK_ALIGN(arg);
970 if ((vec = kzalloc(sizeof(struct mon_pgmap) * (size/CHUNK_SIZE),
971 GFP_KERNEL)) == NULL) {
972 ret = -ENOMEM;
973 break;
974 }
975
976 ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
977 if (ret < 0) {
978 kfree(vec);
979 break;
980 }
981
982 mutex_lock(&rp->fetch_lock);
983 spin_lock_irqsave(&rp->b_lock, flags);
984 mon_free_buff(rp->b_vec, size/CHUNK_SIZE);
985 kfree(rp->b_vec);
986 rp->b_vec = vec;
987 rp->b_size = size;
988 rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
989 rp->cnt_lost = 0;
990 spin_unlock_irqrestore(&rp->b_lock, flags);
991 mutex_unlock(&rp->fetch_lock);
992 }
993 break;
994
995 case MON_IOCH_MFLUSH:
996 ret = mon_bin_flush(rp, arg);
997 break;
998
999 case MON_IOCX_GET:
1000 case MON_IOCX_GETX:
1001 {
1002 struct mon_bin_get getb;
1003
1004 if (copy_from_user(&getb, (void __user *)arg,
1005 sizeof(struct mon_bin_get)))
1006 return -EFAULT;
1007
1008 if (getb.alloc > 0x10000000) /* Want to cast to u32 */
1009 return -EINVAL;
1010 ret = mon_bin_get_event(file, rp, getb.hdr,
1011 (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
1012 getb.data, (unsigned int)getb.alloc);
1013 }
1014 break;
1015
1016 case MON_IOCX_MFETCH:
1017 {
1018 struct mon_bin_mfetch mfetch;
1019 struct mon_bin_mfetch __user *uptr;
1020
1021 uptr = (struct mon_bin_mfetch __user *)arg;
1022
1023 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1024 return -EFAULT;
1025
1026 if (mfetch.nflush) {
1027 ret = mon_bin_flush(rp, mfetch.nflush);
1028 if (ret < 0)
1029 return ret;
1030 if (put_user(ret, &uptr->nflush))
1031 return -EFAULT;
1032 }
1033 ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
1034 if (ret < 0)
1035 return ret;
1036 if (put_user(ret, &uptr->nfetch))
1037 return -EFAULT;
1038 ret = 0;
1039 }
1040 break;
1041
1042 case MON_IOCG_STATS: {
1043 struct mon_bin_stats __user *sp;
1044 unsigned int nevents;
1045 unsigned int ndropped;
1046
1047 spin_lock_irqsave(&rp->b_lock, flags);
1048 ndropped = rp->cnt_lost;
1049 rp->cnt_lost = 0;
1050 spin_unlock_irqrestore(&rp->b_lock, flags);
1051 nevents = mon_bin_queued(rp);
1052
1053 sp = (struct mon_bin_stats __user *)arg;
1054 if (put_user(rp->cnt_lost, &sp->dropped))
1055 return -EFAULT;
1056 if (put_user(nevents, &sp->queued))
1057 return -EFAULT;
1058
1059 }
1060 break;
1061
1062 default:
1063 return -ENOTTY;
1064 }
1065
1066 return ret;
1067 }
1068
1069 #ifdef CONFIG_COMPAT
1070 static long mon_bin_compat_ioctl(struct file *file,
1071 unsigned int cmd, unsigned long arg)
1072 {
1073 struct mon_reader_bin *rp = file->private_data;
1074 int ret;
1075
1076 switch (cmd) {
1077
1078 case MON_IOCX_GET32:
1079 case MON_IOCX_GETX32:
1080 {
1081 struct mon_bin_get32 getb;
1082
1083 if (copy_from_user(&getb, (void __user *)arg,
1084 sizeof(struct mon_bin_get32)))
1085 return -EFAULT;
1086
1087 ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
1088 (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
1089 compat_ptr(getb.data32), getb.alloc32);
1090 if (ret < 0)
1091 return ret;
1092 }
1093 return 0;
1094
1095 case MON_IOCX_MFETCH32:
1096 {
1097 struct mon_bin_mfetch32 mfetch;
1098 struct mon_bin_mfetch32 __user *uptr;
1099
1100 uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
1101
1102 if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
1103 return -EFAULT;
1104
1105 if (mfetch.nflush32) {
1106 ret = mon_bin_flush(rp, mfetch.nflush32);
1107 if (ret < 0)
1108 return ret;
1109 if (put_user(ret, &uptr->nflush32))
1110 return -EFAULT;
1111 }
1112 ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
1113 mfetch.nfetch32);
1114 if (ret < 0)
1115 return ret;
1116 if (put_user(ret, &uptr->nfetch32))
1117 return -EFAULT;
1118 }
1119 return 0;
1120
1121 case MON_IOCG_STATS:
1122 return mon_bin_ioctl(NULL, file, cmd,
1123 (unsigned long) compat_ptr(arg));
1124
1125 case MON_IOCQ_URB_LEN:
1126 case MON_IOCQ_RING_SIZE:
1127 case MON_IOCT_RING_SIZE:
1128 case MON_IOCH_MFLUSH:
1129 return mon_bin_ioctl(NULL, file, cmd, arg);
1130
1131 default:
1132 ;
1133 }
1134 return -ENOTTY;
1135 }
1136 #endif /* CONFIG_COMPAT */
1137
1138 static unsigned int
1139 mon_bin_poll(struct file *file, struct poll_table_struct *wait)
1140 {
1141 struct mon_reader_bin *rp = file->private_data;
1142 unsigned int mask = 0;
1143 unsigned long flags;
1144
1145 if (file->f_mode & FMODE_READ)
1146 poll_wait(file, &rp->b_wait, wait);
1147
1148 spin_lock_irqsave(&rp->b_lock, flags);
1149 if (!MON_RING_EMPTY(rp))
1150 mask |= POLLIN | POLLRDNORM; /* readable */
1151 spin_unlock_irqrestore(&rp->b_lock, flags);
1152 return mask;
1153 }
1154
1155 /*
1156 * open and close: just keep track of how many times the device is
1157 * mapped, to use the proper memory allocation function.
1158 */
1159 static void mon_bin_vma_open(struct vm_area_struct *vma)
1160 {
1161 struct mon_reader_bin *rp = vma->vm_private_data;
1162 rp->mmap_active++;
1163 }
1164
1165 static void mon_bin_vma_close(struct vm_area_struct *vma)
1166 {
1167 struct mon_reader_bin *rp = vma->vm_private_data;
1168 rp->mmap_active--;
1169 }
1170
1171 /*
1172 * Map ring pages to user space.
1173 */
1174 static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1175 {
1176 struct mon_reader_bin *rp = vma->vm_private_data;
1177 unsigned long offset, chunk_idx;
1178 struct page *pageptr;
1179
1180 offset = vmf->pgoff << PAGE_SHIFT;
1181 if (offset >= rp->b_size)
1182 return VM_FAULT_SIGBUS;
1183 chunk_idx = offset / CHUNK_SIZE;
1184 pageptr = rp->b_vec[chunk_idx].pg;
1185 get_page(pageptr);
1186 vmf->page = pageptr;
1187 return 0;
1188 }
1189
1190 static struct vm_operations_struct mon_bin_vm_ops = {
1191 .open = mon_bin_vma_open,
1192 .close = mon_bin_vma_close,
1193 .fault = mon_bin_vma_fault,
1194 };
1195
1196 static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
1197 {
1198 /* don't do anything here: "fault" will set up page table entries */
1199 vma->vm_ops = &mon_bin_vm_ops;
1200 vma->vm_flags |= VM_RESERVED;
1201 vma->vm_private_data = filp->private_data;
1202 mon_bin_vma_open(vma);
1203 return 0;
1204 }
1205
1206 static const struct file_operations mon_fops_binary = {
1207 .owner = THIS_MODULE,
1208 .open = mon_bin_open,
1209 .llseek = no_llseek,
1210 .read = mon_bin_read,
1211 /* .write = mon_text_write, */
1212 .poll = mon_bin_poll,
1213 .ioctl = mon_bin_ioctl,
1214 #ifdef CONFIG_COMPAT
1215 .compat_ioctl = mon_bin_compat_ioctl,
1216 #endif
1217 .release = mon_bin_release,
1218 .mmap = mon_bin_mmap,
1219 };
1220
1221 static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
1222 {
1223 DECLARE_WAITQUEUE(waita, current);
1224 unsigned long flags;
1225
1226 add_wait_queue(&rp->b_wait, &waita);
1227 set_current_state(TASK_INTERRUPTIBLE);
1228
1229 spin_lock_irqsave(&rp->b_lock, flags);
1230 while (MON_RING_EMPTY(rp)) {
1231 spin_unlock_irqrestore(&rp->b_lock, flags);
1232
1233 if (file->f_flags & O_NONBLOCK) {
1234 set_current_state(TASK_RUNNING);
1235 remove_wait_queue(&rp->b_wait, &waita);
1236 return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
1237 }
1238 schedule();
1239 if (signal_pending(current)) {
1240 remove_wait_queue(&rp->b_wait, &waita);
1241 return -EINTR;
1242 }
1243 set_current_state(TASK_INTERRUPTIBLE);
1244
1245 spin_lock_irqsave(&rp->b_lock, flags);
1246 }
1247 spin_unlock_irqrestore(&rp->b_lock, flags);
1248
1249 set_current_state(TASK_RUNNING);
1250 remove_wait_queue(&rp->b_wait, &waita);
1251 return 0;
1252 }
1253
1254 static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1255 {
1256 int n;
1257 unsigned long vaddr;
1258
1259 for (n = 0; n < npages; n++) {
1260 vaddr = get_zeroed_page(GFP_KERNEL);
1261 if (vaddr == 0) {
1262 while (n-- != 0)
1263 free_page((unsigned long) map[n].ptr);
1264 return -ENOMEM;
1265 }
1266 map[n].ptr = (unsigned char *) vaddr;
1267 map[n].pg = virt_to_page((void *) vaddr);
1268 }
1269 return 0;
1270 }
1271
1272 static void mon_free_buff(struct mon_pgmap *map, int npages)
1273 {
1274 int n;
1275
1276 for (n = 0; n < npages; n++)
1277 free_page((unsigned long) map[n].ptr);
1278 }
1279
1280 int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
1281 {
1282 struct device *dev;
1283 unsigned minor = ubus? ubus->busnum: 0;
1284
1285 if (minor >= MON_BIN_MAX_MINOR)
1286 return 0;
1287
1288 dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
1289 MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
1290 "usbmon%d", minor);
1291 if (IS_ERR(dev))
1292 return 0;
1293
1294 mbus->classdev = dev;
1295 return 1;
1296 }
1297
1298 void mon_bin_del(struct mon_bus *mbus)
1299 {
1300 device_destroy(mon_bin_class, mbus->classdev->devt);
1301 }
1302
1303 int __init mon_bin_init(void)
1304 {
1305 int rc;
1306
1307 mon_bin_class = class_create(THIS_MODULE, "usbmon");
1308 if (IS_ERR(mon_bin_class)) {
1309 rc = PTR_ERR(mon_bin_class);
1310 goto err_class;
1311 }
1312
1313 rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
1314 if (rc < 0)
1315 goto err_dev;
1316
1317 cdev_init(&mon_bin_cdev, &mon_fops_binary);
1318 mon_bin_cdev.owner = THIS_MODULE;
1319
1320 rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
1321 if (rc < 0)
1322 goto err_add;
1323
1324 return 0;
1325
1326 err_add:
1327 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1328 err_dev:
1329 class_destroy(mon_bin_class);
1330 err_class:
1331 return rc;
1332 }
1333
1334 void mon_bin_exit(void)
1335 {
1336 cdev_del(&mon_bin_cdev);
1337 unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
1338 class_destroy(mon_bin_class);
1339 }
1340
|
This page was automatically generated by the
LXR engine.
|