mon_text.c 11 KB

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