1 /* 2 Driver for USB RNDIS network devices 3 Copyright (C) 2022 Adrien Destugues <pulkomandy@pulkomandy.tk> 4 Distributed under the terms of the MIT license. 5 */ 6 #ifndef _USB_RNDIS_DEVICE_H_ 7 #define _USB_RNDIS_DEVICE_H_ 8 9 #include "Driver.h" 10 11 class RNDISDevice { 12 public: 13 RNDISDevice(usb_device device); 14 ~RNDISDevice(); 15 16 status_t InitCheck() { return fStatus; }; 17 18 status_t Open(); 19 bool IsOpen() { return fOpen; }; 20 21 status_t Close(); 22 status_t Free(); 23 24 status_t Read(uint8 *buffer, size_t *numBytes); 25 status_t Write(const uint8 *buffer, size_t *numBytes); 26 status_t Control(uint32 op, void *buffer, size_t length); 27 28 void Removed(); 29 bool IsRemoved() { return fRemoved; }; 30 31 status_t CompareAndReattach(usb_device device); 32 33 private: 34 static void _ReadCallback(void *cookie, int32 status, 35 void *data, size_t actualLength); 36 static void _WriteCallback(void *cookie, int32 status, 37 void *data, size_t actualLength); 38 static void _NotifyCallback(void *cookie, int32 status, 39 void *data, size_t actualLength); 40 41 status_t _SendCommand(const void*, size_t); 42 status_t _ReadResponse(void*, size_t); 43 44 status_t _RNDISInitialize(); 45 status_t _GetOID(uint32 oid, void* buffer, size_t length); 46 47 status_t _SetupDevice(); 48 status_t _ReadMACAddress(usb_device device, uint8 *buffer); 49 status_t _ReadMaxSegmentSize(usb_device device); 50 status_t _ReadMediaState(usb_device device); 51 status_t _ReadLinkSpeed(usb_device device); 52 status_t _EnableBroadcast(usb_device device); 53 54 // state tracking 55 status_t fStatus; 56 bool fOpen; 57 bool fRemoved; 58 int32 fInsideNotify; 59 usb_device fDevice; 60 uint16 fVendorID; 61 uint16 fProductID; 62 63 // interface and device infos 64 uint8 fDataInterfaceIndex; 65 uint32 fMaxSegmentSize; 66 67 // pipes for notifications and data io 68 usb_pipe fNotifyEndpoint; 69 usb_pipe fReadEndpoint; 70 usb_pipe fWriteEndpoint; 71 72 // data stores for async usb transfers 73 uint32 fActualLengthRead; 74 uint32 fActualLengthWrite; 75 int32 fStatusRead; 76 int32 fStatusWrite; 77 sem_id fNotifyReadSem; 78 sem_id fNotifyWriteSem; 79 sem_id fLockWriteSem; 80 sem_id fNotifyControlSem; 81 82 uint8 fNotifyBuffer[8]; 83 uint8 fReadBuffer[0x4000]; 84 uint32* fReadHeader; 85 86 // connection data 87 sem_id fLinkStateChangeSem; 88 uint8 fMACAddress[6]; 89 uint32 fMediaConnectState; 90 uint32 fDownstreamSpeed; 91 }; 92 93 #endif //_USB_RNDIS_DEVICE_H_ 94