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 InitCheck()16 status_t InitCheck() { return fStatus; }; 17 18 status_t Open(); IsOpen()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(); IsRemoved()29 bool IsRemoved() { return fRemoved; }; 30 31 private: 32 static void _ReadCallback(void *cookie, int32 status, 33 void *data, size_t actualLength); 34 static void _WriteCallback(void *cookie, int32 status, 35 void *data, size_t actualLength); 36 static void _NotifyCallback(void *cookie, int32 status, 37 void *data, size_t actualLength); 38 39 status_t _SendCommand(const void*, size_t); 40 status_t _ReadResponse(void*, size_t); 41 42 status_t _RNDISInitialize(); 43 status_t _GetOID(uint32 oid, void* buffer, size_t length); 44 45 status_t _SetupDevice(); 46 status_t _ReadMACAddress(usb_device device, uint8 *buffer); 47 status_t _ReadMaxSegmentSize(usb_device device); 48 status_t _ReadMediaState(usb_device device); 49 status_t _ReadLinkSpeed(usb_device device); 50 status_t _EnableBroadcast(usb_device device); 51 52 // state tracking 53 status_t fStatus; 54 bool fOpen; 55 bool fRemoved; 56 int32 fInsideNotify; 57 usb_device fDevice; 58 uint16 fVendorID; 59 uint16 fProductID; 60 61 // interface and device infos 62 uint8 fDataInterfaceIndex; 63 uint32 fMaxSegmentSize; 64 65 // pipes for notifications and data io 66 usb_pipe fNotifyEndpoint; 67 usb_pipe fReadEndpoint; 68 usb_pipe fWriteEndpoint; 69 70 // data stores for async usb transfers 71 uint32 fActualLengthRead; 72 uint32 fActualLengthWrite; 73 int32 fStatusRead; 74 int32 fStatusWrite; 75 sem_id fNotifyReadSem; 76 sem_id fNotifyWriteSem; 77 sem_id fLockWriteSem; 78 sem_id fNotifyControlSem; 79 80 uint8 fNotifyBuffer[8]; 81 uint8 fReadBuffer[0x4000]; 82 uint32* fReadHeader; 83 84 // connection data 85 sem_id fLinkStateChangeSem; 86 uint8 fMACAddress[6]; 87 uint32 fMediaConnectState; 88 uint32 fDownstreamSpeed; 89 }; 90 91 #endif //_USB_RNDIS_DEVICE_H_ 92