usbhost.c 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
/*HEADER*****************************************************************
**************************************************************************
*** 
*** Copyright (c) 2001-2002 ARC International.
*** All rights reserved                                          
***                                                              
*** This software embodies materials and concepts which are      
*** confidential to ARC International and is made
*** available solely pursuant to the terms of a written license   
*** agreement with ARC International             
***
*** $Workfile:echo.c$
*** $Revision: 1.8 $
*** $Date: 2004/06/30 05:55:46 $
***
*** Description:      
***   This file contains the host specific code of the echo example.
***                                                               
**************************************************************************
*END*********************************************************************/

#include "osint.h"
#include "host.h"

/*
 * Host controller state
 */
HOST_GLOBAL_STRUCT	host_global_struct[OS_USB_MAX_CONTROLLERS];

/*
 * Registry of usb host drivers that are available
 *
 * At init time, each driver calls _usb_host_driver_register to
 * add a device specific entry
 */
static usb_host_driver		*host_driver[OS_USB_MAX_DRIVERS];

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetAddress
* Returned Value : None
* Comments       :
*     Chapter 9 SetAddress command
*     See section 9.4.6 (page 192) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
static void
host_ch9SetAddress
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{
//printf("<<<host_ch9SetAddress\n"); /* XXX */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      swab16(setup_ptr->WLENGTH), 0);
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9GetDescription
* Returned Value : None
* Comments       :
*     Chapter 9 GetDescription command
*     See section 9.4.3 (page 189) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
static void
host_ch9GetDescription
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{
//printf("<<<host_ch9GetDescription: ask for %d byte descriptor\n", swab16(setup_ptr->WLENGTH)); /* XXX */
	_usb_host_send_setup(handle, pipe_id, (uchar_ptr)setup_ptr, NULL,
		       buff_ptr, swab16(setup_ptr->WLENGTH), 0);
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetConfig
* Returned Value : None
* Comments       :
*     Chapter 9 SetConfig command
*     See section 9.4.7 (page 193) of the USB 1.1 Specification.
* 
*END*--------------------------------------------------------------------*/
static void
host_ch9SetConfig
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{
//printf("<<<<host_ch9SetConfig\n"); /* XXX */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      swab16(setup_ptr->WLENGTH), 0);
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9SetFeature_host
* Returned Value : None
* Comments       :
*     Chapter 9 SetFeature command
* 
*END*--------------------------------------------------------------------*/
static void
host_ch9SetFeature
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{
//printf("<<<<host_ch9SetFeature\n"); /* XXX */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      swab16(setup_ptr->WLENGTH), 0);
}


/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : ch9Vendor
* Returned Value : 
* Comments       :
*     Chapter 9 Vendor specific command
* 
*END*--------------------------------------------------------------------*/
void
host_ch9Vendor
   (
      /* usb host handle */
      _usb_host_handle  handle,
            
      /* Pipe ID to send on */
      int_16            pipe_id,
      
      /* Device Request to send */
      USB_SETUP_PTR     setup_ptr,
            
      /* buffer to send/receive */
      uchar_ptr         buff_ptr
   )
{
//printf("<<<<host_ch9Vendor\n"); /* XXX */
   _usb_host_send_setup(handle, pipe_id, 
      (uchar_ptr)setup_ptr, NULL, buff_ptr, 
      swab16(setup_ptr->WLENGTH), 0);
}

#define	ENUM_START			0
#define	ENUM_GET_SHORT_DESC		1
#define	ENUM_GET_FULL_DESC		2
#define	ENUM_GET_SHORT_CONFIG_DESC	3
#define	ENUM_GET_FULL_CONFIG_DESC	4
#define	ENUM_GET_SHORT_STRING1		5
#define	ENUM_GET_FULL_STRING1		6
#define	ENUM_GET_SHORT_STRING2		7
#define	ENUM_GET_FULL_STRING2		8
#define	ENUM_DONE			9

static USB_SETUP _usb_get_string_desc =
{
   0x80,    /* device-to-host */
   6,       /* Get Descriptor */
#ifdef BIG_ENDIAN
   0x0003,  /* wValue: HiByte: Descriptor Type (1: Device, 2: Configuration, 
            ** 3: String, 4: Interface, 5: Endpoint), LoByte: Descriptor Index 
            */
   0,       /* wIndex: 0 (or language ID) */
   0x4000   /* wLength: Descriptor Length */
#else
   0x0300,  /* wValue: HiByte: Descriptor Type (1: Device, 2: Configuration, 
            ** 3: String, 4: Interface, 5: Endpoint), LoByte: Descriptor Index 
            */
   0,       /* wIndex: 0 (or language ID) */
   0x40     /* wLength: Descriptor Length */
#endif
};

static u16 _usb_ucode_str[USB_STR_SIZE];

/*
 * Convert LE unicode USB string descriptor to ascii,
 * making the cheesy assumption that the characters
 * reall are ascii already.
 *
 * Null terminates the receiving string if possible.
 * Returns number of characters copied, not counting
 * the terminating null.
 */
s32
ucode_to_str(u16 *ucp, char *sp, s32 size)
{
	s32 n;
	s32 count;
	u32 type;

#ifdef BIG_ENDIAN
	n = (*ucp >> 8) & 0xff;
	type = *ucp & 0xff;
#else
	n = *ucp & 0xff;
	type = (*ucp >>8) & 0xff;
#endif
	if (type != 0x3)
		printf("ucode_to_str: bad string desc type %d\n", type);

	n = MIN(size, n);
	/* skip to first unicode character */
	ucp++;

	for (count = 0; count < n; count++) {
#ifdef BIG_ENDIAN
		*sp++ = (*ucp++ >> 8) & 0xff;
#else
		*sp++ = *ucp++ & 0xff;
#endif
	}

	if (n < size)
		*sp = '\0';

	return n;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : host_process_default_pipe
* Returned Value : 
* Comments       :
*     Callback function to process completed transactions on the default pipe
* 
*END*--------------------------------------------------------------------*/
static void
host_process_default_pipe
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;

	hgp = &host_global_struct[hsp->CTLR_NUM];

//printf("<<host_process_default_pipe: len %d enum_done %d, enum_state %d\n", length, hgp->ENUMERATION_DONE, hgp->ENUMERATION_STATE);	/* XXX */
	if (hgp->ENUMERATION_DONE)
		return;

	/*
	 * State machine for device enumeration
	 */
	switch (hgp->ENUMERATION_STATE) {
	case ENUM_START:
		/* Get first 8 bytes of the device descriptor */
		/*
		 * XXX need mutual exclusion on tx_get_desc and fellows,
		 * XXX since there can be two hosts operating at once.
		 * XXX either that or make separate copies.
		 */
		tx_get_desc.WLENGTH = swab16(8);
		host_ch9GetDescription(handle, hgp->DEFAULT_PIPE, &tx_get_desc,
				(uchar_ptr)&hgp->RX_DESC_DEVICE);
		hgp->ENUMERATION_STATE = ENUM_GET_SHORT_DESC;
		break;
	case ENUM_GET_SHORT_DESC:
		/* Get full descriptor */
		tx_get_desc.WLENGTH = swab16(18);
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &tx_get_desc, 
				(uchar_ptr)&hgp->RX_DESC_DEVICE);
		hgp->ENUMERATION_STATE = ENUM_GET_FULL_DESC;
		break;
	case ENUM_GET_FULL_DESC:
		tx_get_config_desc.WLENGTH = swab16(8);
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &tx_get_config_desc, 
				(uchar_ptr)&hgp->RX_CONFIG_DESC);
		hgp->ENUMERATION_STATE = ENUM_GET_SHORT_CONFIG_DESC;
		break;
	case ENUM_GET_SHORT_CONFIG_DESC:
		/* Now use real length to get the full config desc */
		tx_get_config_desc.WLENGTH = swab16(hgp->RX_CONFIG_DESC[2]);
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &tx_get_config_desc, 
				(uchar_ptr)&hgp->RX_CONFIG_DESC);
		hgp->ENUMERATION_STATE = ENUM_GET_FULL_CONFIG_DESC;
		break;
	case ENUM_GET_FULL_CONFIG_DESC:
#ifdef BIG_ENDIAN
		_usb_get_string_desc.WVALUE = swab16(0x0301);
#else
		_usb_get_string_desc.WVALUE = 0x0103;
#endif
		_usb_get_string_desc.WLENGTH = swab16(2);
		bzero((void *)_usb_ucode_str, sizeof(_usb_ucode_str));
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &_usb_get_string_desc, 
				(uchar_ptr)_usb_ucode_str);
		hgp->ENUMERATION_STATE = ENUM_GET_SHORT_STRING1;
		break;
	case ENUM_GET_SHORT_STRING1:
		_usb_get_string_desc.WLENGTH = swab16(*(char *)_usb_ucode_str);
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &_usb_get_string_desc, 
				(uchar_ptr)_usb_ucode_str);
		hgp->ENUMERATION_STATE = ENUM_GET_FULL_STRING1;
		break;
	case ENUM_GET_FULL_STRING1:
		/* unpack ucode into an ascii string */
		ucode_to_str(_usb_ucode_str, (char *)hgp->MFG_STR, USB_STR_SIZE);
#ifdef BIG_ENDIAN
		_usb_get_string_desc.WVALUE = swab16(0x0302);
#else
		_usb_get_string_desc.WVALUE = 0x0203;
#endif
		_usb_get_string_desc.WLENGTH = swab16(2);
		bzero((void *)_usb_ucode_str, sizeof(_usb_ucode_str));
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &_usb_get_string_desc, 
				(uchar_ptr)_usb_ucode_str);
		hgp->ENUMERATION_STATE = ENUM_GET_SHORT_STRING2;
		break;
	case ENUM_GET_SHORT_STRING2:
		_usb_get_string_desc.WLENGTH = swab16(*(char *)_usb_ucode_str);
		host_ch9GetDescription(
			handle, hgp->DEFAULT_PIPE, &_usb_get_string_desc, 
				(uchar_ptr)_usb_ucode_str);
		hgp->ENUMERATION_STATE = ENUM_GET_FULL_STRING2;
		break;
	case ENUM_GET_FULL_STRING2:
		/* unpack ucode into an ascii string */
		ucode_to_str(_usb_ucode_str, (char *)hgp->PROD_STR, USB_STR_SIZE);
		hgp->ENUMERATION_STATE = ENUM_DONE;
		hgp->ENUMERATION_DONE = TRUE;
//printf("<<host_process_default_pipe: USB%d, ENUM DONE, DEV ATTACH %d\n", hsp->CTLR_NUM, hgp->DEVICE_ATTACHED);	/* XXX */
		break;
	default:
printf("<<host_process_default_pipe: unknown enum_state %d\n", hgp->ENUMERATION_STATE);	/* XXX */
		break;
	}

	return;
}

static void host_process_pipe_transaction_done (_usb_host_handle, uint_32);

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : host_process_attach
* Returned Value : 
* Comments       :
*     Callback function to process the device attach event
* 
*END*--------------------------------------------------------------------*/
static void
host_process_attach
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;

	hgp = &host_global_struct[hsp->CTLR_NUM];

printf("<<host_process_attach: len %d (set ATTACH for %d)\n", length, hsp->CTLR_NUM);	/* XXX */
	
	hgp->DEVICE_ATTACHED = TRUE;
	hgp->DEFAULT_PIPE = _usb_host_open_pipe(handle, 0, 0, USB_RECV, 0, 
				USB_CONTROL_PIPE, 64, 0, 0);

//printf("<<host_process_attach: reg svc 0x%x for pipe %d\n", host_process_default_pipe, hgp->DEFAULT_PIPE);
	_usb_host_register_service(handle, hgp->DEFAULT_PIPE, 
		host_process_default_pipe);
  
        /*
	 * First assign an address to the device
	 */	
	hgp->DEVICE_ADDRESS++;
	tx_set_address.WVALUE = swab16(hgp->DEVICE_ADDRESS);
	/* Enumerate */
//printf("<<<host_process_attach: len %d set address %d\n", length, hgp->DEVICE_ADDRESS);
      	host_ch9SetAddress(handle, hgp->DEFAULT_PIPE, &tx_set_address, 
         		(uchar_ptr)&hgp->RX_TEST_BUFFER);

	hgp->USB_CURRENT_ADDRESS = hgp->DEVICE_ADDRESS;
	hgp->ENUMERATION_DONE = FALSE;
	hgp->ENUMERATION_STATE = ENUM_START;
	hgp->REGISTER_SERVICES = TRUE;
	hgp->DEFAULT_PIPE =
	_usb_host_open_pipe(handle, hgp->USB_CURRENT_ADDRESS,
			    0, USB_RECV, 0, USB_CONTROL_PIPE,
			    hgp->RX_DESC_DEVICE.BMAXPACKETSIZE, 0, 0);
//printf("<<<host_process_attach: reg svc 0x%x for pipe %d\n", host_process_default_pipe, hgp->DEFAULT_PIPE);
        _usb_host_register_service(handle, hgp->DEFAULT_PIPE,
                  host_process_default_pipe);

//printf("<<<host_process_attach: len %d ENUM DONE %d, new pipe %d, addr %d\n", length, hgp->ENUMERATION_DONE, hgp->DEFAULT_PIPE, hgp->USB_CURRENT_ADDRESS);
	return;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : host_process_detach
* Returned Value : 
* Comments       :
*     Callback function to process the device detach event
* 
*END*--------------------------------------------------------------------*/
static void
host_process_detach
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;

	hgp = &host_global_struct[hsp->CTLR_NUM];

printf("<<host_process_detach: handle 0x%x, devnum %d, driverp 0x%x\n", hsp, hsp->CTLR_NUM, hgp->driver);

	/* Cancel any transactions in flight */
	if (hgp->driver)
		hgp->driver->funcs->detach(handle, length);

	_usb_host_close_all_pipes(handle);

	hgp->ENUMERATION_DONE = FALSE;
	hgp->DEVICE_ATTACHED = FALSE;

	/*
	 * Invalidate any currently open device handles for this host
	 */
	__usbInvalidateHandles(hsp->CTLR_NUM);
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : host_process_resume
* Returned Value : 
* Comments       :
*     Callback function to process the remote-wakeup/K-Attach event
* 
*END*--------------------------------------------------------------------*/
static void
host_process_resume
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
printf("<<<<host_process_resume\n"); /* XXX */
	return;   
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : host_process_pipe_transaction_done
* Returned Value : 
* Comments       :
*     Callback function to process the transaction done event on
*     the control pipe (ep0).
* 
*END*--------------------------------------------------------------------*/
static void
host_process_pipe_transaction_done
   (
      _usb_host_handle  handle,
      uint_32           length
   )
{
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;

	hgp = &host_global_struct[hsp->CTLR_NUM];
	hgp->TRANSACTION_COMPLETE = TRUE;

//printf("<<<host_process_pipe_transaction_done\n"); /* XXX */

       	return;
}

/*
 * USB host state machine
 *
 * Called after every host interrupt
 */
void
_usb_host_state_machine(_usb_host_handle handle)
{
	uint_8 error;
	USB_HOST_STATE_STRUCT_PTR hsp = (USB_HOST_STATE_STRUCT_PTR)handle;
	HOST_GLOBAL_STRUCT_PTR hgp;
	_usb_ext_handle *uhp;
	__OSBbUsbMesg *mp;

	hgp = &host_global_struct[hsp->CTLR_NUM];
 
	if (hgp->DEVICE_ATTACHED == 0)
		return;

	if (hgp->ENUMERATION_DONE == 0)
		return;

	if (hgp->REGISTER_SERVICES) {
//printf("\n<<_usb_host_state_machine: register new handler 0x%x for control pipe %d\n", host_process_pipe_transaction_done, hgp->DEFAULT_PIPE);
               (void) _usb_host_unregister_service(handle, hgp->DEFAULT_PIPE);
               error = _usb_host_register_service(handle, hgp->DEFAULT_PIPE,
                  host_process_pipe_transaction_done);
               if (error != USB_OK)
                  printf("\n_usb_host_state_machine: Registration failed. Error: %x\n", error);
               hgp->REGISTER_SERVICES = FALSE;
	}

	if (hgp->driver == NULL)
		return;

	/*
	 * Generic actions for all devices
	 */
	if (hgp->TRANSACTION_COMPLETE == TRUE) {
		if ((uhp = hgp->curhandle) != NULL) {
			if ((mp = uhp->uh_wr_msg) != NULL) {
				/*
				 * Send reply message
				 */
				mp->um_type |= OS_USB_REPLY_MASK;
				mp->um_ret = 0;	/* XXX */
				if (osSendMesg(mp->um_rq,
					(void *)mp, OS_MESG_NOBLOCK) < 0) {
					printf("USBMgr: reply queue full!\n");
				}
				uhp->uh_wr_msg = NULL;
			}
			if ((mp = uhp->uh_rd_msg) != NULL) {
				/*
				 * Send reply message
				 */
				mp->um_type |= OS_USB_REPLY_MASK;
				mp->um_ret = 0;	/* XXX */
				if (osSendMesg(mp->um_rq,
					(void *)mp, OS_MESG_NOBLOCK) < 0) {
					printf("USBMgr: reply queue full!\n");
				}
				uhp->uh_rd_msg = NULL;
			}
			hgp->curhandle = NULL;
			hgp->TRANSACTION_COMPLETE = FALSE;
		}
	}

	/*
	 * Device specific actions
	 */

	return;
}

/*
 * Host side of device framework (Chapter 9)
 *
 * This happens at task level in the application, not in the
 * USB Manager Thread.
 */
void
__usb_host_chapter9_negotiation(s32 which)
{
	_usb_host_handle handle;
	HOST_GLOBAL_STRUCT_PTR hgp;
	int i;

	hgp = &host_global_struct[which];
	handle = __osArcHostHandle[which];

	hgp->TRANSACTION_COMPLETE = FALSE;

//printf("\n<<__usb_host_chapter9 (DEFAULT_PIPE %d)\n", hgp->DEFAULT_PIPE);

	/*
	 * Check the device descriptor against all the registered drivers.
	 */
//printf("<<__usb_host_chapter9 ... look for matching driver\n");
	for (i = 0; i < OS_USB_MAX_DRIVERS; i++) {
		if (host_driver[i] != NULL) {
//printf("<<<< check %d ... ", i);
			if (host_driver[i]->funcs->match(
					(usb_dev_desc *)&hgp->RX_DESC_DEVICE)) {
				hgp->driver = host_driver[i];
				break;
			}
		} else {
//printf("no match.\n");
		}
	}

	if (i == OS_USB_MAX_DRIVERS)
		return;
        
	/*
	 * Found a match, do device-specific portion of Chapter 9 negotiation
	 */
	if (hgp->driver && hgp->driver->funcs)
		hgp->driver->funcs->chap9(handle);

	return;
}

/*
 * Register callback functions for host processing on one USB controller.
 * The rest of host processing is handled by the callbacks.
 */
void
__usb_arc_host_setup(int which, _usb_host_handle *handlep)
{
	uint_8 error;

	if (_usb_host_init(which, which, 16, handlep) != USB_OK) {
		printf("_usb_host_init fails\n");
		return;
	}
   
	error = _usb_host_register_service(*handlep, USB_SERVICE_ATTACH, 
			host_process_attach);
	if (error != USB_OK) {
		printf("\nUSB Host Register ATTACH failed. Error: %x", error);
	}
   
	error = _usb_host_register_service(*handlep, USB_SERVICE_DETACH, 
			host_process_detach);
	if (error != USB_OK) {
		printf("\nUSB Host Register DETACH failed. Error: %x", error);
	}
   
	error = _usb_host_register_service(*handlep, USB_SERVICE_HOST_RESUME,
			host_process_resume);
	if (error != USB_OK) {
		printf("\nUSB Host Register RESUME failed. Error: %x", error);
	}
   
	return;
}

/*
 * Register a host-side device driver
 */
void
__usb_host_driver_register(usb_host_driver *hdp)
{
	int i;

	for (i = 0; i < OS_USB_MAX_DRIVERS; i++) {
		if (host_driver[i] == NULL) {
			host_driver[i] = hdp;
			return;
		}
	}
	printf("__usb_host_driver_register out of slots\n");
	return;
}