mon_bin.c 32 KB

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