xref: /haiku/src/add-ons/kernel/bus_managers/usb/usb_private.h (revision d8b4cfc9829119291e68cfe0152c239fd8850b3d)
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 	{ \
20*d8b4cfc9SRene Gollent 		dprintf("usb %s%s %" B_PRId32 ": ", 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,
93d6e4f54fSJérôme Duval 	USB_SPEED_SUPER,
94319a3798SJérôme Duval 	USB_SPEED_WIRELESS,
95319a3798SJérôme Duval 	USB_SPEED_MAX = USB_SPEED_WIRELESS
96853e6be8SMichael Lotz } usb_speed;
97853e6be8SMichael Lotz 
98853e6be8SMichael Lotz 
99853e6be8SMichael Lotz typedef enum {
100853e6be8SMichael Lotz 	USB_CHANGE_CREATED = 0,
101853e6be8SMichael Lotz 	USB_CHANGE_DESTROYED,
102853e6be8SMichael Lotz 	USB_CHANGE_PIPE_POLICY_CHANGED
103853e6be8SMichael Lotz } usb_change;
104853e6be8SMichael Lotz 
105853e6be8SMichael Lotz 
106853e6be8SMichael Lotz #define USB_OBJECT_NONE					0x00000000
107853e6be8SMichael Lotz #define USB_OBJECT_PIPE					0x00000001
108853e6be8SMichael Lotz #define USB_OBJECT_CONTROL_PIPE			0x00000002
109853e6be8SMichael Lotz #define USB_OBJECT_INTERRUPT_PIPE		0x00000004
110853e6be8SMichael Lotz #define USB_OBJECT_BULK_PIPE			0x00000008
111853e6be8SMichael Lotz #define USB_OBJECT_ISO_PIPE				0x00000010
112853e6be8SMichael Lotz #define USB_OBJECT_INTERFACE			0x00000020
113853e6be8SMichael Lotz #define USB_OBJECT_DEVICE				0x00000040
114853e6be8SMichael Lotz #define USB_OBJECT_HUB					0x00000080
115853e6be8SMichael Lotz 
116853e6be8SMichael Lotz 
117853e6be8SMichael Lotz class Stack {
118853e6be8SMichael Lotz public:
119853e6be8SMichael Lotz 										Stack();
120853e6be8SMichael Lotz 										~Stack();
121853e6be8SMichael Lotz 
122853e6be8SMichael Lotz 		status_t						InitCheck();
123853e6be8SMichael Lotz 
124853e6be8SMichael Lotz 		bool							Lock();
125853e6be8SMichael Lotz 		void							Unlock();
126853e6be8SMichael Lotz 
127853e6be8SMichael Lotz 		usb_id							GetUSBID(Object *object);
128853e6be8SMichael Lotz 		void							PutUSBID(usb_id id);
129853e6be8SMichael Lotz 		Object *						GetObject(usb_id id);
130853e6be8SMichael Lotz 
131853e6be8SMichael Lotz 		// only for the kernel debugger
13238fc536eSMichael Lotz 		Object *						GetObjectNoLock(usb_id id) const;
133853e6be8SMichael Lotz 
134853e6be8SMichael Lotz 		void							AddBusManager(BusManager *bus);
135853e6be8SMichael Lotz 		int32							IndexOfBusManager(BusManager *bus);
13638fc536eSMichael Lotz 		BusManager *					BusManagerAt(int32 index) const;
137853e6be8SMichael Lotz 
138853e6be8SMichael Lotz 		status_t						AllocateChunk(void **logicalAddress,
139*d8b4cfc9SRene Gollent 											phys_addr_t *physicalAddress,
14019b8f8a0SMichael Lotz 											size_t size);
141853e6be8SMichael Lotz 		status_t						FreeChunk(void *logicalAddress,
142*d8b4cfc9SRene Gollent 											phys_addr_t physicalAddress,
143*d8b4cfc9SRene Gollent 											size_t size);
144853e6be8SMichael Lotz 
145853e6be8SMichael Lotz 		area_id							AllocateArea(void **logicalAddress,
146*d8b4cfc9SRene Gollent 											phys_addr_t *physicalAddress,
147853e6be8SMichael Lotz 											size_t size, const char *name);
148853e6be8SMichael Lotz 
149853e6be8SMichael Lotz 		void							NotifyDeviceChange(Device *device,
150853e6be8SMichael Lotz 											rescan_item **rescanList,
151853e6be8SMichael Lotz 											bool added);
152853e6be8SMichael Lotz 		void							RescanDrivers(rescan_item *rescanItem);
153853e6be8SMichael Lotz 
154853e6be8SMichael Lotz 		// USB API
155853e6be8SMichael Lotz 		status_t						RegisterDriver(const char *driverName,
15619b8f8a0SMichael Lotz 											const usb_support_descriptor *
15719b8f8a0SMichael Lotz 												descriptors,
158853e6be8SMichael Lotz 											size_t descriptorCount,
159853e6be8SMichael Lotz 											const char *republishDriverName);
160853e6be8SMichael Lotz 
161853e6be8SMichael Lotz 		status_t						InstallNotify(const char *driverName,
162853e6be8SMichael Lotz 											const usb_notify_hooks *hooks);
163853e6be8SMichael Lotz 		status_t						UninstallNotify(const char *driverName);
164853e6be8SMichael Lotz 
16519b8f8a0SMichael Lotz 		usb_id							USBID() const { return 0; }
16619b8f8a0SMichael Lotz 		const char *					TypeName() const { return "stack"; }
167853e6be8SMichael Lotz 
168853e6be8SMichael Lotz private:
169853e6be8SMichael Lotz static	int32							ExploreThread(void *data);
170853e6be8SMichael Lotz 
171853e6be8SMichael Lotz 		Vector<BusManager *>			fBusManagers;
172853e6be8SMichael Lotz 		thread_id						fExploreThread;
173853e6be8SMichael Lotz 		bool							fFirstExploreDone;
174853e6be8SMichael Lotz 		bool							fStopThreads;
175853e6be8SMichael Lotz 
176853e6be8SMichael Lotz 		mutex							fStackLock;
177853e6be8SMichael Lotz 		mutex							fExploreLock;
178853e6be8SMichael Lotz 		PhysicalMemoryAllocator *		fAllocator;
179853e6be8SMichael Lotz 
180853e6be8SMichael Lotz 		uint32							fObjectIndex;
181853e6be8SMichael Lotz 		uint32							fObjectMaxCount;
182853e6be8SMichael Lotz 		Object **						fObjectArray;
183853e6be8SMichael Lotz 
184853e6be8SMichael Lotz 		usb_driver_info *				fDriverList;
185853e6be8SMichael Lotz };
186853e6be8SMichael Lotz 
187853e6be8SMichael Lotz 
188853e6be8SMichael Lotz /*
189853e6be8SMichael Lotz  * This class manages a bus. It is created by the Stack object
190853e6be8SMichael Lotz  * after a host controller gives positive feedback on whether the hardware
191853e6be8SMichael Lotz  * is found.
192853e6be8SMichael Lotz  */
193853e6be8SMichael Lotz class BusManager {
194853e6be8SMichael Lotz public:
195853e6be8SMichael Lotz 										BusManager(Stack *stack);
196853e6be8SMichael Lotz virtual									~BusManager();
197853e6be8SMichael Lotz 
198853e6be8SMichael Lotz virtual	status_t						InitCheck();
199853e6be8SMichael Lotz 
200853e6be8SMichael Lotz 		bool							Lock();
201853e6be8SMichael Lotz 		void							Unlock();
202853e6be8SMichael Lotz 
203853e6be8SMichael Lotz 		int8							AllocateAddress();
204853e6be8SMichael Lotz 		void							FreeAddress(int8 address);
205853e6be8SMichael Lotz 
206319a3798SJérôme Duval virtual	Device *						AllocateDevice(Hub *parent,
207853e6be8SMichael Lotz 											int8 hubAddress, uint8 hubPort,
208853e6be8SMichael Lotz 											usb_speed speed);
209319a3798SJérôme Duval virtual void							FreeDevice(Device *device);
210853e6be8SMichael Lotz 
211853e6be8SMichael Lotz virtual	status_t						Start();
212853e6be8SMichael Lotz virtual	status_t						Stop();
213853e6be8SMichael Lotz 
214853e6be8SMichael Lotz virtual	status_t						SubmitTransfer(Transfer *transfer);
215853e6be8SMichael Lotz virtual	status_t						CancelQueuedTransfers(Pipe *pipe,
216853e6be8SMichael Lotz 											bool force);
217853e6be8SMichael Lotz 
218853e6be8SMichael Lotz virtual	status_t						NotifyPipeChange(Pipe *pipe,
219853e6be8SMichael Lotz 											usb_change change);
220853e6be8SMichael Lotz 
22119b8f8a0SMichael Lotz 		Object *						RootObject() const
22219b8f8a0SMichael Lotz 											{ return fRootObject; }
223853e6be8SMichael Lotz 
22419b8f8a0SMichael Lotz 		Hub *							GetRootHub() const { return fRootHub; }
22519b8f8a0SMichael Lotz 		void							SetRootHub(Hub *hub) { fRootHub = hub; }
226853e6be8SMichael Lotz 
22719b8f8a0SMichael Lotz 		usb_id							USBID() const { return fUSBID; }
22838fc536eSMichael Lotz virtual	const char *					TypeName() const = 0;
229853e6be8SMichael Lotz 
230853e6be8SMichael Lotz protected:
231853e6be8SMichael Lotz 		bool							fInitOK;
232853e6be8SMichael Lotz 
233853e6be8SMichael Lotz private:
234853e6be8SMichael Lotz 		ControlPipe *					_GetDefaultPipe(usb_speed);
235853e6be8SMichael Lotz 
236853e6be8SMichael Lotz 		mutex							fLock;
237853e6be8SMichael Lotz 
238853e6be8SMichael Lotz 		bool							fDeviceMap[128];
239853e6be8SMichael Lotz 		int8							fDeviceIndex;
240853e6be8SMichael Lotz 
241853e6be8SMichael Lotz 		Stack *							fStack;
242853e6be8SMichael Lotz 		ControlPipe *					fDefaultPipes[USB_SPEED_MAX + 1];
243853e6be8SMichael Lotz 		Hub *							fRootHub;
244853e6be8SMichael Lotz 		Object *						fRootObject;
24519b8f8a0SMichael Lotz 
24619b8f8a0SMichael Lotz 		usb_id							fUSBID;
247853e6be8SMichael Lotz };
248853e6be8SMichael Lotz 
249853e6be8SMichael Lotz 
250853e6be8SMichael Lotz class Object {
251853e6be8SMichael Lotz public:
252853e6be8SMichael Lotz 										Object(Stack *stack, BusManager *bus);
253853e6be8SMichael Lotz 										Object(Object *parent);
254853e6be8SMichael Lotz virtual									~Object();
255853e6be8SMichael Lotz 
25619b8f8a0SMichael Lotz 		Object *						Parent() const { return fParent; }
257853e6be8SMichael Lotz 
25819b8f8a0SMichael Lotz 		BusManager *					GetBusManager() const
25919b8f8a0SMichael Lotz 											{ return fBusManager; }
26019b8f8a0SMichael Lotz 		Stack *							GetStack() const { return fStack; }
261853e6be8SMichael Lotz 
26219b8f8a0SMichael Lotz 		usb_id							USBID() const { return fUSBID; }
26319b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_NONE; }
26419b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "object"; }
265853e6be8SMichael Lotz 
266853e6be8SMichael Lotz 		// Convenience functions for standard requests
267853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
268853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
269853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
270853e6be8SMichael Lotz 
271853e6be8SMichael Lotz private:
272853e6be8SMichael Lotz 		Object *						fParent;
273853e6be8SMichael Lotz 		BusManager *					fBusManager;
274853e6be8SMichael Lotz 		Stack *							fStack;
275853e6be8SMichael Lotz 		usb_id							fUSBID;
276853e6be8SMichael Lotz };
277853e6be8SMichael Lotz 
278853e6be8SMichael Lotz 
279853e6be8SMichael Lotz /*
280853e6be8SMichael Lotz  * The Pipe class is the communication management between the hardware and
281853e6be8SMichael Lotz  * the stack. It creates packets, manages these and performs callbacks.
282853e6be8SMichael Lotz  */
283853e6be8SMichael Lotz class Pipe : public Object {
284853e6be8SMichael Lotz public:
285853e6be8SMichael Lotz 		enum pipeDirection { In, Out, Default };
286853e6be8SMichael Lotz 
287853e6be8SMichael Lotz 										Pipe(Object *parent);
288853e6be8SMichael Lotz virtual									~Pipe();
289853e6be8SMichael Lotz 
290853e6be8SMichael Lotz 		void							InitCommon(int8 deviceAddress,
291853e6be8SMichael Lotz 											uint8 endpointAddress,
292853e6be8SMichael Lotz 											usb_speed speed,
293853e6be8SMichael Lotz 											pipeDirection direction,
294853e6be8SMichael Lotz 											size_t maxPacketSize,
295853e6be8SMichael Lotz 											uint8 interval,
296853e6be8SMichael Lotz 											int8 hubAddress, uint8 hubPort);
297853e6be8SMichael Lotz 
29819b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE; }
29919b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "pipe"; }
300853e6be8SMichael Lotz 
30119b8f8a0SMichael Lotz 		int8							DeviceAddress() const
30219b8f8a0SMichael Lotz 											{ return fDeviceAddress; }
30319b8f8a0SMichael Lotz 		usb_speed						Speed() const { return fSpeed; }
30419b8f8a0SMichael Lotz 		pipeDirection					Direction() const { return fDirection; }
30519b8f8a0SMichael Lotz 		uint8							EndpointAddress() const
30619b8f8a0SMichael Lotz 											{ return fEndpointAddress; }
30719b8f8a0SMichael Lotz 		size_t							MaxPacketSize() const
30819b8f8a0SMichael Lotz 											{ return fMaxPacketSize; }
30919b8f8a0SMichael Lotz 		uint8							Interval() const { return fInterval; }
310853e6be8SMichael Lotz 
311853e6be8SMichael Lotz 		// Hub port being the one-based logical port number on the hub
312853e6be8SMichael Lotz 		void							SetHubInfo(int8 address, uint8 port);
31319b8f8a0SMichael Lotz 		int8							HubAddress() const
31419b8f8a0SMichael Lotz 											{ return fHubAddress; }
31519b8f8a0SMichael Lotz 		uint8							HubPort() const { return fHubPort; }
316853e6be8SMichael Lotz 
31719b8f8a0SMichael Lotz virtual	bool							DataToggle() const
31819b8f8a0SMichael Lotz 											{ return fDataToggle; }
31919b8f8a0SMichael Lotz virtual	void							SetDataToggle(bool toggle)
32019b8f8a0SMichael Lotz 											{ fDataToggle = toggle; }
321853e6be8SMichael Lotz 
322853e6be8SMichael Lotz 		status_t						SubmitTransfer(Transfer *transfer);
323853e6be8SMichael Lotz 		status_t						CancelQueuedTransfers(bool force);
324853e6be8SMichael Lotz 
32519b8f8a0SMichael Lotz 		void							SetControllerCookie(void *cookie)
32619b8f8a0SMichael Lotz 											{ fControllerCookie = cookie; }
32719b8f8a0SMichael Lotz 		void *							ControllerCookie() const
32819b8f8a0SMichael Lotz 											{ return fControllerCookie; }
329853e6be8SMichael Lotz 
330853e6be8SMichael Lotz 		// Convenience functions for standard requests
331853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
332853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
333853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
334853e6be8SMichael Lotz 
335853e6be8SMichael Lotz private:
336853e6be8SMichael Lotz 		int8							fDeviceAddress;
337853e6be8SMichael Lotz 		uint8							fEndpointAddress;
338853e6be8SMichael Lotz 		pipeDirection					fDirection;
339853e6be8SMichael Lotz 		usb_speed						fSpeed;
340853e6be8SMichael Lotz 		size_t							fMaxPacketSize;
341853e6be8SMichael Lotz 		uint8							fInterval;
342853e6be8SMichael Lotz 		int8							fHubAddress;
343853e6be8SMichael Lotz 		uint8							fHubPort;
344853e6be8SMichael Lotz 		bool							fDataToggle;
345853e6be8SMichael Lotz 		void *							fControllerCookie;
346853e6be8SMichael Lotz };
347853e6be8SMichael Lotz 
348853e6be8SMichael Lotz 
349853e6be8SMichael Lotz class ControlPipe : public Pipe {
350853e6be8SMichael Lotz public:
351853e6be8SMichael Lotz 										ControlPipe(Object *parent);
352853e6be8SMichael Lotz virtual									~ControlPipe();
353853e6be8SMichael Lotz 
35419b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
35519b8f8a0SMichael Lotz 											| USB_OBJECT_CONTROL_PIPE; }
35619b8f8a0SMichael Lotz virtual	const char *					TypeName() const
35719b8f8a0SMichael Lotz 											{ return "control pipe"; }
358853e6be8SMichael Lotz 
359853e6be8SMichael Lotz 										// The data toggle is not relevant
360853e6be8SMichael Lotz 										// for control transfers, as they are
361853e6be8SMichael Lotz 										// always enclosed by a setup and
362853e6be8SMichael Lotz 										// status packet. The toggle always
363853e6be8SMichael Lotz 										// starts at 1.
36419b8f8a0SMichael Lotz virtual	bool							DataToggle() const { return true; }
36538fc536eSMichael Lotz virtual	void							SetDataToggle(bool toggle) {}
366853e6be8SMichael Lotz 
367853e6be8SMichael Lotz 		status_t						SendRequest(uint8 requestType,
368853e6be8SMichael Lotz 											uint8 request, uint16 value,
369853e6be8SMichael Lotz 											uint16 index, uint16 length,
370853e6be8SMichael Lotz 											void *data, size_t dataLength,
371853e6be8SMichael Lotz 											size_t *actualLength);
372853e6be8SMichael Lotz static	void							SendRequestCallback(void *cookie,
373853e6be8SMichael Lotz 											status_t status, void *data,
374853e6be8SMichael Lotz 											size_t actualLength);
375853e6be8SMichael Lotz 
376853e6be8SMichael Lotz 		status_t						QueueRequest(uint8 requestType,
377853e6be8SMichael Lotz 											uint8 request, uint16 value,
378853e6be8SMichael Lotz 											uint16 index, uint16 length,
379853e6be8SMichael Lotz 											void *data, size_t dataLength,
380853e6be8SMichael Lotz 											usb_callback_func callback,
381853e6be8SMichael Lotz 											void *callbackCookie);
382853e6be8SMichael Lotz 
383853e6be8SMichael Lotz private:
384853e6be8SMichael Lotz 		mutex							fSendRequestLock;
385853e6be8SMichael Lotz 		sem_id							fNotifySem;
386853e6be8SMichael Lotz 		status_t						fTransferStatus;
387853e6be8SMichael Lotz 		size_t							fActualLength;
388853e6be8SMichael Lotz };
389853e6be8SMichael Lotz 
390853e6be8SMichael Lotz 
391853e6be8SMichael Lotz class InterruptPipe : public Pipe {
392853e6be8SMichael Lotz public:
393853e6be8SMichael Lotz 										InterruptPipe(Object *parent);
394853e6be8SMichael Lotz 
39519b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
39619b8f8a0SMichael Lotz 											| USB_OBJECT_INTERRUPT_PIPE; }
39719b8f8a0SMichael Lotz virtual	const char *					TypeName() const
39819b8f8a0SMichael Lotz 											{ return "interrupt pipe"; }
399853e6be8SMichael Lotz 
400853e6be8SMichael Lotz 		status_t						QueueInterrupt(void *data,
401853e6be8SMichael Lotz 											size_t dataLength,
402853e6be8SMichael Lotz 											usb_callback_func callback,
403853e6be8SMichael Lotz 											void *callbackCookie);
404853e6be8SMichael Lotz };
405853e6be8SMichael Lotz 
406853e6be8SMichael Lotz 
407853e6be8SMichael Lotz class BulkPipe : public Pipe {
408853e6be8SMichael Lotz public:
409853e6be8SMichael Lotz 										BulkPipe(Object *parent);
410853e6be8SMichael Lotz 
41119b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
41219b8f8a0SMichael Lotz 											| USB_OBJECT_BULK_PIPE; }
41319b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "bulk pipe"; }
414853e6be8SMichael Lotz 
415853e6be8SMichael Lotz 		status_t						QueueBulk(void *data,
416853e6be8SMichael Lotz 											size_t dataLength,
417853e6be8SMichael Lotz 											usb_callback_func callback,
418853e6be8SMichael Lotz 											void *callbackCookie);
419853e6be8SMichael Lotz 		status_t						QueueBulkV(iovec *vector,
420853e6be8SMichael Lotz 											size_t vectorCount,
421853e6be8SMichael Lotz 											usb_callback_func callback,
422853e6be8SMichael Lotz 											void *callbackCookie,
423853e6be8SMichael Lotz 											bool physical);
424853e6be8SMichael Lotz };
425853e6be8SMichael Lotz 
426853e6be8SMichael Lotz 
427853e6be8SMichael Lotz class IsochronousPipe : public Pipe {
428853e6be8SMichael Lotz public:
429853e6be8SMichael Lotz 										IsochronousPipe(Object *parent);
430853e6be8SMichael Lotz 
43119b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_PIPE
43219b8f8a0SMichael Lotz 											| USB_OBJECT_ISO_PIPE; }
43319b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "iso pipe"; }
434853e6be8SMichael Lotz 
435853e6be8SMichael Lotz 		status_t						QueueIsochronous(void *data,
436853e6be8SMichael Lotz 											size_t dataLength,
43719b8f8a0SMichael Lotz 											usb_iso_packet_descriptor *
43819b8f8a0SMichael Lotz 												packetDescriptor,
439853e6be8SMichael Lotz 											uint32 packetCount,
440853e6be8SMichael Lotz 											uint32 *startingFrameNumber,
441853e6be8SMichael Lotz 											uint32 flags,
442853e6be8SMichael Lotz 											usb_callback_func callback,
443853e6be8SMichael Lotz 											void *callbackCookie);
444853e6be8SMichael Lotz 
445853e6be8SMichael Lotz 		status_t						SetPipePolicy(uint8 maxQueuedPackets,
446853e6be8SMichael Lotz 											uint16 maxBufferDurationMS,
447853e6be8SMichael Lotz 											uint16 sampleSize);
448853e6be8SMichael Lotz 		status_t						GetPipePolicy(uint8 *maxQueuedPackets,
449853e6be8SMichael Lotz 											uint16 *maxBufferDurationMS,
450853e6be8SMichael Lotz 											uint16 *sampleSize);
451853e6be8SMichael Lotz 
452853e6be8SMichael Lotz private:
453853e6be8SMichael Lotz 		uint8							fMaxQueuedPackets;
454853e6be8SMichael Lotz 		uint16							fMaxBufferDuration;
455853e6be8SMichael Lotz 		uint16							fSampleSize;
456853e6be8SMichael Lotz };
457853e6be8SMichael Lotz 
458853e6be8SMichael Lotz 
459853e6be8SMichael Lotz class Interface : public Object {
460853e6be8SMichael Lotz public:
461853e6be8SMichael Lotz 										Interface(Object *parent,
462853e6be8SMichael Lotz 											uint8 interfaceIndex);
463853e6be8SMichael Lotz 
46419b8f8a0SMichael Lotz virtual	uint32							Type() const
46519b8f8a0SMichael Lotz 											{ return USB_OBJECT_INTERFACE; }
46619b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "interface"; }
467853e6be8SMichael Lotz 
468853e6be8SMichael Lotz 		// Convenience functions for standard requests
469853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
470853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
471853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
472853e6be8SMichael Lotz 
473853e6be8SMichael Lotz private:
474853e6be8SMichael Lotz 		uint8							fInterfaceIndex;
475853e6be8SMichael Lotz };
476853e6be8SMichael Lotz 
477853e6be8SMichael Lotz 
478853e6be8SMichael Lotz class Device : public Object {
479853e6be8SMichael Lotz public:
480853e6be8SMichael Lotz 										Device(Object *parent, int8 hubAddress,
481853e6be8SMichael Lotz 											uint8 hubPort,
482853e6be8SMichael Lotz 											usb_device_descriptor &desc,
483853e6be8SMichael Lotz 											int8 deviceAddress,
4842b31b4a8SJérôme Duval 											usb_speed speed, bool isRootHub,
4852b31b4a8SJérôme Duval 											void *controllerCookie = NULL);
486853e6be8SMichael Lotz virtual									~Device();
487853e6be8SMichael Lotz 
488853e6be8SMichael Lotz 		status_t						InitCheck();
489853e6be8SMichael Lotz 
490853e6be8SMichael Lotz virtual	status_t						Changed(change_item **changeList,
491853e6be8SMichael Lotz 											bool added);
492853e6be8SMichael Lotz 
49319b8f8a0SMichael Lotz virtual	uint32							Type() const
49419b8f8a0SMichael Lotz 											{ return USB_OBJECT_DEVICE; }
49519b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "device"; }
496853e6be8SMichael Lotz 
49719b8f8a0SMichael Lotz 		ControlPipe *					DefaultPipe() const
49819b8f8a0SMichael Lotz 											{ return fDefaultPipe; }
499853e6be8SMichael Lotz 
500853e6be8SMichael Lotz virtual	status_t						GetDescriptor(uint8 descriptorType,
501853e6be8SMichael Lotz 											uint8 index, uint16 languageID,
502853e6be8SMichael Lotz 											void *data, size_t dataLength,
503853e6be8SMichael Lotz 											size_t *actualLength);
504853e6be8SMichael Lotz 
50519b8f8a0SMichael Lotz 		int8							DeviceAddress() const
50619b8f8a0SMichael Lotz 											{ return fDeviceAddress; }
507853e6be8SMichael Lotz 		const usb_device_descriptor *	DeviceDescriptor() const;
50819b8f8a0SMichael Lotz 		usb_speed						Speed() const { return fSpeed; }
509853e6be8SMichael Lotz 
510853e6be8SMichael Lotz 		const usb_configuration_info *	Configuration() const;
511853e6be8SMichael Lotz 		const usb_configuration_info *	ConfigurationAt(uint8 index) const;
51219b8f8a0SMichael Lotz 		status_t						SetConfiguration(
51319b8f8a0SMichael Lotz 											const usb_configuration_info *
51419b8f8a0SMichael Lotz 												configuration);
515853e6be8SMichael Lotz 		status_t						SetConfigurationAt(uint8 index);
516853e6be8SMichael Lotz 		status_t						Unconfigure(bool atDeviceLevel);
517853e6be8SMichael Lotz 
51819b8f8a0SMichael Lotz 		status_t						SetAltInterface(
51919b8f8a0SMichael Lotz 											const usb_interface_info *
52019b8f8a0SMichael Lotz 												interface);
521853e6be8SMichael Lotz 
522853e6be8SMichael Lotz 		void							InitEndpoints(int32 interfaceIndex);
523853e6be8SMichael Lotz 		void							ClearEndpoints(int32 interfaceIndex);
524853e6be8SMichael Lotz 
525853e6be8SMichael Lotz virtual	status_t						ReportDevice(
52619b8f8a0SMichael Lotz 											usb_support_descriptor *
52719b8f8a0SMichael Lotz 												supportDescriptors,
528853e6be8SMichael Lotz 											uint32 supportDescriptorCount,
529853e6be8SMichael Lotz 											const usb_notify_hooks *hooks,
530853e6be8SMichael Lotz 											usb_driver_cookie **cookies,
531853e6be8SMichael Lotz 											bool added, bool recursive);
532853e6be8SMichael Lotz virtual	status_t						BuildDeviceName(char *string,
533853e6be8SMichael Lotz 											uint32 *index, size_t bufferSize,
534853e6be8SMichael Lotz 											Device *device);
535853e6be8SMichael Lotz 
53619b8f8a0SMichael Lotz 		int8							HubAddress() const
53719b8f8a0SMichael Lotz 											{ return fHubAddress; }
53819b8f8a0SMichael Lotz 		uint8							HubPort() const { return fHubPort; }
539853e6be8SMichael Lotz 
5402b31b4a8SJérôme Duval 		void							SetControllerCookie(void *cookie)
5412b31b4a8SJérôme Duval 											{ fControllerCookie = cookie; }
5422b31b4a8SJérôme Duval 		void *							ControllerCookie() const
5432b31b4a8SJérôme Duval 											{ return fControllerCookie; }
5442b31b4a8SJérôme Duval 
545853e6be8SMichael Lotz 		// Convenience functions for standard requests
546853e6be8SMichael Lotz virtual	status_t						SetFeature(uint16 selector);
547853e6be8SMichael Lotz virtual	status_t						ClearFeature(uint16 selector);
548853e6be8SMichael Lotz virtual	status_t						GetStatus(uint16 *status);
549853e6be8SMichael Lotz 
550853e6be8SMichael Lotz protected:
551853e6be8SMichael Lotz 		usb_device_descriptor			fDeviceDescriptor;
552853e6be8SMichael Lotz 		bool							fInitOK;
553853e6be8SMichael Lotz 
554853e6be8SMichael Lotz private:
555853e6be8SMichael Lotz 		bool							fAvailable;
556853e6be8SMichael Lotz 		bool							fIsRootHub;
557853e6be8SMichael Lotz 		usb_configuration_info *		fConfigurations;
558853e6be8SMichael Lotz 		usb_configuration_info *		fCurrentConfiguration;
559853e6be8SMichael Lotz 		usb_speed						fSpeed;
560853e6be8SMichael Lotz 		int8							fDeviceAddress;
561853e6be8SMichael Lotz 		int8							fHubAddress;
562853e6be8SMichael Lotz 		uint8							fHubPort;
563853e6be8SMichael Lotz 		ControlPipe *					fDefaultPipe;
5642b31b4a8SJérôme Duval 		void *							fControllerCookie;
565853e6be8SMichael Lotz };
566853e6be8SMichael Lotz 
567853e6be8SMichael Lotz 
568853e6be8SMichael Lotz class Hub : public Device {
569853e6be8SMichael Lotz public:
570853e6be8SMichael Lotz 										Hub(Object *parent, int8 hubAddress,
571853e6be8SMichael Lotz 											uint8 hubPort,
572853e6be8SMichael Lotz 											usb_device_descriptor &desc,
573853e6be8SMichael Lotz 											int8 deviceAddress,
574853e6be8SMichael Lotz 											usb_speed speed, bool isRootHub);
575853e6be8SMichael Lotz virtual									~Hub();
576853e6be8SMichael Lotz 
577853e6be8SMichael Lotz virtual	status_t						Changed(change_item **changeList,
578853e6be8SMichael Lotz 											bool added);
579853e6be8SMichael Lotz 
58019b8f8a0SMichael Lotz virtual	uint32							Type() const { return USB_OBJECT_DEVICE
58119b8f8a0SMichael Lotz 											| USB_OBJECT_HUB; }
58219b8f8a0SMichael Lotz virtual	const char *					TypeName() const { return "hub"; }
583853e6be8SMichael Lotz 
584853e6be8SMichael Lotz virtual	status_t						GetDescriptor(uint8 descriptorType,
585853e6be8SMichael Lotz 											uint8 index, uint16 languageID,
586853e6be8SMichael Lotz 											void *data, size_t dataLength,
587853e6be8SMichael Lotz 											size_t *actualLength);
588853e6be8SMichael Lotz 
58919b8f8a0SMichael Lotz 		Device *						ChildAt(uint8 index) const
59019b8f8a0SMichael Lotz 											{ return fChildren[index]; }
591853e6be8SMichael Lotz 
592853e6be8SMichael Lotz 		status_t						UpdatePortStatus(uint8 index);
593853e6be8SMichael Lotz 		status_t						ResetPort(uint8 index);
594853e6be8SMichael Lotz 		status_t						DisablePort(uint8 index);
595853e6be8SMichael Lotz 
596853e6be8SMichael Lotz 		void							Explore(change_item **changeList);
597853e6be8SMichael Lotz static	void							InterruptCallback(void *cookie,
598853e6be8SMichael Lotz 											status_t status, void *data,
599853e6be8SMichael Lotz 											size_t actualLength);
600853e6be8SMichael Lotz 
601853e6be8SMichael Lotz virtual	status_t						ReportDevice(
60219b8f8a0SMichael Lotz 											usb_support_descriptor *
60319b8f8a0SMichael Lotz 												supportDescriptors,
604853e6be8SMichael Lotz 											uint32 supportDescriptorCount,
605853e6be8SMichael Lotz 											const usb_notify_hooks *hooks,
606853e6be8SMichael Lotz 											usb_driver_cookie **cookies,
607853e6be8SMichael Lotz 											bool added, bool recursive);
608853e6be8SMichael Lotz virtual	status_t						BuildDeviceName(char *string,
609853e6be8SMichael Lotz 											uint32 *index, size_t bufferSize,
610853e6be8SMichael Lotz 											Device *device);
611853e6be8SMichael Lotz 
612853e6be8SMichael Lotz private:
613853e6be8SMichael Lotz 		InterruptPipe *					fInterruptPipe;
614853e6be8SMichael Lotz 		usb_hub_descriptor				fHubDescriptor;
615853e6be8SMichael Lotz 
616853e6be8SMichael Lotz 		usb_port_status					fInterruptStatus[USB_MAX_PORT_COUNT];
617853e6be8SMichael Lotz 		usb_port_status					fPortStatus[USB_MAX_PORT_COUNT];
618853e6be8SMichael Lotz 		Device *						fChildren[USB_MAX_PORT_COUNT];
619853e6be8SMichael Lotz };
620853e6be8SMichael Lotz 
621853e6be8SMichael Lotz 
622853e6be8SMichael Lotz /*
623853e6be8SMichael Lotz  * A Transfer is allocated on the heap and passed to the Host Controller in
624853e6be8SMichael Lotz  * SubmitTransfer(). It is generated for all queued transfers. If queuing
625853e6be8SMichael Lotz  * succeds (SubmitTransfer() returns with >= B_OK) the Host Controller takes
626853e6be8SMichael Lotz  * ownership of the Transfer and will delete it as soon as it has called the
627853e6be8SMichael Lotz  * set callback function. If SubmitTransfer() failes, the calling function is
628853e6be8SMichael Lotz  * responsible for deleting the Transfer.
629853e6be8SMichael Lotz  * Also, the transfer takes ownership of the usb_request_data passed to it in
630853e6be8SMichael Lotz  * SetRequestData(), but does not take ownership of the data buffer set by
631853e6be8SMichael Lotz  * SetData().
632853e6be8SMichael Lotz  */
633853e6be8SMichael Lotz class Transfer {
634853e6be8SMichael Lotz public:
635853e6be8SMichael Lotz 									Transfer(Pipe *pipe);
636853e6be8SMichael Lotz 									~Transfer();
637853e6be8SMichael Lotz 
63819b8f8a0SMichael Lotz 		Pipe *						TransferPipe() const { return fPipe; }
639853e6be8SMichael Lotz 
640853e6be8SMichael Lotz 		void						SetRequestData(usb_request_data *data);
64119b8f8a0SMichael Lotz 		usb_request_data *			RequestData() const { return fRequestData; }
642853e6be8SMichael Lotz 
64319b8f8a0SMichael Lotz 		void						SetIsochronousData(
64419b8f8a0SMichael Lotz 										usb_isochronous_data *data);
64519b8f8a0SMichael Lotz 		usb_isochronous_data *		IsochronousData() const
64619b8f8a0SMichael Lotz 										{ return fIsochronousData; }
647853e6be8SMichael Lotz 
648853e6be8SMichael Lotz 		void						SetData(uint8 *buffer, size_t length);
64919b8f8a0SMichael Lotz 		uint8 *						Data() const
65019b8f8a0SMichael Lotz 										{ return (uint8 *)fData.iov_base; }
65119b8f8a0SMichael Lotz 		size_t						DataLength() const { return fData.iov_len; }
652853e6be8SMichael Lotz 
653853e6be8SMichael Lotz 		void						SetPhysical(bool physical);
65419b8f8a0SMichael Lotz 		bool						IsPhysical() const { return fPhysical; }
655853e6be8SMichael Lotz 
65619b8f8a0SMichael Lotz 		void						SetVector(iovec *vector,
65719b8f8a0SMichael Lotz 										size_t vectorCount);
65819b8f8a0SMichael Lotz 		iovec *						Vector() { return fVector; }
65919b8f8a0SMichael Lotz 		size_t						VectorCount() const { return fVectorCount; }
660853e6be8SMichael Lotz 		size_t						VectorLength();
661853e6be8SMichael Lotz 
66219b8f8a0SMichael Lotz 		uint16						Bandwidth() const { return fBandwidth; }
663853e6be8SMichael Lotz 
66419b8f8a0SMichael Lotz 		bool						IsFragmented() const { return fFragmented; }
665853e6be8SMichael Lotz 		void						AdvanceByFragment(size_t actualLength);
666853e6be8SMichael Lotz 
667853e6be8SMichael Lotz 		status_t					InitKernelAccess();
668853e6be8SMichael Lotz 		status_t					PrepareKernelAccess();
669853e6be8SMichael Lotz 
670853e6be8SMichael Lotz 		void						SetCallback(usb_callback_func callback,
671853e6be8SMichael Lotz 										void *cookie);
672853e6be8SMichael Lotz 
67319b8f8a0SMichael Lotz 		void						Finished(uint32 status,
67419b8f8a0SMichael Lotz 										size_t actualLength);
675853e6be8SMichael Lotz 
67619b8f8a0SMichael Lotz 		usb_id						USBID() const { return 0; }
67719b8f8a0SMichael Lotz 		const char *				TypeName() const { return "transfer"; }
678853e6be8SMichael Lotz 
679853e6be8SMichael Lotz private:
680853e6be8SMichael Lotz 		status_t					_CalculateBandwidth();
681853e6be8SMichael Lotz 
682853e6be8SMichael Lotz 		// Data that is related to the transfer
683853e6be8SMichael Lotz 		Pipe *						fPipe;
684853e6be8SMichael Lotz 		iovec						fData;
685853e6be8SMichael Lotz 		iovec *						fVector;
686853e6be8SMichael Lotz 		size_t						fVectorCount;
687853e6be8SMichael Lotz 		void *						fBaseAddress;
688853e6be8SMichael Lotz 		bool						fPhysical;
689853e6be8SMichael Lotz 		bool						fFragmented;
690853e6be8SMichael Lotz 		size_t						fActualLength;
691853e6be8SMichael Lotz 		area_id						fUserArea;
692853e6be8SMichael Lotz 		area_id						fClonedArea;
693853e6be8SMichael Lotz 
694853e6be8SMichael Lotz 		usb_callback_func			fCallback;
695853e6be8SMichael Lotz 		void *						fCallbackCookie;
696853e6be8SMichael Lotz 
697853e6be8SMichael Lotz 		// For control transfers
698853e6be8SMichael Lotz 		usb_request_data *			fRequestData;
699853e6be8SMichael Lotz 
700853e6be8SMichael Lotz 		// For isochronous transfers
701853e6be8SMichael Lotz 		usb_isochronous_data *		fIsochronousData;
702853e6be8SMichael Lotz 
703853e6be8SMichael Lotz 		// For bandwidth management.
704853e6be8SMichael Lotz 		// It contains the bandwidth necessary in microseconds
705853e6be8SMichael Lotz 		// for either isochronous, interrupt or control transfers.
706853e6be8SMichael Lotz 		// Not used for bulk transactions.
707853e6be8SMichael Lotz 		uint16						fBandwidth;
708853e6be8SMichael Lotz };
709853e6be8SMichael Lotz 
710cc9f959dSMichael Lotz #endif // _USB_PRIVATE_H
711