mon_bin.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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 <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->sg->nents != urb->num_sgs) {
  367. *flag = 'D';
  368. return length;
  369. }
  370. /* Copy up to the first non-addressable segment */
  371. for_each_sg(urb->sg->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. static void mon_bin_get_isodesc(const struct mon_reader_bin *rp,
  385. unsigned int offset, struct urb *urb, char ev_type, unsigned int ndesc)
  386. {
  387. struct mon_bin_isodesc *dp;
  388. struct usb_iso_packet_descriptor *fp;
  389. fp = urb->iso_frame_desc;
  390. while (ndesc-- != 0) {
  391. dp = (struct mon_bin_isodesc *)
  392. (rp->b_vec[offset / CHUNK_SIZE].ptr + offset % CHUNK_SIZE);
  393. dp->iso_status = fp->status;
  394. dp->iso_off = fp->offset;
  395. dp->iso_len = (ev_type == 'S') ? fp->length : fp->actual_length;
  396. dp->_pad = 0;
  397. if ((offset += sizeof(struct mon_bin_isodesc)) >= rp->b_size)
  398. offset = 0;
  399. fp++;
  400. }
  401. }
  402. static void mon_bin_event(struct mon_reader_bin *rp, struct urb *urb,
  403. char ev_type, int status)
  404. {
  405. const struct usb_endpoint_descriptor *epd = &urb->ep->desc;
  406. unsigned long flags;
  407. struct timeval ts;
  408. unsigned int urb_length;
  409. unsigned int offset;
  410. unsigned int length;
  411. unsigned int delta;
  412. unsigned int ndesc, lendesc;
  413. unsigned char dir;
  414. struct mon_bin_hdr *ep;
  415. char data_tag = 0;
  416. do_gettimeofday(&ts);
  417. spin_lock_irqsave(&rp->b_lock, flags);
  418. /*
  419. * Find the maximum allowable length, then allocate space.
  420. */
  421. if (usb_endpoint_xfer_isoc(epd)) {
  422. if (urb->number_of_packets < 0) {
  423. ndesc = 0;
  424. } else if (urb->number_of_packets >= ISODESC_MAX) {
  425. ndesc = ISODESC_MAX;
  426. } else {
  427. ndesc = urb->number_of_packets;
  428. }
  429. } else {
  430. ndesc = 0;
  431. }
  432. lendesc = ndesc*sizeof(struct mon_bin_isodesc);
  433. urb_length = (ev_type == 'S') ?
  434. urb->transfer_buffer_length : urb->actual_length;
  435. length = urb_length;
  436. if (length >= rp->b_size/5)
  437. length = rp->b_size/5;
  438. if (usb_urb_dir_in(urb)) {
  439. if (ev_type == 'S') {
  440. length = 0;
  441. data_tag = '<';
  442. }
  443. /* Cannot rely on endpoint number in case of control ep.0 */
  444. dir = USB_DIR_IN;
  445. } else {
  446. if (ev_type == 'C') {
  447. length = 0;
  448. data_tag = '>';
  449. }
  450. dir = 0;
  451. }
  452. if (rp->mmap_active) {
  453. offset = mon_buff_area_alloc_contiguous(rp,
  454. length + PKT_SIZE + lendesc);
  455. } else {
  456. offset = mon_buff_area_alloc(rp, length + PKT_SIZE + lendesc);
  457. }
  458. if (offset == ~0) {
  459. rp->cnt_lost++;
  460. spin_unlock_irqrestore(&rp->b_lock, flags);
  461. return;
  462. }
  463. ep = MON_OFF2HDR(rp, offset);
  464. if ((offset += PKT_SIZE) >= rp->b_size) offset = 0;
  465. /*
  466. * Fill the allocated area.
  467. */
  468. memset(ep, 0, PKT_SIZE);
  469. ep->type = ev_type;
  470. ep->xfer_type = xfer_to_pipe[usb_endpoint_type(epd)];
  471. ep->epnum = dir | usb_endpoint_num(epd);
  472. ep->devnum = urb->dev->devnum;
  473. ep->busnum = urb->dev->bus->busnum;
  474. ep->id = (unsigned long) urb;
  475. ep->ts_sec = ts.tv_sec;
  476. ep->ts_usec = ts.tv_usec;
  477. ep->status = status;
  478. ep->len_urb = urb_length;
  479. ep->len_cap = length + lendesc;
  480. ep->xfer_flags = urb->transfer_flags;
  481. if (usb_endpoint_xfer_int(epd)) {
  482. ep->interval = urb->interval;
  483. } else if (usb_endpoint_xfer_isoc(epd)) {
  484. ep->interval = urb->interval;
  485. ep->start_frame = urb->start_frame;
  486. ep->s.iso.error_count = urb->error_count;
  487. ep->s.iso.numdesc = urb->number_of_packets;
  488. }
  489. if (usb_endpoint_xfer_control(epd) && ev_type == 'S') {
  490. ep->flag_setup = mon_bin_get_setup(ep->s.setup, urb, ev_type);
  491. } else {
  492. ep->flag_setup = '-';
  493. }
  494. if (ndesc != 0) {
  495. ep->ndesc = ndesc;
  496. mon_bin_get_isodesc(rp, offset, urb, ev_type, ndesc);
  497. if ((offset += lendesc) >= rp->b_size)
  498. offset -= rp->b_size;
  499. }
  500. if (length != 0) {
  501. length = mon_bin_get_data(rp, offset, urb, length,
  502. &ep->flag_data);
  503. if (length > 0) {
  504. delta = (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  505. ep->len_cap -= length;
  506. delta -= (ep->len_cap + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  507. mon_buff_area_shrink(rp, delta);
  508. }
  509. } else {
  510. ep->flag_data = data_tag;
  511. }
  512. spin_unlock_irqrestore(&rp->b_lock, flags);
  513. wake_up(&rp->b_wait);
  514. }
  515. static void mon_bin_submit(void *data, struct urb *urb)
  516. {
  517. struct mon_reader_bin *rp = data;
  518. mon_bin_event(rp, urb, 'S', -EINPROGRESS);
  519. }
  520. static void mon_bin_complete(void *data, struct urb *urb, int status)
  521. {
  522. struct mon_reader_bin *rp = data;
  523. mon_bin_event(rp, urb, 'C', status);
  524. }
  525. static void mon_bin_error(void *data, struct urb *urb, int error)
  526. {
  527. struct mon_reader_bin *rp = data;
  528. unsigned long flags;
  529. unsigned int offset;
  530. struct mon_bin_hdr *ep;
  531. spin_lock_irqsave(&rp->b_lock, flags);
  532. offset = mon_buff_area_alloc(rp, PKT_SIZE);
  533. if (offset == ~0) {
  534. /* Not incrementing cnt_lost. Just because. */
  535. spin_unlock_irqrestore(&rp->b_lock, flags);
  536. return;
  537. }
  538. ep = MON_OFF2HDR(rp, offset);
  539. memset(ep, 0, PKT_SIZE);
  540. ep->type = 'E';
  541. ep->xfer_type = xfer_to_pipe[usb_endpoint_type(&urb->ep->desc)];
  542. ep->epnum = usb_urb_dir_in(urb) ? USB_DIR_IN : 0;
  543. ep->epnum |= usb_endpoint_num(&urb->ep->desc);
  544. ep->devnum = urb->dev->devnum;
  545. ep->busnum = urb->dev->bus->busnum;
  546. ep->id = (unsigned long) urb;
  547. ep->status = error;
  548. ep->flag_setup = '-';
  549. ep->flag_data = 'E';
  550. spin_unlock_irqrestore(&rp->b_lock, flags);
  551. wake_up(&rp->b_wait);
  552. }
  553. static int mon_bin_open(struct inode *inode, struct file *file)
  554. {
  555. struct mon_bus *mbus;
  556. struct mon_reader_bin *rp;
  557. size_t size;
  558. int rc;
  559. lock_kernel();
  560. mutex_lock(&mon_lock);
  561. if ((mbus = mon_bus_lookup(iminor(inode))) == NULL) {
  562. mutex_unlock(&mon_lock);
  563. unlock_kernel();
  564. return -ENODEV;
  565. }
  566. if (mbus != &mon_bus0 && mbus->u_bus == NULL) {
  567. printk(KERN_ERR TAG ": consistency error on open\n");
  568. mutex_unlock(&mon_lock);
  569. unlock_kernel();
  570. return -ENODEV;
  571. }
  572. rp = kzalloc(sizeof(struct mon_reader_bin), GFP_KERNEL);
  573. if (rp == NULL) {
  574. rc = -ENOMEM;
  575. goto err_alloc;
  576. }
  577. spin_lock_init(&rp->b_lock);
  578. init_waitqueue_head(&rp->b_wait);
  579. mutex_init(&rp->fetch_lock);
  580. rp->b_size = BUFF_DFL;
  581. size = sizeof(struct mon_pgmap) * (rp->b_size/CHUNK_SIZE);
  582. if ((rp->b_vec = kzalloc(size, GFP_KERNEL)) == NULL) {
  583. rc = -ENOMEM;
  584. goto err_allocvec;
  585. }
  586. if ((rc = mon_alloc_buff(rp->b_vec, rp->b_size/CHUNK_SIZE)) < 0)
  587. goto err_allocbuff;
  588. rp->r.m_bus = mbus;
  589. rp->r.r_data = rp;
  590. rp->r.rnf_submit = mon_bin_submit;
  591. rp->r.rnf_error = mon_bin_error;
  592. rp->r.rnf_complete = mon_bin_complete;
  593. mon_reader_add(mbus, &rp->r);
  594. file->private_data = rp;
  595. mutex_unlock(&mon_lock);
  596. unlock_kernel();
  597. return 0;
  598. err_allocbuff:
  599. kfree(rp->b_vec);
  600. err_allocvec:
  601. kfree(rp);
  602. err_alloc:
  603. mutex_unlock(&mon_lock);
  604. unlock_kernel();
  605. return rc;
  606. }
  607. /*
  608. * Extract an event from buffer and copy it to user space.
  609. * Wait if there is no event ready.
  610. * Returns zero or error.
  611. */
  612. static int mon_bin_get_event(struct file *file, struct mon_reader_bin *rp,
  613. struct mon_bin_hdr __user *hdr, unsigned int hdrbytes,
  614. void __user *data, unsigned int nbytes)
  615. {
  616. unsigned long flags;
  617. struct mon_bin_hdr *ep;
  618. size_t step_len;
  619. unsigned int offset;
  620. int rc;
  621. mutex_lock(&rp->fetch_lock);
  622. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  623. mutex_unlock(&rp->fetch_lock);
  624. return rc;
  625. }
  626. ep = MON_OFF2HDR(rp, rp->b_out);
  627. if (copy_to_user(hdr, ep, hdrbytes)) {
  628. mutex_unlock(&rp->fetch_lock);
  629. return -EFAULT;
  630. }
  631. step_len = min(ep->len_cap, nbytes);
  632. if ((offset = rp->b_out + PKT_SIZE) >= rp->b_size) offset = 0;
  633. if (copy_from_buf(rp, offset, data, step_len)) {
  634. mutex_unlock(&rp->fetch_lock);
  635. return -EFAULT;
  636. }
  637. spin_lock_irqsave(&rp->b_lock, flags);
  638. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  639. spin_unlock_irqrestore(&rp->b_lock, flags);
  640. rp->b_read = 0;
  641. mutex_unlock(&rp->fetch_lock);
  642. return 0;
  643. }
  644. static int mon_bin_release(struct inode *inode, struct file *file)
  645. {
  646. struct mon_reader_bin *rp = file->private_data;
  647. struct mon_bus* mbus = rp->r.m_bus;
  648. mutex_lock(&mon_lock);
  649. if (mbus->nreaders <= 0) {
  650. printk(KERN_ERR TAG ": consistency error on close\n");
  651. mutex_unlock(&mon_lock);
  652. return 0;
  653. }
  654. mon_reader_del(mbus, &rp->r);
  655. mon_free_buff(rp->b_vec, rp->b_size/CHUNK_SIZE);
  656. kfree(rp->b_vec);
  657. kfree(rp);
  658. mutex_unlock(&mon_lock);
  659. return 0;
  660. }
  661. static ssize_t mon_bin_read(struct file *file, char __user *buf,
  662. size_t nbytes, loff_t *ppos)
  663. {
  664. struct mon_reader_bin *rp = file->private_data;
  665. unsigned int hdrbytes = PKT_SZ_API0;
  666. unsigned long flags;
  667. struct mon_bin_hdr *ep;
  668. unsigned int offset;
  669. size_t step_len;
  670. char *ptr;
  671. ssize_t done = 0;
  672. int rc;
  673. mutex_lock(&rp->fetch_lock);
  674. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  675. mutex_unlock(&rp->fetch_lock);
  676. return rc;
  677. }
  678. ep = MON_OFF2HDR(rp, rp->b_out);
  679. if (rp->b_read < hdrbytes) {
  680. step_len = min(nbytes, (size_t)(hdrbytes - rp->b_read));
  681. ptr = ((char *)ep) + rp->b_read;
  682. if (step_len && copy_to_user(buf, ptr, step_len)) {
  683. mutex_unlock(&rp->fetch_lock);
  684. return -EFAULT;
  685. }
  686. nbytes -= step_len;
  687. buf += step_len;
  688. rp->b_read += step_len;
  689. done += step_len;
  690. }
  691. if (rp->b_read >= hdrbytes) {
  692. step_len = ep->len_cap;
  693. step_len -= rp->b_read - hdrbytes;
  694. if (step_len > nbytes)
  695. step_len = nbytes;
  696. offset = rp->b_out + PKT_SIZE;
  697. offset += rp->b_read - hdrbytes;
  698. if (offset >= rp->b_size)
  699. offset -= rp->b_size;
  700. if (copy_from_buf(rp, offset, buf, step_len)) {
  701. mutex_unlock(&rp->fetch_lock);
  702. return -EFAULT;
  703. }
  704. nbytes -= step_len;
  705. buf += step_len;
  706. rp->b_read += step_len;
  707. done += step_len;
  708. }
  709. /*
  710. * Check if whole packet was read, and if so, jump to the next one.
  711. */
  712. if (rp->b_read >= hdrbytes + ep->len_cap) {
  713. spin_lock_irqsave(&rp->b_lock, flags);
  714. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  715. spin_unlock_irqrestore(&rp->b_lock, flags);
  716. rp->b_read = 0;
  717. }
  718. mutex_unlock(&rp->fetch_lock);
  719. return done;
  720. }
  721. /*
  722. * Remove at most nevents from chunked buffer.
  723. * Returns the number of removed events.
  724. */
  725. static int mon_bin_flush(struct mon_reader_bin *rp, unsigned nevents)
  726. {
  727. unsigned long flags;
  728. struct mon_bin_hdr *ep;
  729. int i;
  730. mutex_lock(&rp->fetch_lock);
  731. spin_lock_irqsave(&rp->b_lock, flags);
  732. for (i = 0; i < nevents; ++i) {
  733. if (MON_RING_EMPTY(rp))
  734. break;
  735. ep = MON_OFF2HDR(rp, rp->b_out);
  736. mon_buff_area_free(rp, PKT_SIZE + ep->len_cap);
  737. }
  738. spin_unlock_irqrestore(&rp->b_lock, flags);
  739. rp->b_read = 0;
  740. mutex_unlock(&rp->fetch_lock);
  741. return i;
  742. }
  743. /*
  744. * Fetch at most max event offsets into the buffer and put them into vec.
  745. * The events are usually freed later with mon_bin_flush.
  746. * Return the effective number of events fetched.
  747. */
  748. static int mon_bin_fetch(struct file *file, struct mon_reader_bin *rp,
  749. u32 __user *vec, unsigned int max)
  750. {
  751. unsigned int cur_out;
  752. unsigned int bytes, avail;
  753. unsigned int size;
  754. unsigned int nevents;
  755. struct mon_bin_hdr *ep;
  756. unsigned long flags;
  757. int rc;
  758. mutex_lock(&rp->fetch_lock);
  759. if ((rc = mon_bin_wait_event(file, rp)) < 0) {
  760. mutex_unlock(&rp->fetch_lock);
  761. return rc;
  762. }
  763. spin_lock_irqsave(&rp->b_lock, flags);
  764. avail = rp->b_cnt;
  765. spin_unlock_irqrestore(&rp->b_lock, flags);
  766. cur_out = rp->b_out;
  767. nevents = 0;
  768. bytes = 0;
  769. while (bytes < avail) {
  770. if (nevents >= max)
  771. break;
  772. ep = MON_OFF2HDR(rp, cur_out);
  773. if (put_user(cur_out, &vec[nevents])) {
  774. mutex_unlock(&rp->fetch_lock);
  775. return -EFAULT;
  776. }
  777. nevents++;
  778. size = ep->len_cap + PKT_SIZE;
  779. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  780. if ((cur_out += size) >= rp->b_size)
  781. cur_out -= rp->b_size;
  782. bytes += size;
  783. }
  784. mutex_unlock(&rp->fetch_lock);
  785. return nevents;
  786. }
  787. /*
  788. * Count events. This is almost the same as the above mon_bin_fetch,
  789. * only we do not store offsets into user vector, and we have no limit.
  790. */
  791. static int mon_bin_queued(struct mon_reader_bin *rp)
  792. {
  793. unsigned int cur_out;
  794. unsigned int bytes, avail;
  795. unsigned int size;
  796. unsigned int nevents;
  797. struct mon_bin_hdr *ep;
  798. unsigned long flags;
  799. mutex_lock(&rp->fetch_lock);
  800. spin_lock_irqsave(&rp->b_lock, flags);
  801. avail = rp->b_cnt;
  802. spin_unlock_irqrestore(&rp->b_lock, flags);
  803. cur_out = rp->b_out;
  804. nevents = 0;
  805. bytes = 0;
  806. while (bytes < avail) {
  807. ep = MON_OFF2HDR(rp, cur_out);
  808. nevents++;
  809. size = ep->len_cap + PKT_SIZE;
  810. size = (size + PKT_ALIGN-1) & ~(PKT_ALIGN-1);
  811. if ((cur_out += size) >= rp->b_size)
  812. cur_out -= rp->b_size;
  813. bytes += size;
  814. }
  815. mutex_unlock(&rp->fetch_lock);
  816. return nevents;
  817. }
  818. /*
  819. */
  820. static int mon_bin_ioctl(struct inode *inode, struct file *file,
  821. unsigned int cmd, unsigned long arg)
  822. {
  823. struct mon_reader_bin *rp = file->private_data;
  824. // struct mon_bus* mbus = rp->r.m_bus;
  825. int ret = 0;
  826. struct mon_bin_hdr *ep;
  827. unsigned long flags;
  828. switch (cmd) {
  829. case MON_IOCQ_URB_LEN:
  830. /*
  831. * N.B. This only returns the size of data, without the header.
  832. */
  833. spin_lock_irqsave(&rp->b_lock, flags);
  834. if (!MON_RING_EMPTY(rp)) {
  835. ep = MON_OFF2HDR(rp, rp->b_out);
  836. ret = ep->len_cap;
  837. }
  838. spin_unlock_irqrestore(&rp->b_lock, flags);
  839. break;
  840. case MON_IOCQ_RING_SIZE:
  841. ret = rp->b_size;
  842. break;
  843. case MON_IOCT_RING_SIZE:
  844. /*
  845. * Changing the buffer size will flush it's contents; the new
  846. * buffer is allocated before releasing the old one to be sure
  847. * the device will stay functional also in case of memory
  848. * pressure.
  849. */
  850. {
  851. int size;
  852. struct mon_pgmap *vec;
  853. if (arg < BUFF_MIN || arg > BUFF_MAX)
  854. return -EINVAL;
  855. size = CHUNK_ALIGN(arg);
  856. if ((vec = kzalloc(sizeof(struct mon_pgmap) * (size/CHUNK_SIZE),
  857. GFP_KERNEL)) == NULL) {
  858. ret = -ENOMEM;
  859. break;
  860. }
  861. ret = mon_alloc_buff(vec, size/CHUNK_SIZE);
  862. if (ret < 0) {
  863. kfree(vec);
  864. break;
  865. }
  866. mutex_lock(&rp->fetch_lock);
  867. spin_lock_irqsave(&rp->b_lock, flags);
  868. mon_free_buff(rp->b_vec, size/CHUNK_SIZE);
  869. kfree(rp->b_vec);
  870. rp->b_vec = vec;
  871. rp->b_size = size;
  872. rp->b_read = rp->b_in = rp->b_out = rp->b_cnt = 0;
  873. rp->cnt_lost = 0;
  874. spin_unlock_irqrestore(&rp->b_lock, flags);
  875. mutex_unlock(&rp->fetch_lock);
  876. }
  877. break;
  878. case MON_IOCH_MFLUSH:
  879. ret = mon_bin_flush(rp, arg);
  880. break;
  881. case MON_IOCX_GET:
  882. case MON_IOCX_GETX:
  883. {
  884. struct mon_bin_get getb;
  885. if (copy_from_user(&getb, (void __user *)arg,
  886. sizeof(struct mon_bin_get)))
  887. return -EFAULT;
  888. if (getb.alloc > 0x10000000) /* Want to cast to u32 */
  889. return -EINVAL;
  890. ret = mon_bin_get_event(file, rp, getb.hdr,
  891. (cmd == MON_IOCX_GET)? PKT_SZ_API0: PKT_SZ_API1,
  892. getb.data, (unsigned int)getb.alloc);
  893. }
  894. break;
  895. case MON_IOCX_MFETCH:
  896. {
  897. struct mon_bin_mfetch mfetch;
  898. struct mon_bin_mfetch __user *uptr;
  899. uptr = (struct mon_bin_mfetch __user *)arg;
  900. if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
  901. return -EFAULT;
  902. if (mfetch.nflush) {
  903. ret = mon_bin_flush(rp, mfetch.nflush);
  904. if (ret < 0)
  905. return ret;
  906. if (put_user(ret, &uptr->nflush))
  907. return -EFAULT;
  908. }
  909. ret = mon_bin_fetch(file, rp, mfetch.offvec, mfetch.nfetch);
  910. if (ret < 0)
  911. return ret;
  912. if (put_user(ret, &uptr->nfetch))
  913. return -EFAULT;
  914. ret = 0;
  915. }
  916. break;
  917. case MON_IOCG_STATS: {
  918. struct mon_bin_stats __user *sp;
  919. unsigned int nevents;
  920. unsigned int ndropped;
  921. spin_lock_irqsave(&rp->b_lock, flags);
  922. ndropped = rp->cnt_lost;
  923. rp->cnt_lost = 0;
  924. spin_unlock_irqrestore(&rp->b_lock, flags);
  925. nevents = mon_bin_queued(rp);
  926. sp = (struct mon_bin_stats __user *)arg;
  927. if (put_user(rp->cnt_lost, &sp->dropped))
  928. return -EFAULT;
  929. if (put_user(nevents, &sp->queued))
  930. return -EFAULT;
  931. }
  932. break;
  933. default:
  934. return -ENOTTY;
  935. }
  936. return ret;
  937. }
  938. #ifdef CONFIG_COMPAT
  939. static long mon_bin_compat_ioctl(struct file *file,
  940. unsigned int cmd, unsigned long arg)
  941. {
  942. struct mon_reader_bin *rp = file->private_data;
  943. int ret;
  944. switch (cmd) {
  945. case MON_IOCX_GET32:
  946. case MON_IOCX_GETX32:
  947. {
  948. struct mon_bin_get32 getb;
  949. if (copy_from_user(&getb, (void __user *)arg,
  950. sizeof(struct mon_bin_get32)))
  951. return -EFAULT;
  952. ret = mon_bin_get_event(file, rp, compat_ptr(getb.hdr32),
  953. (cmd == MON_IOCX_GET32)? PKT_SZ_API0: PKT_SZ_API1,
  954. compat_ptr(getb.data32), getb.alloc32);
  955. if (ret < 0)
  956. return ret;
  957. }
  958. return 0;
  959. case MON_IOCX_MFETCH32:
  960. {
  961. struct mon_bin_mfetch32 mfetch;
  962. struct mon_bin_mfetch32 __user *uptr;
  963. uptr = (struct mon_bin_mfetch32 __user *) compat_ptr(arg);
  964. if (copy_from_user(&mfetch, uptr, sizeof(mfetch)))
  965. return -EFAULT;
  966. if (mfetch.nflush32) {
  967. ret = mon_bin_flush(rp, mfetch.nflush32);
  968. if (ret < 0)
  969. return ret;
  970. if (put_user(ret, &uptr->nflush32))
  971. return -EFAULT;
  972. }
  973. ret = mon_bin_fetch(file, rp, compat_ptr(mfetch.offvec32),
  974. mfetch.nfetch32);
  975. if (ret < 0)
  976. return ret;
  977. if (put_user(ret, &uptr->nfetch32))
  978. return -EFAULT;
  979. }
  980. return 0;
  981. case MON_IOCG_STATS:
  982. return mon_bin_ioctl(NULL, file, cmd,
  983. (unsigned long) compat_ptr(arg));
  984. case MON_IOCQ_URB_LEN:
  985. case MON_IOCQ_RING_SIZE:
  986. case MON_IOCT_RING_SIZE:
  987. case MON_IOCH_MFLUSH:
  988. return mon_bin_ioctl(NULL, file, cmd, arg);
  989. default:
  990. ;
  991. }
  992. return -ENOTTY;
  993. }
  994. #endif /* CONFIG_COMPAT */
  995. static unsigned int
  996. mon_bin_poll(struct file *file, struct poll_table_struct *wait)
  997. {
  998. struct mon_reader_bin *rp = file->private_data;
  999. unsigned int mask = 0;
  1000. unsigned long flags;
  1001. if (file->f_mode & FMODE_READ)
  1002. poll_wait(file, &rp->b_wait, wait);
  1003. spin_lock_irqsave(&rp->b_lock, flags);
  1004. if (!MON_RING_EMPTY(rp))
  1005. mask |= POLLIN | POLLRDNORM; /* readable */
  1006. spin_unlock_irqrestore(&rp->b_lock, flags);
  1007. return mask;
  1008. }
  1009. /*
  1010. * open and close: just keep track of how many times the device is
  1011. * mapped, to use the proper memory allocation function.
  1012. */
  1013. static void mon_bin_vma_open(struct vm_area_struct *vma)
  1014. {
  1015. struct mon_reader_bin *rp = vma->vm_private_data;
  1016. rp->mmap_active++;
  1017. }
  1018. static void mon_bin_vma_close(struct vm_area_struct *vma)
  1019. {
  1020. struct mon_reader_bin *rp = vma->vm_private_data;
  1021. rp->mmap_active--;
  1022. }
  1023. /*
  1024. * Map ring pages to user space.
  1025. */
  1026. static int mon_bin_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1027. {
  1028. struct mon_reader_bin *rp = vma->vm_private_data;
  1029. unsigned long offset, chunk_idx;
  1030. struct page *pageptr;
  1031. offset = vmf->pgoff << PAGE_SHIFT;
  1032. if (offset >= rp->b_size)
  1033. return VM_FAULT_SIGBUS;
  1034. chunk_idx = offset / CHUNK_SIZE;
  1035. pageptr = rp->b_vec[chunk_idx].pg;
  1036. get_page(pageptr);
  1037. vmf->page = pageptr;
  1038. return 0;
  1039. }
  1040. static const struct vm_operations_struct mon_bin_vm_ops = {
  1041. .open = mon_bin_vma_open,
  1042. .close = mon_bin_vma_close,
  1043. .fault = mon_bin_vma_fault,
  1044. };
  1045. static int mon_bin_mmap(struct file *filp, struct vm_area_struct *vma)
  1046. {
  1047. /* don't do anything here: "fault" will set up page table entries */
  1048. vma->vm_ops = &mon_bin_vm_ops;
  1049. vma->vm_flags |= VM_RESERVED;
  1050. vma->vm_private_data = filp->private_data;
  1051. mon_bin_vma_open(vma);
  1052. return 0;
  1053. }
  1054. static const struct file_operations mon_fops_binary = {
  1055. .owner = THIS_MODULE,
  1056. .open = mon_bin_open,
  1057. .llseek = no_llseek,
  1058. .read = mon_bin_read,
  1059. /* .write = mon_text_write, */
  1060. .poll = mon_bin_poll,
  1061. .ioctl = mon_bin_ioctl,
  1062. #ifdef CONFIG_COMPAT
  1063. .compat_ioctl = mon_bin_compat_ioctl,
  1064. #endif
  1065. .release = mon_bin_release,
  1066. .mmap = mon_bin_mmap,
  1067. };
  1068. static int mon_bin_wait_event(struct file *file, struct mon_reader_bin *rp)
  1069. {
  1070. DECLARE_WAITQUEUE(waita, current);
  1071. unsigned long flags;
  1072. add_wait_queue(&rp->b_wait, &waita);
  1073. set_current_state(TASK_INTERRUPTIBLE);
  1074. spin_lock_irqsave(&rp->b_lock, flags);
  1075. while (MON_RING_EMPTY(rp)) {
  1076. spin_unlock_irqrestore(&rp->b_lock, flags);
  1077. if (file->f_flags & O_NONBLOCK) {
  1078. set_current_state(TASK_RUNNING);
  1079. remove_wait_queue(&rp->b_wait, &waita);
  1080. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  1081. }
  1082. schedule();
  1083. if (signal_pending(current)) {
  1084. remove_wait_queue(&rp->b_wait, &waita);
  1085. return -EINTR;
  1086. }
  1087. set_current_state(TASK_INTERRUPTIBLE);
  1088. spin_lock_irqsave(&rp->b_lock, flags);
  1089. }
  1090. spin_unlock_irqrestore(&rp->b_lock, flags);
  1091. set_current_state(TASK_RUNNING);
  1092. remove_wait_queue(&rp->b_wait, &waita);
  1093. return 0;
  1094. }
  1095. static int mon_alloc_buff(struct mon_pgmap *map, int npages)
  1096. {
  1097. int n;
  1098. unsigned long vaddr;
  1099. for (n = 0; n < npages; n++) {
  1100. vaddr = get_zeroed_page(GFP_KERNEL);
  1101. if (vaddr == 0) {
  1102. while (n-- != 0)
  1103. free_page((unsigned long) map[n].ptr);
  1104. return -ENOMEM;
  1105. }
  1106. map[n].ptr = (unsigned char *) vaddr;
  1107. map[n].pg = virt_to_page((void *) vaddr);
  1108. }
  1109. return 0;
  1110. }
  1111. static void mon_free_buff(struct mon_pgmap *map, int npages)
  1112. {
  1113. int n;
  1114. for (n = 0; n < npages; n++)
  1115. free_page((unsigned long) map[n].ptr);
  1116. }
  1117. int mon_bin_add(struct mon_bus *mbus, const struct usb_bus *ubus)
  1118. {
  1119. struct device *dev;
  1120. unsigned minor = ubus? ubus->busnum: 0;
  1121. if (minor >= MON_BIN_MAX_MINOR)
  1122. return 0;
  1123. dev = device_create(mon_bin_class, ubus ? ubus->controller : NULL,
  1124. MKDEV(MAJOR(mon_bin_dev0), minor), NULL,
  1125. "usbmon%d", minor);
  1126. if (IS_ERR(dev))
  1127. return 0;
  1128. mbus->classdev = dev;
  1129. return 1;
  1130. }
  1131. void mon_bin_del(struct mon_bus *mbus)
  1132. {
  1133. device_destroy(mon_bin_class, mbus->classdev->devt);
  1134. }
  1135. int __init mon_bin_init(void)
  1136. {
  1137. int rc;
  1138. mon_bin_class = class_create(THIS_MODULE, "usbmon");
  1139. if (IS_ERR(mon_bin_class)) {
  1140. rc = PTR_ERR(mon_bin_class);
  1141. goto err_class;
  1142. }
  1143. rc = alloc_chrdev_region(&mon_bin_dev0, 0, MON_BIN_MAX_MINOR, "usbmon");
  1144. if (rc < 0)
  1145. goto err_dev;
  1146. cdev_init(&mon_bin_cdev, &mon_fops_binary);
  1147. mon_bin_cdev.owner = THIS_MODULE;
  1148. rc = cdev_add(&mon_bin_cdev, mon_bin_dev0, MON_BIN_MAX_MINOR);
  1149. if (rc < 0)
  1150. goto err_add;
  1151. return 0;
  1152. err_add:
  1153. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1154. err_dev:
  1155. class_destroy(mon_bin_class);
  1156. err_class:
  1157. return rc;
  1158. }
  1159. void mon_bin_exit(void)
  1160. {
  1161. cdev_del(&mon_bin_cdev);
  1162. unregister_chrdev_region(mon_bin_dev0, MON_BIN_MAX_MINOR);
  1163. class_destroy(mon_bin_class);
  1164. }