1853e6be8SMichael Lotz /* 2853e6be8SMichael Lotz * Copyright 2003-2006, Haiku Inc. All rights reserved. 3853e6be8SMichael Lotz * Distributed under the terms of the MIT License. 4853e6be8SMichael Lotz * 5853e6be8SMichael Lotz * Authors: 6853e6be8SMichael Lotz * Michael Lotz <mmlr@mlotz.ch> 7853e6be8SMichael Lotz * Niels S. Reedijk 8853e6be8SMichael Lotz */ 9cc9f959dSMichael Lotz #ifndef _USB_PRIVATE_H 10cc9f959dSMichael Lotz #define _USB_PRIVATE_H 11853e6be8SMichael Lotz 12cc9f959dSMichael Lotz #include "usbspec_private.h" 13853e6be8SMichael Lotz #include <lock.h> 140e76cf0bSJérôme Duval #include <util/Vector.h> 15853e6be8SMichael Lotz 16853e6be8SMichael Lotz 17853e6be8SMichael Lotz #define TRACE_OUTPUT(x, y, z...) \ 18853e6be8SMichael Lotz { \ 19d8b4cfc9SRene Gollent dprintf("usb %s%s %" B_PRId32 ": ", y, (x)->TypeName(), (x)->USBID()); \ 20853e6be8SMichael Lotz dprintf(z); \ 21853e6be8SMichael Lotz } 22853e6be8SMichael Lotz 23853e6be8SMichael Lotz //#define TRACE_USB 24853e6be8SMichael Lotz #ifdef TRACE_USB 25853e6be8SMichael Lotz #define TRACE(x...) TRACE_OUTPUT(this, "", x) 26853e6be8SMichael Lotz #define TRACE_STATIC(x, y...) TRACE_OUTPUT(x, "", y) 27853e6be8SMichael Lotz #define TRACE_MODULE(x...) dprintf("usb " USB_MODULE_NAME ": " x) 28853e6be8SMichael Lotz #else 29853e6be8SMichael Lotz #define TRACE(x...) /* nothing */ 30853e6be8SMichael Lotz #define TRACE_STATIC(x, y...) /* nothing */ 31853e6be8SMichael Lotz #define TRACE_MODULE(x...) /* nothing */ 32853e6be8SMichael Lotz #endif 33853e6be8SMichael Lotz 34853e6be8SMichael Lotz #define TRACE_ALWAYS(x...) TRACE_OUTPUT(this, "", x) 35853e6be8SMichael Lotz #define TRACE_ERROR(x...) TRACE_OUTPUT(this, "error ", x) 36853e6be8SMichael Lotz #define TRACE_MODULE_ALWAYS(x...) dprintf("usb " USB_MODULE_NAME ": " x) 37853e6be8SMichael Lotz #define TRACE_MODULE_ERROR(x...) dprintf("usb " USB_MODULE_NAME ": " x) 38853e6be8SMichael Lotz 39853e6be8SMichael Lotz class Hub; 40853e6be8SMichael Lotz class Stack; 41853e6be8SMichael Lotz class Device; 42853e6be8SMichael Lotz class Transfer; 43853e6be8SMichael Lotz class BusManager; 44853e6be8SMichael Lotz class Pipe; 45853e6be8SMichael Lotz class ControlPipe; 46853e6be8SMichael Lotz class Object; 47853e6be8SMichael Lotz class PhysicalMemoryAllocator; 48853e6be8SMichael Lotz 49853e6be8SMichael Lotz 50853e6be8SMichael Lotz struct usb_host_controller_info { 51853e6be8SMichael Lotz module_info info; 52853e6be8SMichael Lotz status_t (*control)(uint32 op, void *data, size_t length); 53853e6be8SMichael Lotz status_t (*add_to)(Stack *stack); 54853e6be8SMichael Lotz }; 55853e6be8SMichael Lotz 56853e6be8SMichael Lotz 57853e6be8SMichael Lotz struct usb_driver_cookie { 58853e6be8SMichael Lotz usb_id device; 59853e6be8SMichael Lotz void *cookie; 60853e6be8SMichael Lotz usb_driver_cookie *link; 61853e6be8SMichael Lotz }; 62853e6be8SMichael Lotz 63853e6be8SMichael Lotz 64853e6be8SMichael Lotz struct usb_driver_info { 65853e6be8SMichael Lotz const char *driver_name; 66853e6be8SMichael Lotz usb_support_descriptor *support_descriptors; 67853e6be8SMichael Lotz uint32 support_descriptor_count; 68853e6be8SMichael Lotz const char *republish_driver_name; 69853e6be8SMichael Lotz usb_notify_hooks notify_hooks; 70853e6be8SMichael Lotz usb_driver_cookie *cookies; 71853e6be8SMichael Lotz usb_driver_info *link; 72853e6be8SMichael Lotz }; 73853e6be8SMichael Lotz 74853e6be8SMichael Lotz 75853e6be8SMichael Lotz struct change_item { 76853e6be8SMichael Lotz bool added; 77853e6be8SMichael Lotz Device *device; 78853e6be8SMichael Lotz change_item *link; 79853e6be8SMichael Lotz }; 80853e6be8SMichael Lotz 81853e6be8SMichael Lotz 82853e6be8SMichael Lotz struct rescan_item { 83853e6be8SMichael Lotz const char *name; 84853e6be8SMichael Lotz rescan_item *link; 85853e6be8SMichael Lotz }; 86853e6be8SMichael Lotz 87853e6be8SMichael Lotz 88853e6be8SMichael Lotz typedef enum { 89853e6be8SMichael Lotz USB_SPEED_LOWSPEED = 0, 90853e6be8SMichael Lotz USB_SPEED_FULLSPEED, 91853e6be8SMichael Lotz USB_SPEED_HIGHSPEED, 926e2bbbb1SAugustin Cavalier USB_SPEED_SUPERSPEED, 936e2bbbb1SAugustin Cavalier USB_SPEED_MAX = USB_SPEED_SUPERSPEED 94853e6be8SMichael Lotz } usb_speed; 95853e6be8SMichael Lotz 96853e6be8SMichael Lotz 97853e6be8SMichael Lotz typedef enum { 98853e6be8SMichael Lotz USB_CHANGE_CREATED = 0, 99853e6be8SMichael Lotz USB_CHANGE_DESTROYED, 100853e6be8SMichael Lotz USB_CHANGE_PIPE_POLICY_CHANGED 101853e6be8SMichael Lotz } usb_change; 102853e6be8SMichael Lotz 103853e6be8SMichael Lotz 104853e6be8SMichael Lotz #define USB_OBJECT_NONE 0x00000000 105853e6be8SMichael Lotz #define USB_OBJECT_PIPE 0x00000001 106853e6be8SMichael Lotz #define USB_OBJECT_CONTROL_PIPE 0x00000002 107853e6be8SMichael Lotz #define USB_OBJECT_INTERRUPT_PIPE 0x00000004 108853e6be8SMichael Lotz #define USB_OBJECT_BULK_PIPE 0x00000008 109853e6be8SMichael Lotz #define USB_OBJECT_ISO_PIPE 0x00000010 110853e6be8SMichael Lotz #define USB_OBJECT_INTERFACE 0x00000020 111853e6be8SMichael Lotz #define USB_OBJECT_DEVICE 0x00000040 112853e6be8SMichael Lotz #define USB_OBJECT_HUB 0x00000080 113853e6be8SMichael Lotz 114853e6be8SMichael Lotz 115853e6be8SMichael Lotz class Stack { 116853e6be8SMichael Lotz public: 117853e6be8SMichael Lotz Stack(); 118853e6be8SMichael Lotz ~Stack(); 119853e6be8SMichael Lotz 120853e6be8SMichael Lotz status_t InitCheck(); 121853e6be8SMichael Lotz 122853e6be8SMichael Lotz bool Lock(); 123853e6be8SMichael Lotz void Unlock(); 124853e6be8SMichael Lotz 125853e6be8SMichael Lotz usb_id GetUSBID(Object *object); 1261e02632bSAugustin Cavalier void PutUSBID(Object *object); 127853e6be8SMichael Lotz Object * GetObject(usb_id id); 128853e6be8SMichael Lotz 129853e6be8SMichael Lotz // only for the kernel debugger 13038fc536eSMichael Lotz Object * GetObjectNoLock(usb_id id) const; 131853e6be8SMichael Lotz 132853e6be8SMichael Lotz void AddBusManager(BusManager *bus); 133853e6be8SMichael Lotz int32 IndexOfBusManager(BusManager *bus); 13438fc536eSMichael Lotz BusManager * BusManagerAt(int32 index) const; 135853e6be8SMichael Lotz 136853e6be8SMichael Lotz status_t AllocateChunk(void **logicalAddress, 137d8b4cfc9SRene Gollent phys_addr_t *physicalAddress, 13819b8f8a0SMichael Lotz size_t size); 139853e6be8SMichael Lotz status_t FreeChunk(void *logicalAddress, 140d8b4cfc9SRene Gollent phys_addr_t physicalAddress, 141d8b4cfc9SRene Gollent size_t size); 142853e6be8SMichael Lotz 143853e6be8SMichael Lotz area_id AllocateArea(void **logicalAddress, 144d8b4cfc9SRene Gollent phys_addr_t *physicalAddress, 145853e6be8SMichael Lotz size_t size, const char *name); 146853e6be8SMichael Lotz 147853e6be8SMichael Lotz void NotifyDeviceChange(Device *device, 148853e6be8SMichael Lotz rescan_item **rescanList, 149853e6be8SMichael Lotz bool added); 150853e6be8SMichael Lotz void RescanDrivers(rescan_item *rescanItem); 151853e6be8SMichael Lotz 152853e6be8SMichael Lotz // USB API 153853e6be8SMichael Lotz status_t RegisterDriver(const char *driverName, 15419b8f8a0SMichael Lotz const usb_support_descriptor * 15519b8f8a0SMichael Lotz descriptors, 156853e6be8SMichael Lotz size_t descriptorCount, 157853e6be8SMichael Lotz const char *republishDriverName); 158853e6be8SMichael Lotz 159853e6be8SMichael Lotz status_t InstallNotify(const char *driverName, 160853e6be8SMichael Lotz const usb_notify_hooks *hooks); 161853e6be8SMichael Lotz status_t UninstallNotify(const char *driverName); 162853e6be8SMichael Lotz 16319b8f8a0SMichael Lotz usb_id USBID() const { return 0; } 16419b8f8a0SMichael Lotz const char * TypeName() const { return "stack"; } 165853e6be8SMichael Lotz 166853e6be8SMichael Lotz private: 167853e6be8SMichael Lotz static int32 ExploreThread(void *data); 168853e6be8SMichael Lotz 169853e6be8SMichael Lotz Vector<BusManager *> fBusManagers; 170853e6be8SMichael Lotz thread_id fExploreThread; 171853e6be8SMichael Lotz bool fFirstExploreDone; 172853e6be8SMichael Lotz bool fStopThreads; 173853e6be8SMichael Lotz 174853e6be8SMichael Lotz mutex fStackLock; 175853e6be8SMichael Lotz mutex fExploreLock; 176853e6be8SMichael Lotz PhysicalMemoryAllocator * fAllocator; 177853e6be8SMichael Lotz 178853e6be8SMichael Lotz uint32 fObjectIndex; 179853e6be8SMichael Lotz uint32 fObjectMaxCount; 180853e6be8SMichael Lotz Object ** fObjectArray; 181853e6be8SMichael Lotz 182853e6be8SMichael Lotz usb_driver_info * fDriverList; 183853e6be8SMichael Lotz }; 184853e6be8SMichael Lotz 185853e6be8SMichael Lotz 186853e6be8SMichael Lotz /* 187853e6be8SMichael Lotz * This class manages a bus. It is created by the Stack object 188853e6be8SMichael Lotz * after a host controller gives positive feedback on whether the hardware 189853e6be8SMichael Lotz * is found. 190853e6be8SMichael Lotz */ 191853e6be8SMichael Lotz class BusManager { 192853e6be8SMichael Lotz public: 193853e6be8SMichael Lotz BusManager(Stack *stack); 194853e6be8SMichael Lotz virtual ~BusManager(); 195853e6be8SMichael Lotz 196853e6be8SMichael Lotz virtual status_t InitCheck(); 197853e6be8SMichael Lotz 198853e6be8SMichael Lotz bool Lock(); 199853e6be8SMichael Lotz void Unlock(); 200853e6be8SMichael Lotz 201853e6be8SMichael Lotz int8 AllocateAddress(); 202853e6be8SMichael Lotz void FreeAddress(int8 address); 203853e6be8SMichael Lotz 204319a3798SJérôme Duval virtual Device * AllocateDevice(Hub *parent, 205853e6be8SMichael Lotz int8 hubAddress, uint8 hubPort, 206853e6be8SMichael Lotz usb_speed speed); 207319a3798SJérôme Duval virtual void FreeDevice(Device *device); 208853e6be8SMichael Lotz 209853e6be8SMichael Lotz virtual status_t Start(); 210853e6be8SMichael Lotz virtual status_t Stop(); 211853e6be8SMichael Lotz 212159aa93bSMichael Lotz virtual status_t StartDebugTransfer(Transfer *transfer); 213159aa93bSMichael Lotz virtual status_t CheckDebugTransfer(Transfer *transfer); 214159aa93bSMichael Lotz virtual void CancelDebugTransfer(Transfer *transfer); 215159aa93bSMichael Lotz 216853e6be8SMichael Lotz virtual status_t SubmitTransfer(Transfer *transfer); 217853e6be8SMichael Lotz virtual status_t CancelQueuedTransfers(Pipe *pipe, 218853e6be8SMichael Lotz bool force); 219853e6be8SMichael Lotz 220853e6be8SMichael Lotz virtual status_t NotifyPipeChange(Pipe *pipe, 221853e6be8SMichael Lotz usb_change change); 222853e6be8SMichael Lotz 22319b8f8a0SMichael Lotz Object * RootObject() const 22419b8f8a0SMichael Lotz { return fRootObject; } 225853e6be8SMichael Lotz 22619b8f8a0SMichael Lotz Hub * GetRootHub() const { return fRootHub; } 22719b8f8a0SMichael Lotz void SetRootHub(Hub *hub) { fRootHub = hub; } 228853e6be8SMichael Lotz 22938fc536eSMichael Lotz virtual const char * TypeName() const = 0; 230853e6be8SMichael Lotz 231853e6be8SMichael Lotz protected: 232*decd5b8fSAugustin Cavalier usb_id USBID() const { return fStackIndex; } 233*decd5b8fSAugustin Cavalier 234*decd5b8fSAugustin Cavalier protected: 235853e6be8SMichael Lotz bool fInitOK; 236853e6be8SMichael Lotz 237853e6be8SMichael Lotz private: 238853e6be8SMichael Lotz ControlPipe * _GetDefaultPipe(usb_speed); 239853e6be8SMichael Lotz 240853e6be8SMichael Lotz mutex fLock; 241853e6be8SMichael Lotz 242853e6be8SMichael Lotz bool fDeviceMap[128]; 243853e6be8SMichael Lotz int8 fDeviceIndex; 244853e6be8SMichael Lotz 245853e6be8SMichael Lotz Stack * fStack; 246853e6be8SMichael Lotz ControlPipe * fDefaultPipes[USB_SPEED_MAX + 1]; 247853e6be8SMichael Lotz Hub * fRootHub; 248853e6be8SMichael Lotz Object * fRootObject; 24919b8f8a0SMichael Lotz 250*decd5b8fSAugustin Cavalier usb_id fStackIndex; 251853e6be8SMichael Lotz }; 252853e6be8SMichael Lotz 253853e6be8SMichael Lotz 254853e6be8SMichael Lotz class Object { 255853e6be8SMichael Lotz public: 256853e6be8SMichael Lotz Object(Stack *stack, BusManager *bus); 257853e6be8SMichael Lotz Object(Object *parent); 258853e6be8SMichael Lotz virtual ~Object(); 259853e6be8SMichael Lotz 26019b8f8a0SMichael Lotz Object * Parent() const { return fParent; } 261853e6be8SMichael Lotz 26219b8f8a0SMichael Lotz BusManager * GetBusManager() const 26319b8f8a0SMichael Lotz { return fBusManager; } 26419b8f8a0SMichael Lotz Stack * GetStack() const { return fStack; } 265853e6be8SMichael Lotz 26619b8f8a0SMichael Lotz usb_id USBID() const { return fUSBID; } 26719b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_NONE; } 26819b8f8a0SMichael Lotz virtual const char * TypeName() const { return "object"; } 269853e6be8SMichael Lotz 270853e6be8SMichael Lotz // Convenience functions for standard requests 271853e6be8SMichael Lotz virtual status_t SetFeature(uint16 selector); 272853e6be8SMichael Lotz virtual status_t ClearFeature(uint16 selector); 273853e6be8SMichael Lotz virtual status_t GetStatus(uint16 *status); 274853e6be8SMichael Lotz 2751e02632bSAugustin Cavalier protected: 2761e02632bSAugustin Cavalier void PutUSBID(); 2771e02632bSAugustin Cavalier 278853e6be8SMichael Lotz private: 279853e6be8SMichael Lotz Object * fParent; 280853e6be8SMichael Lotz BusManager * fBusManager; 281853e6be8SMichael Lotz Stack * fStack; 282853e6be8SMichael Lotz usb_id fUSBID; 283853e6be8SMichael Lotz }; 284853e6be8SMichael Lotz 285853e6be8SMichael Lotz 286853e6be8SMichael Lotz /* 287853e6be8SMichael Lotz * The Pipe class is the communication management between the hardware and 288853e6be8SMichael Lotz * the stack. It creates packets, manages these and performs callbacks. 289853e6be8SMichael Lotz */ 290853e6be8SMichael Lotz class Pipe : public Object { 291853e6be8SMichael Lotz public: 292853e6be8SMichael Lotz enum pipeDirection { In, Out, Default }; 293853e6be8SMichael Lotz 294853e6be8SMichael Lotz Pipe(Object *parent); 295853e6be8SMichael Lotz virtual ~Pipe(); 296853e6be8SMichael Lotz 297334c06aeSJérôme Duval virtual void InitCommon(int8 deviceAddress, 298853e6be8SMichael Lotz uint8 endpointAddress, 299853e6be8SMichael Lotz usb_speed speed, 300853e6be8SMichael Lotz pipeDirection direction, 301853e6be8SMichael Lotz size_t maxPacketSize, 302853e6be8SMichael Lotz uint8 interval, 303853e6be8SMichael Lotz int8 hubAddress, uint8 hubPort); 304086528f6SAugustin Cavalier virtual void InitSuperSpeed(uint8 maxBurst, 305086528f6SAugustin Cavalier uint16 bytesPerInterval); 306853e6be8SMichael Lotz 30719b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_PIPE; } 30819b8f8a0SMichael Lotz virtual const char * TypeName() const { return "pipe"; } 309853e6be8SMichael Lotz 31019b8f8a0SMichael Lotz int8 DeviceAddress() const 31119b8f8a0SMichael Lotz { return fDeviceAddress; } 31219b8f8a0SMichael Lotz usb_speed Speed() const { return fSpeed; } 31319b8f8a0SMichael Lotz pipeDirection Direction() const { return fDirection; } 31419b8f8a0SMichael Lotz uint8 EndpointAddress() const 31519b8f8a0SMichael Lotz { return fEndpointAddress; } 31619b8f8a0SMichael Lotz size_t MaxPacketSize() const 31719b8f8a0SMichael Lotz { return fMaxPacketSize; } 31819b8f8a0SMichael Lotz uint8 Interval() const { return fInterval; } 319853e6be8SMichael Lotz 320086528f6SAugustin Cavalier // SuperSpeed-only parameters 321086528f6SAugustin Cavalier uint8 MaxBurst() const 322086528f6SAugustin Cavalier { return fMaxBurst; } 323086528f6SAugustin Cavalier uint16 BytesPerInterval() const 324086528f6SAugustin Cavalier { return fBytesPerInterval; } 325086528f6SAugustin Cavalier 326853e6be8SMichael Lotz // Hub port being the one-based logical port number on the hub 327853e6be8SMichael Lotz void SetHubInfo(int8 address, uint8 port); 32819b8f8a0SMichael Lotz int8 HubAddress() const 32919b8f8a0SMichael Lotz { return fHubAddress; } 33019b8f8a0SMichael Lotz uint8 HubPort() const { return fHubPort; } 331853e6be8SMichael Lotz 33219b8f8a0SMichael Lotz virtual bool DataToggle() const 33319b8f8a0SMichael Lotz { return fDataToggle; } 33419b8f8a0SMichael Lotz virtual void SetDataToggle(bool toggle) 33519b8f8a0SMichael Lotz { fDataToggle = toggle; } 336853e6be8SMichael Lotz 337853e6be8SMichael Lotz status_t SubmitTransfer(Transfer *transfer); 338853e6be8SMichael Lotz status_t CancelQueuedTransfers(bool force); 339853e6be8SMichael Lotz 34019b8f8a0SMichael Lotz void SetControllerCookie(void *cookie) 34119b8f8a0SMichael Lotz { fControllerCookie = cookie; } 34219b8f8a0SMichael Lotz void * ControllerCookie() const 34319b8f8a0SMichael Lotz { return fControllerCookie; } 344853e6be8SMichael Lotz 345853e6be8SMichael Lotz // Convenience functions for standard requests 346853e6be8SMichael Lotz virtual status_t SetFeature(uint16 selector); 347853e6be8SMichael Lotz virtual status_t ClearFeature(uint16 selector); 348853e6be8SMichael Lotz virtual status_t GetStatus(uint16 *status); 349853e6be8SMichael Lotz 350853e6be8SMichael Lotz private: 351853e6be8SMichael Lotz int8 fDeviceAddress; 352853e6be8SMichael Lotz uint8 fEndpointAddress; 353853e6be8SMichael Lotz pipeDirection fDirection; 354853e6be8SMichael Lotz usb_speed fSpeed; 355853e6be8SMichael Lotz size_t fMaxPacketSize; 356853e6be8SMichael Lotz uint8 fInterval; 357086528f6SAugustin Cavalier uint8 fMaxBurst; 358086528f6SAugustin Cavalier uint16 fBytesPerInterval; 359853e6be8SMichael Lotz int8 fHubAddress; 360853e6be8SMichael Lotz uint8 fHubPort; 361853e6be8SMichael Lotz bool fDataToggle; 362853e6be8SMichael Lotz void * fControllerCookie; 363853e6be8SMichael Lotz }; 364853e6be8SMichael Lotz 365853e6be8SMichael Lotz 366853e6be8SMichael Lotz class ControlPipe : public Pipe { 367853e6be8SMichael Lotz public: 368853e6be8SMichael Lotz ControlPipe(Object *parent); 369853e6be8SMichael Lotz virtual ~ControlPipe(); 370853e6be8SMichael Lotz 371b62bb24fSMichael Lotz virtual void InitCommon(int8 deviceAddress, 372b62bb24fSMichael Lotz uint8 endpointAddress, 373b62bb24fSMichael Lotz usb_speed speed, 374b62bb24fSMichael Lotz pipeDirection direction, 375b62bb24fSMichael Lotz size_t maxPacketSize, 376b62bb24fSMichael Lotz uint8 interval, 377b62bb24fSMichael Lotz int8 hubAddress, uint8 hubPort); 378b62bb24fSMichael Lotz 37919b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_PIPE 38019b8f8a0SMichael Lotz | USB_OBJECT_CONTROL_PIPE; } 38119b8f8a0SMichael Lotz virtual const char * TypeName() const 38219b8f8a0SMichael Lotz { return "control pipe"; } 383853e6be8SMichael Lotz 384853e6be8SMichael Lotz // The data toggle is not relevant 385853e6be8SMichael Lotz // for control transfers, as they are 386853e6be8SMichael Lotz // always enclosed by a setup and 387853e6be8SMichael Lotz // status packet. The toggle always 388853e6be8SMichael Lotz // starts at 1. 38919b8f8a0SMichael Lotz virtual bool DataToggle() const { return true; } 39038fc536eSMichael Lotz virtual void SetDataToggle(bool toggle) {} 391853e6be8SMichael Lotz 392853e6be8SMichael Lotz status_t SendRequest(uint8 requestType, 393853e6be8SMichael Lotz uint8 request, uint16 value, 394853e6be8SMichael Lotz uint16 index, uint16 length, 395853e6be8SMichael Lotz void *data, size_t dataLength, 396853e6be8SMichael Lotz size_t *actualLength); 397853e6be8SMichael Lotz static void SendRequestCallback(void *cookie, 398853e6be8SMichael Lotz status_t status, void *data, 399853e6be8SMichael Lotz size_t actualLength); 400853e6be8SMichael Lotz 401853e6be8SMichael Lotz status_t QueueRequest(uint8 requestType, 402853e6be8SMichael Lotz uint8 request, uint16 value, 403853e6be8SMichael Lotz uint16 index, uint16 length, 404853e6be8SMichael Lotz void *data, size_t dataLength, 405853e6be8SMichael Lotz usb_callback_func callback, 406853e6be8SMichael Lotz void *callbackCookie); 407853e6be8SMichael Lotz 408853e6be8SMichael Lotz private: 409853e6be8SMichael Lotz mutex fSendRequestLock; 410853e6be8SMichael Lotz sem_id fNotifySem; 411853e6be8SMichael Lotz status_t fTransferStatus; 412853e6be8SMichael Lotz size_t fActualLength; 413853e6be8SMichael Lotz }; 414853e6be8SMichael Lotz 415853e6be8SMichael Lotz 416853e6be8SMichael Lotz class InterruptPipe : public Pipe { 417853e6be8SMichael Lotz public: 418853e6be8SMichael Lotz InterruptPipe(Object *parent); 419853e6be8SMichael Lotz 42019b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_PIPE 42119b8f8a0SMichael Lotz | USB_OBJECT_INTERRUPT_PIPE; } 42219b8f8a0SMichael Lotz virtual const char * TypeName() const 42319b8f8a0SMichael Lotz { return "interrupt pipe"; } 424853e6be8SMichael Lotz 425853e6be8SMichael Lotz status_t QueueInterrupt(void *data, 426853e6be8SMichael Lotz size_t dataLength, 427853e6be8SMichael Lotz usb_callback_func callback, 428853e6be8SMichael Lotz void *callbackCookie); 429853e6be8SMichael Lotz }; 430853e6be8SMichael Lotz 431853e6be8SMichael Lotz 432853e6be8SMichael Lotz class BulkPipe : public Pipe { 433853e6be8SMichael Lotz public: 434853e6be8SMichael Lotz BulkPipe(Object *parent); 435853e6be8SMichael Lotz 436334c06aeSJérôme Duval virtual void InitCommon(int8 deviceAddress, 437334c06aeSJérôme Duval uint8 endpointAddress, 438334c06aeSJérôme Duval usb_speed speed, 439334c06aeSJérôme Duval pipeDirection direction, 440334c06aeSJérôme Duval size_t maxPacketSize, 441334c06aeSJérôme Duval uint8 interval, 442334c06aeSJérôme Duval int8 hubAddress, uint8 hubPort); 443334c06aeSJérôme Duval 44419b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_PIPE 44519b8f8a0SMichael Lotz | USB_OBJECT_BULK_PIPE; } 44619b8f8a0SMichael Lotz virtual const char * TypeName() const { return "bulk pipe"; } 447853e6be8SMichael Lotz 448853e6be8SMichael Lotz status_t QueueBulk(void *data, 449853e6be8SMichael Lotz size_t dataLength, 450853e6be8SMichael Lotz usb_callback_func callback, 451853e6be8SMichael Lotz void *callbackCookie); 452853e6be8SMichael Lotz status_t QueueBulkV(iovec *vector, 453853e6be8SMichael Lotz size_t vectorCount, 454853e6be8SMichael Lotz usb_callback_func callback, 455853e6be8SMichael Lotz void *callbackCookie, 456853e6be8SMichael Lotz bool physical); 457853e6be8SMichael Lotz }; 458853e6be8SMichael Lotz 459853e6be8SMichael Lotz 460853e6be8SMichael Lotz class IsochronousPipe : public Pipe { 461853e6be8SMichael Lotz public: 462853e6be8SMichael Lotz IsochronousPipe(Object *parent); 463853e6be8SMichael Lotz 46419b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_PIPE 46519b8f8a0SMichael Lotz | USB_OBJECT_ISO_PIPE; } 46619b8f8a0SMichael Lotz virtual const char * TypeName() const { return "iso pipe"; } 467853e6be8SMichael Lotz 468853e6be8SMichael Lotz status_t QueueIsochronous(void *data, 469853e6be8SMichael Lotz size_t dataLength, 47019b8f8a0SMichael Lotz usb_iso_packet_descriptor * 47119b8f8a0SMichael Lotz packetDescriptor, 472853e6be8SMichael Lotz uint32 packetCount, 473853e6be8SMichael Lotz uint32 *startingFrameNumber, 474853e6be8SMichael Lotz uint32 flags, 475853e6be8SMichael Lotz usb_callback_func callback, 476853e6be8SMichael Lotz void *callbackCookie); 477853e6be8SMichael Lotz 478853e6be8SMichael Lotz status_t SetPipePolicy(uint8 maxQueuedPackets, 479853e6be8SMichael Lotz uint16 maxBufferDurationMS, 480853e6be8SMichael Lotz uint16 sampleSize); 481853e6be8SMichael Lotz status_t GetPipePolicy(uint8 *maxQueuedPackets, 482853e6be8SMichael Lotz uint16 *maxBufferDurationMS, 483853e6be8SMichael Lotz uint16 *sampleSize); 484853e6be8SMichael Lotz 485853e6be8SMichael Lotz private: 486853e6be8SMichael Lotz uint8 fMaxQueuedPackets; 487853e6be8SMichael Lotz uint16 fMaxBufferDuration; 488853e6be8SMichael Lotz uint16 fSampleSize; 489853e6be8SMichael Lotz }; 490853e6be8SMichael Lotz 491853e6be8SMichael Lotz 492853e6be8SMichael Lotz class Interface : public Object { 493853e6be8SMichael Lotz public: 494853e6be8SMichael Lotz Interface(Object *parent, 495853e6be8SMichael Lotz uint8 interfaceIndex); 496853e6be8SMichael Lotz 49719b8f8a0SMichael Lotz virtual uint32 Type() const 49819b8f8a0SMichael Lotz { return USB_OBJECT_INTERFACE; } 49919b8f8a0SMichael Lotz virtual const char * TypeName() const { return "interface"; } 500853e6be8SMichael Lotz 501853e6be8SMichael Lotz // Convenience functions for standard requests 502853e6be8SMichael Lotz virtual status_t SetFeature(uint16 selector); 503853e6be8SMichael Lotz virtual status_t ClearFeature(uint16 selector); 504853e6be8SMichael Lotz virtual status_t GetStatus(uint16 *status); 505853e6be8SMichael Lotz 506853e6be8SMichael Lotz private: 507853e6be8SMichael Lotz uint8 fInterfaceIndex; 508853e6be8SMichael Lotz }; 509853e6be8SMichael Lotz 510853e6be8SMichael Lotz 511853e6be8SMichael Lotz class Device : public Object { 512853e6be8SMichael Lotz public: 513853e6be8SMichael Lotz Device(Object *parent, int8 hubAddress, 514853e6be8SMichael Lotz uint8 hubPort, 515853e6be8SMichael Lotz usb_device_descriptor &desc, 516853e6be8SMichael Lotz int8 deviceAddress, 5172b31b4a8SJérôme Duval usb_speed speed, bool isRootHub, 5182b31b4a8SJérôme Duval void *controllerCookie = NULL); 519853e6be8SMichael Lotz virtual ~Device(); 520853e6be8SMichael Lotz 521853e6be8SMichael Lotz status_t InitCheck(); 522853e6be8SMichael Lotz 523853e6be8SMichael Lotz virtual status_t Changed(change_item **changeList, 524853e6be8SMichael Lotz bool added); 525853e6be8SMichael Lotz 52619b8f8a0SMichael Lotz virtual uint32 Type() const 52719b8f8a0SMichael Lotz { return USB_OBJECT_DEVICE; } 52819b8f8a0SMichael Lotz virtual const char * TypeName() const { return "device"; } 529853e6be8SMichael Lotz 53019b8f8a0SMichael Lotz ControlPipe * DefaultPipe() const 53119b8f8a0SMichael Lotz { return fDefaultPipe; } 532853e6be8SMichael Lotz 533853e6be8SMichael Lotz virtual status_t GetDescriptor(uint8 descriptorType, 534853e6be8SMichael Lotz uint8 index, uint16 languageID, 535853e6be8SMichael Lotz void *data, size_t dataLength, 536853e6be8SMichael Lotz size_t *actualLength); 537853e6be8SMichael Lotz 53819b8f8a0SMichael Lotz int8 DeviceAddress() const 53919b8f8a0SMichael Lotz { return fDeviceAddress; } 540853e6be8SMichael Lotz const usb_device_descriptor * DeviceDescriptor() const; 54119b8f8a0SMichael Lotz usb_speed Speed() const { return fSpeed; } 542853e6be8SMichael Lotz 543853e6be8SMichael Lotz const usb_configuration_info * Configuration() const; 544853e6be8SMichael Lotz const usb_configuration_info * ConfigurationAt(uint8 index) const; 54519b8f8a0SMichael Lotz status_t SetConfiguration( 54619b8f8a0SMichael Lotz const usb_configuration_info * 54719b8f8a0SMichael Lotz configuration); 548853e6be8SMichael Lotz status_t SetConfigurationAt(uint8 index); 549853e6be8SMichael Lotz status_t Unconfigure(bool atDeviceLevel); 550853e6be8SMichael Lotz 55119b8f8a0SMichael Lotz status_t SetAltInterface( 55219b8f8a0SMichael Lotz const usb_interface_info * 55319b8f8a0SMichael Lotz interface); 554853e6be8SMichael Lotz 555853e6be8SMichael Lotz void InitEndpoints(int32 interfaceIndex); 556853e6be8SMichael Lotz void ClearEndpoints(int32 interfaceIndex); 557853e6be8SMichael Lotz 558853e6be8SMichael Lotz virtual status_t ReportDevice( 55919b8f8a0SMichael Lotz usb_support_descriptor * 56019b8f8a0SMichael Lotz supportDescriptors, 561853e6be8SMichael Lotz uint32 supportDescriptorCount, 562853e6be8SMichael Lotz const usb_notify_hooks *hooks, 563853e6be8SMichael Lotz usb_driver_cookie **cookies, 564853e6be8SMichael Lotz bool added, bool recursive); 565853e6be8SMichael Lotz virtual status_t BuildDeviceName(char *string, 566853e6be8SMichael Lotz uint32 *index, size_t bufferSize, 567853e6be8SMichael Lotz Device *device); 568853e6be8SMichael Lotz 56919b8f8a0SMichael Lotz int8 HubAddress() const 57019b8f8a0SMichael Lotz { return fHubAddress; } 57119b8f8a0SMichael Lotz uint8 HubPort() const { return fHubPort; } 572853e6be8SMichael Lotz 5732b31b4a8SJérôme Duval void SetControllerCookie(void *cookie) 5742b31b4a8SJérôme Duval { fControllerCookie = cookie; } 5752b31b4a8SJérôme Duval void * ControllerCookie() const 5762b31b4a8SJérôme Duval { return fControllerCookie; } 5772b31b4a8SJérôme Duval 578853e6be8SMichael Lotz // Convenience functions for standard requests 579853e6be8SMichael Lotz virtual status_t SetFeature(uint16 selector); 580853e6be8SMichael Lotz virtual status_t ClearFeature(uint16 selector); 581853e6be8SMichael Lotz virtual status_t GetStatus(uint16 *status); 582853e6be8SMichael Lotz 583853e6be8SMichael Lotz protected: 584853e6be8SMichael Lotz usb_device_descriptor fDeviceDescriptor; 585853e6be8SMichael Lotz bool fInitOK; 586853e6be8SMichael Lotz 587853e6be8SMichael Lotz private: 588853e6be8SMichael Lotz bool fAvailable; 589853e6be8SMichael Lotz bool fIsRootHub; 590853e6be8SMichael Lotz usb_configuration_info * fConfigurations; 591853e6be8SMichael Lotz usb_configuration_info * fCurrentConfiguration; 592853e6be8SMichael Lotz usb_speed fSpeed; 593853e6be8SMichael Lotz int8 fDeviceAddress; 594853e6be8SMichael Lotz int8 fHubAddress; 595853e6be8SMichael Lotz uint8 fHubPort; 596853e6be8SMichael Lotz ControlPipe * fDefaultPipe; 5972b31b4a8SJérôme Duval void * fControllerCookie; 598853e6be8SMichael Lotz }; 599853e6be8SMichael Lotz 600853e6be8SMichael Lotz 601853e6be8SMichael Lotz class Hub : public Device { 602853e6be8SMichael Lotz public: 603853e6be8SMichael Lotz Hub(Object *parent, int8 hubAddress, 604853e6be8SMichael Lotz uint8 hubPort, 605853e6be8SMichael Lotz usb_device_descriptor &desc, 606853e6be8SMichael Lotz int8 deviceAddress, 60717aa359bSAkshay Jaggi usb_speed speed, bool isRootHub, 60817aa359bSAkshay Jaggi void *controllerCookie = NULL); 609853e6be8SMichael Lotz virtual ~Hub(); 610853e6be8SMichael Lotz 611853e6be8SMichael Lotz virtual status_t Changed(change_item **changeList, 612853e6be8SMichael Lotz bool added); 613853e6be8SMichael Lotz 61419b8f8a0SMichael Lotz virtual uint32 Type() const { return USB_OBJECT_DEVICE 61519b8f8a0SMichael Lotz | USB_OBJECT_HUB; } 61619b8f8a0SMichael Lotz virtual const char * TypeName() const { return "hub"; } 617853e6be8SMichael Lotz 618853e6be8SMichael Lotz virtual status_t GetDescriptor(uint8 descriptorType, 619853e6be8SMichael Lotz uint8 index, uint16 languageID, 620853e6be8SMichael Lotz void *data, size_t dataLength, 621853e6be8SMichael Lotz size_t *actualLength); 622853e6be8SMichael Lotz 62319b8f8a0SMichael Lotz Device * ChildAt(uint8 index) const 62419b8f8a0SMichael Lotz { return fChildren[index]; } 625853e6be8SMichael Lotz 626853e6be8SMichael Lotz status_t UpdatePortStatus(uint8 index); 627853e6be8SMichael Lotz status_t ResetPort(uint8 index); 628853e6be8SMichael Lotz status_t DisablePort(uint8 index); 629853e6be8SMichael Lotz 630853e6be8SMichael Lotz void Explore(change_item **changeList); 631853e6be8SMichael Lotz static void InterruptCallback(void *cookie, 632853e6be8SMichael Lotz status_t status, void *data, 633853e6be8SMichael Lotz size_t actualLength); 634853e6be8SMichael Lotz 635853e6be8SMichael Lotz virtual status_t ReportDevice( 63619b8f8a0SMichael Lotz usb_support_descriptor * 63719b8f8a0SMichael Lotz supportDescriptors, 638853e6be8SMichael Lotz uint32 supportDescriptorCount, 639853e6be8SMichael Lotz const usb_notify_hooks *hooks, 640853e6be8SMichael Lotz usb_driver_cookie **cookies, 641853e6be8SMichael Lotz bool added, bool recursive); 642853e6be8SMichael Lotz virtual status_t BuildDeviceName(char *string, 643853e6be8SMichael Lotz uint32 *index, size_t bufferSize, 644853e6be8SMichael Lotz Device *device); 645853e6be8SMichael Lotz 646853e6be8SMichael Lotz private: 647827c7224SMichael Lotz status_t _DebouncePort(uint8 index); 648827c7224SMichael Lotz 649853e6be8SMichael Lotz InterruptPipe * fInterruptPipe; 650853e6be8SMichael Lotz usb_hub_descriptor fHubDescriptor; 651853e6be8SMichael Lotz 652853e6be8SMichael Lotz usb_port_status fInterruptStatus[USB_MAX_PORT_COUNT]; 653853e6be8SMichael Lotz usb_port_status fPortStatus[USB_MAX_PORT_COUNT]; 654853e6be8SMichael Lotz Device * fChildren[USB_MAX_PORT_COUNT]; 655853e6be8SMichael Lotz }; 656853e6be8SMichael Lotz 657853e6be8SMichael Lotz 658853e6be8SMichael Lotz /* 659853e6be8SMichael Lotz * A Transfer is allocated on the heap and passed to the Host Controller in 660853e6be8SMichael Lotz * SubmitTransfer(). It is generated for all queued transfers. If queuing 661853e6be8SMichael Lotz * succeds (SubmitTransfer() returns with >= B_OK) the Host Controller takes 662853e6be8SMichael Lotz * ownership of the Transfer and will delete it as soon as it has called the 663853e6be8SMichael Lotz * set callback function. If SubmitTransfer() failes, the calling function is 664853e6be8SMichael Lotz * responsible for deleting the Transfer. 665853e6be8SMichael Lotz * Also, the transfer takes ownership of the usb_request_data passed to it in 666853e6be8SMichael Lotz * SetRequestData(), but does not take ownership of the data buffer set by 667853e6be8SMichael Lotz * SetData(). 668853e6be8SMichael Lotz */ 669853e6be8SMichael Lotz class Transfer { 670853e6be8SMichael Lotz public: 671853e6be8SMichael Lotz Transfer(Pipe *pipe); 672853e6be8SMichael Lotz ~Transfer(); 673853e6be8SMichael Lotz 67419b8f8a0SMichael Lotz Pipe * TransferPipe() const { return fPipe; } 675853e6be8SMichael Lotz 676853e6be8SMichael Lotz void SetRequestData(usb_request_data *data); 67719b8f8a0SMichael Lotz usb_request_data * RequestData() const { return fRequestData; } 678853e6be8SMichael Lotz 67919b8f8a0SMichael Lotz void SetIsochronousData( 68019b8f8a0SMichael Lotz usb_isochronous_data *data); 68119b8f8a0SMichael Lotz usb_isochronous_data * IsochronousData() const 68219b8f8a0SMichael Lotz { return fIsochronousData; } 683853e6be8SMichael Lotz 684853e6be8SMichael Lotz void SetData(uint8 *buffer, size_t length); 68519b8f8a0SMichael Lotz uint8 * Data() const 68619b8f8a0SMichael Lotz { return (uint8 *)fData.iov_base; } 68719b8f8a0SMichael Lotz size_t DataLength() const { return fData.iov_len; } 688853e6be8SMichael Lotz 689853e6be8SMichael Lotz void SetPhysical(bool physical); 69019b8f8a0SMichael Lotz bool IsPhysical() const { return fPhysical; } 691853e6be8SMichael Lotz 69219b8f8a0SMichael Lotz void SetVector(iovec *vector, 69319b8f8a0SMichael Lotz size_t vectorCount); 69419b8f8a0SMichael Lotz iovec * Vector() { return fVector; } 69519b8f8a0SMichael Lotz size_t VectorCount() const { return fVectorCount; } 696853e6be8SMichael Lotz size_t VectorLength(); 697853e6be8SMichael Lotz 69819b8f8a0SMichael Lotz uint16 Bandwidth() const { return fBandwidth; } 699853e6be8SMichael Lotz 70019b8f8a0SMichael Lotz bool IsFragmented() const { return fFragmented; } 701853e6be8SMichael Lotz void AdvanceByFragment(size_t actualLength); 702853e6be8SMichael Lotz 703853e6be8SMichael Lotz status_t InitKernelAccess(); 704853e6be8SMichael Lotz status_t PrepareKernelAccess(); 705853e6be8SMichael Lotz 706853e6be8SMichael Lotz void SetCallback(usb_callback_func callback, 707853e6be8SMichael Lotz void *cookie); 708159aa93bSMichael Lotz usb_callback_func Callback() const 709159aa93bSMichael Lotz { return fCallback; } 710159aa93bSMichael Lotz void * CallbackCookie() const 711159aa93bSMichael Lotz { return fCallbackCookie; } 712853e6be8SMichael Lotz 71319b8f8a0SMichael Lotz void Finished(uint32 status, 71419b8f8a0SMichael Lotz size_t actualLength); 715853e6be8SMichael Lotz 71619b8f8a0SMichael Lotz usb_id USBID() const { return 0; } 71719b8f8a0SMichael Lotz const char * TypeName() const { return "transfer"; } 718853e6be8SMichael Lotz 719853e6be8SMichael Lotz private: 720853e6be8SMichael Lotz status_t _CalculateBandwidth(); 721853e6be8SMichael Lotz 722853e6be8SMichael Lotz // Data that is related to the transfer 723853e6be8SMichael Lotz Pipe * fPipe; 724853e6be8SMichael Lotz iovec fData; 725853e6be8SMichael Lotz iovec * fVector; 726853e6be8SMichael Lotz size_t fVectorCount; 727853e6be8SMichael Lotz void * fBaseAddress; 728853e6be8SMichael Lotz bool fPhysical; 729853e6be8SMichael Lotz bool fFragmented; 730853e6be8SMichael Lotz size_t fActualLength; 731853e6be8SMichael Lotz area_id fUserArea; 732853e6be8SMichael Lotz area_id fClonedArea; 733853e6be8SMichael Lotz 734853e6be8SMichael Lotz usb_callback_func fCallback; 735853e6be8SMichael Lotz void * fCallbackCookie; 736853e6be8SMichael Lotz 737853e6be8SMichael Lotz // For control transfers 738853e6be8SMichael Lotz usb_request_data * fRequestData; 739853e6be8SMichael Lotz 740853e6be8SMichael Lotz // For isochronous transfers 741853e6be8SMichael Lotz usb_isochronous_data * fIsochronousData; 742853e6be8SMichael Lotz 743853e6be8SMichael Lotz // For bandwidth management. 744853e6be8SMichael Lotz // It contains the bandwidth necessary in microseconds 745853e6be8SMichael Lotz // for either isochronous, interrupt or control transfers. 746853e6be8SMichael Lotz // Not used for bulk transactions. 747853e6be8SMichael Lotz uint16 fBandwidth; 748853e6be8SMichael Lotz }; 749853e6be8SMichael Lotz 750cc9f959dSMichael Lotz #endif // _USB_PRIVATE_H 751