mon_text.c 19 KB

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