xref: /haiku/src/add-ons/kernel/bus_managers/usb/usb_private.h (revision 0e76cf0b69324e071618d5a7105d1084d2a27550)
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>
15*0e76cf0bSJé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,
93853e6be8SMichael Lotz 	USB_SPEED_MAX = USB_SPEED_HIGHSPEED
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);
126853e6be8SMichael Lotz 		void							PutUSBID(usb_id id);
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,
13719b8f8a0SMichael Lotz 											void **physicalAddress,
13819b8f8a0SMichael Lotz 											size_t size);
139853e6be8SMichael Lotz 		status_t						FreeChunk(void *logicalAddress,
140853e6be8SMichael Lotz 											void *physicalAddress, size_t size);
141853e6be8SMichael Lotz 
142853e6be8SMichael Lotz 		area_id							AllocateArea(void **logicalAddress,
143853e6be8SMichael Lotz 											void **physicalAddress,
144853e6be8SMichael Lotz 											size_t size, const char *name);
145853e6be8SMichael Lotz 
146853e6be8SMichael Lotz 		void							NotifyDeviceChange(Device *device,
147853e6be8SMichael Lotz 											rescan_item **rescanList,
148853e6be8SMichael Lotz 											bool added);
149853e6be8SMichael Lotz 		void							RescanDrivers(rescan_item *rescanItem);
150853e6be8SMichael Lotz 
151853e6be8SMichael Lotz 		// USB API
152853e6be8SMichael Lotz 		status_t						RegisterDriver(const char *driverName,
15319b8f8a0SMichael Lotz 											const usb_support_descriptor *
15419b8f8a0SMichael Lotz 												descriptors,
155853e6be8SMichael Lotz 											size_t descriptorCount,
156853e6be8SMichael Lotz 											const char *republishDriverName);
157853e6be8SMichael Lotz 
158853e6be8SMichael Lotz 		status_t						InstallNotify(const char *driverName,
159853e6be8SMichael Lotz 											const usb_notify_hooks *hooks);
160853e6be8SMichael Lotz 		status_t						UninstallNotify(const char *driverName);
161853e6be8SMichael Lotz 
16219b8f8a0SMichael Lotz 		usb_id							USBID() const { return 0; }
16319b8f8a0SMichael Lotz 		const char *					TypeName() const { return "stack"; }
164853e6be8SMichael Lotz 
165853e6be8SMichael Lotz private:
166853e6be8SMichael Lotz static	int32							ExploreThread(void *data);
167853e6be8SMichael Lotz 
168853e6be8SMichael Lotz 		Vector<BusManager *>			fBusManagers;
169853e6be8SMichael Lotz 		thread_id						fExploreThread;
170853e6be8SMichael Lotz 		bool							fFirstExploreDone;
171853e6be8SMichael Lotz 		bool							fStopThreads;
172853e6be8SMichael Lotz 
173853e6be8SMichael Lotz 		mutex							fStackLock;
174853e6be8SMichael Lotz 		mutex							fExploreLock;
175853e6be8SMichael Lotz 		PhysicalMemoryAllocator *		fAllocator;
176853e6be8SMichael Lotz 
177853e6be8SMichael Lotz 		uint32							fObjectIndex;
178853e6be8SMichael Lotz 		uint32							fObjectMaxCount;
179853e6be8SMichael Lotz 		Object **						fObjectArray;
180853e6be8SMichael Lotz 
181853e6be8SMichael Lotz 		usb_driver_info *				fDriverList;
182853e6be8SMichael Lotz };
183853e6be8SMichael Lotz 
184853e6be8SMichael Lotz 
185853e6be8SMichael Lotz /*
186853e6be8SMichael Lotz  * This class manages a bus. It is created by the Stack object
187853e6be8SMichael Lotz  * after a host controller gives positive feedback on whether the hardware
188853e6be8SMichael Lotz  * is found.
189853e6be8SMichael Lotz  */
190853e6be8SMichael Lotz class BusManager {
191853e6be8SMichael Lotz public:
192853e6be8SMichael Lotz 										BusManager(Stack *stack);
193853e6be8SMichael Lotz virtual									~BusManager();
194853e6be8SMichael Lotz 
195853e6be8SMichael Lotz virtual	status_t						InitCheck();
196853e6be8SMichael Lotz 
197853e6be8SMichael Lotz 		bool							Lock();
198853e6be8SMichael Lotz 		void							Unlock();
199853e6be8SMichael Lotz 
200853e6be8SMichael Lotz 		int8							AllocateAddress();
201853e6be8SMichael Lotz 		void							FreeAddress(int8 address);
202853e6be8SMichael Lotz 
203853e6be8SMichael Lotz 		Device *						AllocateDevice(Hub *parent,
204853e6be8SMichael Lotz 											int8 hubAddress, uint8 hubPort,
205853e6be8SMichael Lotz 											usb_speed speed);
206853e6be8SMichael Lotz 		void							FreeDevice(Device *device);
207853e6be8SMichael Lotz 
208853e6be8SMichael Lotz virtual	status_t						Start();
209853e6be8SMichael Lotz virtual	status_t						Stop();
210853e6be8SMichael Lotz 
211853e6be8SMichael Lotz virtual	status_t						SubmitTransfer(Transfer *transfer);
212853e6be8SMichael Lotz virtual	status_t						CancelQueuedTransfers(Pipe *pipe,
213853e6be8SMichael Lotz 											bool force);
214853e6be8SMichael Lotz 
215853e6be8SMichael Lotz virtual	status_t						NotifyPipeChange(Pipe *pipe,
216853e6be8SMichael Lotz 											usb_change change);
217853e6be8SMichael Lotz 
21819b8f8a0SMichael Lotz 		Object *						RootObject() const
21919b8f8a0SMichael Lotz 											{ return fRootObject; }
220853e6be8SMichael Lotz 
22119b8f8a0SMichael Lotz 		Hub *							GetRootHub() const { return fRootHub; }
22219b8f8a0SMichael Lotz 		void							SetRootHub(Hub *hub) { fRootHub = hub; }
223853e6be8SMichael Lotz 
22419b8f8a0SMichael Lotz 		usb_id							USBID() const { return fUSBID; }
22538fc536eSMichael Lotz virtual	const char *					TypeName() const = 0;
226853e6be8SMichael Lotz 
227853e6be8SMichael Lotz protected:
228853e6be8SMichael Lotz 		bool							fInitOK;
229853e6be8SMichael Lotz 
230853e6be8SMichael Lotz private:
231853e6be8SMichael Lotz 		ControlPipe *					_GetDefaultPipe(usb_speed);
232853e6be8SMichael Lotz 
233853e6be8SMichael Lotz 		mutex							fLock;
234853e6be8SMichael Lotz 
235853e6be8SMichael Lotz 		bool							fDeviceMap[128];
236853e6be8SMichael Lotz 		int8							fDeviceIndex;
237853e6be8SMichael Lotz 
238853e6be8SMichael Lotz 		Stack *							fStack;
239853e6be8SMichael Lotz 		ControlPipe *					fDefaultPipes[USB_SPEED_MAX + 1];
240853e6be8SMichael Lotz 		Hub *							fRootHub;
241853e6be8SMichael Lotz 		Object *						fRootObject;
24219b8f8a0SMichael Lotz 
24319b8f8a0SMichael Lotz 		usb_id							fUSBID;
244853e6be8SMichael Lotz };
245853e6be8SMichael Lotz 
246853e6be8SMichael Lotz 
247853e6be8SMichael Lotz class Object {
248853e6be8SMichael Lotz public:
249853e6be8SMichael Lotz 										Object(Stack *stack, BusManager *bus);
250853e6be8SMichael Lotz 										Object(Object *parent);
251853e6be8SMichael Lotz virtual									~Object();
252853e6be8SMichael Lotz 
25319b8f8a0SMichael Lotz 		Object *						Parent() const { return fParent; }
254853e6be8SMichael Lotz 
25519b8f8a0SMichael Lotz 		BusManager *					GetBusManager() const
25619b8f8a0SMichael Lotz 											{ return fBusManager; }
25719b8f8a0SMichael Lotz 		Stack *							GetStack() const { return fStack; }
258853e6be8SMichael Lotz 
25919b8f8a0SMichael Lotz 		usb_id							USBID() const { return fUSBID; }
26019b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_NONE; }
26119b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "object"; }
262853e6be8SMichael Lotz 
263853e6be8SMichael Lotz 		// Convenience functions for standard requests
264853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
265853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
266853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
267853e6be8SMichael Lotz 
268853e6be8SMichael Lotz private:
269853e6be8SMichael Lotz 		Object *						fParent;
270853e6be8SMichael Lotz 		BusManager *					fBusManager;
271853e6be8SMichael Lotz 		Stack *							fStack;
272853e6be8SMichael Lotz 		usb_id							fUSBID;
273853e6be8SMichael Lotz };
274853e6be8SMichael Lotz 
275853e6be8SMichael Lotz 
276853e6be8SMichael Lotz /*
277853e6be8SMichael Lotz  * The Pipe class is the communication management between the hardware and
278853e6be8SMichael Lotz  * the stack. It creates packets, manages these and performs callbacks.
279853e6be8SMichael Lotz  */
280853e6be8SMichael Lotz class Pipe : public Object {
281853e6be8SMichael Lotz public:
282853e6be8SMichael Lotz 		enum pipeDirection { In, Out, Default };
283853e6be8SMichael Lotz 
284853e6be8SMichael Lotz 										Pipe(Object *parent);
285853e6be8SMichael Lotz virtual									~Pipe();
286853e6be8SMichael Lotz 
287853e6be8SMichael Lotz 		void							InitCommon(int8 deviceAddress,
288853e6be8SMichael Lotz 											uint8 endpointAddress,
289853e6be8SMichael Lotz 											usb_speed speed,
290853e6be8SMichael Lotz 											pipeDirection direction,
291853e6be8SMichael Lotz 											size_t maxPacketSize,
292853e6be8SMichael Lotz 											uint8 interval,
293853e6be8SMichael Lotz 											int8 hubAddress, uint8 hubPort);
294853e6be8SMichael Lotz 
29519b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE; }
29619b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "pipe"; }
297853e6be8SMichael Lotz 
29819b8f8a0SMichael Lotz 		int8							DeviceAddress() const
29919b8f8a0SMichael Lotz 											{ return fDeviceAddress; }
30019b8f8a0SMichael Lotz 		usb_speed						Speed() const { return fSpeed; }
30119b8f8a0SMichael Lotz 		pipeDirection					Direction() const { return fDirection; }
30219b8f8a0SMichael Lotz 		uint8							EndpointAddress() const
30319b8f8a0SMichael Lotz 											{ return fEndpointAddress; }
30419b8f8a0SMichael Lotz 		size_t							MaxPacketSize() const
30519b8f8a0SMichael Lotz 											{ return fMaxPacketSize; }
30619b8f8a0SMichael Lotz 		uint8							Interval() const { return fInterval; }
307853e6be8SMichael Lotz 
308853e6be8SMichael Lotz 		// Hub port being the one-based logical port number on the hub
309853e6be8SMichael Lotz 		void							SetHubInfo(int8 address, uint8 port);
31019b8f8a0SMichael Lotz 		int8							HubAddress() const
31119b8f8a0SMichael Lotz 											{ return fHubAddress; }
31219b8f8a0SMichael Lotz 		uint8							HubPort() const { return fHubPort; }
313853e6be8SMichael Lotz 
31419b8f8a0SMichael Lotz virtual	bool							DataToggle() const
31519b8f8a0SMichael Lotz 											{ return fDataToggle; }
31619b8f8a0SMichael Lotz virtual	void							SetDataToggle(bool toggle)
31719b8f8a0SMichael Lotz 											{ fDataToggle = toggle; }
318853e6be8SMichael Lotz 
319853e6be8SMichael Lotz 		status_t						SubmitTransfer(Transfer *transfer);
320853e6be8SMichael Lotz 		status_t						CancelQueuedTransfers(bool force);
321853e6be8SMichael Lotz 
32219b8f8a0SMichael Lotz 		void							SetControllerCookie(void *cookie)
32319b8f8a0SMichael Lotz 											{ fControllerCookie = cookie; }
32419b8f8a0SMichael Lotz 		void *							ControllerCookie() const
32519b8f8a0SMichael Lotz 											{ return fControllerCookie; }
326853e6be8SMichael Lotz 
327853e6be8SMichael Lotz 		// Convenience functions for standard requests
328853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
329853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
330853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
331853e6be8SMichael Lotz 
332853e6be8SMichael Lotz private:
333853e6be8SMichael Lotz 		int8							fDeviceAddress;
334853e6be8SMichael Lotz 		uint8							fEndpointAddress;
335853e6be8SMichael Lotz 		pipeDirection					fDirection;
336853e6be8SMichael Lotz 		usb_speed						fSpeed;
337853e6be8SMichael Lotz 		size_t							fMaxPacketSize;
338853e6be8SMichael Lotz 		uint8							fInterval;
339853e6be8SMichael Lotz 		int8							fHubAddress;
340853e6be8SMichael Lotz 		uint8							fHubPort;
341853e6be8SMichael Lotz 		bool							fDataToggle;
342853e6be8SMichael Lotz 		void *							fControllerCookie;
343853e6be8SMichael Lotz };
344853e6be8SMichael Lotz 
345853e6be8SMichael Lotz 
346853e6be8SMichael Lotz class ControlPipe : public Pipe {
347853e6be8SMichael Lotz public:
348853e6be8SMichael Lotz 										ControlPipe(Object *parent);
349853e6be8SMichael Lotz virtual									~ControlPipe();
350853e6be8SMichael Lotz 
35119b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
35219b8f8a0SMichael Lotz 											| USB_OBJECT_CONTROL_PIPE; }
35319b8f8a0SMichael Lotz virtual	const char *					TypeName() const
35419b8f8a0SMichael Lotz 											{ return "control pipe"; }
355853e6be8SMichael Lotz 
356853e6be8SMichael Lotz 										// The data toggle is not relevant
357853e6be8SMichael Lotz 										// for control transfers, as they are
358853e6be8SMichael Lotz 										// always enclosed by a setup and
359853e6be8SMichael Lotz 										// status packet. The toggle always
360853e6be8SMichael Lotz 										// starts at 1.
36119b8f8a0SMichael Lotz virtual	bool							DataToggle() const { return true; }
36238fc536eSMichael Lotz virtual	void							SetDataToggle(bool toggle) {}
363853e6be8SMichael Lotz 
364853e6be8SMichael Lotz 		status_t						SendRequest(uint8 requestType,
365853e6be8SMichael Lotz 											uint8 request, uint16 value,
366853e6be8SMichael Lotz 											uint16 index, uint16 length,
367853e6be8SMichael Lotz 											void *data, size_t dataLength,
368853e6be8SMichael Lotz 											size_t *actualLength);
369853e6be8SMichael Lotz static	void							SendRequestCallback(void *cookie,
370853e6be8SMichael Lotz 											status_t status, void *data,
371853e6be8SMichael Lotz 											size_t actualLength);
372853e6be8SMichael Lotz 
373853e6be8SMichael Lotz 		status_t						QueueRequest(uint8 requestType,
374853e6be8SMichael Lotz 											uint8 request, uint16 value,
375853e6be8SMichael Lotz 											uint16 index, uint16 length,
376853e6be8SMichael Lotz 											void *data, size_t dataLength,
377853e6be8SMichael Lotz 											usb_callback_func callback,
378853e6be8SMichael Lotz 											void *callbackCookie);
379853e6be8SMichael Lotz 
380853e6be8SMichael Lotz private:
381853e6be8SMichael Lotz 		mutex							fSendRequestLock;
382853e6be8SMichael Lotz 		sem_id							fNotifySem;
383853e6be8SMichael Lotz 		status_t						fTransferStatus;
384853e6be8SMichael Lotz 		size_t							fActualLength;
385853e6be8SMichael Lotz };
386853e6be8SMichael Lotz 
387853e6be8SMichael Lotz 
388853e6be8SMichael Lotz class InterruptPipe : public Pipe {
389853e6be8SMichael Lotz public:
390853e6be8SMichael Lotz 										InterruptPipe(Object *parent);
391853e6be8SMichael Lotz 
39219b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
39319b8f8a0SMichael Lotz 											| USB_OBJECT_INTERRUPT_PIPE; }
39419b8f8a0SMichael Lotz virtual	const char *					TypeName() const
39519b8f8a0SMichael Lotz 											{ return "interrupt pipe"; }
396853e6be8SMichael Lotz 
397853e6be8SMichael Lotz 		status_t						QueueInterrupt(void *data,
398853e6be8SMichael Lotz 											size_t dataLength,
399853e6be8SMichael Lotz 											usb_callback_func callback,
400853e6be8SMichael Lotz 											void *callbackCookie);
401853e6be8SMichael Lotz };
402853e6be8SMichael Lotz 
403853e6be8SMichael Lotz 
404853e6be8SMichael Lotz class BulkPipe : public Pipe {
405853e6be8SMichael Lotz public:
406853e6be8SMichael Lotz 										BulkPipe(Object *parent);
407853e6be8SMichael Lotz 
40819b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
40919b8f8a0SMichael Lotz 											| USB_OBJECT_BULK_PIPE; }
41019b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "bulk pipe"; }
411853e6be8SMichael Lotz 
412853e6be8SMichael Lotz 		status_t						QueueBulk(void *data,
413853e6be8SMichael Lotz 											size_t dataLength,
414853e6be8SMichael Lotz 											usb_callback_func callback,
415853e6be8SMichael Lotz 											void *callbackCookie);
416853e6be8SMichael Lotz 		status_t						QueueBulkV(iovec *vector,
417853e6be8SMichael Lotz 											size_t vectorCount,
418853e6be8SMichael Lotz 											usb_callback_func callback,
419853e6be8SMichael Lotz 											void *callbackCookie,
420853e6be8SMichael Lotz 											bool physical);
421853e6be8SMichael Lotz };
422853e6be8SMichael Lotz 
423853e6be8SMichael Lotz 
424853e6be8SMichael Lotz class IsochronousPipe : public Pipe {
425853e6be8SMichael Lotz public:
426853e6be8SMichael Lotz 										IsochronousPipe(Object *parent);
427853e6be8SMichael Lotz 
42819b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
42919b8f8a0SMichael Lotz 											| USB_OBJECT_ISO_PIPE; }
43019b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "iso pipe"; }
431853e6be8SMichael Lotz 
432853e6be8SMichael Lotz 		status_t						QueueIsochronous(void *data,
433853e6be8SMichael Lotz 											size_t dataLength,
43419b8f8a0SMichael Lotz 											usb_iso_packet_descriptor *
43519b8f8a0SMichael Lotz 												packetDescriptor,
436853e6be8SMichael Lotz 											uint32 packetCount,
437853e6be8SMichael Lotz 											uint32 *startingFrameNumber,
438853e6be8SMichael Lotz 											uint32 flags,
439853e6be8SMichael Lotz 											usb_callback_func callback,
440853e6be8SMichael Lotz 											void *callbackCookie);
441853e6be8SMichael Lotz 
442853e6be8SMichael Lotz 		status_t						SetPipePolicy(uint8 maxQueuedPackets,
443853e6be8SMichael Lotz 											uint16 maxBufferDurationMS,
444853e6be8SMichael Lotz 											uint16 sampleSize);
445853e6be8SMichael Lotz 		status_t						GetPipePolicy(uint8 *maxQueuedPackets,
446853e6be8SMichael Lotz 											uint16 *maxBufferDurationMS,
447853e6be8SMichael Lotz 											uint16 *sampleSize);
448853e6be8SMichael Lotz 
449853e6be8SMichael Lotz private:
450853e6be8SMichael Lotz 		uint8							fMaxQueuedPackets;
451853e6be8SMichael Lotz 		uint16							fMaxBufferDuration;
452853e6be8SMichael Lotz 		uint16							fSampleSize;
453853e6be8SMichael Lotz };
454853e6be8SMichael Lotz 
455853e6be8SMichael Lotz 
456853e6be8SMichael Lotz class Interface : public Object {
457853e6be8SMichael Lotz public:
458853e6be8SMichael Lotz 										Interface(Object *parent,
459853e6be8SMichael Lotz 											uint8 interfaceIndex);
460853e6be8SMichael Lotz 
46119b8f8a0SMichael Lotz virtual	uint32							Type() const
46219b8f8a0SMichael Lotz 											{ return USB_OBJECT_INTERFACE; }
46319b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "interface"; }
464853e6be8SMichael Lotz 
465853e6be8SMichael Lotz 		// Convenience functions for standard requests
466853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
467853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
468853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
469853e6be8SMichael Lotz 
470853e6be8SMichael Lotz private:
471853e6be8SMichael Lotz 		uint8							fInterfaceIndex;
472853e6be8SMichael Lotz };
473853e6be8SMichael Lotz 
474853e6be8SMichael Lotz 
475853e6be8SMichael Lotz class Device : public Object {
476853e6be8SMichael Lotz public:
477853e6be8SMichael Lotz 										Device(Object *parent, int8 hubAddress,
478853e6be8SMichael Lotz 											uint8 hubPort,
479853e6be8SMichael Lotz 											usb_device_descriptor &desc,
480853e6be8SMichael Lotz 											int8 deviceAddress,
481853e6be8SMichael Lotz 											usb_speed speed, bool isRootHub);
482853e6be8SMichael Lotz virtual									~Device();
483853e6be8SMichael Lotz 
484853e6be8SMichael Lotz 		status_t						InitCheck();
485853e6be8SMichael Lotz 
486853e6be8SMichael Lotz virtual	status_t						Changed(change_item **changeList,
487853e6be8SMichael Lotz 											bool added);
488853e6be8SMichael Lotz 
48919b8f8a0SMichael Lotz virtual	uint32							Type() const
49019b8f8a0SMichael Lotz 											{ return USB_OBJECT_DEVICE; }
49119b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "device"; }
492853e6be8SMichael Lotz 
49319b8f8a0SMichael Lotz 		ControlPipe *					DefaultPipe() const
49419b8f8a0SMichael Lotz 											{ return fDefaultPipe; }
495853e6be8SMichael Lotz 
496853e6be8SMichael Lotz virtual	status_t						GetDescriptor(uint8 descriptorType,
497853e6be8SMichael Lotz 											uint8 index, uint16 languageID,
498853e6be8SMichael Lotz 											void *data, size_t dataLength,
499853e6be8SMichael Lotz 											size_t *actualLength);
500853e6be8SMichael Lotz 
50119b8f8a0SMichael Lotz 		int8							DeviceAddress() const
50219b8f8a0SMichael Lotz 											{ return fDeviceAddress; }
503853e6be8SMichael Lotz 		const usb_device_descriptor *	DeviceDescriptor() const;
50419b8f8a0SMichael Lotz 		usb_speed						Speed() const { return fSpeed; }
505853e6be8SMichael Lotz 
506853e6be8SMichael Lotz 		const usb_configuration_info *	Configuration() const;
507853e6be8SMichael Lotz 		const usb_configuration_info *	ConfigurationAt(uint8 index) const;
50819b8f8a0SMichael Lotz 		status_t						SetConfiguration(
50919b8f8a0SMichael Lotz 											const usb_configuration_info *
51019b8f8a0SMichael Lotz 												configuration);
511853e6be8SMichael Lotz 		status_t						SetConfigurationAt(uint8 index);
512853e6be8SMichael Lotz 		status_t						Unconfigure(bool atDeviceLevel);
513853e6be8SMichael Lotz 
51419b8f8a0SMichael Lotz 		status_t						SetAltInterface(
51519b8f8a0SMichael Lotz 											const usb_interface_info *
51619b8f8a0SMichael Lotz 												interface);
517853e6be8SMichael Lotz 
518853e6be8SMichael Lotz 		void							InitEndpoints(int32 interfaceIndex);
519853e6be8SMichael Lotz 		void							ClearEndpoints(int32 interfaceIndex);
520853e6be8SMichael Lotz 
521853e6be8SMichael Lotz virtual	status_t						ReportDevice(
52219b8f8a0SMichael Lotz 											usb_support_descriptor *
52319b8f8a0SMichael Lotz 												supportDescriptors,
524853e6be8SMichael Lotz 											uint32 supportDescriptorCount,
525853e6be8SMichael Lotz 											const usb_notify_hooks *hooks,
526853e6be8SMichael Lotz 											usb_driver_cookie **cookies,
527853e6be8SMichael Lotz 											bool added, bool recursive);
528853e6be8SMichael Lotz virtual	status_t						BuildDeviceName(char *string,
529853e6be8SMichael Lotz 											uint32 *index, size_t bufferSize,
530853e6be8SMichael Lotz 											Device *device);
531853e6be8SMichael Lotz 
53219b8f8a0SMichael Lotz 		int8							HubAddress() const
53319b8f8a0SMichael Lotz 											{ return fHubAddress; }
53419b8f8a0SMichael Lotz 		uint8							HubPort() const { return fHubPort; }
535853e6be8SMichael Lotz 
536853e6be8SMichael Lotz 		// Convenience functions for standard requests
537853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
538853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
539853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
540853e6be8SMichael Lotz 
541853e6be8SMichael Lotz protected:
542853e6be8SMichael Lotz 		usb_device_descriptor			fDeviceDescriptor;
543853e6be8SMichael Lotz 		bool							fInitOK;
544853e6be8SMichael Lotz 
545853e6be8SMichael Lotz private:
546853e6be8SMichael Lotz 		bool							fAvailable;
547853e6be8SMichael Lotz 		bool							fIsRootHub;
548853e6be8SMichael Lotz 		usb_configuration_info *		fConfigurations;
549853e6be8SMichael Lotz 		usb_configuration_info *		fCurrentConfiguration;
550853e6be8SMichael Lotz 		usb_speed						fSpeed;
551853e6be8SMichael Lotz 		int8							fDeviceAddress;
552853e6be8SMichael Lotz 		int8							fHubAddress;
553853e6be8SMichael Lotz 		uint8							fHubPort;
554853e6be8SMichael Lotz 		ControlPipe *					fDefaultPipe;
555853e6be8SMichael Lotz };
556853e6be8SMichael Lotz 
557853e6be8SMichael Lotz 
558853e6be8SMichael Lotz class Hub : public Device {
559853e6be8SMichael Lotz public:
560853e6be8SMichael Lotz 										Hub(Object *parent, int8 hubAddress,
561853e6be8SMichael Lotz 											uint8 hubPort,
562853e6be8SMichael Lotz 											usb_device_descriptor &desc,
563853e6be8SMichael Lotz 											int8 deviceAddress,
564853e6be8SMichael Lotz 											usb_speed speed, bool isRootHub);
565853e6be8SMichael Lotz virtual									~Hub();
566853e6be8SMichael Lotz 
567853e6be8SMichael Lotz virtual	status_t						Changed(change_item **changeList,
568853e6be8SMichael Lotz 											bool added);
569853e6be8SMichael Lotz 
57019b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_DEVICE
57119b8f8a0SMichael Lotz 											| USB_OBJECT_HUB; }
57219b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "hub"; }
573853e6be8SMichael Lotz 
574853e6be8SMichael Lotz virtual	status_t						GetDescriptor(uint8 descriptorType,
575853e6be8SMichael Lotz 											uint8 index, uint16 languageID,
576853e6be8SMichael Lotz 											void *data, size_t dataLength,
577853e6be8SMichael Lotz 											size_t *actualLength);
578853e6be8SMichael Lotz 
57919b8f8a0SMichael Lotz 		Device *						ChildAt(uint8 index) const
58019b8f8a0SMichael Lotz 											{ return fChildren[index]; }
581853e6be8SMichael Lotz 
582853e6be8SMichael Lotz 		status_t						UpdatePortStatus(uint8 index);
583853e6be8SMichael Lotz 		status_t						ResetPort(uint8 index);
584853e6be8SMichael Lotz 		status_t						DisablePort(uint8 index);
585853e6be8SMichael Lotz 
586853e6be8SMichael Lotz 		void							Explore(change_item **changeList);
587853e6be8SMichael Lotz static	void							InterruptCallback(void *cookie,
588853e6be8SMichael Lotz 											status_t status, void *data,
589853e6be8SMichael Lotz 											size_t actualLength);
590853e6be8SMichael Lotz 
591853e6be8SMichael Lotz virtual	status_t						ReportDevice(
59219b8f8a0SMichael Lotz 											usb_support_descriptor *
59319b8f8a0SMichael Lotz 												supportDescriptors,
594853e6be8SMichael Lotz 											uint32 supportDescriptorCount,
595853e6be8SMichael Lotz 											const usb_notify_hooks *hooks,
596853e6be8SMichael Lotz 											usb_driver_cookie **cookies,
597853e6be8SMichael Lotz 											bool added, bool recursive);
598853e6be8SMichael Lotz virtual	status_t						BuildDeviceName(char *string,
599853e6be8SMichael Lotz 											uint32 *index, size_t bufferSize,
600853e6be8SMichael Lotz 											Device *device);
601853e6be8SMichael Lotz 
602853e6be8SMichael Lotz private:
603853e6be8SMichael Lotz 		InterruptPipe *					fInterruptPipe;
604853e6be8SMichael Lotz 		usb_hub_descriptor				fHubDescriptor;
605853e6be8SMichael Lotz 
606853e6be8SMichael Lotz 		usb_port_status					fInterruptStatus[USB_MAX_PORT_COUNT];
607853e6be8SMichael Lotz 		usb_port_status					fPortStatus[USB_MAX_PORT_COUNT];
608853e6be8SMichael Lotz 		Device *						fChildren[USB_MAX_PORT_COUNT];
609853e6be8SMichael Lotz };
610853e6be8SMichael Lotz 
611853e6be8SMichael Lotz 
612853e6be8SMichael Lotz /*
613853e6be8SMichael Lotz  * A Transfer is allocated on the heap and passed to the Host Controller in
614853e6be8SMichael Lotz  * SubmitTransfer(). It is generated for all queued transfers. If queuing
615853e6be8SMichael Lotz  * succeds (SubmitTransfer() returns with >= B_OK) the Host Controller takes
616853e6be8SMichael Lotz  * ownership of the Transfer and will delete it as soon as it has called the
617853e6be8SMichael Lotz  * set callback function. If SubmitTransfer() failes, the calling function is
618853e6be8SMichael Lotz  * responsible for deleting the Transfer.
619853e6be8SMichael Lotz  * Also, the transfer takes ownership of the usb_request_data passed to it in
620853e6be8SMichael Lotz  * SetRequestData(), but does not take ownership of the data buffer set by
621853e6be8SMichael Lotz  * SetData().
622853e6be8SMichael Lotz  */
623853e6be8SMichael Lotz class Transfer {
624853e6be8SMichael Lotz public:
625853e6be8SMichael Lotz 									Transfer(Pipe *pipe);
626853e6be8SMichael Lotz 									~Transfer();
627853e6be8SMichael Lotz 
62819b8f8a0SMichael Lotz 		Pipe *						TransferPipe() const { return fPipe; }
629853e6be8SMichael Lotz 
630853e6be8SMichael Lotz 		void						SetRequestData(usb_request_data *data);
63119b8f8a0SMichael Lotz 		usb_request_data *			RequestData() const { return fRequestData; }
632853e6be8SMichael Lotz 
63319b8f8a0SMichael Lotz 		void						SetIsochronousData(
63419b8f8a0SMichael Lotz 										usb_isochronous_data *data);
63519b8f8a0SMichael Lotz 		usb_isochronous_data *		IsochronousData() const
63619b8f8a0SMichael Lotz 										{ return fIsochronousData; }
637853e6be8SMichael Lotz 
638853e6be8SMichael Lotz 		void						SetData(uint8 *buffer, size_t length);
63919b8f8a0SMichael Lotz 		uint8 *						Data() const
64019b8f8a0SMichael Lotz 										{ return (uint8 *)fData.iov_base; }
64119b8f8a0SMichael Lotz 		size_t						DataLength() const { return fData.iov_len; }
642853e6be8SMichael Lotz 
643853e6be8SMichael Lotz 		void						SetPhysical(bool physical);
64419b8f8a0SMichael Lotz 		bool						IsPhysical() const { return fPhysical; }
645853e6be8SMichael Lotz 
64619b8f8a0SMichael Lotz 		void						SetVector(iovec *vector,
64719b8f8a0SMichael Lotz 										size_t vectorCount);
64819b8f8a0SMichael Lotz 		iovec *						Vector() { return fVector; }
64919b8f8a0SMichael Lotz 		size_t						VectorCount() const { return fVectorCount; }
650853e6be8SMichael Lotz 		size_t						VectorLength();
651853e6be8SMichael Lotz 
65219b8f8a0SMichael Lotz 		uint16						Bandwidth() const { return fBandwidth; }
653853e6be8SMichael Lotz 
65419b8f8a0SMichael Lotz 		bool						IsFragmented() const { return fFragmented; }
655853e6be8SMichael Lotz 		void						AdvanceByFragment(size_t actualLength);
656853e6be8SMichael Lotz 
657853e6be8SMichael Lotz 		status_t					InitKernelAccess();
658853e6be8SMichael Lotz 		status_t					PrepareKernelAccess();
659853e6be8SMichael Lotz 
660853e6be8SMichael Lotz 		void						SetCallback(usb_callback_func callback,
661853e6be8SMichael Lotz 										void *cookie);
662853e6be8SMichael Lotz 
66319b8f8a0SMichael Lotz 		void						Finished(uint32 status,
66419b8f8a0SMichael Lotz 										size_t actualLength);
665853e6be8SMichael Lotz 
66619b8f8a0SMichael Lotz 		usb_id						USBID() const { return 0; }
66719b8f8a0SMichael Lotz 		const char *				TypeName() const { return "transfer"; }
668853e6be8SMichael Lotz 
669853e6be8SMichael Lotz private:
670853e6be8SMichael Lotz 		status_t					_CalculateBandwidth();
671853e6be8SMichael Lotz 
672853e6be8SMichael Lotz 		// Data that is related to the transfer
673853e6be8SMichael Lotz 		Pipe *						fPipe;
674853e6be8SMichael Lotz 		iovec						fData;
675853e6be8SMichael Lotz 		iovec *						fVector;
676853e6be8SMichael Lotz 		size_t						fVectorCount;
677853e6be8SMichael Lotz 		void *						fBaseAddress;
678853e6be8SMichael Lotz 		bool						fPhysical;
679853e6be8SMichael Lotz 		bool						fFragmented;
680853e6be8SMichael Lotz 		size_t						fActualLength;
681853e6be8SMichael Lotz 		area_id						fUserArea;
682853e6be8SMichael Lotz 		area_id						fClonedArea;
683853e6be8SMichael Lotz 
684853e6be8SMichael Lotz 		usb_callback_func			fCallback;
685853e6be8SMichael Lotz 		void *						fCallbackCookie;
686853e6be8SMichael Lotz 
687853e6be8SMichael Lotz 		// For control transfers
688853e6be8SMichael Lotz 		usb_request_data *			fRequestData;
689853e6be8SMichael Lotz 
690853e6be8SMichael Lotz 		// For isochronous transfers
691853e6be8SMichael Lotz 		usb_isochronous_data *		fIsochronousData;
692853e6be8SMichael Lotz 
693853e6be8SMichael Lotz 		// For bandwidth management.
694853e6be8SMichael Lotz 		// It contains the bandwidth necessary in microseconds
695853e6be8SMichael Lotz 		// for either isochronous, interrupt or control transfers.
696853e6be8SMichael Lotz 		// Not used for bulk transactions.
697853e6be8SMichael Lotz 		uint16						fBandwidth;
698853e6be8SMichael Lotz };
699853e6be8SMichael Lotz 
700cc9f959dSMichael Lotz #endif // _USB_PRIVATE_H
701