⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbch9.c

📁 printer usb
💻 C
📖 第 1 页 / 共 5 页
字号:
{
    static Uint8 data8_to_send;
    static Uint8 HIDduration[4] = {0, 0, 0, 0};
    Uint8 ReportID;

    switch (usb_setup.REQUESTTYPE & 0x60) {
      case 0x00:
        switch (usb_setup.REQUEST) {
          /* get status */
          case 0:
            if (setup)
                GetStatus();
            break;

          /* clear feature */
          case 1:
            if (setup)
                ClearFeature();
            break;

          /*   2 reserved */
          case 2:
            STALL_ENDPOINT_0;
            return;

          /* set feature */
          case 3:
            SetFeature(setup);
            break;

          /* set address */
          case 5:
            SetAddress(setup);
            break;

          /* get descriptor */
          case 6:
            if (setup)
                GetDescriptor();
            break;

          /* set descriptor */
          case 7:
            /* not supported */
            STALL_ENDPOINT_0;
            return;

          /* get configuration */
          case 8:
            if (setup) {
                /* Return the currently selected configuration. */
                USB_ISR_io_request(EP0_SEND, &usb_state.USB_CURR_CONFIG, 1);

                /* status phase */
                USB_ISR_io_request(EP0_RECV, 0, 0);
            }
            break;

          /* set configuration */
          case 9:
            if (setup)
                SetConfig();
            break;

          /* get interface */
          case 10:
            if (setup)
                GetInterface();
            break;

          /* set interface */
          case 11:
            if (setup)
                SetInterface();
            break;

          /* synch frame */
          case 12:
          default:
            /* We don't support isochronous transfer endpoints; just stall. */
            STALL_ENDPOINT_0;
            return;
        }
        break;

      case 0x20:
        if (setup) {
            Uint16 length = usb_setup.LENGTH;

            switch (usb_setup.REQUEST) {

            /* class specific request - Printer Class */
              /* GetDeviceId */
              case 0:
                /*
                 * Device ID string allowed to vary with config/interface/
                 * alternate setting (although it currently does not):
                 *     config       = usb_setup.VALUE & 0xFF;
                 *     interface_no = usb_setup.INDEX >>8;
                 *     alternate    = usb_setup.INDEX & 0xFF;
                 */
                if (length > ((Uint32)USB_Conf.DeviceID[0] * 256 +
                              (Uint32)USB_Conf.DeviceID[1]))
                {
                    length = ((Uint32)USB_Conf.DeviceID[0] * 256 +
                              (Uint32)USB_Conf.DeviceID[1]);
                }
                USB_ISR_io_request(EP0_SEND, USB_Conf.DeviceID, length);

                /* status phase */
                USB_ISR_io_request(EP0_RECV, 0, 0);
                break;

              /* GetPortStatus */
              case 1:
                data8_to_send = GetPrinterStatus();
                USB_ISR_io_request(EP0_SEND, &data8_to_send, 1);

                /* status phase */
                USB_ISR_io_request(EP0_RECV, 0, 0);
                break;

              case 3:   /* Legacy support */
                /* status phase */
                USB_ISR_io_request(EP0_SEND, 0, 0);

                /*
                 * Soft reset "flushes all buffers and resets the
                 * Bulk Out and Bulk In pipes to their default states.
                 * This request clears all stall conditions.  This reset
                 * does NOT change the USB addressing or USB configuration.
                 */

                /* Return any queued output buffers to their home pools. */
                FlushOutput(1);
                break;

            /* class specific request - HID Class */
              /* HID GET IDLE */
              case 2:
                ReportID = usb_setup.VALUE & 0xff;
                if (usb_setup.INDEX == HID_IFACE && ReportID < 4) {
                    USB_ISR_io_request(EP0_SEND, &HIDduration[ReportID], 1);

                    /* status phase */
                    USB_ISR_io_request(EP0_RECV, 0, 0);
                }
                else {
                    STALL_ENDPOINT_0;
                    return;
                }
                break;

              /* HID SET IDLE */
              case 10:
                /*
                 * this tells HID interrupt to only report when there is
                 * a status change that merits reporting. we always operate
                 * in this mode.
                 */
                ReportID = usb_setup.VALUE & 0xff;
                if (usb_setup.INDEX == HID_IFACE && ReportID < 4) {
                    HIDduration[ReportID] = usb_setup.VALUE >> 8;

                    /* status phase */
                    USB_ISR_io_request(EP0_SEND, 0, 0);
                }
                else {
                    STALL_ENDPOINT_0;
                    return;
                }
                break;

#ifdef MEMORY_CARD_READER
            /* class specific request - Mass Storage Class */
              /* Get Max LUN */
              case 0xfe:
                /* status phase */
                data8_to_send = 0;
                USB_ISR_io_request(EP0_SEND, &data8_to_send, 1);

                /* status phase */
                USB_ISR_io_request(EP0_RECV, 0, 0);
                break;


              /* Bulk-Only Mass Storage Reset */
              case 0xff:
                /* status phase */
                USB_ISR_io_request(EP0_SEND, 0, 0);
                break;
#endif
              default:
                /* class specific request */
                STALL_ENDPOINT_0;
                return;
            }
            break;
        }
        break;

      case 0x40:
        if (setup) {
            /* Vendor specific request */
            switch (usb_setup.REQUEST) {
              default:
                /* vendor specific request */
                STALL_ENDPOINT_0;
                return;
            }
        }
        break;

      default:
        STALL_ENDPOINT_0;
        return;
    }
}

/**************************************************************************
*
*  Function Name: GetStatus
*  Comments:
*    Chapter 9 GetStatus command
*
**************************************************************************/
static void GetStatus(void)
{
    Uint16 usb_status16;
    static Uint8 usb_status[2];

    switch (usb_setup.REQUESTTYPE) {
      /* Device request */
      case 0x80:
         /* device status: | bit1 REMOTE WAKEUP | bit 0 SELF-POWERED | */
        usb_status[0] = usb_state.USB_DEVICE_STATE & 0xff;
        usb_status[1] = usb_state.USB_DEVICE_STATE >> 8;

        /* Send the requested data */
        USB_ISR_io_request(EP0_SEND, usb_status, 2);
        break;

      /* Interface request */
      case 0x81:
        /*
         * USB 2.0 says reserved field, always return zero (pg. 255)
         * however, we do check the interface number in wIndex and
         * stall if the interface doesn't exit (not interface 0).
         */
        if (usb_setup.INDEX > MAX_IFACE) {
            STALL_ENDPOINT_0;
            return;
        }

        usb_status[0] = usb_status[1] = 0;
        USB_ISR_io_request(EP0_SEND, usb_status, 2);
        break;

      /* Endpoint request */
      case 0x82:
        /* check endpoint no. and stall if not configured */
        if (CheckEP(usb_setup.INDEX))
            return;

        /* get endpoint status (bit 0 - Halt) */
        usb_status16 = USB_ISR_get_endpoint_status(usb_setup.INDEX);
        usb_status[0] = usb_status16 & 0xff;
        usb_status[1] = usb_status16 >> 8;

        /* Send the requested data */
        USB_ISR_io_request(EP0_SEND, usb_status, 2);
        break;

      default:
        /* Unknown request */
        STALL_ENDPOINT_0;
        return;
    }

    /* status phase */
    USB_ISR_io_request(EP0_RECV, 0, 0);
}

/**************************************************************************
*
*  Function Name: ClearFeature
*  Comments:
*    Chapter 9 ClearFeature command
*
**************************************************************************/
static void ClearFeature(void)
{
    if (usb_state.USB_STATE != USB_STATE_CONFIG &&
        usb_state.USB_STATE != USB_STATE_ADDRESS)
    {
        STALL_ENDPOINT_0;
        return;
    }

    switch (usb_setup.REQUESTTYPE) {
      /* clear device feature */
      case 0:
        if (usb_setup.VALUE == 1) {
            /* clear remote wakeup */
            usb_state.USB_DEVICE_STATE &= ~USB_REMOTE_WAKEUP;
        }
        else {
            /* unsupported feature */
            STALL_ENDPOINT_0;
            return;
        }
        break;

      /* clear endpoint feature */
      case 2:
        if (usb_setup.VALUE != 0) {
            STALL_ENDPOINT_0;
            return;
        }

        /* check endpoint no. and stall if not configured or > 0 */
        if (CheckEP(usb_setup.INDEX))
            return;

        /* clear ENDPOINT_HALT */
        USB_ISR_set_endpoint_status(usb_setup.INDEX, 0);
        break;

      /* clear interface feature (no features defined) */
      default:
        STALL_ENDPOINT_0;
        return;
    }

    /* status phase */
    USB_ISR_io_request(EP0_SEND, 0, 0);
}

/**************************************************************************
*
*  Function Name: SetFeature
*  Comments:
*    Chapter 9 SetFeature command
*
**************************************************************************/
static void SetFeature(Boolean setup)
{
    if (setup) {
        switch (usb_setup.REQUESTTYPE) {
          /* set device feature */
          case 0:
            switch (usb_setup.VALUE) {
              /* set remote wakeup */
              case 1:
                usb_state.USB_DEVICE_STATE |= USB_REMOTE_WAKEUP;
                break;

              /* set Test Mode */
              case 2:
                if ( (((usb_setup.INDEX & 0xFF) == 0) && USB20_HighSpeed)
                    &&
                     ((usb_state.USB_STATE == USB_STATE_CONFIG)  ||
                      (usb_state.USB_STATE == USB_STATE_ADDRESS) ||
                      (usb_state.USB_STATE == USB_STATE_DEFAULT))
                    &&
                     (usb_setup.INDEX >> 8 < 6) )
                {
                    /* Note: Must power-cycle to exit test mode. */
                    test_mode_index = usb_setup.INDEX & 0xFF00;
                }
                else {
                    STALL_ENDPOINT_0;
                    return;
                }
                break;

              default:
                STALL_ENDPOINT_0;
                return;
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -