00001
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include "conf_usb.h"
00051
00052
00053 #if USB_DEVICE_FEATURE == ENABLED
00054
00055 #include "usb_drv.h"
00056 #include "usb_descriptors.h"
00057 #include "usb_standard_request.h"
00058 #include "usb_specific_request.h"
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 extern volatile U8 usb_hid_report_mouse[];
00070 U8 g_u8_report_rate=0;
00071
00072 extern const void *pbuffer;
00073 extern U16 data_to_transfer;
00074
00075
00076
00077
00078
00079
00084
00086 void usb_hid_set_idle (U8 u8_report_id, U8 u8_duration )
00087 {
00088 U16 wInterface;
00089
00090
00091 wInterface=Usb_read_endpoint_data(EP_CONTROL, 16);
00092 Usb_ack_setup_received_free();
00093
00094 if( wInterface == INTERFACE_NB_MOUSE )
00095 g_u8_report_rate = u8_duration;
00096
00097 Usb_ack_control_in_ready_send();
00098 while (!Is_usb_control_in_ready());
00099 }
00100
00101
00106 void usb_hid_get_idle (U8 u8_report_id)
00107 {
00108 U16 wLength;
00109 U16 wInterface;
00110
00111
00112 wInterface=Usb_read_endpoint_data(EP_CONTROL, 16);
00113 wLength =Usb_read_endpoint_data(EP_CONTROL, 16);
00114 Usb_ack_setup_received_free();
00115
00116 if( (wLength != 0) && (wInterface == INTERFACE_NB_MOUSE) )
00117 {
00118 Usb_write_endpoint_data(EP_CONTROL, 8, g_u8_report_rate);
00119 Usb_ack_control_in_ready_send();
00120 }
00121
00122 while (!Is_usb_control_out_received());
00123 Usb_ack_control_out_received_free();
00124 }
00125
00126
00129 static void hid_get_descriptor(U8 size_of_report, const U8* p_usb_hid_report)
00130 {
00131 Bool zlp;
00132 U16 wIndex;
00133 U16 wLength;
00134
00135 zlp = FALSE;
00136
00137 data_to_transfer = size_of_report;
00138 pbuffer = p_usb_hid_report;
00139
00140 wIndex = Usb_read_endpoint_data(EP_CONTROL, 16);
00141 wIndex = usb_format_usb_to_mcu_data(16, wIndex);
00142 wLength = Usb_read_endpoint_data(EP_CONTROL, 16);
00143 wLength = usb_format_usb_to_mcu_data(16, wLength);
00144 Usb_ack_setup_received_free();
00145
00146 if (wLength > data_to_transfer)
00147 {
00148 zlp = !(data_to_transfer % EP_CONTROL_LENGTH);
00149 }
00150 else
00151 {
00152 data_to_transfer = wLength;
00153 }
00154
00155 Usb_ack_nak_out(EP_CONTROL);
00156
00157 while (data_to_transfer && (!Is_usb_nak_out(EP_CONTROL)))
00158 {
00159 while( !Is_usb_control_in_ready() && !Is_usb_nak_out(EP_CONTROL) );
00160
00161 if( Is_usb_nak_out(EP_CONTROL) )
00162 break;
00163
00164 Usb_reset_endpoint_fifo_access(EP_CONTROL);
00165 data_to_transfer = usb_write_ep_txpacket(EP_CONTROL, pbuffer,
00166 data_to_transfer, &pbuffer);
00167 if( Is_usb_nak_out(EP_CONTROL) )
00168 break;
00169 else
00170 Usb_ack_control_in_ready_send();
00171 }
00172
00173 if ( zlp && (!Is_usb_nak_out(EP_CONTROL)) )
00174 {
00175 while (!Is_usb_control_in_ready());
00176 Usb_ack_control_in_ready_send();
00177 }
00178
00179 while (!(Is_usb_nak_out(EP_CONTROL)));
00180 Usb_ack_nak_out(EP_CONTROL);
00181 while (!Is_usb_control_out_received());
00182 Usb_ack_control_out_received_free();
00183 }
00184
00185
00189 void usb_user_endpoint_init(U8 conf_nb)
00190 {
00191 #if (USB_HIGH_SPEED_SUPPORT==DISABLED)
00192 (void)Usb_configure_endpoint(EP_HID_MOUSE_IN,
00193 EP_ATTRIBUTES_1,
00194 DIRECTION_IN,
00195 EP_SIZE_1_FS,
00196 SINGLE_BANK);
00197
00198 #else
00199 if( Is_usb_full_speed_mode() )
00200 {
00201 (void)Usb_configure_endpoint(EP_HID_MOUSE_IN,
00202 EP_ATTRIBUTES_1,
00203 DIRECTION_IN,
00204 EP_SIZE_1_FS,
00205 SINGLE_BANK);
00206 }else{
00207 (void)Usb_configure_endpoint(EP_HID_MOUSE_IN,
00208 EP_ATTRIBUTES_1,
00209 DIRECTION_IN,
00210 EP_SIZE_1_HS,
00211 SINGLE_BANK);
00212 }
00213 #endif
00214 }
00215
00216
00223 Bool usb_user_read_request(U8 type, U8 request)
00224 {
00225 U8 wValue_msb;
00226 U8 wValue_lsb;
00227
00228
00229 wValue_lsb = Usb_read_endpoint_data(EP_CONTROL, 8);
00230 wValue_msb = Usb_read_endpoint_data(EP_CONTROL, 8);
00231
00232
00233
00234
00235
00236
00237
00238
00239 if( 0x81 == type )
00240 {
00241 switch( request )
00242 {
00243 case GET_DESCRIPTOR:
00244 switch( wValue_msb )
00245 {
00246 #if (USB_HIGH_SPEED_SUPPORT==DISABLED)
00247 case HID_DESCRIPTOR:
00248 hid_get_descriptor(
00249 sizeof(usb_conf_desc_fs.hid_mouse)
00250 , (const U8*)&usb_conf_desc_fs.hid_mouse);
00251 return TRUE;
00252 #else
00253 case HID_DESCRIPTOR:
00254 if( Is_usb_full_speed_mode() )
00255 {
00256 hid_get_descriptor(
00257 sizeof(usb_conf_desc_fs.hid_mouse)
00258 , (const U8*)&usb_conf_desc_fs.hid_mouse);
00259 }else{
00260 hid_get_descriptor(
00261 sizeof(usb_conf_desc_hs.hid_mouse)
00262 , (const U8*)&usb_conf_desc_hs.hid_mouse);
00263 }
00264 return TRUE;
00265 #endif
00266
00267 case HID_REPORT_DESCRIPTOR:
00268 hid_get_descriptor(
00269 sizeof(usb_hid_report_descriptor_mouse)
00270 , usb_hid_report_descriptor_mouse);
00271 return TRUE;
00272
00273 case HID_PHYSICAL_DESCRIPTOR:
00274
00275 break;
00276 }
00277 break;
00278 }
00279 }
00280 if( 0x21 == type )
00281 {
00282 switch( request )
00283 {
00284 case HID_SET_REPORT:
00285
00286
00287 switch (wValue_msb)
00288 {
00289 case HID_REPORT_INPUT:
00290
00291 break;
00292
00293 case HID_REPORT_OUTPUT:
00294 Usb_ack_setup_received_free();
00295 while (!Is_usb_control_out_received());
00296 Usb_ack_control_out_received_free();
00297 Usb_ack_control_in_ready_send();
00298 while (!Is_usb_control_in_ready());
00299 return TRUE;
00300
00301 case HID_REPORT_FEATURE:
00302 break;
00303 }
00304 break;
00305
00306 case HID_SET_IDLE:
00307 usb_hid_set_idle(wValue_lsb, wValue_msb);
00308 return TRUE;
00309
00310 case HID_SET_PROTOCOL:
00311
00312 break;
00313 }
00314 }
00315 if( 0xA1 == type )
00316 {
00317 switch( request )
00318 {
00319 case HID_GET_REPORT:
00320
00321 break;
00322 case HID_GET_IDLE:
00323 usb_hid_get_idle(wValue_lsb);
00324 return TRUE;
00325 case HID_GET_PROTOCOL:
00326
00327 break;
00328 }
00329 }
00330 return FALSE;
00331 }
00332
00333
00337 Bool usb_user_get_descriptor(U8 type, U8 string)
00338 {
00339 pbuffer = NULL;
00340
00341 switch (type)
00342 {
00343 case STRING_DESCRIPTOR:
00344 switch (string)
00345 {
00346 case LANG_ID:
00347 data_to_transfer = sizeof(usb_user_language_id);
00348 pbuffer = &usb_user_language_id;
00349 break;
00350
00351 case MAN_INDEX:
00352 data_to_transfer = sizeof(usb_user_manufacturer_string_descriptor);
00353 pbuffer = &usb_user_manufacturer_string_descriptor;
00354 break;
00355
00356 case PROD_INDEX:
00357 data_to_transfer = sizeof(usb_user_product_string_descriptor);
00358 pbuffer = &usb_user_product_string_descriptor;
00359 break;
00360
00361 case SN_INDEX:
00362 data_to_transfer = sizeof(usb_user_serial_number);
00363 pbuffer = &usb_user_serial_number;
00364 break;
00365
00366 default:
00367 break;
00368 }
00369 break;
00370
00371 default:
00372 break;
00373 }
00374
00375 return pbuffer != NULL;
00376 }
00377
00378
00379 #endif // USB_DEVICE_FEATURE == ENABLED