linux/drivers/usb/gadget/function/storage_common.h
<<
>>
Prefs
   1#ifndef USB_STORAGE_COMMON_H
   2#define USB_STORAGE_COMMON_H
   3
   4#include <linux/device.h>
   5#include <linux/usb/storage.h>
   6#include <scsi/scsi.h>
   7#include <asm/unaligned.h>
   8
   9#ifndef DEBUG
  10#undef VERBOSE_DEBUG
  11#undef DUMP_MSGS
  12#endif /* !DEBUG */
  13
  14#ifdef VERBOSE_DEBUG
  15#define VLDBG   LDBG
  16#else
  17#define VLDBG(lun, fmt, args...) do { } while (0)
  18#endif /* VERBOSE_DEBUG */
  19
  20#define _LMSG(func, lun, fmt, args...)                                  \
  21        do {                                                            \
  22                if ((lun)->name_pfx && *(lun)->name_pfx)                \
  23                        func("%s/%s: " fmt, *(lun)->name_pfx,           \
  24                                 (lun)->name, ## args);                 \
  25                else                                                    \
  26                        func("%s: " fmt, (lun)->name, ## args);         \
  27        } while (0)
  28
  29#define LDBG(lun, fmt, args...)         _LMSG(pr_debug, lun, fmt, ## args)
  30#define LERROR(lun, fmt, args...)       _LMSG(pr_err, lun, fmt, ## args)
  31#define LWARN(lun, fmt, args...)        _LMSG(pr_warn, lun, fmt, ## args)
  32#define LINFO(lun, fmt, args...)        _LMSG(pr_info, lun, fmt, ## args)
  33
  34
  35#ifdef DUMP_MSGS
  36
  37#  define dump_msg(fsg, /* const char * */ label,                       \
  38                   /* const u8 * */ buf, /* unsigned */ length)         \
  39do {                                                                    \
  40        if (length < 512) {                                             \
  41                DBG(fsg, "%s, length %u:\n", label, length);            \
  42                print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,      \
  43                               16, 1, buf, length, 0);                  \
  44        }                                                               \
  45} while (0)
  46
  47#  define dump_cdb(fsg) do { } while (0)
  48
  49#else
  50
  51#  define dump_msg(fsg, /* const char * */ label, \
  52                   /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  53
  54#  ifdef VERBOSE_DEBUG
  55
  56#    define dump_cdb(fsg)                                               \
  57        print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE,      \
  58                       16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0)         \
  59
  60#  else
  61
  62#    define dump_cdb(fsg) do { } while (0)
  63
  64#  endif /* VERBOSE_DEBUG */
  65
  66#endif /* DUMP_MSGS */
  67
  68/* Length of a SCSI Command Data Block */
  69#define MAX_COMMAND_SIZE        16
  70
  71/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  72#define SS_NO_SENSE                             0
  73#define SS_COMMUNICATION_FAILURE                0x040800
  74#define SS_INVALID_COMMAND                      0x052000
  75#define SS_INVALID_FIELD_IN_CDB                 0x052400
  76#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE   0x052100
  77#define SS_LOGICAL_UNIT_NOT_SUPPORTED           0x052500
  78#define SS_MEDIUM_NOT_PRESENT                   0x023a00
  79#define SS_MEDIUM_REMOVAL_PREVENTED             0x055302
  80#define SS_NOT_READY_TO_READY_TRANSITION        0x062800
  81#define SS_RESET_OCCURRED                       0x062900
  82#define SS_SAVING_PARAMETERS_NOT_SUPPORTED      0x053900
  83#define SS_UNRECOVERED_READ_ERROR               0x031100
  84#define SS_WRITE_ERROR                          0x030c02
  85#define SS_WRITE_PROTECTED                      0x072700
  86
  87#define SK(x)           ((u8) ((x) >> 16))      /* Sense Key byte, etc. */
  88#define ASC(x)          ((u8) ((x) >> 8))
  89#define ASCQ(x)         ((u8) (x))
  90
  91/*
  92 * Vendor (8 chars), product (16 chars), release (4 hexadecimal digits) and NUL
  93 * byte
  94 */
  95#define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
  96
  97struct fsg_lun {
  98        struct file     *filp;
  99        loff_t          file_length;
 100        loff_t          num_sectors;
 101
 102        unsigned int    initially_ro:1;
 103        unsigned int    ro:1;
 104        unsigned int    removable:1;
 105        unsigned int    cdrom:1;
 106        unsigned int    prevent_medium_removal:1;
 107        unsigned int    registered:1;
 108        unsigned int    info_valid:1;
 109        unsigned int    nofua:1;
 110
 111        u32             sense_data;
 112        u32             sense_data_info;
 113        u32             unit_attention_data;
 114
 115        unsigned int    blkbits; /* Bits of logical block size
 116                                                       of bound block device */
 117        unsigned int    blksize; /* logical block size of bound block device */
 118        struct device   dev;
 119        const char      *name;          /* "lun.name" */
 120        const char      **name_pfx;     /* "function.name" */
 121        char            inquiry_string[INQUIRY_STRING_LEN];
 122};
 123
 124static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
 125{
 126        return curlun->filp != NULL;
 127}
 128
 129/* Default size of buffer length. */
 130#define FSG_BUFLEN      ((u32)16384)
 131
 132/* Maximal number of LUNs supported in mass storage function */
 133#define FSG_MAX_LUNS    16
 134
 135enum fsg_buffer_state {
 136        BUF_STATE_EMPTY = 0,
 137        BUF_STATE_FULL,
 138        BUF_STATE_BUSY
 139};
 140
 141struct fsg_buffhd {
 142        void                            *buf;
 143        enum fsg_buffer_state           state;
 144        struct fsg_buffhd               *next;
 145
 146        /*
 147         * The NetChip 2280 is faster, and handles some protocol faults
 148         * better, if we don't submit any short bulk-out read requests.
 149         * So we will record the intended request length here.
 150         */
 151        unsigned int                    bulk_out_intended_length;
 152
 153        struct usb_request              *inreq;
 154        int                             inreq_busy;
 155        struct usb_request              *outreq;
 156        int                             outreq_busy;
 157};
 158
 159enum fsg_state {
 160        /* This one isn't used anywhere */
 161        FSG_STATE_COMMAND_PHASE = -10,
 162        FSG_STATE_DATA_PHASE,
 163        FSG_STATE_STATUS_PHASE,
 164
 165        FSG_STATE_IDLE = 0,
 166        FSG_STATE_ABORT_BULK_OUT,
 167        FSG_STATE_RESET,
 168        FSG_STATE_INTERFACE_CHANGE,
 169        FSG_STATE_CONFIG_CHANGE,
 170        FSG_STATE_DISCONNECT,
 171        FSG_STATE_EXIT,
 172        FSG_STATE_TERMINATED
 173};
 174
 175enum data_direction {
 176        DATA_DIR_UNKNOWN = 0,
 177        DATA_DIR_FROM_HOST,
 178        DATA_DIR_TO_HOST,
 179        DATA_DIR_NONE
 180};
 181
 182static inline u32 get_unaligned_be24(u8 *buf)
 183{
 184        return 0xffffff & (u32) get_unaligned_be32(buf - 1);
 185}
 186
 187static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
 188{
 189        return container_of(dev, struct fsg_lun, dev);
 190}
 191
 192enum {
 193        FSG_STRING_INTERFACE
 194};
 195
 196extern struct usb_interface_descriptor fsg_intf_desc;
 197
 198extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
 199extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
 200extern struct usb_descriptor_header *fsg_fs_function[];
 201
 202extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
 203extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
 204extern struct usb_descriptor_header *fsg_hs_function[];
 205
 206extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
 207extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
 208extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
 209extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
 210extern struct usb_descriptor_header *fsg_ss_function[];
 211
 212void fsg_lun_close(struct fsg_lun *curlun);
 213int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
 214int fsg_lun_fsync_sub(struct fsg_lun *curlun);
 215void store_cdrom_address(u8 *dest, int msf, u32 addr);
 216ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
 217ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
 218ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 219                      char *buf);
 220ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf);
 221ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
 222ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
 223ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 224                     const char *buf, size_t count);
 225ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
 226ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 227                       const char *buf, size_t count);
 228ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 229                        const char *buf, size_t count);
 230ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
 231                            size_t count);
 232ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
 233                                 size_t count);
 234
 235#endif /* USB_STORAGE_COMMON_H */
 236