mon_bin.c 32 KB

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