setup.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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: %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 = -1;
  33. if (setupreq(d->ep[0], RD2H|Rstandard|Rdevice, GET_DESCRIPTOR, (DEVICE<<8)|0, 0, sizeof(buf)) < 0 ||
  34. (nr = setupreply(d->ep[0], buf, sizeof(buf))) < sizeof(buf)) {
  35. fprint(2, "usbd: getmaxpkt: error reading device descriptor for %D, got %d of %d\n", d, nr, sizeof(buf));
  36. return -1;
  37. }
  38. dd = (DDevice*)buf;
  39. return dd->bMaxPacketSize0;
  40. }
  41. int
  42. setaddress(Device *d, int id)
  43. {
  44. if (setupreq(d->ep[0], RH2D, SET_ADDRESS, id, 0, 0) < 0) {
  45. fprint(2, "usbd: set address %D <- %d failed\n", d, id);
  46. return -1;
  47. }
  48. return 0;
  49. }