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>
8 #include <linux/unaligned.h>
18 #define VLDBG(lun, fmt, args...) do { } while (0)
19 #endif /* VERBOSE_DEBUG */
21 #define _LMSG(func, lun, fmt, args...) \
23 if ((lun)->name_pfx && *(lun)->name_pfx) \
24 func("%s/%s: " fmt, *(lun)->name_pfx, \
25 (lun)->name, ## args); \
27 func("%s: " fmt, (lun)->name, ## args); \
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)
38 # define dump_msg(fsg, /* const char * */ label, \
39 /* const u8 * */ buf, /* unsigned */ length) \
42 DBG(fsg, "%s, length %u:\n", label, length); \
43 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
44 16, 1, buf, length, 0); \
48 # define dump_cdb(fsg) do { } while (0)
52 # define dump_msg(fsg, /* const char * */ label, \
53 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
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) \
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 */
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
96 #define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
103 unsigned int initially_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;
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 */
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 (US_BULK_MAX_LUN_LIMIT + 1)
136 enum fsg_buffer_state {
137 BUF_STATE_SENDING = -2,
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;
161 FSG_STATE_ABORT_BULK_OUT,
162 FSG_STATE_PROTOCOL_RESET,
163 FSG_STATE_CONFIG_CHANGE,
168 enum data_direction {
169 DATA_DIR_UNKNOWN = 0,
175 static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
177 return container_of(dev, struct fsg_lun, dev);
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,
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,
220 ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
222 ssize_t fsg_store_forced_eject(struct fsg_lun *curlun, struct rw_semaphore *filesem,
223 const char *buf, size_t count);
225 #endif /* USB_STORAGE_COMMON_H */