mon_bin.c 31 KB

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