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