dump.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <thread.h>
  12. #include <bio.h>
  13. #include <usb/usb.h>
  14. int usbdebug;
  15. static char *edir[] = {"in", "out", "inout"};
  16. static char *etype[] = {"ctl", "iso", "bulk", "intr"};
  17. static char* cnames[] =
  18. {
  19. "none", "audio", "comms", "hid", "",
  20. "", "", "printer", "storage", "hub", "data"
  21. };
  22. /*
  23. static char* devstates[] =
  24. {
  25. "detached", "attached", "enabled", "assigned", "configured"
  26. };
  27. */
  28. char*
  29. classname(int c)
  30. {
  31. static char buf[30];
  32. if(c >= 0 && c < nelem(cnames))
  33. return cnames[c];
  34. else{
  35. seprint(buf, buf+30, "%d", c);
  36. return buf;
  37. }
  38. }
  39. char *
  40. hexstr(void *a, int n)
  41. {
  42. int i;
  43. char *dbuff, *s, *e;
  44. uint8_t *b;
  45. b = a;
  46. dbuff = s = emallocz(1024, 0);
  47. *s = 0;
  48. e = s + 1024;
  49. for(i = 0; i < n; i++)
  50. s = seprint(s, e, " %.2x", b[i]);
  51. if(s == e)
  52. fprint(2, "%s: usb/lib: hexdump: bug: small buffer\n", argv0);
  53. return dbuff;
  54. }
  55. static char *
  56. seprintiface(char *s, char *e, Iface *i)
  57. {
  58. int j;
  59. Altc *a;
  60. Ep *ep;
  61. char *eds, *ets;
  62. s = seprint(s, e, "\t\tiface csp %s.%lu.%lu\n",
  63. classname(Class(i->csp)), Subclass(i->csp), Proto(i->csp));
  64. for(j = 0; j < Naltc; j++){
  65. a=i->altc[j];
  66. if(a == nil)
  67. break;
  68. s = seprint(s, e, "\t\t alt %d attr %d ival %d",
  69. j, a->attrib, a->interval);
  70. if(a->aux != nil)
  71. s = seprint(s, e, " devspec %p\n", a->aux);
  72. else
  73. s = seprint(s, e, "\n");
  74. }
  75. for(j = 0; j < Nep; j++){
  76. ep = i->ep[j];
  77. if(ep == nil)
  78. break;
  79. eds = ets = "";
  80. if(ep->dir <= nelem(edir))
  81. eds = edir[ep->dir];
  82. if(ep->type <= nelem(etype))
  83. ets = etype[ep->type];
  84. s = seprint(s, e, "\t\t ep id %d addr %d dir %s type %s"
  85. " itype %d maxpkt %d ntds %d\n",
  86. ep->id, ep->addr, eds, ets, ep->isotype,
  87. ep->maxpkt, ep->ntds);
  88. }
  89. return s;
  90. }
  91. static char*
  92. seprintconf(char *s, char *e, Usbdev *d, int ci)
  93. {
  94. int i;
  95. Conf *c;
  96. char *hd;
  97. c = d->conf[ci];
  98. s = seprint(s, e, "\tconf: cval %d attrib %x %d mA\n",
  99. c->cval, c->attrib, c->milliamps);
  100. for(i = 0; i < Niface; i++)
  101. if(c->iface[i] == nil)
  102. break;
  103. else
  104. s = seprintiface(s, e, c->iface[i]);
  105. for(i = 0; i < Nddesc; i++)
  106. if(d->ddesc[i] == nil)
  107. break;
  108. else if(d->ddesc[i]->conf == c){
  109. hd = hexstr((uint8_t*)&d->ddesc[i]->data,
  110. d->ddesc[i]->data.bLength);
  111. s = seprint(s, e, "\t\tdev desc %x[%d]: %s\n",
  112. d->ddesc[i]->data.bDescriptorType,
  113. d->ddesc[i]->data.bLength, hd);
  114. free(hd);
  115. }
  116. return s;
  117. }
  118. int
  119. Ufmt(Fmt *f)
  120. {
  121. int i;
  122. Dev *d;
  123. Usbdev *ud;
  124. char buf[1024];
  125. char *s, *e;
  126. s = buf;
  127. e = buf+sizeof(buf);
  128. d = va_arg(f->args, Dev*);
  129. if(d == nil)
  130. return fmtprint(f, "<nildev>\n");
  131. s = seprint(s, e, "%s", d->dir);
  132. ud = d->usb;
  133. if(ud == nil)
  134. return fmtprint(f, "%s %ld refs\n", buf, d->Ref.ref);
  135. s = seprint(s, e, " csp %s.%lu.%lu",
  136. classname(Class(ud->csp)), Subclass(ud->csp), Proto(ud->csp));
  137. s = seprint(s, e, " vid %#x did %#x", ud->vid, ud->did);
  138. s = seprint(s, e, " refs %ld\n", d->Ref.ref);
  139. s = seprint(s, e, "\t%s %s %s\n", ud->vendor, ud->product, ud->serial);
  140. for(i = 0; i < Nconf; i++){
  141. if(ud->conf[i] == nil)
  142. break;
  143. else
  144. s = seprintconf(s, e, ud, i);
  145. }
  146. return fmtprint(f, "%s", buf);
  147. }
  148. char*
  149. estrdup(char *s)
  150. {
  151. char *d;
  152. d = strdup(s);
  153. if(d == nil)
  154. sysfatal("strdup: %r");
  155. setmalloctag(d, getcallerpc());
  156. return d;
  157. }
  158. void*
  159. emallocz(uint32_t size, int zero)
  160. {
  161. void *x;
  162. x = malloc(size);
  163. if(x == nil)
  164. sysfatal("malloc: %r");
  165. if(zero)
  166. memset(x, 0, size);
  167. setmalloctag(x, getcallerpc());
  168. return x;
  169. }