mon_bin.c 33 KB

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