1dee1d999SLukasz Majewski /*
2dee1d999SLukasz Majewski  * storage_common.c -- Common definitions for mass storage functionality
3dee1d999SLukasz Majewski  *
4dee1d999SLukasz Majewski  * Copyright (C) 2003-2008 Alan Stern
5dee1d999SLukasz Majewski  * Copyeight (C) 2009 Samsung Electronics
6dee1d999SLukasz Majewski  * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
7dee1d999SLukasz Majewski  *
8dee1d999SLukasz Majewski  * Ported to u-boot:
9dee1d999SLukasz Majewski  * Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10dee1d999SLukasz Majewski  *
11dee1d999SLukasz Majewski  * Code refactoring & cleanup:
12dee1d999SLukasz Majewski  * Łukasz Majewski <l.majewski@samsung.com>
13dee1d999SLukasz Majewski  *
141a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
15dee1d999SLukasz Majewski  */
16dee1d999SLukasz Majewski 
17dee1d999SLukasz Majewski 
18dee1d999SLukasz Majewski /*
19dee1d999SLukasz Majewski  * This file requires the following identifiers used in USB strings to
20dee1d999SLukasz Majewski  * be defined (each of type pointer to char):
21dee1d999SLukasz Majewski  *  - fsg_string_manufacturer -- name of the manufacturer
22dee1d999SLukasz Majewski  *  - fsg_string_product      -- name of the product
23dee1d999SLukasz Majewski  *  - fsg_string_serial       -- product's serial
24dee1d999SLukasz Majewski  *  - fsg_string_config       -- name of the configuration
25dee1d999SLukasz Majewski  *  - fsg_string_interface    -- name of the interface
26dee1d999SLukasz Majewski  * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
27dee1d999SLukasz Majewski  * macro is defined prior to including this file.
28dee1d999SLukasz Majewski  */
29dee1d999SLukasz Majewski 
30dee1d999SLukasz Majewski /*
31dee1d999SLukasz Majewski  * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
32dee1d999SLukasz Majewski  * fsg_hs_intr_in_desc objects as well as
33dee1d999SLukasz Majewski  * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
34dee1d999SLukasz Majewski  * macros are not defined.
35dee1d999SLukasz Majewski  *
36dee1d999SLukasz Majewski  * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
37dee1d999SLukasz Majewski  * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
38dee1d999SLukasz Majewski  * defined (as well as corresponding entries in string tables are
39dee1d999SLukasz Majewski  * missing) and FSG_STRING_INTERFACE has value of zero.
40dee1d999SLukasz Majewski  *
41dee1d999SLukasz Majewski  * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
42dee1d999SLukasz Majewski  */
43dee1d999SLukasz Majewski 
44dee1d999SLukasz Majewski /*
45dee1d999SLukasz Majewski  * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
46dee1d999SLukasz Majewski  * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
47dee1d999SLukasz Majewski  * characters rather then a pointer to void.
48dee1d999SLukasz Majewski  */
49dee1d999SLukasz Majewski 
50dee1d999SLukasz Majewski 
51dee1d999SLukasz Majewski /* #include <asm/unaligned.h> */
52dee1d999SLukasz Majewski 
53dee1d999SLukasz Majewski 
54dee1d999SLukasz Majewski /*
55dee1d999SLukasz Majewski  * Thanks to NetChip Technologies for donating this product ID.
56dee1d999SLukasz Majewski  *
57dee1d999SLukasz Majewski  * DO NOT REUSE THESE IDs with any other driver!!  Ever!!
58dee1d999SLukasz Majewski  * Instead:  allocate your own, using normal USB-IF procedures.
59dee1d999SLukasz Majewski  */
60dee1d999SLukasz Majewski #define FSG_VENDOR_ID	0x0525	/* NetChip */
61dee1d999SLukasz Majewski #define FSG_PRODUCT_ID	0xa4a5	/* Linux-USB File-backed Storage Gadget */
62dee1d999SLukasz Majewski 
63dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
64dee1d999SLukasz Majewski 
65dee1d999SLukasz Majewski #ifndef DEBUG
66dee1d999SLukasz Majewski #undef VERBOSE_DEBUG
67dee1d999SLukasz Majewski #undef DUMP_MSGS
68dee1d999SLukasz Majewski #endif /* !DEBUG */
69dee1d999SLukasz Majewski 
70dee1d999SLukasz Majewski #ifdef VERBOSE_DEBUG
71dee1d999SLukasz Majewski #define VLDBG	LDBG
72dee1d999SLukasz Majewski #else
73dee1d999SLukasz Majewski #define VLDBG(lun, fmt, args...) do { } while (0)
74dee1d999SLukasz Majewski #endif /* VERBOSE_DEBUG */
75dee1d999SLukasz Majewski 
76dee1d999SLukasz Majewski /*
77dee1d999SLukasz Majewski #define LDBG(lun, fmt, args...)   dev_dbg (&(lun)->dev, fmt, ## args)
78dee1d999SLukasz Majewski #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
79dee1d999SLukasz Majewski #define LWARN(lun, fmt, args...)  dev_warn(&(lun)->dev, fmt, ## args)
80dee1d999SLukasz Majewski #define LINFO(lun, fmt, args...)  dev_info(&(lun)->dev, fmt, ## args)
81dee1d999SLukasz Majewski */
82dee1d999SLukasz Majewski 
83dee1d999SLukasz Majewski #define LDBG(lun, fmt, args...) do { } while (0)
84dee1d999SLukasz Majewski #define LERROR(lun, fmt, args...) do { } while (0)
85dee1d999SLukasz Majewski #define LWARN(lun, fmt, args...) do { } while (0)
86dee1d999SLukasz Majewski #define LINFO(lun, fmt, args...) do { } while (0)
87dee1d999SLukasz Majewski 
88dee1d999SLukasz Majewski /*
89dee1d999SLukasz Majewski  * Keep those macros in sync with those in
90dee1d999SLukasz Majewski  * include/linux/usb/composite.h or else GCC will complain.  If they
91dee1d999SLukasz Majewski  * are identical (the same names of arguments, white spaces in the
92dee1d999SLukasz Majewski  * same places) GCC will allow redefinition otherwise (even if some
93dee1d999SLukasz Majewski  * white space is removed or added) warning will be issued.
94dee1d999SLukasz Majewski  *
95dee1d999SLukasz Majewski  * Those macros are needed here because File Storage Gadget does not
96dee1d999SLukasz Majewski  * include the composite.h header.  For composite gadgets those macros
97dee1d999SLukasz Majewski  * are redundant since composite.h is included any way.
98dee1d999SLukasz Majewski  *
99dee1d999SLukasz Majewski  * One could check whether those macros are already defined (which
100dee1d999SLukasz Majewski  * would indicate composite.h had been included) or not (which would
101dee1d999SLukasz Majewski  * indicate we were in FSG) but this is not done because a warning is
102dee1d999SLukasz Majewski  * desired if definitions here differ from the ones in composite.h.
103dee1d999SLukasz Majewski  *
104dee1d999SLukasz Majewski  * We want the definitions to match and be the same in File Storage
105dee1d999SLukasz Majewski  * Gadget as well as Mass Storage Function (and so composite gadgets
106dee1d999SLukasz Majewski  * using MSF).  If someone changes them in composite.h it will produce
107dee1d999SLukasz Majewski  * a warning in this file when building MSF.
108dee1d999SLukasz Majewski  */
109dee1d999SLukasz Majewski 
110dee1d999SLukasz Majewski #define DBG(d, fmt, args...)     debug(fmt , ## args)
111dee1d999SLukasz Majewski #define VDBG(d, fmt, args...)    debug(fmt , ## args)
112dee1d999SLukasz Majewski /* #define ERROR(d, fmt, args...)   printf(fmt , ## args) */
113dee1d999SLukasz Majewski /* #define WARNING(d, fmt, args...) printf(fmt , ## args) */
114dee1d999SLukasz Majewski /* #define INFO(d, fmt, args...)    printf(fmt , ## args) */
115dee1d999SLukasz Majewski 
116dee1d999SLukasz Majewski /* #define DBG(d, fmt, args...)     do { } while (0) */
117dee1d999SLukasz Majewski /* #define VDBG(d, fmt, args...)    do { } while (0) */
118dee1d999SLukasz Majewski #define ERROR(d, fmt, args...)   do { } while (0)
119dee1d999SLukasz Majewski #define WARNING(d, fmt, args...) do { } while (0)
120dee1d999SLukasz Majewski #define INFO(d, fmt, args...)    do { } while (0)
121dee1d999SLukasz Majewski 
122dee1d999SLukasz Majewski #ifdef DUMP_MSGS
123dee1d999SLukasz Majewski 
124dee1d999SLukasz Majewski /* dump_msg(fsg, const char * label, const u8 * buf, unsigned length); */
125dee1d999SLukasz Majewski # define dump_msg(fsg, label, buf, length) do {                         \
126dee1d999SLukasz Majewski 	if (length < 512) {						\
127dee1d999SLukasz Majewski 		DBG(fsg, "%s, length %u:\n", label, length);		\
128dee1d999SLukasz Majewski 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,	\
129dee1d999SLukasz Majewski 			       16, 1, buf, length, 0);			\
130dee1d999SLukasz Majewski 	}								\
131dee1d999SLukasz Majewski } while (0)
132dee1d999SLukasz Majewski 
133dee1d999SLukasz Majewski #  define dump_cdb(fsg) do { } while (0)
134dee1d999SLukasz Majewski 
135dee1d999SLukasz Majewski #else
136dee1d999SLukasz Majewski 
137dee1d999SLukasz Majewski #  define dump_msg(fsg, /* const char * */ label, \
138dee1d999SLukasz Majewski 		   /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
139dee1d999SLukasz Majewski 
140dee1d999SLukasz Majewski #  ifdef VERBOSE_DEBUG
141dee1d999SLukasz Majewski 
142dee1d999SLukasz Majewski #    define dump_cdb(fsg)						\
143dee1d999SLukasz Majewski 	print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE,	\
144dee1d999SLukasz Majewski 		       16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0)		\
145dee1d999SLukasz Majewski 
146dee1d999SLukasz Majewski #  else
147dee1d999SLukasz Majewski 
148dee1d999SLukasz Majewski #    define dump_cdb(fsg) do { } while (0)
149dee1d999SLukasz Majewski 
150dee1d999SLukasz Majewski #  endif /* VERBOSE_DEBUG */
151dee1d999SLukasz Majewski 
152dee1d999SLukasz Majewski #endif /* DUMP_MSGS */
153dee1d999SLukasz Majewski 
154dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
155dee1d999SLukasz Majewski 
156dee1d999SLukasz Majewski /* SCSI device types */
157dee1d999SLukasz Majewski #define TYPE_DISK	0x00
158dee1d999SLukasz Majewski #define TYPE_CDROM	0x05
159dee1d999SLukasz Majewski 
160dee1d999SLukasz Majewski /* USB protocol value = the transport method */
161dee1d999SLukasz Majewski #define USB_PR_CBI	0x00		/* Control/Bulk/Interrupt */
162dee1d999SLukasz Majewski #define USB_PR_CB	0x01		/* Control/Bulk w/o interrupt */
163dee1d999SLukasz Majewski #define USB_PR_BULK	0x50		/* Bulk-only */
164dee1d999SLukasz Majewski 
165dee1d999SLukasz Majewski /* USB subclass value = the protocol encapsulation */
166dee1d999SLukasz Majewski #define USB_SC_RBC	0x01		/* Reduced Block Commands (flash) */
167dee1d999SLukasz Majewski #define USB_SC_8020	0x02		/* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
168dee1d999SLukasz Majewski #define USB_SC_QIC	0x03		/* QIC-157 (tape) */
169dee1d999SLukasz Majewski #define USB_SC_UFI	0x04		/* UFI (floppy) */
170dee1d999SLukasz Majewski #define USB_SC_8070	0x05		/* SFF-8070i (removable) */
171dee1d999SLukasz Majewski #define USB_SC_SCSI	0x06		/* Transparent SCSI */
172dee1d999SLukasz Majewski 
173dee1d999SLukasz Majewski /* Bulk-only data structures */
174dee1d999SLukasz Majewski 
175dee1d999SLukasz Majewski /* Command Block Wrapper */
176dee1d999SLukasz Majewski struct fsg_bulk_cb_wrap {
177dee1d999SLukasz Majewski 	__le32	Signature;		/* Contains 'USBC' */
178dee1d999SLukasz Majewski 	u32	Tag;			/* Unique per command id */
179dee1d999SLukasz Majewski 	__le32	DataTransferLength;	/* Size of the data */
180dee1d999SLukasz Majewski 	u8	Flags;			/* Direction in bit 7 */
181dee1d999SLukasz Majewski 	u8	Lun;			/* LUN (normally 0) */
182dee1d999SLukasz Majewski 	u8	Length;			/* Of the CDB, <= MAX_COMMAND_SIZE */
183dee1d999SLukasz Majewski 	u8	CDB[16];		/* Command Data Block */
184dee1d999SLukasz Majewski };
185dee1d999SLukasz Majewski 
186dee1d999SLukasz Majewski #define USB_BULK_CB_WRAP_LEN	31
187dee1d999SLukasz Majewski #define USB_BULK_CB_SIG		0x43425355	/* Spells out USBC */
188dee1d999SLukasz Majewski #define USB_BULK_IN_FLAG	0x80
189dee1d999SLukasz Majewski 
190dee1d999SLukasz Majewski /* Command Status Wrapper */
191dee1d999SLukasz Majewski struct bulk_cs_wrap {
192dee1d999SLukasz Majewski 	__le32	Signature;		/* Should = 'USBS' */
193dee1d999SLukasz Majewski 	u32	Tag;			/* Same as original command */
194dee1d999SLukasz Majewski 	__le32	Residue;		/* Amount not transferred */
195dee1d999SLukasz Majewski 	u8	Status;			/* See below */
196dee1d999SLukasz Majewski };
197dee1d999SLukasz Majewski 
198dee1d999SLukasz Majewski #define USB_BULK_CS_WRAP_LEN	13
199dee1d999SLukasz Majewski #define USB_BULK_CS_SIG		0x53425355	/* Spells out 'USBS' */
200dee1d999SLukasz Majewski #define USB_STATUS_PASS		0
201dee1d999SLukasz Majewski #define USB_STATUS_FAIL		1
202dee1d999SLukasz Majewski #define USB_STATUS_PHASE_ERROR	2
203dee1d999SLukasz Majewski 
204dee1d999SLukasz Majewski /* Bulk-only class specific requests */
205dee1d999SLukasz Majewski #define USB_BULK_RESET_REQUEST		0xff
206dee1d999SLukasz Majewski #define USB_BULK_GET_MAX_LUN_REQUEST	0xfe
207dee1d999SLukasz Majewski 
208dee1d999SLukasz Majewski /* CBI Interrupt data structure */
209dee1d999SLukasz Majewski struct interrupt_data {
210dee1d999SLukasz Majewski 	u8	bType;
211dee1d999SLukasz Majewski 	u8	bValue;
212dee1d999SLukasz Majewski };
213dee1d999SLukasz Majewski 
214dee1d999SLukasz Majewski #define CBI_INTERRUPT_DATA_LEN		2
215dee1d999SLukasz Majewski 
216dee1d999SLukasz Majewski /* CBI Accept Device-Specific Command request */
217dee1d999SLukasz Majewski #define USB_CBI_ADSC_REQUEST		0x00
218dee1d999SLukasz Majewski 
219dee1d999SLukasz Majewski /* Length of a SCSI Command Data Block */
220dee1d999SLukasz Majewski #define MAX_COMMAND_SIZE	16
221dee1d999SLukasz Majewski 
222dee1d999SLukasz Majewski /* SCSI commands that we recognize */
223dee1d999SLukasz Majewski #define SC_FORMAT_UNIT			0x04
224dee1d999SLukasz Majewski #define SC_INQUIRY			0x12
225dee1d999SLukasz Majewski #define SC_MODE_SELECT_6		0x15
226dee1d999SLukasz Majewski #define SC_MODE_SELECT_10		0x55
227dee1d999SLukasz Majewski #define SC_MODE_SENSE_6			0x1a
228dee1d999SLukasz Majewski #define SC_MODE_SENSE_10		0x5a
229dee1d999SLukasz Majewski #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL	0x1e
230dee1d999SLukasz Majewski #define SC_READ_6			0x08
231dee1d999SLukasz Majewski #define SC_READ_10			0x28
232dee1d999SLukasz Majewski #define SC_READ_12			0xa8
233dee1d999SLukasz Majewski #define SC_READ_CAPACITY		0x25
234dee1d999SLukasz Majewski #define SC_READ_FORMAT_CAPACITIES	0x23
235dee1d999SLukasz Majewski #define SC_READ_HEADER			0x44
236dee1d999SLukasz Majewski #define SC_READ_TOC			0x43
237dee1d999SLukasz Majewski #define SC_RELEASE			0x17
238dee1d999SLukasz Majewski #define SC_REQUEST_SENSE		0x03
239dee1d999SLukasz Majewski #define SC_RESERVE			0x16
240dee1d999SLukasz Majewski #define SC_SEND_DIAGNOSTIC		0x1d
241dee1d999SLukasz Majewski #define SC_START_STOP_UNIT		0x1b
242dee1d999SLukasz Majewski #define SC_SYNCHRONIZE_CACHE		0x35
243dee1d999SLukasz Majewski #define SC_TEST_UNIT_READY		0x00
244dee1d999SLukasz Majewski #define SC_VERIFY			0x2f
245dee1d999SLukasz Majewski #define SC_WRITE_6			0x0a
246dee1d999SLukasz Majewski #define SC_WRITE_10			0x2a
247dee1d999SLukasz Majewski #define SC_WRITE_12			0xaa
248dee1d999SLukasz Majewski 
249dee1d999SLukasz Majewski /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
250dee1d999SLukasz Majewski #define SS_NO_SENSE				0
251dee1d999SLukasz Majewski #define SS_COMMUNICATION_FAILURE		0x040800
252dee1d999SLukasz Majewski #define SS_INVALID_COMMAND			0x052000
253dee1d999SLukasz Majewski #define SS_INVALID_FIELD_IN_CDB			0x052400
254dee1d999SLukasz Majewski #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE	0x052100
255dee1d999SLukasz Majewski #define SS_LOGICAL_UNIT_NOT_SUPPORTED		0x052500
256dee1d999SLukasz Majewski #define SS_MEDIUM_NOT_PRESENT			0x023a00
257dee1d999SLukasz Majewski #define SS_MEDIUM_REMOVAL_PREVENTED		0x055302
258dee1d999SLukasz Majewski #define SS_NOT_READY_TO_READY_TRANSITION	0x062800
259dee1d999SLukasz Majewski #define SS_RESET_OCCURRED			0x062900
260dee1d999SLukasz Majewski #define SS_SAVING_PARAMETERS_NOT_SUPPORTED	0x053900
261dee1d999SLukasz Majewski #define SS_UNRECOVERED_READ_ERROR		0x031100
262dee1d999SLukasz Majewski #define SS_WRITE_ERROR				0x030c02
263dee1d999SLukasz Majewski #define SS_WRITE_PROTECTED			0x072700
264dee1d999SLukasz Majewski 
265dee1d999SLukasz Majewski #define SK(x)		((u8) ((x) >> 16))	/* Sense Key byte, etc. */
266dee1d999SLukasz Majewski #define ASC(x)		((u8) ((x) >> 8))
267dee1d999SLukasz Majewski #define ASCQ(x)		((u8) (x))
268dee1d999SLukasz Majewski 
269dee1d999SLukasz Majewski struct device_attribute { int i; };
270dee1d999SLukasz Majewski #define ETOOSMALL	525
271dee1d999SLukasz Majewski 
272dee1d999SLukasz Majewski #include <usb_mass_storage.h>
273dee1d999SLukasz Majewski 
274dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
275dee1d999SLukasz Majewski 
276dee1d999SLukasz Majewski struct fsg_lun {
277dee1d999SLukasz Majewski 	loff_t		file_length;
278dee1d999SLukasz Majewski 	loff_t		num_sectors;
279dee1d999SLukasz Majewski 
280dee1d999SLukasz Majewski 	unsigned int	initially_ro:1;
281dee1d999SLukasz Majewski 	unsigned int	ro:1;
282dee1d999SLukasz Majewski 	unsigned int	removable:1;
283dee1d999SLukasz Majewski 	unsigned int	cdrom:1;
284dee1d999SLukasz Majewski 	unsigned int	prevent_medium_removal:1;
285dee1d999SLukasz Majewski 	unsigned int	registered:1;
286dee1d999SLukasz Majewski 	unsigned int	info_valid:1;
287dee1d999SLukasz Majewski 	unsigned int	nofua:1;
288dee1d999SLukasz Majewski 
289dee1d999SLukasz Majewski 	u32		sense_data;
290dee1d999SLukasz Majewski 	u32		sense_data_info;
291dee1d999SLukasz Majewski 	u32		unit_attention_data;
292dee1d999SLukasz Majewski 
293dee1d999SLukasz Majewski 	struct device	dev;
294dee1d999SLukasz Majewski };
295dee1d999SLukasz Majewski 
296dee1d999SLukasz Majewski #define fsg_lun_is_open(curlun)	((curlun)->filp != NULL)
297dee1d999SLukasz Majewski #if 0
298dee1d999SLukasz Majewski static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
299dee1d999SLukasz Majewski {
300dee1d999SLukasz Majewski 	return container_of(dev, struct fsg_lun, dev);
301dee1d999SLukasz Majewski }
302dee1d999SLukasz Majewski #endif
303dee1d999SLukasz Majewski 
304dee1d999SLukasz Majewski /* Big enough to hold our biggest descriptor */
305dee1d999SLukasz Majewski #define EP0_BUFSIZE	256
306dee1d999SLukasz Majewski #define DELAYED_STATUS	(EP0_BUFSIZE + 999)	/* An impossibly large value */
307dee1d999SLukasz Majewski 
308dee1d999SLukasz Majewski /* Number of buffers we will use.  2 is enough for double-buffering */
309dee1d999SLukasz Majewski #define FSG_NUM_BUFFERS	2
310dee1d999SLukasz Majewski 
311dee1d999SLukasz Majewski /* Default size of buffer length. */
312dee1d999SLukasz Majewski #define FSG_BUFLEN	((u32)16384)
313dee1d999SLukasz Majewski 
314dee1d999SLukasz Majewski /* Maximal number of LUNs supported in mass storage function */
315dee1d999SLukasz Majewski #define FSG_MAX_LUNS	8
316dee1d999SLukasz Majewski 
317dee1d999SLukasz Majewski enum fsg_buffer_state {
318dee1d999SLukasz Majewski 	BUF_STATE_EMPTY = 0,
319dee1d999SLukasz Majewski 	BUF_STATE_FULL,
320dee1d999SLukasz Majewski 	BUF_STATE_BUSY
321dee1d999SLukasz Majewski };
322dee1d999SLukasz Majewski 
323dee1d999SLukasz Majewski struct fsg_buffhd {
324dee1d999SLukasz Majewski #ifdef FSG_BUFFHD_STATIC_BUFFER
325dee1d999SLukasz Majewski 	char				buf[FSG_BUFLEN];
326dee1d999SLukasz Majewski #else
327dee1d999SLukasz Majewski 	void				*buf;
328dee1d999SLukasz Majewski #endif
329dee1d999SLukasz Majewski 	enum fsg_buffer_state		state;
330dee1d999SLukasz Majewski 	struct fsg_buffhd		*next;
331dee1d999SLukasz Majewski 
332dee1d999SLukasz Majewski 	/*
333dee1d999SLukasz Majewski 	 * The NetChip 2280 is faster, and handles some protocol faults
334dee1d999SLukasz Majewski 	 * better, if we don't submit any short bulk-out read requests.
335dee1d999SLukasz Majewski 	 * So we will record the intended request length here.
336dee1d999SLukasz Majewski 	 */
337dee1d999SLukasz Majewski 	unsigned int			bulk_out_intended_length;
338dee1d999SLukasz Majewski 
339dee1d999SLukasz Majewski 	struct usb_request		*inreq;
340dee1d999SLukasz Majewski 	int				inreq_busy;
341dee1d999SLukasz Majewski 	struct usb_request		*outreq;
342dee1d999SLukasz Majewski 	int				outreq_busy;
343dee1d999SLukasz Majewski };
344dee1d999SLukasz Majewski 
345dee1d999SLukasz Majewski enum fsg_state {
346dee1d999SLukasz Majewski 	/* This one isn't used anywhere */
347dee1d999SLukasz Majewski 	FSG_STATE_COMMAND_PHASE = -10,
348dee1d999SLukasz Majewski 	FSG_STATE_DATA_PHASE,
349dee1d999SLukasz Majewski 	FSG_STATE_STATUS_PHASE,
350dee1d999SLukasz Majewski 
351dee1d999SLukasz Majewski 	FSG_STATE_IDLE = 0,
352dee1d999SLukasz Majewski 	FSG_STATE_ABORT_BULK_OUT,
353dee1d999SLukasz Majewski 	FSG_STATE_RESET,
354dee1d999SLukasz Majewski 	FSG_STATE_INTERFACE_CHANGE,
355dee1d999SLukasz Majewski 	FSG_STATE_CONFIG_CHANGE,
356dee1d999SLukasz Majewski 	FSG_STATE_DISCONNECT,
357dee1d999SLukasz Majewski 	FSG_STATE_EXIT,
358dee1d999SLukasz Majewski 	FSG_STATE_TERMINATED
359dee1d999SLukasz Majewski };
360dee1d999SLukasz Majewski 
361dee1d999SLukasz Majewski enum data_direction {
362dee1d999SLukasz Majewski 	DATA_DIR_UNKNOWN = 0,
363dee1d999SLukasz Majewski 	DATA_DIR_FROM_HOST,
364dee1d999SLukasz Majewski 	DATA_DIR_TO_HOST,
365dee1d999SLukasz Majewski 	DATA_DIR_NONE
366dee1d999SLukasz Majewski };
367dee1d999SLukasz Majewski 
368dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
369dee1d999SLukasz Majewski 
370dee1d999SLukasz Majewski static inline u32 get_unaligned_be24(u8 *buf)
371dee1d999SLukasz Majewski {
372dee1d999SLukasz Majewski 	return 0xffffff & (u32) get_unaligned_be32(buf - 1);
373dee1d999SLukasz Majewski }
374dee1d999SLukasz Majewski 
375dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
376dee1d999SLukasz Majewski 
377dee1d999SLukasz Majewski enum {
378dee1d999SLukasz Majewski #ifndef FSG_NO_DEVICE_STRINGS
379dee1d999SLukasz Majewski 	FSG_STRING_MANUFACTURER	= 1,
380dee1d999SLukasz Majewski 	FSG_STRING_PRODUCT,
381dee1d999SLukasz Majewski 	FSG_STRING_SERIAL,
382dee1d999SLukasz Majewski 	FSG_STRING_CONFIG,
383dee1d999SLukasz Majewski #endif
384dee1d999SLukasz Majewski 	FSG_STRING_INTERFACE
385dee1d999SLukasz Majewski };
386dee1d999SLukasz Majewski 
387dee1d999SLukasz Majewski #ifndef FSG_NO_OTG
388dee1d999SLukasz Majewski static struct usb_otg_descriptor
389dee1d999SLukasz Majewski fsg_otg_desc = {
390dee1d999SLukasz Majewski 	.bLength =		sizeof fsg_otg_desc,
391dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_OTG,
392dee1d999SLukasz Majewski 
393dee1d999SLukasz Majewski 	.bmAttributes =		USB_OTG_SRP,
394dee1d999SLukasz Majewski };
395dee1d999SLukasz Majewski #endif
396dee1d999SLukasz Majewski 
397dee1d999SLukasz Majewski /* There is only one interface. */
398dee1d999SLukasz Majewski 
399dee1d999SLukasz Majewski static struct usb_interface_descriptor
400dee1d999SLukasz Majewski fsg_intf_desc = {
401dee1d999SLukasz Majewski 	.bLength =		sizeof fsg_intf_desc,
402dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_INTERFACE,
403dee1d999SLukasz Majewski 
404dee1d999SLukasz Majewski 	.bNumEndpoints =	2,		/* Adjusted during fsg_bind() */
405dee1d999SLukasz Majewski 	.bInterfaceClass =	USB_CLASS_MASS_STORAGE,
406dee1d999SLukasz Majewski 	.bInterfaceSubClass =	USB_SC_SCSI,	/* Adjusted during fsg_bind() */
407dee1d999SLukasz Majewski 	.bInterfaceProtocol =	USB_PR_BULK,	/* Adjusted during fsg_bind() */
408dee1d999SLukasz Majewski 	.iInterface =		FSG_STRING_INTERFACE,
409dee1d999SLukasz Majewski };
410dee1d999SLukasz Majewski 
411dee1d999SLukasz Majewski /*
412dee1d999SLukasz Majewski  * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
413dee1d999SLukasz Majewski  * interrupt-in.
414dee1d999SLukasz Majewski  */
415dee1d999SLukasz Majewski 
416dee1d999SLukasz Majewski static struct usb_endpoint_descriptor
417dee1d999SLukasz Majewski fsg_fs_bulk_in_desc = {
418dee1d999SLukasz Majewski 	.bLength =		USB_DT_ENDPOINT_SIZE,
419dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_ENDPOINT,
420dee1d999SLukasz Majewski 
421dee1d999SLukasz Majewski 	.bEndpointAddress =	USB_DIR_IN,
422dee1d999SLukasz Majewski 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
423dee1d999SLukasz Majewski 	/* wMaxPacketSize set by autoconfiguration */
424dee1d999SLukasz Majewski };
425dee1d999SLukasz Majewski 
426dee1d999SLukasz Majewski static struct usb_endpoint_descriptor
427dee1d999SLukasz Majewski fsg_fs_bulk_out_desc = {
428dee1d999SLukasz Majewski 	.bLength =		USB_DT_ENDPOINT_SIZE,
429dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_ENDPOINT,
430dee1d999SLukasz Majewski 
431dee1d999SLukasz Majewski 	.bEndpointAddress =	USB_DIR_OUT,
432dee1d999SLukasz Majewski 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
433dee1d999SLukasz Majewski 	/* wMaxPacketSize set by autoconfiguration */
434dee1d999SLukasz Majewski };
435dee1d999SLukasz Majewski 
436dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP
437dee1d999SLukasz Majewski 
438dee1d999SLukasz Majewski static struct usb_endpoint_descriptor
439dee1d999SLukasz Majewski fsg_fs_intr_in_desc = {
440dee1d999SLukasz Majewski 	.bLength =		USB_DT_ENDPOINT_SIZE,
441dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_ENDPOINT,
442dee1d999SLukasz Majewski 
443dee1d999SLukasz Majewski 	.bEndpointAddress =	USB_DIR_IN,
444dee1d999SLukasz Majewski 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
445dee1d999SLukasz Majewski 	.wMaxPacketSize =	cpu_to_le16(2),
446dee1d999SLukasz Majewski 	.bInterval =		32,	/* frames -> 32 ms */
447dee1d999SLukasz Majewski };
448dee1d999SLukasz Majewski 
449dee1d999SLukasz Majewski #ifndef FSG_NO_OTG
450dee1d999SLukasz Majewski #  define FSG_FS_FUNCTION_PRE_EP_ENTRIES	2
451dee1d999SLukasz Majewski #else
452dee1d999SLukasz Majewski #  define FSG_FS_FUNCTION_PRE_EP_ENTRIES	1
453dee1d999SLukasz Majewski #endif
454dee1d999SLukasz Majewski 
455dee1d999SLukasz Majewski #endif
456dee1d999SLukasz Majewski 
457dee1d999SLukasz Majewski static struct usb_descriptor_header *fsg_fs_function[] = {
458dee1d999SLukasz Majewski #ifndef FSG_NO_OTG
459dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_otg_desc,
460dee1d999SLukasz Majewski #endif
461dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_intf_desc,
462dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
463dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
464dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP
465dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
466dee1d999SLukasz Majewski #endif
467dee1d999SLukasz Majewski 	NULL,
468dee1d999SLukasz Majewski };
469dee1d999SLukasz Majewski 
470dee1d999SLukasz Majewski /*
471dee1d999SLukasz Majewski  * USB 2.0 devices need to expose both high speed and full speed
472dee1d999SLukasz Majewski  * descriptors, unless they only run at full speed.
473dee1d999SLukasz Majewski  *
474dee1d999SLukasz Majewski  * That means alternate endpoint descriptors (bigger packets)
475dee1d999SLukasz Majewski  * and a "device qualifier" ... plus more construction options
476dee1d999SLukasz Majewski  * for the configuration descriptor.
477dee1d999SLukasz Majewski  */
478dee1d999SLukasz Majewski static struct usb_endpoint_descriptor
479dee1d999SLukasz Majewski fsg_hs_bulk_in_desc = {
480dee1d999SLukasz Majewski 	.bLength =		USB_DT_ENDPOINT_SIZE,
481dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_ENDPOINT,
482dee1d999SLukasz Majewski 
483dee1d999SLukasz Majewski 	/* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
484dee1d999SLukasz Majewski 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
485dee1d999SLukasz Majewski 	.wMaxPacketSize =	cpu_to_le16(512),
486dee1d999SLukasz Majewski };
487dee1d999SLukasz Majewski 
488dee1d999SLukasz Majewski static struct usb_endpoint_descriptor
489dee1d999SLukasz Majewski fsg_hs_bulk_out_desc = {
490dee1d999SLukasz Majewski 	.bLength =		USB_DT_ENDPOINT_SIZE,
491dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_ENDPOINT,
492dee1d999SLukasz Majewski 
493dee1d999SLukasz Majewski 	/* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
494dee1d999SLukasz Majewski 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
495dee1d999SLukasz Majewski 	.wMaxPacketSize =	cpu_to_le16(512),
496dee1d999SLukasz Majewski 	.bInterval =		1,	/* NAK every 1 uframe */
497dee1d999SLukasz Majewski };
498dee1d999SLukasz Majewski 
499dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP
500dee1d999SLukasz Majewski 
501dee1d999SLukasz Majewski static struct usb_endpoint_descriptor
502dee1d999SLukasz Majewski fsg_hs_intr_in_desc = {
503dee1d999SLukasz Majewski 	.bLength =		USB_DT_ENDPOINT_SIZE,
504dee1d999SLukasz Majewski 	.bDescriptorType =	USB_DT_ENDPOINT,
505dee1d999SLukasz Majewski 
506dee1d999SLukasz Majewski 	/* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
507dee1d999SLukasz Majewski 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
508dee1d999SLukasz Majewski 	.wMaxPacketSize =	cpu_to_le16(2),
509dee1d999SLukasz Majewski 	.bInterval =		9,	/* 2**(9-1) = 256 uframes -> 32 ms */
510dee1d999SLukasz Majewski };
511dee1d999SLukasz Majewski 
512dee1d999SLukasz Majewski #ifndef FSG_NO_OTG
513dee1d999SLukasz Majewski #  define FSG_HS_FUNCTION_PRE_EP_ENTRIES	2
514dee1d999SLukasz Majewski #else
515dee1d999SLukasz Majewski #  define FSG_HS_FUNCTION_PRE_EP_ENTRIES	1
516dee1d999SLukasz Majewski #endif
517dee1d999SLukasz Majewski 
518dee1d999SLukasz Majewski #endif
519dee1d999SLukasz Majewski 
520dee1d999SLukasz Majewski static struct usb_descriptor_header *fsg_hs_function[] = {
521dee1d999SLukasz Majewski #ifndef FSG_NO_OTG
522dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_otg_desc,
523dee1d999SLukasz Majewski #endif
524dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_intf_desc,
525dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
526dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
527dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP
528dee1d999SLukasz Majewski 	(struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
529dee1d999SLukasz Majewski #endif
530dee1d999SLukasz Majewski 	NULL,
531dee1d999SLukasz Majewski };
532dee1d999SLukasz Majewski 
533dee1d999SLukasz Majewski /* Maxpacket and other transfer characteristics vary by speed. */
534dee1d999SLukasz Majewski static struct usb_endpoint_descriptor *
535dee1d999SLukasz Majewski fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
536dee1d999SLukasz Majewski 		struct usb_endpoint_descriptor *hs)
537dee1d999SLukasz Majewski {
538dee1d999SLukasz Majewski 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
539dee1d999SLukasz Majewski 		return hs;
540dee1d999SLukasz Majewski 	return fs;
541dee1d999SLukasz Majewski }
542dee1d999SLukasz Majewski 
543dee1d999SLukasz Majewski /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
544dee1d999SLukasz Majewski static struct usb_string		fsg_strings[] = {
545dee1d999SLukasz Majewski #ifndef FSG_NO_DEVICE_STRINGS
546dee1d999SLukasz Majewski 	{FSG_STRING_MANUFACTURER,	fsg_string_manufacturer},
547dee1d999SLukasz Majewski 	{FSG_STRING_PRODUCT,		fsg_string_product},
548dee1d999SLukasz Majewski 	{FSG_STRING_SERIAL,		fsg_string_serial},
549dee1d999SLukasz Majewski 	{FSG_STRING_CONFIG,		fsg_string_config},
550dee1d999SLukasz Majewski #endif
551dee1d999SLukasz Majewski 	{FSG_STRING_INTERFACE,		fsg_string_interface},
552dee1d999SLukasz Majewski 	{}
553dee1d999SLukasz Majewski };
554dee1d999SLukasz Majewski 
555dee1d999SLukasz Majewski static struct usb_gadget_strings	fsg_stringtab = {
556dee1d999SLukasz Majewski 	.language	= 0x0409,		/* en-us */
557dee1d999SLukasz Majewski 	.strings	= fsg_strings,
558dee1d999SLukasz Majewski };
559dee1d999SLukasz Majewski 
560dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
561dee1d999SLukasz Majewski 
562dee1d999SLukasz Majewski /*
563dee1d999SLukasz Majewski  * If the next two routines are called while the gadget is registered,
564dee1d999SLukasz Majewski  * the caller must own fsg->filesem for writing.
565dee1d999SLukasz Majewski  */
566dee1d999SLukasz Majewski 
567*02585eb3SStephen Warren static int fsg_lun_open(struct fsg_lun *curlun, unsigned int num_sectors,
568*02585eb3SStephen Warren 			const char *filename)
569dee1d999SLukasz Majewski {
570dee1d999SLukasz Majewski 	int				ro;
571dee1d999SLukasz Majewski 
572dee1d999SLukasz Majewski 	/* R/W if we can, R/O if we must */
573dee1d999SLukasz Majewski 	ro = curlun->initially_ro;
574dee1d999SLukasz Majewski 
575dee1d999SLukasz Majewski 	curlun->ro = ro;
576*02585eb3SStephen Warren 	curlun->file_length = num_sectors << 9;
577*02585eb3SStephen Warren 	curlun->num_sectors = num_sectors;
578dee1d999SLukasz Majewski 	debug("open backing file: %s\n", filename);
579dee1d999SLukasz Majewski 
5800697f206SPrzemyslaw Marczak 	return 0;
581dee1d999SLukasz Majewski }
582dee1d999SLukasz Majewski 
583dee1d999SLukasz Majewski static void fsg_lun_close(struct fsg_lun *curlun)
584dee1d999SLukasz Majewski {
585dee1d999SLukasz Majewski }
586dee1d999SLukasz Majewski 
587dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
588dee1d999SLukasz Majewski 
589dee1d999SLukasz Majewski /*
590dee1d999SLukasz Majewski  * Sync the file data, don't bother with the metadata.
591dee1d999SLukasz Majewski  * This code was copied from fs/buffer.c:sys_fdatasync().
592dee1d999SLukasz Majewski  */
593dee1d999SLukasz Majewski static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
594dee1d999SLukasz Majewski {
595dee1d999SLukasz Majewski 	return 0;
596dee1d999SLukasz Majewski }
597dee1d999SLukasz Majewski 
598dee1d999SLukasz Majewski static void store_cdrom_address(u8 *dest, int msf, u32 addr)
599dee1d999SLukasz Majewski {
600dee1d999SLukasz Majewski 	if (msf) {
601dee1d999SLukasz Majewski 		/* Convert to Minutes-Seconds-Frames */
602dee1d999SLukasz Majewski 		addr >>= 2;		/* Convert to 2048-byte frames */
603dee1d999SLukasz Majewski 		addr += 2*75;		/* Lead-in occupies 2 seconds */
604dee1d999SLukasz Majewski 		dest[3] = addr % 75;	/* Frames */
605dee1d999SLukasz Majewski 		addr /= 75;
606dee1d999SLukasz Majewski 		dest[2] = addr % 60;	/* Seconds */
607dee1d999SLukasz Majewski 		addr /= 60;
608dee1d999SLukasz Majewski 		dest[1] = addr;		/* Minutes */
609dee1d999SLukasz Majewski 		dest[0] = 0;		/* Reserved */
610dee1d999SLukasz Majewski 	} else {
611dee1d999SLukasz Majewski 		/* Absolute sector */
612dee1d999SLukasz Majewski 		put_unaligned_be32(addr, dest);
613dee1d999SLukasz Majewski 	}
614dee1d999SLukasz Majewski }
615dee1d999SLukasz Majewski 
616dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/
617