storage_task.c

Go to the documentation of this file.
00001 /*This file is prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2009 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  * 4. This software may only be redistributed and used in connection with an Atmel
00030  * AVR product.
00031  *
00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00042  */
00043 
00044 //_____  I N C L U D E S ___________________________________________________
00045 
00046 #include "config.h"
00047 #include "conf_usb.h"
00048 #include "storage_task.h"
00049 #include "lib_mcu/usb/usb_drv.h"
00050 #include "usb_descriptors.h"
00051 #include "modules/usb/device_chap9/usb_standard_request.h"
00052 #include "usb_specific_request.h"
00053 #include "modules/scsi_decoder/scsi_decoder.h"
00054 #include "modules/control_access/ctrl_access.h"
00055 #include "lib_mem/df/df.h"
00056 
00057 
00058 
00059 
00060 //_____ M A C R O S ________________________________________________________
00061 
00062 
00063 
00064 
00065 
00066 //_____ D E F I N I T I O N S ______________________________________________
00067 
00068 
00069 
00070 //_____ D E C L A R A T I O N S ____________________________________________
00071 
00072 
00073 
00074 extern  U8   usb_configuration_nb;
00075 
00076 
00077 bit ms_data_direction;
00078 static _MEM_TYPE_SLOW_ U8  dCBWTag[4];
00079 
00080 extern _MEM_TYPE_SLOW_ U8  g_scsi_status;
00081 extern _MEM_TYPE_FAST_ U32 g_scsi_data_remaining;
00082 extern bit ms_multiple_drive;
00083 extern _MEM_TYPE_SLOW_ U8  g_scsi_command[16];
00084 
00085 extern U8   endpoint_status[MAX_EP_NB];
00086 
00087 _MEM_TYPE_SLOW_ U8 usb_LUN;
00088 
00089 
00090 void usb_mass_storage_cbw (void);
00091 void usb_mass_storage_csw (void);
00092 
00102 void storage_task_init(void)
00103 {
00104    df_mem_init();    // Init the hw/sw ressources required to drive the DF.
00105 }
00106 
00107 
00116 void storage_task(void)
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 
00130 
00131 
00132 
00133 
00143 void usb_mass_storage_cbw (void)
00144 {
00145 bit cbw_error;
00146 U8  c;
00147 U8  dummy;
00148 
00149    cbw_error = FALSE;
00150    Usb_select_endpoint(EP_MS_OUT);           
00151    if (0x55 != Usb_read_byte())
00152       { cbw_error = TRUE; } 
00153    if (0x53 != Usb_read_byte())
00154       { cbw_error = TRUE; } 
00155    if (0x42 != Usb_read_byte())
00156       { cbw_error = TRUE; } 
00157    if (0x43 != Usb_read_byte())
00158       { cbw_error = TRUE; } 
00159    if (cbw_error)
00160    {
00161       Usb_ack_receive_out();
00162       Usb_select_endpoint(EP_MS_IN);
00163       Usb_enable_stall_handshake();
00164       endpoint_status[(EP_MS_IN & MSK_EP_DIR)] = 0x01;
00165       return;
00166    }
00167 
00168    dCBWTag[0] = Usb_read_byte();             
00169    dCBWTag[1] = Usb_read_byte();
00170    dCBWTag[2] = Usb_read_byte();
00171    dCBWTag[3] = Usb_read_byte();
00172    
00173    LSB0(g_scsi_data_remaining) = Usb_read_byte();
00174    LSB1(g_scsi_data_remaining) = Usb_read_byte();
00175    LSB2(g_scsi_data_remaining) = Usb_read_byte();
00176    LSB3(g_scsi_data_remaining) = Usb_read_byte();
00177 
00178    if (Usb_read_byte() != 0x00)              
00179    {
00180       Usb_set_ms_data_direction_in();
00181    }
00182    else
00183    {
00184       Usb_set_ms_data_direction_out();
00185    }
00186 
00187    usb_LUN = Usb_read_byte();
00188 
00189    if (!ms_multiple_drive)
00190    {
00191       usb_LUN = get_cur_lun();
00192    }
00193 
00194    dummy      = Usb_read_byte();                // dummy CBWCBLength read
00195 
00196 
00197    for (c=0; c<16; c++)                         // store scsi_command
00198    {
00199       g_scsi_command[c] = Usb_read_byte();
00200    }
00201    Usb_ack_receive_out();
00202 
00203    if (Is_usb_ms_data_direction_in())
00204    {
00205       Usb_select_endpoint(EP_MS_IN);
00206    }
00207 
00208    if (TRUE != scsi_decode_command())
00209    {
00210       U8 ep;
00211       Usb_enable_stall_handshake();
00212       if (Is_usb_ms_data_direction_in())
00213       {
00214          ep = (EP_MS_IN & MSK_EP_DIR);
00215       }else{
00216          ep = (EP_MS_OUT & MSK_EP_DIR);
00217       }
00218       endpoint_status[ep] = 0x01;
00219    }
00220 }
00221 
00222 
00231 void usb_mass_storage_csw (void)
00232 {
00233    Usb_select_endpoint(EP_MS_IN);
00234    while (Is_usb_endpoint_stall_requested())
00235    {
00236       Usb_select_endpoint(EP_CONTROL);
00237       if (Is_usb_receive_setup())       { usb_process_request(); }
00238       Usb_select_endpoint(EP_MS_IN);
00239    }
00240 
00241    Usb_select_endpoint(EP_MS_OUT);
00242    while (Is_usb_endpoint_stall_requested())
00243    {
00244       Usb_select_endpoint(EP_CONTROL);
00245       if (Is_usb_receive_setup())       { usb_process_request(); }
00246       Usb_select_endpoint(EP_MS_OUT);
00247    }
00248 
00249 
00250    Usb_select_endpoint(EP_MS_IN);
00251    while(!Is_usb_write_enabled())
00252    {
00253       if(!Is_usb_endpoint_enabled())   return; // USB Reset
00254    }
00256    Usb_write_byte(0x55);                                 
00257    Usb_write_byte(0x53);                                 
00258    Usb_write_byte(0x42);                                 
00259    Usb_write_byte(0x53);                                 
00260 
00261    Usb_write_byte(dCBWTag[0]);
00262    Usb_write_byte(dCBWTag[1]);
00263    Usb_write_byte(dCBWTag[2]);
00264    Usb_write_byte(dCBWTag[3]);
00266    Usb_write_byte( LSB0(g_scsi_data_remaining) );
00267    Usb_write_byte( LSB1(g_scsi_data_remaining) );
00268    Usb_write_byte( LSB2(g_scsi_data_remaining) );
00269    Usb_write_byte( LSB3(g_scsi_data_remaining) );
00270 
00272    Usb_write_byte(g_scsi_status);                        
00273 
00274    Usb_send_in();
00275 }

Generated on Fri Sep 11 14:39:51 2009 for ATMEL by  doxygen 1.5.3