00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 //_____ I N C L U D E S ___________________________________________________ 00018 00019 #include "config.h" 00020 #include "conf_usb.h" 00021 #include "storage_task.h" 00022 #include "lib_mcu\usb\usb_drv.h" 00023 #include "usb_descriptors.h" 00024 #include "modules\usb\device_chap9\usb_standard_request.h" 00025 #include "usb_specific_request.h" 00026 #include "modules\scsi_decoder\scsi_decoder.h" 00027 #include "modules\control_access\ctrl_access.h" 00028 #include "lib_mem/df/df.h" 00029 00030 00031 00032 00033 //_____ M A C R O S ________________________________________________________ 00034 00035 00036 00037 00038 00039 //_____ D E F I N I T I O N S ______________________________________________ 00040 00041 00042 00043 //_____ D E C L A R A T I O N S ____________________________________________ 00044 00045 00046 00047 extern U8 usb_configuration_nb; 00048 00049 00050 static bit ms_data_direction; 00051 static _MEM_TYPE_SLOW_ U8 dCBWTag[4]; 00052 00053 extern _MEM_TYPE_SLOW_ U8 g_scsi_status; 00054 extern _MEM_TYPE_FAST_ U32 g_scsi_data_remaining; 00055 extern bit ms_multiple_drive; 00056 extern _MEM_TYPE_SLOW_ U8 g_scsi_command[16]; 00057 00058 00059 _MEM_TYPE_SLOW_ U8 usb_LUN; 00060 00061 00062 void usb_mass_storage_cbw (void); 00063 void usb_mass_storage_csw (void); 00064 00065 #define Usb_set_ms_data_direction_in() (ms_data_direction = 1) 00066 #define Usb_set_ms_data_direction_out() (ms_data_direction = 0) 00067 #define Is_usb_ms_data_direction_in() (ms_data_direction == 1) 00068 00069 00079 void storage_task_init(void) 00080 { 00081 Leds_init(); 00082 //Usb_enable_sof_interrupt(); 00083 df_init_spi(); // Init the board (definition of the links between the DF and AVR USB) 00084 df_mem_init(); // Init the hw/sw ressources required to drive the DF. 00085 00086 } 00087 00088 00089 00090 00091 00100 void storage_task(void) 00101 { 00102 if (Is_device_enumerated()) 00103 { 00104 Usb_select_endpoint(EP_MS_OUT); 00105 if (Is_usb_receive_out()) 00106 { 00107 usb_mass_storage_cbw(); 00108 usb_mass_storage_csw(); 00109 } 00110 } 00111 } 00112 00113 00114 00115 00116 00117 00127 void usb_mass_storage_cbw (void) 00128 { 00129 bit cbw_error; 00130 U8 c; 00131 U8 dummy; 00132 00133 cbw_error = FALSE; 00134 Usb_select_endpoint(EP_MS_OUT); 00135 if (0x55 != Usb_read_byte()) 00136 { cbw_error = TRUE; } 00137 if (0x53 != Usb_read_byte()) 00138 { cbw_error = TRUE; } 00139 if (0x42 != Usb_read_byte()) 00140 { cbw_error = TRUE; } 00141 if (0x43 != Usb_read_byte()) 00142 { cbw_error = TRUE; } 00143 00144 dCBWTag[0] = Usb_read_byte(); 00145 dCBWTag[1] = Usb_read_byte(); 00146 dCBWTag[2] = Usb_read_byte(); 00147 dCBWTag[3] = Usb_read_byte(); 00148 00149 LSB0(g_scsi_data_remaining) = Usb_read_byte(); 00150 LSB1(g_scsi_data_remaining) = Usb_read_byte(); 00151 LSB2(g_scsi_data_remaining) = Usb_read_byte(); 00152 LSB3(g_scsi_data_remaining) = Usb_read_byte(); 00153 00154 00155 if (Usb_read_byte() != 0x00) 00156 { 00157 Usb_set_ms_data_direction_in(); 00158 if (cbw_error) 00159 { 00160 Usb_ack_receive_out(); 00161 Usb_select_endpoint(EP_MS_IN); 00162 Usb_enable_stall_handshake(); 00163 return; 00164 } 00165 } 00166 else 00167 { 00168 Usb_set_ms_data_direction_out(); 00169 if (cbw_error) 00170 { 00171 Usb_enable_stall_handshake(); 00172 Usb_ack_receive_out(); 00173 return; 00174 } 00175 } 00176 00177 usb_LUN = Usb_read_byte(); 00178 00179 if (!ms_multiple_drive) 00180 { 00181 usb_LUN = get_cur_lun(); 00182 } 00183 00184 dummy = Usb_read_byte(); 00185 00186 00187 for (c=0; c<16; c++) // store scsi_command 00188 { 00189 g_scsi_command[c] = Usb_read_byte(); 00190 } 00191 00192 Usb_ack_receive_out(); 00193 00194 if (Is_usb_ms_data_direction_in()) 00195 { 00196 Usb_select_endpoint(EP_MS_IN); 00197 } 00198 00199 if (TRUE != scsi_decode_command()) 00200 { 00201 if (g_scsi_data_remaining != 0) 00202 { 00203 Usb_enable_stall_handshake(); 00204 } 00205 } 00206 } 00207 00216 void usb_mass_storage_csw (void) 00217 { 00218 Usb_select_endpoint(EP_MS_IN); 00219 while (Is_usb_endpoint_stall_requested()) 00220 { 00221 Usb_select_endpoint(EP_CONTROL); 00222 if (Is_usb_receive_setup()) { usb_process_request(); } 00223 Usb_select_endpoint(EP_MS_IN); 00224 } 00225 00226 Usb_select_endpoint(EP_MS_OUT); 00227 while (Is_usb_endpoint_stall_requested()) 00228 { 00229 Usb_select_endpoint(EP_CONTROL); 00230 if (Is_usb_receive_setup()) { usb_process_request(); } 00231 Usb_select_endpoint(EP_MS_OUT); 00232 } 00233 00234 Usb_select_endpoint(EP_MS_IN); 00235 while(!Is_usb_write_enabled()); 00237 Usb_write_byte(0x55); 00238 Usb_write_byte(0x53); 00239 Usb_write_byte(0x42); 00240 Usb_write_byte(0x53); 00241 00242 Usb_write_byte(dCBWTag[0]); 00243 Usb_write_byte(dCBWTag[1]); 00244 Usb_write_byte(dCBWTag[2]); 00245 Usb_write_byte(dCBWTag[3]); 00247 Usb_write_byte( ((Byte*)&g_scsi_data_remaining)[3] ); 00248 Usb_write_byte( ((Byte*)&g_scsi_data_remaining)[2] ); 00249 Usb_write_byte( ((Byte*)&g_scsi_data_remaining)[1] ); 00250 Usb_write_byte( ((Byte*)&g_scsi_data_remaining)[0] ); 00251 00253 Usb_write_byte(g_scsi_status); 00254 Usb_send_in(); 00255 }