1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef USB_STORAGE_COMMON_H
3 #define USB_STORAGE_COMMON_H
4
5 #include <linux/device.h>
6 #include <linux/usb/storage.h>
7 #include <scsi/scsi.h>
8 #include <linux/unaligned.h>
9
10 #ifndef DEBUG
11 #undef VERBOSE_DEBUG
12 #undef DUMP_MSGS
13 #endif /* !DEBUG */
14
15 #ifdef VERBOSE_DEBUG
16 #define VLDBG LDBG
17 #else
18 #define VLDBG(lun, fmt, args...) do { } while (0)
19 #endif /* VERBOSE_DEBUG */
20
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)
29
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)
34
35
36 #ifdef DUMP_MSGS
37
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)
47
48 # define dump_cdb(fsg) do { } while (0)
49
50 #else
51
52 # define dump_msg(fsg, /* const char * */ label, \
53 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
54
55 # ifdef VERBOSE_DEBUG
56
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) \
60
61 # else
62
63 # define dump_cdb(fsg) do { } while (0)
64
65 # endif /* VERBOSE_DEBUG */
66
67 #endif /* DUMP_MSGS */
68
69 /* Length of a SCSI Command Data Block */
70 #define MAX_COMMAND_SIZE 16
71
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
87
88 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
89 #define ASC(x) ((u8) ((x) >> 8))
90 #define ASCQ(x) ((u8) (x))
91
92 /*
93 * Vendor (8 chars), product (16 chars), release (4 hexadecimal digits) and NUL
94 * byte
95 */
96 #define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
97
98 struct fsg_lun {
99 struct file *filp;
100 loff_t file_length;
101 loff_t num_sectors;
102
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;
111
112 u32 sense_data;
113 u32 sense_data_info;
114 u32 unit_attention_data;
115
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];
123 };
124
fsg_lun_is_open(struct fsg_lun * curlun)125 static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
126 {
127 return curlun->filp != NULL;
128 }
129
130 /* Default size of buffer length. */
131 #define FSG_BUFLEN ((u32)16384)
132
133 /* Maximal number of LUNs supported in mass storage function */
134 #define FSG_MAX_LUNS 16
135
136 enum fsg_buffer_state {
137 BUF_STATE_SENDING = -2,
138 BUF_STATE_RECEIVING,
139 BUF_STATE_EMPTY = 0,
140 BUF_STATE_FULL
141 };
142
143 struct fsg_buffhd {
144 void *buf;
145 enum fsg_buffer_state state;
146 struct fsg_buffhd *next;
147
148 /*
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.
152 */
153 unsigned int bulk_out_intended_length;
154
155 struct usb_request *inreq;
156 struct usb_request *outreq;
157 };
158
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
166 };
167
168 enum data_direction {
169 DATA_DIR_UNKNOWN = 0,
170 DATA_DIR_FROM_HOST,
171 DATA_DIR_TO_HOST,
172 DATA_DIR_NONE
173 };
174
fsg_lun_from_dev(struct device * dev)175 static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
176 {
177 return container_of(dev, struct fsg_lun, dev);
178 }
179
180 enum {
181 FSG_STRING_INTERFACE
182 };
183
184 extern struct usb_interface_descriptor fsg_intf_desc;
185
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[];
189
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[];
193
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[];
199
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);
222 ssize_t fsg_store_forced_eject(struct fsg_lun *curlun, struct rw_semaphore *filesem,
223 const char *buf, size_t count);
224
225 #endif /* USB_STORAGE_COMMON_H */
226