mon_bin.c 31 KB

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