mon_bin.c 32 KB

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