setup.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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,
  37. "usbd: getmaxpkt: error writing usb device request: GET_DESCRIPTOR for %D: %r\n",
  38. d);
  39. return -1;
  40. }
  41. if ((nr = setupreply(d->ep[0], buf, sizeof buf)) < sizeof buf) {
  42. fprint(2,
  43. "usbd: getmaxpkt: error reading device descriptor for %D, got %d of %d: %r\n",
  44. d, nr, sizeof buf);
  45. return -1;
  46. }
  47. dd = (DDevice*)buf;
  48. return dd->bMaxPacketSize0;
  49. }
  50. int
  51. setaddress(Device *d, int id)
  52. {
  53. if (setupreq(d->ep[0], RH2D, SET_ADDRESS, id, 0, 0) < 0) {
  54. fprint(2, "usbd: set address %D <- %d failed\n", d, id);
  55. return -1;
  56. }
  57. return 0;
  58. }