mon_bin.c 33 KB

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