mon_text.c 19 KB

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