1
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 <asm/unaligned.h>
9
10#ifndef DEBUG
11#undef VERBOSE_DEBUG
12#undef DUMP_MSGS
13#endif
14
15#ifdef VERBOSE_DEBUG
16#define VLDBG LDBG
17#else
18#define VLDBG(lun, fmt, args...) do { } while (0)
19#endif
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, label, \
39 buf, length) \
40do { \
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, label, \
53 buf, 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
66
67#endif
68
69
70#define MAX_COMMAND_SIZE 16
71
72
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))
89#define ASC(x) ((u8) ((x) >> 8))
90#define ASCQ(x) ((u8) (x))
91
92
93
94
95
96#define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
97
98struct 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;
117
118 unsigned int blksize;
119 struct device dev;
120 const char *name;
121 const char **name_pfx;
122 char inquiry_string[INQUIRY_STRING_LEN];
123};
124
125static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
126{
127 return curlun->filp != NULL;
128}
129
130
131#define FSG_BUFLEN ((u32)16384)
132
133
134#define FSG_MAX_LUNS 16
135
136enum fsg_buffer_state {
137 BUF_STATE_SENDING = -2,
138 BUF_STATE_RECEIVING,
139 BUF_STATE_EMPTY = 0,
140 BUF_STATE_FULL
141};
142
143struct fsg_buffhd {
144 void *buf;
145 enum fsg_buffer_state state;
146 struct fsg_buffhd *next;
147
148
149
150
151
152
153 unsigned int bulk_out_intended_length;
154
155 struct usb_request *inreq;
156 struct usb_request *outreq;
157};
158
159enum 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
168enum data_direction {
169 DATA_DIR_UNKNOWN = 0,
170 DATA_DIR_FROM_HOST,
171 DATA_DIR_TO_HOST,
172 DATA_DIR_NONE
173};
174
175static inline u32 get_unaligned_be24(u8 *buf)
176{
177 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
178}
179
180static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
181{
182 return container_of(dev, struct fsg_lun, dev);
183}
184
185enum {
186 FSG_STRING_INTERFACE
187};
188
189extern struct usb_interface_descriptor fsg_intf_desc;
190
191extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
192extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
193extern struct usb_descriptor_header *fsg_fs_function[];
194
195extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
196extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
197extern struct usb_descriptor_header *fsg_hs_function[];
198
199extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
200extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
201extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
202extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
203extern struct usb_descriptor_header *fsg_ss_function[];
204
205void fsg_lun_close(struct fsg_lun *curlun);
206int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
207int fsg_lun_fsync_sub(struct fsg_lun *curlun);
208void store_cdrom_address(u8 *dest, int msf, u32 addr);
209ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
210ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
211ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
212 char *buf);
213ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf);
214ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
215ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
216ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
217 const char *buf, size_t count);
218ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
219ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
220 const char *buf, size_t count);
221ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
222 const char *buf, size_t count);
223ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
224 size_t count);
225ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
226 size_t count);
227
228#endif
229