mon_text.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. if (urb->transfer_buffer == NULL)
  130. return 'Z'; /* '0' would be not as pretty. */
  131. memcpy(ep->data, urb->transfer_buffer, len);
  132. return 0;
  133. }
  134. static inline unsigned int mon_get_timestamp(void)
  135. {
  136. struct timeval tval;
  137. unsigned int stamp;
  138. do_gettimeofday(&tval);
  139. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  140. stamp = stamp * 1000000 + tval.tv_usec;
  141. return stamp;
  142. }
  143. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  144. char ev_type, int status)
  145. {
  146. struct mon_event_text *ep;
  147. unsigned int stamp;
  148. struct usb_iso_packet_descriptor *fp;
  149. struct mon_iso_desc *dp;
  150. int i, ndesc;
  151. stamp = mon_get_timestamp();
  152. if (rp->nevents >= EVENT_MAX ||
  153. (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
  154. rp->r.m_bus->cnt_text_lost++;
  155. return;
  156. }
  157. ep->type = ev_type;
  158. ep->id = (unsigned long) urb;
  159. ep->busnum = urb->dev->bus->busnum;
  160. ep->devnum = urb->dev->devnum;
  161. ep->epnum = usb_endpoint_num(&urb->ep->desc);
  162. ep->xfertype = usb_endpoint_type(&urb->ep->desc);
  163. ep->is_in = usb_urb_dir_in(urb);
  164. ep->tstamp = stamp;
  165. ep->length = (ev_type == 'S') ?
  166. urb->transfer_buffer_length : urb->actual_length;
  167. /* Collecting status makes debugging sense for submits, too */
  168. ep->status = status;
  169. if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
  170. ep->interval = urb->interval;
  171. } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
  172. ep->interval = urb->interval;
  173. ep->start_frame = urb->start_frame;
  174. ep->error_count = urb->error_count;
  175. }
  176. ep->numdesc = urb->number_of_packets;
  177. if (ep->xfertype == USB_ENDPOINT_XFER_ISOC &&
  178. urb->number_of_packets > 0) {
  179. if ((ndesc = urb->number_of_packets) > ISODESC_MAX)
  180. ndesc = ISODESC_MAX;
  181. fp = urb->iso_frame_desc;
  182. dp = ep->isodesc;
  183. for (i = 0; i < ndesc; i++) {
  184. dp->status = fp->status;
  185. dp->offset = fp->offset;
  186. dp->length = (ev_type == 'S') ?
  187. fp->length : fp->actual_length;
  188. fp++;
  189. dp++;
  190. }
  191. }
  192. ep->setup_flag = mon_text_get_setup(ep, urb, ev_type, rp->r.m_bus);
  193. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type,
  194. rp->r.m_bus);
  195. rp->nevents++;
  196. list_add_tail(&ep->e_link, &rp->e_list);
  197. wake_up(&rp->wait);
  198. }
  199. static void mon_text_submit(void *data, struct urb *urb)
  200. {
  201. struct mon_reader_text *rp = data;
  202. mon_text_event(rp, urb, 'S', -EINPROGRESS);
  203. }
  204. static void mon_text_complete(void *data, struct urb *urb, int status)
  205. {
  206. struct mon_reader_text *rp = data;
  207. mon_text_event(rp, urb, 'C', status);
  208. }
  209. static void mon_text_error(void *data, struct urb *urb, int error)
  210. {
  211. struct mon_reader_text *rp = data;
  212. struct mon_event_text *ep;
  213. if (rp->nevents >= EVENT_MAX ||
  214. (ep = kmem_cache_alloc(rp->e_slab, GFP_ATOMIC)) == NULL) {
  215. rp->r.m_bus->cnt_text_lost++;
  216. return;
  217. }
  218. ep->type = 'E';
  219. ep->id = (unsigned long) urb;
  220. ep->busnum = 0;
  221. ep->devnum = urb->dev->devnum;
  222. ep->epnum = usb_endpoint_num(&urb->ep->desc);
  223. ep->xfertype = usb_endpoint_type(&urb->ep->desc);
  224. ep->is_in = usb_urb_dir_in(urb);
  225. ep->tstamp = 0;
  226. ep->length = 0;
  227. ep->status = error;
  228. ep->setup_flag = '-';
  229. ep->data_flag = 'E';
  230. rp->nevents++;
  231. list_add_tail(&ep->e_link, &rp->e_list);
  232. wake_up(&rp->wait);
  233. }
  234. /*
  235. * Fetch next event from the circular buffer.
  236. */
  237. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  238. struct mon_bus *mbus)
  239. {
  240. struct list_head *p;
  241. unsigned long flags;
  242. spin_lock_irqsave(&mbus->lock, flags);
  243. if (list_empty(&rp->e_list)) {
  244. spin_unlock_irqrestore(&mbus->lock, flags);
  245. return NULL;
  246. }
  247. p = rp->e_list.next;
  248. list_del(p);
  249. --rp->nevents;
  250. spin_unlock_irqrestore(&mbus->lock, flags);
  251. return list_entry(p, struct mon_event_text, e_link);
  252. }
  253. /*
  254. */
  255. static int mon_text_open(struct inode *inode, struct file *file)
  256. {
  257. struct mon_bus *mbus;
  258. struct mon_reader_text *rp;
  259. int rc;
  260. mutex_lock(&mon_lock);
  261. mbus = inode->i_private;
  262. rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  263. if (rp == NULL) {
  264. rc = -ENOMEM;
  265. goto err_alloc;
  266. }
  267. INIT_LIST_HEAD(&rp->e_list);
  268. init_waitqueue_head(&rp->wait);
  269. mutex_init(&rp->printf_lock);
  270. rp->printf_size = PRINTF_DFL;
  271. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  272. if (rp->printf_buf == NULL) {
  273. rc = -ENOMEM;
  274. goto err_alloc_pr;
  275. }
  276. rp->r.m_bus = mbus;
  277. rp->r.r_data = rp;
  278. rp->r.rnf_submit = mon_text_submit;
  279. rp->r.rnf_error = mon_text_error;
  280. rp->r.rnf_complete = mon_text_complete;
  281. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon_text_%p", rp);
  282. rp->e_slab = kmem_cache_create(rp->slab_name,
  283. sizeof(struct mon_event_text), sizeof(long), 0,
  284. mon_text_ctor);
  285. if (rp->e_slab == NULL) {
  286. rc = -ENOMEM;
  287. goto err_slab;
  288. }
  289. mon_reader_add(mbus, &rp->r);
  290. file->private_data = rp;
  291. mutex_unlock(&mon_lock);
  292. return 0;
  293. // err_busy:
  294. // kmem_cache_destroy(rp->e_slab);
  295. err_slab:
  296. kfree(rp->printf_buf);
  297. err_alloc_pr:
  298. kfree(rp);
  299. err_alloc:
  300. mutex_unlock(&mon_lock);
  301. return rc;
  302. }
  303. /*
  304. * For simplicity, we read one record in one system call and throw out
  305. * what does not fit. This means that the following does not work:
  306. * dd if=/dbg/usbmon/0t bs=10
  307. * Also, we do not allow seeks and do not bother advancing the offset.
  308. */
  309. static ssize_t mon_text_read_t(struct file *file, char __user *buf,
  310. size_t nbytes, loff_t *ppos)
  311. {
  312. struct mon_reader_text *rp = file->private_data;
  313. struct mon_event_text *ep;
  314. struct mon_text_ptr ptr;
  315. if (IS_ERR(ep = mon_text_read_wait(rp, file)))
  316. return PTR_ERR(ep);
  317. mutex_lock(&rp->printf_lock);
  318. ptr.cnt = 0;
  319. ptr.pbuf = rp->printf_buf;
  320. ptr.limit = rp->printf_size;
  321. mon_text_read_head_t(rp, &ptr, ep);
  322. mon_text_read_statset(rp, &ptr, ep);
  323. ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
  324. " %d", ep->length);
  325. mon_text_read_data(rp, &ptr, ep);
  326. if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
  327. ptr.cnt = -EFAULT;
  328. mutex_unlock(&rp->printf_lock);
  329. kmem_cache_free(rp->e_slab, ep);
  330. return ptr.cnt;
  331. }
  332. static ssize_t mon_text_read_u(struct file *file, char __user *buf,
  333. size_t nbytes, loff_t *ppos)
  334. {
  335. struct mon_reader_text *rp = file->private_data;
  336. struct mon_event_text *ep;
  337. struct mon_text_ptr ptr;
  338. if (IS_ERR(ep = mon_text_read_wait(rp, file)))
  339. return PTR_ERR(ep);
  340. mutex_lock(&rp->printf_lock);
  341. ptr.cnt = 0;
  342. ptr.pbuf = rp->printf_buf;
  343. ptr.limit = rp->printf_size;
  344. mon_text_read_head_u(rp, &ptr, ep);
  345. if (ep->type == 'E') {
  346. mon_text_read_statset(rp, &ptr, ep);
  347. } else if (ep->xfertype == USB_ENDPOINT_XFER_ISOC) {
  348. mon_text_read_isostat(rp, &ptr, ep);
  349. mon_text_read_isodesc(rp, &ptr, ep);
  350. } else if (ep->xfertype == USB_ENDPOINT_XFER_INT) {
  351. mon_text_read_intstat(rp, &ptr, ep);
  352. } else {
  353. mon_text_read_statset(rp, &ptr, ep);
  354. }
  355. ptr.cnt += snprintf(ptr.pbuf + ptr.cnt, ptr.limit - ptr.cnt,
  356. " %d", ep->length);
  357. mon_text_read_data(rp, &ptr, ep);
  358. if (copy_to_user(buf, rp->printf_buf, ptr.cnt))
  359. ptr.cnt = -EFAULT;
  360. mutex_unlock(&rp->printf_lock);
  361. kmem_cache_free(rp->e_slab, ep);
  362. return ptr.cnt;
  363. }
  364. static struct mon_event_text *mon_text_read_wait(struct mon_reader_text *rp,
  365. struct file *file)
  366. {
  367. struct mon_bus *mbus = rp->r.m_bus;
  368. DECLARE_WAITQUEUE(waita, current);
  369. struct mon_event_text *ep;
  370. add_wait_queue(&rp->wait, &waita);
  371. set_current_state(TASK_INTERRUPTIBLE);
  372. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  373. if (file->f_flags & O_NONBLOCK) {
  374. set_current_state(TASK_RUNNING);
  375. remove_wait_queue(&rp->wait, &waita);
  376. return ERR_PTR(-EWOULDBLOCK);
  377. }
  378. /*
  379. * We do not count nwaiters, because ->release is supposed
  380. * to be called when all openers are gone only.
  381. */
  382. schedule();
  383. if (signal_pending(current)) {
  384. remove_wait_queue(&rp->wait, &waita);
  385. return ERR_PTR(-EINTR);
  386. }
  387. set_current_state(TASK_INTERRUPTIBLE);
  388. }
  389. set_current_state(TASK_RUNNING);
  390. remove_wait_queue(&rp->wait, &waita);
  391. return ep;
  392. }
  393. static void mon_text_read_head_t(struct mon_reader_text *rp,
  394. struct mon_text_ptr *p, const struct mon_event_text *ep)
  395. {
  396. char udir, utype;
  397. udir = (ep->is_in ? 'i' : 'o');
  398. switch (ep->xfertype) {
  399. case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
  400. case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
  401. case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
  402. default: /* PIPE_BULK */ utype = 'B';
  403. }
  404. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  405. "%lx %u %c %c%c:%03u:%02u",
  406. ep->id, ep->tstamp, ep->type,
  407. utype, udir, ep->devnum, ep->epnum);
  408. }
  409. static void mon_text_read_head_u(struct mon_reader_text *rp,
  410. struct mon_text_ptr *p, const struct mon_event_text *ep)
  411. {
  412. char udir, utype;
  413. udir = (ep->is_in ? 'i' : 'o');
  414. switch (ep->xfertype) {
  415. case USB_ENDPOINT_XFER_ISOC: utype = 'Z'; break;
  416. case USB_ENDPOINT_XFER_INT: utype = 'I'; break;
  417. case USB_ENDPOINT_XFER_CONTROL: utype = 'C'; break;
  418. default: /* PIPE_BULK */ utype = 'B';
  419. }
  420. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  421. "%lx %u %c %c%c:%d:%03u:%u",
  422. ep->id, ep->tstamp, ep->type,
  423. utype, udir, ep->busnum, ep->devnum, ep->epnum);
  424. }
  425. static void mon_text_read_statset(struct mon_reader_text *rp,
  426. struct mon_text_ptr *p, const struct mon_event_text *ep)
  427. {
  428. if (ep->setup_flag == 0) { /* Setup packet is present and captured */
  429. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  430. " s %02x %02x %04x %04x %04x",
  431. ep->setup[0],
  432. ep->setup[1],
  433. (ep->setup[3] << 8) | ep->setup[2],
  434. (ep->setup[5] << 8) | ep->setup[4],
  435. (ep->setup[7] << 8) | ep->setup[6]);
  436. } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
  437. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  438. " %c __ __ ____ ____ ____", ep->setup_flag);
  439. } else { /* No setup for this kind of URB */
  440. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  441. " %d", ep->status);
  442. }
  443. }
  444. static void mon_text_read_intstat(struct mon_reader_text *rp,
  445. struct mon_text_ptr *p, const struct mon_event_text *ep)
  446. {
  447. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  448. " %d:%d", ep->status, ep->interval);
  449. }
  450. static void mon_text_read_isostat(struct mon_reader_text *rp,
  451. struct mon_text_ptr *p, const struct mon_event_text *ep)
  452. {
  453. if (ep->type == 'S') {
  454. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  455. " %d:%d:%d", ep->status, ep->interval, ep->start_frame);
  456. } else {
  457. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  458. " %d:%d:%d:%d",
  459. ep->status, ep->interval, ep->start_frame, ep->error_count);
  460. }
  461. }
  462. static void mon_text_read_isodesc(struct mon_reader_text *rp,
  463. struct mon_text_ptr *p, const struct mon_event_text *ep)
  464. {
  465. int ndesc; /* Display this many */
  466. int i;
  467. const struct mon_iso_desc *dp;
  468. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  469. " %d", ep->numdesc);
  470. ndesc = ep->numdesc;
  471. if (ndesc > ISODESC_MAX)
  472. ndesc = ISODESC_MAX;
  473. if (ndesc < 0)
  474. ndesc = 0;
  475. dp = ep->isodesc;
  476. for (i = 0; i < ndesc; i++) {
  477. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  478. " %d:%u:%u", dp->status, dp->offset, dp->length);
  479. dp++;
  480. }
  481. }
  482. static void mon_text_read_data(struct mon_reader_text *rp,
  483. struct mon_text_ptr *p, const struct mon_event_text *ep)
  484. {
  485. int data_len, i;
  486. if ((data_len = ep->length) > 0) {
  487. if (ep->data_flag == 0) {
  488. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  489. " =");
  490. if (data_len >= DATA_MAX)
  491. data_len = DATA_MAX;
  492. for (i = 0; i < data_len; i++) {
  493. if (i % 4 == 0) {
  494. p->cnt += snprintf(p->pbuf + p->cnt,
  495. p->limit - p->cnt,
  496. " ");
  497. }
  498. p->cnt += snprintf(p->pbuf + p->cnt,
  499. p->limit - p->cnt,
  500. "%02x", ep->data[i]);
  501. }
  502. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  503. "\n");
  504. } else {
  505. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt,
  506. " %c\n", ep->data_flag);
  507. }
  508. } else {
  509. p->cnt += snprintf(p->pbuf + p->cnt, p->limit - p->cnt, "\n");
  510. }
  511. }
  512. static int mon_text_release(struct inode *inode, struct file *file)
  513. {
  514. struct mon_reader_text *rp = file->private_data;
  515. struct mon_bus *mbus;
  516. /* unsigned long flags; */
  517. struct list_head *p;
  518. struct mon_event_text *ep;
  519. mutex_lock(&mon_lock);
  520. mbus = inode->i_private;
  521. if (mbus->nreaders <= 0) {
  522. printk(KERN_ERR TAG ": consistency error on close\n");
  523. mutex_unlock(&mon_lock);
  524. return 0;
  525. }
  526. mon_reader_del(mbus, &rp->r);
  527. /*
  528. * In theory, e_list is protected by mbus->lock. However,
  529. * after mon_reader_del has finished, the following is the case:
  530. * - we are not on reader list anymore, so new events won't be added;
  531. * - whole mbus may be dropped if it was orphaned.
  532. * So, we better not touch mbus.
  533. */
  534. /* spin_lock_irqsave(&mbus->lock, flags); */
  535. while (!list_empty(&rp->e_list)) {
  536. p = rp->e_list.next;
  537. ep = list_entry(p, struct mon_event_text, e_link);
  538. list_del(p);
  539. --rp->nevents;
  540. kmem_cache_free(rp->e_slab, ep);
  541. }
  542. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  543. kmem_cache_destroy(rp->e_slab);
  544. kfree(rp->printf_buf);
  545. kfree(rp);
  546. mutex_unlock(&mon_lock);
  547. return 0;
  548. }
  549. static const struct file_operations mon_fops_text_t = {
  550. .owner = THIS_MODULE,
  551. .open = mon_text_open,
  552. .llseek = no_llseek,
  553. .read = mon_text_read_t,
  554. .release = mon_text_release,
  555. };
  556. static const struct file_operations mon_fops_text_u = {
  557. .owner = THIS_MODULE,
  558. .open = mon_text_open,
  559. .llseek = no_llseek,
  560. .read = mon_text_read_u,
  561. .release = mon_text_release,
  562. };
  563. int mon_text_add(struct mon_bus *mbus, const struct usb_bus *ubus)
  564. {
  565. struct dentry *d;
  566. enum { NAMESZ = 10 };
  567. char name[NAMESZ];
  568. int busnum = ubus? ubus->busnum: 0;
  569. int rc;
  570. if (ubus != NULL) {
  571. rc = snprintf(name, NAMESZ, "%dt", busnum);
  572. if (rc <= 0 || rc >= NAMESZ)
  573. goto err_print_t;
  574. d = debugfs_create_file(name, 0600, mon_dir, mbus,
  575. &mon_fops_text_t);
  576. if (d == NULL)
  577. goto err_create_t;
  578. mbus->dent_t = d;
  579. }
  580. rc = snprintf(name, NAMESZ, "%du", busnum);
  581. if (rc <= 0 || rc >= NAMESZ)
  582. goto err_print_u;
  583. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_text_u);
  584. if (d == NULL)
  585. goto err_create_u;
  586. mbus->dent_u = d;
  587. rc = snprintf(name, NAMESZ, "%ds", busnum);
  588. if (rc <= 0 || rc >= NAMESZ)
  589. goto err_print_s;
  590. d = debugfs_create_file(name, 0600, mon_dir, mbus, &mon_fops_stat);
  591. if (d == NULL)
  592. goto err_create_s;
  593. mbus->dent_s = d;
  594. return 1;
  595. err_create_s:
  596. err_print_s:
  597. debugfs_remove(mbus->dent_u);
  598. mbus->dent_u = NULL;
  599. err_create_u:
  600. err_print_u:
  601. if (ubus != NULL) {
  602. debugfs_remove(mbus->dent_t);
  603. mbus->dent_t = NULL;
  604. }
  605. err_create_t:
  606. err_print_t:
  607. return 0;
  608. }
  609. void mon_text_del(struct mon_bus *mbus)
  610. {
  611. debugfs_remove(mbus->dent_u);
  612. if (mbus->dent_t != NULL)
  613. debugfs_remove(mbus->dent_t);
  614. debugfs_remove(mbus->dent_s);
  615. }
  616. /*
  617. * Slab interface: constructor.
  618. */
  619. static void mon_text_ctor(void *mem)
  620. {
  621. /*
  622. * Nothing to initialize. No, really!
  623. * So, we fill it with garbage to emulate a reused object.
  624. */
  625. memset(mem, 0xe5, sizeof(struct mon_event_text));
  626. }
  627. int __init mon_text_init(void)
  628. {
  629. struct dentry *mondir;
  630. mondir = debugfs_create_dir("usbmon", usb_debug_root);
  631. if (IS_ERR(mondir)) {
  632. printk(KERN_NOTICE TAG ": debugfs is not available\n");
  633. return -ENODEV;
  634. }
  635. if (mondir == NULL) {
  636. printk(KERN_NOTICE TAG ": unable to create usbmon directory\n");
  637. return -ENODEV;
  638. }
  639. mon_dir = mondir;
  640. return 0;
  641. }
  642. void mon_text_exit(void)
  643. {
  644. debugfs_remove(mon_dir);
  645. }