usb_dfu_vendor.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * usb_dfu_vendor.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include "usb.h"
  27. #include "spi.h"
  28. #define USB_RT_DFU_VENDOR_VERSION ((0 << 8) | 0xc1)
  29. #define USB_RT_DFU_VENDOR_SPI_EXEC ((1 << 8) | 0x41)
  30. #define USB_RT_DFU_VENDOR_SPI_RESULT ((2 << 8) | 0xc1)
  31. static bool
  32. _dfu_vendor_spi_exec_cb(struct usb_xfer *xfer)
  33. {
  34. struct spi_xfer_chunk sx[1] = {
  35. { .data = xfer->data, .len = xfer->len, .read = true, .write = true, },
  36. };
  37. spi_xfer(SPI_CS_FLASH, sx, 1);
  38. return true;
  39. }
  40. enum usb_fnd_resp
  41. dfu_vendor_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
  42. {
  43. switch (req->wRequestAndType)
  44. {
  45. case USB_RT_DFU_VENDOR_VERSION:
  46. xfer->len = 2;
  47. xfer->data[0] = 0x01;
  48. xfer->data[1] = 0x00;
  49. break;
  50. case USB_RT_DFU_VENDOR_SPI_EXEC:
  51. xfer->cb_done = _dfu_vendor_spi_exec_cb;
  52. break;
  53. case USB_RT_DFU_VENDOR_SPI_RESULT:
  54. /* Really nothing to do, data is already in the buffer, and we serve
  55. * whatever the host requested ... */
  56. break;
  57. default:
  58. return USB_FND_ERROR;
  59. }
  60. return USB_FND_SUCCESS;
  61. }