mon_bin.c 34 KB

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