mon_text.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <asm/uaccess.h>
  11. #include "usb_mon.h"
  12. /*
  13. * No, we do not want arbitrarily long data strings.
  14. * Use the binary interface if you want to capture bulk data!
  15. */
  16. #define DATA_MAX 32
  17. /*
  18. * Defined by USB 2.0 clause 9.3, table 9.2.
  19. */
  20. #define SETUP_MAX 8
  21. /*
  22. * This limit exists to prevent OOMs when the user process stops reading.
  23. */
  24. #define EVENT_MAX 25
  25. #define PRINTF_DFL 130
  26. struct mon_event_text {
  27. struct list_head e_link;
  28. int type; /* submit, complete, etc. */
  29. unsigned int pipe; /* Pipe */
  30. unsigned long id; /* From pointer, most of the time */
  31. unsigned int tstamp;
  32. int length; /* Depends on type: xfer length or act length */
  33. int status;
  34. char setup_flag;
  35. char data_flag;
  36. unsigned char setup[SETUP_MAX];
  37. unsigned char data[DATA_MAX];
  38. };
  39. #define SLAB_NAME_SZ 30
  40. struct mon_reader_text {
  41. kmem_cache_t *e_slab;
  42. int nevents;
  43. struct list_head e_list;
  44. struct mon_reader r; /* In C, parent class can be placed anywhere */
  45. wait_queue_head_t wait;
  46. int printf_size;
  47. char *printf_buf;
  48. struct semaphore printf_lock;
  49. char slab_name[SLAB_NAME_SZ];
  50. };
  51. static void mon_text_ctor(void *, kmem_cache_t *, unsigned long);
  52. static void mon_text_dtor(void *, kmem_cache_t *, unsigned long);
  53. /*
  54. * mon_text_submit
  55. * mon_text_complete
  56. *
  57. * May be called from an interrupt.
  58. *
  59. * This is called with the whole mon_bus locked, so no additional lock.
  60. */
  61. static inline char mon_text_get_setup(struct mon_event_text *ep,
  62. struct urb *urb, char ev_type)
  63. {
  64. if (!usb_pipecontrol(urb->pipe) || ev_type != 'S')
  65. return '-';
  66. if (urb->transfer_flags & URB_NO_SETUP_DMA_MAP)
  67. return mon_dmapeek(ep->setup, urb->setup_dma, SETUP_MAX);
  68. if (urb->setup_packet == NULL)
  69. return 'Z'; /* '0' would be not as pretty. */
  70. memcpy(ep->setup, urb->setup_packet, SETUP_MAX);
  71. return 0;
  72. }
  73. static inline char mon_text_get_data(struct mon_event_text *ep, struct urb *urb,
  74. int len, char ev_type)
  75. {
  76. int pipe = urb->pipe;
  77. if (len <= 0)
  78. return 'L';
  79. if (len >= DATA_MAX)
  80. len = DATA_MAX;
  81. /*
  82. * Bulk is easy to shortcut reliably.
  83. * XXX Other pipe types need consideration. Currently, we overdo it
  84. * and collect garbage for them: better more than less.
  85. */
  86. if (usb_pipebulk(pipe) || usb_pipecontrol(pipe)) {
  87. if (usb_pipein(pipe)) {
  88. if (ev_type == 'S')
  89. return '<';
  90. } else {
  91. if (ev_type == 'C')
  92. return '>';
  93. }
  94. }
  95. /*
  96. * The check to see if it's safe to poke at data has an enormous
  97. * number of corner cases, but it seems that the following is
  98. * more or less safe.
  99. *
  100. * We do not even try to look transfer_buffer, because it can
  101. * contain non-NULL garbage in case the upper level promised to
  102. * set DMA for the HCD.
  103. */
  104. if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
  105. return mon_dmapeek(ep->data, urb->transfer_dma, len);
  106. if (urb->transfer_buffer == NULL)
  107. return 'Z'; /* '0' would be not as pretty. */
  108. memcpy(ep->data, urb->transfer_buffer, len);
  109. return 0;
  110. }
  111. static inline unsigned int mon_get_timestamp(void)
  112. {
  113. struct timeval tval;
  114. unsigned int stamp;
  115. do_gettimeofday(&tval);
  116. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s. */
  117. stamp = stamp * 1000000 + tval.tv_usec;
  118. return stamp;
  119. }
  120. static void mon_text_event(struct mon_reader_text *rp, struct urb *urb,
  121. char ev_type)
  122. {
  123. struct mon_event_text *ep;
  124. unsigned int stamp;
  125. stamp = mon_get_timestamp();
  126. if (rp->nevents >= EVENT_MAX ||
  127. (ep = kmem_cache_alloc(rp->e_slab, SLAB_ATOMIC)) == NULL) {
  128. rp->r.m_bus->cnt_text_lost++;
  129. return;
  130. }
  131. ep->type = ev_type;
  132. ep->pipe = urb->pipe;
  133. ep->id = (unsigned long) urb;
  134. ep->tstamp = stamp;
  135. ep->length = (ev_type == 'S') ?
  136. urb->transfer_buffer_length : urb->actual_length;
  137. /* Collecting status makes debugging sense for submits, too */
  138. ep->status = urb->status;
  139. ep->setup_flag = mon_text_get_setup(ep, urb, ev_type);
  140. ep->data_flag = mon_text_get_data(ep, urb, ep->length, ev_type);
  141. rp->nevents++;
  142. list_add_tail(&ep->e_link, &rp->e_list);
  143. wake_up(&rp->wait);
  144. }
  145. static void mon_text_submit(void *data, struct urb *urb)
  146. {
  147. struct mon_reader_text *rp = data;
  148. mon_text_event(rp, urb, 'S');
  149. }
  150. static void mon_text_complete(void *data, struct urb *urb)
  151. {
  152. struct mon_reader_text *rp = data;
  153. mon_text_event(rp, urb, 'C');
  154. }
  155. /*
  156. * Fetch next event from the circular buffer.
  157. */
  158. static struct mon_event_text *mon_text_fetch(struct mon_reader_text *rp,
  159. struct mon_bus *mbus)
  160. {
  161. struct list_head *p;
  162. unsigned long flags;
  163. spin_lock_irqsave(&mbus->lock, flags);
  164. if (list_empty(&rp->e_list)) {
  165. spin_unlock_irqrestore(&mbus->lock, flags);
  166. return NULL;
  167. }
  168. p = rp->e_list.next;
  169. list_del(p);
  170. --rp->nevents;
  171. spin_unlock_irqrestore(&mbus->lock, flags);
  172. return list_entry(p, struct mon_event_text, e_link);
  173. }
  174. /*
  175. */
  176. static int mon_text_open(struct inode *inode, struct file *file)
  177. {
  178. struct mon_bus *mbus;
  179. struct usb_bus *ubus;
  180. struct mon_reader_text *rp;
  181. int rc;
  182. down(&mon_lock);
  183. mbus = inode->u.generic_ip;
  184. ubus = mbus->u_bus;
  185. rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
  186. if (rp == NULL) {
  187. rc = -ENOMEM;
  188. goto err_alloc;
  189. }
  190. memset(rp, 0, sizeof(struct mon_reader_text));
  191. INIT_LIST_HEAD(&rp->e_list);
  192. init_waitqueue_head(&rp->wait);
  193. init_MUTEX(&rp->printf_lock);
  194. rp->printf_size = PRINTF_DFL;
  195. rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
  196. if (rp->printf_buf == NULL) {
  197. rc = -ENOMEM;
  198. goto err_alloc_pr;
  199. }
  200. rp->r.m_bus = mbus;
  201. rp->r.r_data = rp;
  202. rp->r.rnf_submit = mon_text_submit;
  203. rp->r.rnf_complete = mon_text_complete;
  204. snprintf(rp->slab_name, SLAB_NAME_SZ, "mon%dt_%lx", ubus->busnum,
  205. (long)rp);
  206. rp->e_slab = kmem_cache_create(rp->slab_name,
  207. sizeof(struct mon_event_text), sizeof(long), 0,
  208. mon_text_ctor, mon_text_dtor);
  209. if (rp->e_slab == NULL) {
  210. rc = -ENOMEM;
  211. goto err_slab;
  212. }
  213. mon_reader_add(mbus, &rp->r);
  214. file->private_data = rp;
  215. up(&mon_lock);
  216. return 0;
  217. // err_busy:
  218. // kmem_cache_destroy(rp->e_slab);
  219. err_slab:
  220. kfree(rp->printf_buf);
  221. err_alloc_pr:
  222. kfree(rp);
  223. err_alloc:
  224. up(&mon_lock);
  225. return rc;
  226. }
  227. /*
  228. * For simplicity, we read one record in one system call and throw out
  229. * what does not fit. This means that the following does not work:
  230. * dd if=/dbg/usbmon/0t bs=10
  231. * Also, we do not allow seeks and do not bother advancing the offset.
  232. */
  233. static ssize_t mon_text_read(struct file *file, char __user *buf,
  234. size_t nbytes, loff_t *ppos)
  235. {
  236. struct mon_reader_text *rp = file->private_data;
  237. struct mon_bus *mbus = rp->r.m_bus;
  238. DECLARE_WAITQUEUE(waita, current);
  239. struct mon_event_text *ep;
  240. int cnt, limit;
  241. char *pbuf;
  242. char udir, utype;
  243. int data_len, i;
  244. add_wait_queue(&rp->wait, &waita);
  245. set_current_state(TASK_INTERRUPTIBLE);
  246. while ((ep = mon_text_fetch(rp, mbus)) == NULL) {
  247. if (file->f_flags & O_NONBLOCK) {
  248. set_current_state(TASK_RUNNING);
  249. remove_wait_queue(&rp->wait, &waita);
  250. return -EWOULDBLOCK; /* Same as EAGAIN in Linux */
  251. }
  252. /*
  253. * We do not count nwaiters, because ->release is supposed
  254. * to be called when all openers are gone only.
  255. */
  256. schedule();
  257. if (signal_pending(current)) {
  258. remove_wait_queue(&rp->wait, &waita);
  259. return -EINTR;
  260. }
  261. set_current_state(TASK_INTERRUPTIBLE);
  262. }
  263. set_current_state(TASK_RUNNING);
  264. remove_wait_queue(&rp->wait, &waita);
  265. down(&rp->printf_lock);
  266. cnt = 0;
  267. pbuf = rp->printf_buf;
  268. limit = rp->printf_size;
  269. udir = usb_pipein(ep->pipe) ? 'i' : 'o';
  270. switch (usb_pipetype(ep->pipe)) {
  271. case PIPE_ISOCHRONOUS: utype = 'Z'; break;
  272. case PIPE_INTERRUPT: utype = 'I'; break;
  273. case PIPE_CONTROL: utype = 'C'; break;
  274. default: /* PIPE_BULK */ utype = 'B';
  275. }
  276. cnt += snprintf(pbuf + cnt, limit - cnt,
  277. "%lx %u %c %c%c:%03u:%02u",
  278. ep->id, ep->tstamp, ep->type,
  279. utype, udir, usb_pipedevice(ep->pipe), usb_pipeendpoint(ep->pipe));
  280. if (ep->setup_flag == 0) { /* Setup packet is present and captured */
  281. cnt += snprintf(pbuf + cnt, limit - cnt,
  282. " s %02x %02x %04x %04x %04x",
  283. ep->setup[0],
  284. ep->setup[1],
  285. (ep->setup[3] << 8) | ep->setup[2],
  286. (ep->setup[5] << 8) | ep->setup[4],
  287. (ep->setup[7] << 8) | ep->setup[6]);
  288. } else if (ep->setup_flag != '-') { /* Unable to capture setup packet */
  289. cnt += snprintf(pbuf + cnt, limit - cnt,
  290. " %c __ __ ____ ____ ____", ep->setup_flag);
  291. } else { /* No setup for this kind of URB */
  292. cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->status);
  293. }
  294. cnt += snprintf(pbuf + cnt, limit - cnt, " %d", ep->length);
  295. if ((data_len = ep->length) > 0) {
  296. if (ep->data_flag == 0) {
  297. cnt += snprintf(pbuf + cnt, limit - cnt, " =");
  298. if (data_len >= DATA_MAX)
  299. data_len = DATA_MAX;
  300. for (i = 0; i < data_len; i++) {
  301. if (i % 4 == 0) {
  302. cnt += snprintf(pbuf + cnt, limit - cnt,
  303. " ");
  304. }
  305. cnt += snprintf(pbuf + cnt, limit - cnt,
  306. "%02x", ep->data[i]);
  307. }
  308. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  309. } else {
  310. cnt += snprintf(pbuf + cnt, limit - cnt,
  311. " %c\n", ep->data_flag);
  312. }
  313. } else {
  314. cnt += snprintf(pbuf + cnt, limit - cnt, "\n");
  315. }
  316. if (copy_to_user(buf, rp->printf_buf, cnt))
  317. cnt = -EFAULT;
  318. up(&rp->printf_lock);
  319. kmem_cache_free(rp->e_slab, ep);
  320. return cnt;
  321. }
  322. static int mon_text_release(struct inode *inode, struct file *file)
  323. {
  324. struct mon_reader_text *rp = file->private_data;
  325. struct mon_bus *mbus;
  326. /* unsigned long flags; */
  327. struct list_head *p;
  328. struct mon_event_text *ep;
  329. down(&mon_lock);
  330. mbus = inode->u.generic_ip;
  331. if (mbus->nreaders <= 0) {
  332. printk(KERN_ERR TAG ": consistency error on close\n");
  333. up(&mon_lock);
  334. return 0;
  335. }
  336. mon_reader_del(mbus, &rp->r);
  337. /*
  338. * In theory, e_list is protected by mbus->lock. However,
  339. * after mon_reader_del has finished, the following is the case:
  340. * - we are not on reader list anymore, so new events won't be added;
  341. * - whole mbus may be dropped if it was orphaned.
  342. * So, we better not touch mbus.
  343. */
  344. /* spin_lock_irqsave(&mbus->lock, flags); */
  345. while (!list_empty(&rp->e_list)) {
  346. p = rp->e_list.next;
  347. ep = list_entry(p, struct mon_event_text, e_link);
  348. list_del(p);
  349. --rp->nevents;
  350. kmem_cache_free(rp->e_slab, ep);
  351. }
  352. /* spin_unlock_irqrestore(&mbus->lock, flags); */
  353. kmem_cache_destroy(rp->e_slab);
  354. kfree(rp->printf_buf);
  355. kfree(rp);
  356. up(&mon_lock);
  357. return 0;
  358. }
  359. struct file_operations mon_fops_text = {
  360. .owner = THIS_MODULE,
  361. .open = mon_text_open,
  362. .llseek = no_llseek,
  363. .read = mon_text_read,
  364. /* .write = mon_text_write, */
  365. /* .poll = mon_text_poll, */
  366. /* .ioctl = mon_text_ioctl, */
  367. .release = mon_text_release,
  368. };
  369. /*
  370. * Slab interface: constructor.
  371. */
  372. static void mon_text_ctor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  373. {
  374. /*
  375. * Nothing to initialize. No, really!
  376. * So, we fill it with garbage to emulate a reused object.
  377. */
  378. memset(mem, 0xe5, sizeof(struct mon_event_text));
  379. }
  380. static void mon_text_dtor(void *mem, kmem_cache_t *slab, unsigned long sflags)
  381. {
  382. ;
  383. }