Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 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 | |
Andrzej Pietrasiewicz | bd528d4 | 2013-10-09 10:05:53 +0200 | [diff] [blame] | 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 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 34 | |
| 35 | #ifdef DUMP_MSGS |
| 36 | |
| 37 | # define dump_msg(fsg, /* const char * */ label, \ |
| 38 | /* const u8 * */ buf, /* unsigned */ length) \ |
| 39 | do { \ |
| 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 | struct 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; |
Andrzej Pietrasiewicz | bd528d4 | 2013-10-09 10:05:53 +0200 | [diff] [blame] | 113 | const char *name; /* "lun.name" */ |
| 114 | const char **name_pfx; /* "function.name" */ |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static inline bool fsg_lun_is_open(struct fsg_lun *curlun) |
| 118 | { |
| 119 | return curlun->filp != NULL; |
| 120 | } |
| 121 | |
| 122 | /* Big enough to hold our biggest descriptor */ |
| 123 | #define EP0_BUFSIZE 256 |
| 124 | #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */ |
| 125 | |
| 126 | /* Default size of buffer length. */ |
| 127 | #define FSG_BUFLEN ((u32)16384) |
| 128 | |
| 129 | /* Maximal number of LUNs supported in mass storage function */ |
| 130 | #define FSG_MAX_LUNS 8 |
| 131 | |
| 132 | enum fsg_buffer_state { |
| 133 | BUF_STATE_EMPTY = 0, |
| 134 | BUF_STATE_FULL, |
| 135 | BUF_STATE_BUSY |
| 136 | }; |
| 137 | |
| 138 | struct fsg_buffhd { |
| 139 | void *buf; |
| 140 | enum fsg_buffer_state state; |
| 141 | struct fsg_buffhd *next; |
| 142 | |
| 143 | /* |
| 144 | * The NetChip 2280 is faster, and handles some protocol faults |
| 145 | * better, if we don't submit any short bulk-out read requests. |
| 146 | * So we will record the intended request length here. |
| 147 | */ |
| 148 | unsigned int bulk_out_intended_length; |
| 149 | |
| 150 | struct usb_request *inreq; |
| 151 | int inreq_busy; |
| 152 | struct usb_request *outreq; |
| 153 | int outreq_busy; |
| 154 | }; |
| 155 | |
| 156 | enum fsg_state { |
| 157 | /* This one isn't used anywhere */ |
| 158 | FSG_STATE_COMMAND_PHASE = -10, |
| 159 | FSG_STATE_DATA_PHASE, |
| 160 | FSG_STATE_STATUS_PHASE, |
| 161 | |
| 162 | FSG_STATE_IDLE = 0, |
| 163 | FSG_STATE_ABORT_BULK_OUT, |
| 164 | FSG_STATE_RESET, |
| 165 | FSG_STATE_INTERFACE_CHANGE, |
| 166 | FSG_STATE_CONFIG_CHANGE, |
| 167 | FSG_STATE_DISCONNECT, |
| 168 | FSG_STATE_EXIT, |
| 169 | FSG_STATE_TERMINATED |
| 170 | }; |
| 171 | |
| 172 | enum data_direction { |
| 173 | DATA_DIR_UNKNOWN = 0, |
| 174 | DATA_DIR_FROM_HOST, |
| 175 | DATA_DIR_TO_HOST, |
| 176 | DATA_DIR_NONE |
| 177 | }; |
| 178 | |
| 179 | static inline u32 get_unaligned_be24(u8 *buf) |
| 180 | { |
| 181 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); |
| 182 | } |
| 183 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 184 | static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev) |
| 185 | { |
| 186 | return container_of(dev, struct fsg_lun, dev); |
| 187 | } |
| 188 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 189 | enum { |
| 190 | FSG_STRING_INTERFACE |
| 191 | }; |
| 192 | |
| 193 | extern struct usb_interface_descriptor fsg_intf_desc; |
| 194 | |
| 195 | extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc; |
| 196 | extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc; |
| 197 | extern struct usb_descriptor_header *fsg_fs_function[]; |
| 198 | |
| 199 | extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc; |
| 200 | extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc; |
| 201 | extern struct usb_descriptor_header *fsg_hs_function[]; |
| 202 | |
| 203 | extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc; |
| 204 | extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc; |
| 205 | extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc; |
| 206 | extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc; |
| 207 | extern struct usb_descriptor_header *fsg_ss_function[]; |
| 208 | |
| 209 | void fsg_lun_close(struct fsg_lun *curlun); |
| 210 | int fsg_lun_open(struct fsg_lun *curlun, const char *filename); |
| 211 | int fsg_lun_fsync_sub(struct fsg_lun *curlun); |
| 212 | void store_cdrom_address(u8 *dest, int msf, u32 addr); |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 213 | ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf); |
| 214 | ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf); |
| 215 | ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 216 | char *buf); |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 217 | ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf); |
| 218 | ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf); |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 219 | ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 220 | const char *buf, size_t count); |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 221 | ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count); |
| 222 | ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 223 | const char *buf, size_t count); |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 224 | ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
| 225 | const char *buf, size_t count); |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 226 | ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf, |
| 227 | size_t count); |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 228 | |
| 229 | #endif /* USB_STORAGE_COMMON_H */ |