mon_bin.c 33 KB

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