xref: /haiku/src/add-ons/kernel/busses/usb/uhci.cpp (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
1 /*
2  * Copyright 2004-2011, 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  *		Salvatore Benedetto <salvatore.benedetto@gmail.com>
9  */
10 
11 #include <module.h>
12 #include <PCI.h>
13 #include <USB3.h>
14 #include <KernelExport.h>
15 
16 #include "uhci.h"
17 
18 #define USB_MODULE_NAME "uhci"
19 
20 pci_module_info *UHCI::sPCIModule = NULL;
21 
22 static int32 sDebuggerCommandAdded = 0;
23 
24 
25 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
26 
27 
28 class DebugTransfer : public Transfer {
29 public:
30 	DebugTransfer(Pipe *pipe)
31 		:
32 		Transfer(pipe)
33 	{
34 	}
35 
36 	uhci_td	*firstDescriptor;
37 	uhci_qh	*transferQueue;
38 };
39 
40 
41 /*!	The function is an evil hack to allow <tt> <kdebug>usb_keyboard </tt> to
42 	execute transfers.
43 	When invoked the first time, a new transfer is started, each time the
44 	function is called afterwards, it is checked whether the transfer is already
45 	completed. If called with argv[1] == "cancel" the function cancels a
46 	possibly pending transfer.
47 */
48 static int
49 debug_process_transfer(int argc, char **argv)
50 {
51 	Pipe *pipe = (Pipe *)get_debug_variable("_usbPipe", 0);
52 	if (pipe == NULL)
53 		return 2;
54 
55 	// check if we have a UHCI bus at all
56 	if (pipe->GetBusManager()->TypeName()[0] != 'u')
57 		return 3;
58 
59 	uint8 *data = (uint8 *)get_debug_variable("_usbTransferData", 0);
60 	if (data == NULL)
61 		return 4;
62 
63 	size_t length = (size_t)get_debug_variable("_usbTransferLength", 0);
64 	if (length == 0)
65 		return 5;
66 
67 	static uint8 transferBuffer[sizeof(DebugTransfer)]
68 		__attribute__((aligned(16)));
69 	static DebugTransfer* transfer;
70 
71 	UHCI *bus = (UHCI *)pipe->GetBusManager();
72 
73 	if (argc > 1 && strcmp(argv[1], "cancel") == 0) {
74 		if (transfer != NULL) {
75 			bus->CancelDebugTransfer(transfer);
76 			transfer = NULL;
77 		}
78 		return 0;
79 	}
80 
81 	if (transfer != NULL) {
82 		bool stillPending;
83 		status_t error = bus->CheckDebugTransfer(transfer, stillPending);
84 		if (!stillPending)
85 			transfer = NULL;
86 
87 		return error == B_OK ? 0 : 6;
88 	}
89 
90 	transfer = new(transferBuffer) DebugTransfer(pipe);
91 	transfer->SetData(data, length);
92 
93 	if (bus->StartDebugTransfer(transfer) != B_OK) {
94 		transfer = NULL;
95 		return 7;
96 	}
97 
98 	return 0;
99 }
100 #endif
101 
102 
103 static int32
104 uhci_std_ops(int32 op, ...)
105 {
106 	switch (op) {
107 		case B_MODULE_INIT:
108 			TRACE_MODULE("init module\n");
109 			return B_OK;
110 		case B_MODULE_UNINIT:
111 			TRACE_MODULE("uninit module\n");
112 			break;
113 		default:
114 			return EINVAL;
115 	}
116 
117 	return B_OK;
118 }
119 
120 
121 usb_host_controller_info uhci_module = {
122 	{
123 		"busses/usb/uhci",
124 		0,
125 		uhci_std_ops
126 	},
127 	NULL,
128 	UHCI::AddTo
129 };
130 
131 
132 module_info *modules[] = {
133 	(module_info *)&uhci_module,
134 	NULL
135 };
136 
137 
138 //
139 // #pragma mark -
140 //
141 
142 
143 #ifdef TRACE_USB
144 
145 void
146 print_descriptor_chain(uhci_td *descriptor)
147 {
148 	while (descriptor) {
149 		dprintf("ph: 0x%08lx; lp: 0x%08lx; vf: %s; q: %s; t: %s; st: 0x%08lx; to: 0x%08lx\n",
150 			descriptor->this_phy & 0xffffffff, descriptor->link_phy & 0xfffffff0,
151 			descriptor->link_phy & 0x4 ? "y" : "n",
152 			descriptor->link_phy & 0x2 ? "qh" : "td",
153 			descriptor->link_phy & 0x1 ? "y" : "n",
154 			descriptor->status, descriptor->token);
155 
156 		if (descriptor->link_phy & TD_TERMINATE)
157 			break;
158 
159 		descriptor = (uhci_td *)descriptor->link_log;
160 	}
161 }
162 
163 #endif // TRACE_USB
164 
165 
166 //
167 // #pragma mark -
168 //
169 
170 
171 Queue::Queue(Stack *stack)
172 {
173 	fStack = stack;
174 
175 	mutex_init(&fLock, "uhci queue lock");
176 
177 	void *physicalAddress;
178 	fStatus = fStack->AllocateChunk((void **)&fQueueHead, &physicalAddress,
179 		sizeof(uhci_qh));
180 	if (fStatus < B_OK)
181 		return;
182 
183 	fQueueHead->this_phy = (addr_t)physicalAddress;
184 	fQueueHead->element_phy = QH_TERMINATE;
185 
186 	fStrayDescriptor = NULL;
187 	fQueueTop = NULL;
188 }
189 
190 
191 Queue::~Queue()
192 {
193 	Lock();
194 	mutex_destroy(&fLock);
195 
196 	fStack->FreeChunk(fQueueHead, (void *)fQueueHead->this_phy, sizeof(uhci_qh));
197 
198 	if (fStrayDescriptor)
199 		fStack->FreeChunk(fStrayDescriptor, (void *)fStrayDescriptor->this_phy,
200 			sizeof(uhci_td));
201 }
202 
203 
204 status_t
205 Queue::InitCheck()
206 {
207 	return fStatus;
208 }
209 
210 
211 bool
212 Queue::Lock()
213 {
214 	return (mutex_lock(&fLock) == B_OK);
215 }
216 
217 
218 void
219 Queue::Unlock()
220 {
221 	mutex_unlock(&fLock);
222 }
223 
224 
225 status_t
226 Queue::LinkTo(Queue *other)
227 {
228 	if (!other)
229 		return B_BAD_VALUE;
230 
231 	if (!Lock())
232 		return B_ERROR;
233 
234 	fQueueHead->link_phy = other->fQueueHead->this_phy | QH_NEXT_IS_QH;
235 	fQueueHead->link_log = other->fQueueHead;
236 	Unlock();
237 
238 	return B_OK;
239 }
240 
241 
242 status_t
243 Queue::TerminateByStrayDescriptor()
244 {
245 	// According to the *BSD USB sources, there needs to be a stray transfer
246 	// descriptor in order to get some chipset to work nicely (like the PIIX).
247 	void *physicalAddress;
248 	status_t result = fStack->AllocateChunk((void **)&fStrayDescriptor,
249 		&physicalAddress, sizeof(uhci_td));
250 	if (result < B_OK) {
251 		TRACE_ERROR("failed to allocate a stray transfer descriptor\n");
252 		return result;
253 	}
254 
255 	fStrayDescriptor->status = 0;
256 	fStrayDescriptor->this_phy = (addr_t)physicalAddress;
257 	fStrayDescriptor->link_phy = TD_TERMINATE;
258 	fStrayDescriptor->link_log = NULL;
259 	fStrayDescriptor->buffer_phy = 0;
260 	fStrayDescriptor->buffer_log = NULL;
261 	fStrayDescriptor->buffer_size = 0;
262 	fStrayDescriptor->token = TD_TOKEN_NULL_DATA
263 		| (0x7f << TD_TOKEN_DEVADDR_SHIFT) | TD_TOKEN_IN;
264 
265 	if (!Lock()) {
266 		fStack->FreeChunk(fStrayDescriptor, (void *)fStrayDescriptor->this_phy,
267 			sizeof(uhci_td));
268 		return B_ERROR;
269 	}
270 
271 	fQueueHead->link_phy = fStrayDescriptor->this_phy;
272 	fQueueHead->link_log = fStrayDescriptor;
273 	Unlock();
274 
275 	return B_OK;
276 }
277 
278 
279 status_t
280 Queue::AppendTransfer(uhci_qh *transfer, bool lock)
281 {
282 	if (lock && !Lock())
283 		return B_ERROR;
284 
285 	transfer->link_log = NULL;
286 	transfer->link_phy = fQueueHead->link_phy;
287 
288 	if (!fQueueTop) {
289 		// the list is empty, make this the first element
290 		fQueueTop = transfer;
291 		fQueueHead->element_phy = transfer->this_phy | QH_NEXT_IS_QH;
292 	} else {
293 		// append the transfer queue to the list
294 		uhci_qh *element = fQueueTop;
295 		while (element && element->link_log)
296 			element = (uhci_qh *)element->link_log;
297 
298 		element->link_log = transfer;
299 		element->link_phy = transfer->this_phy | QH_NEXT_IS_QH;
300 	}
301 
302 	if (lock)
303 		Unlock();
304 	return B_OK;
305 }
306 
307 
308 status_t
309 Queue::RemoveTransfer(uhci_qh *transfer, bool lock)
310 {
311 	if (lock && !Lock())
312 		return B_ERROR;
313 
314 	if (fQueueTop == transfer) {
315 		// this was the top element
316 		fQueueTop = (uhci_qh *)transfer->link_log;
317 		if (!fQueueTop) {
318 			// this was the only element, terminate this queue
319 			fQueueHead->element_phy = QH_TERMINATE;
320 		} else {
321 			// there are elements left, adjust the element pointer
322 			fQueueHead->element_phy = transfer->link_phy;
323 		}
324 
325 		if (lock)
326 			Unlock();
327 		return B_OK;
328 	} else {
329 		uhci_qh *element = fQueueTop;
330 		while (element) {
331 			if (element->link_log == transfer) {
332 				element->link_log = transfer->link_log;
333 				element->link_phy = transfer->link_phy;
334 				if (lock)
335 					Unlock();
336 				return B_OK;
337 			}
338 
339 			element = (uhci_qh *)element->link_log;
340 		}
341 	}
342 
343 	if (lock)
344 		Unlock();
345 	return B_BAD_VALUE;
346 }
347 
348 
349 addr_t
350 Queue::PhysicalAddress()
351 {
352 	return fQueueHead->this_phy;
353 }
354 
355 
356 void
357 Queue::PrintToStream()
358 {
359 #ifdef TRACE_USB
360 	TRACE("queue:\n");
361 	dprintf("link phy: 0x%08lx; link type: %s; terminate: %s\n", fQueueHead->link_phy & 0xfff0, fQueueHead->link_phy & 0x0002 ? "QH" : "TD", fQueueHead->link_phy & 0x0001 ? "yes" : "no");
362 	dprintf("elem phy: 0x%08lx; elem type: %s; terminate: %s\n", fQueueHead->element_phy & 0xfff0, fQueueHead->element_phy & 0x0002 ? "QH" : "TD", fQueueHead->element_phy & 0x0001 ? "yes" : "no");
363 #endif
364 }
365 
366 
367 //
368 // #pragma mark -
369 //
370 
371 
372 UHCI::UHCI(pci_info *info, Stack *stack)
373 	:	BusManager(stack),
374 		fPCIInfo(info),
375 		fStack(stack),
376 		fEnabledInterrupts(0),
377 		fFrameArea(-1),
378 		fFrameList(NULL),
379 		fFrameBandwidth(NULL),
380 		fFirstIsochronousDescriptor(NULL),
381 		fLastIsochronousDescriptor(NULL),
382 		fQueueCount(0),
383 		fQueues(NULL),
384 		fFirstTransfer(NULL),
385 		fLastTransfer(NULL),
386 		fFinishTransfersSem(-1),
387 		fFinishThread(-1),
388 		fStopThreads(false),
389 		fProcessingPipe(NULL),
390 		fFreeList(NULL),
391 		fCleanupThread(-1),
392 		fCleanupSem(-1),
393 		fCleanupCount(0),
394 		fFirstIsochronousTransfer(NULL),
395 		fLastIsochronousTransfer(NULL),
396 		fFinishIsochronousTransfersSem(-1),
397 		fFinishIsochronousThread(-1),
398 		fRootHub(NULL),
399 		fRootHubAddress(0),
400 		fPortResetChange(0)
401 {
402 	// Create a lock for the isochronous transfer list
403 	mutex_init(&fIsochronousLock, "UHCI isochronous lock");
404 
405 	if (!fInitOK) {
406 		TRACE_ERROR("bus manager failed to init\n");
407 		return;
408 	}
409 
410 	TRACE("constructing new UHCI host controller driver\n");
411 	fInitOK = false;
412 
413 	fRegisterBase = sPCIModule->read_pci_config(fPCIInfo->bus,
414 		fPCIInfo->device, fPCIInfo->function, PCI_memory_base, 4);
415 	fRegisterBase &= PCI_address_io_mask;
416 	TRACE("iospace offset: 0x%08lx\n", fRegisterBase);
417 
418 	if (fRegisterBase == 0) {
419 		fRegisterBase = fPCIInfo->u.h0.base_registers[0];
420 		TRACE_ALWAYS("register base: 0x%08lx\n", fRegisterBase);
421 	}
422 
423 	// enable pci address access
424 	uint16 command = PCI_command_io | PCI_command_master | PCI_command_memory;
425 	command |= sPCIModule->read_pci_config(fPCIInfo->bus, fPCIInfo->device,
426 		fPCIInfo->function, PCI_command, 2);
427 
428 	sPCIModule->write_pci_config(fPCIInfo->bus, fPCIInfo->device,
429 		fPCIInfo->function, PCI_command, 2, command);
430 
431 	// disable interrupts
432 	WriteReg16(UHCI_USBINTR, 0);
433 
434 	// make sure we gain control of the UHCI controller instead of the BIOS
435 	sPCIModule->write_pci_config(fPCIInfo->bus, fPCIInfo->device,
436 		fPCIInfo->function, PCI_LEGSUP, 2, PCI_LEGSUP_USBPIRQDEN
437 		| PCI_LEGSUP_CLEAR_SMI);
438 
439 	// do a global and host reset
440 	GlobalReset();
441 	if (ControllerReset() < B_OK) {
442 		TRACE_ERROR("host failed to reset\n");
443 		return;
444 	}
445 
446 	// Setup the frame list
447 	void *physicalAddress;
448 	fFrameArea = fStack->AllocateArea((void **)&fFrameList,
449 		(void **)&physicalAddress, 4096, "USB UHCI framelist");
450 
451 	if (fFrameArea < B_OK) {
452 		TRACE_ERROR("unable to create an area for the frame pointer list\n");
453 		return;
454 	}
455 
456 	// Set base pointer and reset frame number
457 	WriteReg32(UHCI_FRBASEADD, (uint32)physicalAddress);
458 	WriteReg16(UHCI_FRNUM, 0);
459 
460 	// Set the max packet size for bandwidth reclamation to 64 bytes
461 	WriteReg16(UHCI_USBCMD, ReadReg16(UHCI_USBCMD) | UHCI_USBCMD_MAXP);
462 
463 	// we will create four queues:
464 	// 0: interrupt transfers
465 	// 1: low speed control transfers
466 	// 2: full speed control transfers
467 	// 3: bulk transfers
468 	// 4: debug queue
469 	// TODO: 4: bandwidth reclamation queue
470 	fQueueCount = 5;
471 	fQueues = new(std::nothrow) Queue *[fQueueCount];
472 	if (!fQueues) {
473 		delete_area(fFrameArea);
474 		return;
475 	}
476 
477 	for (int32 i = 0; i < fQueueCount; i++) {
478 		fQueues[i] = new(std::nothrow) Queue(fStack);
479 		if (!fQueues[i] || fQueues[i]->InitCheck() < B_OK) {
480 			TRACE_ERROR("cannot create queues\n");
481 			delete_area(fFrameArea);
482 			return;
483 		}
484 
485 		if (i > 0)
486 			fQueues[i - 1]->LinkTo(fQueues[i]);
487 	}
488 
489 	// Make sure the last queue terminates
490 	fQueues[fQueueCount - 1]->TerminateByStrayDescriptor();
491 
492 	// Create the array that will keep bandwidth information
493 	fFrameBandwidth = new(std::nothrow) uint16[NUMBER_OF_FRAMES];
494 
495 	// Create lists for managing isochronous transfer descriptors
496 	fFirstIsochronousDescriptor = new(std::nothrow) uhci_td *[NUMBER_OF_FRAMES];
497 	if (!fFirstIsochronousDescriptor) {
498 		TRACE_ERROR("faild to allocate memory for first isochronous descriptor\n");
499 		return;
500 	}
501 
502 	fLastIsochronousDescriptor = new(std::nothrow) uhci_td *[NUMBER_OF_FRAMES];
503 	if (!fLastIsochronousDescriptor) {
504 		TRACE_ERROR("failed to allocate memory for last isochronous descriptor\n");
505 		delete [] fFirstIsochronousDescriptor;
506 		return;
507 	}
508 
509 	for (int32 i = 0; i < NUMBER_OF_FRAMES; i++) {
510 		fFrameList[i] =	fQueues[UHCI_INTERRUPT_QUEUE]->PhysicalAddress()
511 			| FRAMELIST_NEXT_IS_QH;
512 		fFrameBandwidth[i] = MAX_AVAILABLE_BANDWIDTH;
513 		fFirstIsochronousDescriptor[i] = NULL;
514 		fLastIsochronousDescriptor[i] = NULL;
515 	}
516 
517 	// Create semaphore the finisher and cleanup threads will wait for
518 	fFinishTransfersSem = create_sem(0, "UHCI Finish Transfers");
519 	if (fFinishTransfersSem < B_OK) {
520 		TRACE_ERROR("failed to create finisher semaphore\n");
521 		return;
522 	}
523 
524 	fCleanupSem = create_sem(0, "UHCI Cleanup");
525 	if (fCleanupSem < B_OK) {
526 		TRACE_ERROR("failed to create cleanup semaphore\n");
527 		return;
528 	}
529 
530 	// Create the finisher service and cleanup threads
531 	fFinishThread = spawn_kernel_thread(FinishThread,
532 		"uhci finish thread", B_URGENT_DISPLAY_PRIORITY, (void *)this);
533 	resume_thread(fFinishThread);
534 
535 	fCleanupThread = spawn_kernel_thread(CleanupThread,
536 		"uhci cleanup thread", B_NORMAL_PRIORITY, (void *)this);
537 	resume_thread(fCleanupThread);
538 
539 	// Create semaphore the isochronous finisher thread will wait for
540 	fFinishIsochronousTransfersSem = create_sem(0,
541 		"UHCI Isochronous Finish Transfers");
542 	if (fFinishIsochronousTransfersSem < B_OK) {
543 		TRACE_ERROR("failed to create isochronous finisher semaphore\n");
544 		return;
545 	}
546 
547 	// Create the isochronous finisher service thread
548 	fFinishIsochronousThread = spawn_kernel_thread(FinishIsochronousThread,
549 		"uhci isochronous finish thread", B_URGENT_DISPLAY_PRIORITY,
550 		(void *)this);
551 	resume_thread(fFinishIsochronousThread);
552 
553 	// Install the interrupt handler
554 	TRACE("installing interrupt handler\n");
555 	install_io_interrupt_handler(fPCIInfo->u.h0.interrupt_line,
556 		InterruptHandler, (void *)this, 0);
557 
558 	// Enable interrupts
559 	fEnabledInterrupts = UHCI_USBSTS_USBINT | UHCI_USBSTS_ERRINT
560 		| UHCI_USBSTS_HOSTERR | UHCI_USBSTS_HCPRERR | UHCI_USBSTS_HCHALT;
561 	WriteReg16(UHCI_USBINTR, UHCI_USBINTR_CRC | UHCI_USBINTR_IOC
562 		| UHCI_USBINTR_SHORT);
563 
564 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
565 	if (atomic_add(&sDebuggerCommandAdded, 1) == 0) {
566 		add_debugger_command("uhci_process_transfer",
567 			&debug_process_transfer,
568 			"Processes a USB transfer with the given variables");
569 	}
570 #endif
571 
572 	TRACE("UHCI host controller driver constructed\n");
573 	fInitOK = true;
574 }
575 
576 
577 UHCI::~UHCI()
578 {
579 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
580 	if (atomic_add(&sDebuggerCommandAdded, -1) == 1) {
581 		remove_debugger_command("uhci_process_transfer",
582 			&debug_process_transfer);
583 	}
584 #endif
585 
586 	int32 result = 0;
587 	fStopThreads = true;
588 	delete_sem(fFinishTransfersSem);
589 	delete_sem(fCleanupSem);
590 	delete_sem(fFinishIsochronousTransfersSem);
591 	wait_for_thread(fFinishThread, &result);
592 	wait_for_thread(fCleanupThread, &result);
593 	wait_for_thread(fFinishIsochronousThread, &result);
594 
595 	LockIsochronous();
596 	isochronous_transfer_data *isoTransfer = fFirstIsochronousTransfer;
597 	while (isoTransfer) {
598 		isochronous_transfer_data *next = isoTransfer->link;
599 		delete isoTransfer;
600 		isoTransfer = next;
601 	}
602 	mutex_destroy(&fIsochronousLock);
603 
604 	Lock();
605 	transfer_data *transfer = fFirstTransfer;
606 	while (transfer) {
607 		transfer->transfer->Finished(B_CANCELED, 0);
608 		delete transfer->transfer;
609 
610 		transfer_data *next = transfer->link;
611 		delete transfer;
612 		transfer = next;
613 	}
614 
615 	for (int32 i = 0; i < fQueueCount; i++)
616 		delete fQueues[i];
617 
618 	delete [] fQueues;
619 	delete [] fFrameBandwidth;
620 	delete [] fFirstIsochronousDescriptor;
621 	delete [] fLastIsochronousDescriptor;
622 	delete fRootHub;
623 	delete_area(fFrameArea);
624 
625 	put_module(B_PCI_MODULE_NAME);
626 	Unlock();
627 }
628 
629 
630 status_t
631 UHCI::Start()
632 {
633 	// Start the host controller, then start the Busmanager
634 	TRACE("starting UHCI BusManager\n");
635 	TRACE("usbcmd reg 0x%04x, usbsts reg 0x%04x\n",
636 		ReadReg16(UHCI_USBCMD), ReadReg16(UHCI_USBSTS));
637 
638 	// Set the run bit in the command register
639 	WriteReg16(UHCI_USBCMD, ReadReg16(UHCI_USBCMD) | UHCI_USBCMD_RS);
640 
641 	bool running = false;
642 	for (int32 i = 0; i < 10; i++) {
643 		uint16 status = ReadReg16(UHCI_USBSTS);
644 		TRACE("current loop %ld, status 0x%04x\n", i, status);
645 
646 		if (status & UHCI_USBSTS_HCHALT)
647 			snooze(10000);
648 		else {
649 			running = true;
650 			break;
651 		}
652 	}
653 
654 	if (!running) {
655 		TRACE_ERROR("controller won't start running\n");
656 		return B_ERROR;
657 	}
658 
659 	fRootHubAddress = AllocateAddress();
660 	fRootHub = new(std::nothrow) UHCIRootHub(RootObject(), fRootHubAddress);
661 	if (!fRootHub) {
662 		TRACE_ERROR("no memory to allocate root hub\n");
663 		return B_NO_MEMORY;
664 	}
665 
666 	if (fRootHub->InitCheck() < B_OK) {
667 		TRACE_ERROR("root hub failed init check\n");
668 		delete fRootHub;
669 		return B_ERROR;
670 	}
671 
672 	SetRootHub(fRootHub);
673 
674 	TRACE("controller is started. status: %u curframe: %u\n",
675 		ReadReg16(UHCI_USBSTS), ReadReg16(UHCI_FRNUM));
676 	TRACE_ALWAYS("successfully started the controller\n");
677 	return BusManager::Start();
678 }
679 
680 
681 status_t
682 UHCI::SubmitTransfer(Transfer *transfer)
683 {
684 	// Short circuit the root hub
685 	Pipe *pipe = transfer->TransferPipe();
686 	if (pipe->DeviceAddress() == fRootHubAddress)
687 		return fRootHub->ProcessTransfer(this, transfer);
688 
689 	TRACE("submit transfer called for device %d\n", pipe->DeviceAddress());
690 	if (pipe->Type() & USB_OBJECT_CONTROL_PIPE)
691 		return SubmitRequest(transfer);
692 
693 	// Process isochronous transfers
694 	if (pipe->Type() & USB_OBJECT_ISO_PIPE)
695 		return SubmitIsochronous(transfer);
696 
697 	uhci_td *firstDescriptor = NULL;
698 	uhci_qh *transferQueue = NULL;
699 	status_t result = CreateFilledTransfer(transfer, &firstDescriptor,
700 		&transferQueue);
701 	if (result < B_OK)
702 		return result;
703 
704 	Queue *queue = NULL;
705 	if (pipe->Type() & USB_OBJECT_INTERRUPT_PIPE)
706 		queue = fQueues[UHCI_INTERRUPT_QUEUE];
707 	else
708 		queue = fQueues[UHCI_BULK_QUEUE];
709 
710 	bool directionIn = (pipe->Direction() == Pipe::In);
711 	result = AddPendingTransfer(transfer, queue, transferQueue,
712 		firstDescriptor, firstDescriptor, directionIn);
713 	if (result < B_OK) {
714 		TRACE_ERROR("failed to add pending transfer\n");
715 		FreeDescriptorChain(firstDescriptor);
716 		FreeTransferQueue(transferQueue);
717 		return result;
718 	}
719 
720 	queue->AppendTransfer(transferQueue);
721 	return B_OK;
722 }
723 
724 
725 status_t
726 UHCI::StartDebugTransfer(DebugTransfer *transfer)
727 {
728 	transfer->firstDescriptor = NULL;
729 	transfer->transferQueue = NULL;
730 	status_t result = CreateFilledTransfer(transfer, &transfer->firstDescriptor,
731 		&transfer->transferQueue);
732 	if (result < B_OK)
733 		return result;
734 
735 	fQueues[UHCI_DEBUG_QUEUE]->AppendTransfer(transfer->transferQueue, false);
736 
737 	return B_OK;
738 }
739 
740 
741 status_t
742 UHCI::CheckDebugTransfer(DebugTransfer *transfer, bool &_stillPending)
743 {
744 	bool transferOK = false;
745 	bool transferError = false;
746 	uhci_td *descriptor = transfer->firstDescriptor;
747 
748 	while (descriptor) {
749 		uint32 status = descriptor->status;
750 		if (status & TD_STATUS_ACTIVE)
751 			break;
752 
753 		if (status & TD_ERROR_MASK) {
754 			transferError = true;
755 			break;
756 		}
757 
758 		if ((descriptor->link_phy & TD_TERMINATE)
759 			|| uhci_td_actual_length(descriptor)
760 				< uhci_td_maximum_length(descriptor)) {
761 			transferOK = true;
762 			break;
763 		}
764 
765 		descriptor = (uhci_td *)descriptor->link_log;
766 	}
767 
768 	if (!transferOK && !transferError) {
769 		spin(200);
770 		_stillPending = true;
771 		return B_OK;
772 	}
773 
774 	if (transferOK) {
775 		uint8 lastDataToggle = 0;
776 		if (transfer->TransferPipe()->Direction() == Pipe::In) {
777 			// data to read out
778 			iovec *vector = transfer->Vector();
779 			size_t vectorCount = transfer->VectorCount();
780 
781 			ReadDescriptorChain(transfer->firstDescriptor,
782 				vector, vectorCount, &lastDataToggle);
783 		} else {
784 			// read the actual length that was sent
785 			ReadActualLength(transfer->firstDescriptor, &lastDataToggle);
786 		}
787 
788 		transfer->TransferPipe()->SetDataToggle(lastDataToggle == 0);
789 	}
790 
791 	fQueues[UHCI_DEBUG_QUEUE]->RemoveTransfer(transfer->transferQueue, false);
792 	FreeDescriptorChain(transfer->firstDescriptor);
793 	FreeTransferQueue(transfer->transferQueue);
794 	_stillPending = false;
795 	return transferOK ? B_OK : B_IO_ERROR;
796 }
797 
798 
799 void
800 UHCI::CancelDebugTransfer(DebugTransfer *transfer)
801 {
802 	// clear the active bit so the descriptors are canceled
803 	uhci_td *descriptor = transfer->firstDescriptor;
804 	while (descriptor) {
805 		descriptor->status &= ~TD_STATUS_ACTIVE;
806 		descriptor = (uhci_td *)descriptor->link_log;
807 	}
808 
809 	transfer->Finished(B_CANCELED, 0);
810 
811 	// dequeue and free resources
812 	fQueues[UHCI_DEBUG_QUEUE]->RemoveTransfer(transfer->transferQueue, false);
813 	FreeDescriptorChain(transfer->firstDescriptor);
814 	FreeTransferQueue(transfer->transferQueue);
815 	// TODO: [bonefish] The Free*() calls cause "PMA: provided address resulted
816 	// in invalid index" to be printed, so apparently something is not right.
817 	// Though I have not clue what. This is the same cleanup code as in
818 	// CheckDebugTransfer() that should undo the CreateFilledTransfer() from
819 	// StartDebugTransfer().
820 }
821 
822 
823 status_t
824 UHCI::CancelQueuedTransfers(Pipe *pipe, bool force)
825 {
826 	if (pipe->Type() & USB_OBJECT_ISO_PIPE)
827 		return CancelQueuedIsochronousTransfers(pipe, force);
828 
829 	if (!Lock())
830 		return B_ERROR;
831 
832 	struct transfer_entry {
833 		Transfer *			transfer;
834 		transfer_entry *	next;
835 	};
836 
837 	transfer_entry *list = NULL;
838 	transfer_data *current = fFirstTransfer;
839 	while (current) {
840 		if (current->transfer && current->transfer->TransferPipe() == pipe) {
841 			// clear the active bit so the descriptors are canceled
842 			uhci_td *descriptor = current->first_descriptor;
843 			while (descriptor) {
844 				descriptor->status &= ~TD_STATUS_ACTIVE;
845 				descriptor = (uhci_td *)descriptor->link_log;
846 			}
847 
848 			if (!force) {
849 				// if the transfer is canceled by force, the one causing the
850 				// cancel is probably not the one who initiated the transfer
851 				// and the callback is likely not safe anymore
852 				transfer_entry *entry
853 					= (transfer_entry *)malloc(sizeof(transfer_entry));
854 				if (entry != NULL) {
855 					entry->transfer = current->transfer;
856 					current->transfer = NULL;
857 					entry->next = list;
858 					list = entry;
859 				}
860 			}
861 
862 			current->canceled = true;
863 		}
864 		current = current->link;
865 	}
866 
867 	Unlock();
868 
869 	while (list != NULL) {
870 		transfer_entry *next = list->next;
871 		list->transfer->Finished(B_CANCELED, 0);
872 		delete list->transfer;
873 		free(list);
874 		list = next;
875 	}
876 
877 	// wait for any transfers that might have made it before canceling
878 	while (fProcessingPipe == pipe)
879 		snooze(1000);
880 
881 	// notify the finisher so it can clean up the canceled transfers
882 	release_sem_etc(fFinishTransfersSem, 1, B_DO_NOT_RESCHEDULE);
883 	return B_OK;
884 }
885 
886 
887 status_t
888 UHCI::CancelQueuedIsochronousTransfers(Pipe *pipe, bool force)
889 {
890 	isochronous_transfer_data *current = fFirstIsochronousTransfer;
891 
892 	while (current) {
893 		if (current->transfer->TransferPipe() == pipe) {
894 			int32 packetCount
895 				= current->transfer->IsochronousData()->packet_count;
896 			// Set the active bit off on every descriptor in order to prevent
897 			// the controller from processing them. Then set off the is_active
898 			// field of the transfer in order to make the finisher thread skip
899 			// the transfer. The FinishIsochronousThread will do the rest.
900 			for (int32 i = 0; i < packetCount; i++)
901 				current->descriptors[i]->status &= ~TD_STATUS_ACTIVE;
902 
903 			// TODO: Use the force paramater in order to avoid calling
904 			// invalid callbacks
905 			current->is_active = false;
906 		}
907 
908 		current = current->link;
909 	}
910 
911 	TRACE_ERROR("no isochronous transfer found!\n");
912 	return B_ERROR;
913 }
914 
915 
916 status_t
917 UHCI::SubmitRequest(Transfer *transfer)
918 {
919 	Pipe *pipe = transfer->TransferPipe();
920 	usb_request_data *requestData = transfer->RequestData();
921 	bool directionIn = (requestData->RequestType & USB_REQTYPE_DEVICE_IN) > 0;
922 
923 	uhci_td *setupDescriptor = CreateDescriptor(pipe, TD_TOKEN_SETUP,
924 		sizeof(usb_request_data));
925 
926 	uhci_td *statusDescriptor = CreateDescriptor(pipe,
927 		directionIn ? TD_TOKEN_OUT : TD_TOKEN_IN, 0);
928 
929 	if (!setupDescriptor || !statusDescriptor) {
930 		TRACE_ERROR("failed to allocate descriptors\n");
931 		FreeDescriptor(setupDescriptor);
932 		FreeDescriptor(statusDescriptor);
933 		return B_NO_MEMORY;
934 	}
935 
936 	iovec vector;
937 	vector.iov_base = requestData;
938 	vector.iov_len = sizeof(usb_request_data);
939 	WriteDescriptorChain(setupDescriptor, &vector, 1);
940 
941 	statusDescriptor->status |= TD_CONTROL_IOC;
942 	statusDescriptor->token |= TD_TOKEN_DATA1;
943 	statusDescriptor->link_phy = TD_TERMINATE;
944 	statusDescriptor->link_log = NULL;
945 
946 	uhci_td *dataDescriptor = NULL;
947 	if (transfer->VectorCount() > 0) {
948 		uhci_td *lastDescriptor = NULL;
949 		status_t result = CreateDescriptorChain(pipe, &dataDescriptor,
950 			&lastDescriptor, directionIn ? TD_TOKEN_IN : TD_TOKEN_OUT,
951 			transfer->VectorLength());
952 
953 		if (result < B_OK) {
954 			FreeDescriptor(setupDescriptor);
955 			FreeDescriptor(statusDescriptor);
956 			return result;
957 		}
958 
959 		if (!directionIn) {
960 			WriteDescriptorChain(dataDescriptor, transfer->Vector(),
961 				transfer->VectorCount());
962 		}
963 
964 		LinkDescriptors(setupDescriptor, dataDescriptor);
965 		LinkDescriptors(lastDescriptor, statusDescriptor);
966 	} else {
967 		// Link transfer and status descriptors directly
968 		LinkDescriptors(setupDescriptor, statusDescriptor);
969 	}
970 
971 	Queue *queue = NULL;
972 	if (pipe->Speed() == USB_SPEED_LOWSPEED)
973 		queue = fQueues[UHCI_LOW_SPEED_CONTROL_QUEUE];
974 	else
975 		queue = fQueues[UHCI_FULL_SPEED_CONTROL_QUEUE];
976 
977 	uhci_qh *transferQueue = CreateTransferQueue(setupDescriptor);
978 	status_t result = AddPendingTransfer(transfer, queue, transferQueue,
979 		setupDescriptor, dataDescriptor, directionIn);
980 	if (result < B_OK) {
981 		TRACE_ERROR("failed to add pending transfer\n");
982 		FreeDescriptorChain(setupDescriptor);
983 		FreeTransferQueue(transferQueue);
984 		return result;
985 	}
986 
987 	queue->AppendTransfer(transferQueue);
988 	return B_OK;
989 }
990 
991 
992 status_t
993 UHCI::AddPendingTransfer(Transfer *transfer, Queue *queue,
994 	uhci_qh *transferQueue, uhci_td *firstDescriptor, uhci_td *dataDescriptor,
995 	bool directionIn)
996 {
997 	if (!transfer || !queue || !transferQueue || !firstDescriptor)
998 		return B_BAD_VALUE;
999 
1000 	transfer_data *data = new(std::nothrow) transfer_data;
1001 	if (!data)
1002 		return B_NO_MEMORY;
1003 
1004 	status_t result = transfer->InitKernelAccess();
1005 	if (result < B_OK) {
1006 		delete data;
1007 		return result;
1008 	}
1009 
1010 	data->transfer = transfer;
1011 	data->queue = queue;
1012 	data->transfer_queue = transferQueue;
1013 	data->first_descriptor = firstDescriptor;
1014 	data->data_descriptor = dataDescriptor;
1015 	data->incoming = directionIn;
1016 	data->canceled = false;
1017 	data->link = NULL;
1018 
1019 	if (!Lock()) {
1020 		delete data;
1021 		return B_ERROR;
1022 	}
1023 
1024 	if (fLastTransfer)
1025 		fLastTransfer->link = data;
1026 	if (!fFirstTransfer)
1027 		fFirstTransfer = data;
1028 
1029 	fLastTransfer = data;
1030 	Unlock();
1031 	return B_OK;
1032 }
1033 
1034 
1035 status_t
1036 UHCI::AddPendingIsochronousTransfer(Transfer *transfer, uhci_td **isoRequest,
1037 	bool directionIn)
1038 {
1039 	if (!transfer || !isoRequest)
1040 		return B_BAD_VALUE;
1041 
1042 	isochronous_transfer_data *data
1043 		= new(std::nothrow) isochronous_transfer_data;
1044 	if (!data)
1045 		return B_NO_MEMORY;
1046 
1047 	status_t result = transfer->InitKernelAccess();
1048 	if (result < B_OK) {
1049 		delete data;
1050 		return result;
1051 	}
1052 
1053 	data->transfer = transfer;
1054 	data->descriptors = isoRequest;
1055 	data->last_to_process = transfer->IsochronousData()->packet_count - 1;
1056 	data->incoming = directionIn;
1057 	data->is_active = true;
1058 	data->link = NULL;
1059 
1060 	// Put in the isochronous transfer list
1061 	if (!LockIsochronous()) {
1062 		delete data;
1063 		return B_ERROR;
1064 	}
1065 
1066 	if (fLastIsochronousTransfer)
1067 		fLastIsochronousTransfer->link = data;
1068 	if (!fFirstIsochronousTransfer)
1069 		fFirstIsochronousTransfer = data;
1070 
1071 	fLastIsochronousTransfer = data;
1072 	UnlockIsochronous();
1073 	return B_OK;
1074 }
1075 
1076 
1077 status_t
1078 UHCI::SubmitIsochronous(Transfer *transfer)
1079 {
1080 	Pipe *pipe = transfer->TransferPipe();
1081 	bool directionIn = (pipe->Direction() == Pipe::In);
1082 	usb_isochronous_data *isochronousData = transfer->IsochronousData();
1083 	size_t packetSize = transfer->DataLength();
1084 	size_t restSize = packetSize % isochronousData->packet_count;
1085 	packetSize /= isochronousData->packet_count;
1086 	uint16 currentFrame;
1087 
1088 	if (packetSize > pipe->MaxPacketSize()) {
1089 		TRACE_ERROR("isochronous packetSize is bigger than pipe MaxPacketSize\n");
1090 		return B_BAD_VALUE;
1091 	}
1092 
1093 	// Ignore the fact that the last descriptor might need less bandwidth.
1094 	// The overhead is not worthy.
1095 	uint16 bandwidth = transfer->Bandwidth() / isochronousData->packet_count;
1096 
1097 	TRACE("isochronous transfer descriptor bandwidth %d\n", bandwidth);
1098 
1099 	// The following holds the list of transfer descriptor of the
1100 	// isochronous request. It is used to quickly remove all the isochronous
1101 	// descriptors from the frame list, as descriptors are not link to each
1102 	// other in a queue like for every other transfer.
1103 	uhci_td **isoRequest
1104 		= new(std::nothrow) uhci_td *[isochronousData->packet_count];
1105 	if (isoRequest == NULL) {
1106 		TRACE("failed to create isoRequest array!\n");
1107 		return B_NO_MEMORY;
1108 	}
1109 
1110 	// Create the list of transfer descriptors
1111 	for (uint32 i = 0; i < (isochronousData->packet_count - 1); i++) {
1112 		isoRequest[i] = CreateDescriptor(pipe,
1113 			directionIn ? TD_TOKEN_IN : TD_TOKEN_OUT, packetSize);
1114 		// If we ran out of memory, clean up and return
1115 		if (isoRequest[i] == NULL) {
1116 			for (uint32 j = 0; j < i; j++)
1117 				FreeDescriptor(isoRequest[j]);
1118 			delete [] isoRequest;
1119 			return B_NO_MEMORY;
1120 		}
1121 		// Make sure data toggle is set to zero
1122 		isoRequest[i]->token &= ~TD_TOKEN_DATA1;
1123 	}
1124 
1125 	// Create the last transfer descriptor which should be of smaller size
1126 	// and set the IOC bit
1127 	isoRequest[isochronousData->packet_count - 1] = CreateDescriptor(pipe,
1128 		directionIn ? TD_TOKEN_IN : TD_TOKEN_OUT,
1129 		(restSize) ? restSize : packetSize);
1130 	// If we are that unlucky...
1131 	if (!isoRequest[isochronousData->packet_count - 1]) {
1132 		for (uint32 i = 0; i < (isochronousData->packet_count - 2); i++)
1133 			FreeDescriptor(isoRequest[i]);
1134 		delete [] isoRequest;
1135 		return B_NO_MEMORY;
1136 	}
1137 	isoRequest[isochronousData->packet_count - 1]->token &= ~TD_TOKEN_DATA1;
1138 
1139 	// If direction is out set every descriptor data
1140 	if (!directionIn) {
1141 		iovec *vector = transfer->Vector();
1142 		WriteIsochronousDescriptorChain(isoRequest,
1143 			isochronousData->packet_count, vector);
1144 	} else {
1145 		// Initialize the packet descriptors
1146 		for (uint32 i = 0; i < isochronousData->packet_count; i++) {
1147 			isochronousData->packet_descriptors[i].actual_length = 0;
1148 			isochronousData->packet_descriptors[i].status = B_NO_INIT;
1149 		}
1150 	}
1151 
1152 	TRACE("isochronous submitted size=%ld bytes, TDs=%ld, "
1153 		"packetSize=%ld, restSize=%ld\n", transfer->DataLength(),
1154 		isochronousData->packet_count, packetSize, restSize);
1155 
1156 	// Find the entry where to start inserting the first Isochronous descriptor
1157 	if (isochronousData->flags & USB_ISO_ASAP ||
1158 		isochronousData->starting_frame_number == NULL) {
1159 		// find the first available frame with enough bandwidth.
1160 		// This should always be the case, as defining the starting frame
1161 		// number in the driver makes no sense for many reason, one of which
1162 		// is that frame numbers value are host controller specific, and the
1163 		// driver does not know which host controller is running.
1164 		currentFrame = ReadReg16(UHCI_FRNUM);
1165 
1166 		// Make sure that:
1167 		// 1. We are at least 5ms ahead the controller
1168 		// 2. We stay in the range 0-1023
1169 		// 3. There is enough bandwidth in the first entry
1170 		currentFrame = (currentFrame + 5) % NUMBER_OF_FRAMES;
1171 	} else {
1172 		// Find out if the frame number specified has enough bandwidth,
1173 		// otherwise find the first next available frame with enough bandwidth
1174 		currentFrame = *isochronousData->starting_frame_number;
1175 	}
1176 
1177 	// Find the first entry with enough bandwidth
1178 	// TODO: should we also check the bandwidth of the following packet_count frames?
1179 	uint16 startSeekingFromFrame = currentFrame;
1180 	while (fFrameBandwidth[currentFrame] < bandwidth) {
1181 		currentFrame = (currentFrame + 1) % NUMBER_OF_FRAMES;
1182 		if (currentFrame == startSeekingFromFrame) {
1183 			TRACE_ERROR("not enough bandwidth to queue the isochronous request");
1184 			for (uint32 i = 0; i < isochronousData->packet_count; i++)
1185 				FreeDescriptor(isoRequest[i]);
1186 			delete [] isoRequest;
1187 		return B_ERROR;
1188 		}
1189 	}
1190 
1191 	if (isochronousData->starting_frame_number)
1192 		*isochronousData->starting_frame_number = currentFrame;
1193 
1194 	// Add transfer to the list
1195 	status_t result = AddPendingIsochronousTransfer(transfer, isoRequest,
1196 		directionIn);
1197 	if (result < B_OK) {
1198 		TRACE_ERROR("failed to add pending isochronous transfer\n");
1199 		for (uint32 i = 0; i < isochronousData->packet_count; i++)
1200 			FreeDescriptor(isoRequest[i]);
1201 		delete [] isoRequest;
1202 		return result;
1203 	}
1204 
1205 	TRACE("appended isochronous transfer by starting at frame number %d\n",
1206 		currentFrame);
1207 
1208 	// Insert the Transfer Descriptor by starting at
1209 	// the starting_frame_number entry
1210 	// TODO: We don't consider bInterval, and assume it's 1!
1211 	for (uint32 i = 0; i < isochronousData->packet_count; i++) {
1212 		result = LinkIsochronousDescriptor(isoRequest[i], currentFrame);
1213 		if (result < B_OK) {
1214 			TRACE_ERROR("failed to add pending isochronous transfer\n");
1215 			for (uint32 i = 0; i < isochronousData->packet_count; i++)
1216 				FreeDescriptor(isoRequest[i]);
1217 			delete [] isoRequest;
1218 			return result;
1219 		}
1220 
1221 		fFrameBandwidth[currentFrame] -= bandwidth;
1222 		currentFrame = (currentFrame + 1) % NUMBER_OF_FRAMES;
1223 	}
1224 
1225 	// Wake up the isochronous finisher thread
1226 	release_sem_etc(fFinishIsochronousTransfersSem, 1, B_DO_NOT_RESCHEDULE);
1227 
1228 	return B_OK;
1229 }
1230 
1231 
1232 isochronous_transfer_data *
1233 UHCI::FindIsochronousTransfer(uhci_td *descriptor)
1234 {
1235 	// Simply check every last descriptor of the isochronous transfer list
1236 	if (LockIsochronous()) {
1237 		isochronous_transfer_data *transfer = fFirstIsochronousTransfer;
1238 		if (transfer) {
1239 			while (transfer->descriptors[transfer->last_to_process]
1240 				!= descriptor) {
1241 				transfer = transfer->link;
1242 				if (!transfer)
1243 					break;
1244 			}
1245 		}
1246 		UnlockIsochronous();
1247 		return transfer;
1248 	}
1249 	return NULL;
1250 }
1251 
1252 
1253 status_t
1254 UHCI::LinkIsochronousDescriptor(uhci_td *descriptor, uint16 frame)
1255 {
1256 	// The transfer descriptor is appended to the last
1257 	// existing isochronous transfer descriptor (if any)
1258 	// in that frame.
1259 	if (LockIsochronous()) {
1260 		if (!fFirstIsochronousDescriptor[frame]) {
1261 			// Insert the transfer descriptor in the first position
1262 			fFrameList[frame] = descriptor->this_phy & ~FRAMELIST_NEXT_IS_QH;
1263 			fFirstIsochronousDescriptor[frame] = descriptor;
1264 			fLastIsochronousDescriptor[frame] = descriptor;
1265 		} else {
1266 			// Append to the last transfer descriptor
1267 			fLastIsochronousDescriptor[frame]->link_log = descriptor;
1268 			fLastIsochronousDescriptor[frame]->link_phy
1269 				= descriptor->this_phy & ~TD_NEXT_IS_QH;
1270 			fLastIsochronousDescriptor[frame] = descriptor;
1271 		}
1272 
1273 		descriptor->link_phy
1274 			= fQueues[UHCI_INTERRUPT_QUEUE]->PhysicalAddress() | TD_NEXT_IS_QH;
1275 		UnlockIsochronous();
1276 		return B_OK;
1277 	}
1278 	return B_ERROR;
1279 }
1280 
1281 
1282 uhci_td *
1283 UHCI::UnlinkIsochronousDescriptor(uint16 frame)
1284 {
1285 	// We always unlink from the top
1286 	if (LockIsochronous()) {
1287 		uhci_td *descriptor = fFirstIsochronousDescriptor[frame];
1288 		if (descriptor) {
1289 			// The descriptor will be freed later.
1290 			fFrameList[frame] = descriptor->link_phy;
1291 			if (descriptor->link_log) {
1292 				fFirstIsochronousDescriptor[frame]
1293 					= (uhci_td *)descriptor->link_log;
1294 			} else {
1295 				fFirstIsochronousDescriptor[frame] = NULL;
1296 				fLastIsochronousDescriptor[frame] = NULL;
1297 			}
1298 		}
1299 		UnlockIsochronous();
1300 		return descriptor;
1301 	}
1302 	return NULL;
1303 }
1304 
1305 
1306 int32
1307 UHCI::FinishThread(void *data)
1308 {
1309 	((UHCI *)data)->FinishTransfers();
1310 	return B_OK;
1311 }
1312 
1313 
1314 void
1315 UHCI::FinishTransfers()
1316 {
1317 	while (!fStopThreads) {
1318 		if (acquire_sem(fFinishTransfersSem) < B_OK)
1319 			continue;
1320 
1321 		// eat up sems that have been released by multiple interrupts
1322 		int32 semCount = 0;
1323 		get_sem_count(fFinishTransfersSem, &semCount);
1324 		if (semCount > 0)
1325 			acquire_sem_etc(fFinishTransfersSem, semCount, B_RELATIVE_TIMEOUT, 0);
1326 
1327 		if (!Lock())
1328 			continue;
1329 
1330 		TRACE("finishing transfers (first transfer: 0x%08lx; last"
1331 			" transfer: 0x%08lx)\n", (uint32)fFirstTransfer,
1332 			(uint32)fLastTransfer);
1333 		transfer_data *lastTransfer = NULL;
1334 		transfer_data *transfer = fFirstTransfer;
1335 		Unlock();
1336 
1337 		while (transfer) {
1338 			bool transferDone = false;
1339 			uhci_td *descriptor = transfer->first_descriptor;
1340 			status_t callbackStatus = B_OK;
1341 
1342 			while (descriptor) {
1343 				uint32 status = descriptor->status;
1344 				if (status & TD_STATUS_ACTIVE) {
1345 					// still in progress
1346 					TRACE("td (0x%08lx) still active\n", descriptor->this_phy);
1347 					break;
1348 				}
1349 
1350 				if (status & TD_ERROR_MASK) {
1351 					// an error occured
1352 					TRACE_ERROR("td (0x%08lx) error: status: 0x%08lx;"
1353 						" token: 0x%08lx;\n", descriptor->this_phy, status,
1354 						descriptor->token);
1355 
1356 					uint8 errorCount = status >> TD_ERROR_COUNT_SHIFT;
1357 					errorCount &= TD_ERROR_COUNT_MASK;
1358 					if (errorCount == 0) {
1359 						// the error counter counted down to zero, report why
1360 						int32 reasons = 0;
1361 						if (status & TD_STATUS_ERROR_BUFFER) {
1362 							callbackStatus = transfer->incoming ? B_DEV_DATA_OVERRUN : B_DEV_DATA_UNDERRUN;
1363 							reasons++;
1364 						}
1365 						if (status & TD_STATUS_ERROR_TIMEOUT) {
1366 							callbackStatus = transfer->incoming ? B_DEV_CRC_ERROR : B_TIMED_OUT;
1367 							reasons++;
1368 						}
1369 						if (status & TD_STATUS_ERROR_NAK) {
1370 							callbackStatus = B_DEV_UNEXPECTED_PID;
1371 							reasons++;
1372 						}
1373 						if (status & TD_STATUS_ERROR_BITSTUFF) {
1374 							callbackStatus = B_DEV_CRC_ERROR;
1375 							reasons++;
1376 						}
1377 
1378 						if (reasons > 1)
1379 							callbackStatus = B_DEV_MULTIPLE_ERRORS;
1380 					} else if (status & TD_STATUS_ERROR_BABBLE) {
1381 						// there is a babble condition
1382 						callbackStatus = transfer->incoming ? B_DEV_FIFO_OVERRUN : B_DEV_FIFO_UNDERRUN;
1383 					} else {
1384 						// if the error counter didn't count down to zero
1385 						// and there was no babble, then this halt was caused
1386 						// by a stall handshake
1387 						callbackStatus = B_DEV_STALLED;
1388 					}
1389 
1390 					transferDone = true;
1391 					break;
1392 				}
1393 
1394 				if ((descriptor->link_phy & TD_TERMINATE)
1395 					|| ((descriptor->status & TD_CONTROL_SPD) != 0
1396 						&& uhci_td_actual_length(descriptor)
1397 							< uhci_td_maximum_length(descriptor))) {
1398 					// all descriptors are done, or we have a short packet
1399 					TRACE("td (0x%08lx) ok\n", descriptor->this_phy);
1400 					callbackStatus = B_OK;
1401 					transferDone = true;
1402 					break;
1403 				}
1404 
1405 				descriptor = (uhci_td *)descriptor->link_log;
1406 			}
1407 
1408 			if (!transferDone) {
1409 				lastTransfer = transfer;
1410 				transfer = transfer->link;
1411 				continue;
1412 			}
1413 
1414 			// remove the transfer from the list first so we are sure
1415 			// it doesn't get canceled while we still process it
1416 			transfer_data *next = transfer->link;
1417 			if (Lock()) {
1418 				if (lastTransfer)
1419 					lastTransfer->link = transfer->link;
1420 
1421 				if (transfer == fFirstTransfer)
1422 					fFirstTransfer = transfer->link;
1423 				if (transfer == fLastTransfer)
1424 					fLastTransfer = lastTransfer;
1425 
1426 				// store the currently processing pipe here so we can wait
1427 				// in cancel if we are processing something on the target pipe
1428 				if (!transfer->canceled)
1429 					fProcessingPipe = transfer->transfer->TransferPipe();
1430 
1431 				transfer->link = NULL;
1432 				Unlock();
1433 			}
1434 
1435 			// if canceled the callback has already been called
1436 			if (!transfer->canceled) {
1437 				size_t actualLength = 0;
1438 				if (callbackStatus == B_OK) {
1439 					uint8 lastDataToggle = 0;
1440 					if (transfer->data_descriptor && transfer->incoming) {
1441 						// data to read out
1442 						iovec *vector = transfer->transfer->Vector();
1443 						size_t vectorCount = transfer->transfer->VectorCount();
1444 
1445 						transfer->transfer->PrepareKernelAccess();
1446 						actualLength = ReadDescriptorChain(
1447 							transfer->data_descriptor,
1448 							vector, vectorCount,
1449 							&lastDataToggle);
1450 					} else if (transfer->data_descriptor) {
1451 						// read the actual length that was sent
1452 						actualLength = ReadActualLength(
1453 							transfer->data_descriptor, &lastDataToggle);
1454 					}
1455 
1456 					transfer->transfer->TransferPipe()->SetDataToggle(lastDataToggle == 0);
1457 
1458 					if (transfer->transfer->IsFragmented()) {
1459 						// this transfer may still have data left
1460 						TRACE("advancing fragmented transfer\n");
1461 						transfer->transfer->AdvanceByFragment(actualLength);
1462 						if (transfer->transfer->VectorLength() > 0) {
1463 							TRACE("still %ld bytes left on transfer\n",
1464 								transfer->transfer->VectorLength());
1465 
1466 							Transfer *resubmit = transfer->transfer;
1467 
1468 							// free the used descriptors
1469 							transfer->queue->RemoveTransfer(
1470 								transfer->transfer_queue);
1471 							AddToFreeList(transfer);
1472 
1473 							// resubmit the advanced transfer so the rest
1474 							// of the buffers are transmitted over the bus
1475 							resubmit->PrepareKernelAccess();
1476 							if (SubmitTransfer(resubmit) != B_OK)
1477 								resubmit->Finished(B_ERROR, 0);
1478 
1479 							transfer = next;
1480 							continue;
1481 						}
1482 
1483 						// the transfer is done, but we already set the
1484 						// actualLength with AdvanceByFragment()
1485 						actualLength = 0;
1486 					}
1487 				}
1488 
1489 				transfer->transfer->Finished(callbackStatus, actualLength);
1490 				fProcessingPipe = NULL;
1491 			}
1492 
1493 			// remove and free the hardware queue and its descriptors
1494 			transfer->queue->RemoveTransfer(transfer->transfer_queue);
1495 			delete transfer->transfer;
1496 			AddToFreeList(transfer);
1497 			transfer = next;
1498 		}
1499 	}
1500 }
1501 
1502 
1503 void
1504 UHCI::AddToFreeList(transfer_data *transfer)
1505 {
1506 	transfer->free_after_frame = ReadReg16(UHCI_FRNUM);
1507 	if (!Lock())
1508 		return;
1509 
1510 	transfer->link = fFreeList;
1511 	fFreeList = transfer;
1512 	Unlock();
1513 
1514 	if (atomic_add(&fCleanupCount, 1) == 0)
1515 		release_sem(fCleanupSem);
1516 }
1517 
1518 
1519 int32
1520 UHCI::CleanupThread(void *data)
1521 {
1522 	((UHCI *)data)->Cleanup();
1523 	return B_OK;
1524 }
1525 
1526 
1527 void
1528 UHCI::Cleanup()
1529 {
1530 	while (!fStopThreads) {
1531 		if (acquire_sem(fCleanupSem) != B_OK)
1532 			continue;
1533 
1534 		bigtime_t nextTime = system_time() + 1000;
1535 		while (atomic_get(&fCleanupCount) != 0) {
1536 			// wait for the frame to pass
1537 			snooze_until(nextTime, B_SYSTEM_TIMEBASE);
1538 			nextTime += 1000;
1539 
1540 			if (!Lock())
1541 				continue;
1542 
1543 			// find the first entry we may free
1544 			transfer_data **link = &fFreeList;
1545 			transfer_data *transfer = fFreeList;
1546 			uint16 frameNumber = ReadReg16(UHCI_FRNUM);
1547 			while (transfer) {
1548 				if (transfer->free_after_frame != frameNumber) {
1549 					*link = NULL;
1550 					break;
1551 				}
1552 
1553 				link = &transfer->link;
1554 				transfer = transfer->link;
1555 			}
1556 
1557 			Unlock();
1558 
1559 			// the transfers below this one are all freeable
1560 			while (transfer) {
1561 				transfer_data *next = transfer->link;
1562 				FreeDescriptorChain(transfer->first_descriptor);
1563 				FreeTransferQueue(transfer->transfer_queue);
1564 				delete transfer;
1565 				atomic_add(&fCleanupCount, -1);
1566 				transfer = next;
1567 			}
1568 		}
1569 	}
1570 }
1571 
1572 
1573 int32
1574 UHCI::FinishIsochronousThread(void *data)
1575 {
1576        ((UHCI *)data)->FinishIsochronousTransfers();
1577        return B_OK;
1578 }
1579 
1580 
1581 void
1582 UHCI::FinishIsochronousTransfers()
1583 {
1584 	/* This thread stays one position behind the controller and processes every
1585 	 * isochronous descriptor. Once it finds the last isochronous descriptor
1586 	 * of a transfer, it processes the entire transfer.
1587 	 */
1588 
1589 	while (!fStopThreads) {
1590 		// Go to sleep if there are not isochronous transfer to process
1591 		if (acquire_sem(fFinishIsochronousTransfersSem) < B_OK)
1592 			return;
1593 
1594 		bool transferDone = false;
1595 		uint16 currentFrame = ReadReg16(UHCI_FRNUM);
1596 
1597 		// Process the frame list until one transfer is processed
1598 		while (!transferDone) {
1599 			// wait 1ms in order to be sure to be one position behind
1600 			// the controller
1601 			if (currentFrame == ReadReg16(UHCI_FRNUM))
1602 				snooze(1000);
1603 
1604 			// Process the frame till it has isochronous descriptors in it.
1605 			while (!(fFrameList[currentFrame] & FRAMELIST_NEXT_IS_QH)) {
1606 				uhci_td *current = UnlinkIsochronousDescriptor(currentFrame);
1607 
1608 				// Process the transfer if we found the last descriptor
1609 				isochronous_transfer_data *transfer
1610 					= FindIsochronousTransfer(current);
1611 					// Process the descriptors only if it is still active and
1612 					// belongs to an inbound transfer. If the transfer is not
1613 					// active, it means the request has been removed, so simply
1614 					// remove the descriptors.
1615 				if (transfer && transfer->is_active) {
1616 					if (current->token & TD_TOKEN_IN) {
1617 						iovec *vector = transfer->transfer->Vector();
1618 						transfer->transfer->PrepareKernelAccess();
1619 						ReadIsochronousDescriptorChain(transfer, vector);
1620 					}
1621 
1622 					// Remove the transfer
1623 					if (LockIsochronous()) {
1624 						if (transfer == fFirstIsochronousTransfer) {
1625 							fFirstIsochronousTransfer = transfer->link;
1626 							if (transfer == fLastIsochronousTransfer)
1627 								fLastIsochronousTransfer = NULL;
1628 						} else {
1629 							isochronous_transfer_data *temp
1630 								= fFirstIsochronousTransfer;
1631 							while (transfer != temp->link)
1632 								temp = temp->link;
1633 
1634 							if (transfer == fLastIsochronousTransfer)
1635 								fLastIsochronousTransfer = temp;
1636 							temp->link = temp->link->link;
1637 						}
1638 						UnlockIsochronous();
1639 					}
1640 
1641 					transfer->transfer->Finished(B_OK, 0);
1642 
1643 					uint32 packetCount =
1644 						transfer->transfer->IsochronousData()->packet_count;
1645 					for (uint32 i = 0; i < packetCount; i++)
1646 						FreeDescriptor(transfer->descriptors[i]);
1647 
1648 					delete [] transfer->descriptors;
1649 					delete transfer->transfer;
1650 					delete transfer;
1651 					transferDone = true;
1652 				}
1653 			}
1654 
1655 			// Make sure to reset the frame bandwidth
1656 			fFrameBandwidth[currentFrame] = MAX_AVAILABLE_BANDWIDTH;
1657 			currentFrame = (currentFrame + 1) % NUMBER_OF_FRAMES;
1658 		}
1659 	}
1660 }
1661 
1662 
1663 void
1664 UHCI::GlobalReset()
1665 {
1666 	uint8 sofValue = ReadReg8(UHCI_SOFMOD);
1667 
1668 	WriteReg16(UHCI_USBCMD, UHCI_USBCMD_GRESET);
1669 	snooze(100000);
1670 	WriteReg16(UHCI_USBCMD, 0);
1671 	snooze(10000);
1672 
1673 	WriteReg8(UHCI_SOFMOD, sofValue);
1674 }
1675 
1676 
1677 status_t
1678 UHCI::ControllerReset()
1679 {
1680 	WriteReg16(UHCI_USBCMD, UHCI_USBCMD_HCRESET);
1681 
1682 	int32 tries = 5;
1683 	while (ReadReg16(UHCI_USBCMD) & UHCI_USBCMD_HCRESET) {
1684 		snooze(10000);
1685 		if (tries-- < 0)
1686 			return B_ERROR;
1687 	}
1688 
1689 	return B_OK;
1690 }
1691 
1692 
1693 status_t
1694 UHCI::GetPortStatus(uint8 index, usb_port_status *status)
1695 {
1696 	if (index > 1)
1697 		return B_BAD_INDEX;
1698 
1699 	status->status = status->change = 0;
1700 	uint16 portStatus = ReadReg16(UHCI_PORTSC1 + index * 2);
1701 
1702 	// build the status
1703 	if (portStatus & UHCI_PORTSC_CURSTAT)
1704 		status->status |= PORT_STATUS_CONNECTION;
1705 	if (portStatus & UHCI_PORTSC_ENABLED)
1706 		status->status |= PORT_STATUS_ENABLE;
1707 	if (portStatus & UHCI_PORTSC_RESET)
1708 		status->status |= PORT_STATUS_RESET;
1709 	if (portStatus & UHCI_PORTSC_LOWSPEED)
1710 		status->status |= PORT_STATUS_LOW_SPEED;
1711 
1712 	// build the change
1713 	if (portStatus & UHCI_PORTSC_STATCHA)
1714 		status->change |= PORT_STATUS_CONNECTION;
1715 	if (portStatus & UHCI_PORTSC_ENABCHA)
1716 		status->change |= PORT_STATUS_ENABLE;
1717 
1718 	// ToDo: work out suspended/resume
1719 
1720 	// there are no bits to indicate reset change
1721 	if (fPortResetChange & (1 << index))
1722 		status->change |= PORT_STATUS_RESET;
1723 
1724 	// the port is automagically powered on
1725 	status->status |= PORT_STATUS_POWER;
1726 	return B_OK;
1727 }
1728 
1729 
1730 status_t
1731 UHCI::SetPortFeature(uint8 index, uint16 feature)
1732 {
1733 	if (index > 1)
1734 		return B_BAD_INDEX;
1735 
1736 	switch (feature) {
1737 		case PORT_RESET:
1738 			return ResetPort(index);
1739 
1740 		case PORT_POWER:
1741 			// the ports are automatically powered
1742 			return B_OK;
1743 	}
1744 
1745 	return B_BAD_VALUE;
1746 }
1747 
1748 
1749 status_t
1750 UHCI::ClearPortFeature(uint8 index, uint16 feature)
1751 {
1752 	if (index > 1)
1753 		return B_BAD_INDEX;
1754 
1755 	uint32 portRegister = UHCI_PORTSC1 + index * 2;
1756 	uint16 portStatus = ReadReg16(portRegister) & UHCI_PORTSC_DATAMASK;
1757 
1758 	switch (feature) {
1759 		case C_PORT_RESET:
1760 			fPortResetChange &= ~(1 << index);
1761 			return B_OK;
1762 
1763 		case C_PORT_CONNECTION:
1764 			WriteReg16(portRegister, portStatus | UHCI_PORTSC_STATCHA);
1765 			return B_OK;
1766 
1767 		case C_PORT_ENABLE:
1768 			WriteReg16(portRegister, portStatus | UHCI_PORTSC_ENABCHA);
1769 			return B_OK;
1770 	}
1771 
1772 	return B_BAD_VALUE;
1773 }
1774 
1775 
1776 status_t
1777 UHCI::ResetPort(uint8 index)
1778 {
1779 	if (index > 1)
1780 		return B_BAD_INDEX;
1781 
1782 	TRACE("reset port %d\n", index);
1783 
1784 	uint32 port = UHCI_PORTSC1 + index * 2;
1785 	uint16 status = ReadReg16(port);
1786 	status &= UHCI_PORTSC_DATAMASK;
1787 	WriteReg16(port, status | UHCI_PORTSC_RESET);
1788 	snooze(250000);
1789 
1790 	status = ReadReg16(port);
1791 	status &= UHCI_PORTSC_DATAMASK;
1792 	WriteReg16(port, status & ~UHCI_PORTSC_RESET);
1793 	snooze(1000);
1794 
1795 	for (int32 i = 10; i > 0; i--) {
1796 		// try to enable the port
1797 		status = ReadReg16(port);
1798 		status &= UHCI_PORTSC_DATAMASK;
1799 		WriteReg16(port, status | UHCI_PORTSC_ENABLED);
1800 		snooze(50000);
1801 
1802 		status = ReadReg16(port);
1803 
1804 		if ((status & UHCI_PORTSC_CURSTAT) == 0) {
1805 			// no device connected. since we waited long enough we can assume
1806 			// that the port was reset and no device is connected.
1807 			break;
1808 		}
1809 
1810 		if (status & (UHCI_PORTSC_STATCHA | UHCI_PORTSC_ENABCHA)) {
1811 			// port enabled changed or connection status were set.
1812 			// acknowledge either / both and wait again.
1813 			status &= UHCI_PORTSC_DATAMASK;
1814 			WriteReg16(port, status | UHCI_PORTSC_STATCHA | UHCI_PORTSC_ENABCHA);
1815 			continue;
1816 		}
1817 
1818 		if (status & UHCI_PORTSC_ENABLED) {
1819 			// the port is enabled
1820 			break;
1821 		}
1822 	}
1823 
1824 	fPortResetChange |= (1 << index);
1825 	TRACE("port was reset: 0x%04x\n", ReadReg16(port));
1826 	return B_OK;
1827 }
1828 
1829 
1830 int32
1831 UHCI::InterruptHandler(void *data)
1832 {
1833 	return ((UHCI *)data)->Interrupt();
1834 }
1835 
1836 
1837 int32
1838 UHCI::Interrupt()
1839 {
1840 	static spinlock lock = B_SPINLOCK_INITIALIZER;
1841 	acquire_spinlock(&lock);
1842 
1843 	// Check if we really had an interrupt
1844 	uint16 status = ReadReg16(UHCI_USBSTS);
1845 	if ((status & fEnabledInterrupts) == 0) {
1846 		if (status != 0) {
1847 			TRACE("discarding not enabled interrupts 0x%08x\n", status);
1848 			WriteReg16(UHCI_USBSTS, status);
1849 		}
1850 
1851 		release_spinlock(&lock);
1852 		return B_UNHANDLED_INTERRUPT;
1853 	}
1854 
1855 	uint16 acknowledge = 0;
1856 	bool finishTransfers = false;
1857 	int32 result = B_HANDLED_INTERRUPT;
1858 
1859 	if (status & UHCI_USBSTS_USBINT) {
1860 		TRACE_MODULE("transfer finished\n");
1861 		acknowledge |= UHCI_USBSTS_USBINT;
1862 		result = B_INVOKE_SCHEDULER;
1863 		finishTransfers = true;
1864 	}
1865 
1866 	if (status & UHCI_USBSTS_ERRINT) {
1867 		TRACE_MODULE("transfer error\n");
1868 		acknowledge |= UHCI_USBSTS_ERRINT;
1869 		result = B_INVOKE_SCHEDULER;
1870 		finishTransfers = true;
1871 	}
1872 
1873 	if (status & UHCI_USBSTS_RESDET) {
1874 		TRACE_MODULE("resume detected\n");
1875 		acknowledge |= UHCI_USBSTS_RESDET;
1876 	}
1877 
1878 	if (status & UHCI_USBSTS_HOSTERR) {
1879 		TRACE_MODULE_ERROR("host system error\n");
1880 		acknowledge |= UHCI_USBSTS_HOSTERR;
1881 	}
1882 
1883 	if (status & UHCI_USBSTS_HCPRERR) {
1884 		TRACE_MODULE_ERROR("process error\n");
1885 		acknowledge |= UHCI_USBSTS_HCPRERR;
1886 	}
1887 
1888 	if (status & UHCI_USBSTS_HCHALT) {
1889 		TRACE_MODULE_ERROR("host controller halted\n");
1890 		// at least disable interrupts so we do not flood the system
1891 		WriteReg16(UHCI_USBINTR, 0);
1892 		fEnabledInterrupts = 0;
1893 		// ToDo: cancel all transfers and reset the host controller
1894 		// acknowledge not needed
1895 	}
1896 
1897 	if (acknowledge)
1898 		WriteReg16(UHCI_USBSTS, acknowledge);
1899 
1900 	release_spinlock(&lock);
1901 
1902 	if (finishTransfers)
1903 		release_sem_etc(fFinishTransfersSem, 1, B_DO_NOT_RESCHEDULE);
1904 
1905 	return result;
1906 }
1907 
1908 
1909 status_t
1910 UHCI::AddTo(Stack *stack)
1911 {
1912 #ifdef TRACE_USB
1913 	set_dprintf_enabled(true);
1914 #ifndef HAIKU_TARGET_PLATFORM_HAIKU
1915 	load_driver_symbols("uhci");
1916 #endif
1917 #endif
1918 
1919 	if (!sPCIModule) {
1920 		status_t status = get_module(B_PCI_MODULE_NAME, (module_info **)&sPCIModule);
1921 		if (status < B_OK) {
1922 			TRACE_MODULE_ERROR("AddTo(): getting pci module failed! 0x%08lx\n",
1923 				status);
1924 			return status;
1925 		}
1926 	}
1927 
1928 	TRACE_MODULE("AddTo(): setting up hardware\n");
1929 
1930 	bool found = false;
1931 	pci_info *item = new(std::nothrow) pci_info;
1932 	if (!item) {
1933 		sPCIModule = NULL;
1934 		put_module(B_PCI_MODULE_NAME);
1935 		return B_NO_MEMORY;
1936 	}
1937 
1938 	for (int32 i = 0; sPCIModule->get_nth_pci_info(i, item) >= B_OK; i++) {
1939 
1940 		if (item->class_base == PCI_serial_bus && item->class_sub == PCI_usb
1941 			&& item->class_api == PCI_usb_uhci) {
1942 			if (item->u.h0.interrupt_line == 0
1943 				|| item->u.h0.interrupt_line == 0xFF) {
1944 				TRACE_MODULE_ERROR("AddTo(): found with invalid IRQ - check IRQ assignement\n");
1945 				continue;
1946 			}
1947 
1948 			TRACE_MODULE("AddTo(): found at IRQ %u\n",
1949 				item->u.h0.interrupt_line);
1950 			UHCI *bus = new(std::nothrow) UHCI(item, stack);
1951 			if (!bus) {
1952 				delete item;
1953 				sPCIModule = NULL;
1954 				put_module(B_PCI_MODULE_NAME);
1955 				return B_NO_MEMORY;
1956 			}
1957 
1958 			if (bus->InitCheck() < B_OK) {
1959 				TRACE_MODULE_ERROR("AddTo(): InitCheck() failed 0x%08lx\n",
1960 					bus->InitCheck());
1961 				delete bus;
1962 				continue;
1963 			}
1964 
1965 			// the bus took it away
1966 			item = new(std::nothrow) pci_info;
1967 
1968 			bus->Start();
1969 			stack->AddBusManager(bus);
1970 			found = true;
1971 		}
1972 	}
1973 
1974 	if (!found) {
1975 		TRACE_MODULE_ERROR("no devices found\n");
1976 		delete item;
1977 		sPCIModule = NULL;
1978 		put_module(B_PCI_MODULE_NAME);
1979 		return ENODEV;
1980 	}
1981 
1982 	delete item;
1983 	return B_OK;
1984 }
1985 
1986 
1987 status_t
1988 UHCI::CreateFilledTransfer(Transfer *transfer, uhci_td **_firstDescriptor,
1989 	uhci_qh **_transferQueue)
1990 {
1991 	Pipe *pipe = transfer->TransferPipe();
1992 	bool directionIn = (pipe->Direction() == Pipe::In);
1993 
1994 	uhci_td *firstDescriptor = NULL;
1995 	uhci_td *lastDescriptor = NULL;
1996 	status_t result = CreateDescriptorChain(pipe, &firstDescriptor,
1997 		&lastDescriptor, directionIn ? TD_TOKEN_IN : TD_TOKEN_OUT,
1998 		transfer->VectorLength());
1999 
2000 	if (result < B_OK)
2001 		return result;
2002 	if (!firstDescriptor || !lastDescriptor)
2003 		return B_NO_MEMORY;
2004 
2005 	lastDescriptor->status |= TD_CONTROL_IOC;
2006 	lastDescriptor->link_phy = TD_TERMINATE;
2007 	lastDescriptor->link_log = NULL;
2008 
2009 	if (!directionIn) {
2010 		WriteDescriptorChain(firstDescriptor, transfer->Vector(),
2011 			transfer->VectorCount());
2012 	}
2013 
2014 	uhci_qh *transferQueue = CreateTransferQueue(firstDescriptor);
2015 	if (!transferQueue) {
2016 		FreeDescriptorChain(firstDescriptor);
2017 		return B_NO_MEMORY;
2018 	}
2019 
2020 	*_firstDescriptor = firstDescriptor;
2021 	*_transferQueue = transferQueue;
2022 	return B_OK;
2023 }
2024 
2025 
2026 uhci_qh *
2027 UHCI::CreateTransferQueue(uhci_td *descriptor)
2028 {
2029 	uhci_qh *queueHead;
2030 	void *physicalAddress;
2031 	if (fStack->AllocateChunk((void **)&queueHead,
2032 		&physicalAddress, sizeof(uhci_qh)) < B_OK)
2033 		return NULL;
2034 
2035 	queueHead->this_phy = (addr_t)physicalAddress;
2036 	queueHead->element_phy = descriptor->this_phy;
2037 	return queueHead;
2038 }
2039 
2040 
2041 void
2042 UHCI::FreeTransferQueue(uhci_qh *queueHead)
2043 {
2044 	if (!queueHead)
2045 		return;
2046 
2047 	fStack->FreeChunk(queueHead, (void *)queueHead->this_phy, sizeof(uhci_qh));
2048 }
2049 
2050 
2051 uhci_td *
2052 UHCI::CreateDescriptor(Pipe *pipe, uint8 direction, size_t bufferSize)
2053 {
2054 	uhci_td *result;
2055 	void *physicalAddress;
2056 
2057 	if (fStack->AllocateChunk((void **)&result, &physicalAddress,
2058 		sizeof(uhci_td)) < B_OK) {
2059 		TRACE_ERROR("failed to allocate a transfer descriptor\n");
2060 		return NULL;
2061 	}
2062 
2063 	result->this_phy = (addr_t)physicalAddress;
2064 	result->status = TD_STATUS_ACTIVE;
2065 	if (pipe->Type() & USB_OBJECT_ISO_PIPE)
2066 		result->status |= TD_CONTROL_ISOCHRONOUS;
2067 	else {
2068 		result->status |= TD_CONTROL_3_ERRORS;
2069 		if (direction == TD_TOKEN_IN
2070 			&& (pipe->Type() & USB_OBJECT_CONTROL_PIPE) == 0)
2071 			result->status |= TD_CONTROL_SPD;
2072 	}
2073 	if (pipe->Speed() == USB_SPEED_LOWSPEED)
2074 		result->status |= TD_CONTROL_LOWSPEED;
2075 
2076 	result->buffer_size = bufferSize;
2077 	if (bufferSize == 0)
2078 		result->token = TD_TOKEN_NULL_DATA;
2079 	else
2080 		result->token = (bufferSize - 1) << TD_TOKEN_MAXLEN_SHIFT;
2081 
2082 	result->token |= (pipe->EndpointAddress() << TD_TOKEN_ENDPTADDR_SHIFT)
2083 		| (pipe->DeviceAddress() << 8) | direction;
2084 
2085 	result->link_phy = 0;
2086 	result->link_log = NULL;
2087 	if (bufferSize <= 0) {
2088 		result->buffer_log = NULL;
2089 		result->buffer_phy = 0;
2090 		return result;
2091 	}
2092 
2093 	if (fStack->AllocateChunk(&result->buffer_log, (void **)&result->buffer_phy,
2094 		bufferSize) < B_OK) {
2095 		TRACE_ERROR("unable to allocate space for the buffer\n");
2096 		fStack->FreeChunk(result, (void *)result->this_phy, sizeof(uhci_td));
2097 		return NULL;
2098 	}
2099 
2100 	return result;
2101 }
2102 
2103 
2104 status_t
2105 UHCI::CreateDescriptorChain(Pipe *pipe, uhci_td **_firstDescriptor,
2106 	uhci_td **_lastDescriptor, uint8 direction, size_t bufferSize)
2107 {
2108 	size_t packetSize = pipe->MaxPacketSize();
2109 	int32 descriptorCount = (bufferSize + packetSize - 1) / packetSize;
2110 	if (descriptorCount == 0)
2111 		descriptorCount = 1;
2112 
2113 	bool dataToggle = pipe->DataToggle();
2114 	uhci_td *firstDescriptor = NULL;
2115 	uhci_td *lastDescriptor = *_firstDescriptor;
2116 	for (int32 i = 0; i < descriptorCount; i++) {
2117 		uhci_td *descriptor = CreateDescriptor(pipe, direction,
2118 			min_c(packetSize, bufferSize));
2119 
2120 		if (!descriptor) {
2121 			FreeDescriptorChain(firstDescriptor);
2122 			return B_NO_MEMORY;
2123 		}
2124 
2125 		if (dataToggle)
2126 			descriptor->token |= TD_TOKEN_DATA1;
2127 
2128 		// link to previous
2129 		if (lastDescriptor)
2130 			LinkDescriptors(lastDescriptor, descriptor);
2131 
2132 		dataToggle = !dataToggle;
2133 		bufferSize -= packetSize;
2134 		lastDescriptor = descriptor;
2135 		if (!firstDescriptor)
2136 			firstDescriptor = descriptor;
2137 	}
2138 
2139 	*_firstDescriptor = firstDescriptor;
2140 	*_lastDescriptor = lastDescriptor;
2141 	return B_OK;
2142 }
2143 
2144 
2145 void
2146 UHCI::FreeDescriptor(uhci_td *descriptor)
2147 {
2148 	if (!descriptor)
2149 		return;
2150 
2151 	if (descriptor->buffer_log) {
2152 		fStack->FreeChunk(descriptor->buffer_log,
2153 			(void *)descriptor->buffer_phy, descriptor->buffer_size);
2154 	}
2155 
2156 	fStack->FreeChunk(descriptor, (void *)descriptor->this_phy, sizeof(uhci_td));
2157 }
2158 
2159 
2160 void
2161 UHCI::FreeDescriptorChain(uhci_td *topDescriptor)
2162 {
2163 	uhci_td *current = topDescriptor;
2164 	uhci_td *next = NULL;
2165 
2166 	while (current) {
2167 		next = (uhci_td *)current->link_log;
2168 		FreeDescriptor(current);
2169 		current = next;
2170 	}
2171 }
2172 
2173 
2174 void
2175 UHCI::LinkDescriptors(uhci_td *first, uhci_td *second)
2176 {
2177 	first->link_phy = second->this_phy | TD_DEPTH_FIRST;
2178 	first->link_log = second;
2179 }
2180 
2181 
2182 size_t
2183 UHCI::WriteDescriptorChain(uhci_td *topDescriptor, iovec *vector,
2184 	size_t vectorCount)
2185 {
2186 	uhci_td *current = topDescriptor;
2187 	size_t actualLength = 0;
2188 	size_t vectorIndex = 0;
2189 	size_t vectorOffset = 0;
2190 	size_t bufferOffset = 0;
2191 
2192 	while (current) {
2193 		if (!current->buffer_log)
2194 			break;
2195 
2196 		while (true) {
2197 			size_t length = min_c(current->buffer_size - bufferOffset,
2198 				vector[vectorIndex].iov_len - vectorOffset);
2199 
2200 			TRACE("copying %ld bytes to bufferOffset %ld from"
2201 				" vectorOffset %ld at index %ld of %ld\n", length, bufferOffset,
2202 				vectorOffset, vectorIndex, vectorCount);
2203 			memcpy((uint8 *)current->buffer_log + bufferOffset,
2204 				(uint8 *)vector[vectorIndex].iov_base + vectorOffset, length);
2205 
2206 			actualLength += length;
2207 			vectorOffset += length;
2208 			bufferOffset += length;
2209 
2210 			if (vectorOffset >= vector[vectorIndex].iov_len) {
2211 				if (++vectorIndex >= vectorCount) {
2212 					TRACE("wrote descriptor chain (%ld bytes, no more vectors)\n",
2213 						actualLength);
2214 					return actualLength;
2215 				}
2216 
2217 				vectorOffset = 0;
2218 			}
2219 
2220 			if (bufferOffset >= current->buffer_size) {
2221 				bufferOffset = 0;
2222 				break;
2223 			}
2224 		}
2225 
2226 		if (current->link_phy & TD_TERMINATE)
2227 			break;
2228 
2229 		current = (uhci_td *)current->link_log;
2230 	}
2231 
2232 	TRACE("wrote descriptor chain (%ld bytes)\n", actualLength);
2233 	return actualLength;
2234 }
2235 
2236 
2237 size_t
2238 UHCI::ReadDescriptorChain(uhci_td *topDescriptor, iovec *vector,
2239 	size_t vectorCount, uint8 *lastDataToggle)
2240 {
2241 	uint8 dataToggle = 0;
2242 	uhci_td *current = topDescriptor;
2243 	size_t actualLength = 0;
2244 	size_t vectorIndex = 0;
2245 	size_t vectorOffset = 0;
2246 	size_t bufferOffset = 0;
2247 
2248 	while (current && (current->status & TD_STATUS_ACTIVE) == 0) {
2249 		if (!current->buffer_log)
2250 			break;
2251 
2252 		dataToggle = (current->token >> TD_TOKEN_DATA_TOGGLE_SHIFT) & 0x01;
2253 		size_t bufferSize = uhci_td_actual_length(current);
2254 
2255 		while (true) {
2256 			size_t length = min_c(bufferSize - bufferOffset,
2257 				vector[vectorIndex].iov_len - vectorOffset);
2258 
2259 			TRACE("copying %ld bytes to vectorOffset %ld from"
2260 				" bufferOffset %ld at index %ld of %ld\n", length, vectorOffset,
2261 				bufferOffset, vectorIndex, vectorCount);
2262 			memcpy((uint8 *)vector[vectorIndex].iov_base + vectorOffset,
2263 				(uint8 *)current->buffer_log + bufferOffset, length);
2264 
2265 			actualLength += length;
2266 			vectorOffset += length;
2267 			bufferOffset += length;
2268 
2269 			if (vectorOffset >= vector[vectorIndex].iov_len) {
2270 				if (++vectorIndex >= vectorCount) {
2271 					TRACE("read descriptor chain (%ld bytes, no more vectors)\n",
2272 						actualLength);
2273 					if (lastDataToggle)
2274 						*lastDataToggle = dataToggle;
2275 					return actualLength;
2276 				}
2277 
2278 				vectorOffset = 0;
2279 			}
2280 
2281 			if (bufferOffset >= bufferSize) {
2282 				bufferOffset = 0;
2283 				break;
2284 			}
2285 		}
2286 
2287 		if (current->link_phy & TD_TERMINATE)
2288 			break;
2289 
2290 		current = (uhci_td *)current->link_log;
2291 	}
2292 
2293 	if (lastDataToggle)
2294 		*lastDataToggle = dataToggle;
2295 
2296 	TRACE("read descriptor chain (%ld bytes)\n", actualLength);
2297 	return actualLength;
2298 }
2299 
2300 
2301 size_t
2302 UHCI::ReadActualLength(uhci_td *topDescriptor, uint8 *lastDataToggle)
2303 {
2304 	size_t actualLength = 0;
2305 	uhci_td *current = topDescriptor;
2306 	uint8 dataToggle = 0;
2307 
2308 	while (current && (current->status & TD_STATUS_ACTIVE) == 0) {
2309 		actualLength += uhci_td_actual_length(current);
2310 		dataToggle = (current->token >> TD_TOKEN_DATA_TOGGLE_SHIFT) & 0x01;
2311 
2312 		if (current->link_phy & TD_TERMINATE)
2313 			break;
2314 
2315 		current = (uhci_td *)current->link_log;
2316 	}
2317 
2318 	if (lastDataToggle)
2319 		*lastDataToggle = dataToggle;
2320 
2321 	TRACE("read actual length (%ld bytes)\n", actualLength);
2322 	return actualLength;
2323 }
2324 
2325 
2326 void
2327 UHCI::WriteIsochronousDescriptorChain(uhci_td **isoRequest, uint32 packetCount,
2328 	iovec *vector)
2329 {
2330 	size_t vectorOffset = 0;
2331 	for (uint32 i = 0; i < packetCount; i++) {
2332 		size_t bufferSize = isoRequest[i]->buffer_size;
2333 		memcpy((uint8 *)isoRequest[i]->buffer_log,
2334 			(uint8 *)vector->iov_base + vectorOffset, bufferSize);
2335 		vectorOffset += bufferSize;
2336 	}
2337 }
2338 
2339 
2340 void
2341 UHCI::ReadIsochronousDescriptorChain(isochronous_transfer_data *transfer,
2342 	iovec *vector)
2343 {
2344 	size_t vectorOffset = 0;
2345 	usb_isochronous_data *isochronousData
2346 		= transfer->transfer->IsochronousData();
2347 
2348 	for (uint32 i = 0; i < isochronousData->packet_count; i++) {
2349 		uhci_td *current = transfer->descriptors[i];
2350 
2351 		size_t bufferSize = current->buffer_size;
2352 		size_t actualLength = uhci_td_actual_length(current);
2353 
2354 		isochronousData->packet_descriptors[i].actual_length = actualLength;
2355 
2356 		if (actualLength > 0)
2357 			isochronousData->packet_descriptors[i].status = B_OK;
2358 		else {
2359 			isochronousData->packet_descriptors[i].status = B_ERROR;
2360 			vectorOffset += bufferSize;
2361 			continue;
2362 		}
2363 		memcpy((uint8 *)vector->iov_base + vectorOffset,
2364 			(uint8 *)current->buffer_log, bufferSize);
2365 
2366 		vectorOffset += bufferSize;
2367 	}
2368 }
2369 
2370 
2371 bool
2372 UHCI::LockIsochronous()
2373 {
2374 	return (mutex_lock(&fIsochronousLock) == B_OK);
2375 }
2376 
2377 
2378 void
2379 UHCI::UnlockIsochronous()
2380 {
2381 	mutex_unlock(&fIsochronousLock);
2382 }
2383 
2384 
2385 inline void
2386 UHCI::WriteReg8(uint32 reg, uint8 value)
2387 {
2388 	sPCIModule->write_io_8(fRegisterBase + reg, value);
2389 }
2390 
2391 
2392 inline void
2393 UHCI::WriteReg16(uint32 reg, uint16 value)
2394 {
2395 	sPCIModule->write_io_16(fRegisterBase + reg, value);
2396 }
2397 
2398 
2399 inline void
2400 UHCI::WriteReg32(uint32 reg, uint32 value)
2401 {
2402 	sPCIModule->write_io_32(fRegisterBase + reg, value);
2403 }
2404 
2405 
2406 inline uint8
2407 UHCI::ReadReg8(uint32 reg)
2408 {
2409 	return sPCIModule->read_io_8(fRegisterBase + reg);
2410 }
2411 
2412 
2413 inline uint16
2414 UHCI::ReadReg16(uint32 reg)
2415 {
2416 	return sPCIModule->read_io_16(fRegisterBase + reg);
2417 }
2418 
2419 
2420 inline uint32
2421 UHCI::ReadReg32(uint32 reg)
2422 {
2423 	return sPCIModule->read_io_32(fRegisterBase + reg);
2424 }
2425