xref: /haiku/src/add-ons/kernel/bus_managers/usb/usb_private.h (revision d6e4f54f2de4c76fbfbe85fc348a8fde8c296dc6)
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 
12853e6be8SMichael Lotz #include "BeOSCompatibility.h"
13cc9f959dSMichael Lotz #include "usbspec_private.h"
14853e6be8SMichael Lotz #include <lock.h>
150e76cf0bSJérôme Duval #include <util/Vector.h>
16853e6be8SMichael Lotz 
17853e6be8SMichael Lotz 
18853e6be8SMichael Lotz #define TRACE_OUTPUT(x, y, z...) \
19853e6be8SMichael Lotz 	{ \
20853e6be8SMichael Lotz 		dprintf("usb %s%s %ld: ", y, (x)->TypeName(), (x)->USBID()); \
21853e6be8SMichael Lotz 		dprintf(z); \
22853e6be8SMichael Lotz 	}
23853e6be8SMichael Lotz 
24853e6be8SMichael Lotz //#define TRACE_USB
25853e6be8SMichael Lotz #ifdef TRACE_USB
26853e6be8SMichael Lotz #define TRACE(x...)					TRACE_OUTPUT(this, "", x)
27853e6be8SMichael Lotz #define TRACE_STATIC(x, y...)		TRACE_OUTPUT(x, "", y)
28853e6be8SMichael Lotz #define TRACE_MODULE(x...)			dprintf("usb "USB_MODULE_NAME": "x)
29853e6be8SMichael Lotz #else
30853e6be8SMichael Lotz #define TRACE(x...)					/* nothing */
31853e6be8SMichael Lotz #define TRACE_STATIC(x, y...)		/* nothing */
32853e6be8SMichael Lotz #define TRACE_MODULE(x...)			/* nothing */
33853e6be8SMichael Lotz #endif
34853e6be8SMichael Lotz 
35853e6be8SMichael Lotz #define TRACE_ALWAYS(x...)			TRACE_OUTPUT(this, "", x)
36853e6be8SMichael Lotz #define TRACE_ERROR(x...)			TRACE_OUTPUT(this, "error ", x)
37853e6be8SMichael Lotz #define TRACE_MODULE_ALWAYS(x...)	dprintf("usb "USB_MODULE_NAME": "x)
38853e6be8SMichael Lotz #define TRACE_MODULE_ERROR(x...)	dprintf("usb "USB_MODULE_NAME": "x)
39853e6be8SMichael Lotz 
40853e6be8SMichael Lotz class Hub;
41853e6be8SMichael Lotz class Stack;
42853e6be8SMichael Lotz class Device;
43853e6be8SMichael Lotz class Transfer;
44853e6be8SMichael Lotz class BusManager;
45853e6be8SMichael Lotz class Pipe;
46853e6be8SMichael Lotz class ControlPipe;
47853e6be8SMichael Lotz class Object;
48853e6be8SMichael Lotz class PhysicalMemoryAllocator;
49853e6be8SMichael Lotz 
50853e6be8SMichael Lotz 
51853e6be8SMichael Lotz struct usb_host_controller_info {
52853e6be8SMichael Lotz 	module_info info;
53853e6be8SMichael Lotz 	status_t (*control)(uint32 op, void *data, size_t length);
54853e6be8SMichael Lotz 	status_t (*add_to)(Stack *stack);
55853e6be8SMichael Lotz };
56853e6be8SMichael Lotz 
57853e6be8SMichael Lotz 
58853e6be8SMichael Lotz struct usb_driver_cookie {
59853e6be8SMichael Lotz 	usb_id device;
60853e6be8SMichael Lotz 	void *cookie;
61853e6be8SMichael Lotz 	usb_driver_cookie *link;
62853e6be8SMichael Lotz };
63853e6be8SMichael Lotz 
64853e6be8SMichael Lotz 
65853e6be8SMichael Lotz struct usb_driver_info {
66853e6be8SMichael Lotz 	const char *driver_name;
67853e6be8SMichael Lotz 	usb_support_descriptor *support_descriptors;
68853e6be8SMichael Lotz 	uint32 support_descriptor_count;
69853e6be8SMichael Lotz 	const char *republish_driver_name;
70853e6be8SMichael Lotz 	usb_notify_hooks notify_hooks;
71853e6be8SMichael Lotz 	usb_driver_cookie *cookies;
72853e6be8SMichael Lotz 	usb_driver_info *link;
73853e6be8SMichael Lotz };
74853e6be8SMichael Lotz 
75853e6be8SMichael Lotz 
76853e6be8SMichael Lotz struct change_item {
77853e6be8SMichael Lotz 	bool added;
78853e6be8SMichael Lotz 	Device *device;
79853e6be8SMichael Lotz 	change_item *link;
80853e6be8SMichael Lotz };
81853e6be8SMichael Lotz 
82853e6be8SMichael Lotz 
83853e6be8SMichael Lotz struct rescan_item {
84853e6be8SMichael Lotz 	const char *name;
85853e6be8SMichael Lotz 	rescan_item *link;
86853e6be8SMichael Lotz };
87853e6be8SMichael Lotz 
88853e6be8SMichael Lotz 
89853e6be8SMichael Lotz typedef enum {
90853e6be8SMichael Lotz 	USB_SPEED_LOWSPEED = 0,
91853e6be8SMichael Lotz 	USB_SPEED_FULLSPEED,
92853e6be8SMichael Lotz 	USB_SPEED_HIGHSPEED,
93*d6e4f54fSJérôme Duval 	USB_SPEED_SUPER,
94*d6e4f54fSJérôme Duval 	USB_SPEED_MAX = USB_SPEED_SUPER
95853e6be8SMichael Lotz } usb_speed;
96853e6be8SMichael Lotz 
97853e6be8SMichael Lotz 
98853e6be8SMichael Lotz typedef enum {
99853e6be8SMichael Lotz 	USB_CHANGE_CREATED = 0,
100853e6be8SMichael Lotz 	USB_CHANGE_DESTROYED,
101853e6be8SMichael Lotz 	USB_CHANGE_PIPE_POLICY_CHANGED
102853e6be8SMichael Lotz } usb_change;
103853e6be8SMichael Lotz 
104853e6be8SMichael Lotz 
105853e6be8SMichael Lotz #define USB_OBJECT_NONE					0x00000000
106853e6be8SMichael Lotz #define USB_OBJECT_PIPE					0x00000001
107853e6be8SMichael Lotz #define USB_OBJECT_CONTROL_PIPE			0x00000002
108853e6be8SMichael Lotz #define USB_OBJECT_INTERRUPT_PIPE		0x00000004
109853e6be8SMichael Lotz #define USB_OBJECT_BULK_PIPE			0x00000008
110853e6be8SMichael Lotz #define USB_OBJECT_ISO_PIPE				0x00000010
111853e6be8SMichael Lotz #define USB_OBJECT_INTERFACE			0x00000020
112853e6be8SMichael Lotz #define USB_OBJECT_DEVICE				0x00000040
113853e6be8SMichael Lotz #define USB_OBJECT_HUB					0x00000080
114853e6be8SMichael Lotz 
115853e6be8SMichael Lotz 
116853e6be8SMichael Lotz class Stack {
117853e6be8SMichael Lotz public:
118853e6be8SMichael Lotz 										Stack();
119853e6be8SMichael Lotz 										~Stack();
120853e6be8SMichael Lotz 
121853e6be8SMichael Lotz 		status_t						InitCheck();
122853e6be8SMichael Lotz 
123853e6be8SMichael Lotz 		bool							Lock();
124853e6be8SMichael Lotz 		void							Unlock();
125853e6be8SMichael Lotz 
126853e6be8SMichael Lotz 		usb_id							GetUSBID(Object *object);
127853e6be8SMichael Lotz 		void							PutUSBID(usb_id id);
128853e6be8SMichael Lotz 		Object *						GetObject(usb_id id);
129853e6be8SMichael Lotz 
130853e6be8SMichael Lotz 		// only for the kernel debugger
13138fc536eSMichael Lotz 		Object *						GetObjectNoLock(usb_id id) const;
132853e6be8SMichael Lotz 
133853e6be8SMichael Lotz 		void							AddBusManager(BusManager *bus);
134853e6be8SMichael Lotz 		int32							IndexOfBusManager(BusManager *bus);
13538fc536eSMichael Lotz 		BusManager *					BusManagerAt(int32 index) const;
136853e6be8SMichael Lotz 
137853e6be8SMichael Lotz 		status_t						AllocateChunk(void **logicalAddress,
13819b8f8a0SMichael Lotz 											void **physicalAddress,
13919b8f8a0SMichael Lotz 											size_t size);
140853e6be8SMichael Lotz 		status_t						FreeChunk(void *logicalAddress,
141853e6be8SMichael Lotz 											void *physicalAddress, size_t size);
142853e6be8SMichael Lotz 
143853e6be8SMichael Lotz 		area_id							AllocateArea(void **logicalAddress,
144853e6be8SMichael Lotz 											void **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 
204853e6be8SMichael Lotz 		Device *						AllocateDevice(Hub *parent,
205853e6be8SMichael Lotz 											int8 hubAddress, uint8 hubPort,
206853e6be8SMichael Lotz 											usb_speed speed);
207853e6be8SMichael Lotz 		void							FreeDevice(Device *device);
208853e6be8SMichael Lotz 
209853e6be8SMichael Lotz virtual	status_t						Start();
210853e6be8SMichael Lotz virtual	status_t						Stop();
211853e6be8SMichael Lotz 
212853e6be8SMichael Lotz virtual	status_t						SubmitTransfer(Transfer *transfer);
213853e6be8SMichael Lotz virtual	status_t						CancelQueuedTransfers(Pipe *pipe,
214853e6be8SMichael Lotz 											bool force);
215853e6be8SMichael Lotz 
216853e6be8SMichael Lotz virtual	status_t						NotifyPipeChange(Pipe *pipe,
217853e6be8SMichael Lotz 											usb_change change);
218853e6be8SMichael Lotz 
21919b8f8a0SMichael Lotz 		Object *						RootObject() const
22019b8f8a0SMichael Lotz 											{ return fRootObject; }
221853e6be8SMichael Lotz 
22219b8f8a0SMichael Lotz 		Hub *							GetRootHub() const { return fRootHub; }
22319b8f8a0SMichael Lotz 		void							SetRootHub(Hub *hub) { fRootHub = hub; }
224853e6be8SMichael Lotz 
22519b8f8a0SMichael Lotz 		usb_id							USBID() const { return fUSBID; }
22638fc536eSMichael Lotz virtual	const char *					TypeName() const = 0;
227853e6be8SMichael Lotz 
228853e6be8SMichael Lotz protected:
229853e6be8SMichael Lotz 		bool							fInitOK;
230853e6be8SMichael Lotz 
231853e6be8SMichael Lotz private:
232853e6be8SMichael Lotz 		ControlPipe *					_GetDefaultPipe(usb_speed);
233853e6be8SMichael Lotz 
234853e6be8SMichael Lotz 		mutex							fLock;
235853e6be8SMichael Lotz 
236853e6be8SMichael Lotz 		bool							fDeviceMap[128];
237853e6be8SMichael Lotz 		int8							fDeviceIndex;
238853e6be8SMichael Lotz 
239853e6be8SMichael Lotz 		Stack *							fStack;
240853e6be8SMichael Lotz 		ControlPipe *					fDefaultPipes[USB_SPEED_MAX + 1];
241853e6be8SMichael Lotz 		Hub *							fRootHub;
242853e6be8SMichael Lotz 		Object *						fRootObject;
24319b8f8a0SMichael Lotz 
24419b8f8a0SMichael Lotz 		usb_id							fUSBID;
245853e6be8SMichael Lotz };
246853e6be8SMichael Lotz 
247853e6be8SMichael Lotz 
248853e6be8SMichael Lotz class Object {
249853e6be8SMichael Lotz public:
250853e6be8SMichael Lotz 										Object(Stack *stack, BusManager *bus);
251853e6be8SMichael Lotz 										Object(Object *parent);
252853e6be8SMichael Lotz virtual									~Object();
253853e6be8SMichael Lotz 
25419b8f8a0SMichael Lotz 		Object *						Parent() const { return fParent; }
255853e6be8SMichael Lotz 
25619b8f8a0SMichael Lotz 		BusManager *					GetBusManager() const
25719b8f8a0SMichael Lotz 											{ return fBusManager; }
25819b8f8a0SMichael Lotz 		Stack *							GetStack() const { return fStack; }
259853e6be8SMichael Lotz 
26019b8f8a0SMichael Lotz 		usb_id							USBID() const { return fUSBID; }
26119b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_NONE; }
26219b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "object"; }
263853e6be8SMichael Lotz 
264853e6be8SMichael Lotz 		// Convenience functions for standard requests
265853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
266853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
267853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
268853e6be8SMichael Lotz 
269853e6be8SMichael Lotz private:
270853e6be8SMichael Lotz 		Object *						fParent;
271853e6be8SMichael Lotz 		BusManager *					fBusManager;
272853e6be8SMichael Lotz 		Stack *							fStack;
273853e6be8SMichael Lotz 		usb_id							fUSBID;
274853e6be8SMichael Lotz };
275853e6be8SMichael Lotz 
276853e6be8SMichael Lotz 
277853e6be8SMichael Lotz /*
278853e6be8SMichael Lotz  * The Pipe class is the communication management between the hardware and
279853e6be8SMichael Lotz  * the stack. It creates packets, manages these and performs callbacks.
280853e6be8SMichael Lotz  */
281853e6be8SMichael Lotz class Pipe : public Object {
282853e6be8SMichael Lotz public:
283853e6be8SMichael Lotz 		enum pipeDirection { In, Out, Default };
284853e6be8SMichael Lotz 
285853e6be8SMichael Lotz 										Pipe(Object *parent);
286853e6be8SMichael Lotz virtual									~Pipe();
287853e6be8SMichael Lotz 
288853e6be8SMichael Lotz 		void							InitCommon(int8 deviceAddress,
289853e6be8SMichael Lotz 											uint8 endpointAddress,
290853e6be8SMichael Lotz 											usb_speed speed,
291853e6be8SMichael Lotz 											pipeDirection direction,
292853e6be8SMichael Lotz 											size_t maxPacketSize,
293853e6be8SMichael Lotz 											uint8 interval,
294853e6be8SMichael Lotz 											int8 hubAddress, uint8 hubPort);
295853e6be8SMichael Lotz 
29619b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE; }
29719b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "pipe"; }
298853e6be8SMichael Lotz 
29919b8f8a0SMichael Lotz 		int8							DeviceAddress() const
30019b8f8a0SMichael Lotz 											{ return fDeviceAddress; }
30119b8f8a0SMichael Lotz 		usb_speed						Speed() const { return fSpeed; }
30219b8f8a0SMichael Lotz 		pipeDirection					Direction() const { return fDirection; }
30319b8f8a0SMichael Lotz 		uint8							EndpointAddress() const
30419b8f8a0SMichael Lotz 											{ return fEndpointAddress; }
30519b8f8a0SMichael Lotz 		size_t							MaxPacketSize() const
30619b8f8a0SMichael Lotz 											{ return fMaxPacketSize; }
30719b8f8a0SMichael Lotz 		uint8							Interval() const { return fInterval; }
308853e6be8SMichael Lotz 
309853e6be8SMichael Lotz 		// Hub port being the one-based logical port number on the hub
310853e6be8SMichael Lotz 		void							SetHubInfo(int8 address, uint8 port);
31119b8f8a0SMichael Lotz 		int8							HubAddress() const
31219b8f8a0SMichael Lotz 											{ return fHubAddress; }
31319b8f8a0SMichael Lotz 		uint8							HubPort() const { return fHubPort; }
314853e6be8SMichael Lotz 
31519b8f8a0SMichael Lotz virtual	bool							DataToggle() const
31619b8f8a0SMichael Lotz 											{ return fDataToggle; }
31719b8f8a0SMichael Lotz virtual	void							SetDataToggle(bool toggle)
31819b8f8a0SMichael Lotz 											{ fDataToggle = toggle; }
319853e6be8SMichael Lotz 
320853e6be8SMichael Lotz 		status_t						SubmitTransfer(Transfer *transfer);
321853e6be8SMichael Lotz 		status_t						CancelQueuedTransfers(bool force);
322853e6be8SMichael Lotz 
32319b8f8a0SMichael Lotz 		void							SetControllerCookie(void *cookie)
32419b8f8a0SMichael Lotz 											{ fControllerCookie = cookie; }
32519b8f8a0SMichael Lotz 		void *							ControllerCookie() const
32619b8f8a0SMichael Lotz 											{ return fControllerCookie; }
327853e6be8SMichael Lotz 
328853e6be8SMichael Lotz 		// Convenience functions for standard requests
329853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
330853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
331853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
332853e6be8SMichael Lotz 
333853e6be8SMichael Lotz private:
334853e6be8SMichael Lotz 		int8							fDeviceAddress;
335853e6be8SMichael Lotz 		uint8							fEndpointAddress;
336853e6be8SMichael Lotz 		pipeDirection					fDirection;
337853e6be8SMichael Lotz 		usb_speed						fSpeed;
338853e6be8SMichael Lotz 		size_t							fMaxPacketSize;
339853e6be8SMichael Lotz 		uint8							fInterval;
340853e6be8SMichael Lotz 		int8							fHubAddress;
341853e6be8SMichael Lotz 		uint8							fHubPort;
342853e6be8SMichael Lotz 		bool							fDataToggle;
343853e6be8SMichael Lotz 		void *							fControllerCookie;
344853e6be8SMichael Lotz };
345853e6be8SMichael Lotz 
346853e6be8SMichael Lotz 
347853e6be8SMichael Lotz class ControlPipe : public Pipe {
348853e6be8SMichael Lotz public:
349853e6be8SMichael Lotz 										ControlPipe(Object *parent);
350853e6be8SMichael Lotz virtual									~ControlPipe();
351853e6be8SMichael Lotz 
35219b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
35319b8f8a0SMichael Lotz 											| USB_OBJECT_CONTROL_PIPE; }
35419b8f8a0SMichael Lotz virtual	const char *					TypeName() const
35519b8f8a0SMichael Lotz 											{ return "control pipe"; }
356853e6be8SMichael Lotz 
357853e6be8SMichael Lotz 										// The data toggle is not relevant
358853e6be8SMichael Lotz 										// for control transfers, as they are
359853e6be8SMichael Lotz 										// always enclosed by a setup and
360853e6be8SMichael Lotz 										// status packet. The toggle always
361853e6be8SMichael Lotz 										// starts at 1.
36219b8f8a0SMichael Lotz virtual	bool							DataToggle() const { return true; }
36338fc536eSMichael Lotz virtual	void							SetDataToggle(bool toggle) {}
364853e6be8SMichael Lotz 
365853e6be8SMichael Lotz 		status_t						SendRequest(uint8 requestType,
366853e6be8SMichael Lotz 											uint8 request, uint16 value,
367853e6be8SMichael Lotz 											uint16 index, uint16 length,
368853e6be8SMichael Lotz 											void *data, size_t dataLength,
369853e6be8SMichael Lotz 											size_t *actualLength);
370853e6be8SMichael Lotz static	void							SendRequestCallback(void *cookie,
371853e6be8SMichael Lotz 											status_t status, void *data,
372853e6be8SMichael Lotz 											size_t actualLength);
373853e6be8SMichael Lotz 
374853e6be8SMichael Lotz 		status_t						QueueRequest(uint8 requestType,
375853e6be8SMichael Lotz 											uint8 request, uint16 value,
376853e6be8SMichael Lotz 											uint16 index, uint16 length,
377853e6be8SMichael Lotz 											void *data, size_t dataLength,
378853e6be8SMichael Lotz 											usb_callback_func callback,
379853e6be8SMichael Lotz 											void *callbackCookie);
380853e6be8SMichael Lotz 
381853e6be8SMichael Lotz private:
382853e6be8SMichael Lotz 		mutex							fSendRequestLock;
383853e6be8SMichael Lotz 		sem_id							fNotifySem;
384853e6be8SMichael Lotz 		status_t						fTransferStatus;
385853e6be8SMichael Lotz 		size_t							fActualLength;
386853e6be8SMichael Lotz };
387853e6be8SMichael Lotz 
388853e6be8SMichael Lotz 
389853e6be8SMichael Lotz class InterruptPipe : public Pipe {
390853e6be8SMichael Lotz public:
391853e6be8SMichael Lotz 										InterruptPipe(Object *parent);
392853e6be8SMichael Lotz 
39319b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
39419b8f8a0SMichael Lotz 											| USB_OBJECT_INTERRUPT_PIPE; }
39519b8f8a0SMichael Lotz virtual	const char *					TypeName() const
39619b8f8a0SMichael Lotz 											{ return "interrupt pipe"; }
397853e6be8SMichael Lotz 
398853e6be8SMichael Lotz 		status_t						QueueInterrupt(void *data,
399853e6be8SMichael Lotz 											size_t dataLength,
400853e6be8SMichael Lotz 											usb_callback_func callback,
401853e6be8SMichael Lotz 											void *callbackCookie);
402853e6be8SMichael Lotz };
403853e6be8SMichael Lotz 
404853e6be8SMichael Lotz 
405853e6be8SMichael Lotz class BulkPipe : public Pipe {
406853e6be8SMichael Lotz public:
407853e6be8SMichael Lotz 										BulkPipe(Object *parent);
408853e6be8SMichael Lotz 
40919b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
41019b8f8a0SMichael Lotz 											| USB_OBJECT_BULK_PIPE; }
41119b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "bulk pipe"; }
412853e6be8SMichael Lotz 
413853e6be8SMichael Lotz 		status_t						QueueBulk(void *data,
414853e6be8SMichael Lotz 											size_t dataLength,
415853e6be8SMichael Lotz 											usb_callback_func callback,
416853e6be8SMichael Lotz 											void *callbackCookie);
417853e6be8SMichael Lotz 		status_t						QueueBulkV(iovec *vector,
418853e6be8SMichael Lotz 											size_t vectorCount,
419853e6be8SMichael Lotz 											usb_callback_func callback,
420853e6be8SMichael Lotz 											void *callbackCookie,
421853e6be8SMichael Lotz 											bool physical);
422853e6be8SMichael Lotz };
423853e6be8SMichael Lotz 
424853e6be8SMichael Lotz 
425853e6be8SMichael Lotz class IsochronousPipe : public Pipe {
426853e6be8SMichael Lotz public:
427853e6be8SMichael Lotz 										IsochronousPipe(Object *parent);
428853e6be8SMichael Lotz 
42919b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
43019b8f8a0SMichael Lotz 											| USB_OBJECT_ISO_PIPE; }
43119b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "iso pipe"; }
432853e6be8SMichael Lotz 
433853e6be8SMichael Lotz 		status_t						QueueIsochronous(void *data,
434853e6be8SMichael Lotz 											size_t dataLength,
43519b8f8a0SMichael Lotz 											usb_iso_packet_descriptor *
43619b8f8a0SMichael Lotz 												packetDescriptor,
437853e6be8SMichael Lotz 											uint32 packetCount,
438853e6be8SMichael Lotz 											uint32 *startingFrameNumber,
439853e6be8SMichael Lotz 											uint32 flags,
440853e6be8SMichael Lotz 											usb_callback_func callback,
441853e6be8SMichael Lotz 											void *callbackCookie);
442853e6be8SMichael Lotz 
443853e6be8SMichael Lotz 		status_t						SetPipePolicy(uint8 maxQueuedPackets,
444853e6be8SMichael Lotz 											uint16 maxBufferDurationMS,
445853e6be8SMichael Lotz 											uint16 sampleSize);
446853e6be8SMichael Lotz 		status_t						GetPipePolicy(uint8 *maxQueuedPackets,
447853e6be8SMichael Lotz 											uint16 *maxBufferDurationMS,
448853e6be8SMichael Lotz 											uint16 *sampleSize);
449853e6be8SMichael Lotz 
450853e6be8SMichael Lotz private:
451853e6be8SMichael Lotz 		uint8							fMaxQueuedPackets;
452853e6be8SMichael Lotz 		uint16							fMaxBufferDuration;
453853e6be8SMichael Lotz 		uint16							fSampleSize;
454853e6be8SMichael Lotz };
455853e6be8SMichael Lotz 
456853e6be8SMichael Lotz 
457853e6be8SMichael Lotz class Interface : public Object {
458853e6be8SMichael Lotz public:
459853e6be8SMichael Lotz 										Interface(Object *parent,
460853e6be8SMichael Lotz 											uint8 interfaceIndex);
461853e6be8SMichael Lotz 
46219b8f8a0SMichael Lotz virtual	uint32							Type() const
46319b8f8a0SMichael Lotz 											{ return USB_OBJECT_INTERFACE; }
46419b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "interface"; }
465853e6be8SMichael Lotz 
466853e6be8SMichael Lotz 		// Convenience functions for standard requests
467853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
468853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
469853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
470853e6be8SMichael Lotz 
471853e6be8SMichael Lotz private:
472853e6be8SMichael Lotz 		uint8							fInterfaceIndex;
473853e6be8SMichael Lotz };
474853e6be8SMichael Lotz 
475853e6be8SMichael Lotz 
476853e6be8SMichael Lotz class Device : public Object {
477853e6be8SMichael Lotz public:
478853e6be8SMichael Lotz 										Device(Object *parent, int8 hubAddress,
479853e6be8SMichael Lotz 											uint8 hubPort,
480853e6be8SMichael Lotz 											usb_device_descriptor &desc,
481853e6be8SMichael Lotz 											int8 deviceAddress,
482853e6be8SMichael Lotz 											usb_speed speed, bool isRootHub);
483853e6be8SMichael Lotz virtual									~Device();
484853e6be8SMichael Lotz 
485853e6be8SMichael Lotz 		status_t						InitCheck();
486853e6be8SMichael Lotz 
487853e6be8SMichael Lotz virtual	status_t						Changed(change_item **changeList,
488853e6be8SMichael Lotz 											bool added);
489853e6be8SMichael Lotz 
49019b8f8a0SMichael Lotz virtual	uint32							Type() const
49119b8f8a0SMichael Lotz 											{ return USB_OBJECT_DEVICE; }
49219b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "device"; }
493853e6be8SMichael Lotz 
49419b8f8a0SMichael Lotz 		ControlPipe *					DefaultPipe() const
49519b8f8a0SMichael Lotz 											{ return fDefaultPipe; }
496853e6be8SMichael Lotz 
497853e6be8SMichael Lotz virtual	status_t						GetDescriptor(uint8 descriptorType,
498853e6be8SMichael Lotz 											uint8 index, uint16 languageID,
499853e6be8SMichael Lotz 											void *data, size_t dataLength,
500853e6be8SMichael Lotz 											size_t *actualLength);
501853e6be8SMichael Lotz 
50219b8f8a0SMichael Lotz 		int8							DeviceAddress() const
50319b8f8a0SMichael Lotz 											{ return fDeviceAddress; }
504853e6be8SMichael Lotz 		const usb_device_descriptor *	DeviceDescriptor() const;
50519b8f8a0SMichael Lotz 		usb_speed						Speed() const { return fSpeed; }
506853e6be8SMichael Lotz 
507853e6be8SMichael Lotz 		const usb_configuration_info *	Configuration() const;
508853e6be8SMichael Lotz 		const usb_configuration_info *	ConfigurationAt(uint8 index) const;
50919b8f8a0SMichael Lotz 		status_t						SetConfiguration(
51019b8f8a0SMichael Lotz 											const usb_configuration_info *
51119b8f8a0SMichael Lotz 												configuration);
512853e6be8SMichael Lotz 		status_t						SetConfigurationAt(uint8 index);
513853e6be8SMichael Lotz 		status_t						Unconfigure(bool atDeviceLevel);
514853e6be8SMichael Lotz 
51519b8f8a0SMichael Lotz 		status_t						SetAltInterface(
51619b8f8a0SMichael Lotz 											const usb_interface_info *
51719b8f8a0SMichael Lotz 												interface);
518853e6be8SMichael Lotz 
519853e6be8SMichael Lotz 		void							InitEndpoints(int32 interfaceIndex);
520853e6be8SMichael Lotz 		void							ClearEndpoints(int32 interfaceIndex);
521853e6be8SMichael Lotz 
522853e6be8SMichael Lotz virtual	status_t						ReportDevice(
52319b8f8a0SMichael Lotz 											usb_support_descriptor *
52419b8f8a0SMichael Lotz 												supportDescriptors,
525853e6be8SMichael Lotz 											uint32 supportDescriptorCount,
526853e6be8SMichael Lotz 											const usb_notify_hooks *hooks,
527853e6be8SMichael Lotz 											usb_driver_cookie **cookies,
528853e6be8SMichael Lotz 											bool added, bool recursive);
529853e6be8SMichael Lotz virtual	status_t						BuildDeviceName(char *string,
530853e6be8SMichael Lotz 											uint32 *index, size_t bufferSize,
531853e6be8SMichael Lotz 											Device *device);
532853e6be8SMichael Lotz 
53319b8f8a0SMichael Lotz 		int8							HubAddress() const
53419b8f8a0SMichael Lotz 											{ return fHubAddress; }
53519b8f8a0SMichael Lotz 		uint8							HubPort() const { return fHubPort; }
536853e6be8SMichael Lotz 
537853e6be8SMichael Lotz 		// Convenience functions for standard requests
538853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
539853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
540853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
541853e6be8SMichael Lotz 
542853e6be8SMichael Lotz protected:
543853e6be8SMichael Lotz 		usb_device_descriptor			fDeviceDescriptor;
544853e6be8SMichael Lotz 		bool							fInitOK;
545853e6be8SMichael Lotz 
546853e6be8SMichael Lotz private:
547853e6be8SMichael Lotz 		bool							fAvailable;
548853e6be8SMichael Lotz 		bool							fIsRootHub;
549853e6be8SMichael Lotz 		usb_configuration_info *		fConfigurations;
550853e6be8SMichael Lotz 		usb_configuration_info *		fCurrentConfiguration;
551853e6be8SMichael Lotz 		usb_speed						fSpeed;
552853e6be8SMichael Lotz 		int8							fDeviceAddress;
553853e6be8SMichael Lotz 		int8							fHubAddress;
554853e6be8SMichael Lotz 		uint8							fHubPort;
555853e6be8SMichael Lotz 		ControlPipe *					fDefaultPipe;
556853e6be8SMichael Lotz };
557853e6be8SMichael Lotz 
558853e6be8SMichael Lotz 
559853e6be8SMichael Lotz class Hub : public Device {
560853e6be8SMichael Lotz public:
561853e6be8SMichael Lotz 										Hub(Object *parent, int8 hubAddress,
562853e6be8SMichael Lotz 											uint8 hubPort,
563853e6be8SMichael Lotz 											usb_device_descriptor &desc,
564853e6be8SMichael Lotz 											int8 deviceAddress,
565853e6be8SMichael Lotz 											usb_speed speed, bool isRootHub);
566853e6be8SMichael Lotz virtual									~Hub();
567853e6be8SMichael Lotz 
568853e6be8SMichael Lotz virtual	status_t						Changed(change_item **changeList,
569853e6be8SMichael Lotz 											bool added);
570853e6be8SMichael Lotz 
57119b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_DEVICE
57219b8f8a0SMichael Lotz 											| USB_OBJECT_HUB; }
57319b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "hub"; }
574853e6be8SMichael Lotz 
575853e6be8SMichael Lotz virtual	status_t						GetDescriptor(uint8 descriptorType,
576853e6be8SMichael Lotz 											uint8 index, uint16 languageID,
577853e6be8SMichael Lotz 											void *data, size_t dataLength,
578853e6be8SMichael Lotz 											size_t *actualLength);
579853e6be8SMichael Lotz 
58019b8f8a0SMichael Lotz 		Device *						ChildAt(uint8 index) const
58119b8f8a0SMichael Lotz 											{ return fChildren[index]; }
582853e6be8SMichael Lotz 
583853e6be8SMichael Lotz 		status_t						UpdatePortStatus(uint8 index);
584853e6be8SMichael Lotz 		status_t						ResetPort(uint8 index);
585853e6be8SMichael Lotz 		status_t						DisablePort(uint8 index);
586853e6be8SMichael Lotz 
587853e6be8SMichael Lotz 		void							Explore(change_item **changeList);
588853e6be8SMichael Lotz static	void							InterruptCallback(void *cookie,
589853e6be8SMichael Lotz 											status_t status, void *data,
590853e6be8SMichael Lotz 											size_t actualLength);
591853e6be8SMichael Lotz 
592853e6be8SMichael Lotz virtual	status_t						ReportDevice(
59319b8f8a0SMichael Lotz 											usb_support_descriptor *
59419b8f8a0SMichael Lotz 												supportDescriptors,
595853e6be8SMichael Lotz 											uint32 supportDescriptorCount,
596853e6be8SMichael Lotz 											const usb_notify_hooks *hooks,
597853e6be8SMichael Lotz 											usb_driver_cookie **cookies,
598853e6be8SMichael Lotz 											bool added, bool recursive);
599853e6be8SMichael Lotz virtual	status_t						BuildDeviceName(char *string,
600853e6be8SMichael Lotz 											uint32 *index, size_t bufferSize,
601853e6be8SMichael Lotz 											Device *device);
602853e6be8SMichael Lotz 
603853e6be8SMichael Lotz private:
604853e6be8SMichael Lotz 		InterruptPipe *					fInterruptPipe;
605853e6be8SMichael Lotz 		usb_hub_descriptor				fHubDescriptor;
606853e6be8SMichael Lotz 
607853e6be8SMichael Lotz 		usb_port_status					fInterruptStatus[USB_MAX_PORT_COUNT];
608853e6be8SMichael Lotz 		usb_port_status					fPortStatus[USB_MAX_PORT_COUNT];
609853e6be8SMichael Lotz 		Device *						fChildren[USB_MAX_PORT_COUNT];
610853e6be8SMichael Lotz };
611853e6be8SMichael Lotz 
612853e6be8SMichael Lotz 
613853e6be8SMichael Lotz /*
614853e6be8SMichael Lotz  * A Transfer is allocated on the heap and passed to the Host Controller in
615853e6be8SMichael Lotz  * SubmitTransfer(). It is generated for all queued transfers. If queuing
616853e6be8SMichael Lotz  * succeds (SubmitTransfer() returns with >= B_OK) the Host Controller takes
617853e6be8SMichael Lotz  * ownership of the Transfer and will delete it as soon as it has called the
618853e6be8SMichael Lotz  * set callback function. If SubmitTransfer() failes, the calling function is
619853e6be8SMichael Lotz  * responsible for deleting the Transfer.
620853e6be8SMichael Lotz  * Also, the transfer takes ownership of the usb_request_data passed to it in
621853e6be8SMichael Lotz  * SetRequestData(), but does not take ownership of the data buffer set by
622853e6be8SMichael Lotz  * SetData().
623853e6be8SMichael Lotz  */
624853e6be8SMichael Lotz class Transfer {
625853e6be8SMichael Lotz public:
626853e6be8SMichael Lotz 									Transfer(Pipe *pipe);
627853e6be8SMichael Lotz 									~Transfer();
628853e6be8SMichael Lotz 
62919b8f8a0SMichael Lotz 		Pipe *						TransferPipe() const { return fPipe; }
630853e6be8SMichael Lotz 
631853e6be8SMichael Lotz 		void						SetRequestData(usb_request_data *data);
63219b8f8a0SMichael Lotz 		usb_request_data *			RequestData() const { return fRequestData; }
633853e6be8SMichael Lotz 
63419b8f8a0SMichael Lotz 		void						SetIsochronousData(
63519b8f8a0SMichael Lotz 										usb_isochronous_data *data);
63619b8f8a0SMichael Lotz 		usb_isochronous_data *		IsochronousData() const
63719b8f8a0SMichael Lotz 										{ return fIsochronousData; }
638853e6be8SMichael Lotz 
639853e6be8SMichael Lotz 		void						SetData(uint8 *buffer, size_t length);
64019b8f8a0SMichael Lotz 		uint8 *						Data() const
64119b8f8a0SMichael Lotz 										{ return (uint8 *)fData.iov_base; }
64219b8f8a0SMichael Lotz 		size_t						DataLength() const { return fData.iov_len; }
643853e6be8SMichael Lotz 
644853e6be8SMichael Lotz 		void						SetPhysical(bool physical);
64519b8f8a0SMichael Lotz 		bool						IsPhysical() const { return fPhysical; }
646853e6be8SMichael Lotz 
64719b8f8a0SMichael Lotz 		void						SetVector(iovec *vector,
64819b8f8a0SMichael Lotz 										size_t vectorCount);
64919b8f8a0SMichael Lotz 		iovec *						Vector() { return fVector; }
65019b8f8a0SMichael Lotz 		size_t						VectorCount() const { return fVectorCount; }
651853e6be8SMichael Lotz 		size_t						VectorLength();
652853e6be8SMichael Lotz 
65319b8f8a0SMichael Lotz 		uint16						Bandwidth() const { return fBandwidth; }
654853e6be8SMichael Lotz 
65519b8f8a0SMichael Lotz 		bool						IsFragmented() const { return fFragmented; }
656853e6be8SMichael Lotz 		void						AdvanceByFragment(size_t actualLength);
657853e6be8SMichael Lotz 
658853e6be8SMichael Lotz 		status_t					InitKernelAccess();
659853e6be8SMichael Lotz 		status_t					PrepareKernelAccess();
660853e6be8SMichael Lotz 
661853e6be8SMichael Lotz 		void						SetCallback(usb_callback_func callback,
662853e6be8SMichael Lotz 										void *cookie);
663853e6be8SMichael Lotz 
66419b8f8a0SMichael Lotz 		void						Finished(uint32 status,
66519b8f8a0SMichael Lotz 										size_t actualLength);
666853e6be8SMichael Lotz 
66719b8f8a0SMichael Lotz 		usb_id						USBID() const { return 0; }
66819b8f8a0SMichael Lotz 		const char *				TypeName() const { return "transfer"; }
669853e6be8SMichael Lotz 
670853e6be8SMichael Lotz private:
671853e6be8SMichael Lotz 		status_t					_CalculateBandwidth();
672853e6be8SMichael Lotz 
673853e6be8SMichael Lotz 		// Data that is related to the transfer
674853e6be8SMichael Lotz 		Pipe *						fPipe;
675853e6be8SMichael Lotz 		iovec						fData;
676853e6be8SMichael Lotz 		iovec *						fVector;
677853e6be8SMichael Lotz 		size_t						fVectorCount;
678853e6be8SMichael Lotz 		void *						fBaseAddress;
679853e6be8SMichael Lotz 		bool						fPhysical;
680853e6be8SMichael Lotz 		bool						fFragmented;
681853e6be8SMichael Lotz 		size_t						fActualLength;
682853e6be8SMichael Lotz 		area_id						fUserArea;
683853e6be8SMichael Lotz 		area_id						fClonedArea;
684853e6be8SMichael Lotz 
685853e6be8SMichael Lotz 		usb_callback_func			fCallback;
686853e6be8SMichael Lotz 		void *						fCallbackCookie;
687853e6be8SMichael Lotz 
688853e6be8SMichael Lotz 		// For control transfers
689853e6be8SMichael Lotz 		usb_request_data *			fRequestData;
690853e6be8SMichael Lotz 
691853e6be8SMichael Lotz 		// For isochronous transfers
692853e6be8SMichael Lotz 		usb_isochronous_data *		fIsochronousData;
693853e6be8SMichael Lotz 
694853e6be8SMichael Lotz 		// For bandwidth management.
695853e6be8SMichael Lotz 		// It contains the bandwidth necessary in microseconds
696853e6be8SMichael Lotz 		// for either isochronous, interrupt or control transfers.
697853e6be8SMichael Lotz 		// Not used for bulk transactions.
698853e6be8SMichael Lotz 		uint16						fBandwidth;
699853e6be8SMichael Lotz };
700853e6be8SMichael Lotz 
701cc9f959dSMichael Lotz #endif // _USB_PRIVATE_H
702