haiku_usb.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Haiku Backend for libusb
  3. * Copyright © 2014 Akshay Jaggi <akshay1994.leo@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <List.h>
  20. #include <Locker.h>
  21. #include <Autolock.h>
  22. #include <USBKit.h>
  23. #include <map>
  24. #include "libusbi.h"
  25. #include "haiku_usb_raw.h"
  26. using namespace std;
  27. class USBDevice;
  28. class USBDeviceHandle;
  29. class USBTransfer;
  30. class USBDevice {
  31. public:
  32. USBDevice(const char *);
  33. virtual ~USBDevice();
  34. const char* Location() const;
  35. uint8 CountConfigurations() const;
  36. const usb_device_descriptor* Descriptor() const;
  37. const usb_configuration_descriptor* ConfigurationDescriptor(uint8) const;
  38. const usb_configuration_descriptor* ActiveConfiguration() const;
  39. uint8 EndpointToIndex(uint8) const;
  40. uint8 EndpointToInterface(uint8) const;
  41. int ClaimInterface(uint8);
  42. int ReleaseInterface(uint8);
  43. int CheckInterfacesFree(uint8);
  44. void SetActiveConfiguration(uint8);
  45. uint8 ActiveConfigurationIndex() const;
  46. bool InitCheck();
  47. private:
  48. int Initialise();
  49. unsigned int fClaimedInterfaces; // Max Interfaces can be 32. Using a bitmask
  50. usb_device_descriptor fDeviceDescriptor;
  51. unsigned char** fConfigurationDescriptors;
  52. uint8 fActiveConfiguration;
  53. char* fPath;
  54. map<uint8,uint8> fConfigToIndex;
  55. map<uint8,uint8>* fEndpointToIndex;
  56. map<uint8,uint8>* fEndpointToInterface;
  57. bool fInitCheck;
  58. };
  59. class USBDeviceHandle {
  60. public:
  61. USBDeviceHandle(USBDevice *dev);
  62. virtual ~USBDeviceHandle();
  63. int ClaimInterface(uint8);
  64. int ReleaseInterface(uint8);
  65. int SetConfiguration(uint8);
  66. int SetAltSetting(uint8, uint8);
  67. int ClearHalt(uint8);
  68. status_t SubmitTransfer(struct usbi_transfer *);
  69. status_t CancelTransfer(USBTransfer *);
  70. bool InitCheck();
  71. private:
  72. int fRawFD;
  73. static status_t TransfersThread(void *);
  74. void TransfersWorker();
  75. USBDevice* fUSBDevice;
  76. unsigned int fClaimedInterfaces;
  77. BList fTransfers;
  78. BLocker fTransfersLock;
  79. sem_id fTransfersSem;
  80. thread_id fTransfersThread;
  81. bool fInitCheck;
  82. };
  83. class USBTransfer {
  84. public:
  85. USBTransfer(struct usbi_transfer *, USBDevice *);
  86. virtual ~USBTransfer();
  87. void Do(int);
  88. struct usbi_transfer* UsbiTransfer();
  89. void SetCancelled();
  90. bool IsCancelled();
  91. private:
  92. struct usbi_transfer* fUsbiTransfer;
  93. struct libusb_transfer* fLibusbTransfer;
  94. USBDevice* fUSBDevice;
  95. BLocker fStatusLock;
  96. bool fCancelled;
  97. };
  98. class USBRoster {
  99. public:
  100. USBRoster();
  101. virtual ~USBRoster();
  102. int Start();
  103. void Stop();
  104. private:
  105. void* fLooper;
  106. };