USBHostMSD.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* mbed USBHost Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef USBHOSTMSD_H
  17. #define USBHOSTMSD_H
  18. #include "USBHostConf.h"
  19. #if USBHOST_MSD
  20. #include "USBHost.h"
  21. #include "FATFileSystem.h"
  22. /**
  23. * A class to communicate a USB flash disk
  24. */
  25. class USBHostMSD : public IUSBEnumerator, public FATFileSystem {
  26. public:
  27. /**
  28. * Constructor
  29. *
  30. * @param rootdir mount name
  31. */
  32. USBHostMSD(const char * rootdir);
  33. /**
  34. * Check if a MSD device is connected
  35. *
  36. * @return true if a MSD device is connected
  37. */
  38. bool connected();
  39. /**
  40. * Try to connect to a MSD device
  41. *
  42. * @return true if connection was successful
  43. */
  44. bool connect();
  45. protected:
  46. //From IUSBEnumerator
  47. virtual void setVidPid(uint16_t vid, uint16_t pid);
  48. virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
  49. virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
  50. // From FATFileSystem
  51. virtual int disk_initialize();
  52. virtual int disk_status() {return 0;};
  53. virtual int disk_read(uint8_t* buffer, uint64_t sector, uint8_t count);
  54. virtual int disk_write(const uint8_t* buffer, uint64_t sector, uint8_t count);
  55. virtual int disk_sync() {return 0;};
  56. virtual uint64_t disk_sectors();
  57. private:
  58. USBHost * host;
  59. USBDeviceConnected * dev;
  60. bool dev_connected;
  61. USBEndpoint * bulk_in;
  62. USBEndpoint * bulk_out;
  63. uint8_t nb_ep;
  64. // Bulk-only CBW
  65. typedef struct {
  66. uint32_t Signature;
  67. uint32_t Tag;
  68. uint32_t DataLength;
  69. uint8_t Flags;
  70. uint8_t LUN;
  71. uint8_t CBLength;
  72. uint8_t CB[16];
  73. } PACKED CBW;
  74. // Bulk-only CSW
  75. typedef struct {
  76. uint32_t Signature;
  77. uint32_t Tag;
  78. uint32_t DataResidue;
  79. uint8_t Status;
  80. } PACKED CSW;
  81. CBW cbw;
  82. CSW csw;
  83. int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len);
  84. int testUnitReady();
  85. int readCapacity();
  86. int inquiry(uint8_t lun, uint8_t page_code);
  87. int SCSIRequestSense();
  88. int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction);
  89. int checkResult(uint8_t res, USBEndpoint * ep);
  90. int getMaxLun();
  91. int blockSize;
  92. uint64_t blockCount;
  93. int msd_intf;
  94. bool msd_device_found;
  95. bool disk_init;
  96. void init();
  97. };
  98. #endif
  99. #endif