00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00013 00014 /* Copyright (c) 2007, Atmel Corporation All rights reserved. 00015 * 00016 * Redistribution and use in source and binary forms, with or without 00017 * modification, are permitted provided that the following conditions are met: 00018 * 00019 * 1. Redistributions of source code must retain the above copyright notice, 00020 * this list of conditions and the following disclaimer. 00021 * 00022 * 2. Redistributions in binary form must reproduce the above copyright notice, 00023 * this list of conditions and the following disclaimer in the documentation 00024 * and/or other materials provided with the distribution. 00025 * 00026 * 3. The name of ATMEL may not be used to endorse or promote products derived 00027 * from this software without specific prior written permission. 00028 * 00029 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED 00030 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00031 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND 00032 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00033 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00034 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00035 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00036 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00037 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00038 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00039 */ 00040 00041 //_____ I N C L U D E S ___________________________________________________ 00042 00043 #include "config.h" 00044 #include "conf_usb.h" 00045 #include "storage_task.h" 00046 #include "lib_mcu/usb/usb_drv.h" 00047 #include "usb_descriptors.h" 00048 #include "modules/usb/device_chap9/usb_standard_request.h" 00049 #include "usb_specific_request.h" 00050 #include "modules/scsi_decoder/scsi_decoder.h" 00051 #include "modules/control_access/ctrl_access.h" 00052 00053 00054 00055 //_____ D E C L A R A T I O N S ____________________________________________ 00056 00057 00058 volatile U8 cpt_sof; 00059 extern U8 usb_configuration_nb; 00060 extern Bool g_b_df_protected; // Global value to manage the write protection on DataFlash 00061 00062 00063 bit ms_data_direction; 00064 static _MEM_TYPE_SLOW_ U8 dCBWTag[4]; 00065 00066 extern _MEM_TYPE_SLOW_ U8 g_scsi_status; 00067 extern _MEM_TYPE_FAST_ U32 g_scsi_data_remaining; 00068 extern bit ms_multiple_drive; 00069 extern _MEM_TYPE_SLOW_ U8 g_scsi_command[16]; 00070 00071 extern U8 endpoint_status[MAX_EP_NB]; 00072 00073 _MEM_TYPE_SLOW_ U8 usb_LUN; 00074 00075 00076 void usb_mass_storage_cbw (void); 00077 void usb_mass_storage_csw (void); 00078 00088 void storage_task_init(void) 00089 { 00090 Leds_init(); 00091 Joy_init(); 00092 Usb_enable_sof_interrupt(); 00093 df_mem_init(); // Init the hw/sw ressources required to drive the DF. 00094 } 00095 00096 00105 void storage_task(void) 00106 { 00107 if(Is_joy_up()) 00108 { 00109 // Look Write Protection DataFlash 00110 g_b_df_protected = TRUE; 00111 } 00112 if(Is_joy_down()) 00113 { 00114 // UnLook Write Protection DataFlash 00115 g_b_df_protected = FALSE; 00116 } 00117 00118 if (Is_device_enumerated()) 00119 { 00120 Usb_select_endpoint(EP_MS_OUT); 00121 if (Is_usb_receive_out()) 00122 { 00123 usb_mass_storage_cbw(); 00124 usb_mass_storage_csw(); 00125 } 00126 } 00127 } 00128 00129 00134 void sof_action() 00135 { 00136 cpt_sof++; 00137 } 00138 00139 00149 void usb_mass_storage_cbw (void) 00150 { 00151 bit cbw_error; 00152 U8 c; 00153 U8 dummy; 00154 00155 cbw_error = FALSE; 00156 Usb_select_endpoint(EP_MS_OUT); 00157 if (0x55 != Usb_read_byte()) 00158 { cbw_error = TRUE; } 00159 if (0x53 != Usb_read_byte()) 00160 { cbw_error = TRUE; } 00161 if (0x42 != Usb_read_byte()) 00162 { cbw_error = TRUE; } 00163 if (0x43 != Usb_read_byte()) 00164 { cbw_error = TRUE; } 00165 if (cbw_error) 00166 { 00167 Usb_ack_receive_out(); 00168 Usb_select_endpoint(EP_MS_IN); 00169 Usb_enable_stall_handshake(); 00170 endpoint_status[(EP_MS_IN & MSK_EP_DIR)] = 0x01; 00171 return; 00172 } 00173 00174 dCBWTag[0] = Usb_read_byte(); 00175 dCBWTag[1] = Usb_read_byte(); 00176 dCBWTag[2] = Usb_read_byte(); 00177 dCBWTag[3] = Usb_read_byte(); 00178 00179 LSB0(g_scsi_data_remaining) = Usb_read_byte(); 00180 LSB1(g_scsi_data_remaining) = Usb_read_byte(); 00181 LSB2(g_scsi_data_remaining) = Usb_read_byte(); 00182 LSB3(g_scsi_data_remaining) = Usb_read_byte(); 00183 00184 if (Usb_read_byte() != 0x00) 00185 { 00186 Usb_set_ms_data_direction_in(); 00187 } 00188 else 00189 { 00190 Usb_set_ms_data_direction_out(); 00191 } 00192 00193 usb_LUN = Usb_read_byte(); 00194 00195 if (!ms_multiple_drive) 00196 { 00197 usb_LUN = get_cur_lun(); 00198 } 00199 00200 dummy = Usb_read_byte(); // dummy CBWCBLength read 00201 00202 00203 for (c=0; c<16; c++) // store scsi_command 00204 { 00205 g_scsi_command[c] = Usb_read_byte(); 00206 } 00207 Usb_ack_receive_out(); 00208 00209 if (Is_usb_ms_data_direction_in()) 00210 { 00211 Usb_select_endpoint(EP_MS_IN); 00212 } 00213 00214 if (TRUE != scsi_decode_command()) 00215 { 00216 U8 ep; 00217 Usb_enable_stall_handshake(); 00218 if (Is_usb_ms_data_direction_in()) 00219 { 00220 ep = (EP_MS_IN & MSK_EP_DIR); 00221 }else{ 00222 ep = (EP_MS_OUT & MSK_EP_DIR); 00223 } 00224 endpoint_status[ep] = 0x01; 00225 } 00226 } 00227 00228 00237 void usb_mass_storage_csw (void) 00238 { 00239 Usb_select_endpoint(EP_MS_IN); 00240 while (Is_usb_endpoint_stall_requested()) 00241 { 00242 Usb_select_endpoint(EP_CONTROL); 00243 if (Is_usb_receive_setup()) { usb_process_request(); } 00244 Usb_select_endpoint(EP_MS_IN); 00245 } 00246 00247 Usb_select_endpoint(EP_MS_OUT); 00248 while (Is_usb_endpoint_stall_requested()) 00249 { 00250 Usb_select_endpoint(EP_CONTROL); 00251 if (Is_usb_receive_setup()) { usb_process_request(); } 00252 Usb_select_endpoint(EP_MS_OUT); 00253 } 00254 00255 00256 Usb_select_endpoint(EP_MS_IN); 00257 while(!Is_usb_write_enabled()) 00258 { 00259 if(!Is_usb_endpoint_enabled()) return; // USB Reset 00260 } 00262 Usb_write_byte(0x55); 00263 Usb_write_byte(0x53); 00264 Usb_write_byte(0x42); 00265 Usb_write_byte(0x53); 00266 00267 Usb_write_byte(dCBWTag[0]); 00268 Usb_write_byte(dCBWTag[1]); 00269 Usb_write_byte(dCBWTag[2]); 00270 Usb_write_byte(dCBWTag[3]); 00272 Usb_write_byte( LSB0(g_scsi_data_remaining) ); 00273 Usb_write_byte( LSB1(g_scsi_data_remaining) ); 00274 Usb_write_byte( LSB2(g_scsi_data_remaining) ); 00275 Usb_write_byte( LSB3(g_scsi_data_remaining) ); 00276 00278 Usb_write_byte(g_scsi_status); 00279 00280 Usb_send_in(); 00281 }
1.5.3