xref: /haiku/src/add-ons/kernel/bus_managers/usb/usb_private.h (revision 06ed40580339e21f65c24b3d210c880dcf2ec739)
1 /*
2  * Copyright 2003-2006, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Lotz <mmlr@mlotz.ch>
7  *		Niels S. Reedijk
8  */
9 #ifndef _USB_PRIVATE_H
10 #define _USB_PRIVATE_H
11 
12 #include "BeOSCompatibility.h"
13 #include "usbspec_private.h"
14 #include <lock.h>
15 #include <util/Vector.h>
16 
17 
18 #define TRACE_OUTPUT(x, y, z...) \
19 	{ \
20 		dprintf("usb %s%s %" B_PRId32 ": ", y, (x)->TypeName(), (x)->USBID()); \
21 		dprintf(z); \
22 	}
23 
24 //#define TRACE_USB
25 #ifdef TRACE_USB
26 #define TRACE(x...)					TRACE_OUTPUT(this, "", x)
27 #define TRACE_STATIC(x, y...)		TRACE_OUTPUT(x, "", y)
28 #define TRACE_MODULE(x...)			dprintf("usb " USB_MODULE_NAME ": " x)
29 #else
30 #define TRACE(x...)					/* nothing */
31 #define TRACE_STATIC(x, y...)		/* nothing */
32 #define TRACE_MODULE(x...)			/* nothing */
33 #endif
34 
35 #define TRACE_ALWAYS(x...)			TRACE_OUTPUT(this, "", x)
36 #define TRACE_ERROR(x...)			TRACE_OUTPUT(this, "error ", x)
37 #define TRACE_MODULE_ALWAYS(x...)	dprintf("usb " USB_MODULE_NAME ": " x)
38 #define TRACE_MODULE_ERROR(x...)	dprintf("usb " USB_MODULE_NAME ": " x)
39 
40 class Hub;
41 class Stack;
42 class Device;
43 class Transfer;
44 class BusManager;
45 class Pipe;
46 class ControlPipe;
47 class Object;
48 class PhysicalMemoryAllocator;
49 
50 
51 struct usb_host_controller_info {
52 	module_info info;
53 	status_t (*control)(uint32 op, void *data, size_t length);
54 	status_t (*add_to)(Stack *stack);
55 };
56 
57 
58 struct usb_driver_cookie {
59 	usb_id device;
60 	void *cookie;
61 	usb_driver_cookie *link;
62 };
63 
64 
65 struct usb_driver_info {
66 	const char *driver_name;
67 	usb_support_descriptor *support_descriptors;
68 	uint32 support_descriptor_count;
69 	const char *republish_driver_name;
70 	usb_notify_hooks notify_hooks;
71 	usb_driver_cookie *cookies;
72 	usb_driver_info *link;
73 };
74 
75 
76 struct change_item {
77 	bool added;
78 	Device *device;
79 	change_item *link;
80 };
81 
82 
83 struct rescan_item {
84 	const char *name;
85 	rescan_item *link;
86 };
87 
88 
89 typedef enum {
90 	USB_SPEED_LOWSPEED = 0,
91 	USB_SPEED_FULLSPEED,
92 	USB_SPEED_HIGHSPEED,
93 	USB_SPEED_SUPER,
94 	USB_SPEED_WIRELESS,
95 	USB_SPEED_MAX = USB_SPEED_WIRELESS
96 } usb_speed;
97 
98 
99 typedef enum {
100 	USB_CHANGE_CREATED = 0,
101 	USB_CHANGE_DESTROYED,
102 	USB_CHANGE_PIPE_POLICY_CHANGED
103 } usb_change;
104 
105 
106 #define USB_OBJECT_NONE					0x00000000
107 #define USB_OBJECT_PIPE					0x00000001
108 #define USB_OBJECT_CONTROL_PIPE			0x00000002
109 #define USB_OBJECT_INTERRUPT_PIPE		0x00000004
110 #define USB_OBJECT_BULK_PIPE			0x00000008
111 #define USB_OBJECT_ISO_PIPE				0x00000010
112 #define USB_OBJECT_INTERFACE			0x00000020
113 #define USB_OBJECT_DEVICE				0x00000040
114 #define USB_OBJECT_HUB					0x00000080
115 
116 
117 class Stack {
118 public:
119 										Stack();
120 										~Stack();
121 
122 		status_t						InitCheck();
123 
124 		bool							Lock();
125 		void							Unlock();
126 
127 		usb_id							GetUSBID(Object *object);
128 		void							PutUSBID(usb_id id);
129 		Object *						GetObject(usb_id id);
130 
131 		// only for the kernel debugger
132 		Object *						GetObjectNoLock(usb_id id) const;
133 
134 		void							AddBusManager(BusManager *bus);
135 		int32							IndexOfBusManager(BusManager *bus);
136 		BusManager *					BusManagerAt(int32 index) const;
137 
138 		status_t						AllocateChunk(void **logicalAddress,
139 											phys_addr_t *physicalAddress,
140 											size_t size);
141 		status_t						FreeChunk(void *logicalAddress,
142 											phys_addr_t physicalAddress,
143 											size_t size);
144 
145 		area_id							AllocateArea(void **logicalAddress,
146 											phys_addr_t *physicalAddress,
147 											size_t size, const char *name);
148 
149 		void							NotifyDeviceChange(Device *device,
150 											rescan_item **rescanList,
151 											bool added);
152 		void							RescanDrivers(rescan_item *rescanItem);
153 
154 		// USB API
155 		status_t						RegisterDriver(const char *driverName,
156 											const usb_support_descriptor *
157 												descriptors,
158 											size_t descriptorCount,
159 											const char *republishDriverName);
160 
161 		status_t						InstallNotify(const char *driverName,
162 											const usb_notify_hooks *hooks);
163 		status_t						UninstallNotify(const char *driverName);
164 
165 		usb_id							USBID() const { return 0; }
166 		const char *					TypeName() const { return "stack"; }
167 
168 private:
169 static	int32							ExploreThread(void *data);
170 
171 		Vector<BusManager *>			fBusManagers;
172 		thread_id						fExploreThread;
173 		bool							fFirstExploreDone;
174 		bool							fStopThreads;
175 
176 		mutex							fStackLock;
177 		mutex							fExploreLock;
178 		PhysicalMemoryAllocator *		fAllocator;
179 
180 		uint32							fObjectIndex;
181 		uint32							fObjectMaxCount;
182 		Object **						fObjectArray;
183 
184 		usb_driver_info *				fDriverList;
185 };
186 
187 
188 /*
189  * This class manages a bus. It is created by the Stack object
190  * after a host controller gives positive feedback on whether the hardware
191  * is found.
192  */
193 class BusManager {
194 public:
195 										BusManager(Stack *stack);
196 virtual									~BusManager();
197 
198 virtual	status_t						InitCheck();
199 
200 		bool							Lock();
201 		void							Unlock();
202 
203 		int8							AllocateAddress();
204 		void							FreeAddress(int8 address);
205 
206 virtual	Device *						AllocateDevice(Hub *parent,
207 											int8 hubAddress, uint8 hubPort,
208 											usb_speed speed);
209 virtual void							FreeDevice(Device *device);
210 
211 virtual	status_t						Start();
212 virtual	status_t						Stop();
213 
214 virtual	status_t						SubmitTransfer(Transfer *transfer);
215 virtual	status_t						CancelQueuedTransfers(Pipe *pipe,
216 											bool force);
217 
218 virtual	status_t						NotifyPipeChange(Pipe *pipe,
219 											usb_change change);
220 
221 		Object *						RootObject() const
222 											{ return fRootObject; }
223 
224 		Hub *							GetRootHub() const { return fRootHub; }
225 		void							SetRootHub(Hub *hub) { fRootHub = hub; }
226 
227 		usb_id							USBID() const { return fUSBID; }
228 virtual	const char *					TypeName() const = 0;
229 
230 protected:
231 		bool							fInitOK;
232 
233 private:
234 		ControlPipe *					_GetDefaultPipe(usb_speed);
235 
236 		mutex							fLock;
237 
238 		bool							fDeviceMap[128];
239 		int8							fDeviceIndex;
240 
241 		Stack *							fStack;
242 		ControlPipe *					fDefaultPipes[USB_SPEED_MAX + 1];
243 		Hub *							fRootHub;
244 		Object *						fRootObject;
245 
246 		usb_id							fUSBID;
247 };
248 
249 
250 class Object {
251 public:
252 										Object(Stack *stack, BusManager *bus);
253 										Object(Object *parent);
254 virtual									~Object();
255 
256 		Object *						Parent() const { return fParent; }
257 
258 		BusManager *					GetBusManager() const
259 											{ return fBusManager; }
260 		Stack *							GetStack() const { return fStack; }
261 
262 		usb_id							USBID() const { return fUSBID; }
263 virtual	uint32							Type() const { return USB_OBJECT_NONE; }
264 virtual	const char *					TypeName() const { return "object"; }
265 
266 		// Convenience functions for standard requests
267 virtual	status_t						SetFeature(uint16 selector);
268 virtual	status_t						ClearFeature(uint16 selector);
269 virtual	status_t						GetStatus(uint16 *status);
270 
271 private:
272 		Object *						fParent;
273 		BusManager *					fBusManager;
274 		Stack *							fStack;
275 		usb_id							fUSBID;
276 };
277 
278 
279 /*
280  * The Pipe class is the communication management between the hardware and
281  * the stack. It creates packets, manages these and performs callbacks.
282  */
283 class Pipe : public Object {
284 public:
285 		enum pipeDirection { In, Out, Default };
286 
287 										Pipe(Object *parent);
288 virtual									~Pipe();
289 
290 virtual	void							InitCommon(int8 deviceAddress,
291 											uint8 endpointAddress,
292 											usb_speed speed,
293 											pipeDirection direction,
294 											size_t maxPacketSize,
295 											uint8 interval,
296 											int8 hubAddress, uint8 hubPort);
297 
298 virtual	uint32							Type() const { return USB_OBJECT_PIPE; }
299 virtual	const char *					TypeName() const { return "pipe"; }
300 
301 		int8							DeviceAddress() const
302 											{ return fDeviceAddress; }
303 		usb_speed						Speed() const { return fSpeed; }
304 		pipeDirection					Direction() const { return fDirection; }
305 		uint8							EndpointAddress() const
306 											{ return fEndpointAddress; }
307 		size_t							MaxPacketSize() const
308 											{ return fMaxPacketSize; }
309 		uint8							Interval() const { return fInterval; }
310 
311 		// Hub port being the one-based logical port number on the hub
312 		void							SetHubInfo(int8 address, uint8 port);
313 		int8							HubAddress() const
314 											{ return fHubAddress; }
315 		uint8							HubPort() const { return fHubPort; }
316 
317 virtual	bool							DataToggle() const
318 											{ return fDataToggle; }
319 virtual	void							SetDataToggle(bool toggle)
320 											{ fDataToggle = toggle; }
321 
322 		status_t						SubmitTransfer(Transfer *transfer);
323 		status_t						CancelQueuedTransfers(bool force);
324 
325 		void							SetControllerCookie(void *cookie)
326 											{ fControllerCookie = cookie; }
327 		void *							ControllerCookie() const
328 											{ return fControllerCookie; }
329 
330 		// Convenience functions for standard requests
331 virtual	status_t						SetFeature(uint16 selector);
332 virtual	status_t						ClearFeature(uint16 selector);
333 virtual	status_t						GetStatus(uint16 *status);
334 
335 private:
336 		int8							fDeviceAddress;
337 		uint8							fEndpointAddress;
338 		pipeDirection					fDirection;
339 		usb_speed						fSpeed;
340 		size_t							fMaxPacketSize;
341 		uint8							fInterval;
342 		int8							fHubAddress;
343 		uint8							fHubPort;
344 		bool							fDataToggle;
345 		void *							fControllerCookie;
346 };
347 
348 
349 class ControlPipe : public Pipe {
350 public:
351 										ControlPipe(Object *parent);
352 virtual									~ControlPipe();
353 
354 virtual	uint32							Type() const { return USB_OBJECT_PIPE
355 											| USB_OBJECT_CONTROL_PIPE; }
356 virtual	const char *					TypeName() const
357 											{ return "control pipe"; }
358 
359 										// The data toggle is not relevant
360 										// for control transfers, as they are
361 										// always enclosed by a setup and
362 										// status packet. The toggle always
363 										// starts at 1.
364 virtual	bool							DataToggle() const { return true; }
365 virtual	void							SetDataToggle(bool toggle) {}
366 
367 		status_t						SendRequest(uint8 requestType,
368 											uint8 request, uint16 value,
369 											uint16 index, uint16 length,
370 											void *data, size_t dataLength,
371 											size_t *actualLength);
372 static	void							SendRequestCallback(void *cookie,
373 											status_t status, void *data,
374 											size_t actualLength);
375 
376 		status_t						QueueRequest(uint8 requestType,
377 											uint8 request, uint16 value,
378 											uint16 index, uint16 length,
379 											void *data, size_t dataLength,
380 											usb_callback_func callback,
381 											void *callbackCookie);
382 
383 private:
384 		mutex							fSendRequestLock;
385 		sem_id							fNotifySem;
386 		status_t						fTransferStatus;
387 		size_t							fActualLength;
388 };
389 
390 
391 class InterruptPipe : public Pipe {
392 public:
393 										InterruptPipe(Object *parent);
394 
395 virtual	uint32							Type() const { return USB_OBJECT_PIPE
396 											| USB_OBJECT_INTERRUPT_PIPE; }
397 virtual	const char *					TypeName() const
398 											{ return "interrupt pipe"; }
399 
400 		status_t						QueueInterrupt(void *data,
401 											size_t dataLength,
402 											usb_callback_func callback,
403 											void *callbackCookie);
404 };
405 
406 
407 class BulkPipe : public Pipe {
408 public:
409 										BulkPipe(Object *parent);
410 
411 virtual	void							InitCommon(int8 deviceAddress,
412 											uint8 endpointAddress,
413 											usb_speed speed,
414 											pipeDirection direction,
415 											size_t maxPacketSize,
416 											uint8 interval,
417 											int8 hubAddress, uint8 hubPort);
418 
419 virtual	uint32							Type() const { return USB_OBJECT_PIPE
420 											| USB_OBJECT_BULK_PIPE; }
421 virtual	const char *					TypeName() const { return "bulk pipe"; }
422 
423 		status_t						QueueBulk(void *data,
424 											size_t dataLength,
425 											usb_callback_func callback,
426 											void *callbackCookie);
427 		status_t						QueueBulkV(iovec *vector,
428 											size_t vectorCount,
429 											usb_callback_func callback,
430 											void *callbackCookie,
431 											bool physical);
432 };
433 
434 
435 class IsochronousPipe : public Pipe {
436 public:
437 										IsochronousPipe(Object *parent);
438 
439 virtual	uint32							Type() const { return USB_OBJECT_PIPE
440 											| USB_OBJECT_ISO_PIPE; }
441 virtual	const char *					TypeName() const { return "iso pipe"; }
442 
443 		status_t						QueueIsochronous(void *data,
444 											size_t dataLength,
445 											usb_iso_packet_descriptor *
446 												packetDescriptor,
447 											uint32 packetCount,
448 											uint32 *startingFrameNumber,
449 											uint32 flags,
450 											usb_callback_func callback,
451 											void *callbackCookie);
452 
453 		status_t						SetPipePolicy(uint8 maxQueuedPackets,
454 											uint16 maxBufferDurationMS,
455 											uint16 sampleSize);
456 		status_t						GetPipePolicy(uint8 *maxQueuedPackets,
457 											uint16 *maxBufferDurationMS,
458 											uint16 *sampleSize);
459 
460 private:
461 		uint8							fMaxQueuedPackets;
462 		uint16							fMaxBufferDuration;
463 		uint16							fSampleSize;
464 };
465 
466 
467 class Interface : public Object {
468 public:
469 										Interface(Object *parent,
470 											uint8 interfaceIndex);
471 
472 virtual	uint32							Type() const
473 											{ return USB_OBJECT_INTERFACE; }
474 virtual	const char *					TypeName() const { return "interface"; }
475 
476 		// Convenience functions for standard requests
477 virtual	status_t						SetFeature(uint16 selector);
478 virtual	status_t						ClearFeature(uint16 selector);
479 virtual	status_t						GetStatus(uint16 *status);
480 
481 private:
482 		uint8							fInterfaceIndex;
483 };
484 
485 
486 class Device : public Object {
487 public:
488 										Device(Object *parent, int8 hubAddress,
489 											uint8 hubPort,
490 											usb_device_descriptor &desc,
491 											int8 deviceAddress,
492 											usb_speed speed, bool isRootHub,
493 											void *controllerCookie = NULL);
494 virtual									~Device();
495 
496 		status_t						InitCheck();
497 
498 virtual	status_t						Changed(change_item **changeList,
499 											bool added);
500 
501 virtual	uint32							Type() const
502 											{ return USB_OBJECT_DEVICE; }
503 virtual	const char *					TypeName() const { return "device"; }
504 
505 		ControlPipe *					DefaultPipe() const
506 											{ return fDefaultPipe; }
507 
508 virtual	status_t						GetDescriptor(uint8 descriptorType,
509 											uint8 index, uint16 languageID,
510 											void *data, size_t dataLength,
511 											size_t *actualLength);
512 
513 		int8							DeviceAddress() const
514 											{ return fDeviceAddress; }
515 		const usb_device_descriptor *	DeviceDescriptor() const;
516 		usb_speed						Speed() const { return fSpeed; }
517 
518 		const usb_configuration_info *	Configuration() const;
519 		const usb_configuration_info *	ConfigurationAt(uint8 index) const;
520 		status_t						SetConfiguration(
521 											const usb_configuration_info *
522 												configuration);
523 		status_t						SetConfigurationAt(uint8 index);
524 		status_t						Unconfigure(bool atDeviceLevel);
525 
526 		status_t						SetAltInterface(
527 											const usb_interface_info *
528 												interface);
529 
530 		void							InitEndpoints(int32 interfaceIndex);
531 		void							ClearEndpoints(int32 interfaceIndex);
532 
533 virtual	status_t						ReportDevice(
534 											usb_support_descriptor *
535 												supportDescriptors,
536 											uint32 supportDescriptorCount,
537 											const usb_notify_hooks *hooks,
538 											usb_driver_cookie **cookies,
539 											bool added, bool recursive);
540 virtual	status_t						BuildDeviceName(char *string,
541 											uint32 *index, size_t bufferSize,
542 											Device *device);
543 
544 		int8							HubAddress() const
545 											{ return fHubAddress; }
546 		uint8							HubPort() const { return fHubPort; }
547 
548 		void							SetControllerCookie(void *cookie)
549 											{ fControllerCookie = cookie; }
550 		void *							ControllerCookie() const
551 											{ return fControllerCookie; }
552 
553 		// Convenience functions for standard requests
554 virtual	status_t						SetFeature(uint16 selector);
555 virtual	status_t						ClearFeature(uint16 selector);
556 virtual	status_t						GetStatus(uint16 *status);
557 
558 protected:
559 		usb_device_descriptor			fDeviceDescriptor;
560 		bool							fInitOK;
561 
562 private:
563 		bool							fAvailable;
564 		bool							fIsRootHub;
565 		usb_configuration_info *		fConfigurations;
566 		usb_configuration_info *		fCurrentConfiguration;
567 		usb_speed						fSpeed;
568 		int8							fDeviceAddress;
569 		int8							fHubAddress;
570 		uint8							fHubPort;
571 		ControlPipe *					fDefaultPipe;
572 		void *							fControllerCookie;
573 };
574 
575 
576 class Hub : public Device {
577 public:
578 										Hub(Object *parent, int8 hubAddress,
579 											uint8 hubPort,
580 											usb_device_descriptor &desc,
581 											int8 deviceAddress,
582 											usb_speed speed, bool isRootHub);
583 virtual									~Hub();
584 
585 virtual	status_t						Changed(change_item **changeList,
586 											bool added);
587 
588 virtual	uint32							Type() const { return USB_OBJECT_DEVICE
589 											| USB_OBJECT_HUB; }
590 virtual	const char *					TypeName() const { return "hub"; }
591 
592 virtual	status_t						GetDescriptor(uint8 descriptorType,
593 											uint8 index, uint16 languageID,
594 											void *data, size_t dataLength,
595 											size_t *actualLength);
596 
597 		Device *						ChildAt(uint8 index) const
598 											{ return fChildren[index]; }
599 
600 		status_t						UpdatePortStatus(uint8 index);
601 		status_t						ResetPort(uint8 index);
602 		status_t						DisablePort(uint8 index);
603 
604 		void							Explore(change_item **changeList);
605 static	void							InterruptCallback(void *cookie,
606 											status_t status, void *data,
607 											size_t actualLength);
608 
609 virtual	status_t						ReportDevice(
610 											usb_support_descriptor *
611 												supportDescriptors,
612 											uint32 supportDescriptorCount,
613 											const usb_notify_hooks *hooks,
614 											usb_driver_cookie **cookies,
615 											bool added, bool recursive);
616 virtual	status_t						BuildDeviceName(char *string,
617 											uint32 *index, size_t bufferSize,
618 											Device *device);
619 
620 private:
621 		status_t						_DebouncePort(uint8 index);
622 
623 		InterruptPipe *					fInterruptPipe;
624 		usb_hub_descriptor				fHubDescriptor;
625 
626 		usb_port_status					fInterruptStatus[USB_MAX_PORT_COUNT];
627 		usb_port_status					fPortStatus[USB_MAX_PORT_COUNT];
628 		Device *						fChildren[USB_MAX_PORT_COUNT];
629 };
630 
631 
632 /*
633  * A Transfer is allocated on the heap and passed to the Host Controller in
634  * SubmitTransfer(). It is generated for all queued transfers. If queuing
635  * succeds (SubmitTransfer() returns with >= B_OK) the Host Controller takes
636  * ownership of the Transfer and will delete it as soon as it has called the
637  * set callback function. If SubmitTransfer() failes, the calling function is
638  * responsible for deleting the Transfer.
639  * Also, the transfer takes ownership of the usb_request_data passed to it in
640  * SetRequestData(), but does not take ownership of the data buffer set by
641  * SetData().
642  */
643 class Transfer {
644 public:
645 									Transfer(Pipe *pipe);
646 									~Transfer();
647 
648 		Pipe *						TransferPipe() const { return fPipe; }
649 
650 		void						SetRequestData(usb_request_data *data);
651 		usb_request_data *			RequestData() const { return fRequestData; }
652 
653 		void						SetIsochronousData(
654 										usb_isochronous_data *data);
655 		usb_isochronous_data *		IsochronousData() const
656 										{ return fIsochronousData; }
657 
658 		void						SetData(uint8 *buffer, size_t length);
659 		uint8 *						Data() const
660 										{ return (uint8 *)fData.iov_base; }
661 		size_t						DataLength() const { return fData.iov_len; }
662 
663 		void						SetPhysical(bool physical);
664 		bool						IsPhysical() const { return fPhysical; }
665 
666 		void						SetVector(iovec *vector,
667 										size_t vectorCount);
668 		iovec *						Vector() { return fVector; }
669 		size_t						VectorCount() const { return fVectorCount; }
670 		size_t						VectorLength();
671 
672 		uint16						Bandwidth() const { return fBandwidth; }
673 
674 		bool						IsFragmented() const { return fFragmented; }
675 		void						AdvanceByFragment(size_t actualLength);
676 
677 		status_t					InitKernelAccess();
678 		status_t					PrepareKernelAccess();
679 
680 		void						SetCallback(usb_callback_func callback,
681 										void *cookie);
682 
683 		void						Finished(uint32 status,
684 										size_t actualLength);
685 
686 		usb_id						USBID() const { return 0; }
687 		const char *				TypeName() const { return "transfer"; }
688 
689 private:
690 		status_t					_CalculateBandwidth();
691 
692 		// Data that is related to the transfer
693 		Pipe *						fPipe;
694 		iovec						fData;
695 		iovec *						fVector;
696 		size_t						fVectorCount;
697 		void *						fBaseAddress;
698 		bool						fPhysical;
699 		bool						fFragmented;
700 		size_t						fActualLength;
701 		area_id						fUserArea;
702 		area_id						fClonedArea;
703 
704 		usb_callback_func			fCallback;
705 		void *						fCallbackCookie;
706 
707 		// For control transfers
708 		usb_request_data *			fRequestData;
709 
710 		// For isochronous transfers
711 		usb_isochronous_data *		fIsochronousData;
712 
713 		// For bandwidth management.
714 		// It contains the bandwidth necessary in microseconds
715 		// for either isochronous, interrupt or control transfers.
716 		// Not used for bulk transactions.
717 		uint16						fBandwidth;
718 };
719 
720 #endif // _USB_PRIVATE_H
721