00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00017 00018 /* Copyright (c) 2007, Atmel Corporation All rights reserved. 00019 * 00020 * Redistribution and use in source and binary forms, with or without 00021 * modification, are permitted provided that the following conditions are met: 00022 * 00023 * 1. Redistributions of source code must retain the above copyright notice, 00024 * this list of conditions and the following disclaimer. 00025 * 00026 * 2. Redistributions in binary form must reproduce the above copyright notice, 00027 * this list of conditions and the following disclaimer in the documentation 00028 * and/or other materials provided with the distribution. 00029 * 00030 * 3. The name of ATMEL may not be used to endorse or promote products derived 00031 * from this software without specific prior written permission. 00032 * 00033 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED 00034 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00035 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND 00036 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00037 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00038 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00039 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00040 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00041 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00042 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00043 */ 00044 00045 00046 //_____ I N C L U D E S ____________________________________________________ 00047 00048 #include "config.h" 00049 #include "conf_usb.h" 00050 #include "lib_mcu/usb/usb_drv.h" 00051 #include "usb_descriptors.h" 00052 #include "modules/usb/device_chap9/usb_standard_request.h" 00053 #include "usb_specific_request.h" 00054 #if ((USB_DEVICE_SN_USE==ENABLE) && (USE_DEVICE_SN_UNIQUE==ENABLE)) 00055 #include "lib_mcu/flash/flash_drv.h" 00056 #endif 00057 00058 //_____ M A C R O S ________________________________________________________ 00059 00060 #define TARGET_UNDEFINE 0 00061 #define TARGET_MICRO 2 00062 00063 typedef struct { 00064 U8 target; // audio target (speaker or micro) 00065 U8 cs; // Command slector 00066 } st_audio_cmd; 00067 00068 00069 //_____ D E F I N I T I O N ________________________________________________ 00070 00071 st_audio_cmd check_audio_control_request(void); 00072 U8 interface_status[NB_INTERFACE]={0,0}; // All interface with default setting 00073 00074 00075 //_____ P R I V A T E D E C L A R A T I O N ______________________________ 00076 00077 #ifdef __GNUC__ 00078 extern PGM_VOID_P pbuffer; 00079 #else 00080 extern U8 code *pbuffer; 00081 #endif 00082 extern U8 data_to_transfer; 00083 00084 extern bit b_micro_mute; 00085 extern S16 s16_micro_volume; 00086 00087 00088 //_____ D E C L A R A T I O N ______________________________________________ 00089 00098 Bool usb_user_read_request(U8 type, U8 request) 00099 { 00100 st_audio_cmd cmd; 00101 00102 //** Specific request from Class Audio 00103 //* AudioControl Requests 00104 if( USB_SETUP_SET_CLASS_INTER == type ) 00105 { 00106 switch( request ) 00107 { 00108 case SETUP_AUDIO_SET_CUR: 00109 cmd = check_audio_control_request(); 00110 switch( cmd.target ) 00111 { 00112 case TARGET_MICRO: 00113 switch( cmd.cs ) 00114 { 00115 case AUDIO_FU_CONTROL_CS_MUTE: 00116 audio_micro_set_mute(); 00117 return TRUE; 00118 break; 00119 case AUDIO_FU_CONTROL_CS_VOLUME: 00120 audio_micro_set_volume(); 00121 return TRUE; 00122 break; 00123 } 00124 break; 00125 } 00126 break; 00127 00128 case SETUP_AUDIO_SET_MIN: 00129 case SETUP_AUDIO_SET_MAX: 00130 case SETUP_AUDIO_SET_RES: 00131 // TODO 00132 break; 00133 } 00134 } 00135 00136 if( USB_SETUP_GET_CLASS_INTER == type ) 00137 { 00138 switch( request ) 00139 { 00140 case SETUP_AUDIO_GET_CUR: 00141 cmd = check_audio_control_request(); 00142 switch( cmd.target ) 00143 { 00144 case TARGET_MICRO: 00145 switch( cmd.cs ) 00146 { 00147 case AUDIO_FU_CONTROL_CS_MUTE: 00148 audio_micro_get_mute(); 00149 return TRUE; 00150 break; 00151 case AUDIO_FU_CONTROL_CS_VOLUME: 00152 audio_micro_get_vol_cur(); 00153 return TRUE; 00154 break; 00155 } 00156 break; 00157 } 00158 break; 00159 00160 case SETUP_AUDIO_GET_MIN: 00161 cmd = check_audio_control_request(); 00162 switch( cmd.target ) 00163 { 00164 case TARGET_MICRO: 00165 switch( cmd.cs ) 00166 { 00167 case AUDIO_FU_CONTROL_CS_MUTE: 00168 audio_micro_get_mute(); 00169 return TRUE; 00170 break; 00171 case AUDIO_FU_CONTROL_CS_VOLUME: 00172 audio_micro_get_vol_min(); 00173 return TRUE; 00174 break; 00175 } 00176 break; 00177 } 00178 break; 00179 00180 case SETUP_AUDIO_GET_MAX: 00181 cmd = check_audio_control_request(); 00182 switch( cmd.target ) 00183 { 00184 case TARGET_MICRO: 00185 switch( cmd.cs ) 00186 { 00187 case AUDIO_FU_CONTROL_CS_MUTE: 00188 audio_micro_get_mute(); 00189 return TRUE; 00190 break; 00191 case AUDIO_FU_CONTROL_CS_VOLUME: 00192 audio_micro_get_vol_max(); 00193 return TRUE; 00194 break; 00195 } 00196 break; 00197 } 00198 break; 00199 00200 case SETUP_AUDIO_GET_RES: 00201 cmd = check_audio_control_request(); 00202 switch( cmd.target ) 00203 { 00204 case TARGET_MICRO: 00205 switch( cmd.cs ) 00206 { 00207 case AUDIO_FU_CONTROL_CS_MUTE: 00208 audio_micro_get_mute(); 00209 return TRUE; 00210 break; 00211 case AUDIO_FU_CONTROL_CS_VOLUME: 00212 audio_micro_get_vol_res(); 00213 return TRUE; 00214 break; 00215 } 00216 break; 00217 } 00218 break; 00219 } 00220 } 00221 00222 //* AudioStreaming Requests 00223 if( USB_SETUP_SET_CLASS_ENDPOINT == type ) 00224 { 00225 switch( request ) 00226 { 00227 case SETUP_AUDIO_SET_MEM: 00228 case SETUP_AUDIO_SET_MIN: 00229 case SETUP_AUDIO_SET_MAX: 00230 case SETUP_AUDIO_SET_RES: 00231 // TODO 00232 break; 00233 } 00234 } 00235 if( USB_SETUP_GET_CLASS_ENDPOINT == type ) 00236 { 00237 switch( request ) 00238 { 00239 case SETUP_AUDIO_GET_CUR: 00240 case SETUP_AUDIO_GET_MIN: 00241 case SETUP_AUDIO_GET_MAX: 00242 case SETUP_AUDIO_GET_RES: 00243 // TODO 00244 break; 00245 } 00246 } 00247 00248 //* Additional Requests 00249 // Set Memory Request 00250 if( USB_SETUP_SET_CLASS_INTER == type ) 00251 { 00252 switch( request ) 00253 { 00254 case SETUP_AUDIO_SET_MEM: 00255 // TODO 00256 break; 00257 } 00258 } 00259 if( USB_SETUP_SET_CLASS_ENDPOINT == type ) 00260 { 00261 switch( request ) 00262 { 00263 case SETUP_AUDIO_SET_MEM: 00264 // TODO 00265 break; 00266 } 00267 } 00268 // Get Memory Request 00269 if( USB_SETUP_GET_CLASS_INTER == type ) 00270 { 00271 switch( request ) 00272 { 00273 case SETUP_AUDIO_GET_MEM: 00274 // TODO 00275 break; 00276 } 00277 } 00278 if( USB_SETUP_GET_CLASS_ENDPOINT == type ) 00279 { 00280 switch( request ) 00281 { 00282 case SETUP_AUDIO_GET_MEM: 00283 // TODO 00284 break; 00285 } 00286 } 00287 // Get Status Request 00288 if( USB_SETUP_GET_CLASS_INTER == type ) 00289 { 00290 switch( request ) 00291 { 00292 case SETUP_AUDIO_GET_STAT: 00293 // TODO 00294 break; 00295 } 00296 } 00297 if( USB_SETUP_GET_CLASS_ENDPOINT == type ) 00298 { 00299 switch( request ) 00300 { 00301 case SETUP_AUDIO_GET_STAT: 00302 // TODO 00303 break; 00304 } 00305 } 00306 return FALSE; // No supported request 00307 } 00308 00309 00314 void usb_user_endpoint_init(U8 conf_nb) 00315 { 00316 usb_configure_endpoint(EP_AUDIO_IN, \ 00317 TYPE_ISOCHRONOUS,\ 00318 DIRECTION_IN, \ 00319 SIZE_16, \ 00320 TWO_BANKS, \ 00321 NYET_ENABLED); 00322 } 00323 00324 00331 U8 usb_user_interface_get( U16 wInterface ) 00332 { 00333 return interface_status[wInterface]; 00334 } 00335 00336 00342 void usb_user_interface_reset(U16 wInterface, U8 alternate_setting) 00343 { 00344 interface_status[wInterface] = alternate_setting; 00345 // default setting selected = reset data toggle 00346 if((NB_OF_STREAMING_INTERFACE == wInterface ) 00347 && (1 == alternate_setting)) 00348 { 00349 // Interface for Microphone, alternate setting with endpoint 00350 Usb_select_endpoint(EP_AUDIO_IN); 00351 Usb_disable_stall_handshake(); 00352 Usb_reset_endpoint(EP_AUDIO_IN); 00353 Usb_reset_data_toggle(); 00354 } 00355 } 00356 00357 00365 Bool usb_user_get_descriptor(U8 type, U8 string) 00366 { 00367 switch(type) 00368 { 00369 case DESCRIPTOR_STRING: 00370 switch (string) 00371 { 00372 case LANG_ID: 00373 data_to_transfer = sizeof (usb_user_language_id); 00374 pbuffer = &(usb_user_language_id.bLength); 00375 return TRUE; 00376 break; 00377 00378 case MAN_INDEX: 00379 data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor); 00380 pbuffer = &(usb_user_manufacturer_string_descriptor.bLength); 00381 return TRUE; 00382 break; 00383 00384 case PROD_INDEX: 00385 data_to_transfer = sizeof (usb_user_product_string_descriptor); 00386 pbuffer = &(usb_user_product_string_descriptor.bLength); 00387 return TRUE; 00388 break; 00389 00390 #if (USB_DEVICE_SN_USE==ENABLE) 00391 case SN_INDEX: 00392 data_to_transfer = sizeof (usb_user_serial_number); 00393 pbuffer = &(usb_user_serial_number.bLength); 00394 #if (USE_DEVICE_SN_UNIQUE==ENABLE) 00395 f_get_serial_string=TRUE; 00396 data_to_transfer += (SN_LENGTH*4); 00397 #endif 00398 return TRUE; 00399 break; 00400 #endif 00401 } 00402 break; 00403 } 00404 return FALSE; 00405 } 00406 00407 00412 st_audio_cmd check_audio_control_request(void) 00413 { 00414 st_audio_cmd cmd; 00415 U16 wValue; 00416 U16 wIndex; 00417 U16 length; 00418 00419 LSB(wValue)=Usb_read_byte(); 00420 MSB(wValue)=Usb_read_byte(); 00421 LSB(wIndex)=Usb_read_byte(); 00422 MSB(wIndex)=Usb_read_byte(); 00423 LSB(length)=Usb_read_byte(); 00424 MSB(length)=Usb_read_byte(); 00425 Usb_ack_receive_setup(); 00426 00427 cmd.target = TARGET_UNDEFINE; 00428 cmd.cs = AUDIO_FU_CONTROL_CS_UNDEFINED; 00429 00430 // Note: The wValue field interpretation is qualified by the value in the wIndex field. 00431 // Check target of the command 00432 if( LSB(wIndex) == AC_INTERFACE_NB) 00433 { 00434 // The request is for the Feature Unit Control then 00435 // MSB(wIndex) = Feature Unit ID, LSB(wValue) = Channel Number (CN), MSB(wValue) = Control Selector (CS) 00436 cmd.cs = MSB(wValue); 00437 00438 if( (MSB(wIndex) == FEATURE_UNIT_ID) 00439 && (LSB(wValue) == 0) ) 00440 { 00441 // for Master Channel of micro 00442 cmd.target = TARGET_MICRO; 00443 } 00444 } 00445 return cmd; 00446 } 00447 00448 00449 //******* Micro control 00450 void audio_micro_set_mute(void) 00451 { 00452 while(!Is_usb_receive_out()); 00453 b_micro_mute=Usb_read_byte(); 00454 Usb_ack_receive_out(); 00455 Usb_send_control_in(); // send a ZLP 00456 } 00457 void audio_micro_get_mute(void) 00458 { 00459 Usb_write_byte(b_micro_mute); 00460 Usb_send_control_in(); 00461 while(!Is_usb_receive_out()); // send a ZLP 00462 Usb_ack_receive_out(); 00463 } 00464 void audio_micro_set_volume(void) 00465 { 00466 while(!Is_usb_receive_out()); 00467 LSB(s16_micro_volume)=Usb_read_byte(); 00468 MSB(s16_micro_volume)=Usb_read_byte(); 00469 Usb_ack_receive_out(); 00470 Usb_send_control_in(); // send a ZLP 00471 } 00472 void audio_micro_get_vol_cur(void) 00473 { 00474 Usb_write_byte(LSB(s16_micro_volume)); 00475 Usb_write_byte(MSB(s16_micro_volume)); 00476 Usb_send_control_in(); 00477 while(!Is_usb_receive_out()); // send a ZLP 00478 Usb_ack_receive_out(); 00479 } 00480 void audio_micro_get_vol_min(void) 00481 { 00482 Usb_write_byte(LOW(MICRO_VOL_MIN)); 00483 Usb_write_byte(HIGH(MICRO_VOL_MIN)); 00484 Usb_send_control_in(); 00485 while(!Is_usb_receive_out()); // send a ZLP 00486 Usb_ack_receive_out(); 00487 } 00488 void audio_micro_get_vol_max(void) 00489 { 00490 Usb_write_byte(LOW(MICRO_VOL_MAX)); 00491 Usb_write_byte(HIGH(MICRO_VOL_MAX)); 00492 Usb_send_control_in(); 00493 while(!Is_usb_receive_out()); // send a ZLP 00494 Usb_ack_receive_out(); 00495 } 00496 void audio_micro_get_vol_res(void) 00497 { 00498 Usb_write_byte(LOW(MICRO_VOL_RES)); 00499 Usb_write_byte(HIGH(MICRO_VOL_RES)); 00500 Usb_send_control_in(); 00501 while(!Is_usb_receive_out()); // send a ZLP 00502 Usb_ack_receive_out(); 00503 }
1.5.3