mon_text.c 19 KB

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