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