Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / usb / gadget / storage_common.h
blobc74c2fdbd56eda5683a13995a96b6221711c30ef
1 #ifndef USB_STORAGE_COMMON_H
2 #define USB_STORAGE_COMMON_H
4 #include <linux/device.h>
5 #include <linux/usb/storage.h>
6 #include <scsi/scsi.h>
7 #include <asm/unaligned.h>
9 #ifndef DEBUG
10 #undef VERBOSE_DEBUG
11 #undef DUMP_MSGS
12 #endif /* !DEBUG */
14 #ifdef VERBOSE_DEBUG
15 #define VLDBG LDBG
16 #else
17 #define VLDBG(lun, fmt, args...) do { } while (0)
18 #endif /* VERBOSE_DEBUG */
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)
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)
35 #ifdef DUMP_MSGS
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)
47 # define dump_cdb(fsg) do { } while (0)
49 #else
51 # define dump_msg(fsg, /* const char * */ label, \
52 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
54 # ifdef VERBOSE_DEBUG
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) \
60 # else
62 # define dump_cdb(fsg) do { } while (0)
64 # endif /* VERBOSE_DEBUG */
66 #endif /* DUMP_MSGS */
68 /* Length of a SCSI Command Data Block */
69 #define MAX_COMMAND_SIZE 16
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
87 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
88 #define ASC(x) ((u8) ((x) >> 8))
89 #define ASCQ(x) ((u8) (x))
91 struct fsg_lun {
92 struct file *filp;
93 loff_t file_length;
94 loff_t num_sectors;
96 unsigned int initially_ro:1;
97 unsigned int ro:1;
98 unsigned int removable:1;
99 unsigned int cdrom:1;
100 unsigned int prevent_medium_removal:1;
101 unsigned int registered:1;
102 unsigned int info_valid:1;
103 unsigned int nofua:1;
105 u32 sense_data;
106 u32 sense_data_info;
107 u32 unit_attention_data;
109 unsigned int blkbits; /* Bits of logical block size
110 of bound block device */
111 unsigned int blksize; /* logical block size of bound block device */
112 struct device dev;
113 const char *name; /* "lun.name" */
114 const char **name_pfx; /* "function.name" */
117 static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
119 return curlun->filp != NULL;
122 /* Big enough to hold our biggest descriptor */
123 #define EP0_BUFSIZE 256
124 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
126 /* Default size of buffer length. */
127 #define FSG_BUFLEN ((u32)16384)
129 /* Maximal number of LUNs supported in mass storage function */
130 #define FSG_MAX_LUNS 8
132 enum fsg_buffer_state {
133 BUF_STATE_EMPTY = 0,
134 BUF_STATE_FULL,
135 BUF_STATE_BUSY
138 struct fsg_buffhd {
139 void *buf;
140 enum fsg_buffer_state state;
141 struct fsg_buffhd *next;
144 * The NetChip 2280 is faster, and handles some protocol faults
145 * better, if we don't submit any short bulk-out read requests.
146 * So we will record the intended request length here.
148 unsigned int bulk_out_intended_length;
150 struct usb_request *inreq;
151 int inreq_busy;
152 struct usb_request *outreq;
153 int outreq_busy;
156 enum fsg_state {
157 /* This one isn't used anywhere */
158 FSG_STATE_COMMAND_PHASE = -10,
159 FSG_STATE_DATA_PHASE,
160 FSG_STATE_STATUS_PHASE,
162 FSG_STATE_IDLE = 0,
163 FSG_STATE_ABORT_BULK_OUT,
164 FSG_STATE_RESET,
165 FSG_STATE_INTERFACE_CHANGE,
166 FSG_STATE_CONFIG_CHANGE,
167 FSG_STATE_DISCONNECT,
168 FSG_STATE_EXIT,
169 FSG_STATE_TERMINATED
172 enum data_direction {
173 DATA_DIR_UNKNOWN = 0,
174 DATA_DIR_FROM_HOST,
175 DATA_DIR_TO_HOST,
176 DATA_DIR_NONE
179 static inline u32 get_unaligned_be24(u8 *buf)
181 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
184 static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
186 return container_of(dev, struct fsg_lun, dev);
189 enum {
190 FSG_STRING_INTERFACE
193 extern struct usb_interface_descriptor fsg_intf_desc;
195 extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
196 extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
197 extern struct usb_descriptor_header *fsg_fs_function[];
199 extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
200 extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
201 extern struct usb_descriptor_header *fsg_hs_function[];
203 extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
204 extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
205 extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
206 extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
207 extern struct usb_descriptor_header *fsg_ss_function[];
209 void fsg_lun_close(struct fsg_lun *curlun);
210 int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
211 int fsg_lun_fsync_sub(struct fsg_lun *curlun);
212 void store_cdrom_address(u8 *dest, int msf, u32 addr);
213 ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
214 ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
215 ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
216 char *buf);
217 ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
218 ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
219 ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
220 const char *buf, size_t count);
221 ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
222 ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
223 const char *buf, size_t count);
224 ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
225 const char *buf, size_t count);
226 ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
227 size_t count);
229 #endif /* USB_STORAGE_COMMON_H */