mon_bin.c 33 KB

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