mon_bin.c 33 KB

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