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 91struct fsg_lun { 92 struct file *filp; 93 loff_t file_length; 94 loff_t num_sectors; 95 96 unsigned int initially_ro:1; 97 unsigned int ro:1; 98 unsigned int removable:1; 99 unsigned int cdrom:1; 100 unsigned int prevent_medium_removal:1; 101 unsigned int registered:1; 102 unsigned int info_valid:1; 103 unsigned int nofua:1; 104 105 u32 sense_data; 106 u32 sense_data_info; 107 u32 unit_attention_data; 108 109 unsigned int blkbits; /* Bits of logical block size 110 of bound block device */ 111 unsigned int blksize; /* logical block size of bound block device */ 112 struct device dev; 113 const char *name; /* "lun.name" */ 114 const char **name_pfx; /* "function.name" */ 115}; 116 117static inline bool fsg_lun_is_open(struct fsg_lun *curlun) 118{ 119 return curlun->filp != NULL; 120} 121 122/* Default size of buffer length. */ 123#define FSG_BUFLEN ((u32)16384) 124 125/* Maximal number of LUNs supported in mass storage function */ 126#define FSG_MAX_LUNS 8 127 128enum fsg_buffer_state { 129 BUF_STATE_EMPTY = 0, 130 BUF_STATE_FULL, 131 BUF_STATE_BUSY 132}; 133 134struct fsg_buffhd { 135 void *buf; 136 enum fsg_buffer_state state; 137 struct fsg_buffhd *next; 138 139 /* 140 * The NetChip 2280 is faster, and handles some protocol faults 141 * better, if we don't submit any short bulk-out read requests. 142 * So we will record the intended request length here. 143 */ 144 unsigned int bulk_out_intended_length; 145 146 struct usb_request *inreq; 147 int inreq_busy; 148 struct usb_request *outreq; 149 int outreq_busy; 150}; 151 152enum fsg_state { 153 /* This one isn't used anywhere */ 154 FSG_STATE_COMMAND_PHASE = -10, 155 FSG_STATE_DATA_PHASE, 156 FSG_STATE_STATUS_PHASE, 157 158 FSG_STATE_IDLE = 0, 159 FSG_STATE_ABORT_BULK_OUT, 160 FSG_STATE_RESET, 161 FSG_STATE_INTERFACE_CHANGE, 162 FSG_STATE_CONFIG_CHANGE, 163 FSG_STATE_DISCONNECT, 164 FSG_STATE_EXIT, 165 FSG_STATE_TERMINATED 166}; 167 168enum data_direction { 169 DATA_DIR_UNKNOWN = 0, 170 DATA_DIR_FROM_HOST, 171 DATA_DIR_TO_HOST, 172 DATA_DIR_NONE 173}; 174 175static inline u32 get_unaligned_be24(u8 *buf) 176{ 177 return 0xffffff & (u32) get_unaligned_be32(buf - 1); 178} 179 180static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev) 181{ 182 return container_of(dev, struct fsg_lun, dev); 183} 184 185enum { 186 FSG_STRING_INTERFACE 187}; 188 189extern struct usb_interface_descriptor fsg_intf_desc; 190 191extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc; 192extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc; 193extern struct usb_descriptor_header *fsg_fs_function[]; 194 195extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc; 196extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc; 197extern struct usb_descriptor_header *fsg_hs_function[]; 198 199extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc; 200extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc; 201extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc; 202extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc; 203extern struct usb_descriptor_header *fsg_ss_function[]; 204 205void fsg_lun_close(struct fsg_lun *curlun); 206int fsg_lun_open(struct fsg_lun *curlun, const char *filename); 207int fsg_lun_fsync_sub(struct fsg_lun *curlun); 208void store_cdrom_address(u8 *dest, int msf, u32 addr); 209ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf); 210ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf); 211ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, 212 char *buf); 213ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf); 214ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf); 215ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem, 216 const char *buf, size_t count); 217ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count); 218ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, 219 const char *buf, size_t count); 220ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem, 221 const char *buf, size_t count); 222ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf, 223 size_t count); 224 225#endif /* USB_STORAGE_COMMON_H */ 226