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