mon_text.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * The USB Monitor, inspired by Dave Harding's USBMon.
  3. *
  4. * This is a text format reader.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/list.h>
  8. #include <linux/usb.h>
  9. #include <linux/time.h>
  10. #include <linux/mutex.h>
  11. #include <linux/debugfs.h>
  12. #include <asm/uaccess.h>
  13. #include "usb_mon.h"
  14. /*
  15. * No, we do not want arbitrarily long data strings.
  16. * Use the binary interface if you want to capture bulk data!
  17. */
  18. #define DATA_MAX 32
  19. /*
  20. * Defined by USB 2.0 clause 9.3, table 9.2.
  21. */
  22. #define SETUP_MAX 8
  23. /*
  24. * This limit exists to prevent OOMs when the user process stops reading.
  25. * If usbmon were available to unprivileged processes, it might be open
  26. * to a local DoS. But we have to keep to root in order to prevent
  27. * password sniffing from HID devices.
  28. */
  29. #define EVENT_MAX (4*PAGE_SIZE / sizeof(struct mon_event_text))
  30. /*
  31. * Potentially unlimited number; we limit it for similar allocations.
  32. * The usbfs limits this to 128, but we're not quite as generous.
  33. */
  34. #define ISODESC_MAX 5
  35. #define PRINTF_DFL 250 /* with 5 ISOs segs */
  36. struct mon_iso_desc {
  37. int status;
  38. unsigned int offset;
  39. unsigned int length; /* Unsigned here, signed in URB. Historic. */
  40. };
  41. struct mon_event_text {
  42. struct list_head e_link;
  43. int type; /* submit, complete, etc. */
  44. unsigned long id; /* From pointer, most of the time */
  45. unsigned int tstamp;
  46. int busnum;
  47. char devnum;
  48. char epnum;
  49. char is_in;
  50. char xfertype;
  51. int length; /* Depends on type: xfer length or act length */
  52. int status;
  53. int interval;
  54. int start_frame;
  55. int error_count;
  56. char setup_flag;
  57. char data_flag;
  58. int numdesc; /* Full number */
  59. struct mon_iso_desc isodesc[ISODESC_MAX];
  60. unsigned char setup[SETUP_MAX];
  61. unsigned char data[DATA_MAX];
  62. };
  63. #define SLAB_NAME_SZ 30
  64. struct mon_reader_text {
  65. struct kmem_cache *e_slab;
  66. int nevents;
  67. struct list_head e_list;
  68. struct mon_reader r; /* In C, parent class can be placed anywhere */
  69. wait_queue_head_t wait;
  70. int printf_size;
  71. char *printf_buf;
  72. struct mutex printf_lock;
  73. char slab_name[SLAB_NAME_SZ];
  74. };
  75. static struct dentry *mon_dir; /* Usually /sys/kernel/debug/usbmon */
  76. static void mon_text_ctor(void *);
  77. struct mon_text_ptr {
  78. int cnt, limit;
  79. char *pbuf;
  80. };
  81. static struct mon_event_text *
  82. mon_text_read_wait(struct mon_reader_text *rp, struct file *file);
  83. static void mon_text_read_head_t(struct mon_reader_text *rp,
  84. struct mon_text_ptr *p, const struct mon_event_text *ep);
  85. static void mon_text_read_head_u(struct mon_reader_text *rp,
  86. struct mon_text_ptr *p, const struct mon_event_text *ep);
  87. static void mon_text_read_statset(struct mon_reader_text *rp,
  88. struct mon_text_ptr *p, const struct mon_event_text *ep);
  89. static void mon_text_read_intstat(struct mon_reader_text *rp,
  90. struct mon_text_ptr *p, const struct mon_event_text *ep);
  91. static void mon_text_read_isostat(struct mon_reader_text *rp,
  92. struct mon_text_ptr *p, const struct mon_event_text *ep);
  93. static void mon_text_read_isodesc(struct mon_reader_text *rp,
  94. struct mon_text_ptr *p, const struct mon_event_text *ep);
  95. static void mon_text_read_data(struct mon_reader_text *rp,
  96. struct mon_text_ptr *p, const struct mon_event_text *ep);
  97. /*
  98. * mon_text_submit
  99. * mon_text_complete
  100. *
  101. * May be called from an interrupt.
  102. *
  103. * This is called with the whole mon_bus locked, so no additional lock.
  104. */
  105. static inline char mon_text_get_setup(struct mon_event_text *ep,
  106. struct urb *urb, char ev_type, struct mon_bus *mbus)
  107. {
  108. if (ep->xfertype != USB_ENDPOINT_XFER_CONTROL || ev_type != 'S')
  109. return '-';
  110. if (urb->setup_packet == NULL)
  111. return 'Z'; /* '0' would be not as pretty. */
  112. memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
  113. return 0;
  114. }
  115. static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
  116. int len, char ev_type, struct mon_bus *mbus)
  117. {
  118. if (len <= 0)
  119. return 'L';
  120. if (len >= DATA_MAX)
  121. len = DATA_MAX;
  122. if (ep->is_in) {
  123. if (ev_type != 'C')
  124. return '<';
  125. } else {
  126. if (ev_type != 'S')
  127. return '>';
  128. }
  129. /*
  130. * The check to see if it's safe to poke at data has an enormous
  131. * number of corner cases, but it seems that the following is
  132. * more or less safe.
  133. *
  134. * We do not even try to look at transfer_buffer, because it can
  135. * contain non-NULL garbage in case the upper level promised to
  136. * set DMA for the HCD.
  137. */
  138. if (urb->dev->bus->uses_dma &&
  139. (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  140. return mon_dmapeek(ep->data, urb->transfer_dma, len);
  141. }
  142. if (urb->transfer_buffer == NULL)
  143. return 'Z'; /* '0' would be not as pretty. */
  144. memcpy(ep->data, urb->transfer_buffer, len);
  145. return 0;
  146. }
  147. static inline unsigned int mon_get_timestamp(void)
  148. {
  149. struct timeval tval;
  150. unsigned int stamp;
  151. do_gettimeofday(&tval);
  152. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  153. stamp = stamp * 1000000 + tval.tv_usec;
  154. return stamp;
  155. }
  156. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  157. char ev_type, int status)
  158. {
  159. struct mon_event_text *ep;
  160. unsigned int stamp;
  161. struct usb_iso_packet_descriptor *fp;
  162. struct mon_iso_desc *dp;
  163. int i, ndesc;
  164. stamp = mon_get_timestamp();
  165. if (rp->nevents >= EVENT_MAX ||
  166. (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
  167. rp->r.m_bus->cnt_text_lost++;
  168. return;
  169. }
  170. ep->type = ev_type;
  171. ep->id = (unsigned long) urb;
  172. ep->busnum = urb->dev->bus->busnum;
  173. ep->devnum = urb->dev->devnum;
  174. ep->epnum = usb_endpoint_num(&urb->ep->desc);
  175. ep->xfertype = usb_endpoint_type(&urb->ep->desc);
  176. ep->is_in = usb_urb_dir_in(urb);
  177. ep->tstamp = stamp;
  178. ep->length = (ev_type == 'S') ?
  179. urb->transfer_buffer_length : urb->actual_length;
  180. /* Collecting status makes debugging sense for submits, too */
  181. ep->status = status;
  182. if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
  183. ep->interval = urb->interval;
  184. } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
  185. ep->interval = urb->interval;
  186. ep->start_frame = urb->start_frame;
  187. ep->error_count = urb->error_count;
  188. }
  189. ep->numdesc = urb->number_of_packets;
  190. if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
  191. urb->number_of_packets > 0) {
  192. if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
  193. ndesc = ISODESC_MAX;
  194. fp = urb->iso_frame_desc;
  195. dp = ep->isodesc;
  196. for (i = 0; i < ndesc; i++) {
  197. dp->status = fp->status;
  198. dp->offset = fp->offset;
  199. dp->length = (ev_type == 'S') ?
  200. fp->length : fp->actual_length;
  201. fp++;
  202. dp++;
  203. }
  204. }
  205. ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
  206. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
  207. rp->r.m_bus);
  208. rp->nevents++;
  209. list_add_tail(&ep->e_link, &rp->e_list);
  210. wake_up(&rp->wait);
  211. }
  212. static void mon_text_submit(void *data, struct urb *urb)
  213. {
  214. struct mon_reader_text *rp = data;
  215. mon_text_event(rp, urb, 'S', -EINPROGRESS);
  216. }
  217. static void mon_text_complete(void *data, struct urb *urb, int status)
  218. {
  219. struct mon_reader_text *rp = data;
  220. mon_text_event(rp, urb, 'C', status);
  221. }
  222. static void mon_text_error(void *data, struct urb *urb, int error)
  223. {
  224. struct mon_reader_text *rp = data;
  225. struct mon_event_text *ep;
  226. if (rp->nevents >= EVENT_MAX ||
  227. (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
  228. rp->r.m_bus->cnt_text_lost++;
  229. return;
  230. }
  231. ep->type = 'E';
  232. ep->id = (unsigned long) urb;
  233. ep->busnum = 0;
  234. ep->devnum = urb->dev->devnum;
  235. ep->epnum = usb_endpoint_num(&urb->ep->desc);
  236. ep->xfertype = usb_endpoint_type(&urb->ep->desc);
  237. ep->is_in = usb_urb_dir_in(urb);
  238. ep->tstamp = 0;
  239. ep->length = 0;
  240. ep->status = error;
  241. ep->setup_flag = '-';
  242. ep->data_flag = 'E';
  243. rp->nevents++;
  244. list_add_tail(&ep->e_link, &rp->e_list);
  245. wake_up(&rp->wait);
  246. }
  247. /*
  248. * Fetch next event from the circular buffer.
  249. */
  250. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  251. struct mon_bus *mbus)
  252. {
  253. struct list_head *p;
  254. unsigned long flags;
  255. spin_lock_irqsave(&mbus->lock, flags);
  256. if (list_empty(&rp->e_list)) {
  257. spin_unlock_irqrestore(&mbus->lock, flags);
  258. return NULL;
  259. }
  260. p = rp->e_list.next;
  261. list_del(p);
  262. --rp->nevents;
  263. spin_unlock_irqrestore(&mbus->lock, flags);
  264. return list_entry(p, struct mon_event_text, e_link);
  265. }
  266. /*
  267. */
  268. static int mon_text_open(struct inode *inode, struct file *file)
  269. {
  270. struct mon_bus *mbus;
  271. struct mon_reader_text *rp;
  272. int rc;
  273. mutex_lock(&mon_lock);
  274. mbus = inode->i_private;
  275. rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  276. if (rp == NULL) {
  277. rc = -ENOMEM;
  278. goto err_alloc;
  279. }
  280. INIT_LIST_HEAD(&rp->e_list);
  281. init_waitqueue_head(&rp->wait);
  282. mutex_init(&rp->printf_lock);
  283. rp->printf_size = PRINTF_DFL;
  284. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  285. if (rp->printf_buf == NULL) {
  286. rc = -ENOMEM;
  287. goto err_alloc_pr;
  288. }
  289. rp->r.m_bus = mbus;
  290. rp->r.r_data = rp;
  291. rp->r.rnf_submit = mon_text_submit;
  292. rp->r.rnf_error = mon_text_error;
  293. rp->r.rnf_complete = mon_text_complete;
  294. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
  295. rp->e_slab = kmem_cache_create(rp->slab_name,
  296. sizeof(struct mon_event_text), sizeof(long), 0,
  297. mon_text_ctor);
  298. if (rp->e_slab == NULL) {
  299. rc = -ENOMEM;
  300. goto err_slab;
  301. }
  302. mon_reader_add(mbus, &rp->r);
  303. file->private_data = rp;
  304. mutex_unlock(&mon_lock);
  305. return 0;
  306. // err_busy:
  307. // kmem_cache_destroy(rp->e_slab);
  308. err_slab:
  309. kfree(rp->printf_buf);
  310. err_alloc_pr:
  311. kfree(rp);
  312. err_alloc:
  313. mutex_unlock(&mon_lock);
  314. return rc;
  315. }
  316. /*
  317. * For simplicity, we read one record in one system call and throw out
  318. * what does not fit. This means that the following does not work:
  319. * dd if=/dbg/usbmon/0t bs=10
  320. * Also, we do not allow seeks and do not bother advancing the offset.
  321. */
  322. static ssize_t mon_text_read_t(struct file *file, char __user *buf,
  323. size_t nbytes, loff_t *ppos)
  324. {
  325. struct mon_reader_text *rp = file->private_data;
  326. struct mon_event_text *ep;
  327. struct mon_text_ptr ptr;
  328. if (IS_ERR(ep = mon_text_read_wait(rp, file)))
  329. return PTR_ERR(ep);
  330. mutex_lock(&rp->printf_lock);
  331. ptr.cnt = 0;
  332. ptr.pbuf = rp->printf_buf;
  333. ptr.limit = rp->printf_size;
  334. mon_text_read_head_t(rp, &ptr, ep);
  335. mon_text_read_statset(rp, &ptr, ep);
  336. ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
  337. " %d", ep->length);
  338. mon_text_read_data(rp, &ptr, ep);
  339. if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
  340. ptr.cnt = -EFAULT;
  341. mutex_unlock(&rp->printf_lock);
  342. kmem_cache_free(rp->e_slab, ep);
  343. return ptr.cnt;
  344. }
  345. static ssize_t mon_text_read_u(struct file *file, char __user *buf,
  346. size_t nbytes, loff_t *ppos)
  347. {
  348. struct mon_reader_text *rp = file->private_data;
  349. struct mon_event_text *ep;
  350. struct mon_text_ptr ptr;
  351. if (IS_ERR(ep = mon_text_read_wait(rp, file)))
  352. return PTR_ERR(ep);
  353. mutex_lock(&rp->printf_lock);
  354. ptr.cnt = 0;
  355. ptr.pbuf = rp->printf_buf;
  356. ptr.limit = rp->printf_size;
  357. mon_text_read_head_u(rp, &ptr, ep);
  358. if (ep->type == 'E') {
  359. mon_text_read_statset(rp, &ptr, ep);
  360. } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
  361. mon_text_read_isostat(rp, &ptr, ep);
  362. mon_text_read_isodesc(rp, &ptr, ep);
  363. } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
  364. mon_text_read_intstat(rp, &ptr, ep);
  365. } else {
  366. mon_text_read_statset(rp, &ptr, ep);
  367. }
  368. ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
  369. " %d", ep->length);
  370. mon_text_read_data(rp, &ptr, ep);
  371. if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
  372. ptr.cnt = -EFAULT;
  373. mutex_unlock(&rp->printf_lock);
  374. kmem_cache_free(rp->e_slab, ep);
  375. return ptr.cnt;
  376. }
  377. static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
  378. struct file *file)
  379. {
  380. struct mon_bus *mbus = rp->r.m_bus;
  381. DECLARE_WAITQUEUE(waita, current);
  382. struct mon_event_text *ep;
  383. add_wait_queue(&rp->wait, &waita);
  384. set_current_state(TASK_INTERRUPTIBLE);
  385. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  386. if (file->f_flags & O_NONBLOCK) {
  387. set_current_state(TASK_RUNNING);
  388. remove_wait_queue(&rp->wait, &waita);
  389. return ERR_PTR(-EWOULDBLOCK);
  390. }
  391. /*
  392. * We do not count nwaiters, because ->release is supposed
  393. * to be called when all openers are gone only.
  394. */
  395. schedule();
  396. if (signal_pending(current)) {
  397. remove_wait_queue(&rp->wait, &waita);
  398. return ERR_PTR(-EINTR);
  399. }
  400. set_current_state(TASK_INTERRUPTIBLE);
  401. }
  402. set_current_state(TASK_RUNNING);
  403. remove_wait_queue(&rp->wait, &waita);
  404. return ep;
  405. }
  406. static void mon_text_read_head_t(struct mon_reader_text *rp,
  407. struct mon_text_ptr *p, const struct mon_event_text *ep)
  408. {
  409. char udir, utype;
  410. udir = (ep->is_in ? 'i' : 'o');
  411. switch (ep->xfertype) {
  412. case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
  413. case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
  414. case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
  415. default: /* PIPE_BULK */ utype = 'B';
  416. }
  417. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  418. "%lx %u %c %c%c:%03u:%02u",
  419. ep->id, ep->tstamp, ep->type,
  420. utype, udir, ep->devnum, ep->epnum);
  421. }
  422. static void mon_text_read_head_u(struct mon_reader_text *rp,
  423. struct mon_text_ptr *p, const struct mon_event_text *ep)
  424. {
  425. char udir, utype;
  426. udir = (ep->is_in ? 'i' : 'o');
  427. switch (ep->xfertype) {
  428. case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
  429. case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
  430. case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
  431. default: /* PIPE_BULK */ utype = 'B';
  432. }
  433. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  434. "%lx %u %c %c%c:%d:%03u:%u",
  435. ep->id, ep->tstamp, ep->type,
  436. utype, udir, ep->busnum, ep->devnum, ep->epnum);
  437. }
  438. static void mon_text_read_statset(struct mon_reader_text *rp,
  439. struct mon_text_ptr *p, const struct mon_event_text *ep)
  440. {
  441. if (ep->setup_flag == 0) { /* Setup packet is present and captured */
  442. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  443. " s %02x %02x %04x %04x %04x",
  444. ep->setup[0],
  445. ep->setup[1],
  446. (ep->setup[3] << 8) | ep->setup[2],
  447. (ep->setup[5] << 8) | ep->setup[4],
  448. (ep->setup[7] << 8) | ep->setup[6]);
  449. } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
  450. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  451. " %c __ __ ____ ____ ____", ep->setup_flag);
  452. } else { /* No setup for this kind of URB */
  453. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  454. " %d", ep->status);
  455. }
  456. }
  457. static void mon_text_read_intstat(struct mon_reader_text *rp,
  458. struct mon_text_ptr *p, const struct mon_event_text *ep)
  459. {
  460. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  461. " %d:%d", ep->status, ep->interval);
  462. }
  463. static void mon_text_read_isostat(struct mon_reader_text *rp,
  464. struct mon_text_ptr *p, const struct mon_event_text *ep)
  465. {
  466. if (ep->type == 'S') {
  467. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  468. " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
  469. } else {
  470. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  471. " %d:%d:%d:%d",
  472. ep->status, ep->interval, ep->start_frame, ep->error_count);
  473. }
  474. }
  475. static void mon_text_read_isodesc(struct mon_reader_text *rp,
  476. struct mon_text_ptr *p, const struct mon_event_text *ep)
  477. {
  478. int ndesc; /* Display this many */
  479. int i;
  480. const struct mon_iso_desc *dp;
  481. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  482. " %d", ep->numdesc);
  483. ndesc = ep->numdesc;
  484. if (ndesc > ISODESC_MAX)
  485. ndesc = ISODESC_MAX;
  486. if (ndesc < 0)
  487. ndesc = 0;
  488. dp = ep->isodesc;
  489. for (i = 0; i < ndesc; i++) {
  490. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  491. " %d:%u:%u", dp->status, dp->offset, dp->length);
  492. dp++;
  493. }
  494. }
  495. static void mon_text_read_data(struct mon_reader_text *rp,
  496. struct mon_text_ptr *p, const struct mon_event_text *ep)
  497. {
  498. int data_len, i;
  499. if ((data_len = ep->length) > 0) {
  500. if (ep->data_flag == 0) {
  501. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  502. " =");
  503. if (data_len >= DATA_MAX)
  504. data_len = DATA_MAX;
  505. for (i = 0; i < data_len; i++) {
  506. if (i % 4 == 0) {
  507. p->cnt += snprintf(p->pbuf + p->cnt,
  508. p->limit - p->cnt,
  509. " ");
  510. }
  511. p->cnt += snprintf(p->pbuf + p->cnt,
  512. p->limit - p->cnt,
  513. "%02x", ep->data[i]);
  514. }
  515. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  516. "\n");
  517. } else {
  518. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  519. " %c\n", ep->data_flag);
  520. }
  521. } else {
  522. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
  523. }
  524. }
  525. static int mon_text_release(struct inode *inode, struct file *file)
  526. {
  527. struct mon_reader_text *rp = file->private_data;
  528. struct mon_bus *mbus;
  529. /* unsigned long flags; */
  530. struct list_head *p;
  531. struct mon_event_text *ep;
  532. mutex_lock(&mon_lock);
  533. mbus = inode->i_private;
  534. if (mbus->nreaders <= 0) {
  535. printk(KERN_ERR TAG ": consistency error on close\n");
  536. mutex_unlock(&mon_lock);
  537. return 0;
  538. }
  539. mon_reader_del(mbus, &rp->r);
  540. /*
  541. * In theory, e_list is protected by mbus->lock. However,
  542. * after mon_reader_del has finished, the following is the case:
  543. * - we are not on reader list anymore, so new events won't be added;
  544. * - whole mbus may be dropped if it was orphaned.
  545. * So, we better not touch mbus.
  546. */
  547. /* spin_lock_irqsave(&mbus->lock, flags); */
  548. while (!list_empty(&rp->e_list)) {
  549. p = rp->e_list.next;
  550. ep = list_entry(p, struct mon_event_text, e_link);
  551. list_del(p);
  552. --rp->nevents;
  553. kmem_cache_free(rp->e_slab, ep);
  554. }
  555. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  556. kmem_cache_destroy(rp->e_slab);
  557. kfree(rp->printf_buf);
  558. kfree(rp);
  559. mutex_unlock(&mon_lock);
  560. return 0;
  561. }
  562. static const struct file_operations mon_fops_text_t = {
  563. .owner = THIS_MODULE,
  564. .open = mon_text_open,
  565. .llseek = no_llseek,
  566. .read = mon_text_read_t,
  567. .release = mon_text_release,
  568. };
  569. static const struct file_operations mon_fops_text_u = {
  570. .owner = THIS_MODULE,
  571. .open = mon_text_open,
  572. .llseek = no_llseek,
  573. .read = mon_text_read_u,
  574. .release = mon_text_release,
  575. };
  576. int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
  577. {
  578. struct dentry *d;
  579. enum { NAMESZ = 10 };
  580. char name[NAMESZ];
  581. int busnum = ubus? ubus->busnum: 0;
  582. int rc;
  583. if (ubus != NULL) {
  584. rc = snprintf(name, NAMESZ, "%dt", busnum);
  585. if (rc <= 0 || rc >= NAMESZ)
  586. goto err_print_t;
  587. d = debugfs_create_file(name, 0600, mon_dir, mbus,
  588. &mon_fops_text_t);
  589. if (d == NULL)
  590. goto err_create_t;
  591. mbus->dent_t = d;
  592. }
  593. rc = snprintf(name, NAMESZ, "%du", busnum);
  594. if (rc <= 0 || rc >= NAMESZ)
  595. goto err_print_u;
  596. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
  597. if (d == NULL)
  598. goto err_create_u;
  599. mbus->dent_u = d;
  600. rc = snprintf(name, NAMESZ, "%ds", busnum);
  601. if (rc <= 0 || rc >= NAMESZ)
  602. goto err_print_s;
  603. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
  604. if (d == NULL)
  605. goto err_create_s;
  606. mbus->dent_s = d;
  607. return 1;
  608. err_create_s:
  609. err_print_s:
  610. debugfs_remove(mbus->dent_u);
  611. mbus->dent_u = NULL;
  612. err_create_u:
  613. err_print_u:
  614. if (ubus != NULL) {
  615. debugfs_remove(mbus->dent_t);
  616. mbus->dent_t = NULL;
  617. }
  618. err_create_t:
  619. err_print_t:
  620. return 0;
  621. }
  622. void mon_text_del(struct mon_bus *mbus)
  623. {
  624. debugfs_remove(mbus->dent_u);
  625. if (mbus->dent_t != NULL)
  626. debugfs_remove(mbus->dent_t);
  627. debugfs_remove(mbus->dent_s);
  628. }
  629. /*
  630. * Slab interface: constructor.
  631. */
  632. static void mon_text_ctor(void *mem)
  633. {
  634. /*
  635. * Nothing to initialize. No, really!
  636. * So, we fill it with garbage to emulate a reused object.
  637. */
  638. memset(mem, 0xe5, sizeof(struct mon_event_text));
  639. }
  640. int __init mon_text_init(void)
  641. {
  642. struct dentry *mondir;
  643. mondir = debugfs_create_dir("usbmon", usb_debug_root);
  644. if (IS_ERR(mondir)) {
  645. printk(KERN_NOTICE TAG ": debugfs is not available\n");
  646. return -ENODEV;
  647. }
  648. if (mondir == NULL) {
  649. printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
  650. return -ENODEV;
  651. }
  652. mon_dir = mondir;
  653. return 0;
  654. }
  655. void mon_text_exit(void)
  656. {
  657. debugfs_remove(mon_dir);
  658. }