setup.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include "usb.h"
  5. #include "dat.h"
  6. #include "fns.h"
  7. void
  8. devspeed(Device *d, int ls)
  9. {
  10. if(fprint(d->ctl, "speed %d", !ls) < 0)
  11. sysfatal("devspeed: write error: %r");
  12. if(debug)
  13. fprint(2, "usbd: %D: set speed %s\n", d, ls?"low":"high");
  14. }
  15. void
  16. setup0(Device *d, int type, int req, int value, int index, int count)
  17. {
  18. if(setupreq(d->ep[0], type, req, value, index, count) < 0)
  19. sysfatal("usbd: setup0: %D: transaction error", d);
  20. }
  21. void
  22. setconfig(Device *d, int n)
  23. {
  24. setup0(d, RH2D|Rstandard|Rdevice, SET_CONFIGURATION, n, 0, 0);
  25. d->state = Configured;
  26. }
  27. int
  28. getmaxpkt(Device *d)
  29. {
  30. DDevice *dd;
  31. byte buf[8];
  32. int nr;
  33. werrstr("");
  34. if(setupreq(d->ep[0], RD2H|Rstandard|Rdevice, GET_DESCRIPTOR,
  35. DEVICE<<8|0, 0, sizeof buf) < 0){
  36. fprint(2, "usbd: getmaxpkt: error writing usb device request: "
  37. "GET_DESCRIPTOR for %D: %r\n", d);
  38. return -1;
  39. }
  40. if((nr = setupreply(d->ep[0], buf, sizeof buf)) < sizeof buf){
  41. fprint(2, "usbd: getmaxpkt: error reading device descriptor "
  42. "for %D, got %d of %d: %r\n", d, nr, sizeof buf);
  43. return -1;
  44. }
  45. dd = (DDevice*)buf;
  46. return dd->bMaxPacketSize0;
  47. }
  48. int
  49. setaddress(Device *d, int id)
  50. {
  51. if(setupreq(d->ep[0], RH2D, SET_ADDRESS, id, 0, 0) < 0){
  52. fprint(2, "usbd: set address %D <- %d failed\n", d, id);
  53. return -1;
  54. }
  55. return 0;
  56. }