- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

# storage_task.c File Reference

,vThis file manages the mass storage task. More...

`#include "config.h"`
`#include "conf_usb.h"`
`#include "storage_task.h"`
`#include "lib_mcu\usb\usb_drv.h"`
`#include "usb_descriptors.h"`
`#include "modules\usb\device_chap9\usb_standard_request.h"`
`#include "usb_specific_request.h"`
`#include "modules\scsi_decoder\scsi_decoder.h"`
`#include "modules\control_access\ctrl_access.h"`
`#include "lib_mem/df/df.h"`

Include dependency graph for storage_task.c:

Go to the source code of this file.

## Defines
#define | Usb_set_ms_data_direction_in() (ms_data_direction = 1)
#define | Usb_set_ms_data_direction_out() (ms_data_direction = 0)
#define | Is_usb_ms_data_direction_in() (ms_data_direction == 1)
## Functions
void | usb_mass_storage_cbw (void)
| USB Command Block Wrapper (CBW) management.
void | usb_mass_storage_csw (void)
| USB Command Status Wrapper (CSW) management.
void | storage_task_init (void)
| This function initializes the hardware/software ressources required for storage task.
void | storage_task (void)
| Entry point of the mass storage task management.
## Variables
U8 | usb_configuration_nb
static bit | ms_data_direction
static _MEM_TYPE_SLOW_ U8 | dCBWTag [4]
_MEM_TYPE_SLOW_ U8 | g_scsi_status
_MEM_TYPE_FAST_ U32 | g_scsi_data_remaining
bit | ms_multiple_drive
_MEM_TYPE_SLOW_ U8 | g_scsi_command [16]
_MEM_TYPE_SLOW_ U8 | usb_LUN

## Detailed Description

,vThis file manages the mass storage task.

Copyright (c) 2004 Atmel.

Please read file license.txt for copyright notice.

Version:
  1.2 at90usb162-2enum-mouse_ms-1_0_1
  Id
    storage_task.c,v 1.2 2007/01/08 16:09:31 arobert Exp

Todo:

Bug:
  /

Definition in file storage_task.c.

## Define Documentation

#define Usb_set_ms_data_direction_in | ( |  | ) | (ms_data_direction = 1)

Definition at line 65 of file storage_task.c.

Referenced by usb_mass_storage_cbw().

#define Usb_set_ms_data_direction_out | ( |  | ) | (ms_data_direction = 0)

Definition at line 66 of file storage_task.c.

Referenced by usb_mass_storage_cbw().

#define Is_usb_ms_data_direction_in | ( |  | ) | (ms_data_direction == 1)

Definition at line 67 of file storage_task.c.

Referenced by usb_mass_storage_cbw().

## Function Documentation

void usb_mass_storage_cbw | ( | void |  | ) |

USB Command Block Wrapper (CBW) management.

This function decodes the CBW command and stores the SCSI command

Warning:
  Code:?? bytes (function code length)

Parameters:

  | none |

Returns:
  none

check if dCBWSignature is correct

'U'

'S'

'B'

'C'

Store CBW Tag to be repeated in CSW

if (bmCBWFlags.bit7 == 1) {direction = IN}

dummy CBWCBLength read

Definition at line 127 of file storage_task.c.

References dCBWTag, EP_MS_IN, EP_MS_OUT, FALSE, g_scsi_command, g_scsi_data_remaining, get_cur_lun(), Is_usb_ms_data_direction_in, LSB0, LSB1, LSB2, LSB3, ms_multiple_drive, scsi_decode_command(), TRUE, Usb_ack_receive_out, Usb_enable_stall_handshake, usb_LUN, Usb_read_byte, Usb_select_endpoint, Usb_set_ms_data_direction_in, and Usb_set_ms_data_direction_out.

Referenced by storage_task().

    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 }

Here is the call graph for this function:

void usb_mass_storage_csw | ( | void |  | ) |

USB Command Status Wrapper (CSW) management.

This function sends the status in relation with the last CBW

Parameters:

  | none |

Returns:
  none

write CSW Signature

'U'

'S'

'B'

'S' write stored CBW Tag

write data residue value

write command status

0 -> PASS, 1 -> FAIL

Definition at line 216 of file storage_task.c.

References dCBWTag, EP_CONTROL, EP_MS_IN, EP_MS_OUT, g_scsi_data_remaining, g_scsi_status, Is_usb_endpoint_stall_requested, Is_usb_receive_setup, Is_usb_write_enabled, usb_process_request(), Usb_select_endpoint, Usb_send_in, and Usb_write_byte.

Referenced by storage_task().

    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 }

Here is the call graph for this function:

void storage_task_init | ( | void |  | ) |

This function initializes the hardware/software ressources required for storage task.

Parameters:

  | none |

Returns:
  none

/

Definition at line 79 of file storage_task.c.

References df_init_spi, df_mem_init(), and Leds_init.

    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 }

Here is the call graph for this function:

void storage_task | ( | void |  | ) |

Entry point of the mass storage task management.

This function links the mass storage SCSI commands and the USB bus.

Parameters:

  | none |

Returns:
  none

Definition at line 100 of file storage_task.c.

References EP_MS_OUT, Is_device_enumerated, Is_usb_receive_out, usb_mass_storage_cbw(), usb_mass_storage_csw(), and Usb_select_endpoint.

    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 }

Here is the call graph for this function:

## Variable Documentation

U8 usb_configuration_nb

Definition at line 71 of file usb_standard_request.c.

bit ms_data_direction`[static]`

Definition at line 50 of file storage_task.c.

_MEM_TYPE_SLOW_ U8 dCBWTag[4]`[static]`

Definition at line 51 of file storage_task.c.

Referenced by usb_mass_storage_cbw(), and usb_mass_storage_csw().

_MEM_TYPE_SLOW_ U8 g_scsi_status

Definition at line 35 of file scsi_decoder.c.

Referenced by usb_mass_storage_csw().

_MEM_TYPE_FAST_ U32 g_scsi_data_remaining

Definition at line 36 of file scsi_decoder.c.

Referenced by sbc_read_10(), sbc_write_10(), usb_mass_storage_cbw(), and usb_mass_storage_csw().

bit ms_multiple_drive

Definition at line 32 of file usb_specific_request.c.

Referenced by usb_mass_storage_cbw(), and usb_user_read_request().

_MEM_TYPE_SLOW_ U8 g_scsi_command[16]

Definition at line 34 of file scsi_decoder.c.

Referenced by sbc_inquiry(), sbc_mode_sense(), sbc_read_10(), sbc_request_sense(), sbc_write_10(), scsi_decode_command(), and usb_mass_storage_cbw().

_MEM_TYPE_SLOW_ U8 usb_LUN

Definition at line 59 of file storage_task.c.

Referenced by sbc_header_mode_sense(), sbc_read_10(), sbc_read_capacity(), sbc_test_unit_ready(), sbc_write_10(), and usb_mass_storage_cbw().