Пример #1
0
/* parse command Set Address */
static kal_bool USB_Cmd_SetAddress(Usb_Ep0_Status *pep0state, Usb_Command *pcmd)
{
	kal_bool bError = KAL_FALSE;

	/* Store device function address until status stage of request */
	if (pcmd->bmRequestType != USB_CMD_STDDEVOUT)
	{
		bError = KAL_TRUE;
	}
	else
	{
		if (gUsbDevice.nDevState <= DEVSTATE_ADDRESS)
		{
			pep0state->byFAddr = (kal_uint8)pcmd->wValue;
			if ((gUsbDevice.nDevState == DEVSTATE_DEFAULT) && (pep0state->byFAddr<=127))
			{
				gUsbDevice.nDevState = DEVSTATE_SET_ADDRESS;
				USB_SetAddress(pep0state->byFAddr, USB_SET_ADDR_DATA);
			}
			else
			{
				gUsbDevice.nDevState = DEVSTATE_DEFAULT;
			}
		}
		else
		{
			bError = KAL_TRUE;
		}	
	}

	return bError;
}
Пример #2
0
void USB_EndPoint0 (uint32_t event) {

  switch (event) {
    case USB_EVT_SETUP:
      USB_SetupStage();
      USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
      EP0Data.Count = SetupPacket.wLength;     /* Number of bytes to transfer */
      switch (SetupPacket.bmRequestType.BM.Type) {

        case REQUEST_STANDARD:
          switch (SetupPacket.bRequest) {
            case USB_REQUEST_GET_STATUS:
              if (!USB_ReqGetStatus()) {
                goto stall_i;
              }
              USB_DataInStage();
              break;

            case USB_REQUEST_CLEAR_FEATURE:
              if (!USB_ReqSetClrFeature(0)) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_FEATURE_EVENT
              USB_Feature_Event();
#endif
              break;

            case USB_REQUEST_SET_FEATURE:
              if (!USB_ReqSetClrFeature(1)) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_FEATURE_EVENT
              USB_Feature_Event();
#endif
              break;

            case USB_REQUEST_SET_ADDRESS:
              if (!USB_ReqSetAddress()) {
                goto stall_i;
              }
              USB_StatusInStage();
              break;

            case USB_REQUEST_GET_DESCRIPTOR:
              if (!USB_ReqGetDescriptor()) {
                goto stall_i;
              }
              USB_DataInStage();
              break;

            case USB_REQUEST_SET_DESCRIPTOR:
/*stall_o:*/  USB_SetStallEP(0x00);            /* not supported */
              EP0Data.Count = 0;
              break;

            case USB_REQUEST_GET_CONFIGURATION:
              if (!USB_ReqGetConfiguration()) {
                goto stall_i;
              }
              USB_DataInStage();
              break;

            case USB_REQUEST_SET_CONFIGURATION:
              if (!USB_ReqSetConfiguration()) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_CONFIGURE_EVENT
              USB_Configure_Event();
#endif
              break;

            case USB_REQUEST_GET_INTERFACE:
              if (!USB_ReqGetInterface()) {
                goto stall_i;
              }
              USB_DataInStage();
              break;

            case USB_REQUEST_SET_INTERFACE:
              if (!USB_ReqSetInterface()) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_INTERFACE_EVENT
              USB_Interface_Event();
#endif
              break;

            default:
              goto stall_i;
          }
          break;  /* end case REQUEST_STANDARD */

#if USB_CLASS
        case REQUEST_CLASS:
          switch (SetupPacket.bmRequestType.BM.Recipient) {

            case REQUEST_TO_DEVICE:
              goto stall_i;                                              /* not supported */

            case REQUEST_TO_INTERFACE:
#if USB_HID
              if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {           /* IF number correct? */
                switch (SetupPacket.bRequest) {
                  case HID_REQUEST_GET_REPORT:
                    if (HID_GetReport()) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_REPORT:
                    EP0Data.pData = EP0Buf;                              /* data to be received */ 
                    goto setup_class_ok;
                  case HID_REQUEST_GET_IDLE:
                    if (HID_GetIdle()) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_IDLE:
                    if (HID_SetIdle()) {
                      USB_StatusInStage();                               /* send Acknowledge */
                      goto setup_class_ok;
                    }
                    break;
                  case HID_REQUEST_GET_PROTOCOL:
                    if (HID_GetProtocol()) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_PROTOCOL:
                    if (HID_SetProtocol()) {
                      USB_StatusInStage();                               /* send Acknowledge */
                      goto setup_class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_HID */
#if USB_MSC
              if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {           /* IF number correct? */
                switch (SetupPacket.bRequest) {
                  case MSC_REQUEST_RESET:
                    if ((SetupPacket.wValue.W == 0) &&	                 /* RESET with invalid parameters -> STALL */
                        (SetupPacket.wLength  == 0)) {
                      if (MSC_Reset()) {
                        USB_StatusInStage();
                        goto setup_class_ok;
                      }
                    }
                    break;
                  case MSC_REQUEST_GET_MAX_LUN:
                    if ((SetupPacket.wValue.W == 0) &&	                 /* GET_MAX_LUN with invalid parameters -> STALL */
                        (SetupPacket.wLength  == 1)) { 
                      if (MSC_GetMaxLUN()) {
                        EP0Data.pData = EP0Buf;
                        USB_DataInStage();
                        goto setup_class_ok;
                      }
                    }
                    break;
                }
              }
#endif  /* USB_MSC */
#if USB_AUDIO
              if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||       /* IF number correct? */
                  (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
                  (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
                switch (SetupPacket.bRequest) {
                  case AUDIO_REQUEST_GET_CUR:
                  case AUDIO_REQUEST_GET_MIN:
                  case AUDIO_REQUEST_GET_MAX:
                  case AUDIO_REQUEST_GET_RES:
                    if (ADC_IF_GetRequest()) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case AUDIO_REQUEST_SET_CUR:
//                case AUDIO_REQUEST_SET_MIN:
//                case AUDIO_REQUEST_SET_MAX:
//                case AUDIO_REQUEST_SET_RES:
                    EP0Data.pData = EP0Buf;                              /* data to be received */ 
                    goto setup_class_ok;
                }
              }
#endif  /* USB_AUDIO */
#if USB_CDC
              if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  ||       /* IF number correct? */
                  (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
                switch (SetupPacket.bRequest) {
                  case CDC_SEND_ENCAPSULATED_COMMAND:
                    EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
                    goto setup_class_ok;
                  case CDC_GET_ENCAPSULATED_RESPONSE:
                    if (CDC_GetEncapsulatedResponse()) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case CDC_SET_COMM_FEATURE:
                    EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
                    goto setup_class_ok;
                  case CDC_GET_COMM_FEATURE:
                    if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case CDC_CLEAR_COMM_FEATURE:
                    if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
                      USB_StatusInStage();                               /* send Acknowledge */
                      goto setup_class_ok;
                    }
                    break;
                  case CDC_SET_LINE_CODING:
                    EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
                    goto setup_class_ok;
                  case CDC_GET_LINE_CODING:
                    if (CDC_GetLineCoding()) {
                      EP0Data.pData = EP0Buf;                            /* point to data to be sent */
                      USB_DataInStage();                                 /* send requested data */
                      goto setup_class_ok;
                    }
                    break;
                  case CDC_SET_CONTROL_LINE_STATE:
                    if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
                      USB_StatusInStage();                               /* send Acknowledge */
                      goto setup_class_ok;
                    }
                    break;
                  case CDC_SEND_BREAK:
                    if (CDC_SendBreak(SetupPacket.wValue.W)) {
                      USB_StatusInStage();                               /* send Acknowledge */
                      goto setup_class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_CDC */
#if USB_UVC
              if ((SetupPacket.wIndex.WB.L == USB_UVC_VCIF_NUM)  ||
                  (SetupPacket.wIndex.WB.L == USB_UVC_VSIF_NUM)) {
                switch (SetupPacket.bRequest) {
                  case SET_CUR:
                    EP0Data.pData = EP0Buf;      /* data to be stored, see USB_EVT_OUT */
                    goto setup_class_ok;
                    break;
                  case GET_CUR:
                    if (UVC_GetCur()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                  case GET_MIN:
                    if (UVC_GetMin()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                  case GET_MAX:
                    if (UVC_GetMax()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                  case GET_RES:
                    if (UVC_GetRes()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                  case GET_LEN:
                    if (UVC_GetLen()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                  case GET_INFO:
                    if (UVC_GetInfo()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                  case GET_DEF:
                    if (UVC_GetDef()) {
                      EP0Data.pData = EP0Buf;    /* data to be sent */
                      USB_DataInStage();         /* send data */
                      goto setup_class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_UVC */
              goto stall_i;                                              /* not supported */
              /* end case REQUEST_TO_INTERFACE */

            case REQUEST_TO_ENDPOINT:
#if USB_AUDIO
              switch (SetupPacket.bRequest) {
                case AUDIO_REQUEST_GET_CUR:
                case AUDIO_REQUEST_GET_MIN:
                case AUDIO_REQUEST_GET_MAX:
                case AUDIO_REQUEST_GET_RES:
                  if (ADC_EP_GetRequest()) {
                    EP0Data.pData = EP0Buf;                              /* point to data to be sent */
                    USB_DataInStage();                                   /* send requested data */
                    goto setup_class_ok;
                  }
                  break;
                case AUDIO_REQUEST_SET_CUR:
//              case AUDIO_REQUEST_SET_MIN:
//              case AUDIO_REQUEST_SET_MAX:
//              case AUDIO_REQUEST_SET_RES:
                  EP0Data.pData = EP0Buf;                                /* data to be received */ 
                  goto setup_class_ok;
              }
#endif  /* USB_AUDIO */
              goto stall_i;
              /* end case REQUEST_TO_ENDPOINT */

            default:
              goto stall_i;
          }
setup_class_ok:                                                          /* request finished successfully */
          break;  /* end case REQUEST_CLASS */
#endif  /* USB_CLASS */

#if USB_VENDOR
        case REQUEST_VENDOR:
          switch (SetupPacket.bmRequestType.BM.Recipient) {

            case REQUEST_TO_DEVICE:
              if (!USB_ReqVendorDev(TRUE)) {
                goto stall_i;                                            /* not supported */               
              }
              break;

            case REQUEST_TO_INTERFACE:
              if (!USB_ReqVendorIF(TRUE)) {
                goto stall_i;                                            /* not supported */               
              }
              break;

            case REQUEST_TO_ENDPOINT:
              if (!USB_ReqVendorEP(TRUE)) {
                goto stall_i;                                            /* not supported */               
              }
              break;

            default:
              goto stall_i;
          }

          if (SetupPacket.wLength) {
            if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
              USB_DataInStage();
            }
          } else {
            USB_StatusInStage();
          }

          break;  /* end case REQUEST_VENDOR */ 
#endif  /* USB_VENDOR */

        default:
stall_i:  USB_SetStallEP(0x80);
          EP0Data.Count = 0;
          break;
      }
      break;  /* end case USB_EVT_SETUP */

    case USB_EVT_OUT:
      if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
        if (EP0Data.Count) {                                             /* still data to receive ? */
          USB_DataOutStage();                                            /* receive data */
          if (EP0Data.Count == 0) {                                      /* data complete ? */
            switch (SetupPacket.bmRequestType.BM.Type) {

              case REQUEST_STANDARD:
                goto stall_i;                                            /* not supported */

#if (USB_CLASS) 
              case REQUEST_CLASS:
                switch (SetupPacket.bmRequestType.BM.Recipient) {
                  case REQUEST_TO_DEVICE:
                    goto stall_i;                                        /* not supported */

                  case REQUEST_TO_INTERFACE:
#if USB_HID
                    if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {     /* IF number correct? */
                      switch (SetupPacket.bRequest) {
                        case HID_REQUEST_SET_REPORT:
                          if (HID_SetReport()) {
                            USB_StatusInStage();                         /* send Acknowledge */
                            goto out_class_ok;
                          }
                          break;
                      }
                    }
#endif  /* USB_HID */  
#if USB_AUDIO
                    if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  || /* IF number correct? */
                        (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
                        (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
                      switch (SetupPacket.bRequest) {
                        case AUDIO_REQUEST_SET_CUR:
//                      case AUDIO_REQUEST_SET_MIN:
//                      case AUDIO_REQUEST_SET_MAX:
//                      case AUDIO_REQUEST_SET_RES:
                          if (ADC_IF_SetRequest()) {
                            USB_StatusInStage();                         /* send Acknowledge */
                            goto out_class_ok;
                          }
                          break;
                      }
                    }
#endif  /* USB_AUDIO */
#if USB_CDC
                    if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  || /* IF number correct? */
                        (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
                      switch (SetupPacket.bRequest) {
                        case CDC_SEND_ENCAPSULATED_COMMAND:
                          if (CDC_SendEncapsulatedCommand()) {
                            USB_StatusInStage();                         /* send Acknowledge */
                            goto out_class_ok;
                          }
                          break;
                        case CDC_SET_COMM_FEATURE:
                          if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
                            USB_StatusInStage();                         /* send Acknowledge */
                            goto out_class_ok;
                          }
                          break;
                        case CDC_SET_LINE_CODING:
                          if (CDC_SetLineCoding()) {
                            USB_StatusInStage();                         /* send Acknowledge */
                            goto out_class_ok;
                          }
                          break;
                      }
                    } 
#endif  /* USB_CDC */
#if USB_UVC
                    if ((SetupPacket.wIndex.WB.L == USB_UVC_VCIF_NUM)  ||
                        (SetupPacket.wIndex.WB.L == USB_UVC_VSIF_NUM)) {
                      switch (SetupPacket.bRequest) {
                        case SET_CUR:
                          if (!UVC_SetCur()) {
                            goto stall_i;
                          }
                          break;
                      } /* end switch (SetupPacket.bRequest) */ 
                      break;
                    }
#endif /* USB_UVC */
                    goto stall_i;
                    /* end case REQUEST_TO_INTERFACE */

                  case REQUEST_TO_ENDPOINT:
#if USB_AUDIO
                    switch (SetupPacket.bRequest) {
                      case AUDIO_REQUEST_SET_CUR:
//                    case AUDIO_REQUEST_SET_MIN:
//                    case AUDIO_REQUEST_SET_MAX:
//                    case AUDIO_REQUEST_SET_RES:
                        if (ADC_EP_SetRequest()) {
                          USB_StatusInStage();                           /* send Acknowledge */
                          goto out_class_ok;
                        }
                        break;
                    }
#endif  /* USB_AUDIO */
                    goto stall_i;
                    /* end case REQUEST_TO_ENDPOINT */

                  default:
                    goto stall_i;
                }
out_class_ok:                                                            /* request finished successfully */
                break; /* end case REQUEST_CLASS */
#endif  /* USB_CLASS */

#if USB_VENDOR
              case REQUEST_VENDOR:
                switch (SetupPacket.bmRequestType.BM.Recipient) {
      
                  case REQUEST_TO_DEVICE:
                    if (!USB_ReqVendorDev(FALSE)) {
                      goto stall_i;                                      /* not supported */               
                    }
                    break;
      
                  case REQUEST_TO_INTERFACE:
                    if (!USB_ReqVendorIF(FALSE)) {
                      goto stall_i;                                      /* not supported */               
                    }
                    break;
      
                  case REQUEST_TO_ENDPOINT:
                    if (!USB_ReqVendorEP(FALSE)) {
                      goto stall_i;                                      /* not supported */               
                    }
                    break;
      
                  default:
                    goto stall_i;
                }
      
                USB_StatusInStage();
      
                break;  /* end case REQUEST_VENDOR */ 
#endif  /* USB_VENDOR */

              default:
                goto stall_i;
            }
          }
        }
      } else {
        USB_StatusOutStage();                                            /* receive Acknowledge */
      }
      break;  /* end case USB_EVT_OUT */

    case USB_EVT_IN :
      if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
        USB_DataInStage();                                               /* send data */
      } else {
        if (USB_DeviceAddress & 0x80) {
          USB_DeviceAddress &= 0x7F;
          USB_SetAddress(USB_DeviceAddress);
        }
      }
      break;  /* end case USB_EVT_IN */

    case USB_EVT_OUT_STALL:
      USB_ClrStallEP(0x00);
      break;

    case USB_EVT_IN_STALL:
      USB_ClrStallEP(0x80);
      break;

  }
}
Пример #3
0
void USB_EndPoint0 (uint32_t event)
{
	switch (event)
	{
		case USB_EVT_SETUP:
			USB_SetupStage();
			EP0Data.Count = SetupPacket.wLength;
			switch (SetupPacket.bmRequestType.BM.Type)
			{
				case REQUEST_STANDARD:
					switch (SetupPacket.bRequest)
					{
						case USB_REQUEST_SET_ADDRESS:
							switch (SetupPacket.bmRequestType.BM.Recipient)
							{
								case REQUEST_TO_DEVICE:
									USB_DeviceAddress = SetupPacket.wValue.WB.L;//0x80 | SetupPacket.wValue.WB.L;
									USB_StatusInStage();
									USB_SetAddress(USB_DeviceAddress);
									break;
								default:
									break;
              				}
              				break;

						case USB_REQUEST_GET_DESCRIPTOR:
							USB_GetDescriptor();
							break;

					/*	case USB_REQUEST_GET_CONFIGURATION:
							switch (SetupPacket.bmRequestType.BM.Recipient)
							{
								case REQUEST_TO_DEVICE:
									EP0Data.pData = &USB_Configuration;
									USB_DataInStage();
									break;
								default:
									break;
							}
							break;	*/

						case USB_REQUEST_SET_CONFIGURATION:
							switch (SetupPacket.bmRequestType.BM.Recipient)
							{
								case REQUEST_TO_DEVICE:
									if (!USB_SetConfiguration())
									{
										USB_SetStallEP(0x80);
										EP0Data.Count = 0;
										break;
									}
									USB_StatusInStage();
#if USB_CONFIGURE_EVENT
                  					USB_Configure_Event();
#endif
									break;
								default:
									break;
							}
							break;

					default:
						break;

					}
					break;

				case REQUEST_CLASS:
#if USB_CLASS
					switch (SetupPacket.bmRequestType.BM.Recipient)
					{
						case REQUEST_TO_INTERFACE:
#if USB_HID
							if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM)
							{
								switch (SetupPacket.bRequest)
								{
									case HID_REQUEST_GET_REPORT:
										if (HID_GetReport())
										{
											EP0Data.pData = EP0Buf;
											USB_DataInStage();
										}
										break;
									case HID_REQUEST_SET_REPORT:
										EP0Data.pData = EP0Buf;
										break;
									case HID_REQUEST_GET_IDLE:
										if (HID_GetIdle())
										{
											EP0Data.pData = EP0Buf;
											USB_DataInStage();
										}
										break;
									case HID_REQUEST_SET_IDLE:
										if (HID_SetIdle())
										{
											USB_StatusInStage();
										}
										break;
									case HID_REQUEST_GET_PROTOCOL:
										if (HID_GetProtocol())
										{
											EP0Data.pData = EP0Buf;
											USB_DataInStage();
										}
										break;
									case HID_REQUEST_SET_PROTOCOL:
										if (HID_SetProtocol())
										{
											USB_StatusInStage();
										}
										break;
									default:
										break;
								}
							}
#endif  /* USB_HID */
						default:
							break;
					}
					break;
#else
#endif  /* USB_CLASS */

				case REQUEST_VENDOR:
					break;

				default:
					USB_SetStallEP(0x80);
					EP0Data.Count = 0;
					break;
			}
			break;

		case USB_EVT_OUT:
			if (SetupPacket.bmRequestType.BM.Dir == 0)
			{
				if (EP0Data.Count)
				{
					USB_DataOutStage();
					if (EP0Data.Count == 0)
					{
						switch (SetupPacket.bmRequestType.BM.Type)
						{
							case REQUEST_STANDARD:
								USB_SetStallEP(0x80);
								//EP0Data.Count = 0;
								break;
#if (USB_CLASS)
							case REQUEST_CLASS:
								switch (SetupPacket.bmRequestType.BM.Recipient)
								{
									case REQUEST_TO_INTERFACE:
#if USB_HID
										if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM)
										{
											if (!HID_SetReport())
											{
												USB_SetStallEP(0x80);
												//EP0Data.Count = 0;
												//break;
											}
											break;
											//HID_SetReport();
										}
#endif
										USB_SetStallEP(0x80);
										//EP0Data.Count = 0;
										break;
									case REQUEST_TO_ENDPOINT:
										USB_SetStallEP(0x80);
										//EP0Data.Count = 0;
										break;
									default:
										USB_SetStallEP(0x80);
										//EP0Data.Count = 0;
										break;
								}
								break;
#endif
							default:
								USB_SetStallEP(0x80);
								//EP0Data.Count = 0;
								break;
						}
						USB_StatusInStage();
					}
				}
			}
			else
			{
				;//USB_StatusOutStage();
			}


			break;

		case USB_EVT_IN:
			if (SetupPacket.bmRequestType.BM.Dir == 1)
			{
				if(EP0Data.Count > 0) // ysq add
					USB_DataInStage();
			}
			//else
			//{
			//	if (USB_DeviceAddress & 0x80)
			//	{
			//		USB_DeviceAddress &= 0x7F;
			//		USB_SetAddress(USB_DeviceAddress);
			//	}
			//}
			break;

		case USB_EVT_IN_STALL:
			USB_ClrStallEP(0x80);
			break;

		case USB_EVT_OUT_STALL:
			USB_ClrStallEP(0x00);
			break;
	}
}
Пример #4
0
/* EP0 interrupt handler called by USB_HISR */
static void USB_Endpoint0_Hdlr(void)
{
	kal_bool b_transaction_end;
	kal_bool b_sent_stall;
	kal_uint32 nCount;

	USB_Get_EP0_Status(&b_transaction_end, &b_sent_stall);

	/* Check for SentStall */
	/* SentStall && SetupEnd are impossible to occur together*/
	if (b_sent_stall == KAL_TRUE) 
	{
		USB_Update_EP0_State(USB_EP0_DRV_STATE_CLEAR_SENT_STALL, KAL_FALSE, KAL_FALSE);
		gUsbDevice.ep0_state = USB_EP0_IDLE;
	}

	/* Check for SetupEnd */
	if (b_transaction_end == KAL_TRUE) 
	{
		USB_Update_EP0_State(USB_EP0_DRV_STATE_TRANSACTION_END, KAL_FALSE, KAL_FALSE);
		gUsbDevice.ep0_state = USB_EP0_IDLE;
	}

	/* Call relevant routines for endpoint 0 state */
	if (gUsbDevice.ep0_state == USB_EP0_IDLE)
	{
		/* receive command request */
		nCount = USB_EP0_Pkt_Len();
		if (nCount > 0) 
		{
			/* idle state handler */
			USB_Endpoint0_Idle();
		}
	}
	else if (gUsbDevice.ep0_state == USB_EP0_RX)
	{
		/* Rx state handler */
		USB_Endpoint0_Rx();
	}
	else if (gUsbDevice.ep0_state == USB_EP0_RX_STATUS)
	{
		/* Data stage is RX, status stage is TX*/
		if(gUsbDevice.nDevState == DEVSTATE_SET_ADDRESS)
		{
			USB_SetAddress(gUsbDevice.ep0info.byFAddr, USB_SET_ADDR_STATUS);
			gUsbDevice.nDevState = DEVSTATE_ADDRESS;
		}
		gUsbDevice.ep0_state = USB_EP0_IDLE;
		
		/* In case next setup followed the previous status very fast and interrupt only happens once*/
		/* receive command request */
		nCount = USB_EP0_Pkt_Len();
		if (nCount > 0) 
		{
			/* idle state handler */
			USB_Endpoint0_Idle();
		}
	}

	/* must use if, not else if, EP0 may enter TX state in previous IDLE state handler */
	if (gUsbDevice.ep0_state == USB_EP0_TX)
	{
		/* Tx state handler */
		USB_Endpoint0_Tx();
	}
}
Пример #5
0
void USB_EndPoint0 (uint32_t event) {

  switch (event) {

    case USB_EVT_SETUP:
      USB_SetupStage();
      USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
      EP0Data.Count = SetupPacket.wLength;
      switch (SetupPacket.bmRequestType.BM.Type) {

        case REQUEST_STANDARD:
          switch (SetupPacket.bRequest) {

            case USB_REQUEST_GET_STATUS:
              if (!USB_GetStatus()) {
                goto stall_i;
              }
              break;

            case USB_REQUEST_CLEAR_FEATURE:
              if (!USB_SetClrFeature(0)) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_FEATURE_EVENT
              USB_Feature_Event();
#endif
              break;

            case USB_REQUEST_SET_FEATURE:
              if (!USB_SetClrFeature(1)) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_FEATURE_EVENT
              USB_Feature_Event();
#endif
              break;

            case USB_REQUEST_SET_ADDRESS:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_DEVICE:
                  USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
                  USB_StatusInStage();
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_GET_DESCRIPTOR:
              if (!USB_GetDescriptor()) {
                goto stall_i;
              }
              break;

            case USB_REQUEST_SET_DESCRIPTOR:
/*stall_o:*/  USB_SetStallEP(0x00);
              EP0Data.Count = 0;
              break;

            case USB_REQUEST_GET_CONFIGURATION:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_DEVICE:
                  EP0Data.pData = &USB_Configuration;
                  USB_DataInStage();
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_SET_CONFIGURATION:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_DEVICE:
                  if (!USB_SetConfiguration()) {
                    goto stall_i;
                  }
                  USB_StatusInStage();
#if USB_CONFIGURE_EVENT
                  USB_Configure_Event();
#endif
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_GET_INTERFACE:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_INTERFACE:
                  if ((USB_Configuration != 0) &&
                      (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
                    EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
                    USB_DataInStage();
                  } else {
                    goto stall_i;
                  }
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_SET_INTERFACE:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_INTERFACE:
                  if (!USB_SetInterface()) {
                    goto stall_i;
                  }
                  USB_StatusInStage();
#if USB_INTERFACE_EVENT
                  USB_Interface_Event();
#endif
                  break;
                default:
                  goto stall_i;
              }
              break;

            default:
              goto stall_i;

          }
          break;

        case REQUEST_CLASS:
#if USB_CLASS
          switch (SetupPacket.bmRequestType.BM.Recipient) {
            case REQUEST_TO_INTERFACE:
#if USB_HID
              if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
                switch (SetupPacket.bRequest) {
                  case HID_REQUEST_GET_REPORT:
                    if (HID_GetReport()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_REPORT:
                    EP0Data.pData = EP0Buf;
                    goto class_ok;
                  case HID_REQUEST_GET_IDLE:
                    if (HID_GetIdle()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_IDLE:
                    if (HID_SetIdle()) {
                      USB_StatusInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_GET_PROTOCOL:
                    if (HID_GetProtocol()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_PROTOCOL:
                    if (HID_SetProtocol()) {
                      USB_StatusInStage();
                      goto class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_HID */
#if USB_MSC
              if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {
                switch (SetupPacket.bRequest) {
                  case MSC_REQUEST_RESET:
                    if (MSC_Reset()) {
                      USB_StatusInStage();
                      goto class_ok;
                    }
                    break;
                  case MSC_REQUEST_GET_MAX_LUN:
                    if (MSC_GetMaxLUN()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_MSC */
#if USB_AUDIO
              if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||
                  (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
                  (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
                if (SetupPacket.bmRequestType.BM.Dir) {
                  if (ADC_IF_GetRequest()) {
                    EP0Data.pData = EP0Buf;
                    USB_DataInStage();
                    goto class_ok;
                  }
                } else {
                  EP0Data.pData = EP0Buf;
                  goto class_ok;
                }
              }
#endif  /* USB_AUDIO */
              goto stall_i;
#if USB_AUDIO
            case REQUEST_TO_ENDPOINT:
              if (SetupPacket.bmRequestType.BM.Dir) {
                if (ADC_EP_GetRequest()) {
                  EP0Data.pData = EP0Buf;
                  USB_DataInStage();
                  goto class_ok;
                }
              } else {
                EP0Data.pData = EP0Buf;
                goto class_ok;
              }
              goto stall_i;
#endif  /* USB_AUDIO */
            default:
              goto stall_i;
          }
class_ok: break;
#else
          goto stall_i;
#endif  /* USB_CLASS */

        case REQUEST_VENDOR:
          goto stall_i;

        default:
stall_i:  USB_SetStallEP(0x80);
          EP0Data.Count = 0;
          break;

      }
      break;

    case USB_EVT_OUT:
      if (SetupPacket.bmRequestType.BM.Dir == 0) {
        if (EP0Data.Count) {
          USB_DataOutStage();
          if (EP0Data.Count == 0) {
            switch (SetupPacket.bmRequestType.BM.Type) {
              case REQUEST_STANDARD:
                goto stall_i;
#if (USB_CLASS)
              case REQUEST_CLASS:
                switch (SetupPacket.bmRequestType.BM.Recipient) {
                  case REQUEST_TO_INTERFACE:
#if USB_HID
                    if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
                      if (!HID_SetReport()) {
                        goto stall_i;
                      }
                      break;
                    }
#endif
#if USB_AUDIO
                    if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||
                        (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
                        (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
                      if (!ADC_IF_SetRequest()) {
                        goto stall_i;
                      }
                      break;
                    }
#endif
                    goto stall_i;
                  case REQUEST_TO_ENDPOINT:
#if USB_AUDIO
                    if (ADC_EP_SetRequest()) break;
#endif
                    goto stall_i;
                  default:
                    goto stall_i;
                }
                break;
#endif
              default:
                goto stall_i;
            }
            USB_StatusInStage();
          }
        }
      } else {
        USB_StatusOutStage();
      }
      break;

    case USB_EVT_IN:
      if (SetupPacket.bmRequestType.BM.Dir == 1) {
        USB_DataInStage();
      } else {
        if (USB_DeviceAddress & 0x80) {
          USB_DeviceAddress &= 0x7F;
          USB_SetAddress(USB_DeviceAddress);
        }
      }
      break;

    case USB_EVT_IN_STALL:
      USB_ClrStallEP(0x80);
      break;

    case USB_EVT_OUT_STALL:
      USB_ClrStallEP(0x00);
      break;

  }
}