天天看点

usb hid函数

Functions

bool  hal_usb_hid_device_req_proc (hal_usb_device_req *req, uint8_t **data_ptr, uint8_t *size, hal_usb_dev_req_resp_t *resp)

Detailed Description

Function for handling HID device requests.

Definition in file hal_usb_hid.c.

Function Documentation

bool hal_usb_hid_device_req_proc ( hal_usb_device_req *  req,
uint8_t **  data_ptr,
uint8_t *  size,
hal_usb_dev_req_resp_t *  resp
)

Function that process a HID device request

This function is usually called from an application that use the USB HID specification

Parameters:

req  The request from host
data_ptr  Address where this function can put data into which is sent to USB-host
size  Size of the data to send
resp  The response this function send back to the USB-host

Definition at line 24 of file hal_usb_hid.c.

00025 {
00026     bool retval = false;
00027     *resp = STALL;
00028 
00029     if(req->bRequest == USB_REQ_GET_DESCRIPTOR )
00030     {
00031         switch( req->wValueMsb )
00032         {
00033             case  USB_CLASS_DESCRIPTOR_REPORT:
00034                 retval = true;
00035 
00036                 *data_ptr = g_usb_hid_hids[LSB(req->wIndex)].hid_report_desc;
00037                 *size = MIN(g_usb_hid_hids[LSB(req->wIndex)].hid_report_desc_size, LSB(req->wLength));
00038                 *resp = DATA;
00039                 break;
00040             case USB_CLASS_DESCRIPTOR_HID:
00041                 retval = true;
00042 
00043                 *data_ptr = (uint8_t*)g_usb_hid_hids[LSB(req->wIndex)].hid_desc;
00044                 *size = MIN(sizeof(hal_usb_hid_desc_t), LSB(req->wLength));
00045                 *resp = DATA;
00046                 break;
00047             default:
00048                 break;
00049         }
00050     } 
00051     else if( ( req->bmRequestType & 0x20 ) == 0x20 ) // This is a class specific request D5..6: Type Class(value 1)
00052     { 
00053         switch( req->bRequest )
00054         {
00055             case 0x01: // Get_Report
00056                 retval = true;
00057                 // For now we just send an "empty" report. No mousemoves, no mouse-key pressed.
00058                 // TODO: This breaks the generic nature of usb.c. 
00059                 // Have to create some global "default" reports in the template later.
00060                 tmp_usb_buf[0] = tmp_usb_buf[1] = 0x00;
00061                 *data_ptr = &tmp_usb_buf[0];
00062                 *size = 0x03;
00063                 *resp = DATA;
00064                 break;
00065             case 0x02: // Get_Idle
00066                 retval = true;
00067                 *resp = STALL;
00068                 break;
00069             case 0x0a: // Set_Idle
00070                 retval = true;
00071                 *resp = STALL;
00072                 break;
00073             case 0x03: // Get_Protocol
00074                 retval = true;
00075                 tmp_usb_buf[0] = ( tmp_usb_hid_protocol & (1 << LSB(req->wIndex)) ) ? 0x01 : 0x00;
00076                 *data_ptr = &tmp_usb_buf[0];
00077                 *size = 0x01;
00078                 *resp = DATA;
00079                 break;
00080             case 0x0b: // Set_Protocol
00081                 retval = true;
00082 #if 1 // Right now we do not support setting of protocol in a intelligent way
00083                 if( req->wValueLsb == 0x01 )
00084                 {
00085                     tmp_usb_hid_protocol |= 1 << LSB(req->wIndex);
00086                 }
00087                 else
00088                 {
00089                     tmp_usb_hid_protocol &= ~(1 << LSB(req->wIndex));
00090                 }
00091 
00092                 *resp = NAK;
00093 #else
00094                 *resp = EMPTY_RESPONSE;
00095 #endif
00096                 break;
00097             case 0x09: // Set_Report
00098                 if( req->wValueMsb == 0x03 ) // Feature report
00099                 {
00100                     retval = false;
00101                 }
00102                 else if ( req->wValueMsb == 0x02 ) // Output report
00103                 {
00104                     // For now we just assume that the OUT packet is a keyboard report.
00105                     *resp =NAK;
00106                     retval = true;
00107                 }
00108                 break;
00109             default:
00110                 break;
00111             }
00112     }
00113 
00114     return retval;
00115 }
      

继续阅读