blob: d7856c599d5a075d5c99562da1e2323b7882cbce [file] [log] [blame]
Michal Nazarewiczb6058d02009-10-28 16:57:14 +01001/*
2 * storage_common.c -- Common definitions for mass storage functionality
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyeight (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010021 */
22
23
Michal Nazarewiczd6181702009-10-28 16:57:15 +010024/*
25 * This file requires the following identifiers used in USB strings to
26 * be defined (each of type pointer to char):
27 * - fsg_string_manufacturer -- name of the manufacturer
28 * - fsg_string_product -- name of the product
Michal Nazarewiczd6181702009-10-28 16:57:15 +010029 * - fsg_string_config -- name of the configuration
30 * - fsg_string_interface -- name of the interface
31 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
32 * macro is defined prior to including this file.
33 */
34
Michal Nazarewicz93bcf122009-10-28 16:57:19 +010035/*
36 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
37 * fsg_hs_intr_in_desc objects as well as
38 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
39 * macros are not defined.
40 *
41 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
42 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
43 * defined (as well as corresponding entries in string tables are
44 * missing) and FSG_STRING_INTERFACE has value of zero.
45 *
46 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
47 */
48
Michal Nazarewicz606206c2009-10-28 16:57:21 +010049/*
50 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
51 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
52 * characters rather then a pointer to void.
53 */
54
Michal Nazarewiczd6181702009-10-28 16:57:15 +010055
56#include <asm/unaligned.h>
57
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010058
Michal Nazarewiczd0893262010-07-05 16:38:04 +020059/*
60 * Thanks to NetChip Technologies for donating this product ID.
Michal Nazarewicz93f93742009-10-28 16:57:17 +010061 *
62 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
Michal Nazarewiczd0893262010-07-05 16:38:04 +020063 * Instead: allocate your own, using normal USB-IF procedures.
64 */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010065#define FSG_VENDOR_ID 0x0525 /* NetChip */
66#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
Michal Nazarewicz93f93742009-10-28 16:57:17 +010067
68
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010069/*-------------------------------------------------------------------------*/
70
71
72#ifndef DEBUG
73#undef VERBOSE_DEBUG
74#undef DUMP_MSGS
75#endif /* !DEBUG */
76
77#ifdef VERBOSE_DEBUG
78#define VLDBG LDBG
79#else
80#define VLDBG(lun, fmt, args...) do { } while (0)
81#endif /* VERBOSE_DEBUG */
82
83#define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
84#define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
85#define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
86#define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
87
Michal Nazarewiczd0893262010-07-05 16:38:04 +020088/*
89 * Keep those macros in sync with those in
90 * include/linux/usb/composite.h or else GCC will complain. If they
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010091 * are identical (the same names of arguments, white spaces in the
92 * same places) GCC will allow redefinition otherwise (even if some
Michal Nazarewiczd0893262010-07-05 16:38:04 +020093 * white space is removed or added) warning will be issued.
94 *
95 * Those macros are needed here because File Storage Gadget does not
96 * include the composite.h header. For composite gadgets those macros
97 * are redundant since composite.h is included any way.
98 *
99 * One could check whether those macros are already defined (which
100 * would indicate composite.h had been included) or not (which would
101 * indicate we were in FSG) but this is not done because a warning is
102 * desired if definitions here differ from the ones in composite.h.
103 *
104 * We want the definitions to match and be the same in File Storage
105 * Gadget as well as Mass Storage Function (and so composite gadgets
106 * using MSF). If someone changes them in composite.h it will produce
107 * a warning in this file when building MSF.
108 */
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100109#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
110#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
111#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
112#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
113#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100114
115
116
117#ifdef DUMP_MSGS
118
119# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100120 /* const u8 * */ buf, /* unsigned */ length) do { \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100121 if (length < 512) { \
122 DBG(fsg, "%s, length %u:\n", label, length); \
123 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
124 16, 1, buf, length, 0); \
125 } \
126} while (0)
127
128# define dump_cdb(fsg) do { } while (0)
129
130#else
131
132# define dump_msg(fsg, /* const char * */ label, \
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100133 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100134
135# ifdef VERBOSE_DEBUG
136
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100137# define dump_cdb(fsg) \
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100138 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
139 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
140
141# else
142
143# define dump_cdb(fsg) do { } while (0)
144
145# endif /* VERBOSE_DEBUG */
146
147#endif /* DUMP_MSGS */
148
149
150
151
152
153/*-------------------------------------------------------------------------*/
154
155/* SCSI device types */
156#define TYPE_DISK 0x00
157#define TYPE_CDROM 0x05
158
159/* USB protocol value = the transport method */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100160#define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */
161#define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
162#define USB_PR_BULK 0x50 /* Bulk-only */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100163
164/* USB subclass value = the protocol encapsulation */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100165#define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */
166#define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */
167#define USB_SC_QIC 0x03 /* QIC-157 (tape) */
168#define USB_SC_UFI 0x04 /* UFI (floppy) */
169#define USB_SC_8070 0x05 /* SFF-8070i (removable) */
170#define USB_SC_SCSI 0x06 /* Transparent SCSI */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100171
172/* Bulk-only data structures */
173
174/* Command Block Wrapper */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100175struct fsg_bulk_cb_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100176 __le32 Signature; /* Contains 'USBC' */
177 u32 Tag; /* Unique per command id */
178 __le32 DataTransferLength; /* Size of the data */
179 u8 Flags; /* Direction in bit 7 */
180 u8 Lun; /* LUN (normally 0) */
181 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
182 u8 CDB[16]; /* Command Data Block */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100183};
184
185#define USB_BULK_CB_WRAP_LEN 31
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100186#define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100187#define USB_BULK_IN_FLAG 0x80
188
189/* Command Status Wrapper */
190struct bulk_cs_wrap {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100191 __le32 Signature; /* Should = 'USBS' */
192 u32 Tag; /* Same as original command */
193 __le32 Residue; /* Amount not transferred */
194 u8 Status; /* See below */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100195};
196
197#define USB_BULK_CS_WRAP_LEN 13
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100198#define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100199#define USB_STATUS_PASS 0
200#define USB_STATUS_FAIL 1
201#define USB_STATUS_PHASE_ERROR 2
202
203/* Bulk-only class specific requests */
204#define USB_BULK_RESET_REQUEST 0xff
205#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
206
207
208/* CBI Interrupt data structure */
209struct interrupt_data {
210 u8 bType;
211 u8 bValue;
212};
213
214#define CBI_INTERRUPT_DATA_LEN 2
215
216/* CBI Accept Device-Specific Command request */
217#define USB_CBI_ADSC_REQUEST 0x00
218
219
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100220/* Length of a SCSI Command Data Block */
221#define MAX_COMMAND_SIZE 16
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100222
223/* SCSI commands that we recognize */
224#define SC_FORMAT_UNIT 0x04
225#define SC_INQUIRY 0x12
226#define SC_MODE_SELECT_6 0x15
227#define SC_MODE_SELECT_10 0x55
228#define SC_MODE_SENSE_6 0x1a
229#define SC_MODE_SENSE_10 0x5a
230#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
231#define SC_READ_6 0x08
232#define SC_READ_10 0x28
233#define SC_READ_12 0xa8
234#define SC_READ_CAPACITY 0x25
235#define SC_READ_FORMAT_CAPACITIES 0x23
236#define SC_READ_HEADER 0x44
237#define SC_READ_TOC 0x43
238#define SC_RELEASE 0x17
239#define SC_REQUEST_SENSE 0x03
240#define SC_RESERVE 0x16
241#define SC_SEND_DIAGNOSTIC 0x1d
242#define SC_START_STOP_UNIT 0x1b
243#define SC_SYNCHRONIZE_CACHE 0x35
244#define SC_TEST_UNIT_READY 0x00
245#define SC_VERIFY 0x2f
246#define SC_WRITE_6 0x0a
247#define SC_WRITE_10 0x2a
248#define SC_WRITE_12 0xaa
249
250/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
251#define SS_NO_SENSE 0
252#define SS_COMMUNICATION_FAILURE 0x040800
253#define SS_INVALID_COMMAND 0x052000
254#define SS_INVALID_FIELD_IN_CDB 0x052400
255#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
256#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
257#define SS_MEDIUM_NOT_PRESENT 0x023a00
258#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
259#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
260#define SS_RESET_OCCURRED 0x062900
261#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
262#define SS_UNRECOVERED_READ_ERROR 0x031100
263#define SS_WRITE_ERROR 0x030c02
264#define SS_WRITE_PROTECTED 0x072700
265
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100266#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100267#define ASC(x) ((u8) ((x) >> 8))
268#define ASCQ(x) ((u8) (x))
269
270
271/*-------------------------------------------------------------------------*/
272
273
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100274struct fsg_lun {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100275 struct file *filp;
276 loff_t file_length;
277 loff_t num_sectors;
278
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100279 unsigned int initially_ro:1;
280 unsigned int ro:1;
281 unsigned int removable:1;
282 unsigned int cdrom:1;
283 unsigned int prevent_medium_removal:1;
284 unsigned int registered:1;
285 unsigned int info_valid:1;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300286 unsigned int nofua:1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100287
288 u32 sense_data;
289 u32 sense_data_info;
290 u32 unit_attention_data;
291
292 struct device dev;
293};
294
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100295#define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100296
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100297static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100298{
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100299 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100300}
301
302
303/* Big enough to hold our biggest descriptor */
304#define EP0_BUFSIZE 256
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100305#define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100306
307/* Number of buffers we will use. 2 is enough for double-buffering */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100308#define FSG_NUM_BUFFERS 2
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100309
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100310/* Default size of buffer length. */
311#define FSG_BUFLEN ((u32)16384)
312
313/* Maximal number of LUNs supported in mass storage function */
314#define FSG_MAX_LUNS 8
315
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100316enum fsg_buffer_state {
317 BUF_STATE_EMPTY = 0,
318 BUF_STATE_FULL,
319 BUF_STATE_BUSY
320};
321
322struct fsg_buffhd {
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100323#ifdef FSG_BUFFHD_STATIC_BUFFER
324 char buf[FSG_BUFLEN];
325#else
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100326 void *buf;
Michal Nazarewicz606206c2009-10-28 16:57:21 +0100327#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100328 enum fsg_buffer_state state;
329 struct fsg_buffhd *next;
330
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200331 /*
332 * The NetChip 2280 is faster, and handles some protocol faults
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100333 * better, if we don't submit any short bulk-out read requests.
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200334 * So we will record the intended request length here.
335 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100336 unsigned int bulk_out_intended_length;
337
338 struct usb_request *inreq;
339 int inreq_busy;
340 struct usb_request *outreq;
341 int outreq_busy;
342};
343
344enum fsg_state {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100345 /* This one isn't used anywhere */
346 FSG_STATE_COMMAND_PHASE = -10,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100347 FSG_STATE_DATA_PHASE,
348 FSG_STATE_STATUS_PHASE,
349
350 FSG_STATE_IDLE = 0,
351 FSG_STATE_ABORT_BULK_OUT,
352 FSG_STATE_RESET,
353 FSG_STATE_INTERFACE_CHANGE,
354 FSG_STATE_CONFIG_CHANGE,
355 FSG_STATE_DISCONNECT,
356 FSG_STATE_EXIT,
357 FSG_STATE_TERMINATED
358};
359
360enum data_direction {
361 DATA_DIR_UNKNOWN = 0,
362 DATA_DIR_FROM_HOST,
363 DATA_DIR_TO_HOST,
364 DATA_DIR_NONE
365};
366
367
368/*-------------------------------------------------------------------------*/
369
370
371static inline u32 get_unaligned_be24(u8 *buf)
372{
373 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
374}
375
376
377/*-------------------------------------------------------------------------*/
378
379
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100380enum {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100381#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100382 FSG_STRING_MANUFACTURER = 1,
383 FSG_STRING_PRODUCT,
384 FSG_STRING_SERIAL,
385 FSG_STRING_CONFIG,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100386#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100387 FSG_STRING_INTERFACE
388};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100389
390
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100391#ifndef FSG_NO_OTG
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100392static struct usb_otg_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100393fsg_otg_desc = {
394 .bLength = sizeof fsg_otg_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100395 .bDescriptorType = USB_DT_OTG,
396
397 .bmAttributes = USB_OTG_SRP,
398};
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100399#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100400
401/* There is only one interface. */
402
403static struct usb_interface_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100404fsg_intf_desc = {
405 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100406 .bDescriptorType = USB_DT_INTERFACE,
407
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100408 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100409 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100410 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
411 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100412 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100413};
414
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200415/*
416 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
417 * interrupt-in.
418 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100419
420static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100421fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100422 .bLength = USB_DT_ENDPOINT_SIZE,
423 .bDescriptorType = USB_DT_ENDPOINT,
424
425 .bEndpointAddress = USB_DIR_IN,
426 .bmAttributes = USB_ENDPOINT_XFER_BULK,
427 /* wMaxPacketSize set by autoconfiguration */
428};
429
430static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100431fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100432 .bLength = USB_DT_ENDPOINT_SIZE,
433 .bDescriptorType = USB_DT_ENDPOINT,
434
435 .bEndpointAddress = USB_DIR_OUT,
436 .bmAttributes = USB_ENDPOINT_XFER_BULK,
437 /* wMaxPacketSize set by autoconfiguration */
438};
439
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100440#ifndef FSG_NO_INTR_EP
441
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100442static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100443fsg_fs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100444 .bLength = USB_DT_ENDPOINT_SIZE,
445 .bDescriptorType = USB_DT_ENDPOINT,
446
447 .bEndpointAddress = USB_DIR_IN,
448 .bmAttributes = USB_ENDPOINT_XFER_INT,
449 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100450 .bInterval = 32, /* frames -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100451};
452
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100453#ifndef FSG_NO_OTG
454# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
455#else
456# define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
457#endif
458
459#endif
460
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100461static struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100462#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100463 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100464#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100465 (struct usb_descriptor_header *) &fsg_intf_desc,
466 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
467 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100468#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100469 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100470#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100471 NULL,
472};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100473
474
475/*
476 * USB 2.0 devices need to expose both high speed and full speed
477 * descriptors, unless they only run at full speed.
478 *
479 * That means alternate endpoint descriptors (bigger packets)
480 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200481 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100482 */
483static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100484fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100485 .bLength = USB_DT_ENDPOINT_SIZE,
486 .bDescriptorType = USB_DT_ENDPOINT,
487
488 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
489 .bmAttributes = USB_ENDPOINT_XFER_BULK,
490 .wMaxPacketSize = cpu_to_le16(512),
491};
492
493static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100494fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100495 .bLength = USB_DT_ENDPOINT_SIZE,
496 .bDescriptorType = USB_DT_ENDPOINT,
497
498 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
499 .bmAttributes = USB_ENDPOINT_XFER_BULK,
500 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100501 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100502};
503
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100504#ifndef FSG_NO_INTR_EP
505
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100506static struct usb_endpoint_descriptor
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100507fsg_hs_intr_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100508 .bLength = USB_DT_ENDPOINT_SIZE,
509 .bDescriptorType = USB_DT_ENDPOINT,
510
511 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
512 .bmAttributes = USB_ENDPOINT_XFER_INT,
513 .wMaxPacketSize = cpu_to_le16(2),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100514 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100515};
516
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100517#ifndef FSG_NO_OTG
518# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
519#else
520# define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
521#endif
522
523#endif
524
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100525static struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100526#ifndef FSG_NO_OTG
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100527 (struct usb_descriptor_header *) &fsg_otg_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100528#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100529 (struct usb_descriptor_header *) &fsg_intf_desc,
530 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
531 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100532#ifndef FSG_NO_INTR_EP
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100533 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100534#endif
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100535 NULL,
536};
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100537
538/* Maxpacket and other transfer characteristics vary by speed. */
539static struct usb_endpoint_descriptor *
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100540fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100541 struct usb_endpoint_descriptor *hs)
542{
543 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
544 return hs;
545 return fs;
546}
547
548
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100549/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100550static struct usb_string fsg_strings[] = {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100551#ifndef FSG_NO_DEVICE_STRINGS
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100552 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
553 {FSG_STRING_PRODUCT, fsg_string_product},
Alan Sternd8087422010-09-03 11:15:41 -0400554 {FSG_STRING_SERIAL, ""},
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100555 {FSG_STRING_CONFIG, fsg_string_config},
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100556#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100557 {FSG_STRING_INTERFACE, fsg_string_interface},
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100558 {}
559};
560
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100561static struct usb_gadget_strings fsg_stringtab = {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100562 .language = 0x0409, /* en-us */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100563 .strings = fsg_strings,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100564};
565
566
567 /*-------------------------------------------------------------------------*/
568
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200569/*
570 * If the next two routines are called while the gadget is registered,
571 * the caller must own fsg->filesem for writing.
572 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100573
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100574static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100575{
576 int ro;
577 struct file *filp = NULL;
578 int rc = -EINVAL;
579 struct inode *inode = NULL;
580 loff_t size;
581 loff_t num_sectors;
582 loff_t min_sectors;
583
584 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100585 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100586 if (!ro) {
587 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
588 if (-EROFS == PTR_ERR(filp))
589 ro = 1;
590 }
591 if (ro)
592 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
593 if (IS_ERR(filp)) {
594 LINFO(curlun, "unable to open backing file: %s\n", filename);
595 return PTR_ERR(filp);
596 }
597
598 if (!(filp->f_mode & FMODE_WRITE))
599 ro = 1;
600
601 if (filp->f_path.dentry)
602 inode = filp->f_path.dentry->d_inode;
603 if (inode && S_ISBLK(inode->i_mode)) {
604 if (bdev_read_only(inode->i_bdev))
605 ro = 1;
606 } else if (!inode || !S_ISREG(inode->i_mode)) {
607 LINFO(curlun, "invalid file type: %s\n", filename);
608 goto out;
609 }
610
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200611 /*
612 * If we can't read the file, it's no good.
613 * If we can't write the file, use it read-only.
614 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100615 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
616 LINFO(curlun, "file not readable: %s\n", filename);
617 goto out;
618 }
619 if (!(filp->f_op->write || filp->f_op->aio_write))
620 ro = 1;
621
622 size = i_size_read(inode->i_mapping->host);
623 if (size < 0) {
624 LINFO(curlun, "unable to find file size: %s\n", filename);
625 rc = (int) size;
626 goto out;
627 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100628 num_sectors = size >> 9; /* File size in 512-byte blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100629 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100630 if (curlun->cdrom) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100631 num_sectors &= ~3; /* Reduce to a multiple of 2048 */
632 min_sectors = 300*4; /* Smallest track is 300 frames */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100633 if (num_sectors >= 256*60*75*4) {
634 num_sectors = (256*60*75 - 1) * 4;
635 LINFO(curlun, "file too big: %s\n", filename);
636 LINFO(curlun, "using only first %d blocks\n",
637 (int) num_sectors);
638 }
639 }
640 if (num_sectors < min_sectors) {
641 LINFO(curlun, "file too small: %s\n", filename);
642 rc = -ETOOSMALL;
643 goto out;
644 }
645
646 get_file(filp);
647 curlun->ro = ro;
648 curlun->filp = filp;
649 curlun->file_length = size;
650 curlun->num_sectors = num_sectors;
651 LDBG(curlun, "open backing file: %s\n", filename);
652 rc = 0;
653
654out:
655 filp_close(filp, current->files);
656 return rc;
657}
658
659
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100660static void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100661{
662 if (curlun->filp) {
663 LDBG(curlun, "close backing file\n");
664 fput(curlun->filp);
665 curlun->filp = NULL;
666 }
667}
668
669
670/*-------------------------------------------------------------------------*/
671
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200672/*
673 * Sync the file data, don't bother with the metadata.
674 * This code was copied from fs/buffer.c:sys_fdatasync().
675 */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100676static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100677{
678 struct file *filp = curlun->filp;
679
680 if (curlun->ro || !filp)
681 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100682 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100683}
684
685static void store_cdrom_address(u8 *dest, int msf, u32 addr)
686{
687 if (msf) {
688 /* Convert to Minutes-Seconds-Frames */
689 addr >>= 2; /* Convert to 2048-byte frames */
690 addr += 2*75; /* Lead-in occupies 2 seconds */
691 dest[3] = addr % 75; /* Frames */
692 addr /= 75;
693 dest[2] = addr % 60; /* Seconds */
694 addr /= 60;
695 dest[1] = addr; /* Minutes */
696 dest[0] = 0; /* Reserved */
697 } else {
698 /* Absolute sector */
699 put_unaligned_be32(addr, dest);
700 }
701}
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100702
703
704/*-------------------------------------------------------------------------*/
705
706
707static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
708 char *buf)
709{
710 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
711
712 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
713 ? curlun->ro
714 : curlun->initially_ro);
715}
716
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300717static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
718 char *buf)
719{
720 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
721
722 return sprintf(buf, "%u\n", curlun->nofua);
723}
724
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100725static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
726 char *buf)
727{
728 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
729 struct rw_semaphore *filesem = dev_get_drvdata(dev);
730 char *p;
731 ssize_t rc;
732
733 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100734 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100735 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
736 if (IS_ERR(p))
737 rc = PTR_ERR(p);
738 else {
739 rc = strlen(p);
740 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100741 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100742 buf[++rc] = 0;
743 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100744 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100745 *buf = 0;
746 rc = 0;
747 }
748 up_read(filesem);
749 return rc;
750}
751
752
753static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
754 const char *buf, size_t count)
755{
756 ssize_t rc = count;
757 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
758 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Andy Shevchenko8156d152010-07-22 11:58:47 +0300759 unsigned long ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100760
Andy Shevchenko8156d152010-07-22 11:58:47 +0300761 if (strict_strtoul(buf, 2, &ro))
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100762 return -EINVAL;
763
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200764 /*
765 * Allow the write-enable status to change only while the
766 * backing file is closed.
767 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100768 down_read(filesem);
769 if (fsg_lun_is_open(curlun)) {
770 LDBG(curlun, "read-only status change prevented\n");
771 rc = -EBUSY;
772 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300773 curlun->ro = ro;
774 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100775 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
776 }
777 up_read(filesem);
778 return rc;
779}
780
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300781static ssize_t fsg_store_nofua(struct device *dev,
782 struct device_attribute *attr,
783 const char *buf, size_t count)
784{
785 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
786 unsigned long nofua;
787
788 if (strict_strtoul(buf, 2, &nofua))
789 return -EINVAL;
790
791 /* Sync data when switching from async mode to sync */
792 if (!nofua && curlun->nofua)
793 fsg_lun_fsync_sub(curlun);
794
795 curlun->nofua = nofua;
796
797 return count;
798}
799
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100800static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
801 const char *buf, size_t count)
802{
803 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
804 struct rw_semaphore *filesem = dev_get_drvdata(dev);
805 int rc = 0;
806
807 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
808 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100809 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100810 }
811
812 /* Remove a trailing newline */
813 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100814 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100815
816 /* Eject current medium */
817 down_write(filesem);
818 if (fsg_lun_is_open(curlun)) {
819 fsg_lun_close(curlun);
820 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
821 }
822
823 /* Load new medium */
824 if (count > 0 && buf[0]) {
825 rc = fsg_lun_open(curlun, buf);
826 if (rc == 0)
827 curlun->unit_attention_data =
828 SS_NOT_READY_TO_READY_TRANSITION;
829 }
830 up_write(filesem);
831 return (rc < 0 ? rc : count);
832}