blob: 1fcda2bfef6ce13d975edb02f30c9b3be8fc6c50 [file] [log] [blame]
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +02001#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 LDBG(lun, fmt, args...) dev_dbg(&(lun)->dev, fmt, ## args)
21#define LERROR(lun, fmt, args...) dev_err(&(lun)->dev, fmt, ## args)
22#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
23#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
24
25#ifdef DUMP_MSGS
26
27# define dump_msg(fsg, /* const char * */ label, \
28 /* const u8 * */ buf, /* unsigned */ length) \
29do { \
30 if (length < 512) { \
31 DBG(fsg, "%s, length %u:\n", label, length); \
32 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
33 16, 1, buf, length, 0); \
34 } \
35} while (0)
36
37# define dump_cdb(fsg) do { } while (0)
38
39#else
40
41# define dump_msg(fsg, /* const char * */ label, \
42 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
43
44# ifdef VERBOSE_DEBUG
45
46# define dump_cdb(fsg) \
47 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
48 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
49
50# else
51
52# define dump_cdb(fsg) do { } while (0)
53
54# endif /* VERBOSE_DEBUG */
55
56#endif /* DUMP_MSGS */
57
58/* Length of a SCSI Command Data Block */
59#define MAX_COMMAND_SIZE 16
60
61/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
62#define SS_NO_SENSE 0
63#define SS_COMMUNICATION_FAILURE 0x040800
64#define SS_INVALID_COMMAND 0x052000
65#define SS_INVALID_FIELD_IN_CDB 0x052400
66#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
67#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
68#define SS_MEDIUM_NOT_PRESENT 0x023a00
69#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
70#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
71#define SS_RESET_OCCURRED 0x062900
72#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
73#define SS_UNRECOVERED_READ_ERROR 0x031100
74#define SS_WRITE_ERROR 0x030c02
75#define SS_WRITE_PROTECTED 0x072700
76
77#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
78#define ASC(x) ((u8) ((x) >> 8))
79#define ASCQ(x) ((u8) (x))
80
81struct fsg_lun {
82 struct file *filp;
83 loff_t file_length;
84 loff_t num_sectors;
85
86 unsigned int initially_ro:1;
87 unsigned int ro:1;
88 unsigned int removable:1;
89 unsigned int cdrom:1;
90 unsigned int prevent_medium_removal:1;
91 unsigned int registered:1;
92 unsigned int info_valid:1;
93 unsigned int nofua:1;
94
95 u32 sense_data;
96 u32 sense_data_info;
97 u32 unit_attention_data;
98
99 unsigned int blkbits; /* Bits of logical block size
100 of bound block device */
101 unsigned int blksize; /* logical block size of bound block device */
102 struct device dev;
103};
104
105static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
106{
107 return curlun->filp != NULL;
108}
109
110/* Big enough to hold our biggest descriptor */
111#define EP0_BUFSIZE 256
112#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
113
114/* Default size of buffer length. */
115#define FSG_BUFLEN ((u32)16384)
116
117/* Maximal number of LUNs supported in mass storage function */
118#define FSG_MAX_LUNS 8
119
120enum fsg_buffer_state {
121 BUF_STATE_EMPTY = 0,
122 BUF_STATE_FULL,
123 BUF_STATE_BUSY
124};
125
126struct fsg_buffhd {
127 void *buf;
128 enum fsg_buffer_state state;
129 struct fsg_buffhd *next;
130
131 /*
132 * The NetChip 2280 is faster, and handles some protocol faults
133 * better, if we don't submit any short bulk-out read requests.
134 * So we will record the intended request length here.
135 */
136 unsigned int bulk_out_intended_length;
137
138 struct usb_request *inreq;
139 int inreq_busy;
140 struct usb_request *outreq;
141 int outreq_busy;
142};
143
144enum fsg_state {
145 /* This one isn't used anywhere */
146 FSG_STATE_COMMAND_PHASE = -10,
147 FSG_STATE_DATA_PHASE,
148 FSG_STATE_STATUS_PHASE,
149
150 FSG_STATE_IDLE = 0,
151 FSG_STATE_ABORT_BULK_OUT,
152 FSG_STATE_RESET,
153 FSG_STATE_INTERFACE_CHANGE,
154 FSG_STATE_CONFIG_CHANGE,
155 FSG_STATE_DISCONNECT,
156 FSG_STATE_EXIT,
157 FSG_STATE_TERMINATED
158};
159
160enum data_direction {
161 DATA_DIR_UNKNOWN = 0,
162 DATA_DIR_FROM_HOST,
163 DATA_DIR_TO_HOST,
164 DATA_DIR_NONE
165};
166
167static inline u32 get_unaligned_be24(u8 *buf)
168{
169 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
170}
171
172enum {
173 FSG_STRING_INTERFACE
174};
175
176extern struct usb_interface_descriptor fsg_intf_desc;
177
178extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
179extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
180extern struct usb_descriptor_header *fsg_fs_function[];
181
182extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
183extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
184extern struct usb_descriptor_header *fsg_hs_function[];
185
186extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
187extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
188extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
189extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
190extern struct usb_descriptor_header *fsg_ss_function[];
191
192void fsg_lun_close(struct fsg_lun *curlun);
193int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
194int fsg_lun_fsync_sub(struct fsg_lun *curlun);
195void store_cdrom_address(u8 *dest, int msf, u32 addr);
196ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
197 char *buf);
198ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
199 char *buf);
200ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
201 char *buf);
202ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
203 const char *buf, size_t count);
204ssize_t fsg_store_nofua(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf, size_t count);
207ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count);
209
210#endif /* USB_STORAGE_COMMON_H */