mon_bin.c 34 KB

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